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