Add support for ICE/STUN/TURN in res_rtp_asterisk and chan_sip.
[asterisk/asterisk.git] / res / pjproject / pjsip-apps / src / 3rdparty_media_sample / alt_pjsua_vid.c
1 /* $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  */
34 struct 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
56 static 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
73 static 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
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 )
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
132 static 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
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)
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
163 static 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
171 static 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
180 static 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 */
189 pj_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 */
226 pj_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 */
235 pj_status_t pjsua_vid_subsys_destroy(void)
236 {
237     /*
238      * TODO: put your 3rd party library cleanup routine here
239      */
240     return PJ_SUCCESS;
241 }
242
243 /* Initialize video call media */
244 pj_status_t pjsua_vid_channel_init(pjsua_call_media *call_med)
245 {
246     /*
247      * TODO: put call media initialization
248      */
249     return PJ_SUCCESS;
250 }
251
252 /* Internal function to stop video stream */
253 void pjsua_vid_stop_stream(pjsua_call_media *call_med)
254 {
255     PJ_LOG(4,(THIS_FILE, "Stopping video stream.."));
256
257     if (call_med->tp) {
258         pjmedia_transport_detach(call_med->tp, call_med);
259     }
260
261     /*
262      * TODO:
263      *   - stop your video stream here
264      */
265
266 }
267
268 /* Our callback to receive incoming RTP packets */
269 static void vid_rtp_cb(void *user_data, void *pkt, pj_ssize_t size)
270 {
271     pjsua_call_media *call_med = (pjsua_call_media*) user_data;
272
273     /* TODO: Do something with the packet */
274     PJ_LOG(4,(THIS_FILE, "RX %d bytes video RTP packet", (int)size));
275 }
276
277 /* Our callback to receive RTCP packets */
278 static void vid_rtcp_cb(void *user_data, void *pkt, pj_ssize_t size)
279 {
280     pjsua_call_media *call_med = (pjsua_call_media*) user_data;
281
282     /* TODO: Do something with the packet here */
283     PJ_LOG(4,(THIS_FILE, "RX %d bytes video RTCP packet", (int)size));
284 }
285
286 /* A demo function to send dummy "RTP" packets periodically. You would not
287  * need to have this function in the real app!
288  */
289 static void timer_to_send_vid_rtp(void *user_data)
290 {
291     pjsua_call_media *call_med = (pjsua_call_media*) user_data;
292     const char *pkt = "Not RTP packet";
293
294     if (call_med->call->inv == NULL) {
295         /* Call has been disconnected. There is race condition here as
296          * this cb may be called sometime after call has been disconnected */
297         return;
298     }
299
300     pjmedia_transport_send_rtp(call_med->tp, pkt, strlen(pkt));
301
302     pjsua_schedule_timer2(&timer_to_send_vid_rtp, call_med, 2000);
303 }
304
305 static void timer_to_send_vid_rtcp(void *user_data)
306 {
307     pjsua_call_media *call_med = (pjsua_call_media*) user_data;
308     const char *pkt = "Not RTCP packet";
309
310     if (call_med->call->inv == NULL) {
311         /* Call has been disconnected. There is race condition here as
312          * this cb may be called sometime after call has been disconnected */
313         return;
314     }
315
316     pjmedia_transport_send_rtcp(call_med->tp, pkt, strlen(pkt));
317
318     pjsua_schedule_timer2(&timer_to_send_vid_rtcp, call_med, 5000);
319 }
320
321 /* update video channel after SDP negotiation */
322 pj_status_t pjsua_vid_channel_update(pjsua_call_media *call_med,
323                                      pj_pool_t *tmp_pool,
324                                      pjmedia_vid_stream_info *si,
325                                      const pjmedia_sdp_session *local_sdp,
326                                      const pjmedia_sdp_session *remote_sdp)
327 {
328     pj_status_t status;
329     
330     PJ_LOG(4,(THIS_FILE, "Video channel update.."));
331     pj_log_push_indent();
332
333     /* Check if no media is active */
334     if (si->dir != PJMEDIA_DIR_NONE) {
335         /* Attach our RTP and RTCP callbacks to the media transport */
336         status = pjmedia_transport_attach(call_med->tp, call_med,
337                                           &si->rem_addr, &si->rem_rtcp,
338                                           pj_sockaddr_get_len(&si->rem_addr),
339                                           &vid_rtp_cb, &vid_rtcp_cb);
340         /*
341          * TODO:
342          *   - Create and start your video stream based on the parameters
343          *     in si
344          */
345
346         /* For a demonstration, let's use a timer to send "RTP" packet
347          * periodically.
348          */
349         pjsua_schedule_timer2(&timer_to_send_vid_rtp, call_med, 1000);
350         pjsua_schedule_timer2(&timer_to_send_vid_rtcp, call_med, 3500);
351     }
352
353     pj_log_pop_indent();
354     return PJ_SUCCESS;
355 }
356
357
358 /*****************************************************************************
359  * Preview
360  */
361
362 PJ_DEF(void)
363 pjsua_call_vid_strm_op_param_default(pjsua_call_vid_strm_op_param *param)
364 {
365     pj_bzero(param, sizeof(*param));
366     param->med_idx = -1;
367     param->dir = PJMEDIA_DIR_ENCODING_DECODING;
368     param->cap_dev = PJMEDIA_VID_DEFAULT_CAPTURE_DEV;
369 }
370
371 PJ_DEF(void) pjsua_vid_preview_param_default(pjsua_vid_preview_param *p)
372 {
373     p->rend_id = PJMEDIA_VID_DEFAULT_RENDER_DEV;
374     p->show = PJ_TRUE;
375 }
376
377 PJ_DEF(pjsua_vid_win_id) pjsua_vid_preview_get_win(pjmedia_vid_dev_index id)
378 {
379     UNIMPLEMENTED(pjsua_vid_preview_get_win)
380     return PJSUA_INVALID_ID;
381 }
382
383 /* Reset internal window structure. Not sure if this is needed?. */
384 PJ_DEF(void) pjsua_vid_win_reset(pjsua_vid_win_id wid)
385 {
386     pjsua_vid_win *w = &pjsua_var.win[wid];
387     pj_pool_t *pool = w->pool;
388
389     pj_bzero(w, sizeof(*w));
390     if (pool) pj_pool_reset(pool);
391     w->ref_cnt = 0;
392     w->pool = pool;
393     w->preview_cap_id = PJMEDIA_VID_INVALID_DEV;
394 }
395
396 /* Does it have built-in preview support. */
397 PJ_DEF(pj_bool_t) pjsua_vid_preview_has_native(pjmedia_vid_dev_index id)
398 {
399     UNIMPLEMENTED(pjsua_vid_preview_has_native)
400     return PJ_FALSE;
401 }
402
403 /* Start video preview window for the specified capture device. */
404 PJ_DEF(pj_status_t) pjsua_vid_preview_start(pjmedia_vid_dev_index id,
405                                             const pjsua_vid_preview_param *prm)
406 {
407     UNIMPLEMENTED(pjsua_vid_preview_start)
408     return PJ_ENOTSUP;
409 }
410
411 /* Stop video preview. */
412 PJ_DEF(pj_status_t) pjsua_vid_preview_stop(pjmedia_vid_dev_index id)
413 {
414     UNIMPLEMENTED(pjsua_vid_preview_stop)
415     return PJ_ENOTSUP;
416 }
417
418
419 /*****************************************************************************
420  * Devices.
421  */
422
423 /* Get the number of video devices installed in the system. */
424 PJ_DEF(unsigned) pjsua_vid_dev_count(void)
425 {
426     UNIMPLEMENTED(pjsua_vid_dev_count)
427     return 0;
428 }
429
430 /* Retrieve the video device info for the specified device index. */
431 PJ_DEF(pj_status_t) pjsua_vid_dev_get_info(pjmedia_vid_dev_index id,
432                                            pjmedia_vid_dev_info *vdi)
433 {
434     UNIMPLEMENTED(pjsua_vid_dev_get_info)
435     return PJ_ENOTSUP;
436 }
437
438 /* Enum all video devices installed in the system. */
439 PJ_DEF(pj_status_t) pjsua_vid_enum_devs(pjmedia_vid_dev_info info[],
440                                         unsigned *count)
441 {
442     UNIMPLEMENTED(pjsua_vid_enum_devs)
443     return PJ_ENOTSUP;
444 }
445
446
447 /*****************************************************************************
448  * Codecs.
449  */
450
451 /* Enum all supported video codecs in the system. */
452 PJ_DEF(pj_status_t) pjsua_vid_enum_codecs( pjsua_codec_info id[],
453                                            unsigned *p_count )
454 {
455     pjmedia_vid_codec_info info[32];
456     unsigned i, j, count, prio[32];
457     pj_status_t status;
458
459     count = PJ_ARRAY_SIZE(info);
460     status = pjmedia_vid_codec_mgr_enum_codecs(NULL, &count, info, prio);
461     if (status != PJ_SUCCESS) {
462         *p_count = 0;
463         return status;
464     }
465     for (i=0, j=0; i<count && j<*p_count; ++i) {
466         if (info[i].packings & PJMEDIA_VID_PACKING_PACKETS) {
467             pj_bzero(&id[j], sizeof(pjsua_codec_info));
468
469             pjmedia_vid_codec_info_to_id(&info[i], id[j].buf_, sizeof(id[j].buf_));
470             id[j].codec_id = pj_str(id[j].buf_);
471             id[j].priority = (pj_uint8_t) prio[i];
472
473             if (id[j].codec_id.slen < sizeof(id[j].buf_)) {
474                 id[j].desc.ptr = id[j].codec_id.ptr + id[j].codec_id.slen + 1;
475                 pj_strncpy(&id[j].desc, &info[i].encoding_desc,
476                            sizeof(id[j].buf_) - id[j].codec_id.slen - 1);
477             }
478
479             ++j;
480         }
481     }
482
483     *p_count = j;
484     return PJ_SUCCESS;
485 }
486
487 /* Change video codec priority. */
488 PJ_DEF(pj_status_t) pjsua_vid_codec_set_priority( const pj_str_t *codec_id,
489                                                   pj_uint8_t priority )
490 {
491     UNIMPLEMENTED(pjsua_vid_codec_set_priority)
492     return PJ_ENOTSUP;
493 }
494
495 /* Get video codec parameters. */
496 PJ_DEF(pj_status_t) pjsua_vid_codec_get_param(
497                                         const pj_str_t *codec_id,
498                                         pjmedia_vid_codec_param *param)
499 {
500     UNIMPLEMENTED(pjsua_vid_codec_get_param)
501     return PJ_ENOTSUP;
502 }
503
504 /* Set video codec parameters. */
505 PJ_DEF(pj_status_t) pjsua_vid_codec_set_param(
506                                         const pj_str_t *codec_id,
507                                         const pjmedia_vid_codec_param *param)
508 {
509     UNIMPLEMENTED(pjsua_vid_codec_set_param)
510     return PJ_ENOTSUP;
511 }
512
513
514 /*****************************************************************************
515  * Window
516  */
517
518 /* Enumerates all video windows. */
519 PJ_DEF(pj_status_t) pjsua_vid_enum_wins( pjsua_vid_win_id wids[],
520                                          unsigned *count)
521 {
522     UNIMPLEMENTED(pjsua_vid_enum_wins)
523     return PJ_ENOTSUP;
524 }
525
526 /* Get window info. */
527 PJ_DEF(pj_status_t) pjsua_vid_win_get_info( pjsua_vid_win_id wid,
528                                             pjsua_vid_win_info *wi)
529 {
530     UNIMPLEMENTED(pjsua_vid_win_get_info)
531     return PJ_ENOTSUP;
532 }
533
534 /* Show or hide window. */
535 PJ_DEF(pj_status_t) pjsua_vid_win_set_show( pjsua_vid_win_id wid,
536                                             pj_bool_t show)
537 {
538     UNIMPLEMENTED(pjsua_vid_win_set_show)
539     return PJ_ENOTSUP;
540 }
541
542 /* Set video window position. */
543 PJ_DEF(pj_status_t) pjsua_vid_win_set_pos( pjsua_vid_win_id wid,
544                                            const pjmedia_coord *pos)
545 {
546     UNIMPLEMENTED(pjsua_vid_win_set_pos)
547     return PJ_ENOTSUP;
548 }
549
550 /* Resize window. */
551 PJ_DEF(pj_status_t) pjsua_vid_win_set_size( pjsua_vid_win_id wid,
552                                             const pjmedia_rect_size *size)
553 {
554     UNIMPLEMENTED(pjsua_vid_win_set_size)
555     return PJ_ENOTSUP;
556 }
557
558 /* Set video orientation. */
559 PJ_DEF(pj_status_t) pjsua_vid_win_rotate( pjsua_vid_win_id wid,
560                                           int angle)
561 {
562     UNIMPLEMENTED(pjsua_vid_win_rotate)
563     return PJ_ENOTSUP;
564 }
565
566 /* Start, stop, and/or manipulate video transmission for the specified call. */
567 PJ_DEF(pj_status_t) pjsua_call_set_vid_strm (
568                                 pjsua_call_id call_id,
569                                 pjsua_call_vid_strm_op op,
570                                 const pjsua_call_vid_strm_op_param *param)
571 {
572     UNIMPLEMENTED(pjsua_call_set_vid_strm)
573     return PJ_ENOTSUP;
574 }
575
576
577 /* Get the media stream index of the default video stream in the call. */
578 PJ_DEF(int) pjsua_call_get_vid_stream_idx(pjsua_call_id call_id)
579 {
580     UNIMPLEMENTED(pjsua_call_get_vid_stream_idx)
581     return -1;
582 }
583
584 /* Determine if video stream for the specified call is currently running
585  * for the specified direction.
586  */
587 PJ_DEF(pj_bool_t) pjsua_call_vid_stream_is_running( pjsua_call_id call_id,
588                                                     int med_idx,
589                                                     pjmedia_dir dir)
590 {
591     UNIMPLEMENTED(pjsua_call_vid_stream_is_running)
592     return PJ_FALSE;
593 }
594
595 #endif  /* PJSUA_HAS_VIDEO */
596