blob: dea8b5b783c4cec1caf5be26876c85f770a24e10 [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
142#if PJMEDIA_HAS_L16_CODEC
143 /* Register L16 family codecs, but disable all */
144 status = pjmedia_codec_l16_init(pjsua_var.med_endpt, 0);
145 if (status != PJ_SUCCESS) {
146 pjsua_perror(THIS_FILE, "Error initializing L16 codecs",
147 status);
148 return status;
149 }
150
151 /* Disable ALL L16 codecs */
152 codec_id = pj_str("L16");
153 pjmedia_codec_mgr_set_codec_priority(
154 pjmedia_endpt_get_codec_mgr(pjsua_var.med_endpt),
155 &codec_id, PJMEDIA_CODEC_PRIO_DISABLED);
156
157#endif /* PJMEDIA_HAS_L16_CODEC */
158
159
160 /* Save additional conference bridge parameters for future
161 * reference.
162 */
163 pjsua_var.mconf_cfg.samples_per_frame = pjsua_var.media_cfg.clock_rate *
Benny Prijonocf0b4b22007-10-06 17:31:09 +0000164 pjsua_var.media_cfg.audio_frame_ptime /
165 1000;
Benny Prijonoeebe9af2006-06-13 22:57:13 +0000166 pjsua_var.mconf_cfg.channel_count = 1;
167 pjsua_var.mconf_cfg.bits_per_sample = 16;
168
Benny Prijono0498d902006-06-19 14:49:14 +0000169 /* Init options for conference bridge. */
170 opt = PJMEDIA_CONF_NO_DEVICE;
171 if (pjsua_var.media_cfg.quality >= 3 &&
Benny Prijono7ca96da2006-08-07 12:11:40 +0000172 pjsua_var.media_cfg.quality <= 4)
Benny Prijono0498d902006-06-19 14:49:14 +0000173 {
174 opt |= PJMEDIA_CONF_SMALL_FILTER;
175 }
176 else if (pjsua_var.media_cfg.quality < 3) {
177 opt |= PJMEDIA_CONF_USE_LINEAR;
178 }
179
180
Benny Prijonoeebe9af2006-06-13 22:57:13 +0000181 /* Init conference bridge. */
182 status = pjmedia_conf_create(pjsua_var.pool,
183 pjsua_var.media_cfg.max_media_ports,
184 pjsua_var.media_cfg.clock_rate,
185 pjsua_var.mconf_cfg.channel_count,
186 pjsua_var.mconf_cfg.samples_per_frame,
187 pjsua_var.mconf_cfg.bits_per_sample,
Benny Prijono0498d902006-06-19 14:49:14 +0000188 opt, &pjsua_var.mconf);
Benny Prijonoeebe9af2006-06-13 22:57:13 +0000189 if (status != PJ_SUCCESS) {
Benny Prijono50f19b32008-03-11 13:15:43 +0000190 pjsua_perror(THIS_FILE, "Error creating conference bridge",
Benny Prijonoeebe9af2006-06-13 22:57:13 +0000191 status);
192 return status;
193 }
194
195 /* Create null port just in case user wants to use null sound. */
196 status = pjmedia_null_port_create(pjsua_var.pool,
197 pjsua_var.media_cfg.clock_rate,
198 pjsua_var.mconf_cfg.channel_count,
199 pjsua_var.mconf_cfg.samples_per_frame,
200 pjsua_var.mconf_cfg.bits_per_sample,
201 &pjsua_var.null_port);
202 PJ_ASSERT_RETURN(status == PJ_SUCCESS, status);
203
Benny Prijono6ba8c542007-10-16 01:34:14 +0000204 /* Perform NAT detection */
205 pjsua_detect_nat_type();
206
Benny Prijonoeebe9af2006-06-13 22:57:13 +0000207 return PJ_SUCCESS;
208}
209
210
211/*
212 * Create RTP and RTCP socket pair, and possibly resolve their public
213 * address via STUN.
214 */
215static pj_status_t create_rtp_rtcp_sock(const pjsua_transport_config *cfg,
216 pjmedia_sock_info *skinfo)
217{
218 enum {
219 RTP_RETRY = 100
220 };
221 int i;
Benny Prijono0a5cad82006-09-26 13:21:02 +0000222 pj_sockaddr_in bound_addr;
Benny Prijonoeebe9af2006-06-13 22:57:13 +0000223 pj_sockaddr_in mapped_addr[2];
224 pj_status_t status = PJ_SUCCESS;
Benny Prijono38fb3ea2008-01-02 08:27:03 +0000225 char addr_buf[PJ_INET6_ADDRSTRLEN+2];
Benny Prijonoeebe9af2006-06-13 22:57:13 +0000226 pj_sock_t sock[2];
227
Benny Prijonoc97608e2007-03-23 16:34:20 +0000228 /* Make sure STUN server resolution has completed */
229 status = pjsua_resolve_stun_server(PJ_TRUE);
230 if (status != PJ_SUCCESS) {
231 pjsua_perror(THIS_FILE, "Error resolving STUN server", status);
232 return status;
233 }
234
Benny Prijonode479562007-03-15 10:23:55 +0000235 if (next_rtp_port == 0)
236 next_rtp_port = (pj_uint16_t)cfg->port;
Benny Prijonoeebe9af2006-06-13 22:57:13 +0000237
238 for (i=0; i<2; ++i)
239 sock[i] = PJ_INVALID_SOCKET;
240
Benny Prijono0a5cad82006-09-26 13:21:02 +0000241 bound_addr.sin_addr.s_addr = PJ_INADDR_ANY;
242 if (cfg->bound_addr.slen) {
243 status = pj_sockaddr_in_set_str_addr(&bound_addr, &cfg->bound_addr);
244 if (status != PJ_SUCCESS) {
245 pjsua_perror(THIS_FILE, "Unable to resolve transport bind address",
246 status);
247 return status;
248 }
249 }
Benny Prijonoeebe9af2006-06-13 22:57:13 +0000250
251 /* Loop retry to bind RTP and RTCP sockets. */
Benny Prijonode479562007-03-15 10:23:55 +0000252 for (i=0; i<RTP_RETRY; ++i, next_rtp_port += 2) {
Benny Prijonoeebe9af2006-06-13 22:57:13 +0000253
254 /* Create and bind RTP socket. */
Benny Prijono8ab968f2007-07-20 08:08:30 +0000255 status = pj_sock_socket(pj_AF_INET(), pj_SOCK_DGRAM(), 0, &sock[0]);
Benny Prijonoeebe9af2006-06-13 22:57:13 +0000256 if (status != PJ_SUCCESS) {
257 pjsua_perror(THIS_FILE, "socket() error", status);
258 return status;
259 }
260
Benny Prijono0cdcd3f2007-12-09 14:14:11 +0000261 status=pj_sock_bind_in(sock[0], pj_ntohl(bound_addr.sin_addr.s_addr),
262 next_rtp_port);
Benny Prijonoeebe9af2006-06-13 22:57:13 +0000263 if (status != PJ_SUCCESS) {
264 pj_sock_close(sock[0]);
265 sock[0] = PJ_INVALID_SOCKET;
266 continue;
267 }
268
269 /* Create and bind RTCP socket. */
Benny Prijono8ab968f2007-07-20 08:08:30 +0000270 status = pj_sock_socket(pj_AF_INET(), pj_SOCK_DGRAM(), 0, &sock[1]);
Benny Prijonoeebe9af2006-06-13 22:57:13 +0000271 if (status != PJ_SUCCESS) {
272 pjsua_perror(THIS_FILE, "socket() error", status);
273 pj_sock_close(sock[0]);
274 return status;
275 }
276
Benny Prijono0cdcd3f2007-12-09 14:14:11 +0000277 status=pj_sock_bind_in(sock[1], pj_ntohl(bound_addr.sin_addr.s_addr),
278 (pj_uint16_t)(next_rtp_port+1));
Benny Prijonoeebe9af2006-06-13 22:57:13 +0000279 if (status != PJ_SUCCESS) {
280 pj_sock_close(sock[0]);
281 sock[0] = PJ_INVALID_SOCKET;
282
283 pj_sock_close(sock[1]);
284 sock[1] = PJ_INVALID_SOCKET;
285 continue;
286 }
287
288 /*
289 * If we're configured to use STUN, then find out the mapped address,
290 * and make sure that the mapped RTCP port is adjacent with the RTP.
291 */
Benny Prijonoc97608e2007-03-23 16:34:20 +0000292 if (pjsua_var.stun_srv.addr.sa_family != 0) {
293 char ip_addr[32];
294 pj_str_t stun_srv;
295
296 pj_ansi_strcpy(ip_addr,
297 pj_inet_ntoa(pjsua_var.stun_srv.ipv4.sin_addr));
298 stun_srv = pj_str(ip_addr);
299
Benny Prijono14c2b862007-02-21 00:40:05 +0000300 status=pjstun_get_mapped_addr(&pjsua_var.cp.factory, 2, sock,
Benny Prijonoaf09dc32007-04-22 12:48:30 +0000301 &stun_srv, pj_ntohs(pjsua_var.stun_srv.ipv4.sin_port),
302 &stun_srv, pj_ntohs(pjsua_var.stun_srv.ipv4.sin_port),
Benny Prijonoeebe9af2006-06-13 22:57:13 +0000303 mapped_addr);
304 if (status != PJ_SUCCESS) {
305 pjsua_perror(THIS_FILE, "STUN resolve error", status);
306 goto on_error;
307 }
308
Benny Prijono80eee892007-11-03 22:43:23 +0000309#if PJSUA_REQUIRE_CONSECUTIVE_RTCP_PORT
Benny Prijonoeebe9af2006-06-13 22:57:13 +0000310 if (pj_ntohs(mapped_addr[1].sin_port) ==
311 pj_ntohs(mapped_addr[0].sin_port)+1)
312 {
313 /* Success! */
314 break;
315 }
316
317 pj_sock_close(sock[0]);
318 sock[0] = PJ_INVALID_SOCKET;
319
320 pj_sock_close(sock[1]);
321 sock[1] = PJ_INVALID_SOCKET;
Benny Prijono80eee892007-11-03 22:43:23 +0000322#else
323 if (pj_ntohs(mapped_addr[1].sin_port) !=
324 pj_ntohs(mapped_addr[0].sin_port)+1)
325 {
326 PJ_LOG(4,(THIS_FILE,
327 "Note: STUN mapped RTCP port %d is not adjacent"
328 " to RTP port %d",
329 pj_ntohs(mapped_addr[1].sin_port),
330 pj_ntohs(mapped_addr[0].sin_port)));
331 }
332 /* Success! */
333 break;
334#endif
Benny Prijonoeebe9af2006-06-13 22:57:13 +0000335
Benny Prijono0a5cad82006-09-26 13:21:02 +0000336 } else if (cfg->public_addr.slen) {
337
338 status = pj_sockaddr_in_init(&mapped_addr[0], &cfg->public_addr,
Benny Prijonode479562007-03-15 10:23:55 +0000339 (pj_uint16_t)next_rtp_port);
Benny Prijono0a5cad82006-09-26 13:21:02 +0000340 if (status != PJ_SUCCESS)
341 goto on_error;
342
343 status = pj_sockaddr_in_init(&mapped_addr[1], &cfg->public_addr,
Benny Prijonode479562007-03-15 10:23:55 +0000344 (pj_uint16_t)(next_rtp_port+1));
Benny Prijono0a5cad82006-09-26 13:21:02 +0000345 if (status != PJ_SUCCESS)
346 goto on_error;
347
348 break;
349
Benny Prijonoeebe9af2006-06-13 22:57:13 +0000350 } else {
Benny Prijonoeebe9af2006-06-13 22:57:13 +0000351
Benny Prijono42d08d22007-12-20 11:23:07 +0000352 if (bound_addr.sin_addr.s_addr == 0) {
353 pj_sockaddr addr;
354
355 /* Get local IP address. */
356 status = pj_gethostip(pj_AF_INET(), &addr);
357 if (status != PJ_SUCCESS)
358 goto on_error;
359
360 bound_addr.sin_addr.s_addr = addr.ipv4.sin_addr.s_addr;
361 }
Benny Prijonoeebe9af2006-06-13 22:57:13 +0000362
Benny Prijonofe0b1d62007-12-05 04:07:37 +0000363 for (i=0; i<2; ++i) {
364 pj_sockaddr_in_init(&mapped_addr[i], NULL, 0);
Benny Prijono42d08d22007-12-20 11:23:07 +0000365 mapped_addr[i].sin_addr.s_addr = bound_addr.sin_addr.s_addr;
Benny Prijonofe0b1d62007-12-05 04:07:37 +0000366 }
Benny Prijonoeebe9af2006-06-13 22:57:13 +0000367
Benny Prijonode479562007-03-15 10:23:55 +0000368 mapped_addr[0].sin_port=pj_htons((pj_uint16_t)next_rtp_port);
369 mapped_addr[1].sin_port=pj_htons((pj_uint16_t)(next_rtp_port+1));
Benny Prijonoeebe9af2006-06-13 22:57:13 +0000370 break;
371 }
372 }
373
374 if (sock[0] == PJ_INVALID_SOCKET) {
375 PJ_LOG(1,(THIS_FILE,
376 "Unable to find appropriate RTP/RTCP ports combination"));
377 goto on_error;
378 }
379
380
381 skinfo->rtp_sock = sock[0];
382 pj_memcpy(&skinfo->rtp_addr_name,
383 &mapped_addr[0], sizeof(pj_sockaddr_in));
384
385 skinfo->rtcp_sock = sock[1];
386 pj_memcpy(&skinfo->rtcp_addr_name,
387 &mapped_addr[1], sizeof(pj_sockaddr_in));
388
Benny Prijono8b22ce12008-02-08 12:57:55 +0000389 PJ_LOG(4,(THIS_FILE, "RTP socket reachable at %s",
Benny Prijono5186eae2007-12-03 14:38:25 +0000390 pj_sockaddr_print(&skinfo->rtp_addr_name, addr_buf,
391 sizeof(addr_buf), 3)));
Benny Prijono8b22ce12008-02-08 12:57:55 +0000392 PJ_LOG(4,(THIS_FILE, "RTCP socket reachable at %s",
Benny Prijono5186eae2007-12-03 14:38:25 +0000393 pj_sockaddr_print(&skinfo->rtcp_addr_name, addr_buf,
394 sizeof(addr_buf), 3)));
Benny Prijonoeebe9af2006-06-13 22:57:13 +0000395
Benny Prijonode479562007-03-15 10:23:55 +0000396 next_rtp_port += 2;
Benny Prijonoeebe9af2006-06-13 22:57:13 +0000397 return PJ_SUCCESS;
398
399on_error:
400 for (i=0; i<2; ++i) {
401 if (sock[i] != PJ_INVALID_SOCKET)
402 pj_sock_close(sock[i]);
403 }
404 return status;
405}
406
407
408/*
409 * Start pjsua media subsystem.
410 */
411pj_status_t pjsua_media_subsys_start(void)
412{
413 pj_status_t status;
414
415 /* Create media for calls, if none is specified */
416 if (pjsua_var.calls[0].med_tp == NULL) {
417 pjsua_transport_config transport_cfg;
418
419 /* Create default transport config */
420 pjsua_transport_config_default(&transport_cfg);
421 transport_cfg.port = DEFAULT_RTP_PORT;
422
423 status = pjsua_media_transports_create(&transport_cfg);
424 if (status != PJ_SUCCESS)
425 return status;
426 }
427
428 /* Create sound port if none is created yet */
Benny Prijonoe909eac2006-07-27 22:04:56 +0000429 if (pjsua_var.snd_port==NULL && pjsua_var.null_snd==NULL &&
430 !pjsua_var.no_snd)
431 {
Benny Prijonoeebe9af2006-06-13 22:57:13 +0000432 status = pjsua_set_snd_dev(pjsua_var.cap_dev, pjsua_var.play_dev);
433 if (status != PJ_SUCCESS) {
Benny Prijonod639efa2007-04-05 22:45:06 +0000434 pjsua_perror(THIS_FILE, "Error opening sound device", status);
435 return status;
Benny Prijonoeebe9af2006-06-13 22:57:13 +0000436 }
437 }
438
439 return PJ_SUCCESS;
440}
441
442
443/*
444 * Destroy pjsua media subsystem.
445 */
446pj_status_t pjsua_media_subsys_destroy(void)
447{
448 unsigned i;
449
450 close_snd_dev();
451
452 if (pjsua_var.mconf) {
453 pjmedia_conf_destroy(pjsua_var.mconf);
454 pjsua_var.mconf = NULL;
455 }
456
457 if (pjsua_var.null_port) {
458 pjmedia_port_destroy(pjsua_var.null_port);
459 pjsua_var.null_port = NULL;
460 }
461
462 /* Destroy file players */
463 for (i=0; i<PJ_ARRAY_SIZE(pjsua_var.player); ++i) {
464 if (pjsua_var.player[i].port) {
465 pjmedia_port_destroy(pjsua_var.player[i].port);
466 pjsua_var.player[i].port = NULL;
467 }
468 }
469
470 /* Destroy file recorders */
471 for (i=0; i<PJ_ARRAY_SIZE(pjsua_var.recorder); ++i) {
472 if (pjsua_var.recorder[i].port) {
473 pjmedia_port_destroy(pjsua_var.recorder[i].port);
474 pjsua_var.recorder[i].port = NULL;
475 }
476 }
477
478 /* Close media transports */
Benny Prijono0875ae82006-12-26 00:11:48 +0000479 for (i=0; i<pjsua_var.ua_cfg.max_calls; ++i) {
Benny Prijonoeebe9af2006-06-13 22:57:13 +0000480 if (pjsua_var.calls[i].med_tp) {
481 (*pjsua_var.calls[i].med_tp->op->destroy)(pjsua_var.calls[i].med_tp);
482 pjsua_var.calls[i].med_tp = NULL;
483 }
484 }
485
486 /* Destroy media endpoint. */
487 if (pjsua_var.med_endpt) {
488
489 /* Shutdown all codecs: */
490# if PJMEDIA_HAS_SPEEX_CODEC
491 pjmedia_codec_speex_deinit();
492# endif /* PJMEDIA_HAS_SPEEX_CODEC */
493
494# if PJMEDIA_HAS_GSM_CODEC
495 pjmedia_codec_gsm_deinit();
496# endif /* PJMEDIA_HAS_GSM_CODEC */
497
498# if PJMEDIA_HAS_G711_CODEC
499 pjmedia_codec_g711_deinit();
500# endif /* PJMEDIA_HAS_G711_CODEC */
501
502# if PJMEDIA_HAS_L16_CODEC
503 pjmedia_codec_l16_deinit();
504# endif /* PJMEDIA_HAS_L16_CODEC */
505
506
507 pjmedia_endpt_destroy(pjsua_var.med_endpt);
508 pjsua_var.med_endpt = NULL;
Benny Prijonoeebe9af2006-06-13 22:57:13 +0000509
Benny Prijonoad2e0ca2007-04-29 12:31:51 +0000510 /* Deinitialize sound subsystem */
Benny Prijonoa41d66e2007-10-01 12:17:23 +0000511 // Not necessary, as pjmedia_snd_deinit() should have been called
512 // in pjmedia_endpt_destroy().
513 //pjmedia_snd_deinit();
Benny Prijonoad2e0ca2007-04-29 12:31:51 +0000514 }
Benny Prijonoaf1bb1e2006-11-21 12:39:31 +0000515
Benny Prijonode479562007-03-15 10:23:55 +0000516 /* Reset RTP port */
517 next_rtp_port = 0;
518
Benny Prijonoeebe9af2006-06-13 22:57:13 +0000519 return PJ_SUCCESS;
520}
521
522
Benny Prijonoc97608e2007-03-23 16:34:20 +0000523/* Create normal UDP media transports */
524static pj_status_t create_udp_media_transports(pjsua_transport_config *cfg)
Benny Prijonoeebe9af2006-06-13 22:57:13 +0000525{
Benny Prijonoeebe9af2006-06-13 22:57:13 +0000526 unsigned i;
Benny Prijono617c5bc2007-04-02 19:51:21 +0000527 pjmedia_sock_info skinfo;
Benny Prijonoeebe9af2006-06-13 22:57:13 +0000528 pj_status_t status;
529
Benny Prijonoeebe9af2006-06-13 22:57:13 +0000530 /* Create each media transport */
531 for (i=0; i<pjsua_var.ua_cfg.max_calls; ++i) {
532
Benny Prijono617c5bc2007-04-02 19:51:21 +0000533 status = create_rtp_rtcp_sock(cfg, &skinfo);
Benny Prijonoeebe9af2006-06-13 22:57:13 +0000534 if (status != PJ_SUCCESS) {
535 pjsua_perror(THIS_FILE, "Unable to create RTP/RTCP socket",
536 status);
537 goto on_error;
538 }
Benny Prijonod8179652008-01-23 20:39:07 +0000539
Benny Prijonoeebe9af2006-06-13 22:57:13 +0000540 status = pjmedia_transport_udp_attach(pjsua_var.med_endpt, NULL,
Benny Prijono617c5bc2007-04-02 19:51:21 +0000541 &skinfo, 0,
Benny Prijonoeebe9af2006-06-13 22:57:13 +0000542 &pjsua_var.calls[i].med_tp);
543 if (status != PJ_SUCCESS) {
544 pjsua_perror(THIS_FILE, "Unable to create media transport",
545 status);
546 goto on_error;
547 }
Benny Prijono00cae612006-07-31 15:19:36 +0000548
Benny Prijonod8179652008-01-23 20:39:07 +0000549 pjmedia_transport_simulate_lost(pjsua_var.calls[i].med_tp,
550 PJMEDIA_DIR_ENCODING,
551 pjsua_var.media_cfg.tx_drop_pct);
Benny Prijono00cae612006-07-31 15:19:36 +0000552
Benny Prijonod8179652008-01-23 20:39:07 +0000553 pjmedia_transport_simulate_lost(pjsua_var.calls[i].med_tp,
554 PJMEDIA_DIR_DECODING,
555 pjsua_var.media_cfg.rx_drop_pct);
Benny Prijono00cae612006-07-31 15:19:36 +0000556
Benny Prijonoeebe9af2006-06-13 22:57:13 +0000557 }
558
Benny Prijonoc97608e2007-03-23 16:34:20 +0000559 return PJ_SUCCESS;
560
561on_error:
562 for (i=0; i<pjsua_var.ua_cfg.max_calls; ++i) {
563 if (pjsua_var.calls[i].med_tp != NULL) {
564 pjmedia_transport_close(pjsua_var.calls[i].med_tp);
565 pjsua_var.calls[i].med_tp = NULL;
566 }
567 }
568
569 return status;
570}
571
572
Benny Prijono096c56c2007-09-15 08:30:16 +0000573/* This callback is called when ICE negotiation completes */
574static void on_ice_complete(pjmedia_transport *tp, pj_status_t result)
575{
576 unsigned id, c;
577 pj_bool_t found = PJ_FALSE;
578
579 /* We're only interested with failure case */
580 if (result == PJ_SUCCESS)
581 return;
582
583 /* Find call which has this media transport */
584
585 PJSUA_LOCK();
586
587 for (id=0, c=0; id<PJSUA_MAX_CALLS && c<pjsua_var.call_cnt; ++id) {
588 pjsua_call *call = &pjsua_var.calls[id];
589 if (call->inv) {
590 ++c;
591
592 if (call->med_tp == tp) {
593 call->media_st = PJSUA_CALL_MEDIA_ERROR;
594 call->media_dir = PJMEDIA_DIR_NONE;
595 found = PJ_TRUE;
596 break;
597 }
598 }
599 }
600
601 PJSUA_UNLOCK();
602
603 if (found && pjsua_var.ua_cfg.cb.on_call_media_state) {
604 pjsua_var.ua_cfg.cb.on_call_media_state(id);
605 }
606}
607
608
Benny Prijonoc97608e2007-03-23 16:34:20 +0000609/* Create ICE media transports (when ice is enabled) */
610static pj_status_t create_ice_media_transports(pjsua_transport_config *cfg)
611{
612 unsigned i;
Benny Prijonob681a2f2007-03-25 18:44:51 +0000613 pj_sockaddr_in addr;
Benny Prijonoc97608e2007-03-23 16:34:20 +0000614 pj_status_t status;
615
Benny Prijonoda9785b2007-04-02 20:43:06 +0000616 /* Make sure STUN server resolution has completed */
617 status = pjsua_resolve_stun_server(PJ_TRUE);
618 if (status != PJ_SUCCESS) {
619 pjsua_perror(THIS_FILE, "Error resolving STUN server", status);
620 return status;
621 }
622
Benny Prijonob681a2f2007-03-25 18:44:51 +0000623 pj_sockaddr_in_init(&addr, 0, (pj_uint16_t)cfg->port);
624
Benny Prijonoc97608e2007-03-23 16:34:20 +0000625 /* Create each media transport */
626 for (i=0; i<pjsua_var.ua_cfg.max_calls; ++i) {
Benny Prijonoa6bd7582007-03-28 15:49:48 +0000627 pj_ice_strans_comp comp;
Benny Prijono096c56c2007-09-15 08:30:16 +0000628 pjmedia_ice_cb ice_cb;
Benny Prijonob681a2f2007-03-25 18:44:51 +0000629 int next_port;
Benny Prijono38fb3ea2008-01-02 08:27:03 +0000630 char name[32];
Benny Prijonodc1fe222007-06-26 10:13:13 +0000631#if PJMEDIA_ADVERTISE_RTCP
632 enum { COMP_CNT=2 };
633#else
634 enum { COMP_CNT=1 };
635#endif
Benny Prijonoc97608e2007-03-23 16:34:20 +0000636
Benny Prijono096c56c2007-09-15 08:30:16 +0000637 pj_bzero(&ice_cb, sizeof(pjmedia_ice_cb));
638 ice_cb.on_ice_complete = &on_ice_complete;
639
Benny Prijono38fb3ea2008-01-02 08:27:03 +0000640 pj_ansi_snprintf(name, sizeof(name), "icetp%02d", i);
641
642 status = pjmedia_ice_create(pjsua_var.med_endpt, name, COMP_CNT,
Benny Prijono096c56c2007-09-15 08:30:16 +0000643 &pjsua_var.stun_cfg, &ice_cb,
Benny Prijonoc97608e2007-03-23 16:34:20 +0000644 &pjsua_var.calls[i].med_tp);
645 if (status != PJ_SUCCESS) {
646 pjsua_perror(THIS_FILE, "Unable to create ICE media transport",
647 status);
648 goto on_error;
649 }
650
Benny Prijonod8179652008-01-23 20:39:07 +0000651 pjmedia_transport_simulate_lost(pjsua_var.calls[i].med_tp,
652 PJMEDIA_DIR_ENCODING,
653 pjsua_var.media_cfg.tx_drop_pct);
Benny Prijono11da9bc2007-09-15 08:55:00 +0000654
Benny Prijonod8179652008-01-23 20:39:07 +0000655 pjmedia_transport_simulate_lost(pjsua_var.calls[i].med_tp,
656 PJMEDIA_DIR_DECODING,
657 pjsua_var.media_cfg.rx_drop_pct);
Benny Prijono11da9bc2007-09-15 08:55:00 +0000658
Benny Prijonob681a2f2007-03-25 18:44:51 +0000659 status = pjmedia_ice_start_init(pjsua_var.calls[i].med_tp, 0, &addr,
660 &pjsua_var.stun_srv.ipv4, NULL);
Benny Prijonoc97608e2007-03-23 16:34:20 +0000661 if (status != PJ_SUCCESS) {
Benny Prijonob681a2f2007-03-25 18:44:51 +0000662 pjsua_perror(THIS_FILE, "Error starting ICE transport",
Benny Prijonoc97608e2007-03-23 16:34:20 +0000663 status);
664 goto on_error;
665 }
666
Benny Prijonob681a2f2007-03-25 18:44:51 +0000667 pjmedia_ice_get_comp(pjsua_var.calls[i].med_tp, 1, &comp);
668 next_port = pj_ntohs(comp.local_addr.ipv4.sin_port);
669 next_port += 2;
670 addr.sin_port = pj_htons((pj_uint16_t)next_port);
Benny Prijonoc97608e2007-03-23 16:34:20 +0000671 }
672
673 /* Wait until all ICE transports are ready */
674 for (i=0; i<pjsua_var.ua_cfg.max_calls; ++i) {
Benny Prijonoc97608e2007-03-23 16:34:20 +0000675
676 /* Wait until interface status is PJ_SUCCESS */
677 for (;;) {
Benny Prijonob681a2f2007-03-25 18:44:51 +0000678 status = pjmedia_ice_get_init_status(pjsua_var.calls[i].med_tp);
Benny Prijonoc97608e2007-03-23 16:34:20 +0000679 if (status == PJ_EPENDING)
680 pjsua_handle_events(100);
681 else
682 break;
683 }
684
685 if (status != PJ_SUCCESS) {
686 pjsua_perror(THIS_FILE,
687 "Error adding STUN address to ICE media transport",
688 status);
689 goto on_error;
690 }
691
Benny Prijonoc97608e2007-03-23 16:34:20 +0000692 }
Benny Prijonoeebe9af2006-06-13 22:57:13 +0000693
694 return PJ_SUCCESS;
695
696on_error:
697 for (i=0; i<pjsua_var.ua_cfg.max_calls; ++i) {
698 if (pjsua_var.calls[i].med_tp != NULL) {
Benny Prijonoc97608e2007-03-23 16:34:20 +0000699 pjmedia_transport_close(pjsua_var.calls[i].med_tp);
Benny Prijonoeebe9af2006-06-13 22:57:13 +0000700 pjsua_var.calls[i].med_tp = NULL;
701 }
702 }
703
Benny Prijonoc97608e2007-03-23 16:34:20 +0000704 return status;
705}
706
707
708/*
709 * Create UDP media transports for all the calls. This function creates
710 * one UDP media transport for each call.
711 */
Benny Prijono1f61a8f2007-08-16 10:11:44 +0000712PJ_DEF(pj_status_t) pjsua_media_transports_create(
713 const pjsua_transport_config *app_cfg)
Benny Prijonoc97608e2007-03-23 16:34:20 +0000714{
715 pjsua_transport_config cfg;
716 unsigned i;
717 pj_status_t status;
718
719
720 /* Make sure pjsua_init() has been called */
721 PJ_ASSERT_RETURN(pjsua_var.ua_cfg.max_calls>0, PJ_EINVALIDOP);
722
723 PJSUA_LOCK();
724
725 /* Delete existing media transports */
726 for (i=0; i<pjsua_var.ua_cfg.max_calls; ++i) {
727 if (pjsua_var.calls[i].med_tp != NULL) {
728 pjmedia_transport_close(pjsua_var.calls[i].med_tp);
729 pjsua_var.calls[i].med_tp = NULL;
730 }
731 }
732
733 /* Copy config */
734 pjsua_transport_config_dup(pjsua_var.pool, &cfg, app_cfg);
735
736 if (pjsua_var.media_cfg.enable_ice) {
737 status = create_ice_media_transports(&cfg);
738 } else {
739 status = create_udp_media_transports(&cfg);
740 }
741
742
Benny Prijonoeebe9af2006-06-13 22:57:13 +0000743 PJSUA_UNLOCK();
744
745 return status;
746}
747
748
Benny Prijonoc97608e2007-03-23 16:34:20 +0000749pj_status_t pjsua_media_channel_init(pjsua_call_id call_id,
Benny Prijonod8179652008-01-23 20:39:07 +0000750 pjsip_role_e role,
Benny Prijono25b2ea12008-01-24 19:20:54 +0000751 int security_level,
752 int *sip_err_code)
Benny Prijonoc97608e2007-03-23 16:34:20 +0000753{
754 pjsua_call *call = &pjsua_var.calls[call_id];
755
Benny Prijonod8179652008-01-23 20:39:07 +0000756#if defined(PJMEDIA_HAS_SRTP) && (PJMEDIA_HAS_SRTP != 0)
757 pjsua_acc *acc = &pjsua_var.acc[call->acc_id];
758 pjmedia_srtp_setting srtp_opt;
759 pjmedia_transport *srtp;
760 pj_status_t status;
761#endif
Benny Prijonoc97608e2007-03-23 16:34:20 +0000762
Benny Prijonod8179652008-01-23 20:39:07 +0000763 PJ_UNUSED_ARG(role);
Benny Prijonoc97608e2007-03-23 16:34:20 +0000764
Benny Prijonod8179652008-01-23 20:39:07 +0000765 /* Return error if media transport has not been created yet
766 * (e.g. application is starting)
767 */
768 if (call->med_tp == NULL) {
769 return PJ_EBUSY;
Benny Prijonoc97608e2007-03-23 16:34:20 +0000770 }
771
Benny Prijonod8179652008-01-23 20:39:07 +0000772 /* Stop media transport (for good measure!) */
773 pjmedia_transport_media_stop(call->med_tp);
774
775#if defined(PJMEDIA_HAS_SRTP) && (PJMEDIA_HAS_SRTP != 0)
776 /* Check if SRTP requires secure signaling */
777 if (acc->cfg.use_srtp != PJMEDIA_SRTP_DISABLED) {
778 if (security_level < acc->cfg.srtp_secure_signaling) {
Benny Prijono25b2ea12008-01-24 19:20:54 +0000779 if (sip_err_code)
780 *sip_err_code = PJSIP_SC_NOT_ACCEPTABLE;
Benny Prijonod8179652008-01-23 20:39:07 +0000781 return PJSIP_ESESSIONINSECURE;
782 }
783 }
784
785 /* Always create SRTP adapter */
786 pjmedia_srtp_setting_default(&srtp_opt);
787 srtp_opt.close_member_tp = PJ_FALSE;
788 srtp_opt.use = acc->cfg.use_srtp;
789 status = pjmedia_transport_srtp_create(pjsua_var.med_endpt,
790 call->med_tp,
791 &srtp_opt, &srtp);
Benny Prijono25b2ea12008-01-24 19:20:54 +0000792 if (status != PJ_SUCCESS) {
793 if (sip_err_code)
794 *sip_err_code = PJSIP_SC_INTERNAL_SERVER_ERROR;
Benny Prijonod8179652008-01-23 20:39:07 +0000795 return status;
Benny Prijono25b2ea12008-01-24 19:20:54 +0000796 }
Benny Prijonod8179652008-01-23 20:39:07 +0000797
798 /* Set SRTP as current media transport */
799 call->med_orig = call->med_tp;
800 call->med_tp = srtp;
801#else
802 call->med_orig = call->med_tp;
803 PJ_UNUSED_ARG(security_level);
804#endif
805
Benny Prijonoc97608e2007-03-23 16:34:20 +0000806 return PJ_SUCCESS;
807}
808
809pj_status_t pjsua_media_channel_create_sdp(pjsua_call_id call_id,
810 pj_pool_t *pool,
Benny Prijonod8179652008-01-23 20:39:07 +0000811 const pjmedia_sdp_session *rem_sdp,
Benny Prijono25b2ea12008-01-24 19:20:54 +0000812 pjmedia_sdp_session **p_sdp,
813 int *sip_status_code)
Benny Prijonoc97608e2007-03-23 16:34:20 +0000814{
Benny Prijonod8179652008-01-23 20:39:07 +0000815 enum { MAX_MEDIA = 1, MEDIA_IDX = 0 };
Benny Prijonoc97608e2007-03-23 16:34:20 +0000816 pjmedia_sdp_session *sdp;
Benny Prijono617c5bc2007-04-02 19:51:21 +0000817 pjmedia_sock_info skinfo;
Benny Prijonoc97608e2007-03-23 16:34:20 +0000818 pjsua_call *call = &pjsua_var.calls[call_id];
819 pj_status_t status;
820
Benny Prijono55e82352007-05-10 20:49:08 +0000821 /* Return error if media transport has not been created yet
822 * (e.g. application is starting)
823 */
824 if (call->med_tp == NULL) {
825 return PJ_EBUSY;
826 }
827
Benny Prijono617c5bc2007-04-02 19:51:21 +0000828 /* Get media socket info */
829 pjmedia_transport_get_info(call->med_tp, &skinfo);
830
831 /* Create SDP */
Benny Prijonod8179652008-01-23 20:39:07 +0000832 status = pjmedia_endpt_create_sdp(pjsua_var.med_endpt, pool, MAX_MEDIA,
Benny Prijono617c5bc2007-04-02 19:51:21 +0000833 &skinfo, &sdp);
Benny Prijono25b2ea12008-01-24 19:20:54 +0000834 if (status != PJ_SUCCESS) {
835 if (sip_status_code) *sip_status_code = 500;
Benny Prijonoc97608e2007-03-23 16:34:20 +0000836 goto on_error;
Benny Prijono25b2ea12008-01-24 19:20:54 +0000837 }
Benny Prijonoc97608e2007-03-23 16:34:20 +0000838
Benny Prijono6ba8c542007-10-16 01:34:14 +0000839 /* Add NAT info in the SDP */
840 if (pjsua_var.ua_cfg.nat_type_in_sdp) {
841 pjmedia_sdp_attr *a;
842 pj_str_t value;
843 char nat_info[80];
844
845 value.ptr = nat_info;
846 if (pjsua_var.ua_cfg.nat_type_in_sdp == 1) {
847 value.slen = pj_ansi_snprintf(nat_info, sizeof(nat_info),
848 "%d", pjsua_var.nat_type);
849 } else {
850 const char *type_name = pj_stun_get_nat_name(pjsua_var.nat_type);
851 value.slen = pj_ansi_snprintf(nat_info, sizeof(nat_info),
852 "%d %s",
853 pjsua_var.nat_type,
854 type_name);
855 }
856
857 a = pjmedia_sdp_attr_create(pool, "X-nat", &value);
858
859 pjmedia_sdp_attr_add(&sdp->attr_count, sdp->attr, a);
860
861 }
862
Benny Prijonod8179652008-01-23 20:39:07 +0000863 /* Give the SDP to media transport */
Benny Prijono2dbed822008-02-21 10:08:27 +0000864 status = pjmedia_transport_media_create(call->med_tp, pool, 0,
Benny Prijonod8179652008-01-23 20:39:07 +0000865 sdp, rem_sdp, MEDIA_IDX);
Benny Prijono25b2ea12008-01-24 19:20:54 +0000866 if (status != PJ_SUCCESS) {
867 if (sip_status_code) *sip_status_code = PJSIP_SC_NOT_ACCEPTABLE;
Benny Prijonod8179652008-01-23 20:39:07 +0000868 goto on_error;
Benny Prijono25b2ea12008-01-24 19:20:54 +0000869 }
Benny Prijonoc97608e2007-03-23 16:34:20 +0000870
871 *p_sdp = sdp;
872 return PJ_SUCCESS;
873
874on_error:
875 pjsua_media_channel_deinit(call_id);
876 return status;
877
878}
879
880
881static void stop_media_session(pjsua_call_id call_id)
882{
883 pjsua_call *call = &pjsua_var.calls[call_id];
884
885 if (call->conf_slot != PJSUA_INVALID_ID) {
886 pjmedia_conf_remove_port(pjsua_var.mconf, call->conf_slot);
887 call->conf_slot = PJSUA_INVALID_ID;
888 }
889
890 if (call->session) {
Benny Prijonofc13bf62008-02-20 08:56:15 +0000891 if (pjsua_var.ua_cfg.cb.on_stream_destroyed) {
892 pjsua_var.ua_cfg.cb.on_stream_destroyed(call_id, call->session, 0);
893 }
894
Benny Prijonoc97608e2007-03-23 16:34:20 +0000895 pjmedia_session_destroy(call->session);
896 call->session = NULL;
897
898 PJ_LOG(4,(THIS_FILE, "Media session for call %d is destroyed",
899 call_id));
900
901 }
902
903 call->media_st = PJSUA_CALL_MEDIA_NONE;
904}
905
906pj_status_t pjsua_media_channel_deinit(pjsua_call_id call_id)
907{
908 pjsua_call *call = &pjsua_var.calls[call_id];
909
910 stop_media_session(call_id);
911
Benny Prijonod8179652008-01-23 20:39:07 +0000912 pjmedia_transport_media_stop(call->med_tp);
Benny Prijonoc97608e2007-03-23 16:34:20 +0000913
Benny Prijonod8179652008-01-23 20:39:07 +0000914 if (call->med_tp != call->med_orig) {
915 pjmedia_transport_close(call->med_tp);
916 call->med_tp = call->med_orig;
917 }
Benny Prijonoc97608e2007-03-23 16:34:20 +0000918 return PJ_SUCCESS;
919}
920
921
922/*
923 * DTMF callback from the stream.
924 */
925static void dtmf_callback(pjmedia_stream *strm, void *user_data,
926 int digit)
927{
928 PJ_UNUSED_ARG(strm);
929
Benny Prijono0c068262008-02-14 14:38:52 +0000930 /* For discussions about call mutex protection related to this
931 * callback, please see ticket #460:
932 * http://trac.pjsip.org/repos/ticket/460#comment:4
933 */
Benny Prijonoc97608e2007-03-23 16:34:20 +0000934 if (pjsua_var.ua_cfg.cb.on_dtmf_digit) {
935 pjsua_call_id call_id;
936
Benny Prijonod8179652008-01-23 20:39:07 +0000937 call_id = (pjsua_call_id)(long)user_data;
Benny Prijonoc97608e2007-03-23 16:34:20 +0000938 pjsua_var.ua_cfg.cb.on_dtmf_digit(call_id, digit);
939 }
940}
941
942
943pj_status_t pjsua_media_channel_update(pjsua_call_id call_id,
Benny Prijonod8179652008-01-23 20:39:07 +0000944 pjmedia_sdp_session *local_sdp,
Benny Prijonodbce2cf2007-03-28 16:24:00 +0000945 const pjmedia_sdp_session *remote_sdp)
Benny Prijonoc97608e2007-03-23 16:34:20 +0000946{
Benny Prijono91e567e2007-12-28 08:51:58 +0000947 unsigned i;
Benny Prijonoc97608e2007-03-23 16:34:20 +0000948 int prev_media_st = 0;
949 pjsua_call *call = &pjsua_var.calls[call_id];
950 pjmedia_session_info sess_info;
Benny Prijono91e567e2007-12-28 08:51:58 +0000951 pjmedia_stream_info *si = NULL;
Benny Prijonoc97608e2007-03-23 16:34:20 +0000952 pjmedia_port *media_port;
Benny Prijonoc97608e2007-03-23 16:34:20 +0000953 pj_status_t status;
954
955 /* Destroy existing media session, if any. */
956 prev_media_st = call->media_st;
957 stop_media_session(call->index);
958
959 /* Create media session info based on SDP parameters.
Benny Prijonoc97608e2007-03-23 16:34:20 +0000960 */
961 status = pjmedia_session_info_from_sdp( call->inv->dlg->pool,
962 pjsua_var.med_endpt,
Benny Prijono91e567e2007-12-28 08:51:58 +0000963 PJMEDIA_MAX_SDP_MEDIA, &sess_info,
Benny Prijonoc97608e2007-03-23 16:34:20 +0000964 local_sdp, remote_sdp);
965 if (status != PJ_SUCCESS)
966 return status;
967
Benny Prijono91e567e2007-12-28 08:51:58 +0000968 /* Find which session is audio (we only support audio for now) */
969 for (i=0; i < sess_info.stream_cnt; ++i) {
970 if (sess_info.stream_info[i].type == PJMEDIA_TYPE_AUDIO &&
Benny Prijonod8179652008-01-23 20:39:07 +0000971 (sess_info.stream_info[i].proto == PJMEDIA_TP_PROTO_RTP_AVP ||
972 sess_info.stream_info[i].proto == PJMEDIA_TP_PROTO_RTP_SAVP))
Benny Prijono91e567e2007-12-28 08:51:58 +0000973 {
974 si = &sess_info.stream_info[i];
975 break;
976 }
977 }
978
979 if (si == NULL) {
980 /* Not found */
981 return PJMEDIA_EINVALIMEDIATYPE;
982 }
983
984
985 /* Reset session info with only one media stream */
986 sess_info.stream_cnt = 1;
987 if (si != &sess_info.stream_info[0])
988 pj_memcpy(&sess_info.stream_info[0], si, sizeof(pjmedia_stream_info));
Benny Prijonoc97608e2007-03-23 16:34:20 +0000989
990 /* Check if media is put on-hold */
Benny Prijono91e567e2007-12-28 08:51:58 +0000991 if (sess_info.stream_cnt == 0 || si->dir == PJMEDIA_DIR_NONE)
Benny Prijonoc97608e2007-03-23 16:34:20 +0000992 {
993
994 /* Determine who puts the call on-hold */
995 if (prev_media_st == PJSUA_CALL_MEDIA_ACTIVE) {
996 if (pjmedia_sdp_neg_was_answer_remote(call->inv->neg)) {
997 /* It was local who offer hold */
998 call->media_st = PJSUA_CALL_MEDIA_LOCAL_HOLD;
999 } else {
1000 call->media_st = PJSUA_CALL_MEDIA_REMOTE_HOLD;
1001 }
1002 }
1003
1004 call->media_dir = PJMEDIA_DIR_NONE;
1005
Benny Prijonod8179652008-01-23 20:39:07 +00001006 /* Shutdown transport's session */
1007 pjmedia_transport_media_stop(call->med_tp);
Benny Prijono667952e2007-04-02 19:27:54 +00001008
Benny Prijonoc97608e2007-03-23 16:34:20 +00001009 /* No need because we need keepalive? */
1010
1011 } else {
Benny Prijonod8179652008-01-23 20:39:07 +00001012 /* Start media transport */
1013 status = pjmedia_transport_media_start(call->med_tp,
1014 call->inv->pool,
1015 local_sdp, remote_sdp, 0);
1016 if (status != PJ_SUCCESS)
1017 return status;
Benny Prijonoc97608e2007-03-23 16:34:20 +00001018
1019 /* Override ptime, if this option is specified. */
1020 if (pjsua_var.media_cfg.ptime != 0) {
Benny Prijono91e567e2007-12-28 08:51:58 +00001021 si->param->setting.frm_per_pkt = (pj_uint8_t)
1022 (pjsua_var.media_cfg.ptime / si->param->info.frm_ptime);
1023 if (si->param->setting.frm_per_pkt == 0)
1024 si->param->setting.frm_per_pkt = 1;
Benny Prijonoc97608e2007-03-23 16:34:20 +00001025 }
1026
1027 /* Disable VAD, if this option is specified. */
1028 if (pjsua_var.media_cfg.no_vad) {
Benny Prijono91e567e2007-12-28 08:51:58 +00001029 si->param->setting.vad = 0;
Benny Prijonoc97608e2007-03-23 16:34:20 +00001030 }
1031
1032
1033 /* Optionally, application may modify other stream settings here
1034 * (such as jitter buffer parameters, codec ptime, etc.)
1035 */
Benny Prijono91e567e2007-12-28 08:51:58 +00001036 si->jb_init = pjsua_var.media_cfg.jb_init;
1037 si->jb_min_pre = pjsua_var.media_cfg.jb_min_pre;
1038 si->jb_max_pre = pjsua_var.media_cfg.jb_max_pre;
1039 si->jb_max = pjsua_var.media_cfg.jb_max;
Benny Prijonoc97608e2007-03-23 16:34:20 +00001040
Benny Prijono8147f402007-11-21 14:50:07 +00001041 /* Set SSRC */
Benny Prijono91e567e2007-12-28 08:51:58 +00001042 si->ssrc = call->ssrc;
Benny Prijono8147f402007-11-21 14:50:07 +00001043
Benny Prijonoc97608e2007-03-23 16:34:20 +00001044 /* Create session based on session info. */
1045 status = pjmedia_session_create( pjsua_var.med_endpt, &sess_info,
1046 &call->med_tp,
1047 call, &call->session );
1048 if (status != PJ_SUCCESS) {
1049 return status;
1050 }
1051
1052 /* If DTMF callback is installed by application, install our
1053 * callback to the session.
1054 */
1055 if (pjsua_var.ua_cfg.cb.on_dtmf_digit) {
1056 pjmedia_session_set_dtmf_callback(call->session, 0,
1057 &dtmf_callback,
Benny Prijonod8179652008-01-23 20:39:07 +00001058 (void*)(long)(call->index));
Benny Prijonoc97608e2007-03-23 16:34:20 +00001059 }
1060
1061 /* Get the port interface of the first stream in the session.
1062 * We need the port interface to add to the conference bridge.
1063 */
1064 pjmedia_session_get_port(call->session, 0, &media_port);
1065
Benny Prijonofc13bf62008-02-20 08:56:15 +00001066 /* Notify application about stream creation.
1067 * Note: application may modify media_port to point to different
1068 * media port
1069 */
1070 if (pjsua_var.ua_cfg.cb.on_stream_created) {
1071 pjsua_var.ua_cfg.cb.on_stream_created(call_id, call->session,
1072 0, &media_port);
1073 }
Benny Prijonoc97608e2007-03-23 16:34:20 +00001074
1075 /*
1076 * Add the call to conference bridge.
1077 */
Benny Prijono38fb3ea2008-01-02 08:27:03 +00001078 {
1079 char tmp[PJSIP_MAX_URL_SIZE];
1080 pj_str_t port_name;
1081
1082 port_name.ptr = tmp;
1083 port_name.slen = pjsip_uri_print(PJSIP_URI_IN_REQ_URI,
1084 call->inv->dlg->remote.info->uri,
1085 tmp, sizeof(tmp));
1086 if (port_name.slen < 1) {
1087 port_name = pj_str("call");
1088 }
1089 status = pjmedia_conf_add_port( pjsua_var.mconf, call->inv->pool,
1090 media_port,
1091 &port_name,
1092 (unsigned*)&call->conf_slot);
1093 if (status != PJ_SUCCESS) {
1094 return status;
1095 }
Benny Prijonoc97608e2007-03-23 16:34:20 +00001096 }
1097
1098 /* Call's media state is active */
1099 call->media_st = PJSUA_CALL_MEDIA_ACTIVE;
Benny Prijono91e567e2007-12-28 08:51:58 +00001100 call->media_dir = si->dir;
Benny Prijonoc97608e2007-03-23 16:34:20 +00001101 }
1102
1103 /* Print info. */
1104 {
1105 char info[80];
1106 int info_len = 0;
1107 unsigned i;
1108
1109 for (i=0; i<sess_info.stream_cnt; ++i) {
1110 int len;
1111 const char *dir;
1112 pjmedia_stream_info *strm_info = &sess_info.stream_info[i];
1113
1114 switch (strm_info->dir) {
1115 case PJMEDIA_DIR_NONE:
1116 dir = "inactive";
1117 break;
1118 case PJMEDIA_DIR_ENCODING:
1119 dir = "sendonly";
1120 break;
1121 case PJMEDIA_DIR_DECODING:
1122 dir = "recvonly";
1123 break;
1124 case PJMEDIA_DIR_ENCODING_DECODING:
1125 dir = "sendrecv";
1126 break;
1127 default:
1128 dir = "unknown";
1129 break;
1130 }
1131 len = pj_ansi_sprintf( info+info_len,
1132 ", stream #%d: %.*s (%s)", i,
1133 (int)strm_info->fmt.encoding_name.slen,
1134 strm_info->fmt.encoding_name.ptr,
1135 dir);
1136 if (len > 0)
1137 info_len += len;
1138 }
1139 PJ_LOG(4,(THIS_FILE,"Media updates%s", info));
1140 }
1141
1142 return PJ_SUCCESS;
1143}
1144
1145
Benny Prijonoeebe9af2006-06-13 22:57:13 +00001146/*
1147 * Get maxinum number of conference ports.
1148 */
1149PJ_DEF(unsigned) pjsua_conf_get_max_ports(void)
1150{
1151 return pjsua_var.media_cfg.max_media_ports;
1152}
1153
1154
1155/*
1156 * Get current number of active ports in the bridge.
1157 */
1158PJ_DEF(unsigned) pjsua_conf_get_active_ports(void)
1159{
Benny Prijono38fb3ea2008-01-02 08:27:03 +00001160 unsigned ports[PJSUA_MAX_CONF_PORTS];
Benny Prijonoeebe9af2006-06-13 22:57:13 +00001161 unsigned count = PJ_ARRAY_SIZE(ports);
1162 pj_status_t status;
1163
1164 status = pjmedia_conf_enum_ports(pjsua_var.mconf, ports, &count);
1165 if (status != PJ_SUCCESS)
1166 count = 0;
1167
1168 return count;
1169}
1170
1171
1172/*
1173 * Enumerate all conference ports.
1174 */
1175PJ_DEF(pj_status_t) pjsua_enum_conf_ports(pjsua_conf_port_id id[],
1176 unsigned *count)
1177{
1178 return pjmedia_conf_enum_ports(pjsua_var.mconf, (unsigned*)id, count);
1179}
1180
1181
1182/*
1183 * Get information about the specified conference port
1184 */
1185PJ_DEF(pj_status_t) pjsua_conf_get_port_info( pjsua_conf_port_id id,
1186 pjsua_conf_port_info *info)
1187{
1188 pjmedia_conf_port_info cinfo;
Benny Prijono0498d902006-06-19 14:49:14 +00001189 unsigned i;
Benny Prijonoeebe9af2006-06-13 22:57:13 +00001190 pj_status_t status;
1191
1192 status = pjmedia_conf_get_port_info( pjsua_var.mconf, id, &cinfo);
1193 if (status != PJ_SUCCESS)
1194 return status;
1195
Benny Prijonoac623b32006-07-03 15:19:31 +00001196 pj_bzero(info, sizeof(*info));
Benny Prijonoeebe9af2006-06-13 22:57:13 +00001197 info->slot_id = id;
1198 info->name = cinfo.name;
1199 info->clock_rate = cinfo.clock_rate;
1200 info->channel_count = cinfo.channel_count;
1201 info->samples_per_frame = cinfo.samples_per_frame;
1202 info->bits_per_sample = cinfo.bits_per_sample;
1203
1204 /* Build array of listeners */
Benny Prijonoc78c3a32006-06-16 15:54:43 +00001205 info->listener_cnt = cinfo.listener_cnt;
1206 for (i=0; i<cinfo.listener_cnt; ++i) {
1207 info->listeners[i] = cinfo.listener_slots[i];
Benny Prijonoeebe9af2006-06-13 22:57:13 +00001208 }
1209
1210 return PJ_SUCCESS;
1211}
1212
1213
1214/*
Benny Prijonoe909eac2006-07-27 22:04:56 +00001215 * Add arbitrary media port to PJSUA's conference bridge.
1216 */
1217PJ_DEF(pj_status_t) pjsua_conf_add_port( pj_pool_t *pool,
1218 pjmedia_port *port,
1219 pjsua_conf_port_id *p_id)
1220{
1221 pj_status_t status;
1222
1223 status = pjmedia_conf_add_port(pjsua_var.mconf, pool,
1224 port, NULL, (unsigned*)p_id);
1225 if (status != PJ_SUCCESS) {
1226 if (p_id)
1227 *p_id = PJSUA_INVALID_ID;
1228 }
1229
1230 return status;
1231}
1232
1233
1234/*
1235 * Remove arbitrary slot from the conference bridge.
1236 */
1237PJ_DEF(pj_status_t) pjsua_conf_remove_port(pjsua_conf_port_id id)
1238{
1239 return pjmedia_conf_remove_port(pjsua_var.mconf, (unsigned)id);
1240}
1241
1242
1243/*
Benny Prijonoeebe9af2006-06-13 22:57:13 +00001244 * Establish unidirectional media flow from souce to sink.
1245 */
1246PJ_DEF(pj_status_t) pjsua_conf_connect( pjsua_conf_port_id source,
1247 pjsua_conf_port_id sink)
1248{
1249 return pjmedia_conf_connect_port(pjsua_var.mconf, source, sink, 0);
1250}
1251
1252
1253/*
1254 * Disconnect media flow from the source to destination port.
1255 */
1256PJ_DEF(pj_status_t) pjsua_conf_disconnect( pjsua_conf_port_id source,
1257 pjsua_conf_port_id sink)
1258{
1259 return pjmedia_conf_disconnect_port(pjsua_var.mconf, source, sink);
1260}
1261
1262
Benny Prijono6dd967c2006-12-26 02:27:14 +00001263/*
1264 * Adjust the signal level to be transmitted from the bridge to the
1265 * specified port by making it louder or quieter.
1266 */
1267PJ_DEF(pj_status_t) pjsua_conf_adjust_tx_level(pjsua_conf_port_id slot,
1268 float level)
1269{
1270 return pjmedia_conf_adjust_tx_level(pjsua_var.mconf, slot,
1271 (int)((level-1) * 128));
1272}
1273
1274/*
1275 * Adjust the signal level to be received from the specified port (to
1276 * the bridge) by making it louder or quieter.
1277 */
1278PJ_DEF(pj_status_t) pjsua_conf_adjust_rx_level(pjsua_conf_port_id slot,
1279 float level)
1280{
1281 return pjmedia_conf_adjust_rx_level(pjsua_var.mconf, slot,
1282 (int)((level-1) * 128));
1283}
1284
1285
1286/*
1287 * Get last signal level transmitted to or received from the specified port.
1288 */
1289PJ_DEF(pj_status_t) pjsua_conf_get_signal_level(pjsua_conf_port_id slot,
1290 unsigned *tx_level,
1291 unsigned *rx_level)
1292{
1293 return pjmedia_conf_get_signal_level(pjsua_var.mconf, slot,
1294 tx_level, rx_level);
1295}
1296
Benny Prijonoeebe9af2006-06-13 22:57:13 +00001297/*****************************************************************************
1298 * File player.
1299 */
1300
Benny Prijonod5696da2007-07-17 16:25:45 +00001301static char* get_basename(const char *path, unsigned len)
1302{
1303 char *p = ((char*)path) + len;
1304
1305 if (len==0)
1306 return p;
1307
Benny Prijono1f61a8f2007-08-16 10:11:44 +00001308 for (--p; p!=path && *p!='/' && *p!='\\'; ) --p;
Benny Prijonod5696da2007-07-17 16:25:45 +00001309
1310 return (p==path) ? p : p+1;
1311}
1312
1313
Benny Prijonoeebe9af2006-06-13 22:57:13 +00001314/*
1315 * Create a file player, and automatically connect this player to
1316 * the conference bridge.
1317 */
1318PJ_DEF(pj_status_t) pjsua_player_create( const pj_str_t *filename,
1319 unsigned options,
Benny Prijonoeebe9af2006-06-13 22:57:13 +00001320 pjsua_player_id *p_id)
1321{
1322 unsigned slot, file_id;
Benny Prijonoa66c3312007-01-21 23:12:40 +00001323 char path[PJ_MAXPATH];
Benny Prijonod5696da2007-07-17 16:25:45 +00001324 pj_pool_t *pool;
Benny Prijonoeebe9af2006-06-13 22:57:13 +00001325 pjmedia_port *port;
1326 pj_status_t status;
1327
1328 if (pjsua_var.player_cnt >= PJ_ARRAY_SIZE(pjsua_var.player))
1329 return PJ_ETOOMANY;
1330
1331 PJSUA_LOCK();
1332
1333 for (file_id=0; file_id<PJ_ARRAY_SIZE(pjsua_var.player); ++file_id) {
1334 if (pjsua_var.player[file_id].port == NULL)
1335 break;
1336 }
1337
1338 if (file_id == PJ_ARRAY_SIZE(pjsua_var.player)) {
1339 /* This is unexpected */
1340 PJSUA_UNLOCK();
1341 pj_assert(0);
1342 return PJ_EBUG;
1343 }
1344
1345 pj_memcpy(path, filename->ptr, filename->slen);
1346 path[filename->slen] = '\0';
Benny Prijonod5696da2007-07-17 16:25:45 +00001347
1348 pool = pjsua_pool_create(get_basename(path, filename->slen), 1000, 1000);
1349 if (!pool) {
1350 PJSUA_UNLOCK();
1351 return PJ_ENOMEM;
1352 }
1353
1354 status = pjmedia_wav_player_port_create(pool, path,
Benny Prijonoeebe9af2006-06-13 22:57:13 +00001355 pjsua_var.mconf_cfg.samples_per_frame *
1356 1000 / pjsua_var.media_cfg.clock_rate,
Benny Prijono00cae612006-07-31 15:19:36 +00001357 options, 0, &port);
Benny Prijonoeebe9af2006-06-13 22:57:13 +00001358 if (status != PJ_SUCCESS) {
1359 PJSUA_UNLOCK();
1360 pjsua_perror(THIS_FILE, "Unable to open file for playback", status);
Benny Prijonod5696da2007-07-17 16:25:45 +00001361 pj_pool_release(pool);
Benny Prijonoeebe9af2006-06-13 22:57:13 +00001362 return status;
1363 }
1364
1365 status = pjmedia_conf_add_port(pjsua_var.mconf, pjsua_var.pool,
1366 port, filename, &slot);
1367 if (status != PJ_SUCCESS) {
1368 pjmedia_port_destroy(port);
1369 PJSUA_UNLOCK();
Benny Prijono32e4f492007-01-24 00:44:26 +00001370 pjsua_perror(THIS_FILE, "Unable to add file to conference bridge",
1371 status);
Benny Prijonod5696da2007-07-17 16:25:45 +00001372 pj_pool_release(pool);
Benny Prijonoeebe9af2006-06-13 22:57:13 +00001373 return status;
1374 }
1375
Benny Prijonoa66c3312007-01-21 23:12:40 +00001376 pjsua_var.player[file_id].type = 0;
Benny Prijonod5696da2007-07-17 16:25:45 +00001377 pjsua_var.player[file_id].pool = pool;
Benny Prijonoeebe9af2006-06-13 22:57:13 +00001378 pjsua_var.player[file_id].port = port;
1379 pjsua_var.player[file_id].slot = slot;
1380
1381 if (p_id) *p_id = file_id;
1382
1383 ++pjsua_var.player_cnt;
1384
1385 PJSUA_UNLOCK();
1386 return PJ_SUCCESS;
1387}
1388
1389
1390/*
Benny Prijonoa66c3312007-01-21 23:12:40 +00001391 * Create a file playlist media port, and automatically add the port
1392 * to the conference bridge.
1393 */
1394PJ_DEF(pj_status_t) pjsua_playlist_create( const pj_str_t file_names[],
1395 unsigned file_count,
1396 const pj_str_t *label,
1397 unsigned options,
1398 pjsua_player_id *p_id)
1399{
1400 unsigned slot, file_id, ptime;
Benny Prijonod5696da2007-07-17 16:25:45 +00001401 pj_pool_t *pool;
Benny Prijonoa66c3312007-01-21 23:12:40 +00001402 pjmedia_port *port;
1403 pj_status_t status;
1404
1405 if (pjsua_var.player_cnt >= PJ_ARRAY_SIZE(pjsua_var.player))
1406 return PJ_ETOOMANY;
1407
1408 PJSUA_LOCK();
1409
1410 for (file_id=0; file_id<PJ_ARRAY_SIZE(pjsua_var.player); ++file_id) {
1411 if (pjsua_var.player[file_id].port == NULL)
1412 break;
1413 }
1414
1415 if (file_id == PJ_ARRAY_SIZE(pjsua_var.player)) {
1416 /* This is unexpected */
1417 PJSUA_UNLOCK();
1418 pj_assert(0);
1419 return PJ_EBUG;
1420 }
1421
1422
1423 ptime = pjsua_var.mconf_cfg.samples_per_frame * 1000 /
1424 pjsua_var.media_cfg.clock_rate;
1425
Benny Prijonod5696da2007-07-17 16:25:45 +00001426 pool = pjsua_pool_create("playlist", 1000, 1000);
1427 if (!pool) {
1428 PJSUA_UNLOCK();
1429 return PJ_ENOMEM;
1430 }
1431
1432 status = pjmedia_wav_playlist_create(pool, label,
Benny Prijonoa66c3312007-01-21 23:12:40 +00001433 file_names, file_count,
1434 ptime, options, 0, &port);
1435 if (status != PJ_SUCCESS) {
1436 PJSUA_UNLOCK();
1437 pjsua_perror(THIS_FILE, "Unable to create playlist", status);
Benny Prijonod5696da2007-07-17 16:25:45 +00001438 pj_pool_release(pool);
Benny Prijonoa66c3312007-01-21 23:12:40 +00001439 return status;
1440 }
1441
Benny Prijonod5696da2007-07-17 16:25:45 +00001442 status = pjmedia_conf_add_port(pjsua_var.mconf, pool,
Benny Prijonoa66c3312007-01-21 23:12:40 +00001443 port, &port->info.name, &slot);
1444 if (status != PJ_SUCCESS) {
1445 pjmedia_port_destroy(port);
1446 PJSUA_UNLOCK();
1447 pjsua_perror(THIS_FILE, "Unable to add port", status);
Benny Prijonod5696da2007-07-17 16:25:45 +00001448 pj_pool_release(pool);
Benny Prijonoa66c3312007-01-21 23:12:40 +00001449 return status;
1450 }
1451
1452 pjsua_var.player[file_id].type = 1;
Benny Prijonod5696da2007-07-17 16:25:45 +00001453 pjsua_var.player[file_id].pool = pool;
Benny Prijonoa66c3312007-01-21 23:12:40 +00001454 pjsua_var.player[file_id].port = port;
1455 pjsua_var.player[file_id].slot = slot;
1456
1457 if (p_id) *p_id = file_id;
1458
1459 ++pjsua_var.player_cnt;
1460
1461 PJSUA_UNLOCK();
1462 return PJ_SUCCESS;
1463
1464}
1465
1466
1467/*
Benny Prijonoeebe9af2006-06-13 22:57:13 +00001468 * Get conference port ID associated with player.
1469 */
1470PJ_DEF(pjsua_conf_port_id) pjsua_player_get_conf_port(pjsua_player_id id)
1471{
Benny Prijonoa1e69682007-05-11 15:14:34 +00001472 PJ_ASSERT_RETURN(id>=0&&id<(int)PJ_ARRAY_SIZE(pjsua_var.player), PJ_EINVAL);
Benny Prijonoeebe9af2006-06-13 22:57:13 +00001473 PJ_ASSERT_RETURN(pjsua_var.player[id].port != NULL, PJ_EINVAL);
1474
1475 return pjsua_var.player[id].slot;
1476}
1477
Benny Prijono469b1522006-12-26 03:05:17 +00001478/*
1479 * Get the media port for the player.
1480 */
Benny Prijonobe41d862008-01-18 13:24:28 +00001481PJ_DEF(pj_status_t) pjsua_player_get_port( pjsua_player_id id,
Benny Prijono469b1522006-12-26 03:05:17 +00001482 pjmedia_port **p_port)
1483{
Benny Prijonoa1e69682007-05-11 15:14:34 +00001484 PJ_ASSERT_RETURN(id>=0&&id<(int)PJ_ARRAY_SIZE(pjsua_var.player), PJ_EINVAL);
Benny Prijono469b1522006-12-26 03:05:17 +00001485 PJ_ASSERT_RETURN(pjsua_var.player[id].port != NULL, PJ_EINVAL);
1486 PJ_ASSERT_RETURN(p_port != NULL, PJ_EINVAL);
1487
1488 *p_port = pjsua_var.player[id].port;
1489
1490 return PJ_SUCCESS;
1491}
Benny Prijonoeebe9af2006-06-13 22:57:13 +00001492
1493/*
1494 * Set playback position.
1495 */
1496PJ_DEF(pj_status_t) pjsua_player_set_pos( pjsua_player_id id,
1497 pj_uint32_t samples)
1498{
Benny Prijonoa1e69682007-05-11 15:14:34 +00001499 PJ_ASSERT_RETURN(id>=0&&id<(int)PJ_ARRAY_SIZE(pjsua_var.player), PJ_EINVAL);
Benny Prijonoeebe9af2006-06-13 22:57:13 +00001500 PJ_ASSERT_RETURN(pjsua_var.player[id].port != NULL, PJ_EINVAL);
Benny Prijonoa66c3312007-01-21 23:12:40 +00001501 PJ_ASSERT_RETURN(pjsua_var.player[id].type == 0, PJ_EINVAL);
Benny Prijonoeebe9af2006-06-13 22:57:13 +00001502
1503 return pjmedia_wav_player_port_set_pos(pjsua_var.player[id].port, samples);
1504}
1505
1506
1507/*
1508 * Close the file, remove the player from the bridge, and free
1509 * resources associated with the file player.
1510 */
1511PJ_DEF(pj_status_t) pjsua_player_destroy(pjsua_player_id id)
1512{
Benny Prijonoa1e69682007-05-11 15:14:34 +00001513 PJ_ASSERT_RETURN(id>=0&&id<(int)PJ_ARRAY_SIZE(pjsua_var.player), PJ_EINVAL);
Benny Prijonoeebe9af2006-06-13 22:57:13 +00001514 PJ_ASSERT_RETURN(pjsua_var.player[id].port != NULL, PJ_EINVAL);
1515
1516 PJSUA_LOCK();
1517
1518 if (pjsua_var.player[id].port) {
1519 pjmedia_conf_remove_port(pjsua_var.mconf,
1520 pjsua_var.player[id].slot);
1521 pjmedia_port_destroy(pjsua_var.player[id].port);
1522 pjsua_var.player[id].port = NULL;
1523 pjsua_var.player[id].slot = 0xFFFF;
Benny Prijonod5696da2007-07-17 16:25:45 +00001524 pj_pool_release(pjsua_var.player[id].pool);
1525 pjsua_var.player[id].pool = NULL;
Benny Prijonoeebe9af2006-06-13 22:57:13 +00001526 pjsua_var.player_cnt--;
1527 }
1528
1529 PJSUA_UNLOCK();
1530
1531 return PJ_SUCCESS;
1532}
1533
1534
1535/*****************************************************************************
1536 * File recorder.
1537 */
1538
1539/*
1540 * Create a file recorder, and automatically connect this recorder to
1541 * the conference bridge.
1542 */
1543PJ_DEF(pj_status_t) pjsua_recorder_create( const pj_str_t *filename,
Benny Prijono8f310522006-10-20 11:08:49 +00001544 unsigned enc_type,
1545 void *enc_param,
Benny Prijonoeebe9af2006-06-13 22:57:13 +00001546 pj_ssize_t max_size,
1547 unsigned options,
Benny Prijonoeebe9af2006-06-13 22:57:13 +00001548 pjsua_recorder_id *p_id)
1549{
Benny Prijonob3cdb2b2006-10-19 15:49:47 +00001550 enum Format
1551 {
1552 FMT_UNKNOWN,
1553 FMT_WAV,
1554 FMT_MP3,
1555 };
Benny Prijonoeebe9af2006-06-13 22:57:13 +00001556 unsigned slot, file_id;
Benny Prijonoa66c3312007-01-21 23:12:40 +00001557 char path[PJ_MAXPATH];
Benny Prijonob3cdb2b2006-10-19 15:49:47 +00001558 pj_str_t ext;
Benny Prijono8f310522006-10-20 11:08:49 +00001559 int file_format;
Benny Prijonod5696da2007-07-17 16:25:45 +00001560 pj_pool_t *pool;
Benny Prijonoeebe9af2006-06-13 22:57:13 +00001561 pjmedia_port *port;
1562 pj_status_t status;
1563
Benny Prijonob3cdb2b2006-10-19 15:49:47 +00001564 /* Filename must present */
1565 PJ_ASSERT_RETURN(filename != NULL, PJ_EINVAL);
1566
Benny Prijono00cae612006-07-31 15:19:36 +00001567 /* Don't support max_size at present */
Benny Prijonob3cdb2b2006-10-19 15:49:47 +00001568 PJ_ASSERT_RETURN(max_size == 0 || max_size == -1, PJ_EINVAL);
Benny Prijono00cae612006-07-31 15:19:36 +00001569
Benny Prijono8f310522006-10-20 11:08:49 +00001570 /* Don't support encoding type at present */
1571 PJ_ASSERT_RETURN(enc_type == 0, PJ_EINVAL);
Benny Prijono00cae612006-07-31 15:19:36 +00001572
Benny Prijonoeebe9af2006-06-13 22:57:13 +00001573 if (pjsua_var.rec_cnt >= PJ_ARRAY_SIZE(pjsua_var.recorder))
1574 return PJ_ETOOMANY;
1575
Benny Prijonob3cdb2b2006-10-19 15:49:47 +00001576 /* Determine the file format */
1577 ext.ptr = filename->ptr + filename->slen - 4;
1578 ext.slen = 4;
1579
1580 if (pj_stricmp2(&ext, ".wav") == 0)
1581 file_format = FMT_WAV;
1582 else if (pj_stricmp2(&ext, ".mp3") == 0)
1583 file_format = FMT_MP3;
1584 else {
1585 PJ_LOG(1,(THIS_FILE, "pjsua_recorder_create() error: unable to "
1586 "determine file format for %.*s",
1587 (int)filename->slen, filename->ptr));
1588 return PJ_ENOTSUP;
1589 }
1590
Benny Prijonoeebe9af2006-06-13 22:57:13 +00001591 PJSUA_LOCK();
1592
1593 for (file_id=0; file_id<PJ_ARRAY_SIZE(pjsua_var.recorder); ++file_id) {
1594 if (pjsua_var.recorder[file_id].port == NULL)
1595 break;
1596 }
1597
1598 if (file_id == PJ_ARRAY_SIZE(pjsua_var.recorder)) {
1599 /* This is unexpected */
1600 PJSUA_UNLOCK();
1601 pj_assert(0);
1602 return PJ_EBUG;
1603 }
1604
1605 pj_memcpy(path, filename->ptr, filename->slen);
1606 path[filename->slen] = '\0';
Benny Prijonob3cdb2b2006-10-19 15:49:47 +00001607
Benny Prijonod5696da2007-07-17 16:25:45 +00001608 pool = pjsua_pool_create(get_basename(path, filename->slen), 1000, 1000);
1609 if (!pool) {
1610 PJSUA_UNLOCK();
1611 return PJ_ENOMEM;
1612 }
1613
Benny Prijonob3cdb2b2006-10-19 15:49:47 +00001614 if (file_format == FMT_WAV) {
Benny Prijonod5696da2007-07-17 16:25:45 +00001615 status = pjmedia_wav_writer_port_create(pool, path,
Benny Prijonob3cdb2b2006-10-19 15:49:47 +00001616 pjsua_var.media_cfg.clock_rate,
1617 pjsua_var.mconf_cfg.channel_count,
1618 pjsua_var.mconf_cfg.samples_per_frame,
1619 pjsua_var.mconf_cfg.bits_per_sample,
1620 options, 0, &port);
Benny Prijonob3cdb2b2006-10-19 15:49:47 +00001621 } else {
Benny Prijonoc95a0f02007-04-09 07:06:08 +00001622 PJ_UNUSED_ARG(enc_param);
Benny Prijonob3cdb2b2006-10-19 15:49:47 +00001623 port = NULL;
1624 status = PJ_ENOTSUP;
1625 }
1626
Benny Prijonoeebe9af2006-06-13 22:57:13 +00001627 if (status != PJ_SUCCESS) {
1628 PJSUA_UNLOCK();
1629 pjsua_perror(THIS_FILE, "Unable to open file for recording", status);
Benny Prijonod5696da2007-07-17 16:25:45 +00001630 pj_pool_release(pool);
Benny Prijonoeebe9af2006-06-13 22:57:13 +00001631 return status;
1632 }
1633
Benny Prijonod5696da2007-07-17 16:25:45 +00001634 status = pjmedia_conf_add_port(pjsua_var.mconf, pool,
Benny Prijonoeebe9af2006-06-13 22:57:13 +00001635 port, filename, &slot);
1636 if (status != PJ_SUCCESS) {
1637 pjmedia_port_destroy(port);
1638 PJSUA_UNLOCK();
Benny Prijonod5696da2007-07-17 16:25:45 +00001639 pj_pool_release(pool);
Benny Prijonoeebe9af2006-06-13 22:57:13 +00001640 return status;
1641 }
1642
1643 pjsua_var.recorder[file_id].port = port;
1644 pjsua_var.recorder[file_id].slot = slot;
Benny Prijonod5696da2007-07-17 16:25:45 +00001645 pjsua_var.recorder[file_id].pool = pool;
Benny Prijonoeebe9af2006-06-13 22:57:13 +00001646
1647 if (p_id) *p_id = file_id;
1648
1649 ++pjsua_var.rec_cnt;
1650
1651 PJSUA_UNLOCK();
1652 return PJ_SUCCESS;
1653}
1654
1655
1656/*
1657 * Get conference port associated with recorder.
1658 */
1659PJ_DEF(pjsua_conf_port_id) pjsua_recorder_get_conf_port(pjsua_recorder_id id)
1660{
Benny Prijonoa1e69682007-05-11 15:14:34 +00001661 PJ_ASSERT_RETURN(id>=0 && id<(int)PJ_ARRAY_SIZE(pjsua_var.recorder),
1662 PJ_EINVAL);
Benny Prijonoeebe9af2006-06-13 22:57:13 +00001663 PJ_ASSERT_RETURN(pjsua_var.recorder[id].port != NULL, PJ_EINVAL);
1664
1665 return pjsua_var.recorder[id].slot;
1666}
1667
Benny Prijono469b1522006-12-26 03:05:17 +00001668/*
1669 * Get the media port for the recorder.
1670 */
1671PJ_DEF(pj_status_t) pjsua_recorder_get_port( pjsua_recorder_id id,
1672 pjmedia_port **p_port)
1673{
Benny Prijonoa1e69682007-05-11 15:14:34 +00001674 PJ_ASSERT_RETURN(id>=0 && id<(int)PJ_ARRAY_SIZE(pjsua_var.recorder),
1675 PJ_EINVAL);
Benny Prijono469b1522006-12-26 03:05:17 +00001676 PJ_ASSERT_RETURN(pjsua_var.recorder[id].port != NULL, PJ_EINVAL);
1677 PJ_ASSERT_RETURN(p_port != NULL, PJ_EINVAL);
1678
1679 *p_port = pjsua_var.recorder[id].port;
1680 return PJ_SUCCESS;
1681}
Benny Prijonoeebe9af2006-06-13 22:57:13 +00001682
1683/*
1684 * Destroy recorder (this will complete recording).
1685 */
1686PJ_DEF(pj_status_t) pjsua_recorder_destroy(pjsua_recorder_id id)
1687{
Benny Prijonoa1e69682007-05-11 15:14:34 +00001688 PJ_ASSERT_RETURN(id>=0 && id<(int)PJ_ARRAY_SIZE(pjsua_var.recorder),
1689 PJ_EINVAL);
Benny Prijonoeebe9af2006-06-13 22:57:13 +00001690 PJ_ASSERT_RETURN(pjsua_var.recorder[id].port != NULL, PJ_EINVAL);
1691
1692 PJSUA_LOCK();
1693
1694 if (pjsua_var.recorder[id].port) {
1695 pjmedia_conf_remove_port(pjsua_var.mconf,
1696 pjsua_var.recorder[id].slot);
1697 pjmedia_port_destroy(pjsua_var.recorder[id].port);
1698 pjsua_var.recorder[id].port = NULL;
1699 pjsua_var.recorder[id].slot = 0xFFFF;
Benny Prijonod5696da2007-07-17 16:25:45 +00001700 pj_pool_release(pjsua_var.recorder[id].pool);
1701 pjsua_var.recorder[id].pool = NULL;
Benny Prijonoeebe9af2006-06-13 22:57:13 +00001702 pjsua_var.rec_cnt--;
1703 }
1704
1705 PJSUA_UNLOCK();
1706
1707 return PJ_SUCCESS;
1708}
1709
1710
1711/*****************************************************************************
1712 * Sound devices.
1713 */
1714
1715/*
1716 * Enum sound devices.
1717 */
1718PJ_DEF(pj_status_t) pjsua_enum_snd_devs( pjmedia_snd_dev_info info[],
1719 unsigned *count)
1720{
1721 unsigned i, dev_count;
1722
1723 dev_count = pjmedia_snd_get_dev_count();
1724
1725 if (dev_count > *count) dev_count = *count;
1726
1727 for (i=0; i<dev_count; ++i) {
1728 const pjmedia_snd_dev_info *ci;
1729
1730 ci = pjmedia_snd_get_dev_info(i);
1731 pj_memcpy(&info[i], ci, sizeof(*ci));
1732 }
1733
1734 *count = dev_count;
1735
1736 return PJ_SUCCESS;
1737}
1738
1739
1740/* Close existing sound device */
1741static void close_snd_dev(void)
1742{
1743 /* Close sound device */
1744 if (pjsua_var.snd_port) {
1745 const pjmedia_snd_dev_info *cap_info, *play_info;
1746
1747 cap_info = pjmedia_snd_get_dev_info(pjsua_var.cap_dev);
1748 play_info = pjmedia_snd_get_dev_info(pjsua_var.play_dev);
1749
1750 PJ_LOG(4,(THIS_FILE, "Closing %s sound playback device and "
1751 "%s sound capture device",
1752 play_info->name, cap_info->name));
1753
1754 pjmedia_snd_port_disconnect(pjsua_var.snd_port);
1755 pjmedia_snd_port_destroy(pjsua_var.snd_port);
1756 pjsua_var.snd_port = NULL;
1757 }
1758
1759 /* Close null sound device */
1760 if (pjsua_var.null_snd) {
1761 PJ_LOG(4,(THIS_FILE, "Closing null sound device.."));
1762 pjmedia_master_port_destroy(pjsua_var.null_snd, PJ_FALSE);
1763 pjsua_var.null_snd = NULL;
1764 }
1765}
1766
1767/*
1768 * Select or change sound device. Application may call this function at
1769 * any time to replace current sound device.
1770 */
1771PJ_DEF(pj_status_t) pjsua_set_snd_dev( int capture_dev,
1772 int playback_dev)
1773{
1774 pjmedia_port *conf_port;
Benny Prijono6dd967c2006-12-26 02:27:14 +00001775 const pjmedia_snd_dev_info *play_info;
Benny Prijonof3758ee2008-02-26 15:32:16 +00001776 unsigned clock_rates[] = {0, 22050, 44100, 48000, 32000, 16000,
1777 8000};
Benny Prijono658a1c52006-10-11 21:56:16 +00001778 unsigned selected_clock_rate = 0;
Benny Prijono26056d82006-10-11 16:03:41 +00001779 unsigned i;
Benny Prijonoc53c6d72006-11-27 09:54:03 +00001780 pjmedia_snd_stream *strm;
1781 pjmedia_snd_stream_info si;
1782 pj_str_t tmp;
Benny Prijono26056d82006-10-11 16:03:41 +00001783 pj_status_t status = -1;
Benny Prijonoeebe9af2006-06-13 22:57:13 +00001784
1785 /* Close existing sound port */
1786 close_snd_dev();
1787
1788
Benny Prijono26056d82006-10-11 16:03:41 +00001789 /* Set default clock rate */
Benny Prijonof3758ee2008-02-26 15:32:16 +00001790 clock_rates[0] = pjsua_var.media_cfg.snd_clock_rate;
1791 if (clock_rates[0] == 0)
1792 clock_rates[0] = pjsua_var.media_cfg.clock_rate;
Benny Prijonoeebe9af2006-06-13 22:57:13 +00001793
Benny Prijono50f19b32008-03-11 13:15:43 +00001794 /* Get the port0 of the conference bridge. */
1795 conf_port = pjmedia_conf_get_master_port(pjsua_var.mconf);
1796 pj_assert(conf_port != NULL);
1797
Benny Prijono26056d82006-10-11 16:03:41 +00001798 /* Attempts to open the sound device with different clock rates */
1799 for (i=0; i<PJ_ARRAY_SIZE(clock_rates); ++i) {
1800 char errmsg[PJ_ERR_MSG_SIZE];
Benny Prijonocf0b4b22007-10-06 17:31:09 +00001801 unsigned fps;
Benny Prijono26056d82006-10-11 16:03:41 +00001802
1803 PJ_LOG(4,(THIS_FILE,
1804 "pjsua_set_snd_dev(): attempting to open devices "
1805 "@%d Hz", clock_rates[i]));
1806
1807 /* Create the sound device. Sound port will start immediately. */
Benny Prijonocf0b4b22007-10-06 17:31:09 +00001808 fps = 1000 / pjsua_var.media_cfg.audio_frame_ptime;
Benny Prijono26056d82006-10-11 16:03:41 +00001809 status = pjmedia_snd_port_create(pjsua_var.pool, capture_dev,
1810 playback_dev,
1811 clock_rates[i], 1,
Benny Prijonocf0b4b22007-10-06 17:31:09 +00001812 clock_rates[i]/fps,
Benny Prijono26056d82006-10-11 16:03:41 +00001813 16, 0, &pjsua_var.snd_port);
1814
Benny Prijono658a1c52006-10-11 21:56:16 +00001815 if (status == PJ_SUCCESS) {
1816 selected_clock_rate = clock_rates[i];
Benny Prijono50f19b32008-03-11 13:15:43 +00001817
1818 /* If there's mismatch between sound port and conference's port,
1819 * create a resample port to bridge them.
1820 */
1821 if (selected_clock_rate != pjsua_var.media_cfg.clock_rate) {
1822 pjmedia_port *resample_port;
1823 unsigned resample_opt = 0;
1824
1825 if (pjsua_var.media_cfg.quality >= 3 &&
1826 pjsua_var.media_cfg.quality <= 4)
1827 {
1828 resample_opt |= PJMEDIA_CONF_SMALL_FILTER;
1829 }
1830 else if (pjsua_var.media_cfg.quality < 3) {
1831 resample_opt |= PJMEDIA_CONF_USE_LINEAR;
1832 }
1833
1834 status = pjmedia_resample_port_create(pjsua_var.pool,
1835 conf_port,
1836 selected_clock_rate,
1837 resample_opt,
1838 &resample_port);
1839 if (status != PJ_SUCCESS) {
1840 pj_strerror(status, errmsg, sizeof(errmsg));
1841 PJ_LOG(4, (THIS_FILE,
1842 "Error creating resample port, trying next "
1843 "clock rate",
1844 errmsg));
1845 pjmedia_snd_port_destroy(pjsua_var.snd_port);
1846 pjsua_var.snd_port = NULL;
1847 continue;
1848 } else {
1849 conf_port = resample_port;
1850 break;
1851 }
1852
1853 } else {
1854 break;
1855 }
Benny Prijono658a1c52006-10-11 21:56:16 +00001856 }
Benny Prijono26056d82006-10-11 16:03:41 +00001857
1858 pj_strerror(status, errmsg, sizeof(errmsg));
Benny Prijono658a1c52006-10-11 21:56:16 +00001859 PJ_LOG(4, (THIS_FILE, "..failed: %s", errmsg));
Benny Prijono26056d82006-10-11 16:03:41 +00001860 }
1861
Benny Prijonoeebe9af2006-06-13 22:57:13 +00001862 if (status != PJ_SUCCESS) {
1863 pjsua_perror(THIS_FILE, "Unable to open sound device", status);
1864 return status;
1865 }
1866
Benny Prijonof20687a2006-08-04 18:27:19 +00001867 /* Set AEC */
Benny Prijono5da50432006-08-07 10:24:52 +00001868 pjmedia_snd_port_set_ec( pjsua_var.snd_port, pjsua_var.pool,
1869 pjsua_var.media_cfg.ec_tail_len,
1870 pjsua_var.media_cfg.ec_options);
Benny Prijonoeebe9af2006-06-13 22:57:13 +00001871
Benny Prijono52a93912006-08-04 20:54:37 +00001872 /* Connect sound port to the bridge */
1873 status = pjmedia_snd_port_connect(pjsua_var.snd_port,
1874 conf_port );
1875 if (status != PJ_SUCCESS) {
1876 pjsua_perror(THIS_FILE, "Unable to connect conference port to "
1877 "sound device", status);
1878 pjmedia_snd_port_destroy(pjsua_var.snd_port);
1879 pjsua_var.snd_port = NULL;
1880 return status;
1881 }
1882
Benny Prijonoeebe9af2006-06-13 22:57:13 +00001883 /* Save the device IDs */
1884 pjsua_var.cap_dev = capture_dev;
1885 pjsua_var.play_dev = playback_dev;
1886
Benny Prijonoc53c6d72006-11-27 09:54:03 +00001887 /* Update sound device name. */
1888 strm = pjmedia_snd_port_get_snd_stream(pjsua_var.snd_port);
1889 pjmedia_snd_stream_get_info(strm, &si);
1890 play_info = pjmedia_snd_get_dev_info(si.rec_id);
1891
Benny Prijonof3758ee2008-02-26 15:32:16 +00001892 if (si.clock_rate != pjsua_var.media_cfg.clock_rate) {
1893 char tmp_buf[128];
1894 int tmp_buf_len = sizeof(tmp_buf);
1895
1896 tmp_buf_len = pj_ansi_snprintf(tmp_buf, sizeof(tmp_buf)-1, "%s (%dKHz)",
1897 play_info->name, si.clock_rate/1000);
1898 pj_strset(&tmp, tmp_buf, tmp_buf_len);
1899 pjmedia_conf_set_port0_name(pjsua_var.mconf, &tmp);
1900 } else {
1901 pjmedia_conf_set_port0_name(pjsua_var.mconf,
1902 pj_cstr(&tmp, play_info->name));
1903 }
Benny Prijonoc53c6d72006-11-27 09:54:03 +00001904
Benny Prijonoeebe9af2006-06-13 22:57:13 +00001905 return PJ_SUCCESS;
1906}
1907
1908
1909/*
Benny Prijonoebdf8772007-02-01 19:25:50 +00001910 * Get currently active sound devices. If sound devices has not been created
1911 * (for example when pjsua_start() is not called), it is possible that
1912 * the function returns PJ_SUCCESS with -1 as device IDs.
1913 */
1914PJ_DEF(pj_status_t) pjsua_get_snd_dev(int *capture_dev,
1915 int *playback_dev)
1916{
1917 if (capture_dev) {
1918 *capture_dev = pjsua_var.cap_dev;
1919 }
1920 if (playback_dev) {
1921 *playback_dev = pjsua_var.play_dev;
1922 }
1923
1924 return PJ_SUCCESS;
1925}
1926
1927
1928/*
Benny Prijonoeebe9af2006-06-13 22:57:13 +00001929 * Use null sound device.
1930 */
1931PJ_DEF(pj_status_t) pjsua_set_null_snd_dev(void)
1932{
1933 pjmedia_port *conf_port;
1934 pj_status_t status;
1935
1936 /* Close existing sound device */
1937 close_snd_dev();
1938
1939 PJ_LOG(4,(THIS_FILE, "Opening null sound device.."));
1940
1941 /* Get the port0 of the conference bridge. */
1942 conf_port = pjmedia_conf_get_master_port(pjsua_var.mconf);
1943 pj_assert(conf_port != NULL);
1944
1945 /* Create master port, connecting port0 of the conference bridge to
1946 * a null port.
1947 */
1948 status = pjmedia_master_port_create(pjsua_var.pool, pjsua_var.null_port,
1949 conf_port, 0, &pjsua_var.null_snd);
1950 if (status != PJ_SUCCESS) {
1951 pjsua_perror(THIS_FILE, "Unable to create null sound device",
1952 status);
1953 return status;
1954 }
1955
1956 /* Start the master port */
1957 status = pjmedia_master_port_start(pjsua_var.null_snd);
1958 PJ_ASSERT_RETURN(status == PJ_SUCCESS, status);
1959
1960 return PJ_SUCCESS;
1961}
1962
1963
Benny Prijonoe909eac2006-07-27 22:04:56 +00001964
1965/*
1966 * Use no device!
1967 */
1968PJ_DEF(pjmedia_port*) pjsua_set_no_snd_dev(void)
1969{
1970 /* Close existing sound device */
1971 close_snd_dev();
1972
1973 pjsua_var.no_snd = PJ_TRUE;
1974 return pjmedia_conf_get_master_port(pjsua_var.mconf);
1975}
1976
1977
Benny Prijonof20687a2006-08-04 18:27:19 +00001978/*
1979 * Configure the AEC settings of the sound port.
1980 */
Benny Prijono5da50432006-08-07 10:24:52 +00001981PJ_DEF(pj_status_t) pjsua_set_ec(unsigned tail_ms, unsigned options)
Benny Prijonof20687a2006-08-04 18:27:19 +00001982{
1983 pjsua_var.media_cfg.ec_tail_len = tail_ms;
1984
1985 if (pjsua_var.snd_port)
Benny Prijono5da50432006-08-07 10:24:52 +00001986 return pjmedia_snd_port_set_ec( pjsua_var.snd_port, pjsua_var.pool,
1987 tail_ms, options);
Benny Prijonof20687a2006-08-04 18:27:19 +00001988
1989 return PJ_SUCCESS;
1990}
1991
1992
1993/*
1994 * Get current AEC tail length.
1995 */
Benny Prijono22dfe592006-08-06 12:07:13 +00001996PJ_DEF(pj_status_t) pjsua_get_ec_tail(unsigned *p_tail_ms)
Benny Prijonof20687a2006-08-04 18:27:19 +00001997{
1998 *p_tail_ms = pjsua_var.media_cfg.ec_tail_len;
1999 return PJ_SUCCESS;
2000}
2001
Benny Prijonoe909eac2006-07-27 22:04:56 +00002002
Benny Prijonoeebe9af2006-06-13 22:57:13 +00002003/*****************************************************************************
2004 * Codecs.
2005 */
2006
2007/*
2008 * Enum all supported codecs in the system.
2009 */
2010PJ_DEF(pj_status_t) pjsua_enum_codecs( pjsua_codec_info id[],
2011 unsigned *p_count )
2012{
2013 pjmedia_codec_mgr *codec_mgr;
2014 pjmedia_codec_info info[32];
2015 unsigned i, count, prio[32];
2016 pj_status_t status;
2017
2018 codec_mgr = pjmedia_endpt_get_codec_mgr(pjsua_var.med_endpt);
2019 count = PJ_ARRAY_SIZE(info);
2020 status = pjmedia_codec_mgr_enum_codecs( codec_mgr, &count, info, prio);
2021 if (status != PJ_SUCCESS) {
2022 *p_count = 0;
2023 return status;
2024 }
2025
2026 if (count > *p_count) count = *p_count;
2027
2028 for (i=0; i<count; ++i) {
2029 pjmedia_codec_info_to_id(&info[i], id[i].buf_, sizeof(id[i].buf_));
2030 id[i].codec_id = pj_str(id[i].buf_);
2031 id[i].priority = (pj_uint8_t) prio[i];
2032 }
2033
2034 *p_count = count;
2035
2036 return PJ_SUCCESS;
2037}
2038
2039
2040/*
2041 * Change codec priority.
2042 */
2043PJ_DEF(pj_status_t) pjsua_codec_set_priority( const pj_str_t *codec_id,
2044 pj_uint8_t priority )
2045{
2046 pjmedia_codec_mgr *codec_mgr;
2047
2048 codec_mgr = pjmedia_endpt_get_codec_mgr(pjsua_var.med_endpt);
2049
2050 return pjmedia_codec_mgr_set_codec_priority(codec_mgr, codec_id,
2051 priority);
2052}
2053
2054
2055/*
2056 * Get codec parameters.
2057 */
2058PJ_DEF(pj_status_t) pjsua_codec_get_param( const pj_str_t *codec_id,
2059 pjmedia_codec_param *param )
2060{
2061 const pjmedia_codec_info *info;
2062 pjmedia_codec_mgr *codec_mgr;
2063 unsigned count = 1;
2064 pj_status_t status;
2065
2066 codec_mgr = pjmedia_endpt_get_codec_mgr(pjsua_var.med_endpt);
2067
2068 status = pjmedia_codec_mgr_find_codecs_by_id(codec_mgr, codec_id,
2069 &count, &info, NULL);
2070 if (status != PJ_SUCCESS)
2071 return status;
2072
2073 if (count != 1)
2074 return PJ_ENOTFOUND;
2075
2076 status = pjmedia_codec_mgr_get_default_param( codec_mgr, info, param);
2077 return status;
2078}
2079
2080
2081/*
2082 * Set codec parameters.
2083 */
2084PJ_DEF(pj_status_t) pjsua_codec_set_param( const pj_str_t *id,
2085 const pjmedia_codec_param *param)
2086{
Benny Prijono00cae612006-07-31 15:19:36 +00002087 PJ_UNUSED_ARG(id);
2088 PJ_UNUSED_ARG(param);
Benny Prijonoeebe9af2006-06-13 22:57:13 +00002089 PJ_TODO(set_codec_param);
2090 return PJ_SUCCESS;
2091}