blob: f6424353735eb57a0128d5aa775cfcf3314472c7 [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 Prijonoeebe9af2006-06-13 22:57:13 +000025#define DEFAULT_RTP_PORT 4000
26
Benny Prijonode479562007-03-15 10:23:55 +000027/* Next RTP port to be used */
28static pj_uint16_t next_rtp_port;
Benny Prijonoeebe9af2006-06-13 22:57:13 +000029
30/* Close existing sound device */
31static void close_snd_dev(void);
32
33
34/**
35 * Init media subsystems.
36 */
37pj_status_t pjsua_media_subsys_init(const pjsua_media_config *cfg)
38{
Benny Prijonoba5926a2007-05-02 11:29:37 +000039 pj_str_t codec_id = {NULL, 0};
Benny Prijono0498d902006-06-19 14:49:14 +000040 unsigned opt;
Benny Prijonoeebe9af2006-06-13 22:57:13 +000041 pj_status_t status;
42
Benny Prijonofc24e692007-01-27 18:31:51 +000043 /* To suppress warning about unused var when all codecs are disabled */
44 PJ_UNUSED_ARG(codec_id);
45
Benny Prijonoeebe9af2006-06-13 22:57:13 +000046 /* Copy configuration */
47 pj_memcpy(&pjsua_var.media_cfg, cfg, sizeof(*cfg));
48
49 /* Normalize configuration */
Benny Prijonoeebe9af2006-06-13 22:57:13 +000050
51 if (pjsua_var.media_cfg.has_ioqueue &&
52 pjsua_var.media_cfg.thread_cnt == 0)
53 {
54 pjsua_var.media_cfg.thread_cnt = 1;
55 }
56
57 if (pjsua_var.media_cfg.max_media_ports < pjsua_var.ua_cfg.max_calls) {
58 pjsua_var.media_cfg.max_media_ports = pjsua_var.ua_cfg.max_calls + 2;
59 }
60
61 /* Create media endpoint. */
62 status = pjmedia_endpt_create(&pjsua_var.cp.factory,
63 pjsua_var.media_cfg.has_ioqueue? NULL :
64 pjsip_endpt_get_ioqueue(pjsua_var.endpt),
65 pjsua_var.media_cfg.thread_cnt,
66 &pjsua_var.med_endpt);
67 if (status != PJ_SUCCESS) {
68 pjsua_perror(THIS_FILE,
69 "Media stack initialization has returned error",
70 status);
71 return status;
72 }
73
74 /* Register all codecs */
75#if PJMEDIA_HAS_SPEEX_CODEC
76 /* Register speex. */
77 status = pjmedia_codec_speex_init(pjsua_var.med_endpt,
Benny Prijono7ca96da2006-08-07 12:11:40 +000078 0,
Benny Prijono0498d902006-06-19 14:49:14 +000079 pjsua_var.media_cfg.quality,
80 pjsua_var.media_cfg.quality);
Benny Prijonoeebe9af2006-06-13 22:57:13 +000081 if (status != PJ_SUCCESS) {
82 pjsua_perror(THIS_FILE, "Error initializing Speex codec",
83 status);
84 return status;
85 }
Benny Prijono7ca96da2006-08-07 12:11:40 +000086
87 /* Set speex/16000 to higher priority*/
88 codec_id = pj_str("speex/16000");
89 pjmedia_codec_mgr_set_codec_priority(
90 pjmedia_endpt_get_codec_mgr(pjsua_var.med_endpt),
91 &codec_id, PJMEDIA_CODEC_PRIO_NORMAL+2);
92
93 /* Set speex/8000 to next higher priority*/
94 codec_id = pj_str("speex/8000");
95 pjmedia_codec_mgr_set_codec_priority(
96 pjmedia_endpt_get_codec_mgr(pjsua_var.med_endpt),
97 &codec_id, PJMEDIA_CODEC_PRIO_NORMAL+1);
98
99
100
Benny Prijonoeebe9af2006-06-13 22:57:13 +0000101#endif /* PJMEDIA_HAS_SPEEX_CODEC */
102
Benny Prijono00cae612006-07-31 15:19:36 +0000103#if PJMEDIA_HAS_ILBC_CODEC
104 /* Register iLBC. */
105 status = pjmedia_codec_ilbc_init( pjsua_var.med_endpt,
106 pjsua_var.media_cfg.ilbc_mode);
107 if (status != PJ_SUCCESS) {
108 pjsua_perror(THIS_FILE, "Error initializing iLBC codec",
109 status);
110 return status;
111 }
112#endif /* PJMEDIA_HAS_ILBC_CODEC */
113
Benny Prijonoeebe9af2006-06-13 22:57:13 +0000114#if PJMEDIA_HAS_GSM_CODEC
115 /* Register GSM */
116 status = pjmedia_codec_gsm_init(pjsua_var.med_endpt);
117 if (status != PJ_SUCCESS) {
118 pjsua_perror(THIS_FILE, "Error initializing GSM codec",
119 status);
120 return status;
121 }
122#endif /* PJMEDIA_HAS_GSM_CODEC */
123
124#if PJMEDIA_HAS_G711_CODEC
125 /* Register PCMA and PCMU */
126 status = pjmedia_codec_g711_init(pjsua_var.med_endpt);
127 if (status != PJ_SUCCESS) {
128 pjsua_perror(THIS_FILE, "Error initializing G711 codec",
129 status);
130 return status;
131 }
132#endif /* PJMEDIA_HAS_G711_CODEC */
133
134#if PJMEDIA_HAS_L16_CODEC
135 /* Register L16 family codecs, but disable all */
136 status = pjmedia_codec_l16_init(pjsua_var.med_endpt, 0);
137 if (status != PJ_SUCCESS) {
138 pjsua_perror(THIS_FILE, "Error initializing L16 codecs",
139 status);
140 return status;
141 }
142
143 /* Disable ALL L16 codecs */
144 codec_id = pj_str("L16");
145 pjmedia_codec_mgr_set_codec_priority(
146 pjmedia_endpt_get_codec_mgr(pjsua_var.med_endpt),
147 &codec_id, PJMEDIA_CODEC_PRIO_DISABLED);
148
149#endif /* PJMEDIA_HAS_L16_CODEC */
150
151
152 /* Save additional conference bridge parameters for future
153 * reference.
154 */
155 pjsua_var.mconf_cfg.samples_per_frame = pjsua_var.media_cfg.clock_rate *
Benny Prijonocf0b4b22007-10-06 17:31:09 +0000156 pjsua_var.media_cfg.audio_frame_ptime /
157 1000;
Benny Prijonoeebe9af2006-06-13 22:57:13 +0000158 pjsua_var.mconf_cfg.channel_count = 1;
159 pjsua_var.mconf_cfg.bits_per_sample = 16;
160
Benny Prijono0498d902006-06-19 14:49:14 +0000161 /* Init options for conference bridge. */
162 opt = PJMEDIA_CONF_NO_DEVICE;
163 if (pjsua_var.media_cfg.quality >= 3 &&
Benny Prijono7ca96da2006-08-07 12:11:40 +0000164 pjsua_var.media_cfg.quality <= 4)
Benny Prijono0498d902006-06-19 14:49:14 +0000165 {
166 opt |= PJMEDIA_CONF_SMALL_FILTER;
167 }
168 else if (pjsua_var.media_cfg.quality < 3) {
169 opt |= PJMEDIA_CONF_USE_LINEAR;
170 }
171
172
Benny Prijonoeebe9af2006-06-13 22:57:13 +0000173 /* Init conference bridge. */
174 status = pjmedia_conf_create(pjsua_var.pool,
175 pjsua_var.media_cfg.max_media_ports,
176 pjsua_var.media_cfg.clock_rate,
177 pjsua_var.mconf_cfg.channel_count,
178 pjsua_var.mconf_cfg.samples_per_frame,
179 pjsua_var.mconf_cfg.bits_per_sample,
Benny Prijono0498d902006-06-19 14:49:14 +0000180 opt, &pjsua_var.mconf);
Benny Prijonoeebe9af2006-06-13 22:57:13 +0000181 if (status != PJ_SUCCESS) {
182 pjsua_perror(THIS_FILE,
183 "Media stack initialization has returned error",
184 status);
185 return status;
186 }
187
188 /* Create null port just in case user wants to use null sound. */
189 status = pjmedia_null_port_create(pjsua_var.pool,
190 pjsua_var.media_cfg.clock_rate,
191 pjsua_var.mconf_cfg.channel_count,
192 pjsua_var.mconf_cfg.samples_per_frame,
193 pjsua_var.mconf_cfg.bits_per_sample,
194 &pjsua_var.null_port);
195 PJ_ASSERT_RETURN(status == PJ_SUCCESS, status);
196
Benny Prijono6ba8c542007-10-16 01:34:14 +0000197 /* Perform NAT detection */
198 pjsua_detect_nat_type();
199
Benny Prijonoeebe9af2006-06-13 22:57:13 +0000200 return PJ_SUCCESS;
201}
202
203
204/*
205 * Create RTP and RTCP socket pair, and possibly resolve their public
206 * address via STUN.
207 */
208static pj_status_t create_rtp_rtcp_sock(const pjsua_transport_config *cfg,
209 pjmedia_sock_info *skinfo)
210{
211 enum {
212 RTP_RETRY = 100
213 };
214 int i;
Benny Prijono0a5cad82006-09-26 13:21:02 +0000215 pj_sockaddr_in bound_addr;
Benny Prijonoeebe9af2006-06-13 22:57:13 +0000216 pj_sockaddr_in mapped_addr[2];
217 pj_status_t status = PJ_SUCCESS;
218 pj_sock_t sock[2];
219
Benny Prijonoc97608e2007-03-23 16:34:20 +0000220 /* Make sure STUN server resolution has completed */
221 status = pjsua_resolve_stun_server(PJ_TRUE);
222 if (status != PJ_SUCCESS) {
223 pjsua_perror(THIS_FILE, "Error resolving STUN server", status);
224 return status;
225 }
226
227
Benny Prijonode479562007-03-15 10:23:55 +0000228 if (next_rtp_port == 0)
229 next_rtp_port = (pj_uint16_t)cfg->port;
Benny Prijonoeebe9af2006-06-13 22:57:13 +0000230
231 for (i=0; i<2; ++i)
232 sock[i] = PJ_INVALID_SOCKET;
233
Benny Prijono0a5cad82006-09-26 13:21:02 +0000234 bound_addr.sin_addr.s_addr = PJ_INADDR_ANY;
235 if (cfg->bound_addr.slen) {
236 status = pj_sockaddr_in_set_str_addr(&bound_addr, &cfg->bound_addr);
237 if (status != PJ_SUCCESS) {
238 pjsua_perror(THIS_FILE, "Unable to resolve transport bind address",
239 status);
240 return status;
241 }
242 }
Benny Prijonoeebe9af2006-06-13 22:57:13 +0000243
244 /* Loop retry to bind RTP and RTCP sockets. */
Benny Prijonode479562007-03-15 10:23:55 +0000245 for (i=0; i<RTP_RETRY; ++i, next_rtp_port += 2) {
Benny Prijonoeebe9af2006-06-13 22:57:13 +0000246
247 /* Create and bind RTP socket. */
Benny Prijono8ab968f2007-07-20 08:08:30 +0000248 status = pj_sock_socket(pj_AF_INET(), pj_SOCK_DGRAM(), 0, &sock[0]);
Benny Prijonoeebe9af2006-06-13 22:57:13 +0000249 if (status != PJ_SUCCESS) {
250 pjsua_perror(THIS_FILE, "socket() error", status);
251 return status;
252 }
253
Benny Prijono0a5cad82006-09-26 13:21:02 +0000254 status = pj_sock_bind_in(sock[0], bound_addr.sin_addr.s_addr,
Benny Prijonode479562007-03-15 10:23:55 +0000255 next_rtp_port);
Benny Prijonoeebe9af2006-06-13 22:57:13 +0000256 if (status != PJ_SUCCESS) {
257 pj_sock_close(sock[0]);
258 sock[0] = PJ_INVALID_SOCKET;
259 continue;
260 }
261
262 /* Create and bind RTCP socket. */
Benny Prijono8ab968f2007-07-20 08:08:30 +0000263 status = pj_sock_socket(pj_AF_INET(), pj_SOCK_DGRAM(), 0, &sock[1]);
Benny Prijonoeebe9af2006-06-13 22:57:13 +0000264 if (status != PJ_SUCCESS) {
265 pjsua_perror(THIS_FILE, "socket() error", status);
266 pj_sock_close(sock[0]);
267 return status;
268 }
269
Benny Prijono0a5cad82006-09-26 13:21:02 +0000270 status = pj_sock_bind_in(sock[1], bound_addr.sin_addr.s_addr,
Benny Prijonode479562007-03-15 10:23:55 +0000271 (pj_uint16_t)(next_rtp_port+1));
Benny Prijonoeebe9af2006-06-13 22:57:13 +0000272 if (status != PJ_SUCCESS) {
273 pj_sock_close(sock[0]);
274 sock[0] = PJ_INVALID_SOCKET;
275
276 pj_sock_close(sock[1]);
277 sock[1] = PJ_INVALID_SOCKET;
278 continue;
279 }
280
281 /*
282 * If we're configured to use STUN, then find out the mapped address,
283 * and make sure that the mapped RTCP port is adjacent with the RTP.
284 */
Benny Prijonoc97608e2007-03-23 16:34:20 +0000285 if (pjsua_var.stun_srv.addr.sa_family != 0) {
286 char ip_addr[32];
287 pj_str_t stun_srv;
288
289 pj_ansi_strcpy(ip_addr,
290 pj_inet_ntoa(pjsua_var.stun_srv.ipv4.sin_addr));
291 stun_srv = pj_str(ip_addr);
292
Benny Prijono14c2b862007-02-21 00:40:05 +0000293 status=pjstun_get_mapped_addr(&pjsua_var.cp.factory, 2, sock,
Benny Prijonoaf09dc32007-04-22 12:48:30 +0000294 &stun_srv, pj_ntohs(pjsua_var.stun_srv.ipv4.sin_port),
295 &stun_srv, pj_ntohs(pjsua_var.stun_srv.ipv4.sin_port),
Benny Prijonoeebe9af2006-06-13 22:57:13 +0000296 mapped_addr);
297 if (status != PJ_SUCCESS) {
298 pjsua_perror(THIS_FILE, "STUN resolve error", status);
299 goto on_error;
300 }
301
302 if (pj_ntohs(mapped_addr[1].sin_port) ==
303 pj_ntohs(mapped_addr[0].sin_port)+1)
304 {
305 /* Success! */
306 break;
307 }
308
309 pj_sock_close(sock[0]);
310 sock[0] = PJ_INVALID_SOCKET;
311
312 pj_sock_close(sock[1]);
313 sock[1] = PJ_INVALID_SOCKET;
314
Benny Prijono0a5cad82006-09-26 13:21:02 +0000315 } else if (cfg->public_addr.slen) {
316
317 status = pj_sockaddr_in_init(&mapped_addr[0], &cfg->public_addr,
Benny Prijonode479562007-03-15 10:23:55 +0000318 (pj_uint16_t)next_rtp_port);
Benny Prijono0a5cad82006-09-26 13:21:02 +0000319 if (status != PJ_SUCCESS)
320 goto on_error;
321
322 status = pj_sockaddr_in_init(&mapped_addr[1], &cfg->public_addr,
Benny Prijonode479562007-03-15 10:23:55 +0000323 (pj_uint16_t)(next_rtp_port+1));
Benny Prijono0a5cad82006-09-26 13:21:02 +0000324 if (status != PJ_SUCCESS)
325 goto on_error;
326
327 break;
328
Benny Prijonoeebe9af2006-06-13 22:57:13 +0000329 } else {
Benny Prijono594e4c52006-09-14 18:51:01 +0000330 pj_in_addr addr;
Benny Prijonoeebe9af2006-06-13 22:57:13 +0000331
332 /* Get local IP address. */
Benny Prijono594e4c52006-09-14 18:51:01 +0000333 status = pj_gethostip(&addr);
334 if (status != PJ_SUCCESS)
Benny Prijonoeebe9af2006-06-13 22:57:13 +0000335 goto on_error;
Benny Prijonoeebe9af2006-06-13 22:57:13 +0000336
337 for (i=0; i<2; ++i)
Benny Prijono594e4c52006-09-14 18:51:01 +0000338 mapped_addr[i].sin_addr = addr;
Benny Prijonoeebe9af2006-06-13 22:57:13 +0000339
Benny Prijonode479562007-03-15 10:23:55 +0000340 mapped_addr[0].sin_port=pj_htons((pj_uint16_t)next_rtp_port);
341 mapped_addr[1].sin_port=pj_htons((pj_uint16_t)(next_rtp_port+1));
Benny Prijonoeebe9af2006-06-13 22:57:13 +0000342 break;
343 }
344 }
345
346 if (sock[0] == PJ_INVALID_SOCKET) {
347 PJ_LOG(1,(THIS_FILE,
348 "Unable to find appropriate RTP/RTCP ports combination"));
349 goto on_error;
350 }
351
352
353 skinfo->rtp_sock = sock[0];
354 pj_memcpy(&skinfo->rtp_addr_name,
355 &mapped_addr[0], sizeof(pj_sockaddr_in));
356
357 skinfo->rtcp_sock = sock[1];
358 pj_memcpy(&skinfo->rtcp_addr_name,
359 &mapped_addr[1], sizeof(pj_sockaddr_in));
360
361 PJ_LOG(4,(THIS_FILE, "RTP socket reachable at %s:%d",
362 pj_inet_ntoa(skinfo->rtp_addr_name.sin_addr),
363 pj_ntohs(skinfo->rtp_addr_name.sin_port)));
364 PJ_LOG(4,(THIS_FILE, "RTCP socket reachable at %s:%d",
365 pj_inet_ntoa(skinfo->rtcp_addr_name.sin_addr),
366 pj_ntohs(skinfo->rtcp_addr_name.sin_port)));
367
Benny Prijonode479562007-03-15 10:23:55 +0000368 next_rtp_port += 2;
Benny Prijonoeebe9af2006-06-13 22:57:13 +0000369 return PJ_SUCCESS;
370
371on_error:
372 for (i=0; i<2; ++i) {
373 if (sock[i] != PJ_INVALID_SOCKET)
374 pj_sock_close(sock[i]);
375 }
376 return status;
377}
378
379
380/*
381 * Start pjsua media subsystem.
382 */
383pj_status_t pjsua_media_subsys_start(void)
384{
385 pj_status_t status;
386
387 /* Create media for calls, if none is specified */
388 if (pjsua_var.calls[0].med_tp == NULL) {
389 pjsua_transport_config transport_cfg;
390
391 /* Create default transport config */
392 pjsua_transport_config_default(&transport_cfg);
393 transport_cfg.port = DEFAULT_RTP_PORT;
394
395 status = pjsua_media_transports_create(&transport_cfg);
396 if (status != PJ_SUCCESS)
397 return status;
398 }
399
400 /* Create sound port if none is created yet */
Benny Prijonoe909eac2006-07-27 22:04:56 +0000401 if (pjsua_var.snd_port==NULL && pjsua_var.null_snd==NULL &&
402 !pjsua_var.no_snd)
403 {
Benny Prijonoeebe9af2006-06-13 22:57:13 +0000404 status = pjsua_set_snd_dev(pjsua_var.cap_dev, pjsua_var.play_dev);
405 if (status != PJ_SUCCESS) {
Benny Prijonod639efa2007-04-05 22:45:06 +0000406 pjsua_perror(THIS_FILE, "Error opening sound device", status);
407 return status;
Benny Prijonoeebe9af2006-06-13 22:57:13 +0000408 }
409 }
410
411 return PJ_SUCCESS;
412}
413
414
415/*
416 * Destroy pjsua media subsystem.
417 */
418pj_status_t pjsua_media_subsys_destroy(void)
419{
420 unsigned i;
421
422 close_snd_dev();
423
424 if (pjsua_var.mconf) {
425 pjmedia_conf_destroy(pjsua_var.mconf);
426 pjsua_var.mconf = NULL;
427 }
428
429 if (pjsua_var.null_port) {
430 pjmedia_port_destroy(pjsua_var.null_port);
431 pjsua_var.null_port = NULL;
432 }
433
434 /* Destroy file players */
435 for (i=0; i<PJ_ARRAY_SIZE(pjsua_var.player); ++i) {
436 if (pjsua_var.player[i].port) {
437 pjmedia_port_destroy(pjsua_var.player[i].port);
438 pjsua_var.player[i].port = NULL;
439 }
440 }
441
442 /* Destroy file recorders */
443 for (i=0; i<PJ_ARRAY_SIZE(pjsua_var.recorder); ++i) {
444 if (pjsua_var.recorder[i].port) {
445 pjmedia_port_destroy(pjsua_var.recorder[i].port);
446 pjsua_var.recorder[i].port = NULL;
447 }
448 }
449
450 /* Close media transports */
Benny Prijono0875ae82006-12-26 00:11:48 +0000451 for (i=0; i<pjsua_var.ua_cfg.max_calls; ++i) {
Benny Prijonoeebe9af2006-06-13 22:57:13 +0000452 if (pjsua_var.calls[i].med_tp) {
453 (*pjsua_var.calls[i].med_tp->op->destroy)(pjsua_var.calls[i].med_tp);
454 pjsua_var.calls[i].med_tp = NULL;
455 }
456 }
457
458 /* Destroy media endpoint. */
459 if (pjsua_var.med_endpt) {
460
461 /* Shutdown all codecs: */
462# if PJMEDIA_HAS_SPEEX_CODEC
463 pjmedia_codec_speex_deinit();
464# endif /* PJMEDIA_HAS_SPEEX_CODEC */
465
466# if PJMEDIA_HAS_GSM_CODEC
467 pjmedia_codec_gsm_deinit();
468# endif /* PJMEDIA_HAS_GSM_CODEC */
469
470# if PJMEDIA_HAS_G711_CODEC
471 pjmedia_codec_g711_deinit();
472# endif /* PJMEDIA_HAS_G711_CODEC */
473
474# if PJMEDIA_HAS_L16_CODEC
475 pjmedia_codec_l16_deinit();
476# endif /* PJMEDIA_HAS_L16_CODEC */
477
478
479 pjmedia_endpt_destroy(pjsua_var.med_endpt);
480 pjsua_var.med_endpt = NULL;
Benny Prijonoeebe9af2006-06-13 22:57:13 +0000481
Benny Prijonoad2e0ca2007-04-29 12:31:51 +0000482 /* Deinitialize sound subsystem */
Benny Prijonoa41d66e2007-10-01 12:17:23 +0000483 // Not necessary, as pjmedia_snd_deinit() should have been called
484 // in pjmedia_endpt_destroy().
485 //pjmedia_snd_deinit();
Benny Prijonoad2e0ca2007-04-29 12:31:51 +0000486 }
Benny Prijonoaf1bb1e2006-11-21 12:39:31 +0000487
Benny Prijonode479562007-03-15 10:23:55 +0000488 /* Reset RTP port */
489 next_rtp_port = 0;
490
Benny Prijonoeebe9af2006-06-13 22:57:13 +0000491 return PJ_SUCCESS;
492}
493
494
Benny Prijonoc97608e2007-03-23 16:34:20 +0000495/* Create normal UDP media transports */
496static pj_status_t create_udp_media_transports(pjsua_transport_config *cfg)
Benny Prijonoeebe9af2006-06-13 22:57:13 +0000497{
Benny Prijonoeebe9af2006-06-13 22:57:13 +0000498 unsigned i;
Benny Prijono617c5bc2007-04-02 19:51:21 +0000499 pjmedia_sock_info skinfo;
Benny Prijonoeebe9af2006-06-13 22:57:13 +0000500 pj_status_t status;
501
Benny Prijonoeebe9af2006-06-13 22:57:13 +0000502 /* Create each media transport */
503 for (i=0; i<pjsua_var.ua_cfg.max_calls; ++i) {
504
Benny Prijono617c5bc2007-04-02 19:51:21 +0000505 status = create_rtp_rtcp_sock(cfg, &skinfo);
Benny Prijonoeebe9af2006-06-13 22:57:13 +0000506 if (status != PJ_SUCCESS) {
507 pjsua_perror(THIS_FILE, "Unable to create RTP/RTCP socket",
508 status);
509 goto on_error;
510 }
511 status = pjmedia_transport_udp_attach(pjsua_var.med_endpt, NULL,
Benny Prijono617c5bc2007-04-02 19:51:21 +0000512 &skinfo, 0,
Benny Prijonoeebe9af2006-06-13 22:57:13 +0000513 &pjsua_var.calls[i].med_tp);
514 if (status != PJ_SUCCESS) {
515 pjsua_perror(THIS_FILE, "Unable to create media transport",
516 status);
517 goto on_error;
518 }
Benny Prijono00cae612006-07-31 15:19:36 +0000519
520 pjmedia_transport_udp_simulate_lost(pjsua_var.calls[i].med_tp,
521 PJMEDIA_DIR_ENCODING,
522 pjsua_var.media_cfg.tx_drop_pct);
523
524 pjmedia_transport_udp_simulate_lost(pjsua_var.calls[i].med_tp,
525 PJMEDIA_DIR_DECODING,
526 pjsua_var.media_cfg.rx_drop_pct);
527
Benny Prijonoeebe9af2006-06-13 22:57:13 +0000528 }
529
Benny Prijonoc97608e2007-03-23 16:34:20 +0000530 return PJ_SUCCESS;
531
532on_error:
533 for (i=0; i<pjsua_var.ua_cfg.max_calls; ++i) {
534 if (pjsua_var.calls[i].med_tp != NULL) {
535 pjmedia_transport_close(pjsua_var.calls[i].med_tp);
536 pjsua_var.calls[i].med_tp = NULL;
537 }
538 }
539
540 return status;
541}
542
543
Benny Prijono096c56c2007-09-15 08:30:16 +0000544/* This callback is called when ICE negotiation completes */
545static void on_ice_complete(pjmedia_transport *tp, pj_status_t result)
546{
547 unsigned id, c;
548 pj_bool_t found = PJ_FALSE;
549
550 /* We're only interested with failure case */
551 if (result == PJ_SUCCESS)
552 return;
553
554 /* Find call which has this media transport */
555
556 PJSUA_LOCK();
557
558 for (id=0, c=0; id<PJSUA_MAX_CALLS && c<pjsua_var.call_cnt; ++id) {
559 pjsua_call *call = &pjsua_var.calls[id];
560 if (call->inv) {
561 ++c;
562
563 if (call->med_tp == tp) {
564 call->media_st = PJSUA_CALL_MEDIA_ERROR;
565 call->media_dir = PJMEDIA_DIR_NONE;
566 found = PJ_TRUE;
567 break;
568 }
569 }
570 }
571
572 PJSUA_UNLOCK();
573
574 if (found && pjsua_var.ua_cfg.cb.on_call_media_state) {
575 pjsua_var.ua_cfg.cb.on_call_media_state(id);
576 }
577}
578
579
Benny Prijonoc97608e2007-03-23 16:34:20 +0000580/* Create ICE media transports (when ice is enabled) */
581static pj_status_t create_ice_media_transports(pjsua_transport_config *cfg)
582{
583 unsigned i;
Benny Prijonob681a2f2007-03-25 18:44:51 +0000584 pj_sockaddr_in addr;
Benny Prijonoc97608e2007-03-23 16:34:20 +0000585 pj_status_t status;
586
Benny Prijonoda9785b2007-04-02 20:43:06 +0000587 /* Make sure STUN server resolution has completed */
588 status = pjsua_resolve_stun_server(PJ_TRUE);
589 if (status != PJ_SUCCESS) {
590 pjsua_perror(THIS_FILE, "Error resolving STUN server", status);
591 return status;
592 }
593
Benny Prijonob681a2f2007-03-25 18:44:51 +0000594 pj_sockaddr_in_init(&addr, 0, (pj_uint16_t)cfg->port);
595
Benny Prijonoc97608e2007-03-23 16:34:20 +0000596 /* Create each media transport */
597 for (i=0; i<pjsua_var.ua_cfg.max_calls; ++i) {
Benny Prijonoa6bd7582007-03-28 15:49:48 +0000598 pj_ice_strans_comp comp;
Benny Prijono096c56c2007-09-15 08:30:16 +0000599 pjmedia_ice_cb ice_cb;
Benny Prijonob681a2f2007-03-25 18:44:51 +0000600 int next_port;
Benny Prijonodc1fe222007-06-26 10:13:13 +0000601#if PJMEDIA_ADVERTISE_RTCP
602 enum { COMP_CNT=2 };
603#else
604 enum { COMP_CNT=1 };
605#endif
Benny Prijonoc97608e2007-03-23 16:34:20 +0000606
Benny Prijono096c56c2007-09-15 08:30:16 +0000607 pj_bzero(&ice_cb, sizeof(pjmedia_ice_cb));
608 ice_cb.on_ice_complete = &on_ice_complete;
609
Benny Prijonodc1fe222007-06-26 10:13:13 +0000610 status = pjmedia_ice_create(pjsua_var.med_endpt, NULL, COMP_CNT,
Benny Prijono096c56c2007-09-15 08:30:16 +0000611 &pjsua_var.stun_cfg, &ice_cb,
Benny Prijonoc97608e2007-03-23 16:34:20 +0000612 &pjsua_var.calls[i].med_tp);
613 if (status != PJ_SUCCESS) {
614 pjsua_perror(THIS_FILE, "Unable to create ICE media transport",
615 status);
616 goto on_error;
617 }
618
Benny Prijono11da9bc2007-09-15 08:55:00 +0000619 pjmedia_ice_simulate_lost(pjsua_var.calls[i].med_tp,
620 PJMEDIA_DIR_ENCODING,
621 pjsua_var.media_cfg.tx_drop_pct);
622
623 pjmedia_ice_simulate_lost(pjsua_var.calls[i].med_tp,
624 PJMEDIA_DIR_DECODING,
625 pjsua_var.media_cfg.rx_drop_pct);
626
Benny Prijonob681a2f2007-03-25 18:44:51 +0000627 status = pjmedia_ice_start_init(pjsua_var.calls[i].med_tp, 0, &addr,
628 &pjsua_var.stun_srv.ipv4, NULL);
Benny Prijonoc97608e2007-03-23 16:34:20 +0000629 if (status != PJ_SUCCESS) {
Benny Prijonob681a2f2007-03-25 18:44:51 +0000630 pjsua_perror(THIS_FILE, "Error starting ICE transport",
Benny Prijonoc97608e2007-03-23 16:34:20 +0000631 status);
632 goto on_error;
633 }
634
Benny Prijonob681a2f2007-03-25 18:44:51 +0000635 pjmedia_ice_get_comp(pjsua_var.calls[i].med_tp, 1, &comp);
636 next_port = pj_ntohs(comp.local_addr.ipv4.sin_port);
637 next_port += 2;
638 addr.sin_port = pj_htons((pj_uint16_t)next_port);
Benny Prijonoc97608e2007-03-23 16:34:20 +0000639 }
640
641 /* Wait until all ICE transports are ready */
642 for (i=0; i<pjsua_var.ua_cfg.max_calls; ++i) {
Benny Prijonoc97608e2007-03-23 16:34:20 +0000643
644 /* Wait until interface status is PJ_SUCCESS */
645 for (;;) {
Benny Prijonob681a2f2007-03-25 18:44:51 +0000646 status = pjmedia_ice_get_init_status(pjsua_var.calls[i].med_tp);
Benny Prijonoc97608e2007-03-23 16:34:20 +0000647 if (status == PJ_EPENDING)
648 pjsua_handle_events(100);
649 else
650 break;
651 }
652
653 if (status != PJ_SUCCESS) {
654 pjsua_perror(THIS_FILE,
655 "Error adding STUN address to ICE media transport",
656 status);
657 goto on_error;
658 }
659
Benny Prijonoc97608e2007-03-23 16:34:20 +0000660 }
Benny Prijonoeebe9af2006-06-13 22:57:13 +0000661
662 return PJ_SUCCESS;
663
664on_error:
665 for (i=0; i<pjsua_var.ua_cfg.max_calls; ++i) {
666 if (pjsua_var.calls[i].med_tp != NULL) {
Benny Prijonoc97608e2007-03-23 16:34:20 +0000667 pjmedia_transport_close(pjsua_var.calls[i].med_tp);
Benny Prijonoeebe9af2006-06-13 22:57:13 +0000668 pjsua_var.calls[i].med_tp = NULL;
669 }
670 }
671
Benny Prijonoc97608e2007-03-23 16:34:20 +0000672 return status;
673}
674
675
676/*
677 * Create UDP media transports for all the calls. This function creates
678 * one UDP media transport for each call.
679 */
Benny Prijono1f61a8f2007-08-16 10:11:44 +0000680PJ_DEF(pj_status_t) pjsua_media_transports_create(
681 const pjsua_transport_config *app_cfg)
Benny Prijonoc97608e2007-03-23 16:34:20 +0000682{
683 pjsua_transport_config cfg;
684 unsigned i;
685 pj_status_t status;
686
687
688 /* Make sure pjsua_init() has been called */
689 PJ_ASSERT_RETURN(pjsua_var.ua_cfg.max_calls>0, PJ_EINVALIDOP);
690
691 PJSUA_LOCK();
692
693 /* Delete existing media transports */
694 for (i=0; i<pjsua_var.ua_cfg.max_calls; ++i) {
695 if (pjsua_var.calls[i].med_tp != NULL) {
696 pjmedia_transport_close(pjsua_var.calls[i].med_tp);
697 pjsua_var.calls[i].med_tp = NULL;
698 }
699 }
700
701 /* Copy config */
702 pjsua_transport_config_dup(pjsua_var.pool, &cfg, app_cfg);
703
704 if (pjsua_var.media_cfg.enable_ice) {
705 status = create_ice_media_transports(&cfg);
706 } else {
707 status = create_udp_media_transports(&cfg);
708 }
709
710
Benny Prijonoeebe9af2006-06-13 22:57:13 +0000711 PJSUA_UNLOCK();
712
713 return status;
714}
715
716
Benny Prijonoc97608e2007-03-23 16:34:20 +0000717pj_status_t pjsua_media_channel_init(pjsua_call_id call_id,
718 pjsip_role_e role)
719{
720 pjsua_call *call = &pjsua_var.calls[call_id];
721
722 if (pjsua_var.media_cfg.enable_ice) {
Benny Prijonoa6bd7582007-03-28 15:49:48 +0000723 pj_ice_sess_role ice_role;
Benny Prijonoc97608e2007-03-23 16:34:20 +0000724 pj_status_t status;
725
Benny Prijonoa6bd7582007-03-28 15:49:48 +0000726 ice_role = (role==PJSIP_ROLE_UAC ? PJ_ICE_SESS_ROLE_CONTROLLING :
727 PJ_ICE_SESS_ROLE_CONTROLLED);
Benny Prijonoc97608e2007-03-23 16:34:20 +0000728
729 /* Restart ICE */
730 pjmedia_ice_stop_ice(call->med_tp);
731
732 status = pjmedia_ice_init_ice(call->med_tp, ice_role, NULL, NULL);
733 if (status != PJ_SUCCESS)
734 return status;
735 }
736
737 return PJ_SUCCESS;
738}
739
740pj_status_t pjsua_media_channel_create_sdp(pjsua_call_id call_id,
741 pj_pool_t *pool,
742 pjmedia_sdp_session **p_sdp)
743{
744 pjmedia_sdp_session *sdp;
Benny Prijono617c5bc2007-04-02 19:51:21 +0000745 pjmedia_sock_info skinfo;
Benny Prijonoc97608e2007-03-23 16:34:20 +0000746 pjsua_call *call = &pjsua_var.calls[call_id];
747 pj_status_t status;
748
Benny Prijono55e82352007-05-10 20:49:08 +0000749 /* Return error if media transport has not been created yet
750 * (e.g. application is starting)
751 */
752 if (call->med_tp == NULL) {
753 return PJ_EBUSY;
754 }
755
Benny Prijono617c5bc2007-04-02 19:51:21 +0000756 /* Get media socket info */
757 pjmedia_transport_get_info(call->med_tp, &skinfo);
758
759 /* Create SDP */
Benny Prijonoc97608e2007-03-23 16:34:20 +0000760 status = pjmedia_endpt_create_sdp(pjsua_var.med_endpt, pool, 1,
Benny Prijono617c5bc2007-04-02 19:51:21 +0000761 &skinfo, &sdp);
Benny Prijonoc97608e2007-03-23 16:34:20 +0000762 if (status != PJ_SUCCESS)
763 goto on_error;
764
Benny Prijono6ba8c542007-10-16 01:34:14 +0000765 /* Add NAT info in the SDP */
766 if (pjsua_var.ua_cfg.nat_type_in_sdp) {
767 pjmedia_sdp_attr *a;
768 pj_str_t value;
769 char nat_info[80];
770
771 value.ptr = nat_info;
772 if (pjsua_var.ua_cfg.nat_type_in_sdp == 1) {
773 value.slen = pj_ansi_snprintf(nat_info, sizeof(nat_info),
774 "%d", pjsua_var.nat_type);
775 } else {
776 const char *type_name = pj_stun_get_nat_name(pjsua_var.nat_type);
777 value.slen = pj_ansi_snprintf(nat_info, sizeof(nat_info),
778 "%d %s",
779 pjsua_var.nat_type,
780 type_name);
781 }
782
783 a = pjmedia_sdp_attr_create(pool, "X-nat", &value);
784
785 pjmedia_sdp_attr_add(&sdp->attr_count, sdp->attr, a);
786
787 }
788
Benny Prijonoc97608e2007-03-23 16:34:20 +0000789 if (pjsua_var.media_cfg.enable_ice) {
790 status = pjmedia_ice_modify_sdp(call->med_tp, pool, sdp);
791 if (status != PJ_SUCCESS)
792 goto on_error;
793 }
794
795 *p_sdp = sdp;
796 return PJ_SUCCESS;
797
798on_error:
799 pjsua_media_channel_deinit(call_id);
800 return status;
801
802}
803
804
805static void stop_media_session(pjsua_call_id call_id)
806{
807 pjsua_call *call = &pjsua_var.calls[call_id];
808
809 if (call->conf_slot != PJSUA_INVALID_ID) {
810 pjmedia_conf_remove_port(pjsua_var.mconf, call->conf_slot);
811 call->conf_slot = PJSUA_INVALID_ID;
812 }
813
814 if (call->session) {
815 pjmedia_session_destroy(call->session);
816 call->session = NULL;
817
818 PJ_LOG(4,(THIS_FILE, "Media session for call %d is destroyed",
819 call_id));
820
821 }
822
823 call->media_st = PJSUA_CALL_MEDIA_NONE;
824}
825
826pj_status_t pjsua_media_channel_deinit(pjsua_call_id call_id)
827{
828 pjsua_call *call = &pjsua_var.calls[call_id];
829
830 stop_media_session(call_id);
831
832 if (pjsua_var.media_cfg.enable_ice) {
833 pjmedia_ice_stop_ice(call->med_tp);
834 }
835
836 return PJ_SUCCESS;
837}
838
839
840/*
841 * DTMF callback from the stream.
842 */
843static void dtmf_callback(pjmedia_stream *strm, void *user_data,
844 int digit)
845{
846 PJ_UNUSED_ARG(strm);
847
848 if (pjsua_var.ua_cfg.cb.on_dtmf_digit) {
849 pjsua_call_id call_id;
850
851 call_id = (pjsua_call_id)user_data;
852 pjsua_var.ua_cfg.cb.on_dtmf_digit(call_id, digit);
853 }
854}
855
856
857pj_status_t pjsua_media_channel_update(pjsua_call_id call_id,
Benny Prijonodbce2cf2007-03-28 16:24:00 +0000858 const pjmedia_sdp_session *local_sdp,
859 const pjmedia_sdp_session *remote_sdp)
Benny Prijonoc97608e2007-03-23 16:34:20 +0000860{
861 int prev_media_st = 0;
862 pjsua_call *call = &pjsua_var.calls[call_id];
863 pjmedia_session_info sess_info;
864 pjmedia_port *media_port;
865 pj_str_t port_name;
866 char tmp[PJSIP_MAX_URL_SIZE];
867 pj_status_t status;
868
869 /* Destroy existing media session, if any. */
870 prev_media_st = call->media_st;
871 stop_media_session(call->index);
872
873 /* Create media session info based on SDP parameters.
874 * We only support one stream per session at the moment
875 */
876 status = pjmedia_session_info_from_sdp( call->inv->dlg->pool,
877 pjsua_var.med_endpt,
878 1,&sess_info,
879 local_sdp, remote_sdp);
880 if (status != PJ_SUCCESS)
881 return status;
882
883
884 /* Check if media is put on-hold */
885 if (sess_info.stream_cnt == 0 ||
886 sess_info.stream_info[0].dir == PJMEDIA_DIR_NONE)
887 {
888
889 /* Determine who puts the call on-hold */
890 if (prev_media_st == PJSUA_CALL_MEDIA_ACTIVE) {
891 if (pjmedia_sdp_neg_was_answer_remote(call->inv->neg)) {
892 /* It was local who offer hold */
893 call->media_st = PJSUA_CALL_MEDIA_LOCAL_HOLD;
894 } else {
895 call->media_st = PJSUA_CALL_MEDIA_REMOTE_HOLD;
896 }
897 }
898
899 call->media_dir = PJMEDIA_DIR_NONE;
900
Benny Prijono667952e2007-04-02 19:27:54 +0000901 /* Shutdown ICE session */
902 if (pjsua_var.media_cfg.enable_ice) {
903 pjmedia_ice_stop_ice(call->med_tp);
904 }
905
Benny Prijonoc97608e2007-03-23 16:34:20 +0000906 /* No need because we need keepalive? */
907
908 } else {
909
910 /* Start ICE */
911 if (pjsua_var.media_cfg.enable_ice) {
912 status = pjmedia_ice_start_ice(call->med_tp, call->inv->pool,
Benny Prijonofd70c9b2007-03-27 11:00:38 +0000913 remote_sdp, 0);
Benny Prijonoc97608e2007-03-23 16:34:20 +0000914 if (status != PJ_SUCCESS)
915 return status;
916 }
917
918 /* Override ptime, if this option is specified. */
919 if (pjsua_var.media_cfg.ptime != 0) {
920 sess_info.stream_info[0].param->setting.frm_per_pkt = (pj_uint8_t)
921 (pjsua_var.media_cfg.ptime / sess_info.stream_info[0].param->info.frm_ptime);
922 if (sess_info.stream_info[0].param->setting.frm_per_pkt == 0)
923 sess_info.stream_info[0].param->setting.frm_per_pkt = 1;
924 }
925
926 /* Disable VAD, if this option is specified. */
927 if (pjsua_var.media_cfg.no_vad) {
928 sess_info.stream_info[0].param->setting.vad = 0;
929 }
930
931
932 /* Optionally, application may modify other stream settings here
933 * (such as jitter buffer parameters, codec ptime, etc.)
934 */
935 sess_info.stream_info[0].jb_init = pjsua_var.media_cfg.jb_init;
936 sess_info.stream_info[0].jb_min_pre = pjsua_var.media_cfg.jb_min_pre;
937 sess_info.stream_info[0].jb_max_pre = pjsua_var.media_cfg.jb_max_pre;
938 sess_info.stream_info[0].jb_max = pjsua_var.media_cfg.jb_max;
939
940 /* Create session based on session info. */
941 status = pjmedia_session_create( pjsua_var.med_endpt, &sess_info,
942 &call->med_tp,
943 call, &call->session );
944 if (status != PJ_SUCCESS) {
945 return status;
946 }
947
948 /* If DTMF callback is installed by application, install our
949 * callback to the session.
950 */
951 if (pjsua_var.ua_cfg.cb.on_dtmf_digit) {
952 pjmedia_session_set_dtmf_callback(call->session, 0,
953 &dtmf_callback,
954 (void*)(call->index));
955 }
956
957 /* Get the port interface of the first stream in the session.
958 * We need the port interface to add to the conference bridge.
959 */
960 pjmedia_session_get_port(call->session, 0, &media_port);
961
962
963 /*
964 * Add the call to conference bridge.
965 */
966 port_name.ptr = tmp;
967 port_name.slen = pjsip_uri_print(PJSIP_URI_IN_REQ_URI,
968 call->inv->dlg->remote.info->uri,
969 tmp, sizeof(tmp));
970 if (port_name.slen < 1) {
971 port_name = pj_str("call");
972 }
973 status = pjmedia_conf_add_port( pjsua_var.mconf, call->inv->pool,
974 media_port,
975 &port_name,
976 (unsigned*)&call->conf_slot);
977 if (status != PJ_SUCCESS) {
978 return status;
979 }
980
981 /* Call's media state is active */
982 call->media_st = PJSUA_CALL_MEDIA_ACTIVE;
983 call->media_dir = sess_info.stream_info[0].dir;
984 }
985
986 /* Print info. */
987 {
988 char info[80];
989 int info_len = 0;
990 unsigned i;
991
992 for (i=0; i<sess_info.stream_cnt; ++i) {
993 int len;
994 const char *dir;
995 pjmedia_stream_info *strm_info = &sess_info.stream_info[i];
996
997 switch (strm_info->dir) {
998 case PJMEDIA_DIR_NONE:
999 dir = "inactive";
1000 break;
1001 case PJMEDIA_DIR_ENCODING:
1002 dir = "sendonly";
1003 break;
1004 case PJMEDIA_DIR_DECODING:
1005 dir = "recvonly";
1006 break;
1007 case PJMEDIA_DIR_ENCODING_DECODING:
1008 dir = "sendrecv";
1009 break;
1010 default:
1011 dir = "unknown";
1012 break;
1013 }
1014 len = pj_ansi_sprintf( info+info_len,
1015 ", stream #%d: %.*s (%s)", i,
1016 (int)strm_info->fmt.encoding_name.slen,
1017 strm_info->fmt.encoding_name.ptr,
1018 dir);
1019 if (len > 0)
1020 info_len += len;
1021 }
1022 PJ_LOG(4,(THIS_FILE,"Media updates%s", info));
1023 }
1024
1025 return PJ_SUCCESS;
1026}
1027
1028
Benny Prijonoeebe9af2006-06-13 22:57:13 +00001029/*
1030 * Get maxinum number of conference ports.
1031 */
1032PJ_DEF(unsigned) pjsua_conf_get_max_ports(void)
1033{
1034 return pjsua_var.media_cfg.max_media_ports;
1035}
1036
1037
1038/*
1039 * Get current number of active ports in the bridge.
1040 */
1041PJ_DEF(unsigned) pjsua_conf_get_active_ports(void)
1042{
1043 unsigned ports[256];
1044 unsigned count = PJ_ARRAY_SIZE(ports);
1045 pj_status_t status;
1046
1047 status = pjmedia_conf_enum_ports(pjsua_var.mconf, ports, &count);
1048 if (status != PJ_SUCCESS)
1049 count = 0;
1050
1051 return count;
1052}
1053
1054
1055/*
1056 * Enumerate all conference ports.
1057 */
1058PJ_DEF(pj_status_t) pjsua_enum_conf_ports(pjsua_conf_port_id id[],
1059 unsigned *count)
1060{
1061 return pjmedia_conf_enum_ports(pjsua_var.mconf, (unsigned*)id, count);
1062}
1063
1064
1065/*
1066 * Get information about the specified conference port
1067 */
1068PJ_DEF(pj_status_t) pjsua_conf_get_port_info( pjsua_conf_port_id id,
1069 pjsua_conf_port_info *info)
1070{
1071 pjmedia_conf_port_info cinfo;
Benny Prijono0498d902006-06-19 14:49:14 +00001072 unsigned i;
Benny Prijonoeebe9af2006-06-13 22:57:13 +00001073 pj_status_t status;
1074
1075 status = pjmedia_conf_get_port_info( pjsua_var.mconf, id, &cinfo);
1076 if (status != PJ_SUCCESS)
1077 return status;
1078
Benny Prijonoac623b32006-07-03 15:19:31 +00001079 pj_bzero(info, sizeof(*info));
Benny Prijonoeebe9af2006-06-13 22:57:13 +00001080 info->slot_id = id;
1081 info->name = cinfo.name;
1082 info->clock_rate = cinfo.clock_rate;
1083 info->channel_count = cinfo.channel_count;
1084 info->samples_per_frame = cinfo.samples_per_frame;
1085 info->bits_per_sample = cinfo.bits_per_sample;
1086
1087 /* Build array of listeners */
Benny Prijonoc78c3a32006-06-16 15:54:43 +00001088 info->listener_cnt = cinfo.listener_cnt;
1089 for (i=0; i<cinfo.listener_cnt; ++i) {
1090 info->listeners[i] = cinfo.listener_slots[i];
Benny Prijonoeebe9af2006-06-13 22:57:13 +00001091 }
1092
1093 return PJ_SUCCESS;
1094}
1095
1096
1097/*
Benny Prijonoe909eac2006-07-27 22:04:56 +00001098 * Add arbitrary media port to PJSUA's conference bridge.
1099 */
1100PJ_DEF(pj_status_t) pjsua_conf_add_port( pj_pool_t *pool,
1101 pjmedia_port *port,
1102 pjsua_conf_port_id *p_id)
1103{
1104 pj_status_t status;
1105
1106 status = pjmedia_conf_add_port(pjsua_var.mconf, pool,
1107 port, NULL, (unsigned*)p_id);
1108 if (status != PJ_SUCCESS) {
1109 if (p_id)
1110 *p_id = PJSUA_INVALID_ID;
1111 }
1112
1113 return status;
1114}
1115
1116
1117/*
1118 * Remove arbitrary slot from the conference bridge.
1119 */
1120PJ_DEF(pj_status_t) pjsua_conf_remove_port(pjsua_conf_port_id id)
1121{
1122 return pjmedia_conf_remove_port(pjsua_var.mconf, (unsigned)id);
1123}
1124
1125
1126/*
Benny Prijonoeebe9af2006-06-13 22:57:13 +00001127 * Establish unidirectional media flow from souce to sink.
1128 */
1129PJ_DEF(pj_status_t) pjsua_conf_connect( pjsua_conf_port_id source,
1130 pjsua_conf_port_id sink)
1131{
1132 return pjmedia_conf_connect_port(pjsua_var.mconf, source, sink, 0);
1133}
1134
1135
1136/*
1137 * Disconnect media flow from the source to destination port.
1138 */
1139PJ_DEF(pj_status_t) pjsua_conf_disconnect( pjsua_conf_port_id source,
1140 pjsua_conf_port_id sink)
1141{
1142 return pjmedia_conf_disconnect_port(pjsua_var.mconf, source, sink);
1143}
1144
1145
Benny Prijono6dd967c2006-12-26 02:27:14 +00001146/*
1147 * Adjust the signal level to be transmitted from the bridge to the
1148 * specified port by making it louder or quieter.
1149 */
1150PJ_DEF(pj_status_t) pjsua_conf_adjust_tx_level(pjsua_conf_port_id slot,
1151 float level)
1152{
1153 return pjmedia_conf_adjust_tx_level(pjsua_var.mconf, slot,
1154 (int)((level-1) * 128));
1155}
1156
1157/*
1158 * Adjust the signal level to be received from the specified port (to
1159 * the bridge) by making it louder or quieter.
1160 */
1161PJ_DEF(pj_status_t) pjsua_conf_adjust_rx_level(pjsua_conf_port_id slot,
1162 float level)
1163{
1164 return pjmedia_conf_adjust_rx_level(pjsua_var.mconf, slot,
1165 (int)((level-1) * 128));
1166}
1167
1168
1169/*
1170 * Get last signal level transmitted to or received from the specified port.
1171 */
1172PJ_DEF(pj_status_t) pjsua_conf_get_signal_level(pjsua_conf_port_id slot,
1173 unsigned *tx_level,
1174 unsigned *rx_level)
1175{
1176 return pjmedia_conf_get_signal_level(pjsua_var.mconf, slot,
1177 tx_level, rx_level);
1178}
1179
Benny Prijonoeebe9af2006-06-13 22:57:13 +00001180/*****************************************************************************
1181 * File player.
1182 */
1183
Benny Prijonod5696da2007-07-17 16:25:45 +00001184static char* get_basename(const char *path, unsigned len)
1185{
1186 char *p = ((char*)path) + len;
1187
1188 if (len==0)
1189 return p;
1190
Benny Prijono1f61a8f2007-08-16 10:11:44 +00001191 for (--p; p!=path && *p!='/' && *p!='\\'; ) --p;
Benny Prijonod5696da2007-07-17 16:25:45 +00001192
1193 return (p==path) ? p : p+1;
1194}
1195
1196
Benny Prijonoeebe9af2006-06-13 22:57:13 +00001197/*
1198 * Create a file player, and automatically connect this player to
1199 * the conference bridge.
1200 */
1201PJ_DEF(pj_status_t) pjsua_player_create( const pj_str_t *filename,
1202 unsigned options,
Benny Prijonoeebe9af2006-06-13 22:57:13 +00001203 pjsua_player_id *p_id)
1204{
1205 unsigned slot, file_id;
Benny Prijonoa66c3312007-01-21 23:12:40 +00001206 char path[PJ_MAXPATH];
Benny Prijonod5696da2007-07-17 16:25:45 +00001207 pj_pool_t *pool;
Benny Prijonoeebe9af2006-06-13 22:57:13 +00001208 pjmedia_port *port;
1209 pj_status_t status;
1210
1211 if (pjsua_var.player_cnt >= PJ_ARRAY_SIZE(pjsua_var.player))
1212 return PJ_ETOOMANY;
1213
1214 PJSUA_LOCK();
1215
1216 for (file_id=0; file_id<PJ_ARRAY_SIZE(pjsua_var.player); ++file_id) {
1217 if (pjsua_var.player[file_id].port == NULL)
1218 break;
1219 }
1220
1221 if (file_id == PJ_ARRAY_SIZE(pjsua_var.player)) {
1222 /* This is unexpected */
1223 PJSUA_UNLOCK();
1224 pj_assert(0);
1225 return PJ_EBUG;
1226 }
1227
1228 pj_memcpy(path, filename->ptr, filename->slen);
1229 path[filename->slen] = '\0';
Benny Prijonod5696da2007-07-17 16:25:45 +00001230
1231 pool = pjsua_pool_create(get_basename(path, filename->slen), 1000, 1000);
1232 if (!pool) {
1233 PJSUA_UNLOCK();
1234 return PJ_ENOMEM;
1235 }
1236
1237 status = pjmedia_wav_player_port_create(pool, path,
Benny Prijonoeebe9af2006-06-13 22:57:13 +00001238 pjsua_var.mconf_cfg.samples_per_frame *
1239 1000 / pjsua_var.media_cfg.clock_rate,
Benny Prijono00cae612006-07-31 15:19:36 +00001240 options, 0, &port);
Benny Prijonoeebe9af2006-06-13 22:57:13 +00001241 if (status != PJ_SUCCESS) {
1242 PJSUA_UNLOCK();
1243 pjsua_perror(THIS_FILE, "Unable to open file for playback", status);
Benny Prijonod5696da2007-07-17 16:25:45 +00001244 pj_pool_release(pool);
Benny Prijonoeebe9af2006-06-13 22:57:13 +00001245 return status;
1246 }
1247
1248 status = pjmedia_conf_add_port(pjsua_var.mconf, pjsua_var.pool,
1249 port, filename, &slot);
1250 if (status != PJ_SUCCESS) {
1251 pjmedia_port_destroy(port);
1252 PJSUA_UNLOCK();
Benny Prijono32e4f492007-01-24 00:44:26 +00001253 pjsua_perror(THIS_FILE, "Unable to add file to conference bridge",
1254 status);
Benny Prijonod5696da2007-07-17 16:25:45 +00001255 pj_pool_release(pool);
Benny Prijonoeebe9af2006-06-13 22:57:13 +00001256 return status;
1257 }
1258
Benny Prijonoa66c3312007-01-21 23:12:40 +00001259 pjsua_var.player[file_id].type = 0;
Benny Prijonod5696da2007-07-17 16:25:45 +00001260 pjsua_var.player[file_id].pool = pool;
Benny Prijonoeebe9af2006-06-13 22:57:13 +00001261 pjsua_var.player[file_id].port = port;
1262 pjsua_var.player[file_id].slot = slot;
1263
1264 if (p_id) *p_id = file_id;
1265
1266 ++pjsua_var.player_cnt;
1267
1268 PJSUA_UNLOCK();
1269 return PJ_SUCCESS;
1270}
1271
1272
1273/*
Benny Prijonoa66c3312007-01-21 23:12:40 +00001274 * Create a file playlist media port, and automatically add the port
1275 * to the conference bridge.
1276 */
1277PJ_DEF(pj_status_t) pjsua_playlist_create( const pj_str_t file_names[],
1278 unsigned file_count,
1279 const pj_str_t *label,
1280 unsigned options,
1281 pjsua_player_id *p_id)
1282{
1283 unsigned slot, file_id, ptime;
Benny Prijonod5696da2007-07-17 16:25:45 +00001284 pj_pool_t *pool;
Benny Prijonoa66c3312007-01-21 23:12:40 +00001285 pjmedia_port *port;
1286 pj_status_t status;
1287
1288 if (pjsua_var.player_cnt >= PJ_ARRAY_SIZE(pjsua_var.player))
1289 return PJ_ETOOMANY;
1290
1291 PJSUA_LOCK();
1292
1293 for (file_id=0; file_id<PJ_ARRAY_SIZE(pjsua_var.player); ++file_id) {
1294 if (pjsua_var.player[file_id].port == NULL)
1295 break;
1296 }
1297
1298 if (file_id == PJ_ARRAY_SIZE(pjsua_var.player)) {
1299 /* This is unexpected */
1300 PJSUA_UNLOCK();
1301 pj_assert(0);
1302 return PJ_EBUG;
1303 }
1304
1305
1306 ptime = pjsua_var.mconf_cfg.samples_per_frame * 1000 /
1307 pjsua_var.media_cfg.clock_rate;
1308
Benny Prijonod5696da2007-07-17 16:25:45 +00001309 pool = pjsua_pool_create("playlist", 1000, 1000);
1310 if (!pool) {
1311 PJSUA_UNLOCK();
1312 return PJ_ENOMEM;
1313 }
1314
1315 status = pjmedia_wav_playlist_create(pool, label,
Benny Prijonoa66c3312007-01-21 23:12:40 +00001316 file_names, file_count,
1317 ptime, options, 0, &port);
1318 if (status != PJ_SUCCESS) {
1319 PJSUA_UNLOCK();
1320 pjsua_perror(THIS_FILE, "Unable to create playlist", status);
Benny Prijonod5696da2007-07-17 16:25:45 +00001321 pj_pool_release(pool);
Benny Prijonoa66c3312007-01-21 23:12:40 +00001322 return status;
1323 }
1324
Benny Prijonod5696da2007-07-17 16:25:45 +00001325 status = pjmedia_conf_add_port(pjsua_var.mconf, pool,
Benny Prijonoa66c3312007-01-21 23:12:40 +00001326 port, &port->info.name, &slot);
1327 if (status != PJ_SUCCESS) {
1328 pjmedia_port_destroy(port);
1329 PJSUA_UNLOCK();
1330 pjsua_perror(THIS_FILE, "Unable to add port", status);
Benny Prijonod5696da2007-07-17 16:25:45 +00001331 pj_pool_release(pool);
Benny Prijonoa66c3312007-01-21 23:12:40 +00001332 return status;
1333 }
1334
1335 pjsua_var.player[file_id].type = 1;
Benny Prijonod5696da2007-07-17 16:25:45 +00001336 pjsua_var.player[file_id].pool = pool;
Benny Prijonoa66c3312007-01-21 23:12:40 +00001337 pjsua_var.player[file_id].port = port;
1338 pjsua_var.player[file_id].slot = slot;
1339
1340 if (p_id) *p_id = file_id;
1341
1342 ++pjsua_var.player_cnt;
1343
1344 PJSUA_UNLOCK();
1345 return PJ_SUCCESS;
1346
1347}
1348
1349
1350/*
Benny Prijonoeebe9af2006-06-13 22:57:13 +00001351 * Get conference port ID associated with player.
1352 */
1353PJ_DEF(pjsua_conf_port_id) pjsua_player_get_conf_port(pjsua_player_id id)
1354{
Benny Prijonoa1e69682007-05-11 15:14:34 +00001355 PJ_ASSERT_RETURN(id>=0&&id<(int)PJ_ARRAY_SIZE(pjsua_var.player), PJ_EINVAL);
Benny Prijonoeebe9af2006-06-13 22:57:13 +00001356 PJ_ASSERT_RETURN(pjsua_var.player[id].port != NULL, PJ_EINVAL);
1357
1358 return pjsua_var.player[id].slot;
1359}
1360
Benny Prijono469b1522006-12-26 03:05:17 +00001361/*
1362 * Get the media port for the player.
1363 */
1364PJ_DEF(pj_status_t) pjsua_player_get_port( pjsua_recorder_id id,
1365 pjmedia_port **p_port)
1366{
Benny Prijonoa1e69682007-05-11 15:14:34 +00001367 PJ_ASSERT_RETURN(id>=0&&id<(int)PJ_ARRAY_SIZE(pjsua_var.player), PJ_EINVAL);
Benny Prijono469b1522006-12-26 03:05:17 +00001368 PJ_ASSERT_RETURN(pjsua_var.player[id].port != NULL, PJ_EINVAL);
1369 PJ_ASSERT_RETURN(p_port != NULL, PJ_EINVAL);
1370
1371 *p_port = pjsua_var.player[id].port;
1372
1373 return PJ_SUCCESS;
1374}
Benny Prijonoeebe9af2006-06-13 22:57:13 +00001375
1376/*
1377 * Set playback position.
1378 */
1379PJ_DEF(pj_status_t) pjsua_player_set_pos( pjsua_player_id id,
1380 pj_uint32_t samples)
1381{
Benny Prijonoa1e69682007-05-11 15:14:34 +00001382 PJ_ASSERT_RETURN(id>=0&&id<(int)PJ_ARRAY_SIZE(pjsua_var.player), PJ_EINVAL);
Benny Prijonoeebe9af2006-06-13 22:57:13 +00001383 PJ_ASSERT_RETURN(pjsua_var.player[id].port != NULL, PJ_EINVAL);
Benny Prijonoa66c3312007-01-21 23:12:40 +00001384 PJ_ASSERT_RETURN(pjsua_var.player[id].type == 0, PJ_EINVAL);
Benny Prijonoeebe9af2006-06-13 22:57:13 +00001385
1386 return pjmedia_wav_player_port_set_pos(pjsua_var.player[id].port, samples);
1387}
1388
1389
1390/*
1391 * Close the file, remove the player from the bridge, and free
1392 * resources associated with the file player.
1393 */
1394PJ_DEF(pj_status_t) pjsua_player_destroy(pjsua_player_id id)
1395{
Benny Prijonoa1e69682007-05-11 15:14:34 +00001396 PJ_ASSERT_RETURN(id>=0&&id<(int)PJ_ARRAY_SIZE(pjsua_var.player), PJ_EINVAL);
Benny Prijonoeebe9af2006-06-13 22:57:13 +00001397 PJ_ASSERT_RETURN(pjsua_var.player[id].port != NULL, PJ_EINVAL);
1398
1399 PJSUA_LOCK();
1400
1401 if (pjsua_var.player[id].port) {
1402 pjmedia_conf_remove_port(pjsua_var.mconf,
1403 pjsua_var.player[id].slot);
1404 pjmedia_port_destroy(pjsua_var.player[id].port);
1405 pjsua_var.player[id].port = NULL;
1406 pjsua_var.player[id].slot = 0xFFFF;
Benny Prijonod5696da2007-07-17 16:25:45 +00001407 pj_pool_release(pjsua_var.player[id].pool);
1408 pjsua_var.player[id].pool = NULL;
Benny Prijonoeebe9af2006-06-13 22:57:13 +00001409 pjsua_var.player_cnt--;
1410 }
1411
1412 PJSUA_UNLOCK();
1413
1414 return PJ_SUCCESS;
1415}
1416
1417
1418/*****************************************************************************
1419 * File recorder.
1420 */
1421
1422/*
1423 * Create a file recorder, and automatically connect this recorder to
1424 * the conference bridge.
1425 */
1426PJ_DEF(pj_status_t) pjsua_recorder_create( const pj_str_t *filename,
Benny Prijono8f310522006-10-20 11:08:49 +00001427 unsigned enc_type,
1428 void *enc_param,
Benny Prijonoeebe9af2006-06-13 22:57:13 +00001429 pj_ssize_t max_size,
1430 unsigned options,
Benny Prijonoeebe9af2006-06-13 22:57:13 +00001431 pjsua_recorder_id *p_id)
1432{
Benny Prijonob3cdb2b2006-10-19 15:49:47 +00001433 enum Format
1434 {
1435 FMT_UNKNOWN,
1436 FMT_WAV,
1437 FMT_MP3,
1438 };
Benny Prijonoeebe9af2006-06-13 22:57:13 +00001439 unsigned slot, file_id;
Benny Prijonoa66c3312007-01-21 23:12:40 +00001440 char path[PJ_MAXPATH];
Benny Prijonob3cdb2b2006-10-19 15:49:47 +00001441 pj_str_t ext;
Benny Prijono8f310522006-10-20 11:08:49 +00001442 int file_format;
Benny Prijonod5696da2007-07-17 16:25:45 +00001443 pj_pool_t *pool;
Benny Prijonoeebe9af2006-06-13 22:57:13 +00001444 pjmedia_port *port;
1445 pj_status_t status;
1446
Benny Prijonob3cdb2b2006-10-19 15:49:47 +00001447 /* Filename must present */
1448 PJ_ASSERT_RETURN(filename != NULL, PJ_EINVAL);
1449
Benny Prijono00cae612006-07-31 15:19:36 +00001450 /* Don't support max_size at present */
Benny Prijonob3cdb2b2006-10-19 15:49:47 +00001451 PJ_ASSERT_RETURN(max_size == 0 || max_size == -1, PJ_EINVAL);
Benny Prijono00cae612006-07-31 15:19:36 +00001452
Benny Prijono8f310522006-10-20 11:08:49 +00001453 /* Don't support encoding type at present */
1454 PJ_ASSERT_RETURN(enc_type == 0, PJ_EINVAL);
Benny Prijono00cae612006-07-31 15:19:36 +00001455
Benny Prijonoeebe9af2006-06-13 22:57:13 +00001456 if (pjsua_var.rec_cnt >= PJ_ARRAY_SIZE(pjsua_var.recorder))
1457 return PJ_ETOOMANY;
1458
Benny Prijonob3cdb2b2006-10-19 15:49:47 +00001459 /* Determine the file format */
1460 ext.ptr = filename->ptr + filename->slen - 4;
1461 ext.slen = 4;
1462
1463 if (pj_stricmp2(&ext, ".wav") == 0)
1464 file_format = FMT_WAV;
1465 else if (pj_stricmp2(&ext, ".mp3") == 0)
1466 file_format = FMT_MP3;
1467 else {
1468 PJ_LOG(1,(THIS_FILE, "pjsua_recorder_create() error: unable to "
1469 "determine file format for %.*s",
1470 (int)filename->slen, filename->ptr));
1471 return PJ_ENOTSUP;
1472 }
1473
Benny Prijonoeebe9af2006-06-13 22:57:13 +00001474 PJSUA_LOCK();
1475
1476 for (file_id=0; file_id<PJ_ARRAY_SIZE(pjsua_var.recorder); ++file_id) {
1477 if (pjsua_var.recorder[file_id].port == NULL)
1478 break;
1479 }
1480
1481 if (file_id == PJ_ARRAY_SIZE(pjsua_var.recorder)) {
1482 /* This is unexpected */
1483 PJSUA_UNLOCK();
1484 pj_assert(0);
1485 return PJ_EBUG;
1486 }
1487
1488 pj_memcpy(path, filename->ptr, filename->slen);
1489 path[filename->slen] = '\0';
Benny Prijonob3cdb2b2006-10-19 15:49:47 +00001490
Benny Prijonod5696da2007-07-17 16:25:45 +00001491 pool = pjsua_pool_create(get_basename(path, filename->slen), 1000, 1000);
1492 if (!pool) {
1493 PJSUA_UNLOCK();
1494 return PJ_ENOMEM;
1495 }
1496
Benny Prijonob3cdb2b2006-10-19 15:49:47 +00001497 if (file_format == FMT_WAV) {
Benny Prijonod5696da2007-07-17 16:25:45 +00001498 status = pjmedia_wav_writer_port_create(pool, path,
Benny Prijonob3cdb2b2006-10-19 15:49:47 +00001499 pjsua_var.media_cfg.clock_rate,
1500 pjsua_var.mconf_cfg.channel_count,
1501 pjsua_var.mconf_cfg.samples_per_frame,
1502 pjsua_var.mconf_cfg.bits_per_sample,
1503 options, 0, &port);
Benny Prijonob3cdb2b2006-10-19 15:49:47 +00001504 } else {
Benny Prijonoc95a0f02007-04-09 07:06:08 +00001505 PJ_UNUSED_ARG(enc_param);
Benny Prijonob3cdb2b2006-10-19 15:49:47 +00001506 port = NULL;
1507 status = PJ_ENOTSUP;
1508 }
1509
Benny Prijonoeebe9af2006-06-13 22:57:13 +00001510 if (status != PJ_SUCCESS) {
1511 PJSUA_UNLOCK();
1512 pjsua_perror(THIS_FILE, "Unable to open file for recording", status);
Benny Prijonod5696da2007-07-17 16:25:45 +00001513 pj_pool_release(pool);
Benny Prijonoeebe9af2006-06-13 22:57:13 +00001514 return status;
1515 }
1516
Benny Prijonod5696da2007-07-17 16:25:45 +00001517 status = pjmedia_conf_add_port(pjsua_var.mconf, pool,
Benny Prijonoeebe9af2006-06-13 22:57:13 +00001518 port, filename, &slot);
1519 if (status != PJ_SUCCESS) {
1520 pjmedia_port_destroy(port);
1521 PJSUA_UNLOCK();
Benny Prijonod5696da2007-07-17 16:25:45 +00001522 pj_pool_release(pool);
Benny Prijonoeebe9af2006-06-13 22:57:13 +00001523 return status;
1524 }
1525
1526 pjsua_var.recorder[file_id].port = port;
1527 pjsua_var.recorder[file_id].slot = slot;
Benny Prijonod5696da2007-07-17 16:25:45 +00001528 pjsua_var.recorder[file_id].pool = pool;
Benny Prijonoeebe9af2006-06-13 22:57:13 +00001529
1530 if (p_id) *p_id = file_id;
1531
1532 ++pjsua_var.rec_cnt;
1533
1534 PJSUA_UNLOCK();
1535 return PJ_SUCCESS;
1536}
1537
1538
1539/*
1540 * Get conference port associated with recorder.
1541 */
1542PJ_DEF(pjsua_conf_port_id) pjsua_recorder_get_conf_port(pjsua_recorder_id id)
1543{
Benny Prijonoa1e69682007-05-11 15:14:34 +00001544 PJ_ASSERT_RETURN(id>=0 && id<(int)PJ_ARRAY_SIZE(pjsua_var.recorder),
1545 PJ_EINVAL);
Benny Prijonoeebe9af2006-06-13 22:57:13 +00001546 PJ_ASSERT_RETURN(pjsua_var.recorder[id].port != NULL, PJ_EINVAL);
1547
1548 return pjsua_var.recorder[id].slot;
1549}
1550
Benny Prijono469b1522006-12-26 03:05:17 +00001551/*
1552 * Get the media port for the recorder.
1553 */
1554PJ_DEF(pj_status_t) pjsua_recorder_get_port( pjsua_recorder_id id,
1555 pjmedia_port **p_port)
1556{
Benny Prijonoa1e69682007-05-11 15:14:34 +00001557 PJ_ASSERT_RETURN(id>=0 && id<(int)PJ_ARRAY_SIZE(pjsua_var.recorder),
1558 PJ_EINVAL);
Benny Prijono469b1522006-12-26 03:05:17 +00001559 PJ_ASSERT_RETURN(pjsua_var.recorder[id].port != NULL, PJ_EINVAL);
1560 PJ_ASSERT_RETURN(p_port != NULL, PJ_EINVAL);
1561
1562 *p_port = pjsua_var.recorder[id].port;
1563 return PJ_SUCCESS;
1564}
Benny Prijonoeebe9af2006-06-13 22:57:13 +00001565
1566/*
1567 * Destroy recorder (this will complete recording).
1568 */
1569PJ_DEF(pj_status_t) pjsua_recorder_destroy(pjsua_recorder_id id)
1570{
Benny Prijonoa1e69682007-05-11 15:14:34 +00001571 PJ_ASSERT_RETURN(id>=0 && id<(int)PJ_ARRAY_SIZE(pjsua_var.recorder),
1572 PJ_EINVAL);
Benny Prijonoeebe9af2006-06-13 22:57:13 +00001573 PJ_ASSERT_RETURN(pjsua_var.recorder[id].port != NULL, PJ_EINVAL);
1574
1575 PJSUA_LOCK();
1576
1577 if (pjsua_var.recorder[id].port) {
1578 pjmedia_conf_remove_port(pjsua_var.mconf,
1579 pjsua_var.recorder[id].slot);
1580 pjmedia_port_destroy(pjsua_var.recorder[id].port);
1581 pjsua_var.recorder[id].port = NULL;
1582 pjsua_var.recorder[id].slot = 0xFFFF;
Benny Prijonod5696da2007-07-17 16:25:45 +00001583 pj_pool_release(pjsua_var.recorder[id].pool);
1584 pjsua_var.recorder[id].pool = NULL;
Benny Prijonoeebe9af2006-06-13 22:57:13 +00001585 pjsua_var.rec_cnt--;
1586 }
1587
1588 PJSUA_UNLOCK();
1589
1590 return PJ_SUCCESS;
1591}
1592
1593
1594/*****************************************************************************
1595 * Sound devices.
1596 */
1597
1598/*
1599 * Enum sound devices.
1600 */
1601PJ_DEF(pj_status_t) pjsua_enum_snd_devs( pjmedia_snd_dev_info info[],
1602 unsigned *count)
1603{
1604 unsigned i, dev_count;
1605
1606 dev_count = pjmedia_snd_get_dev_count();
1607
1608 if (dev_count > *count) dev_count = *count;
1609
1610 for (i=0; i<dev_count; ++i) {
1611 const pjmedia_snd_dev_info *ci;
1612
1613 ci = pjmedia_snd_get_dev_info(i);
1614 pj_memcpy(&info[i], ci, sizeof(*ci));
1615 }
1616
1617 *count = dev_count;
1618
1619 return PJ_SUCCESS;
1620}
1621
1622
1623/* Close existing sound device */
1624static void close_snd_dev(void)
1625{
1626 /* Close sound device */
1627 if (pjsua_var.snd_port) {
1628 const pjmedia_snd_dev_info *cap_info, *play_info;
1629
1630 cap_info = pjmedia_snd_get_dev_info(pjsua_var.cap_dev);
1631 play_info = pjmedia_snd_get_dev_info(pjsua_var.play_dev);
1632
1633 PJ_LOG(4,(THIS_FILE, "Closing %s sound playback device and "
1634 "%s sound capture device",
1635 play_info->name, cap_info->name));
1636
1637 pjmedia_snd_port_disconnect(pjsua_var.snd_port);
1638 pjmedia_snd_port_destroy(pjsua_var.snd_port);
1639 pjsua_var.snd_port = NULL;
1640 }
1641
1642 /* Close null sound device */
1643 if (pjsua_var.null_snd) {
1644 PJ_LOG(4,(THIS_FILE, "Closing null sound device.."));
1645 pjmedia_master_port_destroy(pjsua_var.null_snd, PJ_FALSE);
1646 pjsua_var.null_snd = NULL;
1647 }
1648}
1649
1650/*
1651 * Select or change sound device. Application may call this function at
1652 * any time to replace current sound device.
1653 */
1654PJ_DEF(pj_status_t) pjsua_set_snd_dev( int capture_dev,
1655 int playback_dev)
1656{
1657 pjmedia_port *conf_port;
Benny Prijono6dd967c2006-12-26 02:27:14 +00001658 const pjmedia_snd_dev_info *play_info;
Benny Prijono26056d82006-10-11 16:03:41 +00001659 unsigned clock_rates[] = { 0, 22050, 44100, 48000, 11025, 32000, 8000};
Benny Prijono658a1c52006-10-11 21:56:16 +00001660 unsigned selected_clock_rate = 0;
Benny Prijono26056d82006-10-11 16:03:41 +00001661 unsigned i;
Benny Prijonoc53c6d72006-11-27 09:54:03 +00001662 pjmedia_snd_stream *strm;
1663 pjmedia_snd_stream_info si;
1664 pj_str_t tmp;
Benny Prijono26056d82006-10-11 16:03:41 +00001665 pj_status_t status = -1;
Benny Prijonoeebe9af2006-06-13 22:57:13 +00001666
1667 /* Close existing sound port */
1668 close_snd_dev();
1669
1670
Benny Prijono26056d82006-10-11 16:03:41 +00001671 /* Set default clock rate */
1672 clock_rates[0] = pjsua_var.media_cfg.clock_rate;
Benny Prijonoeebe9af2006-06-13 22:57:13 +00001673
Benny Prijono26056d82006-10-11 16:03:41 +00001674 /* Attempts to open the sound device with different clock rates */
1675 for (i=0; i<PJ_ARRAY_SIZE(clock_rates); ++i) {
1676 char errmsg[PJ_ERR_MSG_SIZE];
Benny Prijonocf0b4b22007-10-06 17:31:09 +00001677 unsigned fps;
Benny Prijono26056d82006-10-11 16:03:41 +00001678
1679 PJ_LOG(4,(THIS_FILE,
1680 "pjsua_set_snd_dev(): attempting to open devices "
1681 "@%d Hz", clock_rates[i]));
1682
1683 /* Create the sound device. Sound port will start immediately. */
Benny Prijonocf0b4b22007-10-06 17:31:09 +00001684 fps = 1000 / pjsua_var.media_cfg.audio_frame_ptime;
Benny Prijono26056d82006-10-11 16:03:41 +00001685 status = pjmedia_snd_port_create(pjsua_var.pool, capture_dev,
1686 playback_dev,
1687 clock_rates[i], 1,
Benny Prijonocf0b4b22007-10-06 17:31:09 +00001688 clock_rates[i]/fps,
Benny Prijono26056d82006-10-11 16:03:41 +00001689 16, 0, &pjsua_var.snd_port);
1690
Benny Prijono658a1c52006-10-11 21:56:16 +00001691 if (status == PJ_SUCCESS) {
1692 selected_clock_rate = clock_rates[i];
Benny Prijono26056d82006-10-11 16:03:41 +00001693 break;
Benny Prijono658a1c52006-10-11 21:56:16 +00001694 }
Benny Prijono26056d82006-10-11 16:03:41 +00001695
1696 pj_strerror(status, errmsg, sizeof(errmsg));
Benny Prijono658a1c52006-10-11 21:56:16 +00001697 PJ_LOG(4, (THIS_FILE, "..failed: %s", errmsg));
Benny Prijono26056d82006-10-11 16:03:41 +00001698 }
1699
Benny Prijonoeebe9af2006-06-13 22:57:13 +00001700 if (status != PJ_SUCCESS) {
1701 pjsua_perror(THIS_FILE, "Unable to open sound device", status);
1702 return status;
1703 }
1704
1705 /* Get the port0 of the conference bridge. */
1706 conf_port = pjmedia_conf_get_master_port(pjsua_var.mconf);
1707 pj_assert(conf_port != NULL);
1708
Benny Prijonof20687a2006-08-04 18:27:19 +00001709 /* Set AEC */
Benny Prijono5da50432006-08-07 10:24:52 +00001710 pjmedia_snd_port_set_ec( pjsua_var.snd_port, pjsua_var.pool,
1711 pjsua_var.media_cfg.ec_tail_len,
1712 pjsua_var.media_cfg.ec_options);
Benny Prijonoeebe9af2006-06-13 22:57:13 +00001713
Benny Prijono658a1c52006-10-11 21:56:16 +00001714 /* If there's mismatch between sound port and conference's port,
1715 * create a resample port to bridge them.
1716 */
1717 if (selected_clock_rate != pjsua_var.media_cfg.clock_rate) {
1718 pjmedia_port *resample_port;
1719
1720 status = pjmedia_resample_port_create(pjsua_var.pool, conf_port,
1721 selected_clock_rate, 0,
1722 &resample_port);
1723 if (status != PJ_SUCCESS) {
1724 pjsua_perror("Error creating resample port", THIS_FILE, status);
1725 return status;
1726 }
1727
1728 conf_port = resample_port;
1729 }
1730
Benny Prijono52a93912006-08-04 20:54:37 +00001731 /* Connect sound port to the bridge */
1732 status = pjmedia_snd_port_connect(pjsua_var.snd_port,
1733 conf_port );
1734 if (status != PJ_SUCCESS) {
1735 pjsua_perror(THIS_FILE, "Unable to connect conference port to "
1736 "sound device", status);
1737 pjmedia_snd_port_destroy(pjsua_var.snd_port);
1738 pjsua_var.snd_port = NULL;
1739 return status;
1740 }
1741
Benny Prijonoeebe9af2006-06-13 22:57:13 +00001742 /* Save the device IDs */
1743 pjsua_var.cap_dev = capture_dev;
1744 pjsua_var.play_dev = playback_dev;
1745
Benny Prijonoc53c6d72006-11-27 09:54:03 +00001746 /* Update sound device name. */
1747 strm = pjmedia_snd_port_get_snd_stream(pjsua_var.snd_port);
1748 pjmedia_snd_stream_get_info(strm, &si);
1749 play_info = pjmedia_snd_get_dev_info(si.rec_id);
1750
1751 pjmedia_conf_set_port0_name(pjsua_var.mconf,
1752 pj_cstr(&tmp, play_info->name));
1753
Benny Prijonoeebe9af2006-06-13 22:57:13 +00001754 return PJ_SUCCESS;
1755}
1756
1757
1758/*
Benny Prijonoebdf8772007-02-01 19:25:50 +00001759 * Get currently active sound devices. If sound devices has not been created
1760 * (for example when pjsua_start() is not called), it is possible that
1761 * the function returns PJ_SUCCESS with -1 as device IDs.
1762 */
1763PJ_DEF(pj_status_t) pjsua_get_snd_dev(int *capture_dev,
1764 int *playback_dev)
1765{
1766 if (capture_dev) {
1767 *capture_dev = pjsua_var.cap_dev;
1768 }
1769 if (playback_dev) {
1770 *playback_dev = pjsua_var.play_dev;
1771 }
1772
1773 return PJ_SUCCESS;
1774}
1775
1776
1777/*
Benny Prijonoeebe9af2006-06-13 22:57:13 +00001778 * Use null sound device.
1779 */
1780PJ_DEF(pj_status_t) pjsua_set_null_snd_dev(void)
1781{
1782 pjmedia_port *conf_port;
1783 pj_status_t status;
1784
1785 /* Close existing sound device */
1786 close_snd_dev();
1787
1788 PJ_LOG(4,(THIS_FILE, "Opening null sound device.."));
1789
1790 /* Get the port0 of the conference bridge. */
1791 conf_port = pjmedia_conf_get_master_port(pjsua_var.mconf);
1792 pj_assert(conf_port != NULL);
1793
1794 /* Create master port, connecting port0 of the conference bridge to
1795 * a null port.
1796 */
1797 status = pjmedia_master_port_create(pjsua_var.pool, pjsua_var.null_port,
1798 conf_port, 0, &pjsua_var.null_snd);
1799 if (status != PJ_SUCCESS) {
1800 pjsua_perror(THIS_FILE, "Unable to create null sound device",
1801 status);
1802 return status;
1803 }
1804
1805 /* Start the master port */
1806 status = pjmedia_master_port_start(pjsua_var.null_snd);
1807 PJ_ASSERT_RETURN(status == PJ_SUCCESS, status);
1808
1809 return PJ_SUCCESS;
1810}
1811
1812
Benny Prijonoe909eac2006-07-27 22:04:56 +00001813
1814/*
1815 * Use no device!
1816 */
1817PJ_DEF(pjmedia_port*) pjsua_set_no_snd_dev(void)
1818{
1819 /* Close existing sound device */
1820 close_snd_dev();
1821
1822 pjsua_var.no_snd = PJ_TRUE;
1823 return pjmedia_conf_get_master_port(pjsua_var.mconf);
1824}
1825
1826
Benny Prijonof20687a2006-08-04 18:27:19 +00001827/*
1828 * Configure the AEC settings of the sound port.
1829 */
Benny Prijono5da50432006-08-07 10:24:52 +00001830PJ_DEF(pj_status_t) pjsua_set_ec(unsigned tail_ms, unsigned options)
Benny Prijonof20687a2006-08-04 18:27:19 +00001831{
1832 pjsua_var.media_cfg.ec_tail_len = tail_ms;
1833
1834 if (pjsua_var.snd_port)
Benny Prijono5da50432006-08-07 10:24:52 +00001835 return pjmedia_snd_port_set_ec( pjsua_var.snd_port, pjsua_var.pool,
1836 tail_ms, options);
Benny Prijonof20687a2006-08-04 18:27:19 +00001837
1838 return PJ_SUCCESS;
1839}
1840
1841
1842/*
1843 * Get current AEC tail length.
1844 */
Benny Prijono22dfe592006-08-06 12:07:13 +00001845PJ_DEF(pj_status_t) pjsua_get_ec_tail(unsigned *p_tail_ms)
Benny Prijonof20687a2006-08-04 18:27:19 +00001846{
1847 *p_tail_ms = pjsua_var.media_cfg.ec_tail_len;
1848 return PJ_SUCCESS;
1849}
1850
Benny Prijonoe909eac2006-07-27 22:04:56 +00001851
Benny Prijonoeebe9af2006-06-13 22:57:13 +00001852/*****************************************************************************
1853 * Codecs.
1854 */
1855
1856/*
1857 * Enum all supported codecs in the system.
1858 */
1859PJ_DEF(pj_status_t) pjsua_enum_codecs( pjsua_codec_info id[],
1860 unsigned *p_count )
1861{
1862 pjmedia_codec_mgr *codec_mgr;
1863 pjmedia_codec_info info[32];
1864 unsigned i, count, prio[32];
1865 pj_status_t status;
1866
1867 codec_mgr = pjmedia_endpt_get_codec_mgr(pjsua_var.med_endpt);
1868 count = PJ_ARRAY_SIZE(info);
1869 status = pjmedia_codec_mgr_enum_codecs( codec_mgr, &count, info, prio);
1870 if (status != PJ_SUCCESS) {
1871 *p_count = 0;
1872 return status;
1873 }
1874
1875 if (count > *p_count) count = *p_count;
1876
1877 for (i=0; i<count; ++i) {
1878 pjmedia_codec_info_to_id(&info[i], id[i].buf_, sizeof(id[i].buf_));
1879 id[i].codec_id = pj_str(id[i].buf_);
1880 id[i].priority = (pj_uint8_t) prio[i];
1881 }
1882
1883 *p_count = count;
1884
1885 return PJ_SUCCESS;
1886}
1887
1888
1889/*
1890 * Change codec priority.
1891 */
1892PJ_DEF(pj_status_t) pjsua_codec_set_priority( const pj_str_t *codec_id,
1893 pj_uint8_t priority )
1894{
1895 pjmedia_codec_mgr *codec_mgr;
1896
1897 codec_mgr = pjmedia_endpt_get_codec_mgr(pjsua_var.med_endpt);
1898
1899 return pjmedia_codec_mgr_set_codec_priority(codec_mgr, codec_id,
1900 priority);
1901}
1902
1903
1904/*
1905 * Get codec parameters.
1906 */
1907PJ_DEF(pj_status_t) pjsua_codec_get_param( const pj_str_t *codec_id,
1908 pjmedia_codec_param *param )
1909{
1910 const pjmedia_codec_info *info;
1911 pjmedia_codec_mgr *codec_mgr;
1912 unsigned count = 1;
1913 pj_status_t status;
1914
1915 codec_mgr = pjmedia_endpt_get_codec_mgr(pjsua_var.med_endpt);
1916
1917 status = pjmedia_codec_mgr_find_codecs_by_id(codec_mgr, codec_id,
1918 &count, &info, NULL);
1919 if (status != PJ_SUCCESS)
1920 return status;
1921
1922 if (count != 1)
1923 return PJ_ENOTFOUND;
1924
1925 status = pjmedia_codec_mgr_get_default_param( codec_mgr, info, param);
1926 return status;
1927}
1928
1929
1930/*
1931 * Set codec parameters.
1932 */
1933PJ_DEF(pj_status_t) pjsua_codec_set_param( const pj_str_t *id,
1934 const pjmedia_codec_param *param)
1935{
Benny Prijono00cae612006-07-31 15:19:36 +00001936 PJ_UNUSED_ARG(id);
1937 PJ_UNUSED_ARG(param);
Benny Prijonoeebe9af2006-06-13 22:57:13 +00001938 PJ_TODO(set_codec_param);
1939 return PJ_SUCCESS;
1940}