blob: 2d6e22432fd2441fb5cbf46c6272a65eec370762 [file] [log] [blame]
Benny Prijonoeebe9af2006-06-13 22:57:13 +00001/* $Id$ */
2/*
Benny Prijonoa771a512007-02-19 01:13:53 +00003 * Copyright (C) 2003-2007 Benny Prijono <benny@prijono.org>
Benny Prijonoeebe9af2006-06-13 22:57:13 +00004 *
5 * This program is free software; you can redistribute it and/or modify
6 * it under the terms of the GNU General Public License as published by
7 * the Free Software Foundation; either version 2 of the License, or
8 * (at your option) any later version.
9 *
10 * This program is distributed in the hope that it will be useful,
11 * but WITHOUT ANY WARRANTY; without even the implied warranty of
12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 * GNU General Public License for more details.
14 *
15 * You should have received a copy of the GNU General Public License
16 * along with this program; if not, write to the Free Software
17 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
18 */
19#include <pjsua-lib/pjsua.h>
20#include <pjsua-lib/pjsua_internal.h>
21
22
23#define THIS_FILE "pjsua_media.c"
24
Benny Prijono80eee892007-11-03 22:43:23 +000025#define DEFAULT_RTP_PORT 4000
26
27#ifndef PJSUA_REQUIRE_CONSECUTIVE_RTCP_PORT
28# define PJSUA_REQUIRE_CONSECUTIVE_RTCP_PORT 0
29#endif
30
Benny Prijonoeebe9af2006-06-13 22:57:13 +000031
Benny Prijonode479562007-03-15 10:23:55 +000032/* Next RTP port to be used */
33static pj_uint16_t next_rtp_port;
Benny Prijonoeebe9af2006-06-13 22:57:13 +000034
35/* Close existing sound device */
36static void close_snd_dev(void);
37
38
39/**
40 * Init media subsystems.
41 */
42pj_status_t pjsua_media_subsys_init(const pjsua_media_config *cfg)
43{
Benny Prijonoba5926a2007-05-02 11:29:37 +000044 pj_str_t codec_id = {NULL, 0};
Benny Prijono0498d902006-06-19 14:49:14 +000045 unsigned opt;
Benny Prijonoeebe9af2006-06-13 22:57:13 +000046 pj_status_t status;
47
Benny Prijonofc24e692007-01-27 18:31:51 +000048 /* To suppress warning about unused var when all codecs are disabled */
49 PJ_UNUSED_ARG(codec_id);
50
Benny Prijonoeebe9af2006-06-13 22:57:13 +000051 /* Copy configuration */
52 pj_memcpy(&pjsua_var.media_cfg, cfg, sizeof(*cfg));
53
54 /* Normalize configuration */
Benny Prijono50f19b32008-03-11 13:15:43 +000055 if (pjsua_var.media_cfg.snd_clock_rate == 0) {
56 pjsua_var.media_cfg.snd_clock_rate = pjsua_var.media_cfg.clock_rate;
57 }
Benny Prijonoeebe9af2006-06-13 22:57:13 +000058
59 if (pjsua_var.media_cfg.has_ioqueue &&
60 pjsua_var.media_cfg.thread_cnt == 0)
61 {
62 pjsua_var.media_cfg.thread_cnt = 1;
63 }
64
65 if (pjsua_var.media_cfg.max_media_ports < pjsua_var.ua_cfg.max_calls) {
66 pjsua_var.media_cfg.max_media_ports = pjsua_var.ua_cfg.max_calls + 2;
67 }
68
69 /* Create media endpoint. */
70 status = pjmedia_endpt_create(&pjsua_var.cp.factory,
71 pjsua_var.media_cfg.has_ioqueue? NULL :
72 pjsip_endpt_get_ioqueue(pjsua_var.endpt),
73 pjsua_var.media_cfg.thread_cnt,
74 &pjsua_var.med_endpt);
75 if (status != PJ_SUCCESS) {
76 pjsua_perror(THIS_FILE,
77 "Media stack initialization has returned error",
78 status);
79 return status;
80 }
81
82 /* Register all codecs */
83#if PJMEDIA_HAS_SPEEX_CODEC
84 /* Register speex. */
85 status = pjmedia_codec_speex_init(pjsua_var.med_endpt,
Benny Prijono7ca96da2006-08-07 12:11:40 +000086 0,
Benny Prijono0498d902006-06-19 14:49:14 +000087 pjsua_var.media_cfg.quality,
88 pjsua_var.media_cfg.quality);
Benny Prijonoeebe9af2006-06-13 22:57:13 +000089 if (status != PJ_SUCCESS) {
90 pjsua_perror(THIS_FILE, "Error initializing Speex codec",
91 status);
92 return status;
93 }
Benny Prijono7ca96da2006-08-07 12:11:40 +000094
95 /* Set speex/16000 to higher priority*/
96 codec_id = pj_str("speex/16000");
97 pjmedia_codec_mgr_set_codec_priority(
98 pjmedia_endpt_get_codec_mgr(pjsua_var.med_endpt),
99 &codec_id, PJMEDIA_CODEC_PRIO_NORMAL+2);
100
101 /* Set speex/8000 to next higher priority*/
102 codec_id = pj_str("speex/8000");
103 pjmedia_codec_mgr_set_codec_priority(
104 pjmedia_endpt_get_codec_mgr(pjsua_var.med_endpt),
105 &codec_id, PJMEDIA_CODEC_PRIO_NORMAL+1);
106
107
108
Benny Prijonoeebe9af2006-06-13 22:57:13 +0000109#endif /* PJMEDIA_HAS_SPEEX_CODEC */
110
Benny Prijono00cae612006-07-31 15:19:36 +0000111#if PJMEDIA_HAS_ILBC_CODEC
112 /* Register iLBC. */
113 status = pjmedia_codec_ilbc_init( pjsua_var.med_endpt,
114 pjsua_var.media_cfg.ilbc_mode);
115 if (status != PJ_SUCCESS) {
116 pjsua_perror(THIS_FILE, "Error initializing iLBC codec",
117 status);
118 return status;
119 }
120#endif /* PJMEDIA_HAS_ILBC_CODEC */
121
Benny Prijonoeebe9af2006-06-13 22:57:13 +0000122#if PJMEDIA_HAS_GSM_CODEC
123 /* Register GSM */
124 status = pjmedia_codec_gsm_init(pjsua_var.med_endpt);
125 if (status != PJ_SUCCESS) {
126 pjsua_perror(THIS_FILE, "Error initializing GSM codec",
127 status);
128 return status;
129 }
130#endif /* PJMEDIA_HAS_GSM_CODEC */
131
132#if PJMEDIA_HAS_G711_CODEC
133 /* Register PCMA and PCMU */
134 status = pjmedia_codec_g711_init(pjsua_var.med_endpt);
135 if (status != PJ_SUCCESS) {
136 pjsua_perror(THIS_FILE, "Error initializing G711 codec",
137 status);
138 return status;
139 }
140#endif /* PJMEDIA_HAS_G711_CODEC */
141
Benny Prijono7ffd7752008-03-17 14:07:53 +0000142#if PJMEDIA_HAS_G722_CODEC
143 status = pjmedia_codec_g722_init( pjsua_var.med_endpt );
144 if (status != PJ_SUCCESS) {
145 pjsua_perror(THIS_FILE, "Error initializing G722 codec",
146 status);
147 return status;
148 }
149#endif /* PJMEDIA_HAS_G722_CODEC */
150
Benny Prijonoeebe9af2006-06-13 22:57:13 +0000151#if PJMEDIA_HAS_L16_CODEC
152 /* Register L16 family codecs, but disable all */
153 status = pjmedia_codec_l16_init(pjsua_var.med_endpt, 0);
154 if (status != PJ_SUCCESS) {
155 pjsua_perror(THIS_FILE, "Error initializing L16 codecs",
156 status);
157 return status;
158 }
159
160 /* Disable ALL L16 codecs */
161 codec_id = pj_str("L16");
162 pjmedia_codec_mgr_set_codec_priority(
163 pjmedia_endpt_get_codec_mgr(pjsua_var.med_endpt),
164 &codec_id, PJMEDIA_CODEC_PRIO_DISABLED);
165
166#endif /* PJMEDIA_HAS_L16_CODEC */
167
168
169 /* Save additional conference bridge parameters for future
170 * reference.
171 */
Benny Prijono7d60d052008-03-29 12:24:20 +0000172 pjsua_var.mconf_cfg.channel_count = pjsua_var.media_cfg.channel_count;
Benny Prijonoeebe9af2006-06-13 22:57:13 +0000173 pjsua_var.mconf_cfg.bits_per_sample = 16;
Benny Prijono6e7c5ad2008-03-13 10:15:16 +0000174 pjsua_var.mconf_cfg.samples_per_frame = pjsua_var.media_cfg.clock_rate *
175 pjsua_var.mconf_cfg.channel_count *
176 pjsua_var.media_cfg.audio_frame_ptime /
177 1000;
Benny Prijonoeebe9af2006-06-13 22:57:13 +0000178
Benny Prijono0498d902006-06-19 14:49:14 +0000179 /* Init options for conference bridge. */
180 opt = PJMEDIA_CONF_NO_DEVICE;
181 if (pjsua_var.media_cfg.quality >= 3 &&
Benny Prijono7ca96da2006-08-07 12:11:40 +0000182 pjsua_var.media_cfg.quality <= 4)
Benny Prijono0498d902006-06-19 14:49:14 +0000183 {
184 opt |= PJMEDIA_CONF_SMALL_FILTER;
185 }
186 else if (pjsua_var.media_cfg.quality < 3) {
187 opt |= PJMEDIA_CONF_USE_LINEAR;
188 }
189
190
Benny Prijonoeebe9af2006-06-13 22:57:13 +0000191 /* Init conference bridge. */
192 status = pjmedia_conf_create(pjsua_var.pool,
193 pjsua_var.media_cfg.max_media_ports,
194 pjsua_var.media_cfg.clock_rate,
195 pjsua_var.mconf_cfg.channel_count,
196 pjsua_var.mconf_cfg.samples_per_frame,
197 pjsua_var.mconf_cfg.bits_per_sample,
Benny Prijono0498d902006-06-19 14:49:14 +0000198 opt, &pjsua_var.mconf);
Benny Prijonoeebe9af2006-06-13 22:57:13 +0000199 if (status != PJ_SUCCESS) {
Benny Prijono50f19b32008-03-11 13:15:43 +0000200 pjsua_perror(THIS_FILE, "Error creating conference bridge",
Benny Prijonoeebe9af2006-06-13 22:57:13 +0000201 status);
202 return status;
203 }
204
205 /* Create null port just in case user wants to use null sound. */
206 status = pjmedia_null_port_create(pjsua_var.pool,
207 pjsua_var.media_cfg.clock_rate,
208 pjsua_var.mconf_cfg.channel_count,
209 pjsua_var.mconf_cfg.samples_per_frame,
210 pjsua_var.mconf_cfg.bits_per_sample,
211 &pjsua_var.null_port);
212 PJ_ASSERT_RETURN(status == PJ_SUCCESS, status);
213
Benny Prijono6ba8c542007-10-16 01:34:14 +0000214 /* Perform NAT detection */
215 pjsua_detect_nat_type();
216
Benny Prijonoeebe9af2006-06-13 22:57:13 +0000217 return PJ_SUCCESS;
218}
219
220
221/*
222 * Create RTP and RTCP socket pair, and possibly resolve their public
223 * address via STUN.
224 */
225static pj_status_t create_rtp_rtcp_sock(const pjsua_transport_config *cfg,
226 pjmedia_sock_info *skinfo)
227{
228 enum {
229 RTP_RETRY = 100
230 };
231 int i;
Benny Prijono0a5cad82006-09-26 13:21:02 +0000232 pj_sockaddr_in bound_addr;
Benny Prijonoeebe9af2006-06-13 22:57:13 +0000233 pj_sockaddr_in mapped_addr[2];
234 pj_status_t status = PJ_SUCCESS;
Benny Prijono38fb3ea2008-01-02 08:27:03 +0000235 char addr_buf[PJ_INET6_ADDRSTRLEN+2];
Benny Prijonoeebe9af2006-06-13 22:57:13 +0000236 pj_sock_t sock[2];
237
Benny Prijonoc97608e2007-03-23 16:34:20 +0000238 /* Make sure STUN server resolution has completed */
239 status = pjsua_resolve_stun_server(PJ_TRUE);
240 if (status != PJ_SUCCESS) {
241 pjsua_perror(THIS_FILE, "Error resolving STUN server", status);
242 return status;
243 }
244
Benny Prijonode479562007-03-15 10:23:55 +0000245 if (next_rtp_port == 0)
246 next_rtp_port = (pj_uint16_t)cfg->port;
Benny Prijonoeebe9af2006-06-13 22:57:13 +0000247
248 for (i=0; i<2; ++i)
249 sock[i] = PJ_INVALID_SOCKET;
250
Benny Prijono0a5cad82006-09-26 13:21:02 +0000251 bound_addr.sin_addr.s_addr = PJ_INADDR_ANY;
252 if (cfg->bound_addr.slen) {
253 status = pj_sockaddr_in_set_str_addr(&bound_addr, &cfg->bound_addr);
254 if (status != PJ_SUCCESS) {
255 pjsua_perror(THIS_FILE, "Unable to resolve transport bind address",
256 status);
257 return status;
258 }
259 }
Benny Prijonoeebe9af2006-06-13 22:57:13 +0000260
261 /* Loop retry to bind RTP and RTCP sockets. */
Benny Prijonode479562007-03-15 10:23:55 +0000262 for (i=0; i<RTP_RETRY; ++i, next_rtp_port += 2) {
Benny Prijonoeebe9af2006-06-13 22:57:13 +0000263
264 /* Create and bind RTP socket. */
Benny Prijono8ab968f2007-07-20 08:08:30 +0000265 status = pj_sock_socket(pj_AF_INET(), pj_SOCK_DGRAM(), 0, &sock[0]);
Benny Prijonoeebe9af2006-06-13 22:57:13 +0000266 if (status != PJ_SUCCESS) {
267 pjsua_perror(THIS_FILE, "socket() error", status);
268 return status;
269 }
270
Benny Prijono0cdcd3f2007-12-09 14:14:11 +0000271 status=pj_sock_bind_in(sock[0], pj_ntohl(bound_addr.sin_addr.s_addr),
272 next_rtp_port);
Benny Prijonoeebe9af2006-06-13 22:57:13 +0000273 if (status != PJ_SUCCESS) {
274 pj_sock_close(sock[0]);
275 sock[0] = PJ_INVALID_SOCKET;
276 continue;
277 }
278
279 /* Create and bind RTCP socket. */
Benny Prijono8ab968f2007-07-20 08:08:30 +0000280 status = pj_sock_socket(pj_AF_INET(), pj_SOCK_DGRAM(), 0, &sock[1]);
Benny Prijonoeebe9af2006-06-13 22:57:13 +0000281 if (status != PJ_SUCCESS) {
282 pjsua_perror(THIS_FILE, "socket() error", status);
283 pj_sock_close(sock[0]);
284 return status;
285 }
286
Benny Prijono0cdcd3f2007-12-09 14:14:11 +0000287 status=pj_sock_bind_in(sock[1], pj_ntohl(bound_addr.sin_addr.s_addr),
288 (pj_uint16_t)(next_rtp_port+1));
Benny Prijonoeebe9af2006-06-13 22:57:13 +0000289 if (status != PJ_SUCCESS) {
290 pj_sock_close(sock[0]);
291 sock[0] = PJ_INVALID_SOCKET;
292
293 pj_sock_close(sock[1]);
294 sock[1] = PJ_INVALID_SOCKET;
295 continue;
296 }
297
298 /*
299 * If we're configured to use STUN, then find out the mapped address,
300 * and make sure that the mapped RTCP port is adjacent with the RTP.
301 */
Benny Prijonoc97608e2007-03-23 16:34:20 +0000302 if (pjsua_var.stun_srv.addr.sa_family != 0) {
303 char ip_addr[32];
304 pj_str_t stun_srv;
305
306 pj_ansi_strcpy(ip_addr,
307 pj_inet_ntoa(pjsua_var.stun_srv.ipv4.sin_addr));
308 stun_srv = pj_str(ip_addr);
309
Benny Prijono14c2b862007-02-21 00:40:05 +0000310 status=pjstun_get_mapped_addr(&pjsua_var.cp.factory, 2, sock,
Benny Prijonoaf09dc32007-04-22 12:48:30 +0000311 &stun_srv, pj_ntohs(pjsua_var.stun_srv.ipv4.sin_port),
312 &stun_srv, pj_ntohs(pjsua_var.stun_srv.ipv4.sin_port),
Benny Prijonoeebe9af2006-06-13 22:57:13 +0000313 mapped_addr);
314 if (status != PJ_SUCCESS) {
315 pjsua_perror(THIS_FILE, "STUN resolve error", status);
316 goto on_error;
317 }
318
Benny Prijono80eee892007-11-03 22:43:23 +0000319#if PJSUA_REQUIRE_CONSECUTIVE_RTCP_PORT
Benny Prijonoeebe9af2006-06-13 22:57:13 +0000320 if (pj_ntohs(mapped_addr[1].sin_port) ==
321 pj_ntohs(mapped_addr[0].sin_port)+1)
322 {
323 /* Success! */
324 break;
325 }
326
327 pj_sock_close(sock[0]);
328 sock[0] = PJ_INVALID_SOCKET;
329
330 pj_sock_close(sock[1]);
331 sock[1] = PJ_INVALID_SOCKET;
Benny Prijono80eee892007-11-03 22:43:23 +0000332#else
333 if (pj_ntohs(mapped_addr[1].sin_port) !=
334 pj_ntohs(mapped_addr[0].sin_port)+1)
335 {
336 PJ_LOG(4,(THIS_FILE,
337 "Note: STUN mapped RTCP port %d is not adjacent"
338 " to RTP port %d",
339 pj_ntohs(mapped_addr[1].sin_port),
340 pj_ntohs(mapped_addr[0].sin_port)));
341 }
342 /* Success! */
343 break;
344#endif
Benny Prijonoeebe9af2006-06-13 22:57:13 +0000345
Benny Prijono0a5cad82006-09-26 13:21:02 +0000346 } else if (cfg->public_addr.slen) {
347
348 status = pj_sockaddr_in_init(&mapped_addr[0], &cfg->public_addr,
Benny Prijonode479562007-03-15 10:23:55 +0000349 (pj_uint16_t)next_rtp_port);
Benny Prijono0a5cad82006-09-26 13:21:02 +0000350 if (status != PJ_SUCCESS)
351 goto on_error;
352
353 status = pj_sockaddr_in_init(&mapped_addr[1], &cfg->public_addr,
Benny Prijonode479562007-03-15 10:23:55 +0000354 (pj_uint16_t)(next_rtp_port+1));
Benny Prijono0a5cad82006-09-26 13:21:02 +0000355 if (status != PJ_SUCCESS)
356 goto on_error;
357
358 break;
359
Benny Prijonoeebe9af2006-06-13 22:57:13 +0000360 } else {
Benny Prijonoeebe9af2006-06-13 22:57:13 +0000361
Benny Prijono42d08d22007-12-20 11:23:07 +0000362 if (bound_addr.sin_addr.s_addr == 0) {
363 pj_sockaddr addr;
364
365 /* Get local IP address. */
366 status = pj_gethostip(pj_AF_INET(), &addr);
367 if (status != PJ_SUCCESS)
368 goto on_error;
369
370 bound_addr.sin_addr.s_addr = addr.ipv4.sin_addr.s_addr;
371 }
Benny Prijonoeebe9af2006-06-13 22:57:13 +0000372
Benny Prijonofe0b1d62007-12-05 04:07:37 +0000373 for (i=0; i<2; ++i) {
374 pj_sockaddr_in_init(&mapped_addr[i], NULL, 0);
Benny Prijono42d08d22007-12-20 11:23:07 +0000375 mapped_addr[i].sin_addr.s_addr = bound_addr.sin_addr.s_addr;
Benny Prijonofe0b1d62007-12-05 04:07:37 +0000376 }
Benny Prijonoeebe9af2006-06-13 22:57:13 +0000377
Benny Prijonode479562007-03-15 10:23:55 +0000378 mapped_addr[0].sin_port=pj_htons((pj_uint16_t)next_rtp_port);
379 mapped_addr[1].sin_port=pj_htons((pj_uint16_t)(next_rtp_port+1));
Benny Prijonoeebe9af2006-06-13 22:57:13 +0000380 break;
381 }
382 }
383
384 if (sock[0] == PJ_INVALID_SOCKET) {
385 PJ_LOG(1,(THIS_FILE,
386 "Unable to find appropriate RTP/RTCP ports combination"));
387 goto on_error;
388 }
389
390
391 skinfo->rtp_sock = sock[0];
392 pj_memcpy(&skinfo->rtp_addr_name,
393 &mapped_addr[0], sizeof(pj_sockaddr_in));
394
395 skinfo->rtcp_sock = sock[1];
396 pj_memcpy(&skinfo->rtcp_addr_name,
397 &mapped_addr[1], sizeof(pj_sockaddr_in));
398
Benny Prijono8b22ce12008-02-08 12:57:55 +0000399 PJ_LOG(4,(THIS_FILE, "RTP socket reachable at %s",
Benny Prijono5186eae2007-12-03 14:38:25 +0000400 pj_sockaddr_print(&skinfo->rtp_addr_name, addr_buf,
401 sizeof(addr_buf), 3)));
Benny Prijono8b22ce12008-02-08 12:57:55 +0000402 PJ_LOG(4,(THIS_FILE, "RTCP socket reachable at %s",
Benny Prijono5186eae2007-12-03 14:38:25 +0000403 pj_sockaddr_print(&skinfo->rtcp_addr_name, addr_buf,
404 sizeof(addr_buf), 3)));
Benny Prijonoeebe9af2006-06-13 22:57:13 +0000405
Benny Prijonode479562007-03-15 10:23:55 +0000406 next_rtp_port += 2;
Benny Prijonoeebe9af2006-06-13 22:57:13 +0000407 return PJ_SUCCESS;
408
409on_error:
410 for (i=0; i<2; ++i) {
411 if (sock[i] != PJ_INVALID_SOCKET)
412 pj_sock_close(sock[i]);
413 }
414 return status;
415}
416
417
418/*
419 * Start pjsua media subsystem.
420 */
421pj_status_t pjsua_media_subsys_start(void)
422{
423 pj_status_t status;
424
425 /* Create media for calls, if none is specified */
426 if (pjsua_var.calls[0].med_tp == NULL) {
427 pjsua_transport_config transport_cfg;
428
429 /* Create default transport config */
430 pjsua_transport_config_default(&transport_cfg);
431 transport_cfg.port = DEFAULT_RTP_PORT;
432
433 status = pjsua_media_transports_create(&transport_cfg);
434 if (status != PJ_SUCCESS)
435 return status;
436 }
437
438 /* Create sound port if none is created yet */
Benny Prijonoe909eac2006-07-27 22:04:56 +0000439 if (pjsua_var.snd_port==NULL && pjsua_var.null_snd==NULL &&
440 !pjsua_var.no_snd)
441 {
Benny Prijonoeebe9af2006-06-13 22:57:13 +0000442 status = pjsua_set_snd_dev(pjsua_var.cap_dev, pjsua_var.play_dev);
443 if (status != PJ_SUCCESS) {
Benny Prijonod639efa2007-04-05 22:45:06 +0000444 pjsua_perror(THIS_FILE, "Error opening sound device", status);
445 return status;
Benny Prijonoeebe9af2006-06-13 22:57:13 +0000446 }
447 }
448
449 return PJ_SUCCESS;
450}
451
452
453/*
454 * Destroy pjsua media subsystem.
455 */
456pj_status_t pjsua_media_subsys_destroy(void)
457{
458 unsigned i;
459
460 close_snd_dev();
461
462 if (pjsua_var.mconf) {
463 pjmedia_conf_destroy(pjsua_var.mconf);
464 pjsua_var.mconf = NULL;
465 }
466
467 if (pjsua_var.null_port) {
468 pjmedia_port_destroy(pjsua_var.null_port);
469 pjsua_var.null_port = NULL;
470 }
471
472 /* Destroy file players */
473 for (i=0; i<PJ_ARRAY_SIZE(pjsua_var.player); ++i) {
474 if (pjsua_var.player[i].port) {
475 pjmedia_port_destroy(pjsua_var.player[i].port);
476 pjsua_var.player[i].port = NULL;
477 }
478 }
479
480 /* Destroy file recorders */
481 for (i=0; i<PJ_ARRAY_SIZE(pjsua_var.recorder); ++i) {
482 if (pjsua_var.recorder[i].port) {
483 pjmedia_port_destroy(pjsua_var.recorder[i].port);
484 pjsua_var.recorder[i].port = NULL;
485 }
486 }
487
488 /* Close media transports */
Benny Prijono0875ae82006-12-26 00:11:48 +0000489 for (i=0; i<pjsua_var.ua_cfg.max_calls; ++i) {
Benny Prijonoeebe9af2006-06-13 22:57:13 +0000490 if (pjsua_var.calls[i].med_tp) {
491 (*pjsua_var.calls[i].med_tp->op->destroy)(pjsua_var.calls[i].med_tp);
492 pjsua_var.calls[i].med_tp = NULL;
493 }
494 }
495
496 /* Destroy media endpoint. */
497 if (pjsua_var.med_endpt) {
498
499 /* Shutdown all codecs: */
500# if PJMEDIA_HAS_SPEEX_CODEC
501 pjmedia_codec_speex_deinit();
502# endif /* PJMEDIA_HAS_SPEEX_CODEC */
503
504# if PJMEDIA_HAS_GSM_CODEC
505 pjmedia_codec_gsm_deinit();
506# endif /* PJMEDIA_HAS_GSM_CODEC */
507
508# if PJMEDIA_HAS_G711_CODEC
509 pjmedia_codec_g711_deinit();
510# endif /* PJMEDIA_HAS_G711_CODEC */
511
Benny Prijono7ffd7752008-03-17 14:07:53 +0000512# if PJMEDIA_HAS_G722_CODEC
513 pjmedia_codec_g722_deinit();
514# endif /* PJMEDIA_HAS_G722_CODEC */
515
Benny Prijonoeebe9af2006-06-13 22:57:13 +0000516# if PJMEDIA_HAS_L16_CODEC
517 pjmedia_codec_l16_deinit();
518# endif /* PJMEDIA_HAS_L16_CODEC */
519
Benny Prijonoeebe9af2006-06-13 22:57:13 +0000520 pjmedia_endpt_destroy(pjsua_var.med_endpt);
521 pjsua_var.med_endpt = NULL;
Benny Prijonoeebe9af2006-06-13 22:57:13 +0000522
Benny Prijonoad2e0ca2007-04-29 12:31:51 +0000523 /* Deinitialize sound subsystem */
Benny Prijonoa41d66e2007-10-01 12:17:23 +0000524 // Not necessary, as pjmedia_snd_deinit() should have been called
525 // in pjmedia_endpt_destroy().
526 //pjmedia_snd_deinit();
Benny Prijonoad2e0ca2007-04-29 12:31:51 +0000527 }
Benny Prijonoaf1bb1e2006-11-21 12:39:31 +0000528
Benny Prijonode479562007-03-15 10:23:55 +0000529 /* Reset RTP port */
530 next_rtp_port = 0;
531
Benny Prijonoeebe9af2006-06-13 22:57:13 +0000532 return PJ_SUCCESS;
533}
534
535
Benny Prijonoc97608e2007-03-23 16:34:20 +0000536/* Create normal UDP media transports */
537static pj_status_t create_udp_media_transports(pjsua_transport_config *cfg)
Benny Prijonoeebe9af2006-06-13 22:57:13 +0000538{
Benny Prijonoeebe9af2006-06-13 22:57:13 +0000539 unsigned i;
Benny Prijono617c5bc2007-04-02 19:51:21 +0000540 pjmedia_sock_info skinfo;
Benny Prijonoeebe9af2006-06-13 22:57:13 +0000541 pj_status_t status;
542
Benny Prijonoeebe9af2006-06-13 22:57:13 +0000543 /* Create each media transport */
544 for (i=0; i<pjsua_var.ua_cfg.max_calls; ++i) {
545
Benny Prijono617c5bc2007-04-02 19:51:21 +0000546 status = create_rtp_rtcp_sock(cfg, &skinfo);
Benny Prijonoeebe9af2006-06-13 22:57:13 +0000547 if (status != PJ_SUCCESS) {
548 pjsua_perror(THIS_FILE, "Unable to create RTP/RTCP socket",
549 status);
550 goto on_error;
551 }
Benny Prijonod8179652008-01-23 20:39:07 +0000552
Benny Prijonoeebe9af2006-06-13 22:57:13 +0000553 status = pjmedia_transport_udp_attach(pjsua_var.med_endpt, NULL,
Benny Prijono617c5bc2007-04-02 19:51:21 +0000554 &skinfo, 0,
Benny Prijonoeebe9af2006-06-13 22:57:13 +0000555 &pjsua_var.calls[i].med_tp);
556 if (status != PJ_SUCCESS) {
557 pjsua_perror(THIS_FILE, "Unable to create media transport",
558 status);
559 goto on_error;
560 }
Benny Prijono00cae612006-07-31 15:19:36 +0000561
Benny Prijonod8179652008-01-23 20:39:07 +0000562 pjmedia_transport_simulate_lost(pjsua_var.calls[i].med_tp,
563 PJMEDIA_DIR_ENCODING,
564 pjsua_var.media_cfg.tx_drop_pct);
Benny Prijono00cae612006-07-31 15:19:36 +0000565
Benny Prijonod8179652008-01-23 20:39:07 +0000566 pjmedia_transport_simulate_lost(pjsua_var.calls[i].med_tp,
567 PJMEDIA_DIR_DECODING,
568 pjsua_var.media_cfg.rx_drop_pct);
Benny Prijono00cae612006-07-31 15:19:36 +0000569
Benny Prijonoeebe9af2006-06-13 22:57:13 +0000570 }
571
Benny Prijonoc97608e2007-03-23 16:34:20 +0000572 return PJ_SUCCESS;
573
574on_error:
575 for (i=0; i<pjsua_var.ua_cfg.max_calls; ++i) {
576 if (pjsua_var.calls[i].med_tp != NULL) {
577 pjmedia_transport_close(pjsua_var.calls[i].med_tp);
578 pjsua_var.calls[i].med_tp = NULL;
579 }
580 }
581
582 return status;
583}
584
585
Benny Prijono096c56c2007-09-15 08:30:16 +0000586/* This callback is called when ICE negotiation completes */
587static void on_ice_complete(pjmedia_transport *tp, pj_status_t result)
588{
589 unsigned id, c;
590 pj_bool_t found = PJ_FALSE;
591
592 /* We're only interested with failure case */
593 if (result == PJ_SUCCESS)
594 return;
595
596 /* Find call which has this media transport */
597
598 PJSUA_LOCK();
599
600 for (id=0, c=0; id<PJSUA_MAX_CALLS && c<pjsua_var.call_cnt; ++id) {
601 pjsua_call *call = &pjsua_var.calls[id];
602 if (call->inv) {
603 ++c;
604
605 if (call->med_tp == tp) {
606 call->media_st = PJSUA_CALL_MEDIA_ERROR;
607 call->media_dir = PJMEDIA_DIR_NONE;
608 found = PJ_TRUE;
609 break;
610 }
611 }
612 }
613
614 PJSUA_UNLOCK();
615
616 if (found && pjsua_var.ua_cfg.cb.on_call_media_state) {
617 pjsua_var.ua_cfg.cb.on_call_media_state(id);
618 }
619}
620
621
Benny Prijonoc97608e2007-03-23 16:34:20 +0000622/* Create ICE media transports (when ice is enabled) */
623static pj_status_t create_ice_media_transports(pjsua_transport_config *cfg)
624{
625 unsigned i;
Benny Prijonob681a2f2007-03-25 18:44:51 +0000626 pj_sockaddr_in addr;
Benny Prijonoc97608e2007-03-23 16:34:20 +0000627 pj_status_t status;
628
Benny Prijonoda9785b2007-04-02 20:43:06 +0000629 /* Make sure STUN server resolution has completed */
630 status = pjsua_resolve_stun_server(PJ_TRUE);
631 if (status != PJ_SUCCESS) {
632 pjsua_perror(THIS_FILE, "Error resolving STUN server", status);
633 return status;
634 }
635
Benny Prijonob681a2f2007-03-25 18:44:51 +0000636 pj_sockaddr_in_init(&addr, 0, (pj_uint16_t)cfg->port);
637
Benny Prijonoc97608e2007-03-23 16:34:20 +0000638 /* Create each media transport */
639 for (i=0; i<pjsua_var.ua_cfg.max_calls; ++i) {
Benny Prijonoa6bd7582007-03-28 15:49:48 +0000640 pj_ice_strans_comp comp;
Benny Prijono096c56c2007-09-15 08:30:16 +0000641 pjmedia_ice_cb ice_cb;
Benny Prijonob681a2f2007-03-25 18:44:51 +0000642 int next_port;
Benny Prijono38fb3ea2008-01-02 08:27:03 +0000643 char name[32];
Benny Prijonodc1fe222007-06-26 10:13:13 +0000644#if PJMEDIA_ADVERTISE_RTCP
645 enum { COMP_CNT=2 };
646#else
647 enum { COMP_CNT=1 };
648#endif
Benny Prijonoc97608e2007-03-23 16:34:20 +0000649
Benny Prijono096c56c2007-09-15 08:30:16 +0000650 pj_bzero(&ice_cb, sizeof(pjmedia_ice_cb));
651 ice_cb.on_ice_complete = &on_ice_complete;
652
Benny Prijono38fb3ea2008-01-02 08:27:03 +0000653 pj_ansi_snprintf(name, sizeof(name), "icetp%02d", i);
654
655 status = pjmedia_ice_create(pjsua_var.med_endpt, name, COMP_CNT,
Benny Prijono096c56c2007-09-15 08:30:16 +0000656 &pjsua_var.stun_cfg, &ice_cb,
Benny Prijonoc97608e2007-03-23 16:34:20 +0000657 &pjsua_var.calls[i].med_tp);
658 if (status != PJ_SUCCESS) {
659 pjsua_perror(THIS_FILE, "Unable to create ICE media transport",
660 status);
661 goto on_error;
662 }
663
Benny Prijonod8179652008-01-23 20:39:07 +0000664 pjmedia_transport_simulate_lost(pjsua_var.calls[i].med_tp,
665 PJMEDIA_DIR_ENCODING,
666 pjsua_var.media_cfg.tx_drop_pct);
Benny Prijono11da9bc2007-09-15 08:55:00 +0000667
Benny Prijonod8179652008-01-23 20:39:07 +0000668 pjmedia_transport_simulate_lost(pjsua_var.calls[i].med_tp,
669 PJMEDIA_DIR_DECODING,
670 pjsua_var.media_cfg.rx_drop_pct);
Benny Prijono11da9bc2007-09-15 08:55:00 +0000671
Benny Prijonob681a2f2007-03-25 18:44:51 +0000672 status = pjmedia_ice_start_init(pjsua_var.calls[i].med_tp, 0, &addr,
673 &pjsua_var.stun_srv.ipv4, NULL);
Benny Prijonoc97608e2007-03-23 16:34:20 +0000674 if (status != PJ_SUCCESS) {
Benny Prijonob681a2f2007-03-25 18:44:51 +0000675 pjsua_perror(THIS_FILE, "Error starting ICE transport",
Benny Prijonoc97608e2007-03-23 16:34:20 +0000676 status);
677 goto on_error;
678 }
679
Benny Prijonob681a2f2007-03-25 18:44:51 +0000680 pjmedia_ice_get_comp(pjsua_var.calls[i].med_tp, 1, &comp);
681 next_port = pj_ntohs(comp.local_addr.ipv4.sin_port);
682 next_port += 2;
683 addr.sin_port = pj_htons((pj_uint16_t)next_port);
Benny Prijonoc97608e2007-03-23 16:34:20 +0000684 }
685
686 /* Wait until all ICE transports are ready */
687 for (i=0; i<pjsua_var.ua_cfg.max_calls; ++i) {
Benny Prijonoc97608e2007-03-23 16:34:20 +0000688
689 /* Wait until interface status is PJ_SUCCESS */
690 for (;;) {
Benny Prijonob681a2f2007-03-25 18:44:51 +0000691 status = pjmedia_ice_get_init_status(pjsua_var.calls[i].med_tp);
Benny Prijonoc97608e2007-03-23 16:34:20 +0000692 if (status == PJ_EPENDING)
693 pjsua_handle_events(100);
694 else
695 break;
696 }
697
698 if (status != PJ_SUCCESS) {
699 pjsua_perror(THIS_FILE,
700 "Error adding STUN address to ICE media transport",
701 status);
702 goto on_error;
703 }
704
Benny Prijonoc97608e2007-03-23 16:34:20 +0000705 }
Benny Prijonoeebe9af2006-06-13 22:57:13 +0000706
707 return PJ_SUCCESS;
708
709on_error:
710 for (i=0; i<pjsua_var.ua_cfg.max_calls; ++i) {
711 if (pjsua_var.calls[i].med_tp != NULL) {
Benny Prijonoc97608e2007-03-23 16:34:20 +0000712 pjmedia_transport_close(pjsua_var.calls[i].med_tp);
Benny Prijonoeebe9af2006-06-13 22:57:13 +0000713 pjsua_var.calls[i].med_tp = NULL;
714 }
715 }
716
Benny Prijonoc97608e2007-03-23 16:34:20 +0000717 return status;
718}
719
720
721/*
722 * Create UDP media transports for all the calls. This function creates
723 * one UDP media transport for each call.
724 */
Benny Prijono1f61a8f2007-08-16 10:11:44 +0000725PJ_DEF(pj_status_t) pjsua_media_transports_create(
726 const pjsua_transport_config *app_cfg)
Benny Prijonoc97608e2007-03-23 16:34:20 +0000727{
728 pjsua_transport_config cfg;
729 unsigned i;
730 pj_status_t status;
731
732
733 /* Make sure pjsua_init() has been called */
734 PJ_ASSERT_RETURN(pjsua_var.ua_cfg.max_calls>0, PJ_EINVALIDOP);
735
736 PJSUA_LOCK();
737
738 /* Delete existing media transports */
739 for (i=0; i<pjsua_var.ua_cfg.max_calls; ++i) {
740 if (pjsua_var.calls[i].med_tp != NULL) {
741 pjmedia_transport_close(pjsua_var.calls[i].med_tp);
742 pjsua_var.calls[i].med_tp = NULL;
743 }
744 }
745
746 /* Copy config */
747 pjsua_transport_config_dup(pjsua_var.pool, &cfg, app_cfg);
748
749 if (pjsua_var.media_cfg.enable_ice) {
750 status = create_ice_media_transports(&cfg);
751 } else {
752 status = create_udp_media_transports(&cfg);
753 }
754
755
Benny Prijonoeebe9af2006-06-13 22:57:13 +0000756 PJSUA_UNLOCK();
757
758 return status;
759}
760
761
Benny Prijonoc97608e2007-03-23 16:34:20 +0000762pj_status_t pjsua_media_channel_init(pjsua_call_id call_id,
Benny Prijonod8179652008-01-23 20:39:07 +0000763 pjsip_role_e role,
Benny Prijono25b2ea12008-01-24 19:20:54 +0000764 int security_level,
765 int *sip_err_code)
Benny Prijonoc97608e2007-03-23 16:34:20 +0000766{
767 pjsua_call *call = &pjsua_var.calls[call_id];
768
Benny Prijonod8179652008-01-23 20:39:07 +0000769#if defined(PJMEDIA_HAS_SRTP) && (PJMEDIA_HAS_SRTP != 0)
770 pjsua_acc *acc = &pjsua_var.acc[call->acc_id];
771 pjmedia_srtp_setting srtp_opt;
772 pjmedia_transport *srtp;
773 pj_status_t status;
774#endif
Benny Prijonoc97608e2007-03-23 16:34:20 +0000775
Benny Prijonod8179652008-01-23 20:39:07 +0000776 PJ_UNUSED_ARG(role);
Benny Prijonoc97608e2007-03-23 16:34:20 +0000777
Benny Prijonod8179652008-01-23 20:39:07 +0000778 /* Return error if media transport has not been created yet
779 * (e.g. application is starting)
780 */
781 if (call->med_tp == NULL) {
782 return PJ_EBUSY;
Benny Prijonoc97608e2007-03-23 16:34:20 +0000783 }
784
Benny Prijonod8179652008-01-23 20:39:07 +0000785#if defined(PJMEDIA_HAS_SRTP) && (PJMEDIA_HAS_SRTP != 0)
Benny Prijono53a7c702008-04-14 02:57:29 +0000786 /* This function may be called when SRTP transport already exists
787 * (e.g: in re-invite, update), don't need to destroy/re-create.
788 */
789 if (!call->med_orig || call->med_tp == call->med_orig) {
790
791 /* Check if SRTP requires secure signaling */
792 if (acc->cfg.use_srtp != PJMEDIA_SRTP_DISABLED) {
793 if (security_level < acc->cfg.srtp_secure_signaling) {
794 if (sip_err_code)
795 *sip_err_code = PJSIP_SC_NOT_ACCEPTABLE;
796 return PJSIP_ESESSIONINSECURE;
797 }
Benny Prijonod8179652008-01-23 20:39:07 +0000798 }
Benny Prijonod8179652008-01-23 20:39:07 +0000799
Benny Prijono53a7c702008-04-14 02:57:29 +0000800 /* Always create SRTP adapter */
801 pjmedia_srtp_setting_default(&srtp_opt);
802 srtp_opt.close_member_tp = PJ_FALSE;
803 srtp_opt.use = acc->cfg.use_srtp;
804 status = pjmedia_transport_srtp_create(pjsua_var.med_endpt,
805 call->med_tp,
806 &srtp_opt, &srtp);
807 if (status != PJ_SUCCESS) {
808 if (sip_err_code)
809 *sip_err_code = PJSIP_SC_INTERNAL_SERVER_ERROR;
810 return status;
811 }
Benny Prijonod8179652008-01-23 20:39:07 +0000812
Benny Prijono53a7c702008-04-14 02:57:29 +0000813 /* Set SRTP as current media transport */
814 call->med_orig = call->med_tp;
815 call->med_tp = srtp;
816 }
Benny Prijonod8179652008-01-23 20:39:07 +0000817#else
818 call->med_orig = call->med_tp;
819 PJ_UNUSED_ARG(security_level);
820#endif
821
Benny Prijonoc97608e2007-03-23 16:34:20 +0000822 return PJ_SUCCESS;
823}
824
825pj_status_t pjsua_media_channel_create_sdp(pjsua_call_id call_id,
826 pj_pool_t *pool,
Benny Prijonod8179652008-01-23 20:39:07 +0000827 const pjmedia_sdp_session *rem_sdp,
Benny Prijono25b2ea12008-01-24 19:20:54 +0000828 pjmedia_sdp_session **p_sdp,
829 int *sip_status_code)
Benny Prijonoc97608e2007-03-23 16:34:20 +0000830{
Benny Prijonod8179652008-01-23 20:39:07 +0000831 enum { MAX_MEDIA = 1, MEDIA_IDX = 0 };
Benny Prijonoc97608e2007-03-23 16:34:20 +0000832 pjmedia_sdp_session *sdp;
Benny Prijonoe1a5a852008-03-11 21:38:05 +0000833 pjmedia_transport_info tpinfo;
Benny Prijonoc97608e2007-03-23 16:34:20 +0000834 pjsua_call *call = &pjsua_var.calls[call_id];
835 pj_status_t status;
836
Benny Prijono55e82352007-05-10 20:49:08 +0000837 /* Return error if media transport has not been created yet
838 * (e.g. application is starting)
839 */
840 if (call->med_tp == NULL) {
841 return PJ_EBUSY;
842 }
843
Benny Prijono617c5bc2007-04-02 19:51:21 +0000844 /* Get media socket info */
Benny Prijono734fc2d2008-03-17 16:05:35 +0000845 pjmedia_transport_info_init(&tpinfo);
Benny Prijonoe1a5a852008-03-11 21:38:05 +0000846 pjmedia_transport_get_info(call->med_tp, &tpinfo);
Benny Prijono617c5bc2007-04-02 19:51:21 +0000847
848 /* Create SDP */
Benny Prijonod8179652008-01-23 20:39:07 +0000849 status = pjmedia_endpt_create_sdp(pjsua_var.med_endpt, pool, MAX_MEDIA,
Benny Prijonoe1a5a852008-03-11 21:38:05 +0000850 &tpinfo.sock_info, &sdp);
Benny Prijono25b2ea12008-01-24 19:20:54 +0000851 if (status != PJ_SUCCESS) {
852 if (sip_status_code) *sip_status_code = 500;
Benny Prijonoc97608e2007-03-23 16:34:20 +0000853 goto on_error;
Benny Prijono25b2ea12008-01-24 19:20:54 +0000854 }
Benny Prijonoc97608e2007-03-23 16:34:20 +0000855
Benny Prijono6ba8c542007-10-16 01:34:14 +0000856 /* Add NAT info in the SDP */
857 if (pjsua_var.ua_cfg.nat_type_in_sdp) {
858 pjmedia_sdp_attr *a;
859 pj_str_t value;
860 char nat_info[80];
861
862 value.ptr = nat_info;
863 if (pjsua_var.ua_cfg.nat_type_in_sdp == 1) {
864 value.slen = pj_ansi_snprintf(nat_info, sizeof(nat_info),
865 "%d", pjsua_var.nat_type);
866 } else {
867 const char *type_name = pj_stun_get_nat_name(pjsua_var.nat_type);
868 value.slen = pj_ansi_snprintf(nat_info, sizeof(nat_info),
869 "%d %s",
870 pjsua_var.nat_type,
871 type_name);
872 }
873
874 a = pjmedia_sdp_attr_create(pool, "X-nat", &value);
875
876 pjmedia_sdp_attr_add(&sdp->attr_count, sdp->attr, a);
877
878 }
879
Benny Prijonod8179652008-01-23 20:39:07 +0000880 /* Give the SDP to media transport */
Benny Prijono2dbed822008-02-21 10:08:27 +0000881 status = pjmedia_transport_media_create(call->med_tp, pool, 0,
Benny Prijonod8179652008-01-23 20:39:07 +0000882 sdp, rem_sdp, MEDIA_IDX);
Benny Prijono25b2ea12008-01-24 19:20:54 +0000883 if (status != PJ_SUCCESS) {
884 if (sip_status_code) *sip_status_code = PJSIP_SC_NOT_ACCEPTABLE;
Benny Prijonod8179652008-01-23 20:39:07 +0000885 goto on_error;
Benny Prijono25b2ea12008-01-24 19:20:54 +0000886 }
Benny Prijonoc97608e2007-03-23 16:34:20 +0000887
888 *p_sdp = sdp;
889 return PJ_SUCCESS;
890
891on_error:
892 pjsua_media_channel_deinit(call_id);
893 return status;
894
895}
896
897
898static void stop_media_session(pjsua_call_id call_id)
899{
900 pjsua_call *call = &pjsua_var.calls[call_id];
901
Benny Prijono53a7c702008-04-14 02:57:29 +0000902 pjmedia_transport_media_stop(call->med_tp);
903
Benny Prijonoc97608e2007-03-23 16:34:20 +0000904 if (call->conf_slot != PJSUA_INVALID_ID) {
905 pjmedia_conf_remove_port(pjsua_var.mconf, call->conf_slot);
906 call->conf_slot = PJSUA_INVALID_ID;
907 }
908
909 if (call->session) {
Benny Prijonofc13bf62008-02-20 08:56:15 +0000910 if (pjsua_var.ua_cfg.cb.on_stream_destroyed) {
911 pjsua_var.ua_cfg.cb.on_stream_destroyed(call_id, call->session, 0);
912 }
913
Benny Prijonoc97608e2007-03-23 16:34:20 +0000914 pjmedia_session_destroy(call->session);
915 call->session = NULL;
916
917 PJ_LOG(4,(THIS_FILE, "Media session for call %d is destroyed",
918 call_id));
919
920 }
921
922 call->media_st = PJSUA_CALL_MEDIA_NONE;
923}
924
925pj_status_t pjsua_media_channel_deinit(pjsua_call_id call_id)
926{
927 pjsua_call *call = &pjsua_var.calls[call_id];
928
929 stop_media_session(call_id);
930
Benny Prijonod8179652008-01-23 20:39:07 +0000931 pjmedia_transport_media_stop(call->med_tp);
Benny Prijonoc97608e2007-03-23 16:34:20 +0000932
Benny Prijono68f9e4f2008-03-21 08:56:02 +0000933 if (call->med_orig && call->med_tp != call->med_orig) {
Benny Prijonod8179652008-01-23 20:39:07 +0000934 pjmedia_transport_close(call->med_tp);
935 call->med_tp = call->med_orig;
936 }
Benny Prijonoc97608e2007-03-23 16:34:20 +0000937 return PJ_SUCCESS;
938}
939
940
941/*
942 * DTMF callback from the stream.
943 */
944static void dtmf_callback(pjmedia_stream *strm, void *user_data,
945 int digit)
946{
947 PJ_UNUSED_ARG(strm);
948
Benny Prijono0c068262008-02-14 14:38:52 +0000949 /* For discussions about call mutex protection related to this
950 * callback, please see ticket #460:
951 * http://trac.pjsip.org/repos/ticket/460#comment:4
952 */
Benny Prijonoc97608e2007-03-23 16:34:20 +0000953 if (pjsua_var.ua_cfg.cb.on_dtmf_digit) {
954 pjsua_call_id call_id;
955
Benny Prijonod8179652008-01-23 20:39:07 +0000956 call_id = (pjsua_call_id)(long)user_data;
Benny Prijonoc97608e2007-03-23 16:34:20 +0000957 pjsua_var.ua_cfg.cb.on_dtmf_digit(call_id, digit);
958 }
959}
960
961
962pj_status_t pjsua_media_channel_update(pjsua_call_id call_id,
Benny Prijonod8179652008-01-23 20:39:07 +0000963 pjmedia_sdp_session *local_sdp,
Benny Prijonodbce2cf2007-03-28 16:24:00 +0000964 const pjmedia_sdp_session *remote_sdp)
Benny Prijonoc97608e2007-03-23 16:34:20 +0000965{
Benny Prijono91e567e2007-12-28 08:51:58 +0000966 unsigned i;
Benny Prijonoc97608e2007-03-23 16:34:20 +0000967 int prev_media_st = 0;
968 pjsua_call *call = &pjsua_var.calls[call_id];
969 pjmedia_session_info sess_info;
Benny Prijono91e567e2007-12-28 08:51:58 +0000970 pjmedia_stream_info *si = NULL;
Benny Prijonoc97608e2007-03-23 16:34:20 +0000971 pjmedia_port *media_port;
Benny Prijonoc97608e2007-03-23 16:34:20 +0000972 pj_status_t status;
973
974 /* Destroy existing media session, if any. */
975 prev_media_st = call->media_st;
976 stop_media_session(call->index);
977
978 /* Create media session info based on SDP parameters.
Benny Prijonoc97608e2007-03-23 16:34:20 +0000979 */
980 status = pjmedia_session_info_from_sdp( call->inv->dlg->pool,
981 pjsua_var.med_endpt,
Benny Prijono91e567e2007-12-28 08:51:58 +0000982 PJMEDIA_MAX_SDP_MEDIA, &sess_info,
Benny Prijonoc97608e2007-03-23 16:34:20 +0000983 local_sdp, remote_sdp);
984 if (status != PJ_SUCCESS)
985 return status;
986
Benny Prijono91e567e2007-12-28 08:51:58 +0000987 /* Find which session is audio (we only support audio for now) */
988 for (i=0; i < sess_info.stream_cnt; ++i) {
989 if (sess_info.stream_info[i].type == PJMEDIA_TYPE_AUDIO &&
Benny Prijonod8179652008-01-23 20:39:07 +0000990 (sess_info.stream_info[i].proto == PJMEDIA_TP_PROTO_RTP_AVP ||
991 sess_info.stream_info[i].proto == PJMEDIA_TP_PROTO_RTP_SAVP))
Benny Prijono91e567e2007-12-28 08:51:58 +0000992 {
993 si = &sess_info.stream_info[i];
994 break;
995 }
996 }
997
998 if (si == NULL) {
999 /* Not found */
1000 return PJMEDIA_EINVALIMEDIATYPE;
1001 }
1002
1003
1004 /* Reset session info with only one media stream */
1005 sess_info.stream_cnt = 1;
1006 if (si != &sess_info.stream_info[0])
1007 pj_memcpy(&sess_info.stream_info[0], si, sizeof(pjmedia_stream_info));
Benny Prijonoc97608e2007-03-23 16:34:20 +00001008
1009 /* Check if media is put on-hold */
Benny Prijono91e567e2007-12-28 08:51:58 +00001010 if (sess_info.stream_cnt == 0 || si->dir == PJMEDIA_DIR_NONE)
Benny Prijonoc97608e2007-03-23 16:34:20 +00001011 {
1012
1013 /* Determine who puts the call on-hold */
1014 if (prev_media_st == PJSUA_CALL_MEDIA_ACTIVE) {
1015 if (pjmedia_sdp_neg_was_answer_remote(call->inv->neg)) {
1016 /* It was local who offer hold */
1017 call->media_st = PJSUA_CALL_MEDIA_LOCAL_HOLD;
1018 } else {
1019 call->media_st = PJSUA_CALL_MEDIA_REMOTE_HOLD;
1020 }
1021 }
1022
1023 call->media_dir = PJMEDIA_DIR_NONE;
1024
Benny Prijonod8179652008-01-23 20:39:07 +00001025 /* Shutdown transport's session */
1026 pjmedia_transport_media_stop(call->med_tp);
Benny Prijono667952e2007-04-02 19:27:54 +00001027
Benny Prijonoc97608e2007-03-23 16:34:20 +00001028 /* No need because we need keepalive? */
1029
Benny Prijono68f9e4f2008-03-21 08:56:02 +00001030 /* Close upper entry of transport stack */
1031 if (call->med_orig && (call->med_tp != call->med_orig)) {
1032 pjmedia_transport_close(call->med_tp);
1033 call->med_tp = call->med_orig;
1034 }
1035
Benny Prijonoc97608e2007-03-23 16:34:20 +00001036 } else {
Benny Prijonod8179652008-01-23 20:39:07 +00001037 /* Start media transport */
1038 status = pjmedia_transport_media_start(call->med_tp,
1039 call->inv->pool,
1040 local_sdp, remote_sdp, 0);
1041 if (status != PJ_SUCCESS)
1042 return status;
Benny Prijonoc97608e2007-03-23 16:34:20 +00001043
1044 /* Override ptime, if this option is specified. */
1045 if (pjsua_var.media_cfg.ptime != 0) {
Benny Prijono91e567e2007-12-28 08:51:58 +00001046 si->param->setting.frm_per_pkt = (pj_uint8_t)
1047 (pjsua_var.media_cfg.ptime / si->param->info.frm_ptime);
1048 if (si->param->setting.frm_per_pkt == 0)
1049 si->param->setting.frm_per_pkt = 1;
Benny Prijonoc97608e2007-03-23 16:34:20 +00001050 }
1051
1052 /* Disable VAD, if this option is specified. */
1053 if (pjsua_var.media_cfg.no_vad) {
Benny Prijono91e567e2007-12-28 08:51:58 +00001054 si->param->setting.vad = 0;
Benny Prijonoc97608e2007-03-23 16:34:20 +00001055 }
1056
1057
1058 /* Optionally, application may modify other stream settings here
1059 * (such as jitter buffer parameters, codec ptime, etc.)
1060 */
Benny Prijono91e567e2007-12-28 08:51:58 +00001061 si->jb_init = pjsua_var.media_cfg.jb_init;
1062 si->jb_min_pre = pjsua_var.media_cfg.jb_min_pre;
1063 si->jb_max_pre = pjsua_var.media_cfg.jb_max_pre;
1064 si->jb_max = pjsua_var.media_cfg.jb_max;
Benny Prijonoc97608e2007-03-23 16:34:20 +00001065
Benny Prijono8147f402007-11-21 14:50:07 +00001066 /* Set SSRC */
Benny Prijono91e567e2007-12-28 08:51:58 +00001067 si->ssrc = call->ssrc;
Benny Prijono8147f402007-11-21 14:50:07 +00001068
Benny Prijonoc97608e2007-03-23 16:34:20 +00001069 /* Create session based on session info. */
1070 status = pjmedia_session_create( pjsua_var.med_endpt, &sess_info,
1071 &call->med_tp,
1072 call, &call->session );
1073 if (status != PJ_SUCCESS) {
1074 return status;
1075 }
1076
1077 /* If DTMF callback is installed by application, install our
1078 * callback to the session.
1079 */
1080 if (pjsua_var.ua_cfg.cb.on_dtmf_digit) {
1081 pjmedia_session_set_dtmf_callback(call->session, 0,
1082 &dtmf_callback,
Benny Prijonod8179652008-01-23 20:39:07 +00001083 (void*)(long)(call->index));
Benny Prijonoc97608e2007-03-23 16:34:20 +00001084 }
1085
1086 /* Get the port interface of the first stream in the session.
1087 * We need the port interface to add to the conference bridge.
1088 */
1089 pjmedia_session_get_port(call->session, 0, &media_port);
1090
Benny Prijonofc13bf62008-02-20 08:56:15 +00001091 /* Notify application about stream creation.
1092 * Note: application may modify media_port to point to different
1093 * media port
1094 */
1095 if (pjsua_var.ua_cfg.cb.on_stream_created) {
1096 pjsua_var.ua_cfg.cb.on_stream_created(call_id, call->session,
1097 0, &media_port);
1098 }
Benny Prijonoc97608e2007-03-23 16:34:20 +00001099
1100 /*
1101 * Add the call to conference bridge.
1102 */
Benny Prijono38fb3ea2008-01-02 08:27:03 +00001103 {
1104 char tmp[PJSIP_MAX_URL_SIZE];
1105 pj_str_t port_name;
1106
1107 port_name.ptr = tmp;
1108 port_name.slen = pjsip_uri_print(PJSIP_URI_IN_REQ_URI,
1109 call->inv->dlg->remote.info->uri,
1110 tmp, sizeof(tmp));
1111 if (port_name.slen < 1) {
1112 port_name = pj_str("call");
1113 }
1114 status = pjmedia_conf_add_port( pjsua_var.mconf, call->inv->pool,
1115 media_port,
1116 &port_name,
1117 (unsigned*)&call->conf_slot);
1118 if (status != PJ_SUCCESS) {
1119 return status;
1120 }
Benny Prijonoc97608e2007-03-23 16:34:20 +00001121 }
1122
1123 /* Call's media state is active */
1124 call->media_st = PJSUA_CALL_MEDIA_ACTIVE;
Benny Prijono91e567e2007-12-28 08:51:58 +00001125 call->media_dir = si->dir;
Benny Prijonoc97608e2007-03-23 16:34:20 +00001126 }
1127
1128 /* Print info. */
1129 {
1130 char info[80];
1131 int info_len = 0;
1132 unsigned i;
1133
1134 for (i=0; i<sess_info.stream_cnt; ++i) {
1135 int len;
1136 const char *dir;
1137 pjmedia_stream_info *strm_info = &sess_info.stream_info[i];
1138
1139 switch (strm_info->dir) {
1140 case PJMEDIA_DIR_NONE:
1141 dir = "inactive";
1142 break;
1143 case PJMEDIA_DIR_ENCODING:
1144 dir = "sendonly";
1145 break;
1146 case PJMEDIA_DIR_DECODING:
1147 dir = "recvonly";
1148 break;
1149 case PJMEDIA_DIR_ENCODING_DECODING:
1150 dir = "sendrecv";
1151 break;
1152 default:
1153 dir = "unknown";
1154 break;
1155 }
1156 len = pj_ansi_sprintf( info+info_len,
1157 ", stream #%d: %.*s (%s)", i,
1158 (int)strm_info->fmt.encoding_name.slen,
1159 strm_info->fmt.encoding_name.ptr,
1160 dir);
1161 if (len > 0)
1162 info_len += len;
1163 }
1164 PJ_LOG(4,(THIS_FILE,"Media updates%s", info));
1165 }
1166
1167 return PJ_SUCCESS;
1168}
1169
1170
Benny Prijonoeebe9af2006-06-13 22:57:13 +00001171/*
1172 * Get maxinum number of conference ports.
1173 */
1174PJ_DEF(unsigned) pjsua_conf_get_max_ports(void)
1175{
1176 return pjsua_var.media_cfg.max_media_ports;
1177}
1178
1179
1180/*
1181 * Get current number of active ports in the bridge.
1182 */
1183PJ_DEF(unsigned) pjsua_conf_get_active_ports(void)
1184{
Benny Prijono38fb3ea2008-01-02 08:27:03 +00001185 unsigned ports[PJSUA_MAX_CONF_PORTS];
Benny Prijonoeebe9af2006-06-13 22:57:13 +00001186 unsigned count = PJ_ARRAY_SIZE(ports);
1187 pj_status_t status;
1188
1189 status = pjmedia_conf_enum_ports(pjsua_var.mconf, ports, &count);
1190 if (status != PJ_SUCCESS)
1191 count = 0;
1192
1193 return count;
1194}
1195
1196
1197/*
1198 * Enumerate all conference ports.
1199 */
1200PJ_DEF(pj_status_t) pjsua_enum_conf_ports(pjsua_conf_port_id id[],
1201 unsigned *count)
1202{
1203 return pjmedia_conf_enum_ports(pjsua_var.mconf, (unsigned*)id, count);
1204}
1205
1206
1207/*
1208 * Get information about the specified conference port
1209 */
1210PJ_DEF(pj_status_t) pjsua_conf_get_port_info( pjsua_conf_port_id id,
1211 pjsua_conf_port_info *info)
1212{
1213 pjmedia_conf_port_info cinfo;
Benny Prijono0498d902006-06-19 14:49:14 +00001214 unsigned i;
Benny Prijonoeebe9af2006-06-13 22:57:13 +00001215 pj_status_t status;
1216
1217 status = pjmedia_conf_get_port_info( pjsua_var.mconf, id, &cinfo);
1218 if (status != PJ_SUCCESS)
1219 return status;
1220
Benny Prijonoac623b32006-07-03 15:19:31 +00001221 pj_bzero(info, sizeof(*info));
Benny Prijonoeebe9af2006-06-13 22:57:13 +00001222 info->slot_id = id;
1223 info->name = cinfo.name;
1224 info->clock_rate = cinfo.clock_rate;
1225 info->channel_count = cinfo.channel_count;
1226 info->samples_per_frame = cinfo.samples_per_frame;
1227 info->bits_per_sample = cinfo.bits_per_sample;
1228
1229 /* Build array of listeners */
Benny Prijonoc78c3a32006-06-16 15:54:43 +00001230 info->listener_cnt = cinfo.listener_cnt;
1231 for (i=0; i<cinfo.listener_cnt; ++i) {
1232 info->listeners[i] = cinfo.listener_slots[i];
Benny Prijonoeebe9af2006-06-13 22:57:13 +00001233 }
1234
1235 return PJ_SUCCESS;
1236}
1237
1238
1239/*
Benny Prijonoe909eac2006-07-27 22:04:56 +00001240 * Add arbitrary media port to PJSUA's conference bridge.
1241 */
1242PJ_DEF(pj_status_t) pjsua_conf_add_port( pj_pool_t *pool,
1243 pjmedia_port *port,
1244 pjsua_conf_port_id *p_id)
1245{
1246 pj_status_t status;
1247
1248 status = pjmedia_conf_add_port(pjsua_var.mconf, pool,
1249 port, NULL, (unsigned*)p_id);
1250 if (status != PJ_SUCCESS) {
1251 if (p_id)
1252 *p_id = PJSUA_INVALID_ID;
1253 }
1254
1255 return status;
1256}
1257
1258
1259/*
1260 * Remove arbitrary slot from the conference bridge.
1261 */
1262PJ_DEF(pj_status_t) pjsua_conf_remove_port(pjsua_conf_port_id id)
1263{
1264 return pjmedia_conf_remove_port(pjsua_var.mconf, (unsigned)id);
1265}
1266
1267
1268/*
Benny Prijonoeebe9af2006-06-13 22:57:13 +00001269 * Establish unidirectional media flow from souce to sink.
1270 */
1271PJ_DEF(pj_status_t) pjsua_conf_connect( pjsua_conf_port_id source,
1272 pjsua_conf_port_id sink)
1273{
1274 return pjmedia_conf_connect_port(pjsua_var.mconf, source, sink, 0);
1275}
1276
1277
1278/*
1279 * Disconnect media flow from the source to destination port.
1280 */
1281PJ_DEF(pj_status_t) pjsua_conf_disconnect( pjsua_conf_port_id source,
1282 pjsua_conf_port_id sink)
1283{
1284 return pjmedia_conf_disconnect_port(pjsua_var.mconf, source, sink);
1285}
1286
1287
Benny Prijono6dd967c2006-12-26 02:27:14 +00001288/*
1289 * Adjust the signal level to be transmitted from the bridge to the
1290 * specified port by making it louder or quieter.
1291 */
1292PJ_DEF(pj_status_t) pjsua_conf_adjust_tx_level(pjsua_conf_port_id slot,
1293 float level)
1294{
1295 return pjmedia_conf_adjust_tx_level(pjsua_var.mconf, slot,
1296 (int)((level-1) * 128));
1297}
1298
1299/*
1300 * Adjust the signal level to be received from the specified port (to
1301 * the bridge) by making it louder or quieter.
1302 */
1303PJ_DEF(pj_status_t) pjsua_conf_adjust_rx_level(pjsua_conf_port_id slot,
1304 float level)
1305{
1306 return pjmedia_conf_adjust_rx_level(pjsua_var.mconf, slot,
1307 (int)((level-1) * 128));
1308}
1309
1310
1311/*
1312 * Get last signal level transmitted to or received from the specified port.
1313 */
1314PJ_DEF(pj_status_t) pjsua_conf_get_signal_level(pjsua_conf_port_id slot,
1315 unsigned *tx_level,
1316 unsigned *rx_level)
1317{
1318 return pjmedia_conf_get_signal_level(pjsua_var.mconf, slot,
1319 tx_level, rx_level);
1320}
1321
Benny Prijonoeebe9af2006-06-13 22:57:13 +00001322/*****************************************************************************
1323 * File player.
1324 */
1325
Benny Prijonod5696da2007-07-17 16:25:45 +00001326static char* get_basename(const char *path, unsigned len)
1327{
1328 char *p = ((char*)path) + len;
1329
1330 if (len==0)
1331 return p;
1332
Benny Prijono1f61a8f2007-08-16 10:11:44 +00001333 for (--p; p!=path && *p!='/' && *p!='\\'; ) --p;
Benny Prijonod5696da2007-07-17 16:25:45 +00001334
1335 return (p==path) ? p : p+1;
1336}
1337
1338
Benny Prijonoeebe9af2006-06-13 22:57:13 +00001339/*
1340 * Create a file player, and automatically connect this player to
1341 * the conference bridge.
1342 */
1343PJ_DEF(pj_status_t) pjsua_player_create( const pj_str_t *filename,
1344 unsigned options,
Benny Prijonoeebe9af2006-06-13 22:57:13 +00001345 pjsua_player_id *p_id)
1346{
1347 unsigned slot, file_id;
Benny Prijonoa66c3312007-01-21 23:12:40 +00001348 char path[PJ_MAXPATH];
Benny Prijonod5696da2007-07-17 16:25:45 +00001349 pj_pool_t *pool;
Benny Prijonoeebe9af2006-06-13 22:57:13 +00001350 pjmedia_port *port;
1351 pj_status_t status;
1352
1353 if (pjsua_var.player_cnt >= PJ_ARRAY_SIZE(pjsua_var.player))
1354 return PJ_ETOOMANY;
1355
1356 PJSUA_LOCK();
1357
1358 for (file_id=0; file_id<PJ_ARRAY_SIZE(pjsua_var.player); ++file_id) {
1359 if (pjsua_var.player[file_id].port == NULL)
1360 break;
1361 }
1362
1363 if (file_id == PJ_ARRAY_SIZE(pjsua_var.player)) {
1364 /* This is unexpected */
1365 PJSUA_UNLOCK();
1366 pj_assert(0);
1367 return PJ_EBUG;
1368 }
1369
1370 pj_memcpy(path, filename->ptr, filename->slen);
1371 path[filename->slen] = '\0';
Benny Prijonod5696da2007-07-17 16:25:45 +00001372
1373 pool = pjsua_pool_create(get_basename(path, filename->slen), 1000, 1000);
1374 if (!pool) {
1375 PJSUA_UNLOCK();
1376 return PJ_ENOMEM;
1377 }
1378
1379 status = pjmedia_wav_player_port_create(pool, path,
Benny Prijonoeebe9af2006-06-13 22:57:13 +00001380 pjsua_var.mconf_cfg.samples_per_frame *
1381 1000 / pjsua_var.media_cfg.clock_rate,
Benny Prijono00cae612006-07-31 15:19:36 +00001382 options, 0, &port);
Benny Prijonoeebe9af2006-06-13 22:57:13 +00001383 if (status != PJ_SUCCESS) {
1384 PJSUA_UNLOCK();
1385 pjsua_perror(THIS_FILE, "Unable to open file for playback", status);
Benny Prijonod5696da2007-07-17 16:25:45 +00001386 pj_pool_release(pool);
Benny Prijonoeebe9af2006-06-13 22:57:13 +00001387 return status;
1388 }
1389
Benny Prijono5297af92008-03-18 13:40:40 +00001390 status = pjmedia_conf_add_port(pjsua_var.mconf, pool,
Benny Prijonoeebe9af2006-06-13 22:57:13 +00001391 port, filename, &slot);
1392 if (status != PJ_SUCCESS) {
1393 pjmedia_port_destroy(port);
1394 PJSUA_UNLOCK();
Benny Prijono32e4f492007-01-24 00:44:26 +00001395 pjsua_perror(THIS_FILE, "Unable to add file to conference bridge",
1396 status);
Benny Prijonod5696da2007-07-17 16:25:45 +00001397 pj_pool_release(pool);
Benny Prijonoeebe9af2006-06-13 22:57:13 +00001398 return status;
1399 }
1400
Benny Prijonoa66c3312007-01-21 23:12:40 +00001401 pjsua_var.player[file_id].type = 0;
Benny Prijonod5696da2007-07-17 16:25:45 +00001402 pjsua_var.player[file_id].pool = pool;
Benny Prijonoeebe9af2006-06-13 22:57:13 +00001403 pjsua_var.player[file_id].port = port;
1404 pjsua_var.player[file_id].slot = slot;
1405
1406 if (p_id) *p_id = file_id;
1407
1408 ++pjsua_var.player_cnt;
1409
1410 PJSUA_UNLOCK();
1411 return PJ_SUCCESS;
1412}
1413
1414
1415/*
Benny Prijonoa66c3312007-01-21 23:12:40 +00001416 * Create a file playlist media port, and automatically add the port
1417 * to the conference bridge.
1418 */
1419PJ_DEF(pj_status_t) pjsua_playlist_create( const pj_str_t file_names[],
1420 unsigned file_count,
1421 const pj_str_t *label,
1422 unsigned options,
1423 pjsua_player_id *p_id)
1424{
1425 unsigned slot, file_id, ptime;
Benny Prijonod5696da2007-07-17 16:25:45 +00001426 pj_pool_t *pool;
Benny Prijonoa66c3312007-01-21 23:12:40 +00001427 pjmedia_port *port;
1428 pj_status_t status;
1429
1430 if (pjsua_var.player_cnt >= PJ_ARRAY_SIZE(pjsua_var.player))
1431 return PJ_ETOOMANY;
1432
1433 PJSUA_LOCK();
1434
1435 for (file_id=0; file_id<PJ_ARRAY_SIZE(pjsua_var.player); ++file_id) {
1436 if (pjsua_var.player[file_id].port == NULL)
1437 break;
1438 }
1439
1440 if (file_id == PJ_ARRAY_SIZE(pjsua_var.player)) {
1441 /* This is unexpected */
1442 PJSUA_UNLOCK();
1443 pj_assert(0);
1444 return PJ_EBUG;
1445 }
1446
1447
1448 ptime = pjsua_var.mconf_cfg.samples_per_frame * 1000 /
1449 pjsua_var.media_cfg.clock_rate;
1450
Benny Prijonod5696da2007-07-17 16:25:45 +00001451 pool = pjsua_pool_create("playlist", 1000, 1000);
1452 if (!pool) {
1453 PJSUA_UNLOCK();
1454 return PJ_ENOMEM;
1455 }
1456
1457 status = pjmedia_wav_playlist_create(pool, label,
Benny Prijonoa66c3312007-01-21 23:12:40 +00001458 file_names, file_count,
1459 ptime, options, 0, &port);
1460 if (status != PJ_SUCCESS) {
1461 PJSUA_UNLOCK();
1462 pjsua_perror(THIS_FILE, "Unable to create playlist", status);
Benny Prijonod5696da2007-07-17 16:25:45 +00001463 pj_pool_release(pool);
Benny Prijonoa66c3312007-01-21 23:12:40 +00001464 return status;
1465 }
1466
Benny Prijonod5696da2007-07-17 16:25:45 +00001467 status = pjmedia_conf_add_port(pjsua_var.mconf, pool,
Benny Prijonoa66c3312007-01-21 23:12:40 +00001468 port, &port->info.name, &slot);
1469 if (status != PJ_SUCCESS) {
1470 pjmedia_port_destroy(port);
1471 PJSUA_UNLOCK();
1472 pjsua_perror(THIS_FILE, "Unable to add port", status);
Benny Prijonod5696da2007-07-17 16:25:45 +00001473 pj_pool_release(pool);
Benny Prijonoa66c3312007-01-21 23:12:40 +00001474 return status;
1475 }
1476
1477 pjsua_var.player[file_id].type = 1;
Benny Prijonod5696da2007-07-17 16:25:45 +00001478 pjsua_var.player[file_id].pool = pool;
Benny Prijonoa66c3312007-01-21 23:12:40 +00001479 pjsua_var.player[file_id].port = port;
1480 pjsua_var.player[file_id].slot = slot;
1481
1482 if (p_id) *p_id = file_id;
1483
1484 ++pjsua_var.player_cnt;
1485
1486 PJSUA_UNLOCK();
1487 return PJ_SUCCESS;
1488
1489}
1490
1491
1492/*
Benny Prijonoeebe9af2006-06-13 22:57:13 +00001493 * Get conference port ID associated with player.
1494 */
1495PJ_DEF(pjsua_conf_port_id) pjsua_player_get_conf_port(pjsua_player_id id)
1496{
Benny Prijonoa1e69682007-05-11 15:14:34 +00001497 PJ_ASSERT_RETURN(id>=0&&id<(int)PJ_ARRAY_SIZE(pjsua_var.player), PJ_EINVAL);
Benny Prijonoeebe9af2006-06-13 22:57:13 +00001498 PJ_ASSERT_RETURN(pjsua_var.player[id].port != NULL, PJ_EINVAL);
1499
1500 return pjsua_var.player[id].slot;
1501}
1502
Benny Prijono469b1522006-12-26 03:05:17 +00001503/*
1504 * Get the media port for the player.
1505 */
Benny Prijonobe41d862008-01-18 13:24:28 +00001506PJ_DEF(pj_status_t) pjsua_player_get_port( pjsua_player_id id,
Benny Prijono469b1522006-12-26 03:05:17 +00001507 pjmedia_port **p_port)
1508{
Benny Prijonoa1e69682007-05-11 15:14:34 +00001509 PJ_ASSERT_RETURN(id>=0&&id<(int)PJ_ARRAY_SIZE(pjsua_var.player), PJ_EINVAL);
Benny Prijono469b1522006-12-26 03:05:17 +00001510 PJ_ASSERT_RETURN(pjsua_var.player[id].port != NULL, PJ_EINVAL);
1511 PJ_ASSERT_RETURN(p_port != NULL, PJ_EINVAL);
1512
1513 *p_port = pjsua_var.player[id].port;
1514
1515 return PJ_SUCCESS;
1516}
Benny Prijonoeebe9af2006-06-13 22:57:13 +00001517
1518/*
1519 * Set playback position.
1520 */
1521PJ_DEF(pj_status_t) pjsua_player_set_pos( pjsua_player_id id,
1522 pj_uint32_t samples)
1523{
Benny Prijonoa1e69682007-05-11 15:14:34 +00001524 PJ_ASSERT_RETURN(id>=0&&id<(int)PJ_ARRAY_SIZE(pjsua_var.player), PJ_EINVAL);
Benny Prijonoeebe9af2006-06-13 22:57:13 +00001525 PJ_ASSERT_RETURN(pjsua_var.player[id].port != NULL, PJ_EINVAL);
Benny Prijonoa66c3312007-01-21 23:12:40 +00001526 PJ_ASSERT_RETURN(pjsua_var.player[id].type == 0, PJ_EINVAL);
Benny Prijonoeebe9af2006-06-13 22:57:13 +00001527
1528 return pjmedia_wav_player_port_set_pos(pjsua_var.player[id].port, samples);
1529}
1530
1531
1532/*
1533 * Close the file, remove the player from the bridge, and free
1534 * resources associated with the file player.
1535 */
1536PJ_DEF(pj_status_t) pjsua_player_destroy(pjsua_player_id id)
1537{
Benny Prijonoa1e69682007-05-11 15:14:34 +00001538 PJ_ASSERT_RETURN(id>=0&&id<(int)PJ_ARRAY_SIZE(pjsua_var.player), PJ_EINVAL);
Benny Prijonoeebe9af2006-06-13 22:57:13 +00001539 PJ_ASSERT_RETURN(pjsua_var.player[id].port != NULL, PJ_EINVAL);
1540
1541 PJSUA_LOCK();
1542
1543 if (pjsua_var.player[id].port) {
1544 pjmedia_conf_remove_port(pjsua_var.mconf,
1545 pjsua_var.player[id].slot);
1546 pjmedia_port_destroy(pjsua_var.player[id].port);
1547 pjsua_var.player[id].port = NULL;
1548 pjsua_var.player[id].slot = 0xFFFF;
Benny Prijonod5696da2007-07-17 16:25:45 +00001549 pj_pool_release(pjsua_var.player[id].pool);
1550 pjsua_var.player[id].pool = NULL;
Benny Prijonoeebe9af2006-06-13 22:57:13 +00001551 pjsua_var.player_cnt--;
1552 }
1553
1554 PJSUA_UNLOCK();
1555
1556 return PJ_SUCCESS;
1557}
1558
1559
1560/*****************************************************************************
1561 * File recorder.
1562 */
1563
1564/*
1565 * Create a file recorder, and automatically connect this recorder to
1566 * the conference bridge.
1567 */
1568PJ_DEF(pj_status_t) pjsua_recorder_create( const pj_str_t *filename,
Benny Prijono8f310522006-10-20 11:08:49 +00001569 unsigned enc_type,
1570 void *enc_param,
Benny Prijonoeebe9af2006-06-13 22:57:13 +00001571 pj_ssize_t max_size,
1572 unsigned options,
Benny Prijonoeebe9af2006-06-13 22:57:13 +00001573 pjsua_recorder_id *p_id)
1574{
Benny Prijonob3cdb2b2006-10-19 15:49:47 +00001575 enum Format
1576 {
1577 FMT_UNKNOWN,
1578 FMT_WAV,
1579 FMT_MP3,
1580 };
Benny Prijonoeebe9af2006-06-13 22:57:13 +00001581 unsigned slot, file_id;
Benny Prijonoa66c3312007-01-21 23:12:40 +00001582 char path[PJ_MAXPATH];
Benny Prijonob3cdb2b2006-10-19 15:49:47 +00001583 pj_str_t ext;
Benny Prijono8f310522006-10-20 11:08:49 +00001584 int file_format;
Benny Prijonod5696da2007-07-17 16:25:45 +00001585 pj_pool_t *pool;
Benny Prijonoeebe9af2006-06-13 22:57:13 +00001586 pjmedia_port *port;
1587 pj_status_t status;
1588
Benny Prijonob3cdb2b2006-10-19 15:49:47 +00001589 /* Filename must present */
1590 PJ_ASSERT_RETURN(filename != NULL, PJ_EINVAL);
1591
Benny Prijono00cae612006-07-31 15:19:36 +00001592 /* Don't support max_size at present */
Benny Prijonob3cdb2b2006-10-19 15:49:47 +00001593 PJ_ASSERT_RETURN(max_size == 0 || max_size == -1, PJ_EINVAL);
Benny Prijono00cae612006-07-31 15:19:36 +00001594
Benny Prijono8f310522006-10-20 11:08:49 +00001595 /* Don't support encoding type at present */
1596 PJ_ASSERT_RETURN(enc_type == 0, PJ_EINVAL);
Benny Prijono00cae612006-07-31 15:19:36 +00001597
Benny Prijonoeebe9af2006-06-13 22:57:13 +00001598 if (pjsua_var.rec_cnt >= PJ_ARRAY_SIZE(pjsua_var.recorder))
1599 return PJ_ETOOMANY;
1600
Benny Prijonob3cdb2b2006-10-19 15:49:47 +00001601 /* Determine the file format */
1602 ext.ptr = filename->ptr + filename->slen - 4;
1603 ext.slen = 4;
1604
1605 if (pj_stricmp2(&ext, ".wav") == 0)
1606 file_format = FMT_WAV;
1607 else if (pj_stricmp2(&ext, ".mp3") == 0)
1608 file_format = FMT_MP3;
1609 else {
1610 PJ_LOG(1,(THIS_FILE, "pjsua_recorder_create() error: unable to "
1611 "determine file format for %.*s",
1612 (int)filename->slen, filename->ptr));
1613 return PJ_ENOTSUP;
1614 }
1615
Benny Prijonoeebe9af2006-06-13 22:57:13 +00001616 PJSUA_LOCK();
1617
1618 for (file_id=0; file_id<PJ_ARRAY_SIZE(pjsua_var.recorder); ++file_id) {
1619 if (pjsua_var.recorder[file_id].port == NULL)
1620 break;
1621 }
1622
1623 if (file_id == PJ_ARRAY_SIZE(pjsua_var.recorder)) {
1624 /* This is unexpected */
1625 PJSUA_UNLOCK();
1626 pj_assert(0);
1627 return PJ_EBUG;
1628 }
1629
1630 pj_memcpy(path, filename->ptr, filename->slen);
1631 path[filename->slen] = '\0';
Benny Prijonob3cdb2b2006-10-19 15:49:47 +00001632
Benny Prijonod5696da2007-07-17 16:25:45 +00001633 pool = pjsua_pool_create(get_basename(path, filename->slen), 1000, 1000);
1634 if (!pool) {
1635 PJSUA_UNLOCK();
1636 return PJ_ENOMEM;
1637 }
1638
Benny Prijonob3cdb2b2006-10-19 15:49:47 +00001639 if (file_format == FMT_WAV) {
Benny Prijonod5696da2007-07-17 16:25:45 +00001640 status = pjmedia_wav_writer_port_create(pool, path,
Benny Prijonob3cdb2b2006-10-19 15:49:47 +00001641 pjsua_var.media_cfg.clock_rate,
1642 pjsua_var.mconf_cfg.channel_count,
1643 pjsua_var.mconf_cfg.samples_per_frame,
1644 pjsua_var.mconf_cfg.bits_per_sample,
1645 options, 0, &port);
Benny Prijonob3cdb2b2006-10-19 15:49:47 +00001646 } else {
Benny Prijonoc95a0f02007-04-09 07:06:08 +00001647 PJ_UNUSED_ARG(enc_param);
Benny Prijonob3cdb2b2006-10-19 15:49:47 +00001648 port = NULL;
1649 status = PJ_ENOTSUP;
1650 }
1651
Benny Prijonoeebe9af2006-06-13 22:57:13 +00001652 if (status != PJ_SUCCESS) {
1653 PJSUA_UNLOCK();
1654 pjsua_perror(THIS_FILE, "Unable to open file for recording", status);
Benny Prijonod5696da2007-07-17 16:25:45 +00001655 pj_pool_release(pool);
Benny Prijonoeebe9af2006-06-13 22:57:13 +00001656 return status;
1657 }
1658
Benny Prijonod5696da2007-07-17 16:25:45 +00001659 status = pjmedia_conf_add_port(pjsua_var.mconf, pool,
Benny Prijonoeebe9af2006-06-13 22:57:13 +00001660 port, filename, &slot);
1661 if (status != PJ_SUCCESS) {
1662 pjmedia_port_destroy(port);
1663 PJSUA_UNLOCK();
Benny Prijonod5696da2007-07-17 16:25:45 +00001664 pj_pool_release(pool);
Benny Prijonoeebe9af2006-06-13 22:57:13 +00001665 return status;
1666 }
1667
1668 pjsua_var.recorder[file_id].port = port;
1669 pjsua_var.recorder[file_id].slot = slot;
Benny Prijonod5696da2007-07-17 16:25:45 +00001670 pjsua_var.recorder[file_id].pool = pool;
Benny Prijonoeebe9af2006-06-13 22:57:13 +00001671
1672 if (p_id) *p_id = file_id;
1673
1674 ++pjsua_var.rec_cnt;
1675
1676 PJSUA_UNLOCK();
1677 return PJ_SUCCESS;
1678}
1679
1680
1681/*
1682 * Get conference port associated with recorder.
1683 */
1684PJ_DEF(pjsua_conf_port_id) pjsua_recorder_get_conf_port(pjsua_recorder_id id)
1685{
Benny Prijonoa1e69682007-05-11 15:14:34 +00001686 PJ_ASSERT_RETURN(id>=0 && id<(int)PJ_ARRAY_SIZE(pjsua_var.recorder),
1687 PJ_EINVAL);
Benny Prijonoeebe9af2006-06-13 22:57:13 +00001688 PJ_ASSERT_RETURN(pjsua_var.recorder[id].port != NULL, PJ_EINVAL);
1689
1690 return pjsua_var.recorder[id].slot;
1691}
1692
Benny Prijono469b1522006-12-26 03:05:17 +00001693/*
1694 * Get the media port for the recorder.
1695 */
1696PJ_DEF(pj_status_t) pjsua_recorder_get_port( pjsua_recorder_id id,
1697 pjmedia_port **p_port)
1698{
Benny Prijonoa1e69682007-05-11 15:14:34 +00001699 PJ_ASSERT_RETURN(id>=0 && id<(int)PJ_ARRAY_SIZE(pjsua_var.recorder),
1700 PJ_EINVAL);
Benny Prijono469b1522006-12-26 03:05:17 +00001701 PJ_ASSERT_RETURN(pjsua_var.recorder[id].port != NULL, PJ_EINVAL);
1702 PJ_ASSERT_RETURN(p_port != NULL, PJ_EINVAL);
1703
1704 *p_port = pjsua_var.recorder[id].port;
1705 return PJ_SUCCESS;
1706}
Benny Prijonoeebe9af2006-06-13 22:57:13 +00001707
1708/*
1709 * Destroy recorder (this will complete recording).
1710 */
1711PJ_DEF(pj_status_t) pjsua_recorder_destroy(pjsua_recorder_id id)
1712{
Benny Prijonoa1e69682007-05-11 15:14:34 +00001713 PJ_ASSERT_RETURN(id>=0 && id<(int)PJ_ARRAY_SIZE(pjsua_var.recorder),
1714 PJ_EINVAL);
Benny Prijonoeebe9af2006-06-13 22:57:13 +00001715 PJ_ASSERT_RETURN(pjsua_var.recorder[id].port != NULL, PJ_EINVAL);
1716
1717 PJSUA_LOCK();
1718
1719 if (pjsua_var.recorder[id].port) {
1720 pjmedia_conf_remove_port(pjsua_var.mconf,
1721 pjsua_var.recorder[id].slot);
1722 pjmedia_port_destroy(pjsua_var.recorder[id].port);
1723 pjsua_var.recorder[id].port = NULL;
1724 pjsua_var.recorder[id].slot = 0xFFFF;
Benny Prijonod5696da2007-07-17 16:25:45 +00001725 pj_pool_release(pjsua_var.recorder[id].pool);
1726 pjsua_var.recorder[id].pool = NULL;
Benny Prijonoeebe9af2006-06-13 22:57:13 +00001727 pjsua_var.rec_cnt--;
1728 }
1729
1730 PJSUA_UNLOCK();
1731
1732 return PJ_SUCCESS;
1733}
1734
1735
1736/*****************************************************************************
1737 * Sound devices.
1738 */
1739
1740/*
1741 * Enum sound devices.
1742 */
1743PJ_DEF(pj_status_t) pjsua_enum_snd_devs( pjmedia_snd_dev_info info[],
1744 unsigned *count)
1745{
1746 unsigned i, dev_count;
1747
1748 dev_count = pjmedia_snd_get_dev_count();
1749
1750 if (dev_count > *count) dev_count = *count;
1751
1752 for (i=0; i<dev_count; ++i) {
1753 const pjmedia_snd_dev_info *ci;
1754
1755 ci = pjmedia_snd_get_dev_info(i);
1756 pj_memcpy(&info[i], ci, sizeof(*ci));
1757 }
1758
1759 *count = dev_count;
1760
1761 return PJ_SUCCESS;
1762}
1763
1764
1765/* Close existing sound device */
1766static void close_snd_dev(void)
1767{
1768 /* Close sound device */
1769 if (pjsua_var.snd_port) {
1770 const pjmedia_snd_dev_info *cap_info, *play_info;
1771
1772 cap_info = pjmedia_snd_get_dev_info(pjsua_var.cap_dev);
1773 play_info = pjmedia_snd_get_dev_info(pjsua_var.play_dev);
1774
1775 PJ_LOG(4,(THIS_FILE, "Closing %s sound playback device and "
1776 "%s sound capture device",
1777 play_info->name, cap_info->name));
1778
1779 pjmedia_snd_port_disconnect(pjsua_var.snd_port);
1780 pjmedia_snd_port_destroy(pjsua_var.snd_port);
1781 pjsua_var.snd_port = NULL;
1782 }
1783
1784 /* Close null sound device */
1785 if (pjsua_var.null_snd) {
1786 PJ_LOG(4,(THIS_FILE, "Closing null sound device.."));
1787 pjmedia_master_port_destroy(pjsua_var.null_snd, PJ_FALSE);
1788 pjsua_var.null_snd = NULL;
1789 }
1790}
1791
1792/*
1793 * Select or change sound device. Application may call this function at
1794 * any time to replace current sound device.
1795 */
1796PJ_DEF(pj_status_t) pjsua_set_snd_dev( int capture_dev,
1797 int playback_dev)
1798{
1799 pjmedia_port *conf_port;
Benny Prijono6dd967c2006-12-26 02:27:14 +00001800 const pjmedia_snd_dev_info *play_info;
Benny Prijonof3758ee2008-02-26 15:32:16 +00001801 unsigned clock_rates[] = {0, 22050, 44100, 48000, 32000, 16000,
1802 8000};
Benny Prijono658a1c52006-10-11 21:56:16 +00001803 unsigned selected_clock_rate = 0;
Benny Prijono26056d82006-10-11 16:03:41 +00001804 unsigned i;
Benny Prijonoc53c6d72006-11-27 09:54:03 +00001805 pjmedia_snd_stream *strm;
1806 pjmedia_snd_stream_info si;
1807 pj_str_t tmp;
Benny Prijono26056d82006-10-11 16:03:41 +00001808 pj_status_t status = -1;
Benny Prijonoeebe9af2006-06-13 22:57:13 +00001809
1810 /* Close existing sound port */
1811 close_snd_dev();
1812
1813
Benny Prijono26056d82006-10-11 16:03:41 +00001814 /* Set default clock rate */
Benny Prijonof3758ee2008-02-26 15:32:16 +00001815 clock_rates[0] = pjsua_var.media_cfg.snd_clock_rate;
1816 if (clock_rates[0] == 0)
1817 clock_rates[0] = pjsua_var.media_cfg.clock_rate;
Benny Prijonoeebe9af2006-06-13 22:57:13 +00001818
Benny Prijono50f19b32008-03-11 13:15:43 +00001819 /* Get the port0 of the conference bridge. */
1820 conf_port = pjmedia_conf_get_master_port(pjsua_var.mconf);
1821 pj_assert(conf_port != NULL);
1822
Benny Prijono26056d82006-10-11 16:03:41 +00001823 /* Attempts to open the sound device with different clock rates */
1824 for (i=0; i<PJ_ARRAY_SIZE(clock_rates); ++i) {
1825 char errmsg[PJ_ERR_MSG_SIZE];
Benny Prijonocf0b4b22007-10-06 17:31:09 +00001826 unsigned fps;
Benny Prijono26056d82006-10-11 16:03:41 +00001827
1828 PJ_LOG(4,(THIS_FILE,
1829 "pjsua_set_snd_dev(): attempting to open devices "
1830 "@%d Hz", clock_rates[i]));
1831
1832 /* Create the sound device. Sound port will start immediately. */
Benny Prijonocf0b4b22007-10-06 17:31:09 +00001833 fps = 1000 / pjsua_var.media_cfg.audio_frame_ptime;
Benny Prijono26056d82006-10-11 16:03:41 +00001834 status = pjmedia_snd_port_create(pjsua_var.pool, capture_dev,
1835 playback_dev,
Benny Prijono7d60d052008-03-29 12:24:20 +00001836 clock_rates[i],
1837 pjsua_var.media_cfg.channel_count,
1838 clock_rates[i]/fps *
1839 pjsua_var.media_cfg.channel_count,
Benny Prijono26056d82006-10-11 16:03:41 +00001840 16, 0, &pjsua_var.snd_port);
1841
Benny Prijono658a1c52006-10-11 21:56:16 +00001842 if (status == PJ_SUCCESS) {
1843 selected_clock_rate = clock_rates[i];
Benny Prijono50f19b32008-03-11 13:15:43 +00001844
1845 /* If there's mismatch between sound port and conference's port,
1846 * create a resample port to bridge them.
1847 */
1848 if (selected_clock_rate != pjsua_var.media_cfg.clock_rate) {
1849 pjmedia_port *resample_port;
1850 unsigned resample_opt = 0;
1851
1852 if (pjsua_var.media_cfg.quality >= 3 &&
1853 pjsua_var.media_cfg.quality <= 4)
1854 {
1855 resample_opt |= PJMEDIA_CONF_SMALL_FILTER;
1856 }
1857 else if (pjsua_var.media_cfg.quality < 3) {
1858 resample_opt |= PJMEDIA_CONF_USE_LINEAR;
1859 }
1860
1861 status = pjmedia_resample_port_create(pjsua_var.pool,
1862 conf_port,
1863 selected_clock_rate,
1864 resample_opt,
1865 &resample_port);
1866 if (status != PJ_SUCCESS) {
1867 pj_strerror(status, errmsg, sizeof(errmsg));
1868 PJ_LOG(4, (THIS_FILE,
1869 "Error creating resample port, trying next "
1870 "clock rate",
1871 errmsg));
1872 pjmedia_snd_port_destroy(pjsua_var.snd_port);
1873 pjsua_var.snd_port = NULL;
1874 continue;
1875 } else {
1876 conf_port = resample_port;
1877 break;
1878 }
1879
1880 } else {
1881 break;
1882 }
Benny Prijono658a1c52006-10-11 21:56:16 +00001883 }
Benny Prijono26056d82006-10-11 16:03:41 +00001884
1885 pj_strerror(status, errmsg, sizeof(errmsg));
Benny Prijono658a1c52006-10-11 21:56:16 +00001886 PJ_LOG(4, (THIS_FILE, "..failed: %s", errmsg));
Benny Prijono26056d82006-10-11 16:03:41 +00001887 }
1888
Benny Prijonoeebe9af2006-06-13 22:57:13 +00001889 if (status != PJ_SUCCESS) {
1890 pjsua_perror(THIS_FILE, "Unable to open sound device", status);
1891 return status;
1892 }
1893
Benny Prijonof20687a2006-08-04 18:27:19 +00001894 /* Set AEC */
Benny Prijono5da50432006-08-07 10:24:52 +00001895 pjmedia_snd_port_set_ec( pjsua_var.snd_port, pjsua_var.pool,
1896 pjsua_var.media_cfg.ec_tail_len,
1897 pjsua_var.media_cfg.ec_options);
Benny Prijonoeebe9af2006-06-13 22:57:13 +00001898
Benny Prijono52a93912006-08-04 20:54:37 +00001899 /* Connect sound port to the bridge */
1900 status = pjmedia_snd_port_connect(pjsua_var.snd_port,
1901 conf_port );
1902 if (status != PJ_SUCCESS) {
1903 pjsua_perror(THIS_FILE, "Unable to connect conference port to "
1904 "sound device", status);
1905 pjmedia_snd_port_destroy(pjsua_var.snd_port);
1906 pjsua_var.snd_port = NULL;
1907 return status;
1908 }
1909
Benny Prijonoeebe9af2006-06-13 22:57:13 +00001910 /* Save the device IDs */
1911 pjsua_var.cap_dev = capture_dev;
1912 pjsua_var.play_dev = playback_dev;
1913
Benny Prijonoc53c6d72006-11-27 09:54:03 +00001914 /* Update sound device name. */
1915 strm = pjmedia_snd_port_get_snd_stream(pjsua_var.snd_port);
1916 pjmedia_snd_stream_get_info(strm, &si);
1917 play_info = pjmedia_snd_get_dev_info(si.rec_id);
1918
Benny Prijonof3758ee2008-02-26 15:32:16 +00001919 if (si.clock_rate != pjsua_var.media_cfg.clock_rate) {
1920 char tmp_buf[128];
1921 int tmp_buf_len = sizeof(tmp_buf);
1922
1923 tmp_buf_len = pj_ansi_snprintf(tmp_buf, sizeof(tmp_buf)-1, "%s (%dKHz)",
1924 play_info->name, si.clock_rate/1000);
1925 pj_strset(&tmp, tmp_buf, tmp_buf_len);
1926 pjmedia_conf_set_port0_name(pjsua_var.mconf, &tmp);
1927 } else {
1928 pjmedia_conf_set_port0_name(pjsua_var.mconf,
1929 pj_cstr(&tmp, play_info->name));
1930 }
Benny Prijonoc53c6d72006-11-27 09:54:03 +00001931
Benny Prijonoeebe9af2006-06-13 22:57:13 +00001932 return PJ_SUCCESS;
1933}
1934
1935
1936/*
Benny Prijonoebdf8772007-02-01 19:25:50 +00001937 * Get currently active sound devices. If sound devices has not been created
1938 * (for example when pjsua_start() is not called), it is possible that
1939 * the function returns PJ_SUCCESS with -1 as device IDs.
1940 */
1941PJ_DEF(pj_status_t) pjsua_get_snd_dev(int *capture_dev,
1942 int *playback_dev)
1943{
1944 if (capture_dev) {
1945 *capture_dev = pjsua_var.cap_dev;
1946 }
1947 if (playback_dev) {
1948 *playback_dev = pjsua_var.play_dev;
1949 }
1950
1951 return PJ_SUCCESS;
1952}
1953
1954
1955/*
Benny Prijonoeebe9af2006-06-13 22:57:13 +00001956 * Use null sound device.
1957 */
1958PJ_DEF(pj_status_t) pjsua_set_null_snd_dev(void)
1959{
1960 pjmedia_port *conf_port;
1961 pj_status_t status;
1962
1963 /* Close existing sound device */
1964 close_snd_dev();
1965
1966 PJ_LOG(4,(THIS_FILE, "Opening null sound device.."));
1967
1968 /* Get the port0 of the conference bridge. */
1969 conf_port = pjmedia_conf_get_master_port(pjsua_var.mconf);
1970 pj_assert(conf_port != NULL);
1971
1972 /* Create master port, connecting port0 of the conference bridge to
1973 * a null port.
1974 */
1975 status = pjmedia_master_port_create(pjsua_var.pool, pjsua_var.null_port,
1976 conf_port, 0, &pjsua_var.null_snd);
1977 if (status != PJ_SUCCESS) {
1978 pjsua_perror(THIS_FILE, "Unable to create null sound device",
1979 status);
1980 return status;
1981 }
1982
1983 /* Start the master port */
1984 status = pjmedia_master_port_start(pjsua_var.null_snd);
1985 PJ_ASSERT_RETURN(status == PJ_SUCCESS, status);
1986
1987 return PJ_SUCCESS;
1988}
1989
1990
Benny Prijonoe909eac2006-07-27 22:04:56 +00001991
1992/*
1993 * Use no device!
1994 */
1995PJ_DEF(pjmedia_port*) pjsua_set_no_snd_dev(void)
1996{
1997 /* Close existing sound device */
1998 close_snd_dev();
1999
2000 pjsua_var.no_snd = PJ_TRUE;
2001 return pjmedia_conf_get_master_port(pjsua_var.mconf);
2002}
2003
2004
Benny Prijonof20687a2006-08-04 18:27:19 +00002005/*
2006 * Configure the AEC settings of the sound port.
2007 */
Benny Prijono5da50432006-08-07 10:24:52 +00002008PJ_DEF(pj_status_t) pjsua_set_ec(unsigned tail_ms, unsigned options)
Benny Prijonof20687a2006-08-04 18:27:19 +00002009{
2010 pjsua_var.media_cfg.ec_tail_len = tail_ms;
2011
2012 if (pjsua_var.snd_port)
Benny Prijono5da50432006-08-07 10:24:52 +00002013 return pjmedia_snd_port_set_ec( pjsua_var.snd_port, pjsua_var.pool,
2014 tail_ms, options);
Benny Prijonof20687a2006-08-04 18:27:19 +00002015
2016 return PJ_SUCCESS;
2017}
2018
2019
2020/*
2021 * Get current AEC tail length.
2022 */
Benny Prijono22dfe592006-08-06 12:07:13 +00002023PJ_DEF(pj_status_t) pjsua_get_ec_tail(unsigned *p_tail_ms)
Benny Prijonof20687a2006-08-04 18:27:19 +00002024{
2025 *p_tail_ms = pjsua_var.media_cfg.ec_tail_len;
2026 return PJ_SUCCESS;
2027}
2028
Benny Prijonoe909eac2006-07-27 22:04:56 +00002029
Benny Prijonoeebe9af2006-06-13 22:57:13 +00002030/*****************************************************************************
2031 * Codecs.
2032 */
2033
2034/*
2035 * Enum all supported codecs in the system.
2036 */
2037PJ_DEF(pj_status_t) pjsua_enum_codecs( pjsua_codec_info id[],
2038 unsigned *p_count )
2039{
2040 pjmedia_codec_mgr *codec_mgr;
2041 pjmedia_codec_info info[32];
2042 unsigned i, count, prio[32];
2043 pj_status_t status;
2044
2045 codec_mgr = pjmedia_endpt_get_codec_mgr(pjsua_var.med_endpt);
2046 count = PJ_ARRAY_SIZE(info);
2047 status = pjmedia_codec_mgr_enum_codecs( codec_mgr, &count, info, prio);
2048 if (status != PJ_SUCCESS) {
2049 *p_count = 0;
2050 return status;
2051 }
2052
2053 if (count > *p_count) count = *p_count;
2054
2055 for (i=0; i<count; ++i) {
2056 pjmedia_codec_info_to_id(&info[i], id[i].buf_, sizeof(id[i].buf_));
2057 id[i].codec_id = pj_str(id[i].buf_);
2058 id[i].priority = (pj_uint8_t) prio[i];
2059 }
2060
2061 *p_count = count;
2062
2063 return PJ_SUCCESS;
2064}
2065
2066
2067/*
2068 * Change codec priority.
2069 */
2070PJ_DEF(pj_status_t) pjsua_codec_set_priority( const pj_str_t *codec_id,
2071 pj_uint8_t priority )
2072{
2073 pjmedia_codec_mgr *codec_mgr;
2074
2075 codec_mgr = pjmedia_endpt_get_codec_mgr(pjsua_var.med_endpt);
2076
2077 return pjmedia_codec_mgr_set_codec_priority(codec_mgr, codec_id,
2078 priority);
2079}
2080
2081
2082/*
2083 * Get codec parameters.
2084 */
2085PJ_DEF(pj_status_t) pjsua_codec_get_param( const pj_str_t *codec_id,
2086 pjmedia_codec_param *param )
2087{
2088 const pjmedia_codec_info *info;
2089 pjmedia_codec_mgr *codec_mgr;
2090 unsigned count = 1;
2091 pj_status_t status;
2092
2093 codec_mgr = pjmedia_endpt_get_codec_mgr(pjsua_var.med_endpt);
2094
2095 status = pjmedia_codec_mgr_find_codecs_by_id(codec_mgr, codec_id,
2096 &count, &info, NULL);
2097 if (status != PJ_SUCCESS)
2098 return status;
2099
2100 if (count != 1)
2101 return PJ_ENOTFOUND;
2102
2103 status = pjmedia_codec_mgr_get_default_param( codec_mgr, info, param);
2104 return status;
2105}
2106
2107
2108/*
2109 * Set codec parameters.
2110 */
2111PJ_DEF(pj_status_t) pjsua_codec_set_param( const pj_str_t *id,
2112 const pjmedia_codec_param *param)
2113{
Benny Prijono00cae612006-07-31 15:19:36 +00002114 PJ_UNUSED_ARG(id);
2115 PJ_UNUSED_ARG(param);
Benny Prijonoeebe9af2006-06-13 22:57:13 +00002116 PJ_TODO(set_codec_param);
2117 return PJ_SUCCESS;
2118}