blob: 80e4383e8b9044ba58fb36786675a4d0d00d6c1e [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 Prijono403e24c2007-01-25 17:13:34 +000025#define PTIME 20
Benny Prijonoeebe9af2006-06-13 22:57:13 +000026#define FPS (1000/PTIME)
27#define DEFAULT_RTP_PORT 4000
28
Benny Prijonode479562007-03-15 10:23:55 +000029/* Next RTP port to be used */
30static pj_uint16_t next_rtp_port;
Benny Prijonoeebe9af2006-06-13 22:57:13 +000031
32/* Close existing sound device */
33static void close_snd_dev(void);
34
35
36/**
37 * Init media subsystems.
38 */
39pj_status_t pjsua_media_subsys_init(const pjsua_media_config *cfg)
40{
41 pj_str_t codec_id;
Benny Prijono0498d902006-06-19 14:49:14 +000042 unsigned opt;
Benny Prijonoeebe9af2006-06-13 22:57:13 +000043 pj_status_t status;
44
Benny Prijonofc24e692007-01-27 18:31:51 +000045 /* To suppress warning about unused var when all codecs are disabled */
46 PJ_UNUSED_ARG(codec_id);
47
Benny Prijonoeebe9af2006-06-13 22:57:13 +000048 /* Copy configuration */
49 pj_memcpy(&pjsua_var.media_cfg, cfg, sizeof(*cfg));
50
51 /* Normalize configuration */
Benny Prijonoeebe9af2006-06-13 22:57:13 +000052
53 if (pjsua_var.media_cfg.has_ioqueue &&
54 pjsua_var.media_cfg.thread_cnt == 0)
55 {
56 pjsua_var.media_cfg.thread_cnt = 1;
57 }
58
59 if (pjsua_var.media_cfg.max_media_ports < pjsua_var.ua_cfg.max_calls) {
60 pjsua_var.media_cfg.max_media_ports = pjsua_var.ua_cfg.max_calls + 2;
61 }
62
63 /* Create media endpoint. */
64 status = pjmedia_endpt_create(&pjsua_var.cp.factory,
65 pjsua_var.media_cfg.has_ioqueue? NULL :
66 pjsip_endpt_get_ioqueue(pjsua_var.endpt),
67 pjsua_var.media_cfg.thread_cnt,
68 &pjsua_var.med_endpt);
69 if (status != PJ_SUCCESS) {
70 pjsua_perror(THIS_FILE,
71 "Media stack initialization has returned error",
72 status);
73 return status;
74 }
75
76 /* Register all codecs */
77#if PJMEDIA_HAS_SPEEX_CODEC
78 /* Register speex. */
79 status = pjmedia_codec_speex_init(pjsua_var.med_endpt,
Benny Prijono7ca96da2006-08-07 12:11:40 +000080 0,
Benny Prijono0498d902006-06-19 14:49:14 +000081 pjsua_var.media_cfg.quality,
82 pjsua_var.media_cfg.quality);
Benny Prijonoeebe9af2006-06-13 22:57:13 +000083 if (status != PJ_SUCCESS) {
84 pjsua_perror(THIS_FILE, "Error initializing Speex codec",
85 status);
86 return status;
87 }
Benny Prijono7ca96da2006-08-07 12:11:40 +000088
89 /* Set speex/16000 to higher priority*/
90 codec_id = pj_str("speex/16000");
91 pjmedia_codec_mgr_set_codec_priority(
92 pjmedia_endpt_get_codec_mgr(pjsua_var.med_endpt),
93 &codec_id, PJMEDIA_CODEC_PRIO_NORMAL+2);
94
95 /* Set speex/8000 to next higher priority*/
96 codec_id = pj_str("speex/8000");
97 pjmedia_codec_mgr_set_codec_priority(
98 pjmedia_endpt_get_codec_mgr(pjsua_var.med_endpt),
99 &codec_id, PJMEDIA_CODEC_PRIO_NORMAL+1);
100
101
102
Benny Prijonoeebe9af2006-06-13 22:57:13 +0000103#endif /* PJMEDIA_HAS_SPEEX_CODEC */
104
Benny Prijono00cae612006-07-31 15:19:36 +0000105#if PJMEDIA_HAS_ILBC_CODEC
106 /* Register iLBC. */
107 status = pjmedia_codec_ilbc_init( pjsua_var.med_endpt,
108 pjsua_var.media_cfg.ilbc_mode);
109 if (status != PJ_SUCCESS) {
110 pjsua_perror(THIS_FILE, "Error initializing iLBC codec",
111 status);
112 return status;
113 }
114#endif /* PJMEDIA_HAS_ILBC_CODEC */
115
Benny Prijonoeebe9af2006-06-13 22:57:13 +0000116#if PJMEDIA_HAS_GSM_CODEC
117 /* Register GSM */
118 status = pjmedia_codec_gsm_init(pjsua_var.med_endpt);
119 if (status != PJ_SUCCESS) {
120 pjsua_perror(THIS_FILE, "Error initializing GSM codec",
121 status);
122 return status;
123 }
124#endif /* PJMEDIA_HAS_GSM_CODEC */
125
126#if PJMEDIA_HAS_G711_CODEC
127 /* Register PCMA and PCMU */
128 status = pjmedia_codec_g711_init(pjsua_var.med_endpt);
129 if (status != PJ_SUCCESS) {
130 pjsua_perror(THIS_FILE, "Error initializing G711 codec",
131 status);
132 return status;
133 }
134#endif /* PJMEDIA_HAS_G711_CODEC */
135
136#if PJMEDIA_HAS_L16_CODEC
137 /* Register L16 family codecs, but disable all */
138 status = pjmedia_codec_l16_init(pjsua_var.med_endpt, 0);
139 if (status != PJ_SUCCESS) {
140 pjsua_perror(THIS_FILE, "Error initializing L16 codecs",
141 status);
142 return status;
143 }
144
145 /* Disable ALL L16 codecs */
146 codec_id = pj_str("L16");
147 pjmedia_codec_mgr_set_codec_priority(
148 pjmedia_endpt_get_codec_mgr(pjsua_var.med_endpt),
149 &codec_id, PJMEDIA_CODEC_PRIO_DISABLED);
150
151#endif /* PJMEDIA_HAS_L16_CODEC */
152
153
154 /* Save additional conference bridge parameters for future
155 * reference.
156 */
157 pjsua_var.mconf_cfg.samples_per_frame = pjsua_var.media_cfg.clock_rate *
158 PTIME / 1000;
159 pjsua_var.mconf_cfg.channel_count = 1;
160 pjsua_var.mconf_cfg.bits_per_sample = 16;
161
Benny Prijono0498d902006-06-19 14:49:14 +0000162 /* Init options for conference bridge. */
163 opt = PJMEDIA_CONF_NO_DEVICE;
164 if (pjsua_var.media_cfg.quality >= 3 &&
Benny Prijono7ca96da2006-08-07 12:11:40 +0000165 pjsua_var.media_cfg.quality <= 4)
Benny Prijono0498d902006-06-19 14:49:14 +0000166 {
167 opt |= PJMEDIA_CONF_SMALL_FILTER;
168 }
169 else if (pjsua_var.media_cfg.quality < 3) {
170 opt |= PJMEDIA_CONF_USE_LINEAR;
171 }
172
173
Benny Prijonoeebe9af2006-06-13 22:57:13 +0000174 /* Init conference bridge. */
175 status = pjmedia_conf_create(pjsua_var.pool,
176 pjsua_var.media_cfg.max_media_ports,
177 pjsua_var.media_cfg.clock_rate,
178 pjsua_var.mconf_cfg.channel_count,
179 pjsua_var.mconf_cfg.samples_per_frame,
180 pjsua_var.mconf_cfg.bits_per_sample,
Benny Prijono0498d902006-06-19 14:49:14 +0000181 opt, &pjsua_var.mconf);
Benny Prijonoeebe9af2006-06-13 22:57:13 +0000182 if (status != PJ_SUCCESS) {
183 pjsua_perror(THIS_FILE,
184 "Media stack initialization has returned error",
185 status);
186 return status;
187 }
188
189 /* Create null port just in case user wants to use null sound. */
190 status = pjmedia_null_port_create(pjsua_var.pool,
191 pjsua_var.media_cfg.clock_rate,
192 pjsua_var.mconf_cfg.channel_count,
193 pjsua_var.mconf_cfg.samples_per_frame,
194 pjsua_var.mconf_cfg.bits_per_sample,
195 &pjsua_var.null_port);
196 PJ_ASSERT_RETURN(status == PJ_SUCCESS, status);
197
198 return PJ_SUCCESS;
199}
200
201
202/*
203 * Create RTP and RTCP socket pair, and possibly resolve their public
204 * address via STUN.
205 */
206static pj_status_t create_rtp_rtcp_sock(const pjsua_transport_config *cfg,
207 pjmedia_sock_info *skinfo)
208{
209 enum {
210 RTP_RETRY = 100
211 };
212 int i;
Benny Prijono0a5cad82006-09-26 13:21:02 +0000213 pj_sockaddr_in bound_addr;
Benny Prijonoeebe9af2006-06-13 22:57:13 +0000214 pj_sockaddr_in mapped_addr[2];
215 pj_status_t status = PJ_SUCCESS;
216 pj_sock_t sock[2];
217
Benny Prijonoc97608e2007-03-23 16:34:20 +0000218 /* Make sure STUN server resolution has completed */
219 status = pjsua_resolve_stun_server(PJ_TRUE);
220 if (status != PJ_SUCCESS) {
221 pjsua_perror(THIS_FILE, "Error resolving STUN server", status);
222 return status;
223 }
224
225
Benny Prijonode479562007-03-15 10:23:55 +0000226 if (next_rtp_port == 0)
227 next_rtp_port = (pj_uint16_t)cfg->port;
Benny Prijonoeebe9af2006-06-13 22:57:13 +0000228
229 for (i=0; i<2; ++i)
230 sock[i] = PJ_INVALID_SOCKET;
231
Benny Prijono0a5cad82006-09-26 13:21:02 +0000232 bound_addr.sin_addr.s_addr = PJ_INADDR_ANY;
233 if (cfg->bound_addr.slen) {
234 status = pj_sockaddr_in_set_str_addr(&bound_addr, &cfg->bound_addr);
235 if (status != PJ_SUCCESS) {
236 pjsua_perror(THIS_FILE, "Unable to resolve transport bind address",
237 status);
238 return status;
239 }
240 }
Benny Prijonoeebe9af2006-06-13 22:57:13 +0000241
242 /* Loop retry to bind RTP and RTCP sockets. */
Benny Prijonode479562007-03-15 10:23:55 +0000243 for (i=0; i<RTP_RETRY; ++i, next_rtp_port += 2) {
Benny Prijonoeebe9af2006-06-13 22:57:13 +0000244
245 /* Create and bind RTP socket. */
246 status = pj_sock_socket(PJ_AF_INET, PJ_SOCK_DGRAM, 0, &sock[0]);
247 if (status != PJ_SUCCESS) {
248 pjsua_perror(THIS_FILE, "socket() error", status);
249 return status;
250 }
251
Benny Prijono0a5cad82006-09-26 13:21:02 +0000252 status = pj_sock_bind_in(sock[0], bound_addr.sin_addr.s_addr,
Benny Prijonode479562007-03-15 10:23:55 +0000253 next_rtp_port);
Benny Prijonoeebe9af2006-06-13 22:57:13 +0000254 if (status != PJ_SUCCESS) {
255 pj_sock_close(sock[0]);
256 sock[0] = PJ_INVALID_SOCKET;
257 continue;
258 }
259
260 /* Create and bind RTCP socket. */
261 status = pj_sock_socket(PJ_AF_INET, PJ_SOCK_DGRAM, 0, &sock[1]);
262 if (status != PJ_SUCCESS) {
263 pjsua_perror(THIS_FILE, "socket() error", status);
264 pj_sock_close(sock[0]);
265 return status;
266 }
267
Benny Prijono0a5cad82006-09-26 13:21:02 +0000268 status = pj_sock_bind_in(sock[1], bound_addr.sin_addr.s_addr,
Benny Prijonode479562007-03-15 10:23:55 +0000269 (pj_uint16_t)(next_rtp_port+1));
Benny Prijonoeebe9af2006-06-13 22:57:13 +0000270 if (status != PJ_SUCCESS) {
271 pj_sock_close(sock[0]);
272 sock[0] = PJ_INVALID_SOCKET;
273
274 pj_sock_close(sock[1]);
275 sock[1] = PJ_INVALID_SOCKET;
276 continue;
277 }
278
279 /*
280 * If we're configured to use STUN, then find out the mapped address,
281 * and make sure that the mapped RTCP port is adjacent with the RTP.
282 */
Benny Prijonoc97608e2007-03-23 16:34:20 +0000283 if (pjsua_var.stun_srv.addr.sa_family != 0) {
284 char ip_addr[32];
285 pj_str_t stun_srv;
286
287 pj_ansi_strcpy(ip_addr,
288 pj_inet_ntoa(pjsua_var.stun_srv.ipv4.sin_addr));
289 stun_srv = pj_str(ip_addr);
290
Benny Prijono14c2b862007-02-21 00:40:05 +0000291 status=pjstun_get_mapped_addr(&pjsua_var.cp.factory, 2, sock,
Benny Prijonoaf09dc32007-04-22 12:48:30 +0000292 &stun_srv, pj_ntohs(pjsua_var.stun_srv.ipv4.sin_port),
293 &stun_srv, pj_ntohs(pjsua_var.stun_srv.ipv4.sin_port),
Benny Prijonoeebe9af2006-06-13 22:57:13 +0000294 mapped_addr);
295 if (status != PJ_SUCCESS) {
296 pjsua_perror(THIS_FILE, "STUN resolve error", status);
297 goto on_error;
298 }
299
300 if (pj_ntohs(mapped_addr[1].sin_port) ==
301 pj_ntohs(mapped_addr[0].sin_port)+1)
302 {
303 /* Success! */
304 break;
305 }
306
307 pj_sock_close(sock[0]);
308 sock[0] = PJ_INVALID_SOCKET;
309
310 pj_sock_close(sock[1]);
311 sock[1] = PJ_INVALID_SOCKET;
312
Benny Prijono0a5cad82006-09-26 13:21:02 +0000313 } else if (cfg->public_addr.slen) {
314
315 status = pj_sockaddr_in_init(&mapped_addr[0], &cfg->public_addr,
Benny Prijonode479562007-03-15 10:23:55 +0000316 (pj_uint16_t)next_rtp_port);
Benny Prijono0a5cad82006-09-26 13:21:02 +0000317 if (status != PJ_SUCCESS)
318 goto on_error;
319
320 status = pj_sockaddr_in_init(&mapped_addr[1], &cfg->public_addr,
Benny Prijonode479562007-03-15 10:23:55 +0000321 (pj_uint16_t)(next_rtp_port+1));
Benny Prijono0a5cad82006-09-26 13:21:02 +0000322 if (status != PJ_SUCCESS)
323 goto on_error;
324
325 break;
326
Benny Prijonoeebe9af2006-06-13 22:57:13 +0000327 } else {
Benny Prijono594e4c52006-09-14 18:51:01 +0000328 pj_in_addr addr;
Benny Prijonoeebe9af2006-06-13 22:57:13 +0000329
330 /* Get local IP address. */
Benny Prijono594e4c52006-09-14 18:51:01 +0000331 status = pj_gethostip(&addr);
332 if (status != PJ_SUCCESS)
Benny Prijonoeebe9af2006-06-13 22:57:13 +0000333 goto on_error;
Benny Prijonoeebe9af2006-06-13 22:57:13 +0000334
335 for (i=0; i<2; ++i)
Benny Prijono594e4c52006-09-14 18:51:01 +0000336 mapped_addr[i].sin_addr = addr;
Benny Prijonoeebe9af2006-06-13 22:57:13 +0000337
Benny Prijonode479562007-03-15 10:23:55 +0000338 mapped_addr[0].sin_port=pj_htons((pj_uint16_t)next_rtp_port);
339 mapped_addr[1].sin_port=pj_htons((pj_uint16_t)(next_rtp_port+1));
Benny Prijonoeebe9af2006-06-13 22:57:13 +0000340 break;
341 }
342 }
343
344 if (sock[0] == PJ_INVALID_SOCKET) {
345 PJ_LOG(1,(THIS_FILE,
346 "Unable to find appropriate RTP/RTCP ports combination"));
347 goto on_error;
348 }
349
350
351 skinfo->rtp_sock = sock[0];
352 pj_memcpy(&skinfo->rtp_addr_name,
353 &mapped_addr[0], sizeof(pj_sockaddr_in));
354
355 skinfo->rtcp_sock = sock[1];
356 pj_memcpy(&skinfo->rtcp_addr_name,
357 &mapped_addr[1], sizeof(pj_sockaddr_in));
358
359 PJ_LOG(4,(THIS_FILE, "RTP socket reachable at %s:%d",
360 pj_inet_ntoa(skinfo->rtp_addr_name.sin_addr),
361 pj_ntohs(skinfo->rtp_addr_name.sin_port)));
362 PJ_LOG(4,(THIS_FILE, "RTCP socket reachable at %s:%d",
363 pj_inet_ntoa(skinfo->rtcp_addr_name.sin_addr),
364 pj_ntohs(skinfo->rtcp_addr_name.sin_port)));
365
Benny Prijonode479562007-03-15 10:23:55 +0000366 next_rtp_port += 2;
Benny Prijonoeebe9af2006-06-13 22:57:13 +0000367 return PJ_SUCCESS;
368
369on_error:
370 for (i=0; i<2; ++i) {
371 if (sock[i] != PJ_INVALID_SOCKET)
372 pj_sock_close(sock[i]);
373 }
374 return status;
375}
376
377
378/*
379 * Start pjsua media subsystem.
380 */
381pj_status_t pjsua_media_subsys_start(void)
382{
383 pj_status_t status;
384
385 /* Create media for calls, if none is specified */
386 if (pjsua_var.calls[0].med_tp == NULL) {
387 pjsua_transport_config transport_cfg;
388
389 /* Create default transport config */
390 pjsua_transport_config_default(&transport_cfg);
391 transport_cfg.port = DEFAULT_RTP_PORT;
392
393 status = pjsua_media_transports_create(&transport_cfg);
394 if (status != PJ_SUCCESS)
395 return status;
396 }
397
398 /* Create sound port if none is created yet */
Benny Prijonoe909eac2006-07-27 22:04:56 +0000399 if (pjsua_var.snd_port==NULL && pjsua_var.null_snd==NULL &&
400 !pjsua_var.no_snd)
401 {
Benny Prijonoeebe9af2006-06-13 22:57:13 +0000402 status = pjsua_set_snd_dev(pjsua_var.cap_dev, pjsua_var.play_dev);
403 if (status != PJ_SUCCESS) {
Benny Prijonod639efa2007-04-05 22:45:06 +0000404 pjsua_perror(THIS_FILE, "Error opening sound device", status);
405 return status;
Benny Prijonoeebe9af2006-06-13 22:57:13 +0000406 }
407 }
408
409 return PJ_SUCCESS;
410}
411
412
413/*
414 * Destroy pjsua media subsystem.
415 */
416pj_status_t pjsua_media_subsys_destroy(void)
417{
418 unsigned i;
419
420 close_snd_dev();
421
422 if (pjsua_var.mconf) {
423 pjmedia_conf_destroy(pjsua_var.mconf);
424 pjsua_var.mconf = NULL;
425 }
426
427 if (pjsua_var.null_port) {
428 pjmedia_port_destroy(pjsua_var.null_port);
429 pjsua_var.null_port = NULL;
430 }
431
432 /* Destroy file players */
433 for (i=0; i<PJ_ARRAY_SIZE(pjsua_var.player); ++i) {
434 if (pjsua_var.player[i].port) {
435 pjmedia_port_destroy(pjsua_var.player[i].port);
436 pjsua_var.player[i].port = NULL;
437 }
438 }
439
440 /* Destroy file recorders */
441 for (i=0; i<PJ_ARRAY_SIZE(pjsua_var.recorder); ++i) {
442 if (pjsua_var.recorder[i].port) {
443 pjmedia_port_destroy(pjsua_var.recorder[i].port);
444 pjsua_var.recorder[i].port = NULL;
445 }
446 }
447
448 /* Close media transports */
Benny Prijono0875ae82006-12-26 00:11:48 +0000449 for (i=0; i<pjsua_var.ua_cfg.max_calls; ++i) {
Benny Prijonoeebe9af2006-06-13 22:57:13 +0000450 if (pjsua_var.calls[i].med_tp) {
451 (*pjsua_var.calls[i].med_tp->op->destroy)(pjsua_var.calls[i].med_tp);
452 pjsua_var.calls[i].med_tp = NULL;
453 }
454 }
455
456 /* Destroy media endpoint. */
457 if (pjsua_var.med_endpt) {
458
459 /* Shutdown all codecs: */
460# if PJMEDIA_HAS_SPEEX_CODEC
461 pjmedia_codec_speex_deinit();
462# endif /* PJMEDIA_HAS_SPEEX_CODEC */
463
464# if PJMEDIA_HAS_GSM_CODEC
465 pjmedia_codec_gsm_deinit();
466# endif /* PJMEDIA_HAS_GSM_CODEC */
467
468# if PJMEDIA_HAS_G711_CODEC
469 pjmedia_codec_g711_deinit();
470# endif /* PJMEDIA_HAS_G711_CODEC */
471
472# if PJMEDIA_HAS_L16_CODEC
473 pjmedia_codec_l16_deinit();
474# endif /* PJMEDIA_HAS_L16_CODEC */
475
476
477 pjmedia_endpt_destroy(pjsua_var.med_endpt);
478 pjsua_var.med_endpt = NULL;
Benny Prijonoeebe9af2006-06-13 22:57:13 +0000479
Benny Prijonoad2e0ca2007-04-29 12:31:51 +0000480 /* Deinitialize sound subsystem */
481 pjmedia_snd_deinit();
482 }
Benny Prijonoaf1bb1e2006-11-21 12:39:31 +0000483
Benny Prijonode479562007-03-15 10:23:55 +0000484 /* Reset RTP port */
485 next_rtp_port = 0;
486
Benny Prijonoeebe9af2006-06-13 22:57:13 +0000487 return PJ_SUCCESS;
488}
489
490
Benny Prijonoc97608e2007-03-23 16:34:20 +0000491/* Create normal UDP media transports */
492static pj_status_t create_udp_media_transports(pjsua_transport_config *cfg)
Benny Prijonoeebe9af2006-06-13 22:57:13 +0000493{
Benny Prijonoeebe9af2006-06-13 22:57:13 +0000494 unsigned i;
Benny Prijono617c5bc2007-04-02 19:51:21 +0000495 pjmedia_sock_info skinfo;
Benny Prijonoeebe9af2006-06-13 22:57:13 +0000496 pj_status_t status;
497
Benny Prijonoeebe9af2006-06-13 22:57:13 +0000498 /* Create each media transport */
499 for (i=0; i<pjsua_var.ua_cfg.max_calls; ++i) {
500
Benny Prijono617c5bc2007-04-02 19:51:21 +0000501 status = create_rtp_rtcp_sock(cfg, &skinfo);
Benny Prijonoeebe9af2006-06-13 22:57:13 +0000502 if (status != PJ_SUCCESS) {
503 pjsua_perror(THIS_FILE, "Unable to create RTP/RTCP socket",
504 status);
505 goto on_error;
506 }
507 status = pjmedia_transport_udp_attach(pjsua_var.med_endpt, NULL,
Benny Prijono617c5bc2007-04-02 19:51:21 +0000508 &skinfo, 0,
Benny Prijonoeebe9af2006-06-13 22:57:13 +0000509 &pjsua_var.calls[i].med_tp);
510 if (status != PJ_SUCCESS) {
511 pjsua_perror(THIS_FILE, "Unable to create media transport",
512 status);
513 goto on_error;
514 }
Benny Prijono00cae612006-07-31 15:19:36 +0000515
516 pjmedia_transport_udp_simulate_lost(pjsua_var.calls[i].med_tp,
517 PJMEDIA_DIR_ENCODING,
518 pjsua_var.media_cfg.tx_drop_pct);
519
520 pjmedia_transport_udp_simulate_lost(pjsua_var.calls[i].med_tp,
521 PJMEDIA_DIR_DECODING,
522 pjsua_var.media_cfg.rx_drop_pct);
523
Benny Prijonoeebe9af2006-06-13 22:57:13 +0000524 }
525
Benny Prijonoc97608e2007-03-23 16:34:20 +0000526 return PJ_SUCCESS;
527
528on_error:
529 for (i=0; i<pjsua_var.ua_cfg.max_calls; ++i) {
530 if (pjsua_var.calls[i].med_tp != NULL) {
531 pjmedia_transport_close(pjsua_var.calls[i].med_tp);
532 pjsua_var.calls[i].med_tp = NULL;
533 }
534 }
535
536 return status;
537}
538
539
540/* Create ICE media transports (when ice is enabled) */
541static pj_status_t create_ice_media_transports(pjsua_transport_config *cfg)
542{
543 unsigned i;
Benny Prijonob681a2f2007-03-25 18:44:51 +0000544 pj_sockaddr_in addr;
Benny Prijonoc97608e2007-03-23 16:34:20 +0000545 pj_status_t status;
546
Benny Prijonoda9785b2007-04-02 20:43:06 +0000547 /* Make sure STUN server resolution has completed */
548 status = pjsua_resolve_stun_server(PJ_TRUE);
549 if (status != PJ_SUCCESS) {
550 pjsua_perror(THIS_FILE, "Error resolving STUN server", status);
551 return status;
552 }
553
Benny Prijonob681a2f2007-03-25 18:44:51 +0000554 pj_sockaddr_in_init(&addr, 0, (pj_uint16_t)cfg->port);
555
Benny Prijonoc97608e2007-03-23 16:34:20 +0000556 /* Create each media transport */
557 for (i=0; i<pjsua_var.ua_cfg.max_calls; ++i) {
Benny Prijonoa6bd7582007-03-28 15:49:48 +0000558 pj_ice_strans_comp comp;
Benny Prijonob681a2f2007-03-25 18:44:51 +0000559 int next_port;
Benny Prijonoc97608e2007-03-23 16:34:20 +0000560
Benny Prijono7e0d52c2007-03-26 13:25:07 +0000561 status = pjmedia_ice_create(pjsua_var.med_endpt, NULL, 2,
Benny Prijonoc97608e2007-03-23 16:34:20 +0000562 &pjsua_var.stun_cfg,
563 &pjsua_var.calls[i].med_tp);
564 if (status != PJ_SUCCESS) {
565 pjsua_perror(THIS_FILE, "Unable to create ICE media transport",
566 status);
567 goto on_error;
568 }
569
Benny Prijonob681a2f2007-03-25 18:44:51 +0000570 status = pjmedia_ice_start_init(pjsua_var.calls[i].med_tp, 0, &addr,
571 &pjsua_var.stun_srv.ipv4, NULL);
Benny Prijonoc97608e2007-03-23 16:34:20 +0000572 if (status != PJ_SUCCESS) {
Benny Prijonob681a2f2007-03-25 18:44:51 +0000573 pjsua_perror(THIS_FILE, "Error starting ICE transport",
Benny Prijonoc97608e2007-03-23 16:34:20 +0000574 status);
575 goto on_error;
576 }
577
Benny Prijonob681a2f2007-03-25 18:44:51 +0000578 pjmedia_ice_get_comp(pjsua_var.calls[i].med_tp, 1, &comp);
579 next_port = pj_ntohs(comp.local_addr.ipv4.sin_port);
580 next_port += 2;
581 addr.sin_port = pj_htons((pj_uint16_t)next_port);
Benny Prijonoc97608e2007-03-23 16:34:20 +0000582 }
583
584 /* Wait until all ICE transports are ready */
585 for (i=0; i<pjsua_var.ua_cfg.max_calls; ++i) {
Benny Prijonoc97608e2007-03-23 16:34:20 +0000586
587 /* Wait until interface status is PJ_SUCCESS */
588 for (;;) {
Benny Prijonob681a2f2007-03-25 18:44:51 +0000589 status = pjmedia_ice_get_init_status(pjsua_var.calls[i].med_tp);
Benny Prijonoc97608e2007-03-23 16:34:20 +0000590 if (status == PJ_EPENDING)
591 pjsua_handle_events(100);
592 else
593 break;
594 }
595
596 if (status != PJ_SUCCESS) {
597 pjsua_perror(THIS_FILE,
598 "Error adding STUN address to ICE media transport",
599 status);
600 goto on_error;
601 }
602
Benny Prijonoc97608e2007-03-23 16:34:20 +0000603 }
Benny Prijonoeebe9af2006-06-13 22:57:13 +0000604
605 return PJ_SUCCESS;
606
607on_error:
608 for (i=0; i<pjsua_var.ua_cfg.max_calls; ++i) {
609 if (pjsua_var.calls[i].med_tp != NULL) {
Benny Prijonoc97608e2007-03-23 16:34:20 +0000610 pjmedia_transport_close(pjsua_var.calls[i].med_tp);
Benny Prijonoeebe9af2006-06-13 22:57:13 +0000611 pjsua_var.calls[i].med_tp = NULL;
612 }
613 }
614
Benny Prijonoc97608e2007-03-23 16:34:20 +0000615 return status;
616}
617
618
619/*
620 * Create UDP media transports for all the calls. This function creates
621 * one UDP media transport for each call.
622 */
623PJ_DEF(pj_status_t)
624pjsua_media_transports_create(const pjsua_transport_config *app_cfg)
625{
626 pjsua_transport_config cfg;
627 unsigned i;
628 pj_status_t status;
629
630
631 /* Make sure pjsua_init() has been called */
632 PJ_ASSERT_RETURN(pjsua_var.ua_cfg.max_calls>0, PJ_EINVALIDOP);
633
634 PJSUA_LOCK();
635
636 /* Delete existing media transports */
637 for (i=0; i<pjsua_var.ua_cfg.max_calls; ++i) {
638 if (pjsua_var.calls[i].med_tp != NULL) {
639 pjmedia_transport_close(pjsua_var.calls[i].med_tp);
640 pjsua_var.calls[i].med_tp = NULL;
641 }
642 }
643
644 /* Copy config */
645 pjsua_transport_config_dup(pjsua_var.pool, &cfg, app_cfg);
646
647 if (pjsua_var.media_cfg.enable_ice) {
648 status = create_ice_media_transports(&cfg);
649 } else {
650 status = create_udp_media_transports(&cfg);
651 }
652
653
Benny Prijonoeebe9af2006-06-13 22:57:13 +0000654 PJSUA_UNLOCK();
655
656 return status;
657}
658
659
Benny Prijonoc97608e2007-03-23 16:34:20 +0000660pj_status_t pjsua_media_channel_init(pjsua_call_id call_id,
661 pjsip_role_e role)
662{
663 pjsua_call *call = &pjsua_var.calls[call_id];
664
665 if (pjsua_var.media_cfg.enable_ice) {
Benny Prijonoa6bd7582007-03-28 15:49:48 +0000666 pj_ice_sess_role ice_role;
Benny Prijonoc97608e2007-03-23 16:34:20 +0000667 pj_status_t status;
668
Benny Prijonoa6bd7582007-03-28 15:49:48 +0000669 ice_role = (role==PJSIP_ROLE_UAC ? PJ_ICE_SESS_ROLE_CONTROLLING :
670 PJ_ICE_SESS_ROLE_CONTROLLED);
Benny Prijonoc97608e2007-03-23 16:34:20 +0000671
672 /* Restart ICE */
673 pjmedia_ice_stop_ice(call->med_tp);
674
675 status = pjmedia_ice_init_ice(call->med_tp, ice_role, NULL, NULL);
676 if (status != PJ_SUCCESS)
677 return status;
678 }
679
680 return PJ_SUCCESS;
681}
682
683pj_status_t pjsua_media_channel_create_sdp(pjsua_call_id call_id,
684 pj_pool_t *pool,
685 pjmedia_sdp_session **p_sdp)
686{
687 pjmedia_sdp_session *sdp;
Benny Prijono617c5bc2007-04-02 19:51:21 +0000688 pjmedia_sock_info skinfo;
Benny Prijonoc97608e2007-03-23 16:34:20 +0000689 pjsua_call *call = &pjsua_var.calls[call_id];
690 pj_status_t status;
691
Benny Prijono617c5bc2007-04-02 19:51:21 +0000692 /* Get media socket info */
693 pjmedia_transport_get_info(call->med_tp, &skinfo);
694
695 /* Create SDP */
Benny Prijonoc97608e2007-03-23 16:34:20 +0000696 status = pjmedia_endpt_create_sdp(pjsua_var.med_endpt, pool, 1,
Benny Prijono617c5bc2007-04-02 19:51:21 +0000697 &skinfo, &sdp);
Benny Prijonoc97608e2007-03-23 16:34:20 +0000698 if (status != PJ_SUCCESS)
699 goto on_error;
700
701 if (pjsua_var.media_cfg.enable_ice) {
702 status = pjmedia_ice_modify_sdp(call->med_tp, pool, sdp);
703 if (status != PJ_SUCCESS)
704 goto on_error;
705 }
706
707 *p_sdp = sdp;
708 return PJ_SUCCESS;
709
710on_error:
711 pjsua_media_channel_deinit(call_id);
712 return status;
713
714}
715
716
717static void stop_media_session(pjsua_call_id call_id)
718{
719 pjsua_call *call = &pjsua_var.calls[call_id];
720
721 if (call->conf_slot != PJSUA_INVALID_ID) {
722 pjmedia_conf_remove_port(pjsua_var.mconf, call->conf_slot);
723 call->conf_slot = PJSUA_INVALID_ID;
724 }
725
726 if (call->session) {
727 pjmedia_session_destroy(call->session);
728 call->session = NULL;
729
730 PJ_LOG(4,(THIS_FILE, "Media session for call %d is destroyed",
731 call_id));
732
733 }
734
735 call->media_st = PJSUA_CALL_MEDIA_NONE;
736}
737
738pj_status_t pjsua_media_channel_deinit(pjsua_call_id call_id)
739{
740 pjsua_call *call = &pjsua_var.calls[call_id];
741
742 stop_media_session(call_id);
743
744 if (pjsua_var.media_cfg.enable_ice) {
745 pjmedia_ice_stop_ice(call->med_tp);
746 }
747
748 return PJ_SUCCESS;
749}
750
751
752/*
753 * DTMF callback from the stream.
754 */
755static void dtmf_callback(pjmedia_stream *strm, void *user_data,
756 int digit)
757{
758 PJ_UNUSED_ARG(strm);
759
760 if (pjsua_var.ua_cfg.cb.on_dtmf_digit) {
761 pjsua_call_id call_id;
762
763 call_id = (pjsua_call_id)user_data;
764 pjsua_var.ua_cfg.cb.on_dtmf_digit(call_id, digit);
765 }
766}
767
768
769pj_status_t pjsua_media_channel_update(pjsua_call_id call_id,
Benny Prijonodbce2cf2007-03-28 16:24:00 +0000770 const pjmedia_sdp_session *local_sdp,
771 const pjmedia_sdp_session *remote_sdp)
Benny Prijonoc97608e2007-03-23 16:34:20 +0000772{
773 int prev_media_st = 0;
774 pjsua_call *call = &pjsua_var.calls[call_id];
775 pjmedia_session_info sess_info;
776 pjmedia_port *media_port;
777 pj_str_t port_name;
778 char tmp[PJSIP_MAX_URL_SIZE];
779 pj_status_t status;
780
781 /* Destroy existing media session, if any. */
782 prev_media_st = call->media_st;
783 stop_media_session(call->index);
784
785 /* Create media session info based on SDP parameters.
786 * We only support one stream per session at the moment
787 */
788 status = pjmedia_session_info_from_sdp( call->inv->dlg->pool,
789 pjsua_var.med_endpt,
790 1,&sess_info,
791 local_sdp, remote_sdp);
792 if (status != PJ_SUCCESS)
793 return status;
794
795
796 /* Check if media is put on-hold */
797 if (sess_info.stream_cnt == 0 ||
798 sess_info.stream_info[0].dir == PJMEDIA_DIR_NONE)
799 {
800
801 /* Determine who puts the call on-hold */
802 if (prev_media_st == PJSUA_CALL_MEDIA_ACTIVE) {
803 if (pjmedia_sdp_neg_was_answer_remote(call->inv->neg)) {
804 /* It was local who offer hold */
805 call->media_st = PJSUA_CALL_MEDIA_LOCAL_HOLD;
806 } else {
807 call->media_st = PJSUA_CALL_MEDIA_REMOTE_HOLD;
808 }
809 }
810
811 call->media_dir = PJMEDIA_DIR_NONE;
812
Benny Prijono667952e2007-04-02 19:27:54 +0000813 /* Shutdown ICE session */
814 if (pjsua_var.media_cfg.enable_ice) {
815 pjmedia_ice_stop_ice(call->med_tp);
816 }
817
Benny Prijonoc97608e2007-03-23 16:34:20 +0000818 /* No need because we need keepalive? */
819
820 } else {
821
822 /* Start ICE */
823 if (pjsua_var.media_cfg.enable_ice) {
824 status = pjmedia_ice_start_ice(call->med_tp, call->inv->pool,
Benny Prijonofd70c9b2007-03-27 11:00:38 +0000825 remote_sdp, 0);
Benny Prijonoc97608e2007-03-23 16:34:20 +0000826 if (status != PJ_SUCCESS)
827 return status;
828 }
829
830 /* Override ptime, if this option is specified. */
831 if (pjsua_var.media_cfg.ptime != 0) {
832 sess_info.stream_info[0].param->setting.frm_per_pkt = (pj_uint8_t)
833 (pjsua_var.media_cfg.ptime / sess_info.stream_info[0].param->info.frm_ptime);
834 if (sess_info.stream_info[0].param->setting.frm_per_pkt == 0)
835 sess_info.stream_info[0].param->setting.frm_per_pkt = 1;
836 }
837
838 /* Disable VAD, if this option is specified. */
839 if (pjsua_var.media_cfg.no_vad) {
840 sess_info.stream_info[0].param->setting.vad = 0;
841 }
842
843
844 /* Optionally, application may modify other stream settings here
845 * (such as jitter buffer parameters, codec ptime, etc.)
846 */
847 sess_info.stream_info[0].jb_init = pjsua_var.media_cfg.jb_init;
848 sess_info.stream_info[0].jb_min_pre = pjsua_var.media_cfg.jb_min_pre;
849 sess_info.stream_info[0].jb_max_pre = pjsua_var.media_cfg.jb_max_pre;
850 sess_info.stream_info[0].jb_max = pjsua_var.media_cfg.jb_max;
851
852 /* Create session based on session info. */
853 status = pjmedia_session_create( pjsua_var.med_endpt, &sess_info,
854 &call->med_tp,
855 call, &call->session );
856 if (status != PJ_SUCCESS) {
857 return status;
858 }
859
860 /* If DTMF callback is installed by application, install our
861 * callback to the session.
862 */
863 if (pjsua_var.ua_cfg.cb.on_dtmf_digit) {
864 pjmedia_session_set_dtmf_callback(call->session, 0,
865 &dtmf_callback,
866 (void*)(call->index));
867 }
868
869 /* Get the port interface of the first stream in the session.
870 * We need the port interface to add to the conference bridge.
871 */
872 pjmedia_session_get_port(call->session, 0, &media_port);
873
874
875 /*
876 * Add the call to conference bridge.
877 */
878 port_name.ptr = tmp;
879 port_name.slen = pjsip_uri_print(PJSIP_URI_IN_REQ_URI,
880 call->inv->dlg->remote.info->uri,
881 tmp, sizeof(tmp));
882 if (port_name.slen < 1) {
883 port_name = pj_str("call");
884 }
885 status = pjmedia_conf_add_port( pjsua_var.mconf, call->inv->pool,
886 media_port,
887 &port_name,
888 (unsigned*)&call->conf_slot);
889 if (status != PJ_SUCCESS) {
890 return status;
891 }
892
893 /* Call's media state is active */
894 call->media_st = PJSUA_CALL_MEDIA_ACTIVE;
895 call->media_dir = sess_info.stream_info[0].dir;
896 }
897
898 /* Print info. */
899 {
900 char info[80];
901 int info_len = 0;
902 unsigned i;
903
904 for (i=0; i<sess_info.stream_cnt; ++i) {
905 int len;
906 const char *dir;
907 pjmedia_stream_info *strm_info = &sess_info.stream_info[i];
908
909 switch (strm_info->dir) {
910 case PJMEDIA_DIR_NONE:
911 dir = "inactive";
912 break;
913 case PJMEDIA_DIR_ENCODING:
914 dir = "sendonly";
915 break;
916 case PJMEDIA_DIR_DECODING:
917 dir = "recvonly";
918 break;
919 case PJMEDIA_DIR_ENCODING_DECODING:
920 dir = "sendrecv";
921 break;
922 default:
923 dir = "unknown";
924 break;
925 }
926 len = pj_ansi_sprintf( info+info_len,
927 ", stream #%d: %.*s (%s)", i,
928 (int)strm_info->fmt.encoding_name.slen,
929 strm_info->fmt.encoding_name.ptr,
930 dir);
931 if (len > 0)
932 info_len += len;
933 }
934 PJ_LOG(4,(THIS_FILE,"Media updates%s", info));
935 }
936
937 return PJ_SUCCESS;
938}
939
940
Benny Prijonoeebe9af2006-06-13 22:57:13 +0000941/*
942 * Get maxinum number of conference ports.
943 */
944PJ_DEF(unsigned) pjsua_conf_get_max_ports(void)
945{
946 return pjsua_var.media_cfg.max_media_ports;
947}
948
949
950/*
951 * Get current number of active ports in the bridge.
952 */
953PJ_DEF(unsigned) pjsua_conf_get_active_ports(void)
954{
955 unsigned ports[256];
956 unsigned count = PJ_ARRAY_SIZE(ports);
957 pj_status_t status;
958
959 status = pjmedia_conf_enum_ports(pjsua_var.mconf, ports, &count);
960 if (status != PJ_SUCCESS)
961 count = 0;
962
963 return count;
964}
965
966
967/*
968 * Enumerate all conference ports.
969 */
970PJ_DEF(pj_status_t) pjsua_enum_conf_ports(pjsua_conf_port_id id[],
971 unsigned *count)
972{
973 return pjmedia_conf_enum_ports(pjsua_var.mconf, (unsigned*)id, count);
974}
975
976
977/*
978 * Get information about the specified conference port
979 */
980PJ_DEF(pj_status_t) pjsua_conf_get_port_info( pjsua_conf_port_id id,
981 pjsua_conf_port_info *info)
982{
983 pjmedia_conf_port_info cinfo;
Benny Prijono0498d902006-06-19 14:49:14 +0000984 unsigned i;
Benny Prijonoeebe9af2006-06-13 22:57:13 +0000985 pj_status_t status;
986
987 status = pjmedia_conf_get_port_info( pjsua_var.mconf, id, &cinfo);
988 if (status != PJ_SUCCESS)
989 return status;
990
Benny Prijonoac623b32006-07-03 15:19:31 +0000991 pj_bzero(info, sizeof(*info));
Benny Prijonoeebe9af2006-06-13 22:57:13 +0000992 info->slot_id = id;
993 info->name = cinfo.name;
994 info->clock_rate = cinfo.clock_rate;
995 info->channel_count = cinfo.channel_count;
996 info->samples_per_frame = cinfo.samples_per_frame;
997 info->bits_per_sample = cinfo.bits_per_sample;
998
999 /* Build array of listeners */
Benny Prijonoc78c3a32006-06-16 15:54:43 +00001000 info->listener_cnt = cinfo.listener_cnt;
1001 for (i=0; i<cinfo.listener_cnt; ++i) {
1002 info->listeners[i] = cinfo.listener_slots[i];
Benny Prijonoeebe9af2006-06-13 22:57:13 +00001003 }
1004
1005 return PJ_SUCCESS;
1006}
1007
1008
1009/*
Benny Prijonoe909eac2006-07-27 22:04:56 +00001010 * Add arbitrary media port to PJSUA's conference bridge.
1011 */
1012PJ_DEF(pj_status_t) pjsua_conf_add_port( pj_pool_t *pool,
1013 pjmedia_port *port,
1014 pjsua_conf_port_id *p_id)
1015{
1016 pj_status_t status;
1017
1018 status = pjmedia_conf_add_port(pjsua_var.mconf, pool,
1019 port, NULL, (unsigned*)p_id);
1020 if (status != PJ_SUCCESS) {
1021 if (p_id)
1022 *p_id = PJSUA_INVALID_ID;
1023 }
1024
1025 return status;
1026}
1027
1028
1029/*
1030 * Remove arbitrary slot from the conference bridge.
1031 */
1032PJ_DEF(pj_status_t) pjsua_conf_remove_port(pjsua_conf_port_id id)
1033{
1034 return pjmedia_conf_remove_port(pjsua_var.mconf, (unsigned)id);
1035}
1036
1037
1038/*
Benny Prijonoeebe9af2006-06-13 22:57:13 +00001039 * Establish unidirectional media flow from souce to sink.
1040 */
1041PJ_DEF(pj_status_t) pjsua_conf_connect( pjsua_conf_port_id source,
1042 pjsua_conf_port_id sink)
1043{
1044 return pjmedia_conf_connect_port(pjsua_var.mconf, source, sink, 0);
1045}
1046
1047
1048/*
1049 * Disconnect media flow from the source to destination port.
1050 */
1051PJ_DEF(pj_status_t) pjsua_conf_disconnect( pjsua_conf_port_id source,
1052 pjsua_conf_port_id sink)
1053{
1054 return pjmedia_conf_disconnect_port(pjsua_var.mconf, source, sink);
1055}
1056
1057
Benny Prijono6dd967c2006-12-26 02:27:14 +00001058/*
1059 * Adjust the signal level to be transmitted from the bridge to the
1060 * specified port by making it louder or quieter.
1061 */
1062PJ_DEF(pj_status_t) pjsua_conf_adjust_tx_level(pjsua_conf_port_id slot,
1063 float level)
1064{
1065 return pjmedia_conf_adjust_tx_level(pjsua_var.mconf, slot,
1066 (int)((level-1) * 128));
1067}
1068
1069/*
1070 * Adjust the signal level to be received from the specified port (to
1071 * the bridge) by making it louder or quieter.
1072 */
1073PJ_DEF(pj_status_t) pjsua_conf_adjust_rx_level(pjsua_conf_port_id slot,
1074 float level)
1075{
1076 return pjmedia_conf_adjust_rx_level(pjsua_var.mconf, slot,
1077 (int)((level-1) * 128));
1078}
1079
1080
1081/*
1082 * Get last signal level transmitted to or received from the specified port.
1083 */
1084PJ_DEF(pj_status_t) pjsua_conf_get_signal_level(pjsua_conf_port_id slot,
1085 unsigned *tx_level,
1086 unsigned *rx_level)
1087{
1088 return pjmedia_conf_get_signal_level(pjsua_var.mconf, slot,
1089 tx_level, rx_level);
1090}
1091
Benny Prijonoeebe9af2006-06-13 22:57:13 +00001092/*****************************************************************************
1093 * File player.
1094 */
1095
1096/*
1097 * Create a file player, and automatically connect this player to
1098 * the conference bridge.
1099 */
1100PJ_DEF(pj_status_t) pjsua_player_create( const pj_str_t *filename,
1101 unsigned options,
Benny Prijonoeebe9af2006-06-13 22:57:13 +00001102 pjsua_player_id *p_id)
1103{
1104 unsigned slot, file_id;
Benny Prijonoa66c3312007-01-21 23:12:40 +00001105 char path[PJ_MAXPATH];
Benny Prijonoeebe9af2006-06-13 22:57:13 +00001106 pjmedia_port *port;
1107 pj_status_t status;
1108
1109 if (pjsua_var.player_cnt >= PJ_ARRAY_SIZE(pjsua_var.player))
1110 return PJ_ETOOMANY;
1111
1112 PJSUA_LOCK();
1113
1114 for (file_id=0; file_id<PJ_ARRAY_SIZE(pjsua_var.player); ++file_id) {
1115 if (pjsua_var.player[file_id].port == NULL)
1116 break;
1117 }
1118
1119 if (file_id == PJ_ARRAY_SIZE(pjsua_var.player)) {
1120 /* This is unexpected */
1121 PJSUA_UNLOCK();
1122 pj_assert(0);
1123 return PJ_EBUG;
1124 }
1125
1126 pj_memcpy(path, filename->ptr, filename->slen);
1127 path[filename->slen] = '\0';
1128 status = pjmedia_wav_player_port_create(pjsua_var.pool, path,
1129 pjsua_var.mconf_cfg.samples_per_frame *
1130 1000 / pjsua_var.media_cfg.clock_rate,
Benny Prijono00cae612006-07-31 15:19:36 +00001131 options, 0, &port);
Benny Prijonoeebe9af2006-06-13 22:57:13 +00001132 if (status != PJ_SUCCESS) {
1133 PJSUA_UNLOCK();
1134 pjsua_perror(THIS_FILE, "Unable to open file for playback", status);
1135 return status;
1136 }
1137
1138 status = pjmedia_conf_add_port(pjsua_var.mconf, pjsua_var.pool,
1139 port, filename, &slot);
1140 if (status != PJ_SUCCESS) {
1141 pjmedia_port_destroy(port);
1142 PJSUA_UNLOCK();
Benny Prijono32e4f492007-01-24 00:44:26 +00001143 pjsua_perror(THIS_FILE, "Unable to add file to conference bridge",
1144 status);
Benny Prijonoeebe9af2006-06-13 22:57:13 +00001145 return status;
1146 }
1147
Benny Prijonoa66c3312007-01-21 23:12:40 +00001148 pjsua_var.player[file_id].type = 0;
Benny Prijonoeebe9af2006-06-13 22:57:13 +00001149 pjsua_var.player[file_id].port = port;
1150 pjsua_var.player[file_id].slot = slot;
1151
1152 if (p_id) *p_id = file_id;
1153
1154 ++pjsua_var.player_cnt;
1155
1156 PJSUA_UNLOCK();
1157 return PJ_SUCCESS;
1158}
1159
1160
1161/*
Benny Prijonoa66c3312007-01-21 23:12:40 +00001162 * Create a file playlist media port, and automatically add the port
1163 * to the conference bridge.
1164 */
1165PJ_DEF(pj_status_t) pjsua_playlist_create( const pj_str_t file_names[],
1166 unsigned file_count,
1167 const pj_str_t *label,
1168 unsigned options,
1169 pjsua_player_id *p_id)
1170{
1171 unsigned slot, file_id, ptime;
1172 pjmedia_port *port;
1173 pj_status_t status;
1174
1175 if (pjsua_var.player_cnt >= PJ_ARRAY_SIZE(pjsua_var.player))
1176 return PJ_ETOOMANY;
1177
1178 PJSUA_LOCK();
1179
1180 for (file_id=0; file_id<PJ_ARRAY_SIZE(pjsua_var.player); ++file_id) {
1181 if (pjsua_var.player[file_id].port == NULL)
1182 break;
1183 }
1184
1185 if (file_id == PJ_ARRAY_SIZE(pjsua_var.player)) {
1186 /* This is unexpected */
1187 PJSUA_UNLOCK();
1188 pj_assert(0);
1189 return PJ_EBUG;
1190 }
1191
1192
1193 ptime = pjsua_var.mconf_cfg.samples_per_frame * 1000 /
1194 pjsua_var.media_cfg.clock_rate;
1195
1196 status = pjmedia_wav_playlist_create(pjsua_var.pool, label,
1197 file_names, file_count,
1198 ptime, options, 0, &port);
1199 if (status != PJ_SUCCESS) {
1200 PJSUA_UNLOCK();
1201 pjsua_perror(THIS_FILE, "Unable to create playlist", status);
1202 return status;
1203 }
1204
1205 status = pjmedia_conf_add_port(pjsua_var.mconf, pjsua_var.pool,
1206 port, &port->info.name, &slot);
1207 if (status != PJ_SUCCESS) {
1208 pjmedia_port_destroy(port);
1209 PJSUA_UNLOCK();
1210 pjsua_perror(THIS_FILE, "Unable to add port", status);
1211 return status;
1212 }
1213
1214 pjsua_var.player[file_id].type = 1;
1215 pjsua_var.player[file_id].port = port;
1216 pjsua_var.player[file_id].slot = slot;
1217
1218 if (p_id) *p_id = file_id;
1219
1220 ++pjsua_var.player_cnt;
1221
1222 PJSUA_UNLOCK();
1223 return PJ_SUCCESS;
1224
1225}
1226
1227
1228/*
Benny Prijonoeebe9af2006-06-13 22:57:13 +00001229 * Get conference port ID associated with player.
1230 */
1231PJ_DEF(pjsua_conf_port_id) pjsua_player_get_conf_port(pjsua_player_id id)
1232{
1233 PJ_ASSERT_RETURN(id>=0 && id<PJ_ARRAY_SIZE(pjsua_var.player), PJ_EINVAL);
1234 PJ_ASSERT_RETURN(pjsua_var.player[id].port != NULL, PJ_EINVAL);
1235
1236 return pjsua_var.player[id].slot;
1237}
1238
Benny Prijono469b1522006-12-26 03:05:17 +00001239/*
1240 * Get the media port for the player.
1241 */
1242PJ_DEF(pj_status_t) pjsua_player_get_port( pjsua_recorder_id id,
1243 pjmedia_port **p_port)
1244{
1245 PJ_ASSERT_RETURN(id>=0 && id<PJ_ARRAY_SIZE(pjsua_var.player), PJ_EINVAL);
1246 PJ_ASSERT_RETURN(pjsua_var.player[id].port != NULL, PJ_EINVAL);
1247 PJ_ASSERT_RETURN(p_port != NULL, PJ_EINVAL);
1248
1249 *p_port = pjsua_var.player[id].port;
1250
1251 return PJ_SUCCESS;
1252}
Benny Prijonoeebe9af2006-06-13 22:57:13 +00001253
1254/*
1255 * Set playback position.
1256 */
1257PJ_DEF(pj_status_t) pjsua_player_set_pos( pjsua_player_id id,
1258 pj_uint32_t samples)
1259{
1260 PJ_ASSERT_RETURN(id>=0 && id<PJ_ARRAY_SIZE(pjsua_var.player), PJ_EINVAL);
1261 PJ_ASSERT_RETURN(pjsua_var.player[id].port != NULL, PJ_EINVAL);
Benny Prijonoa66c3312007-01-21 23:12:40 +00001262 PJ_ASSERT_RETURN(pjsua_var.player[id].type == 0, PJ_EINVAL);
Benny Prijonoeebe9af2006-06-13 22:57:13 +00001263
1264 return pjmedia_wav_player_port_set_pos(pjsua_var.player[id].port, samples);
1265}
1266
1267
1268/*
1269 * Close the file, remove the player from the bridge, and free
1270 * resources associated with the file player.
1271 */
1272PJ_DEF(pj_status_t) pjsua_player_destroy(pjsua_player_id id)
1273{
1274 PJ_ASSERT_RETURN(id>=0 && id<PJ_ARRAY_SIZE(pjsua_var.player), PJ_EINVAL);
1275 PJ_ASSERT_RETURN(pjsua_var.player[id].port != NULL, PJ_EINVAL);
1276
1277 PJSUA_LOCK();
1278
1279 if (pjsua_var.player[id].port) {
1280 pjmedia_conf_remove_port(pjsua_var.mconf,
1281 pjsua_var.player[id].slot);
1282 pjmedia_port_destroy(pjsua_var.player[id].port);
1283 pjsua_var.player[id].port = NULL;
1284 pjsua_var.player[id].slot = 0xFFFF;
1285 pjsua_var.player_cnt--;
1286 }
1287
1288 PJSUA_UNLOCK();
1289
1290 return PJ_SUCCESS;
1291}
1292
1293
1294/*****************************************************************************
1295 * File recorder.
1296 */
1297
1298/*
1299 * Create a file recorder, and automatically connect this recorder to
1300 * the conference bridge.
1301 */
1302PJ_DEF(pj_status_t) pjsua_recorder_create( const pj_str_t *filename,
Benny Prijono8f310522006-10-20 11:08:49 +00001303 unsigned enc_type,
1304 void *enc_param,
Benny Prijonoeebe9af2006-06-13 22:57:13 +00001305 pj_ssize_t max_size,
1306 unsigned options,
Benny Prijonoeebe9af2006-06-13 22:57:13 +00001307 pjsua_recorder_id *p_id)
1308{
Benny Prijonob3cdb2b2006-10-19 15:49:47 +00001309 enum Format
1310 {
1311 FMT_UNKNOWN,
1312 FMT_WAV,
1313 FMT_MP3,
1314 };
Benny Prijonoeebe9af2006-06-13 22:57:13 +00001315 unsigned slot, file_id;
Benny Prijonoa66c3312007-01-21 23:12:40 +00001316 char path[PJ_MAXPATH];
Benny Prijonob3cdb2b2006-10-19 15:49:47 +00001317 pj_str_t ext;
Benny Prijono8f310522006-10-20 11:08:49 +00001318 int file_format;
Benny Prijonoeebe9af2006-06-13 22:57:13 +00001319 pjmedia_port *port;
1320 pj_status_t status;
1321
Benny Prijonob3cdb2b2006-10-19 15:49:47 +00001322 /* Filename must present */
1323 PJ_ASSERT_RETURN(filename != NULL, PJ_EINVAL);
1324
Benny Prijono00cae612006-07-31 15:19:36 +00001325 /* Don't support max_size at present */
Benny Prijonob3cdb2b2006-10-19 15:49:47 +00001326 PJ_ASSERT_RETURN(max_size == 0 || max_size == -1, PJ_EINVAL);
Benny Prijono00cae612006-07-31 15:19:36 +00001327
Benny Prijono8f310522006-10-20 11:08:49 +00001328 /* Don't support encoding type at present */
1329 PJ_ASSERT_RETURN(enc_type == 0, PJ_EINVAL);
Benny Prijono00cae612006-07-31 15:19:36 +00001330
Benny Prijonoeebe9af2006-06-13 22:57:13 +00001331 if (pjsua_var.rec_cnt >= PJ_ARRAY_SIZE(pjsua_var.recorder))
1332 return PJ_ETOOMANY;
1333
Benny Prijonob3cdb2b2006-10-19 15:49:47 +00001334 /* Determine the file format */
1335 ext.ptr = filename->ptr + filename->slen - 4;
1336 ext.slen = 4;
1337
1338 if (pj_stricmp2(&ext, ".wav") == 0)
1339 file_format = FMT_WAV;
1340 else if (pj_stricmp2(&ext, ".mp3") == 0)
1341 file_format = FMT_MP3;
1342 else {
1343 PJ_LOG(1,(THIS_FILE, "pjsua_recorder_create() error: unable to "
1344 "determine file format for %.*s",
1345 (int)filename->slen, filename->ptr));
1346 return PJ_ENOTSUP;
1347 }
1348
Benny Prijonoeebe9af2006-06-13 22:57:13 +00001349 PJSUA_LOCK();
1350
1351 for (file_id=0; file_id<PJ_ARRAY_SIZE(pjsua_var.recorder); ++file_id) {
1352 if (pjsua_var.recorder[file_id].port == NULL)
1353 break;
1354 }
1355
1356 if (file_id == PJ_ARRAY_SIZE(pjsua_var.recorder)) {
1357 /* This is unexpected */
1358 PJSUA_UNLOCK();
1359 pj_assert(0);
1360 return PJ_EBUG;
1361 }
1362
1363 pj_memcpy(path, filename->ptr, filename->slen);
1364 path[filename->slen] = '\0';
Benny Prijonob3cdb2b2006-10-19 15:49:47 +00001365
1366 if (file_format == FMT_WAV) {
1367 status = pjmedia_wav_writer_port_create(pjsua_var.pool, path,
1368 pjsua_var.media_cfg.clock_rate,
1369 pjsua_var.mconf_cfg.channel_count,
1370 pjsua_var.mconf_cfg.samples_per_frame,
1371 pjsua_var.mconf_cfg.bits_per_sample,
1372 options, 0, &port);
Benny Prijonob3cdb2b2006-10-19 15:49:47 +00001373 } else {
Benny Prijonoc95a0f02007-04-09 07:06:08 +00001374 PJ_UNUSED_ARG(enc_param);
Benny Prijonob3cdb2b2006-10-19 15:49:47 +00001375 port = NULL;
1376 status = PJ_ENOTSUP;
1377 }
1378
Benny Prijonoeebe9af2006-06-13 22:57:13 +00001379 if (status != PJ_SUCCESS) {
1380 PJSUA_UNLOCK();
1381 pjsua_perror(THIS_FILE, "Unable to open file for recording", status);
1382 return status;
1383 }
1384
1385 status = pjmedia_conf_add_port(pjsua_var.mconf, pjsua_var.pool,
1386 port, filename, &slot);
1387 if (status != PJ_SUCCESS) {
1388 pjmedia_port_destroy(port);
1389 PJSUA_UNLOCK();
1390 return status;
1391 }
1392
1393 pjsua_var.recorder[file_id].port = port;
1394 pjsua_var.recorder[file_id].slot = slot;
1395
1396 if (p_id) *p_id = file_id;
1397
1398 ++pjsua_var.rec_cnt;
1399
1400 PJSUA_UNLOCK();
1401 return PJ_SUCCESS;
1402}
1403
1404
1405/*
1406 * Get conference port associated with recorder.
1407 */
1408PJ_DEF(pjsua_conf_port_id) pjsua_recorder_get_conf_port(pjsua_recorder_id id)
1409{
1410 PJ_ASSERT_RETURN(id>=0 && id<PJ_ARRAY_SIZE(pjsua_var.recorder), PJ_EINVAL);
1411 PJ_ASSERT_RETURN(pjsua_var.recorder[id].port != NULL, PJ_EINVAL);
1412
1413 return pjsua_var.recorder[id].slot;
1414}
1415
Benny Prijono469b1522006-12-26 03:05:17 +00001416/*
1417 * Get the media port for the recorder.
1418 */
1419PJ_DEF(pj_status_t) pjsua_recorder_get_port( pjsua_recorder_id id,
1420 pjmedia_port **p_port)
1421{
1422 PJ_ASSERT_RETURN(id>=0 && id<PJ_ARRAY_SIZE(pjsua_var.recorder), PJ_EINVAL);
1423 PJ_ASSERT_RETURN(pjsua_var.recorder[id].port != NULL, PJ_EINVAL);
1424 PJ_ASSERT_RETURN(p_port != NULL, PJ_EINVAL);
1425
1426 *p_port = pjsua_var.recorder[id].port;
1427 return PJ_SUCCESS;
1428}
Benny Prijonoeebe9af2006-06-13 22:57:13 +00001429
1430/*
1431 * Destroy recorder (this will complete recording).
1432 */
1433PJ_DEF(pj_status_t) pjsua_recorder_destroy(pjsua_recorder_id id)
1434{
1435 PJ_ASSERT_RETURN(id>=0 && id<PJ_ARRAY_SIZE(pjsua_var.recorder), PJ_EINVAL);
1436 PJ_ASSERT_RETURN(pjsua_var.recorder[id].port != NULL, PJ_EINVAL);
1437
1438 PJSUA_LOCK();
1439
1440 if (pjsua_var.recorder[id].port) {
1441 pjmedia_conf_remove_port(pjsua_var.mconf,
1442 pjsua_var.recorder[id].slot);
1443 pjmedia_port_destroy(pjsua_var.recorder[id].port);
1444 pjsua_var.recorder[id].port = NULL;
1445 pjsua_var.recorder[id].slot = 0xFFFF;
1446 pjsua_var.rec_cnt--;
1447 }
1448
1449 PJSUA_UNLOCK();
1450
1451 return PJ_SUCCESS;
1452}
1453
1454
1455/*****************************************************************************
1456 * Sound devices.
1457 */
1458
1459/*
1460 * Enum sound devices.
1461 */
1462PJ_DEF(pj_status_t) pjsua_enum_snd_devs( pjmedia_snd_dev_info info[],
1463 unsigned *count)
1464{
1465 unsigned i, dev_count;
1466
1467 dev_count = pjmedia_snd_get_dev_count();
1468
1469 if (dev_count > *count) dev_count = *count;
1470
1471 for (i=0; i<dev_count; ++i) {
1472 const pjmedia_snd_dev_info *ci;
1473
1474 ci = pjmedia_snd_get_dev_info(i);
1475 pj_memcpy(&info[i], ci, sizeof(*ci));
1476 }
1477
1478 *count = dev_count;
1479
1480 return PJ_SUCCESS;
1481}
1482
1483
1484/* Close existing sound device */
1485static void close_snd_dev(void)
1486{
1487 /* Close sound device */
1488 if (pjsua_var.snd_port) {
1489 const pjmedia_snd_dev_info *cap_info, *play_info;
1490
1491 cap_info = pjmedia_snd_get_dev_info(pjsua_var.cap_dev);
1492 play_info = pjmedia_snd_get_dev_info(pjsua_var.play_dev);
1493
1494 PJ_LOG(4,(THIS_FILE, "Closing %s sound playback device and "
1495 "%s sound capture device",
1496 play_info->name, cap_info->name));
1497
1498 pjmedia_snd_port_disconnect(pjsua_var.snd_port);
1499 pjmedia_snd_port_destroy(pjsua_var.snd_port);
1500 pjsua_var.snd_port = NULL;
1501 }
1502
1503 /* Close null sound device */
1504 if (pjsua_var.null_snd) {
1505 PJ_LOG(4,(THIS_FILE, "Closing null sound device.."));
1506 pjmedia_master_port_destroy(pjsua_var.null_snd, PJ_FALSE);
1507 pjsua_var.null_snd = NULL;
1508 }
1509}
1510
1511/*
1512 * Select or change sound device. Application may call this function at
1513 * any time to replace current sound device.
1514 */
1515PJ_DEF(pj_status_t) pjsua_set_snd_dev( int capture_dev,
1516 int playback_dev)
1517{
1518 pjmedia_port *conf_port;
Benny Prijono6dd967c2006-12-26 02:27:14 +00001519 const pjmedia_snd_dev_info *play_info;
Benny Prijono26056d82006-10-11 16:03:41 +00001520 unsigned clock_rates[] = { 0, 22050, 44100, 48000, 11025, 32000, 8000};
Benny Prijono658a1c52006-10-11 21:56:16 +00001521 unsigned selected_clock_rate = 0;
Benny Prijono26056d82006-10-11 16:03:41 +00001522 unsigned i;
Benny Prijonoc53c6d72006-11-27 09:54:03 +00001523 pjmedia_snd_stream *strm;
1524 pjmedia_snd_stream_info si;
1525 pj_str_t tmp;
Benny Prijono26056d82006-10-11 16:03:41 +00001526 pj_status_t status = -1;
Benny Prijonoeebe9af2006-06-13 22:57:13 +00001527
1528 /* Close existing sound port */
1529 close_snd_dev();
1530
1531
Benny Prijono26056d82006-10-11 16:03:41 +00001532 /* Set default clock rate */
1533 clock_rates[0] = pjsua_var.media_cfg.clock_rate;
Benny Prijonoeebe9af2006-06-13 22:57:13 +00001534
Benny Prijono26056d82006-10-11 16:03:41 +00001535 /* Attempts to open the sound device with different clock rates */
1536 for (i=0; i<PJ_ARRAY_SIZE(clock_rates); ++i) {
1537 char errmsg[PJ_ERR_MSG_SIZE];
1538
1539 PJ_LOG(4,(THIS_FILE,
1540 "pjsua_set_snd_dev(): attempting to open devices "
1541 "@%d Hz", clock_rates[i]));
1542
1543 /* Create the sound device. Sound port will start immediately. */
1544 status = pjmedia_snd_port_create(pjsua_var.pool, capture_dev,
1545 playback_dev,
1546 clock_rates[i], 1,
1547 clock_rates[i]/FPS,
1548 16, 0, &pjsua_var.snd_port);
1549
Benny Prijono658a1c52006-10-11 21:56:16 +00001550 if (status == PJ_SUCCESS) {
1551 selected_clock_rate = clock_rates[i];
Benny Prijono26056d82006-10-11 16:03:41 +00001552 break;
Benny Prijono658a1c52006-10-11 21:56:16 +00001553 }
Benny Prijono26056d82006-10-11 16:03:41 +00001554
1555 pj_strerror(status, errmsg, sizeof(errmsg));
Benny Prijono658a1c52006-10-11 21:56:16 +00001556 PJ_LOG(4, (THIS_FILE, "..failed: %s", errmsg));
Benny Prijono26056d82006-10-11 16:03:41 +00001557 }
1558
Benny Prijonoeebe9af2006-06-13 22:57:13 +00001559 if (status != PJ_SUCCESS) {
1560 pjsua_perror(THIS_FILE, "Unable to open sound device", status);
1561 return status;
1562 }
1563
1564 /* Get the port0 of the conference bridge. */
1565 conf_port = pjmedia_conf_get_master_port(pjsua_var.mconf);
1566 pj_assert(conf_port != NULL);
1567
Benny Prijonof20687a2006-08-04 18:27:19 +00001568 /* Set AEC */
Benny Prijono5da50432006-08-07 10:24:52 +00001569 pjmedia_snd_port_set_ec( pjsua_var.snd_port, pjsua_var.pool,
1570 pjsua_var.media_cfg.ec_tail_len,
1571 pjsua_var.media_cfg.ec_options);
Benny Prijonoeebe9af2006-06-13 22:57:13 +00001572
Benny Prijono658a1c52006-10-11 21:56:16 +00001573 /* If there's mismatch between sound port and conference's port,
1574 * create a resample port to bridge them.
1575 */
1576 if (selected_clock_rate != pjsua_var.media_cfg.clock_rate) {
1577 pjmedia_port *resample_port;
1578
1579 status = pjmedia_resample_port_create(pjsua_var.pool, conf_port,
1580 selected_clock_rate, 0,
1581 &resample_port);
1582 if (status != PJ_SUCCESS) {
1583 pjsua_perror("Error creating resample port", THIS_FILE, status);
1584 return status;
1585 }
1586
1587 conf_port = resample_port;
1588 }
1589
Benny Prijono52a93912006-08-04 20:54:37 +00001590 /* Connect sound port to the bridge */
1591 status = pjmedia_snd_port_connect(pjsua_var.snd_port,
1592 conf_port );
1593 if (status != PJ_SUCCESS) {
1594 pjsua_perror(THIS_FILE, "Unable to connect conference port to "
1595 "sound device", status);
1596 pjmedia_snd_port_destroy(pjsua_var.snd_port);
1597 pjsua_var.snd_port = NULL;
1598 return status;
1599 }
1600
Benny Prijonoeebe9af2006-06-13 22:57:13 +00001601 /* Save the device IDs */
1602 pjsua_var.cap_dev = capture_dev;
1603 pjsua_var.play_dev = playback_dev;
1604
Benny Prijonoc53c6d72006-11-27 09:54:03 +00001605 /* Update sound device name. */
1606 strm = pjmedia_snd_port_get_snd_stream(pjsua_var.snd_port);
1607 pjmedia_snd_stream_get_info(strm, &si);
1608 play_info = pjmedia_snd_get_dev_info(si.rec_id);
1609
1610 pjmedia_conf_set_port0_name(pjsua_var.mconf,
1611 pj_cstr(&tmp, play_info->name));
1612
Benny Prijonoeebe9af2006-06-13 22:57:13 +00001613 return PJ_SUCCESS;
1614}
1615
1616
1617/*
Benny Prijonoebdf8772007-02-01 19:25:50 +00001618 * Get currently active sound devices. If sound devices has not been created
1619 * (for example when pjsua_start() is not called), it is possible that
1620 * the function returns PJ_SUCCESS with -1 as device IDs.
1621 */
1622PJ_DEF(pj_status_t) pjsua_get_snd_dev(int *capture_dev,
1623 int *playback_dev)
1624{
1625 if (capture_dev) {
1626 *capture_dev = pjsua_var.cap_dev;
1627 }
1628 if (playback_dev) {
1629 *playback_dev = pjsua_var.play_dev;
1630 }
1631
1632 return PJ_SUCCESS;
1633}
1634
1635
1636/*
Benny Prijonoeebe9af2006-06-13 22:57:13 +00001637 * Use null sound device.
1638 */
1639PJ_DEF(pj_status_t) pjsua_set_null_snd_dev(void)
1640{
1641 pjmedia_port *conf_port;
1642 pj_status_t status;
1643
1644 /* Close existing sound device */
1645 close_snd_dev();
1646
1647 PJ_LOG(4,(THIS_FILE, "Opening null sound device.."));
1648
1649 /* Get the port0 of the conference bridge. */
1650 conf_port = pjmedia_conf_get_master_port(pjsua_var.mconf);
1651 pj_assert(conf_port != NULL);
1652
1653 /* Create master port, connecting port0 of the conference bridge to
1654 * a null port.
1655 */
1656 status = pjmedia_master_port_create(pjsua_var.pool, pjsua_var.null_port,
1657 conf_port, 0, &pjsua_var.null_snd);
1658 if (status != PJ_SUCCESS) {
1659 pjsua_perror(THIS_FILE, "Unable to create null sound device",
1660 status);
1661 return status;
1662 }
1663
1664 /* Start the master port */
1665 status = pjmedia_master_port_start(pjsua_var.null_snd);
1666 PJ_ASSERT_RETURN(status == PJ_SUCCESS, status);
1667
1668 return PJ_SUCCESS;
1669}
1670
1671
Benny Prijonoe909eac2006-07-27 22:04:56 +00001672
1673/*
1674 * Use no device!
1675 */
1676PJ_DEF(pjmedia_port*) pjsua_set_no_snd_dev(void)
1677{
1678 /* Close existing sound device */
1679 close_snd_dev();
1680
1681 pjsua_var.no_snd = PJ_TRUE;
1682 return pjmedia_conf_get_master_port(pjsua_var.mconf);
1683}
1684
1685
Benny Prijonof20687a2006-08-04 18:27:19 +00001686/*
1687 * Configure the AEC settings of the sound port.
1688 */
Benny Prijono5da50432006-08-07 10:24:52 +00001689PJ_DEF(pj_status_t) pjsua_set_ec(unsigned tail_ms, unsigned options)
Benny Prijonof20687a2006-08-04 18:27:19 +00001690{
1691 pjsua_var.media_cfg.ec_tail_len = tail_ms;
1692
1693 if (pjsua_var.snd_port)
Benny Prijono5da50432006-08-07 10:24:52 +00001694 return pjmedia_snd_port_set_ec( pjsua_var.snd_port, pjsua_var.pool,
1695 tail_ms, options);
Benny Prijonof20687a2006-08-04 18:27:19 +00001696
1697 return PJ_SUCCESS;
1698}
1699
1700
1701/*
1702 * Get current AEC tail length.
1703 */
Benny Prijono22dfe592006-08-06 12:07:13 +00001704PJ_DEF(pj_status_t) pjsua_get_ec_tail(unsigned *p_tail_ms)
Benny Prijonof20687a2006-08-04 18:27:19 +00001705{
1706 *p_tail_ms = pjsua_var.media_cfg.ec_tail_len;
1707 return PJ_SUCCESS;
1708}
1709
Benny Prijonoe909eac2006-07-27 22:04:56 +00001710
Benny Prijonoeebe9af2006-06-13 22:57:13 +00001711/*****************************************************************************
1712 * Codecs.
1713 */
1714
1715/*
1716 * Enum all supported codecs in the system.
1717 */
1718PJ_DEF(pj_status_t) pjsua_enum_codecs( pjsua_codec_info id[],
1719 unsigned *p_count )
1720{
1721 pjmedia_codec_mgr *codec_mgr;
1722 pjmedia_codec_info info[32];
1723 unsigned i, count, prio[32];
1724 pj_status_t status;
1725
1726 codec_mgr = pjmedia_endpt_get_codec_mgr(pjsua_var.med_endpt);
1727 count = PJ_ARRAY_SIZE(info);
1728 status = pjmedia_codec_mgr_enum_codecs( codec_mgr, &count, info, prio);
1729 if (status != PJ_SUCCESS) {
1730 *p_count = 0;
1731 return status;
1732 }
1733
1734 if (count > *p_count) count = *p_count;
1735
1736 for (i=0; i<count; ++i) {
1737 pjmedia_codec_info_to_id(&info[i], id[i].buf_, sizeof(id[i].buf_));
1738 id[i].codec_id = pj_str(id[i].buf_);
1739 id[i].priority = (pj_uint8_t) prio[i];
1740 }
1741
1742 *p_count = count;
1743
1744 return PJ_SUCCESS;
1745}
1746
1747
1748/*
1749 * Change codec priority.
1750 */
1751PJ_DEF(pj_status_t) pjsua_codec_set_priority( const pj_str_t *codec_id,
1752 pj_uint8_t priority )
1753{
1754 pjmedia_codec_mgr *codec_mgr;
1755
1756 codec_mgr = pjmedia_endpt_get_codec_mgr(pjsua_var.med_endpt);
1757
1758 return pjmedia_codec_mgr_set_codec_priority(codec_mgr, codec_id,
1759 priority);
1760}
1761
1762
1763/*
1764 * Get codec parameters.
1765 */
1766PJ_DEF(pj_status_t) pjsua_codec_get_param( const pj_str_t *codec_id,
1767 pjmedia_codec_param *param )
1768{
1769 const pjmedia_codec_info *info;
1770 pjmedia_codec_mgr *codec_mgr;
1771 unsigned count = 1;
1772 pj_status_t status;
1773
1774 codec_mgr = pjmedia_endpt_get_codec_mgr(pjsua_var.med_endpt);
1775
1776 status = pjmedia_codec_mgr_find_codecs_by_id(codec_mgr, codec_id,
1777 &count, &info, NULL);
1778 if (status != PJ_SUCCESS)
1779 return status;
1780
1781 if (count != 1)
1782 return PJ_ENOTFOUND;
1783
1784 status = pjmedia_codec_mgr_get_default_param( codec_mgr, info, param);
1785 return status;
1786}
1787
1788
1789/*
1790 * Set codec parameters.
1791 */
1792PJ_DEF(pj_status_t) pjsua_codec_set_param( const pj_str_t *id,
1793 const pjmedia_codec_param *param)
1794{
Benny Prijono00cae612006-07-31 15:19:36 +00001795 PJ_UNUSED_ARG(id);
1796 PJ_UNUSED_ARG(param);
Benny Prijonoeebe9af2006-06-13 22:57:13 +00001797 PJ_TODO(set_codec_param);
1798 return PJ_SUCCESS;
1799}