blob: aac10e65a7a218b8480fcd7bc45fcc9796d201a8 [file] [log] [blame]
Tristan Matthews0a329cc2013-07-17 13:20:14 -04001/* $Id$ */
2/*
3 * Copyright (C) 2011-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#include <pjsua-lib/pjsua.h>
20#include <pjsua-lib/pjsua_internal.h>
21
22#if defined(PJSUA_MEDIA_HAS_PJMEDIA) && PJSUA_MEDIA_HAS_PJMEDIA != 0
23# error The PJSUA_MEDIA_HAS_PJMEDIA should be declared as zero
24#endif
25
26#if PJSUA_HAS_VIDEO
27
28#define THIS_FILE "alt_pjsua_vid.c"
29#define UNIMPLEMENTED(func) PJ_LOG(2,(THIS_FILE, "*** Call to unimplemented function %s ***", #func));
30
31/*****************************************************************************
32 * Our video codec descriptors
33 */
34struct alt_codec_desc
35{
36 /* Predefined info */
37 pjmedia_vid_codec_info info;
38 pjmedia_format_id base_fmt_id;
39 pj_uint32_t avg_bps;
40 pj_uint32_t max_bps;
41 pjmedia_codec_fmtp dec_fmtp;
42} alt_vid_codecs[] =
43{
44 /* H.263+ */
45 {
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
49 },
50 PJMEDIA_FORMAT_H263, 256000, 512000,
51 {2, { {{"CIF",3}, {"1",1}},
52 {{"QCIF",4}, {"1",1}}, } },
53 }
54};
55
56static const struct alt_codec_desc* find_codec_desc_by_info(const pjmedia_vid_codec_info *info)
57{
58 unsigned i;
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))
65 {
66 return desc;
67 }
68 }
69
70 return NULL;
71}
72
73static pj_status_t alt_vid_codec_test_alloc( pjmedia_vid_codec_factory *factory,
74 const pjmedia_vid_codec_info *id )
75{
76 const struct alt_codec_desc *desc = find_codec_desc_by_info(id);
77 return desc? PJ_SUCCESS : PJMEDIA_CODEC_EUNSUP;
78}
79
80static 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 )
83{
84 const struct alt_codec_desc *desc = find_codec_desc_by_info(info);
85 unsigned i;
86
87 if (!desc)
88 return PJMEDIA_CODEC_EUNSUP;
89
90 pj_bzero(attr, sizeof(pjmedia_vid_codec_param));
91
92 /* Scan the requested packings and use the lowest number */
93 attr->packing = 0;
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;
98 break;
99 }
100 }
101 if (attr->packing == 0) {
102 /* No supported packing in info */
103 return PJMEDIA_CODEC_EUNSUP;
104 }
105
106 /* Direction */
107 attr->dir = desc->info.dir;
108
109 /* Encoded format */
110 pjmedia_format_init_video(&attr->enc_fmt, desc->info.fmt_id,
111 720, 480, 30000, 1001);
112
113 /* Decoded format */
114 pjmedia_format_init_video(&attr->dec_fmt, desc->info.dec_fmt_id[0],
115 //352, 288, 30000, 1001);
116 720, 576, 30000, 1001);
117
118 /* Decoding fmtp */
119 attr->dec_fmtp = desc->dec_fmtp;
120
121 /* Bitrate */
122 attr->enc_fmt.det.vid.avg_bps = desc->avg_bps;
123 attr->enc_fmt.det.vid.max_bps = desc->max_bps;
124
125 /* MTU */
126 attr->enc_mtu = PJMEDIA_MAX_MTU;
127
128 return PJ_SUCCESS;
129}
130
131
132static pj_status_t alt_vid_codec_enum_codecs( pjmedia_vid_codec_factory *factory,
133 unsigned *count,
134 pjmedia_vid_codec_info codecs[])
135{
136 unsigned i, max_cnt;
137
138 PJ_ASSERT_RETURN(codecs && *count > 0, PJ_EINVAL);
139
140 max_cnt = PJ_MIN(*count, PJ_ARRAY_SIZE(alt_vid_codecs));
141 *count = 0;
142
143 for (i=0; i<max_cnt; ++i) {
144 pj_memcpy(&codecs[*count], &alt_vid_codecs[i].info,
145 sizeof(pjmedia_vid_codec_info));
146 (*count)++;
147 }
148
149 return PJ_SUCCESS;
150}
151
152
153static 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)
156{
157 /* This will never get called since we won't be using this codec */
158 UNIMPLEMENTED(alt_vid_codec_alloc_codec)
159 return PJ_ENOTSUP;
160}
161
162
163static pj_status_t alt_vid_codec_dealloc_codec( pjmedia_vid_codec_factory *factory,
164 pjmedia_vid_codec *codec )
165{
166 /* This will never get called since we won't be using this codec */
167 UNIMPLEMENTED(alt_vid_codec_dealloc_codec)
168 return PJ_ENOTSUP;
169}
170
171static pjmedia_vid_codec_factory_op alt_vid_codec_factory_op =
172{
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
178};
179
180static struct alt_vid_codec_factory {
181 pjmedia_vid_codec_factory base;
182} alt_vid_codec_factory;
183
184/*****************************************************************************
185 * Video API implementation
186 */
187
188/* Initialize the video library */
189pj_status_t pjsua_vid_subsys_init(void)
190{
191 pjmedia_vid_codec_mgr *mgr;
192 pj_status_t status;
193
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"));
199 return status;
200 }
201
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"));
207 return status;
208 }
209
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;
213
214 status = pjmedia_vid_codec_mgr_register_factory(mgr, &alt_vid_codec_factory.base);
215 if (status != PJ_SUCCESS)
216 return status;
217
218 /*
219 * TODO: put your 3rd party library initialization routine here
220 */
221
222 return PJ_SUCCESS;
223}
224
225/* Start the video library */
226pj_status_t pjsua_vid_subsys_start(void)
227{
228 /*
229 * TODO: put your 3rd party library startup routine here
230 */
231 return PJ_SUCCESS;
232}
233
234/* Cleanup and deinitialize the video library */
235pj_status_t pjsua_vid_subsys_destroy(void)
236{
237 if (pjmedia_vid_codec_mgr_instance())
238 pjmedia_vid_codec_mgr_destroy(NULL);
239
240 if (pjmedia_video_format_mgr_instance())
241 pjmedia_video_format_mgr_destroy(NULL);
242
243 /*
244 * TODO: put your 3rd party library cleanup routine here
245 */
246 return PJ_SUCCESS;
247}
248
249/* Initialize video call media */
250pj_status_t pjsua_vid_channel_init(pjsua_call_media *call_med)
251{
252 /*
253 * TODO: put call media initialization
254 */
255 return PJ_SUCCESS;
256}
257
258/* Internal function to stop video stream */
259void pjsua_vid_stop_stream(pjsua_call_media *call_med)
260{
261 PJ_LOG(4,(THIS_FILE, "Stopping video stream.."));
262
263 if (call_med->tp) {
264 pjmedia_transport_detach(call_med->tp, call_med);
265 }
266
267 /*
268 * TODO:
269 * - stop your video stream here
270 */
271
272}
273
274/* Our callback to receive incoming RTP packets */
275static void vid_rtp_cb(void *user_data, void *pkt, pj_ssize_t size)
276{
277 pjsua_call_media *call_med = (pjsua_call_media*) user_data;
278
279 /* TODO: Do something with the packet */
280 PJ_LOG(4,(THIS_FILE, "RX %d bytes video RTP packet", (int)size));
281}
282
283/* Our callback to receive RTCP packets */
284static void vid_rtcp_cb(void *user_data, void *pkt, pj_ssize_t size)
285{
286 pjsua_call_media *call_med = (pjsua_call_media*) user_data;
287
288 /* TODO: Do something with the packet here */
289 PJ_LOG(4,(THIS_FILE, "RX %d bytes video RTCP packet", (int)size));
290}
291
292/* A demo function to send dummy "RTP" packets periodically. You would not
293 * need to have this function in the real app!
294 */
295static void timer_to_send_vid_rtp(void *user_data)
296{
297 pjsua_call_media *call_med = (pjsua_call_media*) user_data;
298 const char *pkt = "Not RTP packet";
299
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 */
303 return;
304 }
305
306 pjmedia_transport_send_rtp(call_med->tp, pkt, strlen(pkt));
307
308 pjsua_schedule_timer2(&timer_to_send_vid_rtp, call_med, 2000);
309}
310
311static void timer_to_send_vid_rtcp(void *user_data)
312{
313 pjsua_call_media *call_med = (pjsua_call_media*) user_data;
314 const char *pkt = "Not RTCP packet";
315
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 */
319 return;
320 }
321
322 pjmedia_transport_send_rtcp(call_med->tp, pkt, strlen(pkt));
323
324 pjsua_schedule_timer2(&timer_to_send_vid_rtcp, call_med, 5000);
325}
326
327/* update video channel after SDP negotiation */
328pj_status_t pjsua_vid_channel_update(pjsua_call_media *call_med,
329 pj_pool_t *tmp_pool,
330 pjmedia_vid_stream_info *si,
331 const pjmedia_sdp_session *local_sdp,
332 const pjmedia_sdp_session *remote_sdp)
333{
334 pj_status_t status;
335
336 PJ_LOG(4,(THIS_FILE, "Video channel update.."));
337 pj_log_push_indent();
338
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);
346 /*
347 * TODO:
348 * - Create and start your video stream based on the parameters
349 * in si
350 */
351
352 /* For a demonstration, let's use a timer to send "RTP" packet
353 * periodically.
354 */
355 pjsua_schedule_timer2(&timer_to_send_vid_rtp, call_med, 1000);
356 pjsua_schedule_timer2(&timer_to_send_vid_rtcp, call_med, 3500);
357 }
358
359 pj_log_pop_indent();
360 return PJ_SUCCESS;
361}
362
363
364/*****************************************************************************
365 * Preview
366 */
367
368PJ_DEF(void)
369pjsua_call_vid_strm_op_param_default(pjsua_call_vid_strm_op_param *param)
370{
371 pj_bzero(param, sizeof(*param));
372 param->med_idx = -1;
373 param->dir = PJMEDIA_DIR_ENCODING_DECODING;
374 param->cap_dev = PJMEDIA_VID_DEFAULT_CAPTURE_DEV;
375}
376
377PJ_DEF(void) pjsua_vid_preview_param_default(pjsua_vid_preview_param *p)
378{
379 p->rend_id = PJMEDIA_VID_DEFAULT_RENDER_DEV;
380 p->show = PJ_TRUE;
381}
382
383PJ_DEF(pjsua_vid_win_id) pjsua_vid_preview_get_win(pjmedia_vid_dev_index id)
384{
385 UNIMPLEMENTED(pjsua_vid_preview_get_win)
386 return PJSUA_INVALID_ID;
387}
388
389/* Reset internal window structure. Not sure if this is needed?. */
390PJ_DEF(void) pjsua_vid_win_reset(pjsua_vid_win_id wid)
391{
392 pjsua_vid_win *w = &pjsua_var.win[wid];
393 pj_pool_t *pool = w->pool;
394
395 pj_bzero(w, sizeof(*w));
396 if (pool) pj_pool_reset(pool);
397 w->ref_cnt = 0;
398 w->pool = pool;
399 w->preview_cap_id = PJMEDIA_VID_INVALID_DEV;
400}
401
402/* Does it have built-in preview support. */
403PJ_DEF(pj_bool_t) pjsua_vid_preview_has_native(pjmedia_vid_dev_index id)
404{
405 UNIMPLEMENTED(pjsua_vid_preview_has_native)
406 return PJ_FALSE;
407}
408
409/* Start video preview window for the specified capture device. */
410PJ_DEF(pj_status_t) pjsua_vid_preview_start(pjmedia_vid_dev_index id,
411 const pjsua_vid_preview_param *prm)
412{
413 UNIMPLEMENTED(pjsua_vid_preview_start)
414 return PJ_ENOTSUP;
415}
416
417/* Stop video preview. */
418PJ_DEF(pj_status_t) pjsua_vid_preview_stop(pjmedia_vid_dev_index id)
419{
420 UNIMPLEMENTED(pjsua_vid_preview_stop)
421 return PJ_ENOTSUP;
422}
423
424
425/*****************************************************************************
426 * Devices.
427 */
428
429/* Get the number of video devices installed in the system. */
430PJ_DEF(unsigned) pjsua_vid_dev_count(void)
431{
432 UNIMPLEMENTED(pjsua_vid_dev_count)
433 return 0;
434}
435
436/* Retrieve the video device info for the specified device index. */
437PJ_DEF(pj_status_t) pjsua_vid_dev_get_info(pjmedia_vid_dev_index id,
438 pjmedia_vid_dev_info *vdi)
439{
440 UNIMPLEMENTED(pjsua_vid_dev_get_info)
441 return PJ_ENOTSUP;
442}
443
444/* Enum all video devices installed in the system. */
445PJ_DEF(pj_status_t) pjsua_vid_enum_devs(pjmedia_vid_dev_info info[],
446 unsigned *count)
447{
448 UNIMPLEMENTED(pjsua_vid_enum_devs)
449 return PJ_ENOTSUP;
450}
451
452
453/*****************************************************************************
454 * Codecs.
455 */
456
457/* Enum all supported video codecs in the system. */
458PJ_DEF(pj_status_t) pjsua_vid_enum_codecs( pjsua_codec_info id[],
459 unsigned *p_count )
460{
461 pjmedia_vid_codec_info info[32];
462 unsigned i, j, count, prio[32];
463 pj_status_t status;
464
465 count = PJ_ARRAY_SIZE(info);
466 status = pjmedia_vid_codec_mgr_enum_codecs(NULL, &count, info, prio);
467 if (status != PJ_SUCCESS) {
468 *p_count = 0;
469 return status;
470 }
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));
474
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];
478
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);
483 }
484
485 ++j;
486 }
487 }
488
489 *p_count = j;
490 return PJ_SUCCESS;
491}
492
493/* Change video codec priority. */
494PJ_DEF(pj_status_t) pjsua_vid_codec_set_priority( const pj_str_t *codec_id,
495 pj_uint8_t priority )
496{
497 UNIMPLEMENTED(pjsua_vid_codec_set_priority)
498 return PJ_ENOTSUP;
499}
500
501/* Get video codec parameters. */
502PJ_DEF(pj_status_t) pjsua_vid_codec_get_param(
503 const pj_str_t *codec_id,
504 pjmedia_vid_codec_param *param)
505{
506 UNIMPLEMENTED(pjsua_vid_codec_get_param)
507 return PJ_ENOTSUP;
508}
509
510/* Set video codec parameters. */
511PJ_DEF(pj_status_t) pjsua_vid_codec_set_param(
512 const pj_str_t *codec_id,
513 const pjmedia_vid_codec_param *param)
514{
515 UNIMPLEMENTED(pjsua_vid_codec_set_param)
516 return PJ_ENOTSUP;
517}
518
519
520/*****************************************************************************
521 * Window
522 */
523
524/* Enumerates all video windows. */
525PJ_DEF(pj_status_t) pjsua_vid_enum_wins( pjsua_vid_win_id wids[],
526 unsigned *count)
527{
528 UNIMPLEMENTED(pjsua_vid_enum_wins)
529 return PJ_ENOTSUP;
530}
531
532/* Get window info. */
533PJ_DEF(pj_status_t) pjsua_vid_win_get_info( pjsua_vid_win_id wid,
534 pjsua_vid_win_info *wi)
535{
536 UNIMPLEMENTED(pjsua_vid_win_get_info)
537 return PJ_ENOTSUP;
538}
539
540/* Show or hide window. */
541PJ_DEF(pj_status_t) pjsua_vid_win_set_show( pjsua_vid_win_id wid,
542 pj_bool_t show)
543{
544 UNIMPLEMENTED(pjsua_vid_win_set_show)
545 return PJ_ENOTSUP;
546}
547
548/* Set video window position. */
549PJ_DEF(pj_status_t) pjsua_vid_win_set_pos( pjsua_vid_win_id wid,
550 const pjmedia_coord *pos)
551{
552 UNIMPLEMENTED(pjsua_vid_win_set_pos)
553 return PJ_ENOTSUP;
554}
555
556/* Resize window. */
557PJ_DEF(pj_status_t) pjsua_vid_win_set_size( pjsua_vid_win_id wid,
558 const pjmedia_rect_size *size)
559{
560 UNIMPLEMENTED(pjsua_vid_win_set_size)
561 return PJ_ENOTSUP;
562}
563
564/* Set video orientation. */
565PJ_DEF(pj_status_t) pjsua_vid_win_rotate( pjsua_vid_win_id wid,
566 int angle)
567{
568 UNIMPLEMENTED(pjsua_vid_win_rotate)
569 return PJ_ENOTSUP;
570}
571
572/* Start, stop, and/or manipulate video transmission for the specified call. */
573PJ_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)
577{
578 UNIMPLEMENTED(pjsua_call_set_vid_strm)
579 return PJ_ENOTSUP;
580}
581
582
583/* Get the media stream index of the default video stream in the call. */
584PJ_DEF(int) pjsua_call_get_vid_stream_idx(pjsua_call_id call_id)
585{
586 UNIMPLEMENTED(pjsua_call_get_vid_stream_idx)
587 return -1;
588}
589
590/* Determine if video stream for the specified call is currently running
591 * for the specified direction.
592 */
593PJ_DEF(pj_bool_t) pjsua_call_vid_stream_is_running( pjsua_call_id call_id,
594 int med_idx,
595 pjmedia_dir dir)
596{
597 UNIMPLEMENTED(pjsua_call_vid_stream_is_running)
598 return PJ_FALSE;
599}
600
601#endif /* PJSUA_HAS_VIDEO */
602