blob: 932fdc8e16123988353e4cef889735a167114d00 [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) {
1504 pj_pool_t *tmp_pool = (call->inv? call->inv->pool_prov:
1505 call->async_call.dlg->pool);
1506
1507 status = pjmedia_transport_media_create(
1508 call_med->tp, tmp_pool,
1509 0, call->async_call.rem_sdp, mi);
1510 if (status != PJ_SUCCESS) {
1511 call->med_ch_info.status = status;
1512 call->med_ch_info.med_idx = mi;
1513 call->med_ch_info.state = call_med->tp_st;
1514 call->med_ch_info.sip_err_code = PJSIP_SC_NOT_ACCEPTABLE;
1515 pjsua_media_channel_deinit(call_id);
1516 goto on_error;
1517 }
1518
1519 set_media_tp_state(call_med, PJSUA_MED_TP_INIT);
1520 }
1521 }
1522
1523 call->med_ch_info.status = PJ_SUCCESS;
1524
1525on_error:
1526 if (call->med_ch_cb)
1527 (*call->med_ch_cb)(call->index, &call->med_ch_info);
1528
Benny Prijono0bc99a92011-03-17 04:34:43 +00001529 return status;
1530}
Nanang Izzuddin1e952a82010-10-05 16:32:04 +00001531
Benny Prijonod8179652008-01-23 20:39:07 +00001532pj_status_t pjsua_media_channel_init(pjsua_call_id call_id,
1533 pjsip_role_e role,
1534 int security_level,
1535 pj_pool_t *tmp_pool,
1536 const pjmedia_sdp_session *rem_sdp,
Sauw Ming73ecfe82011-09-21 10:20:01 +00001537 int *sip_err_code,
1538 pj_bool_t async,
1539 pjsua_med_tp_state_cb cb)
Benny Prijonoc97608e2007-03-23 16:34:20 +00001540{
Benny Prijono0bc99a92011-03-17 04:34:43 +00001541 const pj_str_t STR_AUDIO = { "audio", 5 };
1542 const pj_str_t STR_VIDEO = { "video", 5 };
Benny Prijonod8179652008-01-23 20:39:07 +00001543 pjsua_call *call = &pjsua_var.calls[call_id];
Benny Prijonod8179652008-01-23 20:39:07 +00001544 pjsua_acc *acc = &pjsua_var.acc[call->acc_id];
Benny Prijono0bc99a92011-03-17 04:34:43 +00001545 pj_uint8_t maudidx[PJSUA_MAX_CALL_MEDIA];
1546 unsigned maudcnt = PJ_ARRAY_SIZE(maudidx);
1547 pj_uint8_t mvididx[PJSUA_MAX_CALL_MEDIA];
1548 unsigned mvidcnt = PJ_ARRAY_SIZE(mvididx);
1549 pjmedia_type media_types[PJSUA_MAX_CALL_MEDIA];
1550 unsigned mi;
Sauw Ming73ecfe82011-09-21 10:20:01 +00001551 pj_bool_t pending_med_tp = PJ_FALSE;
Benny Prijono0bc99a92011-03-17 04:34:43 +00001552 pj_status_t status;
Benny Prijonoc97608e2007-03-23 16:34:20 +00001553
Benny Prijonod8179652008-01-23 20:39:07 +00001554 PJ_UNUSED_ARG(role);
Sauw Ming73ecfe82011-09-21 10:20:01 +00001555 PJ_UNUSED_ARG(tmp_pool);
Benny Prijonod8179652008-01-23 20:39:07 +00001556
Nanang Izzuddinb6c239c2011-05-10 05:42:28 +00001557 /*
1558 * Note: this function may be called when the media already exists
1559 * (e.g. in reinvites, updates, etc).
1560 */
1561
Benny Prijono0bc99a92011-03-17 04:34:43 +00001562 if (pjsua_get_state() != PJSUA_STATE_RUNNING)
1563 return PJ_EBUSY;
1564
Sauw Ming73ecfe82011-09-21 10:20:01 +00001565 if (async) {
1566 pj_pool_t *tmppool = (call->inv? call->inv->pool_prov:
1567 call->async_call.dlg->pool);
1568
1569 status = pj_mutex_create_simple(tmppool, NULL, &call->med_ch_mutex);
1570 if (status != PJ_SUCCESS)
1571 return status;
1572 }
1573
Benny Prijonob90fd382011-09-18 14:59:56 +00001574 PJ_LOG(4,(THIS_FILE, "Call %d: initializing media..", call_id));
1575 pj_log_push_indent();
1576
Benny Prijono0bc99a92011-03-17 04:34:43 +00001577#if DISABLED_FOR_TICKET_1185
Benny Prijonod8179652008-01-23 20:39:07 +00001578 /* Return error if media transport has not been created yet
Benny Prijono68f9e4f2008-03-21 08:56:02 +00001579 * (e.g. application is starting)
1580 */
Benny Prijono0bc99a92011-03-17 04:34:43 +00001581 for (i=0; i<call->med_cnt; ++i) {
1582 if (call->media[i].tp == NULL) {
Benny Prijonob90fd382011-09-18 14:59:56 +00001583 status = PJ_EBUSY;
1584 goto on_error;
Benny Prijonod8179652008-01-23 20:39:07 +00001585 }
Benny Prijonoc97608e2007-03-23 16:34:20 +00001586 }
Benny Prijonod8179652008-01-23 20:39:07 +00001587#endif
Benny Prijonoc97608e2007-03-23 16:34:20 +00001588
Benny Prijono0bc99a92011-03-17 04:34:43 +00001589 if (rem_sdp) {
1590 sort_media(rem_sdp, &STR_AUDIO, acc->cfg.use_srtp,
1591 maudidx, &maudcnt);
Nanang Izzuddind8c33d82011-08-18 18:30:55 +00001592 // Don't apply media count limitation until SDP negotiation is done.
1593 //if (maudcnt > acc->cfg.max_audio_cnt)
1594 // maudcnt = acc->cfg.max_audio_cnt;
Benny Prijonoc97608e2007-03-23 16:34:20 +00001595
Benny Prijono0bc99a92011-03-17 04:34:43 +00001596 if (maudcnt==0) {
1597 /* Expecting audio in the offer */
1598 if (sip_err_code) *sip_err_code = PJSIP_SC_NOT_ACCEPTABLE_HERE;
1599 pjsua_media_channel_deinit(call_id);
Benny Prijonob90fd382011-09-18 14:59:56 +00001600 status = PJSIP_ERRNO_FROM_SIP_STATUS(PJSIP_SC_NOT_ACCEPTABLE_HERE);
1601 goto on_error;
Benny Prijonoc97608e2007-03-23 16:34:20 +00001602 }
1603
Nanang Izzuddind8c33d82011-08-18 18:30:55 +00001604#if PJMEDIA_HAS_VIDEO
Benny Prijono0bc99a92011-03-17 04:34:43 +00001605 sort_media(rem_sdp, &STR_VIDEO, acc->cfg.use_srtp,
1606 mvididx, &mvidcnt);
Nanang Izzuddind8c33d82011-08-18 18:30:55 +00001607 // Don't apply media count limitation until SDP negotiation is done.
1608 //if (mvidcnt > acc->cfg.max_video_cnt)
1609 //mvidcnt = acc->cfg.max_video_cnt;
1610#else
1611 mvidcnt = 0;
1612#endif
Nanang Izzuddinb6c239c2011-05-10 05:42:28 +00001613
1614 /* Update media count only when remote add any media, this media count
1615 * must never decrease.
Benny Prijonod8179652008-01-23 20:39:07 +00001616 */
Nanang Izzuddinb6c239c2011-05-10 05:42:28 +00001617 if (call->med_cnt < rem_sdp->media_count)
1618 call->med_cnt = PJ_MIN(rem_sdp->media_count, PJSUA_MAX_CALL_MEDIA);
Benny Prijonod8179652008-01-23 20:39:07 +00001619
Benny Prijono0bc99a92011-03-17 04:34:43 +00001620 } else {
1621 maudcnt = acc->cfg.max_audio_cnt;
1622 for (mi=0; mi<maudcnt; ++mi) {
Nanang Izzuddinbf26db12011-03-18 07:54:50 +00001623 maudidx[mi] = (pj_uint8_t)mi;
Benny Prijono0bc99a92011-03-17 04:34:43 +00001624 media_types[mi] = PJMEDIA_TYPE_AUDIO;
Benny Prijonod8179652008-01-23 20:39:07 +00001625 }
Nanang Izzuddind8c33d82011-08-18 18:30:55 +00001626#if PJMEDIA_HAS_VIDEO
Benny Prijono0bc99a92011-03-17 04:34:43 +00001627 mvidcnt = acc->cfg.max_video_cnt;
1628 for (mi=0; mi<mvidcnt; ++mi) {
1629 media_types[maudcnt + mi] = PJMEDIA_TYPE_VIDEO;
1630 }
Nanang Izzuddind8c33d82011-08-18 18:30:55 +00001631#else
1632 mvidcnt = 0;
1633#endif
Nanang Izzuddinb6c239c2011-05-10 05:42:28 +00001634 call->med_cnt = maudcnt + mvidcnt;
Benny Prijonoa310bd22008-06-27 21:19:44 +00001635 }
1636
Benny Prijono0bc99a92011-03-17 04:34:43 +00001637 if (call->med_cnt == 0) {
1638 /* Expecting at least one media */
Benny Prijonoab8dba92008-06-27 21:59:15 +00001639 if (sip_err_code) *sip_err_code = PJSIP_SC_NOT_ACCEPTABLE_HERE;
Benny Prijonoa310bd22008-06-27 21:19:44 +00001640 pjsua_media_channel_deinit(call_id);
Benny Prijonob90fd382011-09-18 14:59:56 +00001641 status = PJSIP_ERRNO_FROM_SIP_STATUS(PJSIP_SC_NOT_ACCEPTABLE_HERE);
1642 goto on_error;
Benny Prijonoa310bd22008-06-27 21:19:44 +00001643 }
1644
Sauw Ming73ecfe82011-09-21 10:20:01 +00001645 if (async) {
1646 call->med_ch_cb = cb;
1647 if (rem_sdp) {
1648 /* TODO: change rem_sdp to non-const parameter. */
1649 call->async_call.rem_sdp =
1650 pjmedia_sdp_session_clone(call->inv->pool_prov, rem_sdp);
1651 }
1652 }
1653
Benny Prijono0bc99a92011-03-17 04:34:43 +00001654 /* Initialize each media line */
1655 for (mi=0; mi < call->med_cnt; ++mi) {
1656 pjsua_call_media *call_med = &call->media[mi];
1657 pj_bool_t enabled = PJ_FALSE;
1658 pjmedia_type media_type = PJMEDIA_TYPE_NONE;
Benny Prijonoa310bd22008-06-27 21:19:44 +00001659
Benny Prijono0bc99a92011-03-17 04:34:43 +00001660 if (rem_sdp) {
Nanang Izzuddinb6c239c2011-05-10 05:42:28 +00001661 if (mi >= rem_sdp->media_count) {
1662 /* Media has been removed in remote re-offer */
1663 media_type = call_med->type;
1664 } else if (!pj_stricmp(&rem_sdp->media[mi]->desc.media, &STR_AUDIO)) {
Benny Prijono0bc99a92011-03-17 04:34:43 +00001665 media_type = PJMEDIA_TYPE_AUDIO;
1666 if (pj_memchr(maudidx, mi, maudcnt * sizeof(maudidx[0]))) {
1667 enabled = PJ_TRUE;
1668 }
1669 }
1670 else if (!pj_stricmp(&rem_sdp->media[mi]->desc.media, &STR_VIDEO)) {
1671 media_type = PJMEDIA_TYPE_VIDEO;
1672 if (pj_memchr(mvididx, mi, mvidcnt * sizeof(mvididx[0]))) {
1673 enabled = PJ_TRUE;
1674 }
1675 }
1676
1677 } else {
1678 enabled = PJ_TRUE;
1679 media_type = media_types[mi];
1680 }
1681
1682 if (enabled) {
1683 status = pjsua_call_media_init(call_med, media_type,
1684 &acc->cfg.rtp_cfg,
Sauw Ming73ecfe82011-09-21 10:20:01 +00001685 security_level, sip_err_code,
1686 async,
1687 (async? (pjsua_med_tp_state_cb)
1688 &media_channel_init_cb: NULL));
1689 if (status == PJ_EPENDING) {
1690 pending_med_tp = PJ_TRUE;
1691 } else if (status != PJ_SUCCESS) {
1692 if (pending_med_tp) {
1693 /* Save failure information. */
1694 call_med->tp_ready = status;
1695 pj_bzero(&call->med_ch_info, sizeof(call->med_ch_info));
1696 call->med_ch_info.status = status;
1697 call->med_ch_info.state = call_med->tp_st;
1698 call->med_ch_info.med_idx = call_med->idx;
1699 if (sip_err_code)
1700 call->med_ch_info.sip_err_code = *sip_err_code;
1701
1702 /* We will return failure in the callback later. */
1703 return PJ_EPENDING;
1704 }
1705
1706 pjsua_media_channel_deinit(call_id);
Benny Prijonob90fd382011-09-18 14:59:56 +00001707 goto on_error;
Benny Prijono0bc99a92011-03-17 04:34:43 +00001708 }
1709 } else {
Nanang Izzuddinb6c239c2011-05-10 05:42:28 +00001710 /* By convention, the media is disabled if transport is NULL
1711 * or transport state is PJSUA_MED_TP_DISABLED.
1712 */
Benny Prijono0bc99a92011-03-17 04:34:43 +00001713 if (call_med->tp) {
Nanang Izzuddinb6c239c2011-05-10 05:42:28 +00001714 // Don't close transport here, as SDP negotiation has not been
1715 // done and stream may be still active.
1716 //pjmedia_transport_close(call_med->tp);
1717 //call_med->tp = NULL;
1718 pj_assert(call_med->tp_st == PJSUA_MED_TP_INIT ||
1719 call_med->tp_st == PJSUA_MED_TP_RUNNING);
Sauw Ming73ecfe82011-09-21 10:20:01 +00001720 set_media_tp_state(call_med, PJSUA_MED_TP_DISABLED);
Benny Prijono0bc99a92011-03-17 04:34:43 +00001721 }
Nanang Izzuddinb6c239c2011-05-10 05:42:28 +00001722
1723 /* Put media type just for info */
1724 call_med->type = media_type;
Benny Prijono0bc99a92011-03-17 04:34:43 +00001725 }
Benny Prijono224b4e22008-06-19 14:10:28 +00001726 }
1727
Benny Prijono0bc99a92011-03-17 04:34:43 +00001728 call->audio_idx = maudidx[0];
1729
1730 PJ_LOG(4,(THIS_FILE, "Media index %d selected for audio call %d",
1731 call->audio_idx, call->index));
1732
Sauw Ming73ecfe82011-09-21 10:20:01 +00001733 if (pending_med_tp) {
1734 /* We have a pending media transport initialization. */
1735 pj_log_pop_indent();
1736 return PJ_EPENDING;
Benny Prijono0bc99a92011-03-17 04:34:43 +00001737 }
1738
Sauw Ming73ecfe82011-09-21 10:20:01 +00001739 /* Media transport initialization completed immediately, so
1740 * we don't need to call the callback.
1741 */
1742 call->med_ch_cb = NULL;
1743
1744 status = media_channel_init_cb(call_id, NULL);
1745 if (status != PJ_SUCCESS && sip_err_code)
1746 *sip_err_code = call->med_ch_info.sip_err_code;
1747
Benny Prijonob90fd382011-09-18 14:59:56 +00001748 pj_log_pop_indent();
Sauw Ming73ecfe82011-09-21 10:20:01 +00001749 return status;
Benny Prijonob90fd382011-09-18 14:59:56 +00001750
1751on_error:
Sauw Ming73ecfe82011-09-21 10:20:01 +00001752 if (call->med_ch_mutex) {
1753 pj_mutex_destroy(call->med_ch_mutex);
1754 call->med_ch_mutex = NULL;
1755 }
1756
Benny Prijonob90fd382011-09-18 14:59:56 +00001757 pj_log_pop_indent();
1758 return status;
Benny Prijonoc97608e2007-03-23 16:34:20 +00001759}
1760
1761pj_status_t pjsua_media_channel_create_sdp(pjsua_call_id call_id,
1762 pj_pool_t *pool,
Benny Prijonod8179652008-01-23 20:39:07 +00001763 const pjmedia_sdp_session *rem_sdp,
Benny Prijono25b2ea12008-01-24 19:20:54 +00001764 pjmedia_sdp_session **p_sdp,
Benny Prijono0bc99a92011-03-17 04:34:43 +00001765 int *sip_err_code)
Benny Prijonoc97608e2007-03-23 16:34:20 +00001766{
Benny Prijono0bc99a92011-03-17 04:34:43 +00001767 enum { MAX_MEDIA = PJSUA_MAX_CALL_MEDIA };
Benny Prijonoc97608e2007-03-23 16:34:20 +00001768 pjmedia_sdp_session *sdp;
Benny Prijono0bc99a92011-03-17 04:34:43 +00001769 pj_sockaddr origin;
Benny Prijonoc97608e2007-03-23 16:34:20 +00001770 pjsua_call *call = &pjsua_var.calls[call_id];
Nanang Izzuddin1e952a82010-10-05 16:32:04 +00001771 pjmedia_sdp_neg_state sdp_neg_state = PJMEDIA_SDP_NEG_STATE_NULL;
Benny Prijono0bc99a92011-03-17 04:34:43 +00001772 unsigned mi;
Benny Prijonoc97608e2007-03-23 16:34:20 +00001773 pj_status_t status;
1774
Benny Prijono0bc99a92011-03-17 04:34:43 +00001775 if (pjsua_get_state() != PJSUA_STATE_RUNNING)
Benny Prijono55e82352007-05-10 20:49:08 +00001776 return PJ_EBUSY;
Benny Prijono55e82352007-05-10 20:49:08 +00001777
Benny Prijono0bc99a92011-03-17 04:34:43 +00001778 if (rem_sdp) {
Nanang Izzuddinb6c239c2011-05-10 05:42:28 +00001779 /* If this is a re-offer, let's re-initialize media as remote may
1780 * add or remove media
1781 */
1782 if (call->inv && call->inv->state == PJSIP_INV_STATE_CONFIRMED) {
1783 status = pjsua_media_channel_init(call_id, PJSIP_ROLE_UAS,
1784 call->secure_level, pool,
Sauw Ming73ecfe82011-09-21 10:20:01 +00001785 rem_sdp, sip_err_code,
1786 PJ_FALSE, NULL);
Nanang Izzuddinb6c239c2011-05-10 05:42:28 +00001787 if (status != PJ_SUCCESS)
1788 return status;
Benny Prijono0324ba52010-12-02 10:41:46 +00001789 }
Nanang Izzuddinb6c239c2011-05-10 05:42:28 +00001790
1791#if 0
1792 pjsua_acc *acc = &pjsua_var.acc[call->acc_id];
Benny Prijono0bc99a92011-03-17 04:34:43 +00001793 pj_uint8_t maudidx[PJSUA_MAX_CALL_MEDIA];
1794 unsigned maudcnt = PJ_ARRAY_SIZE(maudidx);
Nanang Izzuddin3150d8b2010-12-01 08:20:28 +00001795
Benny Prijono0bc99a92011-03-17 04:34:43 +00001796 sort_media(rem_sdp, &STR_AUDIO, acc->cfg.use_srtp,
1797 maudidx, &maudcnt);
Nanang Izzuddin3150d8b2010-12-01 08:20:28 +00001798
Benny Prijono0bc99a92011-03-17 04:34:43 +00001799 if (maudcnt==0) {
1800 /* Expecting audio in the offer */
1801 if (sip_err_code) *sip_err_code = PJSIP_SC_NOT_ACCEPTABLE_HERE;
1802 pjsua_media_channel_deinit(call_id);
1803 return PJSIP_ERRNO_FROM_SIP_STATUS(PJSIP_SC_NOT_ACCEPTABLE_HERE);
Benny Prijono224b4e22008-06-19 14:10:28 +00001804 }
Benny Prijono224b4e22008-06-19 14:10:28 +00001805
Benny Prijono0bc99a92011-03-17 04:34:43 +00001806 call->audio_idx = maudidx[0];
Nanang Izzuddinb6c239c2011-05-10 05:42:28 +00001807#endif
Benny Prijono0bc99a92011-03-17 04:34:43 +00001808 } else {
1809 /* Audio is first in our offer, by convention */
Nanang Izzuddinb6c239c2011-05-10 05:42:28 +00001810 // The audio_idx should not be changed here, as this function may be
1811 // called in generating re-offer and the current active audio index
1812 // can be anywhere.
1813 //call->audio_idx = 0;
Benny Prijono224b4e22008-06-19 14:10:28 +00001814 }
1815
Nanang Izzuddinb6c239c2011-05-10 05:42:28 +00001816#if 0
1817 // Since r3512, old-style hold should have got transport, created by
1818 // pjsua_media_channel_init() in initial offer/answer or remote reoffer.
Benny Prijono224b4e22008-06-19 14:10:28 +00001819 /* Create media if it's not created. This could happen when call is
Benny Prijono0bc99a92011-03-17 04:34:43 +00001820 * currently on-hold (with the old style hold)
Benny Prijono224b4e22008-06-19 14:10:28 +00001821 */
Benny Prijono0bc99a92011-03-17 04:34:43 +00001822 if (call->media[call->audio_idx].tp == NULL) {
Benny Prijono224b4e22008-06-19 14:10:28 +00001823 pjsip_role_e role;
1824 role = (rem_sdp ? PJSIP_ROLE_UAS : PJSIP_ROLE_UAC);
1825 status = pjsua_media_channel_init(call_id, role, call->secure_level,
Benny Prijono0bc99a92011-03-17 04:34:43 +00001826 pool, rem_sdp, sip_err_code);
Benny Prijono224b4e22008-06-19 14:10:28 +00001827 if (status != PJ_SUCCESS)
1828 return status;
1829 }
Nanang Izzuddinb6c239c2011-05-10 05:42:28 +00001830#endif
Benny Prijono224b4e22008-06-19 14:10:28 +00001831
Nanang Izzuddin1e952a82010-10-05 16:32:04 +00001832 /* Get SDP negotiator state */
1833 if (call->inv && call->inv->neg)
1834 sdp_neg_state = pjmedia_sdp_neg_get_state(call->inv->neg);
1835
Benny Prijono0bc99a92011-03-17 04:34:43 +00001836 /* Get one address to use in the origin field */
1837 pj_bzero(&origin, sizeof(origin));
1838 for (mi=0; mi<call->med_cnt; ++mi) {
1839 pjmedia_transport_info tpinfo;
Benny Prijono617c5bc2007-04-02 19:51:21 +00001840
Benny Prijono0bc99a92011-03-17 04:34:43 +00001841 if (call->media[mi].tp == NULL)
1842 continue;
1843
1844 pjmedia_transport_info_init(&tpinfo);
1845 pjmedia_transport_get_info(call->media[mi].tp, &tpinfo);
1846 pj_sockaddr_cp(&origin, &tpinfo.sock_info.rtp_addr_name);
1847 break;
Benny Prijono25b2ea12008-01-24 19:20:54 +00001848 }
Benny Prijonoc97608e2007-03-23 16:34:20 +00001849
Benny Prijono0bc99a92011-03-17 04:34:43 +00001850 /* Create the base (blank) SDP */
1851 status = pjmedia_endpt_create_base_sdp(pjsua_var.med_endpt, pool, NULL,
1852 &origin, &sdp);
1853 if (status != PJ_SUCCESS)
1854 return status;
Benny Prijonoa310bd22008-06-27 21:19:44 +00001855
Benny Prijono0bc99a92011-03-17 04:34:43 +00001856 /* Process each media line */
1857 for (mi=0; mi<call->med_cnt; ++mi) {
1858 pjsua_call_media *call_med = &call->media[mi];
1859 pjmedia_sdp_media *m = NULL;
1860 pjmedia_transport_info tpinfo;
1861
Nanang Izzuddinb6c239c2011-05-10 05:42:28 +00001862 if (rem_sdp && mi >= rem_sdp->media_count) {
1863 /* Remote might have removed some media lines. */
1864 break;
Nanang Izzuddin1e952a82010-10-05 16:32:04 +00001865 }
1866
Nanang Izzuddinb6c239c2011-05-10 05:42:28 +00001867 if (call_med->tp == NULL || call_med->tp_st == PJSUA_MED_TP_DISABLED)
1868 {
Benny Prijono0bc99a92011-03-17 04:34:43 +00001869 /*
Nanang Izzuddinb6c239c2011-05-10 05:42:28 +00001870 * This media is disabled. Just create a valid SDP with zero
Benny Prijono0bc99a92011-03-17 04:34:43 +00001871 * port.
1872 */
Nanang Izzuddind8c33d82011-08-18 18:30:55 +00001873 if (rem_sdp) {
1874 /* Just clone the remote media and deactivate it */
1875 m = pjmedia_sdp_media_clone_deactivate(pool,
1876 rem_sdp->media[mi]);
1877 } else {
1878 m = PJ_POOL_ZALLOC_T(pool, pjmedia_sdp_media);
1879 m->desc.transport = pj_str("RTP/AVP");
1880 m->desc.fmt_count = 1;
1881 m->conn = PJ_POOL_ZALLOC_T(pool, pjmedia_sdp_conn);
1882 m->conn->net_type = pj_str("IN");
1883 m->conn->addr_type = pj_str("IP4");
1884 m->conn->addr = pj_str("127.0.0.1");
Benny Prijonoa310bd22008-06-27 21:19:44 +00001885
Nanang Izzuddind8c33d82011-08-18 18:30:55 +00001886 switch (call_med->type) {
1887 case PJMEDIA_TYPE_AUDIO:
1888 m->desc.media = pj_str("audio");
1889 m->desc.fmt[0] = pj_str("0");
1890 break;
1891 case PJMEDIA_TYPE_VIDEO:
1892 m->desc.media = pj_str("video");
1893 m->desc.fmt[0] = pj_str("31");
1894 break;
1895 default:
1896 if (rem_sdp) {
1897 pj_strdup(pool, &m->desc.media,
1898 &rem_sdp->media[mi]->desc.media);
1899 pj_strdup(pool, &m->desc.fmt[0],
1900 &rem_sdp->media[mi]->desc.fmt[0]);
1901 } else {
1902 pj_assert(!"Invalid call_med media type");
1903 return PJ_EBUG;
1904 }
Benny Prijono0bc99a92011-03-17 04:34:43 +00001905 }
Benny Prijonoa310bd22008-06-27 21:19:44 +00001906 }
Benny Prijono0bc99a92011-03-17 04:34:43 +00001907
1908 sdp->media[sdp->media_count++] = m;
1909 continue;
Benny Prijonoa310bd22008-06-27 21:19:44 +00001910 }
1911
Benny Prijono0bc99a92011-03-17 04:34:43 +00001912 /* Get transport address info */
1913 pjmedia_transport_info_init(&tpinfo);
1914 pjmedia_transport_get_info(call_med->tp, &tpinfo);
Benny Prijonoa310bd22008-06-27 21:19:44 +00001915
Benny Prijono0bc99a92011-03-17 04:34:43 +00001916 /* Ask pjmedia endpoint to create SDP media line */
1917 switch (call_med->type) {
1918 case PJMEDIA_TYPE_AUDIO:
1919 status = pjmedia_endpt_create_audio_sdp(pjsua_var.med_endpt, pool,
1920 &tpinfo.sock_info, 0, &m);
1921 break;
Nanang Izzuddin63b3c132011-07-19 11:11:07 +00001922#if defined(PJMEDIA_HAS_VIDEO) && (PJMEDIA_HAS_VIDEO != 0)
Benny Prijono0bc99a92011-03-17 04:34:43 +00001923 case PJMEDIA_TYPE_VIDEO:
1924 status = pjmedia_endpt_create_video_sdp(pjsua_var.med_endpt, pool,
1925 &tpinfo.sock_info, 0, &m);
1926 break;
Nanang Izzuddin63b3c132011-07-19 11:11:07 +00001927#endif
Benny Prijono0bc99a92011-03-17 04:34:43 +00001928 default:
1929 pj_assert(!"Invalid call_med media type");
1930 return PJ_EBUG;
1931 }
Benny Prijonoa310bd22008-06-27 21:19:44 +00001932
Benny Prijono0bc99a92011-03-17 04:34:43 +00001933 if (status != PJ_SUCCESS)
1934 return status;
1935
1936 sdp->media[sdp->media_count++] = m;
1937
1938 /* Give to transport */
1939 status = pjmedia_transport_encode_sdp(call_med->tp, pool,
1940 sdp, rem_sdp, mi);
1941 if (status != PJ_SUCCESS) {
1942 if (sip_err_code) *sip_err_code = PJSIP_SC_NOT_ACCEPTABLE;
1943 return status;
Benny Prijonoa310bd22008-06-27 21:19:44 +00001944 }
Nanang Izzuddin91ba2a22011-04-11 19:59:09 +00001945
1946 /* Copy c= line of the first media to session level,
1947 * if there's none.
1948 */
1949 if (sdp->conn == NULL) {
1950 sdp->conn = pjmedia_sdp_conn_clone(pool, m->conn);
Benny Prijonoa310bd22008-06-27 21:19:44 +00001951 }
1952 }
1953
Benny Prijono6ba8c542007-10-16 01:34:14 +00001954 /* Add NAT info in the SDP */
1955 if (pjsua_var.ua_cfg.nat_type_in_sdp) {
1956 pjmedia_sdp_attr *a;
1957 pj_str_t value;
1958 char nat_info[80];
1959
1960 value.ptr = nat_info;
1961 if (pjsua_var.ua_cfg.nat_type_in_sdp == 1) {
1962 value.slen = pj_ansi_snprintf(nat_info, sizeof(nat_info),
1963 "%d", pjsua_var.nat_type);
1964 } else {
1965 const char *type_name = pj_stun_get_nat_name(pjsua_var.nat_type);
1966 value.slen = pj_ansi_snprintf(nat_info, sizeof(nat_info),
1967 "%d %s",
1968 pjsua_var.nat_type,
1969 type_name);
1970 }
1971
1972 a = pjmedia_sdp_attr_create(pool, "X-nat", &value);
1973
1974 pjmedia_sdp_attr_add(&sdp->attr_count, sdp->attr, a);
1975
1976 }
1977
Benny Prijonoc97608e2007-03-23 16:34:20 +00001978
Benny Prijono0bc99a92011-03-17 04:34:43 +00001979#if DISABLED_FOR_TICKET_1185 && defined(PJMEDIA_HAS_SRTP) && (PJMEDIA_HAS_SRTP != 0)
Nanang Izzuddind89cc3a2010-05-13 05:22:51 +00001980 /* Check if SRTP is in optional mode and configured to use duplicated
1981 * media, i.e: secured and unsecured version, in the SDP offer.
1982 */
1983 if (!rem_sdp &&
1984 pjsua_var.acc[call->acc_id].cfg.use_srtp == PJMEDIA_SRTP_OPTIONAL &&
1985 pjsua_var.acc[call->acc_id].cfg.srtp_optional_dup_offer)
1986 {
1987 unsigned i;
1988
1989 for (i = 0; i < sdp->media_count; ++i) {
1990 pjmedia_sdp_media *m = sdp->media[i];
1991
Benny Prijono0bc99a92011-03-17 04:34:43 +00001992 /* Check if this media is unsecured but has SDP "crypto"
Nanang Izzuddind89cc3a2010-05-13 05:22:51 +00001993 * attribute.
1994 */
1995 if (pj_stricmp2(&m->desc.transport, "RTP/AVP") == 0 &&
1996 pjmedia_sdp_media_find_attr2(m, "crypto", NULL) != NULL)
1997 {
Benny Prijono0bc99a92011-03-17 04:34:43 +00001998 if (i == (unsigned)call->audio_idx &&
Nanang Izzuddin1e952a82010-10-05 16:32:04 +00001999 sdp_neg_state == PJMEDIA_SDP_NEG_STATE_DONE)
2000 {
2001 /* This is a session update, and peer has chosen the
2002 * unsecured version, so let's make this unsecured too.
2003 */
2004 pjmedia_sdp_media_remove_all_attr(m, "crypto");
2005 } else {
2006 /* This is new offer, duplicate media so we'll have
2007 * secured (with "RTP/SAVP" transport) and and unsecured
2008 * versions.
2009 */
2010 pjmedia_sdp_media *new_m;
Nanang Izzuddind89cc3a2010-05-13 05:22:51 +00002011
Nanang Izzuddin1e952a82010-10-05 16:32:04 +00002012 /* Duplicate this media and apply secured transport */
2013 new_m = pjmedia_sdp_media_clone(pool, m);
2014 pj_strdup2(pool, &new_m->desc.transport, "RTP/SAVP");
Nanang Izzuddind89cc3a2010-05-13 05:22:51 +00002015
Nanang Izzuddin1e952a82010-10-05 16:32:04 +00002016 /* Remove the "crypto" attribute in the unsecured media */
2017 pjmedia_sdp_media_remove_all_attr(m, "crypto");
Nanang Izzuddind89cc3a2010-05-13 05:22:51 +00002018
Nanang Izzuddin1e952a82010-10-05 16:32:04 +00002019 /* Insert the new media before the unsecured media */
2020 if (sdp->media_count < PJMEDIA_MAX_SDP_MEDIA) {
Benny Prijono0bc99a92011-03-17 04:34:43 +00002021 pj_array_insert(sdp->media, sizeof(new_m),
Nanang Izzuddin1e952a82010-10-05 16:32:04 +00002022 sdp->media_count, i, &new_m);
2023 ++sdp->media_count;
2024 ++i;
2025 }
Nanang Izzuddind89cc3a2010-05-13 05:22:51 +00002026 }
2027 }
2028 }
2029 }
2030#endif
2031
Benny Prijonoc97608e2007-03-23 16:34:20 +00002032 *p_sdp = sdp;
2033 return PJ_SUCCESS;
Benny Prijonoc97608e2007-03-23 16:34:20 +00002034}
2035
2036
2037static void stop_media_session(pjsua_call_id call_id)
2038{
2039 pjsua_call *call = &pjsua_var.calls[call_id];
Benny Prijono0bc99a92011-03-17 04:34:43 +00002040 unsigned mi;
Benny Prijonoc97608e2007-03-23 16:34:20 +00002041
Benny Prijonob90fd382011-09-18 14:59:56 +00002042 pj_log_push_indent();
2043
Benny Prijono0bc99a92011-03-17 04:34:43 +00002044 for (mi=0; mi<call->med_cnt; ++mi) {
2045 pjsua_call_media *call_med = &call->media[mi];
2046
2047 if (call_med->type == PJMEDIA_TYPE_AUDIO) {
2048 pjmedia_stream *strm = call_med->strm.a.stream;
2049 pjmedia_rtcp_stat stat;
2050
2051 if (strm) {
2052 if (call_med->strm.a.conf_slot != PJSUA_INVALID_ID) {
2053 if (pjsua_var.mconf) {
2054 pjsua_conf_remove_port(call_med->strm.a.conf_slot);
2055 }
2056 call_med->strm.a.conf_slot = PJSUA_INVALID_ID;
2057 }
2058
2059 if ((call_med->dir & PJMEDIA_DIR_ENCODING) &&
2060 (pjmedia_stream_get_stat(strm, &stat) == PJ_SUCCESS))
2061 {
2062 /* Save RTP timestamp & sequence, so when media session is
2063 * restarted, those values will be restored as the initial
2064 * RTP timestamp & sequence of the new media session. So in
2065 * the same call session, RTP timestamp and sequence are
2066 * guaranteed to be contigue.
2067 */
2068 call_med->rtp_tx_seq_ts_set = 1 | (1 << 1);
2069 call_med->rtp_tx_seq = stat.rtp_tx_last_seq;
2070 call_med->rtp_tx_ts = stat.rtp_tx_last_ts;
2071 }
2072
2073 if (pjsua_var.ua_cfg.cb.on_stream_destroyed) {
2074 pjsua_var.ua_cfg.cb.on_stream_destroyed(call_id, strm, mi);
2075 }
2076
2077 pjmedia_stream_destroy(strm);
2078 call_med->strm.a.stream = NULL;
Nanang Izzuddinbf26db12011-03-18 07:54:50 +00002079 }
Nanang Izzuddinfd461eb2008-06-09 09:35:59 +00002080 }
Nanang Izzuddin50fae732011-03-22 09:49:23 +00002081
2082#if PJMEDIA_HAS_VIDEO
2083 else if (call_med->type == PJMEDIA_TYPE_VIDEO) {
Nanang Izzuddin62053a62011-07-12 11:08:32 +00002084 stop_video_stream(call_med);
Benny Prijonoc97608e2007-03-23 16:34:20 +00002085 }
Nanang Izzuddin50fae732011-03-22 09:49:23 +00002086#endif
Nanang Izzuddinbf26db12011-03-18 07:54:50 +00002087
2088 PJ_LOG(4,(THIS_FILE, "Media session call%02d:%d is destroyed",
2089 call_id, mi));
Benny Prijono0bc99a92011-03-17 04:34:43 +00002090 call_med->state = PJSUA_CALL_MEDIA_NONE;
Benny Prijonoc97608e2007-03-23 16:34:20 +00002091 }
Benny Prijonob90fd382011-09-18 14:59:56 +00002092
2093 pj_log_pop_indent();
Benny Prijonoc97608e2007-03-23 16:34:20 +00002094}
2095
2096pj_status_t pjsua_media_channel_deinit(pjsua_call_id call_id)
2097{
2098 pjsua_call *call = &pjsua_var.calls[call_id];
Benny Prijono0bc99a92011-03-17 04:34:43 +00002099 unsigned mi;
Benny Prijonoc97608e2007-03-23 16:34:20 +00002100
Benny Prijonob90fd382011-09-18 14:59:56 +00002101 PJ_LOG(4,(THIS_FILE, "Call %d: deinitializing media..", call_id));
2102 pj_log_push_indent();
2103
Benny Prijonoc97608e2007-03-23 16:34:20 +00002104 stop_media_session(call_id);
2105
Benny Prijono0bc99a92011-03-17 04:34:43 +00002106 for (mi=0; mi<call->med_cnt; ++mi) {
2107 pjsua_call_media *call_med = &call->media[mi];
Benny Prijonoc97608e2007-03-23 16:34:20 +00002108
Sauw Ming73ecfe82011-09-21 10:20:01 +00002109 if (call_med->tp_st > PJSUA_MED_TP_IDLE) {
Benny Prijono0bc99a92011-03-17 04:34:43 +00002110 pjmedia_transport_media_stop(call_med->tp);
Sauw Ming73ecfe82011-09-21 10:20:01 +00002111 set_media_tp_state(call_med, PJSUA_MED_TP_IDLE);
Benny Prijono0bc99a92011-03-17 04:34:43 +00002112 }
2113
Nanang Izzuddinb6c239c2011-05-10 05:42:28 +00002114 //if (call_med->tp_orig && call_med->tp &&
2115 // call_med->tp != call_med->tp_orig)
2116 //{
2117 // pjmedia_transport_close(call_med->tp);
2118 // call_med->tp = call_med->tp_orig;
2119 //}
2120 if (call_med->tp) {
Benny Prijono0bc99a92011-03-17 04:34:43 +00002121 pjmedia_transport_close(call_med->tp);
Nanang Izzuddinb6c239c2011-05-10 05:42:28 +00002122 call_med->tp = call_med->tp_orig = NULL;
Benny Prijono0bc99a92011-03-17 04:34:43 +00002123 }
Benny Prijonod8179652008-01-23 20:39:07 +00002124 }
Nanang Izzuddin670f71b2009-01-20 14:05:54 +00002125
2126 check_snd_dev_idle();
Benny Prijonob90fd382011-09-18 14:59:56 +00002127 pj_log_pop_indent();
Nanang Izzuddin670f71b2009-01-20 14:05:54 +00002128
Benny Prijonoc97608e2007-03-23 16:34:20 +00002129 return PJ_SUCCESS;
2130}
2131
2132
2133/*
2134 * DTMF callback from the stream.
2135 */
2136static void dtmf_callback(pjmedia_stream *strm, void *user_data,
2137 int digit)
2138{
2139 PJ_UNUSED_ARG(strm);
2140
Benny Prijonob90fd382011-09-18 14:59:56 +00002141 pj_log_push_indent();
2142
Benny Prijono0c068262008-02-14 14:38:52 +00002143 /* For discussions about call mutex protection related to this
2144 * callback, please see ticket #460:
2145 * http://trac.pjsip.org/repos/ticket/460#comment:4
2146 */
Benny Prijonoc97608e2007-03-23 16:34:20 +00002147 if (pjsua_var.ua_cfg.cb.on_dtmf_digit) {
2148 pjsua_call_id call_id;
2149
Benny Prijonod8179652008-01-23 20:39:07 +00002150 call_id = (pjsua_call_id)(long)user_data;
Benny Prijonoc97608e2007-03-23 16:34:20 +00002151 pjsua_var.ua_cfg.cb.on_dtmf_digit(call_id, digit);
2152 }
Benny Prijonob90fd382011-09-18 14:59:56 +00002153
2154 pj_log_pop_indent();
Benny Prijonoc97608e2007-03-23 16:34:20 +00002155}
2156
2157
Benny Prijono0bc99a92011-03-17 04:34:43 +00002158static pj_status_t audio_channel_update(pjsua_call_media *call_med,
2159 pj_pool_t *tmp_pool,
2160 const pjmedia_sdp_session *local_sdp,
2161 const pjmedia_sdp_session *remote_sdp)
Benny Prijonoc97608e2007-03-23 16:34:20 +00002162{
Benny Prijono0bc99a92011-03-17 04:34:43 +00002163 pjsua_call *call = call_med->call;
2164 pjmedia_stream_info the_si, *si = &the_si;
Benny Prijonoc97608e2007-03-23 16:34:20 +00002165 pjmedia_port *media_port;
Benny Prijono0bc99a92011-03-17 04:34:43 +00002166 unsigned strm_idx = call_med->idx;
Benny Prijonoc97608e2007-03-23 16:34:20 +00002167 pj_status_t status;
Benny Prijonob90fd382011-09-18 14:59:56 +00002168
2169 PJ_LOG(4,(THIS_FILE,"Audio channel update.."));
2170 pj_log_push_indent();
Benny Prijono0bc99a92011-03-17 04:34:43 +00002171
2172 status = pjmedia_stream_info_from_sdp(si, tmp_pool, pjsua_var.med_endpt,
2173 local_sdp, remote_sdp, strm_idx);
Benny Prijonoc97608e2007-03-23 16:34:20 +00002174 if (status != PJ_SUCCESS)
Benny Prijonob90fd382011-09-18 14:59:56 +00002175 goto on_return;
Benny Prijonoc97608e2007-03-23 16:34:20 +00002176
Nanang Izzuddin99d69522008-08-04 15:01:38 +00002177 /* Check if no media is active */
Benny Prijono0bc99a92011-03-17 04:34:43 +00002178 if (si->dir == PJMEDIA_DIR_NONE) {
Nanang Izzuddin99d69522008-08-04 15:01:38 +00002179 /* Call media state */
Benny Prijono0bc99a92011-03-17 04:34:43 +00002180 call_med->state = PJSUA_CALL_MEDIA_NONE;
Benny Prijonoc97608e2007-03-23 16:34:20 +00002181
Nanang Izzuddin99d69522008-08-04 15:01:38 +00002182 /* Call media direction */
Benny Prijono0bc99a92011-03-17 04:34:43 +00002183 call_med->dir = PJMEDIA_DIR_NONE;
Benny Prijono68f9e4f2008-03-21 08:56:02 +00002184
Benny Prijonoc97608e2007-03-23 16:34:20 +00002185 } else {
Nanang Izzuddin4375f902008-06-26 19:12:09 +00002186 pjmedia_transport_info tp_info;
2187
Benny Prijono224b4e22008-06-19 14:10:28 +00002188 /* Start/restart media transport */
Benny Prijono0bc99a92011-03-17 04:34:43 +00002189 status = pjmedia_transport_media_start(call_med->tp,
2190 tmp_pool, local_sdp,
2191 remote_sdp, strm_idx);
Benny Prijonod8179652008-01-23 20:39:07 +00002192 if (status != PJ_SUCCESS)
Benny Prijonob90fd382011-09-18 14:59:56 +00002193 goto on_return;
Benny Prijonoc97608e2007-03-23 16:34:20 +00002194
Sauw Ming73ecfe82011-09-21 10:20:01 +00002195 set_media_tp_state(call_med, PJSUA_MED_TP_RUNNING);
Benny Prijono224b4e22008-06-19 14:10:28 +00002196
Nanang Izzuddin4375f902008-06-26 19:12:09 +00002197 /* Get remote SRTP usage policy */
2198 pjmedia_transport_info_init(&tp_info);
Benny Prijono0bc99a92011-03-17 04:34:43 +00002199 pjmedia_transport_get_info(call_med->tp, &tp_info);
Nanang Izzuddin4375f902008-06-26 19:12:09 +00002200 if (tp_info.specific_info_cnt > 0) {
Benny Prijonof5d9f1f2009-10-14 13:13:18 +00002201 unsigned i;
Nanang Izzuddin4375f902008-06-26 19:12:09 +00002202 for (i = 0; i < tp_info.specific_info_cnt; ++i) {
2203 if (tp_info.spc_info[i].type == PJMEDIA_TRANSPORT_TYPE_SRTP)
2204 {
2205 pjmedia_srtp_info *srtp_info =
2206 (pjmedia_srtp_info*) tp_info.spc_info[i].buffer;
2207
Benny Prijono0bc99a92011-03-17 04:34:43 +00002208 call_med->rem_srtp_use = srtp_info->peer_use;
Nanang Izzuddin4375f902008-06-26 19:12:09 +00002209 break;
2210 }
2211 }
2212 }
2213
Benny Prijonoc97608e2007-03-23 16:34:20 +00002214 /* Override ptime, if this option is specified. */
2215 if (pjsua_var.media_cfg.ptime != 0) {
Benny Prijono91e567e2007-12-28 08:51:58 +00002216 si->param->setting.frm_per_pkt = (pj_uint8_t)
2217 (pjsua_var.media_cfg.ptime / si->param->info.frm_ptime);
2218 if (si->param->setting.frm_per_pkt == 0)
2219 si->param->setting.frm_per_pkt = 1;
Benny Prijonoc97608e2007-03-23 16:34:20 +00002220 }
2221
2222 /* Disable VAD, if this option is specified. */
2223 if (pjsua_var.media_cfg.no_vad) {
Benny Prijono91e567e2007-12-28 08:51:58 +00002224 si->param->setting.vad = 0;
Benny Prijonoc97608e2007-03-23 16:34:20 +00002225 }
2226
2227
2228 /* Optionally, application may modify other stream settings here
2229 * (such as jitter buffer parameters, codec ptime, etc.)
2230 */
Benny Prijono91e567e2007-12-28 08:51:58 +00002231 si->jb_init = pjsua_var.media_cfg.jb_init;
2232 si->jb_min_pre = pjsua_var.media_cfg.jb_min_pre;
2233 si->jb_max_pre = pjsua_var.media_cfg.jb_max_pre;
2234 si->jb_max = pjsua_var.media_cfg.jb_max;
Benny Prijonoc97608e2007-03-23 16:34:20 +00002235
Benny Prijono8147f402007-11-21 14:50:07 +00002236 /* Set SSRC */
Benny Prijono0bc99a92011-03-17 04:34:43 +00002237 si->ssrc = call_med->ssrc;
Benny Prijono8147f402007-11-21 14:50:07 +00002238
Nanang Izzuddina815ceb2008-08-26 16:51:28 +00002239 /* Set RTP timestamp & sequence, normally these value are intialized
2240 * automatically when stream session created, but for some cases (e.g:
2241 * call reinvite, call update) timestamp and sequence need to be kept
2242 * contigue.
2243 */
Benny Prijono0bc99a92011-03-17 04:34:43 +00002244 si->rtp_ts = call_med->rtp_tx_ts;
2245 si->rtp_seq = call_med->rtp_tx_seq;
2246 si->rtp_seq_ts_set = call_med->rtp_tx_seq_ts_set;
Nanang Izzuddina815ceb2008-08-26 16:51:28 +00002247
Nanang Izzuddin5e39a2b2010-09-20 06:13:02 +00002248#if defined(PJMEDIA_STREAM_ENABLE_KA) && PJMEDIA_STREAM_ENABLE_KA!=0
2249 /* Enable/disable stream keep-alive and NAT hole punch. */
2250 si->use_ka = pjsua_var.acc[call->acc_id].cfg.use_stream_ka;
2251#endif
2252
Benny Prijonoc97608e2007-03-23 16:34:20 +00002253 /* Create session based on session info. */
Benny Prijono0bc99a92011-03-17 04:34:43 +00002254 status = pjmedia_stream_create(pjsua_var.med_endpt, NULL, si,
2255 call_med->tp, NULL,
2256 &call_med->strm.a.stream);
2257 if (status != PJ_SUCCESS) {
Benny Prijonob90fd382011-09-18 14:59:56 +00002258 goto on_return;
Benny Prijono0bc99a92011-03-17 04:34:43 +00002259 }
2260
2261 /* Start stream */
2262 status = pjmedia_stream_start(call_med->strm.a.stream);
Benny Prijonoc97608e2007-03-23 16:34:20 +00002263 if (status != PJ_SUCCESS) {
Benny Prijonob90fd382011-09-18 14:59:56 +00002264 goto on_return;
Benny Prijonoc97608e2007-03-23 16:34:20 +00002265 }
2266
2267 /* If DTMF callback is installed by application, install our
2268 * callback to the session.
2269 */
2270 if (pjsua_var.ua_cfg.cb.on_dtmf_digit) {
Benny Prijono0bc99a92011-03-17 04:34:43 +00002271 pjmedia_stream_set_dtmf_callback(call_med->strm.a.stream,
2272 &dtmf_callback,
2273 (void*)(long)(call->index));
Benny Prijonoc97608e2007-03-23 16:34:20 +00002274 }
2275
2276 /* Get the port interface of the first stream in the session.
2277 * We need the port interface to add to the conference bridge.
2278 */
Benny Prijono0bc99a92011-03-17 04:34:43 +00002279 pjmedia_stream_get_port(call_med->strm.a.stream, &media_port);
Benny Prijonoc97608e2007-03-23 16:34:20 +00002280
Benny Prijonofc13bf62008-02-20 08:56:15 +00002281 /* Notify application about stream creation.
2282 * Note: application may modify media_port to point to different
2283 * media port
2284 */
2285 if (pjsua_var.ua_cfg.cb.on_stream_created) {
Benny Prijono0bc99a92011-03-17 04:34:43 +00002286 pjsua_var.ua_cfg.cb.on_stream_created(call->index,
2287 call_med->strm.a.stream,
2288 strm_idx, &media_port);
Benny Prijonofc13bf62008-02-20 08:56:15 +00002289 }
Benny Prijonoc97608e2007-03-23 16:34:20 +00002290
2291 /*
2292 * Add the call to conference bridge.
2293 */
Benny Prijono38fb3ea2008-01-02 08:27:03 +00002294 {
2295 char tmp[PJSIP_MAX_URL_SIZE];
2296 pj_str_t port_name;
2297
2298 port_name.ptr = tmp;
2299 port_name.slen = pjsip_uri_print(PJSIP_URI_IN_REQ_URI,
2300 call->inv->dlg->remote.info->uri,
2301 tmp, sizeof(tmp));
2302 if (port_name.slen < 1) {
2303 port_name = pj_str("call");
2304 }
Benny Prijono40d62b62009-08-12 17:53:47 +00002305 status = pjmedia_conf_add_port( pjsua_var.mconf,
2306 call->inv->pool_prov,
Benny Prijono38fb3ea2008-01-02 08:27:03 +00002307 media_port,
2308 &port_name,
Benny Prijono0bc99a92011-03-17 04:34:43 +00002309 (unsigned*)
2310 &call_med->strm.a.conf_slot);
Benny Prijono38fb3ea2008-01-02 08:27:03 +00002311 if (status != PJ_SUCCESS) {
Benny Prijonob90fd382011-09-18 14:59:56 +00002312 goto on_return;
Benny Prijono38fb3ea2008-01-02 08:27:03 +00002313 }
Benny Prijonoc97608e2007-03-23 16:34:20 +00002314 }
2315
Nanang Izzuddin99d69522008-08-04 15:01:38 +00002316 /* Call media direction */
Benny Prijono0bc99a92011-03-17 04:34:43 +00002317 call_med->dir = si->dir;
Nanang Izzuddin99d69522008-08-04 15:01:38 +00002318
2319 /* Call media state */
2320 if (call->local_hold)
Benny Prijono0bc99a92011-03-17 04:34:43 +00002321 call_med->state = PJSUA_CALL_MEDIA_LOCAL_HOLD;
2322 else if (call_med->dir == PJMEDIA_DIR_DECODING)
2323 call_med->state = PJSUA_CALL_MEDIA_REMOTE_HOLD;
Nanang Izzuddin99d69522008-08-04 15:01:38 +00002324 else
Benny Prijono0bc99a92011-03-17 04:34:43 +00002325 call_med->state = PJSUA_CALL_MEDIA_ACTIVE;
Benny Prijonoc97608e2007-03-23 16:34:20 +00002326 }
2327
2328 /* Print info. */
2329 {
2330 char info[80];
2331 int info_len = 0;
Benny Prijono0bc99a92011-03-17 04:34:43 +00002332 int len;
2333 const char *dir;
Benny Prijonoc97608e2007-03-23 16:34:20 +00002334
Benny Prijono0bc99a92011-03-17 04:34:43 +00002335 switch (si->dir) {
2336 case PJMEDIA_DIR_NONE:
2337 dir = "inactive";
2338 break;
2339 case PJMEDIA_DIR_ENCODING:
2340 dir = "sendonly";
2341 break;
2342 case PJMEDIA_DIR_DECODING:
2343 dir = "recvonly";
2344 break;
2345 case PJMEDIA_DIR_ENCODING_DECODING:
2346 dir = "sendrecv";
2347 break;
2348 default:
2349 dir = "unknown";
2350 break;
Benny Prijonoc97608e2007-03-23 16:34:20 +00002351 }
Benny Prijono0bc99a92011-03-17 04:34:43 +00002352 len = pj_ansi_sprintf( info+info_len,
2353 ", stream #%d: %.*s (%s)", strm_idx,
2354 (int)si->fmt.encoding_name.slen,
2355 si->fmt.encoding_name.ptr,
2356 dir);
2357 if (len > 0)
2358 info_len += len;
Benny Prijonob90fd382011-09-18 14:59:56 +00002359 PJ_LOG(4,(THIS_FILE,"Audio updated%s", info));
Benny Prijonoc97608e2007-03-23 16:34:20 +00002360 }
2361
Benny Prijonob90fd382011-09-18 14:59:56 +00002362on_return:
2363 pj_log_pop_indent();
2364 return status;
Benny Prijonoc97608e2007-03-23 16:34:20 +00002365}
2366
Benny Prijono0bc99a92011-03-17 04:34:43 +00002367pj_status_t pjsua_media_channel_update(pjsua_call_id call_id,
2368 const pjmedia_sdp_session *local_sdp,
2369 const pjmedia_sdp_session *remote_sdp)
2370{
2371 pjsua_call *call = &pjsua_var.calls[call_id];
Nanang Izzuddind8c33d82011-08-18 18:30:55 +00002372 pjsua_acc *acc = &pjsua_var.acc[call->acc_id];
Benny Prijono0bc99a92011-03-17 04:34:43 +00002373 pj_pool_t *tmp_pool = call->inv->pool_prov;
2374 unsigned mi;
Nanang Izzuddinb6c239c2011-05-10 05:42:28 +00002375 pj_bool_t got_media = PJ_FALSE;
Nanang Izzuddinbf26db12011-03-18 07:54:50 +00002376 pj_status_t status = PJ_SUCCESS;
Benny Prijono0bc99a92011-03-17 04:34:43 +00002377
Nanang Izzuddind8c33d82011-08-18 18:30:55 +00002378 const pj_str_t STR_AUDIO = { "audio", 5 };
2379 const pj_str_t STR_VIDEO = { "video", 5 };
2380 pj_uint8_t maudidx[PJSUA_MAX_CALL_MEDIA];
2381 unsigned maudcnt = PJ_ARRAY_SIZE(maudidx);
2382 pj_uint8_t mvididx[PJSUA_MAX_CALL_MEDIA];
2383 unsigned mvidcnt = PJ_ARRAY_SIZE(mvididx);
2384 pj_bool_t need_renego_sdp = PJ_FALSE;
2385
Benny Prijono0bc99a92011-03-17 04:34:43 +00002386 if (pjsua_get_state() != PJSUA_STATE_RUNNING)
2387 return PJ_EBUSY;
2388
Benny Prijonob90fd382011-09-18 14:59:56 +00002389 PJ_LOG(4,(THIS_FILE, "Call %d: updating media..", call_id));
2390 pj_log_push_indent();
2391
Benny Prijono0bc99a92011-03-17 04:34:43 +00002392 /* Destroy existing media session, if any. */
2393 stop_media_session(call->index);
2394
Nanang Izzuddind8c33d82011-08-18 18:30:55 +00002395 /* Call media count must be at least equal to SDP media. Note that
2396 * it may not be equal when remote removed any SDP media line.
2397 */
2398 pj_assert(call->med_cnt >= local_sdp->media_count);
2399
Benny Prijono0bc99a92011-03-17 04:34:43 +00002400 /* Reset audio_idx first */
2401 call->audio_idx = -1;
2402
Nanang Izzuddind8c33d82011-08-18 18:30:55 +00002403 /* Apply maximum audio/video count of the account */
2404 sort_media(local_sdp, &STR_AUDIO, acc->cfg.use_srtp,
2405 maudidx, &maudcnt);
2406#if PJMEDIA_HAS_VIDEO
2407 sort_media(local_sdp, &STR_VIDEO, acc->cfg.use_srtp,
2408 mvididx, &mvidcnt);
2409#else
2410 PJ_UNUSED_ARG(STR_VIDEO);
2411 mvidcnt = 0;
2412#endif
2413 if (maudcnt > acc->cfg.max_audio_cnt || mvidcnt > acc->cfg.max_video_cnt)
2414 {
2415 pjmedia_sdp_session *local_sdp2;
2416
2417 maudcnt = PJ_MIN(maudcnt, acc->cfg.max_audio_cnt);
2418 mvidcnt = PJ_MIN(mvidcnt, acc->cfg.max_video_cnt);
2419 local_sdp2 = pjmedia_sdp_session_clone(tmp_pool, local_sdp);
2420
2421 for (mi=0; mi < local_sdp2->media_count; ++mi) {
2422 pjmedia_sdp_media *m = local_sdp2->media[mi];
2423
2424 if (m->desc.port == 0 ||
2425 pj_memchr(maudidx, mi, maudcnt*sizeof(maudidx[0])) ||
2426 pj_memchr(mvididx, mi, mvidcnt*sizeof(mvididx[0])))
2427 {
2428 continue;
2429 }
2430
2431 /* Deactivate this media */
2432 pjmedia_sdp_media_deactivate(tmp_pool, m);
2433 }
2434
2435 local_sdp = local_sdp2;
2436 need_renego_sdp = PJ_TRUE;
2437 }
2438
Benny Prijono0bc99a92011-03-17 04:34:43 +00002439 /* Process each media stream */
2440 for (mi=0; mi < call->med_cnt; ++mi) {
2441 pjsua_call_media *call_med = &call->media[mi];
2442
Nanang Izzuddinb6c239c2011-05-10 05:42:28 +00002443 if (mi >= local_sdp->media_count ||
2444 mi >= remote_sdp->media_count)
Benny Prijono0bc99a92011-03-17 04:34:43 +00002445 {
Nanang Izzuddinb6c239c2011-05-10 05:42:28 +00002446 /* This may happen when remote removed any SDP media lines in
2447 * its re-offer.
2448 */
2449 continue;
2450#if 0
Benny Prijono0bc99a92011-03-17 04:34:43 +00002451 /* Something is wrong */
2452 PJ_LOG(1,(THIS_FILE, "Error updating media for call %d: "
2453 "invalid media index %d in SDP", call_id, mi));
Benny Prijonob90fd382011-09-18 14:59:56 +00002454 status = PJMEDIA_SDP_EINSDP;
2455 goto on_error;
Nanang Izzuddinb6c239c2011-05-10 05:42:28 +00002456#endif
Benny Prijono0bc99a92011-03-17 04:34:43 +00002457 }
2458
2459 switch (call_med->type) {
2460 case PJMEDIA_TYPE_AUDIO:
2461 status = audio_channel_update(call_med, tmp_pool,
2462 local_sdp, remote_sdp);
2463 if (call->audio_idx==-1 && status==PJ_SUCCESS &&
Nanang Izzuddinbf26db12011-03-18 07:54:50 +00002464 call_med->strm.a.stream)
Benny Prijono0bc99a92011-03-17 04:34:43 +00002465 {
2466 call->audio_idx = mi;
2467 }
2468 break;
Nanang Izzuddin63b3c132011-07-19 11:11:07 +00002469#if defined(PJMEDIA_HAS_VIDEO) && (PJMEDIA_HAS_VIDEO != 0)
Benny Prijono0bc99a92011-03-17 04:34:43 +00002470 case PJMEDIA_TYPE_VIDEO:
Nanang Izzuddinbf26db12011-03-18 07:54:50 +00002471 status = video_channel_update(call_med, tmp_pool,
2472 local_sdp, remote_sdp);
Benny Prijono0bc99a92011-03-17 04:34:43 +00002473 break;
Nanang Izzuddin50fae732011-03-22 09:49:23 +00002474#endif
Benny Prijono0bc99a92011-03-17 04:34:43 +00002475 default:
Nanang Izzuddinb6c239c2011-05-10 05:42:28 +00002476 status = PJMEDIA_EINVALIMEDIATYPE;
Benny Prijono0bc99a92011-03-17 04:34:43 +00002477 break;
2478 }
2479
Nanang Izzuddind8c33d82011-08-18 18:30:55 +00002480 /* Close the transport of deactivated media, need this here as media
2481 * can be deactivated by the SDP negotiation and the max media count
2482 * (account) setting.
2483 */
2484 if (local_sdp->media[mi]->desc.port==0 && call_med->tp) {
2485 pjmedia_transport_close(call_med->tp);
2486 call_med->tp = call_med->tp_orig = NULL;
Sauw Ming73ecfe82011-09-21 10:20:01 +00002487 set_media_tp_state(call_med, PJSUA_MED_TP_IDLE);
Nanang Izzuddind8c33d82011-08-18 18:30:55 +00002488 }
2489
Benny Prijono0bc99a92011-03-17 04:34:43 +00002490 if (status != PJ_SUCCESS) {
Nanang Izzuddinb6c239c2011-05-10 05:42:28 +00002491 PJ_PERROR(1,(THIS_FILE, status, "Error updating media call%02d:%d",
Benny Prijono0bc99a92011-03-17 04:34:43 +00002492 call_id, mi));
Nanang Izzuddinb6c239c2011-05-10 05:42:28 +00002493 } else {
2494 got_media = PJ_TRUE;
Benny Prijono0bc99a92011-03-17 04:34:43 +00002495 }
2496 }
2497
Nanang Izzuddind8c33d82011-08-18 18:30:55 +00002498 /* Perform SDP re-negotiation if needed. */
2499 if (got_media && need_renego_sdp) {
2500 pjmedia_sdp_neg *neg = call->inv->neg;
2501
2502 /* This should only happen when we are the answerer. */
2503 PJ_ASSERT_RETURN(neg && !pjmedia_sdp_neg_was_answer_remote(neg),
2504 PJMEDIA_SDPNEG_EINSTATE);
2505
2506 status = pjmedia_sdp_neg_set_remote_offer(tmp_pool, neg, remote_sdp);
2507 if (status != PJ_SUCCESS)
Benny Prijonob90fd382011-09-18 14:59:56 +00002508 goto on_error;
Nanang Izzuddind8c33d82011-08-18 18:30:55 +00002509
2510 status = pjmedia_sdp_neg_set_local_answer(tmp_pool, neg, local_sdp);
2511 if (status != PJ_SUCCESS)
Benny Prijonob90fd382011-09-18 14:59:56 +00002512 goto on_error;
Nanang Izzuddind8c33d82011-08-18 18:30:55 +00002513
2514 status = pjmedia_sdp_neg_negotiate(tmp_pool, neg, 0);
2515 if (status != PJ_SUCCESS)
Benny Prijonob90fd382011-09-18 14:59:56 +00002516 goto on_error;
Nanang Izzuddind8c33d82011-08-18 18:30:55 +00002517 }
2518
Benny Prijonob90fd382011-09-18 14:59:56 +00002519 pj_log_pop_indent();
Nanang Izzuddinb6c239c2011-05-10 05:42:28 +00002520 return (got_media? PJ_SUCCESS : PJMEDIA_SDPNEG_ENOMEDIA);
Benny Prijonob90fd382011-09-18 14:59:56 +00002521
2522on_error:
2523 pj_log_pop_indent();
2524 return status;
Benny Prijono0bc99a92011-03-17 04:34:43 +00002525}
2526
Benny Prijonoeebe9af2006-06-13 22:57:13 +00002527/*
2528 * Get maxinum number of conference ports.
2529 */
2530PJ_DEF(unsigned) pjsua_conf_get_max_ports(void)
2531{
2532 return pjsua_var.media_cfg.max_media_ports;
2533}
2534
2535
2536/*
2537 * Get current number of active ports in the bridge.
2538 */
2539PJ_DEF(unsigned) pjsua_conf_get_active_ports(void)
2540{
Benny Prijono38fb3ea2008-01-02 08:27:03 +00002541 unsigned ports[PJSUA_MAX_CONF_PORTS];
Benny Prijonoeebe9af2006-06-13 22:57:13 +00002542 unsigned count = PJ_ARRAY_SIZE(ports);
2543 pj_status_t status;
2544
2545 status = pjmedia_conf_enum_ports(pjsua_var.mconf, ports, &count);
2546 if (status != PJ_SUCCESS)
2547 count = 0;
2548
2549 return count;
2550}
2551
2552
2553/*
2554 * Enumerate all conference ports.
2555 */
2556PJ_DEF(pj_status_t) pjsua_enum_conf_ports(pjsua_conf_port_id id[],
2557 unsigned *count)
2558{
2559 return pjmedia_conf_enum_ports(pjsua_var.mconf, (unsigned*)id, count);
2560}
2561
2562
2563/*
2564 * Get information about the specified conference port
2565 */
2566PJ_DEF(pj_status_t) pjsua_conf_get_port_info( pjsua_conf_port_id id,
2567 pjsua_conf_port_info *info)
2568{
2569 pjmedia_conf_port_info cinfo;
Benny Prijono0498d902006-06-19 14:49:14 +00002570 unsigned i;
Benny Prijonoeebe9af2006-06-13 22:57:13 +00002571 pj_status_t status;
2572
2573 status = pjmedia_conf_get_port_info( pjsua_var.mconf, id, &cinfo);
2574 if (status != PJ_SUCCESS)
2575 return status;
2576
Benny Prijonoac623b32006-07-03 15:19:31 +00002577 pj_bzero(info, sizeof(*info));
Benny Prijonoeebe9af2006-06-13 22:57:13 +00002578 info->slot_id = id;
2579 info->name = cinfo.name;
2580 info->clock_rate = cinfo.clock_rate;
2581 info->channel_count = cinfo.channel_count;
2582 info->samples_per_frame = cinfo.samples_per_frame;
2583 info->bits_per_sample = cinfo.bits_per_sample;
2584
2585 /* Build array of listeners */
Benny Prijonoc78c3a32006-06-16 15:54:43 +00002586 info->listener_cnt = cinfo.listener_cnt;
2587 for (i=0; i<cinfo.listener_cnt; ++i) {
2588 info->listeners[i] = cinfo.listener_slots[i];
Benny Prijonoeebe9af2006-06-13 22:57:13 +00002589 }
2590
2591 return PJ_SUCCESS;
2592}
2593
2594
2595/*
Benny Prijonoe909eac2006-07-27 22:04:56 +00002596 * Add arbitrary media port to PJSUA's conference bridge.
2597 */
2598PJ_DEF(pj_status_t) pjsua_conf_add_port( pj_pool_t *pool,
2599 pjmedia_port *port,
2600 pjsua_conf_port_id *p_id)
2601{
2602 pj_status_t status;
2603
2604 status = pjmedia_conf_add_port(pjsua_var.mconf, pool,
2605 port, NULL, (unsigned*)p_id);
2606 if (status != PJ_SUCCESS) {
2607 if (p_id)
2608 *p_id = PJSUA_INVALID_ID;
2609 }
2610
2611 return status;
2612}
2613
2614
2615/*
2616 * Remove arbitrary slot from the conference bridge.
2617 */
2618PJ_DEF(pj_status_t) pjsua_conf_remove_port(pjsua_conf_port_id id)
2619{
Nanang Izzuddin148fd392008-06-16 09:52:50 +00002620 pj_status_t status;
2621
2622 status = pjmedia_conf_remove_port(pjsua_var.mconf, (unsigned)id);
2623 check_snd_dev_idle();
2624
2625 return status;
Benny Prijonoe909eac2006-07-27 22:04:56 +00002626}
2627
2628
2629/*
Benny Prijonoeebe9af2006-06-13 22:57:13 +00002630 * Establish unidirectional media flow from souce to sink.
2631 */
2632PJ_DEF(pj_status_t) pjsua_conf_connect( pjsua_conf_port_id source,
2633 pjsua_conf_port_id sink)
2634{
Benny Prijonob90fd382011-09-18 14:59:56 +00002635 pj_status_t status = PJ_SUCCESS;
2636
2637 PJ_LOG(4,(THIS_FILE, "%s connect: %d --> %d",
2638 (pjsua_var.is_mswitch ? "Switch" : "Conf"),
2639 source, sink));
2640 pj_log_push_indent();
2641
Nanang Izzuddin68559c32008-06-13 17:01:46 +00002642 /* If sound device idle timer is active, cancel it first. */
Benny Prijono0f711b42009-05-06 19:08:43 +00002643 PJSUA_LOCK();
Nanang Izzuddin68559c32008-06-13 17:01:46 +00002644 if (pjsua_var.snd_idle_timer.id) {
2645 pjsip_endpt_cancel_timer(pjsua_var.endpt, &pjsua_var.snd_idle_timer);
2646 pjsua_var.snd_idle_timer.id = PJ_FALSE;
2647 }
Benny Prijono0f711b42009-05-06 19:08:43 +00002648 PJSUA_UNLOCK();
Nanang Izzuddin68559c32008-06-13 17:01:46 +00002649
Nanang Izzuddinfe02a062009-02-18 14:28:49 +00002650
Benny Prijonof798e502009-03-09 13:08:16 +00002651 /* For audio switchboard (i.e. APS-Direct):
2652 * Check if sound device need to be reopened, i.e: its attributes
Nanang Izzuddinfe02a062009-02-18 14:28:49 +00002653 * (format, clock rate, channel count) must match to peer's.
2654 * Note that sound device can be reopened only if it doesn't have
2655 * any connection.
2656 */
Benny Prijonof798e502009-03-09 13:08:16 +00002657 if (pjsua_var.is_mswitch) {
Nanang Izzuddinfe02a062009-02-18 14:28:49 +00002658 pjmedia_conf_port_info port0_info;
2659 pjmedia_conf_port_info peer_info;
2660 unsigned peer_id;
2661 pj_bool_t need_reopen = PJ_FALSE;
Nanang Izzuddinfe02a062009-02-18 14:28:49 +00002662
2663 peer_id = (source!=0)? source : sink;
2664 status = pjmedia_conf_get_port_info(pjsua_var.mconf, peer_id,
2665 &peer_info);
2666 pj_assert(status == PJ_SUCCESS);
2667
2668 status = pjmedia_conf_get_port_info(pjsua_var.mconf, 0, &port0_info);
2669 pj_assert(status == PJ_SUCCESS);
2670
2671 /* Check if sound device is instantiated. */
2672 need_reopen = (pjsua_var.snd_port==NULL && pjsua_var.null_snd==NULL &&
2673 !pjsua_var.no_snd);
2674
2675 /* Check if sound device need to reopen because it needs to modify
2676 * settings to match its peer. Sound device must be idle in this case
2677 * though.
2678 */
2679 if (!need_reopen &&
2680 port0_info.listener_cnt==0 && port0_info.transmitter_cnt==0)
2681 {
2682 need_reopen = (peer_info.format.id != port0_info.format.id ||
Benny Prijonoc45d9512010-12-10 11:04:30 +00002683 peer_info.format.det.aud.avg_bps !=
2684 port0_info.format.det.aud.avg_bps ||
Nanang Izzuddinfe02a062009-02-18 14:28:49 +00002685 peer_info.clock_rate != port0_info.clock_rate ||
Benny Prijonoc45d9512010-12-10 11:04:30 +00002686 peer_info.channel_count!=port0_info.channel_count);
Nanang Izzuddinfe02a062009-02-18 14:28:49 +00002687 }
2688
2689 if (need_reopen) {
Benny Prijonod65f78c2009-06-03 18:59:37 +00002690 if (pjsua_var.cap_dev != NULL_SND_DEV_ID) {
Sauw Ming98766c72011-03-11 06:57:24 +00002691 pjmedia_snd_port_param param;
Nanang Izzuddinfe02a062009-02-18 14:28:49 +00002692
Benny Prijonod65f78c2009-06-03 18:59:37 +00002693 /* Create parameter based on peer info */
Sauw Ming98766c72011-03-11 06:57:24 +00002694 status = create_aud_param(&param.base, pjsua_var.cap_dev,
Benny Prijonod65f78c2009-06-03 18:59:37 +00002695 pjsua_var.play_dev,
2696 peer_info.clock_rate,
2697 peer_info.channel_count,
2698 peer_info.samples_per_frame,
2699 peer_info.bits_per_sample);
2700 if (status != PJ_SUCCESS) {
Benny Prijonoc45d9512010-12-10 11:04:30 +00002701 pjsua_perror(THIS_FILE, "Error opening sound device",
2702 status);
Benny Prijonob90fd382011-09-18 14:59:56 +00002703 goto on_return;
Benny Prijonod65f78c2009-06-03 18:59:37 +00002704 }
Benny Prijonof798e502009-03-09 13:08:16 +00002705
Benny Prijonod65f78c2009-06-03 18:59:37 +00002706 /* And peer format */
2707 if (peer_info.format.id != PJMEDIA_FORMAT_PCM) {
Sauw Ming98766c72011-03-11 06:57:24 +00002708 param.base.flags |= PJMEDIA_AUD_DEV_CAP_EXT_FORMAT;
2709 param.base.ext_fmt = peer_info.format;
Benny Prijonod65f78c2009-06-03 18:59:37 +00002710 }
Benny Prijono10454dc2009-02-21 14:21:59 +00002711
Sauw Ming98766c72011-03-11 06:57:24 +00002712 param.options = 0;
Benny Prijonod65f78c2009-06-03 18:59:37 +00002713 status = open_snd_dev(&param);
2714 if (status != PJ_SUCCESS) {
Benny Prijonoc45d9512010-12-10 11:04:30 +00002715 pjsua_perror(THIS_FILE, "Error opening sound device",
2716 status);
Benny Prijonob90fd382011-09-18 14:59:56 +00002717 goto on_return;
Benny Prijonod65f78c2009-06-03 18:59:37 +00002718 }
2719 } else {
2720 /* Null-audio */
Benny Prijonoc45d9512010-12-10 11:04:30 +00002721 status = pjsua_set_snd_dev(pjsua_var.cap_dev,
2722 pjsua_var.play_dev);
Benny Prijonod65f78c2009-06-03 18:59:37 +00002723 if (status != PJ_SUCCESS) {
Benny Prijonoc45d9512010-12-10 11:04:30 +00002724 pjsua_perror(THIS_FILE, "Error opening sound device",
2725 status);
Benny Prijonob90fd382011-09-18 14:59:56 +00002726 goto on_return;
Benny Prijonod65f78c2009-06-03 18:59:37 +00002727 }
Nanang Izzuddinfe02a062009-02-18 14:28:49 +00002728 }
Benny Prijono2d647722011-07-13 03:05:22 +00002729 } else if (pjsua_var.no_snd) {
2730 if (!pjsua_var.snd_is_on) {
2731 pjsua_var.snd_is_on = PJ_TRUE;
2732 /* Notify app */
2733 if (pjsua_var.ua_cfg.cb.on_snd_dev_operation) {
2734 (*pjsua_var.ua_cfg.cb.on_snd_dev_operation)(1);
2735 }
2736 }
Nanang Izzuddinfe02a062009-02-18 14:28:49 +00002737 }
Nanang Izzuddinfe02a062009-02-18 14:28:49 +00002738
Benny Prijonof798e502009-03-09 13:08:16 +00002739 } else {
2740 /* The bridge version */
Nanang Izzuddinfe02a062009-02-18 14:28:49 +00002741
Benny Prijonof798e502009-03-09 13:08:16 +00002742 /* Create sound port if none is instantiated */
2743 if (pjsua_var.snd_port==NULL && pjsua_var.null_snd==NULL &&
2744 !pjsua_var.no_snd)
2745 {
2746 pj_status_t status;
Nanang Izzuddin68559c32008-06-13 17:01:46 +00002747
Benny Prijonof798e502009-03-09 13:08:16 +00002748 status = pjsua_set_snd_dev(pjsua_var.cap_dev, pjsua_var.play_dev);
2749 if (status != PJ_SUCCESS) {
2750 pjsua_perror(THIS_FILE, "Error opening sound device", status);
Benny Prijonob90fd382011-09-18 14:59:56 +00002751 goto on_return;
Benny Prijonof798e502009-03-09 13:08:16 +00002752 }
Benny Prijono2d647722011-07-13 03:05:22 +00002753 } else if (pjsua_var.no_snd && !pjsua_var.snd_is_on) {
2754 pjsua_var.snd_is_on = PJ_TRUE;
2755 /* Notify app */
2756 if (pjsua_var.ua_cfg.cb.on_snd_dev_operation) {
2757 (*pjsua_var.ua_cfg.cb.on_snd_dev_operation)(1);
2758 }
Nanang Izzuddin68559c32008-06-13 17:01:46 +00002759 }
Benny Prijonof798e502009-03-09 13:08:16 +00002760 }
Nanang Izzuddinfe02a062009-02-18 14:28:49 +00002761
Benny Prijonob90fd382011-09-18 14:59:56 +00002762 status = pjmedia_conf_connect_port(pjsua_var.mconf, source, sink, 0);
2763
2764on_return:
2765 pj_log_pop_indent();
2766 return status;
Benny Prijonoeebe9af2006-06-13 22:57:13 +00002767}
2768
2769
2770/*
2771 * Disconnect media flow from the source to destination port.
2772 */
2773PJ_DEF(pj_status_t) pjsua_conf_disconnect( pjsua_conf_port_id source,
2774 pjsua_conf_port_id sink)
2775{
Nanang Izzuddin68559c32008-06-13 17:01:46 +00002776 pj_status_t status;
2777
Benny Prijonob90fd382011-09-18 14:59:56 +00002778 PJ_LOG(4,(THIS_FILE, "%s disconnect: %d -x- %d",
2779 (pjsua_var.is_mswitch ? "Switch" : "Conf"),
2780 source, sink));
2781 pj_log_push_indent();
2782
Nanang Izzuddin68559c32008-06-13 17:01:46 +00002783 status = pjmedia_conf_disconnect_port(pjsua_var.mconf, source, sink);
Nanang Izzuddin148fd392008-06-16 09:52:50 +00002784 check_snd_dev_idle();
Nanang Izzuddin68559c32008-06-13 17:01:46 +00002785
Benny Prijonob90fd382011-09-18 14:59:56 +00002786 pj_log_pop_indent();
Nanang Izzuddin68559c32008-06-13 17:01:46 +00002787 return status;
Benny Prijonoeebe9af2006-06-13 22:57:13 +00002788}
2789
2790
Benny Prijono6dd967c2006-12-26 02:27:14 +00002791/*
2792 * Adjust the signal level to be transmitted from the bridge to the
2793 * specified port by making it louder or quieter.
2794 */
2795PJ_DEF(pj_status_t) pjsua_conf_adjust_tx_level(pjsua_conf_port_id slot,
2796 float level)
2797{
2798 return pjmedia_conf_adjust_tx_level(pjsua_var.mconf, slot,
2799 (int)((level-1) * 128));
2800}
2801
2802/*
2803 * Adjust the signal level to be received from the specified port (to
2804 * the bridge) by making it louder or quieter.
2805 */
2806PJ_DEF(pj_status_t) pjsua_conf_adjust_rx_level(pjsua_conf_port_id slot,
2807 float level)
2808{
2809 return pjmedia_conf_adjust_rx_level(pjsua_var.mconf, slot,
2810 (int)((level-1) * 128));
2811}
2812
2813
2814/*
2815 * Get last signal level transmitted to or received from the specified port.
2816 */
2817PJ_DEF(pj_status_t) pjsua_conf_get_signal_level(pjsua_conf_port_id slot,
2818 unsigned *tx_level,
2819 unsigned *rx_level)
2820{
2821 return pjmedia_conf_get_signal_level(pjsua_var.mconf, slot,
2822 tx_level, rx_level);
2823}
2824
Benny Prijonoeebe9af2006-06-13 22:57:13 +00002825/*****************************************************************************
2826 * File player.
2827 */
2828
Benny Prijonod5696da2007-07-17 16:25:45 +00002829static char* get_basename(const char *path, unsigned len)
2830{
2831 char *p = ((char*)path) + len;
2832
2833 if (len==0)
2834 return p;
2835
Benny Prijono1f61a8f2007-08-16 10:11:44 +00002836 for (--p; p!=path && *p!='/' && *p!='\\'; ) --p;
Benny Prijonod5696da2007-07-17 16:25:45 +00002837
2838 return (p==path) ? p : p+1;
2839}
2840
2841
Benny Prijonoeebe9af2006-06-13 22:57:13 +00002842/*
2843 * Create a file player, and automatically connect this player to
2844 * the conference bridge.
2845 */
2846PJ_DEF(pj_status_t) pjsua_player_create( const pj_str_t *filename,
2847 unsigned options,
Benny Prijonoeebe9af2006-06-13 22:57:13 +00002848 pjsua_player_id *p_id)
2849{
2850 unsigned slot, file_id;
Benny Prijonoa66c3312007-01-21 23:12:40 +00002851 char path[PJ_MAXPATH];
Benny Prijonob90fd382011-09-18 14:59:56 +00002852 pj_pool_t *pool = NULL;
Benny Prijonoeebe9af2006-06-13 22:57:13 +00002853 pjmedia_port *port;
Benny Prijonob90fd382011-09-18 14:59:56 +00002854 pj_status_t status = PJ_SUCCESS;
Benny Prijonoeebe9af2006-06-13 22:57:13 +00002855
2856 if (pjsua_var.player_cnt >= PJ_ARRAY_SIZE(pjsua_var.player))
2857 return PJ_ETOOMANY;
2858
Benny Prijonob90fd382011-09-18 14:59:56 +00002859 PJ_LOG(4,(THIS_FILE, "Creating file player: %.*s..",
2860 (int)filename->slen, filename->ptr));
2861 pj_log_push_indent();
2862
Benny Prijonoeebe9af2006-06-13 22:57:13 +00002863 PJSUA_LOCK();
2864
2865 for (file_id=0; file_id<PJ_ARRAY_SIZE(pjsua_var.player); ++file_id) {
2866 if (pjsua_var.player[file_id].port == NULL)
2867 break;
2868 }
2869
2870 if (file_id == PJ_ARRAY_SIZE(pjsua_var.player)) {
2871 /* This is unexpected */
Benny Prijonoeebe9af2006-06-13 22:57:13 +00002872 pj_assert(0);
Benny Prijonob90fd382011-09-18 14:59:56 +00002873 status = PJ_EBUG;
2874 goto on_error;
Benny Prijonoeebe9af2006-06-13 22:57:13 +00002875 }
2876
2877 pj_memcpy(path, filename->ptr, filename->slen);
2878 path[filename->slen] = '\0';
Benny Prijonod5696da2007-07-17 16:25:45 +00002879
2880 pool = pjsua_pool_create(get_basename(path, filename->slen), 1000, 1000);
2881 if (!pool) {
Benny Prijonob90fd382011-09-18 14:59:56 +00002882 status = PJ_ENOMEM;
2883 goto on_error;
Benny Prijonod5696da2007-07-17 16:25:45 +00002884 }
2885
Nanang Izzuddin81e9bd52008-06-27 12:52:51 +00002886 status = pjmedia_wav_player_port_create(
2887 pool, path,
2888 pjsua_var.mconf_cfg.samples_per_frame *
Benny Prijonoc45d9512010-12-10 11:04:30 +00002889 1000 / pjsua_var.media_cfg.channel_count /
Nanang Izzuddin81e9bd52008-06-27 12:52:51 +00002890 pjsua_var.media_cfg.clock_rate,
2891 options, 0, &port);
Benny Prijonoeebe9af2006-06-13 22:57:13 +00002892 if (status != PJ_SUCCESS) {
Benny Prijonoeebe9af2006-06-13 22:57:13 +00002893 pjsua_perror(THIS_FILE, "Unable to open file for playback", status);
Benny Prijonob90fd382011-09-18 14:59:56 +00002894 goto on_error;
Benny Prijonoeebe9af2006-06-13 22:57:13 +00002895 }
2896
Benny Prijono5297af92008-03-18 13:40:40 +00002897 status = pjmedia_conf_add_port(pjsua_var.mconf, pool,
Benny Prijonoeebe9af2006-06-13 22:57:13 +00002898 port, filename, &slot);
2899 if (status != PJ_SUCCESS) {
2900 pjmedia_port_destroy(port);
Benny Prijono32e4f492007-01-24 00:44:26 +00002901 pjsua_perror(THIS_FILE, "Unable to add file to conference bridge",
2902 status);
Benny Prijonob90fd382011-09-18 14:59:56 +00002903 goto on_error;
Benny Prijonoeebe9af2006-06-13 22:57:13 +00002904 }
2905
Benny Prijonoa66c3312007-01-21 23:12:40 +00002906 pjsua_var.player[file_id].type = 0;
Benny Prijonod5696da2007-07-17 16:25:45 +00002907 pjsua_var.player[file_id].pool = pool;
Benny Prijonoeebe9af2006-06-13 22:57:13 +00002908 pjsua_var.player[file_id].port = port;
2909 pjsua_var.player[file_id].slot = slot;
2910
2911 if (p_id) *p_id = file_id;
2912
2913 ++pjsua_var.player_cnt;
2914
2915 PJSUA_UNLOCK();
Benny Prijonob90fd382011-09-18 14:59:56 +00002916
2917 PJ_LOG(4,(THIS_FILE, "Player created, id=%d, slot=%d", file_id, slot));
2918
2919 pj_log_pop_indent();
Benny Prijonoeebe9af2006-06-13 22:57:13 +00002920 return PJ_SUCCESS;
Benny Prijonob90fd382011-09-18 14:59:56 +00002921
2922on_error:
2923 PJSUA_UNLOCK();
2924 if (pool) pj_pool_release(pool);
2925 pj_log_pop_indent();
2926 return status;
Benny Prijonoeebe9af2006-06-13 22:57:13 +00002927}
2928
2929
2930/*
Benny Prijonoa66c3312007-01-21 23:12:40 +00002931 * Create a file playlist media port, and automatically add the port
2932 * to the conference bridge.
2933 */
2934PJ_DEF(pj_status_t) pjsua_playlist_create( const pj_str_t file_names[],
2935 unsigned file_count,
2936 const pj_str_t *label,
2937 unsigned options,
2938 pjsua_player_id *p_id)
2939{
2940 unsigned slot, file_id, ptime;
Benny Prijonob90fd382011-09-18 14:59:56 +00002941 pj_pool_t *pool = NULL;
Benny Prijonoa66c3312007-01-21 23:12:40 +00002942 pjmedia_port *port;
Benny Prijonob90fd382011-09-18 14:59:56 +00002943 pj_status_t status = PJ_SUCCESS;
Benny Prijonoa66c3312007-01-21 23:12:40 +00002944
2945 if (pjsua_var.player_cnt >= PJ_ARRAY_SIZE(pjsua_var.player))
2946 return PJ_ETOOMANY;
2947
Benny Prijonob90fd382011-09-18 14:59:56 +00002948 PJ_LOG(4,(THIS_FILE, "Creating playlist with %d file(s)..", file_count));
2949 pj_log_push_indent();
2950
Benny Prijonoa66c3312007-01-21 23:12:40 +00002951 PJSUA_LOCK();
2952
2953 for (file_id=0; file_id<PJ_ARRAY_SIZE(pjsua_var.player); ++file_id) {
2954 if (pjsua_var.player[file_id].port == NULL)
2955 break;
2956 }
2957
2958 if (file_id == PJ_ARRAY_SIZE(pjsua_var.player)) {
2959 /* This is unexpected */
Benny Prijonoa66c3312007-01-21 23:12:40 +00002960 pj_assert(0);
Benny Prijonob90fd382011-09-18 14:59:56 +00002961 status = PJ_EBUG;
2962 goto on_error;
Benny Prijonoa66c3312007-01-21 23:12:40 +00002963 }
2964
2965
2966 ptime = pjsua_var.mconf_cfg.samples_per_frame * 1000 /
2967 pjsua_var.media_cfg.clock_rate;
2968
Benny Prijonod5696da2007-07-17 16:25:45 +00002969 pool = pjsua_pool_create("playlist", 1000, 1000);
2970 if (!pool) {
Benny Prijonob90fd382011-09-18 14:59:56 +00002971 status = PJ_ENOMEM;
2972 goto on_error;
Benny Prijonod5696da2007-07-17 16:25:45 +00002973 }
2974
2975 status = pjmedia_wav_playlist_create(pool, label,
Benny Prijonoa66c3312007-01-21 23:12:40 +00002976 file_names, file_count,
2977 ptime, options, 0, &port);
2978 if (status != PJ_SUCCESS) {
Benny Prijonoa66c3312007-01-21 23:12:40 +00002979 pjsua_perror(THIS_FILE, "Unable to create playlist", status);
Benny Prijonob90fd382011-09-18 14:59:56 +00002980 goto on_error;
Benny Prijonoa66c3312007-01-21 23:12:40 +00002981 }
2982
Benny Prijonod5696da2007-07-17 16:25:45 +00002983 status = pjmedia_conf_add_port(pjsua_var.mconf, pool,
Benny Prijonoa66c3312007-01-21 23:12:40 +00002984 port, &port->info.name, &slot);
2985 if (status != PJ_SUCCESS) {
2986 pjmedia_port_destroy(port);
Benny Prijonoa66c3312007-01-21 23:12:40 +00002987 pjsua_perror(THIS_FILE, "Unable to add port", status);
Benny Prijonob90fd382011-09-18 14:59:56 +00002988 goto on_error;
Benny Prijonoa66c3312007-01-21 23:12:40 +00002989 }
2990
2991 pjsua_var.player[file_id].type = 1;
Benny Prijonod5696da2007-07-17 16:25:45 +00002992 pjsua_var.player[file_id].pool = pool;
Benny Prijonoa66c3312007-01-21 23:12:40 +00002993 pjsua_var.player[file_id].port = port;
2994 pjsua_var.player[file_id].slot = slot;
2995
2996 if (p_id) *p_id = file_id;
2997
2998 ++pjsua_var.player_cnt;
2999
3000 PJSUA_UNLOCK();
Benny Prijonob90fd382011-09-18 14:59:56 +00003001
3002 PJ_LOG(4,(THIS_FILE, "Playlist created, id=%d, slot=%d", file_id, slot));
3003
3004 pj_log_pop_indent();
3005
Benny Prijonoa66c3312007-01-21 23:12:40 +00003006 return PJ_SUCCESS;
3007
Benny Prijonob90fd382011-09-18 14:59:56 +00003008on_error:
3009 PJSUA_UNLOCK();
3010 if (pool) pj_pool_release(pool);
3011 pj_log_pop_indent();
3012
3013 return status;
Benny Prijonoa66c3312007-01-21 23:12:40 +00003014}
3015
3016
3017/*
Benny Prijonoeebe9af2006-06-13 22:57:13 +00003018 * Get conference port ID associated with player.
3019 */
3020PJ_DEF(pjsua_conf_port_id) pjsua_player_get_conf_port(pjsua_player_id id)
3021{
Benny Prijonoa1e69682007-05-11 15:14:34 +00003022 PJ_ASSERT_RETURN(id>=0&&id<(int)PJ_ARRAY_SIZE(pjsua_var.player), PJ_EINVAL);
Benny Prijonoeebe9af2006-06-13 22:57:13 +00003023 PJ_ASSERT_RETURN(pjsua_var.player[id].port != NULL, PJ_EINVAL);
3024
3025 return pjsua_var.player[id].slot;
3026}
3027
Benny Prijono469b1522006-12-26 03:05:17 +00003028/*
3029 * Get the media port for the player.
3030 */
Benny Prijonobe41d862008-01-18 13:24:28 +00003031PJ_DEF(pj_status_t) pjsua_player_get_port( pjsua_player_id id,
Benny Prijono469b1522006-12-26 03:05:17 +00003032 pjmedia_port **p_port)
3033{
Benny Prijonoa1e69682007-05-11 15:14:34 +00003034 PJ_ASSERT_RETURN(id>=0&&id<(int)PJ_ARRAY_SIZE(pjsua_var.player), PJ_EINVAL);
Benny Prijono469b1522006-12-26 03:05:17 +00003035 PJ_ASSERT_RETURN(pjsua_var.player[id].port != NULL, PJ_EINVAL);
3036 PJ_ASSERT_RETURN(p_port != NULL, PJ_EINVAL);
3037
3038 *p_port = pjsua_var.player[id].port;
3039
3040 return PJ_SUCCESS;
3041}
Benny Prijonoeebe9af2006-06-13 22:57:13 +00003042
3043/*
3044 * Set playback position.
3045 */
3046PJ_DEF(pj_status_t) pjsua_player_set_pos( pjsua_player_id id,
3047 pj_uint32_t samples)
3048{
Benny Prijonoa1e69682007-05-11 15:14:34 +00003049 PJ_ASSERT_RETURN(id>=0&&id<(int)PJ_ARRAY_SIZE(pjsua_var.player), PJ_EINVAL);
Benny Prijonoeebe9af2006-06-13 22:57:13 +00003050 PJ_ASSERT_RETURN(pjsua_var.player[id].port != NULL, PJ_EINVAL);
Benny Prijonoa66c3312007-01-21 23:12:40 +00003051 PJ_ASSERT_RETURN(pjsua_var.player[id].type == 0, PJ_EINVAL);
Benny Prijonoeebe9af2006-06-13 22:57:13 +00003052
3053 return pjmedia_wav_player_port_set_pos(pjsua_var.player[id].port, samples);
3054}
3055
3056
3057/*
3058 * Close the file, remove the player from the bridge, and free
3059 * resources associated with the file player.
3060 */
3061PJ_DEF(pj_status_t) pjsua_player_destroy(pjsua_player_id id)
3062{
Benny Prijonoa1e69682007-05-11 15:14:34 +00003063 PJ_ASSERT_RETURN(id>=0&&id<(int)PJ_ARRAY_SIZE(pjsua_var.player), PJ_EINVAL);
Benny Prijonoeebe9af2006-06-13 22:57:13 +00003064 PJ_ASSERT_RETURN(pjsua_var.player[id].port != NULL, PJ_EINVAL);
3065
Benny Prijonob90fd382011-09-18 14:59:56 +00003066 PJ_LOG(4,(THIS_FILE, "Destroying player %d..", id));
3067 pj_log_push_indent();
3068
Benny Prijonoeebe9af2006-06-13 22:57:13 +00003069 PJSUA_LOCK();
3070
3071 if (pjsua_var.player[id].port) {
Nanang Izzuddin148fd392008-06-16 09:52:50 +00003072 pjsua_conf_remove_port(pjsua_var.player[id].slot);
Benny Prijonoeebe9af2006-06-13 22:57:13 +00003073 pjmedia_port_destroy(pjsua_var.player[id].port);
3074 pjsua_var.player[id].port = NULL;
3075 pjsua_var.player[id].slot = 0xFFFF;
Benny Prijonod5696da2007-07-17 16:25:45 +00003076 pj_pool_release(pjsua_var.player[id].pool);
3077 pjsua_var.player[id].pool = NULL;
Benny Prijonoeebe9af2006-06-13 22:57:13 +00003078 pjsua_var.player_cnt--;
3079 }
3080
3081 PJSUA_UNLOCK();
Benny Prijonob90fd382011-09-18 14:59:56 +00003082 pj_log_pop_indent();
Benny Prijonoeebe9af2006-06-13 22:57:13 +00003083
3084 return PJ_SUCCESS;
3085}
3086
3087
3088/*****************************************************************************
3089 * File recorder.
3090 */
3091
3092/*
3093 * Create a file recorder, and automatically connect this recorder to
3094 * the conference bridge.
3095 */
3096PJ_DEF(pj_status_t) pjsua_recorder_create( const pj_str_t *filename,
Benny Prijono8f310522006-10-20 11:08:49 +00003097 unsigned enc_type,
3098 void *enc_param,
Benny Prijonoeebe9af2006-06-13 22:57:13 +00003099 pj_ssize_t max_size,
3100 unsigned options,
Benny Prijonoeebe9af2006-06-13 22:57:13 +00003101 pjsua_recorder_id *p_id)
3102{
Benny Prijonob3cdb2b2006-10-19 15:49:47 +00003103 enum Format
3104 {
3105 FMT_UNKNOWN,
3106 FMT_WAV,
3107 FMT_MP3,
3108 };
Benny Prijonoeebe9af2006-06-13 22:57:13 +00003109 unsigned slot, file_id;
Benny Prijonoa66c3312007-01-21 23:12:40 +00003110 char path[PJ_MAXPATH];
Benny Prijonob3cdb2b2006-10-19 15:49:47 +00003111 pj_str_t ext;
Benny Prijono8f310522006-10-20 11:08:49 +00003112 int file_format;
Benny Prijonob90fd382011-09-18 14:59:56 +00003113 pj_pool_t *pool = NULL;
Benny Prijonoeebe9af2006-06-13 22:57:13 +00003114 pjmedia_port *port;
Benny Prijonob90fd382011-09-18 14:59:56 +00003115 pj_status_t status = PJ_SUCCESS;
Benny Prijonoeebe9af2006-06-13 22:57:13 +00003116
Benny Prijonob3cdb2b2006-10-19 15:49:47 +00003117 /* Filename must present */
3118 PJ_ASSERT_RETURN(filename != NULL, PJ_EINVAL);
3119
Benny Prijono00cae612006-07-31 15:19:36 +00003120 /* Don't support max_size at present */
Benny Prijonob3cdb2b2006-10-19 15:49:47 +00003121 PJ_ASSERT_RETURN(max_size == 0 || max_size == -1, PJ_EINVAL);
Benny Prijono00cae612006-07-31 15:19:36 +00003122
Benny Prijono8f310522006-10-20 11:08:49 +00003123 /* Don't support encoding type at present */
3124 PJ_ASSERT_RETURN(enc_type == 0, PJ_EINVAL);
Benny Prijono00cae612006-07-31 15:19:36 +00003125
Benny Prijonob90fd382011-09-18 14:59:56 +00003126 PJ_LOG(4,(THIS_FILE, "Creating recorder %.*s..",
3127 (int)filename->slen, filename->ptr));
3128 pj_log_push_indent();
3129
3130 if (pjsua_var.rec_cnt >= PJ_ARRAY_SIZE(pjsua_var.recorder)) {
3131 pj_log_pop_indent();
Benny Prijonoeebe9af2006-06-13 22:57:13 +00003132 return PJ_ETOOMANY;
Benny Prijonob90fd382011-09-18 14:59:56 +00003133 }
Benny Prijonoeebe9af2006-06-13 22:57:13 +00003134
Benny Prijonob3cdb2b2006-10-19 15:49:47 +00003135 /* Determine the file format */
3136 ext.ptr = filename->ptr + filename->slen - 4;
3137 ext.slen = 4;
3138
3139 if (pj_stricmp2(&ext, ".wav") == 0)
3140 file_format = FMT_WAV;
3141 else if (pj_stricmp2(&ext, ".mp3") == 0)
3142 file_format = FMT_MP3;
3143 else {
3144 PJ_LOG(1,(THIS_FILE, "pjsua_recorder_create() error: unable to "
3145 "determine file format for %.*s",
3146 (int)filename->slen, filename->ptr));
Benny Prijonob90fd382011-09-18 14:59:56 +00003147 pj_log_pop_indent();
Benny Prijonob3cdb2b2006-10-19 15:49:47 +00003148 return PJ_ENOTSUP;
3149 }
3150
Benny Prijonoeebe9af2006-06-13 22:57:13 +00003151 PJSUA_LOCK();
3152
3153 for (file_id=0; file_id<PJ_ARRAY_SIZE(pjsua_var.recorder); ++file_id) {
3154 if (pjsua_var.recorder[file_id].port == NULL)
3155 break;
3156 }
3157
3158 if (file_id == PJ_ARRAY_SIZE(pjsua_var.recorder)) {
3159 /* This is unexpected */
Benny Prijonoeebe9af2006-06-13 22:57:13 +00003160 pj_assert(0);
Benny Prijonob90fd382011-09-18 14:59:56 +00003161 status = PJ_EBUG;
3162 goto on_return;
Benny Prijonoeebe9af2006-06-13 22:57:13 +00003163 }
3164
3165 pj_memcpy(path, filename->ptr, filename->slen);
3166 path[filename->slen] = '\0';
Benny Prijonob3cdb2b2006-10-19 15:49:47 +00003167
Benny Prijonod5696da2007-07-17 16:25:45 +00003168 pool = pjsua_pool_create(get_basename(path, filename->slen), 1000, 1000);
3169 if (!pool) {
Benny Prijonob90fd382011-09-18 14:59:56 +00003170 status = PJ_ENOMEM;
3171 goto on_return;
Benny Prijonod5696da2007-07-17 16:25:45 +00003172 }
3173
Benny Prijonob3cdb2b2006-10-19 15:49:47 +00003174 if (file_format == FMT_WAV) {
Benny Prijonod5696da2007-07-17 16:25:45 +00003175 status = pjmedia_wav_writer_port_create(pool, path,
Benny Prijonob3cdb2b2006-10-19 15:49:47 +00003176 pjsua_var.media_cfg.clock_rate,
3177 pjsua_var.mconf_cfg.channel_count,
3178 pjsua_var.mconf_cfg.samples_per_frame,
3179 pjsua_var.mconf_cfg.bits_per_sample,
3180 options, 0, &port);
Benny Prijonob3cdb2b2006-10-19 15:49:47 +00003181 } else {
Benny Prijonoc95a0f02007-04-09 07:06:08 +00003182 PJ_UNUSED_ARG(enc_param);
Benny Prijonob3cdb2b2006-10-19 15:49:47 +00003183 port = NULL;
3184 status = PJ_ENOTSUP;
3185 }
3186
Benny Prijonoeebe9af2006-06-13 22:57:13 +00003187 if (status != PJ_SUCCESS) {
Benny Prijonoeebe9af2006-06-13 22:57:13 +00003188 pjsua_perror(THIS_FILE, "Unable to open file for recording", status);
Benny Prijonob90fd382011-09-18 14:59:56 +00003189 goto on_return;
Benny Prijonoeebe9af2006-06-13 22:57:13 +00003190 }
3191
Benny Prijonod5696da2007-07-17 16:25:45 +00003192 status = pjmedia_conf_add_port(pjsua_var.mconf, pool,
Benny Prijonoeebe9af2006-06-13 22:57:13 +00003193 port, filename, &slot);
3194 if (status != PJ_SUCCESS) {
3195 pjmedia_port_destroy(port);
Benny Prijonob90fd382011-09-18 14:59:56 +00003196 goto on_return;
Benny Prijonoeebe9af2006-06-13 22:57:13 +00003197 }
3198
3199 pjsua_var.recorder[file_id].port = port;
3200 pjsua_var.recorder[file_id].slot = slot;
Benny Prijonod5696da2007-07-17 16:25:45 +00003201 pjsua_var.recorder[file_id].pool = pool;
Benny Prijonoeebe9af2006-06-13 22:57:13 +00003202
3203 if (p_id) *p_id = file_id;
3204
3205 ++pjsua_var.rec_cnt;
3206
3207 PJSUA_UNLOCK();
Benny Prijonob90fd382011-09-18 14:59:56 +00003208
3209 PJ_LOG(4,(THIS_FILE, "Recorder created, id=%d, slot=%d", file_id, slot));
3210
3211 pj_log_pop_indent();
Benny Prijonoeebe9af2006-06-13 22:57:13 +00003212 return PJ_SUCCESS;
Benny Prijonob90fd382011-09-18 14:59:56 +00003213
3214on_return:
3215 PJSUA_UNLOCK();
3216 if (pool) pj_pool_release(pool);
3217 pj_log_pop_indent();
3218 return status;
Benny Prijonoeebe9af2006-06-13 22:57:13 +00003219}
3220
3221
3222/*
3223 * Get conference port associated with recorder.
3224 */
3225PJ_DEF(pjsua_conf_port_id) pjsua_recorder_get_conf_port(pjsua_recorder_id id)
3226{
Benny Prijonoa1e69682007-05-11 15:14:34 +00003227 PJ_ASSERT_RETURN(id>=0 && id<(int)PJ_ARRAY_SIZE(pjsua_var.recorder),
3228 PJ_EINVAL);
Benny Prijonoeebe9af2006-06-13 22:57:13 +00003229 PJ_ASSERT_RETURN(pjsua_var.recorder[id].port != NULL, PJ_EINVAL);
3230
3231 return pjsua_var.recorder[id].slot;
3232}
3233
Benny Prijono469b1522006-12-26 03:05:17 +00003234/*
3235 * Get the media port for the recorder.
3236 */
3237PJ_DEF(pj_status_t) pjsua_recorder_get_port( pjsua_recorder_id id,
3238 pjmedia_port **p_port)
3239{
Benny Prijonoa1e69682007-05-11 15:14:34 +00003240 PJ_ASSERT_RETURN(id>=0 && id<(int)PJ_ARRAY_SIZE(pjsua_var.recorder),
3241 PJ_EINVAL);
Benny Prijono469b1522006-12-26 03:05:17 +00003242 PJ_ASSERT_RETURN(pjsua_var.recorder[id].port != NULL, PJ_EINVAL);
3243 PJ_ASSERT_RETURN(p_port != NULL, PJ_EINVAL);
3244
3245 *p_port = pjsua_var.recorder[id].port;
3246 return PJ_SUCCESS;
3247}
Benny Prijonoeebe9af2006-06-13 22:57:13 +00003248
3249/*
3250 * Destroy recorder (this will complete recording).
3251 */
3252PJ_DEF(pj_status_t) pjsua_recorder_destroy(pjsua_recorder_id id)
3253{
Benny Prijonoa1e69682007-05-11 15:14:34 +00003254 PJ_ASSERT_RETURN(id>=0 && id<(int)PJ_ARRAY_SIZE(pjsua_var.recorder),
3255 PJ_EINVAL);
Benny Prijonoeebe9af2006-06-13 22:57:13 +00003256 PJ_ASSERT_RETURN(pjsua_var.recorder[id].port != NULL, PJ_EINVAL);
3257
Benny Prijonob90fd382011-09-18 14:59:56 +00003258 PJ_LOG(4,(THIS_FILE, "Destroying recorder %d..", id));
3259 pj_log_push_indent();
3260
Benny Prijonoeebe9af2006-06-13 22:57:13 +00003261 PJSUA_LOCK();
3262
3263 if (pjsua_var.recorder[id].port) {
Nanang Izzuddin148fd392008-06-16 09:52:50 +00003264 pjsua_conf_remove_port(pjsua_var.recorder[id].slot);
Benny Prijonoeebe9af2006-06-13 22:57:13 +00003265 pjmedia_port_destroy(pjsua_var.recorder[id].port);
3266 pjsua_var.recorder[id].port = NULL;
3267 pjsua_var.recorder[id].slot = 0xFFFF;
Benny Prijonod5696da2007-07-17 16:25:45 +00003268 pj_pool_release(pjsua_var.recorder[id].pool);
3269 pjsua_var.recorder[id].pool = NULL;
Benny Prijonoeebe9af2006-06-13 22:57:13 +00003270 pjsua_var.rec_cnt--;
3271 }
3272
3273 PJSUA_UNLOCK();
Benny Prijonob90fd382011-09-18 14:59:56 +00003274 pj_log_pop_indent();
Benny Prijonoeebe9af2006-06-13 22:57:13 +00003275
3276 return PJ_SUCCESS;
3277}
3278
3279
3280/*****************************************************************************
3281 * Sound devices.
3282 */
3283
3284/*
3285 * Enum sound devices.
3286 */
Benny Prijonof798e502009-03-09 13:08:16 +00003287
3288PJ_DEF(pj_status_t) pjsua_enum_aud_devs( pjmedia_aud_dev_info info[],
Benny Prijonoeebe9af2006-06-13 22:57:13 +00003289 unsigned *count)
3290{
3291 unsigned i, dev_count;
3292
Benny Prijono10454dc2009-02-21 14:21:59 +00003293 dev_count = pjmedia_aud_dev_count();
Benny Prijonoeebe9af2006-06-13 22:57:13 +00003294
3295 if (dev_count > *count) dev_count = *count;
3296
3297 for (i=0; i<dev_count; ++i) {
Benny Prijono10454dc2009-02-21 14:21:59 +00003298 pj_status_t status;
Benny Prijonoeebe9af2006-06-13 22:57:13 +00003299
Benny Prijono10454dc2009-02-21 14:21:59 +00003300 status = pjmedia_aud_dev_get_info(i, &info[i]);
3301 if (status != PJ_SUCCESS)
3302 return status;
Benny Prijonoeebe9af2006-06-13 22:57:13 +00003303 }
3304
3305 *count = dev_count;
3306
3307 return PJ_SUCCESS;
3308}
Benny Prijonof798e502009-03-09 13:08:16 +00003309
3310
Benny Prijono10454dc2009-02-21 14:21:59 +00003311PJ_DEF(pj_status_t) pjsua_enum_snd_devs( pjmedia_snd_dev_info info[],
3312 unsigned *count)
3313{
3314 unsigned i, dev_count;
3315
3316 dev_count = pjmedia_aud_dev_count();
3317
3318 if (dev_count > *count) dev_count = *count;
3319 pj_bzero(info, dev_count * sizeof(pjmedia_snd_dev_info));
3320
3321 for (i=0; i<dev_count; ++i) {
3322 pjmedia_aud_dev_info ai;
3323 pj_status_t status;
3324
3325 status = pjmedia_aud_dev_get_info(i, &ai);
3326 if (status != PJ_SUCCESS)
3327 return status;
3328
3329 strncpy(info[i].name, ai.name, sizeof(info[i].name));
3330 info[i].name[sizeof(info[i].name)-1] = '\0';
3331 info[i].input_count = ai.input_count;
3332 info[i].output_count = ai.output_count;
3333 info[i].default_samples_per_sec = ai.default_samples_per_sec;
3334 }
3335
3336 *count = dev_count;
3337
3338 return PJ_SUCCESS;
3339}
Benny Prijono10454dc2009-02-21 14:21:59 +00003340
Benny Prijonof798e502009-03-09 13:08:16 +00003341/* Create audio device parameter to open the device */
3342static pj_status_t create_aud_param(pjmedia_aud_param *param,
3343 pjmedia_aud_dev_index capture_dev,
3344 pjmedia_aud_dev_index playback_dev,
3345 unsigned clock_rate,
3346 unsigned channel_count,
3347 unsigned samples_per_frame,
3348 unsigned bits_per_sample)
Benny Prijonoeebe9af2006-06-13 22:57:13 +00003349{
Nanang Izzuddin8465c682009-03-04 17:23:25 +00003350 pj_status_t status;
3351
Benny Prijono96e74f32009-02-22 12:00:12 +00003352 /* Normalize device ID with new convention about default device ID */
3353 if (playback_dev == PJMEDIA_AUD_DEFAULT_CAPTURE_DEV)
3354 playback_dev = PJMEDIA_AUD_DEFAULT_PLAYBACK_DEV;
3355
Benny Prijono10454dc2009-02-21 14:21:59 +00003356 /* Create default parameters for the device */
Benny Prijonof798e502009-03-09 13:08:16 +00003357 status = pjmedia_aud_dev_default_param(capture_dev, param);
Benny Prijono10454dc2009-02-21 14:21:59 +00003358 if (status != PJ_SUCCESS) {
Benny Prijono96e74f32009-02-22 12:00:12 +00003359 pjsua_perror(THIS_FILE, "Error retrieving default audio "
3360 "device parameters", status);
Benny Prijono10454dc2009-02-21 14:21:59 +00003361 return status;
3362 }
Benny Prijonof798e502009-03-09 13:08:16 +00003363 param->dir = PJMEDIA_DIR_CAPTURE_PLAYBACK;
3364 param->rec_id = capture_dev;
3365 param->play_id = playback_dev;
3366 param->clock_rate = clock_rate;
3367 param->channel_count = channel_count;
3368 param->samples_per_frame = samples_per_frame;
3369 param->bits_per_sample = bits_per_sample;
3370
3371 /* Update the setting with user preference */
3372#define update_param(cap, field) \
3373 if (pjsua_var.aud_param.flags & cap) { \
3374 param->flags |= cap; \
3375 param->field = pjsua_var.aud_param.field; \
3376 }
3377 update_param( PJMEDIA_AUD_DEV_CAP_INPUT_VOLUME_SETTING, input_vol);
3378 update_param( PJMEDIA_AUD_DEV_CAP_OUTPUT_VOLUME_SETTING, output_vol);
3379 update_param( PJMEDIA_AUD_DEV_CAP_INPUT_ROUTE, input_route);
3380 update_param( PJMEDIA_AUD_DEV_CAP_OUTPUT_ROUTE, output_route);
3381#undef update_param
3382
Benny Prijono10454dc2009-02-21 14:21:59 +00003383 /* Latency settings */
Benny Prijonof798e502009-03-09 13:08:16 +00003384 param->flags |= (PJMEDIA_AUD_DEV_CAP_INPUT_LATENCY |
3385 PJMEDIA_AUD_DEV_CAP_OUTPUT_LATENCY);
3386 param->input_latency_ms = pjsua_var.media_cfg.snd_rec_latency;
3387 param->output_latency_ms = pjsua_var.media_cfg.snd_play_latency;
3388
Benny Prijono10454dc2009-02-21 14:21:59 +00003389 /* EC settings */
3390 if (pjsua_var.media_cfg.ec_tail_len) {
Benny Prijonof798e502009-03-09 13:08:16 +00003391 param->flags |= (PJMEDIA_AUD_DEV_CAP_EC | PJMEDIA_AUD_DEV_CAP_EC_TAIL);
3392 param->ec_enabled = PJ_TRUE;
3393 param->ec_tail_ms = pjsua_var.media_cfg.ec_tail_len;
Benny Prijono10454dc2009-02-21 14:21:59 +00003394 } else {
Benny Prijonof798e502009-03-09 13:08:16 +00003395 param->flags &= ~(PJMEDIA_AUD_DEV_CAP_EC|PJMEDIA_AUD_DEV_CAP_EC_TAIL);
Benny Prijono10454dc2009-02-21 14:21:59 +00003396 }
3397
Benny Prijonof798e502009-03-09 13:08:16 +00003398 return PJ_SUCCESS;
3399}
Benny Prijono26056d82006-10-11 16:03:41 +00003400
Benny Prijonof798e502009-03-09 13:08:16 +00003401/* Internal: the first time the audio device is opened (during app
3402 * startup), retrieve the audio settings such as volume level
3403 * so that aud_get_settings() will work.
3404 */
3405static pj_status_t update_initial_aud_param()
3406{
3407 pjmedia_aud_stream *strm;
3408 pjmedia_aud_param param;
3409 pj_status_t status;
Benny Prijono26056d82006-10-11 16:03:41 +00003410
Benny Prijonof798e502009-03-09 13:08:16 +00003411 PJ_ASSERT_RETURN(pjsua_var.snd_port != NULL, PJ_EBUG);
Nanang Izzuddin3c1ae632008-08-21 15:04:20 +00003412
Benny Prijonof798e502009-03-09 13:08:16 +00003413 strm = pjmedia_snd_port_get_snd_stream(pjsua_var.snd_port);
Benny Prijono26056d82006-10-11 16:03:41 +00003414
Benny Prijonof798e502009-03-09 13:08:16 +00003415 status = pjmedia_aud_stream_get_param(strm, &param);
Benny Prijonoeebe9af2006-06-13 22:57:13 +00003416 if (status != PJ_SUCCESS) {
Benny Prijonof798e502009-03-09 13:08:16 +00003417 pjsua_perror(THIS_FILE, "Error audio stream "
3418 "device parameters", status);
Benny Prijonoeebe9af2006-06-13 22:57:13 +00003419 return status;
3420 }
3421
Benny Prijonof798e502009-03-09 13:08:16 +00003422#define update_saved_param(cap, field) \
3423 if (param.flags & cap) { \
3424 pjsua_var.aud_param.flags |= cap; \
3425 pjsua_var.aud_param.field = param.field; \
3426 }
3427
3428 update_saved_param(PJMEDIA_AUD_DEV_CAP_INPUT_VOLUME_SETTING, input_vol);
3429 update_saved_param(PJMEDIA_AUD_DEV_CAP_OUTPUT_VOLUME_SETTING, output_vol);
3430 update_saved_param(PJMEDIA_AUD_DEV_CAP_INPUT_ROUTE, input_route);
3431 update_saved_param(PJMEDIA_AUD_DEV_CAP_OUTPUT_ROUTE, output_route);
3432#undef update_saved_param
3433
3434 return PJ_SUCCESS;
3435}
3436
3437/* Get format name */
3438static const char *get_fmt_name(pj_uint32_t id)
3439{
3440 static char name[8];
3441
3442 if (id == PJMEDIA_FORMAT_L16)
3443 return "PCM";
3444 pj_memcpy(name, &id, 4);
3445 name[4] = '\0';
3446 return name;
3447}
3448
3449/* Open sound device with the setting. */
Sauw Ming98766c72011-03-11 06:57:24 +00003450static pj_status_t open_snd_dev(pjmedia_snd_port_param *param)
Benny Prijonof798e502009-03-09 13:08:16 +00003451{
3452 pjmedia_port *conf_port;
3453 pj_status_t status;
3454
3455 PJ_ASSERT_RETURN(param, PJ_EINVAL);
3456
3457 /* Check if NULL sound device is used */
Sauw Ming98766c72011-03-11 06:57:24 +00003458 if (NULL_SND_DEV_ID==param->base.rec_id ||
3459 NULL_SND_DEV_ID==param->base.play_id)
3460 {
Benny Prijonof798e502009-03-09 13:08:16 +00003461 return pjsua_set_null_snd_dev();
3462 }
3463
3464 /* Close existing sound port */
3465 close_snd_dev();
3466
Benny Prijono2d647722011-07-13 03:05:22 +00003467 /* Notify app */
3468 if (pjsua_var.ua_cfg.cb.on_snd_dev_operation) {
3469 (*pjsua_var.ua_cfg.cb.on_snd_dev_operation)(1);
3470 }
3471
Benny Prijonof798e502009-03-09 13:08:16 +00003472 /* Create memory pool for sound device. */
3473 pjsua_var.snd_pool = pjsua_pool_create("pjsua_snd", 4000, 4000);
3474 PJ_ASSERT_RETURN(pjsua_var.snd_pool, PJ_ENOMEM);
3475
3476
3477 PJ_LOG(4,(THIS_FILE, "Opening sound device %s@%d/%d/%dms",
Sauw Ming98766c72011-03-11 06:57:24 +00003478 get_fmt_name(param->base.ext_fmt.id),
3479 param->base.clock_rate, param->base.channel_count,
3480 param->base.samples_per_frame / param->base.channel_count *
3481 1000 / param->base.clock_rate));
Benny Prijonob90fd382011-09-18 14:59:56 +00003482 pj_log_push_indent();
Benny Prijonof798e502009-03-09 13:08:16 +00003483
3484 status = pjmedia_snd_port_create2( pjsua_var.snd_pool,
Sauw Ming98766c72011-03-11 06:57:24 +00003485 param, &pjsua_var.snd_port);
Benny Prijonof798e502009-03-09 13:08:16 +00003486 if (status != PJ_SUCCESS)
Benny Prijonob90fd382011-09-18 14:59:56 +00003487 goto on_error;
Benny Prijonof798e502009-03-09 13:08:16 +00003488
3489 /* Get the port0 of the conference bridge. */
3490 conf_port = pjmedia_conf_get_master_port(pjsua_var.mconf);
3491 pj_assert(conf_port != NULL);
3492
3493 /* For conference bridge, resample if necessary if the bridge's
3494 * clock rate is different than the sound device's clock rate.
3495 */
3496 if (!pjsua_var.is_mswitch &&
Sauw Ming98766c72011-03-11 06:57:24 +00003497 param->base.ext_fmt.id == PJMEDIA_FORMAT_PCM &&
Nanang Izzuddinfe68f1d2011-07-19 03:42:28 +00003498 PJMEDIA_PIA_SRATE(&conf_port->info) != param->base.clock_rate)
Benny Prijonof798e502009-03-09 13:08:16 +00003499 {
3500 pjmedia_port *resample_port;
3501 unsigned resample_opt = 0;
3502
3503 if (pjsua_var.media_cfg.quality >= 3 &&
3504 pjsua_var.media_cfg.quality <= 4)
3505 {
3506 resample_opt |= PJMEDIA_RESAMPLE_USE_SMALL_FILTER;
3507 }
3508 else if (pjsua_var.media_cfg.quality < 3) {
3509 resample_opt |= PJMEDIA_RESAMPLE_USE_LINEAR;
3510 }
3511
3512 status = pjmedia_resample_port_create(pjsua_var.snd_pool,
3513 conf_port,
Sauw Ming98766c72011-03-11 06:57:24 +00003514 param->base.clock_rate,
Benny Prijonof798e502009-03-09 13:08:16 +00003515 resample_opt,
3516 &resample_port);
3517 if (status != PJ_SUCCESS) {
3518 char errmsg[PJ_ERR_MSG_SIZE];
3519 pj_strerror(status, errmsg, sizeof(errmsg));
3520 PJ_LOG(4, (THIS_FILE,
3521 "Error creating resample port: %s",
3522 errmsg));
3523 close_snd_dev();
Benny Prijonob90fd382011-09-18 14:59:56 +00003524 goto on_error;
Benny Prijonof798e502009-03-09 13:08:16 +00003525 }
3526
3527 conf_port = resample_port;
3528 }
3529
3530 /* Otherwise for audio switchboard, the switch's port0 setting is
3531 * derived from the sound device setting, so update the setting.
3532 */
3533 if (pjsua_var.is_mswitch) {
Nanang Izzuddinfe68f1d2011-07-19 03:42:28 +00003534 pj_memcpy(&conf_port->info.fmt, &param->base.ext_fmt,
Benny Prijonoc45d9512010-12-10 11:04:30 +00003535 sizeof(conf_port->info.fmt));
Nanang Izzuddinfe68f1d2011-07-19 03:42:28 +00003536 conf_port->info.fmt.det.aud.clock_rate = param->base.clock_rate;
3537 conf_port->info.fmt.det.aud.frame_time_usec = param->base.samples_per_frame*
Benny Prijonoc45d9512010-12-10 11:04:30 +00003538 1000000 /
Nanang Izzuddinfe68f1d2011-07-19 03:42:28 +00003539 param->base.clock_rate;
3540 conf_port->info.fmt.det.aud.channel_count = param->base.channel_count;
Benny Prijonoc45d9512010-12-10 11:04:30 +00003541 conf_port->info.fmt.det.aud.bits_per_sample = 16;
Benny Prijonof798e502009-03-09 13:08:16 +00003542 }
3543
Benny Prijonoc45d9512010-12-10 11:04:30 +00003544
Benny Prijonof798e502009-03-09 13:08:16 +00003545 /* Connect sound port to the bridge */
Benny Prijono52a93912006-08-04 20:54:37 +00003546 status = pjmedia_snd_port_connect(pjsua_var.snd_port,
3547 conf_port );
3548 if (status != PJ_SUCCESS) {
3549 pjsua_perror(THIS_FILE, "Unable to connect conference port to "
3550 "sound device", status);
3551 pjmedia_snd_port_destroy(pjsua_var.snd_port);
3552 pjsua_var.snd_port = NULL;
Benny Prijonob90fd382011-09-18 14:59:56 +00003553 goto on_error;
Benny Prijono52a93912006-08-04 20:54:37 +00003554 }
3555
Benny Prijonoeebe9af2006-06-13 22:57:13 +00003556 /* Save the device IDs */
Sauw Ming98766c72011-03-11 06:57:24 +00003557 pjsua_var.cap_dev = param->base.rec_id;
3558 pjsua_var.play_dev = param->base.play_id;
Benny Prijonoeebe9af2006-06-13 22:57:13 +00003559
Benny Prijonoc53c6d72006-11-27 09:54:03 +00003560 /* Update sound device name. */
Benny Prijonof798e502009-03-09 13:08:16 +00003561 {
3562 pjmedia_aud_dev_info rec_info;
3563 pjmedia_aud_stream *strm;
3564 pjmedia_aud_param si;
3565 pj_str_t tmp;
Benny Prijonoc53c6d72006-11-27 09:54:03 +00003566
Benny Prijonof798e502009-03-09 13:08:16 +00003567 strm = pjmedia_snd_port_get_snd_stream(pjsua_var.snd_port);
3568 status = pjmedia_aud_stream_get_param(strm, &si);
3569 if (status == PJ_SUCCESS)
3570 status = pjmedia_aud_dev_get_info(si.rec_id, &rec_info);
Benny Prijonof3758ee2008-02-26 15:32:16 +00003571
Benny Prijonof798e502009-03-09 13:08:16 +00003572 if (status==PJ_SUCCESS) {
Sauw Ming98766c72011-03-11 06:57:24 +00003573 if (param->base.clock_rate != pjsua_var.media_cfg.clock_rate) {
Benny Prijonof798e502009-03-09 13:08:16 +00003574 char tmp_buf[128];
3575 int tmp_buf_len = sizeof(tmp_buf);
3576
3577 tmp_buf_len = pj_ansi_snprintf(tmp_buf, sizeof(tmp_buf)-1,
3578 "%s (%dKHz)",
3579 rec_info.name,
Sauw Ming98766c72011-03-11 06:57:24 +00003580 param->base.clock_rate/1000);
Benny Prijonof798e502009-03-09 13:08:16 +00003581 pj_strset(&tmp, tmp_buf, tmp_buf_len);
3582 pjmedia_conf_set_port0_name(pjsua_var.mconf, &tmp);
3583 } else {
3584 pjmedia_conf_set_port0_name(pjsua_var.mconf,
3585 pj_cstr(&tmp, rec_info.name));
3586 }
3587 }
3588
3589 /* Any error is not major, let it through */
3590 status = PJ_SUCCESS;
Benny Prijonob90fd382011-09-18 14:59:56 +00003591 }
Benny Prijonof798e502009-03-09 13:08:16 +00003592
3593 /* If this is the first time the audio device is open, retrieve some
3594 * settings from the device (such as volume settings) so that the
3595 * pjsua_snd_get_setting() work.
3596 */
3597 if (pjsua_var.aud_open_cnt == 0) {
3598 update_initial_aud_param();
3599 ++pjsua_var.aud_open_cnt;
Benny Prijonof3758ee2008-02-26 15:32:16 +00003600 }
Benny Prijonoc53c6d72006-11-27 09:54:03 +00003601
Benny Prijonob90fd382011-09-18 14:59:56 +00003602 pj_log_pop_indent();
Benny Prijonoeebe9af2006-06-13 22:57:13 +00003603 return PJ_SUCCESS;
Benny Prijonob90fd382011-09-18 14:59:56 +00003604
3605on_error:
3606 pj_log_pop_indent();
3607 return status;
Benny Prijonof798e502009-03-09 13:08:16 +00003608}
Nanang Izzuddin8465c682009-03-04 17:23:25 +00003609
Nanang Izzuddin8465c682009-03-04 17:23:25 +00003610
Benny Prijonof798e502009-03-09 13:08:16 +00003611/* Close existing sound device */
3612static void close_snd_dev(void)
3613{
Benny Prijonob90fd382011-09-18 14:59:56 +00003614 pj_log_push_indent();
3615
Benny Prijono2d647722011-07-13 03:05:22 +00003616 /* Notify app */
3617 if (pjsua_var.snd_is_on && pjsua_var.ua_cfg.cb.on_snd_dev_operation) {
3618 (*pjsua_var.ua_cfg.cb.on_snd_dev_operation)(0);
3619 }
3620
Benny Prijonof798e502009-03-09 13:08:16 +00003621 /* Close sound device */
3622 if (pjsua_var.snd_port) {
3623 pjmedia_aud_dev_info cap_info, play_info;
3624 pjmedia_aud_stream *strm;
3625 pjmedia_aud_param param;
3626
3627 strm = pjmedia_snd_port_get_snd_stream(pjsua_var.snd_port);
3628 pjmedia_aud_stream_get_param(strm, &param);
3629
3630 if (pjmedia_aud_dev_get_info(param.rec_id, &cap_info) != PJ_SUCCESS)
3631 cap_info.name[0] = '\0';
3632 if (pjmedia_aud_dev_get_info(param.play_id, &play_info) != PJ_SUCCESS)
3633 play_info.name[0] = '\0';
3634
3635 PJ_LOG(4,(THIS_FILE, "Closing %s sound playback device and "
3636 "%s sound capture device",
3637 play_info.name, cap_info.name));
3638
3639 pjmedia_snd_port_disconnect(pjsua_var.snd_port);
3640 pjmedia_snd_port_destroy(pjsua_var.snd_port);
3641 pjsua_var.snd_port = NULL;
3642 }
3643
3644 /* Close null sound device */
3645 if (pjsua_var.null_snd) {
3646 PJ_LOG(4,(THIS_FILE, "Closing null sound device.."));
3647 pjmedia_master_port_destroy(pjsua_var.null_snd, PJ_FALSE);
3648 pjsua_var.null_snd = NULL;
3649 }
3650
3651 if (pjsua_var.snd_pool)
3652 pj_pool_release(pjsua_var.snd_pool);
3653 pjsua_var.snd_pool = NULL;
Benny Prijono2d647722011-07-13 03:05:22 +00003654 pjsua_var.snd_is_on = PJ_FALSE;
Benny Prijonob90fd382011-09-18 14:59:56 +00003655
3656 pj_log_pop_indent();
Benny Prijonof798e502009-03-09 13:08:16 +00003657}
3658
3659
3660/*
3661 * Select or change sound device. Application may call this function at
3662 * any time to replace current sound device.
3663 */
3664PJ_DEF(pj_status_t) pjsua_set_snd_dev( int capture_dev,
3665 int playback_dev)
3666{
3667 unsigned alt_cr_cnt = 1;
3668 unsigned alt_cr[] = {0, 44100, 48000, 32000, 16000, 8000};
3669 unsigned i;
3670 pj_status_t status = -1;
3671
Benny Prijonob90fd382011-09-18 14:59:56 +00003672 PJ_LOG(4,(THIS_FILE, "Set sound device: capture=%d, playback=%d",
3673 capture_dev, playback_dev));
3674 pj_log_push_indent();
3675
Benny Prijono23ea21a2009-06-03 12:43:06 +00003676 /* Null-sound */
3677 if (capture_dev==NULL_SND_DEV_ID && playback_dev==NULL_SND_DEV_ID) {
Benny Prijonob90fd382011-09-18 14:59:56 +00003678 status = pjsua_set_null_snd_dev();
3679 pj_log_pop_indent();
3680 return status;
Benny Prijono23ea21a2009-06-03 12:43:06 +00003681 }
3682
Benny Prijonof798e502009-03-09 13:08:16 +00003683 /* Set default clock rate */
3684 alt_cr[0] = pjsua_var.media_cfg.snd_clock_rate;
3685 if (alt_cr[0] == 0)
3686 alt_cr[0] = pjsua_var.media_cfg.clock_rate;
3687
3688 /* Allow retrying of different clock rate if we're using conference
3689 * bridge (meaning audio format is always PCM), otherwise lock on
3690 * to one clock rate.
3691 */
3692 if (pjsua_var.is_mswitch) {
3693 alt_cr_cnt = 1;
3694 } else {
3695 alt_cr_cnt = PJ_ARRAY_SIZE(alt_cr);
3696 }
3697
3698 /* Attempts to open the sound device with different clock rates */
3699 for (i=0; i<alt_cr_cnt; ++i) {
Sauw Ming98766c72011-03-11 06:57:24 +00003700 pjmedia_snd_port_param param;
Benny Prijonof798e502009-03-09 13:08:16 +00003701 unsigned samples_per_frame;
3702
3703 /* Create the default audio param */
3704 samples_per_frame = alt_cr[i] *
3705 pjsua_var.media_cfg.audio_frame_ptime *
3706 pjsua_var.media_cfg.channel_count / 1000;
Sauw Ming98766c72011-03-11 06:57:24 +00003707 status = create_aud_param(&param.base, capture_dev, playback_dev,
Benny Prijonof798e502009-03-09 13:08:16 +00003708 alt_cr[i], pjsua_var.media_cfg.channel_count,
3709 samples_per_frame, 16);
3710 if (status != PJ_SUCCESS)
Benny Prijonob90fd382011-09-18 14:59:56 +00003711 goto on_error;
Benny Prijonof798e502009-03-09 13:08:16 +00003712
3713 /* Open! */
Sauw Ming98766c72011-03-11 06:57:24 +00003714 param.options = 0;
Benny Prijonof798e502009-03-09 13:08:16 +00003715 status = open_snd_dev(&param);
3716 if (status == PJ_SUCCESS)
3717 break;
3718 }
3719
3720 if (status != PJ_SUCCESS) {
3721 pjsua_perror(THIS_FILE, "Unable to open sound device", status);
Benny Prijonob90fd382011-09-18 14:59:56 +00003722 goto on_error;
Benny Prijonof798e502009-03-09 13:08:16 +00003723 }
3724
Benny Prijonoe25fe6f2009-07-16 17:52:08 +00003725 pjsua_var.no_snd = PJ_FALSE;
Benny Prijono2d647722011-07-13 03:05:22 +00003726 pjsua_var.snd_is_on = PJ_TRUE;
Benny Prijonoe25fe6f2009-07-16 17:52:08 +00003727
Benny Prijonob90fd382011-09-18 14:59:56 +00003728 pj_log_pop_indent();
Benny Prijonof798e502009-03-09 13:08:16 +00003729 return PJ_SUCCESS;
Benny Prijonob90fd382011-09-18 14:59:56 +00003730
3731on_error:
3732 pj_log_pop_indent();
3733 return status;
Benny Prijonoeebe9af2006-06-13 22:57:13 +00003734}
3735
3736
3737/*
Benny Prijonoebdf8772007-02-01 19:25:50 +00003738 * Get currently active sound devices. If sound devices has not been created
3739 * (for example when pjsua_start() is not called), it is possible that
3740 * the function returns PJ_SUCCESS with -1 as device IDs.
3741 */
3742PJ_DEF(pj_status_t) pjsua_get_snd_dev(int *capture_dev,
3743 int *playback_dev)
3744{
3745 if (capture_dev) {
3746 *capture_dev = pjsua_var.cap_dev;
3747 }
3748 if (playback_dev) {
3749 *playback_dev = pjsua_var.play_dev;
3750 }
3751
3752 return PJ_SUCCESS;
3753}
3754
3755
3756/*
Benny Prijonoeebe9af2006-06-13 22:57:13 +00003757 * Use null sound device.
3758 */
3759PJ_DEF(pj_status_t) pjsua_set_null_snd_dev(void)
3760{
3761 pjmedia_port *conf_port;
3762 pj_status_t status;
3763
Benny Prijonob90fd382011-09-18 14:59:56 +00003764 PJ_LOG(4,(THIS_FILE, "Setting null sound device.."));
3765 pj_log_push_indent();
3766
3767
Benny Prijonoeebe9af2006-06-13 22:57:13 +00003768 /* Close existing sound device */
3769 close_snd_dev();
3770
Benny Prijono2d647722011-07-13 03:05:22 +00003771 /* Notify app */
3772 if (pjsua_var.ua_cfg.cb.on_snd_dev_operation) {
3773 (*pjsua_var.ua_cfg.cb.on_snd_dev_operation)(1);
3774 }
3775
Nanang Izzuddin68559c32008-06-13 17:01:46 +00003776 /* Create memory pool for sound device. */
3777 pjsua_var.snd_pool = pjsua_pool_create("pjsua_snd", 4000, 4000);
3778 PJ_ASSERT_RETURN(pjsua_var.snd_pool, PJ_ENOMEM);
3779
Benny Prijonoeebe9af2006-06-13 22:57:13 +00003780 PJ_LOG(4,(THIS_FILE, "Opening null sound device.."));
3781
3782 /* Get the port0 of the conference bridge. */
3783 conf_port = pjmedia_conf_get_master_port(pjsua_var.mconf);
3784 pj_assert(conf_port != NULL);
3785
3786 /* Create master port, connecting port0 of the conference bridge to
3787 * a null port.
3788 */
Nanang Izzuddin68559c32008-06-13 17:01:46 +00003789 status = pjmedia_master_port_create(pjsua_var.snd_pool, pjsua_var.null_port,
Benny Prijonoeebe9af2006-06-13 22:57:13 +00003790 conf_port, 0, &pjsua_var.null_snd);
3791 if (status != PJ_SUCCESS) {
3792 pjsua_perror(THIS_FILE, "Unable to create null sound device",
3793 status);
Benny Prijonob90fd382011-09-18 14:59:56 +00003794 pj_log_pop_indent();
Benny Prijonoeebe9af2006-06-13 22:57:13 +00003795 return status;
3796 }
3797
3798 /* Start the master port */
3799 status = pjmedia_master_port_start(pjsua_var.null_snd);
3800 PJ_ASSERT_RETURN(status == PJ_SUCCESS, status);
3801
Nanang Izzuddin68559c32008-06-13 17:01:46 +00003802 pjsua_var.cap_dev = NULL_SND_DEV_ID;
3803 pjsua_var.play_dev = NULL_SND_DEV_ID;
3804
Benny Prijonoe25fe6f2009-07-16 17:52:08 +00003805 pjsua_var.no_snd = PJ_FALSE;
Benny Prijono2d647722011-07-13 03:05:22 +00003806 pjsua_var.snd_is_on = PJ_TRUE;
Benny Prijonoe25fe6f2009-07-16 17:52:08 +00003807
Benny Prijonob90fd382011-09-18 14:59:56 +00003808 pj_log_pop_indent();
Benny Prijonoeebe9af2006-06-13 22:57:13 +00003809 return PJ_SUCCESS;
3810}
3811
3812
Benny Prijonoe909eac2006-07-27 22:04:56 +00003813
3814/*
3815 * Use no device!
3816 */
3817PJ_DEF(pjmedia_port*) pjsua_set_no_snd_dev(void)
3818{
3819 /* Close existing sound device */
3820 close_snd_dev();
3821
3822 pjsua_var.no_snd = PJ_TRUE;
3823 return pjmedia_conf_get_master_port(pjsua_var.mconf);
3824}
3825
3826
Benny Prijonof20687a2006-08-04 18:27:19 +00003827/*
3828 * Configure the AEC settings of the sound port.
3829 */
Benny Prijono5da50432006-08-07 10:24:52 +00003830PJ_DEF(pj_status_t) pjsua_set_ec(unsigned tail_ms, unsigned options)
Benny Prijonof20687a2006-08-04 18:27:19 +00003831{
3832 pjsua_var.media_cfg.ec_tail_len = tail_ms;
3833
3834 if (pjsua_var.snd_port)
Benny Prijono5da50432006-08-07 10:24:52 +00003835 return pjmedia_snd_port_set_ec( pjsua_var.snd_port, pjsua_var.pool,
3836 tail_ms, options);
Benny Prijonof20687a2006-08-04 18:27:19 +00003837
3838 return PJ_SUCCESS;
3839}
3840
3841
3842/*
3843 * Get current AEC tail length.
3844 */
Benny Prijono22dfe592006-08-06 12:07:13 +00003845PJ_DEF(pj_status_t) pjsua_get_ec_tail(unsigned *p_tail_ms)
Benny Prijonof20687a2006-08-04 18:27:19 +00003846{
3847 *p_tail_ms = pjsua_var.media_cfg.ec_tail_len;
3848 return PJ_SUCCESS;
3849}
3850
Nanang Izzuddinfe02a062009-02-18 14:28:49 +00003851
Nanang Izzuddin0cb3b022009-02-27 17:37:35 +00003852/*
Benny Prijonof798e502009-03-09 13:08:16 +00003853 * Check whether the sound device is currently active.
Nanang Izzuddin0cb3b022009-02-27 17:37:35 +00003854 */
Benny Prijonof798e502009-03-09 13:08:16 +00003855PJ_DEF(pj_bool_t) pjsua_snd_is_active(void)
Nanang Izzuddin0cb3b022009-02-27 17:37:35 +00003856{
Benny Prijonof798e502009-03-09 13:08:16 +00003857 return pjsua_var.snd_port != NULL;
3858}
Nanang Izzuddin0cb3b022009-02-27 17:37:35 +00003859
Nanang Izzuddin0cb3b022009-02-27 17:37:35 +00003860
Benny Prijonof798e502009-03-09 13:08:16 +00003861/*
3862 * Configure sound device setting to the sound device being used.
3863 */
3864PJ_DEF(pj_status_t) pjsua_snd_set_setting( pjmedia_aud_dev_cap cap,
3865 const void *pval,
3866 pj_bool_t keep)
3867{
Benny Prijono09b0ff62009-03-10 12:07:51 +00003868 pj_status_t status;
3869
Benny Prijonof798e502009-03-09 13:08:16 +00003870 /* Check if we are allowed to set the cap */
Benny Prijono09b0ff62009-03-10 12:07:51 +00003871 if ((cap & pjsua_var.aud_svmask) == 0) {
Benny Prijonof798e502009-03-09 13:08:16 +00003872 return PJMEDIA_EAUD_INVCAP;
3873 }
3874
Benny Prijono09b0ff62009-03-10 12:07:51 +00003875 /* If sound is active, set it immediately */
Benny Prijonof798e502009-03-09 13:08:16 +00003876 if (pjsua_snd_is_active()) {
Benny Prijonof798e502009-03-09 13:08:16 +00003877 pjmedia_aud_stream *strm;
3878
3879 strm = pjmedia_snd_port_get_snd_stream(pjsua_var.snd_port);
Benny Prijono09b0ff62009-03-10 12:07:51 +00003880 status = pjmedia_aud_stream_set_cap(strm, cap, pval);
Benny Prijonof798e502009-03-09 13:08:16 +00003881 } else {
Benny Prijono09b0ff62009-03-10 12:07:51 +00003882 status = PJ_SUCCESS;
Benny Prijonof798e502009-03-09 13:08:16 +00003883 }
Benny Prijono09b0ff62009-03-10 12:07:51 +00003884
3885 if (status != PJ_SUCCESS)
3886 return status;
3887
3888 /* Save in internal param for later device open */
3889 if (keep) {
3890 status = pjmedia_aud_param_set_cap(&pjsua_var.aud_param,
3891 cap, pval);
3892 }
3893
3894 return status;
Benny Prijonof798e502009-03-09 13:08:16 +00003895}
3896
3897/*
3898 * Retrieve a sound device setting.
3899 */
3900PJ_DEF(pj_status_t) pjsua_snd_get_setting( pjmedia_aud_dev_cap cap,
3901 void *pval)
3902{
3903 /* If sound device has never been opened before, open it to
3904 * retrieve the initial setting from the device (e.g. audio
3905 * volume)
3906 */
Benny Prijono09b0ff62009-03-10 12:07:51 +00003907 if (pjsua_var.aud_open_cnt==0) {
3908 PJ_LOG(4,(THIS_FILE, "Opening sound device to get initial settings"));
Benny Prijonof798e502009-03-09 13:08:16 +00003909 pjsua_set_snd_dev(pjsua_var.cap_dev, pjsua_var.play_dev);
Benny Prijono09b0ff62009-03-10 12:07:51 +00003910 close_snd_dev();
3911 }
Benny Prijonof798e502009-03-09 13:08:16 +00003912
3913 if (pjsua_snd_is_active()) {
3914 /* Sound is active, retrieve from device directly */
3915 pjmedia_aud_stream *strm;
3916
3917 strm = pjmedia_snd_port_get_snd_stream(pjsua_var.snd_port);
3918 return pjmedia_aud_stream_get_cap(strm, cap, pval);
3919 } else {
3920 /* Otherwise retrieve from internal param */
3921 return pjmedia_aud_param_get_cap(&pjsua_var.aud_param,
3922 cap, pval);
3923 }
Nanang Izzuddin0cb3b022009-02-27 17:37:35 +00003924}
3925
3926
Benny Prijonoeebe9af2006-06-13 22:57:13 +00003927/*****************************************************************************
3928 * Codecs.
3929 */
3930
3931/*
3932 * Enum all supported codecs in the system.
3933 */
3934PJ_DEF(pj_status_t) pjsua_enum_codecs( pjsua_codec_info id[],
3935 unsigned *p_count )
3936{
3937 pjmedia_codec_mgr *codec_mgr;
3938 pjmedia_codec_info info[32];
3939 unsigned i, count, prio[32];
3940 pj_status_t status;
3941
3942 codec_mgr = pjmedia_endpt_get_codec_mgr(pjsua_var.med_endpt);
3943 count = PJ_ARRAY_SIZE(info);
3944 status = pjmedia_codec_mgr_enum_codecs( codec_mgr, &count, info, prio);
3945 if (status != PJ_SUCCESS) {
3946 *p_count = 0;
3947 return status;
3948 }
3949
3950 if (count > *p_count) count = *p_count;
3951
3952 for (i=0; i<count; ++i) {
Nanang Izzuddin56b2ce42011-04-06 13:55:01 +00003953 pj_bzero(&id[i], sizeof(pjsua_codec_info));
3954
Benny Prijonoeebe9af2006-06-13 22:57:13 +00003955 pjmedia_codec_info_to_id(&info[i], id[i].buf_, sizeof(id[i].buf_));
3956 id[i].codec_id = pj_str(id[i].buf_);
3957 id[i].priority = (pj_uint8_t) prio[i];
3958 }
3959
3960 *p_count = count;
3961
3962 return PJ_SUCCESS;
3963}
3964
3965
3966/*
3967 * Change codec priority.
3968 */
3969PJ_DEF(pj_status_t) pjsua_codec_set_priority( const pj_str_t *codec_id,
3970 pj_uint8_t priority )
3971{
Benny Prijono88accae2008-06-26 15:48:14 +00003972 const pj_str_t all = { NULL, 0 };
Benny Prijonoeebe9af2006-06-13 22:57:13 +00003973 pjmedia_codec_mgr *codec_mgr;
3974
3975 codec_mgr = pjmedia_endpt_get_codec_mgr(pjsua_var.med_endpt);
3976
Benny Prijono88accae2008-06-26 15:48:14 +00003977 if (codec_id->slen==1 && *codec_id->ptr=='*')
3978 codec_id = &all;
3979
Benny Prijonoeebe9af2006-06-13 22:57:13 +00003980 return pjmedia_codec_mgr_set_codec_priority(codec_mgr, codec_id,
3981 priority);
3982}
3983
3984
3985/*
3986 * Get codec parameters.
3987 */
3988PJ_DEF(pj_status_t) pjsua_codec_get_param( const pj_str_t *codec_id,
3989 pjmedia_codec_param *param )
3990{
Benny Prijono88accae2008-06-26 15:48:14 +00003991 const pj_str_t all = { NULL, 0 };
Benny Prijonoeebe9af2006-06-13 22:57:13 +00003992 const pjmedia_codec_info *info;
3993 pjmedia_codec_mgr *codec_mgr;
3994 unsigned count = 1;
3995 pj_status_t status;
3996
3997 codec_mgr = pjmedia_endpt_get_codec_mgr(pjsua_var.med_endpt);
3998
Benny Prijono88accae2008-06-26 15:48:14 +00003999 if (codec_id->slen==1 && *codec_id->ptr=='*')
4000 codec_id = &all;
4001
Benny Prijonoeebe9af2006-06-13 22:57:13 +00004002 status = pjmedia_codec_mgr_find_codecs_by_id(codec_mgr, codec_id,
4003 &count, &info, NULL);
4004 if (status != PJ_SUCCESS)
4005 return status;
4006
4007 if (count != 1)
Nanang Izzuddin50fae732011-03-22 09:49:23 +00004008 return (count > 1? PJ_ETOOMANY : PJ_ENOTFOUND);
Benny Prijonoeebe9af2006-06-13 22:57:13 +00004009
4010 status = pjmedia_codec_mgr_get_default_param( codec_mgr, info, param);
4011 return status;
4012}
4013
4014
4015/*
4016 * Set codec parameters.
4017 */
Nanang Izzuddin06839e72010-01-27 11:48:31 +00004018PJ_DEF(pj_status_t) pjsua_codec_set_param( const pj_str_t *codec_id,
Benny Prijonoeebe9af2006-06-13 22:57:13 +00004019 const pjmedia_codec_param *param)
4020{
Nanang Izzuddin06839e72010-01-27 11:48:31 +00004021 const pjmedia_codec_info *info[2];
4022 pjmedia_codec_mgr *codec_mgr;
4023 unsigned count = 2;
4024 pj_status_t status;
4025
4026 codec_mgr = pjmedia_endpt_get_codec_mgr(pjsua_var.med_endpt);
4027
4028 status = pjmedia_codec_mgr_find_codecs_by_id(codec_mgr, codec_id,
4029 &count, info, NULL);
4030 if (status != PJ_SUCCESS)
4031 return status;
4032
4033 /* Codec ID should be specific, except for G.722.1 */
4034 if (count > 1 &&
4035 pj_strnicmp2(codec_id, "G7221/16", 8) != 0 &&
4036 pj_strnicmp2(codec_id, "G7221/32", 8) != 0)
4037 {
4038 pj_assert(!"Codec ID is not specific");
4039 return PJ_ETOOMANY;
4040 }
4041
4042 status = pjmedia_codec_mgr_set_default_param(codec_mgr, info[0], param);
4043 return status;
Benny Prijonoeebe9af2006-06-13 22:57:13 +00004044}
Nanang Izzuddin50fae732011-03-22 09:49:23 +00004045
4046