blob: 60aebbeba7ba8c5da093608b888b87917e89106e [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 */
Sauw Minge7dbbc82011-10-24 09:28:13 +0000399pj_status_t pjsua_media_subsys_destroy(unsigned flags)
Benny Prijonoeebe9af2006-06-13 22:57:13 +0000400{
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) {
Sauw Minge7dbbc82011-10-24 09:28:13 +0000444 /* TODO: check if we're not allowed to send to network in the
445 * "flags", and if so do not do TURN allocation...
446 */
447 PJ_UNUSED_ARG(flags);
Benny Prijono0bc99a92011-03-17 04:34:43 +0000448 pjmedia_transport_close(call_med->tp);
449 }
450 call_med->tp = NULL;
Benny Prijono311b63f2008-07-14 11:31:40 +0000451 }
Benny Prijonoeebe9af2006-06-13 22:57:13 +0000452 }
453
454 /* Destroy media endpoint. */
455 if (pjsua_var.med_endpt) {
456
Benny Prijono0bc99a92011-03-17 04:34:43 +0000457# if PJMEDIA_HAS_VIDEO
Benny Prijono9f468d12011-07-07 07:46:33 +0000458 pjsua_vid_subsys_destroy();
Benny Prijono0bc99a92011-03-17 04:34:43 +0000459# endif
Benny Prijonoeebe9af2006-06-13 22:57:13 +0000460
Benny Prijonoeebe9af2006-06-13 22:57:13 +0000461 pjmedia_endpt_destroy(pjsua_var.med_endpt);
462 pjsua_var.med_endpt = NULL;
Benny Prijonoeebe9af2006-06-13 22:57:13 +0000463
Benny Prijonoad2e0ca2007-04-29 12:31:51 +0000464 /* Deinitialize sound subsystem */
Benny Prijonoa41d66e2007-10-01 12:17:23 +0000465 // Not necessary, as pjmedia_snd_deinit() should have been called
466 // in pjmedia_endpt_destroy().
467 //pjmedia_snd_deinit();
Benny Prijonoad2e0ca2007-04-29 12:31:51 +0000468 }
Benny Prijonoaf1bb1e2006-11-21 12:39:31 +0000469
Benny Prijonode479562007-03-15 10:23:55 +0000470 /* Reset RTP port */
471 next_rtp_port = 0;
472
Benny Prijonob90fd382011-09-18 14:59:56 +0000473 pj_log_pop_indent();
474
Benny Prijonoeebe9af2006-06-13 22:57:13 +0000475 return PJ_SUCCESS;
476}
477
Benny Prijono0bc99a92011-03-17 04:34:43 +0000478/*
Benny Prijonoeebe9af2006-06-13 22:57:13 +0000479 * Create RTP and RTCP socket pair, and possibly resolve their public
480 * address via STUN.
481 */
482static pj_status_t create_rtp_rtcp_sock(const pjsua_transport_config *cfg,
483 pjmedia_sock_info *skinfo)
484{
Benny Prijono0bc99a92011-03-17 04:34:43 +0000485 enum {
Benny Prijonoeebe9af2006-06-13 22:57:13 +0000486 RTP_RETRY = 100
487 };
488 int i;
489 pj_sockaddr_in bound_addr;
490 pj_sockaddr_in mapped_addr[2];
491 pj_status_t status = PJ_SUCCESS;
492 char addr_buf[PJ_INET6_ADDRSTRLEN+2];
493 pj_sock_t sock[2];
494
495 /* Make sure STUN server resolution has completed */
496 status = resolve_stun_server(PJ_TRUE);
497 if (status != PJ_SUCCESS) {
498 pjsua_perror(THIS_FILE, "Error resolving STUN server", status);
499 return status;
500 }
501
502 if (next_rtp_port == 0)
503 next_rtp_port = (pj_uint16_t)cfg->port;
504
Benny Prijono0bc99a92011-03-17 04:34:43 +0000505 if (next_rtp_port == 0)
506 next_rtp_port = (pj_uint16_t)40000;
507
Benny Prijonoeebe9af2006-06-13 22:57:13 +0000508 for (i=0; i<2; ++i)
509 sock[i] = PJ_INVALID_SOCKET;
510
511 bound_addr.sin_addr.s_addr = PJ_INADDR_ANY;
512 if (cfg->bound_addr.slen) {
513 status = pj_sockaddr_in_set_str_addr(&bound_addr, &cfg->bound_addr);
514 if (status != PJ_SUCCESS) {
515 pjsua_perror(THIS_FILE, "Unable to resolve transport bind address",
516 status);
517 return status;
518 }
519 }
520
521 /* Loop retry to bind RTP and RTCP sockets. */
522 for (i=0; i<RTP_RETRY; ++i, next_rtp_port += 2) {
523
524 /* Create RTP socket. */
525 status = pj_sock_socket(pj_AF_INET(), pj_SOCK_DGRAM(), 0, &sock[0]);
526 if (status != PJ_SUCCESS) {
527 pjsua_perror(THIS_FILE, "socket() error", status);
528 return status;
529 }
530
531 /* Apply QoS to RTP socket, if specified */
Benny Prijono0bc99a92011-03-17 04:34:43 +0000532 status = pj_sock_apply_qos2(sock[0], cfg->qos_type,
533 &cfg->qos_params,
Benny Prijonoeebe9af2006-06-13 22:57:13 +0000534 2, THIS_FILE, "RTP socket");
535
536 /* Bind RTP socket */
Benny Prijono0bc99a92011-03-17 04:34:43 +0000537 status=pj_sock_bind_in(sock[0], pj_ntohl(bound_addr.sin_addr.s_addr),
Benny Prijonoeebe9af2006-06-13 22:57:13 +0000538 next_rtp_port);
539 if (status != PJ_SUCCESS) {
Benny Prijono0bc99a92011-03-17 04:34:43 +0000540 pj_sock_close(sock[0]);
Benny Prijonoeebe9af2006-06-13 22:57:13 +0000541 sock[0] = PJ_INVALID_SOCKET;
542 continue;
543 }
544
545 /* Create RTCP socket. */
546 status = pj_sock_socket(pj_AF_INET(), pj_SOCK_DGRAM(), 0, &sock[1]);
547 if (status != PJ_SUCCESS) {
548 pjsua_perror(THIS_FILE, "socket() error", status);
549 pj_sock_close(sock[0]);
550 return status;
551 }
552
553 /* Apply QoS to RTCP socket, if specified */
Benny Prijono0bc99a92011-03-17 04:34:43 +0000554 status = pj_sock_apply_qos2(sock[1], cfg->qos_type,
555 &cfg->qos_params,
Benny Prijonoeebe9af2006-06-13 22:57:13 +0000556 2, THIS_FILE, "RTCP socket");
557
558 /* Bind RTCP socket */
Benny Prijono0bc99a92011-03-17 04:34:43 +0000559 status=pj_sock_bind_in(sock[1], pj_ntohl(bound_addr.sin_addr.s_addr),
Benny Prijonoeebe9af2006-06-13 22:57:13 +0000560 (pj_uint16_t)(next_rtp_port+1));
561 if (status != PJ_SUCCESS) {
Benny Prijono0bc99a92011-03-17 04:34:43 +0000562 pj_sock_close(sock[0]);
Benny Prijonoeebe9af2006-06-13 22:57:13 +0000563 sock[0] = PJ_INVALID_SOCKET;
564
Benny Prijono0bc99a92011-03-17 04:34:43 +0000565 pj_sock_close(sock[1]);
Benny Prijonoeebe9af2006-06-13 22:57:13 +0000566 sock[1] = PJ_INVALID_SOCKET;
567 continue;
568 }
569
570 /*
571 * If we're configured to use STUN, then find out the mapped address,
572 * and make sure that the mapped RTCP port is adjacent with the RTP.
573 */
574 if (pjsua_var.stun_srv.addr.sa_family != 0) {
575 char ip_addr[32];
576 pj_str_t stun_srv;
577
Benny Prijono0bc99a92011-03-17 04:34:43 +0000578 pj_ansi_strcpy(ip_addr,
Benny Prijonoeebe9af2006-06-13 22:57:13 +0000579 pj_inet_ntoa(pjsua_var.stun_srv.ipv4.sin_addr));
580 stun_srv = pj_str(ip_addr);
581
582 status=pjstun_get_mapped_addr(&pjsua_var.cp.factory, 2, sock,
583 &stun_srv, pj_ntohs(pjsua_var.stun_srv.ipv4.sin_port),
584 &stun_srv, pj_ntohs(pjsua_var.stun_srv.ipv4.sin_port),
585 mapped_addr);
586 if (status != PJ_SUCCESS) {
587 pjsua_perror(THIS_FILE, "STUN resolve error", status);
588 goto on_error;
589 }
590
591#if PJSUA_REQUIRE_CONSECUTIVE_RTCP_PORT
Benny Prijono0bc99a92011-03-17 04:34:43 +0000592 if (pj_ntohs(mapped_addr[1].sin_port) ==
Benny Prijonoeebe9af2006-06-13 22:57:13 +0000593 pj_ntohs(mapped_addr[0].sin_port)+1)
594 {
595 /* Success! */
596 break;
597 }
598
Benny Prijono0bc99a92011-03-17 04:34:43 +0000599 pj_sock_close(sock[0]);
Benny Prijonoeebe9af2006-06-13 22:57:13 +0000600 sock[0] = PJ_INVALID_SOCKET;
601
Benny Prijono0bc99a92011-03-17 04:34:43 +0000602 pj_sock_close(sock[1]);
Benny Prijonoeebe9af2006-06-13 22:57:13 +0000603 sock[1] = PJ_INVALID_SOCKET;
604#else
Benny Prijono0bc99a92011-03-17 04:34:43 +0000605 if (pj_ntohs(mapped_addr[1].sin_port) !=
Benny Prijonoeebe9af2006-06-13 22:57:13 +0000606 pj_ntohs(mapped_addr[0].sin_port)+1)
607 {
Benny Prijono0bc99a92011-03-17 04:34:43 +0000608 PJ_LOG(4,(THIS_FILE,
Benny Prijonoeebe9af2006-06-13 22:57:13 +0000609 "Note: STUN mapped RTCP port %d is not adjacent"
610 " to RTP port %d",
611 pj_ntohs(mapped_addr[1].sin_port),
612 pj_ntohs(mapped_addr[0].sin_port)));
613 }
614 /* Success! */
615 break;
616#endif
617
618 } else if (cfg->public_addr.slen) {
619
620 status = pj_sockaddr_in_init(&mapped_addr[0], &cfg->public_addr,
621 (pj_uint16_t)next_rtp_port);
622 if (status != PJ_SUCCESS)
623 goto on_error;
624
625 status = pj_sockaddr_in_init(&mapped_addr[1], &cfg->public_addr,
626 (pj_uint16_t)(next_rtp_port+1));
627 if (status != PJ_SUCCESS)
628 goto on_error;
629
630 break;
631
632 } else {
633
634 if (bound_addr.sin_addr.s_addr == 0) {
635 pj_sockaddr addr;
636
637 /* Get local IP address. */
638 status = pj_gethostip(pj_AF_INET(), &addr);
639 if (status != PJ_SUCCESS)
640 goto on_error;
641
642 bound_addr.sin_addr.s_addr = addr.ipv4.sin_addr.s_addr;
643 }
644
645 for (i=0; i<2; ++i) {
646 pj_sockaddr_in_init(&mapped_addr[i], NULL, 0);
647 mapped_addr[i].sin_addr.s_addr = bound_addr.sin_addr.s_addr;
648 }
649
650 mapped_addr[0].sin_port=pj_htons((pj_uint16_t)next_rtp_port);
651 mapped_addr[1].sin_port=pj_htons((pj_uint16_t)(next_rtp_port+1));
652 break;
653 }
654 }
655
656 if (sock[0] == PJ_INVALID_SOCKET) {
Benny Prijono0bc99a92011-03-17 04:34:43 +0000657 PJ_LOG(1,(THIS_FILE,
Benny Prijonoeebe9af2006-06-13 22:57:13 +0000658 "Unable to find appropriate RTP/RTCP ports combination"));
659 goto on_error;
660 }
661
662
663 skinfo->rtp_sock = sock[0];
Benny Prijono0bc99a92011-03-17 04:34:43 +0000664 pj_memcpy(&skinfo->rtp_addr_name,
Benny Prijonoeebe9af2006-06-13 22:57:13 +0000665 &mapped_addr[0], sizeof(pj_sockaddr_in));
666
667 skinfo->rtcp_sock = sock[1];
Benny Prijono0bc99a92011-03-17 04:34:43 +0000668 pj_memcpy(&skinfo->rtcp_addr_name,
Benny Prijonoeebe9af2006-06-13 22:57:13 +0000669 &mapped_addr[1], sizeof(pj_sockaddr_in));
670
671 PJ_LOG(4,(THIS_FILE, "RTP socket reachable at %s",
672 pj_sockaddr_print(&skinfo->rtp_addr_name, addr_buf,
673 sizeof(addr_buf), 3)));
674 PJ_LOG(4,(THIS_FILE, "RTCP socket reachable at %s",
675 pj_sockaddr_print(&skinfo->rtcp_addr_name, addr_buf,
676 sizeof(addr_buf), 3)));
677
678 next_rtp_port += 2;
679 return PJ_SUCCESS;
680
681on_error:
682 for (i=0; i<2; ++i) {
683 if (sock[i] != PJ_INVALID_SOCKET)
684 pj_sock_close(sock[i]);
685 }
686 return status;
687}
688
Benny Prijono0bc99a92011-03-17 04:34:43 +0000689/* Create normal UDP media transports */
690static pj_status_t create_udp_media_transport(const pjsua_transport_config *cfg,
691 pjsua_call_media *call_med)
Benny Prijonoeebe9af2006-06-13 22:57:13 +0000692{
Benny Prijono0bc99a92011-03-17 04:34:43 +0000693 pjmedia_sock_info skinfo;
Benny Prijonoeebe9af2006-06-13 22:57:13 +0000694 pj_status_t status;
695
Benny Prijono0bc99a92011-03-17 04:34:43 +0000696 status = create_rtp_rtcp_sock(cfg, &skinfo);
697 if (status != PJ_SUCCESS) {
698 pjsua_perror(THIS_FILE, "Unable to create RTP/RTCP socket",
699 status);
700 goto on_error;
Benny Prijonoeebe9af2006-06-13 22:57:13 +0000701 }
702
Benny Prijono0bc99a92011-03-17 04:34:43 +0000703 status = pjmedia_transport_udp_attach(pjsua_var.med_endpt, NULL,
704 &skinfo, 0, &call_med->tp);
705 if (status != PJ_SUCCESS) {
706 pjsua_perror(THIS_FILE, "Unable to create media transport",
707 status);
708 goto on_error;
709 }
Benny Prijonoeebe9af2006-06-13 22:57:13 +0000710
Benny Prijono0bc99a92011-03-17 04:34:43 +0000711 pjmedia_transport_simulate_lost(call_med->tp, PJMEDIA_DIR_ENCODING,
712 pjsua_var.media_cfg.tx_drop_pct);
713
714 pjmedia_transport_simulate_lost(call_med->tp, PJMEDIA_DIR_DECODING,
715 pjsua_var.media_cfg.rx_drop_pct);
Benny Prijonoeebe9af2006-06-13 22:57:13 +0000716
Sauw Ming73ecfe82011-09-21 10:20:01 +0000717 call_med->tp_ready = PJ_SUCCESS;
718
Benny Prijonoeebe9af2006-06-13 22:57:13 +0000719 return PJ_SUCCESS;
Benny Prijono0bc99a92011-03-17 04:34:43 +0000720
721on_error:
722 if (call_med->tp)
723 pjmedia_transport_close(call_med->tp);
724
725 return status;
Benny Prijonoeebe9af2006-06-13 22:57:13 +0000726}
727
Benny Prijono0bc99a92011-03-17 04:34:43 +0000728#if DISABLED_FOR_TICKET_1185
Benny Prijonoc97608e2007-03-23 16:34:20 +0000729/* Create normal UDP media transports */
730static pj_status_t create_udp_media_transports(pjsua_transport_config *cfg)
Benny Prijonoeebe9af2006-06-13 22:57:13 +0000731{
Benny Prijonoeebe9af2006-06-13 22:57:13 +0000732 unsigned i;
733 pj_status_t status;
734
Benny Prijono0bc99a92011-03-17 04:34:43 +0000735 for (i=0; i < pjsua_var.ua_cfg.max_calls; ++i) {
736 pjsua_call *call = &pjsua_var.calls[i];
737 unsigned strm_idx;
Benny Prijonoeebe9af2006-06-13 22:57:13 +0000738
Benny Prijono0bc99a92011-03-17 04:34:43 +0000739 for (strm_idx=0; strm_idx < call->med_cnt; ++strm_idx) {
740 pjsua_call_media *call_med = &call->media[strm_idx];
741
742 status = create_udp_media_transport(cfg, &call_med->tp);
743 if (status != PJ_SUCCESS)
744 goto on_error;
Benny Prijonoeebe9af2006-06-13 22:57:13 +0000745 }
Benny Prijonoeebe9af2006-06-13 22:57:13 +0000746 }
747
Benny Prijonoc97608e2007-03-23 16:34:20 +0000748 return PJ_SUCCESS;
749
750on_error:
Benny Prijono0bc99a92011-03-17 04:34:43 +0000751 for (i=0; i < pjsua_var.ua_cfg.max_calls; ++i) {
752 pjsua_call *call = &pjsua_var.calls[i];
753 unsigned strm_idx;
754
755 for (strm_idx=0; strm_idx < call->med_cnt; ++strm_idx) {
756 pjsua_call_media *call_med = &call->media[strm_idx];
757
758 if (call_med->tp) {
759 pjmedia_transport_close(call_med->tp);
760 call_med->tp = NULL;
761 }
Benny Prijonoc97608e2007-03-23 16:34:20 +0000762 }
763 }
Benny Prijonoc97608e2007-03-23 16:34:20 +0000764 return status;
765}
Benny Prijono0bc99a92011-03-17 04:34:43 +0000766#endif
Benny Prijonoc97608e2007-03-23 16:34:20 +0000767
Benny Prijono096c56c2007-09-15 08:30:16 +0000768/* This callback is called when ICE negotiation completes */
Benny Prijonof76e1392008-06-06 14:51:48 +0000769static void on_ice_complete(pjmedia_transport *tp,
770 pj_ice_strans_op op,
771 pj_status_t result)
Benny Prijono096c56c2007-09-15 08:30:16 +0000772{
Benny Prijono0bc99a92011-03-17 04:34:43 +0000773 pjsua_call_media *call_med = (pjsua_call_media*)tp->user_data;
Benny Prijono096c56c2007-09-15 08:30:16 +0000774
Benny Prijono0bc99a92011-03-17 04:34:43 +0000775 if (!call_med)
Benny Prijonof76e1392008-06-06 14:51:48 +0000776 return;
777
778 switch (op) {
779 case PJ_ICE_STRANS_OP_INIT:
Sauw Ming73ecfe82011-09-21 10:20:01 +0000780 call_med->tp_ready = result;
781 if (call_med->med_create_cb)
782 (*call_med->med_create_cb)(call_med, result,
783 call_med->call->secure_level, NULL);
Benny Prijonof76e1392008-06-06 14:51:48 +0000784 break;
785 case PJ_ICE_STRANS_OP_NEGOTIATION:
786 if (result != PJ_SUCCESS) {
Benny Prijono0bc99a92011-03-17 04:34:43 +0000787 call_med->state = PJSUA_CALL_MEDIA_ERROR;
788 call_med->dir = PJMEDIA_DIR_NONE;
Benny Prijonof76e1392008-06-06 14:51:48 +0000789
Benny Prijono0bc99a92011-03-17 04:34:43 +0000790 if (call_med->call && pjsua_var.ua_cfg.cb.on_call_media_state) {
791 pjsua_var.ua_cfg.cb.on_call_media_state(call_med->call->index);
Benny Prijonof76e1392008-06-06 14:51:48 +0000792 }
Benny Prijono0bc99a92011-03-17 04:34:43 +0000793 } else if (call_med->call) {
Benny Prijonof5d9f1f2009-10-14 13:13:18 +0000794 /* Send UPDATE if default transport address is different than
795 * what was advertised (ticket #881)
796 */
797 pjmedia_transport_info tpinfo;
798 pjmedia_ice_transport_info *ii = NULL;
799 unsigned i;
800
801 pjmedia_transport_info_init(&tpinfo);
802 pjmedia_transport_get_info(tp, &tpinfo);
803 for (i=0; i<tpinfo.specific_info_cnt; ++i) {
804 if (tpinfo.spc_info[i].type==PJMEDIA_TRANSPORT_TYPE_ICE) {
805 ii = (pjmedia_ice_transport_info*)
806 tpinfo.spc_info[i].buffer;
807 break;
808 }
809 }
810
811 if (ii && ii->role==PJ_ICE_SESS_ROLE_CONTROLLING &&
812 pj_sockaddr_cmp(&tpinfo.sock_info.rtp_addr_name,
Benny Prijono0bc99a92011-03-17 04:34:43 +0000813 &call_med->rtp_addr))
Benny Prijonof5d9f1f2009-10-14 13:13:18 +0000814 {
Benny Prijonoa8f9e622010-06-21 13:28:55 +0000815 pj_bool_t use_update;
816 const pj_str_t STR_UPDATE = { "UPDATE", 6 };
817 pjsip_dialog_cap_status support_update;
818 pjsip_dialog *dlg;
819
Benny Prijono0bc99a92011-03-17 04:34:43 +0000820 dlg = call_med->call->inv->dlg;
Benny Prijonoa8f9e622010-06-21 13:28:55 +0000821 support_update = pjsip_dlg_remote_has_cap(dlg, PJSIP_H_ALLOW,
822 NULL, &STR_UPDATE);
823 use_update = (support_update == PJSIP_DIALOG_CAP_SUPPORTED);
824
Benny Prijonof5d9f1f2009-10-14 13:13:18 +0000825 PJ_LOG(4,(THIS_FILE,
826 "ICE default transport address has changed for "
Benny Prijono0bc99a92011-03-17 04:34:43 +0000827 "call %d, sending %s", call_med->call->index,
Benny Prijonoa8f9e622010-06-21 13:28:55 +0000828 (use_update ? "UPDATE" : "re-INVITE")));
829
830 if (use_update)
Benny Prijono0bc99a92011-03-17 04:34:43 +0000831 pjsua_call_update(call_med->call->index, 0, NULL);
Benny Prijonoa8f9e622010-06-21 13:28:55 +0000832 else
Benny Prijono0bc99a92011-03-17 04:34:43 +0000833 pjsua_call_reinvite(call_med->call->index, 0, NULL);
Benny Prijonof5d9f1f2009-10-14 13:13:18 +0000834 }
Benny Prijonof76e1392008-06-06 14:51:48 +0000835 }
836 break;
Benny Prijono4d6ff4d2010-06-19 12:35:33 +0000837 case PJ_ICE_STRANS_OP_KEEP_ALIVE:
838 if (result != PJ_SUCCESS) {
839 PJ_PERROR(4,(THIS_FILE, result,
Benny Prijono0bc99a92011-03-17 04:34:43 +0000840 "ICE keep alive failure for transport %d:%d",
841 call_med->call->index, call_med->idx));
Benny Prijono4d6ff4d2010-06-19 12:35:33 +0000842 }
Sauw Ming73ecfe82011-09-21 10:20:01 +0000843 if (pjsua_var.ua_cfg.cb.on_call_media_transport_state) {
844 pjsua_med_tp_state_info info;
845
846 pj_bzero(&info, sizeof(info));
847 info.med_idx = call_med->idx;
848 info.state = call_med->tp_st;
849 info.status = result;
850 info.ext_info = &op;
851 (*pjsua_var.ua_cfg.cb.on_call_media_transport_state)(
852 call_med->call->index, &info);
853 }
Benny Prijono4d6ff4d2010-06-19 12:35:33 +0000854 if (pjsua_var.ua_cfg.cb.on_ice_transport_error) {
Benny Prijono0bc99a92011-03-17 04:34:43 +0000855 pjsua_call_id id = call_med->call->index;
Benny Prijono4d6ff4d2010-06-19 12:35:33 +0000856 (*pjsua_var.ua_cfg.cb.on_ice_transport_error)(id, op, result,
857 NULL);
858 }
859 break;
Benny Prijono096c56c2007-09-15 08:30:16 +0000860 }
861}
862
863
Benny Prijonof76e1392008-06-06 14:51:48 +0000864/* Parse "HOST:PORT" format */
865static pj_status_t parse_host_port(const pj_str_t *host_port,
866 pj_str_t *host, pj_uint16_t *port)
Benny Prijonoc97608e2007-03-23 16:34:20 +0000867{
Benny Prijonof76e1392008-06-06 14:51:48 +0000868 pj_str_t str_port;
869
870 str_port.ptr = pj_strchr(host_port, ':');
871 if (str_port.ptr != NULL) {
872 int iport;
873
874 host->ptr = host_port->ptr;
875 host->slen = (str_port.ptr - host->ptr);
876 str_port.ptr++;
877 str_port.slen = host_port->slen - host->slen - 1;
878 iport = (int)pj_strtoul(&str_port);
879 if (iport < 1 || iport > 65535)
880 return PJ_EINVAL;
881 *port = (pj_uint16_t)iport;
882 } else {
883 *host = *host_port;
884 *port = 0;
885 }
886
887 return PJ_SUCCESS;
888}
889
890/* Create ICE media transports (when ice is enabled) */
Benny Prijono0bc99a92011-03-17 04:34:43 +0000891static pj_status_t create_ice_media_transport(
892 const pjsua_transport_config *cfg,
Sauw Ming73ecfe82011-09-21 10:20:01 +0000893 pjsua_call_media *call_med,
894 pj_bool_t async)
Benny Prijonof76e1392008-06-06 14:51:48 +0000895{
896 char stunip[PJ_INET6_ADDRSTRLEN];
897 pj_ice_strans_cfg ice_cfg;
Benny Prijono0bc99a92011-03-17 04:34:43 +0000898 pjmedia_ice_cb ice_cb;
899 char name[32];
900 unsigned comp_cnt;
Benny Prijonoc97608e2007-03-23 16:34:20 +0000901 pj_status_t status;
902
Benny Prijonoda9785b2007-04-02 20:43:06 +0000903 /* Make sure STUN server resolution has completed */
Benny Prijonobb995fd2009-08-12 11:03:23 +0000904 status = resolve_stun_server(PJ_TRUE);
Benny Prijonoda9785b2007-04-02 20:43:06 +0000905 if (status != PJ_SUCCESS) {
906 pjsua_perror(THIS_FILE, "Error resolving STUN server", status);
907 return status;
908 }
909
Benny Prijonof76e1392008-06-06 14:51:48 +0000910 /* Create ICE stream transport configuration */
911 pj_ice_strans_cfg_default(&ice_cfg);
912 pj_stun_config_init(&ice_cfg.stun_cfg, &pjsua_var.cp.factory, 0,
913 pjsip_endpt_get_ioqueue(pjsua_var.endpt),
914 pjsip_endpt_get_timer_heap(pjsua_var.endpt));
915
916 ice_cfg.af = pj_AF_INET();
917 ice_cfg.resolver = pjsua_var.resolver;
918
Benny Prijono329d6382009-05-29 13:04:03 +0000919 ice_cfg.opt = pjsua_var.media_cfg.ice_opt;
920
Benny Prijonof76e1392008-06-06 14:51:48 +0000921 /* Configure STUN settings */
922 if (pj_sockaddr_has_addr(&pjsua_var.stun_srv)) {
923 pj_sockaddr_print(&pjsua_var.stun_srv, stunip, sizeof(stunip), 0);
924 ice_cfg.stun.server = pj_str(stunip);
925 ice_cfg.stun.port = pj_sockaddr_get_port(&pjsua_var.stun_srv);
926 }
Benny Prijono329d6382009-05-29 13:04:03 +0000927 if (pjsua_var.media_cfg.ice_max_host_cands >= 0)
928 ice_cfg.stun.max_host_cands = pjsua_var.media_cfg.ice_max_host_cands;
Benny Prijonof76e1392008-06-06 14:51:48 +0000929
Benny Prijono4d79b0f2009-10-25 09:02:07 +0000930 /* Copy QoS setting to STUN setting */
931 ice_cfg.stun.cfg.qos_type = cfg->qos_type;
932 pj_memcpy(&ice_cfg.stun.cfg.qos_params, &cfg->qos_params,
933 sizeof(cfg->qos_params));
934
Benny Prijonof76e1392008-06-06 14:51:48 +0000935 /* Configure TURN settings */
936 if (pjsua_var.media_cfg.enable_turn) {
937 status = parse_host_port(&pjsua_var.media_cfg.turn_server,
938 &ice_cfg.turn.server,
939 &ice_cfg.turn.port);
940 if (status != PJ_SUCCESS || ice_cfg.turn.server.slen == 0) {
941 PJ_LOG(1,(THIS_FILE, "Invalid TURN server setting"));
942 return PJ_EINVAL;
943 }
944 if (ice_cfg.turn.port == 0)
945 ice_cfg.turn.port = 3479;
946 ice_cfg.turn.conn_type = pjsua_var.media_cfg.turn_conn_type;
947 pj_memcpy(&ice_cfg.turn.auth_cred,
948 &pjsua_var.media_cfg.turn_auth_cred,
949 sizeof(ice_cfg.turn.auth_cred));
Benny Prijono4d79b0f2009-10-25 09:02:07 +0000950
951 /* Copy QoS setting to TURN setting */
952 ice_cfg.turn.cfg.qos_type = cfg->qos_type;
953 pj_memcpy(&ice_cfg.turn.cfg.qos_params, &cfg->qos_params,
954 sizeof(cfg->qos_params));
Benny Prijonof76e1392008-06-06 14:51:48 +0000955 }
Benny Prijonob681a2f2007-03-25 18:44:51 +0000956
Benny Prijono0bc99a92011-03-17 04:34:43 +0000957 pj_bzero(&ice_cb, sizeof(pjmedia_ice_cb));
958 ice_cb.on_ice_complete = &on_ice_complete;
959 pj_ansi_snprintf(name, sizeof(name), "icetp%02d", call_med->idx);
960 call_med->tp_ready = PJ_EPENDING;
Benny Prijonoc97608e2007-03-23 16:34:20 +0000961
Benny Prijono0bc99a92011-03-17 04:34:43 +0000962 comp_cnt = 1;
963 if (PJMEDIA_ADVERTISE_RTCP && !pjsua_var.media_cfg.ice_no_rtcp)
964 ++comp_cnt;
Benny Prijonof76e1392008-06-06 14:51:48 +0000965
Benny Prijonobd6613f2011-04-11 17:27:14 +0000966 status = pjmedia_ice_create3(pjsua_var.med_endpt, name, comp_cnt,
967 &ice_cfg, &ice_cb, 0, call_med,
968 &call_med->tp);
Benny Prijono0bc99a92011-03-17 04:34:43 +0000969 if (status != PJ_SUCCESS) {
970 pjsua_perror(THIS_FILE, "Unable to create ICE media transport",
971 status);
972 goto on_error;
Benny Prijonoc97608e2007-03-23 16:34:20 +0000973 }
Benny Prijonoeebe9af2006-06-13 22:57:13 +0000974
Benny Prijono0bc99a92011-03-17 04:34:43 +0000975 /* Wait until transport is initialized, or time out */
Sauw Ming73ecfe82011-09-21 10:20:01 +0000976 if (!async) {
977 PJSUA_UNLOCK();
978 while (call_med->tp_ready == PJ_EPENDING) {
979 pjsua_handle_events(100);
980 }
981 PJSUA_LOCK();
Benny Prijono0bc99a92011-03-17 04:34:43 +0000982 }
Sauw Ming73ecfe82011-09-21 10:20:01 +0000983
984 if (async && call_med->tp_ready == PJ_EPENDING) {
985 return PJ_EPENDING;
986 } else if (call_med->tp_ready != PJ_SUCCESS) {
Benny Prijono0bc99a92011-03-17 04:34:43 +0000987 pjsua_perror(THIS_FILE, "Error initializing ICE media transport",
988 call_med->tp_ready);
989 status = call_med->tp_ready;
990 goto on_error;
991 }
992
993 pjmedia_transport_simulate_lost(call_med->tp, PJMEDIA_DIR_ENCODING,
994 pjsua_var.media_cfg.tx_drop_pct);
995
996 pjmedia_transport_simulate_lost(call_med->tp, PJMEDIA_DIR_DECODING,
997 pjsua_var.media_cfg.rx_drop_pct);
998
Benny Prijonoeebe9af2006-06-13 22:57:13 +0000999 return PJ_SUCCESS;
1000
1001on_error:
Benny Prijono0bc99a92011-03-17 04:34:43 +00001002 if (call_med->tp != NULL) {
1003 pjmedia_transport_close(call_med->tp);
1004 call_med->tp = NULL;
Benny Prijonoeebe9af2006-06-13 22:57:13 +00001005 }
1006
Benny Prijonoc97608e2007-03-23 16:34:20 +00001007 return status;
1008}
1009
Benny Prijono0bc99a92011-03-17 04:34:43 +00001010#if DISABLED_FOR_TICKET_1185
1011/* Create ICE media transports (when ice is enabled) */
1012static pj_status_t create_ice_media_transports(pjsua_transport_config *cfg)
1013{
1014 unsigned i;
1015 pj_status_t status;
Benny Prijonoc97608e2007-03-23 16:34:20 +00001016
Benny Prijono0bc99a92011-03-17 04:34:43 +00001017 for (i=0; i < pjsua_var.ua_cfg.max_calls; ++i) {
1018 pjsua_call *call = &pjsua_var.calls[i];
1019 unsigned strm_idx;
1020
1021 for (strm_idx=0; strm_idx < call->med_cnt; ++strm_idx) {
1022 pjsua_call_media *call_med = &call->media[strm_idx];
1023
1024 status = create_ice_media_transport(cfg, call_med);
1025 if (status != PJ_SUCCESS)
1026 goto on_error;
1027 }
1028 }
1029
1030 return PJ_SUCCESS;
1031
1032on_error:
1033 for (i=0; i < pjsua_var.ua_cfg.max_calls; ++i) {
1034 pjsua_call *call = &pjsua_var.calls[i];
1035 unsigned strm_idx;
1036
1037 for (strm_idx=0; strm_idx < call->med_cnt; ++strm_idx) {
1038 pjsua_call_media *call_med = &call->media[strm_idx];
1039
1040 if (call_med->tp) {
1041 pjmedia_transport_close(call_med->tp);
1042 call_med->tp = NULL;
1043 }
1044 }
1045 }
1046 return status;
1047}
1048#endif
1049
1050#if DISABLED_FOR_TICKET_1185
Benny Prijonoc97608e2007-03-23 16:34:20 +00001051/*
Benny Prijono0bc99a92011-03-17 04:34:43 +00001052 * Create media transports for all the calls. This function creates
Benny Prijonoc97608e2007-03-23 16:34:20 +00001053 * one UDP media transport for each call.
1054 */
Benny Prijono1f61a8f2007-08-16 10:11:44 +00001055PJ_DEF(pj_status_t) pjsua_media_transports_create(
1056 const pjsua_transport_config *app_cfg)
Benny Prijonoc97608e2007-03-23 16:34:20 +00001057{
1058 pjsua_transport_config cfg;
1059 unsigned i;
1060 pj_status_t status;
1061
1062
1063 /* Make sure pjsua_init() has been called */
1064 PJ_ASSERT_RETURN(pjsua_var.ua_cfg.max_calls>0, PJ_EINVALIDOP);
1065
1066 PJSUA_LOCK();
1067
1068 /* Delete existing media transports */
1069 for (i=0; i<pjsua_var.ua_cfg.max_calls; ++i) {
Benny Prijono0bc99a92011-03-17 04:34:43 +00001070 pjsua_call *call = &pjsua_var.calls[i];
1071 unsigned strm_idx;
1072
1073 for (strm_idx=0; strm_idx < call->med_cnt; ++strm_idx) {
1074 pjsua_call_media *call_med = &call->media[strm_idx];
1075
1076 if (call_med->tp && call_med->tp_auto_del) {
1077 pjmedia_transport_close(call_med->tp);
1078 call_med->tp = NULL;
1079 call_med->tp_orig = NULL;
1080 }
Benny Prijonoc97608e2007-03-23 16:34:20 +00001081 }
1082 }
1083
1084 /* Copy config */
1085 pjsua_transport_config_dup(pjsua_var.pool, &cfg, app_cfg);
1086
Benny Prijono40860c32008-09-04 13:55:33 +00001087 /* Create the transports */
Benny Prijonoc97608e2007-03-23 16:34:20 +00001088 if (pjsua_var.media_cfg.enable_ice) {
Benny Prijono4d79b0f2009-10-25 09:02:07 +00001089 status = create_ice_media_transports(&cfg);
Benny Prijonoc97608e2007-03-23 16:34:20 +00001090 } else {
1091 status = create_udp_media_transports(&cfg);
1092 }
1093
Benny Prijono40860c32008-09-04 13:55:33 +00001094 /* Set media transport auto_delete to True */
1095 for (i=0; i<pjsua_var.ua_cfg.max_calls; ++i) {
Benny Prijono0bc99a92011-03-17 04:34:43 +00001096 pjsua_call *call = &pjsua_var.calls[i];
1097 unsigned strm_idx;
1098
1099 for (strm_idx=0; strm_idx < call->med_cnt; ++strm_idx) {
1100 pjsua_call_media *call_med = &call->media[strm_idx];
1101
1102 call_med->tp_auto_del = PJ_TRUE;
1103 }
Benny Prijono40860c32008-09-04 13:55:33 +00001104 }
Benny Prijonoc97608e2007-03-23 16:34:20 +00001105
Benny Prijonoeebe9af2006-06-13 22:57:13 +00001106 PJSUA_UNLOCK();
1107
1108 return status;
1109}
1110
Benny Prijono40860c32008-09-04 13:55:33 +00001111/*
1112 * Attach application's created media transports.
1113 */
1114PJ_DEF(pj_status_t) pjsua_media_transports_attach(pjsua_media_transport tp[],
1115 unsigned count,
1116 pj_bool_t auto_delete)
1117{
1118 unsigned i;
1119
1120 PJ_ASSERT_RETURN(tp && count==pjsua_var.ua_cfg.max_calls, PJ_EINVAL);
1121
1122 /* Assign the media transports */
1123 for (i=0; i<pjsua_var.ua_cfg.max_calls; ++i) {
Benny Prijono0bc99a92011-03-17 04:34:43 +00001124 pjsua_call *call = &pjsua_var.calls[i];
1125 unsigned strm_idx;
1126
1127 for (strm_idx=0; strm_idx < call->med_cnt; ++strm_idx) {
1128 pjsua_call_media *call_med = &call->media[strm_idx];
1129
1130 if (call_med->tp && call_med->tp_auto_del) {
1131 pjmedia_transport_close(call_med->tp);
1132 call_med->tp = NULL;
1133 call_med->tp_orig = NULL;
1134 }
Benny Prijono40860c32008-09-04 13:55:33 +00001135 }
1136
Benny Prijono0bc99a92011-03-17 04:34:43 +00001137 PJ_TODO(remove_pjsua_media_transports_attach);
1138
1139 call->media[0].tp = tp[i].transport;
1140 call->media[0].tp_auto_del = auto_delete;
Benny Prijono40860c32008-09-04 13:55:33 +00001141 }
1142
1143 return PJ_SUCCESS;
1144}
Benny Prijono0bc99a92011-03-17 04:34:43 +00001145#endif
Benny Prijono40860c32008-09-04 13:55:33 +00001146
Benny Prijono0bc99a92011-03-17 04:34:43 +00001147/* Go through the list of media in the SDP, find acceptable media, and
1148 * sort them based on the "quality" of the media, and store the indexes
1149 * in the specified array. Media with the best quality will be listed
1150 * first in the array. The quality factors considered currently is
1151 * encryption.
1152 */
1153static void sort_media(const pjmedia_sdp_session *sdp,
1154 const pj_str_t *type,
1155 pjmedia_srtp_use use_srtp,
1156 pj_uint8_t midx[],
1157 unsigned *p_count)
Nanang Izzuddin1e952a82010-10-05 16:32:04 +00001158{
1159 unsigned i;
Benny Prijono0bc99a92011-03-17 04:34:43 +00001160 unsigned count = 0;
1161 int score[PJSUA_MAX_CALL_MEDIA];
Nanang Izzuddin1e952a82010-10-05 16:32:04 +00001162
Benny Prijono0bc99a92011-03-17 04:34:43 +00001163 pj_assert(*p_count >= PJSUA_MAX_CALL_MEDIA);
1164
1165 *p_count = 0;
Benny Prijono09c0d672011-04-11 05:03:24 +00001166 for (i=0; i<PJSUA_MAX_CALL_MEDIA; ++i)
1167 score[i] = 1;
Benny Prijono0bc99a92011-03-17 04:34:43 +00001168
1169 /* Score each media */
1170 for (i=0; i<sdp->media_count && count<PJSUA_MAX_CALL_MEDIA; ++i) {
Nanang Izzuddin1e952a82010-10-05 16:32:04 +00001171 const pjmedia_sdp_media *m = sdp->media[i];
Nanang Izzuddina6414292011-04-08 04:26:18 +00001172 const pjmedia_sdp_conn *c;
Nanang Izzuddin1e952a82010-10-05 16:32:04 +00001173
Benny Prijono0bc99a92011-03-17 04:34:43 +00001174 /* Skip different media */
1175 if (pj_stricmp(&m->desc.media, type) != 0) {
1176 score[count++] = -22000;
Nanang Izzuddin1e952a82010-10-05 16:32:04 +00001177 continue;
1178 }
1179
Nanang Izzuddina6414292011-04-08 04:26:18 +00001180 c = m->conn? m->conn : sdp->conn;
1181
Benny Prijono0bc99a92011-03-17 04:34:43 +00001182 /* Supported transports */
1183 if (pj_stricmp2(&m->desc.transport, "RTP/SAVP")==0) {
1184 switch (use_srtp) {
1185 case PJMEDIA_SRTP_MANDATORY:
1186 case PJMEDIA_SRTP_OPTIONAL:
1187 ++score[i];
Nanang Izzuddin1e952a82010-10-05 16:32:04 +00001188 break;
Benny Prijono0bc99a92011-03-17 04:34:43 +00001189 case PJMEDIA_SRTP_DISABLED:
Nanang Izzuddind8c33d82011-08-18 18:30:55 +00001190 //--score[i];
1191 score[i] -= 5;
Benny Prijono0bc99a92011-03-17 04:34:43 +00001192 break;
Nanang Izzuddin1e952a82010-10-05 16:32:04 +00001193 }
Benny Prijono0bc99a92011-03-17 04:34:43 +00001194 } else if (pj_stricmp2(&m->desc.transport, "RTP/AVP")==0) {
1195 switch (use_srtp) {
1196 case PJMEDIA_SRTP_MANDATORY:
Nanang Izzuddind8c33d82011-08-18 18:30:55 +00001197 //--score[i];
1198 score[i] -= 5;
Benny Prijono0bc99a92011-03-17 04:34:43 +00001199 break;
1200 case PJMEDIA_SRTP_OPTIONAL:
1201 /* No change in score */
1202 break;
1203 case PJMEDIA_SRTP_DISABLED:
1204 ++score[i];
1205 break;
1206 }
1207 } else {
1208 score[i] -= 10;
1209 }
1210
1211 /* Is media disabled? */
1212 if (m->desc.port == 0)
1213 score[i] -= 10;
1214
1215 /* Is media inactive? */
Nanang Izzuddina6414292011-04-08 04:26:18 +00001216 if (pjmedia_sdp_media_find_attr2(m, "inactive", NULL) ||
1217 pj_strcmp2(&c->addr, "0.0.0.0") == 0)
1218 {
1219 //score[i] -= 10;
1220 score[i] -= 1;
1221 }
Benny Prijono0bc99a92011-03-17 04:34:43 +00001222
1223 ++count;
1224 }
1225
1226 /* Created sorted list based on quality */
1227 for (i=0; i<count; ++i) {
1228 unsigned j;
1229 int best = 0;
1230
1231 for (j=1; j<count; ++j) {
1232 if (score[j] > score[best])
1233 best = j;
1234 }
1235 /* Don't put media with negative score, that media is unacceptable
1236 * for us.
1237 */
1238 if (score[best] >= 0) {
1239 midx[*p_count] = (pj_uint8_t)best;
1240 (*p_count)++;
1241 }
1242
1243 score[best] = -22000;
1244
1245 }
1246}
1247
Benny Prijonoee0ba182011-07-15 06:18:29 +00001248/* Callback to receive media events */
1249static pj_status_t call_media_on_event(pjmedia_event_subscription *esub,
1250 pjmedia_event *event)
1251{
1252 pjsua_call_media *call_med = (pjsua_call_media*)esub->user_data;
1253 pjsua_call *call = call_med->call;
1254
1255 if (pjsua_var.ua_cfg.cb.on_call_media_event && call) {
1256 ++event->proc_cnt;
1257 (*pjsua_var.ua_cfg.cb.on_call_media_event)(call->index,
1258 call_med->idx, event);
Benny Prijono53a7c702008-04-14 02:57:29 +00001259 }
1260
1261 return PJ_SUCCESS;
1262}
1263
Sauw Ming73ecfe82011-09-21 10:20:01 +00001264/* Set media transport state and notify the application via the callback. */
1265void set_media_tp_state(pjsua_call_media *call_med,
1266 pjsua_med_tp_st tp_st)
1267{
1268 if (pjsua_var.ua_cfg.cb.on_call_media_transport_state &&
1269 call_med->tp_st != tp_st)
1270 {
1271 pjsua_med_tp_state_info info;
1272
1273 pj_bzero(&info, sizeof(info));
1274 info.med_idx = call_med->idx;
1275 info.state = tp_st;
1276 info.status = call_med->tp_ready;
1277 (*pjsua_var.ua_cfg.cb.on_call_media_transport_state)(
1278 call_med->call->index, &info);
1279 }
1280
1281 call_med->tp_st = tp_st;
1282}
1283
1284/* Callback to resume pjsua_call_media_init() after media transport
1285 * creation is completed.
1286 */
1287static pj_status_t call_media_init_cb(pjsua_call_media *call_med,
1288 pj_status_t status,
1289 int security_level,
1290 int *sip_err_code)
Nanang Izzuddin1e952a82010-10-05 16:32:04 +00001291{
Benny Prijono0bc99a92011-03-17 04:34:43 +00001292 pjsua_acc *acc = &pjsua_var.acc[call_med->call->acc_id];
Sauw Ming73ecfe82011-09-21 10:20:01 +00001293 int err_code = 0;
Nanang Izzuddin1e952a82010-10-05 16:32:04 +00001294
Sauw Ming73ecfe82011-09-21 10:20:01 +00001295 if (status != PJ_SUCCESS)
1296 goto on_error;
Nanang Izzuddin1e952a82010-10-05 16:32:04 +00001297
Sauw Ming73ecfe82011-09-21 10:20:01 +00001298 if (call_med->tp_st == PJSUA_MED_TP_CREATING)
1299 set_media_tp_state(call_med, PJSUA_MED_TP_IDLE);
Nanang Izzuddin1e952a82010-10-05 16:32:04 +00001300
Sauw Minge7dbbc82011-10-24 09:28:13 +00001301 if (!call_med->tp_orig &&
1302 pjsua_var.ua_cfg.cb.on_create_media_transport)
1303 {
1304 call_med->use_custom_med_tp = PJ_TRUE;
1305 } else
1306 call_med->use_custom_med_tp = PJ_FALSE;
1307
Benny Prijono0bc99a92011-03-17 04:34:43 +00001308#if defined(PJMEDIA_HAS_SRTP) && (PJMEDIA_HAS_SRTP != 0)
1309 /* This function may be called when SRTP transport already exists
1310 * (e.g: in re-invite, update), don't need to destroy/re-create.
1311 */
Sauw Minge7dbbc82011-10-24 09:28:13 +00001312 if (!call_med->tp_orig) {
Benny Prijono0bc99a92011-03-17 04:34:43 +00001313 pjmedia_srtp_setting srtp_opt;
1314 pjmedia_transport *srtp = NULL;
Nanang Izzuddin1e952a82010-10-05 16:32:04 +00001315
Benny Prijono0bc99a92011-03-17 04:34:43 +00001316 /* Check if SRTP requires secure signaling */
1317 if (acc->cfg.use_srtp != PJMEDIA_SRTP_DISABLED) {
1318 if (security_level < acc->cfg.srtp_secure_signaling) {
Sauw Ming73ecfe82011-09-21 10:20:01 +00001319 err_code = PJSIP_SC_NOT_ACCEPTABLE;
Benny Prijono0bc99a92011-03-17 04:34:43 +00001320 status = PJSIP_ESESSIONINSECURE;
1321 goto on_error;
1322 }
1323 }
1324
1325 /* Always create SRTP adapter */
1326 pjmedia_srtp_setting_default(&srtp_opt);
Nanang Izzuddinb6c239c2011-05-10 05:42:28 +00001327 srtp_opt.close_member_tp = PJ_TRUE;
Sauw Minge7dbbc82011-10-24 09:28:13 +00001328 /* If media session has been ever established, let's use remote's
Benny Prijono0bc99a92011-03-17 04:34:43 +00001329 * preference in SRTP usage policy, especially when it is stricter.
1330 */
1331 if (call_med->rem_srtp_use > acc->cfg.use_srtp)
1332 srtp_opt.use = call_med->rem_srtp_use;
1333 else
1334 srtp_opt.use = acc->cfg.use_srtp;
1335
1336 status = pjmedia_transport_srtp_create(pjsua_var.med_endpt,
1337 call_med->tp,
1338 &srtp_opt, &srtp);
1339 if (status != PJ_SUCCESS) {
Sauw Ming73ecfe82011-09-21 10:20:01 +00001340 err_code = PJSIP_SC_INTERNAL_SERVER_ERROR;
Benny Prijono0bc99a92011-03-17 04:34:43 +00001341 goto on_error;
1342 }
1343
1344 /* Set SRTP as current media transport */
1345 call_med->tp_orig = call_med->tp;
1346 call_med->tp = srtp;
1347 }
1348#else
Benny Prijono7df19342011-07-23 02:54:03 +00001349 call_med->tp_orig = call_med->tp;
Benny Prijono0bc99a92011-03-17 04:34:43 +00001350 PJ_UNUSED_ARG(security_level);
1351#endif
1352
Benny Prijono0bc99a92011-03-17 04:34:43 +00001353on_error:
Sauw Ming73ecfe82011-09-21 10:20:01 +00001354 if (status != PJ_SUCCESS && call_med->tp) {
Benny Prijono0bc99a92011-03-17 04:34:43 +00001355 pjmedia_transport_close(call_med->tp);
1356 call_med->tp = NULL;
1357 }
Sauw Ming73ecfe82011-09-21 10:20:01 +00001358
1359 if (sip_err_code)
1360 *sip_err_code = err_code;
1361
1362 if (call_med->med_init_cb) {
1363 pjsua_med_tp_state_info info;
1364
1365 pj_bzero(&info, sizeof(info));
1366 info.status = status;
1367 info.state = call_med->tp_st;
1368 info.med_idx = call_med->idx;
1369 info.sip_err_code = err_code;
1370 (*call_med->med_init_cb)(call_med->call->index, &info);
1371 }
1372
1373 return status;
1374}
1375
1376/* Initialize the media line */
1377pj_status_t pjsua_call_media_init(pjsua_call_media *call_med,
1378 pjmedia_type type,
1379 const pjsua_transport_config *tcfg,
1380 int security_level,
1381 int *sip_err_code,
1382 pj_bool_t async,
1383 pjsua_med_tp_state_cb cb)
1384{
1385 pjsua_acc *acc = &pjsua_var.acc[call_med->call->acc_id];
1386 pj_status_t status = PJ_SUCCESS;
1387
1388 /*
1389 * Note: this function may be called when the media already exists
1390 * (e.g. in reinvites, updates, etc.)
1391 */
1392 call_med->type = type;
1393
1394 /* Create the media transport for initial call. */
1395 if (call_med->tp == NULL) {
1396#if defined(PJMEDIA_HAS_VIDEO) && (PJMEDIA_HAS_VIDEO != 0)
1397 /* While in initial call, set default video devices */
1398 if (type == PJMEDIA_TYPE_VIDEO) {
1399 call_med->strm.v.rdr_dev = acc->cfg.vid_rend_dev;
1400 call_med->strm.v.cap_dev = acc->cfg.vid_cap_dev;
1401 if (call_med->strm.v.rdr_dev == PJMEDIA_VID_DEFAULT_RENDER_DEV) {
1402 pjmedia_vid_dev_info info;
1403 pjmedia_vid_dev_get_info(call_med->strm.v.rdr_dev, &info);
1404 call_med->strm.v.rdr_dev = info.id;
1405 }
1406 if (call_med->strm.v.cap_dev == PJMEDIA_VID_DEFAULT_CAPTURE_DEV) {
1407 pjmedia_vid_dev_info info;
1408 pjmedia_vid_dev_get_info(call_med->strm.v.cap_dev, &info);
1409 call_med->strm.v.cap_dev = info.id;
1410 }
Nanang Izzuddinc12a19d2011-10-03 05:23:59 +00001411
1412 /* Init event subscribtion */
1413 pjmedia_event_subscription_init(&call_med->esub_rend, &call_media_on_event,
1414 call_med);
1415 pjmedia_event_subscription_init(&call_med->esub_cap, &call_media_on_event,
1416 call_med);
Sauw Ming73ecfe82011-09-21 10:20:01 +00001417 }
1418#endif
1419
1420 set_media_tp_state(call_med, PJSUA_MED_TP_CREATING);
1421
1422 if (async) {
1423 call_med->med_create_cb = &call_media_init_cb;
1424 call_med->med_init_cb = cb;
1425 }
1426
1427 if (pjsua_var.media_cfg.enable_ice) {
1428 status = create_ice_media_transport(tcfg, call_med, async);
Sauw Ming848742f2011-09-28 04:20:30 +00001429 if (async && status == PJ_SUCCESS) {
1430 /* Callback has been called. */
1431 call_med->med_init_cb = NULL;
1432 /* We cannot return PJ_SUCCESS here since we already call
1433 * the callback.
1434 */
1435 return PJ_EPENDING;
1436 } else if (async && status == PJ_EPENDING) {
1437 /* We will resume call media initialization in the
1438 * on_ice_complete() callback.
1439 */
1440 return PJ_EPENDING;
1441 }
Sauw Ming73ecfe82011-09-21 10:20:01 +00001442 } else {
1443 status = create_udp_media_transport(tcfg, call_med);
1444 }
1445
Sauw Ming848742f2011-09-28 04:20:30 +00001446 if (status != PJ_SUCCESS) {
Sauw Ming73ecfe82011-09-21 10:20:01 +00001447 PJ_PERROR(1,(THIS_FILE, status, "Error creating media transport"));
1448 return status;
1449 }
1450
1451 /* Media transport creation completed immediately, so
1452 * we don't need to call the callback.
1453 */
1454 call_med->med_init_cb = NULL;
1455
1456 } else if (call_med->tp_st == PJSUA_MED_TP_DISABLED) {
1457 /* Media is being reenabled. */
1458 set_media_tp_state(call_med, PJSUA_MED_TP_INIT);
1459 }
1460
1461 return call_media_init_cb(call_med, status, security_level,
1462 sip_err_code);
1463}
1464
1465/* Callback to resume pjsua_media_channel_init() after media transport
1466 * initialization is completed.
1467 */
1468static pj_status_t media_channel_init_cb(pjsua_call_id call_id,
1469 const pjsua_med_tp_state_info *info)
1470{
1471 pjsua_call *call = &pjsua_var.calls[call_id];
1472 pj_status_t status = (info? info->status : PJ_SUCCESS);
1473 unsigned mi;
1474
1475 if (info) {
1476 pj_mutex_lock(call->med_ch_mutex);
1477
1478 /* Set the callback to NULL to indicate that the async operation
1479 * has completed.
1480 */
1481 call->media[info->med_idx].med_init_cb = NULL;
1482
1483 /* In case of failure, save the information to be returned
1484 * by the last media transport to finish.
1485 */
1486 if (info->status != PJ_SUCCESS)
1487 pj_memcpy(&call->med_ch_info, info, sizeof(info));
1488
1489 /* Check whether all the call's medias have finished calling their
1490 * callbacks.
1491 */
1492 for (mi=0; mi < call->med_cnt; ++mi) {
1493 pjsua_call_media *call_med = &call->media[mi];
1494
Sauw Ming3a55bb92011-10-06 06:49:09 +00001495 if (call_med->med_init_cb ||
1496 call_med->tp_st == PJSUA_MED_TP_NULL)
1497 {
Sauw Ming73ecfe82011-09-21 10:20:01 +00001498 pj_mutex_unlock(call->med_ch_mutex);
1499 return PJ_SUCCESS;
1500 }
1501
1502 if (call_med->tp_ready != PJ_SUCCESS)
1503 status = call_med->tp_ready;
1504 }
1505
1506 /* OK, we are called by the last media transport finished. */
1507 pj_mutex_unlock(call->med_ch_mutex);
1508 }
1509
1510 if (call->med_ch_mutex) {
1511 pj_mutex_destroy(call->med_ch_mutex);
1512 call->med_ch_mutex = NULL;
1513 }
1514
1515 if (status != PJ_SUCCESS) {
1516 pjsua_media_channel_deinit(call_id);
1517 goto on_error;
1518 }
1519
1520 /* Tell the media transport of a new offer/answer session */
1521 for (mi=0; mi < call->med_cnt; ++mi) {
1522 pjsua_call_media *call_med = &call->media[mi];
1523
1524 /* Note: tp may be NULL if this media line is disabled */
1525 if (call_med->tp && call_med->tp_st == PJSUA_MED_TP_IDLE) {
Sauw Ming99cc8ff2011-09-22 04:24:56 +00001526 pj_pool_t *tmp_pool = call->async_call.pool_prov;
1527
1528 if (!tmp_pool) {
1529 tmp_pool = (call->inv? call->inv->pool_prov:
1530 call->async_call.dlg->pool);
1531 }
Sauw Ming73ecfe82011-09-21 10:20:01 +00001532
Sauw Minge7dbbc82011-10-24 09:28:13 +00001533 if (call_med->use_custom_med_tp) {
1534 unsigned custom_med_tp_flags = 0;
1535
1536 /* Use custom media transport returned by the application */
1537 call_med->tp =
1538 (*pjsua_var.ua_cfg.cb.on_create_media_transport)
1539 (call_id, mi, call_med->tp,
1540 custom_med_tp_flags);
1541 if (!call_med->tp) {
1542 status =
1543 PJSIP_ERRNO_FROM_SIP_STATUS(PJSIP_SC_NOT_ACCEPTABLE);
1544 }
1545 }
1546
1547 if (call_med->tp) {
1548 status = pjmedia_transport_media_create(
1549 call_med->tp, tmp_pool,
1550 0, call->async_call.rem_sdp, mi);
1551 }
Sauw Ming73ecfe82011-09-21 10:20:01 +00001552 if (status != PJ_SUCCESS) {
1553 call->med_ch_info.status = status;
1554 call->med_ch_info.med_idx = mi;
1555 call->med_ch_info.state = call_med->tp_st;
1556 call->med_ch_info.sip_err_code = PJSIP_SC_NOT_ACCEPTABLE;
1557 pjsua_media_channel_deinit(call_id);
1558 goto on_error;
1559 }
1560
1561 set_media_tp_state(call_med, PJSUA_MED_TP_INIT);
1562 }
1563 }
1564
1565 call->med_ch_info.status = PJ_SUCCESS;
1566
1567on_error:
1568 if (call->med_ch_cb)
1569 (*call->med_ch_cb)(call->index, &call->med_ch_info);
1570
Benny Prijono0bc99a92011-03-17 04:34:43 +00001571 return status;
1572}
Nanang Izzuddin1e952a82010-10-05 16:32:04 +00001573
Benny Prijonod8179652008-01-23 20:39:07 +00001574pj_status_t pjsua_media_channel_init(pjsua_call_id call_id,
1575 pjsip_role_e role,
1576 int security_level,
1577 pj_pool_t *tmp_pool,
1578 const pjmedia_sdp_session *rem_sdp,
Sauw Ming73ecfe82011-09-21 10:20:01 +00001579 int *sip_err_code,
1580 pj_bool_t async,
1581 pjsua_med_tp_state_cb cb)
Benny Prijonoc97608e2007-03-23 16:34:20 +00001582{
Benny Prijono0bc99a92011-03-17 04:34:43 +00001583 const pj_str_t STR_AUDIO = { "audio", 5 };
1584 const pj_str_t STR_VIDEO = { "video", 5 };
Benny Prijonod8179652008-01-23 20:39:07 +00001585 pjsua_call *call = &pjsua_var.calls[call_id];
Benny Prijonod8179652008-01-23 20:39:07 +00001586 pjsua_acc *acc = &pjsua_var.acc[call->acc_id];
Benny Prijono0bc99a92011-03-17 04:34:43 +00001587 pj_uint8_t maudidx[PJSUA_MAX_CALL_MEDIA];
1588 unsigned maudcnt = PJ_ARRAY_SIZE(maudidx);
1589 pj_uint8_t mvididx[PJSUA_MAX_CALL_MEDIA];
1590 unsigned mvidcnt = PJ_ARRAY_SIZE(mvididx);
1591 pjmedia_type media_types[PJSUA_MAX_CALL_MEDIA];
1592 unsigned mi;
Sauw Ming73ecfe82011-09-21 10:20:01 +00001593 pj_bool_t pending_med_tp = PJ_FALSE;
Benny Prijono0bc99a92011-03-17 04:34:43 +00001594 pj_status_t status;
Benny Prijonoc97608e2007-03-23 16:34:20 +00001595
Benny Prijonod8179652008-01-23 20:39:07 +00001596 PJ_UNUSED_ARG(role);
1597
Nanang Izzuddinb6c239c2011-05-10 05:42:28 +00001598 /*
1599 * Note: this function may be called when the media already exists
1600 * (e.g. in reinvites, updates, etc).
1601 */
1602
Benny Prijono0bc99a92011-03-17 04:34:43 +00001603 if (pjsua_get_state() != PJSUA_STATE_RUNNING)
1604 return PJ_EBUSY;
1605
Sauw Ming73ecfe82011-09-21 10:20:01 +00001606 if (async) {
1607 pj_pool_t *tmppool = (call->inv? call->inv->pool_prov:
1608 call->async_call.dlg->pool);
1609
1610 status = pj_mutex_create_simple(tmppool, NULL, &call->med_ch_mutex);
1611 if (status != PJ_SUCCESS)
1612 return status;
1613 }
1614
Benny Prijonob90fd382011-09-18 14:59:56 +00001615 PJ_LOG(4,(THIS_FILE, "Call %d: initializing media..", call_id));
1616 pj_log_push_indent();
1617
Benny Prijono0bc99a92011-03-17 04:34:43 +00001618#if DISABLED_FOR_TICKET_1185
Benny Prijonod8179652008-01-23 20:39:07 +00001619 /* Return error if media transport has not been created yet
Benny Prijono68f9e4f2008-03-21 08:56:02 +00001620 * (e.g. application is starting)
1621 */
Benny Prijono0bc99a92011-03-17 04:34:43 +00001622 for (i=0; i<call->med_cnt; ++i) {
1623 if (call->media[i].tp == NULL) {
Benny Prijonob90fd382011-09-18 14:59:56 +00001624 status = PJ_EBUSY;
1625 goto on_error;
Benny Prijonod8179652008-01-23 20:39:07 +00001626 }
Benny Prijonoc97608e2007-03-23 16:34:20 +00001627 }
Benny Prijonod8179652008-01-23 20:39:07 +00001628#endif
Benny Prijonoc97608e2007-03-23 16:34:20 +00001629
Benny Prijono0bc99a92011-03-17 04:34:43 +00001630 if (rem_sdp) {
1631 sort_media(rem_sdp, &STR_AUDIO, acc->cfg.use_srtp,
1632 maudidx, &maudcnt);
Nanang Izzuddind8c33d82011-08-18 18:30:55 +00001633 // Don't apply media count limitation until SDP negotiation is done.
1634 //if (maudcnt > acc->cfg.max_audio_cnt)
1635 // maudcnt = acc->cfg.max_audio_cnt;
Benny Prijonoc97608e2007-03-23 16:34:20 +00001636
Benny Prijono0bc99a92011-03-17 04:34:43 +00001637 if (maudcnt==0) {
1638 /* Expecting audio in the offer */
1639 if (sip_err_code) *sip_err_code = PJSIP_SC_NOT_ACCEPTABLE_HERE;
1640 pjsua_media_channel_deinit(call_id);
Benny Prijonob90fd382011-09-18 14:59:56 +00001641 status = PJSIP_ERRNO_FROM_SIP_STATUS(PJSIP_SC_NOT_ACCEPTABLE_HERE);
1642 goto on_error;
Benny Prijonoc97608e2007-03-23 16:34:20 +00001643 }
1644
Nanang Izzuddind8c33d82011-08-18 18:30:55 +00001645#if PJMEDIA_HAS_VIDEO
Benny Prijono0bc99a92011-03-17 04:34:43 +00001646 sort_media(rem_sdp, &STR_VIDEO, acc->cfg.use_srtp,
1647 mvididx, &mvidcnt);
Nanang Izzuddind8c33d82011-08-18 18:30:55 +00001648 // Don't apply media count limitation until SDP negotiation is done.
1649 //if (mvidcnt > acc->cfg.max_video_cnt)
1650 //mvidcnt = acc->cfg.max_video_cnt;
1651#else
1652 mvidcnt = 0;
1653#endif
Nanang Izzuddinb6c239c2011-05-10 05:42:28 +00001654
1655 /* Update media count only when remote add any media, this media count
1656 * must never decrease.
Benny Prijonod8179652008-01-23 20:39:07 +00001657 */
Nanang Izzuddinb6c239c2011-05-10 05:42:28 +00001658 if (call->med_cnt < rem_sdp->media_count)
1659 call->med_cnt = PJ_MIN(rem_sdp->media_count, PJSUA_MAX_CALL_MEDIA);
Benny Prijonod8179652008-01-23 20:39:07 +00001660
Benny Prijono0bc99a92011-03-17 04:34:43 +00001661 } else {
1662 maudcnt = acc->cfg.max_audio_cnt;
1663 for (mi=0; mi<maudcnt; ++mi) {
Nanang Izzuddinbf26db12011-03-18 07:54:50 +00001664 maudidx[mi] = (pj_uint8_t)mi;
Benny Prijono0bc99a92011-03-17 04:34:43 +00001665 media_types[mi] = PJMEDIA_TYPE_AUDIO;
Benny Prijonod8179652008-01-23 20:39:07 +00001666 }
Nanang Izzuddind8c33d82011-08-18 18:30:55 +00001667#if PJMEDIA_HAS_VIDEO
Benny Prijono0bc99a92011-03-17 04:34:43 +00001668 mvidcnt = acc->cfg.max_video_cnt;
1669 for (mi=0; mi<mvidcnt; ++mi) {
1670 media_types[maudcnt + mi] = PJMEDIA_TYPE_VIDEO;
1671 }
Nanang Izzuddind8c33d82011-08-18 18:30:55 +00001672#else
1673 mvidcnt = 0;
1674#endif
Nanang Izzuddinb6c239c2011-05-10 05:42:28 +00001675 call->med_cnt = maudcnt + mvidcnt;
Benny Prijonoa310bd22008-06-27 21:19:44 +00001676 }
1677
Benny Prijono0bc99a92011-03-17 04:34:43 +00001678 if (call->med_cnt == 0) {
1679 /* Expecting at least one media */
Benny Prijonoab8dba92008-06-27 21:59:15 +00001680 if (sip_err_code) *sip_err_code = PJSIP_SC_NOT_ACCEPTABLE_HERE;
Benny Prijonoa310bd22008-06-27 21:19:44 +00001681 pjsua_media_channel_deinit(call_id);
Benny Prijonob90fd382011-09-18 14:59:56 +00001682 status = PJSIP_ERRNO_FROM_SIP_STATUS(PJSIP_SC_NOT_ACCEPTABLE_HERE);
1683 goto on_error;
Benny Prijonoa310bd22008-06-27 21:19:44 +00001684 }
1685
Sauw Ming73ecfe82011-09-21 10:20:01 +00001686 if (async) {
1687 call->med_ch_cb = cb;
1688 if (rem_sdp) {
Sauw Ming73ecfe82011-09-21 10:20:01 +00001689 call->async_call.rem_sdp =
1690 pjmedia_sdp_session_clone(call->inv->pool_prov, rem_sdp);
1691 }
1692 }
1693
Sauw Ming99cc8ff2011-09-22 04:24:56 +00001694 call->async_call.pool_prov = tmp_pool;
1695
Benny Prijono0bc99a92011-03-17 04:34:43 +00001696 /* Initialize each media line */
1697 for (mi=0; mi < call->med_cnt; ++mi) {
1698 pjsua_call_media *call_med = &call->media[mi];
1699 pj_bool_t enabled = PJ_FALSE;
1700 pjmedia_type media_type = PJMEDIA_TYPE_NONE;
Benny Prijonoa310bd22008-06-27 21:19:44 +00001701
Benny Prijono0bc99a92011-03-17 04:34:43 +00001702 if (rem_sdp) {
Nanang Izzuddinb6c239c2011-05-10 05:42:28 +00001703 if (mi >= rem_sdp->media_count) {
1704 /* Media has been removed in remote re-offer */
1705 media_type = call_med->type;
1706 } else if (!pj_stricmp(&rem_sdp->media[mi]->desc.media, &STR_AUDIO)) {
Benny Prijono0bc99a92011-03-17 04:34:43 +00001707 media_type = PJMEDIA_TYPE_AUDIO;
1708 if (pj_memchr(maudidx, mi, maudcnt * sizeof(maudidx[0]))) {
1709 enabled = PJ_TRUE;
1710 }
1711 }
1712 else if (!pj_stricmp(&rem_sdp->media[mi]->desc.media, &STR_VIDEO)) {
1713 media_type = PJMEDIA_TYPE_VIDEO;
1714 if (pj_memchr(mvididx, mi, mvidcnt * sizeof(mvididx[0]))) {
1715 enabled = PJ_TRUE;
1716 }
1717 }
1718
1719 } else {
1720 enabled = PJ_TRUE;
1721 media_type = media_types[mi];
1722 }
1723
1724 if (enabled) {
1725 status = pjsua_call_media_init(call_med, media_type,
1726 &acc->cfg.rtp_cfg,
Sauw Ming73ecfe82011-09-21 10:20:01 +00001727 security_level, sip_err_code,
1728 async,
Sauw Ming3a55bb92011-10-06 06:49:09 +00001729 (async? &media_channel_init_cb:
1730 NULL));
Sauw Ming73ecfe82011-09-21 10:20:01 +00001731 if (status == PJ_EPENDING) {
1732 pending_med_tp = PJ_TRUE;
1733 } else if (status != PJ_SUCCESS) {
1734 if (pending_med_tp) {
1735 /* Save failure information. */
1736 call_med->tp_ready = status;
1737 pj_bzero(&call->med_ch_info, sizeof(call->med_ch_info));
1738 call->med_ch_info.status = status;
1739 call->med_ch_info.state = call_med->tp_st;
1740 call->med_ch_info.med_idx = call_med->idx;
1741 if (sip_err_code)
1742 call->med_ch_info.sip_err_code = *sip_err_code;
1743
1744 /* We will return failure in the callback later. */
1745 return PJ_EPENDING;
1746 }
1747
1748 pjsua_media_channel_deinit(call_id);
Benny Prijonob90fd382011-09-18 14:59:56 +00001749 goto on_error;
Benny Prijono0bc99a92011-03-17 04:34:43 +00001750 }
1751 } else {
Nanang Izzuddinb6c239c2011-05-10 05:42:28 +00001752 /* By convention, the media is disabled if transport is NULL
1753 * or transport state is PJSUA_MED_TP_DISABLED.
1754 */
Benny Prijono0bc99a92011-03-17 04:34:43 +00001755 if (call_med->tp) {
Nanang Izzuddinb6c239c2011-05-10 05:42:28 +00001756 // Don't close transport here, as SDP negotiation has not been
1757 // done and stream may be still active.
1758 //pjmedia_transport_close(call_med->tp);
1759 //call_med->tp = NULL;
1760 pj_assert(call_med->tp_st == PJSUA_MED_TP_INIT ||
1761 call_med->tp_st == PJSUA_MED_TP_RUNNING);
Sauw Ming73ecfe82011-09-21 10:20:01 +00001762 set_media_tp_state(call_med, PJSUA_MED_TP_DISABLED);
Benny Prijono0bc99a92011-03-17 04:34:43 +00001763 }
Nanang Izzuddinb6c239c2011-05-10 05:42:28 +00001764
1765 /* Put media type just for info */
1766 call_med->type = media_type;
Benny Prijono0bc99a92011-03-17 04:34:43 +00001767 }
Benny Prijono224b4e22008-06-19 14:10:28 +00001768 }
1769
Benny Prijono0bc99a92011-03-17 04:34:43 +00001770 call->audio_idx = maudidx[0];
1771
1772 PJ_LOG(4,(THIS_FILE, "Media index %d selected for audio call %d",
1773 call->audio_idx, call->index));
1774
Sauw Ming73ecfe82011-09-21 10:20:01 +00001775 if (pending_med_tp) {
Sauw Ming99cc8ff2011-09-22 04:24:56 +00001776 /* We shouldn't use temporary pool anymore. */
1777 call->async_call.pool_prov = NULL;
Sauw Ming73ecfe82011-09-21 10:20:01 +00001778 /* We have a pending media transport initialization. */
1779 pj_log_pop_indent();
1780 return PJ_EPENDING;
Benny Prijono0bc99a92011-03-17 04:34:43 +00001781 }
1782
Sauw Ming73ecfe82011-09-21 10:20:01 +00001783 /* Media transport initialization completed immediately, so
1784 * we don't need to call the callback.
1785 */
1786 call->med_ch_cb = NULL;
1787
1788 status = media_channel_init_cb(call_id, NULL);
1789 if (status != PJ_SUCCESS && sip_err_code)
1790 *sip_err_code = call->med_ch_info.sip_err_code;
1791
Benny Prijonob90fd382011-09-18 14:59:56 +00001792 pj_log_pop_indent();
Sauw Ming73ecfe82011-09-21 10:20:01 +00001793 return status;
Benny Prijonob90fd382011-09-18 14:59:56 +00001794
1795on_error:
Sauw Ming73ecfe82011-09-21 10:20:01 +00001796 if (call->med_ch_mutex) {
1797 pj_mutex_destroy(call->med_ch_mutex);
1798 call->med_ch_mutex = NULL;
1799 }
1800
Benny Prijonob90fd382011-09-18 14:59:56 +00001801 pj_log_pop_indent();
1802 return status;
Benny Prijonoc97608e2007-03-23 16:34:20 +00001803}
1804
1805pj_status_t pjsua_media_channel_create_sdp(pjsua_call_id call_id,
1806 pj_pool_t *pool,
Benny Prijonod8179652008-01-23 20:39:07 +00001807 const pjmedia_sdp_session *rem_sdp,
Benny Prijono25b2ea12008-01-24 19:20:54 +00001808 pjmedia_sdp_session **p_sdp,
Benny Prijono0bc99a92011-03-17 04:34:43 +00001809 int *sip_err_code)
Benny Prijonoc97608e2007-03-23 16:34:20 +00001810{
Benny Prijono0bc99a92011-03-17 04:34:43 +00001811 enum { MAX_MEDIA = PJSUA_MAX_CALL_MEDIA };
Benny Prijonoc97608e2007-03-23 16:34:20 +00001812 pjmedia_sdp_session *sdp;
Benny Prijono0bc99a92011-03-17 04:34:43 +00001813 pj_sockaddr origin;
Benny Prijonoc97608e2007-03-23 16:34:20 +00001814 pjsua_call *call = &pjsua_var.calls[call_id];
Nanang Izzuddin1e952a82010-10-05 16:32:04 +00001815 pjmedia_sdp_neg_state sdp_neg_state = PJMEDIA_SDP_NEG_STATE_NULL;
Benny Prijono0bc99a92011-03-17 04:34:43 +00001816 unsigned mi;
Benny Prijonoc97608e2007-03-23 16:34:20 +00001817 pj_status_t status;
1818
Benny Prijono0bc99a92011-03-17 04:34:43 +00001819 if (pjsua_get_state() != PJSUA_STATE_RUNNING)
Benny Prijono55e82352007-05-10 20:49:08 +00001820 return PJ_EBUSY;
Benny Prijono55e82352007-05-10 20:49:08 +00001821
Benny Prijono0bc99a92011-03-17 04:34:43 +00001822 if (rem_sdp) {
Nanang Izzuddinb6c239c2011-05-10 05:42:28 +00001823 /* If this is a re-offer, let's re-initialize media as remote may
1824 * add or remove media
1825 */
1826 if (call->inv && call->inv->state == PJSIP_INV_STATE_CONFIRMED) {
1827 status = pjsua_media_channel_init(call_id, PJSIP_ROLE_UAS,
1828 call->secure_level, pool,
Sauw Ming73ecfe82011-09-21 10:20:01 +00001829 rem_sdp, sip_err_code,
1830 PJ_FALSE, NULL);
Nanang Izzuddinb6c239c2011-05-10 05:42:28 +00001831 if (status != PJ_SUCCESS)
1832 return status;
Benny Prijono0324ba52010-12-02 10:41:46 +00001833 }
Nanang Izzuddinb6c239c2011-05-10 05:42:28 +00001834
1835#if 0
1836 pjsua_acc *acc = &pjsua_var.acc[call->acc_id];
Benny Prijono0bc99a92011-03-17 04:34:43 +00001837 pj_uint8_t maudidx[PJSUA_MAX_CALL_MEDIA];
1838 unsigned maudcnt = PJ_ARRAY_SIZE(maudidx);
Nanang Izzuddin3150d8b2010-12-01 08:20:28 +00001839
Benny Prijono0bc99a92011-03-17 04:34:43 +00001840 sort_media(rem_sdp, &STR_AUDIO, acc->cfg.use_srtp,
1841 maudidx, &maudcnt);
Nanang Izzuddin3150d8b2010-12-01 08:20:28 +00001842
Benny Prijono0bc99a92011-03-17 04:34:43 +00001843 if (maudcnt==0) {
1844 /* Expecting audio in the offer */
1845 if (sip_err_code) *sip_err_code = PJSIP_SC_NOT_ACCEPTABLE_HERE;
1846 pjsua_media_channel_deinit(call_id);
1847 return PJSIP_ERRNO_FROM_SIP_STATUS(PJSIP_SC_NOT_ACCEPTABLE_HERE);
Benny Prijono224b4e22008-06-19 14:10:28 +00001848 }
Benny Prijono224b4e22008-06-19 14:10:28 +00001849
Benny Prijono0bc99a92011-03-17 04:34:43 +00001850 call->audio_idx = maudidx[0];
Nanang Izzuddinb6c239c2011-05-10 05:42:28 +00001851#endif
Benny Prijono0bc99a92011-03-17 04:34:43 +00001852 } else {
1853 /* Audio is first in our offer, by convention */
Nanang Izzuddinb6c239c2011-05-10 05:42:28 +00001854 // The audio_idx should not be changed here, as this function may be
1855 // called in generating re-offer and the current active audio index
1856 // can be anywhere.
1857 //call->audio_idx = 0;
Benny Prijono224b4e22008-06-19 14:10:28 +00001858 }
1859
Nanang Izzuddinb6c239c2011-05-10 05:42:28 +00001860#if 0
1861 // Since r3512, old-style hold should have got transport, created by
1862 // pjsua_media_channel_init() in initial offer/answer or remote reoffer.
Benny Prijono224b4e22008-06-19 14:10:28 +00001863 /* Create media if it's not created. This could happen when call is
Benny Prijono0bc99a92011-03-17 04:34:43 +00001864 * currently on-hold (with the old style hold)
Benny Prijono224b4e22008-06-19 14:10:28 +00001865 */
Benny Prijono0bc99a92011-03-17 04:34:43 +00001866 if (call->media[call->audio_idx].tp == NULL) {
Benny Prijono224b4e22008-06-19 14:10:28 +00001867 pjsip_role_e role;
1868 role = (rem_sdp ? PJSIP_ROLE_UAS : PJSIP_ROLE_UAC);
1869 status = pjsua_media_channel_init(call_id, role, call->secure_level,
Benny Prijono0bc99a92011-03-17 04:34:43 +00001870 pool, rem_sdp, sip_err_code);
Benny Prijono224b4e22008-06-19 14:10:28 +00001871 if (status != PJ_SUCCESS)
1872 return status;
1873 }
Nanang Izzuddinb6c239c2011-05-10 05:42:28 +00001874#endif
Benny Prijono224b4e22008-06-19 14:10:28 +00001875
Nanang Izzuddin1e952a82010-10-05 16:32:04 +00001876 /* Get SDP negotiator state */
1877 if (call->inv && call->inv->neg)
1878 sdp_neg_state = pjmedia_sdp_neg_get_state(call->inv->neg);
1879
Benny Prijono0bc99a92011-03-17 04:34:43 +00001880 /* Get one address to use in the origin field */
1881 pj_bzero(&origin, sizeof(origin));
1882 for (mi=0; mi<call->med_cnt; ++mi) {
1883 pjmedia_transport_info tpinfo;
Benny Prijono617c5bc2007-04-02 19:51:21 +00001884
Benny Prijono0bc99a92011-03-17 04:34:43 +00001885 if (call->media[mi].tp == NULL)
1886 continue;
1887
1888 pjmedia_transport_info_init(&tpinfo);
1889 pjmedia_transport_get_info(call->media[mi].tp, &tpinfo);
1890 pj_sockaddr_cp(&origin, &tpinfo.sock_info.rtp_addr_name);
1891 break;
Benny Prijono25b2ea12008-01-24 19:20:54 +00001892 }
Benny Prijonoc97608e2007-03-23 16:34:20 +00001893
Benny Prijono0bc99a92011-03-17 04:34:43 +00001894 /* Create the base (blank) SDP */
1895 status = pjmedia_endpt_create_base_sdp(pjsua_var.med_endpt, pool, NULL,
1896 &origin, &sdp);
1897 if (status != PJ_SUCCESS)
1898 return status;
Benny Prijonoa310bd22008-06-27 21:19:44 +00001899
Benny Prijono0bc99a92011-03-17 04:34:43 +00001900 /* Process each media line */
1901 for (mi=0; mi<call->med_cnt; ++mi) {
1902 pjsua_call_media *call_med = &call->media[mi];
1903 pjmedia_sdp_media *m = NULL;
1904 pjmedia_transport_info tpinfo;
1905
Nanang Izzuddinb6c239c2011-05-10 05:42:28 +00001906 if (rem_sdp && mi >= rem_sdp->media_count) {
1907 /* Remote might have removed some media lines. */
1908 break;
Nanang Izzuddin1e952a82010-10-05 16:32:04 +00001909 }
1910
Nanang Izzuddinb6c239c2011-05-10 05:42:28 +00001911 if (call_med->tp == NULL || call_med->tp_st == PJSUA_MED_TP_DISABLED)
1912 {
Benny Prijono0bc99a92011-03-17 04:34:43 +00001913 /*
Nanang Izzuddinb6c239c2011-05-10 05:42:28 +00001914 * This media is disabled. Just create a valid SDP with zero
Benny Prijono0bc99a92011-03-17 04:34:43 +00001915 * port.
1916 */
Nanang Izzuddind8c33d82011-08-18 18:30:55 +00001917 if (rem_sdp) {
1918 /* Just clone the remote media and deactivate it */
1919 m = pjmedia_sdp_media_clone_deactivate(pool,
1920 rem_sdp->media[mi]);
1921 } else {
1922 m = PJ_POOL_ZALLOC_T(pool, pjmedia_sdp_media);
1923 m->desc.transport = pj_str("RTP/AVP");
1924 m->desc.fmt_count = 1;
1925 m->conn = PJ_POOL_ZALLOC_T(pool, pjmedia_sdp_conn);
1926 m->conn->net_type = pj_str("IN");
1927 m->conn->addr_type = pj_str("IP4");
1928 m->conn->addr = pj_str("127.0.0.1");
Benny Prijonoa310bd22008-06-27 21:19:44 +00001929
Nanang Izzuddind8c33d82011-08-18 18:30:55 +00001930 switch (call_med->type) {
1931 case PJMEDIA_TYPE_AUDIO:
1932 m->desc.media = pj_str("audio");
1933 m->desc.fmt[0] = pj_str("0");
1934 break;
1935 case PJMEDIA_TYPE_VIDEO:
1936 m->desc.media = pj_str("video");
1937 m->desc.fmt[0] = pj_str("31");
1938 break;
1939 default:
1940 if (rem_sdp) {
1941 pj_strdup(pool, &m->desc.media,
1942 &rem_sdp->media[mi]->desc.media);
1943 pj_strdup(pool, &m->desc.fmt[0],
1944 &rem_sdp->media[mi]->desc.fmt[0]);
1945 } else {
1946 pj_assert(!"Invalid call_med media type");
1947 return PJ_EBUG;
1948 }
Benny Prijono0bc99a92011-03-17 04:34:43 +00001949 }
Benny Prijonoa310bd22008-06-27 21:19:44 +00001950 }
Benny Prijono0bc99a92011-03-17 04:34:43 +00001951
1952 sdp->media[sdp->media_count++] = m;
1953 continue;
Benny Prijonoa310bd22008-06-27 21:19:44 +00001954 }
1955
Benny Prijono0bc99a92011-03-17 04:34:43 +00001956 /* Get transport address info */
1957 pjmedia_transport_info_init(&tpinfo);
1958 pjmedia_transport_get_info(call_med->tp, &tpinfo);
Benny Prijonoa310bd22008-06-27 21:19:44 +00001959
Benny Prijono0bc99a92011-03-17 04:34:43 +00001960 /* Ask pjmedia endpoint to create SDP media line */
1961 switch (call_med->type) {
1962 case PJMEDIA_TYPE_AUDIO:
1963 status = pjmedia_endpt_create_audio_sdp(pjsua_var.med_endpt, pool,
1964 &tpinfo.sock_info, 0, &m);
1965 break;
Nanang Izzuddin63b3c132011-07-19 11:11:07 +00001966#if defined(PJMEDIA_HAS_VIDEO) && (PJMEDIA_HAS_VIDEO != 0)
Benny Prijono0bc99a92011-03-17 04:34:43 +00001967 case PJMEDIA_TYPE_VIDEO:
1968 status = pjmedia_endpt_create_video_sdp(pjsua_var.med_endpt, pool,
1969 &tpinfo.sock_info, 0, &m);
1970 break;
Nanang Izzuddin63b3c132011-07-19 11:11:07 +00001971#endif
Benny Prijono0bc99a92011-03-17 04:34:43 +00001972 default:
1973 pj_assert(!"Invalid call_med media type");
1974 return PJ_EBUG;
1975 }
Benny Prijonoa310bd22008-06-27 21:19:44 +00001976
Benny Prijono0bc99a92011-03-17 04:34:43 +00001977 if (status != PJ_SUCCESS)
1978 return status;
1979
1980 sdp->media[sdp->media_count++] = m;
1981
1982 /* Give to transport */
1983 status = pjmedia_transport_encode_sdp(call_med->tp, pool,
1984 sdp, rem_sdp, mi);
1985 if (status != PJ_SUCCESS) {
1986 if (sip_err_code) *sip_err_code = PJSIP_SC_NOT_ACCEPTABLE;
1987 return status;
Benny Prijonoa310bd22008-06-27 21:19:44 +00001988 }
Nanang Izzuddin91ba2a22011-04-11 19:59:09 +00001989
1990 /* Copy c= line of the first media to session level,
1991 * if there's none.
1992 */
1993 if (sdp->conn == NULL) {
1994 sdp->conn = pjmedia_sdp_conn_clone(pool, m->conn);
Benny Prijonoa310bd22008-06-27 21:19:44 +00001995 }
1996 }
1997
Benny Prijono6ba8c542007-10-16 01:34:14 +00001998 /* Add NAT info in the SDP */
1999 if (pjsua_var.ua_cfg.nat_type_in_sdp) {
2000 pjmedia_sdp_attr *a;
2001 pj_str_t value;
2002 char nat_info[80];
2003
2004 value.ptr = nat_info;
2005 if (pjsua_var.ua_cfg.nat_type_in_sdp == 1) {
2006 value.slen = pj_ansi_snprintf(nat_info, sizeof(nat_info),
2007 "%d", pjsua_var.nat_type);
2008 } else {
2009 const char *type_name = pj_stun_get_nat_name(pjsua_var.nat_type);
2010 value.slen = pj_ansi_snprintf(nat_info, sizeof(nat_info),
2011 "%d %s",
2012 pjsua_var.nat_type,
2013 type_name);
2014 }
2015
2016 a = pjmedia_sdp_attr_create(pool, "X-nat", &value);
2017
2018 pjmedia_sdp_attr_add(&sdp->attr_count, sdp->attr, a);
2019
2020 }
2021
Benny Prijonoc97608e2007-03-23 16:34:20 +00002022
Benny Prijono0bc99a92011-03-17 04:34:43 +00002023#if DISABLED_FOR_TICKET_1185 && defined(PJMEDIA_HAS_SRTP) && (PJMEDIA_HAS_SRTP != 0)
Nanang Izzuddind89cc3a2010-05-13 05:22:51 +00002024 /* Check if SRTP is in optional mode and configured to use duplicated
2025 * media, i.e: secured and unsecured version, in the SDP offer.
2026 */
2027 if (!rem_sdp &&
2028 pjsua_var.acc[call->acc_id].cfg.use_srtp == PJMEDIA_SRTP_OPTIONAL &&
2029 pjsua_var.acc[call->acc_id].cfg.srtp_optional_dup_offer)
2030 {
2031 unsigned i;
2032
2033 for (i = 0; i < sdp->media_count; ++i) {
2034 pjmedia_sdp_media *m = sdp->media[i];
2035
Benny Prijono0bc99a92011-03-17 04:34:43 +00002036 /* Check if this media is unsecured but has SDP "crypto"
Nanang Izzuddind89cc3a2010-05-13 05:22:51 +00002037 * attribute.
2038 */
2039 if (pj_stricmp2(&m->desc.transport, "RTP/AVP") == 0 &&
2040 pjmedia_sdp_media_find_attr2(m, "crypto", NULL) != NULL)
2041 {
Benny Prijono0bc99a92011-03-17 04:34:43 +00002042 if (i == (unsigned)call->audio_idx &&
Nanang Izzuddin1e952a82010-10-05 16:32:04 +00002043 sdp_neg_state == PJMEDIA_SDP_NEG_STATE_DONE)
2044 {
2045 /* This is a session update, and peer has chosen the
2046 * unsecured version, so let's make this unsecured too.
2047 */
2048 pjmedia_sdp_media_remove_all_attr(m, "crypto");
2049 } else {
2050 /* This is new offer, duplicate media so we'll have
2051 * secured (with "RTP/SAVP" transport) and and unsecured
2052 * versions.
2053 */
2054 pjmedia_sdp_media *new_m;
Nanang Izzuddind89cc3a2010-05-13 05:22:51 +00002055
Nanang Izzuddin1e952a82010-10-05 16:32:04 +00002056 /* Duplicate this media and apply secured transport */
2057 new_m = pjmedia_sdp_media_clone(pool, m);
2058 pj_strdup2(pool, &new_m->desc.transport, "RTP/SAVP");
Nanang Izzuddind89cc3a2010-05-13 05:22:51 +00002059
Nanang Izzuddin1e952a82010-10-05 16:32:04 +00002060 /* Remove the "crypto" attribute in the unsecured media */
2061 pjmedia_sdp_media_remove_all_attr(m, "crypto");
Nanang Izzuddind89cc3a2010-05-13 05:22:51 +00002062
Nanang Izzuddin1e952a82010-10-05 16:32:04 +00002063 /* Insert the new media before the unsecured media */
2064 if (sdp->media_count < PJMEDIA_MAX_SDP_MEDIA) {
Benny Prijono0bc99a92011-03-17 04:34:43 +00002065 pj_array_insert(sdp->media, sizeof(new_m),
Nanang Izzuddin1e952a82010-10-05 16:32:04 +00002066 sdp->media_count, i, &new_m);
2067 ++sdp->media_count;
2068 ++i;
2069 }
Nanang Izzuddind89cc3a2010-05-13 05:22:51 +00002070 }
2071 }
2072 }
2073 }
2074#endif
2075
Benny Prijonoc97608e2007-03-23 16:34:20 +00002076 *p_sdp = sdp;
2077 return PJ_SUCCESS;
Benny Prijonoc97608e2007-03-23 16:34:20 +00002078}
2079
2080
2081static void stop_media_session(pjsua_call_id call_id)
2082{
2083 pjsua_call *call = &pjsua_var.calls[call_id];
Benny Prijono0bc99a92011-03-17 04:34:43 +00002084 unsigned mi;
Benny Prijonoc97608e2007-03-23 16:34:20 +00002085
Benny Prijonob90fd382011-09-18 14:59:56 +00002086 pj_log_push_indent();
2087
Benny Prijono0bc99a92011-03-17 04:34:43 +00002088 for (mi=0; mi<call->med_cnt; ++mi) {
2089 pjsua_call_media *call_med = &call->media[mi];
2090
2091 if (call_med->type == PJMEDIA_TYPE_AUDIO) {
2092 pjmedia_stream *strm = call_med->strm.a.stream;
2093 pjmedia_rtcp_stat stat;
2094
2095 if (strm) {
2096 if (call_med->strm.a.conf_slot != PJSUA_INVALID_ID) {
2097 if (pjsua_var.mconf) {
2098 pjsua_conf_remove_port(call_med->strm.a.conf_slot);
2099 }
2100 call_med->strm.a.conf_slot = PJSUA_INVALID_ID;
2101 }
2102
2103 if ((call_med->dir & PJMEDIA_DIR_ENCODING) &&
2104 (pjmedia_stream_get_stat(strm, &stat) == PJ_SUCCESS))
2105 {
2106 /* Save RTP timestamp & sequence, so when media session is
2107 * restarted, those values will be restored as the initial
2108 * RTP timestamp & sequence of the new media session. So in
2109 * the same call session, RTP timestamp and sequence are
2110 * guaranteed to be contigue.
2111 */
2112 call_med->rtp_tx_seq_ts_set = 1 | (1 << 1);
2113 call_med->rtp_tx_seq = stat.rtp_tx_last_seq;
2114 call_med->rtp_tx_ts = stat.rtp_tx_last_ts;
2115 }
2116
2117 if (pjsua_var.ua_cfg.cb.on_stream_destroyed) {
2118 pjsua_var.ua_cfg.cb.on_stream_destroyed(call_id, strm, mi);
2119 }
2120
2121 pjmedia_stream_destroy(strm);
2122 call_med->strm.a.stream = NULL;
Nanang Izzuddinbf26db12011-03-18 07:54:50 +00002123 }
Nanang Izzuddinfd461eb2008-06-09 09:35:59 +00002124 }
Nanang Izzuddin50fae732011-03-22 09:49:23 +00002125
2126#if PJMEDIA_HAS_VIDEO
2127 else if (call_med->type == PJMEDIA_TYPE_VIDEO) {
Nanang Izzuddin62053a62011-07-12 11:08:32 +00002128 stop_video_stream(call_med);
Benny Prijonoc97608e2007-03-23 16:34:20 +00002129 }
Nanang Izzuddin50fae732011-03-22 09:49:23 +00002130#endif
Nanang Izzuddinbf26db12011-03-18 07:54:50 +00002131
2132 PJ_LOG(4,(THIS_FILE, "Media session call%02d:%d is destroyed",
2133 call_id, mi));
Sauw Minge7dbbc82011-10-24 09:28:13 +00002134 call_med->prev_state = call_med->state;
Benny Prijono0bc99a92011-03-17 04:34:43 +00002135 call_med->state = PJSUA_CALL_MEDIA_NONE;
Benny Prijonoc97608e2007-03-23 16:34:20 +00002136 }
Benny Prijonob90fd382011-09-18 14:59:56 +00002137
2138 pj_log_pop_indent();
Benny Prijonoc97608e2007-03-23 16:34:20 +00002139}
2140
2141pj_status_t pjsua_media_channel_deinit(pjsua_call_id call_id)
2142{
2143 pjsua_call *call = &pjsua_var.calls[call_id];
Benny Prijono0bc99a92011-03-17 04:34:43 +00002144 unsigned mi;
Benny Prijonoc97608e2007-03-23 16:34:20 +00002145
Sauw Ming903154f2011-10-03 08:22:48 +00002146 PJSUA_LOCK();
Sauw Mingec765352011-10-03 02:04:36 +00002147 for (mi=0; mi<call->med_cnt; ++mi) {
2148 pjsua_call_media *call_med = &call->media[mi];
2149
Sauw Ming903154f2011-10-03 08:22:48 +00002150 if (call_med->tp_st == PJSUA_MED_TP_CREATING) {
2151 /* We will do the deinitialization after media transport
2152 * creation is completed.
2153 */
2154 call->async_call.med_ch_deinit = PJ_TRUE;
2155 PJSUA_UNLOCK();
2156 return PJ_SUCCESS;
2157 }
Sauw Mingec765352011-10-03 02:04:36 +00002158 }
Sauw Ming903154f2011-10-03 08:22:48 +00002159 PJSUA_UNLOCK();
Sauw Mingec765352011-10-03 02:04:36 +00002160
Benny Prijonob90fd382011-09-18 14:59:56 +00002161 PJ_LOG(4,(THIS_FILE, "Call %d: deinitializing media..", call_id));
2162 pj_log_push_indent();
2163
Sauw Minge7dbbc82011-10-24 09:28:13 +00002164 for (mi=0; mi<call->med_cnt; ++mi) {
2165 pjsua_call_media *call_med = &call->media[mi];
2166
2167 if (call_med->type == PJMEDIA_TYPE_AUDIO && call_med->strm.a.stream)
2168 pjmedia_stream_send_rtcp_bye(call_med->strm.a.stream);
2169 }
2170
Benny Prijonoc97608e2007-03-23 16:34:20 +00002171 stop_media_session(call_id);
2172
Benny Prijono0bc99a92011-03-17 04:34:43 +00002173 for (mi=0; mi<call->med_cnt; ++mi) {
2174 pjsua_call_media *call_med = &call->media[mi];
Benny Prijonoc97608e2007-03-23 16:34:20 +00002175
Sauw Minge7dbbc82011-10-24 09:28:13 +00002176 if (call_med->tp_st > PJSUA_MED_TP_IDLE) {
Benny Prijono0bc99a92011-03-17 04:34:43 +00002177 pjmedia_transport_media_stop(call_med->tp);
Sauw Ming73ecfe82011-09-21 10:20:01 +00002178 set_media_tp_state(call_med, PJSUA_MED_TP_IDLE);
Benny Prijono0bc99a92011-03-17 04:34:43 +00002179 }
2180
Nanang Izzuddinb6c239c2011-05-10 05:42:28 +00002181 //if (call_med->tp_orig && call_med->tp &&
2182 // call_med->tp != call_med->tp_orig)
2183 //{
2184 // pjmedia_transport_close(call_med->tp);
2185 // call_med->tp = call_med->tp_orig;
2186 //}
2187 if (call_med->tp) {
Benny Prijono0bc99a92011-03-17 04:34:43 +00002188 pjmedia_transport_close(call_med->tp);
Nanang Izzuddinb6c239c2011-05-10 05:42:28 +00002189 call_med->tp = call_med->tp_orig = NULL;
Benny Prijono0bc99a92011-03-17 04:34:43 +00002190 }
Sauw Minge7dbbc82011-10-24 09:28:13 +00002191 call_med->tp_orig = NULL;
Benny Prijonod8179652008-01-23 20:39:07 +00002192 }
Nanang Izzuddin670f71b2009-01-20 14:05:54 +00002193
2194 check_snd_dev_idle();
Benny Prijonob90fd382011-09-18 14:59:56 +00002195 pj_log_pop_indent();
Nanang Izzuddin670f71b2009-01-20 14:05:54 +00002196
Benny Prijonoc97608e2007-03-23 16:34:20 +00002197 return PJ_SUCCESS;
2198}
2199
2200
2201/*
2202 * DTMF callback from the stream.
2203 */
2204static void dtmf_callback(pjmedia_stream *strm, void *user_data,
2205 int digit)
2206{
2207 PJ_UNUSED_ARG(strm);
2208
Benny Prijonob90fd382011-09-18 14:59:56 +00002209 pj_log_push_indent();
2210
Benny Prijono0c068262008-02-14 14:38:52 +00002211 /* For discussions about call mutex protection related to this
2212 * callback, please see ticket #460:
2213 * http://trac.pjsip.org/repos/ticket/460#comment:4
2214 */
Benny Prijonoc97608e2007-03-23 16:34:20 +00002215 if (pjsua_var.ua_cfg.cb.on_dtmf_digit) {
2216 pjsua_call_id call_id;
2217
Benny Prijonod8179652008-01-23 20:39:07 +00002218 call_id = (pjsua_call_id)(long)user_data;
Benny Prijonoc97608e2007-03-23 16:34:20 +00002219 pjsua_var.ua_cfg.cb.on_dtmf_digit(call_id, digit);
2220 }
Benny Prijonob90fd382011-09-18 14:59:56 +00002221
2222 pj_log_pop_indent();
Benny Prijonoc97608e2007-03-23 16:34:20 +00002223}
2224
2225
Benny Prijono0bc99a92011-03-17 04:34:43 +00002226static pj_status_t audio_channel_update(pjsua_call_media *call_med,
2227 pj_pool_t *tmp_pool,
2228 const pjmedia_sdp_session *local_sdp,
2229 const pjmedia_sdp_session *remote_sdp)
Benny Prijonoc97608e2007-03-23 16:34:20 +00002230{
Benny Prijono0bc99a92011-03-17 04:34:43 +00002231 pjsua_call *call = call_med->call;
2232 pjmedia_stream_info the_si, *si = &the_si;
Benny Prijonoc97608e2007-03-23 16:34:20 +00002233 pjmedia_port *media_port;
Benny Prijono0bc99a92011-03-17 04:34:43 +00002234 unsigned strm_idx = call_med->idx;
Benny Prijonoc97608e2007-03-23 16:34:20 +00002235 pj_status_t status;
Benny Prijonob90fd382011-09-18 14:59:56 +00002236
2237 PJ_LOG(4,(THIS_FILE,"Audio channel update.."));
2238 pj_log_push_indent();
Benny Prijono0bc99a92011-03-17 04:34:43 +00002239
2240 status = pjmedia_stream_info_from_sdp(si, tmp_pool, pjsua_var.med_endpt,
2241 local_sdp, remote_sdp, strm_idx);
Benny Prijonoc97608e2007-03-23 16:34:20 +00002242 if (status != PJ_SUCCESS)
Benny Prijonob90fd382011-09-18 14:59:56 +00002243 goto on_return;
Benny Prijonoc97608e2007-03-23 16:34:20 +00002244
Sauw Minge7dbbc82011-10-24 09:28:13 +00002245 si->rtcp_sdes_bye_disabled = PJ_TRUE;
2246
Nanang Izzuddin99d69522008-08-04 15:01:38 +00002247 /* Check if no media is active */
Benny Prijono0bc99a92011-03-17 04:34:43 +00002248 if (si->dir == PJMEDIA_DIR_NONE) {
Nanang Izzuddin99d69522008-08-04 15:01:38 +00002249 /* Call media state */
Benny Prijono0bc99a92011-03-17 04:34:43 +00002250 call_med->state = PJSUA_CALL_MEDIA_NONE;
Benny Prijonoc97608e2007-03-23 16:34:20 +00002251
Nanang Izzuddin99d69522008-08-04 15:01:38 +00002252 /* Call media direction */
Benny Prijono0bc99a92011-03-17 04:34:43 +00002253 call_med->dir = PJMEDIA_DIR_NONE;
Benny Prijono68f9e4f2008-03-21 08:56:02 +00002254
Benny Prijonoc97608e2007-03-23 16:34:20 +00002255 } else {
Nanang Izzuddin4375f902008-06-26 19:12:09 +00002256 pjmedia_transport_info tp_info;
2257
Benny Prijono224b4e22008-06-19 14:10:28 +00002258 /* Start/restart media transport */
Benny Prijono0bc99a92011-03-17 04:34:43 +00002259 status = pjmedia_transport_media_start(call_med->tp,
2260 tmp_pool, local_sdp,
2261 remote_sdp, strm_idx);
Benny Prijonod8179652008-01-23 20:39:07 +00002262 if (status != PJ_SUCCESS)
Benny Prijonob90fd382011-09-18 14:59:56 +00002263 goto on_return;
Benny Prijonoc97608e2007-03-23 16:34:20 +00002264
Sauw Ming73ecfe82011-09-21 10:20:01 +00002265 set_media_tp_state(call_med, PJSUA_MED_TP_RUNNING);
Benny Prijono224b4e22008-06-19 14:10:28 +00002266
Nanang Izzuddin4375f902008-06-26 19:12:09 +00002267 /* Get remote SRTP usage policy */
2268 pjmedia_transport_info_init(&tp_info);
Benny Prijono0bc99a92011-03-17 04:34:43 +00002269 pjmedia_transport_get_info(call_med->tp, &tp_info);
Nanang Izzuddin4375f902008-06-26 19:12:09 +00002270 if (tp_info.specific_info_cnt > 0) {
Benny Prijonof5d9f1f2009-10-14 13:13:18 +00002271 unsigned i;
Nanang Izzuddin4375f902008-06-26 19:12:09 +00002272 for (i = 0; i < tp_info.specific_info_cnt; ++i) {
2273 if (tp_info.spc_info[i].type == PJMEDIA_TRANSPORT_TYPE_SRTP)
2274 {
2275 pjmedia_srtp_info *srtp_info =
2276 (pjmedia_srtp_info*) tp_info.spc_info[i].buffer;
2277
Benny Prijono0bc99a92011-03-17 04:34:43 +00002278 call_med->rem_srtp_use = srtp_info->peer_use;
Nanang Izzuddin4375f902008-06-26 19:12:09 +00002279 break;
2280 }
2281 }
2282 }
2283
Benny Prijonoc97608e2007-03-23 16:34:20 +00002284 /* Override ptime, if this option is specified. */
2285 if (pjsua_var.media_cfg.ptime != 0) {
Benny Prijono91e567e2007-12-28 08:51:58 +00002286 si->param->setting.frm_per_pkt = (pj_uint8_t)
2287 (pjsua_var.media_cfg.ptime / si->param->info.frm_ptime);
2288 if (si->param->setting.frm_per_pkt == 0)
2289 si->param->setting.frm_per_pkt = 1;
Benny Prijonoc97608e2007-03-23 16:34:20 +00002290 }
2291
2292 /* Disable VAD, if this option is specified. */
2293 if (pjsua_var.media_cfg.no_vad) {
Benny Prijono91e567e2007-12-28 08:51:58 +00002294 si->param->setting.vad = 0;
Benny Prijonoc97608e2007-03-23 16:34:20 +00002295 }
2296
2297
2298 /* Optionally, application may modify other stream settings here
2299 * (such as jitter buffer parameters, codec ptime, etc.)
2300 */
Benny Prijono91e567e2007-12-28 08:51:58 +00002301 si->jb_init = pjsua_var.media_cfg.jb_init;
2302 si->jb_min_pre = pjsua_var.media_cfg.jb_min_pre;
2303 si->jb_max_pre = pjsua_var.media_cfg.jb_max_pre;
2304 si->jb_max = pjsua_var.media_cfg.jb_max;
Benny Prijonoc97608e2007-03-23 16:34:20 +00002305
Benny Prijono8147f402007-11-21 14:50:07 +00002306 /* Set SSRC */
Benny Prijono0bc99a92011-03-17 04:34:43 +00002307 si->ssrc = call_med->ssrc;
Benny Prijono8147f402007-11-21 14:50:07 +00002308
Nanang Izzuddina815ceb2008-08-26 16:51:28 +00002309 /* Set RTP timestamp & sequence, normally these value are intialized
2310 * automatically when stream session created, but for some cases (e.g:
2311 * call reinvite, call update) timestamp and sequence need to be kept
2312 * contigue.
2313 */
Benny Prijono0bc99a92011-03-17 04:34:43 +00002314 si->rtp_ts = call_med->rtp_tx_ts;
2315 si->rtp_seq = call_med->rtp_tx_seq;
2316 si->rtp_seq_ts_set = call_med->rtp_tx_seq_ts_set;
Nanang Izzuddina815ceb2008-08-26 16:51:28 +00002317
Nanang Izzuddin5e39a2b2010-09-20 06:13:02 +00002318#if defined(PJMEDIA_STREAM_ENABLE_KA) && PJMEDIA_STREAM_ENABLE_KA!=0
2319 /* Enable/disable stream keep-alive and NAT hole punch. */
2320 si->use_ka = pjsua_var.acc[call->acc_id].cfg.use_stream_ka;
2321#endif
2322
Benny Prijonoc97608e2007-03-23 16:34:20 +00002323 /* Create session based on session info. */
Benny Prijono0bc99a92011-03-17 04:34:43 +00002324 status = pjmedia_stream_create(pjsua_var.med_endpt, NULL, si,
2325 call_med->tp, NULL,
2326 &call_med->strm.a.stream);
2327 if (status != PJ_SUCCESS) {
Benny Prijonob90fd382011-09-18 14:59:56 +00002328 goto on_return;
Benny Prijono0bc99a92011-03-17 04:34:43 +00002329 }
2330
2331 /* Start stream */
2332 status = pjmedia_stream_start(call_med->strm.a.stream);
Benny Prijonoc97608e2007-03-23 16:34:20 +00002333 if (status != PJ_SUCCESS) {
Benny Prijonob90fd382011-09-18 14:59:56 +00002334 goto on_return;
Benny Prijonoc97608e2007-03-23 16:34:20 +00002335 }
2336
Sauw Minge7dbbc82011-10-24 09:28:13 +00002337 if (call_med->prev_state == PJSUA_CALL_MEDIA_NONE)
2338 pjmedia_stream_send_rtcp_sdes(call_med->strm.a.stream);
2339
Benny Prijonoc97608e2007-03-23 16:34:20 +00002340 /* If DTMF callback is installed by application, install our
2341 * callback to the session.
2342 */
2343 if (pjsua_var.ua_cfg.cb.on_dtmf_digit) {
Benny Prijono0bc99a92011-03-17 04:34:43 +00002344 pjmedia_stream_set_dtmf_callback(call_med->strm.a.stream,
2345 &dtmf_callback,
2346 (void*)(long)(call->index));
Benny Prijonoc97608e2007-03-23 16:34:20 +00002347 }
2348
2349 /* Get the port interface of the first stream in the session.
2350 * We need the port interface to add to the conference bridge.
2351 */
Benny Prijono0bc99a92011-03-17 04:34:43 +00002352 pjmedia_stream_get_port(call_med->strm.a.stream, &media_port);
Benny Prijonoc97608e2007-03-23 16:34:20 +00002353
Benny Prijonofc13bf62008-02-20 08:56:15 +00002354 /* Notify application about stream creation.
2355 * Note: application may modify media_port to point to different
2356 * media port
2357 */
2358 if (pjsua_var.ua_cfg.cb.on_stream_created) {
Benny Prijono0bc99a92011-03-17 04:34:43 +00002359 pjsua_var.ua_cfg.cb.on_stream_created(call->index,
2360 call_med->strm.a.stream,
2361 strm_idx, &media_port);
Benny Prijonofc13bf62008-02-20 08:56:15 +00002362 }
Benny Prijonoc97608e2007-03-23 16:34:20 +00002363
2364 /*
2365 * Add the call to conference bridge.
2366 */
Benny Prijono38fb3ea2008-01-02 08:27:03 +00002367 {
2368 char tmp[PJSIP_MAX_URL_SIZE];
2369 pj_str_t port_name;
2370
2371 port_name.ptr = tmp;
2372 port_name.slen = pjsip_uri_print(PJSIP_URI_IN_REQ_URI,
2373 call->inv->dlg->remote.info->uri,
2374 tmp, sizeof(tmp));
2375 if (port_name.slen < 1) {
2376 port_name = pj_str("call");
2377 }
Benny Prijono40d62b62009-08-12 17:53:47 +00002378 status = pjmedia_conf_add_port( pjsua_var.mconf,
2379 call->inv->pool_prov,
Benny Prijono38fb3ea2008-01-02 08:27:03 +00002380 media_port,
2381 &port_name,
Benny Prijono0bc99a92011-03-17 04:34:43 +00002382 (unsigned*)
2383 &call_med->strm.a.conf_slot);
Benny Prijono38fb3ea2008-01-02 08:27:03 +00002384 if (status != PJ_SUCCESS) {
Benny Prijonob90fd382011-09-18 14:59:56 +00002385 goto on_return;
Benny Prijono38fb3ea2008-01-02 08:27:03 +00002386 }
Benny Prijonoc97608e2007-03-23 16:34:20 +00002387 }
2388
Nanang Izzuddin99d69522008-08-04 15:01:38 +00002389 /* Call media direction */
Benny Prijono0bc99a92011-03-17 04:34:43 +00002390 call_med->dir = si->dir;
Nanang Izzuddin99d69522008-08-04 15:01:38 +00002391
2392 /* Call media state */
2393 if (call->local_hold)
Benny Prijono0bc99a92011-03-17 04:34:43 +00002394 call_med->state = PJSUA_CALL_MEDIA_LOCAL_HOLD;
2395 else if (call_med->dir == PJMEDIA_DIR_DECODING)
2396 call_med->state = PJSUA_CALL_MEDIA_REMOTE_HOLD;
Nanang Izzuddin99d69522008-08-04 15:01:38 +00002397 else
Benny Prijono0bc99a92011-03-17 04:34:43 +00002398 call_med->state = PJSUA_CALL_MEDIA_ACTIVE;
Benny Prijonoc97608e2007-03-23 16:34:20 +00002399 }
2400
2401 /* Print info. */
2402 {
2403 char info[80];
2404 int info_len = 0;
Benny Prijono0bc99a92011-03-17 04:34:43 +00002405 int len;
2406 const char *dir;
Benny Prijonoc97608e2007-03-23 16:34:20 +00002407
Benny Prijono0bc99a92011-03-17 04:34:43 +00002408 switch (si->dir) {
2409 case PJMEDIA_DIR_NONE:
2410 dir = "inactive";
2411 break;
2412 case PJMEDIA_DIR_ENCODING:
2413 dir = "sendonly";
2414 break;
2415 case PJMEDIA_DIR_DECODING:
2416 dir = "recvonly";
2417 break;
2418 case PJMEDIA_DIR_ENCODING_DECODING:
2419 dir = "sendrecv";
2420 break;
2421 default:
2422 dir = "unknown";
2423 break;
Benny Prijonoc97608e2007-03-23 16:34:20 +00002424 }
Benny Prijono0bc99a92011-03-17 04:34:43 +00002425 len = pj_ansi_sprintf( info+info_len,
2426 ", stream #%d: %.*s (%s)", strm_idx,
2427 (int)si->fmt.encoding_name.slen,
2428 si->fmt.encoding_name.ptr,
2429 dir);
2430 if (len > 0)
2431 info_len += len;
Benny Prijonob90fd382011-09-18 14:59:56 +00002432 PJ_LOG(4,(THIS_FILE,"Audio updated%s", info));
Benny Prijonoc97608e2007-03-23 16:34:20 +00002433 }
2434
Benny Prijonob90fd382011-09-18 14:59:56 +00002435on_return:
2436 pj_log_pop_indent();
2437 return status;
Benny Prijonoc97608e2007-03-23 16:34:20 +00002438}
2439
Benny Prijono0bc99a92011-03-17 04:34:43 +00002440pj_status_t pjsua_media_channel_update(pjsua_call_id call_id,
2441 const pjmedia_sdp_session *local_sdp,
2442 const pjmedia_sdp_session *remote_sdp)
2443{
2444 pjsua_call *call = &pjsua_var.calls[call_id];
Nanang Izzuddind8c33d82011-08-18 18:30:55 +00002445 pjsua_acc *acc = &pjsua_var.acc[call->acc_id];
Benny Prijono0bc99a92011-03-17 04:34:43 +00002446 pj_pool_t *tmp_pool = call->inv->pool_prov;
2447 unsigned mi;
Nanang Izzuddinb6c239c2011-05-10 05:42:28 +00002448 pj_bool_t got_media = PJ_FALSE;
Nanang Izzuddinbf26db12011-03-18 07:54:50 +00002449 pj_status_t status = PJ_SUCCESS;
Benny Prijono0bc99a92011-03-17 04:34:43 +00002450
Nanang Izzuddind8c33d82011-08-18 18:30:55 +00002451 const pj_str_t STR_AUDIO = { "audio", 5 };
2452 const pj_str_t STR_VIDEO = { "video", 5 };
2453 pj_uint8_t maudidx[PJSUA_MAX_CALL_MEDIA];
2454 unsigned maudcnt = PJ_ARRAY_SIZE(maudidx);
2455 pj_uint8_t mvididx[PJSUA_MAX_CALL_MEDIA];
2456 unsigned mvidcnt = PJ_ARRAY_SIZE(mvididx);
2457 pj_bool_t need_renego_sdp = PJ_FALSE;
2458
Benny Prijono0bc99a92011-03-17 04:34:43 +00002459 if (pjsua_get_state() != PJSUA_STATE_RUNNING)
2460 return PJ_EBUSY;
2461
Benny Prijonob90fd382011-09-18 14:59:56 +00002462 PJ_LOG(4,(THIS_FILE, "Call %d: updating media..", call_id));
2463 pj_log_push_indent();
2464
Benny Prijono0bc99a92011-03-17 04:34:43 +00002465 /* Destroy existing media session, if any. */
2466 stop_media_session(call->index);
2467
Nanang Izzuddind8c33d82011-08-18 18:30:55 +00002468 /* Call media count must be at least equal to SDP media. Note that
2469 * it may not be equal when remote removed any SDP media line.
2470 */
2471 pj_assert(call->med_cnt >= local_sdp->media_count);
2472
Benny Prijono0bc99a92011-03-17 04:34:43 +00002473 /* Reset audio_idx first */
2474 call->audio_idx = -1;
2475
Nanang Izzuddind8c33d82011-08-18 18:30:55 +00002476 /* Apply maximum audio/video count of the account */
2477 sort_media(local_sdp, &STR_AUDIO, acc->cfg.use_srtp,
2478 maudidx, &maudcnt);
2479#if PJMEDIA_HAS_VIDEO
2480 sort_media(local_sdp, &STR_VIDEO, acc->cfg.use_srtp,
2481 mvididx, &mvidcnt);
2482#else
2483 PJ_UNUSED_ARG(STR_VIDEO);
2484 mvidcnt = 0;
2485#endif
2486 if (maudcnt > acc->cfg.max_audio_cnt || mvidcnt > acc->cfg.max_video_cnt)
2487 {
2488 pjmedia_sdp_session *local_sdp2;
2489
2490 maudcnt = PJ_MIN(maudcnt, acc->cfg.max_audio_cnt);
2491 mvidcnt = PJ_MIN(mvidcnt, acc->cfg.max_video_cnt);
2492 local_sdp2 = pjmedia_sdp_session_clone(tmp_pool, local_sdp);
2493
2494 for (mi=0; mi < local_sdp2->media_count; ++mi) {
2495 pjmedia_sdp_media *m = local_sdp2->media[mi];
2496
2497 if (m->desc.port == 0 ||
2498 pj_memchr(maudidx, mi, maudcnt*sizeof(maudidx[0])) ||
2499 pj_memchr(mvididx, mi, mvidcnt*sizeof(mvididx[0])))
2500 {
2501 continue;
2502 }
2503
2504 /* Deactivate this media */
2505 pjmedia_sdp_media_deactivate(tmp_pool, m);
2506 }
2507
2508 local_sdp = local_sdp2;
2509 need_renego_sdp = PJ_TRUE;
2510 }
2511
Benny Prijono0bc99a92011-03-17 04:34:43 +00002512 /* Process each media stream */
2513 for (mi=0; mi < call->med_cnt; ++mi) {
2514 pjsua_call_media *call_med = &call->media[mi];
2515
Nanang Izzuddinb6c239c2011-05-10 05:42:28 +00002516 if (mi >= local_sdp->media_count ||
2517 mi >= remote_sdp->media_count)
Benny Prijono0bc99a92011-03-17 04:34:43 +00002518 {
Nanang Izzuddinb6c239c2011-05-10 05:42:28 +00002519 /* This may happen when remote removed any SDP media lines in
2520 * its re-offer.
2521 */
2522 continue;
2523#if 0
Benny Prijono0bc99a92011-03-17 04:34:43 +00002524 /* Something is wrong */
2525 PJ_LOG(1,(THIS_FILE, "Error updating media for call %d: "
2526 "invalid media index %d in SDP", call_id, mi));
Benny Prijonob90fd382011-09-18 14:59:56 +00002527 status = PJMEDIA_SDP_EINSDP;
2528 goto on_error;
Nanang Izzuddinb6c239c2011-05-10 05:42:28 +00002529#endif
Benny Prijono0bc99a92011-03-17 04:34:43 +00002530 }
2531
2532 switch (call_med->type) {
2533 case PJMEDIA_TYPE_AUDIO:
2534 status = audio_channel_update(call_med, tmp_pool,
2535 local_sdp, remote_sdp);
2536 if (call->audio_idx==-1 && status==PJ_SUCCESS &&
Nanang Izzuddinbf26db12011-03-18 07:54:50 +00002537 call_med->strm.a.stream)
Benny Prijono0bc99a92011-03-17 04:34:43 +00002538 {
2539 call->audio_idx = mi;
2540 }
2541 break;
Nanang Izzuddin63b3c132011-07-19 11:11:07 +00002542#if defined(PJMEDIA_HAS_VIDEO) && (PJMEDIA_HAS_VIDEO != 0)
Benny Prijono0bc99a92011-03-17 04:34:43 +00002543 case PJMEDIA_TYPE_VIDEO:
Nanang Izzuddinbf26db12011-03-18 07:54:50 +00002544 status = video_channel_update(call_med, tmp_pool,
2545 local_sdp, remote_sdp);
Benny Prijono0bc99a92011-03-17 04:34:43 +00002546 break;
Nanang Izzuddin50fae732011-03-22 09:49:23 +00002547#endif
Benny Prijono0bc99a92011-03-17 04:34:43 +00002548 default:
Nanang Izzuddinb6c239c2011-05-10 05:42:28 +00002549 status = PJMEDIA_EINVALIMEDIATYPE;
Benny Prijono0bc99a92011-03-17 04:34:43 +00002550 break;
2551 }
2552
Nanang Izzuddind8c33d82011-08-18 18:30:55 +00002553 /* Close the transport of deactivated media, need this here as media
2554 * can be deactivated by the SDP negotiation and the max media count
2555 * (account) setting.
2556 */
2557 if (local_sdp->media[mi]->desc.port==0 && call_med->tp) {
2558 pjmedia_transport_close(call_med->tp);
2559 call_med->tp = call_med->tp_orig = NULL;
Sauw Ming73ecfe82011-09-21 10:20:01 +00002560 set_media_tp_state(call_med, PJSUA_MED_TP_IDLE);
Nanang Izzuddind8c33d82011-08-18 18:30:55 +00002561 }
2562
Benny Prijono0bc99a92011-03-17 04:34:43 +00002563 if (status != PJ_SUCCESS) {
Nanang Izzuddinb6c239c2011-05-10 05:42:28 +00002564 PJ_PERROR(1,(THIS_FILE, status, "Error updating media call%02d:%d",
Benny Prijono0bc99a92011-03-17 04:34:43 +00002565 call_id, mi));
Nanang Izzuddinb6c239c2011-05-10 05:42:28 +00002566 } else {
2567 got_media = PJ_TRUE;
Benny Prijono0bc99a92011-03-17 04:34:43 +00002568 }
2569 }
2570
Nanang Izzuddind8c33d82011-08-18 18:30:55 +00002571 /* Perform SDP re-negotiation if needed. */
2572 if (got_media && need_renego_sdp) {
2573 pjmedia_sdp_neg *neg = call->inv->neg;
2574
2575 /* This should only happen when we are the answerer. */
2576 PJ_ASSERT_RETURN(neg && !pjmedia_sdp_neg_was_answer_remote(neg),
2577 PJMEDIA_SDPNEG_EINSTATE);
2578
2579 status = pjmedia_sdp_neg_set_remote_offer(tmp_pool, neg, remote_sdp);
2580 if (status != PJ_SUCCESS)
Benny Prijonob90fd382011-09-18 14:59:56 +00002581 goto on_error;
Nanang Izzuddind8c33d82011-08-18 18:30:55 +00002582
2583 status = pjmedia_sdp_neg_set_local_answer(tmp_pool, neg, local_sdp);
2584 if (status != PJ_SUCCESS)
Benny Prijonob90fd382011-09-18 14:59:56 +00002585 goto on_error;
Nanang Izzuddind8c33d82011-08-18 18:30:55 +00002586
2587 status = pjmedia_sdp_neg_negotiate(tmp_pool, neg, 0);
2588 if (status != PJ_SUCCESS)
Benny Prijonob90fd382011-09-18 14:59:56 +00002589 goto on_error;
Nanang Izzuddind8c33d82011-08-18 18:30:55 +00002590 }
2591
Benny Prijonob90fd382011-09-18 14:59:56 +00002592 pj_log_pop_indent();
Nanang Izzuddinb6c239c2011-05-10 05:42:28 +00002593 return (got_media? PJ_SUCCESS : PJMEDIA_SDPNEG_ENOMEDIA);
Benny Prijonob90fd382011-09-18 14:59:56 +00002594
2595on_error:
2596 pj_log_pop_indent();
2597 return status;
Benny Prijono0bc99a92011-03-17 04:34:43 +00002598}
2599
Benny Prijonoeebe9af2006-06-13 22:57:13 +00002600/*
2601 * Get maxinum number of conference ports.
2602 */
2603PJ_DEF(unsigned) pjsua_conf_get_max_ports(void)
2604{
2605 return pjsua_var.media_cfg.max_media_ports;
2606}
2607
2608
2609/*
2610 * Get current number of active ports in the bridge.
2611 */
2612PJ_DEF(unsigned) pjsua_conf_get_active_ports(void)
2613{
Benny Prijono38fb3ea2008-01-02 08:27:03 +00002614 unsigned ports[PJSUA_MAX_CONF_PORTS];
Benny Prijonoeebe9af2006-06-13 22:57:13 +00002615 unsigned count = PJ_ARRAY_SIZE(ports);
2616 pj_status_t status;
2617
2618 status = pjmedia_conf_enum_ports(pjsua_var.mconf, ports, &count);
2619 if (status != PJ_SUCCESS)
2620 count = 0;
2621
2622 return count;
2623}
2624
2625
2626/*
2627 * Enumerate all conference ports.
2628 */
2629PJ_DEF(pj_status_t) pjsua_enum_conf_ports(pjsua_conf_port_id id[],
2630 unsigned *count)
2631{
2632 return pjmedia_conf_enum_ports(pjsua_var.mconf, (unsigned*)id, count);
2633}
2634
2635
2636/*
2637 * Get information about the specified conference port
2638 */
2639PJ_DEF(pj_status_t) pjsua_conf_get_port_info( pjsua_conf_port_id id,
2640 pjsua_conf_port_info *info)
2641{
2642 pjmedia_conf_port_info cinfo;
Benny Prijono0498d902006-06-19 14:49:14 +00002643 unsigned i;
Benny Prijonoeebe9af2006-06-13 22:57:13 +00002644 pj_status_t status;
2645
2646 status = pjmedia_conf_get_port_info( pjsua_var.mconf, id, &cinfo);
2647 if (status != PJ_SUCCESS)
2648 return status;
2649
Benny Prijonoac623b32006-07-03 15:19:31 +00002650 pj_bzero(info, sizeof(*info));
Benny Prijonoeebe9af2006-06-13 22:57:13 +00002651 info->slot_id = id;
2652 info->name = cinfo.name;
2653 info->clock_rate = cinfo.clock_rate;
2654 info->channel_count = cinfo.channel_count;
2655 info->samples_per_frame = cinfo.samples_per_frame;
2656 info->bits_per_sample = cinfo.bits_per_sample;
2657
2658 /* Build array of listeners */
Benny Prijonoc78c3a32006-06-16 15:54:43 +00002659 info->listener_cnt = cinfo.listener_cnt;
2660 for (i=0; i<cinfo.listener_cnt; ++i) {
2661 info->listeners[i] = cinfo.listener_slots[i];
Benny Prijonoeebe9af2006-06-13 22:57:13 +00002662 }
2663
2664 return PJ_SUCCESS;
2665}
2666
2667
2668/*
Benny Prijonoe909eac2006-07-27 22:04:56 +00002669 * Add arbitrary media port to PJSUA's conference bridge.
2670 */
2671PJ_DEF(pj_status_t) pjsua_conf_add_port( pj_pool_t *pool,
2672 pjmedia_port *port,
2673 pjsua_conf_port_id *p_id)
2674{
2675 pj_status_t status;
2676
2677 status = pjmedia_conf_add_port(pjsua_var.mconf, pool,
2678 port, NULL, (unsigned*)p_id);
2679 if (status != PJ_SUCCESS) {
2680 if (p_id)
2681 *p_id = PJSUA_INVALID_ID;
2682 }
2683
2684 return status;
2685}
2686
2687
2688/*
2689 * Remove arbitrary slot from the conference bridge.
2690 */
2691PJ_DEF(pj_status_t) pjsua_conf_remove_port(pjsua_conf_port_id id)
2692{
Nanang Izzuddin148fd392008-06-16 09:52:50 +00002693 pj_status_t status;
2694
2695 status = pjmedia_conf_remove_port(pjsua_var.mconf, (unsigned)id);
2696 check_snd_dev_idle();
2697
2698 return status;
Benny Prijonoe909eac2006-07-27 22:04:56 +00002699}
2700
2701
2702/*
Benny Prijonoeebe9af2006-06-13 22:57:13 +00002703 * Establish unidirectional media flow from souce to sink.
2704 */
2705PJ_DEF(pj_status_t) pjsua_conf_connect( pjsua_conf_port_id source,
2706 pjsua_conf_port_id sink)
2707{
Benny Prijonob90fd382011-09-18 14:59:56 +00002708 pj_status_t status = PJ_SUCCESS;
2709
2710 PJ_LOG(4,(THIS_FILE, "%s connect: %d --> %d",
2711 (pjsua_var.is_mswitch ? "Switch" : "Conf"),
2712 source, sink));
2713 pj_log_push_indent();
2714
Nanang Izzuddin68559c32008-06-13 17:01:46 +00002715 /* If sound device idle timer is active, cancel it first. */
Benny Prijono0f711b42009-05-06 19:08:43 +00002716 PJSUA_LOCK();
Nanang Izzuddin68559c32008-06-13 17:01:46 +00002717 if (pjsua_var.snd_idle_timer.id) {
2718 pjsip_endpt_cancel_timer(pjsua_var.endpt, &pjsua_var.snd_idle_timer);
2719 pjsua_var.snd_idle_timer.id = PJ_FALSE;
2720 }
Benny Prijono0f711b42009-05-06 19:08:43 +00002721 PJSUA_UNLOCK();
Nanang Izzuddin68559c32008-06-13 17:01:46 +00002722
Nanang Izzuddinfe02a062009-02-18 14:28:49 +00002723
Benny Prijonof798e502009-03-09 13:08:16 +00002724 /* For audio switchboard (i.e. APS-Direct):
2725 * Check if sound device need to be reopened, i.e: its attributes
Nanang Izzuddinfe02a062009-02-18 14:28:49 +00002726 * (format, clock rate, channel count) must match to peer's.
2727 * Note that sound device can be reopened only if it doesn't have
2728 * any connection.
2729 */
Benny Prijonof798e502009-03-09 13:08:16 +00002730 if (pjsua_var.is_mswitch) {
Nanang Izzuddinfe02a062009-02-18 14:28:49 +00002731 pjmedia_conf_port_info port0_info;
2732 pjmedia_conf_port_info peer_info;
2733 unsigned peer_id;
2734 pj_bool_t need_reopen = PJ_FALSE;
Nanang Izzuddinfe02a062009-02-18 14:28:49 +00002735
2736 peer_id = (source!=0)? source : sink;
2737 status = pjmedia_conf_get_port_info(pjsua_var.mconf, peer_id,
2738 &peer_info);
2739 pj_assert(status == PJ_SUCCESS);
2740
2741 status = pjmedia_conf_get_port_info(pjsua_var.mconf, 0, &port0_info);
2742 pj_assert(status == PJ_SUCCESS);
2743
2744 /* Check if sound device is instantiated. */
2745 need_reopen = (pjsua_var.snd_port==NULL && pjsua_var.null_snd==NULL &&
2746 !pjsua_var.no_snd);
2747
2748 /* Check if sound device need to reopen because it needs to modify
2749 * settings to match its peer. Sound device must be idle in this case
2750 * though.
2751 */
2752 if (!need_reopen &&
2753 port0_info.listener_cnt==0 && port0_info.transmitter_cnt==0)
2754 {
2755 need_reopen = (peer_info.format.id != port0_info.format.id ||
Benny Prijonoc45d9512010-12-10 11:04:30 +00002756 peer_info.format.det.aud.avg_bps !=
2757 port0_info.format.det.aud.avg_bps ||
Nanang Izzuddinfe02a062009-02-18 14:28:49 +00002758 peer_info.clock_rate != port0_info.clock_rate ||
Benny Prijonoc45d9512010-12-10 11:04:30 +00002759 peer_info.channel_count!=port0_info.channel_count);
Nanang Izzuddinfe02a062009-02-18 14:28:49 +00002760 }
2761
2762 if (need_reopen) {
Benny Prijonod65f78c2009-06-03 18:59:37 +00002763 if (pjsua_var.cap_dev != NULL_SND_DEV_ID) {
Sauw Ming98766c72011-03-11 06:57:24 +00002764 pjmedia_snd_port_param param;
Nanang Izzuddinfe02a062009-02-18 14:28:49 +00002765
Benny Prijonod65f78c2009-06-03 18:59:37 +00002766 /* Create parameter based on peer info */
Sauw Ming98766c72011-03-11 06:57:24 +00002767 status = create_aud_param(&param.base, pjsua_var.cap_dev,
Benny Prijonod65f78c2009-06-03 18:59:37 +00002768 pjsua_var.play_dev,
2769 peer_info.clock_rate,
2770 peer_info.channel_count,
2771 peer_info.samples_per_frame,
2772 peer_info.bits_per_sample);
2773 if (status != PJ_SUCCESS) {
Benny Prijonoc45d9512010-12-10 11:04:30 +00002774 pjsua_perror(THIS_FILE, "Error opening sound device",
2775 status);
Benny Prijonob90fd382011-09-18 14:59:56 +00002776 goto on_return;
Benny Prijonod65f78c2009-06-03 18:59:37 +00002777 }
Benny Prijonof798e502009-03-09 13:08:16 +00002778
Benny Prijonod65f78c2009-06-03 18:59:37 +00002779 /* And peer format */
2780 if (peer_info.format.id != PJMEDIA_FORMAT_PCM) {
Sauw Ming98766c72011-03-11 06:57:24 +00002781 param.base.flags |= PJMEDIA_AUD_DEV_CAP_EXT_FORMAT;
2782 param.base.ext_fmt = peer_info.format;
Benny Prijonod65f78c2009-06-03 18:59:37 +00002783 }
Benny Prijono10454dc2009-02-21 14:21:59 +00002784
Sauw Ming98766c72011-03-11 06:57:24 +00002785 param.options = 0;
Benny Prijonod65f78c2009-06-03 18:59:37 +00002786 status = open_snd_dev(&param);
2787 if (status != PJ_SUCCESS) {
Benny Prijonoc45d9512010-12-10 11:04:30 +00002788 pjsua_perror(THIS_FILE, "Error opening sound device",
2789 status);
Benny Prijonob90fd382011-09-18 14:59:56 +00002790 goto on_return;
Benny Prijonod65f78c2009-06-03 18:59:37 +00002791 }
2792 } else {
2793 /* Null-audio */
Benny Prijonoc45d9512010-12-10 11:04:30 +00002794 status = pjsua_set_snd_dev(pjsua_var.cap_dev,
2795 pjsua_var.play_dev);
Benny Prijonod65f78c2009-06-03 18:59:37 +00002796 if (status != PJ_SUCCESS) {
Benny Prijonoc45d9512010-12-10 11:04:30 +00002797 pjsua_perror(THIS_FILE, "Error opening sound device",
2798 status);
Benny Prijonob90fd382011-09-18 14:59:56 +00002799 goto on_return;
Benny Prijonod65f78c2009-06-03 18:59:37 +00002800 }
Nanang Izzuddinfe02a062009-02-18 14:28:49 +00002801 }
Benny Prijono2d647722011-07-13 03:05:22 +00002802 } else if (pjsua_var.no_snd) {
2803 if (!pjsua_var.snd_is_on) {
2804 pjsua_var.snd_is_on = PJ_TRUE;
2805 /* Notify app */
2806 if (pjsua_var.ua_cfg.cb.on_snd_dev_operation) {
2807 (*pjsua_var.ua_cfg.cb.on_snd_dev_operation)(1);
2808 }
2809 }
Nanang Izzuddinfe02a062009-02-18 14:28:49 +00002810 }
Nanang Izzuddinfe02a062009-02-18 14:28:49 +00002811
Benny Prijonof798e502009-03-09 13:08:16 +00002812 } else {
2813 /* The bridge version */
Nanang Izzuddinfe02a062009-02-18 14:28:49 +00002814
Benny Prijonof798e502009-03-09 13:08:16 +00002815 /* Create sound port if none is instantiated */
2816 if (pjsua_var.snd_port==NULL && pjsua_var.null_snd==NULL &&
2817 !pjsua_var.no_snd)
2818 {
2819 pj_status_t status;
Nanang Izzuddin68559c32008-06-13 17:01:46 +00002820
Benny Prijonof798e502009-03-09 13:08:16 +00002821 status = pjsua_set_snd_dev(pjsua_var.cap_dev, pjsua_var.play_dev);
2822 if (status != PJ_SUCCESS) {
2823 pjsua_perror(THIS_FILE, "Error opening sound device", status);
Benny Prijonob90fd382011-09-18 14:59:56 +00002824 goto on_return;
Benny Prijonof798e502009-03-09 13:08:16 +00002825 }
Benny Prijono2d647722011-07-13 03:05:22 +00002826 } else if (pjsua_var.no_snd && !pjsua_var.snd_is_on) {
2827 pjsua_var.snd_is_on = PJ_TRUE;
2828 /* Notify app */
2829 if (pjsua_var.ua_cfg.cb.on_snd_dev_operation) {
2830 (*pjsua_var.ua_cfg.cb.on_snd_dev_operation)(1);
2831 }
Nanang Izzuddin68559c32008-06-13 17:01:46 +00002832 }
Benny Prijonof798e502009-03-09 13:08:16 +00002833 }
Nanang Izzuddinfe02a062009-02-18 14:28:49 +00002834
Benny Prijonob90fd382011-09-18 14:59:56 +00002835 status = pjmedia_conf_connect_port(pjsua_var.mconf, source, sink, 0);
2836
2837on_return:
2838 pj_log_pop_indent();
2839 return status;
Benny Prijonoeebe9af2006-06-13 22:57:13 +00002840}
2841
2842
2843/*
2844 * Disconnect media flow from the source to destination port.
2845 */
2846PJ_DEF(pj_status_t) pjsua_conf_disconnect( pjsua_conf_port_id source,
2847 pjsua_conf_port_id sink)
2848{
Nanang Izzuddin68559c32008-06-13 17:01:46 +00002849 pj_status_t status;
2850
Benny Prijonob90fd382011-09-18 14:59:56 +00002851 PJ_LOG(4,(THIS_FILE, "%s disconnect: %d -x- %d",
2852 (pjsua_var.is_mswitch ? "Switch" : "Conf"),
2853 source, sink));
2854 pj_log_push_indent();
2855
Nanang Izzuddin68559c32008-06-13 17:01:46 +00002856 status = pjmedia_conf_disconnect_port(pjsua_var.mconf, source, sink);
Nanang Izzuddin148fd392008-06-16 09:52:50 +00002857 check_snd_dev_idle();
Nanang Izzuddin68559c32008-06-13 17:01:46 +00002858
Benny Prijonob90fd382011-09-18 14:59:56 +00002859 pj_log_pop_indent();
Nanang Izzuddin68559c32008-06-13 17:01:46 +00002860 return status;
Benny Prijonoeebe9af2006-06-13 22:57:13 +00002861}
2862
2863
Benny Prijono6dd967c2006-12-26 02:27:14 +00002864/*
2865 * Adjust the signal level to be transmitted from the bridge to the
2866 * specified port by making it louder or quieter.
2867 */
2868PJ_DEF(pj_status_t) pjsua_conf_adjust_tx_level(pjsua_conf_port_id slot,
2869 float level)
2870{
2871 return pjmedia_conf_adjust_tx_level(pjsua_var.mconf, slot,
2872 (int)((level-1) * 128));
2873}
2874
2875/*
2876 * Adjust the signal level to be received from the specified port (to
2877 * the bridge) by making it louder or quieter.
2878 */
2879PJ_DEF(pj_status_t) pjsua_conf_adjust_rx_level(pjsua_conf_port_id slot,
2880 float level)
2881{
2882 return pjmedia_conf_adjust_rx_level(pjsua_var.mconf, slot,
2883 (int)((level-1) * 128));
2884}
2885
2886
2887/*
2888 * Get last signal level transmitted to or received from the specified port.
2889 */
2890PJ_DEF(pj_status_t) pjsua_conf_get_signal_level(pjsua_conf_port_id slot,
2891 unsigned *tx_level,
2892 unsigned *rx_level)
2893{
2894 return pjmedia_conf_get_signal_level(pjsua_var.mconf, slot,
2895 tx_level, rx_level);
2896}
2897
Benny Prijonoeebe9af2006-06-13 22:57:13 +00002898/*****************************************************************************
2899 * File player.
2900 */
2901
Benny Prijonod5696da2007-07-17 16:25:45 +00002902static char* get_basename(const char *path, unsigned len)
2903{
2904 char *p = ((char*)path) + len;
2905
2906 if (len==0)
2907 return p;
2908
Benny Prijono1f61a8f2007-08-16 10:11:44 +00002909 for (--p; p!=path && *p!='/' && *p!='\\'; ) --p;
Benny Prijonod5696da2007-07-17 16:25:45 +00002910
2911 return (p==path) ? p : p+1;
2912}
2913
2914
Benny Prijonoeebe9af2006-06-13 22:57:13 +00002915/*
2916 * Create a file player, and automatically connect this player to
2917 * the conference bridge.
2918 */
2919PJ_DEF(pj_status_t) pjsua_player_create( const pj_str_t *filename,
2920 unsigned options,
Benny Prijonoeebe9af2006-06-13 22:57:13 +00002921 pjsua_player_id *p_id)
2922{
2923 unsigned slot, file_id;
Benny Prijonoa66c3312007-01-21 23:12:40 +00002924 char path[PJ_MAXPATH];
Benny Prijonob90fd382011-09-18 14:59:56 +00002925 pj_pool_t *pool = NULL;
Benny Prijonoeebe9af2006-06-13 22:57:13 +00002926 pjmedia_port *port;
Benny Prijonob90fd382011-09-18 14:59:56 +00002927 pj_status_t status = PJ_SUCCESS;
Benny Prijonoeebe9af2006-06-13 22:57:13 +00002928
2929 if (pjsua_var.player_cnt >= PJ_ARRAY_SIZE(pjsua_var.player))
2930 return PJ_ETOOMANY;
2931
Benny Prijonob90fd382011-09-18 14:59:56 +00002932 PJ_LOG(4,(THIS_FILE, "Creating file player: %.*s..",
2933 (int)filename->slen, filename->ptr));
2934 pj_log_push_indent();
2935
Benny Prijonoeebe9af2006-06-13 22:57:13 +00002936 PJSUA_LOCK();
2937
2938 for (file_id=0; file_id<PJ_ARRAY_SIZE(pjsua_var.player); ++file_id) {
2939 if (pjsua_var.player[file_id].port == NULL)
2940 break;
2941 }
2942
2943 if (file_id == PJ_ARRAY_SIZE(pjsua_var.player)) {
2944 /* This is unexpected */
Benny Prijonoeebe9af2006-06-13 22:57:13 +00002945 pj_assert(0);
Benny Prijonob90fd382011-09-18 14:59:56 +00002946 status = PJ_EBUG;
2947 goto on_error;
Benny Prijonoeebe9af2006-06-13 22:57:13 +00002948 }
2949
2950 pj_memcpy(path, filename->ptr, filename->slen);
2951 path[filename->slen] = '\0';
Benny Prijonod5696da2007-07-17 16:25:45 +00002952
2953 pool = pjsua_pool_create(get_basename(path, filename->slen), 1000, 1000);
2954 if (!pool) {
Benny Prijonob90fd382011-09-18 14:59:56 +00002955 status = PJ_ENOMEM;
2956 goto on_error;
Benny Prijonod5696da2007-07-17 16:25:45 +00002957 }
2958
Nanang Izzuddin81e9bd52008-06-27 12:52:51 +00002959 status = pjmedia_wav_player_port_create(
2960 pool, path,
2961 pjsua_var.mconf_cfg.samples_per_frame *
Benny Prijonoc45d9512010-12-10 11:04:30 +00002962 1000 / pjsua_var.media_cfg.channel_count /
Nanang Izzuddin81e9bd52008-06-27 12:52:51 +00002963 pjsua_var.media_cfg.clock_rate,
2964 options, 0, &port);
Benny Prijonoeebe9af2006-06-13 22:57:13 +00002965 if (status != PJ_SUCCESS) {
Benny Prijonoeebe9af2006-06-13 22:57:13 +00002966 pjsua_perror(THIS_FILE, "Unable to open file for playback", status);
Benny Prijonob90fd382011-09-18 14:59:56 +00002967 goto on_error;
Benny Prijonoeebe9af2006-06-13 22:57:13 +00002968 }
2969
Benny Prijono5297af92008-03-18 13:40:40 +00002970 status = pjmedia_conf_add_port(pjsua_var.mconf, pool,
Benny Prijonoeebe9af2006-06-13 22:57:13 +00002971 port, filename, &slot);
2972 if (status != PJ_SUCCESS) {
2973 pjmedia_port_destroy(port);
Benny Prijono32e4f492007-01-24 00:44:26 +00002974 pjsua_perror(THIS_FILE, "Unable to add file to conference bridge",
2975 status);
Benny Prijonob90fd382011-09-18 14:59:56 +00002976 goto on_error;
Benny Prijonoeebe9af2006-06-13 22:57:13 +00002977 }
2978
Benny Prijonoa66c3312007-01-21 23:12:40 +00002979 pjsua_var.player[file_id].type = 0;
Benny Prijonod5696da2007-07-17 16:25:45 +00002980 pjsua_var.player[file_id].pool = pool;
Benny Prijonoeebe9af2006-06-13 22:57:13 +00002981 pjsua_var.player[file_id].port = port;
2982 pjsua_var.player[file_id].slot = slot;
2983
2984 if (p_id) *p_id = file_id;
2985
2986 ++pjsua_var.player_cnt;
2987
2988 PJSUA_UNLOCK();
Benny Prijonob90fd382011-09-18 14:59:56 +00002989
2990 PJ_LOG(4,(THIS_FILE, "Player created, id=%d, slot=%d", file_id, slot));
2991
2992 pj_log_pop_indent();
Benny Prijonoeebe9af2006-06-13 22:57:13 +00002993 return PJ_SUCCESS;
Benny Prijonob90fd382011-09-18 14:59:56 +00002994
2995on_error:
2996 PJSUA_UNLOCK();
2997 if (pool) pj_pool_release(pool);
2998 pj_log_pop_indent();
2999 return status;
Benny Prijonoeebe9af2006-06-13 22:57:13 +00003000}
3001
3002
3003/*
Benny Prijonoa66c3312007-01-21 23:12:40 +00003004 * Create a file playlist media port, and automatically add the port
3005 * to the conference bridge.
3006 */
3007PJ_DEF(pj_status_t) pjsua_playlist_create( const pj_str_t file_names[],
3008 unsigned file_count,
3009 const pj_str_t *label,
3010 unsigned options,
3011 pjsua_player_id *p_id)
3012{
3013 unsigned slot, file_id, ptime;
Benny Prijonob90fd382011-09-18 14:59:56 +00003014 pj_pool_t *pool = NULL;
Benny Prijonoa66c3312007-01-21 23:12:40 +00003015 pjmedia_port *port;
Benny Prijonob90fd382011-09-18 14:59:56 +00003016 pj_status_t status = PJ_SUCCESS;
Benny Prijonoa66c3312007-01-21 23:12:40 +00003017
3018 if (pjsua_var.player_cnt >= PJ_ARRAY_SIZE(pjsua_var.player))
3019 return PJ_ETOOMANY;
3020
Benny Prijonob90fd382011-09-18 14:59:56 +00003021 PJ_LOG(4,(THIS_FILE, "Creating playlist with %d file(s)..", file_count));
3022 pj_log_push_indent();
3023
Benny Prijonoa66c3312007-01-21 23:12:40 +00003024 PJSUA_LOCK();
3025
3026 for (file_id=0; file_id<PJ_ARRAY_SIZE(pjsua_var.player); ++file_id) {
3027 if (pjsua_var.player[file_id].port == NULL)
3028 break;
3029 }
3030
3031 if (file_id == PJ_ARRAY_SIZE(pjsua_var.player)) {
3032 /* This is unexpected */
Benny Prijonoa66c3312007-01-21 23:12:40 +00003033 pj_assert(0);
Benny Prijonob90fd382011-09-18 14:59:56 +00003034 status = PJ_EBUG;
3035 goto on_error;
Benny Prijonoa66c3312007-01-21 23:12:40 +00003036 }
3037
3038
3039 ptime = pjsua_var.mconf_cfg.samples_per_frame * 1000 /
3040 pjsua_var.media_cfg.clock_rate;
3041
Benny Prijonod5696da2007-07-17 16:25:45 +00003042 pool = pjsua_pool_create("playlist", 1000, 1000);
3043 if (!pool) {
Benny Prijonob90fd382011-09-18 14:59:56 +00003044 status = PJ_ENOMEM;
3045 goto on_error;
Benny Prijonod5696da2007-07-17 16:25:45 +00003046 }
3047
3048 status = pjmedia_wav_playlist_create(pool, label,
Benny Prijonoa66c3312007-01-21 23:12:40 +00003049 file_names, file_count,
3050 ptime, options, 0, &port);
3051 if (status != PJ_SUCCESS) {
Benny Prijonoa66c3312007-01-21 23:12:40 +00003052 pjsua_perror(THIS_FILE, "Unable to create playlist", status);
Benny Prijonob90fd382011-09-18 14:59:56 +00003053 goto on_error;
Benny Prijonoa66c3312007-01-21 23:12:40 +00003054 }
3055
Benny Prijonod5696da2007-07-17 16:25:45 +00003056 status = pjmedia_conf_add_port(pjsua_var.mconf, pool,
Benny Prijonoa66c3312007-01-21 23:12:40 +00003057 port, &port->info.name, &slot);
3058 if (status != PJ_SUCCESS) {
3059 pjmedia_port_destroy(port);
Benny Prijonoa66c3312007-01-21 23:12:40 +00003060 pjsua_perror(THIS_FILE, "Unable to add port", status);
Benny Prijonob90fd382011-09-18 14:59:56 +00003061 goto on_error;
Benny Prijonoa66c3312007-01-21 23:12:40 +00003062 }
3063
3064 pjsua_var.player[file_id].type = 1;
Benny Prijonod5696da2007-07-17 16:25:45 +00003065 pjsua_var.player[file_id].pool = pool;
Benny Prijonoa66c3312007-01-21 23:12:40 +00003066 pjsua_var.player[file_id].port = port;
3067 pjsua_var.player[file_id].slot = slot;
3068
3069 if (p_id) *p_id = file_id;
3070
3071 ++pjsua_var.player_cnt;
3072
3073 PJSUA_UNLOCK();
Benny Prijonob90fd382011-09-18 14:59:56 +00003074
3075 PJ_LOG(4,(THIS_FILE, "Playlist created, id=%d, slot=%d", file_id, slot));
3076
3077 pj_log_pop_indent();
3078
Benny Prijonoa66c3312007-01-21 23:12:40 +00003079 return PJ_SUCCESS;
3080
Benny Prijonob90fd382011-09-18 14:59:56 +00003081on_error:
3082 PJSUA_UNLOCK();
3083 if (pool) pj_pool_release(pool);
3084 pj_log_pop_indent();
3085
3086 return status;
Benny Prijonoa66c3312007-01-21 23:12:40 +00003087}
3088
3089
3090/*
Benny Prijonoeebe9af2006-06-13 22:57:13 +00003091 * Get conference port ID associated with player.
3092 */
3093PJ_DEF(pjsua_conf_port_id) pjsua_player_get_conf_port(pjsua_player_id id)
3094{
Benny Prijonoa1e69682007-05-11 15:14:34 +00003095 PJ_ASSERT_RETURN(id>=0&&id<(int)PJ_ARRAY_SIZE(pjsua_var.player), PJ_EINVAL);
Benny Prijonoeebe9af2006-06-13 22:57:13 +00003096 PJ_ASSERT_RETURN(pjsua_var.player[id].port != NULL, PJ_EINVAL);
3097
3098 return pjsua_var.player[id].slot;
3099}
3100
Benny Prijono469b1522006-12-26 03:05:17 +00003101/*
3102 * Get the media port for the player.
3103 */
Benny Prijonobe41d862008-01-18 13:24:28 +00003104PJ_DEF(pj_status_t) pjsua_player_get_port( pjsua_player_id id,
Benny Prijono469b1522006-12-26 03:05:17 +00003105 pjmedia_port **p_port)
3106{
Benny Prijonoa1e69682007-05-11 15:14:34 +00003107 PJ_ASSERT_RETURN(id>=0&&id<(int)PJ_ARRAY_SIZE(pjsua_var.player), PJ_EINVAL);
Benny Prijono469b1522006-12-26 03:05:17 +00003108 PJ_ASSERT_RETURN(pjsua_var.player[id].port != NULL, PJ_EINVAL);
3109 PJ_ASSERT_RETURN(p_port != NULL, PJ_EINVAL);
3110
3111 *p_port = pjsua_var.player[id].port;
3112
3113 return PJ_SUCCESS;
3114}
Benny Prijonoeebe9af2006-06-13 22:57:13 +00003115
3116/*
3117 * Set playback position.
3118 */
3119PJ_DEF(pj_status_t) pjsua_player_set_pos( pjsua_player_id id,
3120 pj_uint32_t samples)
3121{
Benny Prijonoa1e69682007-05-11 15:14:34 +00003122 PJ_ASSERT_RETURN(id>=0&&id<(int)PJ_ARRAY_SIZE(pjsua_var.player), PJ_EINVAL);
Benny Prijonoeebe9af2006-06-13 22:57:13 +00003123 PJ_ASSERT_RETURN(pjsua_var.player[id].port != NULL, PJ_EINVAL);
Benny Prijonoa66c3312007-01-21 23:12:40 +00003124 PJ_ASSERT_RETURN(pjsua_var.player[id].type == 0, PJ_EINVAL);
Benny Prijonoeebe9af2006-06-13 22:57:13 +00003125
3126 return pjmedia_wav_player_port_set_pos(pjsua_var.player[id].port, samples);
3127}
3128
3129
3130/*
3131 * Close the file, remove the player from the bridge, and free
3132 * resources associated with the file player.
3133 */
3134PJ_DEF(pj_status_t) pjsua_player_destroy(pjsua_player_id id)
3135{
Benny Prijonoa1e69682007-05-11 15:14:34 +00003136 PJ_ASSERT_RETURN(id>=0&&id<(int)PJ_ARRAY_SIZE(pjsua_var.player), PJ_EINVAL);
Benny Prijonoeebe9af2006-06-13 22:57:13 +00003137 PJ_ASSERT_RETURN(pjsua_var.player[id].port != NULL, PJ_EINVAL);
3138
Benny Prijonob90fd382011-09-18 14:59:56 +00003139 PJ_LOG(4,(THIS_FILE, "Destroying player %d..", id));
3140 pj_log_push_indent();
3141
Benny Prijonoeebe9af2006-06-13 22:57:13 +00003142 PJSUA_LOCK();
3143
3144 if (pjsua_var.player[id].port) {
Nanang Izzuddin148fd392008-06-16 09:52:50 +00003145 pjsua_conf_remove_port(pjsua_var.player[id].slot);
Benny Prijonoeebe9af2006-06-13 22:57:13 +00003146 pjmedia_port_destroy(pjsua_var.player[id].port);
3147 pjsua_var.player[id].port = NULL;
3148 pjsua_var.player[id].slot = 0xFFFF;
Benny Prijonod5696da2007-07-17 16:25:45 +00003149 pj_pool_release(pjsua_var.player[id].pool);
3150 pjsua_var.player[id].pool = NULL;
Benny Prijonoeebe9af2006-06-13 22:57:13 +00003151 pjsua_var.player_cnt--;
3152 }
3153
3154 PJSUA_UNLOCK();
Benny Prijonob90fd382011-09-18 14:59:56 +00003155 pj_log_pop_indent();
Benny Prijonoeebe9af2006-06-13 22:57:13 +00003156
3157 return PJ_SUCCESS;
3158}
3159
3160
3161/*****************************************************************************
3162 * File recorder.
3163 */
3164
3165/*
3166 * Create a file recorder, and automatically connect this recorder to
3167 * the conference bridge.
3168 */
3169PJ_DEF(pj_status_t) pjsua_recorder_create( const pj_str_t *filename,
Benny Prijono8f310522006-10-20 11:08:49 +00003170 unsigned enc_type,
3171 void *enc_param,
Benny Prijonoeebe9af2006-06-13 22:57:13 +00003172 pj_ssize_t max_size,
3173 unsigned options,
Benny Prijonoeebe9af2006-06-13 22:57:13 +00003174 pjsua_recorder_id *p_id)
3175{
Benny Prijonob3cdb2b2006-10-19 15:49:47 +00003176 enum Format
3177 {
3178 FMT_UNKNOWN,
3179 FMT_WAV,
3180 FMT_MP3,
3181 };
Benny Prijonoeebe9af2006-06-13 22:57:13 +00003182 unsigned slot, file_id;
Benny Prijonoa66c3312007-01-21 23:12:40 +00003183 char path[PJ_MAXPATH];
Benny Prijonob3cdb2b2006-10-19 15:49:47 +00003184 pj_str_t ext;
Benny Prijono8f310522006-10-20 11:08:49 +00003185 int file_format;
Benny Prijonob90fd382011-09-18 14:59:56 +00003186 pj_pool_t *pool = NULL;
Benny Prijonoeebe9af2006-06-13 22:57:13 +00003187 pjmedia_port *port;
Benny Prijonob90fd382011-09-18 14:59:56 +00003188 pj_status_t status = PJ_SUCCESS;
Benny Prijonoeebe9af2006-06-13 22:57:13 +00003189
Benny Prijonob3cdb2b2006-10-19 15:49:47 +00003190 /* Filename must present */
3191 PJ_ASSERT_RETURN(filename != NULL, PJ_EINVAL);
3192
Benny Prijono00cae612006-07-31 15:19:36 +00003193 /* Don't support max_size at present */
Benny Prijonob3cdb2b2006-10-19 15:49:47 +00003194 PJ_ASSERT_RETURN(max_size == 0 || max_size == -1, PJ_EINVAL);
Benny Prijono00cae612006-07-31 15:19:36 +00003195
Benny Prijono8f310522006-10-20 11:08:49 +00003196 /* Don't support encoding type at present */
3197 PJ_ASSERT_RETURN(enc_type == 0, PJ_EINVAL);
Benny Prijono00cae612006-07-31 15:19:36 +00003198
Benny Prijonob90fd382011-09-18 14:59:56 +00003199 PJ_LOG(4,(THIS_FILE, "Creating recorder %.*s..",
3200 (int)filename->slen, filename->ptr));
3201 pj_log_push_indent();
3202
3203 if (pjsua_var.rec_cnt >= PJ_ARRAY_SIZE(pjsua_var.recorder)) {
3204 pj_log_pop_indent();
Benny Prijonoeebe9af2006-06-13 22:57:13 +00003205 return PJ_ETOOMANY;
Benny Prijonob90fd382011-09-18 14:59:56 +00003206 }
Benny Prijonoeebe9af2006-06-13 22:57:13 +00003207
Benny Prijonob3cdb2b2006-10-19 15:49:47 +00003208 /* Determine the file format */
3209 ext.ptr = filename->ptr + filename->slen - 4;
3210 ext.slen = 4;
3211
3212 if (pj_stricmp2(&ext, ".wav") == 0)
3213 file_format = FMT_WAV;
3214 else if (pj_stricmp2(&ext, ".mp3") == 0)
3215 file_format = FMT_MP3;
3216 else {
3217 PJ_LOG(1,(THIS_FILE, "pjsua_recorder_create() error: unable to "
3218 "determine file format for %.*s",
3219 (int)filename->slen, filename->ptr));
Benny Prijonob90fd382011-09-18 14:59:56 +00003220 pj_log_pop_indent();
Benny Prijonob3cdb2b2006-10-19 15:49:47 +00003221 return PJ_ENOTSUP;
3222 }
3223
Benny Prijonoeebe9af2006-06-13 22:57:13 +00003224 PJSUA_LOCK();
3225
3226 for (file_id=0; file_id<PJ_ARRAY_SIZE(pjsua_var.recorder); ++file_id) {
3227 if (pjsua_var.recorder[file_id].port == NULL)
3228 break;
3229 }
3230
3231 if (file_id == PJ_ARRAY_SIZE(pjsua_var.recorder)) {
3232 /* This is unexpected */
Benny Prijonoeebe9af2006-06-13 22:57:13 +00003233 pj_assert(0);
Benny Prijonob90fd382011-09-18 14:59:56 +00003234 status = PJ_EBUG;
3235 goto on_return;
Benny Prijonoeebe9af2006-06-13 22:57:13 +00003236 }
3237
3238 pj_memcpy(path, filename->ptr, filename->slen);
3239 path[filename->slen] = '\0';
Benny Prijonob3cdb2b2006-10-19 15:49:47 +00003240
Benny Prijonod5696da2007-07-17 16:25:45 +00003241 pool = pjsua_pool_create(get_basename(path, filename->slen), 1000, 1000);
3242 if (!pool) {
Benny Prijonob90fd382011-09-18 14:59:56 +00003243 status = PJ_ENOMEM;
3244 goto on_return;
Benny Prijonod5696da2007-07-17 16:25:45 +00003245 }
3246
Benny Prijonob3cdb2b2006-10-19 15:49:47 +00003247 if (file_format == FMT_WAV) {
Benny Prijonod5696da2007-07-17 16:25:45 +00003248 status = pjmedia_wav_writer_port_create(pool, path,
Benny Prijonob3cdb2b2006-10-19 15:49:47 +00003249 pjsua_var.media_cfg.clock_rate,
3250 pjsua_var.mconf_cfg.channel_count,
3251 pjsua_var.mconf_cfg.samples_per_frame,
3252 pjsua_var.mconf_cfg.bits_per_sample,
3253 options, 0, &port);
Benny Prijonob3cdb2b2006-10-19 15:49:47 +00003254 } else {
Benny Prijonoc95a0f02007-04-09 07:06:08 +00003255 PJ_UNUSED_ARG(enc_param);
Benny Prijonob3cdb2b2006-10-19 15:49:47 +00003256 port = NULL;
3257 status = PJ_ENOTSUP;
3258 }
3259
Benny Prijonoeebe9af2006-06-13 22:57:13 +00003260 if (status != PJ_SUCCESS) {
Benny Prijonoeebe9af2006-06-13 22:57:13 +00003261 pjsua_perror(THIS_FILE, "Unable to open file for recording", status);
Benny Prijonob90fd382011-09-18 14:59:56 +00003262 goto on_return;
Benny Prijonoeebe9af2006-06-13 22:57:13 +00003263 }
3264
Benny Prijonod5696da2007-07-17 16:25:45 +00003265 status = pjmedia_conf_add_port(pjsua_var.mconf, pool,
Benny Prijonoeebe9af2006-06-13 22:57:13 +00003266 port, filename, &slot);
3267 if (status != PJ_SUCCESS) {
3268 pjmedia_port_destroy(port);
Benny Prijonob90fd382011-09-18 14:59:56 +00003269 goto on_return;
Benny Prijonoeebe9af2006-06-13 22:57:13 +00003270 }
3271
3272 pjsua_var.recorder[file_id].port = port;
3273 pjsua_var.recorder[file_id].slot = slot;
Benny Prijonod5696da2007-07-17 16:25:45 +00003274 pjsua_var.recorder[file_id].pool = pool;
Benny Prijonoeebe9af2006-06-13 22:57:13 +00003275
3276 if (p_id) *p_id = file_id;
3277
3278 ++pjsua_var.rec_cnt;
3279
3280 PJSUA_UNLOCK();
Benny Prijonob90fd382011-09-18 14:59:56 +00003281
3282 PJ_LOG(4,(THIS_FILE, "Recorder created, id=%d, slot=%d", file_id, slot));
3283
3284 pj_log_pop_indent();
Benny Prijonoeebe9af2006-06-13 22:57:13 +00003285 return PJ_SUCCESS;
Benny Prijonob90fd382011-09-18 14:59:56 +00003286
3287on_return:
3288 PJSUA_UNLOCK();
3289 if (pool) pj_pool_release(pool);
3290 pj_log_pop_indent();
3291 return status;
Benny Prijonoeebe9af2006-06-13 22:57:13 +00003292}
3293
3294
3295/*
3296 * Get conference port associated with recorder.
3297 */
3298PJ_DEF(pjsua_conf_port_id) pjsua_recorder_get_conf_port(pjsua_recorder_id id)
3299{
Benny Prijonoa1e69682007-05-11 15:14:34 +00003300 PJ_ASSERT_RETURN(id>=0 && id<(int)PJ_ARRAY_SIZE(pjsua_var.recorder),
3301 PJ_EINVAL);
Benny Prijonoeebe9af2006-06-13 22:57:13 +00003302 PJ_ASSERT_RETURN(pjsua_var.recorder[id].port != NULL, PJ_EINVAL);
3303
3304 return pjsua_var.recorder[id].slot;
3305}
3306
Benny Prijono469b1522006-12-26 03:05:17 +00003307/*
3308 * Get the media port for the recorder.
3309 */
3310PJ_DEF(pj_status_t) pjsua_recorder_get_port( pjsua_recorder_id id,
3311 pjmedia_port **p_port)
3312{
Benny Prijonoa1e69682007-05-11 15:14:34 +00003313 PJ_ASSERT_RETURN(id>=0 && id<(int)PJ_ARRAY_SIZE(pjsua_var.recorder),
3314 PJ_EINVAL);
Benny Prijono469b1522006-12-26 03:05:17 +00003315 PJ_ASSERT_RETURN(pjsua_var.recorder[id].port != NULL, PJ_EINVAL);
3316 PJ_ASSERT_RETURN(p_port != NULL, PJ_EINVAL);
3317
3318 *p_port = pjsua_var.recorder[id].port;
3319 return PJ_SUCCESS;
3320}
Benny Prijonoeebe9af2006-06-13 22:57:13 +00003321
3322/*
3323 * Destroy recorder (this will complete recording).
3324 */
3325PJ_DEF(pj_status_t) pjsua_recorder_destroy(pjsua_recorder_id id)
3326{
Benny Prijonoa1e69682007-05-11 15:14:34 +00003327 PJ_ASSERT_RETURN(id>=0 && id<(int)PJ_ARRAY_SIZE(pjsua_var.recorder),
3328 PJ_EINVAL);
Benny Prijonoeebe9af2006-06-13 22:57:13 +00003329 PJ_ASSERT_RETURN(pjsua_var.recorder[id].port != NULL, PJ_EINVAL);
3330
Benny Prijonob90fd382011-09-18 14:59:56 +00003331 PJ_LOG(4,(THIS_FILE, "Destroying recorder %d..", id));
3332 pj_log_push_indent();
3333
Benny Prijonoeebe9af2006-06-13 22:57:13 +00003334 PJSUA_LOCK();
3335
3336 if (pjsua_var.recorder[id].port) {
Nanang Izzuddin148fd392008-06-16 09:52:50 +00003337 pjsua_conf_remove_port(pjsua_var.recorder[id].slot);
Benny Prijonoeebe9af2006-06-13 22:57:13 +00003338 pjmedia_port_destroy(pjsua_var.recorder[id].port);
3339 pjsua_var.recorder[id].port = NULL;
3340 pjsua_var.recorder[id].slot = 0xFFFF;
Benny Prijonod5696da2007-07-17 16:25:45 +00003341 pj_pool_release(pjsua_var.recorder[id].pool);
3342 pjsua_var.recorder[id].pool = NULL;
Benny Prijonoeebe9af2006-06-13 22:57:13 +00003343 pjsua_var.rec_cnt--;
3344 }
3345
3346 PJSUA_UNLOCK();
Benny Prijonob90fd382011-09-18 14:59:56 +00003347 pj_log_pop_indent();
Benny Prijonoeebe9af2006-06-13 22:57:13 +00003348
3349 return PJ_SUCCESS;
3350}
3351
3352
3353/*****************************************************************************
3354 * Sound devices.
3355 */
3356
3357/*
3358 * Enum sound devices.
3359 */
Benny Prijonof798e502009-03-09 13:08:16 +00003360
3361PJ_DEF(pj_status_t) pjsua_enum_aud_devs( pjmedia_aud_dev_info info[],
Benny Prijonoeebe9af2006-06-13 22:57:13 +00003362 unsigned *count)
3363{
3364 unsigned i, dev_count;
3365
Benny Prijono10454dc2009-02-21 14:21:59 +00003366 dev_count = pjmedia_aud_dev_count();
Benny Prijonoeebe9af2006-06-13 22:57:13 +00003367
3368 if (dev_count > *count) dev_count = *count;
3369
3370 for (i=0; i<dev_count; ++i) {
Benny Prijono10454dc2009-02-21 14:21:59 +00003371 pj_status_t status;
Benny Prijonoeebe9af2006-06-13 22:57:13 +00003372
Benny Prijono10454dc2009-02-21 14:21:59 +00003373 status = pjmedia_aud_dev_get_info(i, &info[i]);
3374 if (status != PJ_SUCCESS)
3375 return status;
Benny Prijonoeebe9af2006-06-13 22:57:13 +00003376 }
3377
3378 *count = dev_count;
3379
3380 return PJ_SUCCESS;
3381}
Benny Prijonof798e502009-03-09 13:08:16 +00003382
3383
Benny Prijono10454dc2009-02-21 14:21:59 +00003384PJ_DEF(pj_status_t) pjsua_enum_snd_devs( pjmedia_snd_dev_info info[],
3385 unsigned *count)
3386{
3387 unsigned i, dev_count;
3388
3389 dev_count = pjmedia_aud_dev_count();
3390
3391 if (dev_count > *count) dev_count = *count;
3392 pj_bzero(info, dev_count * sizeof(pjmedia_snd_dev_info));
3393
3394 for (i=0; i<dev_count; ++i) {
3395 pjmedia_aud_dev_info ai;
3396 pj_status_t status;
3397
3398 status = pjmedia_aud_dev_get_info(i, &ai);
3399 if (status != PJ_SUCCESS)
3400 return status;
3401
3402 strncpy(info[i].name, ai.name, sizeof(info[i].name));
3403 info[i].name[sizeof(info[i].name)-1] = '\0';
3404 info[i].input_count = ai.input_count;
3405 info[i].output_count = ai.output_count;
3406 info[i].default_samples_per_sec = ai.default_samples_per_sec;
3407 }
3408
3409 *count = dev_count;
3410
3411 return PJ_SUCCESS;
3412}
Benny Prijono10454dc2009-02-21 14:21:59 +00003413
Benny Prijonof798e502009-03-09 13:08:16 +00003414/* Create audio device parameter to open the device */
3415static pj_status_t create_aud_param(pjmedia_aud_param *param,
3416 pjmedia_aud_dev_index capture_dev,
3417 pjmedia_aud_dev_index playback_dev,
3418 unsigned clock_rate,
3419 unsigned channel_count,
3420 unsigned samples_per_frame,
3421 unsigned bits_per_sample)
Benny Prijonoeebe9af2006-06-13 22:57:13 +00003422{
Nanang Izzuddin8465c682009-03-04 17:23:25 +00003423 pj_status_t status;
3424
Benny Prijono96e74f32009-02-22 12:00:12 +00003425 /* Normalize device ID with new convention about default device ID */
3426 if (playback_dev == PJMEDIA_AUD_DEFAULT_CAPTURE_DEV)
3427 playback_dev = PJMEDIA_AUD_DEFAULT_PLAYBACK_DEV;
3428
Benny Prijono10454dc2009-02-21 14:21:59 +00003429 /* Create default parameters for the device */
Benny Prijonof798e502009-03-09 13:08:16 +00003430 status = pjmedia_aud_dev_default_param(capture_dev, param);
Benny Prijono10454dc2009-02-21 14:21:59 +00003431 if (status != PJ_SUCCESS) {
Benny Prijono96e74f32009-02-22 12:00:12 +00003432 pjsua_perror(THIS_FILE, "Error retrieving default audio "
3433 "device parameters", status);
Benny Prijono10454dc2009-02-21 14:21:59 +00003434 return status;
3435 }
Benny Prijonof798e502009-03-09 13:08:16 +00003436 param->dir = PJMEDIA_DIR_CAPTURE_PLAYBACK;
3437 param->rec_id = capture_dev;
3438 param->play_id = playback_dev;
3439 param->clock_rate = clock_rate;
3440 param->channel_count = channel_count;
3441 param->samples_per_frame = samples_per_frame;
3442 param->bits_per_sample = bits_per_sample;
3443
3444 /* Update the setting with user preference */
3445#define update_param(cap, field) \
3446 if (pjsua_var.aud_param.flags & cap) { \
3447 param->flags |= cap; \
3448 param->field = pjsua_var.aud_param.field; \
3449 }
3450 update_param( PJMEDIA_AUD_DEV_CAP_INPUT_VOLUME_SETTING, input_vol);
3451 update_param( PJMEDIA_AUD_DEV_CAP_OUTPUT_VOLUME_SETTING, output_vol);
3452 update_param( PJMEDIA_AUD_DEV_CAP_INPUT_ROUTE, input_route);
3453 update_param( PJMEDIA_AUD_DEV_CAP_OUTPUT_ROUTE, output_route);
3454#undef update_param
3455
Benny Prijono10454dc2009-02-21 14:21:59 +00003456 /* Latency settings */
Benny Prijonof798e502009-03-09 13:08:16 +00003457 param->flags |= (PJMEDIA_AUD_DEV_CAP_INPUT_LATENCY |
3458 PJMEDIA_AUD_DEV_CAP_OUTPUT_LATENCY);
3459 param->input_latency_ms = pjsua_var.media_cfg.snd_rec_latency;
3460 param->output_latency_ms = pjsua_var.media_cfg.snd_play_latency;
3461
Benny Prijono10454dc2009-02-21 14:21:59 +00003462 /* EC settings */
3463 if (pjsua_var.media_cfg.ec_tail_len) {
Benny Prijonof798e502009-03-09 13:08:16 +00003464 param->flags |= (PJMEDIA_AUD_DEV_CAP_EC | PJMEDIA_AUD_DEV_CAP_EC_TAIL);
3465 param->ec_enabled = PJ_TRUE;
3466 param->ec_tail_ms = pjsua_var.media_cfg.ec_tail_len;
Benny Prijono10454dc2009-02-21 14:21:59 +00003467 } else {
Benny Prijonof798e502009-03-09 13:08:16 +00003468 param->flags &= ~(PJMEDIA_AUD_DEV_CAP_EC|PJMEDIA_AUD_DEV_CAP_EC_TAIL);
Benny Prijono10454dc2009-02-21 14:21:59 +00003469 }
3470
Benny Prijonof798e502009-03-09 13:08:16 +00003471 return PJ_SUCCESS;
3472}
Benny Prijono26056d82006-10-11 16:03:41 +00003473
Benny Prijonof798e502009-03-09 13:08:16 +00003474/* Internal: the first time the audio device is opened (during app
3475 * startup), retrieve the audio settings such as volume level
3476 * so that aud_get_settings() will work.
3477 */
3478static pj_status_t update_initial_aud_param()
3479{
3480 pjmedia_aud_stream *strm;
3481 pjmedia_aud_param param;
3482 pj_status_t status;
Benny Prijono26056d82006-10-11 16:03:41 +00003483
Benny Prijonof798e502009-03-09 13:08:16 +00003484 PJ_ASSERT_RETURN(pjsua_var.snd_port != NULL, PJ_EBUG);
Nanang Izzuddin3c1ae632008-08-21 15:04:20 +00003485
Benny Prijonof798e502009-03-09 13:08:16 +00003486 strm = pjmedia_snd_port_get_snd_stream(pjsua_var.snd_port);
Benny Prijono26056d82006-10-11 16:03:41 +00003487
Benny Prijonof798e502009-03-09 13:08:16 +00003488 status = pjmedia_aud_stream_get_param(strm, &param);
Benny Prijonoeebe9af2006-06-13 22:57:13 +00003489 if (status != PJ_SUCCESS) {
Benny Prijonof798e502009-03-09 13:08:16 +00003490 pjsua_perror(THIS_FILE, "Error audio stream "
3491 "device parameters", status);
Benny Prijonoeebe9af2006-06-13 22:57:13 +00003492 return status;
3493 }
3494
Benny Prijonof798e502009-03-09 13:08:16 +00003495#define update_saved_param(cap, field) \
3496 if (param.flags & cap) { \
3497 pjsua_var.aud_param.flags |= cap; \
3498 pjsua_var.aud_param.field = param.field; \
3499 }
3500
3501 update_saved_param(PJMEDIA_AUD_DEV_CAP_INPUT_VOLUME_SETTING, input_vol);
3502 update_saved_param(PJMEDIA_AUD_DEV_CAP_OUTPUT_VOLUME_SETTING, output_vol);
3503 update_saved_param(PJMEDIA_AUD_DEV_CAP_INPUT_ROUTE, input_route);
3504 update_saved_param(PJMEDIA_AUD_DEV_CAP_OUTPUT_ROUTE, output_route);
3505#undef update_saved_param
3506
3507 return PJ_SUCCESS;
3508}
3509
3510/* Get format name */
3511static const char *get_fmt_name(pj_uint32_t id)
3512{
3513 static char name[8];
3514
3515 if (id == PJMEDIA_FORMAT_L16)
3516 return "PCM";
3517 pj_memcpy(name, &id, 4);
3518 name[4] = '\0';
3519 return name;
3520}
3521
3522/* Open sound device with the setting. */
Sauw Ming98766c72011-03-11 06:57:24 +00003523static pj_status_t open_snd_dev(pjmedia_snd_port_param *param)
Benny Prijonof798e502009-03-09 13:08:16 +00003524{
3525 pjmedia_port *conf_port;
3526 pj_status_t status;
3527
3528 PJ_ASSERT_RETURN(param, PJ_EINVAL);
3529
3530 /* Check if NULL sound device is used */
Sauw Ming98766c72011-03-11 06:57:24 +00003531 if (NULL_SND_DEV_ID==param->base.rec_id ||
3532 NULL_SND_DEV_ID==param->base.play_id)
3533 {
Benny Prijonof798e502009-03-09 13:08:16 +00003534 return pjsua_set_null_snd_dev();
3535 }
3536
3537 /* Close existing sound port */
3538 close_snd_dev();
3539
Benny Prijono2d647722011-07-13 03:05:22 +00003540 /* Notify app */
3541 if (pjsua_var.ua_cfg.cb.on_snd_dev_operation) {
3542 (*pjsua_var.ua_cfg.cb.on_snd_dev_operation)(1);
3543 }
3544
Benny Prijonof798e502009-03-09 13:08:16 +00003545 /* Create memory pool for sound device. */
3546 pjsua_var.snd_pool = pjsua_pool_create("pjsua_snd", 4000, 4000);
3547 PJ_ASSERT_RETURN(pjsua_var.snd_pool, PJ_ENOMEM);
3548
3549
3550 PJ_LOG(4,(THIS_FILE, "Opening sound device %s@%d/%d/%dms",
Sauw Ming98766c72011-03-11 06:57:24 +00003551 get_fmt_name(param->base.ext_fmt.id),
3552 param->base.clock_rate, param->base.channel_count,
3553 param->base.samples_per_frame / param->base.channel_count *
3554 1000 / param->base.clock_rate));
Benny Prijonob90fd382011-09-18 14:59:56 +00003555 pj_log_push_indent();
Benny Prijonof798e502009-03-09 13:08:16 +00003556
3557 status = pjmedia_snd_port_create2( pjsua_var.snd_pool,
Sauw Ming98766c72011-03-11 06:57:24 +00003558 param, &pjsua_var.snd_port);
Benny Prijonof798e502009-03-09 13:08:16 +00003559 if (status != PJ_SUCCESS)
Benny Prijonob90fd382011-09-18 14:59:56 +00003560 goto on_error;
Benny Prijonof798e502009-03-09 13:08:16 +00003561
3562 /* Get the port0 of the conference bridge. */
3563 conf_port = pjmedia_conf_get_master_port(pjsua_var.mconf);
3564 pj_assert(conf_port != NULL);
3565
3566 /* For conference bridge, resample if necessary if the bridge's
3567 * clock rate is different than the sound device's clock rate.
3568 */
3569 if (!pjsua_var.is_mswitch &&
Sauw Ming98766c72011-03-11 06:57:24 +00003570 param->base.ext_fmt.id == PJMEDIA_FORMAT_PCM &&
Nanang Izzuddinfe68f1d2011-07-19 03:42:28 +00003571 PJMEDIA_PIA_SRATE(&conf_port->info) != param->base.clock_rate)
Benny Prijonof798e502009-03-09 13:08:16 +00003572 {
3573 pjmedia_port *resample_port;
3574 unsigned resample_opt = 0;
3575
3576 if (pjsua_var.media_cfg.quality >= 3 &&
3577 pjsua_var.media_cfg.quality <= 4)
3578 {
3579 resample_opt |= PJMEDIA_RESAMPLE_USE_SMALL_FILTER;
3580 }
3581 else if (pjsua_var.media_cfg.quality < 3) {
3582 resample_opt |= PJMEDIA_RESAMPLE_USE_LINEAR;
3583 }
3584
3585 status = pjmedia_resample_port_create(pjsua_var.snd_pool,
3586 conf_port,
Sauw Ming98766c72011-03-11 06:57:24 +00003587 param->base.clock_rate,
Benny Prijonof798e502009-03-09 13:08:16 +00003588 resample_opt,
3589 &resample_port);
3590 if (status != PJ_SUCCESS) {
3591 char errmsg[PJ_ERR_MSG_SIZE];
3592 pj_strerror(status, errmsg, sizeof(errmsg));
3593 PJ_LOG(4, (THIS_FILE,
3594 "Error creating resample port: %s",
3595 errmsg));
3596 close_snd_dev();
Benny Prijonob90fd382011-09-18 14:59:56 +00003597 goto on_error;
Benny Prijonof798e502009-03-09 13:08:16 +00003598 }
3599
3600 conf_port = resample_port;
3601 }
3602
3603 /* Otherwise for audio switchboard, the switch's port0 setting is
3604 * derived from the sound device setting, so update the setting.
3605 */
3606 if (pjsua_var.is_mswitch) {
Nanang Izzuddinfe68f1d2011-07-19 03:42:28 +00003607 pj_memcpy(&conf_port->info.fmt, &param->base.ext_fmt,
Benny Prijonoc45d9512010-12-10 11:04:30 +00003608 sizeof(conf_port->info.fmt));
Nanang Izzuddinfe68f1d2011-07-19 03:42:28 +00003609 conf_port->info.fmt.det.aud.clock_rate = param->base.clock_rate;
3610 conf_port->info.fmt.det.aud.frame_time_usec = param->base.samples_per_frame*
Benny Prijonoc45d9512010-12-10 11:04:30 +00003611 1000000 /
Nanang Izzuddinfe68f1d2011-07-19 03:42:28 +00003612 param->base.clock_rate;
3613 conf_port->info.fmt.det.aud.channel_count = param->base.channel_count;
Benny Prijonoc45d9512010-12-10 11:04:30 +00003614 conf_port->info.fmt.det.aud.bits_per_sample = 16;
Benny Prijonof798e502009-03-09 13:08:16 +00003615 }
3616
Benny Prijonoc45d9512010-12-10 11:04:30 +00003617
Benny Prijonof798e502009-03-09 13:08:16 +00003618 /* Connect sound port to the bridge */
Benny Prijono52a93912006-08-04 20:54:37 +00003619 status = pjmedia_snd_port_connect(pjsua_var.snd_port,
3620 conf_port );
3621 if (status != PJ_SUCCESS) {
3622 pjsua_perror(THIS_FILE, "Unable to connect conference port to "
3623 "sound device", status);
3624 pjmedia_snd_port_destroy(pjsua_var.snd_port);
3625 pjsua_var.snd_port = NULL;
Benny Prijonob90fd382011-09-18 14:59:56 +00003626 goto on_error;
Benny Prijono52a93912006-08-04 20:54:37 +00003627 }
3628
Benny Prijonoeebe9af2006-06-13 22:57:13 +00003629 /* Save the device IDs */
Sauw Ming98766c72011-03-11 06:57:24 +00003630 pjsua_var.cap_dev = param->base.rec_id;
3631 pjsua_var.play_dev = param->base.play_id;
Benny Prijonoeebe9af2006-06-13 22:57:13 +00003632
Benny Prijonoc53c6d72006-11-27 09:54:03 +00003633 /* Update sound device name. */
Benny Prijonof798e502009-03-09 13:08:16 +00003634 {
3635 pjmedia_aud_dev_info rec_info;
3636 pjmedia_aud_stream *strm;
3637 pjmedia_aud_param si;
3638 pj_str_t tmp;
Benny Prijonoc53c6d72006-11-27 09:54:03 +00003639
Benny Prijonof798e502009-03-09 13:08:16 +00003640 strm = pjmedia_snd_port_get_snd_stream(pjsua_var.snd_port);
3641 status = pjmedia_aud_stream_get_param(strm, &si);
3642 if (status == PJ_SUCCESS)
3643 status = pjmedia_aud_dev_get_info(si.rec_id, &rec_info);
Benny Prijonof3758ee2008-02-26 15:32:16 +00003644
Benny Prijonof798e502009-03-09 13:08:16 +00003645 if (status==PJ_SUCCESS) {
Sauw Ming98766c72011-03-11 06:57:24 +00003646 if (param->base.clock_rate != pjsua_var.media_cfg.clock_rate) {
Benny Prijonof798e502009-03-09 13:08:16 +00003647 char tmp_buf[128];
3648 int tmp_buf_len = sizeof(tmp_buf);
3649
3650 tmp_buf_len = pj_ansi_snprintf(tmp_buf, sizeof(tmp_buf)-1,
3651 "%s (%dKHz)",
3652 rec_info.name,
Sauw Ming98766c72011-03-11 06:57:24 +00003653 param->base.clock_rate/1000);
Benny Prijonof798e502009-03-09 13:08:16 +00003654 pj_strset(&tmp, tmp_buf, tmp_buf_len);
3655 pjmedia_conf_set_port0_name(pjsua_var.mconf, &tmp);
3656 } else {
3657 pjmedia_conf_set_port0_name(pjsua_var.mconf,
3658 pj_cstr(&tmp, rec_info.name));
3659 }
3660 }
3661
3662 /* Any error is not major, let it through */
3663 status = PJ_SUCCESS;
Benny Prijonob90fd382011-09-18 14:59:56 +00003664 }
Benny Prijonof798e502009-03-09 13:08:16 +00003665
3666 /* If this is the first time the audio device is open, retrieve some
3667 * settings from the device (such as volume settings) so that the
3668 * pjsua_snd_get_setting() work.
3669 */
3670 if (pjsua_var.aud_open_cnt == 0) {
3671 update_initial_aud_param();
3672 ++pjsua_var.aud_open_cnt;
Benny Prijonof3758ee2008-02-26 15:32:16 +00003673 }
Benny Prijonoc53c6d72006-11-27 09:54:03 +00003674
Benny Prijonob90fd382011-09-18 14:59:56 +00003675 pj_log_pop_indent();
Benny Prijonoeebe9af2006-06-13 22:57:13 +00003676 return PJ_SUCCESS;
Benny Prijonob90fd382011-09-18 14:59:56 +00003677
3678on_error:
3679 pj_log_pop_indent();
3680 return status;
Benny Prijonof798e502009-03-09 13:08:16 +00003681}
Nanang Izzuddin8465c682009-03-04 17:23:25 +00003682
Nanang Izzuddin8465c682009-03-04 17:23:25 +00003683
Benny Prijonof798e502009-03-09 13:08:16 +00003684/* Close existing sound device */
3685static void close_snd_dev(void)
3686{
Benny Prijonob90fd382011-09-18 14:59:56 +00003687 pj_log_push_indent();
3688
Benny Prijono2d647722011-07-13 03:05:22 +00003689 /* Notify app */
3690 if (pjsua_var.snd_is_on && pjsua_var.ua_cfg.cb.on_snd_dev_operation) {
3691 (*pjsua_var.ua_cfg.cb.on_snd_dev_operation)(0);
3692 }
3693
Benny Prijonof798e502009-03-09 13:08:16 +00003694 /* Close sound device */
3695 if (pjsua_var.snd_port) {
3696 pjmedia_aud_dev_info cap_info, play_info;
3697 pjmedia_aud_stream *strm;
3698 pjmedia_aud_param param;
3699
3700 strm = pjmedia_snd_port_get_snd_stream(pjsua_var.snd_port);
3701 pjmedia_aud_stream_get_param(strm, &param);
3702
3703 if (pjmedia_aud_dev_get_info(param.rec_id, &cap_info) != PJ_SUCCESS)
3704 cap_info.name[0] = '\0';
3705 if (pjmedia_aud_dev_get_info(param.play_id, &play_info) != PJ_SUCCESS)
3706 play_info.name[0] = '\0';
3707
3708 PJ_LOG(4,(THIS_FILE, "Closing %s sound playback device and "
3709 "%s sound capture device",
3710 play_info.name, cap_info.name));
3711
3712 pjmedia_snd_port_disconnect(pjsua_var.snd_port);
3713 pjmedia_snd_port_destroy(pjsua_var.snd_port);
3714 pjsua_var.snd_port = NULL;
3715 }
3716
3717 /* Close null sound device */
3718 if (pjsua_var.null_snd) {
3719 PJ_LOG(4,(THIS_FILE, "Closing null sound device.."));
3720 pjmedia_master_port_destroy(pjsua_var.null_snd, PJ_FALSE);
3721 pjsua_var.null_snd = NULL;
3722 }
3723
3724 if (pjsua_var.snd_pool)
3725 pj_pool_release(pjsua_var.snd_pool);
3726 pjsua_var.snd_pool = NULL;
Benny Prijono2d647722011-07-13 03:05:22 +00003727 pjsua_var.snd_is_on = PJ_FALSE;
Benny Prijonob90fd382011-09-18 14:59:56 +00003728
3729 pj_log_pop_indent();
Benny Prijonof798e502009-03-09 13:08:16 +00003730}
3731
3732
3733/*
3734 * Select or change sound device. Application may call this function at
3735 * any time to replace current sound device.
3736 */
3737PJ_DEF(pj_status_t) pjsua_set_snd_dev( int capture_dev,
3738 int playback_dev)
3739{
3740 unsigned alt_cr_cnt = 1;
3741 unsigned alt_cr[] = {0, 44100, 48000, 32000, 16000, 8000};
3742 unsigned i;
3743 pj_status_t status = -1;
3744
Benny Prijonob90fd382011-09-18 14:59:56 +00003745 PJ_LOG(4,(THIS_FILE, "Set sound device: capture=%d, playback=%d",
3746 capture_dev, playback_dev));
3747 pj_log_push_indent();
3748
Benny Prijono23ea21a2009-06-03 12:43:06 +00003749 /* Null-sound */
3750 if (capture_dev==NULL_SND_DEV_ID && playback_dev==NULL_SND_DEV_ID) {
Benny Prijonob90fd382011-09-18 14:59:56 +00003751 status = pjsua_set_null_snd_dev();
3752 pj_log_pop_indent();
3753 return status;
Benny Prijono23ea21a2009-06-03 12:43:06 +00003754 }
3755
Benny Prijonof798e502009-03-09 13:08:16 +00003756 /* Set default clock rate */
3757 alt_cr[0] = pjsua_var.media_cfg.snd_clock_rate;
3758 if (alt_cr[0] == 0)
3759 alt_cr[0] = pjsua_var.media_cfg.clock_rate;
3760
3761 /* Allow retrying of different clock rate if we're using conference
3762 * bridge (meaning audio format is always PCM), otherwise lock on
3763 * to one clock rate.
3764 */
3765 if (pjsua_var.is_mswitch) {
3766 alt_cr_cnt = 1;
3767 } else {
3768 alt_cr_cnt = PJ_ARRAY_SIZE(alt_cr);
3769 }
3770
3771 /* Attempts to open the sound device with different clock rates */
3772 for (i=0; i<alt_cr_cnt; ++i) {
Sauw Ming98766c72011-03-11 06:57:24 +00003773 pjmedia_snd_port_param param;
Benny Prijonof798e502009-03-09 13:08:16 +00003774 unsigned samples_per_frame;
3775
3776 /* Create the default audio param */
3777 samples_per_frame = alt_cr[i] *
3778 pjsua_var.media_cfg.audio_frame_ptime *
3779 pjsua_var.media_cfg.channel_count / 1000;
Sauw Ming98766c72011-03-11 06:57:24 +00003780 status = create_aud_param(&param.base, capture_dev, playback_dev,
Benny Prijonof798e502009-03-09 13:08:16 +00003781 alt_cr[i], pjsua_var.media_cfg.channel_count,
3782 samples_per_frame, 16);
3783 if (status != PJ_SUCCESS)
Benny Prijonob90fd382011-09-18 14:59:56 +00003784 goto on_error;
Benny Prijonof798e502009-03-09 13:08:16 +00003785
3786 /* Open! */
Sauw Ming98766c72011-03-11 06:57:24 +00003787 param.options = 0;
Benny Prijonof798e502009-03-09 13:08:16 +00003788 status = open_snd_dev(&param);
3789 if (status == PJ_SUCCESS)
3790 break;
3791 }
3792
3793 if (status != PJ_SUCCESS) {
3794 pjsua_perror(THIS_FILE, "Unable to open sound device", status);
Benny Prijonob90fd382011-09-18 14:59:56 +00003795 goto on_error;
Benny Prijonof798e502009-03-09 13:08:16 +00003796 }
3797
Benny Prijonoe25fe6f2009-07-16 17:52:08 +00003798 pjsua_var.no_snd = PJ_FALSE;
Benny Prijono2d647722011-07-13 03:05:22 +00003799 pjsua_var.snd_is_on = PJ_TRUE;
Benny Prijonoe25fe6f2009-07-16 17:52:08 +00003800
Benny Prijonob90fd382011-09-18 14:59:56 +00003801 pj_log_pop_indent();
Benny Prijonof798e502009-03-09 13:08:16 +00003802 return PJ_SUCCESS;
Benny Prijonob90fd382011-09-18 14:59:56 +00003803
3804on_error:
3805 pj_log_pop_indent();
3806 return status;
Benny Prijonoeebe9af2006-06-13 22:57:13 +00003807}
3808
3809
3810/*
Benny Prijonoebdf8772007-02-01 19:25:50 +00003811 * Get currently active sound devices. If sound devices has not been created
3812 * (for example when pjsua_start() is not called), it is possible that
3813 * the function returns PJ_SUCCESS with -1 as device IDs.
3814 */
3815PJ_DEF(pj_status_t) pjsua_get_snd_dev(int *capture_dev,
3816 int *playback_dev)
3817{
3818 if (capture_dev) {
3819 *capture_dev = pjsua_var.cap_dev;
3820 }
3821 if (playback_dev) {
3822 *playback_dev = pjsua_var.play_dev;
3823 }
3824
3825 return PJ_SUCCESS;
3826}
3827
3828
3829/*
Benny Prijonoeebe9af2006-06-13 22:57:13 +00003830 * Use null sound device.
3831 */
3832PJ_DEF(pj_status_t) pjsua_set_null_snd_dev(void)
3833{
3834 pjmedia_port *conf_port;
3835 pj_status_t status;
3836
Benny Prijonob90fd382011-09-18 14:59:56 +00003837 PJ_LOG(4,(THIS_FILE, "Setting null sound device.."));
3838 pj_log_push_indent();
3839
3840
Benny Prijonoeebe9af2006-06-13 22:57:13 +00003841 /* Close existing sound device */
3842 close_snd_dev();
3843
Benny Prijono2d647722011-07-13 03:05:22 +00003844 /* Notify app */
3845 if (pjsua_var.ua_cfg.cb.on_snd_dev_operation) {
3846 (*pjsua_var.ua_cfg.cb.on_snd_dev_operation)(1);
3847 }
3848
Nanang Izzuddin68559c32008-06-13 17:01:46 +00003849 /* Create memory pool for sound device. */
3850 pjsua_var.snd_pool = pjsua_pool_create("pjsua_snd", 4000, 4000);
3851 PJ_ASSERT_RETURN(pjsua_var.snd_pool, PJ_ENOMEM);
3852
Benny Prijonoeebe9af2006-06-13 22:57:13 +00003853 PJ_LOG(4,(THIS_FILE, "Opening null sound device.."));
3854
3855 /* Get the port0 of the conference bridge. */
3856 conf_port = pjmedia_conf_get_master_port(pjsua_var.mconf);
3857 pj_assert(conf_port != NULL);
3858
3859 /* Create master port, connecting port0 of the conference bridge to
3860 * a null port.
3861 */
Nanang Izzuddin68559c32008-06-13 17:01:46 +00003862 status = pjmedia_master_port_create(pjsua_var.snd_pool, pjsua_var.null_port,
Benny Prijonoeebe9af2006-06-13 22:57:13 +00003863 conf_port, 0, &pjsua_var.null_snd);
3864 if (status != PJ_SUCCESS) {
3865 pjsua_perror(THIS_FILE, "Unable to create null sound device",
3866 status);
Benny Prijonob90fd382011-09-18 14:59:56 +00003867 pj_log_pop_indent();
Benny Prijonoeebe9af2006-06-13 22:57:13 +00003868 return status;
3869 }
3870
3871 /* Start the master port */
3872 status = pjmedia_master_port_start(pjsua_var.null_snd);
3873 PJ_ASSERT_RETURN(status == PJ_SUCCESS, status);
3874
Nanang Izzuddin68559c32008-06-13 17:01:46 +00003875 pjsua_var.cap_dev = NULL_SND_DEV_ID;
3876 pjsua_var.play_dev = NULL_SND_DEV_ID;
3877
Benny Prijonoe25fe6f2009-07-16 17:52:08 +00003878 pjsua_var.no_snd = PJ_FALSE;
Benny Prijono2d647722011-07-13 03:05:22 +00003879 pjsua_var.snd_is_on = PJ_TRUE;
Benny Prijonoe25fe6f2009-07-16 17:52:08 +00003880
Benny Prijonob90fd382011-09-18 14:59:56 +00003881 pj_log_pop_indent();
Benny Prijonoeebe9af2006-06-13 22:57:13 +00003882 return PJ_SUCCESS;
3883}
3884
3885
Benny Prijonoe909eac2006-07-27 22:04:56 +00003886
3887/*
3888 * Use no device!
3889 */
3890PJ_DEF(pjmedia_port*) pjsua_set_no_snd_dev(void)
3891{
3892 /* Close existing sound device */
3893 close_snd_dev();
3894
3895 pjsua_var.no_snd = PJ_TRUE;
3896 return pjmedia_conf_get_master_port(pjsua_var.mconf);
3897}
3898
3899
Benny Prijonof20687a2006-08-04 18:27:19 +00003900/*
3901 * Configure the AEC settings of the sound port.
3902 */
Benny Prijono5da50432006-08-07 10:24:52 +00003903PJ_DEF(pj_status_t) pjsua_set_ec(unsigned tail_ms, unsigned options)
Benny Prijonof20687a2006-08-04 18:27:19 +00003904{
3905 pjsua_var.media_cfg.ec_tail_len = tail_ms;
3906
3907 if (pjsua_var.snd_port)
Benny Prijono5da50432006-08-07 10:24:52 +00003908 return pjmedia_snd_port_set_ec( pjsua_var.snd_port, pjsua_var.pool,
3909 tail_ms, options);
Benny Prijonof20687a2006-08-04 18:27:19 +00003910
3911 return PJ_SUCCESS;
3912}
3913
3914
3915/*
3916 * Get current AEC tail length.
3917 */
Benny Prijono22dfe592006-08-06 12:07:13 +00003918PJ_DEF(pj_status_t) pjsua_get_ec_tail(unsigned *p_tail_ms)
Benny Prijonof20687a2006-08-04 18:27:19 +00003919{
3920 *p_tail_ms = pjsua_var.media_cfg.ec_tail_len;
3921 return PJ_SUCCESS;
3922}
3923
Nanang Izzuddinfe02a062009-02-18 14:28:49 +00003924
Nanang Izzuddin0cb3b022009-02-27 17:37:35 +00003925/*
Benny Prijonof798e502009-03-09 13:08:16 +00003926 * Check whether the sound device is currently active.
Nanang Izzuddin0cb3b022009-02-27 17:37:35 +00003927 */
Benny Prijonof798e502009-03-09 13:08:16 +00003928PJ_DEF(pj_bool_t) pjsua_snd_is_active(void)
Nanang Izzuddin0cb3b022009-02-27 17:37:35 +00003929{
Benny Prijonof798e502009-03-09 13:08:16 +00003930 return pjsua_var.snd_port != NULL;
3931}
Nanang Izzuddin0cb3b022009-02-27 17:37:35 +00003932
Nanang Izzuddin0cb3b022009-02-27 17:37:35 +00003933
Benny Prijonof798e502009-03-09 13:08:16 +00003934/*
3935 * Configure sound device setting to the sound device being used.
3936 */
3937PJ_DEF(pj_status_t) pjsua_snd_set_setting( pjmedia_aud_dev_cap cap,
3938 const void *pval,
3939 pj_bool_t keep)
3940{
Benny Prijono09b0ff62009-03-10 12:07:51 +00003941 pj_status_t status;
3942
Benny Prijonof798e502009-03-09 13:08:16 +00003943 /* Check if we are allowed to set the cap */
Benny Prijono09b0ff62009-03-10 12:07:51 +00003944 if ((cap & pjsua_var.aud_svmask) == 0) {
Benny Prijonof798e502009-03-09 13:08:16 +00003945 return PJMEDIA_EAUD_INVCAP;
3946 }
3947
Benny Prijono09b0ff62009-03-10 12:07:51 +00003948 /* If sound is active, set it immediately */
Benny Prijonof798e502009-03-09 13:08:16 +00003949 if (pjsua_snd_is_active()) {
Benny Prijonof798e502009-03-09 13:08:16 +00003950 pjmedia_aud_stream *strm;
3951
3952 strm = pjmedia_snd_port_get_snd_stream(pjsua_var.snd_port);
Benny Prijono09b0ff62009-03-10 12:07:51 +00003953 status = pjmedia_aud_stream_set_cap(strm, cap, pval);
Benny Prijonof798e502009-03-09 13:08:16 +00003954 } else {
Benny Prijono09b0ff62009-03-10 12:07:51 +00003955 status = PJ_SUCCESS;
Benny Prijonof798e502009-03-09 13:08:16 +00003956 }
Benny Prijono09b0ff62009-03-10 12:07:51 +00003957
3958 if (status != PJ_SUCCESS)
3959 return status;
3960
3961 /* Save in internal param for later device open */
3962 if (keep) {
3963 status = pjmedia_aud_param_set_cap(&pjsua_var.aud_param,
3964 cap, pval);
3965 }
3966
3967 return status;
Benny Prijonof798e502009-03-09 13:08:16 +00003968}
3969
3970/*
3971 * Retrieve a sound device setting.
3972 */
3973PJ_DEF(pj_status_t) pjsua_snd_get_setting( pjmedia_aud_dev_cap cap,
3974 void *pval)
3975{
3976 /* If sound device has never been opened before, open it to
3977 * retrieve the initial setting from the device (e.g. audio
3978 * volume)
3979 */
Benny Prijono09b0ff62009-03-10 12:07:51 +00003980 if (pjsua_var.aud_open_cnt==0) {
3981 PJ_LOG(4,(THIS_FILE, "Opening sound device to get initial settings"));
Benny Prijonof798e502009-03-09 13:08:16 +00003982 pjsua_set_snd_dev(pjsua_var.cap_dev, pjsua_var.play_dev);
Benny Prijono09b0ff62009-03-10 12:07:51 +00003983 close_snd_dev();
3984 }
Benny Prijonof798e502009-03-09 13:08:16 +00003985
3986 if (pjsua_snd_is_active()) {
3987 /* Sound is active, retrieve from device directly */
3988 pjmedia_aud_stream *strm;
3989
3990 strm = pjmedia_snd_port_get_snd_stream(pjsua_var.snd_port);
3991 return pjmedia_aud_stream_get_cap(strm, cap, pval);
3992 } else {
3993 /* Otherwise retrieve from internal param */
3994 return pjmedia_aud_param_get_cap(&pjsua_var.aud_param,
3995 cap, pval);
3996 }
Nanang Izzuddin0cb3b022009-02-27 17:37:35 +00003997}
3998
3999
Benny Prijonoeebe9af2006-06-13 22:57:13 +00004000/*****************************************************************************
4001 * Codecs.
4002 */
4003
4004/*
4005 * Enum all supported codecs in the system.
4006 */
4007PJ_DEF(pj_status_t) pjsua_enum_codecs( pjsua_codec_info id[],
4008 unsigned *p_count )
4009{
4010 pjmedia_codec_mgr *codec_mgr;
4011 pjmedia_codec_info info[32];
4012 unsigned i, count, prio[32];
4013 pj_status_t status;
4014
4015 codec_mgr = pjmedia_endpt_get_codec_mgr(pjsua_var.med_endpt);
4016 count = PJ_ARRAY_SIZE(info);
4017 status = pjmedia_codec_mgr_enum_codecs( codec_mgr, &count, info, prio);
4018 if (status != PJ_SUCCESS) {
4019 *p_count = 0;
4020 return status;
4021 }
4022
4023 if (count > *p_count) count = *p_count;
4024
4025 for (i=0; i<count; ++i) {
Nanang Izzuddin56b2ce42011-04-06 13:55:01 +00004026 pj_bzero(&id[i], sizeof(pjsua_codec_info));
4027
Benny Prijonoeebe9af2006-06-13 22:57:13 +00004028 pjmedia_codec_info_to_id(&info[i], id[i].buf_, sizeof(id[i].buf_));
4029 id[i].codec_id = pj_str(id[i].buf_);
4030 id[i].priority = (pj_uint8_t) prio[i];
4031 }
4032
4033 *p_count = count;
4034
4035 return PJ_SUCCESS;
4036}
4037
4038
4039/*
4040 * Change codec priority.
4041 */
4042PJ_DEF(pj_status_t) pjsua_codec_set_priority( const pj_str_t *codec_id,
4043 pj_uint8_t priority )
4044{
Benny Prijono88accae2008-06-26 15:48:14 +00004045 const pj_str_t all = { NULL, 0 };
Benny Prijonoeebe9af2006-06-13 22:57:13 +00004046 pjmedia_codec_mgr *codec_mgr;
4047
4048 codec_mgr = pjmedia_endpt_get_codec_mgr(pjsua_var.med_endpt);
4049
Benny Prijono88accae2008-06-26 15:48:14 +00004050 if (codec_id->slen==1 && *codec_id->ptr=='*')
4051 codec_id = &all;
4052
Benny Prijonoeebe9af2006-06-13 22:57:13 +00004053 return pjmedia_codec_mgr_set_codec_priority(codec_mgr, codec_id,
4054 priority);
4055}
4056
4057
4058/*
4059 * Get codec parameters.
4060 */
4061PJ_DEF(pj_status_t) pjsua_codec_get_param( const pj_str_t *codec_id,
4062 pjmedia_codec_param *param )
4063{
Benny Prijono88accae2008-06-26 15:48:14 +00004064 const pj_str_t all = { NULL, 0 };
Benny Prijonoeebe9af2006-06-13 22:57:13 +00004065 const pjmedia_codec_info *info;
4066 pjmedia_codec_mgr *codec_mgr;
4067 unsigned count = 1;
4068 pj_status_t status;
4069
4070 codec_mgr = pjmedia_endpt_get_codec_mgr(pjsua_var.med_endpt);
4071
Benny Prijono88accae2008-06-26 15:48:14 +00004072 if (codec_id->slen==1 && *codec_id->ptr=='*')
4073 codec_id = &all;
4074
Benny Prijonoeebe9af2006-06-13 22:57:13 +00004075 status = pjmedia_codec_mgr_find_codecs_by_id(codec_mgr, codec_id,
4076 &count, &info, NULL);
4077 if (status != PJ_SUCCESS)
4078 return status;
4079
4080 if (count != 1)
Nanang Izzuddin50fae732011-03-22 09:49:23 +00004081 return (count > 1? PJ_ETOOMANY : PJ_ENOTFOUND);
Benny Prijonoeebe9af2006-06-13 22:57:13 +00004082
4083 status = pjmedia_codec_mgr_get_default_param( codec_mgr, info, param);
4084 return status;
4085}
4086
4087
4088/*
4089 * Set codec parameters.
4090 */
Nanang Izzuddin06839e72010-01-27 11:48:31 +00004091PJ_DEF(pj_status_t) pjsua_codec_set_param( const pj_str_t *codec_id,
Benny Prijonoeebe9af2006-06-13 22:57:13 +00004092 const pjmedia_codec_param *param)
4093{
Nanang Izzuddin06839e72010-01-27 11:48:31 +00004094 const pjmedia_codec_info *info[2];
4095 pjmedia_codec_mgr *codec_mgr;
4096 unsigned count = 2;
4097 pj_status_t status;
4098
4099 codec_mgr = pjmedia_endpt_get_codec_mgr(pjsua_var.med_endpt);
4100
4101 status = pjmedia_codec_mgr_find_codecs_by_id(codec_mgr, codec_id,
4102 &count, info, NULL);
4103 if (status != PJ_SUCCESS)
4104 return status;
4105
4106 /* Codec ID should be specific, except for G.722.1 */
4107 if (count > 1 &&
4108 pj_strnicmp2(codec_id, "G7221/16", 8) != 0 &&
4109 pj_strnicmp2(codec_id, "G7221/32", 8) != 0)
4110 {
4111 pj_assert(!"Codec ID is not specific");
4112 return PJ_ETOOMANY;
4113 }
4114
4115 status = pjmedia_codec_mgr_set_default_param(codec_mgr, info[0], param);
4116 return status;
Benny Prijonoeebe9af2006-06-13 22:57:13 +00004117}
Nanang Izzuddin50fae732011-03-22 09:49:23 +00004118
4119