Benny Prijono | eebe9af | 2006-06-13 22:57:13 +0000 | [diff] [blame] | 1 | /* $Id$ */ |
| 2 | /* |
Benny Prijono | 844653c | 2008-12-23 17:27:53 +0000 | [diff] [blame] | 3 | * Copyright (C) 2008-2009 Teluu Inc. (http://www.teluu.com) |
Benny Prijono | 32177c0 | 2008-06-20 22:44:47 +0000 | [diff] [blame] | 4 | * Copyright (C) 2003-2008 Benny Prijono <benny@prijono.org> |
Benny Prijono | eebe9af | 2006-06-13 22:57:13 +0000 | [diff] [blame] | 5 | * |
| 6 | * This program is free software; you can redistribute it and/or modify |
| 7 | * it under the terms of the GNU General Public License as published by |
| 8 | * the Free Software Foundation; either version 2 of the License, or |
| 9 | * (at your option) any later version. |
| 10 | * |
| 11 | * This program is distributed in the hope that it will be useful, |
| 12 | * but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 13 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
| 14 | * GNU General Public License for more details. |
| 15 | * |
| 16 | * You should have received a copy of the GNU General Public License |
| 17 | * along with this program; if not, write to the Free Software |
| 18 | * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA |
| 19 | */ |
| 20 | #include <pjsua-lib/pjsua.h> |
| 21 | #include <pjsua-lib/pjsua_internal.h> |
| 22 | |
| 23 | |
| 24 | #define THIS_FILE "pjsua_media.c" |
| 25 | |
Nanang Izzuddin | 68559c3 | 2008-06-13 17:01:46 +0000 | [diff] [blame] | 26 | #define DEFAULT_RTP_PORT 4000 |
| 27 | |
| 28 | #define NULL_SND_DEV_ID -99 |
Benny Prijono | 80eee89 | 2007-11-03 22:43:23 +0000 | [diff] [blame] | 29 | |
| 30 | #ifndef PJSUA_REQUIRE_CONSECUTIVE_RTCP_PORT |
| 31 | # define PJSUA_REQUIRE_CONSECUTIVE_RTCP_PORT 0 |
| 32 | #endif |
| 33 | |
Benny Prijono | eebe9af | 2006-06-13 22:57:13 +0000 | [diff] [blame] | 34 | |
Benny Prijono | de47956 | 2007-03-15 10:23:55 +0000 | [diff] [blame] | 35 | /* Next RTP port to be used */ |
| 36 | static pj_uint16_t next_rtp_port; |
Benny Prijono | eebe9af | 2006-06-13 22:57:13 +0000 | [diff] [blame] | 37 | |
| 38 | /* Close existing sound device */ |
| 39 | static void close_snd_dev(void); |
| 40 | |
| 41 | |
Benny Prijono | f76e139 | 2008-06-06 14:51:48 +0000 | [diff] [blame] | 42 | static void pjsua_media_config_dup(pj_pool_t *pool, |
| 43 | pjsua_media_config *dst, |
| 44 | const pjsua_media_config *src) |
| 45 | { |
| 46 | pj_memcpy(dst, src, sizeof(*src)); |
| 47 | pj_strdup(pool, &dst->turn_server, &src->turn_server); |
| 48 | pj_stun_auth_cred_dup(pool, &dst->turn_auth_cred, &src->turn_auth_cred); |
| 49 | } |
| 50 | |
Benny Prijono | eebe9af | 2006-06-13 22:57:13 +0000 | [diff] [blame] | 51 | /** |
| 52 | * Init media subsystems. |
| 53 | */ |
| 54 | pj_status_t pjsua_media_subsys_init(const pjsua_media_config *cfg) |
| 55 | { |
Benny Prijono | ba5926a | 2007-05-02 11:29:37 +0000 | [diff] [blame] | 56 | pj_str_t codec_id = {NULL, 0}; |
Benny Prijono | 0498d90 | 2006-06-19 14:49:14 +0000 | [diff] [blame] | 57 | unsigned opt; |
Benny Prijono | eebe9af | 2006-06-13 22:57:13 +0000 | [diff] [blame] | 58 | pj_status_t status; |
| 59 | |
Benny Prijono | fc24e69 | 2007-01-27 18:31:51 +0000 | [diff] [blame] | 60 | /* To suppress warning about unused var when all codecs are disabled */ |
| 61 | PJ_UNUSED_ARG(codec_id); |
| 62 | |
Benny Prijono | eebe9af | 2006-06-13 22:57:13 +0000 | [diff] [blame] | 63 | /* Copy configuration */ |
Benny Prijono | f76e139 | 2008-06-06 14:51:48 +0000 | [diff] [blame] | 64 | pjsua_media_config_dup(pjsua_var.pool, &pjsua_var.media_cfg, cfg); |
Benny Prijono | eebe9af | 2006-06-13 22:57:13 +0000 | [diff] [blame] | 65 | |
| 66 | /* Normalize configuration */ |
Benny Prijono | 50f19b3 | 2008-03-11 13:15:43 +0000 | [diff] [blame] | 67 | if (pjsua_var.media_cfg.snd_clock_rate == 0) { |
| 68 | pjsua_var.media_cfg.snd_clock_rate = pjsua_var.media_cfg.clock_rate; |
| 69 | } |
Benny Prijono | eebe9af | 2006-06-13 22:57:13 +0000 | [diff] [blame] | 70 | |
| 71 | if (pjsua_var.media_cfg.has_ioqueue && |
| 72 | pjsua_var.media_cfg.thread_cnt == 0) |
| 73 | { |
| 74 | pjsua_var.media_cfg.thread_cnt = 1; |
| 75 | } |
| 76 | |
| 77 | if (pjsua_var.media_cfg.max_media_ports < pjsua_var.ua_cfg.max_calls) { |
| 78 | pjsua_var.media_cfg.max_media_ports = pjsua_var.ua_cfg.max_calls + 2; |
| 79 | } |
| 80 | |
| 81 | /* Create media endpoint. */ |
| 82 | status = pjmedia_endpt_create(&pjsua_var.cp.factory, |
| 83 | pjsua_var.media_cfg.has_ioqueue? NULL : |
| 84 | pjsip_endpt_get_ioqueue(pjsua_var.endpt), |
| 85 | pjsua_var.media_cfg.thread_cnt, |
| 86 | &pjsua_var.med_endpt); |
| 87 | if (status != PJ_SUCCESS) { |
| 88 | pjsua_perror(THIS_FILE, |
| 89 | "Media stack initialization has returned error", |
| 90 | status); |
| 91 | return status; |
| 92 | } |
| 93 | |
| 94 | /* Register all codecs */ |
Nanang Izzuddin | 493a8db | 2008-08-15 13:17:39 +0000 | [diff] [blame] | 95 | |
Benny Prijono | eebe9af | 2006-06-13 22:57:13 +0000 | [diff] [blame] | 96 | #if PJMEDIA_HAS_SPEEX_CODEC |
| 97 | /* Register speex. */ |
Nanang Izzuddin | 9dbad15 | 2008-06-10 18:56:10 +0000 | [diff] [blame] | 98 | status = pjmedia_codec_speex_init(pjsua_var.med_endpt, |
| 99 | 0, |
| 100 | pjsua_var.media_cfg.quality, |
| 101 | -1); |
Benny Prijono | eebe9af | 2006-06-13 22:57:13 +0000 | [diff] [blame] | 102 | if (status != PJ_SUCCESS) { |
| 103 | pjsua_perror(THIS_FILE, "Error initializing Speex codec", |
| 104 | status); |
| 105 | return status; |
| 106 | } |
Benny Prijono | 7ca96da | 2006-08-07 12:11:40 +0000 | [diff] [blame] | 107 | |
| 108 | /* Set speex/16000 to higher priority*/ |
| 109 | codec_id = pj_str("speex/16000"); |
| 110 | pjmedia_codec_mgr_set_codec_priority( |
| 111 | pjmedia_endpt_get_codec_mgr(pjsua_var.med_endpt), |
| 112 | &codec_id, PJMEDIA_CODEC_PRIO_NORMAL+2); |
| 113 | |
| 114 | /* Set speex/8000 to next higher priority*/ |
| 115 | codec_id = pj_str("speex/8000"); |
| 116 | pjmedia_codec_mgr_set_codec_priority( |
| 117 | pjmedia_endpt_get_codec_mgr(pjsua_var.med_endpt), |
| 118 | &codec_id, PJMEDIA_CODEC_PRIO_NORMAL+1); |
| 119 | |
| 120 | |
| 121 | |
Benny Prijono | eebe9af | 2006-06-13 22:57:13 +0000 | [diff] [blame] | 122 | #endif /* PJMEDIA_HAS_SPEEX_CODEC */ |
| 123 | |
Benny Prijono | 00cae61 | 2006-07-31 15:19:36 +0000 | [diff] [blame] | 124 | #if PJMEDIA_HAS_ILBC_CODEC |
| 125 | /* Register iLBC. */ |
| 126 | status = pjmedia_codec_ilbc_init( pjsua_var.med_endpt, |
| 127 | pjsua_var.media_cfg.ilbc_mode); |
| 128 | if (status != PJ_SUCCESS) { |
| 129 | pjsua_perror(THIS_FILE, "Error initializing iLBC codec", |
| 130 | status); |
| 131 | return status; |
| 132 | } |
| 133 | #endif /* PJMEDIA_HAS_ILBC_CODEC */ |
| 134 | |
Benny Prijono | eebe9af | 2006-06-13 22:57:13 +0000 | [diff] [blame] | 135 | #if PJMEDIA_HAS_GSM_CODEC |
| 136 | /* Register GSM */ |
| 137 | status = pjmedia_codec_gsm_init(pjsua_var.med_endpt); |
| 138 | if (status != PJ_SUCCESS) { |
| 139 | pjsua_perror(THIS_FILE, "Error initializing GSM codec", |
| 140 | status); |
| 141 | return status; |
| 142 | } |
| 143 | #endif /* PJMEDIA_HAS_GSM_CODEC */ |
| 144 | |
| 145 | #if PJMEDIA_HAS_G711_CODEC |
| 146 | /* Register PCMA and PCMU */ |
| 147 | status = pjmedia_codec_g711_init(pjsua_var.med_endpt); |
| 148 | if (status != PJ_SUCCESS) { |
| 149 | pjsua_perror(THIS_FILE, "Error initializing G711 codec", |
| 150 | status); |
| 151 | return status; |
| 152 | } |
| 153 | #endif /* PJMEDIA_HAS_G711_CODEC */ |
| 154 | |
Benny Prijono | 7ffd775 | 2008-03-17 14:07:53 +0000 | [diff] [blame] | 155 | #if PJMEDIA_HAS_G722_CODEC |
| 156 | status = pjmedia_codec_g722_init( pjsua_var.med_endpt ); |
| 157 | if (status != PJ_SUCCESS) { |
| 158 | pjsua_perror(THIS_FILE, "Error initializing G722 codec", |
| 159 | status); |
| 160 | return status; |
| 161 | } |
| 162 | #endif /* PJMEDIA_HAS_G722_CODEC */ |
| 163 | |
Benny Prijono | a4e7cdd | 2008-08-19 15:01:48 +0000 | [diff] [blame] | 164 | #if PJMEDIA_HAS_INTEL_IPP |
Nanang Izzuddin | 493a8db | 2008-08-15 13:17:39 +0000 | [diff] [blame] | 165 | /* Register IPP codecs */ |
| 166 | status = pjmedia_codec_ipp_init(pjsua_var.med_endpt); |
| 167 | if (status != PJ_SUCCESS) { |
| 168 | pjsua_perror(THIS_FILE, "Error initializing IPP codecs", |
| 169 | status); |
| 170 | return status; |
| 171 | } |
| 172 | |
Benny Prijono | a4e7cdd | 2008-08-19 15:01:48 +0000 | [diff] [blame] | 173 | #endif /* PJMEDIA_HAS_INTEL_IPP */ |
Nanang Izzuddin | 493a8db | 2008-08-15 13:17:39 +0000 | [diff] [blame] | 174 | |
Nanang Izzuddin | 81db8c7 | 2009-02-05 10:59:14 +0000 | [diff] [blame^] | 175 | #if PJMEDIA_HAS_PASSTHROUGH_CODECS |
| 176 | /* Register passthrough codecs */ |
| 177 | status = pjmedia_codec_passthrough_init(pjsua_var.med_endpt); |
| 178 | if (status != PJ_SUCCESS) { |
| 179 | pjsua_perror(THIS_FILE, "Error initializing passthrough codecs", |
| 180 | status); |
| 181 | return status; |
| 182 | } |
| 183 | #endif /* PJMEDIA_HAS_PASSTHROUGH_CODECS */ |
| 184 | |
Benny Prijono | eebe9af | 2006-06-13 22:57:13 +0000 | [diff] [blame] | 185 | #if PJMEDIA_HAS_L16_CODEC |
| 186 | /* Register L16 family codecs, but disable all */ |
| 187 | status = pjmedia_codec_l16_init(pjsua_var.med_endpt, 0); |
| 188 | if (status != PJ_SUCCESS) { |
| 189 | pjsua_perror(THIS_FILE, "Error initializing L16 codecs", |
| 190 | status); |
| 191 | return status; |
| 192 | } |
| 193 | |
| 194 | /* Disable ALL L16 codecs */ |
| 195 | codec_id = pj_str("L16"); |
| 196 | pjmedia_codec_mgr_set_codec_priority( |
| 197 | pjmedia_endpt_get_codec_mgr(pjsua_var.med_endpt), |
| 198 | &codec_id, PJMEDIA_CODEC_PRIO_DISABLED); |
| 199 | |
| 200 | #endif /* PJMEDIA_HAS_L16_CODEC */ |
| 201 | |
| 202 | |
| 203 | /* Save additional conference bridge parameters for future |
| 204 | * reference. |
| 205 | */ |
Benny Prijono | 7d60d05 | 2008-03-29 12:24:20 +0000 | [diff] [blame] | 206 | pjsua_var.mconf_cfg.channel_count = pjsua_var.media_cfg.channel_count; |
Benny Prijono | eebe9af | 2006-06-13 22:57:13 +0000 | [diff] [blame] | 207 | pjsua_var.mconf_cfg.bits_per_sample = 16; |
Benny Prijono | 6e7c5ad | 2008-03-13 10:15:16 +0000 | [diff] [blame] | 208 | pjsua_var.mconf_cfg.samples_per_frame = pjsua_var.media_cfg.clock_rate * |
| 209 | pjsua_var.mconf_cfg.channel_count * |
| 210 | pjsua_var.media_cfg.audio_frame_ptime / |
| 211 | 1000; |
Benny Prijono | eebe9af | 2006-06-13 22:57:13 +0000 | [diff] [blame] | 212 | |
Benny Prijono | 0498d90 | 2006-06-19 14:49:14 +0000 | [diff] [blame] | 213 | /* Init options for conference bridge. */ |
| 214 | opt = PJMEDIA_CONF_NO_DEVICE; |
| 215 | if (pjsua_var.media_cfg.quality >= 3 && |
Benny Prijono | 7ca96da | 2006-08-07 12:11:40 +0000 | [diff] [blame] | 216 | pjsua_var.media_cfg.quality <= 4) |
Benny Prijono | 0498d90 | 2006-06-19 14:49:14 +0000 | [diff] [blame] | 217 | { |
| 218 | opt |= PJMEDIA_CONF_SMALL_FILTER; |
| 219 | } |
| 220 | else if (pjsua_var.media_cfg.quality < 3) { |
| 221 | opt |= PJMEDIA_CONF_USE_LINEAR; |
| 222 | } |
| 223 | |
| 224 | |
Benny Prijono | eebe9af | 2006-06-13 22:57:13 +0000 | [diff] [blame] | 225 | /* Init conference bridge. */ |
| 226 | status = pjmedia_conf_create(pjsua_var.pool, |
| 227 | pjsua_var.media_cfg.max_media_ports, |
| 228 | pjsua_var.media_cfg.clock_rate, |
| 229 | pjsua_var.mconf_cfg.channel_count, |
| 230 | pjsua_var.mconf_cfg.samples_per_frame, |
| 231 | pjsua_var.mconf_cfg.bits_per_sample, |
Benny Prijono | 0498d90 | 2006-06-19 14:49:14 +0000 | [diff] [blame] | 232 | opt, &pjsua_var.mconf); |
Benny Prijono | eebe9af | 2006-06-13 22:57:13 +0000 | [diff] [blame] | 233 | if (status != PJ_SUCCESS) { |
Benny Prijono | 50f19b3 | 2008-03-11 13:15:43 +0000 | [diff] [blame] | 234 | pjsua_perror(THIS_FILE, "Error creating conference bridge", |
Benny Prijono | eebe9af | 2006-06-13 22:57:13 +0000 | [diff] [blame] | 235 | status); |
| 236 | return status; |
| 237 | } |
| 238 | |
| 239 | /* Create null port just in case user wants to use null sound. */ |
| 240 | status = pjmedia_null_port_create(pjsua_var.pool, |
| 241 | pjsua_var.media_cfg.clock_rate, |
| 242 | pjsua_var.mconf_cfg.channel_count, |
| 243 | pjsua_var.mconf_cfg.samples_per_frame, |
| 244 | pjsua_var.mconf_cfg.bits_per_sample, |
| 245 | &pjsua_var.null_port); |
| 246 | PJ_ASSERT_RETURN(status == PJ_SUCCESS, status); |
| 247 | |
Benny Prijono | 6ba8c54 | 2007-10-16 01:34:14 +0000 | [diff] [blame] | 248 | /* Perform NAT detection */ |
| 249 | pjsua_detect_nat_type(); |
| 250 | |
Benny Prijono | eebe9af | 2006-06-13 22:57:13 +0000 | [diff] [blame] | 251 | return PJ_SUCCESS; |
| 252 | } |
| 253 | |
| 254 | |
| 255 | /* |
| 256 | * Create RTP and RTCP socket pair, and possibly resolve their public |
| 257 | * address via STUN. |
| 258 | */ |
| 259 | static pj_status_t create_rtp_rtcp_sock(const pjsua_transport_config *cfg, |
| 260 | pjmedia_sock_info *skinfo) |
| 261 | { |
| 262 | enum { |
| 263 | RTP_RETRY = 100 |
| 264 | }; |
| 265 | int i; |
Benny Prijono | 0a5cad8 | 2006-09-26 13:21:02 +0000 | [diff] [blame] | 266 | pj_sockaddr_in bound_addr; |
Benny Prijono | eebe9af | 2006-06-13 22:57:13 +0000 | [diff] [blame] | 267 | pj_sockaddr_in mapped_addr[2]; |
| 268 | pj_status_t status = PJ_SUCCESS; |
Benny Prijono | 38fb3ea | 2008-01-02 08:27:03 +0000 | [diff] [blame] | 269 | char addr_buf[PJ_INET6_ADDRSTRLEN+2]; |
Benny Prijono | eebe9af | 2006-06-13 22:57:13 +0000 | [diff] [blame] | 270 | pj_sock_t sock[2]; |
| 271 | |
Benny Prijono | c97608e | 2007-03-23 16:34:20 +0000 | [diff] [blame] | 272 | /* Make sure STUN server resolution has completed */ |
| 273 | status = pjsua_resolve_stun_server(PJ_TRUE); |
| 274 | if (status != PJ_SUCCESS) { |
| 275 | pjsua_perror(THIS_FILE, "Error resolving STUN server", status); |
| 276 | return status; |
| 277 | } |
| 278 | |
Benny Prijono | de47956 | 2007-03-15 10:23:55 +0000 | [diff] [blame] | 279 | if (next_rtp_port == 0) |
| 280 | next_rtp_port = (pj_uint16_t)cfg->port; |
Benny Prijono | eebe9af | 2006-06-13 22:57:13 +0000 | [diff] [blame] | 281 | |
| 282 | for (i=0; i<2; ++i) |
| 283 | sock[i] = PJ_INVALID_SOCKET; |
| 284 | |
Benny Prijono | 0a5cad8 | 2006-09-26 13:21:02 +0000 | [diff] [blame] | 285 | bound_addr.sin_addr.s_addr = PJ_INADDR_ANY; |
| 286 | if (cfg->bound_addr.slen) { |
| 287 | status = pj_sockaddr_in_set_str_addr(&bound_addr, &cfg->bound_addr); |
| 288 | if (status != PJ_SUCCESS) { |
| 289 | pjsua_perror(THIS_FILE, "Unable to resolve transport bind address", |
| 290 | status); |
| 291 | return status; |
| 292 | } |
| 293 | } |
Benny Prijono | eebe9af | 2006-06-13 22:57:13 +0000 | [diff] [blame] | 294 | |
| 295 | /* Loop retry to bind RTP and RTCP sockets. */ |
Benny Prijono | de47956 | 2007-03-15 10:23:55 +0000 | [diff] [blame] | 296 | for (i=0; i<RTP_RETRY; ++i, next_rtp_port += 2) { |
Benny Prijono | eebe9af | 2006-06-13 22:57:13 +0000 | [diff] [blame] | 297 | |
| 298 | /* Create and bind RTP socket. */ |
Benny Prijono | 8ab968f | 2007-07-20 08:08:30 +0000 | [diff] [blame] | 299 | 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] | 300 | if (status != PJ_SUCCESS) { |
| 301 | pjsua_perror(THIS_FILE, "socket() error", status); |
| 302 | return status; |
| 303 | } |
| 304 | |
Benny Prijono | 0cdcd3f | 2007-12-09 14:14:11 +0000 | [diff] [blame] | 305 | status=pj_sock_bind_in(sock[0], pj_ntohl(bound_addr.sin_addr.s_addr), |
| 306 | next_rtp_port); |
Benny Prijono | eebe9af | 2006-06-13 22:57:13 +0000 | [diff] [blame] | 307 | if (status != PJ_SUCCESS) { |
| 308 | pj_sock_close(sock[0]); |
| 309 | sock[0] = PJ_INVALID_SOCKET; |
| 310 | continue; |
| 311 | } |
| 312 | |
| 313 | /* Create and bind RTCP socket. */ |
Benny Prijono | 8ab968f | 2007-07-20 08:08:30 +0000 | [diff] [blame] | 314 | 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] | 315 | if (status != PJ_SUCCESS) { |
| 316 | pjsua_perror(THIS_FILE, "socket() error", status); |
| 317 | pj_sock_close(sock[0]); |
| 318 | return status; |
| 319 | } |
| 320 | |
Benny Prijono | 0cdcd3f | 2007-12-09 14:14:11 +0000 | [diff] [blame] | 321 | status=pj_sock_bind_in(sock[1], pj_ntohl(bound_addr.sin_addr.s_addr), |
| 322 | (pj_uint16_t)(next_rtp_port+1)); |
Benny Prijono | eebe9af | 2006-06-13 22:57:13 +0000 | [diff] [blame] | 323 | if (status != PJ_SUCCESS) { |
| 324 | pj_sock_close(sock[0]); |
| 325 | sock[0] = PJ_INVALID_SOCKET; |
| 326 | |
| 327 | pj_sock_close(sock[1]); |
| 328 | sock[1] = PJ_INVALID_SOCKET; |
| 329 | continue; |
| 330 | } |
| 331 | |
| 332 | /* |
| 333 | * If we're configured to use STUN, then find out the mapped address, |
| 334 | * and make sure that the mapped RTCP port is adjacent with the RTP. |
| 335 | */ |
Benny Prijono | c97608e | 2007-03-23 16:34:20 +0000 | [diff] [blame] | 336 | if (pjsua_var.stun_srv.addr.sa_family != 0) { |
| 337 | char ip_addr[32]; |
| 338 | pj_str_t stun_srv; |
| 339 | |
| 340 | pj_ansi_strcpy(ip_addr, |
| 341 | pj_inet_ntoa(pjsua_var.stun_srv.ipv4.sin_addr)); |
| 342 | stun_srv = pj_str(ip_addr); |
| 343 | |
Benny Prijono | 14c2b86 | 2007-02-21 00:40:05 +0000 | [diff] [blame] | 344 | status=pjstun_get_mapped_addr(&pjsua_var.cp.factory, 2, sock, |
Benny Prijono | af09dc3 | 2007-04-22 12:48:30 +0000 | [diff] [blame] | 345 | &stun_srv, pj_ntohs(pjsua_var.stun_srv.ipv4.sin_port), |
| 346 | &stun_srv, pj_ntohs(pjsua_var.stun_srv.ipv4.sin_port), |
Benny Prijono | eebe9af | 2006-06-13 22:57:13 +0000 | [diff] [blame] | 347 | mapped_addr); |
| 348 | if (status != PJ_SUCCESS) { |
| 349 | pjsua_perror(THIS_FILE, "STUN resolve error", status); |
| 350 | goto on_error; |
| 351 | } |
| 352 | |
Benny Prijono | 80eee89 | 2007-11-03 22:43:23 +0000 | [diff] [blame] | 353 | #if PJSUA_REQUIRE_CONSECUTIVE_RTCP_PORT |
Benny Prijono | eebe9af | 2006-06-13 22:57:13 +0000 | [diff] [blame] | 354 | if (pj_ntohs(mapped_addr[1].sin_port) == |
| 355 | pj_ntohs(mapped_addr[0].sin_port)+1) |
| 356 | { |
| 357 | /* Success! */ |
| 358 | break; |
| 359 | } |
| 360 | |
| 361 | pj_sock_close(sock[0]); |
| 362 | sock[0] = PJ_INVALID_SOCKET; |
| 363 | |
| 364 | pj_sock_close(sock[1]); |
| 365 | sock[1] = PJ_INVALID_SOCKET; |
Benny Prijono | 80eee89 | 2007-11-03 22:43:23 +0000 | [diff] [blame] | 366 | #else |
| 367 | if (pj_ntohs(mapped_addr[1].sin_port) != |
| 368 | pj_ntohs(mapped_addr[0].sin_port)+1) |
| 369 | { |
| 370 | PJ_LOG(4,(THIS_FILE, |
| 371 | "Note: STUN mapped RTCP port %d is not adjacent" |
| 372 | " to RTP port %d", |
| 373 | pj_ntohs(mapped_addr[1].sin_port), |
| 374 | pj_ntohs(mapped_addr[0].sin_port))); |
| 375 | } |
| 376 | /* Success! */ |
| 377 | break; |
| 378 | #endif |
Benny Prijono | eebe9af | 2006-06-13 22:57:13 +0000 | [diff] [blame] | 379 | |
Benny Prijono | 0a5cad8 | 2006-09-26 13:21:02 +0000 | [diff] [blame] | 380 | } else if (cfg->public_addr.slen) { |
| 381 | |
| 382 | status = pj_sockaddr_in_init(&mapped_addr[0], &cfg->public_addr, |
Benny Prijono | de47956 | 2007-03-15 10:23:55 +0000 | [diff] [blame] | 383 | (pj_uint16_t)next_rtp_port); |
Benny Prijono | 0a5cad8 | 2006-09-26 13:21:02 +0000 | [diff] [blame] | 384 | if (status != PJ_SUCCESS) |
| 385 | goto on_error; |
| 386 | |
| 387 | status = pj_sockaddr_in_init(&mapped_addr[1], &cfg->public_addr, |
Benny Prijono | de47956 | 2007-03-15 10:23:55 +0000 | [diff] [blame] | 388 | (pj_uint16_t)(next_rtp_port+1)); |
Benny Prijono | 0a5cad8 | 2006-09-26 13:21:02 +0000 | [diff] [blame] | 389 | if (status != PJ_SUCCESS) |
| 390 | goto on_error; |
| 391 | |
| 392 | break; |
| 393 | |
Benny Prijono | eebe9af | 2006-06-13 22:57:13 +0000 | [diff] [blame] | 394 | } else { |
Benny Prijono | eebe9af | 2006-06-13 22:57:13 +0000 | [diff] [blame] | 395 | |
Benny Prijono | 42d08d2 | 2007-12-20 11:23:07 +0000 | [diff] [blame] | 396 | if (bound_addr.sin_addr.s_addr == 0) { |
| 397 | pj_sockaddr addr; |
| 398 | |
| 399 | /* Get local IP address. */ |
| 400 | status = pj_gethostip(pj_AF_INET(), &addr); |
| 401 | if (status != PJ_SUCCESS) |
| 402 | goto on_error; |
| 403 | |
| 404 | bound_addr.sin_addr.s_addr = addr.ipv4.sin_addr.s_addr; |
| 405 | } |
Benny Prijono | eebe9af | 2006-06-13 22:57:13 +0000 | [diff] [blame] | 406 | |
Benny Prijono | fe0b1d6 | 2007-12-05 04:07:37 +0000 | [diff] [blame] | 407 | for (i=0; i<2; ++i) { |
| 408 | pj_sockaddr_in_init(&mapped_addr[i], NULL, 0); |
Benny Prijono | 42d08d2 | 2007-12-20 11:23:07 +0000 | [diff] [blame] | 409 | 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] | 410 | } |
Benny Prijono | eebe9af | 2006-06-13 22:57:13 +0000 | [diff] [blame] | 411 | |
Benny Prijono | de47956 | 2007-03-15 10:23:55 +0000 | [diff] [blame] | 412 | mapped_addr[0].sin_port=pj_htons((pj_uint16_t)next_rtp_port); |
| 413 | 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] | 414 | break; |
| 415 | } |
| 416 | } |
| 417 | |
| 418 | if (sock[0] == PJ_INVALID_SOCKET) { |
| 419 | PJ_LOG(1,(THIS_FILE, |
| 420 | "Unable to find appropriate RTP/RTCP ports combination")); |
| 421 | goto on_error; |
| 422 | } |
| 423 | |
| 424 | |
| 425 | skinfo->rtp_sock = sock[0]; |
| 426 | pj_memcpy(&skinfo->rtp_addr_name, |
| 427 | &mapped_addr[0], sizeof(pj_sockaddr_in)); |
| 428 | |
| 429 | skinfo->rtcp_sock = sock[1]; |
| 430 | pj_memcpy(&skinfo->rtcp_addr_name, |
| 431 | &mapped_addr[1], sizeof(pj_sockaddr_in)); |
| 432 | |
Benny Prijono | 8b22ce1 | 2008-02-08 12:57:55 +0000 | [diff] [blame] | 433 | PJ_LOG(4,(THIS_FILE, "RTP socket reachable at %s", |
Benny Prijono | 5186eae | 2007-12-03 14:38:25 +0000 | [diff] [blame] | 434 | pj_sockaddr_print(&skinfo->rtp_addr_name, addr_buf, |
| 435 | sizeof(addr_buf), 3))); |
Benny Prijono | 8b22ce1 | 2008-02-08 12:57:55 +0000 | [diff] [blame] | 436 | PJ_LOG(4,(THIS_FILE, "RTCP socket reachable at %s", |
Benny Prijono | 5186eae | 2007-12-03 14:38:25 +0000 | [diff] [blame] | 437 | pj_sockaddr_print(&skinfo->rtcp_addr_name, addr_buf, |
| 438 | sizeof(addr_buf), 3))); |
Benny Prijono | eebe9af | 2006-06-13 22:57:13 +0000 | [diff] [blame] | 439 | |
Benny Prijono | de47956 | 2007-03-15 10:23:55 +0000 | [diff] [blame] | 440 | next_rtp_port += 2; |
Benny Prijono | eebe9af | 2006-06-13 22:57:13 +0000 | [diff] [blame] | 441 | return PJ_SUCCESS; |
| 442 | |
| 443 | on_error: |
| 444 | for (i=0; i<2; ++i) { |
| 445 | if (sock[i] != PJ_INVALID_SOCKET) |
| 446 | pj_sock_close(sock[i]); |
| 447 | } |
| 448 | return status; |
| 449 | } |
| 450 | |
Nanang Izzuddin | 148fd39 | 2008-06-16 09:52:50 +0000 | [diff] [blame] | 451 | /* Check if sound device is idle. */ |
| 452 | static void check_snd_dev_idle() |
| 453 | { |
| 454 | |
| 455 | /* Activate sound device auto-close timer if sound device is idle. |
| 456 | * It is idle when there is no port connection in the bridge. |
| 457 | */ |
| 458 | if ((pjsua_var.snd_port!=NULL || pjsua_var.null_snd!=NULL) && |
| 459 | pjsua_var.snd_idle_timer.id == PJ_FALSE && |
| 460 | pjmedia_conf_get_connect_count(pjsua_var.mconf) == 0 && |
| 461 | pjsua_var.media_cfg.snd_auto_close_time >= 0) |
| 462 | { |
| 463 | pj_time_val delay; |
| 464 | |
| 465 | delay.msec = 0; |
| 466 | delay.sec = pjsua_var.media_cfg.snd_auto_close_time; |
| 467 | |
| 468 | pjsua_var.snd_idle_timer.id = PJ_TRUE; |
| 469 | pjsip_endpt_schedule_timer(pjsua_var.endpt, &pjsua_var.snd_idle_timer, |
| 470 | &delay); |
| 471 | } |
| 472 | } |
| 473 | |
| 474 | |
Nanang Izzuddin | 68559c3 | 2008-06-13 17:01:46 +0000 | [diff] [blame] | 475 | /* Timer callback to close sound device */ |
| 476 | static void close_snd_timer_cb( pj_timer_heap_t *th, |
| 477 | pj_timer_entry *entry) |
| 478 | { |
| 479 | PJ_UNUSED_ARG(th); |
| 480 | |
| 481 | PJ_LOG(4,(THIS_FILE,"Closing sound device after idle for %d seconds", |
| 482 | pjsua_var.media_cfg.snd_auto_close_time)); |
| 483 | |
| 484 | entry->id = PJ_FALSE; |
| 485 | |
| 486 | close_snd_dev(); |
| 487 | } |
| 488 | |
Benny Prijono | eebe9af | 2006-06-13 22:57:13 +0000 | [diff] [blame] | 489 | |
| 490 | /* |
| 491 | * Start pjsua media subsystem. |
| 492 | */ |
| 493 | pj_status_t pjsua_media_subsys_start(void) |
| 494 | { |
| 495 | pj_status_t status; |
| 496 | |
| 497 | /* Create media for calls, if none is specified */ |
| 498 | if (pjsua_var.calls[0].med_tp == NULL) { |
| 499 | pjsua_transport_config transport_cfg; |
| 500 | |
| 501 | /* Create default transport config */ |
| 502 | pjsua_transport_config_default(&transport_cfg); |
| 503 | transport_cfg.port = DEFAULT_RTP_PORT; |
| 504 | |
| 505 | status = pjsua_media_transports_create(&transport_cfg); |
| 506 | if (status != PJ_SUCCESS) |
| 507 | return status; |
| 508 | } |
| 509 | |
Nanang Izzuddin | 68559c3 | 2008-06-13 17:01:46 +0000 | [diff] [blame] | 510 | pj_timer_entry_init(&pjsua_var.snd_idle_timer, PJ_FALSE, NULL, |
| 511 | &close_snd_timer_cb); |
Benny Prijono | eebe9af | 2006-06-13 22:57:13 +0000 | [diff] [blame] | 512 | |
| 513 | return PJ_SUCCESS; |
| 514 | } |
| 515 | |
| 516 | |
| 517 | /* |
| 518 | * Destroy pjsua media subsystem. |
| 519 | */ |
| 520 | pj_status_t pjsua_media_subsys_destroy(void) |
| 521 | { |
| 522 | unsigned i; |
| 523 | |
| 524 | close_snd_dev(); |
| 525 | |
| 526 | if (pjsua_var.mconf) { |
| 527 | pjmedia_conf_destroy(pjsua_var.mconf); |
| 528 | pjsua_var.mconf = NULL; |
| 529 | } |
| 530 | |
| 531 | if (pjsua_var.null_port) { |
| 532 | pjmedia_port_destroy(pjsua_var.null_port); |
| 533 | pjsua_var.null_port = NULL; |
| 534 | } |
| 535 | |
| 536 | /* Destroy file players */ |
| 537 | for (i=0; i<PJ_ARRAY_SIZE(pjsua_var.player); ++i) { |
| 538 | if (pjsua_var.player[i].port) { |
| 539 | pjmedia_port_destroy(pjsua_var.player[i].port); |
| 540 | pjsua_var.player[i].port = NULL; |
| 541 | } |
| 542 | } |
| 543 | |
| 544 | /* Destroy file recorders */ |
| 545 | for (i=0; i<PJ_ARRAY_SIZE(pjsua_var.recorder); ++i) { |
| 546 | if (pjsua_var.recorder[i].port) { |
| 547 | pjmedia_port_destroy(pjsua_var.recorder[i].port); |
| 548 | pjsua_var.recorder[i].port = NULL; |
| 549 | } |
| 550 | } |
| 551 | |
| 552 | /* Close media transports */ |
Benny Prijono | 0875ae8 | 2006-12-26 00:11:48 +0000 | [diff] [blame] | 553 | for (i=0; i<pjsua_var.ua_cfg.max_calls; ++i) { |
Benny Prijono | 311b63f | 2008-07-14 11:31:40 +0000 | [diff] [blame] | 554 | if (pjsua_var.calls[i].med_tp_st != PJSUA_MED_TP_IDLE) { |
| 555 | pjsua_media_channel_deinit(i); |
| 556 | } |
Benny Prijono | 40860c3 | 2008-09-04 13:55:33 +0000 | [diff] [blame] | 557 | if (pjsua_var.calls[i].med_tp && pjsua_var.calls[i].med_tp_auto_del) { |
| 558 | pjmedia_transport_close(pjsua_var.calls[i].med_tp); |
Benny Prijono | eebe9af | 2006-06-13 22:57:13 +0000 | [diff] [blame] | 559 | } |
Benny Prijono | 40860c3 | 2008-09-04 13:55:33 +0000 | [diff] [blame] | 560 | pjsua_var.calls[i].med_tp = NULL; |
Benny Prijono | eebe9af | 2006-06-13 22:57:13 +0000 | [diff] [blame] | 561 | } |
| 562 | |
| 563 | /* Destroy media endpoint. */ |
| 564 | if (pjsua_var.med_endpt) { |
| 565 | |
| 566 | /* Shutdown all codecs: */ |
| 567 | # if PJMEDIA_HAS_SPEEX_CODEC |
| 568 | pjmedia_codec_speex_deinit(); |
| 569 | # endif /* PJMEDIA_HAS_SPEEX_CODEC */ |
| 570 | |
| 571 | # if PJMEDIA_HAS_GSM_CODEC |
| 572 | pjmedia_codec_gsm_deinit(); |
| 573 | # endif /* PJMEDIA_HAS_GSM_CODEC */ |
| 574 | |
| 575 | # if PJMEDIA_HAS_G711_CODEC |
| 576 | pjmedia_codec_g711_deinit(); |
| 577 | # endif /* PJMEDIA_HAS_G711_CODEC */ |
| 578 | |
Benny Prijono | 7ffd775 | 2008-03-17 14:07:53 +0000 | [diff] [blame] | 579 | # if PJMEDIA_HAS_G722_CODEC |
| 580 | pjmedia_codec_g722_deinit(); |
| 581 | # endif /* PJMEDIA_HAS_G722_CODEC */ |
| 582 | |
Nanang Izzuddin | 6df1d53 | 2008-08-25 13:46:03 +0000 | [diff] [blame] | 583 | # if PJMEDIA_HAS_INTEL_IPP |
Nanang Izzuddin | 7dd3268 | 2008-08-19 11:23:33 +0000 | [diff] [blame] | 584 | pjmedia_codec_ipp_deinit(); |
Nanang Izzuddin | 6df1d53 | 2008-08-25 13:46:03 +0000 | [diff] [blame] | 585 | # endif /* PJMEDIA_HAS_INTEL_IPP */ |
Nanang Izzuddin | 7dd3268 | 2008-08-19 11:23:33 +0000 | [diff] [blame] | 586 | |
Nanang Izzuddin | 81db8c7 | 2009-02-05 10:59:14 +0000 | [diff] [blame^] | 587 | # if PJMEDIA_HAS_PASSTHROUGH_CODECS |
| 588 | pjmedia_codec_passthrough_deinit(); |
| 589 | # endif /* PJMEDIA_HAS_PASSTHROUGH_CODECS */ |
| 590 | |
Benny Prijono | eebe9af | 2006-06-13 22:57:13 +0000 | [diff] [blame] | 591 | # if PJMEDIA_HAS_L16_CODEC |
| 592 | pjmedia_codec_l16_deinit(); |
| 593 | # endif /* PJMEDIA_HAS_L16_CODEC */ |
| 594 | |
Benny Prijono | eebe9af | 2006-06-13 22:57:13 +0000 | [diff] [blame] | 595 | pjmedia_endpt_destroy(pjsua_var.med_endpt); |
| 596 | pjsua_var.med_endpt = NULL; |
Benny Prijono | eebe9af | 2006-06-13 22:57:13 +0000 | [diff] [blame] | 597 | |
Benny Prijono | ad2e0ca | 2007-04-29 12:31:51 +0000 | [diff] [blame] | 598 | /* Deinitialize sound subsystem */ |
Benny Prijono | a41d66e | 2007-10-01 12:17:23 +0000 | [diff] [blame] | 599 | // Not necessary, as pjmedia_snd_deinit() should have been called |
| 600 | // in pjmedia_endpt_destroy(). |
| 601 | //pjmedia_snd_deinit(); |
Benny Prijono | ad2e0ca | 2007-04-29 12:31:51 +0000 | [diff] [blame] | 602 | } |
Benny Prijono | af1bb1e | 2006-11-21 12:39:31 +0000 | [diff] [blame] | 603 | |
Benny Prijono | de47956 | 2007-03-15 10:23:55 +0000 | [diff] [blame] | 604 | /* Reset RTP port */ |
| 605 | next_rtp_port = 0; |
| 606 | |
Benny Prijono | eebe9af | 2006-06-13 22:57:13 +0000 | [diff] [blame] | 607 | return PJ_SUCCESS; |
| 608 | } |
| 609 | |
| 610 | |
Benny Prijono | c97608e | 2007-03-23 16:34:20 +0000 | [diff] [blame] | 611 | /* Create normal UDP media transports */ |
| 612 | static pj_status_t create_udp_media_transports(pjsua_transport_config *cfg) |
Benny Prijono | eebe9af | 2006-06-13 22:57:13 +0000 | [diff] [blame] | 613 | { |
Benny Prijono | eebe9af | 2006-06-13 22:57:13 +0000 | [diff] [blame] | 614 | unsigned i; |
Benny Prijono | 617c5bc | 2007-04-02 19:51:21 +0000 | [diff] [blame] | 615 | pjmedia_sock_info skinfo; |
Benny Prijono | eebe9af | 2006-06-13 22:57:13 +0000 | [diff] [blame] | 616 | pj_status_t status; |
| 617 | |
Benny Prijono | eebe9af | 2006-06-13 22:57:13 +0000 | [diff] [blame] | 618 | /* Create each media transport */ |
| 619 | for (i=0; i<pjsua_var.ua_cfg.max_calls; ++i) { |
| 620 | |
Benny Prijono | 617c5bc | 2007-04-02 19:51:21 +0000 | [diff] [blame] | 621 | status = create_rtp_rtcp_sock(cfg, &skinfo); |
Benny Prijono | eebe9af | 2006-06-13 22:57:13 +0000 | [diff] [blame] | 622 | if (status != PJ_SUCCESS) { |
| 623 | pjsua_perror(THIS_FILE, "Unable to create RTP/RTCP socket", |
| 624 | status); |
| 625 | goto on_error; |
| 626 | } |
Benny Prijono | d817965 | 2008-01-23 20:39:07 +0000 | [diff] [blame] | 627 | |
Benny Prijono | eebe9af | 2006-06-13 22:57:13 +0000 | [diff] [blame] | 628 | status = pjmedia_transport_udp_attach(pjsua_var.med_endpt, NULL, |
Benny Prijono | 617c5bc | 2007-04-02 19:51:21 +0000 | [diff] [blame] | 629 | &skinfo, 0, |
Benny Prijono | eebe9af | 2006-06-13 22:57:13 +0000 | [diff] [blame] | 630 | &pjsua_var.calls[i].med_tp); |
| 631 | if (status != PJ_SUCCESS) { |
| 632 | pjsua_perror(THIS_FILE, "Unable to create media transport", |
| 633 | status); |
| 634 | goto on_error; |
| 635 | } |
Benny Prijono | 00cae61 | 2006-07-31 15:19:36 +0000 | [diff] [blame] | 636 | |
Benny Prijono | d817965 | 2008-01-23 20:39:07 +0000 | [diff] [blame] | 637 | pjmedia_transport_simulate_lost(pjsua_var.calls[i].med_tp, |
| 638 | PJMEDIA_DIR_ENCODING, |
| 639 | pjsua_var.media_cfg.tx_drop_pct); |
Benny Prijono | 00cae61 | 2006-07-31 15:19:36 +0000 | [diff] [blame] | 640 | |
Benny Prijono | d817965 | 2008-01-23 20:39:07 +0000 | [diff] [blame] | 641 | pjmedia_transport_simulate_lost(pjsua_var.calls[i].med_tp, |
| 642 | PJMEDIA_DIR_DECODING, |
| 643 | pjsua_var.media_cfg.rx_drop_pct); |
Benny Prijono | 00cae61 | 2006-07-31 15:19:36 +0000 | [diff] [blame] | 644 | |
Benny Prijono | eebe9af | 2006-06-13 22:57:13 +0000 | [diff] [blame] | 645 | } |
| 646 | |
Benny Prijono | c97608e | 2007-03-23 16:34:20 +0000 | [diff] [blame] | 647 | return PJ_SUCCESS; |
| 648 | |
| 649 | on_error: |
| 650 | for (i=0; i<pjsua_var.ua_cfg.max_calls; ++i) { |
| 651 | if (pjsua_var.calls[i].med_tp != NULL) { |
| 652 | pjmedia_transport_close(pjsua_var.calls[i].med_tp); |
| 653 | pjsua_var.calls[i].med_tp = NULL; |
| 654 | } |
| 655 | } |
| 656 | |
| 657 | return status; |
| 658 | } |
| 659 | |
| 660 | |
Benny Prijono | 096c56c | 2007-09-15 08:30:16 +0000 | [diff] [blame] | 661 | /* This callback is called when ICE negotiation completes */ |
Benny Prijono | f76e139 | 2008-06-06 14:51:48 +0000 | [diff] [blame] | 662 | static void on_ice_complete(pjmedia_transport *tp, |
| 663 | pj_ice_strans_op op, |
| 664 | pj_status_t result) |
Benny Prijono | 096c56c | 2007-09-15 08:30:16 +0000 | [diff] [blame] | 665 | { |
Benny Prijono | f76e139 | 2008-06-06 14:51:48 +0000 | [diff] [blame] | 666 | unsigned id; |
Benny Prijono | 096c56c | 2007-09-15 08:30:16 +0000 | [diff] [blame] | 667 | pj_bool_t found = PJ_FALSE; |
| 668 | |
Benny Prijono | 096c56c | 2007-09-15 08:30:16 +0000 | [diff] [blame] | 669 | /* Find call which has this media transport */ |
| 670 | |
| 671 | PJSUA_LOCK(); |
| 672 | |
Benny Prijono | f76e139 | 2008-06-06 14:51:48 +0000 | [diff] [blame] | 673 | for (id=0; id<pjsua_var.ua_cfg.max_calls; ++id) { |
| 674 | if (pjsua_var.calls[id].med_tp == tp || |
| 675 | pjsua_var.calls[id].med_orig == tp) |
| 676 | { |
| 677 | found = PJ_TRUE; |
| 678 | break; |
Benny Prijono | 096c56c | 2007-09-15 08:30:16 +0000 | [diff] [blame] | 679 | } |
| 680 | } |
| 681 | |
| 682 | PJSUA_UNLOCK(); |
| 683 | |
Benny Prijono | f76e139 | 2008-06-06 14:51:48 +0000 | [diff] [blame] | 684 | if (!found) |
| 685 | return; |
| 686 | |
| 687 | switch (op) { |
| 688 | case PJ_ICE_STRANS_OP_INIT: |
Benny Prijono | 224b4e2 | 2008-06-19 14:10:28 +0000 | [diff] [blame] | 689 | pjsua_var.calls[id].med_tp_ready = result; |
Benny Prijono | f76e139 | 2008-06-06 14:51:48 +0000 | [diff] [blame] | 690 | break; |
| 691 | case PJ_ICE_STRANS_OP_NEGOTIATION: |
| 692 | if (result != PJ_SUCCESS) { |
| 693 | pjsua_var.calls[id].media_st = PJSUA_CALL_MEDIA_ERROR; |
| 694 | pjsua_var.calls[id].media_dir = PJMEDIA_DIR_NONE; |
| 695 | |
| 696 | if (pjsua_var.ua_cfg.cb.on_call_media_state) { |
| 697 | pjsua_var.ua_cfg.cb.on_call_media_state(id); |
| 698 | } |
| 699 | } |
| 700 | break; |
Benny Prijono | 096c56c | 2007-09-15 08:30:16 +0000 | [diff] [blame] | 701 | } |
| 702 | } |
| 703 | |
| 704 | |
Benny Prijono | f76e139 | 2008-06-06 14:51:48 +0000 | [diff] [blame] | 705 | /* Parse "HOST:PORT" format */ |
| 706 | static pj_status_t parse_host_port(const pj_str_t *host_port, |
| 707 | pj_str_t *host, pj_uint16_t *port) |
Benny Prijono | c97608e | 2007-03-23 16:34:20 +0000 | [diff] [blame] | 708 | { |
Benny Prijono | f76e139 | 2008-06-06 14:51:48 +0000 | [diff] [blame] | 709 | pj_str_t str_port; |
| 710 | |
| 711 | str_port.ptr = pj_strchr(host_port, ':'); |
| 712 | if (str_port.ptr != NULL) { |
| 713 | int iport; |
| 714 | |
| 715 | host->ptr = host_port->ptr; |
| 716 | host->slen = (str_port.ptr - host->ptr); |
| 717 | str_port.ptr++; |
| 718 | str_port.slen = host_port->slen - host->slen - 1; |
| 719 | iport = (int)pj_strtoul(&str_port); |
| 720 | if (iport < 1 || iport > 65535) |
| 721 | return PJ_EINVAL; |
| 722 | *port = (pj_uint16_t)iport; |
| 723 | } else { |
| 724 | *host = *host_port; |
| 725 | *port = 0; |
| 726 | } |
| 727 | |
| 728 | return PJ_SUCCESS; |
| 729 | } |
| 730 | |
| 731 | /* Create ICE media transports (when ice is enabled) */ |
| 732 | static pj_status_t create_ice_media_transports(void) |
| 733 | { |
| 734 | char stunip[PJ_INET6_ADDRSTRLEN]; |
| 735 | pj_ice_strans_cfg ice_cfg; |
Benny Prijono | c97608e | 2007-03-23 16:34:20 +0000 | [diff] [blame] | 736 | unsigned i; |
| 737 | pj_status_t status; |
| 738 | |
Benny Prijono | da9785b | 2007-04-02 20:43:06 +0000 | [diff] [blame] | 739 | /* Make sure STUN server resolution has completed */ |
| 740 | status = pjsua_resolve_stun_server(PJ_TRUE); |
| 741 | if (status != PJ_SUCCESS) { |
| 742 | pjsua_perror(THIS_FILE, "Error resolving STUN server", status); |
| 743 | return status; |
| 744 | } |
| 745 | |
Benny Prijono | f76e139 | 2008-06-06 14:51:48 +0000 | [diff] [blame] | 746 | /* Create ICE stream transport configuration */ |
| 747 | pj_ice_strans_cfg_default(&ice_cfg); |
| 748 | pj_stun_config_init(&ice_cfg.stun_cfg, &pjsua_var.cp.factory, 0, |
| 749 | pjsip_endpt_get_ioqueue(pjsua_var.endpt), |
| 750 | pjsip_endpt_get_timer_heap(pjsua_var.endpt)); |
| 751 | |
| 752 | ice_cfg.af = pj_AF_INET(); |
| 753 | ice_cfg.resolver = pjsua_var.resolver; |
| 754 | |
| 755 | /* Configure STUN settings */ |
| 756 | if (pj_sockaddr_has_addr(&pjsua_var.stun_srv)) { |
| 757 | pj_sockaddr_print(&pjsua_var.stun_srv, stunip, sizeof(stunip), 0); |
| 758 | ice_cfg.stun.server = pj_str(stunip); |
| 759 | ice_cfg.stun.port = pj_sockaddr_get_port(&pjsua_var.stun_srv); |
| 760 | } |
| 761 | ice_cfg.stun.no_host_cands = pjsua_var.media_cfg.ice_no_host_cands; |
| 762 | |
| 763 | /* Configure TURN settings */ |
| 764 | if (pjsua_var.media_cfg.enable_turn) { |
| 765 | status = parse_host_port(&pjsua_var.media_cfg.turn_server, |
| 766 | &ice_cfg.turn.server, |
| 767 | &ice_cfg.turn.port); |
| 768 | if (status != PJ_SUCCESS || ice_cfg.turn.server.slen == 0) { |
| 769 | PJ_LOG(1,(THIS_FILE, "Invalid TURN server setting")); |
| 770 | return PJ_EINVAL; |
| 771 | } |
| 772 | if (ice_cfg.turn.port == 0) |
| 773 | ice_cfg.turn.port = 3479; |
| 774 | ice_cfg.turn.conn_type = pjsua_var.media_cfg.turn_conn_type; |
| 775 | pj_memcpy(&ice_cfg.turn.auth_cred, |
| 776 | &pjsua_var.media_cfg.turn_auth_cred, |
| 777 | sizeof(ice_cfg.turn.auth_cred)); |
| 778 | } |
Benny Prijono | b681a2f | 2007-03-25 18:44:51 +0000 | [diff] [blame] | 779 | |
Benny Prijono | c97608e | 2007-03-23 16:34:20 +0000 | [diff] [blame] | 780 | /* Create each media transport */ |
| 781 | for (i=0; i<pjsua_var.ua_cfg.max_calls; ++i) { |
Benny Prijono | 096c56c | 2007-09-15 08:30:16 +0000 | [diff] [blame] | 782 | pjmedia_ice_cb ice_cb; |
Benny Prijono | 38fb3ea | 2008-01-02 08:27:03 +0000 | [diff] [blame] | 783 | char name[32]; |
Benny Prijono | f76e139 | 2008-06-06 14:51:48 +0000 | [diff] [blame] | 784 | unsigned comp_cnt; |
Benny Prijono | c97608e | 2007-03-23 16:34:20 +0000 | [diff] [blame] | 785 | |
Benny Prijono | 096c56c | 2007-09-15 08:30:16 +0000 | [diff] [blame] | 786 | pj_bzero(&ice_cb, sizeof(pjmedia_ice_cb)); |
| 787 | ice_cb.on_ice_complete = &on_ice_complete; |
Benny Prijono | 38fb3ea | 2008-01-02 08:27:03 +0000 | [diff] [blame] | 788 | pj_ansi_snprintf(name, sizeof(name), "icetp%02d", i); |
Benny Prijono | 224b4e2 | 2008-06-19 14:10:28 +0000 | [diff] [blame] | 789 | pjsua_var.calls[i].med_tp_ready = PJ_EPENDING; |
Benny Prijono | f76e139 | 2008-06-06 14:51:48 +0000 | [diff] [blame] | 790 | |
| 791 | comp_cnt = 1; |
Benny Prijono | 551af42 | 2008-08-07 09:55:52 +0000 | [diff] [blame] | 792 | if (PJMEDIA_ADVERTISE_RTCP && !pjsua_var.media_cfg.ice_no_rtcp) |
Benny Prijono | f76e139 | 2008-06-06 14:51:48 +0000 | [diff] [blame] | 793 | ++comp_cnt; |
| 794 | |
| 795 | status = pjmedia_ice_create(pjsua_var.med_endpt, name, comp_cnt, |
| 796 | &ice_cfg, &ice_cb, |
Benny Prijono | c97608e | 2007-03-23 16:34:20 +0000 | [diff] [blame] | 797 | &pjsua_var.calls[i].med_tp); |
| 798 | if (status != PJ_SUCCESS) { |
| 799 | pjsua_perror(THIS_FILE, "Unable to create ICE media transport", |
| 800 | status); |
| 801 | goto on_error; |
| 802 | } |
| 803 | |
Benny Prijono | f76e139 | 2008-06-06 14:51:48 +0000 | [diff] [blame] | 804 | /* Wait until transport is initialized, or time out */ |
| 805 | PJSUA_UNLOCK(); |
Benny Prijono | 224b4e2 | 2008-06-19 14:10:28 +0000 | [diff] [blame] | 806 | while (pjsua_var.calls[i].med_tp_ready == PJ_EPENDING) { |
Benny Prijono | f76e139 | 2008-06-06 14:51:48 +0000 | [diff] [blame] | 807 | pjsua_handle_events(100); |
| 808 | } |
| 809 | PJSUA_LOCK(); |
Benny Prijono | 224b4e2 | 2008-06-19 14:10:28 +0000 | [diff] [blame] | 810 | if (pjsua_var.calls[i].med_tp_ready != PJ_SUCCESS) { |
Benny Prijono | f76e139 | 2008-06-06 14:51:48 +0000 | [diff] [blame] | 811 | pjsua_perror(THIS_FILE, "Error initializing ICE media transport", |
Benny Prijono | 224b4e2 | 2008-06-19 14:10:28 +0000 | [diff] [blame] | 812 | pjsua_var.calls[i].med_tp_ready); |
| 813 | status = pjsua_var.calls[i].med_tp_ready; |
Benny Prijono | f76e139 | 2008-06-06 14:51:48 +0000 | [diff] [blame] | 814 | goto on_error; |
| 815 | } |
| 816 | |
Benny Prijono | d817965 | 2008-01-23 20:39:07 +0000 | [diff] [blame] | 817 | pjmedia_transport_simulate_lost(pjsua_var.calls[i].med_tp, |
| 818 | PJMEDIA_DIR_ENCODING, |
| 819 | pjsua_var.media_cfg.tx_drop_pct); |
Benny Prijono | 11da9bc | 2007-09-15 08:55:00 +0000 | [diff] [blame] | 820 | |
Benny Prijono | d817965 | 2008-01-23 20:39:07 +0000 | [diff] [blame] | 821 | pjmedia_transport_simulate_lost(pjsua_var.calls[i].med_tp, |
| 822 | PJMEDIA_DIR_DECODING, |
| 823 | pjsua_var.media_cfg.rx_drop_pct); |
Benny Prijono | c97608e | 2007-03-23 16:34:20 +0000 | [diff] [blame] | 824 | } |
Benny Prijono | eebe9af | 2006-06-13 22:57:13 +0000 | [diff] [blame] | 825 | |
| 826 | return PJ_SUCCESS; |
| 827 | |
| 828 | on_error: |
| 829 | for (i=0; i<pjsua_var.ua_cfg.max_calls; ++i) { |
| 830 | if (pjsua_var.calls[i].med_tp != NULL) { |
Benny Prijono | c97608e | 2007-03-23 16:34:20 +0000 | [diff] [blame] | 831 | pjmedia_transport_close(pjsua_var.calls[i].med_tp); |
Benny Prijono | eebe9af | 2006-06-13 22:57:13 +0000 | [diff] [blame] | 832 | pjsua_var.calls[i].med_tp = NULL; |
| 833 | } |
| 834 | } |
| 835 | |
Benny Prijono | c97608e | 2007-03-23 16:34:20 +0000 | [diff] [blame] | 836 | return status; |
| 837 | } |
| 838 | |
| 839 | |
| 840 | /* |
| 841 | * Create UDP media transports for all the calls. This function creates |
| 842 | * one UDP media transport for each call. |
| 843 | */ |
Benny Prijono | 1f61a8f | 2007-08-16 10:11:44 +0000 | [diff] [blame] | 844 | PJ_DEF(pj_status_t) pjsua_media_transports_create( |
| 845 | const pjsua_transport_config *app_cfg) |
Benny Prijono | c97608e | 2007-03-23 16:34:20 +0000 | [diff] [blame] | 846 | { |
| 847 | pjsua_transport_config cfg; |
| 848 | unsigned i; |
| 849 | pj_status_t status; |
| 850 | |
| 851 | |
| 852 | /* Make sure pjsua_init() has been called */ |
| 853 | PJ_ASSERT_RETURN(pjsua_var.ua_cfg.max_calls>0, PJ_EINVALIDOP); |
| 854 | |
| 855 | PJSUA_LOCK(); |
| 856 | |
| 857 | /* Delete existing media transports */ |
| 858 | for (i=0; i<pjsua_var.ua_cfg.max_calls; ++i) { |
Benny Prijono | 40860c3 | 2008-09-04 13:55:33 +0000 | [diff] [blame] | 859 | if (pjsua_var.calls[i].med_tp != NULL && |
| 860 | pjsua_var.calls[i].med_tp_auto_del) |
| 861 | { |
Benny Prijono | c97608e | 2007-03-23 16:34:20 +0000 | [diff] [blame] | 862 | pjmedia_transport_close(pjsua_var.calls[i].med_tp); |
| 863 | pjsua_var.calls[i].med_tp = NULL; |
Nanang Izzuddin | d704a8b | 2008-09-23 16:34:07 +0000 | [diff] [blame] | 864 | pjsua_var.calls[i].med_orig = NULL; |
Benny Prijono | c97608e | 2007-03-23 16:34:20 +0000 | [diff] [blame] | 865 | } |
| 866 | } |
| 867 | |
| 868 | /* Copy config */ |
| 869 | pjsua_transport_config_dup(pjsua_var.pool, &cfg, app_cfg); |
| 870 | |
Benny Prijono | 40860c3 | 2008-09-04 13:55:33 +0000 | [diff] [blame] | 871 | /* Create the transports */ |
Benny Prijono | c97608e | 2007-03-23 16:34:20 +0000 | [diff] [blame] | 872 | if (pjsua_var.media_cfg.enable_ice) { |
Benny Prijono | f76e139 | 2008-06-06 14:51:48 +0000 | [diff] [blame] | 873 | status = create_ice_media_transports(); |
Benny Prijono | c97608e | 2007-03-23 16:34:20 +0000 | [diff] [blame] | 874 | } else { |
| 875 | status = create_udp_media_transports(&cfg); |
| 876 | } |
| 877 | |
Benny Prijono | 40860c3 | 2008-09-04 13:55:33 +0000 | [diff] [blame] | 878 | /* Set media transport auto_delete to True */ |
| 879 | for (i=0; i<pjsua_var.ua_cfg.max_calls; ++i) { |
| 880 | pjsua_var.calls[i].med_tp_auto_del = PJ_TRUE; |
| 881 | } |
Benny Prijono | c97608e | 2007-03-23 16:34:20 +0000 | [diff] [blame] | 882 | |
Benny Prijono | eebe9af | 2006-06-13 22:57:13 +0000 | [diff] [blame] | 883 | PJSUA_UNLOCK(); |
| 884 | |
| 885 | return status; |
| 886 | } |
| 887 | |
Benny Prijono | 40860c3 | 2008-09-04 13:55:33 +0000 | [diff] [blame] | 888 | /* |
| 889 | * Attach application's created media transports. |
| 890 | */ |
| 891 | PJ_DEF(pj_status_t) pjsua_media_transports_attach(pjsua_media_transport tp[], |
| 892 | unsigned count, |
| 893 | pj_bool_t auto_delete) |
| 894 | { |
| 895 | unsigned i; |
| 896 | |
| 897 | PJ_ASSERT_RETURN(tp && count==pjsua_var.ua_cfg.max_calls, PJ_EINVAL); |
| 898 | |
| 899 | /* Assign the media transports */ |
| 900 | for (i=0; i<pjsua_var.ua_cfg.max_calls; ++i) { |
| 901 | if (pjsua_var.calls[i].med_tp != NULL && |
| 902 | pjsua_var.calls[i].med_tp_auto_del) |
| 903 | { |
| 904 | pjmedia_transport_close(pjsua_var.calls[i].med_tp); |
| 905 | } |
| 906 | |
| 907 | pjsua_var.calls[i].med_tp = tp[i].transport; |
| 908 | pjsua_var.calls[i].med_tp_auto_del = auto_delete; |
| 909 | } |
| 910 | |
| 911 | return PJ_SUCCESS; |
| 912 | } |
| 913 | |
Benny Prijono | eebe9af | 2006-06-13 22:57:13 +0000 | [diff] [blame] | 914 | |
Benny Prijono | c97608e | 2007-03-23 16:34:20 +0000 | [diff] [blame] | 915 | pj_status_t pjsua_media_channel_init(pjsua_call_id call_id, |
Benny Prijono | d817965 | 2008-01-23 20:39:07 +0000 | [diff] [blame] | 916 | pjsip_role_e role, |
Benny Prijono | 25b2ea1 | 2008-01-24 19:20:54 +0000 | [diff] [blame] | 917 | int security_level, |
Benny Prijono | 224b4e2 | 2008-06-19 14:10:28 +0000 | [diff] [blame] | 918 | pj_pool_t *tmp_pool, |
| 919 | const pjmedia_sdp_session *rem_sdp, |
Benny Prijono | 25b2ea1 | 2008-01-24 19:20:54 +0000 | [diff] [blame] | 920 | int *sip_err_code) |
Benny Prijono | c97608e | 2007-03-23 16:34:20 +0000 | [diff] [blame] | 921 | { |
| 922 | pjsua_call *call = &pjsua_var.calls[call_id]; |
Benny Prijono | 224b4e2 | 2008-06-19 14:10:28 +0000 | [diff] [blame] | 923 | pj_status_t status; |
Benny Prijono | c97608e | 2007-03-23 16:34:20 +0000 | [diff] [blame] | 924 | |
Benny Prijono | d817965 | 2008-01-23 20:39:07 +0000 | [diff] [blame] | 925 | #if defined(PJMEDIA_HAS_SRTP) && (PJMEDIA_HAS_SRTP != 0) |
| 926 | pjsua_acc *acc = &pjsua_var.acc[call->acc_id]; |
| 927 | pjmedia_srtp_setting srtp_opt; |
Benny Prijono | a310bd2 | 2008-06-27 21:19:44 +0000 | [diff] [blame] | 928 | pjmedia_transport *srtp = NULL; |
Benny Prijono | d817965 | 2008-01-23 20:39:07 +0000 | [diff] [blame] | 929 | #endif |
Benny Prijono | c97608e | 2007-03-23 16:34:20 +0000 | [diff] [blame] | 930 | |
Benny Prijono | d817965 | 2008-01-23 20:39:07 +0000 | [diff] [blame] | 931 | PJ_UNUSED_ARG(role); |
Benny Prijono | c97608e | 2007-03-23 16:34:20 +0000 | [diff] [blame] | 932 | |
Benny Prijono | d817965 | 2008-01-23 20:39:07 +0000 | [diff] [blame] | 933 | /* Return error if media transport has not been created yet |
| 934 | * (e.g. application is starting) |
| 935 | */ |
| 936 | if (call->med_tp == NULL) { |
Benny Prijono | 0378905 | 2008-09-16 14:30:50 +0000 | [diff] [blame] | 937 | if (sip_err_code) |
| 938 | *sip_err_code = PJSIP_SC_INTERNAL_SERVER_ERROR; |
Benny Prijono | d817965 | 2008-01-23 20:39:07 +0000 | [diff] [blame] | 939 | return PJ_EBUSY; |
Benny Prijono | c97608e | 2007-03-23 16:34:20 +0000 | [diff] [blame] | 940 | } |
| 941 | |
Benny Prijono | d817965 | 2008-01-23 20:39:07 +0000 | [diff] [blame] | 942 | #if defined(PJMEDIA_HAS_SRTP) && (PJMEDIA_HAS_SRTP != 0) |
Benny Prijono | 53a7c70 | 2008-04-14 02:57:29 +0000 | [diff] [blame] | 943 | /* This function may be called when SRTP transport already exists |
| 944 | * (e.g: in re-invite, update), don't need to destroy/re-create. |
| 945 | */ |
| 946 | if (!call->med_orig || call->med_tp == call->med_orig) { |
| 947 | |
| 948 | /* Check if SRTP requires secure signaling */ |
| 949 | if (acc->cfg.use_srtp != PJMEDIA_SRTP_DISABLED) { |
| 950 | if (security_level < acc->cfg.srtp_secure_signaling) { |
| 951 | if (sip_err_code) |
| 952 | *sip_err_code = PJSIP_SC_NOT_ACCEPTABLE; |
| 953 | return PJSIP_ESESSIONINSECURE; |
| 954 | } |
Benny Prijono | d817965 | 2008-01-23 20:39:07 +0000 | [diff] [blame] | 955 | } |
Benny Prijono | d817965 | 2008-01-23 20:39:07 +0000 | [diff] [blame] | 956 | |
Benny Prijono | 53a7c70 | 2008-04-14 02:57:29 +0000 | [diff] [blame] | 957 | /* Always create SRTP adapter */ |
| 958 | pjmedia_srtp_setting_default(&srtp_opt); |
| 959 | srtp_opt.close_member_tp = PJ_FALSE; |
Nanang Izzuddin | 4375f90 | 2008-06-26 19:12:09 +0000 | [diff] [blame] | 960 | /* If media session has been ever established, let's use remote's |
| 961 | * preference in SRTP usage policy, especially when it is stricter. |
| 962 | */ |
| 963 | if (call->rem_srtp_use > acc->cfg.use_srtp) |
| 964 | srtp_opt.use = call->rem_srtp_use; |
| 965 | else |
| 966 | srtp_opt.use = acc->cfg.use_srtp; |
| 967 | |
Benny Prijono | 53a7c70 | 2008-04-14 02:57:29 +0000 | [diff] [blame] | 968 | status = pjmedia_transport_srtp_create(pjsua_var.med_endpt, |
| 969 | call->med_tp, |
| 970 | &srtp_opt, &srtp); |
| 971 | if (status != PJ_SUCCESS) { |
| 972 | if (sip_err_code) |
| 973 | *sip_err_code = PJSIP_SC_INTERNAL_SERVER_ERROR; |
| 974 | return status; |
| 975 | } |
Benny Prijono | d817965 | 2008-01-23 20:39:07 +0000 | [diff] [blame] | 976 | |
Benny Prijono | 53a7c70 | 2008-04-14 02:57:29 +0000 | [diff] [blame] | 977 | /* Set SRTP as current media transport */ |
| 978 | call->med_orig = call->med_tp; |
| 979 | call->med_tp = srtp; |
| 980 | } |
Benny Prijono | d817965 | 2008-01-23 20:39:07 +0000 | [diff] [blame] | 981 | #else |
| 982 | call->med_orig = call->med_tp; |
| 983 | PJ_UNUSED_ARG(security_level); |
| 984 | #endif |
| 985 | |
Benny Prijono | a310bd2 | 2008-06-27 21:19:44 +0000 | [diff] [blame] | 986 | /* Find out which media line in SDP that we support. If we are offerer, |
| 987 | * audio will be at index 0 in SDP. |
| 988 | */ |
| 989 | if (rem_sdp == 0) { |
| 990 | call->audio_idx = 0; |
| 991 | } |
| 992 | /* Otherwise find out the candidate audio media line in SDP */ |
| 993 | else { |
| 994 | unsigned i; |
| 995 | pj_bool_t srtp_active; |
| 996 | |
| 997 | #if defined(PJMEDIA_HAS_SRTP) && (PJMEDIA_HAS_SRTP != 0) |
| 998 | srtp_active = acc->cfg.use_srtp && srtp != NULL; |
| 999 | #else |
| 1000 | srtp_active = PJ_FALSE; |
| 1001 | #endif |
| 1002 | |
| 1003 | /* Media count must have been checked */ |
| 1004 | pj_assert(rem_sdp->media_count != 0); |
| 1005 | |
| 1006 | for (i=0; i<rem_sdp->media_count; ++i) { |
| 1007 | const pjmedia_sdp_media *m = rem_sdp->media[i]; |
| 1008 | |
| 1009 | /* Skip if media is not audio */ |
| 1010 | if (pj_stricmp2(&m->desc.media, "audio") != 0) |
| 1011 | continue; |
| 1012 | |
| 1013 | /* Skip if media is disabled */ |
| 1014 | if (m->desc.port == 0) |
| 1015 | continue; |
| 1016 | |
| 1017 | /* Skip if transport is not supported */ |
| 1018 | if (pj_stricmp2(&m->desc.transport, "RTP/AVP") != 0 && |
| 1019 | pj_stricmp2(&m->desc.transport, "RTP/SAVP") != 0) |
| 1020 | { |
| 1021 | continue; |
| 1022 | } |
| 1023 | |
| 1024 | if (call->audio_idx == -1) { |
| 1025 | call->audio_idx = i; |
| 1026 | } else { |
| 1027 | /* We've found multiple candidates. This could happen |
| 1028 | * e.g. when remote is offering both RTP/AVP and RTP/AVP, |
| 1029 | * or when remote for some reason offers two audio. |
| 1030 | */ |
| 1031 | |
| 1032 | if (srtp_active && |
| 1033 | pj_stricmp2(&m->desc.transport, "RTP/SAVP")==0) |
| 1034 | { |
| 1035 | /* Prefer RTP/SAVP when our media transport is SRTP */ |
| 1036 | call->audio_idx = i; |
| 1037 | } else if (!srtp_active && |
| 1038 | pj_stricmp2(&m->desc.transport, "RTP/AVP")==0) |
| 1039 | { |
| 1040 | /* Prefer RTP/AVP when our media transport is NOT SRTP */ |
| 1041 | call->audio_idx = i; |
| 1042 | } |
| 1043 | } |
| 1044 | } |
| 1045 | } |
| 1046 | |
| 1047 | /* Reject offer if we couldn't find a good m=audio line in offer */ |
| 1048 | if (call->audio_idx < 0) { |
Benny Prijono | ab8dba9 | 2008-06-27 21:59:15 +0000 | [diff] [blame] | 1049 | if (sip_err_code) *sip_err_code = PJSIP_SC_NOT_ACCEPTABLE_HERE; |
Benny Prijono | a310bd2 | 2008-06-27 21:19:44 +0000 | [diff] [blame] | 1050 | pjsua_media_channel_deinit(call_id); |
Benny Prijono | ab8dba9 | 2008-06-27 21:59:15 +0000 | [diff] [blame] | 1051 | return PJSIP_ERRNO_FROM_SIP_STATUS(PJSIP_SC_NOT_ACCEPTABLE_HERE); |
Benny Prijono | a310bd2 | 2008-06-27 21:19:44 +0000 | [diff] [blame] | 1052 | } |
| 1053 | |
| 1054 | PJ_LOG(4,(THIS_FILE, "Media index %d selected for call %d", |
| 1055 | call->audio_idx, call->index)); |
| 1056 | |
Benny Prijono | 224b4e2 | 2008-06-19 14:10:28 +0000 | [diff] [blame] | 1057 | /* Create the media transport */ |
| 1058 | status = pjmedia_transport_media_create(call->med_tp, tmp_pool, 0, |
Benny Prijono | a310bd2 | 2008-06-27 21:19:44 +0000 | [diff] [blame] | 1059 | rem_sdp, call->audio_idx); |
Benny Prijono | 224b4e2 | 2008-06-19 14:10:28 +0000 | [diff] [blame] | 1060 | if (status != PJ_SUCCESS) { |
| 1061 | if (sip_err_code) *sip_err_code = PJSIP_SC_NOT_ACCEPTABLE; |
| 1062 | pjsua_media_channel_deinit(call_id); |
| 1063 | return status; |
| 1064 | } |
| 1065 | |
| 1066 | call->med_tp_st = PJSUA_MED_TP_INIT; |
Benny Prijono | c97608e | 2007-03-23 16:34:20 +0000 | [diff] [blame] | 1067 | return PJ_SUCCESS; |
| 1068 | } |
| 1069 | |
| 1070 | pj_status_t pjsua_media_channel_create_sdp(pjsua_call_id call_id, |
| 1071 | pj_pool_t *pool, |
Benny Prijono | d817965 | 2008-01-23 20:39:07 +0000 | [diff] [blame] | 1072 | const pjmedia_sdp_session *rem_sdp, |
Benny Prijono | 25b2ea1 | 2008-01-24 19:20:54 +0000 | [diff] [blame] | 1073 | pjmedia_sdp_session **p_sdp, |
| 1074 | int *sip_status_code) |
Benny Prijono | c97608e | 2007-03-23 16:34:20 +0000 | [diff] [blame] | 1075 | { |
Benny Prijono | a310bd2 | 2008-06-27 21:19:44 +0000 | [diff] [blame] | 1076 | enum { MAX_MEDIA = 1 }; |
Benny Prijono | c97608e | 2007-03-23 16:34:20 +0000 | [diff] [blame] | 1077 | pjmedia_sdp_session *sdp; |
Benny Prijono | e1a5a85 | 2008-03-11 21:38:05 +0000 | [diff] [blame] | 1078 | pjmedia_transport_info tpinfo; |
Benny Prijono | c97608e | 2007-03-23 16:34:20 +0000 | [diff] [blame] | 1079 | pjsua_call *call = &pjsua_var.calls[call_id]; |
| 1080 | pj_status_t status; |
| 1081 | |
Benny Prijono | 55e8235 | 2007-05-10 20:49:08 +0000 | [diff] [blame] | 1082 | /* Return error if media transport has not been created yet |
| 1083 | * (e.g. application is starting) |
| 1084 | */ |
| 1085 | if (call->med_tp == NULL) { |
| 1086 | return PJ_EBUSY; |
| 1087 | } |
| 1088 | |
Benny Prijono | a310bd2 | 2008-06-27 21:19:44 +0000 | [diff] [blame] | 1089 | /* Media index must have been determined before */ |
| 1090 | pj_assert(call->audio_idx != -1); |
| 1091 | |
Benny Prijono | 224b4e2 | 2008-06-19 14:10:28 +0000 | [diff] [blame] | 1092 | /* Create media if it's not created. This could happen when call is |
| 1093 | * currently on-hold |
| 1094 | */ |
| 1095 | if (call->med_tp_st == PJSUA_MED_TP_IDLE) { |
| 1096 | pjsip_role_e role; |
| 1097 | role = (rem_sdp ? PJSIP_ROLE_UAS : PJSIP_ROLE_UAC); |
| 1098 | status = pjsua_media_channel_init(call_id, role, call->secure_level, |
| 1099 | pool, rem_sdp, sip_status_code); |
| 1100 | if (status != PJ_SUCCESS) |
| 1101 | return status; |
| 1102 | } |
| 1103 | |
Benny Prijono | 617c5bc | 2007-04-02 19:51:21 +0000 | [diff] [blame] | 1104 | /* Get media socket info */ |
Benny Prijono | 734fc2d | 2008-03-17 16:05:35 +0000 | [diff] [blame] | 1105 | pjmedia_transport_info_init(&tpinfo); |
Benny Prijono | e1a5a85 | 2008-03-11 21:38:05 +0000 | [diff] [blame] | 1106 | pjmedia_transport_get_info(call->med_tp, &tpinfo); |
Benny Prijono | 617c5bc | 2007-04-02 19:51:21 +0000 | [diff] [blame] | 1107 | |
| 1108 | /* Create SDP */ |
Benny Prijono | d817965 | 2008-01-23 20:39:07 +0000 | [diff] [blame] | 1109 | status = pjmedia_endpt_create_sdp(pjsua_var.med_endpt, pool, MAX_MEDIA, |
Benny Prijono | e1a5a85 | 2008-03-11 21:38:05 +0000 | [diff] [blame] | 1110 | &tpinfo.sock_info, &sdp); |
Benny Prijono | 25b2ea1 | 2008-01-24 19:20:54 +0000 | [diff] [blame] | 1111 | if (status != PJ_SUCCESS) { |
| 1112 | if (sip_status_code) *sip_status_code = 500; |
Benny Prijono | 224b4e2 | 2008-06-19 14:10:28 +0000 | [diff] [blame] | 1113 | return status; |
Benny Prijono | 25b2ea1 | 2008-01-24 19:20:54 +0000 | [diff] [blame] | 1114 | } |
Benny Prijono | c97608e | 2007-03-23 16:34:20 +0000 | [diff] [blame] | 1115 | |
Benny Prijono | a310bd2 | 2008-06-27 21:19:44 +0000 | [diff] [blame] | 1116 | /* If we're answering and the selected media is not the first media |
| 1117 | * in SDP, then fill in the unselected media with with zero port. |
| 1118 | * Otherwise we'll crash in transport_encode_sdp() because the media |
| 1119 | * lines are not aligned between offer and answer. |
| 1120 | */ |
| 1121 | if (rem_sdp && call->audio_idx != 0) { |
| 1122 | unsigned i; |
| 1123 | |
| 1124 | for (i=0; i<rem_sdp->media_count; ++i) { |
| 1125 | const pjmedia_sdp_media *rem_m = rem_sdp->media[i]; |
| 1126 | pjmedia_sdp_media *m; |
| 1127 | const pjmedia_sdp_attr *a; |
| 1128 | |
| 1129 | if ((int)i == call->audio_idx) |
| 1130 | continue; |
| 1131 | |
| 1132 | m = PJ_POOL_ZALLOC_T(pool, pjmedia_sdp_media); |
| 1133 | pj_strdup(pool, &m->desc.media, &rem_m->desc.media); |
| 1134 | pj_strdup(pool, &m->desc.transport, &rem_m->desc.transport); |
| 1135 | m->desc.port = 0; |
| 1136 | |
| 1137 | /* Add one format, copy from the offer. And copy the corresponding |
| 1138 | * rtpmap and fmtp attributes too. |
| 1139 | */ |
| 1140 | m->desc.fmt_count = 1; |
| 1141 | pj_strdup(pool, &m->desc.fmt[0], &rem_m->desc.fmt[0]); |
| 1142 | if ((a=pjmedia_sdp_attr_find2(rem_m->attr_count, rem_m->attr, |
| 1143 | "rtpmap", &m->desc.fmt[0])) != NULL) |
| 1144 | { |
| 1145 | m->attr[m->attr_count++] = pjmedia_sdp_attr_clone(pool, a); |
| 1146 | } |
| 1147 | if ((a=pjmedia_sdp_attr_find2(rem_m->attr_count, rem_m->attr, |
| 1148 | "fmtp", &m->desc.fmt[0])) != NULL) |
| 1149 | { |
| 1150 | m->attr[m->attr_count++] = pjmedia_sdp_attr_clone(pool, a); |
| 1151 | } |
| 1152 | |
| 1153 | if (i==sdp->media_count) |
| 1154 | sdp->media[sdp->media_count++] = m; |
| 1155 | else { |
| 1156 | pj_array_insert(sdp->media, sizeof(sdp->media[0]), |
| 1157 | sdp->media_count, i, &m); |
| 1158 | ++sdp->media_count; |
| 1159 | } |
| 1160 | } |
| 1161 | } |
| 1162 | |
Benny Prijono | 6ba8c54 | 2007-10-16 01:34:14 +0000 | [diff] [blame] | 1163 | /* Add NAT info in the SDP */ |
| 1164 | if (pjsua_var.ua_cfg.nat_type_in_sdp) { |
| 1165 | pjmedia_sdp_attr *a; |
| 1166 | pj_str_t value; |
| 1167 | char nat_info[80]; |
| 1168 | |
| 1169 | value.ptr = nat_info; |
| 1170 | if (pjsua_var.ua_cfg.nat_type_in_sdp == 1) { |
| 1171 | value.slen = pj_ansi_snprintf(nat_info, sizeof(nat_info), |
| 1172 | "%d", pjsua_var.nat_type); |
| 1173 | } else { |
| 1174 | const char *type_name = pj_stun_get_nat_name(pjsua_var.nat_type); |
| 1175 | value.slen = pj_ansi_snprintf(nat_info, sizeof(nat_info), |
| 1176 | "%d %s", |
| 1177 | pjsua_var.nat_type, |
| 1178 | type_name); |
| 1179 | } |
| 1180 | |
| 1181 | a = pjmedia_sdp_attr_create(pool, "X-nat", &value); |
| 1182 | |
| 1183 | pjmedia_sdp_attr_add(&sdp->attr_count, sdp->attr, a); |
| 1184 | |
| 1185 | } |
| 1186 | |
Benny Prijono | d817965 | 2008-01-23 20:39:07 +0000 | [diff] [blame] | 1187 | /* Give the SDP to media transport */ |
Benny Prijono | 224b4e2 | 2008-06-19 14:10:28 +0000 | [diff] [blame] | 1188 | status = pjmedia_transport_encode_sdp(call->med_tp, pool, sdp, rem_sdp, |
Benny Prijono | a310bd2 | 2008-06-27 21:19:44 +0000 | [diff] [blame] | 1189 | call->audio_idx); |
Benny Prijono | 25b2ea1 | 2008-01-24 19:20:54 +0000 | [diff] [blame] | 1190 | if (status != PJ_SUCCESS) { |
| 1191 | if (sip_status_code) *sip_status_code = PJSIP_SC_NOT_ACCEPTABLE; |
Benny Prijono | 224b4e2 | 2008-06-19 14:10:28 +0000 | [diff] [blame] | 1192 | return status; |
Benny Prijono | 25b2ea1 | 2008-01-24 19:20:54 +0000 | [diff] [blame] | 1193 | } |
Benny Prijono | c97608e | 2007-03-23 16:34:20 +0000 | [diff] [blame] | 1194 | |
| 1195 | *p_sdp = sdp; |
| 1196 | return PJ_SUCCESS; |
Benny Prijono | c97608e | 2007-03-23 16:34:20 +0000 | [diff] [blame] | 1197 | } |
| 1198 | |
| 1199 | |
| 1200 | static void stop_media_session(pjsua_call_id call_id) |
| 1201 | { |
| 1202 | pjsua_call *call = &pjsua_var.calls[call_id]; |
| 1203 | |
| 1204 | if (call->conf_slot != PJSUA_INVALID_ID) { |
Nanang Izzuddin | fd461eb | 2008-06-09 09:35:59 +0000 | [diff] [blame] | 1205 | if (pjsua_var.mconf) { |
Nanang Izzuddin | 148fd39 | 2008-06-16 09:52:50 +0000 | [diff] [blame] | 1206 | pjsua_conf_remove_port(call->conf_slot); |
Nanang Izzuddin | fd461eb | 2008-06-09 09:35:59 +0000 | [diff] [blame] | 1207 | } |
Benny Prijono | c97608e | 2007-03-23 16:34:20 +0000 | [diff] [blame] | 1208 | call->conf_slot = PJSUA_INVALID_ID; |
| 1209 | } |
| 1210 | |
| 1211 | if (call->session) { |
Nanang Izzuddin | a815ceb | 2008-08-26 16:51:28 +0000 | [diff] [blame] | 1212 | pjmedia_rtcp_stat stat; |
| 1213 | |
Nanang Izzuddin | 437d77c | 2008-08-26 18:04:15 +0000 | [diff] [blame] | 1214 | if (pjmedia_session_get_stream_stat(call->session, 0, &stat) |
| 1215 | == PJ_SUCCESS) |
Nanang Izzuddin | a815ceb | 2008-08-26 16:51:28 +0000 | [diff] [blame] | 1216 | { |
| 1217 | /* Save RTP timestamp & sequence, so when media session is |
| 1218 | * restarted, those values will be restored as the initial |
| 1219 | * RTP timestamp & sequence of the new media session. So in |
| 1220 | * the same call session, RTP timestamp and sequence are |
| 1221 | * guaranteed to be contigue. |
| 1222 | */ |
| 1223 | call->rtp_tx_seq_ts_set = 1 | (1 << 1); |
| 1224 | call->rtp_tx_seq = stat.rtp_tx_last_seq; |
| 1225 | call->rtp_tx_ts = stat.rtp_tx_last_ts; |
| 1226 | } |
| 1227 | |
Benny Prijono | fc13bf6 | 2008-02-20 08:56:15 +0000 | [diff] [blame] | 1228 | if (pjsua_var.ua_cfg.cb.on_stream_destroyed) { |
| 1229 | pjsua_var.ua_cfg.cb.on_stream_destroyed(call_id, call->session, 0); |
| 1230 | } |
| 1231 | |
Benny Prijono | c97608e | 2007-03-23 16:34:20 +0000 | [diff] [blame] | 1232 | pjmedia_session_destroy(call->session); |
| 1233 | call->session = NULL; |
| 1234 | |
| 1235 | PJ_LOG(4,(THIS_FILE, "Media session for call %d is destroyed", |
| 1236 | call_id)); |
| 1237 | |
| 1238 | } |
| 1239 | |
| 1240 | call->media_st = PJSUA_CALL_MEDIA_NONE; |
| 1241 | } |
| 1242 | |
| 1243 | pj_status_t pjsua_media_channel_deinit(pjsua_call_id call_id) |
| 1244 | { |
| 1245 | pjsua_call *call = &pjsua_var.calls[call_id]; |
| 1246 | |
| 1247 | stop_media_session(call_id); |
| 1248 | |
Benny Prijono | 224b4e2 | 2008-06-19 14:10:28 +0000 | [diff] [blame] | 1249 | if (call->med_tp_st != PJSUA_MED_TP_IDLE) { |
| 1250 | pjmedia_transport_media_stop(call->med_tp); |
| 1251 | call->med_tp_st = PJSUA_MED_TP_IDLE; |
| 1252 | } |
Benny Prijono | c97608e | 2007-03-23 16:34:20 +0000 | [diff] [blame] | 1253 | |
Benny Prijono | 311b63f | 2008-07-14 11:31:40 +0000 | [diff] [blame] | 1254 | if (call->med_orig && call->med_tp && call->med_tp != call->med_orig) { |
Benny Prijono | d817965 | 2008-01-23 20:39:07 +0000 | [diff] [blame] | 1255 | pjmedia_transport_close(call->med_tp); |
| 1256 | call->med_tp = call->med_orig; |
| 1257 | } |
Nanang Izzuddin | 670f71b | 2009-01-20 14:05:54 +0000 | [diff] [blame] | 1258 | |
| 1259 | check_snd_dev_idle(); |
| 1260 | |
Benny Prijono | c97608e | 2007-03-23 16:34:20 +0000 | [diff] [blame] | 1261 | return PJ_SUCCESS; |
| 1262 | } |
| 1263 | |
| 1264 | |
| 1265 | /* |
| 1266 | * DTMF callback from the stream. |
| 1267 | */ |
| 1268 | static void dtmf_callback(pjmedia_stream *strm, void *user_data, |
| 1269 | int digit) |
| 1270 | { |
| 1271 | PJ_UNUSED_ARG(strm); |
| 1272 | |
Benny Prijono | 0c06826 | 2008-02-14 14:38:52 +0000 | [diff] [blame] | 1273 | /* For discussions about call mutex protection related to this |
| 1274 | * callback, please see ticket #460: |
| 1275 | * http://trac.pjsip.org/repos/ticket/460#comment:4 |
| 1276 | */ |
Benny Prijono | c97608e | 2007-03-23 16:34:20 +0000 | [diff] [blame] | 1277 | if (pjsua_var.ua_cfg.cb.on_dtmf_digit) { |
| 1278 | pjsua_call_id call_id; |
| 1279 | |
Benny Prijono | d817965 | 2008-01-23 20:39:07 +0000 | [diff] [blame] | 1280 | call_id = (pjsua_call_id)(long)user_data; |
Benny Prijono | c97608e | 2007-03-23 16:34:20 +0000 | [diff] [blame] | 1281 | pjsua_var.ua_cfg.cb.on_dtmf_digit(call_id, digit); |
| 1282 | } |
| 1283 | } |
| 1284 | |
| 1285 | |
| 1286 | pj_status_t pjsua_media_channel_update(pjsua_call_id call_id, |
Benny Prijono | 224b4e2 | 2008-06-19 14:10:28 +0000 | [diff] [blame] | 1287 | const pjmedia_sdp_session *local_sdp, |
Benny Prijono | dbce2cf | 2007-03-28 16:24:00 +0000 | [diff] [blame] | 1288 | const pjmedia_sdp_session *remote_sdp) |
Benny Prijono | c97608e | 2007-03-23 16:34:20 +0000 | [diff] [blame] | 1289 | { |
| 1290 | int prev_media_st = 0; |
| 1291 | pjsua_call *call = &pjsua_var.calls[call_id]; |
| 1292 | pjmedia_session_info sess_info; |
Benny Prijono | 91e567e | 2007-12-28 08:51:58 +0000 | [diff] [blame] | 1293 | pjmedia_stream_info *si = NULL; |
Benny Prijono | c97608e | 2007-03-23 16:34:20 +0000 | [diff] [blame] | 1294 | pjmedia_port *media_port; |
Benny Prijono | c97608e | 2007-03-23 16:34:20 +0000 | [diff] [blame] | 1295 | pj_status_t status; |
| 1296 | |
| 1297 | /* Destroy existing media session, if any. */ |
| 1298 | prev_media_st = call->media_st; |
| 1299 | stop_media_session(call->index); |
| 1300 | |
| 1301 | /* Create media session info based on SDP parameters. |
Benny Prijono | c97608e | 2007-03-23 16:34:20 +0000 | [diff] [blame] | 1302 | */ |
| 1303 | status = pjmedia_session_info_from_sdp( call->inv->dlg->pool, |
| 1304 | pjsua_var.med_endpt, |
Benny Prijono | 91e567e | 2007-12-28 08:51:58 +0000 | [diff] [blame] | 1305 | PJMEDIA_MAX_SDP_MEDIA, &sess_info, |
Benny Prijono | c97608e | 2007-03-23 16:34:20 +0000 | [diff] [blame] | 1306 | local_sdp, remote_sdp); |
| 1307 | if (status != PJ_SUCCESS) |
| 1308 | return status; |
| 1309 | |
Benny Prijono | a310bd2 | 2008-06-27 21:19:44 +0000 | [diff] [blame] | 1310 | /* Find which session is audio */ |
| 1311 | PJ_ASSERT_RETURN(call->audio_idx != -1, PJ_EBUG); |
| 1312 | PJ_ASSERT_RETURN(call->audio_idx < (int)sess_info.stream_cnt, PJ_EBUG); |
| 1313 | si = &sess_info.stream_info[call->audio_idx]; |
Benny Prijono | 91e567e | 2007-12-28 08:51:58 +0000 | [diff] [blame] | 1314 | |
| 1315 | /* Reset session info with only one media stream */ |
| 1316 | sess_info.stream_cnt = 1; |
| 1317 | if (si != &sess_info.stream_info[0]) |
| 1318 | pj_memcpy(&sess_info.stream_info[0], si, sizeof(pjmedia_stream_info)); |
Benny Prijono | c97608e | 2007-03-23 16:34:20 +0000 | [diff] [blame] | 1319 | |
Nanang Izzuddin | 99d6952 | 2008-08-04 15:01:38 +0000 | [diff] [blame] | 1320 | /* Check if no media is active */ |
Benny Prijono | 91e567e | 2007-12-28 08:51:58 +0000 | [diff] [blame] | 1321 | if (sess_info.stream_cnt == 0 || si->dir == PJMEDIA_DIR_NONE) |
Benny Prijono | c97608e | 2007-03-23 16:34:20 +0000 | [diff] [blame] | 1322 | { |
Nanang Izzuddin | 99d6952 | 2008-08-04 15:01:38 +0000 | [diff] [blame] | 1323 | /* Call media state */ |
| 1324 | call->media_st = PJSUA_CALL_MEDIA_NONE; |
Benny Prijono | c97608e | 2007-03-23 16:34:20 +0000 | [diff] [blame] | 1325 | |
Nanang Izzuddin | 99d6952 | 2008-08-04 15:01:38 +0000 | [diff] [blame] | 1326 | /* Call media direction */ |
Benny Prijono | c97608e | 2007-03-23 16:34:20 +0000 | [diff] [blame] | 1327 | call->media_dir = PJMEDIA_DIR_NONE; |
| 1328 | |
Benny Prijono | d817965 | 2008-01-23 20:39:07 +0000 | [diff] [blame] | 1329 | /* Shutdown transport's session */ |
| 1330 | pjmedia_transport_media_stop(call->med_tp); |
Benny Prijono | 224b4e2 | 2008-06-19 14:10:28 +0000 | [diff] [blame] | 1331 | call->med_tp_st = PJSUA_MED_TP_IDLE; |
Benny Prijono | 667952e | 2007-04-02 19:27:54 +0000 | [diff] [blame] | 1332 | |
Benny Prijono | c97608e | 2007-03-23 16:34:20 +0000 | [diff] [blame] | 1333 | /* No need because we need keepalive? */ |
| 1334 | |
Benny Prijono | 68f9e4f | 2008-03-21 08:56:02 +0000 | [diff] [blame] | 1335 | /* Close upper entry of transport stack */ |
| 1336 | if (call->med_orig && (call->med_tp != call->med_orig)) { |
| 1337 | pjmedia_transport_close(call->med_tp); |
| 1338 | call->med_tp = call->med_orig; |
| 1339 | } |
| 1340 | |
Benny Prijono | c97608e | 2007-03-23 16:34:20 +0000 | [diff] [blame] | 1341 | } else { |
Nanang Izzuddin | 4375f90 | 2008-06-26 19:12:09 +0000 | [diff] [blame] | 1342 | pjmedia_transport_info tp_info; |
| 1343 | |
Benny Prijono | 224b4e2 | 2008-06-19 14:10:28 +0000 | [diff] [blame] | 1344 | /* Start/restart media transport */ |
Benny Prijono | d817965 | 2008-01-23 20:39:07 +0000 | [diff] [blame] | 1345 | status = pjmedia_transport_media_start(call->med_tp, |
| 1346 | call->inv->pool, |
| 1347 | local_sdp, remote_sdp, 0); |
| 1348 | if (status != PJ_SUCCESS) |
| 1349 | return status; |
Benny Prijono | c97608e | 2007-03-23 16:34:20 +0000 | [diff] [blame] | 1350 | |
Benny Prijono | 224b4e2 | 2008-06-19 14:10:28 +0000 | [diff] [blame] | 1351 | call->med_tp_st = PJSUA_MED_TP_RUNNING; |
| 1352 | |
Nanang Izzuddin | 4375f90 | 2008-06-26 19:12:09 +0000 | [diff] [blame] | 1353 | /* Get remote SRTP usage policy */ |
| 1354 | pjmedia_transport_info_init(&tp_info); |
| 1355 | pjmedia_transport_get_info(call->med_tp, &tp_info); |
| 1356 | if (tp_info.specific_info_cnt > 0) { |
| 1357 | int i; |
| 1358 | for (i = 0; i < tp_info.specific_info_cnt; ++i) { |
| 1359 | if (tp_info.spc_info[i].type == PJMEDIA_TRANSPORT_TYPE_SRTP) |
| 1360 | { |
| 1361 | pjmedia_srtp_info *srtp_info = |
| 1362 | (pjmedia_srtp_info*) tp_info.spc_info[i].buffer; |
| 1363 | |
| 1364 | call->rem_srtp_use = srtp_info->peer_use; |
| 1365 | break; |
| 1366 | } |
| 1367 | } |
| 1368 | } |
| 1369 | |
Benny Prijono | c97608e | 2007-03-23 16:34:20 +0000 | [diff] [blame] | 1370 | /* Override ptime, if this option is specified. */ |
| 1371 | if (pjsua_var.media_cfg.ptime != 0) { |
Benny Prijono | 91e567e | 2007-12-28 08:51:58 +0000 | [diff] [blame] | 1372 | si->param->setting.frm_per_pkt = (pj_uint8_t) |
| 1373 | (pjsua_var.media_cfg.ptime / si->param->info.frm_ptime); |
| 1374 | if (si->param->setting.frm_per_pkt == 0) |
| 1375 | si->param->setting.frm_per_pkt = 1; |
Benny Prijono | c97608e | 2007-03-23 16:34:20 +0000 | [diff] [blame] | 1376 | } |
| 1377 | |
| 1378 | /* Disable VAD, if this option is specified. */ |
| 1379 | if (pjsua_var.media_cfg.no_vad) { |
Benny Prijono | 91e567e | 2007-12-28 08:51:58 +0000 | [diff] [blame] | 1380 | si->param->setting.vad = 0; |
Benny Prijono | c97608e | 2007-03-23 16:34:20 +0000 | [diff] [blame] | 1381 | } |
| 1382 | |
| 1383 | |
| 1384 | /* Optionally, application may modify other stream settings here |
| 1385 | * (such as jitter buffer parameters, codec ptime, etc.) |
| 1386 | */ |
Benny Prijono | 91e567e | 2007-12-28 08:51:58 +0000 | [diff] [blame] | 1387 | si->jb_init = pjsua_var.media_cfg.jb_init; |
| 1388 | si->jb_min_pre = pjsua_var.media_cfg.jb_min_pre; |
| 1389 | si->jb_max_pre = pjsua_var.media_cfg.jb_max_pre; |
| 1390 | si->jb_max = pjsua_var.media_cfg.jb_max; |
Benny Prijono | c97608e | 2007-03-23 16:34:20 +0000 | [diff] [blame] | 1391 | |
Benny Prijono | 8147f40 | 2007-11-21 14:50:07 +0000 | [diff] [blame] | 1392 | /* Set SSRC */ |
Benny Prijono | 91e567e | 2007-12-28 08:51:58 +0000 | [diff] [blame] | 1393 | si->ssrc = call->ssrc; |
Benny Prijono | 8147f40 | 2007-11-21 14:50:07 +0000 | [diff] [blame] | 1394 | |
Nanang Izzuddin | a815ceb | 2008-08-26 16:51:28 +0000 | [diff] [blame] | 1395 | /* Set RTP timestamp & sequence, normally these value are intialized |
| 1396 | * automatically when stream session created, but for some cases (e.g: |
| 1397 | * call reinvite, call update) timestamp and sequence need to be kept |
| 1398 | * contigue. |
| 1399 | */ |
| 1400 | si->rtp_ts = call->rtp_tx_ts; |
| 1401 | si->rtp_seq = call->rtp_tx_seq; |
| 1402 | si->rtp_seq_ts_set = call->rtp_tx_seq_ts_set; |
| 1403 | |
Benny Prijono | c97608e | 2007-03-23 16:34:20 +0000 | [diff] [blame] | 1404 | /* Create session based on session info. */ |
| 1405 | status = pjmedia_session_create( pjsua_var.med_endpt, &sess_info, |
| 1406 | &call->med_tp, |
| 1407 | call, &call->session ); |
| 1408 | if (status != PJ_SUCCESS) { |
| 1409 | return status; |
| 1410 | } |
| 1411 | |
| 1412 | /* If DTMF callback is installed by application, install our |
| 1413 | * callback to the session. |
| 1414 | */ |
| 1415 | if (pjsua_var.ua_cfg.cb.on_dtmf_digit) { |
| 1416 | pjmedia_session_set_dtmf_callback(call->session, 0, |
| 1417 | &dtmf_callback, |
Benny Prijono | d817965 | 2008-01-23 20:39:07 +0000 | [diff] [blame] | 1418 | (void*)(long)(call->index)); |
Benny Prijono | c97608e | 2007-03-23 16:34:20 +0000 | [diff] [blame] | 1419 | } |
| 1420 | |
| 1421 | /* Get the port interface of the first stream in the session. |
| 1422 | * We need the port interface to add to the conference bridge. |
| 1423 | */ |
| 1424 | pjmedia_session_get_port(call->session, 0, &media_port); |
| 1425 | |
Benny Prijono | fc13bf6 | 2008-02-20 08:56:15 +0000 | [diff] [blame] | 1426 | /* Notify application about stream creation. |
| 1427 | * Note: application may modify media_port to point to different |
| 1428 | * media port |
| 1429 | */ |
| 1430 | if (pjsua_var.ua_cfg.cb.on_stream_created) { |
| 1431 | pjsua_var.ua_cfg.cb.on_stream_created(call_id, call->session, |
| 1432 | 0, &media_port); |
| 1433 | } |
Benny Prijono | c97608e | 2007-03-23 16:34:20 +0000 | [diff] [blame] | 1434 | |
| 1435 | /* |
| 1436 | * Add the call to conference bridge. |
| 1437 | */ |
Benny Prijono | 38fb3ea | 2008-01-02 08:27:03 +0000 | [diff] [blame] | 1438 | { |
| 1439 | char tmp[PJSIP_MAX_URL_SIZE]; |
| 1440 | pj_str_t port_name; |
| 1441 | |
| 1442 | port_name.ptr = tmp; |
| 1443 | port_name.slen = pjsip_uri_print(PJSIP_URI_IN_REQ_URI, |
| 1444 | call->inv->dlg->remote.info->uri, |
| 1445 | tmp, sizeof(tmp)); |
| 1446 | if (port_name.slen < 1) { |
| 1447 | port_name = pj_str("call"); |
| 1448 | } |
| 1449 | status = pjmedia_conf_add_port( pjsua_var.mconf, call->inv->pool, |
| 1450 | media_port, |
| 1451 | &port_name, |
| 1452 | (unsigned*)&call->conf_slot); |
| 1453 | if (status != PJ_SUCCESS) { |
| 1454 | return status; |
| 1455 | } |
Benny Prijono | c97608e | 2007-03-23 16:34:20 +0000 | [diff] [blame] | 1456 | } |
| 1457 | |
Nanang Izzuddin | 99d6952 | 2008-08-04 15:01:38 +0000 | [diff] [blame] | 1458 | /* Call media direction */ |
Benny Prijono | 91e567e | 2007-12-28 08:51:58 +0000 | [diff] [blame] | 1459 | call->media_dir = si->dir; |
Nanang Izzuddin | 99d6952 | 2008-08-04 15:01:38 +0000 | [diff] [blame] | 1460 | |
| 1461 | /* Call media state */ |
| 1462 | if (call->local_hold) |
| 1463 | call->media_st = PJSUA_CALL_MEDIA_LOCAL_HOLD; |
| 1464 | else if (call->media_dir == PJMEDIA_DIR_DECODING) |
| 1465 | call->media_st = PJSUA_CALL_MEDIA_REMOTE_HOLD; |
| 1466 | else |
| 1467 | call->media_st = PJSUA_CALL_MEDIA_ACTIVE; |
Benny Prijono | c97608e | 2007-03-23 16:34:20 +0000 | [diff] [blame] | 1468 | } |
| 1469 | |
| 1470 | /* Print info. */ |
| 1471 | { |
| 1472 | char info[80]; |
| 1473 | int info_len = 0; |
| 1474 | unsigned i; |
| 1475 | |
| 1476 | for (i=0; i<sess_info.stream_cnt; ++i) { |
| 1477 | int len; |
| 1478 | const char *dir; |
| 1479 | pjmedia_stream_info *strm_info = &sess_info.stream_info[i]; |
| 1480 | |
| 1481 | switch (strm_info->dir) { |
| 1482 | case PJMEDIA_DIR_NONE: |
| 1483 | dir = "inactive"; |
| 1484 | break; |
| 1485 | case PJMEDIA_DIR_ENCODING: |
| 1486 | dir = "sendonly"; |
| 1487 | break; |
| 1488 | case PJMEDIA_DIR_DECODING: |
| 1489 | dir = "recvonly"; |
| 1490 | break; |
| 1491 | case PJMEDIA_DIR_ENCODING_DECODING: |
| 1492 | dir = "sendrecv"; |
| 1493 | break; |
| 1494 | default: |
| 1495 | dir = "unknown"; |
| 1496 | break; |
| 1497 | } |
| 1498 | len = pj_ansi_sprintf( info+info_len, |
| 1499 | ", stream #%d: %.*s (%s)", i, |
| 1500 | (int)strm_info->fmt.encoding_name.slen, |
| 1501 | strm_info->fmt.encoding_name.ptr, |
| 1502 | dir); |
| 1503 | if (len > 0) |
| 1504 | info_len += len; |
| 1505 | } |
| 1506 | PJ_LOG(4,(THIS_FILE,"Media updates%s", info)); |
| 1507 | } |
| 1508 | |
| 1509 | return PJ_SUCCESS; |
| 1510 | } |
| 1511 | |
| 1512 | |
Benny Prijono | eebe9af | 2006-06-13 22:57:13 +0000 | [diff] [blame] | 1513 | /* |
| 1514 | * Get maxinum number of conference ports. |
| 1515 | */ |
| 1516 | PJ_DEF(unsigned) pjsua_conf_get_max_ports(void) |
| 1517 | { |
| 1518 | return pjsua_var.media_cfg.max_media_ports; |
| 1519 | } |
| 1520 | |
| 1521 | |
| 1522 | /* |
| 1523 | * Get current number of active ports in the bridge. |
| 1524 | */ |
| 1525 | PJ_DEF(unsigned) pjsua_conf_get_active_ports(void) |
| 1526 | { |
Benny Prijono | 38fb3ea | 2008-01-02 08:27:03 +0000 | [diff] [blame] | 1527 | unsigned ports[PJSUA_MAX_CONF_PORTS]; |
Benny Prijono | eebe9af | 2006-06-13 22:57:13 +0000 | [diff] [blame] | 1528 | unsigned count = PJ_ARRAY_SIZE(ports); |
| 1529 | pj_status_t status; |
| 1530 | |
| 1531 | status = pjmedia_conf_enum_ports(pjsua_var.mconf, ports, &count); |
| 1532 | if (status != PJ_SUCCESS) |
| 1533 | count = 0; |
| 1534 | |
| 1535 | return count; |
| 1536 | } |
| 1537 | |
| 1538 | |
| 1539 | /* |
| 1540 | * Enumerate all conference ports. |
| 1541 | */ |
| 1542 | PJ_DEF(pj_status_t) pjsua_enum_conf_ports(pjsua_conf_port_id id[], |
| 1543 | unsigned *count) |
| 1544 | { |
| 1545 | return pjmedia_conf_enum_ports(pjsua_var.mconf, (unsigned*)id, count); |
| 1546 | } |
| 1547 | |
| 1548 | |
| 1549 | /* |
| 1550 | * Get information about the specified conference port |
| 1551 | */ |
| 1552 | PJ_DEF(pj_status_t) pjsua_conf_get_port_info( pjsua_conf_port_id id, |
| 1553 | pjsua_conf_port_info *info) |
| 1554 | { |
| 1555 | pjmedia_conf_port_info cinfo; |
Benny Prijono | 0498d90 | 2006-06-19 14:49:14 +0000 | [diff] [blame] | 1556 | unsigned i; |
Benny Prijono | eebe9af | 2006-06-13 22:57:13 +0000 | [diff] [blame] | 1557 | pj_status_t status; |
| 1558 | |
| 1559 | status = pjmedia_conf_get_port_info( pjsua_var.mconf, id, &cinfo); |
| 1560 | if (status != PJ_SUCCESS) |
| 1561 | return status; |
| 1562 | |
Benny Prijono | ac623b3 | 2006-07-03 15:19:31 +0000 | [diff] [blame] | 1563 | pj_bzero(info, sizeof(*info)); |
Benny Prijono | eebe9af | 2006-06-13 22:57:13 +0000 | [diff] [blame] | 1564 | info->slot_id = id; |
| 1565 | info->name = cinfo.name; |
| 1566 | info->clock_rate = cinfo.clock_rate; |
| 1567 | info->channel_count = cinfo.channel_count; |
| 1568 | info->samples_per_frame = cinfo.samples_per_frame; |
| 1569 | info->bits_per_sample = cinfo.bits_per_sample; |
| 1570 | |
| 1571 | /* Build array of listeners */ |
Benny Prijono | c78c3a3 | 2006-06-16 15:54:43 +0000 | [diff] [blame] | 1572 | info->listener_cnt = cinfo.listener_cnt; |
| 1573 | for (i=0; i<cinfo.listener_cnt; ++i) { |
| 1574 | info->listeners[i] = cinfo.listener_slots[i]; |
Benny Prijono | eebe9af | 2006-06-13 22:57:13 +0000 | [diff] [blame] | 1575 | } |
| 1576 | |
| 1577 | return PJ_SUCCESS; |
| 1578 | } |
| 1579 | |
| 1580 | |
| 1581 | /* |
Benny Prijono | e909eac | 2006-07-27 22:04:56 +0000 | [diff] [blame] | 1582 | * Add arbitrary media port to PJSUA's conference bridge. |
| 1583 | */ |
| 1584 | PJ_DEF(pj_status_t) pjsua_conf_add_port( pj_pool_t *pool, |
| 1585 | pjmedia_port *port, |
| 1586 | pjsua_conf_port_id *p_id) |
| 1587 | { |
| 1588 | pj_status_t status; |
| 1589 | |
| 1590 | status = pjmedia_conf_add_port(pjsua_var.mconf, pool, |
| 1591 | port, NULL, (unsigned*)p_id); |
| 1592 | if (status != PJ_SUCCESS) { |
| 1593 | if (p_id) |
| 1594 | *p_id = PJSUA_INVALID_ID; |
| 1595 | } |
| 1596 | |
| 1597 | return status; |
| 1598 | } |
| 1599 | |
| 1600 | |
| 1601 | /* |
| 1602 | * Remove arbitrary slot from the conference bridge. |
| 1603 | */ |
| 1604 | PJ_DEF(pj_status_t) pjsua_conf_remove_port(pjsua_conf_port_id id) |
| 1605 | { |
Nanang Izzuddin | 148fd39 | 2008-06-16 09:52:50 +0000 | [diff] [blame] | 1606 | pj_status_t status; |
| 1607 | |
| 1608 | status = pjmedia_conf_remove_port(pjsua_var.mconf, (unsigned)id); |
| 1609 | check_snd_dev_idle(); |
| 1610 | |
| 1611 | return status; |
Benny Prijono | e909eac | 2006-07-27 22:04:56 +0000 | [diff] [blame] | 1612 | } |
| 1613 | |
| 1614 | |
| 1615 | /* |
Benny Prijono | eebe9af | 2006-06-13 22:57:13 +0000 | [diff] [blame] | 1616 | * Establish unidirectional media flow from souce to sink. |
| 1617 | */ |
| 1618 | PJ_DEF(pj_status_t) pjsua_conf_connect( pjsua_conf_port_id source, |
| 1619 | pjsua_conf_port_id sink) |
| 1620 | { |
Nanang Izzuddin | 68559c3 | 2008-06-13 17:01:46 +0000 | [diff] [blame] | 1621 | /* If sound device idle timer is active, cancel it first. */ |
| 1622 | if (pjsua_var.snd_idle_timer.id) { |
| 1623 | pjsip_endpt_cancel_timer(pjsua_var.endpt, &pjsua_var.snd_idle_timer); |
| 1624 | pjsua_var.snd_idle_timer.id = PJ_FALSE; |
| 1625 | } |
| 1626 | |
| 1627 | /* Create sound port if none is instantiated */ |
| 1628 | if (pjsua_var.snd_port==NULL && pjsua_var.null_snd==NULL && |
| 1629 | !pjsua_var.no_snd) |
| 1630 | { |
| 1631 | pj_status_t status; |
| 1632 | |
| 1633 | status = pjsua_set_snd_dev(pjsua_var.cap_dev, pjsua_var.play_dev); |
| 1634 | if (status != PJ_SUCCESS) { |
| 1635 | pjsua_perror(THIS_FILE, "Error opening sound device", status); |
| 1636 | return status; |
| 1637 | } |
| 1638 | } |
| 1639 | |
Benny Prijono | eebe9af | 2006-06-13 22:57:13 +0000 | [diff] [blame] | 1640 | return pjmedia_conf_connect_port(pjsua_var.mconf, source, sink, 0); |
| 1641 | } |
| 1642 | |
| 1643 | |
| 1644 | /* |
| 1645 | * Disconnect media flow from the source to destination port. |
| 1646 | */ |
| 1647 | PJ_DEF(pj_status_t) pjsua_conf_disconnect( pjsua_conf_port_id source, |
| 1648 | pjsua_conf_port_id sink) |
| 1649 | { |
Nanang Izzuddin | 68559c3 | 2008-06-13 17:01:46 +0000 | [diff] [blame] | 1650 | pj_status_t status; |
| 1651 | |
| 1652 | status = pjmedia_conf_disconnect_port(pjsua_var.mconf, source, sink); |
Nanang Izzuddin | 148fd39 | 2008-06-16 09:52:50 +0000 | [diff] [blame] | 1653 | check_snd_dev_idle(); |
Nanang Izzuddin | 68559c3 | 2008-06-13 17:01:46 +0000 | [diff] [blame] | 1654 | |
| 1655 | return status; |
Benny Prijono | eebe9af | 2006-06-13 22:57:13 +0000 | [diff] [blame] | 1656 | } |
| 1657 | |
| 1658 | |
Benny Prijono | 6dd967c | 2006-12-26 02:27:14 +0000 | [diff] [blame] | 1659 | /* |
| 1660 | * Adjust the signal level to be transmitted from the bridge to the |
| 1661 | * specified port by making it louder or quieter. |
| 1662 | */ |
| 1663 | PJ_DEF(pj_status_t) pjsua_conf_adjust_tx_level(pjsua_conf_port_id slot, |
| 1664 | float level) |
| 1665 | { |
| 1666 | return pjmedia_conf_adjust_tx_level(pjsua_var.mconf, slot, |
| 1667 | (int)((level-1) * 128)); |
| 1668 | } |
| 1669 | |
| 1670 | /* |
| 1671 | * Adjust the signal level to be received from the specified port (to |
| 1672 | * the bridge) by making it louder or quieter. |
| 1673 | */ |
| 1674 | PJ_DEF(pj_status_t) pjsua_conf_adjust_rx_level(pjsua_conf_port_id slot, |
| 1675 | float level) |
| 1676 | { |
| 1677 | return pjmedia_conf_adjust_rx_level(pjsua_var.mconf, slot, |
| 1678 | (int)((level-1) * 128)); |
| 1679 | } |
| 1680 | |
| 1681 | |
| 1682 | /* |
| 1683 | * Get last signal level transmitted to or received from the specified port. |
| 1684 | */ |
| 1685 | PJ_DEF(pj_status_t) pjsua_conf_get_signal_level(pjsua_conf_port_id slot, |
| 1686 | unsigned *tx_level, |
| 1687 | unsigned *rx_level) |
| 1688 | { |
| 1689 | return pjmedia_conf_get_signal_level(pjsua_var.mconf, slot, |
| 1690 | tx_level, rx_level); |
| 1691 | } |
| 1692 | |
Benny Prijono | eebe9af | 2006-06-13 22:57:13 +0000 | [diff] [blame] | 1693 | /***************************************************************************** |
| 1694 | * File player. |
| 1695 | */ |
| 1696 | |
Benny Prijono | d5696da | 2007-07-17 16:25:45 +0000 | [diff] [blame] | 1697 | static char* get_basename(const char *path, unsigned len) |
| 1698 | { |
| 1699 | char *p = ((char*)path) + len; |
| 1700 | |
| 1701 | if (len==0) |
| 1702 | return p; |
| 1703 | |
Benny Prijono | 1f61a8f | 2007-08-16 10:11:44 +0000 | [diff] [blame] | 1704 | for (--p; p!=path && *p!='/' && *p!='\\'; ) --p; |
Benny Prijono | d5696da | 2007-07-17 16:25:45 +0000 | [diff] [blame] | 1705 | |
| 1706 | return (p==path) ? p : p+1; |
| 1707 | } |
| 1708 | |
| 1709 | |
Benny Prijono | eebe9af | 2006-06-13 22:57:13 +0000 | [diff] [blame] | 1710 | /* |
| 1711 | * Create a file player, and automatically connect this player to |
| 1712 | * the conference bridge. |
| 1713 | */ |
| 1714 | PJ_DEF(pj_status_t) pjsua_player_create( const pj_str_t *filename, |
| 1715 | unsigned options, |
Benny Prijono | eebe9af | 2006-06-13 22:57:13 +0000 | [diff] [blame] | 1716 | pjsua_player_id *p_id) |
| 1717 | { |
| 1718 | unsigned slot, file_id; |
Benny Prijono | a66c331 | 2007-01-21 23:12:40 +0000 | [diff] [blame] | 1719 | char path[PJ_MAXPATH]; |
Benny Prijono | d5696da | 2007-07-17 16:25:45 +0000 | [diff] [blame] | 1720 | pj_pool_t *pool; |
Benny Prijono | eebe9af | 2006-06-13 22:57:13 +0000 | [diff] [blame] | 1721 | pjmedia_port *port; |
| 1722 | pj_status_t status; |
| 1723 | |
| 1724 | if (pjsua_var.player_cnt >= PJ_ARRAY_SIZE(pjsua_var.player)) |
| 1725 | return PJ_ETOOMANY; |
| 1726 | |
| 1727 | PJSUA_LOCK(); |
| 1728 | |
| 1729 | for (file_id=0; file_id<PJ_ARRAY_SIZE(pjsua_var.player); ++file_id) { |
| 1730 | if (pjsua_var.player[file_id].port == NULL) |
| 1731 | break; |
| 1732 | } |
| 1733 | |
| 1734 | if (file_id == PJ_ARRAY_SIZE(pjsua_var.player)) { |
| 1735 | /* This is unexpected */ |
| 1736 | PJSUA_UNLOCK(); |
| 1737 | pj_assert(0); |
| 1738 | return PJ_EBUG; |
| 1739 | } |
| 1740 | |
| 1741 | pj_memcpy(path, filename->ptr, filename->slen); |
| 1742 | path[filename->slen] = '\0'; |
Benny Prijono | d5696da | 2007-07-17 16:25:45 +0000 | [diff] [blame] | 1743 | |
| 1744 | pool = pjsua_pool_create(get_basename(path, filename->slen), 1000, 1000); |
| 1745 | if (!pool) { |
| 1746 | PJSUA_UNLOCK(); |
| 1747 | return PJ_ENOMEM; |
| 1748 | } |
| 1749 | |
Nanang Izzuddin | 81e9bd5 | 2008-06-27 12:52:51 +0000 | [diff] [blame] | 1750 | status = pjmedia_wav_player_port_create( |
| 1751 | pool, path, |
| 1752 | pjsua_var.mconf_cfg.samples_per_frame * |
| 1753 | 1000 / pjsua_var.media_cfg.channel_count / |
| 1754 | pjsua_var.media_cfg.clock_rate, |
| 1755 | options, 0, &port); |
Benny Prijono | eebe9af | 2006-06-13 22:57:13 +0000 | [diff] [blame] | 1756 | if (status != PJ_SUCCESS) { |
| 1757 | PJSUA_UNLOCK(); |
| 1758 | pjsua_perror(THIS_FILE, "Unable to open file for playback", status); |
Benny Prijono | d5696da | 2007-07-17 16:25:45 +0000 | [diff] [blame] | 1759 | pj_pool_release(pool); |
Benny Prijono | eebe9af | 2006-06-13 22:57:13 +0000 | [diff] [blame] | 1760 | return status; |
| 1761 | } |
| 1762 | |
Benny Prijono | 5297af9 | 2008-03-18 13:40:40 +0000 | [diff] [blame] | 1763 | status = pjmedia_conf_add_port(pjsua_var.mconf, pool, |
Benny Prijono | eebe9af | 2006-06-13 22:57:13 +0000 | [diff] [blame] | 1764 | port, filename, &slot); |
| 1765 | if (status != PJ_SUCCESS) { |
| 1766 | pjmedia_port_destroy(port); |
| 1767 | PJSUA_UNLOCK(); |
Benny Prijono | 32e4f49 | 2007-01-24 00:44:26 +0000 | [diff] [blame] | 1768 | pjsua_perror(THIS_FILE, "Unable to add file to conference bridge", |
| 1769 | status); |
Benny Prijono | d5696da | 2007-07-17 16:25:45 +0000 | [diff] [blame] | 1770 | pj_pool_release(pool); |
Benny Prijono | eebe9af | 2006-06-13 22:57:13 +0000 | [diff] [blame] | 1771 | return status; |
| 1772 | } |
| 1773 | |
Benny Prijono | a66c331 | 2007-01-21 23:12:40 +0000 | [diff] [blame] | 1774 | pjsua_var.player[file_id].type = 0; |
Benny Prijono | d5696da | 2007-07-17 16:25:45 +0000 | [diff] [blame] | 1775 | pjsua_var.player[file_id].pool = pool; |
Benny Prijono | eebe9af | 2006-06-13 22:57:13 +0000 | [diff] [blame] | 1776 | pjsua_var.player[file_id].port = port; |
| 1777 | pjsua_var.player[file_id].slot = slot; |
| 1778 | |
| 1779 | if (p_id) *p_id = file_id; |
| 1780 | |
| 1781 | ++pjsua_var.player_cnt; |
| 1782 | |
| 1783 | PJSUA_UNLOCK(); |
| 1784 | return PJ_SUCCESS; |
| 1785 | } |
| 1786 | |
| 1787 | |
| 1788 | /* |
Benny Prijono | a66c331 | 2007-01-21 23:12:40 +0000 | [diff] [blame] | 1789 | * Create a file playlist media port, and automatically add the port |
| 1790 | * to the conference bridge. |
| 1791 | */ |
| 1792 | PJ_DEF(pj_status_t) pjsua_playlist_create( const pj_str_t file_names[], |
| 1793 | unsigned file_count, |
| 1794 | const pj_str_t *label, |
| 1795 | unsigned options, |
| 1796 | pjsua_player_id *p_id) |
| 1797 | { |
| 1798 | unsigned slot, file_id, ptime; |
Benny Prijono | d5696da | 2007-07-17 16:25:45 +0000 | [diff] [blame] | 1799 | pj_pool_t *pool; |
Benny Prijono | a66c331 | 2007-01-21 23:12:40 +0000 | [diff] [blame] | 1800 | pjmedia_port *port; |
| 1801 | pj_status_t status; |
| 1802 | |
| 1803 | if (pjsua_var.player_cnt >= PJ_ARRAY_SIZE(pjsua_var.player)) |
| 1804 | return PJ_ETOOMANY; |
| 1805 | |
| 1806 | PJSUA_LOCK(); |
| 1807 | |
| 1808 | for (file_id=0; file_id<PJ_ARRAY_SIZE(pjsua_var.player); ++file_id) { |
| 1809 | if (pjsua_var.player[file_id].port == NULL) |
| 1810 | break; |
| 1811 | } |
| 1812 | |
| 1813 | if (file_id == PJ_ARRAY_SIZE(pjsua_var.player)) { |
| 1814 | /* This is unexpected */ |
| 1815 | PJSUA_UNLOCK(); |
| 1816 | pj_assert(0); |
| 1817 | return PJ_EBUG; |
| 1818 | } |
| 1819 | |
| 1820 | |
| 1821 | ptime = pjsua_var.mconf_cfg.samples_per_frame * 1000 / |
| 1822 | pjsua_var.media_cfg.clock_rate; |
| 1823 | |
Benny Prijono | d5696da | 2007-07-17 16:25:45 +0000 | [diff] [blame] | 1824 | pool = pjsua_pool_create("playlist", 1000, 1000); |
| 1825 | if (!pool) { |
| 1826 | PJSUA_UNLOCK(); |
| 1827 | return PJ_ENOMEM; |
| 1828 | } |
| 1829 | |
| 1830 | status = pjmedia_wav_playlist_create(pool, label, |
Benny Prijono | a66c331 | 2007-01-21 23:12:40 +0000 | [diff] [blame] | 1831 | file_names, file_count, |
| 1832 | ptime, options, 0, &port); |
| 1833 | if (status != PJ_SUCCESS) { |
| 1834 | PJSUA_UNLOCK(); |
| 1835 | pjsua_perror(THIS_FILE, "Unable to create playlist", status); |
Benny Prijono | d5696da | 2007-07-17 16:25:45 +0000 | [diff] [blame] | 1836 | pj_pool_release(pool); |
Benny Prijono | a66c331 | 2007-01-21 23:12:40 +0000 | [diff] [blame] | 1837 | return status; |
| 1838 | } |
| 1839 | |
Benny Prijono | d5696da | 2007-07-17 16:25:45 +0000 | [diff] [blame] | 1840 | status = pjmedia_conf_add_port(pjsua_var.mconf, pool, |
Benny Prijono | a66c331 | 2007-01-21 23:12:40 +0000 | [diff] [blame] | 1841 | port, &port->info.name, &slot); |
| 1842 | if (status != PJ_SUCCESS) { |
| 1843 | pjmedia_port_destroy(port); |
| 1844 | PJSUA_UNLOCK(); |
| 1845 | pjsua_perror(THIS_FILE, "Unable to add port", status); |
Benny Prijono | d5696da | 2007-07-17 16:25:45 +0000 | [diff] [blame] | 1846 | pj_pool_release(pool); |
Benny Prijono | a66c331 | 2007-01-21 23:12:40 +0000 | [diff] [blame] | 1847 | return status; |
| 1848 | } |
| 1849 | |
| 1850 | pjsua_var.player[file_id].type = 1; |
Benny Prijono | d5696da | 2007-07-17 16:25:45 +0000 | [diff] [blame] | 1851 | pjsua_var.player[file_id].pool = pool; |
Benny Prijono | a66c331 | 2007-01-21 23:12:40 +0000 | [diff] [blame] | 1852 | pjsua_var.player[file_id].port = port; |
| 1853 | pjsua_var.player[file_id].slot = slot; |
| 1854 | |
| 1855 | if (p_id) *p_id = file_id; |
| 1856 | |
| 1857 | ++pjsua_var.player_cnt; |
| 1858 | |
| 1859 | PJSUA_UNLOCK(); |
| 1860 | return PJ_SUCCESS; |
| 1861 | |
| 1862 | } |
| 1863 | |
| 1864 | |
| 1865 | /* |
Benny Prijono | eebe9af | 2006-06-13 22:57:13 +0000 | [diff] [blame] | 1866 | * Get conference port ID associated with player. |
| 1867 | */ |
| 1868 | PJ_DEF(pjsua_conf_port_id) pjsua_player_get_conf_port(pjsua_player_id id) |
| 1869 | { |
Benny Prijono | a1e6968 | 2007-05-11 15:14:34 +0000 | [diff] [blame] | 1870 | 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] | 1871 | PJ_ASSERT_RETURN(pjsua_var.player[id].port != NULL, PJ_EINVAL); |
| 1872 | |
| 1873 | return pjsua_var.player[id].slot; |
| 1874 | } |
| 1875 | |
Benny Prijono | 469b152 | 2006-12-26 03:05:17 +0000 | [diff] [blame] | 1876 | /* |
| 1877 | * Get the media port for the player. |
| 1878 | */ |
Benny Prijono | be41d86 | 2008-01-18 13:24:28 +0000 | [diff] [blame] | 1879 | 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] | 1880 | pjmedia_port **p_port) |
| 1881 | { |
Benny Prijono | a1e6968 | 2007-05-11 15:14:34 +0000 | [diff] [blame] | 1882 | 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] | 1883 | PJ_ASSERT_RETURN(pjsua_var.player[id].port != NULL, PJ_EINVAL); |
| 1884 | PJ_ASSERT_RETURN(p_port != NULL, PJ_EINVAL); |
| 1885 | |
| 1886 | *p_port = pjsua_var.player[id].port; |
| 1887 | |
| 1888 | return PJ_SUCCESS; |
| 1889 | } |
Benny Prijono | eebe9af | 2006-06-13 22:57:13 +0000 | [diff] [blame] | 1890 | |
| 1891 | /* |
| 1892 | * Set playback position. |
| 1893 | */ |
| 1894 | PJ_DEF(pj_status_t) pjsua_player_set_pos( pjsua_player_id id, |
| 1895 | pj_uint32_t samples) |
| 1896 | { |
Benny Prijono | a1e6968 | 2007-05-11 15:14:34 +0000 | [diff] [blame] | 1897 | 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] | 1898 | PJ_ASSERT_RETURN(pjsua_var.player[id].port != NULL, PJ_EINVAL); |
Benny Prijono | a66c331 | 2007-01-21 23:12:40 +0000 | [diff] [blame] | 1899 | PJ_ASSERT_RETURN(pjsua_var.player[id].type == 0, PJ_EINVAL); |
Benny Prijono | eebe9af | 2006-06-13 22:57:13 +0000 | [diff] [blame] | 1900 | |
| 1901 | return pjmedia_wav_player_port_set_pos(pjsua_var.player[id].port, samples); |
| 1902 | } |
| 1903 | |
| 1904 | |
| 1905 | /* |
| 1906 | * Close the file, remove the player from the bridge, and free |
| 1907 | * resources associated with the file player. |
| 1908 | */ |
| 1909 | PJ_DEF(pj_status_t) pjsua_player_destroy(pjsua_player_id id) |
| 1910 | { |
Benny Prijono | a1e6968 | 2007-05-11 15:14:34 +0000 | [diff] [blame] | 1911 | 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] | 1912 | PJ_ASSERT_RETURN(pjsua_var.player[id].port != NULL, PJ_EINVAL); |
| 1913 | |
| 1914 | PJSUA_LOCK(); |
| 1915 | |
| 1916 | if (pjsua_var.player[id].port) { |
Nanang Izzuddin | 148fd39 | 2008-06-16 09:52:50 +0000 | [diff] [blame] | 1917 | pjsua_conf_remove_port(pjsua_var.player[id].slot); |
Benny Prijono | eebe9af | 2006-06-13 22:57:13 +0000 | [diff] [blame] | 1918 | pjmedia_port_destroy(pjsua_var.player[id].port); |
| 1919 | pjsua_var.player[id].port = NULL; |
| 1920 | pjsua_var.player[id].slot = 0xFFFF; |
Benny Prijono | d5696da | 2007-07-17 16:25:45 +0000 | [diff] [blame] | 1921 | pj_pool_release(pjsua_var.player[id].pool); |
| 1922 | pjsua_var.player[id].pool = NULL; |
Benny Prijono | eebe9af | 2006-06-13 22:57:13 +0000 | [diff] [blame] | 1923 | pjsua_var.player_cnt--; |
| 1924 | } |
| 1925 | |
| 1926 | PJSUA_UNLOCK(); |
| 1927 | |
| 1928 | return PJ_SUCCESS; |
| 1929 | } |
| 1930 | |
| 1931 | |
| 1932 | /***************************************************************************** |
| 1933 | * File recorder. |
| 1934 | */ |
| 1935 | |
| 1936 | /* |
| 1937 | * Create a file recorder, and automatically connect this recorder to |
| 1938 | * the conference bridge. |
| 1939 | */ |
| 1940 | 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] | 1941 | unsigned enc_type, |
| 1942 | void *enc_param, |
Benny Prijono | eebe9af | 2006-06-13 22:57:13 +0000 | [diff] [blame] | 1943 | pj_ssize_t max_size, |
| 1944 | unsigned options, |
Benny Prijono | eebe9af | 2006-06-13 22:57:13 +0000 | [diff] [blame] | 1945 | pjsua_recorder_id *p_id) |
| 1946 | { |
Benny Prijono | b3cdb2b | 2006-10-19 15:49:47 +0000 | [diff] [blame] | 1947 | enum Format |
| 1948 | { |
| 1949 | FMT_UNKNOWN, |
| 1950 | FMT_WAV, |
| 1951 | FMT_MP3, |
| 1952 | }; |
Benny Prijono | eebe9af | 2006-06-13 22:57:13 +0000 | [diff] [blame] | 1953 | unsigned slot, file_id; |
Benny Prijono | a66c331 | 2007-01-21 23:12:40 +0000 | [diff] [blame] | 1954 | char path[PJ_MAXPATH]; |
Benny Prijono | b3cdb2b | 2006-10-19 15:49:47 +0000 | [diff] [blame] | 1955 | pj_str_t ext; |
Benny Prijono | 8f31052 | 2006-10-20 11:08:49 +0000 | [diff] [blame] | 1956 | int file_format; |
Benny Prijono | d5696da | 2007-07-17 16:25:45 +0000 | [diff] [blame] | 1957 | pj_pool_t *pool; |
Benny Prijono | eebe9af | 2006-06-13 22:57:13 +0000 | [diff] [blame] | 1958 | pjmedia_port *port; |
| 1959 | pj_status_t status; |
| 1960 | |
Benny Prijono | b3cdb2b | 2006-10-19 15:49:47 +0000 | [diff] [blame] | 1961 | /* Filename must present */ |
| 1962 | PJ_ASSERT_RETURN(filename != NULL, PJ_EINVAL); |
| 1963 | |
Benny Prijono | 00cae61 | 2006-07-31 15:19:36 +0000 | [diff] [blame] | 1964 | /* Don't support max_size at present */ |
Benny Prijono | b3cdb2b | 2006-10-19 15:49:47 +0000 | [diff] [blame] | 1965 | PJ_ASSERT_RETURN(max_size == 0 || max_size == -1, PJ_EINVAL); |
Benny Prijono | 00cae61 | 2006-07-31 15:19:36 +0000 | [diff] [blame] | 1966 | |
Benny Prijono | 8f31052 | 2006-10-20 11:08:49 +0000 | [diff] [blame] | 1967 | /* Don't support encoding type at present */ |
| 1968 | PJ_ASSERT_RETURN(enc_type == 0, PJ_EINVAL); |
Benny Prijono | 00cae61 | 2006-07-31 15:19:36 +0000 | [diff] [blame] | 1969 | |
Benny Prijono | eebe9af | 2006-06-13 22:57:13 +0000 | [diff] [blame] | 1970 | if (pjsua_var.rec_cnt >= PJ_ARRAY_SIZE(pjsua_var.recorder)) |
| 1971 | return PJ_ETOOMANY; |
| 1972 | |
Benny Prijono | b3cdb2b | 2006-10-19 15:49:47 +0000 | [diff] [blame] | 1973 | /* Determine the file format */ |
| 1974 | ext.ptr = filename->ptr + filename->slen - 4; |
| 1975 | ext.slen = 4; |
| 1976 | |
| 1977 | if (pj_stricmp2(&ext, ".wav") == 0) |
| 1978 | file_format = FMT_WAV; |
| 1979 | else if (pj_stricmp2(&ext, ".mp3") == 0) |
| 1980 | file_format = FMT_MP3; |
| 1981 | else { |
| 1982 | PJ_LOG(1,(THIS_FILE, "pjsua_recorder_create() error: unable to " |
| 1983 | "determine file format for %.*s", |
| 1984 | (int)filename->slen, filename->ptr)); |
| 1985 | return PJ_ENOTSUP; |
| 1986 | } |
| 1987 | |
Benny Prijono | eebe9af | 2006-06-13 22:57:13 +0000 | [diff] [blame] | 1988 | PJSUA_LOCK(); |
| 1989 | |
| 1990 | for (file_id=0; file_id<PJ_ARRAY_SIZE(pjsua_var.recorder); ++file_id) { |
| 1991 | if (pjsua_var.recorder[file_id].port == NULL) |
| 1992 | break; |
| 1993 | } |
| 1994 | |
| 1995 | if (file_id == PJ_ARRAY_SIZE(pjsua_var.recorder)) { |
| 1996 | /* This is unexpected */ |
| 1997 | PJSUA_UNLOCK(); |
| 1998 | pj_assert(0); |
| 1999 | return PJ_EBUG; |
| 2000 | } |
| 2001 | |
| 2002 | pj_memcpy(path, filename->ptr, filename->slen); |
| 2003 | path[filename->slen] = '\0'; |
Benny Prijono | b3cdb2b | 2006-10-19 15:49:47 +0000 | [diff] [blame] | 2004 | |
Benny Prijono | d5696da | 2007-07-17 16:25:45 +0000 | [diff] [blame] | 2005 | pool = pjsua_pool_create(get_basename(path, filename->slen), 1000, 1000); |
| 2006 | if (!pool) { |
| 2007 | PJSUA_UNLOCK(); |
| 2008 | return PJ_ENOMEM; |
| 2009 | } |
| 2010 | |
Benny Prijono | b3cdb2b | 2006-10-19 15:49:47 +0000 | [diff] [blame] | 2011 | if (file_format == FMT_WAV) { |
Benny Prijono | d5696da | 2007-07-17 16:25:45 +0000 | [diff] [blame] | 2012 | status = pjmedia_wav_writer_port_create(pool, path, |
Benny Prijono | b3cdb2b | 2006-10-19 15:49:47 +0000 | [diff] [blame] | 2013 | pjsua_var.media_cfg.clock_rate, |
| 2014 | pjsua_var.mconf_cfg.channel_count, |
| 2015 | pjsua_var.mconf_cfg.samples_per_frame, |
| 2016 | pjsua_var.mconf_cfg.bits_per_sample, |
| 2017 | options, 0, &port); |
Benny Prijono | b3cdb2b | 2006-10-19 15:49:47 +0000 | [diff] [blame] | 2018 | } else { |
Benny Prijono | c95a0f0 | 2007-04-09 07:06:08 +0000 | [diff] [blame] | 2019 | PJ_UNUSED_ARG(enc_param); |
Benny Prijono | b3cdb2b | 2006-10-19 15:49:47 +0000 | [diff] [blame] | 2020 | port = NULL; |
| 2021 | status = PJ_ENOTSUP; |
| 2022 | } |
| 2023 | |
Benny Prijono | eebe9af | 2006-06-13 22:57:13 +0000 | [diff] [blame] | 2024 | if (status != PJ_SUCCESS) { |
| 2025 | PJSUA_UNLOCK(); |
| 2026 | pjsua_perror(THIS_FILE, "Unable to open file for recording", status); |
Benny Prijono | d5696da | 2007-07-17 16:25:45 +0000 | [diff] [blame] | 2027 | pj_pool_release(pool); |
Benny Prijono | eebe9af | 2006-06-13 22:57:13 +0000 | [diff] [blame] | 2028 | return status; |
| 2029 | } |
| 2030 | |
Benny Prijono | d5696da | 2007-07-17 16:25:45 +0000 | [diff] [blame] | 2031 | status = pjmedia_conf_add_port(pjsua_var.mconf, pool, |
Benny Prijono | eebe9af | 2006-06-13 22:57:13 +0000 | [diff] [blame] | 2032 | port, filename, &slot); |
| 2033 | if (status != PJ_SUCCESS) { |
| 2034 | pjmedia_port_destroy(port); |
| 2035 | PJSUA_UNLOCK(); |
Benny Prijono | d5696da | 2007-07-17 16:25:45 +0000 | [diff] [blame] | 2036 | pj_pool_release(pool); |
Benny Prijono | eebe9af | 2006-06-13 22:57:13 +0000 | [diff] [blame] | 2037 | return status; |
| 2038 | } |
| 2039 | |
| 2040 | pjsua_var.recorder[file_id].port = port; |
| 2041 | pjsua_var.recorder[file_id].slot = slot; |
Benny Prijono | d5696da | 2007-07-17 16:25:45 +0000 | [diff] [blame] | 2042 | pjsua_var.recorder[file_id].pool = pool; |
Benny Prijono | eebe9af | 2006-06-13 22:57:13 +0000 | [diff] [blame] | 2043 | |
| 2044 | if (p_id) *p_id = file_id; |
| 2045 | |
| 2046 | ++pjsua_var.rec_cnt; |
| 2047 | |
| 2048 | PJSUA_UNLOCK(); |
| 2049 | return PJ_SUCCESS; |
| 2050 | } |
| 2051 | |
| 2052 | |
| 2053 | /* |
| 2054 | * Get conference port associated with recorder. |
| 2055 | */ |
| 2056 | PJ_DEF(pjsua_conf_port_id) pjsua_recorder_get_conf_port(pjsua_recorder_id id) |
| 2057 | { |
Benny Prijono | a1e6968 | 2007-05-11 15:14:34 +0000 | [diff] [blame] | 2058 | PJ_ASSERT_RETURN(id>=0 && id<(int)PJ_ARRAY_SIZE(pjsua_var.recorder), |
| 2059 | PJ_EINVAL); |
Benny Prijono | eebe9af | 2006-06-13 22:57:13 +0000 | [diff] [blame] | 2060 | PJ_ASSERT_RETURN(pjsua_var.recorder[id].port != NULL, PJ_EINVAL); |
| 2061 | |
| 2062 | return pjsua_var.recorder[id].slot; |
| 2063 | } |
| 2064 | |
Benny Prijono | 469b152 | 2006-12-26 03:05:17 +0000 | [diff] [blame] | 2065 | /* |
| 2066 | * Get the media port for the recorder. |
| 2067 | */ |
| 2068 | PJ_DEF(pj_status_t) pjsua_recorder_get_port( pjsua_recorder_id id, |
| 2069 | pjmedia_port **p_port) |
| 2070 | { |
Benny Prijono | a1e6968 | 2007-05-11 15:14:34 +0000 | [diff] [blame] | 2071 | PJ_ASSERT_RETURN(id>=0 && id<(int)PJ_ARRAY_SIZE(pjsua_var.recorder), |
| 2072 | PJ_EINVAL); |
Benny Prijono | 469b152 | 2006-12-26 03:05:17 +0000 | [diff] [blame] | 2073 | PJ_ASSERT_RETURN(pjsua_var.recorder[id].port != NULL, PJ_EINVAL); |
| 2074 | PJ_ASSERT_RETURN(p_port != NULL, PJ_EINVAL); |
| 2075 | |
| 2076 | *p_port = pjsua_var.recorder[id].port; |
| 2077 | return PJ_SUCCESS; |
| 2078 | } |
Benny Prijono | eebe9af | 2006-06-13 22:57:13 +0000 | [diff] [blame] | 2079 | |
| 2080 | /* |
| 2081 | * Destroy recorder (this will complete recording). |
| 2082 | */ |
| 2083 | PJ_DEF(pj_status_t) pjsua_recorder_destroy(pjsua_recorder_id id) |
| 2084 | { |
Benny Prijono | a1e6968 | 2007-05-11 15:14:34 +0000 | [diff] [blame] | 2085 | PJ_ASSERT_RETURN(id>=0 && id<(int)PJ_ARRAY_SIZE(pjsua_var.recorder), |
| 2086 | PJ_EINVAL); |
Benny Prijono | eebe9af | 2006-06-13 22:57:13 +0000 | [diff] [blame] | 2087 | PJ_ASSERT_RETURN(pjsua_var.recorder[id].port != NULL, PJ_EINVAL); |
| 2088 | |
| 2089 | PJSUA_LOCK(); |
| 2090 | |
| 2091 | if (pjsua_var.recorder[id].port) { |
Nanang Izzuddin | 148fd39 | 2008-06-16 09:52:50 +0000 | [diff] [blame] | 2092 | pjsua_conf_remove_port(pjsua_var.recorder[id].slot); |
Benny Prijono | eebe9af | 2006-06-13 22:57:13 +0000 | [diff] [blame] | 2093 | pjmedia_port_destroy(pjsua_var.recorder[id].port); |
| 2094 | pjsua_var.recorder[id].port = NULL; |
| 2095 | pjsua_var.recorder[id].slot = 0xFFFF; |
Benny Prijono | d5696da | 2007-07-17 16:25:45 +0000 | [diff] [blame] | 2096 | pj_pool_release(pjsua_var.recorder[id].pool); |
| 2097 | pjsua_var.recorder[id].pool = NULL; |
Benny Prijono | eebe9af | 2006-06-13 22:57:13 +0000 | [diff] [blame] | 2098 | pjsua_var.rec_cnt--; |
| 2099 | } |
| 2100 | |
| 2101 | PJSUA_UNLOCK(); |
| 2102 | |
| 2103 | return PJ_SUCCESS; |
| 2104 | } |
| 2105 | |
| 2106 | |
| 2107 | /***************************************************************************** |
| 2108 | * Sound devices. |
| 2109 | */ |
| 2110 | |
| 2111 | /* |
| 2112 | * Enum sound devices. |
| 2113 | */ |
| 2114 | PJ_DEF(pj_status_t) pjsua_enum_snd_devs( pjmedia_snd_dev_info info[], |
| 2115 | unsigned *count) |
| 2116 | { |
| 2117 | unsigned i, dev_count; |
| 2118 | |
| 2119 | dev_count = pjmedia_snd_get_dev_count(); |
| 2120 | |
| 2121 | if (dev_count > *count) dev_count = *count; |
| 2122 | |
| 2123 | for (i=0; i<dev_count; ++i) { |
| 2124 | const pjmedia_snd_dev_info *ci; |
| 2125 | |
| 2126 | ci = pjmedia_snd_get_dev_info(i); |
| 2127 | pj_memcpy(&info[i], ci, sizeof(*ci)); |
| 2128 | } |
| 2129 | |
| 2130 | *count = dev_count; |
| 2131 | |
| 2132 | return PJ_SUCCESS; |
| 2133 | } |
| 2134 | |
| 2135 | |
| 2136 | /* Close existing sound device */ |
| 2137 | static void close_snd_dev(void) |
| 2138 | { |
| 2139 | /* Close sound device */ |
| 2140 | if (pjsua_var.snd_port) { |
| 2141 | const pjmedia_snd_dev_info *cap_info, *play_info; |
| 2142 | |
| 2143 | cap_info = pjmedia_snd_get_dev_info(pjsua_var.cap_dev); |
| 2144 | play_info = pjmedia_snd_get_dev_info(pjsua_var.play_dev); |
| 2145 | |
| 2146 | PJ_LOG(4,(THIS_FILE, "Closing %s sound playback device and " |
| 2147 | "%s sound capture device", |
| 2148 | play_info->name, cap_info->name)); |
| 2149 | |
| 2150 | pjmedia_snd_port_disconnect(pjsua_var.snd_port); |
| 2151 | pjmedia_snd_port_destroy(pjsua_var.snd_port); |
| 2152 | pjsua_var.snd_port = NULL; |
| 2153 | } |
| 2154 | |
| 2155 | /* Close null sound device */ |
| 2156 | if (pjsua_var.null_snd) { |
| 2157 | PJ_LOG(4,(THIS_FILE, "Closing null sound device..")); |
| 2158 | pjmedia_master_port_destroy(pjsua_var.null_snd, PJ_FALSE); |
| 2159 | pjsua_var.null_snd = NULL; |
| 2160 | } |
Nanang Izzuddin | 68559c3 | 2008-06-13 17:01:46 +0000 | [diff] [blame] | 2161 | |
| 2162 | if (pjsua_var.snd_pool) |
| 2163 | pj_pool_release(pjsua_var.snd_pool); |
| 2164 | pjsua_var.snd_pool = NULL; |
Benny Prijono | eebe9af | 2006-06-13 22:57:13 +0000 | [diff] [blame] | 2165 | } |
| 2166 | |
| 2167 | /* |
| 2168 | * Select or change sound device. Application may call this function at |
| 2169 | * any time to replace current sound device. |
| 2170 | */ |
| 2171 | PJ_DEF(pj_status_t) pjsua_set_snd_dev( int capture_dev, |
| 2172 | int playback_dev) |
| 2173 | { |
| 2174 | pjmedia_port *conf_port; |
Benny Prijono | 6dd967c | 2006-12-26 02:27:14 +0000 | [diff] [blame] | 2175 | const pjmedia_snd_dev_info *play_info; |
Nanang Izzuddin | 6aa4495 | 2008-10-07 12:42:24 +0000 | [diff] [blame] | 2176 | unsigned clock_rates[] = {0, 44100, 48000, 32000, 16000, 8000}; |
Benny Prijono | 658a1c5 | 2006-10-11 21:56:16 +0000 | [diff] [blame] | 2177 | unsigned selected_clock_rate = 0; |
Benny Prijono | 26056d8 | 2006-10-11 16:03:41 +0000 | [diff] [blame] | 2178 | unsigned i; |
Benny Prijono | c53c6d7 | 2006-11-27 09:54:03 +0000 | [diff] [blame] | 2179 | pjmedia_snd_stream *strm; |
| 2180 | pjmedia_snd_stream_info si; |
| 2181 | pj_str_t tmp; |
Benny Prijono | 26056d8 | 2006-10-11 16:03:41 +0000 | [diff] [blame] | 2182 | pj_status_t status = -1; |
Benny Prijono | eebe9af | 2006-06-13 22:57:13 +0000 | [diff] [blame] | 2183 | |
Nanang Izzuddin | 68559c3 | 2008-06-13 17:01:46 +0000 | [diff] [blame] | 2184 | /* Check if NULL sound device is used */ |
| 2185 | if (NULL_SND_DEV_ID == capture_dev || NULL_SND_DEV_ID == playback_dev) { |
| 2186 | return pjsua_set_null_snd_dev(); |
| 2187 | } |
| 2188 | |
Benny Prijono | eebe9af | 2006-06-13 22:57:13 +0000 | [diff] [blame] | 2189 | /* Close existing sound port */ |
| 2190 | close_snd_dev(); |
| 2191 | |
Nanang Izzuddin | 68559c3 | 2008-06-13 17:01:46 +0000 | [diff] [blame] | 2192 | /* Create memory pool for sound device. */ |
| 2193 | pjsua_var.snd_pool = pjsua_pool_create("pjsua_snd", 4000, 4000); |
| 2194 | PJ_ASSERT_RETURN(pjsua_var.snd_pool, PJ_ENOMEM); |
Benny Prijono | eebe9af | 2006-06-13 22:57:13 +0000 | [diff] [blame] | 2195 | |
Benny Prijono | 26056d8 | 2006-10-11 16:03:41 +0000 | [diff] [blame] | 2196 | /* Set default clock rate */ |
Benny Prijono | f3758ee | 2008-02-26 15:32:16 +0000 | [diff] [blame] | 2197 | clock_rates[0] = pjsua_var.media_cfg.snd_clock_rate; |
| 2198 | if (clock_rates[0] == 0) |
| 2199 | clock_rates[0] = pjsua_var.media_cfg.clock_rate; |
Benny Prijono | eebe9af | 2006-06-13 22:57:13 +0000 | [diff] [blame] | 2200 | |
Benny Prijono | 50f19b3 | 2008-03-11 13:15:43 +0000 | [diff] [blame] | 2201 | /* Get the port0 of the conference bridge. */ |
| 2202 | conf_port = pjmedia_conf_get_master_port(pjsua_var.mconf); |
| 2203 | pj_assert(conf_port != NULL); |
| 2204 | |
Benny Prijono | 26056d8 | 2006-10-11 16:03:41 +0000 | [diff] [blame] | 2205 | /* Attempts to open the sound device with different clock rates */ |
| 2206 | for (i=0; i<PJ_ARRAY_SIZE(clock_rates); ++i) { |
| 2207 | char errmsg[PJ_ERR_MSG_SIZE]; |
Nanang Izzuddin | 3c1ae63 | 2008-08-21 15:04:20 +0000 | [diff] [blame] | 2208 | unsigned samples_per_frame; |
Benny Prijono | 26056d8 | 2006-10-11 16:03:41 +0000 | [diff] [blame] | 2209 | |
| 2210 | PJ_LOG(4,(THIS_FILE, |
| 2211 | "pjsua_set_snd_dev(): attempting to open devices " |
| 2212 | "@%d Hz", clock_rates[i])); |
| 2213 | |
Nanang Izzuddin | 3c1ae63 | 2008-08-21 15:04:20 +0000 | [diff] [blame] | 2214 | samples_per_frame = clock_rates[i] * |
| 2215 | pjsua_var.media_cfg.audio_frame_ptime * |
| 2216 | pjsua_var.media_cfg.channel_count / 1000; |
| 2217 | |
Benny Prijono | 26056d8 | 2006-10-11 16:03:41 +0000 | [diff] [blame] | 2218 | /* Create the sound device. Sound port will start immediately. */ |
Nanang Izzuddin | 68559c3 | 2008-06-13 17:01:46 +0000 | [diff] [blame] | 2219 | status = pjmedia_snd_port_create(pjsua_var.snd_pool, capture_dev, |
Benny Prijono | 26056d8 | 2006-10-11 16:03:41 +0000 | [diff] [blame] | 2220 | playback_dev, |
Benny Prijono | 7d60d05 | 2008-03-29 12:24:20 +0000 | [diff] [blame] | 2221 | clock_rates[i], |
| 2222 | pjsua_var.media_cfg.channel_count, |
Nanang Izzuddin | 3c1ae63 | 2008-08-21 15:04:20 +0000 | [diff] [blame] | 2223 | samples_per_frame, |
Benny Prijono | 26056d8 | 2006-10-11 16:03:41 +0000 | [diff] [blame] | 2224 | 16, 0, &pjsua_var.snd_port); |
| 2225 | |
Benny Prijono | 658a1c5 | 2006-10-11 21:56:16 +0000 | [diff] [blame] | 2226 | if (status == PJ_SUCCESS) { |
| 2227 | selected_clock_rate = clock_rates[i]; |
Benny Prijono | 50f19b3 | 2008-03-11 13:15:43 +0000 | [diff] [blame] | 2228 | |
| 2229 | /* If there's mismatch between sound port and conference's port, |
| 2230 | * create a resample port to bridge them. |
| 2231 | */ |
| 2232 | if (selected_clock_rate != pjsua_var.media_cfg.clock_rate) { |
| 2233 | pjmedia_port *resample_port; |
| 2234 | unsigned resample_opt = 0; |
| 2235 | |
| 2236 | if (pjsua_var.media_cfg.quality >= 3 && |
| 2237 | pjsua_var.media_cfg.quality <= 4) |
| 2238 | { |
Nanang Izzuddin | e85a183 | 2008-05-29 11:03:23 +0000 | [diff] [blame] | 2239 | resample_opt |= PJMEDIA_RESAMPLE_USE_SMALL_FILTER; |
Benny Prijono | 50f19b3 | 2008-03-11 13:15:43 +0000 | [diff] [blame] | 2240 | } |
| 2241 | else if (pjsua_var.media_cfg.quality < 3) { |
Nanang Izzuddin | e85a183 | 2008-05-29 11:03:23 +0000 | [diff] [blame] | 2242 | resample_opt |= PJMEDIA_RESAMPLE_USE_LINEAR; |
Benny Prijono | 50f19b3 | 2008-03-11 13:15:43 +0000 | [diff] [blame] | 2243 | } |
| 2244 | |
Nanang Izzuddin | 68559c3 | 2008-06-13 17:01:46 +0000 | [diff] [blame] | 2245 | status = pjmedia_resample_port_create(pjsua_var.snd_pool, |
Benny Prijono | 50f19b3 | 2008-03-11 13:15:43 +0000 | [diff] [blame] | 2246 | conf_port, |
| 2247 | selected_clock_rate, |
| 2248 | resample_opt, |
| 2249 | &resample_port); |
| 2250 | if (status != PJ_SUCCESS) { |
| 2251 | pj_strerror(status, errmsg, sizeof(errmsg)); |
| 2252 | PJ_LOG(4, (THIS_FILE, |
| 2253 | "Error creating resample port, trying next " |
| 2254 | "clock rate", |
| 2255 | errmsg)); |
| 2256 | pjmedia_snd_port_destroy(pjsua_var.snd_port); |
| 2257 | pjsua_var.snd_port = NULL; |
| 2258 | continue; |
| 2259 | } else { |
| 2260 | conf_port = resample_port; |
| 2261 | break; |
| 2262 | } |
| 2263 | |
| 2264 | } else { |
| 2265 | break; |
| 2266 | } |
Benny Prijono | 658a1c5 | 2006-10-11 21:56:16 +0000 | [diff] [blame] | 2267 | } |
Benny Prijono | 26056d8 | 2006-10-11 16:03:41 +0000 | [diff] [blame] | 2268 | |
| 2269 | pj_strerror(status, errmsg, sizeof(errmsg)); |
Benny Prijono | 658a1c5 | 2006-10-11 21:56:16 +0000 | [diff] [blame] | 2270 | PJ_LOG(4, (THIS_FILE, "..failed: %s", errmsg)); |
Benny Prijono | 26056d8 | 2006-10-11 16:03:41 +0000 | [diff] [blame] | 2271 | } |
| 2272 | |
Benny Prijono | eebe9af | 2006-06-13 22:57:13 +0000 | [diff] [blame] | 2273 | if (status != PJ_SUCCESS) { |
| 2274 | pjsua_perror(THIS_FILE, "Unable to open sound device", status); |
| 2275 | return status; |
| 2276 | } |
| 2277 | |
Benny Prijono | f20687a | 2006-08-04 18:27:19 +0000 | [diff] [blame] | 2278 | /* Set AEC */ |
Nanang Izzuddin | 68559c3 | 2008-06-13 17:01:46 +0000 | [diff] [blame] | 2279 | pjmedia_snd_port_set_ec( pjsua_var.snd_port, pjsua_var.snd_pool, |
Benny Prijono | 5da5043 | 2006-08-07 10:24:52 +0000 | [diff] [blame] | 2280 | pjsua_var.media_cfg.ec_tail_len, |
| 2281 | pjsua_var.media_cfg.ec_options); |
Benny Prijono | eebe9af | 2006-06-13 22:57:13 +0000 | [diff] [blame] | 2282 | |
Benny Prijono | 52a9391 | 2006-08-04 20:54:37 +0000 | [diff] [blame] | 2283 | /* Connect sound port to the bridge */ |
| 2284 | status = pjmedia_snd_port_connect(pjsua_var.snd_port, |
| 2285 | conf_port ); |
| 2286 | if (status != PJ_SUCCESS) { |
| 2287 | pjsua_perror(THIS_FILE, "Unable to connect conference port to " |
| 2288 | "sound device", status); |
| 2289 | pjmedia_snd_port_destroy(pjsua_var.snd_port); |
| 2290 | pjsua_var.snd_port = NULL; |
| 2291 | return status; |
| 2292 | } |
| 2293 | |
Benny Prijono | eebe9af | 2006-06-13 22:57:13 +0000 | [diff] [blame] | 2294 | /* Save the device IDs */ |
| 2295 | pjsua_var.cap_dev = capture_dev; |
| 2296 | pjsua_var.play_dev = playback_dev; |
| 2297 | |
Benny Prijono | c53c6d7 | 2006-11-27 09:54:03 +0000 | [diff] [blame] | 2298 | /* Update sound device name. */ |
| 2299 | strm = pjmedia_snd_port_get_snd_stream(pjsua_var.snd_port); |
| 2300 | pjmedia_snd_stream_get_info(strm, &si); |
| 2301 | play_info = pjmedia_snd_get_dev_info(si.rec_id); |
| 2302 | |
Benny Prijono | f3758ee | 2008-02-26 15:32:16 +0000 | [diff] [blame] | 2303 | if (si.clock_rate != pjsua_var.media_cfg.clock_rate) { |
| 2304 | char tmp_buf[128]; |
| 2305 | int tmp_buf_len = sizeof(tmp_buf); |
| 2306 | |
| 2307 | tmp_buf_len = pj_ansi_snprintf(tmp_buf, sizeof(tmp_buf)-1, "%s (%dKHz)", |
| 2308 | play_info->name, si.clock_rate/1000); |
| 2309 | pj_strset(&tmp, tmp_buf, tmp_buf_len); |
| 2310 | pjmedia_conf_set_port0_name(pjsua_var.mconf, &tmp); |
| 2311 | } else { |
| 2312 | pjmedia_conf_set_port0_name(pjsua_var.mconf, |
| 2313 | pj_cstr(&tmp, play_info->name)); |
| 2314 | } |
Benny Prijono | c53c6d7 | 2006-11-27 09:54:03 +0000 | [diff] [blame] | 2315 | |
Benny Prijono | eebe9af | 2006-06-13 22:57:13 +0000 | [diff] [blame] | 2316 | return PJ_SUCCESS; |
| 2317 | } |
| 2318 | |
| 2319 | |
| 2320 | /* |
Benny Prijono | ebdf877 | 2007-02-01 19:25:50 +0000 | [diff] [blame] | 2321 | * Get currently active sound devices. If sound devices has not been created |
| 2322 | * (for example when pjsua_start() is not called), it is possible that |
| 2323 | * the function returns PJ_SUCCESS with -1 as device IDs. |
| 2324 | */ |
| 2325 | PJ_DEF(pj_status_t) pjsua_get_snd_dev(int *capture_dev, |
| 2326 | int *playback_dev) |
| 2327 | { |
| 2328 | if (capture_dev) { |
| 2329 | *capture_dev = pjsua_var.cap_dev; |
| 2330 | } |
| 2331 | if (playback_dev) { |
| 2332 | *playback_dev = pjsua_var.play_dev; |
| 2333 | } |
| 2334 | |
| 2335 | return PJ_SUCCESS; |
| 2336 | } |
| 2337 | |
| 2338 | |
| 2339 | /* |
Benny Prijono | eebe9af | 2006-06-13 22:57:13 +0000 | [diff] [blame] | 2340 | * Use null sound device. |
| 2341 | */ |
| 2342 | PJ_DEF(pj_status_t) pjsua_set_null_snd_dev(void) |
| 2343 | { |
| 2344 | pjmedia_port *conf_port; |
| 2345 | pj_status_t status; |
| 2346 | |
| 2347 | /* Close existing sound device */ |
| 2348 | close_snd_dev(); |
| 2349 | |
Nanang Izzuddin | 68559c3 | 2008-06-13 17:01:46 +0000 | [diff] [blame] | 2350 | /* Create memory pool for sound device. */ |
| 2351 | pjsua_var.snd_pool = pjsua_pool_create("pjsua_snd", 4000, 4000); |
| 2352 | PJ_ASSERT_RETURN(pjsua_var.snd_pool, PJ_ENOMEM); |
| 2353 | |
Benny Prijono | eebe9af | 2006-06-13 22:57:13 +0000 | [diff] [blame] | 2354 | PJ_LOG(4,(THIS_FILE, "Opening null sound device..")); |
| 2355 | |
| 2356 | /* Get the port0 of the conference bridge. */ |
| 2357 | conf_port = pjmedia_conf_get_master_port(pjsua_var.mconf); |
| 2358 | pj_assert(conf_port != NULL); |
| 2359 | |
| 2360 | /* Create master port, connecting port0 of the conference bridge to |
| 2361 | * a null port. |
| 2362 | */ |
Nanang Izzuddin | 68559c3 | 2008-06-13 17:01:46 +0000 | [diff] [blame] | 2363 | status = pjmedia_master_port_create(pjsua_var.snd_pool, pjsua_var.null_port, |
Benny Prijono | eebe9af | 2006-06-13 22:57:13 +0000 | [diff] [blame] | 2364 | conf_port, 0, &pjsua_var.null_snd); |
| 2365 | if (status != PJ_SUCCESS) { |
| 2366 | pjsua_perror(THIS_FILE, "Unable to create null sound device", |
| 2367 | status); |
| 2368 | return status; |
| 2369 | } |
| 2370 | |
| 2371 | /* Start the master port */ |
| 2372 | status = pjmedia_master_port_start(pjsua_var.null_snd); |
| 2373 | PJ_ASSERT_RETURN(status == PJ_SUCCESS, status); |
| 2374 | |
Nanang Izzuddin | 68559c3 | 2008-06-13 17:01:46 +0000 | [diff] [blame] | 2375 | pjsua_var.cap_dev = NULL_SND_DEV_ID; |
| 2376 | pjsua_var.play_dev = NULL_SND_DEV_ID; |
| 2377 | |
Benny Prijono | eebe9af | 2006-06-13 22:57:13 +0000 | [diff] [blame] | 2378 | return PJ_SUCCESS; |
| 2379 | } |
| 2380 | |
| 2381 | |
Benny Prijono | e909eac | 2006-07-27 22:04:56 +0000 | [diff] [blame] | 2382 | |
| 2383 | /* |
| 2384 | * Use no device! |
| 2385 | */ |
| 2386 | PJ_DEF(pjmedia_port*) pjsua_set_no_snd_dev(void) |
| 2387 | { |
| 2388 | /* Close existing sound device */ |
| 2389 | close_snd_dev(); |
| 2390 | |
| 2391 | pjsua_var.no_snd = PJ_TRUE; |
| 2392 | return pjmedia_conf_get_master_port(pjsua_var.mconf); |
| 2393 | } |
| 2394 | |
| 2395 | |
Benny Prijono | f20687a | 2006-08-04 18:27:19 +0000 | [diff] [blame] | 2396 | /* |
| 2397 | * Configure the AEC settings of the sound port. |
| 2398 | */ |
Benny Prijono | 5da5043 | 2006-08-07 10:24:52 +0000 | [diff] [blame] | 2399 | 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] | 2400 | { |
| 2401 | pjsua_var.media_cfg.ec_tail_len = tail_ms; |
| 2402 | |
| 2403 | if (pjsua_var.snd_port) |
Benny Prijono | 5da5043 | 2006-08-07 10:24:52 +0000 | [diff] [blame] | 2404 | return pjmedia_snd_port_set_ec( pjsua_var.snd_port, pjsua_var.pool, |
| 2405 | tail_ms, options); |
Benny Prijono | f20687a | 2006-08-04 18:27:19 +0000 | [diff] [blame] | 2406 | |
| 2407 | return PJ_SUCCESS; |
| 2408 | } |
| 2409 | |
| 2410 | |
| 2411 | /* |
| 2412 | * Get current AEC tail length. |
| 2413 | */ |
Benny Prijono | 22dfe59 | 2006-08-06 12:07:13 +0000 | [diff] [blame] | 2414 | 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] | 2415 | { |
| 2416 | *p_tail_ms = pjsua_var.media_cfg.ec_tail_len; |
| 2417 | return PJ_SUCCESS; |
| 2418 | } |
| 2419 | |
Benny Prijono | e909eac | 2006-07-27 22:04:56 +0000 | [diff] [blame] | 2420 | |
Benny Prijono | eebe9af | 2006-06-13 22:57:13 +0000 | [diff] [blame] | 2421 | /***************************************************************************** |
| 2422 | * Codecs. |
| 2423 | */ |
| 2424 | |
| 2425 | /* |
| 2426 | * Enum all supported codecs in the system. |
| 2427 | */ |
| 2428 | PJ_DEF(pj_status_t) pjsua_enum_codecs( pjsua_codec_info id[], |
| 2429 | unsigned *p_count ) |
| 2430 | { |
| 2431 | pjmedia_codec_mgr *codec_mgr; |
| 2432 | pjmedia_codec_info info[32]; |
| 2433 | unsigned i, count, prio[32]; |
| 2434 | pj_status_t status; |
| 2435 | |
| 2436 | codec_mgr = pjmedia_endpt_get_codec_mgr(pjsua_var.med_endpt); |
| 2437 | count = PJ_ARRAY_SIZE(info); |
| 2438 | status = pjmedia_codec_mgr_enum_codecs( codec_mgr, &count, info, prio); |
| 2439 | if (status != PJ_SUCCESS) { |
| 2440 | *p_count = 0; |
| 2441 | return status; |
| 2442 | } |
| 2443 | |
| 2444 | if (count > *p_count) count = *p_count; |
| 2445 | |
| 2446 | for (i=0; i<count; ++i) { |
| 2447 | pjmedia_codec_info_to_id(&info[i], id[i].buf_, sizeof(id[i].buf_)); |
| 2448 | id[i].codec_id = pj_str(id[i].buf_); |
| 2449 | id[i].priority = (pj_uint8_t) prio[i]; |
| 2450 | } |
| 2451 | |
| 2452 | *p_count = count; |
| 2453 | |
| 2454 | return PJ_SUCCESS; |
| 2455 | } |
| 2456 | |
| 2457 | |
| 2458 | /* |
| 2459 | * Change codec priority. |
| 2460 | */ |
| 2461 | PJ_DEF(pj_status_t) pjsua_codec_set_priority( const pj_str_t *codec_id, |
| 2462 | pj_uint8_t priority ) |
| 2463 | { |
Benny Prijono | 88accae | 2008-06-26 15:48:14 +0000 | [diff] [blame] | 2464 | const pj_str_t all = { NULL, 0 }; |
Benny Prijono | eebe9af | 2006-06-13 22:57:13 +0000 | [diff] [blame] | 2465 | pjmedia_codec_mgr *codec_mgr; |
| 2466 | |
| 2467 | codec_mgr = pjmedia_endpt_get_codec_mgr(pjsua_var.med_endpt); |
| 2468 | |
Benny Prijono | 88accae | 2008-06-26 15:48:14 +0000 | [diff] [blame] | 2469 | if (codec_id->slen==1 && *codec_id->ptr=='*') |
| 2470 | codec_id = &all; |
| 2471 | |
Benny Prijono | eebe9af | 2006-06-13 22:57:13 +0000 | [diff] [blame] | 2472 | return pjmedia_codec_mgr_set_codec_priority(codec_mgr, codec_id, |
| 2473 | priority); |
| 2474 | } |
| 2475 | |
| 2476 | |
| 2477 | /* |
| 2478 | * Get codec parameters. |
| 2479 | */ |
| 2480 | PJ_DEF(pj_status_t) pjsua_codec_get_param( const pj_str_t *codec_id, |
| 2481 | pjmedia_codec_param *param ) |
| 2482 | { |
Benny Prijono | 88accae | 2008-06-26 15:48:14 +0000 | [diff] [blame] | 2483 | const pj_str_t all = { NULL, 0 }; |
Benny Prijono | eebe9af | 2006-06-13 22:57:13 +0000 | [diff] [blame] | 2484 | const pjmedia_codec_info *info; |
| 2485 | pjmedia_codec_mgr *codec_mgr; |
| 2486 | unsigned count = 1; |
| 2487 | pj_status_t status; |
| 2488 | |
| 2489 | codec_mgr = pjmedia_endpt_get_codec_mgr(pjsua_var.med_endpt); |
| 2490 | |
Benny Prijono | 88accae | 2008-06-26 15:48:14 +0000 | [diff] [blame] | 2491 | if (codec_id->slen==1 && *codec_id->ptr=='*') |
| 2492 | codec_id = &all; |
| 2493 | |
Benny Prijono | eebe9af | 2006-06-13 22:57:13 +0000 | [diff] [blame] | 2494 | status = pjmedia_codec_mgr_find_codecs_by_id(codec_mgr, codec_id, |
| 2495 | &count, &info, NULL); |
| 2496 | if (status != PJ_SUCCESS) |
| 2497 | return status; |
| 2498 | |
| 2499 | if (count != 1) |
| 2500 | return PJ_ENOTFOUND; |
| 2501 | |
| 2502 | status = pjmedia_codec_mgr_get_default_param( codec_mgr, info, param); |
| 2503 | return status; |
| 2504 | } |
| 2505 | |
| 2506 | |
| 2507 | /* |
| 2508 | * Set codec parameters. |
| 2509 | */ |
| 2510 | PJ_DEF(pj_status_t) pjsua_codec_set_param( const pj_str_t *id, |
| 2511 | const pjmedia_codec_param *param) |
| 2512 | { |
Benny Prijono | 00cae61 | 2006-07-31 15:19:36 +0000 | [diff] [blame] | 2513 | PJ_UNUSED_ARG(id); |
| 2514 | PJ_UNUSED_ARG(param); |
Benny Prijono | eebe9af | 2006-06-13 22:57:13 +0000 | [diff] [blame] | 2515 | PJ_TODO(set_codec_param); |
| 2516 | return PJ_SUCCESS; |
| 2517 | } |