blob: 626965b5d0cb56aaf3f529f3a192e1d75772549c [file] [log] [blame]
Benny Prijonoeebe9af2006-06-13 22:57:13 +00001/* $Id$ */
2/*
Nanang Izzuddina62ffc92011-05-05 06:14:19 +00003 * Copyright (C) 2008-2011 Teluu Inc. (http://www.teluu.com)
Benny Prijono32177c02008-06-20 22:44:47 +00004 * Copyright (C) 2003-2008 Benny Prijono <benny@prijono.org>
Benny Prijonoeebe9af2006-06-13 22:57:13 +00005 *
6 * This program is free software; you can redistribute it and/or modify
7 * it under the terms of the GNU General Public License as published by
8 * the Free Software Foundation; either version 2 of the License, or
9 * (at your option) any later version.
10 *
11 * This program is distributed in the hope that it will be useful,
12 * but WITHOUT ANY WARRANTY; without even the implied warranty of
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 * GNU General Public License for more details.
15 *
16 * You should have received a copy of the GNU General Public License
17 * along with this program; if not, write to the Free Software
18 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
19 */
20#include <pjsua-lib/pjsua.h>
21#include <pjsua-lib/pjsua_internal.h>
22
23
24#define THIS_FILE "pjsua_media.c"
25
Nanang Izzuddin68559c32008-06-13 17:01:46 +000026#define DEFAULT_RTP_PORT 4000
27
28#define NULL_SND_DEV_ID -99
Benny Prijono80eee892007-11-03 22:43:23 +000029
30#ifndef PJSUA_REQUIRE_CONSECUTIVE_RTCP_PORT
31# define PJSUA_REQUIRE_CONSECUTIVE_RTCP_PORT 0
32#endif
33
Benny Prijonoeebe9af2006-06-13 22:57:13 +000034
Benny Prijonode479562007-03-15 10:23:55 +000035/* Next RTP port to be used */
36static pj_uint16_t next_rtp_port;
Benny Prijonoeebe9af2006-06-13 22:57:13 +000037
Benny Prijonof798e502009-03-09 13:08:16 +000038/* Open sound dev */
Sauw Ming98766c72011-03-11 06:57:24 +000039static pj_status_t open_snd_dev(pjmedia_snd_port_param *param);
Benny Prijonoeebe9af2006-06-13 22:57:13 +000040/* Close existing sound device */
41static void close_snd_dev(void);
Benny Prijonof798e502009-03-09 13:08:16 +000042/* Create audio device param */
43static pj_status_t create_aud_param(pjmedia_aud_param *param,
44 pjmedia_aud_dev_index capture_dev,
45 pjmedia_aud_dev_index playback_dev,
46 unsigned clock_rate,
47 unsigned channel_count,
48 unsigned samples_per_frame,
49 unsigned bits_per_sample);
Benny Prijonoeebe9af2006-06-13 22:57:13 +000050
51
Benny Prijonof76e1392008-06-06 14:51:48 +000052static void pjsua_media_config_dup(pj_pool_t *pool,
53 pjsua_media_config *dst,
54 const pjsua_media_config *src)
55{
56 pj_memcpy(dst, src, sizeof(*src));
57 pj_strdup(pool, &dst->turn_server, &src->turn_server);
58 pj_stun_auth_cred_dup(pool, &dst->turn_auth_cred, &src->turn_auth_cred);
59}
60
Nanang Izzuddin50fae732011-03-22 09:49:23 +000061
Benny Prijonoeebe9af2006-06-13 22:57:13 +000062/**
63 * Init media subsystems.
64 */
65pj_status_t pjsua_media_subsys_init(const pjsua_media_config *cfg)
66{
Benny Prijonoba5926a2007-05-02 11:29:37 +000067 pj_str_t codec_id = {NULL, 0};
Benny Prijono0498d902006-06-19 14:49:14 +000068 unsigned opt;
Benny Prijono35fc1eb2011-07-15 09:51:46 +000069 pjmedia_audio_codec_config codec_cfg;
Benny Prijonoeebe9af2006-06-13 22:57:13 +000070 pj_status_t status;
71
Benny Prijonofc24e692007-01-27 18:31:51 +000072 /* To suppress warning about unused var when all codecs are disabled */
73 PJ_UNUSED_ARG(codec_id);
74
Benny Prijonob90fd382011-09-18 14:59:56 +000075 pj_log_push_indent();
76
Benny Prijonof798e502009-03-09 13:08:16 +000077 /* Specify which audio device settings are save-able */
78 pjsua_var.aud_svmask = 0xFFFFFFFF;
79 /* These are not-settable */
80 pjsua_var.aud_svmask &= ~(PJMEDIA_AUD_DEV_CAP_EXT_FORMAT |
81 PJMEDIA_AUD_DEV_CAP_INPUT_SIGNAL_METER |
82 PJMEDIA_AUD_DEV_CAP_OUTPUT_SIGNAL_METER);
Benny Prijonoe506c8c2009-03-10 13:28:43 +000083 /* EC settings use different API */
84 pjsua_var.aud_svmask &= ~(PJMEDIA_AUD_DEV_CAP_EC |
85 PJMEDIA_AUD_DEV_CAP_EC_TAIL);
Benny Prijonof798e502009-03-09 13:08:16 +000086
Benny Prijonoeebe9af2006-06-13 22:57:13 +000087 /* Copy configuration */
Benny Prijonof76e1392008-06-06 14:51:48 +000088 pjsua_media_config_dup(pjsua_var.pool, &pjsua_var.media_cfg, cfg);
Benny Prijonoeebe9af2006-06-13 22:57:13 +000089
90 /* Normalize configuration */
Benny Prijono50f19b32008-03-11 13:15:43 +000091 if (pjsua_var.media_cfg.snd_clock_rate == 0) {
92 pjsua_var.media_cfg.snd_clock_rate = pjsua_var.media_cfg.clock_rate;
93 }
Benny Prijonoeebe9af2006-06-13 22:57:13 +000094
95 if (pjsua_var.media_cfg.has_ioqueue &&
96 pjsua_var.media_cfg.thread_cnt == 0)
97 {
98 pjsua_var.media_cfg.thread_cnt = 1;
99 }
100
101 if (pjsua_var.media_cfg.max_media_ports < pjsua_var.ua_cfg.max_calls) {
102 pjsua_var.media_cfg.max_media_ports = pjsua_var.ua_cfg.max_calls + 2;
103 }
104
105 /* Create media endpoint. */
106 status = pjmedia_endpt_create(&pjsua_var.cp.factory,
107 pjsua_var.media_cfg.has_ioqueue? NULL :
108 pjsip_endpt_get_ioqueue(pjsua_var.endpt),
109 pjsua_var.media_cfg.thread_cnt,
110 &pjsua_var.med_endpt);
111 if (status != PJ_SUCCESS) {
112 pjsua_perror(THIS_FILE,
113 "Media stack initialization has returned error",
114 status);
Benny Prijonob90fd382011-09-18 14:59:56 +0000115 goto on_error;
Benny Prijonoeebe9af2006-06-13 22:57:13 +0000116 }
117
Benny Prijono35fc1eb2011-07-15 09:51:46 +0000118 /*
119 * Register all codecs
120 */
121 pjmedia_audio_codec_config_default(&codec_cfg);
122 codec_cfg.speex.quality = pjsua_var.media_cfg.quality;
123 codec_cfg.speex.complexity = -1;
124 codec_cfg.ilbc.mode = pjsua_var.media_cfg.ilbc_mode;
Nanang Izzuddin493a8db2008-08-15 13:17:39 +0000125
Nanang Izzuddin81db8c72009-02-05 10:59:14 +0000126#if PJMEDIA_HAS_PASSTHROUGH_CODECS
127 /* Register passthrough codecs */
Nanang Izzuddinabf58db2009-06-30 15:02:06 +0000128 {
129 unsigned aud_idx;
130 unsigned ext_fmt_cnt = 0;
131 pjmedia_format ext_fmts[32];
Nanang Izzuddinabf58db2009-06-30 15:02:06 +0000132
133 /* List extended formats supported by audio devices */
134 for (aud_idx = 0; aud_idx < pjmedia_aud_dev_count(); ++aud_idx) {
135 pjmedia_aud_dev_info aud_info;
136 unsigned i;
137
138 status = pjmedia_aud_dev_get_info(aud_idx, &aud_info);
139 if (status != PJ_SUCCESS) {
140 pjsua_perror(THIS_FILE, "Error querying audio device info",
141 status);
Benny Prijonob90fd382011-09-18 14:59:56 +0000142 goto on_error;
Nanang Izzuddinabf58db2009-06-30 15:02:06 +0000143 }
144
145 /* Collect extended formats supported by this audio device */
146 for (i = 0; i < aud_info.ext_fmt_cnt; ++i) {
147 unsigned j;
148 pj_bool_t is_listed = PJ_FALSE;
149
150 /* See if this extended format is already in the list */
151 for (j = 0; j < ext_fmt_cnt && !is_listed; ++j) {
152 if (ext_fmts[j].id == aud_info.ext_fmt[i].id &&
153 ext_fmts[j].bitrate == aud_info.ext_fmt[i].bitrate)
154 {
155 is_listed = PJ_TRUE;
156 }
157 }
158
159 /* Put this format into the list, if it is not in the list */
160 if (!is_listed)
161 ext_fmts[ext_fmt_cnt++] = aud_info.ext_fmt[i];
162
163 pj_assert(ext_fmt_cnt <= PJ_ARRAY_SIZE(ext_fmts));
164 }
165 }
166
167 /* Init the passthrough codec with supported formats only */
Benny Prijono35fc1eb2011-07-15 09:51:46 +0000168 codec_cfg.passthrough.setting.fmt_cnt = ext_fmt_cnt;
169 codec_cfg.passthrough.setting.fmts = ext_fmts;
170 codec_cfg.passthrough.setting.ilbc_mode = cfg->ilbc_mode;
Nanang Izzuddin81db8c72009-02-05 10:59:14 +0000171 }
172#endif /* PJMEDIA_HAS_PASSTHROUGH_CODECS */
173
Benny Prijono35fc1eb2011-07-15 09:51:46 +0000174 /* Register all codecs */
175 status = pjmedia_codec_register_audio_codecs(pjsua_var.med_endpt,
176 &codec_cfg);
Nanang Izzuddin57b88572009-04-01 12:05:34 +0000177 if (status != PJ_SUCCESS) {
Benny Prijono35fc1eb2011-07-15 09:51:46 +0000178 PJ_PERROR(1,(THIS_FILE, status, "Error registering codecs"));
Benny Prijonob90fd382011-09-18 14:59:56 +0000179 goto on_error;
Nanang Izzuddin57b88572009-04-01 12:05:34 +0000180 }
Nanang Izzuddin57b88572009-04-01 12:05:34 +0000181
Benny Prijono35fc1eb2011-07-15 09:51:46 +0000182 /* Set speex/16000 to higher priority*/
183 codec_id = pj_str("speex/16000");
184 pjmedia_codec_mgr_set_codec_priority(
185 pjmedia_endpt_get_codec_mgr(pjsua_var.med_endpt),
186 &codec_id, PJMEDIA_CODEC_PRIO_NORMAL+2);
187
188 /* Set speex/8000 to next higher priority*/
189 codec_id = pj_str("speex/8000");
190 pjmedia_codec_mgr_set_codec_priority(
191 pjmedia_endpt_get_codec_mgr(pjsua_var.med_endpt),
192 &codec_id, PJMEDIA_CODEC_PRIO_NORMAL+1);
Benny Prijonoeebe9af2006-06-13 22:57:13 +0000193
194 /* Disable ALL L16 codecs */
195 codec_id = pj_str("L16");
196 pjmedia_codec_mgr_set_codec_priority(
197 pjmedia_endpt_get_codec_mgr(pjsua_var.med_endpt),
198 &codec_id, PJMEDIA_CODEC_PRIO_DISABLED);
199
Benny Prijonoeebe9af2006-06-13 22:57:13 +0000200
201 /* Save additional conference bridge parameters for future
202 * reference.
203 */
Benny Prijono7d60d052008-03-29 12:24:20 +0000204 pjsua_var.mconf_cfg.channel_count = pjsua_var.media_cfg.channel_count;
Benny Prijonoeebe9af2006-06-13 22:57:13 +0000205 pjsua_var.mconf_cfg.bits_per_sample = 16;
Benny Prijono6e7c5ad2008-03-13 10:15:16 +0000206 pjsua_var.mconf_cfg.samples_per_frame = pjsua_var.media_cfg.clock_rate *
207 pjsua_var.mconf_cfg.channel_count *
208 pjsua_var.media_cfg.audio_frame_ptime /
209 1000;
Benny Prijonoeebe9af2006-06-13 22:57:13 +0000210
Benny Prijono0498d902006-06-19 14:49:14 +0000211 /* Init options for conference bridge. */
212 opt = PJMEDIA_CONF_NO_DEVICE;
213 if (pjsua_var.media_cfg.quality >= 3 &&
Benny Prijono7ca96da2006-08-07 12:11:40 +0000214 pjsua_var.media_cfg.quality <= 4)
Benny Prijono0498d902006-06-19 14:49:14 +0000215 {
216 opt |= PJMEDIA_CONF_SMALL_FILTER;
217 }
218 else if (pjsua_var.media_cfg.quality < 3) {
219 opt |= PJMEDIA_CONF_USE_LINEAR;
220 }
221
Benny Prijonoeebe9af2006-06-13 22:57:13 +0000222 /* Init conference bridge. */
223 status = pjmedia_conf_create(pjsua_var.pool,
224 pjsua_var.media_cfg.max_media_ports,
225 pjsua_var.media_cfg.clock_rate,
226 pjsua_var.mconf_cfg.channel_count,
227 pjsua_var.mconf_cfg.samples_per_frame,
228 pjsua_var.mconf_cfg.bits_per_sample,
Benny Prijono0498d902006-06-19 14:49:14 +0000229 opt, &pjsua_var.mconf);
Benny Prijonoeebe9af2006-06-13 22:57:13 +0000230 if (status != PJ_SUCCESS) {
Benny Prijono50f19b32008-03-11 13:15:43 +0000231 pjsua_perror(THIS_FILE, "Error creating conference bridge",
Benny Prijonoeebe9af2006-06-13 22:57:13 +0000232 status);
Benny Prijonob90fd382011-09-18 14:59:56 +0000233 goto on_error;
Benny Prijonoeebe9af2006-06-13 22:57:13 +0000234 }
235
Benny Prijonof798e502009-03-09 13:08:16 +0000236 /* Are we using the audio switchboard (a.k.a APS-Direct)? */
237 pjsua_var.is_mswitch = pjmedia_conf_get_master_port(pjsua_var.mconf)
238 ->info.signature == PJMEDIA_CONF_SWITCH_SIGNATURE;
239
Benny Prijonoeebe9af2006-06-13 22:57:13 +0000240 /* Create null port just in case user wants to use null sound. */
241 status = pjmedia_null_port_create(pjsua_var.pool,
242 pjsua_var.media_cfg.clock_rate,
243 pjsua_var.mconf_cfg.channel_count,
244 pjsua_var.mconf_cfg.samples_per_frame,
245 pjsua_var.mconf_cfg.bits_per_sample,
246 &pjsua_var.null_port);
247 PJ_ASSERT_RETURN(status == PJ_SUCCESS, status);
248
Nanang Izzuddin69b69ae2009-04-14 15:18:30 +0000249#if defined(PJMEDIA_HAS_SRTP) && (PJMEDIA_HAS_SRTP != 0)
250 /* Initialize SRTP library. */
251 status = pjmedia_srtp_init_lib();
252 if (status != PJ_SUCCESS) {
253 pjsua_perror(THIS_FILE, "Error initializing SRTP library",
254 status);
Benny Prijonob90fd382011-09-18 14:59:56 +0000255 goto on_error;
Nanang Izzuddin69b69ae2009-04-14 15:18:30 +0000256 }
257#endif
258
Benny Prijono9f468d12011-07-07 07:46:33 +0000259 /* Video */
260#if PJMEDIA_HAS_VIDEO
261 status = pjsua_vid_subsys_init();
262 if (status != PJ_SUCCESS)
Benny Prijonob90fd382011-09-18 14:59:56 +0000263 goto on_error;
Benny Prijono9f468d12011-07-07 07:46:33 +0000264#endif
265
Benny Prijonob90fd382011-09-18 14:59:56 +0000266 pj_log_pop_indent();
Benny Prijonoeebe9af2006-06-13 22:57:13 +0000267 return PJ_SUCCESS;
Benny Prijonob90fd382011-09-18 14:59:56 +0000268
269on_error:
270 pj_log_pop_indent();
271 return status;
Benny Prijonoeebe9af2006-06-13 22:57:13 +0000272}
273
274
Nanang Izzuddin148fd392008-06-16 09:52:50 +0000275/* Check if sound device is idle. */
276static void check_snd_dev_idle()
277{
Nanang Izzuddin7082b262009-06-12 11:15:08 +0000278 unsigned call_cnt;
279
280 /* Get the call count, we shouldn't close the sound device when there is
281 * any calls active.
282 */
283 call_cnt = pjsua_call_get_count();
284
285 /* When this function is called from pjsua_media_channel_deinit() upon
286 * disconnecting call, actually the call count hasn't been updated/
287 * decreased. So we put additional check here, if there is only one
288 * call and it's in DISCONNECTED state, there is actually no active
289 * call.
290 */
291 if (call_cnt == 1) {
292 pjsua_call_id call_id;
293 pj_status_t status;
294
295 status = pjsua_enum_calls(&call_id, &call_cnt);
296 if (status == PJ_SUCCESS && call_cnt > 0 &&
297 !pjsua_call_is_active(call_id))
298 {
299 call_cnt = 0;
300 }
301 }
Nanang Izzuddin148fd392008-06-16 09:52:50 +0000302
303 /* Activate sound device auto-close timer if sound device is idle.
Nanang Izzuddin7082b262009-06-12 11:15:08 +0000304 * It is idle when there is no port connection in the bridge and
305 * there is no active call.
Benny Prijono2d647722011-07-13 03:05:22 +0000306 *
307 * Note: this block is now valid if no snd dev is used because of #1299
Nanang Izzuddin148fd392008-06-16 09:52:50 +0000308 */
Benny Prijono2d647722011-07-13 03:05:22 +0000309 if ((pjsua_var.snd_port!=NULL || pjsua_var.null_snd!=NULL ||
310 pjsua_var.no_snd) &&
Nanang Izzuddin148fd392008-06-16 09:52:50 +0000311 pjsua_var.snd_idle_timer.id == PJ_FALSE &&
312 pjmedia_conf_get_connect_count(pjsua_var.mconf) == 0 &&
Nanang Izzuddin7082b262009-06-12 11:15:08 +0000313 call_cnt == 0 &&
Nanang Izzuddin148fd392008-06-16 09:52:50 +0000314 pjsua_var.media_cfg.snd_auto_close_time >= 0)
315 {
316 pj_time_val delay;
317
318 delay.msec = 0;
319 delay.sec = pjsua_var.media_cfg.snd_auto_close_time;
320
321 pjsua_var.snd_idle_timer.id = PJ_TRUE;
Benny Prijono0bc99a92011-03-17 04:34:43 +0000322 pjsip_endpt_schedule_timer(pjsua_var.endpt, &pjsua_var.snd_idle_timer,
Nanang Izzuddin148fd392008-06-16 09:52:50 +0000323 &delay);
324 }
325}
326
327
Nanang Izzuddin68559c32008-06-13 17:01:46 +0000328/* Timer callback to close sound device */
329static void close_snd_timer_cb( pj_timer_heap_t *th,
330 pj_timer_entry *entry)
331{
332 PJ_UNUSED_ARG(th);
333
Benny Prijono0f711b42009-05-06 19:08:43 +0000334 PJSUA_LOCK();
335 if (entry->id) {
Benny Prijono0bc99a92011-03-17 04:34:43 +0000336 PJ_LOG(4,(THIS_FILE,"Closing sound device after idle for %d seconds",
Benny Prijono0f711b42009-05-06 19:08:43 +0000337 pjsua_var.media_cfg.snd_auto_close_time));
Nanang Izzuddin68559c32008-06-13 17:01:46 +0000338
Benny Prijono0f711b42009-05-06 19:08:43 +0000339 entry->id = PJ_FALSE;
Nanang Izzuddin68559c32008-06-13 17:01:46 +0000340
Benny Prijono0f711b42009-05-06 19:08:43 +0000341 close_snd_dev();
342 }
343 PJSUA_UNLOCK();
Nanang Izzuddin68559c32008-06-13 17:01:46 +0000344}
345
Benny Prijonoeebe9af2006-06-13 22:57:13 +0000346
347/*
348 * Start pjsua media subsystem.
349 */
350pj_status_t pjsua_media_subsys_start(void)
351{
352 pj_status_t status;
353
Benny Prijonob90fd382011-09-18 14:59:56 +0000354 pj_log_push_indent();
355
Benny Prijono0bc99a92011-03-17 04:34:43 +0000356#if DISABLED_FOR_TICKET_1185
Benny Prijonoeebe9af2006-06-13 22:57:13 +0000357 /* Create media for calls, if none is specified */
Benny Prijono0bc99a92011-03-17 04:34:43 +0000358 if (pjsua_var.calls[0].media[0].tp == NULL) {
Benny Prijonoeebe9af2006-06-13 22:57:13 +0000359 pjsua_transport_config transport_cfg;
360
361 /* Create default transport config */
362 pjsua_transport_config_default(&transport_cfg);
363 transport_cfg.port = DEFAULT_RTP_PORT;
364
365 status = pjsua_media_transports_create(&transport_cfg);
Benny Prijonob90fd382011-09-18 14:59:56 +0000366 if (status != PJ_SUCCESS) {
367 pj_log_pop_indent();
Benny Prijonoeebe9af2006-06-13 22:57:13 +0000368 return status;
Benny Prijonob90fd382011-09-18 14:59:56 +0000369 }
Benny Prijonoeebe9af2006-06-13 22:57:13 +0000370 }
Benny Prijono0bc99a92011-03-17 04:34:43 +0000371#endif
Benny Prijonoeebe9af2006-06-13 22:57:13 +0000372
Benny Prijono0bc99a92011-03-17 04:34:43 +0000373 pj_timer_entry_init(&pjsua_var.snd_idle_timer, PJ_FALSE, NULL,
Nanang Izzuddin68559c32008-06-13 17:01:46 +0000374 &close_snd_timer_cb);
Benny Prijonoeebe9af2006-06-13 22:57:13 +0000375
Benny Prijono9f468d12011-07-07 07:46:33 +0000376 /* Video */
377#if PJMEDIA_HAS_VIDEO
378 status = pjsua_vid_subsys_start();
Benny Prijonob90fd382011-09-18 14:59:56 +0000379 if (status != PJ_SUCCESS) {
380 pj_log_pop_indent();
Benny Prijono9f468d12011-07-07 07:46:33 +0000381 return status;
Benny Prijonob90fd382011-09-18 14:59:56 +0000382 }
Benny Prijono9f468d12011-07-07 07:46:33 +0000383#endif
384
Benny Prijonobf53b002010-01-04 13:08:31 +0000385 /* Perform NAT detection */
Benny Prijono0bc99a92011-03-17 04:34:43 +0000386 status = pjsua_detect_nat_type();
387 if (status != PJ_SUCCESS) {
388 PJ_PERROR(1,(THIS_FILE, status, "NAT type detection failed"));
389 }
Benny Prijonobf53b002010-01-04 13:08:31 +0000390
Benny Prijonob90fd382011-09-18 14:59:56 +0000391 pj_log_pop_indent();
Benny Prijonoeebe9af2006-06-13 22:57:13 +0000392 return PJ_SUCCESS;
393}
394
395
396/*
397 * Destroy pjsua media subsystem.
398 */
399pj_status_t pjsua_media_subsys_destroy(void)
400{
401 unsigned i;
402
Benny Prijono384dab42009-10-14 01:58:04 +0000403 PJ_LOG(4,(THIS_FILE, "Shutting down media.."));
Benny Prijonob90fd382011-09-18 14:59:56 +0000404 pj_log_push_indent();
Benny Prijono384dab42009-10-14 01:58:04 +0000405
Benny Prijonoeebe9af2006-06-13 22:57:13 +0000406 close_snd_dev();
407
408 if (pjsua_var.mconf) {
409 pjmedia_conf_destroy(pjsua_var.mconf);
410 pjsua_var.mconf = NULL;
411 }
412
413 if (pjsua_var.null_port) {
414 pjmedia_port_destroy(pjsua_var.null_port);
415 pjsua_var.null_port = NULL;
416 }
417
418 /* Destroy file players */
419 for (i=0; i<PJ_ARRAY_SIZE(pjsua_var.player); ++i) {
420 if (pjsua_var.player[i].port) {
421 pjmedia_port_destroy(pjsua_var.player[i].port);
422 pjsua_var.player[i].port = NULL;
423 }
424 }
425
426 /* Destroy file recorders */
427 for (i=0; i<PJ_ARRAY_SIZE(pjsua_var.recorder); ++i) {
428 if (pjsua_var.recorder[i].port) {
429 pjmedia_port_destroy(pjsua_var.recorder[i].port);
430 pjsua_var.recorder[i].port = NULL;
431 }
432 }
433
434 /* Close media transports */
Benny Prijono0875ae82006-12-26 00:11:48 +0000435 for (i=0; i<pjsua_var.ua_cfg.max_calls; ++i) {
Benny Prijono0bc99a92011-03-17 04:34:43 +0000436 unsigned strm_idx;
437 pjsua_call *call = &pjsua_var.calls[i];
438 for (strm_idx=0; strm_idx<call->med_cnt; ++strm_idx) {
439 pjsua_call_media *call_med = &call->media[strm_idx];
440 if (call_med->tp_st != PJSUA_MED_TP_IDLE) {
441 pjsua_media_channel_deinit(i);
442 }
443 if (call_med->tp && call_med->tp_auto_del) {
444 pjmedia_transport_close(call_med->tp);
445 }
446 call_med->tp = NULL;
Benny Prijono311b63f2008-07-14 11:31:40 +0000447 }
Benny Prijonoeebe9af2006-06-13 22:57:13 +0000448 }
449
450 /* Destroy media endpoint. */
451 if (pjsua_var.med_endpt) {
452
Benny Prijono0bc99a92011-03-17 04:34:43 +0000453# if PJMEDIA_HAS_VIDEO
Benny Prijono9f468d12011-07-07 07:46:33 +0000454 pjsua_vid_subsys_destroy();
Benny Prijono0bc99a92011-03-17 04:34:43 +0000455# endif
Benny Prijonoeebe9af2006-06-13 22:57:13 +0000456
Benny Prijonoeebe9af2006-06-13 22:57:13 +0000457 pjmedia_endpt_destroy(pjsua_var.med_endpt);
458 pjsua_var.med_endpt = NULL;
Benny Prijonoeebe9af2006-06-13 22:57:13 +0000459
Benny Prijonoad2e0ca2007-04-29 12:31:51 +0000460 /* Deinitialize sound subsystem */
Benny Prijonoa41d66e2007-10-01 12:17:23 +0000461 // Not necessary, as pjmedia_snd_deinit() should have been called
462 // in pjmedia_endpt_destroy().
463 //pjmedia_snd_deinit();
Benny Prijonoad2e0ca2007-04-29 12:31:51 +0000464 }
Benny Prijonoaf1bb1e2006-11-21 12:39:31 +0000465
Benny Prijonode479562007-03-15 10:23:55 +0000466 /* Reset RTP port */
467 next_rtp_port = 0;
468
Benny Prijonob90fd382011-09-18 14:59:56 +0000469 pj_log_pop_indent();
470
Benny Prijonoeebe9af2006-06-13 22:57:13 +0000471 return PJ_SUCCESS;
472}
473
Benny Prijono0bc99a92011-03-17 04:34:43 +0000474/*
Benny Prijonoeebe9af2006-06-13 22:57:13 +0000475 * Create RTP and RTCP socket pair, and possibly resolve their public
476 * address via STUN.
477 */
478static pj_status_t create_rtp_rtcp_sock(const pjsua_transport_config *cfg,
479 pjmedia_sock_info *skinfo)
480{
Benny Prijono0bc99a92011-03-17 04:34:43 +0000481 enum {
Benny Prijonoeebe9af2006-06-13 22:57:13 +0000482 RTP_RETRY = 100
483 };
484 int i;
485 pj_sockaddr_in bound_addr;
486 pj_sockaddr_in mapped_addr[2];
487 pj_status_t status = PJ_SUCCESS;
488 char addr_buf[PJ_INET6_ADDRSTRLEN+2];
489 pj_sock_t sock[2];
490
491 /* Make sure STUN server resolution has completed */
492 status = resolve_stun_server(PJ_TRUE);
493 if (status != PJ_SUCCESS) {
494 pjsua_perror(THIS_FILE, "Error resolving STUN server", status);
495 return status;
496 }
497
498 if (next_rtp_port == 0)
499 next_rtp_port = (pj_uint16_t)cfg->port;
500
Benny Prijono0bc99a92011-03-17 04:34:43 +0000501 if (next_rtp_port == 0)
502 next_rtp_port = (pj_uint16_t)40000;
503
Benny Prijonoeebe9af2006-06-13 22:57:13 +0000504 for (i=0; i<2; ++i)
505 sock[i] = PJ_INVALID_SOCKET;
506
507 bound_addr.sin_addr.s_addr = PJ_INADDR_ANY;
508 if (cfg->bound_addr.slen) {
509 status = pj_sockaddr_in_set_str_addr(&bound_addr, &cfg->bound_addr);
510 if (status != PJ_SUCCESS) {
511 pjsua_perror(THIS_FILE, "Unable to resolve transport bind address",
512 status);
513 return status;
514 }
515 }
516
517 /* Loop retry to bind RTP and RTCP sockets. */
518 for (i=0; i<RTP_RETRY; ++i, next_rtp_port += 2) {
519
520 /* Create RTP socket. */
521 status = pj_sock_socket(pj_AF_INET(), pj_SOCK_DGRAM(), 0, &sock[0]);
522 if (status != PJ_SUCCESS) {
523 pjsua_perror(THIS_FILE, "socket() error", status);
524 return status;
525 }
526
527 /* Apply QoS to RTP socket, if specified */
Benny Prijono0bc99a92011-03-17 04:34:43 +0000528 status = pj_sock_apply_qos2(sock[0], cfg->qos_type,
529 &cfg->qos_params,
Benny Prijonoeebe9af2006-06-13 22:57:13 +0000530 2, THIS_FILE, "RTP socket");
531
532 /* Bind RTP socket */
Benny Prijono0bc99a92011-03-17 04:34:43 +0000533 status=pj_sock_bind_in(sock[0], pj_ntohl(bound_addr.sin_addr.s_addr),
Benny Prijonoeebe9af2006-06-13 22:57:13 +0000534 next_rtp_port);
535 if (status != PJ_SUCCESS) {
Benny Prijono0bc99a92011-03-17 04:34:43 +0000536 pj_sock_close(sock[0]);
Benny Prijonoeebe9af2006-06-13 22:57:13 +0000537 sock[0] = PJ_INVALID_SOCKET;
538 continue;
539 }
540
541 /* Create RTCP socket. */
542 status = pj_sock_socket(pj_AF_INET(), pj_SOCK_DGRAM(), 0, &sock[1]);
543 if (status != PJ_SUCCESS) {
544 pjsua_perror(THIS_FILE, "socket() error", status);
545 pj_sock_close(sock[0]);
546 return status;
547 }
548
549 /* Apply QoS to RTCP socket, if specified */
Benny Prijono0bc99a92011-03-17 04:34:43 +0000550 status = pj_sock_apply_qos2(sock[1], cfg->qos_type,
551 &cfg->qos_params,
Benny Prijonoeebe9af2006-06-13 22:57:13 +0000552 2, THIS_FILE, "RTCP socket");
553
554 /* Bind RTCP socket */
Benny Prijono0bc99a92011-03-17 04:34:43 +0000555 status=pj_sock_bind_in(sock[1], pj_ntohl(bound_addr.sin_addr.s_addr),
Benny Prijonoeebe9af2006-06-13 22:57:13 +0000556 (pj_uint16_t)(next_rtp_port+1));
557 if (status != PJ_SUCCESS) {
Benny Prijono0bc99a92011-03-17 04:34:43 +0000558 pj_sock_close(sock[0]);
Benny Prijonoeebe9af2006-06-13 22:57:13 +0000559 sock[0] = PJ_INVALID_SOCKET;
560
Benny Prijono0bc99a92011-03-17 04:34:43 +0000561 pj_sock_close(sock[1]);
Benny Prijonoeebe9af2006-06-13 22:57:13 +0000562 sock[1] = PJ_INVALID_SOCKET;
563 continue;
564 }
565
566 /*
567 * If we're configured to use STUN, then find out the mapped address,
568 * and make sure that the mapped RTCP port is adjacent with the RTP.
569 */
570 if (pjsua_var.stun_srv.addr.sa_family != 0) {
571 char ip_addr[32];
572 pj_str_t stun_srv;
573
Benny Prijono0bc99a92011-03-17 04:34:43 +0000574 pj_ansi_strcpy(ip_addr,
Benny Prijonoeebe9af2006-06-13 22:57:13 +0000575 pj_inet_ntoa(pjsua_var.stun_srv.ipv4.sin_addr));
576 stun_srv = pj_str(ip_addr);
577
578 status=pjstun_get_mapped_addr(&pjsua_var.cp.factory, 2, sock,
579 &stun_srv, pj_ntohs(pjsua_var.stun_srv.ipv4.sin_port),
580 &stun_srv, pj_ntohs(pjsua_var.stun_srv.ipv4.sin_port),
581 mapped_addr);
582 if (status != PJ_SUCCESS) {
583 pjsua_perror(THIS_FILE, "STUN resolve error", status);
584 goto on_error;
585 }
586
587#if PJSUA_REQUIRE_CONSECUTIVE_RTCP_PORT
Benny Prijono0bc99a92011-03-17 04:34:43 +0000588 if (pj_ntohs(mapped_addr[1].sin_port) ==
Benny Prijonoeebe9af2006-06-13 22:57:13 +0000589 pj_ntohs(mapped_addr[0].sin_port)+1)
590 {
591 /* Success! */
592 break;
593 }
594
Benny Prijono0bc99a92011-03-17 04:34:43 +0000595 pj_sock_close(sock[0]);
Benny Prijonoeebe9af2006-06-13 22:57:13 +0000596 sock[0] = PJ_INVALID_SOCKET;
597
Benny Prijono0bc99a92011-03-17 04:34:43 +0000598 pj_sock_close(sock[1]);
Benny Prijonoeebe9af2006-06-13 22:57:13 +0000599 sock[1] = PJ_INVALID_SOCKET;
600#else
Benny Prijono0bc99a92011-03-17 04:34:43 +0000601 if (pj_ntohs(mapped_addr[1].sin_port) !=
Benny Prijonoeebe9af2006-06-13 22:57:13 +0000602 pj_ntohs(mapped_addr[0].sin_port)+1)
603 {
Benny Prijono0bc99a92011-03-17 04:34:43 +0000604 PJ_LOG(4,(THIS_FILE,
Benny Prijonoeebe9af2006-06-13 22:57:13 +0000605 "Note: STUN mapped RTCP port %d is not adjacent"
606 " to RTP port %d",
607 pj_ntohs(mapped_addr[1].sin_port),
608 pj_ntohs(mapped_addr[0].sin_port)));
609 }
610 /* Success! */
611 break;
612#endif
613
614 } else if (cfg->public_addr.slen) {
615
616 status = pj_sockaddr_in_init(&mapped_addr[0], &cfg->public_addr,
617 (pj_uint16_t)next_rtp_port);
618 if (status != PJ_SUCCESS)
619 goto on_error;
620
621 status = pj_sockaddr_in_init(&mapped_addr[1], &cfg->public_addr,
622 (pj_uint16_t)(next_rtp_port+1));
623 if (status != PJ_SUCCESS)
624 goto on_error;
625
626 break;
627
628 } else {
629
630 if (bound_addr.sin_addr.s_addr == 0) {
631 pj_sockaddr addr;
632
633 /* Get local IP address. */
634 status = pj_gethostip(pj_AF_INET(), &addr);
635 if (status != PJ_SUCCESS)
636 goto on_error;
637
638 bound_addr.sin_addr.s_addr = addr.ipv4.sin_addr.s_addr;
639 }
640
641 for (i=0; i<2; ++i) {
642 pj_sockaddr_in_init(&mapped_addr[i], NULL, 0);
643 mapped_addr[i].sin_addr.s_addr = bound_addr.sin_addr.s_addr;
644 }
645
646 mapped_addr[0].sin_port=pj_htons((pj_uint16_t)next_rtp_port);
647 mapped_addr[1].sin_port=pj_htons((pj_uint16_t)(next_rtp_port+1));
648 break;
649 }
650 }
651
652 if (sock[0] == PJ_INVALID_SOCKET) {
Benny Prijono0bc99a92011-03-17 04:34:43 +0000653 PJ_LOG(1,(THIS_FILE,
Benny Prijonoeebe9af2006-06-13 22:57:13 +0000654 "Unable to find appropriate RTP/RTCP ports combination"));
655 goto on_error;
656 }
657
658
659 skinfo->rtp_sock = sock[0];
Benny Prijono0bc99a92011-03-17 04:34:43 +0000660 pj_memcpy(&skinfo->rtp_addr_name,
Benny Prijonoeebe9af2006-06-13 22:57:13 +0000661 &mapped_addr[0], sizeof(pj_sockaddr_in));
662
663 skinfo->rtcp_sock = sock[1];
Benny Prijono0bc99a92011-03-17 04:34:43 +0000664 pj_memcpy(&skinfo->rtcp_addr_name,
Benny Prijonoeebe9af2006-06-13 22:57:13 +0000665 &mapped_addr[1], sizeof(pj_sockaddr_in));
666
667 PJ_LOG(4,(THIS_FILE, "RTP socket reachable at %s",
668 pj_sockaddr_print(&skinfo->rtp_addr_name, addr_buf,
669 sizeof(addr_buf), 3)));
670 PJ_LOG(4,(THIS_FILE, "RTCP socket reachable at %s",
671 pj_sockaddr_print(&skinfo->rtcp_addr_name, addr_buf,
672 sizeof(addr_buf), 3)));
673
674 next_rtp_port += 2;
675 return PJ_SUCCESS;
676
677on_error:
678 for (i=0; i<2; ++i) {
679 if (sock[i] != PJ_INVALID_SOCKET)
680 pj_sock_close(sock[i]);
681 }
682 return status;
683}
684
Benny Prijono0bc99a92011-03-17 04:34:43 +0000685/* Create normal UDP media transports */
686static pj_status_t create_udp_media_transport(const pjsua_transport_config *cfg,
687 pjsua_call_media *call_med)
Benny Prijonoeebe9af2006-06-13 22:57:13 +0000688{
Benny Prijono0bc99a92011-03-17 04:34:43 +0000689 pjmedia_sock_info skinfo;
Benny Prijonoeebe9af2006-06-13 22:57:13 +0000690 pj_status_t status;
691
Benny Prijono0bc99a92011-03-17 04:34:43 +0000692 status = create_rtp_rtcp_sock(cfg, &skinfo);
693 if (status != PJ_SUCCESS) {
694 pjsua_perror(THIS_FILE, "Unable to create RTP/RTCP socket",
695 status);
696 goto on_error;
Benny Prijonoeebe9af2006-06-13 22:57:13 +0000697 }
698
Benny Prijono0bc99a92011-03-17 04:34:43 +0000699 status = pjmedia_transport_udp_attach(pjsua_var.med_endpt, NULL,
700 &skinfo, 0, &call_med->tp);
701 if (status != PJ_SUCCESS) {
702 pjsua_perror(THIS_FILE, "Unable to create media transport",
703 status);
704 goto on_error;
705 }
Benny Prijonoeebe9af2006-06-13 22:57:13 +0000706
Benny Prijono0bc99a92011-03-17 04:34:43 +0000707 pjmedia_transport_simulate_lost(call_med->tp, PJMEDIA_DIR_ENCODING,
708 pjsua_var.media_cfg.tx_drop_pct);
709
710 pjmedia_transport_simulate_lost(call_med->tp, PJMEDIA_DIR_DECODING,
711 pjsua_var.media_cfg.rx_drop_pct);
Benny Prijonoeebe9af2006-06-13 22:57:13 +0000712
Sauw Ming73ecfe82011-09-21 10:20:01 +0000713 call_med->tp_ready = PJ_SUCCESS;
714
Benny Prijonoeebe9af2006-06-13 22:57:13 +0000715 return PJ_SUCCESS;
Benny Prijono0bc99a92011-03-17 04:34:43 +0000716
717on_error:
718 if (call_med->tp)
719 pjmedia_transport_close(call_med->tp);
720
721 return status;
Benny Prijonoeebe9af2006-06-13 22:57:13 +0000722}
723
Benny Prijono0bc99a92011-03-17 04:34:43 +0000724#if DISABLED_FOR_TICKET_1185
Benny Prijonoc97608e2007-03-23 16:34:20 +0000725/* Create normal UDP media transports */
726static pj_status_t create_udp_media_transports(pjsua_transport_config *cfg)
Benny Prijonoeebe9af2006-06-13 22:57:13 +0000727{
Benny Prijonoeebe9af2006-06-13 22:57:13 +0000728 unsigned i;
729 pj_status_t status;
730
Benny Prijono0bc99a92011-03-17 04:34:43 +0000731 for (i=0; i < pjsua_var.ua_cfg.max_calls; ++i) {
732 pjsua_call *call = &pjsua_var.calls[i];
733 unsigned strm_idx;
Benny Prijonoeebe9af2006-06-13 22:57:13 +0000734
Benny Prijono0bc99a92011-03-17 04:34:43 +0000735 for (strm_idx=0; strm_idx < call->med_cnt; ++strm_idx) {
736 pjsua_call_media *call_med = &call->media[strm_idx];
737
738 status = create_udp_media_transport(cfg, &call_med->tp);
739 if (status != PJ_SUCCESS)
740 goto on_error;
Benny Prijonoeebe9af2006-06-13 22:57:13 +0000741 }
Benny Prijonoeebe9af2006-06-13 22:57:13 +0000742 }
743
Benny Prijonoc97608e2007-03-23 16:34:20 +0000744 return PJ_SUCCESS;
745
746on_error:
Benny Prijono0bc99a92011-03-17 04:34:43 +0000747 for (i=0; i < pjsua_var.ua_cfg.max_calls; ++i) {
748 pjsua_call *call = &pjsua_var.calls[i];
749 unsigned strm_idx;
750
751 for (strm_idx=0; strm_idx < call->med_cnt; ++strm_idx) {
752 pjsua_call_media *call_med = &call->media[strm_idx];
753
754 if (call_med->tp) {
755 pjmedia_transport_close(call_med->tp);
756 call_med->tp = NULL;
757 }
Benny Prijonoc97608e2007-03-23 16:34:20 +0000758 }
759 }
Benny Prijonoc97608e2007-03-23 16:34:20 +0000760 return status;
761}
Benny Prijono0bc99a92011-03-17 04:34:43 +0000762#endif
Benny Prijonoc97608e2007-03-23 16:34:20 +0000763
Benny Prijono096c56c2007-09-15 08:30:16 +0000764/* This callback is called when ICE negotiation completes */
Benny Prijonof76e1392008-06-06 14:51:48 +0000765static void on_ice_complete(pjmedia_transport *tp,
766 pj_ice_strans_op op,
767 pj_status_t result)
Benny Prijono096c56c2007-09-15 08:30:16 +0000768{
Benny Prijono0bc99a92011-03-17 04:34:43 +0000769 pjsua_call_media *call_med = (pjsua_call_media*)tp->user_data;
Benny Prijono096c56c2007-09-15 08:30:16 +0000770
Benny Prijono0bc99a92011-03-17 04:34:43 +0000771 if (!call_med)
Benny Prijonof76e1392008-06-06 14:51:48 +0000772 return;
773
774 switch (op) {
775 case PJ_ICE_STRANS_OP_INIT:
Sauw Ming73ecfe82011-09-21 10:20:01 +0000776 call_med->tp_ready = result;
777 if (call_med->med_create_cb)
778 (*call_med->med_create_cb)(call_med, result,
779 call_med->call->secure_level, NULL);
Benny Prijonof76e1392008-06-06 14:51:48 +0000780 break;
781 case PJ_ICE_STRANS_OP_NEGOTIATION:
782 if (result != PJ_SUCCESS) {
Benny Prijono0bc99a92011-03-17 04:34:43 +0000783 call_med->state = PJSUA_CALL_MEDIA_ERROR;
784 call_med->dir = PJMEDIA_DIR_NONE;
Benny Prijonof76e1392008-06-06 14:51:48 +0000785
Benny Prijono0bc99a92011-03-17 04:34:43 +0000786 if (call_med->call && pjsua_var.ua_cfg.cb.on_call_media_state) {
787 pjsua_var.ua_cfg.cb.on_call_media_state(call_med->call->index);
Benny Prijonof76e1392008-06-06 14:51:48 +0000788 }
Benny Prijono0bc99a92011-03-17 04:34:43 +0000789 } else if (call_med->call) {
Benny Prijonof5d9f1f2009-10-14 13:13:18 +0000790 /* Send UPDATE if default transport address is different than
791 * what was advertised (ticket #881)
792 */
793 pjmedia_transport_info tpinfo;
794 pjmedia_ice_transport_info *ii = NULL;
795 unsigned i;
796
797 pjmedia_transport_info_init(&tpinfo);
798 pjmedia_transport_get_info(tp, &tpinfo);
799 for (i=0; i<tpinfo.specific_info_cnt; ++i) {
800 if (tpinfo.spc_info[i].type==PJMEDIA_TRANSPORT_TYPE_ICE) {
801 ii = (pjmedia_ice_transport_info*)
802 tpinfo.spc_info[i].buffer;
803 break;
804 }
805 }
806
807 if (ii && ii->role==PJ_ICE_SESS_ROLE_CONTROLLING &&
808 pj_sockaddr_cmp(&tpinfo.sock_info.rtp_addr_name,
Benny Prijono0bc99a92011-03-17 04:34:43 +0000809 &call_med->rtp_addr))
Benny Prijonof5d9f1f2009-10-14 13:13:18 +0000810 {
Benny Prijonoa8f9e622010-06-21 13:28:55 +0000811 pj_bool_t use_update;
812 const pj_str_t STR_UPDATE = { "UPDATE", 6 };
813 pjsip_dialog_cap_status support_update;
814 pjsip_dialog *dlg;
815
Benny Prijono0bc99a92011-03-17 04:34:43 +0000816 dlg = call_med->call->inv->dlg;
Benny Prijonoa8f9e622010-06-21 13:28:55 +0000817 support_update = pjsip_dlg_remote_has_cap(dlg, PJSIP_H_ALLOW,
818 NULL, &STR_UPDATE);
819 use_update = (support_update == PJSIP_DIALOG_CAP_SUPPORTED);
820
Benny Prijonof5d9f1f2009-10-14 13:13:18 +0000821 PJ_LOG(4,(THIS_FILE,
822 "ICE default transport address has changed for "
Benny Prijono0bc99a92011-03-17 04:34:43 +0000823 "call %d, sending %s", call_med->call->index,
Benny Prijonoa8f9e622010-06-21 13:28:55 +0000824 (use_update ? "UPDATE" : "re-INVITE")));
825
826 if (use_update)
Benny Prijono0bc99a92011-03-17 04:34:43 +0000827 pjsua_call_update(call_med->call->index, 0, NULL);
Benny Prijonoa8f9e622010-06-21 13:28:55 +0000828 else
Benny Prijono0bc99a92011-03-17 04:34:43 +0000829 pjsua_call_reinvite(call_med->call->index, 0, NULL);
Benny Prijonof5d9f1f2009-10-14 13:13:18 +0000830 }
Benny Prijonof76e1392008-06-06 14:51:48 +0000831 }
832 break;
Benny Prijono4d6ff4d2010-06-19 12:35:33 +0000833 case PJ_ICE_STRANS_OP_KEEP_ALIVE:
834 if (result != PJ_SUCCESS) {
835 PJ_PERROR(4,(THIS_FILE, result,
Benny Prijono0bc99a92011-03-17 04:34:43 +0000836 "ICE keep alive failure for transport %d:%d",
837 call_med->call->index, call_med->idx));
Benny Prijono4d6ff4d2010-06-19 12:35:33 +0000838 }
Sauw Ming73ecfe82011-09-21 10:20:01 +0000839 if (pjsua_var.ua_cfg.cb.on_call_media_transport_state) {
840 pjsua_med_tp_state_info info;
841
842 pj_bzero(&info, sizeof(info));
843 info.med_idx = call_med->idx;
844 info.state = call_med->tp_st;
845 info.status = result;
846 info.ext_info = &op;
847 (*pjsua_var.ua_cfg.cb.on_call_media_transport_state)(
848 call_med->call->index, &info);
849 }
Benny Prijono4d6ff4d2010-06-19 12:35:33 +0000850 if (pjsua_var.ua_cfg.cb.on_ice_transport_error) {
Benny Prijono0bc99a92011-03-17 04:34:43 +0000851 pjsua_call_id id = call_med->call->index;
Benny Prijono4d6ff4d2010-06-19 12:35:33 +0000852 (*pjsua_var.ua_cfg.cb.on_ice_transport_error)(id, op, result,
853 NULL);
854 }
855 break;
Benny Prijono096c56c2007-09-15 08:30:16 +0000856 }
857}
858
859
Benny Prijonof76e1392008-06-06 14:51:48 +0000860/* Parse "HOST:PORT" format */
861static pj_status_t parse_host_port(const pj_str_t *host_port,
862 pj_str_t *host, pj_uint16_t *port)
Benny Prijonoc97608e2007-03-23 16:34:20 +0000863{
Benny Prijonof76e1392008-06-06 14:51:48 +0000864 pj_str_t str_port;
865
866 str_port.ptr = pj_strchr(host_port, ':');
867 if (str_port.ptr != NULL) {
868 int iport;
869
870 host->ptr = host_port->ptr;
871 host->slen = (str_port.ptr - host->ptr);
872 str_port.ptr++;
873 str_port.slen = host_port->slen - host->slen - 1;
874 iport = (int)pj_strtoul(&str_port);
875 if (iport < 1 || iport > 65535)
876 return PJ_EINVAL;
877 *port = (pj_uint16_t)iport;
878 } else {
879 *host = *host_port;
880 *port = 0;
881 }
882
883 return PJ_SUCCESS;
884}
885
886/* Create ICE media transports (when ice is enabled) */
Benny Prijono0bc99a92011-03-17 04:34:43 +0000887static pj_status_t create_ice_media_transport(
888 const pjsua_transport_config *cfg,
Sauw Ming73ecfe82011-09-21 10:20:01 +0000889 pjsua_call_media *call_med,
890 pj_bool_t async)
Benny Prijonof76e1392008-06-06 14:51:48 +0000891{
892 char stunip[PJ_INET6_ADDRSTRLEN];
893 pj_ice_strans_cfg ice_cfg;
Benny Prijono0bc99a92011-03-17 04:34:43 +0000894 pjmedia_ice_cb ice_cb;
895 char name[32];
896 unsigned comp_cnt;
Benny Prijonoc97608e2007-03-23 16:34:20 +0000897 pj_status_t status;
898
Benny Prijonoda9785b2007-04-02 20:43:06 +0000899 /* Make sure STUN server resolution has completed */
Benny Prijonobb995fd2009-08-12 11:03:23 +0000900 status = resolve_stun_server(PJ_TRUE);
Benny Prijonoda9785b2007-04-02 20:43:06 +0000901 if (status != PJ_SUCCESS) {
902 pjsua_perror(THIS_FILE, "Error resolving STUN server", status);
903 return status;
904 }
905
Benny Prijonof76e1392008-06-06 14:51:48 +0000906 /* Create ICE stream transport configuration */
907 pj_ice_strans_cfg_default(&ice_cfg);
908 pj_stun_config_init(&ice_cfg.stun_cfg, &pjsua_var.cp.factory, 0,
909 pjsip_endpt_get_ioqueue(pjsua_var.endpt),
910 pjsip_endpt_get_timer_heap(pjsua_var.endpt));
911
912 ice_cfg.af = pj_AF_INET();
913 ice_cfg.resolver = pjsua_var.resolver;
914
Benny Prijono329d6382009-05-29 13:04:03 +0000915 ice_cfg.opt = pjsua_var.media_cfg.ice_opt;
916
Benny Prijonof76e1392008-06-06 14:51:48 +0000917 /* Configure STUN settings */
918 if (pj_sockaddr_has_addr(&pjsua_var.stun_srv)) {
919 pj_sockaddr_print(&pjsua_var.stun_srv, stunip, sizeof(stunip), 0);
920 ice_cfg.stun.server = pj_str(stunip);
921 ice_cfg.stun.port = pj_sockaddr_get_port(&pjsua_var.stun_srv);
922 }
Benny Prijono329d6382009-05-29 13:04:03 +0000923 if (pjsua_var.media_cfg.ice_max_host_cands >= 0)
924 ice_cfg.stun.max_host_cands = pjsua_var.media_cfg.ice_max_host_cands;
Benny Prijonof76e1392008-06-06 14:51:48 +0000925
Benny Prijono4d79b0f2009-10-25 09:02:07 +0000926 /* Copy QoS setting to STUN setting */
927 ice_cfg.stun.cfg.qos_type = cfg->qos_type;
928 pj_memcpy(&ice_cfg.stun.cfg.qos_params, &cfg->qos_params,
929 sizeof(cfg->qos_params));
930
Benny Prijonof76e1392008-06-06 14:51:48 +0000931 /* Configure TURN settings */
932 if (pjsua_var.media_cfg.enable_turn) {
933 status = parse_host_port(&pjsua_var.media_cfg.turn_server,
934 &ice_cfg.turn.server,
935 &ice_cfg.turn.port);
936 if (status != PJ_SUCCESS || ice_cfg.turn.server.slen == 0) {
937 PJ_LOG(1,(THIS_FILE, "Invalid TURN server setting"));
938 return PJ_EINVAL;
939 }
940 if (ice_cfg.turn.port == 0)
941 ice_cfg.turn.port = 3479;
942 ice_cfg.turn.conn_type = pjsua_var.media_cfg.turn_conn_type;
943 pj_memcpy(&ice_cfg.turn.auth_cred,
944 &pjsua_var.media_cfg.turn_auth_cred,
945 sizeof(ice_cfg.turn.auth_cred));
Benny Prijono4d79b0f2009-10-25 09:02:07 +0000946
947 /* Copy QoS setting to TURN setting */
948 ice_cfg.turn.cfg.qos_type = cfg->qos_type;
949 pj_memcpy(&ice_cfg.turn.cfg.qos_params, &cfg->qos_params,
950 sizeof(cfg->qos_params));
Benny Prijonof76e1392008-06-06 14:51:48 +0000951 }
Benny Prijonob681a2f2007-03-25 18:44:51 +0000952
Benny Prijono0bc99a92011-03-17 04:34:43 +0000953 pj_bzero(&ice_cb, sizeof(pjmedia_ice_cb));
954 ice_cb.on_ice_complete = &on_ice_complete;
955 pj_ansi_snprintf(name, sizeof(name), "icetp%02d", call_med->idx);
956 call_med->tp_ready = PJ_EPENDING;
Benny Prijonoc97608e2007-03-23 16:34:20 +0000957
Benny Prijono0bc99a92011-03-17 04:34:43 +0000958 comp_cnt = 1;
959 if (PJMEDIA_ADVERTISE_RTCP && !pjsua_var.media_cfg.ice_no_rtcp)
960 ++comp_cnt;
Benny Prijonof76e1392008-06-06 14:51:48 +0000961
Benny Prijonobd6613f2011-04-11 17:27:14 +0000962 status = pjmedia_ice_create3(pjsua_var.med_endpt, name, comp_cnt,
963 &ice_cfg, &ice_cb, 0, call_med,
964 &call_med->tp);
Benny Prijono0bc99a92011-03-17 04:34:43 +0000965 if (status != PJ_SUCCESS) {
966 pjsua_perror(THIS_FILE, "Unable to create ICE media transport",
967 status);
968 goto on_error;
Benny Prijonoc97608e2007-03-23 16:34:20 +0000969 }
Benny Prijonoeebe9af2006-06-13 22:57:13 +0000970
Benny Prijono0bc99a92011-03-17 04:34:43 +0000971 /* Wait until transport is initialized, or time out */
Sauw Ming73ecfe82011-09-21 10:20:01 +0000972 if (!async) {
973 PJSUA_UNLOCK();
974 while (call_med->tp_ready == PJ_EPENDING) {
975 pjsua_handle_events(100);
976 }
977 PJSUA_LOCK();
Benny Prijono0bc99a92011-03-17 04:34:43 +0000978 }
Sauw Ming73ecfe82011-09-21 10:20:01 +0000979
980 if (async && call_med->tp_ready == PJ_EPENDING) {
981 return PJ_EPENDING;
982 } else if (call_med->tp_ready != PJ_SUCCESS) {
Benny Prijono0bc99a92011-03-17 04:34:43 +0000983 pjsua_perror(THIS_FILE, "Error initializing ICE media transport",
984 call_med->tp_ready);
985 status = call_med->tp_ready;
986 goto on_error;
987 }
988
989 pjmedia_transport_simulate_lost(call_med->tp, PJMEDIA_DIR_ENCODING,
990 pjsua_var.media_cfg.tx_drop_pct);
991
992 pjmedia_transport_simulate_lost(call_med->tp, PJMEDIA_DIR_DECODING,
993 pjsua_var.media_cfg.rx_drop_pct);
994
Benny Prijonoeebe9af2006-06-13 22:57:13 +0000995 return PJ_SUCCESS;
996
997on_error:
Benny Prijono0bc99a92011-03-17 04:34:43 +0000998 if (call_med->tp != NULL) {
999 pjmedia_transport_close(call_med->tp);
1000 call_med->tp = NULL;
Benny Prijonoeebe9af2006-06-13 22:57:13 +00001001 }
1002
Benny Prijonoc97608e2007-03-23 16:34:20 +00001003 return status;
1004}
1005
Benny Prijono0bc99a92011-03-17 04:34:43 +00001006#if DISABLED_FOR_TICKET_1185
1007/* Create ICE media transports (when ice is enabled) */
1008static pj_status_t create_ice_media_transports(pjsua_transport_config *cfg)
1009{
1010 unsigned i;
1011 pj_status_t status;
Benny Prijonoc97608e2007-03-23 16:34:20 +00001012
Benny Prijono0bc99a92011-03-17 04:34:43 +00001013 for (i=0; i < pjsua_var.ua_cfg.max_calls; ++i) {
1014 pjsua_call *call = &pjsua_var.calls[i];
1015 unsigned strm_idx;
1016
1017 for (strm_idx=0; strm_idx < call->med_cnt; ++strm_idx) {
1018 pjsua_call_media *call_med = &call->media[strm_idx];
1019
1020 status = create_ice_media_transport(cfg, call_med);
1021 if (status != PJ_SUCCESS)
1022 goto on_error;
1023 }
1024 }
1025
1026 return PJ_SUCCESS;
1027
1028on_error:
1029 for (i=0; i < pjsua_var.ua_cfg.max_calls; ++i) {
1030 pjsua_call *call = &pjsua_var.calls[i];
1031 unsigned strm_idx;
1032
1033 for (strm_idx=0; strm_idx < call->med_cnt; ++strm_idx) {
1034 pjsua_call_media *call_med = &call->media[strm_idx];
1035
1036 if (call_med->tp) {
1037 pjmedia_transport_close(call_med->tp);
1038 call_med->tp = NULL;
1039 }
1040 }
1041 }
1042 return status;
1043}
1044#endif
1045
1046#if DISABLED_FOR_TICKET_1185
Benny Prijonoc97608e2007-03-23 16:34:20 +00001047/*
Benny Prijono0bc99a92011-03-17 04:34:43 +00001048 * Create media transports for all the calls. This function creates
Benny Prijonoc97608e2007-03-23 16:34:20 +00001049 * one UDP media transport for each call.
1050 */
Benny Prijono1f61a8f2007-08-16 10:11:44 +00001051PJ_DEF(pj_status_t) pjsua_media_transports_create(
1052 const pjsua_transport_config *app_cfg)
Benny Prijonoc97608e2007-03-23 16:34:20 +00001053{
1054 pjsua_transport_config cfg;
1055 unsigned i;
1056 pj_status_t status;
1057
1058
1059 /* Make sure pjsua_init() has been called */
1060 PJ_ASSERT_RETURN(pjsua_var.ua_cfg.max_calls>0, PJ_EINVALIDOP);
1061
1062 PJSUA_LOCK();
1063
1064 /* Delete existing media transports */
1065 for (i=0; i<pjsua_var.ua_cfg.max_calls; ++i) {
Benny Prijono0bc99a92011-03-17 04:34:43 +00001066 pjsua_call *call = &pjsua_var.calls[i];
1067 unsigned strm_idx;
1068
1069 for (strm_idx=0; strm_idx < call->med_cnt; ++strm_idx) {
1070 pjsua_call_media *call_med = &call->media[strm_idx];
1071
1072 if (call_med->tp && call_med->tp_auto_del) {
1073 pjmedia_transport_close(call_med->tp);
1074 call_med->tp = NULL;
1075 call_med->tp_orig = NULL;
1076 }
Benny Prijonoc97608e2007-03-23 16:34:20 +00001077 }
1078 }
1079
1080 /* Copy config */
1081 pjsua_transport_config_dup(pjsua_var.pool, &cfg, app_cfg);
1082
Benny Prijono40860c32008-09-04 13:55:33 +00001083 /* Create the transports */
Benny Prijonoc97608e2007-03-23 16:34:20 +00001084 if (pjsua_var.media_cfg.enable_ice) {
Benny Prijono4d79b0f2009-10-25 09:02:07 +00001085 status = create_ice_media_transports(&cfg);
Benny Prijonoc97608e2007-03-23 16:34:20 +00001086 } else {
1087 status = create_udp_media_transports(&cfg);
1088 }
1089
Benny Prijono40860c32008-09-04 13:55:33 +00001090 /* Set media transport auto_delete to True */
1091 for (i=0; i<pjsua_var.ua_cfg.max_calls; ++i) {
Benny Prijono0bc99a92011-03-17 04:34:43 +00001092 pjsua_call *call = &pjsua_var.calls[i];
1093 unsigned strm_idx;
1094
1095 for (strm_idx=0; strm_idx < call->med_cnt; ++strm_idx) {
1096 pjsua_call_media *call_med = &call->media[strm_idx];
1097
1098 call_med->tp_auto_del = PJ_TRUE;
1099 }
Benny Prijono40860c32008-09-04 13:55:33 +00001100 }
Benny Prijonoc97608e2007-03-23 16:34:20 +00001101
Benny Prijonoeebe9af2006-06-13 22:57:13 +00001102 PJSUA_UNLOCK();
1103
1104 return status;
1105}
1106
Benny Prijono40860c32008-09-04 13:55:33 +00001107/*
1108 * Attach application's created media transports.
1109 */
1110PJ_DEF(pj_status_t) pjsua_media_transports_attach(pjsua_media_transport tp[],
1111 unsigned count,
1112 pj_bool_t auto_delete)
1113{
1114 unsigned i;
1115
1116 PJ_ASSERT_RETURN(tp && count==pjsua_var.ua_cfg.max_calls, PJ_EINVAL);
1117
1118 /* Assign the media transports */
1119 for (i=0; i<pjsua_var.ua_cfg.max_calls; ++i) {
Benny Prijono0bc99a92011-03-17 04:34:43 +00001120 pjsua_call *call = &pjsua_var.calls[i];
1121 unsigned strm_idx;
1122
1123 for (strm_idx=0; strm_idx < call->med_cnt; ++strm_idx) {
1124 pjsua_call_media *call_med = &call->media[strm_idx];
1125
1126 if (call_med->tp && call_med->tp_auto_del) {
1127 pjmedia_transport_close(call_med->tp);
1128 call_med->tp = NULL;
1129 call_med->tp_orig = NULL;
1130 }
Benny Prijono40860c32008-09-04 13:55:33 +00001131 }
1132
Benny Prijono0bc99a92011-03-17 04:34:43 +00001133 PJ_TODO(remove_pjsua_media_transports_attach);
1134
1135 call->media[0].tp = tp[i].transport;
1136 call->media[0].tp_auto_del = auto_delete;
Benny Prijono40860c32008-09-04 13:55:33 +00001137 }
1138
1139 return PJ_SUCCESS;
1140}
Benny Prijono0bc99a92011-03-17 04:34:43 +00001141#endif
Benny Prijono40860c32008-09-04 13:55:33 +00001142
Benny Prijono0bc99a92011-03-17 04:34:43 +00001143/* Go through the list of media in the SDP, find acceptable media, and
1144 * sort them based on the "quality" of the media, and store the indexes
1145 * in the specified array. Media with the best quality will be listed
1146 * first in the array. The quality factors considered currently is
1147 * encryption.
1148 */
1149static void sort_media(const pjmedia_sdp_session *sdp,
1150 const pj_str_t *type,
1151 pjmedia_srtp_use use_srtp,
1152 pj_uint8_t midx[],
1153 unsigned *p_count)
Nanang Izzuddin1e952a82010-10-05 16:32:04 +00001154{
1155 unsigned i;
Benny Prijono0bc99a92011-03-17 04:34:43 +00001156 unsigned count = 0;
1157 int score[PJSUA_MAX_CALL_MEDIA];
Nanang Izzuddin1e952a82010-10-05 16:32:04 +00001158
Benny Prijono0bc99a92011-03-17 04:34:43 +00001159 pj_assert(*p_count >= PJSUA_MAX_CALL_MEDIA);
1160
1161 *p_count = 0;
Benny Prijono09c0d672011-04-11 05:03:24 +00001162 for (i=0; i<PJSUA_MAX_CALL_MEDIA; ++i)
1163 score[i] = 1;
Benny Prijono0bc99a92011-03-17 04:34:43 +00001164
1165 /* Score each media */
1166 for (i=0; i<sdp->media_count && count<PJSUA_MAX_CALL_MEDIA; ++i) {
Nanang Izzuddin1e952a82010-10-05 16:32:04 +00001167 const pjmedia_sdp_media *m = sdp->media[i];
Nanang Izzuddina6414292011-04-08 04:26:18 +00001168 const pjmedia_sdp_conn *c;
Nanang Izzuddin1e952a82010-10-05 16:32:04 +00001169
Benny Prijono0bc99a92011-03-17 04:34:43 +00001170 /* Skip different media */
1171 if (pj_stricmp(&m->desc.media, type) != 0) {
1172 score[count++] = -22000;
Nanang Izzuddin1e952a82010-10-05 16:32:04 +00001173 continue;
1174 }
1175
Nanang Izzuddina6414292011-04-08 04:26:18 +00001176 c = m->conn? m->conn : sdp->conn;
1177
Benny Prijono0bc99a92011-03-17 04:34:43 +00001178 /* Supported transports */
1179 if (pj_stricmp2(&m->desc.transport, "RTP/SAVP")==0) {
1180 switch (use_srtp) {
1181 case PJMEDIA_SRTP_MANDATORY:
1182 case PJMEDIA_SRTP_OPTIONAL:
1183 ++score[i];
Nanang Izzuddin1e952a82010-10-05 16:32:04 +00001184 break;
Benny Prijono0bc99a92011-03-17 04:34:43 +00001185 case PJMEDIA_SRTP_DISABLED:
Nanang Izzuddind8c33d82011-08-18 18:30:55 +00001186 //--score[i];
1187 score[i] -= 5;
Benny Prijono0bc99a92011-03-17 04:34:43 +00001188 break;
Nanang Izzuddin1e952a82010-10-05 16:32:04 +00001189 }
Benny Prijono0bc99a92011-03-17 04:34:43 +00001190 } else if (pj_stricmp2(&m->desc.transport, "RTP/AVP")==0) {
1191 switch (use_srtp) {
1192 case PJMEDIA_SRTP_MANDATORY:
Nanang Izzuddind8c33d82011-08-18 18:30:55 +00001193 //--score[i];
1194 score[i] -= 5;
Benny Prijono0bc99a92011-03-17 04:34:43 +00001195 break;
1196 case PJMEDIA_SRTP_OPTIONAL:
1197 /* No change in score */
1198 break;
1199 case PJMEDIA_SRTP_DISABLED:
1200 ++score[i];
1201 break;
1202 }
1203 } else {
1204 score[i] -= 10;
1205 }
1206
1207 /* Is media disabled? */
1208 if (m->desc.port == 0)
1209 score[i] -= 10;
1210
1211 /* Is media inactive? */
Nanang Izzuddina6414292011-04-08 04:26:18 +00001212 if (pjmedia_sdp_media_find_attr2(m, "inactive", NULL) ||
1213 pj_strcmp2(&c->addr, "0.0.0.0") == 0)
1214 {
1215 //score[i] -= 10;
1216 score[i] -= 1;
1217 }
Benny Prijono0bc99a92011-03-17 04:34:43 +00001218
1219 ++count;
1220 }
1221
1222 /* Created sorted list based on quality */
1223 for (i=0; i<count; ++i) {
1224 unsigned j;
1225 int best = 0;
1226
1227 for (j=1; j<count; ++j) {
1228 if (score[j] > score[best])
1229 best = j;
1230 }
1231 /* Don't put media with negative score, that media is unacceptable
1232 * for us.
1233 */
1234 if (score[best] >= 0) {
1235 midx[*p_count] = (pj_uint8_t)best;
1236 (*p_count)++;
1237 }
1238
1239 score[best] = -22000;
1240
1241 }
1242}
1243
Benny Prijonoee0ba182011-07-15 06:18:29 +00001244/* Callback to receive media events */
1245static pj_status_t call_media_on_event(pjmedia_event_subscription *esub,
1246 pjmedia_event *event)
1247{
1248 pjsua_call_media *call_med = (pjsua_call_media*)esub->user_data;
1249 pjsua_call *call = call_med->call;
1250
1251 if (pjsua_var.ua_cfg.cb.on_call_media_event && call) {
1252 ++event->proc_cnt;
1253 (*pjsua_var.ua_cfg.cb.on_call_media_event)(call->index,
1254 call_med->idx, event);
Benny Prijono53a7c702008-04-14 02:57:29 +00001255 }
1256
1257 return PJ_SUCCESS;
1258}
1259
Sauw Ming73ecfe82011-09-21 10:20:01 +00001260/* Set media transport state and notify the application via the callback. */
1261void set_media_tp_state(pjsua_call_media *call_med,
1262 pjsua_med_tp_st tp_st)
1263{
1264 if (pjsua_var.ua_cfg.cb.on_call_media_transport_state &&
1265 call_med->tp_st != tp_st)
1266 {
1267 pjsua_med_tp_state_info info;
1268
1269 pj_bzero(&info, sizeof(info));
1270 info.med_idx = call_med->idx;
1271 info.state = tp_st;
1272 info.status = call_med->tp_ready;
1273 (*pjsua_var.ua_cfg.cb.on_call_media_transport_state)(
1274 call_med->call->index, &info);
1275 }
1276
1277 call_med->tp_st = tp_st;
1278}
1279
1280/* Callback to resume pjsua_call_media_init() after media transport
1281 * creation is completed.
1282 */
1283static pj_status_t call_media_init_cb(pjsua_call_media *call_med,
1284 pj_status_t status,
1285 int security_level,
1286 int *sip_err_code)
Nanang Izzuddin1e952a82010-10-05 16:32:04 +00001287{
Benny Prijono0bc99a92011-03-17 04:34:43 +00001288 pjsua_acc *acc = &pjsua_var.acc[call_med->call->acc_id];
Sauw Ming73ecfe82011-09-21 10:20:01 +00001289 int err_code = 0;
Nanang Izzuddin1e952a82010-10-05 16:32:04 +00001290
Sauw Ming73ecfe82011-09-21 10:20:01 +00001291 if (status != PJ_SUCCESS)
1292 goto on_error;
Nanang Izzuddin1e952a82010-10-05 16:32:04 +00001293
Sauw Ming73ecfe82011-09-21 10:20:01 +00001294 if (call_med->tp_st == PJSUA_MED_TP_CREATING)
1295 set_media_tp_state(call_med, PJSUA_MED_TP_IDLE);
Nanang Izzuddin1e952a82010-10-05 16:32:04 +00001296
Benny Prijono0bc99a92011-03-17 04:34:43 +00001297#if defined(PJMEDIA_HAS_SRTP) && (PJMEDIA_HAS_SRTP != 0)
1298 /* This function may be called when SRTP transport already exists
1299 * (e.g: in re-invite, update), don't need to destroy/re-create.
1300 */
1301 if (!call_med->tp_orig || call_med->tp == call_med->tp_orig) {
1302 pjmedia_srtp_setting srtp_opt;
1303 pjmedia_transport *srtp = NULL;
Nanang Izzuddin1e952a82010-10-05 16:32:04 +00001304
Benny Prijono0bc99a92011-03-17 04:34:43 +00001305 /* Check if SRTP requires secure signaling */
1306 if (acc->cfg.use_srtp != PJMEDIA_SRTP_DISABLED) {
1307 if (security_level < acc->cfg.srtp_secure_signaling) {
Sauw Ming73ecfe82011-09-21 10:20:01 +00001308 err_code = PJSIP_SC_NOT_ACCEPTABLE;
Benny Prijono0bc99a92011-03-17 04:34:43 +00001309 status = PJSIP_ESESSIONINSECURE;
1310 goto on_error;
1311 }
1312 }
1313
1314 /* Always create SRTP adapter */
1315 pjmedia_srtp_setting_default(&srtp_opt);
Nanang Izzuddinb6c239c2011-05-10 05:42:28 +00001316 srtp_opt.close_member_tp = PJ_TRUE;
Benny Prijono0bc99a92011-03-17 04:34:43 +00001317 /* If media session has been ever established, let's use remote's
1318 * preference in SRTP usage policy, especially when it is stricter.
1319 */
1320 if (call_med->rem_srtp_use > acc->cfg.use_srtp)
1321 srtp_opt.use = call_med->rem_srtp_use;
1322 else
1323 srtp_opt.use = acc->cfg.use_srtp;
1324
1325 status = pjmedia_transport_srtp_create(pjsua_var.med_endpt,
1326 call_med->tp,
1327 &srtp_opt, &srtp);
1328 if (status != PJ_SUCCESS) {
Sauw Ming73ecfe82011-09-21 10:20:01 +00001329 err_code = PJSIP_SC_INTERNAL_SERVER_ERROR;
Benny Prijono0bc99a92011-03-17 04:34:43 +00001330 goto on_error;
1331 }
1332
1333 /* Set SRTP as current media transport */
1334 call_med->tp_orig = call_med->tp;
1335 call_med->tp = srtp;
1336 }
1337#else
Benny Prijono7df19342011-07-23 02:54:03 +00001338 call_med->tp_orig = call_med->tp;
Benny Prijono0bc99a92011-03-17 04:34:43 +00001339 PJ_UNUSED_ARG(security_level);
1340#endif
1341
Benny Prijono1fe04ee2011-07-15 07:27:05 +00001342 pjmedia_event_subscription_init(&call_med->esub_rend, &call_media_on_event,
Benny Prijonoee0ba182011-07-15 06:18:29 +00001343 call_med);
Benny Prijono1fe04ee2011-07-15 07:27:05 +00001344 pjmedia_event_subscription_init(&call_med->esub_cap, &call_media_on_event,
Sauw Ming73ecfe82011-09-21 10:20:01 +00001345 call_med);
Benny Prijono0bc99a92011-03-17 04:34:43 +00001346
1347on_error:
Sauw Ming73ecfe82011-09-21 10:20:01 +00001348 if (status != PJ_SUCCESS && call_med->tp) {
Benny Prijono0bc99a92011-03-17 04:34:43 +00001349 pjmedia_transport_close(call_med->tp);
1350 call_med->tp = NULL;
1351 }
Sauw Ming73ecfe82011-09-21 10:20:01 +00001352
1353 if (sip_err_code)
1354 *sip_err_code = err_code;
1355
1356 if (call_med->med_init_cb) {
1357 pjsua_med_tp_state_info info;
1358
1359 pj_bzero(&info, sizeof(info));
1360 info.status = status;
1361 info.state = call_med->tp_st;
1362 info.med_idx = call_med->idx;
1363 info.sip_err_code = err_code;
1364 (*call_med->med_init_cb)(call_med->call->index, &info);
1365 }
1366
1367 return status;
1368}
1369
1370/* Initialize the media line */
1371pj_status_t pjsua_call_media_init(pjsua_call_media *call_med,
1372 pjmedia_type type,
1373 const pjsua_transport_config *tcfg,
1374 int security_level,
1375 int *sip_err_code,
1376 pj_bool_t async,
1377 pjsua_med_tp_state_cb cb)
1378{
1379 pjsua_acc *acc = &pjsua_var.acc[call_med->call->acc_id];
1380 pj_status_t status = PJ_SUCCESS;
1381
1382 /*
1383 * Note: this function may be called when the media already exists
1384 * (e.g. in reinvites, updates, etc.)
1385 */
1386 call_med->type = type;
1387
1388 /* Create the media transport for initial call. */
1389 if (call_med->tp == NULL) {
1390#if defined(PJMEDIA_HAS_VIDEO) && (PJMEDIA_HAS_VIDEO != 0)
1391 /* While in initial call, set default video devices */
1392 if (type == PJMEDIA_TYPE_VIDEO) {
1393 call_med->strm.v.rdr_dev = acc->cfg.vid_rend_dev;
1394 call_med->strm.v.cap_dev = acc->cfg.vid_cap_dev;
1395 if (call_med->strm.v.rdr_dev == PJMEDIA_VID_DEFAULT_RENDER_DEV) {
1396 pjmedia_vid_dev_info info;
1397 pjmedia_vid_dev_get_info(call_med->strm.v.rdr_dev, &info);
1398 call_med->strm.v.rdr_dev = info.id;
1399 }
1400 if (call_med->strm.v.cap_dev == PJMEDIA_VID_DEFAULT_CAPTURE_DEV) {
1401 pjmedia_vid_dev_info info;
1402 pjmedia_vid_dev_get_info(call_med->strm.v.cap_dev, &info);
1403 call_med->strm.v.cap_dev = info.id;
1404 }
1405 }
1406#endif
1407
1408 set_media_tp_state(call_med, PJSUA_MED_TP_CREATING);
1409
1410 if (async) {
1411 call_med->med_create_cb = &call_media_init_cb;
1412 call_med->med_init_cb = cb;
1413 }
1414
1415 if (pjsua_var.media_cfg.enable_ice) {
1416 status = create_ice_media_transport(tcfg, call_med, async);
1417 } else {
1418 status = create_udp_media_transport(tcfg, call_med);
1419 }
1420
1421 if (status == PJ_EPENDING) {
1422 /* We will resume call media initialization in the
1423 * on_ice_complete() callback.
1424 */
1425 return PJ_EPENDING;
1426 } else if (status != PJ_SUCCESS) {
1427 PJ_PERROR(1,(THIS_FILE, status, "Error creating media transport"));
1428 return status;
1429 }
1430
1431 /* Media transport creation completed immediately, so
1432 * we don't need to call the callback.
1433 */
1434 call_med->med_init_cb = NULL;
1435
1436 } else if (call_med->tp_st == PJSUA_MED_TP_DISABLED) {
1437 /* Media is being reenabled. */
1438 set_media_tp_state(call_med, PJSUA_MED_TP_INIT);
1439 }
1440
1441 return call_media_init_cb(call_med, status, security_level,
1442 sip_err_code);
1443}
1444
1445/* Callback to resume pjsua_media_channel_init() after media transport
1446 * initialization is completed.
1447 */
1448static pj_status_t media_channel_init_cb(pjsua_call_id call_id,
1449 const pjsua_med_tp_state_info *info)
1450{
1451 pjsua_call *call = &pjsua_var.calls[call_id];
1452 pj_status_t status = (info? info->status : PJ_SUCCESS);
1453 unsigned mi;
1454
1455 if (info) {
1456 pj_mutex_lock(call->med_ch_mutex);
1457
1458 /* Set the callback to NULL to indicate that the async operation
1459 * has completed.
1460 */
1461 call->media[info->med_idx].med_init_cb = NULL;
1462
1463 /* In case of failure, save the information to be returned
1464 * by the last media transport to finish.
1465 */
1466 if (info->status != PJ_SUCCESS)
1467 pj_memcpy(&call->med_ch_info, info, sizeof(info));
1468
1469 /* Check whether all the call's medias have finished calling their
1470 * callbacks.
1471 */
1472 for (mi=0; mi < call->med_cnt; ++mi) {
1473 pjsua_call_media *call_med = &call->media[mi];
1474
1475 if (call_med->med_init_cb) {
1476 pj_mutex_unlock(call->med_ch_mutex);
1477 return PJ_SUCCESS;
1478 }
1479
1480 if (call_med->tp_ready != PJ_SUCCESS)
1481 status = call_med->tp_ready;
1482 }
1483
1484 /* OK, we are called by the last media transport finished. */
1485 pj_mutex_unlock(call->med_ch_mutex);
1486 }
1487
1488 if (call->med_ch_mutex) {
1489 pj_mutex_destroy(call->med_ch_mutex);
1490 call->med_ch_mutex = NULL;
1491 }
1492
1493 if (status != PJ_SUCCESS) {
1494 pjsua_media_channel_deinit(call_id);
1495 goto on_error;
1496 }
1497
1498 /* Tell the media transport of a new offer/answer session */
1499 for (mi=0; mi < call->med_cnt; ++mi) {
1500 pjsua_call_media *call_med = &call->media[mi];
1501
1502 /* Note: tp may be NULL if this media line is disabled */
1503 if (call_med->tp && call_med->tp_st == PJSUA_MED_TP_IDLE) {
Sauw Ming99cc8ff2011-09-22 04:24:56 +00001504 pj_pool_t *tmp_pool = call->async_call.pool_prov;
1505
1506 if (!tmp_pool) {
1507 tmp_pool = (call->inv? call->inv->pool_prov:
1508 call->async_call.dlg->pool);
1509 }
Sauw Ming73ecfe82011-09-21 10:20:01 +00001510
1511 status = pjmedia_transport_media_create(
1512 call_med->tp, tmp_pool,
1513 0, call->async_call.rem_sdp, mi);
1514 if (status != PJ_SUCCESS) {
1515 call->med_ch_info.status = status;
1516 call->med_ch_info.med_idx = mi;
1517 call->med_ch_info.state = call_med->tp_st;
1518 call->med_ch_info.sip_err_code = PJSIP_SC_NOT_ACCEPTABLE;
1519 pjsua_media_channel_deinit(call_id);
1520 goto on_error;
1521 }
1522
1523 set_media_tp_state(call_med, PJSUA_MED_TP_INIT);
1524 }
1525 }
1526
1527 call->med_ch_info.status = PJ_SUCCESS;
1528
1529on_error:
1530 if (call->med_ch_cb)
1531 (*call->med_ch_cb)(call->index, &call->med_ch_info);
1532
Benny Prijono0bc99a92011-03-17 04:34:43 +00001533 return status;
1534}
Nanang Izzuddin1e952a82010-10-05 16:32:04 +00001535
Benny Prijonod8179652008-01-23 20:39:07 +00001536pj_status_t pjsua_media_channel_init(pjsua_call_id call_id,
1537 pjsip_role_e role,
1538 int security_level,
1539 pj_pool_t *tmp_pool,
1540 const pjmedia_sdp_session *rem_sdp,
Sauw Ming73ecfe82011-09-21 10:20:01 +00001541 int *sip_err_code,
1542 pj_bool_t async,
1543 pjsua_med_tp_state_cb cb)
Benny Prijonoc97608e2007-03-23 16:34:20 +00001544{
Benny Prijono0bc99a92011-03-17 04:34:43 +00001545 const pj_str_t STR_AUDIO = { "audio", 5 };
1546 const pj_str_t STR_VIDEO = { "video", 5 };
Benny Prijonod8179652008-01-23 20:39:07 +00001547 pjsua_call *call = &pjsua_var.calls[call_id];
Benny Prijonod8179652008-01-23 20:39:07 +00001548 pjsua_acc *acc = &pjsua_var.acc[call->acc_id];
Benny Prijono0bc99a92011-03-17 04:34:43 +00001549 pj_uint8_t maudidx[PJSUA_MAX_CALL_MEDIA];
1550 unsigned maudcnt = PJ_ARRAY_SIZE(maudidx);
1551 pj_uint8_t mvididx[PJSUA_MAX_CALL_MEDIA];
1552 unsigned mvidcnt = PJ_ARRAY_SIZE(mvididx);
1553 pjmedia_type media_types[PJSUA_MAX_CALL_MEDIA];
1554 unsigned mi;
Sauw Ming73ecfe82011-09-21 10:20:01 +00001555 pj_bool_t pending_med_tp = PJ_FALSE;
Benny Prijono0bc99a92011-03-17 04:34:43 +00001556 pj_status_t status;
Benny Prijonoc97608e2007-03-23 16:34:20 +00001557
Benny Prijonod8179652008-01-23 20:39:07 +00001558 PJ_UNUSED_ARG(role);
1559
Nanang Izzuddinb6c239c2011-05-10 05:42:28 +00001560 /*
1561 * Note: this function may be called when the media already exists
1562 * (e.g. in reinvites, updates, etc).
1563 */
1564
Benny Prijono0bc99a92011-03-17 04:34:43 +00001565 if (pjsua_get_state() != PJSUA_STATE_RUNNING)
1566 return PJ_EBUSY;
1567
Sauw Ming73ecfe82011-09-21 10:20:01 +00001568 if (async) {
1569 pj_pool_t *tmppool = (call->inv? call->inv->pool_prov:
1570 call->async_call.dlg->pool);
1571
1572 status = pj_mutex_create_simple(tmppool, NULL, &call->med_ch_mutex);
1573 if (status != PJ_SUCCESS)
1574 return status;
1575 }
1576
Benny Prijonob90fd382011-09-18 14:59:56 +00001577 PJ_LOG(4,(THIS_FILE, "Call %d: initializing media..", call_id));
1578 pj_log_push_indent();
1579
Benny Prijono0bc99a92011-03-17 04:34:43 +00001580#if DISABLED_FOR_TICKET_1185
Benny Prijonod8179652008-01-23 20:39:07 +00001581 /* Return error if media transport has not been created yet
Benny Prijono68f9e4f2008-03-21 08:56:02 +00001582 * (e.g. application is starting)
1583 */
Benny Prijono0bc99a92011-03-17 04:34:43 +00001584 for (i=0; i<call->med_cnt; ++i) {
1585 if (call->media[i].tp == NULL) {
Benny Prijonob90fd382011-09-18 14:59:56 +00001586 status = PJ_EBUSY;
1587 goto on_error;
Benny Prijonod8179652008-01-23 20:39:07 +00001588 }
Benny Prijonoc97608e2007-03-23 16:34:20 +00001589 }
Benny Prijonod8179652008-01-23 20:39:07 +00001590#endif
Benny Prijonoc97608e2007-03-23 16:34:20 +00001591
Benny Prijono0bc99a92011-03-17 04:34:43 +00001592 if (rem_sdp) {
1593 sort_media(rem_sdp, &STR_AUDIO, acc->cfg.use_srtp,
1594 maudidx, &maudcnt);
Nanang Izzuddind8c33d82011-08-18 18:30:55 +00001595 // Don't apply media count limitation until SDP negotiation is done.
1596 //if (maudcnt > acc->cfg.max_audio_cnt)
1597 // maudcnt = acc->cfg.max_audio_cnt;
Benny Prijonoc97608e2007-03-23 16:34:20 +00001598
Benny Prijono0bc99a92011-03-17 04:34:43 +00001599 if (maudcnt==0) {
1600 /* Expecting audio in the offer */
1601 if (sip_err_code) *sip_err_code = PJSIP_SC_NOT_ACCEPTABLE_HERE;
1602 pjsua_media_channel_deinit(call_id);
Benny Prijonob90fd382011-09-18 14:59:56 +00001603 status = PJSIP_ERRNO_FROM_SIP_STATUS(PJSIP_SC_NOT_ACCEPTABLE_HERE);
1604 goto on_error;
Benny Prijonoc97608e2007-03-23 16:34:20 +00001605 }
1606
Nanang Izzuddind8c33d82011-08-18 18:30:55 +00001607#if PJMEDIA_HAS_VIDEO
Benny Prijono0bc99a92011-03-17 04:34:43 +00001608 sort_media(rem_sdp, &STR_VIDEO, acc->cfg.use_srtp,
1609 mvididx, &mvidcnt);
Nanang Izzuddind8c33d82011-08-18 18:30:55 +00001610 // Don't apply media count limitation until SDP negotiation is done.
1611 //if (mvidcnt > acc->cfg.max_video_cnt)
1612 //mvidcnt = acc->cfg.max_video_cnt;
1613#else
1614 mvidcnt = 0;
1615#endif
Nanang Izzuddinb6c239c2011-05-10 05:42:28 +00001616
1617 /* Update media count only when remote add any media, this media count
1618 * must never decrease.
Benny Prijonod8179652008-01-23 20:39:07 +00001619 */
Nanang Izzuddinb6c239c2011-05-10 05:42:28 +00001620 if (call->med_cnt < rem_sdp->media_count)
1621 call->med_cnt = PJ_MIN(rem_sdp->media_count, PJSUA_MAX_CALL_MEDIA);
Benny Prijonod8179652008-01-23 20:39:07 +00001622
Benny Prijono0bc99a92011-03-17 04:34:43 +00001623 } else {
1624 maudcnt = acc->cfg.max_audio_cnt;
1625 for (mi=0; mi<maudcnt; ++mi) {
Nanang Izzuddinbf26db12011-03-18 07:54:50 +00001626 maudidx[mi] = (pj_uint8_t)mi;
Benny Prijono0bc99a92011-03-17 04:34:43 +00001627 media_types[mi] = PJMEDIA_TYPE_AUDIO;
Benny Prijonod8179652008-01-23 20:39:07 +00001628 }
Nanang Izzuddind8c33d82011-08-18 18:30:55 +00001629#if PJMEDIA_HAS_VIDEO
Benny Prijono0bc99a92011-03-17 04:34:43 +00001630 mvidcnt = acc->cfg.max_video_cnt;
1631 for (mi=0; mi<mvidcnt; ++mi) {
1632 media_types[maudcnt + mi] = PJMEDIA_TYPE_VIDEO;
1633 }
Nanang Izzuddind8c33d82011-08-18 18:30:55 +00001634#else
1635 mvidcnt = 0;
1636#endif
Nanang Izzuddinb6c239c2011-05-10 05:42:28 +00001637 call->med_cnt = maudcnt + mvidcnt;
Benny Prijonoa310bd22008-06-27 21:19:44 +00001638 }
1639
Benny Prijono0bc99a92011-03-17 04:34:43 +00001640 if (call->med_cnt == 0) {
1641 /* Expecting at least one media */
Benny Prijonoab8dba92008-06-27 21:59:15 +00001642 if (sip_err_code) *sip_err_code = PJSIP_SC_NOT_ACCEPTABLE_HERE;
Benny Prijonoa310bd22008-06-27 21:19:44 +00001643 pjsua_media_channel_deinit(call_id);
Benny Prijonob90fd382011-09-18 14:59:56 +00001644 status = PJSIP_ERRNO_FROM_SIP_STATUS(PJSIP_SC_NOT_ACCEPTABLE_HERE);
1645 goto on_error;
Benny Prijonoa310bd22008-06-27 21:19:44 +00001646 }
1647
Sauw Ming73ecfe82011-09-21 10:20:01 +00001648 if (async) {
1649 call->med_ch_cb = cb;
1650 if (rem_sdp) {
1651 /* TODO: change rem_sdp to non-const parameter. */
1652 call->async_call.rem_sdp =
1653 pjmedia_sdp_session_clone(call->inv->pool_prov, rem_sdp);
1654 }
1655 }
1656
Sauw Ming99cc8ff2011-09-22 04:24:56 +00001657 call->async_call.pool_prov = tmp_pool;
1658
Benny Prijono0bc99a92011-03-17 04:34:43 +00001659 /* Initialize each media line */
1660 for (mi=0; mi < call->med_cnt; ++mi) {
1661 pjsua_call_media *call_med = &call->media[mi];
1662 pj_bool_t enabled = PJ_FALSE;
1663 pjmedia_type media_type = PJMEDIA_TYPE_NONE;
Benny Prijonoa310bd22008-06-27 21:19:44 +00001664
Benny Prijono0bc99a92011-03-17 04:34:43 +00001665 if (rem_sdp) {
Nanang Izzuddinb6c239c2011-05-10 05:42:28 +00001666 if (mi >= rem_sdp->media_count) {
1667 /* Media has been removed in remote re-offer */
1668 media_type = call_med->type;
1669 } else if (!pj_stricmp(&rem_sdp->media[mi]->desc.media, &STR_AUDIO)) {
Benny Prijono0bc99a92011-03-17 04:34:43 +00001670 media_type = PJMEDIA_TYPE_AUDIO;
1671 if (pj_memchr(maudidx, mi, maudcnt * sizeof(maudidx[0]))) {
1672 enabled = PJ_TRUE;
1673 }
1674 }
1675 else if (!pj_stricmp(&rem_sdp->media[mi]->desc.media, &STR_VIDEO)) {
1676 media_type = PJMEDIA_TYPE_VIDEO;
1677 if (pj_memchr(mvididx, mi, mvidcnt * sizeof(mvididx[0]))) {
1678 enabled = PJ_TRUE;
1679 }
1680 }
1681
1682 } else {
1683 enabled = PJ_TRUE;
1684 media_type = media_types[mi];
1685 }
1686
1687 if (enabled) {
1688 status = pjsua_call_media_init(call_med, media_type,
1689 &acc->cfg.rtp_cfg,
Sauw Ming73ecfe82011-09-21 10:20:01 +00001690 security_level, sip_err_code,
1691 async,
1692 (async? (pjsua_med_tp_state_cb)
1693 &media_channel_init_cb: NULL));
1694 if (status == PJ_EPENDING) {
1695 pending_med_tp = PJ_TRUE;
1696 } else if (status != PJ_SUCCESS) {
1697 if (pending_med_tp) {
1698 /* Save failure information. */
1699 call_med->tp_ready = status;
1700 pj_bzero(&call->med_ch_info, sizeof(call->med_ch_info));
1701 call->med_ch_info.status = status;
1702 call->med_ch_info.state = call_med->tp_st;
1703 call->med_ch_info.med_idx = call_med->idx;
1704 if (sip_err_code)
1705 call->med_ch_info.sip_err_code = *sip_err_code;
1706
1707 /* We will return failure in the callback later. */
1708 return PJ_EPENDING;
1709 }
1710
1711 pjsua_media_channel_deinit(call_id);
Benny Prijonob90fd382011-09-18 14:59:56 +00001712 goto on_error;
Benny Prijono0bc99a92011-03-17 04:34:43 +00001713 }
1714 } else {
Nanang Izzuddinb6c239c2011-05-10 05:42:28 +00001715 /* By convention, the media is disabled if transport is NULL
1716 * or transport state is PJSUA_MED_TP_DISABLED.
1717 */
Benny Prijono0bc99a92011-03-17 04:34:43 +00001718 if (call_med->tp) {
Nanang Izzuddinb6c239c2011-05-10 05:42:28 +00001719 // Don't close transport here, as SDP negotiation has not been
1720 // done and stream may be still active.
1721 //pjmedia_transport_close(call_med->tp);
1722 //call_med->tp = NULL;
1723 pj_assert(call_med->tp_st == PJSUA_MED_TP_INIT ||
1724 call_med->tp_st == PJSUA_MED_TP_RUNNING);
Sauw Ming73ecfe82011-09-21 10:20:01 +00001725 set_media_tp_state(call_med, PJSUA_MED_TP_DISABLED);
Benny Prijono0bc99a92011-03-17 04:34:43 +00001726 }
Nanang Izzuddinb6c239c2011-05-10 05:42:28 +00001727
1728 /* Put media type just for info */
1729 call_med->type = media_type;
Benny Prijono0bc99a92011-03-17 04:34:43 +00001730 }
Benny Prijono224b4e22008-06-19 14:10:28 +00001731 }
1732
Benny Prijono0bc99a92011-03-17 04:34:43 +00001733 call->audio_idx = maudidx[0];
1734
1735 PJ_LOG(4,(THIS_FILE, "Media index %d selected for audio call %d",
1736 call->audio_idx, call->index));
1737
Sauw Ming73ecfe82011-09-21 10:20:01 +00001738 if (pending_med_tp) {
Sauw Ming99cc8ff2011-09-22 04:24:56 +00001739 /* We shouldn't use temporary pool anymore. */
1740 call->async_call.pool_prov = NULL;
Sauw Ming73ecfe82011-09-21 10:20:01 +00001741 /* We have a pending media transport initialization. */
1742 pj_log_pop_indent();
1743 return PJ_EPENDING;
Benny Prijono0bc99a92011-03-17 04:34:43 +00001744 }
1745
Sauw Ming73ecfe82011-09-21 10:20:01 +00001746 /* Media transport initialization completed immediately, so
1747 * we don't need to call the callback.
1748 */
1749 call->med_ch_cb = NULL;
1750
1751 status = media_channel_init_cb(call_id, NULL);
1752 if (status != PJ_SUCCESS && sip_err_code)
1753 *sip_err_code = call->med_ch_info.sip_err_code;
1754
Benny Prijonob90fd382011-09-18 14:59:56 +00001755 pj_log_pop_indent();
Sauw Ming73ecfe82011-09-21 10:20:01 +00001756 return status;
Benny Prijonob90fd382011-09-18 14:59:56 +00001757
1758on_error:
Sauw Ming73ecfe82011-09-21 10:20:01 +00001759 if (call->med_ch_mutex) {
1760 pj_mutex_destroy(call->med_ch_mutex);
1761 call->med_ch_mutex = NULL;
1762 }
1763
Benny Prijonob90fd382011-09-18 14:59:56 +00001764 pj_log_pop_indent();
1765 return status;
Benny Prijonoc97608e2007-03-23 16:34:20 +00001766}
1767
1768pj_status_t pjsua_media_channel_create_sdp(pjsua_call_id call_id,
1769 pj_pool_t *pool,
Benny Prijonod8179652008-01-23 20:39:07 +00001770 const pjmedia_sdp_session *rem_sdp,
Benny Prijono25b2ea12008-01-24 19:20:54 +00001771 pjmedia_sdp_session **p_sdp,
Benny Prijono0bc99a92011-03-17 04:34:43 +00001772 int *sip_err_code)
Benny Prijonoc97608e2007-03-23 16:34:20 +00001773{
Benny Prijono0bc99a92011-03-17 04:34:43 +00001774 enum { MAX_MEDIA = PJSUA_MAX_CALL_MEDIA };
Benny Prijonoc97608e2007-03-23 16:34:20 +00001775 pjmedia_sdp_session *sdp;
Benny Prijono0bc99a92011-03-17 04:34:43 +00001776 pj_sockaddr origin;
Benny Prijonoc97608e2007-03-23 16:34:20 +00001777 pjsua_call *call = &pjsua_var.calls[call_id];
Nanang Izzuddin1e952a82010-10-05 16:32:04 +00001778 pjmedia_sdp_neg_state sdp_neg_state = PJMEDIA_SDP_NEG_STATE_NULL;
Benny Prijono0bc99a92011-03-17 04:34:43 +00001779 unsigned mi;
Benny Prijonoc97608e2007-03-23 16:34:20 +00001780 pj_status_t status;
1781
Benny Prijono0bc99a92011-03-17 04:34:43 +00001782 if (pjsua_get_state() != PJSUA_STATE_RUNNING)
Benny Prijono55e82352007-05-10 20:49:08 +00001783 return PJ_EBUSY;
Benny Prijono55e82352007-05-10 20:49:08 +00001784
Benny Prijono0bc99a92011-03-17 04:34:43 +00001785 if (rem_sdp) {
Nanang Izzuddinb6c239c2011-05-10 05:42:28 +00001786 /* If this is a re-offer, let's re-initialize media as remote may
1787 * add or remove media
1788 */
1789 if (call->inv && call->inv->state == PJSIP_INV_STATE_CONFIRMED) {
1790 status = pjsua_media_channel_init(call_id, PJSIP_ROLE_UAS,
1791 call->secure_level, pool,
Sauw Ming73ecfe82011-09-21 10:20:01 +00001792 rem_sdp, sip_err_code,
1793 PJ_FALSE, NULL);
Nanang Izzuddinb6c239c2011-05-10 05:42:28 +00001794 if (status != PJ_SUCCESS)
1795 return status;
Benny Prijono0324ba52010-12-02 10:41:46 +00001796 }
Nanang Izzuddinb6c239c2011-05-10 05:42:28 +00001797
1798#if 0
1799 pjsua_acc *acc = &pjsua_var.acc[call->acc_id];
Benny Prijono0bc99a92011-03-17 04:34:43 +00001800 pj_uint8_t maudidx[PJSUA_MAX_CALL_MEDIA];
1801 unsigned maudcnt = PJ_ARRAY_SIZE(maudidx);
Nanang Izzuddin3150d8b2010-12-01 08:20:28 +00001802
Benny Prijono0bc99a92011-03-17 04:34:43 +00001803 sort_media(rem_sdp, &STR_AUDIO, acc->cfg.use_srtp,
1804 maudidx, &maudcnt);
Nanang Izzuddin3150d8b2010-12-01 08:20:28 +00001805
Benny Prijono0bc99a92011-03-17 04:34:43 +00001806 if (maudcnt==0) {
1807 /* Expecting audio in the offer */
1808 if (sip_err_code) *sip_err_code = PJSIP_SC_NOT_ACCEPTABLE_HERE;
1809 pjsua_media_channel_deinit(call_id);
1810 return PJSIP_ERRNO_FROM_SIP_STATUS(PJSIP_SC_NOT_ACCEPTABLE_HERE);
Benny Prijono224b4e22008-06-19 14:10:28 +00001811 }
Benny Prijono224b4e22008-06-19 14:10:28 +00001812
Benny Prijono0bc99a92011-03-17 04:34:43 +00001813 call->audio_idx = maudidx[0];
Nanang Izzuddinb6c239c2011-05-10 05:42:28 +00001814#endif
Benny Prijono0bc99a92011-03-17 04:34:43 +00001815 } else {
1816 /* Audio is first in our offer, by convention */
Nanang Izzuddinb6c239c2011-05-10 05:42:28 +00001817 // The audio_idx should not be changed here, as this function may be
1818 // called in generating re-offer and the current active audio index
1819 // can be anywhere.
1820 //call->audio_idx = 0;
Benny Prijono224b4e22008-06-19 14:10:28 +00001821 }
1822
Nanang Izzuddinb6c239c2011-05-10 05:42:28 +00001823#if 0
1824 // Since r3512, old-style hold should have got transport, created by
1825 // pjsua_media_channel_init() in initial offer/answer or remote reoffer.
Benny Prijono224b4e22008-06-19 14:10:28 +00001826 /* Create media if it's not created. This could happen when call is
Benny Prijono0bc99a92011-03-17 04:34:43 +00001827 * currently on-hold (with the old style hold)
Benny Prijono224b4e22008-06-19 14:10:28 +00001828 */
Benny Prijono0bc99a92011-03-17 04:34:43 +00001829 if (call->media[call->audio_idx].tp == NULL) {
Benny Prijono224b4e22008-06-19 14:10:28 +00001830 pjsip_role_e role;
1831 role = (rem_sdp ? PJSIP_ROLE_UAS : PJSIP_ROLE_UAC);
1832 status = pjsua_media_channel_init(call_id, role, call->secure_level,
Benny Prijono0bc99a92011-03-17 04:34:43 +00001833 pool, rem_sdp, sip_err_code);
Benny Prijono224b4e22008-06-19 14:10:28 +00001834 if (status != PJ_SUCCESS)
1835 return status;
1836 }
Nanang Izzuddinb6c239c2011-05-10 05:42:28 +00001837#endif
Benny Prijono224b4e22008-06-19 14:10:28 +00001838
Nanang Izzuddin1e952a82010-10-05 16:32:04 +00001839 /* Get SDP negotiator state */
1840 if (call->inv && call->inv->neg)
1841 sdp_neg_state = pjmedia_sdp_neg_get_state(call->inv->neg);
1842
Benny Prijono0bc99a92011-03-17 04:34:43 +00001843 /* Get one address to use in the origin field */
1844 pj_bzero(&origin, sizeof(origin));
1845 for (mi=0; mi<call->med_cnt; ++mi) {
1846 pjmedia_transport_info tpinfo;
Benny Prijono617c5bc2007-04-02 19:51:21 +00001847
Benny Prijono0bc99a92011-03-17 04:34:43 +00001848 if (call->media[mi].tp == NULL)
1849 continue;
1850
1851 pjmedia_transport_info_init(&tpinfo);
1852 pjmedia_transport_get_info(call->media[mi].tp, &tpinfo);
1853 pj_sockaddr_cp(&origin, &tpinfo.sock_info.rtp_addr_name);
1854 break;
Benny Prijono25b2ea12008-01-24 19:20:54 +00001855 }
Benny Prijonoc97608e2007-03-23 16:34:20 +00001856
Benny Prijono0bc99a92011-03-17 04:34:43 +00001857 /* Create the base (blank) SDP */
1858 status = pjmedia_endpt_create_base_sdp(pjsua_var.med_endpt, pool, NULL,
1859 &origin, &sdp);
1860 if (status != PJ_SUCCESS)
1861 return status;
Benny Prijonoa310bd22008-06-27 21:19:44 +00001862
Benny Prijono0bc99a92011-03-17 04:34:43 +00001863 /* Process each media line */
1864 for (mi=0; mi<call->med_cnt; ++mi) {
1865 pjsua_call_media *call_med = &call->media[mi];
1866 pjmedia_sdp_media *m = NULL;
1867 pjmedia_transport_info tpinfo;
1868
Nanang Izzuddinb6c239c2011-05-10 05:42:28 +00001869 if (rem_sdp && mi >= rem_sdp->media_count) {
1870 /* Remote might have removed some media lines. */
1871 break;
Nanang Izzuddin1e952a82010-10-05 16:32:04 +00001872 }
1873
Nanang Izzuddinb6c239c2011-05-10 05:42:28 +00001874 if (call_med->tp == NULL || call_med->tp_st == PJSUA_MED_TP_DISABLED)
1875 {
Benny Prijono0bc99a92011-03-17 04:34:43 +00001876 /*
Nanang Izzuddinb6c239c2011-05-10 05:42:28 +00001877 * This media is disabled. Just create a valid SDP with zero
Benny Prijono0bc99a92011-03-17 04:34:43 +00001878 * port.
1879 */
Nanang Izzuddind8c33d82011-08-18 18:30:55 +00001880 if (rem_sdp) {
1881 /* Just clone the remote media and deactivate it */
1882 m = pjmedia_sdp_media_clone_deactivate(pool,
1883 rem_sdp->media[mi]);
1884 } else {
1885 m = PJ_POOL_ZALLOC_T(pool, pjmedia_sdp_media);
1886 m->desc.transport = pj_str("RTP/AVP");
1887 m->desc.fmt_count = 1;
1888 m->conn = PJ_POOL_ZALLOC_T(pool, pjmedia_sdp_conn);
1889 m->conn->net_type = pj_str("IN");
1890 m->conn->addr_type = pj_str("IP4");
1891 m->conn->addr = pj_str("127.0.0.1");
Benny Prijonoa310bd22008-06-27 21:19:44 +00001892
Nanang Izzuddind8c33d82011-08-18 18:30:55 +00001893 switch (call_med->type) {
1894 case PJMEDIA_TYPE_AUDIO:
1895 m->desc.media = pj_str("audio");
1896 m->desc.fmt[0] = pj_str("0");
1897 break;
1898 case PJMEDIA_TYPE_VIDEO:
1899 m->desc.media = pj_str("video");
1900 m->desc.fmt[0] = pj_str("31");
1901 break;
1902 default:
1903 if (rem_sdp) {
1904 pj_strdup(pool, &m->desc.media,
1905 &rem_sdp->media[mi]->desc.media);
1906 pj_strdup(pool, &m->desc.fmt[0],
1907 &rem_sdp->media[mi]->desc.fmt[0]);
1908 } else {
1909 pj_assert(!"Invalid call_med media type");
1910 return PJ_EBUG;
1911 }
Benny Prijono0bc99a92011-03-17 04:34:43 +00001912 }
Benny Prijonoa310bd22008-06-27 21:19:44 +00001913 }
Benny Prijono0bc99a92011-03-17 04:34:43 +00001914
1915 sdp->media[sdp->media_count++] = m;
1916 continue;
Benny Prijonoa310bd22008-06-27 21:19:44 +00001917 }
1918
Benny Prijono0bc99a92011-03-17 04:34:43 +00001919 /* Get transport address info */
1920 pjmedia_transport_info_init(&tpinfo);
1921 pjmedia_transport_get_info(call_med->tp, &tpinfo);
Benny Prijonoa310bd22008-06-27 21:19:44 +00001922
Benny Prijono0bc99a92011-03-17 04:34:43 +00001923 /* Ask pjmedia endpoint to create SDP media line */
1924 switch (call_med->type) {
1925 case PJMEDIA_TYPE_AUDIO:
1926 status = pjmedia_endpt_create_audio_sdp(pjsua_var.med_endpt, pool,
1927 &tpinfo.sock_info, 0, &m);
1928 break;
Nanang Izzuddin63b3c132011-07-19 11:11:07 +00001929#if defined(PJMEDIA_HAS_VIDEO) && (PJMEDIA_HAS_VIDEO != 0)
Benny Prijono0bc99a92011-03-17 04:34:43 +00001930 case PJMEDIA_TYPE_VIDEO:
1931 status = pjmedia_endpt_create_video_sdp(pjsua_var.med_endpt, pool,
1932 &tpinfo.sock_info, 0, &m);
1933 break;
Nanang Izzuddin63b3c132011-07-19 11:11:07 +00001934#endif
Benny Prijono0bc99a92011-03-17 04:34:43 +00001935 default:
1936 pj_assert(!"Invalid call_med media type");
1937 return PJ_EBUG;
1938 }
Benny Prijonoa310bd22008-06-27 21:19:44 +00001939
Benny Prijono0bc99a92011-03-17 04:34:43 +00001940 if (status != PJ_SUCCESS)
1941 return status;
1942
1943 sdp->media[sdp->media_count++] = m;
1944
1945 /* Give to transport */
1946 status = pjmedia_transport_encode_sdp(call_med->tp, pool,
1947 sdp, rem_sdp, mi);
1948 if (status != PJ_SUCCESS) {
1949 if (sip_err_code) *sip_err_code = PJSIP_SC_NOT_ACCEPTABLE;
1950 return status;
Benny Prijonoa310bd22008-06-27 21:19:44 +00001951 }
Nanang Izzuddin91ba2a22011-04-11 19:59:09 +00001952
1953 /* Copy c= line of the first media to session level,
1954 * if there's none.
1955 */
1956 if (sdp->conn == NULL) {
1957 sdp->conn = pjmedia_sdp_conn_clone(pool, m->conn);
Benny Prijonoa310bd22008-06-27 21:19:44 +00001958 }
1959 }
1960
Benny Prijono6ba8c542007-10-16 01:34:14 +00001961 /* Add NAT info in the SDP */
1962 if (pjsua_var.ua_cfg.nat_type_in_sdp) {
1963 pjmedia_sdp_attr *a;
1964 pj_str_t value;
1965 char nat_info[80];
1966
1967 value.ptr = nat_info;
1968 if (pjsua_var.ua_cfg.nat_type_in_sdp == 1) {
1969 value.slen = pj_ansi_snprintf(nat_info, sizeof(nat_info),
1970 "%d", pjsua_var.nat_type);
1971 } else {
1972 const char *type_name = pj_stun_get_nat_name(pjsua_var.nat_type);
1973 value.slen = pj_ansi_snprintf(nat_info, sizeof(nat_info),
1974 "%d %s",
1975 pjsua_var.nat_type,
1976 type_name);
1977 }
1978
1979 a = pjmedia_sdp_attr_create(pool, "X-nat", &value);
1980
1981 pjmedia_sdp_attr_add(&sdp->attr_count, sdp->attr, a);
1982
1983 }
1984
Benny Prijonoc97608e2007-03-23 16:34:20 +00001985
Benny Prijono0bc99a92011-03-17 04:34:43 +00001986#if DISABLED_FOR_TICKET_1185 && defined(PJMEDIA_HAS_SRTP) && (PJMEDIA_HAS_SRTP != 0)
Nanang Izzuddind89cc3a2010-05-13 05:22:51 +00001987 /* Check if SRTP is in optional mode and configured to use duplicated
1988 * media, i.e: secured and unsecured version, in the SDP offer.
1989 */
1990 if (!rem_sdp &&
1991 pjsua_var.acc[call->acc_id].cfg.use_srtp == PJMEDIA_SRTP_OPTIONAL &&
1992 pjsua_var.acc[call->acc_id].cfg.srtp_optional_dup_offer)
1993 {
1994 unsigned i;
1995
1996 for (i = 0; i < sdp->media_count; ++i) {
1997 pjmedia_sdp_media *m = sdp->media[i];
1998
Benny Prijono0bc99a92011-03-17 04:34:43 +00001999 /* Check if this media is unsecured but has SDP "crypto"
Nanang Izzuddind89cc3a2010-05-13 05:22:51 +00002000 * attribute.
2001 */
2002 if (pj_stricmp2(&m->desc.transport, "RTP/AVP") == 0 &&
2003 pjmedia_sdp_media_find_attr2(m, "crypto", NULL) != NULL)
2004 {
Benny Prijono0bc99a92011-03-17 04:34:43 +00002005 if (i == (unsigned)call->audio_idx &&
Nanang Izzuddin1e952a82010-10-05 16:32:04 +00002006 sdp_neg_state == PJMEDIA_SDP_NEG_STATE_DONE)
2007 {
2008 /* This is a session update, and peer has chosen the
2009 * unsecured version, so let's make this unsecured too.
2010 */
2011 pjmedia_sdp_media_remove_all_attr(m, "crypto");
2012 } else {
2013 /* This is new offer, duplicate media so we'll have
2014 * secured (with "RTP/SAVP" transport) and and unsecured
2015 * versions.
2016 */
2017 pjmedia_sdp_media *new_m;
Nanang Izzuddind89cc3a2010-05-13 05:22:51 +00002018
Nanang Izzuddin1e952a82010-10-05 16:32:04 +00002019 /* Duplicate this media and apply secured transport */
2020 new_m = pjmedia_sdp_media_clone(pool, m);
2021 pj_strdup2(pool, &new_m->desc.transport, "RTP/SAVP");
Nanang Izzuddind89cc3a2010-05-13 05:22:51 +00002022
Nanang Izzuddin1e952a82010-10-05 16:32:04 +00002023 /* Remove the "crypto" attribute in the unsecured media */
2024 pjmedia_sdp_media_remove_all_attr(m, "crypto");
Nanang Izzuddind89cc3a2010-05-13 05:22:51 +00002025
Nanang Izzuddin1e952a82010-10-05 16:32:04 +00002026 /* Insert the new media before the unsecured media */
2027 if (sdp->media_count < PJMEDIA_MAX_SDP_MEDIA) {
Benny Prijono0bc99a92011-03-17 04:34:43 +00002028 pj_array_insert(sdp->media, sizeof(new_m),
Nanang Izzuddin1e952a82010-10-05 16:32:04 +00002029 sdp->media_count, i, &new_m);
2030 ++sdp->media_count;
2031 ++i;
2032 }
Nanang Izzuddind89cc3a2010-05-13 05:22:51 +00002033 }
2034 }
2035 }
2036 }
2037#endif
2038
Benny Prijonoc97608e2007-03-23 16:34:20 +00002039 *p_sdp = sdp;
2040 return PJ_SUCCESS;
Benny Prijonoc97608e2007-03-23 16:34:20 +00002041}
2042
2043
2044static void stop_media_session(pjsua_call_id call_id)
2045{
2046 pjsua_call *call = &pjsua_var.calls[call_id];
Benny Prijono0bc99a92011-03-17 04:34:43 +00002047 unsigned mi;
Benny Prijonoc97608e2007-03-23 16:34:20 +00002048
Benny Prijonob90fd382011-09-18 14:59:56 +00002049 pj_log_push_indent();
2050
Benny Prijono0bc99a92011-03-17 04:34:43 +00002051 for (mi=0; mi<call->med_cnt; ++mi) {
2052 pjsua_call_media *call_med = &call->media[mi];
2053
2054 if (call_med->type == PJMEDIA_TYPE_AUDIO) {
2055 pjmedia_stream *strm = call_med->strm.a.stream;
2056 pjmedia_rtcp_stat stat;
2057
2058 if (strm) {
2059 if (call_med->strm.a.conf_slot != PJSUA_INVALID_ID) {
2060 if (pjsua_var.mconf) {
2061 pjsua_conf_remove_port(call_med->strm.a.conf_slot);
2062 }
2063 call_med->strm.a.conf_slot = PJSUA_INVALID_ID;
2064 }
2065
2066 if ((call_med->dir & PJMEDIA_DIR_ENCODING) &&
2067 (pjmedia_stream_get_stat(strm, &stat) == PJ_SUCCESS))
2068 {
2069 /* Save RTP timestamp & sequence, so when media session is
2070 * restarted, those values will be restored as the initial
2071 * RTP timestamp & sequence of the new media session. So in
2072 * the same call session, RTP timestamp and sequence are
2073 * guaranteed to be contigue.
2074 */
2075 call_med->rtp_tx_seq_ts_set = 1 | (1 << 1);
2076 call_med->rtp_tx_seq = stat.rtp_tx_last_seq;
2077 call_med->rtp_tx_ts = stat.rtp_tx_last_ts;
2078 }
2079
2080 if (pjsua_var.ua_cfg.cb.on_stream_destroyed) {
2081 pjsua_var.ua_cfg.cb.on_stream_destroyed(call_id, strm, mi);
2082 }
2083
2084 pjmedia_stream_destroy(strm);
2085 call_med->strm.a.stream = NULL;
Nanang Izzuddinbf26db12011-03-18 07:54:50 +00002086 }
Nanang Izzuddinfd461eb2008-06-09 09:35:59 +00002087 }
Nanang Izzuddin50fae732011-03-22 09:49:23 +00002088
2089#if PJMEDIA_HAS_VIDEO
2090 else if (call_med->type == PJMEDIA_TYPE_VIDEO) {
Nanang Izzuddin62053a62011-07-12 11:08:32 +00002091 stop_video_stream(call_med);
Benny Prijonoc97608e2007-03-23 16:34:20 +00002092 }
Nanang Izzuddin50fae732011-03-22 09:49:23 +00002093#endif
Nanang Izzuddinbf26db12011-03-18 07:54:50 +00002094
2095 PJ_LOG(4,(THIS_FILE, "Media session call%02d:%d is destroyed",
2096 call_id, mi));
Benny Prijono0bc99a92011-03-17 04:34:43 +00002097 call_med->state = PJSUA_CALL_MEDIA_NONE;
Benny Prijonoc97608e2007-03-23 16:34:20 +00002098 }
Benny Prijonob90fd382011-09-18 14:59:56 +00002099
2100 pj_log_pop_indent();
Benny Prijonoc97608e2007-03-23 16:34:20 +00002101}
2102
2103pj_status_t pjsua_media_channel_deinit(pjsua_call_id call_id)
2104{
2105 pjsua_call *call = &pjsua_var.calls[call_id];
Benny Prijono0bc99a92011-03-17 04:34:43 +00002106 unsigned mi;
Benny Prijonoc97608e2007-03-23 16:34:20 +00002107
Benny Prijonob90fd382011-09-18 14:59:56 +00002108 PJ_LOG(4,(THIS_FILE, "Call %d: deinitializing media..", call_id));
2109 pj_log_push_indent();
2110
Benny Prijonoc97608e2007-03-23 16:34:20 +00002111 stop_media_session(call_id);
2112
Benny Prijono0bc99a92011-03-17 04:34:43 +00002113 for (mi=0; mi<call->med_cnt; ++mi) {
2114 pjsua_call_media *call_med = &call->media[mi];
Benny Prijonoc97608e2007-03-23 16:34:20 +00002115
Sauw Ming73ecfe82011-09-21 10:20:01 +00002116 if (call_med->tp_st > PJSUA_MED_TP_IDLE) {
Benny Prijono0bc99a92011-03-17 04:34:43 +00002117 pjmedia_transport_media_stop(call_med->tp);
Sauw Ming73ecfe82011-09-21 10:20:01 +00002118 set_media_tp_state(call_med, PJSUA_MED_TP_IDLE);
Benny Prijono0bc99a92011-03-17 04:34:43 +00002119 }
2120
Nanang Izzuddinb6c239c2011-05-10 05:42:28 +00002121 //if (call_med->tp_orig && call_med->tp &&
2122 // call_med->tp != call_med->tp_orig)
2123 //{
2124 // pjmedia_transport_close(call_med->tp);
2125 // call_med->tp = call_med->tp_orig;
2126 //}
2127 if (call_med->tp) {
Benny Prijono0bc99a92011-03-17 04:34:43 +00002128 pjmedia_transport_close(call_med->tp);
Nanang Izzuddinb6c239c2011-05-10 05:42:28 +00002129 call_med->tp = call_med->tp_orig = NULL;
Benny Prijono0bc99a92011-03-17 04:34:43 +00002130 }
Benny Prijonod8179652008-01-23 20:39:07 +00002131 }
Nanang Izzuddin670f71b2009-01-20 14:05:54 +00002132
2133 check_snd_dev_idle();
Benny Prijonob90fd382011-09-18 14:59:56 +00002134 pj_log_pop_indent();
Nanang Izzuddin670f71b2009-01-20 14:05:54 +00002135
Benny Prijonoc97608e2007-03-23 16:34:20 +00002136 return PJ_SUCCESS;
2137}
2138
2139
2140/*
2141 * DTMF callback from the stream.
2142 */
2143static void dtmf_callback(pjmedia_stream *strm, void *user_data,
2144 int digit)
2145{
2146 PJ_UNUSED_ARG(strm);
2147
Benny Prijonob90fd382011-09-18 14:59:56 +00002148 pj_log_push_indent();
2149
Benny Prijono0c068262008-02-14 14:38:52 +00002150 /* For discussions about call mutex protection related to this
2151 * callback, please see ticket #460:
2152 * http://trac.pjsip.org/repos/ticket/460#comment:4
2153 */
Benny Prijonoc97608e2007-03-23 16:34:20 +00002154 if (pjsua_var.ua_cfg.cb.on_dtmf_digit) {
2155 pjsua_call_id call_id;
2156
Benny Prijonod8179652008-01-23 20:39:07 +00002157 call_id = (pjsua_call_id)(long)user_data;
Benny Prijonoc97608e2007-03-23 16:34:20 +00002158 pjsua_var.ua_cfg.cb.on_dtmf_digit(call_id, digit);
2159 }
Benny Prijonob90fd382011-09-18 14:59:56 +00002160
2161 pj_log_pop_indent();
Benny Prijonoc97608e2007-03-23 16:34:20 +00002162}
2163
2164
Benny Prijono0bc99a92011-03-17 04:34:43 +00002165static pj_status_t audio_channel_update(pjsua_call_media *call_med,
2166 pj_pool_t *tmp_pool,
2167 const pjmedia_sdp_session *local_sdp,
2168 const pjmedia_sdp_session *remote_sdp)
Benny Prijonoc97608e2007-03-23 16:34:20 +00002169{
Benny Prijono0bc99a92011-03-17 04:34:43 +00002170 pjsua_call *call = call_med->call;
2171 pjmedia_stream_info the_si, *si = &the_si;
Benny Prijonoc97608e2007-03-23 16:34:20 +00002172 pjmedia_port *media_port;
Benny Prijono0bc99a92011-03-17 04:34:43 +00002173 unsigned strm_idx = call_med->idx;
Benny Prijonoc97608e2007-03-23 16:34:20 +00002174 pj_status_t status;
Benny Prijonob90fd382011-09-18 14:59:56 +00002175
2176 PJ_LOG(4,(THIS_FILE,"Audio channel update.."));
2177 pj_log_push_indent();
Benny Prijono0bc99a92011-03-17 04:34:43 +00002178
2179 status = pjmedia_stream_info_from_sdp(si, tmp_pool, pjsua_var.med_endpt,
2180 local_sdp, remote_sdp, strm_idx);
Benny Prijonoc97608e2007-03-23 16:34:20 +00002181 if (status != PJ_SUCCESS)
Benny Prijonob90fd382011-09-18 14:59:56 +00002182 goto on_return;
Benny Prijonoc97608e2007-03-23 16:34:20 +00002183
Nanang Izzuddin99d69522008-08-04 15:01:38 +00002184 /* Check if no media is active */
Benny Prijono0bc99a92011-03-17 04:34:43 +00002185 if (si->dir == PJMEDIA_DIR_NONE) {
Nanang Izzuddin99d69522008-08-04 15:01:38 +00002186 /* Call media state */
Benny Prijono0bc99a92011-03-17 04:34:43 +00002187 call_med->state = PJSUA_CALL_MEDIA_NONE;
Benny Prijonoc97608e2007-03-23 16:34:20 +00002188
Nanang Izzuddin99d69522008-08-04 15:01:38 +00002189 /* Call media direction */
Benny Prijono0bc99a92011-03-17 04:34:43 +00002190 call_med->dir = PJMEDIA_DIR_NONE;
Benny Prijono68f9e4f2008-03-21 08:56:02 +00002191
Benny Prijonoc97608e2007-03-23 16:34:20 +00002192 } else {
Nanang Izzuddin4375f902008-06-26 19:12:09 +00002193 pjmedia_transport_info tp_info;
2194
Benny Prijono224b4e22008-06-19 14:10:28 +00002195 /* Start/restart media transport */
Benny Prijono0bc99a92011-03-17 04:34:43 +00002196 status = pjmedia_transport_media_start(call_med->tp,
2197 tmp_pool, local_sdp,
2198 remote_sdp, strm_idx);
Benny Prijonod8179652008-01-23 20:39:07 +00002199 if (status != PJ_SUCCESS)
Benny Prijonob90fd382011-09-18 14:59:56 +00002200 goto on_return;
Benny Prijonoc97608e2007-03-23 16:34:20 +00002201
Sauw Ming73ecfe82011-09-21 10:20:01 +00002202 set_media_tp_state(call_med, PJSUA_MED_TP_RUNNING);
Benny Prijono224b4e22008-06-19 14:10:28 +00002203
Nanang Izzuddin4375f902008-06-26 19:12:09 +00002204 /* Get remote SRTP usage policy */
2205 pjmedia_transport_info_init(&tp_info);
Benny Prijono0bc99a92011-03-17 04:34:43 +00002206 pjmedia_transport_get_info(call_med->tp, &tp_info);
Nanang Izzuddin4375f902008-06-26 19:12:09 +00002207 if (tp_info.specific_info_cnt > 0) {
Benny Prijonof5d9f1f2009-10-14 13:13:18 +00002208 unsigned i;
Nanang Izzuddin4375f902008-06-26 19:12:09 +00002209 for (i = 0; i < tp_info.specific_info_cnt; ++i) {
2210 if (tp_info.spc_info[i].type == PJMEDIA_TRANSPORT_TYPE_SRTP)
2211 {
2212 pjmedia_srtp_info *srtp_info =
2213 (pjmedia_srtp_info*) tp_info.spc_info[i].buffer;
2214
Benny Prijono0bc99a92011-03-17 04:34:43 +00002215 call_med->rem_srtp_use = srtp_info->peer_use;
Nanang Izzuddin4375f902008-06-26 19:12:09 +00002216 break;
2217 }
2218 }
2219 }
2220
Benny Prijonoc97608e2007-03-23 16:34:20 +00002221 /* Override ptime, if this option is specified. */
2222 if (pjsua_var.media_cfg.ptime != 0) {
Benny Prijono91e567e2007-12-28 08:51:58 +00002223 si->param->setting.frm_per_pkt = (pj_uint8_t)
2224 (pjsua_var.media_cfg.ptime / si->param->info.frm_ptime);
2225 if (si->param->setting.frm_per_pkt == 0)
2226 si->param->setting.frm_per_pkt = 1;
Benny Prijonoc97608e2007-03-23 16:34:20 +00002227 }
2228
2229 /* Disable VAD, if this option is specified. */
2230 if (pjsua_var.media_cfg.no_vad) {
Benny Prijono91e567e2007-12-28 08:51:58 +00002231 si->param->setting.vad = 0;
Benny Prijonoc97608e2007-03-23 16:34:20 +00002232 }
2233
2234
2235 /* Optionally, application may modify other stream settings here
2236 * (such as jitter buffer parameters, codec ptime, etc.)
2237 */
Benny Prijono91e567e2007-12-28 08:51:58 +00002238 si->jb_init = pjsua_var.media_cfg.jb_init;
2239 si->jb_min_pre = pjsua_var.media_cfg.jb_min_pre;
2240 si->jb_max_pre = pjsua_var.media_cfg.jb_max_pre;
2241 si->jb_max = pjsua_var.media_cfg.jb_max;
Benny Prijonoc97608e2007-03-23 16:34:20 +00002242
Benny Prijono8147f402007-11-21 14:50:07 +00002243 /* Set SSRC */
Benny Prijono0bc99a92011-03-17 04:34:43 +00002244 si->ssrc = call_med->ssrc;
Benny Prijono8147f402007-11-21 14:50:07 +00002245
Nanang Izzuddina815ceb2008-08-26 16:51:28 +00002246 /* Set RTP timestamp & sequence, normally these value are intialized
2247 * automatically when stream session created, but for some cases (e.g:
2248 * call reinvite, call update) timestamp and sequence need to be kept
2249 * contigue.
2250 */
Benny Prijono0bc99a92011-03-17 04:34:43 +00002251 si->rtp_ts = call_med->rtp_tx_ts;
2252 si->rtp_seq = call_med->rtp_tx_seq;
2253 si->rtp_seq_ts_set = call_med->rtp_tx_seq_ts_set;
Nanang Izzuddina815ceb2008-08-26 16:51:28 +00002254
Nanang Izzuddin5e39a2b2010-09-20 06:13:02 +00002255#if defined(PJMEDIA_STREAM_ENABLE_KA) && PJMEDIA_STREAM_ENABLE_KA!=0
2256 /* Enable/disable stream keep-alive and NAT hole punch. */
2257 si->use_ka = pjsua_var.acc[call->acc_id].cfg.use_stream_ka;
2258#endif
2259
Benny Prijonoc97608e2007-03-23 16:34:20 +00002260 /* Create session based on session info. */
Benny Prijono0bc99a92011-03-17 04:34:43 +00002261 status = pjmedia_stream_create(pjsua_var.med_endpt, NULL, si,
2262 call_med->tp, NULL,
2263 &call_med->strm.a.stream);
2264 if (status != PJ_SUCCESS) {
Benny Prijonob90fd382011-09-18 14:59:56 +00002265 goto on_return;
Benny Prijono0bc99a92011-03-17 04:34:43 +00002266 }
2267
2268 /* Start stream */
2269 status = pjmedia_stream_start(call_med->strm.a.stream);
Benny Prijonoc97608e2007-03-23 16:34:20 +00002270 if (status != PJ_SUCCESS) {
Benny Prijonob90fd382011-09-18 14:59:56 +00002271 goto on_return;
Benny Prijonoc97608e2007-03-23 16:34:20 +00002272 }
2273
2274 /* If DTMF callback is installed by application, install our
2275 * callback to the session.
2276 */
2277 if (pjsua_var.ua_cfg.cb.on_dtmf_digit) {
Benny Prijono0bc99a92011-03-17 04:34:43 +00002278 pjmedia_stream_set_dtmf_callback(call_med->strm.a.stream,
2279 &dtmf_callback,
2280 (void*)(long)(call->index));
Benny Prijonoc97608e2007-03-23 16:34:20 +00002281 }
2282
2283 /* Get the port interface of the first stream in the session.
2284 * We need the port interface to add to the conference bridge.
2285 */
Benny Prijono0bc99a92011-03-17 04:34:43 +00002286 pjmedia_stream_get_port(call_med->strm.a.stream, &media_port);
Benny Prijonoc97608e2007-03-23 16:34:20 +00002287
Benny Prijonofc13bf62008-02-20 08:56:15 +00002288 /* Notify application about stream creation.
2289 * Note: application may modify media_port to point to different
2290 * media port
2291 */
2292 if (pjsua_var.ua_cfg.cb.on_stream_created) {
Benny Prijono0bc99a92011-03-17 04:34:43 +00002293 pjsua_var.ua_cfg.cb.on_stream_created(call->index,
2294 call_med->strm.a.stream,
2295 strm_idx, &media_port);
Benny Prijonofc13bf62008-02-20 08:56:15 +00002296 }
Benny Prijonoc97608e2007-03-23 16:34:20 +00002297
2298 /*
2299 * Add the call to conference bridge.
2300 */
Benny Prijono38fb3ea2008-01-02 08:27:03 +00002301 {
2302 char tmp[PJSIP_MAX_URL_SIZE];
2303 pj_str_t port_name;
2304
2305 port_name.ptr = tmp;
2306 port_name.slen = pjsip_uri_print(PJSIP_URI_IN_REQ_URI,
2307 call->inv->dlg->remote.info->uri,
2308 tmp, sizeof(tmp));
2309 if (port_name.slen < 1) {
2310 port_name = pj_str("call");
2311 }
Benny Prijono40d62b62009-08-12 17:53:47 +00002312 status = pjmedia_conf_add_port( pjsua_var.mconf,
2313 call->inv->pool_prov,
Benny Prijono38fb3ea2008-01-02 08:27:03 +00002314 media_port,
2315 &port_name,
Benny Prijono0bc99a92011-03-17 04:34:43 +00002316 (unsigned*)
2317 &call_med->strm.a.conf_slot);
Benny Prijono38fb3ea2008-01-02 08:27:03 +00002318 if (status != PJ_SUCCESS) {
Benny Prijonob90fd382011-09-18 14:59:56 +00002319 goto on_return;
Benny Prijono38fb3ea2008-01-02 08:27:03 +00002320 }
Benny Prijonoc97608e2007-03-23 16:34:20 +00002321 }
2322
Nanang Izzuddin99d69522008-08-04 15:01:38 +00002323 /* Call media direction */
Benny Prijono0bc99a92011-03-17 04:34:43 +00002324 call_med->dir = si->dir;
Nanang Izzuddin99d69522008-08-04 15:01:38 +00002325
2326 /* Call media state */
2327 if (call->local_hold)
Benny Prijono0bc99a92011-03-17 04:34:43 +00002328 call_med->state = PJSUA_CALL_MEDIA_LOCAL_HOLD;
2329 else if (call_med->dir == PJMEDIA_DIR_DECODING)
2330 call_med->state = PJSUA_CALL_MEDIA_REMOTE_HOLD;
Nanang Izzuddin99d69522008-08-04 15:01:38 +00002331 else
Benny Prijono0bc99a92011-03-17 04:34:43 +00002332 call_med->state = PJSUA_CALL_MEDIA_ACTIVE;
Benny Prijonoc97608e2007-03-23 16:34:20 +00002333 }
2334
2335 /* Print info. */
2336 {
2337 char info[80];
2338 int info_len = 0;
Benny Prijono0bc99a92011-03-17 04:34:43 +00002339 int len;
2340 const char *dir;
Benny Prijonoc97608e2007-03-23 16:34:20 +00002341
Benny Prijono0bc99a92011-03-17 04:34:43 +00002342 switch (si->dir) {
2343 case PJMEDIA_DIR_NONE:
2344 dir = "inactive";
2345 break;
2346 case PJMEDIA_DIR_ENCODING:
2347 dir = "sendonly";
2348 break;
2349 case PJMEDIA_DIR_DECODING:
2350 dir = "recvonly";
2351 break;
2352 case PJMEDIA_DIR_ENCODING_DECODING:
2353 dir = "sendrecv";
2354 break;
2355 default:
2356 dir = "unknown";
2357 break;
Benny Prijonoc97608e2007-03-23 16:34:20 +00002358 }
Benny Prijono0bc99a92011-03-17 04:34:43 +00002359 len = pj_ansi_sprintf( info+info_len,
2360 ", stream #%d: %.*s (%s)", strm_idx,
2361 (int)si->fmt.encoding_name.slen,
2362 si->fmt.encoding_name.ptr,
2363 dir);
2364 if (len > 0)
2365 info_len += len;
Benny Prijonob90fd382011-09-18 14:59:56 +00002366 PJ_LOG(4,(THIS_FILE,"Audio updated%s", info));
Benny Prijonoc97608e2007-03-23 16:34:20 +00002367 }
2368
Benny Prijonob90fd382011-09-18 14:59:56 +00002369on_return:
2370 pj_log_pop_indent();
2371 return status;
Benny Prijonoc97608e2007-03-23 16:34:20 +00002372}
2373
Benny Prijono0bc99a92011-03-17 04:34:43 +00002374pj_status_t pjsua_media_channel_update(pjsua_call_id call_id,
2375 const pjmedia_sdp_session *local_sdp,
2376 const pjmedia_sdp_session *remote_sdp)
2377{
2378 pjsua_call *call = &pjsua_var.calls[call_id];
Nanang Izzuddind8c33d82011-08-18 18:30:55 +00002379 pjsua_acc *acc = &pjsua_var.acc[call->acc_id];
Benny Prijono0bc99a92011-03-17 04:34:43 +00002380 pj_pool_t *tmp_pool = call->inv->pool_prov;
2381 unsigned mi;
Nanang Izzuddinb6c239c2011-05-10 05:42:28 +00002382 pj_bool_t got_media = PJ_FALSE;
Nanang Izzuddinbf26db12011-03-18 07:54:50 +00002383 pj_status_t status = PJ_SUCCESS;
Benny Prijono0bc99a92011-03-17 04:34:43 +00002384
Nanang Izzuddind8c33d82011-08-18 18:30:55 +00002385 const pj_str_t STR_AUDIO = { "audio", 5 };
2386 const pj_str_t STR_VIDEO = { "video", 5 };
2387 pj_uint8_t maudidx[PJSUA_MAX_CALL_MEDIA];
2388 unsigned maudcnt = PJ_ARRAY_SIZE(maudidx);
2389 pj_uint8_t mvididx[PJSUA_MAX_CALL_MEDIA];
2390 unsigned mvidcnt = PJ_ARRAY_SIZE(mvididx);
2391 pj_bool_t need_renego_sdp = PJ_FALSE;
2392
Benny Prijono0bc99a92011-03-17 04:34:43 +00002393 if (pjsua_get_state() != PJSUA_STATE_RUNNING)
2394 return PJ_EBUSY;
2395
Benny Prijonob90fd382011-09-18 14:59:56 +00002396 PJ_LOG(4,(THIS_FILE, "Call %d: updating media..", call_id));
2397 pj_log_push_indent();
2398
Benny Prijono0bc99a92011-03-17 04:34:43 +00002399 /* Destroy existing media session, if any. */
2400 stop_media_session(call->index);
2401
Nanang Izzuddind8c33d82011-08-18 18:30:55 +00002402 /* Call media count must be at least equal to SDP media. Note that
2403 * it may not be equal when remote removed any SDP media line.
2404 */
2405 pj_assert(call->med_cnt >= local_sdp->media_count);
2406
Benny Prijono0bc99a92011-03-17 04:34:43 +00002407 /* Reset audio_idx first */
2408 call->audio_idx = -1;
2409
Nanang Izzuddind8c33d82011-08-18 18:30:55 +00002410 /* Apply maximum audio/video count of the account */
2411 sort_media(local_sdp, &STR_AUDIO, acc->cfg.use_srtp,
2412 maudidx, &maudcnt);
2413#if PJMEDIA_HAS_VIDEO
2414 sort_media(local_sdp, &STR_VIDEO, acc->cfg.use_srtp,
2415 mvididx, &mvidcnt);
2416#else
2417 PJ_UNUSED_ARG(STR_VIDEO);
2418 mvidcnt = 0;
2419#endif
2420 if (maudcnt > acc->cfg.max_audio_cnt || mvidcnt > acc->cfg.max_video_cnt)
2421 {
2422 pjmedia_sdp_session *local_sdp2;
2423
2424 maudcnt = PJ_MIN(maudcnt, acc->cfg.max_audio_cnt);
2425 mvidcnt = PJ_MIN(mvidcnt, acc->cfg.max_video_cnt);
2426 local_sdp2 = pjmedia_sdp_session_clone(tmp_pool, local_sdp);
2427
2428 for (mi=0; mi < local_sdp2->media_count; ++mi) {
2429 pjmedia_sdp_media *m = local_sdp2->media[mi];
2430
2431 if (m->desc.port == 0 ||
2432 pj_memchr(maudidx, mi, maudcnt*sizeof(maudidx[0])) ||
2433 pj_memchr(mvididx, mi, mvidcnt*sizeof(mvididx[0])))
2434 {
2435 continue;
2436 }
2437
2438 /* Deactivate this media */
2439 pjmedia_sdp_media_deactivate(tmp_pool, m);
2440 }
2441
2442 local_sdp = local_sdp2;
2443 need_renego_sdp = PJ_TRUE;
2444 }
2445
Benny Prijono0bc99a92011-03-17 04:34:43 +00002446 /* Process each media stream */
2447 for (mi=0; mi < call->med_cnt; ++mi) {
2448 pjsua_call_media *call_med = &call->media[mi];
2449
Nanang Izzuddinb6c239c2011-05-10 05:42:28 +00002450 if (mi >= local_sdp->media_count ||
2451 mi >= remote_sdp->media_count)
Benny Prijono0bc99a92011-03-17 04:34:43 +00002452 {
Nanang Izzuddinb6c239c2011-05-10 05:42:28 +00002453 /* This may happen when remote removed any SDP media lines in
2454 * its re-offer.
2455 */
2456 continue;
2457#if 0
Benny Prijono0bc99a92011-03-17 04:34:43 +00002458 /* Something is wrong */
2459 PJ_LOG(1,(THIS_FILE, "Error updating media for call %d: "
2460 "invalid media index %d in SDP", call_id, mi));
Benny Prijonob90fd382011-09-18 14:59:56 +00002461 status = PJMEDIA_SDP_EINSDP;
2462 goto on_error;
Nanang Izzuddinb6c239c2011-05-10 05:42:28 +00002463#endif
Benny Prijono0bc99a92011-03-17 04:34:43 +00002464 }
2465
2466 switch (call_med->type) {
2467 case PJMEDIA_TYPE_AUDIO:
2468 status = audio_channel_update(call_med, tmp_pool,
2469 local_sdp, remote_sdp);
2470 if (call->audio_idx==-1 && status==PJ_SUCCESS &&
Nanang Izzuddinbf26db12011-03-18 07:54:50 +00002471 call_med->strm.a.stream)
Benny Prijono0bc99a92011-03-17 04:34:43 +00002472 {
2473 call->audio_idx = mi;
2474 }
2475 break;
Nanang Izzuddin63b3c132011-07-19 11:11:07 +00002476#if defined(PJMEDIA_HAS_VIDEO) && (PJMEDIA_HAS_VIDEO != 0)
Benny Prijono0bc99a92011-03-17 04:34:43 +00002477 case PJMEDIA_TYPE_VIDEO:
Nanang Izzuddinbf26db12011-03-18 07:54:50 +00002478 status = video_channel_update(call_med, tmp_pool,
2479 local_sdp, remote_sdp);
Benny Prijono0bc99a92011-03-17 04:34:43 +00002480 break;
Nanang Izzuddin50fae732011-03-22 09:49:23 +00002481#endif
Benny Prijono0bc99a92011-03-17 04:34:43 +00002482 default:
Nanang Izzuddinb6c239c2011-05-10 05:42:28 +00002483 status = PJMEDIA_EINVALIMEDIATYPE;
Benny Prijono0bc99a92011-03-17 04:34:43 +00002484 break;
2485 }
2486
Nanang Izzuddind8c33d82011-08-18 18:30:55 +00002487 /* Close the transport of deactivated media, need this here as media
2488 * can be deactivated by the SDP negotiation and the max media count
2489 * (account) setting.
2490 */
2491 if (local_sdp->media[mi]->desc.port==0 && call_med->tp) {
2492 pjmedia_transport_close(call_med->tp);
2493 call_med->tp = call_med->tp_orig = NULL;
Sauw Ming73ecfe82011-09-21 10:20:01 +00002494 set_media_tp_state(call_med, PJSUA_MED_TP_IDLE);
Nanang Izzuddind8c33d82011-08-18 18:30:55 +00002495 }
2496
Benny Prijono0bc99a92011-03-17 04:34:43 +00002497 if (status != PJ_SUCCESS) {
Nanang Izzuddinb6c239c2011-05-10 05:42:28 +00002498 PJ_PERROR(1,(THIS_FILE, status, "Error updating media call%02d:%d",
Benny Prijono0bc99a92011-03-17 04:34:43 +00002499 call_id, mi));
Nanang Izzuddinb6c239c2011-05-10 05:42:28 +00002500 } else {
2501 got_media = PJ_TRUE;
Benny Prijono0bc99a92011-03-17 04:34:43 +00002502 }
2503 }
2504
Nanang Izzuddind8c33d82011-08-18 18:30:55 +00002505 /* Perform SDP re-negotiation if needed. */
2506 if (got_media && need_renego_sdp) {
2507 pjmedia_sdp_neg *neg = call->inv->neg;
2508
2509 /* This should only happen when we are the answerer. */
2510 PJ_ASSERT_RETURN(neg && !pjmedia_sdp_neg_was_answer_remote(neg),
2511 PJMEDIA_SDPNEG_EINSTATE);
2512
2513 status = pjmedia_sdp_neg_set_remote_offer(tmp_pool, neg, remote_sdp);
2514 if (status != PJ_SUCCESS)
Benny Prijonob90fd382011-09-18 14:59:56 +00002515 goto on_error;
Nanang Izzuddind8c33d82011-08-18 18:30:55 +00002516
2517 status = pjmedia_sdp_neg_set_local_answer(tmp_pool, neg, local_sdp);
2518 if (status != PJ_SUCCESS)
Benny Prijonob90fd382011-09-18 14:59:56 +00002519 goto on_error;
Nanang Izzuddind8c33d82011-08-18 18:30:55 +00002520
2521 status = pjmedia_sdp_neg_negotiate(tmp_pool, neg, 0);
2522 if (status != PJ_SUCCESS)
Benny Prijonob90fd382011-09-18 14:59:56 +00002523 goto on_error;
Nanang Izzuddind8c33d82011-08-18 18:30:55 +00002524 }
2525
Benny Prijonob90fd382011-09-18 14:59:56 +00002526 pj_log_pop_indent();
Nanang Izzuddinb6c239c2011-05-10 05:42:28 +00002527 return (got_media? PJ_SUCCESS : PJMEDIA_SDPNEG_ENOMEDIA);
Benny Prijonob90fd382011-09-18 14:59:56 +00002528
2529on_error:
2530 pj_log_pop_indent();
2531 return status;
Benny Prijono0bc99a92011-03-17 04:34:43 +00002532}
2533
Benny Prijonoeebe9af2006-06-13 22:57:13 +00002534/*
2535 * Get maxinum number of conference ports.
2536 */
2537PJ_DEF(unsigned) pjsua_conf_get_max_ports(void)
2538{
2539 return pjsua_var.media_cfg.max_media_ports;
2540}
2541
2542
2543/*
2544 * Get current number of active ports in the bridge.
2545 */
2546PJ_DEF(unsigned) pjsua_conf_get_active_ports(void)
2547{
Benny Prijono38fb3ea2008-01-02 08:27:03 +00002548 unsigned ports[PJSUA_MAX_CONF_PORTS];
Benny Prijonoeebe9af2006-06-13 22:57:13 +00002549 unsigned count = PJ_ARRAY_SIZE(ports);
2550 pj_status_t status;
2551
2552 status = pjmedia_conf_enum_ports(pjsua_var.mconf, ports, &count);
2553 if (status != PJ_SUCCESS)
2554 count = 0;
2555
2556 return count;
2557}
2558
2559
2560/*
2561 * Enumerate all conference ports.
2562 */
2563PJ_DEF(pj_status_t) pjsua_enum_conf_ports(pjsua_conf_port_id id[],
2564 unsigned *count)
2565{
2566 return pjmedia_conf_enum_ports(pjsua_var.mconf, (unsigned*)id, count);
2567}
2568
2569
2570/*
2571 * Get information about the specified conference port
2572 */
2573PJ_DEF(pj_status_t) pjsua_conf_get_port_info( pjsua_conf_port_id id,
2574 pjsua_conf_port_info *info)
2575{
2576 pjmedia_conf_port_info cinfo;
Benny Prijono0498d902006-06-19 14:49:14 +00002577 unsigned i;
Benny Prijonoeebe9af2006-06-13 22:57:13 +00002578 pj_status_t status;
2579
2580 status = pjmedia_conf_get_port_info( pjsua_var.mconf, id, &cinfo);
2581 if (status != PJ_SUCCESS)
2582 return status;
2583
Benny Prijonoac623b32006-07-03 15:19:31 +00002584 pj_bzero(info, sizeof(*info));
Benny Prijonoeebe9af2006-06-13 22:57:13 +00002585 info->slot_id = id;
2586 info->name = cinfo.name;
2587 info->clock_rate = cinfo.clock_rate;
2588 info->channel_count = cinfo.channel_count;
2589 info->samples_per_frame = cinfo.samples_per_frame;
2590 info->bits_per_sample = cinfo.bits_per_sample;
2591
2592 /* Build array of listeners */
Benny Prijonoc78c3a32006-06-16 15:54:43 +00002593 info->listener_cnt = cinfo.listener_cnt;
2594 for (i=0; i<cinfo.listener_cnt; ++i) {
2595 info->listeners[i] = cinfo.listener_slots[i];
Benny Prijonoeebe9af2006-06-13 22:57:13 +00002596 }
2597
2598 return PJ_SUCCESS;
2599}
2600
2601
2602/*
Benny Prijonoe909eac2006-07-27 22:04:56 +00002603 * Add arbitrary media port to PJSUA's conference bridge.
2604 */
2605PJ_DEF(pj_status_t) pjsua_conf_add_port( pj_pool_t *pool,
2606 pjmedia_port *port,
2607 pjsua_conf_port_id *p_id)
2608{
2609 pj_status_t status;
2610
2611 status = pjmedia_conf_add_port(pjsua_var.mconf, pool,
2612 port, NULL, (unsigned*)p_id);
2613 if (status != PJ_SUCCESS) {
2614 if (p_id)
2615 *p_id = PJSUA_INVALID_ID;
2616 }
2617
2618 return status;
2619}
2620
2621
2622/*
2623 * Remove arbitrary slot from the conference bridge.
2624 */
2625PJ_DEF(pj_status_t) pjsua_conf_remove_port(pjsua_conf_port_id id)
2626{
Nanang Izzuddin148fd392008-06-16 09:52:50 +00002627 pj_status_t status;
2628
2629 status = pjmedia_conf_remove_port(pjsua_var.mconf, (unsigned)id);
2630 check_snd_dev_idle();
2631
2632 return status;
Benny Prijonoe909eac2006-07-27 22:04:56 +00002633}
2634
2635
2636/*
Benny Prijonoeebe9af2006-06-13 22:57:13 +00002637 * Establish unidirectional media flow from souce to sink.
2638 */
2639PJ_DEF(pj_status_t) pjsua_conf_connect( pjsua_conf_port_id source,
2640 pjsua_conf_port_id sink)
2641{
Benny Prijonob90fd382011-09-18 14:59:56 +00002642 pj_status_t status = PJ_SUCCESS;
2643
2644 PJ_LOG(4,(THIS_FILE, "%s connect: %d --> %d",
2645 (pjsua_var.is_mswitch ? "Switch" : "Conf"),
2646 source, sink));
2647 pj_log_push_indent();
2648
Nanang Izzuddin68559c32008-06-13 17:01:46 +00002649 /* If sound device idle timer is active, cancel it first. */
Benny Prijono0f711b42009-05-06 19:08:43 +00002650 PJSUA_LOCK();
Nanang Izzuddin68559c32008-06-13 17:01:46 +00002651 if (pjsua_var.snd_idle_timer.id) {
2652 pjsip_endpt_cancel_timer(pjsua_var.endpt, &pjsua_var.snd_idle_timer);
2653 pjsua_var.snd_idle_timer.id = PJ_FALSE;
2654 }
Benny Prijono0f711b42009-05-06 19:08:43 +00002655 PJSUA_UNLOCK();
Nanang Izzuddin68559c32008-06-13 17:01:46 +00002656
Nanang Izzuddinfe02a062009-02-18 14:28:49 +00002657
Benny Prijonof798e502009-03-09 13:08:16 +00002658 /* For audio switchboard (i.e. APS-Direct):
2659 * Check if sound device need to be reopened, i.e: its attributes
Nanang Izzuddinfe02a062009-02-18 14:28:49 +00002660 * (format, clock rate, channel count) must match to peer's.
2661 * Note that sound device can be reopened only if it doesn't have
2662 * any connection.
2663 */
Benny Prijonof798e502009-03-09 13:08:16 +00002664 if (pjsua_var.is_mswitch) {
Nanang Izzuddinfe02a062009-02-18 14:28:49 +00002665 pjmedia_conf_port_info port0_info;
2666 pjmedia_conf_port_info peer_info;
2667 unsigned peer_id;
2668 pj_bool_t need_reopen = PJ_FALSE;
Nanang Izzuddinfe02a062009-02-18 14:28:49 +00002669
2670 peer_id = (source!=0)? source : sink;
2671 status = pjmedia_conf_get_port_info(pjsua_var.mconf, peer_id,
2672 &peer_info);
2673 pj_assert(status == PJ_SUCCESS);
2674
2675 status = pjmedia_conf_get_port_info(pjsua_var.mconf, 0, &port0_info);
2676 pj_assert(status == PJ_SUCCESS);
2677
2678 /* Check if sound device is instantiated. */
2679 need_reopen = (pjsua_var.snd_port==NULL && pjsua_var.null_snd==NULL &&
2680 !pjsua_var.no_snd);
2681
2682 /* Check if sound device need to reopen because it needs to modify
2683 * settings to match its peer. Sound device must be idle in this case
2684 * though.
2685 */
2686 if (!need_reopen &&
2687 port0_info.listener_cnt==0 && port0_info.transmitter_cnt==0)
2688 {
2689 need_reopen = (peer_info.format.id != port0_info.format.id ||
Benny Prijonoc45d9512010-12-10 11:04:30 +00002690 peer_info.format.det.aud.avg_bps !=
2691 port0_info.format.det.aud.avg_bps ||
Nanang Izzuddinfe02a062009-02-18 14:28:49 +00002692 peer_info.clock_rate != port0_info.clock_rate ||
Benny Prijonoc45d9512010-12-10 11:04:30 +00002693 peer_info.channel_count!=port0_info.channel_count);
Nanang Izzuddinfe02a062009-02-18 14:28:49 +00002694 }
2695
2696 if (need_reopen) {
Benny Prijonod65f78c2009-06-03 18:59:37 +00002697 if (pjsua_var.cap_dev != NULL_SND_DEV_ID) {
Sauw Ming98766c72011-03-11 06:57:24 +00002698 pjmedia_snd_port_param param;
Nanang Izzuddinfe02a062009-02-18 14:28:49 +00002699
Benny Prijonod65f78c2009-06-03 18:59:37 +00002700 /* Create parameter based on peer info */
Sauw Ming98766c72011-03-11 06:57:24 +00002701 status = create_aud_param(&param.base, pjsua_var.cap_dev,
Benny Prijonod65f78c2009-06-03 18:59:37 +00002702 pjsua_var.play_dev,
2703 peer_info.clock_rate,
2704 peer_info.channel_count,
2705 peer_info.samples_per_frame,
2706 peer_info.bits_per_sample);
2707 if (status != PJ_SUCCESS) {
Benny Prijonoc45d9512010-12-10 11:04:30 +00002708 pjsua_perror(THIS_FILE, "Error opening sound device",
2709 status);
Benny Prijonob90fd382011-09-18 14:59:56 +00002710 goto on_return;
Benny Prijonod65f78c2009-06-03 18:59:37 +00002711 }
Benny Prijonof798e502009-03-09 13:08:16 +00002712
Benny Prijonod65f78c2009-06-03 18:59:37 +00002713 /* And peer format */
2714 if (peer_info.format.id != PJMEDIA_FORMAT_PCM) {
Sauw Ming98766c72011-03-11 06:57:24 +00002715 param.base.flags |= PJMEDIA_AUD_DEV_CAP_EXT_FORMAT;
2716 param.base.ext_fmt = peer_info.format;
Benny Prijonod65f78c2009-06-03 18:59:37 +00002717 }
Benny Prijono10454dc2009-02-21 14:21:59 +00002718
Sauw Ming98766c72011-03-11 06:57:24 +00002719 param.options = 0;
Benny Prijonod65f78c2009-06-03 18:59:37 +00002720 status = open_snd_dev(&param);
2721 if (status != PJ_SUCCESS) {
Benny Prijonoc45d9512010-12-10 11:04:30 +00002722 pjsua_perror(THIS_FILE, "Error opening sound device",
2723 status);
Benny Prijonob90fd382011-09-18 14:59:56 +00002724 goto on_return;
Benny Prijonod65f78c2009-06-03 18:59:37 +00002725 }
2726 } else {
2727 /* Null-audio */
Benny Prijonoc45d9512010-12-10 11:04:30 +00002728 status = pjsua_set_snd_dev(pjsua_var.cap_dev,
2729 pjsua_var.play_dev);
Benny Prijonod65f78c2009-06-03 18:59:37 +00002730 if (status != PJ_SUCCESS) {
Benny Prijonoc45d9512010-12-10 11:04:30 +00002731 pjsua_perror(THIS_FILE, "Error opening sound device",
2732 status);
Benny Prijonob90fd382011-09-18 14:59:56 +00002733 goto on_return;
Benny Prijonod65f78c2009-06-03 18:59:37 +00002734 }
Nanang Izzuddinfe02a062009-02-18 14:28:49 +00002735 }
Benny Prijono2d647722011-07-13 03:05:22 +00002736 } else if (pjsua_var.no_snd) {
2737 if (!pjsua_var.snd_is_on) {
2738 pjsua_var.snd_is_on = PJ_TRUE;
2739 /* Notify app */
2740 if (pjsua_var.ua_cfg.cb.on_snd_dev_operation) {
2741 (*pjsua_var.ua_cfg.cb.on_snd_dev_operation)(1);
2742 }
2743 }
Nanang Izzuddinfe02a062009-02-18 14:28:49 +00002744 }
Nanang Izzuddinfe02a062009-02-18 14:28:49 +00002745
Benny Prijonof798e502009-03-09 13:08:16 +00002746 } else {
2747 /* The bridge version */
Nanang Izzuddinfe02a062009-02-18 14:28:49 +00002748
Benny Prijonof798e502009-03-09 13:08:16 +00002749 /* Create sound port if none is instantiated */
2750 if (pjsua_var.snd_port==NULL && pjsua_var.null_snd==NULL &&
2751 !pjsua_var.no_snd)
2752 {
2753 pj_status_t status;
Nanang Izzuddin68559c32008-06-13 17:01:46 +00002754
Benny Prijonof798e502009-03-09 13:08:16 +00002755 status = pjsua_set_snd_dev(pjsua_var.cap_dev, pjsua_var.play_dev);
2756 if (status != PJ_SUCCESS) {
2757 pjsua_perror(THIS_FILE, "Error opening sound device", status);
Benny Prijonob90fd382011-09-18 14:59:56 +00002758 goto on_return;
Benny Prijonof798e502009-03-09 13:08:16 +00002759 }
Benny Prijono2d647722011-07-13 03:05:22 +00002760 } else if (pjsua_var.no_snd && !pjsua_var.snd_is_on) {
2761 pjsua_var.snd_is_on = PJ_TRUE;
2762 /* Notify app */
2763 if (pjsua_var.ua_cfg.cb.on_snd_dev_operation) {
2764 (*pjsua_var.ua_cfg.cb.on_snd_dev_operation)(1);
2765 }
Nanang Izzuddin68559c32008-06-13 17:01:46 +00002766 }
Benny Prijonof798e502009-03-09 13:08:16 +00002767 }
Nanang Izzuddinfe02a062009-02-18 14:28:49 +00002768
Benny Prijonob90fd382011-09-18 14:59:56 +00002769 status = pjmedia_conf_connect_port(pjsua_var.mconf, source, sink, 0);
2770
2771on_return:
2772 pj_log_pop_indent();
2773 return status;
Benny Prijonoeebe9af2006-06-13 22:57:13 +00002774}
2775
2776
2777/*
2778 * Disconnect media flow from the source to destination port.
2779 */
2780PJ_DEF(pj_status_t) pjsua_conf_disconnect( pjsua_conf_port_id source,
2781 pjsua_conf_port_id sink)
2782{
Nanang Izzuddin68559c32008-06-13 17:01:46 +00002783 pj_status_t status;
2784
Benny Prijonob90fd382011-09-18 14:59:56 +00002785 PJ_LOG(4,(THIS_FILE, "%s disconnect: %d -x- %d",
2786 (pjsua_var.is_mswitch ? "Switch" : "Conf"),
2787 source, sink));
2788 pj_log_push_indent();
2789
Nanang Izzuddin68559c32008-06-13 17:01:46 +00002790 status = pjmedia_conf_disconnect_port(pjsua_var.mconf, source, sink);
Nanang Izzuddin148fd392008-06-16 09:52:50 +00002791 check_snd_dev_idle();
Nanang Izzuddin68559c32008-06-13 17:01:46 +00002792
Benny Prijonob90fd382011-09-18 14:59:56 +00002793 pj_log_pop_indent();
Nanang Izzuddin68559c32008-06-13 17:01:46 +00002794 return status;
Benny Prijonoeebe9af2006-06-13 22:57:13 +00002795}
2796
2797
Benny Prijono6dd967c2006-12-26 02:27:14 +00002798/*
2799 * Adjust the signal level to be transmitted from the bridge to the
2800 * specified port by making it louder or quieter.
2801 */
2802PJ_DEF(pj_status_t) pjsua_conf_adjust_tx_level(pjsua_conf_port_id slot,
2803 float level)
2804{
2805 return pjmedia_conf_adjust_tx_level(pjsua_var.mconf, slot,
2806 (int)((level-1) * 128));
2807}
2808
2809/*
2810 * Adjust the signal level to be received from the specified port (to
2811 * the bridge) by making it louder or quieter.
2812 */
2813PJ_DEF(pj_status_t) pjsua_conf_adjust_rx_level(pjsua_conf_port_id slot,
2814 float level)
2815{
2816 return pjmedia_conf_adjust_rx_level(pjsua_var.mconf, slot,
2817 (int)((level-1) * 128));
2818}
2819
2820
2821/*
2822 * Get last signal level transmitted to or received from the specified port.
2823 */
2824PJ_DEF(pj_status_t) pjsua_conf_get_signal_level(pjsua_conf_port_id slot,
2825 unsigned *tx_level,
2826 unsigned *rx_level)
2827{
2828 return pjmedia_conf_get_signal_level(pjsua_var.mconf, slot,
2829 tx_level, rx_level);
2830}
2831
Benny Prijonoeebe9af2006-06-13 22:57:13 +00002832/*****************************************************************************
2833 * File player.
2834 */
2835
Benny Prijonod5696da2007-07-17 16:25:45 +00002836static char* get_basename(const char *path, unsigned len)
2837{
2838 char *p = ((char*)path) + len;
2839
2840 if (len==0)
2841 return p;
2842
Benny Prijono1f61a8f2007-08-16 10:11:44 +00002843 for (--p; p!=path && *p!='/' && *p!='\\'; ) --p;
Benny Prijonod5696da2007-07-17 16:25:45 +00002844
2845 return (p==path) ? p : p+1;
2846}
2847
2848
Benny Prijonoeebe9af2006-06-13 22:57:13 +00002849/*
2850 * Create a file player, and automatically connect this player to
2851 * the conference bridge.
2852 */
2853PJ_DEF(pj_status_t) pjsua_player_create( const pj_str_t *filename,
2854 unsigned options,
Benny Prijonoeebe9af2006-06-13 22:57:13 +00002855 pjsua_player_id *p_id)
2856{
2857 unsigned slot, file_id;
Benny Prijonoa66c3312007-01-21 23:12:40 +00002858 char path[PJ_MAXPATH];
Benny Prijonob90fd382011-09-18 14:59:56 +00002859 pj_pool_t *pool = NULL;
Benny Prijonoeebe9af2006-06-13 22:57:13 +00002860 pjmedia_port *port;
Benny Prijonob90fd382011-09-18 14:59:56 +00002861 pj_status_t status = PJ_SUCCESS;
Benny Prijonoeebe9af2006-06-13 22:57:13 +00002862
2863 if (pjsua_var.player_cnt >= PJ_ARRAY_SIZE(pjsua_var.player))
2864 return PJ_ETOOMANY;
2865
Benny Prijonob90fd382011-09-18 14:59:56 +00002866 PJ_LOG(4,(THIS_FILE, "Creating file player: %.*s..",
2867 (int)filename->slen, filename->ptr));
2868 pj_log_push_indent();
2869
Benny Prijonoeebe9af2006-06-13 22:57:13 +00002870 PJSUA_LOCK();
2871
2872 for (file_id=0; file_id<PJ_ARRAY_SIZE(pjsua_var.player); ++file_id) {
2873 if (pjsua_var.player[file_id].port == NULL)
2874 break;
2875 }
2876
2877 if (file_id == PJ_ARRAY_SIZE(pjsua_var.player)) {
2878 /* This is unexpected */
Benny Prijonoeebe9af2006-06-13 22:57:13 +00002879 pj_assert(0);
Benny Prijonob90fd382011-09-18 14:59:56 +00002880 status = PJ_EBUG;
2881 goto on_error;
Benny Prijonoeebe9af2006-06-13 22:57:13 +00002882 }
2883
2884 pj_memcpy(path, filename->ptr, filename->slen);
2885 path[filename->slen] = '\0';
Benny Prijonod5696da2007-07-17 16:25:45 +00002886
2887 pool = pjsua_pool_create(get_basename(path, filename->slen), 1000, 1000);
2888 if (!pool) {
Benny Prijonob90fd382011-09-18 14:59:56 +00002889 status = PJ_ENOMEM;
2890 goto on_error;
Benny Prijonod5696da2007-07-17 16:25:45 +00002891 }
2892
Nanang Izzuddin81e9bd52008-06-27 12:52:51 +00002893 status = pjmedia_wav_player_port_create(
2894 pool, path,
2895 pjsua_var.mconf_cfg.samples_per_frame *
Benny Prijonoc45d9512010-12-10 11:04:30 +00002896 1000 / pjsua_var.media_cfg.channel_count /
Nanang Izzuddin81e9bd52008-06-27 12:52:51 +00002897 pjsua_var.media_cfg.clock_rate,
2898 options, 0, &port);
Benny Prijonoeebe9af2006-06-13 22:57:13 +00002899 if (status != PJ_SUCCESS) {
Benny Prijonoeebe9af2006-06-13 22:57:13 +00002900 pjsua_perror(THIS_FILE, "Unable to open file for playback", status);
Benny Prijonob90fd382011-09-18 14:59:56 +00002901 goto on_error;
Benny Prijonoeebe9af2006-06-13 22:57:13 +00002902 }
2903
Benny Prijono5297af92008-03-18 13:40:40 +00002904 status = pjmedia_conf_add_port(pjsua_var.mconf, pool,
Benny Prijonoeebe9af2006-06-13 22:57:13 +00002905 port, filename, &slot);
2906 if (status != PJ_SUCCESS) {
2907 pjmedia_port_destroy(port);
Benny Prijono32e4f492007-01-24 00:44:26 +00002908 pjsua_perror(THIS_FILE, "Unable to add file to conference bridge",
2909 status);
Benny Prijonob90fd382011-09-18 14:59:56 +00002910 goto on_error;
Benny Prijonoeebe9af2006-06-13 22:57:13 +00002911 }
2912
Benny Prijonoa66c3312007-01-21 23:12:40 +00002913 pjsua_var.player[file_id].type = 0;
Benny Prijonod5696da2007-07-17 16:25:45 +00002914 pjsua_var.player[file_id].pool = pool;
Benny Prijonoeebe9af2006-06-13 22:57:13 +00002915 pjsua_var.player[file_id].port = port;
2916 pjsua_var.player[file_id].slot = slot;
2917
2918 if (p_id) *p_id = file_id;
2919
2920 ++pjsua_var.player_cnt;
2921
2922 PJSUA_UNLOCK();
Benny Prijonob90fd382011-09-18 14:59:56 +00002923
2924 PJ_LOG(4,(THIS_FILE, "Player created, id=%d, slot=%d", file_id, slot));
2925
2926 pj_log_pop_indent();
Benny Prijonoeebe9af2006-06-13 22:57:13 +00002927 return PJ_SUCCESS;
Benny Prijonob90fd382011-09-18 14:59:56 +00002928
2929on_error:
2930 PJSUA_UNLOCK();
2931 if (pool) pj_pool_release(pool);
2932 pj_log_pop_indent();
2933 return status;
Benny Prijonoeebe9af2006-06-13 22:57:13 +00002934}
2935
2936
2937/*
Benny Prijonoa66c3312007-01-21 23:12:40 +00002938 * Create a file playlist media port, and automatically add the port
2939 * to the conference bridge.
2940 */
2941PJ_DEF(pj_status_t) pjsua_playlist_create( const pj_str_t file_names[],
2942 unsigned file_count,
2943 const pj_str_t *label,
2944 unsigned options,
2945 pjsua_player_id *p_id)
2946{
2947 unsigned slot, file_id, ptime;
Benny Prijonob90fd382011-09-18 14:59:56 +00002948 pj_pool_t *pool = NULL;
Benny Prijonoa66c3312007-01-21 23:12:40 +00002949 pjmedia_port *port;
Benny Prijonob90fd382011-09-18 14:59:56 +00002950 pj_status_t status = PJ_SUCCESS;
Benny Prijonoa66c3312007-01-21 23:12:40 +00002951
2952 if (pjsua_var.player_cnt >= PJ_ARRAY_SIZE(pjsua_var.player))
2953 return PJ_ETOOMANY;
2954
Benny Prijonob90fd382011-09-18 14:59:56 +00002955 PJ_LOG(4,(THIS_FILE, "Creating playlist with %d file(s)..", file_count));
2956 pj_log_push_indent();
2957
Benny Prijonoa66c3312007-01-21 23:12:40 +00002958 PJSUA_LOCK();
2959
2960 for (file_id=0; file_id<PJ_ARRAY_SIZE(pjsua_var.player); ++file_id) {
2961 if (pjsua_var.player[file_id].port == NULL)
2962 break;
2963 }
2964
2965 if (file_id == PJ_ARRAY_SIZE(pjsua_var.player)) {
2966 /* This is unexpected */
Benny Prijonoa66c3312007-01-21 23:12:40 +00002967 pj_assert(0);
Benny Prijonob90fd382011-09-18 14:59:56 +00002968 status = PJ_EBUG;
2969 goto on_error;
Benny Prijonoa66c3312007-01-21 23:12:40 +00002970 }
2971
2972
2973 ptime = pjsua_var.mconf_cfg.samples_per_frame * 1000 /
2974 pjsua_var.media_cfg.clock_rate;
2975
Benny Prijonod5696da2007-07-17 16:25:45 +00002976 pool = pjsua_pool_create("playlist", 1000, 1000);
2977 if (!pool) {
Benny Prijonob90fd382011-09-18 14:59:56 +00002978 status = PJ_ENOMEM;
2979 goto on_error;
Benny Prijonod5696da2007-07-17 16:25:45 +00002980 }
2981
2982 status = pjmedia_wav_playlist_create(pool, label,
Benny Prijonoa66c3312007-01-21 23:12:40 +00002983 file_names, file_count,
2984 ptime, options, 0, &port);
2985 if (status != PJ_SUCCESS) {
Benny Prijonoa66c3312007-01-21 23:12:40 +00002986 pjsua_perror(THIS_FILE, "Unable to create playlist", status);
Benny Prijonob90fd382011-09-18 14:59:56 +00002987 goto on_error;
Benny Prijonoa66c3312007-01-21 23:12:40 +00002988 }
2989
Benny Prijonod5696da2007-07-17 16:25:45 +00002990 status = pjmedia_conf_add_port(pjsua_var.mconf, pool,
Benny Prijonoa66c3312007-01-21 23:12:40 +00002991 port, &port->info.name, &slot);
2992 if (status != PJ_SUCCESS) {
2993 pjmedia_port_destroy(port);
Benny Prijonoa66c3312007-01-21 23:12:40 +00002994 pjsua_perror(THIS_FILE, "Unable to add port", status);
Benny Prijonob90fd382011-09-18 14:59:56 +00002995 goto on_error;
Benny Prijonoa66c3312007-01-21 23:12:40 +00002996 }
2997
2998 pjsua_var.player[file_id].type = 1;
Benny Prijonod5696da2007-07-17 16:25:45 +00002999 pjsua_var.player[file_id].pool = pool;
Benny Prijonoa66c3312007-01-21 23:12:40 +00003000 pjsua_var.player[file_id].port = port;
3001 pjsua_var.player[file_id].slot = slot;
3002
3003 if (p_id) *p_id = file_id;
3004
3005 ++pjsua_var.player_cnt;
3006
3007 PJSUA_UNLOCK();
Benny Prijonob90fd382011-09-18 14:59:56 +00003008
3009 PJ_LOG(4,(THIS_FILE, "Playlist created, id=%d, slot=%d", file_id, slot));
3010
3011 pj_log_pop_indent();
3012
Benny Prijonoa66c3312007-01-21 23:12:40 +00003013 return PJ_SUCCESS;
3014
Benny Prijonob90fd382011-09-18 14:59:56 +00003015on_error:
3016 PJSUA_UNLOCK();
3017 if (pool) pj_pool_release(pool);
3018 pj_log_pop_indent();
3019
3020 return status;
Benny Prijonoa66c3312007-01-21 23:12:40 +00003021}
3022
3023
3024/*
Benny Prijonoeebe9af2006-06-13 22:57:13 +00003025 * Get conference port ID associated with player.
3026 */
3027PJ_DEF(pjsua_conf_port_id) pjsua_player_get_conf_port(pjsua_player_id id)
3028{
Benny Prijonoa1e69682007-05-11 15:14:34 +00003029 PJ_ASSERT_RETURN(id>=0&&id<(int)PJ_ARRAY_SIZE(pjsua_var.player), PJ_EINVAL);
Benny Prijonoeebe9af2006-06-13 22:57:13 +00003030 PJ_ASSERT_RETURN(pjsua_var.player[id].port != NULL, PJ_EINVAL);
3031
3032 return pjsua_var.player[id].slot;
3033}
3034
Benny Prijono469b1522006-12-26 03:05:17 +00003035/*
3036 * Get the media port for the player.
3037 */
Benny Prijonobe41d862008-01-18 13:24:28 +00003038PJ_DEF(pj_status_t) pjsua_player_get_port( pjsua_player_id id,
Benny Prijono469b1522006-12-26 03:05:17 +00003039 pjmedia_port **p_port)
3040{
Benny Prijonoa1e69682007-05-11 15:14:34 +00003041 PJ_ASSERT_RETURN(id>=0&&id<(int)PJ_ARRAY_SIZE(pjsua_var.player), PJ_EINVAL);
Benny Prijono469b1522006-12-26 03:05:17 +00003042 PJ_ASSERT_RETURN(pjsua_var.player[id].port != NULL, PJ_EINVAL);
3043 PJ_ASSERT_RETURN(p_port != NULL, PJ_EINVAL);
3044
3045 *p_port = pjsua_var.player[id].port;
3046
3047 return PJ_SUCCESS;
3048}
Benny Prijonoeebe9af2006-06-13 22:57:13 +00003049
3050/*
3051 * Set playback position.
3052 */
3053PJ_DEF(pj_status_t) pjsua_player_set_pos( pjsua_player_id id,
3054 pj_uint32_t samples)
3055{
Benny Prijonoa1e69682007-05-11 15:14:34 +00003056 PJ_ASSERT_RETURN(id>=0&&id<(int)PJ_ARRAY_SIZE(pjsua_var.player), PJ_EINVAL);
Benny Prijonoeebe9af2006-06-13 22:57:13 +00003057 PJ_ASSERT_RETURN(pjsua_var.player[id].port != NULL, PJ_EINVAL);
Benny Prijonoa66c3312007-01-21 23:12:40 +00003058 PJ_ASSERT_RETURN(pjsua_var.player[id].type == 0, PJ_EINVAL);
Benny Prijonoeebe9af2006-06-13 22:57:13 +00003059
3060 return pjmedia_wav_player_port_set_pos(pjsua_var.player[id].port, samples);
3061}
3062
3063
3064/*
3065 * Close the file, remove the player from the bridge, and free
3066 * resources associated with the file player.
3067 */
3068PJ_DEF(pj_status_t) pjsua_player_destroy(pjsua_player_id id)
3069{
Benny Prijonoa1e69682007-05-11 15:14:34 +00003070 PJ_ASSERT_RETURN(id>=0&&id<(int)PJ_ARRAY_SIZE(pjsua_var.player), PJ_EINVAL);
Benny Prijonoeebe9af2006-06-13 22:57:13 +00003071 PJ_ASSERT_RETURN(pjsua_var.player[id].port != NULL, PJ_EINVAL);
3072
Benny Prijonob90fd382011-09-18 14:59:56 +00003073 PJ_LOG(4,(THIS_FILE, "Destroying player %d..", id));
3074 pj_log_push_indent();
3075
Benny Prijonoeebe9af2006-06-13 22:57:13 +00003076 PJSUA_LOCK();
3077
3078 if (pjsua_var.player[id].port) {
Nanang Izzuddin148fd392008-06-16 09:52:50 +00003079 pjsua_conf_remove_port(pjsua_var.player[id].slot);
Benny Prijonoeebe9af2006-06-13 22:57:13 +00003080 pjmedia_port_destroy(pjsua_var.player[id].port);
3081 pjsua_var.player[id].port = NULL;
3082 pjsua_var.player[id].slot = 0xFFFF;
Benny Prijonod5696da2007-07-17 16:25:45 +00003083 pj_pool_release(pjsua_var.player[id].pool);
3084 pjsua_var.player[id].pool = NULL;
Benny Prijonoeebe9af2006-06-13 22:57:13 +00003085 pjsua_var.player_cnt--;
3086 }
3087
3088 PJSUA_UNLOCK();
Benny Prijonob90fd382011-09-18 14:59:56 +00003089 pj_log_pop_indent();
Benny Prijonoeebe9af2006-06-13 22:57:13 +00003090
3091 return PJ_SUCCESS;
3092}
3093
3094
3095/*****************************************************************************
3096 * File recorder.
3097 */
3098
3099/*
3100 * Create a file recorder, and automatically connect this recorder to
3101 * the conference bridge.
3102 */
3103PJ_DEF(pj_status_t) pjsua_recorder_create( const pj_str_t *filename,
Benny Prijono8f310522006-10-20 11:08:49 +00003104 unsigned enc_type,
3105 void *enc_param,
Benny Prijonoeebe9af2006-06-13 22:57:13 +00003106 pj_ssize_t max_size,
3107 unsigned options,
Benny Prijonoeebe9af2006-06-13 22:57:13 +00003108 pjsua_recorder_id *p_id)
3109{
Benny Prijonob3cdb2b2006-10-19 15:49:47 +00003110 enum Format
3111 {
3112 FMT_UNKNOWN,
3113 FMT_WAV,
3114 FMT_MP3,
3115 };
Benny Prijonoeebe9af2006-06-13 22:57:13 +00003116 unsigned slot, file_id;
Benny Prijonoa66c3312007-01-21 23:12:40 +00003117 char path[PJ_MAXPATH];
Benny Prijonob3cdb2b2006-10-19 15:49:47 +00003118 pj_str_t ext;
Benny Prijono8f310522006-10-20 11:08:49 +00003119 int file_format;
Benny Prijonob90fd382011-09-18 14:59:56 +00003120 pj_pool_t *pool = NULL;
Benny Prijonoeebe9af2006-06-13 22:57:13 +00003121 pjmedia_port *port;
Benny Prijonob90fd382011-09-18 14:59:56 +00003122 pj_status_t status = PJ_SUCCESS;
Benny Prijonoeebe9af2006-06-13 22:57:13 +00003123
Benny Prijonob3cdb2b2006-10-19 15:49:47 +00003124 /* Filename must present */
3125 PJ_ASSERT_RETURN(filename != NULL, PJ_EINVAL);
3126
Benny Prijono00cae612006-07-31 15:19:36 +00003127 /* Don't support max_size at present */
Benny Prijonob3cdb2b2006-10-19 15:49:47 +00003128 PJ_ASSERT_RETURN(max_size == 0 || max_size == -1, PJ_EINVAL);
Benny Prijono00cae612006-07-31 15:19:36 +00003129
Benny Prijono8f310522006-10-20 11:08:49 +00003130 /* Don't support encoding type at present */
3131 PJ_ASSERT_RETURN(enc_type == 0, PJ_EINVAL);
Benny Prijono00cae612006-07-31 15:19:36 +00003132
Benny Prijonob90fd382011-09-18 14:59:56 +00003133 PJ_LOG(4,(THIS_FILE, "Creating recorder %.*s..",
3134 (int)filename->slen, filename->ptr));
3135 pj_log_push_indent();
3136
3137 if (pjsua_var.rec_cnt >= PJ_ARRAY_SIZE(pjsua_var.recorder)) {
3138 pj_log_pop_indent();
Benny Prijonoeebe9af2006-06-13 22:57:13 +00003139 return PJ_ETOOMANY;
Benny Prijonob90fd382011-09-18 14:59:56 +00003140 }
Benny Prijonoeebe9af2006-06-13 22:57:13 +00003141
Benny Prijonob3cdb2b2006-10-19 15:49:47 +00003142 /* Determine the file format */
3143 ext.ptr = filename->ptr + filename->slen - 4;
3144 ext.slen = 4;
3145
3146 if (pj_stricmp2(&ext, ".wav") == 0)
3147 file_format = FMT_WAV;
3148 else if (pj_stricmp2(&ext, ".mp3") == 0)
3149 file_format = FMT_MP3;
3150 else {
3151 PJ_LOG(1,(THIS_FILE, "pjsua_recorder_create() error: unable to "
3152 "determine file format for %.*s",
3153 (int)filename->slen, filename->ptr));
Benny Prijonob90fd382011-09-18 14:59:56 +00003154 pj_log_pop_indent();
Benny Prijonob3cdb2b2006-10-19 15:49:47 +00003155 return PJ_ENOTSUP;
3156 }
3157
Benny Prijonoeebe9af2006-06-13 22:57:13 +00003158 PJSUA_LOCK();
3159
3160 for (file_id=0; file_id<PJ_ARRAY_SIZE(pjsua_var.recorder); ++file_id) {
3161 if (pjsua_var.recorder[file_id].port == NULL)
3162 break;
3163 }
3164
3165 if (file_id == PJ_ARRAY_SIZE(pjsua_var.recorder)) {
3166 /* This is unexpected */
Benny Prijonoeebe9af2006-06-13 22:57:13 +00003167 pj_assert(0);
Benny Prijonob90fd382011-09-18 14:59:56 +00003168 status = PJ_EBUG;
3169 goto on_return;
Benny Prijonoeebe9af2006-06-13 22:57:13 +00003170 }
3171
3172 pj_memcpy(path, filename->ptr, filename->slen);
3173 path[filename->slen] = '\0';
Benny Prijonob3cdb2b2006-10-19 15:49:47 +00003174
Benny Prijonod5696da2007-07-17 16:25:45 +00003175 pool = pjsua_pool_create(get_basename(path, filename->slen), 1000, 1000);
3176 if (!pool) {
Benny Prijonob90fd382011-09-18 14:59:56 +00003177 status = PJ_ENOMEM;
3178 goto on_return;
Benny Prijonod5696da2007-07-17 16:25:45 +00003179 }
3180
Benny Prijonob3cdb2b2006-10-19 15:49:47 +00003181 if (file_format == FMT_WAV) {
Benny Prijonod5696da2007-07-17 16:25:45 +00003182 status = pjmedia_wav_writer_port_create(pool, path,
Benny Prijonob3cdb2b2006-10-19 15:49:47 +00003183 pjsua_var.media_cfg.clock_rate,
3184 pjsua_var.mconf_cfg.channel_count,
3185 pjsua_var.mconf_cfg.samples_per_frame,
3186 pjsua_var.mconf_cfg.bits_per_sample,
3187 options, 0, &port);
Benny Prijonob3cdb2b2006-10-19 15:49:47 +00003188 } else {
Benny Prijonoc95a0f02007-04-09 07:06:08 +00003189 PJ_UNUSED_ARG(enc_param);
Benny Prijonob3cdb2b2006-10-19 15:49:47 +00003190 port = NULL;
3191 status = PJ_ENOTSUP;
3192 }
3193
Benny Prijonoeebe9af2006-06-13 22:57:13 +00003194 if (status != PJ_SUCCESS) {
Benny Prijonoeebe9af2006-06-13 22:57:13 +00003195 pjsua_perror(THIS_FILE, "Unable to open file for recording", status);
Benny Prijonob90fd382011-09-18 14:59:56 +00003196 goto on_return;
Benny Prijonoeebe9af2006-06-13 22:57:13 +00003197 }
3198
Benny Prijonod5696da2007-07-17 16:25:45 +00003199 status = pjmedia_conf_add_port(pjsua_var.mconf, pool,
Benny Prijonoeebe9af2006-06-13 22:57:13 +00003200 port, filename, &slot);
3201 if (status != PJ_SUCCESS) {
3202 pjmedia_port_destroy(port);
Benny Prijonob90fd382011-09-18 14:59:56 +00003203 goto on_return;
Benny Prijonoeebe9af2006-06-13 22:57:13 +00003204 }
3205
3206 pjsua_var.recorder[file_id].port = port;
3207 pjsua_var.recorder[file_id].slot = slot;
Benny Prijonod5696da2007-07-17 16:25:45 +00003208 pjsua_var.recorder[file_id].pool = pool;
Benny Prijonoeebe9af2006-06-13 22:57:13 +00003209
3210 if (p_id) *p_id = file_id;
3211
3212 ++pjsua_var.rec_cnt;
3213
3214 PJSUA_UNLOCK();
Benny Prijonob90fd382011-09-18 14:59:56 +00003215
3216 PJ_LOG(4,(THIS_FILE, "Recorder created, id=%d, slot=%d", file_id, slot));
3217
3218 pj_log_pop_indent();
Benny Prijonoeebe9af2006-06-13 22:57:13 +00003219 return PJ_SUCCESS;
Benny Prijonob90fd382011-09-18 14:59:56 +00003220
3221on_return:
3222 PJSUA_UNLOCK();
3223 if (pool) pj_pool_release(pool);
3224 pj_log_pop_indent();
3225 return status;
Benny Prijonoeebe9af2006-06-13 22:57:13 +00003226}
3227
3228
3229/*
3230 * Get conference port associated with recorder.
3231 */
3232PJ_DEF(pjsua_conf_port_id) pjsua_recorder_get_conf_port(pjsua_recorder_id id)
3233{
Benny Prijonoa1e69682007-05-11 15:14:34 +00003234 PJ_ASSERT_RETURN(id>=0 && id<(int)PJ_ARRAY_SIZE(pjsua_var.recorder),
3235 PJ_EINVAL);
Benny Prijonoeebe9af2006-06-13 22:57:13 +00003236 PJ_ASSERT_RETURN(pjsua_var.recorder[id].port != NULL, PJ_EINVAL);
3237
3238 return pjsua_var.recorder[id].slot;
3239}
3240
Benny Prijono469b1522006-12-26 03:05:17 +00003241/*
3242 * Get the media port for the recorder.
3243 */
3244PJ_DEF(pj_status_t) pjsua_recorder_get_port( pjsua_recorder_id id,
3245 pjmedia_port **p_port)
3246{
Benny Prijonoa1e69682007-05-11 15:14:34 +00003247 PJ_ASSERT_RETURN(id>=0 && id<(int)PJ_ARRAY_SIZE(pjsua_var.recorder),
3248 PJ_EINVAL);
Benny Prijono469b1522006-12-26 03:05:17 +00003249 PJ_ASSERT_RETURN(pjsua_var.recorder[id].port != NULL, PJ_EINVAL);
3250 PJ_ASSERT_RETURN(p_port != NULL, PJ_EINVAL);
3251
3252 *p_port = pjsua_var.recorder[id].port;
3253 return PJ_SUCCESS;
3254}
Benny Prijonoeebe9af2006-06-13 22:57:13 +00003255
3256/*
3257 * Destroy recorder (this will complete recording).
3258 */
3259PJ_DEF(pj_status_t) pjsua_recorder_destroy(pjsua_recorder_id id)
3260{
Benny Prijonoa1e69682007-05-11 15:14:34 +00003261 PJ_ASSERT_RETURN(id>=0 && id<(int)PJ_ARRAY_SIZE(pjsua_var.recorder),
3262 PJ_EINVAL);
Benny Prijonoeebe9af2006-06-13 22:57:13 +00003263 PJ_ASSERT_RETURN(pjsua_var.recorder[id].port != NULL, PJ_EINVAL);
3264
Benny Prijonob90fd382011-09-18 14:59:56 +00003265 PJ_LOG(4,(THIS_FILE, "Destroying recorder %d..", id));
3266 pj_log_push_indent();
3267
Benny Prijonoeebe9af2006-06-13 22:57:13 +00003268 PJSUA_LOCK();
3269
3270 if (pjsua_var.recorder[id].port) {
Nanang Izzuddin148fd392008-06-16 09:52:50 +00003271 pjsua_conf_remove_port(pjsua_var.recorder[id].slot);
Benny Prijonoeebe9af2006-06-13 22:57:13 +00003272 pjmedia_port_destroy(pjsua_var.recorder[id].port);
3273 pjsua_var.recorder[id].port = NULL;
3274 pjsua_var.recorder[id].slot = 0xFFFF;
Benny Prijonod5696da2007-07-17 16:25:45 +00003275 pj_pool_release(pjsua_var.recorder[id].pool);
3276 pjsua_var.recorder[id].pool = NULL;
Benny Prijonoeebe9af2006-06-13 22:57:13 +00003277 pjsua_var.rec_cnt--;
3278 }
3279
3280 PJSUA_UNLOCK();
Benny Prijonob90fd382011-09-18 14:59:56 +00003281 pj_log_pop_indent();
Benny Prijonoeebe9af2006-06-13 22:57:13 +00003282
3283 return PJ_SUCCESS;
3284}
3285
3286
3287/*****************************************************************************
3288 * Sound devices.
3289 */
3290
3291/*
3292 * Enum sound devices.
3293 */
Benny Prijonof798e502009-03-09 13:08:16 +00003294
3295PJ_DEF(pj_status_t) pjsua_enum_aud_devs( pjmedia_aud_dev_info info[],
Benny Prijonoeebe9af2006-06-13 22:57:13 +00003296 unsigned *count)
3297{
3298 unsigned i, dev_count;
3299
Benny Prijono10454dc2009-02-21 14:21:59 +00003300 dev_count = pjmedia_aud_dev_count();
Benny Prijonoeebe9af2006-06-13 22:57:13 +00003301
3302 if (dev_count > *count) dev_count = *count;
3303
3304 for (i=0; i<dev_count; ++i) {
Benny Prijono10454dc2009-02-21 14:21:59 +00003305 pj_status_t status;
Benny Prijonoeebe9af2006-06-13 22:57:13 +00003306
Benny Prijono10454dc2009-02-21 14:21:59 +00003307 status = pjmedia_aud_dev_get_info(i, &info[i]);
3308 if (status != PJ_SUCCESS)
3309 return status;
Benny Prijonoeebe9af2006-06-13 22:57:13 +00003310 }
3311
3312 *count = dev_count;
3313
3314 return PJ_SUCCESS;
3315}
Benny Prijonof798e502009-03-09 13:08:16 +00003316
3317
Benny Prijono10454dc2009-02-21 14:21:59 +00003318PJ_DEF(pj_status_t) pjsua_enum_snd_devs( pjmedia_snd_dev_info info[],
3319 unsigned *count)
3320{
3321 unsigned i, dev_count;
3322
3323 dev_count = pjmedia_aud_dev_count();
3324
3325 if (dev_count > *count) dev_count = *count;
3326 pj_bzero(info, dev_count * sizeof(pjmedia_snd_dev_info));
3327
3328 for (i=0; i<dev_count; ++i) {
3329 pjmedia_aud_dev_info ai;
3330 pj_status_t status;
3331
3332 status = pjmedia_aud_dev_get_info(i, &ai);
3333 if (status != PJ_SUCCESS)
3334 return status;
3335
3336 strncpy(info[i].name, ai.name, sizeof(info[i].name));
3337 info[i].name[sizeof(info[i].name)-1] = '\0';
3338 info[i].input_count = ai.input_count;
3339 info[i].output_count = ai.output_count;
3340 info[i].default_samples_per_sec = ai.default_samples_per_sec;
3341 }
3342
3343 *count = dev_count;
3344
3345 return PJ_SUCCESS;
3346}
Benny Prijono10454dc2009-02-21 14:21:59 +00003347
Benny Prijonof798e502009-03-09 13:08:16 +00003348/* Create audio device parameter to open the device */
3349static pj_status_t create_aud_param(pjmedia_aud_param *param,
3350 pjmedia_aud_dev_index capture_dev,
3351 pjmedia_aud_dev_index playback_dev,
3352 unsigned clock_rate,
3353 unsigned channel_count,
3354 unsigned samples_per_frame,
3355 unsigned bits_per_sample)
Benny Prijonoeebe9af2006-06-13 22:57:13 +00003356{
Nanang Izzuddin8465c682009-03-04 17:23:25 +00003357 pj_status_t status;
3358
Benny Prijono96e74f32009-02-22 12:00:12 +00003359 /* Normalize device ID with new convention about default device ID */
3360 if (playback_dev == PJMEDIA_AUD_DEFAULT_CAPTURE_DEV)
3361 playback_dev = PJMEDIA_AUD_DEFAULT_PLAYBACK_DEV;
3362
Benny Prijono10454dc2009-02-21 14:21:59 +00003363 /* Create default parameters for the device */
Benny Prijonof798e502009-03-09 13:08:16 +00003364 status = pjmedia_aud_dev_default_param(capture_dev, param);
Benny Prijono10454dc2009-02-21 14:21:59 +00003365 if (status != PJ_SUCCESS) {
Benny Prijono96e74f32009-02-22 12:00:12 +00003366 pjsua_perror(THIS_FILE, "Error retrieving default audio "
3367 "device parameters", status);
Benny Prijono10454dc2009-02-21 14:21:59 +00003368 return status;
3369 }
Benny Prijonof798e502009-03-09 13:08:16 +00003370 param->dir = PJMEDIA_DIR_CAPTURE_PLAYBACK;
3371 param->rec_id = capture_dev;
3372 param->play_id = playback_dev;
3373 param->clock_rate = clock_rate;
3374 param->channel_count = channel_count;
3375 param->samples_per_frame = samples_per_frame;
3376 param->bits_per_sample = bits_per_sample;
3377
3378 /* Update the setting with user preference */
3379#define update_param(cap, field) \
3380 if (pjsua_var.aud_param.flags & cap) { \
3381 param->flags |= cap; \
3382 param->field = pjsua_var.aud_param.field; \
3383 }
3384 update_param( PJMEDIA_AUD_DEV_CAP_INPUT_VOLUME_SETTING, input_vol);
3385 update_param( PJMEDIA_AUD_DEV_CAP_OUTPUT_VOLUME_SETTING, output_vol);
3386 update_param( PJMEDIA_AUD_DEV_CAP_INPUT_ROUTE, input_route);
3387 update_param( PJMEDIA_AUD_DEV_CAP_OUTPUT_ROUTE, output_route);
3388#undef update_param
3389
Benny Prijono10454dc2009-02-21 14:21:59 +00003390 /* Latency settings */
Benny Prijonof798e502009-03-09 13:08:16 +00003391 param->flags |= (PJMEDIA_AUD_DEV_CAP_INPUT_LATENCY |
3392 PJMEDIA_AUD_DEV_CAP_OUTPUT_LATENCY);
3393 param->input_latency_ms = pjsua_var.media_cfg.snd_rec_latency;
3394 param->output_latency_ms = pjsua_var.media_cfg.snd_play_latency;
3395
Benny Prijono10454dc2009-02-21 14:21:59 +00003396 /* EC settings */
3397 if (pjsua_var.media_cfg.ec_tail_len) {
Benny Prijonof798e502009-03-09 13:08:16 +00003398 param->flags |= (PJMEDIA_AUD_DEV_CAP_EC | PJMEDIA_AUD_DEV_CAP_EC_TAIL);
3399 param->ec_enabled = PJ_TRUE;
3400 param->ec_tail_ms = pjsua_var.media_cfg.ec_tail_len;
Benny Prijono10454dc2009-02-21 14:21:59 +00003401 } else {
Benny Prijonof798e502009-03-09 13:08:16 +00003402 param->flags &= ~(PJMEDIA_AUD_DEV_CAP_EC|PJMEDIA_AUD_DEV_CAP_EC_TAIL);
Benny Prijono10454dc2009-02-21 14:21:59 +00003403 }
3404
Benny Prijonof798e502009-03-09 13:08:16 +00003405 return PJ_SUCCESS;
3406}
Benny Prijono26056d82006-10-11 16:03:41 +00003407
Benny Prijonof798e502009-03-09 13:08:16 +00003408/* Internal: the first time the audio device is opened (during app
3409 * startup), retrieve the audio settings such as volume level
3410 * so that aud_get_settings() will work.
3411 */
3412static pj_status_t update_initial_aud_param()
3413{
3414 pjmedia_aud_stream *strm;
3415 pjmedia_aud_param param;
3416 pj_status_t status;
Benny Prijono26056d82006-10-11 16:03:41 +00003417
Benny Prijonof798e502009-03-09 13:08:16 +00003418 PJ_ASSERT_RETURN(pjsua_var.snd_port != NULL, PJ_EBUG);
Nanang Izzuddin3c1ae632008-08-21 15:04:20 +00003419
Benny Prijonof798e502009-03-09 13:08:16 +00003420 strm = pjmedia_snd_port_get_snd_stream(pjsua_var.snd_port);
Benny Prijono26056d82006-10-11 16:03:41 +00003421
Benny Prijonof798e502009-03-09 13:08:16 +00003422 status = pjmedia_aud_stream_get_param(strm, &param);
Benny Prijonoeebe9af2006-06-13 22:57:13 +00003423 if (status != PJ_SUCCESS) {
Benny Prijonof798e502009-03-09 13:08:16 +00003424 pjsua_perror(THIS_FILE, "Error audio stream "
3425 "device parameters", status);
Benny Prijonoeebe9af2006-06-13 22:57:13 +00003426 return status;
3427 }
3428
Benny Prijonof798e502009-03-09 13:08:16 +00003429#define update_saved_param(cap, field) \
3430 if (param.flags & cap) { \
3431 pjsua_var.aud_param.flags |= cap; \
3432 pjsua_var.aud_param.field = param.field; \
3433 }
3434
3435 update_saved_param(PJMEDIA_AUD_DEV_CAP_INPUT_VOLUME_SETTING, input_vol);
3436 update_saved_param(PJMEDIA_AUD_DEV_CAP_OUTPUT_VOLUME_SETTING, output_vol);
3437 update_saved_param(PJMEDIA_AUD_DEV_CAP_INPUT_ROUTE, input_route);
3438 update_saved_param(PJMEDIA_AUD_DEV_CAP_OUTPUT_ROUTE, output_route);
3439#undef update_saved_param
3440
3441 return PJ_SUCCESS;
3442}
3443
3444/* Get format name */
3445static const char *get_fmt_name(pj_uint32_t id)
3446{
3447 static char name[8];
3448
3449 if (id == PJMEDIA_FORMAT_L16)
3450 return "PCM";
3451 pj_memcpy(name, &id, 4);
3452 name[4] = '\0';
3453 return name;
3454}
3455
3456/* Open sound device with the setting. */
Sauw Ming98766c72011-03-11 06:57:24 +00003457static pj_status_t open_snd_dev(pjmedia_snd_port_param *param)
Benny Prijonof798e502009-03-09 13:08:16 +00003458{
3459 pjmedia_port *conf_port;
3460 pj_status_t status;
3461
3462 PJ_ASSERT_RETURN(param, PJ_EINVAL);
3463
3464 /* Check if NULL sound device is used */
Sauw Ming98766c72011-03-11 06:57:24 +00003465 if (NULL_SND_DEV_ID==param->base.rec_id ||
3466 NULL_SND_DEV_ID==param->base.play_id)
3467 {
Benny Prijonof798e502009-03-09 13:08:16 +00003468 return pjsua_set_null_snd_dev();
3469 }
3470
3471 /* Close existing sound port */
3472 close_snd_dev();
3473
Benny Prijono2d647722011-07-13 03:05:22 +00003474 /* Notify app */
3475 if (pjsua_var.ua_cfg.cb.on_snd_dev_operation) {
3476 (*pjsua_var.ua_cfg.cb.on_snd_dev_operation)(1);
3477 }
3478
Benny Prijonof798e502009-03-09 13:08:16 +00003479 /* Create memory pool for sound device. */
3480 pjsua_var.snd_pool = pjsua_pool_create("pjsua_snd", 4000, 4000);
3481 PJ_ASSERT_RETURN(pjsua_var.snd_pool, PJ_ENOMEM);
3482
3483
3484 PJ_LOG(4,(THIS_FILE, "Opening sound device %s@%d/%d/%dms",
Sauw Ming98766c72011-03-11 06:57:24 +00003485 get_fmt_name(param->base.ext_fmt.id),
3486 param->base.clock_rate, param->base.channel_count,
3487 param->base.samples_per_frame / param->base.channel_count *
3488 1000 / param->base.clock_rate));
Benny Prijonob90fd382011-09-18 14:59:56 +00003489 pj_log_push_indent();
Benny Prijonof798e502009-03-09 13:08:16 +00003490
3491 status = pjmedia_snd_port_create2( pjsua_var.snd_pool,
Sauw Ming98766c72011-03-11 06:57:24 +00003492 param, &pjsua_var.snd_port);
Benny Prijonof798e502009-03-09 13:08:16 +00003493 if (status != PJ_SUCCESS)
Benny Prijonob90fd382011-09-18 14:59:56 +00003494 goto on_error;
Benny Prijonof798e502009-03-09 13:08:16 +00003495
3496 /* Get the port0 of the conference bridge. */
3497 conf_port = pjmedia_conf_get_master_port(pjsua_var.mconf);
3498 pj_assert(conf_port != NULL);
3499
3500 /* For conference bridge, resample if necessary if the bridge's
3501 * clock rate is different than the sound device's clock rate.
3502 */
3503 if (!pjsua_var.is_mswitch &&
Sauw Ming98766c72011-03-11 06:57:24 +00003504 param->base.ext_fmt.id == PJMEDIA_FORMAT_PCM &&
Nanang Izzuddinfe68f1d2011-07-19 03:42:28 +00003505 PJMEDIA_PIA_SRATE(&conf_port->info) != param->base.clock_rate)
Benny Prijonof798e502009-03-09 13:08:16 +00003506 {
3507 pjmedia_port *resample_port;
3508 unsigned resample_opt = 0;
3509
3510 if (pjsua_var.media_cfg.quality >= 3 &&
3511 pjsua_var.media_cfg.quality <= 4)
3512 {
3513 resample_opt |= PJMEDIA_RESAMPLE_USE_SMALL_FILTER;
3514 }
3515 else if (pjsua_var.media_cfg.quality < 3) {
3516 resample_opt |= PJMEDIA_RESAMPLE_USE_LINEAR;
3517 }
3518
3519 status = pjmedia_resample_port_create(pjsua_var.snd_pool,
3520 conf_port,
Sauw Ming98766c72011-03-11 06:57:24 +00003521 param->base.clock_rate,
Benny Prijonof798e502009-03-09 13:08:16 +00003522 resample_opt,
3523 &resample_port);
3524 if (status != PJ_SUCCESS) {
3525 char errmsg[PJ_ERR_MSG_SIZE];
3526 pj_strerror(status, errmsg, sizeof(errmsg));
3527 PJ_LOG(4, (THIS_FILE,
3528 "Error creating resample port: %s",
3529 errmsg));
3530 close_snd_dev();
Benny Prijonob90fd382011-09-18 14:59:56 +00003531 goto on_error;
Benny Prijonof798e502009-03-09 13:08:16 +00003532 }
3533
3534 conf_port = resample_port;
3535 }
3536
3537 /* Otherwise for audio switchboard, the switch's port0 setting is
3538 * derived from the sound device setting, so update the setting.
3539 */
3540 if (pjsua_var.is_mswitch) {
Nanang Izzuddinfe68f1d2011-07-19 03:42:28 +00003541 pj_memcpy(&conf_port->info.fmt, &param->base.ext_fmt,
Benny Prijonoc45d9512010-12-10 11:04:30 +00003542 sizeof(conf_port->info.fmt));
Nanang Izzuddinfe68f1d2011-07-19 03:42:28 +00003543 conf_port->info.fmt.det.aud.clock_rate = param->base.clock_rate;
3544 conf_port->info.fmt.det.aud.frame_time_usec = param->base.samples_per_frame*
Benny Prijonoc45d9512010-12-10 11:04:30 +00003545 1000000 /
Nanang Izzuddinfe68f1d2011-07-19 03:42:28 +00003546 param->base.clock_rate;
3547 conf_port->info.fmt.det.aud.channel_count = param->base.channel_count;
Benny Prijonoc45d9512010-12-10 11:04:30 +00003548 conf_port->info.fmt.det.aud.bits_per_sample = 16;
Benny Prijonof798e502009-03-09 13:08:16 +00003549 }
3550
Benny Prijonoc45d9512010-12-10 11:04:30 +00003551
Benny Prijonof798e502009-03-09 13:08:16 +00003552 /* Connect sound port to the bridge */
Benny Prijono52a93912006-08-04 20:54:37 +00003553 status = pjmedia_snd_port_connect(pjsua_var.snd_port,
3554 conf_port );
3555 if (status != PJ_SUCCESS) {
3556 pjsua_perror(THIS_FILE, "Unable to connect conference port to "
3557 "sound device", status);
3558 pjmedia_snd_port_destroy(pjsua_var.snd_port);
3559 pjsua_var.snd_port = NULL;
Benny Prijonob90fd382011-09-18 14:59:56 +00003560 goto on_error;
Benny Prijono52a93912006-08-04 20:54:37 +00003561 }
3562
Benny Prijonoeebe9af2006-06-13 22:57:13 +00003563 /* Save the device IDs */
Sauw Ming98766c72011-03-11 06:57:24 +00003564 pjsua_var.cap_dev = param->base.rec_id;
3565 pjsua_var.play_dev = param->base.play_id;
Benny Prijonoeebe9af2006-06-13 22:57:13 +00003566
Benny Prijonoc53c6d72006-11-27 09:54:03 +00003567 /* Update sound device name. */
Benny Prijonof798e502009-03-09 13:08:16 +00003568 {
3569 pjmedia_aud_dev_info rec_info;
3570 pjmedia_aud_stream *strm;
3571 pjmedia_aud_param si;
3572 pj_str_t tmp;
Benny Prijonoc53c6d72006-11-27 09:54:03 +00003573
Benny Prijonof798e502009-03-09 13:08:16 +00003574 strm = pjmedia_snd_port_get_snd_stream(pjsua_var.snd_port);
3575 status = pjmedia_aud_stream_get_param(strm, &si);
3576 if (status == PJ_SUCCESS)
3577 status = pjmedia_aud_dev_get_info(si.rec_id, &rec_info);
Benny Prijonof3758ee2008-02-26 15:32:16 +00003578
Benny Prijonof798e502009-03-09 13:08:16 +00003579 if (status==PJ_SUCCESS) {
Sauw Ming98766c72011-03-11 06:57:24 +00003580 if (param->base.clock_rate != pjsua_var.media_cfg.clock_rate) {
Benny Prijonof798e502009-03-09 13:08:16 +00003581 char tmp_buf[128];
3582 int tmp_buf_len = sizeof(tmp_buf);
3583
3584 tmp_buf_len = pj_ansi_snprintf(tmp_buf, sizeof(tmp_buf)-1,
3585 "%s (%dKHz)",
3586 rec_info.name,
Sauw Ming98766c72011-03-11 06:57:24 +00003587 param->base.clock_rate/1000);
Benny Prijonof798e502009-03-09 13:08:16 +00003588 pj_strset(&tmp, tmp_buf, tmp_buf_len);
3589 pjmedia_conf_set_port0_name(pjsua_var.mconf, &tmp);
3590 } else {
3591 pjmedia_conf_set_port0_name(pjsua_var.mconf,
3592 pj_cstr(&tmp, rec_info.name));
3593 }
3594 }
3595
3596 /* Any error is not major, let it through */
3597 status = PJ_SUCCESS;
Benny Prijonob90fd382011-09-18 14:59:56 +00003598 }
Benny Prijonof798e502009-03-09 13:08:16 +00003599
3600 /* If this is the first time the audio device is open, retrieve some
3601 * settings from the device (such as volume settings) so that the
3602 * pjsua_snd_get_setting() work.
3603 */
3604 if (pjsua_var.aud_open_cnt == 0) {
3605 update_initial_aud_param();
3606 ++pjsua_var.aud_open_cnt;
Benny Prijonof3758ee2008-02-26 15:32:16 +00003607 }
Benny Prijonoc53c6d72006-11-27 09:54:03 +00003608
Benny Prijonob90fd382011-09-18 14:59:56 +00003609 pj_log_pop_indent();
Benny Prijonoeebe9af2006-06-13 22:57:13 +00003610 return PJ_SUCCESS;
Benny Prijonob90fd382011-09-18 14:59:56 +00003611
3612on_error:
3613 pj_log_pop_indent();
3614 return status;
Benny Prijonof798e502009-03-09 13:08:16 +00003615}
Nanang Izzuddin8465c682009-03-04 17:23:25 +00003616
Nanang Izzuddin8465c682009-03-04 17:23:25 +00003617
Benny Prijonof798e502009-03-09 13:08:16 +00003618/* Close existing sound device */
3619static void close_snd_dev(void)
3620{
Benny Prijonob90fd382011-09-18 14:59:56 +00003621 pj_log_push_indent();
3622
Benny Prijono2d647722011-07-13 03:05:22 +00003623 /* Notify app */
3624 if (pjsua_var.snd_is_on && pjsua_var.ua_cfg.cb.on_snd_dev_operation) {
3625 (*pjsua_var.ua_cfg.cb.on_snd_dev_operation)(0);
3626 }
3627
Benny Prijonof798e502009-03-09 13:08:16 +00003628 /* Close sound device */
3629 if (pjsua_var.snd_port) {
3630 pjmedia_aud_dev_info cap_info, play_info;
3631 pjmedia_aud_stream *strm;
3632 pjmedia_aud_param param;
3633
3634 strm = pjmedia_snd_port_get_snd_stream(pjsua_var.snd_port);
3635 pjmedia_aud_stream_get_param(strm, &param);
3636
3637 if (pjmedia_aud_dev_get_info(param.rec_id, &cap_info) != PJ_SUCCESS)
3638 cap_info.name[0] = '\0';
3639 if (pjmedia_aud_dev_get_info(param.play_id, &play_info) != PJ_SUCCESS)
3640 play_info.name[0] = '\0';
3641
3642 PJ_LOG(4,(THIS_FILE, "Closing %s sound playback device and "
3643 "%s sound capture device",
3644 play_info.name, cap_info.name));
3645
3646 pjmedia_snd_port_disconnect(pjsua_var.snd_port);
3647 pjmedia_snd_port_destroy(pjsua_var.snd_port);
3648 pjsua_var.snd_port = NULL;
3649 }
3650
3651 /* Close null sound device */
3652 if (pjsua_var.null_snd) {
3653 PJ_LOG(4,(THIS_FILE, "Closing null sound device.."));
3654 pjmedia_master_port_destroy(pjsua_var.null_snd, PJ_FALSE);
3655 pjsua_var.null_snd = NULL;
3656 }
3657
3658 if (pjsua_var.snd_pool)
3659 pj_pool_release(pjsua_var.snd_pool);
3660 pjsua_var.snd_pool = NULL;
Benny Prijono2d647722011-07-13 03:05:22 +00003661 pjsua_var.snd_is_on = PJ_FALSE;
Benny Prijonob90fd382011-09-18 14:59:56 +00003662
3663 pj_log_pop_indent();
Benny Prijonof798e502009-03-09 13:08:16 +00003664}
3665
3666
3667/*
3668 * Select or change sound device. Application may call this function at
3669 * any time to replace current sound device.
3670 */
3671PJ_DEF(pj_status_t) pjsua_set_snd_dev( int capture_dev,
3672 int playback_dev)
3673{
3674 unsigned alt_cr_cnt = 1;
3675 unsigned alt_cr[] = {0, 44100, 48000, 32000, 16000, 8000};
3676 unsigned i;
3677 pj_status_t status = -1;
3678
Benny Prijonob90fd382011-09-18 14:59:56 +00003679 PJ_LOG(4,(THIS_FILE, "Set sound device: capture=%d, playback=%d",
3680 capture_dev, playback_dev));
3681 pj_log_push_indent();
3682
Benny Prijono23ea21a2009-06-03 12:43:06 +00003683 /* Null-sound */
3684 if (capture_dev==NULL_SND_DEV_ID && playback_dev==NULL_SND_DEV_ID) {
Benny Prijonob90fd382011-09-18 14:59:56 +00003685 status = pjsua_set_null_snd_dev();
3686 pj_log_pop_indent();
3687 return status;
Benny Prijono23ea21a2009-06-03 12:43:06 +00003688 }
3689
Benny Prijonof798e502009-03-09 13:08:16 +00003690 /* Set default clock rate */
3691 alt_cr[0] = pjsua_var.media_cfg.snd_clock_rate;
3692 if (alt_cr[0] == 0)
3693 alt_cr[0] = pjsua_var.media_cfg.clock_rate;
3694
3695 /* Allow retrying of different clock rate if we're using conference
3696 * bridge (meaning audio format is always PCM), otherwise lock on
3697 * to one clock rate.
3698 */
3699 if (pjsua_var.is_mswitch) {
3700 alt_cr_cnt = 1;
3701 } else {
3702 alt_cr_cnt = PJ_ARRAY_SIZE(alt_cr);
3703 }
3704
3705 /* Attempts to open the sound device with different clock rates */
3706 for (i=0; i<alt_cr_cnt; ++i) {
Sauw Ming98766c72011-03-11 06:57:24 +00003707 pjmedia_snd_port_param param;
Benny Prijonof798e502009-03-09 13:08:16 +00003708 unsigned samples_per_frame;
3709
3710 /* Create the default audio param */
3711 samples_per_frame = alt_cr[i] *
3712 pjsua_var.media_cfg.audio_frame_ptime *
3713 pjsua_var.media_cfg.channel_count / 1000;
Sauw Ming98766c72011-03-11 06:57:24 +00003714 status = create_aud_param(&param.base, capture_dev, playback_dev,
Benny Prijonof798e502009-03-09 13:08:16 +00003715 alt_cr[i], pjsua_var.media_cfg.channel_count,
3716 samples_per_frame, 16);
3717 if (status != PJ_SUCCESS)
Benny Prijonob90fd382011-09-18 14:59:56 +00003718 goto on_error;
Benny Prijonof798e502009-03-09 13:08:16 +00003719
3720 /* Open! */
Sauw Ming98766c72011-03-11 06:57:24 +00003721 param.options = 0;
Benny Prijonof798e502009-03-09 13:08:16 +00003722 status = open_snd_dev(&param);
3723 if (status == PJ_SUCCESS)
3724 break;
3725 }
3726
3727 if (status != PJ_SUCCESS) {
3728 pjsua_perror(THIS_FILE, "Unable to open sound device", status);
Benny Prijonob90fd382011-09-18 14:59:56 +00003729 goto on_error;
Benny Prijonof798e502009-03-09 13:08:16 +00003730 }
3731
Benny Prijonoe25fe6f2009-07-16 17:52:08 +00003732 pjsua_var.no_snd = PJ_FALSE;
Benny Prijono2d647722011-07-13 03:05:22 +00003733 pjsua_var.snd_is_on = PJ_TRUE;
Benny Prijonoe25fe6f2009-07-16 17:52:08 +00003734
Benny Prijonob90fd382011-09-18 14:59:56 +00003735 pj_log_pop_indent();
Benny Prijonof798e502009-03-09 13:08:16 +00003736 return PJ_SUCCESS;
Benny Prijonob90fd382011-09-18 14:59:56 +00003737
3738on_error:
3739 pj_log_pop_indent();
3740 return status;
Benny Prijonoeebe9af2006-06-13 22:57:13 +00003741}
3742
3743
3744/*
Benny Prijonoebdf8772007-02-01 19:25:50 +00003745 * Get currently active sound devices. If sound devices has not been created
3746 * (for example when pjsua_start() is not called), it is possible that
3747 * the function returns PJ_SUCCESS with -1 as device IDs.
3748 */
3749PJ_DEF(pj_status_t) pjsua_get_snd_dev(int *capture_dev,
3750 int *playback_dev)
3751{
3752 if (capture_dev) {
3753 *capture_dev = pjsua_var.cap_dev;
3754 }
3755 if (playback_dev) {
3756 *playback_dev = pjsua_var.play_dev;
3757 }
3758
3759 return PJ_SUCCESS;
3760}
3761
3762
3763/*
Benny Prijonoeebe9af2006-06-13 22:57:13 +00003764 * Use null sound device.
3765 */
3766PJ_DEF(pj_status_t) pjsua_set_null_snd_dev(void)
3767{
3768 pjmedia_port *conf_port;
3769 pj_status_t status;
3770
Benny Prijonob90fd382011-09-18 14:59:56 +00003771 PJ_LOG(4,(THIS_FILE, "Setting null sound device.."));
3772 pj_log_push_indent();
3773
3774
Benny Prijonoeebe9af2006-06-13 22:57:13 +00003775 /* Close existing sound device */
3776 close_snd_dev();
3777
Benny Prijono2d647722011-07-13 03:05:22 +00003778 /* Notify app */
3779 if (pjsua_var.ua_cfg.cb.on_snd_dev_operation) {
3780 (*pjsua_var.ua_cfg.cb.on_snd_dev_operation)(1);
3781 }
3782
Nanang Izzuddin68559c32008-06-13 17:01:46 +00003783 /* Create memory pool for sound device. */
3784 pjsua_var.snd_pool = pjsua_pool_create("pjsua_snd", 4000, 4000);
3785 PJ_ASSERT_RETURN(pjsua_var.snd_pool, PJ_ENOMEM);
3786
Benny Prijonoeebe9af2006-06-13 22:57:13 +00003787 PJ_LOG(4,(THIS_FILE, "Opening null sound device.."));
3788
3789 /* Get the port0 of the conference bridge. */
3790 conf_port = pjmedia_conf_get_master_port(pjsua_var.mconf);
3791 pj_assert(conf_port != NULL);
3792
3793 /* Create master port, connecting port0 of the conference bridge to
3794 * a null port.
3795 */
Nanang Izzuddin68559c32008-06-13 17:01:46 +00003796 status = pjmedia_master_port_create(pjsua_var.snd_pool, pjsua_var.null_port,
Benny Prijonoeebe9af2006-06-13 22:57:13 +00003797 conf_port, 0, &pjsua_var.null_snd);
3798 if (status != PJ_SUCCESS) {
3799 pjsua_perror(THIS_FILE, "Unable to create null sound device",
3800 status);
Benny Prijonob90fd382011-09-18 14:59:56 +00003801 pj_log_pop_indent();
Benny Prijonoeebe9af2006-06-13 22:57:13 +00003802 return status;
3803 }
3804
3805 /* Start the master port */
3806 status = pjmedia_master_port_start(pjsua_var.null_snd);
3807 PJ_ASSERT_RETURN(status == PJ_SUCCESS, status);
3808
Nanang Izzuddin68559c32008-06-13 17:01:46 +00003809 pjsua_var.cap_dev = NULL_SND_DEV_ID;
3810 pjsua_var.play_dev = NULL_SND_DEV_ID;
3811
Benny Prijonoe25fe6f2009-07-16 17:52:08 +00003812 pjsua_var.no_snd = PJ_FALSE;
Benny Prijono2d647722011-07-13 03:05:22 +00003813 pjsua_var.snd_is_on = PJ_TRUE;
Benny Prijonoe25fe6f2009-07-16 17:52:08 +00003814
Benny Prijonob90fd382011-09-18 14:59:56 +00003815 pj_log_pop_indent();
Benny Prijonoeebe9af2006-06-13 22:57:13 +00003816 return PJ_SUCCESS;
3817}
3818
3819
Benny Prijonoe909eac2006-07-27 22:04:56 +00003820
3821/*
3822 * Use no device!
3823 */
3824PJ_DEF(pjmedia_port*) pjsua_set_no_snd_dev(void)
3825{
3826 /* Close existing sound device */
3827 close_snd_dev();
3828
3829 pjsua_var.no_snd = PJ_TRUE;
3830 return pjmedia_conf_get_master_port(pjsua_var.mconf);
3831}
3832
3833
Benny Prijonof20687a2006-08-04 18:27:19 +00003834/*
3835 * Configure the AEC settings of the sound port.
3836 */
Benny Prijono5da50432006-08-07 10:24:52 +00003837PJ_DEF(pj_status_t) pjsua_set_ec(unsigned tail_ms, unsigned options)
Benny Prijonof20687a2006-08-04 18:27:19 +00003838{
3839 pjsua_var.media_cfg.ec_tail_len = tail_ms;
3840
3841 if (pjsua_var.snd_port)
Benny Prijono5da50432006-08-07 10:24:52 +00003842 return pjmedia_snd_port_set_ec( pjsua_var.snd_port, pjsua_var.pool,
3843 tail_ms, options);
Benny Prijonof20687a2006-08-04 18:27:19 +00003844
3845 return PJ_SUCCESS;
3846}
3847
3848
3849/*
3850 * Get current AEC tail length.
3851 */
Benny Prijono22dfe592006-08-06 12:07:13 +00003852PJ_DEF(pj_status_t) pjsua_get_ec_tail(unsigned *p_tail_ms)
Benny Prijonof20687a2006-08-04 18:27:19 +00003853{
3854 *p_tail_ms = pjsua_var.media_cfg.ec_tail_len;
3855 return PJ_SUCCESS;
3856}
3857
Nanang Izzuddinfe02a062009-02-18 14:28:49 +00003858
Nanang Izzuddin0cb3b022009-02-27 17:37:35 +00003859/*
Benny Prijonof798e502009-03-09 13:08:16 +00003860 * Check whether the sound device is currently active.
Nanang Izzuddin0cb3b022009-02-27 17:37:35 +00003861 */
Benny Prijonof798e502009-03-09 13:08:16 +00003862PJ_DEF(pj_bool_t) pjsua_snd_is_active(void)
Nanang Izzuddin0cb3b022009-02-27 17:37:35 +00003863{
Benny Prijonof798e502009-03-09 13:08:16 +00003864 return pjsua_var.snd_port != NULL;
3865}
Nanang Izzuddin0cb3b022009-02-27 17:37:35 +00003866
Nanang Izzuddin0cb3b022009-02-27 17:37:35 +00003867
Benny Prijonof798e502009-03-09 13:08:16 +00003868/*
3869 * Configure sound device setting to the sound device being used.
3870 */
3871PJ_DEF(pj_status_t) pjsua_snd_set_setting( pjmedia_aud_dev_cap cap,
3872 const void *pval,
3873 pj_bool_t keep)
3874{
Benny Prijono09b0ff62009-03-10 12:07:51 +00003875 pj_status_t status;
3876
Benny Prijonof798e502009-03-09 13:08:16 +00003877 /* Check if we are allowed to set the cap */
Benny Prijono09b0ff62009-03-10 12:07:51 +00003878 if ((cap & pjsua_var.aud_svmask) == 0) {
Benny Prijonof798e502009-03-09 13:08:16 +00003879 return PJMEDIA_EAUD_INVCAP;
3880 }
3881
Benny Prijono09b0ff62009-03-10 12:07:51 +00003882 /* If sound is active, set it immediately */
Benny Prijonof798e502009-03-09 13:08:16 +00003883 if (pjsua_snd_is_active()) {
Benny Prijonof798e502009-03-09 13:08:16 +00003884 pjmedia_aud_stream *strm;
3885
3886 strm = pjmedia_snd_port_get_snd_stream(pjsua_var.snd_port);
Benny Prijono09b0ff62009-03-10 12:07:51 +00003887 status = pjmedia_aud_stream_set_cap(strm, cap, pval);
Benny Prijonof798e502009-03-09 13:08:16 +00003888 } else {
Benny Prijono09b0ff62009-03-10 12:07:51 +00003889 status = PJ_SUCCESS;
Benny Prijonof798e502009-03-09 13:08:16 +00003890 }
Benny Prijono09b0ff62009-03-10 12:07:51 +00003891
3892 if (status != PJ_SUCCESS)
3893 return status;
3894
3895 /* Save in internal param for later device open */
3896 if (keep) {
3897 status = pjmedia_aud_param_set_cap(&pjsua_var.aud_param,
3898 cap, pval);
3899 }
3900
3901 return status;
Benny Prijonof798e502009-03-09 13:08:16 +00003902}
3903
3904/*
3905 * Retrieve a sound device setting.
3906 */
3907PJ_DEF(pj_status_t) pjsua_snd_get_setting( pjmedia_aud_dev_cap cap,
3908 void *pval)
3909{
3910 /* If sound device has never been opened before, open it to
3911 * retrieve the initial setting from the device (e.g. audio
3912 * volume)
3913 */
Benny Prijono09b0ff62009-03-10 12:07:51 +00003914 if (pjsua_var.aud_open_cnt==0) {
3915 PJ_LOG(4,(THIS_FILE, "Opening sound device to get initial settings"));
Benny Prijonof798e502009-03-09 13:08:16 +00003916 pjsua_set_snd_dev(pjsua_var.cap_dev, pjsua_var.play_dev);
Benny Prijono09b0ff62009-03-10 12:07:51 +00003917 close_snd_dev();
3918 }
Benny Prijonof798e502009-03-09 13:08:16 +00003919
3920 if (pjsua_snd_is_active()) {
3921 /* Sound is active, retrieve from device directly */
3922 pjmedia_aud_stream *strm;
3923
3924 strm = pjmedia_snd_port_get_snd_stream(pjsua_var.snd_port);
3925 return pjmedia_aud_stream_get_cap(strm, cap, pval);
3926 } else {
3927 /* Otherwise retrieve from internal param */
3928 return pjmedia_aud_param_get_cap(&pjsua_var.aud_param,
3929 cap, pval);
3930 }
Nanang Izzuddin0cb3b022009-02-27 17:37:35 +00003931}
3932
3933
Benny Prijonoeebe9af2006-06-13 22:57:13 +00003934/*****************************************************************************
3935 * Codecs.
3936 */
3937
3938/*
3939 * Enum all supported codecs in the system.
3940 */
3941PJ_DEF(pj_status_t) pjsua_enum_codecs( pjsua_codec_info id[],
3942 unsigned *p_count )
3943{
3944 pjmedia_codec_mgr *codec_mgr;
3945 pjmedia_codec_info info[32];
3946 unsigned i, count, prio[32];
3947 pj_status_t status;
3948
3949 codec_mgr = pjmedia_endpt_get_codec_mgr(pjsua_var.med_endpt);
3950 count = PJ_ARRAY_SIZE(info);
3951 status = pjmedia_codec_mgr_enum_codecs( codec_mgr, &count, info, prio);
3952 if (status != PJ_SUCCESS) {
3953 *p_count = 0;
3954 return status;
3955 }
3956
3957 if (count > *p_count) count = *p_count;
3958
3959 for (i=0; i<count; ++i) {
Nanang Izzuddin56b2ce42011-04-06 13:55:01 +00003960 pj_bzero(&id[i], sizeof(pjsua_codec_info));
3961
Benny Prijonoeebe9af2006-06-13 22:57:13 +00003962 pjmedia_codec_info_to_id(&info[i], id[i].buf_, sizeof(id[i].buf_));
3963 id[i].codec_id = pj_str(id[i].buf_);
3964 id[i].priority = (pj_uint8_t) prio[i];
3965 }
3966
3967 *p_count = count;
3968
3969 return PJ_SUCCESS;
3970}
3971
3972
3973/*
3974 * Change codec priority.
3975 */
3976PJ_DEF(pj_status_t) pjsua_codec_set_priority( const pj_str_t *codec_id,
3977 pj_uint8_t priority )
3978{
Benny Prijono88accae2008-06-26 15:48:14 +00003979 const pj_str_t all = { NULL, 0 };
Benny Prijonoeebe9af2006-06-13 22:57:13 +00003980 pjmedia_codec_mgr *codec_mgr;
3981
3982 codec_mgr = pjmedia_endpt_get_codec_mgr(pjsua_var.med_endpt);
3983
Benny Prijono88accae2008-06-26 15:48:14 +00003984 if (codec_id->slen==1 && *codec_id->ptr=='*')
3985 codec_id = &all;
3986
Benny Prijonoeebe9af2006-06-13 22:57:13 +00003987 return pjmedia_codec_mgr_set_codec_priority(codec_mgr, codec_id,
3988 priority);
3989}
3990
3991
3992/*
3993 * Get codec parameters.
3994 */
3995PJ_DEF(pj_status_t) pjsua_codec_get_param( const pj_str_t *codec_id,
3996 pjmedia_codec_param *param )
3997{
Benny Prijono88accae2008-06-26 15:48:14 +00003998 const pj_str_t all = { NULL, 0 };
Benny Prijonoeebe9af2006-06-13 22:57:13 +00003999 const pjmedia_codec_info *info;
4000 pjmedia_codec_mgr *codec_mgr;
4001 unsigned count = 1;
4002 pj_status_t status;
4003
4004 codec_mgr = pjmedia_endpt_get_codec_mgr(pjsua_var.med_endpt);
4005
Benny Prijono88accae2008-06-26 15:48:14 +00004006 if (codec_id->slen==1 && *codec_id->ptr=='*')
4007 codec_id = &all;
4008
Benny Prijonoeebe9af2006-06-13 22:57:13 +00004009 status = pjmedia_codec_mgr_find_codecs_by_id(codec_mgr, codec_id,
4010 &count, &info, NULL);
4011 if (status != PJ_SUCCESS)
4012 return status;
4013
4014 if (count != 1)
Nanang Izzuddin50fae732011-03-22 09:49:23 +00004015 return (count > 1? PJ_ETOOMANY : PJ_ENOTFOUND);
Benny Prijonoeebe9af2006-06-13 22:57:13 +00004016
4017 status = pjmedia_codec_mgr_get_default_param( codec_mgr, info, param);
4018 return status;
4019}
4020
4021
4022/*
4023 * Set codec parameters.
4024 */
Nanang Izzuddin06839e72010-01-27 11:48:31 +00004025PJ_DEF(pj_status_t) pjsua_codec_set_param( const pj_str_t *codec_id,
Benny Prijonoeebe9af2006-06-13 22:57:13 +00004026 const pjmedia_codec_param *param)
4027{
Nanang Izzuddin06839e72010-01-27 11:48:31 +00004028 const pjmedia_codec_info *info[2];
4029 pjmedia_codec_mgr *codec_mgr;
4030 unsigned count = 2;
4031 pj_status_t status;
4032
4033 codec_mgr = pjmedia_endpt_get_codec_mgr(pjsua_var.med_endpt);
4034
4035 status = pjmedia_codec_mgr_find_codecs_by_id(codec_mgr, codec_id,
4036 &count, info, NULL);
4037 if (status != PJ_SUCCESS)
4038 return status;
4039
4040 /* Codec ID should be specific, except for G.722.1 */
4041 if (count > 1 &&
4042 pj_strnicmp2(codec_id, "G7221/16", 8) != 0 &&
4043 pj_strnicmp2(codec_id, "G7221/32", 8) != 0)
4044 {
4045 pj_assert(!"Codec ID is not specific");
4046 return PJ_ETOOMANY;
4047 }
4048
4049 status = pjmedia_codec_mgr_set_default_param(codec_mgr, info[0], param);
4050 return status;
Benny Prijonoeebe9af2006-06-13 22:57:13 +00004051}
Nanang Izzuddin50fae732011-03-22 09:49:23 +00004052
4053