blob: 2c5b31b24eb46b98425cc4d6b6fdd3fa125880f8 [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{
Benny Prijonoba5926a2007-05-02 11:29:37 +000041 pj_str_t codec_id = {NULL, 0};
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 Prijono55e82352007-05-10 20:49:08 +0000692 /* Return error if media transport has not been created yet
693 * (e.g. application is starting)
694 */
695 if (call->med_tp == NULL) {
696 return PJ_EBUSY;
697 }
698
Benny Prijono617c5bc2007-04-02 19:51:21 +0000699 /* Get media socket info */
700 pjmedia_transport_get_info(call->med_tp, &skinfo);
701
702 /* Create SDP */
Benny Prijonoc97608e2007-03-23 16:34:20 +0000703 status = pjmedia_endpt_create_sdp(pjsua_var.med_endpt, pool, 1,
Benny Prijono617c5bc2007-04-02 19:51:21 +0000704 &skinfo, &sdp);
Benny Prijonoc97608e2007-03-23 16:34:20 +0000705 if (status != PJ_SUCCESS)
706 goto on_error;
707
708 if (pjsua_var.media_cfg.enable_ice) {
709 status = pjmedia_ice_modify_sdp(call->med_tp, pool, sdp);
710 if (status != PJ_SUCCESS)
711 goto on_error;
712 }
713
714 *p_sdp = sdp;
715 return PJ_SUCCESS;
716
717on_error:
718 pjsua_media_channel_deinit(call_id);
719 return status;
720
721}
722
723
724static void stop_media_session(pjsua_call_id call_id)
725{
726 pjsua_call *call = &pjsua_var.calls[call_id];
727
728 if (call->conf_slot != PJSUA_INVALID_ID) {
729 pjmedia_conf_remove_port(pjsua_var.mconf, call->conf_slot);
730 call->conf_slot = PJSUA_INVALID_ID;
731 }
732
733 if (call->session) {
734 pjmedia_session_destroy(call->session);
735 call->session = NULL;
736
737 PJ_LOG(4,(THIS_FILE, "Media session for call %d is destroyed",
738 call_id));
739
740 }
741
742 call->media_st = PJSUA_CALL_MEDIA_NONE;
743}
744
745pj_status_t pjsua_media_channel_deinit(pjsua_call_id call_id)
746{
747 pjsua_call *call = &pjsua_var.calls[call_id];
748
749 stop_media_session(call_id);
750
751 if (pjsua_var.media_cfg.enable_ice) {
752 pjmedia_ice_stop_ice(call->med_tp);
753 }
754
755 return PJ_SUCCESS;
756}
757
758
759/*
760 * DTMF callback from the stream.
761 */
762static void dtmf_callback(pjmedia_stream *strm, void *user_data,
763 int digit)
764{
765 PJ_UNUSED_ARG(strm);
766
767 if (pjsua_var.ua_cfg.cb.on_dtmf_digit) {
768 pjsua_call_id call_id;
769
770 call_id = (pjsua_call_id)user_data;
771 pjsua_var.ua_cfg.cb.on_dtmf_digit(call_id, digit);
772 }
773}
774
775
776pj_status_t pjsua_media_channel_update(pjsua_call_id call_id,
Benny Prijonodbce2cf2007-03-28 16:24:00 +0000777 const pjmedia_sdp_session *local_sdp,
778 const pjmedia_sdp_session *remote_sdp)
Benny Prijonoc97608e2007-03-23 16:34:20 +0000779{
780 int prev_media_st = 0;
781 pjsua_call *call = &pjsua_var.calls[call_id];
782 pjmedia_session_info sess_info;
783 pjmedia_port *media_port;
784 pj_str_t port_name;
785 char tmp[PJSIP_MAX_URL_SIZE];
786 pj_status_t status;
787
788 /* Destroy existing media session, if any. */
789 prev_media_st = call->media_st;
790 stop_media_session(call->index);
791
792 /* Create media session info based on SDP parameters.
793 * We only support one stream per session at the moment
794 */
795 status = pjmedia_session_info_from_sdp( call->inv->dlg->pool,
796 pjsua_var.med_endpt,
797 1,&sess_info,
798 local_sdp, remote_sdp);
799 if (status != PJ_SUCCESS)
800 return status;
801
802
803 /* Check if media is put on-hold */
804 if (sess_info.stream_cnt == 0 ||
805 sess_info.stream_info[0].dir == PJMEDIA_DIR_NONE)
806 {
807
808 /* Determine who puts the call on-hold */
809 if (prev_media_st == PJSUA_CALL_MEDIA_ACTIVE) {
810 if (pjmedia_sdp_neg_was_answer_remote(call->inv->neg)) {
811 /* It was local who offer hold */
812 call->media_st = PJSUA_CALL_MEDIA_LOCAL_HOLD;
813 } else {
814 call->media_st = PJSUA_CALL_MEDIA_REMOTE_HOLD;
815 }
816 }
817
818 call->media_dir = PJMEDIA_DIR_NONE;
819
Benny Prijono667952e2007-04-02 19:27:54 +0000820 /* Shutdown ICE session */
821 if (pjsua_var.media_cfg.enable_ice) {
822 pjmedia_ice_stop_ice(call->med_tp);
823 }
824
Benny Prijonoc97608e2007-03-23 16:34:20 +0000825 /* No need because we need keepalive? */
826
827 } else {
828
829 /* Start ICE */
830 if (pjsua_var.media_cfg.enable_ice) {
831 status = pjmedia_ice_start_ice(call->med_tp, call->inv->pool,
Benny Prijonofd70c9b2007-03-27 11:00:38 +0000832 remote_sdp, 0);
Benny Prijonoc97608e2007-03-23 16:34:20 +0000833 if (status != PJ_SUCCESS)
834 return status;
835 }
836
837 /* Override ptime, if this option is specified. */
838 if (pjsua_var.media_cfg.ptime != 0) {
839 sess_info.stream_info[0].param->setting.frm_per_pkt = (pj_uint8_t)
840 (pjsua_var.media_cfg.ptime / sess_info.stream_info[0].param->info.frm_ptime);
841 if (sess_info.stream_info[0].param->setting.frm_per_pkt == 0)
842 sess_info.stream_info[0].param->setting.frm_per_pkt = 1;
843 }
844
845 /* Disable VAD, if this option is specified. */
846 if (pjsua_var.media_cfg.no_vad) {
847 sess_info.stream_info[0].param->setting.vad = 0;
848 }
849
850
851 /* Optionally, application may modify other stream settings here
852 * (such as jitter buffer parameters, codec ptime, etc.)
853 */
854 sess_info.stream_info[0].jb_init = pjsua_var.media_cfg.jb_init;
855 sess_info.stream_info[0].jb_min_pre = pjsua_var.media_cfg.jb_min_pre;
856 sess_info.stream_info[0].jb_max_pre = pjsua_var.media_cfg.jb_max_pre;
857 sess_info.stream_info[0].jb_max = pjsua_var.media_cfg.jb_max;
858
859 /* Create session based on session info. */
860 status = pjmedia_session_create( pjsua_var.med_endpt, &sess_info,
861 &call->med_tp,
862 call, &call->session );
863 if (status != PJ_SUCCESS) {
864 return status;
865 }
866
867 /* If DTMF callback is installed by application, install our
868 * callback to the session.
869 */
870 if (pjsua_var.ua_cfg.cb.on_dtmf_digit) {
871 pjmedia_session_set_dtmf_callback(call->session, 0,
872 &dtmf_callback,
873 (void*)(call->index));
874 }
875
876 /* Get the port interface of the first stream in the session.
877 * We need the port interface to add to the conference bridge.
878 */
879 pjmedia_session_get_port(call->session, 0, &media_port);
880
881
882 /*
883 * Add the call to conference bridge.
884 */
885 port_name.ptr = tmp;
886 port_name.slen = pjsip_uri_print(PJSIP_URI_IN_REQ_URI,
887 call->inv->dlg->remote.info->uri,
888 tmp, sizeof(tmp));
889 if (port_name.slen < 1) {
890 port_name = pj_str("call");
891 }
892 status = pjmedia_conf_add_port( pjsua_var.mconf, call->inv->pool,
893 media_port,
894 &port_name,
895 (unsigned*)&call->conf_slot);
896 if (status != PJ_SUCCESS) {
897 return status;
898 }
899
900 /* Call's media state is active */
901 call->media_st = PJSUA_CALL_MEDIA_ACTIVE;
902 call->media_dir = sess_info.stream_info[0].dir;
903 }
904
905 /* Print info. */
906 {
907 char info[80];
908 int info_len = 0;
909 unsigned i;
910
911 for (i=0; i<sess_info.stream_cnt; ++i) {
912 int len;
913 const char *dir;
914 pjmedia_stream_info *strm_info = &sess_info.stream_info[i];
915
916 switch (strm_info->dir) {
917 case PJMEDIA_DIR_NONE:
918 dir = "inactive";
919 break;
920 case PJMEDIA_DIR_ENCODING:
921 dir = "sendonly";
922 break;
923 case PJMEDIA_DIR_DECODING:
924 dir = "recvonly";
925 break;
926 case PJMEDIA_DIR_ENCODING_DECODING:
927 dir = "sendrecv";
928 break;
929 default:
930 dir = "unknown";
931 break;
932 }
933 len = pj_ansi_sprintf( info+info_len,
934 ", stream #%d: %.*s (%s)", i,
935 (int)strm_info->fmt.encoding_name.slen,
936 strm_info->fmt.encoding_name.ptr,
937 dir);
938 if (len > 0)
939 info_len += len;
940 }
941 PJ_LOG(4,(THIS_FILE,"Media updates%s", info));
942 }
943
944 return PJ_SUCCESS;
945}
946
947
Benny Prijonoeebe9af2006-06-13 22:57:13 +0000948/*
949 * Get maxinum number of conference ports.
950 */
951PJ_DEF(unsigned) pjsua_conf_get_max_ports(void)
952{
953 return pjsua_var.media_cfg.max_media_ports;
954}
955
956
957/*
958 * Get current number of active ports in the bridge.
959 */
960PJ_DEF(unsigned) pjsua_conf_get_active_ports(void)
961{
962 unsigned ports[256];
963 unsigned count = PJ_ARRAY_SIZE(ports);
964 pj_status_t status;
965
966 status = pjmedia_conf_enum_ports(pjsua_var.mconf, ports, &count);
967 if (status != PJ_SUCCESS)
968 count = 0;
969
970 return count;
971}
972
973
974/*
975 * Enumerate all conference ports.
976 */
977PJ_DEF(pj_status_t) pjsua_enum_conf_ports(pjsua_conf_port_id id[],
978 unsigned *count)
979{
980 return pjmedia_conf_enum_ports(pjsua_var.mconf, (unsigned*)id, count);
981}
982
983
984/*
985 * Get information about the specified conference port
986 */
987PJ_DEF(pj_status_t) pjsua_conf_get_port_info( pjsua_conf_port_id id,
988 pjsua_conf_port_info *info)
989{
990 pjmedia_conf_port_info cinfo;
Benny Prijono0498d902006-06-19 14:49:14 +0000991 unsigned i;
Benny Prijonoeebe9af2006-06-13 22:57:13 +0000992 pj_status_t status;
993
994 status = pjmedia_conf_get_port_info( pjsua_var.mconf, id, &cinfo);
995 if (status != PJ_SUCCESS)
996 return status;
997
Benny Prijonoac623b32006-07-03 15:19:31 +0000998 pj_bzero(info, sizeof(*info));
Benny Prijonoeebe9af2006-06-13 22:57:13 +0000999 info->slot_id = id;
1000 info->name = cinfo.name;
1001 info->clock_rate = cinfo.clock_rate;
1002 info->channel_count = cinfo.channel_count;
1003 info->samples_per_frame = cinfo.samples_per_frame;
1004 info->bits_per_sample = cinfo.bits_per_sample;
1005
1006 /* Build array of listeners */
Benny Prijonoc78c3a32006-06-16 15:54:43 +00001007 info->listener_cnt = cinfo.listener_cnt;
1008 for (i=0; i<cinfo.listener_cnt; ++i) {
1009 info->listeners[i] = cinfo.listener_slots[i];
Benny Prijonoeebe9af2006-06-13 22:57:13 +00001010 }
1011
1012 return PJ_SUCCESS;
1013}
1014
1015
1016/*
Benny Prijonoe909eac2006-07-27 22:04:56 +00001017 * Add arbitrary media port to PJSUA's conference bridge.
1018 */
1019PJ_DEF(pj_status_t) pjsua_conf_add_port( pj_pool_t *pool,
1020 pjmedia_port *port,
1021 pjsua_conf_port_id *p_id)
1022{
1023 pj_status_t status;
1024
1025 status = pjmedia_conf_add_port(pjsua_var.mconf, pool,
1026 port, NULL, (unsigned*)p_id);
1027 if (status != PJ_SUCCESS) {
1028 if (p_id)
1029 *p_id = PJSUA_INVALID_ID;
1030 }
1031
1032 return status;
1033}
1034
1035
1036/*
1037 * Remove arbitrary slot from the conference bridge.
1038 */
1039PJ_DEF(pj_status_t) pjsua_conf_remove_port(pjsua_conf_port_id id)
1040{
1041 return pjmedia_conf_remove_port(pjsua_var.mconf, (unsigned)id);
1042}
1043
1044
1045/*
Benny Prijonoeebe9af2006-06-13 22:57:13 +00001046 * Establish unidirectional media flow from souce to sink.
1047 */
1048PJ_DEF(pj_status_t) pjsua_conf_connect( pjsua_conf_port_id source,
1049 pjsua_conf_port_id sink)
1050{
1051 return pjmedia_conf_connect_port(pjsua_var.mconf, source, sink, 0);
1052}
1053
1054
1055/*
1056 * Disconnect media flow from the source to destination port.
1057 */
1058PJ_DEF(pj_status_t) pjsua_conf_disconnect( pjsua_conf_port_id source,
1059 pjsua_conf_port_id sink)
1060{
1061 return pjmedia_conf_disconnect_port(pjsua_var.mconf, source, sink);
1062}
1063
1064
Benny Prijono6dd967c2006-12-26 02:27:14 +00001065/*
1066 * Adjust the signal level to be transmitted from the bridge to the
1067 * specified port by making it louder or quieter.
1068 */
1069PJ_DEF(pj_status_t) pjsua_conf_adjust_tx_level(pjsua_conf_port_id slot,
1070 float level)
1071{
1072 return pjmedia_conf_adjust_tx_level(pjsua_var.mconf, slot,
1073 (int)((level-1) * 128));
1074}
1075
1076/*
1077 * Adjust the signal level to be received from the specified port (to
1078 * the bridge) by making it louder or quieter.
1079 */
1080PJ_DEF(pj_status_t) pjsua_conf_adjust_rx_level(pjsua_conf_port_id slot,
1081 float level)
1082{
1083 return pjmedia_conf_adjust_rx_level(pjsua_var.mconf, slot,
1084 (int)((level-1) * 128));
1085}
1086
1087
1088/*
1089 * Get last signal level transmitted to or received from the specified port.
1090 */
1091PJ_DEF(pj_status_t) pjsua_conf_get_signal_level(pjsua_conf_port_id slot,
1092 unsigned *tx_level,
1093 unsigned *rx_level)
1094{
1095 return pjmedia_conf_get_signal_level(pjsua_var.mconf, slot,
1096 tx_level, rx_level);
1097}
1098
Benny Prijonoeebe9af2006-06-13 22:57:13 +00001099/*****************************************************************************
1100 * File player.
1101 */
1102
1103/*
1104 * Create a file player, and automatically connect this player to
1105 * the conference bridge.
1106 */
1107PJ_DEF(pj_status_t) pjsua_player_create( const pj_str_t *filename,
1108 unsigned options,
Benny Prijonoeebe9af2006-06-13 22:57:13 +00001109 pjsua_player_id *p_id)
1110{
1111 unsigned slot, file_id;
Benny Prijonoa66c3312007-01-21 23:12:40 +00001112 char path[PJ_MAXPATH];
Benny Prijonoeebe9af2006-06-13 22:57:13 +00001113 pjmedia_port *port;
1114 pj_status_t status;
1115
1116 if (pjsua_var.player_cnt >= PJ_ARRAY_SIZE(pjsua_var.player))
1117 return PJ_ETOOMANY;
1118
1119 PJSUA_LOCK();
1120
1121 for (file_id=0; file_id<PJ_ARRAY_SIZE(pjsua_var.player); ++file_id) {
1122 if (pjsua_var.player[file_id].port == NULL)
1123 break;
1124 }
1125
1126 if (file_id == PJ_ARRAY_SIZE(pjsua_var.player)) {
1127 /* This is unexpected */
1128 PJSUA_UNLOCK();
1129 pj_assert(0);
1130 return PJ_EBUG;
1131 }
1132
1133 pj_memcpy(path, filename->ptr, filename->slen);
1134 path[filename->slen] = '\0';
1135 status = pjmedia_wav_player_port_create(pjsua_var.pool, path,
1136 pjsua_var.mconf_cfg.samples_per_frame *
1137 1000 / pjsua_var.media_cfg.clock_rate,
Benny Prijono00cae612006-07-31 15:19:36 +00001138 options, 0, &port);
Benny Prijonoeebe9af2006-06-13 22:57:13 +00001139 if (status != PJ_SUCCESS) {
1140 PJSUA_UNLOCK();
1141 pjsua_perror(THIS_FILE, "Unable to open file for playback", status);
1142 return status;
1143 }
1144
1145 status = pjmedia_conf_add_port(pjsua_var.mconf, pjsua_var.pool,
1146 port, filename, &slot);
1147 if (status != PJ_SUCCESS) {
1148 pjmedia_port_destroy(port);
1149 PJSUA_UNLOCK();
Benny Prijono32e4f492007-01-24 00:44:26 +00001150 pjsua_perror(THIS_FILE, "Unable to add file to conference bridge",
1151 status);
Benny Prijonoeebe9af2006-06-13 22:57:13 +00001152 return status;
1153 }
1154
Benny Prijonoa66c3312007-01-21 23:12:40 +00001155 pjsua_var.player[file_id].type = 0;
Benny Prijonoeebe9af2006-06-13 22:57:13 +00001156 pjsua_var.player[file_id].port = port;
1157 pjsua_var.player[file_id].slot = slot;
1158
1159 if (p_id) *p_id = file_id;
1160
1161 ++pjsua_var.player_cnt;
1162
1163 PJSUA_UNLOCK();
1164 return PJ_SUCCESS;
1165}
1166
1167
1168/*
Benny Prijonoa66c3312007-01-21 23:12:40 +00001169 * Create a file playlist media port, and automatically add the port
1170 * to the conference bridge.
1171 */
1172PJ_DEF(pj_status_t) pjsua_playlist_create( const pj_str_t file_names[],
1173 unsigned file_count,
1174 const pj_str_t *label,
1175 unsigned options,
1176 pjsua_player_id *p_id)
1177{
1178 unsigned slot, file_id, ptime;
1179 pjmedia_port *port;
1180 pj_status_t status;
1181
1182 if (pjsua_var.player_cnt >= PJ_ARRAY_SIZE(pjsua_var.player))
1183 return PJ_ETOOMANY;
1184
1185 PJSUA_LOCK();
1186
1187 for (file_id=0; file_id<PJ_ARRAY_SIZE(pjsua_var.player); ++file_id) {
1188 if (pjsua_var.player[file_id].port == NULL)
1189 break;
1190 }
1191
1192 if (file_id == PJ_ARRAY_SIZE(pjsua_var.player)) {
1193 /* This is unexpected */
1194 PJSUA_UNLOCK();
1195 pj_assert(0);
1196 return PJ_EBUG;
1197 }
1198
1199
1200 ptime = pjsua_var.mconf_cfg.samples_per_frame * 1000 /
1201 pjsua_var.media_cfg.clock_rate;
1202
1203 status = pjmedia_wav_playlist_create(pjsua_var.pool, label,
1204 file_names, file_count,
1205 ptime, options, 0, &port);
1206 if (status != PJ_SUCCESS) {
1207 PJSUA_UNLOCK();
1208 pjsua_perror(THIS_FILE, "Unable to create playlist", status);
1209 return status;
1210 }
1211
1212 status = pjmedia_conf_add_port(pjsua_var.mconf, pjsua_var.pool,
1213 port, &port->info.name, &slot);
1214 if (status != PJ_SUCCESS) {
1215 pjmedia_port_destroy(port);
1216 PJSUA_UNLOCK();
1217 pjsua_perror(THIS_FILE, "Unable to add port", status);
1218 return status;
1219 }
1220
1221 pjsua_var.player[file_id].type = 1;
1222 pjsua_var.player[file_id].port = port;
1223 pjsua_var.player[file_id].slot = slot;
1224
1225 if (p_id) *p_id = file_id;
1226
1227 ++pjsua_var.player_cnt;
1228
1229 PJSUA_UNLOCK();
1230 return PJ_SUCCESS;
1231
1232}
1233
1234
1235/*
Benny Prijonoeebe9af2006-06-13 22:57:13 +00001236 * Get conference port ID associated with player.
1237 */
1238PJ_DEF(pjsua_conf_port_id) pjsua_player_get_conf_port(pjsua_player_id id)
1239{
Benny Prijonoa1e69682007-05-11 15:14:34 +00001240 PJ_ASSERT_RETURN(id>=0&&id<(int)PJ_ARRAY_SIZE(pjsua_var.player), PJ_EINVAL);
Benny Prijonoeebe9af2006-06-13 22:57:13 +00001241 PJ_ASSERT_RETURN(pjsua_var.player[id].port != NULL, PJ_EINVAL);
1242
1243 return pjsua_var.player[id].slot;
1244}
1245
Benny Prijono469b1522006-12-26 03:05:17 +00001246/*
1247 * Get the media port for the player.
1248 */
1249PJ_DEF(pj_status_t) pjsua_player_get_port( pjsua_recorder_id id,
1250 pjmedia_port **p_port)
1251{
Benny Prijonoa1e69682007-05-11 15:14:34 +00001252 PJ_ASSERT_RETURN(id>=0&&id<(int)PJ_ARRAY_SIZE(pjsua_var.player), PJ_EINVAL);
Benny Prijono469b1522006-12-26 03:05:17 +00001253 PJ_ASSERT_RETURN(pjsua_var.player[id].port != NULL, PJ_EINVAL);
1254 PJ_ASSERT_RETURN(p_port != NULL, PJ_EINVAL);
1255
1256 *p_port = pjsua_var.player[id].port;
1257
1258 return PJ_SUCCESS;
1259}
Benny Prijonoeebe9af2006-06-13 22:57:13 +00001260
1261/*
1262 * Set playback position.
1263 */
1264PJ_DEF(pj_status_t) pjsua_player_set_pos( pjsua_player_id id,
1265 pj_uint32_t samples)
1266{
Benny Prijonoa1e69682007-05-11 15:14:34 +00001267 PJ_ASSERT_RETURN(id>=0&&id<(int)PJ_ARRAY_SIZE(pjsua_var.player), PJ_EINVAL);
Benny Prijonoeebe9af2006-06-13 22:57:13 +00001268 PJ_ASSERT_RETURN(pjsua_var.player[id].port != NULL, PJ_EINVAL);
Benny Prijonoa66c3312007-01-21 23:12:40 +00001269 PJ_ASSERT_RETURN(pjsua_var.player[id].type == 0, PJ_EINVAL);
Benny Prijonoeebe9af2006-06-13 22:57:13 +00001270
1271 return pjmedia_wav_player_port_set_pos(pjsua_var.player[id].port, samples);
1272}
1273
1274
1275/*
1276 * Close the file, remove the player from the bridge, and free
1277 * resources associated with the file player.
1278 */
1279PJ_DEF(pj_status_t) pjsua_player_destroy(pjsua_player_id id)
1280{
Benny Prijonoa1e69682007-05-11 15:14:34 +00001281 PJ_ASSERT_RETURN(id>=0&&id<(int)PJ_ARRAY_SIZE(pjsua_var.player), PJ_EINVAL);
Benny Prijonoeebe9af2006-06-13 22:57:13 +00001282 PJ_ASSERT_RETURN(pjsua_var.player[id].port != NULL, PJ_EINVAL);
1283
1284 PJSUA_LOCK();
1285
1286 if (pjsua_var.player[id].port) {
1287 pjmedia_conf_remove_port(pjsua_var.mconf,
1288 pjsua_var.player[id].slot);
1289 pjmedia_port_destroy(pjsua_var.player[id].port);
1290 pjsua_var.player[id].port = NULL;
1291 pjsua_var.player[id].slot = 0xFFFF;
1292 pjsua_var.player_cnt--;
1293 }
1294
1295 PJSUA_UNLOCK();
1296
1297 return PJ_SUCCESS;
1298}
1299
1300
1301/*****************************************************************************
1302 * File recorder.
1303 */
1304
1305/*
1306 * Create a file recorder, and automatically connect this recorder to
1307 * the conference bridge.
1308 */
1309PJ_DEF(pj_status_t) pjsua_recorder_create( const pj_str_t *filename,
Benny Prijono8f310522006-10-20 11:08:49 +00001310 unsigned enc_type,
1311 void *enc_param,
Benny Prijonoeebe9af2006-06-13 22:57:13 +00001312 pj_ssize_t max_size,
1313 unsigned options,
Benny Prijonoeebe9af2006-06-13 22:57:13 +00001314 pjsua_recorder_id *p_id)
1315{
Benny Prijonob3cdb2b2006-10-19 15:49:47 +00001316 enum Format
1317 {
1318 FMT_UNKNOWN,
1319 FMT_WAV,
1320 FMT_MP3,
1321 };
Benny Prijonoeebe9af2006-06-13 22:57:13 +00001322 unsigned slot, file_id;
Benny Prijonoa66c3312007-01-21 23:12:40 +00001323 char path[PJ_MAXPATH];
Benny Prijonob3cdb2b2006-10-19 15:49:47 +00001324 pj_str_t ext;
Benny Prijono8f310522006-10-20 11:08:49 +00001325 int file_format;
Benny Prijonoeebe9af2006-06-13 22:57:13 +00001326 pjmedia_port *port;
1327 pj_status_t status;
1328
Benny Prijonob3cdb2b2006-10-19 15:49:47 +00001329 /* Filename must present */
1330 PJ_ASSERT_RETURN(filename != NULL, PJ_EINVAL);
1331
Benny Prijono00cae612006-07-31 15:19:36 +00001332 /* Don't support max_size at present */
Benny Prijonob3cdb2b2006-10-19 15:49:47 +00001333 PJ_ASSERT_RETURN(max_size == 0 || max_size == -1, PJ_EINVAL);
Benny Prijono00cae612006-07-31 15:19:36 +00001334
Benny Prijono8f310522006-10-20 11:08:49 +00001335 /* Don't support encoding type at present */
1336 PJ_ASSERT_RETURN(enc_type == 0, PJ_EINVAL);
Benny Prijono00cae612006-07-31 15:19:36 +00001337
Benny Prijonoeebe9af2006-06-13 22:57:13 +00001338 if (pjsua_var.rec_cnt >= PJ_ARRAY_SIZE(pjsua_var.recorder))
1339 return PJ_ETOOMANY;
1340
Benny Prijonob3cdb2b2006-10-19 15:49:47 +00001341 /* Determine the file format */
1342 ext.ptr = filename->ptr + filename->slen - 4;
1343 ext.slen = 4;
1344
1345 if (pj_stricmp2(&ext, ".wav") == 0)
1346 file_format = FMT_WAV;
1347 else if (pj_stricmp2(&ext, ".mp3") == 0)
1348 file_format = FMT_MP3;
1349 else {
1350 PJ_LOG(1,(THIS_FILE, "pjsua_recorder_create() error: unable to "
1351 "determine file format for %.*s",
1352 (int)filename->slen, filename->ptr));
1353 return PJ_ENOTSUP;
1354 }
1355
Benny Prijonoeebe9af2006-06-13 22:57:13 +00001356 PJSUA_LOCK();
1357
1358 for (file_id=0; file_id<PJ_ARRAY_SIZE(pjsua_var.recorder); ++file_id) {
1359 if (pjsua_var.recorder[file_id].port == NULL)
1360 break;
1361 }
1362
1363 if (file_id == PJ_ARRAY_SIZE(pjsua_var.recorder)) {
1364 /* This is unexpected */
1365 PJSUA_UNLOCK();
1366 pj_assert(0);
1367 return PJ_EBUG;
1368 }
1369
1370 pj_memcpy(path, filename->ptr, filename->slen);
1371 path[filename->slen] = '\0';
Benny Prijonob3cdb2b2006-10-19 15:49:47 +00001372
1373 if (file_format == FMT_WAV) {
1374 status = pjmedia_wav_writer_port_create(pjsua_var.pool, path,
1375 pjsua_var.media_cfg.clock_rate,
1376 pjsua_var.mconf_cfg.channel_count,
1377 pjsua_var.mconf_cfg.samples_per_frame,
1378 pjsua_var.mconf_cfg.bits_per_sample,
1379 options, 0, &port);
Benny Prijonob3cdb2b2006-10-19 15:49:47 +00001380 } else {
Benny Prijonoc95a0f02007-04-09 07:06:08 +00001381 PJ_UNUSED_ARG(enc_param);
Benny Prijonob3cdb2b2006-10-19 15:49:47 +00001382 port = NULL;
1383 status = PJ_ENOTSUP;
1384 }
1385
Benny Prijonoeebe9af2006-06-13 22:57:13 +00001386 if (status != PJ_SUCCESS) {
1387 PJSUA_UNLOCK();
1388 pjsua_perror(THIS_FILE, "Unable to open file for recording", status);
1389 return status;
1390 }
1391
1392 status = pjmedia_conf_add_port(pjsua_var.mconf, pjsua_var.pool,
1393 port, filename, &slot);
1394 if (status != PJ_SUCCESS) {
1395 pjmedia_port_destroy(port);
1396 PJSUA_UNLOCK();
1397 return status;
1398 }
1399
1400 pjsua_var.recorder[file_id].port = port;
1401 pjsua_var.recorder[file_id].slot = slot;
1402
1403 if (p_id) *p_id = file_id;
1404
1405 ++pjsua_var.rec_cnt;
1406
1407 PJSUA_UNLOCK();
1408 return PJ_SUCCESS;
1409}
1410
1411
1412/*
1413 * Get conference port associated with recorder.
1414 */
1415PJ_DEF(pjsua_conf_port_id) pjsua_recorder_get_conf_port(pjsua_recorder_id id)
1416{
Benny Prijonoa1e69682007-05-11 15:14:34 +00001417 PJ_ASSERT_RETURN(id>=0 && id<(int)PJ_ARRAY_SIZE(pjsua_var.recorder),
1418 PJ_EINVAL);
Benny Prijonoeebe9af2006-06-13 22:57:13 +00001419 PJ_ASSERT_RETURN(pjsua_var.recorder[id].port != NULL, PJ_EINVAL);
1420
1421 return pjsua_var.recorder[id].slot;
1422}
1423
Benny Prijono469b1522006-12-26 03:05:17 +00001424/*
1425 * Get the media port for the recorder.
1426 */
1427PJ_DEF(pj_status_t) pjsua_recorder_get_port( pjsua_recorder_id id,
1428 pjmedia_port **p_port)
1429{
Benny Prijonoa1e69682007-05-11 15:14:34 +00001430 PJ_ASSERT_RETURN(id>=0 && id<(int)PJ_ARRAY_SIZE(pjsua_var.recorder),
1431 PJ_EINVAL);
Benny Prijono469b1522006-12-26 03:05:17 +00001432 PJ_ASSERT_RETURN(pjsua_var.recorder[id].port != NULL, PJ_EINVAL);
1433 PJ_ASSERT_RETURN(p_port != NULL, PJ_EINVAL);
1434
1435 *p_port = pjsua_var.recorder[id].port;
1436 return PJ_SUCCESS;
1437}
Benny Prijonoeebe9af2006-06-13 22:57:13 +00001438
1439/*
1440 * Destroy recorder (this will complete recording).
1441 */
1442PJ_DEF(pj_status_t) pjsua_recorder_destroy(pjsua_recorder_id id)
1443{
Benny Prijonoa1e69682007-05-11 15:14:34 +00001444 PJ_ASSERT_RETURN(id>=0 && id<(int)PJ_ARRAY_SIZE(pjsua_var.recorder),
1445 PJ_EINVAL);
Benny Prijonoeebe9af2006-06-13 22:57:13 +00001446 PJ_ASSERT_RETURN(pjsua_var.recorder[id].port != NULL, PJ_EINVAL);
1447
1448 PJSUA_LOCK();
1449
1450 if (pjsua_var.recorder[id].port) {
1451 pjmedia_conf_remove_port(pjsua_var.mconf,
1452 pjsua_var.recorder[id].slot);
1453 pjmedia_port_destroy(pjsua_var.recorder[id].port);
1454 pjsua_var.recorder[id].port = NULL;
1455 pjsua_var.recorder[id].slot = 0xFFFF;
1456 pjsua_var.rec_cnt--;
1457 }
1458
1459 PJSUA_UNLOCK();
1460
1461 return PJ_SUCCESS;
1462}
1463
1464
1465/*****************************************************************************
1466 * Sound devices.
1467 */
1468
1469/*
1470 * Enum sound devices.
1471 */
1472PJ_DEF(pj_status_t) pjsua_enum_snd_devs( pjmedia_snd_dev_info info[],
1473 unsigned *count)
1474{
1475 unsigned i, dev_count;
1476
1477 dev_count = pjmedia_snd_get_dev_count();
1478
1479 if (dev_count > *count) dev_count = *count;
1480
1481 for (i=0; i<dev_count; ++i) {
1482 const pjmedia_snd_dev_info *ci;
1483
1484 ci = pjmedia_snd_get_dev_info(i);
1485 pj_memcpy(&info[i], ci, sizeof(*ci));
1486 }
1487
1488 *count = dev_count;
1489
1490 return PJ_SUCCESS;
1491}
1492
1493
1494/* Close existing sound device */
1495static void close_snd_dev(void)
1496{
1497 /* Close sound device */
1498 if (pjsua_var.snd_port) {
1499 const pjmedia_snd_dev_info *cap_info, *play_info;
1500
1501 cap_info = pjmedia_snd_get_dev_info(pjsua_var.cap_dev);
1502 play_info = pjmedia_snd_get_dev_info(pjsua_var.play_dev);
1503
1504 PJ_LOG(4,(THIS_FILE, "Closing %s sound playback device and "
1505 "%s sound capture device",
1506 play_info->name, cap_info->name));
1507
1508 pjmedia_snd_port_disconnect(pjsua_var.snd_port);
1509 pjmedia_snd_port_destroy(pjsua_var.snd_port);
1510 pjsua_var.snd_port = NULL;
1511 }
1512
1513 /* Close null sound device */
1514 if (pjsua_var.null_snd) {
1515 PJ_LOG(4,(THIS_FILE, "Closing null sound device.."));
1516 pjmedia_master_port_destroy(pjsua_var.null_snd, PJ_FALSE);
1517 pjsua_var.null_snd = NULL;
1518 }
1519}
1520
1521/*
1522 * Select or change sound device. Application may call this function at
1523 * any time to replace current sound device.
1524 */
1525PJ_DEF(pj_status_t) pjsua_set_snd_dev( int capture_dev,
1526 int playback_dev)
1527{
1528 pjmedia_port *conf_port;
Benny Prijono6dd967c2006-12-26 02:27:14 +00001529 const pjmedia_snd_dev_info *play_info;
Benny Prijono26056d82006-10-11 16:03:41 +00001530 unsigned clock_rates[] = { 0, 22050, 44100, 48000, 11025, 32000, 8000};
Benny Prijono658a1c52006-10-11 21:56:16 +00001531 unsigned selected_clock_rate = 0;
Benny Prijono26056d82006-10-11 16:03:41 +00001532 unsigned i;
Benny Prijonoc53c6d72006-11-27 09:54:03 +00001533 pjmedia_snd_stream *strm;
1534 pjmedia_snd_stream_info si;
1535 pj_str_t tmp;
Benny Prijono26056d82006-10-11 16:03:41 +00001536 pj_status_t status = -1;
Benny Prijonoeebe9af2006-06-13 22:57:13 +00001537
1538 /* Close existing sound port */
1539 close_snd_dev();
1540
1541
Benny Prijono26056d82006-10-11 16:03:41 +00001542 /* Set default clock rate */
1543 clock_rates[0] = pjsua_var.media_cfg.clock_rate;
Benny Prijonoeebe9af2006-06-13 22:57:13 +00001544
Benny Prijono26056d82006-10-11 16:03:41 +00001545 /* Attempts to open the sound device with different clock rates */
1546 for (i=0; i<PJ_ARRAY_SIZE(clock_rates); ++i) {
1547 char errmsg[PJ_ERR_MSG_SIZE];
1548
1549 PJ_LOG(4,(THIS_FILE,
1550 "pjsua_set_snd_dev(): attempting to open devices "
1551 "@%d Hz", clock_rates[i]));
1552
1553 /* Create the sound device. Sound port will start immediately. */
1554 status = pjmedia_snd_port_create(pjsua_var.pool, capture_dev,
1555 playback_dev,
1556 clock_rates[i], 1,
1557 clock_rates[i]/FPS,
1558 16, 0, &pjsua_var.snd_port);
1559
Benny Prijono658a1c52006-10-11 21:56:16 +00001560 if (status == PJ_SUCCESS) {
1561 selected_clock_rate = clock_rates[i];
Benny Prijono26056d82006-10-11 16:03:41 +00001562 break;
Benny Prijono658a1c52006-10-11 21:56:16 +00001563 }
Benny Prijono26056d82006-10-11 16:03:41 +00001564
1565 pj_strerror(status, errmsg, sizeof(errmsg));
Benny Prijono658a1c52006-10-11 21:56:16 +00001566 PJ_LOG(4, (THIS_FILE, "..failed: %s", errmsg));
Benny Prijono26056d82006-10-11 16:03:41 +00001567 }
1568
Benny Prijonoeebe9af2006-06-13 22:57:13 +00001569 if (status != PJ_SUCCESS) {
1570 pjsua_perror(THIS_FILE, "Unable to open sound device", status);
1571 return status;
1572 }
1573
1574 /* Get the port0 of the conference bridge. */
1575 conf_port = pjmedia_conf_get_master_port(pjsua_var.mconf);
1576 pj_assert(conf_port != NULL);
1577
Benny Prijonof20687a2006-08-04 18:27:19 +00001578 /* Set AEC */
Benny Prijono5da50432006-08-07 10:24:52 +00001579 pjmedia_snd_port_set_ec( pjsua_var.snd_port, pjsua_var.pool,
1580 pjsua_var.media_cfg.ec_tail_len,
1581 pjsua_var.media_cfg.ec_options);
Benny Prijonoeebe9af2006-06-13 22:57:13 +00001582
Benny Prijono658a1c52006-10-11 21:56:16 +00001583 /* If there's mismatch between sound port and conference's port,
1584 * create a resample port to bridge them.
1585 */
1586 if (selected_clock_rate != pjsua_var.media_cfg.clock_rate) {
1587 pjmedia_port *resample_port;
1588
1589 status = pjmedia_resample_port_create(pjsua_var.pool, conf_port,
1590 selected_clock_rate, 0,
1591 &resample_port);
1592 if (status != PJ_SUCCESS) {
1593 pjsua_perror("Error creating resample port", THIS_FILE, status);
1594 return status;
1595 }
1596
1597 conf_port = resample_port;
1598 }
1599
Benny Prijono52a93912006-08-04 20:54:37 +00001600 /* Connect sound port to the bridge */
1601 status = pjmedia_snd_port_connect(pjsua_var.snd_port,
1602 conf_port );
1603 if (status != PJ_SUCCESS) {
1604 pjsua_perror(THIS_FILE, "Unable to connect conference port to "
1605 "sound device", status);
1606 pjmedia_snd_port_destroy(pjsua_var.snd_port);
1607 pjsua_var.snd_port = NULL;
1608 return status;
1609 }
1610
Benny Prijonoeebe9af2006-06-13 22:57:13 +00001611 /* Save the device IDs */
1612 pjsua_var.cap_dev = capture_dev;
1613 pjsua_var.play_dev = playback_dev;
1614
Benny Prijonoc53c6d72006-11-27 09:54:03 +00001615 /* Update sound device name. */
1616 strm = pjmedia_snd_port_get_snd_stream(pjsua_var.snd_port);
1617 pjmedia_snd_stream_get_info(strm, &si);
1618 play_info = pjmedia_snd_get_dev_info(si.rec_id);
1619
1620 pjmedia_conf_set_port0_name(pjsua_var.mconf,
1621 pj_cstr(&tmp, play_info->name));
1622
Benny Prijonoeebe9af2006-06-13 22:57:13 +00001623 return PJ_SUCCESS;
1624}
1625
1626
1627/*
Benny Prijonoebdf8772007-02-01 19:25:50 +00001628 * Get currently active sound devices. If sound devices has not been created
1629 * (for example when pjsua_start() is not called), it is possible that
1630 * the function returns PJ_SUCCESS with -1 as device IDs.
1631 */
1632PJ_DEF(pj_status_t) pjsua_get_snd_dev(int *capture_dev,
1633 int *playback_dev)
1634{
1635 if (capture_dev) {
1636 *capture_dev = pjsua_var.cap_dev;
1637 }
1638 if (playback_dev) {
1639 *playback_dev = pjsua_var.play_dev;
1640 }
1641
1642 return PJ_SUCCESS;
1643}
1644
1645
1646/*
Benny Prijonoeebe9af2006-06-13 22:57:13 +00001647 * Use null sound device.
1648 */
1649PJ_DEF(pj_status_t) pjsua_set_null_snd_dev(void)
1650{
1651 pjmedia_port *conf_port;
1652 pj_status_t status;
1653
1654 /* Close existing sound device */
1655 close_snd_dev();
1656
1657 PJ_LOG(4,(THIS_FILE, "Opening null sound device.."));
1658
1659 /* Get the port0 of the conference bridge. */
1660 conf_port = pjmedia_conf_get_master_port(pjsua_var.mconf);
1661 pj_assert(conf_port != NULL);
1662
1663 /* Create master port, connecting port0 of the conference bridge to
1664 * a null port.
1665 */
1666 status = pjmedia_master_port_create(pjsua_var.pool, pjsua_var.null_port,
1667 conf_port, 0, &pjsua_var.null_snd);
1668 if (status != PJ_SUCCESS) {
1669 pjsua_perror(THIS_FILE, "Unable to create null sound device",
1670 status);
1671 return status;
1672 }
1673
1674 /* Start the master port */
1675 status = pjmedia_master_port_start(pjsua_var.null_snd);
1676 PJ_ASSERT_RETURN(status == PJ_SUCCESS, status);
1677
1678 return PJ_SUCCESS;
1679}
1680
1681
Benny Prijonoe909eac2006-07-27 22:04:56 +00001682
1683/*
1684 * Use no device!
1685 */
1686PJ_DEF(pjmedia_port*) pjsua_set_no_snd_dev(void)
1687{
1688 /* Close existing sound device */
1689 close_snd_dev();
1690
1691 pjsua_var.no_snd = PJ_TRUE;
1692 return pjmedia_conf_get_master_port(pjsua_var.mconf);
1693}
1694
1695
Benny Prijonof20687a2006-08-04 18:27:19 +00001696/*
1697 * Configure the AEC settings of the sound port.
1698 */
Benny Prijono5da50432006-08-07 10:24:52 +00001699PJ_DEF(pj_status_t) pjsua_set_ec(unsigned tail_ms, unsigned options)
Benny Prijonof20687a2006-08-04 18:27:19 +00001700{
1701 pjsua_var.media_cfg.ec_tail_len = tail_ms;
1702
1703 if (pjsua_var.snd_port)
Benny Prijono5da50432006-08-07 10:24:52 +00001704 return pjmedia_snd_port_set_ec( pjsua_var.snd_port, pjsua_var.pool,
1705 tail_ms, options);
Benny Prijonof20687a2006-08-04 18:27:19 +00001706
1707 return PJ_SUCCESS;
1708}
1709
1710
1711/*
1712 * Get current AEC tail length.
1713 */
Benny Prijono22dfe592006-08-06 12:07:13 +00001714PJ_DEF(pj_status_t) pjsua_get_ec_tail(unsigned *p_tail_ms)
Benny Prijonof20687a2006-08-04 18:27:19 +00001715{
1716 *p_tail_ms = pjsua_var.media_cfg.ec_tail_len;
1717 return PJ_SUCCESS;
1718}
1719
Benny Prijonoe909eac2006-07-27 22:04:56 +00001720
Benny Prijonoeebe9af2006-06-13 22:57:13 +00001721/*****************************************************************************
1722 * Codecs.
1723 */
1724
1725/*
1726 * Enum all supported codecs in the system.
1727 */
1728PJ_DEF(pj_status_t) pjsua_enum_codecs( pjsua_codec_info id[],
1729 unsigned *p_count )
1730{
1731 pjmedia_codec_mgr *codec_mgr;
1732 pjmedia_codec_info info[32];
1733 unsigned i, count, prio[32];
1734 pj_status_t status;
1735
1736 codec_mgr = pjmedia_endpt_get_codec_mgr(pjsua_var.med_endpt);
1737 count = PJ_ARRAY_SIZE(info);
1738 status = pjmedia_codec_mgr_enum_codecs( codec_mgr, &count, info, prio);
1739 if (status != PJ_SUCCESS) {
1740 *p_count = 0;
1741 return status;
1742 }
1743
1744 if (count > *p_count) count = *p_count;
1745
1746 for (i=0; i<count; ++i) {
1747 pjmedia_codec_info_to_id(&info[i], id[i].buf_, sizeof(id[i].buf_));
1748 id[i].codec_id = pj_str(id[i].buf_);
1749 id[i].priority = (pj_uint8_t) prio[i];
1750 }
1751
1752 *p_count = count;
1753
1754 return PJ_SUCCESS;
1755}
1756
1757
1758/*
1759 * Change codec priority.
1760 */
1761PJ_DEF(pj_status_t) pjsua_codec_set_priority( const pj_str_t *codec_id,
1762 pj_uint8_t priority )
1763{
1764 pjmedia_codec_mgr *codec_mgr;
1765
1766 codec_mgr = pjmedia_endpt_get_codec_mgr(pjsua_var.med_endpt);
1767
1768 return pjmedia_codec_mgr_set_codec_priority(codec_mgr, codec_id,
1769 priority);
1770}
1771
1772
1773/*
1774 * Get codec parameters.
1775 */
1776PJ_DEF(pj_status_t) pjsua_codec_get_param( const pj_str_t *codec_id,
1777 pjmedia_codec_param *param )
1778{
1779 const pjmedia_codec_info *info;
1780 pjmedia_codec_mgr *codec_mgr;
1781 unsigned count = 1;
1782 pj_status_t status;
1783
1784 codec_mgr = pjmedia_endpt_get_codec_mgr(pjsua_var.med_endpt);
1785
1786 status = pjmedia_codec_mgr_find_codecs_by_id(codec_mgr, codec_id,
1787 &count, &info, NULL);
1788 if (status != PJ_SUCCESS)
1789 return status;
1790
1791 if (count != 1)
1792 return PJ_ENOTFOUND;
1793
1794 status = pjmedia_codec_mgr_get_default_param( codec_mgr, info, param);
1795 return status;
1796}
1797
1798
1799/*
1800 * Set codec parameters.
1801 */
1802PJ_DEF(pj_status_t) pjsua_codec_set_param( const pj_str_t *id,
1803 const pjmedia_codec_param *param)
1804{
Benny Prijono00cae612006-07-31 15:19:36 +00001805 PJ_UNUSED_ARG(id);
1806 PJ_UNUSED_ARG(param);
Benny Prijonoeebe9af2006-06-13 22:57:13 +00001807 PJ_TODO(set_codec_param);
1808 return PJ_SUCCESS;
1809}