Benny Prijono | eebe9af | 2006-06-13 22:57:13 +0000 | [diff] [blame] | 1 | /* $Id$ */ |
| 2 | /* |
Benny Prijono | a771a51 | 2007-02-19 01:13:53 +0000 | [diff] [blame] | 3 | * Copyright (C) 2003-2007 Benny Prijono <benny@prijono.org> |
Benny Prijono | eebe9af | 2006-06-13 22:57:13 +0000 | [diff] [blame] | 4 | * |
| 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 Prijono | 403e24c | 2007-01-25 17:13:34 +0000 | [diff] [blame] | 25 | #define PTIME 20 |
Benny Prijono | eebe9af | 2006-06-13 22:57:13 +0000 | [diff] [blame] | 26 | #define FPS (1000/PTIME) |
| 27 | #define DEFAULT_RTP_PORT 4000 |
| 28 | |
Benny Prijono | de47956 | 2007-03-15 10:23:55 +0000 | [diff] [blame] | 29 | /* Next RTP port to be used */ |
| 30 | static pj_uint16_t next_rtp_port; |
Benny Prijono | eebe9af | 2006-06-13 22:57:13 +0000 | [diff] [blame] | 31 | |
| 32 | /* Close existing sound device */ |
| 33 | static void close_snd_dev(void); |
| 34 | |
| 35 | |
| 36 | /** |
| 37 | * Init media subsystems. |
| 38 | */ |
| 39 | pj_status_t pjsua_media_subsys_init(const pjsua_media_config *cfg) |
| 40 | { |
Benny Prijono | ba5926a | 2007-05-02 11:29:37 +0000 | [diff] [blame] | 41 | pj_str_t codec_id = {NULL, 0}; |
Benny Prijono | 0498d90 | 2006-06-19 14:49:14 +0000 | [diff] [blame] | 42 | unsigned opt; |
Benny Prijono | eebe9af | 2006-06-13 22:57:13 +0000 | [diff] [blame] | 43 | pj_status_t status; |
| 44 | |
Benny Prijono | fc24e69 | 2007-01-27 18:31:51 +0000 | [diff] [blame] | 45 | /* To suppress warning about unused var when all codecs are disabled */ |
| 46 | PJ_UNUSED_ARG(codec_id); |
| 47 | |
Benny Prijono | eebe9af | 2006-06-13 22:57:13 +0000 | [diff] [blame] | 48 | /* Copy configuration */ |
| 49 | pj_memcpy(&pjsua_var.media_cfg, cfg, sizeof(*cfg)); |
| 50 | |
| 51 | /* Normalize configuration */ |
Benny Prijono | eebe9af | 2006-06-13 22:57:13 +0000 | [diff] [blame] | 52 | |
| 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 Prijono | 7ca96da | 2006-08-07 12:11:40 +0000 | [diff] [blame] | 80 | 0, |
Benny Prijono | 0498d90 | 2006-06-19 14:49:14 +0000 | [diff] [blame] | 81 | pjsua_var.media_cfg.quality, |
| 82 | pjsua_var.media_cfg.quality); |
Benny Prijono | eebe9af | 2006-06-13 22:57:13 +0000 | [diff] [blame] | 83 | if (status != PJ_SUCCESS) { |
| 84 | pjsua_perror(THIS_FILE, "Error initializing Speex codec", |
| 85 | status); |
| 86 | return status; |
| 87 | } |
Benny Prijono | 7ca96da | 2006-08-07 12:11:40 +0000 | [diff] [blame] | 88 | |
| 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 Prijono | eebe9af | 2006-06-13 22:57:13 +0000 | [diff] [blame] | 103 | #endif /* PJMEDIA_HAS_SPEEX_CODEC */ |
| 104 | |
Benny Prijono | 00cae61 | 2006-07-31 15:19:36 +0000 | [diff] [blame] | 105 | #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 Prijono | eebe9af | 2006-06-13 22:57:13 +0000 | [diff] [blame] | 116 | #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 Prijono | 0498d90 | 2006-06-19 14:49:14 +0000 | [diff] [blame] | 162 | /* Init options for conference bridge. */ |
| 163 | opt = PJMEDIA_CONF_NO_DEVICE; |
| 164 | if (pjsua_var.media_cfg.quality >= 3 && |
Benny Prijono | 7ca96da | 2006-08-07 12:11:40 +0000 | [diff] [blame] | 165 | pjsua_var.media_cfg.quality <= 4) |
Benny Prijono | 0498d90 | 2006-06-19 14:49:14 +0000 | [diff] [blame] | 166 | { |
| 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 Prijono | eebe9af | 2006-06-13 22:57:13 +0000 | [diff] [blame] | 174 | /* 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 Prijono | 0498d90 | 2006-06-19 14:49:14 +0000 | [diff] [blame] | 181 | opt, &pjsua_var.mconf); |
Benny Prijono | eebe9af | 2006-06-13 22:57:13 +0000 | [diff] [blame] | 182 | 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 | */ |
| 206 | static 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 Prijono | 0a5cad8 | 2006-09-26 13:21:02 +0000 | [diff] [blame] | 213 | pj_sockaddr_in bound_addr; |
Benny Prijono | eebe9af | 2006-06-13 22:57:13 +0000 | [diff] [blame] | 214 | pj_sockaddr_in mapped_addr[2]; |
| 215 | pj_status_t status = PJ_SUCCESS; |
| 216 | pj_sock_t sock[2]; |
| 217 | |
Benny Prijono | c97608e | 2007-03-23 16:34:20 +0000 | [diff] [blame] | 218 | /* 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 Prijono | de47956 | 2007-03-15 10:23:55 +0000 | [diff] [blame] | 226 | if (next_rtp_port == 0) |
| 227 | next_rtp_port = (pj_uint16_t)cfg->port; |
Benny Prijono | eebe9af | 2006-06-13 22:57:13 +0000 | [diff] [blame] | 228 | |
| 229 | for (i=0; i<2; ++i) |
| 230 | sock[i] = PJ_INVALID_SOCKET; |
| 231 | |
Benny Prijono | 0a5cad8 | 2006-09-26 13:21:02 +0000 | [diff] [blame] | 232 | 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 Prijono | eebe9af | 2006-06-13 22:57:13 +0000 | [diff] [blame] | 241 | |
| 242 | /* Loop retry to bind RTP and RTCP sockets. */ |
Benny Prijono | de47956 | 2007-03-15 10:23:55 +0000 | [diff] [blame] | 243 | for (i=0; i<RTP_RETRY; ++i, next_rtp_port += 2) { |
Benny Prijono | eebe9af | 2006-06-13 22:57:13 +0000 | [diff] [blame] | 244 | |
| 245 | /* Create and bind RTP socket. */ |
Benny Prijono | 8ab968f | 2007-07-20 08:08:30 +0000 | [diff] [blame] | 246 | status = pj_sock_socket(pj_AF_INET(), pj_SOCK_DGRAM(), 0, &sock[0]); |
Benny Prijono | eebe9af | 2006-06-13 22:57:13 +0000 | [diff] [blame] | 247 | if (status != PJ_SUCCESS) { |
| 248 | pjsua_perror(THIS_FILE, "socket() error", status); |
| 249 | return status; |
| 250 | } |
| 251 | |
Benny Prijono | 0a5cad8 | 2006-09-26 13:21:02 +0000 | [diff] [blame] | 252 | status = pj_sock_bind_in(sock[0], bound_addr.sin_addr.s_addr, |
Benny Prijono | de47956 | 2007-03-15 10:23:55 +0000 | [diff] [blame] | 253 | next_rtp_port); |
Benny Prijono | eebe9af | 2006-06-13 22:57:13 +0000 | [diff] [blame] | 254 | 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 Prijono | 8ab968f | 2007-07-20 08:08:30 +0000 | [diff] [blame] | 261 | status = pj_sock_socket(pj_AF_INET(), pj_SOCK_DGRAM(), 0, &sock[1]); |
Benny Prijono | eebe9af | 2006-06-13 22:57:13 +0000 | [diff] [blame] | 262 | if (status != PJ_SUCCESS) { |
| 263 | pjsua_perror(THIS_FILE, "socket() error", status); |
| 264 | pj_sock_close(sock[0]); |
| 265 | return status; |
| 266 | } |
| 267 | |
Benny Prijono | 0a5cad8 | 2006-09-26 13:21:02 +0000 | [diff] [blame] | 268 | status = pj_sock_bind_in(sock[1], bound_addr.sin_addr.s_addr, |
Benny Prijono | de47956 | 2007-03-15 10:23:55 +0000 | [diff] [blame] | 269 | (pj_uint16_t)(next_rtp_port+1)); |
Benny Prijono | eebe9af | 2006-06-13 22:57:13 +0000 | [diff] [blame] | 270 | 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 Prijono | c97608e | 2007-03-23 16:34:20 +0000 | [diff] [blame] | 283 | 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 Prijono | 14c2b86 | 2007-02-21 00:40:05 +0000 | [diff] [blame] | 291 | status=pjstun_get_mapped_addr(&pjsua_var.cp.factory, 2, sock, |
Benny Prijono | af09dc3 | 2007-04-22 12:48:30 +0000 | [diff] [blame] | 292 | &stun_srv, pj_ntohs(pjsua_var.stun_srv.ipv4.sin_port), |
| 293 | &stun_srv, pj_ntohs(pjsua_var.stun_srv.ipv4.sin_port), |
Benny Prijono | eebe9af | 2006-06-13 22:57:13 +0000 | [diff] [blame] | 294 | 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 Prijono | 0a5cad8 | 2006-09-26 13:21:02 +0000 | [diff] [blame] | 313 | } else if (cfg->public_addr.slen) { |
| 314 | |
| 315 | status = pj_sockaddr_in_init(&mapped_addr[0], &cfg->public_addr, |
Benny Prijono | de47956 | 2007-03-15 10:23:55 +0000 | [diff] [blame] | 316 | (pj_uint16_t)next_rtp_port); |
Benny Prijono | 0a5cad8 | 2006-09-26 13:21:02 +0000 | [diff] [blame] | 317 | if (status != PJ_SUCCESS) |
| 318 | goto on_error; |
| 319 | |
| 320 | status = pj_sockaddr_in_init(&mapped_addr[1], &cfg->public_addr, |
Benny Prijono | de47956 | 2007-03-15 10:23:55 +0000 | [diff] [blame] | 321 | (pj_uint16_t)(next_rtp_port+1)); |
Benny Prijono | 0a5cad8 | 2006-09-26 13:21:02 +0000 | [diff] [blame] | 322 | if (status != PJ_SUCCESS) |
| 323 | goto on_error; |
| 324 | |
| 325 | break; |
| 326 | |
Benny Prijono | eebe9af | 2006-06-13 22:57:13 +0000 | [diff] [blame] | 327 | } else { |
Benny Prijono | 594e4c5 | 2006-09-14 18:51:01 +0000 | [diff] [blame] | 328 | pj_in_addr addr; |
Benny Prijono | eebe9af | 2006-06-13 22:57:13 +0000 | [diff] [blame] | 329 | |
| 330 | /* Get local IP address. */ |
Benny Prijono | 594e4c5 | 2006-09-14 18:51:01 +0000 | [diff] [blame] | 331 | status = pj_gethostip(&addr); |
| 332 | if (status != PJ_SUCCESS) |
Benny Prijono | eebe9af | 2006-06-13 22:57:13 +0000 | [diff] [blame] | 333 | goto on_error; |
Benny Prijono | eebe9af | 2006-06-13 22:57:13 +0000 | [diff] [blame] | 334 | |
| 335 | for (i=0; i<2; ++i) |
Benny Prijono | 594e4c5 | 2006-09-14 18:51:01 +0000 | [diff] [blame] | 336 | mapped_addr[i].sin_addr = addr; |
Benny Prijono | eebe9af | 2006-06-13 22:57:13 +0000 | [diff] [blame] | 337 | |
Benny Prijono | de47956 | 2007-03-15 10:23:55 +0000 | [diff] [blame] | 338 | 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 Prijono | eebe9af | 2006-06-13 22:57:13 +0000 | [diff] [blame] | 340 | 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 Prijono | de47956 | 2007-03-15 10:23:55 +0000 | [diff] [blame] | 366 | next_rtp_port += 2; |
Benny Prijono | eebe9af | 2006-06-13 22:57:13 +0000 | [diff] [blame] | 367 | return PJ_SUCCESS; |
| 368 | |
| 369 | on_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 | */ |
| 381 | pj_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 Prijono | e909eac | 2006-07-27 22:04:56 +0000 | [diff] [blame] | 399 | if (pjsua_var.snd_port==NULL && pjsua_var.null_snd==NULL && |
| 400 | !pjsua_var.no_snd) |
| 401 | { |
Benny Prijono | eebe9af | 2006-06-13 22:57:13 +0000 | [diff] [blame] | 402 | status = pjsua_set_snd_dev(pjsua_var.cap_dev, pjsua_var.play_dev); |
| 403 | if (status != PJ_SUCCESS) { |
Benny Prijono | d639efa | 2007-04-05 22:45:06 +0000 | [diff] [blame] | 404 | pjsua_perror(THIS_FILE, "Error opening sound device", status); |
| 405 | return status; |
Benny Prijono | eebe9af | 2006-06-13 22:57:13 +0000 | [diff] [blame] | 406 | } |
| 407 | } |
| 408 | |
| 409 | return PJ_SUCCESS; |
| 410 | } |
| 411 | |
| 412 | |
| 413 | /* |
| 414 | * Destroy pjsua media subsystem. |
| 415 | */ |
| 416 | pj_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 Prijono | 0875ae8 | 2006-12-26 00:11:48 +0000 | [diff] [blame] | 449 | for (i=0; i<pjsua_var.ua_cfg.max_calls; ++i) { |
Benny Prijono | eebe9af | 2006-06-13 22:57:13 +0000 | [diff] [blame] | 450 | 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 Prijono | eebe9af | 2006-06-13 22:57:13 +0000 | [diff] [blame] | 479 | |
Benny Prijono | ad2e0ca | 2007-04-29 12:31:51 +0000 | [diff] [blame] | 480 | /* Deinitialize sound subsystem */ |
Benny Prijono | a41d66e | 2007-10-01 12:17:23 +0000 | [diff] [blame^] | 481 | // Not necessary, as pjmedia_snd_deinit() should have been called |
| 482 | // in pjmedia_endpt_destroy(). |
| 483 | //pjmedia_snd_deinit(); |
Benny Prijono | ad2e0ca | 2007-04-29 12:31:51 +0000 | [diff] [blame] | 484 | } |
Benny Prijono | af1bb1e | 2006-11-21 12:39:31 +0000 | [diff] [blame] | 485 | |
Benny Prijono | de47956 | 2007-03-15 10:23:55 +0000 | [diff] [blame] | 486 | /* Reset RTP port */ |
| 487 | next_rtp_port = 0; |
| 488 | |
Benny Prijono | eebe9af | 2006-06-13 22:57:13 +0000 | [diff] [blame] | 489 | return PJ_SUCCESS; |
| 490 | } |
| 491 | |
| 492 | |
Benny Prijono | c97608e | 2007-03-23 16:34:20 +0000 | [diff] [blame] | 493 | /* Create normal UDP media transports */ |
| 494 | static pj_status_t create_udp_media_transports(pjsua_transport_config *cfg) |
Benny Prijono | eebe9af | 2006-06-13 22:57:13 +0000 | [diff] [blame] | 495 | { |
Benny Prijono | eebe9af | 2006-06-13 22:57:13 +0000 | [diff] [blame] | 496 | unsigned i; |
Benny Prijono | 617c5bc | 2007-04-02 19:51:21 +0000 | [diff] [blame] | 497 | pjmedia_sock_info skinfo; |
Benny Prijono | eebe9af | 2006-06-13 22:57:13 +0000 | [diff] [blame] | 498 | pj_status_t status; |
| 499 | |
Benny Prijono | eebe9af | 2006-06-13 22:57:13 +0000 | [diff] [blame] | 500 | /* Create each media transport */ |
| 501 | for (i=0; i<pjsua_var.ua_cfg.max_calls; ++i) { |
| 502 | |
Benny Prijono | 617c5bc | 2007-04-02 19:51:21 +0000 | [diff] [blame] | 503 | status = create_rtp_rtcp_sock(cfg, &skinfo); |
Benny Prijono | eebe9af | 2006-06-13 22:57:13 +0000 | [diff] [blame] | 504 | 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 Prijono | 617c5bc | 2007-04-02 19:51:21 +0000 | [diff] [blame] | 510 | &skinfo, 0, |
Benny Prijono | eebe9af | 2006-06-13 22:57:13 +0000 | [diff] [blame] | 511 | &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 Prijono | 00cae61 | 2006-07-31 15:19:36 +0000 | [diff] [blame] | 517 | |
| 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 Prijono | eebe9af | 2006-06-13 22:57:13 +0000 | [diff] [blame] | 526 | } |
| 527 | |
Benny Prijono | c97608e | 2007-03-23 16:34:20 +0000 | [diff] [blame] | 528 | return PJ_SUCCESS; |
| 529 | |
| 530 | on_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 Prijono | 096c56c | 2007-09-15 08:30:16 +0000 | [diff] [blame] | 542 | /* This callback is called when ICE negotiation completes */ |
| 543 | static 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 Prijono | c97608e | 2007-03-23 16:34:20 +0000 | [diff] [blame] | 578 | /* Create ICE media transports (when ice is enabled) */ |
| 579 | static pj_status_t create_ice_media_transports(pjsua_transport_config *cfg) |
| 580 | { |
| 581 | unsigned i; |
Benny Prijono | b681a2f | 2007-03-25 18:44:51 +0000 | [diff] [blame] | 582 | pj_sockaddr_in addr; |
Benny Prijono | c97608e | 2007-03-23 16:34:20 +0000 | [diff] [blame] | 583 | pj_status_t status; |
| 584 | |
Benny Prijono | da9785b | 2007-04-02 20:43:06 +0000 | [diff] [blame] | 585 | /* 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 Prijono | b681a2f | 2007-03-25 18:44:51 +0000 | [diff] [blame] | 592 | pj_sockaddr_in_init(&addr, 0, (pj_uint16_t)cfg->port); |
| 593 | |
Benny Prijono | c97608e | 2007-03-23 16:34:20 +0000 | [diff] [blame] | 594 | /* Create each media transport */ |
| 595 | for (i=0; i<pjsua_var.ua_cfg.max_calls; ++i) { |
Benny Prijono | a6bd758 | 2007-03-28 15:49:48 +0000 | [diff] [blame] | 596 | pj_ice_strans_comp comp; |
Benny Prijono | 096c56c | 2007-09-15 08:30:16 +0000 | [diff] [blame] | 597 | pjmedia_ice_cb ice_cb; |
Benny Prijono | b681a2f | 2007-03-25 18:44:51 +0000 | [diff] [blame] | 598 | int next_port; |
Benny Prijono | dc1fe22 | 2007-06-26 10:13:13 +0000 | [diff] [blame] | 599 | #if PJMEDIA_ADVERTISE_RTCP |
| 600 | enum { COMP_CNT=2 }; |
| 601 | #else |
| 602 | enum { COMP_CNT=1 }; |
| 603 | #endif |
Benny Prijono | c97608e | 2007-03-23 16:34:20 +0000 | [diff] [blame] | 604 | |
Benny Prijono | 096c56c | 2007-09-15 08:30:16 +0000 | [diff] [blame] | 605 | pj_bzero(&ice_cb, sizeof(pjmedia_ice_cb)); |
| 606 | ice_cb.on_ice_complete = &on_ice_complete; |
| 607 | |
Benny Prijono | dc1fe22 | 2007-06-26 10:13:13 +0000 | [diff] [blame] | 608 | status = pjmedia_ice_create(pjsua_var.med_endpt, NULL, COMP_CNT, |
Benny Prijono | 096c56c | 2007-09-15 08:30:16 +0000 | [diff] [blame] | 609 | &pjsua_var.stun_cfg, &ice_cb, |
Benny Prijono | c97608e | 2007-03-23 16:34:20 +0000 | [diff] [blame] | 610 | &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 Prijono | 11da9bc | 2007-09-15 08:55:00 +0000 | [diff] [blame] | 617 | 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 Prijono | b681a2f | 2007-03-25 18:44:51 +0000 | [diff] [blame] | 625 | status = pjmedia_ice_start_init(pjsua_var.calls[i].med_tp, 0, &addr, |
| 626 | &pjsua_var.stun_srv.ipv4, NULL); |
Benny Prijono | c97608e | 2007-03-23 16:34:20 +0000 | [diff] [blame] | 627 | if (status != PJ_SUCCESS) { |
Benny Prijono | b681a2f | 2007-03-25 18:44:51 +0000 | [diff] [blame] | 628 | pjsua_perror(THIS_FILE, "Error starting ICE transport", |
Benny Prijono | c97608e | 2007-03-23 16:34:20 +0000 | [diff] [blame] | 629 | status); |
| 630 | goto on_error; |
| 631 | } |
| 632 | |
Benny Prijono | b681a2f | 2007-03-25 18:44:51 +0000 | [diff] [blame] | 633 | 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 Prijono | c97608e | 2007-03-23 16:34:20 +0000 | [diff] [blame] | 637 | } |
| 638 | |
| 639 | /* Wait until all ICE transports are ready */ |
| 640 | for (i=0; i<pjsua_var.ua_cfg.max_calls; ++i) { |
Benny Prijono | c97608e | 2007-03-23 16:34:20 +0000 | [diff] [blame] | 641 | |
| 642 | /* Wait until interface status is PJ_SUCCESS */ |
| 643 | for (;;) { |
Benny Prijono | b681a2f | 2007-03-25 18:44:51 +0000 | [diff] [blame] | 644 | status = pjmedia_ice_get_init_status(pjsua_var.calls[i].med_tp); |
Benny Prijono | c97608e | 2007-03-23 16:34:20 +0000 | [diff] [blame] | 645 | 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 Prijono | c97608e | 2007-03-23 16:34:20 +0000 | [diff] [blame] | 658 | } |
Benny Prijono | eebe9af | 2006-06-13 22:57:13 +0000 | [diff] [blame] | 659 | |
| 660 | return PJ_SUCCESS; |
| 661 | |
| 662 | on_error: |
| 663 | for (i=0; i<pjsua_var.ua_cfg.max_calls; ++i) { |
| 664 | if (pjsua_var.calls[i].med_tp != NULL) { |
Benny Prijono | c97608e | 2007-03-23 16:34:20 +0000 | [diff] [blame] | 665 | pjmedia_transport_close(pjsua_var.calls[i].med_tp); |
Benny Prijono | eebe9af | 2006-06-13 22:57:13 +0000 | [diff] [blame] | 666 | pjsua_var.calls[i].med_tp = NULL; |
| 667 | } |
| 668 | } |
| 669 | |
Benny Prijono | c97608e | 2007-03-23 16:34:20 +0000 | [diff] [blame] | 670 | 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 Prijono | 1f61a8f | 2007-08-16 10:11:44 +0000 | [diff] [blame] | 678 | PJ_DEF(pj_status_t) pjsua_media_transports_create( |
| 679 | const pjsua_transport_config *app_cfg) |
Benny Prijono | c97608e | 2007-03-23 16:34:20 +0000 | [diff] [blame] | 680 | { |
| 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 Prijono | eebe9af | 2006-06-13 22:57:13 +0000 | [diff] [blame] | 709 | PJSUA_UNLOCK(); |
| 710 | |
| 711 | return status; |
| 712 | } |
| 713 | |
| 714 | |
Benny Prijono | c97608e | 2007-03-23 16:34:20 +0000 | [diff] [blame] | 715 | pj_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 Prijono | a6bd758 | 2007-03-28 15:49:48 +0000 | [diff] [blame] | 721 | pj_ice_sess_role ice_role; |
Benny Prijono | c97608e | 2007-03-23 16:34:20 +0000 | [diff] [blame] | 722 | pj_status_t status; |
| 723 | |
Benny Prijono | a6bd758 | 2007-03-28 15:49:48 +0000 | [diff] [blame] | 724 | ice_role = (role==PJSIP_ROLE_UAC ? PJ_ICE_SESS_ROLE_CONTROLLING : |
| 725 | PJ_ICE_SESS_ROLE_CONTROLLED); |
Benny Prijono | c97608e | 2007-03-23 16:34:20 +0000 | [diff] [blame] | 726 | |
| 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 | |
| 738 | pj_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 Prijono | 617c5bc | 2007-04-02 19:51:21 +0000 | [diff] [blame] | 743 | pjmedia_sock_info skinfo; |
Benny Prijono | c97608e | 2007-03-23 16:34:20 +0000 | [diff] [blame] | 744 | pjsua_call *call = &pjsua_var.calls[call_id]; |
| 745 | pj_status_t status; |
| 746 | |
Benny Prijono | 55e8235 | 2007-05-10 20:49:08 +0000 | [diff] [blame] | 747 | /* 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 Prijono | 617c5bc | 2007-04-02 19:51:21 +0000 | [diff] [blame] | 754 | /* Get media socket info */ |
| 755 | pjmedia_transport_get_info(call->med_tp, &skinfo); |
| 756 | |
| 757 | /* Create SDP */ |
Benny Prijono | c97608e | 2007-03-23 16:34:20 +0000 | [diff] [blame] | 758 | status = pjmedia_endpt_create_sdp(pjsua_var.med_endpt, pool, 1, |
Benny Prijono | 617c5bc | 2007-04-02 19:51:21 +0000 | [diff] [blame] | 759 | &skinfo, &sdp); |
Benny Prijono | c97608e | 2007-03-23 16:34:20 +0000 | [diff] [blame] | 760 | 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 | |
| 772 | on_error: |
| 773 | pjsua_media_channel_deinit(call_id); |
| 774 | return status; |
| 775 | |
| 776 | } |
| 777 | |
| 778 | |
| 779 | static 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 | |
| 800 | pj_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 | */ |
| 817 | static 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 | |
| 831 | pj_status_t pjsua_media_channel_update(pjsua_call_id call_id, |
Benny Prijono | dbce2cf | 2007-03-28 16:24:00 +0000 | [diff] [blame] | 832 | const pjmedia_sdp_session *local_sdp, |
| 833 | const pjmedia_sdp_session *remote_sdp) |
Benny Prijono | c97608e | 2007-03-23 16:34:20 +0000 | [diff] [blame] | 834 | { |
| 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 Prijono | 667952e | 2007-04-02 19:27:54 +0000 | [diff] [blame] | 875 | /* Shutdown ICE session */ |
| 876 | if (pjsua_var.media_cfg.enable_ice) { |
| 877 | pjmedia_ice_stop_ice(call->med_tp); |
| 878 | } |
| 879 | |
Benny Prijono | c97608e | 2007-03-23 16:34:20 +0000 | [diff] [blame] | 880 | /* 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 Prijono | fd70c9b | 2007-03-27 11:00:38 +0000 | [diff] [blame] | 887 | remote_sdp, 0); |
Benny Prijono | c97608e | 2007-03-23 16:34:20 +0000 | [diff] [blame] | 888 | 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 Prijono | eebe9af | 2006-06-13 22:57:13 +0000 | [diff] [blame] | 1003 | /* |
| 1004 | * Get maxinum number of conference ports. |
| 1005 | */ |
| 1006 | PJ_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 | */ |
| 1015 | PJ_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 | */ |
| 1032 | PJ_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 | */ |
| 1042 | PJ_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 Prijono | 0498d90 | 2006-06-19 14:49:14 +0000 | [diff] [blame] | 1046 | unsigned i; |
Benny Prijono | eebe9af | 2006-06-13 22:57:13 +0000 | [diff] [blame] | 1047 | 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 Prijono | ac623b3 | 2006-07-03 15:19:31 +0000 | [diff] [blame] | 1053 | pj_bzero(info, sizeof(*info)); |
Benny Prijono | eebe9af | 2006-06-13 22:57:13 +0000 | [diff] [blame] | 1054 | 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 Prijono | c78c3a3 | 2006-06-16 15:54:43 +0000 | [diff] [blame] | 1062 | info->listener_cnt = cinfo.listener_cnt; |
| 1063 | for (i=0; i<cinfo.listener_cnt; ++i) { |
| 1064 | info->listeners[i] = cinfo.listener_slots[i]; |
Benny Prijono | eebe9af | 2006-06-13 22:57:13 +0000 | [diff] [blame] | 1065 | } |
| 1066 | |
| 1067 | return PJ_SUCCESS; |
| 1068 | } |
| 1069 | |
| 1070 | |
| 1071 | /* |
Benny Prijono | e909eac | 2006-07-27 22:04:56 +0000 | [diff] [blame] | 1072 | * Add arbitrary media port to PJSUA's conference bridge. |
| 1073 | */ |
| 1074 | PJ_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 | */ |
| 1094 | PJ_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 Prijono | eebe9af | 2006-06-13 22:57:13 +0000 | [diff] [blame] | 1101 | * Establish unidirectional media flow from souce to sink. |
| 1102 | */ |
| 1103 | PJ_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 | */ |
| 1113 | PJ_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 Prijono | 6dd967c | 2006-12-26 02:27:14 +0000 | [diff] [blame] | 1120 | /* |
| 1121 | * Adjust the signal level to be transmitted from the bridge to the |
| 1122 | * specified port by making it louder or quieter. |
| 1123 | */ |
| 1124 | PJ_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 | */ |
| 1135 | PJ_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 | */ |
| 1146 | PJ_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 Prijono | eebe9af | 2006-06-13 22:57:13 +0000 | [diff] [blame] | 1154 | /***************************************************************************** |
| 1155 | * File player. |
| 1156 | */ |
| 1157 | |
Benny Prijono | d5696da | 2007-07-17 16:25:45 +0000 | [diff] [blame] | 1158 | static 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 Prijono | 1f61a8f | 2007-08-16 10:11:44 +0000 | [diff] [blame] | 1165 | for (--p; p!=path && *p!='/' && *p!='\\'; ) --p; |
Benny Prijono | d5696da | 2007-07-17 16:25:45 +0000 | [diff] [blame] | 1166 | |
| 1167 | return (p==path) ? p : p+1; |
| 1168 | } |
| 1169 | |
| 1170 | |
Benny Prijono | eebe9af | 2006-06-13 22:57:13 +0000 | [diff] [blame] | 1171 | /* |
| 1172 | * Create a file player, and automatically connect this player to |
| 1173 | * the conference bridge. |
| 1174 | */ |
| 1175 | PJ_DEF(pj_status_t) pjsua_player_create( const pj_str_t *filename, |
| 1176 | unsigned options, |
Benny Prijono | eebe9af | 2006-06-13 22:57:13 +0000 | [diff] [blame] | 1177 | pjsua_player_id *p_id) |
| 1178 | { |
| 1179 | unsigned slot, file_id; |
Benny Prijono | a66c331 | 2007-01-21 23:12:40 +0000 | [diff] [blame] | 1180 | char path[PJ_MAXPATH]; |
Benny Prijono | d5696da | 2007-07-17 16:25:45 +0000 | [diff] [blame] | 1181 | pj_pool_t *pool; |
Benny Prijono | eebe9af | 2006-06-13 22:57:13 +0000 | [diff] [blame] | 1182 | 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 Prijono | d5696da | 2007-07-17 16:25:45 +0000 | [diff] [blame] | 1204 | |
| 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 Prijono | eebe9af | 2006-06-13 22:57:13 +0000 | [diff] [blame] | 1212 | pjsua_var.mconf_cfg.samples_per_frame * |
| 1213 | 1000 / pjsua_var.media_cfg.clock_rate, |
Benny Prijono | 00cae61 | 2006-07-31 15:19:36 +0000 | [diff] [blame] | 1214 | options, 0, &port); |
Benny Prijono | eebe9af | 2006-06-13 22:57:13 +0000 | [diff] [blame] | 1215 | if (status != PJ_SUCCESS) { |
| 1216 | PJSUA_UNLOCK(); |
| 1217 | pjsua_perror(THIS_FILE, "Unable to open file for playback", status); |
Benny Prijono | d5696da | 2007-07-17 16:25:45 +0000 | [diff] [blame] | 1218 | pj_pool_release(pool); |
Benny Prijono | eebe9af | 2006-06-13 22:57:13 +0000 | [diff] [blame] | 1219 | 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 Prijono | 32e4f49 | 2007-01-24 00:44:26 +0000 | [diff] [blame] | 1227 | pjsua_perror(THIS_FILE, "Unable to add file to conference bridge", |
| 1228 | status); |
Benny Prijono | d5696da | 2007-07-17 16:25:45 +0000 | [diff] [blame] | 1229 | pj_pool_release(pool); |
Benny Prijono | eebe9af | 2006-06-13 22:57:13 +0000 | [diff] [blame] | 1230 | return status; |
| 1231 | } |
| 1232 | |
Benny Prijono | a66c331 | 2007-01-21 23:12:40 +0000 | [diff] [blame] | 1233 | pjsua_var.player[file_id].type = 0; |
Benny Prijono | d5696da | 2007-07-17 16:25:45 +0000 | [diff] [blame] | 1234 | pjsua_var.player[file_id].pool = pool; |
Benny Prijono | eebe9af | 2006-06-13 22:57:13 +0000 | [diff] [blame] | 1235 | 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 Prijono | a66c331 | 2007-01-21 23:12:40 +0000 | [diff] [blame] | 1248 | * Create a file playlist media port, and automatically add the port |
| 1249 | * to the conference bridge. |
| 1250 | */ |
| 1251 | PJ_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 Prijono | d5696da | 2007-07-17 16:25:45 +0000 | [diff] [blame] | 1258 | pj_pool_t *pool; |
Benny Prijono | a66c331 | 2007-01-21 23:12:40 +0000 | [diff] [blame] | 1259 | 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 Prijono | d5696da | 2007-07-17 16:25:45 +0000 | [diff] [blame] | 1283 | 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 Prijono | a66c331 | 2007-01-21 23:12:40 +0000 | [diff] [blame] | 1290 | 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 Prijono | d5696da | 2007-07-17 16:25:45 +0000 | [diff] [blame] | 1295 | pj_pool_release(pool); |
Benny Prijono | a66c331 | 2007-01-21 23:12:40 +0000 | [diff] [blame] | 1296 | return status; |
| 1297 | } |
| 1298 | |
Benny Prijono | d5696da | 2007-07-17 16:25:45 +0000 | [diff] [blame] | 1299 | status = pjmedia_conf_add_port(pjsua_var.mconf, pool, |
Benny Prijono | a66c331 | 2007-01-21 23:12:40 +0000 | [diff] [blame] | 1300 | 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 Prijono | d5696da | 2007-07-17 16:25:45 +0000 | [diff] [blame] | 1305 | pj_pool_release(pool); |
Benny Prijono | a66c331 | 2007-01-21 23:12:40 +0000 | [diff] [blame] | 1306 | return status; |
| 1307 | } |
| 1308 | |
| 1309 | pjsua_var.player[file_id].type = 1; |
Benny Prijono | d5696da | 2007-07-17 16:25:45 +0000 | [diff] [blame] | 1310 | pjsua_var.player[file_id].pool = pool; |
Benny Prijono | a66c331 | 2007-01-21 23:12:40 +0000 | [diff] [blame] | 1311 | 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 Prijono | eebe9af | 2006-06-13 22:57:13 +0000 | [diff] [blame] | 1325 | * Get conference port ID associated with player. |
| 1326 | */ |
| 1327 | PJ_DEF(pjsua_conf_port_id) pjsua_player_get_conf_port(pjsua_player_id id) |
| 1328 | { |
Benny Prijono | a1e6968 | 2007-05-11 15:14:34 +0000 | [diff] [blame] | 1329 | PJ_ASSERT_RETURN(id>=0&&id<(int)PJ_ARRAY_SIZE(pjsua_var.player), PJ_EINVAL); |
Benny Prijono | eebe9af | 2006-06-13 22:57:13 +0000 | [diff] [blame] | 1330 | PJ_ASSERT_RETURN(pjsua_var.player[id].port != NULL, PJ_EINVAL); |
| 1331 | |
| 1332 | return pjsua_var.player[id].slot; |
| 1333 | } |
| 1334 | |
Benny Prijono | 469b152 | 2006-12-26 03:05:17 +0000 | [diff] [blame] | 1335 | /* |
| 1336 | * Get the media port for the player. |
| 1337 | */ |
| 1338 | PJ_DEF(pj_status_t) pjsua_player_get_port( pjsua_recorder_id id, |
| 1339 | pjmedia_port **p_port) |
| 1340 | { |
Benny Prijono | a1e6968 | 2007-05-11 15:14:34 +0000 | [diff] [blame] | 1341 | PJ_ASSERT_RETURN(id>=0&&id<(int)PJ_ARRAY_SIZE(pjsua_var.player), PJ_EINVAL); |
Benny Prijono | 469b152 | 2006-12-26 03:05:17 +0000 | [diff] [blame] | 1342 | 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 Prijono | eebe9af | 2006-06-13 22:57:13 +0000 | [diff] [blame] | 1349 | |
| 1350 | /* |
| 1351 | * Set playback position. |
| 1352 | */ |
| 1353 | PJ_DEF(pj_status_t) pjsua_player_set_pos( pjsua_player_id id, |
| 1354 | pj_uint32_t samples) |
| 1355 | { |
Benny Prijono | a1e6968 | 2007-05-11 15:14:34 +0000 | [diff] [blame] | 1356 | PJ_ASSERT_RETURN(id>=0&&id<(int)PJ_ARRAY_SIZE(pjsua_var.player), PJ_EINVAL); |
Benny Prijono | eebe9af | 2006-06-13 22:57:13 +0000 | [diff] [blame] | 1357 | PJ_ASSERT_RETURN(pjsua_var.player[id].port != NULL, PJ_EINVAL); |
Benny Prijono | a66c331 | 2007-01-21 23:12:40 +0000 | [diff] [blame] | 1358 | PJ_ASSERT_RETURN(pjsua_var.player[id].type == 0, PJ_EINVAL); |
Benny Prijono | eebe9af | 2006-06-13 22:57:13 +0000 | [diff] [blame] | 1359 | |
| 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 | */ |
| 1368 | PJ_DEF(pj_status_t) pjsua_player_destroy(pjsua_player_id id) |
| 1369 | { |
Benny Prijono | a1e6968 | 2007-05-11 15:14:34 +0000 | [diff] [blame] | 1370 | PJ_ASSERT_RETURN(id>=0&&id<(int)PJ_ARRAY_SIZE(pjsua_var.player), PJ_EINVAL); |
Benny Prijono | eebe9af | 2006-06-13 22:57:13 +0000 | [diff] [blame] | 1371 | 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 Prijono | d5696da | 2007-07-17 16:25:45 +0000 | [diff] [blame] | 1381 | pj_pool_release(pjsua_var.player[id].pool); |
| 1382 | pjsua_var.player[id].pool = NULL; |
Benny Prijono | eebe9af | 2006-06-13 22:57:13 +0000 | [diff] [blame] | 1383 | 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 | */ |
| 1400 | PJ_DEF(pj_status_t) pjsua_recorder_create( const pj_str_t *filename, |
Benny Prijono | 8f31052 | 2006-10-20 11:08:49 +0000 | [diff] [blame] | 1401 | unsigned enc_type, |
| 1402 | void *enc_param, |
Benny Prijono | eebe9af | 2006-06-13 22:57:13 +0000 | [diff] [blame] | 1403 | pj_ssize_t max_size, |
| 1404 | unsigned options, |
Benny Prijono | eebe9af | 2006-06-13 22:57:13 +0000 | [diff] [blame] | 1405 | pjsua_recorder_id *p_id) |
| 1406 | { |
Benny Prijono | b3cdb2b | 2006-10-19 15:49:47 +0000 | [diff] [blame] | 1407 | enum Format |
| 1408 | { |
| 1409 | FMT_UNKNOWN, |
| 1410 | FMT_WAV, |
| 1411 | FMT_MP3, |
| 1412 | }; |
Benny Prijono | eebe9af | 2006-06-13 22:57:13 +0000 | [diff] [blame] | 1413 | unsigned slot, file_id; |
Benny Prijono | a66c331 | 2007-01-21 23:12:40 +0000 | [diff] [blame] | 1414 | char path[PJ_MAXPATH]; |
Benny Prijono | b3cdb2b | 2006-10-19 15:49:47 +0000 | [diff] [blame] | 1415 | pj_str_t ext; |
Benny Prijono | 8f31052 | 2006-10-20 11:08:49 +0000 | [diff] [blame] | 1416 | int file_format; |
Benny Prijono | d5696da | 2007-07-17 16:25:45 +0000 | [diff] [blame] | 1417 | pj_pool_t *pool; |
Benny Prijono | eebe9af | 2006-06-13 22:57:13 +0000 | [diff] [blame] | 1418 | pjmedia_port *port; |
| 1419 | pj_status_t status; |
| 1420 | |
Benny Prijono | b3cdb2b | 2006-10-19 15:49:47 +0000 | [diff] [blame] | 1421 | /* Filename must present */ |
| 1422 | PJ_ASSERT_RETURN(filename != NULL, PJ_EINVAL); |
| 1423 | |
Benny Prijono | 00cae61 | 2006-07-31 15:19:36 +0000 | [diff] [blame] | 1424 | /* Don't support max_size at present */ |
Benny Prijono | b3cdb2b | 2006-10-19 15:49:47 +0000 | [diff] [blame] | 1425 | PJ_ASSERT_RETURN(max_size == 0 || max_size == -1, PJ_EINVAL); |
Benny Prijono | 00cae61 | 2006-07-31 15:19:36 +0000 | [diff] [blame] | 1426 | |
Benny Prijono | 8f31052 | 2006-10-20 11:08:49 +0000 | [diff] [blame] | 1427 | /* Don't support encoding type at present */ |
| 1428 | PJ_ASSERT_RETURN(enc_type == 0, PJ_EINVAL); |
Benny Prijono | 00cae61 | 2006-07-31 15:19:36 +0000 | [diff] [blame] | 1429 | |
Benny Prijono | eebe9af | 2006-06-13 22:57:13 +0000 | [diff] [blame] | 1430 | if (pjsua_var.rec_cnt >= PJ_ARRAY_SIZE(pjsua_var.recorder)) |
| 1431 | return PJ_ETOOMANY; |
| 1432 | |
Benny Prijono | b3cdb2b | 2006-10-19 15:49:47 +0000 | [diff] [blame] | 1433 | /* 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 Prijono | eebe9af | 2006-06-13 22:57:13 +0000 | [diff] [blame] | 1448 | 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 Prijono | b3cdb2b | 2006-10-19 15:49:47 +0000 | [diff] [blame] | 1464 | |
Benny Prijono | d5696da | 2007-07-17 16:25:45 +0000 | [diff] [blame] | 1465 | pool = pjsua_pool_create(get_basename(path, filename->slen), 1000, 1000); |
| 1466 | if (!pool) { |
| 1467 | PJSUA_UNLOCK(); |
| 1468 | return PJ_ENOMEM; |
| 1469 | } |
| 1470 | |
Benny Prijono | b3cdb2b | 2006-10-19 15:49:47 +0000 | [diff] [blame] | 1471 | if (file_format == FMT_WAV) { |
Benny Prijono | d5696da | 2007-07-17 16:25:45 +0000 | [diff] [blame] | 1472 | status = pjmedia_wav_writer_port_create(pool, path, |
Benny Prijono | b3cdb2b | 2006-10-19 15:49:47 +0000 | [diff] [blame] | 1473 | 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 Prijono | b3cdb2b | 2006-10-19 15:49:47 +0000 | [diff] [blame] | 1478 | } else { |
Benny Prijono | c95a0f0 | 2007-04-09 07:06:08 +0000 | [diff] [blame] | 1479 | PJ_UNUSED_ARG(enc_param); |
Benny Prijono | b3cdb2b | 2006-10-19 15:49:47 +0000 | [diff] [blame] | 1480 | port = NULL; |
| 1481 | status = PJ_ENOTSUP; |
| 1482 | } |
| 1483 | |
Benny Prijono | eebe9af | 2006-06-13 22:57:13 +0000 | [diff] [blame] | 1484 | if (status != PJ_SUCCESS) { |
| 1485 | PJSUA_UNLOCK(); |
| 1486 | pjsua_perror(THIS_FILE, "Unable to open file for recording", status); |
Benny Prijono | d5696da | 2007-07-17 16:25:45 +0000 | [diff] [blame] | 1487 | pj_pool_release(pool); |
Benny Prijono | eebe9af | 2006-06-13 22:57:13 +0000 | [diff] [blame] | 1488 | return status; |
| 1489 | } |
| 1490 | |
Benny Prijono | d5696da | 2007-07-17 16:25:45 +0000 | [diff] [blame] | 1491 | status = pjmedia_conf_add_port(pjsua_var.mconf, pool, |
Benny Prijono | eebe9af | 2006-06-13 22:57:13 +0000 | [diff] [blame] | 1492 | port, filename, &slot); |
| 1493 | if (status != PJ_SUCCESS) { |
| 1494 | pjmedia_port_destroy(port); |
| 1495 | PJSUA_UNLOCK(); |
Benny Prijono | d5696da | 2007-07-17 16:25:45 +0000 | [diff] [blame] | 1496 | pj_pool_release(pool); |
Benny Prijono | eebe9af | 2006-06-13 22:57:13 +0000 | [diff] [blame] | 1497 | return status; |
| 1498 | } |
| 1499 | |
| 1500 | pjsua_var.recorder[file_id].port = port; |
| 1501 | pjsua_var.recorder[file_id].slot = slot; |
Benny Prijono | d5696da | 2007-07-17 16:25:45 +0000 | [diff] [blame] | 1502 | pjsua_var.recorder[file_id].pool = pool; |
Benny Prijono | eebe9af | 2006-06-13 22:57:13 +0000 | [diff] [blame] | 1503 | |
| 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 | */ |
| 1516 | PJ_DEF(pjsua_conf_port_id) pjsua_recorder_get_conf_port(pjsua_recorder_id id) |
| 1517 | { |
Benny Prijono | a1e6968 | 2007-05-11 15:14:34 +0000 | [diff] [blame] | 1518 | PJ_ASSERT_RETURN(id>=0 && id<(int)PJ_ARRAY_SIZE(pjsua_var.recorder), |
| 1519 | PJ_EINVAL); |
Benny Prijono | eebe9af | 2006-06-13 22:57:13 +0000 | [diff] [blame] | 1520 | PJ_ASSERT_RETURN(pjsua_var.recorder[id].port != NULL, PJ_EINVAL); |
| 1521 | |
| 1522 | return pjsua_var.recorder[id].slot; |
| 1523 | } |
| 1524 | |
Benny Prijono | 469b152 | 2006-12-26 03:05:17 +0000 | [diff] [blame] | 1525 | /* |
| 1526 | * Get the media port for the recorder. |
| 1527 | */ |
| 1528 | PJ_DEF(pj_status_t) pjsua_recorder_get_port( pjsua_recorder_id id, |
| 1529 | pjmedia_port **p_port) |
| 1530 | { |
Benny Prijono | a1e6968 | 2007-05-11 15:14:34 +0000 | [diff] [blame] | 1531 | PJ_ASSERT_RETURN(id>=0 && id<(int)PJ_ARRAY_SIZE(pjsua_var.recorder), |
| 1532 | PJ_EINVAL); |
Benny Prijono | 469b152 | 2006-12-26 03:05:17 +0000 | [diff] [blame] | 1533 | 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 Prijono | eebe9af | 2006-06-13 22:57:13 +0000 | [diff] [blame] | 1539 | |
| 1540 | /* |
| 1541 | * Destroy recorder (this will complete recording). |
| 1542 | */ |
| 1543 | PJ_DEF(pj_status_t) pjsua_recorder_destroy(pjsua_recorder_id id) |
| 1544 | { |
Benny Prijono | a1e6968 | 2007-05-11 15:14:34 +0000 | [diff] [blame] | 1545 | PJ_ASSERT_RETURN(id>=0 && id<(int)PJ_ARRAY_SIZE(pjsua_var.recorder), |
| 1546 | PJ_EINVAL); |
Benny Prijono | eebe9af | 2006-06-13 22:57:13 +0000 | [diff] [blame] | 1547 | 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 Prijono | d5696da | 2007-07-17 16:25:45 +0000 | [diff] [blame] | 1557 | pj_pool_release(pjsua_var.recorder[id].pool); |
| 1558 | pjsua_var.recorder[id].pool = NULL; |
Benny Prijono | eebe9af | 2006-06-13 22:57:13 +0000 | [diff] [blame] | 1559 | 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 | */ |
| 1575 | PJ_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 */ |
| 1598 | static 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 | */ |
| 1628 | PJ_DEF(pj_status_t) pjsua_set_snd_dev( int capture_dev, |
| 1629 | int playback_dev) |
| 1630 | { |
| 1631 | pjmedia_port *conf_port; |
Benny Prijono | 6dd967c | 2006-12-26 02:27:14 +0000 | [diff] [blame] | 1632 | const pjmedia_snd_dev_info *play_info; |
Benny Prijono | 26056d8 | 2006-10-11 16:03:41 +0000 | [diff] [blame] | 1633 | unsigned clock_rates[] = { 0, 22050, 44100, 48000, 11025, 32000, 8000}; |
Benny Prijono | 658a1c5 | 2006-10-11 21:56:16 +0000 | [diff] [blame] | 1634 | unsigned selected_clock_rate = 0; |
Benny Prijono | 26056d8 | 2006-10-11 16:03:41 +0000 | [diff] [blame] | 1635 | unsigned i; |
Benny Prijono | c53c6d7 | 2006-11-27 09:54:03 +0000 | [diff] [blame] | 1636 | pjmedia_snd_stream *strm; |
| 1637 | pjmedia_snd_stream_info si; |
| 1638 | pj_str_t tmp; |
Benny Prijono | 26056d8 | 2006-10-11 16:03:41 +0000 | [diff] [blame] | 1639 | pj_status_t status = -1; |
Benny Prijono | eebe9af | 2006-06-13 22:57:13 +0000 | [diff] [blame] | 1640 | |
| 1641 | /* Close existing sound port */ |
| 1642 | close_snd_dev(); |
| 1643 | |
| 1644 | |
Benny Prijono | 26056d8 | 2006-10-11 16:03:41 +0000 | [diff] [blame] | 1645 | /* Set default clock rate */ |
| 1646 | clock_rates[0] = pjsua_var.media_cfg.clock_rate; |
Benny Prijono | eebe9af | 2006-06-13 22:57:13 +0000 | [diff] [blame] | 1647 | |
Benny Prijono | 26056d8 | 2006-10-11 16:03:41 +0000 | [diff] [blame] | 1648 | /* 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 Prijono | 658a1c5 | 2006-10-11 21:56:16 +0000 | [diff] [blame] | 1663 | if (status == PJ_SUCCESS) { |
| 1664 | selected_clock_rate = clock_rates[i]; |
Benny Prijono | 26056d8 | 2006-10-11 16:03:41 +0000 | [diff] [blame] | 1665 | break; |
Benny Prijono | 658a1c5 | 2006-10-11 21:56:16 +0000 | [diff] [blame] | 1666 | } |
Benny Prijono | 26056d8 | 2006-10-11 16:03:41 +0000 | [diff] [blame] | 1667 | |
| 1668 | pj_strerror(status, errmsg, sizeof(errmsg)); |
Benny Prijono | 658a1c5 | 2006-10-11 21:56:16 +0000 | [diff] [blame] | 1669 | PJ_LOG(4, (THIS_FILE, "..failed: %s", errmsg)); |
Benny Prijono | 26056d8 | 2006-10-11 16:03:41 +0000 | [diff] [blame] | 1670 | } |
| 1671 | |
Benny Prijono | eebe9af | 2006-06-13 22:57:13 +0000 | [diff] [blame] | 1672 | 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 Prijono | f20687a | 2006-08-04 18:27:19 +0000 | [diff] [blame] | 1681 | /* Set AEC */ |
Benny Prijono | 5da5043 | 2006-08-07 10:24:52 +0000 | [diff] [blame] | 1682 | 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 Prijono | eebe9af | 2006-06-13 22:57:13 +0000 | [diff] [blame] | 1685 | |
Benny Prijono | 658a1c5 | 2006-10-11 21:56:16 +0000 | [diff] [blame] | 1686 | /* 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 Prijono | 52a9391 | 2006-08-04 20:54:37 +0000 | [diff] [blame] | 1703 | /* 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 Prijono | eebe9af | 2006-06-13 22:57:13 +0000 | [diff] [blame] | 1714 | /* Save the device IDs */ |
| 1715 | pjsua_var.cap_dev = capture_dev; |
| 1716 | pjsua_var.play_dev = playback_dev; |
| 1717 | |
Benny Prijono | c53c6d7 | 2006-11-27 09:54:03 +0000 | [diff] [blame] | 1718 | /* 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 Prijono | eebe9af | 2006-06-13 22:57:13 +0000 | [diff] [blame] | 1726 | return PJ_SUCCESS; |
| 1727 | } |
| 1728 | |
| 1729 | |
| 1730 | /* |
Benny Prijono | ebdf877 | 2007-02-01 19:25:50 +0000 | [diff] [blame] | 1731 | * 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 | */ |
| 1735 | PJ_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 Prijono | eebe9af | 2006-06-13 22:57:13 +0000 | [diff] [blame] | 1750 | * Use null sound device. |
| 1751 | */ |
| 1752 | PJ_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 Prijono | e909eac | 2006-07-27 22:04:56 +0000 | [diff] [blame] | 1785 | |
| 1786 | /* |
| 1787 | * Use no device! |
| 1788 | */ |
| 1789 | PJ_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 Prijono | f20687a | 2006-08-04 18:27:19 +0000 | [diff] [blame] | 1799 | /* |
| 1800 | * Configure the AEC settings of the sound port. |
| 1801 | */ |
Benny Prijono | 5da5043 | 2006-08-07 10:24:52 +0000 | [diff] [blame] | 1802 | PJ_DEF(pj_status_t) pjsua_set_ec(unsigned tail_ms, unsigned options) |
Benny Prijono | f20687a | 2006-08-04 18:27:19 +0000 | [diff] [blame] | 1803 | { |
| 1804 | pjsua_var.media_cfg.ec_tail_len = tail_ms; |
| 1805 | |
| 1806 | if (pjsua_var.snd_port) |
Benny Prijono | 5da5043 | 2006-08-07 10:24:52 +0000 | [diff] [blame] | 1807 | return pjmedia_snd_port_set_ec( pjsua_var.snd_port, pjsua_var.pool, |
| 1808 | tail_ms, options); |
Benny Prijono | f20687a | 2006-08-04 18:27:19 +0000 | [diff] [blame] | 1809 | |
| 1810 | return PJ_SUCCESS; |
| 1811 | } |
| 1812 | |
| 1813 | |
| 1814 | /* |
| 1815 | * Get current AEC tail length. |
| 1816 | */ |
Benny Prijono | 22dfe59 | 2006-08-06 12:07:13 +0000 | [diff] [blame] | 1817 | PJ_DEF(pj_status_t) pjsua_get_ec_tail(unsigned *p_tail_ms) |
Benny Prijono | f20687a | 2006-08-04 18:27:19 +0000 | [diff] [blame] | 1818 | { |
| 1819 | *p_tail_ms = pjsua_var.media_cfg.ec_tail_len; |
| 1820 | return PJ_SUCCESS; |
| 1821 | } |
| 1822 | |
Benny Prijono | e909eac | 2006-07-27 22:04:56 +0000 | [diff] [blame] | 1823 | |
Benny Prijono | eebe9af | 2006-06-13 22:57:13 +0000 | [diff] [blame] | 1824 | /***************************************************************************** |
| 1825 | * Codecs. |
| 1826 | */ |
| 1827 | |
| 1828 | /* |
| 1829 | * Enum all supported codecs in the system. |
| 1830 | */ |
| 1831 | PJ_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 | */ |
| 1864 | PJ_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 | */ |
| 1879 | PJ_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 | */ |
| 1905 | PJ_DEF(pj_status_t) pjsua_codec_set_param( const pj_str_t *id, |
| 1906 | const pjmedia_codec_param *param) |
| 1907 | { |
Benny Prijono | 00cae61 | 2006-07-31 15:19:36 +0000 | [diff] [blame] | 1908 | PJ_UNUSED_ARG(id); |
| 1909 | PJ_UNUSED_ARG(param); |
Benny Prijono | eebe9af | 2006-06-13 22:57:13 +0000 | [diff] [blame] | 1910 | PJ_TODO(set_codec_param); |
| 1911 | return PJ_SUCCESS; |
| 1912 | } |