blob: 0d4f6b2a5820e70cfe23db4ae66c6c5250f747a6 [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;
508 pj_status_t status;
509
Benny Prijonoeebe9af2006-06-13 22:57:13 +0000510 /* Create each media transport */
511 for (i=0; i<pjsua_var.ua_cfg.max_calls; ++i) {
512
Benny Prijonoc97608e2007-03-23 16:34:20 +0000513 status = create_rtp_rtcp_sock(cfg, &pjsua_var.calls[i].skinfo);
Benny Prijonoeebe9af2006-06-13 22:57:13 +0000514 if (status != PJ_SUCCESS) {
515 pjsua_perror(THIS_FILE, "Unable to create RTP/RTCP socket",
516 status);
517 goto on_error;
518 }
519 status = pjmedia_transport_udp_attach(pjsua_var.med_endpt, NULL,
520 &pjsua_var.calls[i].skinfo, 0,
521 &pjsua_var.calls[i].med_tp);
522 if (status != PJ_SUCCESS) {
523 pjsua_perror(THIS_FILE, "Unable to create media transport",
524 status);
525 goto on_error;
526 }
Benny Prijono00cae612006-07-31 15:19:36 +0000527
528 pjmedia_transport_udp_simulate_lost(pjsua_var.calls[i].med_tp,
529 PJMEDIA_DIR_ENCODING,
530 pjsua_var.media_cfg.tx_drop_pct);
531
532 pjmedia_transport_udp_simulate_lost(pjsua_var.calls[i].med_tp,
533 PJMEDIA_DIR_DECODING,
534 pjsua_var.media_cfg.rx_drop_pct);
535
Benny Prijonoeebe9af2006-06-13 22:57:13 +0000536 }
537
Benny Prijonoc97608e2007-03-23 16:34:20 +0000538 return PJ_SUCCESS;
539
540on_error:
541 for (i=0; i<pjsua_var.ua_cfg.max_calls; ++i) {
542 if (pjsua_var.calls[i].med_tp != NULL) {
543 pjmedia_transport_close(pjsua_var.calls[i].med_tp);
544 pjsua_var.calls[i].med_tp = NULL;
545 }
546 }
547
548 return status;
549}
550
551
552/* Create ICE media transports (when ice is enabled) */
553static pj_status_t create_ice_media_transports(pjsua_transport_config *cfg)
554{
555 unsigned i;
Benny Prijonob681a2f2007-03-25 18:44:51 +0000556 pj_sockaddr_in addr;
Benny Prijonoc97608e2007-03-23 16:34:20 +0000557 pj_status_t status;
558
Benny Prijonob681a2f2007-03-25 18:44:51 +0000559 pj_sockaddr_in_init(&addr, 0, (pj_uint16_t)cfg->port);
560
Benny Prijonoc97608e2007-03-23 16:34:20 +0000561 /* Create each media transport */
562 for (i=0; i<pjsua_var.ua_cfg.max_calls; ++i) {
Benny Prijonoa6bd7582007-03-28 15:49:48 +0000563 pj_ice_strans_comp comp;
Benny Prijonob681a2f2007-03-25 18:44:51 +0000564 int next_port;
Benny Prijonoc97608e2007-03-23 16:34:20 +0000565
Benny Prijono7e0d52c2007-03-26 13:25:07 +0000566 status = pjmedia_ice_create(pjsua_var.med_endpt, NULL, 2,
Benny Prijonoc97608e2007-03-23 16:34:20 +0000567 &pjsua_var.stun_cfg,
568 &pjsua_var.calls[i].med_tp);
569 if (status != PJ_SUCCESS) {
570 pjsua_perror(THIS_FILE, "Unable to create ICE media transport",
571 status);
572 goto on_error;
573 }
574
Benny Prijonob681a2f2007-03-25 18:44:51 +0000575 status = pjmedia_ice_start_init(pjsua_var.calls[i].med_tp, 0, &addr,
576 &pjsua_var.stun_srv.ipv4, NULL);
Benny Prijonoc97608e2007-03-23 16:34:20 +0000577 if (status != PJ_SUCCESS) {
Benny Prijonob681a2f2007-03-25 18:44:51 +0000578 pjsua_perror(THIS_FILE, "Error starting ICE transport",
Benny Prijonoc97608e2007-03-23 16:34:20 +0000579 status);
580 goto on_error;
581 }
582
Benny Prijonob681a2f2007-03-25 18:44:51 +0000583 pjmedia_ice_get_comp(pjsua_var.calls[i].med_tp, 1, &comp);
584 next_port = pj_ntohs(comp.local_addr.ipv4.sin_port);
585 next_port += 2;
586 addr.sin_port = pj_htons((pj_uint16_t)next_port);
Benny Prijonoc97608e2007-03-23 16:34:20 +0000587 }
588
589 /* Wait until all ICE transports are ready */
590 for (i=0; i<pjsua_var.ua_cfg.max_calls; ++i) {
Benny Prijonoc97608e2007-03-23 16:34:20 +0000591
592 /* Wait until interface status is PJ_SUCCESS */
593 for (;;) {
Benny Prijonob681a2f2007-03-25 18:44:51 +0000594 status = pjmedia_ice_get_init_status(pjsua_var.calls[i].med_tp);
Benny Prijonoc97608e2007-03-23 16:34:20 +0000595 if (status == PJ_EPENDING)
596 pjsua_handle_events(100);
597 else
598 break;
599 }
600
601 if (status != PJ_SUCCESS) {
602 pjsua_perror(THIS_FILE,
603 "Error adding STUN address to ICE media transport",
604 status);
605 goto on_error;
606 }
607
608 /* Get transport info */
609 pjmedia_transport_get_info(pjsua_var.calls[i].med_tp,
610 &pjsua_var.calls[i].skinfo);
611
612 }
Benny Prijonoeebe9af2006-06-13 22:57:13 +0000613
614 return PJ_SUCCESS;
615
616on_error:
617 for (i=0; i<pjsua_var.ua_cfg.max_calls; ++i) {
618 if (pjsua_var.calls[i].med_tp != NULL) {
Benny Prijonoc97608e2007-03-23 16:34:20 +0000619 pjmedia_transport_close(pjsua_var.calls[i].med_tp);
Benny Prijonoeebe9af2006-06-13 22:57:13 +0000620 pjsua_var.calls[i].med_tp = NULL;
621 }
622 }
623
Benny Prijonoc97608e2007-03-23 16:34:20 +0000624 return status;
625}
626
627
628/*
629 * Create UDP media transports for all the calls. This function creates
630 * one UDP media transport for each call.
631 */
632PJ_DEF(pj_status_t)
633pjsua_media_transports_create(const pjsua_transport_config *app_cfg)
634{
635 pjsua_transport_config cfg;
636 unsigned i;
637 pj_status_t status;
638
639
640 /* Make sure pjsua_init() has been called */
641 PJ_ASSERT_RETURN(pjsua_var.ua_cfg.max_calls>0, PJ_EINVALIDOP);
642
643 PJSUA_LOCK();
644
645 /* Delete existing media transports */
646 for (i=0; i<pjsua_var.ua_cfg.max_calls; ++i) {
647 if (pjsua_var.calls[i].med_tp != NULL) {
648 pjmedia_transport_close(pjsua_var.calls[i].med_tp);
649 pjsua_var.calls[i].med_tp = NULL;
650 }
651 }
652
653 /* Copy config */
654 pjsua_transport_config_dup(pjsua_var.pool, &cfg, app_cfg);
655
656 if (pjsua_var.media_cfg.enable_ice) {
657 status = create_ice_media_transports(&cfg);
658 } else {
659 status = create_udp_media_transports(&cfg);
660 }
661
662
Benny Prijonoeebe9af2006-06-13 22:57:13 +0000663 PJSUA_UNLOCK();
664
665 return status;
666}
667
668
Benny Prijonoc97608e2007-03-23 16:34:20 +0000669pj_status_t pjsua_media_channel_init(pjsua_call_id call_id,
670 pjsip_role_e role)
671{
672 pjsua_call *call = &pjsua_var.calls[call_id];
673
674 if (pjsua_var.media_cfg.enable_ice) {
Benny Prijonoa6bd7582007-03-28 15:49:48 +0000675 pj_ice_sess_role ice_role;
Benny Prijonoc97608e2007-03-23 16:34:20 +0000676 pj_status_t status;
677
Benny Prijonoa6bd7582007-03-28 15:49:48 +0000678 ice_role = (role==PJSIP_ROLE_UAC ? PJ_ICE_SESS_ROLE_CONTROLLING :
679 PJ_ICE_SESS_ROLE_CONTROLLED);
Benny Prijonoc97608e2007-03-23 16:34:20 +0000680
681 /* Restart ICE */
682 pjmedia_ice_stop_ice(call->med_tp);
683
684 status = pjmedia_ice_init_ice(call->med_tp, ice_role, NULL, NULL);
685 if (status != PJ_SUCCESS)
686 return status;
687 }
688
689 return PJ_SUCCESS;
690}
691
692pj_status_t pjsua_media_channel_create_sdp(pjsua_call_id call_id,
693 pj_pool_t *pool,
694 pjmedia_sdp_session **p_sdp)
695{
696 pjmedia_sdp_session *sdp;
697 pjsua_call *call = &pjsua_var.calls[call_id];
698 pj_status_t status;
699
700 status = pjmedia_endpt_create_sdp(pjsua_var.med_endpt, pool, 1,
701 &call->skinfo, &sdp);
702 if (status != PJ_SUCCESS)
703 goto on_error;
704
705 if (pjsua_var.media_cfg.enable_ice) {
706 status = pjmedia_ice_modify_sdp(call->med_tp, pool, sdp);
707 if (status != PJ_SUCCESS)
708 goto on_error;
709 }
710
711 *p_sdp = sdp;
712 return PJ_SUCCESS;
713
714on_error:
715 pjsua_media_channel_deinit(call_id);
716 return status;
717
718}
719
720
721static void stop_media_session(pjsua_call_id call_id)
722{
723 pjsua_call *call = &pjsua_var.calls[call_id];
724
725 if (call->conf_slot != PJSUA_INVALID_ID) {
726 pjmedia_conf_remove_port(pjsua_var.mconf, call->conf_slot);
727 call->conf_slot = PJSUA_INVALID_ID;
728 }
729
730 if (call->session) {
731 pjmedia_session_destroy(call->session);
732 call->session = NULL;
733
734 PJ_LOG(4,(THIS_FILE, "Media session for call %d is destroyed",
735 call_id));
736
737 }
738
739 call->media_st = PJSUA_CALL_MEDIA_NONE;
740}
741
742pj_status_t pjsua_media_channel_deinit(pjsua_call_id call_id)
743{
744 pjsua_call *call = &pjsua_var.calls[call_id];
745
746 stop_media_session(call_id);
747
748 if (pjsua_var.media_cfg.enable_ice) {
749 pjmedia_ice_stop_ice(call->med_tp);
750 }
751
752 return PJ_SUCCESS;
753}
754
755
756/*
757 * DTMF callback from the stream.
758 */
759static void dtmf_callback(pjmedia_stream *strm, void *user_data,
760 int digit)
761{
762 PJ_UNUSED_ARG(strm);
763
764 if (pjsua_var.ua_cfg.cb.on_dtmf_digit) {
765 pjsua_call_id call_id;
766
767 call_id = (pjsua_call_id)user_data;
768 pjsua_var.ua_cfg.cb.on_dtmf_digit(call_id, digit);
769 }
770}
771
772
773pj_status_t pjsua_media_channel_update(pjsua_call_id call_id,
Benny Prijonodbce2cf2007-03-28 16:24:00 +0000774 const pjmedia_sdp_session *local_sdp,
775 const pjmedia_sdp_session *remote_sdp)
Benny Prijonoc97608e2007-03-23 16:34:20 +0000776{
777 int prev_media_st = 0;
778 pjsua_call *call = &pjsua_var.calls[call_id];
779 pjmedia_session_info sess_info;
780 pjmedia_port *media_port;
781 pj_str_t port_name;
782 char tmp[PJSIP_MAX_URL_SIZE];
783 pj_status_t status;
784
785 /* Destroy existing media session, if any. */
786 prev_media_st = call->media_st;
787 stop_media_session(call->index);
788
789 /* Create media session info based on SDP parameters.
790 * We only support one stream per session at the moment
791 */
792 status = pjmedia_session_info_from_sdp( call->inv->dlg->pool,
793 pjsua_var.med_endpt,
794 1,&sess_info,
795 local_sdp, remote_sdp);
796 if (status != PJ_SUCCESS)
797 return status;
798
799
800 /* Check if media is put on-hold */
801 if (sess_info.stream_cnt == 0 ||
802 sess_info.stream_info[0].dir == PJMEDIA_DIR_NONE)
803 {
804
805 /* Determine who puts the call on-hold */
806 if (prev_media_st == PJSUA_CALL_MEDIA_ACTIVE) {
807 if (pjmedia_sdp_neg_was_answer_remote(call->inv->neg)) {
808 /* It was local who offer hold */
809 call->media_st = PJSUA_CALL_MEDIA_LOCAL_HOLD;
810 } else {
811 call->media_st = PJSUA_CALL_MEDIA_REMOTE_HOLD;
812 }
813 }
814
815 call->media_dir = PJMEDIA_DIR_NONE;
816
817 /* Shutdown transport */
818 /* No need because we need keepalive? */
819
820 } else {
821
822 /* Start ICE */
823 if (pjsua_var.media_cfg.enable_ice) {
824 status = pjmedia_ice_start_ice(call->med_tp, call->inv->pool,
Benny Prijonofd70c9b2007-03-27 11:00:38 +0000825 remote_sdp, 0);
Benny Prijonoc97608e2007-03-23 16:34:20 +0000826 if (status != PJ_SUCCESS)
827 return status;
828 }
829
830 /* Override ptime, if this option is specified. */
831 if (pjsua_var.media_cfg.ptime != 0) {
832 sess_info.stream_info[0].param->setting.frm_per_pkt = (pj_uint8_t)
833 (pjsua_var.media_cfg.ptime / sess_info.stream_info[0].param->info.frm_ptime);
834 if (sess_info.stream_info[0].param->setting.frm_per_pkt == 0)
835 sess_info.stream_info[0].param->setting.frm_per_pkt = 1;
836 }
837
838 /* Disable VAD, if this option is specified. */
839 if (pjsua_var.media_cfg.no_vad) {
840 sess_info.stream_info[0].param->setting.vad = 0;
841 }
842
843
844 /* Optionally, application may modify other stream settings here
845 * (such as jitter buffer parameters, codec ptime, etc.)
846 */
847 sess_info.stream_info[0].jb_init = pjsua_var.media_cfg.jb_init;
848 sess_info.stream_info[0].jb_min_pre = pjsua_var.media_cfg.jb_min_pre;
849 sess_info.stream_info[0].jb_max_pre = pjsua_var.media_cfg.jb_max_pre;
850 sess_info.stream_info[0].jb_max = pjsua_var.media_cfg.jb_max;
851
852 /* Create session based on session info. */
853 status = pjmedia_session_create( pjsua_var.med_endpt, &sess_info,
854 &call->med_tp,
855 call, &call->session );
856 if (status != PJ_SUCCESS) {
857 return status;
858 }
859
860 /* If DTMF callback is installed by application, install our
861 * callback to the session.
862 */
863 if (pjsua_var.ua_cfg.cb.on_dtmf_digit) {
864 pjmedia_session_set_dtmf_callback(call->session, 0,
865 &dtmf_callback,
866 (void*)(call->index));
867 }
868
869 /* Get the port interface of the first stream in the session.
870 * We need the port interface to add to the conference bridge.
871 */
872 pjmedia_session_get_port(call->session, 0, &media_port);
873
874
875 /*
876 * Add the call to conference bridge.
877 */
878 port_name.ptr = tmp;
879 port_name.slen = pjsip_uri_print(PJSIP_URI_IN_REQ_URI,
880 call->inv->dlg->remote.info->uri,
881 tmp, sizeof(tmp));
882 if (port_name.slen < 1) {
883 port_name = pj_str("call");
884 }
885 status = pjmedia_conf_add_port( pjsua_var.mconf, call->inv->pool,
886 media_port,
887 &port_name,
888 (unsigned*)&call->conf_slot);
889 if (status != PJ_SUCCESS) {
890 return status;
891 }
892
893 /* Call's media state is active */
894 call->media_st = PJSUA_CALL_MEDIA_ACTIVE;
895 call->media_dir = sess_info.stream_info[0].dir;
896 }
897
898 /* Print info. */
899 {
900 char info[80];
901 int info_len = 0;
902 unsigned i;
903
904 for (i=0; i<sess_info.stream_cnt; ++i) {
905 int len;
906 const char *dir;
907 pjmedia_stream_info *strm_info = &sess_info.stream_info[i];
908
909 switch (strm_info->dir) {
910 case PJMEDIA_DIR_NONE:
911 dir = "inactive";
912 break;
913 case PJMEDIA_DIR_ENCODING:
914 dir = "sendonly";
915 break;
916 case PJMEDIA_DIR_DECODING:
917 dir = "recvonly";
918 break;
919 case PJMEDIA_DIR_ENCODING_DECODING:
920 dir = "sendrecv";
921 break;
922 default:
923 dir = "unknown";
924 break;
925 }
926 len = pj_ansi_sprintf( info+info_len,
927 ", stream #%d: %.*s (%s)", i,
928 (int)strm_info->fmt.encoding_name.slen,
929 strm_info->fmt.encoding_name.ptr,
930 dir);
931 if (len > 0)
932 info_len += len;
933 }
934 PJ_LOG(4,(THIS_FILE,"Media updates%s", info));
935 }
936
937 return PJ_SUCCESS;
938}
939
940
Benny Prijonoeebe9af2006-06-13 22:57:13 +0000941/*
942 * Get maxinum number of conference ports.
943 */
944PJ_DEF(unsigned) pjsua_conf_get_max_ports(void)
945{
946 return pjsua_var.media_cfg.max_media_ports;
947}
948
949
950/*
951 * Get current number of active ports in the bridge.
952 */
953PJ_DEF(unsigned) pjsua_conf_get_active_ports(void)
954{
955 unsigned ports[256];
956 unsigned count = PJ_ARRAY_SIZE(ports);
957 pj_status_t status;
958
959 status = pjmedia_conf_enum_ports(pjsua_var.mconf, ports, &count);
960 if (status != PJ_SUCCESS)
961 count = 0;
962
963 return count;
964}
965
966
967/*
968 * Enumerate all conference ports.
969 */
970PJ_DEF(pj_status_t) pjsua_enum_conf_ports(pjsua_conf_port_id id[],
971 unsigned *count)
972{
973 return pjmedia_conf_enum_ports(pjsua_var.mconf, (unsigned*)id, count);
974}
975
976
977/*
978 * Get information about the specified conference port
979 */
980PJ_DEF(pj_status_t) pjsua_conf_get_port_info( pjsua_conf_port_id id,
981 pjsua_conf_port_info *info)
982{
983 pjmedia_conf_port_info cinfo;
Benny Prijono0498d902006-06-19 14:49:14 +0000984 unsigned i;
Benny Prijonoeebe9af2006-06-13 22:57:13 +0000985 pj_status_t status;
986
987 status = pjmedia_conf_get_port_info( pjsua_var.mconf, id, &cinfo);
988 if (status != PJ_SUCCESS)
989 return status;
990
Benny Prijonoac623b32006-07-03 15:19:31 +0000991 pj_bzero(info, sizeof(*info));
Benny Prijonoeebe9af2006-06-13 22:57:13 +0000992 info->slot_id = id;
993 info->name = cinfo.name;
994 info->clock_rate = cinfo.clock_rate;
995 info->channel_count = cinfo.channel_count;
996 info->samples_per_frame = cinfo.samples_per_frame;
997 info->bits_per_sample = cinfo.bits_per_sample;
998
999 /* Build array of listeners */
Benny Prijonoc78c3a32006-06-16 15:54:43 +00001000 info->listener_cnt = cinfo.listener_cnt;
1001 for (i=0; i<cinfo.listener_cnt; ++i) {
1002 info->listeners[i] = cinfo.listener_slots[i];
Benny Prijonoeebe9af2006-06-13 22:57:13 +00001003 }
1004
1005 return PJ_SUCCESS;
1006}
1007
1008
1009/*
Benny Prijonoe909eac2006-07-27 22:04:56 +00001010 * Add arbitrary media port to PJSUA's conference bridge.
1011 */
1012PJ_DEF(pj_status_t) pjsua_conf_add_port( pj_pool_t *pool,
1013 pjmedia_port *port,
1014 pjsua_conf_port_id *p_id)
1015{
1016 pj_status_t status;
1017
1018 status = pjmedia_conf_add_port(pjsua_var.mconf, pool,
1019 port, NULL, (unsigned*)p_id);
1020 if (status != PJ_SUCCESS) {
1021 if (p_id)
1022 *p_id = PJSUA_INVALID_ID;
1023 }
1024
1025 return status;
1026}
1027
1028
1029/*
1030 * Remove arbitrary slot from the conference bridge.
1031 */
1032PJ_DEF(pj_status_t) pjsua_conf_remove_port(pjsua_conf_port_id id)
1033{
1034 return pjmedia_conf_remove_port(pjsua_var.mconf, (unsigned)id);
1035}
1036
1037
1038/*
Benny Prijonoeebe9af2006-06-13 22:57:13 +00001039 * Establish unidirectional media flow from souce to sink.
1040 */
1041PJ_DEF(pj_status_t) pjsua_conf_connect( pjsua_conf_port_id source,
1042 pjsua_conf_port_id sink)
1043{
1044 return pjmedia_conf_connect_port(pjsua_var.mconf, source, sink, 0);
1045}
1046
1047
1048/*
1049 * Disconnect media flow from the source to destination port.
1050 */
1051PJ_DEF(pj_status_t) pjsua_conf_disconnect( pjsua_conf_port_id source,
1052 pjsua_conf_port_id sink)
1053{
1054 return pjmedia_conf_disconnect_port(pjsua_var.mconf, source, sink);
1055}
1056
1057
Benny Prijono6dd967c2006-12-26 02:27:14 +00001058/*
1059 * Adjust the signal level to be transmitted from the bridge to the
1060 * specified port by making it louder or quieter.
1061 */
1062PJ_DEF(pj_status_t) pjsua_conf_adjust_tx_level(pjsua_conf_port_id slot,
1063 float level)
1064{
1065 return pjmedia_conf_adjust_tx_level(pjsua_var.mconf, slot,
1066 (int)((level-1) * 128));
1067}
1068
1069/*
1070 * Adjust the signal level to be received from the specified port (to
1071 * the bridge) by making it louder or quieter.
1072 */
1073PJ_DEF(pj_status_t) pjsua_conf_adjust_rx_level(pjsua_conf_port_id slot,
1074 float level)
1075{
1076 return pjmedia_conf_adjust_rx_level(pjsua_var.mconf, slot,
1077 (int)((level-1) * 128));
1078}
1079
1080
1081/*
1082 * Get last signal level transmitted to or received from the specified port.
1083 */
1084PJ_DEF(pj_status_t) pjsua_conf_get_signal_level(pjsua_conf_port_id slot,
1085 unsigned *tx_level,
1086 unsigned *rx_level)
1087{
1088 return pjmedia_conf_get_signal_level(pjsua_var.mconf, slot,
1089 tx_level, rx_level);
1090}
1091
Benny Prijonoeebe9af2006-06-13 22:57:13 +00001092/*****************************************************************************
1093 * File player.
1094 */
1095
1096/*
1097 * Create a file player, and automatically connect this player to
1098 * the conference bridge.
1099 */
1100PJ_DEF(pj_status_t) pjsua_player_create( const pj_str_t *filename,
1101 unsigned options,
Benny Prijonoeebe9af2006-06-13 22:57:13 +00001102 pjsua_player_id *p_id)
1103{
1104 unsigned slot, file_id;
Benny Prijonoa66c3312007-01-21 23:12:40 +00001105 char path[PJ_MAXPATH];
Benny Prijonoeebe9af2006-06-13 22:57:13 +00001106 pjmedia_port *port;
1107 pj_status_t status;
1108
1109 if (pjsua_var.player_cnt >= PJ_ARRAY_SIZE(pjsua_var.player))
1110 return PJ_ETOOMANY;
1111
1112 PJSUA_LOCK();
1113
1114 for (file_id=0; file_id<PJ_ARRAY_SIZE(pjsua_var.player); ++file_id) {
1115 if (pjsua_var.player[file_id].port == NULL)
1116 break;
1117 }
1118
1119 if (file_id == PJ_ARRAY_SIZE(pjsua_var.player)) {
1120 /* This is unexpected */
1121 PJSUA_UNLOCK();
1122 pj_assert(0);
1123 return PJ_EBUG;
1124 }
1125
1126 pj_memcpy(path, filename->ptr, filename->slen);
1127 path[filename->slen] = '\0';
1128 status = pjmedia_wav_player_port_create(pjsua_var.pool, path,
1129 pjsua_var.mconf_cfg.samples_per_frame *
1130 1000 / pjsua_var.media_cfg.clock_rate,
Benny Prijono00cae612006-07-31 15:19:36 +00001131 options, 0, &port);
Benny Prijonoeebe9af2006-06-13 22:57:13 +00001132 if (status != PJ_SUCCESS) {
1133 PJSUA_UNLOCK();
1134 pjsua_perror(THIS_FILE, "Unable to open file for playback", status);
1135 return status;
1136 }
1137
1138 status = pjmedia_conf_add_port(pjsua_var.mconf, pjsua_var.pool,
1139 port, filename, &slot);
1140 if (status != PJ_SUCCESS) {
1141 pjmedia_port_destroy(port);
1142 PJSUA_UNLOCK();
Benny Prijono32e4f492007-01-24 00:44:26 +00001143 pjsua_perror(THIS_FILE, "Unable to add file to conference bridge",
1144 status);
Benny Prijonoeebe9af2006-06-13 22:57:13 +00001145 return status;
1146 }
1147
Benny Prijonoa66c3312007-01-21 23:12:40 +00001148 pjsua_var.player[file_id].type = 0;
Benny Prijonoeebe9af2006-06-13 22:57:13 +00001149 pjsua_var.player[file_id].port = port;
1150 pjsua_var.player[file_id].slot = slot;
1151
1152 if (p_id) *p_id = file_id;
1153
1154 ++pjsua_var.player_cnt;
1155
1156 PJSUA_UNLOCK();
1157 return PJ_SUCCESS;
1158}
1159
1160
1161/*
Benny Prijonoa66c3312007-01-21 23:12:40 +00001162 * Create a file playlist media port, and automatically add the port
1163 * to the conference bridge.
1164 */
1165PJ_DEF(pj_status_t) pjsua_playlist_create( const pj_str_t file_names[],
1166 unsigned file_count,
1167 const pj_str_t *label,
1168 unsigned options,
1169 pjsua_player_id *p_id)
1170{
1171 unsigned slot, file_id, ptime;
1172 pjmedia_port *port;
1173 pj_status_t status;
1174
1175 if (pjsua_var.player_cnt >= PJ_ARRAY_SIZE(pjsua_var.player))
1176 return PJ_ETOOMANY;
1177
1178 PJSUA_LOCK();
1179
1180 for (file_id=0; file_id<PJ_ARRAY_SIZE(pjsua_var.player); ++file_id) {
1181 if (pjsua_var.player[file_id].port == NULL)
1182 break;
1183 }
1184
1185 if (file_id == PJ_ARRAY_SIZE(pjsua_var.player)) {
1186 /* This is unexpected */
1187 PJSUA_UNLOCK();
1188 pj_assert(0);
1189 return PJ_EBUG;
1190 }
1191
1192
1193 ptime = pjsua_var.mconf_cfg.samples_per_frame * 1000 /
1194 pjsua_var.media_cfg.clock_rate;
1195
1196 status = pjmedia_wav_playlist_create(pjsua_var.pool, label,
1197 file_names, file_count,
1198 ptime, options, 0, &port);
1199 if (status != PJ_SUCCESS) {
1200 PJSUA_UNLOCK();
1201 pjsua_perror(THIS_FILE, "Unable to create playlist", status);
1202 return status;
1203 }
1204
1205 status = pjmedia_conf_add_port(pjsua_var.mconf, pjsua_var.pool,
1206 port, &port->info.name, &slot);
1207 if (status != PJ_SUCCESS) {
1208 pjmedia_port_destroy(port);
1209 PJSUA_UNLOCK();
1210 pjsua_perror(THIS_FILE, "Unable to add port", status);
1211 return status;
1212 }
1213
1214 pjsua_var.player[file_id].type = 1;
1215 pjsua_var.player[file_id].port = port;
1216 pjsua_var.player[file_id].slot = slot;
1217
1218 if (p_id) *p_id = file_id;
1219
1220 ++pjsua_var.player_cnt;
1221
1222 PJSUA_UNLOCK();
1223 return PJ_SUCCESS;
1224
1225}
1226
1227
1228/*
Benny Prijonoeebe9af2006-06-13 22:57:13 +00001229 * Get conference port ID associated with player.
1230 */
1231PJ_DEF(pjsua_conf_port_id) pjsua_player_get_conf_port(pjsua_player_id id)
1232{
1233 PJ_ASSERT_RETURN(id>=0 && id<PJ_ARRAY_SIZE(pjsua_var.player), PJ_EINVAL);
1234 PJ_ASSERT_RETURN(pjsua_var.player[id].port != NULL, PJ_EINVAL);
1235
1236 return pjsua_var.player[id].slot;
1237}
1238
Benny Prijono469b1522006-12-26 03:05:17 +00001239/*
1240 * Get the media port for the player.
1241 */
1242PJ_DEF(pj_status_t) pjsua_player_get_port( pjsua_recorder_id id,
1243 pjmedia_port **p_port)
1244{
1245 PJ_ASSERT_RETURN(id>=0 && id<PJ_ARRAY_SIZE(pjsua_var.player), PJ_EINVAL);
1246 PJ_ASSERT_RETURN(pjsua_var.player[id].port != NULL, PJ_EINVAL);
1247 PJ_ASSERT_RETURN(p_port != NULL, PJ_EINVAL);
1248
1249 *p_port = pjsua_var.player[id].port;
1250
1251 return PJ_SUCCESS;
1252}
Benny Prijonoeebe9af2006-06-13 22:57:13 +00001253
1254/*
1255 * Set playback position.
1256 */
1257PJ_DEF(pj_status_t) pjsua_player_set_pos( pjsua_player_id id,
1258 pj_uint32_t samples)
1259{
1260 PJ_ASSERT_RETURN(id>=0 && id<PJ_ARRAY_SIZE(pjsua_var.player), PJ_EINVAL);
1261 PJ_ASSERT_RETURN(pjsua_var.player[id].port != NULL, PJ_EINVAL);
Benny Prijonoa66c3312007-01-21 23:12:40 +00001262 PJ_ASSERT_RETURN(pjsua_var.player[id].type == 0, PJ_EINVAL);
Benny Prijonoeebe9af2006-06-13 22:57:13 +00001263
1264 return pjmedia_wav_player_port_set_pos(pjsua_var.player[id].port, samples);
1265}
1266
1267
1268/*
1269 * Close the file, remove the player from the bridge, and free
1270 * resources associated with the file player.
1271 */
1272PJ_DEF(pj_status_t) pjsua_player_destroy(pjsua_player_id id)
1273{
1274 PJ_ASSERT_RETURN(id>=0 && id<PJ_ARRAY_SIZE(pjsua_var.player), PJ_EINVAL);
1275 PJ_ASSERT_RETURN(pjsua_var.player[id].port != NULL, PJ_EINVAL);
1276
1277 PJSUA_LOCK();
1278
1279 if (pjsua_var.player[id].port) {
1280 pjmedia_conf_remove_port(pjsua_var.mconf,
1281 pjsua_var.player[id].slot);
1282 pjmedia_port_destroy(pjsua_var.player[id].port);
1283 pjsua_var.player[id].port = NULL;
1284 pjsua_var.player[id].slot = 0xFFFF;
1285 pjsua_var.player_cnt--;
1286 }
1287
1288 PJSUA_UNLOCK();
1289
1290 return PJ_SUCCESS;
1291}
1292
1293
1294/*****************************************************************************
1295 * File recorder.
1296 */
1297
1298/*
1299 * Create a file recorder, and automatically connect this recorder to
1300 * the conference bridge.
1301 */
1302PJ_DEF(pj_status_t) pjsua_recorder_create( const pj_str_t *filename,
Benny Prijono8f310522006-10-20 11:08:49 +00001303 unsigned enc_type,
1304 void *enc_param,
Benny Prijonoeebe9af2006-06-13 22:57:13 +00001305 pj_ssize_t max_size,
1306 unsigned options,
Benny Prijonoeebe9af2006-06-13 22:57:13 +00001307 pjsua_recorder_id *p_id)
1308{
Benny Prijonob3cdb2b2006-10-19 15:49:47 +00001309 enum Format
1310 {
1311 FMT_UNKNOWN,
1312 FMT_WAV,
1313 FMT_MP3,
1314 };
Benny Prijonoeebe9af2006-06-13 22:57:13 +00001315 unsigned slot, file_id;
Benny Prijonoa66c3312007-01-21 23:12:40 +00001316 char path[PJ_MAXPATH];
Benny Prijonob3cdb2b2006-10-19 15:49:47 +00001317 pj_str_t ext;
Benny Prijono8f310522006-10-20 11:08:49 +00001318 int file_format;
Benny Prijonoeebe9af2006-06-13 22:57:13 +00001319 pjmedia_port *port;
1320 pj_status_t status;
1321
Benny Prijonob3cdb2b2006-10-19 15:49:47 +00001322 /* Filename must present */
1323 PJ_ASSERT_RETURN(filename != NULL, PJ_EINVAL);
1324
Benny Prijono00cae612006-07-31 15:19:36 +00001325 /* Don't support max_size at present */
Benny Prijonob3cdb2b2006-10-19 15:49:47 +00001326 PJ_ASSERT_RETURN(max_size == 0 || max_size == -1, PJ_EINVAL);
Benny Prijono00cae612006-07-31 15:19:36 +00001327
Benny Prijono8f310522006-10-20 11:08:49 +00001328 /* Don't support encoding type at present */
1329 PJ_ASSERT_RETURN(enc_type == 0, PJ_EINVAL);
Benny Prijono00cae612006-07-31 15:19:36 +00001330
Benny Prijonoeebe9af2006-06-13 22:57:13 +00001331 if (pjsua_var.rec_cnt >= PJ_ARRAY_SIZE(pjsua_var.recorder))
1332 return PJ_ETOOMANY;
1333
Benny Prijonob3cdb2b2006-10-19 15:49:47 +00001334 /* Determine the file format */
1335 ext.ptr = filename->ptr + filename->slen - 4;
1336 ext.slen = 4;
1337
1338 if (pj_stricmp2(&ext, ".wav") == 0)
1339 file_format = FMT_WAV;
1340 else if (pj_stricmp2(&ext, ".mp3") == 0)
1341 file_format = FMT_MP3;
1342 else {
1343 PJ_LOG(1,(THIS_FILE, "pjsua_recorder_create() error: unable to "
1344 "determine file format for %.*s",
1345 (int)filename->slen, filename->ptr));
1346 return PJ_ENOTSUP;
1347 }
1348
Benny Prijonoeebe9af2006-06-13 22:57:13 +00001349 PJSUA_LOCK();
1350
1351 for (file_id=0; file_id<PJ_ARRAY_SIZE(pjsua_var.recorder); ++file_id) {
1352 if (pjsua_var.recorder[file_id].port == NULL)
1353 break;
1354 }
1355
1356 if (file_id == PJ_ARRAY_SIZE(pjsua_var.recorder)) {
1357 /* This is unexpected */
1358 PJSUA_UNLOCK();
1359 pj_assert(0);
1360 return PJ_EBUG;
1361 }
1362
1363 pj_memcpy(path, filename->ptr, filename->slen);
1364 path[filename->slen] = '\0';
Benny Prijonob3cdb2b2006-10-19 15:49:47 +00001365
1366 if (file_format == FMT_WAV) {
1367 status = pjmedia_wav_writer_port_create(pjsua_var.pool, path,
1368 pjsua_var.media_cfg.clock_rate,
1369 pjsua_var.mconf_cfg.channel_count,
1370 pjsua_var.mconf_cfg.samples_per_frame,
1371 pjsua_var.mconf_cfg.bits_per_sample,
1372 options, 0, &port);
1373 } else if (file_format == FMT_MP3) {
1374 status = pjmedia_mp3_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,
Benny Prijono8f310522006-10-20 11:08:49 +00001379 enc_param, &port);
Benny Prijonob3cdb2b2006-10-19 15:49:47 +00001380 } else {
1381 port = NULL;
1382 status = PJ_ENOTSUP;
1383 }
1384
Benny Prijonoeebe9af2006-06-13 22:57:13 +00001385 if (status != PJ_SUCCESS) {
1386 PJSUA_UNLOCK();
1387 pjsua_perror(THIS_FILE, "Unable to open file for recording", status);
1388 return status;
1389 }
1390
1391 status = pjmedia_conf_add_port(pjsua_var.mconf, pjsua_var.pool,
1392 port, filename, &slot);
1393 if (status != PJ_SUCCESS) {
1394 pjmedia_port_destroy(port);
1395 PJSUA_UNLOCK();
1396 return status;
1397 }
1398
1399 pjsua_var.recorder[file_id].port = port;
1400 pjsua_var.recorder[file_id].slot = slot;
1401
1402 if (p_id) *p_id = file_id;
1403
1404 ++pjsua_var.rec_cnt;
1405
1406 PJSUA_UNLOCK();
1407 return PJ_SUCCESS;
1408}
1409
1410
1411/*
1412 * Get conference port associated with recorder.
1413 */
1414PJ_DEF(pjsua_conf_port_id) pjsua_recorder_get_conf_port(pjsua_recorder_id id)
1415{
1416 PJ_ASSERT_RETURN(id>=0 && id<PJ_ARRAY_SIZE(pjsua_var.recorder), PJ_EINVAL);
1417 PJ_ASSERT_RETURN(pjsua_var.recorder[id].port != NULL, PJ_EINVAL);
1418
1419 return pjsua_var.recorder[id].slot;
1420}
1421
Benny Prijono469b1522006-12-26 03:05:17 +00001422/*
1423 * Get the media port for the recorder.
1424 */
1425PJ_DEF(pj_status_t) pjsua_recorder_get_port( pjsua_recorder_id id,
1426 pjmedia_port **p_port)
1427{
1428 PJ_ASSERT_RETURN(id>=0 && id<PJ_ARRAY_SIZE(pjsua_var.recorder), PJ_EINVAL);
1429 PJ_ASSERT_RETURN(pjsua_var.recorder[id].port != NULL, PJ_EINVAL);
1430 PJ_ASSERT_RETURN(p_port != NULL, PJ_EINVAL);
1431
1432 *p_port = pjsua_var.recorder[id].port;
1433 return PJ_SUCCESS;
1434}
Benny Prijonoeebe9af2006-06-13 22:57:13 +00001435
1436/*
1437 * Destroy recorder (this will complete recording).
1438 */
1439PJ_DEF(pj_status_t) pjsua_recorder_destroy(pjsua_recorder_id id)
1440{
1441 PJ_ASSERT_RETURN(id>=0 && id<PJ_ARRAY_SIZE(pjsua_var.recorder), PJ_EINVAL);
1442 PJ_ASSERT_RETURN(pjsua_var.recorder[id].port != NULL, PJ_EINVAL);
1443
1444 PJSUA_LOCK();
1445
1446 if (pjsua_var.recorder[id].port) {
1447 pjmedia_conf_remove_port(pjsua_var.mconf,
1448 pjsua_var.recorder[id].slot);
1449 pjmedia_port_destroy(pjsua_var.recorder[id].port);
1450 pjsua_var.recorder[id].port = NULL;
1451 pjsua_var.recorder[id].slot = 0xFFFF;
1452 pjsua_var.rec_cnt--;
1453 }
1454
1455 PJSUA_UNLOCK();
1456
1457 return PJ_SUCCESS;
1458}
1459
1460
1461/*****************************************************************************
1462 * Sound devices.
1463 */
1464
1465/*
1466 * Enum sound devices.
1467 */
1468PJ_DEF(pj_status_t) pjsua_enum_snd_devs( pjmedia_snd_dev_info info[],
1469 unsigned *count)
1470{
1471 unsigned i, dev_count;
1472
1473 dev_count = pjmedia_snd_get_dev_count();
1474
1475 if (dev_count > *count) dev_count = *count;
1476
1477 for (i=0; i<dev_count; ++i) {
1478 const pjmedia_snd_dev_info *ci;
1479
1480 ci = pjmedia_snd_get_dev_info(i);
1481 pj_memcpy(&info[i], ci, sizeof(*ci));
1482 }
1483
1484 *count = dev_count;
1485
1486 return PJ_SUCCESS;
1487}
1488
1489
1490/* Close existing sound device */
1491static void close_snd_dev(void)
1492{
1493 /* Close sound device */
1494 if (pjsua_var.snd_port) {
1495 const pjmedia_snd_dev_info *cap_info, *play_info;
1496
1497 cap_info = pjmedia_snd_get_dev_info(pjsua_var.cap_dev);
1498 play_info = pjmedia_snd_get_dev_info(pjsua_var.play_dev);
1499
1500 PJ_LOG(4,(THIS_FILE, "Closing %s sound playback device and "
1501 "%s sound capture device",
1502 play_info->name, cap_info->name));
1503
1504 pjmedia_snd_port_disconnect(pjsua_var.snd_port);
1505 pjmedia_snd_port_destroy(pjsua_var.snd_port);
1506 pjsua_var.snd_port = NULL;
1507 }
1508
1509 /* Close null sound device */
1510 if (pjsua_var.null_snd) {
1511 PJ_LOG(4,(THIS_FILE, "Closing null sound device.."));
1512 pjmedia_master_port_destroy(pjsua_var.null_snd, PJ_FALSE);
1513 pjsua_var.null_snd = NULL;
1514 }
1515}
1516
1517/*
1518 * Select or change sound device. Application may call this function at
1519 * any time to replace current sound device.
1520 */
1521PJ_DEF(pj_status_t) pjsua_set_snd_dev( int capture_dev,
1522 int playback_dev)
1523{
1524 pjmedia_port *conf_port;
Benny Prijono6dd967c2006-12-26 02:27:14 +00001525 const pjmedia_snd_dev_info *play_info;
Benny Prijono26056d82006-10-11 16:03:41 +00001526 unsigned clock_rates[] = { 0, 22050, 44100, 48000, 11025, 32000, 8000};
Benny Prijono658a1c52006-10-11 21:56:16 +00001527 unsigned selected_clock_rate = 0;
Benny Prijono26056d82006-10-11 16:03:41 +00001528 unsigned i;
Benny Prijonoc53c6d72006-11-27 09:54:03 +00001529 pjmedia_snd_stream *strm;
1530 pjmedia_snd_stream_info si;
1531 pj_str_t tmp;
Benny Prijono26056d82006-10-11 16:03:41 +00001532 pj_status_t status = -1;
Benny Prijonoeebe9af2006-06-13 22:57:13 +00001533
1534 /* Close existing sound port */
1535 close_snd_dev();
1536
1537
Benny Prijono26056d82006-10-11 16:03:41 +00001538 /* Set default clock rate */
1539 clock_rates[0] = pjsua_var.media_cfg.clock_rate;
Benny Prijonoeebe9af2006-06-13 22:57:13 +00001540
Benny Prijono26056d82006-10-11 16:03:41 +00001541 /* Attempts to open the sound device with different clock rates */
1542 for (i=0; i<PJ_ARRAY_SIZE(clock_rates); ++i) {
1543 char errmsg[PJ_ERR_MSG_SIZE];
1544
1545 PJ_LOG(4,(THIS_FILE,
1546 "pjsua_set_snd_dev(): attempting to open devices "
1547 "@%d Hz", clock_rates[i]));
1548
1549 /* Create the sound device. Sound port will start immediately. */
1550 status = pjmedia_snd_port_create(pjsua_var.pool, capture_dev,
1551 playback_dev,
1552 clock_rates[i], 1,
1553 clock_rates[i]/FPS,
1554 16, 0, &pjsua_var.snd_port);
1555
Benny Prijono658a1c52006-10-11 21:56:16 +00001556 if (status == PJ_SUCCESS) {
1557 selected_clock_rate = clock_rates[i];
Benny Prijono26056d82006-10-11 16:03:41 +00001558 break;
Benny Prijono658a1c52006-10-11 21:56:16 +00001559 }
Benny Prijono26056d82006-10-11 16:03:41 +00001560
1561 pj_strerror(status, errmsg, sizeof(errmsg));
Benny Prijono658a1c52006-10-11 21:56:16 +00001562 PJ_LOG(4, (THIS_FILE, "..failed: %s", errmsg));
Benny Prijono26056d82006-10-11 16:03:41 +00001563 }
1564
Benny Prijonoeebe9af2006-06-13 22:57:13 +00001565 if (status != PJ_SUCCESS) {
1566 pjsua_perror(THIS_FILE, "Unable to open sound device", status);
1567 return status;
1568 }
1569
1570 /* Get the port0 of the conference bridge. */
1571 conf_port = pjmedia_conf_get_master_port(pjsua_var.mconf);
1572 pj_assert(conf_port != NULL);
1573
Benny Prijonof20687a2006-08-04 18:27:19 +00001574 /* Set AEC */
Benny Prijono5da50432006-08-07 10:24:52 +00001575 pjmedia_snd_port_set_ec( pjsua_var.snd_port, pjsua_var.pool,
1576 pjsua_var.media_cfg.ec_tail_len,
1577 pjsua_var.media_cfg.ec_options);
Benny Prijonoeebe9af2006-06-13 22:57:13 +00001578
Benny Prijono658a1c52006-10-11 21:56:16 +00001579 /* If there's mismatch between sound port and conference's port,
1580 * create a resample port to bridge them.
1581 */
1582 if (selected_clock_rate != pjsua_var.media_cfg.clock_rate) {
1583 pjmedia_port *resample_port;
1584
1585 status = pjmedia_resample_port_create(pjsua_var.pool, conf_port,
1586 selected_clock_rate, 0,
1587 &resample_port);
1588 if (status != PJ_SUCCESS) {
1589 pjsua_perror("Error creating resample port", THIS_FILE, status);
1590 return status;
1591 }
1592
1593 conf_port = resample_port;
1594 }
1595
Benny Prijono52a93912006-08-04 20:54:37 +00001596 /* Connect sound port to the bridge */
1597 status = pjmedia_snd_port_connect(pjsua_var.snd_port,
1598 conf_port );
1599 if (status != PJ_SUCCESS) {
1600 pjsua_perror(THIS_FILE, "Unable to connect conference port to "
1601 "sound device", status);
1602 pjmedia_snd_port_destroy(pjsua_var.snd_port);
1603 pjsua_var.snd_port = NULL;
1604 return status;
1605 }
1606
Benny Prijonoeebe9af2006-06-13 22:57:13 +00001607 /* Save the device IDs */
1608 pjsua_var.cap_dev = capture_dev;
1609 pjsua_var.play_dev = playback_dev;
1610
Benny Prijonoc53c6d72006-11-27 09:54:03 +00001611 /* Update sound device name. */
1612 strm = pjmedia_snd_port_get_snd_stream(pjsua_var.snd_port);
1613 pjmedia_snd_stream_get_info(strm, &si);
1614 play_info = pjmedia_snd_get_dev_info(si.rec_id);
1615
1616 pjmedia_conf_set_port0_name(pjsua_var.mconf,
1617 pj_cstr(&tmp, play_info->name));
1618
Benny Prijonoeebe9af2006-06-13 22:57:13 +00001619 return PJ_SUCCESS;
1620}
1621
1622
1623/*
Benny Prijonoebdf8772007-02-01 19:25:50 +00001624 * Get currently active sound devices. If sound devices has not been created
1625 * (for example when pjsua_start() is not called), it is possible that
1626 * the function returns PJ_SUCCESS with -1 as device IDs.
1627 */
1628PJ_DEF(pj_status_t) pjsua_get_snd_dev(int *capture_dev,
1629 int *playback_dev)
1630{
1631 if (capture_dev) {
1632 *capture_dev = pjsua_var.cap_dev;
1633 }
1634 if (playback_dev) {
1635 *playback_dev = pjsua_var.play_dev;
1636 }
1637
1638 return PJ_SUCCESS;
1639}
1640
1641
1642/*
Benny Prijonoeebe9af2006-06-13 22:57:13 +00001643 * Use null sound device.
1644 */
1645PJ_DEF(pj_status_t) pjsua_set_null_snd_dev(void)
1646{
1647 pjmedia_port *conf_port;
1648 pj_status_t status;
1649
1650 /* Close existing sound device */
1651 close_snd_dev();
1652
1653 PJ_LOG(4,(THIS_FILE, "Opening null sound device.."));
1654
1655 /* Get the port0 of the conference bridge. */
1656 conf_port = pjmedia_conf_get_master_port(pjsua_var.mconf);
1657 pj_assert(conf_port != NULL);
1658
1659 /* Create master port, connecting port0 of the conference bridge to
1660 * a null port.
1661 */
1662 status = pjmedia_master_port_create(pjsua_var.pool, pjsua_var.null_port,
1663 conf_port, 0, &pjsua_var.null_snd);
1664 if (status != PJ_SUCCESS) {
1665 pjsua_perror(THIS_FILE, "Unable to create null sound device",
1666 status);
1667 return status;
1668 }
1669
1670 /* Start the master port */
1671 status = pjmedia_master_port_start(pjsua_var.null_snd);
1672 PJ_ASSERT_RETURN(status == PJ_SUCCESS, status);
1673
1674 return PJ_SUCCESS;
1675}
1676
1677
Benny Prijonoe909eac2006-07-27 22:04:56 +00001678
1679/*
1680 * Use no device!
1681 */
1682PJ_DEF(pjmedia_port*) pjsua_set_no_snd_dev(void)
1683{
1684 /* Close existing sound device */
1685 close_snd_dev();
1686
1687 pjsua_var.no_snd = PJ_TRUE;
1688 return pjmedia_conf_get_master_port(pjsua_var.mconf);
1689}
1690
1691
Benny Prijonof20687a2006-08-04 18:27:19 +00001692/*
1693 * Configure the AEC settings of the sound port.
1694 */
Benny Prijono5da50432006-08-07 10:24:52 +00001695PJ_DEF(pj_status_t) pjsua_set_ec(unsigned tail_ms, unsigned options)
Benny Prijonof20687a2006-08-04 18:27:19 +00001696{
1697 pjsua_var.media_cfg.ec_tail_len = tail_ms;
1698
1699 if (pjsua_var.snd_port)
Benny Prijono5da50432006-08-07 10:24:52 +00001700 return pjmedia_snd_port_set_ec( pjsua_var.snd_port, pjsua_var.pool,
1701 tail_ms, options);
Benny Prijonof20687a2006-08-04 18:27:19 +00001702
1703 return PJ_SUCCESS;
1704}
1705
1706
1707/*
1708 * Get current AEC tail length.
1709 */
Benny Prijono22dfe592006-08-06 12:07:13 +00001710PJ_DEF(pj_status_t) pjsua_get_ec_tail(unsigned *p_tail_ms)
Benny Prijonof20687a2006-08-04 18:27:19 +00001711{
1712 *p_tail_ms = pjsua_var.media_cfg.ec_tail_len;
1713 return PJ_SUCCESS;
1714}
1715
Benny Prijonoe909eac2006-07-27 22:04:56 +00001716
Benny Prijonoeebe9af2006-06-13 22:57:13 +00001717/*****************************************************************************
1718 * Codecs.
1719 */
1720
1721/*
1722 * Enum all supported codecs in the system.
1723 */
1724PJ_DEF(pj_status_t) pjsua_enum_codecs( pjsua_codec_info id[],
1725 unsigned *p_count )
1726{
1727 pjmedia_codec_mgr *codec_mgr;
1728 pjmedia_codec_info info[32];
1729 unsigned i, count, prio[32];
1730 pj_status_t status;
1731
1732 codec_mgr = pjmedia_endpt_get_codec_mgr(pjsua_var.med_endpt);
1733 count = PJ_ARRAY_SIZE(info);
1734 status = pjmedia_codec_mgr_enum_codecs( codec_mgr, &count, info, prio);
1735 if (status != PJ_SUCCESS) {
1736 *p_count = 0;
1737 return status;
1738 }
1739
1740 if (count > *p_count) count = *p_count;
1741
1742 for (i=0; i<count; ++i) {
1743 pjmedia_codec_info_to_id(&info[i], id[i].buf_, sizeof(id[i].buf_));
1744 id[i].codec_id = pj_str(id[i].buf_);
1745 id[i].priority = (pj_uint8_t) prio[i];
1746 }
1747
1748 *p_count = count;
1749
1750 return PJ_SUCCESS;
1751}
1752
1753
1754/*
1755 * Change codec priority.
1756 */
1757PJ_DEF(pj_status_t) pjsua_codec_set_priority( const pj_str_t *codec_id,
1758 pj_uint8_t priority )
1759{
1760 pjmedia_codec_mgr *codec_mgr;
1761
1762 codec_mgr = pjmedia_endpt_get_codec_mgr(pjsua_var.med_endpt);
1763
1764 return pjmedia_codec_mgr_set_codec_priority(codec_mgr, codec_id,
1765 priority);
1766}
1767
1768
1769/*
1770 * Get codec parameters.
1771 */
1772PJ_DEF(pj_status_t) pjsua_codec_get_param( const pj_str_t *codec_id,
1773 pjmedia_codec_param *param )
1774{
1775 const pjmedia_codec_info *info;
1776 pjmedia_codec_mgr *codec_mgr;
1777 unsigned count = 1;
1778 pj_status_t status;
1779
1780 codec_mgr = pjmedia_endpt_get_codec_mgr(pjsua_var.med_endpt);
1781
1782 status = pjmedia_codec_mgr_find_codecs_by_id(codec_mgr, codec_id,
1783 &count, &info, NULL);
1784 if (status != PJ_SUCCESS)
1785 return status;
1786
1787 if (count != 1)
1788 return PJ_ENOTFOUND;
1789
1790 status = pjmedia_codec_mgr_get_default_param( codec_mgr, info, param);
1791 return status;
1792}
1793
1794
1795/*
1796 * Set codec parameters.
1797 */
1798PJ_DEF(pj_status_t) pjsua_codec_set_param( const pj_str_t *id,
1799 const pjmedia_codec_param *param)
1800{
Benny Prijono00cae612006-07-31 15:19:36 +00001801 PJ_UNUSED_ARG(id);
1802 PJ_UNUSED_ARG(param);
Benny Prijonoeebe9af2006-06-13 22:57:13 +00001803 PJ_TODO(set_codec_param);
1804 return PJ_SUCCESS;
1805}