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