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