Debian Codegraph @

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

raw symbols SCIP
1 /*
2 * Copyright (C) 2010-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 #include <pjmedia.h>
20 #include <pjmedia/converter.h>
21 #include <pjmedia-codec.h>
22 #include <pjlib-util.h>
23 #include <pjlib.h>
24 #include <stdio.h>
25 #include <stdlib.h>
26
27 #ifndef _MSC_VER
28 #include <signal.h>
29 #endif
30
31 /**
32 * \page page_pjmedia_samples_aviplay_c Samples: Playing AVI File to
33 * Video and Sound Devices
34 *
35 * This is a very simple example to use the @ref PJMEDIA_FILE_PLAY,
36 * @ref PJMED_SND_PORT, and @ref PJMEDIA_VIDEO_PORT. In this example, we
37 * open the file, video, and sound devices, then connect the file to both
38 * video and sound devices to play the contents of the file.
39 *
40 *
41 * This file is pjsip-apps/src/samples/aviplay.c
42 *
43 * \includelineno aviplay.c
44 */
45
46
47 /*
48 * aviplay.c
49 *
50 * PURPOSE:
51 * Play a AVI file to video and sound devices.
52 *
53 * USAGE:
54 * aviplay FILE.AVI
55 */
56
57
58 #if defined(PJMEDIA_HAS_VIDEO) && (PJMEDIA_HAS_VIDEO != 0)
59
60 #include "util.h"
61
62 /* For logging purpose. */
63 #define THIS_FILE "aviplay.c"
64
65 static const char *desc =
66 " FILE \n"
67 " \n"
68 " aviplay.c \n"
69 " \n"
70 " PURPOSE \n"
71 " \n"
72 " Demonstrate how to play a AVI file. \n"
73 " \n"
74 " USAGE \n"
75 " \n"
76 " aviplay FILE.AVI \n";
77
78 struct codec_fmt {
79 pj_uint32_t pjmedia_id;
80 const char *codec_id;
81 /* Do we need to convert the decoded frame? */
82 pj_bool_t need_conversion;
83 /* If conversion is needed, dst_fmt indicates the destination format */
84 pjmedia_format_id dst_fmt;
85 } codec_fmts[] = {{PJMEDIA_FORMAT_MJPEG, "mjpeg",
86 PJ_TRUE , PJMEDIA_FORMAT_I420},
87 {PJMEDIA_FORMAT_H263 , "h263" ,
88 PJ_FALSE, 0},
89 {PJMEDIA_FORMAT_MPEG4, "mp4v"},
90 {PJMEDIA_FORMAT_H264 , "h264"}
91 };
92
93 typedef struct avi_port_t
94 {
95 pjmedia_vid_port *vid_port;
96 pjmedia_snd_port *snd_port;
97 pj_bool_t is_running;
98 pj_bool_t is_quitting;
99 } avi_port_t;
100
101 typedef struct codec_port_data_t
102 {
103 pjmedia_vid_codec *codec;
104 pjmedia_port *src_port;
105 pj_uint8_t *enc_buf;
106 pj_size_t enc_buf_size;
107
108 pjmedia_converter *conv;
109 } codec_port_data_t;
110
111 static pj_status_t avi_event_cb(pjmedia_event *event,
112 void *user_data)
113 {
114 avi_port_t *ap = (avi_port_t *)user_data;
115
116 switch (event->type) {
117 case PJMEDIA_EVENT_WND_CLOSED:
118 ap->is_quitting = PJ_TRUE;
119 break;
120 case PJMEDIA_EVENT_MOUSE_BTN_DOWN:
121 if (ap->is_running) {
122 pjmedia_vid_port_stop(ap->vid_port);
123 if (ap->snd_port)
124 pjmedia_aud_stream_stop(
125 pjmedia_snd_port_get_snd_stream(ap->snd_port));
126 } else {
127 pjmedia_vid_port_start(ap->vid_port);
128 if (ap->snd_port)
129 pjmedia_aud_stream_start(
130 pjmedia_snd_port_get_snd_stream(ap->snd_port));
131 }
132 ap->is_running = !ap->is_running;
133 break;
134 default:
135 return PJ_SUCCESS;
136 }
137
138 /* We handled the event on our own, so return non-PJ_SUCCESS here */
139 return -1;
140 }
141
142 static pj_status_t codec_get_frame(pjmedia_port *port,
143 pjmedia_frame *frame)
144 {
145 codec_port_data_t *port_data = (codec_port_data_t*)port->port_data.pdata;
146 pjmedia_vid_codec *codec = port_data->codec;
147 pjmedia_frame enc_frame;
148 pj_status_t status;
149
150 enc_frame.buf = port_data->enc_buf;
151 enc_frame.size = port_data->enc_buf_size;
152
153 if (port_data->conv) {
154 pj_size_t frame_size = frame->size;
155
156 status = pjmedia_port_get_frame(port_data->src_port, frame);
157 if (status != PJ_SUCCESS) goto on_error;
158
159 status = pjmedia_vid_codec_decode(codec, 1, frame,
160 (unsigned)frame->size, &enc_frame);
161 if (status != PJ_SUCCESS) goto on_error;
162
163 frame->size = frame_size;
164 status = pjmedia_converter_convert(port_data->conv, &enc_frame, frame);
165 if (status != PJ_SUCCESS) goto on_error;
166
167 return PJ_SUCCESS;
168 }
169
170 status = pjmedia_port_get_frame(port_data->src_port, &enc_frame);
171 if (status != PJ_SUCCESS) goto on_error;
172
173 status = pjmedia_vid_codec_decode(codec, 1, &enc_frame,
174 (unsigned)frame->size, frame);
175 if (status != PJ_SUCCESS) goto on_error;
176
177 return PJ_SUCCESS;
178
179 on_error:
180 pj_perror(3, THIS_FILE, status, "codec_get_frame() error");
181 return status;
182 }
183
184 static int aviplay(pj_pool_t *pool, const char *fname)
185 {
186 pjmedia_vid_port *renderer=NULL;
187 pjmedia_vid_port_param param;
188 const pjmedia_video_format_info *vfi;
189 pjmedia_video_format_detail *vfd;
190 pjmedia_snd_port *snd_port = NULL;
191 pj_status_t status;
192 int rc = 0;
193 pjmedia_avi_streams *avi_streams;
194 pjmedia_avi_stream *vid_stream, *aud_stream;
195 pjmedia_port *vid_port = NULL, *aud_port = NULL;
196 pjmedia_vid_codec *codec=NULL;
197 codec_port_data_t codec_port_data;
198 avi_port_t avi_port;
199
200 pj_bzero(&avi_port, sizeof(avi_port));
201
202 CHECK( pjmedia_avi_player_create_streams(pool, fname, 0, &avi_streams) );
203
204 vid_stream = pjmedia_avi_streams_get_stream_by_media(avi_streams,
205 0,
206 PJMEDIA_TYPE_VIDEO);
207 vid_port = pjmedia_avi_stream_get_port(vid_stream);
208
209 if (vid_port) {
210 pjmedia_vid_port_param_default(&param);
211
212 CHECK( pjmedia_vid_dev_default_param(pool,
213 PJMEDIA_VID_DEFAULT_RENDER_DEV,
214 &param.vidparam) );
215
216 /* Create renderer, set it to active */
217 param.active = PJ_TRUE;
218 param.vidparam.dir = PJMEDIA_DIR_RENDER;
219 param.vidparam.flags |= PJMEDIA_VID_DEV_CAP_OUTPUT_WINDOW_FLAGS;
220 param.vidparam.window_flags = PJMEDIA_VID_DEV_WND_BORDER | PJMEDIA_VID_DEV_WND_RESIZABLE;
221 vfd = pjmedia_format_get_video_format_detail(&vid_port->info.fmt,
222 PJ_TRUE);
223 pjmedia_format_init_video(&param.vidparam.fmt,
224 vid_port->info.fmt.id,
225 vfd->size.w, vfd->size.h,
226 vfd->fps.num, vfd->fps.denum);
227
228 vfi = pjmedia_get_video_format_info(
229 pjmedia_video_format_mgr_instance(),
230 vid_port->info.fmt.id);
231 /* Check whether the frame is encoded */
232 if (!vfi || vfi->bpp == 0) {
233 /* Yes, prepare codec */
234 pj_str_t codec_id_st;
235 unsigned info_cnt = 1, i, k;
236 const pjmedia_vid_codec_info *codec_info;
237 pj_str_t port_name = {"codec", 5};
238 pj_uint8_t *enc_buf = NULL;
239 pj_size_t enc_buf_size = 0;
240 pjmedia_vid_dev_info rdr_info;
241 pjmedia_port codec_port;
242 pjmedia_vid_codec_param codec_param;
243 struct codec_fmt *codecp = NULL;
244
245 /* Lookup codec */
246 for (i = 0; i < PJ_ARRAY_SIZE(codec_fmts); i++) {
247 if (vid_port->info.fmt.id == codec_fmts[i].pjmedia_id) {
248 codecp = &codec_fmts[i];
249 break;
250 }
251 }
252 if (!codecp) {
253 rc = 242; goto on_return;
254 }
255 pj_cstr(&codec_id_st, codecp->codec_id);
256 CHECK( pjmedia_vid_codec_mgr_find_codecs_by_id(NULL,
257 &codec_id_st,
258 &info_cnt,
259 &codec_info,
260 NULL) );
261
262 CHECK( pjmedia_vid_codec_mgr_get_default_param(NULL, codec_info,
263 &codec_param) );
264
265 pjmedia_format_copy(&codec_param.enc_fmt, &param.vidparam.fmt);
266
267 pjmedia_vid_dev_get_info(param.vidparam.rend_id, &rdr_info);
268 for (i=0; i<codec_info->dec_fmt_id_cnt; ++i) {
269 for (k=0; k<rdr_info.fmt_cnt; ++k) {
270 if ((int)codec_info->dec_fmt_id[i]==(int)rdr_info.fmt[k].id)
271 {
272 param.vidparam.fmt.id = codec_info->dec_fmt_id[i];
273 i = codec_info->dec_fmt_id_cnt;
274 break;
275 }
276 }
277 }
278
279 /* Open codec */
280 CHECK( pjmedia_vid_codec_mgr_alloc_codec(NULL, codec_info,
281 &codec));
282
283 CHECK( pjmedia_vid_codec_init(codec, pool) );
284
285 pjmedia_format_copy(&codec_param.dec_fmt, &param.vidparam.fmt);
286 codec_param.dir = PJMEDIA_DIR_DECODING;
287 codec_param.packing = PJMEDIA_VID_PACKING_WHOLE;
288 CHECK( pjmedia_vid_codec_open(codec, &codec_param) );
289
290 /* Alloc encoding buffer */
291 enc_buf_size = codec_param.dec_fmt.det.vid.size.w *
292 codec_param.dec_fmt.det.vid.size.h * 4;
293 enc_buf = pj_pool_alloc(pool, enc_buf_size +
294 128 /*< padding, required for vid codecs
295 such as ffmpeg. Must be >=
296 AV_INPUT_BUFFER_PADDING_SIZE.
297 And must not be included in
298 the enc_buf_size calculation
299 above. */);
300
301 /* Init codec port */
302 pj_bzero(&codec_port, sizeof(codec_port));
303 CHECK( pjmedia_port_info_init2(&codec_port.info, &port_name,
304 0x1234,
305 PJMEDIA_DIR_ENCODING,
306 &codec_param.dec_fmt) );
307
308 pj_bzero(&codec_port_data, sizeof(codec_port_data));
309 codec_port_data.codec = codec;
310 codec_port_data.src_port = vid_port;
311 codec_port_data.enc_buf = enc_buf;
312 codec_port_data.enc_buf_size = enc_buf_size;
313
314 codec_port.get_frame = &codec_get_frame;
315 codec_port.port_data.pdata = &codec_port_data;
316
317 /* Check whether we need to convert the decoded frame */
318 if (codecp->need_conversion) {
319 pjmedia_conversion_param conv_param;
320
321 pjmedia_format_copy(&conv_param.src, &param.vidparam.fmt);
322 pjmedia_format_copy(&conv_param.dst, &param.vidparam.fmt);
323 conv_param.dst.id = codecp->dst_fmt;
324 param.vidparam.fmt.id = conv_param.dst.id;
325
326 CHECK( pjmedia_converter_create(NULL, pool, &conv_param,
327 &codec_port_data.conv));
328 }
329
330 CHECK( pjmedia_vid_port_create(pool, &param, &renderer) );
331
332 CHECK( pjmedia_vid_port_connect(renderer, &codec_port,
333 PJ_FALSE) );
334 } else {
335 CHECK( pjmedia_vid_port_create(pool, &param, &renderer) );
336
337 /* Connect avi port to renderer */
338 CHECK( pjmedia_vid_port_connect(renderer, vid_port,
339 PJ_FALSE) );
340 }
341
342 }
343
344 aud_stream = pjmedia_avi_streams_get_stream_by_media(avi_streams,
345 0,
346 PJMEDIA_TYPE_AUDIO);
347 aud_port = pjmedia_avi_stream_get_port(aud_stream);
348
349 if (aud_port) {
350 /* Create sound player port. */
351 CHECK( pjmedia_snd_port_create_player(
352 pool, /* pool */
353 -1, /* use default dev. */
354 PJMEDIA_PIA_SRATE(&aud_port->info),/* clock rate. */
355 PJMEDIA_PIA_CCNT(&aud_port->info), /* # of channels. */
356 PJMEDIA_PIA_SPF(&aud_port->info), /* samples per frame. */
357 PJMEDIA_PIA_BITS(&aud_port->info), /* bits per sample. */
358 0, /* options */
359 &snd_port /* returned port */
360 ));
361
362 /* Connect file port to the sound player.
363 * Stream playing will commence immediately.
364 */
365 CHECK( pjmedia_snd_port_connect(snd_port, aud_port) );
366 }
367
368 if (vid_port) {
369 pjmedia_vid_dev_cb cb;
370
371 pj_bzero(&cb, sizeof(cb));
372 avi_port.snd_port = snd_port;
373 avi_port.vid_port = renderer;
374 avi_port.is_running = PJ_TRUE;
375 pjmedia_vid_port_set_cb(renderer, &cb, &avi_port);
376
377 /* subscribe events */
378 pjmedia_event_subscribe(NULL, &avi_event_cb, &avi_port,
379 renderer);
380
381 if (snd_port) {
382 /* Synchronize video rendering and audio playback */
383 pjmedia_vid_port_set_clock_src(
384 renderer,
385 pjmedia_snd_port_get_clock_src(
386 snd_port, PJMEDIA_DIR_PLAYBACK));
387 }
388
389
390 /* Start video streaming.. */
391 CHECK( pjmedia_vid_port_start(renderer) );
392 }
393
394 while (!avi_port.is_quitting) {
395 pj_thread_sleep(100);
396 }
397
398 on_error:
399 on_return:
400 if (snd_port) {
401 pjmedia_snd_port_disconnect(snd_port);
402 /* Without this sleep, Windows/DirectSound will repeteadly
403 * play the last frame during destroy.
404 */
405 pj_thread_sleep(100);
406 pjmedia_snd_port_destroy(snd_port);
407 }
408 if (renderer) {
409 pjmedia_event_unsubscribe(NULL, &avi_event_cb, &avi_port,
410 renderer);
411 pjmedia_vid_port_destroy(renderer);
412 }
413 if (aud_port)
414 pjmedia_port_destroy(aud_port);
415 if (vid_port)
416 pjmedia_port_destroy(vid_port);
417 if (codec) {
418 pjmedia_vid_codec_close(codec);
419 pjmedia_vid_codec_mgr_dealloc_codec(NULL, codec);
420 }
421
422 return rc;
423 }
424
425 #ifndef _MSC_VER
426 static void sig_handler(int sig)
427 {
428 switch (sig)
429 {
430 case SIGINT:
431 case SIGTERM:
432 break;
433 }
434 puts("exit..");
435 exit(1);
436 }
437 #endif
438
439 static int main_func(int argc, char *argv[])
440 {
441 pj_caching_pool cp;
442 pj_pool_t *pool;
443 int rc = 0;
444 pj_status_t status = PJ_SUCCESS;
445
446 if (argc != 2) {
447 puts("Error: filename required");
448 puts(desc);
449 return 110;
450 }
451
452 #ifndef _MSC_VER
453 signal(SIGINT, sig_handler);
454 signal(SIGTERM, sig_handler);
455 #endif
456
457 /* Must init PJLIB first: */
458 status = pj_init();
459 PJ_ASSERT_RETURN(status == PJ_SUCCESS, 120);
460
461 /* Must create a pool factory before we can allocate any memory. */
462 pj_caching_pool_init(&cp, &pj_pool_factory_default_policy, 0);
463
464 /* Create memory pool for our file player */
465 pool = pj_pool_create( &cp.factory, /* pool factory */
466 "AVI", /* pool name. */
467 4000, /* init size */
468 4000, /* increment size */
469 NULL /* callback on error */
470 );
471
472 pjmedia_video_format_mgr_create(pool, 64, 0, NULL);
473 pjmedia_converter_mgr_create(pool, NULL);
474 pjmedia_event_mgr_create(pool, 0, NULL);
475 pjmedia_vid_codec_mgr_create(pool, NULL);
476
477 status = pjmedia_vid_dev_subsys_init(&cp.factory);
478 if (status != PJ_SUCCESS) {
479 app_perror(THIS_FILE, "Video init failed", status);
480 rc = 130;
481 goto on_return;
482 }
483
484 status = pjmedia_aud_subsys_init(&cp.factory);
485 if (status != PJ_SUCCESS) {
486 rc = 140;
487 goto on_return;
488 }
489
490 #if defined(PJMEDIA_HAS_OPENH264_CODEC) && PJMEDIA_HAS_OPENH264_CODEC != 0
491 status = pjmedia_codec_openh264_vid_init(NULL, &cp.factory);
492 if (status != PJ_SUCCESS) {
493 rc = 150;
494 goto on_return;
495 }
496 #endif
497
498 #if PJMEDIA_HAS_FFMPEG_VID_CODEC
499 status = pjmedia_codec_ffmpeg_vid_init(NULL, &cp.factory);
500 if (status != PJ_SUCCESS) {
501 rc = 160;
502 goto on_return;
503 }
504 #endif
505
506 rc = aviplay(pool, argv[1]);
507
508 /*
509 * File should be playing and looping now
510 */
511
512 /* Without this sleep, Windows/DirectSound will repeteadly
513 * play the last frame during destroy.
514 */
515 pj_thread_sleep(100);
516
517 on_return:
518 #if PJMEDIA_HAS_FFMPEG_VID_CODEC
519 pjmedia_codec_ffmpeg_vid_deinit();
520 #endif
521 #if defined(PJMEDIA_HAS_OPENH264_CODEC) && PJMEDIA_HAS_OPENH264_CODEC != 0
522 pjmedia_codec_openh264_vid_deinit();
523 #endif
524 pjmedia_aud_subsys_shutdown();
525 pjmedia_vid_dev_subsys_shutdown();
526
527 pjmedia_video_format_mgr_destroy(pjmedia_video_format_mgr_instance());
528 pjmedia_converter_mgr_destroy(pjmedia_converter_mgr_instance());
529 pjmedia_event_mgr_destroy(pjmedia_event_mgr_instance());
530 pjmedia_vid_codec_mgr_destroy(pjmedia_vid_codec_mgr_instance());
531
532 /* Release application pool */
533 pj_pool_release( pool );
534
535 /* Destroy pool factory */
536 pj_caching_pool_destroy( &cp );
537
538 /* Shutdown PJLIB */
539 pj_shutdown();
540
541 /* Done. */
542 return rc;
543 }
544
545 int main(int argc, char *argv[])
546 {
547 return pj_run_app(&main_func, argc, argv, 0);
548 }
549
550 #else
551
552 int main(int argc, char *argv[])
553 {
554 PJ_UNUSED_ARG(argc);
555 PJ_UNUSED_ARG(argv);
556 puts("Error: this sample requires video capability (PJMEDIA_HAS_VIDEO == 1)");
557 return -1;
558 }
559
560 #endif /* PJMEDIA_HAS_VIDEO */