blob: 0e58d1002d936bb21064a60cf57a97271ee60d2a [file] [log] [blame]
Benny Prijonoeebe9af2006-06-13 22:57:13 +00001/* $Id$ */
2/*
Benny Prijonoa771a512007-02-19 01:13:53 +00003 * Copyright (C) 2003-2007 Benny Prijono <benny@prijono.org>
Benny Prijonoeebe9af2006-06-13 22:57:13 +00004 *
5 * This program is free software; you can redistribute it and/or modify
6 * it under the terms of the GNU General Public License as published by
7 * the Free Software Foundation; either version 2 of the License, or
8 * (at your option) any later version.
9 *
10 * This program is distributed in the hope that it will be useful,
11 * but WITHOUT ANY WARRANTY; without even the implied warranty of
12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 * GNU General Public License for more details.
14 *
15 * You should have received a copy of the GNU General Public License
16 * along with this program; if not, write to the Free Software
17 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
18 */
19#include <pjsua-lib/pjsua.h>
20#include <pjsua-lib/pjsua_internal.h>
21
22
23#define THIS_FILE "pjsua_media.c"
24
Benny Prijono403e24c2007-01-25 17:13:34 +000025#define PTIME 20
Benny Prijonoeebe9af2006-06-13 22:57:13 +000026#define FPS (1000/PTIME)
27#define DEFAULT_RTP_PORT 4000
28
Benny Prijonode479562007-03-15 10:23:55 +000029/* Next RTP port to be used */
30static pj_uint16_t next_rtp_port;
Benny Prijonoeebe9af2006-06-13 22:57:13 +000031
32/* Close existing sound device */
33static void close_snd_dev(void);
34
35
36/**
37 * Init media subsystems.
38 */
39pj_status_t pjsua_media_subsys_init(const pjsua_media_config *cfg)
40{
Benny Prijonoba5926a2007-05-02 11:29:37 +000041 pj_str_t codec_id = {NULL, 0};
Benny Prijono0498d902006-06-19 14:49:14 +000042 unsigned opt;
Benny Prijonoeebe9af2006-06-13 22:57:13 +000043 pj_status_t status;
44
Benny Prijonofc24e692007-01-27 18:31:51 +000045 /* To suppress warning about unused var when all codecs are disabled */
46 PJ_UNUSED_ARG(codec_id);
47
Benny Prijonoeebe9af2006-06-13 22:57:13 +000048 /* Copy configuration */
49 pj_memcpy(&pjsua_var.media_cfg, cfg, sizeof(*cfg));
50
51 /* Normalize configuration */
Benny Prijonoeebe9af2006-06-13 22:57:13 +000052
53 if (pjsua_var.media_cfg.has_ioqueue &&
54 pjsua_var.media_cfg.thread_cnt == 0)
55 {
56 pjsua_var.media_cfg.thread_cnt = 1;
57 }
58
59 if (pjsua_var.media_cfg.max_media_ports < pjsua_var.ua_cfg.max_calls) {
60 pjsua_var.media_cfg.max_media_ports = pjsua_var.ua_cfg.max_calls + 2;
61 }
62
63 /* Create media endpoint. */
64 status = pjmedia_endpt_create(&pjsua_var.cp.factory,
65 pjsua_var.media_cfg.has_ioqueue? NULL :
66 pjsip_endpt_get_ioqueue(pjsua_var.endpt),
67 pjsua_var.media_cfg.thread_cnt,
68 &pjsua_var.med_endpt);
69 if (status != PJ_SUCCESS) {
70 pjsua_perror(THIS_FILE,
71 "Media stack initialization has returned error",
72 status);
73 return status;
74 }
75
76 /* Register all codecs */
77#if PJMEDIA_HAS_SPEEX_CODEC
78 /* Register speex. */
79 status = pjmedia_codec_speex_init(pjsua_var.med_endpt,
Benny Prijono7ca96da2006-08-07 12:11:40 +000080 0,
Benny Prijono0498d902006-06-19 14:49:14 +000081 pjsua_var.media_cfg.quality,
82 pjsua_var.media_cfg.quality);
Benny Prijonoeebe9af2006-06-13 22:57:13 +000083 if (status != PJ_SUCCESS) {
84 pjsua_perror(THIS_FILE, "Error initializing Speex codec",
85 status);
86 return status;
87 }
Benny Prijono7ca96da2006-08-07 12:11:40 +000088
89 /* Set speex/16000 to higher priority*/
90 codec_id = pj_str("speex/16000");
91 pjmedia_codec_mgr_set_codec_priority(
92 pjmedia_endpt_get_codec_mgr(pjsua_var.med_endpt),
93 &codec_id, PJMEDIA_CODEC_PRIO_NORMAL+2);
94
95 /* Set speex/8000 to next higher priority*/
96 codec_id = pj_str("speex/8000");
97 pjmedia_codec_mgr_set_codec_priority(
98 pjmedia_endpt_get_codec_mgr(pjsua_var.med_endpt),
99 &codec_id, PJMEDIA_CODEC_PRIO_NORMAL+1);
100
101
102
Benny Prijonoeebe9af2006-06-13 22:57:13 +0000103#endif /* PJMEDIA_HAS_SPEEX_CODEC */
104
Benny Prijono00cae612006-07-31 15:19:36 +0000105#if PJMEDIA_HAS_ILBC_CODEC
106 /* Register iLBC. */
107 status = pjmedia_codec_ilbc_init( pjsua_var.med_endpt,
108 pjsua_var.media_cfg.ilbc_mode);
109 if (status != PJ_SUCCESS) {
110 pjsua_perror(THIS_FILE, "Error initializing iLBC codec",
111 status);
112 return status;
113 }
114#endif /* PJMEDIA_HAS_ILBC_CODEC */
115
Benny Prijonoeebe9af2006-06-13 22:57:13 +0000116#if PJMEDIA_HAS_GSM_CODEC
117 /* Register GSM */
118 status = pjmedia_codec_gsm_init(pjsua_var.med_endpt);
119 if (status != PJ_SUCCESS) {
120 pjsua_perror(THIS_FILE, "Error initializing GSM codec",
121 status);
122 return status;
123 }
124#endif /* PJMEDIA_HAS_GSM_CODEC */
125
126#if PJMEDIA_HAS_G711_CODEC
127 /* Register PCMA and PCMU */
128 status = pjmedia_codec_g711_init(pjsua_var.med_endpt);
129 if (status != PJ_SUCCESS) {
130 pjsua_perror(THIS_FILE, "Error initializing G711 codec",
131 status);
132 return status;
133 }
134#endif /* PJMEDIA_HAS_G711_CODEC */
135
136#if PJMEDIA_HAS_L16_CODEC
137 /* Register L16 family codecs, but disable all */
138 status = pjmedia_codec_l16_init(pjsua_var.med_endpt, 0);
139 if (status != PJ_SUCCESS) {
140 pjsua_perror(THIS_FILE, "Error initializing L16 codecs",
141 status);
142 return status;
143 }
144
145 /* Disable ALL L16 codecs */
146 codec_id = pj_str("L16");
147 pjmedia_codec_mgr_set_codec_priority(
148 pjmedia_endpt_get_codec_mgr(pjsua_var.med_endpt),
149 &codec_id, PJMEDIA_CODEC_PRIO_DISABLED);
150
151#endif /* PJMEDIA_HAS_L16_CODEC */
152
153
154 /* Save additional conference bridge parameters for future
155 * reference.
156 */
157 pjsua_var.mconf_cfg.samples_per_frame = pjsua_var.media_cfg.clock_rate *
158 PTIME / 1000;
159 pjsua_var.mconf_cfg.channel_count = 1;
160 pjsua_var.mconf_cfg.bits_per_sample = 16;
161
Benny Prijono0498d902006-06-19 14:49:14 +0000162 /* Init options for conference bridge. */
163 opt = PJMEDIA_CONF_NO_DEVICE;
164 if (pjsua_var.media_cfg.quality >= 3 &&
Benny Prijono7ca96da2006-08-07 12:11:40 +0000165 pjsua_var.media_cfg.quality <= 4)
Benny Prijono0498d902006-06-19 14:49:14 +0000166 {
167 opt |= PJMEDIA_CONF_SMALL_FILTER;
168 }
169 else if (pjsua_var.media_cfg.quality < 3) {
170 opt |= PJMEDIA_CONF_USE_LINEAR;
171 }
172
173
Benny Prijonoeebe9af2006-06-13 22:57:13 +0000174 /* Init conference bridge. */
175 status = pjmedia_conf_create(pjsua_var.pool,
176 pjsua_var.media_cfg.max_media_ports,
177 pjsua_var.media_cfg.clock_rate,
178 pjsua_var.mconf_cfg.channel_count,
179 pjsua_var.mconf_cfg.samples_per_frame,
180 pjsua_var.mconf_cfg.bits_per_sample,
Benny Prijono0498d902006-06-19 14:49:14 +0000181 opt, &pjsua_var.mconf);
Benny Prijonoeebe9af2006-06-13 22:57:13 +0000182 if (status != PJ_SUCCESS) {
183 pjsua_perror(THIS_FILE,
184 "Media stack initialization has returned error",
185 status);
186 return status;
187 }
188
189 /* Create null port just in case user wants to use null sound. */
190 status = pjmedia_null_port_create(pjsua_var.pool,
191 pjsua_var.media_cfg.clock_rate,
192 pjsua_var.mconf_cfg.channel_count,
193 pjsua_var.mconf_cfg.samples_per_frame,
194 pjsua_var.mconf_cfg.bits_per_sample,
195 &pjsua_var.null_port);
196 PJ_ASSERT_RETURN(status == PJ_SUCCESS, status);
197
198 return PJ_SUCCESS;
199}
200
201
202/*
203 * Create RTP and RTCP socket pair, and possibly resolve their public
204 * address via STUN.
205 */
206static pj_status_t create_rtp_rtcp_sock(const pjsua_transport_config *cfg,
207 pjmedia_sock_info *skinfo)
208{
209 enum {
210 RTP_RETRY = 100
211 };
212 int i;
Benny Prijono0a5cad82006-09-26 13:21:02 +0000213 pj_sockaddr_in bound_addr;
Benny Prijonoeebe9af2006-06-13 22:57:13 +0000214 pj_sockaddr_in mapped_addr[2];
215 pj_status_t status = PJ_SUCCESS;
216 pj_sock_t sock[2];
217
Benny Prijonoc97608e2007-03-23 16:34:20 +0000218 /* Make sure STUN server resolution has completed */
219 status = pjsua_resolve_stun_server(PJ_TRUE);
220 if (status != PJ_SUCCESS) {
221 pjsua_perror(THIS_FILE, "Error resolving STUN server", status);
222 return status;
223 }
224
225
Benny Prijonode479562007-03-15 10:23:55 +0000226 if (next_rtp_port == 0)
227 next_rtp_port = (pj_uint16_t)cfg->port;
Benny Prijonoeebe9af2006-06-13 22:57:13 +0000228
229 for (i=0; i<2; ++i)
230 sock[i] = PJ_INVALID_SOCKET;
231
Benny Prijono0a5cad82006-09-26 13:21:02 +0000232 bound_addr.sin_addr.s_addr = PJ_INADDR_ANY;
233 if (cfg->bound_addr.slen) {
234 status = pj_sockaddr_in_set_str_addr(&bound_addr, &cfg->bound_addr);
235 if (status != PJ_SUCCESS) {
236 pjsua_perror(THIS_FILE, "Unable to resolve transport bind address",
237 status);
238 return status;
239 }
240 }
Benny Prijonoeebe9af2006-06-13 22:57:13 +0000241
242 /* Loop retry to bind RTP and RTCP sockets. */
Benny Prijonode479562007-03-15 10:23:55 +0000243 for (i=0; i<RTP_RETRY; ++i, next_rtp_port += 2) {
Benny Prijonoeebe9af2006-06-13 22:57:13 +0000244
245 /* Create and bind RTP socket. */
Benny Prijono8ab968f2007-07-20 08:08:30 +0000246 status = pj_sock_socket(pj_AF_INET(), pj_SOCK_DGRAM(), 0, &sock[0]);
Benny Prijonoeebe9af2006-06-13 22:57:13 +0000247 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. */
Benny Prijono8ab968f2007-07-20 08:08:30 +0000261 status = pj_sock_socket(pj_AF_INET(), pj_SOCK_DGRAM(), 0, &sock[1]);
Benny Prijonoeebe9af2006-06-13 22:57:13 +0000262 if (status != PJ_SUCCESS) {
263 pjsua_perror(THIS_FILE, "socket() error", status);
264 pj_sock_close(sock[0]);
265 return status;
266 }
267
Benny Prijono0a5cad82006-09-26 13:21:02 +0000268 status = pj_sock_bind_in(sock[1], bound_addr.sin_addr.s_addr,
Benny Prijonode479562007-03-15 10:23:55 +0000269 (pj_uint16_t)(next_rtp_port+1));
Benny Prijonoeebe9af2006-06-13 22:57:13 +0000270 if (status != PJ_SUCCESS) {
271 pj_sock_close(sock[0]);
272 sock[0] = PJ_INVALID_SOCKET;
273
274 pj_sock_close(sock[1]);
275 sock[1] = PJ_INVALID_SOCKET;
276 continue;
277 }
278
279 /*
280 * If we're configured to use STUN, then find out the mapped address,
281 * and make sure that the mapped RTCP port is adjacent with the RTP.
282 */
Benny Prijonoc97608e2007-03-23 16:34:20 +0000283 if (pjsua_var.stun_srv.addr.sa_family != 0) {
284 char ip_addr[32];
285 pj_str_t stun_srv;
286
287 pj_ansi_strcpy(ip_addr,
288 pj_inet_ntoa(pjsua_var.stun_srv.ipv4.sin_addr));
289 stun_srv = pj_str(ip_addr);
290
Benny Prijono14c2b862007-02-21 00:40:05 +0000291 status=pjstun_get_mapped_addr(&pjsua_var.cp.factory, 2, sock,
Benny Prijonoaf09dc32007-04-22 12:48:30 +0000292 &stun_srv, pj_ntohs(pjsua_var.stun_srv.ipv4.sin_port),
293 &stun_srv, pj_ntohs(pjsua_var.stun_srv.ipv4.sin_port),
Benny Prijonoeebe9af2006-06-13 22:57:13 +0000294 mapped_addr);
295 if (status != PJ_SUCCESS) {
296 pjsua_perror(THIS_FILE, "STUN resolve error", status);
297 goto on_error;
298 }
299
300 if (pj_ntohs(mapped_addr[1].sin_port) ==
301 pj_ntohs(mapped_addr[0].sin_port)+1)
302 {
303 /* Success! */
304 break;
305 }
306
307 pj_sock_close(sock[0]);
308 sock[0] = PJ_INVALID_SOCKET;
309
310 pj_sock_close(sock[1]);
311 sock[1] = PJ_INVALID_SOCKET;
312
Benny Prijono0a5cad82006-09-26 13:21:02 +0000313 } else if (cfg->public_addr.slen) {
314
315 status = pj_sockaddr_in_init(&mapped_addr[0], &cfg->public_addr,
Benny Prijonode479562007-03-15 10:23:55 +0000316 (pj_uint16_t)next_rtp_port);
Benny Prijono0a5cad82006-09-26 13:21:02 +0000317 if (status != PJ_SUCCESS)
318 goto on_error;
319
320 status = pj_sockaddr_in_init(&mapped_addr[1], &cfg->public_addr,
Benny Prijonode479562007-03-15 10:23:55 +0000321 (pj_uint16_t)(next_rtp_port+1));
Benny Prijono0a5cad82006-09-26 13:21:02 +0000322 if (status != PJ_SUCCESS)
323 goto on_error;
324
325 break;
326
Benny Prijonoeebe9af2006-06-13 22:57:13 +0000327 } else {
Benny Prijono594e4c52006-09-14 18:51:01 +0000328 pj_in_addr addr;
Benny Prijonoeebe9af2006-06-13 22:57:13 +0000329
330 /* Get local IP address. */
Benny Prijono594e4c52006-09-14 18:51:01 +0000331 status = pj_gethostip(&addr);
332 if (status != PJ_SUCCESS)
Benny Prijonoeebe9af2006-06-13 22:57:13 +0000333 goto on_error;
Benny Prijonoeebe9af2006-06-13 22:57:13 +0000334
335 for (i=0; i<2; ++i)
Benny Prijono594e4c52006-09-14 18:51:01 +0000336 mapped_addr[i].sin_addr = addr;
Benny Prijonoeebe9af2006-06-13 22:57:13 +0000337
Benny Prijonode479562007-03-15 10:23:55 +0000338 mapped_addr[0].sin_port=pj_htons((pj_uint16_t)next_rtp_port);
339 mapped_addr[1].sin_port=pj_htons((pj_uint16_t)(next_rtp_port+1));
Benny Prijonoeebe9af2006-06-13 22:57:13 +0000340 break;
341 }
342 }
343
344 if (sock[0] == PJ_INVALID_SOCKET) {
345 PJ_LOG(1,(THIS_FILE,
346 "Unable to find appropriate RTP/RTCP ports combination"));
347 goto on_error;
348 }
349
350
351 skinfo->rtp_sock = sock[0];
352 pj_memcpy(&skinfo->rtp_addr_name,
353 &mapped_addr[0], sizeof(pj_sockaddr_in));
354
355 skinfo->rtcp_sock = sock[1];
356 pj_memcpy(&skinfo->rtcp_addr_name,
357 &mapped_addr[1], sizeof(pj_sockaddr_in));
358
359 PJ_LOG(4,(THIS_FILE, "RTP socket reachable at %s:%d",
360 pj_inet_ntoa(skinfo->rtp_addr_name.sin_addr),
361 pj_ntohs(skinfo->rtp_addr_name.sin_port)));
362 PJ_LOG(4,(THIS_FILE, "RTCP socket reachable at %s:%d",
363 pj_inet_ntoa(skinfo->rtcp_addr_name.sin_addr),
364 pj_ntohs(skinfo->rtcp_addr_name.sin_port)));
365
Benny Prijonode479562007-03-15 10:23:55 +0000366 next_rtp_port += 2;
Benny Prijonoeebe9af2006-06-13 22:57:13 +0000367 return PJ_SUCCESS;
368
369on_error:
370 for (i=0; i<2; ++i) {
371 if (sock[i] != PJ_INVALID_SOCKET)
372 pj_sock_close(sock[i]);
373 }
374 return status;
375}
376
377
378/*
379 * Start pjsua media subsystem.
380 */
381pj_status_t pjsua_media_subsys_start(void)
382{
383 pj_status_t status;
384
385 /* Create media for calls, if none is specified */
386 if (pjsua_var.calls[0].med_tp == NULL) {
387 pjsua_transport_config transport_cfg;
388
389 /* Create default transport config */
390 pjsua_transport_config_default(&transport_cfg);
391 transport_cfg.port = DEFAULT_RTP_PORT;
392
393 status = pjsua_media_transports_create(&transport_cfg);
394 if (status != PJ_SUCCESS)
395 return status;
396 }
397
398 /* Create sound port if none is created yet */
Benny Prijonoe909eac2006-07-27 22:04:56 +0000399 if (pjsua_var.snd_port==NULL && pjsua_var.null_snd==NULL &&
400 !pjsua_var.no_snd)
401 {
Benny Prijonoeebe9af2006-06-13 22:57:13 +0000402 status = pjsua_set_snd_dev(pjsua_var.cap_dev, pjsua_var.play_dev);
403 if (status != PJ_SUCCESS) {
Benny Prijonod639efa2007-04-05 22:45:06 +0000404 pjsua_perror(THIS_FILE, "Error opening sound device", status);
405 return status;
Benny Prijonoeebe9af2006-06-13 22:57:13 +0000406 }
407 }
408
409 return PJ_SUCCESS;
410}
411
412
413/*
414 * Destroy pjsua media subsystem.
415 */
416pj_status_t pjsua_media_subsys_destroy(void)
417{
418 unsigned i;
419
420 close_snd_dev();
421
422 if (pjsua_var.mconf) {
423 pjmedia_conf_destroy(pjsua_var.mconf);
424 pjsua_var.mconf = NULL;
425 }
426
427 if (pjsua_var.null_port) {
428 pjmedia_port_destroy(pjsua_var.null_port);
429 pjsua_var.null_port = NULL;
430 }
431
432 /* Destroy file players */
433 for (i=0; i<PJ_ARRAY_SIZE(pjsua_var.player); ++i) {
434 if (pjsua_var.player[i].port) {
435 pjmedia_port_destroy(pjsua_var.player[i].port);
436 pjsua_var.player[i].port = NULL;
437 }
438 }
439
440 /* Destroy file recorders */
441 for (i=0; i<PJ_ARRAY_SIZE(pjsua_var.recorder); ++i) {
442 if (pjsua_var.recorder[i].port) {
443 pjmedia_port_destroy(pjsua_var.recorder[i].port);
444 pjsua_var.recorder[i].port = NULL;
445 }
446 }
447
448 /* Close media transports */
Benny Prijono0875ae82006-12-26 00:11:48 +0000449 for (i=0; i<pjsua_var.ua_cfg.max_calls; ++i) {
Benny Prijonoeebe9af2006-06-13 22:57:13 +0000450 if (pjsua_var.calls[i].med_tp) {
451 (*pjsua_var.calls[i].med_tp->op->destroy)(pjsua_var.calls[i].med_tp);
452 pjsua_var.calls[i].med_tp = NULL;
453 }
454 }
455
456 /* Destroy media endpoint. */
457 if (pjsua_var.med_endpt) {
458
459 /* Shutdown all codecs: */
460# if PJMEDIA_HAS_SPEEX_CODEC
461 pjmedia_codec_speex_deinit();
462# endif /* PJMEDIA_HAS_SPEEX_CODEC */
463
464# if PJMEDIA_HAS_GSM_CODEC
465 pjmedia_codec_gsm_deinit();
466# endif /* PJMEDIA_HAS_GSM_CODEC */
467
468# if PJMEDIA_HAS_G711_CODEC
469 pjmedia_codec_g711_deinit();
470# endif /* PJMEDIA_HAS_G711_CODEC */
471
472# if PJMEDIA_HAS_L16_CODEC
473 pjmedia_codec_l16_deinit();
474# endif /* PJMEDIA_HAS_L16_CODEC */
475
476
477 pjmedia_endpt_destroy(pjsua_var.med_endpt);
478 pjsua_var.med_endpt = NULL;
Benny Prijonoeebe9af2006-06-13 22:57:13 +0000479
Benny Prijonoad2e0ca2007-04-29 12:31:51 +0000480 /* Deinitialize sound subsystem */
481 pjmedia_snd_deinit();
482 }
Benny Prijonoaf1bb1e2006-11-21 12:39:31 +0000483
Benny Prijonode479562007-03-15 10:23:55 +0000484 /* Reset RTP port */
485 next_rtp_port = 0;
486
Benny Prijonoeebe9af2006-06-13 22:57:13 +0000487 return PJ_SUCCESS;
488}
489
490
Benny Prijonoc97608e2007-03-23 16:34:20 +0000491/* Create normal UDP media transports */
492static pj_status_t create_udp_media_transports(pjsua_transport_config *cfg)
Benny Prijonoeebe9af2006-06-13 22:57:13 +0000493{
Benny Prijonoeebe9af2006-06-13 22:57:13 +0000494 unsigned i;
Benny Prijono617c5bc2007-04-02 19:51:21 +0000495 pjmedia_sock_info skinfo;
Benny Prijonoeebe9af2006-06-13 22:57:13 +0000496 pj_status_t status;
497
Benny Prijonoeebe9af2006-06-13 22:57:13 +0000498 /* Create each media transport */
499 for (i=0; i<pjsua_var.ua_cfg.max_calls; ++i) {
500
Benny Prijono617c5bc2007-04-02 19:51:21 +0000501 status = create_rtp_rtcp_sock(cfg, &skinfo);
Benny Prijonoeebe9af2006-06-13 22:57:13 +0000502 if (status != PJ_SUCCESS) {
503 pjsua_perror(THIS_FILE, "Unable to create RTP/RTCP socket",
504 status);
505 goto on_error;
506 }
507 status = pjmedia_transport_udp_attach(pjsua_var.med_endpt, NULL,
Benny Prijono617c5bc2007-04-02 19:51:21 +0000508 &skinfo, 0,
Benny Prijonoeebe9af2006-06-13 22:57:13 +0000509 &pjsua_var.calls[i].med_tp);
510 if (status != PJ_SUCCESS) {
511 pjsua_perror(THIS_FILE, "Unable to create media transport",
512 status);
513 goto on_error;
514 }
Benny Prijono00cae612006-07-31 15:19:36 +0000515
516 pjmedia_transport_udp_simulate_lost(pjsua_var.calls[i].med_tp,
517 PJMEDIA_DIR_ENCODING,
518 pjsua_var.media_cfg.tx_drop_pct);
519
520 pjmedia_transport_udp_simulate_lost(pjsua_var.calls[i].med_tp,
521 PJMEDIA_DIR_DECODING,
522 pjsua_var.media_cfg.rx_drop_pct);
523
Benny Prijonoeebe9af2006-06-13 22:57:13 +0000524 }
525
Benny Prijonoc97608e2007-03-23 16:34:20 +0000526 return PJ_SUCCESS;
527
528on_error:
529 for (i=0; i<pjsua_var.ua_cfg.max_calls; ++i) {
530 if (pjsua_var.calls[i].med_tp != NULL) {
531 pjmedia_transport_close(pjsua_var.calls[i].med_tp);
532 pjsua_var.calls[i].med_tp = NULL;
533 }
534 }
535
536 return status;
537}
538
539
Benny Prijono096c56c2007-09-15 08:30:16 +0000540/* This callback is called when ICE negotiation completes */
541static void on_ice_complete(pjmedia_transport *tp, pj_status_t result)
542{
543 unsigned id, c;
544 pj_bool_t found = PJ_FALSE;
545
546 /* We're only interested with failure case */
547 if (result == PJ_SUCCESS)
548 return;
549
550 /* Find call which has this media transport */
551
552 PJSUA_LOCK();
553
554 for (id=0, c=0; id<PJSUA_MAX_CALLS && c<pjsua_var.call_cnt; ++id) {
555 pjsua_call *call = &pjsua_var.calls[id];
556 if (call->inv) {
557 ++c;
558
559 if (call->med_tp == tp) {
560 call->media_st = PJSUA_CALL_MEDIA_ERROR;
561 call->media_dir = PJMEDIA_DIR_NONE;
562 found = PJ_TRUE;
563 break;
564 }
565 }
566 }
567
568 PJSUA_UNLOCK();
569
570 if (found && pjsua_var.ua_cfg.cb.on_call_media_state) {
571 pjsua_var.ua_cfg.cb.on_call_media_state(id);
572 }
573}
574
575
Benny Prijonoc97608e2007-03-23 16:34:20 +0000576/* Create ICE media transports (when ice is enabled) */
577static pj_status_t create_ice_media_transports(pjsua_transport_config *cfg)
578{
579 unsigned i;
Benny Prijonob681a2f2007-03-25 18:44:51 +0000580 pj_sockaddr_in addr;
Benny Prijonoc97608e2007-03-23 16:34:20 +0000581 pj_status_t status;
582
Benny Prijonoda9785b2007-04-02 20:43:06 +0000583 /* Make sure STUN server resolution has completed */
584 status = pjsua_resolve_stun_server(PJ_TRUE);
585 if (status != PJ_SUCCESS) {
586 pjsua_perror(THIS_FILE, "Error resolving STUN server", status);
587 return status;
588 }
589
Benny Prijonob681a2f2007-03-25 18:44:51 +0000590 pj_sockaddr_in_init(&addr, 0, (pj_uint16_t)cfg->port);
591
Benny Prijonoc97608e2007-03-23 16:34:20 +0000592 /* Create each media transport */
593 for (i=0; i<pjsua_var.ua_cfg.max_calls; ++i) {
Benny Prijonoa6bd7582007-03-28 15:49:48 +0000594 pj_ice_strans_comp comp;
Benny Prijono096c56c2007-09-15 08:30:16 +0000595 pjmedia_ice_cb ice_cb;
Benny Prijonob681a2f2007-03-25 18:44:51 +0000596 int next_port;
Benny Prijonodc1fe222007-06-26 10:13:13 +0000597#if PJMEDIA_ADVERTISE_RTCP
598 enum { COMP_CNT=2 };
599#else
600 enum { COMP_CNT=1 };
601#endif
Benny Prijonoc97608e2007-03-23 16:34:20 +0000602
Benny Prijono096c56c2007-09-15 08:30:16 +0000603 pj_bzero(&ice_cb, sizeof(pjmedia_ice_cb));
604 ice_cb.on_ice_complete = &on_ice_complete;
605
Benny Prijonodc1fe222007-06-26 10:13:13 +0000606 status = pjmedia_ice_create(pjsua_var.med_endpt, NULL, COMP_CNT,
Benny Prijono096c56c2007-09-15 08:30:16 +0000607 &pjsua_var.stun_cfg, &ice_cb,
Benny Prijonoc97608e2007-03-23 16:34:20 +0000608 &pjsua_var.calls[i].med_tp);
609 if (status != PJ_SUCCESS) {
610 pjsua_perror(THIS_FILE, "Unable to create ICE media transport",
611 status);
612 goto on_error;
613 }
614
Benny Prijonob681a2f2007-03-25 18:44:51 +0000615 status = pjmedia_ice_start_init(pjsua_var.calls[i].med_tp, 0, &addr,
616 &pjsua_var.stun_srv.ipv4, NULL);
Benny Prijonoc97608e2007-03-23 16:34:20 +0000617 if (status != PJ_SUCCESS) {
Benny Prijonob681a2f2007-03-25 18:44:51 +0000618 pjsua_perror(THIS_FILE, "Error starting ICE transport",
Benny Prijonoc97608e2007-03-23 16:34:20 +0000619 status);
620 goto on_error;
621 }
622
Benny Prijonob681a2f2007-03-25 18:44:51 +0000623 pjmedia_ice_get_comp(pjsua_var.calls[i].med_tp, 1, &comp);
624 next_port = pj_ntohs(comp.local_addr.ipv4.sin_port);
625 next_port += 2;
626 addr.sin_port = pj_htons((pj_uint16_t)next_port);
Benny Prijonoc97608e2007-03-23 16:34:20 +0000627 }
628
629 /* Wait until all ICE transports are ready */
630 for (i=0; i<pjsua_var.ua_cfg.max_calls; ++i) {
Benny Prijonoc97608e2007-03-23 16:34:20 +0000631
632 /* Wait until interface status is PJ_SUCCESS */
633 for (;;) {
Benny Prijonob681a2f2007-03-25 18:44:51 +0000634 status = pjmedia_ice_get_init_status(pjsua_var.calls[i].med_tp);
Benny Prijonoc97608e2007-03-23 16:34:20 +0000635 if (status == PJ_EPENDING)
636 pjsua_handle_events(100);
637 else
638 break;
639 }
640
641 if (status != PJ_SUCCESS) {
642 pjsua_perror(THIS_FILE,
643 "Error adding STUN address to ICE media transport",
644 status);
645 goto on_error;
646 }
647
Benny Prijonoc97608e2007-03-23 16:34:20 +0000648 }
Benny Prijonoeebe9af2006-06-13 22:57:13 +0000649
650 return PJ_SUCCESS;
651
652on_error:
653 for (i=0; i<pjsua_var.ua_cfg.max_calls; ++i) {
654 if (pjsua_var.calls[i].med_tp != NULL) {
Benny Prijonoc97608e2007-03-23 16:34:20 +0000655 pjmedia_transport_close(pjsua_var.calls[i].med_tp);
Benny Prijonoeebe9af2006-06-13 22:57:13 +0000656 pjsua_var.calls[i].med_tp = NULL;
657 }
658 }
659
Benny Prijonoc97608e2007-03-23 16:34:20 +0000660 return status;
661}
662
663
664/*
665 * Create UDP media transports for all the calls. This function creates
666 * one UDP media transport for each call.
667 */
Benny Prijono1f61a8f2007-08-16 10:11:44 +0000668PJ_DEF(pj_status_t) pjsua_media_transports_create(
669 const pjsua_transport_config *app_cfg)
Benny Prijonoc97608e2007-03-23 16:34:20 +0000670{
671 pjsua_transport_config cfg;
672 unsigned i;
673 pj_status_t status;
674
675
676 /* Make sure pjsua_init() has been called */
677 PJ_ASSERT_RETURN(pjsua_var.ua_cfg.max_calls>0, PJ_EINVALIDOP);
678
679 PJSUA_LOCK();
680
681 /* Delete existing media transports */
682 for (i=0; i<pjsua_var.ua_cfg.max_calls; ++i) {
683 if (pjsua_var.calls[i].med_tp != NULL) {
684 pjmedia_transport_close(pjsua_var.calls[i].med_tp);
685 pjsua_var.calls[i].med_tp = NULL;
686 }
687 }
688
689 /* Copy config */
690 pjsua_transport_config_dup(pjsua_var.pool, &cfg, app_cfg);
691
692 if (pjsua_var.media_cfg.enable_ice) {
693 status = create_ice_media_transports(&cfg);
694 } else {
695 status = create_udp_media_transports(&cfg);
696 }
697
698
Benny Prijonoeebe9af2006-06-13 22:57:13 +0000699 PJSUA_UNLOCK();
700
701 return status;
702}
703
704
Benny Prijonoc97608e2007-03-23 16:34:20 +0000705pj_status_t pjsua_media_channel_init(pjsua_call_id call_id,
706 pjsip_role_e role)
707{
708 pjsua_call *call = &pjsua_var.calls[call_id];
709
710 if (pjsua_var.media_cfg.enable_ice) {
Benny Prijonoa6bd7582007-03-28 15:49:48 +0000711 pj_ice_sess_role ice_role;
Benny Prijonoc97608e2007-03-23 16:34:20 +0000712 pj_status_t status;
713
Benny Prijonoa6bd7582007-03-28 15:49:48 +0000714 ice_role = (role==PJSIP_ROLE_UAC ? PJ_ICE_SESS_ROLE_CONTROLLING :
715 PJ_ICE_SESS_ROLE_CONTROLLED);
Benny Prijonoc97608e2007-03-23 16:34:20 +0000716
717 /* Restart ICE */
718 pjmedia_ice_stop_ice(call->med_tp);
719
720 status = pjmedia_ice_init_ice(call->med_tp, ice_role, NULL, NULL);
721 if (status != PJ_SUCCESS)
722 return status;
723 }
724
725 return PJ_SUCCESS;
726}
727
728pj_status_t pjsua_media_channel_create_sdp(pjsua_call_id call_id,
729 pj_pool_t *pool,
730 pjmedia_sdp_session **p_sdp)
731{
732 pjmedia_sdp_session *sdp;
Benny Prijono617c5bc2007-04-02 19:51:21 +0000733 pjmedia_sock_info skinfo;
Benny Prijonoc97608e2007-03-23 16:34:20 +0000734 pjsua_call *call = &pjsua_var.calls[call_id];
735 pj_status_t status;
736
Benny Prijono55e82352007-05-10 20:49:08 +0000737 /* Return error if media transport has not been created yet
738 * (e.g. application is starting)
739 */
740 if (call->med_tp == NULL) {
741 return PJ_EBUSY;
742 }
743
Benny Prijono617c5bc2007-04-02 19:51:21 +0000744 /* Get media socket info */
745 pjmedia_transport_get_info(call->med_tp, &skinfo);
746
747 /* Create SDP */
Benny Prijonoc97608e2007-03-23 16:34:20 +0000748 status = pjmedia_endpt_create_sdp(pjsua_var.med_endpt, pool, 1,
Benny Prijono617c5bc2007-04-02 19:51:21 +0000749 &skinfo, &sdp);
Benny Prijonoc97608e2007-03-23 16:34:20 +0000750 if (status != PJ_SUCCESS)
751 goto on_error;
752
753 if (pjsua_var.media_cfg.enable_ice) {
754 status = pjmedia_ice_modify_sdp(call->med_tp, pool, sdp);
755 if (status != PJ_SUCCESS)
756 goto on_error;
757 }
758
759 *p_sdp = sdp;
760 return PJ_SUCCESS;
761
762on_error:
763 pjsua_media_channel_deinit(call_id);
764 return status;
765
766}
767
768
769static void stop_media_session(pjsua_call_id call_id)
770{
771 pjsua_call *call = &pjsua_var.calls[call_id];
772
773 if (call->conf_slot != PJSUA_INVALID_ID) {
774 pjmedia_conf_remove_port(pjsua_var.mconf, call->conf_slot);
775 call->conf_slot = PJSUA_INVALID_ID;
776 }
777
778 if (call->session) {
779 pjmedia_session_destroy(call->session);
780 call->session = NULL;
781
782 PJ_LOG(4,(THIS_FILE, "Media session for call %d is destroyed",
783 call_id));
784
785 }
786
787 call->media_st = PJSUA_CALL_MEDIA_NONE;
788}
789
790pj_status_t pjsua_media_channel_deinit(pjsua_call_id call_id)
791{
792 pjsua_call *call = &pjsua_var.calls[call_id];
793
794 stop_media_session(call_id);
795
796 if (pjsua_var.media_cfg.enable_ice) {
797 pjmedia_ice_stop_ice(call->med_tp);
798 }
799
800 return PJ_SUCCESS;
801}
802
803
804/*
805 * DTMF callback from the stream.
806 */
807static void dtmf_callback(pjmedia_stream *strm, void *user_data,
808 int digit)
809{
810 PJ_UNUSED_ARG(strm);
811
812 if (pjsua_var.ua_cfg.cb.on_dtmf_digit) {
813 pjsua_call_id call_id;
814
815 call_id = (pjsua_call_id)user_data;
816 pjsua_var.ua_cfg.cb.on_dtmf_digit(call_id, digit);
817 }
818}
819
820
821pj_status_t pjsua_media_channel_update(pjsua_call_id call_id,
Benny Prijonodbce2cf2007-03-28 16:24:00 +0000822 const pjmedia_sdp_session *local_sdp,
823 const pjmedia_sdp_session *remote_sdp)
Benny Prijonoc97608e2007-03-23 16:34:20 +0000824{
825 int prev_media_st = 0;
826 pjsua_call *call = &pjsua_var.calls[call_id];
827 pjmedia_session_info sess_info;
828 pjmedia_port *media_port;
829 pj_str_t port_name;
830 char tmp[PJSIP_MAX_URL_SIZE];
831 pj_status_t status;
832
833 /* Destroy existing media session, if any. */
834 prev_media_st = call->media_st;
835 stop_media_session(call->index);
836
837 /* Create media session info based on SDP parameters.
838 * We only support one stream per session at the moment
839 */
840 status = pjmedia_session_info_from_sdp( call->inv->dlg->pool,
841 pjsua_var.med_endpt,
842 1,&sess_info,
843 local_sdp, remote_sdp);
844 if (status != PJ_SUCCESS)
845 return status;
846
847
848 /* Check if media is put on-hold */
849 if (sess_info.stream_cnt == 0 ||
850 sess_info.stream_info[0].dir == PJMEDIA_DIR_NONE)
851 {
852
853 /* Determine who puts the call on-hold */
854 if (prev_media_st == PJSUA_CALL_MEDIA_ACTIVE) {
855 if (pjmedia_sdp_neg_was_answer_remote(call->inv->neg)) {
856 /* It was local who offer hold */
857 call->media_st = PJSUA_CALL_MEDIA_LOCAL_HOLD;
858 } else {
859 call->media_st = PJSUA_CALL_MEDIA_REMOTE_HOLD;
860 }
861 }
862
863 call->media_dir = PJMEDIA_DIR_NONE;
864
Benny Prijono667952e2007-04-02 19:27:54 +0000865 /* Shutdown ICE session */
866 if (pjsua_var.media_cfg.enable_ice) {
867 pjmedia_ice_stop_ice(call->med_tp);
868 }
869
Benny Prijonoc97608e2007-03-23 16:34:20 +0000870 /* No need because we need keepalive? */
871
872 } else {
873
874 /* Start ICE */
875 if (pjsua_var.media_cfg.enable_ice) {
876 status = pjmedia_ice_start_ice(call->med_tp, call->inv->pool,
Benny Prijonofd70c9b2007-03-27 11:00:38 +0000877 remote_sdp, 0);
Benny Prijonoc97608e2007-03-23 16:34:20 +0000878 if (status != PJ_SUCCESS)
879 return status;
880 }
881
882 /* Override ptime, if this option is specified. */
883 if (pjsua_var.media_cfg.ptime != 0) {
884 sess_info.stream_info[0].param->setting.frm_per_pkt = (pj_uint8_t)
885 (pjsua_var.media_cfg.ptime / sess_info.stream_info[0].param->info.frm_ptime);
886 if (sess_info.stream_info[0].param->setting.frm_per_pkt == 0)
887 sess_info.stream_info[0].param->setting.frm_per_pkt = 1;
888 }
889
890 /* Disable VAD, if this option is specified. */
891 if (pjsua_var.media_cfg.no_vad) {
892 sess_info.stream_info[0].param->setting.vad = 0;
893 }
894
895
896 /* Optionally, application may modify other stream settings here
897 * (such as jitter buffer parameters, codec ptime, etc.)
898 */
899 sess_info.stream_info[0].jb_init = pjsua_var.media_cfg.jb_init;
900 sess_info.stream_info[0].jb_min_pre = pjsua_var.media_cfg.jb_min_pre;
901 sess_info.stream_info[0].jb_max_pre = pjsua_var.media_cfg.jb_max_pre;
902 sess_info.stream_info[0].jb_max = pjsua_var.media_cfg.jb_max;
903
904 /* Create session based on session info. */
905 status = pjmedia_session_create( pjsua_var.med_endpt, &sess_info,
906 &call->med_tp,
907 call, &call->session );
908 if (status != PJ_SUCCESS) {
909 return status;
910 }
911
912 /* If DTMF callback is installed by application, install our
913 * callback to the session.
914 */
915 if (pjsua_var.ua_cfg.cb.on_dtmf_digit) {
916 pjmedia_session_set_dtmf_callback(call->session, 0,
917 &dtmf_callback,
918 (void*)(call->index));
919 }
920
921 /* Get the port interface of the first stream in the session.
922 * We need the port interface to add to the conference bridge.
923 */
924 pjmedia_session_get_port(call->session, 0, &media_port);
925
926
927 /*
928 * Add the call to conference bridge.
929 */
930 port_name.ptr = tmp;
931 port_name.slen = pjsip_uri_print(PJSIP_URI_IN_REQ_URI,
932 call->inv->dlg->remote.info->uri,
933 tmp, sizeof(tmp));
934 if (port_name.slen < 1) {
935 port_name = pj_str("call");
936 }
937 status = pjmedia_conf_add_port( pjsua_var.mconf, call->inv->pool,
938 media_port,
939 &port_name,
940 (unsigned*)&call->conf_slot);
941 if (status != PJ_SUCCESS) {
942 return status;
943 }
944
945 /* Call's media state is active */
946 call->media_st = PJSUA_CALL_MEDIA_ACTIVE;
947 call->media_dir = sess_info.stream_info[0].dir;
948 }
949
950 /* Print info. */
951 {
952 char info[80];
953 int info_len = 0;
954 unsigned i;
955
956 for (i=0; i<sess_info.stream_cnt; ++i) {
957 int len;
958 const char *dir;
959 pjmedia_stream_info *strm_info = &sess_info.stream_info[i];
960
961 switch (strm_info->dir) {
962 case PJMEDIA_DIR_NONE:
963 dir = "inactive";
964 break;
965 case PJMEDIA_DIR_ENCODING:
966 dir = "sendonly";
967 break;
968 case PJMEDIA_DIR_DECODING:
969 dir = "recvonly";
970 break;
971 case PJMEDIA_DIR_ENCODING_DECODING:
972 dir = "sendrecv";
973 break;
974 default:
975 dir = "unknown";
976 break;
977 }
978 len = pj_ansi_sprintf( info+info_len,
979 ", stream #%d: %.*s (%s)", i,
980 (int)strm_info->fmt.encoding_name.slen,
981 strm_info->fmt.encoding_name.ptr,
982 dir);
983 if (len > 0)
984 info_len += len;
985 }
986 PJ_LOG(4,(THIS_FILE,"Media updates%s", info));
987 }
988
989 return PJ_SUCCESS;
990}
991
992
Benny Prijonoeebe9af2006-06-13 22:57:13 +0000993/*
994 * Get maxinum number of conference ports.
995 */
996PJ_DEF(unsigned) pjsua_conf_get_max_ports(void)
997{
998 return pjsua_var.media_cfg.max_media_ports;
999}
1000
1001
1002/*
1003 * Get current number of active ports in the bridge.
1004 */
1005PJ_DEF(unsigned) pjsua_conf_get_active_ports(void)
1006{
1007 unsigned ports[256];
1008 unsigned count = PJ_ARRAY_SIZE(ports);
1009 pj_status_t status;
1010
1011 status = pjmedia_conf_enum_ports(pjsua_var.mconf, ports, &count);
1012 if (status != PJ_SUCCESS)
1013 count = 0;
1014
1015 return count;
1016}
1017
1018
1019/*
1020 * Enumerate all conference ports.
1021 */
1022PJ_DEF(pj_status_t) pjsua_enum_conf_ports(pjsua_conf_port_id id[],
1023 unsigned *count)
1024{
1025 return pjmedia_conf_enum_ports(pjsua_var.mconf, (unsigned*)id, count);
1026}
1027
1028
1029/*
1030 * Get information about the specified conference port
1031 */
1032PJ_DEF(pj_status_t) pjsua_conf_get_port_info( pjsua_conf_port_id id,
1033 pjsua_conf_port_info *info)
1034{
1035 pjmedia_conf_port_info cinfo;
Benny Prijono0498d902006-06-19 14:49:14 +00001036 unsigned i;
Benny Prijonoeebe9af2006-06-13 22:57:13 +00001037 pj_status_t status;
1038
1039 status = pjmedia_conf_get_port_info( pjsua_var.mconf, id, &cinfo);
1040 if (status != PJ_SUCCESS)
1041 return status;
1042
Benny Prijonoac623b32006-07-03 15:19:31 +00001043 pj_bzero(info, sizeof(*info));
Benny Prijonoeebe9af2006-06-13 22:57:13 +00001044 info->slot_id = id;
1045 info->name = cinfo.name;
1046 info->clock_rate = cinfo.clock_rate;
1047 info->channel_count = cinfo.channel_count;
1048 info->samples_per_frame = cinfo.samples_per_frame;
1049 info->bits_per_sample = cinfo.bits_per_sample;
1050
1051 /* Build array of listeners */
Benny Prijonoc78c3a32006-06-16 15:54:43 +00001052 info->listener_cnt = cinfo.listener_cnt;
1053 for (i=0; i<cinfo.listener_cnt; ++i) {
1054 info->listeners[i] = cinfo.listener_slots[i];
Benny Prijonoeebe9af2006-06-13 22:57:13 +00001055 }
1056
1057 return PJ_SUCCESS;
1058}
1059
1060
1061/*
Benny Prijonoe909eac2006-07-27 22:04:56 +00001062 * Add arbitrary media port to PJSUA's conference bridge.
1063 */
1064PJ_DEF(pj_status_t) pjsua_conf_add_port( pj_pool_t *pool,
1065 pjmedia_port *port,
1066 pjsua_conf_port_id *p_id)
1067{
1068 pj_status_t status;
1069
1070 status = pjmedia_conf_add_port(pjsua_var.mconf, pool,
1071 port, NULL, (unsigned*)p_id);
1072 if (status != PJ_SUCCESS) {
1073 if (p_id)
1074 *p_id = PJSUA_INVALID_ID;
1075 }
1076
1077 return status;
1078}
1079
1080
1081/*
1082 * Remove arbitrary slot from the conference bridge.
1083 */
1084PJ_DEF(pj_status_t) pjsua_conf_remove_port(pjsua_conf_port_id id)
1085{
1086 return pjmedia_conf_remove_port(pjsua_var.mconf, (unsigned)id);
1087}
1088
1089
1090/*
Benny Prijonoeebe9af2006-06-13 22:57:13 +00001091 * Establish unidirectional media flow from souce to sink.
1092 */
1093PJ_DEF(pj_status_t) pjsua_conf_connect( pjsua_conf_port_id source,
1094 pjsua_conf_port_id sink)
1095{
1096 return pjmedia_conf_connect_port(pjsua_var.mconf, source, sink, 0);
1097}
1098
1099
1100/*
1101 * Disconnect media flow from the source to destination port.
1102 */
1103PJ_DEF(pj_status_t) pjsua_conf_disconnect( pjsua_conf_port_id source,
1104 pjsua_conf_port_id sink)
1105{
1106 return pjmedia_conf_disconnect_port(pjsua_var.mconf, source, sink);
1107}
1108
1109
Benny Prijono6dd967c2006-12-26 02:27:14 +00001110/*
1111 * Adjust the signal level to be transmitted from the bridge to the
1112 * specified port by making it louder or quieter.
1113 */
1114PJ_DEF(pj_status_t) pjsua_conf_adjust_tx_level(pjsua_conf_port_id slot,
1115 float level)
1116{
1117 return pjmedia_conf_adjust_tx_level(pjsua_var.mconf, slot,
1118 (int)((level-1) * 128));
1119}
1120
1121/*
1122 * Adjust the signal level to be received from the specified port (to
1123 * the bridge) by making it louder or quieter.
1124 */
1125PJ_DEF(pj_status_t) pjsua_conf_adjust_rx_level(pjsua_conf_port_id slot,
1126 float level)
1127{
1128 return pjmedia_conf_adjust_rx_level(pjsua_var.mconf, slot,
1129 (int)((level-1) * 128));
1130}
1131
1132
1133/*
1134 * Get last signal level transmitted to or received from the specified port.
1135 */
1136PJ_DEF(pj_status_t) pjsua_conf_get_signal_level(pjsua_conf_port_id slot,
1137 unsigned *tx_level,
1138 unsigned *rx_level)
1139{
1140 return pjmedia_conf_get_signal_level(pjsua_var.mconf, slot,
1141 tx_level, rx_level);
1142}
1143
Benny Prijonoeebe9af2006-06-13 22:57:13 +00001144/*****************************************************************************
1145 * File player.
1146 */
1147
Benny Prijonod5696da2007-07-17 16:25:45 +00001148static char* get_basename(const char *path, unsigned len)
1149{
1150 char *p = ((char*)path) + len;
1151
1152 if (len==0)
1153 return p;
1154
Benny Prijono1f61a8f2007-08-16 10:11:44 +00001155 for (--p; p!=path && *p!='/' && *p!='\\'; ) --p;
Benny Prijonod5696da2007-07-17 16:25:45 +00001156
1157 return (p==path) ? p : p+1;
1158}
1159
1160
Benny Prijonoeebe9af2006-06-13 22:57:13 +00001161/*
1162 * Create a file player, and automatically connect this player to
1163 * the conference bridge.
1164 */
1165PJ_DEF(pj_status_t) pjsua_player_create( const pj_str_t *filename,
1166 unsigned options,
Benny Prijonoeebe9af2006-06-13 22:57:13 +00001167 pjsua_player_id *p_id)
1168{
1169 unsigned slot, file_id;
Benny Prijonoa66c3312007-01-21 23:12:40 +00001170 char path[PJ_MAXPATH];
Benny Prijonod5696da2007-07-17 16:25:45 +00001171 pj_pool_t *pool;
Benny Prijonoeebe9af2006-06-13 22:57:13 +00001172 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 pj_memcpy(path, filename->ptr, filename->slen);
1193 path[filename->slen] = '\0';
Benny Prijonod5696da2007-07-17 16:25:45 +00001194
1195 pool = pjsua_pool_create(get_basename(path, filename->slen), 1000, 1000);
1196 if (!pool) {
1197 PJSUA_UNLOCK();
1198 return PJ_ENOMEM;
1199 }
1200
1201 status = pjmedia_wav_player_port_create(pool, path,
Benny Prijonoeebe9af2006-06-13 22:57:13 +00001202 pjsua_var.mconf_cfg.samples_per_frame *
1203 1000 / pjsua_var.media_cfg.clock_rate,
Benny Prijono00cae612006-07-31 15:19:36 +00001204 options, 0, &port);
Benny Prijonoeebe9af2006-06-13 22:57:13 +00001205 if (status != PJ_SUCCESS) {
1206 PJSUA_UNLOCK();
1207 pjsua_perror(THIS_FILE, "Unable to open file for playback", status);
Benny Prijonod5696da2007-07-17 16:25:45 +00001208 pj_pool_release(pool);
Benny Prijonoeebe9af2006-06-13 22:57:13 +00001209 return status;
1210 }
1211
1212 status = pjmedia_conf_add_port(pjsua_var.mconf, pjsua_var.pool,
1213 port, filename, &slot);
1214 if (status != PJ_SUCCESS) {
1215 pjmedia_port_destroy(port);
1216 PJSUA_UNLOCK();
Benny Prijono32e4f492007-01-24 00:44:26 +00001217 pjsua_perror(THIS_FILE, "Unable to add file to conference bridge",
1218 status);
Benny Prijonod5696da2007-07-17 16:25:45 +00001219 pj_pool_release(pool);
Benny Prijonoeebe9af2006-06-13 22:57:13 +00001220 return status;
1221 }
1222
Benny Prijonoa66c3312007-01-21 23:12:40 +00001223 pjsua_var.player[file_id].type = 0;
Benny Prijonod5696da2007-07-17 16:25:45 +00001224 pjsua_var.player[file_id].pool = pool;
Benny Prijonoeebe9af2006-06-13 22:57:13 +00001225 pjsua_var.player[file_id].port = port;
1226 pjsua_var.player[file_id].slot = slot;
1227
1228 if (p_id) *p_id = file_id;
1229
1230 ++pjsua_var.player_cnt;
1231
1232 PJSUA_UNLOCK();
1233 return PJ_SUCCESS;
1234}
1235
1236
1237/*
Benny Prijonoa66c3312007-01-21 23:12:40 +00001238 * Create a file playlist media port, and automatically add the port
1239 * to the conference bridge.
1240 */
1241PJ_DEF(pj_status_t) pjsua_playlist_create( const pj_str_t file_names[],
1242 unsigned file_count,
1243 const pj_str_t *label,
1244 unsigned options,
1245 pjsua_player_id *p_id)
1246{
1247 unsigned slot, file_id, ptime;
Benny Prijonod5696da2007-07-17 16:25:45 +00001248 pj_pool_t *pool;
Benny Prijonoa66c3312007-01-21 23:12:40 +00001249 pjmedia_port *port;
1250 pj_status_t status;
1251
1252 if (pjsua_var.player_cnt >= PJ_ARRAY_SIZE(pjsua_var.player))
1253 return PJ_ETOOMANY;
1254
1255 PJSUA_LOCK();
1256
1257 for (file_id=0; file_id<PJ_ARRAY_SIZE(pjsua_var.player); ++file_id) {
1258 if (pjsua_var.player[file_id].port == NULL)
1259 break;
1260 }
1261
1262 if (file_id == PJ_ARRAY_SIZE(pjsua_var.player)) {
1263 /* This is unexpected */
1264 PJSUA_UNLOCK();
1265 pj_assert(0);
1266 return PJ_EBUG;
1267 }
1268
1269
1270 ptime = pjsua_var.mconf_cfg.samples_per_frame * 1000 /
1271 pjsua_var.media_cfg.clock_rate;
1272
Benny Prijonod5696da2007-07-17 16:25:45 +00001273 pool = pjsua_pool_create("playlist", 1000, 1000);
1274 if (!pool) {
1275 PJSUA_UNLOCK();
1276 return PJ_ENOMEM;
1277 }
1278
1279 status = pjmedia_wav_playlist_create(pool, label,
Benny Prijonoa66c3312007-01-21 23:12:40 +00001280 file_names, file_count,
1281 ptime, options, 0, &port);
1282 if (status != PJ_SUCCESS) {
1283 PJSUA_UNLOCK();
1284 pjsua_perror(THIS_FILE, "Unable to create playlist", status);
Benny Prijonod5696da2007-07-17 16:25:45 +00001285 pj_pool_release(pool);
Benny Prijonoa66c3312007-01-21 23:12:40 +00001286 return status;
1287 }
1288
Benny Prijonod5696da2007-07-17 16:25:45 +00001289 status = pjmedia_conf_add_port(pjsua_var.mconf, pool,
Benny Prijonoa66c3312007-01-21 23:12:40 +00001290 port, &port->info.name, &slot);
1291 if (status != PJ_SUCCESS) {
1292 pjmedia_port_destroy(port);
1293 PJSUA_UNLOCK();
1294 pjsua_perror(THIS_FILE, "Unable to add port", status);
Benny Prijonod5696da2007-07-17 16:25:45 +00001295 pj_pool_release(pool);
Benny Prijonoa66c3312007-01-21 23:12:40 +00001296 return status;
1297 }
1298
1299 pjsua_var.player[file_id].type = 1;
Benny Prijonod5696da2007-07-17 16:25:45 +00001300 pjsua_var.player[file_id].pool = pool;
Benny Prijonoa66c3312007-01-21 23:12:40 +00001301 pjsua_var.player[file_id].port = port;
1302 pjsua_var.player[file_id].slot = slot;
1303
1304 if (p_id) *p_id = file_id;
1305
1306 ++pjsua_var.player_cnt;
1307
1308 PJSUA_UNLOCK();
1309 return PJ_SUCCESS;
1310
1311}
1312
1313
1314/*
Benny Prijonoeebe9af2006-06-13 22:57:13 +00001315 * Get conference port ID associated with player.
1316 */
1317PJ_DEF(pjsua_conf_port_id) pjsua_player_get_conf_port(pjsua_player_id id)
1318{
Benny Prijonoa1e69682007-05-11 15:14:34 +00001319 PJ_ASSERT_RETURN(id>=0&&id<(int)PJ_ARRAY_SIZE(pjsua_var.player), PJ_EINVAL);
Benny Prijonoeebe9af2006-06-13 22:57:13 +00001320 PJ_ASSERT_RETURN(pjsua_var.player[id].port != NULL, PJ_EINVAL);
1321
1322 return pjsua_var.player[id].slot;
1323}
1324
Benny Prijono469b1522006-12-26 03:05:17 +00001325/*
1326 * Get the media port for the player.
1327 */
1328PJ_DEF(pj_status_t) pjsua_player_get_port( pjsua_recorder_id id,
1329 pjmedia_port **p_port)
1330{
Benny Prijonoa1e69682007-05-11 15:14:34 +00001331 PJ_ASSERT_RETURN(id>=0&&id<(int)PJ_ARRAY_SIZE(pjsua_var.player), PJ_EINVAL);
Benny Prijono469b1522006-12-26 03:05:17 +00001332 PJ_ASSERT_RETURN(pjsua_var.player[id].port != NULL, PJ_EINVAL);
1333 PJ_ASSERT_RETURN(p_port != NULL, PJ_EINVAL);
1334
1335 *p_port = pjsua_var.player[id].port;
1336
1337 return PJ_SUCCESS;
1338}
Benny Prijonoeebe9af2006-06-13 22:57:13 +00001339
1340/*
1341 * Set playback position.
1342 */
1343PJ_DEF(pj_status_t) pjsua_player_set_pos( pjsua_player_id id,
1344 pj_uint32_t samples)
1345{
Benny Prijonoa1e69682007-05-11 15:14:34 +00001346 PJ_ASSERT_RETURN(id>=0&&id<(int)PJ_ARRAY_SIZE(pjsua_var.player), PJ_EINVAL);
Benny Prijonoeebe9af2006-06-13 22:57:13 +00001347 PJ_ASSERT_RETURN(pjsua_var.player[id].port != NULL, PJ_EINVAL);
Benny Prijonoa66c3312007-01-21 23:12:40 +00001348 PJ_ASSERT_RETURN(pjsua_var.player[id].type == 0, PJ_EINVAL);
Benny Prijonoeebe9af2006-06-13 22:57:13 +00001349
1350 return pjmedia_wav_player_port_set_pos(pjsua_var.player[id].port, samples);
1351}
1352
1353
1354/*
1355 * Close the file, remove the player from the bridge, and free
1356 * resources associated with the file player.
1357 */
1358PJ_DEF(pj_status_t) pjsua_player_destroy(pjsua_player_id id)
1359{
Benny Prijonoa1e69682007-05-11 15:14:34 +00001360 PJ_ASSERT_RETURN(id>=0&&id<(int)PJ_ARRAY_SIZE(pjsua_var.player), PJ_EINVAL);
Benny Prijonoeebe9af2006-06-13 22:57:13 +00001361 PJ_ASSERT_RETURN(pjsua_var.player[id].port != NULL, PJ_EINVAL);
1362
1363 PJSUA_LOCK();
1364
1365 if (pjsua_var.player[id].port) {
1366 pjmedia_conf_remove_port(pjsua_var.mconf,
1367 pjsua_var.player[id].slot);
1368 pjmedia_port_destroy(pjsua_var.player[id].port);
1369 pjsua_var.player[id].port = NULL;
1370 pjsua_var.player[id].slot = 0xFFFF;
Benny Prijonod5696da2007-07-17 16:25:45 +00001371 pj_pool_release(pjsua_var.player[id].pool);
1372 pjsua_var.player[id].pool = NULL;
Benny Prijonoeebe9af2006-06-13 22:57:13 +00001373 pjsua_var.player_cnt--;
1374 }
1375
1376 PJSUA_UNLOCK();
1377
1378 return PJ_SUCCESS;
1379}
1380
1381
1382/*****************************************************************************
1383 * File recorder.
1384 */
1385
1386/*
1387 * Create a file recorder, and automatically connect this recorder to
1388 * the conference bridge.
1389 */
1390PJ_DEF(pj_status_t) pjsua_recorder_create( const pj_str_t *filename,
Benny Prijono8f310522006-10-20 11:08:49 +00001391 unsigned enc_type,
1392 void *enc_param,
Benny Prijonoeebe9af2006-06-13 22:57:13 +00001393 pj_ssize_t max_size,
1394 unsigned options,
Benny Prijonoeebe9af2006-06-13 22:57:13 +00001395 pjsua_recorder_id *p_id)
1396{
Benny Prijonob3cdb2b2006-10-19 15:49:47 +00001397 enum Format
1398 {
1399 FMT_UNKNOWN,
1400 FMT_WAV,
1401 FMT_MP3,
1402 };
Benny Prijonoeebe9af2006-06-13 22:57:13 +00001403 unsigned slot, file_id;
Benny Prijonoa66c3312007-01-21 23:12:40 +00001404 char path[PJ_MAXPATH];
Benny Prijonob3cdb2b2006-10-19 15:49:47 +00001405 pj_str_t ext;
Benny Prijono8f310522006-10-20 11:08:49 +00001406 int file_format;
Benny Prijonod5696da2007-07-17 16:25:45 +00001407 pj_pool_t *pool;
Benny Prijonoeebe9af2006-06-13 22:57:13 +00001408 pjmedia_port *port;
1409 pj_status_t status;
1410
Benny Prijonob3cdb2b2006-10-19 15:49:47 +00001411 /* Filename must present */
1412 PJ_ASSERT_RETURN(filename != NULL, PJ_EINVAL);
1413
Benny Prijono00cae612006-07-31 15:19:36 +00001414 /* Don't support max_size at present */
Benny Prijonob3cdb2b2006-10-19 15:49:47 +00001415 PJ_ASSERT_RETURN(max_size == 0 || max_size == -1, PJ_EINVAL);
Benny Prijono00cae612006-07-31 15:19:36 +00001416
Benny Prijono8f310522006-10-20 11:08:49 +00001417 /* Don't support encoding type at present */
1418 PJ_ASSERT_RETURN(enc_type == 0, PJ_EINVAL);
Benny Prijono00cae612006-07-31 15:19:36 +00001419
Benny Prijonoeebe9af2006-06-13 22:57:13 +00001420 if (pjsua_var.rec_cnt >= PJ_ARRAY_SIZE(pjsua_var.recorder))
1421 return PJ_ETOOMANY;
1422
Benny Prijonob3cdb2b2006-10-19 15:49:47 +00001423 /* Determine the file format */
1424 ext.ptr = filename->ptr + filename->slen - 4;
1425 ext.slen = 4;
1426
1427 if (pj_stricmp2(&ext, ".wav") == 0)
1428 file_format = FMT_WAV;
1429 else if (pj_stricmp2(&ext, ".mp3") == 0)
1430 file_format = FMT_MP3;
1431 else {
1432 PJ_LOG(1,(THIS_FILE, "pjsua_recorder_create() error: unable to "
1433 "determine file format for %.*s",
1434 (int)filename->slen, filename->ptr));
1435 return PJ_ENOTSUP;
1436 }
1437
Benny Prijonoeebe9af2006-06-13 22:57:13 +00001438 PJSUA_LOCK();
1439
1440 for (file_id=0; file_id<PJ_ARRAY_SIZE(pjsua_var.recorder); ++file_id) {
1441 if (pjsua_var.recorder[file_id].port == NULL)
1442 break;
1443 }
1444
1445 if (file_id == PJ_ARRAY_SIZE(pjsua_var.recorder)) {
1446 /* This is unexpected */
1447 PJSUA_UNLOCK();
1448 pj_assert(0);
1449 return PJ_EBUG;
1450 }
1451
1452 pj_memcpy(path, filename->ptr, filename->slen);
1453 path[filename->slen] = '\0';
Benny Prijonob3cdb2b2006-10-19 15:49:47 +00001454
Benny Prijonod5696da2007-07-17 16:25:45 +00001455 pool = pjsua_pool_create(get_basename(path, filename->slen), 1000, 1000);
1456 if (!pool) {
1457 PJSUA_UNLOCK();
1458 return PJ_ENOMEM;
1459 }
1460
Benny Prijonob3cdb2b2006-10-19 15:49:47 +00001461 if (file_format == FMT_WAV) {
Benny Prijonod5696da2007-07-17 16:25:45 +00001462 status = pjmedia_wav_writer_port_create(pool, path,
Benny Prijonob3cdb2b2006-10-19 15:49:47 +00001463 pjsua_var.media_cfg.clock_rate,
1464 pjsua_var.mconf_cfg.channel_count,
1465 pjsua_var.mconf_cfg.samples_per_frame,
1466 pjsua_var.mconf_cfg.bits_per_sample,
1467 options, 0, &port);
Benny Prijonob3cdb2b2006-10-19 15:49:47 +00001468 } else {
Benny Prijonoc95a0f02007-04-09 07:06:08 +00001469 PJ_UNUSED_ARG(enc_param);
Benny Prijonob3cdb2b2006-10-19 15:49:47 +00001470 port = NULL;
1471 status = PJ_ENOTSUP;
1472 }
1473
Benny Prijonoeebe9af2006-06-13 22:57:13 +00001474 if (status != PJ_SUCCESS) {
1475 PJSUA_UNLOCK();
1476 pjsua_perror(THIS_FILE, "Unable to open file for recording", status);
Benny Prijonod5696da2007-07-17 16:25:45 +00001477 pj_pool_release(pool);
Benny Prijonoeebe9af2006-06-13 22:57:13 +00001478 return status;
1479 }
1480
Benny Prijonod5696da2007-07-17 16:25:45 +00001481 status = pjmedia_conf_add_port(pjsua_var.mconf, pool,
Benny Prijonoeebe9af2006-06-13 22:57:13 +00001482 port, filename, &slot);
1483 if (status != PJ_SUCCESS) {
1484 pjmedia_port_destroy(port);
1485 PJSUA_UNLOCK();
Benny Prijonod5696da2007-07-17 16:25:45 +00001486 pj_pool_release(pool);
Benny Prijonoeebe9af2006-06-13 22:57:13 +00001487 return status;
1488 }
1489
1490 pjsua_var.recorder[file_id].port = port;
1491 pjsua_var.recorder[file_id].slot = slot;
Benny Prijonod5696da2007-07-17 16:25:45 +00001492 pjsua_var.recorder[file_id].pool = pool;
Benny Prijonoeebe9af2006-06-13 22:57:13 +00001493
1494 if (p_id) *p_id = file_id;
1495
1496 ++pjsua_var.rec_cnt;
1497
1498 PJSUA_UNLOCK();
1499 return PJ_SUCCESS;
1500}
1501
1502
1503/*
1504 * Get conference port associated with recorder.
1505 */
1506PJ_DEF(pjsua_conf_port_id) pjsua_recorder_get_conf_port(pjsua_recorder_id id)
1507{
Benny Prijonoa1e69682007-05-11 15:14:34 +00001508 PJ_ASSERT_RETURN(id>=0 && id<(int)PJ_ARRAY_SIZE(pjsua_var.recorder),
1509 PJ_EINVAL);
Benny Prijonoeebe9af2006-06-13 22:57:13 +00001510 PJ_ASSERT_RETURN(pjsua_var.recorder[id].port != NULL, PJ_EINVAL);
1511
1512 return pjsua_var.recorder[id].slot;
1513}
1514
Benny Prijono469b1522006-12-26 03:05:17 +00001515/*
1516 * Get the media port for the recorder.
1517 */
1518PJ_DEF(pj_status_t) pjsua_recorder_get_port( pjsua_recorder_id id,
1519 pjmedia_port **p_port)
1520{
Benny Prijonoa1e69682007-05-11 15:14:34 +00001521 PJ_ASSERT_RETURN(id>=0 && id<(int)PJ_ARRAY_SIZE(pjsua_var.recorder),
1522 PJ_EINVAL);
Benny Prijono469b1522006-12-26 03:05:17 +00001523 PJ_ASSERT_RETURN(pjsua_var.recorder[id].port != NULL, PJ_EINVAL);
1524 PJ_ASSERT_RETURN(p_port != NULL, PJ_EINVAL);
1525
1526 *p_port = pjsua_var.recorder[id].port;
1527 return PJ_SUCCESS;
1528}
Benny Prijonoeebe9af2006-06-13 22:57:13 +00001529
1530/*
1531 * Destroy recorder (this will complete recording).
1532 */
1533PJ_DEF(pj_status_t) pjsua_recorder_destroy(pjsua_recorder_id id)
1534{
Benny Prijonoa1e69682007-05-11 15:14:34 +00001535 PJ_ASSERT_RETURN(id>=0 && id<(int)PJ_ARRAY_SIZE(pjsua_var.recorder),
1536 PJ_EINVAL);
Benny Prijonoeebe9af2006-06-13 22:57:13 +00001537 PJ_ASSERT_RETURN(pjsua_var.recorder[id].port != NULL, PJ_EINVAL);
1538
1539 PJSUA_LOCK();
1540
1541 if (pjsua_var.recorder[id].port) {
1542 pjmedia_conf_remove_port(pjsua_var.mconf,
1543 pjsua_var.recorder[id].slot);
1544 pjmedia_port_destroy(pjsua_var.recorder[id].port);
1545 pjsua_var.recorder[id].port = NULL;
1546 pjsua_var.recorder[id].slot = 0xFFFF;
Benny Prijonod5696da2007-07-17 16:25:45 +00001547 pj_pool_release(pjsua_var.recorder[id].pool);
1548 pjsua_var.recorder[id].pool = NULL;
Benny Prijonoeebe9af2006-06-13 22:57:13 +00001549 pjsua_var.rec_cnt--;
1550 }
1551
1552 PJSUA_UNLOCK();
1553
1554 return PJ_SUCCESS;
1555}
1556
1557
1558/*****************************************************************************
1559 * Sound devices.
1560 */
1561
1562/*
1563 * Enum sound devices.
1564 */
1565PJ_DEF(pj_status_t) pjsua_enum_snd_devs( pjmedia_snd_dev_info info[],
1566 unsigned *count)
1567{
1568 unsigned i, dev_count;
1569
1570 dev_count = pjmedia_snd_get_dev_count();
1571
1572 if (dev_count > *count) dev_count = *count;
1573
1574 for (i=0; i<dev_count; ++i) {
1575 const pjmedia_snd_dev_info *ci;
1576
1577 ci = pjmedia_snd_get_dev_info(i);
1578 pj_memcpy(&info[i], ci, sizeof(*ci));
1579 }
1580
1581 *count = dev_count;
1582
1583 return PJ_SUCCESS;
1584}
1585
1586
1587/* Close existing sound device */
1588static void close_snd_dev(void)
1589{
1590 /* Close sound device */
1591 if (pjsua_var.snd_port) {
1592 const pjmedia_snd_dev_info *cap_info, *play_info;
1593
1594 cap_info = pjmedia_snd_get_dev_info(pjsua_var.cap_dev);
1595 play_info = pjmedia_snd_get_dev_info(pjsua_var.play_dev);
1596
1597 PJ_LOG(4,(THIS_FILE, "Closing %s sound playback device and "
1598 "%s sound capture device",
1599 play_info->name, cap_info->name));
1600
1601 pjmedia_snd_port_disconnect(pjsua_var.snd_port);
1602 pjmedia_snd_port_destroy(pjsua_var.snd_port);
1603 pjsua_var.snd_port = NULL;
1604 }
1605
1606 /* Close null sound device */
1607 if (pjsua_var.null_snd) {
1608 PJ_LOG(4,(THIS_FILE, "Closing null sound device.."));
1609 pjmedia_master_port_destroy(pjsua_var.null_snd, PJ_FALSE);
1610 pjsua_var.null_snd = NULL;
1611 }
1612}
1613
1614/*
1615 * Select or change sound device. Application may call this function at
1616 * any time to replace current sound device.
1617 */
1618PJ_DEF(pj_status_t) pjsua_set_snd_dev( int capture_dev,
1619 int playback_dev)
1620{
1621 pjmedia_port *conf_port;
Benny Prijono6dd967c2006-12-26 02:27:14 +00001622 const pjmedia_snd_dev_info *play_info;
Benny Prijono26056d82006-10-11 16:03:41 +00001623 unsigned clock_rates[] = { 0, 22050, 44100, 48000, 11025, 32000, 8000};
Benny Prijono658a1c52006-10-11 21:56:16 +00001624 unsigned selected_clock_rate = 0;
Benny Prijono26056d82006-10-11 16:03:41 +00001625 unsigned i;
Benny Prijonoc53c6d72006-11-27 09:54:03 +00001626 pjmedia_snd_stream *strm;
1627 pjmedia_snd_stream_info si;
1628 pj_str_t tmp;
Benny Prijono26056d82006-10-11 16:03:41 +00001629 pj_status_t status = -1;
Benny Prijonoeebe9af2006-06-13 22:57:13 +00001630
1631 /* Close existing sound port */
1632 close_snd_dev();
1633
1634
Benny Prijono26056d82006-10-11 16:03:41 +00001635 /* Set default clock rate */
1636 clock_rates[0] = pjsua_var.media_cfg.clock_rate;
Benny Prijonoeebe9af2006-06-13 22:57:13 +00001637
Benny Prijono26056d82006-10-11 16:03:41 +00001638 /* Attempts to open the sound device with different clock rates */
1639 for (i=0; i<PJ_ARRAY_SIZE(clock_rates); ++i) {
1640 char errmsg[PJ_ERR_MSG_SIZE];
1641
1642 PJ_LOG(4,(THIS_FILE,
1643 "pjsua_set_snd_dev(): attempting to open devices "
1644 "@%d Hz", clock_rates[i]));
1645
1646 /* Create the sound device. Sound port will start immediately. */
1647 status = pjmedia_snd_port_create(pjsua_var.pool, capture_dev,
1648 playback_dev,
1649 clock_rates[i], 1,
1650 clock_rates[i]/FPS,
1651 16, 0, &pjsua_var.snd_port);
1652
Benny Prijono658a1c52006-10-11 21:56:16 +00001653 if (status == PJ_SUCCESS) {
1654 selected_clock_rate = clock_rates[i];
Benny Prijono26056d82006-10-11 16:03:41 +00001655 break;
Benny Prijono658a1c52006-10-11 21:56:16 +00001656 }
Benny Prijono26056d82006-10-11 16:03:41 +00001657
1658 pj_strerror(status, errmsg, sizeof(errmsg));
Benny Prijono658a1c52006-10-11 21:56:16 +00001659 PJ_LOG(4, (THIS_FILE, "..failed: %s", errmsg));
Benny Prijono26056d82006-10-11 16:03:41 +00001660 }
1661
Benny Prijonoeebe9af2006-06-13 22:57:13 +00001662 if (status != PJ_SUCCESS) {
1663 pjsua_perror(THIS_FILE, "Unable to open sound device", status);
1664 return status;
1665 }
1666
1667 /* Get the port0 of the conference bridge. */
1668 conf_port = pjmedia_conf_get_master_port(pjsua_var.mconf);
1669 pj_assert(conf_port != NULL);
1670
Benny Prijonof20687a2006-08-04 18:27:19 +00001671 /* Set AEC */
Benny Prijono5da50432006-08-07 10:24:52 +00001672 pjmedia_snd_port_set_ec( pjsua_var.snd_port, pjsua_var.pool,
1673 pjsua_var.media_cfg.ec_tail_len,
1674 pjsua_var.media_cfg.ec_options);
Benny Prijonoeebe9af2006-06-13 22:57:13 +00001675
Benny Prijono658a1c52006-10-11 21:56:16 +00001676 /* If there's mismatch between sound port and conference's port,
1677 * create a resample port to bridge them.
1678 */
1679 if (selected_clock_rate != pjsua_var.media_cfg.clock_rate) {
1680 pjmedia_port *resample_port;
1681
1682 status = pjmedia_resample_port_create(pjsua_var.pool, conf_port,
1683 selected_clock_rate, 0,
1684 &resample_port);
1685 if (status != PJ_SUCCESS) {
1686 pjsua_perror("Error creating resample port", THIS_FILE, status);
1687 return status;
1688 }
1689
1690 conf_port = resample_port;
1691 }
1692
Benny Prijono52a93912006-08-04 20:54:37 +00001693 /* Connect sound port to the bridge */
1694 status = pjmedia_snd_port_connect(pjsua_var.snd_port,
1695 conf_port );
1696 if (status != PJ_SUCCESS) {
1697 pjsua_perror(THIS_FILE, "Unable to connect conference port to "
1698 "sound device", status);
1699 pjmedia_snd_port_destroy(pjsua_var.snd_port);
1700 pjsua_var.snd_port = NULL;
1701 return status;
1702 }
1703
Benny Prijonoeebe9af2006-06-13 22:57:13 +00001704 /* Save the device IDs */
1705 pjsua_var.cap_dev = capture_dev;
1706 pjsua_var.play_dev = playback_dev;
1707
Benny Prijonoc53c6d72006-11-27 09:54:03 +00001708 /* Update sound device name. */
1709 strm = pjmedia_snd_port_get_snd_stream(pjsua_var.snd_port);
1710 pjmedia_snd_stream_get_info(strm, &si);
1711 play_info = pjmedia_snd_get_dev_info(si.rec_id);
1712
1713 pjmedia_conf_set_port0_name(pjsua_var.mconf,
1714 pj_cstr(&tmp, play_info->name));
1715
Benny Prijonoeebe9af2006-06-13 22:57:13 +00001716 return PJ_SUCCESS;
1717}
1718
1719
1720/*
Benny Prijonoebdf8772007-02-01 19:25:50 +00001721 * Get currently active sound devices. If sound devices has not been created
1722 * (for example when pjsua_start() is not called), it is possible that
1723 * the function returns PJ_SUCCESS with -1 as device IDs.
1724 */
1725PJ_DEF(pj_status_t) pjsua_get_snd_dev(int *capture_dev,
1726 int *playback_dev)
1727{
1728 if (capture_dev) {
1729 *capture_dev = pjsua_var.cap_dev;
1730 }
1731 if (playback_dev) {
1732 *playback_dev = pjsua_var.play_dev;
1733 }
1734
1735 return PJ_SUCCESS;
1736}
1737
1738
1739/*
Benny Prijonoeebe9af2006-06-13 22:57:13 +00001740 * Use null sound device.
1741 */
1742PJ_DEF(pj_status_t) pjsua_set_null_snd_dev(void)
1743{
1744 pjmedia_port *conf_port;
1745 pj_status_t status;
1746
1747 /* Close existing sound device */
1748 close_snd_dev();
1749
1750 PJ_LOG(4,(THIS_FILE, "Opening null sound device.."));
1751
1752 /* Get the port0 of the conference bridge. */
1753 conf_port = pjmedia_conf_get_master_port(pjsua_var.mconf);
1754 pj_assert(conf_port != NULL);
1755
1756 /* Create master port, connecting port0 of the conference bridge to
1757 * a null port.
1758 */
1759 status = pjmedia_master_port_create(pjsua_var.pool, pjsua_var.null_port,
1760 conf_port, 0, &pjsua_var.null_snd);
1761 if (status != PJ_SUCCESS) {
1762 pjsua_perror(THIS_FILE, "Unable to create null sound device",
1763 status);
1764 return status;
1765 }
1766
1767 /* Start the master port */
1768 status = pjmedia_master_port_start(pjsua_var.null_snd);
1769 PJ_ASSERT_RETURN(status == PJ_SUCCESS, status);
1770
1771 return PJ_SUCCESS;
1772}
1773
1774
Benny Prijonoe909eac2006-07-27 22:04:56 +00001775
1776/*
1777 * Use no device!
1778 */
1779PJ_DEF(pjmedia_port*) pjsua_set_no_snd_dev(void)
1780{
1781 /* Close existing sound device */
1782 close_snd_dev();
1783
1784 pjsua_var.no_snd = PJ_TRUE;
1785 return pjmedia_conf_get_master_port(pjsua_var.mconf);
1786}
1787
1788
Benny Prijonof20687a2006-08-04 18:27:19 +00001789/*
1790 * Configure the AEC settings of the sound port.
1791 */
Benny Prijono5da50432006-08-07 10:24:52 +00001792PJ_DEF(pj_status_t) pjsua_set_ec(unsigned tail_ms, unsigned options)
Benny Prijonof20687a2006-08-04 18:27:19 +00001793{
1794 pjsua_var.media_cfg.ec_tail_len = tail_ms;
1795
1796 if (pjsua_var.snd_port)
Benny Prijono5da50432006-08-07 10:24:52 +00001797 return pjmedia_snd_port_set_ec( pjsua_var.snd_port, pjsua_var.pool,
1798 tail_ms, options);
Benny Prijonof20687a2006-08-04 18:27:19 +00001799
1800 return PJ_SUCCESS;
1801}
1802
1803
1804/*
1805 * Get current AEC tail length.
1806 */
Benny Prijono22dfe592006-08-06 12:07:13 +00001807PJ_DEF(pj_status_t) pjsua_get_ec_tail(unsigned *p_tail_ms)
Benny Prijonof20687a2006-08-04 18:27:19 +00001808{
1809 *p_tail_ms = pjsua_var.media_cfg.ec_tail_len;
1810 return PJ_SUCCESS;
1811}
1812
Benny Prijonoe909eac2006-07-27 22:04:56 +00001813
Benny Prijonoeebe9af2006-06-13 22:57:13 +00001814/*****************************************************************************
1815 * Codecs.
1816 */
1817
1818/*
1819 * Enum all supported codecs in the system.
1820 */
1821PJ_DEF(pj_status_t) pjsua_enum_codecs( pjsua_codec_info id[],
1822 unsigned *p_count )
1823{
1824 pjmedia_codec_mgr *codec_mgr;
1825 pjmedia_codec_info info[32];
1826 unsigned i, count, prio[32];
1827 pj_status_t status;
1828
1829 codec_mgr = pjmedia_endpt_get_codec_mgr(pjsua_var.med_endpt);
1830 count = PJ_ARRAY_SIZE(info);
1831 status = pjmedia_codec_mgr_enum_codecs( codec_mgr, &count, info, prio);
1832 if (status != PJ_SUCCESS) {
1833 *p_count = 0;
1834 return status;
1835 }
1836
1837 if (count > *p_count) count = *p_count;
1838
1839 for (i=0; i<count; ++i) {
1840 pjmedia_codec_info_to_id(&info[i], id[i].buf_, sizeof(id[i].buf_));
1841 id[i].codec_id = pj_str(id[i].buf_);
1842 id[i].priority = (pj_uint8_t) prio[i];
1843 }
1844
1845 *p_count = count;
1846
1847 return PJ_SUCCESS;
1848}
1849
1850
1851/*
1852 * Change codec priority.
1853 */
1854PJ_DEF(pj_status_t) pjsua_codec_set_priority( const pj_str_t *codec_id,
1855 pj_uint8_t priority )
1856{
1857 pjmedia_codec_mgr *codec_mgr;
1858
1859 codec_mgr = pjmedia_endpt_get_codec_mgr(pjsua_var.med_endpt);
1860
1861 return pjmedia_codec_mgr_set_codec_priority(codec_mgr, codec_id,
1862 priority);
1863}
1864
1865
1866/*
1867 * Get codec parameters.
1868 */
1869PJ_DEF(pj_status_t) pjsua_codec_get_param( const pj_str_t *codec_id,
1870 pjmedia_codec_param *param )
1871{
1872 const pjmedia_codec_info *info;
1873 pjmedia_codec_mgr *codec_mgr;
1874 unsigned count = 1;
1875 pj_status_t status;
1876
1877 codec_mgr = pjmedia_endpt_get_codec_mgr(pjsua_var.med_endpt);
1878
1879 status = pjmedia_codec_mgr_find_codecs_by_id(codec_mgr, codec_id,
1880 &count, &info, NULL);
1881 if (status != PJ_SUCCESS)
1882 return status;
1883
1884 if (count != 1)
1885 return PJ_ENOTFOUND;
1886
1887 status = pjmedia_codec_mgr_get_default_param( codec_mgr, info, param);
1888 return status;
1889}
1890
1891
1892/*
1893 * Set codec parameters.
1894 */
1895PJ_DEF(pj_status_t) pjsua_codec_set_param( const pj_str_t *id,
1896 const pjmedia_codec_param *param)
1897{
Benny Prijono00cae612006-07-31 15:19:36 +00001898 PJ_UNUSED_ARG(id);
1899 PJ_UNUSED_ARG(param);
Benny Prijonoeebe9af2006-06-13 22:57:13 +00001900 PJ_TODO(set_codec_param);
1901 return PJ_SUCCESS;
1902}