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