-
Notifications
You must be signed in to change notification settings - Fork 8
/
sharedmem_image_data_viewer.c
556 lines (446 loc) · 16.2 KB
/
sharedmem_image_data_viewer.c
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
/*
* Copyright (c) 2016 PolySync
*
*
* Permission is hereby granted, free of charge, to any person obtaining a copy
* of this software and associated documentation files (the "Software"), to deal
* in the Software without restriction, including without limitation the rights
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
* copies of the Software, and to permit persons to whom the Software is
* furnished to do so, subject to the following conditions:
*
*
* The above copyright notice and this permission notice shall be included in
* all copies or substantial portions of the Software.
*
*
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
* THE SOFTWARE.
*/
/**
* \example shared_image_data_viewer.c
*
* Shared Memory Image Data Viewer Example.
*
* Shows how to decode and view encoded and raw
* image data received over a PolySync Shared Memory Queue.
*
*/
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <signal.h>
#include <unistd.h>
#include <glib-2.0/glib.h>
// API headers
#include "polysync_core.h"
#include "polysync_message.h"
#include "polysync_sdf.h"
#include "polysync_video.h"
#include "polysync_shdmem.h"
// GUI headers
#include "gui.h"
// *****************************************************
// static global types/macros
// *****************************************************
/**
* @brief Node reference used by the example.
*
*/
static ps_node_ref global_node_ref = PSYNC_NODE_REF_INVALID;
/**
* @brief Flag indicating exit signal was caught.
*
*/
static sig_atomic_t global_exit_signal = 0;
/**
* @brief PolySync node name.
*
*/
static const char NODE_NAME[] = "polysync-sharedmem-image-data-viewer";
// *****************************************************
// static declarations
// *****************************************************
/**
* @brief Signal handler.
*
* @param [in] signal Signal value to handle.
*
*/
static void sig_handler( int signal );
// *****************************************************
// static definitions
// *****************************************************
//
static void sig_handler( int sig )
{
if( sig == SIGINT )
{
global_exit_signal = 1;
}
}
// *****************************************************
// main
// *****************************************************
int main( int argc, char **argv )
{
// polysync return status
int ret = DTC_NONE;
// GUI data
gui_context_s *gui = NULL;
// shared memory queue
psync_shdmem_queue shdmem_queue = PSYNC_SHDMEM_QUEUE_INVALID;
// desired decoder output pixel format, RGB
ps_pixel_format_kind desired_decoder_format = PIXEL_FORMAT_RGB24;
// publisher image width/height
unsigned long publisher_width = 0;
unsigned long publisher_height = 0;
// publisher pixel format
ps_pixel_format_kind publisher_format = PIXEL_FORMAT_INVALID;
// video decoder - converts compressed stream into raw image
ps_video_decoder video_decoder;
// local decoder data buffer - filled by the decoder
unsigned char *decoder_buffer = NULL;
// size in bytes of the decoded image data
unsigned long decoded_frame_size = 0;
// number of bytes decoded per frame
unsigned long bytes_decoded = 0;
// shared memory message buffer
unsigned char *shdmem_buffer = NULL;
// size in bytes of the shared memory buffer
unsigned long shdmem_buffer_size = 0;
// shared memory key
unsigned long shdmem_key = 0;
// current time
ps_timestamp timestamp = 0;
// time to redraw
ps_timestamp time_to_draw = 0;
// sleep counter
unsigned long sleep_tick = 0;
// last image rx/decoded timestamp
ps_timestamp last_rx_time = 0;
// zero
memset( &video_decoder, 0, sizeof(video_decoder) );
// check for shared memory key argument
if( (argc < 2) || (strlen(argv[1]) <= 0) )
{
printf( "usage: %s <shared_mem_key>\n", NODE_NAME );
return EXIT_FAILURE;
}
// convert to int
shdmem_key = (unsigned long) atol(argv[1]);
// init core API
ret = psync_init(
NODE_NAME,
PSYNC_NODE_TYPE_API_USER,
PSYNC_DEFAULT_DOMAIN,
PSYNC_SDF_ID_INVALID,
PSYNC_INIT_FLAG_STDOUT_LOGGING,
&global_node_ref );
// error check
if( ret!= DTC_NONE )
{
psync_log_message(
LOG_LEVEL_ERROR,
"%s : (%u) -- psync_init returned DTC %d",
__FILE__,
__LINE__,
ret );
return EXIT_FAILURE;
}
// nodes typically should shutdown after handling SIGINT
// hook up the control-c signal handler, sets exit signaled flag
signal( SIGINT, sig_handler );
// allow signals to interrupt
siginterrupt( SIGINT, 1 );
printf( "attaching to shared memory queue with key: 0x%lX (%lu)\n",
shdmem_key,
shdmem_key );
// attach to existing shared memory queue
ret = psync_shdmem_queue_attach( shdmem_key, &shdmem_queue );
// error check
if( ret!= DTC_NONE )
{
psync_log_message(
LOG_LEVEL_ERROR,
"%s : (%u) -- psync_shdmem_queue_attach returned DTC %d",
__FILE__,
__LINE__,
ret );
goto GRACEFUL_EXIT_STMNT;
}
// arbitrary large size
shdmem_buffer_size = (1980 * 1400 * 4);
// allocate shared memory buffer, enough space for a full raw frame
if( (shdmem_buffer = malloc( shdmem_buffer_size )) == NULL )
{
psync_log_message(
LOG_LEVEL_ERROR,
"%s : (%u) -- failed to allocate shared memory frame buffer - size %lu bytes",
__FILE__,
__LINE__,
shdmem_buffer_size );
goto GRACEFUL_EXIT_STMNT;
}
printf( "waiting for image data on shared memory queue with key: 0x%lX (%lu)\n",
shdmem_key,
shdmem_key );
// while we haven't seen a valid image data message
while( publisher_format == PIXEL_FORMAT_INVALID )
{
// check for data
if( psync_shdmem_queue_try_pop( &shdmem_queue, shdmem_buffer, shdmem_buffer_size ) == DTC_NONE )
{
// cast header
ps_shdmem_message_header * const header =
(ps_shdmem_message_header*) &shdmem_buffer[ 0 ];
// cast message
ps_shdmem_image_data_msg * const message =
(ps_shdmem_image_data_msg*) &shdmem_buffer[ sizeof(*header) ];
// check for supported image data message type
if( header->msg_type == PSYNC_SHDMEM_MSG_TYPE_IMAGE_DATA )
{
unsigned int producer_valid = 0;
char format_string[256];
// check for supported pixel format
if( message->pixel_format == PIXEL_FORMAT_H264 )
{
producer_valid = 1;
strncpy( format_string, "H264 (encoded)", sizeof(format_string) );
}
else if( message->pixel_format == PIXEL_FORMAT_MJPEG )
{
producer_valid = 1;
strncpy( format_string, "MJPEG (encoded)", sizeof(format_string) );
}
else if( message->pixel_format == PIXEL_FORMAT_RGB24 )
{
producer_valid = 1;
strncpy( format_string, "RGB (raw)", sizeof(format_string) );
}
// get image information if supported
if( producer_valid != 0 )
{
printf( "found shared memory image data producer - pixel_format: '%s'\n",
format_string );
// set publisher format
publisher_format = message->pixel_format;
// set publisher pixel format and w/h
publisher_width = (unsigned long) message->width;
publisher_height = (unsigned long) message->height;
}
}
}
// check for exit
if( global_exit_signal != 0 )
{
goto GRACEFUL_EXIT_STMNT;
}
}
// if viewing an encoded pixel format
if( publisher_format != PIXEL_FORMAT_RGB24 )
{
// initialize decoder, frame-rate will be determined by stream if possible, otherwise use default
ret = psync_video_decoder_init(
&video_decoder,
publisher_format,
publisher_width,
publisher_height,
desired_decoder_format,
publisher_width,
publisher_height,
PSYNC_VIDEO_DEFAULT_FRAMES_PER_SECOND );
// error check
if( ret!= DTC_NONE )
{
psync_log_message(
LOG_LEVEL_ERROR,
"%s : (%u) -- psync_video_decoder_init returned DTC %d",
__FILE__,
__LINE__,
ret );
goto GRACEFUL_EXIT_STMNT;
}
}
// set the decoded frame size, RGB
decoded_frame_size = (publisher_width * publisher_height * 3);
// allocate decoder buffer, enough space for a full raw frame in the desired pixel format which is RGB in this example
if( (decoder_buffer = malloc( decoded_frame_size )) == NULL )
{
psync_log_message(
LOG_LEVEL_ERROR,
"%s : (%u) -- failed to allocate decoder buffer - size %lu bytes",
__FILE__,
__LINE__,
decoded_frame_size );
goto GRACEFUL_EXIT_STMNT;
}
// create GUI
if( (gui = gui_init( NODE_NAME, publisher_width, publisher_height )) == NULL )
{
psync_log_message(
LOG_LEVEL_ERROR,
"%s : (%u) -- failed to create GUI context",
__FILE__,
__LINE__ );
goto GRACEFUL_EXIT_STMNT;
}
// enabled help
gui->config.help_visible = 1;
// main event loop
// loop until signaled (control-c)
while( global_exit_signal == 0 )
{
// zero
bytes_decoded = 0;
// pointer to the default image buffer
const unsigned char * buffer_ptr = decoder_buffer;
// check for data
ret = psync_shdmem_queue_try_pop( &shdmem_queue, shdmem_buffer, shdmem_buffer_size );
if( ret == DTC_NONE )
{
// cast header
ps_shdmem_message_header * const header =
(ps_shdmem_message_header*) &shdmem_buffer[ 0 ];
// cast message
ps_shdmem_image_data_msg * const message =
(ps_shdmem_image_data_msg*) &shdmem_buffer[ sizeof(*header) ];
// check for supported image data message type
if( header->msg_type == PSYNC_SHDMEM_MSG_TYPE_IMAGE_DATA )
{
// check if publisher is what we've initialized to using its and pixel format
if( message->pixel_format == publisher_format )
{
// if receiving an encoded pixel format
if( publisher_format != PIXEL_FORMAT_RGB24 )
{
// decode the data
ret = psync_video_decoder_decode(
&video_decoder,
message->timestamp,
&shdmem_buffer[ sizeof(*header) + sizeof(*message) ],
message->data_size );
// error check
if( ret!= DTC_NONE )
{
psync_log_message(
LOG_LEVEL_ERROR,
"%s : (%u) -- psync_video_decoder_decode returned DTC %d",
__FILE__,
__LINE__,
ret );
goto GRACEFUL_EXIT_STMNT;
}
// copy the decoded bytes into our local buffer, this is the raw frame is our desired pixel format
ret = psync_video_decoder_copy_bytes(
&video_decoder,
decoder_buffer,
decoded_frame_size,
&bytes_decoded );
// error check
if( ret!= DTC_NONE )
{
psync_log_message(
LOG_LEVEL_ERROR,
"%s : (%u) -- psync_video_decoder_copy_bytes returned DTC %d",
__FILE__,
__LINE__,
ret );
goto GRACEFUL_EXIT_STMNT;
}
}
else
{
// point buffer to the raw image data, no decoding needed
buffer_ptr = &shdmem_buffer[ sizeof(*header) + sizeof(*message) ];
bytes_decoded = (unsigned long) message->data_size;
}
// if decoder/buffer has data available
if( bytes_decoded != 0 )
{
// update frame counter
if( message->frame_id != 0 )
{
gui->frame_cnt = (unsigned long) message->frame_id;
}
else
{
// default to using local indexing
gui->frame_cnt += 1;
}
// get timestamp
ps_timestamp now = 0;
(void) psync_get_timestamp( &now );
// get rx FPS
gui->rx_fps = (double) (now - last_rx_time) / 1000000.0;
gui->rx_fps = 1.0 / gui->rx_fps;
// update last timestamp
last_rx_time = now;
// update texture with new data and we're not in freeze-frame
if( gui->config.freeze_frame == 0 )
{
gui_update_image_data( gui, buffer_ptr, bytes_decoded );
}
// reset sleep ticker
sleep_tick = 0;
}
}
}
}
else if( ret != DTC_UNAVAILABLE )
{
global_exit_signal = 1;
}
// get timestamp
ret = psync_get_timestamp( ×tamp );
// update gui
gui_update( gui, timestamp, &time_to_draw );
// disable sleep ticker if redraw time is less than 5 ms
if( time_to_draw < 5000 )
{
// reset ticker
sleep_tick = 0;
}
// check sleep ticker
if( sleep_tick >= 10 )
{
// sleep ~1 ms
usleep( 1000 );
// reset to enabled
sleep_tick = 10;
}
// increment sleep ticker
sleep_tick++;
}
// using 'goto' to allow for an easy example exit
GRACEFUL_EXIT_STMNT:
global_exit_signal = 1;
// release GUI
if( gui != NULL )
{
// free GUI
gui_release( gui );
free( gui );
gui = NULL;
}
// release video decoder
ret = psync_video_decoder_release( &video_decoder );
// release core API
ret = psync_release( &global_node_ref );
// free shared memory buffer
if( shdmem_buffer != NULL )
{
free(shdmem_buffer);
}
// free decoder buffer
if( decoder_buffer != NULL )
{
free(decoder_buffer);
}
return EXIT_SUCCESS;
}