blob: fd8163a0ee53d59ffbc352a6e64418eb9036fb41 [file] [log] [blame]
Benny Prijonoeebe9af2006-06-13 22:57:13 +00001/* $Id$ */
2/*
Benny Prijono844653c2008-12-23 17:27:53 +00003 * Copyright (C) 2008-2009 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 */
39static pj_status_t open_snd_dev(pjmedia_aud_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
Benny Prijonoeebe9af2006-06-13 22:57:13 +000061/**
62 * Init media subsystems.
63 */
64pj_status_t pjsua_media_subsys_init(const pjsua_media_config *cfg)
65{
Benny Prijonoba5926a2007-05-02 11:29:37 +000066 pj_str_t codec_id = {NULL, 0};
Benny Prijono0498d902006-06-19 14:49:14 +000067 unsigned opt;
Benny Prijonoeebe9af2006-06-13 22:57:13 +000068 pj_status_t status;
69
Benny Prijonofc24e692007-01-27 18:31:51 +000070 /* To suppress warning about unused var when all codecs are disabled */
71 PJ_UNUSED_ARG(codec_id);
72
Benny Prijonof798e502009-03-09 13:08:16 +000073 /* Specify which audio device settings are save-able */
74 pjsua_var.aud_svmask = 0xFFFFFFFF;
75 /* These are not-settable */
76 pjsua_var.aud_svmask &= ~(PJMEDIA_AUD_DEV_CAP_EXT_FORMAT |
77 PJMEDIA_AUD_DEV_CAP_INPUT_SIGNAL_METER |
78 PJMEDIA_AUD_DEV_CAP_OUTPUT_SIGNAL_METER);
Benny Prijonoe506c8c2009-03-10 13:28:43 +000079 /* EC settings use different API */
80 pjsua_var.aud_svmask &= ~(PJMEDIA_AUD_DEV_CAP_EC |
81 PJMEDIA_AUD_DEV_CAP_EC_TAIL);
Benny Prijonof798e502009-03-09 13:08:16 +000082
Benny Prijonoeebe9af2006-06-13 22:57:13 +000083 /* Copy configuration */
Benny Prijonof76e1392008-06-06 14:51:48 +000084 pjsua_media_config_dup(pjsua_var.pool, &pjsua_var.media_cfg, cfg);
Benny Prijonoeebe9af2006-06-13 22:57:13 +000085
86 /* Normalize configuration */
Benny Prijono50f19b32008-03-11 13:15:43 +000087 if (pjsua_var.media_cfg.snd_clock_rate == 0) {
88 pjsua_var.media_cfg.snd_clock_rate = pjsua_var.media_cfg.clock_rate;
89 }
Benny Prijonoeebe9af2006-06-13 22:57:13 +000090
91 if (pjsua_var.media_cfg.has_ioqueue &&
92 pjsua_var.media_cfg.thread_cnt == 0)
93 {
94 pjsua_var.media_cfg.thread_cnt = 1;
95 }
96
97 if (pjsua_var.media_cfg.max_media_ports < pjsua_var.ua_cfg.max_calls) {
98 pjsua_var.media_cfg.max_media_ports = pjsua_var.ua_cfg.max_calls + 2;
99 }
100
101 /* Create media endpoint. */
102 status = pjmedia_endpt_create(&pjsua_var.cp.factory,
103 pjsua_var.media_cfg.has_ioqueue? NULL :
104 pjsip_endpt_get_ioqueue(pjsua_var.endpt),
105 pjsua_var.media_cfg.thread_cnt,
106 &pjsua_var.med_endpt);
107 if (status != PJ_SUCCESS) {
108 pjsua_perror(THIS_FILE,
109 "Media stack initialization has returned error",
110 status);
111 return status;
112 }
113
114 /* Register all codecs */
Nanang Izzuddin493a8db2008-08-15 13:17:39 +0000115
Benny Prijonoeebe9af2006-06-13 22:57:13 +0000116#if PJMEDIA_HAS_SPEEX_CODEC
117 /* Register speex. */
Nanang Izzuddin9dbad152008-06-10 18:56:10 +0000118 status = pjmedia_codec_speex_init(pjsua_var.med_endpt,
119 0,
120 pjsua_var.media_cfg.quality,
121 -1);
Benny Prijonoeebe9af2006-06-13 22:57:13 +0000122 if (status != PJ_SUCCESS) {
123 pjsua_perror(THIS_FILE, "Error initializing Speex codec",
124 status);
125 return status;
126 }
Benny Prijono7ca96da2006-08-07 12:11:40 +0000127
128 /* Set speex/16000 to higher priority*/
129 codec_id = pj_str("speex/16000");
130 pjmedia_codec_mgr_set_codec_priority(
131 pjmedia_endpt_get_codec_mgr(pjsua_var.med_endpt),
132 &codec_id, PJMEDIA_CODEC_PRIO_NORMAL+2);
133
134 /* Set speex/8000 to next higher priority*/
135 codec_id = pj_str("speex/8000");
136 pjmedia_codec_mgr_set_codec_priority(
137 pjmedia_endpt_get_codec_mgr(pjsua_var.med_endpt),
138 &codec_id, PJMEDIA_CODEC_PRIO_NORMAL+1);
139
140
141
Benny Prijonoeebe9af2006-06-13 22:57:13 +0000142#endif /* PJMEDIA_HAS_SPEEX_CODEC */
143
Benny Prijono00cae612006-07-31 15:19:36 +0000144#if PJMEDIA_HAS_ILBC_CODEC
145 /* Register iLBC. */
146 status = pjmedia_codec_ilbc_init( pjsua_var.med_endpt,
147 pjsua_var.media_cfg.ilbc_mode);
148 if (status != PJ_SUCCESS) {
149 pjsua_perror(THIS_FILE, "Error initializing iLBC codec",
150 status);
151 return status;
152 }
153#endif /* PJMEDIA_HAS_ILBC_CODEC */
154
Benny Prijonoeebe9af2006-06-13 22:57:13 +0000155#if PJMEDIA_HAS_GSM_CODEC
156 /* Register GSM */
157 status = pjmedia_codec_gsm_init(pjsua_var.med_endpt);
158 if (status != PJ_SUCCESS) {
159 pjsua_perror(THIS_FILE, "Error initializing GSM codec",
160 status);
161 return status;
162 }
163#endif /* PJMEDIA_HAS_GSM_CODEC */
164
165#if PJMEDIA_HAS_G711_CODEC
166 /* Register PCMA and PCMU */
167 status = pjmedia_codec_g711_init(pjsua_var.med_endpt);
168 if (status != PJ_SUCCESS) {
169 pjsua_perror(THIS_FILE, "Error initializing G711 codec",
170 status);
171 return status;
172 }
173#endif /* PJMEDIA_HAS_G711_CODEC */
174
Benny Prijono7ffd7752008-03-17 14:07:53 +0000175#if PJMEDIA_HAS_G722_CODEC
176 status = pjmedia_codec_g722_init( pjsua_var.med_endpt );
177 if (status != PJ_SUCCESS) {
178 pjsua_perror(THIS_FILE, "Error initializing G722 codec",
179 status);
180 return status;
181 }
182#endif /* PJMEDIA_HAS_G722_CODEC */
183
Benny Prijonoa4e7cdd2008-08-19 15:01:48 +0000184#if PJMEDIA_HAS_INTEL_IPP
Nanang Izzuddin493a8db2008-08-15 13:17:39 +0000185 /* Register IPP codecs */
186 status = pjmedia_codec_ipp_init(pjsua_var.med_endpt);
187 if (status != PJ_SUCCESS) {
188 pjsua_perror(THIS_FILE, "Error initializing IPP codecs",
189 status);
190 return status;
191 }
192
Benny Prijonoa4e7cdd2008-08-19 15:01:48 +0000193#endif /* PJMEDIA_HAS_INTEL_IPP */
Nanang Izzuddin493a8db2008-08-15 13:17:39 +0000194
Nanang Izzuddin81db8c72009-02-05 10:59:14 +0000195#if PJMEDIA_HAS_PASSTHROUGH_CODECS
196 /* Register passthrough codecs */
Nanang Izzuddinabf58db2009-06-30 15:02:06 +0000197 {
198 unsigned aud_idx;
199 unsigned ext_fmt_cnt = 0;
200 pjmedia_format ext_fmts[32];
201 pjmedia_codec_passthrough_setting setting;
202
203 /* List extended formats supported by audio devices */
204 for (aud_idx = 0; aud_idx < pjmedia_aud_dev_count(); ++aud_idx) {
205 pjmedia_aud_dev_info aud_info;
206 unsigned i;
207
208 status = pjmedia_aud_dev_get_info(aud_idx, &aud_info);
209 if (status != PJ_SUCCESS) {
210 pjsua_perror(THIS_FILE, "Error querying audio device info",
211 status);
212 return status;
213 }
214
215 /* Collect extended formats supported by this audio device */
216 for (i = 0; i < aud_info.ext_fmt_cnt; ++i) {
217 unsigned j;
218 pj_bool_t is_listed = PJ_FALSE;
219
220 /* See if this extended format is already in the list */
221 for (j = 0; j < ext_fmt_cnt && !is_listed; ++j) {
222 if (ext_fmts[j].id == aud_info.ext_fmt[i].id &&
223 ext_fmts[j].bitrate == aud_info.ext_fmt[i].bitrate)
224 {
225 is_listed = PJ_TRUE;
226 }
227 }
228
229 /* Put this format into the list, if it is not in the list */
230 if (!is_listed)
231 ext_fmts[ext_fmt_cnt++] = aud_info.ext_fmt[i];
232
233 pj_assert(ext_fmt_cnt <= PJ_ARRAY_SIZE(ext_fmts));
234 }
235 }
236
237 /* Init the passthrough codec with supported formats only */
238 setting.fmt_cnt = ext_fmt_cnt;
239 setting.fmts = ext_fmts;
Nanang Izzuddin873f3e42009-07-15 17:55:16 +0000240 setting.ilbc_mode = cfg->ilbc_mode;
Nanang Izzuddinabf58db2009-06-30 15:02:06 +0000241 status = pjmedia_codec_passthrough_init2(pjsua_var.med_endpt, &setting);
242 if (status != PJ_SUCCESS) {
243 pjsua_perror(THIS_FILE, "Error initializing passthrough codecs",
244 status);
245 return status;
246 }
Nanang Izzuddin81db8c72009-02-05 10:59:14 +0000247 }
248#endif /* PJMEDIA_HAS_PASSTHROUGH_CODECS */
249
Nanang Izzuddin57b88572009-04-01 12:05:34 +0000250#if PJMEDIA_HAS_G7221_CODEC
251 /* Register G722.1 codecs */
252 status = pjmedia_codec_g7221_init(pjsua_var.med_endpt);
253 if (status != PJ_SUCCESS) {
254 pjsua_perror(THIS_FILE, "Error initializing G722.1 codec",
255 status);
256 return status;
257 }
258#endif /* PJMEDIA_HAS_G7221_CODEC */
259
Benny Prijonoeebe9af2006-06-13 22:57:13 +0000260#if PJMEDIA_HAS_L16_CODEC
261 /* Register L16 family codecs, but disable all */
262 status = pjmedia_codec_l16_init(pjsua_var.med_endpt, 0);
263 if (status != PJ_SUCCESS) {
264 pjsua_perror(THIS_FILE, "Error initializing L16 codecs",
265 status);
266 return status;
267 }
268
269 /* Disable ALL L16 codecs */
270 codec_id = pj_str("L16");
271 pjmedia_codec_mgr_set_codec_priority(
272 pjmedia_endpt_get_codec_mgr(pjsua_var.med_endpt),
273 &codec_id, PJMEDIA_CODEC_PRIO_DISABLED);
274
275#endif /* PJMEDIA_HAS_L16_CODEC */
276
277
278 /* Save additional conference bridge parameters for future
279 * reference.
280 */
Benny Prijono7d60d052008-03-29 12:24:20 +0000281 pjsua_var.mconf_cfg.channel_count = pjsua_var.media_cfg.channel_count;
Benny Prijonoeebe9af2006-06-13 22:57:13 +0000282 pjsua_var.mconf_cfg.bits_per_sample = 16;
Benny Prijono6e7c5ad2008-03-13 10:15:16 +0000283 pjsua_var.mconf_cfg.samples_per_frame = pjsua_var.media_cfg.clock_rate *
284 pjsua_var.mconf_cfg.channel_count *
285 pjsua_var.media_cfg.audio_frame_ptime /
286 1000;
Benny Prijonoeebe9af2006-06-13 22:57:13 +0000287
Benny Prijono0498d902006-06-19 14:49:14 +0000288 /* Init options for conference bridge. */
289 opt = PJMEDIA_CONF_NO_DEVICE;
290 if (pjsua_var.media_cfg.quality >= 3 &&
Benny Prijono7ca96da2006-08-07 12:11:40 +0000291 pjsua_var.media_cfg.quality <= 4)
Benny Prijono0498d902006-06-19 14:49:14 +0000292 {
293 opt |= PJMEDIA_CONF_SMALL_FILTER;
294 }
295 else if (pjsua_var.media_cfg.quality < 3) {
296 opt |= PJMEDIA_CONF_USE_LINEAR;
297 }
298
299
Benny Prijonoeebe9af2006-06-13 22:57:13 +0000300 /* Init conference bridge. */
301 status = pjmedia_conf_create(pjsua_var.pool,
302 pjsua_var.media_cfg.max_media_ports,
303 pjsua_var.media_cfg.clock_rate,
304 pjsua_var.mconf_cfg.channel_count,
305 pjsua_var.mconf_cfg.samples_per_frame,
306 pjsua_var.mconf_cfg.bits_per_sample,
Benny Prijono0498d902006-06-19 14:49:14 +0000307 opt, &pjsua_var.mconf);
Benny Prijonoeebe9af2006-06-13 22:57:13 +0000308 if (status != PJ_SUCCESS) {
Benny Prijono50f19b32008-03-11 13:15:43 +0000309 pjsua_perror(THIS_FILE, "Error creating conference bridge",
Benny Prijonoeebe9af2006-06-13 22:57:13 +0000310 status);
311 return status;
312 }
313
Benny Prijonof798e502009-03-09 13:08:16 +0000314 /* Are we using the audio switchboard (a.k.a APS-Direct)? */
315 pjsua_var.is_mswitch = pjmedia_conf_get_master_port(pjsua_var.mconf)
316 ->info.signature == PJMEDIA_CONF_SWITCH_SIGNATURE;
317
Benny Prijonoeebe9af2006-06-13 22:57:13 +0000318 /* Create null port just in case user wants to use null sound. */
319 status = pjmedia_null_port_create(pjsua_var.pool,
320 pjsua_var.media_cfg.clock_rate,
321 pjsua_var.mconf_cfg.channel_count,
322 pjsua_var.mconf_cfg.samples_per_frame,
323 pjsua_var.mconf_cfg.bits_per_sample,
324 &pjsua_var.null_port);
325 PJ_ASSERT_RETURN(status == PJ_SUCCESS, status);
326
Nanang Izzuddin69b69ae2009-04-14 15:18:30 +0000327#if defined(PJMEDIA_HAS_SRTP) && (PJMEDIA_HAS_SRTP != 0)
328 /* Initialize SRTP library. */
329 status = pjmedia_srtp_init_lib();
330 if (status != PJ_SUCCESS) {
331 pjsua_perror(THIS_FILE, "Error initializing SRTP library",
332 status);
333 return status;
334 }
335#endif
336
Benny Prijonoeebe9af2006-06-13 22:57:13 +0000337 return PJ_SUCCESS;
338}
339
340
341/*
342 * Create RTP and RTCP socket pair, and possibly resolve their public
343 * address via STUN.
344 */
345static pj_status_t create_rtp_rtcp_sock(const pjsua_transport_config *cfg,
346 pjmedia_sock_info *skinfo)
347{
348 enum {
349 RTP_RETRY = 100
350 };
351 int i;
Benny Prijono0a5cad82006-09-26 13:21:02 +0000352 pj_sockaddr_in bound_addr;
Benny Prijonoeebe9af2006-06-13 22:57:13 +0000353 pj_sockaddr_in mapped_addr[2];
354 pj_status_t status = PJ_SUCCESS;
Benny Prijono38fb3ea2008-01-02 08:27:03 +0000355 char addr_buf[PJ_INET6_ADDRSTRLEN+2];
Benny Prijonoeebe9af2006-06-13 22:57:13 +0000356 pj_sock_t sock[2];
357
Benny Prijonoc97608e2007-03-23 16:34:20 +0000358 /* Make sure STUN server resolution has completed */
Benny Prijonobb995fd2009-08-12 11:03:23 +0000359 status = resolve_stun_server(PJ_TRUE);
Benny Prijonoc97608e2007-03-23 16:34:20 +0000360 if (status != PJ_SUCCESS) {
361 pjsua_perror(THIS_FILE, "Error resolving STUN server", status);
362 return status;
363 }
364
Benny Prijonode479562007-03-15 10:23:55 +0000365 if (next_rtp_port == 0)
366 next_rtp_port = (pj_uint16_t)cfg->port;
Benny Prijonoeebe9af2006-06-13 22:57:13 +0000367
368 for (i=0; i<2; ++i)
369 sock[i] = PJ_INVALID_SOCKET;
370
Benny Prijono0a5cad82006-09-26 13:21:02 +0000371 bound_addr.sin_addr.s_addr = PJ_INADDR_ANY;
372 if (cfg->bound_addr.slen) {
373 status = pj_sockaddr_in_set_str_addr(&bound_addr, &cfg->bound_addr);
374 if (status != PJ_SUCCESS) {
375 pjsua_perror(THIS_FILE, "Unable to resolve transport bind address",
376 status);
377 return status;
378 }
379 }
Benny Prijonoeebe9af2006-06-13 22:57:13 +0000380
381 /* Loop retry to bind RTP and RTCP sockets. */
Benny Prijonode479562007-03-15 10:23:55 +0000382 for (i=0; i<RTP_RETRY; ++i, next_rtp_port += 2) {
Benny Prijonoeebe9af2006-06-13 22:57:13 +0000383
Benny Prijono4d79b0f2009-10-25 09:02:07 +0000384 /* Create RTP socket. */
Benny Prijono8ab968f2007-07-20 08:08:30 +0000385 status = pj_sock_socket(pj_AF_INET(), pj_SOCK_DGRAM(), 0, &sock[0]);
Benny Prijonoeebe9af2006-06-13 22:57:13 +0000386 if (status != PJ_SUCCESS) {
387 pjsua_perror(THIS_FILE, "socket() error", status);
388 return status;
389 }
390
Benny Prijono4d79b0f2009-10-25 09:02:07 +0000391 /* Apply QoS to RTP socket, if specified */
392 status = pj_sock_apply_qos2(sock[0], cfg->qos_type,
393 &cfg->qos_params,
394 2, THIS_FILE, "RTP socket");
395
396 /* Bind RTP socket */
Benny Prijono0cdcd3f2007-12-09 14:14:11 +0000397 status=pj_sock_bind_in(sock[0], pj_ntohl(bound_addr.sin_addr.s_addr),
398 next_rtp_port);
Benny Prijonoeebe9af2006-06-13 22:57:13 +0000399 if (status != PJ_SUCCESS) {
400 pj_sock_close(sock[0]);
401 sock[0] = PJ_INVALID_SOCKET;
402 continue;
403 }
404
Benny Prijono4d79b0f2009-10-25 09:02:07 +0000405 /* Create RTCP socket. */
Benny Prijono8ab968f2007-07-20 08:08:30 +0000406 status = pj_sock_socket(pj_AF_INET(), pj_SOCK_DGRAM(), 0, &sock[1]);
Benny Prijonoeebe9af2006-06-13 22:57:13 +0000407 if (status != PJ_SUCCESS) {
408 pjsua_perror(THIS_FILE, "socket() error", status);
409 pj_sock_close(sock[0]);
410 return status;
411 }
412
Benny Prijono4d79b0f2009-10-25 09:02:07 +0000413 /* Apply QoS to RTCP socket, if specified */
414 status = pj_sock_apply_qos2(sock[1], cfg->qos_type,
415 &cfg->qos_params,
416 2, THIS_FILE, "RTCP socket");
417
418 /* Bind RTCP socket */
Benny Prijono0cdcd3f2007-12-09 14:14:11 +0000419 status=pj_sock_bind_in(sock[1], pj_ntohl(bound_addr.sin_addr.s_addr),
420 (pj_uint16_t)(next_rtp_port+1));
Benny Prijonoeebe9af2006-06-13 22:57:13 +0000421 if (status != PJ_SUCCESS) {
422 pj_sock_close(sock[0]);
423 sock[0] = PJ_INVALID_SOCKET;
424
425 pj_sock_close(sock[1]);
426 sock[1] = PJ_INVALID_SOCKET;
427 continue;
428 }
429
430 /*
431 * If we're configured to use STUN, then find out the mapped address,
432 * and make sure that the mapped RTCP port is adjacent with the RTP.
433 */
Benny Prijonoc97608e2007-03-23 16:34:20 +0000434 if (pjsua_var.stun_srv.addr.sa_family != 0) {
435 char ip_addr[32];
436 pj_str_t stun_srv;
437
438 pj_ansi_strcpy(ip_addr,
439 pj_inet_ntoa(pjsua_var.stun_srv.ipv4.sin_addr));
440 stun_srv = pj_str(ip_addr);
441
Benny Prijono14c2b862007-02-21 00:40:05 +0000442 status=pjstun_get_mapped_addr(&pjsua_var.cp.factory, 2, sock,
Benny Prijonoaf09dc32007-04-22 12:48:30 +0000443 &stun_srv, pj_ntohs(pjsua_var.stun_srv.ipv4.sin_port),
444 &stun_srv, pj_ntohs(pjsua_var.stun_srv.ipv4.sin_port),
Benny Prijonoeebe9af2006-06-13 22:57:13 +0000445 mapped_addr);
446 if (status != PJ_SUCCESS) {
447 pjsua_perror(THIS_FILE, "STUN resolve error", status);
448 goto on_error;
449 }
450
Benny Prijono80eee892007-11-03 22:43:23 +0000451#if PJSUA_REQUIRE_CONSECUTIVE_RTCP_PORT
Benny Prijonoeebe9af2006-06-13 22:57:13 +0000452 if (pj_ntohs(mapped_addr[1].sin_port) ==
453 pj_ntohs(mapped_addr[0].sin_port)+1)
454 {
455 /* Success! */
456 break;
457 }
458
459 pj_sock_close(sock[0]);
460 sock[0] = PJ_INVALID_SOCKET;
461
462 pj_sock_close(sock[1]);
463 sock[1] = PJ_INVALID_SOCKET;
Benny Prijono80eee892007-11-03 22:43:23 +0000464#else
465 if (pj_ntohs(mapped_addr[1].sin_port) !=
466 pj_ntohs(mapped_addr[0].sin_port)+1)
467 {
468 PJ_LOG(4,(THIS_FILE,
469 "Note: STUN mapped RTCP port %d is not adjacent"
470 " to RTP port %d",
471 pj_ntohs(mapped_addr[1].sin_port),
472 pj_ntohs(mapped_addr[0].sin_port)));
473 }
474 /* Success! */
475 break;
476#endif
Benny Prijonoeebe9af2006-06-13 22:57:13 +0000477
Benny Prijono0a5cad82006-09-26 13:21:02 +0000478 } else if (cfg->public_addr.slen) {
479
480 status = pj_sockaddr_in_init(&mapped_addr[0], &cfg->public_addr,
Benny Prijonode479562007-03-15 10:23:55 +0000481 (pj_uint16_t)next_rtp_port);
Benny Prijono0a5cad82006-09-26 13:21:02 +0000482 if (status != PJ_SUCCESS)
483 goto on_error;
484
485 status = pj_sockaddr_in_init(&mapped_addr[1], &cfg->public_addr,
Benny Prijonode479562007-03-15 10:23:55 +0000486 (pj_uint16_t)(next_rtp_port+1));
Benny Prijono0a5cad82006-09-26 13:21:02 +0000487 if (status != PJ_SUCCESS)
488 goto on_error;
489
490 break;
491
Benny Prijonoeebe9af2006-06-13 22:57:13 +0000492 } else {
Benny Prijonoeebe9af2006-06-13 22:57:13 +0000493
Benny Prijono42d08d22007-12-20 11:23:07 +0000494 if (bound_addr.sin_addr.s_addr == 0) {
495 pj_sockaddr addr;
496
497 /* Get local IP address. */
498 status = pj_gethostip(pj_AF_INET(), &addr);
499 if (status != PJ_SUCCESS)
500 goto on_error;
501
502 bound_addr.sin_addr.s_addr = addr.ipv4.sin_addr.s_addr;
503 }
Benny Prijonoeebe9af2006-06-13 22:57:13 +0000504
Benny Prijonofe0b1d62007-12-05 04:07:37 +0000505 for (i=0; i<2; ++i) {
506 pj_sockaddr_in_init(&mapped_addr[i], NULL, 0);
Benny Prijono42d08d22007-12-20 11:23:07 +0000507 mapped_addr[i].sin_addr.s_addr = bound_addr.sin_addr.s_addr;
Benny Prijonofe0b1d62007-12-05 04:07:37 +0000508 }
Benny Prijonoeebe9af2006-06-13 22:57:13 +0000509
Benny Prijonode479562007-03-15 10:23:55 +0000510 mapped_addr[0].sin_port=pj_htons((pj_uint16_t)next_rtp_port);
511 mapped_addr[1].sin_port=pj_htons((pj_uint16_t)(next_rtp_port+1));
Benny Prijonoeebe9af2006-06-13 22:57:13 +0000512 break;
513 }
514 }
515
516 if (sock[0] == PJ_INVALID_SOCKET) {
517 PJ_LOG(1,(THIS_FILE,
518 "Unable to find appropriate RTP/RTCP ports combination"));
519 goto on_error;
520 }
521
522
523 skinfo->rtp_sock = sock[0];
524 pj_memcpy(&skinfo->rtp_addr_name,
525 &mapped_addr[0], sizeof(pj_sockaddr_in));
526
527 skinfo->rtcp_sock = sock[1];
528 pj_memcpy(&skinfo->rtcp_addr_name,
529 &mapped_addr[1], sizeof(pj_sockaddr_in));
530
Benny Prijono8b22ce12008-02-08 12:57:55 +0000531 PJ_LOG(4,(THIS_FILE, "RTP socket reachable at %s",
Benny Prijono5186eae2007-12-03 14:38:25 +0000532 pj_sockaddr_print(&skinfo->rtp_addr_name, addr_buf,
533 sizeof(addr_buf), 3)));
Benny Prijono8b22ce12008-02-08 12:57:55 +0000534 PJ_LOG(4,(THIS_FILE, "RTCP socket reachable at %s",
Benny Prijono5186eae2007-12-03 14:38:25 +0000535 pj_sockaddr_print(&skinfo->rtcp_addr_name, addr_buf,
536 sizeof(addr_buf), 3)));
Benny Prijonoeebe9af2006-06-13 22:57:13 +0000537
Benny Prijonode479562007-03-15 10:23:55 +0000538 next_rtp_port += 2;
Benny Prijonoeebe9af2006-06-13 22:57:13 +0000539 return PJ_SUCCESS;
540
541on_error:
542 for (i=0; i<2; ++i) {
543 if (sock[i] != PJ_INVALID_SOCKET)
544 pj_sock_close(sock[i]);
545 }
546 return status;
547}
548
Nanang Izzuddin148fd392008-06-16 09:52:50 +0000549/* Check if sound device is idle. */
550static void check_snd_dev_idle()
551{
Nanang Izzuddin7082b262009-06-12 11:15:08 +0000552 unsigned call_cnt;
553
554 /* Get the call count, we shouldn't close the sound device when there is
555 * any calls active.
556 */
557 call_cnt = pjsua_call_get_count();
558
559 /* When this function is called from pjsua_media_channel_deinit() upon
560 * disconnecting call, actually the call count hasn't been updated/
561 * decreased. So we put additional check here, if there is only one
562 * call and it's in DISCONNECTED state, there is actually no active
563 * call.
564 */
565 if (call_cnt == 1) {
566 pjsua_call_id call_id;
567 pj_status_t status;
568
569 status = pjsua_enum_calls(&call_id, &call_cnt);
570 if (status == PJ_SUCCESS && call_cnt > 0 &&
571 !pjsua_call_is_active(call_id))
572 {
573 call_cnt = 0;
574 }
575 }
Nanang Izzuddin148fd392008-06-16 09:52:50 +0000576
577 /* Activate sound device auto-close timer if sound device is idle.
Nanang Izzuddin7082b262009-06-12 11:15:08 +0000578 * It is idle when there is no port connection in the bridge and
579 * there is no active call.
Nanang Izzuddin148fd392008-06-16 09:52:50 +0000580 */
581 if ((pjsua_var.snd_port!=NULL || pjsua_var.null_snd!=NULL) &&
582 pjsua_var.snd_idle_timer.id == PJ_FALSE &&
583 pjmedia_conf_get_connect_count(pjsua_var.mconf) == 0 &&
Nanang Izzuddin7082b262009-06-12 11:15:08 +0000584 call_cnt == 0 &&
Nanang Izzuddin148fd392008-06-16 09:52:50 +0000585 pjsua_var.media_cfg.snd_auto_close_time >= 0)
586 {
587 pj_time_val delay;
588
589 delay.msec = 0;
590 delay.sec = pjsua_var.media_cfg.snd_auto_close_time;
591
592 pjsua_var.snd_idle_timer.id = PJ_TRUE;
593 pjsip_endpt_schedule_timer(pjsua_var.endpt, &pjsua_var.snd_idle_timer,
594 &delay);
595 }
596}
597
598
Nanang Izzuddin68559c32008-06-13 17:01:46 +0000599/* Timer callback to close sound device */
600static void close_snd_timer_cb( pj_timer_heap_t *th,
601 pj_timer_entry *entry)
602{
603 PJ_UNUSED_ARG(th);
604
Benny Prijono0f711b42009-05-06 19:08:43 +0000605 PJSUA_LOCK();
606 if (entry->id) {
607 PJ_LOG(4,(THIS_FILE,"Closing sound device after idle for %d seconds",
608 pjsua_var.media_cfg.snd_auto_close_time));
Nanang Izzuddin68559c32008-06-13 17:01:46 +0000609
Benny Prijono0f711b42009-05-06 19:08:43 +0000610 entry->id = PJ_FALSE;
Nanang Izzuddin68559c32008-06-13 17:01:46 +0000611
Benny Prijono0f711b42009-05-06 19:08:43 +0000612 close_snd_dev();
613 }
614 PJSUA_UNLOCK();
Nanang Izzuddin68559c32008-06-13 17:01:46 +0000615}
616
Benny Prijonoeebe9af2006-06-13 22:57:13 +0000617
618/*
619 * Start pjsua media subsystem.
620 */
621pj_status_t pjsua_media_subsys_start(void)
622{
623 pj_status_t status;
624
625 /* Create media for calls, if none is specified */
626 if (pjsua_var.calls[0].med_tp == NULL) {
627 pjsua_transport_config transport_cfg;
628
629 /* Create default transport config */
630 pjsua_transport_config_default(&transport_cfg);
631 transport_cfg.port = DEFAULT_RTP_PORT;
632
633 status = pjsua_media_transports_create(&transport_cfg);
634 if (status != PJ_SUCCESS)
635 return status;
636 }
637
Nanang Izzuddin68559c32008-06-13 17:01:46 +0000638 pj_timer_entry_init(&pjsua_var.snd_idle_timer, PJ_FALSE, NULL,
639 &close_snd_timer_cb);
Benny Prijonoeebe9af2006-06-13 22:57:13 +0000640
Benny Prijonobf53b002010-01-04 13:08:31 +0000641 /* Perform NAT detection */
642 pjsua_detect_nat_type();
643
Benny Prijonoeebe9af2006-06-13 22:57:13 +0000644 return PJ_SUCCESS;
645}
646
647
648/*
649 * Destroy pjsua media subsystem.
650 */
651pj_status_t pjsua_media_subsys_destroy(void)
652{
653 unsigned i;
654
Benny Prijono384dab42009-10-14 01:58:04 +0000655 PJ_LOG(4,(THIS_FILE, "Shutting down media.."));
656
Benny Prijonoeebe9af2006-06-13 22:57:13 +0000657 close_snd_dev();
658
659 if (pjsua_var.mconf) {
660 pjmedia_conf_destroy(pjsua_var.mconf);
661 pjsua_var.mconf = NULL;
662 }
663
664 if (pjsua_var.null_port) {
665 pjmedia_port_destroy(pjsua_var.null_port);
666 pjsua_var.null_port = NULL;
667 }
668
669 /* Destroy file players */
670 for (i=0; i<PJ_ARRAY_SIZE(pjsua_var.player); ++i) {
671 if (pjsua_var.player[i].port) {
672 pjmedia_port_destroy(pjsua_var.player[i].port);
673 pjsua_var.player[i].port = NULL;
674 }
675 }
676
677 /* Destroy file recorders */
678 for (i=0; i<PJ_ARRAY_SIZE(pjsua_var.recorder); ++i) {
679 if (pjsua_var.recorder[i].port) {
680 pjmedia_port_destroy(pjsua_var.recorder[i].port);
681 pjsua_var.recorder[i].port = NULL;
682 }
683 }
684
685 /* Close media transports */
Benny Prijono0875ae82006-12-26 00:11:48 +0000686 for (i=0; i<pjsua_var.ua_cfg.max_calls; ++i) {
Benny Prijono311b63f2008-07-14 11:31:40 +0000687 if (pjsua_var.calls[i].med_tp_st != PJSUA_MED_TP_IDLE) {
688 pjsua_media_channel_deinit(i);
689 }
Benny Prijono40860c32008-09-04 13:55:33 +0000690 if (pjsua_var.calls[i].med_tp && pjsua_var.calls[i].med_tp_auto_del) {
691 pjmedia_transport_close(pjsua_var.calls[i].med_tp);
Benny Prijonoeebe9af2006-06-13 22:57:13 +0000692 }
Benny Prijono40860c32008-09-04 13:55:33 +0000693 pjsua_var.calls[i].med_tp = NULL;
Benny Prijonoeebe9af2006-06-13 22:57:13 +0000694 }
695
696 /* Destroy media endpoint. */
697 if (pjsua_var.med_endpt) {
698
699 /* Shutdown all codecs: */
700# if PJMEDIA_HAS_SPEEX_CODEC
701 pjmedia_codec_speex_deinit();
702# endif /* PJMEDIA_HAS_SPEEX_CODEC */
703
704# if PJMEDIA_HAS_GSM_CODEC
705 pjmedia_codec_gsm_deinit();
706# endif /* PJMEDIA_HAS_GSM_CODEC */
707
708# if PJMEDIA_HAS_G711_CODEC
709 pjmedia_codec_g711_deinit();
710# endif /* PJMEDIA_HAS_G711_CODEC */
711
Benny Prijono7ffd7752008-03-17 14:07:53 +0000712# if PJMEDIA_HAS_G722_CODEC
713 pjmedia_codec_g722_deinit();
714# endif /* PJMEDIA_HAS_G722_CODEC */
715
Nanang Izzuddin6df1d532008-08-25 13:46:03 +0000716# if PJMEDIA_HAS_INTEL_IPP
Nanang Izzuddin7dd32682008-08-19 11:23:33 +0000717 pjmedia_codec_ipp_deinit();
Nanang Izzuddin6df1d532008-08-25 13:46:03 +0000718# endif /* PJMEDIA_HAS_INTEL_IPP */
Nanang Izzuddin7dd32682008-08-19 11:23:33 +0000719
Nanang Izzuddin81db8c72009-02-05 10:59:14 +0000720# if PJMEDIA_HAS_PASSTHROUGH_CODECS
721 pjmedia_codec_passthrough_deinit();
722# endif /* PJMEDIA_HAS_PASSTHROUGH_CODECS */
723
Nanang Izzuddin57b88572009-04-01 12:05:34 +0000724# if PJMEDIA_HAS_G7221_CODEC
725 pjmedia_codec_g7221_deinit();
726# endif /* PJMEDIA_HAS_G7221_CODEC */
727
Benny Prijonoeebe9af2006-06-13 22:57:13 +0000728# if PJMEDIA_HAS_L16_CODEC
729 pjmedia_codec_l16_deinit();
730# endif /* PJMEDIA_HAS_L16_CODEC */
731
Benny Prijonoeebe9af2006-06-13 22:57:13 +0000732 pjmedia_endpt_destroy(pjsua_var.med_endpt);
733 pjsua_var.med_endpt = NULL;
Benny Prijonoeebe9af2006-06-13 22:57:13 +0000734
Benny Prijonoad2e0ca2007-04-29 12:31:51 +0000735 /* Deinitialize sound subsystem */
Benny Prijonoa41d66e2007-10-01 12:17:23 +0000736 // Not necessary, as pjmedia_snd_deinit() should have been called
737 // in pjmedia_endpt_destroy().
738 //pjmedia_snd_deinit();
Benny Prijonoad2e0ca2007-04-29 12:31:51 +0000739 }
Benny Prijonoaf1bb1e2006-11-21 12:39:31 +0000740
Benny Prijonode479562007-03-15 10:23:55 +0000741 /* Reset RTP port */
742 next_rtp_port = 0;
743
Benny Prijonoeebe9af2006-06-13 22:57:13 +0000744 return PJ_SUCCESS;
745}
746
747
Benny Prijonoc97608e2007-03-23 16:34:20 +0000748/* Create normal UDP media transports */
749static pj_status_t create_udp_media_transports(pjsua_transport_config *cfg)
Benny Prijonoeebe9af2006-06-13 22:57:13 +0000750{
Benny Prijonoeebe9af2006-06-13 22:57:13 +0000751 unsigned i;
Benny Prijono617c5bc2007-04-02 19:51:21 +0000752 pjmedia_sock_info skinfo;
Benny Prijonoeebe9af2006-06-13 22:57:13 +0000753 pj_status_t status;
754
Benny Prijonoeebe9af2006-06-13 22:57:13 +0000755 /* Create each media transport */
756 for (i=0; i<pjsua_var.ua_cfg.max_calls; ++i) {
757
Benny Prijono617c5bc2007-04-02 19:51:21 +0000758 status = create_rtp_rtcp_sock(cfg, &skinfo);
Benny Prijonoeebe9af2006-06-13 22:57:13 +0000759 if (status != PJ_SUCCESS) {
760 pjsua_perror(THIS_FILE, "Unable to create RTP/RTCP socket",
761 status);
762 goto on_error;
763 }
Benny Prijonod8179652008-01-23 20:39:07 +0000764
Benny Prijonoeebe9af2006-06-13 22:57:13 +0000765 status = pjmedia_transport_udp_attach(pjsua_var.med_endpt, NULL,
Benny Prijono617c5bc2007-04-02 19:51:21 +0000766 &skinfo, 0,
Benny Prijonoeebe9af2006-06-13 22:57:13 +0000767 &pjsua_var.calls[i].med_tp);
768 if (status != PJ_SUCCESS) {
769 pjsua_perror(THIS_FILE, "Unable to create media transport",
770 status);
771 goto on_error;
772 }
Benny Prijono00cae612006-07-31 15:19:36 +0000773
Benny Prijonod8179652008-01-23 20:39:07 +0000774 pjmedia_transport_simulate_lost(pjsua_var.calls[i].med_tp,
775 PJMEDIA_DIR_ENCODING,
776 pjsua_var.media_cfg.tx_drop_pct);
Benny Prijono00cae612006-07-31 15:19:36 +0000777
Benny Prijonod8179652008-01-23 20:39:07 +0000778 pjmedia_transport_simulate_lost(pjsua_var.calls[i].med_tp,
779 PJMEDIA_DIR_DECODING,
780 pjsua_var.media_cfg.rx_drop_pct);
Benny Prijono00cae612006-07-31 15:19:36 +0000781
Benny Prijonoeebe9af2006-06-13 22:57:13 +0000782 }
783
Benny Prijonoc97608e2007-03-23 16:34:20 +0000784 return PJ_SUCCESS;
785
786on_error:
787 for (i=0; i<pjsua_var.ua_cfg.max_calls; ++i) {
788 if (pjsua_var.calls[i].med_tp != NULL) {
789 pjmedia_transport_close(pjsua_var.calls[i].med_tp);
790 pjsua_var.calls[i].med_tp = NULL;
791 }
792 }
793
794 return status;
795}
796
797
Benny Prijono096c56c2007-09-15 08:30:16 +0000798/* This callback is called when ICE negotiation completes */
Benny Prijonof76e1392008-06-06 14:51:48 +0000799static void on_ice_complete(pjmedia_transport *tp,
800 pj_ice_strans_op op,
801 pj_status_t result)
Benny Prijono096c56c2007-09-15 08:30:16 +0000802{
Benny Prijonof76e1392008-06-06 14:51:48 +0000803 unsigned id;
Benny Prijono096c56c2007-09-15 08:30:16 +0000804 pj_bool_t found = PJ_FALSE;
805
Benny Prijono096c56c2007-09-15 08:30:16 +0000806 /* Find call which has this media transport */
807
808 PJSUA_LOCK();
809
Benny Prijonof76e1392008-06-06 14:51:48 +0000810 for (id=0; id<pjsua_var.ua_cfg.max_calls; ++id) {
811 if (pjsua_var.calls[id].med_tp == tp ||
812 pjsua_var.calls[id].med_orig == tp)
813 {
814 found = PJ_TRUE;
815 break;
Benny Prijono096c56c2007-09-15 08:30:16 +0000816 }
817 }
818
819 PJSUA_UNLOCK();
820
Benny Prijonof76e1392008-06-06 14:51:48 +0000821 if (!found)
822 return;
823
824 switch (op) {
825 case PJ_ICE_STRANS_OP_INIT:
Benny Prijono224b4e22008-06-19 14:10:28 +0000826 pjsua_var.calls[id].med_tp_ready = result;
Benny Prijonof76e1392008-06-06 14:51:48 +0000827 break;
828 case PJ_ICE_STRANS_OP_NEGOTIATION:
829 if (result != PJ_SUCCESS) {
830 pjsua_var.calls[id].media_st = PJSUA_CALL_MEDIA_ERROR;
831 pjsua_var.calls[id].media_dir = PJMEDIA_DIR_NONE;
832
833 if (pjsua_var.ua_cfg.cb.on_call_media_state) {
834 pjsua_var.ua_cfg.cb.on_call_media_state(id);
835 }
Benny Prijonof5d9f1f2009-10-14 13:13:18 +0000836 } else {
837 /* Send UPDATE if default transport address is different than
838 * what was advertised (ticket #881)
839 */
840 pjmedia_transport_info tpinfo;
841 pjmedia_ice_transport_info *ii = NULL;
842 unsigned i;
843
844 pjmedia_transport_info_init(&tpinfo);
845 pjmedia_transport_get_info(tp, &tpinfo);
846 for (i=0; i<tpinfo.specific_info_cnt; ++i) {
847 if (tpinfo.spc_info[i].type==PJMEDIA_TRANSPORT_TYPE_ICE) {
848 ii = (pjmedia_ice_transport_info*)
849 tpinfo.spc_info[i].buffer;
850 break;
851 }
852 }
853
854 if (ii && ii->role==PJ_ICE_SESS_ROLE_CONTROLLING &&
855 pj_sockaddr_cmp(&tpinfo.sock_info.rtp_addr_name,
856 &pjsua_var.calls[id].med_rtp_addr))
857 {
Benny Prijonoa8f9e622010-06-21 13:28:55 +0000858 pj_bool_t use_update;
859 const pj_str_t STR_UPDATE = { "UPDATE", 6 };
860 pjsip_dialog_cap_status support_update;
861 pjsip_dialog *dlg;
862
863 dlg = pjsua_var.calls[id].inv->dlg;
864 support_update = pjsip_dlg_remote_has_cap(dlg, PJSIP_H_ALLOW,
865 NULL, &STR_UPDATE);
866 use_update = (support_update == PJSIP_DIALOG_CAP_SUPPORTED);
867
Benny Prijonof5d9f1f2009-10-14 13:13:18 +0000868 PJ_LOG(4,(THIS_FILE,
869 "ICE default transport address has changed for "
Benny Prijonoa8f9e622010-06-21 13:28:55 +0000870 "call %d, sending %s", id,
871 (use_update ? "UPDATE" : "re-INVITE")));
872
873 if (use_update)
874 pjsua_call_update(id, 0, NULL);
875 else
876 pjsua_call_reinvite(id, 0, NULL);
Benny Prijonof5d9f1f2009-10-14 13:13:18 +0000877 }
Benny Prijonof76e1392008-06-06 14:51:48 +0000878 }
879 break;
Benny Prijono4d6ff4d2010-06-19 12:35:33 +0000880 case PJ_ICE_STRANS_OP_KEEP_ALIVE:
881 if (result != PJ_SUCCESS) {
882 PJ_PERROR(4,(THIS_FILE, result,
883 "ICE keep alive failure for transport %d", id));
884 }
885 if (pjsua_var.ua_cfg.cb.on_ice_transport_error) {
886 (*pjsua_var.ua_cfg.cb.on_ice_transport_error)(id, op, result,
887 NULL);
888 }
889 break;
Benny Prijono096c56c2007-09-15 08:30:16 +0000890 }
891}
892
893
Benny Prijonof76e1392008-06-06 14:51:48 +0000894/* Parse "HOST:PORT" format */
895static pj_status_t parse_host_port(const pj_str_t *host_port,
896 pj_str_t *host, pj_uint16_t *port)
Benny Prijonoc97608e2007-03-23 16:34:20 +0000897{
Benny Prijonof76e1392008-06-06 14:51:48 +0000898 pj_str_t str_port;
899
900 str_port.ptr = pj_strchr(host_port, ':');
901 if (str_port.ptr != NULL) {
902 int iport;
903
904 host->ptr = host_port->ptr;
905 host->slen = (str_port.ptr - host->ptr);
906 str_port.ptr++;
907 str_port.slen = host_port->slen - host->slen - 1;
908 iport = (int)pj_strtoul(&str_port);
909 if (iport < 1 || iport > 65535)
910 return PJ_EINVAL;
911 *port = (pj_uint16_t)iport;
912 } else {
913 *host = *host_port;
914 *port = 0;
915 }
916
917 return PJ_SUCCESS;
918}
919
920/* Create ICE media transports (when ice is enabled) */
Benny Prijono4d79b0f2009-10-25 09:02:07 +0000921static pj_status_t create_ice_media_transports(pjsua_transport_config *cfg)
Benny Prijonof76e1392008-06-06 14:51:48 +0000922{
923 char stunip[PJ_INET6_ADDRSTRLEN];
924 pj_ice_strans_cfg ice_cfg;
Benny Prijonoc97608e2007-03-23 16:34:20 +0000925 unsigned i;
926 pj_status_t status;
927
Benny Prijonoda9785b2007-04-02 20:43:06 +0000928 /* Make sure STUN server resolution has completed */
Benny Prijonobb995fd2009-08-12 11:03:23 +0000929 status = resolve_stun_server(PJ_TRUE);
Benny Prijonoda9785b2007-04-02 20:43:06 +0000930 if (status != PJ_SUCCESS) {
931 pjsua_perror(THIS_FILE, "Error resolving STUN server", status);
932 return status;
933 }
934
Benny Prijonof76e1392008-06-06 14:51:48 +0000935 /* Create ICE stream transport configuration */
936 pj_ice_strans_cfg_default(&ice_cfg);
937 pj_stun_config_init(&ice_cfg.stun_cfg, &pjsua_var.cp.factory, 0,
938 pjsip_endpt_get_ioqueue(pjsua_var.endpt),
939 pjsip_endpt_get_timer_heap(pjsua_var.endpt));
940
941 ice_cfg.af = pj_AF_INET();
942 ice_cfg.resolver = pjsua_var.resolver;
943
Benny Prijono329d6382009-05-29 13:04:03 +0000944 ice_cfg.opt = pjsua_var.media_cfg.ice_opt;
945
Benny Prijonof76e1392008-06-06 14:51:48 +0000946 /* Configure STUN settings */
947 if (pj_sockaddr_has_addr(&pjsua_var.stun_srv)) {
948 pj_sockaddr_print(&pjsua_var.stun_srv, stunip, sizeof(stunip), 0);
949 ice_cfg.stun.server = pj_str(stunip);
950 ice_cfg.stun.port = pj_sockaddr_get_port(&pjsua_var.stun_srv);
951 }
Benny Prijono329d6382009-05-29 13:04:03 +0000952 if (pjsua_var.media_cfg.ice_max_host_cands >= 0)
953 ice_cfg.stun.max_host_cands = pjsua_var.media_cfg.ice_max_host_cands;
Benny Prijonof76e1392008-06-06 14:51:48 +0000954
Benny Prijono4d79b0f2009-10-25 09:02:07 +0000955 /* Copy QoS setting to STUN setting */
956 ice_cfg.stun.cfg.qos_type = cfg->qos_type;
957 pj_memcpy(&ice_cfg.stun.cfg.qos_params, &cfg->qos_params,
958 sizeof(cfg->qos_params));
959
Benny Prijonof76e1392008-06-06 14:51:48 +0000960 /* Configure TURN settings */
961 if (pjsua_var.media_cfg.enable_turn) {
962 status = parse_host_port(&pjsua_var.media_cfg.turn_server,
963 &ice_cfg.turn.server,
964 &ice_cfg.turn.port);
965 if (status != PJ_SUCCESS || ice_cfg.turn.server.slen == 0) {
966 PJ_LOG(1,(THIS_FILE, "Invalid TURN server setting"));
967 return PJ_EINVAL;
968 }
969 if (ice_cfg.turn.port == 0)
970 ice_cfg.turn.port = 3479;
971 ice_cfg.turn.conn_type = pjsua_var.media_cfg.turn_conn_type;
972 pj_memcpy(&ice_cfg.turn.auth_cred,
973 &pjsua_var.media_cfg.turn_auth_cred,
974 sizeof(ice_cfg.turn.auth_cred));
Benny Prijono4d79b0f2009-10-25 09:02:07 +0000975
976 /* Copy QoS setting to TURN setting */
977 ice_cfg.turn.cfg.qos_type = cfg->qos_type;
978 pj_memcpy(&ice_cfg.turn.cfg.qos_params, &cfg->qos_params,
979 sizeof(cfg->qos_params));
Benny Prijonof76e1392008-06-06 14:51:48 +0000980 }
Benny Prijonob681a2f2007-03-25 18:44:51 +0000981
Benny Prijonoc97608e2007-03-23 16:34:20 +0000982 /* Create each media transport */
983 for (i=0; i<pjsua_var.ua_cfg.max_calls; ++i) {
Benny Prijono096c56c2007-09-15 08:30:16 +0000984 pjmedia_ice_cb ice_cb;
Benny Prijono38fb3ea2008-01-02 08:27:03 +0000985 char name[32];
Benny Prijonof76e1392008-06-06 14:51:48 +0000986 unsigned comp_cnt;
Benny Prijonoc97608e2007-03-23 16:34:20 +0000987
Benny Prijono096c56c2007-09-15 08:30:16 +0000988 pj_bzero(&ice_cb, sizeof(pjmedia_ice_cb));
989 ice_cb.on_ice_complete = &on_ice_complete;
Benny Prijono38fb3ea2008-01-02 08:27:03 +0000990 pj_ansi_snprintf(name, sizeof(name), "icetp%02d", i);
Benny Prijono224b4e22008-06-19 14:10:28 +0000991 pjsua_var.calls[i].med_tp_ready = PJ_EPENDING;
Benny Prijonof76e1392008-06-06 14:51:48 +0000992
993 comp_cnt = 1;
Benny Prijono551af422008-08-07 09:55:52 +0000994 if (PJMEDIA_ADVERTISE_RTCP && !pjsua_var.media_cfg.ice_no_rtcp)
Benny Prijonof76e1392008-06-06 14:51:48 +0000995 ++comp_cnt;
996
997 status = pjmedia_ice_create(pjsua_var.med_endpt, name, comp_cnt,
998 &ice_cfg, &ice_cb,
Benny Prijonoc97608e2007-03-23 16:34:20 +0000999 &pjsua_var.calls[i].med_tp);
1000 if (status != PJ_SUCCESS) {
1001 pjsua_perror(THIS_FILE, "Unable to create ICE media transport",
1002 status);
1003 goto on_error;
1004 }
1005
Benny Prijonof76e1392008-06-06 14:51:48 +00001006 /* Wait until transport is initialized, or time out */
1007 PJSUA_UNLOCK();
Benny Prijono224b4e22008-06-19 14:10:28 +00001008 while (pjsua_var.calls[i].med_tp_ready == PJ_EPENDING) {
Benny Prijonof76e1392008-06-06 14:51:48 +00001009 pjsua_handle_events(100);
1010 }
1011 PJSUA_LOCK();
Benny Prijono224b4e22008-06-19 14:10:28 +00001012 if (pjsua_var.calls[i].med_tp_ready != PJ_SUCCESS) {
Benny Prijonof76e1392008-06-06 14:51:48 +00001013 pjsua_perror(THIS_FILE, "Error initializing ICE media transport",
Benny Prijono224b4e22008-06-19 14:10:28 +00001014 pjsua_var.calls[i].med_tp_ready);
1015 status = pjsua_var.calls[i].med_tp_ready;
Benny Prijonof76e1392008-06-06 14:51:48 +00001016 goto on_error;
1017 }
1018
Benny Prijonod8179652008-01-23 20:39:07 +00001019 pjmedia_transport_simulate_lost(pjsua_var.calls[i].med_tp,
1020 PJMEDIA_DIR_ENCODING,
1021 pjsua_var.media_cfg.tx_drop_pct);
Benny Prijono11da9bc2007-09-15 08:55:00 +00001022
Benny Prijonod8179652008-01-23 20:39:07 +00001023 pjmedia_transport_simulate_lost(pjsua_var.calls[i].med_tp,
1024 PJMEDIA_DIR_DECODING,
1025 pjsua_var.media_cfg.rx_drop_pct);
Benny Prijonoc97608e2007-03-23 16:34:20 +00001026 }
Benny Prijonoeebe9af2006-06-13 22:57:13 +00001027
1028 return PJ_SUCCESS;
1029
1030on_error:
1031 for (i=0; i<pjsua_var.ua_cfg.max_calls; ++i) {
1032 if (pjsua_var.calls[i].med_tp != NULL) {
Benny Prijonoc97608e2007-03-23 16:34:20 +00001033 pjmedia_transport_close(pjsua_var.calls[i].med_tp);
Benny Prijonoeebe9af2006-06-13 22:57:13 +00001034 pjsua_var.calls[i].med_tp = NULL;
1035 }
1036 }
1037
Benny Prijonoc97608e2007-03-23 16:34:20 +00001038 return status;
1039}
1040
1041
1042/*
1043 * Create UDP media transports for all the calls. This function creates
1044 * one UDP media transport for each call.
1045 */
Benny Prijono1f61a8f2007-08-16 10:11:44 +00001046PJ_DEF(pj_status_t) pjsua_media_transports_create(
1047 const pjsua_transport_config *app_cfg)
Benny Prijonoc97608e2007-03-23 16:34:20 +00001048{
1049 pjsua_transport_config cfg;
1050 unsigned i;
1051 pj_status_t status;
1052
1053
1054 /* Make sure pjsua_init() has been called */
1055 PJ_ASSERT_RETURN(pjsua_var.ua_cfg.max_calls>0, PJ_EINVALIDOP);
1056
1057 PJSUA_LOCK();
1058
1059 /* Delete existing media transports */
1060 for (i=0; i<pjsua_var.ua_cfg.max_calls; ++i) {
Benny Prijono40860c32008-09-04 13:55:33 +00001061 if (pjsua_var.calls[i].med_tp != NULL &&
1062 pjsua_var.calls[i].med_tp_auto_del)
1063 {
Benny Prijonoc97608e2007-03-23 16:34:20 +00001064 pjmedia_transport_close(pjsua_var.calls[i].med_tp);
1065 pjsua_var.calls[i].med_tp = NULL;
Nanang Izzuddind704a8b2008-09-23 16:34:07 +00001066 pjsua_var.calls[i].med_orig = NULL;
Benny Prijonoc97608e2007-03-23 16:34:20 +00001067 }
1068 }
1069
1070 /* Copy config */
1071 pjsua_transport_config_dup(pjsua_var.pool, &cfg, app_cfg);
1072
Benny Prijono40860c32008-09-04 13:55:33 +00001073 /* Create the transports */
Benny Prijonoc97608e2007-03-23 16:34:20 +00001074 if (pjsua_var.media_cfg.enable_ice) {
Benny Prijono4d79b0f2009-10-25 09:02:07 +00001075 status = create_ice_media_transports(&cfg);
Benny Prijonoc97608e2007-03-23 16:34:20 +00001076 } else {
1077 status = create_udp_media_transports(&cfg);
1078 }
1079
Benny Prijono40860c32008-09-04 13:55:33 +00001080 /* Set media transport auto_delete to True */
1081 for (i=0; i<pjsua_var.ua_cfg.max_calls; ++i) {
1082 pjsua_var.calls[i].med_tp_auto_del = PJ_TRUE;
1083 }
Benny Prijonoc97608e2007-03-23 16:34:20 +00001084
Benny Prijonoeebe9af2006-06-13 22:57:13 +00001085 PJSUA_UNLOCK();
1086
1087 return status;
1088}
1089
Benny Prijono40860c32008-09-04 13:55:33 +00001090/*
1091 * Attach application's created media transports.
1092 */
1093PJ_DEF(pj_status_t) pjsua_media_transports_attach(pjsua_media_transport tp[],
1094 unsigned count,
1095 pj_bool_t auto_delete)
1096{
1097 unsigned i;
1098
1099 PJ_ASSERT_RETURN(tp && count==pjsua_var.ua_cfg.max_calls, PJ_EINVAL);
1100
1101 /* Assign the media transports */
1102 for (i=0; i<pjsua_var.ua_cfg.max_calls; ++i) {
1103 if (pjsua_var.calls[i].med_tp != NULL &&
1104 pjsua_var.calls[i].med_tp_auto_del)
1105 {
1106 pjmedia_transport_close(pjsua_var.calls[i].med_tp);
1107 }
1108
1109 pjsua_var.calls[i].med_tp = tp[i].transport;
1110 pjsua_var.calls[i].med_tp_auto_del = auto_delete;
1111 }
1112
1113 return PJ_SUCCESS;
1114}
1115
Benny Prijonoeebe9af2006-06-13 22:57:13 +00001116
Benny Prijonoc97608e2007-03-23 16:34:20 +00001117pj_status_t pjsua_media_channel_init(pjsua_call_id call_id,
Benny Prijonod8179652008-01-23 20:39:07 +00001118 pjsip_role_e role,
Benny Prijono25b2ea12008-01-24 19:20:54 +00001119 int security_level,
Benny Prijono224b4e22008-06-19 14:10:28 +00001120 pj_pool_t *tmp_pool,
1121 const pjmedia_sdp_session *rem_sdp,
Benny Prijono25b2ea12008-01-24 19:20:54 +00001122 int *sip_err_code)
Benny Prijonoc97608e2007-03-23 16:34:20 +00001123{
1124 pjsua_call *call = &pjsua_var.calls[call_id];
Benny Prijono224b4e22008-06-19 14:10:28 +00001125 pj_status_t status;
Benny Prijonoc97608e2007-03-23 16:34:20 +00001126
Benny Prijonod8179652008-01-23 20:39:07 +00001127#if defined(PJMEDIA_HAS_SRTP) && (PJMEDIA_HAS_SRTP != 0)
1128 pjsua_acc *acc = &pjsua_var.acc[call->acc_id];
1129 pjmedia_srtp_setting srtp_opt;
Benny Prijonoa310bd22008-06-27 21:19:44 +00001130 pjmedia_transport *srtp = NULL;
Benny Prijonod8179652008-01-23 20:39:07 +00001131#endif
Benny Prijonoc97608e2007-03-23 16:34:20 +00001132
Benny Prijonod8179652008-01-23 20:39:07 +00001133 PJ_UNUSED_ARG(role);
Benny Prijonoc97608e2007-03-23 16:34:20 +00001134
Benny Prijonod8179652008-01-23 20:39:07 +00001135 /* Return error if media transport has not been created yet
1136 * (e.g. application is starting)
1137 */
1138 if (call->med_tp == NULL) {
Benny Prijono03789052008-09-16 14:30:50 +00001139 if (sip_err_code)
1140 *sip_err_code = PJSIP_SC_INTERNAL_SERVER_ERROR;
Benny Prijonod8179652008-01-23 20:39:07 +00001141 return PJ_EBUSY;
Benny Prijonoc97608e2007-03-23 16:34:20 +00001142 }
1143
Benny Prijonod8179652008-01-23 20:39:07 +00001144#if defined(PJMEDIA_HAS_SRTP) && (PJMEDIA_HAS_SRTP != 0)
Benny Prijono53a7c702008-04-14 02:57:29 +00001145 /* This function may be called when SRTP transport already exists
1146 * (e.g: in re-invite, update), don't need to destroy/re-create.
1147 */
1148 if (!call->med_orig || call->med_tp == call->med_orig) {
1149
1150 /* Check if SRTP requires secure signaling */
1151 if (acc->cfg.use_srtp != PJMEDIA_SRTP_DISABLED) {
1152 if (security_level < acc->cfg.srtp_secure_signaling) {
1153 if (sip_err_code)
1154 *sip_err_code = PJSIP_SC_NOT_ACCEPTABLE;
1155 return PJSIP_ESESSIONINSECURE;
1156 }
Benny Prijonod8179652008-01-23 20:39:07 +00001157 }
Benny Prijonod8179652008-01-23 20:39:07 +00001158
Benny Prijono53a7c702008-04-14 02:57:29 +00001159 /* Always create SRTP adapter */
1160 pjmedia_srtp_setting_default(&srtp_opt);
1161 srtp_opt.close_member_tp = PJ_FALSE;
Nanang Izzuddin4375f902008-06-26 19:12:09 +00001162 /* If media session has been ever established, let's use remote's
1163 * preference in SRTP usage policy, especially when it is stricter.
1164 */
1165 if (call->rem_srtp_use > acc->cfg.use_srtp)
1166 srtp_opt.use = call->rem_srtp_use;
1167 else
1168 srtp_opt.use = acc->cfg.use_srtp;
1169
Benny Prijono53a7c702008-04-14 02:57:29 +00001170 status = pjmedia_transport_srtp_create(pjsua_var.med_endpt,
1171 call->med_tp,
1172 &srtp_opt, &srtp);
1173 if (status != PJ_SUCCESS) {
1174 if (sip_err_code)
1175 *sip_err_code = PJSIP_SC_INTERNAL_SERVER_ERROR;
1176 return status;
1177 }
Benny Prijonod8179652008-01-23 20:39:07 +00001178
Benny Prijono53a7c702008-04-14 02:57:29 +00001179 /* Set SRTP as current media transport */
1180 call->med_orig = call->med_tp;
1181 call->med_tp = srtp;
1182 }
Benny Prijonod8179652008-01-23 20:39:07 +00001183#else
1184 call->med_orig = call->med_tp;
1185 PJ_UNUSED_ARG(security_level);
1186#endif
1187
Benny Prijonoa310bd22008-06-27 21:19:44 +00001188 /* Find out which media line in SDP that we support. If we are offerer,
1189 * audio will be at index 0 in SDP.
1190 */
1191 if (rem_sdp == 0) {
1192 call->audio_idx = 0;
1193 }
1194 /* Otherwise find out the candidate audio media line in SDP */
1195 else {
1196 unsigned i;
1197 pj_bool_t srtp_active;
1198
1199#if defined(PJMEDIA_HAS_SRTP) && (PJMEDIA_HAS_SRTP != 0)
1200 srtp_active = acc->cfg.use_srtp && srtp != NULL;
1201#else
1202 srtp_active = PJ_FALSE;
1203#endif
1204
1205 /* Media count must have been checked */
1206 pj_assert(rem_sdp->media_count != 0);
1207
1208 for (i=0; i<rem_sdp->media_count; ++i) {
1209 const pjmedia_sdp_media *m = rem_sdp->media[i];
1210
1211 /* Skip if media is not audio */
1212 if (pj_stricmp2(&m->desc.media, "audio") != 0)
1213 continue;
1214
1215 /* Skip if media is disabled */
1216 if (m->desc.port == 0)
1217 continue;
1218
1219 /* Skip if transport is not supported */
1220 if (pj_stricmp2(&m->desc.transport, "RTP/AVP") != 0 &&
1221 pj_stricmp2(&m->desc.transport, "RTP/SAVP") != 0)
1222 {
1223 continue;
1224 }
1225
1226 if (call->audio_idx == -1) {
1227 call->audio_idx = i;
1228 } else {
1229 /* We've found multiple candidates. This could happen
1230 * e.g. when remote is offering both RTP/AVP and RTP/AVP,
1231 * or when remote for some reason offers two audio.
1232 */
1233
1234 if (srtp_active &&
1235 pj_stricmp2(&m->desc.transport, "RTP/SAVP")==0)
1236 {
1237 /* Prefer RTP/SAVP when our media transport is SRTP */
1238 call->audio_idx = i;
1239 } else if (!srtp_active &&
1240 pj_stricmp2(&m->desc.transport, "RTP/AVP")==0)
1241 {
1242 /* Prefer RTP/AVP when our media transport is NOT SRTP */
1243 call->audio_idx = i;
1244 }
1245 }
1246 }
1247 }
1248
1249 /* Reject offer if we couldn't find a good m=audio line in offer */
1250 if (call->audio_idx < 0) {
Benny Prijonoab8dba92008-06-27 21:59:15 +00001251 if (sip_err_code) *sip_err_code = PJSIP_SC_NOT_ACCEPTABLE_HERE;
Benny Prijonoa310bd22008-06-27 21:19:44 +00001252 pjsua_media_channel_deinit(call_id);
Benny Prijonoab8dba92008-06-27 21:59:15 +00001253 return PJSIP_ERRNO_FROM_SIP_STATUS(PJSIP_SC_NOT_ACCEPTABLE_HERE);
Benny Prijonoa310bd22008-06-27 21:19:44 +00001254 }
1255
1256 PJ_LOG(4,(THIS_FILE, "Media index %d selected for call %d",
1257 call->audio_idx, call->index));
1258
Benny Prijono224b4e22008-06-19 14:10:28 +00001259 /* Create the media transport */
1260 status = pjmedia_transport_media_create(call->med_tp, tmp_pool, 0,
Benny Prijonoa310bd22008-06-27 21:19:44 +00001261 rem_sdp, call->audio_idx);
Benny Prijono224b4e22008-06-19 14:10:28 +00001262 if (status != PJ_SUCCESS) {
1263 if (sip_err_code) *sip_err_code = PJSIP_SC_NOT_ACCEPTABLE;
1264 pjsua_media_channel_deinit(call_id);
1265 return status;
1266 }
1267
1268 call->med_tp_st = PJSUA_MED_TP_INIT;
Benny Prijonoc97608e2007-03-23 16:34:20 +00001269 return PJ_SUCCESS;
1270}
1271
1272pj_status_t pjsua_media_channel_create_sdp(pjsua_call_id call_id,
1273 pj_pool_t *pool,
Benny Prijonod8179652008-01-23 20:39:07 +00001274 const pjmedia_sdp_session *rem_sdp,
Benny Prijono25b2ea12008-01-24 19:20:54 +00001275 pjmedia_sdp_session **p_sdp,
1276 int *sip_status_code)
Benny Prijonoc97608e2007-03-23 16:34:20 +00001277{
Benny Prijonoa310bd22008-06-27 21:19:44 +00001278 enum { MAX_MEDIA = 1 };
Benny Prijonoc97608e2007-03-23 16:34:20 +00001279 pjmedia_sdp_session *sdp;
Benny Prijonoe1a5a852008-03-11 21:38:05 +00001280 pjmedia_transport_info tpinfo;
Benny Prijonoc97608e2007-03-23 16:34:20 +00001281 pjsua_call *call = &pjsua_var.calls[call_id];
1282 pj_status_t status;
1283
Benny Prijono55e82352007-05-10 20:49:08 +00001284 /* Return error if media transport has not been created yet
1285 * (e.g. application is starting)
1286 */
1287 if (call->med_tp == NULL) {
1288 return PJ_EBUSY;
1289 }
1290
Benny Prijonoa310bd22008-06-27 21:19:44 +00001291 /* Media index must have been determined before */
1292 pj_assert(call->audio_idx != -1);
1293
Benny Prijono224b4e22008-06-19 14:10:28 +00001294 /* Create media if it's not created. This could happen when call is
1295 * currently on-hold
1296 */
1297 if (call->med_tp_st == PJSUA_MED_TP_IDLE) {
1298 pjsip_role_e role;
1299 role = (rem_sdp ? PJSIP_ROLE_UAS : PJSIP_ROLE_UAC);
1300 status = pjsua_media_channel_init(call_id, role, call->secure_level,
1301 pool, rem_sdp, sip_status_code);
1302 if (status != PJ_SUCCESS)
1303 return status;
1304 }
1305
Benny Prijono617c5bc2007-04-02 19:51:21 +00001306 /* Get media socket info */
Benny Prijono734fc2d2008-03-17 16:05:35 +00001307 pjmedia_transport_info_init(&tpinfo);
Benny Prijonoe1a5a852008-03-11 21:38:05 +00001308 pjmedia_transport_get_info(call->med_tp, &tpinfo);
Benny Prijono617c5bc2007-04-02 19:51:21 +00001309
1310 /* Create SDP */
Benny Prijonod8179652008-01-23 20:39:07 +00001311 status = pjmedia_endpt_create_sdp(pjsua_var.med_endpt, pool, MAX_MEDIA,
Benny Prijonoe1a5a852008-03-11 21:38:05 +00001312 &tpinfo.sock_info, &sdp);
Benny Prijono25b2ea12008-01-24 19:20:54 +00001313 if (status != PJ_SUCCESS) {
1314 if (sip_status_code) *sip_status_code = 500;
Benny Prijono224b4e22008-06-19 14:10:28 +00001315 return status;
Benny Prijono25b2ea12008-01-24 19:20:54 +00001316 }
Benny Prijonoc97608e2007-03-23 16:34:20 +00001317
Benny Prijonoa310bd22008-06-27 21:19:44 +00001318 /* If we're answering and the selected media is not the first media
1319 * in SDP, then fill in the unselected media with with zero port.
1320 * Otherwise we'll crash in transport_encode_sdp() because the media
1321 * lines are not aligned between offer and answer.
1322 */
1323 if (rem_sdp && call->audio_idx != 0) {
1324 unsigned i;
1325
1326 for (i=0; i<rem_sdp->media_count; ++i) {
1327 const pjmedia_sdp_media *rem_m = rem_sdp->media[i];
1328 pjmedia_sdp_media *m;
Benny Prijonoa310bd22008-06-27 21:19:44 +00001329
1330 if ((int)i == call->audio_idx)
1331 continue;
1332
Nanang Izzuddin16852b32010-06-04 13:41:34 +00001333 m = pjmedia_sdp_media_clone_deactivate(pool, rem_m);
Benny Prijonoa310bd22008-06-27 21:19:44 +00001334 if (i==sdp->media_count)
1335 sdp->media[sdp->media_count++] = m;
1336 else {
1337 pj_array_insert(sdp->media, sizeof(sdp->media[0]),
1338 sdp->media_count, i, &m);
1339 ++sdp->media_count;
1340 }
1341 }
1342 }
1343
Benny Prijono6ba8c542007-10-16 01:34:14 +00001344 /* Add NAT info in the SDP */
1345 if (pjsua_var.ua_cfg.nat_type_in_sdp) {
1346 pjmedia_sdp_attr *a;
1347 pj_str_t value;
1348 char nat_info[80];
1349
1350 value.ptr = nat_info;
1351 if (pjsua_var.ua_cfg.nat_type_in_sdp == 1) {
1352 value.slen = pj_ansi_snprintf(nat_info, sizeof(nat_info),
1353 "%d", pjsua_var.nat_type);
1354 } else {
1355 const char *type_name = pj_stun_get_nat_name(pjsua_var.nat_type);
1356 value.slen = pj_ansi_snprintf(nat_info, sizeof(nat_info),
1357 "%d %s",
1358 pjsua_var.nat_type,
1359 type_name);
1360 }
1361
1362 a = pjmedia_sdp_attr_create(pool, "X-nat", &value);
1363
1364 pjmedia_sdp_attr_add(&sdp->attr_count, sdp->attr, a);
1365
1366 }
1367
Benny Prijonod8179652008-01-23 20:39:07 +00001368 /* Give the SDP to media transport */
Benny Prijono224b4e22008-06-19 14:10:28 +00001369 status = pjmedia_transport_encode_sdp(call->med_tp, pool, sdp, rem_sdp,
Benny Prijonoa310bd22008-06-27 21:19:44 +00001370 call->audio_idx);
Benny Prijono25b2ea12008-01-24 19:20:54 +00001371 if (status != PJ_SUCCESS) {
1372 if (sip_status_code) *sip_status_code = PJSIP_SC_NOT_ACCEPTABLE;
Benny Prijono224b4e22008-06-19 14:10:28 +00001373 return status;
Benny Prijono25b2ea12008-01-24 19:20:54 +00001374 }
Benny Prijonoc97608e2007-03-23 16:34:20 +00001375
Nanang Izzuddind89cc3a2010-05-13 05:22:51 +00001376#if defined(PJMEDIA_HAS_SRTP) && (PJMEDIA_HAS_SRTP != 0)
1377 /* Check if SRTP is in optional mode and configured to use duplicated
1378 * media, i.e: secured and unsecured version, in the SDP offer.
1379 */
1380 if (!rem_sdp &&
1381 pjsua_var.acc[call->acc_id].cfg.use_srtp == PJMEDIA_SRTP_OPTIONAL &&
1382 pjsua_var.acc[call->acc_id].cfg.srtp_optional_dup_offer)
1383 {
1384 unsigned i;
1385
1386 for (i = 0; i < sdp->media_count; ++i) {
1387 pjmedia_sdp_media *m = sdp->media[i];
1388
1389 /* Check if this media is unsecured but has SDP "crypto"
1390 * attribute.
1391 */
1392 if (pj_stricmp2(&m->desc.transport, "RTP/AVP") == 0 &&
1393 pjmedia_sdp_media_find_attr2(m, "crypto", NULL) != NULL)
1394 {
1395 pjmedia_sdp_media *new_m;
1396
1397 /* Duplicate this media and apply secured transport */
1398 new_m = pjmedia_sdp_media_clone(pool, m);
1399 pj_strdup2(pool, &new_m->desc.transport, "RTP/SAVP");
1400
1401 /* Remove the "crypto" attribute in the unsecured media */
1402 pjmedia_sdp_media_remove_all_attr(m, "crypto");
1403
1404 /* Insert the new media before the unsecured media */
1405 if (sdp->media_count < PJMEDIA_MAX_SDP_MEDIA) {
1406 pj_array_insert(sdp->media, sizeof(new_m),
1407 sdp->media_count, i, &new_m);
1408 ++sdp->media_count;
1409 ++i;
1410 }
1411 }
1412 }
1413 }
1414#endif
1415
Benny Prijonof5d9f1f2009-10-14 13:13:18 +00001416 /* Update currently advertised RTP source address */
1417 pj_memcpy(&call->med_rtp_addr, &tpinfo.sock_info.rtp_addr_name,
1418 sizeof(pj_sockaddr));
1419
Benny Prijonoc97608e2007-03-23 16:34:20 +00001420 *p_sdp = sdp;
1421 return PJ_SUCCESS;
Benny Prijonoc97608e2007-03-23 16:34:20 +00001422}
1423
1424
1425static void stop_media_session(pjsua_call_id call_id)
1426{
1427 pjsua_call *call = &pjsua_var.calls[call_id];
1428
1429 if (call->conf_slot != PJSUA_INVALID_ID) {
Nanang Izzuddinfd461eb2008-06-09 09:35:59 +00001430 if (pjsua_var.mconf) {
Nanang Izzuddin148fd392008-06-16 09:52:50 +00001431 pjsua_conf_remove_port(call->conf_slot);
Nanang Izzuddinfd461eb2008-06-09 09:35:59 +00001432 }
Benny Prijonoc97608e2007-03-23 16:34:20 +00001433 call->conf_slot = PJSUA_INVALID_ID;
1434 }
1435
1436 if (call->session) {
Nanang Izzuddina815ceb2008-08-26 16:51:28 +00001437 pjmedia_rtcp_stat stat;
1438
Nanang Izzuddin8ea7eb02009-11-09 08:11:34 +00001439 if ((call->media_dir & PJMEDIA_DIR_ENCODING) &&
1440 (pjmedia_session_get_stream_stat(call->session, 0, &stat)
1441 == PJ_SUCCESS))
Nanang Izzuddina815ceb2008-08-26 16:51:28 +00001442 {
1443 /* Save RTP timestamp & sequence, so when media session is
1444 * restarted, those values will be restored as the initial
1445 * RTP timestamp & sequence of the new media session. So in
1446 * the same call session, RTP timestamp and sequence are
1447 * guaranteed to be contigue.
1448 */
1449 call->rtp_tx_seq_ts_set = 1 | (1 << 1);
1450 call->rtp_tx_seq = stat.rtp_tx_last_seq;
1451 call->rtp_tx_ts = stat.rtp_tx_last_ts;
1452 }
1453
Benny Prijonofc13bf62008-02-20 08:56:15 +00001454 if (pjsua_var.ua_cfg.cb.on_stream_destroyed) {
1455 pjsua_var.ua_cfg.cb.on_stream_destroyed(call_id, call->session, 0);
1456 }
1457
Benny Prijonoc97608e2007-03-23 16:34:20 +00001458 pjmedia_session_destroy(call->session);
1459 call->session = NULL;
1460
1461 PJ_LOG(4,(THIS_FILE, "Media session for call %d is destroyed",
1462 call_id));
1463
1464 }
1465
1466 call->media_st = PJSUA_CALL_MEDIA_NONE;
1467}
1468
1469pj_status_t pjsua_media_channel_deinit(pjsua_call_id call_id)
1470{
1471 pjsua_call *call = &pjsua_var.calls[call_id];
1472
1473 stop_media_session(call_id);
1474
Benny Prijono224b4e22008-06-19 14:10:28 +00001475 if (call->med_tp_st != PJSUA_MED_TP_IDLE) {
1476 pjmedia_transport_media_stop(call->med_tp);
1477 call->med_tp_st = PJSUA_MED_TP_IDLE;
1478 }
Benny Prijonoc97608e2007-03-23 16:34:20 +00001479
Benny Prijono311b63f2008-07-14 11:31:40 +00001480 if (call->med_orig && call->med_tp && call->med_tp != call->med_orig) {
Benny Prijonod8179652008-01-23 20:39:07 +00001481 pjmedia_transport_close(call->med_tp);
1482 call->med_tp = call->med_orig;
1483 }
Nanang Izzuddin670f71b2009-01-20 14:05:54 +00001484
1485 check_snd_dev_idle();
1486
Benny Prijonoc97608e2007-03-23 16:34:20 +00001487 return PJ_SUCCESS;
1488}
1489
1490
1491/*
1492 * DTMF callback from the stream.
1493 */
1494static void dtmf_callback(pjmedia_stream *strm, void *user_data,
1495 int digit)
1496{
1497 PJ_UNUSED_ARG(strm);
1498
Benny Prijono0c068262008-02-14 14:38:52 +00001499 /* For discussions about call mutex protection related to this
1500 * callback, please see ticket #460:
1501 * http://trac.pjsip.org/repos/ticket/460#comment:4
1502 */
Benny Prijonoc97608e2007-03-23 16:34:20 +00001503 if (pjsua_var.ua_cfg.cb.on_dtmf_digit) {
1504 pjsua_call_id call_id;
1505
Benny Prijonod8179652008-01-23 20:39:07 +00001506 call_id = (pjsua_call_id)(long)user_data;
Benny Prijonoc97608e2007-03-23 16:34:20 +00001507 pjsua_var.ua_cfg.cb.on_dtmf_digit(call_id, digit);
1508 }
1509}
1510
1511
1512pj_status_t pjsua_media_channel_update(pjsua_call_id call_id,
Benny Prijono224b4e22008-06-19 14:10:28 +00001513 const pjmedia_sdp_session *local_sdp,
Benny Prijonodbce2cf2007-03-28 16:24:00 +00001514 const pjmedia_sdp_session *remote_sdp)
Benny Prijonoc97608e2007-03-23 16:34:20 +00001515{
1516 int prev_media_st = 0;
1517 pjsua_call *call = &pjsua_var.calls[call_id];
1518 pjmedia_session_info sess_info;
Benny Prijono91e567e2007-12-28 08:51:58 +00001519 pjmedia_stream_info *si = NULL;
Benny Prijonoc97608e2007-03-23 16:34:20 +00001520 pjmedia_port *media_port;
Benny Prijonoc97608e2007-03-23 16:34:20 +00001521 pj_status_t status;
1522
Benny Prijono4e5c3f52010-04-29 12:11:51 +00001523 if (!pjsua_var.med_endpt) {
1524 /* We're being shutdown */
1525 return PJ_EBUSY;
1526 }
1527
Benny Prijonoc97608e2007-03-23 16:34:20 +00001528 /* Destroy existing media session, if any. */
1529 prev_media_st = call->media_st;
1530 stop_media_session(call->index);
1531
1532 /* Create media session info based on SDP parameters.
Benny Prijonoc97608e2007-03-23 16:34:20 +00001533 */
Benny Prijono40d62b62009-08-12 17:53:47 +00001534 status = pjmedia_session_info_from_sdp( call->inv->pool_prov,
Benny Prijonoc97608e2007-03-23 16:34:20 +00001535 pjsua_var.med_endpt,
Benny Prijono91e567e2007-12-28 08:51:58 +00001536 PJMEDIA_MAX_SDP_MEDIA, &sess_info,
Benny Prijonoc97608e2007-03-23 16:34:20 +00001537 local_sdp, remote_sdp);
1538 if (status != PJ_SUCCESS)
1539 return status;
1540
Benny Prijonoa310bd22008-06-27 21:19:44 +00001541 /* Find which session is audio */
1542 PJ_ASSERT_RETURN(call->audio_idx != -1, PJ_EBUG);
1543 PJ_ASSERT_RETURN(call->audio_idx < (int)sess_info.stream_cnt, PJ_EBUG);
1544 si = &sess_info.stream_info[call->audio_idx];
Benny Prijono91e567e2007-12-28 08:51:58 +00001545
1546 /* Reset session info with only one media stream */
1547 sess_info.stream_cnt = 1;
1548 if (si != &sess_info.stream_info[0])
1549 pj_memcpy(&sess_info.stream_info[0], si, sizeof(pjmedia_stream_info));
Benny Prijonoc97608e2007-03-23 16:34:20 +00001550
Nanang Izzuddin99d69522008-08-04 15:01:38 +00001551 /* Check if no media is active */
Benny Prijono91e567e2007-12-28 08:51:58 +00001552 if (sess_info.stream_cnt == 0 || si->dir == PJMEDIA_DIR_NONE)
Benny Prijonoc97608e2007-03-23 16:34:20 +00001553 {
Nanang Izzuddin99d69522008-08-04 15:01:38 +00001554 /* Call media state */
1555 call->media_st = PJSUA_CALL_MEDIA_NONE;
Benny Prijonoc97608e2007-03-23 16:34:20 +00001556
Nanang Izzuddin99d69522008-08-04 15:01:38 +00001557 /* Call media direction */
Benny Prijonoc97608e2007-03-23 16:34:20 +00001558 call->media_dir = PJMEDIA_DIR_NONE;
1559
Benny Prijonod8179652008-01-23 20:39:07 +00001560 /* Shutdown transport's session */
1561 pjmedia_transport_media_stop(call->med_tp);
Benny Prijono224b4e22008-06-19 14:10:28 +00001562 call->med_tp_st = PJSUA_MED_TP_IDLE;
Benny Prijono667952e2007-04-02 19:27:54 +00001563
Benny Prijonoc97608e2007-03-23 16:34:20 +00001564 /* No need because we need keepalive? */
1565
Benny Prijono68f9e4f2008-03-21 08:56:02 +00001566 /* Close upper entry of transport stack */
1567 if (call->med_orig && (call->med_tp != call->med_orig)) {
1568 pjmedia_transport_close(call->med_tp);
1569 call->med_tp = call->med_orig;
1570 }
1571
Benny Prijonoc97608e2007-03-23 16:34:20 +00001572 } else {
Nanang Izzuddin4375f902008-06-26 19:12:09 +00001573 pjmedia_transport_info tp_info;
1574
Benny Prijono224b4e22008-06-19 14:10:28 +00001575 /* Start/restart media transport */
Benny Prijonod8179652008-01-23 20:39:07 +00001576 status = pjmedia_transport_media_start(call->med_tp,
Benny Prijono40d62b62009-08-12 17:53:47 +00001577 call->inv->pool_prov,
Benny Prijonod8179652008-01-23 20:39:07 +00001578 local_sdp, remote_sdp, 0);
1579 if (status != PJ_SUCCESS)
1580 return status;
Benny Prijonoc97608e2007-03-23 16:34:20 +00001581
Benny Prijono224b4e22008-06-19 14:10:28 +00001582 call->med_tp_st = PJSUA_MED_TP_RUNNING;
1583
Nanang Izzuddin4375f902008-06-26 19:12:09 +00001584 /* Get remote SRTP usage policy */
1585 pjmedia_transport_info_init(&tp_info);
1586 pjmedia_transport_get_info(call->med_tp, &tp_info);
1587 if (tp_info.specific_info_cnt > 0) {
Benny Prijonof5d9f1f2009-10-14 13:13:18 +00001588 unsigned i;
Nanang Izzuddin4375f902008-06-26 19:12:09 +00001589 for (i = 0; i < tp_info.specific_info_cnt; ++i) {
1590 if (tp_info.spc_info[i].type == PJMEDIA_TRANSPORT_TYPE_SRTP)
1591 {
1592 pjmedia_srtp_info *srtp_info =
1593 (pjmedia_srtp_info*) tp_info.spc_info[i].buffer;
1594
1595 call->rem_srtp_use = srtp_info->peer_use;
1596 break;
1597 }
1598 }
1599 }
1600
Benny Prijonoc97608e2007-03-23 16:34:20 +00001601 /* Override ptime, if this option is specified. */
1602 if (pjsua_var.media_cfg.ptime != 0) {
Benny Prijono91e567e2007-12-28 08:51:58 +00001603 si->param->setting.frm_per_pkt = (pj_uint8_t)
1604 (pjsua_var.media_cfg.ptime / si->param->info.frm_ptime);
1605 if (si->param->setting.frm_per_pkt == 0)
1606 si->param->setting.frm_per_pkt = 1;
Benny Prijonoc97608e2007-03-23 16:34:20 +00001607 }
1608
1609 /* Disable VAD, if this option is specified. */
1610 if (pjsua_var.media_cfg.no_vad) {
Benny Prijono91e567e2007-12-28 08:51:58 +00001611 si->param->setting.vad = 0;
Benny Prijonoc97608e2007-03-23 16:34:20 +00001612 }
1613
1614
1615 /* Optionally, application may modify other stream settings here
1616 * (such as jitter buffer parameters, codec ptime, etc.)
1617 */
Benny Prijono91e567e2007-12-28 08:51:58 +00001618 si->jb_init = pjsua_var.media_cfg.jb_init;
1619 si->jb_min_pre = pjsua_var.media_cfg.jb_min_pre;
1620 si->jb_max_pre = pjsua_var.media_cfg.jb_max_pre;
1621 si->jb_max = pjsua_var.media_cfg.jb_max;
Benny Prijonoc97608e2007-03-23 16:34:20 +00001622
Benny Prijono8147f402007-11-21 14:50:07 +00001623 /* Set SSRC */
Benny Prijono91e567e2007-12-28 08:51:58 +00001624 si->ssrc = call->ssrc;
Benny Prijono8147f402007-11-21 14:50:07 +00001625
Nanang Izzuddina815ceb2008-08-26 16:51:28 +00001626 /* Set RTP timestamp & sequence, normally these value are intialized
1627 * automatically when stream session created, but for some cases (e.g:
1628 * call reinvite, call update) timestamp and sequence need to be kept
1629 * contigue.
1630 */
1631 si->rtp_ts = call->rtp_tx_ts;
1632 si->rtp_seq = call->rtp_tx_seq;
1633 si->rtp_seq_ts_set = call->rtp_tx_seq_ts_set;
1634
Benny Prijonoc97608e2007-03-23 16:34:20 +00001635 /* Create session based on session info. */
1636 status = pjmedia_session_create( pjsua_var.med_endpt, &sess_info,
1637 &call->med_tp,
1638 call, &call->session );
1639 if (status != PJ_SUCCESS) {
1640 return status;
1641 }
1642
1643 /* If DTMF callback is installed by application, install our
1644 * callback to the session.
1645 */
1646 if (pjsua_var.ua_cfg.cb.on_dtmf_digit) {
1647 pjmedia_session_set_dtmf_callback(call->session, 0,
1648 &dtmf_callback,
Benny Prijonod8179652008-01-23 20:39:07 +00001649 (void*)(long)(call->index));
Benny Prijonoc97608e2007-03-23 16:34:20 +00001650 }
1651
1652 /* Get the port interface of the first stream in the session.
1653 * We need the port interface to add to the conference bridge.
1654 */
1655 pjmedia_session_get_port(call->session, 0, &media_port);
1656
Benny Prijonofc13bf62008-02-20 08:56:15 +00001657 /* Notify application about stream creation.
1658 * Note: application may modify media_port to point to different
1659 * media port
1660 */
1661 if (pjsua_var.ua_cfg.cb.on_stream_created) {
1662 pjsua_var.ua_cfg.cb.on_stream_created(call_id, call->session,
1663 0, &media_port);
1664 }
Benny Prijonoc97608e2007-03-23 16:34:20 +00001665
1666 /*
1667 * Add the call to conference bridge.
1668 */
Benny Prijono38fb3ea2008-01-02 08:27:03 +00001669 {
1670 char tmp[PJSIP_MAX_URL_SIZE];
1671 pj_str_t port_name;
1672
1673 port_name.ptr = tmp;
1674 port_name.slen = pjsip_uri_print(PJSIP_URI_IN_REQ_URI,
1675 call->inv->dlg->remote.info->uri,
1676 tmp, sizeof(tmp));
1677 if (port_name.slen < 1) {
1678 port_name = pj_str("call");
1679 }
Benny Prijono40d62b62009-08-12 17:53:47 +00001680 status = pjmedia_conf_add_port( pjsua_var.mconf,
1681 call->inv->pool_prov,
Benny Prijono38fb3ea2008-01-02 08:27:03 +00001682 media_port,
1683 &port_name,
1684 (unsigned*)&call->conf_slot);
1685 if (status != PJ_SUCCESS) {
1686 return status;
1687 }
Benny Prijonoc97608e2007-03-23 16:34:20 +00001688 }
1689
Nanang Izzuddin99d69522008-08-04 15:01:38 +00001690 /* Call media direction */
Benny Prijono91e567e2007-12-28 08:51:58 +00001691 call->media_dir = si->dir;
Nanang Izzuddin99d69522008-08-04 15:01:38 +00001692
1693 /* Call media state */
1694 if (call->local_hold)
1695 call->media_st = PJSUA_CALL_MEDIA_LOCAL_HOLD;
1696 else if (call->media_dir == PJMEDIA_DIR_DECODING)
1697 call->media_st = PJSUA_CALL_MEDIA_REMOTE_HOLD;
1698 else
1699 call->media_st = PJSUA_CALL_MEDIA_ACTIVE;
Benny Prijonoc97608e2007-03-23 16:34:20 +00001700 }
1701
1702 /* Print info. */
1703 {
1704 char info[80];
1705 int info_len = 0;
1706 unsigned i;
1707
1708 for (i=0; i<sess_info.stream_cnt; ++i) {
1709 int len;
1710 const char *dir;
1711 pjmedia_stream_info *strm_info = &sess_info.stream_info[i];
1712
1713 switch (strm_info->dir) {
1714 case PJMEDIA_DIR_NONE:
1715 dir = "inactive";
1716 break;
1717 case PJMEDIA_DIR_ENCODING:
1718 dir = "sendonly";
1719 break;
1720 case PJMEDIA_DIR_DECODING:
1721 dir = "recvonly";
1722 break;
1723 case PJMEDIA_DIR_ENCODING_DECODING:
1724 dir = "sendrecv";
1725 break;
1726 default:
1727 dir = "unknown";
1728 break;
1729 }
1730 len = pj_ansi_sprintf( info+info_len,
1731 ", stream #%d: %.*s (%s)", i,
1732 (int)strm_info->fmt.encoding_name.slen,
1733 strm_info->fmt.encoding_name.ptr,
1734 dir);
1735 if (len > 0)
1736 info_len += len;
1737 }
1738 PJ_LOG(4,(THIS_FILE,"Media updates%s", info));
1739 }
1740
1741 return PJ_SUCCESS;
1742}
1743
Benny Prijonoeebe9af2006-06-13 22:57:13 +00001744/*
1745 * Get maxinum number of conference ports.
1746 */
1747PJ_DEF(unsigned) pjsua_conf_get_max_ports(void)
1748{
1749 return pjsua_var.media_cfg.max_media_ports;
1750}
1751
1752
1753/*
1754 * Get current number of active ports in the bridge.
1755 */
1756PJ_DEF(unsigned) pjsua_conf_get_active_ports(void)
1757{
Benny Prijono38fb3ea2008-01-02 08:27:03 +00001758 unsigned ports[PJSUA_MAX_CONF_PORTS];
Benny Prijonoeebe9af2006-06-13 22:57:13 +00001759 unsigned count = PJ_ARRAY_SIZE(ports);
1760 pj_status_t status;
1761
1762 status = pjmedia_conf_enum_ports(pjsua_var.mconf, ports, &count);
1763 if (status != PJ_SUCCESS)
1764 count = 0;
1765
1766 return count;
1767}
1768
1769
1770/*
1771 * Enumerate all conference ports.
1772 */
1773PJ_DEF(pj_status_t) pjsua_enum_conf_ports(pjsua_conf_port_id id[],
1774 unsigned *count)
1775{
1776 return pjmedia_conf_enum_ports(pjsua_var.mconf, (unsigned*)id, count);
1777}
1778
1779
1780/*
1781 * Get information about the specified conference port
1782 */
1783PJ_DEF(pj_status_t) pjsua_conf_get_port_info( pjsua_conf_port_id id,
1784 pjsua_conf_port_info *info)
1785{
1786 pjmedia_conf_port_info cinfo;
Benny Prijono0498d902006-06-19 14:49:14 +00001787 unsigned i;
Benny Prijonoeebe9af2006-06-13 22:57:13 +00001788 pj_status_t status;
1789
1790 status = pjmedia_conf_get_port_info( pjsua_var.mconf, id, &cinfo);
1791 if (status != PJ_SUCCESS)
1792 return status;
1793
Benny Prijonoac623b32006-07-03 15:19:31 +00001794 pj_bzero(info, sizeof(*info));
Benny Prijonoeebe9af2006-06-13 22:57:13 +00001795 info->slot_id = id;
1796 info->name = cinfo.name;
1797 info->clock_rate = cinfo.clock_rate;
1798 info->channel_count = cinfo.channel_count;
1799 info->samples_per_frame = cinfo.samples_per_frame;
1800 info->bits_per_sample = cinfo.bits_per_sample;
1801
1802 /* Build array of listeners */
Benny Prijonoc78c3a32006-06-16 15:54:43 +00001803 info->listener_cnt = cinfo.listener_cnt;
1804 for (i=0; i<cinfo.listener_cnt; ++i) {
1805 info->listeners[i] = cinfo.listener_slots[i];
Benny Prijonoeebe9af2006-06-13 22:57:13 +00001806 }
1807
1808 return PJ_SUCCESS;
1809}
1810
1811
1812/*
Benny Prijonoe909eac2006-07-27 22:04:56 +00001813 * Add arbitrary media port to PJSUA's conference bridge.
1814 */
1815PJ_DEF(pj_status_t) pjsua_conf_add_port( pj_pool_t *pool,
1816 pjmedia_port *port,
1817 pjsua_conf_port_id *p_id)
1818{
1819 pj_status_t status;
1820
1821 status = pjmedia_conf_add_port(pjsua_var.mconf, pool,
1822 port, NULL, (unsigned*)p_id);
1823 if (status != PJ_SUCCESS) {
1824 if (p_id)
1825 *p_id = PJSUA_INVALID_ID;
1826 }
1827
1828 return status;
1829}
1830
1831
1832/*
1833 * Remove arbitrary slot from the conference bridge.
1834 */
1835PJ_DEF(pj_status_t) pjsua_conf_remove_port(pjsua_conf_port_id id)
1836{
Nanang Izzuddin148fd392008-06-16 09:52:50 +00001837 pj_status_t status;
1838
1839 status = pjmedia_conf_remove_port(pjsua_var.mconf, (unsigned)id);
1840 check_snd_dev_idle();
1841
1842 return status;
Benny Prijonoe909eac2006-07-27 22:04:56 +00001843}
1844
1845
1846/*
Benny Prijonoeebe9af2006-06-13 22:57:13 +00001847 * Establish unidirectional media flow from souce to sink.
1848 */
1849PJ_DEF(pj_status_t) pjsua_conf_connect( pjsua_conf_port_id source,
1850 pjsua_conf_port_id sink)
1851{
Nanang Izzuddin68559c32008-06-13 17:01:46 +00001852 /* If sound device idle timer is active, cancel it first. */
Benny Prijono0f711b42009-05-06 19:08:43 +00001853 PJSUA_LOCK();
Nanang Izzuddin68559c32008-06-13 17:01:46 +00001854 if (pjsua_var.snd_idle_timer.id) {
1855 pjsip_endpt_cancel_timer(pjsua_var.endpt, &pjsua_var.snd_idle_timer);
1856 pjsua_var.snd_idle_timer.id = PJ_FALSE;
1857 }
Benny Prijono0f711b42009-05-06 19:08:43 +00001858 PJSUA_UNLOCK();
Nanang Izzuddin68559c32008-06-13 17:01:46 +00001859
Nanang Izzuddinfe02a062009-02-18 14:28:49 +00001860
Benny Prijonof798e502009-03-09 13:08:16 +00001861 /* For audio switchboard (i.e. APS-Direct):
1862 * Check if sound device need to be reopened, i.e: its attributes
Nanang Izzuddinfe02a062009-02-18 14:28:49 +00001863 * (format, clock rate, channel count) must match to peer's.
1864 * Note that sound device can be reopened only if it doesn't have
1865 * any connection.
1866 */
Benny Prijonof798e502009-03-09 13:08:16 +00001867 if (pjsua_var.is_mswitch) {
Nanang Izzuddinfe02a062009-02-18 14:28:49 +00001868 pjmedia_conf_port_info port0_info;
1869 pjmedia_conf_port_info peer_info;
1870 unsigned peer_id;
1871 pj_bool_t need_reopen = PJ_FALSE;
1872 pj_status_t status;
1873
1874 peer_id = (source!=0)? source : sink;
1875 status = pjmedia_conf_get_port_info(pjsua_var.mconf, peer_id,
1876 &peer_info);
1877 pj_assert(status == PJ_SUCCESS);
1878
1879 status = pjmedia_conf_get_port_info(pjsua_var.mconf, 0, &port0_info);
1880 pj_assert(status == PJ_SUCCESS);
1881
1882 /* Check if sound device is instantiated. */
1883 need_reopen = (pjsua_var.snd_port==NULL && pjsua_var.null_snd==NULL &&
1884 !pjsua_var.no_snd);
1885
1886 /* Check if sound device need to reopen because it needs to modify
1887 * settings to match its peer. Sound device must be idle in this case
1888 * though.
1889 */
1890 if (!need_reopen &&
1891 port0_info.listener_cnt==0 && port0_info.transmitter_cnt==0)
1892 {
1893 need_reopen = (peer_info.format.id != port0_info.format.id ||
1894 peer_info.format.bitrate != port0_info.format.bitrate ||
1895 peer_info.clock_rate != port0_info.clock_rate ||
1896 peer_info.channel_count != port0_info.channel_count);
1897 }
1898
1899 if (need_reopen) {
Benny Prijonod65f78c2009-06-03 18:59:37 +00001900 if (pjsua_var.cap_dev != NULL_SND_DEV_ID) {
1901 pjmedia_aud_param param;
Nanang Izzuddinfe02a062009-02-18 14:28:49 +00001902
Benny Prijonod65f78c2009-06-03 18:59:37 +00001903 /* Create parameter based on peer info */
1904 status = create_aud_param(&param, pjsua_var.cap_dev,
1905 pjsua_var.play_dev,
1906 peer_info.clock_rate,
1907 peer_info.channel_count,
1908 peer_info.samples_per_frame,
1909 peer_info.bits_per_sample);
1910 if (status != PJ_SUCCESS) {
1911 pjsua_perror(THIS_FILE, "Error opening sound device", status);
1912 return status;
1913 }
Benny Prijonof798e502009-03-09 13:08:16 +00001914
Benny Prijonod65f78c2009-06-03 18:59:37 +00001915 /* And peer format */
1916 if (peer_info.format.id != PJMEDIA_FORMAT_PCM) {
1917 param.flags |= PJMEDIA_AUD_DEV_CAP_EXT_FORMAT;
1918 param.ext_fmt = peer_info.format;
1919 }
Benny Prijono10454dc2009-02-21 14:21:59 +00001920
Benny Prijonod65f78c2009-06-03 18:59:37 +00001921 status = open_snd_dev(&param);
1922 if (status != PJ_SUCCESS) {
1923 pjsua_perror(THIS_FILE, "Error opening sound device", status);
1924 return status;
1925 }
1926 } else {
1927 /* Null-audio */
1928 status = pjsua_set_snd_dev(pjsua_var.cap_dev, pjsua_var.play_dev);
1929 if (status != PJ_SUCCESS) {
1930 pjsua_perror(THIS_FILE, "Error opening sound device", status);
1931 return status;
1932 }
Nanang Izzuddinfe02a062009-02-18 14:28:49 +00001933 }
1934 }
Nanang Izzuddinfe02a062009-02-18 14:28:49 +00001935
Benny Prijonof798e502009-03-09 13:08:16 +00001936 } else {
1937 /* The bridge version */
Nanang Izzuddinfe02a062009-02-18 14:28:49 +00001938
Benny Prijonof798e502009-03-09 13:08:16 +00001939 /* Create sound port if none is instantiated */
1940 if (pjsua_var.snd_port==NULL && pjsua_var.null_snd==NULL &&
1941 !pjsua_var.no_snd)
1942 {
1943 pj_status_t status;
Nanang Izzuddin68559c32008-06-13 17:01:46 +00001944
Benny Prijonof798e502009-03-09 13:08:16 +00001945 status = pjsua_set_snd_dev(pjsua_var.cap_dev, pjsua_var.play_dev);
1946 if (status != PJ_SUCCESS) {
1947 pjsua_perror(THIS_FILE, "Error opening sound device", status);
1948 return status;
1949 }
Nanang Izzuddin68559c32008-06-13 17:01:46 +00001950 }
Nanang Izzuddin68559c32008-06-13 17:01:46 +00001951
Benny Prijonof798e502009-03-09 13:08:16 +00001952 }
Nanang Izzuddinfe02a062009-02-18 14:28:49 +00001953
Benny Prijonoeebe9af2006-06-13 22:57:13 +00001954 return pjmedia_conf_connect_port(pjsua_var.mconf, source, sink, 0);
1955}
1956
1957
1958/*
1959 * Disconnect media flow from the source to destination port.
1960 */
1961PJ_DEF(pj_status_t) pjsua_conf_disconnect( pjsua_conf_port_id source,
1962 pjsua_conf_port_id sink)
1963{
Nanang Izzuddin68559c32008-06-13 17:01:46 +00001964 pj_status_t status;
1965
1966 status = pjmedia_conf_disconnect_port(pjsua_var.mconf, source, sink);
Nanang Izzuddin148fd392008-06-16 09:52:50 +00001967 check_snd_dev_idle();
Nanang Izzuddin68559c32008-06-13 17:01:46 +00001968
1969 return status;
Benny Prijonoeebe9af2006-06-13 22:57:13 +00001970}
1971
1972
Benny Prijono6dd967c2006-12-26 02:27:14 +00001973/*
1974 * Adjust the signal level to be transmitted from the bridge to the
1975 * specified port by making it louder or quieter.
1976 */
1977PJ_DEF(pj_status_t) pjsua_conf_adjust_tx_level(pjsua_conf_port_id slot,
1978 float level)
1979{
1980 return pjmedia_conf_adjust_tx_level(pjsua_var.mconf, slot,
1981 (int)((level-1) * 128));
1982}
1983
1984/*
1985 * Adjust the signal level to be received from the specified port (to
1986 * the bridge) by making it louder or quieter.
1987 */
1988PJ_DEF(pj_status_t) pjsua_conf_adjust_rx_level(pjsua_conf_port_id slot,
1989 float level)
1990{
1991 return pjmedia_conf_adjust_rx_level(pjsua_var.mconf, slot,
1992 (int)((level-1) * 128));
1993}
1994
1995
1996/*
1997 * Get last signal level transmitted to or received from the specified port.
1998 */
1999PJ_DEF(pj_status_t) pjsua_conf_get_signal_level(pjsua_conf_port_id slot,
2000 unsigned *tx_level,
2001 unsigned *rx_level)
2002{
2003 return pjmedia_conf_get_signal_level(pjsua_var.mconf, slot,
2004 tx_level, rx_level);
2005}
2006
Benny Prijonoeebe9af2006-06-13 22:57:13 +00002007/*****************************************************************************
2008 * File player.
2009 */
2010
Benny Prijonod5696da2007-07-17 16:25:45 +00002011static char* get_basename(const char *path, unsigned len)
2012{
2013 char *p = ((char*)path) + len;
2014
2015 if (len==0)
2016 return p;
2017
Benny Prijono1f61a8f2007-08-16 10:11:44 +00002018 for (--p; p!=path && *p!='/' && *p!='\\'; ) --p;
Benny Prijonod5696da2007-07-17 16:25:45 +00002019
2020 return (p==path) ? p : p+1;
2021}
2022
2023
Benny Prijonoeebe9af2006-06-13 22:57:13 +00002024/*
2025 * Create a file player, and automatically connect this player to
2026 * the conference bridge.
2027 */
2028PJ_DEF(pj_status_t) pjsua_player_create( const pj_str_t *filename,
2029 unsigned options,
Benny Prijonoeebe9af2006-06-13 22:57:13 +00002030 pjsua_player_id *p_id)
2031{
2032 unsigned slot, file_id;
Benny Prijonoa66c3312007-01-21 23:12:40 +00002033 char path[PJ_MAXPATH];
Benny Prijonod5696da2007-07-17 16:25:45 +00002034 pj_pool_t *pool;
Benny Prijonoeebe9af2006-06-13 22:57:13 +00002035 pjmedia_port *port;
2036 pj_status_t status;
2037
2038 if (pjsua_var.player_cnt >= PJ_ARRAY_SIZE(pjsua_var.player))
2039 return PJ_ETOOMANY;
2040
2041 PJSUA_LOCK();
2042
2043 for (file_id=0; file_id<PJ_ARRAY_SIZE(pjsua_var.player); ++file_id) {
2044 if (pjsua_var.player[file_id].port == NULL)
2045 break;
2046 }
2047
2048 if (file_id == PJ_ARRAY_SIZE(pjsua_var.player)) {
2049 /* This is unexpected */
2050 PJSUA_UNLOCK();
2051 pj_assert(0);
2052 return PJ_EBUG;
2053 }
2054
2055 pj_memcpy(path, filename->ptr, filename->slen);
2056 path[filename->slen] = '\0';
Benny Prijonod5696da2007-07-17 16:25:45 +00002057
2058 pool = pjsua_pool_create(get_basename(path, filename->slen), 1000, 1000);
2059 if (!pool) {
2060 PJSUA_UNLOCK();
2061 return PJ_ENOMEM;
2062 }
2063
Nanang Izzuddin81e9bd52008-06-27 12:52:51 +00002064 status = pjmedia_wav_player_port_create(
2065 pool, path,
2066 pjsua_var.mconf_cfg.samples_per_frame *
2067 1000 / pjsua_var.media_cfg.channel_count /
2068 pjsua_var.media_cfg.clock_rate,
2069 options, 0, &port);
Benny Prijonoeebe9af2006-06-13 22:57:13 +00002070 if (status != PJ_SUCCESS) {
2071 PJSUA_UNLOCK();
2072 pjsua_perror(THIS_FILE, "Unable to open file for playback", status);
Benny Prijonod5696da2007-07-17 16:25:45 +00002073 pj_pool_release(pool);
Benny Prijonoeebe9af2006-06-13 22:57:13 +00002074 return status;
2075 }
2076
Benny Prijono5297af92008-03-18 13:40:40 +00002077 status = pjmedia_conf_add_port(pjsua_var.mconf, pool,
Benny Prijonoeebe9af2006-06-13 22:57:13 +00002078 port, filename, &slot);
2079 if (status != PJ_SUCCESS) {
2080 pjmedia_port_destroy(port);
2081 PJSUA_UNLOCK();
Benny Prijono32e4f492007-01-24 00:44:26 +00002082 pjsua_perror(THIS_FILE, "Unable to add file to conference bridge",
2083 status);
Benny Prijonod5696da2007-07-17 16:25:45 +00002084 pj_pool_release(pool);
Benny Prijonoeebe9af2006-06-13 22:57:13 +00002085 return status;
2086 }
2087
Benny Prijonoa66c3312007-01-21 23:12:40 +00002088 pjsua_var.player[file_id].type = 0;
Benny Prijonod5696da2007-07-17 16:25:45 +00002089 pjsua_var.player[file_id].pool = pool;
Benny Prijonoeebe9af2006-06-13 22:57:13 +00002090 pjsua_var.player[file_id].port = port;
2091 pjsua_var.player[file_id].slot = slot;
2092
2093 if (p_id) *p_id = file_id;
2094
2095 ++pjsua_var.player_cnt;
2096
2097 PJSUA_UNLOCK();
2098 return PJ_SUCCESS;
2099}
2100
2101
2102/*
Benny Prijonoa66c3312007-01-21 23:12:40 +00002103 * Create a file playlist media port, and automatically add the port
2104 * to the conference bridge.
2105 */
2106PJ_DEF(pj_status_t) pjsua_playlist_create( const pj_str_t file_names[],
2107 unsigned file_count,
2108 const pj_str_t *label,
2109 unsigned options,
2110 pjsua_player_id *p_id)
2111{
2112 unsigned slot, file_id, ptime;
Benny Prijonod5696da2007-07-17 16:25:45 +00002113 pj_pool_t *pool;
Benny Prijonoa66c3312007-01-21 23:12:40 +00002114 pjmedia_port *port;
2115 pj_status_t status;
2116
2117 if (pjsua_var.player_cnt >= PJ_ARRAY_SIZE(pjsua_var.player))
2118 return PJ_ETOOMANY;
2119
2120 PJSUA_LOCK();
2121
2122 for (file_id=0; file_id<PJ_ARRAY_SIZE(pjsua_var.player); ++file_id) {
2123 if (pjsua_var.player[file_id].port == NULL)
2124 break;
2125 }
2126
2127 if (file_id == PJ_ARRAY_SIZE(pjsua_var.player)) {
2128 /* This is unexpected */
2129 PJSUA_UNLOCK();
2130 pj_assert(0);
2131 return PJ_EBUG;
2132 }
2133
2134
2135 ptime = pjsua_var.mconf_cfg.samples_per_frame * 1000 /
2136 pjsua_var.media_cfg.clock_rate;
2137
Benny Prijonod5696da2007-07-17 16:25:45 +00002138 pool = pjsua_pool_create("playlist", 1000, 1000);
2139 if (!pool) {
2140 PJSUA_UNLOCK();
2141 return PJ_ENOMEM;
2142 }
2143
2144 status = pjmedia_wav_playlist_create(pool, label,
Benny Prijonoa66c3312007-01-21 23:12:40 +00002145 file_names, file_count,
2146 ptime, options, 0, &port);
2147 if (status != PJ_SUCCESS) {
2148 PJSUA_UNLOCK();
2149 pjsua_perror(THIS_FILE, "Unable to create playlist", status);
Benny Prijonod5696da2007-07-17 16:25:45 +00002150 pj_pool_release(pool);
Benny Prijonoa66c3312007-01-21 23:12:40 +00002151 return status;
2152 }
2153
Benny Prijonod5696da2007-07-17 16:25:45 +00002154 status = pjmedia_conf_add_port(pjsua_var.mconf, pool,
Benny Prijonoa66c3312007-01-21 23:12:40 +00002155 port, &port->info.name, &slot);
2156 if (status != PJ_SUCCESS) {
2157 pjmedia_port_destroy(port);
2158 PJSUA_UNLOCK();
2159 pjsua_perror(THIS_FILE, "Unable to add port", status);
Benny Prijonod5696da2007-07-17 16:25:45 +00002160 pj_pool_release(pool);
Benny Prijonoa66c3312007-01-21 23:12:40 +00002161 return status;
2162 }
2163
2164 pjsua_var.player[file_id].type = 1;
Benny Prijonod5696da2007-07-17 16:25:45 +00002165 pjsua_var.player[file_id].pool = pool;
Benny Prijonoa66c3312007-01-21 23:12:40 +00002166 pjsua_var.player[file_id].port = port;
2167 pjsua_var.player[file_id].slot = slot;
2168
2169 if (p_id) *p_id = file_id;
2170
2171 ++pjsua_var.player_cnt;
2172
2173 PJSUA_UNLOCK();
2174 return PJ_SUCCESS;
2175
2176}
2177
2178
2179/*
Benny Prijonoeebe9af2006-06-13 22:57:13 +00002180 * Get conference port ID associated with player.
2181 */
2182PJ_DEF(pjsua_conf_port_id) pjsua_player_get_conf_port(pjsua_player_id id)
2183{
Benny Prijonoa1e69682007-05-11 15:14:34 +00002184 PJ_ASSERT_RETURN(id>=0&&id<(int)PJ_ARRAY_SIZE(pjsua_var.player), PJ_EINVAL);
Benny Prijonoeebe9af2006-06-13 22:57:13 +00002185 PJ_ASSERT_RETURN(pjsua_var.player[id].port != NULL, PJ_EINVAL);
2186
2187 return pjsua_var.player[id].slot;
2188}
2189
Benny Prijono469b1522006-12-26 03:05:17 +00002190/*
2191 * Get the media port for the player.
2192 */
Benny Prijonobe41d862008-01-18 13:24:28 +00002193PJ_DEF(pj_status_t) pjsua_player_get_port( pjsua_player_id id,
Benny Prijono469b1522006-12-26 03:05:17 +00002194 pjmedia_port **p_port)
2195{
Benny Prijonoa1e69682007-05-11 15:14:34 +00002196 PJ_ASSERT_RETURN(id>=0&&id<(int)PJ_ARRAY_SIZE(pjsua_var.player), PJ_EINVAL);
Benny Prijono469b1522006-12-26 03:05:17 +00002197 PJ_ASSERT_RETURN(pjsua_var.player[id].port != NULL, PJ_EINVAL);
2198 PJ_ASSERT_RETURN(p_port != NULL, PJ_EINVAL);
2199
2200 *p_port = pjsua_var.player[id].port;
2201
2202 return PJ_SUCCESS;
2203}
Benny Prijonoeebe9af2006-06-13 22:57:13 +00002204
2205/*
2206 * Set playback position.
2207 */
2208PJ_DEF(pj_status_t) pjsua_player_set_pos( pjsua_player_id id,
2209 pj_uint32_t samples)
2210{
Benny Prijonoa1e69682007-05-11 15:14:34 +00002211 PJ_ASSERT_RETURN(id>=0&&id<(int)PJ_ARRAY_SIZE(pjsua_var.player), PJ_EINVAL);
Benny Prijonoeebe9af2006-06-13 22:57:13 +00002212 PJ_ASSERT_RETURN(pjsua_var.player[id].port != NULL, PJ_EINVAL);
Benny Prijonoa66c3312007-01-21 23:12:40 +00002213 PJ_ASSERT_RETURN(pjsua_var.player[id].type == 0, PJ_EINVAL);
Benny Prijonoeebe9af2006-06-13 22:57:13 +00002214
2215 return pjmedia_wav_player_port_set_pos(pjsua_var.player[id].port, samples);
2216}
2217
2218
2219/*
2220 * Close the file, remove the player from the bridge, and free
2221 * resources associated with the file player.
2222 */
2223PJ_DEF(pj_status_t) pjsua_player_destroy(pjsua_player_id id)
2224{
Benny Prijonoa1e69682007-05-11 15:14:34 +00002225 PJ_ASSERT_RETURN(id>=0&&id<(int)PJ_ARRAY_SIZE(pjsua_var.player), PJ_EINVAL);
Benny Prijonoeebe9af2006-06-13 22:57:13 +00002226 PJ_ASSERT_RETURN(pjsua_var.player[id].port != NULL, PJ_EINVAL);
2227
2228 PJSUA_LOCK();
2229
2230 if (pjsua_var.player[id].port) {
Nanang Izzuddin148fd392008-06-16 09:52:50 +00002231 pjsua_conf_remove_port(pjsua_var.player[id].slot);
Benny Prijonoeebe9af2006-06-13 22:57:13 +00002232 pjmedia_port_destroy(pjsua_var.player[id].port);
2233 pjsua_var.player[id].port = NULL;
2234 pjsua_var.player[id].slot = 0xFFFF;
Benny Prijonod5696da2007-07-17 16:25:45 +00002235 pj_pool_release(pjsua_var.player[id].pool);
2236 pjsua_var.player[id].pool = NULL;
Benny Prijonoeebe9af2006-06-13 22:57:13 +00002237 pjsua_var.player_cnt--;
2238 }
2239
2240 PJSUA_UNLOCK();
2241
2242 return PJ_SUCCESS;
2243}
2244
2245
2246/*****************************************************************************
2247 * File recorder.
2248 */
2249
2250/*
2251 * Create a file recorder, and automatically connect this recorder to
2252 * the conference bridge.
2253 */
2254PJ_DEF(pj_status_t) pjsua_recorder_create( const pj_str_t *filename,
Benny Prijono8f310522006-10-20 11:08:49 +00002255 unsigned enc_type,
2256 void *enc_param,
Benny Prijonoeebe9af2006-06-13 22:57:13 +00002257 pj_ssize_t max_size,
2258 unsigned options,
Benny Prijonoeebe9af2006-06-13 22:57:13 +00002259 pjsua_recorder_id *p_id)
2260{
Benny Prijonob3cdb2b2006-10-19 15:49:47 +00002261 enum Format
2262 {
2263 FMT_UNKNOWN,
2264 FMT_WAV,
2265 FMT_MP3,
2266 };
Benny Prijonoeebe9af2006-06-13 22:57:13 +00002267 unsigned slot, file_id;
Benny Prijonoa66c3312007-01-21 23:12:40 +00002268 char path[PJ_MAXPATH];
Benny Prijonob3cdb2b2006-10-19 15:49:47 +00002269 pj_str_t ext;
Benny Prijono8f310522006-10-20 11:08:49 +00002270 int file_format;
Benny Prijonod5696da2007-07-17 16:25:45 +00002271 pj_pool_t *pool;
Benny Prijonoeebe9af2006-06-13 22:57:13 +00002272 pjmedia_port *port;
2273 pj_status_t status;
2274
Benny Prijonob3cdb2b2006-10-19 15:49:47 +00002275 /* Filename must present */
2276 PJ_ASSERT_RETURN(filename != NULL, PJ_EINVAL);
2277
Benny Prijono00cae612006-07-31 15:19:36 +00002278 /* Don't support max_size at present */
Benny Prijonob3cdb2b2006-10-19 15:49:47 +00002279 PJ_ASSERT_RETURN(max_size == 0 || max_size == -1, PJ_EINVAL);
Benny Prijono00cae612006-07-31 15:19:36 +00002280
Benny Prijono8f310522006-10-20 11:08:49 +00002281 /* Don't support encoding type at present */
2282 PJ_ASSERT_RETURN(enc_type == 0, PJ_EINVAL);
Benny Prijono00cae612006-07-31 15:19:36 +00002283
Benny Prijonoeebe9af2006-06-13 22:57:13 +00002284 if (pjsua_var.rec_cnt >= PJ_ARRAY_SIZE(pjsua_var.recorder))
2285 return PJ_ETOOMANY;
2286
Benny Prijonob3cdb2b2006-10-19 15:49:47 +00002287 /* Determine the file format */
2288 ext.ptr = filename->ptr + filename->slen - 4;
2289 ext.slen = 4;
2290
2291 if (pj_stricmp2(&ext, ".wav") == 0)
2292 file_format = FMT_WAV;
2293 else if (pj_stricmp2(&ext, ".mp3") == 0)
2294 file_format = FMT_MP3;
2295 else {
2296 PJ_LOG(1,(THIS_FILE, "pjsua_recorder_create() error: unable to "
2297 "determine file format for %.*s",
2298 (int)filename->slen, filename->ptr));
2299 return PJ_ENOTSUP;
2300 }
2301
Benny Prijonoeebe9af2006-06-13 22:57:13 +00002302 PJSUA_LOCK();
2303
2304 for (file_id=0; file_id<PJ_ARRAY_SIZE(pjsua_var.recorder); ++file_id) {
2305 if (pjsua_var.recorder[file_id].port == NULL)
2306 break;
2307 }
2308
2309 if (file_id == PJ_ARRAY_SIZE(pjsua_var.recorder)) {
2310 /* This is unexpected */
2311 PJSUA_UNLOCK();
2312 pj_assert(0);
2313 return PJ_EBUG;
2314 }
2315
2316 pj_memcpy(path, filename->ptr, filename->slen);
2317 path[filename->slen] = '\0';
Benny Prijonob3cdb2b2006-10-19 15:49:47 +00002318
Benny Prijonod5696da2007-07-17 16:25:45 +00002319 pool = pjsua_pool_create(get_basename(path, filename->slen), 1000, 1000);
2320 if (!pool) {
2321 PJSUA_UNLOCK();
2322 return PJ_ENOMEM;
2323 }
2324
Benny Prijonob3cdb2b2006-10-19 15:49:47 +00002325 if (file_format == FMT_WAV) {
Benny Prijonod5696da2007-07-17 16:25:45 +00002326 status = pjmedia_wav_writer_port_create(pool, path,
Benny Prijonob3cdb2b2006-10-19 15:49:47 +00002327 pjsua_var.media_cfg.clock_rate,
2328 pjsua_var.mconf_cfg.channel_count,
2329 pjsua_var.mconf_cfg.samples_per_frame,
2330 pjsua_var.mconf_cfg.bits_per_sample,
2331 options, 0, &port);
Benny Prijonob3cdb2b2006-10-19 15:49:47 +00002332 } else {
Benny Prijonoc95a0f02007-04-09 07:06:08 +00002333 PJ_UNUSED_ARG(enc_param);
Benny Prijonob3cdb2b2006-10-19 15:49:47 +00002334 port = NULL;
2335 status = PJ_ENOTSUP;
2336 }
2337
Benny Prijonoeebe9af2006-06-13 22:57:13 +00002338 if (status != PJ_SUCCESS) {
2339 PJSUA_UNLOCK();
2340 pjsua_perror(THIS_FILE, "Unable to open file for recording", status);
Benny Prijonod5696da2007-07-17 16:25:45 +00002341 pj_pool_release(pool);
Benny Prijonoeebe9af2006-06-13 22:57:13 +00002342 return status;
2343 }
2344
Benny Prijonod5696da2007-07-17 16:25:45 +00002345 status = pjmedia_conf_add_port(pjsua_var.mconf, pool,
Benny Prijonoeebe9af2006-06-13 22:57:13 +00002346 port, filename, &slot);
2347 if (status != PJ_SUCCESS) {
2348 pjmedia_port_destroy(port);
2349 PJSUA_UNLOCK();
Benny Prijonod5696da2007-07-17 16:25:45 +00002350 pj_pool_release(pool);
Benny Prijonoeebe9af2006-06-13 22:57:13 +00002351 return status;
2352 }
2353
2354 pjsua_var.recorder[file_id].port = port;
2355 pjsua_var.recorder[file_id].slot = slot;
Benny Prijonod5696da2007-07-17 16:25:45 +00002356 pjsua_var.recorder[file_id].pool = pool;
Benny Prijonoeebe9af2006-06-13 22:57:13 +00002357
2358 if (p_id) *p_id = file_id;
2359
2360 ++pjsua_var.rec_cnt;
2361
2362 PJSUA_UNLOCK();
2363 return PJ_SUCCESS;
2364}
2365
2366
2367/*
2368 * Get conference port associated with recorder.
2369 */
2370PJ_DEF(pjsua_conf_port_id) pjsua_recorder_get_conf_port(pjsua_recorder_id id)
2371{
Benny Prijonoa1e69682007-05-11 15:14:34 +00002372 PJ_ASSERT_RETURN(id>=0 && id<(int)PJ_ARRAY_SIZE(pjsua_var.recorder),
2373 PJ_EINVAL);
Benny Prijonoeebe9af2006-06-13 22:57:13 +00002374 PJ_ASSERT_RETURN(pjsua_var.recorder[id].port != NULL, PJ_EINVAL);
2375
2376 return pjsua_var.recorder[id].slot;
2377}
2378
Benny Prijono469b1522006-12-26 03:05:17 +00002379/*
2380 * Get the media port for the recorder.
2381 */
2382PJ_DEF(pj_status_t) pjsua_recorder_get_port( pjsua_recorder_id id,
2383 pjmedia_port **p_port)
2384{
Benny Prijonoa1e69682007-05-11 15:14:34 +00002385 PJ_ASSERT_RETURN(id>=0 && id<(int)PJ_ARRAY_SIZE(pjsua_var.recorder),
2386 PJ_EINVAL);
Benny Prijono469b1522006-12-26 03:05:17 +00002387 PJ_ASSERT_RETURN(pjsua_var.recorder[id].port != NULL, PJ_EINVAL);
2388 PJ_ASSERT_RETURN(p_port != NULL, PJ_EINVAL);
2389
2390 *p_port = pjsua_var.recorder[id].port;
2391 return PJ_SUCCESS;
2392}
Benny Prijonoeebe9af2006-06-13 22:57:13 +00002393
2394/*
2395 * Destroy recorder (this will complete recording).
2396 */
2397PJ_DEF(pj_status_t) pjsua_recorder_destroy(pjsua_recorder_id id)
2398{
Benny Prijonoa1e69682007-05-11 15:14:34 +00002399 PJ_ASSERT_RETURN(id>=0 && id<(int)PJ_ARRAY_SIZE(pjsua_var.recorder),
2400 PJ_EINVAL);
Benny Prijonoeebe9af2006-06-13 22:57:13 +00002401 PJ_ASSERT_RETURN(pjsua_var.recorder[id].port != NULL, PJ_EINVAL);
2402
2403 PJSUA_LOCK();
2404
2405 if (pjsua_var.recorder[id].port) {
Nanang Izzuddin148fd392008-06-16 09:52:50 +00002406 pjsua_conf_remove_port(pjsua_var.recorder[id].slot);
Benny Prijonoeebe9af2006-06-13 22:57:13 +00002407 pjmedia_port_destroy(pjsua_var.recorder[id].port);
2408 pjsua_var.recorder[id].port = NULL;
2409 pjsua_var.recorder[id].slot = 0xFFFF;
Benny Prijonod5696da2007-07-17 16:25:45 +00002410 pj_pool_release(pjsua_var.recorder[id].pool);
2411 pjsua_var.recorder[id].pool = NULL;
Benny Prijonoeebe9af2006-06-13 22:57:13 +00002412 pjsua_var.rec_cnt--;
2413 }
2414
2415 PJSUA_UNLOCK();
2416
2417 return PJ_SUCCESS;
2418}
2419
2420
2421/*****************************************************************************
2422 * Sound devices.
2423 */
2424
2425/*
2426 * Enum sound devices.
2427 */
Benny Prijonof798e502009-03-09 13:08:16 +00002428
2429PJ_DEF(pj_status_t) pjsua_enum_aud_devs( pjmedia_aud_dev_info info[],
Benny Prijonoeebe9af2006-06-13 22:57:13 +00002430 unsigned *count)
2431{
2432 unsigned i, dev_count;
2433
Benny Prijono10454dc2009-02-21 14:21:59 +00002434 dev_count = pjmedia_aud_dev_count();
Benny Prijonoeebe9af2006-06-13 22:57:13 +00002435
2436 if (dev_count > *count) dev_count = *count;
2437
2438 for (i=0; i<dev_count; ++i) {
Benny Prijono10454dc2009-02-21 14:21:59 +00002439 pj_status_t status;
Benny Prijonoeebe9af2006-06-13 22:57:13 +00002440
Benny Prijono10454dc2009-02-21 14:21:59 +00002441 status = pjmedia_aud_dev_get_info(i, &info[i]);
2442 if (status != PJ_SUCCESS)
2443 return status;
Benny Prijonoeebe9af2006-06-13 22:57:13 +00002444 }
2445
2446 *count = dev_count;
2447
2448 return PJ_SUCCESS;
2449}
Benny Prijonof798e502009-03-09 13:08:16 +00002450
2451
Benny Prijono10454dc2009-02-21 14:21:59 +00002452PJ_DEF(pj_status_t) pjsua_enum_snd_devs( pjmedia_snd_dev_info info[],
2453 unsigned *count)
2454{
2455 unsigned i, dev_count;
2456
2457 dev_count = pjmedia_aud_dev_count();
2458
2459 if (dev_count > *count) dev_count = *count;
2460 pj_bzero(info, dev_count * sizeof(pjmedia_snd_dev_info));
2461
2462 for (i=0; i<dev_count; ++i) {
2463 pjmedia_aud_dev_info ai;
2464 pj_status_t status;
2465
2466 status = pjmedia_aud_dev_get_info(i, &ai);
2467 if (status != PJ_SUCCESS)
2468 return status;
2469
2470 strncpy(info[i].name, ai.name, sizeof(info[i].name));
2471 info[i].name[sizeof(info[i].name)-1] = '\0';
2472 info[i].input_count = ai.input_count;
2473 info[i].output_count = ai.output_count;
2474 info[i].default_samples_per_sec = ai.default_samples_per_sec;
2475 }
2476
2477 *count = dev_count;
2478
2479 return PJ_SUCCESS;
2480}
Benny Prijono10454dc2009-02-21 14:21:59 +00002481
Benny Prijonof798e502009-03-09 13:08:16 +00002482/* Create audio device parameter to open the device */
2483static pj_status_t create_aud_param(pjmedia_aud_param *param,
2484 pjmedia_aud_dev_index capture_dev,
2485 pjmedia_aud_dev_index playback_dev,
2486 unsigned clock_rate,
2487 unsigned channel_count,
2488 unsigned samples_per_frame,
2489 unsigned bits_per_sample)
Benny Prijonoeebe9af2006-06-13 22:57:13 +00002490{
Nanang Izzuddin8465c682009-03-04 17:23:25 +00002491 pj_status_t status;
2492
Benny Prijono96e74f32009-02-22 12:00:12 +00002493 /* Normalize device ID with new convention about default device ID */
2494 if (playback_dev == PJMEDIA_AUD_DEFAULT_CAPTURE_DEV)
2495 playback_dev = PJMEDIA_AUD_DEFAULT_PLAYBACK_DEV;
2496
Benny Prijono10454dc2009-02-21 14:21:59 +00002497 /* Create default parameters for the device */
Benny Prijonof798e502009-03-09 13:08:16 +00002498 status = pjmedia_aud_dev_default_param(capture_dev, param);
Benny Prijono10454dc2009-02-21 14:21:59 +00002499 if (status != PJ_SUCCESS) {
Benny Prijono96e74f32009-02-22 12:00:12 +00002500 pjsua_perror(THIS_FILE, "Error retrieving default audio "
2501 "device parameters", status);
Benny Prijono10454dc2009-02-21 14:21:59 +00002502 return status;
2503 }
Benny Prijonof798e502009-03-09 13:08:16 +00002504 param->dir = PJMEDIA_DIR_CAPTURE_PLAYBACK;
2505 param->rec_id = capture_dev;
2506 param->play_id = playback_dev;
2507 param->clock_rate = clock_rate;
2508 param->channel_count = channel_count;
2509 param->samples_per_frame = samples_per_frame;
2510 param->bits_per_sample = bits_per_sample;
2511
2512 /* Update the setting with user preference */
2513#define update_param(cap, field) \
2514 if (pjsua_var.aud_param.flags & cap) { \
2515 param->flags |= cap; \
2516 param->field = pjsua_var.aud_param.field; \
2517 }
2518 update_param( PJMEDIA_AUD_DEV_CAP_INPUT_VOLUME_SETTING, input_vol);
2519 update_param( PJMEDIA_AUD_DEV_CAP_OUTPUT_VOLUME_SETTING, output_vol);
2520 update_param( PJMEDIA_AUD_DEV_CAP_INPUT_ROUTE, input_route);
2521 update_param( PJMEDIA_AUD_DEV_CAP_OUTPUT_ROUTE, output_route);
2522#undef update_param
2523
Benny Prijono10454dc2009-02-21 14:21:59 +00002524 /* Latency settings */
Benny Prijonof798e502009-03-09 13:08:16 +00002525 param->flags |= (PJMEDIA_AUD_DEV_CAP_INPUT_LATENCY |
2526 PJMEDIA_AUD_DEV_CAP_OUTPUT_LATENCY);
2527 param->input_latency_ms = pjsua_var.media_cfg.snd_rec_latency;
2528 param->output_latency_ms = pjsua_var.media_cfg.snd_play_latency;
2529
Benny Prijono10454dc2009-02-21 14:21:59 +00002530 /* EC settings */
2531 if (pjsua_var.media_cfg.ec_tail_len) {
Benny Prijonof798e502009-03-09 13:08:16 +00002532 param->flags |= (PJMEDIA_AUD_DEV_CAP_EC | PJMEDIA_AUD_DEV_CAP_EC_TAIL);
2533 param->ec_enabled = PJ_TRUE;
2534 param->ec_tail_ms = pjsua_var.media_cfg.ec_tail_len;
Benny Prijono10454dc2009-02-21 14:21:59 +00002535 } else {
Benny Prijonof798e502009-03-09 13:08:16 +00002536 param->flags &= ~(PJMEDIA_AUD_DEV_CAP_EC|PJMEDIA_AUD_DEV_CAP_EC_TAIL);
Benny Prijono10454dc2009-02-21 14:21:59 +00002537 }
2538
Benny Prijonof798e502009-03-09 13:08:16 +00002539 return PJ_SUCCESS;
2540}
Benny Prijono26056d82006-10-11 16:03:41 +00002541
Benny Prijonof798e502009-03-09 13:08:16 +00002542/* Internal: the first time the audio device is opened (during app
2543 * startup), retrieve the audio settings such as volume level
2544 * so that aud_get_settings() will work.
2545 */
2546static pj_status_t update_initial_aud_param()
2547{
2548 pjmedia_aud_stream *strm;
2549 pjmedia_aud_param param;
2550 pj_status_t status;
Benny Prijono26056d82006-10-11 16:03:41 +00002551
Benny Prijonof798e502009-03-09 13:08:16 +00002552 PJ_ASSERT_RETURN(pjsua_var.snd_port != NULL, PJ_EBUG);
Nanang Izzuddin3c1ae632008-08-21 15:04:20 +00002553
Benny Prijonof798e502009-03-09 13:08:16 +00002554 strm = pjmedia_snd_port_get_snd_stream(pjsua_var.snd_port);
Benny Prijono26056d82006-10-11 16:03:41 +00002555
Benny Prijonof798e502009-03-09 13:08:16 +00002556 status = pjmedia_aud_stream_get_param(strm, &param);
Benny Prijonoeebe9af2006-06-13 22:57:13 +00002557 if (status != PJ_SUCCESS) {
Benny Prijonof798e502009-03-09 13:08:16 +00002558 pjsua_perror(THIS_FILE, "Error audio stream "
2559 "device parameters", status);
Benny Prijonoeebe9af2006-06-13 22:57:13 +00002560 return status;
2561 }
2562
Benny Prijonof798e502009-03-09 13:08:16 +00002563#define update_saved_param(cap, field) \
2564 if (param.flags & cap) { \
2565 pjsua_var.aud_param.flags |= cap; \
2566 pjsua_var.aud_param.field = param.field; \
2567 }
2568
2569 update_saved_param(PJMEDIA_AUD_DEV_CAP_INPUT_VOLUME_SETTING, input_vol);
2570 update_saved_param(PJMEDIA_AUD_DEV_CAP_OUTPUT_VOLUME_SETTING, output_vol);
2571 update_saved_param(PJMEDIA_AUD_DEV_CAP_INPUT_ROUTE, input_route);
2572 update_saved_param(PJMEDIA_AUD_DEV_CAP_OUTPUT_ROUTE, output_route);
2573#undef update_saved_param
2574
2575 return PJ_SUCCESS;
2576}
2577
2578/* Get format name */
2579static const char *get_fmt_name(pj_uint32_t id)
2580{
2581 static char name[8];
2582
2583 if (id == PJMEDIA_FORMAT_L16)
2584 return "PCM";
2585 pj_memcpy(name, &id, 4);
2586 name[4] = '\0';
2587 return name;
2588}
2589
2590/* Open sound device with the setting. */
2591static pj_status_t open_snd_dev(pjmedia_aud_param *param)
2592{
2593 pjmedia_port *conf_port;
2594 pj_status_t status;
2595
2596 PJ_ASSERT_RETURN(param, PJ_EINVAL);
2597
2598 /* Check if NULL sound device is used */
2599 if (NULL_SND_DEV_ID==param->rec_id || NULL_SND_DEV_ID==param->play_id) {
2600 return pjsua_set_null_snd_dev();
2601 }
2602
2603 /* Close existing sound port */
2604 close_snd_dev();
2605
2606 /* Create memory pool for sound device. */
2607 pjsua_var.snd_pool = pjsua_pool_create("pjsua_snd", 4000, 4000);
2608 PJ_ASSERT_RETURN(pjsua_var.snd_pool, PJ_ENOMEM);
2609
2610
2611 PJ_LOG(4,(THIS_FILE, "Opening sound device %s@%d/%d/%dms",
2612 get_fmt_name(param->ext_fmt.id),
2613 param->clock_rate, param->channel_count,
2614 param->samples_per_frame / param->channel_count * 1000 /
2615 param->clock_rate));
2616
2617 status = pjmedia_snd_port_create2( pjsua_var.snd_pool,
2618 param, &pjsua_var.snd_port);
2619 if (status != PJ_SUCCESS)
2620 return status;
2621
2622 /* Get the port0 of the conference bridge. */
2623 conf_port = pjmedia_conf_get_master_port(pjsua_var.mconf);
2624 pj_assert(conf_port != NULL);
2625
2626 /* For conference bridge, resample if necessary if the bridge's
2627 * clock rate is different than the sound device's clock rate.
2628 */
2629 if (!pjsua_var.is_mswitch &&
2630 param->ext_fmt.id == PJMEDIA_FORMAT_PCM &&
2631 conf_port->info.clock_rate != param->clock_rate)
2632 {
2633 pjmedia_port *resample_port;
2634 unsigned resample_opt = 0;
2635
2636 if (pjsua_var.media_cfg.quality >= 3 &&
2637 pjsua_var.media_cfg.quality <= 4)
2638 {
2639 resample_opt |= PJMEDIA_RESAMPLE_USE_SMALL_FILTER;
2640 }
2641 else if (pjsua_var.media_cfg.quality < 3) {
2642 resample_opt |= PJMEDIA_RESAMPLE_USE_LINEAR;
2643 }
2644
2645 status = pjmedia_resample_port_create(pjsua_var.snd_pool,
2646 conf_port,
2647 param->clock_rate,
2648 resample_opt,
2649 &resample_port);
2650 if (status != PJ_SUCCESS) {
2651 char errmsg[PJ_ERR_MSG_SIZE];
2652 pj_strerror(status, errmsg, sizeof(errmsg));
2653 PJ_LOG(4, (THIS_FILE,
2654 "Error creating resample port: %s",
2655 errmsg));
2656 close_snd_dev();
2657 return status;
2658 }
2659
2660 conf_port = resample_port;
2661 }
2662
2663 /* Otherwise for audio switchboard, the switch's port0 setting is
2664 * derived from the sound device setting, so update the setting.
2665 */
2666 if (pjsua_var.is_mswitch) {
2667 pj_memcpy(&conf_port->info.format, &param->ext_fmt,
2668 sizeof(conf_port->info.format));
2669 conf_port->info.clock_rate = param->clock_rate;
2670 conf_port->info.samples_per_frame = param->samples_per_frame;
2671 conf_port->info.channel_count = param->channel_count;
2672 conf_port->info.bits_per_sample = 16;
2673 }
2674
2675 /* Connect sound port to the bridge */
Benny Prijono52a93912006-08-04 20:54:37 +00002676 status = pjmedia_snd_port_connect(pjsua_var.snd_port,
2677 conf_port );
2678 if (status != PJ_SUCCESS) {
2679 pjsua_perror(THIS_FILE, "Unable to connect conference port to "
2680 "sound device", status);
2681 pjmedia_snd_port_destroy(pjsua_var.snd_port);
2682 pjsua_var.snd_port = NULL;
2683 return status;
2684 }
2685
Benny Prijonoeebe9af2006-06-13 22:57:13 +00002686 /* Save the device IDs */
Benny Prijonof798e502009-03-09 13:08:16 +00002687 pjsua_var.cap_dev = param->rec_id;
2688 pjsua_var.play_dev = param->play_id;
Benny Prijonoeebe9af2006-06-13 22:57:13 +00002689
Benny Prijonoc53c6d72006-11-27 09:54:03 +00002690 /* Update sound device name. */
Benny Prijonof798e502009-03-09 13:08:16 +00002691 {
2692 pjmedia_aud_dev_info rec_info;
2693 pjmedia_aud_stream *strm;
2694 pjmedia_aud_param si;
2695 pj_str_t tmp;
Benny Prijonoc53c6d72006-11-27 09:54:03 +00002696
Benny Prijonof798e502009-03-09 13:08:16 +00002697 strm = pjmedia_snd_port_get_snd_stream(pjsua_var.snd_port);
2698 status = pjmedia_aud_stream_get_param(strm, &si);
2699 if (status == PJ_SUCCESS)
2700 status = pjmedia_aud_dev_get_info(si.rec_id, &rec_info);
Benny Prijonof3758ee2008-02-26 15:32:16 +00002701
Benny Prijonof798e502009-03-09 13:08:16 +00002702 if (status==PJ_SUCCESS) {
2703 if (param->clock_rate != pjsua_var.media_cfg.clock_rate) {
2704 char tmp_buf[128];
2705 int tmp_buf_len = sizeof(tmp_buf);
2706
2707 tmp_buf_len = pj_ansi_snprintf(tmp_buf, sizeof(tmp_buf)-1,
2708 "%s (%dKHz)",
2709 rec_info.name,
2710 param->clock_rate/1000);
2711 pj_strset(&tmp, tmp_buf, tmp_buf_len);
2712 pjmedia_conf_set_port0_name(pjsua_var.mconf, &tmp);
2713 } else {
2714 pjmedia_conf_set_port0_name(pjsua_var.mconf,
2715 pj_cstr(&tmp, rec_info.name));
2716 }
2717 }
2718
2719 /* Any error is not major, let it through */
2720 status = PJ_SUCCESS;
2721 };
2722
2723 /* If this is the first time the audio device is open, retrieve some
2724 * settings from the device (such as volume settings) so that the
2725 * pjsua_snd_get_setting() work.
2726 */
2727 if (pjsua_var.aud_open_cnt == 0) {
2728 update_initial_aud_param();
2729 ++pjsua_var.aud_open_cnt;
Benny Prijonof3758ee2008-02-26 15:32:16 +00002730 }
Benny Prijonoc53c6d72006-11-27 09:54:03 +00002731
Benny Prijonoeebe9af2006-06-13 22:57:13 +00002732 return PJ_SUCCESS;
Benny Prijonof798e502009-03-09 13:08:16 +00002733}
Nanang Izzuddin8465c682009-03-04 17:23:25 +00002734
Nanang Izzuddin8465c682009-03-04 17:23:25 +00002735
Benny Prijonof798e502009-03-09 13:08:16 +00002736/* Close existing sound device */
2737static void close_snd_dev(void)
2738{
2739 /* Close sound device */
2740 if (pjsua_var.snd_port) {
2741 pjmedia_aud_dev_info cap_info, play_info;
2742 pjmedia_aud_stream *strm;
2743 pjmedia_aud_param param;
2744
2745 strm = pjmedia_snd_port_get_snd_stream(pjsua_var.snd_port);
2746 pjmedia_aud_stream_get_param(strm, &param);
2747
2748 if (pjmedia_aud_dev_get_info(param.rec_id, &cap_info) != PJ_SUCCESS)
2749 cap_info.name[0] = '\0';
2750 if (pjmedia_aud_dev_get_info(param.play_id, &play_info) != PJ_SUCCESS)
2751 play_info.name[0] = '\0';
2752
2753 PJ_LOG(4,(THIS_FILE, "Closing %s sound playback device and "
2754 "%s sound capture device",
2755 play_info.name, cap_info.name));
2756
2757 pjmedia_snd_port_disconnect(pjsua_var.snd_port);
2758 pjmedia_snd_port_destroy(pjsua_var.snd_port);
2759 pjsua_var.snd_port = NULL;
2760 }
2761
2762 /* Close null sound device */
2763 if (pjsua_var.null_snd) {
2764 PJ_LOG(4,(THIS_FILE, "Closing null sound device.."));
2765 pjmedia_master_port_destroy(pjsua_var.null_snd, PJ_FALSE);
2766 pjsua_var.null_snd = NULL;
2767 }
2768
2769 if (pjsua_var.snd_pool)
2770 pj_pool_release(pjsua_var.snd_pool);
2771 pjsua_var.snd_pool = NULL;
2772}
2773
2774
2775/*
2776 * Select or change sound device. Application may call this function at
2777 * any time to replace current sound device.
2778 */
2779PJ_DEF(pj_status_t) pjsua_set_snd_dev( int capture_dev,
2780 int playback_dev)
2781{
2782 unsigned alt_cr_cnt = 1;
2783 unsigned alt_cr[] = {0, 44100, 48000, 32000, 16000, 8000};
2784 unsigned i;
2785 pj_status_t status = -1;
2786
Benny Prijono23ea21a2009-06-03 12:43:06 +00002787 /* Null-sound */
2788 if (capture_dev==NULL_SND_DEV_ID && playback_dev==NULL_SND_DEV_ID) {
2789 return pjsua_set_null_snd_dev();
2790 }
2791
Benny Prijonof798e502009-03-09 13:08:16 +00002792 /* Set default clock rate */
2793 alt_cr[0] = pjsua_var.media_cfg.snd_clock_rate;
2794 if (alt_cr[0] == 0)
2795 alt_cr[0] = pjsua_var.media_cfg.clock_rate;
2796
2797 /* Allow retrying of different clock rate if we're using conference
2798 * bridge (meaning audio format is always PCM), otherwise lock on
2799 * to one clock rate.
2800 */
2801 if (pjsua_var.is_mswitch) {
2802 alt_cr_cnt = 1;
2803 } else {
2804 alt_cr_cnt = PJ_ARRAY_SIZE(alt_cr);
2805 }
2806
2807 /* Attempts to open the sound device with different clock rates */
2808 for (i=0; i<alt_cr_cnt; ++i) {
2809 pjmedia_aud_param param;
2810 unsigned samples_per_frame;
2811
2812 /* Create the default audio param */
2813 samples_per_frame = alt_cr[i] *
2814 pjsua_var.media_cfg.audio_frame_ptime *
2815 pjsua_var.media_cfg.channel_count / 1000;
2816 status = create_aud_param(&param, capture_dev, playback_dev,
2817 alt_cr[i], pjsua_var.media_cfg.channel_count,
2818 samples_per_frame, 16);
2819 if (status != PJ_SUCCESS)
2820 return status;
2821
2822 /* Open! */
2823 status = open_snd_dev(&param);
2824 if (status == PJ_SUCCESS)
2825 break;
2826 }
2827
2828 if (status != PJ_SUCCESS) {
2829 pjsua_perror(THIS_FILE, "Unable to open sound device", status);
2830 return status;
2831 }
2832
Benny Prijonoe25fe6f2009-07-16 17:52:08 +00002833 pjsua_var.no_snd = PJ_FALSE;
2834
Benny Prijonof798e502009-03-09 13:08:16 +00002835 return PJ_SUCCESS;
Benny Prijonoeebe9af2006-06-13 22:57:13 +00002836}
2837
2838
2839/*
Benny Prijonoebdf8772007-02-01 19:25:50 +00002840 * Get currently active sound devices. If sound devices has not been created
2841 * (for example when pjsua_start() is not called), it is possible that
2842 * the function returns PJ_SUCCESS with -1 as device IDs.
2843 */
2844PJ_DEF(pj_status_t) pjsua_get_snd_dev(int *capture_dev,
2845 int *playback_dev)
2846{
2847 if (capture_dev) {
2848 *capture_dev = pjsua_var.cap_dev;
2849 }
2850 if (playback_dev) {
2851 *playback_dev = pjsua_var.play_dev;
2852 }
2853
2854 return PJ_SUCCESS;
2855}
2856
2857
2858/*
Benny Prijonoeebe9af2006-06-13 22:57:13 +00002859 * Use null sound device.
2860 */
2861PJ_DEF(pj_status_t) pjsua_set_null_snd_dev(void)
2862{
2863 pjmedia_port *conf_port;
2864 pj_status_t status;
2865
2866 /* Close existing sound device */
2867 close_snd_dev();
2868
Nanang Izzuddin68559c32008-06-13 17:01:46 +00002869 /* Create memory pool for sound device. */
2870 pjsua_var.snd_pool = pjsua_pool_create("pjsua_snd", 4000, 4000);
2871 PJ_ASSERT_RETURN(pjsua_var.snd_pool, PJ_ENOMEM);
2872
Benny Prijonoeebe9af2006-06-13 22:57:13 +00002873 PJ_LOG(4,(THIS_FILE, "Opening null sound device.."));
2874
2875 /* Get the port0 of the conference bridge. */
2876 conf_port = pjmedia_conf_get_master_port(pjsua_var.mconf);
2877 pj_assert(conf_port != NULL);
2878
2879 /* Create master port, connecting port0 of the conference bridge to
2880 * a null port.
2881 */
Nanang Izzuddin68559c32008-06-13 17:01:46 +00002882 status = pjmedia_master_port_create(pjsua_var.snd_pool, pjsua_var.null_port,
Benny Prijonoeebe9af2006-06-13 22:57:13 +00002883 conf_port, 0, &pjsua_var.null_snd);
2884 if (status != PJ_SUCCESS) {
2885 pjsua_perror(THIS_FILE, "Unable to create null sound device",
2886 status);
2887 return status;
2888 }
2889
2890 /* Start the master port */
2891 status = pjmedia_master_port_start(pjsua_var.null_snd);
2892 PJ_ASSERT_RETURN(status == PJ_SUCCESS, status);
2893
Nanang Izzuddin68559c32008-06-13 17:01:46 +00002894 pjsua_var.cap_dev = NULL_SND_DEV_ID;
2895 pjsua_var.play_dev = NULL_SND_DEV_ID;
2896
Benny Prijonoe25fe6f2009-07-16 17:52:08 +00002897 pjsua_var.no_snd = PJ_FALSE;
2898
Benny Prijonoeebe9af2006-06-13 22:57:13 +00002899 return PJ_SUCCESS;
2900}
2901
2902
Benny Prijonoe909eac2006-07-27 22:04:56 +00002903
2904/*
2905 * Use no device!
2906 */
2907PJ_DEF(pjmedia_port*) pjsua_set_no_snd_dev(void)
2908{
2909 /* Close existing sound device */
2910 close_snd_dev();
2911
2912 pjsua_var.no_snd = PJ_TRUE;
2913 return pjmedia_conf_get_master_port(pjsua_var.mconf);
2914}
2915
2916
Benny Prijonof20687a2006-08-04 18:27:19 +00002917/*
2918 * Configure the AEC settings of the sound port.
2919 */
Benny Prijono5da50432006-08-07 10:24:52 +00002920PJ_DEF(pj_status_t) pjsua_set_ec(unsigned tail_ms, unsigned options)
Benny Prijonof20687a2006-08-04 18:27:19 +00002921{
2922 pjsua_var.media_cfg.ec_tail_len = tail_ms;
2923
2924 if (pjsua_var.snd_port)
Benny Prijono5da50432006-08-07 10:24:52 +00002925 return pjmedia_snd_port_set_ec( pjsua_var.snd_port, pjsua_var.pool,
2926 tail_ms, options);
Benny Prijonof20687a2006-08-04 18:27:19 +00002927
2928 return PJ_SUCCESS;
2929}
2930
2931
2932/*
2933 * Get current AEC tail length.
2934 */
Benny Prijono22dfe592006-08-06 12:07:13 +00002935PJ_DEF(pj_status_t) pjsua_get_ec_tail(unsigned *p_tail_ms)
Benny Prijonof20687a2006-08-04 18:27:19 +00002936{
2937 *p_tail_ms = pjsua_var.media_cfg.ec_tail_len;
2938 return PJ_SUCCESS;
2939}
2940
Nanang Izzuddinfe02a062009-02-18 14:28:49 +00002941
Nanang Izzuddin0cb3b022009-02-27 17:37:35 +00002942/*
Benny Prijonof798e502009-03-09 13:08:16 +00002943 * Check whether the sound device is currently active.
Nanang Izzuddin0cb3b022009-02-27 17:37:35 +00002944 */
Benny Prijonof798e502009-03-09 13:08:16 +00002945PJ_DEF(pj_bool_t) pjsua_snd_is_active(void)
Nanang Izzuddin0cb3b022009-02-27 17:37:35 +00002946{
Benny Prijonof798e502009-03-09 13:08:16 +00002947 return pjsua_var.snd_port != NULL;
2948}
Nanang Izzuddin0cb3b022009-02-27 17:37:35 +00002949
Nanang Izzuddin0cb3b022009-02-27 17:37:35 +00002950
Benny Prijonof798e502009-03-09 13:08:16 +00002951/*
2952 * Configure sound device setting to the sound device being used.
2953 */
2954PJ_DEF(pj_status_t) pjsua_snd_set_setting( pjmedia_aud_dev_cap cap,
2955 const void *pval,
2956 pj_bool_t keep)
2957{
Benny Prijono09b0ff62009-03-10 12:07:51 +00002958 pj_status_t status;
2959
Benny Prijonof798e502009-03-09 13:08:16 +00002960 /* Check if we are allowed to set the cap */
Benny Prijono09b0ff62009-03-10 12:07:51 +00002961 if ((cap & pjsua_var.aud_svmask) == 0) {
Benny Prijonof798e502009-03-09 13:08:16 +00002962 return PJMEDIA_EAUD_INVCAP;
2963 }
2964
Benny Prijono09b0ff62009-03-10 12:07:51 +00002965 /* If sound is active, set it immediately */
Benny Prijonof798e502009-03-09 13:08:16 +00002966 if (pjsua_snd_is_active()) {
Benny Prijonof798e502009-03-09 13:08:16 +00002967 pjmedia_aud_stream *strm;
2968
2969 strm = pjmedia_snd_port_get_snd_stream(pjsua_var.snd_port);
Benny Prijono09b0ff62009-03-10 12:07:51 +00002970 status = pjmedia_aud_stream_set_cap(strm, cap, pval);
Benny Prijonof798e502009-03-09 13:08:16 +00002971 } else {
Benny Prijono09b0ff62009-03-10 12:07:51 +00002972 status = PJ_SUCCESS;
Benny Prijonof798e502009-03-09 13:08:16 +00002973 }
Benny Prijono09b0ff62009-03-10 12:07:51 +00002974
2975 if (status != PJ_SUCCESS)
2976 return status;
2977
2978 /* Save in internal param for later device open */
2979 if (keep) {
2980 status = pjmedia_aud_param_set_cap(&pjsua_var.aud_param,
2981 cap, pval);
2982 }
2983
2984 return status;
Benny Prijonof798e502009-03-09 13:08:16 +00002985}
2986
2987/*
2988 * Retrieve a sound device setting.
2989 */
2990PJ_DEF(pj_status_t) pjsua_snd_get_setting( pjmedia_aud_dev_cap cap,
2991 void *pval)
2992{
2993 /* If sound device has never been opened before, open it to
2994 * retrieve the initial setting from the device (e.g. audio
2995 * volume)
2996 */
Benny Prijono09b0ff62009-03-10 12:07:51 +00002997 if (pjsua_var.aud_open_cnt==0) {
2998 PJ_LOG(4,(THIS_FILE, "Opening sound device to get initial settings"));
Benny Prijonof798e502009-03-09 13:08:16 +00002999 pjsua_set_snd_dev(pjsua_var.cap_dev, pjsua_var.play_dev);
Benny Prijono09b0ff62009-03-10 12:07:51 +00003000 close_snd_dev();
3001 }
Benny Prijonof798e502009-03-09 13:08:16 +00003002
3003 if (pjsua_snd_is_active()) {
3004 /* Sound is active, retrieve from device directly */
3005 pjmedia_aud_stream *strm;
3006
3007 strm = pjmedia_snd_port_get_snd_stream(pjsua_var.snd_port);
3008 return pjmedia_aud_stream_get_cap(strm, cap, pval);
3009 } else {
3010 /* Otherwise retrieve from internal param */
3011 return pjmedia_aud_param_get_cap(&pjsua_var.aud_param,
3012 cap, pval);
3013 }
Nanang Izzuddin0cb3b022009-02-27 17:37:35 +00003014}
3015
3016
Benny Prijonoeebe9af2006-06-13 22:57:13 +00003017/*****************************************************************************
3018 * Codecs.
3019 */
3020
3021/*
3022 * Enum all supported codecs in the system.
3023 */
3024PJ_DEF(pj_status_t) pjsua_enum_codecs( pjsua_codec_info id[],
3025 unsigned *p_count )
3026{
3027 pjmedia_codec_mgr *codec_mgr;
3028 pjmedia_codec_info info[32];
3029 unsigned i, count, prio[32];
3030 pj_status_t status;
3031
3032 codec_mgr = pjmedia_endpt_get_codec_mgr(pjsua_var.med_endpt);
3033 count = PJ_ARRAY_SIZE(info);
3034 status = pjmedia_codec_mgr_enum_codecs( codec_mgr, &count, info, prio);
3035 if (status != PJ_SUCCESS) {
3036 *p_count = 0;
3037 return status;
3038 }
3039
3040 if (count > *p_count) count = *p_count;
3041
3042 for (i=0; i<count; ++i) {
3043 pjmedia_codec_info_to_id(&info[i], id[i].buf_, sizeof(id[i].buf_));
3044 id[i].codec_id = pj_str(id[i].buf_);
3045 id[i].priority = (pj_uint8_t) prio[i];
3046 }
3047
3048 *p_count = count;
3049
3050 return PJ_SUCCESS;
3051}
3052
3053
3054/*
3055 * Change codec priority.
3056 */
3057PJ_DEF(pj_status_t) pjsua_codec_set_priority( const pj_str_t *codec_id,
3058 pj_uint8_t priority )
3059{
Benny Prijono88accae2008-06-26 15:48:14 +00003060 const pj_str_t all = { NULL, 0 };
Benny Prijonoeebe9af2006-06-13 22:57:13 +00003061 pjmedia_codec_mgr *codec_mgr;
3062
3063 codec_mgr = pjmedia_endpt_get_codec_mgr(pjsua_var.med_endpt);
3064
Benny Prijono88accae2008-06-26 15:48:14 +00003065 if (codec_id->slen==1 && *codec_id->ptr=='*')
3066 codec_id = &all;
3067
Benny Prijonoeebe9af2006-06-13 22:57:13 +00003068 return pjmedia_codec_mgr_set_codec_priority(codec_mgr, codec_id,
3069 priority);
3070}
3071
3072
3073/*
3074 * Get codec parameters.
3075 */
3076PJ_DEF(pj_status_t) pjsua_codec_get_param( const pj_str_t *codec_id,
3077 pjmedia_codec_param *param )
3078{
Benny Prijono88accae2008-06-26 15:48:14 +00003079 const pj_str_t all = { NULL, 0 };
Benny Prijonoeebe9af2006-06-13 22:57:13 +00003080 const pjmedia_codec_info *info;
3081 pjmedia_codec_mgr *codec_mgr;
3082 unsigned count = 1;
3083 pj_status_t status;
3084
3085 codec_mgr = pjmedia_endpt_get_codec_mgr(pjsua_var.med_endpt);
3086
Benny Prijono88accae2008-06-26 15:48:14 +00003087 if (codec_id->slen==1 && *codec_id->ptr=='*')
3088 codec_id = &all;
3089
Benny Prijonoeebe9af2006-06-13 22:57:13 +00003090 status = pjmedia_codec_mgr_find_codecs_by_id(codec_mgr, codec_id,
3091 &count, &info, NULL);
3092 if (status != PJ_SUCCESS)
3093 return status;
3094
3095 if (count != 1)
3096 return PJ_ENOTFOUND;
3097
3098 status = pjmedia_codec_mgr_get_default_param( codec_mgr, info, param);
3099 return status;
3100}
3101
3102
3103/*
3104 * Set codec parameters.
3105 */
Nanang Izzuddin06839e72010-01-27 11:48:31 +00003106PJ_DEF(pj_status_t) pjsua_codec_set_param( const pj_str_t *codec_id,
Benny Prijonoeebe9af2006-06-13 22:57:13 +00003107 const pjmedia_codec_param *param)
3108{
Nanang Izzuddin06839e72010-01-27 11:48:31 +00003109 const pjmedia_codec_info *info[2];
3110 pjmedia_codec_mgr *codec_mgr;
3111 unsigned count = 2;
3112 pj_status_t status;
3113
3114 codec_mgr = pjmedia_endpt_get_codec_mgr(pjsua_var.med_endpt);
3115
3116 status = pjmedia_codec_mgr_find_codecs_by_id(codec_mgr, codec_id,
3117 &count, info, NULL);
3118 if (status != PJ_SUCCESS)
3119 return status;
3120
3121 /* Codec ID should be specific, except for G.722.1 */
3122 if (count > 1 &&
3123 pj_strnicmp2(codec_id, "G7221/16", 8) != 0 &&
3124 pj_strnicmp2(codec_id, "G7221/32", 8) != 0)
3125 {
3126 pj_assert(!"Codec ID is not specific");
3127 return PJ_ETOOMANY;
3128 }
3129
3130 status = pjmedia_codec_mgr_set_default_param(codec_mgr, info[0], param);
3131 return status;
Benny Prijonoeebe9af2006-06-13 22:57:13 +00003132}