12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265 |
- #include <gst/gst.h>
- #include <glib.h>
- #include <stdio.h>
- #include <stdbool.h>
- #include <stdlib.h>
- #include <string.h>
- #include <sys/stat.h>
- #include <sys/time.h>
- #include <sys/timeb.h>
- #include <sys/types.h>
- #include <sys/inotify.h>
- #include <time.h>
- #include <unistd.h>
- #include <errno.h>
- #include "deepstream_app.h"
- #include "deepstream_config_file_parser.h"
- #include "nvds_version.h"
- #include <termios.h>
- #include <X11/Xlib.h>
- #include <X11/Xutil.h>
- #include "gstnvdsmeta.h"
- #include "nvdsmeta_schema.h"
- #include "deepstream_test5_app.h"
- #include "analytics.h"
- #define MAX_DISPLAY_LEN (64)
- #define MAX_TIME_STAMP_LEN (64)
- #define STREAMMUX_BUFFER_POOL_SIZE (16)
- typedef enum
- {
- APP_CONFIG_ANALYTICS_MODELS_UNKNOWN = 0,
- APP_CONFIG_ANALYTICS_RESNET_PGIE_3SGIE_TYPE_COLOR_MAKE = 1,
- } AppConfigAnalyticsModel;
- #define RESNET10_PGIE_3SGIE_TYPE_COLOR_MAKECLASS_ID_CAR (0)
- #ifdef GENERATE_DUMMY_META_EXT
- #define RESNET10_PGIE_3SGIE_TYPE_COLOR_MAKECLASS_ID_PERSON (2)
- #endif
- #define PERSON_ID 0
- #ifdef EN_DEBUG
- #define LOGD(...) printf(__VA_ARGS__)
- #else
- #define LOGD(...)
- #endif
- static TestAppCtx *testAppCtx;
- GST_DEBUG_CATEGORY (NVDS_APP);
- #define MAX_INSTANCES 128
- #define APP_TITLE "DeepStreamTest5App"
- #define DEFAULT_X_WINDOW_WIDTH 1920
- #define DEFAULT_X_WINDOW_HEIGHT 1080
- AppCtx *appCtx[MAX_INSTANCES];
- static guint cintr = FALSE;
- static GMainLoop *main_loop = NULL;
- static gchar **cfg_files = NULL;
- static gchar **input_files = NULL;
- static gchar **override_cfg_file = NULL;
- static gboolean playback_utc = TRUE;
- static gboolean print_version = FALSE;
- static gboolean show_bbox_text = TRUE;
- static gboolean force_tcp = TRUE;
- static gboolean print_dependencies_version = FALSE;
- static gboolean quit = FALSE;
- static gint return_value = 0;
- static guint num_instances;
- static guint num_input_files;
- static GMutex fps_lock;
- static gdouble fps[MAX_SOURCE_BINS];
- static gdouble fps_avg[MAX_SOURCE_BINS];
- static Display *display = NULL;
- static Window windows[MAX_INSTANCES] = { 0 };
- static GThread *x_event_thread = NULL;
- static GMutex disp_lock;
- static guint rrow, rcol, rcfg;
- static gboolean rrowsel = FALSE, selecting = FALSE;
- static AppConfigAnalyticsModel model_used = APP_CONFIG_ANALYTICS_MODELS_UNKNOWN;
- GOptionEntry entries[] = {
- {"version", 'v', 0, G_OPTION_ARG_NONE, &print_version,
- "Print DeepStreamSDK version", NULL}
- ,
- {"tiledtext", 't', 0, G_OPTION_ARG_NONE, &show_bbox_text,
- "Display Bounding box labels in tiled mode", NULL}
- ,
- {"version-all", 0, 0, G_OPTION_ARG_NONE, &print_dependencies_version,
- "Print DeepStreamSDK and dependencies version", NULL}
- ,
- {"cfg-file", 'c', 0, G_OPTION_ARG_FILENAME_ARRAY, &cfg_files,
- "Set the config file", NULL}
- ,
- {"override-cfg-file", 'o', 0, G_OPTION_ARG_FILENAME_ARRAY, &override_cfg_file,
- "Set the override config file, used for on-the-fly model update feature",
- NULL}
- ,
- {"input-file", 'i', 0, G_OPTION_ARG_FILENAME_ARRAY, &input_files,
- "Set the input file", NULL}
- ,
- {"playback-utc", 'p', 0, G_OPTION_ARG_INT, &playback_utc,
- "Playback utc; default=true (base UTC from file/rtsp URL); =false (base UTC from file-URL or RTCP Sender Report)",
- NULL}
- ,
- {"pgie-model-used", 'm', 0, G_OPTION_ARG_INT, &model_used,
- "PGIE Model used; {0 - Unknown [DEFAULT]}, {1: Resnet 4-class [Car, Bicycle, Person, Roadsign]}",
- NULL}
- ,
- {"no-force-tcp", 0, G_OPTION_FLAG_REVERSE, G_OPTION_ARG_NONE, &force_tcp,
- "Do not force TCP for RTP transport", NULL}
- ,
- {NULL}
- ,
- };
- static void
- generate_ts_rfc3339 (char *buf, int buf_size)
- {
- time_t tloc;
- struct tm tm_log;
- struct timespec ts;
- char strmsec[6];
- clock_gettime (CLOCK_REALTIME, &ts);
- memcpy (&tloc, (void *) (&ts.tv_sec), sizeof (time_t));
- gmtime_r (&tloc, &tm_log);
- strftime (buf, buf_size, "%Y-%m-%dT%H:%M:%S", &tm_log);
- int ms = ts.tv_nsec / 1000000;
- g_snprintf (strmsec, sizeof (strmsec), ".%.3dZ", ms);
- strncat (buf, strmsec, buf_size);
- }
- static GstClockTime
- generate_ts_rfc3339_from_ts (char *buf, int buf_size, GstClockTime ts,
- gchar * src_uri, gint stream_id)
- {
- time_t tloc;
- struct tm tm_log;
- char strmsec[6];
- int ms;
- GstClockTime ts_generated;
- if (playback_utc
- || (appCtx[0]->config.multi_source_config[stream_id].type !=
- NV_DS_SOURCE_RTSP)) {
- if (testAppCtx->streams[stream_id].meta_number == 0) {
- testAppCtx->streams[stream_id].timespec_first_frame =
- extract_utc_from_uri (src_uri);
- memcpy (&tloc,
- (void *) (&testAppCtx->streams[stream_id].timespec_first_frame.
- tv_sec), sizeof (time_t));
- ms = testAppCtx->streams[stream_id].timespec_first_frame.tv_nsec /
- 1000000;
- testAppCtx->streams[stream_id].gst_ts_first_frame = ts;
- ts_generated =
- GST_TIMESPEC_TO_TIME (testAppCtx->streams[stream_id].
- timespec_first_frame);
- if (ts_generated == 0) {
- g_print
- ("WARNING; playback mode used with URI [%s] not conforming to timestamp format;"
- " check README; using system-time\n", src_uri);
- clock_gettime (CLOCK_REALTIME,
- &testAppCtx->streams[stream_id].timespec_first_frame);
- ts_generated =
- GST_TIMESPEC_TO_TIME (testAppCtx->streams[stream_id].
- timespec_first_frame);
- }
- } else {
- GstClockTime ts_current =
- GST_TIMESPEC_TO_TIME (testAppCtx->
- streams[stream_id].timespec_first_frame) + (ts -
- testAppCtx->streams[stream_id].gst_ts_first_frame);
- struct timespec timespec_current;
- GST_TIME_TO_TIMESPEC (ts_current, timespec_current);
- memcpy (&tloc, (void *) (×pec_current.tv_sec), sizeof (time_t));
- ms = timespec_current.tv_nsec / 1000000;
- ts_generated = ts_current;
- }
- } else {
-
- struct timespec timespec_current;
- GST_TIME_TO_TIMESPEC (ts, timespec_current);
- memcpy (&tloc, (void *) (×pec_current.tv_sec), sizeof (time_t));
- ms = timespec_current.tv_nsec / 1000000;
- ts_generated = ts;
- }
- gmtime_r (&tloc, &tm_log);
- strftime (buf, buf_size, "%Y-%m-%dT%H:%M:%S", &tm_log);
- g_snprintf (strmsec, sizeof (strmsec), ".%.3dZ", ms);
- strncat (buf, strmsec, buf_size);
- LOGD ("ts=%s\n", buf);
- return ts_generated;
- }
- static gpointer
- meta_copy_func (gpointer data, gpointer user_data)
- {
- NvDsUserMeta *user_meta = (NvDsUserMeta *) data;
- NvDsEventMsgMeta *srcMeta = (NvDsEventMsgMeta *) user_meta->user_meta_data;
- NvDsEventMsgMeta *dstMeta = NULL;
- dstMeta = g_memdup (srcMeta, sizeof (NvDsEventMsgMeta));
- if (srcMeta->ts)
- dstMeta->ts = g_strdup (srcMeta->ts);
- if (srcMeta->objSignature.size > 0) {
- dstMeta->objSignature.signature = g_memdup (srcMeta->objSignature.signature,
- srcMeta->objSignature.size);
- dstMeta->objSignature.size = srcMeta->objSignature.size;
- }
- if (srcMeta->objectId) {
- dstMeta->objectId = g_strdup (srcMeta->objectId);
- }
-
- if (srcMeta->extMsgSize > 0) {
- if (srcMeta->objType == NVDS_OBJECT_TYPE_PERSON) {
- NvDsPersonObject *srcObj = (NvDsPersonObject *) srcMeta->extMsg;
- NvDsPersonObject *obj =
- (NvDsPersonObject *) g_malloc0 (sizeof (NvDsPersonObject));
- obj->age = srcObj->age;
- if (srcObj->gender)
- obj->gender = g_strdup (srcObj->gender);
- if (srcObj->cap)
- obj->cap = g_strdup (srcObj->cap);
- if (srcObj->hair)
- obj->hair = g_strdup (srcObj->hair);
- if (srcObj->apparel)
- obj->apparel = g_strdup (srcObj->apparel);
- dstMeta->extMsg = obj;
- dstMeta->extMsgSize = sizeof (NvDsPersonObject);
- }
- }
- return dstMeta;
- }
- static void
- meta_free_func (gpointer data, gpointer user_data)
- {
- NvDsUserMeta *user_meta = (NvDsUserMeta *) data;
- NvDsEventMsgMeta *srcMeta = (NvDsEventMsgMeta *) user_meta->user_meta_data;
- user_meta->user_meta_data = NULL;
- if (srcMeta->ts) {
- g_free (srcMeta->ts);
- }
- if (srcMeta->objSignature.size > 0) {
- g_free (srcMeta->objSignature.signature);
- srcMeta->objSignature.size = 0;
- }
- if (srcMeta->objectId) {
- g_free (srcMeta->objectId);
- }
- if (srcMeta->sensorStr) {
- g_free (srcMeta->sensorStr);
- }
- if (srcMeta->extMsgSize > 0) {
- if (srcMeta->objType == NVDS_OBJECT_TYPE_VEHICLE) {
- NvDsVehicleObject *obj = (NvDsVehicleObject *) srcMeta->extMsg;
- if (obj->type)
- g_free (obj->type);
- if (obj->color)
- g_free (obj->color);
- if (obj->make)
- g_free (obj->make);
- if (obj->model)
- g_free (obj->model);
- if (obj->license)
- g_free (obj->license);
- if (obj->region)
- g_free (obj->region);
- } else if (srcMeta->objType == NVDS_OBJECT_TYPE_PERSON) {
- NvDsPersonObject *obj = (NvDsPersonObject *) srcMeta->extMsg;
- if (obj->gender)
- g_free (obj->gender);
- if (obj->cap)
- g_free (obj->cap);
- if (obj->hair)
- g_free (obj->hair);
- if (obj->apparel)
- g_free (obj->apparel);
- }
- g_free (srcMeta->extMsg);
- srcMeta->extMsg = NULL;
- srcMeta->extMsgSize = 0;
- }
- g_free (srcMeta);
- }
- #ifdef GENERATE_DUMMY_META_EXT
- #endif
- static void
- generate_event_msg_meta (gpointer data, gint class_id, gboolean useTs,
- GstClockTime ts, gchar * src_uri, gint stream_id, guint sensor_id,
- AnalyticsUserMeta * obj_params, float scaleW, float scaleH,
- NvDsFrameMeta * frame_meta)
- {
- NvDsEventMsgMeta *meta = (NvDsEventMsgMeta *) data;
- GstClockTime ts_generated = 0;
- meta->objType = NVDS_OBJECT_TYPE_UNKNOWN;
- meta->frameId = frame_meta->frame_num;
- meta->ts = (gchar *) g_malloc0 (MAX_TIME_STAMP_LEN + 1);
- meta->objectId = (gchar *) g_malloc0 (MAX_LABEL_SIZE);
-
-
- if (useTs && src_uri) {
- ts_generated =
- generate_ts_rfc3339_from_ts (meta->ts, MAX_TIME_STAMP_LEN, ts, src_uri,
- stream_id);
- } else {
- generate_ts_rfc3339 (meta->ts, MAX_TIME_STAMP_LEN);
- }
-
- meta->trackingId = class_id;
- (void) ts_generated;
- meta->type = NVDS_EVENT_ENTRY;
- meta->objType = NVDS_OBJECT_TYPE_PERSON;
- meta->objClassId = PERSON_ID;
- meta->occupancy = obj_params->lccum_cnt;
- meta->lccum_cnt_entry = obj_params->lcc_cnt_entry;
- meta->lccum_cnt_exit = obj_params->lcc_cnt_exit ;
- meta->source_id = obj_params->source_id;
- }
- void analytics_custom_parse_nvdsanalytics_meta_data (NvDsMetaList *l_user, AnalyticsUserMeta *data);
- static void
- bbox_generated_probe_after_analytics (AppCtx * appCtx, GstBuffer * buf,
- NvDsBatchMeta * batch_meta, guint index)
- {
- NvDsObjectMeta *obj_meta = NULL;
- GstClockTime buffer_pts = 0;
- guint32 stream_id = 0;
- if (!appCtx->config.dsanalytics_config.enable){
- g_print ("Unable to get nvdsanalytics src pad\n");
- return;
- }
- for (NvDsMetaList * l_frame = batch_meta->frame_meta_list; l_frame != NULL;
- l_frame = l_frame->next) {
- NvDsFrameMeta *frame_meta = l_frame->data;
- stream_id = frame_meta->source_id;
- GstClockTime buf_ntp_time = 0;
- if (playback_utc == FALSE) {
-
- StreamSourceInfo *src_stream = &testAppCtx->streams[stream_id];
- buf_ntp_time = frame_meta->ntp_timestamp;
- if (buf_ntp_time < src_stream->last_ntp_time) {
- NVGSTDS_WARN_MSG_V ("Source %d: NTP timestamps are backward in time."
- " Current: %lu previous: %lu", stream_id, buf_ntp_time,
- src_stream->last_ntp_time);
- }
- src_stream->last_ntp_time = buf_ntp_time;
- }
- GList *l;
- NvDsMetaList *l_analyticsuser;
- l_analyticsuser = frame_meta->frame_user_meta_list;
- AnalyticsUserMeta *user_data =
- (AnalyticsUserMeta *) g_malloc0(sizeof(AnalyticsUserMeta));
- if (l_analyticsuser != NULL) {
- analytics_custom_parse_nvdsanalytics_meta_data(l_analyticsuser, user_data);
- }
- user_data->source_id = stream_id;
-
-
- for (l = frame_meta->obj_meta_list; l != NULL; l = l->next) {
-
- obj_meta = (NvDsObjectMeta *) (l->data);
- {
-
- float scaleW = 0;
- float scaleH = 0;
-
- buffer_pts = frame_meta->buf_pts;
- if (!appCtx->config.streammux_config.pipeline_width
- || !appCtx->config.streammux_config.pipeline_height) {
- g_print ("invalid pipeline params\n");
- return;
- }
- LOGD ("stream %d==%d [%d X %d]\n", frame_meta->source_id,
- frame_meta->pad_index, frame_meta->source_frame_width,
- frame_meta->source_frame_height);
- scaleW =
- (float) frame_meta->source_frame_width /
- appCtx->config.streammux_config.pipeline_width;
- scaleH =
- (float) frame_meta->source_frame_height /
- appCtx->config.streammux_config.pipeline_height;
- if (playback_utc == FALSE) {
-
- buffer_pts = buf_ntp_time;
- }
-
- NvDsEventMsgMeta *msg_meta =
- (NvDsEventMsgMeta *) g_malloc0 (sizeof (NvDsEventMsgMeta));
- generate_event_msg_meta (msg_meta, PERSON_ID, TRUE,
-
- buffer_pts,
- appCtx->config.multi_source_config[stream_id].uri, stream_id,
- appCtx->config.multi_source_config[stream_id].camera_id,
- user_data, scaleW, scaleH, frame_meta);
- testAppCtx->streams[stream_id].meta_number++;
- NvDsUserMeta *user_event_meta =
- nvds_acquire_user_meta_from_pool (batch_meta);
- if (user_event_meta) {
-
- user_event_meta->user_meta_data = (void *) msg_meta;
- user_event_meta->base_meta.batch_meta = batch_meta;
- user_event_meta->base_meta.meta_type = NVDS_EVENT_MSG_META;
- user_event_meta->base_meta.copy_func =
- (NvDsMetaCopyFunc) meta_copy_func;
- user_event_meta->base_meta.release_func =
- (NvDsMetaReleaseFunc) meta_free_func;
- nvds_add_user_meta_to_frame (frame_meta, user_event_meta);
- } else {
- g_print ("Error in attaching event meta to buffer\n");
- }
- }
- }
- testAppCtx->streams[stream_id].frameCount++;
- g_free(user_data);
- }
- }
- static void
- _intr_handler (int signum)
- {
- struct sigaction action;
- NVGSTDS_ERR_MSG_V ("User Interrupted.. \n");
- memset (&action, 0, sizeof (action));
- action.sa_handler = SIG_DFL;
- sigaction (SIGINT, &action, NULL);
- cintr = TRUE;
- }
- static void
- perf_cb (gpointer context, NvDsAppPerfStruct * str)
- {
- static guint header_print_cnt = 0;
- guint i;
- AppCtx *appCtx = (AppCtx *) context;
- guint numf = str->num_instances;
- g_mutex_lock (&fps_lock);
- for (i = 0; i < numf; i++) {
- fps[i] = str->fps[i];
- fps_avg[i] = str->fps_avg[i];
- }
- if (header_print_cnt % 20 == 0) {
- g_print ("\n**PERF: ");
- for (i = 0; i < numf; i++) {
- g_print ("FPS %d (Avg)\t", i);
- }
- g_print ("\n");
- header_print_cnt = 0;
- }
- header_print_cnt++;
- time_t t = time (NULL);
- struct tm *tm = localtime (&t);
- printf ("%s", asctime (tm));
- if (num_instances > 1)
- g_print ("PERF(%d): ", appCtx->index);
- else
- g_print ("**PERF: ");
- for (i = 0; i < numf; i++) {
- g_print ("%.2f (%.2f)\t", fps[i], fps_avg[i]);
- }
- g_print ("\n");
- g_mutex_unlock (&fps_lock);
- }
- static gboolean
- check_for_interrupt (gpointer data)
- {
- if (quit) {
- return FALSE;
- }
- if (cintr) {
- cintr = FALSE;
- quit = TRUE;
- g_main_loop_quit (main_loop);
- return FALSE;
- }
- return TRUE;
- }
- static void
- _intr_setup (void)
- {
- struct sigaction action;
- memset (&action, 0, sizeof (action));
- action.sa_handler = _intr_handler;
- sigaction (SIGINT, &action, NULL);
- }
- static gboolean
- kbhit (void)
- {
- struct timeval tv;
- fd_set rdfs;
- tv.tv_sec = 0;
- tv.tv_usec = 0;
- FD_ZERO (&rdfs);
- FD_SET (STDIN_FILENO, &rdfs);
- select (STDIN_FILENO + 1, &rdfs, NULL, NULL, &tv);
- return FD_ISSET (STDIN_FILENO, &rdfs);
- }
- static void
- changemode (int dir)
- {
- static struct termios oldt, newt;
- if (dir == 1) {
- tcgetattr (STDIN_FILENO, &oldt);
- newt = oldt;
- newt.c_lflag &= ~(ICANON);
- tcsetattr (STDIN_FILENO, TCSANOW, &newt);
- } else
- tcsetattr (STDIN_FILENO, TCSANOW, &oldt);
- }
- static void
- print_runtime_commands (void)
- {
- g_print ("\nRuntime commands:\n"
- "\th: Print this help\n"
- "\tq: Quit\n\n" "\tp: Pause\n" "\tr: Resume\n\n");
- if (appCtx[0]->config.tiled_display_config.enable) {
- g_print
- ("NOTE: To expand a source in the 2D tiled display and view object details,"
- " left-click on the source.\n"
- " To go back to the tiled display, right-click anywhere on the window.\n\n");
- }
- }
- static gboolean
- event_thread_func (gpointer arg)
- {
- guint i;
- gboolean ret = TRUE;
-
- for (i = 0; i < num_instances; i++) {
- if (!appCtx[i]->quit)
- break;
- }
- if (i == num_instances) {
- quit = TRUE;
- g_main_loop_quit (main_loop);
- return FALSE;
- }
-
- if (!kbhit ()) {
-
- return TRUE;
- }
- int c = fgetc (stdin);
- g_print ("\n");
- gint source_id;
- GstElement *tiler = appCtx[rcfg]->pipeline.tiled_display_bin.tiler;
- g_object_get (G_OBJECT (tiler), "show-source", &source_id, NULL);
- if (selecting) {
- if (rrowsel == FALSE) {
- if (c >= '0' && c <= '9') {
- rrow = c - '0';
- g_print ("--selecting source row %d--\n", rrow);
- rrowsel = TRUE;
- }
- } else {
- if (c >= '0' && c <= '9') {
- int tile_num_columns = appCtx[rcfg]->config.tiled_display_config.columns;
- rcol = c - '0';
- selecting = FALSE;
- rrowsel = FALSE;
- source_id = tile_num_columns * rrow + rcol;
- g_print ("--selecting source col %d sou=%d--\n", rcol, source_id);
- if (source_id >= (gint) appCtx[rcfg]->config.num_source_sub_bins) {
- source_id = -1;
- } else {
- appCtx[rcfg]->show_bbox_text = TRUE;
- appCtx[rcfg]->active_source_index = source_id;
- g_object_set (G_OBJECT (tiler), "show-source", source_id, NULL);
- }
- }
- }
- }
- switch (c) {
- case 'h':
- print_runtime_commands ();
- break;
- case 'p':
- for (i = 0; i < num_instances; i++)
- pause_pipeline (appCtx[i]);
- break;
- case 'r':
- for (i = 0; i < num_instances; i++)
- resume_pipeline (appCtx[i]);
- break;
- case 'q':
- quit = TRUE;
- g_main_loop_quit (main_loop);
- ret = FALSE;
- break;
- case 'c':
- if (selecting == FALSE && source_id == -1) {
- g_print("--selecting config file --\n");
- c = fgetc(stdin);
- if (c >= '0' && c <= '9') {
- rcfg = c - '0';
- if (rcfg < num_instances) {
- g_print("--selecting config %d--\n", rcfg);
- } else {
- g_print("--selected config file %d out of bound, reenter\n", rcfg);
- rcfg = 0;
- }
- }
- }
- break;
- case 'z':
- if (source_id == -1 && selecting == FALSE) {
- g_print ("--selecting source --\n");
- selecting = TRUE;
- } else {
- if (!show_bbox_text) {
- GstElement *nvosd = appCtx[rcfg]->pipeline.instance_bins[0].osd_bin.nvosd;
- g_object_set (G_OBJECT (nvosd), "display-text", FALSE, NULL);
- g_object_set (G_OBJECT (tiler), "show-source", -1, NULL);
- }
- appCtx[rcfg]->active_source_index = -1;
- selecting = FALSE;
- rcfg = 0;
- g_print("--tiled mode --\n");
- }
- break;
- default:
- break;
- }
- return ret;
- }
- static int
- get_source_id_from_coordinates (float x_rel, float y_rel, AppCtx *appCtx)
- {
- int tile_num_rows = appCtx->config.tiled_display_config.rows;
- int tile_num_columns = appCtx->config.tiled_display_config.columns;
- int source_id = (int) (x_rel * tile_num_columns);
- source_id += ((int) (y_rel * tile_num_rows)) * tile_num_columns;
-
- if (source_id >= (gint) appCtx->config.num_source_sub_bins)
- source_id = -1;
- return source_id;
- }
- static gpointer
- nvds_x_event_thread (gpointer data)
- {
- g_mutex_lock (&disp_lock);
- while (display) {
- XEvent e;
- guint index;
- while (XPending (display)) {
- XNextEvent (display, &e);
- switch (e.type) {
- case ButtonPress:
- {
- XWindowAttributes win_attr;
- XButtonEvent ev = e.xbutton;
- gint source_id;
- GstElement *tiler;
- XGetWindowAttributes (display, ev.window, &win_attr);
- for (index = 0; index < MAX_INSTANCES; index++)
- if (ev.window == windows[index])
- break;
- tiler = appCtx[index]->pipeline.tiled_display_bin.tiler;
- g_object_get (G_OBJECT (tiler), "show-source", &source_id, NULL);
- if (ev.button == Button1 && source_id == -1) {
- source_id =
- get_source_id_from_coordinates (ev.x * 1.0 / win_attr.width,
- ev.y * 1.0 / win_attr.height, appCtx[index]);
- if (source_id > -1) {
- g_object_set (G_OBJECT (tiler), "show-source", source_id, NULL);
- appCtx[index]->active_source_index = source_id;
- appCtx[index]->show_bbox_text = TRUE;
- GstElement *nvosd = appCtx[index]->pipeline.instance_bins[0].osd_bin.nvosd;
- g_object_set (G_OBJECT (nvosd), "display-text", TRUE, NULL);
- }
- } else if (ev.button == Button3) {
- g_object_set (G_OBJECT (tiler), "show-source", -1, NULL);
- appCtx[index]->active_source_index = -1;
- if (!show_bbox_text) {
- appCtx[index]->show_bbox_text = FALSE;
- GstElement *nvosd = appCtx[index]->pipeline.instance_bins[0].osd_bin.nvosd;
- g_object_set (G_OBJECT (nvosd), "display-text", FALSE, NULL);
- }
- }
- }
- break;
- case KeyRelease:
- {
- KeySym p, r, q;
- guint i;
- p = XKeysymToKeycode (display, XK_P);
- r = XKeysymToKeycode (display, XK_R);
- q = XKeysymToKeycode (display, XK_Q);
- if (e.xkey.keycode == p) {
- for (i = 0; i < num_instances; i++)
- pause_pipeline (appCtx[i]);
- break;
- }
- if (e.xkey.keycode == r) {
- for (i = 0; i < num_instances; i++)
- resume_pipeline (appCtx[i]);
- break;
- }
- if (e.xkey.keycode == q) {
- quit = TRUE;
- g_main_loop_quit (main_loop);
- }
- }
- break;
- case ClientMessage:
- {
- Atom wm_delete;
- for (index = 0; index < MAX_INSTANCES; index++)
- if (e.xclient.window == windows[index])
- break;
- wm_delete = XInternAtom (display, "WM_DELETE_WINDOW", 1);
- if (wm_delete != None && wm_delete == (Atom) e.xclient.data.l[0]) {
- quit = TRUE;
- g_main_loop_quit (main_loop);
- }
- }
- break;
- }
- }
- g_mutex_unlock (&disp_lock);
- g_usleep (G_USEC_PER_SEC / 20);
- g_mutex_lock (&disp_lock);
- }
- g_mutex_unlock (&disp_lock);
- return NULL;
- }
- static gboolean
- overlay_graphics (AppCtx * appCtx, GstBuffer * buf,
- NvDsBatchMeta * batch_meta, guint index)
- {
- return TRUE;
- }
- int
- main (int argc, char *argv[])
- {
- testAppCtx = (TestAppCtx *) g_malloc0 (sizeof (TestAppCtx));
- GOptionContext *ctx = NULL;
- GOptionGroup *group = NULL;
- GError *error = NULL;
- guint i;
- ctx = g_option_context_new ("Nvidia DeepStream Test5");
- group = g_option_group_new ("abc", NULL, NULL, NULL, NULL);
- g_option_group_add_entries (group, entries);
- g_option_context_set_main_group (ctx, group);
- g_option_context_add_group (ctx, gst_init_get_option_group ());
- GST_DEBUG_CATEGORY_INIT (NVDS_APP, "NVDS_APP", 0, NULL);
- if (!g_option_context_parse (ctx, &argc, &argv, &error)) {
- NVGSTDS_ERR_MSG_V ("%s", error->message);
- g_print ("%s",g_option_context_get_help (ctx, TRUE, NULL));
- return -1;
- }
- if (print_version) {
- g_print ("deepstream-test5-app version %d.%d.%d\n",
- NVDS_APP_VERSION_MAJOR, NVDS_APP_VERSION_MINOR, NVDS_APP_VERSION_MICRO);
- return 0;
- }
- if (print_dependencies_version) {
- g_print ("deepstream-test5-app version %d.%d.%d\n",
- NVDS_APP_VERSION_MAJOR, NVDS_APP_VERSION_MINOR, NVDS_APP_VERSION_MICRO);
- return 0;
- }
- if (cfg_files) {
- num_instances = g_strv_length (cfg_files);
- }
- if (input_files) {
- num_input_files = g_strv_length (input_files);
- }
- if (!cfg_files || num_instances == 0) {
- NVGSTDS_ERR_MSG_V ("Specify config file with -c option");
- return_value = -1;
- goto done;
- }
- for (i = 0; i < num_instances; i++) {
- appCtx[i] = (AppCtx *) g_malloc0 (sizeof (AppCtx));
- appCtx[i]->person_class_id = -1;
- appCtx[i]->car_class_id = -1;
- appCtx[i]->index = i;
- appCtx[i]->active_source_index = -1;
- if (show_bbox_text) {
- appCtx[i]->show_bbox_text = TRUE;
- }
- if (input_files && input_files[i]) {
- appCtx[i]->config.multi_source_config[0].uri =
- g_strdup_printf ("file://%s", input_files[i]);
- g_free (input_files[i]);
- }
- if (!parse_config_file (&appCtx[i]->config, cfg_files[i])) {
- NVGSTDS_ERR_MSG_V ("Failed to parse config file '%s'", cfg_files[i]);
- appCtx[i]->return_value = -1;
- goto done;
- }
- if (override_cfg_file && override_cfg_file[i]) {
- if (!g_file_test (override_cfg_file[i],
- G_FILE_TEST_IS_REGULAR | G_FILE_TEST_IS_SYMLINK))
- {
- g_print ("Override file %s does not exist, quitting...\n",
- override_cfg_file[i]);
- appCtx[i]->return_value = -1;
- goto done;
- }
- }
- }
- for (i = 0; i < num_instances; i++) {
- for (guint j = 0; j < appCtx[i]->config.num_source_sub_bins; j++) {
-
- if (force_tcp)
- appCtx[i]->config.multi_source_config[j].select_rtp_protocol = 0x04;
- }
- if (!create_pipeline (appCtx[i], bbox_generated_probe_after_analytics,
- NULL, perf_cb, overlay_graphics)) {
- NVGSTDS_ERR_MSG_V ("Failed to create pipeline");
- return_value = -1;
- goto done;
- }
-
-
- for (guint j = 0; j < appCtx[i]->pipeline.multi_src_bin.num_bins; j++) {
- testAppCtx->streams[j].id = j;
- }
-
- g_object_set (appCtx[i]->pipeline.multi_src_bin.streammux,
- "buffer-pool-size", STREAMMUX_BUFFER_POOL_SIZE, NULL);
- }
- main_loop = g_main_loop_new (NULL, FALSE);
- _intr_setup ();
- g_timeout_add (400, check_for_interrupt, NULL);
- g_mutex_init (&disp_lock);
- display = XOpenDisplay (NULL);
- for (i = 0; i < num_instances; i++) {
- guint j;
- if (!show_bbox_text) {
- GstElement *nvosd = appCtx[i]->pipeline.instance_bins[0].osd_bin.nvosd;
- g_object_set(G_OBJECT(nvosd), "display-text", FALSE, NULL);
- }
- if (gst_element_set_state (appCtx[i]->pipeline.pipeline,
- GST_STATE_PAUSED) == GST_STATE_CHANGE_FAILURE) {
- NVGSTDS_ERR_MSG_V ("Failed to set pipeline to PAUSED");
- return_value = -1;
- goto done;
- }
- if (!appCtx[i]->config.tiled_display_config.enable)
- continue;
- for (j = 0; j < appCtx[i]->config.num_sink_sub_bins; j++) {
- XTextProperty xproperty;
- gchar *title;
- guint width, height;
- XSizeHints hints = {0};
- if (!GST_IS_VIDEO_OVERLAY (appCtx[i]->pipeline.instance_bins[0].sink_bin.
- sub_bins[j].sink)) {
- continue;
- }
- if (!display) {
- NVGSTDS_ERR_MSG_V ("Could not open X Display");
- return_value = -1;
- goto done;
- }
- if (appCtx[i]->config.sink_bin_sub_bin_config[j].render_config.width)
- width =
- appCtx[i]->config.sink_bin_sub_bin_config[j].render_config.width;
- else
- width = appCtx[i]->config.tiled_display_config.width;
- if (appCtx[i]->config.sink_bin_sub_bin_config[j].render_config.height)
- height =
- appCtx[i]->config.sink_bin_sub_bin_config[j].render_config.height;
- else
- height = appCtx[i]->config.tiled_display_config.height;
- width = (width) ? width : DEFAULT_X_WINDOW_WIDTH;
- height = (height) ? height : DEFAULT_X_WINDOW_HEIGHT;
- hints.flags = PPosition | PSize;
- hints.x = appCtx[i]->config.sink_bin_sub_bin_config[j].render_config.offset_x;
- hints.y = appCtx[i]->config.sink_bin_sub_bin_config[j].render_config.offset_y;
- hints.width = width;
- hints.height = height;
- windows[i] =
- XCreateSimpleWindow (display, RootWindow (display,
- DefaultScreen (display)), hints.x, hints.y, width, height, 2,
- 0x00000000, 0x00000000);
- XSetNormalHints(display, windows[i], &hints);
- if (num_instances > 1)
- title = g_strdup_printf (APP_TITLE "-%d", i);
- else
- title = g_strdup (APP_TITLE);
- if (XStringListToTextProperty ((char **) &title, 1, &xproperty) != 0) {
- XSetWMName (display, windows[i], &xproperty);
- XFree (xproperty.value);
- }
- XSetWindowAttributes attr = { 0 };
- if ((appCtx[i]->config.tiled_display_config.enable &&
- appCtx[i]->config.tiled_display_config.rows *
- appCtx[i]->config.tiled_display_config.columns == 1) ||
- (appCtx[i]->config.tiled_display_config.enable == 0 &&
- appCtx[i]->config.num_source_sub_bins == 1)) {
- } else {
- attr.event_mask = ButtonPress | KeyRelease;
- }
- XChangeWindowAttributes (display, windows[i], CWEventMask, &attr);
- Atom wmDeleteMessage = XInternAtom (display, "WM_DELETE_WINDOW", False);
- if (wmDeleteMessage != None) {
- XSetWMProtocols (display, windows[i], &wmDeleteMessage, 1);
- }
- XMapRaised (display, windows[i]);
- XSync (display, 1);
- gst_video_overlay_set_window_handle (GST_VIDEO_OVERLAY (appCtx
- [i]->pipeline.instance_bins[0].sink_bin.sub_bins[j].sink),
- (gulong) windows[i]);
- gst_video_overlay_expose (GST_VIDEO_OVERLAY (appCtx[i]->pipeline.
- instance_bins[0].sink_bin.sub_bins[j].sink));
- if (!x_event_thread)
- x_event_thread = g_thread_new ("nvds-window-event-thread",
- nvds_x_event_thread, NULL);
- }
- }
-
- if (return_value != -1) {
- for (i = 0; i < num_instances; i++) {
- if (gst_element_set_state (appCtx[i]->pipeline.pipeline,
- GST_STATE_PLAYING) == GST_STATE_CHANGE_FAILURE) {
- g_print ("\ncan't set pipeline to playing state.\n");
- return_value = -1;
- goto done;
- }
- }
- }
- print_runtime_commands ();
- changemode (1);
- g_timeout_add (40, event_thread_func, NULL);
- g_main_loop_run (main_loop);
- changemode (0);
- done:
- g_print ("Quitting\n");
- for (i = 0; i < num_instances; i++) {
- if (appCtx[i] == NULL)
- continue;
- if (appCtx[i]->return_value == -1)
- return_value = -1;
- destroy_pipeline (appCtx[i]);
- g_mutex_lock (&disp_lock);
- if (windows[i])
- XDestroyWindow (display, windows[i]);
- windows[i] = 0;
- g_mutex_unlock (&disp_lock);
- g_free (appCtx[i]);
- }
- g_mutex_lock (&disp_lock);
- if (display)
- XCloseDisplay (display);
- display = NULL;
- g_mutex_unlock (&disp_lock);
- g_mutex_clear (&disp_lock);
- if (main_loop) {
- g_main_loop_unref (main_loop);
- }
- if (ctx) {
- g_option_context_free (ctx);
- }
- if (return_value == 0) {
- g_print ("App run successful\n");
- } else {
- g_print ("App run failed\n");
- }
- gst_deinit ();
- return return_value;
- g_free (testAppCtx);
- return 0;
- }
- static gchar *
- get_first_result_label (NvDsClassifierMeta * classifierMeta)
- {
- GList *n;
- for (n = classifierMeta->label_info_list; n != NULL; n = n->next) {
- NvDsLabelInfo *labelInfo = (NvDsLabelInfo *) (n->data);
- if (labelInfo->result_label[0] != '\0') {
- return g_strdup (labelInfo->result_label);
- }
- }
- return NULL;
- }
|