-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathmain.c
executable file
·683 lines (564 loc) · 18.9 KB
/
main.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
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
/*
Copyright (c) 2012, Broadcom Europe Ltd
All rights reserved.
Redistribution and use in source and binary forms, with or without
modification, are permitted provided that the following conditions are met:
* Redistributions of source code must retain the above copyright
notice, this list of conditions and the following disclaimer.
* Redistributions in binary form must reproduce the above copyright
notice, this list of conditions and the following disclaimer in the
documentation and/or other materials provided with the distribution.
* Neither the name of the copyright holder nor the
names of its contributors may be used to endorse or promote products
derived from this software without specific prior written permission.
THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND
ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE FOR ANY
DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
(INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
(INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
*/
// Video deocode demo using OpenMAX IL though the ilcient helper library
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <netinet/tcp.h>
#include <arpa/inet.h>
#include <sys/types.h>
#include <sys/socket.h>
#include <netdb.h>
#include <unistd.h>
#include <getopt.h>
#include <pthread.h>
#include <time.h>
#include <signal.h>
static int terminated;
#include "ariss_overlay.h"
#include "backgroundLayer.h"
#include "imageLayer.h"
#include "key.h"
#include "loadpng.h"
#include "bcm_host.h"
#include "bcm_host.h"
#include "ilclient.h"
#include "input_buffer.h"
rxBuffer_t rxBuffer;
rxBuffer_t rxTcpBuffer;
#include "ts/ts.h"
#define TCP_RX_WAIT 4000
#define VIDEO_PID 256
#define SCREEN_RESOLUTION_HEIGHT 1080
#define SCREEN_RESOLUTION_WIDTH 1920
static FILE *input_file;
static int input_socket;
enum input_source_t {SOURCE_TCP, SOURCE_FILE, SOURCE_NONE};
static enum input_source_t input_source = SOURCE_NONE;
static uint64_t data_received = 0;
static uint64_t timestamp(void) {
uint64_t _ts = 0;
struct timespec spec;
clock_gettime(CLOCK_REALTIME, &spec);
_ts = (uint64_t) spec.tv_sec;
return _ts;
}
static unsigned int ts_read(unsigned char* destination, unsigned int length)
{
switch(input_source)
{
case SOURCE_FILE:
return fread(destination, 1, length, input_file);
case SOURCE_TCP:
return rxBufferTimedWaitPop(&rxBuffer, destination, length, TCP_RX_WAIT);
case SOURCE_NONE:
default:
return 0;
}
}
#define OVERLAY_DISPLAY_NUM 2
#define OVERLAY_LAYER_NUM 1
#define OVERLAY_LAYER_X (1*(SCREEN_RESOLUTION_WIDTH-800)/25)
#define OVERLAY_LAYER_Y (24*(SCREEN_RESOLUTION_HEIGHT-200)/25)
pthread_t video_thread;
void video_play(void);
void* video_loop(void *arg)
{
(void) arg;
uint32_t canary_length = 1;
uint8_t canary_buffer[1];
while(1)
{
while(ts_read(canary_buffer,canary_length) == 0) {};
printf("Video in buffer. Preparing overlay..\n");
char tmp_overlay_filename[] = "/tmp/ariss-video-overlay_XXXXXX";
int tmp_overlay_file = mkstemp(tmp_overlay_filename);
if(tmp_overlay_file < 0)
{
printf("Error opening temporary file for overlay!\n");
break;
}
write(tmp_overlay_file, ariss_overlay_png, ariss_overlay_png_len);
close(tmp_overlay_file);
DISPMANX_MODEINFO_T overlay_info;
IMAGE_LAYER_T overlay_imageLayer;
DISPMANX_DISPLAY_HANDLE_T overlay_display = vc_dispmanx_display_open(OVERLAY_DISPLAY_NUM);
if(overlay_display == 0)
{
fprintf(stderr,"Error opening dispmanx display\n");
}
if(vc_dispmanx_display_get_info(overlay_display, &overlay_info) != 0)
{
fprintf(stderr, "Error loading dispmanx display info.\n");
}
if (loadPng(&(overlay_imageLayer.image), tmp_overlay_filename) == false)
{
fprintf(stderr, "Error loading overlay png: %s\n", tmp_overlay_filename);
}
createResourceImageLayer(&overlay_imageLayer, OVERLAY_LAYER_NUM);
DISPMANX_UPDATE_HANDLE_T overlay_update = vc_dispmanx_update_start(0);
if(overlay_update == 0)
{
fprintf(stderr, "Error creating overlay update handle\n");
}
addElementImageLayerOffset(&overlay_imageLayer, OVERLAY_LAYER_X, OVERLAY_LAYER_Y, overlay_display, overlay_update);
if(vc_dispmanx_update_submit_sync(overlay_update) != 0)
{
fprintf(stderr, "Error updating overlay image with offsets\n");
}
printf("Starting Video player..\n");
video_play();
printf("Video player stopped.\n");
printf("Stopping video playback.\n");
destroyImageLayer(&overlay_imageLayer);
if(vc_dispmanx_display_close(overlay_display) != 0)
{
fprintf(stderr, "Error closing dispmanx display\n");
}
unlink(tmp_overlay_filename);
printf("Overlay stopped. Waiting for data..");
}
}
void video_play(void)
{
OMX_VIDEO_PARAM_PORTFORMATTYPE format;
OMX_TIME_CONFIG_CLOCKSTATETYPE cstate;
COMPONENT_T *video_decode = NULL, *video_scheduler = NULL, *video_render = NULL, *clock = NULL;
COMPONENT_T *list[5];
TUNNEL_T tunnel[4];
ILCLIENT_T *client;
int status = 0;
memset(list, 0, sizeof(list));
memset(tunnel, 0, sizeof(tunnel));
if((client = ilclient_init()) == NULL)
{
printf("Error: Null video client!\n");
return;
}
if(OMX_Init() != OMX_ErrorNone)
{
ilclient_destroy(client);
printf("Error: OMX Init failed!\n");
return;
}
// create video_decode
if(ilclient_create_component(client, &video_decode, "video_decode", ILCLIENT_DISABLE_ALL_PORTS | ILCLIENT_ENABLE_INPUT_BUFFERS) != 0)
status = -14;
list[0] = video_decode;
// create video_render
if(status == 0 && ilclient_create_component(client, &video_render, "video_render", ILCLIENT_DISABLE_ALL_PORTS) != 0)
status = -14;
list[1] = video_render;
// create clock
if(status == 0 && ilclient_create_component(client, &clock, "clock", ILCLIENT_DISABLE_ALL_PORTS) != 0)
status = -14;
list[2] = clock;
memset(&cstate, 0, sizeof(cstate));
cstate.nSize = sizeof(cstate);
cstate.nVersion.nVersion = OMX_VERSION;
cstate.eState = OMX_TIME_ClockStateWaitingForStartTime;
cstate.nWaitMask = 1;
if(clock != NULL && OMX_SetParameter(ILC_GET_HANDLE(clock), OMX_IndexConfigTimeClockState, &cstate) != OMX_ErrorNone)
status = -13;
// create video_scheduler
if(status == 0 && ilclient_create_component(client, &video_scheduler, "video_scheduler", ILCLIENT_DISABLE_ALL_PORTS) != 0)
status = -14;
list[3] = video_scheduler;
set_tunnel(tunnel, video_decode, 131, video_scheduler, 10);
set_tunnel(tunnel+1, video_scheduler, 11, video_render, 90);
set_tunnel(tunnel+2, clock, 80, video_scheduler, 12);
// setup clock tunnel first
if(status == 0 && ilclient_setup_tunnel(tunnel+2, 0, 0) != 0)
status = -15;
else
ilclient_change_component_state(clock, OMX_StateExecuting);
if(status == 0)
ilclient_change_component_state(video_decode, OMX_StateIdle);
memset(&format, 0, sizeof(OMX_VIDEO_PARAM_PORTFORMATTYPE));
format.nSize = sizeof(OMX_VIDEO_PARAM_PORTFORMATTYPE);
format.nVersion.nVersion = OMX_VERSION;
format.nPortIndex = 130;
//format.eCompressionFormat = OMX_VIDEO_CodingAVC;
//format.eCompressionFormat = OMX_VIDEO_CodingMPEG4;
format.eCompressionFormat = OMX_VIDEO_CodingMPEG2;
//format.eCompressionFormat = OMX_VIDEO_CodingAutoDetect;
format.xFramerate = 29.97 * (1<<16);
/* Set fullscreen 1080x1920, forced aspect ratio */
OMX_ERRORTYPE omx_err;
OMX_CONFIG_DISPLAYREGIONTYPE configDisplay;
memset(&configDisplay, 0, sizeof(configDisplay));
configDisplay.nSize = sizeof(configDisplay);
configDisplay.nVersion.s.nVersionMajor = OMX_VERSION_MAJOR;
configDisplay.nVersion.s.nVersionMinor = OMX_VERSION_MINOR;
configDisplay.nVersion.s.nRevision = OMX_VERSION_REVISION;
configDisplay.nVersion.s.nStep = OMX_VERSION_STEP;
configDisplay.nPortIndex = 90; //m_omx_render.GetInputPort();
configDisplay.set = (OMX_DISPLAYSETTYPE)(OMX_DISPLAY_SET_NOASPECT | OMX_DISPLAY_SET_MODE | OMX_DISPLAY_SET_SRC_RECT | OMX_DISPLAY_SET_FULLSCREEN | OMX_DISPLAY_SET_PIXEL);
configDisplay.noaspect = OMX_TRUE;
configDisplay.mode = OMX_DISPLAY_MODE_LETTERBOX;
configDisplay.src_rect.x_offset = (int)(0+0.5f);
configDisplay.src_rect.y_offset = (int)(0+0.5f);
configDisplay.src_rect.width = (int)(SCREEN_RESOLUTION_WIDTH+0.5f);
configDisplay.src_rect.height = (int)(SCREEN_RESOLUTION_HEIGHT+0.5f);
configDisplay.fullscreen = OMX_TRUE;
configDisplay.pixel_x = 0;
configDisplay.pixel_y = 0;
configDisplay.layer = 1;
omx_err = OMX_SetConfig(ILC_GET_HANDLE(video_render), OMX_IndexConfigDisplayRegion, &configDisplay);
if (omx_err != OMX_ErrorNone)
{
printf("Error setting render display options! omx_err(0x%08x)\n", omx_err);
}
if(status == 0 &&
OMX_SetParameter(ILC_GET_HANDLE(video_decode), OMX_IndexParamVideoPortFormat, &format) == OMX_ErrorNone &&
ilclient_enable_port_buffers(video_decode, 130, NULL, NULL, NULL) == 0)
{
OMX_BUFFERHEADERTYPE *buf;
int port_settings_changed = 0;
int first_packet = 1;
ilclient_change_component_state(video_decode, OMX_StateExecuting);
while((buf = ilclient_get_input_buffer(video_decode, 130, 1)) != NULL)
{
// feed data and wait until we get port settings changed
buf->nFilledLen = ts_read(buf->pBuffer, buf->nAllocLen);
if(buf->nFilledLen == 0)
break;
buf->nOffset = 0;
if(port_settings_changed == 0 &&
((buf->nFilledLen > 0 && ilclient_remove_event(video_decode, OMX_EventPortSettingsChanged, 131, 0, 0, 1) == 0) ||
(buf->nFilledLen == 0 && ilclient_wait_for_event(video_decode, OMX_EventPortSettingsChanged, 131, 0, 0, 1,
ILCLIENT_EVENT_ERROR | ILCLIENT_PARAMETER_CHANGED, 10000) == 0)))
{
port_settings_changed = 1;
if(ilclient_setup_tunnel(tunnel, 0, 0) != 0)
{
status = -7;
break;
}
ilclient_change_component_state(video_scheduler, OMX_StateExecuting);
// now setup tunnel to video_
if(ilclient_setup_tunnel(tunnel+1, 0, 1000) != 0)
{
status = -12;
break;
}
ilclient_change_component_state(video_render, OMX_StateExecuting);
}
if(first_packet)
{
buf->nFlags = OMX_BUFFERFLAG_STARTTIME;
first_packet = 0;
}
else
buf->nFlags = OMX_BUFFERFLAG_TIME_UNKNOWN;
if(OMX_EmptyThisBuffer(ILC_GET_HANDLE(video_decode), buf) != OMX_ErrorNone)
{
status = -6;
break;
}
}
buf->nFilledLen = 0;
buf->nFlags = OMX_BUFFERFLAG_TIME_UNKNOWN | OMX_BUFFERFLAG_EOS;
if(OMX_EmptyThisBuffer(ILC_GET_HANDLE(video_decode), buf) != OMX_ErrorNone)
status = -20;
// wait for EOS from render
ilclient_wait_for_event(video_render, OMX_EventBufferFlag, 90, 0, OMX_BUFFERFLAG_EOS, 0,
ILCLIENT_BUFFER_FLAG_EOS, -1);
// need to flush the renderer to allow video_decode to disable its input port
ilclient_flush_tunnels(tunnel, 0);
}
ilclient_disable_tunnel(tunnel);
ilclient_disable_tunnel(tunnel+1);
ilclient_disable_tunnel(tunnel+2);
ilclient_disable_port_buffers(video_decode, 130, NULL, NULL, NULL);
ilclient_teardown_tunnels(tunnel);
ilclient_state_transition(list, OMX_StateIdle);
ilclient_state_transition(list, OMX_StateLoaded);
ilclient_cleanup_components(list);
OMX_Deinit();
ilclient_destroy(client);
return;
}
static int ts_file_open(char* filename)
{
if((input_file = fopen(filename, "rb")) == NULL)
return -1;
else
return 0;
}
static int ts_tcp_open(char *hostname, char *port, int ai_family)
{
int r;
struct addrinfo hints;
struct addrinfo *re, *rp;
char s[INET6_ADDRSTRLEN];
memset(&hints, 0, sizeof(struct addrinfo));
hints.ai_family = ai_family;
hints.ai_socktype = SOCK_STREAM;
r = getaddrinfo(hostname, port, &hints, &re);
if(r != 0)
{
printf("Error resolving hostname: %s\n", gai_strerror(r));
return(-1);
}
/* Try IPv6 first */
for(input_socket = -1, rp = re; input_socket == -1 && rp != NULL; rp = rp->ai_next)
{
if(rp->ai_addr->sa_family != AF_INET6) continue;
inet_ntop(AF_INET6, &(((struct sockaddr_in6 *) rp->ai_addr)->sin6_addr), s, INET6_ADDRSTRLEN);
//printf("Connecting to [%s]:%d\n", s, ntohs((((struct sockaddr_in6 *) rp->ai_addr)->sin6_port)));
input_socket = socket(rp->ai_family, rp->ai_socktype, rp->ai_protocol);
if(input_socket == -1)
{
//perror("socket");
continue;
}
if(connect(input_socket, rp->ai_addr, rp->ai_addrlen) == -1)
{
//perror("connect");
close(input_socket);
input_socket = -1;
}
}
/* Try IPv4 next */
for(rp = re; input_socket == -1 && rp != NULL; rp = rp->ai_next)
{
if(rp->ai_addr->sa_family != AF_INET) continue;
inet_ntop(AF_INET, &(((struct sockaddr_in *) rp->ai_addr)->sin_addr), s, INET6_ADDRSTRLEN);
//printf("Connecting to %s:%d\n", s, ntohs((((struct sockaddr_in *) rp->ai_addr)->sin_port)));
input_socket = socket(rp->ai_family, rp->ai_socktype, rp->ai_protocol);
if(input_socket == -1)
{
//perror("socket");
continue;
}
if(connect(input_socket, rp->ai_addr, rp->ai_addrlen) == -1)
{
//perror("connect");
close(input_socket);
input_socket = -1;
}
}
freeaddrinfo(re);
int value = 1;
if (setsockopt(input_socket, IPPROTO_TCP, TCP_NODELAY, (char *)&value, sizeof(int)) < 0)
return -2;
if(input_socket < 0)
return -1;
else
return 0;
}
static void _print_usage(void)
{
printf(
"\n"
"Usage: ariss-video [options]\n"
"\n"
" -f, --file <name> Set filename to read from. (conflicts with -h)\n"
" -h, --host <name> Set the hostname to read data from (conflicts with -f)\n"
" -p, --port <number> Set the port number to connect to on the host. Default: 5679\n"
" -4, --ipv4 Force IPv4 only.\n"
" -6, --ipv6 Force IPv6 only.\n"
"\n"
);
}
pthread_t tcp_rx_thread;
struct tcp_rx_params_t {
char *hostname;
char *port;
int ai_family;
};
struct tcp_rx_params_t tcp_rx_params;
void* tcp_rx_loop(void *arg)
{
(void) arg;
int status;
uint8_t buffer[16384];
uint32_t length;
printf("Attempting to connect to TCP Input %s:%s\n",tcp_rx_params.hostname,tcp_rx_params.port);
while((status = ts_tcp_open(tcp_rx_params.hostname, tcp_rx_params.port, tcp_rx_params.ai_family)) < 0)
{
sleep(1);
}
printf("TCP Input Connected to %s:%s\n",tcp_rx_params.hostname,tcp_rx_params.port);
while(1)
{
length = recv(input_socket, buffer, 16384, 0);
if(length==0)
{
close(input_socket);
printf("Attempting to reconnect to TCP Input %s:%s\n",tcp_rx_params.hostname,tcp_rx_params.port);
while((status = ts_tcp_open(tcp_rx_params.hostname, tcp_rx_params.port, tcp_rx_params.ai_family)) < 0)
{
sleep(1);
}
printf("TCP Input re-connected to %s:%s\n",tcp_rx_params.hostname,tcp_rx_params.port);
}
else if(length<=16384)
{
//printf("Pushing to tcp buffer (%d bytes)\n", length);
rxBufferPush(&rxTcpBuffer, buffer, length);
data_received = timestamp();
}
}
}
pthread_t tcp_buffer_thread;
void* tcp_buffer_loop(void *arg)
{
(void) arg;
int status;
uint8_t buffer[16384];
uint32_t length;
uint8_t video_buffer[16384];
ts_header_t ts_header;
while(1)
{
length = rxBufferWaitTSPop(&rxTcpBuffer, buffer);
if(length != TS_PACKET_SIZE)
{
printf("Incoming packet invalid size, expected %d bytes, got %d\n", TS_PACKET_SIZE, length);
continue;
}
/* Feed in the packet(s) */
if((status = ts_parse_header(&ts_header, buffer)) != TS_OK)
{
printf("Failed to parse TS Header! (Error: %d)\n", status);
continue;
}
if((ts_header.pid == VIDEO_PID) && (ts_header.payload_flag > 0))
{
memcpy(video_buffer,&buffer[ts_header.payload_offset], (TS_PACKET_SIZE - ts_header.payload_offset));
//printf("Pushing to video buffer (%d bytes)\n", video_length);
rxBufferPush(&rxBuffer, video_buffer, (TS_PACKET_SIZE - ts_header.payload_offset));
}
}
}
void signal_handler(int sig)
{
if (sig == SIGINT || sig == SIGTERM)
{
terminated = 1;
}
}
int main (int argc, char **argv)
{
int opt,c;
char *filename = NULL;
char *hostname = NULL;
char *port = "5679";
int ai_family = AF_INET;
static const struct option long_options[] = {
{ "file", required_argument, 0, 'f' },
{ "host", required_argument, 0, 'h' },
{ "port", required_argument, 0, 'p' },
{ "ipv6", no_argument, 0, '6' },
{ "ipv4", no_argument, 0, '4' },
{ 0, 0, 0, 0 }
};
terminated = 0;
if(signal(SIGINT, signal_handler) == SIG_ERR
|| signal(SIGTERM, signal_handler) == SIG_ERR)
{
printf("Error setting up signal handler!\n");
return 1;
}
opterr = 0;
while((c = getopt_long(argc, argv, "f:h:p:64", long_options, &opt)) != -1)
{
switch(c)
{
case 'f': /* --filename <name> */
filename = optarg;
input_source = SOURCE_FILE;
break;
case 'h': /* --host <name> */
hostname = optarg;
input_source = SOURCE_TCP;
break;
case 'p': /* --port <number> */
port = optarg;
break;
case '6': /* --ipv6 */
ai_family = AF_INET6;
break;
case '4': /* --ipv4 */
ai_family = AF_INET;
break;
case '?':
_print_usage();
return(0);
}
}
if(filename != NULL && hostname != NULL)
{
printf("Error: Only one source permitted!");
_print_usage();
return(-1);
}
switch(input_source)
{
case SOURCE_FILE:
if(ts_file_open(filename) < 0)
{
printf("Error: Opening file input failed!\n");
return(-1);
}
else
{
printf("File Input Opened: %s\n",filename);
}
break;
case SOURCE_TCP:
rxBufferInit(&rxBuffer);
rxBufferInit(&rxTcpBuffer);
tcp_rx_params.hostname = hostname;
tcp_rx_params.port = port;
tcp_rx_params.ai_family = ai_family;
pthread_create(&tcp_rx_thread, NULL, &tcp_rx_loop, NULL);
pthread_create(&tcp_buffer_thread, NULL, &tcp_buffer_loop, NULL);
break;
case SOURCE_NONE:
printf("Error: No source specified!\n");
_print_usage();
return(-1);
}
printf("Initialising videocore..\n");
bcm_host_init();
printf("Starting video thread..\n");
pthread_create(&video_thread, NULL, &video_loop, NULL);
while(!terminated) { usleep(100*1000); }
switch(input_source)
{
case SOURCE_FILE:
fclose(input_file);
break;
case SOURCE_TCP:
close(input_socket);
break;
case SOURCE_NONE:
break;
}
return 0;
}