blob: 7cd133a4714a1e2a0f88bdd4b76753e616afe79a [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 Prijono0bc99a92011-03-17 04:34:43 +00001342on_error:
Sauw Ming73ecfe82011-09-21 10:20:01 +00001343 if (status != PJ_SUCCESS && call_med->tp) {
Benny Prijono0bc99a92011-03-17 04:34:43 +00001344 pjmedia_transport_close(call_med->tp);
1345 call_med->tp = NULL;
1346 }
Sauw Ming73ecfe82011-09-21 10:20:01 +00001347
1348 if (sip_err_code)
1349 *sip_err_code = err_code;
1350
1351 if (call_med->med_init_cb) {
1352 pjsua_med_tp_state_info info;
1353
1354 pj_bzero(&info, sizeof(info));
1355 info.status = status;
1356 info.state = call_med->tp_st;
1357 info.med_idx = call_med->idx;
1358 info.sip_err_code = err_code;
1359 (*call_med->med_init_cb)(call_med->call->index, &info);
1360 }
1361
1362 return status;
1363}
1364
1365/* Initialize the media line */
1366pj_status_t pjsua_call_media_init(pjsua_call_media *call_med,
1367 pjmedia_type type,
1368 const pjsua_transport_config *tcfg,
1369 int security_level,
1370 int *sip_err_code,
1371 pj_bool_t async,
1372 pjsua_med_tp_state_cb cb)
1373{
1374 pjsua_acc *acc = &pjsua_var.acc[call_med->call->acc_id];
1375 pj_status_t status = PJ_SUCCESS;
1376
1377 /*
1378 * Note: this function may be called when the media already exists
1379 * (e.g. in reinvites, updates, etc.)
1380 */
1381 call_med->type = type;
1382
1383 /* Create the media transport for initial call. */
1384 if (call_med->tp == NULL) {
1385#if defined(PJMEDIA_HAS_VIDEO) && (PJMEDIA_HAS_VIDEO != 0)
1386 /* While in initial call, set default video devices */
1387 if (type == PJMEDIA_TYPE_VIDEO) {
1388 call_med->strm.v.rdr_dev = acc->cfg.vid_rend_dev;
1389 call_med->strm.v.cap_dev = acc->cfg.vid_cap_dev;
1390 if (call_med->strm.v.rdr_dev == PJMEDIA_VID_DEFAULT_RENDER_DEV) {
1391 pjmedia_vid_dev_info info;
1392 pjmedia_vid_dev_get_info(call_med->strm.v.rdr_dev, &info);
1393 call_med->strm.v.rdr_dev = info.id;
1394 }
1395 if (call_med->strm.v.cap_dev == PJMEDIA_VID_DEFAULT_CAPTURE_DEV) {
1396 pjmedia_vid_dev_info info;
1397 pjmedia_vid_dev_get_info(call_med->strm.v.cap_dev, &info);
1398 call_med->strm.v.cap_dev = info.id;
1399 }
Nanang Izzuddinc12a19d2011-10-03 05:23:59 +00001400
1401 /* Init event subscribtion */
1402 pjmedia_event_subscription_init(&call_med->esub_rend, &call_media_on_event,
1403 call_med);
1404 pjmedia_event_subscription_init(&call_med->esub_cap, &call_media_on_event,
1405 call_med);
Sauw Ming73ecfe82011-09-21 10:20:01 +00001406 }
1407#endif
1408
1409 set_media_tp_state(call_med, PJSUA_MED_TP_CREATING);
1410
1411 if (async) {
1412 call_med->med_create_cb = &call_media_init_cb;
1413 call_med->med_init_cb = cb;
1414 }
1415
1416 if (pjsua_var.media_cfg.enable_ice) {
1417 status = create_ice_media_transport(tcfg, call_med, async);
Sauw Ming848742f2011-09-28 04:20:30 +00001418 if (async && status == PJ_SUCCESS) {
1419 /* Callback has been called. */
1420 call_med->med_init_cb = NULL;
1421 /* We cannot return PJ_SUCCESS here since we already call
1422 * the callback.
1423 */
1424 return PJ_EPENDING;
1425 } else if (async && status == PJ_EPENDING) {
1426 /* We will resume call media initialization in the
1427 * on_ice_complete() callback.
1428 */
1429 return PJ_EPENDING;
1430 }
Sauw Ming73ecfe82011-09-21 10:20:01 +00001431 } else {
1432 status = create_udp_media_transport(tcfg, call_med);
1433 }
1434
Sauw Ming848742f2011-09-28 04:20:30 +00001435 if (status != PJ_SUCCESS) {
Sauw Ming73ecfe82011-09-21 10:20:01 +00001436 PJ_PERROR(1,(THIS_FILE, status, "Error creating media transport"));
1437 return status;
1438 }
1439
1440 /* Media transport creation completed immediately, so
1441 * we don't need to call the callback.
1442 */
1443 call_med->med_init_cb = NULL;
1444
1445 } else if (call_med->tp_st == PJSUA_MED_TP_DISABLED) {
1446 /* Media is being reenabled. */
1447 set_media_tp_state(call_med, PJSUA_MED_TP_INIT);
1448 }
1449
1450 return call_media_init_cb(call_med, status, security_level,
1451 sip_err_code);
1452}
1453
1454/* Callback to resume pjsua_media_channel_init() after media transport
1455 * initialization is completed.
1456 */
1457static pj_status_t media_channel_init_cb(pjsua_call_id call_id,
1458 const pjsua_med_tp_state_info *info)
1459{
1460 pjsua_call *call = &pjsua_var.calls[call_id];
1461 pj_status_t status = (info? info->status : PJ_SUCCESS);
1462 unsigned mi;
1463
1464 if (info) {
1465 pj_mutex_lock(call->med_ch_mutex);
1466
1467 /* Set the callback to NULL to indicate that the async operation
1468 * has completed.
1469 */
1470 call->media[info->med_idx].med_init_cb = NULL;
1471
1472 /* In case of failure, save the information to be returned
1473 * by the last media transport to finish.
1474 */
1475 if (info->status != PJ_SUCCESS)
1476 pj_memcpy(&call->med_ch_info, info, sizeof(info));
1477
1478 /* Check whether all the call's medias have finished calling their
1479 * callbacks.
1480 */
1481 for (mi=0; mi < call->med_cnt; ++mi) {
1482 pjsua_call_media *call_med = &call->media[mi];
1483
1484 if (call_med->med_init_cb) {
1485 pj_mutex_unlock(call->med_ch_mutex);
1486 return PJ_SUCCESS;
1487 }
1488
1489 if (call_med->tp_ready != PJ_SUCCESS)
1490 status = call_med->tp_ready;
1491 }
1492
1493 /* OK, we are called by the last media transport finished. */
1494 pj_mutex_unlock(call->med_ch_mutex);
1495 }
1496
1497 if (call->med_ch_mutex) {
1498 pj_mutex_destroy(call->med_ch_mutex);
1499 call->med_ch_mutex = NULL;
1500 }
1501
1502 if (status != PJ_SUCCESS) {
1503 pjsua_media_channel_deinit(call_id);
1504 goto on_error;
1505 }
1506
1507 /* Tell the media transport of a new offer/answer session */
1508 for (mi=0; mi < call->med_cnt; ++mi) {
1509 pjsua_call_media *call_med = &call->media[mi];
1510
1511 /* Note: tp may be NULL if this media line is disabled */
1512 if (call_med->tp && call_med->tp_st == PJSUA_MED_TP_IDLE) {
Sauw Ming99cc8ff2011-09-22 04:24:56 +00001513 pj_pool_t *tmp_pool = call->async_call.pool_prov;
1514
1515 if (!tmp_pool) {
1516 tmp_pool = (call->inv? call->inv->pool_prov:
1517 call->async_call.dlg->pool);
1518 }
Sauw Ming73ecfe82011-09-21 10:20:01 +00001519
1520 status = pjmedia_transport_media_create(
1521 call_med->tp, tmp_pool,
1522 0, call->async_call.rem_sdp, mi);
1523 if (status != PJ_SUCCESS) {
1524 call->med_ch_info.status = status;
1525 call->med_ch_info.med_idx = mi;
1526 call->med_ch_info.state = call_med->tp_st;
1527 call->med_ch_info.sip_err_code = PJSIP_SC_NOT_ACCEPTABLE;
1528 pjsua_media_channel_deinit(call_id);
1529 goto on_error;
1530 }
1531
1532 set_media_tp_state(call_med, PJSUA_MED_TP_INIT);
1533 }
1534 }
1535
1536 call->med_ch_info.status = PJ_SUCCESS;
1537
1538on_error:
1539 if (call->med_ch_cb)
1540 (*call->med_ch_cb)(call->index, &call->med_ch_info);
1541
Benny Prijono0bc99a92011-03-17 04:34:43 +00001542 return status;
1543}
Nanang Izzuddin1e952a82010-10-05 16:32:04 +00001544
Benny Prijonod8179652008-01-23 20:39:07 +00001545pj_status_t pjsua_media_channel_init(pjsua_call_id call_id,
1546 pjsip_role_e role,
1547 int security_level,
1548 pj_pool_t *tmp_pool,
1549 const pjmedia_sdp_session *rem_sdp,
Sauw Ming73ecfe82011-09-21 10:20:01 +00001550 int *sip_err_code,
1551 pj_bool_t async,
1552 pjsua_med_tp_state_cb cb)
Benny Prijonoc97608e2007-03-23 16:34:20 +00001553{
Benny Prijono0bc99a92011-03-17 04:34:43 +00001554 const pj_str_t STR_AUDIO = { "audio", 5 };
1555 const pj_str_t STR_VIDEO = { "video", 5 };
Benny Prijonod8179652008-01-23 20:39:07 +00001556 pjsua_call *call = &pjsua_var.calls[call_id];
Benny Prijonod8179652008-01-23 20:39:07 +00001557 pjsua_acc *acc = &pjsua_var.acc[call->acc_id];
Benny Prijono0bc99a92011-03-17 04:34:43 +00001558 pj_uint8_t maudidx[PJSUA_MAX_CALL_MEDIA];
1559 unsigned maudcnt = PJ_ARRAY_SIZE(maudidx);
1560 pj_uint8_t mvididx[PJSUA_MAX_CALL_MEDIA];
1561 unsigned mvidcnt = PJ_ARRAY_SIZE(mvididx);
1562 pjmedia_type media_types[PJSUA_MAX_CALL_MEDIA];
1563 unsigned mi;
Sauw Ming73ecfe82011-09-21 10:20:01 +00001564 pj_bool_t pending_med_tp = PJ_FALSE;
Benny Prijono0bc99a92011-03-17 04:34:43 +00001565 pj_status_t status;
Benny Prijonoc97608e2007-03-23 16:34:20 +00001566
Benny Prijonod8179652008-01-23 20:39:07 +00001567 PJ_UNUSED_ARG(role);
1568
Nanang Izzuddinb6c239c2011-05-10 05:42:28 +00001569 /*
1570 * Note: this function may be called when the media already exists
1571 * (e.g. in reinvites, updates, etc).
1572 */
1573
Benny Prijono0bc99a92011-03-17 04:34:43 +00001574 if (pjsua_get_state() != PJSUA_STATE_RUNNING)
1575 return PJ_EBUSY;
1576
Sauw Ming73ecfe82011-09-21 10:20:01 +00001577 if (async) {
1578 pj_pool_t *tmppool = (call->inv? call->inv->pool_prov:
1579 call->async_call.dlg->pool);
1580
1581 status = pj_mutex_create_simple(tmppool, NULL, &call->med_ch_mutex);
1582 if (status != PJ_SUCCESS)
1583 return status;
1584 }
1585
Benny Prijonob90fd382011-09-18 14:59:56 +00001586 PJ_LOG(4,(THIS_FILE, "Call %d: initializing media..", call_id));
1587 pj_log_push_indent();
1588
Benny Prijono0bc99a92011-03-17 04:34:43 +00001589#if DISABLED_FOR_TICKET_1185
Benny Prijonod8179652008-01-23 20:39:07 +00001590 /* Return error if media transport has not been created yet
Benny Prijono68f9e4f2008-03-21 08:56:02 +00001591 * (e.g. application is starting)
1592 */
Benny Prijono0bc99a92011-03-17 04:34:43 +00001593 for (i=0; i<call->med_cnt; ++i) {
1594 if (call->media[i].tp == NULL) {
Benny Prijonob90fd382011-09-18 14:59:56 +00001595 status = PJ_EBUSY;
1596 goto on_error;
Benny Prijonod8179652008-01-23 20:39:07 +00001597 }
Benny Prijonoc97608e2007-03-23 16:34:20 +00001598 }
Benny Prijonod8179652008-01-23 20:39:07 +00001599#endif
Benny Prijonoc97608e2007-03-23 16:34:20 +00001600
Benny Prijono0bc99a92011-03-17 04:34:43 +00001601 if (rem_sdp) {
1602 sort_media(rem_sdp, &STR_AUDIO, acc->cfg.use_srtp,
1603 maudidx, &maudcnt);
Nanang Izzuddind8c33d82011-08-18 18:30:55 +00001604 // Don't apply media count limitation until SDP negotiation is done.
1605 //if (maudcnt > acc->cfg.max_audio_cnt)
1606 // maudcnt = acc->cfg.max_audio_cnt;
Benny Prijonoc97608e2007-03-23 16:34:20 +00001607
Benny Prijono0bc99a92011-03-17 04:34:43 +00001608 if (maudcnt==0) {
1609 /* Expecting audio in the offer */
1610 if (sip_err_code) *sip_err_code = PJSIP_SC_NOT_ACCEPTABLE_HERE;
1611 pjsua_media_channel_deinit(call_id);
Benny Prijonob90fd382011-09-18 14:59:56 +00001612 status = PJSIP_ERRNO_FROM_SIP_STATUS(PJSIP_SC_NOT_ACCEPTABLE_HERE);
1613 goto on_error;
Benny Prijonoc97608e2007-03-23 16:34:20 +00001614 }
1615
Nanang Izzuddind8c33d82011-08-18 18:30:55 +00001616#if PJMEDIA_HAS_VIDEO
Benny Prijono0bc99a92011-03-17 04:34:43 +00001617 sort_media(rem_sdp, &STR_VIDEO, acc->cfg.use_srtp,
1618 mvididx, &mvidcnt);
Nanang Izzuddind8c33d82011-08-18 18:30:55 +00001619 // Don't apply media count limitation until SDP negotiation is done.
1620 //if (mvidcnt > acc->cfg.max_video_cnt)
1621 //mvidcnt = acc->cfg.max_video_cnt;
1622#else
1623 mvidcnt = 0;
1624#endif
Nanang Izzuddinb6c239c2011-05-10 05:42:28 +00001625
1626 /* Update media count only when remote add any media, this media count
1627 * must never decrease.
Benny Prijonod8179652008-01-23 20:39:07 +00001628 */
Nanang Izzuddinb6c239c2011-05-10 05:42:28 +00001629 if (call->med_cnt < rem_sdp->media_count)
1630 call->med_cnt = PJ_MIN(rem_sdp->media_count, PJSUA_MAX_CALL_MEDIA);
Benny Prijonod8179652008-01-23 20:39:07 +00001631
Benny Prijono0bc99a92011-03-17 04:34:43 +00001632 } else {
1633 maudcnt = acc->cfg.max_audio_cnt;
1634 for (mi=0; mi<maudcnt; ++mi) {
Nanang Izzuddinbf26db12011-03-18 07:54:50 +00001635 maudidx[mi] = (pj_uint8_t)mi;
Benny Prijono0bc99a92011-03-17 04:34:43 +00001636 media_types[mi] = PJMEDIA_TYPE_AUDIO;
Benny Prijonod8179652008-01-23 20:39:07 +00001637 }
Nanang Izzuddind8c33d82011-08-18 18:30:55 +00001638#if PJMEDIA_HAS_VIDEO
Benny Prijono0bc99a92011-03-17 04:34:43 +00001639 mvidcnt = acc->cfg.max_video_cnt;
1640 for (mi=0; mi<mvidcnt; ++mi) {
1641 media_types[maudcnt + mi] = PJMEDIA_TYPE_VIDEO;
1642 }
Nanang Izzuddind8c33d82011-08-18 18:30:55 +00001643#else
1644 mvidcnt = 0;
1645#endif
Nanang Izzuddinb6c239c2011-05-10 05:42:28 +00001646 call->med_cnt = maudcnt + mvidcnt;
Benny Prijonoa310bd22008-06-27 21:19:44 +00001647 }
1648
Benny Prijono0bc99a92011-03-17 04:34:43 +00001649 if (call->med_cnt == 0) {
1650 /* Expecting at least one media */
Benny Prijonoab8dba92008-06-27 21:59:15 +00001651 if (sip_err_code) *sip_err_code = PJSIP_SC_NOT_ACCEPTABLE_HERE;
Benny Prijonoa310bd22008-06-27 21:19:44 +00001652 pjsua_media_channel_deinit(call_id);
Benny Prijonob90fd382011-09-18 14:59:56 +00001653 status = PJSIP_ERRNO_FROM_SIP_STATUS(PJSIP_SC_NOT_ACCEPTABLE_HERE);
1654 goto on_error;
Benny Prijonoa310bd22008-06-27 21:19:44 +00001655 }
1656
Sauw Ming73ecfe82011-09-21 10:20:01 +00001657 if (async) {
1658 call->med_ch_cb = cb;
1659 if (rem_sdp) {
Sauw Ming73ecfe82011-09-21 10:20:01 +00001660 call->async_call.rem_sdp =
1661 pjmedia_sdp_session_clone(call->inv->pool_prov, rem_sdp);
1662 }
1663 }
1664
Sauw Ming99cc8ff2011-09-22 04:24:56 +00001665 call->async_call.pool_prov = tmp_pool;
1666
Benny Prijono0bc99a92011-03-17 04:34:43 +00001667 /* Initialize each media line */
1668 for (mi=0; mi < call->med_cnt; ++mi) {
1669 pjsua_call_media *call_med = &call->media[mi];
1670 pj_bool_t enabled = PJ_FALSE;
1671 pjmedia_type media_type = PJMEDIA_TYPE_NONE;
Benny Prijonoa310bd22008-06-27 21:19:44 +00001672
Benny Prijono0bc99a92011-03-17 04:34:43 +00001673 if (rem_sdp) {
Nanang Izzuddinb6c239c2011-05-10 05:42:28 +00001674 if (mi >= rem_sdp->media_count) {
1675 /* Media has been removed in remote re-offer */
1676 media_type = call_med->type;
1677 } else if (!pj_stricmp(&rem_sdp->media[mi]->desc.media, &STR_AUDIO)) {
Benny Prijono0bc99a92011-03-17 04:34:43 +00001678 media_type = PJMEDIA_TYPE_AUDIO;
1679 if (pj_memchr(maudidx, mi, maudcnt * sizeof(maudidx[0]))) {
1680 enabled = PJ_TRUE;
1681 }
1682 }
1683 else if (!pj_stricmp(&rem_sdp->media[mi]->desc.media, &STR_VIDEO)) {
1684 media_type = PJMEDIA_TYPE_VIDEO;
1685 if (pj_memchr(mvididx, mi, mvidcnt * sizeof(mvididx[0]))) {
1686 enabled = PJ_TRUE;
1687 }
1688 }
1689
1690 } else {
1691 enabled = PJ_TRUE;
1692 media_type = media_types[mi];
1693 }
1694
1695 if (enabled) {
1696 status = pjsua_call_media_init(call_med, media_type,
1697 &acc->cfg.rtp_cfg,
Sauw Ming73ecfe82011-09-21 10:20:01 +00001698 security_level, sip_err_code,
1699 async,
1700 (async? (pjsua_med_tp_state_cb)
1701 &media_channel_init_cb: NULL));
1702 if (status == PJ_EPENDING) {
1703 pending_med_tp = PJ_TRUE;
1704 } else if (status != PJ_SUCCESS) {
1705 if (pending_med_tp) {
1706 /* Save failure information. */
1707 call_med->tp_ready = status;
1708 pj_bzero(&call->med_ch_info, sizeof(call->med_ch_info));
1709 call->med_ch_info.status = status;
1710 call->med_ch_info.state = call_med->tp_st;
1711 call->med_ch_info.med_idx = call_med->idx;
1712 if (sip_err_code)
1713 call->med_ch_info.sip_err_code = *sip_err_code;
1714
1715 /* We will return failure in the callback later. */
1716 return PJ_EPENDING;
1717 }
1718
1719 pjsua_media_channel_deinit(call_id);
Benny Prijonob90fd382011-09-18 14:59:56 +00001720 goto on_error;
Benny Prijono0bc99a92011-03-17 04:34:43 +00001721 }
1722 } else {
Nanang Izzuddinb6c239c2011-05-10 05:42:28 +00001723 /* By convention, the media is disabled if transport is NULL
1724 * or transport state is PJSUA_MED_TP_DISABLED.
1725 */
Benny Prijono0bc99a92011-03-17 04:34:43 +00001726 if (call_med->tp) {
Nanang Izzuddinb6c239c2011-05-10 05:42:28 +00001727 // Don't close transport here, as SDP negotiation has not been
1728 // done and stream may be still active.
1729 //pjmedia_transport_close(call_med->tp);
1730 //call_med->tp = NULL;
1731 pj_assert(call_med->tp_st == PJSUA_MED_TP_INIT ||
1732 call_med->tp_st == PJSUA_MED_TP_RUNNING);
Sauw Ming73ecfe82011-09-21 10:20:01 +00001733 set_media_tp_state(call_med, PJSUA_MED_TP_DISABLED);
Benny Prijono0bc99a92011-03-17 04:34:43 +00001734 }
Nanang Izzuddinb6c239c2011-05-10 05:42:28 +00001735
1736 /* Put media type just for info */
1737 call_med->type = media_type;
Benny Prijono0bc99a92011-03-17 04:34:43 +00001738 }
Benny Prijono224b4e22008-06-19 14:10:28 +00001739 }
1740
Benny Prijono0bc99a92011-03-17 04:34:43 +00001741 call->audio_idx = maudidx[0];
1742
1743 PJ_LOG(4,(THIS_FILE, "Media index %d selected for audio call %d",
1744 call->audio_idx, call->index));
1745
Sauw Ming73ecfe82011-09-21 10:20:01 +00001746 if (pending_med_tp) {
Sauw Ming99cc8ff2011-09-22 04:24:56 +00001747 /* We shouldn't use temporary pool anymore. */
1748 call->async_call.pool_prov = NULL;
Sauw Ming73ecfe82011-09-21 10:20:01 +00001749 /* We have a pending media transport initialization. */
1750 pj_log_pop_indent();
1751 return PJ_EPENDING;
Benny Prijono0bc99a92011-03-17 04:34:43 +00001752 }
1753
Sauw Ming73ecfe82011-09-21 10:20:01 +00001754 /* Media transport initialization completed immediately, so
1755 * we don't need to call the callback.
1756 */
1757 call->med_ch_cb = NULL;
1758
1759 status = media_channel_init_cb(call_id, NULL);
1760 if (status != PJ_SUCCESS && sip_err_code)
1761 *sip_err_code = call->med_ch_info.sip_err_code;
1762
Benny Prijonob90fd382011-09-18 14:59:56 +00001763 pj_log_pop_indent();
Sauw Ming73ecfe82011-09-21 10:20:01 +00001764 return status;
Benny Prijonob90fd382011-09-18 14:59:56 +00001765
1766on_error:
Sauw Ming73ecfe82011-09-21 10:20:01 +00001767 if (call->med_ch_mutex) {
1768 pj_mutex_destroy(call->med_ch_mutex);
1769 call->med_ch_mutex = NULL;
1770 }
1771
Benny Prijonob90fd382011-09-18 14:59:56 +00001772 pj_log_pop_indent();
1773 return status;
Benny Prijonoc97608e2007-03-23 16:34:20 +00001774}
1775
1776pj_status_t pjsua_media_channel_create_sdp(pjsua_call_id call_id,
1777 pj_pool_t *pool,
Benny Prijonod8179652008-01-23 20:39:07 +00001778 const pjmedia_sdp_session *rem_sdp,
Benny Prijono25b2ea12008-01-24 19:20:54 +00001779 pjmedia_sdp_session **p_sdp,
Benny Prijono0bc99a92011-03-17 04:34:43 +00001780 int *sip_err_code)
Benny Prijonoc97608e2007-03-23 16:34:20 +00001781{
Benny Prijono0bc99a92011-03-17 04:34:43 +00001782 enum { MAX_MEDIA = PJSUA_MAX_CALL_MEDIA };
Benny Prijonoc97608e2007-03-23 16:34:20 +00001783 pjmedia_sdp_session *sdp;
Benny Prijono0bc99a92011-03-17 04:34:43 +00001784 pj_sockaddr origin;
Benny Prijonoc97608e2007-03-23 16:34:20 +00001785 pjsua_call *call = &pjsua_var.calls[call_id];
Nanang Izzuddin1e952a82010-10-05 16:32:04 +00001786 pjmedia_sdp_neg_state sdp_neg_state = PJMEDIA_SDP_NEG_STATE_NULL;
Benny Prijono0bc99a92011-03-17 04:34:43 +00001787 unsigned mi;
Benny Prijonoc97608e2007-03-23 16:34:20 +00001788 pj_status_t status;
1789
Benny Prijono0bc99a92011-03-17 04:34:43 +00001790 if (pjsua_get_state() != PJSUA_STATE_RUNNING)
Benny Prijono55e82352007-05-10 20:49:08 +00001791 return PJ_EBUSY;
Benny Prijono55e82352007-05-10 20:49:08 +00001792
Benny Prijono0bc99a92011-03-17 04:34:43 +00001793 if (rem_sdp) {
Nanang Izzuddinb6c239c2011-05-10 05:42:28 +00001794 /* If this is a re-offer, let's re-initialize media as remote may
1795 * add or remove media
1796 */
1797 if (call->inv && call->inv->state == PJSIP_INV_STATE_CONFIRMED) {
1798 status = pjsua_media_channel_init(call_id, PJSIP_ROLE_UAS,
1799 call->secure_level, pool,
Sauw Ming73ecfe82011-09-21 10:20:01 +00001800 rem_sdp, sip_err_code,
1801 PJ_FALSE, NULL);
Nanang Izzuddinb6c239c2011-05-10 05:42:28 +00001802 if (status != PJ_SUCCESS)
1803 return status;
Benny Prijono0324ba52010-12-02 10:41:46 +00001804 }
Nanang Izzuddinb6c239c2011-05-10 05:42:28 +00001805
1806#if 0
1807 pjsua_acc *acc = &pjsua_var.acc[call->acc_id];
Benny Prijono0bc99a92011-03-17 04:34:43 +00001808 pj_uint8_t maudidx[PJSUA_MAX_CALL_MEDIA];
1809 unsigned maudcnt = PJ_ARRAY_SIZE(maudidx);
Nanang Izzuddin3150d8b2010-12-01 08:20:28 +00001810
Benny Prijono0bc99a92011-03-17 04:34:43 +00001811 sort_media(rem_sdp, &STR_AUDIO, acc->cfg.use_srtp,
1812 maudidx, &maudcnt);
Nanang Izzuddin3150d8b2010-12-01 08:20:28 +00001813
Benny Prijono0bc99a92011-03-17 04:34:43 +00001814 if (maudcnt==0) {
1815 /* Expecting audio in the offer */
1816 if (sip_err_code) *sip_err_code = PJSIP_SC_NOT_ACCEPTABLE_HERE;
1817 pjsua_media_channel_deinit(call_id);
1818 return PJSIP_ERRNO_FROM_SIP_STATUS(PJSIP_SC_NOT_ACCEPTABLE_HERE);
Benny Prijono224b4e22008-06-19 14:10:28 +00001819 }
Benny Prijono224b4e22008-06-19 14:10:28 +00001820
Benny Prijono0bc99a92011-03-17 04:34:43 +00001821 call->audio_idx = maudidx[0];
Nanang Izzuddinb6c239c2011-05-10 05:42:28 +00001822#endif
Benny Prijono0bc99a92011-03-17 04:34:43 +00001823 } else {
1824 /* Audio is first in our offer, by convention */
Nanang Izzuddinb6c239c2011-05-10 05:42:28 +00001825 // The audio_idx should not be changed here, as this function may be
1826 // called in generating re-offer and the current active audio index
1827 // can be anywhere.
1828 //call->audio_idx = 0;
Benny Prijono224b4e22008-06-19 14:10:28 +00001829 }
1830
Nanang Izzuddinb6c239c2011-05-10 05:42:28 +00001831#if 0
1832 // Since r3512, old-style hold should have got transport, created by
1833 // pjsua_media_channel_init() in initial offer/answer or remote reoffer.
Benny Prijono224b4e22008-06-19 14:10:28 +00001834 /* Create media if it's not created. This could happen when call is
Benny Prijono0bc99a92011-03-17 04:34:43 +00001835 * currently on-hold (with the old style hold)
Benny Prijono224b4e22008-06-19 14:10:28 +00001836 */
Benny Prijono0bc99a92011-03-17 04:34:43 +00001837 if (call->media[call->audio_idx].tp == NULL) {
Benny Prijono224b4e22008-06-19 14:10:28 +00001838 pjsip_role_e role;
1839 role = (rem_sdp ? PJSIP_ROLE_UAS : PJSIP_ROLE_UAC);
1840 status = pjsua_media_channel_init(call_id, role, call->secure_level,
Benny Prijono0bc99a92011-03-17 04:34:43 +00001841 pool, rem_sdp, sip_err_code);
Benny Prijono224b4e22008-06-19 14:10:28 +00001842 if (status != PJ_SUCCESS)
1843 return status;
1844 }
Nanang Izzuddinb6c239c2011-05-10 05:42:28 +00001845#endif
Benny Prijono224b4e22008-06-19 14:10:28 +00001846
Nanang Izzuddin1e952a82010-10-05 16:32:04 +00001847 /* Get SDP negotiator state */
1848 if (call->inv && call->inv->neg)
1849 sdp_neg_state = pjmedia_sdp_neg_get_state(call->inv->neg);
1850
Benny Prijono0bc99a92011-03-17 04:34:43 +00001851 /* Get one address to use in the origin field */
1852 pj_bzero(&origin, sizeof(origin));
1853 for (mi=0; mi<call->med_cnt; ++mi) {
1854 pjmedia_transport_info tpinfo;
Benny Prijono617c5bc2007-04-02 19:51:21 +00001855
Benny Prijono0bc99a92011-03-17 04:34:43 +00001856 if (call->media[mi].tp == NULL)
1857 continue;
1858
1859 pjmedia_transport_info_init(&tpinfo);
1860 pjmedia_transport_get_info(call->media[mi].tp, &tpinfo);
1861 pj_sockaddr_cp(&origin, &tpinfo.sock_info.rtp_addr_name);
1862 break;
Benny Prijono25b2ea12008-01-24 19:20:54 +00001863 }
Benny Prijonoc97608e2007-03-23 16:34:20 +00001864
Benny Prijono0bc99a92011-03-17 04:34:43 +00001865 /* Create the base (blank) SDP */
1866 status = pjmedia_endpt_create_base_sdp(pjsua_var.med_endpt, pool, NULL,
1867 &origin, &sdp);
1868 if (status != PJ_SUCCESS)
1869 return status;
Benny Prijonoa310bd22008-06-27 21:19:44 +00001870
Benny Prijono0bc99a92011-03-17 04:34:43 +00001871 /* Process each media line */
1872 for (mi=0; mi<call->med_cnt; ++mi) {
1873 pjsua_call_media *call_med = &call->media[mi];
1874 pjmedia_sdp_media *m = NULL;
1875 pjmedia_transport_info tpinfo;
1876
Nanang Izzuddinb6c239c2011-05-10 05:42:28 +00001877 if (rem_sdp && mi >= rem_sdp->media_count) {
1878 /* Remote might have removed some media lines. */
1879 break;
Nanang Izzuddin1e952a82010-10-05 16:32:04 +00001880 }
1881
Nanang Izzuddinb6c239c2011-05-10 05:42:28 +00001882 if (call_med->tp == NULL || call_med->tp_st == PJSUA_MED_TP_DISABLED)
1883 {
Benny Prijono0bc99a92011-03-17 04:34:43 +00001884 /*
Nanang Izzuddinb6c239c2011-05-10 05:42:28 +00001885 * This media is disabled. Just create a valid SDP with zero
Benny Prijono0bc99a92011-03-17 04:34:43 +00001886 * port.
1887 */
Nanang Izzuddind8c33d82011-08-18 18:30:55 +00001888 if (rem_sdp) {
1889 /* Just clone the remote media and deactivate it */
1890 m = pjmedia_sdp_media_clone_deactivate(pool,
1891 rem_sdp->media[mi]);
1892 } else {
1893 m = PJ_POOL_ZALLOC_T(pool, pjmedia_sdp_media);
1894 m->desc.transport = pj_str("RTP/AVP");
1895 m->desc.fmt_count = 1;
1896 m->conn = PJ_POOL_ZALLOC_T(pool, pjmedia_sdp_conn);
1897 m->conn->net_type = pj_str("IN");
1898 m->conn->addr_type = pj_str("IP4");
1899 m->conn->addr = pj_str("127.0.0.1");
Benny Prijonoa310bd22008-06-27 21:19:44 +00001900
Nanang Izzuddind8c33d82011-08-18 18:30:55 +00001901 switch (call_med->type) {
1902 case PJMEDIA_TYPE_AUDIO:
1903 m->desc.media = pj_str("audio");
1904 m->desc.fmt[0] = pj_str("0");
1905 break;
1906 case PJMEDIA_TYPE_VIDEO:
1907 m->desc.media = pj_str("video");
1908 m->desc.fmt[0] = pj_str("31");
1909 break;
1910 default:
1911 if (rem_sdp) {
1912 pj_strdup(pool, &m->desc.media,
1913 &rem_sdp->media[mi]->desc.media);
1914 pj_strdup(pool, &m->desc.fmt[0],
1915 &rem_sdp->media[mi]->desc.fmt[0]);
1916 } else {
1917 pj_assert(!"Invalid call_med media type");
1918 return PJ_EBUG;
1919 }
Benny Prijono0bc99a92011-03-17 04:34:43 +00001920 }
Benny Prijonoa310bd22008-06-27 21:19:44 +00001921 }
Benny Prijono0bc99a92011-03-17 04:34:43 +00001922
1923 sdp->media[sdp->media_count++] = m;
1924 continue;
Benny Prijonoa310bd22008-06-27 21:19:44 +00001925 }
1926
Benny Prijono0bc99a92011-03-17 04:34:43 +00001927 /* Get transport address info */
1928 pjmedia_transport_info_init(&tpinfo);
1929 pjmedia_transport_get_info(call_med->tp, &tpinfo);
Benny Prijonoa310bd22008-06-27 21:19:44 +00001930
Benny Prijono0bc99a92011-03-17 04:34:43 +00001931 /* Ask pjmedia endpoint to create SDP media line */
1932 switch (call_med->type) {
1933 case PJMEDIA_TYPE_AUDIO:
1934 status = pjmedia_endpt_create_audio_sdp(pjsua_var.med_endpt, pool,
1935 &tpinfo.sock_info, 0, &m);
1936 break;
Nanang Izzuddin63b3c132011-07-19 11:11:07 +00001937#if defined(PJMEDIA_HAS_VIDEO) && (PJMEDIA_HAS_VIDEO != 0)
Benny Prijono0bc99a92011-03-17 04:34:43 +00001938 case PJMEDIA_TYPE_VIDEO:
1939 status = pjmedia_endpt_create_video_sdp(pjsua_var.med_endpt, pool,
1940 &tpinfo.sock_info, 0, &m);
1941 break;
Nanang Izzuddin63b3c132011-07-19 11:11:07 +00001942#endif
Benny Prijono0bc99a92011-03-17 04:34:43 +00001943 default:
1944 pj_assert(!"Invalid call_med media type");
1945 return PJ_EBUG;
1946 }
Benny Prijonoa310bd22008-06-27 21:19:44 +00001947
Benny Prijono0bc99a92011-03-17 04:34:43 +00001948 if (status != PJ_SUCCESS)
1949 return status;
1950
1951 sdp->media[sdp->media_count++] = m;
1952
1953 /* Give to transport */
1954 status = pjmedia_transport_encode_sdp(call_med->tp, pool,
1955 sdp, rem_sdp, mi);
1956 if (status != PJ_SUCCESS) {
1957 if (sip_err_code) *sip_err_code = PJSIP_SC_NOT_ACCEPTABLE;
1958 return status;
Benny Prijonoa310bd22008-06-27 21:19:44 +00001959 }
Nanang Izzuddin91ba2a22011-04-11 19:59:09 +00001960
1961 /* Copy c= line of the first media to session level,
1962 * if there's none.
1963 */
1964 if (sdp->conn == NULL) {
1965 sdp->conn = pjmedia_sdp_conn_clone(pool, m->conn);
Benny Prijonoa310bd22008-06-27 21:19:44 +00001966 }
1967 }
1968
Benny Prijono6ba8c542007-10-16 01:34:14 +00001969 /* Add NAT info in the SDP */
1970 if (pjsua_var.ua_cfg.nat_type_in_sdp) {
1971 pjmedia_sdp_attr *a;
1972 pj_str_t value;
1973 char nat_info[80];
1974
1975 value.ptr = nat_info;
1976 if (pjsua_var.ua_cfg.nat_type_in_sdp == 1) {
1977 value.slen = pj_ansi_snprintf(nat_info, sizeof(nat_info),
1978 "%d", pjsua_var.nat_type);
1979 } else {
1980 const char *type_name = pj_stun_get_nat_name(pjsua_var.nat_type);
1981 value.slen = pj_ansi_snprintf(nat_info, sizeof(nat_info),
1982 "%d %s",
1983 pjsua_var.nat_type,
1984 type_name);
1985 }
1986
1987 a = pjmedia_sdp_attr_create(pool, "X-nat", &value);
1988
1989 pjmedia_sdp_attr_add(&sdp->attr_count, sdp->attr, a);
1990
1991 }
1992
Benny Prijonoc97608e2007-03-23 16:34:20 +00001993
Benny Prijono0bc99a92011-03-17 04:34:43 +00001994#if DISABLED_FOR_TICKET_1185 && defined(PJMEDIA_HAS_SRTP) && (PJMEDIA_HAS_SRTP != 0)
Nanang Izzuddind89cc3a2010-05-13 05:22:51 +00001995 /* Check if SRTP is in optional mode and configured to use duplicated
1996 * media, i.e: secured and unsecured version, in the SDP offer.
1997 */
1998 if (!rem_sdp &&
1999 pjsua_var.acc[call->acc_id].cfg.use_srtp == PJMEDIA_SRTP_OPTIONAL &&
2000 pjsua_var.acc[call->acc_id].cfg.srtp_optional_dup_offer)
2001 {
2002 unsigned i;
2003
2004 for (i = 0; i < sdp->media_count; ++i) {
2005 pjmedia_sdp_media *m = sdp->media[i];
2006
Benny Prijono0bc99a92011-03-17 04:34:43 +00002007 /* Check if this media is unsecured but has SDP "crypto"
Nanang Izzuddind89cc3a2010-05-13 05:22:51 +00002008 * attribute.
2009 */
2010 if (pj_stricmp2(&m->desc.transport, "RTP/AVP") == 0 &&
2011 pjmedia_sdp_media_find_attr2(m, "crypto", NULL) != NULL)
2012 {
Benny Prijono0bc99a92011-03-17 04:34:43 +00002013 if (i == (unsigned)call->audio_idx &&
Nanang Izzuddin1e952a82010-10-05 16:32:04 +00002014 sdp_neg_state == PJMEDIA_SDP_NEG_STATE_DONE)
2015 {
2016 /* This is a session update, and peer has chosen the
2017 * unsecured version, so let's make this unsecured too.
2018 */
2019 pjmedia_sdp_media_remove_all_attr(m, "crypto");
2020 } else {
2021 /* This is new offer, duplicate media so we'll have
2022 * secured (with "RTP/SAVP" transport) and and unsecured
2023 * versions.
2024 */
2025 pjmedia_sdp_media *new_m;
Nanang Izzuddind89cc3a2010-05-13 05:22:51 +00002026
Nanang Izzuddin1e952a82010-10-05 16:32:04 +00002027 /* Duplicate this media and apply secured transport */
2028 new_m = pjmedia_sdp_media_clone(pool, m);
2029 pj_strdup2(pool, &new_m->desc.transport, "RTP/SAVP");
Nanang Izzuddind89cc3a2010-05-13 05:22:51 +00002030
Nanang Izzuddin1e952a82010-10-05 16:32:04 +00002031 /* Remove the "crypto" attribute in the unsecured media */
2032 pjmedia_sdp_media_remove_all_attr(m, "crypto");
Nanang Izzuddind89cc3a2010-05-13 05:22:51 +00002033
Nanang Izzuddin1e952a82010-10-05 16:32:04 +00002034 /* Insert the new media before the unsecured media */
2035 if (sdp->media_count < PJMEDIA_MAX_SDP_MEDIA) {
Benny Prijono0bc99a92011-03-17 04:34:43 +00002036 pj_array_insert(sdp->media, sizeof(new_m),
Nanang Izzuddin1e952a82010-10-05 16:32:04 +00002037 sdp->media_count, i, &new_m);
2038 ++sdp->media_count;
2039 ++i;
2040 }
Nanang Izzuddind89cc3a2010-05-13 05:22:51 +00002041 }
2042 }
2043 }
2044 }
2045#endif
2046
Benny Prijonoc97608e2007-03-23 16:34:20 +00002047 *p_sdp = sdp;
2048 return PJ_SUCCESS;
Benny Prijonoc97608e2007-03-23 16:34:20 +00002049}
2050
2051
2052static void stop_media_session(pjsua_call_id call_id)
2053{
2054 pjsua_call *call = &pjsua_var.calls[call_id];
Benny Prijono0bc99a92011-03-17 04:34:43 +00002055 unsigned mi;
Benny Prijonoc97608e2007-03-23 16:34:20 +00002056
Benny Prijonob90fd382011-09-18 14:59:56 +00002057 pj_log_push_indent();
2058
Benny Prijono0bc99a92011-03-17 04:34:43 +00002059 for (mi=0; mi<call->med_cnt; ++mi) {
2060 pjsua_call_media *call_med = &call->media[mi];
2061
2062 if (call_med->type == PJMEDIA_TYPE_AUDIO) {
2063 pjmedia_stream *strm = call_med->strm.a.stream;
2064 pjmedia_rtcp_stat stat;
2065
2066 if (strm) {
2067 if (call_med->strm.a.conf_slot != PJSUA_INVALID_ID) {
2068 if (pjsua_var.mconf) {
2069 pjsua_conf_remove_port(call_med->strm.a.conf_slot);
2070 }
2071 call_med->strm.a.conf_slot = PJSUA_INVALID_ID;
2072 }
2073
2074 if ((call_med->dir & PJMEDIA_DIR_ENCODING) &&
2075 (pjmedia_stream_get_stat(strm, &stat) == PJ_SUCCESS))
2076 {
2077 /* Save RTP timestamp & sequence, so when media session is
2078 * restarted, those values will be restored as the initial
2079 * RTP timestamp & sequence of the new media session. So in
2080 * the same call session, RTP timestamp and sequence are
2081 * guaranteed to be contigue.
2082 */
2083 call_med->rtp_tx_seq_ts_set = 1 | (1 << 1);
2084 call_med->rtp_tx_seq = stat.rtp_tx_last_seq;
2085 call_med->rtp_tx_ts = stat.rtp_tx_last_ts;
2086 }
2087
2088 if (pjsua_var.ua_cfg.cb.on_stream_destroyed) {
2089 pjsua_var.ua_cfg.cb.on_stream_destroyed(call_id, strm, mi);
2090 }
2091
2092 pjmedia_stream_destroy(strm);
2093 call_med->strm.a.stream = NULL;
Nanang Izzuddinbf26db12011-03-18 07:54:50 +00002094 }
Nanang Izzuddinfd461eb2008-06-09 09:35:59 +00002095 }
Nanang Izzuddin50fae732011-03-22 09:49:23 +00002096
2097#if PJMEDIA_HAS_VIDEO
2098 else if (call_med->type == PJMEDIA_TYPE_VIDEO) {
Nanang Izzuddin62053a62011-07-12 11:08:32 +00002099 stop_video_stream(call_med);
Benny Prijonoc97608e2007-03-23 16:34:20 +00002100 }
Nanang Izzuddin50fae732011-03-22 09:49:23 +00002101#endif
Nanang Izzuddinbf26db12011-03-18 07:54:50 +00002102
2103 PJ_LOG(4,(THIS_FILE, "Media session call%02d:%d is destroyed",
2104 call_id, mi));
Benny Prijono0bc99a92011-03-17 04:34:43 +00002105 call_med->state = PJSUA_CALL_MEDIA_NONE;
Benny Prijonoc97608e2007-03-23 16:34:20 +00002106 }
Benny Prijonob90fd382011-09-18 14:59:56 +00002107
2108 pj_log_pop_indent();
Benny Prijonoc97608e2007-03-23 16:34:20 +00002109}
2110
2111pj_status_t pjsua_media_channel_deinit(pjsua_call_id call_id)
2112{
2113 pjsua_call *call = &pjsua_var.calls[call_id];
Benny Prijono0bc99a92011-03-17 04:34:43 +00002114 unsigned mi;
Benny Prijonoc97608e2007-03-23 16:34:20 +00002115
Sauw Mingec765352011-10-03 02:04:36 +00002116 for (mi=0; mi<call->med_cnt; ++mi) {
2117 pjsua_call_media *call_med = &call->media[mi];
2118
2119 if (call_med->tp_st == PJSUA_MED_TP_CREATING)
2120 return PJ_EBUSY;
2121 }
2122
Benny Prijonob90fd382011-09-18 14:59:56 +00002123 PJ_LOG(4,(THIS_FILE, "Call %d: deinitializing media..", call_id));
2124 pj_log_push_indent();
2125
Benny Prijonoc97608e2007-03-23 16:34:20 +00002126 stop_media_session(call_id);
2127
Benny Prijono0bc99a92011-03-17 04:34:43 +00002128 for (mi=0; mi<call->med_cnt; ++mi) {
2129 pjsua_call_media *call_med = &call->media[mi];
Benny Prijonoc97608e2007-03-23 16:34:20 +00002130
Sauw Ming73ecfe82011-09-21 10:20:01 +00002131 if (call_med->tp_st > PJSUA_MED_TP_IDLE) {
Benny Prijono0bc99a92011-03-17 04:34:43 +00002132 pjmedia_transport_media_stop(call_med->tp);
Sauw Ming73ecfe82011-09-21 10:20:01 +00002133 set_media_tp_state(call_med, PJSUA_MED_TP_IDLE);
Benny Prijono0bc99a92011-03-17 04:34:43 +00002134 }
2135
Nanang Izzuddinb6c239c2011-05-10 05:42:28 +00002136 //if (call_med->tp_orig && call_med->tp &&
2137 // call_med->tp != call_med->tp_orig)
2138 //{
2139 // pjmedia_transport_close(call_med->tp);
2140 // call_med->tp = call_med->tp_orig;
2141 //}
2142 if (call_med->tp) {
Benny Prijono0bc99a92011-03-17 04:34:43 +00002143 pjmedia_transport_close(call_med->tp);
Nanang Izzuddinb6c239c2011-05-10 05:42:28 +00002144 call_med->tp = call_med->tp_orig = NULL;
Benny Prijono0bc99a92011-03-17 04:34:43 +00002145 }
Benny Prijonod8179652008-01-23 20:39:07 +00002146 }
Nanang Izzuddin670f71b2009-01-20 14:05:54 +00002147
2148 check_snd_dev_idle();
Benny Prijonob90fd382011-09-18 14:59:56 +00002149 pj_log_pop_indent();
Nanang Izzuddin670f71b2009-01-20 14:05:54 +00002150
Benny Prijonoc97608e2007-03-23 16:34:20 +00002151 return PJ_SUCCESS;
2152}
2153
2154
2155/*
2156 * DTMF callback from the stream.
2157 */
2158static void dtmf_callback(pjmedia_stream *strm, void *user_data,
2159 int digit)
2160{
2161 PJ_UNUSED_ARG(strm);
2162
Benny Prijonob90fd382011-09-18 14:59:56 +00002163 pj_log_push_indent();
2164
Benny Prijono0c068262008-02-14 14:38:52 +00002165 /* For discussions about call mutex protection related to this
2166 * callback, please see ticket #460:
2167 * http://trac.pjsip.org/repos/ticket/460#comment:4
2168 */
Benny Prijonoc97608e2007-03-23 16:34:20 +00002169 if (pjsua_var.ua_cfg.cb.on_dtmf_digit) {
2170 pjsua_call_id call_id;
2171
Benny Prijonod8179652008-01-23 20:39:07 +00002172 call_id = (pjsua_call_id)(long)user_data;
Benny Prijonoc97608e2007-03-23 16:34:20 +00002173 pjsua_var.ua_cfg.cb.on_dtmf_digit(call_id, digit);
2174 }
Benny Prijonob90fd382011-09-18 14:59:56 +00002175
2176 pj_log_pop_indent();
Benny Prijonoc97608e2007-03-23 16:34:20 +00002177}
2178
2179
Benny Prijono0bc99a92011-03-17 04:34:43 +00002180static pj_status_t audio_channel_update(pjsua_call_media *call_med,
2181 pj_pool_t *tmp_pool,
2182 const pjmedia_sdp_session *local_sdp,
2183 const pjmedia_sdp_session *remote_sdp)
Benny Prijonoc97608e2007-03-23 16:34:20 +00002184{
Benny Prijono0bc99a92011-03-17 04:34:43 +00002185 pjsua_call *call = call_med->call;
2186 pjmedia_stream_info the_si, *si = &the_si;
Benny Prijonoc97608e2007-03-23 16:34:20 +00002187 pjmedia_port *media_port;
Benny Prijono0bc99a92011-03-17 04:34:43 +00002188 unsigned strm_idx = call_med->idx;
Benny Prijonoc97608e2007-03-23 16:34:20 +00002189 pj_status_t status;
Benny Prijonob90fd382011-09-18 14:59:56 +00002190
2191 PJ_LOG(4,(THIS_FILE,"Audio channel update.."));
2192 pj_log_push_indent();
Benny Prijono0bc99a92011-03-17 04:34:43 +00002193
2194 status = pjmedia_stream_info_from_sdp(si, tmp_pool, pjsua_var.med_endpt,
2195 local_sdp, remote_sdp, strm_idx);
Benny Prijonoc97608e2007-03-23 16:34:20 +00002196 if (status != PJ_SUCCESS)
Benny Prijonob90fd382011-09-18 14:59:56 +00002197 goto on_return;
Benny Prijonoc97608e2007-03-23 16:34:20 +00002198
Nanang Izzuddin99d69522008-08-04 15:01:38 +00002199 /* Check if no media is active */
Benny Prijono0bc99a92011-03-17 04:34:43 +00002200 if (si->dir == PJMEDIA_DIR_NONE) {
Nanang Izzuddin99d69522008-08-04 15:01:38 +00002201 /* Call media state */
Benny Prijono0bc99a92011-03-17 04:34:43 +00002202 call_med->state = PJSUA_CALL_MEDIA_NONE;
Benny Prijonoc97608e2007-03-23 16:34:20 +00002203
Nanang Izzuddin99d69522008-08-04 15:01:38 +00002204 /* Call media direction */
Benny Prijono0bc99a92011-03-17 04:34:43 +00002205 call_med->dir = PJMEDIA_DIR_NONE;
Benny Prijono68f9e4f2008-03-21 08:56:02 +00002206
Benny Prijonoc97608e2007-03-23 16:34:20 +00002207 } else {
Nanang Izzuddin4375f902008-06-26 19:12:09 +00002208 pjmedia_transport_info tp_info;
2209
Benny Prijono224b4e22008-06-19 14:10:28 +00002210 /* Start/restart media transport */
Benny Prijono0bc99a92011-03-17 04:34:43 +00002211 status = pjmedia_transport_media_start(call_med->tp,
2212 tmp_pool, local_sdp,
2213 remote_sdp, strm_idx);
Benny Prijonod8179652008-01-23 20:39:07 +00002214 if (status != PJ_SUCCESS)
Benny Prijonob90fd382011-09-18 14:59:56 +00002215 goto on_return;
Benny Prijonoc97608e2007-03-23 16:34:20 +00002216
Sauw Ming73ecfe82011-09-21 10:20:01 +00002217 set_media_tp_state(call_med, PJSUA_MED_TP_RUNNING);
Benny Prijono224b4e22008-06-19 14:10:28 +00002218
Nanang Izzuddin4375f902008-06-26 19:12:09 +00002219 /* Get remote SRTP usage policy */
2220 pjmedia_transport_info_init(&tp_info);
Benny Prijono0bc99a92011-03-17 04:34:43 +00002221 pjmedia_transport_get_info(call_med->tp, &tp_info);
Nanang Izzuddin4375f902008-06-26 19:12:09 +00002222 if (tp_info.specific_info_cnt > 0) {
Benny Prijonof5d9f1f2009-10-14 13:13:18 +00002223 unsigned i;
Nanang Izzuddin4375f902008-06-26 19:12:09 +00002224 for (i = 0; i < tp_info.specific_info_cnt; ++i) {
2225 if (tp_info.spc_info[i].type == PJMEDIA_TRANSPORT_TYPE_SRTP)
2226 {
2227 pjmedia_srtp_info *srtp_info =
2228 (pjmedia_srtp_info*) tp_info.spc_info[i].buffer;
2229
Benny Prijono0bc99a92011-03-17 04:34:43 +00002230 call_med->rem_srtp_use = srtp_info->peer_use;
Nanang Izzuddin4375f902008-06-26 19:12:09 +00002231 break;
2232 }
2233 }
2234 }
2235
Benny Prijonoc97608e2007-03-23 16:34:20 +00002236 /* Override ptime, if this option is specified. */
2237 if (pjsua_var.media_cfg.ptime != 0) {
Benny Prijono91e567e2007-12-28 08:51:58 +00002238 si->param->setting.frm_per_pkt = (pj_uint8_t)
2239 (pjsua_var.media_cfg.ptime / si->param->info.frm_ptime);
2240 if (si->param->setting.frm_per_pkt == 0)
2241 si->param->setting.frm_per_pkt = 1;
Benny Prijonoc97608e2007-03-23 16:34:20 +00002242 }
2243
2244 /* Disable VAD, if this option is specified. */
2245 if (pjsua_var.media_cfg.no_vad) {
Benny Prijono91e567e2007-12-28 08:51:58 +00002246 si->param->setting.vad = 0;
Benny Prijonoc97608e2007-03-23 16:34:20 +00002247 }
2248
2249
2250 /* Optionally, application may modify other stream settings here
2251 * (such as jitter buffer parameters, codec ptime, etc.)
2252 */
Benny Prijono91e567e2007-12-28 08:51:58 +00002253 si->jb_init = pjsua_var.media_cfg.jb_init;
2254 si->jb_min_pre = pjsua_var.media_cfg.jb_min_pre;
2255 si->jb_max_pre = pjsua_var.media_cfg.jb_max_pre;
2256 si->jb_max = pjsua_var.media_cfg.jb_max;
Benny Prijonoc97608e2007-03-23 16:34:20 +00002257
Benny Prijono8147f402007-11-21 14:50:07 +00002258 /* Set SSRC */
Benny Prijono0bc99a92011-03-17 04:34:43 +00002259 si->ssrc = call_med->ssrc;
Benny Prijono8147f402007-11-21 14:50:07 +00002260
Nanang Izzuddina815ceb2008-08-26 16:51:28 +00002261 /* Set RTP timestamp & sequence, normally these value are intialized
2262 * automatically when stream session created, but for some cases (e.g:
2263 * call reinvite, call update) timestamp and sequence need to be kept
2264 * contigue.
2265 */
Benny Prijono0bc99a92011-03-17 04:34:43 +00002266 si->rtp_ts = call_med->rtp_tx_ts;
2267 si->rtp_seq = call_med->rtp_tx_seq;
2268 si->rtp_seq_ts_set = call_med->rtp_tx_seq_ts_set;
Nanang Izzuddina815ceb2008-08-26 16:51:28 +00002269
Nanang Izzuddin5e39a2b2010-09-20 06:13:02 +00002270#if defined(PJMEDIA_STREAM_ENABLE_KA) && PJMEDIA_STREAM_ENABLE_KA!=0
2271 /* Enable/disable stream keep-alive and NAT hole punch. */
2272 si->use_ka = pjsua_var.acc[call->acc_id].cfg.use_stream_ka;
2273#endif
2274
Benny Prijonoc97608e2007-03-23 16:34:20 +00002275 /* Create session based on session info. */
Benny Prijono0bc99a92011-03-17 04:34:43 +00002276 status = pjmedia_stream_create(pjsua_var.med_endpt, NULL, si,
2277 call_med->tp, NULL,
2278 &call_med->strm.a.stream);
2279 if (status != PJ_SUCCESS) {
Benny Prijonob90fd382011-09-18 14:59:56 +00002280 goto on_return;
Benny Prijono0bc99a92011-03-17 04:34:43 +00002281 }
2282
2283 /* Start stream */
2284 status = pjmedia_stream_start(call_med->strm.a.stream);
Benny Prijonoc97608e2007-03-23 16:34:20 +00002285 if (status != PJ_SUCCESS) {
Benny Prijonob90fd382011-09-18 14:59:56 +00002286 goto on_return;
Benny Prijonoc97608e2007-03-23 16:34:20 +00002287 }
2288
2289 /* If DTMF callback is installed by application, install our
2290 * callback to the session.
2291 */
2292 if (pjsua_var.ua_cfg.cb.on_dtmf_digit) {
Benny Prijono0bc99a92011-03-17 04:34:43 +00002293 pjmedia_stream_set_dtmf_callback(call_med->strm.a.stream,
2294 &dtmf_callback,
2295 (void*)(long)(call->index));
Benny Prijonoc97608e2007-03-23 16:34:20 +00002296 }
2297
2298 /* Get the port interface of the first stream in the session.
2299 * We need the port interface to add to the conference bridge.
2300 */
Benny Prijono0bc99a92011-03-17 04:34:43 +00002301 pjmedia_stream_get_port(call_med->strm.a.stream, &media_port);
Benny Prijonoc97608e2007-03-23 16:34:20 +00002302
Benny Prijonofc13bf62008-02-20 08:56:15 +00002303 /* Notify application about stream creation.
2304 * Note: application may modify media_port to point to different
2305 * media port
2306 */
2307 if (pjsua_var.ua_cfg.cb.on_stream_created) {
Benny Prijono0bc99a92011-03-17 04:34:43 +00002308 pjsua_var.ua_cfg.cb.on_stream_created(call->index,
2309 call_med->strm.a.stream,
2310 strm_idx, &media_port);
Benny Prijonofc13bf62008-02-20 08:56:15 +00002311 }
Benny Prijonoc97608e2007-03-23 16:34:20 +00002312
2313 /*
2314 * Add the call to conference bridge.
2315 */
Benny Prijono38fb3ea2008-01-02 08:27:03 +00002316 {
2317 char tmp[PJSIP_MAX_URL_SIZE];
2318 pj_str_t port_name;
2319
2320 port_name.ptr = tmp;
2321 port_name.slen = pjsip_uri_print(PJSIP_URI_IN_REQ_URI,
2322 call->inv->dlg->remote.info->uri,
2323 tmp, sizeof(tmp));
2324 if (port_name.slen < 1) {
2325 port_name = pj_str("call");
2326 }
Benny Prijono40d62b62009-08-12 17:53:47 +00002327 status = pjmedia_conf_add_port( pjsua_var.mconf,
2328 call->inv->pool_prov,
Benny Prijono38fb3ea2008-01-02 08:27:03 +00002329 media_port,
2330 &port_name,
Benny Prijono0bc99a92011-03-17 04:34:43 +00002331 (unsigned*)
2332 &call_med->strm.a.conf_slot);
Benny Prijono38fb3ea2008-01-02 08:27:03 +00002333 if (status != PJ_SUCCESS) {
Benny Prijonob90fd382011-09-18 14:59:56 +00002334 goto on_return;
Benny Prijono38fb3ea2008-01-02 08:27:03 +00002335 }
Benny Prijonoc97608e2007-03-23 16:34:20 +00002336 }
2337
Nanang Izzuddin99d69522008-08-04 15:01:38 +00002338 /* Call media direction */
Benny Prijono0bc99a92011-03-17 04:34:43 +00002339 call_med->dir = si->dir;
Nanang Izzuddin99d69522008-08-04 15:01:38 +00002340
2341 /* Call media state */
2342 if (call->local_hold)
Benny Prijono0bc99a92011-03-17 04:34:43 +00002343 call_med->state = PJSUA_CALL_MEDIA_LOCAL_HOLD;
2344 else if (call_med->dir == PJMEDIA_DIR_DECODING)
2345 call_med->state = PJSUA_CALL_MEDIA_REMOTE_HOLD;
Nanang Izzuddin99d69522008-08-04 15:01:38 +00002346 else
Benny Prijono0bc99a92011-03-17 04:34:43 +00002347 call_med->state = PJSUA_CALL_MEDIA_ACTIVE;
Benny Prijonoc97608e2007-03-23 16:34:20 +00002348 }
2349
2350 /* Print info. */
2351 {
2352 char info[80];
2353 int info_len = 0;
Benny Prijono0bc99a92011-03-17 04:34:43 +00002354 int len;
2355 const char *dir;
Benny Prijonoc97608e2007-03-23 16:34:20 +00002356
Benny Prijono0bc99a92011-03-17 04:34:43 +00002357 switch (si->dir) {
2358 case PJMEDIA_DIR_NONE:
2359 dir = "inactive";
2360 break;
2361 case PJMEDIA_DIR_ENCODING:
2362 dir = "sendonly";
2363 break;
2364 case PJMEDIA_DIR_DECODING:
2365 dir = "recvonly";
2366 break;
2367 case PJMEDIA_DIR_ENCODING_DECODING:
2368 dir = "sendrecv";
2369 break;
2370 default:
2371 dir = "unknown";
2372 break;
Benny Prijonoc97608e2007-03-23 16:34:20 +00002373 }
Benny Prijono0bc99a92011-03-17 04:34:43 +00002374 len = pj_ansi_sprintf( info+info_len,
2375 ", stream #%d: %.*s (%s)", strm_idx,
2376 (int)si->fmt.encoding_name.slen,
2377 si->fmt.encoding_name.ptr,
2378 dir);
2379 if (len > 0)
2380 info_len += len;
Benny Prijonob90fd382011-09-18 14:59:56 +00002381 PJ_LOG(4,(THIS_FILE,"Audio updated%s", info));
Benny Prijonoc97608e2007-03-23 16:34:20 +00002382 }
2383
Benny Prijonob90fd382011-09-18 14:59:56 +00002384on_return:
2385 pj_log_pop_indent();
2386 return status;
Benny Prijonoc97608e2007-03-23 16:34:20 +00002387}
2388
Benny Prijono0bc99a92011-03-17 04:34:43 +00002389pj_status_t pjsua_media_channel_update(pjsua_call_id call_id,
2390 const pjmedia_sdp_session *local_sdp,
2391 const pjmedia_sdp_session *remote_sdp)
2392{
2393 pjsua_call *call = &pjsua_var.calls[call_id];
Nanang Izzuddind8c33d82011-08-18 18:30:55 +00002394 pjsua_acc *acc = &pjsua_var.acc[call->acc_id];
Benny Prijono0bc99a92011-03-17 04:34:43 +00002395 pj_pool_t *tmp_pool = call->inv->pool_prov;
2396 unsigned mi;
Nanang Izzuddinb6c239c2011-05-10 05:42:28 +00002397 pj_bool_t got_media = PJ_FALSE;
Nanang Izzuddinbf26db12011-03-18 07:54:50 +00002398 pj_status_t status = PJ_SUCCESS;
Benny Prijono0bc99a92011-03-17 04:34:43 +00002399
Nanang Izzuddind8c33d82011-08-18 18:30:55 +00002400 const pj_str_t STR_AUDIO = { "audio", 5 };
2401 const pj_str_t STR_VIDEO = { "video", 5 };
2402 pj_uint8_t maudidx[PJSUA_MAX_CALL_MEDIA];
2403 unsigned maudcnt = PJ_ARRAY_SIZE(maudidx);
2404 pj_uint8_t mvididx[PJSUA_MAX_CALL_MEDIA];
2405 unsigned mvidcnt = PJ_ARRAY_SIZE(mvididx);
2406 pj_bool_t need_renego_sdp = PJ_FALSE;
2407
Benny Prijono0bc99a92011-03-17 04:34:43 +00002408 if (pjsua_get_state() != PJSUA_STATE_RUNNING)
2409 return PJ_EBUSY;
2410
Benny Prijonob90fd382011-09-18 14:59:56 +00002411 PJ_LOG(4,(THIS_FILE, "Call %d: updating media..", call_id));
2412 pj_log_push_indent();
2413
Benny Prijono0bc99a92011-03-17 04:34:43 +00002414 /* Destroy existing media session, if any. */
2415 stop_media_session(call->index);
2416
Nanang Izzuddind8c33d82011-08-18 18:30:55 +00002417 /* Call media count must be at least equal to SDP media. Note that
2418 * it may not be equal when remote removed any SDP media line.
2419 */
2420 pj_assert(call->med_cnt >= local_sdp->media_count);
2421
Benny Prijono0bc99a92011-03-17 04:34:43 +00002422 /* Reset audio_idx first */
2423 call->audio_idx = -1;
2424
Nanang Izzuddind8c33d82011-08-18 18:30:55 +00002425 /* Apply maximum audio/video count of the account */
2426 sort_media(local_sdp, &STR_AUDIO, acc->cfg.use_srtp,
2427 maudidx, &maudcnt);
2428#if PJMEDIA_HAS_VIDEO
2429 sort_media(local_sdp, &STR_VIDEO, acc->cfg.use_srtp,
2430 mvididx, &mvidcnt);
2431#else
2432 PJ_UNUSED_ARG(STR_VIDEO);
2433 mvidcnt = 0;
2434#endif
2435 if (maudcnt > acc->cfg.max_audio_cnt || mvidcnt > acc->cfg.max_video_cnt)
2436 {
2437 pjmedia_sdp_session *local_sdp2;
2438
2439 maudcnt = PJ_MIN(maudcnt, acc->cfg.max_audio_cnt);
2440 mvidcnt = PJ_MIN(mvidcnt, acc->cfg.max_video_cnt);
2441 local_sdp2 = pjmedia_sdp_session_clone(tmp_pool, local_sdp);
2442
2443 for (mi=0; mi < local_sdp2->media_count; ++mi) {
2444 pjmedia_sdp_media *m = local_sdp2->media[mi];
2445
2446 if (m->desc.port == 0 ||
2447 pj_memchr(maudidx, mi, maudcnt*sizeof(maudidx[0])) ||
2448 pj_memchr(mvididx, mi, mvidcnt*sizeof(mvididx[0])))
2449 {
2450 continue;
2451 }
2452
2453 /* Deactivate this media */
2454 pjmedia_sdp_media_deactivate(tmp_pool, m);
2455 }
2456
2457 local_sdp = local_sdp2;
2458 need_renego_sdp = PJ_TRUE;
2459 }
2460
Benny Prijono0bc99a92011-03-17 04:34:43 +00002461 /* Process each media stream */
2462 for (mi=0; mi < call->med_cnt; ++mi) {
2463 pjsua_call_media *call_med = &call->media[mi];
2464
Nanang Izzuddinb6c239c2011-05-10 05:42:28 +00002465 if (mi >= local_sdp->media_count ||
2466 mi >= remote_sdp->media_count)
Benny Prijono0bc99a92011-03-17 04:34:43 +00002467 {
Nanang Izzuddinb6c239c2011-05-10 05:42:28 +00002468 /* This may happen when remote removed any SDP media lines in
2469 * its re-offer.
2470 */
2471 continue;
2472#if 0
Benny Prijono0bc99a92011-03-17 04:34:43 +00002473 /* Something is wrong */
2474 PJ_LOG(1,(THIS_FILE, "Error updating media for call %d: "
2475 "invalid media index %d in SDP", call_id, mi));
Benny Prijonob90fd382011-09-18 14:59:56 +00002476 status = PJMEDIA_SDP_EINSDP;
2477 goto on_error;
Nanang Izzuddinb6c239c2011-05-10 05:42:28 +00002478#endif
Benny Prijono0bc99a92011-03-17 04:34:43 +00002479 }
2480
2481 switch (call_med->type) {
2482 case PJMEDIA_TYPE_AUDIO:
2483 status = audio_channel_update(call_med, tmp_pool,
2484 local_sdp, remote_sdp);
2485 if (call->audio_idx==-1 && status==PJ_SUCCESS &&
Nanang Izzuddinbf26db12011-03-18 07:54:50 +00002486 call_med->strm.a.stream)
Benny Prijono0bc99a92011-03-17 04:34:43 +00002487 {
2488 call->audio_idx = mi;
2489 }
2490 break;
Nanang Izzuddin63b3c132011-07-19 11:11:07 +00002491#if defined(PJMEDIA_HAS_VIDEO) && (PJMEDIA_HAS_VIDEO != 0)
Benny Prijono0bc99a92011-03-17 04:34:43 +00002492 case PJMEDIA_TYPE_VIDEO:
Nanang Izzuddinbf26db12011-03-18 07:54:50 +00002493 status = video_channel_update(call_med, tmp_pool,
2494 local_sdp, remote_sdp);
Benny Prijono0bc99a92011-03-17 04:34:43 +00002495 break;
Nanang Izzuddin50fae732011-03-22 09:49:23 +00002496#endif
Benny Prijono0bc99a92011-03-17 04:34:43 +00002497 default:
Nanang Izzuddinb6c239c2011-05-10 05:42:28 +00002498 status = PJMEDIA_EINVALIMEDIATYPE;
Benny Prijono0bc99a92011-03-17 04:34:43 +00002499 break;
2500 }
2501
Nanang Izzuddind8c33d82011-08-18 18:30:55 +00002502 /* Close the transport of deactivated media, need this here as media
2503 * can be deactivated by the SDP negotiation and the max media count
2504 * (account) setting.
2505 */
2506 if (local_sdp->media[mi]->desc.port==0 && call_med->tp) {
2507 pjmedia_transport_close(call_med->tp);
2508 call_med->tp = call_med->tp_orig = NULL;
Sauw Ming73ecfe82011-09-21 10:20:01 +00002509 set_media_tp_state(call_med, PJSUA_MED_TP_IDLE);
Nanang Izzuddind8c33d82011-08-18 18:30:55 +00002510 }
2511
Benny Prijono0bc99a92011-03-17 04:34:43 +00002512 if (status != PJ_SUCCESS) {
Nanang Izzuddinb6c239c2011-05-10 05:42:28 +00002513 PJ_PERROR(1,(THIS_FILE, status, "Error updating media call%02d:%d",
Benny Prijono0bc99a92011-03-17 04:34:43 +00002514 call_id, mi));
Nanang Izzuddinb6c239c2011-05-10 05:42:28 +00002515 } else {
2516 got_media = PJ_TRUE;
Benny Prijono0bc99a92011-03-17 04:34:43 +00002517 }
2518 }
2519
Nanang Izzuddind8c33d82011-08-18 18:30:55 +00002520 /* Perform SDP re-negotiation if needed. */
2521 if (got_media && need_renego_sdp) {
2522 pjmedia_sdp_neg *neg = call->inv->neg;
2523
2524 /* This should only happen when we are the answerer. */
2525 PJ_ASSERT_RETURN(neg && !pjmedia_sdp_neg_was_answer_remote(neg),
2526 PJMEDIA_SDPNEG_EINSTATE);
2527
2528 status = pjmedia_sdp_neg_set_remote_offer(tmp_pool, neg, remote_sdp);
2529 if (status != PJ_SUCCESS)
Benny Prijonob90fd382011-09-18 14:59:56 +00002530 goto on_error;
Nanang Izzuddind8c33d82011-08-18 18:30:55 +00002531
2532 status = pjmedia_sdp_neg_set_local_answer(tmp_pool, neg, local_sdp);
2533 if (status != PJ_SUCCESS)
Benny Prijonob90fd382011-09-18 14:59:56 +00002534 goto on_error;
Nanang Izzuddind8c33d82011-08-18 18:30:55 +00002535
2536 status = pjmedia_sdp_neg_negotiate(tmp_pool, neg, 0);
2537 if (status != PJ_SUCCESS)
Benny Prijonob90fd382011-09-18 14:59:56 +00002538 goto on_error;
Nanang Izzuddind8c33d82011-08-18 18:30:55 +00002539 }
2540
Benny Prijonob90fd382011-09-18 14:59:56 +00002541 pj_log_pop_indent();
Nanang Izzuddinb6c239c2011-05-10 05:42:28 +00002542 return (got_media? PJ_SUCCESS : PJMEDIA_SDPNEG_ENOMEDIA);
Benny Prijonob90fd382011-09-18 14:59:56 +00002543
2544on_error:
2545 pj_log_pop_indent();
2546 return status;
Benny Prijono0bc99a92011-03-17 04:34:43 +00002547}
2548
Benny Prijonoeebe9af2006-06-13 22:57:13 +00002549/*
2550 * Get maxinum number of conference ports.
2551 */
2552PJ_DEF(unsigned) pjsua_conf_get_max_ports(void)
2553{
2554 return pjsua_var.media_cfg.max_media_ports;
2555}
2556
2557
2558/*
2559 * Get current number of active ports in the bridge.
2560 */
2561PJ_DEF(unsigned) pjsua_conf_get_active_ports(void)
2562{
Benny Prijono38fb3ea2008-01-02 08:27:03 +00002563 unsigned ports[PJSUA_MAX_CONF_PORTS];
Benny Prijonoeebe9af2006-06-13 22:57:13 +00002564 unsigned count = PJ_ARRAY_SIZE(ports);
2565 pj_status_t status;
2566
2567 status = pjmedia_conf_enum_ports(pjsua_var.mconf, ports, &count);
2568 if (status != PJ_SUCCESS)
2569 count = 0;
2570
2571 return count;
2572}
2573
2574
2575/*
2576 * Enumerate all conference ports.
2577 */
2578PJ_DEF(pj_status_t) pjsua_enum_conf_ports(pjsua_conf_port_id id[],
2579 unsigned *count)
2580{
2581 return pjmedia_conf_enum_ports(pjsua_var.mconf, (unsigned*)id, count);
2582}
2583
2584
2585/*
2586 * Get information about the specified conference port
2587 */
2588PJ_DEF(pj_status_t) pjsua_conf_get_port_info( pjsua_conf_port_id id,
2589 pjsua_conf_port_info *info)
2590{
2591 pjmedia_conf_port_info cinfo;
Benny Prijono0498d902006-06-19 14:49:14 +00002592 unsigned i;
Benny Prijonoeebe9af2006-06-13 22:57:13 +00002593 pj_status_t status;
2594
2595 status = pjmedia_conf_get_port_info( pjsua_var.mconf, id, &cinfo);
2596 if (status != PJ_SUCCESS)
2597 return status;
2598
Benny Prijonoac623b32006-07-03 15:19:31 +00002599 pj_bzero(info, sizeof(*info));
Benny Prijonoeebe9af2006-06-13 22:57:13 +00002600 info->slot_id = id;
2601 info->name = cinfo.name;
2602 info->clock_rate = cinfo.clock_rate;
2603 info->channel_count = cinfo.channel_count;
2604 info->samples_per_frame = cinfo.samples_per_frame;
2605 info->bits_per_sample = cinfo.bits_per_sample;
2606
2607 /* Build array of listeners */
Benny Prijonoc78c3a32006-06-16 15:54:43 +00002608 info->listener_cnt = cinfo.listener_cnt;
2609 for (i=0; i<cinfo.listener_cnt; ++i) {
2610 info->listeners[i] = cinfo.listener_slots[i];
Benny Prijonoeebe9af2006-06-13 22:57:13 +00002611 }
2612
2613 return PJ_SUCCESS;
2614}
2615
2616
2617/*
Benny Prijonoe909eac2006-07-27 22:04:56 +00002618 * Add arbitrary media port to PJSUA's conference bridge.
2619 */
2620PJ_DEF(pj_status_t) pjsua_conf_add_port( pj_pool_t *pool,
2621 pjmedia_port *port,
2622 pjsua_conf_port_id *p_id)
2623{
2624 pj_status_t status;
2625
2626 status = pjmedia_conf_add_port(pjsua_var.mconf, pool,
2627 port, NULL, (unsigned*)p_id);
2628 if (status != PJ_SUCCESS) {
2629 if (p_id)
2630 *p_id = PJSUA_INVALID_ID;
2631 }
2632
2633 return status;
2634}
2635
2636
2637/*
2638 * Remove arbitrary slot from the conference bridge.
2639 */
2640PJ_DEF(pj_status_t) pjsua_conf_remove_port(pjsua_conf_port_id id)
2641{
Nanang Izzuddin148fd392008-06-16 09:52:50 +00002642 pj_status_t status;
2643
2644 status = pjmedia_conf_remove_port(pjsua_var.mconf, (unsigned)id);
2645 check_snd_dev_idle();
2646
2647 return status;
Benny Prijonoe909eac2006-07-27 22:04:56 +00002648}
2649
2650
2651/*
Benny Prijonoeebe9af2006-06-13 22:57:13 +00002652 * Establish unidirectional media flow from souce to sink.
2653 */
2654PJ_DEF(pj_status_t) pjsua_conf_connect( pjsua_conf_port_id source,
2655 pjsua_conf_port_id sink)
2656{
Benny Prijonob90fd382011-09-18 14:59:56 +00002657 pj_status_t status = PJ_SUCCESS;
2658
2659 PJ_LOG(4,(THIS_FILE, "%s connect: %d --> %d",
2660 (pjsua_var.is_mswitch ? "Switch" : "Conf"),
2661 source, sink));
2662 pj_log_push_indent();
2663
Nanang Izzuddin68559c32008-06-13 17:01:46 +00002664 /* If sound device idle timer is active, cancel it first. */
Benny Prijono0f711b42009-05-06 19:08:43 +00002665 PJSUA_LOCK();
Nanang Izzuddin68559c32008-06-13 17:01:46 +00002666 if (pjsua_var.snd_idle_timer.id) {
2667 pjsip_endpt_cancel_timer(pjsua_var.endpt, &pjsua_var.snd_idle_timer);
2668 pjsua_var.snd_idle_timer.id = PJ_FALSE;
2669 }
Benny Prijono0f711b42009-05-06 19:08:43 +00002670 PJSUA_UNLOCK();
Nanang Izzuddin68559c32008-06-13 17:01:46 +00002671
Nanang Izzuddinfe02a062009-02-18 14:28:49 +00002672
Benny Prijonof798e502009-03-09 13:08:16 +00002673 /* For audio switchboard (i.e. APS-Direct):
2674 * Check if sound device need to be reopened, i.e: its attributes
Nanang Izzuddinfe02a062009-02-18 14:28:49 +00002675 * (format, clock rate, channel count) must match to peer's.
2676 * Note that sound device can be reopened only if it doesn't have
2677 * any connection.
2678 */
Benny Prijonof798e502009-03-09 13:08:16 +00002679 if (pjsua_var.is_mswitch) {
Nanang Izzuddinfe02a062009-02-18 14:28:49 +00002680 pjmedia_conf_port_info port0_info;
2681 pjmedia_conf_port_info peer_info;
2682 unsigned peer_id;
2683 pj_bool_t need_reopen = PJ_FALSE;
Nanang Izzuddinfe02a062009-02-18 14:28:49 +00002684
2685 peer_id = (source!=0)? source : sink;
2686 status = pjmedia_conf_get_port_info(pjsua_var.mconf, peer_id,
2687 &peer_info);
2688 pj_assert(status == PJ_SUCCESS);
2689
2690 status = pjmedia_conf_get_port_info(pjsua_var.mconf, 0, &port0_info);
2691 pj_assert(status == PJ_SUCCESS);
2692
2693 /* Check if sound device is instantiated. */
2694 need_reopen = (pjsua_var.snd_port==NULL && pjsua_var.null_snd==NULL &&
2695 !pjsua_var.no_snd);
2696
2697 /* Check if sound device need to reopen because it needs to modify
2698 * settings to match its peer. Sound device must be idle in this case
2699 * though.
2700 */
2701 if (!need_reopen &&
2702 port0_info.listener_cnt==0 && port0_info.transmitter_cnt==0)
2703 {
2704 need_reopen = (peer_info.format.id != port0_info.format.id ||
Benny Prijonoc45d9512010-12-10 11:04:30 +00002705 peer_info.format.det.aud.avg_bps !=
2706 port0_info.format.det.aud.avg_bps ||
Nanang Izzuddinfe02a062009-02-18 14:28:49 +00002707 peer_info.clock_rate != port0_info.clock_rate ||
Benny Prijonoc45d9512010-12-10 11:04:30 +00002708 peer_info.channel_count!=port0_info.channel_count);
Nanang Izzuddinfe02a062009-02-18 14:28:49 +00002709 }
2710
2711 if (need_reopen) {
Benny Prijonod65f78c2009-06-03 18:59:37 +00002712 if (pjsua_var.cap_dev != NULL_SND_DEV_ID) {
Sauw Ming98766c72011-03-11 06:57:24 +00002713 pjmedia_snd_port_param param;
Nanang Izzuddinfe02a062009-02-18 14:28:49 +00002714
Benny Prijonod65f78c2009-06-03 18:59:37 +00002715 /* Create parameter based on peer info */
Sauw Ming98766c72011-03-11 06:57:24 +00002716 status = create_aud_param(&param.base, pjsua_var.cap_dev,
Benny Prijonod65f78c2009-06-03 18:59:37 +00002717 pjsua_var.play_dev,
2718 peer_info.clock_rate,
2719 peer_info.channel_count,
2720 peer_info.samples_per_frame,
2721 peer_info.bits_per_sample);
2722 if (status != PJ_SUCCESS) {
Benny Prijonoc45d9512010-12-10 11:04:30 +00002723 pjsua_perror(THIS_FILE, "Error opening sound device",
2724 status);
Benny Prijonob90fd382011-09-18 14:59:56 +00002725 goto on_return;
Benny Prijonod65f78c2009-06-03 18:59:37 +00002726 }
Benny Prijonof798e502009-03-09 13:08:16 +00002727
Benny Prijonod65f78c2009-06-03 18:59:37 +00002728 /* And peer format */
2729 if (peer_info.format.id != PJMEDIA_FORMAT_PCM) {
Sauw Ming98766c72011-03-11 06:57:24 +00002730 param.base.flags |= PJMEDIA_AUD_DEV_CAP_EXT_FORMAT;
2731 param.base.ext_fmt = peer_info.format;
Benny Prijonod65f78c2009-06-03 18:59:37 +00002732 }
Benny Prijono10454dc2009-02-21 14:21:59 +00002733
Sauw Ming98766c72011-03-11 06:57:24 +00002734 param.options = 0;
Benny Prijonod65f78c2009-06-03 18:59:37 +00002735 status = open_snd_dev(&param);
2736 if (status != PJ_SUCCESS) {
Benny Prijonoc45d9512010-12-10 11:04:30 +00002737 pjsua_perror(THIS_FILE, "Error opening sound device",
2738 status);
Benny Prijonob90fd382011-09-18 14:59:56 +00002739 goto on_return;
Benny Prijonod65f78c2009-06-03 18:59:37 +00002740 }
2741 } else {
2742 /* Null-audio */
Benny Prijonoc45d9512010-12-10 11:04:30 +00002743 status = pjsua_set_snd_dev(pjsua_var.cap_dev,
2744 pjsua_var.play_dev);
Benny Prijonod65f78c2009-06-03 18:59:37 +00002745 if (status != PJ_SUCCESS) {
Benny Prijonoc45d9512010-12-10 11:04:30 +00002746 pjsua_perror(THIS_FILE, "Error opening sound device",
2747 status);
Benny Prijonob90fd382011-09-18 14:59:56 +00002748 goto on_return;
Benny Prijonod65f78c2009-06-03 18:59:37 +00002749 }
Nanang Izzuddinfe02a062009-02-18 14:28:49 +00002750 }
Benny Prijono2d647722011-07-13 03:05:22 +00002751 } else if (pjsua_var.no_snd) {
2752 if (!pjsua_var.snd_is_on) {
2753 pjsua_var.snd_is_on = PJ_TRUE;
2754 /* Notify app */
2755 if (pjsua_var.ua_cfg.cb.on_snd_dev_operation) {
2756 (*pjsua_var.ua_cfg.cb.on_snd_dev_operation)(1);
2757 }
2758 }
Nanang Izzuddinfe02a062009-02-18 14:28:49 +00002759 }
Nanang Izzuddinfe02a062009-02-18 14:28:49 +00002760
Benny Prijonof798e502009-03-09 13:08:16 +00002761 } else {
2762 /* The bridge version */
Nanang Izzuddinfe02a062009-02-18 14:28:49 +00002763
Benny Prijonof798e502009-03-09 13:08:16 +00002764 /* Create sound port if none is instantiated */
2765 if (pjsua_var.snd_port==NULL && pjsua_var.null_snd==NULL &&
2766 !pjsua_var.no_snd)
2767 {
2768 pj_status_t status;
Nanang Izzuddin68559c32008-06-13 17:01:46 +00002769
Benny Prijonof798e502009-03-09 13:08:16 +00002770 status = pjsua_set_snd_dev(pjsua_var.cap_dev, pjsua_var.play_dev);
2771 if (status != PJ_SUCCESS) {
2772 pjsua_perror(THIS_FILE, "Error opening sound device", status);
Benny Prijonob90fd382011-09-18 14:59:56 +00002773 goto on_return;
Benny Prijonof798e502009-03-09 13:08:16 +00002774 }
Benny Prijono2d647722011-07-13 03:05:22 +00002775 } else if (pjsua_var.no_snd && !pjsua_var.snd_is_on) {
2776 pjsua_var.snd_is_on = PJ_TRUE;
2777 /* Notify app */
2778 if (pjsua_var.ua_cfg.cb.on_snd_dev_operation) {
2779 (*pjsua_var.ua_cfg.cb.on_snd_dev_operation)(1);
2780 }
Nanang Izzuddin68559c32008-06-13 17:01:46 +00002781 }
Benny Prijonof798e502009-03-09 13:08:16 +00002782 }
Nanang Izzuddinfe02a062009-02-18 14:28:49 +00002783
Benny Prijonob90fd382011-09-18 14:59:56 +00002784 status = pjmedia_conf_connect_port(pjsua_var.mconf, source, sink, 0);
2785
2786on_return:
2787 pj_log_pop_indent();
2788 return status;
Benny Prijonoeebe9af2006-06-13 22:57:13 +00002789}
2790
2791
2792/*
2793 * Disconnect media flow from the source to destination port.
2794 */
2795PJ_DEF(pj_status_t) pjsua_conf_disconnect( pjsua_conf_port_id source,
2796 pjsua_conf_port_id sink)
2797{
Nanang Izzuddin68559c32008-06-13 17:01:46 +00002798 pj_status_t status;
2799
Benny Prijonob90fd382011-09-18 14:59:56 +00002800 PJ_LOG(4,(THIS_FILE, "%s disconnect: %d -x- %d",
2801 (pjsua_var.is_mswitch ? "Switch" : "Conf"),
2802 source, sink));
2803 pj_log_push_indent();
2804
Nanang Izzuddin68559c32008-06-13 17:01:46 +00002805 status = pjmedia_conf_disconnect_port(pjsua_var.mconf, source, sink);
Nanang Izzuddin148fd392008-06-16 09:52:50 +00002806 check_snd_dev_idle();
Nanang Izzuddin68559c32008-06-13 17:01:46 +00002807
Benny Prijonob90fd382011-09-18 14:59:56 +00002808 pj_log_pop_indent();
Nanang Izzuddin68559c32008-06-13 17:01:46 +00002809 return status;
Benny Prijonoeebe9af2006-06-13 22:57:13 +00002810}
2811
2812
Benny Prijono6dd967c2006-12-26 02:27:14 +00002813/*
2814 * Adjust the signal level to be transmitted from the bridge to the
2815 * specified port by making it louder or quieter.
2816 */
2817PJ_DEF(pj_status_t) pjsua_conf_adjust_tx_level(pjsua_conf_port_id slot,
2818 float level)
2819{
2820 return pjmedia_conf_adjust_tx_level(pjsua_var.mconf, slot,
2821 (int)((level-1) * 128));
2822}
2823
2824/*
2825 * Adjust the signal level to be received from the specified port (to
2826 * the bridge) by making it louder or quieter.
2827 */
2828PJ_DEF(pj_status_t) pjsua_conf_adjust_rx_level(pjsua_conf_port_id slot,
2829 float level)
2830{
2831 return pjmedia_conf_adjust_rx_level(pjsua_var.mconf, slot,
2832 (int)((level-1) * 128));
2833}
2834
2835
2836/*
2837 * Get last signal level transmitted to or received from the specified port.
2838 */
2839PJ_DEF(pj_status_t) pjsua_conf_get_signal_level(pjsua_conf_port_id slot,
2840 unsigned *tx_level,
2841 unsigned *rx_level)
2842{
2843 return pjmedia_conf_get_signal_level(pjsua_var.mconf, slot,
2844 tx_level, rx_level);
2845}
2846
Benny Prijonoeebe9af2006-06-13 22:57:13 +00002847/*****************************************************************************
2848 * File player.
2849 */
2850
Benny Prijonod5696da2007-07-17 16:25:45 +00002851static char* get_basename(const char *path, unsigned len)
2852{
2853 char *p = ((char*)path) + len;
2854
2855 if (len==0)
2856 return p;
2857
Benny Prijono1f61a8f2007-08-16 10:11:44 +00002858 for (--p; p!=path && *p!='/' && *p!='\\'; ) --p;
Benny Prijonod5696da2007-07-17 16:25:45 +00002859
2860 return (p==path) ? p : p+1;
2861}
2862
2863
Benny Prijonoeebe9af2006-06-13 22:57:13 +00002864/*
2865 * Create a file player, and automatically connect this player to
2866 * the conference bridge.
2867 */
2868PJ_DEF(pj_status_t) pjsua_player_create( const pj_str_t *filename,
2869 unsigned options,
Benny Prijonoeebe9af2006-06-13 22:57:13 +00002870 pjsua_player_id *p_id)
2871{
2872 unsigned slot, file_id;
Benny Prijonoa66c3312007-01-21 23:12:40 +00002873 char path[PJ_MAXPATH];
Benny Prijonob90fd382011-09-18 14:59:56 +00002874 pj_pool_t *pool = NULL;
Benny Prijonoeebe9af2006-06-13 22:57:13 +00002875 pjmedia_port *port;
Benny Prijonob90fd382011-09-18 14:59:56 +00002876 pj_status_t status = PJ_SUCCESS;
Benny Prijonoeebe9af2006-06-13 22:57:13 +00002877
2878 if (pjsua_var.player_cnt >= PJ_ARRAY_SIZE(pjsua_var.player))
2879 return PJ_ETOOMANY;
2880
Benny Prijonob90fd382011-09-18 14:59:56 +00002881 PJ_LOG(4,(THIS_FILE, "Creating file player: %.*s..",
2882 (int)filename->slen, filename->ptr));
2883 pj_log_push_indent();
2884
Benny Prijonoeebe9af2006-06-13 22:57:13 +00002885 PJSUA_LOCK();
2886
2887 for (file_id=0; file_id<PJ_ARRAY_SIZE(pjsua_var.player); ++file_id) {
2888 if (pjsua_var.player[file_id].port == NULL)
2889 break;
2890 }
2891
2892 if (file_id == PJ_ARRAY_SIZE(pjsua_var.player)) {
2893 /* This is unexpected */
Benny Prijonoeebe9af2006-06-13 22:57:13 +00002894 pj_assert(0);
Benny Prijonob90fd382011-09-18 14:59:56 +00002895 status = PJ_EBUG;
2896 goto on_error;
Benny Prijonoeebe9af2006-06-13 22:57:13 +00002897 }
2898
2899 pj_memcpy(path, filename->ptr, filename->slen);
2900 path[filename->slen] = '\0';
Benny Prijonod5696da2007-07-17 16:25:45 +00002901
2902 pool = pjsua_pool_create(get_basename(path, filename->slen), 1000, 1000);
2903 if (!pool) {
Benny Prijonob90fd382011-09-18 14:59:56 +00002904 status = PJ_ENOMEM;
2905 goto on_error;
Benny Prijonod5696da2007-07-17 16:25:45 +00002906 }
2907
Nanang Izzuddin81e9bd52008-06-27 12:52:51 +00002908 status = pjmedia_wav_player_port_create(
2909 pool, path,
2910 pjsua_var.mconf_cfg.samples_per_frame *
Benny Prijonoc45d9512010-12-10 11:04:30 +00002911 1000 / pjsua_var.media_cfg.channel_count /
Nanang Izzuddin81e9bd52008-06-27 12:52:51 +00002912 pjsua_var.media_cfg.clock_rate,
2913 options, 0, &port);
Benny Prijonoeebe9af2006-06-13 22:57:13 +00002914 if (status != PJ_SUCCESS) {
Benny Prijonoeebe9af2006-06-13 22:57:13 +00002915 pjsua_perror(THIS_FILE, "Unable to open file for playback", status);
Benny Prijonob90fd382011-09-18 14:59:56 +00002916 goto on_error;
Benny Prijonoeebe9af2006-06-13 22:57:13 +00002917 }
2918
Benny Prijono5297af92008-03-18 13:40:40 +00002919 status = pjmedia_conf_add_port(pjsua_var.mconf, pool,
Benny Prijonoeebe9af2006-06-13 22:57:13 +00002920 port, filename, &slot);
2921 if (status != PJ_SUCCESS) {
2922 pjmedia_port_destroy(port);
Benny Prijono32e4f492007-01-24 00:44:26 +00002923 pjsua_perror(THIS_FILE, "Unable to add file to conference bridge",
2924 status);
Benny Prijonob90fd382011-09-18 14:59:56 +00002925 goto on_error;
Benny Prijonoeebe9af2006-06-13 22:57:13 +00002926 }
2927
Benny Prijonoa66c3312007-01-21 23:12:40 +00002928 pjsua_var.player[file_id].type = 0;
Benny Prijonod5696da2007-07-17 16:25:45 +00002929 pjsua_var.player[file_id].pool = pool;
Benny Prijonoeebe9af2006-06-13 22:57:13 +00002930 pjsua_var.player[file_id].port = port;
2931 pjsua_var.player[file_id].slot = slot;
2932
2933 if (p_id) *p_id = file_id;
2934
2935 ++pjsua_var.player_cnt;
2936
2937 PJSUA_UNLOCK();
Benny Prijonob90fd382011-09-18 14:59:56 +00002938
2939 PJ_LOG(4,(THIS_FILE, "Player created, id=%d, slot=%d", file_id, slot));
2940
2941 pj_log_pop_indent();
Benny Prijonoeebe9af2006-06-13 22:57:13 +00002942 return PJ_SUCCESS;
Benny Prijonob90fd382011-09-18 14:59:56 +00002943
2944on_error:
2945 PJSUA_UNLOCK();
2946 if (pool) pj_pool_release(pool);
2947 pj_log_pop_indent();
2948 return status;
Benny Prijonoeebe9af2006-06-13 22:57:13 +00002949}
2950
2951
2952/*
Benny Prijonoa66c3312007-01-21 23:12:40 +00002953 * Create a file playlist media port, and automatically add the port
2954 * to the conference bridge.
2955 */
2956PJ_DEF(pj_status_t) pjsua_playlist_create( const pj_str_t file_names[],
2957 unsigned file_count,
2958 const pj_str_t *label,
2959 unsigned options,
2960 pjsua_player_id *p_id)
2961{
2962 unsigned slot, file_id, ptime;
Benny Prijonob90fd382011-09-18 14:59:56 +00002963 pj_pool_t *pool = NULL;
Benny Prijonoa66c3312007-01-21 23:12:40 +00002964 pjmedia_port *port;
Benny Prijonob90fd382011-09-18 14:59:56 +00002965 pj_status_t status = PJ_SUCCESS;
Benny Prijonoa66c3312007-01-21 23:12:40 +00002966
2967 if (pjsua_var.player_cnt >= PJ_ARRAY_SIZE(pjsua_var.player))
2968 return PJ_ETOOMANY;
2969
Benny Prijonob90fd382011-09-18 14:59:56 +00002970 PJ_LOG(4,(THIS_FILE, "Creating playlist with %d file(s)..", file_count));
2971 pj_log_push_indent();
2972
Benny Prijonoa66c3312007-01-21 23:12:40 +00002973 PJSUA_LOCK();
2974
2975 for (file_id=0; file_id<PJ_ARRAY_SIZE(pjsua_var.player); ++file_id) {
2976 if (pjsua_var.player[file_id].port == NULL)
2977 break;
2978 }
2979
2980 if (file_id == PJ_ARRAY_SIZE(pjsua_var.player)) {
2981 /* This is unexpected */
Benny Prijonoa66c3312007-01-21 23:12:40 +00002982 pj_assert(0);
Benny Prijonob90fd382011-09-18 14:59:56 +00002983 status = PJ_EBUG;
2984 goto on_error;
Benny Prijonoa66c3312007-01-21 23:12:40 +00002985 }
2986
2987
2988 ptime = pjsua_var.mconf_cfg.samples_per_frame * 1000 /
2989 pjsua_var.media_cfg.clock_rate;
2990
Benny Prijonod5696da2007-07-17 16:25:45 +00002991 pool = pjsua_pool_create("playlist", 1000, 1000);
2992 if (!pool) {
Benny Prijonob90fd382011-09-18 14:59:56 +00002993 status = PJ_ENOMEM;
2994 goto on_error;
Benny Prijonod5696da2007-07-17 16:25:45 +00002995 }
2996
2997 status = pjmedia_wav_playlist_create(pool, label,
Benny Prijonoa66c3312007-01-21 23:12:40 +00002998 file_names, file_count,
2999 ptime, options, 0, &port);
3000 if (status != PJ_SUCCESS) {
Benny Prijonoa66c3312007-01-21 23:12:40 +00003001 pjsua_perror(THIS_FILE, "Unable to create playlist", status);
Benny Prijonob90fd382011-09-18 14:59:56 +00003002 goto on_error;
Benny Prijonoa66c3312007-01-21 23:12:40 +00003003 }
3004
Benny Prijonod5696da2007-07-17 16:25:45 +00003005 status = pjmedia_conf_add_port(pjsua_var.mconf, pool,
Benny Prijonoa66c3312007-01-21 23:12:40 +00003006 port, &port->info.name, &slot);
3007 if (status != PJ_SUCCESS) {
3008 pjmedia_port_destroy(port);
Benny Prijonoa66c3312007-01-21 23:12:40 +00003009 pjsua_perror(THIS_FILE, "Unable to add port", status);
Benny Prijonob90fd382011-09-18 14:59:56 +00003010 goto on_error;
Benny Prijonoa66c3312007-01-21 23:12:40 +00003011 }
3012
3013 pjsua_var.player[file_id].type = 1;
Benny Prijonod5696da2007-07-17 16:25:45 +00003014 pjsua_var.player[file_id].pool = pool;
Benny Prijonoa66c3312007-01-21 23:12:40 +00003015 pjsua_var.player[file_id].port = port;
3016 pjsua_var.player[file_id].slot = slot;
3017
3018 if (p_id) *p_id = file_id;
3019
3020 ++pjsua_var.player_cnt;
3021
3022 PJSUA_UNLOCK();
Benny Prijonob90fd382011-09-18 14:59:56 +00003023
3024 PJ_LOG(4,(THIS_FILE, "Playlist created, id=%d, slot=%d", file_id, slot));
3025
3026 pj_log_pop_indent();
3027
Benny Prijonoa66c3312007-01-21 23:12:40 +00003028 return PJ_SUCCESS;
3029
Benny Prijonob90fd382011-09-18 14:59:56 +00003030on_error:
3031 PJSUA_UNLOCK();
3032 if (pool) pj_pool_release(pool);
3033 pj_log_pop_indent();
3034
3035 return status;
Benny Prijonoa66c3312007-01-21 23:12:40 +00003036}
3037
3038
3039/*
Benny Prijonoeebe9af2006-06-13 22:57:13 +00003040 * Get conference port ID associated with player.
3041 */
3042PJ_DEF(pjsua_conf_port_id) pjsua_player_get_conf_port(pjsua_player_id id)
3043{
Benny Prijonoa1e69682007-05-11 15:14:34 +00003044 PJ_ASSERT_RETURN(id>=0&&id<(int)PJ_ARRAY_SIZE(pjsua_var.player), PJ_EINVAL);
Benny Prijonoeebe9af2006-06-13 22:57:13 +00003045 PJ_ASSERT_RETURN(pjsua_var.player[id].port != NULL, PJ_EINVAL);
3046
3047 return pjsua_var.player[id].slot;
3048}
3049
Benny Prijono469b1522006-12-26 03:05:17 +00003050/*
3051 * Get the media port for the player.
3052 */
Benny Prijonobe41d862008-01-18 13:24:28 +00003053PJ_DEF(pj_status_t) pjsua_player_get_port( pjsua_player_id id,
Benny Prijono469b1522006-12-26 03:05:17 +00003054 pjmedia_port **p_port)
3055{
Benny Prijonoa1e69682007-05-11 15:14:34 +00003056 PJ_ASSERT_RETURN(id>=0&&id<(int)PJ_ARRAY_SIZE(pjsua_var.player), PJ_EINVAL);
Benny Prijono469b1522006-12-26 03:05:17 +00003057 PJ_ASSERT_RETURN(pjsua_var.player[id].port != NULL, PJ_EINVAL);
3058 PJ_ASSERT_RETURN(p_port != NULL, PJ_EINVAL);
3059
3060 *p_port = pjsua_var.player[id].port;
3061
3062 return PJ_SUCCESS;
3063}
Benny Prijonoeebe9af2006-06-13 22:57:13 +00003064
3065/*
3066 * Set playback position.
3067 */
3068PJ_DEF(pj_status_t) pjsua_player_set_pos( pjsua_player_id id,
3069 pj_uint32_t samples)
3070{
Benny Prijonoa1e69682007-05-11 15:14:34 +00003071 PJ_ASSERT_RETURN(id>=0&&id<(int)PJ_ARRAY_SIZE(pjsua_var.player), PJ_EINVAL);
Benny Prijonoeebe9af2006-06-13 22:57:13 +00003072 PJ_ASSERT_RETURN(pjsua_var.player[id].port != NULL, PJ_EINVAL);
Benny Prijonoa66c3312007-01-21 23:12:40 +00003073 PJ_ASSERT_RETURN(pjsua_var.player[id].type == 0, PJ_EINVAL);
Benny Prijonoeebe9af2006-06-13 22:57:13 +00003074
3075 return pjmedia_wav_player_port_set_pos(pjsua_var.player[id].port, samples);
3076}
3077
3078
3079/*
3080 * Close the file, remove the player from the bridge, and free
3081 * resources associated with the file player.
3082 */
3083PJ_DEF(pj_status_t) pjsua_player_destroy(pjsua_player_id id)
3084{
Benny Prijonoa1e69682007-05-11 15:14:34 +00003085 PJ_ASSERT_RETURN(id>=0&&id<(int)PJ_ARRAY_SIZE(pjsua_var.player), PJ_EINVAL);
Benny Prijonoeebe9af2006-06-13 22:57:13 +00003086 PJ_ASSERT_RETURN(pjsua_var.player[id].port != NULL, PJ_EINVAL);
3087
Benny Prijonob90fd382011-09-18 14:59:56 +00003088 PJ_LOG(4,(THIS_FILE, "Destroying player %d..", id));
3089 pj_log_push_indent();
3090
Benny Prijonoeebe9af2006-06-13 22:57:13 +00003091 PJSUA_LOCK();
3092
3093 if (pjsua_var.player[id].port) {
Nanang Izzuddin148fd392008-06-16 09:52:50 +00003094 pjsua_conf_remove_port(pjsua_var.player[id].slot);
Benny Prijonoeebe9af2006-06-13 22:57:13 +00003095 pjmedia_port_destroy(pjsua_var.player[id].port);
3096 pjsua_var.player[id].port = NULL;
3097 pjsua_var.player[id].slot = 0xFFFF;
Benny Prijonod5696da2007-07-17 16:25:45 +00003098 pj_pool_release(pjsua_var.player[id].pool);
3099 pjsua_var.player[id].pool = NULL;
Benny Prijonoeebe9af2006-06-13 22:57:13 +00003100 pjsua_var.player_cnt--;
3101 }
3102
3103 PJSUA_UNLOCK();
Benny Prijonob90fd382011-09-18 14:59:56 +00003104 pj_log_pop_indent();
Benny Prijonoeebe9af2006-06-13 22:57:13 +00003105
3106 return PJ_SUCCESS;
3107}
3108
3109
3110/*****************************************************************************
3111 * File recorder.
3112 */
3113
3114/*
3115 * Create a file recorder, and automatically connect this recorder to
3116 * the conference bridge.
3117 */
3118PJ_DEF(pj_status_t) pjsua_recorder_create( const pj_str_t *filename,
Benny Prijono8f310522006-10-20 11:08:49 +00003119 unsigned enc_type,
3120 void *enc_param,
Benny Prijonoeebe9af2006-06-13 22:57:13 +00003121 pj_ssize_t max_size,
3122 unsigned options,
Benny Prijonoeebe9af2006-06-13 22:57:13 +00003123 pjsua_recorder_id *p_id)
3124{
Benny Prijonob3cdb2b2006-10-19 15:49:47 +00003125 enum Format
3126 {
3127 FMT_UNKNOWN,
3128 FMT_WAV,
3129 FMT_MP3,
3130 };
Benny Prijonoeebe9af2006-06-13 22:57:13 +00003131 unsigned slot, file_id;
Benny Prijonoa66c3312007-01-21 23:12:40 +00003132 char path[PJ_MAXPATH];
Benny Prijonob3cdb2b2006-10-19 15:49:47 +00003133 pj_str_t ext;
Benny Prijono8f310522006-10-20 11:08:49 +00003134 int file_format;
Benny Prijonob90fd382011-09-18 14:59:56 +00003135 pj_pool_t *pool = NULL;
Benny Prijonoeebe9af2006-06-13 22:57:13 +00003136 pjmedia_port *port;
Benny Prijonob90fd382011-09-18 14:59:56 +00003137 pj_status_t status = PJ_SUCCESS;
Benny Prijonoeebe9af2006-06-13 22:57:13 +00003138
Benny Prijonob3cdb2b2006-10-19 15:49:47 +00003139 /* Filename must present */
3140 PJ_ASSERT_RETURN(filename != NULL, PJ_EINVAL);
3141
Benny Prijono00cae612006-07-31 15:19:36 +00003142 /* Don't support max_size at present */
Benny Prijonob3cdb2b2006-10-19 15:49:47 +00003143 PJ_ASSERT_RETURN(max_size == 0 || max_size == -1, PJ_EINVAL);
Benny Prijono00cae612006-07-31 15:19:36 +00003144
Benny Prijono8f310522006-10-20 11:08:49 +00003145 /* Don't support encoding type at present */
3146 PJ_ASSERT_RETURN(enc_type == 0, PJ_EINVAL);
Benny Prijono00cae612006-07-31 15:19:36 +00003147
Benny Prijonob90fd382011-09-18 14:59:56 +00003148 PJ_LOG(4,(THIS_FILE, "Creating recorder %.*s..",
3149 (int)filename->slen, filename->ptr));
3150 pj_log_push_indent();
3151
3152 if (pjsua_var.rec_cnt >= PJ_ARRAY_SIZE(pjsua_var.recorder)) {
3153 pj_log_pop_indent();
Benny Prijonoeebe9af2006-06-13 22:57:13 +00003154 return PJ_ETOOMANY;
Benny Prijonob90fd382011-09-18 14:59:56 +00003155 }
Benny Prijonoeebe9af2006-06-13 22:57:13 +00003156
Benny Prijonob3cdb2b2006-10-19 15:49:47 +00003157 /* Determine the file format */
3158 ext.ptr = filename->ptr + filename->slen - 4;
3159 ext.slen = 4;
3160
3161 if (pj_stricmp2(&ext, ".wav") == 0)
3162 file_format = FMT_WAV;
3163 else if (pj_stricmp2(&ext, ".mp3") == 0)
3164 file_format = FMT_MP3;
3165 else {
3166 PJ_LOG(1,(THIS_FILE, "pjsua_recorder_create() error: unable to "
3167 "determine file format for %.*s",
3168 (int)filename->slen, filename->ptr));
Benny Prijonob90fd382011-09-18 14:59:56 +00003169 pj_log_pop_indent();
Benny Prijonob3cdb2b2006-10-19 15:49:47 +00003170 return PJ_ENOTSUP;
3171 }
3172
Benny Prijonoeebe9af2006-06-13 22:57:13 +00003173 PJSUA_LOCK();
3174
3175 for (file_id=0; file_id<PJ_ARRAY_SIZE(pjsua_var.recorder); ++file_id) {
3176 if (pjsua_var.recorder[file_id].port == NULL)
3177 break;
3178 }
3179
3180 if (file_id == PJ_ARRAY_SIZE(pjsua_var.recorder)) {
3181 /* This is unexpected */
Benny Prijonoeebe9af2006-06-13 22:57:13 +00003182 pj_assert(0);
Benny Prijonob90fd382011-09-18 14:59:56 +00003183 status = PJ_EBUG;
3184 goto on_return;
Benny Prijonoeebe9af2006-06-13 22:57:13 +00003185 }
3186
3187 pj_memcpy(path, filename->ptr, filename->slen);
3188 path[filename->slen] = '\0';
Benny Prijonob3cdb2b2006-10-19 15:49:47 +00003189
Benny Prijonod5696da2007-07-17 16:25:45 +00003190 pool = pjsua_pool_create(get_basename(path, filename->slen), 1000, 1000);
3191 if (!pool) {
Benny Prijonob90fd382011-09-18 14:59:56 +00003192 status = PJ_ENOMEM;
3193 goto on_return;
Benny Prijonod5696da2007-07-17 16:25:45 +00003194 }
3195
Benny Prijonob3cdb2b2006-10-19 15:49:47 +00003196 if (file_format == FMT_WAV) {
Benny Prijonod5696da2007-07-17 16:25:45 +00003197 status = pjmedia_wav_writer_port_create(pool, path,
Benny Prijonob3cdb2b2006-10-19 15:49:47 +00003198 pjsua_var.media_cfg.clock_rate,
3199 pjsua_var.mconf_cfg.channel_count,
3200 pjsua_var.mconf_cfg.samples_per_frame,
3201 pjsua_var.mconf_cfg.bits_per_sample,
3202 options, 0, &port);
Benny Prijonob3cdb2b2006-10-19 15:49:47 +00003203 } else {
Benny Prijonoc95a0f02007-04-09 07:06:08 +00003204 PJ_UNUSED_ARG(enc_param);
Benny Prijonob3cdb2b2006-10-19 15:49:47 +00003205 port = NULL;
3206 status = PJ_ENOTSUP;
3207 }
3208
Benny Prijonoeebe9af2006-06-13 22:57:13 +00003209 if (status != PJ_SUCCESS) {
Benny Prijonoeebe9af2006-06-13 22:57:13 +00003210 pjsua_perror(THIS_FILE, "Unable to open file for recording", status);
Benny Prijonob90fd382011-09-18 14:59:56 +00003211 goto on_return;
Benny Prijonoeebe9af2006-06-13 22:57:13 +00003212 }
3213
Benny Prijonod5696da2007-07-17 16:25:45 +00003214 status = pjmedia_conf_add_port(pjsua_var.mconf, pool,
Benny Prijonoeebe9af2006-06-13 22:57:13 +00003215 port, filename, &slot);
3216 if (status != PJ_SUCCESS) {
3217 pjmedia_port_destroy(port);
Benny Prijonob90fd382011-09-18 14:59:56 +00003218 goto on_return;
Benny Prijonoeebe9af2006-06-13 22:57:13 +00003219 }
3220
3221 pjsua_var.recorder[file_id].port = port;
3222 pjsua_var.recorder[file_id].slot = slot;
Benny Prijonod5696da2007-07-17 16:25:45 +00003223 pjsua_var.recorder[file_id].pool = pool;
Benny Prijonoeebe9af2006-06-13 22:57:13 +00003224
3225 if (p_id) *p_id = file_id;
3226
3227 ++pjsua_var.rec_cnt;
3228
3229 PJSUA_UNLOCK();
Benny Prijonob90fd382011-09-18 14:59:56 +00003230
3231 PJ_LOG(4,(THIS_FILE, "Recorder created, id=%d, slot=%d", file_id, slot));
3232
3233 pj_log_pop_indent();
Benny Prijonoeebe9af2006-06-13 22:57:13 +00003234 return PJ_SUCCESS;
Benny Prijonob90fd382011-09-18 14:59:56 +00003235
3236on_return:
3237 PJSUA_UNLOCK();
3238 if (pool) pj_pool_release(pool);
3239 pj_log_pop_indent();
3240 return status;
Benny Prijonoeebe9af2006-06-13 22:57:13 +00003241}
3242
3243
3244/*
3245 * Get conference port associated with recorder.
3246 */
3247PJ_DEF(pjsua_conf_port_id) pjsua_recorder_get_conf_port(pjsua_recorder_id id)
3248{
Benny Prijonoa1e69682007-05-11 15:14:34 +00003249 PJ_ASSERT_RETURN(id>=0 && id<(int)PJ_ARRAY_SIZE(pjsua_var.recorder),
3250 PJ_EINVAL);
Benny Prijonoeebe9af2006-06-13 22:57:13 +00003251 PJ_ASSERT_RETURN(pjsua_var.recorder[id].port != NULL, PJ_EINVAL);
3252
3253 return pjsua_var.recorder[id].slot;
3254}
3255
Benny Prijono469b1522006-12-26 03:05:17 +00003256/*
3257 * Get the media port for the recorder.
3258 */
3259PJ_DEF(pj_status_t) pjsua_recorder_get_port( pjsua_recorder_id id,
3260 pjmedia_port **p_port)
3261{
Benny Prijonoa1e69682007-05-11 15:14:34 +00003262 PJ_ASSERT_RETURN(id>=0 && id<(int)PJ_ARRAY_SIZE(pjsua_var.recorder),
3263 PJ_EINVAL);
Benny Prijono469b1522006-12-26 03:05:17 +00003264 PJ_ASSERT_RETURN(pjsua_var.recorder[id].port != NULL, PJ_EINVAL);
3265 PJ_ASSERT_RETURN(p_port != NULL, PJ_EINVAL);
3266
3267 *p_port = pjsua_var.recorder[id].port;
3268 return PJ_SUCCESS;
3269}
Benny Prijonoeebe9af2006-06-13 22:57:13 +00003270
3271/*
3272 * Destroy recorder (this will complete recording).
3273 */
3274PJ_DEF(pj_status_t) pjsua_recorder_destroy(pjsua_recorder_id id)
3275{
Benny Prijonoa1e69682007-05-11 15:14:34 +00003276 PJ_ASSERT_RETURN(id>=0 && id<(int)PJ_ARRAY_SIZE(pjsua_var.recorder),
3277 PJ_EINVAL);
Benny Prijonoeebe9af2006-06-13 22:57:13 +00003278 PJ_ASSERT_RETURN(pjsua_var.recorder[id].port != NULL, PJ_EINVAL);
3279
Benny Prijonob90fd382011-09-18 14:59:56 +00003280 PJ_LOG(4,(THIS_FILE, "Destroying recorder %d..", id));
3281 pj_log_push_indent();
3282
Benny Prijonoeebe9af2006-06-13 22:57:13 +00003283 PJSUA_LOCK();
3284
3285 if (pjsua_var.recorder[id].port) {
Nanang Izzuddin148fd392008-06-16 09:52:50 +00003286 pjsua_conf_remove_port(pjsua_var.recorder[id].slot);
Benny Prijonoeebe9af2006-06-13 22:57:13 +00003287 pjmedia_port_destroy(pjsua_var.recorder[id].port);
3288 pjsua_var.recorder[id].port = NULL;
3289 pjsua_var.recorder[id].slot = 0xFFFF;
Benny Prijonod5696da2007-07-17 16:25:45 +00003290 pj_pool_release(pjsua_var.recorder[id].pool);
3291 pjsua_var.recorder[id].pool = NULL;
Benny Prijonoeebe9af2006-06-13 22:57:13 +00003292 pjsua_var.rec_cnt--;
3293 }
3294
3295 PJSUA_UNLOCK();
Benny Prijonob90fd382011-09-18 14:59:56 +00003296 pj_log_pop_indent();
Benny Prijonoeebe9af2006-06-13 22:57:13 +00003297
3298 return PJ_SUCCESS;
3299}
3300
3301
3302/*****************************************************************************
3303 * Sound devices.
3304 */
3305
3306/*
3307 * Enum sound devices.
3308 */
Benny Prijonof798e502009-03-09 13:08:16 +00003309
3310PJ_DEF(pj_status_t) pjsua_enum_aud_devs( pjmedia_aud_dev_info info[],
Benny Prijonoeebe9af2006-06-13 22:57:13 +00003311 unsigned *count)
3312{
3313 unsigned i, dev_count;
3314
Benny Prijono10454dc2009-02-21 14:21:59 +00003315 dev_count = pjmedia_aud_dev_count();
Benny Prijonoeebe9af2006-06-13 22:57:13 +00003316
3317 if (dev_count > *count) dev_count = *count;
3318
3319 for (i=0; i<dev_count; ++i) {
Benny Prijono10454dc2009-02-21 14:21:59 +00003320 pj_status_t status;
Benny Prijonoeebe9af2006-06-13 22:57:13 +00003321
Benny Prijono10454dc2009-02-21 14:21:59 +00003322 status = pjmedia_aud_dev_get_info(i, &info[i]);
3323 if (status != PJ_SUCCESS)
3324 return status;
Benny Prijonoeebe9af2006-06-13 22:57:13 +00003325 }
3326
3327 *count = dev_count;
3328
3329 return PJ_SUCCESS;
3330}
Benny Prijonof798e502009-03-09 13:08:16 +00003331
3332
Benny Prijono10454dc2009-02-21 14:21:59 +00003333PJ_DEF(pj_status_t) pjsua_enum_snd_devs( pjmedia_snd_dev_info info[],
3334 unsigned *count)
3335{
3336 unsigned i, dev_count;
3337
3338 dev_count = pjmedia_aud_dev_count();
3339
3340 if (dev_count > *count) dev_count = *count;
3341 pj_bzero(info, dev_count * sizeof(pjmedia_snd_dev_info));
3342
3343 for (i=0; i<dev_count; ++i) {
3344 pjmedia_aud_dev_info ai;
3345 pj_status_t status;
3346
3347 status = pjmedia_aud_dev_get_info(i, &ai);
3348 if (status != PJ_SUCCESS)
3349 return status;
3350
3351 strncpy(info[i].name, ai.name, sizeof(info[i].name));
3352 info[i].name[sizeof(info[i].name)-1] = '\0';
3353 info[i].input_count = ai.input_count;
3354 info[i].output_count = ai.output_count;
3355 info[i].default_samples_per_sec = ai.default_samples_per_sec;
3356 }
3357
3358 *count = dev_count;
3359
3360 return PJ_SUCCESS;
3361}
Benny Prijono10454dc2009-02-21 14:21:59 +00003362
Benny Prijonof798e502009-03-09 13:08:16 +00003363/* Create audio device parameter to open the device */
3364static pj_status_t create_aud_param(pjmedia_aud_param *param,
3365 pjmedia_aud_dev_index capture_dev,
3366 pjmedia_aud_dev_index playback_dev,
3367 unsigned clock_rate,
3368 unsigned channel_count,
3369 unsigned samples_per_frame,
3370 unsigned bits_per_sample)
Benny Prijonoeebe9af2006-06-13 22:57:13 +00003371{
Nanang Izzuddin8465c682009-03-04 17:23:25 +00003372 pj_status_t status;
3373
Benny Prijono96e74f32009-02-22 12:00:12 +00003374 /* Normalize device ID with new convention about default device ID */
3375 if (playback_dev == PJMEDIA_AUD_DEFAULT_CAPTURE_DEV)
3376 playback_dev = PJMEDIA_AUD_DEFAULT_PLAYBACK_DEV;
3377
Benny Prijono10454dc2009-02-21 14:21:59 +00003378 /* Create default parameters for the device */
Benny Prijonof798e502009-03-09 13:08:16 +00003379 status = pjmedia_aud_dev_default_param(capture_dev, param);
Benny Prijono10454dc2009-02-21 14:21:59 +00003380 if (status != PJ_SUCCESS) {
Benny Prijono96e74f32009-02-22 12:00:12 +00003381 pjsua_perror(THIS_FILE, "Error retrieving default audio "
3382 "device parameters", status);
Benny Prijono10454dc2009-02-21 14:21:59 +00003383 return status;
3384 }
Benny Prijonof798e502009-03-09 13:08:16 +00003385 param->dir = PJMEDIA_DIR_CAPTURE_PLAYBACK;
3386 param->rec_id = capture_dev;
3387 param->play_id = playback_dev;
3388 param->clock_rate = clock_rate;
3389 param->channel_count = channel_count;
3390 param->samples_per_frame = samples_per_frame;
3391 param->bits_per_sample = bits_per_sample;
3392
3393 /* Update the setting with user preference */
3394#define update_param(cap, field) \
3395 if (pjsua_var.aud_param.flags & cap) { \
3396 param->flags |= cap; \
3397 param->field = pjsua_var.aud_param.field; \
3398 }
3399 update_param( PJMEDIA_AUD_DEV_CAP_INPUT_VOLUME_SETTING, input_vol);
3400 update_param( PJMEDIA_AUD_DEV_CAP_OUTPUT_VOLUME_SETTING, output_vol);
3401 update_param( PJMEDIA_AUD_DEV_CAP_INPUT_ROUTE, input_route);
3402 update_param( PJMEDIA_AUD_DEV_CAP_OUTPUT_ROUTE, output_route);
3403#undef update_param
3404
Benny Prijono10454dc2009-02-21 14:21:59 +00003405 /* Latency settings */
Benny Prijonof798e502009-03-09 13:08:16 +00003406 param->flags |= (PJMEDIA_AUD_DEV_CAP_INPUT_LATENCY |
3407 PJMEDIA_AUD_DEV_CAP_OUTPUT_LATENCY);
3408 param->input_latency_ms = pjsua_var.media_cfg.snd_rec_latency;
3409 param->output_latency_ms = pjsua_var.media_cfg.snd_play_latency;
3410
Benny Prijono10454dc2009-02-21 14:21:59 +00003411 /* EC settings */
3412 if (pjsua_var.media_cfg.ec_tail_len) {
Benny Prijonof798e502009-03-09 13:08:16 +00003413 param->flags |= (PJMEDIA_AUD_DEV_CAP_EC | PJMEDIA_AUD_DEV_CAP_EC_TAIL);
3414 param->ec_enabled = PJ_TRUE;
3415 param->ec_tail_ms = pjsua_var.media_cfg.ec_tail_len;
Benny Prijono10454dc2009-02-21 14:21:59 +00003416 } else {
Benny Prijonof798e502009-03-09 13:08:16 +00003417 param->flags &= ~(PJMEDIA_AUD_DEV_CAP_EC|PJMEDIA_AUD_DEV_CAP_EC_TAIL);
Benny Prijono10454dc2009-02-21 14:21:59 +00003418 }
3419
Benny Prijonof798e502009-03-09 13:08:16 +00003420 return PJ_SUCCESS;
3421}
Benny Prijono26056d82006-10-11 16:03:41 +00003422
Benny Prijonof798e502009-03-09 13:08:16 +00003423/* Internal: the first time the audio device is opened (during app
3424 * startup), retrieve the audio settings such as volume level
3425 * so that aud_get_settings() will work.
3426 */
3427static pj_status_t update_initial_aud_param()
3428{
3429 pjmedia_aud_stream *strm;
3430 pjmedia_aud_param param;
3431 pj_status_t status;
Benny Prijono26056d82006-10-11 16:03:41 +00003432
Benny Prijonof798e502009-03-09 13:08:16 +00003433 PJ_ASSERT_RETURN(pjsua_var.snd_port != NULL, PJ_EBUG);
Nanang Izzuddin3c1ae632008-08-21 15:04:20 +00003434
Benny Prijonof798e502009-03-09 13:08:16 +00003435 strm = pjmedia_snd_port_get_snd_stream(pjsua_var.snd_port);
Benny Prijono26056d82006-10-11 16:03:41 +00003436
Benny Prijonof798e502009-03-09 13:08:16 +00003437 status = pjmedia_aud_stream_get_param(strm, &param);
Benny Prijonoeebe9af2006-06-13 22:57:13 +00003438 if (status != PJ_SUCCESS) {
Benny Prijonof798e502009-03-09 13:08:16 +00003439 pjsua_perror(THIS_FILE, "Error audio stream "
3440 "device parameters", status);
Benny Prijonoeebe9af2006-06-13 22:57:13 +00003441 return status;
3442 }
3443
Benny Prijonof798e502009-03-09 13:08:16 +00003444#define update_saved_param(cap, field) \
3445 if (param.flags & cap) { \
3446 pjsua_var.aud_param.flags |= cap; \
3447 pjsua_var.aud_param.field = param.field; \
3448 }
3449
3450 update_saved_param(PJMEDIA_AUD_DEV_CAP_INPUT_VOLUME_SETTING, input_vol);
3451 update_saved_param(PJMEDIA_AUD_DEV_CAP_OUTPUT_VOLUME_SETTING, output_vol);
3452 update_saved_param(PJMEDIA_AUD_DEV_CAP_INPUT_ROUTE, input_route);
3453 update_saved_param(PJMEDIA_AUD_DEV_CAP_OUTPUT_ROUTE, output_route);
3454#undef update_saved_param
3455
3456 return PJ_SUCCESS;
3457}
3458
3459/* Get format name */
3460static const char *get_fmt_name(pj_uint32_t id)
3461{
3462 static char name[8];
3463
3464 if (id == PJMEDIA_FORMAT_L16)
3465 return "PCM";
3466 pj_memcpy(name, &id, 4);
3467 name[4] = '\0';
3468 return name;
3469}
3470
3471/* Open sound device with the setting. */
Sauw Ming98766c72011-03-11 06:57:24 +00003472static pj_status_t open_snd_dev(pjmedia_snd_port_param *param)
Benny Prijonof798e502009-03-09 13:08:16 +00003473{
3474 pjmedia_port *conf_port;
3475 pj_status_t status;
3476
3477 PJ_ASSERT_RETURN(param, PJ_EINVAL);
3478
3479 /* Check if NULL sound device is used */
Sauw Ming98766c72011-03-11 06:57:24 +00003480 if (NULL_SND_DEV_ID==param->base.rec_id ||
3481 NULL_SND_DEV_ID==param->base.play_id)
3482 {
Benny Prijonof798e502009-03-09 13:08:16 +00003483 return pjsua_set_null_snd_dev();
3484 }
3485
3486 /* Close existing sound port */
3487 close_snd_dev();
3488
Benny Prijono2d647722011-07-13 03:05:22 +00003489 /* Notify app */
3490 if (pjsua_var.ua_cfg.cb.on_snd_dev_operation) {
3491 (*pjsua_var.ua_cfg.cb.on_snd_dev_operation)(1);
3492 }
3493
Benny Prijonof798e502009-03-09 13:08:16 +00003494 /* Create memory pool for sound device. */
3495 pjsua_var.snd_pool = pjsua_pool_create("pjsua_snd", 4000, 4000);
3496 PJ_ASSERT_RETURN(pjsua_var.snd_pool, PJ_ENOMEM);
3497
3498
3499 PJ_LOG(4,(THIS_FILE, "Opening sound device %s@%d/%d/%dms",
Sauw Ming98766c72011-03-11 06:57:24 +00003500 get_fmt_name(param->base.ext_fmt.id),
3501 param->base.clock_rate, param->base.channel_count,
3502 param->base.samples_per_frame / param->base.channel_count *
3503 1000 / param->base.clock_rate));
Benny Prijonob90fd382011-09-18 14:59:56 +00003504 pj_log_push_indent();
Benny Prijonof798e502009-03-09 13:08:16 +00003505
3506 status = pjmedia_snd_port_create2( pjsua_var.snd_pool,
Sauw Ming98766c72011-03-11 06:57:24 +00003507 param, &pjsua_var.snd_port);
Benny Prijonof798e502009-03-09 13:08:16 +00003508 if (status != PJ_SUCCESS)
Benny Prijonob90fd382011-09-18 14:59:56 +00003509 goto on_error;
Benny Prijonof798e502009-03-09 13:08:16 +00003510
3511 /* Get the port0 of the conference bridge. */
3512 conf_port = pjmedia_conf_get_master_port(pjsua_var.mconf);
3513 pj_assert(conf_port != NULL);
3514
3515 /* For conference bridge, resample if necessary if the bridge's
3516 * clock rate is different than the sound device's clock rate.
3517 */
3518 if (!pjsua_var.is_mswitch &&
Sauw Ming98766c72011-03-11 06:57:24 +00003519 param->base.ext_fmt.id == PJMEDIA_FORMAT_PCM &&
Nanang Izzuddinfe68f1d2011-07-19 03:42:28 +00003520 PJMEDIA_PIA_SRATE(&conf_port->info) != param->base.clock_rate)
Benny Prijonof798e502009-03-09 13:08:16 +00003521 {
3522 pjmedia_port *resample_port;
3523 unsigned resample_opt = 0;
3524
3525 if (pjsua_var.media_cfg.quality >= 3 &&
3526 pjsua_var.media_cfg.quality <= 4)
3527 {
3528 resample_opt |= PJMEDIA_RESAMPLE_USE_SMALL_FILTER;
3529 }
3530 else if (pjsua_var.media_cfg.quality < 3) {
3531 resample_opt |= PJMEDIA_RESAMPLE_USE_LINEAR;
3532 }
3533
3534 status = pjmedia_resample_port_create(pjsua_var.snd_pool,
3535 conf_port,
Sauw Ming98766c72011-03-11 06:57:24 +00003536 param->base.clock_rate,
Benny Prijonof798e502009-03-09 13:08:16 +00003537 resample_opt,
3538 &resample_port);
3539 if (status != PJ_SUCCESS) {
3540 char errmsg[PJ_ERR_MSG_SIZE];
3541 pj_strerror(status, errmsg, sizeof(errmsg));
3542 PJ_LOG(4, (THIS_FILE,
3543 "Error creating resample port: %s",
3544 errmsg));
3545 close_snd_dev();
Benny Prijonob90fd382011-09-18 14:59:56 +00003546 goto on_error;
Benny Prijonof798e502009-03-09 13:08:16 +00003547 }
3548
3549 conf_port = resample_port;
3550 }
3551
3552 /* Otherwise for audio switchboard, the switch's port0 setting is
3553 * derived from the sound device setting, so update the setting.
3554 */
3555 if (pjsua_var.is_mswitch) {
Nanang Izzuddinfe68f1d2011-07-19 03:42:28 +00003556 pj_memcpy(&conf_port->info.fmt, &param->base.ext_fmt,
Benny Prijonoc45d9512010-12-10 11:04:30 +00003557 sizeof(conf_port->info.fmt));
Nanang Izzuddinfe68f1d2011-07-19 03:42:28 +00003558 conf_port->info.fmt.det.aud.clock_rate = param->base.clock_rate;
3559 conf_port->info.fmt.det.aud.frame_time_usec = param->base.samples_per_frame*
Benny Prijonoc45d9512010-12-10 11:04:30 +00003560 1000000 /
Nanang Izzuddinfe68f1d2011-07-19 03:42:28 +00003561 param->base.clock_rate;
3562 conf_port->info.fmt.det.aud.channel_count = param->base.channel_count;
Benny Prijonoc45d9512010-12-10 11:04:30 +00003563 conf_port->info.fmt.det.aud.bits_per_sample = 16;
Benny Prijonof798e502009-03-09 13:08:16 +00003564 }
3565
Benny Prijonoc45d9512010-12-10 11:04:30 +00003566
Benny Prijonof798e502009-03-09 13:08:16 +00003567 /* Connect sound port to the bridge */
Benny Prijono52a93912006-08-04 20:54:37 +00003568 status = pjmedia_snd_port_connect(pjsua_var.snd_port,
3569 conf_port );
3570 if (status != PJ_SUCCESS) {
3571 pjsua_perror(THIS_FILE, "Unable to connect conference port to "
3572 "sound device", status);
3573 pjmedia_snd_port_destroy(pjsua_var.snd_port);
3574 pjsua_var.snd_port = NULL;
Benny Prijonob90fd382011-09-18 14:59:56 +00003575 goto on_error;
Benny Prijono52a93912006-08-04 20:54:37 +00003576 }
3577
Benny Prijonoeebe9af2006-06-13 22:57:13 +00003578 /* Save the device IDs */
Sauw Ming98766c72011-03-11 06:57:24 +00003579 pjsua_var.cap_dev = param->base.rec_id;
3580 pjsua_var.play_dev = param->base.play_id;
Benny Prijonoeebe9af2006-06-13 22:57:13 +00003581
Benny Prijonoc53c6d72006-11-27 09:54:03 +00003582 /* Update sound device name. */
Benny Prijonof798e502009-03-09 13:08:16 +00003583 {
3584 pjmedia_aud_dev_info rec_info;
3585 pjmedia_aud_stream *strm;
3586 pjmedia_aud_param si;
3587 pj_str_t tmp;
Benny Prijonoc53c6d72006-11-27 09:54:03 +00003588
Benny Prijonof798e502009-03-09 13:08:16 +00003589 strm = pjmedia_snd_port_get_snd_stream(pjsua_var.snd_port);
3590 status = pjmedia_aud_stream_get_param(strm, &si);
3591 if (status == PJ_SUCCESS)
3592 status = pjmedia_aud_dev_get_info(si.rec_id, &rec_info);
Benny Prijonof3758ee2008-02-26 15:32:16 +00003593
Benny Prijonof798e502009-03-09 13:08:16 +00003594 if (status==PJ_SUCCESS) {
Sauw Ming98766c72011-03-11 06:57:24 +00003595 if (param->base.clock_rate != pjsua_var.media_cfg.clock_rate) {
Benny Prijonof798e502009-03-09 13:08:16 +00003596 char tmp_buf[128];
3597 int tmp_buf_len = sizeof(tmp_buf);
3598
3599 tmp_buf_len = pj_ansi_snprintf(tmp_buf, sizeof(tmp_buf)-1,
3600 "%s (%dKHz)",
3601 rec_info.name,
Sauw Ming98766c72011-03-11 06:57:24 +00003602 param->base.clock_rate/1000);
Benny Prijonof798e502009-03-09 13:08:16 +00003603 pj_strset(&tmp, tmp_buf, tmp_buf_len);
3604 pjmedia_conf_set_port0_name(pjsua_var.mconf, &tmp);
3605 } else {
3606 pjmedia_conf_set_port0_name(pjsua_var.mconf,
3607 pj_cstr(&tmp, rec_info.name));
3608 }
3609 }
3610
3611 /* Any error is not major, let it through */
3612 status = PJ_SUCCESS;
Benny Prijonob90fd382011-09-18 14:59:56 +00003613 }
Benny Prijonof798e502009-03-09 13:08:16 +00003614
3615 /* If this is the first time the audio device is open, retrieve some
3616 * settings from the device (such as volume settings) so that the
3617 * pjsua_snd_get_setting() work.
3618 */
3619 if (pjsua_var.aud_open_cnt == 0) {
3620 update_initial_aud_param();
3621 ++pjsua_var.aud_open_cnt;
Benny Prijonof3758ee2008-02-26 15:32:16 +00003622 }
Benny Prijonoc53c6d72006-11-27 09:54:03 +00003623
Benny Prijonob90fd382011-09-18 14:59:56 +00003624 pj_log_pop_indent();
Benny Prijonoeebe9af2006-06-13 22:57:13 +00003625 return PJ_SUCCESS;
Benny Prijonob90fd382011-09-18 14:59:56 +00003626
3627on_error:
3628 pj_log_pop_indent();
3629 return status;
Benny Prijonof798e502009-03-09 13:08:16 +00003630}
Nanang Izzuddin8465c682009-03-04 17:23:25 +00003631
Nanang Izzuddin8465c682009-03-04 17:23:25 +00003632
Benny Prijonof798e502009-03-09 13:08:16 +00003633/* Close existing sound device */
3634static void close_snd_dev(void)
3635{
Benny Prijonob90fd382011-09-18 14:59:56 +00003636 pj_log_push_indent();
3637
Benny Prijono2d647722011-07-13 03:05:22 +00003638 /* Notify app */
3639 if (pjsua_var.snd_is_on && pjsua_var.ua_cfg.cb.on_snd_dev_operation) {
3640 (*pjsua_var.ua_cfg.cb.on_snd_dev_operation)(0);
3641 }
3642
Benny Prijonof798e502009-03-09 13:08:16 +00003643 /* Close sound device */
3644 if (pjsua_var.snd_port) {
3645 pjmedia_aud_dev_info cap_info, play_info;
3646 pjmedia_aud_stream *strm;
3647 pjmedia_aud_param param;
3648
3649 strm = pjmedia_snd_port_get_snd_stream(pjsua_var.snd_port);
3650 pjmedia_aud_stream_get_param(strm, &param);
3651
3652 if (pjmedia_aud_dev_get_info(param.rec_id, &cap_info) != PJ_SUCCESS)
3653 cap_info.name[0] = '\0';
3654 if (pjmedia_aud_dev_get_info(param.play_id, &play_info) != PJ_SUCCESS)
3655 play_info.name[0] = '\0';
3656
3657 PJ_LOG(4,(THIS_FILE, "Closing %s sound playback device and "
3658 "%s sound capture device",
3659 play_info.name, cap_info.name));
3660
3661 pjmedia_snd_port_disconnect(pjsua_var.snd_port);
3662 pjmedia_snd_port_destroy(pjsua_var.snd_port);
3663 pjsua_var.snd_port = NULL;
3664 }
3665
3666 /* Close null sound device */
3667 if (pjsua_var.null_snd) {
3668 PJ_LOG(4,(THIS_FILE, "Closing null sound device.."));
3669 pjmedia_master_port_destroy(pjsua_var.null_snd, PJ_FALSE);
3670 pjsua_var.null_snd = NULL;
3671 }
3672
3673 if (pjsua_var.snd_pool)
3674 pj_pool_release(pjsua_var.snd_pool);
3675 pjsua_var.snd_pool = NULL;
Benny Prijono2d647722011-07-13 03:05:22 +00003676 pjsua_var.snd_is_on = PJ_FALSE;
Benny Prijonob90fd382011-09-18 14:59:56 +00003677
3678 pj_log_pop_indent();
Benny Prijonof798e502009-03-09 13:08:16 +00003679}
3680
3681
3682/*
3683 * Select or change sound device. Application may call this function at
3684 * any time to replace current sound device.
3685 */
3686PJ_DEF(pj_status_t) pjsua_set_snd_dev( int capture_dev,
3687 int playback_dev)
3688{
3689 unsigned alt_cr_cnt = 1;
3690 unsigned alt_cr[] = {0, 44100, 48000, 32000, 16000, 8000};
3691 unsigned i;
3692 pj_status_t status = -1;
3693
Benny Prijonob90fd382011-09-18 14:59:56 +00003694 PJ_LOG(4,(THIS_FILE, "Set sound device: capture=%d, playback=%d",
3695 capture_dev, playback_dev));
3696 pj_log_push_indent();
3697
Benny Prijono23ea21a2009-06-03 12:43:06 +00003698 /* Null-sound */
3699 if (capture_dev==NULL_SND_DEV_ID && playback_dev==NULL_SND_DEV_ID) {
Benny Prijonob90fd382011-09-18 14:59:56 +00003700 status = pjsua_set_null_snd_dev();
3701 pj_log_pop_indent();
3702 return status;
Benny Prijono23ea21a2009-06-03 12:43:06 +00003703 }
3704
Benny Prijonof798e502009-03-09 13:08:16 +00003705 /* Set default clock rate */
3706 alt_cr[0] = pjsua_var.media_cfg.snd_clock_rate;
3707 if (alt_cr[0] == 0)
3708 alt_cr[0] = pjsua_var.media_cfg.clock_rate;
3709
3710 /* Allow retrying of different clock rate if we're using conference
3711 * bridge (meaning audio format is always PCM), otherwise lock on
3712 * to one clock rate.
3713 */
3714 if (pjsua_var.is_mswitch) {
3715 alt_cr_cnt = 1;
3716 } else {
3717 alt_cr_cnt = PJ_ARRAY_SIZE(alt_cr);
3718 }
3719
3720 /* Attempts to open the sound device with different clock rates */
3721 for (i=0; i<alt_cr_cnt; ++i) {
Sauw Ming98766c72011-03-11 06:57:24 +00003722 pjmedia_snd_port_param param;
Benny Prijonof798e502009-03-09 13:08:16 +00003723 unsigned samples_per_frame;
3724
3725 /* Create the default audio param */
3726 samples_per_frame = alt_cr[i] *
3727 pjsua_var.media_cfg.audio_frame_ptime *
3728 pjsua_var.media_cfg.channel_count / 1000;
Sauw Ming98766c72011-03-11 06:57:24 +00003729 status = create_aud_param(&param.base, capture_dev, playback_dev,
Benny Prijonof798e502009-03-09 13:08:16 +00003730 alt_cr[i], pjsua_var.media_cfg.channel_count,
3731 samples_per_frame, 16);
3732 if (status != PJ_SUCCESS)
Benny Prijonob90fd382011-09-18 14:59:56 +00003733 goto on_error;
Benny Prijonof798e502009-03-09 13:08:16 +00003734
3735 /* Open! */
Sauw Ming98766c72011-03-11 06:57:24 +00003736 param.options = 0;
Benny Prijonof798e502009-03-09 13:08:16 +00003737 status = open_snd_dev(&param);
3738 if (status == PJ_SUCCESS)
3739 break;
3740 }
3741
3742 if (status != PJ_SUCCESS) {
3743 pjsua_perror(THIS_FILE, "Unable to open sound device", status);
Benny Prijonob90fd382011-09-18 14:59:56 +00003744 goto on_error;
Benny Prijonof798e502009-03-09 13:08:16 +00003745 }
3746
Benny Prijonoe25fe6f2009-07-16 17:52:08 +00003747 pjsua_var.no_snd = PJ_FALSE;
Benny Prijono2d647722011-07-13 03:05:22 +00003748 pjsua_var.snd_is_on = PJ_TRUE;
Benny Prijonoe25fe6f2009-07-16 17:52:08 +00003749
Benny Prijonob90fd382011-09-18 14:59:56 +00003750 pj_log_pop_indent();
Benny Prijonof798e502009-03-09 13:08:16 +00003751 return PJ_SUCCESS;
Benny Prijonob90fd382011-09-18 14:59:56 +00003752
3753on_error:
3754 pj_log_pop_indent();
3755 return status;
Benny Prijonoeebe9af2006-06-13 22:57:13 +00003756}
3757
3758
3759/*
Benny Prijonoebdf8772007-02-01 19:25:50 +00003760 * Get currently active sound devices. If sound devices has not been created
3761 * (for example when pjsua_start() is not called), it is possible that
3762 * the function returns PJ_SUCCESS with -1 as device IDs.
3763 */
3764PJ_DEF(pj_status_t) pjsua_get_snd_dev(int *capture_dev,
3765 int *playback_dev)
3766{
3767 if (capture_dev) {
3768 *capture_dev = pjsua_var.cap_dev;
3769 }
3770 if (playback_dev) {
3771 *playback_dev = pjsua_var.play_dev;
3772 }
3773
3774 return PJ_SUCCESS;
3775}
3776
3777
3778/*
Benny Prijonoeebe9af2006-06-13 22:57:13 +00003779 * Use null sound device.
3780 */
3781PJ_DEF(pj_status_t) pjsua_set_null_snd_dev(void)
3782{
3783 pjmedia_port *conf_port;
3784 pj_status_t status;
3785
Benny Prijonob90fd382011-09-18 14:59:56 +00003786 PJ_LOG(4,(THIS_FILE, "Setting null sound device.."));
3787 pj_log_push_indent();
3788
3789
Benny Prijonoeebe9af2006-06-13 22:57:13 +00003790 /* Close existing sound device */
3791 close_snd_dev();
3792
Benny Prijono2d647722011-07-13 03:05:22 +00003793 /* Notify app */
3794 if (pjsua_var.ua_cfg.cb.on_snd_dev_operation) {
3795 (*pjsua_var.ua_cfg.cb.on_snd_dev_operation)(1);
3796 }
3797
Nanang Izzuddin68559c32008-06-13 17:01:46 +00003798 /* Create memory pool for sound device. */
3799 pjsua_var.snd_pool = pjsua_pool_create("pjsua_snd", 4000, 4000);
3800 PJ_ASSERT_RETURN(pjsua_var.snd_pool, PJ_ENOMEM);
3801
Benny Prijonoeebe9af2006-06-13 22:57:13 +00003802 PJ_LOG(4,(THIS_FILE, "Opening null sound device.."));
3803
3804 /* Get the port0 of the conference bridge. */
3805 conf_port = pjmedia_conf_get_master_port(pjsua_var.mconf);
3806 pj_assert(conf_port != NULL);
3807
3808 /* Create master port, connecting port0 of the conference bridge to
3809 * a null port.
3810 */
Nanang Izzuddin68559c32008-06-13 17:01:46 +00003811 status = pjmedia_master_port_create(pjsua_var.snd_pool, pjsua_var.null_port,
Benny Prijonoeebe9af2006-06-13 22:57:13 +00003812 conf_port, 0, &pjsua_var.null_snd);
3813 if (status != PJ_SUCCESS) {
3814 pjsua_perror(THIS_FILE, "Unable to create null sound device",
3815 status);
Benny Prijonob90fd382011-09-18 14:59:56 +00003816 pj_log_pop_indent();
Benny Prijonoeebe9af2006-06-13 22:57:13 +00003817 return status;
3818 }
3819
3820 /* Start the master port */
3821 status = pjmedia_master_port_start(pjsua_var.null_snd);
3822 PJ_ASSERT_RETURN(status == PJ_SUCCESS, status);
3823
Nanang Izzuddin68559c32008-06-13 17:01:46 +00003824 pjsua_var.cap_dev = NULL_SND_DEV_ID;
3825 pjsua_var.play_dev = NULL_SND_DEV_ID;
3826
Benny Prijonoe25fe6f2009-07-16 17:52:08 +00003827 pjsua_var.no_snd = PJ_FALSE;
Benny Prijono2d647722011-07-13 03:05:22 +00003828 pjsua_var.snd_is_on = PJ_TRUE;
Benny Prijonoe25fe6f2009-07-16 17:52:08 +00003829
Benny Prijonob90fd382011-09-18 14:59:56 +00003830 pj_log_pop_indent();
Benny Prijonoeebe9af2006-06-13 22:57:13 +00003831 return PJ_SUCCESS;
3832}
3833
3834
Benny Prijonoe909eac2006-07-27 22:04:56 +00003835
3836/*
3837 * Use no device!
3838 */
3839PJ_DEF(pjmedia_port*) pjsua_set_no_snd_dev(void)
3840{
3841 /* Close existing sound device */
3842 close_snd_dev();
3843
3844 pjsua_var.no_snd = PJ_TRUE;
3845 return pjmedia_conf_get_master_port(pjsua_var.mconf);
3846}
3847
3848
Benny Prijonof20687a2006-08-04 18:27:19 +00003849/*
3850 * Configure the AEC settings of the sound port.
3851 */
Benny Prijono5da50432006-08-07 10:24:52 +00003852PJ_DEF(pj_status_t) pjsua_set_ec(unsigned tail_ms, unsigned options)
Benny Prijonof20687a2006-08-04 18:27:19 +00003853{
3854 pjsua_var.media_cfg.ec_tail_len = tail_ms;
3855
3856 if (pjsua_var.snd_port)
Benny Prijono5da50432006-08-07 10:24:52 +00003857 return pjmedia_snd_port_set_ec( pjsua_var.snd_port, pjsua_var.pool,
3858 tail_ms, options);
Benny Prijonof20687a2006-08-04 18:27:19 +00003859
3860 return PJ_SUCCESS;
3861}
3862
3863
3864/*
3865 * Get current AEC tail length.
3866 */
Benny Prijono22dfe592006-08-06 12:07:13 +00003867PJ_DEF(pj_status_t) pjsua_get_ec_tail(unsigned *p_tail_ms)
Benny Prijonof20687a2006-08-04 18:27:19 +00003868{
3869 *p_tail_ms = pjsua_var.media_cfg.ec_tail_len;
3870 return PJ_SUCCESS;
3871}
3872
Nanang Izzuddinfe02a062009-02-18 14:28:49 +00003873
Nanang Izzuddin0cb3b022009-02-27 17:37:35 +00003874/*
Benny Prijonof798e502009-03-09 13:08:16 +00003875 * Check whether the sound device is currently active.
Nanang Izzuddin0cb3b022009-02-27 17:37:35 +00003876 */
Benny Prijonof798e502009-03-09 13:08:16 +00003877PJ_DEF(pj_bool_t) pjsua_snd_is_active(void)
Nanang Izzuddin0cb3b022009-02-27 17:37:35 +00003878{
Benny Prijonof798e502009-03-09 13:08:16 +00003879 return pjsua_var.snd_port != NULL;
3880}
Nanang Izzuddin0cb3b022009-02-27 17:37:35 +00003881
Nanang Izzuddin0cb3b022009-02-27 17:37:35 +00003882
Benny Prijonof798e502009-03-09 13:08:16 +00003883/*
3884 * Configure sound device setting to the sound device being used.
3885 */
3886PJ_DEF(pj_status_t) pjsua_snd_set_setting( pjmedia_aud_dev_cap cap,
3887 const void *pval,
3888 pj_bool_t keep)
3889{
Benny Prijono09b0ff62009-03-10 12:07:51 +00003890 pj_status_t status;
3891
Benny Prijonof798e502009-03-09 13:08:16 +00003892 /* Check if we are allowed to set the cap */
Benny Prijono09b0ff62009-03-10 12:07:51 +00003893 if ((cap & pjsua_var.aud_svmask) == 0) {
Benny Prijonof798e502009-03-09 13:08:16 +00003894 return PJMEDIA_EAUD_INVCAP;
3895 }
3896
Benny Prijono09b0ff62009-03-10 12:07:51 +00003897 /* If sound is active, set it immediately */
Benny Prijonof798e502009-03-09 13:08:16 +00003898 if (pjsua_snd_is_active()) {
Benny Prijonof798e502009-03-09 13:08:16 +00003899 pjmedia_aud_stream *strm;
3900
3901 strm = pjmedia_snd_port_get_snd_stream(pjsua_var.snd_port);
Benny Prijono09b0ff62009-03-10 12:07:51 +00003902 status = pjmedia_aud_stream_set_cap(strm, cap, pval);
Benny Prijonof798e502009-03-09 13:08:16 +00003903 } else {
Benny Prijono09b0ff62009-03-10 12:07:51 +00003904 status = PJ_SUCCESS;
Benny Prijonof798e502009-03-09 13:08:16 +00003905 }
Benny Prijono09b0ff62009-03-10 12:07:51 +00003906
3907 if (status != PJ_SUCCESS)
3908 return status;
3909
3910 /* Save in internal param for later device open */
3911 if (keep) {
3912 status = pjmedia_aud_param_set_cap(&pjsua_var.aud_param,
3913 cap, pval);
3914 }
3915
3916 return status;
Benny Prijonof798e502009-03-09 13:08:16 +00003917}
3918
3919/*
3920 * Retrieve a sound device setting.
3921 */
3922PJ_DEF(pj_status_t) pjsua_snd_get_setting( pjmedia_aud_dev_cap cap,
3923 void *pval)
3924{
3925 /* If sound device has never been opened before, open it to
3926 * retrieve the initial setting from the device (e.g. audio
3927 * volume)
3928 */
Benny Prijono09b0ff62009-03-10 12:07:51 +00003929 if (pjsua_var.aud_open_cnt==0) {
3930 PJ_LOG(4,(THIS_FILE, "Opening sound device to get initial settings"));
Benny Prijonof798e502009-03-09 13:08:16 +00003931 pjsua_set_snd_dev(pjsua_var.cap_dev, pjsua_var.play_dev);
Benny Prijono09b0ff62009-03-10 12:07:51 +00003932 close_snd_dev();
3933 }
Benny Prijonof798e502009-03-09 13:08:16 +00003934
3935 if (pjsua_snd_is_active()) {
3936 /* Sound is active, retrieve from device directly */
3937 pjmedia_aud_stream *strm;
3938
3939 strm = pjmedia_snd_port_get_snd_stream(pjsua_var.snd_port);
3940 return pjmedia_aud_stream_get_cap(strm, cap, pval);
3941 } else {
3942 /* Otherwise retrieve from internal param */
3943 return pjmedia_aud_param_get_cap(&pjsua_var.aud_param,
3944 cap, pval);
3945 }
Nanang Izzuddin0cb3b022009-02-27 17:37:35 +00003946}
3947
3948
Benny Prijonoeebe9af2006-06-13 22:57:13 +00003949/*****************************************************************************
3950 * Codecs.
3951 */
3952
3953/*
3954 * Enum all supported codecs in the system.
3955 */
3956PJ_DEF(pj_status_t) pjsua_enum_codecs( pjsua_codec_info id[],
3957 unsigned *p_count )
3958{
3959 pjmedia_codec_mgr *codec_mgr;
3960 pjmedia_codec_info info[32];
3961 unsigned i, count, prio[32];
3962 pj_status_t status;
3963
3964 codec_mgr = pjmedia_endpt_get_codec_mgr(pjsua_var.med_endpt);
3965 count = PJ_ARRAY_SIZE(info);
3966 status = pjmedia_codec_mgr_enum_codecs( codec_mgr, &count, info, prio);
3967 if (status != PJ_SUCCESS) {
3968 *p_count = 0;
3969 return status;
3970 }
3971
3972 if (count > *p_count) count = *p_count;
3973
3974 for (i=0; i<count; ++i) {
Nanang Izzuddin56b2ce42011-04-06 13:55:01 +00003975 pj_bzero(&id[i], sizeof(pjsua_codec_info));
3976
Benny Prijonoeebe9af2006-06-13 22:57:13 +00003977 pjmedia_codec_info_to_id(&info[i], id[i].buf_, sizeof(id[i].buf_));
3978 id[i].codec_id = pj_str(id[i].buf_);
3979 id[i].priority = (pj_uint8_t) prio[i];
3980 }
3981
3982 *p_count = count;
3983
3984 return PJ_SUCCESS;
3985}
3986
3987
3988/*
3989 * Change codec priority.
3990 */
3991PJ_DEF(pj_status_t) pjsua_codec_set_priority( const pj_str_t *codec_id,
3992 pj_uint8_t priority )
3993{
Benny Prijono88accae2008-06-26 15:48:14 +00003994 const pj_str_t all = { NULL, 0 };
Benny Prijonoeebe9af2006-06-13 22:57:13 +00003995 pjmedia_codec_mgr *codec_mgr;
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 return pjmedia_codec_mgr_set_codec_priority(codec_mgr, codec_id,
4003 priority);
4004}
4005
4006
4007/*
4008 * Get codec parameters.
4009 */
4010PJ_DEF(pj_status_t) pjsua_codec_get_param( const pj_str_t *codec_id,
4011 pjmedia_codec_param *param )
4012{
Benny Prijono88accae2008-06-26 15:48:14 +00004013 const pj_str_t all = { NULL, 0 };
Benny Prijonoeebe9af2006-06-13 22:57:13 +00004014 const pjmedia_codec_info *info;
4015 pjmedia_codec_mgr *codec_mgr;
4016 unsigned count = 1;
4017 pj_status_t status;
4018
4019 codec_mgr = pjmedia_endpt_get_codec_mgr(pjsua_var.med_endpt);
4020
Benny Prijono88accae2008-06-26 15:48:14 +00004021 if (codec_id->slen==1 && *codec_id->ptr=='*')
4022 codec_id = &all;
4023
Benny Prijonoeebe9af2006-06-13 22:57:13 +00004024 status = pjmedia_codec_mgr_find_codecs_by_id(codec_mgr, codec_id,
4025 &count, &info, NULL);
4026 if (status != PJ_SUCCESS)
4027 return status;
4028
4029 if (count != 1)
Nanang Izzuddin50fae732011-03-22 09:49:23 +00004030 return (count > 1? PJ_ETOOMANY : PJ_ENOTFOUND);
Benny Prijonoeebe9af2006-06-13 22:57:13 +00004031
4032 status = pjmedia_codec_mgr_get_default_param( codec_mgr, info, param);
4033 return status;
4034}
4035
4036
4037/*
4038 * Set codec parameters.
4039 */
Nanang Izzuddin06839e72010-01-27 11:48:31 +00004040PJ_DEF(pj_status_t) pjsua_codec_set_param( const pj_str_t *codec_id,
Benny Prijonoeebe9af2006-06-13 22:57:13 +00004041 const pjmedia_codec_param *param)
4042{
Nanang Izzuddin06839e72010-01-27 11:48:31 +00004043 const pjmedia_codec_info *info[2];
4044 pjmedia_codec_mgr *codec_mgr;
4045 unsigned count = 2;
4046 pj_status_t status;
4047
4048 codec_mgr = pjmedia_endpt_get_codec_mgr(pjsua_var.med_endpt);
4049
4050 status = pjmedia_codec_mgr_find_codecs_by_id(codec_mgr, codec_id,
4051 &count, info, NULL);
4052 if (status != PJ_SUCCESS)
4053 return status;
4054
4055 /* Codec ID should be specific, except for G.722.1 */
4056 if (count > 1 &&
4057 pj_strnicmp2(codec_id, "G7221/16", 8) != 0 &&
4058 pj_strnicmp2(codec_id, "G7221/32", 8) != 0)
4059 {
4060 pj_assert(!"Codec ID is not specific");
4061 return PJ_ETOOMANY;
4062 }
4063
4064 status = pjmedia_codec_mgr_set_default_param(codec_mgr, info[0], param);
4065 return status;
Benny Prijonoeebe9af2006-06-13 22:57:13 +00004066}
Nanang Izzuddin50fae732011-03-22 09:49:23 +00004067
4068