deepstream_test5_app_main.c 36 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265
  1. /*
  2. * Copyright (c) 2019-2020, NVIDIA CORPORATION. All rights reserved.
  3. *
  4. * Permission is hereby granted, free of charge, to any person obtaining a
  5. * copy of this software and associated documentation files (the "Software"),
  6. * to deal in the Software without restriction, including without limitation
  7. * the rights to use, copy, modify, merge, publish, distribute, sublicense,
  8. * and/or sell copies of the Software, and to permit persons to whom the
  9. * Software is furnished to do so, subject to the following conditions:
  10. *
  11. * The above copyright notice and this permission notice shall be included in
  12. * all copies or substantial portions of the Software.
  13. *
  14. * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
  15. * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
  16. * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
  17. * THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
  18. * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
  19. * FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
  20. * DEALINGS IN THE SOFTWARE.
  21. */
  22. #include <gst/gst.h>
  23. #include <glib.h>
  24. #include <stdio.h>
  25. #include <stdbool.h>
  26. #include <stdlib.h>
  27. #include <string.h>
  28. #include <sys/stat.h>
  29. #include <sys/time.h>
  30. #include <sys/timeb.h>
  31. #include <sys/types.h>
  32. #include <sys/inotify.h>
  33. #include <time.h>
  34. #include <unistd.h>
  35. #include <errno.h>
  36. #include "deepstream_app.h"
  37. #include "deepstream_config_file_parser.h"
  38. #include "nvds_version.h"
  39. #include <termios.h>
  40. #include <X11/Xlib.h>
  41. #include <X11/Xutil.h>
  42. #include "gstnvdsmeta.h"
  43. #include "nvdsmeta_schema.h"
  44. #include "deepstream_test5_app.h"
  45. /*Analytics header*/
  46. #include "analytics.h"
  47. #define MAX_DISPLAY_LEN (64)
  48. #define MAX_TIME_STAMP_LEN (64)
  49. #define STREAMMUX_BUFFER_POOL_SIZE (16)
  50. /** @{
  51. * Macro's below and corresponding code-blocks are used to demonstrate
  52. * nvmsgconv + Broker Metadata manipulation possibility
  53. */
  54. /**
  55. * IMPORTANT Note 1:
  56. * The code within the check for model_used == APP_CONFIG_ANALYTICS_RESNET_PGIE_3SGIE_TYPE_COLOR_MAKE
  57. * is applicable as sample demo code for
  58. * configs that use resnet PGIE model
  59. * with class ID's: {0, 1, 2, 3} for {CAR, BICYCLE, PERSON, ROADSIGN}
  60. * followed by optional Tracker + 3 X SGIEs (Vehicle-Type,Color,Make)
  61. * only!
  62. * Please comment out the code if using any other
  63. * custom PGIE + SGIE combinations
  64. * and use the code as reference to write your own
  65. * NvDsEventMsgMeta generation code in generate_event_msg_meta()
  66. * function
  67. */
  68. typedef enum
  69. {
  70. APP_CONFIG_ANALYTICS_MODELS_UNKNOWN = 0,
  71. APP_CONFIG_ANALYTICS_RESNET_PGIE_3SGIE_TYPE_COLOR_MAKE = 1,
  72. } AppConfigAnalyticsModel;
  73. #define RESNET10_PGIE_3SGIE_TYPE_COLOR_MAKECLASS_ID_CAR (0)
  74. #ifdef GENERATE_DUMMY_META_EXT
  75. #define RESNET10_PGIE_3SGIE_TYPE_COLOR_MAKECLASS_ID_PERSON (2)
  76. #endif
  77. /** @} */
  78. /* PERSON ID definition. */
  79. #define PERSON_ID 0
  80. #ifdef EN_DEBUG
  81. #define LOGD(...) printf(__VA_ARGS__)
  82. #else
  83. #define LOGD(...)
  84. #endif
  85. static TestAppCtx *testAppCtx;
  86. GST_DEBUG_CATEGORY (NVDS_APP);
  87. /** @{ imported from deepstream-app as is */
  88. #define MAX_INSTANCES 128
  89. #define APP_TITLE "DeepStreamTest5App"
  90. #define DEFAULT_X_WINDOW_WIDTH 1920
  91. #define DEFAULT_X_WINDOW_HEIGHT 1080
  92. AppCtx *appCtx[MAX_INSTANCES];
  93. static guint cintr = FALSE;
  94. static GMainLoop *main_loop = NULL;
  95. static gchar **cfg_files = NULL;
  96. static gchar **input_files = NULL;
  97. static gchar **override_cfg_file = NULL;
  98. static gboolean playback_utc = TRUE;
  99. static gboolean print_version = FALSE;
  100. static gboolean show_bbox_text = TRUE;
  101. static gboolean force_tcp = TRUE;
  102. static gboolean print_dependencies_version = FALSE;
  103. static gboolean quit = FALSE;
  104. static gint return_value = 0;
  105. static guint num_instances;
  106. static guint num_input_files;
  107. static GMutex fps_lock;
  108. static gdouble fps[MAX_SOURCE_BINS];
  109. static gdouble fps_avg[MAX_SOURCE_BINS];
  110. static Display *display = NULL;
  111. static Window windows[MAX_INSTANCES] = { 0 };
  112. static GThread *x_event_thread = NULL;
  113. static GMutex disp_lock;
  114. static guint rrow, rcol, rcfg;
  115. static gboolean rrowsel = FALSE, selecting = FALSE;
  116. static AppConfigAnalyticsModel model_used = APP_CONFIG_ANALYTICS_MODELS_UNKNOWN;
  117. /** @} imported from deepstream-app as is */
  118. GOptionEntry entries[] = {
  119. {"version", 'v', 0, G_OPTION_ARG_NONE, &print_version,
  120. "Print DeepStreamSDK version", NULL}
  121. ,
  122. {"tiledtext", 't', 0, G_OPTION_ARG_NONE, &show_bbox_text,
  123. "Display Bounding box labels in tiled mode", NULL}
  124. ,
  125. {"version-all", 0, 0, G_OPTION_ARG_NONE, &print_dependencies_version,
  126. "Print DeepStreamSDK and dependencies version", NULL}
  127. ,
  128. {"cfg-file", 'c', 0, G_OPTION_ARG_FILENAME_ARRAY, &cfg_files,
  129. "Set the config file", NULL}
  130. ,
  131. {"override-cfg-file", 'o', 0, G_OPTION_ARG_FILENAME_ARRAY, &override_cfg_file,
  132. "Set the override config file, used for on-the-fly model update feature",
  133. NULL}
  134. ,
  135. {"input-file", 'i', 0, G_OPTION_ARG_FILENAME_ARRAY, &input_files,
  136. "Set the input file", NULL}
  137. ,
  138. {"playback-utc", 'p', 0, G_OPTION_ARG_INT, &playback_utc,
  139. "Playback utc; default=true (base UTC from file/rtsp URL); =false (base UTC from file-URL or RTCP Sender Report)",
  140. NULL}
  141. ,
  142. {"pgie-model-used", 'm', 0, G_OPTION_ARG_INT, &model_used,
  143. "PGIE Model used; {0 - Unknown [DEFAULT]}, {1: Resnet 4-class [Car, Bicycle, Person, Roadsign]}",
  144. NULL}
  145. ,
  146. {"no-force-tcp", 0, G_OPTION_FLAG_REVERSE, G_OPTION_ARG_NONE, &force_tcp,
  147. "Do not force TCP for RTP transport", NULL}
  148. ,
  149. {NULL}
  150. ,
  151. };
  152. static void
  153. generate_ts_rfc3339 (char *buf, int buf_size)
  154. {
  155. time_t tloc;
  156. struct tm tm_log;
  157. struct timespec ts;
  158. char strmsec[6]; //.nnnZ\0
  159. clock_gettime (CLOCK_REALTIME, &ts);
  160. memcpy (&tloc, (void *) (&ts.tv_sec), sizeof (time_t));
  161. gmtime_r (&tloc, &tm_log);
  162. strftime (buf, buf_size, "%Y-%m-%dT%H:%M:%S", &tm_log);
  163. int ms = ts.tv_nsec / 1000000;
  164. g_snprintf (strmsec, sizeof (strmsec), ".%.3dZ", ms);
  165. strncat (buf, strmsec, buf_size);
  166. }
  167. static GstClockTime
  168. generate_ts_rfc3339_from_ts (char *buf, int buf_size, GstClockTime ts,
  169. gchar * src_uri, gint stream_id)
  170. {
  171. time_t tloc;
  172. struct tm tm_log;
  173. char strmsec[6]; //.nnnZ\0
  174. int ms;
  175. GstClockTime ts_generated;
  176. if (playback_utc
  177. || (appCtx[0]->config.multi_source_config[stream_id].type !=
  178. NV_DS_SOURCE_RTSP)) {
  179. if (testAppCtx->streams[stream_id].meta_number == 0) {
  180. testAppCtx->streams[stream_id].timespec_first_frame =
  181. extract_utc_from_uri (src_uri);
  182. memcpy (&tloc,
  183. (void *) (&testAppCtx->streams[stream_id].timespec_first_frame.
  184. tv_sec), sizeof (time_t));
  185. ms = testAppCtx->streams[stream_id].timespec_first_frame.tv_nsec /
  186. 1000000;
  187. testAppCtx->streams[stream_id].gst_ts_first_frame = ts;
  188. ts_generated =
  189. GST_TIMESPEC_TO_TIME (testAppCtx->streams[stream_id].
  190. timespec_first_frame);
  191. if (ts_generated == 0) {
  192. g_print
  193. ("WARNING; playback mode used with URI [%s] not conforming to timestamp format;"
  194. " check README; using system-time\n", src_uri);
  195. clock_gettime (CLOCK_REALTIME,
  196. &testAppCtx->streams[stream_id].timespec_first_frame);
  197. ts_generated =
  198. GST_TIMESPEC_TO_TIME (testAppCtx->streams[stream_id].
  199. timespec_first_frame);
  200. }
  201. } else {
  202. GstClockTime ts_current =
  203. GST_TIMESPEC_TO_TIME (testAppCtx->
  204. streams[stream_id].timespec_first_frame) + (ts -
  205. testAppCtx->streams[stream_id].gst_ts_first_frame);
  206. struct timespec timespec_current;
  207. GST_TIME_TO_TIMESPEC (ts_current, timespec_current);
  208. memcpy (&tloc, (void *) (&timespec_current.tv_sec), sizeof (time_t));
  209. ms = timespec_current.tv_nsec / 1000000;
  210. ts_generated = ts_current;
  211. }
  212. } else {
  213. /** ts itself is UTC Time in ns */
  214. struct timespec timespec_current;
  215. GST_TIME_TO_TIMESPEC (ts, timespec_current);
  216. memcpy (&tloc, (void *) (&timespec_current.tv_sec), sizeof (time_t));
  217. ms = timespec_current.tv_nsec / 1000000;
  218. ts_generated = ts;
  219. }
  220. gmtime_r (&tloc, &tm_log);
  221. strftime (buf, buf_size, "%Y-%m-%dT%H:%M:%S", &tm_log);
  222. g_snprintf (strmsec, sizeof (strmsec), ".%.3dZ", ms);
  223. strncat (buf, strmsec, buf_size);
  224. LOGD ("ts=%s\n", buf);
  225. return ts_generated;
  226. }
  227. static gpointer
  228. meta_copy_func (gpointer data, gpointer user_data)
  229. {
  230. NvDsUserMeta *user_meta = (NvDsUserMeta *) data;
  231. NvDsEventMsgMeta *srcMeta = (NvDsEventMsgMeta *) user_meta->user_meta_data;
  232. NvDsEventMsgMeta *dstMeta = NULL;
  233. dstMeta = g_memdup (srcMeta, sizeof (NvDsEventMsgMeta));
  234. if (srcMeta->ts)
  235. dstMeta->ts = g_strdup (srcMeta->ts);
  236. if (srcMeta->objSignature.size > 0) {
  237. dstMeta->objSignature.signature = g_memdup (srcMeta->objSignature.signature,
  238. srcMeta->objSignature.size);
  239. dstMeta->objSignature.size = srcMeta->objSignature.size;
  240. }
  241. if (srcMeta->objectId) {
  242. dstMeta->objectId = g_strdup (srcMeta->objectId);
  243. }
  244. /*
  245. if (srcMeta->sensorStr) {
  246. dstMeta->sensorStr = g_strdup (srcMeta->sensorStr);
  247. }
  248. */
  249. if (srcMeta->extMsgSize > 0) {
  250. if (srcMeta->objType == NVDS_OBJECT_TYPE_PERSON) {
  251. NvDsPersonObject *srcObj = (NvDsPersonObject *) srcMeta->extMsg;
  252. NvDsPersonObject *obj =
  253. (NvDsPersonObject *) g_malloc0 (sizeof (NvDsPersonObject));
  254. obj->age = srcObj->age;
  255. if (srcObj->gender)
  256. obj->gender = g_strdup (srcObj->gender);
  257. if (srcObj->cap)
  258. obj->cap = g_strdup (srcObj->cap);
  259. if (srcObj->hair)
  260. obj->hair = g_strdup (srcObj->hair);
  261. if (srcObj->apparel)
  262. obj->apparel = g_strdup (srcObj->apparel);
  263. dstMeta->extMsg = obj;
  264. dstMeta->extMsgSize = sizeof (NvDsPersonObject);
  265. }
  266. }
  267. return dstMeta;
  268. }
  269. static void
  270. meta_free_func (gpointer data, gpointer user_data)
  271. {
  272. NvDsUserMeta *user_meta = (NvDsUserMeta *) data;
  273. NvDsEventMsgMeta *srcMeta = (NvDsEventMsgMeta *) user_meta->user_meta_data;
  274. user_meta->user_meta_data = NULL;
  275. if (srcMeta->ts) {
  276. g_free (srcMeta->ts);
  277. }
  278. if (srcMeta->objSignature.size > 0) {
  279. g_free (srcMeta->objSignature.signature);
  280. srcMeta->objSignature.size = 0;
  281. }
  282. if (srcMeta->objectId) {
  283. g_free (srcMeta->objectId);
  284. }
  285. if (srcMeta->sensorStr) {
  286. g_free (srcMeta->sensorStr);
  287. }
  288. if (srcMeta->extMsgSize > 0) {
  289. if (srcMeta->objType == NVDS_OBJECT_TYPE_VEHICLE) {
  290. NvDsVehicleObject *obj = (NvDsVehicleObject *) srcMeta->extMsg;
  291. if (obj->type)
  292. g_free (obj->type);
  293. if (obj->color)
  294. g_free (obj->color);
  295. if (obj->make)
  296. g_free (obj->make);
  297. if (obj->model)
  298. g_free (obj->model);
  299. if (obj->license)
  300. g_free (obj->license);
  301. if (obj->region)
  302. g_free (obj->region);
  303. } else if (srcMeta->objType == NVDS_OBJECT_TYPE_PERSON) {
  304. NvDsPersonObject *obj = (NvDsPersonObject *) srcMeta->extMsg;
  305. if (obj->gender)
  306. g_free (obj->gender);
  307. if (obj->cap)
  308. g_free (obj->cap);
  309. if (obj->hair)
  310. g_free (obj->hair);
  311. if (obj->apparel)
  312. g_free (obj->apparel);
  313. }
  314. g_free (srcMeta->extMsg);
  315. srcMeta->extMsg = NULL;
  316. srcMeta->extMsgSize = 0;
  317. }
  318. g_free (srcMeta);
  319. }
  320. #ifdef GENERATE_DUMMY_META_EXT
  321. #endif /**< GENERATE_DUMMY_META_EXT */
  322. static void
  323. generate_event_msg_meta (gpointer data, gint class_id, gboolean useTs,
  324. GstClockTime ts, gchar * src_uri, gint stream_id, guint sensor_id,
  325. AnalyticsUserMeta * obj_params, float scaleW, float scaleH,
  326. NvDsFrameMeta * frame_meta)
  327. {
  328. NvDsEventMsgMeta *meta = (NvDsEventMsgMeta *) data;
  329. GstClockTime ts_generated = 0;
  330. meta->objType = NVDS_OBJECT_TYPE_UNKNOWN; /**< object unknown */
  331. meta->frameId = frame_meta->frame_num;
  332. meta->ts = (gchar *) g_malloc0 (MAX_TIME_STAMP_LEN + 1);
  333. meta->objectId = (gchar *) g_malloc0 (MAX_LABEL_SIZE);
  334. //strncpy (meta->objectId, obj_params->obj_label, MAX_LABEL_SIZE);
  335. /** INFO: This API is called once for every 30 frames (now) */
  336. if (useTs && src_uri) {
  337. ts_generated =
  338. generate_ts_rfc3339_from_ts (meta->ts, MAX_TIME_STAMP_LEN, ts, src_uri,
  339. stream_id);
  340. } else {
  341. generate_ts_rfc3339 (meta->ts, MAX_TIME_STAMP_LEN);
  342. }
  343. /** tracking ID */
  344. meta->trackingId = class_id;
  345. (void) ts_generated;
  346. meta->type = NVDS_EVENT_ENTRY;
  347. meta->objType = NVDS_OBJECT_TYPE_PERSON;
  348. meta->objClassId = PERSON_ID;
  349. meta->occupancy = obj_params->lccum_cnt;
  350. meta->lccum_cnt_entry = obj_params->lcc_cnt_entry;
  351. meta->lccum_cnt_exit = obj_params->lcc_cnt_exit ;
  352. meta->source_id = obj_params->source_id;
  353. // g_print("source id: %d, Enter: %d, Exit: %d\n", meta->source_id, meta->lccum_cnt_entry, meta->lccum_cnt_exit);
  354. }
  355. /*
  356. * Access analytics data.
  357. */
  358. void analytics_custom_parse_nvdsanalytics_meta_data (NvDsMetaList *l_user, AnalyticsUserMeta *data);
  359. /**
  360. * Callback function to be called once all inferences (Primary + Secondary)
  361. * are done. This is opportunity to modify content of the metadata.
  362. * e.g. Here Person is being replaced with Man/Woman and corresponding counts
  363. * are being maintained. It should be modified according to network classes
  364. * or can be removed altogether if not required.
  365. */
  366. static void
  367. bbox_generated_probe_after_analytics (AppCtx * appCtx, GstBuffer * buf,
  368. NvDsBatchMeta * batch_meta, guint index)
  369. {
  370. NvDsObjectMeta *obj_meta = NULL;
  371. GstClockTime buffer_pts = 0;
  372. guint32 stream_id = 0;
  373. if (!appCtx->config.dsanalytics_config.enable){
  374. g_print ("Unable to get nvdsanalytics src pad\n");
  375. return;
  376. }
  377. for (NvDsMetaList * l_frame = batch_meta->frame_meta_list; l_frame != NULL;
  378. l_frame = l_frame->next) {
  379. NvDsFrameMeta *frame_meta = l_frame->data;
  380. stream_id = frame_meta->source_id;
  381. GstClockTime buf_ntp_time = 0;
  382. if (playback_utc == FALSE) {
  383. /** Calculate the buffer-NTP-time
  384. * derived from this stream's RTCP Sender Report here:
  385. */
  386. StreamSourceInfo *src_stream = &testAppCtx->streams[stream_id];
  387. buf_ntp_time = frame_meta->ntp_timestamp;
  388. if (buf_ntp_time < src_stream->last_ntp_time) {
  389. NVGSTDS_WARN_MSG_V ("Source %d: NTP timestamps are backward in time."
  390. " Current: %lu previous: %lu", stream_id, buf_ntp_time,
  391. src_stream->last_ntp_time);
  392. }
  393. src_stream->last_ntp_time = buf_ntp_time;
  394. }
  395. GList *l;
  396. NvDsMetaList *l_analyticsuser;
  397. l_analyticsuser = frame_meta->frame_user_meta_list;
  398. AnalyticsUserMeta *user_data =
  399. (AnalyticsUserMeta *) g_malloc0(sizeof(AnalyticsUserMeta));
  400. if (l_analyticsuser != NULL) {
  401. analytics_custom_parse_nvdsanalytics_meta_data(l_analyticsuser, user_data);
  402. }
  403. user_data->source_id = stream_id;
  404. // l_analyticsuser = l_analyticsuser->next;
  405. /* Code from test5 application */
  406. for (l = frame_meta->obj_meta_list; l != NULL; l = l->next) {
  407. /* Now using above information we need to form a text that should
  408. * be displayed on top of the bounding box, so lets form it here. */
  409. obj_meta = (NvDsObjectMeta *) (l->data);
  410. {
  411. /**
  412. * Enable only if this callback is after tiler
  413. * NOTE: Scaling back code-commented
  414. * now that bbox_generated_probe_after_analytics() is post analytics
  415. * (say pgie, tracker or sgie)
  416. * and before tiler, no plugin shall scale metadata and will be
  417. * corresponding to the nvstreammux resolution
  418. */
  419. float scaleW = 0;
  420. float scaleH = 0;
  421. /* Frequency of messages to be send will be based on use case.
  422. * Here message is being sent for first object every 30 frames.
  423. */
  424. buffer_pts = frame_meta->buf_pts;
  425. if (!appCtx->config.streammux_config.pipeline_width
  426. || !appCtx->config.streammux_config.pipeline_height) {
  427. g_print ("invalid pipeline params\n");
  428. return;
  429. }
  430. LOGD ("stream %d==%d [%d X %d]\n", frame_meta->source_id,
  431. frame_meta->pad_index, frame_meta->source_frame_width,
  432. frame_meta->source_frame_height);
  433. scaleW =
  434. (float) frame_meta->source_frame_width /
  435. appCtx->config.streammux_config.pipeline_width;
  436. scaleH =
  437. (float) frame_meta->source_frame_height /
  438. appCtx->config.streammux_config.pipeline_height;
  439. if (playback_utc == FALSE) {
  440. /** Use the buffer-NTP-time derived from this stream's RTCP Sender
  441. * Report here:
  442. */
  443. buffer_pts = buf_ntp_time;
  444. }
  445. /** Generate NvDsEventMsgMeta for every object */
  446. NvDsEventMsgMeta *msg_meta =
  447. (NvDsEventMsgMeta *) g_malloc0 (sizeof (NvDsEventMsgMeta));
  448. generate_event_msg_meta (msg_meta, PERSON_ID, TRUE,
  449. /**< useTs NOTE: Pass FALSE for files without base-timestamp in URI */
  450. buffer_pts,
  451. appCtx->config.multi_source_config[stream_id].uri, stream_id,
  452. appCtx->config.multi_source_config[stream_id].camera_id,
  453. user_data, scaleW, scaleH, frame_meta);
  454. testAppCtx->streams[stream_id].meta_number++;
  455. NvDsUserMeta *user_event_meta =
  456. nvds_acquire_user_meta_from_pool (batch_meta);
  457. if (user_event_meta) {
  458. /*
  459. * Since generated event metadata has custom objects for
  460. * Vehicle / Person which are allocated dynamically, we are
  461. * setting copy and free function to handle those fields when
  462. * metadata copy happens between two components.
  463. */
  464. user_event_meta->user_meta_data = (void *) msg_meta;
  465. user_event_meta->base_meta.batch_meta = batch_meta;
  466. user_event_meta->base_meta.meta_type = NVDS_EVENT_MSG_META;
  467. user_event_meta->base_meta.copy_func =
  468. (NvDsMetaCopyFunc) meta_copy_func;
  469. user_event_meta->base_meta.release_func =
  470. (NvDsMetaReleaseFunc) meta_free_func;
  471. nvds_add_user_meta_to_frame (frame_meta, user_event_meta);
  472. } else {
  473. g_print ("Error in attaching event meta to buffer\n");
  474. }
  475. }
  476. }
  477. testAppCtx->streams[stream_id].frameCount++;
  478. g_free(user_data);
  479. }
  480. }
  481. /** @{ imported from deepstream-app as is */
  482. /**
  483. * Function to handle program interrupt signal.
  484. * It installs default handler after handling the interrupt.
  485. */
  486. static void
  487. _intr_handler (int signum)
  488. {
  489. struct sigaction action;
  490. NVGSTDS_ERR_MSG_V ("User Interrupted.. \n");
  491. memset (&action, 0, sizeof (action));
  492. action.sa_handler = SIG_DFL;
  493. sigaction (SIGINT, &action, NULL);
  494. cintr = TRUE;
  495. }
  496. /**
  497. * callback function to print the performance numbers of each stream.
  498. */
  499. static void
  500. perf_cb (gpointer context, NvDsAppPerfStruct * str)
  501. {
  502. static guint header_print_cnt = 0;
  503. guint i;
  504. AppCtx *appCtx = (AppCtx *) context;
  505. guint numf = str->num_instances;
  506. g_mutex_lock (&fps_lock);
  507. for (i = 0; i < numf; i++) {
  508. fps[i] = str->fps[i];
  509. fps_avg[i] = str->fps_avg[i];
  510. }
  511. if (header_print_cnt % 20 == 0) {
  512. g_print ("\n**PERF: ");
  513. for (i = 0; i < numf; i++) {
  514. g_print ("FPS %d (Avg)\t", i);
  515. }
  516. g_print ("\n");
  517. header_print_cnt = 0;
  518. }
  519. header_print_cnt++;
  520. time_t t = time (NULL);
  521. struct tm *tm = localtime (&t);
  522. printf ("%s", asctime (tm));
  523. if (num_instances > 1)
  524. g_print ("PERF(%d): ", appCtx->index);
  525. else
  526. g_print ("**PERF: ");
  527. for (i = 0; i < numf; i++) {
  528. g_print ("%.2f (%.2f)\t", fps[i], fps_avg[i]);
  529. }
  530. g_print ("\n");
  531. g_mutex_unlock (&fps_lock);
  532. }
  533. /**
  534. * Loop function to check the status of interrupts.
  535. * It comes out of loop if application got interrupted.
  536. */
  537. static gboolean
  538. check_for_interrupt (gpointer data)
  539. {
  540. if (quit) {
  541. return FALSE;
  542. }
  543. if (cintr) {
  544. cintr = FALSE;
  545. quit = TRUE;
  546. g_main_loop_quit (main_loop);
  547. return FALSE;
  548. }
  549. return TRUE;
  550. }
  551. /*
  552. * Function to install custom handler for program interrupt signal.
  553. */
  554. static void
  555. _intr_setup (void)
  556. {
  557. struct sigaction action;
  558. memset (&action, 0, sizeof (action));
  559. action.sa_handler = _intr_handler;
  560. sigaction (SIGINT, &action, NULL);
  561. }
  562. static gboolean
  563. kbhit (void)
  564. {
  565. struct timeval tv;
  566. fd_set rdfs;
  567. tv.tv_sec = 0;
  568. tv.tv_usec = 0;
  569. FD_ZERO (&rdfs);
  570. FD_SET (STDIN_FILENO, &rdfs);
  571. select (STDIN_FILENO + 1, &rdfs, NULL, NULL, &tv);
  572. return FD_ISSET (STDIN_FILENO, &rdfs);
  573. }
  574. /*
  575. * Function to enable / disable the canonical mode of terminal.
  576. * In non canonical mode input is available immediately (without the user
  577. * having to type a line-delimiter character).
  578. */
  579. static void
  580. changemode (int dir)
  581. {
  582. static struct termios oldt, newt;
  583. if (dir == 1) {
  584. tcgetattr (STDIN_FILENO, &oldt);
  585. newt = oldt;
  586. newt.c_lflag &= ~(ICANON);
  587. tcsetattr (STDIN_FILENO, TCSANOW, &newt);
  588. } else
  589. tcsetattr (STDIN_FILENO, TCSANOW, &oldt);
  590. }
  591. static void
  592. print_runtime_commands (void)
  593. {
  594. g_print ("\nRuntime commands:\n"
  595. "\th: Print this help\n"
  596. "\tq: Quit\n\n" "\tp: Pause\n" "\tr: Resume\n\n");
  597. if (appCtx[0]->config.tiled_display_config.enable) {
  598. g_print
  599. ("NOTE: To expand a source in the 2D tiled display and view object details,"
  600. " left-click on the source.\n"
  601. " To go back to the tiled display, right-click anywhere on the window.\n\n");
  602. }
  603. }
  604. /**
  605. * Loop function to check keyboard inputs and status of each pipeline.
  606. */
  607. static gboolean
  608. event_thread_func (gpointer arg)
  609. {
  610. guint i;
  611. gboolean ret = TRUE;
  612. // Check if all instances have quit
  613. for (i = 0; i < num_instances; i++) {
  614. if (!appCtx[i]->quit)
  615. break;
  616. }
  617. if (i == num_instances) {
  618. quit = TRUE;
  619. g_main_loop_quit (main_loop);
  620. return FALSE;
  621. }
  622. // Check for keyboard input
  623. if (!kbhit ()) {
  624. //continue;
  625. return TRUE;
  626. }
  627. int c = fgetc (stdin);
  628. g_print ("\n");
  629. gint source_id;
  630. GstElement *tiler = appCtx[rcfg]->pipeline.tiled_display_bin.tiler;
  631. g_object_get (G_OBJECT (tiler), "show-source", &source_id, NULL);
  632. if (selecting) {
  633. if (rrowsel == FALSE) {
  634. if (c >= '0' && c <= '9') {
  635. rrow = c - '0';
  636. g_print ("--selecting source row %d--\n", rrow);
  637. rrowsel = TRUE;
  638. }
  639. } else {
  640. if (c >= '0' && c <= '9') {
  641. int tile_num_columns = appCtx[rcfg]->config.tiled_display_config.columns;
  642. rcol = c - '0';
  643. selecting = FALSE;
  644. rrowsel = FALSE;
  645. source_id = tile_num_columns * rrow + rcol;
  646. g_print ("--selecting source col %d sou=%d--\n", rcol, source_id);
  647. if (source_id >= (gint) appCtx[rcfg]->config.num_source_sub_bins) {
  648. source_id = -1;
  649. } else {
  650. appCtx[rcfg]->show_bbox_text = TRUE;
  651. appCtx[rcfg]->active_source_index = source_id;
  652. g_object_set (G_OBJECT (tiler), "show-source", source_id, NULL);
  653. }
  654. }
  655. }
  656. }
  657. switch (c) {
  658. case 'h':
  659. print_runtime_commands ();
  660. break;
  661. case 'p':
  662. for (i = 0; i < num_instances; i++)
  663. pause_pipeline (appCtx[i]);
  664. break;
  665. case 'r':
  666. for (i = 0; i < num_instances; i++)
  667. resume_pipeline (appCtx[i]);
  668. break;
  669. case 'q':
  670. quit = TRUE;
  671. g_main_loop_quit (main_loop);
  672. ret = FALSE;
  673. break;
  674. case 'c':
  675. if (selecting == FALSE && source_id == -1) {
  676. g_print("--selecting config file --\n");
  677. c = fgetc(stdin);
  678. if (c >= '0' && c <= '9') {
  679. rcfg = c - '0';
  680. if (rcfg < num_instances) {
  681. g_print("--selecting config %d--\n", rcfg);
  682. } else {
  683. g_print("--selected config file %d out of bound, reenter\n", rcfg);
  684. rcfg = 0;
  685. }
  686. }
  687. }
  688. break;
  689. case 'z':
  690. if (source_id == -1 && selecting == FALSE) {
  691. g_print ("--selecting source --\n");
  692. selecting = TRUE;
  693. } else {
  694. if (!show_bbox_text) {
  695. GstElement *nvosd = appCtx[rcfg]->pipeline.instance_bins[0].osd_bin.nvosd;
  696. g_object_set (G_OBJECT (nvosd), "display-text", FALSE, NULL);
  697. g_object_set (G_OBJECT (tiler), "show-source", -1, NULL);
  698. }
  699. appCtx[rcfg]->active_source_index = -1;
  700. selecting = FALSE;
  701. rcfg = 0;
  702. g_print("--tiled mode --\n");
  703. }
  704. break;
  705. default:
  706. break;
  707. }
  708. return ret;
  709. }
  710. static int
  711. get_source_id_from_coordinates (float x_rel, float y_rel, AppCtx *appCtx)
  712. {
  713. int tile_num_rows = appCtx->config.tiled_display_config.rows;
  714. int tile_num_columns = appCtx->config.tiled_display_config.columns;
  715. int source_id = (int) (x_rel * tile_num_columns);
  716. source_id += ((int) (y_rel * tile_num_rows)) * tile_num_columns;
  717. /* Don't allow clicks on empty tiles. */
  718. if (source_id >= (gint) appCtx->config.num_source_sub_bins)
  719. source_id = -1;
  720. return source_id;
  721. }
  722. /**
  723. * Thread to monitor X window events.
  724. */
  725. static gpointer
  726. nvds_x_event_thread (gpointer data)
  727. {
  728. g_mutex_lock (&disp_lock);
  729. while (display) {
  730. XEvent e;
  731. guint index;
  732. while (XPending (display)) {
  733. XNextEvent (display, &e);
  734. switch (e.type) {
  735. case ButtonPress:
  736. {
  737. XWindowAttributes win_attr;
  738. XButtonEvent ev = e.xbutton;
  739. gint source_id;
  740. GstElement *tiler;
  741. XGetWindowAttributes (display, ev.window, &win_attr);
  742. for (index = 0; index < MAX_INSTANCES; index++)
  743. if (ev.window == windows[index])
  744. break;
  745. tiler = appCtx[index]->pipeline.tiled_display_bin.tiler;
  746. g_object_get (G_OBJECT (tiler), "show-source", &source_id, NULL);
  747. if (ev.button == Button1 && source_id == -1) {
  748. source_id =
  749. get_source_id_from_coordinates (ev.x * 1.0 / win_attr.width,
  750. ev.y * 1.0 / win_attr.height, appCtx[index]);
  751. if (source_id > -1) {
  752. g_object_set (G_OBJECT (tiler), "show-source", source_id, NULL);
  753. appCtx[index]->active_source_index = source_id;
  754. appCtx[index]->show_bbox_text = TRUE;
  755. GstElement *nvosd = appCtx[index]->pipeline.instance_bins[0].osd_bin.nvosd;
  756. g_object_set (G_OBJECT (nvosd), "display-text", TRUE, NULL);
  757. }
  758. } else if (ev.button == Button3) {
  759. g_object_set (G_OBJECT (tiler), "show-source", -1, NULL);
  760. appCtx[index]->active_source_index = -1;
  761. if (!show_bbox_text) {
  762. appCtx[index]->show_bbox_text = FALSE;
  763. GstElement *nvosd = appCtx[index]->pipeline.instance_bins[0].osd_bin.nvosd;
  764. g_object_set (G_OBJECT (nvosd), "display-text", FALSE, NULL);
  765. }
  766. }
  767. }
  768. break;
  769. case KeyRelease:
  770. {
  771. KeySym p, r, q;
  772. guint i;
  773. p = XKeysymToKeycode (display, XK_P);
  774. r = XKeysymToKeycode (display, XK_R);
  775. q = XKeysymToKeycode (display, XK_Q);
  776. if (e.xkey.keycode == p) {
  777. for (i = 0; i < num_instances; i++)
  778. pause_pipeline (appCtx[i]);
  779. break;
  780. }
  781. if (e.xkey.keycode == r) {
  782. for (i = 0; i < num_instances; i++)
  783. resume_pipeline (appCtx[i]);
  784. break;
  785. }
  786. if (e.xkey.keycode == q) {
  787. quit = TRUE;
  788. g_main_loop_quit (main_loop);
  789. }
  790. }
  791. break;
  792. case ClientMessage:
  793. {
  794. Atom wm_delete;
  795. for (index = 0; index < MAX_INSTANCES; index++)
  796. if (e.xclient.window == windows[index])
  797. break;
  798. wm_delete = XInternAtom (display, "WM_DELETE_WINDOW", 1);
  799. if (wm_delete != None && wm_delete == (Atom) e.xclient.data.l[0]) {
  800. quit = TRUE;
  801. g_main_loop_quit (main_loop);
  802. }
  803. }
  804. break;
  805. }
  806. }
  807. g_mutex_unlock (&disp_lock);
  808. g_usleep (G_USEC_PER_SEC / 20);
  809. g_mutex_lock (&disp_lock);
  810. }
  811. g_mutex_unlock (&disp_lock);
  812. return NULL;
  813. }
  814. /**
  815. * callback function to add application specific metadata.
  816. * Here it demonstrates how to display the URI of source in addition to
  817. * the text generated after inference.
  818. */
  819. static gboolean
  820. overlay_graphics (AppCtx * appCtx, GstBuffer * buf,
  821. NvDsBatchMeta * batch_meta, guint index)
  822. {
  823. return TRUE;
  824. }
  825. /** @} imported from deepstream-app as is */
  826. int
  827. main (int argc, char *argv[])
  828. {
  829. testAppCtx = (TestAppCtx *) g_malloc0 (sizeof (TestAppCtx));
  830. GOptionContext *ctx = NULL;
  831. GOptionGroup *group = NULL;
  832. GError *error = NULL;
  833. guint i;
  834. ctx = g_option_context_new ("Nvidia DeepStream Test5");
  835. group = g_option_group_new ("abc", NULL, NULL, NULL, NULL);
  836. g_option_group_add_entries (group, entries);
  837. g_option_context_set_main_group (ctx, group);
  838. g_option_context_add_group (ctx, gst_init_get_option_group ());
  839. GST_DEBUG_CATEGORY_INIT (NVDS_APP, "NVDS_APP", 0, NULL);
  840. if (!g_option_context_parse (ctx, &argc, &argv, &error)) {
  841. NVGSTDS_ERR_MSG_V ("%s", error->message);
  842. g_print ("%s",g_option_context_get_help (ctx, TRUE, NULL));
  843. return -1;
  844. }
  845. if (print_version) {
  846. g_print ("deepstream-test5-app version %d.%d.%d\n",
  847. NVDS_APP_VERSION_MAJOR, NVDS_APP_VERSION_MINOR, NVDS_APP_VERSION_MICRO);
  848. return 0;
  849. }
  850. if (print_dependencies_version) {
  851. g_print ("deepstream-test5-app version %d.%d.%d\n",
  852. NVDS_APP_VERSION_MAJOR, NVDS_APP_VERSION_MINOR, NVDS_APP_VERSION_MICRO);
  853. return 0;
  854. }
  855. if (cfg_files) {
  856. num_instances = g_strv_length (cfg_files);
  857. }
  858. if (input_files) {
  859. num_input_files = g_strv_length (input_files);
  860. }
  861. if (!cfg_files || num_instances == 0) {
  862. NVGSTDS_ERR_MSG_V ("Specify config file with -c option");
  863. return_value = -1;
  864. goto done;
  865. }
  866. for (i = 0; i < num_instances; i++) {
  867. appCtx[i] = (AppCtx *) g_malloc0 (sizeof (AppCtx));
  868. appCtx[i]->person_class_id = -1;
  869. appCtx[i]->car_class_id = -1;
  870. appCtx[i]->index = i;
  871. appCtx[i]->active_source_index = -1;
  872. if (show_bbox_text) {
  873. appCtx[i]->show_bbox_text = TRUE;
  874. }
  875. if (input_files && input_files[i]) {
  876. appCtx[i]->config.multi_source_config[0].uri =
  877. g_strdup_printf ("file://%s", input_files[i]);
  878. g_free (input_files[i]);
  879. }
  880. if (!parse_config_file (&appCtx[i]->config, cfg_files[i])) {
  881. NVGSTDS_ERR_MSG_V ("Failed to parse config file '%s'", cfg_files[i]);
  882. appCtx[i]->return_value = -1;
  883. goto done;
  884. }
  885. if (override_cfg_file && override_cfg_file[i]) {
  886. if (!g_file_test (override_cfg_file[i],
  887. G_FILE_TEST_IS_REGULAR | G_FILE_TEST_IS_SYMLINK))
  888. {
  889. g_print ("Override file %s does not exist, quitting...\n",
  890. override_cfg_file[i]);
  891. appCtx[i]->return_value = -1;
  892. goto done;
  893. }
  894. }
  895. }
  896. for (i = 0; i < num_instances; i++) {
  897. for (guint j = 0; j < appCtx[i]->config.num_source_sub_bins; j++) {
  898. /** Force the source (applicable only if RTSP)
  899. * to use TCP for RTP/RTCP channels.
  900. * forcing TCP to avoid problems with UDP port usage from within docker-
  901. * container.
  902. * The UDP RTCP channel when run within docker had issues receiving
  903. * RTCP Sender Reports from server
  904. */
  905. if (force_tcp)
  906. appCtx[i]->config.multi_source_config[j].select_rtp_protocol = 0x04;
  907. }
  908. if (!create_pipeline (appCtx[i], bbox_generated_probe_after_analytics,
  909. NULL, perf_cb, overlay_graphics)) {
  910. NVGSTDS_ERR_MSG_V ("Failed to create pipeline");
  911. return_value = -1;
  912. goto done;
  913. }
  914. /* if (appCtx[i]->config.dsanalytics_config.enable){
  915. GstPad *src_pad = NULL;
  916. GstElement *nvdsanalytics = appCtx[i]->pipeline.common_elements.dsanalytics_bin.elem_dsanalytics;
  917. src_pad = gst_element_get_static_pad (nvdsanalytics, "src");
  918. if (!src_pad)
  919. g_print ("Unable to get nvdsanalytics src pad\n");
  920. else
  921. {
  922. gst_pad_add_probe (src_pad, GST_PAD_PROBE_TYPE_BUFFER,
  923. nvdsanalytics_src_pad_buffer_probe, NULL, NULL);
  924. gst_object_unref (src_pad);
  925. }
  926. }*/
  927. /** Now add probe to RTPSession plugin src pad */
  928. for (guint j = 0; j < appCtx[i]->pipeline.multi_src_bin.num_bins; j++) {
  929. testAppCtx->streams[j].id = j;
  930. }
  931. /** In test5 app, as we could have several sources connected
  932. * for a typical IoT use-case, raising the nvstreammux's
  933. * buffer-pool-size to 16 */
  934. g_object_set (appCtx[i]->pipeline.multi_src_bin.streammux,
  935. "buffer-pool-size", STREAMMUX_BUFFER_POOL_SIZE, NULL);
  936. }
  937. main_loop = g_main_loop_new (NULL, FALSE);
  938. _intr_setup ();
  939. g_timeout_add (400, check_for_interrupt, NULL);
  940. g_mutex_init (&disp_lock);
  941. display = XOpenDisplay (NULL);
  942. for (i = 0; i < num_instances; i++) {
  943. guint j;
  944. if (!show_bbox_text) {
  945. GstElement *nvosd = appCtx[i]->pipeline.instance_bins[0].osd_bin.nvosd;
  946. g_object_set(G_OBJECT(nvosd), "display-text", FALSE, NULL);
  947. }
  948. if (gst_element_set_state (appCtx[i]->pipeline.pipeline,
  949. GST_STATE_PAUSED) == GST_STATE_CHANGE_FAILURE) {
  950. NVGSTDS_ERR_MSG_V ("Failed to set pipeline to PAUSED");
  951. return_value = -1;
  952. goto done;
  953. }
  954. if (!appCtx[i]->config.tiled_display_config.enable)
  955. continue;
  956. for (j = 0; j < appCtx[i]->config.num_sink_sub_bins; j++) {
  957. XTextProperty xproperty;
  958. gchar *title;
  959. guint width, height;
  960. XSizeHints hints = {0};
  961. if (!GST_IS_VIDEO_OVERLAY (appCtx[i]->pipeline.instance_bins[0].sink_bin.
  962. sub_bins[j].sink)) {
  963. continue;
  964. }
  965. if (!display) {
  966. NVGSTDS_ERR_MSG_V ("Could not open X Display");
  967. return_value = -1;
  968. goto done;
  969. }
  970. if (appCtx[i]->config.sink_bin_sub_bin_config[j].render_config.width)
  971. width =
  972. appCtx[i]->config.sink_bin_sub_bin_config[j].render_config.width;
  973. else
  974. width = appCtx[i]->config.tiled_display_config.width;
  975. if (appCtx[i]->config.sink_bin_sub_bin_config[j].render_config.height)
  976. height =
  977. appCtx[i]->config.sink_bin_sub_bin_config[j].render_config.height;
  978. else
  979. height = appCtx[i]->config.tiled_display_config.height;
  980. width = (width) ? width : DEFAULT_X_WINDOW_WIDTH;
  981. height = (height) ? height : DEFAULT_X_WINDOW_HEIGHT;
  982. hints.flags = PPosition | PSize;
  983. hints.x = appCtx[i]->config.sink_bin_sub_bin_config[j].render_config.offset_x;
  984. hints.y = appCtx[i]->config.sink_bin_sub_bin_config[j].render_config.offset_y;
  985. hints.width = width;
  986. hints.height = height;
  987. windows[i] =
  988. XCreateSimpleWindow (display, RootWindow (display,
  989. DefaultScreen (display)), hints.x, hints.y, width, height, 2,
  990. 0x00000000, 0x00000000);
  991. XSetNormalHints(display, windows[i], &hints);
  992. if (num_instances > 1)
  993. title = g_strdup_printf (APP_TITLE "-%d", i);
  994. else
  995. title = g_strdup (APP_TITLE);
  996. if (XStringListToTextProperty ((char **) &title, 1, &xproperty) != 0) {
  997. XSetWMName (display, windows[i], &xproperty);
  998. XFree (xproperty.value);
  999. }
  1000. XSetWindowAttributes attr = { 0 };
  1001. if ((appCtx[i]->config.tiled_display_config.enable &&
  1002. appCtx[i]->config.tiled_display_config.rows *
  1003. appCtx[i]->config.tiled_display_config.columns == 1) ||
  1004. (appCtx[i]->config.tiled_display_config.enable == 0 &&
  1005. appCtx[i]->config.num_source_sub_bins == 1)) {
  1006. } else {
  1007. attr.event_mask = ButtonPress | KeyRelease;
  1008. }
  1009. XChangeWindowAttributes (display, windows[i], CWEventMask, &attr);
  1010. Atom wmDeleteMessage = XInternAtom (display, "WM_DELETE_WINDOW", False);
  1011. if (wmDeleteMessage != None) {
  1012. XSetWMProtocols (display, windows[i], &wmDeleteMessage, 1);
  1013. }
  1014. XMapRaised (display, windows[i]);
  1015. XSync (display, 1); //discard the events for now
  1016. gst_video_overlay_set_window_handle (GST_VIDEO_OVERLAY (appCtx
  1017. [i]->pipeline.instance_bins[0].sink_bin.sub_bins[j].sink),
  1018. (gulong) windows[i]);
  1019. gst_video_overlay_expose (GST_VIDEO_OVERLAY (appCtx[i]->pipeline.
  1020. instance_bins[0].sink_bin.sub_bins[j].sink));
  1021. if (!x_event_thread)
  1022. x_event_thread = g_thread_new ("nvds-window-event-thread",
  1023. nvds_x_event_thread, NULL);
  1024. }
  1025. }
  1026. /* Dont try to set playing state if error is observed */
  1027. if (return_value != -1) {
  1028. for (i = 0; i < num_instances; i++) {
  1029. if (gst_element_set_state (appCtx[i]->pipeline.pipeline,
  1030. GST_STATE_PLAYING) == GST_STATE_CHANGE_FAILURE) {
  1031. g_print ("\ncan't set pipeline to playing state.\n");
  1032. return_value = -1;
  1033. goto done;
  1034. }
  1035. }
  1036. }
  1037. print_runtime_commands ();
  1038. changemode (1);
  1039. g_timeout_add (40, event_thread_func, NULL);
  1040. g_main_loop_run (main_loop);
  1041. changemode (0);
  1042. done:
  1043. g_print ("Quitting\n");
  1044. for (i = 0; i < num_instances; i++) {
  1045. if (appCtx[i] == NULL)
  1046. continue;
  1047. if (appCtx[i]->return_value == -1)
  1048. return_value = -1;
  1049. destroy_pipeline (appCtx[i]);
  1050. g_mutex_lock (&disp_lock);
  1051. if (windows[i])
  1052. XDestroyWindow (display, windows[i]);
  1053. windows[i] = 0;
  1054. g_mutex_unlock (&disp_lock);
  1055. g_free (appCtx[i]);
  1056. }
  1057. g_mutex_lock (&disp_lock);
  1058. if (display)
  1059. XCloseDisplay (display);
  1060. display = NULL;
  1061. g_mutex_unlock (&disp_lock);
  1062. g_mutex_clear (&disp_lock);
  1063. if (main_loop) {
  1064. g_main_loop_unref (main_loop);
  1065. }
  1066. if (ctx) {
  1067. g_option_context_free (ctx);
  1068. }
  1069. if (return_value == 0) {
  1070. g_print ("App run successful\n");
  1071. } else {
  1072. g_print ("App run failed\n");
  1073. }
  1074. gst_deinit ();
  1075. return return_value;
  1076. g_free (testAppCtx);
  1077. return 0;
  1078. }
  1079. static gchar *
  1080. get_first_result_label (NvDsClassifierMeta * classifierMeta)
  1081. {
  1082. GList *n;
  1083. for (n = classifierMeta->label_info_list; n != NULL; n = n->next) {
  1084. NvDsLabelInfo *labelInfo = (NvDsLabelInfo *) (n->data);
  1085. if (labelInfo->result_label[0] != '\0') {
  1086. return g_strdup (labelInfo->result_label);
  1087. }
  1088. }
  1089. return NULL;
  1090. }