Debian Codegraph @

Xpjproject/pjsip/src/pjsua-lib/pjsua_vid.c

raw symbols SCIP
1 /*
2 * Copyright (C) 2011-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 #include <pjsua-lib/pjsua.h>
19 #include <pjsua-lib/pjsua_internal.h>
20
21 #if defined(PJSUA_MEDIA_HAS_PJMEDIA) && PJSUA_MEDIA_HAS_PJMEDIA != 0
22
23 #define THIS_FILE "pjsua_vid.c"
24
25 #if PJSUA_HAS_VIDEO
26
27 #define ENABLE_EVENT 1
28
29 #define PJSUA_SHOW_WINDOW 1
30 #define PJSUA_HIDE_WINDOW 0
31
32
33 static pjsua_vid_win_id vid_preview_get_win(pjmedia_vid_dev_index id,
34 pj_bool_t running_only);
35 static void free_vid_win(pjsua_vid_win_id wid);
36 static pj_status_t media_event_unsubscribe(pjmedia_event_mgr* mgr,
37 pjmedia_event_cb* cb,
38 void* user_data,
39 void* epub);
40
41 /*****************************************************************************
42 * pjsua video subsystem.
43 */
44 pj_status_t pjsua_vid_subsys_init(void)
45 {
46 unsigned i;
47 pj_status_t status;
48
49 PJ_LOG(4,(THIS_FILE, "Initializing video subsystem.."));
50 pj_log_push_indent();
51
52 status = pjmedia_video_format_mgr_create(pjsua_var.pool, 64, 0, NULL);
53 if (status != PJ_SUCCESS) {
54 pjsua_perror(THIS_FILE, "Error creating PJMEDIA video format manager",
55 status);
56 goto on_error;
57 }
58
59 status = pjmedia_converter_mgr_create(pjsua_var.pool, NULL);
60 if (status != PJ_SUCCESS) {
61 pjsua_perror(THIS_FILE, "Error creating PJMEDIA converter manager",
62 status);
63 goto on_error;
64 }
65
66 status = pjmedia_vid_codec_mgr_create(pjsua_var.pool, NULL);
67 if (status != PJ_SUCCESS) {
68 pjsua_perror(THIS_FILE, "Error creating PJMEDIA video codec manager",
69 status);
70 goto on_error;
71 }
72
73 status = pjmedia_vid_conf_create(pjsua_var.pool, NULL,
74 &pjsua_var.vid_conf);
75 if (status != PJ_SUCCESS) {
76 pjsua_perror(THIS_FILE,
77 "Error creating PJMEDIA video conference bridge",
78 status);
79 goto on_error;
80 }
81
82 #if PJMEDIA_HAS_VIDEO && PJMEDIA_HAS_VID_TOOLBOX_CODEC
83 status = pjmedia_codec_vid_toolbox_init(NULL, &pjsua_var.cp.factory);
84 if (status != PJ_SUCCESS) {
85 pjsua_perror(THIS_FILE, "Error initializing Video Toolbox codec",
86 status);
87 goto on_error;
88 }
89 #endif
90
91 #if PJMEDIA_HAS_VIDEO && PJMEDIA_HAS_ANDROID_MEDIACODEC
92 status = pjmedia_codec_and_media_vid_init(NULL, &pjsua_var.cp.factory);
93 if (status != PJ_SUCCESS) {
94 pjsua_perror(THIS_FILE, "Error initializing AMediaCodec library",
95 status);
96 goto on_error;
97 }
98 #endif
99
100 #if PJMEDIA_HAS_VIDEO && PJMEDIA_HAS_OPENH264_CODEC
101 status = pjmedia_codec_openh264_vid_init(NULL, &pjsua_var.cp.factory);
102 if (status != PJ_SUCCESS) {
103 pjsua_perror(THIS_FILE, "Error initializing OpenH264 library",
104 status);
105 goto on_error;
106 }
107 #endif
108
109 #if PJMEDIA_HAS_VIDEO && PJMEDIA_HAS_FFMPEG_VID_CODEC
110 status = pjmedia_codec_ffmpeg_vid_init(NULL, &pjsua_var.cp.factory);
111 if (status != PJ_SUCCESS) {
112 pjsua_perror(THIS_FILE, "Error initializing ffmpeg library", status);
113 goto on_error;
114 }
115 #endif
116
117 #if PJMEDIA_HAS_VIDEO && PJMEDIA_HAS_VPX_CODEC
118 status = pjmedia_codec_vpx_vid_init(NULL, &pjsua_var.cp.factory);
119 if (status != PJ_SUCCESS) {
120 pjsua_perror(THIS_FILE, "Error initializing VPX library",
121 status);
122 goto on_error;
123 }
124 #endif
125
126 #if !defined(PJSUA_DONT_INIT_VID_DEV_SUBSYS) || \
127 PJSUA_DONT_INIT_VID_DEV_SUBSYS==0
128 status = pjmedia_vid_dev_subsys_init(&pjsua_var.cp.factory);
129 if (status != PJ_SUCCESS) {
130 pjsua_perror(THIS_FILE, "Error creating PJMEDIA video subsystem",
131 status);
132 goto on_error;
133 }
134 #endif
135
136 for (i=0; i<PJSUA_MAX_VID_WINS; ++i) {
137 if (pjsua_var.win[i].pool == NULL) {
138 pjsua_var.win[i].pool = pjsua_pool_create("win%p", 512, 512);
139 if (pjsua_var.win[i].pool == NULL) {
140 status = PJ_ENOMEM;
141 goto on_error;
142 }
143 }
144 }
145
146 /* Set vid conf operation callback. */
147 if (pjsua_var.ua_cfg.cb.on_vid_conf_op_completed) {
148 pjmedia_vid_conf_set_op_cb(pjsua_var.vid_conf,
149 pjsua_var.ua_cfg.cb.on_vid_conf_op_completed);
150 }
151
152 pj_log_pop_indent();
153 return PJ_SUCCESS;
154
155 on_error:
156 pj_log_pop_indent();
157 return status;
158 }
159
160 pj_status_t pjsua_vid_subsys_start(void)
161 {
162 return PJ_SUCCESS;
163 }
164
165 pj_status_t pjsua_vid_subsys_destroy(void)
166 {
167 unsigned i;
168
169 PJ_LOG(4,(THIS_FILE, "Destroying video subsystem.."));
170 pj_log_push_indent();
171
172 for (i=0; i<PJSUA_MAX_VID_WINS; ++i) {
173 if (pjsua_var.win[i].pool) {
174 free_vid_win(i);
175 pj_pool_release(pjsua_var.win[i].pool);
176 pjsua_var.win[i].pool = NULL;
177 }
178 }
179
180 if (pjsua_var.vid_conf) {
181 pjmedia_vid_conf_destroy(pjsua_var.vid_conf);
182 pjsua_var.vid_conf = NULL;
183 }
184
185 /* Destroy avi file players. To safely release the resources, all ports
186 * created by the player needed to be disconnected from other ports.
187 * Disconnecting and removing the ports from the conference will
188 * be done from #pjmedia_vid_conf_destroy() above.
189 */
190 for (i = 0; i < PJ_ARRAY_SIZE(pjsua_var.avi_player); ++i) {
191 if (pjsua_var.avi_player[i].avi_streams != NULL)
192 {
193 PJ_LOG(2, (THIS_FILE, "Destructor for avi player id=%d "
194 "is not called", i));
195 pjsua_avi_player_destroy(i);
196 }
197 }
198 if (pjsua_var.avi_factory)
199 pjsua_var.avi_factory = NULL;
200
201 /* Destroy avi file recorders */
202 for (i = 0; i < PJ_ARRAY_SIZE(pjsua_var.avi_recorder); ++i) {
203 if (pjsua_var.avi_recorder[i].avi_streams != NULL)
204 {
205 PJ_LOG(2, (THIS_FILE, "Destructor for avi recorder id=%d "
206 "is not called", i));
207 pjsua_avi_recorder_destroy(i);
208 }
209 }
210
211 pjmedia_vid_dev_subsys_shutdown();
212
213 #if PJMEDIA_HAS_FFMPEG_VID_CODEC
214 pjmedia_codec_ffmpeg_vid_deinit();
215 #endif
216
217 #if PJMEDIA_HAS_VIDEO && PJMEDIA_HAS_VID_TOOLBOX_CODEC
218 pjmedia_codec_vid_toolbox_deinit();
219 #endif
220
221 #if defined(PJMEDIA_HAS_ANDROID_MEDIACODEC) && \
222 PJMEDIA_HAS_ANDROID_MEDIACODEC != 0
223 pjmedia_codec_and_media_vid_deinit();
224 #endif
225
226 #if defined(PJMEDIA_HAS_OPENH264_CODEC) && PJMEDIA_HAS_OPENH264_CODEC != 0
227 pjmedia_codec_openh264_vid_deinit();
228 #endif
229
230 #if defined(PJMEDIA_HAS_VPX_CODEC) && PJMEDIA_HAS_VPX_CODEC != 0
231 pjmedia_codec_vpx_vid_deinit();
232 #endif
233
234 if (pjmedia_vid_codec_mgr_instance())
235 pjmedia_vid_codec_mgr_destroy(NULL);
236
237 if (pjmedia_converter_mgr_instance())
238 pjmedia_converter_mgr_destroy(NULL);
239
240 if (pjmedia_video_format_mgr_instance())
241 pjmedia_video_format_mgr_destroy(NULL);
242
243 pj_log_pop_indent();
244 return PJ_SUCCESS;
245 }
246
247 PJ_DEF(const char*) pjsua_vid_win_type_name(pjsua_vid_win_type wt)
248 {
249 const char *win_type_names[] = {
250 "none",
251 "preview",
252 "stream"
253 };
254
255 return (wt < PJ_ARRAY_SIZE(win_type_names)) ? win_type_names[wt] : "??";
256 }
257
258 PJ_DEF(void)
259 pjsua_call_vid_strm_op_param_default(pjsua_call_vid_strm_op_param *param)
260 {
261 pj_bzero(param, sizeof(*param));
262 param->med_idx = -1;
263 param->dir = PJMEDIA_DIR_ENCODING_DECODING;
264 param->cap_dev = PJMEDIA_VID_DEFAULT_CAPTURE_DEV;
265 }
266
267 PJ_DEF(void) pjsua_vid_preview_param_default(pjsua_vid_preview_param *p)
268 {
269 p->rend_id = PJMEDIA_VID_DEFAULT_RENDER_DEV;
270 p->show = PJ_TRUE;
271 p->wnd_flags = 0;
272 pj_bzero(&p->format, sizeof(p->format));
273 pj_bzero(&p->wnd, sizeof(p->wnd));
274 }
275
276 static pj_status_t media_event_unsubscribe(pjmedia_event_mgr* mgr,
277 pjmedia_event_cb* cb,
278 void* user_data,
279 void* epub)
280 {
281 unsigned num_locks = 0;
282 pj_status_t status;
283
284 /* Release locks before unsubscribing, to avoid deadlock. */
285 num_locks = PJSUA_RELEASE_LOCK();
286
287 status = pjmedia_event_unsubscribe(mgr, cb, user_data, epub);
288
289 /* Re-acquire the locks. */
290 PJSUA_RELOCK(num_locks);
291
292 return status;
293 }
294
295 /*****************************************************************************
296 * Devices.
297 */
298
299 /*
300 * Get the number of video devices installed in the system.
301 */
302 PJ_DEF(unsigned) pjsua_vid_dev_count(void)
303 {
304 return pjmedia_vid_dev_count();
305 }
306
307 /*
308 * Retrieve the video device info for the specified device index.
309 */
310 PJ_DEF(pj_status_t) pjsua_vid_dev_get_info(pjmedia_vid_dev_index id,
311 pjmedia_vid_dev_info *vdi)
312 {
313 return pjmedia_vid_dev_get_info(id, vdi);
314 }
315
316 /*
317 * Check whether the video device is currently active.
318 */
319 PJ_DEF(pj_bool_t) pjsua_vid_dev_is_active(pjmedia_vid_dev_index id)
320 {
321 pjsua_vid_win_id wid = vid_preview_get_win(id, PJ_FALSE);
322
323 return (wid != PJSUA_INVALID_ID? PJ_TRUE: PJ_FALSE);
324 }
325
326 /*
327 * Set the capability of the video device.
328 */
329 PJ_DEF(pj_status_t) pjsua_vid_dev_set_setting( pjmedia_vid_dev_index id,
330 pjmedia_vid_dev_cap cap,
331 const void *pval,
332 pj_bool_t keep)
333 {
334 pj_status_t status = PJ_SUCCESS;
335 pjsua_vid_win_id wid;
336
337 PJSUA_LOCK();
338 wid = vid_preview_get_win(id, PJ_FALSE);
339 if (wid != PJSUA_INVALID_ID) {
340 pjsua_vid_win *w;
341 pjmedia_vid_dev_stream *cap_dev;
342
343 w = &pjsua_var.win[wid];
344 cap_dev = pjmedia_vid_port_get_stream(w->vp_cap);
345
346 status = pjmedia_vid_dev_stream_set_cap(cap_dev, cap, pval);
347 if (status != PJ_SUCCESS) {
348 PJSUA_UNLOCK();
349 return status;
350 }
351 } else {
352 status = PJ_ENOTFOUND;
353 }
354 PJSUA_UNLOCK();
355
356 if (keep) {
357 pjmedia_vid_dev_info info;
358
359 status = pjmedia_vid_dev_get_info(id, &info);
360 if (status != PJ_SUCCESS || (info.dir & PJMEDIA_DIR_CAPTURE) == 0)
361 return status;
362
363 /* Get real capture ID, if set to PJMEDIA_VID_DEFAULT_CAPTURE_DEV */
364 id = info.id;
365 status = pjmedia_vid_dev_param_set_cap(&pjsua_var.vid_param[id],
366 cap, pval);
367 if (status == PJ_SUCCESS) {
368 pjsua_var.vid_caps[id] |= cap;
369 }
370 }
371
372 return status;
373 }
374
375 /*
376 * Get the value of the video device capability.
377 */
378 PJ_DEF(pj_status_t) pjsua_vid_dev_get_setting( pjmedia_vid_dev_index id,
379 pjmedia_vid_dev_cap cap,
380 void *pval)
381 {
382 pj_status_t status = PJ_SUCCESS;
383 pjsua_vid_win_id wid;
384
385 PJSUA_LOCK();
386 wid = vid_preview_get_win(id, PJ_FALSE);
387 if (wid != PJSUA_INVALID_ID) {
388 pjsua_vid_win *w;
389 pjmedia_vid_dev_stream *cap_dev;
390
391 w = &pjsua_var.win[wid];
392 cap_dev = pjmedia_vid_port_get_stream(w->vp_cap);
393
394 status = pjmedia_vid_dev_stream_get_cap(cap_dev, cap, pval);
395
396 PJSUA_UNLOCK();
397 } else {
398 pjmedia_vid_dev_info info;
399
400 PJSUA_UNLOCK();
401
402 status = pjmedia_vid_dev_get_info(id, &info);
403 if (status != PJ_SUCCESS)
404 return status;
405
406 /* Get real device ID, if set to default device */
407 id = info.id;
408
409 if ((pjsua_var.vid_caps[id] & cap) != 0) {
410 status = pjmedia_vid_dev_param_get_cap(&pjsua_var.vid_param[id],
411 cap, pval);
412 } else {
413 status = PJ_ENOTFOUND;
414 }
415 }
416
417 return status;
418 }
419
420 /*
421 * Enum all video devices installed in the system.
422 */
423 PJ_DEF(pj_status_t) pjsua_vid_enum_devs(pjmedia_vid_dev_info info[],
424 unsigned *count)
425 {
426 unsigned i, dev_count;
427
428 dev_count = pjmedia_vid_dev_count();
429
430 if (dev_count > *count) dev_count = *count;
431
432 for (i=0; i<dev_count; ++i) {
433 pj_status_t status;
434
435 status = pjmedia_vid_dev_get_info(i, &info[i]);
436 if (status != PJ_SUCCESS)
437 return status;
438 }
439
440 *count = dev_count;
441
442 return PJ_SUCCESS;
443 }
444
445
446 /*****************************************************************************
447 * Codecs.
448 */
449
450 static pj_status_t find_codecs_with_rtp_packing(
451 const pj_str_t *codec_id,
452 unsigned *count,
453 const pjmedia_vid_codec_info *p_info[])
454 {
455 const pjmedia_vid_codec_info *info[32];
456 unsigned i, j, count_ = PJ_ARRAY_SIZE(info);
457 pj_status_t status;
458
459 status = pjmedia_vid_codec_mgr_find_codecs_by_id(NULL, codec_id,
460 &count_, info, NULL);
461 if (status != PJ_SUCCESS)
462 return status;
463
464 for (i = 0, j = 0; i < count_ && j<*count; ++i) {
465 if ((info[i]->packings & PJMEDIA_VID_PACKING_PACKETS) == 0)
466 continue;
467 p_info[j++] = info[i];
468 }
469 *count = j;
470 return PJ_SUCCESS;
471 }
472
473 /*
474 * Enum all supported video codecs in the system.
475 */
476 PJ_DEF(pj_status_t) pjsua_vid_enum_codecs( pjsua_codec_info id[],
477 unsigned *p_count )
478 {
479 pjmedia_vid_codec_info info[32];
480 unsigned i, j, count, prio[32];
481 pj_status_t status;
482
483 count = PJ_ARRAY_SIZE(info);
484 status = pjmedia_vid_codec_mgr_enum_codecs(NULL, &count, info, prio);
485 if (status != PJ_SUCCESS) {
486 *p_count = 0;
487 return status;
488 }
489
490 for (i=0, j=0; i<count && j<*p_count; ++i) {
491 if (info[i].packings & PJMEDIA_VID_PACKING_PACKETS) {
492 pj_bzero(&id[j], sizeof(pjsua_codec_info));
493
494 pjmedia_vid_codec_info_to_id(&info[i], id[j].buf_, sizeof(id[j].buf_));
495 id[j].codec_id = pj_str(id[j].buf_);
496 id[j].priority = (pj_uint8_t) prio[i];
497
498 if (id[j].codec_id.slen < (pj_ssize_t)sizeof(id[j].buf_)) {
499 id[j].desc.ptr = id[j].codec_id.ptr + id[j].codec_id.slen + 1;
500 pj_strncpy(&id[j].desc, &info[i].encoding_desc,
501 sizeof(id[j].buf_) - id[j].codec_id.slen - 1);
502 }
503
504 ++j;
505 }
506 }
507
508 *p_count = j;
509
510 return PJ_SUCCESS;
511 }
512
513
514 /*
515 * Change video codec priority.
516 */
517 PJ_DEF(pj_status_t) pjsua_vid_codec_set_priority( const pj_str_t *codec_id,
518 pj_uint8_t priority )
519 {
520 const pj_str_t all = { NULL, 0 };
521
522 if (codec_id->slen==1 && *codec_id->ptr=='*')
523 codec_id = &all;
524
525 return pjmedia_vid_codec_mgr_set_codec_priority(NULL, codec_id,
526 priority);
527 }
528
529
530 /*
531 * Get video codec parameters.
532 */
533 PJ_DEF(pj_status_t) pjsua_vid_codec_get_param(
534 const pj_str_t *codec_id,
535 pjmedia_vid_codec_param *param)
536 {
537 const pjmedia_vid_codec_info *info[2];
538 unsigned count = 2;
539 pj_status_t status;
540
541 status = find_codecs_with_rtp_packing(codec_id, &count, info);
542 if (status != PJ_SUCCESS)
543 return status;
544
545 if (count != 1)
546 return (count > 1? PJ_ETOOMANY : PJ_ENOTFOUND);
547
548 status = pjmedia_vid_codec_mgr_get_default_param(NULL, info[0], param);
549 return status;
550 }
551
552
553 /*
554 * Set video codec parameters.
555 */
556 PJ_DEF(pj_status_t) pjsua_vid_codec_set_param(
557 const pj_str_t *codec_id,
558 const pjmedia_vid_codec_param *param)
559 {
560 const pjmedia_vid_codec_info *info[2];
561 unsigned count = 2;
562 pj_status_t status;
563
564 status = find_codecs_with_rtp_packing(codec_id, &count, info);
565 if (status != PJ_SUCCESS)
566 return status;
567
568 if (count != 1)
569 return (count > 1? PJ_ETOOMANY : PJ_ENOTFOUND);
570
571 status = pjmedia_vid_codec_mgr_set_default_param(NULL, info[0], param);
572 return status;
573 }
574
575
576 /*****************************************************************************
577 * Preview
578 */
579
580 static pjsua_vid_win_id vid_preview_get_win(pjmedia_vid_dev_index id,
581 pj_bool_t running_only)
582 {
583 pjsua_vid_win_id wid = PJSUA_INVALID_ID;
584 unsigned i;
585
586 PJSUA_LOCK();
587
588 /* Get real capture ID, if set to PJMEDIA_VID_DEFAULT_CAPTURE_DEV */
589 if (id == PJMEDIA_VID_DEFAULT_CAPTURE_DEV) {
590 pjmedia_vid_dev_info info;
591 if (pjmedia_vid_dev_get_info(id, &info) != PJ_SUCCESS) {
592 PJSUA_UNLOCK();
593 return wid;
594 }
595 id = info.id;
596 }
597
598 for (i=0; i<PJSUA_MAX_VID_WINS; ++i) {
599 pjsua_vid_win *w = &pjsua_var.win[i];
600 if (w->type == PJSUA_WND_TYPE_PREVIEW && w->preview_cap_id == id) {
601 wid = i;
602 break;
603 }
604 }
605
606 if (wid != PJSUA_INVALID_ID && running_only) {
607 pjsua_vid_win *w = &pjsua_var.win[wid];
608 wid = w->preview_running ? wid : PJSUA_INVALID_ID;
609 }
610
611 PJSUA_UNLOCK();
612
613 return wid;
614 }
615
616 /*
617 * NOTE: internal function don't use this!!! Use vid_preview_get_win()
618 * instead. This is because this function will only return window ID
619 * if preview is currently running.
620 */
621 PJ_DEF(pjsua_vid_win_id) pjsua_vid_preview_get_win(pjmedia_vid_dev_index id)
622 {
623 return vid_preview_get_win(id, PJ_TRUE);
624 }
625
626 /*
627 * Get video conference slot ID of the specified capture device.
628 */
629 PJ_DEF(pjsua_conf_port_id) pjsua_vid_preview_get_vid_conf_port(
630 pjmedia_vid_dev_index id)
631 {
632 pjsua_vid_win_id wid;
633 pjsua_vid_win *w;
634 pjsua_conf_port_id conf_id = PJSUA_INVALID_ID;
635
636 PJSUA_LOCK();
637 wid = vid_preview_get_win(id, PJ_TRUE);
638 if (wid != PJSUA_INVALID_ID) {
639 w = &pjsua_var.win[wid];
640 conf_id = w->cap_slot;
641 }
642 PJSUA_UNLOCK();
643
644 return conf_id;
645 }
646
647
648 void pjsua_vid_win_reset(pjsua_vid_win_id wid)
649 {
650 pjsua_vid_win *w = &pjsua_var.win[wid];
651 pj_pool_t *pool = w->pool;
652
653 pj_bzero(w, sizeof(*w));
654 if (pool) pj_pool_reset(pool);
655 w->ref_cnt = 0;
656 w->pool = pool;
657 w->preview_cap_id = PJMEDIA_VID_INVALID_DEV;
658 }
659
660 /* Allocate and initialize pjsua video window:
661 * - If the type is preview: capture port and render port
662 * will be instantiated, and connected via conf.
663 * - If the type is stream: only render port will be created.
664 */
665 static pj_status_t create_vid_win(pjsua_vid_win_type type,
666 const pjmedia_format *fmt,
667 pjmedia_vid_dev_index rend_id,
668 pjmedia_vid_dev_index cap_id,
669 pj_bool_t show,
670 unsigned wnd_flags,
671 const pjmedia_vid_dev_hwnd *wnd,
672 pjsua_vid_win_id *id)
673 {
674 pj_bool_t enable_native_preview;
675 pjsua_vid_win_id wid = PJSUA_INVALID_ID;
676 pjsua_vid_win *w = NULL;
677 pjmedia_vid_port_param vp_param;
678 pjmedia_format fmt_;
679 pj_status_t status;
680 unsigned i;
681
682 enable_native_preview = pjsua_var.media_cfg.vid_preview_enable_native;
683
684 PJ_LOG(4,(THIS_FILE,
685 "Creating video window: type=%s, cap_id=%d, rend_id=%d",
686 pjsua_vid_win_type_name(type), cap_id, rend_id));
687 pj_log_push_indent();
688
689 /* If type is preview, check if it exists already */
690 if (type == PJSUA_WND_TYPE_PREVIEW) {
691 wid = vid_preview_get_win(cap_id, PJ_FALSE);
692 if (wid != PJSUA_INVALID_ID) {
693 /* Yes, it exists */
694 /* Show/hide window */
695 pjmedia_vid_dev_stream *strm;
696 pj_bool_t hide = !show;
697
698 w = &pjsua_var.win[wid];
699
700 PJ_LOG(4,(THIS_FILE,
701 "Window already exists for cap_dev=%d, returning wid=%d",
702 cap_id, wid));
703
704
705 if (w->is_native) {
706 strm = pjmedia_vid_port_get_stream(w->vp_cap);
707 } else {
708 strm = pjmedia_vid_port_get_stream(w->vp_rend);
709 }
710 pj_assert(strm);
711
712 /* Try to apply show/hide, window flags, and output window */
713
714 status = pjmedia_vid_dev_stream_set_cap(
715 strm, PJMEDIA_VID_DEV_CAP_OUTPUT_HIDE,
716 &hide);
717 if (status != PJ_SUCCESS) {
718 PJ_PERROR(4,(THIS_FILE, status,
719 "Ignored error on setting window visibility "
720 "on wid=%d", wid));
721 }
722
723 status = pjmedia_vid_dev_stream_set_cap(
724 strm, PJMEDIA_VID_DEV_CAP_OUTPUT_WINDOW_FLAGS,
725 &wnd_flags);
726 if (status != PJ_SUCCESS) {
727 PJ_PERROR(4,(THIS_FILE, status,
728 "Ignored error on setting window flags "
729 "on wid=%d", wid));
730 }
731
732 if (wnd) {
733 status = pjmedia_vid_dev_stream_set_cap(
734 strm, PJMEDIA_VID_DEV_CAP_OUTPUT_WINDOW, wnd);
735
736 if (status != PJ_SUCCESS) {
737 PJ_PERROR(4, (THIS_FILE, status,
738 "Ignored error on setting window handle "
739 "on wid=%d", wid));
740 }
741 }
742
743 if (!w->is_native) {
744 status = pjsua_vid_conf_connect(w->cap_slot, w->rend_slot,
745 NULL);
746 if (status != PJ_SUCCESS) {
747 PJ_PERROR(4, (THIS_FILE, status,
748 "Ignored error on connecting video ports "
749 "on wid=%d", wid));
750 }
751 }
752
753 /* Done */
754 *id = wid;
755 pj_log_pop_indent();
756
757 return PJ_SUCCESS;
758 }
759 }
760
761 /* Allocate window */
762 for (i=0; i<PJSUA_MAX_VID_WINS; ++i) {
763 w = &pjsua_var.win[i];
764 if (w->type == PJSUA_WND_TYPE_NONE) {
765 wid = i;
766 w->type = type;
767 break;
768 }
769 }
770 if (i == PJSUA_MAX_VID_WINS) {
771 pj_log_pop_indent();
772 return PJ_ETOOMANY;
773 }
774
775 /* Initialize window */
776 pjmedia_vid_port_param_default(&vp_param);
777
778 if (w->type == PJSUA_WND_TYPE_PREVIEW) {
779 pjmedia_vid_dev_info vdi;
780
781 /*
782 * Determine if the device supports native preview.
783 */
784 status = pjmedia_vid_dev_get_info(cap_id, &vdi);
785 if (status != PJ_SUCCESS)
786 goto on_error;
787
788 if (enable_native_preview &&
789 (vdi.caps & PJMEDIA_VID_DEV_CAP_INPUT_PREVIEW))
790 {
791 /* Device supports native preview! */
792 w->is_native = PJ_TRUE;
793 }
794
795 status = pjmedia_vid_dev_default_param(w->pool, cap_id,
796 &vp_param.vidparam);
797 if (status != PJ_SUCCESS)
798 goto on_error;
799
800 if (w->is_native) {
801 vp_param.vidparam.flags |= PJMEDIA_VID_DEV_CAP_OUTPUT_HIDE;
802 vp_param.vidparam.window_hide = !show;
803 vp_param.vidparam.flags |= PJMEDIA_VID_DEV_CAP_OUTPUT_WINDOW_FLAGS;
804 vp_param.vidparam.window_flags = wnd_flags;
805 }
806
807 /* Normalize capture ID, in case it was set to
808 * PJMEDIA_VID_DEFAULT_CAPTURE_DEV
809 */
810 cap_id = vp_param.vidparam.cap_id;
811
812 /* Assign preview capture device ID */
813 w->preview_cap_id = cap_id;
814
815 /* Create capture video port */
816 vp_param.active = PJ_FALSE;
817 vp_param.vidparam.dir = PJMEDIA_DIR_CAPTURE;
818
819 /* Update the video setting with user preference */
820 #define update_param(cap, field) \
821 if ((pjsua_var.vid_caps[cap_id] & cap) && (vdi.caps & cap)) { \
822 vp_param.vidparam.flags |= cap; \
823 pj_memcpy(&vp_param.vidparam.field, \
824 &pjsua_var.vid_param[cap_id].field, \
825 sizeof(vp_param.vidparam.field)); \
826 }
827
828 if (fmt) {
829 vp_param.vidparam.fmt = *fmt;
830 } else {
831 update_param(PJMEDIA_VID_DEV_CAP_FORMAT, fmt);
832 }
833
834 update_param(PJMEDIA_VID_DEV_CAP_ORIENTATION, orient);
835
836 #undef update_param
837
838 status = pjmedia_vid_port_create(w->pool, &vp_param, &w->vp_cap);
839 if (status != PJ_SUCCESS)
840 goto on_error;
841
842 /* Update format info */
843 fmt_ = vp_param.vidparam.fmt;
844 fmt = &fmt_;
845
846 /* Register capturer to the video conf */
847 status = pjsua_vid_conf_add_port(
848 w->pool,
849 pjmedia_vid_port_get_passive_port(w->vp_cap),
850 NULL, &w->cap_slot);
851 if (status != PJ_SUCCESS)
852 goto on_error;
853
854 /* If device supports native preview, enable it */
855 if (w->is_native) {
856 pjmedia_vid_dev_stream *cap_dev;
857 pj_bool_t enabled = PJ_TRUE;
858
859 cap_dev = pjmedia_vid_port_get_stream(w->vp_cap);
860 status = pjmedia_vid_dev_stream_set_cap(
861 cap_dev, PJMEDIA_VID_DEV_CAP_INPUT_PREVIEW,
862 &enabled);
863 if (status != PJ_SUCCESS) {
864 pjsua_perror(THIS_FILE,
865 "Error activating native preview, falling back "
866 "to software preview..",
867 status);
868 w->is_native = PJ_FALSE;
869 }
870 }
871 }
872
873 /* Create renderer video port, only if it's not a native preview */
874 if (!w->is_native) {
875 status = pjmedia_vid_dev_default_param(w->pool, rend_id,
876 &vp_param.vidparam);
877 if (status != PJ_SUCCESS)
878 goto on_error;
879
880 vp_param.active = PJ_FALSE;
881 vp_param.vidparam.dir = PJMEDIA_DIR_RENDER;
882 vp_param.vidparam.fmt = *fmt;
883 vp_param.vidparam.disp_size = fmt->det.vid.size;
884 vp_param.vidparam.flags |= PJMEDIA_VID_DEV_CAP_OUTPUT_HIDE;
885 vp_param.vidparam.window_hide = !show;
886 vp_param.vidparam.flags |= PJMEDIA_VID_DEV_CAP_OUTPUT_WINDOW_FLAGS;
887 vp_param.vidparam.window_flags = wnd_flags;
888 if (wnd) {
889 vp_param.vidparam.flags |= PJMEDIA_VID_DEV_CAP_OUTPUT_WINDOW;
890 vp_param.vidparam.window = *wnd;
891 }
892
893 status = pjmedia_vid_port_create(w->pool, &vp_param, &w->vp_rend);
894 if (status != PJ_SUCCESS)
895 goto on_error;
896
897 /* Register renderer to the video conf */
898 status = pjsua_vid_conf_add_port(
899 w->pool,
900 pjmedia_vid_port_get_passive_port(w->vp_rend),
901 NULL, &w->rend_slot);
902 if (status != PJ_SUCCESS)
903 goto on_error;
904
905 /* For preview window, connect capturer & renderer (via conf) */
906 if (w->type == PJSUA_WND_TYPE_PREVIEW && show) {
907 status = pjsua_vid_conf_connect(w->cap_slot, w->rend_slot, NULL);
908 if (status != PJ_SUCCESS)
909 goto on_error;
910 }
911
912 PJ_LOG(4,(THIS_FILE,
913 "%s window id %d created for cap_dev=%d rend_dev=%d",
914 pjsua_vid_win_type_name(type), wid, cap_id, rend_id));
915 } else {
916 PJ_LOG(4,(THIS_FILE,
917 "Preview window id %d created for cap_dev %d, "
918 "using built-in preview!",
919 wid, cap_id));
920 }
921
922
923 /* Done */
924 *id = wid;
925
926 PJ_LOG(4,(THIS_FILE, "Window %d created", wid));
927 pj_log_pop_indent();
928 return PJ_SUCCESS;
929
930 on_error:
931 free_vid_win(wid);
932 pj_log_pop_indent();
933 return status;
934 }
935
936
937 static void free_vid_win(pjsua_vid_win_id wid)
938 {
939 pjsua_vid_win *w = &pjsua_var.win[wid];
940 unsigned num_locks = 0;
941
942 PJ_LOG(4,(THIS_FILE, "Window %d: destroying..", wid));
943 pj_log_push_indent();
944
945 /* Release locks before unsubscribing/destroying, to avoid deadlock. */
946 num_locks = PJSUA_RELEASE_LOCK();
947
948 if (w->vp_cap) {
949 pjsua_vid_conf_remove_port(w->cap_slot);
950 pjmedia_event_unsubscribe(NULL, &call_media_on_event, NULL,
951 w->vp_cap);
952 pjmedia_vid_port_stop(w->vp_cap);
953 pjmedia_vid_port_destroy(w->vp_cap);
954 }
955 if (w->vp_rend) {
956 pjsua_vid_conf_remove_port(w->rend_slot);
957 pjmedia_event_unsubscribe(NULL, &call_media_on_event, NULL,
958 w->vp_rend);
959 pjmedia_vid_port_stop(w->vp_rend);
960 pjmedia_vid_port_destroy(w->vp_rend);
961 }
962 /* Re-acquire the locks. */
963 PJSUA_RELOCK(num_locks);
964
965 pjsua_vid_win_reset(wid);
966
967 pj_log_pop_indent();
968 }
969
970
971 static void inc_vid_win(pjsua_vid_win_id wid)
972 {
973 pjsua_vid_win *w;
974
975 pj_assert(wid >= 0 && wid < PJSUA_MAX_VID_WINS);
976
977 w = &pjsua_var.win[wid];
978 pj_assert(w->type != PJSUA_WND_TYPE_NONE);
979 ++w->ref_cnt;
980 }
981
982 static void dec_vid_win(pjsua_vid_win_id wid)
983 {
984 pjsua_vid_win *w;
985
986 pj_assert(wid >= 0 && wid < PJSUA_MAX_VID_WINS);
987
988 w = &pjsua_var.win[wid];
989 pj_assert(w->type != PJSUA_WND_TYPE_NONE);
990 if (--w->ref_cnt == 0)
991 free_vid_win(wid);
992 }
993
994 /* Initialize video call media */
995 pj_status_t pjsua_vid_channel_init(pjsua_call_media *call_med)
996 {
997 pjsua_acc *acc = &pjsua_var.acc[call_med->call->acc_id];
998
999 call_med->strm.v.rdr_dev = acc->cfg.vid_rend_dev;
1000 call_med->strm.v.cap_dev = acc->cfg.vid_cap_dev;
1001 call_med->strm.v.strm_dec_slot = PJSUA_INVALID_ID;
1002 call_med->strm.v.strm_enc_slot = PJSUA_INVALID_ID;
1003 /*
1004 * pjmedia_vid_dev_get_info() will raise assertion when video device
1005 * subsys initialization is delayed (see PJSUA_DONT_INIT_VID_DEV_SUBSYS
1006 * or #2777). While normalizing default device IDs is not urgent
1007 * at this point.
1008
1009 if (call_med->strm.v.rdr_dev == PJMEDIA_VID_DEFAULT_RENDER_DEV) {
1010 pjmedia_vid_dev_info info;
1011 pjmedia_vid_dev_get_info(call_med->strm.v.rdr_dev, &info);
1012 call_med->strm.v.rdr_dev = info.id;
1013 }
1014 if (call_med->strm.v.cap_dev == PJMEDIA_VID_DEFAULT_CAPTURE_DEV) {
1015 pjmedia_vid_dev_info info;
1016 pjmedia_vid_dev_get_info(call_med->strm.v.cap_dev, &info);
1017 call_med->strm.v.cap_dev = info.id;
1018 }
1019 */
1020
1021 return PJ_SUCCESS;
1022 }
1023
1024 static pj_status_t setup_vid_capture(pjsua_call_media *call_med)
1025 {
1026 pjsua_acc *acc_enc = &pjsua_var.acc[call_med->call->acc_id];
1027 pjmedia_port *media_port;
1028 pjsua_vid_win *w;
1029 pjsua_vid_win_id wid;
1030 pj_bool_t just_created = PJ_FALSE;
1031 pj_status_t status;
1032
1033 /* Retrieve stream encoding port */
1034 status = pjmedia_vid_stream_get_port(call_med->strm.v.stream,
1035 PJMEDIA_DIR_ENCODING,
1036 &media_port);
1037 if (status != PJ_SUCCESS)
1038 return status;
1039
1040 PJSUA_LOCK();
1041
1042 /* Note: calling pjsua_vid_preview_get_win() even though
1043 * create_vid_win() will automatically create the window
1044 * if it doesn't exist, because create_vid_win() will modify
1045 * existing window SHOW/HIDE value.
1046 */
1047 wid = vid_preview_get_win(call_med->strm.v.cap_dev, PJ_FALSE);
1048 if (wid == PJSUA_INVALID_ID) {
1049 /* Create preview video window */
1050 status = create_vid_win(PJSUA_WND_TYPE_PREVIEW,
1051 &media_port->info.fmt,
1052 call_med->strm.v.rdr_dev,
1053 call_med->strm.v.cap_dev,
1054 PJSUA_HIDE_WINDOW,
1055 acc_enc->cfg.vid_wnd_flags,
1056 NULL,
1057 &wid);
1058 if (status != PJ_SUCCESS)
1059 goto on_error;
1060
1061 just_created = PJ_TRUE;
1062 }
1063
1064 w = &pjsua_var.win[wid];
1065 #if ENABLE_EVENT
1066 pjmedia_event_subscribe(NULL, &call_media_on_event,
1067 call_med, w->vp_cap);
1068 #endif
1069
1070 /* Connect capturer to stream encoding (via conf) */
1071 status = pjsua_vid_conf_connect(w->cap_slot,
1072 call_med->strm.v.strm_enc_slot,
1073 NULL);
1074 if (status != PJ_SUCCESS)
1075 goto on_error;
1076
1077 /* Start capturer */
1078 if (just_created) {
1079 status = pjmedia_vid_port_start(w->vp_cap);
1080 if (status != PJ_SUCCESS)
1081 goto on_error;
1082 }
1083
1084 /* Done */
1085 inc_vid_win(wid);
1086 call_med->strm.v.cap_win_id = wid;
1087 PJ_LOG(4,(THIS_FILE, "Call %d media %d: video capture set up with "
1088 "dev %d, wid=%d", call_med->call->index,
1089 call_med->idx, call_med->strm.v.cap_dev, wid));
1090
1091 PJSUA_UNLOCK();
1092
1093 return PJ_SUCCESS;
1094
1095 on_error:
1096 PJSUA_UNLOCK();
1097 return status;
1098 }
1099
1100
1101 static void timer_on_start_vid_stream_encoding(void *user_data)
1102 {
1103 pjmedia_vid_stream *vid_strm = (pjmedia_vid_stream*)user_data;
1104 pjmedia_stream_common *c_strm = (pjmedia_stream_common*)vid_strm;
1105
1106 pjmedia_vid_stream_resume(vid_strm, PJMEDIA_DIR_ENCODING);
1107 pj_grp_lock_dec_ref(c_strm->grp_lock);
1108 }
1109
1110
1111 /* Internal function: update video channel after SDP negotiation.
1112 * Warning: do not use temporary/flip-flop pool, e.g: inv->pool_prov,
1113 * for creating stream, etc, as after SDP negotiation and when
1114 * the SDP media is not changed, the stream should remain running
1115 * while the temporary/flip-flop pool may be released.
1116 */
1117 pj_status_t pjsua_vid_channel_update(pjsua_call_media *call_med,
1118 pj_pool_t *tmp_pool,
1119 pjmedia_vid_stream_info *si,
1120 const pjmedia_sdp_session *local_sdp,
1121 const pjmedia_sdp_session *remote_sdp)
1122 {
1123 pjsua_call *call = call_med->call;
1124 unsigned strm_idx = call_med->idx;
1125 pjsua_acc *acc = &pjsua_var.acc[call->acc_id];
1126 pjmedia_port *media_port;
1127 pj_status_t status;
1128
1129 PJ_UNUSED_ARG(tmp_pool);
1130 PJ_UNUSED_ARG(local_sdp);
1131 PJ_UNUSED_ARG(remote_sdp);
1132
1133 PJ_LOG(4,(THIS_FILE, "Video channel update.."));
1134 pj_log_push_indent();
1135
1136 si->rtcp_sdes_bye_disabled = pjsua_var.media_cfg.no_rtcp_sdes_bye;;
1137
1138 /* Check if no media is active */
1139 if (local_sdp->media[strm_idx]->desc.port != 0) {
1140 /* Optionally, application may modify other stream settings here
1141 * (such as jitter buffer parameters, codec ptime, etc.)
1142 */
1143 si->jb_init = pjsua_var.media_cfg.jb_init;
1144 si->jb_min_pre = pjsua_var.media_cfg.jb_min_pre;
1145 si->jb_max_pre = pjsua_var.media_cfg.jb_max_pre;
1146 si->jb_max = pjsua_var.media_cfg.jb_max;
1147
1148 /* Set SSRC and CNAME */
1149 si->ssrc = call_med->ssrc;
1150 si->cname = call->cname;
1151
1152 /* Set RTP timestamp & sequence, normally these value are intialized
1153 * automatically when stream session created, but for some cases (e.g:
1154 * call reinvite, call update) timestamp and sequence need to be kept
1155 * contigue.
1156 */
1157 si->rtp_ts = call_med->rtp_tx_ts;
1158 si->rtp_seq = call_med->rtp_tx_seq;
1159 si->rtp_seq_ts_set = call_med->rtp_tx_seq_ts_set;
1160
1161 /* Set rate control config from account setting */
1162 si->rc_cfg = acc->cfg.vid_stream_rc_cfg;
1163
1164 /* Set send keyframe config from account setting */
1165 si->sk_cfg = acc->cfg.vid_stream_sk_cfg;
1166
1167 #if defined(PJMEDIA_STREAM_ENABLE_KA) && PJMEDIA_STREAM_ENABLE_KA!=0
1168 /* Enable/disable stream keep-alive and NAT hole punch. */
1169 si->use_ka = acc->cfg.use_stream_ka;
1170
1171 si->ka_cfg = acc->cfg.stream_ka_cfg;
1172 #endif
1173
1174 /* Try to get shared format ID between the capture device and
1175 * the encoder to avoid format conversion in the capture device.
1176 */
1177 if (si->dir & PJMEDIA_DIR_ENCODING) {
1178 pjmedia_vid_dev_info dev_info;
1179 pjmedia_vid_codec_info *codec_info = &si->codec_info;
1180 unsigned i, j;
1181
1182 status = pjmedia_vid_dev_get_info(call_med->strm.v.cap_dev,
1183 &dev_info);
1184 if (status != PJ_SUCCESS)
1185 goto on_error;
1186
1187 /* Find matched format ID */
1188 for (i = 0; i < codec_info->dec_fmt_id_cnt; ++i) {
1189 for (j = 0; j < dev_info.fmt_cnt; ++j) {
1190 if (codec_info->dec_fmt_id[i] ==
1191 (pjmedia_format_id)dev_info.fmt[j].id)
1192 {
1193 /* Apply the matched format ID to the codec */
1194 si->codec_param->dec_fmt.id =
1195 codec_info->dec_fmt_id[i];
1196
1197 /* Force outer loop to break */
1198 i = codec_info->dec_fmt_id_cnt;
1199 break;
1200 }
1201 }
1202 }
1203 }
1204
1205 if (!call->hanging_up && pjsua_var.ua_cfg.cb.on_stream_precreate) {
1206 pjsua_on_stream_precreate_param prm;
1207 prm.stream_idx = strm_idx;
1208 prm.stream_info.type = PJMEDIA_TYPE_VIDEO;
1209 prm.stream_info.info.vid = *si;
1210 (*pjsua_var.ua_cfg.cb.on_stream_precreate)(call->index, &prm);
1211
1212 /* Copy back only the fields which are allowed to be changed. */
1213 si->jb_init = prm.stream_info.info.vid.jb_init;
1214 si->jb_min_pre = prm.stream_info.info.vid.jb_min_pre;
1215 si->jb_max_pre = prm.stream_info.info.vid.jb_max_pre;
1216 si->jb_max = prm.stream_info.info.vid.jb_max;
1217 #if defined(PJMEDIA_STREAM_ENABLE_KA) && (PJMEDIA_STREAM_ENABLE_KA != 0)
1218 si->use_ka = prm.stream_info.info.vid.use_ka;
1219
1220 si->ka_cfg = prm.stream_info.info.vid.ka_cfg;
1221 #endif
1222 si->rtcp_sdes_bye_disabled = prm.stream_info.info.vid.rtcp_sdes_bye_disabled;
1223 si->codec_param->enc_fmt = prm.stream_info.info.vid.codec_param->enc_fmt;
1224 }
1225
1226 /* Create session based on session info. */
1227 status = pjmedia_vid_stream_create(pjsua_var.med_endpt, NULL, si,
1228 call_med->tp, NULL,
1229 &call_med->strm.v.stream);
1230 if (status != PJ_SUCCESS)
1231 goto on_error;
1232
1233 /* Add stream to synchronizer */
1234 if (call->av_sync) {
1235 status = pjmedia_stream_common_set_avsync(
1236 (pjmedia_stream_common*)call_med->strm.v.stream,
1237 call->av_sync);
1238 if (status != PJ_SUCCESS)
1239 goto on_error;
1240 }
1241
1242 /* Subscribe to video stream events */
1243 pjmedia_event_subscribe(NULL, &call_media_on_event,
1244 call_med, call_med->strm.v.stream);
1245
1246 /* Start stream */
1247 status = pjmedia_vid_stream_start(call_med->strm.v.stream);
1248 if (status != PJ_SUCCESS)
1249 goto on_error;
1250
1251 /* Temporarily pause the encoding to avoid early RTP packet lost
1252 * because media transports are being setup/attached to streams
1253 * and CPU may be spiking after SDP nego.
1254 */
1255 status = pjmedia_vid_stream_pause(call_med->strm.v.stream,
1256 PJMEDIA_DIR_ENCODING);
1257 if (status != PJ_SUCCESS)
1258 goto on_error;
1259
1260 /* Schedule start stream encoding */
1261 if (acc->cfg.vid_out_auto_transmit) {
1262 pjmedia_stream_common *c_strm = (pjmedia_stream_common*)
1263 call_med->strm.v.stream;
1264 pj_grp_lock_add_ref(c_strm->grp_lock);
1265 if (pjsua_schedule_timer2(&timer_on_start_vid_stream_encoding,
1266 call_med->strm.v.stream,
1267 PJSUA_VIDEO_STREAM_DELAY_START_ENCODE)
1268 != PJ_SUCCESS)
1269 {
1270 if (PJSUA_VIDEO_STREAM_DELAY_START_ENCODE) {
1271 PJ_LOG(4,(THIS_FILE, "Failed in scheduling video stream "
1272 "encoding start, start it now."));
1273 }
1274 /* Just in case there is failure in scheduling */
1275 timer_on_start_vid_stream_encoding(call_med->strm.v.stream);
1276 }
1277 }
1278
1279 if (call_med->prev_state == PJSUA_CALL_MEDIA_NONE)
1280 pjmedia_vid_stream_send_rtcp_sdes(call_med->strm.v.stream);
1281
1282 /* Setup decoding direction */
1283 if (si->dir & PJMEDIA_DIR_DECODING)
1284 {
1285 pjsua_vid_win_id wid;
1286 pjsua_vid_win *w;
1287
1288 PJ_LOG(4,(THIS_FILE, "Setting up RX.."));
1289 pj_log_push_indent();
1290
1291 /* Retrieve stream decoding port */
1292 status = pjmedia_vid_stream_get_port(call_med->strm.v.stream,
1293 PJMEDIA_DIR_DECODING,
1294 &media_port);
1295 if (status != PJ_SUCCESS) {
1296 pj_log_pop_indent();
1297 goto on_error;
1298 }
1299
1300 /* Create stream video window */
1301 PJSUA_LOCK();
1302 status = create_vid_win(PJSUA_WND_TYPE_STREAM,
1303 &media_port->info.fmt,
1304 call_med->strm.v.rdr_dev,
1305 //acc->cfg.vid_rend_dev,
1306 PJSUA_INVALID_ID,
1307 acc->cfg.vid_in_auto_show,
1308 acc->cfg.vid_wnd_flags,
1309 NULL,
1310 &wid);
1311 if (status != PJ_SUCCESS) {
1312 PJSUA_UNLOCK();
1313 pj_log_pop_indent();
1314 goto on_error;
1315 }
1316
1317 w = &pjsua_var.win[wid];
1318
1319 #if ENABLE_EVENT
1320 /* Register to video events */
1321 pjmedia_event_subscribe(NULL, &call_media_on_event,
1322 call_med, w->vp_rend);
1323 pjmedia_event_subscribe(NULL, &call_media_on_event,
1324 call_med, media_port);
1325 #endif
1326
1327 /* Register renderer to stream events */
1328 pjmedia_vid_port_subscribe_event(w->vp_rend, media_port);
1329
1330 /* Register stream decoding to conf, using tmp_pool should be fine
1331 * as bridge will create its own pool (using tmp_pool factory).
1332 */
1333 status = pjsua_vid_conf_add_port(tmp_pool, media_port, NULL,
1334 &call_med->strm.v.strm_dec_slot);
1335 if (status != PJ_SUCCESS) {
1336 PJSUA_UNLOCK();
1337 pj_log_pop_indent();
1338 goto on_error;
1339 }
1340
1341 /* Connect stream to renderer (via conf) */
1342 status = pjsua_vid_conf_connect(call_med->strm.v.strm_dec_slot,
1343 w->rend_slot, NULL);
1344 if (status != PJ_SUCCESS) {
1345 PJSUA_UNLOCK();
1346 pj_log_pop_indent();
1347 goto on_error;
1348 }
1349
1350 /* Start renderer */
1351 status = pjmedia_vid_port_start(w->vp_rend);
1352 if (status != PJ_SUCCESS) {
1353 PJSUA_UNLOCK();
1354 pj_log_pop_indent();
1355 goto on_error;
1356 }
1357
1358 /* Done */
1359 inc_vid_win(wid);
1360 call_med->strm.v.rdr_win_id = wid;
1361 PJSUA_UNLOCK();
1362 pj_log_pop_indent();
1363 }
1364
1365 /* Retrieve stream encoding port */
1366 status = pjmedia_vid_stream_get_port(call_med->strm.v.stream,
1367 PJMEDIA_DIR_ENCODING,
1368 &media_port);
1369 if (status != PJ_SUCCESS)
1370 goto on_error;
1371
1372 /* Register stream encoding to conf, using tmp_pool should be fine
1373 * as bridge will create its own pool (using tmp_pool factory).
1374 */
1375 status = pjsua_vid_conf_add_port(tmp_pool, media_port, NULL,
1376 &call_med->strm.v.strm_enc_slot);
1377 if (status != PJ_SUCCESS)
1378 goto on_error;
1379
1380 /* Setup encoding direction */
1381 if (si->dir & PJMEDIA_DIR_ENCODING) {
1382 PJ_LOG(4,(THIS_FILE, "Setting up TX.."));
1383 pj_log_push_indent();
1384
1385 if (!call->local_hold && acc->cfg.vid_out_auto_transmit) {
1386 status = setup_vid_capture(call_med);
1387 if (status != PJ_SUCCESS)
1388 goto on_error;
1389 }
1390
1391 pj_log_pop_indent();
1392 }
1393 }
1394
1395 pj_log_pop_indent();
1396 return PJ_SUCCESS;
1397
1398 on_error:
1399 pj_log_pop_indent();
1400 return status;
1401 }
1402
1403
1404 /* Internal function to stop video stream */
1405 void pjsua_vid_stop_stream(pjsua_call_media *call_med)
1406 {
1407 pjmedia_vid_stream *strm = call_med->strm.v.stream;
1408 pjmedia_rtcp_stat stat;
1409 pjmedia_vid_stream_info prev_vid_si;
1410 unsigned num_locks = 0;
1411
1412 pj_assert(call_med->type == PJMEDIA_TYPE_VIDEO);
1413
1414 if (!strm)
1415 return;
1416
1417 PJ_LOG(4,(THIS_FILE, "Stopping video stream.."));
1418 pj_log_push_indent();
1419
1420 pjmedia_vid_stream_get_info(strm, &prev_vid_si);
1421 call_med->prev_local_addr = prev_vid_si.local_addr;
1422 call_med->prev_rem_addr = prev_vid_si.rem_addr;
1423
1424 pjmedia_vid_stream_send_rtcp_bye(strm);
1425
1426 /* Release locks before unsubscribing, to avoid deadlock. */
1427 num_locks = PJSUA_RELEASE_LOCK();
1428
1429 /* Unsubscribe events first, otherwise the event callbacks
1430 * can be called and access already destroyed objects.
1431 */
1432 if (call_med->strm.v.cap_win_id != PJSUA_INVALID_ID) {
1433 pjsua_vid_win *w = &pjsua_var.win[call_med->strm.v.cap_win_id];
1434
1435 /* Unsubscribe event */
1436 pjmedia_event_unsubscribe(NULL, &call_media_on_event, call_med,
1437 w->vp_cap);
1438 }
1439 if (call_med->strm.v.rdr_win_id != PJSUA_INVALID_ID) {
1440 pj_status_t status;
1441 pjmedia_port *media_port;
1442 pjsua_vid_win *w = &pjsua_var.win[call_med->strm.v.rdr_win_id];
1443
1444 /* Unsubscribe event, but stop the render first */
1445 pjmedia_vid_port_stop(w->vp_rend);
1446 pjmedia_event_unsubscribe(NULL, &call_media_on_event, call_med,
1447 w->vp_rend);
1448
1449 /* Retrieve stream decoding port */
1450 status = pjmedia_vid_stream_get_port(strm, PJMEDIA_DIR_DECODING,
1451 &media_port);
1452 if (status == PJ_SUCCESS) {
1453 pjmedia_event_unsubscribe(NULL, &call_media_on_event,
1454 call_med, media_port);
1455
1456 pjmedia_vid_port_unsubscribe_event(w->vp_rend, media_port);
1457 }
1458 }
1459 /* Unsubscribe from video stream events */
1460 pjmedia_event_unsubscribe(NULL, &call_media_on_event, call_med, strm);
1461
1462 /* Re-acquire the locks. */
1463 PJSUA_RELOCK(num_locks);
1464
1465 PJSUA_LOCK();
1466
1467 /* Now that we have unsubscribed from all events, we no longer
1468 * receive future events. But we may have scheduled some timers
1469 * to call media event callbacks, so we need to wait until those
1470 * complete. Note that we can't cancel those timers since timer
1471 * implementation doesn't guarantee there's no race between
1472 * entry cancellation and the callback being called from poll.
1473 */
1474 while (1) {
1475 pjsua_timer_list *act_timer;
1476
1477 act_timer = pjsua_var.active_timer_list.next;
1478 while (act_timer != &pjsua_var.active_timer_list) {
1479 if (act_timer->cb == &call_med_event_cb) {
1480 pjsua_event_list *eve;
1481
1482 eve = (pjsua_event_list *)act_timer->user_data;
1483
1484 if (eve->call_id == (int)call_med->call->index &&
1485 eve->med_idx == call_med->idx)
1486 {
1487 pjsip_dialog *dlg = call_med->call->inv ?
1488 call_med->call->inv->dlg : NULL;
1489
1490 /* The function may be called from worker thread, we have
1491 * to handle the events instead of simple sleep here
1492 * and must not hold any lock while handling the events:
1493 * https://github.com/pjsip/pjproject/issues/1737
1494 */
1495 num_locks = PJSUA_RELEASE_LOCK();
1496
1497 if (dlg) {
1498 pjsip_dlg_inc_session(dlg, &pjsua_var.mod);
1499 pjsip_dlg_dec_lock(dlg);
1500 }
1501
1502 pjsua_handle_events(10);
1503
1504 if (dlg) {
1505 pjsip_dlg_inc_lock(dlg);
1506 pjsip_dlg_dec_session(dlg, &pjsua_var.mod);
1507 }
1508
1509 PJSUA_RELOCK(num_locks);
1510 break;
1511 }
1512 }
1513 act_timer = act_timer->next;
1514 }
1515 if (act_timer == &pjsua_var.active_timer_list)
1516 break;
1517 }
1518
1519 if (call_med->strm.v.cap_win_id != PJSUA_INVALID_ID) {
1520 /* Decrement ref count of preview video window */
1521 dec_vid_win(call_med->strm.v.cap_win_id);
1522 call_med->strm.v.cap_win_id = PJSUA_INVALID_ID;
1523
1524 PJ_LOG(4,(THIS_FILE, "Call %d media %d: Preview video window "
1525 "released", call_med->call->index,
1526 call_med->idx));
1527 }
1528
1529 if (call_med->strm.v.rdr_win_id != PJSUA_INVALID_ID) {
1530 /* Decrement ref count of stream video window */
1531 dec_vid_win(call_med->strm.v.rdr_win_id);
1532 call_med->strm.v.rdr_win_id = PJSUA_INVALID_ID;
1533
1534 PJ_LOG(4,(THIS_FILE, "Call %d media %d: Stream video window "
1535 "released", call_med->call->index,
1536 call_med->idx));
1537 }
1538 PJSUA_UNLOCK();
1539
1540 /* Unregister video stream ports (encode+decode) from conference */
1541 if (call_med->strm.v.strm_enc_slot != PJSUA_INVALID_ID) {
1542 pjsua_vid_conf_remove_port(call_med->strm.v.strm_enc_slot);
1543 call_med->strm.v.strm_enc_slot = PJSUA_INVALID_ID;
1544 }
1545 if (call_med->strm.v.strm_dec_slot != PJSUA_INVALID_ID) {
1546 pjsua_vid_conf_remove_port(call_med->strm.v.strm_dec_slot);
1547 call_med->strm.v.strm_dec_slot = PJSUA_INVALID_ID;
1548 }
1549
1550 /* Don't check for direction and transmitted packets count as we
1551 * assume that RTP timestamp remains increasing when outgoing
1552 * direction is disabled/paused.
1553 */
1554 //if ((call_med->dir & PJMEDIA_DIR_ENCODING) &&
1555 // (pjmedia_vid_stream_get_stat(strm, &stat) == PJ_SUCCESS) &&
1556 // stat.tx.pkt)
1557 if (pjmedia_vid_stream_get_stat(strm, &stat) == PJ_SUCCESS)
1558 {
1559 /* Save RTP timestamp & sequence, so when media session is
1560 * restarted, those values will be restored as the initial
1561 * RTP timestamp & sequence of the new media session. So in
1562 * the same call session, RTP timestamp and sequence are
1563 * guaranteed to be contigue.
1564 */
1565 call_med->rtp_tx_seq_ts_set = 1 | (1 << 1);
1566 call_med->rtp_tx_seq = stat.rtp_tx_last_seq;
1567 call_med->rtp_tx_ts = stat.rtp_tx_last_ts;
1568 }
1569
1570 pjmedia_vid_stream_destroy(strm);
1571 call_med->strm.v.stream = NULL;
1572
1573 pj_log_pop_indent();
1574 }
1575
1576 /*
1577 * Does it have built-in preview support.
1578 */
1579 PJ_DEF(pj_bool_t) pjsua_vid_preview_has_native(pjmedia_vid_dev_index id)
1580 {
1581 pjmedia_vid_dev_info vdi;
1582
1583 return (pjmedia_vid_dev_get_info(id, &vdi)==PJ_SUCCESS) ?
1584 ((vdi.caps & PJMEDIA_VID_DEV_CAP_INPUT_PREVIEW)!=0) : PJ_FALSE;
1585 }
1586
1587 /*
1588 * Start video preview window for the specified capture device.
1589 */
1590 PJ_DEF(pj_status_t) pjsua_vid_preview_start(pjmedia_vid_dev_index id,
1591 const pjsua_vid_preview_param *prm)
1592 {
1593 pjsua_vid_win_id wid;
1594 pjsua_vid_win *w;
1595 pjmedia_vid_dev_index rend_id;
1596 pjsua_vid_preview_param default_param;
1597 const pjmedia_format *fmt = NULL;
1598 pj_status_t status;
1599
1600 if (!prm) {
1601 pjsua_vid_preview_param_default(&default_param);
1602 prm = &default_param;
1603 }
1604
1605 PJ_LOG(4,(THIS_FILE, "Starting preview for cap_dev=%d, show=%d",
1606 id, prm->show));
1607 pj_log_push_indent();
1608
1609 PJSUA_LOCK();
1610
1611 rend_id = prm->rend_id;
1612
1613 if (prm->format.type == PJMEDIA_TYPE_VIDEO &&
1614 prm->format.detail_type == PJMEDIA_FORMAT_DETAIL_VIDEO)
1615 {
1616 fmt = &prm->format;
1617 }
1618 status = create_vid_win(PJSUA_WND_TYPE_PREVIEW, fmt, rend_id, id,
1619 prm->show, prm->wnd_flags,
1620 (prm->wnd.info.window? &prm->wnd: NULL), &wid);
1621 if (status != PJ_SUCCESS) {
1622 PJSUA_UNLOCK();
1623 pj_log_pop_indent();
1624 return status;
1625 }
1626
1627 w = &pjsua_var.win[wid];
1628 if (w->preview_running) {
1629 PJSUA_UNLOCK();
1630 pj_log_pop_indent();
1631 return PJ_SUCCESS;
1632 }
1633
1634 /* Start renderer, unless it's native preview */
1635 if (w->is_native && !pjmedia_vid_port_is_running(w->vp_cap)) {
1636 pjmedia_vid_dev_stream *cap_dev;
1637 pj_bool_t enabled = PJ_TRUE;
1638
1639 cap_dev = pjmedia_vid_port_get_stream(w->vp_cap);
1640 status = pjmedia_vid_dev_stream_set_cap(
1641 cap_dev, PJMEDIA_VID_DEV_CAP_INPUT_PREVIEW,
1642 &enabled);
1643 if (status != PJ_SUCCESS) {
1644 pjsua_perror(THIS_FILE,
1645 "Error activating native preview, falling back "
1646 "to software preview..",
1647 status);
1648 w->is_native = PJ_FALSE;
1649 }
1650 }
1651
1652 if (!w->is_native && !pjmedia_vid_port_is_running(w->vp_rend)) {
1653 status = pjmedia_vid_port_start(w->vp_rend);
1654 if (status != PJ_SUCCESS) {
1655 PJSUA_UNLOCK();
1656 pj_log_pop_indent();
1657 return status;
1658 }
1659 }
1660
1661 /* Start capturer */
1662 if (!pjmedia_vid_port_is_running(w->vp_cap)) {
1663 status = pjmedia_vid_port_start(w->vp_cap);
1664 if (status != PJ_SUCCESS) {
1665 PJSUA_UNLOCK();
1666 pj_log_pop_indent();
1667 return status;
1668 }
1669 }
1670
1671 inc_vid_win(wid);
1672 w->preview_running = PJ_TRUE;
1673
1674 PJSUA_UNLOCK();
1675 pj_log_pop_indent();
1676 return PJ_SUCCESS;
1677 }
1678
1679 /*
1680 * Stop video preview.
1681 */
1682 PJ_DEF(pj_status_t) pjsua_vid_preview_stop(pjmedia_vid_dev_index id)
1683 {
1684 pjsua_vid_win_id wid = PJSUA_INVALID_ID;
1685 pjsua_vid_win *w;
1686 pj_status_t status;
1687
1688 PJSUA_LOCK();
1689 wid = pjsua_vid_preview_get_win(id);
1690 if (wid == PJSUA_INVALID_ID) {
1691 PJSUA_UNLOCK();
1692 return PJ_ENOTFOUND;
1693 }
1694
1695 PJ_LOG(4,(THIS_FILE, "Stopping preview for cap_dev=%d", id));
1696 pj_log_push_indent();
1697
1698 w = &pjsua_var.win[wid];
1699 if (w->preview_running) {
1700 if (w->is_native) {
1701 pjmedia_vid_dev_stream *cap_dev;
1702 pj_bool_t enabled = PJ_FALSE;
1703
1704 cap_dev = pjmedia_vid_port_get_stream(w->vp_cap);
1705 status = pjmedia_vid_dev_stream_set_cap(
1706 cap_dev, PJMEDIA_VID_DEV_CAP_INPUT_PREVIEW,
1707 &enabled);
1708 } else {
1709 status = pjsua_vid_conf_disconnect(w->cap_slot, w->rend_slot);
1710 if (status != PJ_SUCCESS) {
1711 PJ_PERROR(4, (THIS_FILE, status,
1712 "Ignored error on disconnecting video ports "
1713 "on stopping preview wid=%d", wid));
1714 }
1715 status = pjmedia_vid_port_stop(w->vp_rend);
1716 }
1717
1718 if (status != PJ_SUCCESS) {
1719 PJ_PERROR(1,(THIS_FILE, status, "Error stopping %spreview",
1720 (w->is_native ? "native " : "")));
1721 PJSUA_UNLOCK();
1722 pj_log_pop_indent();
1723 return status;
1724 }
1725
1726 dec_vid_win(wid);
1727 w->preview_running = PJ_FALSE;
1728 }
1729
1730 PJSUA_UNLOCK();
1731 pj_log_pop_indent();
1732
1733 return PJ_SUCCESS;
1734 }
1735
1736
1737 /*****************************************************************************
1738 * Window
1739 */
1740
1741
1742 /*
1743 * Enumerates all video windows.
1744 */
1745 PJ_DEF(pj_status_t) pjsua_vid_enum_wins( pjsua_vid_win_id wids[],
1746 unsigned *count)
1747 {
1748 unsigned i, cnt;
1749
1750 cnt = 0;
1751
1752 for (i=0; i<PJSUA_MAX_VID_WINS && cnt <*count; ++i) {
1753 pjsua_vid_win *w = &pjsua_var.win[i];
1754 if (w->type != PJSUA_WND_TYPE_NONE)
1755 wids[cnt++] = i;
1756 }
1757
1758 *count = cnt;
1759
1760 return PJ_SUCCESS;
1761 }
1762
1763
1764 /*
1765 * Get window info.
1766 */
1767 PJ_DEF(pj_status_t) pjsua_vid_win_get_info( pjsua_vid_win_id wid,
1768 pjsua_vid_win_info *wi)
1769 {
1770 pjsua_vid_win *w;
1771 pjmedia_vid_dev_stream *s;
1772 pjmedia_vid_dev_param vparam;
1773 pj_status_t status;
1774
1775 PJ_ASSERT_RETURN(wid >= 0 && wid < PJSUA_MAX_VID_WINS && wi, PJ_EINVAL);
1776
1777 pj_bzero(wi, sizeof(*wi));
1778
1779 PJSUA_LOCK();
1780 w = &pjsua_var.win[wid];
1781
1782 wi->is_native = w->is_native;
1783
1784 if (w->is_native) {
1785 pjmedia_vid_dev_stream *cap_strm;
1786 pjmedia_vid_dev_cap cap = PJMEDIA_VID_DEV_CAP_INPUT_PREVIEW;
1787
1788 if (!w->vp_cap) {
1789 status = PJ_EINVAL;
1790 } else {
1791 cap_strm = pjmedia_vid_port_get_stream(w->vp_cap);
1792 if (!cap_strm) {
1793 status = PJ_EINVAL;
1794 } else {
1795 status = pjmedia_vid_dev_stream_get_cap(cap_strm, cap,
1796 &wi->hwnd);
1797 }
1798 }
1799 PJSUA_UNLOCK();
1800 return status;
1801 }
1802
1803 if (w->vp_rend == NULL) {
1804 PJSUA_UNLOCK();
1805 return PJ_EINVAL;
1806 }
1807
1808 s = pjmedia_vid_port_get_stream(w->vp_rend);
1809 if (s == NULL) {
1810 PJSUA_UNLOCK();
1811 return PJ_EINVAL;
1812 }
1813
1814 status = pjmedia_vid_dev_stream_get_param(s, &vparam);
1815 if (status != PJ_SUCCESS) {
1816 PJSUA_UNLOCK();
1817 return status;
1818 }
1819
1820 wi->rdr_dev = vparam.rend_id;
1821 wi->slot_id = w->rend_slot;
1822 wi->hwnd = vparam.window;
1823 wi->show = !vparam.window_hide;
1824 wi->pos = vparam.window_pos;
1825 wi->size = vparam.disp_size;
1826
1827 PJSUA_UNLOCK();
1828
1829 return PJ_SUCCESS;
1830 }
1831
1832 /*
1833 * Show or hide window.
1834 */
1835 PJ_DEF(pj_status_t) pjsua_vid_win_set_show( pjsua_vid_win_id wid,
1836 pj_bool_t show)
1837 {
1838 pjsua_vid_win *w;
1839 pjmedia_vid_dev_stream *s;
1840 pj_bool_t hide;
1841 pj_status_t status;
1842
1843 PJ_ASSERT_RETURN(wid >= 0 && wid < PJSUA_MAX_VID_WINS, PJ_EINVAL);
1844
1845 PJSUA_LOCK();
1846
1847 w = &pjsua_var.win[wid];
1848 s = pjmedia_vid_port_get_stream(w->vp_rend? w->vp_rend: w->vp_cap);
1849 if (s == NULL) {
1850 PJSUA_UNLOCK();
1851 return PJ_EINVAL;
1852 }
1853
1854 /* Make sure that renderer gets started before shown up */
1855 if (show && w->vp_rend && !pjmedia_vid_port_is_running(w->vp_rend)) {
1856 status = pjmedia_vid_port_start(w->vp_rend);
1857 if (status != PJ_SUCCESS) {
1858 PJSUA_UNLOCK();
1859 return status;
1860 }
1861 }
1862
1863 hide = !show;
1864 status = pjmedia_vid_dev_stream_set_cap(s,
1865 PJMEDIA_VID_DEV_CAP_OUTPUT_HIDE, &hide);
1866
1867 PJSUA_UNLOCK();
1868
1869 return status;
1870 }
1871
1872 /*
1873 * Set video window position.
1874 */
1875 PJ_DEF(pj_status_t) pjsua_vid_win_set_pos( pjsua_vid_win_id wid,
1876 const pjmedia_coord *pos)
1877 {
1878 pjsua_vid_win *w;
1879 pjmedia_vid_dev_stream *s;
1880 pj_status_t status;
1881
1882 PJ_ASSERT_RETURN(wid >= 0 && wid < PJSUA_MAX_VID_WINS && pos, PJ_EINVAL);
1883
1884 PJSUA_LOCK();
1885
1886 w = &pjsua_var.win[wid];
1887 s = pjmedia_vid_port_get_stream(w->vp_rend? w->vp_rend: w->vp_cap);
1888 if (s == NULL) {
1889 PJSUA_UNLOCK();
1890 return PJ_EINVAL;
1891 }
1892
1893 status = pjmedia_vid_dev_stream_set_cap(s,
1894 PJMEDIA_VID_DEV_CAP_OUTPUT_POSITION, pos);
1895
1896 PJSUA_UNLOCK();
1897
1898 return status;
1899 }
1900
1901 /*
1902 * Resize window.
1903 */
1904 PJ_DEF(pj_status_t) pjsua_vid_win_set_size( pjsua_vid_win_id wid,
1905 const pjmedia_rect_size *size)
1906 {
1907 pjsua_vid_win *w;
1908 pjmedia_vid_dev_stream *s;
1909 pj_status_t status;
1910
1911 PJ_ASSERT_RETURN(wid >= 0 && wid < PJSUA_MAX_VID_WINS && size, PJ_EINVAL);
1912
1913 PJSUA_LOCK();
1914
1915 w = &pjsua_var.win[wid];
1916 s = pjmedia_vid_port_get_stream(w->vp_rend? w->vp_rend: w->vp_cap);
1917 if (s == NULL) {
1918 PJSUA_UNLOCK();
1919 return PJ_EINVAL;
1920 }
1921
1922 status = pjmedia_vid_dev_stream_set_cap(s,
1923 PJMEDIA_VID_DEV_CAP_OUTPUT_RESIZE, size);
1924
1925 PJSUA_UNLOCK();
1926
1927 return status;
1928 }
1929
1930 /*
1931 * Set output window.
1932 */
1933 PJ_DEF(pj_status_t) pjsua_vid_win_set_win( pjsua_vid_win_id wid,
1934 const pjmedia_vid_dev_hwnd *win)
1935 {
1936 pjsua_vid_win *w;
1937 pjmedia_vid_dev_stream *s;
1938 pj_status_t status;
1939
1940 PJ_ASSERT_RETURN(wid >= 0 && wid < PJSUA_MAX_VID_WINS && win, PJ_EINVAL);
1941
1942 PJSUA_LOCK();
1943
1944 w = &pjsua_var.win[wid];
1945 s = pjmedia_vid_port_get_stream(w->vp_rend? w->vp_rend: w->vp_cap);
1946 if (s == NULL) {
1947 PJSUA_UNLOCK();
1948 return PJ_EINVAL;
1949 }
1950
1951 status = pjmedia_vid_dev_stream_set_cap(s,
1952 PJMEDIA_VID_DEV_CAP_OUTPUT_WINDOW, win);
1953
1954 PJSUA_UNLOCK();
1955
1956 return status;
1957 }
1958
1959 /*
1960 * Set video orientation.
1961 */
1962 PJ_DEF(pj_status_t) pjsua_vid_win_rotate( pjsua_vid_win_id wid,
1963 int angle)
1964 {
1965 pjsua_vid_win *w;
1966 pjmedia_vid_dev_stream *s;
1967 pjmedia_orient orient;
1968 pj_status_t status;
1969
1970 PJ_ASSERT_RETURN(wid >= 0 && wid < PJSUA_MAX_VID_WINS, PJ_EINVAL);
1971 PJ_ASSERT_RETURN((angle % 90) == 0, PJ_EINVAL);
1972
1973 /* Normalize angle, so it must be 0, 90, 180, or 270. */
1974 angle %= 360;
1975 if (angle < 0)
1976 angle += 360;
1977
1978 /* Convert angle to pjmedia_orient */
1979 switch(angle) {
1980 case 0:
1981 /* No rotation */
1982 return PJ_SUCCESS;
1983 case 90:
1984 orient = PJMEDIA_ORIENT_ROTATE_90DEG;
1985 break;
1986 case 180:
1987 orient = PJMEDIA_ORIENT_ROTATE_180DEG;
1988 break;
1989 case 270:
1990 orient = PJMEDIA_ORIENT_ROTATE_270DEG;
1991 break;
1992 default:
1993 pj_assert(!"Angle must have been validated");
1994 return PJ_EBUG;
1995 }
1996
1997 PJSUA_LOCK();
1998
1999 w = &pjsua_var.win[wid];
2000 s = pjmedia_vid_port_get_stream(w->vp_rend? w->vp_rend: w->vp_cap);
2001 if (s == NULL) {
2002 PJSUA_UNLOCK();
2003 return PJ_EINVAL;
2004 }
2005
2006 status = pjmedia_vid_dev_stream_set_cap(s,
2007 PJMEDIA_VID_DEV_CAP_ORIENTATION, &orient);
2008
2009 PJSUA_UNLOCK();
2010
2011 return status;
2012 }
2013
2014
2015 /*
2016 * Set video window fullscreen.
2017 */
2018 PJ_DEF(pj_status_t) pjsua_vid_win_set_fullscreen(
2019 pjsua_vid_win_id wid,
2020 pjmedia_vid_dev_fullscreen_flag mode)
2021 {
2022 pjsua_vid_win *w;
2023 pjmedia_vid_dev_stream *s;
2024 pj_status_t status;
2025
2026 PJ_ASSERT_RETURN(wid >= 0 && wid < PJSUA_MAX_VID_WINS, PJ_EINVAL);
2027
2028 PJSUA_LOCK();
2029
2030 w = &pjsua_var.win[wid];
2031 s = pjmedia_vid_port_get_stream(w->vp_rend? w->vp_rend: w->vp_cap);
2032 if (s == NULL) {
2033 PJSUA_UNLOCK();
2034 return PJ_EINVAL;
2035 }
2036
2037 status = pjmedia_vid_dev_stream_set_cap(s,
2038 PJMEDIA_VID_DEV_CAP_OUTPUT_FULLSCREEN, &mode);
2039
2040 PJSUA_UNLOCK();
2041
2042 return status;
2043 }
2044
2045 static void call_get_vid_strm_info(pjsua_call *call,
2046 int *first_active,
2047 int *first_inactive,
2048 unsigned *active_cnt,
2049 unsigned *cnt)
2050 {
2051 unsigned i, var_cnt = 0;
2052
2053 if (first_active && ++var_cnt)
2054 *first_active = -1;
2055 if (first_inactive && ++var_cnt)
2056 *first_inactive = -1;
2057 if (active_cnt && ++var_cnt)
2058 *active_cnt = 0;
2059 if (cnt && ++var_cnt)
2060 *cnt = 0;
2061
2062 for (i = 0; i < call->med_cnt && var_cnt; ++i) {
2063 if (call->media[i].type == PJMEDIA_TYPE_VIDEO) {
2064 if (call->media[i].dir != PJMEDIA_DIR_NONE)
2065 {
2066 if (first_active && *first_active == -1) {
2067 *first_active = i;
2068 --var_cnt;
2069 }
2070 if (active_cnt)
2071 ++(*active_cnt);
2072 } else if (first_inactive && *first_inactive == -1) {
2073 *first_inactive = i;
2074 --var_cnt;
2075 }
2076 if (cnt)
2077 ++(*cnt);
2078 }
2079 }
2080 }
2081
2082
2083 /* Send SDP reoffer. */
2084 static pj_status_t call_reoffer_sdp(pjsua_call_id call_id,
2085 pjmedia_sdp_session *sdp)
2086 {
2087 pjsua_call *call;
2088 pjsip_tx_data *tdata;
2089 pjsip_dialog *dlg;
2090 pj_status_t status;
2091
2092 status = acquire_call("call_reoffer_sdp()", call_id, &call, &dlg);
2093 if (status != PJ_SUCCESS)
2094 return status;
2095
2096 if (call->inv->state != PJSIP_INV_STATE_CONFIRMED) {
2097 PJ_LOG(3,(THIS_FILE, "Can not re-INVITE call that is not confirmed"));
2098 pjsip_dlg_dec_lock(dlg);
2099 return PJSIP_ESESSIONSTATE;
2100 }
2101
2102 /* Notify application */
2103 if (!call->hanging_up && pjsua_var.ua_cfg.cb.on_call_sdp_created) {
2104 (*pjsua_var.ua_cfg.cb.on_call_sdp_created)(call_id, sdp,
2105 call->inv->pool_prov,
2106 NULL);
2107 }
2108
2109 /* Create re-INVITE with new offer */
2110 status = pjsip_inv_reinvite( call->inv, NULL, sdp, &tdata);
2111 if (status != PJ_SUCCESS) {
2112 pjsua_perror(THIS_FILE, "Unable to create re-INVITE", status);
2113 pjsip_dlg_dec_lock(dlg);
2114 return status;
2115 }
2116
2117 /* Send the request */
2118 status = pjsip_inv_send_msg( call->inv, tdata);
2119 if (status != PJ_SUCCESS) {
2120 pjsua_perror(THIS_FILE, "Unable to send re-INVITE", status);
2121 pjsip_dlg_dec_lock(dlg);
2122 return status;
2123 }
2124
2125 pjsip_dlg_dec_lock(dlg);
2126
2127 return PJ_SUCCESS;
2128 }
2129
2130 /* Add a new video stream into a call */
2131 static pj_status_t call_add_video(pjsua_call *call,
2132 pjmedia_vid_dev_index cap_dev,
2133 pjmedia_dir dir)
2134 {
2135 pj_pool_t *pool = call->inv->pool_prov;
2136 pjsua_acc_config *acc_cfg = &pjsua_var.acc[call->acc_id].cfg;
2137 pjsua_call_media *call_med;
2138 const pjmedia_sdp_session *current_sdp;
2139 pjmedia_sdp_session *sdp;
2140 pjmedia_sdp_media *sdp_m;
2141 pjmedia_transport_info tpinfo;
2142 unsigned options;
2143 pj_status_t status;
2144
2145 /* Verify media slot availability */
2146 if (call->med_cnt == PJSUA_MAX_CALL_MEDIA)
2147 return PJ_ETOOMANY;
2148
2149 if (pjsua_call_media_is_changing(call)) {
2150 PJ_LOG(1,(THIS_FILE, "Unable to add video" ERR_MEDIA_CHANGING));
2151 return PJ_EINVALIDOP;
2152 }
2153
2154 /* Get active local SDP and clone it */
2155 status = pjmedia_sdp_neg_get_active_local(call->inv->neg, &current_sdp);
2156 if (status != PJ_SUCCESS)
2157 return status;
2158
2159 sdp = pjmedia_sdp_session_clone(call->inv->pool_prov, current_sdp);
2160
2161 /* Clean up & sync provisional media before using it */
2162 pjsua_media_prov_revert(call->index);
2163
2164 /* Initialize call media */
2165 call_med = &call->media_prov[call->med_prov_cnt++];
2166 status = pjsua_call_media_init(call_med, PJMEDIA_TYPE_VIDEO, NULL,
2167 &acc_cfg->rtp_cfg, call->secure_level,
2168 NULL, PJ_FALSE, NULL);
2169 if (status != PJ_SUCCESS)
2170 goto on_error;
2171
2172 /* Override default capture device setting */
2173 call_med->strm.v.cap_dev = cap_dev;
2174
2175 /* Init transport media */
2176 options = (call_med->enable_rtcp_mux? PJMEDIA_TPMED_RTCP_MUX: 0);
2177 status = pjmedia_transport_media_create(call_med->tp, pool, options,
2178 NULL, call_med->idx);
2179 if (status != PJ_SUCCESS)
2180 goto on_error;
2181
2182 pjsua_set_media_tp_state(call_med, PJSUA_MED_TP_INIT);
2183
2184 /* Get transport address info */
2185 pjmedia_transport_info_init(&tpinfo);
2186 pjmedia_transport_get_info(call_med->tp, &tpinfo);
2187
2188 /* Create SDP media line */
2189 status = pjmedia_endpt_create_video_sdp(pjsua_var.med_endpt, pool,
2190 &tpinfo.sock_info, 0, &sdp_m);
2191 if (status != PJ_SUCCESS)
2192 goto on_error;
2193
2194 sdp->media[sdp->media_count++] = sdp_m;
2195
2196 /* Update media direction, if it is not 'sendrecv' */
2197 if (dir != PJMEDIA_DIR_ENCODING_DECODING) {
2198 pjmedia_sdp_attr *a;
2199
2200 /* Remove sendrecv direction attribute, if any */
2201 pjmedia_sdp_media_remove_all_attr(sdp_m, "sendrecv");
2202
2203 if (dir == PJMEDIA_DIR_ENCODING)
2204 a = pjmedia_sdp_attr_create(pool, "sendonly", NULL);
2205 else if (dir == PJMEDIA_DIR_DECODING)
2206 a = pjmedia_sdp_attr_create(pool, "recvonly", NULL);
2207 else
2208 a = pjmedia_sdp_attr_create(pool, "inactive", NULL);
2209
2210 pjmedia_sdp_media_add_attr(sdp_m, a);
2211 }
2212 call_med->def_dir = dir;
2213
2214 /* Update SDP media line by media transport */
2215 status = pjmedia_transport_encode_sdp(call_med->tp, pool,
2216 sdp, NULL, call_med->idx);
2217 if (status != PJ_SUCCESS)
2218 goto on_error;
2219
2220 status = call_reoffer_sdp(call->index, sdp);
2221 if (status != PJ_SUCCESS)
2222 goto on_error;
2223
2224 call->opt.vid_cnt++;
2225
2226 return PJ_SUCCESS;
2227
2228 on_error:
2229 if (call_med->tp) {
2230 pjsua_set_media_tp_state(call_med, PJSUA_MED_TP_NULL);
2231 pjmedia_transport_close(call_med->tp);
2232 call_med->tp = call_med->tp_orig = NULL;
2233 }
2234 call->med_prov_cnt = 0;
2235
2236 return status;
2237 }
2238
2239
2240 /* Modify a video stream from a call, i.e: update direction,
2241 * remove/disable.
2242 */
2243 static pj_status_t call_modify_video(pjsua_call *call,
2244 int med_idx,
2245 pjmedia_dir dir,
2246 pj_bool_t remove)
2247 {
2248 pjsua_call_media *call_med;
2249 const pjmedia_sdp_session *current_sdp;
2250 pjmedia_sdp_session *sdp;
2251 pj_status_t status;
2252
2253 if (pjsua_call_media_is_changing(call)) {
2254 PJ_LOG(1,(THIS_FILE, "Unable to modify video" ERR_MEDIA_CHANGING));
2255 return PJ_EINVALIDOP;
2256 }
2257
2258 /* Verify and normalize media index */
2259 if (med_idx == -1) {
2260 int first_active;
2261
2262 call_get_vid_strm_info(call, &first_active, NULL, NULL, NULL);
2263 if (first_active == -1)
2264 return PJ_ENOTFOUND;
2265
2266 med_idx = first_active;
2267 }
2268
2269 /* Clean up & sync provisional media before using it */
2270 pjsua_media_prov_revert(call->index);
2271
2272 call_med = &call->media_prov[med_idx];
2273
2274 /* Verify if the stream media type is video */
2275 if (call_med->type != PJMEDIA_TYPE_VIDEO)
2276 return PJ_EINVAL;
2277
2278 /* Verify if the stream dir is not changed */
2279 if ((!remove && call_med->dir == dir) ||
2280 ( remove && (call_med->tp_st == PJSUA_MED_TP_DISABLED ||
2281 call_med->tp == NULL)))
2282 {
2283 return PJ_SUCCESS;
2284 }
2285
2286 /* Get active local SDP and clone it */
2287 status = pjmedia_sdp_neg_get_active_local(call->inv->neg, &current_sdp);
2288 if (status != PJ_SUCCESS)
2289 return status;
2290
2291 sdp = pjmedia_sdp_session_clone(call->inv->pool_prov, current_sdp);
2292
2293 pj_assert(med_idx < (int)sdp->media_count);
2294
2295 if (!remove) {
2296 pjsua_acc_config *acc_cfg = &pjsua_var.acc[call->acc_id].cfg;
2297 pj_pool_t *pool = call->inv->pool_prov;
2298 pjmedia_sdp_media *sdp_m;
2299
2300 /* Enabling video */
2301 if (call_med->dir == PJMEDIA_DIR_NONE) {
2302 unsigned i, vid_cnt = 0;
2303
2304 /* Check if vid_cnt in call option needs to be increased */
2305 for (i = 0; i < call->med_cnt; ++i) {
2306 if (call->media[i].type == PJMEDIA_TYPE_VIDEO &&
2307 call->media[i].dir != PJMEDIA_DIR_NONE)
2308 {
2309 ++vid_cnt;
2310 }
2311 }
2312 if (call->opt.vid_cnt <= vid_cnt)
2313 call->opt.vid_cnt++;
2314 }
2315
2316 status = pjsua_call_media_init(call_med, PJMEDIA_TYPE_VIDEO, NULL,
2317 &acc_cfg->rtp_cfg, call->secure_level,
2318 NULL, PJ_FALSE, NULL);
2319 if (status != PJ_SUCCESS)
2320 goto on_error;
2321
2322 /* Init transport media */
2323 if (call_med->tp && call_med->tp_st == PJSUA_MED_TP_IDLE) {
2324 unsigned options = (call_med->enable_rtcp_mux?
2325 PJMEDIA_TPMED_RTCP_MUX: 0);
2326 status = pjmedia_transport_media_create(call_med->tp, pool,
2327 options, NULL,
2328 call_med->idx);
2329 if (status != PJ_SUCCESS)
2330 goto on_error;
2331 }
2332
2333 sdp_m = sdp->media[med_idx];
2334
2335 /* Create new SDP media line if the stream is disabled */
2336 if (sdp->media[med_idx]->desc.port == 0) {
2337 pjmedia_transport_info tpinfo;
2338
2339 /* Get transport address info */
2340 pjmedia_transport_info_init(&tpinfo);
2341 pjmedia_transport_get_info(call_med->tp, &tpinfo);
2342
2343 status = pjmedia_endpt_create_video_sdp(pjsua_var.med_endpt, pool,
2344 &tpinfo.sock_info, 0, &sdp_m);
2345 if (status != PJ_SUCCESS)
2346 goto on_error;
2347 }
2348
2349 {
2350 pjmedia_sdp_attr *a;
2351
2352 /* Remove any direction attributes */
2353 pjmedia_sdp_media_remove_all_attr(sdp_m, "sendrecv");
2354 pjmedia_sdp_media_remove_all_attr(sdp_m, "sendonly");
2355 pjmedia_sdp_media_remove_all_attr(sdp_m, "recvonly");
2356 pjmedia_sdp_media_remove_all_attr(sdp_m, "inactive");
2357
2358 /* Update media direction */
2359 if (dir == PJMEDIA_DIR_ENCODING_DECODING)
2360 a = pjmedia_sdp_attr_create(pool, "sendrecv", NULL);
2361 else if (dir == PJMEDIA_DIR_ENCODING)
2362 a = pjmedia_sdp_attr_create(pool, "sendonly", NULL);
2363 else if (dir == PJMEDIA_DIR_DECODING)
2364 a = pjmedia_sdp_attr_create(pool, "recvonly", NULL);
2365 else
2366 a = pjmedia_sdp_attr_create(pool, "inactive", NULL);
2367
2368 pjmedia_sdp_media_add_attr(sdp_m, a);
2369 }
2370
2371 sdp->media[med_idx] = sdp_m;
2372
2373 if (call_med->dir == PJMEDIA_DIR_NONE && call_med->tp) {
2374 /* Update SDP media line by media transport */
2375 status = pjmedia_transport_encode_sdp(call_med->tp, pool,
2376 sdp, NULL, call_med->idx);
2377 if (status != PJ_SUCCESS)
2378 goto on_error;
2379 }
2380
2381 on_error:
2382 if (status != PJ_SUCCESS) {
2383 /* Revert back provisional media. */
2384 pjsua_media_prov_revert(call->index);
2385
2386 return status;
2387 }
2388
2389 } else {
2390
2391 pj_pool_t *pool = call->inv->pool_prov;
2392
2393 /* Mark media transport to disabled */
2394 // Don't close this here, as SDP negotiation has not been
2395 // done and stream may be still active.
2396 pjsua_set_media_tp_state(call_med, PJSUA_MED_TP_DISABLED);
2397
2398 /* Deactivate the stream */
2399 pjmedia_sdp_media_deactivate(pool, sdp->media[med_idx]);
2400
2401 call->opt.vid_cnt--;
2402 }
2403
2404 status = call_reoffer_sdp(call->index, sdp);
2405 if (status != PJ_SUCCESS)
2406 return status;
2407
2408 return PJ_SUCCESS;
2409 }
2410
2411
2412 /* Change capture device of a video stream in a call */
2413 static pj_status_t call_change_cap_dev(pjsua_call *call,
2414 int med_idx,
2415 pjmedia_vid_dev_index cap_dev)
2416 {
2417 pjsua_call_media *call_med;
2418 pjmedia_vid_dev_stream *old_dev;
2419 pjmedia_vid_dev_switch_param switch_prm;
2420 pjmedia_vid_dev_info info;
2421 pjsua_vid_win *w, *new_w = NULL;
2422 pjsua_vid_win_id wid, new_wid;
2423 pjmedia_port *media_port;
2424 pj_status_t status;
2425
2426 /* Verify and normalize media index */
2427 if (med_idx == -1) {
2428 int first_active;
2429
2430 call_get_vid_strm_info(call, &first_active, NULL, NULL, NULL);
2431 if (first_active == -1)
2432 return PJ_ENOTFOUND;
2433
2434 med_idx = first_active;
2435 }
2436
2437 call_med = &call->media[med_idx];
2438
2439 /* Verify if the stream media type is video */
2440 if (call_med->type != PJMEDIA_TYPE_VIDEO)
2441 return PJ_EINVAL;
2442
2443 /* Verify the capture device */
2444 status = pjmedia_vid_dev_get_info(cap_dev, &info);
2445 if (status != PJ_SUCCESS || (info.dir & PJMEDIA_DIR_CAPTURE) == 0)
2446 return PJ_EINVAL;
2447
2448 /* The specified capture device is being used already */
2449 if (call_med->strm.v.cap_dev == cap_dev)
2450 return PJ_SUCCESS;
2451
2452 /* == Apply the new capture device == */
2453 PJSUA_LOCK();
2454
2455 /* If media does not have active preview, simply set capture device ID */
2456 if (call_med->strm.v.cap_win_id == PJSUA_INVALID_ID) {
2457 call_med->strm.v.cap_dev = cap_dev;
2458
2459 /* That's it */
2460 goto on_sync_and_return;
2461 }
2462
2463 wid = call_med->strm.v.cap_win_id;
2464 w = &pjsua_var.win[wid];
2465 pj_assert(w->type == PJSUA_WND_TYPE_PREVIEW && w->vp_cap);
2466
2467 /* If the old device supports fast switching, then that's excellent! */
2468 old_dev = pjmedia_vid_port_get_stream(w->vp_cap);
2469 pjmedia_vid_dev_switch_param_default(&switch_prm);
2470 switch_prm.target_id = cap_dev;
2471 status = pjmedia_vid_dev_stream_set_cap(old_dev,
2472 PJMEDIA_VID_DEV_CAP_SWITCH,
2473 &switch_prm);
2474 if (status == PJ_SUCCESS) {
2475 w->preview_cap_id = cap_dev;
2476 call_med->strm.v.cap_dev = cap_dev;
2477
2478 /* Yay, change capturer done! Now return */
2479 goto on_sync_and_return;
2480 }
2481
2482 /* Oh no, it doesn't support fast switching. Do normal change then,
2483 * i.e: remove the old and create a new capture.
2484 */
2485 status = pjmedia_vid_stream_get_port(call_med->strm.v.stream,
2486 PJMEDIA_DIR_ENCODING, &media_port);
2487 if (status != PJ_SUCCESS) {
2488 PJSUA_UNLOCK();
2489 return status;
2490 }
2491
2492 media_event_unsubscribe(NULL, &call_media_on_event, call_med, w->vp_cap);
2493
2494 /* Disconnect the old capture device to stream encoding port */
2495 status = pjsua_vid_conf_disconnect(w->cap_slot,
2496 call_med->strm.v.strm_enc_slot);
2497 if (status != PJ_SUCCESS) {
2498 PJSUA_UNLOCK();
2499 return status;
2500 }
2501
2502
2503 /* = Attach stream port to the new capture device = */
2504
2505 /* Note: calling pjsua_vid_preview_get_win() even though
2506 * create_vid_win() will automatically create the window
2507 * if it doesn't exist, because create_vid_win() will modify
2508 * existing window SHOW/HIDE value.
2509 */
2510 new_wid = vid_preview_get_win(cap_dev, PJ_FALSE);
2511 if (new_wid == PJSUA_INVALID_ID) {
2512 pjsua_acc *acc = &pjsua_var.acc[call_med->call->acc_id];
2513
2514 /* Create preview video window */
2515 status = create_vid_win(PJSUA_WND_TYPE_PREVIEW,
2516 &media_port->info.fmt,
2517 call_med->strm.v.rdr_dev,
2518 cap_dev,
2519 PJSUA_HIDE_WINDOW,
2520 acc->cfg.vid_wnd_flags,
2521 NULL,
2522 &new_wid);
2523 if (status != PJ_SUCCESS)
2524 goto on_error;
2525 }
2526
2527 inc_vid_win(new_wid);
2528 new_w = &pjsua_var.win[new_wid];
2529
2530 if (new_w->vp_rend) {
2531 /* Start renderer */
2532 status = pjmedia_vid_port_start(new_w->vp_rend);
2533 if (status != PJ_SUCCESS)
2534 goto on_error;
2535 }
2536
2537 #if ENABLE_EVENT
2538 pjmedia_event_subscribe(NULL, &call_media_on_event,
2539 call_med, new_w->vp_cap);
2540 #endif
2541
2542 /* Start capturer */
2543 if (!pjmedia_vid_port_is_running(new_w->vp_cap)) {
2544 status = pjmedia_vid_port_start(new_w->vp_cap);
2545 if (status != PJ_SUCCESS)
2546 goto on_error;
2547 }
2548
2549 /* Connect capturer to stream encoding port (via conf) */
2550 status = pjsua_vid_conf_connect(new_w->cap_slot,
2551 call_med->strm.v.strm_enc_slot,
2552 NULL);
2553 if (status != PJ_SUCCESS)
2554 goto on_error;
2555
2556 /* Finally */
2557 call_med->strm.v.cap_dev = cap_dev;
2558 call_med->strm.v.cap_win_id = new_wid;
2559 dec_vid_win(wid);
2560
2561 on_sync_and_return:
2562
2563 /* Sync provisional media from call media */
2564 pj_memcpy(&call->media_prov[med_idx], call_med, sizeof(call->media[0]));
2565
2566 PJSUA_UNLOCK();
2567
2568 return PJ_SUCCESS;
2569
2570 on_error:
2571 PJ_PERROR(4,(THIS_FILE, status,
2572 "Call %d: error changing capture device to %d",
2573 call->index, cap_dev));
2574
2575 if (new_w) {
2576 /* Unsubscribe, just in case */
2577 media_event_unsubscribe(NULL, &call_media_on_event, call_med,
2578 new_w->vp_cap);
2579
2580 /* Release the new capturer */
2581 dec_vid_win(new_wid);
2582 }
2583
2584 /* Revert back to the old capturer */
2585 status = pjsua_vid_conf_connect(w->cap_slot,
2586 call_med->strm.v.strm_enc_slot, NULL);
2587 if (status != PJ_SUCCESS) {
2588 PJSUA_UNLOCK();
2589 return status;
2590 }
2591
2592 #if ENABLE_EVENT
2593 /* Resubscribe */
2594 pjmedia_event_subscribe(NULL, &call_media_on_event,
2595 call_med, w->vp_cap);
2596 #endif
2597
2598 PJSUA_UNLOCK();
2599
2600 return status;
2601 }
2602
2603
2604 /* Start/stop transmitting video stream in a call */
2605 static pj_status_t call_set_tx_video(pjsua_call *call,
2606 int med_idx,
2607 pj_bool_t enable)
2608 {
2609 pjsua_call_media *call_med;
2610 pj_status_t status;
2611
2612 /* Verify and normalize media index */
2613 if (med_idx == -1) {
2614 int first_active;
2615
2616 call_get_vid_strm_info(call, &first_active, NULL, NULL, NULL);
2617 if (first_active == -1)
2618 return PJ_ENOTFOUND;
2619
2620 med_idx = first_active;
2621 }
2622
2623 call_med = &call->media[med_idx];
2624
2625 /* Verify if the stream is transmitting video */
2626 if (call_med->type != PJMEDIA_TYPE_VIDEO ||
2627 (enable && (call_med->dir & PJMEDIA_DIR_ENCODING) == 0))
2628 {
2629 return PJ_EINVAL;
2630 }
2631
2632 if (enable) {
2633 if (call_med->strm.v.cap_win_id == PJSUA_INVALID_ID) {
2634 /* Setup the video capture first */
2635 status = setup_vid_capture(call_med);
2636 if (status != PJ_SUCCESS)
2637 return status;
2638 }
2639
2640 /* Resume stream in encoding direction */
2641 status = pjmedia_vid_stream_resume(call_med->strm.v.stream,
2642 PJMEDIA_DIR_ENCODING);
2643 } else {
2644 pjsua_vid_win_id wid;
2645 pjsua_vid_win *w;
2646
2647 /* Pause stream in encoding direction */
2648 status = pjmedia_vid_stream_pause( call_med->strm.v.stream,
2649 PJMEDIA_DIR_ENCODING);
2650
2651 PJSUA_LOCK();
2652
2653 wid = vid_preview_get_win(call_med->strm.v.cap_dev, PJ_FALSE);
2654 if (wid != PJSUA_INVALID_ID) {
2655 w = &pjsua_var.win[wid];
2656
2657 /* Unsubscribe event */
2658 media_event_unsubscribe(NULL, &call_media_on_event, call_med,
2659 w->vp_cap);
2660
2661 /* Disconnect from video conference */
2662 pjsua_vid_conf_disconnect(w->cap_slot,
2663 call_med->strm.v.strm_enc_slot);
2664
2665 /* Decrement ref count of the video window */
2666 dec_vid_win(call_med->strm.v.cap_win_id);
2667 call_med->strm.v.cap_win_id = PJSUA_INVALID_ID;
2668 }
2669
2670 PJSUA_UNLOCK();
2671 }
2672
2673 /* Sync provisional media from call media */
2674 pj_memcpy(&call->media_prov[med_idx], call_med, sizeof(call->media[0]));
2675
2676 return status;
2677 }
2678
2679
2680 static pj_status_t call_send_vid_keyframe(pjsua_call *call,
2681 int med_idx)
2682 {
2683 pjsua_call_media *call_med;
2684
2685 /* Verify and normalize media index */
2686 if (med_idx == -1) {
2687 int first_active;
2688
2689 call_get_vid_strm_info(call, &first_active, NULL, NULL, NULL);
2690 if (first_active == -1)
2691 return PJ_ENOTFOUND;
2692
2693 med_idx = first_active;
2694 }
2695
2696 call_med = &call->media[med_idx];
2697
2698 /* Verify media type and stream instance. */
2699 if (call_med->type != PJMEDIA_TYPE_VIDEO || !call_med->strm.v.stream)
2700 return PJ_EINVAL;
2701
2702 return pjmedia_vid_stream_send_keyframe(call_med->strm.v.stream);
2703 }
2704
2705
2706 /*
2707 * Start, stop, and/or manipulate video transmission for the specified call.
2708 */
2709 PJ_DEF(pj_status_t) pjsua_call_set_vid_strm (
2710 pjsua_call_id call_id,
2711 pjsua_call_vid_strm_op op,
2712 const pjsua_call_vid_strm_op_param *param)
2713 {
2714 pjsua_call *call;
2715 pjsip_dialog *dlg = NULL;
2716 pjsua_call_vid_strm_op_param param_;
2717 pj_status_t status;
2718
2719 PJ_ASSERT_RETURN(call_id>=0 && call_id<(int)pjsua_var.ua_cfg.max_calls,
2720 PJ_EINVAL);
2721 PJ_ASSERT_RETURN(op != PJSUA_CALL_VID_STRM_NO_OP, PJ_EINVAL);
2722
2723 PJ_LOG(4,(THIS_FILE, "Call %d: set video stream, op=%d",
2724 call_id, op));
2725 pj_log_push_indent();
2726
2727 status = acquire_call("pjsua_call_set_vid_strm()", call_id, &call, &dlg);
2728 if (status != PJ_SUCCESS)
2729 goto on_return;
2730
2731 if (param) {
2732 param_ = *param;
2733 } else {
2734 pjsua_call_vid_strm_op_param_default(&param_);
2735 }
2736
2737 /* If set to PJMEDIA_VID_DEFAULT_CAPTURE_DEV, replace it with
2738 * account default video capture device.
2739 */
2740 if (param_.cap_dev == PJMEDIA_VID_DEFAULT_CAPTURE_DEV) {
2741 pjsua_acc_config *acc_cfg = &pjsua_var.acc[call->acc_id].cfg;
2742 param_.cap_dev = acc_cfg->vid_cap_dev;
2743
2744 /* If the account default video capture device is
2745 * PJMEDIA_VID_DEFAULT_CAPTURE_DEV, replace it with
2746 * global default video capture device.
2747 */
2748 if (param_.cap_dev == PJMEDIA_VID_DEFAULT_CAPTURE_DEV) {
2749 pjmedia_vid_dev_info info;
2750 status = pjmedia_vid_dev_get_info(param_.cap_dev, &info);
2751 if (status != PJ_SUCCESS)
2752 goto on_return;
2753 pj_assert(info.dir == PJMEDIA_DIR_CAPTURE);
2754 param_.cap_dev = info.id;
2755 }
2756 }
2757
2758 switch (op) {
2759 case PJSUA_CALL_VID_STRM_ADD:
2760 status = call_add_video(call, param_.cap_dev, param_.dir);
2761 break;
2762 case PJSUA_CALL_VID_STRM_REMOVE:
2763 status = call_modify_video(call, param_.med_idx, PJMEDIA_DIR_NONE,
2764 PJ_TRUE);
2765 break;
2766 case PJSUA_CALL_VID_STRM_CHANGE_DIR:
2767 status = call_modify_video(call, param_.med_idx, param_.dir, PJ_FALSE);
2768 break;
2769 case PJSUA_CALL_VID_STRM_CHANGE_CAP_DEV:
2770 status = call_change_cap_dev(call, param_.med_idx, param_.cap_dev);
2771 break;
2772 case PJSUA_CALL_VID_STRM_START_TRANSMIT:
2773 status = call_set_tx_video(call, param_.med_idx, PJ_TRUE);
2774 break;
2775 case PJSUA_CALL_VID_STRM_STOP_TRANSMIT:
2776 status = call_set_tx_video(call, param_.med_idx, PJ_FALSE);
2777 break;
2778 case PJSUA_CALL_VID_STRM_SEND_KEYFRAME:
2779 status = call_send_vid_keyframe(call, param_.med_idx);
2780 break;
2781 default:
2782 status = PJ_EINVALIDOP;
2783 break;
2784 }
2785
2786 on_return:
2787 if (dlg) pjsip_dlg_dec_lock(dlg);
2788 pj_log_pop_indent();
2789 return status;
2790 }
2791
2792
2793 /*
2794 * Modify video stream's codec parameters.
2795 */
2796 PJ_DEF(pj_status_t)
2797 pjsua_call_vid_stream_modify_codec_param(pjsua_call_id call_id,
2798 int med_idx,
2799 const pjmedia_vid_codec_param *param)
2800 {
2801 pjsua_call *call;
2802 pjsua_call_media *call_med;
2803 pj_status_t status;
2804
2805 PJ_ASSERT_RETURN(call_id>=0 && call_id<(int)pjsua_var.ua_cfg.max_calls &&
2806 med_idx>=0 &&
2807 med_idx<(int)pjsua_var.calls[call_id].med_cnt && param,
2808 PJ_EINVAL);
2809
2810 PJSUA_LOCK();
2811
2812 /* Verify media index */
2813 call = &pjsua_var.calls[call_id];
2814
2815 /* Verify if the media is audio */
2816 call_med = &call->media[med_idx];
2817 if (call_med->type != PJMEDIA_TYPE_VIDEO || !call_med->strm.v.stream) {
2818 PJSUA_UNLOCK();
2819 return PJ_EINVALIDOP;
2820 }
2821
2822 status = pjmedia_vid_stream_modify_codec_param(call_med->strm.v.stream,
2823 param);
2824
2825 PJSUA_UNLOCK();
2826 return status;
2827 }
2828
2829
2830 /*
2831 * Get the media stream index of the default video stream in the call.
2832 */
2833 PJ_DEF(int) pjsua_call_get_vid_stream_idx(pjsua_call_id call_id)
2834 {
2835 pjsua_call *call;
2836 int first_active, first_inactive;
2837
2838 PJ_ASSERT_RETURN(call_id>=0 && call_id<(int)pjsua_var.ua_cfg.max_calls,
2839 PJ_EINVAL);
2840
2841 PJSUA_LOCK();
2842 call = &pjsua_var.calls[call_id];
2843 call_get_vid_strm_info(call, &first_active, &first_inactive, NULL, NULL);
2844 PJSUA_UNLOCK();
2845
2846 if (first_active == -1)
2847 return first_inactive;
2848
2849 return first_active;
2850 }
2851
2852
2853 /*
2854 * Determine if video stream for the specified call is currently running
2855 * for the specified direction.
2856 */
2857 PJ_DEF(pj_bool_t) pjsua_call_vid_stream_is_running( pjsua_call_id call_id,
2858 int med_idx,
2859 pjmedia_dir dir)
2860 {
2861 pjsua_call *call;
2862 pjsua_call_media *call_med;
2863
2864 PJ_ASSERT_RETURN(call_id>=0 && call_id<(int)pjsua_var.ua_cfg.max_calls,
2865 PJ_EINVAL);
2866
2867 /* Verify and normalize media index */
2868 if (med_idx == -1) {
2869 med_idx = pjsua_call_get_vid_stream_idx(call_id);
2870 }
2871
2872 call = &pjsua_var.calls[call_id];
2873 PJ_ASSERT_RETURN(med_idx >= 0 && med_idx < (int)call->med_cnt, PJ_EINVAL);
2874
2875 call_med = &call->media[med_idx];
2876
2877 /* Verify if the stream is transmitting video */
2878 if (call_med->type != PJMEDIA_TYPE_VIDEO || (call_med->dir & dir) == 0 ||
2879 !call_med->strm.v.stream)
2880 {
2881 return PJ_FALSE;
2882 }
2883
2884 return pjmedia_vid_stream_is_running(call_med->strm.v.stream, dir);
2885 }
2886
2887
2888 /*****************************************************************************
2889 * Video conference
2890 */
2891
2892 /*
2893 * Get current number of active ports in the bridge.
2894 */
2895 PJ_DEF(unsigned) pjsua_vid_conf_get_active_ports(void)
2896 {
2897 return pjmedia_vid_conf_get_port_count(pjsua_var.vid_conf);
2898 }
2899
2900
2901 /*
2902 * Enumerate all video conference ports.
2903 */
2904 PJ_DEF(pj_status_t) pjsua_vid_conf_enum_ports( pjsua_conf_port_id id[],
2905 unsigned *count)
2906 {
2907 return pjmedia_vid_conf_enum_ports(pjsua_var.vid_conf,
2908 (unsigned*)id, count);
2909 }
2910
2911
2912 /*
2913 * Get information about the specified video conference port
2914 */
2915 PJ_DEF(pj_status_t) pjsua_vid_conf_get_port_info(
2916 pjsua_conf_port_id port_id,
2917 pjsua_vid_conf_port_info *info)
2918 {
2919 pjmedia_vid_conf_port_info cinfo;
2920 unsigned i;
2921 pj_status_t status;
2922
2923 PJ_ASSERT_RETURN(port_id >= 0, PJ_EINVAL);
2924
2925 status = pjmedia_vid_conf_get_port_info(pjsua_var.vid_conf,
2926 (unsigned)port_id, &cinfo);
2927 if (status != PJ_SUCCESS)
2928 return status;
2929
2930 pj_bzero(info, sizeof(*info));
2931 info->slot_id = port_id;
2932 info->name = cinfo.name;
2933 pjmedia_format_copy(&info->format, &cinfo.format);
2934
2935 /* Build array of listeners */
2936 info->listener_cnt = cinfo.listener_cnt;
2937 for (i=0; i<cinfo.listener_cnt; ++i) {
2938 info->listeners[i] = cinfo.listener_slots[i];
2939 }
2940
2941 /* Build array of transmitters */
2942 info->transmitter_cnt = cinfo.transmitter_cnt;
2943 for (i=0; i<cinfo.transmitter_cnt; ++i) {
2944 info->transmitters[i] = cinfo.transmitter_slots[i];
2945 }
2946
2947 return PJ_SUCCESS;
2948
2949 }
2950
2951
2952 /*
2953 * Add arbitrary video media port to PJSUA's video conference bridge.
2954 */
2955 PJ_DEF(pj_status_t) pjsua_vid_conf_add_port( pj_pool_t *pool,
2956 pjmedia_port *port,
2957 const void *param,
2958 pjsua_conf_port_id *p_id)
2959 {
2960 pj_status_t status;
2961
2962 PJ_UNUSED_ARG(param);
2963
2964 status = pjmedia_vid_conf_add_port(pjsua_var.vid_conf, pool,
2965 port, NULL, NULL, (unsigned*)p_id);
2966 if (status != PJ_SUCCESS) {
2967 if (p_id)
2968 *p_id = PJSUA_INVALID_ID;
2969 }
2970
2971 return status;
2972 }
2973
2974
2975 /*
2976 * Remove arbitrary slot from the video conference bridge.
2977 */
2978 PJ_DEF(pj_status_t) pjsua_vid_conf_remove_port(pjsua_conf_port_id id)
2979 {
2980 PJ_ASSERT_RETURN(id >= 0, PJ_EINVAL);
2981
2982 return pjmedia_vid_conf_remove_port(pjsua_var.vid_conf, (unsigned)id);
2983 }
2984
2985
2986 /*
2987 * Establish unidirectional video flow from souce to sink.
2988 */
2989 PJ_DEF(pj_status_t) pjsua_vid_conf_connect( pjsua_conf_port_id source,
2990 pjsua_conf_port_id sink,
2991 const void *param)
2992 {
2993 PJ_UNUSED_ARG(param);
2994
2995 PJ_ASSERT_RETURN(source >= 0 && sink >= 0, PJ_EINVAL);
2996
2997 return pjmedia_vid_conf_connect_port(pjsua_var.vid_conf, source, sink,
2998 NULL);
2999 }
3000
3001
3002 /*
3003 * Disconnect video flow from the source to destination port.
3004 */
3005 PJ_DEF(pj_status_t) pjsua_vid_conf_disconnect(pjsua_conf_port_id source,
3006 pjsua_conf_port_id sink)
3007 {
3008 PJ_ASSERT_RETURN(source >= 0 && sink >= 0, PJ_EINVAL);
3009
3010 return pjmedia_vid_conf_disconnect_port(pjsua_var.vid_conf, source, sink);
3011 }
3012
3013
3014 /*
3015 * Update or refresh port states from video port info.
3016 */
3017 PJ_DEF(pj_status_t) pjsua_vid_conf_update_port(pjsua_conf_port_id id)
3018 {
3019 return pjmedia_vid_conf_update_port(pjsua_var.vid_conf, id);
3020 }
3021
3022 PJ_DEF(pj_status_t) pjsua_vid_conf_set_op_cb(pjmedia_vid_conf_op_cb cb)
3023 {
3024 return pjmedia_vid_conf_set_op_cb(pjsua_var.vid_conf, cb);
3025 }
3026
3027 /*
3028 * Get the video window associated with the call.
3029 */
3030 PJ_DEF(pjsua_vid_win_id) pjsua_call_get_vid_win(pjsua_call_id call_id)
3031 {
3032 pjsua_call *call;
3033 pjsua_vid_win_id wid = PJSUA_INVALID_ID;
3034 unsigned i;
3035
3036 PJ_ASSERT_RETURN(call_id>=0 && call_id<(int)pjsua_var.ua_cfg.max_calls,
3037 PJ_EINVAL);
3038
3039 /* Use PJSUA_LOCK() instead of acquire_call():
3040 * https://github.com/pjsip/pjproject/issues/1371
3041 */
3042 PJSUA_LOCK();
3043
3044 if (!pjsua_call_is_active(call_id))
3045 goto on_return;
3046
3047 call = &pjsua_var.calls[call_id];
3048 for (i = 0; i < call->med_cnt; ++i) {
3049 if (call->media[i].type == PJMEDIA_TYPE_VIDEO &&
3050 (call->media[i].dir & PJMEDIA_DIR_DECODING))
3051 {
3052 wid = call->media[i].strm.v.rdr_win_id;
3053 break;
3054 }
3055 }
3056
3057 on_return:
3058 PJSUA_UNLOCK();
3059
3060 return wid;
3061 }
3062
3063
3064 /*
3065 * Get the video conference port identification associated with the call.
3066 */
3067 PJ_DEF(pjsua_conf_port_id) pjsua_call_get_vid_conf_port(
3068 pjsua_call_id call_id,
3069 pjmedia_dir dir)
3070 {
3071 pjsua_call *call;
3072 pjsua_conf_port_id port_id = PJSUA_INVALID_ID;
3073 unsigned i;
3074
3075 PJ_ASSERT_RETURN(call_id>=0 && call_id<(int)pjsua_var.ua_cfg.max_calls,
3076 PJ_EINVAL);
3077 PJ_ASSERT_RETURN(dir==PJMEDIA_DIR_ENCODING || dir==PJMEDIA_DIR_DECODING,
3078 PJ_EINVAL);
3079
3080 /* Use PJSUA_LOCK() instead of acquire_call():
3081 * https://github.com/pjsip/pjproject/issues/1371
3082 */
3083 PJSUA_LOCK();
3084
3085 if (!pjsua_call_is_active(call_id))
3086 goto on_return;
3087
3088 call = &pjsua_var.calls[call_id];
3089 for (i = 0; i < call->med_cnt; ++i) {
3090 if (call->media[i].type == PJMEDIA_TYPE_VIDEO &&
3091 (call->media[i].dir & dir))
3092 {
3093 port_id = (dir==PJMEDIA_DIR_ENCODING)?
3094 call->media[i].strm.v.strm_enc_slot :
3095 call->media[i].strm.v.strm_dec_slot;
3096 break;
3097 }
3098 }
3099
3100 on_return:
3101 PJSUA_UNLOCK();
3102
3103 return port_id;
3104 }
3105
3106 /*****************************************************************************
3107 * AVI player.
3108 */
3109 void pjsua_reset_avi_player_data(pjsua_avi_player_id id)
3110 {
3111 unsigned i = 0;
3112
3113 pjsua_var.avi_player[id].vid_dev_id = PJMEDIA_VID_INVALID_DEV;
3114 pjsua_var.avi_player[id].vid_cnt = 0;
3115 pjsua_var.avi_player[id].aud_cnt = 0;
3116 pjsua_var.avi_player[id].avi_streams = NULL;
3117 for (; i < PJ_ARRAY_SIZE(pjsua_var.avi_player[id].port); ++i) {
3118 pjsua_var.avi_player[id].port[i] = NULL;
3119 pjsua_var.avi_player[id].slot[i] = PJSUA_INVALID_ID;
3120 pjsua_var.avi_player[id].type[i] = PJMEDIA_TYPE_NONE;
3121 }
3122 }
3123
3124 static void add_port_to_conf(pjsua_avi_player_id avi_id,
3125 const pj_str_t *med_name,
3126 pjmedia_type med_type,
3127 unsigned start_idx,
3128 unsigned *num_stream)
3129 {
3130 unsigned strm_cnt, strm_idx, num_strm = 0;
3131 pjmedia_avi_streams *streams = pjsua_var.avi_player[avi_id].avi_streams;
3132 pj_pool_t *pool = pjsua_var.avi_player[avi_id].pool;
3133
3134 strm_cnt = pjmedia_avi_streams_get_num_streams_by_media(streams,
3135 med_type);
3136
3137 for (strm_idx = 0; strm_idx < strm_cnt; ++strm_idx) {
3138 pjmedia_port *port;
3139 pjmedia_format *fmt;
3140 pjsua_conf_port_id slot = 0;
3141 pj_status_t status = PJ_EINVAL;
3142
3143 port = (pjmedia_port*)pjmedia_avi_streams_get_stream_by_media(streams,
3144 strm_idx, med_type);
3145 fmt = &port->info.fmt;
3146
3147 if (med_type == PJMEDIA_TYPE_VIDEO) {
3148 /* Only supports one video stream */
3149 if (pjsua_var.avi_player[avi_id].vid_cnt > 0) {
3150 *num_stream = num_strm;
3151 return;
3152 }
3153
3154 status = pjsua_vid_conf_add_port(pool, port,
3155 NULL, &slot);
3156 }
3157 else if (med_type == PJMEDIA_TYPE_AUDIO) {
3158 if (fmt->id == PJMEDIA_FORMAT_PCM) {
3159 status = pjsua_conf_add_port(pool, port, &slot);
3160 } else {
3161 status = PJ_ENOTSUP;
3162 char fmt_name[5];
3163
3164 pjmedia_fourcc_name(fmt->id, fmt_name);
3165 PJ_PERROR(4, (THIS_FILE, status,
3166 "AVI %.*s: audio ignored, format=%s",
3167 (int)med_name->slen, med_name->ptr,
3168 fmt_name));
3169 }
3170 }
3171 if (status == PJ_SUCCESS) {
3172 PJ_LOG(4, (THIS_FILE,
3173 "AVI %.*s: %s added to slot %d",
3174 (int)med_name->slen, med_name->ptr,
3175 med_type == PJMEDIA_TYPE_VIDEO ? "video":"audio",
3176 slot));
3177 pjsua_var.avi_player[avi_id].slot[start_idx + num_strm] = slot;
3178 pjsua_var.avi_player[avi_id].port[start_idx + num_strm] = port;
3179 pjsua_var.avi_player[avi_id].type[start_idx + num_strm] = med_type;
3180 num_strm++;
3181 }
3182 }
3183 *num_stream = num_strm;
3184 }
3185
3186 PJ_DEF(pj_status_t) pjsua_avi_player_create(const pj_str_t *filename,
3187 pjsua_avi_player_id *id)
3188 {
3189 pj_status_t status = PJ_SUCCESS;
3190 pj_pool_t *pool = NULL;
3191 pjmedia_avi_dev_param avdp;
3192 pjmedia_vid_dev_index avid;
3193 unsigned avi_id;
3194
3195 if (filename->slen >= PJ_MAXPATH)
3196 return PJ_ENAMETOOLONG;
3197 if (filename->slen < 4)
3198 return PJ_EINVALIDOP;
3199
3200 PJ_LOG(4, (THIS_FILE, "Creating avi file player: %.*s..",
3201 (int)filename->slen, filename->ptr));
3202 pj_log_push_indent();
3203
3204 PJSUA_LOCK();
3205
3206 if (pjsua_var.avi_player_cnt >= PJ_ARRAY_SIZE(pjsua_var.avi_player)) {
3207 status = PJ_ETOOMANY;
3208 goto on_return;
3209 }
3210
3211 for (avi_id = 0; avi_id < PJ_ARRAY_SIZE(pjsua_var.avi_player); ++avi_id) {
3212 if (pjsua_var.avi_player[avi_id].avi_streams == NULL)
3213 break;
3214 }
3215
3216 if (avi_id == PJ_ARRAY_SIZE(pjsua_var.avi_player)) {
3217 /* This is unexpected */
3218 pj_assert(0);
3219 status = PJ_EBUG;
3220 goto on_return;
3221 }
3222
3223 pjmedia_avi_dev_param_default(&avdp);
3224 avdp.path = *filename;
3225
3226 pool = pjsua_pool_create(pjsua_get_basename(filename->ptr,
3227 (unsigned)filename->slen), 1000, 1000);
3228 if (!pool) {
3229 status = PJ_ENOMEM;
3230 goto on_return;
3231 }
3232 pjsua_var.avi_player[avi_id].pool = pool;
3233
3234 if (pjsua_var.avi_factory == NULL) {
3235 status = pjmedia_avi_dev_create_factory(pjsua_get_pool_factory(),
3236 PJSUA_MAX_AVI_PLAYERS,
3237 &pjsua_var.avi_factory);
3238 if (status != PJ_SUCCESS) {
3239 PJ_PERROR(1, (THIS_FILE, status, "Error creating AVI factory"));
3240 goto on_return;
3241 }
3242 }
3243
3244 status = pjmedia_avi_dev_alloc(pjsua_var.avi_factory, &avdp, &avid);
3245 if (status != PJ_SUCCESS) {
3246 PJ_PERROR(1, (THIS_FILE, status,
3247 "Error creating AVI player for %.*s",
3248 (int)avdp.path.slen, avdp.path.ptr));
3249 goto on_return;
3250 }
3251 pjsua_reset_avi_player_data(avi_id);
3252
3253 PJ_LOG(4, (THIS_FILE, "AVI player %.*s created, id=%d, dev_id=%d",
3254 (int)avdp.title.slen, avdp.title.ptr, avi_id, avid));
3255
3256 pjsua_var.avi_player[avi_id].vid_dev_id = avid;
3257 pjsua_var.avi_player[avi_id].avi_streams = avdp.avi_streams;
3258 add_port_to_conf(avi_id, &avdp.title, PJMEDIA_TYPE_VIDEO, 0,
3259 &pjsua_var.avi_player[avi_id].vid_cnt);
3260 add_port_to_conf(avi_id, &avdp.title, PJMEDIA_TYPE_AUDIO,
3261 pjsua_var.avi_player[avi_id].vid_cnt,
3262 &pjsua_var.avi_player[avi_id].aud_cnt);
3263
3264 pjsua_var.avi_player_cnt++;
3265 *id = avi_id;
3266
3267 on_return:
3268 PJSUA_UNLOCK();
3269 if (pool) pj_pool_release(pool);
3270 pj_log_pop_indent();
3271 return status;
3272 }
3273
3274 PJ_DEF(pjmedia_vid_dev_index) pjsua_avi_player_get_vid_dev(
3275 pjsua_avi_player_id id)
3276 {
3277 if (id >= (int)PJ_ARRAY_SIZE(pjsua_var.avi_player))
3278 return PJMEDIA_VID_INVALID_DEV;
3279
3280 return pjsua_var.avi_player[id].vid_dev_id;
3281 }
3282
3283 PJ_DEF(unsigned) pjsua_avi_player_get_num_stream(pjsua_avi_player_id id,
3284 pjmedia_type strm_type)
3285 {
3286 PJ_ASSERT_RETURN(id >= 0 && id < (int)PJ_ARRAY_SIZE(pjsua_var.avi_player),
3287 0);
3288
3289 switch (strm_type) {
3290 case PJMEDIA_TYPE_AUDIO:
3291 return pjsua_var.avi_player[id].aud_cnt;
3292 break;
3293 case PJMEDIA_TYPE_VIDEO:
3294 return pjsua_var.avi_player[id].vid_cnt;
3295 break;
3296 default:
3297 return 0;
3298 }
3299 }
3300
3301 #define AUD_IDX(id, strm_idx) (pjsua_var.avi_player[id].vid_cnt+strm_idx)
3302
3303 PJ_DEF(pjsua_conf_port_id) pjsua_avi_player_get_conf_port(
3304 pjsua_avi_player_id id,
3305 pjmedia_type strm_type,
3306 unsigned strm_idx)
3307 {
3308 PJ_ASSERT_RETURN(id >= 0 && id < (int)PJ_ARRAY_SIZE(pjsua_var.avi_player),
3309 PJSUA_INVALID_ID);
3310 PJ_ASSERT_RETURN(strm_idx < PJSUA_MAX_AVI_NUM_STREAMS, PJSUA_INVALID_ID);
3311
3312 switch (strm_type) {
3313 case PJMEDIA_TYPE_AUDIO:
3314 if (pjsua_var.avi_player[id].aud_cnt)
3315 return pjsua_var.avi_player[id].slot[AUD_IDX(id, strm_idx)];
3316 else
3317 return PJSUA_INVALID_ID;
3318 case PJMEDIA_TYPE_VIDEO:
3319 if (pjsua_var.avi_player[id].vid_cnt)
3320 return pjsua_var.avi_player[id].slot[strm_idx];
3321 else
3322 return PJSUA_INVALID_ID;
3323 default:
3324 return PJSUA_INVALID_ID;
3325 }
3326 }
3327
3328 PJ_DEF(pj_status_t) pjsua_avi_player_get_port(pjsua_avi_player_id id,
3329 pjmedia_type strm_type,
3330 unsigned strm_idx,
3331 pjmedia_port **p_port)
3332 {
3333 pj_status_t status = PJ_SUCCESS;
3334 PJ_ASSERT_RETURN(id >= 0 && id < (int)PJ_ARRAY_SIZE(pjsua_var.avi_player),
3335 PJ_EINVAL);
3336 PJ_ASSERT_RETURN(strm_idx < PJSUA_MAX_AVI_NUM_STREAMS, PJSUA_INVALID_ID);
3337
3338 switch (strm_type) {
3339 case PJMEDIA_TYPE_AUDIO:
3340 PJ_ASSERT_RETURN(strm_idx < pjsua_var.avi_player[id].aud_cnt,
3341 PJSUA_INVALID_ID);
3342
3343 *p_port = pjsua_var.avi_player[id].port[AUD_IDX(id, strm_idx)];
3344 break;
3345 case PJMEDIA_TYPE_VIDEO:
3346 PJ_ASSERT_RETURN(strm_idx < pjsua_var.avi_player[id].vid_cnt,
3347 PJSUA_INVALID_ID);
3348
3349 *p_port = pjsua_var.avi_player[id].port[strm_idx];
3350 break;
3351 default:
3352 status = PJ_ENOTFOUND;
3353 }
3354 return status;
3355 }
3356
3357 PJ_DECL(pj_status_t) pjsua_avi_player_destroy(pjsua_avi_player_id id)
3358 {
3359 PJ_ASSERT_RETURN(id >= 0 && id < (int)PJ_ARRAY_SIZE(pjsua_var.avi_player),
3360 PJ_EINVAL);
3361
3362 PJ_LOG(4, (THIS_FILE, "Destroying avi file player %d..", id));
3363 pj_log_push_indent();
3364
3365 PJSUA_LOCK();
3366
3367 if (pjsua_var.avi_player[id].avi_streams != NULL) {
3368 unsigned i = 0;
3369 pj_status_t status;
3370
3371 for (; i < PJ_ARRAY_SIZE(pjsua_var.avi_player[id].port); ++i) {
3372 if (pjsua_var.avi_player[id].slot[i] == PJSUA_INVALID_ID)
3373 continue;
3374
3375 if (pjsua_var.avi_player[id].type[i] == PJMEDIA_TYPE_VIDEO) {
3376 if (pjsua_var.vid_conf) {
3377 pjsua_vid_conf_remove_port(pjsua_var.avi_player[id].slot[i]);
3378 --pjsua_var.avi_player[id].vid_cnt;
3379 }
3380 } else if (pjsua_var.avi_player[id].type[i] == PJMEDIA_TYPE_AUDIO) {
3381 if (pjsua_var.mconf) {
3382 pjsua_conf_remove_port(pjsua_var.avi_player[id].slot[i]);
3383 --pjsua_var.avi_player[id].aud_cnt;
3384 }
3385 }
3386 }
3387 // All of the video/audio ports will be destroyed here.
3388 status = pjmedia_avi_dev_free(pjsua_var.avi_player[id].vid_dev_id);
3389 if (status != PJ_SUCCESS) {
3390 PJ_PERROR(4, (THIS_FILE, status,
3391 "Fail destroying avi file player %d", id));
3392
3393 PJSUA_UNLOCK();
3394 pj_log_pop_indent();
3395 return status;
3396 }
3397
3398 pjsua_reset_avi_player_data(id);
3399 pjsua_var.avi_player_cnt--;
3400 }
3401
3402 PJSUA_UNLOCK();
3403 pj_log_pop_indent();
3404
3405 return PJ_SUCCESS;
3406 }
3407
3408 /*****************************************************************************
3409 * AVI recordder.
3410 */
3411
3412 #define AVI_REC_FILE_MAX_SIZE 5 /* 5Mb */
3413 #define AVI_REC_FILE_FMT PJMEDIA_FORMAT_I420
3414 #define AVI_REC_FILE_WIDTH 320
3415 #define AVI_REC_FILE_HEIGHT 240
3416 #define AVI_REC_FILE_FPS_NUM 15
3417 #define AVI_REC_FILE_FPS_DENUM 1
3418
3419 static void avi_writer_cb(pjmedia_avi_streams *streams,
3420 void *usr_data)
3421 {
3422 pjsua_avi_rec_id id;
3423 PJ_UNUSED_ARG(streams);
3424 id = (pjsua_avi_rec_id)(pj_ssize_t)usr_data;
3425 if (id < 0 || id >= PJ_ARRAY_SIZE(pjsua_var.avi_recorder))
3426 return;
3427
3428 pj_log_push_indent();
3429 if (pjsua_var.avi_recorder[id].cb) {
3430 (*pjsua_var.avi_recorder[id].cb)(id,
3431 pjsua_var.avi_recorder[id].user_data);
3432 }
3433 pj_log_pop_indent();
3434 }
3435
3436 void pjsua_reset_avi_recorder_data(pjsua_avi_rec_id id)
3437 {
3438 pjsua_var.avi_recorder[id].aud_port = NULL;
3439 pjsua_var.avi_recorder[id].vid_port = NULL;
3440 pjsua_var.avi_recorder[id].aud_slot = PJSUA_INVALID_ID;
3441 pjsua_var.avi_recorder[id].vid_slot = PJSUA_INVALID_ID;
3442 pjsua_var.avi_recorder[id].avi_streams = NULL;
3443 pjsua_var.avi_recorder[id].cb = NULL;
3444 pjsua_var.avi_recorder[id].user_data = NULL;
3445 }
3446
3447 PJ_DEF(pj_status_t) pjsua_avi_recorder_create(const pj_str_t *filename,
3448 pj_ssize_t max_size,
3449 const pjmedia_format *vid_fmt,
3450 const pjmedia_format *aud_fmt,
3451 unsigned options,
3452 pjsua_avi_rec_id *id)
3453 {
3454 pjmedia_format fmt[2];
3455 pjmedia_avi_streams *streams;
3456 pj_pool_t *pool = NULL;
3457 pjmedia_port *aviw_port;
3458 pj_status_t status = PJ_SUCCESS;
3459 unsigned avi_id;
3460
3461 /* Filename must present */
3462 PJ_ASSERT_RETURN(filename != NULL, PJ_EINVAL);
3463
3464 if (filename->slen >= PJ_MAXPATH)
3465 return PJ_ENAMETOOLONG;
3466 if (filename->slen < 4)
3467 return PJ_EINVALIDOP;
3468
3469 if (max_size <= 0)
3470 max_size = AVI_REC_FILE_MAX_SIZE * 1000000;
3471
3472 PJ_LOG(4, (THIS_FILE, "Creating avi recorder %.*s..",
3473 (int)filename->slen, filename->ptr));
3474
3475 pj_log_push_indent();
3476
3477 if (pjsua_var.avi_rec_cnt >= PJ_ARRAY_SIZE(pjsua_var.avi_recorder)) {
3478 pj_log_pop_indent();
3479 return PJ_ETOOMANY;
3480 }
3481
3482 PJSUA_LOCK();
3483
3484 for (avi_id = 0; avi_id <
3485 PJ_ARRAY_SIZE(pjsua_var.avi_recorder); ++avi_id)
3486 {
3487 if (pjsua_var.avi_recorder[avi_id].avi_streams == NULL)
3488 break;
3489 }
3490
3491 if (avi_id == PJ_ARRAY_SIZE(pjsua_var.avi_recorder)) {
3492 status = PJ_ETOOMANY;
3493 goto on_return;
3494 }
3495
3496 pool = pjsua_pool_create(pjsua_get_basename(filename->ptr,
3497 (unsigned)filename->slen), 1000, 1000);
3498 if (!pool) {
3499 status = PJ_ENOMEM;
3500 goto on_return;
3501 }
3502 pjsua_var.avi_recorder[avi_id].pool = pool;
3503
3504 if (!vid_fmt) {
3505 pjmedia_format_init_video(&fmt[0], AVI_REC_FILE_FMT,
3506 AVI_REC_FILE_WIDTH, AVI_REC_FILE_HEIGHT,
3507 AVI_REC_FILE_FPS_NUM, AVI_REC_FILE_FPS_DENUM);
3508 } else {
3509 fmt[0] = *vid_fmt;
3510 }
3511
3512 if (!aud_fmt) {
3513 pjmedia_format_init_audio(&fmt[1], PJMEDIA_FORMAT_PCM,
3514 pjsua_var.media_cfg.clock_rate,
3515 pjsua_var.media_cfg.channel_count,
3516 16,
3517 pjsua_var.media_cfg.audio_frame_ptime * 1000,
3518 0, 0);
3519 } else {
3520 fmt[1] = *aud_fmt;
3521 }
3522 status = pjmedia_avi_writer_create_streams(pool, filename->ptr,
3523 (pj_uint32_t)max_size,
3524 2, fmt, options, &streams);
3525 if (status != PJ_SUCCESS)
3526 goto on_return;
3527
3528 aviw_port = (pjmedia_port*)pjmedia_avi_streams_get_stream(streams, 0);
3529 if (aviw_port) {
3530 status = pjsua_vid_conf_add_port(pjsua_var.avi_recorder[avi_id].pool,
3531 aviw_port, NULL,
3532 &pjsua_var.avi_recorder[avi_id].vid_slot);
3533 if (status != PJ_SUCCESS)
3534 goto on_return;
3535
3536 pjsua_var.avi_recorder[avi_id].vid_port = aviw_port;
3537 }
3538
3539 aviw_port = (pjmedia_port*)pjmedia_avi_streams_get_stream(streams, 1);
3540 if (aviw_port) {
3541 status = pjsua_conf_add_port(pjsua_var.avi_recorder[avi_id].pool, aviw_port,
3542 &pjsua_var.avi_recorder[avi_id].aud_slot);
3543
3544 if (status != PJ_SUCCESS)
3545 goto on_return;
3546
3547 pjsua_var.avi_recorder[avi_id].aud_port = aviw_port;
3548 }
3549 pjsua_var.avi_recorder[avi_id].avi_streams = streams;
3550 pjsua_var.avi_rec_cnt++;
3551 *id = avi_id;
3552
3553 PJ_LOG(4, (THIS_FILE, "AVI Recorder created, id=%d", avi_id));
3554
3555 on_return:
3556 PJSUA_UNLOCK();
3557 if (pool) pj_pool_release(pool);
3558 pj_log_pop_indent();
3559 return status;
3560 }
3561
3562 PJ_DEF(pjsua_conf_port_id) pjsua_avi_recorder_get_conf_port(
3563 pjsua_avi_rec_id id,
3564 pjmedia_type strm_type)
3565 {
3566 PJ_ASSERT_RETURN(id >= 0 && id < (int)PJ_ARRAY_SIZE(pjsua_var.avi_recorder),
3567 PJSUA_INVALID_ID);
3568
3569 switch (strm_type) {
3570 case PJMEDIA_TYPE_AUDIO:
3571 return pjsua_var.avi_recorder[id].aud_slot;
3572 case PJMEDIA_TYPE_VIDEO:
3573 return pjsua_var.avi_recorder[id].vid_slot;
3574 default:
3575 return PJSUA_INVALID_ID;
3576 }
3577 }
3578
3579 PJ_DEF(pj_status_t) pjsua_avi_recorder_get_port(pjsua_avi_rec_id id,
3580 pjmedia_type strm_type,
3581 pjmedia_port **p_port)
3582 {
3583 pj_status_t status = PJ_SUCCESS;
3584 PJ_ASSERT_RETURN(id >= 0 && id < (int)PJ_ARRAY_SIZE(pjsua_var.avi_recorder),
3585 PJSUA_INVALID_ID);
3586
3587 switch (strm_type) {
3588 case PJMEDIA_TYPE_AUDIO:
3589 *p_port = pjsua_var.avi_recorder[id].aud_port;
3590 break;
3591 case PJMEDIA_TYPE_VIDEO:
3592 *p_port = pjsua_var.avi_recorder[id].vid_port;
3593 break;
3594 default:
3595 *p_port = NULL;
3596 status = PJ_ENOTFOUND;
3597 break;
3598 }
3599 return status;
3600 }
3601
3602 PJ_DEF(pj_status_t) pjsua_avi_recorder_set_cb(pjsua_avi_rec_id id,
3603 void *user_data,
3604 void(*cb)(pjsua_avi_rec_id rec_id,
3605 void *usr_data))
3606 {
3607 PJ_ASSERT_RETURN(id >= 0 && id < (int)PJ_ARRAY_SIZE(pjsua_var.avi_recorder),
3608 PJSUA_INVALID_ID);
3609
3610 if (pjsua_var.avi_recorder[id].avi_streams) {
3611 pjsua_var.avi_recorder[id].cb = cb;
3612 pjsua_var.avi_recorder[id].user_data = user_data;
3613 pjmedia_avi_streams_set_cb(pjsua_var.avi_recorder[id].avi_streams,
3614 (void*)(intptr_t)id, &avi_writer_cb);
3615 }
3616
3617 return PJ_SUCCESS;
3618 }
3619
3620 PJ_DEF(pj_status_t) pjsua_avi_recorder_destroy(pjsua_avi_rec_id id)
3621 {
3622 PJ_ASSERT_RETURN(id >= 0 && id < (int)PJ_ARRAY_SIZE(pjsua_var.avi_recorder),
3623 PJSUA_INVALID_ID);
3624
3625 PJ_LOG(4, (THIS_FILE, "Destroying avi recorder %d..", id));
3626 pj_log_push_indent();
3627 PJSUA_LOCK();
3628
3629 if (pjsua_var.avi_recorder[id].avi_streams != NULL) {
3630 if (pjsua_var.vid_conf)
3631 pjsua_vid_conf_remove_port(pjsua_var.avi_recorder[id].vid_slot);
3632
3633 if (pjsua_var.avi_recorder[id].vid_port)
3634 pjmedia_port_destroy(pjsua_var.avi_recorder[id].vid_port);
3635
3636 if (pjsua_var.mconf)
3637 pjsua_conf_remove_port(pjsua_var.avi_recorder[id].aud_slot);
3638
3639 if (pjsua_var.avi_recorder[id].aud_port)
3640 pjmedia_port_destroy(pjsua_var.avi_recorder[id].aud_port);
3641
3642 pjsua_reset_avi_recorder_data(id);
3643 pjsua_var.avi_rec_cnt--;
3644 }
3645 PJSUA_UNLOCK();
3646 pj_log_pop_indent();
3647
3648 return PJ_SUCCESS;
3649 }
3650
3651 #endif /* PJSUA_HAS_VIDEO */
3652
3653 #endif /* PJSUA_MEDIA_HAS_PJMEDIA */