blob: e4f6ec14a723c2e29576216c8ac1233796e8eb9b [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 Prijonoc97608e2007-03-23 16:34:20 +0000292 &stun_srv, 3478,
293 &stun_srv, 3478,
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) {
404 /* Error opening sound device, use null device */
405 char errmsg[PJ_ERR_MSG_SIZE];
406
407 pj_strerror(status, errmsg, sizeof(errmsg));
408 PJ_LOG(4,(THIS_FILE,
409 "Error opening default sound device (%s (status=%d)). "
410 "Will use NULL device instead",
411 errmsg, status));
412
413 status = pjsua_set_null_snd_dev();
414 if (status != PJ_SUCCESS) {
415 pjsua_perror(THIS_FILE, "Error opening NULL sound device",
416 status);
417 return status;
418 }
419 }
420 }
421
422 return PJ_SUCCESS;
423}
424
425
426/*
427 * Destroy pjsua media subsystem.
428 */
429pj_status_t pjsua_media_subsys_destroy(void)
430{
431 unsigned i;
432
433 close_snd_dev();
434
435 if (pjsua_var.mconf) {
436 pjmedia_conf_destroy(pjsua_var.mconf);
437 pjsua_var.mconf = NULL;
438 }
439
440 if (pjsua_var.null_port) {
441 pjmedia_port_destroy(pjsua_var.null_port);
442 pjsua_var.null_port = NULL;
443 }
444
445 /* Destroy file players */
446 for (i=0; i<PJ_ARRAY_SIZE(pjsua_var.player); ++i) {
447 if (pjsua_var.player[i].port) {
448 pjmedia_port_destroy(pjsua_var.player[i].port);
449 pjsua_var.player[i].port = NULL;
450 }
451 }
452
453 /* Destroy file recorders */
454 for (i=0; i<PJ_ARRAY_SIZE(pjsua_var.recorder); ++i) {
455 if (pjsua_var.recorder[i].port) {
456 pjmedia_port_destroy(pjsua_var.recorder[i].port);
457 pjsua_var.recorder[i].port = NULL;
458 }
459 }
460
461 /* Close media transports */
Benny Prijono0875ae82006-12-26 00:11:48 +0000462 for (i=0; i<pjsua_var.ua_cfg.max_calls; ++i) {
Benny Prijonoeebe9af2006-06-13 22:57:13 +0000463 if (pjsua_var.calls[i].med_tp) {
464 (*pjsua_var.calls[i].med_tp->op->destroy)(pjsua_var.calls[i].med_tp);
465 pjsua_var.calls[i].med_tp = NULL;
466 }
467 }
468
469 /* Destroy media endpoint. */
470 if (pjsua_var.med_endpt) {
471
472 /* Shutdown all codecs: */
473# if PJMEDIA_HAS_SPEEX_CODEC
474 pjmedia_codec_speex_deinit();
475# endif /* PJMEDIA_HAS_SPEEX_CODEC */
476
477# if PJMEDIA_HAS_GSM_CODEC
478 pjmedia_codec_gsm_deinit();
479# endif /* PJMEDIA_HAS_GSM_CODEC */
480
481# if PJMEDIA_HAS_G711_CODEC
482 pjmedia_codec_g711_deinit();
483# endif /* PJMEDIA_HAS_G711_CODEC */
484
485# if PJMEDIA_HAS_L16_CODEC
486 pjmedia_codec_l16_deinit();
487# endif /* PJMEDIA_HAS_L16_CODEC */
488
489
490 pjmedia_endpt_destroy(pjsua_var.med_endpt);
491 pjsua_var.med_endpt = NULL;
492 }
493
Benny Prijonoaf1bb1e2006-11-21 12:39:31 +0000494 /* Deinitialize sound subsystem */
495 pjmedia_snd_deinit();
496
Benny Prijonode479562007-03-15 10:23:55 +0000497 /* Reset RTP port */
498 next_rtp_port = 0;
499
Benny Prijonoeebe9af2006-06-13 22:57:13 +0000500 return PJ_SUCCESS;
501}
502
503
Benny Prijonoc97608e2007-03-23 16:34:20 +0000504/* Create normal UDP media transports */
505static pj_status_t create_udp_media_transports(pjsua_transport_config *cfg)
Benny Prijonoeebe9af2006-06-13 22:57:13 +0000506{
Benny Prijonoeebe9af2006-06-13 22:57:13 +0000507 unsigned i;
Benny Prijono617c5bc2007-04-02 19:51:21 +0000508 pjmedia_sock_info skinfo;
Benny Prijonoeebe9af2006-06-13 22:57:13 +0000509 pj_status_t status;
510
Benny Prijonoeebe9af2006-06-13 22:57:13 +0000511 /* Create each media transport */
512 for (i=0; i<pjsua_var.ua_cfg.max_calls; ++i) {
513
Benny Prijono617c5bc2007-04-02 19:51:21 +0000514 status = create_rtp_rtcp_sock(cfg, &skinfo);
Benny Prijonoeebe9af2006-06-13 22:57:13 +0000515 if (status != PJ_SUCCESS) {
516 pjsua_perror(THIS_FILE, "Unable to create RTP/RTCP socket",
517 status);
518 goto on_error;
519 }
520 status = pjmedia_transport_udp_attach(pjsua_var.med_endpt, NULL,
Benny Prijono617c5bc2007-04-02 19:51:21 +0000521 &skinfo, 0,
Benny Prijonoeebe9af2006-06-13 22:57:13 +0000522 &pjsua_var.calls[i].med_tp);
523 if (status != PJ_SUCCESS) {
524 pjsua_perror(THIS_FILE, "Unable to create media transport",
525 status);
526 goto on_error;
527 }
Benny Prijono00cae612006-07-31 15:19:36 +0000528
529 pjmedia_transport_udp_simulate_lost(pjsua_var.calls[i].med_tp,
530 PJMEDIA_DIR_ENCODING,
531 pjsua_var.media_cfg.tx_drop_pct);
532
533 pjmedia_transport_udp_simulate_lost(pjsua_var.calls[i].med_tp,
534 PJMEDIA_DIR_DECODING,
535 pjsua_var.media_cfg.rx_drop_pct);
536
Benny Prijonoeebe9af2006-06-13 22:57:13 +0000537 }
538
Benny Prijonoc97608e2007-03-23 16:34:20 +0000539 return PJ_SUCCESS;
540
541on_error:
542 for (i=0; i<pjsua_var.ua_cfg.max_calls; ++i) {
543 if (pjsua_var.calls[i].med_tp != NULL) {
544 pjmedia_transport_close(pjsua_var.calls[i].med_tp);
545 pjsua_var.calls[i].med_tp = NULL;
546 }
547 }
548
549 return status;
550}
551
552
553/* Create ICE media transports (when ice is enabled) */
554static pj_status_t create_ice_media_transports(pjsua_transport_config *cfg)
555{
556 unsigned i;
Benny Prijonob681a2f2007-03-25 18:44:51 +0000557 pj_sockaddr_in addr;
Benny Prijonoc97608e2007-03-23 16:34:20 +0000558 pj_status_t status;
559
Benny Prijonob681a2f2007-03-25 18:44:51 +0000560 pj_sockaddr_in_init(&addr, 0, (pj_uint16_t)cfg->port);
561
Benny Prijonoc97608e2007-03-23 16:34:20 +0000562 /* Create each media transport */
563 for (i=0; i<pjsua_var.ua_cfg.max_calls; ++i) {
Benny Prijonoa6bd7582007-03-28 15:49:48 +0000564 pj_ice_strans_comp comp;
Benny Prijonob681a2f2007-03-25 18:44:51 +0000565 int next_port;
Benny Prijonoc97608e2007-03-23 16:34:20 +0000566
Benny Prijono7e0d52c2007-03-26 13:25:07 +0000567 status = pjmedia_ice_create(pjsua_var.med_endpt, NULL, 2,
Benny Prijonoc97608e2007-03-23 16:34:20 +0000568 &pjsua_var.stun_cfg,
569 &pjsua_var.calls[i].med_tp);
570 if (status != PJ_SUCCESS) {
571 pjsua_perror(THIS_FILE, "Unable to create ICE media transport",
572 status);
573 goto on_error;
574 }
575
Benny Prijonob681a2f2007-03-25 18:44:51 +0000576 status = pjmedia_ice_start_init(pjsua_var.calls[i].med_tp, 0, &addr,
577 &pjsua_var.stun_srv.ipv4, NULL);
Benny Prijonoc97608e2007-03-23 16:34:20 +0000578 if (status != PJ_SUCCESS) {
Benny Prijonob681a2f2007-03-25 18:44:51 +0000579 pjsua_perror(THIS_FILE, "Error starting ICE transport",
Benny Prijonoc97608e2007-03-23 16:34:20 +0000580 status);
581 goto on_error;
582 }
583
Benny Prijonob681a2f2007-03-25 18:44:51 +0000584 pjmedia_ice_get_comp(pjsua_var.calls[i].med_tp, 1, &comp);
585 next_port = pj_ntohs(comp.local_addr.ipv4.sin_port);
586 next_port += 2;
587 addr.sin_port = pj_htons((pj_uint16_t)next_port);
Benny Prijonoc97608e2007-03-23 16:34:20 +0000588 }
589
590 /* Wait until all ICE transports are ready */
591 for (i=0; i<pjsua_var.ua_cfg.max_calls; ++i) {
Benny Prijonoc97608e2007-03-23 16:34:20 +0000592
593 /* Wait until interface status is PJ_SUCCESS */
594 for (;;) {
Benny Prijonob681a2f2007-03-25 18:44:51 +0000595 status = pjmedia_ice_get_init_status(pjsua_var.calls[i].med_tp);
Benny Prijonoc97608e2007-03-23 16:34:20 +0000596 if (status == PJ_EPENDING)
597 pjsua_handle_events(100);
598 else
599 break;
600 }
601
602 if (status != PJ_SUCCESS) {
603 pjsua_perror(THIS_FILE,
604 "Error adding STUN address to ICE media transport",
605 status);
606 goto on_error;
607 }
608
Benny Prijonoc97608e2007-03-23 16:34:20 +0000609 }
Benny Prijonoeebe9af2006-06-13 22:57:13 +0000610
611 return PJ_SUCCESS;
612
613on_error:
614 for (i=0; i<pjsua_var.ua_cfg.max_calls; ++i) {
615 if (pjsua_var.calls[i].med_tp != NULL) {
Benny Prijonoc97608e2007-03-23 16:34:20 +0000616 pjmedia_transport_close(pjsua_var.calls[i].med_tp);
Benny Prijonoeebe9af2006-06-13 22:57:13 +0000617 pjsua_var.calls[i].med_tp = NULL;
618 }
619 }
620
Benny Prijonoc97608e2007-03-23 16:34:20 +0000621 return status;
622}
623
624
625/*
626 * Create UDP media transports for all the calls. This function creates
627 * one UDP media transport for each call.
628 */
629PJ_DEF(pj_status_t)
630pjsua_media_transports_create(const pjsua_transport_config *app_cfg)
631{
632 pjsua_transport_config cfg;
633 unsigned i;
634 pj_status_t status;
635
636
637 /* Make sure pjsua_init() has been called */
638 PJ_ASSERT_RETURN(pjsua_var.ua_cfg.max_calls>0, PJ_EINVALIDOP);
639
640 PJSUA_LOCK();
641
642 /* Delete existing media transports */
643 for (i=0; i<pjsua_var.ua_cfg.max_calls; ++i) {
644 if (pjsua_var.calls[i].med_tp != NULL) {
645 pjmedia_transport_close(pjsua_var.calls[i].med_tp);
646 pjsua_var.calls[i].med_tp = NULL;
647 }
648 }
649
650 /* Copy config */
651 pjsua_transport_config_dup(pjsua_var.pool, &cfg, app_cfg);
652
653 if (pjsua_var.media_cfg.enable_ice) {
654 status = create_ice_media_transports(&cfg);
655 } else {
656 status = create_udp_media_transports(&cfg);
657 }
658
659
Benny Prijonoeebe9af2006-06-13 22:57:13 +0000660 PJSUA_UNLOCK();
661
662 return status;
663}
664
665
Benny Prijonoc97608e2007-03-23 16:34:20 +0000666pj_status_t pjsua_media_channel_init(pjsua_call_id call_id,
667 pjsip_role_e role)
668{
669 pjsua_call *call = &pjsua_var.calls[call_id];
670
671 if (pjsua_var.media_cfg.enable_ice) {
Benny Prijonoa6bd7582007-03-28 15:49:48 +0000672 pj_ice_sess_role ice_role;
Benny Prijonoc97608e2007-03-23 16:34:20 +0000673 pj_status_t status;
674
Benny Prijonoa6bd7582007-03-28 15:49:48 +0000675 ice_role = (role==PJSIP_ROLE_UAC ? PJ_ICE_SESS_ROLE_CONTROLLING :
676 PJ_ICE_SESS_ROLE_CONTROLLED);
Benny Prijonoc97608e2007-03-23 16:34:20 +0000677
678 /* Restart ICE */
679 pjmedia_ice_stop_ice(call->med_tp);
680
681 status = pjmedia_ice_init_ice(call->med_tp, ice_role, NULL, NULL);
682 if (status != PJ_SUCCESS)
683 return status;
684 }
685
686 return PJ_SUCCESS;
687}
688
689pj_status_t pjsua_media_channel_create_sdp(pjsua_call_id call_id,
690 pj_pool_t *pool,
691 pjmedia_sdp_session **p_sdp)
692{
693 pjmedia_sdp_session *sdp;
Benny Prijono617c5bc2007-04-02 19:51:21 +0000694 pjmedia_sock_info skinfo;
Benny Prijonoc97608e2007-03-23 16:34:20 +0000695 pjsua_call *call = &pjsua_var.calls[call_id];
696 pj_status_t status;
697
Benny Prijono617c5bc2007-04-02 19:51:21 +0000698 /* Get media socket info */
699 pjmedia_transport_get_info(call->med_tp, &skinfo);
700
701 /* Create SDP */
Benny Prijonoc97608e2007-03-23 16:34:20 +0000702 status = pjmedia_endpt_create_sdp(pjsua_var.med_endpt, pool, 1,
Benny Prijono617c5bc2007-04-02 19:51:21 +0000703 &skinfo, &sdp);
Benny Prijonoc97608e2007-03-23 16:34:20 +0000704 if (status != PJ_SUCCESS)
705 goto on_error;
706
707 if (pjsua_var.media_cfg.enable_ice) {
708 status = pjmedia_ice_modify_sdp(call->med_tp, pool, sdp);
709 if (status != PJ_SUCCESS)
710 goto on_error;
711 }
712
713 *p_sdp = sdp;
714 return PJ_SUCCESS;
715
716on_error:
717 pjsua_media_channel_deinit(call_id);
718 return status;
719
720}
721
722
723static void stop_media_session(pjsua_call_id call_id)
724{
725 pjsua_call *call = &pjsua_var.calls[call_id];
726
727 if (call->conf_slot != PJSUA_INVALID_ID) {
728 pjmedia_conf_remove_port(pjsua_var.mconf, call->conf_slot);
729 call->conf_slot = PJSUA_INVALID_ID;
730 }
731
732 if (call->session) {
733 pjmedia_session_destroy(call->session);
734 call->session = NULL;
735
736 PJ_LOG(4,(THIS_FILE, "Media session for call %d is destroyed",
737 call_id));
738
739 }
740
741 call->media_st = PJSUA_CALL_MEDIA_NONE;
742}
743
744pj_status_t pjsua_media_channel_deinit(pjsua_call_id call_id)
745{
746 pjsua_call *call = &pjsua_var.calls[call_id];
747
748 stop_media_session(call_id);
749
750 if (pjsua_var.media_cfg.enable_ice) {
751 pjmedia_ice_stop_ice(call->med_tp);
752 }
753
754 return PJ_SUCCESS;
755}
756
757
758/*
759 * DTMF callback from the stream.
760 */
761static void dtmf_callback(pjmedia_stream *strm, void *user_data,
762 int digit)
763{
764 PJ_UNUSED_ARG(strm);
765
766 if (pjsua_var.ua_cfg.cb.on_dtmf_digit) {
767 pjsua_call_id call_id;
768
769 call_id = (pjsua_call_id)user_data;
770 pjsua_var.ua_cfg.cb.on_dtmf_digit(call_id, digit);
771 }
772}
773
774
775pj_status_t pjsua_media_channel_update(pjsua_call_id call_id,
Benny Prijonodbce2cf2007-03-28 16:24:00 +0000776 const pjmedia_sdp_session *local_sdp,
777 const pjmedia_sdp_session *remote_sdp)
Benny Prijonoc97608e2007-03-23 16:34:20 +0000778{
779 int prev_media_st = 0;
780 pjsua_call *call = &pjsua_var.calls[call_id];
781 pjmedia_session_info sess_info;
782 pjmedia_port *media_port;
783 pj_str_t port_name;
784 char tmp[PJSIP_MAX_URL_SIZE];
785 pj_status_t status;
786
787 /* Destroy existing media session, if any. */
788 prev_media_st = call->media_st;
789 stop_media_session(call->index);
790
791 /* Create media session info based on SDP parameters.
792 * We only support one stream per session at the moment
793 */
794 status = pjmedia_session_info_from_sdp( call->inv->dlg->pool,
795 pjsua_var.med_endpt,
796 1,&sess_info,
797 local_sdp, remote_sdp);
798 if (status != PJ_SUCCESS)
799 return status;
800
801
802 /* Check if media is put on-hold */
803 if (sess_info.stream_cnt == 0 ||
804 sess_info.stream_info[0].dir == PJMEDIA_DIR_NONE)
805 {
806
807 /* Determine who puts the call on-hold */
808 if (prev_media_st == PJSUA_CALL_MEDIA_ACTIVE) {
809 if (pjmedia_sdp_neg_was_answer_remote(call->inv->neg)) {
810 /* It was local who offer hold */
811 call->media_st = PJSUA_CALL_MEDIA_LOCAL_HOLD;
812 } else {
813 call->media_st = PJSUA_CALL_MEDIA_REMOTE_HOLD;
814 }
815 }
816
817 call->media_dir = PJMEDIA_DIR_NONE;
818
Benny Prijono667952e2007-04-02 19:27:54 +0000819 /* Shutdown ICE session */
820 if (pjsua_var.media_cfg.enable_ice) {
821 pjmedia_ice_stop_ice(call->med_tp);
822 }
823
Benny Prijonoc97608e2007-03-23 16:34:20 +0000824 /* No need because we need keepalive? */
825
826 } else {
827
828 /* Start ICE */
829 if (pjsua_var.media_cfg.enable_ice) {
830 status = pjmedia_ice_start_ice(call->med_tp, call->inv->pool,
Benny Prijonofd70c9b2007-03-27 11:00:38 +0000831 remote_sdp, 0);
Benny Prijonoc97608e2007-03-23 16:34:20 +0000832 if (status != PJ_SUCCESS)
833 return status;
834 }
835
836 /* Override ptime, if this option is specified. */
837 if (pjsua_var.media_cfg.ptime != 0) {
838 sess_info.stream_info[0].param->setting.frm_per_pkt = (pj_uint8_t)
839 (pjsua_var.media_cfg.ptime / sess_info.stream_info[0].param->info.frm_ptime);
840 if (sess_info.stream_info[0].param->setting.frm_per_pkt == 0)
841 sess_info.stream_info[0].param->setting.frm_per_pkt = 1;
842 }
843
844 /* Disable VAD, if this option is specified. */
845 if (pjsua_var.media_cfg.no_vad) {
846 sess_info.stream_info[0].param->setting.vad = 0;
847 }
848
849
850 /* Optionally, application may modify other stream settings here
851 * (such as jitter buffer parameters, codec ptime, etc.)
852 */
853 sess_info.stream_info[0].jb_init = pjsua_var.media_cfg.jb_init;
854 sess_info.stream_info[0].jb_min_pre = pjsua_var.media_cfg.jb_min_pre;
855 sess_info.stream_info[0].jb_max_pre = pjsua_var.media_cfg.jb_max_pre;
856 sess_info.stream_info[0].jb_max = pjsua_var.media_cfg.jb_max;
857
858 /* Create session based on session info. */
859 status = pjmedia_session_create( pjsua_var.med_endpt, &sess_info,
860 &call->med_tp,
861 call, &call->session );
862 if (status != PJ_SUCCESS) {
863 return status;
864 }
865
866 /* If DTMF callback is installed by application, install our
867 * callback to the session.
868 */
869 if (pjsua_var.ua_cfg.cb.on_dtmf_digit) {
870 pjmedia_session_set_dtmf_callback(call->session, 0,
871 &dtmf_callback,
872 (void*)(call->index));
873 }
874
875 /* Get the port interface of the first stream in the session.
876 * We need the port interface to add to the conference bridge.
877 */
878 pjmedia_session_get_port(call->session, 0, &media_port);
879
880
881 /*
882 * Add the call to conference bridge.
883 */
884 port_name.ptr = tmp;
885 port_name.slen = pjsip_uri_print(PJSIP_URI_IN_REQ_URI,
886 call->inv->dlg->remote.info->uri,
887 tmp, sizeof(tmp));
888 if (port_name.slen < 1) {
889 port_name = pj_str("call");
890 }
891 status = pjmedia_conf_add_port( pjsua_var.mconf, call->inv->pool,
892 media_port,
893 &port_name,
894 (unsigned*)&call->conf_slot);
895 if (status != PJ_SUCCESS) {
896 return status;
897 }
898
899 /* Call's media state is active */
900 call->media_st = PJSUA_CALL_MEDIA_ACTIVE;
901 call->media_dir = sess_info.stream_info[0].dir;
902 }
903
904 /* Print info. */
905 {
906 char info[80];
907 int info_len = 0;
908 unsigned i;
909
910 for (i=0; i<sess_info.stream_cnt; ++i) {
911 int len;
912 const char *dir;
913 pjmedia_stream_info *strm_info = &sess_info.stream_info[i];
914
915 switch (strm_info->dir) {
916 case PJMEDIA_DIR_NONE:
917 dir = "inactive";
918 break;
919 case PJMEDIA_DIR_ENCODING:
920 dir = "sendonly";
921 break;
922 case PJMEDIA_DIR_DECODING:
923 dir = "recvonly";
924 break;
925 case PJMEDIA_DIR_ENCODING_DECODING:
926 dir = "sendrecv";
927 break;
928 default:
929 dir = "unknown";
930 break;
931 }
932 len = pj_ansi_sprintf( info+info_len,
933 ", stream #%d: %.*s (%s)", i,
934 (int)strm_info->fmt.encoding_name.slen,
935 strm_info->fmt.encoding_name.ptr,
936 dir);
937 if (len > 0)
938 info_len += len;
939 }
940 PJ_LOG(4,(THIS_FILE,"Media updates%s", info));
941 }
942
943 return PJ_SUCCESS;
944}
945
946
Benny Prijonoeebe9af2006-06-13 22:57:13 +0000947/*
948 * Get maxinum number of conference ports.
949 */
950PJ_DEF(unsigned) pjsua_conf_get_max_ports(void)
951{
952 return pjsua_var.media_cfg.max_media_ports;
953}
954
955
956/*
957 * Get current number of active ports in the bridge.
958 */
959PJ_DEF(unsigned) pjsua_conf_get_active_ports(void)
960{
961 unsigned ports[256];
962 unsigned count = PJ_ARRAY_SIZE(ports);
963 pj_status_t status;
964
965 status = pjmedia_conf_enum_ports(pjsua_var.mconf, ports, &count);
966 if (status != PJ_SUCCESS)
967 count = 0;
968
969 return count;
970}
971
972
973/*
974 * Enumerate all conference ports.
975 */
976PJ_DEF(pj_status_t) pjsua_enum_conf_ports(pjsua_conf_port_id id[],
977 unsigned *count)
978{
979 return pjmedia_conf_enum_ports(pjsua_var.mconf, (unsigned*)id, count);
980}
981
982
983/*
984 * Get information about the specified conference port
985 */
986PJ_DEF(pj_status_t) pjsua_conf_get_port_info( pjsua_conf_port_id id,
987 pjsua_conf_port_info *info)
988{
989 pjmedia_conf_port_info cinfo;
Benny Prijono0498d902006-06-19 14:49:14 +0000990 unsigned i;
Benny Prijonoeebe9af2006-06-13 22:57:13 +0000991 pj_status_t status;
992
993 status = pjmedia_conf_get_port_info( pjsua_var.mconf, id, &cinfo);
994 if (status != PJ_SUCCESS)
995 return status;
996
Benny Prijonoac623b32006-07-03 15:19:31 +0000997 pj_bzero(info, sizeof(*info));
Benny Prijonoeebe9af2006-06-13 22:57:13 +0000998 info->slot_id = id;
999 info->name = cinfo.name;
1000 info->clock_rate = cinfo.clock_rate;
1001 info->channel_count = cinfo.channel_count;
1002 info->samples_per_frame = cinfo.samples_per_frame;
1003 info->bits_per_sample = cinfo.bits_per_sample;
1004
1005 /* Build array of listeners */
Benny Prijonoc78c3a32006-06-16 15:54:43 +00001006 info->listener_cnt = cinfo.listener_cnt;
1007 for (i=0; i<cinfo.listener_cnt; ++i) {
1008 info->listeners[i] = cinfo.listener_slots[i];
Benny Prijonoeebe9af2006-06-13 22:57:13 +00001009 }
1010
1011 return PJ_SUCCESS;
1012}
1013
1014
1015/*
Benny Prijonoe909eac2006-07-27 22:04:56 +00001016 * Add arbitrary media port to PJSUA's conference bridge.
1017 */
1018PJ_DEF(pj_status_t) pjsua_conf_add_port( pj_pool_t *pool,
1019 pjmedia_port *port,
1020 pjsua_conf_port_id *p_id)
1021{
1022 pj_status_t status;
1023
1024 status = pjmedia_conf_add_port(pjsua_var.mconf, pool,
1025 port, NULL, (unsigned*)p_id);
1026 if (status != PJ_SUCCESS) {
1027 if (p_id)
1028 *p_id = PJSUA_INVALID_ID;
1029 }
1030
1031 return status;
1032}
1033
1034
1035/*
1036 * Remove arbitrary slot from the conference bridge.
1037 */
1038PJ_DEF(pj_status_t) pjsua_conf_remove_port(pjsua_conf_port_id id)
1039{
1040 return pjmedia_conf_remove_port(pjsua_var.mconf, (unsigned)id);
1041}
1042
1043
1044/*
Benny Prijonoeebe9af2006-06-13 22:57:13 +00001045 * Establish unidirectional media flow from souce to sink.
1046 */
1047PJ_DEF(pj_status_t) pjsua_conf_connect( pjsua_conf_port_id source,
1048 pjsua_conf_port_id sink)
1049{
1050 return pjmedia_conf_connect_port(pjsua_var.mconf, source, sink, 0);
1051}
1052
1053
1054/*
1055 * Disconnect media flow from the source to destination port.
1056 */
1057PJ_DEF(pj_status_t) pjsua_conf_disconnect( pjsua_conf_port_id source,
1058 pjsua_conf_port_id sink)
1059{
1060 return pjmedia_conf_disconnect_port(pjsua_var.mconf, source, sink);
1061}
1062
1063
Benny Prijono6dd967c2006-12-26 02:27:14 +00001064/*
1065 * Adjust the signal level to be transmitted from the bridge to the
1066 * specified port by making it louder or quieter.
1067 */
1068PJ_DEF(pj_status_t) pjsua_conf_adjust_tx_level(pjsua_conf_port_id slot,
1069 float level)
1070{
1071 return pjmedia_conf_adjust_tx_level(pjsua_var.mconf, slot,
1072 (int)((level-1) * 128));
1073}
1074
1075/*
1076 * Adjust the signal level to be received from the specified port (to
1077 * the bridge) by making it louder or quieter.
1078 */
1079PJ_DEF(pj_status_t) pjsua_conf_adjust_rx_level(pjsua_conf_port_id slot,
1080 float level)
1081{
1082 return pjmedia_conf_adjust_rx_level(pjsua_var.mconf, slot,
1083 (int)((level-1) * 128));
1084}
1085
1086
1087/*
1088 * Get last signal level transmitted to or received from the specified port.
1089 */
1090PJ_DEF(pj_status_t) pjsua_conf_get_signal_level(pjsua_conf_port_id slot,
1091 unsigned *tx_level,
1092 unsigned *rx_level)
1093{
1094 return pjmedia_conf_get_signal_level(pjsua_var.mconf, slot,
1095 tx_level, rx_level);
1096}
1097
Benny Prijonoeebe9af2006-06-13 22:57:13 +00001098/*****************************************************************************
1099 * File player.
1100 */
1101
1102/*
1103 * Create a file player, and automatically connect this player to
1104 * the conference bridge.
1105 */
1106PJ_DEF(pj_status_t) pjsua_player_create( const pj_str_t *filename,
1107 unsigned options,
Benny Prijonoeebe9af2006-06-13 22:57:13 +00001108 pjsua_player_id *p_id)
1109{
1110 unsigned slot, file_id;
Benny Prijonoa66c3312007-01-21 23:12:40 +00001111 char path[PJ_MAXPATH];
Benny Prijonoeebe9af2006-06-13 22:57:13 +00001112 pjmedia_port *port;
1113 pj_status_t status;
1114
1115 if (pjsua_var.player_cnt >= PJ_ARRAY_SIZE(pjsua_var.player))
1116 return PJ_ETOOMANY;
1117
1118 PJSUA_LOCK();
1119
1120 for (file_id=0; file_id<PJ_ARRAY_SIZE(pjsua_var.player); ++file_id) {
1121 if (pjsua_var.player[file_id].port == NULL)
1122 break;
1123 }
1124
1125 if (file_id == PJ_ARRAY_SIZE(pjsua_var.player)) {
1126 /* This is unexpected */
1127 PJSUA_UNLOCK();
1128 pj_assert(0);
1129 return PJ_EBUG;
1130 }
1131
1132 pj_memcpy(path, filename->ptr, filename->slen);
1133 path[filename->slen] = '\0';
1134 status = pjmedia_wav_player_port_create(pjsua_var.pool, path,
1135 pjsua_var.mconf_cfg.samples_per_frame *
1136 1000 / pjsua_var.media_cfg.clock_rate,
Benny Prijono00cae612006-07-31 15:19:36 +00001137 options, 0, &port);
Benny Prijonoeebe9af2006-06-13 22:57:13 +00001138 if (status != PJ_SUCCESS) {
1139 PJSUA_UNLOCK();
1140 pjsua_perror(THIS_FILE, "Unable to open file for playback", status);
1141 return status;
1142 }
1143
1144 status = pjmedia_conf_add_port(pjsua_var.mconf, pjsua_var.pool,
1145 port, filename, &slot);
1146 if (status != PJ_SUCCESS) {
1147 pjmedia_port_destroy(port);
1148 PJSUA_UNLOCK();
Benny Prijono32e4f492007-01-24 00:44:26 +00001149 pjsua_perror(THIS_FILE, "Unable to add file to conference bridge",
1150 status);
Benny Prijonoeebe9af2006-06-13 22:57:13 +00001151 return status;
1152 }
1153
Benny Prijonoa66c3312007-01-21 23:12:40 +00001154 pjsua_var.player[file_id].type = 0;
Benny Prijonoeebe9af2006-06-13 22:57:13 +00001155 pjsua_var.player[file_id].port = port;
1156 pjsua_var.player[file_id].slot = slot;
1157
1158 if (p_id) *p_id = file_id;
1159
1160 ++pjsua_var.player_cnt;
1161
1162 PJSUA_UNLOCK();
1163 return PJ_SUCCESS;
1164}
1165
1166
1167/*
Benny Prijonoa66c3312007-01-21 23:12:40 +00001168 * Create a file playlist media port, and automatically add the port
1169 * to the conference bridge.
1170 */
1171PJ_DEF(pj_status_t) pjsua_playlist_create( const pj_str_t file_names[],
1172 unsigned file_count,
1173 const pj_str_t *label,
1174 unsigned options,
1175 pjsua_player_id *p_id)
1176{
1177 unsigned slot, file_id, ptime;
1178 pjmedia_port *port;
1179 pj_status_t status;
1180
1181 if (pjsua_var.player_cnt >= PJ_ARRAY_SIZE(pjsua_var.player))
1182 return PJ_ETOOMANY;
1183
1184 PJSUA_LOCK();
1185
1186 for (file_id=0; file_id<PJ_ARRAY_SIZE(pjsua_var.player); ++file_id) {
1187 if (pjsua_var.player[file_id].port == NULL)
1188 break;
1189 }
1190
1191 if (file_id == PJ_ARRAY_SIZE(pjsua_var.player)) {
1192 /* This is unexpected */
1193 PJSUA_UNLOCK();
1194 pj_assert(0);
1195 return PJ_EBUG;
1196 }
1197
1198
1199 ptime = pjsua_var.mconf_cfg.samples_per_frame * 1000 /
1200 pjsua_var.media_cfg.clock_rate;
1201
1202 status = pjmedia_wav_playlist_create(pjsua_var.pool, label,
1203 file_names, file_count,
1204 ptime, options, 0, &port);
1205 if (status != PJ_SUCCESS) {
1206 PJSUA_UNLOCK();
1207 pjsua_perror(THIS_FILE, "Unable to create playlist", status);
1208 return status;
1209 }
1210
1211 status = pjmedia_conf_add_port(pjsua_var.mconf, pjsua_var.pool,
1212 port, &port->info.name, &slot);
1213 if (status != PJ_SUCCESS) {
1214 pjmedia_port_destroy(port);
1215 PJSUA_UNLOCK();
1216 pjsua_perror(THIS_FILE, "Unable to add port", status);
1217 return status;
1218 }
1219
1220 pjsua_var.player[file_id].type = 1;
1221 pjsua_var.player[file_id].port = port;
1222 pjsua_var.player[file_id].slot = slot;
1223
1224 if (p_id) *p_id = file_id;
1225
1226 ++pjsua_var.player_cnt;
1227
1228 PJSUA_UNLOCK();
1229 return PJ_SUCCESS;
1230
1231}
1232
1233
1234/*
Benny Prijonoeebe9af2006-06-13 22:57:13 +00001235 * Get conference port ID associated with player.
1236 */
1237PJ_DEF(pjsua_conf_port_id) pjsua_player_get_conf_port(pjsua_player_id id)
1238{
1239 PJ_ASSERT_RETURN(id>=0 && id<PJ_ARRAY_SIZE(pjsua_var.player), PJ_EINVAL);
1240 PJ_ASSERT_RETURN(pjsua_var.player[id].port != NULL, PJ_EINVAL);
1241
1242 return pjsua_var.player[id].slot;
1243}
1244
Benny Prijono469b1522006-12-26 03:05:17 +00001245/*
1246 * Get the media port for the player.
1247 */
1248PJ_DEF(pj_status_t) pjsua_player_get_port( pjsua_recorder_id id,
1249 pjmedia_port **p_port)
1250{
1251 PJ_ASSERT_RETURN(id>=0 && id<PJ_ARRAY_SIZE(pjsua_var.player), PJ_EINVAL);
1252 PJ_ASSERT_RETURN(pjsua_var.player[id].port != NULL, PJ_EINVAL);
1253 PJ_ASSERT_RETURN(p_port != NULL, PJ_EINVAL);
1254
1255 *p_port = pjsua_var.player[id].port;
1256
1257 return PJ_SUCCESS;
1258}
Benny Prijonoeebe9af2006-06-13 22:57:13 +00001259
1260/*
1261 * Set playback position.
1262 */
1263PJ_DEF(pj_status_t) pjsua_player_set_pos( pjsua_player_id id,
1264 pj_uint32_t samples)
1265{
1266 PJ_ASSERT_RETURN(id>=0 && id<PJ_ARRAY_SIZE(pjsua_var.player), PJ_EINVAL);
1267 PJ_ASSERT_RETURN(pjsua_var.player[id].port != NULL, PJ_EINVAL);
Benny Prijonoa66c3312007-01-21 23:12:40 +00001268 PJ_ASSERT_RETURN(pjsua_var.player[id].type == 0, PJ_EINVAL);
Benny Prijonoeebe9af2006-06-13 22:57:13 +00001269
1270 return pjmedia_wav_player_port_set_pos(pjsua_var.player[id].port, samples);
1271}
1272
1273
1274/*
1275 * Close the file, remove the player from the bridge, and free
1276 * resources associated with the file player.
1277 */
1278PJ_DEF(pj_status_t) pjsua_player_destroy(pjsua_player_id id)
1279{
1280 PJ_ASSERT_RETURN(id>=0 && id<PJ_ARRAY_SIZE(pjsua_var.player), PJ_EINVAL);
1281 PJ_ASSERT_RETURN(pjsua_var.player[id].port != NULL, PJ_EINVAL);
1282
1283 PJSUA_LOCK();
1284
1285 if (pjsua_var.player[id].port) {
1286 pjmedia_conf_remove_port(pjsua_var.mconf,
1287 pjsua_var.player[id].slot);
1288 pjmedia_port_destroy(pjsua_var.player[id].port);
1289 pjsua_var.player[id].port = NULL;
1290 pjsua_var.player[id].slot = 0xFFFF;
1291 pjsua_var.player_cnt--;
1292 }
1293
1294 PJSUA_UNLOCK();
1295
1296 return PJ_SUCCESS;
1297}
1298
1299
1300/*****************************************************************************
1301 * File recorder.
1302 */
1303
1304/*
1305 * Create a file recorder, and automatically connect this recorder to
1306 * the conference bridge.
1307 */
1308PJ_DEF(pj_status_t) pjsua_recorder_create( const pj_str_t *filename,
Benny Prijono8f310522006-10-20 11:08:49 +00001309 unsigned enc_type,
1310 void *enc_param,
Benny Prijonoeebe9af2006-06-13 22:57:13 +00001311 pj_ssize_t max_size,
1312 unsigned options,
Benny Prijonoeebe9af2006-06-13 22:57:13 +00001313 pjsua_recorder_id *p_id)
1314{
Benny Prijonob3cdb2b2006-10-19 15:49:47 +00001315 enum Format
1316 {
1317 FMT_UNKNOWN,
1318 FMT_WAV,
1319 FMT_MP3,
1320 };
Benny Prijonoeebe9af2006-06-13 22:57:13 +00001321 unsigned slot, file_id;
Benny Prijonoa66c3312007-01-21 23:12:40 +00001322 char path[PJ_MAXPATH];
Benny Prijonob3cdb2b2006-10-19 15:49:47 +00001323 pj_str_t ext;
Benny Prijono8f310522006-10-20 11:08:49 +00001324 int file_format;
Benny Prijonoeebe9af2006-06-13 22:57:13 +00001325 pjmedia_port *port;
1326 pj_status_t status;
1327
Benny Prijonob3cdb2b2006-10-19 15:49:47 +00001328 /* Filename must present */
1329 PJ_ASSERT_RETURN(filename != NULL, PJ_EINVAL);
1330
Benny Prijono00cae612006-07-31 15:19:36 +00001331 /* Don't support max_size at present */
Benny Prijonob3cdb2b2006-10-19 15:49:47 +00001332 PJ_ASSERT_RETURN(max_size == 0 || max_size == -1, PJ_EINVAL);
Benny Prijono00cae612006-07-31 15:19:36 +00001333
Benny Prijono8f310522006-10-20 11:08:49 +00001334 /* Don't support encoding type at present */
1335 PJ_ASSERT_RETURN(enc_type == 0, PJ_EINVAL);
Benny Prijono00cae612006-07-31 15:19:36 +00001336
Benny Prijonoeebe9af2006-06-13 22:57:13 +00001337 if (pjsua_var.rec_cnt >= PJ_ARRAY_SIZE(pjsua_var.recorder))
1338 return PJ_ETOOMANY;
1339
Benny Prijonob3cdb2b2006-10-19 15:49:47 +00001340 /* Determine the file format */
1341 ext.ptr = filename->ptr + filename->slen - 4;
1342 ext.slen = 4;
1343
1344 if (pj_stricmp2(&ext, ".wav") == 0)
1345 file_format = FMT_WAV;
1346 else if (pj_stricmp2(&ext, ".mp3") == 0)
1347 file_format = FMT_MP3;
1348 else {
1349 PJ_LOG(1,(THIS_FILE, "pjsua_recorder_create() error: unable to "
1350 "determine file format for %.*s",
1351 (int)filename->slen, filename->ptr));
1352 return PJ_ENOTSUP;
1353 }
1354
Benny Prijonoeebe9af2006-06-13 22:57:13 +00001355 PJSUA_LOCK();
1356
1357 for (file_id=0; file_id<PJ_ARRAY_SIZE(pjsua_var.recorder); ++file_id) {
1358 if (pjsua_var.recorder[file_id].port == NULL)
1359 break;
1360 }
1361
1362 if (file_id == PJ_ARRAY_SIZE(pjsua_var.recorder)) {
1363 /* This is unexpected */
1364 PJSUA_UNLOCK();
1365 pj_assert(0);
1366 return PJ_EBUG;
1367 }
1368
1369 pj_memcpy(path, filename->ptr, filename->slen);
1370 path[filename->slen] = '\0';
Benny Prijonob3cdb2b2006-10-19 15:49:47 +00001371
1372 if (file_format == FMT_WAV) {
1373 status = pjmedia_wav_writer_port_create(pjsua_var.pool, path,
1374 pjsua_var.media_cfg.clock_rate,
1375 pjsua_var.mconf_cfg.channel_count,
1376 pjsua_var.mconf_cfg.samples_per_frame,
1377 pjsua_var.mconf_cfg.bits_per_sample,
1378 options, 0, &port);
1379 } else if (file_format == FMT_MP3) {
1380 status = pjmedia_mp3_writer_port_create(pjsua_var.pool, path,
1381 pjsua_var.media_cfg.clock_rate,
1382 pjsua_var.mconf_cfg.channel_count,
1383 pjsua_var.mconf_cfg.samples_per_frame,
1384 pjsua_var.mconf_cfg.bits_per_sample,
Benny Prijono8f310522006-10-20 11:08:49 +00001385 enc_param, &port);
Benny Prijonob3cdb2b2006-10-19 15:49:47 +00001386 } else {
1387 port = NULL;
1388 status = PJ_ENOTSUP;
1389 }
1390
Benny Prijonoeebe9af2006-06-13 22:57:13 +00001391 if (status != PJ_SUCCESS) {
1392 PJSUA_UNLOCK();
1393 pjsua_perror(THIS_FILE, "Unable to open file for recording", status);
1394 return status;
1395 }
1396
1397 status = pjmedia_conf_add_port(pjsua_var.mconf, pjsua_var.pool,
1398 port, filename, &slot);
1399 if (status != PJ_SUCCESS) {
1400 pjmedia_port_destroy(port);
1401 PJSUA_UNLOCK();
1402 return status;
1403 }
1404
1405 pjsua_var.recorder[file_id].port = port;
1406 pjsua_var.recorder[file_id].slot = slot;
1407
1408 if (p_id) *p_id = file_id;
1409
1410 ++pjsua_var.rec_cnt;
1411
1412 PJSUA_UNLOCK();
1413 return PJ_SUCCESS;
1414}
1415
1416
1417/*
1418 * Get conference port associated with recorder.
1419 */
1420PJ_DEF(pjsua_conf_port_id) pjsua_recorder_get_conf_port(pjsua_recorder_id id)
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
1425 return pjsua_var.recorder[id].slot;
1426}
1427
Benny Prijono469b1522006-12-26 03:05:17 +00001428/*
1429 * Get the media port for the recorder.
1430 */
1431PJ_DEF(pj_status_t) pjsua_recorder_get_port( pjsua_recorder_id id,
1432 pjmedia_port **p_port)
1433{
1434 PJ_ASSERT_RETURN(id>=0 && id<PJ_ARRAY_SIZE(pjsua_var.recorder), PJ_EINVAL);
1435 PJ_ASSERT_RETURN(pjsua_var.recorder[id].port != NULL, PJ_EINVAL);
1436 PJ_ASSERT_RETURN(p_port != NULL, PJ_EINVAL);
1437
1438 *p_port = pjsua_var.recorder[id].port;
1439 return PJ_SUCCESS;
1440}
Benny Prijonoeebe9af2006-06-13 22:57:13 +00001441
1442/*
1443 * Destroy recorder (this will complete recording).
1444 */
1445PJ_DEF(pj_status_t) pjsua_recorder_destroy(pjsua_recorder_id id)
1446{
1447 PJ_ASSERT_RETURN(id>=0 && id<PJ_ARRAY_SIZE(pjsua_var.recorder), PJ_EINVAL);
1448 PJ_ASSERT_RETURN(pjsua_var.recorder[id].port != NULL, PJ_EINVAL);
1449
1450 PJSUA_LOCK();
1451
1452 if (pjsua_var.recorder[id].port) {
1453 pjmedia_conf_remove_port(pjsua_var.mconf,
1454 pjsua_var.recorder[id].slot);
1455 pjmedia_port_destroy(pjsua_var.recorder[id].port);
1456 pjsua_var.recorder[id].port = NULL;
1457 pjsua_var.recorder[id].slot = 0xFFFF;
1458 pjsua_var.rec_cnt--;
1459 }
1460
1461 PJSUA_UNLOCK();
1462
1463 return PJ_SUCCESS;
1464}
1465
1466
1467/*****************************************************************************
1468 * Sound devices.
1469 */
1470
1471/*
1472 * Enum sound devices.
1473 */
1474PJ_DEF(pj_status_t) pjsua_enum_snd_devs( pjmedia_snd_dev_info info[],
1475 unsigned *count)
1476{
1477 unsigned i, dev_count;
1478
1479 dev_count = pjmedia_snd_get_dev_count();
1480
1481 if (dev_count > *count) dev_count = *count;
1482
1483 for (i=0; i<dev_count; ++i) {
1484 const pjmedia_snd_dev_info *ci;
1485
1486 ci = pjmedia_snd_get_dev_info(i);
1487 pj_memcpy(&info[i], ci, sizeof(*ci));
1488 }
1489
1490 *count = dev_count;
1491
1492 return PJ_SUCCESS;
1493}
1494
1495
1496/* Close existing sound device */
1497static void close_snd_dev(void)
1498{
1499 /* Close sound device */
1500 if (pjsua_var.snd_port) {
1501 const pjmedia_snd_dev_info *cap_info, *play_info;
1502
1503 cap_info = pjmedia_snd_get_dev_info(pjsua_var.cap_dev);
1504 play_info = pjmedia_snd_get_dev_info(pjsua_var.play_dev);
1505
1506 PJ_LOG(4,(THIS_FILE, "Closing %s sound playback device and "
1507 "%s sound capture device",
1508 play_info->name, cap_info->name));
1509
1510 pjmedia_snd_port_disconnect(pjsua_var.snd_port);
1511 pjmedia_snd_port_destroy(pjsua_var.snd_port);
1512 pjsua_var.snd_port = NULL;
1513 }
1514
1515 /* Close null sound device */
1516 if (pjsua_var.null_snd) {
1517 PJ_LOG(4,(THIS_FILE, "Closing null sound device.."));
1518 pjmedia_master_port_destroy(pjsua_var.null_snd, PJ_FALSE);
1519 pjsua_var.null_snd = NULL;
1520 }
1521}
1522
1523/*
1524 * Select or change sound device. Application may call this function at
1525 * any time to replace current sound device.
1526 */
1527PJ_DEF(pj_status_t) pjsua_set_snd_dev( int capture_dev,
1528 int playback_dev)
1529{
1530 pjmedia_port *conf_port;
Benny Prijono6dd967c2006-12-26 02:27:14 +00001531 const pjmedia_snd_dev_info *play_info;
Benny Prijono26056d82006-10-11 16:03:41 +00001532 unsigned clock_rates[] = { 0, 22050, 44100, 48000, 11025, 32000, 8000};
Benny Prijono658a1c52006-10-11 21:56:16 +00001533 unsigned selected_clock_rate = 0;
Benny Prijono26056d82006-10-11 16:03:41 +00001534 unsigned i;
Benny Prijonoc53c6d72006-11-27 09:54:03 +00001535 pjmedia_snd_stream *strm;
1536 pjmedia_snd_stream_info si;
1537 pj_str_t tmp;
Benny Prijono26056d82006-10-11 16:03:41 +00001538 pj_status_t status = -1;
Benny Prijonoeebe9af2006-06-13 22:57:13 +00001539
1540 /* Close existing sound port */
1541 close_snd_dev();
1542
1543
Benny Prijono26056d82006-10-11 16:03:41 +00001544 /* Set default clock rate */
1545 clock_rates[0] = pjsua_var.media_cfg.clock_rate;
Benny Prijonoeebe9af2006-06-13 22:57:13 +00001546
Benny Prijono26056d82006-10-11 16:03:41 +00001547 /* Attempts to open the sound device with different clock rates */
1548 for (i=0; i<PJ_ARRAY_SIZE(clock_rates); ++i) {
1549 char errmsg[PJ_ERR_MSG_SIZE];
1550
1551 PJ_LOG(4,(THIS_FILE,
1552 "pjsua_set_snd_dev(): attempting to open devices "
1553 "@%d Hz", clock_rates[i]));
1554
1555 /* Create the sound device. Sound port will start immediately. */
1556 status = pjmedia_snd_port_create(pjsua_var.pool, capture_dev,
1557 playback_dev,
1558 clock_rates[i], 1,
1559 clock_rates[i]/FPS,
1560 16, 0, &pjsua_var.snd_port);
1561
Benny Prijono658a1c52006-10-11 21:56:16 +00001562 if (status == PJ_SUCCESS) {
1563 selected_clock_rate = clock_rates[i];
Benny Prijono26056d82006-10-11 16:03:41 +00001564 break;
Benny Prijono658a1c52006-10-11 21:56:16 +00001565 }
Benny Prijono26056d82006-10-11 16:03:41 +00001566
1567 pj_strerror(status, errmsg, sizeof(errmsg));
Benny Prijono658a1c52006-10-11 21:56:16 +00001568 PJ_LOG(4, (THIS_FILE, "..failed: %s", errmsg));
Benny Prijono26056d82006-10-11 16:03:41 +00001569 }
1570
Benny Prijonoeebe9af2006-06-13 22:57:13 +00001571 if (status != PJ_SUCCESS) {
1572 pjsua_perror(THIS_FILE, "Unable to open sound device", status);
1573 return status;
1574 }
1575
1576 /* Get the port0 of the conference bridge. */
1577 conf_port = pjmedia_conf_get_master_port(pjsua_var.mconf);
1578 pj_assert(conf_port != NULL);
1579
Benny Prijonof20687a2006-08-04 18:27:19 +00001580 /* Set AEC */
Benny Prijono5da50432006-08-07 10:24:52 +00001581 pjmedia_snd_port_set_ec( pjsua_var.snd_port, pjsua_var.pool,
1582 pjsua_var.media_cfg.ec_tail_len,
1583 pjsua_var.media_cfg.ec_options);
Benny Prijonoeebe9af2006-06-13 22:57:13 +00001584
Benny Prijono658a1c52006-10-11 21:56:16 +00001585 /* If there's mismatch between sound port and conference's port,
1586 * create a resample port to bridge them.
1587 */
1588 if (selected_clock_rate != pjsua_var.media_cfg.clock_rate) {
1589 pjmedia_port *resample_port;
1590
1591 status = pjmedia_resample_port_create(pjsua_var.pool, conf_port,
1592 selected_clock_rate, 0,
1593 &resample_port);
1594 if (status != PJ_SUCCESS) {
1595 pjsua_perror("Error creating resample port", THIS_FILE, status);
1596 return status;
1597 }
1598
1599 conf_port = resample_port;
1600 }
1601
Benny Prijono52a93912006-08-04 20:54:37 +00001602 /* Connect sound port to the bridge */
1603 status = pjmedia_snd_port_connect(pjsua_var.snd_port,
1604 conf_port );
1605 if (status != PJ_SUCCESS) {
1606 pjsua_perror(THIS_FILE, "Unable to connect conference port to "
1607 "sound device", status);
1608 pjmedia_snd_port_destroy(pjsua_var.snd_port);
1609 pjsua_var.snd_port = NULL;
1610 return status;
1611 }
1612
Benny Prijonoeebe9af2006-06-13 22:57:13 +00001613 /* Save the device IDs */
1614 pjsua_var.cap_dev = capture_dev;
1615 pjsua_var.play_dev = playback_dev;
1616
Benny Prijonoc53c6d72006-11-27 09:54:03 +00001617 /* Update sound device name. */
1618 strm = pjmedia_snd_port_get_snd_stream(pjsua_var.snd_port);
1619 pjmedia_snd_stream_get_info(strm, &si);
1620 play_info = pjmedia_snd_get_dev_info(si.rec_id);
1621
1622 pjmedia_conf_set_port0_name(pjsua_var.mconf,
1623 pj_cstr(&tmp, play_info->name));
1624
Benny Prijonoeebe9af2006-06-13 22:57:13 +00001625 return PJ_SUCCESS;
1626}
1627
1628
1629/*
Benny Prijonoebdf8772007-02-01 19:25:50 +00001630 * Get currently active sound devices. If sound devices has not been created
1631 * (for example when pjsua_start() is not called), it is possible that
1632 * the function returns PJ_SUCCESS with -1 as device IDs.
1633 */
1634PJ_DEF(pj_status_t) pjsua_get_snd_dev(int *capture_dev,
1635 int *playback_dev)
1636{
1637 if (capture_dev) {
1638 *capture_dev = pjsua_var.cap_dev;
1639 }
1640 if (playback_dev) {
1641 *playback_dev = pjsua_var.play_dev;
1642 }
1643
1644 return PJ_SUCCESS;
1645}
1646
1647
1648/*
Benny Prijonoeebe9af2006-06-13 22:57:13 +00001649 * Use null sound device.
1650 */
1651PJ_DEF(pj_status_t) pjsua_set_null_snd_dev(void)
1652{
1653 pjmedia_port *conf_port;
1654 pj_status_t status;
1655
1656 /* Close existing sound device */
1657 close_snd_dev();
1658
1659 PJ_LOG(4,(THIS_FILE, "Opening null sound device.."));
1660
1661 /* Get the port0 of the conference bridge. */
1662 conf_port = pjmedia_conf_get_master_port(pjsua_var.mconf);
1663 pj_assert(conf_port != NULL);
1664
1665 /* Create master port, connecting port0 of the conference bridge to
1666 * a null port.
1667 */
1668 status = pjmedia_master_port_create(pjsua_var.pool, pjsua_var.null_port,
1669 conf_port, 0, &pjsua_var.null_snd);
1670 if (status != PJ_SUCCESS) {
1671 pjsua_perror(THIS_FILE, "Unable to create null sound device",
1672 status);
1673 return status;
1674 }
1675
1676 /* Start the master port */
1677 status = pjmedia_master_port_start(pjsua_var.null_snd);
1678 PJ_ASSERT_RETURN(status == PJ_SUCCESS, status);
1679
1680 return PJ_SUCCESS;
1681}
1682
1683
Benny Prijonoe909eac2006-07-27 22:04:56 +00001684
1685/*
1686 * Use no device!
1687 */
1688PJ_DEF(pjmedia_port*) pjsua_set_no_snd_dev(void)
1689{
1690 /* Close existing sound device */
1691 close_snd_dev();
1692
1693 pjsua_var.no_snd = PJ_TRUE;
1694 return pjmedia_conf_get_master_port(pjsua_var.mconf);
1695}
1696
1697
Benny Prijonof20687a2006-08-04 18:27:19 +00001698/*
1699 * Configure the AEC settings of the sound port.
1700 */
Benny Prijono5da50432006-08-07 10:24:52 +00001701PJ_DEF(pj_status_t) pjsua_set_ec(unsigned tail_ms, unsigned options)
Benny Prijonof20687a2006-08-04 18:27:19 +00001702{
1703 pjsua_var.media_cfg.ec_tail_len = tail_ms;
1704
1705 if (pjsua_var.snd_port)
Benny Prijono5da50432006-08-07 10:24:52 +00001706 return pjmedia_snd_port_set_ec( pjsua_var.snd_port, pjsua_var.pool,
1707 tail_ms, options);
Benny Prijonof20687a2006-08-04 18:27:19 +00001708
1709 return PJ_SUCCESS;
1710}
1711
1712
1713/*
1714 * Get current AEC tail length.
1715 */
Benny Prijono22dfe592006-08-06 12:07:13 +00001716PJ_DEF(pj_status_t) pjsua_get_ec_tail(unsigned *p_tail_ms)
Benny Prijonof20687a2006-08-04 18:27:19 +00001717{
1718 *p_tail_ms = pjsua_var.media_cfg.ec_tail_len;
1719 return PJ_SUCCESS;
1720}
1721
Benny Prijonoe909eac2006-07-27 22:04:56 +00001722
Benny Prijonoeebe9af2006-06-13 22:57:13 +00001723/*****************************************************************************
1724 * Codecs.
1725 */
1726
1727/*
1728 * Enum all supported codecs in the system.
1729 */
1730PJ_DEF(pj_status_t) pjsua_enum_codecs( pjsua_codec_info id[],
1731 unsigned *p_count )
1732{
1733 pjmedia_codec_mgr *codec_mgr;
1734 pjmedia_codec_info info[32];
1735 unsigned i, count, prio[32];
1736 pj_status_t status;
1737
1738 codec_mgr = pjmedia_endpt_get_codec_mgr(pjsua_var.med_endpt);
1739 count = PJ_ARRAY_SIZE(info);
1740 status = pjmedia_codec_mgr_enum_codecs( codec_mgr, &count, info, prio);
1741 if (status != PJ_SUCCESS) {
1742 *p_count = 0;
1743 return status;
1744 }
1745
1746 if (count > *p_count) count = *p_count;
1747
1748 for (i=0; i<count; ++i) {
1749 pjmedia_codec_info_to_id(&info[i], id[i].buf_, sizeof(id[i].buf_));
1750 id[i].codec_id = pj_str(id[i].buf_);
1751 id[i].priority = (pj_uint8_t) prio[i];
1752 }
1753
1754 *p_count = count;
1755
1756 return PJ_SUCCESS;
1757}
1758
1759
1760/*
1761 * Change codec priority.
1762 */
1763PJ_DEF(pj_status_t) pjsua_codec_set_priority( const pj_str_t *codec_id,
1764 pj_uint8_t priority )
1765{
1766 pjmedia_codec_mgr *codec_mgr;
1767
1768 codec_mgr = pjmedia_endpt_get_codec_mgr(pjsua_var.med_endpt);
1769
1770 return pjmedia_codec_mgr_set_codec_priority(codec_mgr, codec_id,
1771 priority);
1772}
1773
1774
1775/*
1776 * Get codec parameters.
1777 */
1778PJ_DEF(pj_status_t) pjsua_codec_get_param( const pj_str_t *codec_id,
1779 pjmedia_codec_param *param )
1780{
1781 const pjmedia_codec_info *info;
1782 pjmedia_codec_mgr *codec_mgr;
1783 unsigned count = 1;
1784 pj_status_t status;
1785
1786 codec_mgr = pjmedia_endpt_get_codec_mgr(pjsua_var.med_endpt);
1787
1788 status = pjmedia_codec_mgr_find_codecs_by_id(codec_mgr, codec_id,
1789 &count, &info, NULL);
1790 if (status != PJ_SUCCESS)
1791 return status;
1792
1793 if (count != 1)
1794 return PJ_ENOTFOUND;
1795
1796 status = pjmedia_codec_mgr_get_default_param( codec_mgr, info, param);
1797 return status;
1798}
1799
1800
1801/*
1802 * Set codec parameters.
1803 */
1804PJ_DEF(pj_status_t) pjsua_codec_set_param( const pj_str_t *id,
1805 const pjmedia_codec_param *param)
1806{
Benny Prijono00cae612006-07-31 15:19:36 +00001807 PJ_UNUSED_ARG(id);
1808 PJ_UNUSED_ARG(param);
Benny Prijonoeebe9af2006-06-13 22:57:13 +00001809 PJ_TODO(set_codec_param);
1810 return PJ_SUCCESS;
1811}