blob: 9b847b39073b71d9151dabcef7009363ed2e027b [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 */
Benny Prijonoa41d66e2007-10-01 12:17:23 +0000481 // Not necessary, as pjmedia_snd_deinit() should have been called
482 // in pjmedia_endpt_destroy().
483 //pjmedia_snd_deinit();
Benny Prijonoad2e0ca2007-04-29 12:31:51 +0000484 }
Benny Prijonoaf1bb1e2006-11-21 12:39:31 +0000485
Benny Prijonode479562007-03-15 10:23:55 +0000486 /* Reset RTP port */
487 next_rtp_port = 0;
488
Benny Prijonoeebe9af2006-06-13 22:57:13 +0000489 return PJ_SUCCESS;
490}
491
492
Benny Prijonoc97608e2007-03-23 16:34:20 +0000493/* Create normal UDP media transports */
494static pj_status_t create_udp_media_transports(pjsua_transport_config *cfg)
Benny Prijonoeebe9af2006-06-13 22:57:13 +0000495{
Benny Prijonoeebe9af2006-06-13 22:57:13 +0000496 unsigned i;
Benny Prijono617c5bc2007-04-02 19:51:21 +0000497 pjmedia_sock_info skinfo;
Benny Prijonoeebe9af2006-06-13 22:57:13 +0000498 pj_status_t status;
499
Benny Prijonoeebe9af2006-06-13 22:57:13 +0000500 /* Create each media transport */
501 for (i=0; i<pjsua_var.ua_cfg.max_calls; ++i) {
502
Benny Prijono617c5bc2007-04-02 19:51:21 +0000503 status = create_rtp_rtcp_sock(cfg, &skinfo);
Benny Prijonoeebe9af2006-06-13 22:57:13 +0000504 if (status != PJ_SUCCESS) {
505 pjsua_perror(THIS_FILE, "Unable to create RTP/RTCP socket",
506 status);
507 goto on_error;
508 }
509 status = pjmedia_transport_udp_attach(pjsua_var.med_endpt, NULL,
Benny Prijono617c5bc2007-04-02 19:51:21 +0000510 &skinfo, 0,
Benny Prijonoeebe9af2006-06-13 22:57:13 +0000511 &pjsua_var.calls[i].med_tp);
512 if (status != PJ_SUCCESS) {
513 pjsua_perror(THIS_FILE, "Unable to create media transport",
514 status);
515 goto on_error;
516 }
Benny Prijono00cae612006-07-31 15:19:36 +0000517
518 pjmedia_transport_udp_simulate_lost(pjsua_var.calls[i].med_tp,
519 PJMEDIA_DIR_ENCODING,
520 pjsua_var.media_cfg.tx_drop_pct);
521
522 pjmedia_transport_udp_simulate_lost(pjsua_var.calls[i].med_tp,
523 PJMEDIA_DIR_DECODING,
524 pjsua_var.media_cfg.rx_drop_pct);
525
Benny Prijonoeebe9af2006-06-13 22:57:13 +0000526 }
527
Benny Prijonoc97608e2007-03-23 16:34:20 +0000528 return PJ_SUCCESS;
529
530on_error:
531 for (i=0; i<pjsua_var.ua_cfg.max_calls; ++i) {
532 if (pjsua_var.calls[i].med_tp != NULL) {
533 pjmedia_transport_close(pjsua_var.calls[i].med_tp);
534 pjsua_var.calls[i].med_tp = NULL;
535 }
536 }
537
538 return status;
539}
540
541
Benny Prijono096c56c2007-09-15 08:30:16 +0000542/* This callback is called when ICE negotiation completes */
543static void on_ice_complete(pjmedia_transport *tp, pj_status_t result)
544{
545 unsigned id, c;
546 pj_bool_t found = PJ_FALSE;
547
548 /* We're only interested with failure case */
549 if (result == PJ_SUCCESS)
550 return;
551
552 /* Find call which has this media transport */
553
554 PJSUA_LOCK();
555
556 for (id=0, c=0; id<PJSUA_MAX_CALLS && c<pjsua_var.call_cnt; ++id) {
557 pjsua_call *call = &pjsua_var.calls[id];
558 if (call->inv) {
559 ++c;
560
561 if (call->med_tp == tp) {
562 call->media_st = PJSUA_CALL_MEDIA_ERROR;
563 call->media_dir = PJMEDIA_DIR_NONE;
564 found = PJ_TRUE;
565 break;
566 }
567 }
568 }
569
570 PJSUA_UNLOCK();
571
572 if (found && pjsua_var.ua_cfg.cb.on_call_media_state) {
573 pjsua_var.ua_cfg.cb.on_call_media_state(id);
574 }
575}
576
577
Benny Prijonoc97608e2007-03-23 16:34:20 +0000578/* Create ICE media transports (when ice is enabled) */
579static pj_status_t create_ice_media_transports(pjsua_transport_config *cfg)
580{
581 unsigned i;
Benny Prijonob681a2f2007-03-25 18:44:51 +0000582 pj_sockaddr_in addr;
Benny Prijonoc97608e2007-03-23 16:34:20 +0000583 pj_status_t status;
584
Benny Prijonoda9785b2007-04-02 20:43:06 +0000585 /* Make sure STUN server resolution has completed */
586 status = pjsua_resolve_stun_server(PJ_TRUE);
587 if (status != PJ_SUCCESS) {
588 pjsua_perror(THIS_FILE, "Error resolving STUN server", status);
589 return status;
590 }
591
Benny Prijonob681a2f2007-03-25 18:44:51 +0000592 pj_sockaddr_in_init(&addr, 0, (pj_uint16_t)cfg->port);
593
Benny Prijonoc97608e2007-03-23 16:34:20 +0000594 /* Create each media transport */
595 for (i=0; i<pjsua_var.ua_cfg.max_calls; ++i) {
Benny Prijonoa6bd7582007-03-28 15:49:48 +0000596 pj_ice_strans_comp comp;
Benny Prijono096c56c2007-09-15 08:30:16 +0000597 pjmedia_ice_cb ice_cb;
Benny Prijonob681a2f2007-03-25 18:44:51 +0000598 int next_port;
Benny Prijonodc1fe222007-06-26 10:13:13 +0000599#if PJMEDIA_ADVERTISE_RTCP
600 enum { COMP_CNT=2 };
601#else
602 enum { COMP_CNT=1 };
603#endif
Benny Prijonoc97608e2007-03-23 16:34:20 +0000604
Benny Prijono096c56c2007-09-15 08:30:16 +0000605 pj_bzero(&ice_cb, sizeof(pjmedia_ice_cb));
606 ice_cb.on_ice_complete = &on_ice_complete;
607
Benny Prijonodc1fe222007-06-26 10:13:13 +0000608 status = pjmedia_ice_create(pjsua_var.med_endpt, NULL, COMP_CNT,
Benny Prijono096c56c2007-09-15 08:30:16 +0000609 &pjsua_var.stun_cfg, &ice_cb,
Benny Prijonoc97608e2007-03-23 16:34:20 +0000610 &pjsua_var.calls[i].med_tp);
611 if (status != PJ_SUCCESS) {
612 pjsua_perror(THIS_FILE, "Unable to create ICE media transport",
613 status);
614 goto on_error;
615 }
616
Benny Prijono11da9bc2007-09-15 08:55:00 +0000617 pjmedia_ice_simulate_lost(pjsua_var.calls[i].med_tp,
618 PJMEDIA_DIR_ENCODING,
619 pjsua_var.media_cfg.tx_drop_pct);
620
621 pjmedia_ice_simulate_lost(pjsua_var.calls[i].med_tp,
622 PJMEDIA_DIR_DECODING,
623 pjsua_var.media_cfg.rx_drop_pct);
624
Benny Prijonob681a2f2007-03-25 18:44:51 +0000625 status = pjmedia_ice_start_init(pjsua_var.calls[i].med_tp, 0, &addr,
626 &pjsua_var.stun_srv.ipv4, NULL);
Benny Prijonoc97608e2007-03-23 16:34:20 +0000627 if (status != PJ_SUCCESS) {
Benny Prijonob681a2f2007-03-25 18:44:51 +0000628 pjsua_perror(THIS_FILE, "Error starting ICE transport",
Benny Prijonoc97608e2007-03-23 16:34:20 +0000629 status);
630 goto on_error;
631 }
632
Benny Prijonob681a2f2007-03-25 18:44:51 +0000633 pjmedia_ice_get_comp(pjsua_var.calls[i].med_tp, 1, &comp);
634 next_port = pj_ntohs(comp.local_addr.ipv4.sin_port);
635 next_port += 2;
636 addr.sin_port = pj_htons((pj_uint16_t)next_port);
Benny Prijonoc97608e2007-03-23 16:34:20 +0000637 }
638
639 /* Wait until all ICE transports are ready */
640 for (i=0; i<pjsua_var.ua_cfg.max_calls; ++i) {
Benny Prijonoc97608e2007-03-23 16:34:20 +0000641
642 /* Wait until interface status is PJ_SUCCESS */
643 for (;;) {
Benny Prijonob681a2f2007-03-25 18:44:51 +0000644 status = pjmedia_ice_get_init_status(pjsua_var.calls[i].med_tp);
Benny Prijonoc97608e2007-03-23 16:34:20 +0000645 if (status == PJ_EPENDING)
646 pjsua_handle_events(100);
647 else
648 break;
649 }
650
651 if (status != PJ_SUCCESS) {
652 pjsua_perror(THIS_FILE,
653 "Error adding STUN address to ICE media transport",
654 status);
655 goto on_error;
656 }
657
Benny Prijonoc97608e2007-03-23 16:34:20 +0000658 }
Benny Prijonoeebe9af2006-06-13 22:57:13 +0000659
660 return PJ_SUCCESS;
661
662on_error:
663 for (i=0; i<pjsua_var.ua_cfg.max_calls; ++i) {
664 if (pjsua_var.calls[i].med_tp != NULL) {
Benny Prijonoc97608e2007-03-23 16:34:20 +0000665 pjmedia_transport_close(pjsua_var.calls[i].med_tp);
Benny Prijonoeebe9af2006-06-13 22:57:13 +0000666 pjsua_var.calls[i].med_tp = NULL;
667 }
668 }
669
Benny Prijonoc97608e2007-03-23 16:34:20 +0000670 return status;
671}
672
673
674/*
675 * Create UDP media transports for all the calls. This function creates
676 * one UDP media transport for each call.
677 */
Benny Prijono1f61a8f2007-08-16 10:11:44 +0000678PJ_DEF(pj_status_t) pjsua_media_transports_create(
679 const pjsua_transport_config *app_cfg)
Benny Prijonoc97608e2007-03-23 16:34:20 +0000680{
681 pjsua_transport_config cfg;
682 unsigned i;
683 pj_status_t status;
684
685
686 /* Make sure pjsua_init() has been called */
687 PJ_ASSERT_RETURN(pjsua_var.ua_cfg.max_calls>0, PJ_EINVALIDOP);
688
689 PJSUA_LOCK();
690
691 /* Delete existing media transports */
692 for (i=0; i<pjsua_var.ua_cfg.max_calls; ++i) {
693 if (pjsua_var.calls[i].med_tp != NULL) {
694 pjmedia_transport_close(pjsua_var.calls[i].med_tp);
695 pjsua_var.calls[i].med_tp = NULL;
696 }
697 }
698
699 /* Copy config */
700 pjsua_transport_config_dup(pjsua_var.pool, &cfg, app_cfg);
701
702 if (pjsua_var.media_cfg.enable_ice) {
703 status = create_ice_media_transports(&cfg);
704 } else {
705 status = create_udp_media_transports(&cfg);
706 }
707
708
Benny Prijonoeebe9af2006-06-13 22:57:13 +0000709 PJSUA_UNLOCK();
710
711 return status;
712}
713
714
Benny Prijonoc97608e2007-03-23 16:34:20 +0000715pj_status_t pjsua_media_channel_init(pjsua_call_id call_id,
716 pjsip_role_e role)
717{
718 pjsua_call *call = &pjsua_var.calls[call_id];
719
720 if (pjsua_var.media_cfg.enable_ice) {
Benny Prijonoa6bd7582007-03-28 15:49:48 +0000721 pj_ice_sess_role ice_role;
Benny Prijonoc97608e2007-03-23 16:34:20 +0000722 pj_status_t status;
723
Benny Prijonoa6bd7582007-03-28 15:49:48 +0000724 ice_role = (role==PJSIP_ROLE_UAC ? PJ_ICE_SESS_ROLE_CONTROLLING :
725 PJ_ICE_SESS_ROLE_CONTROLLED);
Benny Prijonoc97608e2007-03-23 16:34:20 +0000726
727 /* Restart ICE */
728 pjmedia_ice_stop_ice(call->med_tp);
729
730 status = pjmedia_ice_init_ice(call->med_tp, ice_role, NULL, NULL);
731 if (status != PJ_SUCCESS)
732 return status;
733 }
734
735 return PJ_SUCCESS;
736}
737
738pj_status_t pjsua_media_channel_create_sdp(pjsua_call_id call_id,
739 pj_pool_t *pool,
740 pjmedia_sdp_session **p_sdp)
741{
742 pjmedia_sdp_session *sdp;
Benny Prijono617c5bc2007-04-02 19:51:21 +0000743 pjmedia_sock_info skinfo;
Benny Prijonoc97608e2007-03-23 16:34:20 +0000744 pjsua_call *call = &pjsua_var.calls[call_id];
745 pj_status_t status;
746
Benny Prijono55e82352007-05-10 20:49:08 +0000747 /* Return error if media transport has not been created yet
748 * (e.g. application is starting)
749 */
750 if (call->med_tp == NULL) {
751 return PJ_EBUSY;
752 }
753
Benny Prijono617c5bc2007-04-02 19:51:21 +0000754 /* Get media socket info */
755 pjmedia_transport_get_info(call->med_tp, &skinfo);
756
757 /* Create SDP */
Benny Prijonoc97608e2007-03-23 16:34:20 +0000758 status = pjmedia_endpt_create_sdp(pjsua_var.med_endpt, pool, 1,
Benny Prijono617c5bc2007-04-02 19:51:21 +0000759 &skinfo, &sdp);
Benny Prijonoc97608e2007-03-23 16:34:20 +0000760 if (status != PJ_SUCCESS)
761 goto on_error;
762
763 if (pjsua_var.media_cfg.enable_ice) {
764 status = pjmedia_ice_modify_sdp(call->med_tp, pool, sdp);
765 if (status != PJ_SUCCESS)
766 goto on_error;
767 }
768
769 *p_sdp = sdp;
770 return PJ_SUCCESS;
771
772on_error:
773 pjsua_media_channel_deinit(call_id);
774 return status;
775
776}
777
778
779static void stop_media_session(pjsua_call_id call_id)
780{
781 pjsua_call *call = &pjsua_var.calls[call_id];
782
783 if (call->conf_slot != PJSUA_INVALID_ID) {
784 pjmedia_conf_remove_port(pjsua_var.mconf, call->conf_slot);
785 call->conf_slot = PJSUA_INVALID_ID;
786 }
787
788 if (call->session) {
789 pjmedia_session_destroy(call->session);
790 call->session = NULL;
791
792 PJ_LOG(4,(THIS_FILE, "Media session for call %d is destroyed",
793 call_id));
794
795 }
796
797 call->media_st = PJSUA_CALL_MEDIA_NONE;
798}
799
800pj_status_t pjsua_media_channel_deinit(pjsua_call_id call_id)
801{
802 pjsua_call *call = &pjsua_var.calls[call_id];
803
804 stop_media_session(call_id);
805
806 if (pjsua_var.media_cfg.enable_ice) {
807 pjmedia_ice_stop_ice(call->med_tp);
808 }
809
810 return PJ_SUCCESS;
811}
812
813
814/*
815 * DTMF callback from the stream.
816 */
817static void dtmf_callback(pjmedia_stream *strm, void *user_data,
818 int digit)
819{
820 PJ_UNUSED_ARG(strm);
821
822 if (pjsua_var.ua_cfg.cb.on_dtmf_digit) {
823 pjsua_call_id call_id;
824
825 call_id = (pjsua_call_id)user_data;
826 pjsua_var.ua_cfg.cb.on_dtmf_digit(call_id, digit);
827 }
828}
829
830
831pj_status_t pjsua_media_channel_update(pjsua_call_id call_id,
Benny Prijonodbce2cf2007-03-28 16:24:00 +0000832 const pjmedia_sdp_session *local_sdp,
833 const pjmedia_sdp_session *remote_sdp)
Benny Prijonoc97608e2007-03-23 16:34:20 +0000834{
835 int prev_media_st = 0;
836 pjsua_call *call = &pjsua_var.calls[call_id];
837 pjmedia_session_info sess_info;
838 pjmedia_port *media_port;
839 pj_str_t port_name;
840 char tmp[PJSIP_MAX_URL_SIZE];
841 pj_status_t status;
842
843 /* Destroy existing media session, if any. */
844 prev_media_st = call->media_st;
845 stop_media_session(call->index);
846
847 /* Create media session info based on SDP parameters.
848 * We only support one stream per session at the moment
849 */
850 status = pjmedia_session_info_from_sdp( call->inv->dlg->pool,
851 pjsua_var.med_endpt,
852 1,&sess_info,
853 local_sdp, remote_sdp);
854 if (status != PJ_SUCCESS)
855 return status;
856
857
858 /* Check if media is put on-hold */
859 if (sess_info.stream_cnt == 0 ||
860 sess_info.stream_info[0].dir == PJMEDIA_DIR_NONE)
861 {
862
863 /* Determine who puts the call on-hold */
864 if (prev_media_st == PJSUA_CALL_MEDIA_ACTIVE) {
865 if (pjmedia_sdp_neg_was_answer_remote(call->inv->neg)) {
866 /* It was local who offer hold */
867 call->media_st = PJSUA_CALL_MEDIA_LOCAL_HOLD;
868 } else {
869 call->media_st = PJSUA_CALL_MEDIA_REMOTE_HOLD;
870 }
871 }
872
873 call->media_dir = PJMEDIA_DIR_NONE;
874
Benny Prijono667952e2007-04-02 19:27:54 +0000875 /* Shutdown ICE session */
876 if (pjsua_var.media_cfg.enable_ice) {
877 pjmedia_ice_stop_ice(call->med_tp);
878 }
879
Benny Prijonoc97608e2007-03-23 16:34:20 +0000880 /* No need because we need keepalive? */
881
882 } else {
883
884 /* Start ICE */
885 if (pjsua_var.media_cfg.enable_ice) {
886 status = pjmedia_ice_start_ice(call->med_tp, call->inv->pool,
Benny Prijonofd70c9b2007-03-27 11:00:38 +0000887 remote_sdp, 0);
Benny Prijonoc97608e2007-03-23 16:34:20 +0000888 if (status != PJ_SUCCESS)
889 return status;
890 }
891
892 /* Override ptime, if this option is specified. */
893 if (pjsua_var.media_cfg.ptime != 0) {
894 sess_info.stream_info[0].param->setting.frm_per_pkt = (pj_uint8_t)
895 (pjsua_var.media_cfg.ptime / sess_info.stream_info[0].param->info.frm_ptime);
896 if (sess_info.stream_info[0].param->setting.frm_per_pkt == 0)
897 sess_info.stream_info[0].param->setting.frm_per_pkt = 1;
898 }
899
900 /* Disable VAD, if this option is specified. */
901 if (pjsua_var.media_cfg.no_vad) {
902 sess_info.stream_info[0].param->setting.vad = 0;
903 }
904
905
906 /* Optionally, application may modify other stream settings here
907 * (such as jitter buffer parameters, codec ptime, etc.)
908 */
909 sess_info.stream_info[0].jb_init = pjsua_var.media_cfg.jb_init;
910 sess_info.stream_info[0].jb_min_pre = pjsua_var.media_cfg.jb_min_pre;
911 sess_info.stream_info[0].jb_max_pre = pjsua_var.media_cfg.jb_max_pre;
912 sess_info.stream_info[0].jb_max = pjsua_var.media_cfg.jb_max;
913
914 /* Create session based on session info. */
915 status = pjmedia_session_create( pjsua_var.med_endpt, &sess_info,
916 &call->med_tp,
917 call, &call->session );
918 if (status != PJ_SUCCESS) {
919 return status;
920 }
921
922 /* If DTMF callback is installed by application, install our
923 * callback to the session.
924 */
925 if (pjsua_var.ua_cfg.cb.on_dtmf_digit) {
926 pjmedia_session_set_dtmf_callback(call->session, 0,
927 &dtmf_callback,
928 (void*)(call->index));
929 }
930
931 /* Get the port interface of the first stream in the session.
932 * We need the port interface to add to the conference bridge.
933 */
934 pjmedia_session_get_port(call->session, 0, &media_port);
935
936
937 /*
938 * Add the call to conference bridge.
939 */
940 port_name.ptr = tmp;
941 port_name.slen = pjsip_uri_print(PJSIP_URI_IN_REQ_URI,
942 call->inv->dlg->remote.info->uri,
943 tmp, sizeof(tmp));
944 if (port_name.slen < 1) {
945 port_name = pj_str("call");
946 }
947 status = pjmedia_conf_add_port( pjsua_var.mconf, call->inv->pool,
948 media_port,
949 &port_name,
950 (unsigned*)&call->conf_slot);
951 if (status != PJ_SUCCESS) {
952 return status;
953 }
954
955 /* Call's media state is active */
956 call->media_st = PJSUA_CALL_MEDIA_ACTIVE;
957 call->media_dir = sess_info.stream_info[0].dir;
958 }
959
960 /* Print info. */
961 {
962 char info[80];
963 int info_len = 0;
964 unsigned i;
965
966 for (i=0; i<sess_info.stream_cnt; ++i) {
967 int len;
968 const char *dir;
969 pjmedia_stream_info *strm_info = &sess_info.stream_info[i];
970
971 switch (strm_info->dir) {
972 case PJMEDIA_DIR_NONE:
973 dir = "inactive";
974 break;
975 case PJMEDIA_DIR_ENCODING:
976 dir = "sendonly";
977 break;
978 case PJMEDIA_DIR_DECODING:
979 dir = "recvonly";
980 break;
981 case PJMEDIA_DIR_ENCODING_DECODING:
982 dir = "sendrecv";
983 break;
984 default:
985 dir = "unknown";
986 break;
987 }
988 len = pj_ansi_sprintf( info+info_len,
989 ", stream #%d: %.*s (%s)", i,
990 (int)strm_info->fmt.encoding_name.slen,
991 strm_info->fmt.encoding_name.ptr,
992 dir);
993 if (len > 0)
994 info_len += len;
995 }
996 PJ_LOG(4,(THIS_FILE,"Media updates%s", info));
997 }
998
999 return PJ_SUCCESS;
1000}
1001
1002
Benny Prijonoeebe9af2006-06-13 22:57:13 +00001003/*
1004 * Get maxinum number of conference ports.
1005 */
1006PJ_DEF(unsigned) pjsua_conf_get_max_ports(void)
1007{
1008 return pjsua_var.media_cfg.max_media_ports;
1009}
1010
1011
1012/*
1013 * Get current number of active ports in the bridge.
1014 */
1015PJ_DEF(unsigned) pjsua_conf_get_active_ports(void)
1016{
1017 unsigned ports[256];
1018 unsigned count = PJ_ARRAY_SIZE(ports);
1019 pj_status_t status;
1020
1021 status = pjmedia_conf_enum_ports(pjsua_var.mconf, ports, &count);
1022 if (status != PJ_SUCCESS)
1023 count = 0;
1024
1025 return count;
1026}
1027
1028
1029/*
1030 * Enumerate all conference ports.
1031 */
1032PJ_DEF(pj_status_t) pjsua_enum_conf_ports(pjsua_conf_port_id id[],
1033 unsigned *count)
1034{
1035 return pjmedia_conf_enum_ports(pjsua_var.mconf, (unsigned*)id, count);
1036}
1037
1038
1039/*
1040 * Get information about the specified conference port
1041 */
1042PJ_DEF(pj_status_t) pjsua_conf_get_port_info( pjsua_conf_port_id id,
1043 pjsua_conf_port_info *info)
1044{
1045 pjmedia_conf_port_info cinfo;
Benny Prijono0498d902006-06-19 14:49:14 +00001046 unsigned i;
Benny Prijonoeebe9af2006-06-13 22:57:13 +00001047 pj_status_t status;
1048
1049 status = pjmedia_conf_get_port_info( pjsua_var.mconf, id, &cinfo);
1050 if (status != PJ_SUCCESS)
1051 return status;
1052
Benny Prijonoac623b32006-07-03 15:19:31 +00001053 pj_bzero(info, sizeof(*info));
Benny Prijonoeebe9af2006-06-13 22:57:13 +00001054 info->slot_id = id;
1055 info->name = cinfo.name;
1056 info->clock_rate = cinfo.clock_rate;
1057 info->channel_count = cinfo.channel_count;
1058 info->samples_per_frame = cinfo.samples_per_frame;
1059 info->bits_per_sample = cinfo.bits_per_sample;
1060
1061 /* Build array of listeners */
Benny Prijonoc78c3a32006-06-16 15:54:43 +00001062 info->listener_cnt = cinfo.listener_cnt;
1063 for (i=0; i<cinfo.listener_cnt; ++i) {
1064 info->listeners[i] = cinfo.listener_slots[i];
Benny Prijonoeebe9af2006-06-13 22:57:13 +00001065 }
1066
1067 return PJ_SUCCESS;
1068}
1069
1070
1071/*
Benny Prijonoe909eac2006-07-27 22:04:56 +00001072 * Add arbitrary media port to PJSUA's conference bridge.
1073 */
1074PJ_DEF(pj_status_t) pjsua_conf_add_port( pj_pool_t *pool,
1075 pjmedia_port *port,
1076 pjsua_conf_port_id *p_id)
1077{
1078 pj_status_t status;
1079
1080 status = pjmedia_conf_add_port(pjsua_var.mconf, pool,
1081 port, NULL, (unsigned*)p_id);
1082 if (status != PJ_SUCCESS) {
1083 if (p_id)
1084 *p_id = PJSUA_INVALID_ID;
1085 }
1086
1087 return status;
1088}
1089
1090
1091/*
1092 * Remove arbitrary slot from the conference bridge.
1093 */
1094PJ_DEF(pj_status_t) pjsua_conf_remove_port(pjsua_conf_port_id id)
1095{
1096 return pjmedia_conf_remove_port(pjsua_var.mconf, (unsigned)id);
1097}
1098
1099
1100/*
Benny Prijonoeebe9af2006-06-13 22:57:13 +00001101 * Establish unidirectional media flow from souce to sink.
1102 */
1103PJ_DEF(pj_status_t) pjsua_conf_connect( pjsua_conf_port_id source,
1104 pjsua_conf_port_id sink)
1105{
1106 return pjmedia_conf_connect_port(pjsua_var.mconf, source, sink, 0);
1107}
1108
1109
1110/*
1111 * Disconnect media flow from the source to destination port.
1112 */
1113PJ_DEF(pj_status_t) pjsua_conf_disconnect( pjsua_conf_port_id source,
1114 pjsua_conf_port_id sink)
1115{
1116 return pjmedia_conf_disconnect_port(pjsua_var.mconf, source, sink);
1117}
1118
1119
Benny Prijono6dd967c2006-12-26 02:27:14 +00001120/*
1121 * Adjust the signal level to be transmitted from the bridge to the
1122 * specified port by making it louder or quieter.
1123 */
1124PJ_DEF(pj_status_t) pjsua_conf_adjust_tx_level(pjsua_conf_port_id slot,
1125 float level)
1126{
1127 return pjmedia_conf_adjust_tx_level(pjsua_var.mconf, slot,
1128 (int)((level-1) * 128));
1129}
1130
1131/*
1132 * Adjust the signal level to be received from the specified port (to
1133 * the bridge) by making it louder or quieter.
1134 */
1135PJ_DEF(pj_status_t) pjsua_conf_adjust_rx_level(pjsua_conf_port_id slot,
1136 float level)
1137{
1138 return pjmedia_conf_adjust_rx_level(pjsua_var.mconf, slot,
1139 (int)((level-1) * 128));
1140}
1141
1142
1143/*
1144 * Get last signal level transmitted to or received from the specified port.
1145 */
1146PJ_DEF(pj_status_t) pjsua_conf_get_signal_level(pjsua_conf_port_id slot,
1147 unsigned *tx_level,
1148 unsigned *rx_level)
1149{
1150 return pjmedia_conf_get_signal_level(pjsua_var.mconf, slot,
1151 tx_level, rx_level);
1152}
1153
Benny Prijonoeebe9af2006-06-13 22:57:13 +00001154/*****************************************************************************
1155 * File player.
1156 */
1157
Benny Prijonod5696da2007-07-17 16:25:45 +00001158static char* get_basename(const char *path, unsigned len)
1159{
1160 char *p = ((char*)path) + len;
1161
1162 if (len==0)
1163 return p;
1164
Benny Prijono1f61a8f2007-08-16 10:11:44 +00001165 for (--p; p!=path && *p!='/' && *p!='\\'; ) --p;
Benny Prijonod5696da2007-07-17 16:25:45 +00001166
1167 return (p==path) ? p : p+1;
1168}
1169
1170
Benny Prijonoeebe9af2006-06-13 22:57:13 +00001171/*
1172 * Create a file player, and automatically connect this player to
1173 * the conference bridge.
1174 */
1175PJ_DEF(pj_status_t) pjsua_player_create( const pj_str_t *filename,
1176 unsigned options,
Benny Prijonoeebe9af2006-06-13 22:57:13 +00001177 pjsua_player_id *p_id)
1178{
1179 unsigned slot, file_id;
Benny Prijonoa66c3312007-01-21 23:12:40 +00001180 char path[PJ_MAXPATH];
Benny Prijonod5696da2007-07-17 16:25:45 +00001181 pj_pool_t *pool;
Benny Prijonoeebe9af2006-06-13 22:57:13 +00001182 pjmedia_port *port;
1183 pj_status_t status;
1184
1185 if (pjsua_var.player_cnt >= PJ_ARRAY_SIZE(pjsua_var.player))
1186 return PJ_ETOOMANY;
1187
1188 PJSUA_LOCK();
1189
1190 for (file_id=0; file_id<PJ_ARRAY_SIZE(pjsua_var.player); ++file_id) {
1191 if (pjsua_var.player[file_id].port == NULL)
1192 break;
1193 }
1194
1195 if (file_id == PJ_ARRAY_SIZE(pjsua_var.player)) {
1196 /* This is unexpected */
1197 PJSUA_UNLOCK();
1198 pj_assert(0);
1199 return PJ_EBUG;
1200 }
1201
1202 pj_memcpy(path, filename->ptr, filename->slen);
1203 path[filename->slen] = '\0';
Benny Prijonod5696da2007-07-17 16:25:45 +00001204
1205 pool = pjsua_pool_create(get_basename(path, filename->slen), 1000, 1000);
1206 if (!pool) {
1207 PJSUA_UNLOCK();
1208 return PJ_ENOMEM;
1209 }
1210
1211 status = pjmedia_wav_player_port_create(pool, path,
Benny Prijonoeebe9af2006-06-13 22:57:13 +00001212 pjsua_var.mconf_cfg.samples_per_frame *
1213 1000 / pjsua_var.media_cfg.clock_rate,
Benny Prijono00cae612006-07-31 15:19:36 +00001214 options, 0, &port);
Benny Prijonoeebe9af2006-06-13 22:57:13 +00001215 if (status != PJ_SUCCESS) {
1216 PJSUA_UNLOCK();
1217 pjsua_perror(THIS_FILE, "Unable to open file for playback", status);
Benny Prijonod5696da2007-07-17 16:25:45 +00001218 pj_pool_release(pool);
Benny Prijonoeebe9af2006-06-13 22:57:13 +00001219 return status;
1220 }
1221
1222 status = pjmedia_conf_add_port(pjsua_var.mconf, pjsua_var.pool,
1223 port, filename, &slot);
1224 if (status != PJ_SUCCESS) {
1225 pjmedia_port_destroy(port);
1226 PJSUA_UNLOCK();
Benny Prijono32e4f492007-01-24 00:44:26 +00001227 pjsua_perror(THIS_FILE, "Unable to add file to conference bridge",
1228 status);
Benny Prijonod5696da2007-07-17 16:25:45 +00001229 pj_pool_release(pool);
Benny Prijonoeebe9af2006-06-13 22:57:13 +00001230 return status;
1231 }
1232
Benny Prijonoa66c3312007-01-21 23:12:40 +00001233 pjsua_var.player[file_id].type = 0;
Benny Prijonod5696da2007-07-17 16:25:45 +00001234 pjsua_var.player[file_id].pool = pool;
Benny Prijonoeebe9af2006-06-13 22:57:13 +00001235 pjsua_var.player[file_id].port = port;
1236 pjsua_var.player[file_id].slot = slot;
1237
1238 if (p_id) *p_id = file_id;
1239
1240 ++pjsua_var.player_cnt;
1241
1242 PJSUA_UNLOCK();
1243 return PJ_SUCCESS;
1244}
1245
1246
1247/*
Benny Prijonoa66c3312007-01-21 23:12:40 +00001248 * Create a file playlist media port, and automatically add the port
1249 * to the conference bridge.
1250 */
1251PJ_DEF(pj_status_t) pjsua_playlist_create( const pj_str_t file_names[],
1252 unsigned file_count,
1253 const pj_str_t *label,
1254 unsigned options,
1255 pjsua_player_id *p_id)
1256{
1257 unsigned slot, file_id, ptime;
Benny Prijonod5696da2007-07-17 16:25:45 +00001258 pj_pool_t *pool;
Benny Prijonoa66c3312007-01-21 23:12:40 +00001259 pjmedia_port *port;
1260 pj_status_t status;
1261
1262 if (pjsua_var.player_cnt >= PJ_ARRAY_SIZE(pjsua_var.player))
1263 return PJ_ETOOMANY;
1264
1265 PJSUA_LOCK();
1266
1267 for (file_id=0; file_id<PJ_ARRAY_SIZE(pjsua_var.player); ++file_id) {
1268 if (pjsua_var.player[file_id].port == NULL)
1269 break;
1270 }
1271
1272 if (file_id == PJ_ARRAY_SIZE(pjsua_var.player)) {
1273 /* This is unexpected */
1274 PJSUA_UNLOCK();
1275 pj_assert(0);
1276 return PJ_EBUG;
1277 }
1278
1279
1280 ptime = pjsua_var.mconf_cfg.samples_per_frame * 1000 /
1281 pjsua_var.media_cfg.clock_rate;
1282
Benny Prijonod5696da2007-07-17 16:25:45 +00001283 pool = pjsua_pool_create("playlist", 1000, 1000);
1284 if (!pool) {
1285 PJSUA_UNLOCK();
1286 return PJ_ENOMEM;
1287 }
1288
1289 status = pjmedia_wav_playlist_create(pool, label,
Benny Prijonoa66c3312007-01-21 23:12:40 +00001290 file_names, file_count,
1291 ptime, options, 0, &port);
1292 if (status != PJ_SUCCESS) {
1293 PJSUA_UNLOCK();
1294 pjsua_perror(THIS_FILE, "Unable to create playlist", 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
Benny Prijonod5696da2007-07-17 16:25:45 +00001299 status = pjmedia_conf_add_port(pjsua_var.mconf, pool,
Benny Prijonoa66c3312007-01-21 23:12:40 +00001300 port, &port->info.name, &slot);
1301 if (status != PJ_SUCCESS) {
1302 pjmedia_port_destroy(port);
1303 PJSUA_UNLOCK();
1304 pjsua_perror(THIS_FILE, "Unable to add port", status);
Benny Prijonod5696da2007-07-17 16:25:45 +00001305 pj_pool_release(pool);
Benny Prijonoa66c3312007-01-21 23:12:40 +00001306 return status;
1307 }
1308
1309 pjsua_var.player[file_id].type = 1;
Benny Prijonod5696da2007-07-17 16:25:45 +00001310 pjsua_var.player[file_id].pool = pool;
Benny Prijonoa66c3312007-01-21 23:12:40 +00001311 pjsua_var.player[file_id].port = port;
1312 pjsua_var.player[file_id].slot = slot;
1313
1314 if (p_id) *p_id = file_id;
1315
1316 ++pjsua_var.player_cnt;
1317
1318 PJSUA_UNLOCK();
1319 return PJ_SUCCESS;
1320
1321}
1322
1323
1324/*
Benny Prijonoeebe9af2006-06-13 22:57:13 +00001325 * Get conference port ID associated with player.
1326 */
1327PJ_DEF(pjsua_conf_port_id) pjsua_player_get_conf_port(pjsua_player_id id)
1328{
Benny Prijonoa1e69682007-05-11 15:14:34 +00001329 PJ_ASSERT_RETURN(id>=0&&id<(int)PJ_ARRAY_SIZE(pjsua_var.player), PJ_EINVAL);
Benny Prijonoeebe9af2006-06-13 22:57:13 +00001330 PJ_ASSERT_RETURN(pjsua_var.player[id].port != NULL, PJ_EINVAL);
1331
1332 return pjsua_var.player[id].slot;
1333}
1334
Benny Prijono469b1522006-12-26 03:05:17 +00001335/*
1336 * Get the media port for the player.
1337 */
1338PJ_DEF(pj_status_t) pjsua_player_get_port( pjsua_recorder_id id,
1339 pjmedia_port **p_port)
1340{
Benny Prijonoa1e69682007-05-11 15:14:34 +00001341 PJ_ASSERT_RETURN(id>=0&&id<(int)PJ_ARRAY_SIZE(pjsua_var.player), PJ_EINVAL);
Benny Prijono469b1522006-12-26 03:05:17 +00001342 PJ_ASSERT_RETURN(pjsua_var.player[id].port != NULL, PJ_EINVAL);
1343 PJ_ASSERT_RETURN(p_port != NULL, PJ_EINVAL);
1344
1345 *p_port = pjsua_var.player[id].port;
1346
1347 return PJ_SUCCESS;
1348}
Benny Prijonoeebe9af2006-06-13 22:57:13 +00001349
1350/*
1351 * Set playback position.
1352 */
1353PJ_DEF(pj_status_t) pjsua_player_set_pos( pjsua_player_id id,
1354 pj_uint32_t samples)
1355{
Benny Prijonoa1e69682007-05-11 15:14:34 +00001356 PJ_ASSERT_RETURN(id>=0&&id<(int)PJ_ARRAY_SIZE(pjsua_var.player), PJ_EINVAL);
Benny Prijonoeebe9af2006-06-13 22:57:13 +00001357 PJ_ASSERT_RETURN(pjsua_var.player[id].port != NULL, PJ_EINVAL);
Benny Prijonoa66c3312007-01-21 23:12:40 +00001358 PJ_ASSERT_RETURN(pjsua_var.player[id].type == 0, PJ_EINVAL);
Benny Prijonoeebe9af2006-06-13 22:57:13 +00001359
1360 return pjmedia_wav_player_port_set_pos(pjsua_var.player[id].port, samples);
1361}
1362
1363
1364/*
1365 * Close the file, remove the player from the bridge, and free
1366 * resources associated with the file player.
1367 */
1368PJ_DEF(pj_status_t) pjsua_player_destroy(pjsua_player_id id)
1369{
Benny Prijonoa1e69682007-05-11 15:14:34 +00001370 PJ_ASSERT_RETURN(id>=0&&id<(int)PJ_ARRAY_SIZE(pjsua_var.player), PJ_EINVAL);
Benny Prijonoeebe9af2006-06-13 22:57:13 +00001371 PJ_ASSERT_RETURN(pjsua_var.player[id].port != NULL, PJ_EINVAL);
1372
1373 PJSUA_LOCK();
1374
1375 if (pjsua_var.player[id].port) {
1376 pjmedia_conf_remove_port(pjsua_var.mconf,
1377 pjsua_var.player[id].slot);
1378 pjmedia_port_destroy(pjsua_var.player[id].port);
1379 pjsua_var.player[id].port = NULL;
1380 pjsua_var.player[id].slot = 0xFFFF;
Benny Prijonod5696da2007-07-17 16:25:45 +00001381 pj_pool_release(pjsua_var.player[id].pool);
1382 pjsua_var.player[id].pool = NULL;
Benny Prijonoeebe9af2006-06-13 22:57:13 +00001383 pjsua_var.player_cnt--;
1384 }
1385
1386 PJSUA_UNLOCK();
1387
1388 return PJ_SUCCESS;
1389}
1390
1391
1392/*****************************************************************************
1393 * File recorder.
1394 */
1395
1396/*
1397 * Create a file recorder, and automatically connect this recorder to
1398 * the conference bridge.
1399 */
1400PJ_DEF(pj_status_t) pjsua_recorder_create( const pj_str_t *filename,
Benny Prijono8f310522006-10-20 11:08:49 +00001401 unsigned enc_type,
1402 void *enc_param,
Benny Prijonoeebe9af2006-06-13 22:57:13 +00001403 pj_ssize_t max_size,
1404 unsigned options,
Benny Prijonoeebe9af2006-06-13 22:57:13 +00001405 pjsua_recorder_id *p_id)
1406{
Benny Prijonob3cdb2b2006-10-19 15:49:47 +00001407 enum Format
1408 {
1409 FMT_UNKNOWN,
1410 FMT_WAV,
1411 FMT_MP3,
1412 };
Benny Prijonoeebe9af2006-06-13 22:57:13 +00001413 unsigned slot, file_id;
Benny Prijonoa66c3312007-01-21 23:12:40 +00001414 char path[PJ_MAXPATH];
Benny Prijonob3cdb2b2006-10-19 15:49:47 +00001415 pj_str_t ext;
Benny Prijono8f310522006-10-20 11:08:49 +00001416 int file_format;
Benny Prijonod5696da2007-07-17 16:25:45 +00001417 pj_pool_t *pool;
Benny Prijonoeebe9af2006-06-13 22:57:13 +00001418 pjmedia_port *port;
1419 pj_status_t status;
1420
Benny Prijonob3cdb2b2006-10-19 15:49:47 +00001421 /* Filename must present */
1422 PJ_ASSERT_RETURN(filename != NULL, PJ_EINVAL);
1423
Benny Prijono00cae612006-07-31 15:19:36 +00001424 /* Don't support max_size at present */
Benny Prijonob3cdb2b2006-10-19 15:49:47 +00001425 PJ_ASSERT_RETURN(max_size == 0 || max_size == -1, PJ_EINVAL);
Benny Prijono00cae612006-07-31 15:19:36 +00001426
Benny Prijono8f310522006-10-20 11:08:49 +00001427 /* Don't support encoding type at present */
1428 PJ_ASSERT_RETURN(enc_type == 0, PJ_EINVAL);
Benny Prijono00cae612006-07-31 15:19:36 +00001429
Benny Prijonoeebe9af2006-06-13 22:57:13 +00001430 if (pjsua_var.rec_cnt >= PJ_ARRAY_SIZE(pjsua_var.recorder))
1431 return PJ_ETOOMANY;
1432
Benny Prijonob3cdb2b2006-10-19 15:49:47 +00001433 /* Determine the file format */
1434 ext.ptr = filename->ptr + filename->slen - 4;
1435 ext.slen = 4;
1436
1437 if (pj_stricmp2(&ext, ".wav") == 0)
1438 file_format = FMT_WAV;
1439 else if (pj_stricmp2(&ext, ".mp3") == 0)
1440 file_format = FMT_MP3;
1441 else {
1442 PJ_LOG(1,(THIS_FILE, "pjsua_recorder_create() error: unable to "
1443 "determine file format for %.*s",
1444 (int)filename->slen, filename->ptr));
1445 return PJ_ENOTSUP;
1446 }
1447
Benny Prijonoeebe9af2006-06-13 22:57:13 +00001448 PJSUA_LOCK();
1449
1450 for (file_id=0; file_id<PJ_ARRAY_SIZE(pjsua_var.recorder); ++file_id) {
1451 if (pjsua_var.recorder[file_id].port == NULL)
1452 break;
1453 }
1454
1455 if (file_id == PJ_ARRAY_SIZE(pjsua_var.recorder)) {
1456 /* This is unexpected */
1457 PJSUA_UNLOCK();
1458 pj_assert(0);
1459 return PJ_EBUG;
1460 }
1461
1462 pj_memcpy(path, filename->ptr, filename->slen);
1463 path[filename->slen] = '\0';
Benny Prijonob3cdb2b2006-10-19 15:49:47 +00001464
Benny Prijonod5696da2007-07-17 16:25:45 +00001465 pool = pjsua_pool_create(get_basename(path, filename->slen), 1000, 1000);
1466 if (!pool) {
1467 PJSUA_UNLOCK();
1468 return PJ_ENOMEM;
1469 }
1470
Benny Prijonob3cdb2b2006-10-19 15:49:47 +00001471 if (file_format == FMT_WAV) {
Benny Prijonod5696da2007-07-17 16:25:45 +00001472 status = pjmedia_wav_writer_port_create(pool, path,
Benny Prijonob3cdb2b2006-10-19 15:49:47 +00001473 pjsua_var.media_cfg.clock_rate,
1474 pjsua_var.mconf_cfg.channel_count,
1475 pjsua_var.mconf_cfg.samples_per_frame,
1476 pjsua_var.mconf_cfg.bits_per_sample,
1477 options, 0, &port);
Benny Prijonob3cdb2b2006-10-19 15:49:47 +00001478 } else {
Benny Prijonoc95a0f02007-04-09 07:06:08 +00001479 PJ_UNUSED_ARG(enc_param);
Benny Prijonob3cdb2b2006-10-19 15:49:47 +00001480 port = NULL;
1481 status = PJ_ENOTSUP;
1482 }
1483
Benny Prijonoeebe9af2006-06-13 22:57:13 +00001484 if (status != PJ_SUCCESS) {
1485 PJSUA_UNLOCK();
1486 pjsua_perror(THIS_FILE, "Unable to open file for recording", status);
Benny Prijonod5696da2007-07-17 16:25:45 +00001487 pj_pool_release(pool);
Benny Prijonoeebe9af2006-06-13 22:57:13 +00001488 return status;
1489 }
1490
Benny Prijonod5696da2007-07-17 16:25:45 +00001491 status = pjmedia_conf_add_port(pjsua_var.mconf, pool,
Benny Prijonoeebe9af2006-06-13 22:57:13 +00001492 port, filename, &slot);
1493 if (status != PJ_SUCCESS) {
1494 pjmedia_port_destroy(port);
1495 PJSUA_UNLOCK();
Benny Prijonod5696da2007-07-17 16:25:45 +00001496 pj_pool_release(pool);
Benny Prijonoeebe9af2006-06-13 22:57:13 +00001497 return status;
1498 }
1499
1500 pjsua_var.recorder[file_id].port = port;
1501 pjsua_var.recorder[file_id].slot = slot;
Benny Prijonod5696da2007-07-17 16:25:45 +00001502 pjsua_var.recorder[file_id].pool = pool;
Benny Prijonoeebe9af2006-06-13 22:57:13 +00001503
1504 if (p_id) *p_id = file_id;
1505
1506 ++pjsua_var.rec_cnt;
1507
1508 PJSUA_UNLOCK();
1509 return PJ_SUCCESS;
1510}
1511
1512
1513/*
1514 * Get conference port associated with recorder.
1515 */
1516PJ_DEF(pjsua_conf_port_id) pjsua_recorder_get_conf_port(pjsua_recorder_id id)
1517{
Benny Prijonoa1e69682007-05-11 15:14:34 +00001518 PJ_ASSERT_RETURN(id>=0 && id<(int)PJ_ARRAY_SIZE(pjsua_var.recorder),
1519 PJ_EINVAL);
Benny Prijonoeebe9af2006-06-13 22:57:13 +00001520 PJ_ASSERT_RETURN(pjsua_var.recorder[id].port != NULL, PJ_EINVAL);
1521
1522 return pjsua_var.recorder[id].slot;
1523}
1524
Benny Prijono469b1522006-12-26 03:05:17 +00001525/*
1526 * Get the media port for the recorder.
1527 */
1528PJ_DEF(pj_status_t) pjsua_recorder_get_port( pjsua_recorder_id id,
1529 pjmedia_port **p_port)
1530{
Benny Prijonoa1e69682007-05-11 15:14:34 +00001531 PJ_ASSERT_RETURN(id>=0 && id<(int)PJ_ARRAY_SIZE(pjsua_var.recorder),
1532 PJ_EINVAL);
Benny Prijono469b1522006-12-26 03:05:17 +00001533 PJ_ASSERT_RETURN(pjsua_var.recorder[id].port != NULL, PJ_EINVAL);
1534 PJ_ASSERT_RETURN(p_port != NULL, PJ_EINVAL);
1535
1536 *p_port = pjsua_var.recorder[id].port;
1537 return PJ_SUCCESS;
1538}
Benny Prijonoeebe9af2006-06-13 22:57:13 +00001539
1540/*
1541 * Destroy recorder (this will complete recording).
1542 */
1543PJ_DEF(pj_status_t) pjsua_recorder_destroy(pjsua_recorder_id id)
1544{
Benny Prijonoa1e69682007-05-11 15:14:34 +00001545 PJ_ASSERT_RETURN(id>=0 && id<(int)PJ_ARRAY_SIZE(pjsua_var.recorder),
1546 PJ_EINVAL);
Benny Prijonoeebe9af2006-06-13 22:57:13 +00001547 PJ_ASSERT_RETURN(pjsua_var.recorder[id].port != NULL, PJ_EINVAL);
1548
1549 PJSUA_LOCK();
1550
1551 if (pjsua_var.recorder[id].port) {
1552 pjmedia_conf_remove_port(pjsua_var.mconf,
1553 pjsua_var.recorder[id].slot);
1554 pjmedia_port_destroy(pjsua_var.recorder[id].port);
1555 pjsua_var.recorder[id].port = NULL;
1556 pjsua_var.recorder[id].slot = 0xFFFF;
Benny Prijonod5696da2007-07-17 16:25:45 +00001557 pj_pool_release(pjsua_var.recorder[id].pool);
1558 pjsua_var.recorder[id].pool = NULL;
Benny Prijonoeebe9af2006-06-13 22:57:13 +00001559 pjsua_var.rec_cnt--;
1560 }
1561
1562 PJSUA_UNLOCK();
1563
1564 return PJ_SUCCESS;
1565}
1566
1567
1568/*****************************************************************************
1569 * Sound devices.
1570 */
1571
1572/*
1573 * Enum sound devices.
1574 */
1575PJ_DEF(pj_status_t) pjsua_enum_snd_devs( pjmedia_snd_dev_info info[],
1576 unsigned *count)
1577{
1578 unsigned i, dev_count;
1579
1580 dev_count = pjmedia_snd_get_dev_count();
1581
1582 if (dev_count > *count) dev_count = *count;
1583
1584 for (i=0; i<dev_count; ++i) {
1585 const pjmedia_snd_dev_info *ci;
1586
1587 ci = pjmedia_snd_get_dev_info(i);
1588 pj_memcpy(&info[i], ci, sizeof(*ci));
1589 }
1590
1591 *count = dev_count;
1592
1593 return PJ_SUCCESS;
1594}
1595
1596
1597/* Close existing sound device */
1598static void close_snd_dev(void)
1599{
1600 /* Close sound device */
1601 if (pjsua_var.snd_port) {
1602 const pjmedia_snd_dev_info *cap_info, *play_info;
1603
1604 cap_info = pjmedia_snd_get_dev_info(pjsua_var.cap_dev);
1605 play_info = pjmedia_snd_get_dev_info(pjsua_var.play_dev);
1606
1607 PJ_LOG(4,(THIS_FILE, "Closing %s sound playback device and "
1608 "%s sound capture device",
1609 play_info->name, cap_info->name));
1610
1611 pjmedia_snd_port_disconnect(pjsua_var.snd_port);
1612 pjmedia_snd_port_destroy(pjsua_var.snd_port);
1613 pjsua_var.snd_port = NULL;
1614 }
1615
1616 /* Close null sound device */
1617 if (pjsua_var.null_snd) {
1618 PJ_LOG(4,(THIS_FILE, "Closing null sound device.."));
1619 pjmedia_master_port_destroy(pjsua_var.null_snd, PJ_FALSE);
1620 pjsua_var.null_snd = NULL;
1621 }
1622}
1623
1624/*
1625 * Select or change sound device. Application may call this function at
1626 * any time to replace current sound device.
1627 */
1628PJ_DEF(pj_status_t) pjsua_set_snd_dev( int capture_dev,
1629 int playback_dev)
1630{
1631 pjmedia_port *conf_port;
Benny Prijono6dd967c2006-12-26 02:27:14 +00001632 const pjmedia_snd_dev_info *play_info;
Benny Prijono26056d82006-10-11 16:03:41 +00001633 unsigned clock_rates[] = { 0, 22050, 44100, 48000, 11025, 32000, 8000};
Benny Prijono658a1c52006-10-11 21:56:16 +00001634 unsigned selected_clock_rate = 0;
Benny Prijono26056d82006-10-11 16:03:41 +00001635 unsigned i;
Benny Prijonoc53c6d72006-11-27 09:54:03 +00001636 pjmedia_snd_stream *strm;
1637 pjmedia_snd_stream_info si;
1638 pj_str_t tmp;
Benny Prijono26056d82006-10-11 16:03:41 +00001639 pj_status_t status = -1;
Benny Prijonoeebe9af2006-06-13 22:57:13 +00001640
1641 /* Close existing sound port */
1642 close_snd_dev();
1643
1644
Benny Prijono26056d82006-10-11 16:03:41 +00001645 /* Set default clock rate */
1646 clock_rates[0] = pjsua_var.media_cfg.clock_rate;
Benny Prijonoeebe9af2006-06-13 22:57:13 +00001647
Benny Prijono26056d82006-10-11 16:03:41 +00001648 /* Attempts to open the sound device with different clock rates */
1649 for (i=0; i<PJ_ARRAY_SIZE(clock_rates); ++i) {
1650 char errmsg[PJ_ERR_MSG_SIZE];
1651
1652 PJ_LOG(4,(THIS_FILE,
1653 "pjsua_set_snd_dev(): attempting to open devices "
1654 "@%d Hz", clock_rates[i]));
1655
1656 /* Create the sound device. Sound port will start immediately. */
1657 status = pjmedia_snd_port_create(pjsua_var.pool, capture_dev,
1658 playback_dev,
1659 clock_rates[i], 1,
1660 clock_rates[i]/FPS,
1661 16, 0, &pjsua_var.snd_port);
1662
Benny Prijono658a1c52006-10-11 21:56:16 +00001663 if (status == PJ_SUCCESS) {
1664 selected_clock_rate = clock_rates[i];
Benny Prijono26056d82006-10-11 16:03:41 +00001665 break;
Benny Prijono658a1c52006-10-11 21:56:16 +00001666 }
Benny Prijono26056d82006-10-11 16:03:41 +00001667
1668 pj_strerror(status, errmsg, sizeof(errmsg));
Benny Prijono658a1c52006-10-11 21:56:16 +00001669 PJ_LOG(4, (THIS_FILE, "..failed: %s", errmsg));
Benny Prijono26056d82006-10-11 16:03:41 +00001670 }
1671
Benny Prijonoeebe9af2006-06-13 22:57:13 +00001672 if (status != PJ_SUCCESS) {
1673 pjsua_perror(THIS_FILE, "Unable to open sound device", status);
1674 return status;
1675 }
1676
1677 /* Get the port0 of the conference bridge. */
1678 conf_port = pjmedia_conf_get_master_port(pjsua_var.mconf);
1679 pj_assert(conf_port != NULL);
1680
Benny Prijonof20687a2006-08-04 18:27:19 +00001681 /* Set AEC */
Benny Prijono5da50432006-08-07 10:24:52 +00001682 pjmedia_snd_port_set_ec( pjsua_var.snd_port, pjsua_var.pool,
1683 pjsua_var.media_cfg.ec_tail_len,
1684 pjsua_var.media_cfg.ec_options);
Benny Prijonoeebe9af2006-06-13 22:57:13 +00001685
Benny Prijono658a1c52006-10-11 21:56:16 +00001686 /* If there's mismatch between sound port and conference's port,
1687 * create a resample port to bridge them.
1688 */
1689 if (selected_clock_rate != pjsua_var.media_cfg.clock_rate) {
1690 pjmedia_port *resample_port;
1691
1692 status = pjmedia_resample_port_create(pjsua_var.pool, conf_port,
1693 selected_clock_rate, 0,
1694 &resample_port);
1695 if (status != PJ_SUCCESS) {
1696 pjsua_perror("Error creating resample port", THIS_FILE, status);
1697 return status;
1698 }
1699
1700 conf_port = resample_port;
1701 }
1702
Benny Prijono52a93912006-08-04 20:54:37 +00001703 /* Connect sound port to the bridge */
1704 status = pjmedia_snd_port_connect(pjsua_var.snd_port,
1705 conf_port );
1706 if (status != PJ_SUCCESS) {
1707 pjsua_perror(THIS_FILE, "Unable to connect conference port to "
1708 "sound device", status);
1709 pjmedia_snd_port_destroy(pjsua_var.snd_port);
1710 pjsua_var.snd_port = NULL;
1711 return status;
1712 }
1713
Benny Prijonoeebe9af2006-06-13 22:57:13 +00001714 /* Save the device IDs */
1715 pjsua_var.cap_dev = capture_dev;
1716 pjsua_var.play_dev = playback_dev;
1717
Benny Prijonoc53c6d72006-11-27 09:54:03 +00001718 /* Update sound device name. */
1719 strm = pjmedia_snd_port_get_snd_stream(pjsua_var.snd_port);
1720 pjmedia_snd_stream_get_info(strm, &si);
1721 play_info = pjmedia_snd_get_dev_info(si.rec_id);
1722
1723 pjmedia_conf_set_port0_name(pjsua_var.mconf,
1724 pj_cstr(&tmp, play_info->name));
1725
Benny Prijonoeebe9af2006-06-13 22:57:13 +00001726 return PJ_SUCCESS;
1727}
1728
1729
1730/*
Benny Prijonoebdf8772007-02-01 19:25:50 +00001731 * Get currently active sound devices. If sound devices has not been created
1732 * (for example when pjsua_start() is not called), it is possible that
1733 * the function returns PJ_SUCCESS with -1 as device IDs.
1734 */
1735PJ_DEF(pj_status_t) pjsua_get_snd_dev(int *capture_dev,
1736 int *playback_dev)
1737{
1738 if (capture_dev) {
1739 *capture_dev = pjsua_var.cap_dev;
1740 }
1741 if (playback_dev) {
1742 *playback_dev = pjsua_var.play_dev;
1743 }
1744
1745 return PJ_SUCCESS;
1746}
1747
1748
1749/*
Benny Prijonoeebe9af2006-06-13 22:57:13 +00001750 * Use null sound device.
1751 */
1752PJ_DEF(pj_status_t) pjsua_set_null_snd_dev(void)
1753{
1754 pjmedia_port *conf_port;
1755 pj_status_t status;
1756
1757 /* Close existing sound device */
1758 close_snd_dev();
1759
1760 PJ_LOG(4,(THIS_FILE, "Opening null sound device.."));
1761
1762 /* Get the port0 of the conference bridge. */
1763 conf_port = pjmedia_conf_get_master_port(pjsua_var.mconf);
1764 pj_assert(conf_port != NULL);
1765
1766 /* Create master port, connecting port0 of the conference bridge to
1767 * a null port.
1768 */
1769 status = pjmedia_master_port_create(pjsua_var.pool, pjsua_var.null_port,
1770 conf_port, 0, &pjsua_var.null_snd);
1771 if (status != PJ_SUCCESS) {
1772 pjsua_perror(THIS_FILE, "Unable to create null sound device",
1773 status);
1774 return status;
1775 }
1776
1777 /* Start the master port */
1778 status = pjmedia_master_port_start(pjsua_var.null_snd);
1779 PJ_ASSERT_RETURN(status == PJ_SUCCESS, status);
1780
1781 return PJ_SUCCESS;
1782}
1783
1784
Benny Prijonoe909eac2006-07-27 22:04:56 +00001785
1786/*
1787 * Use no device!
1788 */
1789PJ_DEF(pjmedia_port*) pjsua_set_no_snd_dev(void)
1790{
1791 /* Close existing sound device */
1792 close_snd_dev();
1793
1794 pjsua_var.no_snd = PJ_TRUE;
1795 return pjmedia_conf_get_master_port(pjsua_var.mconf);
1796}
1797
1798
Benny Prijonof20687a2006-08-04 18:27:19 +00001799/*
1800 * Configure the AEC settings of the sound port.
1801 */
Benny Prijono5da50432006-08-07 10:24:52 +00001802PJ_DEF(pj_status_t) pjsua_set_ec(unsigned tail_ms, unsigned options)
Benny Prijonof20687a2006-08-04 18:27:19 +00001803{
1804 pjsua_var.media_cfg.ec_tail_len = tail_ms;
1805
1806 if (pjsua_var.snd_port)
Benny Prijono5da50432006-08-07 10:24:52 +00001807 return pjmedia_snd_port_set_ec( pjsua_var.snd_port, pjsua_var.pool,
1808 tail_ms, options);
Benny Prijonof20687a2006-08-04 18:27:19 +00001809
1810 return PJ_SUCCESS;
1811}
1812
1813
1814/*
1815 * Get current AEC tail length.
1816 */
Benny Prijono22dfe592006-08-06 12:07:13 +00001817PJ_DEF(pj_status_t) pjsua_get_ec_tail(unsigned *p_tail_ms)
Benny Prijonof20687a2006-08-04 18:27:19 +00001818{
1819 *p_tail_ms = pjsua_var.media_cfg.ec_tail_len;
1820 return PJ_SUCCESS;
1821}
1822
Benny Prijonoe909eac2006-07-27 22:04:56 +00001823
Benny Prijonoeebe9af2006-06-13 22:57:13 +00001824/*****************************************************************************
1825 * Codecs.
1826 */
1827
1828/*
1829 * Enum all supported codecs in the system.
1830 */
1831PJ_DEF(pj_status_t) pjsua_enum_codecs( pjsua_codec_info id[],
1832 unsigned *p_count )
1833{
1834 pjmedia_codec_mgr *codec_mgr;
1835 pjmedia_codec_info info[32];
1836 unsigned i, count, prio[32];
1837 pj_status_t status;
1838
1839 codec_mgr = pjmedia_endpt_get_codec_mgr(pjsua_var.med_endpt);
1840 count = PJ_ARRAY_SIZE(info);
1841 status = pjmedia_codec_mgr_enum_codecs( codec_mgr, &count, info, prio);
1842 if (status != PJ_SUCCESS) {
1843 *p_count = 0;
1844 return status;
1845 }
1846
1847 if (count > *p_count) count = *p_count;
1848
1849 for (i=0; i<count; ++i) {
1850 pjmedia_codec_info_to_id(&info[i], id[i].buf_, sizeof(id[i].buf_));
1851 id[i].codec_id = pj_str(id[i].buf_);
1852 id[i].priority = (pj_uint8_t) prio[i];
1853 }
1854
1855 *p_count = count;
1856
1857 return PJ_SUCCESS;
1858}
1859
1860
1861/*
1862 * Change codec priority.
1863 */
1864PJ_DEF(pj_status_t) pjsua_codec_set_priority( const pj_str_t *codec_id,
1865 pj_uint8_t priority )
1866{
1867 pjmedia_codec_mgr *codec_mgr;
1868
1869 codec_mgr = pjmedia_endpt_get_codec_mgr(pjsua_var.med_endpt);
1870
1871 return pjmedia_codec_mgr_set_codec_priority(codec_mgr, codec_id,
1872 priority);
1873}
1874
1875
1876/*
1877 * Get codec parameters.
1878 */
1879PJ_DEF(pj_status_t) pjsua_codec_get_param( const pj_str_t *codec_id,
1880 pjmedia_codec_param *param )
1881{
1882 const pjmedia_codec_info *info;
1883 pjmedia_codec_mgr *codec_mgr;
1884 unsigned count = 1;
1885 pj_status_t status;
1886
1887 codec_mgr = pjmedia_endpt_get_codec_mgr(pjsua_var.med_endpt);
1888
1889 status = pjmedia_codec_mgr_find_codecs_by_id(codec_mgr, codec_id,
1890 &count, &info, NULL);
1891 if (status != PJ_SUCCESS)
1892 return status;
1893
1894 if (count != 1)
1895 return PJ_ENOTFOUND;
1896
1897 status = pjmedia_codec_mgr_get_default_param( codec_mgr, info, param);
1898 return status;
1899}
1900
1901
1902/*
1903 * Set codec parameters.
1904 */
1905PJ_DEF(pj_status_t) pjsua_codec_set_param( const pj_str_t *id,
1906 const pjmedia_codec_param *param)
1907{
Benny Prijono00cae612006-07-31 15:19:36 +00001908 PJ_UNUSED_ARG(id);
1909 PJ_UNUSED_ARG(param);
Benny Prijonoeebe9af2006-06-13 22:57:13 +00001910 PJ_TODO(set_codec_param);
1911 return PJ_SUCCESS;
1912}