1
/* $Id: alt_pjsua_vid.c 4174 2012-06-21 08:09:53Z bennylp $ */
3
* Copyright (C) 2011-2011 Teluu Inc. (http://www.teluu.com)
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.
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.
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
19
#include <pjsua-lib/pjsua.h>
20
#include <pjsua-lib/pjsua_internal.h>
22
#if defined(PJSUA_MEDIA_HAS_PJMEDIA) && PJSUA_MEDIA_HAS_PJMEDIA != 0
23
# error The PJSUA_MEDIA_HAS_PJMEDIA should be declared as zero
28
#define THIS_FILE "alt_pjsua_vid.c"
29
#define UNIMPLEMENTED(func) PJ_LOG(2,(THIS_FILE, "*** Call to unimplemented function %s ***", #func));
31
/*****************************************************************************
32
* Our video codec descriptors
37
pjmedia_vid_codec_info info;
38
pjmedia_format_id base_fmt_id;
41
pjmedia_codec_fmtp dec_fmtp;
46
{PJMEDIA_FORMAT_H263P, PJMEDIA_RTP_PT_H263P, {"H263-1998",9},
47
{"H.263 codec", 11}, 90000, PJMEDIA_DIR_ENCODING_DECODING,
48
0, {PJMEDIA_FORMAT_RGB24}, PJMEDIA_VID_PACKING_PACKETS
50
PJMEDIA_FORMAT_H263, 256000, 512000,
51
{2, { {{"CIF",3}, {"1",1}},
52
{{"QCIF",4}, {"1",1}}, } },
56
static const struct alt_codec_desc* find_codec_desc_by_info(const pjmedia_vid_codec_info *info)
59
for (i=0; i<PJ_ARRAY_SIZE(alt_vid_codecs); ++i) {
60
struct alt_codec_desc *desc = &alt_vid_codecs[i];
61
if ((desc->info.fmt_id == info->fmt_id) &&
62
((desc->info.dir & info->dir) == info->dir) &&
63
(desc->info.pt == info->pt) &&
64
(desc->info.packings & info->packings))
73
static pj_status_t alt_vid_codec_test_alloc( pjmedia_vid_codec_factory *factory,
74
const pjmedia_vid_codec_info *id )
76
const struct alt_codec_desc *desc = find_codec_desc_by_info(id);
77
return desc? PJ_SUCCESS : PJMEDIA_CODEC_EUNSUP;
80
static pj_status_t alt_vid_codec_default_attr( pjmedia_vid_codec_factory *factory,
81
const pjmedia_vid_codec_info *info,
82
pjmedia_vid_codec_param *attr )
84
const struct alt_codec_desc *desc = find_codec_desc_by_info(info);
88
return PJMEDIA_CODEC_EUNSUP;
90
pj_bzero(attr, sizeof(pjmedia_vid_codec_param));
92
/* Scan the requested packings and use the lowest number */
94
for (i=0; i<15; ++i) {
95
unsigned packing = (1 << i);
96
if ((desc->info.packings & info->packings) & packing) {
97
attr->packing = (pjmedia_vid_packing)packing;
101
if (attr->packing == 0) {
102
/* No supported packing in info */
103
return PJMEDIA_CODEC_EUNSUP;
107
attr->dir = desc->info.dir;
110
pjmedia_format_init_video(&attr->enc_fmt, desc->info.fmt_id,
111
720, 480, 30000, 1001);
114
pjmedia_format_init_video(&attr->dec_fmt, desc->info.dec_fmt_id[0],
115
//352, 288, 30000, 1001);
116
720, 576, 30000, 1001);
119
attr->dec_fmtp = desc->dec_fmtp;
122
attr->enc_fmt.det.vid.avg_bps = desc->avg_bps;
123
attr->enc_fmt.det.vid.max_bps = desc->max_bps;
126
attr->enc_mtu = PJMEDIA_MAX_MTU;
132
static pj_status_t alt_vid_codec_enum_codecs( pjmedia_vid_codec_factory *factory,
134
pjmedia_vid_codec_info codecs[])
138
PJ_ASSERT_RETURN(codecs && *count > 0, PJ_EINVAL);
140
max_cnt = PJ_MIN(*count, PJ_ARRAY_SIZE(alt_vid_codecs));
143
for (i=0; i<max_cnt; ++i) {
144
pj_memcpy(&codecs[*count], &alt_vid_codecs[i].info,
145
sizeof(pjmedia_vid_codec_info));
153
static pj_status_t alt_vid_codec_alloc_codec( pjmedia_vid_codec_factory *factory,
154
const pjmedia_vid_codec_info *info,
155
pjmedia_vid_codec **p_codec)
157
/* This will never get called since we won't be using this codec */
158
UNIMPLEMENTED(alt_vid_codec_alloc_codec)
163
static pj_status_t alt_vid_codec_dealloc_codec( pjmedia_vid_codec_factory *factory,
164
pjmedia_vid_codec *codec )
166
/* This will never get called since we won't be using this codec */
167
UNIMPLEMENTED(alt_vid_codec_dealloc_codec)
171
static pjmedia_vid_codec_factory_op alt_vid_codec_factory_op =
173
&alt_vid_codec_test_alloc,
174
&alt_vid_codec_default_attr,
175
&alt_vid_codec_enum_codecs,
176
&alt_vid_codec_alloc_codec,
177
&alt_vid_codec_dealloc_codec
180
static struct alt_vid_codec_factory {
181
pjmedia_vid_codec_factory base;
182
} alt_vid_codec_factory;
184
/*****************************************************************************
185
* Video API implementation
188
/* Initialize the video library */
189
pj_status_t pjsua_vid_subsys_init(void)
191
pjmedia_vid_codec_mgr *mgr;
194
/* Format manager singleton is needed */
195
status = pjmedia_video_format_mgr_create(pjsua_var.pool, 64, 0, NULL);
196
if (status != PJ_SUCCESS) {
197
PJ_PERROR(1,(THIS_FILE, status,
198
"Error creating PJMEDIA video format manager"));
202
/* Create video codec manager singleton */
203
status = pjmedia_vid_codec_mgr_create(pjsua_var.pool, &mgr);
204
if (status != PJ_SUCCESS) {
205
PJ_PERROR(1,(THIS_FILE, status,
206
"Error creating PJMEDIA video codec manager"));
210
/* Register our codecs */
211
alt_vid_codec_factory.base.op = &alt_vid_codec_factory_op;
212
alt_vid_codec_factory.base.factory_data = NULL;
214
status = pjmedia_vid_codec_mgr_register_factory(mgr, &alt_vid_codec_factory.base);
215
if (status != PJ_SUCCESS)
219
* TODO: put your 3rd party library initialization routine here
225
/* Start the video library */
226
pj_status_t pjsua_vid_subsys_start(void)
229
* TODO: put your 3rd party library startup routine here
234
/* Cleanup and deinitialize the video library */
235
pj_status_t pjsua_vid_subsys_destroy(void)
237
if (pjmedia_vid_codec_mgr_instance())
238
pjmedia_vid_codec_mgr_destroy(NULL);
240
if (pjmedia_video_format_mgr_instance())
241
pjmedia_video_format_mgr_destroy(NULL);
244
* TODO: put your 3rd party library cleanup routine here
249
/* Initialize video call media */
250
pj_status_t pjsua_vid_channel_init(pjsua_call_media *call_med)
253
* TODO: put call media initialization
258
/* Internal function to stop video stream */
259
void pjsua_vid_stop_stream(pjsua_call_media *call_med)
261
PJ_LOG(4,(THIS_FILE, "Stopping video stream.."));
264
pjmedia_transport_detach(call_med->tp, call_med);
269
* - stop your video stream here
274
/* Our callback to receive incoming RTP packets */
275
static void vid_rtp_cb(void *user_data, void *pkt, pj_ssize_t size)
277
pjsua_call_media *call_med = (pjsua_call_media*) user_data;
279
/* TODO: Do something with the packet */
280
PJ_LOG(4,(THIS_FILE, "RX %d bytes video RTP packet", (int)size));
283
/* Our callback to receive RTCP packets */
284
static void vid_rtcp_cb(void *user_data, void *pkt, pj_ssize_t size)
286
pjsua_call_media *call_med = (pjsua_call_media*) user_data;
288
/* TODO: Do something with the packet here */
289
PJ_LOG(4,(THIS_FILE, "RX %d bytes video RTCP packet", (int)size));
292
/* A demo function to send dummy "RTP" packets periodically. You would not
293
* need to have this function in the real app!
295
static void timer_to_send_vid_rtp(void *user_data)
297
pjsua_call_media *call_med = (pjsua_call_media*) user_data;
298
const char *pkt = "Not RTP packet";
300
if (!call_med->call || !call_med->call->inv || !call_med->tp) {
301
/* Call has been disconnected. There is race condition here as
302
* this cb may be called sometime after call has been disconnected */
306
pjmedia_transport_send_rtp(call_med->tp, pkt, strlen(pkt));
308
pjsua_schedule_timer2(&timer_to_send_vid_rtp, call_med, 2000);
311
static void timer_to_send_vid_rtcp(void *user_data)
313
pjsua_call_media *call_med = (pjsua_call_media*) user_data;
314
const char *pkt = "Not RTCP packet";
316
if (!call_med->call || !call_med->call->inv || !call_med->tp) {
317
/* Call has been disconnected. There is race condition here as
318
* this cb may be called sometime after call has been disconnected */
322
pjmedia_transport_send_rtcp(call_med->tp, pkt, strlen(pkt));
324
pjsua_schedule_timer2(&timer_to_send_vid_rtcp, call_med, 5000);
327
/* update video channel after SDP negotiation */
328
pj_status_t pjsua_vid_channel_update(pjsua_call_media *call_med,
330
pjmedia_vid_stream_info *si,
331
const pjmedia_sdp_session *local_sdp,
332
const pjmedia_sdp_session *remote_sdp)
336
PJ_LOG(4,(THIS_FILE, "Video channel update.."));
337
pj_log_push_indent();
339
/* Check if no media is active */
340
if (si->dir != PJMEDIA_DIR_NONE) {
341
/* Attach our RTP and RTCP callbacks to the media transport */
342
status = pjmedia_transport_attach(call_med->tp, call_med,
343
&si->rem_addr, &si->rem_rtcp,
344
pj_sockaddr_get_len(&si->rem_addr),
345
&vid_rtp_cb, &vid_rtcp_cb);
348
* - Create and start your video stream based on the parameters
352
/* For a demonstration, let's use a timer to send "RTP" packet
355
pjsua_schedule_timer2(&timer_to_send_vid_rtp, call_med, 1000);
356
pjsua_schedule_timer2(&timer_to_send_vid_rtcp, call_med, 3500);
364
/*****************************************************************************
369
pjsua_call_vid_strm_op_param_default(pjsua_call_vid_strm_op_param *param)
371
pj_bzero(param, sizeof(*param));
373
param->dir = PJMEDIA_DIR_ENCODING_DECODING;
374
param->cap_dev = PJMEDIA_VID_DEFAULT_CAPTURE_DEV;
377
PJ_DEF(void) pjsua_vid_preview_param_default(pjsua_vid_preview_param *p)
379
p->rend_id = PJMEDIA_VID_DEFAULT_RENDER_DEV;
383
PJ_DEF(pjsua_vid_win_id) pjsua_vid_preview_get_win(pjmedia_vid_dev_index id)
385
UNIMPLEMENTED(pjsua_vid_preview_get_win)
386
return PJSUA_INVALID_ID;
389
/* Reset internal window structure. Not sure if this is needed?. */
390
PJ_DEF(void) pjsua_vid_win_reset(pjsua_vid_win_id wid)
392
pjsua_vid_win *w = &pjsua_var.win[wid];
393
pj_pool_t *pool = w->pool;
395
pj_bzero(w, sizeof(*w));
396
if (pool) pj_pool_reset(pool);
399
w->preview_cap_id = PJMEDIA_VID_INVALID_DEV;
402
/* Does it have built-in preview support. */
403
PJ_DEF(pj_bool_t) pjsua_vid_preview_has_native(pjmedia_vid_dev_index id)
405
UNIMPLEMENTED(pjsua_vid_preview_has_native)
409
/* Start video preview window for the specified capture device. */
410
PJ_DEF(pj_status_t) pjsua_vid_preview_start(pjmedia_vid_dev_index id,
411
const pjsua_vid_preview_param *prm)
413
UNIMPLEMENTED(pjsua_vid_preview_start)
417
/* Stop video preview. */
418
PJ_DEF(pj_status_t) pjsua_vid_preview_stop(pjmedia_vid_dev_index id)
420
UNIMPLEMENTED(pjsua_vid_preview_stop)
425
/*****************************************************************************
429
/* Get the number of video devices installed in the system. */
430
PJ_DEF(unsigned) pjsua_vid_dev_count(void)
432
UNIMPLEMENTED(pjsua_vid_dev_count)
436
/* Retrieve the video device info for the specified device index. */
437
PJ_DEF(pj_status_t) pjsua_vid_dev_get_info(pjmedia_vid_dev_index id,
438
pjmedia_vid_dev_info *vdi)
440
UNIMPLEMENTED(pjsua_vid_dev_get_info)
444
/* Enum all video devices installed in the system. */
445
PJ_DEF(pj_status_t) pjsua_vid_enum_devs(pjmedia_vid_dev_info info[],
448
UNIMPLEMENTED(pjsua_vid_enum_devs)
453
/*****************************************************************************
457
/* Enum all supported video codecs in the system. */
458
PJ_DEF(pj_status_t) pjsua_vid_enum_codecs( pjsua_codec_info id[],
461
pjmedia_vid_codec_info info[32];
462
unsigned i, j, count, prio[32];
465
count = PJ_ARRAY_SIZE(info);
466
status = pjmedia_vid_codec_mgr_enum_codecs(NULL, &count, info, prio);
467
if (status != PJ_SUCCESS) {
471
for (i=0, j=0; i<count && j<*p_count; ++i) {
472
if (info[i].packings & PJMEDIA_VID_PACKING_PACKETS) {
473
pj_bzero(&id[j], sizeof(pjsua_codec_info));
475
pjmedia_vid_codec_info_to_id(&info[i], id[j].buf_, sizeof(id[j].buf_));
476
id[j].codec_id = pj_str(id[j].buf_);
477
id[j].priority = (pj_uint8_t) prio[i];
479
if (id[j].codec_id.slen < sizeof(id[j].buf_)) {
480
id[j].desc.ptr = id[j].codec_id.ptr + id[j].codec_id.slen + 1;
481
pj_strncpy(&id[j].desc, &info[i].encoding_desc,
482
sizeof(id[j].buf_) - id[j].codec_id.slen - 1);
493
/* Change video codec priority. */
494
PJ_DEF(pj_status_t) pjsua_vid_codec_set_priority( const pj_str_t *codec_id,
495
pj_uint8_t priority )
497
UNIMPLEMENTED(pjsua_vid_codec_set_priority)
501
/* Get video codec parameters. */
502
PJ_DEF(pj_status_t) pjsua_vid_codec_get_param(
503
const pj_str_t *codec_id,
504
pjmedia_vid_codec_param *param)
506
UNIMPLEMENTED(pjsua_vid_codec_get_param)
510
/* Set video codec parameters. */
511
PJ_DEF(pj_status_t) pjsua_vid_codec_set_param(
512
const pj_str_t *codec_id,
513
const pjmedia_vid_codec_param *param)
515
UNIMPLEMENTED(pjsua_vid_codec_set_param)
520
/*****************************************************************************
524
/* Enumerates all video windows. */
525
PJ_DEF(pj_status_t) pjsua_vid_enum_wins( pjsua_vid_win_id wids[],
528
UNIMPLEMENTED(pjsua_vid_enum_wins)
532
/* Get window info. */
533
PJ_DEF(pj_status_t) pjsua_vid_win_get_info( pjsua_vid_win_id wid,
534
pjsua_vid_win_info *wi)
536
UNIMPLEMENTED(pjsua_vid_win_get_info)
540
/* Show or hide window. */
541
PJ_DEF(pj_status_t) pjsua_vid_win_set_show( pjsua_vid_win_id wid,
544
UNIMPLEMENTED(pjsua_vid_win_set_show)
548
/* Set video window position. */
549
PJ_DEF(pj_status_t) pjsua_vid_win_set_pos( pjsua_vid_win_id wid,
550
const pjmedia_coord *pos)
552
UNIMPLEMENTED(pjsua_vid_win_set_pos)
557
PJ_DEF(pj_status_t) pjsua_vid_win_set_size( pjsua_vid_win_id wid,
558
const pjmedia_rect_size *size)
560
UNIMPLEMENTED(pjsua_vid_win_set_size)
564
/* Set video orientation. */
565
PJ_DEF(pj_status_t) pjsua_vid_win_rotate( pjsua_vid_win_id wid,
568
UNIMPLEMENTED(pjsua_vid_win_rotate)
572
/* Start, stop, and/or manipulate video transmission for the specified call. */
573
PJ_DEF(pj_status_t) pjsua_call_set_vid_strm (
574
pjsua_call_id call_id,
575
pjsua_call_vid_strm_op op,
576
const pjsua_call_vid_strm_op_param *param)
578
UNIMPLEMENTED(pjsua_call_set_vid_strm)
583
/* Get the media stream index of the default video stream in the call. */
584
PJ_DEF(int) pjsua_call_get_vid_stream_idx(pjsua_call_id call_id)
586
UNIMPLEMENTED(pjsua_call_get_vid_stream_idx)
590
/* Determine if video stream for the specified call is currently running
591
* for the specified direction.
593
PJ_DEF(pj_bool_t) pjsua_call_vid_stream_is_running( pjsua_call_id call_id,
597
UNIMPLEMENTED(pjsua_call_vid_stream_is_running)
601
#endif /* PJSUA_HAS_VIDEO */