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 | 80eee89 | 2007-11-03 22:43:23 +0000 | [diff] [blame] | 25 | #define DEFAULT_RTP_PORT 4000 |
| 26 | |
| 27 | #ifndef PJSUA_REQUIRE_CONSECUTIVE_RTCP_PORT |
| 28 | # define PJSUA_REQUIRE_CONSECUTIVE_RTCP_PORT 0 |
| 29 | #endif |
| 30 | |
Benny Prijono | eebe9af | 2006-06-13 22:57:13 +0000 | [diff] [blame] | 31 | |
Benny Prijono | de47956 | 2007-03-15 10:23:55 +0000 | [diff] [blame] | 32 | /* Next RTP port to be used */ |
| 33 | static pj_uint16_t next_rtp_port; |
Benny Prijono | eebe9af | 2006-06-13 22:57:13 +0000 | [diff] [blame] | 34 | |
| 35 | /* Close existing sound device */ |
| 36 | static void close_snd_dev(void); |
| 37 | |
| 38 | |
| 39 | /** |
| 40 | * Init media subsystems. |
| 41 | */ |
| 42 | pj_status_t pjsua_media_subsys_init(const pjsua_media_config *cfg) |
| 43 | { |
Benny Prijono | ba5926a | 2007-05-02 11:29:37 +0000 | [diff] [blame] | 44 | pj_str_t codec_id = {NULL, 0}; |
Benny Prijono | 0498d90 | 2006-06-19 14:49:14 +0000 | [diff] [blame] | 45 | unsigned opt; |
Benny Prijono | eebe9af | 2006-06-13 22:57:13 +0000 | [diff] [blame] | 46 | pj_status_t status; |
| 47 | |
Benny Prijono | fc24e69 | 2007-01-27 18:31:51 +0000 | [diff] [blame] | 48 | /* To suppress warning about unused var when all codecs are disabled */ |
| 49 | PJ_UNUSED_ARG(codec_id); |
| 50 | |
Benny Prijono | eebe9af | 2006-06-13 22:57:13 +0000 | [diff] [blame] | 51 | /* Copy configuration */ |
| 52 | pj_memcpy(&pjsua_var.media_cfg, cfg, sizeof(*cfg)); |
| 53 | |
| 54 | /* Normalize configuration */ |
Benny Prijono | 50f19b3 | 2008-03-11 13:15:43 +0000 | [diff] [blame^] | 55 | if (pjsua_var.media_cfg.snd_clock_rate == 0) { |
| 56 | pjsua_var.media_cfg.snd_clock_rate = pjsua_var.media_cfg.clock_rate; |
| 57 | } |
Benny Prijono | eebe9af | 2006-06-13 22:57:13 +0000 | [diff] [blame] | 58 | |
| 59 | if (pjsua_var.media_cfg.has_ioqueue && |
| 60 | pjsua_var.media_cfg.thread_cnt == 0) |
| 61 | { |
| 62 | pjsua_var.media_cfg.thread_cnt = 1; |
| 63 | } |
| 64 | |
| 65 | if (pjsua_var.media_cfg.max_media_ports < pjsua_var.ua_cfg.max_calls) { |
| 66 | pjsua_var.media_cfg.max_media_ports = pjsua_var.ua_cfg.max_calls + 2; |
| 67 | } |
| 68 | |
| 69 | /* Create media endpoint. */ |
| 70 | status = pjmedia_endpt_create(&pjsua_var.cp.factory, |
| 71 | pjsua_var.media_cfg.has_ioqueue? NULL : |
| 72 | pjsip_endpt_get_ioqueue(pjsua_var.endpt), |
| 73 | pjsua_var.media_cfg.thread_cnt, |
| 74 | &pjsua_var.med_endpt); |
| 75 | if (status != PJ_SUCCESS) { |
| 76 | pjsua_perror(THIS_FILE, |
| 77 | "Media stack initialization has returned error", |
| 78 | status); |
| 79 | return status; |
| 80 | } |
| 81 | |
| 82 | /* Register all codecs */ |
| 83 | #if PJMEDIA_HAS_SPEEX_CODEC |
| 84 | /* Register speex. */ |
| 85 | status = pjmedia_codec_speex_init(pjsua_var.med_endpt, |
Benny Prijono | 7ca96da | 2006-08-07 12:11:40 +0000 | [diff] [blame] | 86 | 0, |
Benny Prijono | 0498d90 | 2006-06-19 14:49:14 +0000 | [diff] [blame] | 87 | pjsua_var.media_cfg.quality, |
| 88 | pjsua_var.media_cfg.quality); |
Benny Prijono | eebe9af | 2006-06-13 22:57:13 +0000 | [diff] [blame] | 89 | if (status != PJ_SUCCESS) { |
| 90 | pjsua_perror(THIS_FILE, "Error initializing Speex codec", |
| 91 | status); |
| 92 | return status; |
| 93 | } |
Benny Prijono | 7ca96da | 2006-08-07 12:11:40 +0000 | [diff] [blame] | 94 | |
| 95 | /* Set speex/16000 to higher priority*/ |
| 96 | codec_id = pj_str("speex/16000"); |
| 97 | pjmedia_codec_mgr_set_codec_priority( |
| 98 | pjmedia_endpt_get_codec_mgr(pjsua_var.med_endpt), |
| 99 | &codec_id, PJMEDIA_CODEC_PRIO_NORMAL+2); |
| 100 | |
| 101 | /* Set speex/8000 to next higher priority*/ |
| 102 | codec_id = pj_str("speex/8000"); |
| 103 | pjmedia_codec_mgr_set_codec_priority( |
| 104 | pjmedia_endpt_get_codec_mgr(pjsua_var.med_endpt), |
| 105 | &codec_id, PJMEDIA_CODEC_PRIO_NORMAL+1); |
| 106 | |
| 107 | |
| 108 | |
Benny Prijono | eebe9af | 2006-06-13 22:57:13 +0000 | [diff] [blame] | 109 | #endif /* PJMEDIA_HAS_SPEEX_CODEC */ |
| 110 | |
Benny Prijono | 00cae61 | 2006-07-31 15:19:36 +0000 | [diff] [blame] | 111 | #if PJMEDIA_HAS_ILBC_CODEC |
| 112 | /* Register iLBC. */ |
| 113 | status = pjmedia_codec_ilbc_init( pjsua_var.med_endpt, |
| 114 | pjsua_var.media_cfg.ilbc_mode); |
| 115 | if (status != PJ_SUCCESS) { |
| 116 | pjsua_perror(THIS_FILE, "Error initializing iLBC codec", |
| 117 | status); |
| 118 | return status; |
| 119 | } |
| 120 | #endif /* PJMEDIA_HAS_ILBC_CODEC */ |
| 121 | |
Benny Prijono | eebe9af | 2006-06-13 22:57:13 +0000 | [diff] [blame] | 122 | #if PJMEDIA_HAS_GSM_CODEC |
| 123 | /* Register GSM */ |
| 124 | status = pjmedia_codec_gsm_init(pjsua_var.med_endpt); |
| 125 | if (status != PJ_SUCCESS) { |
| 126 | pjsua_perror(THIS_FILE, "Error initializing GSM codec", |
| 127 | status); |
| 128 | return status; |
| 129 | } |
| 130 | #endif /* PJMEDIA_HAS_GSM_CODEC */ |
| 131 | |
| 132 | #if PJMEDIA_HAS_G711_CODEC |
| 133 | /* Register PCMA and PCMU */ |
| 134 | status = pjmedia_codec_g711_init(pjsua_var.med_endpt); |
| 135 | if (status != PJ_SUCCESS) { |
| 136 | pjsua_perror(THIS_FILE, "Error initializing G711 codec", |
| 137 | status); |
| 138 | return status; |
| 139 | } |
| 140 | #endif /* PJMEDIA_HAS_G711_CODEC */ |
| 141 | |
| 142 | #if PJMEDIA_HAS_L16_CODEC |
| 143 | /* Register L16 family codecs, but disable all */ |
| 144 | status = pjmedia_codec_l16_init(pjsua_var.med_endpt, 0); |
| 145 | if (status != PJ_SUCCESS) { |
| 146 | pjsua_perror(THIS_FILE, "Error initializing L16 codecs", |
| 147 | status); |
| 148 | return status; |
| 149 | } |
| 150 | |
| 151 | /* Disable ALL L16 codecs */ |
| 152 | codec_id = pj_str("L16"); |
| 153 | pjmedia_codec_mgr_set_codec_priority( |
| 154 | pjmedia_endpt_get_codec_mgr(pjsua_var.med_endpt), |
| 155 | &codec_id, PJMEDIA_CODEC_PRIO_DISABLED); |
| 156 | |
| 157 | #endif /* PJMEDIA_HAS_L16_CODEC */ |
| 158 | |
| 159 | |
| 160 | /* Save additional conference bridge parameters for future |
| 161 | * reference. |
| 162 | */ |
| 163 | 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] | 164 | pjsua_var.media_cfg.audio_frame_ptime / |
| 165 | 1000; |
Benny Prijono | eebe9af | 2006-06-13 22:57:13 +0000 | [diff] [blame] | 166 | pjsua_var.mconf_cfg.channel_count = 1; |
| 167 | pjsua_var.mconf_cfg.bits_per_sample = 16; |
| 168 | |
Benny Prijono | 0498d90 | 2006-06-19 14:49:14 +0000 | [diff] [blame] | 169 | /* Init options for conference bridge. */ |
| 170 | opt = PJMEDIA_CONF_NO_DEVICE; |
| 171 | if (pjsua_var.media_cfg.quality >= 3 && |
Benny Prijono | 7ca96da | 2006-08-07 12:11:40 +0000 | [diff] [blame] | 172 | pjsua_var.media_cfg.quality <= 4) |
Benny Prijono | 0498d90 | 2006-06-19 14:49:14 +0000 | [diff] [blame] | 173 | { |
| 174 | opt |= PJMEDIA_CONF_SMALL_FILTER; |
| 175 | } |
| 176 | else if (pjsua_var.media_cfg.quality < 3) { |
| 177 | opt |= PJMEDIA_CONF_USE_LINEAR; |
| 178 | } |
| 179 | |
| 180 | |
Benny Prijono | eebe9af | 2006-06-13 22:57:13 +0000 | [diff] [blame] | 181 | /* Init conference bridge. */ |
| 182 | status = pjmedia_conf_create(pjsua_var.pool, |
| 183 | pjsua_var.media_cfg.max_media_ports, |
| 184 | pjsua_var.media_cfg.clock_rate, |
| 185 | pjsua_var.mconf_cfg.channel_count, |
| 186 | pjsua_var.mconf_cfg.samples_per_frame, |
| 187 | pjsua_var.mconf_cfg.bits_per_sample, |
Benny Prijono | 0498d90 | 2006-06-19 14:49:14 +0000 | [diff] [blame] | 188 | opt, &pjsua_var.mconf); |
Benny Prijono | eebe9af | 2006-06-13 22:57:13 +0000 | [diff] [blame] | 189 | if (status != PJ_SUCCESS) { |
Benny Prijono | 50f19b3 | 2008-03-11 13:15:43 +0000 | [diff] [blame^] | 190 | pjsua_perror(THIS_FILE, "Error creating conference bridge", |
Benny Prijono | eebe9af | 2006-06-13 22:57:13 +0000 | [diff] [blame] | 191 | status); |
| 192 | return status; |
| 193 | } |
| 194 | |
| 195 | /* Create null port just in case user wants to use null sound. */ |
| 196 | status = pjmedia_null_port_create(pjsua_var.pool, |
| 197 | pjsua_var.media_cfg.clock_rate, |
| 198 | pjsua_var.mconf_cfg.channel_count, |
| 199 | pjsua_var.mconf_cfg.samples_per_frame, |
| 200 | pjsua_var.mconf_cfg.bits_per_sample, |
| 201 | &pjsua_var.null_port); |
| 202 | PJ_ASSERT_RETURN(status == PJ_SUCCESS, status); |
| 203 | |
Benny Prijono | 6ba8c54 | 2007-10-16 01:34:14 +0000 | [diff] [blame] | 204 | /* Perform NAT detection */ |
| 205 | pjsua_detect_nat_type(); |
| 206 | |
Benny Prijono | eebe9af | 2006-06-13 22:57:13 +0000 | [diff] [blame] | 207 | return PJ_SUCCESS; |
| 208 | } |
| 209 | |
| 210 | |
| 211 | /* |
| 212 | * Create RTP and RTCP socket pair, and possibly resolve their public |
| 213 | * address via STUN. |
| 214 | */ |
| 215 | static pj_status_t create_rtp_rtcp_sock(const pjsua_transport_config *cfg, |
| 216 | pjmedia_sock_info *skinfo) |
| 217 | { |
| 218 | enum { |
| 219 | RTP_RETRY = 100 |
| 220 | }; |
| 221 | int i; |
Benny Prijono | 0a5cad8 | 2006-09-26 13:21:02 +0000 | [diff] [blame] | 222 | pj_sockaddr_in bound_addr; |
Benny Prijono | eebe9af | 2006-06-13 22:57:13 +0000 | [diff] [blame] | 223 | pj_sockaddr_in mapped_addr[2]; |
| 224 | pj_status_t status = PJ_SUCCESS; |
Benny Prijono | 38fb3ea | 2008-01-02 08:27:03 +0000 | [diff] [blame] | 225 | char addr_buf[PJ_INET6_ADDRSTRLEN+2]; |
Benny Prijono | eebe9af | 2006-06-13 22:57:13 +0000 | [diff] [blame] | 226 | pj_sock_t sock[2]; |
| 227 | |
Benny Prijono | c97608e | 2007-03-23 16:34:20 +0000 | [diff] [blame] | 228 | /* Make sure STUN server resolution has completed */ |
| 229 | status = pjsua_resolve_stun_server(PJ_TRUE); |
| 230 | if (status != PJ_SUCCESS) { |
| 231 | pjsua_perror(THIS_FILE, "Error resolving STUN server", status); |
| 232 | return status; |
| 233 | } |
| 234 | |
Benny Prijono | de47956 | 2007-03-15 10:23:55 +0000 | [diff] [blame] | 235 | if (next_rtp_port == 0) |
| 236 | next_rtp_port = (pj_uint16_t)cfg->port; |
Benny Prijono | eebe9af | 2006-06-13 22:57:13 +0000 | [diff] [blame] | 237 | |
| 238 | for (i=0; i<2; ++i) |
| 239 | sock[i] = PJ_INVALID_SOCKET; |
| 240 | |
Benny Prijono | 0a5cad8 | 2006-09-26 13:21:02 +0000 | [diff] [blame] | 241 | bound_addr.sin_addr.s_addr = PJ_INADDR_ANY; |
| 242 | if (cfg->bound_addr.slen) { |
| 243 | status = pj_sockaddr_in_set_str_addr(&bound_addr, &cfg->bound_addr); |
| 244 | if (status != PJ_SUCCESS) { |
| 245 | pjsua_perror(THIS_FILE, "Unable to resolve transport bind address", |
| 246 | status); |
| 247 | return status; |
| 248 | } |
| 249 | } |
Benny Prijono | eebe9af | 2006-06-13 22:57:13 +0000 | [diff] [blame] | 250 | |
| 251 | /* Loop retry to bind RTP and RTCP sockets. */ |
Benny Prijono | de47956 | 2007-03-15 10:23:55 +0000 | [diff] [blame] | 252 | for (i=0; i<RTP_RETRY; ++i, next_rtp_port += 2) { |
Benny Prijono | eebe9af | 2006-06-13 22:57:13 +0000 | [diff] [blame] | 253 | |
| 254 | /* Create and bind RTP socket. */ |
Benny Prijono | 8ab968f | 2007-07-20 08:08:30 +0000 | [diff] [blame] | 255 | 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] | 256 | if (status != PJ_SUCCESS) { |
| 257 | pjsua_perror(THIS_FILE, "socket() error", status); |
| 258 | return status; |
| 259 | } |
| 260 | |
Benny Prijono | 0cdcd3f | 2007-12-09 14:14:11 +0000 | [diff] [blame] | 261 | status=pj_sock_bind_in(sock[0], pj_ntohl(bound_addr.sin_addr.s_addr), |
| 262 | next_rtp_port); |
Benny Prijono | eebe9af | 2006-06-13 22:57:13 +0000 | [diff] [blame] | 263 | if (status != PJ_SUCCESS) { |
| 264 | pj_sock_close(sock[0]); |
| 265 | sock[0] = PJ_INVALID_SOCKET; |
| 266 | continue; |
| 267 | } |
| 268 | |
| 269 | /* Create and bind RTCP socket. */ |
Benny Prijono | 8ab968f | 2007-07-20 08:08:30 +0000 | [diff] [blame] | 270 | 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] | 271 | if (status != PJ_SUCCESS) { |
| 272 | pjsua_perror(THIS_FILE, "socket() error", status); |
| 273 | pj_sock_close(sock[0]); |
| 274 | return status; |
| 275 | } |
| 276 | |
Benny Prijono | 0cdcd3f | 2007-12-09 14:14:11 +0000 | [diff] [blame] | 277 | status=pj_sock_bind_in(sock[1], pj_ntohl(bound_addr.sin_addr.s_addr), |
| 278 | (pj_uint16_t)(next_rtp_port+1)); |
Benny Prijono | eebe9af | 2006-06-13 22:57:13 +0000 | [diff] [blame] | 279 | if (status != PJ_SUCCESS) { |
| 280 | pj_sock_close(sock[0]); |
| 281 | sock[0] = PJ_INVALID_SOCKET; |
| 282 | |
| 283 | pj_sock_close(sock[1]); |
| 284 | sock[1] = PJ_INVALID_SOCKET; |
| 285 | continue; |
| 286 | } |
| 287 | |
| 288 | /* |
| 289 | * If we're configured to use STUN, then find out the mapped address, |
| 290 | * and make sure that the mapped RTCP port is adjacent with the RTP. |
| 291 | */ |
Benny Prijono | c97608e | 2007-03-23 16:34:20 +0000 | [diff] [blame] | 292 | if (pjsua_var.stun_srv.addr.sa_family != 0) { |
| 293 | char ip_addr[32]; |
| 294 | pj_str_t stun_srv; |
| 295 | |
| 296 | pj_ansi_strcpy(ip_addr, |
| 297 | pj_inet_ntoa(pjsua_var.stun_srv.ipv4.sin_addr)); |
| 298 | stun_srv = pj_str(ip_addr); |
| 299 | |
Benny Prijono | 14c2b86 | 2007-02-21 00:40:05 +0000 | [diff] [blame] | 300 | status=pjstun_get_mapped_addr(&pjsua_var.cp.factory, 2, sock, |
Benny Prijono | af09dc3 | 2007-04-22 12:48:30 +0000 | [diff] [blame] | 301 | &stun_srv, pj_ntohs(pjsua_var.stun_srv.ipv4.sin_port), |
| 302 | &stun_srv, pj_ntohs(pjsua_var.stun_srv.ipv4.sin_port), |
Benny Prijono | eebe9af | 2006-06-13 22:57:13 +0000 | [diff] [blame] | 303 | mapped_addr); |
| 304 | if (status != PJ_SUCCESS) { |
| 305 | pjsua_perror(THIS_FILE, "STUN resolve error", status); |
| 306 | goto on_error; |
| 307 | } |
| 308 | |
Benny Prijono | 80eee89 | 2007-11-03 22:43:23 +0000 | [diff] [blame] | 309 | #if PJSUA_REQUIRE_CONSECUTIVE_RTCP_PORT |
Benny Prijono | eebe9af | 2006-06-13 22:57:13 +0000 | [diff] [blame] | 310 | if (pj_ntohs(mapped_addr[1].sin_port) == |
| 311 | pj_ntohs(mapped_addr[0].sin_port)+1) |
| 312 | { |
| 313 | /* Success! */ |
| 314 | break; |
| 315 | } |
| 316 | |
| 317 | pj_sock_close(sock[0]); |
| 318 | sock[0] = PJ_INVALID_SOCKET; |
| 319 | |
| 320 | pj_sock_close(sock[1]); |
| 321 | sock[1] = PJ_INVALID_SOCKET; |
Benny Prijono | 80eee89 | 2007-11-03 22:43:23 +0000 | [diff] [blame] | 322 | #else |
| 323 | if (pj_ntohs(mapped_addr[1].sin_port) != |
| 324 | pj_ntohs(mapped_addr[0].sin_port)+1) |
| 325 | { |
| 326 | PJ_LOG(4,(THIS_FILE, |
| 327 | "Note: STUN mapped RTCP port %d is not adjacent" |
| 328 | " to RTP port %d", |
| 329 | pj_ntohs(mapped_addr[1].sin_port), |
| 330 | pj_ntohs(mapped_addr[0].sin_port))); |
| 331 | } |
| 332 | /* Success! */ |
| 333 | break; |
| 334 | #endif |
Benny Prijono | eebe9af | 2006-06-13 22:57:13 +0000 | [diff] [blame] | 335 | |
Benny Prijono | 0a5cad8 | 2006-09-26 13:21:02 +0000 | [diff] [blame] | 336 | } else if (cfg->public_addr.slen) { |
| 337 | |
| 338 | status = pj_sockaddr_in_init(&mapped_addr[0], &cfg->public_addr, |
Benny Prijono | de47956 | 2007-03-15 10:23:55 +0000 | [diff] [blame] | 339 | (pj_uint16_t)next_rtp_port); |
Benny Prijono | 0a5cad8 | 2006-09-26 13:21:02 +0000 | [diff] [blame] | 340 | if (status != PJ_SUCCESS) |
| 341 | goto on_error; |
| 342 | |
| 343 | status = pj_sockaddr_in_init(&mapped_addr[1], &cfg->public_addr, |
Benny Prijono | de47956 | 2007-03-15 10:23:55 +0000 | [diff] [blame] | 344 | (pj_uint16_t)(next_rtp_port+1)); |
Benny Prijono | 0a5cad8 | 2006-09-26 13:21:02 +0000 | [diff] [blame] | 345 | if (status != PJ_SUCCESS) |
| 346 | goto on_error; |
| 347 | |
| 348 | break; |
| 349 | |
Benny Prijono | eebe9af | 2006-06-13 22:57:13 +0000 | [diff] [blame] | 350 | } else { |
Benny Prijono | eebe9af | 2006-06-13 22:57:13 +0000 | [diff] [blame] | 351 | |
Benny Prijono | 42d08d2 | 2007-12-20 11:23:07 +0000 | [diff] [blame] | 352 | if (bound_addr.sin_addr.s_addr == 0) { |
| 353 | pj_sockaddr addr; |
| 354 | |
| 355 | /* Get local IP address. */ |
| 356 | status = pj_gethostip(pj_AF_INET(), &addr); |
| 357 | if (status != PJ_SUCCESS) |
| 358 | goto on_error; |
| 359 | |
| 360 | bound_addr.sin_addr.s_addr = addr.ipv4.sin_addr.s_addr; |
| 361 | } |
Benny Prijono | eebe9af | 2006-06-13 22:57:13 +0000 | [diff] [blame] | 362 | |
Benny Prijono | fe0b1d6 | 2007-12-05 04:07:37 +0000 | [diff] [blame] | 363 | for (i=0; i<2; ++i) { |
| 364 | pj_sockaddr_in_init(&mapped_addr[i], NULL, 0); |
Benny Prijono | 42d08d2 | 2007-12-20 11:23:07 +0000 | [diff] [blame] | 365 | mapped_addr[i].sin_addr.s_addr = bound_addr.sin_addr.s_addr; |
Benny Prijono | fe0b1d6 | 2007-12-05 04:07:37 +0000 | [diff] [blame] | 366 | } |
Benny Prijono | eebe9af | 2006-06-13 22:57:13 +0000 | [diff] [blame] | 367 | |
Benny Prijono | de47956 | 2007-03-15 10:23:55 +0000 | [diff] [blame] | 368 | mapped_addr[0].sin_port=pj_htons((pj_uint16_t)next_rtp_port); |
| 369 | 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] | 370 | break; |
| 371 | } |
| 372 | } |
| 373 | |
| 374 | if (sock[0] == PJ_INVALID_SOCKET) { |
| 375 | PJ_LOG(1,(THIS_FILE, |
| 376 | "Unable to find appropriate RTP/RTCP ports combination")); |
| 377 | goto on_error; |
| 378 | } |
| 379 | |
| 380 | |
| 381 | skinfo->rtp_sock = sock[0]; |
| 382 | pj_memcpy(&skinfo->rtp_addr_name, |
| 383 | &mapped_addr[0], sizeof(pj_sockaddr_in)); |
| 384 | |
| 385 | skinfo->rtcp_sock = sock[1]; |
| 386 | pj_memcpy(&skinfo->rtcp_addr_name, |
| 387 | &mapped_addr[1], sizeof(pj_sockaddr_in)); |
| 388 | |
Benny Prijono | 8b22ce1 | 2008-02-08 12:57:55 +0000 | [diff] [blame] | 389 | PJ_LOG(4,(THIS_FILE, "RTP socket reachable at %s", |
Benny Prijono | 5186eae | 2007-12-03 14:38:25 +0000 | [diff] [blame] | 390 | pj_sockaddr_print(&skinfo->rtp_addr_name, addr_buf, |
| 391 | sizeof(addr_buf), 3))); |
Benny Prijono | 8b22ce1 | 2008-02-08 12:57:55 +0000 | [diff] [blame] | 392 | PJ_LOG(4,(THIS_FILE, "RTCP socket reachable at %s", |
Benny Prijono | 5186eae | 2007-12-03 14:38:25 +0000 | [diff] [blame] | 393 | pj_sockaddr_print(&skinfo->rtcp_addr_name, addr_buf, |
| 394 | sizeof(addr_buf), 3))); |
Benny Prijono | eebe9af | 2006-06-13 22:57:13 +0000 | [diff] [blame] | 395 | |
Benny Prijono | de47956 | 2007-03-15 10:23:55 +0000 | [diff] [blame] | 396 | next_rtp_port += 2; |
Benny Prijono | eebe9af | 2006-06-13 22:57:13 +0000 | [diff] [blame] | 397 | return PJ_SUCCESS; |
| 398 | |
| 399 | on_error: |
| 400 | for (i=0; i<2; ++i) { |
| 401 | if (sock[i] != PJ_INVALID_SOCKET) |
| 402 | pj_sock_close(sock[i]); |
| 403 | } |
| 404 | return status; |
| 405 | } |
| 406 | |
| 407 | |
| 408 | /* |
| 409 | * Start pjsua media subsystem. |
| 410 | */ |
| 411 | pj_status_t pjsua_media_subsys_start(void) |
| 412 | { |
| 413 | pj_status_t status; |
| 414 | |
| 415 | /* Create media for calls, if none is specified */ |
| 416 | if (pjsua_var.calls[0].med_tp == NULL) { |
| 417 | pjsua_transport_config transport_cfg; |
| 418 | |
| 419 | /* Create default transport config */ |
| 420 | pjsua_transport_config_default(&transport_cfg); |
| 421 | transport_cfg.port = DEFAULT_RTP_PORT; |
| 422 | |
| 423 | status = pjsua_media_transports_create(&transport_cfg); |
| 424 | if (status != PJ_SUCCESS) |
| 425 | return status; |
| 426 | } |
| 427 | |
| 428 | /* Create sound port if none is created yet */ |
Benny Prijono | e909eac | 2006-07-27 22:04:56 +0000 | [diff] [blame] | 429 | if (pjsua_var.snd_port==NULL && pjsua_var.null_snd==NULL && |
| 430 | !pjsua_var.no_snd) |
| 431 | { |
Benny Prijono | eebe9af | 2006-06-13 22:57:13 +0000 | [diff] [blame] | 432 | status = pjsua_set_snd_dev(pjsua_var.cap_dev, pjsua_var.play_dev); |
| 433 | if (status != PJ_SUCCESS) { |
Benny Prijono | d639efa | 2007-04-05 22:45:06 +0000 | [diff] [blame] | 434 | pjsua_perror(THIS_FILE, "Error opening sound device", status); |
| 435 | return status; |
Benny Prijono | eebe9af | 2006-06-13 22:57:13 +0000 | [diff] [blame] | 436 | } |
| 437 | } |
| 438 | |
| 439 | return PJ_SUCCESS; |
| 440 | } |
| 441 | |
| 442 | |
| 443 | /* |
| 444 | * Destroy pjsua media subsystem. |
| 445 | */ |
| 446 | pj_status_t pjsua_media_subsys_destroy(void) |
| 447 | { |
| 448 | unsigned i; |
| 449 | |
| 450 | close_snd_dev(); |
| 451 | |
| 452 | if (pjsua_var.mconf) { |
| 453 | pjmedia_conf_destroy(pjsua_var.mconf); |
| 454 | pjsua_var.mconf = NULL; |
| 455 | } |
| 456 | |
| 457 | if (pjsua_var.null_port) { |
| 458 | pjmedia_port_destroy(pjsua_var.null_port); |
| 459 | pjsua_var.null_port = NULL; |
| 460 | } |
| 461 | |
| 462 | /* Destroy file players */ |
| 463 | for (i=0; i<PJ_ARRAY_SIZE(pjsua_var.player); ++i) { |
| 464 | if (pjsua_var.player[i].port) { |
| 465 | pjmedia_port_destroy(pjsua_var.player[i].port); |
| 466 | pjsua_var.player[i].port = NULL; |
| 467 | } |
| 468 | } |
| 469 | |
| 470 | /* Destroy file recorders */ |
| 471 | for (i=0; i<PJ_ARRAY_SIZE(pjsua_var.recorder); ++i) { |
| 472 | if (pjsua_var.recorder[i].port) { |
| 473 | pjmedia_port_destroy(pjsua_var.recorder[i].port); |
| 474 | pjsua_var.recorder[i].port = NULL; |
| 475 | } |
| 476 | } |
| 477 | |
| 478 | /* Close media transports */ |
Benny Prijono | 0875ae8 | 2006-12-26 00:11:48 +0000 | [diff] [blame] | 479 | for (i=0; i<pjsua_var.ua_cfg.max_calls; ++i) { |
Benny Prijono | eebe9af | 2006-06-13 22:57:13 +0000 | [diff] [blame] | 480 | if (pjsua_var.calls[i].med_tp) { |
| 481 | (*pjsua_var.calls[i].med_tp->op->destroy)(pjsua_var.calls[i].med_tp); |
| 482 | pjsua_var.calls[i].med_tp = NULL; |
| 483 | } |
| 484 | } |
| 485 | |
| 486 | /* Destroy media endpoint. */ |
| 487 | if (pjsua_var.med_endpt) { |
| 488 | |
| 489 | /* Shutdown all codecs: */ |
| 490 | # if PJMEDIA_HAS_SPEEX_CODEC |
| 491 | pjmedia_codec_speex_deinit(); |
| 492 | # endif /* PJMEDIA_HAS_SPEEX_CODEC */ |
| 493 | |
| 494 | # if PJMEDIA_HAS_GSM_CODEC |
| 495 | pjmedia_codec_gsm_deinit(); |
| 496 | # endif /* PJMEDIA_HAS_GSM_CODEC */ |
| 497 | |
| 498 | # if PJMEDIA_HAS_G711_CODEC |
| 499 | pjmedia_codec_g711_deinit(); |
| 500 | # endif /* PJMEDIA_HAS_G711_CODEC */ |
| 501 | |
| 502 | # if PJMEDIA_HAS_L16_CODEC |
| 503 | pjmedia_codec_l16_deinit(); |
| 504 | # endif /* PJMEDIA_HAS_L16_CODEC */ |
| 505 | |
| 506 | |
| 507 | pjmedia_endpt_destroy(pjsua_var.med_endpt); |
| 508 | pjsua_var.med_endpt = NULL; |
Benny Prijono | eebe9af | 2006-06-13 22:57:13 +0000 | [diff] [blame] | 509 | |
Benny Prijono | ad2e0ca | 2007-04-29 12:31:51 +0000 | [diff] [blame] | 510 | /* Deinitialize sound subsystem */ |
Benny Prijono | a41d66e | 2007-10-01 12:17:23 +0000 | [diff] [blame] | 511 | // Not necessary, as pjmedia_snd_deinit() should have been called |
| 512 | // in pjmedia_endpt_destroy(). |
| 513 | //pjmedia_snd_deinit(); |
Benny Prijono | ad2e0ca | 2007-04-29 12:31:51 +0000 | [diff] [blame] | 514 | } |
Benny Prijono | af1bb1e | 2006-11-21 12:39:31 +0000 | [diff] [blame] | 515 | |
Benny Prijono | de47956 | 2007-03-15 10:23:55 +0000 | [diff] [blame] | 516 | /* Reset RTP port */ |
| 517 | next_rtp_port = 0; |
| 518 | |
Benny Prijono | eebe9af | 2006-06-13 22:57:13 +0000 | [diff] [blame] | 519 | return PJ_SUCCESS; |
| 520 | } |
| 521 | |
| 522 | |
Benny Prijono | c97608e | 2007-03-23 16:34:20 +0000 | [diff] [blame] | 523 | /* Create normal UDP media transports */ |
| 524 | static pj_status_t create_udp_media_transports(pjsua_transport_config *cfg) |
Benny Prijono | eebe9af | 2006-06-13 22:57:13 +0000 | [diff] [blame] | 525 | { |
Benny Prijono | eebe9af | 2006-06-13 22:57:13 +0000 | [diff] [blame] | 526 | unsigned i; |
Benny Prijono | 617c5bc | 2007-04-02 19:51:21 +0000 | [diff] [blame] | 527 | pjmedia_sock_info skinfo; |
Benny Prijono | eebe9af | 2006-06-13 22:57:13 +0000 | [diff] [blame] | 528 | pj_status_t status; |
| 529 | |
Benny Prijono | eebe9af | 2006-06-13 22:57:13 +0000 | [diff] [blame] | 530 | /* Create each media transport */ |
| 531 | for (i=0; i<pjsua_var.ua_cfg.max_calls; ++i) { |
| 532 | |
Benny Prijono | 617c5bc | 2007-04-02 19:51:21 +0000 | [diff] [blame] | 533 | status = create_rtp_rtcp_sock(cfg, &skinfo); |
Benny Prijono | eebe9af | 2006-06-13 22:57:13 +0000 | [diff] [blame] | 534 | if (status != PJ_SUCCESS) { |
| 535 | pjsua_perror(THIS_FILE, "Unable to create RTP/RTCP socket", |
| 536 | status); |
| 537 | goto on_error; |
| 538 | } |
Benny Prijono | d817965 | 2008-01-23 20:39:07 +0000 | [diff] [blame] | 539 | |
Benny Prijono | eebe9af | 2006-06-13 22:57:13 +0000 | [diff] [blame] | 540 | status = pjmedia_transport_udp_attach(pjsua_var.med_endpt, NULL, |
Benny Prijono | 617c5bc | 2007-04-02 19:51:21 +0000 | [diff] [blame] | 541 | &skinfo, 0, |
Benny Prijono | eebe9af | 2006-06-13 22:57:13 +0000 | [diff] [blame] | 542 | &pjsua_var.calls[i].med_tp); |
| 543 | if (status != PJ_SUCCESS) { |
| 544 | pjsua_perror(THIS_FILE, "Unable to create media transport", |
| 545 | status); |
| 546 | goto on_error; |
| 547 | } |
Benny Prijono | 00cae61 | 2006-07-31 15:19:36 +0000 | [diff] [blame] | 548 | |
Benny Prijono | d817965 | 2008-01-23 20:39:07 +0000 | [diff] [blame] | 549 | pjmedia_transport_simulate_lost(pjsua_var.calls[i].med_tp, |
| 550 | PJMEDIA_DIR_ENCODING, |
| 551 | pjsua_var.media_cfg.tx_drop_pct); |
Benny Prijono | 00cae61 | 2006-07-31 15:19:36 +0000 | [diff] [blame] | 552 | |
Benny Prijono | d817965 | 2008-01-23 20:39:07 +0000 | [diff] [blame] | 553 | pjmedia_transport_simulate_lost(pjsua_var.calls[i].med_tp, |
| 554 | PJMEDIA_DIR_DECODING, |
| 555 | pjsua_var.media_cfg.rx_drop_pct); |
Benny Prijono | 00cae61 | 2006-07-31 15:19:36 +0000 | [diff] [blame] | 556 | |
Benny Prijono | eebe9af | 2006-06-13 22:57:13 +0000 | [diff] [blame] | 557 | } |
| 558 | |
Benny Prijono | c97608e | 2007-03-23 16:34:20 +0000 | [diff] [blame] | 559 | return PJ_SUCCESS; |
| 560 | |
| 561 | on_error: |
| 562 | for (i=0; i<pjsua_var.ua_cfg.max_calls; ++i) { |
| 563 | if (pjsua_var.calls[i].med_tp != NULL) { |
| 564 | pjmedia_transport_close(pjsua_var.calls[i].med_tp); |
| 565 | pjsua_var.calls[i].med_tp = NULL; |
| 566 | } |
| 567 | } |
| 568 | |
| 569 | return status; |
| 570 | } |
| 571 | |
| 572 | |
Benny Prijono | 096c56c | 2007-09-15 08:30:16 +0000 | [diff] [blame] | 573 | /* This callback is called when ICE negotiation completes */ |
| 574 | static void on_ice_complete(pjmedia_transport *tp, pj_status_t result) |
| 575 | { |
| 576 | unsigned id, c; |
| 577 | pj_bool_t found = PJ_FALSE; |
| 578 | |
| 579 | /* We're only interested with failure case */ |
| 580 | if (result == PJ_SUCCESS) |
| 581 | return; |
| 582 | |
| 583 | /* Find call which has this media transport */ |
| 584 | |
| 585 | PJSUA_LOCK(); |
| 586 | |
| 587 | for (id=0, c=0; id<PJSUA_MAX_CALLS && c<pjsua_var.call_cnt; ++id) { |
| 588 | pjsua_call *call = &pjsua_var.calls[id]; |
| 589 | if (call->inv) { |
| 590 | ++c; |
| 591 | |
| 592 | if (call->med_tp == tp) { |
| 593 | call->media_st = PJSUA_CALL_MEDIA_ERROR; |
| 594 | call->media_dir = PJMEDIA_DIR_NONE; |
| 595 | found = PJ_TRUE; |
| 596 | break; |
| 597 | } |
| 598 | } |
| 599 | } |
| 600 | |
| 601 | PJSUA_UNLOCK(); |
| 602 | |
| 603 | if (found && pjsua_var.ua_cfg.cb.on_call_media_state) { |
| 604 | pjsua_var.ua_cfg.cb.on_call_media_state(id); |
| 605 | } |
| 606 | } |
| 607 | |
| 608 | |
Benny Prijono | c97608e | 2007-03-23 16:34:20 +0000 | [diff] [blame] | 609 | /* Create ICE media transports (when ice is enabled) */ |
| 610 | static pj_status_t create_ice_media_transports(pjsua_transport_config *cfg) |
| 611 | { |
| 612 | unsigned i; |
Benny Prijono | b681a2f | 2007-03-25 18:44:51 +0000 | [diff] [blame] | 613 | pj_sockaddr_in addr; |
Benny Prijono | c97608e | 2007-03-23 16:34:20 +0000 | [diff] [blame] | 614 | pj_status_t status; |
| 615 | |
Benny Prijono | da9785b | 2007-04-02 20:43:06 +0000 | [diff] [blame] | 616 | /* Make sure STUN server resolution has completed */ |
| 617 | status = pjsua_resolve_stun_server(PJ_TRUE); |
| 618 | if (status != PJ_SUCCESS) { |
| 619 | pjsua_perror(THIS_FILE, "Error resolving STUN server", status); |
| 620 | return status; |
| 621 | } |
| 622 | |
Benny Prijono | b681a2f | 2007-03-25 18:44:51 +0000 | [diff] [blame] | 623 | pj_sockaddr_in_init(&addr, 0, (pj_uint16_t)cfg->port); |
| 624 | |
Benny Prijono | c97608e | 2007-03-23 16:34:20 +0000 | [diff] [blame] | 625 | /* Create each media transport */ |
| 626 | for (i=0; i<pjsua_var.ua_cfg.max_calls; ++i) { |
Benny Prijono | a6bd758 | 2007-03-28 15:49:48 +0000 | [diff] [blame] | 627 | pj_ice_strans_comp comp; |
Benny Prijono | 096c56c | 2007-09-15 08:30:16 +0000 | [diff] [blame] | 628 | pjmedia_ice_cb ice_cb; |
Benny Prijono | b681a2f | 2007-03-25 18:44:51 +0000 | [diff] [blame] | 629 | int next_port; |
Benny Prijono | 38fb3ea | 2008-01-02 08:27:03 +0000 | [diff] [blame] | 630 | char name[32]; |
Benny Prijono | dc1fe22 | 2007-06-26 10:13:13 +0000 | [diff] [blame] | 631 | #if PJMEDIA_ADVERTISE_RTCP |
| 632 | enum { COMP_CNT=2 }; |
| 633 | #else |
| 634 | enum { COMP_CNT=1 }; |
| 635 | #endif |
Benny Prijono | c97608e | 2007-03-23 16:34:20 +0000 | [diff] [blame] | 636 | |
Benny Prijono | 096c56c | 2007-09-15 08:30:16 +0000 | [diff] [blame] | 637 | pj_bzero(&ice_cb, sizeof(pjmedia_ice_cb)); |
| 638 | ice_cb.on_ice_complete = &on_ice_complete; |
| 639 | |
Benny Prijono | 38fb3ea | 2008-01-02 08:27:03 +0000 | [diff] [blame] | 640 | pj_ansi_snprintf(name, sizeof(name), "icetp%02d", i); |
| 641 | |
| 642 | status = pjmedia_ice_create(pjsua_var.med_endpt, name, COMP_CNT, |
Benny Prijono | 096c56c | 2007-09-15 08:30:16 +0000 | [diff] [blame] | 643 | &pjsua_var.stun_cfg, &ice_cb, |
Benny Prijono | c97608e | 2007-03-23 16:34:20 +0000 | [diff] [blame] | 644 | &pjsua_var.calls[i].med_tp); |
| 645 | if (status != PJ_SUCCESS) { |
| 646 | pjsua_perror(THIS_FILE, "Unable to create ICE media transport", |
| 647 | status); |
| 648 | goto on_error; |
| 649 | } |
| 650 | |
Benny Prijono | d817965 | 2008-01-23 20:39:07 +0000 | [diff] [blame] | 651 | pjmedia_transport_simulate_lost(pjsua_var.calls[i].med_tp, |
| 652 | PJMEDIA_DIR_ENCODING, |
| 653 | pjsua_var.media_cfg.tx_drop_pct); |
Benny Prijono | 11da9bc | 2007-09-15 08:55:00 +0000 | [diff] [blame] | 654 | |
Benny Prijono | d817965 | 2008-01-23 20:39:07 +0000 | [diff] [blame] | 655 | pjmedia_transport_simulate_lost(pjsua_var.calls[i].med_tp, |
| 656 | PJMEDIA_DIR_DECODING, |
| 657 | pjsua_var.media_cfg.rx_drop_pct); |
Benny Prijono | 11da9bc | 2007-09-15 08:55:00 +0000 | [diff] [blame] | 658 | |
Benny Prijono | b681a2f | 2007-03-25 18:44:51 +0000 | [diff] [blame] | 659 | status = pjmedia_ice_start_init(pjsua_var.calls[i].med_tp, 0, &addr, |
| 660 | &pjsua_var.stun_srv.ipv4, NULL); |
Benny Prijono | c97608e | 2007-03-23 16:34:20 +0000 | [diff] [blame] | 661 | if (status != PJ_SUCCESS) { |
Benny Prijono | b681a2f | 2007-03-25 18:44:51 +0000 | [diff] [blame] | 662 | pjsua_perror(THIS_FILE, "Error starting ICE transport", |
Benny Prijono | c97608e | 2007-03-23 16:34:20 +0000 | [diff] [blame] | 663 | status); |
| 664 | goto on_error; |
| 665 | } |
| 666 | |
Benny Prijono | b681a2f | 2007-03-25 18:44:51 +0000 | [diff] [blame] | 667 | pjmedia_ice_get_comp(pjsua_var.calls[i].med_tp, 1, &comp); |
| 668 | next_port = pj_ntohs(comp.local_addr.ipv4.sin_port); |
| 669 | next_port += 2; |
| 670 | addr.sin_port = pj_htons((pj_uint16_t)next_port); |
Benny Prijono | c97608e | 2007-03-23 16:34:20 +0000 | [diff] [blame] | 671 | } |
| 672 | |
| 673 | /* Wait until all ICE transports are ready */ |
| 674 | for (i=0; i<pjsua_var.ua_cfg.max_calls; ++i) { |
Benny Prijono | c97608e | 2007-03-23 16:34:20 +0000 | [diff] [blame] | 675 | |
| 676 | /* Wait until interface status is PJ_SUCCESS */ |
| 677 | for (;;) { |
Benny Prijono | b681a2f | 2007-03-25 18:44:51 +0000 | [diff] [blame] | 678 | status = pjmedia_ice_get_init_status(pjsua_var.calls[i].med_tp); |
Benny Prijono | c97608e | 2007-03-23 16:34:20 +0000 | [diff] [blame] | 679 | if (status == PJ_EPENDING) |
| 680 | pjsua_handle_events(100); |
| 681 | else |
| 682 | break; |
| 683 | } |
| 684 | |
| 685 | if (status != PJ_SUCCESS) { |
| 686 | pjsua_perror(THIS_FILE, |
| 687 | "Error adding STUN address to ICE media transport", |
| 688 | status); |
| 689 | goto on_error; |
| 690 | } |
| 691 | |
Benny Prijono | c97608e | 2007-03-23 16:34:20 +0000 | [diff] [blame] | 692 | } |
Benny Prijono | eebe9af | 2006-06-13 22:57:13 +0000 | [diff] [blame] | 693 | |
| 694 | return PJ_SUCCESS; |
| 695 | |
| 696 | on_error: |
| 697 | for (i=0; i<pjsua_var.ua_cfg.max_calls; ++i) { |
| 698 | if (pjsua_var.calls[i].med_tp != NULL) { |
Benny Prijono | c97608e | 2007-03-23 16:34:20 +0000 | [diff] [blame] | 699 | pjmedia_transport_close(pjsua_var.calls[i].med_tp); |
Benny Prijono | eebe9af | 2006-06-13 22:57:13 +0000 | [diff] [blame] | 700 | pjsua_var.calls[i].med_tp = NULL; |
| 701 | } |
| 702 | } |
| 703 | |
Benny Prijono | c97608e | 2007-03-23 16:34:20 +0000 | [diff] [blame] | 704 | return status; |
| 705 | } |
| 706 | |
| 707 | |
| 708 | /* |
| 709 | * Create UDP media transports for all the calls. This function creates |
| 710 | * one UDP media transport for each call. |
| 711 | */ |
Benny Prijono | 1f61a8f | 2007-08-16 10:11:44 +0000 | [diff] [blame] | 712 | PJ_DEF(pj_status_t) pjsua_media_transports_create( |
| 713 | const pjsua_transport_config *app_cfg) |
Benny Prijono | c97608e | 2007-03-23 16:34:20 +0000 | [diff] [blame] | 714 | { |
| 715 | pjsua_transport_config cfg; |
| 716 | unsigned i; |
| 717 | pj_status_t status; |
| 718 | |
| 719 | |
| 720 | /* Make sure pjsua_init() has been called */ |
| 721 | PJ_ASSERT_RETURN(pjsua_var.ua_cfg.max_calls>0, PJ_EINVALIDOP); |
| 722 | |
| 723 | PJSUA_LOCK(); |
| 724 | |
| 725 | /* Delete existing media transports */ |
| 726 | for (i=0; i<pjsua_var.ua_cfg.max_calls; ++i) { |
| 727 | if (pjsua_var.calls[i].med_tp != NULL) { |
| 728 | pjmedia_transport_close(pjsua_var.calls[i].med_tp); |
| 729 | pjsua_var.calls[i].med_tp = NULL; |
| 730 | } |
| 731 | } |
| 732 | |
| 733 | /* Copy config */ |
| 734 | pjsua_transport_config_dup(pjsua_var.pool, &cfg, app_cfg); |
| 735 | |
| 736 | if (pjsua_var.media_cfg.enable_ice) { |
| 737 | status = create_ice_media_transports(&cfg); |
| 738 | } else { |
| 739 | status = create_udp_media_transports(&cfg); |
| 740 | } |
| 741 | |
| 742 | |
Benny Prijono | eebe9af | 2006-06-13 22:57:13 +0000 | [diff] [blame] | 743 | PJSUA_UNLOCK(); |
| 744 | |
| 745 | return status; |
| 746 | } |
| 747 | |
| 748 | |
Benny Prijono | c97608e | 2007-03-23 16:34:20 +0000 | [diff] [blame] | 749 | pj_status_t pjsua_media_channel_init(pjsua_call_id call_id, |
Benny Prijono | d817965 | 2008-01-23 20:39:07 +0000 | [diff] [blame] | 750 | pjsip_role_e role, |
Benny Prijono | 25b2ea1 | 2008-01-24 19:20:54 +0000 | [diff] [blame] | 751 | int security_level, |
| 752 | int *sip_err_code) |
Benny Prijono | c97608e | 2007-03-23 16:34:20 +0000 | [diff] [blame] | 753 | { |
| 754 | pjsua_call *call = &pjsua_var.calls[call_id]; |
| 755 | |
Benny Prijono | d817965 | 2008-01-23 20:39:07 +0000 | [diff] [blame] | 756 | #if defined(PJMEDIA_HAS_SRTP) && (PJMEDIA_HAS_SRTP != 0) |
| 757 | pjsua_acc *acc = &pjsua_var.acc[call->acc_id]; |
| 758 | pjmedia_srtp_setting srtp_opt; |
| 759 | pjmedia_transport *srtp; |
| 760 | pj_status_t status; |
| 761 | #endif |
Benny Prijono | c97608e | 2007-03-23 16:34:20 +0000 | [diff] [blame] | 762 | |
Benny Prijono | d817965 | 2008-01-23 20:39:07 +0000 | [diff] [blame] | 763 | PJ_UNUSED_ARG(role); |
Benny Prijono | c97608e | 2007-03-23 16:34:20 +0000 | [diff] [blame] | 764 | |
Benny Prijono | d817965 | 2008-01-23 20:39:07 +0000 | [diff] [blame] | 765 | /* Return error if media transport has not been created yet |
| 766 | * (e.g. application is starting) |
| 767 | */ |
| 768 | if (call->med_tp == NULL) { |
| 769 | return PJ_EBUSY; |
Benny Prijono | c97608e | 2007-03-23 16:34:20 +0000 | [diff] [blame] | 770 | } |
| 771 | |
Benny Prijono | d817965 | 2008-01-23 20:39:07 +0000 | [diff] [blame] | 772 | /* Stop media transport (for good measure!) */ |
| 773 | pjmedia_transport_media_stop(call->med_tp); |
| 774 | |
| 775 | #if defined(PJMEDIA_HAS_SRTP) && (PJMEDIA_HAS_SRTP != 0) |
| 776 | /* Check if SRTP requires secure signaling */ |
| 777 | if (acc->cfg.use_srtp != PJMEDIA_SRTP_DISABLED) { |
| 778 | if (security_level < acc->cfg.srtp_secure_signaling) { |
Benny Prijono | 25b2ea1 | 2008-01-24 19:20:54 +0000 | [diff] [blame] | 779 | if (sip_err_code) |
| 780 | *sip_err_code = PJSIP_SC_NOT_ACCEPTABLE; |
Benny Prijono | d817965 | 2008-01-23 20:39:07 +0000 | [diff] [blame] | 781 | return PJSIP_ESESSIONINSECURE; |
| 782 | } |
| 783 | } |
| 784 | |
| 785 | /* Always create SRTP adapter */ |
| 786 | pjmedia_srtp_setting_default(&srtp_opt); |
| 787 | srtp_opt.close_member_tp = PJ_FALSE; |
| 788 | srtp_opt.use = acc->cfg.use_srtp; |
| 789 | status = pjmedia_transport_srtp_create(pjsua_var.med_endpt, |
| 790 | call->med_tp, |
| 791 | &srtp_opt, &srtp); |
Benny Prijono | 25b2ea1 | 2008-01-24 19:20:54 +0000 | [diff] [blame] | 792 | if (status != PJ_SUCCESS) { |
| 793 | if (sip_err_code) |
| 794 | *sip_err_code = PJSIP_SC_INTERNAL_SERVER_ERROR; |
Benny Prijono | d817965 | 2008-01-23 20:39:07 +0000 | [diff] [blame] | 795 | return status; |
Benny Prijono | 25b2ea1 | 2008-01-24 19:20:54 +0000 | [diff] [blame] | 796 | } |
Benny Prijono | d817965 | 2008-01-23 20:39:07 +0000 | [diff] [blame] | 797 | |
| 798 | /* Set SRTP as current media transport */ |
| 799 | call->med_orig = call->med_tp; |
| 800 | call->med_tp = srtp; |
| 801 | #else |
| 802 | call->med_orig = call->med_tp; |
| 803 | PJ_UNUSED_ARG(security_level); |
| 804 | #endif |
| 805 | |
Benny Prijono | c97608e | 2007-03-23 16:34:20 +0000 | [diff] [blame] | 806 | return PJ_SUCCESS; |
| 807 | } |
| 808 | |
| 809 | pj_status_t pjsua_media_channel_create_sdp(pjsua_call_id call_id, |
| 810 | pj_pool_t *pool, |
Benny Prijono | d817965 | 2008-01-23 20:39:07 +0000 | [diff] [blame] | 811 | const pjmedia_sdp_session *rem_sdp, |
Benny Prijono | 25b2ea1 | 2008-01-24 19:20:54 +0000 | [diff] [blame] | 812 | pjmedia_sdp_session **p_sdp, |
| 813 | int *sip_status_code) |
Benny Prijono | c97608e | 2007-03-23 16:34:20 +0000 | [diff] [blame] | 814 | { |
Benny Prijono | d817965 | 2008-01-23 20:39:07 +0000 | [diff] [blame] | 815 | enum { MAX_MEDIA = 1, MEDIA_IDX = 0 }; |
Benny Prijono | c97608e | 2007-03-23 16:34:20 +0000 | [diff] [blame] | 816 | pjmedia_sdp_session *sdp; |
Benny Prijono | 617c5bc | 2007-04-02 19:51:21 +0000 | [diff] [blame] | 817 | pjmedia_sock_info skinfo; |
Benny Prijono | c97608e | 2007-03-23 16:34:20 +0000 | [diff] [blame] | 818 | pjsua_call *call = &pjsua_var.calls[call_id]; |
| 819 | pj_status_t status; |
| 820 | |
Benny Prijono | 55e8235 | 2007-05-10 20:49:08 +0000 | [diff] [blame] | 821 | /* Return error if media transport has not been created yet |
| 822 | * (e.g. application is starting) |
| 823 | */ |
| 824 | if (call->med_tp == NULL) { |
| 825 | return PJ_EBUSY; |
| 826 | } |
| 827 | |
Benny Prijono | 617c5bc | 2007-04-02 19:51:21 +0000 | [diff] [blame] | 828 | /* Get media socket info */ |
| 829 | pjmedia_transport_get_info(call->med_tp, &skinfo); |
| 830 | |
| 831 | /* Create SDP */ |
Benny Prijono | d817965 | 2008-01-23 20:39:07 +0000 | [diff] [blame] | 832 | status = pjmedia_endpt_create_sdp(pjsua_var.med_endpt, pool, MAX_MEDIA, |
Benny Prijono | 617c5bc | 2007-04-02 19:51:21 +0000 | [diff] [blame] | 833 | &skinfo, &sdp); |
Benny Prijono | 25b2ea1 | 2008-01-24 19:20:54 +0000 | [diff] [blame] | 834 | if (status != PJ_SUCCESS) { |
| 835 | if (sip_status_code) *sip_status_code = 500; |
Benny Prijono | c97608e | 2007-03-23 16:34:20 +0000 | [diff] [blame] | 836 | goto on_error; |
Benny Prijono | 25b2ea1 | 2008-01-24 19:20:54 +0000 | [diff] [blame] | 837 | } |
Benny Prijono | c97608e | 2007-03-23 16:34:20 +0000 | [diff] [blame] | 838 | |
Benny Prijono | 6ba8c54 | 2007-10-16 01:34:14 +0000 | [diff] [blame] | 839 | /* Add NAT info in the SDP */ |
| 840 | if (pjsua_var.ua_cfg.nat_type_in_sdp) { |
| 841 | pjmedia_sdp_attr *a; |
| 842 | pj_str_t value; |
| 843 | char nat_info[80]; |
| 844 | |
| 845 | value.ptr = nat_info; |
| 846 | if (pjsua_var.ua_cfg.nat_type_in_sdp == 1) { |
| 847 | value.slen = pj_ansi_snprintf(nat_info, sizeof(nat_info), |
| 848 | "%d", pjsua_var.nat_type); |
| 849 | } else { |
| 850 | const char *type_name = pj_stun_get_nat_name(pjsua_var.nat_type); |
| 851 | value.slen = pj_ansi_snprintf(nat_info, sizeof(nat_info), |
| 852 | "%d %s", |
| 853 | pjsua_var.nat_type, |
| 854 | type_name); |
| 855 | } |
| 856 | |
| 857 | a = pjmedia_sdp_attr_create(pool, "X-nat", &value); |
| 858 | |
| 859 | pjmedia_sdp_attr_add(&sdp->attr_count, sdp->attr, a); |
| 860 | |
| 861 | } |
| 862 | |
Benny Prijono | d817965 | 2008-01-23 20:39:07 +0000 | [diff] [blame] | 863 | /* Give the SDP to media transport */ |
Benny Prijono | 2dbed82 | 2008-02-21 10:08:27 +0000 | [diff] [blame] | 864 | status = pjmedia_transport_media_create(call->med_tp, pool, 0, |
Benny Prijono | d817965 | 2008-01-23 20:39:07 +0000 | [diff] [blame] | 865 | sdp, rem_sdp, MEDIA_IDX); |
Benny Prijono | 25b2ea1 | 2008-01-24 19:20:54 +0000 | [diff] [blame] | 866 | if (status != PJ_SUCCESS) { |
| 867 | if (sip_status_code) *sip_status_code = PJSIP_SC_NOT_ACCEPTABLE; |
Benny Prijono | d817965 | 2008-01-23 20:39:07 +0000 | [diff] [blame] | 868 | goto on_error; |
Benny Prijono | 25b2ea1 | 2008-01-24 19:20:54 +0000 | [diff] [blame] | 869 | } |
Benny Prijono | c97608e | 2007-03-23 16:34:20 +0000 | [diff] [blame] | 870 | |
| 871 | *p_sdp = sdp; |
| 872 | return PJ_SUCCESS; |
| 873 | |
| 874 | on_error: |
| 875 | pjsua_media_channel_deinit(call_id); |
| 876 | return status; |
| 877 | |
| 878 | } |
| 879 | |
| 880 | |
| 881 | static void stop_media_session(pjsua_call_id call_id) |
| 882 | { |
| 883 | pjsua_call *call = &pjsua_var.calls[call_id]; |
| 884 | |
| 885 | if (call->conf_slot != PJSUA_INVALID_ID) { |
| 886 | pjmedia_conf_remove_port(pjsua_var.mconf, call->conf_slot); |
| 887 | call->conf_slot = PJSUA_INVALID_ID; |
| 888 | } |
| 889 | |
| 890 | if (call->session) { |
Benny Prijono | fc13bf6 | 2008-02-20 08:56:15 +0000 | [diff] [blame] | 891 | if (pjsua_var.ua_cfg.cb.on_stream_destroyed) { |
| 892 | pjsua_var.ua_cfg.cb.on_stream_destroyed(call_id, call->session, 0); |
| 893 | } |
| 894 | |
Benny Prijono | c97608e | 2007-03-23 16:34:20 +0000 | [diff] [blame] | 895 | pjmedia_session_destroy(call->session); |
| 896 | call->session = NULL; |
| 897 | |
| 898 | PJ_LOG(4,(THIS_FILE, "Media session for call %d is destroyed", |
| 899 | call_id)); |
| 900 | |
| 901 | } |
| 902 | |
| 903 | call->media_st = PJSUA_CALL_MEDIA_NONE; |
| 904 | } |
| 905 | |
| 906 | pj_status_t pjsua_media_channel_deinit(pjsua_call_id call_id) |
| 907 | { |
| 908 | pjsua_call *call = &pjsua_var.calls[call_id]; |
| 909 | |
| 910 | stop_media_session(call_id); |
| 911 | |
Benny Prijono | d817965 | 2008-01-23 20:39:07 +0000 | [diff] [blame] | 912 | pjmedia_transport_media_stop(call->med_tp); |
Benny Prijono | c97608e | 2007-03-23 16:34:20 +0000 | [diff] [blame] | 913 | |
Benny Prijono | d817965 | 2008-01-23 20:39:07 +0000 | [diff] [blame] | 914 | if (call->med_tp != call->med_orig) { |
| 915 | pjmedia_transport_close(call->med_tp); |
| 916 | call->med_tp = call->med_orig; |
| 917 | } |
Benny Prijono | c97608e | 2007-03-23 16:34:20 +0000 | [diff] [blame] | 918 | return PJ_SUCCESS; |
| 919 | } |
| 920 | |
| 921 | |
| 922 | /* |
| 923 | * DTMF callback from the stream. |
| 924 | */ |
| 925 | static void dtmf_callback(pjmedia_stream *strm, void *user_data, |
| 926 | int digit) |
| 927 | { |
| 928 | PJ_UNUSED_ARG(strm); |
| 929 | |
Benny Prijono | 0c06826 | 2008-02-14 14:38:52 +0000 | [diff] [blame] | 930 | /* For discussions about call mutex protection related to this |
| 931 | * callback, please see ticket #460: |
| 932 | * http://trac.pjsip.org/repos/ticket/460#comment:4 |
| 933 | */ |
Benny Prijono | c97608e | 2007-03-23 16:34:20 +0000 | [diff] [blame] | 934 | if (pjsua_var.ua_cfg.cb.on_dtmf_digit) { |
| 935 | pjsua_call_id call_id; |
| 936 | |
Benny Prijono | d817965 | 2008-01-23 20:39:07 +0000 | [diff] [blame] | 937 | call_id = (pjsua_call_id)(long)user_data; |
Benny Prijono | c97608e | 2007-03-23 16:34:20 +0000 | [diff] [blame] | 938 | pjsua_var.ua_cfg.cb.on_dtmf_digit(call_id, digit); |
| 939 | } |
| 940 | } |
| 941 | |
| 942 | |
| 943 | pj_status_t pjsua_media_channel_update(pjsua_call_id call_id, |
Benny Prijono | d817965 | 2008-01-23 20:39:07 +0000 | [diff] [blame] | 944 | pjmedia_sdp_session *local_sdp, |
Benny Prijono | dbce2cf | 2007-03-28 16:24:00 +0000 | [diff] [blame] | 945 | const pjmedia_sdp_session *remote_sdp) |
Benny Prijono | c97608e | 2007-03-23 16:34:20 +0000 | [diff] [blame] | 946 | { |
Benny Prijono | 91e567e | 2007-12-28 08:51:58 +0000 | [diff] [blame] | 947 | unsigned i; |
Benny Prijono | c97608e | 2007-03-23 16:34:20 +0000 | [diff] [blame] | 948 | int prev_media_st = 0; |
| 949 | pjsua_call *call = &pjsua_var.calls[call_id]; |
| 950 | pjmedia_session_info sess_info; |
Benny Prijono | 91e567e | 2007-12-28 08:51:58 +0000 | [diff] [blame] | 951 | pjmedia_stream_info *si = NULL; |
Benny Prijono | c97608e | 2007-03-23 16:34:20 +0000 | [diff] [blame] | 952 | pjmedia_port *media_port; |
Benny Prijono | c97608e | 2007-03-23 16:34:20 +0000 | [diff] [blame] | 953 | pj_status_t status; |
| 954 | |
| 955 | /* Destroy existing media session, if any. */ |
| 956 | prev_media_st = call->media_st; |
| 957 | stop_media_session(call->index); |
| 958 | |
| 959 | /* Create media session info based on SDP parameters. |
Benny Prijono | c97608e | 2007-03-23 16:34:20 +0000 | [diff] [blame] | 960 | */ |
| 961 | status = pjmedia_session_info_from_sdp( call->inv->dlg->pool, |
| 962 | pjsua_var.med_endpt, |
Benny Prijono | 91e567e | 2007-12-28 08:51:58 +0000 | [diff] [blame] | 963 | PJMEDIA_MAX_SDP_MEDIA, &sess_info, |
Benny Prijono | c97608e | 2007-03-23 16:34:20 +0000 | [diff] [blame] | 964 | local_sdp, remote_sdp); |
| 965 | if (status != PJ_SUCCESS) |
| 966 | return status; |
| 967 | |
Benny Prijono | 91e567e | 2007-12-28 08:51:58 +0000 | [diff] [blame] | 968 | /* Find which session is audio (we only support audio for now) */ |
| 969 | for (i=0; i < sess_info.stream_cnt; ++i) { |
| 970 | if (sess_info.stream_info[i].type == PJMEDIA_TYPE_AUDIO && |
Benny Prijono | d817965 | 2008-01-23 20:39:07 +0000 | [diff] [blame] | 971 | (sess_info.stream_info[i].proto == PJMEDIA_TP_PROTO_RTP_AVP || |
| 972 | sess_info.stream_info[i].proto == PJMEDIA_TP_PROTO_RTP_SAVP)) |
Benny Prijono | 91e567e | 2007-12-28 08:51:58 +0000 | [diff] [blame] | 973 | { |
| 974 | si = &sess_info.stream_info[i]; |
| 975 | break; |
| 976 | } |
| 977 | } |
| 978 | |
| 979 | if (si == NULL) { |
| 980 | /* Not found */ |
| 981 | return PJMEDIA_EINVALIMEDIATYPE; |
| 982 | } |
| 983 | |
| 984 | |
| 985 | /* Reset session info with only one media stream */ |
| 986 | sess_info.stream_cnt = 1; |
| 987 | if (si != &sess_info.stream_info[0]) |
| 988 | pj_memcpy(&sess_info.stream_info[0], si, sizeof(pjmedia_stream_info)); |
Benny Prijono | c97608e | 2007-03-23 16:34:20 +0000 | [diff] [blame] | 989 | |
| 990 | /* Check if media is put on-hold */ |
Benny Prijono | 91e567e | 2007-12-28 08:51:58 +0000 | [diff] [blame] | 991 | if (sess_info.stream_cnt == 0 || si->dir == PJMEDIA_DIR_NONE) |
Benny Prijono | c97608e | 2007-03-23 16:34:20 +0000 | [diff] [blame] | 992 | { |
| 993 | |
| 994 | /* Determine who puts the call on-hold */ |
| 995 | if (prev_media_st == PJSUA_CALL_MEDIA_ACTIVE) { |
| 996 | if (pjmedia_sdp_neg_was_answer_remote(call->inv->neg)) { |
| 997 | /* It was local who offer hold */ |
| 998 | call->media_st = PJSUA_CALL_MEDIA_LOCAL_HOLD; |
| 999 | } else { |
| 1000 | call->media_st = PJSUA_CALL_MEDIA_REMOTE_HOLD; |
| 1001 | } |
| 1002 | } |
| 1003 | |
| 1004 | call->media_dir = PJMEDIA_DIR_NONE; |
| 1005 | |
Benny Prijono | d817965 | 2008-01-23 20:39:07 +0000 | [diff] [blame] | 1006 | /* Shutdown transport's session */ |
| 1007 | pjmedia_transport_media_stop(call->med_tp); |
Benny Prijono | 667952e | 2007-04-02 19:27:54 +0000 | [diff] [blame] | 1008 | |
Benny Prijono | c97608e | 2007-03-23 16:34:20 +0000 | [diff] [blame] | 1009 | /* No need because we need keepalive? */ |
| 1010 | |
| 1011 | } else { |
Benny Prijono | d817965 | 2008-01-23 20:39:07 +0000 | [diff] [blame] | 1012 | /* Start media transport */ |
| 1013 | status = pjmedia_transport_media_start(call->med_tp, |
| 1014 | call->inv->pool, |
| 1015 | local_sdp, remote_sdp, 0); |
| 1016 | if (status != PJ_SUCCESS) |
| 1017 | return status; |
Benny Prijono | c97608e | 2007-03-23 16:34:20 +0000 | [diff] [blame] | 1018 | |
| 1019 | /* Override ptime, if this option is specified. */ |
| 1020 | if (pjsua_var.media_cfg.ptime != 0) { |
Benny Prijono | 91e567e | 2007-12-28 08:51:58 +0000 | [diff] [blame] | 1021 | si->param->setting.frm_per_pkt = (pj_uint8_t) |
| 1022 | (pjsua_var.media_cfg.ptime / si->param->info.frm_ptime); |
| 1023 | if (si->param->setting.frm_per_pkt == 0) |
| 1024 | si->param->setting.frm_per_pkt = 1; |
Benny Prijono | c97608e | 2007-03-23 16:34:20 +0000 | [diff] [blame] | 1025 | } |
| 1026 | |
| 1027 | /* Disable VAD, if this option is specified. */ |
| 1028 | if (pjsua_var.media_cfg.no_vad) { |
Benny Prijono | 91e567e | 2007-12-28 08:51:58 +0000 | [diff] [blame] | 1029 | si->param->setting.vad = 0; |
Benny Prijono | c97608e | 2007-03-23 16:34:20 +0000 | [diff] [blame] | 1030 | } |
| 1031 | |
| 1032 | |
| 1033 | /* Optionally, application may modify other stream settings here |
| 1034 | * (such as jitter buffer parameters, codec ptime, etc.) |
| 1035 | */ |
Benny Prijono | 91e567e | 2007-12-28 08:51:58 +0000 | [diff] [blame] | 1036 | si->jb_init = pjsua_var.media_cfg.jb_init; |
| 1037 | si->jb_min_pre = pjsua_var.media_cfg.jb_min_pre; |
| 1038 | si->jb_max_pre = pjsua_var.media_cfg.jb_max_pre; |
| 1039 | si->jb_max = pjsua_var.media_cfg.jb_max; |
Benny Prijono | c97608e | 2007-03-23 16:34:20 +0000 | [diff] [blame] | 1040 | |
Benny Prijono | 8147f40 | 2007-11-21 14:50:07 +0000 | [diff] [blame] | 1041 | /* Set SSRC */ |
Benny Prijono | 91e567e | 2007-12-28 08:51:58 +0000 | [diff] [blame] | 1042 | si->ssrc = call->ssrc; |
Benny Prijono | 8147f40 | 2007-11-21 14:50:07 +0000 | [diff] [blame] | 1043 | |
Benny Prijono | c97608e | 2007-03-23 16:34:20 +0000 | [diff] [blame] | 1044 | /* Create session based on session info. */ |
| 1045 | status = pjmedia_session_create( pjsua_var.med_endpt, &sess_info, |
| 1046 | &call->med_tp, |
| 1047 | call, &call->session ); |
| 1048 | if (status != PJ_SUCCESS) { |
| 1049 | return status; |
| 1050 | } |
| 1051 | |
| 1052 | /* If DTMF callback is installed by application, install our |
| 1053 | * callback to the session. |
| 1054 | */ |
| 1055 | if (pjsua_var.ua_cfg.cb.on_dtmf_digit) { |
| 1056 | pjmedia_session_set_dtmf_callback(call->session, 0, |
| 1057 | &dtmf_callback, |
Benny Prijono | d817965 | 2008-01-23 20:39:07 +0000 | [diff] [blame] | 1058 | (void*)(long)(call->index)); |
Benny Prijono | c97608e | 2007-03-23 16:34:20 +0000 | [diff] [blame] | 1059 | } |
| 1060 | |
| 1061 | /* Get the port interface of the first stream in the session. |
| 1062 | * We need the port interface to add to the conference bridge. |
| 1063 | */ |
| 1064 | pjmedia_session_get_port(call->session, 0, &media_port); |
| 1065 | |
Benny Prijono | fc13bf6 | 2008-02-20 08:56:15 +0000 | [diff] [blame] | 1066 | /* Notify application about stream creation. |
| 1067 | * Note: application may modify media_port to point to different |
| 1068 | * media port |
| 1069 | */ |
| 1070 | if (pjsua_var.ua_cfg.cb.on_stream_created) { |
| 1071 | pjsua_var.ua_cfg.cb.on_stream_created(call_id, call->session, |
| 1072 | 0, &media_port); |
| 1073 | } |
Benny Prijono | c97608e | 2007-03-23 16:34:20 +0000 | [diff] [blame] | 1074 | |
| 1075 | /* |
| 1076 | * Add the call to conference bridge. |
| 1077 | */ |
Benny Prijono | 38fb3ea | 2008-01-02 08:27:03 +0000 | [diff] [blame] | 1078 | { |
| 1079 | char tmp[PJSIP_MAX_URL_SIZE]; |
| 1080 | pj_str_t port_name; |
| 1081 | |
| 1082 | port_name.ptr = tmp; |
| 1083 | port_name.slen = pjsip_uri_print(PJSIP_URI_IN_REQ_URI, |
| 1084 | call->inv->dlg->remote.info->uri, |
| 1085 | tmp, sizeof(tmp)); |
| 1086 | if (port_name.slen < 1) { |
| 1087 | port_name = pj_str("call"); |
| 1088 | } |
| 1089 | status = pjmedia_conf_add_port( pjsua_var.mconf, call->inv->pool, |
| 1090 | media_port, |
| 1091 | &port_name, |
| 1092 | (unsigned*)&call->conf_slot); |
| 1093 | if (status != PJ_SUCCESS) { |
| 1094 | return status; |
| 1095 | } |
Benny Prijono | c97608e | 2007-03-23 16:34:20 +0000 | [diff] [blame] | 1096 | } |
| 1097 | |
| 1098 | /* Call's media state is active */ |
| 1099 | call->media_st = PJSUA_CALL_MEDIA_ACTIVE; |
Benny Prijono | 91e567e | 2007-12-28 08:51:58 +0000 | [diff] [blame] | 1100 | call->media_dir = si->dir; |
Benny Prijono | c97608e | 2007-03-23 16:34:20 +0000 | [diff] [blame] | 1101 | } |
| 1102 | |
| 1103 | /* Print info. */ |
| 1104 | { |
| 1105 | char info[80]; |
| 1106 | int info_len = 0; |
| 1107 | unsigned i; |
| 1108 | |
| 1109 | for (i=0; i<sess_info.stream_cnt; ++i) { |
| 1110 | int len; |
| 1111 | const char *dir; |
| 1112 | pjmedia_stream_info *strm_info = &sess_info.stream_info[i]; |
| 1113 | |
| 1114 | switch (strm_info->dir) { |
| 1115 | case PJMEDIA_DIR_NONE: |
| 1116 | dir = "inactive"; |
| 1117 | break; |
| 1118 | case PJMEDIA_DIR_ENCODING: |
| 1119 | dir = "sendonly"; |
| 1120 | break; |
| 1121 | case PJMEDIA_DIR_DECODING: |
| 1122 | dir = "recvonly"; |
| 1123 | break; |
| 1124 | case PJMEDIA_DIR_ENCODING_DECODING: |
| 1125 | dir = "sendrecv"; |
| 1126 | break; |
| 1127 | default: |
| 1128 | dir = "unknown"; |
| 1129 | break; |
| 1130 | } |
| 1131 | len = pj_ansi_sprintf( info+info_len, |
| 1132 | ", stream #%d: %.*s (%s)", i, |
| 1133 | (int)strm_info->fmt.encoding_name.slen, |
| 1134 | strm_info->fmt.encoding_name.ptr, |
| 1135 | dir); |
| 1136 | if (len > 0) |
| 1137 | info_len += len; |
| 1138 | } |
| 1139 | PJ_LOG(4,(THIS_FILE,"Media updates%s", info)); |
| 1140 | } |
| 1141 | |
| 1142 | return PJ_SUCCESS; |
| 1143 | } |
| 1144 | |
| 1145 | |
Benny Prijono | eebe9af | 2006-06-13 22:57:13 +0000 | [diff] [blame] | 1146 | /* |
| 1147 | * Get maxinum number of conference ports. |
| 1148 | */ |
| 1149 | PJ_DEF(unsigned) pjsua_conf_get_max_ports(void) |
| 1150 | { |
| 1151 | return pjsua_var.media_cfg.max_media_ports; |
| 1152 | } |
| 1153 | |
| 1154 | |
| 1155 | /* |
| 1156 | * Get current number of active ports in the bridge. |
| 1157 | */ |
| 1158 | PJ_DEF(unsigned) pjsua_conf_get_active_ports(void) |
| 1159 | { |
Benny Prijono | 38fb3ea | 2008-01-02 08:27:03 +0000 | [diff] [blame] | 1160 | unsigned ports[PJSUA_MAX_CONF_PORTS]; |
Benny Prijono | eebe9af | 2006-06-13 22:57:13 +0000 | [diff] [blame] | 1161 | unsigned count = PJ_ARRAY_SIZE(ports); |
| 1162 | pj_status_t status; |
| 1163 | |
| 1164 | status = pjmedia_conf_enum_ports(pjsua_var.mconf, ports, &count); |
| 1165 | if (status != PJ_SUCCESS) |
| 1166 | count = 0; |
| 1167 | |
| 1168 | return count; |
| 1169 | } |
| 1170 | |
| 1171 | |
| 1172 | /* |
| 1173 | * Enumerate all conference ports. |
| 1174 | */ |
| 1175 | PJ_DEF(pj_status_t) pjsua_enum_conf_ports(pjsua_conf_port_id id[], |
| 1176 | unsigned *count) |
| 1177 | { |
| 1178 | return pjmedia_conf_enum_ports(pjsua_var.mconf, (unsigned*)id, count); |
| 1179 | } |
| 1180 | |
| 1181 | |
| 1182 | /* |
| 1183 | * Get information about the specified conference port |
| 1184 | */ |
| 1185 | PJ_DEF(pj_status_t) pjsua_conf_get_port_info( pjsua_conf_port_id id, |
| 1186 | pjsua_conf_port_info *info) |
| 1187 | { |
| 1188 | pjmedia_conf_port_info cinfo; |
Benny Prijono | 0498d90 | 2006-06-19 14:49:14 +0000 | [diff] [blame] | 1189 | unsigned i; |
Benny Prijono | eebe9af | 2006-06-13 22:57:13 +0000 | [diff] [blame] | 1190 | pj_status_t status; |
| 1191 | |
| 1192 | status = pjmedia_conf_get_port_info( pjsua_var.mconf, id, &cinfo); |
| 1193 | if (status != PJ_SUCCESS) |
| 1194 | return status; |
| 1195 | |
Benny Prijono | ac623b3 | 2006-07-03 15:19:31 +0000 | [diff] [blame] | 1196 | pj_bzero(info, sizeof(*info)); |
Benny Prijono | eebe9af | 2006-06-13 22:57:13 +0000 | [diff] [blame] | 1197 | info->slot_id = id; |
| 1198 | info->name = cinfo.name; |
| 1199 | info->clock_rate = cinfo.clock_rate; |
| 1200 | info->channel_count = cinfo.channel_count; |
| 1201 | info->samples_per_frame = cinfo.samples_per_frame; |
| 1202 | info->bits_per_sample = cinfo.bits_per_sample; |
| 1203 | |
| 1204 | /* Build array of listeners */ |
Benny Prijono | c78c3a3 | 2006-06-16 15:54:43 +0000 | [diff] [blame] | 1205 | info->listener_cnt = cinfo.listener_cnt; |
| 1206 | for (i=0; i<cinfo.listener_cnt; ++i) { |
| 1207 | info->listeners[i] = cinfo.listener_slots[i]; |
Benny Prijono | eebe9af | 2006-06-13 22:57:13 +0000 | [diff] [blame] | 1208 | } |
| 1209 | |
| 1210 | return PJ_SUCCESS; |
| 1211 | } |
| 1212 | |
| 1213 | |
| 1214 | /* |
Benny Prijono | e909eac | 2006-07-27 22:04:56 +0000 | [diff] [blame] | 1215 | * Add arbitrary media port to PJSUA's conference bridge. |
| 1216 | */ |
| 1217 | PJ_DEF(pj_status_t) pjsua_conf_add_port( pj_pool_t *pool, |
| 1218 | pjmedia_port *port, |
| 1219 | pjsua_conf_port_id *p_id) |
| 1220 | { |
| 1221 | pj_status_t status; |
| 1222 | |
| 1223 | status = pjmedia_conf_add_port(pjsua_var.mconf, pool, |
| 1224 | port, NULL, (unsigned*)p_id); |
| 1225 | if (status != PJ_SUCCESS) { |
| 1226 | if (p_id) |
| 1227 | *p_id = PJSUA_INVALID_ID; |
| 1228 | } |
| 1229 | |
| 1230 | return status; |
| 1231 | } |
| 1232 | |
| 1233 | |
| 1234 | /* |
| 1235 | * Remove arbitrary slot from the conference bridge. |
| 1236 | */ |
| 1237 | PJ_DEF(pj_status_t) pjsua_conf_remove_port(pjsua_conf_port_id id) |
| 1238 | { |
| 1239 | return pjmedia_conf_remove_port(pjsua_var.mconf, (unsigned)id); |
| 1240 | } |
| 1241 | |
| 1242 | |
| 1243 | /* |
Benny Prijono | eebe9af | 2006-06-13 22:57:13 +0000 | [diff] [blame] | 1244 | * Establish unidirectional media flow from souce to sink. |
| 1245 | */ |
| 1246 | PJ_DEF(pj_status_t) pjsua_conf_connect( pjsua_conf_port_id source, |
| 1247 | pjsua_conf_port_id sink) |
| 1248 | { |
| 1249 | return pjmedia_conf_connect_port(pjsua_var.mconf, source, sink, 0); |
| 1250 | } |
| 1251 | |
| 1252 | |
| 1253 | /* |
| 1254 | * Disconnect media flow from the source to destination port. |
| 1255 | */ |
| 1256 | PJ_DEF(pj_status_t) pjsua_conf_disconnect( pjsua_conf_port_id source, |
| 1257 | pjsua_conf_port_id sink) |
| 1258 | { |
| 1259 | return pjmedia_conf_disconnect_port(pjsua_var.mconf, source, sink); |
| 1260 | } |
| 1261 | |
| 1262 | |
Benny Prijono | 6dd967c | 2006-12-26 02:27:14 +0000 | [diff] [blame] | 1263 | /* |
| 1264 | * Adjust the signal level to be transmitted from the bridge to the |
| 1265 | * specified port by making it louder or quieter. |
| 1266 | */ |
| 1267 | PJ_DEF(pj_status_t) pjsua_conf_adjust_tx_level(pjsua_conf_port_id slot, |
| 1268 | float level) |
| 1269 | { |
| 1270 | return pjmedia_conf_adjust_tx_level(pjsua_var.mconf, slot, |
| 1271 | (int)((level-1) * 128)); |
| 1272 | } |
| 1273 | |
| 1274 | /* |
| 1275 | * Adjust the signal level to be received from the specified port (to |
| 1276 | * the bridge) by making it louder or quieter. |
| 1277 | */ |
| 1278 | PJ_DEF(pj_status_t) pjsua_conf_adjust_rx_level(pjsua_conf_port_id slot, |
| 1279 | float level) |
| 1280 | { |
| 1281 | return pjmedia_conf_adjust_rx_level(pjsua_var.mconf, slot, |
| 1282 | (int)((level-1) * 128)); |
| 1283 | } |
| 1284 | |
| 1285 | |
| 1286 | /* |
| 1287 | * Get last signal level transmitted to or received from the specified port. |
| 1288 | */ |
| 1289 | PJ_DEF(pj_status_t) pjsua_conf_get_signal_level(pjsua_conf_port_id slot, |
| 1290 | unsigned *tx_level, |
| 1291 | unsigned *rx_level) |
| 1292 | { |
| 1293 | return pjmedia_conf_get_signal_level(pjsua_var.mconf, slot, |
| 1294 | tx_level, rx_level); |
| 1295 | } |
| 1296 | |
Benny Prijono | eebe9af | 2006-06-13 22:57:13 +0000 | [diff] [blame] | 1297 | /***************************************************************************** |
| 1298 | * File player. |
| 1299 | */ |
| 1300 | |
Benny Prijono | d5696da | 2007-07-17 16:25:45 +0000 | [diff] [blame] | 1301 | static char* get_basename(const char *path, unsigned len) |
| 1302 | { |
| 1303 | char *p = ((char*)path) + len; |
| 1304 | |
| 1305 | if (len==0) |
| 1306 | return p; |
| 1307 | |
Benny Prijono | 1f61a8f | 2007-08-16 10:11:44 +0000 | [diff] [blame] | 1308 | for (--p; p!=path && *p!='/' && *p!='\\'; ) --p; |
Benny Prijono | d5696da | 2007-07-17 16:25:45 +0000 | [diff] [blame] | 1309 | |
| 1310 | return (p==path) ? p : p+1; |
| 1311 | } |
| 1312 | |
| 1313 | |
Benny Prijono | eebe9af | 2006-06-13 22:57:13 +0000 | [diff] [blame] | 1314 | /* |
| 1315 | * Create a file player, and automatically connect this player to |
| 1316 | * the conference bridge. |
| 1317 | */ |
| 1318 | PJ_DEF(pj_status_t) pjsua_player_create( const pj_str_t *filename, |
| 1319 | unsigned options, |
Benny Prijono | eebe9af | 2006-06-13 22:57:13 +0000 | [diff] [blame] | 1320 | pjsua_player_id *p_id) |
| 1321 | { |
| 1322 | unsigned slot, file_id; |
Benny Prijono | a66c331 | 2007-01-21 23:12:40 +0000 | [diff] [blame] | 1323 | char path[PJ_MAXPATH]; |
Benny Prijono | d5696da | 2007-07-17 16:25:45 +0000 | [diff] [blame] | 1324 | pj_pool_t *pool; |
Benny Prijono | eebe9af | 2006-06-13 22:57:13 +0000 | [diff] [blame] | 1325 | pjmedia_port *port; |
| 1326 | pj_status_t status; |
| 1327 | |
| 1328 | if (pjsua_var.player_cnt >= PJ_ARRAY_SIZE(pjsua_var.player)) |
| 1329 | return PJ_ETOOMANY; |
| 1330 | |
| 1331 | PJSUA_LOCK(); |
| 1332 | |
| 1333 | for (file_id=0; file_id<PJ_ARRAY_SIZE(pjsua_var.player); ++file_id) { |
| 1334 | if (pjsua_var.player[file_id].port == NULL) |
| 1335 | break; |
| 1336 | } |
| 1337 | |
| 1338 | if (file_id == PJ_ARRAY_SIZE(pjsua_var.player)) { |
| 1339 | /* This is unexpected */ |
| 1340 | PJSUA_UNLOCK(); |
| 1341 | pj_assert(0); |
| 1342 | return PJ_EBUG; |
| 1343 | } |
| 1344 | |
| 1345 | pj_memcpy(path, filename->ptr, filename->slen); |
| 1346 | path[filename->slen] = '\0'; |
Benny Prijono | d5696da | 2007-07-17 16:25:45 +0000 | [diff] [blame] | 1347 | |
| 1348 | pool = pjsua_pool_create(get_basename(path, filename->slen), 1000, 1000); |
| 1349 | if (!pool) { |
| 1350 | PJSUA_UNLOCK(); |
| 1351 | return PJ_ENOMEM; |
| 1352 | } |
| 1353 | |
| 1354 | status = pjmedia_wav_player_port_create(pool, path, |
Benny Prijono | eebe9af | 2006-06-13 22:57:13 +0000 | [diff] [blame] | 1355 | pjsua_var.mconf_cfg.samples_per_frame * |
| 1356 | 1000 / pjsua_var.media_cfg.clock_rate, |
Benny Prijono | 00cae61 | 2006-07-31 15:19:36 +0000 | [diff] [blame] | 1357 | options, 0, &port); |
Benny Prijono | eebe9af | 2006-06-13 22:57:13 +0000 | [diff] [blame] | 1358 | if (status != PJ_SUCCESS) { |
| 1359 | PJSUA_UNLOCK(); |
| 1360 | pjsua_perror(THIS_FILE, "Unable to open file for playback", status); |
Benny Prijono | d5696da | 2007-07-17 16:25:45 +0000 | [diff] [blame] | 1361 | pj_pool_release(pool); |
Benny Prijono | eebe9af | 2006-06-13 22:57:13 +0000 | [diff] [blame] | 1362 | return status; |
| 1363 | } |
| 1364 | |
| 1365 | status = pjmedia_conf_add_port(pjsua_var.mconf, pjsua_var.pool, |
| 1366 | port, filename, &slot); |
| 1367 | if (status != PJ_SUCCESS) { |
| 1368 | pjmedia_port_destroy(port); |
| 1369 | PJSUA_UNLOCK(); |
Benny Prijono | 32e4f49 | 2007-01-24 00:44:26 +0000 | [diff] [blame] | 1370 | pjsua_perror(THIS_FILE, "Unable to add file to conference bridge", |
| 1371 | status); |
Benny Prijono | d5696da | 2007-07-17 16:25:45 +0000 | [diff] [blame] | 1372 | pj_pool_release(pool); |
Benny Prijono | eebe9af | 2006-06-13 22:57:13 +0000 | [diff] [blame] | 1373 | return status; |
| 1374 | } |
| 1375 | |
Benny Prijono | a66c331 | 2007-01-21 23:12:40 +0000 | [diff] [blame] | 1376 | pjsua_var.player[file_id].type = 0; |
Benny Prijono | d5696da | 2007-07-17 16:25:45 +0000 | [diff] [blame] | 1377 | pjsua_var.player[file_id].pool = pool; |
Benny Prijono | eebe9af | 2006-06-13 22:57:13 +0000 | [diff] [blame] | 1378 | pjsua_var.player[file_id].port = port; |
| 1379 | pjsua_var.player[file_id].slot = slot; |
| 1380 | |
| 1381 | if (p_id) *p_id = file_id; |
| 1382 | |
| 1383 | ++pjsua_var.player_cnt; |
| 1384 | |
| 1385 | PJSUA_UNLOCK(); |
| 1386 | return PJ_SUCCESS; |
| 1387 | } |
| 1388 | |
| 1389 | |
| 1390 | /* |
Benny Prijono | a66c331 | 2007-01-21 23:12:40 +0000 | [diff] [blame] | 1391 | * Create a file playlist media port, and automatically add the port |
| 1392 | * to the conference bridge. |
| 1393 | */ |
| 1394 | PJ_DEF(pj_status_t) pjsua_playlist_create( const pj_str_t file_names[], |
| 1395 | unsigned file_count, |
| 1396 | const pj_str_t *label, |
| 1397 | unsigned options, |
| 1398 | pjsua_player_id *p_id) |
| 1399 | { |
| 1400 | unsigned slot, file_id, ptime; |
Benny Prijono | d5696da | 2007-07-17 16:25:45 +0000 | [diff] [blame] | 1401 | pj_pool_t *pool; |
Benny Prijono | a66c331 | 2007-01-21 23:12:40 +0000 | [diff] [blame] | 1402 | pjmedia_port *port; |
| 1403 | pj_status_t status; |
| 1404 | |
| 1405 | if (pjsua_var.player_cnt >= PJ_ARRAY_SIZE(pjsua_var.player)) |
| 1406 | return PJ_ETOOMANY; |
| 1407 | |
| 1408 | PJSUA_LOCK(); |
| 1409 | |
| 1410 | for (file_id=0; file_id<PJ_ARRAY_SIZE(pjsua_var.player); ++file_id) { |
| 1411 | if (pjsua_var.player[file_id].port == NULL) |
| 1412 | break; |
| 1413 | } |
| 1414 | |
| 1415 | if (file_id == PJ_ARRAY_SIZE(pjsua_var.player)) { |
| 1416 | /* This is unexpected */ |
| 1417 | PJSUA_UNLOCK(); |
| 1418 | pj_assert(0); |
| 1419 | return PJ_EBUG; |
| 1420 | } |
| 1421 | |
| 1422 | |
| 1423 | ptime = pjsua_var.mconf_cfg.samples_per_frame * 1000 / |
| 1424 | pjsua_var.media_cfg.clock_rate; |
| 1425 | |
Benny Prijono | d5696da | 2007-07-17 16:25:45 +0000 | [diff] [blame] | 1426 | pool = pjsua_pool_create("playlist", 1000, 1000); |
| 1427 | if (!pool) { |
| 1428 | PJSUA_UNLOCK(); |
| 1429 | return PJ_ENOMEM; |
| 1430 | } |
| 1431 | |
| 1432 | status = pjmedia_wav_playlist_create(pool, label, |
Benny Prijono | a66c331 | 2007-01-21 23:12:40 +0000 | [diff] [blame] | 1433 | file_names, file_count, |
| 1434 | ptime, options, 0, &port); |
| 1435 | if (status != PJ_SUCCESS) { |
| 1436 | PJSUA_UNLOCK(); |
| 1437 | pjsua_perror(THIS_FILE, "Unable to create playlist", status); |
Benny Prijono | d5696da | 2007-07-17 16:25:45 +0000 | [diff] [blame] | 1438 | pj_pool_release(pool); |
Benny Prijono | a66c331 | 2007-01-21 23:12:40 +0000 | [diff] [blame] | 1439 | return status; |
| 1440 | } |
| 1441 | |
Benny Prijono | d5696da | 2007-07-17 16:25:45 +0000 | [diff] [blame] | 1442 | status = pjmedia_conf_add_port(pjsua_var.mconf, pool, |
Benny Prijono | a66c331 | 2007-01-21 23:12:40 +0000 | [diff] [blame] | 1443 | port, &port->info.name, &slot); |
| 1444 | if (status != PJ_SUCCESS) { |
| 1445 | pjmedia_port_destroy(port); |
| 1446 | PJSUA_UNLOCK(); |
| 1447 | pjsua_perror(THIS_FILE, "Unable to add port", status); |
Benny Prijono | d5696da | 2007-07-17 16:25:45 +0000 | [diff] [blame] | 1448 | pj_pool_release(pool); |
Benny Prijono | a66c331 | 2007-01-21 23:12:40 +0000 | [diff] [blame] | 1449 | return status; |
| 1450 | } |
| 1451 | |
| 1452 | pjsua_var.player[file_id].type = 1; |
Benny Prijono | d5696da | 2007-07-17 16:25:45 +0000 | [diff] [blame] | 1453 | pjsua_var.player[file_id].pool = pool; |
Benny Prijono | a66c331 | 2007-01-21 23:12:40 +0000 | [diff] [blame] | 1454 | pjsua_var.player[file_id].port = port; |
| 1455 | pjsua_var.player[file_id].slot = slot; |
| 1456 | |
| 1457 | if (p_id) *p_id = file_id; |
| 1458 | |
| 1459 | ++pjsua_var.player_cnt; |
| 1460 | |
| 1461 | PJSUA_UNLOCK(); |
| 1462 | return PJ_SUCCESS; |
| 1463 | |
| 1464 | } |
| 1465 | |
| 1466 | |
| 1467 | /* |
Benny Prijono | eebe9af | 2006-06-13 22:57:13 +0000 | [diff] [blame] | 1468 | * Get conference port ID associated with player. |
| 1469 | */ |
| 1470 | PJ_DEF(pjsua_conf_port_id) pjsua_player_get_conf_port(pjsua_player_id id) |
| 1471 | { |
Benny Prijono | a1e6968 | 2007-05-11 15:14:34 +0000 | [diff] [blame] | 1472 | 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] | 1473 | PJ_ASSERT_RETURN(pjsua_var.player[id].port != NULL, PJ_EINVAL); |
| 1474 | |
| 1475 | return pjsua_var.player[id].slot; |
| 1476 | } |
| 1477 | |
Benny Prijono | 469b152 | 2006-12-26 03:05:17 +0000 | [diff] [blame] | 1478 | /* |
| 1479 | * Get the media port for the player. |
| 1480 | */ |
Benny Prijono | be41d86 | 2008-01-18 13:24:28 +0000 | [diff] [blame] | 1481 | PJ_DEF(pj_status_t) pjsua_player_get_port( pjsua_player_id id, |
Benny Prijono | 469b152 | 2006-12-26 03:05:17 +0000 | [diff] [blame] | 1482 | pjmedia_port **p_port) |
| 1483 | { |
Benny Prijono | a1e6968 | 2007-05-11 15:14:34 +0000 | [diff] [blame] | 1484 | 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] | 1485 | PJ_ASSERT_RETURN(pjsua_var.player[id].port != NULL, PJ_EINVAL); |
| 1486 | PJ_ASSERT_RETURN(p_port != NULL, PJ_EINVAL); |
| 1487 | |
| 1488 | *p_port = pjsua_var.player[id].port; |
| 1489 | |
| 1490 | return PJ_SUCCESS; |
| 1491 | } |
Benny Prijono | eebe9af | 2006-06-13 22:57:13 +0000 | [diff] [blame] | 1492 | |
| 1493 | /* |
| 1494 | * Set playback position. |
| 1495 | */ |
| 1496 | PJ_DEF(pj_status_t) pjsua_player_set_pos( pjsua_player_id id, |
| 1497 | pj_uint32_t samples) |
| 1498 | { |
Benny Prijono | a1e6968 | 2007-05-11 15:14:34 +0000 | [diff] [blame] | 1499 | 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] | 1500 | PJ_ASSERT_RETURN(pjsua_var.player[id].port != NULL, PJ_EINVAL); |
Benny Prijono | a66c331 | 2007-01-21 23:12:40 +0000 | [diff] [blame] | 1501 | PJ_ASSERT_RETURN(pjsua_var.player[id].type == 0, PJ_EINVAL); |
Benny Prijono | eebe9af | 2006-06-13 22:57:13 +0000 | [diff] [blame] | 1502 | |
| 1503 | return pjmedia_wav_player_port_set_pos(pjsua_var.player[id].port, samples); |
| 1504 | } |
| 1505 | |
| 1506 | |
| 1507 | /* |
| 1508 | * Close the file, remove the player from the bridge, and free |
| 1509 | * resources associated with the file player. |
| 1510 | */ |
| 1511 | PJ_DEF(pj_status_t) pjsua_player_destroy(pjsua_player_id id) |
| 1512 | { |
Benny Prijono | a1e6968 | 2007-05-11 15:14:34 +0000 | [diff] [blame] | 1513 | 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] | 1514 | PJ_ASSERT_RETURN(pjsua_var.player[id].port != NULL, PJ_EINVAL); |
| 1515 | |
| 1516 | PJSUA_LOCK(); |
| 1517 | |
| 1518 | if (pjsua_var.player[id].port) { |
| 1519 | pjmedia_conf_remove_port(pjsua_var.mconf, |
| 1520 | pjsua_var.player[id].slot); |
| 1521 | pjmedia_port_destroy(pjsua_var.player[id].port); |
| 1522 | pjsua_var.player[id].port = NULL; |
| 1523 | pjsua_var.player[id].slot = 0xFFFF; |
Benny Prijono | d5696da | 2007-07-17 16:25:45 +0000 | [diff] [blame] | 1524 | pj_pool_release(pjsua_var.player[id].pool); |
| 1525 | pjsua_var.player[id].pool = NULL; |
Benny Prijono | eebe9af | 2006-06-13 22:57:13 +0000 | [diff] [blame] | 1526 | pjsua_var.player_cnt--; |
| 1527 | } |
| 1528 | |
| 1529 | PJSUA_UNLOCK(); |
| 1530 | |
| 1531 | return PJ_SUCCESS; |
| 1532 | } |
| 1533 | |
| 1534 | |
| 1535 | /***************************************************************************** |
| 1536 | * File recorder. |
| 1537 | */ |
| 1538 | |
| 1539 | /* |
| 1540 | * Create a file recorder, and automatically connect this recorder to |
| 1541 | * the conference bridge. |
| 1542 | */ |
| 1543 | 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] | 1544 | unsigned enc_type, |
| 1545 | void *enc_param, |
Benny Prijono | eebe9af | 2006-06-13 22:57:13 +0000 | [diff] [blame] | 1546 | pj_ssize_t max_size, |
| 1547 | unsigned options, |
Benny Prijono | eebe9af | 2006-06-13 22:57:13 +0000 | [diff] [blame] | 1548 | pjsua_recorder_id *p_id) |
| 1549 | { |
Benny Prijono | b3cdb2b | 2006-10-19 15:49:47 +0000 | [diff] [blame] | 1550 | enum Format |
| 1551 | { |
| 1552 | FMT_UNKNOWN, |
| 1553 | FMT_WAV, |
| 1554 | FMT_MP3, |
| 1555 | }; |
Benny Prijono | eebe9af | 2006-06-13 22:57:13 +0000 | [diff] [blame] | 1556 | unsigned slot, file_id; |
Benny Prijono | a66c331 | 2007-01-21 23:12:40 +0000 | [diff] [blame] | 1557 | char path[PJ_MAXPATH]; |
Benny Prijono | b3cdb2b | 2006-10-19 15:49:47 +0000 | [diff] [blame] | 1558 | pj_str_t ext; |
Benny Prijono | 8f31052 | 2006-10-20 11:08:49 +0000 | [diff] [blame] | 1559 | int file_format; |
Benny Prijono | d5696da | 2007-07-17 16:25:45 +0000 | [diff] [blame] | 1560 | pj_pool_t *pool; |
Benny Prijono | eebe9af | 2006-06-13 22:57:13 +0000 | [diff] [blame] | 1561 | pjmedia_port *port; |
| 1562 | pj_status_t status; |
| 1563 | |
Benny Prijono | b3cdb2b | 2006-10-19 15:49:47 +0000 | [diff] [blame] | 1564 | /* Filename must present */ |
| 1565 | PJ_ASSERT_RETURN(filename != NULL, PJ_EINVAL); |
| 1566 | |
Benny Prijono | 00cae61 | 2006-07-31 15:19:36 +0000 | [diff] [blame] | 1567 | /* Don't support max_size at present */ |
Benny Prijono | b3cdb2b | 2006-10-19 15:49:47 +0000 | [diff] [blame] | 1568 | PJ_ASSERT_RETURN(max_size == 0 || max_size == -1, PJ_EINVAL); |
Benny Prijono | 00cae61 | 2006-07-31 15:19:36 +0000 | [diff] [blame] | 1569 | |
Benny Prijono | 8f31052 | 2006-10-20 11:08:49 +0000 | [diff] [blame] | 1570 | /* Don't support encoding type at present */ |
| 1571 | PJ_ASSERT_RETURN(enc_type == 0, PJ_EINVAL); |
Benny Prijono | 00cae61 | 2006-07-31 15:19:36 +0000 | [diff] [blame] | 1572 | |
Benny Prijono | eebe9af | 2006-06-13 22:57:13 +0000 | [diff] [blame] | 1573 | if (pjsua_var.rec_cnt >= PJ_ARRAY_SIZE(pjsua_var.recorder)) |
| 1574 | return PJ_ETOOMANY; |
| 1575 | |
Benny Prijono | b3cdb2b | 2006-10-19 15:49:47 +0000 | [diff] [blame] | 1576 | /* Determine the file format */ |
| 1577 | ext.ptr = filename->ptr + filename->slen - 4; |
| 1578 | ext.slen = 4; |
| 1579 | |
| 1580 | if (pj_stricmp2(&ext, ".wav") == 0) |
| 1581 | file_format = FMT_WAV; |
| 1582 | else if (pj_stricmp2(&ext, ".mp3") == 0) |
| 1583 | file_format = FMT_MP3; |
| 1584 | else { |
| 1585 | PJ_LOG(1,(THIS_FILE, "pjsua_recorder_create() error: unable to " |
| 1586 | "determine file format for %.*s", |
| 1587 | (int)filename->slen, filename->ptr)); |
| 1588 | return PJ_ENOTSUP; |
| 1589 | } |
| 1590 | |
Benny Prijono | eebe9af | 2006-06-13 22:57:13 +0000 | [diff] [blame] | 1591 | PJSUA_LOCK(); |
| 1592 | |
| 1593 | for (file_id=0; file_id<PJ_ARRAY_SIZE(pjsua_var.recorder); ++file_id) { |
| 1594 | if (pjsua_var.recorder[file_id].port == NULL) |
| 1595 | break; |
| 1596 | } |
| 1597 | |
| 1598 | if (file_id == PJ_ARRAY_SIZE(pjsua_var.recorder)) { |
| 1599 | /* This is unexpected */ |
| 1600 | PJSUA_UNLOCK(); |
| 1601 | pj_assert(0); |
| 1602 | return PJ_EBUG; |
| 1603 | } |
| 1604 | |
| 1605 | pj_memcpy(path, filename->ptr, filename->slen); |
| 1606 | path[filename->slen] = '\0'; |
Benny Prijono | b3cdb2b | 2006-10-19 15:49:47 +0000 | [diff] [blame] | 1607 | |
Benny Prijono | d5696da | 2007-07-17 16:25:45 +0000 | [diff] [blame] | 1608 | pool = pjsua_pool_create(get_basename(path, filename->slen), 1000, 1000); |
| 1609 | if (!pool) { |
| 1610 | PJSUA_UNLOCK(); |
| 1611 | return PJ_ENOMEM; |
| 1612 | } |
| 1613 | |
Benny Prijono | b3cdb2b | 2006-10-19 15:49:47 +0000 | [diff] [blame] | 1614 | if (file_format == FMT_WAV) { |
Benny Prijono | d5696da | 2007-07-17 16:25:45 +0000 | [diff] [blame] | 1615 | status = pjmedia_wav_writer_port_create(pool, path, |
Benny Prijono | b3cdb2b | 2006-10-19 15:49:47 +0000 | [diff] [blame] | 1616 | pjsua_var.media_cfg.clock_rate, |
| 1617 | pjsua_var.mconf_cfg.channel_count, |
| 1618 | pjsua_var.mconf_cfg.samples_per_frame, |
| 1619 | pjsua_var.mconf_cfg.bits_per_sample, |
| 1620 | options, 0, &port); |
Benny Prijono | b3cdb2b | 2006-10-19 15:49:47 +0000 | [diff] [blame] | 1621 | } else { |
Benny Prijono | c95a0f0 | 2007-04-09 07:06:08 +0000 | [diff] [blame] | 1622 | PJ_UNUSED_ARG(enc_param); |
Benny Prijono | b3cdb2b | 2006-10-19 15:49:47 +0000 | [diff] [blame] | 1623 | port = NULL; |
| 1624 | status = PJ_ENOTSUP; |
| 1625 | } |
| 1626 | |
Benny Prijono | eebe9af | 2006-06-13 22:57:13 +0000 | [diff] [blame] | 1627 | if (status != PJ_SUCCESS) { |
| 1628 | PJSUA_UNLOCK(); |
| 1629 | pjsua_perror(THIS_FILE, "Unable to open file for recording", status); |
Benny Prijono | d5696da | 2007-07-17 16:25:45 +0000 | [diff] [blame] | 1630 | pj_pool_release(pool); |
Benny Prijono | eebe9af | 2006-06-13 22:57:13 +0000 | [diff] [blame] | 1631 | return status; |
| 1632 | } |
| 1633 | |
Benny Prijono | d5696da | 2007-07-17 16:25:45 +0000 | [diff] [blame] | 1634 | status = pjmedia_conf_add_port(pjsua_var.mconf, pool, |
Benny Prijono | eebe9af | 2006-06-13 22:57:13 +0000 | [diff] [blame] | 1635 | port, filename, &slot); |
| 1636 | if (status != PJ_SUCCESS) { |
| 1637 | pjmedia_port_destroy(port); |
| 1638 | PJSUA_UNLOCK(); |
Benny Prijono | d5696da | 2007-07-17 16:25:45 +0000 | [diff] [blame] | 1639 | pj_pool_release(pool); |
Benny Prijono | eebe9af | 2006-06-13 22:57:13 +0000 | [diff] [blame] | 1640 | return status; |
| 1641 | } |
| 1642 | |
| 1643 | pjsua_var.recorder[file_id].port = port; |
| 1644 | pjsua_var.recorder[file_id].slot = slot; |
Benny Prijono | d5696da | 2007-07-17 16:25:45 +0000 | [diff] [blame] | 1645 | pjsua_var.recorder[file_id].pool = pool; |
Benny Prijono | eebe9af | 2006-06-13 22:57:13 +0000 | [diff] [blame] | 1646 | |
| 1647 | if (p_id) *p_id = file_id; |
| 1648 | |
| 1649 | ++pjsua_var.rec_cnt; |
| 1650 | |
| 1651 | PJSUA_UNLOCK(); |
| 1652 | return PJ_SUCCESS; |
| 1653 | } |
| 1654 | |
| 1655 | |
| 1656 | /* |
| 1657 | * Get conference port associated with recorder. |
| 1658 | */ |
| 1659 | PJ_DEF(pjsua_conf_port_id) pjsua_recorder_get_conf_port(pjsua_recorder_id id) |
| 1660 | { |
Benny Prijono | a1e6968 | 2007-05-11 15:14:34 +0000 | [diff] [blame] | 1661 | PJ_ASSERT_RETURN(id>=0 && id<(int)PJ_ARRAY_SIZE(pjsua_var.recorder), |
| 1662 | PJ_EINVAL); |
Benny Prijono | eebe9af | 2006-06-13 22:57:13 +0000 | [diff] [blame] | 1663 | PJ_ASSERT_RETURN(pjsua_var.recorder[id].port != NULL, PJ_EINVAL); |
| 1664 | |
| 1665 | return pjsua_var.recorder[id].slot; |
| 1666 | } |
| 1667 | |
Benny Prijono | 469b152 | 2006-12-26 03:05:17 +0000 | [diff] [blame] | 1668 | /* |
| 1669 | * Get the media port for the recorder. |
| 1670 | */ |
| 1671 | PJ_DEF(pj_status_t) pjsua_recorder_get_port( pjsua_recorder_id id, |
| 1672 | pjmedia_port **p_port) |
| 1673 | { |
Benny Prijono | a1e6968 | 2007-05-11 15:14:34 +0000 | [diff] [blame] | 1674 | PJ_ASSERT_RETURN(id>=0 && id<(int)PJ_ARRAY_SIZE(pjsua_var.recorder), |
| 1675 | PJ_EINVAL); |
Benny Prijono | 469b152 | 2006-12-26 03:05:17 +0000 | [diff] [blame] | 1676 | PJ_ASSERT_RETURN(pjsua_var.recorder[id].port != NULL, PJ_EINVAL); |
| 1677 | PJ_ASSERT_RETURN(p_port != NULL, PJ_EINVAL); |
| 1678 | |
| 1679 | *p_port = pjsua_var.recorder[id].port; |
| 1680 | return PJ_SUCCESS; |
| 1681 | } |
Benny Prijono | eebe9af | 2006-06-13 22:57:13 +0000 | [diff] [blame] | 1682 | |
| 1683 | /* |
| 1684 | * Destroy recorder (this will complete recording). |
| 1685 | */ |
| 1686 | PJ_DEF(pj_status_t) pjsua_recorder_destroy(pjsua_recorder_id id) |
| 1687 | { |
Benny Prijono | a1e6968 | 2007-05-11 15:14:34 +0000 | [diff] [blame] | 1688 | PJ_ASSERT_RETURN(id>=0 && id<(int)PJ_ARRAY_SIZE(pjsua_var.recorder), |
| 1689 | PJ_EINVAL); |
Benny Prijono | eebe9af | 2006-06-13 22:57:13 +0000 | [diff] [blame] | 1690 | PJ_ASSERT_RETURN(pjsua_var.recorder[id].port != NULL, PJ_EINVAL); |
| 1691 | |
| 1692 | PJSUA_LOCK(); |
| 1693 | |
| 1694 | if (pjsua_var.recorder[id].port) { |
| 1695 | pjmedia_conf_remove_port(pjsua_var.mconf, |
| 1696 | pjsua_var.recorder[id].slot); |
| 1697 | pjmedia_port_destroy(pjsua_var.recorder[id].port); |
| 1698 | pjsua_var.recorder[id].port = NULL; |
| 1699 | pjsua_var.recorder[id].slot = 0xFFFF; |
Benny Prijono | d5696da | 2007-07-17 16:25:45 +0000 | [diff] [blame] | 1700 | pj_pool_release(pjsua_var.recorder[id].pool); |
| 1701 | pjsua_var.recorder[id].pool = NULL; |
Benny Prijono | eebe9af | 2006-06-13 22:57:13 +0000 | [diff] [blame] | 1702 | pjsua_var.rec_cnt--; |
| 1703 | } |
| 1704 | |
| 1705 | PJSUA_UNLOCK(); |
| 1706 | |
| 1707 | return PJ_SUCCESS; |
| 1708 | } |
| 1709 | |
| 1710 | |
| 1711 | /***************************************************************************** |
| 1712 | * Sound devices. |
| 1713 | */ |
| 1714 | |
| 1715 | /* |
| 1716 | * Enum sound devices. |
| 1717 | */ |
| 1718 | PJ_DEF(pj_status_t) pjsua_enum_snd_devs( pjmedia_snd_dev_info info[], |
| 1719 | unsigned *count) |
| 1720 | { |
| 1721 | unsigned i, dev_count; |
| 1722 | |
| 1723 | dev_count = pjmedia_snd_get_dev_count(); |
| 1724 | |
| 1725 | if (dev_count > *count) dev_count = *count; |
| 1726 | |
| 1727 | for (i=0; i<dev_count; ++i) { |
| 1728 | const pjmedia_snd_dev_info *ci; |
| 1729 | |
| 1730 | ci = pjmedia_snd_get_dev_info(i); |
| 1731 | pj_memcpy(&info[i], ci, sizeof(*ci)); |
| 1732 | } |
| 1733 | |
| 1734 | *count = dev_count; |
| 1735 | |
| 1736 | return PJ_SUCCESS; |
| 1737 | } |
| 1738 | |
| 1739 | |
| 1740 | /* Close existing sound device */ |
| 1741 | static void close_snd_dev(void) |
| 1742 | { |
| 1743 | /* Close sound device */ |
| 1744 | if (pjsua_var.snd_port) { |
| 1745 | const pjmedia_snd_dev_info *cap_info, *play_info; |
| 1746 | |
| 1747 | cap_info = pjmedia_snd_get_dev_info(pjsua_var.cap_dev); |
| 1748 | play_info = pjmedia_snd_get_dev_info(pjsua_var.play_dev); |
| 1749 | |
| 1750 | PJ_LOG(4,(THIS_FILE, "Closing %s sound playback device and " |
| 1751 | "%s sound capture device", |
| 1752 | play_info->name, cap_info->name)); |
| 1753 | |
| 1754 | pjmedia_snd_port_disconnect(pjsua_var.snd_port); |
| 1755 | pjmedia_snd_port_destroy(pjsua_var.snd_port); |
| 1756 | pjsua_var.snd_port = NULL; |
| 1757 | } |
| 1758 | |
| 1759 | /* Close null sound device */ |
| 1760 | if (pjsua_var.null_snd) { |
| 1761 | PJ_LOG(4,(THIS_FILE, "Closing null sound device..")); |
| 1762 | pjmedia_master_port_destroy(pjsua_var.null_snd, PJ_FALSE); |
| 1763 | pjsua_var.null_snd = NULL; |
| 1764 | } |
| 1765 | } |
| 1766 | |
| 1767 | /* |
| 1768 | * Select or change sound device. Application may call this function at |
| 1769 | * any time to replace current sound device. |
| 1770 | */ |
| 1771 | PJ_DEF(pj_status_t) pjsua_set_snd_dev( int capture_dev, |
| 1772 | int playback_dev) |
| 1773 | { |
| 1774 | pjmedia_port *conf_port; |
Benny Prijono | 6dd967c | 2006-12-26 02:27:14 +0000 | [diff] [blame] | 1775 | const pjmedia_snd_dev_info *play_info; |
Benny Prijono | f3758ee | 2008-02-26 15:32:16 +0000 | [diff] [blame] | 1776 | unsigned clock_rates[] = {0, 22050, 44100, 48000, 32000, 16000, |
| 1777 | 8000}; |
Benny Prijono | 658a1c5 | 2006-10-11 21:56:16 +0000 | [diff] [blame] | 1778 | unsigned selected_clock_rate = 0; |
Benny Prijono | 26056d8 | 2006-10-11 16:03:41 +0000 | [diff] [blame] | 1779 | unsigned i; |
Benny Prijono | c53c6d7 | 2006-11-27 09:54:03 +0000 | [diff] [blame] | 1780 | pjmedia_snd_stream *strm; |
| 1781 | pjmedia_snd_stream_info si; |
| 1782 | pj_str_t tmp; |
Benny Prijono | 26056d8 | 2006-10-11 16:03:41 +0000 | [diff] [blame] | 1783 | pj_status_t status = -1; |
Benny Prijono | eebe9af | 2006-06-13 22:57:13 +0000 | [diff] [blame] | 1784 | |
| 1785 | /* Close existing sound port */ |
| 1786 | close_snd_dev(); |
| 1787 | |
| 1788 | |
Benny Prijono | 26056d8 | 2006-10-11 16:03:41 +0000 | [diff] [blame] | 1789 | /* Set default clock rate */ |
Benny Prijono | f3758ee | 2008-02-26 15:32:16 +0000 | [diff] [blame] | 1790 | clock_rates[0] = pjsua_var.media_cfg.snd_clock_rate; |
| 1791 | if (clock_rates[0] == 0) |
| 1792 | clock_rates[0] = pjsua_var.media_cfg.clock_rate; |
Benny Prijono | eebe9af | 2006-06-13 22:57:13 +0000 | [diff] [blame] | 1793 | |
Benny Prijono | 50f19b3 | 2008-03-11 13:15:43 +0000 | [diff] [blame^] | 1794 | /* Get the port0 of the conference bridge. */ |
| 1795 | conf_port = pjmedia_conf_get_master_port(pjsua_var.mconf); |
| 1796 | pj_assert(conf_port != NULL); |
| 1797 | |
Benny Prijono | 26056d8 | 2006-10-11 16:03:41 +0000 | [diff] [blame] | 1798 | /* Attempts to open the sound device with different clock rates */ |
| 1799 | for (i=0; i<PJ_ARRAY_SIZE(clock_rates); ++i) { |
| 1800 | char errmsg[PJ_ERR_MSG_SIZE]; |
Benny Prijono | cf0b4b2 | 2007-10-06 17:31:09 +0000 | [diff] [blame] | 1801 | unsigned fps; |
Benny Prijono | 26056d8 | 2006-10-11 16:03:41 +0000 | [diff] [blame] | 1802 | |
| 1803 | PJ_LOG(4,(THIS_FILE, |
| 1804 | "pjsua_set_snd_dev(): attempting to open devices " |
| 1805 | "@%d Hz", clock_rates[i])); |
| 1806 | |
| 1807 | /* Create the sound device. Sound port will start immediately. */ |
Benny Prijono | cf0b4b2 | 2007-10-06 17:31:09 +0000 | [diff] [blame] | 1808 | fps = 1000 / pjsua_var.media_cfg.audio_frame_ptime; |
Benny Prijono | 26056d8 | 2006-10-11 16:03:41 +0000 | [diff] [blame] | 1809 | status = pjmedia_snd_port_create(pjsua_var.pool, capture_dev, |
| 1810 | playback_dev, |
| 1811 | clock_rates[i], 1, |
Benny Prijono | cf0b4b2 | 2007-10-06 17:31:09 +0000 | [diff] [blame] | 1812 | clock_rates[i]/fps, |
Benny Prijono | 26056d8 | 2006-10-11 16:03:41 +0000 | [diff] [blame] | 1813 | 16, 0, &pjsua_var.snd_port); |
| 1814 | |
Benny Prijono | 658a1c5 | 2006-10-11 21:56:16 +0000 | [diff] [blame] | 1815 | if (status == PJ_SUCCESS) { |
| 1816 | selected_clock_rate = clock_rates[i]; |
Benny Prijono | 50f19b3 | 2008-03-11 13:15:43 +0000 | [diff] [blame^] | 1817 | |
| 1818 | /* If there's mismatch between sound port and conference's port, |
| 1819 | * create a resample port to bridge them. |
| 1820 | */ |
| 1821 | if (selected_clock_rate != pjsua_var.media_cfg.clock_rate) { |
| 1822 | pjmedia_port *resample_port; |
| 1823 | unsigned resample_opt = 0; |
| 1824 | |
| 1825 | if (pjsua_var.media_cfg.quality >= 3 && |
| 1826 | pjsua_var.media_cfg.quality <= 4) |
| 1827 | { |
| 1828 | resample_opt |= PJMEDIA_CONF_SMALL_FILTER; |
| 1829 | } |
| 1830 | else if (pjsua_var.media_cfg.quality < 3) { |
| 1831 | resample_opt |= PJMEDIA_CONF_USE_LINEAR; |
| 1832 | } |
| 1833 | |
| 1834 | status = pjmedia_resample_port_create(pjsua_var.pool, |
| 1835 | conf_port, |
| 1836 | selected_clock_rate, |
| 1837 | resample_opt, |
| 1838 | &resample_port); |
| 1839 | if (status != PJ_SUCCESS) { |
| 1840 | pj_strerror(status, errmsg, sizeof(errmsg)); |
| 1841 | PJ_LOG(4, (THIS_FILE, |
| 1842 | "Error creating resample port, trying next " |
| 1843 | "clock rate", |
| 1844 | errmsg)); |
| 1845 | pjmedia_snd_port_destroy(pjsua_var.snd_port); |
| 1846 | pjsua_var.snd_port = NULL; |
| 1847 | continue; |
| 1848 | } else { |
| 1849 | conf_port = resample_port; |
| 1850 | break; |
| 1851 | } |
| 1852 | |
| 1853 | } else { |
| 1854 | break; |
| 1855 | } |
Benny Prijono | 658a1c5 | 2006-10-11 21:56:16 +0000 | [diff] [blame] | 1856 | } |
Benny Prijono | 26056d8 | 2006-10-11 16:03:41 +0000 | [diff] [blame] | 1857 | |
| 1858 | pj_strerror(status, errmsg, sizeof(errmsg)); |
Benny Prijono | 658a1c5 | 2006-10-11 21:56:16 +0000 | [diff] [blame] | 1859 | PJ_LOG(4, (THIS_FILE, "..failed: %s", errmsg)); |
Benny Prijono | 26056d8 | 2006-10-11 16:03:41 +0000 | [diff] [blame] | 1860 | } |
| 1861 | |
Benny Prijono | eebe9af | 2006-06-13 22:57:13 +0000 | [diff] [blame] | 1862 | if (status != PJ_SUCCESS) { |
| 1863 | pjsua_perror(THIS_FILE, "Unable to open sound device", status); |
| 1864 | return status; |
| 1865 | } |
| 1866 | |
Benny Prijono | f20687a | 2006-08-04 18:27:19 +0000 | [diff] [blame] | 1867 | /* Set AEC */ |
Benny Prijono | 5da5043 | 2006-08-07 10:24:52 +0000 | [diff] [blame] | 1868 | pjmedia_snd_port_set_ec( pjsua_var.snd_port, pjsua_var.pool, |
| 1869 | pjsua_var.media_cfg.ec_tail_len, |
| 1870 | pjsua_var.media_cfg.ec_options); |
Benny Prijono | eebe9af | 2006-06-13 22:57:13 +0000 | [diff] [blame] | 1871 | |
Benny Prijono | 52a9391 | 2006-08-04 20:54:37 +0000 | [diff] [blame] | 1872 | /* Connect sound port to the bridge */ |
| 1873 | status = pjmedia_snd_port_connect(pjsua_var.snd_port, |
| 1874 | conf_port ); |
| 1875 | if (status != PJ_SUCCESS) { |
| 1876 | pjsua_perror(THIS_FILE, "Unable to connect conference port to " |
| 1877 | "sound device", status); |
| 1878 | pjmedia_snd_port_destroy(pjsua_var.snd_port); |
| 1879 | pjsua_var.snd_port = NULL; |
| 1880 | return status; |
| 1881 | } |
| 1882 | |
Benny Prijono | eebe9af | 2006-06-13 22:57:13 +0000 | [diff] [blame] | 1883 | /* Save the device IDs */ |
| 1884 | pjsua_var.cap_dev = capture_dev; |
| 1885 | pjsua_var.play_dev = playback_dev; |
| 1886 | |
Benny Prijono | c53c6d7 | 2006-11-27 09:54:03 +0000 | [diff] [blame] | 1887 | /* Update sound device name. */ |
| 1888 | strm = pjmedia_snd_port_get_snd_stream(pjsua_var.snd_port); |
| 1889 | pjmedia_snd_stream_get_info(strm, &si); |
| 1890 | play_info = pjmedia_snd_get_dev_info(si.rec_id); |
| 1891 | |
Benny Prijono | f3758ee | 2008-02-26 15:32:16 +0000 | [diff] [blame] | 1892 | if (si.clock_rate != pjsua_var.media_cfg.clock_rate) { |
| 1893 | char tmp_buf[128]; |
| 1894 | int tmp_buf_len = sizeof(tmp_buf); |
| 1895 | |
| 1896 | tmp_buf_len = pj_ansi_snprintf(tmp_buf, sizeof(tmp_buf)-1, "%s (%dKHz)", |
| 1897 | play_info->name, si.clock_rate/1000); |
| 1898 | pj_strset(&tmp, tmp_buf, tmp_buf_len); |
| 1899 | pjmedia_conf_set_port0_name(pjsua_var.mconf, &tmp); |
| 1900 | } else { |
| 1901 | pjmedia_conf_set_port0_name(pjsua_var.mconf, |
| 1902 | pj_cstr(&tmp, play_info->name)); |
| 1903 | } |
Benny Prijono | c53c6d7 | 2006-11-27 09:54:03 +0000 | [diff] [blame] | 1904 | |
Benny Prijono | eebe9af | 2006-06-13 22:57:13 +0000 | [diff] [blame] | 1905 | return PJ_SUCCESS; |
| 1906 | } |
| 1907 | |
| 1908 | |
| 1909 | /* |
Benny Prijono | ebdf877 | 2007-02-01 19:25:50 +0000 | [diff] [blame] | 1910 | * Get currently active sound devices. If sound devices has not been created |
| 1911 | * (for example when pjsua_start() is not called), it is possible that |
| 1912 | * the function returns PJ_SUCCESS with -1 as device IDs. |
| 1913 | */ |
| 1914 | PJ_DEF(pj_status_t) pjsua_get_snd_dev(int *capture_dev, |
| 1915 | int *playback_dev) |
| 1916 | { |
| 1917 | if (capture_dev) { |
| 1918 | *capture_dev = pjsua_var.cap_dev; |
| 1919 | } |
| 1920 | if (playback_dev) { |
| 1921 | *playback_dev = pjsua_var.play_dev; |
| 1922 | } |
| 1923 | |
| 1924 | return PJ_SUCCESS; |
| 1925 | } |
| 1926 | |
| 1927 | |
| 1928 | /* |
Benny Prijono | eebe9af | 2006-06-13 22:57:13 +0000 | [diff] [blame] | 1929 | * Use null sound device. |
| 1930 | */ |
| 1931 | PJ_DEF(pj_status_t) pjsua_set_null_snd_dev(void) |
| 1932 | { |
| 1933 | pjmedia_port *conf_port; |
| 1934 | pj_status_t status; |
| 1935 | |
| 1936 | /* Close existing sound device */ |
| 1937 | close_snd_dev(); |
| 1938 | |
| 1939 | PJ_LOG(4,(THIS_FILE, "Opening null sound device..")); |
| 1940 | |
| 1941 | /* Get the port0 of the conference bridge. */ |
| 1942 | conf_port = pjmedia_conf_get_master_port(pjsua_var.mconf); |
| 1943 | pj_assert(conf_port != NULL); |
| 1944 | |
| 1945 | /* Create master port, connecting port0 of the conference bridge to |
| 1946 | * a null port. |
| 1947 | */ |
| 1948 | status = pjmedia_master_port_create(pjsua_var.pool, pjsua_var.null_port, |
| 1949 | conf_port, 0, &pjsua_var.null_snd); |
| 1950 | if (status != PJ_SUCCESS) { |
| 1951 | pjsua_perror(THIS_FILE, "Unable to create null sound device", |
| 1952 | status); |
| 1953 | return status; |
| 1954 | } |
| 1955 | |
| 1956 | /* Start the master port */ |
| 1957 | status = pjmedia_master_port_start(pjsua_var.null_snd); |
| 1958 | PJ_ASSERT_RETURN(status == PJ_SUCCESS, status); |
| 1959 | |
| 1960 | return PJ_SUCCESS; |
| 1961 | } |
| 1962 | |
| 1963 | |
Benny Prijono | e909eac | 2006-07-27 22:04:56 +0000 | [diff] [blame] | 1964 | |
| 1965 | /* |
| 1966 | * Use no device! |
| 1967 | */ |
| 1968 | PJ_DEF(pjmedia_port*) pjsua_set_no_snd_dev(void) |
| 1969 | { |
| 1970 | /* Close existing sound device */ |
| 1971 | close_snd_dev(); |
| 1972 | |
| 1973 | pjsua_var.no_snd = PJ_TRUE; |
| 1974 | return pjmedia_conf_get_master_port(pjsua_var.mconf); |
| 1975 | } |
| 1976 | |
| 1977 | |
Benny Prijono | f20687a | 2006-08-04 18:27:19 +0000 | [diff] [blame] | 1978 | /* |
| 1979 | * Configure the AEC settings of the sound port. |
| 1980 | */ |
Benny Prijono | 5da5043 | 2006-08-07 10:24:52 +0000 | [diff] [blame] | 1981 | 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] | 1982 | { |
| 1983 | pjsua_var.media_cfg.ec_tail_len = tail_ms; |
| 1984 | |
| 1985 | if (pjsua_var.snd_port) |
Benny Prijono | 5da5043 | 2006-08-07 10:24:52 +0000 | [diff] [blame] | 1986 | return pjmedia_snd_port_set_ec( pjsua_var.snd_port, pjsua_var.pool, |
| 1987 | tail_ms, options); |
Benny Prijono | f20687a | 2006-08-04 18:27:19 +0000 | [diff] [blame] | 1988 | |
| 1989 | return PJ_SUCCESS; |
| 1990 | } |
| 1991 | |
| 1992 | |
| 1993 | /* |
| 1994 | * Get current AEC tail length. |
| 1995 | */ |
Benny Prijono | 22dfe59 | 2006-08-06 12:07:13 +0000 | [diff] [blame] | 1996 | 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] | 1997 | { |
| 1998 | *p_tail_ms = pjsua_var.media_cfg.ec_tail_len; |
| 1999 | return PJ_SUCCESS; |
| 2000 | } |
| 2001 | |
Benny Prijono | e909eac | 2006-07-27 22:04:56 +0000 | [diff] [blame] | 2002 | |
Benny Prijono | eebe9af | 2006-06-13 22:57:13 +0000 | [diff] [blame] | 2003 | /***************************************************************************** |
| 2004 | * Codecs. |
| 2005 | */ |
| 2006 | |
| 2007 | /* |
| 2008 | * Enum all supported codecs in the system. |
| 2009 | */ |
| 2010 | PJ_DEF(pj_status_t) pjsua_enum_codecs( pjsua_codec_info id[], |
| 2011 | unsigned *p_count ) |
| 2012 | { |
| 2013 | pjmedia_codec_mgr *codec_mgr; |
| 2014 | pjmedia_codec_info info[32]; |
| 2015 | unsigned i, count, prio[32]; |
| 2016 | pj_status_t status; |
| 2017 | |
| 2018 | codec_mgr = pjmedia_endpt_get_codec_mgr(pjsua_var.med_endpt); |
| 2019 | count = PJ_ARRAY_SIZE(info); |
| 2020 | status = pjmedia_codec_mgr_enum_codecs( codec_mgr, &count, info, prio); |
| 2021 | if (status != PJ_SUCCESS) { |
| 2022 | *p_count = 0; |
| 2023 | return status; |
| 2024 | } |
| 2025 | |
| 2026 | if (count > *p_count) count = *p_count; |
| 2027 | |
| 2028 | for (i=0; i<count; ++i) { |
| 2029 | pjmedia_codec_info_to_id(&info[i], id[i].buf_, sizeof(id[i].buf_)); |
| 2030 | id[i].codec_id = pj_str(id[i].buf_); |
| 2031 | id[i].priority = (pj_uint8_t) prio[i]; |
| 2032 | } |
| 2033 | |
| 2034 | *p_count = count; |
| 2035 | |
| 2036 | return PJ_SUCCESS; |
| 2037 | } |
| 2038 | |
| 2039 | |
| 2040 | /* |
| 2041 | * Change codec priority. |
| 2042 | */ |
| 2043 | PJ_DEF(pj_status_t) pjsua_codec_set_priority( const pj_str_t *codec_id, |
| 2044 | pj_uint8_t priority ) |
| 2045 | { |
| 2046 | pjmedia_codec_mgr *codec_mgr; |
| 2047 | |
| 2048 | codec_mgr = pjmedia_endpt_get_codec_mgr(pjsua_var.med_endpt); |
| 2049 | |
| 2050 | return pjmedia_codec_mgr_set_codec_priority(codec_mgr, codec_id, |
| 2051 | priority); |
| 2052 | } |
| 2053 | |
| 2054 | |
| 2055 | /* |
| 2056 | * Get codec parameters. |
| 2057 | */ |
| 2058 | PJ_DEF(pj_status_t) pjsua_codec_get_param( const pj_str_t *codec_id, |
| 2059 | pjmedia_codec_param *param ) |
| 2060 | { |
| 2061 | const pjmedia_codec_info *info; |
| 2062 | pjmedia_codec_mgr *codec_mgr; |
| 2063 | unsigned count = 1; |
| 2064 | pj_status_t status; |
| 2065 | |
| 2066 | codec_mgr = pjmedia_endpt_get_codec_mgr(pjsua_var.med_endpt); |
| 2067 | |
| 2068 | status = pjmedia_codec_mgr_find_codecs_by_id(codec_mgr, codec_id, |
| 2069 | &count, &info, NULL); |
| 2070 | if (status != PJ_SUCCESS) |
| 2071 | return status; |
| 2072 | |
| 2073 | if (count != 1) |
| 2074 | return PJ_ENOTFOUND; |
| 2075 | |
| 2076 | status = pjmedia_codec_mgr_get_default_param( codec_mgr, info, param); |
| 2077 | return status; |
| 2078 | } |
| 2079 | |
| 2080 | |
| 2081 | /* |
| 2082 | * Set codec parameters. |
| 2083 | */ |
| 2084 | PJ_DEF(pj_status_t) pjsua_codec_set_param( const pj_str_t *id, |
| 2085 | const pjmedia_codec_param *param) |
| 2086 | { |
Benny Prijono | 00cae61 | 2006-07-31 15:19:36 +0000 | [diff] [blame] | 2087 | PJ_UNUSED_ARG(id); |
| 2088 | PJ_UNUSED_ARG(param); |
Benny Prijono | eebe9af | 2006-06-13 22:57:13 +0000 | [diff] [blame] | 2089 | PJ_TODO(set_codec_param); |
| 2090 | return PJ_SUCCESS; |
| 2091 | } |