Debian Codegraph @

Xpjproject/pjsip-apps/src/samples/vid_streamutil.c

raw symbols SCIP
1 /*
2 * Copyright (C) 2011 Teluu Inc. (http://www.teluu.com)
3 *
4 * This program is free software; you can redistribute it and/or modify
5 * it under the terms of the GNU General Public License as published by
6 * the Free Software Foundation; either version 2 of the License, or
7 * (at your option) any later version.
8 *
9 * This program is distributed in the hope that it will be useful,
10 * but WITHOUT ANY WARRANTY; without even the implied warranty of
11 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12 * GNU General Public License for more details.
13 *
14 * You should have received a copy of the GNU General Public License
15 * along with this program; if not, write to the Free Software
16 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
17 */
18
19
20 /**
21 * \page page_pjmedia_samples_vid_streamutil_c Samples: Video Streaming
22 *
23 * This example mainly demonstrates how to stream video to remote
24 * peer using RTP.
25 *
26 * This file is pjsip-apps/src/samples/vid_streamutil.c
27 *
28 * \includelineno vid_streamutil.c
29 */
30
31 #include <pjlib.h>
32 #include <pjlib-util.h>
33 #include <pjmedia.h>
34 #include <pjmedia-codec.h>
35 #include <pjmedia/transport_srtp.h>
36
37
38 #if defined(PJMEDIA_HAS_VIDEO) && (PJMEDIA_HAS_VIDEO != 0)
39
40
41 #include <stdlib.h> /* atoi() */
42 #include <stdio.h>
43
44 #include "util.h"
45
46
47 static const char *desc =
48 " vid_streamutil \n"
49 "\n"
50 " PURPOSE: \n"
51 " Demonstrate how to use pjmedia video stream component to \n"
52 " transmit/receive RTP packets to/from video device/file. \n"
53 "\n"
54 "\n"
55 " USAGE: \n"
56 " vid_streamutil [options] \n"
57 "\n"
58 "\n"
59 " Options: \n"
60 " --codec=CODEC Set the codec name. \n"
61 " --local-port=PORT Set local RTP port (default=4000) \n"
62 " --remote=IP:PORT Set the remote peer. If this option is set, \n"
63 " the program will transmit RTP audio to the \n"
64 " specified address. (default: recv only) \n"
65 " --play-file=AVI Send video from the AVI file instead of from \n"
66 " the video device. \n"
67 " --send-recv Set stream direction to bidirectional. \n"
68 " --send-only Set stream direction to send only \n"
69 " --recv-only Set stream direction to recv only (default) \n"
70
71 " --send-width Video width to be sent \n"
72 " --send-height Video height to be sent \n"
73 " --send-width and --send-height not applicable \n"
74 " for file streaming (see --play-file) \n"
75
76 " --send-pt Payload type for sending \n"
77 " --recv-pt Payload type for receiving \n"
78
79 #if defined(PJMEDIA_HAS_SRTP) && (PJMEDIA_HAS_SRTP != 0)
80 " --use-srtp[=NAME] Enable SRTP with crypto suite NAME \n"
81 " e.g: AES_CM_128_HMAC_SHA1_80 (default), \n"
82 " AES_CM_128_HMAC_SHA1_32 \n"
83 " Use this option along with the TX & RX keys, \n"
84 " formated of 60 hex digits (e.g: E148DA..) \n"
85 " --srtp-tx-key SRTP key for transmiting \n"
86 " --srtp-rx-key SRTP key for receiving \n"
87 #endif
88
89 "\n"
90 ;
91
92 #define THIS_FILE "vid_streamutil.c"
93
94
95 /* If set, local renderer will be created to play original file */
96 #define HAS_LOCAL_RENDERER_FOR_PLAY_FILE 1
97
98
99 /* Default width and height for the renderer, better be set to maximum
100 * acceptable size.
101 */
102 #define DEF_RENDERER_WIDTH 640
103 #define DEF_RENDERER_HEIGHT 480
104
105
106 /* Hexa string to octet array */
107 int my_hex_string_to_octet_string(char *raw, char *hex, int len)
108 {
109 int i;
110 for (i = 0; i < len; i+=2) {
111 int tmp;
112 if (i+1 >= len || !pj_isxdigit(hex[i]) || !pj_isxdigit(hex[i+1]))
113 return i;
114 tmp = pj_hex_digit_to_val((unsigned char)hex[i]) << 4;
115 tmp |= pj_hex_digit_to_val((unsigned char)hex[i+1]);
116 raw[i/2] = (char)(tmp & 0xFF);
117 }
118 return len;
119 }
120
121 /*
122 * Register all codecs.
123 */
124 static pj_status_t init_codecs(pj_pool_factory *pf)
125 {
126 pj_status_t status;
127
128 /* To suppress warning about unused var when all codecs are disabled */
129 PJ_UNUSED_ARG(status);
130 PJ_UNUSED_ARG(pf);
131
132 #if defined(PJMEDIA_HAS_OPENH264_CODEC) && PJMEDIA_HAS_OPENH264_CODEC != 0
133 status = pjmedia_codec_openh264_vid_init(NULL, pf);
134 PJ_ASSERT_RETURN(status == PJ_SUCCESS, status);
135 #endif
136
137 #if defined(PJMEDIA_HAS_VID_TOOLBOX_CODEC) && \
138 PJMEDIA_HAS_VID_TOOLBOX_CODEC != 0
139 status = pjmedia_codec_vid_toolbox_init(NULL, pf);
140 PJ_ASSERT_RETURN(status == PJ_SUCCESS, status);
141 #endif
142
143 #if defined(PJMEDIA_HAS_VPX_CODEC) && PJMEDIA_HAS_VPX_CODEC != 0
144 status = pjmedia_codec_vpx_vid_init(NULL, pf);
145 PJ_ASSERT_RETURN(status == PJ_SUCCESS, status);
146 #endif
147
148 #if defined(PJMEDIA_HAS_FFMPEG_VID_CODEC) && PJMEDIA_HAS_FFMPEG_VID_CODEC != 0
149 status = pjmedia_codec_ffmpeg_vid_init(NULL, pf);
150 PJ_ASSERT_RETURN(status == PJ_SUCCESS, status);
151 #endif
152
153 return PJ_SUCCESS;
154 }
155
156 /*
157 * Register all codecs.
158 */
159 static void deinit_codecs()
160 {
161 #if defined(PJMEDIA_HAS_FFMPEG_VID_CODEC) && PJMEDIA_HAS_FFMPEG_VID_CODEC != 0
162 pjmedia_codec_ffmpeg_vid_deinit();
163 #endif
164
165 #if defined(PJMEDIA_HAS_OPENH264_CODEC) && PJMEDIA_HAS_OPENH264_CODEC != 0
166 pjmedia_codec_openh264_vid_deinit();
167 #endif
168
169 #if defined(PJMEDIA_HAS_VID_TOOLBOX_CODEC) && \
170 PJMEDIA_HAS_VID_TOOLBOX_CODEC != 0
171 pjmedia_codec_vid_toolbox_deinit();
172 #endif
173
174 #if defined(PJMEDIA_HAS_VPX_CODEC) && PJMEDIA_HAS_VPX_CODEC != 0
175 pjmedia_codec_vpx_vid_deinit();
176 #endif
177
178 }
179
180 static pj_status_t create_file_player( pj_pool_t *pool,
181 const char *file_name,
182 pjmedia_port **p_play_port)
183 {
184 pjmedia_avi_streams *avi_streams;
185 pjmedia_avi_stream *vid_stream;
186 pjmedia_port *play_port;
187 pj_status_t status;
188
189 status = pjmedia_avi_player_create_streams(pool, file_name, 0, &avi_streams);
190 if (status != PJ_SUCCESS)
191 return status;
192
193 vid_stream = pjmedia_avi_streams_get_stream_by_media(avi_streams,
194 0,
195 PJMEDIA_TYPE_VIDEO);
196 if (!vid_stream)
197 return PJ_ENOTFOUND;
198
199 play_port = pjmedia_avi_stream_get_port(vid_stream);
200 pj_assert(play_port);
201
202 *p_play_port = play_port;
203
204 return PJ_SUCCESS;
205 }
206
207 /*
208 * Create stream based on the codec, dir, remote address, etc.
209 */
210 static pj_status_t create_stream( pj_pool_t *pool,
211 pjmedia_endpt *med_endpt,
212 const pjmedia_vid_codec_info *codec_info,
213 pjmedia_vid_codec_param *codec_param,
214 pjmedia_dir dir,
215 pj_int8_t rx_pt,
216 pj_int8_t tx_pt,
217 pj_uint16_t local_port,
218 const pj_sockaddr_in *rem_addr,
219 #if defined(PJMEDIA_HAS_SRTP) && (PJMEDIA_HAS_SRTP != 0)
220 pj_bool_t use_srtp,
221 const pj_str_t *crypto_suite,
222 const pj_str_t *srtp_tx_key,
223 const pj_str_t *srtp_rx_key,
224 #endif
225 pjmedia_vid_stream **p_stream )
226 {
227 pjmedia_vid_stream_info info;
228 pjmedia_transport *transport = NULL;
229 pj_status_t status;
230 #if defined(PJMEDIA_HAS_SRTP) && (PJMEDIA_HAS_SRTP != 0)
231 pjmedia_transport *srtp_tp = NULL;
232 #endif
233
234 /* Reset stream info. */
235 pj_bzero(&info, sizeof(info));
236
237 /* Initialize stream info formats */
238 info.type = PJMEDIA_TYPE_VIDEO;
239 info.dir = dir;
240 info.codec_info = *codec_info;
241 info.tx_pt = (tx_pt == -1)? (pj_uint8_t)codec_info->pt : tx_pt;
242 info.rx_pt = (rx_pt == -1)? (pj_uint8_t)codec_info->pt : rx_pt;
243 info.ssrc = pj_rand();
244 if (codec_param)
245 info.codec_param = codec_param;
246
247 /* Copy remote address */
248 pj_memcpy(&info.rem_addr, rem_addr, sizeof(pj_sockaddr_in));
249
250 /* If remote address is not set, set to an arbitrary address
251 * (otherwise stream will assert).
252 */
253 if (info.rem_addr.addr.sa_family == 0) {
254 const pj_str_t addr = pj_str("127.0.0.1");
255 pj_sockaddr_in_init(&info.rem_addr.ipv4, &addr, 0);
256 }
257
258 /* Create media transport */
259 status = pjmedia_transport_udp_create(med_endpt, NULL, local_port,
260 0, &transport);
261 if (status != PJ_SUCCESS)
262 return status;
263
264 #if defined(PJMEDIA_HAS_SRTP) && (PJMEDIA_HAS_SRTP != 0)
265 /* Check if SRTP enabled */
266 if (use_srtp) {
267 pjmedia_srtp_crypto tx_plc, rx_plc;
268
269 status = pjmedia_transport_srtp_create(med_endpt, transport,
270 NULL, &srtp_tp);
271 if (status != PJ_SUCCESS)
272 return status;
273
274 pj_bzero(&tx_plc, sizeof(pjmedia_srtp_crypto));
275 pj_bzero(&rx_plc, sizeof(pjmedia_srtp_crypto));
276
277 tx_plc.key = *srtp_tx_key;
278 tx_plc.name = *crypto_suite;
279 rx_plc.key = *srtp_rx_key;
280 rx_plc.name = *crypto_suite;
281
282 status = pjmedia_transport_srtp_start(srtp_tp, &tx_plc, &rx_plc);
283 if (status != PJ_SUCCESS)
284 return status;
285
286 transport = srtp_tp;
287 }
288 #endif
289
290 /* Now that the stream info is initialized, we can create the
291 * stream.
292 */
293
294 status = pjmedia_vid_stream_create( med_endpt, pool, &info,
295 transport,
296 NULL, p_stream);
297
298 if (status != PJ_SUCCESS) {
299 app_perror(THIS_FILE, "Error creating stream", status);
300 pjmedia_transport_close(transport);
301 return status;
302 }
303
304 /* Start media transport */
305 pjmedia_transport_media_start(transport, 0, 0, 0, 0);
306
307 return PJ_SUCCESS;
308 }
309
310
311 typedef struct play_file_data
312 {
313 const char *file_name;
314 pjmedia_port *play_port;
315 pjmedia_port *stream_port;
316 pjmedia_vid_codec *decoder;
317 pjmedia_port *renderer;
318 void *read_buf;
319 pj_size_t read_buf_size;
320 void *dec_buf;
321 pj_size_t dec_buf_size;
322 } play_file_data;
323
324
325 static void clock_cb(const pj_timestamp *ts, void *user_data)
326 {
327 play_file_data *play_file = (play_file_data*)user_data;
328 pjmedia_frame read_frame, write_frame;
329 pj_status_t status;
330
331 PJ_UNUSED_ARG(ts);
332
333 /* Read frame from file */
334 read_frame.buf = play_file->read_buf;
335 read_frame.size = play_file->read_buf_size;
336 pjmedia_port_get_frame(play_file->play_port, &read_frame);
337
338 /* Decode frame, if needed */
339 if (play_file->decoder) {
340 pjmedia_vid_codec *decoder = play_file->decoder;
341
342 write_frame.buf = play_file->dec_buf;
343 write_frame.size = play_file->dec_buf_size;
344 status = pjmedia_vid_codec_decode(decoder, 1, &read_frame,
345 (unsigned)write_frame.size,
346 &write_frame);
347 if (status != PJ_SUCCESS || write_frame.size == 0)
348 return;
349 } else {
350 write_frame = read_frame;
351 }
352
353 /* Display frame locally */
354 if (play_file->renderer)
355 pjmedia_port_put_frame(play_file->renderer, &write_frame);
356
357 /* Send frame */
358 pjmedia_port_put_frame(play_file->stream_port, &write_frame);
359 }
360
361
362 /*
363 * usage()
364 */
365 static void usage()
366 {
367 puts(desc);
368 }
369
370 /*
371 * main()
372 */
373 static int main_func(int argc, char *argv[])
374 {
375 pj_caching_pool cp;
376 pjmedia_endpt *med_endpt;
377 pj_pool_t *pool;
378 pjmedia_vid_stream *stream = NULL;
379 pjmedia_port *enc_port, *dec_port;
380 char addr[PJ_INET_ADDRSTRLEN];
381 pj_status_t status;
382
383 pjmedia_vid_port *capture=NULL, *renderer=NULL;
384 pjmedia_vid_port_param vpp;
385
386 #if defined(PJMEDIA_HAS_SRTP) && (PJMEDIA_HAS_SRTP != 0)
387 /* SRTP variables */
388 pj_bool_t use_srtp = PJ_FALSE;
389 char tmp_tx_key[64];
390 char tmp_rx_key[64];
391 pj_str_t srtp_tx_key = {NULL, 0};
392 pj_str_t srtp_rx_key = {NULL, 0};
393 pj_str_t srtp_crypto_suite = {NULL, 0};
394 int tmp_key_len;
395 #endif
396
397 /* Default values */
398 const pjmedia_vid_codec_info *codec_info;
399 pjmedia_vid_codec_param codec_param;
400 pjmedia_dir dir = PJMEDIA_DIR_DECODING;
401 pj_sockaddr_in remote_addr;
402 pj_uint16_t local_port = 4000;
403 char *codec_id = NULL;
404 pjmedia_rect_size tx_size = {0};
405 pj_int8_t rx_pt = -1, tx_pt = -1;
406
407 play_file_data play_file = { NULL };
408 pjmedia_port *play_port = NULL;
409 pjmedia_vid_codec *play_decoder = NULL;
410 pjmedia_clock *play_clock = NULL;
411
412 enum {
413 OPT_CODEC = 'c',
414 OPT_LOCAL_PORT = 'p',
415 OPT_REMOTE = 'r',
416 OPT_PLAY_FILE = 'f',
417 OPT_SEND_RECV = 'b',
418 OPT_SEND_ONLY = 's',
419 OPT_RECV_ONLY = 'i',
420 OPT_SEND_WIDTH = 'W',
421 OPT_SEND_HEIGHT = 'H',
422 OPT_RECV_PT = 't',
423 OPT_SEND_PT = 'T',
424 #if defined(PJMEDIA_HAS_SRTP) && (PJMEDIA_HAS_SRTP != 0)
425 OPT_USE_SRTP = 'S',
426 #endif
427 OPT_SRTP_TX_KEY = 'x',
428 OPT_SRTP_RX_KEY = 'y',
429 OPT_HELP = 'h',
430 };
431
432 struct pj_getopt_option long_options[] = {
433 { "codec", 1, 0, OPT_CODEC },
434 { "local-port", 1, 0, OPT_LOCAL_PORT },
435 { "remote", 1, 0, OPT_REMOTE },
436 { "play-file", 1, 0, OPT_PLAY_FILE },
437 { "send-recv", 0, 0, OPT_SEND_RECV },
438 { "send-only", 0, 0, OPT_SEND_ONLY },
439 { "recv-only", 0, 0, OPT_RECV_ONLY },
440 { "send-width", 1, 0, OPT_SEND_WIDTH },
441 { "send-height", 1, 0, OPT_SEND_HEIGHT },
442 { "recv-pt", 1, 0, OPT_RECV_PT },
443 { "send-pt", 1, 0, OPT_SEND_PT },
444 #if defined(PJMEDIA_HAS_SRTP) && (PJMEDIA_HAS_SRTP != 0)
445 { "use-srtp", 2, 0, OPT_USE_SRTP },
446 { "srtp-tx-key", 1, 0, OPT_SRTP_TX_KEY },
447 { "srtp-rx-key", 1, 0, OPT_SRTP_RX_KEY },
448 #endif
449 { "help", 0, 0, OPT_HELP },
450 { NULL, 0, 0, 0 },
451 };
452
453 int c;
454 int option_index;
455
456
457 pj_bzero(&remote_addr, sizeof(remote_addr));
458
459
460 /* init PJLIB : */
461 status = pj_init();
462 PJ_ASSERT_RETURN(status == PJ_SUCCESS, 1);
463
464
465 /* Parse arguments */
466 pj_optind = 0;
467 while((c=pj_getopt_long(argc,argv, "h", long_options, &option_index))!=-1)
468 {
469 switch (c) {
470 case OPT_CODEC:
471 codec_id = pj_optarg;
472 break;
473
474 case OPT_LOCAL_PORT:
475 local_port = (pj_uint16_t) atoi(pj_optarg);
476 if (local_port < 1) {
477 printf("Error: invalid local port %s\n", pj_optarg);
478 return 1;
479 }
480 break;
481
482 case OPT_REMOTE:
483 {
484 pj_str_t ip = pj_str(strtok(pj_optarg, ":"));
485 pj_uint16_t port = (pj_uint16_t) atoi(strtok(NULL, ":"));
486
487 status = pj_sockaddr_in_init(&remote_addr, &ip, port);
488 if (status != PJ_SUCCESS) {
489 app_perror(THIS_FILE, "Invalid remote address", status);
490 return 1;
491 }
492 }
493 break;
494
495 case OPT_PLAY_FILE:
496 play_file.file_name = pj_optarg;
497 break;
498
499 case OPT_SEND_RECV:
500 dir = PJMEDIA_DIR_ENCODING_DECODING;
501 break;
502
503 case OPT_SEND_ONLY:
504 dir = PJMEDIA_DIR_ENCODING;
505 break;
506
507 case OPT_RECV_ONLY:
508 dir = PJMEDIA_DIR_DECODING;
509 break;
510
511 case OPT_SEND_WIDTH:
512 tx_size.w = (unsigned)atoi(pj_optarg);
513 break;
514
515 case OPT_SEND_HEIGHT:
516 tx_size.h = (unsigned)atoi(pj_optarg);
517 break;
518
519 case OPT_RECV_PT:
520 rx_pt = (pj_int8_t)atoi(pj_optarg);
521 break;
522
523 case OPT_SEND_PT:
524 tx_pt = (pj_int8_t)atoi(pj_optarg);
525 break;
526
527 #if defined(PJMEDIA_HAS_SRTP) && (PJMEDIA_HAS_SRTP != 0)
528 case OPT_USE_SRTP:
529 use_srtp = PJ_TRUE;
530 if (pj_optarg) {
531 pj_strset(&srtp_crypto_suite, pj_optarg, strlen(pj_optarg));
532 } else {
533 srtp_crypto_suite = pj_str("AES_CM_128_HMAC_SHA1_80");
534 }
535 break;
536
537 case OPT_SRTP_TX_KEY:
538 tmp_key_len = my_hex_string_to_octet_string(tmp_tx_key, pj_optarg,
539 (int)strlen(pj_optarg));
540 pj_strset(&srtp_tx_key, tmp_tx_key, tmp_key_len/2);
541 break;
542
543 case OPT_SRTP_RX_KEY:
544 tmp_key_len = my_hex_string_to_octet_string(tmp_rx_key, pj_optarg,
545 (int)strlen(pj_optarg));
546 pj_strset(&srtp_rx_key, tmp_rx_key, tmp_key_len/2);
547 break;
548 #endif
549
550 case OPT_HELP:
551 usage();
552 return 1;
553
554 default:
555 printf("Invalid options %s\n", argv[pj_optind]);
556 return 1;
557 }
558
559 }
560
561
562 /* Verify arguments. */
563 if (dir & PJMEDIA_DIR_ENCODING) {
564 if (remote_addr.sin_addr.s_addr == 0) {
565 printf("Error: remote address must be set\n");
566 return 1;
567 }
568 }
569
570 if (play_file.file_name != NULL && dir != PJMEDIA_DIR_ENCODING) {
571 printf("Direction is set to --send-only because of --play-file\n");
572 dir = PJMEDIA_DIR_ENCODING;
573 }
574
575 #if defined(PJMEDIA_HAS_SRTP) && (PJMEDIA_HAS_SRTP != 0)
576 /* SRTP validation */
577 if (use_srtp) {
578 if (!srtp_tx_key.slen || !srtp_rx_key.slen)
579 {
580 printf("Error: Key for each SRTP stream direction must be set\n");
581 return 1;
582 }
583 }
584 #endif
585
586 /* Must create a pool factory before we can allocate any memory. */
587 pj_caching_pool_init(&cp, &pj_pool_factory_default_policy, 0);
588
589 /*
590 * Initialize media endpoint.
591 * This will implicitly initialize PJMEDIA too.
592 */
593 status = pjmedia_endpt_create(&cp.factory, NULL, 1, &med_endpt);
594 PJ_ASSERT_RETURN(status == PJ_SUCCESS, 1);
595
596 /* Create memory pool for application purpose */
597 pool = pj_pool_create( &cp.factory, /* pool factory */
598 "app", /* pool name. */
599 4000, /* init size */
600 4000, /* increment size */
601 NULL /* callback on error */
602 );
603
604 /* Init video format manager */
605 pjmedia_video_format_mgr_create(pool, 64, 0, NULL);
606
607 /* Init video converter manager */
608 pjmedia_converter_mgr_create(pool, NULL);
609
610 /* Init event manager */
611 pjmedia_event_mgr_create(pool, 0, NULL);
612
613 /* Init video codec manager */
614 pjmedia_vid_codec_mgr_create(pool, NULL);
615
616 /* Init video subsystem */
617 pjmedia_vid_dev_subsys_init(&cp.factory);
618
619 /* Register all supported codecs */
620 status = init_codecs(&cp.factory);
621 PJ_ASSERT_RETURN(status == PJ_SUCCESS, 1);
622
623
624 /* Find which codec to use. */
625 if (codec_id) {
626 unsigned count = 1;
627 pj_str_t str_codec_id = pj_str(codec_id);
628
629 status = pjmedia_vid_codec_mgr_find_codecs_by_id(NULL,
630 &str_codec_id, &count,
631 &codec_info, NULL);
632 if (status != PJ_SUCCESS) {
633 printf("Error: unable to find codec %s\n", codec_id);
634 return 1;
635 }
636 } else {
637 static pjmedia_vid_codec_info info[1];
638 unsigned count = PJ_ARRAY_SIZE(info);
639
640 /* Default to first codec */
641 pjmedia_vid_codec_mgr_enum_codecs(NULL, &count, info, NULL);
642 codec_info = &info[0];
643 }
644
645 /* Get codec default param for info */
646 status = pjmedia_vid_codec_mgr_get_default_param(NULL, codec_info,
647 &codec_param);
648 pj_assert(status == PJ_SUCCESS);
649
650 /* Set outgoing video size */
651 if (tx_size.w && tx_size.h)
652 codec_param.enc_fmt.det.vid.size = tx_size;
653
654 #if DEF_RENDERER_WIDTH && DEF_RENDERER_HEIGHT
655 /* Set incoming video size */
656 if (DEF_RENDERER_WIDTH > codec_param.dec_fmt.det.vid.size.w)
657 codec_param.dec_fmt.det.vid.size.w = DEF_RENDERER_WIDTH;
658 if (DEF_RENDERER_HEIGHT > codec_param.dec_fmt.det.vid.size.h)
659 codec_param.dec_fmt.det.vid.size.h = DEF_RENDERER_HEIGHT;
660 #endif
661
662 if (play_file.file_name) {
663 pjmedia_video_format_detail *file_vfd;
664 pjmedia_clock_param clock_param;
665 char fmt_name[5];
666
667 /* Create file player */
668 status = create_file_player(pool, play_file.file_name, &play_port);
669 if (status != PJ_SUCCESS) {
670 app_perror(THIS_FILE, "Error opening file", status);
671 goto on_exit;
672 }
673
674 /* Collect format info */
675 file_vfd = pjmedia_format_get_video_format_detail(&play_port->info.fmt,
676 PJ_TRUE);
677 PJ_LOG(2, (THIS_FILE, "Reading video stream %dx%d %s @%.2ffps",
678 file_vfd->size.w, file_vfd->size.h,
679 pjmedia_fourcc_name(play_port->info.fmt.id, fmt_name),
680 (1.0*file_vfd->fps.num/file_vfd->fps.denum)));
681
682 /* Create decoder, if the file and the stream uses different codec */
683 if (codec_info->dec_fmt_id[0] !=
684 (pjmedia_format_id)play_port->info.fmt.id)
685 {
686 const pjmedia_video_format_info *dec_vfi;
687 pjmedia_video_apply_fmt_param dec_vafp = {0};
688 const pjmedia_vid_codec_info *codec_info2;
689 pjmedia_vid_codec_param codec_param2;
690
691 /* Find decoder */
692 status = pjmedia_vid_codec_mgr_get_codec_info2(NULL,
693 (pjmedia_format_id)
694 play_port->info.fmt.id,
695 &codec_info2);
696 if (status != PJ_SUCCESS)
697 goto on_exit;
698
699 /* Init decoder */
700 status = pjmedia_vid_codec_mgr_alloc_codec(NULL, codec_info2,
701 &play_decoder);
702 if (status != PJ_SUCCESS)
703 goto on_exit;
704
705 status = play_decoder->op->init(play_decoder, pool);
706 if (status != PJ_SUCCESS)
707 goto on_exit;
708
709 /* Open decoder */
710 status = pjmedia_vid_codec_mgr_get_default_param(NULL, codec_info2,
711 &codec_param2);
712 if (status != PJ_SUCCESS)
713 goto on_exit;
714
715 codec_param2.dir = PJMEDIA_DIR_DECODING;
716 codec_param2.packing = PJMEDIA_VID_PACKING_WHOLE;
717 status = play_decoder->op->open(play_decoder, &codec_param2);
718 if (status != PJ_SUCCESS)
719 goto on_exit;
720
721 /* Get decoder format info and apply param */
722 dec_vfi = pjmedia_get_video_format_info(NULL,
723 codec_info2->dec_fmt_id[0]);
724 if (!dec_vfi || !dec_vfi->apply_fmt) {
725 status = PJ_ENOTSUP;
726 goto on_exit;
727 }
728 dec_vafp.size = file_vfd->size;
729 (*dec_vfi->apply_fmt)(dec_vfi, &dec_vafp);
730
731 /* Allocate file read buffer */
732 play_file.read_buf_size = PJMEDIA_MAX_VIDEO_ENC_FRAME_SIZE;
733 play_file.read_buf = pj_pool_zalloc(pool, play_file.read_buf_size);
734
735 /* Allocate buffer to receive decoder output */
736 play_file.dec_buf_size = dec_vafp.framebytes;
737 play_file.dec_buf = pj_pool_zalloc(pool, play_file.dec_buf_size);
738 } else {
739 const pjmedia_video_format_info *dec_vfi;
740 pjmedia_video_apply_fmt_param dec_vafp = {0};
741
742 /* Get decoder format info and apply param */
743 dec_vfi = pjmedia_get_video_format_info(NULL,
744 play_port->info.fmt.id);
745 if (!dec_vfi || !dec_vfi->apply_fmt) {
746 status = PJ_ENOTSUP;
747 goto on_exit;
748 }
749 dec_vafp.size = file_vfd->size;
750 (*dec_vfi->apply_fmt)(dec_vfi, &dec_vafp);
751
752 /* Allocate file read buffer */
753 play_file.read_buf_size = dec_vafp.framebytes;
754 play_file.read_buf = pj_pool_zalloc(pool, play_file.read_buf_size);
755 }
756
757 /* Create player clock */
758 clock_param.usec_interval = PJMEDIA_PTIME(&file_vfd->fps);
759 clock_param.clock_rate = codec_info->clock_rate;
760 status = pjmedia_clock_create2(pool, &clock_param,
761 PJMEDIA_CLOCK_NO_HIGHEST_PRIO,
762 &clock_cb, &play_file, &play_clock);
763 if (status != PJ_SUCCESS)
764 goto on_exit;
765
766 /* Override stream codec param for encoding direction */
767 codec_param.enc_fmt.det.vid.size = file_vfd->size;
768 codec_param.enc_fmt.det.vid.fps = file_vfd->fps;
769
770 } else {
771 pjmedia_vid_port_param_default(&vpp);
772
773 /* Set as active for all video devices */
774 vpp.active = PJ_TRUE;
775
776 /* Create video device port. */
777 if (dir & PJMEDIA_DIR_ENCODING) {
778 /* Create capture */
779 status = pjmedia_vid_dev_default_param(
780 pool,
781 PJMEDIA_VID_DEFAULT_CAPTURE_DEV,
782 &vpp.vidparam);
783 if (status != PJ_SUCCESS)
784 goto on_exit;
785
786 pjmedia_format_copy(&vpp.vidparam.fmt, &codec_param.enc_fmt);
787 vpp.vidparam.fmt.id = codec_param.dec_fmt.id;
788 vpp.vidparam.dir = PJMEDIA_DIR_CAPTURE;
789
790 status = pjmedia_vid_port_create(pool, &vpp, &capture);
791 if (status != PJ_SUCCESS)
792 goto on_exit;
793 }
794
795 if (dir & PJMEDIA_DIR_DECODING) {
796 /* Create renderer */
797 status = pjmedia_vid_dev_default_param(
798 pool,
799 PJMEDIA_VID_DEFAULT_RENDER_DEV,
800 &vpp.vidparam);
801 if (status != PJ_SUCCESS)
802 goto on_exit;
803
804 pjmedia_format_copy(&vpp.vidparam.fmt, &codec_param.dec_fmt);
805 vpp.vidparam.dir = PJMEDIA_DIR_RENDER;
806 vpp.vidparam.disp_size = vpp.vidparam.fmt.det.vid.size;
807 vpp.vidparam.flags |= PJMEDIA_VID_DEV_CAP_OUTPUT_WINDOW_FLAGS;
808 vpp.vidparam.window_flags = PJMEDIA_VID_DEV_WND_BORDER |
809 PJMEDIA_VID_DEV_WND_RESIZABLE;
810
811 status = pjmedia_vid_port_create(pool, &vpp, &renderer);
812 if (status != PJ_SUCCESS)
813 goto on_exit;
814 }
815 }
816
817 /* Set to ignore fmtp */
818 codec_param.ignore_fmtp = PJ_TRUE;
819
820 /* Create stream based on program arguments */
821 status = create_stream(pool, med_endpt, codec_info, &codec_param,
822 dir, rx_pt, tx_pt, local_port, &remote_addr,
823 #if defined(PJMEDIA_HAS_SRTP) && (PJMEDIA_HAS_SRTP != 0)
824 use_srtp, &srtp_crypto_suite,
825 &srtp_tx_key, &srtp_rx_key,
826 #endif
827 &stream);
828 if (status != PJ_SUCCESS)
829 goto on_exit;
830
831 /* Get the port interface of the stream */
832 status = pjmedia_vid_stream_get_port(stream, PJMEDIA_DIR_ENCODING,
833 &enc_port);
834 PJ_ASSERT_RETURN(status == PJ_SUCCESS, 1);
835
836 status = pjmedia_vid_stream_get_port(stream, PJMEDIA_DIR_DECODING,
837 &dec_port);
838 PJ_ASSERT_RETURN(status == PJ_SUCCESS, 1);
839
840 /* Start streaming */
841 status = pjmedia_vid_stream_start(stream);
842 if (status != PJ_SUCCESS)
843 goto on_exit;
844
845 /* Start renderer */
846 if (renderer) {
847 status = pjmedia_vid_port_connect(renderer, dec_port, PJ_FALSE);
848 if (status != PJ_SUCCESS)
849 goto on_exit;
850 status = pjmedia_vid_port_start(renderer);
851 if (status != PJ_SUCCESS)
852 goto on_exit;
853 }
854
855 /* Start capture */
856 if (capture) {
857 status = pjmedia_vid_port_connect(capture, enc_port, PJ_FALSE);
858 if (status != PJ_SUCCESS)
859 goto on_exit;
860 status = pjmedia_vid_port_start(capture);
861 if (status != PJ_SUCCESS)
862 goto on_exit;
863 }
864
865 /* Start playing file */
866 if (play_file.file_name) {
867
868 #if HAS_LOCAL_RENDERER_FOR_PLAY_FILE
869 /* Create local renderer */
870 pjmedia_vid_port_param_default(&vpp);
871 vpp.active = PJ_FALSE;
872 status = pjmedia_vid_dev_default_param(
873 pool,
874 PJMEDIA_VID_DEFAULT_RENDER_DEV,
875 &vpp.vidparam);
876 if (status != PJ_SUCCESS)
877 goto on_exit;
878
879 vpp.vidparam.dir = PJMEDIA_DIR_RENDER;
880 pjmedia_format_copy(&vpp.vidparam.fmt, &codec_param.dec_fmt);
881 vpp.vidparam.fmt.det.vid.size = play_port->info.fmt.det.vid.size;
882 vpp.vidparam.fmt.det.vid.fps = play_port->info.fmt.det.vid.fps;
883 vpp.vidparam.disp_size = vpp.vidparam.fmt.det.vid.size;
884 vpp.vidparam.flags |= PJMEDIA_VID_DEV_CAP_OUTPUT_WINDOW_FLAGS;
885 vpp.vidparam.window_flags = PJMEDIA_VID_DEV_WND_BORDER |
886 PJMEDIA_VID_DEV_WND_RESIZABLE;
887
888 status = pjmedia_vid_port_create(pool, &vpp, &renderer);
889 if (status != PJ_SUCCESS)
890 goto on_exit;
891 status = pjmedia_vid_port_start(renderer);
892 if (status != PJ_SUCCESS)
893 goto on_exit;
894 #endif
895
896 /* Init play file data */
897 play_file.play_port = play_port;
898 play_file.stream_port = enc_port;
899 play_file.decoder = play_decoder;
900 if (renderer) {
901 play_file.renderer = pjmedia_vid_port_get_passive_port(renderer);
902 }
903
904 status = pjmedia_clock_start(play_clock);
905 if (status != PJ_SUCCESS)
906 goto on_exit;
907 }
908
909 /* Done */
910
911 if (dir == PJMEDIA_DIR_DECODING)
912 printf("Stream is active, dir is recv-only, local port is %d\n",
913 local_port);
914 else if (dir == PJMEDIA_DIR_ENCODING)
915 printf("Stream is active, dir is send-only, sending to %s:%d\n",
916 pj_inet_ntop2(pj_AF_INET(), &remote_addr.sin_addr, addr,
917 sizeof(addr)),
918 pj_ntohs(remote_addr.sin_port));
919 else
920 printf("Stream is active, send/recv, local port is %d, "
921 "sending to %s:%d\n",
922 local_port,
923 pj_inet_ntop2(pj_AF_INET(), &remote_addr.sin_addr, addr,
924 sizeof(addr)),
925 pj_ntohs(remote_addr.sin_port));
926
927 if (dir & PJMEDIA_DIR_ENCODING)
928 PJ_LOG(2, (THIS_FILE, "Sending %dx%d %.*s @%.2ffps",
929 codec_param.enc_fmt.det.vid.size.w,
930 codec_param.enc_fmt.det.vid.size.h,
931 (int)codec_info->encoding_name.slen,
932 codec_info->encoding_name.ptr,
933 (1.0*codec_param.enc_fmt.det.vid.fps.num/
934 codec_param.enc_fmt.det.vid.fps.denum)));
935
936 for (;;) {
937 char tmp[10];
938
939 puts("");
940 puts("Commands:");
941 puts(" q Quit");
942 puts("");
943
944 printf("Command: "); fflush(stdout);
945
946 if (fgets(tmp, sizeof(tmp), stdin) == NULL) {
947 puts("EOF while reading stdin, will quit now..");
948 break;
949 }
950
951 if (tmp[0] == 'q')
952 break;
953
954 }
955
956
957
958 /* Start deinitialization: */
959 on_exit:
960
961 if (status != PJ_SUCCESS) {
962 app_perror(THIS_FILE, "Something wrong", status);
963 }
964
965 /* Stop video devices */
966 if (capture)
967 pjmedia_vid_port_stop(capture);
968 if (renderer)
969 pjmedia_vid_port_stop(renderer);
970
971 /* Stop and destroy file clock */
972 if (play_clock) {
973 pjmedia_clock_stop(play_clock);
974 pjmedia_clock_destroy(play_clock);
975 }
976
977 /* Destroy file reader/player */
978 if (play_port)
979 pjmedia_port_destroy(play_port);
980
981 /* Destroy file decoder */
982 if (play_decoder) {
983 play_decoder->op->close(play_decoder);
984 pjmedia_vid_codec_mgr_dealloc_codec(NULL, play_decoder);
985 }
986
987 /* Destroy video devices */
988 if (capture)
989 pjmedia_vid_port_destroy(capture);
990 if (renderer)
991 pjmedia_vid_port_destroy(renderer);
992
993 /* Destroy stream */
994 if (stream) {
995 pjmedia_transport *tp;
996
997 tp = pjmedia_vid_stream_get_transport(stream);
998 pjmedia_vid_stream_destroy(stream);
999
1000 pjmedia_transport_media_stop(tp);
1001 pjmedia_transport_close(tp);
1002 }
1003
1004 /* Deinit codecs */
1005 deinit_codecs();
1006
1007 /* Shutdown video subsystem */
1008 pjmedia_vid_dev_subsys_shutdown();
1009
1010 /* Destroy event manager */
1011 pjmedia_event_mgr_destroy(NULL);
1012
1013 /* Release application pool */
1014 pj_pool_release( pool );
1015
1016 /* Destroy media endpoint. */
1017 pjmedia_endpt_destroy( med_endpt );
1018
1019 /* Destroy pool factory */
1020 pj_caching_pool_destroy( &cp );
1021
1022 /* Shutdown PJLIB */
1023 pj_shutdown();
1024
1025 return (status == PJ_SUCCESS) ? 0 : 1;
1026 }
1027
1028
1029 int main(int argc, char *argv[])
1030 {
1031 return pj_run_app(&main_func, argc, argv, 0);
1032 }
1033
1034 #else
1035
1036 int main(int argc, char *argv[])
1037 {
1038 PJ_UNUSED_ARG(argc);
1039 PJ_UNUSED_ARG(argv);
1040 puts("Error: this sample requires video capability (PJMEDIA_HAS_VIDEO == 1)");
1041 return -1;
1042 }
1043
1044 #endif /* PJMEDIA_HAS_VIDEO */