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 | eebe9af | 2006-06-13 22:57:13 +0000 | [diff] [blame] | 25 | #define DEFAULT_RTP_PORT 4000 |
| 26 | |
Benny Prijono | de47956 | 2007-03-15 10:23:55 +0000 | [diff] [blame] | 27 | /* Next RTP port to be used */ |
| 28 | static pj_uint16_t next_rtp_port; |
Benny Prijono | eebe9af | 2006-06-13 22:57:13 +0000 | [diff] [blame] | 29 | |
| 30 | /* Close existing sound device */ |
| 31 | static void close_snd_dev(void); |
| 32 | |
| 33 | |
| 34 | /** |
| 35 | * Init media subsystems. |
| 36 | */ |
| 37 | pj_status_t pjsua_media_subsys_init(const pjsua_media_config *cfg) |
| 38 | { |
Benny Prijono | ba5926a | 2007-05-02 11:29:37 +0000 | [diff] [blame] | 39 | pj_str_t codec_id = {NULL, 0}; |
Benny Prijono | 0498d90 | 2006-06-19 14:49:14 +0000 | [diff] [blame] | 40 | unsigned opt; |
Benny Prijono | eebe9af | 2006-06-13 22:57:13 +0000 | [diff] [blame] | 41 | pj_status_t status; |
| 42 | |
Benny Prijono | fc24e69 | 2007-01-27 18:31:51 +0000 | [diff] [blame] | 43 | /* To suppress warning about unused var when all codecs are disabled */ |
| 44 | PJ_UNUSED_ARG(codec_id); |
| 45 | |
Benny Prijono | eebe9af | 2006-06-13 22:57:13 +0000 | [diff] [blame] | 46 | /* Copy configuration */ |
| 47 | pj_memcpy(&pjsua_var.media_cfg, cfg, sizeof(*cfg)); |
| 48 | |
| 49 | /* Normalize configuration */ |
Benny Prijono | eebe9af | 2006-06-13 22:57:13 +0000 | [diff] [blame] | 50 | |
| 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 Prijono | 7ca96da | 2006-08-07 12:11:40 +0000 | [diff] [blame] | 78 | 0, |
Benny Prijono | 0498d90 | 2006-06-19 14:49:14 +0000 | [diff] [blame] | 79 | pjsua_var.media_cfg.quality, |
| 80 | pjsua_var.media_cfg.quality); |
Benny Prijono | eebe9af | 2006-06-13 22:57:13 +0000 | [diff] [blame] | 81 | if (status != PJ_SUCCESS) { |
| 82 | pjsua_perror(THIS_FILE, "Error initializing Speex codec", |
| 83 | status); |
| 84 | return status; |
| 85 | } |
Benny Prijono | 7ca96da | 2006-08-07 12:11:40 +0000 | [diff] [blame] | 86 | |
| 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 Prijono | eebe9af | 2006-06-13 22:57:13 +0000 | [diff] [blame] | 101 | #endif /* PJMEDIA_HAS_SPEEX_CODEC */ |
| 102 | |
Benny Prijono | 00cae61 | 2006-07-31 15:19:36 +0000 | [diff] [blame] | 103 | #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 Prijono | eebe9af | 2006-06-13 22:57:13 +0000 | [diff] [blame] | 114 | #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 Prijono | cf0b4b2 | 2007-10-06 17:31:09 +0000 | [diff] [blame] | 156 | pjsua_var.media_cfg.audio_frame_ptime / |
| 157 | 1000; |
Benny Prijono | eebe9af | 2006-06-13 22:57:13 +0000 | [diff] [blame] | 158 | pjsua_var.mconf_cfg.channel_count = 1; |
| 159 | pjsua_var.mconf_cfg.bits_per_sample = 16; |
| 160 | |
Benny Prijono | 0498d90 | 2006-06-19 14:49:14 +0000 | [diff] [blame] | 161 | /* Init options for conference bridge. */ |
| 162 | opt = PJMEDIA_CONF_NO_DEVICE; |
| 163 | if (pjsua_var.media_cfg.quality >= 3 && |
Benny Prijono | 7ca96da | 2006-08-07 12:11:40 +0000 | [diff] [blame] | 164 | pjsua_var.media_cfg.quality <= 4) |
Benny Prijono | 0498d90 | 2006-06-19 14:49:14 +0000 | [diff] [blame] | 165 | { |
| 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 Prijono | eebe9af | 2006-06-13 22:57:13 +0000 | [diff] [blame] | 173 | /* 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 Prijono | 0498d90 | 2006-06-19 14:49:14 +0000 | [diff] [blame] | 180 | opt, &pjsua_var.mconf); |
Benny Prijono | eebe9af | 2006-06-13 22:57:13 +0000 | [diff] [blame] | 181 | 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 Prijono | 6ba8c54 | 2007-10-16 01:34:14 +0000 | [diff] [blame^] | 197 | /* Perform NAT detection */ |
| 198 | pjsua_detect_nat_type(); |
| 199 | |
Benny Prijono | eebe9af | 2006-06-13 22:57:13 +0000 | [diff] [blame] | 200 | return PJ_SUCCESS; |
| 201 | } |
| 202 | |
| 203 | |
| 204 | /* |
| 205 | * Create RTP and RTCP socket pair, and possibly resolve their public |
| 206 | * address via STUN. |
| 207 | */ |
| 208 | static 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 Prijono | 0a5cad8 | 2006-09-26 13:21:02 +0000 | [diff] [blame] | 215 | pj_sockaddr_in bound_addr; |
Benny Prijono | eebe9af | 2006-06-13 22:57:13 +0000 | [diff] [blame] | 216 | pj_sockaddr_in mapped_addr[2]; |
| 217 | pj_status_t status = PJ_SUCCESS; |
| 218 | pj_sock_t sock[2]; |
| 219 | |
Benny Prijono | c97608e | 2007-03-23 16:34:20 +0000 | [diff] [blame] | 220 | /* 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 Prijono | de47956 | 2007-03-15 10:23:55 +0000 | [diff] [blame] | 228 | if (next_rtp_port == 0) |
| 229 | next_rtp_port = (pj_uint16_t)cfg->port; |
Benny Prijono | eebe9af | 2006-06-13 22:57:13 +0000 | [diff] [blame] | 230 | |
| 231 | for (i=0; i<2; ++i) |
| 232 | sock[i] = PJ_INVALID_SOCKET; |
| 233 | |
Benny Prijono | 0a5cad8 | 2006-09-26 13:21:02 +0000 | [diff] [blame] | 234 | 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 Prijono | eebe9af | 2006-06-13 22:57:13 +0000 | [diff] [blame] | 243 | |
| 244 | /* Loop retry to bind RTP and RTCP sockets. */ |
Benny Prijono | de47956 | 2007-03-15 10:23:55 +0000 | [diff] [blame] | 245 | for (i=0; i<RTP_RETRY; ++i, next_rtp_port += 2) { |
Benny Prijono | eebe9af | 2006-06-13 22:57:13 +0000 | [diff] [blame] | 246 | |
| 247 | /* Create and bind RTP socket. */ |
Benny Prijono | 8ab968f | 2007-07-20 08:08:30 +0000 | [diff] [blame] | 248 | 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] | 249 | if (status != PJ_SUCCESS) { |
| 250 | pjsua_perror(THIS_FILE, "socket() error", status); |
| 251 | return status; |
| 252 | } |
| 253 | |
Benny Prijono | 0a5cad8 | 2006-09-26 13:21:02 +0000 | [diff] [blame] | 254 | 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] | 255 | next_rtp_port); |
Benny Prijono | eebe9af | 2006-06-13 22:57:13 +0000 | [diff] [blame] | 256 | 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 Prijono | 8ab968f | 2007-07-20 08:08:30 +0000 | [diff] [blame] | 263 | 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] | 264 | if (status != PJ_SUCCESS) { |
| 265 | pjsua_perror(THIS_FILE, "socket() error", status); |
| 266 | pj_sock_close(sock[0]); |
| 267 | return status; |
| 268 | } |
| 269 | |
Benny Prijono | 0a5cad8 | 2006-09-26 13:21:02 +0000 | [diff] [blame] | 270 | 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] | 271 | (pj_uint16_t)(next_rtp_port+1)); |
Benny Prijono | eebe9af | 2006-06-13 22:57:13 +0000 | [diff] [blame] | 272 | 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 Prijono | c97608e | 2007-03-23 16:34:20 +0000 | [diff] [blame] | 285 | 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 Prijono | 14c2b86 | 2007-02-21 00:40:05 +0000 | [diff] [blame] | 293 | status=pjstun_get_mapped_addr(&pjsua_var.cp.factory, 2, sock, |
Benny Prijono | af09dc3 | 2007-04-22 12:48:30 +0000 | [diff] [blame] | 294 | &stun_srv, pj_ntohs(pjsua_var.stun_srv.ipv4.sin_port), |
| 295 | &stun_srv, pj_ntohs(pjsua_var.stun_srv.ipv4.sin_port), |
Benny Prijono | eebe9af | 2006-06-13 22:57:13 +0000 | [diff] [blame] | 296 | 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 Prijono | 0a5cad8 | 2006-09-26 13:21:02 +0000 | [diff] [blame] | 315 | } else if (cfg->public_addr.slen) { |
| 316 | |
| 317 | status = pj_sockaddr_in_init(&mapped_addr[0], &cfg->public_addr, |
Benny Prijono | de47956 | 2007-03-15 10:23:55 +0000 | [diff] [blame] | 318 | (pj_uint16_t)next_rtp_port); |
Benny Prijono | 0a5cad8 | 2006-09-26 13:21:02 +0000 | [diff] [blame] | 319 | if (status != PJ_SUCCESS) |
| 320 | goto on_error; |
| 321 | |
| 322 | status = pj_sockaddr_in_init(&mapped_addr[1], &cfg->public_addr, |
Benny Prijono | de47956 | 2007-03-15 10:23:55 +0000 | [diff] [blame] | 323 | (pj_uint16_t)(next_rtp_port+1)); |
Benny Prijono | 0a5cad8 | 2006-09-26 13:21:02 +0000 | [diff] [blame] | 324 | if (status != PJ_SUCCESS) |
| 325 | goto on_error; |
| 326 | |
| 327 | break; |
| 328 | |
Benny Prijono | eebe9af | 2006-06-13 22:57:13 +0000 | [diff] [blame] | 329 | } else { |
Benny Prijono | 594e4c5 | 2006-09-14 18:51:01 +0000 | [diff] [blame] | 330 | pj_in_addr addr; |
Benny Prijono | eebe9af | 2006-06-13 22:57:13 +0000 | [diff] [blame] | 331 | |
| 332 | /* Get local IP address. */ |
Benny Prijono | 594e4c5 | 2006-09-14 18:51:01 +0000 | [diff] [blame] | 333 | status = pj_gethostip(&addr); |
| 334 | if (status != PJ_SUCCESS) |
Benny Prijono | eebe9af | 2006-06-13 22:57:13 +0000 | [diff] [blame] | 335 | goto on_error; |
Benny Prijono | eebe9af | 2006-06-13 22:57:13 +0000 | [diff] [blame] | 336 | |
| 337 | for (i=0; i<2; ++i) |
Benny Prijono | 594e4c5 | 2006-09-14 18:51:01 +0000 | [diff] [blame] | 338 | mapped_addr[i].sin_addr = addr; |
Benny Prijono | eebe9af | 2006-06-13 22:57:13 +0000 | [diff] [blame] | 339 | |
Benny Prijono | de47956 | 2007-03-15 10:23:55 +0000 | [diff] [blame] | 340 | 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 Prijono | eebe9af | 2006-06-13 22:57:13 +0000 | [diff] [blame] | 342 | 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 Prijono | de47956 | 2007-03-15 10:23:55 +0000 | [diff] [blame] | 368 | next_rtp_port += 2; |
Benny Prijono | eebe9af | 2006-06-13 22:57:13 +0000 | [diff] [blame] | 369 | return PJ_SUCCESS; |
| 370 | |
| 371 | on_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 | */ |
| 383 | pj_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 Prijono | e909eac | 2006-07-27 22:04:56 +0000 | [diff] [blame] | 401 | if (pjsua_var.snd_port==NULL && pjsua_var.null_snd==NULL && |
| 402 | !pjsua_var.no_snd) |
| 403 | { |
Benny Prijono | eebe9af | 2006-06-13 22:57:13 +0000 | [diff] [blame] | 404 | status = pjsua_set_snd_dev(pjsua_var.cap_dev, pjsua_var.play_dev); |
| 405 | if (status != PJ_SUCCESS) { |
Benny Prijono | d639efa | 2007-04-05 22:45:06 +0000 | [diff] [blame] | 406 | pjsua_perror(THIS_FILE, "Error opening sound device", status); |
| 407 | return status; |
Benny Prijono | eebe9af | 2006-06-13 22:57:13 +0000 | [diff] [blame] | 408 | } |
| 409 | } |
| 410 | |
| 411 | return PJ_SUCCESS; |
| 412 | } |
| 413 | |
| 414 | |
| 415 | /* |
| 416 | * Destroy pjsua media subsystem. |
| 417 | */ |
| 418 | pj_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 Prijono | 0875ae8 | 2006-12-26 00:11:48 +0000 | [diff] [blame] | 451 | for (i=0; i<pjsua_var.ua_cfg.max_calls; ++i) { |
Benny Prijono | eebe9af | 2006-06-13 22:57:13 +0000 | [diff] [blame] | 452 | 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 Prijono | eebe9af | 2006-06-13 22:57:13 +0000 | [diff] [blame] | 481 | |
Benny Prijono | ad2e0ca | 2007-04-29 12:31:51 +0000 | [diff] [blame] | 482 | /* Deinitialize sound subsystem */ |
Benny Prijono | a41d66e | 2007-10-01 12:17:23 +0000 | [diff] [blame] | 483 | // Not necessary, as pjmedia_snd_deinit() should have been called |
| 484 | // in pjmedia_endpt_destroy(). |
| 485 | //pjmedia_snd_deinit(); |
Benny Prijono | ad2e0ca | 2007-04-29 12:31:51 +0000 | [diff] [blame] | 486 | } |
Benny Prijono | af1bb1e | 2006-11-21 12:39:31 +0000 | [diff] [blame] | 487 | |
Benny Prijono | de47956 | 2007-03-15 10:23:55 +0000 | [diff] [blame] | 488 | /* Reset RTP port */ |
| 489 | next_rtp_port = 0; |
| 490 | |
Benny Prijono | eebe9af | 2006-06-13 22:57:13 +0000 | [diff] [blame] | 491 | return PJ_SUCCESS; |
| 492 | } |
| 493 | |
| 494 | |
Benny Prijono | c97608e | 2007-03-23 16:34:20 +0000 | [diff] [blame] | 495 | /* Create normal UDP media transports */ |
| 496 | static pj_status_t create_udp_media_transports(pjsua_transport_config *cfg) |
Benny Prijono | eebe9af | 2006-06-13 22:57:13 +0000 | [diff] [blame] | 497 | { |
Benny Prijono | eebe9af | 2006-06-13 22:57:13 +0000 | [diff] [blame] | 498 | unsigned i; |
Benny Prijono | 617c5bc | 2007-04-02 19:51:21 +0000 | [diff] [blame] | 499 | pjmedia_sock_info skinfo; |
Benny Prijono | eebe9af | 2006-06-13 22:57:13 +0000 | [diff] [blame] | 500 | pj_status_t status; |
| 501 | |
Benny Prijono | eebe9af | 2006-06-13 22:57:13 +0000 | [diff] [blame] | 502 | /* Create each media transport */ |
| 503 | for (i=0; i<pjsua_var.ua_cfg.max_calls; ++i) { |
| 504 | |
Benny Prijono | 617c5bc | 2007-04-02 19:51:21 +0000 | [diff] [blame] | 505 | status = create_rtp_rtcp_sock(cfg, &skinfo); |
Benny Prijono | eebe9af | 2006-06-13 22:57:13 +0000 | [diff] [blame] | 506 | 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 Prijono | 617c5bc | 2007-04-02 19:51:21 +0000 | [diff] [blame] | 512 | &skinfo, 0, |
Benny Prijono | eebe9af | 2006-06-13 22:57:13 +0000 | [diff] [blame] | 513 | &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 Prijono | 00cae61 | 2006-07-31 15:19:36 +0000 | [diff] [blame] | 519 | |
| 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 Prijono | eebe9af | 2006-06-13 22:57:13 +0000 | [diff] [blame] | 528 | } |
| 529 | |
Benny Prijono | c97608e | 2007-03-23 16:34:20 +0000 | [diff] [blame] | 530 | return PJ_SUCCESS; |
| 531 | |
| 532 | on_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 Prijono | 096c56c | 2007-09-15 08:30:16 +0000 | [diff] [blame] | 544 | /* This callback is called when ICE negotiation completes */ |
| 545 | static 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 Prijono | c97608e | 2007-03-23 16:34:20 +0000 | [diff] [blame] | 580 | /* Create ICE media transports (when ice is enabled) */ |
| 581 | static pj_status_t create_ice_media_transports(pjsua_transport_config *cfg) |
| 582 | { |
| 583 | unsigned i; |
Benny Prijono | b681a2f | 2007-03-25 18:44:51 +0000 | [diff] [blame] | 584 | pj_sockaddr_in addr; |
Benny Prijono | c97608e | 2007-03-23 16:34:20 +0000 | [diff] [blame] | 585 | pj_status_t status; |
| 586 | |
Benny Prijono | da9785b | 2007-04-02 20:43:06 +0000 | [diff] [blame] | 587 | /* 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 Prijono | b681a2f | 2007-03-25 18:44:51 +0000 | [diff] [blame] | 594 | pj_sockaddr_in_init(&addr, 0, (pj_uint16_t)cfg->port); |
| 595 | |
Benny Prijono | c97608e | 2007-03-23 16:34:20 +0000 | [diff] [blame] | 596 | /* Create each media transport */ |
| 597 | for (i=0; i<pjsua_var.ua_cfg.max_calls; ++i) { |
Benny Prijono | a6bd758 | 2007-03-28 15:49:48 +0000 | [diff] [blame] | 598 | pj_ice_strans_comp comp; |
Benny Prijono | 096c56c | 2007-09-15 08:30:16 +0000 | [diff] [blame] | 599 | pjmedia_ice_cb ice_cb; |
Benny Prijono | b681a2f | 2007-03-25 18:44:51 +0000 | [diff] [blame] | 600 | int next_port; |
Benny Prijono | dc1fe22 | 2007-06-26 10:13:13 +0000 | [diff] [blame] | 601 | #if PJMEDIA_ADVERTISE_RTCP |
| 602 | enum { COMP_CNT=2 }; |
| 603 | #else |
| 604 | enum { COMP_CNT=1 }; |
| 605 | #endif |
Benny Prijono | c97608e | 2007-03-23 16:34:20 +0000 | [diff] [blame] | 606 | |
Benny Prijono | 096c56c | 2007-09-15 08:30:16 +0000 | [diff] [blame] | 607 | pj_bzero(&ice_cb, sizeof(pjmedia_ice_cb)); |
| 608 | ice_cb.on_ice_complete = &on_ice_complete; |
| 609 | |
Benny Prijono | dc1fe22 | 2007-06-26 10:13:13 +0000 | [diff] [blame] | 610 | status = pjmedia_ice_create(pjsua_var.med_endpt, NULL, COMP_CNT, |
Benny Prijono | 096c56c | 2007-09-15 08:30:16 +0000 | [diff] [blame] | 611 | &pjsua_var.stun_cfg, &ice_cb, |
Benny Prijono | c97608e | 2007-03-23 16:34:20 +0000 | [diff] [blame] | 612 | &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 Prijono | 11da9bc | 2007-09-15 08:55:00 +0000 | [diff] [blame] | 619 | 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 Prijono | b681a2f | 2007-03-25 18:44:51 +0000 | [diff] [blame] | 627 | status = pjmedia_ice_start_init(pjsua_var.calls[i].med_tp, 0, &addr, |
| 628 | &pjsua_var.stun_srv.ipv4, NULL); |
Benny Prijono | c97608e | 2007-03-23 16:34:20 +0000 | [diff] [blame] | 629 | if (status != PJ_SUCCESS) { |
Benny Prijono | b681a2f | 2007-03-25 18:44:51 +0000 | [diff] [blame] | 630 | pjsua_perror(THIS_FILE, "Error starting ICE transport", |
Benny Prijono | c97608e | 2007-03-23 16:34:20 +0000 | [diff] [blame] | 631 | status); |
| 632 | goto on_error; |
| 633 | } |
| 634 | |
Benny Prijono | b681a2f | 2007-03-25 18:44:51 +0000 | [diff] [blame] | 635 | 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 Prijono | c97608e | 2007-03-23 16:34:20 +0000 | [diff] [blame] | 639 | } |
| 640 | |
| 641 | /* Wait until all ICE transports are ready */ |
| 642 | for (i=0; i<pjsua_var.ua_cfg.max_calls; ++i) { |
Benny Prijono | c97608e | 2007-03-23 16:34:20 +0000 | [diff] [blame] | 643 | |
| 644 | /* Wait until interface status is PJ_SUCCESS */ |
| 645 | for (;;) { |
Benny Prijono | b681a2f | 2007-03-25 18:44:51 +0000 | [diff] [blame] | 646 | status = pjmedia_ice_get_init_status(pjsua_var.calls[i].med_tp); |
Benny Prijono | c97608e | 2007-03-23 16:34:20 +0000 | [diff] [blame] | 647 | 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 Prijono | c97608e | 2007-03-23 16:34:20 +0000 | [diff] [blame] | 660 | } |
Benny Prijono | eebe9af | 2006-06-13 22:57:13 +0000 | [diff] [blame] | 661 | |
| 662 | return PJ_SUCCESS; |
| 663 | |
| 664 | on_error: |
| 665 | for (i=0; i<pjsua_var.ua_cfg.max_calls; ++i) { |
| 666 | if (pjsua_var.calls[i].med_tp != NULL) { |
Benny Prijono | c97608e | 2007-03-23 16:34:20 +0000 | [diff] [blame] | 667 | pjmedia_transport_close(pjsua_var.calls[i].med_tp); |
Benny Prijono | eebe9af | 2006-06-13 22:57:13 +0000 | [diff] [blame] | 668 | pjsua_var.calls[i].med_tp = NULL; |
| 669 | } |
| 670 | } |
| 671 | |
Benny Prijono | c97608e | 2007-03-23 16:34:20 +0000 | [diff] [blame] | 672 | 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 Prijono | 1f61a8f | 2007-08-16 10:11:44 +0000 | [diff] [blame] | 680 | PJ_DEF(pj_status_t) pjsua_media_transports_create( |
| 681 | const pjsua_transport_config *app_cfg) |
Benny Prijono | c97608e | 2007-03-23 16:34:20 +0000 | [diff] [blame] | 682 | { |
| 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 Prijono | eebe9af | 2006-06-13 22:57:13 +0000 | [diff] [blame] | 711 | PJSUA_UNLOCK(); |
| 712 | |
| 713 | return status; |
| 714 | } |
| 715 | |
| 716 | |
Benny Prijono | c97608e | 2007-03-23 16:34:20 +0000 | [diff] [blame] | 717 | pj_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 Prijono | a6bd758 | 2007-03-28 15:49:48 +0000 | [diff] [blame] | 723 | pj_ice_sess_role ice_role; |
Benny Prijono | c97608e | 2007-03-23 16:34:20 +0000 | [diff] [blame] | 724 | pj_status_t status; |
| 725 | |
Benny Prijono | a6bd758 | 2007-03-28 15:49:48 +0000 | [diff] [blame] | 726 | ice_role = (role==PJSIP_ROLE_UAC ? PJ_ICE_SESS_ROLE_CONTROLLING : |
| 727 | PJ_ICE_SESS_ROLE_CONTROLLED); |
Benny Prijono | c97608e | 2007-03-23 16:34:20 +0000 | [diff] [blame] | 728 | |
| 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 | |
| 740 | pj_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 Prijono | 617c5bc | 2007-04-02 19:51:21 +0000 | [diff] [blame] | 745 | pjmedia_sock_info skinfo; |
Benny Prijono | c97608e | 2007-03-23 16:34:20 +0000 | [diff] [blame] | 746 | pjsua_call *call = &pjsua_var.calls[call_id]; |
| 747 | pj_status_t status; |
| 748 | |
Benny Prijono | 55e8235 | 2007-05-10 20:49:08 +0000 | [diff] [blame] | 749 | /* 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 Prijono | 617c5bc | 2007-04-02 19:51:21 +0000 | [diff] [blame] | 756 | /* Get media socket info */ |
| 757 | pjmedia_transport_get_info(call->med_tp, &skinfo); |
| 758 | |
| 759 | /* Create SDP */ |
Benny Prijono | c97608e | 2007-03-23 16:34:20 +0000 | [diff] [blame] | 760 | status = pjmedia_endpt_create_sdp(pjsua_var.med_endpt, pool, 1, |
Benny Prijono | 617c5bc | 2007-04-02 19:51:21 +0000 | [diff] [blame] | 761 | &skinfo, &sdp); |
Benny Prijono | c97608e | 2007-03-23 16:34:20 +0000 | [diff] [blame] | 762 | if (status != PJ_SUCCESS) |
| 763 | goto on_error; |
| 764 | |
Benny Prijono | 6ba8c54 | 2007-10-16 01:34:14 +0000 | [diff] [blame^] | 765 | /* 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 Prijono | c97608e | 2007-03-23 16:34:20 +0000 | [diff] [blame] | 789 | 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 | |
| 798 | on_error: |
| 799 | pjsua_media_channel_deinit(call_id); |
| 800 | return status; |
| 801 | |
| 802 | } |
| 803 | |
| 804 | |
| 805 | static 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 | |
| 826 | pj_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 | */ |
| 843 | static 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 | |
| 857 | pj_status_t pjsua_media_channel_update(pjsua_call_id call_id, |
Benny Prijono | dbce2cf | 2007-03-28 16:24:00 +0000 | [diff] [blame] | 858 | const pjmedia_sdp_session *local_sdp, |
| 859 | const pjmedia_sdp_session *remote_sdp) |
Benny Prijono | c97608e | 2007-03-23 16:34:20 +0000 | [diff] [blame] | 860 | { |
| 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 Prijono | 667952e | 2007-04-02 19:27:54 +0000 | [diff] [blame] | 901 | /* Shutdown ICE session */ |
| 902 | if (pjsua_var.media_cfg.enable_ice) { |
| 903 | pjmedia_ice_stop_ice(call->med_tp); |
| 904 | } |
| 905 | |
Benny Prijono | c97608e | 2007-03-23 16:34:20 +0000 | [diff] [blame] | 906 | /* 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 Prijono | fd70c9b | 2007-03-27 11:00:38 +0000 | [diff] [blame] | 913 | remote_sdp, 0); |
Benny Prijono | c97608e | 2007-03-23 16:34:20 +0000 | [diff] [blame] | 914 | 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 Prijono | eebe9af | 2006-06-13 22:57:13 +0000 | [diff] [blame] | 1029 | /* |
| 1030 | * Get maxinum number of conference ports. |
| 1031 | */ |
| 1032 | PJ_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 | */ |
| 1041 | PJ_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 | */ |
| 1058 | PJ_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 | */ |
| 1068 | PJ_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 Prijono | 0498d90 | 2006-06-19 14:49:14 +0000 | [diff] [blame] | 1072 | unsigned i; |
Benny Prijono | eebe9af | 2006-06-13 22:57:13 +0000 | [diff] [blame] | 1073 | 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 Prijono | ac623b3 | 2006-07-03 15:19:31 +0000 | [diff] [blame] | 1079 | pj_bzero(info, sizeof(*info)); |
Benny Prijono | eebe9af | 2006-06-13 22:57:13 +0000 | [diff] [blame] | 1080 | 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 Prijono | c78c3a3 | 2006-06-16 15:54:43 +0000 | [diff] [blame] | 1088 | info->listener_cnt = cinfo.listener_cnt; |
| 1089 | for (i=0; i<cinfo.listener_cnt; ++i) { |
| 1090 | info->listeners[i] = cinfo.listener_slots[i]; |
Benny Prijono | eebe9af | 2006-06-13 22:57:13 +0000 | [diff] [blame] | 1091 | } |
| 1092 | |
| 1093 | return PJ_SUCCESS; |
| 1094 | } |
| 1095 | |
| 1096 | |
| 1097 | /* |
Benny Prijono | e909eac | 2006-07-27 22:04:56 +0000 | [diff] [blame] | 1098 | * Add arbitrary media port to PJSUA's conference bridge. |
| 1099 | */ |
| 1100 | PJ_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 | */ |
| 1120 | PJ_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 Prijono | eebe9af | 2006-06-13 22:57:13 +0000 | [diff] [blame] | 1127 | * Establish unidirectional media flow from souce to sink. |
| 1128 | */ |
| 1129 | PJ_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 | */ |
| 1139 | PJ_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 Prijono | 6dd967c | 2006-12-26 02:27:14 +0000 | [diff] [blame] | 1146 | /* |
| 1147 | * Adjust the signal level to be transmitted from the bridge to the |
| 1148 | * specified port by making it louder or quieter. |
| 1149 | */ |
| 1150 | PJ_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 | */ |
| 1161 | PJ_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 | */ |
| 1172 | PJ_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 Prijono | eebe9af | 2006-06-13 22:57:13 +0000 | [diff] [blame] | 1180 | /***************************************************************************** |
| 1181 | * File player. |
| 1182 | */ |
| 1183 | |
Benny Prijono | d5696da | 2007-07-17 16:25:45 +0000 | [diff] [blame] | 1184 | static 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 Prijono | 1f61a8f | 2007-08-16 10:11:44 +0000 | [diff] [blame] | 1191 | for (--p; p!=path && *p!='/' && *p!='\\'; ) --p; |
Benny Prijono | d5696da | 2007-07-17 16:25:45 +0000 | [diff] [blame] | 1192 | |
| 1193 | return (p==path) ? p : p+1; |
| 1194 | } |
| 1195 | |
| 1196 | |
Benny Prijono | eebe9af | 2006-06-13 22:57:13 +0000 | [diff] [blame] | 1197 | /* |
| 1198 | * Create a file player, and automatically connect this player to |
| 1199 | * the conference bridge. |
| 1200 | */ |
| 1201 | PJ_DEF(pj_status_t) pjsua_player_create( const pj_str_t *filename, |
| 1202 | unsigned options, |
Benny Prijono | eebe9af | 2006-06-13 22:57:13 +0000 | [diff] [blame] | 1203 | pjsua_player_id *p_id) |
| 1204 | { |
| 1205 | unsigned slot, file_id; |
Benny Prijono | a66c331 | 2007-01-21 23:12:40 +0000 | [diff] [blame] | 1206 | char path[PJ_MAXPATH]; |
Benny Prijono | d5696da | 2007-07-17 16:25:45 +0000 | [diff] [blame] | 1207 | pj_pool_t *pool; |
Benny Prijono | eebe9af | 2006-06-13 22:57:13 +0000 | [diff] [blame] | 1208 | 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 Prijono | d5696da | 2007-07-17 16:25:45 +0000 | [diff] [blame] | 1230 | |
| 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 Prijono | eebe9af | 2006-06-13 22:57:13 +0000 | [diff] [blame] | 1238 | pjsua_var.mconf_cfg.samples_per_frame * |
| 1239 | 1000 / pjsua_var.media_cfg.clock_rate, |
Benny Prijono | 00cae61 | 2006-07-31 15:19:36 +0000 | [diff] [blame] | 1240 | options, 0, &port); |
Benny Prijono | eebe9af | 2006-06-13 22:57:13 +0000 | [diff] [blame] | 1241 | if (status != PJ_SUCCESS) { |
| 1242 | PJSUA_UNLOCK(); |
| 1243 | pjsua_perror(THIS_FILE, "Unable to open file for playback", status); |
Benny Prijono | d5696da | 2007-07-17 16:25:45 +0000 | [diff] [blame] | 1244 | pj_pool_release(pool); |
Benny Prijono | eebe9af | 2006-06-13 22:57:13 +0000 | [diff] [blame] | 1245 | 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 Prijono | 32e4f49 | 2007-01-24 00:44:26 +0000 | [diff] [blame] | 1253 | pjsua_perror(THIS_FILE, "Unable to add file to conference bridge", |
| 1254 | status); |
Benny Prijono | d5696da | 2007-07-17 16:25:45 +0000 | [diff] [blame] | 1255 | pj_pool_release(pool); |
Benny Prijono | eebe9af | 2006-06-13 22:57:13 +0000 | [diff] [blame] | 1256 | return status; |
| 1257 | } |
| 1258 | |
Benny Prijono | a66c331 | 2007-01-21 23:12:40 +0000 | [diff] [blame] | 1259 | pjsua_var.player[file_id].type = 0; |
Benny Prijono | d5696da | 2007-07-17 16:25:45 +0000 | [diff] [blame] | 1260 | pjsua_var.player[file_id].pool = pool; |
Benny Prijono | eebe9af | 2006-06-13 22:57:13 +0000 | [diff] [blame] | 1261 | 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 Prijono | a66c331 | 2007-01-21 23:12:40 +0000 | [diff] [blame] | 1274 | * Create a file playlist media port, and automatically add the port |
| 1275 | * to the conference bridge. |
| 1276 | */ |
| 1277 | PJ_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 Prijono | d5696da | 2007-07-17 16:25:45 +0000 | [diff] [blame] | 1284 | pj_pool_t *pool; |
Benny Prijono | a66c331 | 2007-01-21 23:12:40 +0000 | [diff] [blame] | 1285 | 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 Prijono | d5696da | 2007-07-17 16:25:45 +0000 | [diff] [blame] | 1309 | 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 Prijono | a66c331 | 2007-01-21 23:12:40 +0000 | [diff] [blame] | 1316 | 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 Prijono | d5696da | 2007-07-17 16:25:45 +0000 | [diff] [blame] | 1321 | pj_pool_release(pool); |
Benny Prijono | a66c331 | 2007-01-21 23:12:40 +0000 | [diff] [blame] | 1322 | return status; |
| 1323 | } |
| 1324 | |
Benny Prijono | d5696da | 2007-07-17 16:25:45 +0000 | [diff] [blame] | 1325 | status = pjmedia_conf_add_port(pjsua_var.mconf, pool, |
Benny Prijono | a66c331 | 2007-01-21 23:12:40 +0000 | [diff] [blame] | 1326 | 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 Prijono | d5696da | 2007-07-17 16:25:45 +0000 | [diff] [blame] | 1331 | pj_pool_release(pool); |
Benny Prijono | a66c331 | 2007-01-21 23:12:40 +0000 | [diff] [blame] | 1332 | return status; |
| 1333 | } |
| 1334 | |
| 1335 | pjsua_var.player[file_id].type = 1; |
Benny Prijono | d5696da | 2007-07-17 16:25:45 +0000 | [diff] [blame] | 1336 | pjsua_var.player[file_id].pool = pool; |
Benny Prijono | a66c331 | 2007-01-21 23:12:40 +0000 | [diff] [blame] | 1337 | 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 Prijono | eebe9af | 2006-06-13 22:57:13 +0000 | [diff] [blame] | 1351 | * Get conference port ID associated with player. |
| 1352 | */ |
| 1353 | PJ_DEF(pjsua_conf_port_id) pjsua_player_get_conf_port(pjsua_player_id id) |
| 1354 | { |
Benny Prijono | a1e6968 | 2007-05-11 15:14:34 +0000 | [diff] [blame] | 1355 | 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] | 1356 | PJ_ASSERT_RETURN(pjsua_var.player[id].port != NULL, PJ_EINVAL); |
| 1357 | |
| 1358 | return pjsua_var.player[id].slot; |
| 1359 | } |
| 1360 | |
Benny Prijono | 469b152 | 2006-12-26 03:05:17 +0000 | [diff] [blame] | 1361 | /* |
| 1362 | * Get the media port for the player. |
| 1363 | */ |
| 1364 | PJ_DEF(pj_status_t) pjsua_player_get_port( pjsua_recorder_id id, |
| 1365 | pjmedia_port **p_port) |
| 1366 | { |
Benny Prijono | a1e6968 | 2007-05-11 15:14:34 +0000 | [diff] [blame] | 1367 | 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] | 1368 | 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 Prijono | eebe9af | 2006-06-13 22:57:13 +0000 | [diff] [blame] | 1375 | |
| 1376 | /* |
| 1377 | * Set playback position. |
| 1378 | */ |
| 1379 | PJ_DEF(pj_status_t) pjsua_player_set_pos( pjsua_player_id id, |
| 1380 | pj_uint32_t samples) |
| 1381 | { |
Benny Prijono | a1e6968 | 2007-05-11 15:14:34 +0000 | [diff] [blame] | 1382 | 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] | 1383 | PJ_ASSERT_RETURN(pjsua_var.player[id].port != NULL, PJ_EINVAL); |
Benny Prijono | a66c331 | 2007-01-21 23:12:40 +0000 | [diff] [blame] | 1384 | PJ_ASSERT_RETURN(pjsua_var.player[id].type == 0, PJ_EINVAL); |
Benny Prijono | eebe9af | 2006-06-13 22:57:13 +0000 | [diff] [blame] | 1385 | |
| 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 | */ |
| 1394 | PJ_DEF(pj_status_t) pjsua_player_destroy(pjsua_player_id id) |
| 1395 | { |
Benny Prijono | a1e6968 | 2007-05-11 15:14:34 +0000 | [diff] [blame] | 1396 | 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] | 1397 | 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 Prijono | d5696da | 2007-07-17 16:25:45 +0000 | [diff] [blame] | 1407 | pj_pool_release(pjsua_var.player[id].pool); |
| 1408 | pjsua_var.player[id].pool = NULL; |
Benny Prijono | eebe9af | 2006-06-13 22:57:13 +0000 | [diff] [blame] | 1409 | 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 | */ |
| 1426 | 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] | 1427 | unsigned enc_type, |
| 1428 | void *enc_param, |
Benny Prijono | eebe9af | 2006-06-13 22:57:13 +0000 | [diff] [blame] | 1429 | pj_ssize_t max_size, |
| 1430 | unsigned options, |
Benny Prijono | eebe9af | 2006-06-13 22:57:13 +0000 | [diff] [blame] | 1431 | pjsua_recorder_id *p_id) |
| 1432 | { |
Benny Prijono | b3cdb2b | 2006-10-19 15:49:47 +0000 | [diff] [blame] | 1433 | enum Format |
| 1434 | { |
| 1435 | FMT_UNKNOWN, |
| 1436 | FMT_WAV, |
| 1437 | FMT_MP3, |
| 1438 | }; |
Benny Prijono | eebe9af | 2006-06-13 22:57:13 +0000 | [diff] [blame] | 1439 | unsigned slot, file_id; |
Benny Prijono | a66c331 | 2007-01-21 23:12:40 +0000 | [diff] [blame] | 1440 | char path[PJ_MAXPATH]; |
Benny Prijono | b3cdb2b | 2006-10-19 15:49:47 +0000 | [diff] [blame] | 1441 | pj_str_t ext; |
Benny Prijono | 8f31052 | 2006-10-20 11:08:49 +0000 | [diff] [blame] | 1442 | int file_format; |
Benny Prijono | d5696da | 2007-07-17 16:25:45 +0000 | [diff] [blame] | 1443 | pj_pool_t *pool; |
Benny Prijono | eebe9af | 2006-06-13 22:57:13 +0000 | [diff] [blame] | 1444 | pjmedia_port *port; |
| 1445 | pj_status_t status; |
| 1446 | |
Benny Prijono | b3cdb2b | 2006-10-19 15:49:47 +0000 | [diff] [blame] | 1447 | /* Filename must present */ |
| 1448 | PJ_ASSERT_RETURN(filename != NULL, PJ_EINVAL); |
| 1449 | |
Benny Prijono | 00cae61 | 2006-07-31 15:19:36 +0000 | [diff] [blame] | 1450 | /* Don't support max_size at present */ |
Benny Prijono | b3cdb2b | 2006-10-19 15:49:47 +0000 | [diff] [blame] | 1451 | PJ_ASSERT_RETURN(max_size == 0 || max_size == -1, PJ_EINVAL); |
Benny Prijono | 00cae61 | 2006-07-31 15:19:36 +0000 | [diff] [blame] | 1452 | |
Benny Prijono | 8f31052 | 2006-10-20 11:08:49 +0000 | [diff] [blame] | 1453 | /* Don't support encoding type at present */ |
| 1454 | PJ_ASSERT_RETURN(enc_type == 0, PJ_EINVAL); |
Benny Prijono | 00cae61 | 2006-07-31 15:19:36 +0000 | [diff] [blame] | 1455 | |
Benny Prijono | eebe9af | 2006-06-13 22:57:13 +0000 | [diff] [blame] | 1456 | if (pjsua_var.rec_cnt >= PJ_ARRAY_SIZE(pjsua_var.recorder)) |
| 1457 | return PJ_ETOOMANY; |
| 1458 | |
Benny Prijono | b3cdb2b | 2006-10-19 15:49:47 +0000 | [diff] [blame] | 1459 | /* 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 Prijono | eebe9af | 2006-06-13 22:57:13 +0000 | [diff] [blame] | 1474 | 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 Prijono | b3cdb2b | 2006-10-19 15:49:47 +0000 | [diff] [blame] | 1490 | |
Benny Prijono | d5696da | 2007-07-17 16:25:45 +0000 | [diff] [blame] | 1491 | pool = pjsua_pool_create(get_basename(path, filename->slen), 1000, 1000); |
| 1492 | if (!pool) { |
| 1493 | PJSUA_UNLOCK(); |
| 1494 | return PJ_ENOMEM; |
| 1495 | } |
| 1496 | |
Benny Prijono | b3cdb2b | 2006-10-19 15:49:47 +0000 | [diff] [blame] | 1497 | if (file_format == FMT_WAV) { |
Benny Prijono | d5696da | 2007-07-17 16:25:45 +0000 | [diff] [blame] | 1498 | status = pjmedia_wav_writer_port_create(pool, path, |
Benny Prijono | b3cdb2b | 2006-10-19 15:49:47 +0000 | [diff] [blame] | 1499 | 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 Prijono | b3cdb2b | 2006-10-19 15:49:47 +0000 | [diff] [blame] | 1504 | } else { |
Benny Prijono | c95a0f0 | 2007-04-09 07:06:08 +0000 | [diff] [blame] | 1505 | PJ_UNUSED_ARG(enc_param); |
Benny Prijono | b3cdb2b | 2006-10-19 15:49:47 +0000 | [diff] [blame] | 1506 | port = NULL; |
| 1507 | status = PJ_ENOTSUP; |
| 1508 | } |
| 1509 | |
Benny Prijono | eebe9af | 2006-06-13 22:57:13 +0000 | [diff] [blame] | 1510 | if (status != PJ_SUCCESS) { |
| 1511 | PJSUA_UNLOCK(); |
| 1512 | pjsua_perror(THIS_FILE, "Unable to open file for recording", status); |
Benny Prijono | d5696da | 2007-07-17 16:25:45 +0000 | [diff] [blame] | 1513 | pj_pool_release(pool); |
Benny Prijono | eebe9af | 2006-06-13 22:57:13 +0000 | [diff] [blame] | 1514 | return status; |
| 1515 | } |
| 1516 | |
Benny Prijono | d5696da | 2007-07-17 16:25:45 +0000 | [diff] [blame] | 1517 | status = pjmedia_conf_add_port(pjsua_var.mconf, pool, |
Benny Prijono | eebe9af | 2006-06-13 22:57:13 +0000 | [diff] [blame] | 1518 | port, filename, &slot); |
| 1519 | if (status != PJ_SUCCESS) { |
| 1520 | pjmedia_port_destroy(port); |
| 1521 | PJSUA_UNLOCK(); |
Benny Prijono | d5696da | 2007-07-17 16:25:45 +0000 | [diff] [blame] | 1522 | pj_pool_release(pool); |
Benny Prijono | eebe9af | 2006-06-13 22:57:13 +0000 | [diff] [blame] | 1523 | return status; |
| 1524 | } |
| 1525 | |
| 1526 | pjsua_var.recorder[file_id].port = port; |
| 1527 | pjsua_var.recorder[file_id].slot = slot; |
Benny Prijono | d5696da | 2007-07-17 16:25:45 +0000 | [diff] [blame] | 1528 | pjsua_var.recorder[file_id].pool = pool; |
Benny Prijono | eebe9af | 2006-06-13 22:57:13 +0000 | [diff] [blame] | 1529 | |
| 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 | */ |
| 1542 | PJ_DEF(pjsua_conf_port_id) pjsua_recorder_get_conf_port(pjsua_recorder_id id) |
| 1543 | { |
Benny Prijono | a1e6968 | 2007-05-11 15:14:34 +0000 | [diff] [blame] | 1544 | PJ_ASSERT_RETURN(id>=0 && id<(int)PJ_ARRAY_SIZE(pjsua_var.recorder), |
| 1545 | PJ_EINVAL); |
Benny Prijono | eebe9af | 2006-06-13 22:57:13 +0000 | [diff] [blame] | 1546 | PJ_ASSERT_RETURN(pjsua_var.recorder[id].port != NULL, PJ_EINVAL); |
| 1547 | |
| 1548 | return pjsua_var.recorder[id].slot; |
| 1549 | } |
| 1550 | |
Benny Prijono | 469b152 | 2006-12-26 03:05:17 +0000 | [diff] [blame] | 1551 | /* |
| 1552 | * Get the media port for the recorder. |
| 1553 | */ |
| 1554 | PJ_DEF(pj_status_t) pjsua_recorder_get_port( pjsua_recorder_id id, |
| 1555 | pjmedia_port **p_port) |
| 1556 | { |
Benny Prijono | a1e6968 | 2007-05-11 15:14:34 +0000 | [diff] [blame] | 1557 | PJ_ASSERT_RETURN(id>=0 && id<(int)PJ_ARRAY_SIZE(pjsua_var.recorder), |
| 1558 | PJ_EINVAL); |
Benny Prijono | 469b152 | 2006-12-26 03:05:17 +0000 | [diff] [blame] | 1559 | 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 Prijono | eebe9af | 2006-06-13 22:57:13 +0000 | [diff] [blame] | 1565 | |
| 1566 | /* |
| 1567 | * Destroy recorder (this will complete recording). |
| 1568 | */ |
| 1569 | PJ_DEF(pj_status_t) pjsua_recorder_destroy(pjsua_recorder_id id) |
| 1570 | { |
Benny Prijono | a1e6968 | 2007-05-11 15:14:34 +0000 | [diff] [blame] | 1571 | PJ_ASSERT_RETURN(id>=0 && id<(int)PJ_ARRAY_SIZE(pjsua_var.recorder), |
| 1572 | PJ_EINVAL); |
Benny Prijono | eebe9af | 2006-06-13 22:57:13 +0000 | [diff] [blame] | 1573 | 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 Prijono | d5696da | 2007-07-17 16:25:45 +0000 | [diff] [blame] | 1583 | pj_pool_release(pjsua_var.recorder[id].pool); |
| 1584 | pjsua_var.recorder[id].pool = NULL; |
Benny Prijono | eebe9af | 2006-06-13 22:57:13 +0000 | [diff] [blame] | 1585 | 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 | */ |
| 1601 | PJ_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 */ |
| 1624 | static 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 | */ |
| 1654 | PJ_DEF(pj_status_t) pjsua_set_snd_dev( int capture_dev, |
| 1655 | int playback_dev) |
| 1656 | { |
| 1657 | pjmedia_port *conf_port; |
Benny Prijono | 6dd967c | 2006-12-26 02:27:14 +0000 | [diff] [blame] | 1658 | const pjmedia_snd_dev_info *play_info; |
Benny Prijono | 26056d8 | 2006-10-11 16:03:41 +0000 | [diff] [blame] | 1659 | unsigned clock_rates[] = { 0, 22050, 44100, 48000, 11025, 32000, 8000}; |
Benny Prijono | 658a1c5 | 2006-10-11 21:56:16 +0000 | [diff] [blame] | 1660 | unsigned selected_clock_rate = 0; |
Benny Prijono | 26056d8 | 2006-10-11 16:03:41 +0000 | [diff] [blame] | 1661 | unsigned i; |
Benny Prijono | c53c6d7 | 2006-11-27 09:54:03 +0000 | [diff] [blame] | 1662 | pjmedia_snd_stream *strm; |
| 1663 | pjmedia_snd_stream_info si; |
| 1664 | pj_str_t tmp; |
Benny Prijono | 26056d8 | 2006-10-11 16:03:41 +0000 | [diff] [blame] | 1665 | pj_status_t status = -1; |
Benny Prijono | eebe9af | 2006-06-13 22:57:13 +0000 | [diff] [blame] | 1666 | |
| 1667 | /* Close existing sound port */ |
| 1668 | close_snd_dev(); |
| 1669 | |
| 1670 | |
Benny Prijono | 26056d8 | 2006-10-11 16:03:41 +0000 | [diff] [blame] | 1671 | /* Set default clock rate */ |
| 1672 | clock_rates[0] = pjsua_var.media_cfg.clock_rate; |
Benny Prijono | eebe9af | 2006-06-13 22:57:13 +0000 | [diff] [blame] | 1673 | |
Benny Prijono | 26056d8 | 2006-10-11 16:03:41 +0000 | [diff] [blame] | 1674 | /* 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 Prijono | cf0b4b2 | 2007-10-06 17:31:09 +0000 | [diff] [blame] | 1677 | unsigned fps; |
Benny Prijono | 26056d8 | 2006-10-11 16:03:41 +0000 | [diff] [blame] | 1678 | |
| 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 Prijono | cf0b4b2 | 2007-10-06 17:31:09 +0000 | [diff] [blame] | 1684 | fps = 1000 / pjsua_var.media_cfg.audio_frame_ptime; |
Benny Prijono | 26056d8 | 2006-10-11 16:03:41 +0000 | [diff] [blame] | 1685 | status = pjmedia_snd_port_create(pjsua_var.pool, capture_dev, |
| 1686 | playback_dev, |
| 1687 | clock_rates[i], 1, |
Benny Prijono | cf0b4b2 | 2007-10-06 17:31:09 +0000 | [diff] [blame] | 1688 | clock_rates[i]/fps, |
Benny Prijono | 26056d8 | 2006-10-11 16:03:41 +0000 | [diff] [blame] | 1689 | 16, 0, &pjsua_var.snd_port); |
| 1690 | |
Benny Prijono | 658a1c5 | 2006-10-11 21:56:16 +0000 | [diff] [blame] | 1691 | if (status == PJ_SUCCESS) { |
| 1692 | selected_clock_rate = clock_rates[i]; |
Benny Prijono | 26056d8 | 2006-10-11 16:03:41 +0000 | [diff] [blame] | 1693 | break; |
Benny Prijono | 658a1c5 | 2006-10-11 21:56:16 +0000 | [diff] [blame] | 1694 | } |
Benny Prijono | 26056d8 | 2006-10-11 16:03:41 +0000 | [diff] [blame] | 1695 | |
| 1696 | pj_strerror(status, errmsg, sizeof(errmsg)); |
Benny Prijono | 658a1c5 | 2006-10-11 21:56:16 +0000 | [diff] [blame] | 1697 | PJ_LOG(4, (THIS_FILE, "..failed: %s", errmsg)); |
Benny Prijono | 26056d8 | 2006-10-11 16:03:41 +0000 | [diff] [blame] | 1698 | } |
| 1699 | |
Benny Prijono | eebe9af | 2006-06-13 22:57:13 +0000 | [diff] [blame] | 1700 | 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 Prijono | f20687a | 2006-08-04 18:27:19 +0000 | [diff] [blame] | 1709 | /* Set AEC */ |
Benny Prijono | 5da5043 | 2006-08-07 10:24:52 +0000 | [diff] [blame] | 1710 | 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 Prijono | eebe9af | 2006-06-13 22:57:13 +0000 | [diff] [blame] | 1713 | |
Benny Prijono | 658a1c5 | 2006-10-11 21:56:16 +0000 | [diff] [blame] | 1714 | /* 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 Prijono | 52a9391 | 2006-08-04 20:54:37 +0000 | [diff] [blame] | 1731 | /* 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 Prijono | eebe9af | 2006-06-13 22:57:13 +0000 | [diff] [blame] | 1742 | /* Save the device IDs */ |
| 1743 | pjsua_var.cap_dev = capture_dev; |
| 1744 | pjsua_var.play_dev = playback_dev; |
| 1745 | |
Benny Prijono | c53c6d7 | 2006-11-27 09:54:03 +0000 | [diff] [blame] | 1746 | /* 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 Prijono | eebe9af | 2006-06-13 22:57:13 +0000 | [diff] [blame] | 1754 | return PJ_SUCCESS; |
| 1755 | } |
| 1756 | |
| 1757 | |
| 1758 | /* |
Benny Prijono | ebdf877 | 2007-02-01 19:25:50 +0000 | [diff] [blame] | 1759 | * 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 | */ |
| 1763 | PJ_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 Prijono | eebe9af | 2006-06-13 22:57:13 +0000 | [diff] [blame] | 1778 | * Use null sound device. |
| 1779 | */ |
| 1780 | PJ_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 Prijono | e909eac | 2006-07-27 22:04:56 +0000 | [diff] [blame] | 1813 | |
| 1814 | /* |
| 1815 | * Use no device! |
| 1816 | */ |
| 1817 | PJ_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 Prijono | f20687a | 2006-08-04 18:27:19 +0000 | [diff] [blame] | 1827 | /* |
| 1828 | * Configure the AEC settings of the sound port. |
| 1829 | */ |
Benny Prijono | 5da5043 | 2006-08-07 10:24:52 +0000 | [diff] [blame] | 1830 | 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] | 1831 | { |
| 1832 | pjsua_var.media_cfg.ec_tail_len = tail_ms; |
| 1833 | |
| 1834 | if (pjsua_var.snd_port) |
Benny Prijono | 5da5043 | 2006-08-07 10:24:52 +0000 | [diff] [blame] | 1835 | return pjmedia_snd_port_set_ec( pjsua_var.snd_port, pjsua_var.pool, |
| 1836 | tail_ms, options); |
Benny Prijono | f20687a | 2006-08-04 18:27:19 +0000 | [diff] [blame] | 1837 | |
| 1838 | return PJ_SUCCESS; |
| 1839 | } |
| 1840 | |
| 1841 | |
| 1842 | /* |
| 1843 | * Get current AEC tail length. |
| 1844 | */ |
Benny Prijono | 22dfe59 | 2006-08-06 12:07:13 +0000 | [diff] [blame] | 1845 | 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] | 1846 | { |
| 1847 | *p_tail_ms = pjsua_var.media_cfg.ec_tail_len; |
| 1848 | return PJ_SUCCESS; |
| 1849 | } |
| 1850 | |
Benny Prijono | e909eac | 2006-07-27 22:04:56 +0000 | [diff] [blame] | 1851 | |
Benny Prijono | eebe9af | 2006-06-13 22:57:13 +0000 | [diff] [blame] | 1852 | /***************************************************************************** |
| 1853 | * Codecs. |
| 1854 | */ |
| 1855 | |
| 1856 | /* |
| 1857 | * Enum all supported codecs in the system. |
| 1858 | */ |
| 1859 | PJ_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 | */ |
| 1892 | PJ_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 | */ |
| 1907 | PJ_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 | */ |
| 1933 | PJ_DEF(pj_status_t) pjsua_codec_set_param( const pj_str_t *id, |
| 1934 | const pjmedia_codec_param *param) |
| 1935 | { |
Benny Prijono | 00cae61 | 2006-07-31 15:19:36 +0000 | [diff] [blame] | 1936 | PJ_UNUSED_ARG(id); |
| 1937 | PJ_UNUSED_ARG(param); |
Benny Prijono | eebe9af | 2006-06-13 22:57:13 +0000 | [diff] [blame] | 1938 | PJ_TODO(set_codec_param); |
| 1939 | return PJ_SUCCESS; |
| 1940 | } |