blob: 55f9ac76b2d325f2f60c379543131c26ee60a05b [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 Prijono11da9bc2007-09-15 08:55:00 +0000615 pjmedia_ice_simulate_lost(pjsua_var.calls[i].med_tp,
616 PJMEDIA_DIR_ENCODING,
617 pjsua_var.media_cfg.tx_drop_pct);
618
619 pjmedia_ice_simulate_lost(pjsua_var.calls[i].med_tp,
620 PJMEDIA_DIR_DECODING,
621 pjsua_var.media_cfg.rx_drop_pct);
622
Benny Prijonob681a2f2007-03-25 18:44:51 +0000623 status = pjmedia_ice_start_init(pjsua_var.calls[i].med_tp, 0, &addr,
624 &pjsua_var.stun_srv.ipv4, NULL);
Benny Prijonoc97608e2007-03-23 16:34:20 +0000625 if (status != PJ_SUCCESS) {
Benny Prijonob681a2f2007-03-25 18:44:51 +0000626 pjsua_perror(THIS_FILE, "Error starting ICE transport",
Benny Prijonoc97608e2007-03-23 16:34:20 +0000627 status);
628 goto on_error;
629 }
630
Benny Prijonob681a2f2007-03-25 18:44:51 +0000631 pjmedia_ice_get_comp(pjsua_var.calls[i].med_tp, 1, &comp);
632 next_port = pj_ntohs(comp.local_addr.ipv4.sin_port);
633 next_port += 2;
634 addr.sin_port = pj_htons((pj_uint16_t)next_port);
Benny Prijonoc97608e2007-03-23 16:34:20 +0000635 }
636
637 /* Wait until all ICE transports are ready */
638 for (i=0; i<pjsua_var.ua_cfg.max_calls; ++i) {
Benny Prijonoc97608e2007-03-23 16:34:20 +0000639
640 /* Wait until interface status is PJ_SUCCESS */
641 for (;;) {
Benny Prijonob681a2f2007-03-25 18:44:51 +0000642 status = pjmedia_ice_get_init_status(pjsua_var.calls[i].med_tp);
Benny Prijonoc97608e2007-03-23 16:34:20 +0000643 if (status == PJ_EPENDING)
644 pjsua_handle_events(100);
645 else
646 break;
647 }
648
649 if (status != PJ_SUCCESS) {
650 pjsua_perror(THIS_FILE,
651 "Error adding STUN address to ICE media transport",
652 status);
653 goto on_error;
654 }
655
Benny Prijonoc97608e2007-03-23 16:34:20 +0000656 }
Benny Prijonoeebe9af2006-06-13 22:57:13 +0000657
658 return PJ_SUCCESS;
659
660on_error:
661 for (i=0; i<pjsua_var.ua_cfg.max_calls; ++i) {
662 if (pjsua_var.calls[i].med_tp != NULL) {
Benny Prijonoc97608e2007-03-23 16:34:20 +0000663 pjmedia_transport_close(pjsua_var.calls[i].med_tp);
Benny Prijonoeebe9af2006-06-13 22:57:13 +0000664 pjsua_var.calls[i].med_tp = NULL;
665 }
666 }
667
Benny Prijonoc97608e2007-03-23 16:34:20 +0000668 return status;
669}
670
671
672/*
673 * Create UDP media transports for all the calls. This function creates
674 * one UDP media transport for each call.
675 */
Benny Prijono1f61a8f2007-08-16 10:11:44 +0000676PJ_DEF(pj_status_t) pjsua_media_transports_create(
677 const pjsua_transport_config *app_cfg)
Benny Prijonoc97608e2007-03-23 16:34:20 +0000678{
679 pjsua_transport_config cfg;
680 unsigned i;
681 pj_status_t status;
682
683
684 /* Make sure pjsua_init() has been called */
685 PJ_ASSERT_RETURN(pjsua_var.ua_cfg.max_calls>0, PJ_EINVALIDOP);
686
687 PJSUA_LOCK();
688
689 /* Delete existing media transports */
690 for (i=0; i<pjsua_var.ua_cfg.max_calls; ++i) {
691 if (pjsua_var.calls[i].med_tp != NULL) {
692 pjmedia_transport_close(pjsua_var.calls[i].med_tp);
693 pjsua_var.calls[i].med_tp = NULL;
694 }
695 }
696
697 /* Copy config */
698 pjsua_transport_config_dup(pjsua_var.pool, &cfg, app_cfg);
699
700 if (pjsua_var.media_cfg.enable_ice) {
701 status = create_ice_media_transports(&cfg);
702 } else {
703 status = create_udp_media_transports(&cfg);
704 }
705
706
Benny Prijonoeebe9af2006-06-13 22:57:13 +0000707 PJSUA_UNLOCK();
708
709 return status;
710}
711
712
Benny Prijonoc97608e2007-03-23 16:34:20 +0000713pj_status_t pjsua_media_channel_init(pjsua_call_id call_id,
714 pjsip_role_e role)
715{
716 pjsua_call *call = &pjsua_var.calls[call_id];
717
718 if (pjsua_var.media_cfg.enable_ice) {
Benny Prijonoa6bd7582007-03-28 15:49:48 +0000719 pj_ice_sess_role ice_role;
Benny Prijonoc97608e2007-03-23 16:34:20 +0000720 pj_status_t status;
721
Benny Prijonoa6bd7582007-03-28 15:49:48 +0000722 ice_role = (role==PJSIP_ROLE_UAC ? PJ_ICE_SESS_ROLE_CONTROLLING :
723 PJ_ICE_SESS_ROLE_CONTROLLED);
Benny Prijonoc97608e2007-03-23 16:34:20 +0000724
725 /* Restart ICE */
726 pjmedia_ice_stop_ice(call->med_tp);
727
728 status = pjmedia_ice_init_ice(call->med_tp, ice_role, NULL, NULL);
729 if (status != PJ_SUCCESS)
730 return status;
731 }
732
733 return PJ_SUCCESS;
734}
735
736pj_status_t pjsua_media_channel_create_sdp(pjsua_call_id call_id,
737 pj_pool_t *pool,
738 pjmedia_sdp_session **p_sdp)
739{
740 pjmedia_sdp_session *sdp;
Benny Prijono617c5bc2007-04-02 19:51:21 +0000741 pjmedia_sock_info skinfo;
Benny Prijonoc97608e2007-03-23 16:34:20 +0000742 pjsua_call *call = &pjsua_var.calls[call_id];
743 pj_status_t status;
744
Benny Prijono55e82352007-05-10 20:49:08 +0000745 /* Return error if media transport has not been created yet
746 * (e.g. application is starting)
747 */
748 if (call->med_tp == NULL) {
749 return PJ_EBUSY;
750 }
751
Benny Prijono617c5bc2007-04-02 19:51:21 +0000752 /* Get media socket info */
753 pjmedia_transport_get_info(call->med_tp, &skinfo);
754
755 /* Create SDP */
Benny Prijonoc97608e2007-03-23 16:34:20 +0000756 status = pjmedia_endpt_create_sdp(pjsua_var.med_endpt, pool, 1,
Benny Prijono617c5bc2007-04-02 19:51:21 +0000757 &skinfo, &sdp);
Benny Prijonoc97608e2007-03-23 16:34:20 +0000758 if (status != PJ_SUCCESS)
759 goto on_error;
760
761 if (pjsua_var.media_cfg.enable_ice) {
762 status = pjmedia_ice_modify_sdp(call->med_tp, pool, sdp);
763 if (status != PJ_SUCCESS)
764 goto on_error;
765 }
766
767 *p_sdp = sdp;
768 return PJ_SUCCESS;
769
770on_error:
771 pjsua_media_channel_deinit(call_id);
772 return status;
773
774}
775
776
777static void stop_media_session(pjsua_call_id call_id)
778{
779 pjsua_call *call = &pjsua_var.calls[call_id];
780
781 if (call->conf_slot != PJSUA_INVALID_ID) {
782 pjmedia_conf_remove_port(pjsua_var.mconf, call->conf_slot);
783 call->conf_slot = PJSUA_INVALID_ID;
784 }
785
786 if (call->session) {
787 pjmedia_session_destroy(call->session);
788 call->session = NULL;
789
790 PJ_LOG(4,(THIS_FILE, "Media session for call %d is destroyed",
791 call_id));
792
793 }
794
795 call->media_st = PJSUA_CALL_MEDIA_NONE;
796}
797
798pj_status_t pjsua_media_channel_deinit(pjsua_call_id call_id)
799{
800 pjsua_call *call = &pjsua_var.calls[call_id];
801
802 stop_media_session(call_id);
803
804 if (pjsua_var.media_cfg.enable_ice) {
805 pjmedia_ice_stop_ice(call->med_tp);
806 }
807
808 return PJ_SUCCESS;
809}
810
811
812/*
813 * DTMF callback from the stream.
814 */
815static void dtmf_callback(pjmedia_stream *strm, void *user_data,
816 int digit)
817{
818 PJ_UNUSED_ARG(strm);
819
820 if (pjsua_var.ua_cfg.cb.on_dtmf_digit) {
821 pjsua_call_id call_id;
822
823 call_id = (pjsua_call_id)user_data;
824 pjsua_var.ua_cfg.cb.on_dtmf_digit(call_id, digit);
825 }
826}
827
828
829pj_status_t pjsua_media_channel_update(pjsua_call_id call_id,
Benny Prijonodbce2cf2007-03-28 16:24:00 +0000830 const pjmedia_sdp_session *local_sdp,
831 const pjmedia_sdp_session *remote_sdp)
Benny Prijonoc97608e2007-03-23 16:34:20 +0000832{
833 int prev_media_st = 0;
834 pjsua_call *call = &pjsua_var.calls[call_id];
835 pjmedia_session_info sess_info;
836 pjmedia_port *media_port;
837 pj_str_t port_name;
838 char tmp[PJSIP_MAX_URL_SIZE];
839 pj_status_t status;
840
841 /* Destroy existing media session, if any. */
842 prev_media_st = call->media_st;
843 stop_media_session(call->index);
844
845 /* Create media session info based on SDP parameters.
846 * We only support one stream per session at the moment
847 */
848 status = pjmedia_session_info_from_sdp( call->inv->dlg->pool,
849 pjsua_var.med_endpt,
850 1,&sess_info,
851 local_sdp, remote_sdp);
852 if (status != PJ_SUCCESS)
853 return status;
854
855
856 /* Check if media is put on-hold */
857 if (sess_info.stream_cnt == 0 ||
858 sess_info.stream_info[0].dir == PJMEDIA_DIR_NONE)
859 {
860
861 /* Determine who puts the call on-hold */
862 if (prev_media_st == PJSUA_CALL_MEDIA_ACTIVE) {
863 if (pjmedia_sdp_neg_was_answer_remote(call->inv->neg)) {
864 /* It was local who offer hold */
865 call->media_st = PJSUA_CALL_MEDIA_LOCAL_HOLD;
866 } else {
867 call->media_st = PJSUA_CALL_MEDIA_REMOTE_HOLD;
868 }
869 }
870
871 call->media_dir = PJMEDIA_DIR_NONE;
872
Benny Prijono667952e2007-04-02 19:27:54 +0000873 /* Shutdown ICE session */
874 if (pjsua_var.media_cfg.enable_ice) {
875 pjmedia_ice_stop_ice(call->med_tp);
876 }
877
Benny Prijonoc97608e2007-03-23 16:34:20 +0000878 /* No need because we need keepalive? */
879
880 } else {
881
882 /* Start ICE */
883 if (pjsua_var.media_cfg.enable_ice) {
884 status = pjmedia_ice_start_ice(call->med_tp, call->inv->pool,
Benny Prijonofd70c9b2007-03-27 11:00:38 +0000885 remote_sdp, 0);
Benny Prijonoc97608e2007-03-23 16:34:20 +0000886 if (status != PJ_SUCCESS)
887 return status;
888 }
889
890 /* Override ptime, if this option is specified. */
891 if (pjsua_var.media_cfg.ptime != 0) {
892 sess_info.stream_info[0].param->setting.frm_per_pkt = (pj_uint8_t)
893 (pjsua_var.media_cfg.ptime / sess_info.stream_info[0].param->info.frm_ptime);
894 if (sess_info.stream_info[0].param->setting.frm_per_pkt == 0)
895 sess_info.stream_info[0].param->setting.frm_per_pkt = 1;
896 }
897
898 /* Disable VAD, if this option is specified. */
899 if (pjsua_var.media_cfg.no_vad) {
900 sess_info.stream_info[0].param->setting.vad = 0;
901 }
902
903
904 /* Optionally, application may modify other stream settings here
905 * (such as jitter buffer parameters, codec ptime, etc.)
906 */
907 sess_info.stream_info[0].jb_init = pjsua_var.media_cfg.jb_init;
908 sess_info.stream_info[0].jb_min_pre = pjsua_var.media_cfg.jb_min_pre;
909 sess_info.stream_info[0].jb_max_pre = pjsua_var.media_cfg.jb_max_pre;
910 sess_info.stream_info[0].jb_max = pjsua_var.media_cfg.jb_max;
911
912 /* Create session based on session info. */
913 status = pjmedia_session_create( pjsua_var.med_endpt, &sess_info,
914 &call->med_tp,
915 call, &call->session );
916 if (status != PJ_SUCCESS) {
917 return status;
918 }
919
920 /* If DTMF callback is installed by application, install our
921 * callback to the session.
922 */
923 if (pjsua_var.ua_cfg.cb.on_dtmf_digit) {
924 pjmedia_session_set_dtmf_callback(call->session, 0,
925 &dtmf_callback,
926 (void*)(call->index));
927 }
928
929 /* Get the port interface of the first stream in the session.
930 * We need the port interface to add to the conference bridge.
931 */
932 pjmedia_session_get_port(call->session, 0, &media_port);
933
934
935 /*
936 * Add the call to conference bridge.
937 */
938 port_name.ptr = tmp;
939 port_name.slen = pjsip_uri_print(PJSIP_URI_IN_REQ_URI,
940 call->inv->dlg->remote.info->uri,
941 tmp, sizeof(tmp));
942 if (port_name.slen < 1) {
943 port_name = pj_str("call");
944 }
945 status = pjmedia_conf_add_port( pjsua_var.mconf, call->inv->pool,
946 media_port,
947 &port_name,
948 (unsigned*)&call->conf_slot);
949 if (status != PJ_SUCCESS) {
950 return status;
951 }
952
953 /* Call's media state is active */
954 call->media_st = PJSUA_CALL_MEDIA_ACTIVE;
955 call->media_dir = sess_info.stream_info[0].dir;
956 }
957
958 /* Print info. */
959 {
960 char info[80];
961 int info_len = 0;
962 unsigned i;
963
964 for (i=0; i<sess_info.stream_cnt; ++i) {
965 int len;
966 const char *dir;
967 pjmedia_stream_info *strm_info = &sess_info.stream_info[i];
968
969 switch (strm_info->dir) {
970 case PJMEDIA_DIR_NONE:
971 dir = "inactive";
972 break;
973 case PJMEDIA_DIR_ENCODING:
974 dir = "sendonly";
975 break;
976 case PJMEDIA_DIR_DECODING:
977 dir = "recvonly";
978 break;
979 case PJMEDIA_DIR_ENCODING_DECODING:
980 dir = "sendrecv";
981 break;
982 default:
983 dir = "unknown";
984 break;
985 }
986 len = pj_ansi_sprintf( info+info_len,
987 ", stream #%d: %.*s (%s)", i,
988 (int)strm_info->fmt.encoding_name.slen,
989 strm_info->fmt.encoding_name.ptr,
990 dir);
991 if (len > 0)
992 info_len += len;
993 }
994 PJ_LOG(4,(THIS_FILE,"Media updates%s", info));
995 }
996
997 return PJ_SUCCESS;
998}
999
1000
Benny Prijonoeebe9af2006-06-13 22:57:13 +00001001/*
1002 * Get maxinum number of conference ports.
1003 */
1004PJ_DEF(unsigned) pjsua_conf_get_max_ports(void)
1005{
1006 return pjsua_var.media_cfg.max_media_ports;
1007}
1008
1009
1010/*
1011 * Get current number of active ports in the bridge.
1012 */
1013PJ_DEF(unsigned) pjsua_conf_get_active_ports(void)
1014{
1015 unsigned ports[256];
1016 unsigned count = PJ_ARRAY_SIZE(ports);
1017 pj_status_t status;
1018
1019 status = pjmedia_conf_enum_ports(pjsua_var.mconf, ports, &count);
1020 if (status != PJ_SUCCESS)
1021 count = 0;
1022
1023 return count;
1024}
1025
1026
1027/*
1028 * Enumerate all conference ports.
1029 */
1030PJ_DEF(pj_status_t) pjsua_enum_conf_ports(pjsua_conf_port_id id[],
1031 unsigned *count)
1032{
1033 return pjmedia_conf_enum_ports(pjsua_var.mconf, (unsigned*)id, count);
1034}
1035
1036
1037/*
1038 * Get information about the specified conference port
1039 */
1040PJ_DEF(pj_status_t) pjsua_conf_get_port_info( pjsua_conf_port_id id,
1041 pjsua_conf_port_info *info)
1042{
1043 pjmedia_conf_port_info cinfo;
Benny Prijono0498d902006-06-19 14:49:14 +00001044 unsigned i;
Benny Prijonoeebe9af2006-06-13 22:57:13 +00001045 pj_status_t status;
1046
1047 status = pjmedia_conf_get_port_info( pjsua_var.mconf, id, &cinfo);
1048 if (status != PJ_SUCCESS)
1049 return status;
1050
Benny Prijonoac623b32006-07-03 15:19:31 +00001051 pj_bzero(info, sizeof(*info));
Benny Prijonoeebe9af2006-06-13 22:57:13 +00001052 info->slot_id = id;
1053 info->name = cinfo.name;
1054 info->clock_rate = cinfo.clock_rate;
1055 info->channel_count = cinfo.channel_count;
1056 info->samples_per_frame = cinfo.samples_per_frame;
1057 info->bits_per_sample = cinfo.bits_per_sample;
1058
1059 /* Build array of listeners */
Benny Prijonoc78c3a32006-06-16 15:54:43 +00001060 info->listener_cnt = cinfo.listener_cnt;
1061 for (i=0; i<cinfo.listener_cnt; ++i) {
1062 info->listeners[i] = cinfo.listener_slots[i];
Benny Prijonoeebe9af2006-06-13 22:57:13 +00001063 }
1064
1065 return PJ_SUCCESS;
1066}
1067
1068
1069/*
Benny Prijonoe909eac2006-07-27 22:04:56 +00001070 * Add arbitrary media port to PJSUA's conference bridge.
1071 */
1072PJ_DEF(pj_status_t) pjsua_conf_add_port( pj_pool_t *pool,
1073 pjmedia_port *port,
1074 pjsua_conf_port_id *p_id)
1075{
1076 pj_status_t status;
1077
1078 status = pjmedia_conf_add_port(pjsua_var.mconf, pool,
1079 port, NULL, (unsigned*)p_id);
1080 if (status != PJ_SUCCESS) {
1081 if (p_id)
1082 *p_id = PJSUA_INVALID_ID;
1083 }
1084
1085 return status;
1086}
1087
1088
1089/*
1090 * Remove arbitrary slot from the conference bridge.
1091 */
1092PJ_DEF(pj_status_t) pjsua_conf_remove_port(pjsua_conf_port_id id)
1093{
1094 return pjmedia_conf_remove_port(pjsua_var.mconf, (unsigned)id);
1095}
1096
1097
1098/*
Benny Prijonoeebe9af2006-06-13 22:57:13 +00001099 * Establish unidirectional media flow from souce to sink.
1100 */
1101PJ_DEF(pj_status_t) pjsua_conf_connect( pjsua_conf_port_id source,
1102 pjsua_conf_port_id sink)
1103{
1104 return pjmedia_conf_connect_port(pjsua_var.mconf, source, sink, 0);
1105}
1106
1107
1108/*
1109 * Disconnect media flow from the source to destination port.
1110 */
1111PJ_DEF(pj_status_t) pjsua_conf_disconnect( pjsua_conf_port_id source,
1112 pjsua_conf_port_id sink)
1113{
1114 return pjmedia_conf_disconnect_port(pjsua_var.mconf, source, sink);
1115}
1116
1117
Benny Prijono6dd967c2006-12-26 02:27:14 +00001118/*
1119 * Adjust the signal level to be transmitted from the bridge to the
1120 * specified port by making it louder or quieter.
1121 */
1122PJ_DEF(pj_status_t) pjsua_conf_adjust_tx_level(pjsua_conf_port_id slot,
1123 float level)
1124{
1125 return pjmedia_conf_adjust_tx_level(pjsua_var.mconf, slot,
1126 (int)((level-1) * 128));
1127}
1128
1129/*
1130 * Adjust the signal level to be received from the specified port (to
1131 * the bridge) by making it louder or quieter.
1132 */
1133PJ_DEF(pj_status_t) pjsua_conf_adjust_rx_level(pjsua_conf_port_id slot,
1134 float level)
1135{
1136 return pjmedia_conf_adjust_rx_level(pjsua_var.mconf, slot,
1137 (int)((level-1) * 128));
1138}
1139
1140
1141/*
1142 * Get last signal level transmitted to or received from the specified port.
1143 */
1144PJ_DEF(pj_status_t) pjsua_conf_get_signal_level(pjsua_conf_port_id slot,
1145 unsigned *tx_level,
1146 unsigned *rx_level)
1147{
1148 return pjmedia_conf_get_signal_level(pjsua_var.mconf, slot,
1149 tx_level, rx_level);
1150}
1151
Benny Prijonoeebe9af2006-06-13 22:57:13 +00001152/*****************************************************************************
1153 * File player.
1154 */
1155
Benny Prijonod5696da2007-07-17 16:25:45 +00001156static char* get_basename(const char *path, unsigned len)
1157{
1158 char *p = ((char*)path) + len;
1159
1160 if (len==0)
1161 return p;
1162
Benny Prijono1f61a8f2007-08-16 10:11:44 +00001163 for (--p; p!=path && *p!='/' && *p!='\\'; ) --p;
Benny Prijonod5696da2007-07-17 16:25:45 +00001164
1165 return (p==path) ? p : p+1;
1166}
1167
1168
Benny Prijonoeebe9af2006-06-13 22:57:13 +00001169/*
1170 * Create a file player, and automatically connect this player to
1171 * the conference bridge.
1172 */
1173PJ_DEF(pj_status_t) pjsua_player_create( const pj_str_t *filename,
1174 unsigned options,
Benny Prijonoeebe9af2006-06-13 22:57:13 +00001175 pjsua_player_id *p_id)
1176{
1177 unsigned slot, file_id;
Benny Prijonoa66c3312007-01-21 23:12:40 +00001178 char path[PJ_MAXPATH];
Benny Prijonod5696da2007-07-17 16:25:45 +00001179 pj_pool_t *pool;
Benny Prijonoeebe9af2006-06-13 22:57:13 +00001180 pjmedia_port *port;
1181 pj_status_t status;
1182
1183 if (pjsua_var.player_cnt >= PJ_ARRAY_SIZE(pjsua_var.player))
1184 return PJ_ETOOMANY;
1185
1186 PJSUA_LOCK();
1187
1188 for (file_id=0; file_id<PJ_ARRAY_SIZE(pjsua_var.player); ++file_id) {
1189 if (pjsua_var.player[file_id].port == NULL)
1190 break;
1191 }
1192
1193 if (file_id == PJ_ARRAY_SIZE(pjsua_var.player)) {
1194 /* This is unexpected */
1195 PJSUA_UNLOCK();
1196 pj_assert(0);
1197 return PJ_EBUG;
1198 }
1199
1200 pj_memcpy(path, filename->ptr, filename->slen);
1201 path[filename->slen] = '\0';
Benny Prijonod5696da2007-07-17 16:25:45 +00001202
1203 pool = pjsua_pool_create(get_basename(path, filename->slen), 1000, 1000);
1204 if (!pool) {
1205 PJSUA_UNLOCK();
1206 return PJ_ENOMEM;
1207 }
1208
1209 status = pjmedia_wav_player_port_create(pool, path,
Benny Prijonoeebe9af2006-06-13 22:57:13 +00001210 pjsua_var.mconf_cfg.samples_per_frame *
1211 1000 / pjsua_var.media_cfg.clock_rate,
Benny Prijono00cae612006-07-31 15:19:36 +00001212 options, 0, &port);
Benny Prijonoeebe9af2006-06-13 22:57:13 +00001213 if (status != PJ_SUCCESS) {
1214 PJSUA_UNLOCK();
1215 pjsua_perror(THIS_FILE, "Unable to open file for playback", status);
Benny Prijonod5696da2007-07-17 16:25:45 +00001216 pj_pool_release(pool);
Benny Prijonoeebe9af2006-06-13 22:57:13 +00001217 return status;
1218 }
1219
1220 status = pjmedia_conf_add_port(pjsua_var.mconf, pjsua_var.pool,
1221 port, filename, &slot);
1222 if (status != PJ_SUCCESS) {
1223 pjmedia_port_destroy(port);
1224 PJSUA_UNLOCK();
Benny Prijono32e4f492007-01-24 00:44:26 +00001225 pjsua_perror(THIS_FILE, "Unable to add file to conference bridge",
1226 status);
Benny Prijonod5696da2007-07-17 16:25:45 +00001227 pj_pool_release(pool);
Benny Prijonoeebe9af2006-06-13 22:57:13 +00001228 return status;
1229 }
1230
Benny Prijonoa66c3312007-01-21 23:12:40 +00001231 pjsua_var.player[file_id].type = 0;
Benny Prijonod5696da2007-07-17 16:25:45 +00001232 pjsua_var.player[file_id].pool = pool;
Benny Prijonoeebe9af2006-06-13 22:57:13 +00001233 pjsua_var.player[file_id].port = port;
1234 pjsua_var.player[file_id].slot = slot;
1235
1236 if (p_id) *p_id = file_id;
1237
1238 ++pjsua_var.player_cnt;
1239
1240 PJSUA_UNLOCK();
1241 return PJ_SUCCESS;
1242}
1243
1244
1245/*
Benny Prijonoa66c3312007-01-21 23:12:40 +00001246 * Create a file playlist media port, and automatically add the port
1247 * to the conference bridge.
1248 */
1249PJ_DEF(pj_status_t) pjsua_playlist_create( const pj_str_t file_names[],
1250 unsigned file_count,
1251 const pj_str_t *label,
1252 unsigned options,
1253 pjsua_player_id *p_id)
1254{
1255 unsigned slot, file_id, ptime;
Benny Prijonod5696da2007-07-17 16:25:45 +00001256 pj_pool_t *pool;
Benny Prijonoa66c3312007-01-21 23:12:40 +00001257 pjmedia_port *port;
1258 pj_status_t status;
1259
1260 if (pjsua_var.player_cnt >= PJ_ARRAY_SIZE(pjsua_var.player))
1261 return PJ_ETOOMANY;
1262
1263 PJSUA_LOCK();
1264
1265 for (file_id=0; file_id<PJ_ARRAY_SIZE(pjsua_var.player); ++file_id) {
1266 if (pjsua_var.player[file_id].port == NULL)
1267 break;
1268 }
1269
1270 if (file_id == PJ_ARRAY_SIZE(pjsua_var.player)) {
1271 /* This is unexpected */
1272 PJSUA_UNLOCK();
1273 pj_assert(0);
1274 return PJ_EBUG;
1275 }
1276
1277
1278 ptime = pjsua_var.mconf_cfg.samples_per_frame * 1000 /
1279 pjsua_var.media_cfg.clock_rate;
1280
Benny Prijonod5696da2007-07-17 16:25:45 +00001281 pool = pjsua_pool_create("playlist", 1000, 1000);
1282 if (!pool) {
1283 PJSUA_UNLOCK();
1284 return PJ_ENOMEM;
1285 }
1286
1287 status = pjmedia_wav_playlist_create(pool, label,
Benny Prijonoa66c3312007-01-21 23:12:40 +00001288 file_names, file_count,
1289 ptime, options, 0, &port);
1290 if (status != PJ_SUCCESS) {
1291 PJSUA_UNLOCK();
1292 pjsua_perror(THIS_FILE, "Unable to create playlist", status);
Benny Prijonod5696da2007-07-17 16:25:45 +00001293 pj_pool_release(pool);
Benny Prijonoa66c3312007-01-21 23:12:40 +00001294 return status;
1295 }
1296
Benny Prijonod5696da2007-07-17 16:25:45 +00001297 status = pjmedia_conf_add_port(pjsua_var.mconf, pool,
Benny Prijonoa66c3312007-01-21 23:12:40 +00001298 port, &port->info.name, &slot);
1299 if (status != PJ_SUCCESS) {
1300 pjmedia_port_destroy(port);
1301 PJSUA_UNLOCK();
1302 pjsua_perror(THIS_FILE, "Unable to add port", status);
Benny Prijonod5696da2007-07-17 16:25:45 +00001303 pj_pool_release(pool);
Benny Prijonoa66c3312007-01-21 23:12:40 +00001304 return status;
1305 }
1306
1307 pjsua_var.player[file_id].type = 1;
Benny Prijonod5696da2007-07-17 16:25:45 +00001308 pjsua_var.player[file_id].pool = pool;
Benny Prijonoa66c3312007-01-21 23:12:40 +00001309 pjsua_var.player[file_id].port = port;
1310 pjsua_var.player[file_id].slot = slot;
1311
1312 if (p_id) *p_id = file_id;
1313
1314 ++pjsua_var.player_cnt;
1315
1316 PJSUA_UNLOCK();
1317 return PJ_SUCCESS;
1318
1319}
1320
1321
1322/*
Benny Prijonoeebe9af2006-06-13 22:57:13 +00001323 * Get conference port ID associated with player.
1324 */
1325PJ_DEF(pjsua_conf_port_id) pjsua_player_get_conf_port(pjsua_player_id id)
1326{
Benny Prijonoa1e69682007-05-11 15:14:34 +00001327 PJ_ASSERT_RETURN(id>=0&&id<(int)PJ_ARRAY_SIZE(pjsua_var.player), PJ_EINVAL);
Benny Prijonoeebe9af2006-06-13 22:57:13 +00001328 PJ_ASSERT_RETURN(pjsua_var.player[id].port != NULL, PJ_EINVAL);
1329
1330 return pjsua_var.player[id].slot;
1331}
1332
Benny Prijono469b1522006-12-26 03:05:17 +00001333/*
1334 * Get the media port for the player.
1335 */
1336PJ_DEF(pj_status_t) pjsua_player_get_port( pjsua_recorder_id id,
1337 pjmedia_port **p_port)
1338{
Benny Prijonoa1e69682007-05-11 15:14:34 +00001339 PJ_ASSERT_RETURN(id>=0&&id<(int)PJ_ARRAY_SIZE(pjsua_var.player), PJ_EINVAL);
Benny Prijono469b1522006-12-26 03:05:17 +00001340 PJ_ASSERT_RETURN(pjsua_var.player[id].port != NULL, PJ_EINVAL);
1341 PJ_ASSERT_RETURN(p_port != NULL, PJ_EINVAL);
1342
1343 *p_port = pjsua_var.player[id].port;
1344
1345 return PJ_SUCCESS;
1346}
Benny Prijonoeebe9af2006-06-13 22:57:13 +00001347
1348/*
1349 * Set playback position.
1350 */
1351PJ_DEF(pj_status_t) pjsua_player_set_pos( pjsua_player_id id,
1352 pj_uint32_t samples)
1353{
Benny Prijonoa1e69682007-05-11 15:14:34 +00001354 PJ_ASSERT_RETURN(id>=0&&id<(int)PJ_ARRAY_SIZE(pjsua_var.player), PJ_EINVAL);
Benny Prijonoeebe9af2006-06-13 22:57:13 +00001355 PJ_ASSERT_RETURN(pjsua_var.player[id].port != NULL, PJ_EINVAL);
Benny Prijonoa66c3312007-01-21 23:12:40 +00001356 PJ_ASSERT_RETURN(pjsua_var.player[id].type == 0, PJ_EINVAL);
Benny Prijonoeebe9af2006-06-13 22:57:13 +00001357
1358 return pjmedia_wav_player_port_set_pos(pjsua_var.player[id].port, samples);
1359}
1360
1361
1362/*
1363 * Close the file, remove the player from the bridge, and free
1364 * resources associated with the file player.
1365 */
1366PJ_DEF(pj_status_t) pjsua_player_destroy(pjsua_player_id id)
1367{
Benny Prijonoa1e69682007-05-11 15:14:34 +00001368 PJ_ASSERT_RETURN(id>=0&&id<(int)PJ_ARRAY_SIZE(pjsua_var.player), PJ_EINVAL);
Benny Prijonoeebe9af2006-06-13 22:57:13 +00001369 PJ_ASSERT_RETURN(pjsua_var.player[id].port != NULL, PJ_EINVAL);
1370
1371 PJSUA_LOCK();
1372
1373 if (pjsua_var.player[id].port) {
1374 pjmedia_conf_remove_port(pjsua_var.mconf,
1375 pjsua_var.player[id].slot);
1376 pjmedia_port_destroy(pjsua_var.player[id].port);
1377 pjsua_var.player[id].port = NULL;
1378 pjsua_var.player[id].slot = 0xFFFF;
Benny Prijonod5696da2007-07-17 16:25:45 +00001379 pj_pool_release(pjsua_var.player[id].pool);
1380 pjsua_var.player[id].pool = NULL;
Benny Prijonoeebe9af2006-06-13 22:57:13 +00001381 pjsua_var.player_cnt--;
1382 }
1383
1384 PJSUA_UNLOCK();
1385
1386 return PJ_SUCCESS;
1387}
1388
1389
1390/*****************************************************************************
1391 * File recorder.
1392 */
1393
1394/*
1395 * Create a file recorder, and automatically connect this recorder to
1396 * the conference bridge.
1397 */
1398PJ_DEF(pj_status_t) pjsua_recorder_create( const pj_str_t *filename,
Benny Prijono8f310522006-10-20 11:08:49 +00001399 unsigned enc_type,
1400 void *enc_param,
Benny Prijonoeebe9af2006-06-13 22:57:13 +00001401 pj_ssize_t max_size,
1402 unsigned options,
Benny Prijonoeebe9af2006-06-13 22:57:13 +00001403 pjsua_recorder_id *p_id)
1404{
Benny Prijonob3cdb2b2006-10-19 15:49:47 +00001405 enum Format
1406 {
1407 FMT_UNKNOWN,
1408 FMT_WAV,
1409 FMT_MP3,
1410 };
Benny Prijonoeebe9af2006-06-13 22:57:13 +00001411 unsigned slot, file_id;
Benny Prijonoa66c3312007-01-21 23:12:40 +00001412 char path[PJ_MAXPATH];
Benny Prijonob3cdb2b2006-10-19 15:49:47 +00001413 pj_str_t ext;
Benny Prijono8f310522006-10-20 11:08:49 +00001414 int file_format;
Benny Prijonod5696da2007-07-17 16:25:45 +00001415 pj_pool_t *pool;
Benny Prijonoeebe9af2006-06-13 22:57:13 +00001416 pjmedia_port *port;
1417 pj_status_t status;
1418
Benny Prijonob3cdb2b2006-10-19 15:49:47 +00001419 /* Filename must present */
1420 PJ_ASSERT_RETURN(filename != NULL, PJ_EINVAL);
1421
Benny Prijono00cae612006-07-31 15:19:36 +00001422 /* Don't support max_size at present */
Benny Prijonob3cdb2b2006-10-19 15:49:47 +00001423 PJ_ASSERT_RETURN(max_size == 0 || max_size == -1, PJ_EINVAL);
Benny Prijono00cae612006-07-31 15:19:36 +00001424
Benny Prijono8f310522006-10-20 11:08:49 +00001425 /* Don't support encoding type at present */
1426 PJ_ASSERT_RETURN(enc_type == 0, PJ_EINVAL);
Benny Prijono00cae612006-07-31 15:19:36 +00001427
Benny Prijonoeebe9af2006-06-13 22:57:13 +00001428 if (pjsua_var.rec_cnt >= PJ_ARRAY_SIZE(pjsua_var.recorder))
1429 return PJ_ETOOMANY;
1430
Benny Prijonob3cdb2b2006-10-19 15:49:47 +00001431 /* Determine the file format */
1432 ext.ptr = filename->ptr + filename->slen - 4;
1433 ext.slen = 4;
1434
1435 if (pj_stricmp2(&ext, ".wav") == 0)
1436 file_format = FMT_WAV;
1437 else if (pj_stricmp2(&ext, ".mp3") == 0)
1438 file_format = FMT_MP3;
1439 else {
1440 PJ_LOG(1,(THIS_FILE, "pjsua_recorder_create() error: unable to "
1441 "determine file format for %.*s",
1442 (int)filename->slen, filename->ptr));
1443 return PJ_ENOTSUP;
1444 }
1445
Benny Prijonoeebe9af2006-06-13 22:57:13 +00001446 PJSUA_LOCK();
1447
1448 for (file_id=0; file_id<PJ_ARRAY_SIZE(pjsua_var.recorder); ++file_id) {
1449 if (pjsua_var.recorder[file_id].port == NULL)
1450 break;
1451 }
1452
1453 if (file_id == PJ_ARRAY_SIZE(pjsua_var.recorder)) {
1454 /* This is unexpected */
1455 PJSUA_UNLOCK();
1456 pj_assert(0);
1457 return PJ_EBUG;
1458 }
1459
1460 pj_memcpy(path, filename->ptr, filename->slen);
1461 path[filename->slen] = '\0';
Benny Prijonob3cdb2b2006-10-19 15:49:47 +00001462
Benny Prijonod5696da2007-07-17 16:25:45 +00001463 pool = pjsua_pool_create(get_basename(path, filename->slen), 1000, 1000);
1464 if (!pool) {
1465 PJSUA_UNLOCK();
1466 return PJ_ENOMEM;
1467 }
1468
Benny Prijonob3cdb2b2006-10-19 15:49:47 +00001469 if (file_format == FMT_WAV) {
Benny Prijonod5696da2007-07-17 16:25:45 +00001470 status = pjmedia_wav_writer_port_create(pool, path,
Benny Prijonob3cdb2b2006-10-19 15:49:47 +00001471 pjsua_var.media_cfg.clock_rate,
1472 pjsua_var.mconf_cfg.channel_count,
1473 pjsua_var.mconf_cfg.samples_per_frame,
1474 pjsua_var.mconf_cfg.bits_per_sample,
1475 options, 0, &port);
Benny Prijonob3cdb2b2006-10-19 15:49:47 +00001476 } else {
Benny Prijonoc95a0f02007-04-09 07:06:08 +00001477 PJ_UNUSED_ARG(enc_param);
Benny Prijonob3cdb2b2006-10-19 15:49:47 +00001478 port = NULL;
1479 status = PJ_ENOTSUP;
1480 }
1481
Benny Prijonoeebe9af2006-06-13 22:57:13 +00001482 if (status != PJ_SUCCESS) {
1483 PJSUA_UNLOCK();
1484 pjsua_perror(THIS_FILE, "Unable to open file for recording", status);
Benny Prijonod5696da2007-07-17 16:25:45 +00001485 pj_pool_release(pool);
Benny Prijonoeebe9af2006-06-13 22:57:13 +00001486 return status;
1487 }
1488
Benny Prijonod5696da2007-07-17 16:25:45 +00001489 status = pjmedia_conf_add_port(pjsua_var.mconf, pool,
Benny Prijonoeebe9af2006-06-13 22:57:13 +00001490 port, filename, &slot);
1491 if (status != PJ_SUCCESS) {
1492 pjmedia_port_destroy(port);
1493 PJSUA_UNLOCK();
Benny Prijonod5696da2007-07-17 16:25:45 +00001494 pj_pool_release(pool);
Benny Prijonoeebe9af2006-06-13 22:57:13 +00001495 return status;
1496 }
1497
1498 pjsua_var.recorder[file_id].port = port;
1499 pjsua_var.recorder[file_id].slot = slot;
Benny Prijonod5696da2007-07-17 16:25:45 +00001500 pjsua_var.recorder[file_id].pool = pool;
Benny Prijonoeebe9af2006-06-13 22:57:13 +00001501
1502 if (p_id) *p_id = file_id;
1503
1504 ++pjsua_var.rec_cnt;
1505
1506 PJSUA_UNLOCK();
1507 return PJ_SUCCESS;
1508}
1509
1510
1511/*
1512 * Get conference port associated with recorder.
1513 */
1514PJ_DEF(pjsua_conf_port_id) pjsua_recorder_get_conf_port(pjsua_recorder_id id)
1515{
Benny Prijonoa1e69682007-05-11 15:14:34 +00001516 PJ_ASSERT_RETURN(id>=0 && id<(int)PJ_ARRAY_SIZE(pjsua_var.recorder),
1517 PJ_EINVAL);
Benny Prijonoeebe9af2006-06-13 22:57:13 +00001518 PJ_ASSERT_RETURN(pjsua_var.recorder[id].port != NULL, PJ_EINVAL);
1519
1520 return pjsua_var.recorder[id].slot;
1521}
1522
Benny Prijono469b1522006-12-26 03:05:17 +00001523/*
1524 * Get the media port for the recorder.
1525 */
1526PJ_DEF(pj_status_t) pjsua_recorder_get_port( pjsua_recorder_id id,
1527 pjmedia_port **p_port)
1528{
Benny Prijonoa1e69682007-05-11 15:14:34 +00001529 PJ_ASSERT_RETURN(id>=0 && id<(int)PJ_ARRAY_SIZE(pjsua_var.recorder),
1530 PJ_EINVAL);
Benny Prijono469b1522006-12-26 03:05:17 +00001531 PJ_ASSERT_RETURN(pjsua_var.recorder[id].port != NULL, PJ_EINVAL);
1532 PJ_ASSERT_RETURN(p_port != NULL, PJ_EINVAL);
1533
1534 *p_port = pjsua_var.recorder[id].port;
1535 return PJ_SUCCESS;
1536}
Benny Prijonoeebe9af2006-06-13 22:57:13 +00001537
1538/*
1539 * Destroy recorder (this will complete recording).
1540 */
1541PJ_DEF(pj_status_t) pjsua_recorder_destroy(pjsua_recorder_id id)
1542{
Benny Prijonoa1e69682007-05-11 15:14:34 +00001543 PJ_ASSERT_RETURN(id>=0 && id<(int)PJ_ARRAY_SIZE(pjsua_var.recorder),
1544 PJ_EINVAL);
Benny Prijonoeebe9af2006-06-13 22:57:13 +00001545 PJ_ASSERT_RETURN(pjsua_var.recorder[id].port != NULL, PJ_EINVAL);
1546
1547 PJSUA_LOCK();
1548
1549 if (pjsua_var.recorder[id].port) {
1550 pjmedia_conf_remove_port(pjsua_var.mconf,
1551 pjsua_var.recorder[id].slot);
1552 pjmedia_port_destroy(pjsua_var.recorder[id].port);
1553 pjsua_var.recorder[id].port = NULL;
1554 pjsua_var.recorder[id].slot = 0xFFFF;
Benny Prijonod5696da2007-07-17 16:25:45 +00001555 pj_pool_release(pjsua_var.recorder[id].pool);
1556 pjsua_var.recorder[id].pool = NULL;
Benny Prijonoeebe9af2006-06-13 22:57:13 +00001557 pjsua_var.rec_cnt--;
1558 }
1559
1560 PJSUA_UNLOCK();
1561
1562 return PJ_SUCCESS;
1563}
1564
1565
1566/*****************************************************************************
1567 * Sound devices.
1568 */
1569
1570/*
1571 * Enum sound devices.
1572 */
1573PJ_DEF(pj_status_t) pjsua_enum_snd_devs( pjmedia_snd_dev_info info[],
1574 unsigned *count)
1575{
1576 unsigned i, dev_count;
1577
1578 dev_count = pjmedia_snd_get_dev_count();
1579
1580 if (dev_count > *count) dev_count = *count;
1581
1582 for (i=0; i<dev_count; ++i) {
1583 const pjmedia_snd_dev_info *ci;
1584
1585 ci = pjmedia_snd_get_dev_info(i);
1586 pj_memcpy(&info[i], ci, sizeof(*ci));
1587 }
1588
1589 *count = dev_count;
1590
1591 return PJ_SUCCESS;
1592}
1593
1594
1595/* Close existing sound device */
1596static void close_snd_dev(void)
1597{
1598 /* Close sound device */
1599 if (pjsua_var.snd_port) {
1600 const pjmedia_snd_dev_info *cap_info, *play_info;
1601
1602 cap_info = pjmedia_snd_get_dev_info(pjsua_var.cap_dev);
1603 play_info = pjmedia_snd_get_dev_info(pjsua_var.play_dev);
1604
1605 PJ_LOG(4,(THIS_FILE, "Closing %s sound playback device and "
1606 "%s sound capture device",
1607 play_info->name, cap_info->name));
1608
1609 pjmedia_snd_port_disconnect(pjsua_var.snd_port);
1610 pjmedia_snd_port_destroy(pjsua_var.snd_port);
1611 pjsua_var.snd_port = NULL;
1612 }
1613
1614 /* Close null sound device */
1615 if (pjsua_var.null_snd) {
1616 PJ_LOG(4,(THIS_FILE, "Closing null sound device.."));
1617 pjmedia_master_port_destroy(pjsua_var.null_snd, PJ_FALSE);
1618 pjsua_var.null_snd = NULL;
1619 }
1620}
1621
1622/*
1623 * Select or change sound device. Application may call this function at
1624 * any time to replace current sound device.
1625 */
1626PJ_DEF(pj_status_t) pjsua_set_snd_dev( int capture_dev,
1627 int playback_dev)
1628{
1629 pjmedia_port *conf_port;
Benny Prijono6dd967c2006-12-26 02:27:14 +00001630 const pjmedia_snd_dev_info *play_info;
Benny Prijono26056d82006-10-11 16:03:41 +00001631 unsigned clock_rates[] = { 0, 22050, 44100, 48000, 11025, 32000, 8000};
Benny Prijono658a1c52006-10-11 21:56:16 +00001632 unsigned selected_clock_rate = 0;
Benny Prijono26056d82006-10-11 16:03:41 +00001633 unsigned i;
Benny Prijonoc53c6d72006-11-27 09:54:03 +00001634 pjmedia_snd_stream *strm;
1635 pjmedia_snd_stream_info si;
1636 pj_str_t tmp;
Benny Prijono26056d82006-10-11 16:03:41 +00001637 pj_status_t status = -1;
Benny Prijonoeebe9af2006-06-13 22:57:13 +00001638
1639 /* Close existing sound port */
1640 close_snd_dev();
1641
1642
Benny Prijono26056d82006-10-11 16:03:41 +00001643 /* Set default clock rate */
1644 clock_rates[0] = pjsua_var.media_cfg.clock_rate;
Benny Prijonoeebe9af2006-06-13 22:57:13 +00001645
Benny Prijono26056d82006-10-11 16:03:41 +00001646 /* Attempts to open the sound device with different clock rates */
1647 for (i=0; i<PJ_ARRAY_SIZE(clock_rates); ++i) {
1648 char errmsg[PJ_ERR_MSG_SIZE];
1649
1650 PJ_LOG(4,(THIS_FILE,
1651 "pjsua_set_snd_dev(): attempting to open devices "
1652 "@%d Hz", clock_rates[i]));
1653
1654 /* Create the sound device. Sound port will start immediately. */
1655 status = pjmedia_snd_port_create(pjsua_var.pool, capture_dev,
1656 playback_dev,
1657 clock_rates[i], 1,
1658 clock_rates[i]/FPS,
1659 16, 0, &pjsua_var.snd_port);
1660
Benny Prijono658a1c52006-10-11 21:56:16 +00001661 if (status == PJ_SUCCESS) {
1662 selected_clock_rate = clock_rates[i];
Benny Prijono26056d82006-10-11 16:03:41 +00001663 break;
Benny Prijono658a1c52006-10-11 21:56:16 +00001664 }
Benny Prijono26056d82006-10-11 16:03:41 +00001665
1666 pj_strerror(status, errmsg, sizeof(errmsg));
Benny Prijono658a1c52006-10-11 21:56:16 +00001667 PJ_LOG(4, (THIS_FILE, "..failed: %s", errmsg));
Benny Prijono26056d82006-10-11 16:03:41 +00001668 }
1669
Benny Prijonoeebe9af2006-06-13 22:57:13 +00001670 if (status != PJ_SUCCESS) {
1671 pjsua_perror(THIS_FILE, "Unable to open sound device", status);
1672 return status;
1673 }
1674
1675 /* Get the port0 of the conference bridge. */
1676 conf_port = pjmedia_conf_get_master_port(pjsua_var.mconf);
1677 pj_assert(conf_port != NULL);
1678
Benny Prijonof20687a2006-08-04 18:27:19 +00001679 /* Set AEC */
Benny Prijono5da50432006-08-07 10:24:52 +00001680 pjmedia_snd_port_set_ec( pjsua_var.snd_port, pjsua_var.pool,
1681 pjsua_var.media_cfg.ec_tail_len,
1682 pjsua_var.media_cfg.ec_options);
Benny Prijonoeebe9af2006-06-13 22:57:13 +00001683
Benny Prijono658a1c52006-10-11 21:56:16 +00001684 /* If there's mismatch between sound port and conference's port,
1685 * create a resample port to bridge them.
1686 */
1687 if (selected_clock_rate != pjsua_var.media_cfg.clock_rate) {
1688 pjmedia_port *resample_port;
1689
1690 status = pjmedia_resample_port_create(pjsua_var.pool, conf_port,
1691 selected_clock_rate, 0,
1692 &resample_port);
1693 if (status != PJ_SUCCESS) {
1694 pjsua_perror("Error creating resample port", THIS_FILE, status);
1695 return status;
1696 }
1697
1698 conf_port = resample_port;
1699 }
1700
Benny Prijono52a93912006-08-04 20:54:37 +00001701 /* Connect sound port to the bridge */
1702 status = pjmedia_snd_port_connect(pjsua_var.snd_port,
1703 conf_port );
1704 if (status != PJ_SUCCESS) {
1705 pjsua_perror(THIS_FILE, "Unable to connect conference port to "
1706 "sound device", status);
1707 pjmedia_snd_port_destroy(pjsua_var.snd_port);
1708 pjsua_var.snd_port = NULL;
1709 return status;
1710 }
1711
Benny Prijonoeebe9af2006-06-13 22:57:13 +00001712 /* Save the device IDs */
1713 pjsua_var.cap_dev = capture_dev;
1714 pjsua_var.play_dev = playback_dev;
1715
Benny Prijonoc53c6d72006-11-27 09:54:03 +00001716 /* Update sound device name. */
1717 strm = pjmedia_snd_port_get_snd_stream(pjsua_var.snd_port);
1718 pjmedia_snd_stream_get_info(strm, &si);
1719 play_info = pjmedia_snd_get_dev_info(si.rec_id);
1720
1721 pjmedia_conf_set_port0_name(pjsua_var.mconf,
1722 pj_cstr(&tmp, play_info->name));
1723
Benny Prijonoeebe9af2006-06-13 22:57:13 +00001724 return PJ_SUCCESS;
1725}
1726
1727
1728/*
Benny Prijonoebdf8772007-02-01 19:25:50 +00001729 * Get currently active sound devices. If sound devices has not been created
1730 * (for example when pjsua_start() is not called), it is possible that
1731 * the function returns PJ_SUCCESS with -1 as device IDs.
1732 */
1733PJ_DEF(pj_status_t) pjsua_get_snd_dev(int *capture_dev,
1734 int *playback_dev)
1735{
1736 if (capture_dev) {
1737 *capture_dev = pjsua_var.cap_dev;
1738 }
1739 if (playback_dev) {
1740 *playback_dev = pjsua_var.play_dev;
1741 }
1742
1743 return PJ_SUCCESS;
1744}
1745
1746
1747/*
Benny Prijonoeebe9af2006-06-13 22:57:13 +00001748 * Use null sound device.
1749 */
1750PJ_DEF(pj_status_t) pjsua_set_null_snd_dev(void)
1751{
1752 pjmedia_port *conf_port;
1753 pj_status_t status;
1754
1755 /* Close existing sound device */
1756 close_snd_dev();
1757
1758 PJ_LOG(4,(THIS_FILE, "Opening null sound device.."));
1759
1760 /* Get the port0 of the conference bridge. */
1761 conf_port = pjmedia_conf_get_master_port(pjsua_var.mconf);
1762 pj_assert(conf_port != NULL);
1763
1764 /* Create master port, connecting port0 of the conference bridge to
1765 * a null port.
1766 */
1767 status = pjmedia_master_port_create(pjsua_var.pool, pjsua_var.null_port,
1768 conf_port, 0, &pjsua_var.null_snd);
1769 if (status != PJ_SUCCESS) {
1770 pjsua_perror(THIS_FILE, "Unable to create null sound device",
1771 status);
1772 return status;
1773 }
1774
1775 /* Start the master port */
1776 status = pjmedia_master_port_start(pjsua_var.null_snd);
1777 PJ_ASSERT_RETURN(status == PJ_SUCCESS, status);
1778
1779 return PJ_SUCCESS;
1780}
1781
1782
Benny Prijonoe909eac2006-07-27 22:04:56 +00001783
1784/*
1785 * Use no device!
1786 */
1787PJ_DEF(pjmedia_port*) pjsua_set_no_snd_dev(void)
1788{
1789 /* Close existing sound device */
1790 close_snd_dev();
1791
1792 pjsua_var.no_snd = PJ_TRUE;
1793 return pjmedia_conf_get_master_port(pjsua_var.mconf);
1794}
1795
1796
Benny Prijonof20687a2006-08-04 18:27:19 +00001797/*
1798 * Configure the AEC settings of the sound port.
1799 */
Benny Prijono5da50432006-08-07 10:24:52 +00001800PJ_DEF(pj_status_t) pjsua_set_ec(unsigned tail_ms, unsigned options)
Benny Prijonof20687a2006-08-04 18:27:19 +00001801{
1802 pjsua_var.media_cfg.ec_tail_len = tail_ms;
1803
1804 if (pjsua_var.snd_port)
Benny Prijono5da50432006-08-07 10:24:52 +00001805 return pjmedia_snd_port_set_ec( pjsua_var.snd_port, pjsua_var.pool,
1806 tail_ms, options);
Benny Prijonof20687a2006-08-04 18:27:19 +00001807
1808 return PJ_SUCCESS;
1809}
1810
1811
1812/*
1813 * Get current AEC tail length.
1814 */
Benny Prijono22dfe592006-08-06 12:07:13 +00001815PJ_DEF(pj_status_t) pjsua_get_ec_tail(unsigned *p_tail_ms)
Benny Prijonof20687a2006-08-04 18:27:19 +00001816{
1817 *p_tail_ms = pjsua_var.media_cfg.ec_tail_len;
1818 return PJ_SUCCESS;
1819}
1820
Benny Prijonoe909eac2006-07-27 22:04:56 +00001821
Benny Prijonoeebe9af2006-06-13 22:57:13 +00001822/*****************************************************************************
1823 * Codecs.
1824 */
1825
1826/*
1827 * Enum all supported codecs in the system.
1828 */
1829PJ_DEF(pj_status_t) pjsua_enum_codecs( pjsua_codec_info id[],
1830 unsigned *p_count )
1831{
1832 pjmedia_codec_mgr *codec_mgr;
1833 pjmedia_codec_info info[32];
1834 unsigned i, count, prio[32];
1835 pj_status_t status;
1836
1837 codec_mgr = pjmedia_endpt_get_codec_mgr(pjsua_var.med_endpt);
1838 count = PJ_ARRAY_SIZE(info);
1839 status = pjmedia_codec_mgr_enum_codecs( codec_mgr, &count, info, prio);
1840 if (status != PJ_SUCCESS) {
1841 *p_count = 0;
1842 return status;
1843 }
1844
1845 if (count > *p_count) count = *p_count;
1846
1847 for (i=0; i<count; ++i) {
1848 pjmedia_codec_info_to_id(&info[i], id[i].buf_, sizeof(id[i].buf_));
1849 id[i].codec_id = pj_str(id[i].buf_);
1850 id[i].priority = (pj_uint8_t) prio[i];
1851 }
1852
1853 *p_count = count;
1854
1855 return PJ_SUCCESS;
1856}
1857
1858
1859/*
1860 * Change codec priority.
1861 */
1862PJ_DEF(pj_status_t) pjsua_codec_set_priority( const pj_str_t *codec_id,
1863 pj_uint8_t priority )
1864{
1865 pjmedia_codec_mgr *codec_mgr;
1866
1867 codec_mgr = pjmedia_endpt_get_codec_mgr(pjsua_var.med_endpt);
1868
1869 return pjmedia_codec_mgr_set_codec_priority(codec_mgr, codec_id,
1870 priority);
1871}
1872
1873
1874/*
1875 * Get codec parameters.
1876 */
1877PJ_DEF(pj_status_t) pjsua_codec_get_param( const pj_str_t *codec_id,
1878 pjmedia_codec_param *param )
1879{
1880 const pjmedia_codec_info *info;
1881 pjmedia_codec_mgr *codec_mgr;
1882 unsigned count = 1;
1883 pj_status_t status;
1884
1885 codec_mgr = pjmedia_endpt_get_codec_mgr(pjsua_var.med_endpt);
1886
1887 status = pjmedia_codec_mgr_find_codecs_by_id(codec_mgr, codec_id,
1888 &count, &info, NULL);
1889 if (status != PJ_SUCCESS)
1890 return status;
1891
1892 if (count != 1)
1893 return PJ_ENOTFOUND;
1894
1895 status = pjmedia_codec_mgr_get_default_param( codec_mgr, info, param);
1896 return status;
1897}
1898
1899
1900/*
1901 * Set codec parameters.
1902 */
1903PJ_DEF(pj_status_t) pjsua_codec_set_param( const pj_str_t *id,
1904 const pjmedia_codec_param *param)
1905{
Benny Prijono00cae612006-07-31 15:19:36 +00001906 PJ_UNUSED_ARG(id);
1907 PJ_UNUSED_ARG(param);
Benny Prijonoeebe9af2006-06-13 22:57:13 +00001908 PJ_TODO(set_codec_param);
1909 return PJ_SUCCESS;
1910}