Benny Prijono | eebe9af | 2006-06-13 22:57:13 +0000 | [diff] [blame] | 1 | /* $Id$ */ |
| 2 | /* |
Nanang Izzuddin | a62ffc9 | 2011-05-05 06:14:19 +0000 | [diff] [blame] | 3 | * Copyright (C) 2008-2011 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 */ |
Sauw Ming | 98766c7 | 2011-03-11 06:57:24 +0000 | [diff] [blame] | 39 | static pj_status_t open_snd_dev(pjmedia_snd_port_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 | |
Nanang Izzuddin | 50fae73 | 2011-03-22 09:49:23 +0000 | [diff] [blame] | 61 | |
Benny Prijono | eebe9af | 2006-06-13 22:57:13 +0000 | [diff] [blame] | 62 | /** |
| 63 | * Init media subsystems. |
| 64 | */ |
| 65 | pj_status_t pjsua_media_subsys_init(const pjsua_media_config *cfg) |
| 66 | { |
Benny Prijono | ba5926a | 2007-05-02 11:29:37 +0000 | [diff] [blame] | 67 | pj_str_t codec_id = {NULL, 0}; |
Benny Prijono | 0498d90 | 2006-06-19 14:49:14 +0000 | [diff] [blame] | 68 | unsigned opt; |
Benny Prijono | 35fc1eb | 2011-07-15 09:51:46 +0000 | [diff] [blame] | 69 | pjmedia_audio_codec_config codec_cfg; |
Benny Prijono | eebe9af | 2006-06-13 22:57:13 +0000 | [diff] [blame] | 70 | pj_status_t status; |
| 71 | |
Benny Prijono | fc24e69 | 2007-01-27 18:31:51 +0000 | [diff] [blame] | 72 | /* To suppress warning about unused var when all codecs are disabled */ |
| 73 | PJ_UNUSED_ARG(codec_id); |
| 74 | |
Benny Prijono | b90fd38 | 2011-09-18 14:59:56 +0000 | [diff] [blame] | 75 | pj_log_push_indent(); |
| 76 | |
Benny Prijono | f798e50 | 2009-03-09 13:08:16 +0000 | [diff] [blame] | 77 | /* Specify which audio device settings are save-able */ |
| 78 | pjsua_var.aud_svmask = 0xFFFFFFFF; |
| 79 | /* These are not-settable */ |
| 80 | pjsua_var.aud_svmask &= ~(PJMEDIA_AUD_DEV_CAP_EXT_FORMAT | |
| 81 | PJMEDIA_AUD_DEV_CAP_INPUT_SIGNAL_METER | |
| 82 | PJMEDIA_AUD_DEV_CAP_OUTPUT_SIGNAL_METER); |
Benny Prijono | e506c8c | 2009-03-10 13:28:43 +0000 | [diff] [blame] | 83 | /* EC settings use different API */ |
| 84 | pjsua_var.aud_svmask &= ~(PJMEDIA_AUD_DEV_CAP_EC | |
| 85 | PJMEDIA_AUD_DEV_CAP_EC_TAIL); |
Benny Prijono | f798e50 | 2009-03-09 13:08:16 +0000 | [diff] [blame] | 86 | |
Benny Prijono | eebe9af | 2006-06-13 22:57:13 +0000 | [diff] [blame] | 87 | /* Copy configuration */ |
Benny Prijono | f76e139 | 2008-06-06 14:51:48 +0000 | [diff] [blame] | 88 | pjsua_media_config_dup(pjsua_var.pool, &pjsua_var.media_cfg, cfg); |
Benny Prijono | eebe9af | 2006-06-13 22:57:13 +0000 | [diff] [blame] | 89 | |
| 90 | /* Normalize configuration */ |
Benny Prijono | 50f19b3 | 2008-03-11 13:15:43 +0000 | [diff] [blame] | 91 | if (pjsua_var.media_cfg.snd_clock_rate == 0) { |
| 92 | pjsua_var.media_cfg.snd_clock_rate = pjsua_var.media_cfg.clock_rate; |
| 93 | } |
Benny Prijono | eebe9af | 2006-06-13 22:57:13 +0000 | [diff] [blame] | 94 | |
| 95 | if (pjsua_var.media_cfg.has_ioqueue && |
| 96 | pjsua_var.media_cfg.thread_cnt == 0) |
| 97 | { |
| 98 | pjsua_var.media_cfg.thread_cnt = 1; |
| 99 | } |
| 100 | |
| 101 | if (pjsua_var.media_cfg.max_media_ports < pjsua_var.ua_cfg.max_calls) { |
| 102 | pjsua_var.media_cfg.max_media_ports = pjsua_var.ua_cfg.max_calls + 2; |
| 103 | } |
| 104 | |
| 105 | /* Create media endpoint. */ |
| 106 | status = pjmedia_endpt_create(&pjsua_var.cp.factory, |
| 107 | pjsua_var.media_cfg.has_ioqueue? NULL : |
| 108 | pjsip_endpt_get_ioqueue(pjsua_var.endpt), |
| 109 | pjsua_var.media_cfg.thread_cnt, |
| 110 | &pjsua_var.med_endpt); |
| 111 | if (status != PJ_SUCCESS) { |
| 112 | pjsua_perror(THIS_FILE, |
| 113 | "Media stack initialization has returned error", |
| 114 | status); |
Benny Prijono | b90fd38 | 2011-09-18 14:59:56 +0000 | [diff] [blame] | 115 | goto on_error; |
Benny Prijono | eebe9af | 2006-06-13 22:57:13 +0000 | [diff] [blame] | 116 | } |
| 117 | |
Benny Prijono | 35fc1eb | 2011-07-15 09:51:46 +0000 | [diff] [blame] | 118 | /* |
| 119 | * Register all codecs |
| 120 | */ |
| 121 | pjmedia_audio_codec_config_default(&codec_cfg); |
| 122 | codec_cfg.speex.quality = pjsua_var.media_cfg.quality; |
| 123 | codec_cfg.speex.complexity = -1; |
| 124 | codec_cfg.ilbc.mode = pjsua_var.media_cfg.ilbc_mode; |
Nanang Izzuddin | 493a8db | 2008-08-15 13:17:39 +0000 | [diff] [blame] | 125 | |
Nanang Izzuddin | 81db8c7 | 2009-02-05 10:59:14 +0000 | [diff] [blame] | 126 | #if PJMEDIA_HAS_PASSTHROUGH_CODECS |
| 127 | /* Register passthrough codecs */ |
Nanang Izzuddin | abf58db | 2009-06-30 15:02:06 +0000 | [diff] [blame] | 128 | { |
| 129 | unsigned aud_idx; |
| 130 | unsigned ext_fmt_cnt = 0; |
| 131 | pjmedia_format ext_fmts[32]; |
Nanang Izzuddin | abf58db | 2009-06-30 15:02:06 +0000 | [diff] [blame] | 132 | |
| 133 | /* List extended formats supported by audio devices */ |
| 134 | for (aud_idx = 0; aud_idx < pjmedia_aud_dev_count(); ++aud_idx) { |
| 135 | pjmedia_aud_dev_info aud_info; |
| 136 | unsigned i; |
| 137 | |
| 138 | status = pjmedia_aud_dev_get_info(aud_idx, &aud_info); |
| 139 | if (status != PJ_SUCCESS) { |
| 140 | pjsua_perror(THIS_FILE, "Error querying audio device info", |
| 141 | status); |
Benny Prijono | b90fd38 | 2011-09-18 14:59:56 +0000 | [diff] [blame] | 142 | goto on_error; |
Nanang Izzuddin | abf58db | 2009-06-30 15:02:06 +0000 | [diff] [blame] | 143 | } |
| 144 | |
| 145 | /* Collect extended formats supported by this audio device */ |
| 146 | for (i = 0; i < aud_info.ext_fmt_cnt; ++i) { |
| 147 | unsigned j; |
| 148 | pj_bool_t is_listed = PJ_FALSE; |
| 149 | |
| 150 | /* See if this extended format is already in the list */ |
| 151 | for (j = 0; j < ext_fmt_cnt && !is_listed; ++j) { |
| 152 | if (ext_fmts[j].id == aud_info.ext_fmt[i].id && |
| 153 | ext_fmts[j].bitrate == aud_info.ext_fmt[i].bitrate) |
| 154 | { |
| 155 | is_listed = PJ_TRUE; |
| 156 | } |
| 157 | } |
| 158 | |
| 159 | /* Put this format into the list, if it is not in the list */ |
| 160 | if (!is_listed) |
| 161 | ext_fmts[ext_fmt_cnt++] = aud_info.ext_fmt[i]; |
| 162 | |
| 163 | pj_assert(ext_fmt_cnt <= PJ_ARRAY_SIZE(ext_fmts)); |
| 164 | } |
| 165 | } |
| 166 | |
| 167 | /* Init the passthrough codec with supported formats only */ |
Benny Prijono | 35fc1eb | 2011-07-15 09:51:46 +0000 | [diff] [blame] | 168 | codec_cfg.passthrough.setting.fmt_cnt = ext_fmt_cnt; |
| 169 | codec_cfg.passthrough.setting.fmts = ext_fmts; |
| 170 | codec_cfg.passthrough.setting.ilbc_mode = cfg->ilbc_mode; |
Nanang Izzuddin | 81db8c7 | 2009-02-05 10:59:14 +0000 | [diff] [blame] | 171 | } |
| 172 | #endif /* PJMEDIA_HAS_PASSTHROUGH_CODECS */ |
| 173 | |
Benny Prijono | 35fc1eb | 2011-07-15 09:51:46 +0000 | [diff] [blame] | 174 | /* Register all codecs */ |
| 175 | status = pjmedia_codec_register_audio_codecs(pjsua_var.med_endpt, |
| 176 | &codec_cfg); |
Nanang Izzuddin | 57b8857 | 2009-04-01 12:05:34 +0000 | [diff] [blame] | 177 | if (status != PJ_SUCCESS) { |
Benny Prijono | 35fc1eb | 2011-07-15 09:51:46 +0000 | [diff] [blame] | 178 | PJ_PERROR(1,(THIS_FILE, status, "Error registering codecs")); |
Benny Prijono | b90fd38 | 2011-09-18 14:59:56 +0000 | [diff] [blame] | 179 | goto on_error; |
Nanang Izzuddin | 57b8857 | 2009-04-01 12:05:34 +0000 | [diff] [blame] | 180 | } |
Nanang Izzuddin | 57b8857 | 2009-04-01 12:05:34 +0000 | [diff] [blame] | 181 | |
Benny Prijono | 35fc1eb | 2011-07-15 09:51:46 +0000 | [diff] [blame] | 182 | /* Set speex/16000 to higher priority*/ |
| 183 | codec_id = pj_str("speex/16000"); |
| 184 | pjmedia_codec_mgr_set_codec_priority( |
| 185 | pjmedia_endpt_get_codec_mgr(pjsua_var.med_endpt), |
| 186 | &codec_id, PJMEDIA_CODEC_PRIO_NORMAL+2); |
| 187 | |
| 188 | /* Set speex/8000 to next higher priority*/ |
| 189 | codec_id = pj_str("speex/8000"); |
| 190 | pjmedia_codec_mgr_set_codec_priority( |
| 191 | pjmedia_endpt_get_codec_mgr(pjsua_var.med_endpt), |
| 192 | &codec_id, PJMEDIA_CODEC_PRIO_NORMAL+1); |
Benny Prijono | eebe9af | 2006-06-13 22:57:13 +0000 | [diff] [blame] | 193 | |
| 194 | /* Disable ALL L16 codecs */ |
| 195 | codec_id = pj_str("L16"); |
| 196 | pjmedia_codec_mgr_set_codec_priority( |
| 197 | pjmedia_endpt_get_codec_mgr(pjsua_var.med_endpt), |
| 198 | &codec_id, PJMEDIA_CODEC_PRIO_DISABLED); |
| 199 | |
Benny Prijono | eebe9af | 2006-06-13 22:57:13 +0000 | [diff] [blame] | 200 | |
| 201 | /* Save additional conference bridge parameters for future |
| 202 | * reference. |
| 203 | */ |
Benny Prijono | 7d60d05 | 2008-03-29 12:24:20 +0000 | [diff] [blame] | 204 | pjsua_var.mconf_cfg.channel_count = pjsua_var.media_cfg.channel_count; |
Benny Prijono | eebe9af | 2006-06-13 22:57:13 +0000 | [diff] [blame] | 205 | pjsua_var.mconf_cfg.bits_per_sample = 16; |
Benny Prijono | 6e7c5ad | 2008-03-13 10:15:16 +0000 | [diff] [blame] | 206 | pjsua_var.mconf_cfg.samples_per_frame = pjsua_var.media_cfg.clock_rate * |
| 207 | pjsua_var.mconf_cfg.channel_count * |
| 208 | pjsua_var.media_cfg.audio_frame_ptime / |
| 209 | 1000; |
Benny Prijono | eebe9af | 2006-06-13 22:57:13 +0000 | [diff] [blame] | 210 | |
Benny Prijono | 0498d90 | 2006-06-19 14:49:14 +0000 | [diff] [blame] | 211 | /* Init options for conference bridge. */ |
| 212 | opt = PJMEDIA_CONF_NO_DEVICE; |
| 213 | if (pjsua_var.media_cfg.quality >= 3 && |
Benny Prijono | 7ca96da | 2006-08-07 12:11:40 +0000 | [diff] [blame] | 214 | pjsua_var.media_cfg.quality <= 4) |
Benny Prijono | 0498d90 | 2006-06-19 14:49:14 +0000 | [diff] [blame] | 215 | { |
| 216 | opt |= PJMEDIA_CONF_SMALL_FILTER; |
| 217 | } |
| 218 | else if (pjsua_var.media_cfg.quality < 3) { |
| 219 | opt |= PJMEDIA_CONF_USE_LINEAR; |
| 220 | } |
| 221 | |
Benny Prijono | eebe9af | 2006-06-13 22:57:13 +0000 | [diff] [blame] | 222 | /* Init conference bridge. */ |
| 223 | status = pjmedia_conf_create(pjsua_var.pool, |
| 224 | pjsua_var.media_cfg.max_media_ports, |
| 225 | pjsua_var.media_cfg.clock_rate, |
| 226 | pjsua_var.mconf_cfg.channel_count, |
| 227 | pjsua_var.mconf_cfg.samples_per_frame, |
| 228 | pjsua_var.mconf_cfg.bits_per_sample, |
Benny Prijono | 0498d90 | 2006-06-19 14:49:14 +0000 | [diff] [blame] | 229 | opt, &pjsua_var.mconf); |
Benny Prijono | eebe9af | 2006-06-13 22:57:13 +0000 | [diff] [blame] | 230 | if (status != PJ_SUCCESS) { |
Benny Prijono | 50f19b3 | 2008-03-11 13:15:43 +0000 | [diff] [blame] | 231 | pjsua_perror(THIS_FILE, "Error creating conference bridge", |
Benny Prijono | eebe9af | 2006-06-13 22:57:13 +0000 | [diff] [blame] | 232 | status); |
Benny Prijono | b90fd38 | 2011-09-18 14:59:56 +0000 | [diff] [blame] | 233 | goto on_error; |
Benny Prijono | eebe9af | 2006-06-13 22:57:13 +0000 | [diff] [blame] | 234 | } |
| 235 | |
Benny Prijono | f798e50 | 2009-03-09 13:08:16 +0000 | [diff] [blame] | 236 | /* Are we using the audio switchboard (a.k.a APS-Direct)? */ |
| 237 | pjsua_var.is_mswitch = pjmedia_conf_get_master_port(pjsua_var.mconf) |
| 238 | ->info.signature == PJMEDIA_CONF_SWITCH_SIGNATURE; |
| 239 | |
Benny Prijono | eebe9af | 2006-06-13 22:57:13 +0000 | [diff] [blame] | 240 | /* Create null port just in case user wants to use null sound. */ |
| 241 | status = pjmedia_null_port_create(pjsua_var.pool, |
| 242 | pjsua_var.media_cfg.clock_rate, |
| 243 | pjsua_var.mconf_cfg.channel_count, |
| 244 | pjsua_var.mconf_cfg.samples_per_frame, |
| 245 | pjsua_var.mconf_cfg.bits_per_sample, |
| 246 | &pjsua_var.null_port); |
| 247 | PJ_ASSERT_RETURN(status == PJ_SUCCESS, status); |
| 248 | |
Nanang Izzuddin | 69b69ae | 2009-04-14 15:18:30 +0000 | [diff] [blame] | 249 | #if defined(PJMEDIA_HAS_SRTP) && (PJMEDIA_HAS_SRTP != 0) |
| 250 | /* Initialize SRTP library. */ |
| 251 | status = pjmedia_srtp_init_lib(); |
| 252 | if (status != PJ_SUCCESS) { |
| 253 | pjsua_perror(THIS_FILE, "Error initializing SRTP library", |
| 254 | status); |
Benny Prijono | b90fd38 | 2011-09-18 14:59:56 +0000 | [diff] [blame] | 255 | goto on_error; |
Nanang Izzuddin | 69b69ae | 2009-04-14 15:18:30 +0000 | [diff] [blame] | 256 | } |
| 257 | #endif |
| 258 | |
Benny Prijono | 9f468d1 | 2011-07-07 07:46:33 +0000 | [diff] [blame] | 259 | /* Video */ |
| 260 | #if PJMEDIA_HAS_VIDEO |
| 261 | status = pjsua_vid_subsys_init(); |
| 262 | if (status != PJ_SUCCESS) |
Benny Prijono | b90fd38 | 2011-09-18 14:59:56 +0000 | [diff] [blame] | 263 | goto on_error; |
Benny Prijono | 9f468d1 | 2011-07-07 07:46:33 +0000 | [diff] [blame] | 264 | #endif |
| 265 | |
Benny Prijono | b90fd38 | 2011-09-18 14:59:56 +0000 | [diff] [blame] | 266 | pj_log_pop_indent(); |
Benny Prijono | eebe9af | 2006-06-13 22:57:13 +0000 | [diff] [blame] | 267 | return PJ_SUCCESS; |
Benny Prijono | b90fd38 | 2011-09-18 14:59:56 +0000 | [diff] [blame] | 268 | |
| 269 | on_error: |
| 270 | pj_log_pop_indent(); |
| 271 | return status; |
Benny Prijono | eebe9af | 2006-06-13 22:57:13 +0000 | [diff] [blame] | 272 | } |
| 273 | |
| 274 | |
Nanang Izzuddin | 148fd39 | 2008-06-16 09:52:50 +0000 | [diff] [blame] | 275 | /* Check if sound device is idle. */ |
| 276 | static void check_snd_dev_idle() |
| 277 | { |
Nanang Izzuddin | 7082b26 | 2009-06-12 11:15:08 +0000 | [diff] [blame] | 278 | unsigned call_cnt; |
| 279 | |
| 280 | /* Get the call count, we shouldn't close the sound device when there is |
| 281 | * any calls active. |
| 282 | */ |
| 283 | call_cnt = pjsua_call_get_count(); |
| 284 | |
| 285 | /* When this function is called from pjsua_media_channel_deinit() upon |
| 286 | * disconnecting call, actually the call count hasn't been updated/ |
| 287 | * decreased. So we put additional check here, if there is only one |
| 288 | * call and it's in DISCONNECTED state, there is actually no active |
| 289 | * call. |
| 290 | */ |
| 291 | if (call_cnt == 1) { |
| 292 | pjsua_call_id call_id; |
| 293 | pj_status_t status; |
| 294 | |
| 295 | status = pjsua_enum_calls(&call_id, &call_cnt); |
| 296 | if (status == PJ_SUCCESS && call_cnt > 0 && |
| 297 | !pjsua_call_is_active(call_id)) |
| 298 | { |
| 299 | call_cnt = 0; |
| 300 | } |
| 301 | } |
Nanang Izzuddin | 148fd39 | 2008-06-16 09:52:50 +0000 | [diff] [blame] | 302 | |
| 303 | /* Activate sound device auto-close timer if sound device is idle. |
Nanang Izzuddin | 7082b26 | 2009-06-12 11:15:08 +0000 | [diff] [blame] | 304 | * It is idle when there is no port connection in the bridge and |
| 305 | * there is no active call. |
Benny Prijono | 2d64772 | 2011-07-13 03:05:22 +0000 | [diff] [blame] | 306 | * |
| 307 | * Note: this block is now valid if no snd dev is used because of #1299 |
Nanang Izzuddin | 148fd39 | 2008-06-16 09:52:50 +0000 | [diff] [blame] | 308 | */ |
Benny Prijono | 2d64772 | 2011-07-13 03:05:22 +0000 | [diff] [blame] | 309 | if ((pjsua_var.snd_port!=NULL || pjsua_var.null_snd!=NULL || |
| 310 | pjsua_var.no_snd) && |
Nanang Izzuddin | 148fd39 | 2008-06-16 09:52:50 +0000 | [diff] [blame] | 311 | pjsua_var.snd_idle_timer.id == PJ_FALSE && |
| 312 | pjmedia_conf_get_connect_count(pjsua_var.mconf) == 0 && |
Nanang Izzuddin | 7082b26 | 2009-06-12 11:15:08 +0000 | [diff] [blame] | 313 | call_cnt == 0 && |
Nanang Izzuddin | 148fd39 | 2008-06-16 09:52:50 +0000 | [diff] [blame] | 314 | pjsua_var.media_cfg.snd_auto_close_time >= 0) |
| 315 | { |
| 316 | pj_time_val delay; |
| 317 | |
| 318 | delay.msec = 0; |
| 319 | delay.sec = pjsua_var.media_cfg.snd_auto_close_time; |
| 320 | |
| 321 | pjsua_var.snd_idle_timer.id = PJ_TRUE; |
Benny Prijono | 0bc99a9 | 2011-03-17 04:34:43 +0000 | [diff] [blame] | 322 | pjsip_endpt_schedule_timer(pjsua_var.endpt, &pjsua_var.snd_idle_timer, |
Nanang Izzuddin | 148fd39 | 2008-06-16 09:52:50 +0000 | [diff] [blame] | 323 | &delay); |
| 324 | } |
| 325 | } |
| 326 | |
| 327 | |
Nanang Izzuddin | 68559c3 | 2008-06-13 17:01:46 +0000 | [diff] [blame] | 328 | /* Timer callback to close sound device */ |
| 329 | static void close_snd_timer_cb( pj_timer_heap_t *th, |
| 330 | pj_timer_entry *entry) |
| 331 | { |
| 332 | PJ_UNUSED_ARG(th); |
| 333 | |
Benny Prijono | 0f711b4 | 2009-05-06 19:08:43 +0000 | [diff] [blame] | 334 | PJSUA_LOCK(); |
| 335 | if (entry->id) { |
Benny Prijono | 0bc99a9 | 2011-03-17 04:34:43 +0000 | [diff] [blame] | 336 | PJ_LOG(4,(THIS_FILE,"Closing sound device after idle for %d seconds", |
Benny Prijono | 0f711b4 | 2009-05-06 19:08:43 +0000 | [diff] [blame] | 337 | pjsua_var.media_cfg.snd_auto_close_time)); |
Nanang Izzuddin | 68559c3 | 2008-06-13 17:01:46 +0000 | [diff] [blame] | 338 | |
Benny Prijono | 0f711b4 | 2009-05-06 19:08:43 +0000 | [diff] [blame] | 339 | entry->id = PJ_FALSE; |
Nanang Izzuddin | 68559c3 | 2008-06-13 17:01:46 +0000 | [diff] [blame] | 340 | |
Benny Prijono | 0f711b4 | 2009-05-06 19:08:43 +0000 | [diff] [blame] | 341 | close_snd_dev(); |
| 342 | } |
| 343 | PJSUA_UNLOCK(); |
Nanang Izzuddin | 68559c3 | 2008-06-13 17:01:46 +0000 | [diff] [blame] | 344 | } |
| 345 | |
Benny Prijono | eebe9af | 2006-06-13 22:57:13 +0000 | [diff] [blame] | 346 | |
| 347 | /* |
| 348 | * Start pjsua media subsystem. |
| 349 | */ |
| 350 | pj_status_t pjsua_media_subsys_start(void) |
| 351 | { |
| 352 | pj_status_t status; |
| 353 | |
Benny Prijono | b90fd38 | 2011-09-18 14:59:56 +0000 | [diff] [blame] | 354 | pj_log_push_indent(); |
| 355 | |
Benny Prijono | 0bc99a9 | 2011-03-17 04:34:43 +0000 | [diff] [blame] | 356 | #if DISABLED_FOR_TICKET_1185 |
Benny Prijono | eebe9af | 2006-06-13 22:57:13 +0000 | [diff] [blame] | 357 | /* Create media for calls, if none is specified */ |
Benny Prijono | 0bc99a9 | 2011-03-17 04:34:43 +0000 | [diff] [blame] | 358 | if (pjsua_var.calls[0].media[0].tp == NULL) { |
Benny Prijono | eebe9af | 2006-06-13 22:57:13 +0000 | [diff] [blame] | 359 | pjsua_transport_config transport_cfg; |
| 360 | |
| 361 | /* Create default transport config */ |
| 362 | pjsua_transport_config_default(&transport_cfg); |
| 363 | transport_cfg.port = DEFAULT_RTP_PORT; |
| 364 | |
| 365 | status = pjsua_media_transports_create(&transport_cfg); |
Benny Prijono | b90fd38 | 2011-09-18 14:59:56 +0000 | [diff] [blame] | 366 | if (status != PJ_SUCCESS) { |
| 367 | pj_log_pop_indent(); |
Benny Prijono | eebe9af | 2006-06-13 22:57:13 +0000 | [diff] [blame] | 368 | return status; |
Benny Prijono | b90fd38 | 2011-09-18 14:59:56 +0000 | [diff] [blame] | 369 | } |
Benny Prijono | eebe9af | 2006-06-13 22:57:13 +0000 | [diff] [blame] | 370 | } |
Benny Prijono | 0bc99a9 | 2011-03-17 04:34:43 +0000 | [diff] [blame] | 371 | #endif |
Benny Prijono | eebe9af | 2006-06-13 22:57:13 +0000 | [diff] [blame] | 372 | |
Benny Prijono | 0bc99a9 | 2011-03-17 04:34:43 +0000 | [diff] [blame] | 373 | pj_timer_entry_init(&pjsua_var.snd_idle_timer, PJ_FALSE, NULL, |
Nanang Izzuddin | 68559c3 | 2008-06-13 17:01:46 +0000 | [diff] [blame] | 374 | &close_snd_timer_cb); |
Benny Prijono | eebe9af | 2006-06-13 22:57:13 +0000 | [diff] [blame] | 375 | |
Benny Prijono | 9f468d1 | 2011-07-07 07:46:33 +0000 | [diff] [blame] | 376 | /* Video */ |
| 377 | #if PJMEDIA_HAS_VIDEO |
| 378 | status = pjsua_vid_subsys_start(); |
Benny Prijono | b90fd38 | 2011-09-18 14:59:56 +0000 | [diff] [blame] | 379 | if (status != PJ_SUCCESS) { |
| 380 | pj_log_pop_indent(); |
Benny Prijono | 9f468d1 | 2011-07-07 07:46:33 +0000 | [diff] [blame] | 381 | return status; |
Benny Prijono | b90fd38 | 2011-09-18 14:59:56 +0000 | [diff] [blame] | 382 | } |
Benny Prijono | 9f468d1 | 2011-07-07 07:46:33 +0000 | [diff] [blame] | 383 | #endif |
| 384 | |
Benny Prijono | bf53b00 | 2010-01-04 13:08:31 +0000 | [diff] [blame] | 385 | /* Perform NAT detection */ |
Benny Prijono | 0bc99a9 | 2011-03-17 04:34:43 +0000 | [diff] [blame] | 386 | status = pjsua_detect_nat_type(); |
| 387 | if (status != PJ_SUCCESS) { |
| 388 | PJ_PERROR(1,(THIS_FILE, status, "NAT type detection failed")); |
| 389 | } |
Benny Prijono | bf53b00 | 2010-01-04 13:08:31 +0000 | [diff] [blame] | 390 | |
Benny Prijono | b90fd38 | 2011-09-18 14:59:56 +0000 | [diff] [blame] | 391 | pj_log_pop_indent(); |
Benny Prijono | eebe9af | 2006-06-13 22:57:13 +0000 | [diff] [blame] | 392 | return PJ_SUCCESS; |
| 393 | } |
| 394 | |
| 395 | |
| 396 | /* |
| 397 | * Destroy pjsua media subsystem. |
| 398 | */ |
| 399 | pj_status_t pjsua_media_subsys_destroy(void) |
| 400 | { |
| 401 | unsigned i; |
| 402 | |
Benny Prijono | 384dab4 | 2009-10-14 01:58:04 +0000 | [diff] [blame] | 403 | PJ_LOG(4,(THIS_FILE, "Shutting down media..")); |
Benny Prijono | b90fd38 | 2011-09-18 14:59:56 +0000 | [diff] [blame] | 404 | pj_log_push_indent(); |
Benny Prijono | 384dab4 | 2009-10-14 01:58:04 +0000 | [diff] [blame] | 405 | |
Benny Prijono | eebe9af | 2006-06-13 22:57:13 +0000 | [diff] [blame] | 406 | close_snd_dev(); |
| 407 | |
| 408 | if (pjsua_var.mconf) { |
| 409 | pjmedia_conf_destroy(pjsua_var.mconf); |
| 410 | pjsua_var.mconf = NULL; |
| 411 | } |
| 412 | |
| 413 | if (pjsua_var.null_port) { |
| 414 | pjmedia_port_destroy(pjsua_var.null_port); |
| 415 | pjsua_var.null_port = NULL; |
| 416 | } |
| 417 | |
| 418 | /* Destroy file players */ |
| 419 | for (i=0; i<PJ_ARRAY_SIZE(pjsua_var.player); ++i) { |
| 420 | if (pjsua_var.player[i].port) { |
| 421 | pjmedia_port_destroy(pjsua_var.player[i].port); |
| 422 | pjsua_var.player[i].port = NULL; |
| 423 | } |
| 424 | } |
| 425 | |
| 426 | /* Destroy file recorders */ |
| 427 | for (i=0; i<PJ_ARRAY_SIZE(pjsua_var.recorder); ++i) { |
| 428 | if (pjsua_var.recorder[i].port) { |
| 429 | pjmedia_port_destroy(pjsua_var.recorder[i].port); |
| 430 | pjsua_var.recorder[i].port = NULL; |
| 431 | } |
| 432 | } |
| 433 | |
| 434 | /* Close media transports */ |
Benny Prijono | 0875ae8 | 2006-12-26 00:11:48 +0000 | [diff] [blame] | 435 | for (i=0; i<pjsua_var.ua_cfg.max_calls; ++i) { |
Benny Prijono | 0bc99a9 | 2011-03-17 04:34:43 +0000 | [diff] [blame] | 436 | unsigned strm_idx; |
| 437 | pjsua_call *call = &pjsua_var.calls[i]; |
| 438 | for (strm_idx=0; strm_idx<call->med_cnt; ++strm_idx) { |
| 439 | pjsua_call_media *call_med = &call->media[strm_idx]; |
| 440 | if (call_med->tp_st != PJSUA_MED_TP_IDLE) { |
| 441 | pjsua_media_channel_deinit(i); |
| 442 | } |
| 443 | if (call_med->tp && call_med->tp_auto_del) { |
| 444 | pjmedia_transport_close(call_med->tp); |
| 445 | } |
| 446 | call_med->tp = NULL; |
Benny Prijono | 311b63f | 2008-07-14 11:31:40 +0000 | [diff] [blame] | 447 | } |
Benny Prijono | eebe9af | 2006-06-13 22:57:13 +0000 | [diff] [blame] | 448 | } |
| 449 | |
| 450 | /* Destroy media endpoint. */ |
| 451 | if (pjsua_var.med_endpt) { |
| 452 | |
Benny Prijono | 0bc99a9 | 2011-03-17 04:34:43 +0000 | [diff] [blame] | 453 | # if PJMEDIA_HAS_VIDEO |
Benny Prijono | 9f468d1 | 2011-07-07 07:46:33 +0000 | [diff] [blame] | 454 | pjsua_vid_subsys_destroy(); |
Benny Prijono | 0bc99a9 | 2011-03-17 04:34:43 +0000 | [diff] [blame] | 455 | # endif |
Benny Prijono | eebe9af | 2006-06-13 22:57:13 +0000 | [diff] [blame] | 456 | |
Benny Prijono | eebe9af | 2006-06-13 22:57:13 +0000 | [diff] [blame] | 457 | pjmedia_endpt_destroy(pjsua_var.med_endpt); |
| 458 | pjsua_var.med_endpt = NULL; |
Benny Prijono | eebe9af | 2006-06-13 22:57:13 +0000 | [diff] [blame] | 459 | |
Benny Prijono | ad2e0ca | 2007-04-29 12:31:51 +0000 | [diff] [blame] | 460 | /* Deinitialize sound subsystem */ |
Benny Prijono | a41d66e | 2007-10-01 12:17:23 +0000 | [diff] [blame] | 461 | // Not necessary, as pjmedia_snd_deinit() should have been called |
| 462 | // in pjmedia_endpt_destroy(). |
| 463 | //pjmedia_snd_deinit(); |
Benny Prijono | ad2e0ca | 2007-04-29 12:31:51 +0000 | [diff] [blame] | 464 | } |
Benny Prijono | af1bb1e | 2006-11-21 12:39:31 +0000 | [diff] [blame] | 465 | |
Benny Prijono | de47956 | 2007-03-15 10:23:55 +0000 | [diff] [blame] | 466 | /* Reset RTP port */ |
| 467 | next_rtp_port = 0; |
| 468 | |
Benny Prijono | b90fd38 | 2011-09-18 14:59:56 +0000 | [diff] [blame] | 469 | pj_log_pop_indent(); |
| 470 | |
Benny Prijono | eebe9af | 2006-06-13 22:57:13 +0000 | [diff] [blame] | 471 | return PJ_SUCCESS; |
| 472 | } |
| 473 | |
Benny Prijono | 0bc99a9 | 2011-03-17 04:34:43 +0000 | [diff] [blame] | 474 | /* |
Benny Prijono | eebe9af | 2006-06-13 22:57:13 +0000 | [diff] [blame] | 475 | * Create RTP and RTCP socket pair, and possibly resolve their public |
| 476 | * address via STUN. |
| 477 | */ |
| 478 | static pj_status_t create_rtp_rtcp_sock(const pjsua_transport_config *cfg, |
| 479 | pjmedia_sock_info *skinfo) |
| 480 | { |
Benny Prijono | 0bc99a9 | 2011-03-17 04:34:43 +0000 | [diff] [blame] | 481 | enum { |
Benny Prijono | eebe9af | 2006-06-13 22:57:13 +0000 | [diff] [blame] | 482 | RTP_RETRY = 100 |
| 483 | }; |
| 484 | int i; |
| 485 | pj_sockaddr_in bound_addr; |
| 486 | pj_sockaddr_in mapped_addr[2]; |
| 487 | pj_status_t status = PJ_SUCCESS; |
| 488 | char addr_buf[PJ_INET6_ADDRSTRLEN+2]; |
| 489 | pj_sock_t sock[2]; |
| 490 | |
| 491 | /* Make sure STUN server resolution has completed */ |
| 492 | status = resolve_stun_server(PJ_TRUE); |
| 493 | if (status != PJ_SUCCESS) { |
| 494 | pjsua_perror(THIS_FILE, "Error resolving STUN server", status); |
| 495 | return status; |
| 496 | } |
| 497 | |
| 498 | if (next_rtp_port == 0) |
| 499 | next_rtp_port = (pj_uint16_t)cfg->port; |
| 500 | |
Benny Prijono | 0bc99a9 | 2011-03-17 04:34:43 +0000 | [diff] [blame] | 501 | if (next_rtp_port == 0) |
| 502 | next_rtp_port = (pj_uint16_t)40000; |
| 503 | |
Benny Prijono | eebe9af | 2006-06-13 22:57:13 +0000 | [diff] [blame] | 504 | for (i=0; i<2; ++i) |
| 505 | sock[i] = PJ_INVALID_SOCKET; |
| 506 | |
| 507 | bound_addr.sin_addr.s_addr = PJ_INADDR_ANY; |
| 508 | if (cfg->bound_addr.slen) { |
| 509 | status = pj_sockaddr_in_set_str_addr(&bound_addr, &cfg->bound_addr); |
| 510 | if (status != PJ_SUCCESS) { |
| 511 | pjsua_perror(THIS_FILE, "Unable to resolve transport bind address", |
| 512 | status); |
| 513 | return status; |
| 514 | } |
| 515 | } |
| 516 | |
| 517 | /* Loop retry to bind RTP and RTCP sockets. */ |
| 518 | for (i=0; i<RTP_RETRY; ++i, next_rtp_port += 2) { |
| 519 | |
| 520 | /* Create RTP socket. */ |
| 521 | status = pj_sock_socket(pj_AF_INET(), pj_SOCK_DGRAM(), 0, &sock[0]); |
| 522 | if (status != PJ_SUCCESS) { |
| 523 | pjsua_perror(THIS_FILE, "socket() error", status); |
| 524 | return status; |
| 525 | } |
| 526 | |
| 527 | /* Apply QoS to RTP socket, if specified */ |
Benny Prijono | 0bc99a9 | 2011-03-17 04:34:43 +0000 | [diff] [blame] | 528 | status = pj_sock_apply_qos2(sock[0], cfg->qos_type, |
| 529 | &cfg->qos_params, |
Benny Prijono | eebe9af | 2006-06-13 22:57:13 +0000 | [diff] [blame] | 530 | 2, THIS_FILE, "RTP socket"); |
| 531 | |
| 532 | /* Bind RTP socket */ |
Benny Prijono | 0bc99a9 | 2011-03-17 04:34:43 +0000 | [diff] [blame] | 533 | status=pj_sock_bind_in(sock[0], pj_ntohl(bound_addr.sin_addr.s_addr), |
Benny Prijono | eebe9af | 2006-06-13 22:57:13 +0000 | [diff] [blame] | 534 | next_rtp_port); |
| 535 | if (status != PJ_SUCCESS) { |
Benny Prijono | 0bc99a9 | 2011-03-17 04:34:43 +0000 | [diff] [blame] | 536 | pj_sock_close(sock[0]); |
Benny Prijono | eebe9af | 2006-06-13 22:57:13 +0000 | [diff] [blame] | 537 | sock[0] = PJ_INVALID_SOCKET; |
| 538 | continue; |
| 539 | } |
| 540 | |
| 541 | /* Create RTCP socket. */ |
| 542 | status = pj_sock_socket(pj_AF_INET(), pj_SOCK_DGRAM(), 0, &sock[1]); |
| 543 | if (status != PJ_SUCCESS) { |
| 544 | pjsua_perror(THIS_FILE, "socket() error", status); |
| 545 | pj_sock_close(sock[0]); |
| 546 | return status; |
| 547 | } |
| 548 | |
| 549 | /* Apply QoS to RTCP socket, if specified */ |
Benny Prijono | 0bc99a9 | 2011-03-17 04:34:43 +0000 | [diff] [blame] | 550 | status = pj_sock_apply_qos2(sock[1], cfg->qos_type, |
| 551 | &cfg->qos_params, |
Benny Prijono | eebe9af | 2006-06-13 22:57:13 +0000 | [diff] [blame] | 552 | 2, THIS_FILE, "RTCP socket"); |
| 553 | |
| 554 | /* Bind RTCP socket */ |
Benny Prijono | 0bc99a9 | 2011-03-17 04:34:43 +0000 | [diff] [blame] | 555 | status=pj_sock_bind_in(sock[1], pj_ntohl(bound_addr.sin_addr.s_addr), |
Benny Prijono | eebe9af | 2006-06-13 22:57:13 +0000 | [diff] [blame] | 556 | (pj_uint16_t)(next_rtp_port+1)); |
| 557 | if (status != PJ_SUCCESS) { |
Benny Prijono | 0bc99a9 | 2011-03-17 04:34:43 +0000 | [diff] [blame] | 558 | pj_sock_close(sock[0]); |
Benny Prijono | eebe9af | 2006-06-13 22:57:13 +0000 | [diff] [blame] | 559 | sock[0] = PJ_INVALID_SOCKET; |
| 560 | |
Benny Prijono | 0bc99a9 | 2011-03-17 04:34:43 +0000 | [diff] [blame] | 561 | pj_sock_close(sock[1]); |
Benny Prijono | eebe9af | 2006-06-13 22:57:13 +0000 | [diff] [blame] | 562 | sock[1] = PJ_INVALID_SOCKET; |
| 563 | continue; |
| 564 | } |
| 565 | |
| 566 | /* |
| 567 | * If we're configured to use STUN, then find out the mapped address, |
| 568 | * and make sure that the mapped RTCP port is adjacent with the RTP. |
| 569 | */ |
| 570 | if (pjsua_var.stun_srv.addr.sa_family != 0) { |
| 571 | char ip_addr[32]; |
| 572 | pj_str_t stun_srv; |
| 573 | |
Benny Prijono | 0bc99a9 | 2011-03-17 04:34:43 +0000 | [diff] [blame] | 574 | pj_ansi_strcpy(ip_addr, |
Benny Prijono | eebe9af | 2006-06-13 22:57:13 +0000 | [diff] [blame] | 575 | pj_inet_ntoa(pjsua_var.stun_srv.ipv4.sin_addr)); |
| 576 | stun_srv = pj_str(ip_addr); |
| 577 | |
| 578 | status=pjstun_get_mapped_addr(&pjsua_var.cp.factory, 2, sock, |
| 579 | &stun_srv, pj_ntohs(pjsua_var.stun_srv.ipv4.sin_port), |
| 580 | &stun_srv, pj_ntohs(pjsua_var.stun_srv.ipv4.sin_port), |
| 581 | mapped_addr); |
| 582 | if (status != PJ_SUCCESS) { |
| 583 | pjsua_perror(THIS_FILE, "STUN resolve error", status); |
| 584 | goto on_error; |
| 585 | } |
| 586 | |
| 587 | #if PJSUA_REQUIRE_CONSECUTIVE_RTCP_PORT |
Benny Prijono | 0bc99a9 | 2011-03-17 04:34:43 +0000 | [diff] [blame] | 588 | if (pj_ntohs(mapped_addr[1].sin_port) == |
Benny Prijono | eebe9af | 2006-06-13 22:57:13 +0000 | [diff] [blame] | 589 | pj_ntohs(mapped_addr[0].sin_port)+1) |
| 590 | { |
| 591 | /* Success! */ |
| 592 | break; |
| 593 | } |
| 594 | |
Benny Prijono | 0bc99a9 | 2011-03-17 04:34:43 +0000 | [diff] [blame] | 595 | pj_sock_close(sock[0]); |
Benny Prijono | eebe9af | 2006-06-13 22:57:13 +0000 | [diff] [blame] | 596 | sock[0] = PJ_INVALID_SOCKET; |
| 597 | |
Benny Prijono | 0bc99a9 | 2011-03-17 04:34:43 +0000 | [diff] [blame] | 598 | pj_sock_close(sock[1]); |
Benny Prijono | eebe9af | 2006-06-13 22:57:13 +0000 | [diff] [blame] | 599 | sock[1] = PJ_INVALID_SOCKET; |
| 600 | #else |
Benny Prijono | 0bc99a9 | 2011-03-17 04:34:43 +0000 | [diff] [blame] | 601 | if (pj_ntohs(mapped_addr[1].sin_port) != |
Benny Prijono | eebe9af | 2006-06-13 22:57:13 +0000 | [diff] [blame] | 602 | pj_ntohs(mapped_addr[0].sin_port)+1) |
| 603 | { |
Benny Prijono | 0bc99a9 | 2011-03-17 04:34:43 +0000 | [diff] [blame] | 604 | PJ_LOG(4,(THIS_FILE, |
Benny Prijono | eebe9af | 2006-06-13 22:57:13 +0000 | [diff] [blame] | 605 | "Note: STUN mapped RTCP port %d is not adjacent" |
| 606 | " to RTP port %d", |
| 607 | pj_ntohs(mapped_addr[1].sin_port), |
| 608 | pj_ntohs(mapped_addr[0].sin_port))); |
| 609 | } |
| 610 | /* Success! */ |
| 611 | break; |
| 612 | #endif |
| 613 | |
| 614 | } else if (cfg->public_addr.slen) { |
| 615 | |
| 616 | status = pj_sockaddr_in_init(&mapped_addr[0], &cfg->public_addr, |
| 617 | (pj_uint16_t)next_rtp_port); |
| 618 | if (status != PJ_SUCCESS) |
| 619 | goto on_error; |
| 620 | |
| 621 | status = pj_sockaddr_in_init(&mapped_addr[1], &cfg->public_addr, |
| 622 | (pj_uint16_t)(next_rtp_port+1)); |
| 623 | if (status != PJ_SUCCESS) |
| 624 | goto on_error; |
| 625 | |
| 626 | break; |
| 627 | |
| 628 | } else { |
| 629 | |
| 630 | if (bound_addr.sin_addr.s_addr == 0) { |
| 631 | pj_sockaddr addr; |
| 632 | |
| 633 | /* Get local IP address. */ |
| 634 | status = pj_gethostip(pj_AF_INET(), &addr); |
| 635 | if (status != PJ_SUCCESS) |
| 636 | goto on_error; |
| 637 | |
| 638 | bound_addr.sin_addr.s_addr = addr.ipv4.sin_addr.s_addr; |
| 639 | } |
| 640 | |
| 641 | for (i=0; i<2; ++i) { |
| 642 | pj_sockaddr_in_init(&mapped_addr[i], NULL, 0); |
| 643 | mapped_addr[i].sin_addr.s_addr = bound_addr.sin_addr.s_addr; |
| 644 | } |
| 645 | |
| 646 | mapped_addr[0].sin_port=pj_htons((pj_uint16_t)next_rtp_port); |
| 647 | mapped_addr[1].sin_port=pj_htons((pj_uint16_t)(next_rtp_port+1)); |
| 648 | break; |
| 649 | } |
| 650 | } |
| 651 | |
| 652 | if (sock[0] == PJ_INVALID_SOCKET) { |
Benny Prijono | 0bc99a9 | 2011-03-17 04:34:43 +0000 | [diff] [blame] | 653 | PJ_LOG(1,(THIS_FILE, |
Benny Prijono | eebe9af | 2006-06-13 22:57:13 +0000 | [diff] [blame] | 654 | "Unable to find appropriate RTP/RTCP ports combination")); |
| 655 | goto on_error; |
| 656 | } |
| 657 | |
| 658 | |
| 659 | skinfo->rtp_sock = sock[0]; |
Benny Prijono | 0bc99a9 | 2011-03-17 04:34:43 +0000 | [diff] [blame] | 660 | pj_memcpy(&skinfo->rtp_addr_name, |
Benny Prijono | eebe9af | 2006-06-13 22:57:13 +0000 | [diff] [blame] | 661 | &mapped_addr[0], sizeof(pj_sockaddr_in)); |
| 662 | |
| 663 | skinfo->rtcp_sock = sock[1]; |
Benny Prijono | 0bc99a9 | 2011-03-17 04:34:43 +0000 | [diff] [blame] | 664 | pj_memcpy(&skinfo->rtcp_addr_name, |
Benny Prijono | eebe9af | 2006-06-13 22:57:13 +0000 | [diff] [blame] | 665 | &mapped_addr[1], sizeof(pj_sockaddr_in)); |
| 666 | |
| 667 | PJ_LOG(4,(THIS_FILE, "RTP socket reachable at %s", |
| 668 | pj_sockaddr_print(&skinfo->rtp_addr_name, addr_buf, |
| 669 | sizeof(addr_buf), 3))); |
| 670 | PJ_LOG(4,(THIS_FILE, "RTCP socket reachable at %s", |
| 671 | pj_sockaddr_print(&skinfo->rtcp_addr_name, addr_buf, |
| 672 | sizeof(addr_buf), 3))); |
| 673 | |
| 674 | next_rtp_port += 2; |
| 675 | return PJ_SUCCESS; |
| 676 | |
| 677 | on_error: |
| 678 | for (i=0; i<2; ++i) { |
| 679 | if (sock[i] != PJ_INVALID_SOCKET) |
| 680 | pj_sock_close(sock[i]); |
| 681 | } |
| 682 | return status; |
| 683 | } |
| 684 | |
Benny Prijono | 0bc99a9 | 2011-03-17 04:34:43 +0000 | [diff] [blame] | 685 | /* Create normal UDP media transports */ |
| 686 | static pj_status_t create_udp_media_transport(const pjsua_transport_config *cfg, |
| 687 | pjsua_call_media *call_med) |
Benny Prijono | eebe9af | 2006-06-13 22:57:13 +0000 | [diff] [blame] | 688 | { |
Benny Prijono | 0bc99a9 | 2011-03-17 04:34:43 +0000 | [diff] [blame] | 689 | pjmedia_sock_info skinfo; |
Benny Prijono | eebe9af | 2006-06-13 22:57:13 +0000 | [diff] [blame] | 690 | pj_status_t status; |
| 691 | |
Benny Prijono | 0bc99a9 | 2011-03-17 04:34:43 +0000 | [diff] [blame] | 692 | status = create_rtp_rtcp_sock(cfg, &skinfo); |
| 693 | if (status != PJ_SUCCESS) { |
| 694 | pjsua_perror(THIS_FILE, "Unable to create RTP/RTCP socket", |
| 695 | status); |
| 696 | goto on_error; |
Benny Prijono | eebe9af | 2006-06-13 22:57:13 +0000 | [diff] [blame] | 697 | } |
| 698 | |
Benny Prijono | 0bc99a9 | 2011-03-17 04:34:43 +0000 | [diff] [blame] | 699 | status = pjmedia_transport_udp_attach(pjsua_var.med_endpt, NULL, |
| 700 | &skinfo, 0, &call_med->tp); |
| 701 | if (status != PJ_SUCCESS) { |
| 702 | pjsua_perror(THIS_FILE, "Unable to create media transport", |
| 703 | status); |
| 704 | goto on_error; |
| 705 | } |
Benny Prijono | eebe9af | 2006-06-13 22:57:13 +0000 | [diff] [blame] | 706 | |
Benny Prijono | 0bc99a9 | 2011-03-17 04:34:43 +0000 | [diff] [blame] | 707 | pjmedia_transport_simulate_lost(call_med->tp, PJMEDIA_DIR_ENCODING, |
| 708 | pjsua_var.media_cfg.tx_drop_pct); |
| 709 | |
| 710 | pjmedia_transport_simulate_lost(call_med->tp, PJMEDIA_DIR_DECODING, |
| 711 | pjsua_var.media_cfg.rx_drop_pct); |
Benny Prijono | eebe9af | 2006-06-13 22:57:13 +0000 | [diff] [blame] | 712 | |
Sauw Ming | 73ecfe8 | 2011-09-21 10:20:01 +0000 | [diff] [blame] | 713 | call_med->tp_ready = PJ_SUCCESS; |
| 714 | |
Benny Prijono | eebe9af | 2006-06-13 22:57:13 +0000 | [diff] [blame] | 715 | return PJ_SUCCESS; |
Benny Prijono | 0bc99a9 | 2011-03-17 04:34:43 +0000 | [diff] [blame] | 716 | |
| 717 | on_error: |
| 718 | if (call_med->tp) |
| 719 | pjmedia_transport_close(call_med->tp); |
| 720 | |
| 721 | return status; |
Benny Prijono | eebe9af | 2006-06-13 22:57:13 +0000 | [diff] [blame] | 722 | } |
| 723 | |
Benny Prijono | 0bc99a9 | 2011-03-17 04:34:43 +0000 | [diff] [blame] | 724 | #if DISABLED_FOR_TICKET_1185 |
Benny Prijono | c97608e | 2007-03-23 16:34:20 +0000 | [diff] [blame] | 725 | /* Create normal UDP media transports */ |
| 726 | static pj_status_t create_udp_media_transports(pjsua_transport_config *cfg) |
Benny Prijono | eebe9af | 2006-06-13 22:57:13 +0000 | [diff] [blame] | 727 | { |
Benny Prijono | eebe9af | 2006-06-13 22:57:13 +0000 | [diff] [blame] | 728 | unsigned i; |
| 729 | pj_status_t status; |
| 730 | |
Benny Prijono | 0bc99a9 | 2011-03-17 04:34:43 +0000 | [diff] [blame] | 731 | for (i=0; i < pjsua_var.ua_cfg.max_calls; ++i) { |
| 732 | pjsua_call *call = &pjsua_var.calls[i]; |
| 733 | unsigned strm_idx; |
Benny Prijono | eebe9af | 2006-06-13 22:57:13 +0000 | [diff] [blame] | 734 | |
Benny Prijono | 0bc99a9 | 2011-03-17 04:34:43 +0000 | [diff] [blame] | 735 | for (strm_idx=0; strm_idx < call->med_cnt; ++strm_idx) { |
| 736 | pjsua_call_media *call_med = &call->media[strm_idx]; |
| 737 | |
| 738 | status = create_udp_media_transport(cfg, &call_med->tp); |
| 739 | if (status != PJ_SUCCESS) |
| 740 | goto on_error; |
Benny Prijono | eebe9af | 2006-06-13 22:57:13 +0000 | [diff] [blame] | 741 | } |
Benny Prijono | eebe9af | 2006-06-13 22:57:13 +0000 | [diff] [blame] | 742 | } |
| 743 | |
Benny Prijono | c97608e | 2007-03-23 16:34:20 +0000 | [diff] [blame] | 744 | return PJ_SUCCESS; |
| 745 | |
| 746 | on_error: |
Benny Prijono | 0bc99a9 | 2011-03-17 04:34:43 +0000 | [diff] [blame] | 747 | for (i=0; i < pjsua_var.ua_cfg.max_calls; ++i) { |
| 748 | pjsua_call *call = &pjsua_var.calls[i]; |
| 749 | unsigned strm_idx; |
| 750 | |
| 751 | for (strm_idx=0; strm_idx < call->med_cnt; ++strm_idx) { |
| 752 | pjsua_call_media *call_med = &call->media[strm_idx]; |
| 753 | |
| 754 | if (call_med->tp) { |
| 755 | pjmedia_transport_close(call_med->tp); |
| 756 | call_med->tp = NULL; |
| 757 | } |
Benny Prijono | c97608e | 2007-03-23 16:34:20 +0000 | [diff] [blame] | 758 | } |
| 759 | } |
Benny Prijono | c97608e | 2007-03-23 16:34:20 +0000 | [diff] [blame] | 760 | return status; |
| 761 | } |
Benny Prijono | 0bc99a9 | 2011-03-17 04:34:43 +0000 | [diff] [blame] | 762 | #endif |
Benny Prijono | c97608e | 2007-03-23 16:34:20 +0000 | [diff] [blame] | 763 | |
Benny Prijono | 096c56c | 2007-09-15 08:30:16 +0000 | [diff] [blame] | 764 | /* This callback is called when ICE negotiation completes */ |
Benny Prijono | f76e139 | 2008-06-06 14:51:48 +0000 | [diff] [blame] | 765 | static void on_ice_complete(pjmedia_transport *tp, |
| 766 | pj_ice_strans_op op, |
| 767 | pj_status_t result) |
Benny Prijono | 096c56c | 2007-09-15 08:30:16 +0000 | [diff] [blame] | 768 | { |
Benny Prijono | 0bc99a9 | 2011-03-17 04:34:43 +0000 | [diff] [blame] | 769 | pjsua_call_media *call_med = (pjsua_call_media*)tp->user_data; |
Benny Prijono | 096c56c | 2007-09-15 08:30:16 +0000 | [diff] [blame] | 770 | |
Benny Prijono | 0bc99a9 | 2011-03-17 04:34:43 +0000 | [diff] [blame] | 771 | if (!call_med) |
Benny Prijono | f76e139 | 2008-06-06 14:51:48 +0000 | [diff] [blame] | 772 | return; |
| 773 | |
| 774 | switch (op) { |
| 775 | case PJ_ICE_STRANS_OP_INIT: |
Sauw Ming | 73ecfe8 | 2011-09-21 10:20:01 +0000 | [diff] [blame] | 776 | call_med->tp_ready = result; |
| 777 | if (call_med->med_create_cb) |
| 778 | (*call_med->med_create_cb)(call_med, result, |
| 779 | call_med->call->secure_level, NULL); |
Benny Prijono | f76e139 | 2008-06-06 14:51:48 +0000 | [diff] [blame] | 780 | break; |
| 781 | case PJ_ICE_STRANS_OP_NEGOTIATION: |
| 782 | if (result != PJ_SUCCESS) { |
Benny Prijono | 0bc99a9 | 2011-03-17 04:34:43 +0000 | [diff] [blame] | 783 | call_med->state = PJSUA_CALL_MEDIA_ERROR; |
| 784 | call_med->dir = PJMEDIA_DIR_NONE; |
Benny Prijono | f76e139 | 2008-06-06 14:51:48 +0000 | [diff] [blame] | 785 | |
Benny Prijono | 0bc99a9 | 2011-03-17 04:34:43 +0000 | [diff] [blame] | 786 | if (call_med->call && pjsua_var.ua_cfg.cb.on_call_media_state) { |
| 787 | pjsua_var.ua_cfg.cb.on_call_media_state(call_med->call->index); |
Benny Prijono | f76e139 | 2008-06-06 14:51:48 +0000 | [diff] [blame] | 788 | } |
Benny Prijono | 0bc99a9 | 2011-03-17 04:34:43 +0000 | [diff] [blame] | 789 | } else if (call_med->call) { |
Benny Prijono | f5d9f1f | 2009-10-14 13:13:18 +0000 | [diff] [blame] | 790 | /* Send UPDATE if default transport address is different than |
| 791 | * what was advertised (ticket #881) |
| 792 | */ |
| 793 | pjmedia_transport_info tpinfo; |
| 794 | pjmedia_ice_transport_info *ii = NULL; |
| 795 | unsigned i; |
| 796 | |
| 797 | pjmedia_transport_info_init(&tpinfo); |
| 798 | pjmedia_transport_get_info(tp, &tpinfo); |
| 799 | for (i=0; i<tpinfo.specific_info_cnt; ++i) { |
| 800 | if (tpinfo.spc_info[i].type==PJMEDIA_TRANSPORT_TYPE_ICE) { |
| 801 | ii = (pjmedia_ice_transport_info*) |
| 802 | tpinfo.spc_info[i].buffer; |
| 803 | break; |
| 804 | } |
| 805 | } |
| 806 | |
| 807 | if (ii && ii->role==PJ_ICE_SESS_ROLE_CONTROLLING && |
| 808 | pj_sockaddr_cmp(&tpinfo.sock_info.rtp_addr_name, |
Benny Prijono | 0bc99a9 | 2011-03-17 04:34:43 +0000 | [diff] [blame] | 809 | &call_med->rtp_addr)) |
Benny Prijono | f5d9f1f | 2009-10-14 13:13:18 +0000 | [diff] [blame] | 810 | { |
Benny Prijono | a8f9e62 | 2010-06-21 13:28:55 +0000 | [diff] [blame] | 811 | pj_bool_t use_update; |
| 812 | const pj_str_t STR_UPDATE = { "UPDATE", 6 }; |
| 813 | pjsip_dialog_cap_status support_update; |
| 814 | pjsip_dialog *dlg; |
| 815 | |
Benny Prijono | 0bc99a9 | 2011-03-17 04:34:43 +0000 | [diff] [blame] | 816 | dlg = call_med->call->inv->dlg; |
Benny Prijono | a8f9e62 | 2010-06-21 13:28:55 +0000 | [diff] [blame] | 817 | support_update = pjsip_dlg_remote_has_cap(dlg, PJSIP_H_ALLOW, |
| 818 | NULL, &STR_UPDATE); |
| 819 | use_update = (support_update == PJSIP_DIALOG_CAP_SUPPORTED); |
| 820 | |
Benny Prijono | f5d9f1f | 2009-10-14 13:13:18 +0000 | [diff] [blame] | 821 | PJ_LOG(4,(THIS_FILE, |
| 822 | "ICE default transport address has changed for " |
Benny Prijono | 0bc99a9 | 2011-03-17 04:34:43 +0000 | [diff] [blame] | 823 | "call %d, sending %s", call_med->call->index, |
Benny Prijono | a8f9e62 | 2010-06-21 13:28:55 +0000 | [diff] [blame] | 824 | (use_update ? "UPDATE" : "re-INVITE"))); |
| 825 | |
| 826 | if (use_update) |
Benny Prijono | 0bc99a9 | 2011-03-17 04:34:43 +0000 | [diff] [blame] | 827 | pjsua_call_update(call_med->call->index, 0, NULL); |
Benny Prijono | a8f9e62 | 2010-06-21 13:28:55 +0000 | [diff] [blame] | 828 | else |
Benny Prijono | 0bc99a9 | 2011-03-17 04:34:43 +0000 | [diff] [blame] | 829 | pjsua_call_reinvite(call_med->call->index, 0, NULL); |
Benny Prijono | f5d9f1f | 2009-10-14 13:13:18 +0000 | [diff] [blame] | 830 | } |
Benny Prijono | f76e139 | 2008-06-06 14:51:48 +0000 | [diff] [blame] | 831 | } |
| 832 | break; |
Benny Prijono | 4d6ff4d | 2010-06-19 12:35:33 +0000 | [diff] [blame] | 833 | case PJ_ICE_STRANS_OP_KEEP_ALIVE: |
| 834 | if (result != PJ_SUCCESS) { |
| 835 | PJ_PERROR(4,(THIS_FILE, result, |
Benny Prijono | 0bc99a9 | 2011-03-17 04:34:43 +0000 | [diff] [blame] | 836 | "ICE keep alive failure for transport %d:%d", |
| 837 | call_med->call->index, call_med->idx)); |
Benny Prijono | 4d6ff4d | 2010-06-19 12:35:33 +0000 | [diff] [blame] | 838 | } |
Sauw Ming | 73ecfe8 | 2011-09-21 10:20:01 +0000 | [diff] [blame] | 839 | if (pjsua_var.ua_cfg.cb.on_call_media_transport_state) { |
| 840 | pjsua_med_tp_state_info info; |
| 841 | |
| 842 | pj_bzero(&info, sizeof(info)); |
| 843 | info.med_idx = call_med->idx; |
| 844 | info.state = call_med->tp_st; |
| 845 | info.status = result; |
| 846 | info.ext_info = &op; |
| 847 | (*pjsua_var.ua_cfg.cb.on_call_media_transport_state)( |
| 848 | call_med->call->index, &info); |
| 849 | } |
Benny Prijono | 4d6ff4d | 2010-06-19 12:35:33 +0000 | [diff] [blame] | 850 | if (pjsua_var.ua_cfg.cb.on_ice_transport_error) { |
Benny Prijono | 0bc99a9 | 2011-03-17 04:34:43 +0000 | [diff] [blame] | 851 | pjsua_call_id id = call_med->call->index; |
Benny Prijono | 4d6ff4d | 2010-06-19 12:35:33 +0000 | [diff] [blame] | 852 | (*pjsua_var.ua_cfg.cb.on_ice_transport_error)(id, op, result, |
| 853 | NULL); |
| 854 | } |
| 855 | break; |
Benny Prijono | 096c56c | 2007-09-15 08:30:16 +0000 | [diff] [blame] | 856 | } |
| 857 | } |
| 858 | |
| 859 | |
Benny Prijono | f76e139 | 2008-06-06 14:51:48 +0000 | [diff] [blame] | 860 | /* Parse "HOST:PORT" format */ |
| 861 | static pj_status_t parse_host_port(const pj_str_t *host_port, |
| 862 | pj_str_t *host, pj_uint16_t *port) |
Benny Prijono | c97608e | 2007-03-23 16:34:20 +0000 | [diff] [blame] | 863 | { |
Benny Prijono | f76e139 | 2008-06-06 14:51:48 +0000 | [diff] [blame] | 864 | pj_str_t str_port; |
| 865 | |
| 866 | str_port.ptr = pj_strchr(host_port, ':'); |
| 867 | if (str_port.ptr != NULL) { |
| 868 | int iport; |
| 869 | |
| 870 | host->ptr = host_port->ptr; |
| 871 | host->slen = (str_port.ptr - host->ptr); |
| 872 | str_port.ptr++; |
| 873 | str_port.slen = host_port->slen - host->slen - 1; |
| 874 | iport = (int)pj_strtoul(&str_port); |
| 875 | if (iport < 1 || iport > 65535) |
| 876 | return PJ_EINVAL; |
| 877 | *port = (pj_uint16_t)iport; |
| 878 | } else { |
| 879 | *host = *host_port; |
| 880 | *port = 0; |
| 881 | } |
| 882 | |
| 883 | return PJ_SUCCESS; |
| 884 | } |
| 885 | |
| 886 | /* Create ICE media transports (when ice is enabled) */ |
Benny Prijono | 0bc99a9 | 2011-03-17 04:34:43 +0000 | [diff] [blame] | 887 | static pj_status_t create_ice_media_transport( |
| 888 | const pjsua_transport_config *cfg, |
Sauw Ming | 73ecfe8 | 2011-09-21 10:20:01 +0000 | [diff] [blame] | 889 | pjsua_call_media *call_med, |
| 890 | pj_bool_t async) |
Benny Prijono | f76e139 | 2008-06-06 14:51:48 +0000 | [diff] [blame] | 891 | { |
| 892 | char stunip[PJ_INET6_ADDRSTRLEN]; |
| 893 | pj_ice_strans_cfg ice_cfg; |
Benny Prijono | 0bc99a9 | 2011-03-17 04:34:43 +0000 | [diff] [blame] | 894 | pjmedia_ice_cb ice_cb; |
| 895 | char name[32]; |
| 896 | unsigned comp_cnt; |
Benny Prijono | c97608e | 2007-03-23 16:34:20 +0000 | [diff] [blame] | 897 | pj_status_t status; |
| 898 | |
Benny Prijono | da9785b | 2007-04-02 20:43:06 +0000 | [diff] [blame] | 899 | /* Make sure STUN server resolution has completed */ |
Benny Prijono | bb995fd | 2009-08-12 11:03:23 +0000 | [diff] [blame] | 900 | status = resolve_stun_server(PJ_TRUE); |
Benny Prijono | da9785b | 2007-04-02 20:43:06 +0000 | [diff] [blame] | 901 | if (status != PJ_SUCCESS) { |
| 902 | pjsua_perror(THIS_FILE, "Error resolving STUN server", status); |
| 903 | return status; |
| 904 | } |
| 905 | |
Benny Prijono | f76e139 | 2008-06-06 14:51:48 +0000 | [diff] [blame] | 906 | /* Create ICE stream transport configuration */ |
| 907 | pj_ice_strans_cfg_default(&ice_cfg); |
| 908 | pj_stun_config_init(&ice_cfg.stun_cfg, &pjsua_var.cp.factory, 0, |
| 909 | pjsip_endpt_get_ioqueue(pjsua_var.endpt), |
| 910 | pjsip_endpt_get_timer_heap(pjsua_var.endpt)); |
| 911 | |
| 912 | ice_cfg.af = pj_AF_INET(); |
| 913 | ice_cfg.resolver = pjsua_var.resolver; |
| 914 | |
Benny Prijono | 329d638 | 2009-05-29 13:04:03 +0000 | [diff] [blame] | 915 | ice_cfg.opt = pjsua_var.media_cfg.ice_opt; |
| 916 | |
Benny Prijono | f76e139 | 2008-06-06 14:51:48 +0000 | [diff] [blame] | 917 | /* Configure STUN settings */ |
| 918 | if (pj_sockaddr_has_addr(&pjsua_var.stun_srv)) { |
| 919 | pj_sockaddr_print(&pjsua_var.stun_srv, stunip, sizeof(stunip), 0); |
| 920 | ice_cfg.stun.server = pj_str(stunip); |
| 921 | ice_cfg.stun.port = pj_sockaddr_get_port(&pjsua_var.stun_srv); |
| 922 | } |
Benny Prijono | 329d638 | 2009-05-29 13:04:03 +0000 | [diff] [blame] | 923 | if (pjsua_var.media_cfg.ice_max_host_cands >= 0) |
| 924 | 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] | 925 | |
Benny Prijono | 4d79b0f | 2009-10-25 09:02:07 +0000 | [diff] [blame] | 926 | /* Copy QoS setting to STUN setting */ |
| 927 | ice_cfg.stun.cfg.qos_type = cfg->qos_type; |
| 928 | pj_memcpy(&ice_cfg.stun.cfg.qos_params, &cfg->qos_params, |
| 929 | sizeof(cfg->qos_params)); |
| 930 | |
Benny Prijono | f76e139 | 2008-06-06 14:51:48 +0000 | [diff] [blame] | 931 | /* Configure TURN settings */ |
| 932 | if (pjsua_var.media_cfg.enable_turn) { |
| 933 | status = parse_host_port(&pjsua_var.media_cfg.turn_server, |
| 934 | &ice_cfg.turn.server, |
| 935 | &ice_cfg.turn.port); |
| 936 | if (status != PJ_SUCCESS || ice_cfg.turn.server.slen == 0) { |
| 937 | PJ_LOG(1,(THIS_FILE, "Invalid TURN server setting")); |
| 938 | return PJ_EINVAL; |
| 939 | } |
| 940 | if (ice_cfg.turn.port == 0) |
| 941 | ice_cfg.turn.port = 3479; |
| 942 | ice_cfg.turn.conn_type = pjsua_var.media_cfg.turn_conn_type; |
| 943 | pj_memcpy(&ice_cfg.turn.auth_cred, |
| 944 | &pjsua_var.media_cfg.turn_auth_cred, |
| 945 | sizeof(ice_cfg.turn.auth_cred)); |
Benny Prijono | 4d79b0f | 2009-10-25 09:02:07 +0000 | [diff] [blame] | 946 | |
| 947 | /* Copy QoS setting to TURN setting */ |
| 948 | ice_cfg.turn.cfg.qos_type = cfg->qos_type; |
| 949 | pj_memcpy(&ice_cfg.turn.cfg.qos_params, &cfg->qos_params, |
| 950 | sizeof(cfg->qos_params)); |
Benny Prijono | f76e139 | 2008-06-06 14:51:48 +0000 | [diff] [blame] | 951 | } |
Benny Prijono | b681a2f | 2007-03-25 18:44:51 +0000 | [diff] [blame] | 952 | |
Benny Prijono | 0bc99a9 | 2011-03-17 04:34:43 +0000 | [diff] [blame] | 953 | pj_bzero(&ice_cb, sizeof(pjmedia_ice_cb)); |
| 954 | ice_cb.on_ice_complete = &on_ice_complete; |
| 955 | pj_ansi_snprintf(name, sizeof(name), "icetp%02d", call_med->idx); |
| 956 | call_med->tp_ready = PJ_EPENDING; |
Benny Prijono | c97608e | 2007-03-23 16:34:20 +0000 | [diff] [blame] | 957 | |
Benny Prijono | 0bc99a9 | 2011-03-17 04:34:43 +0000 | [diff] [blame] | 958 | comp_cnt = 1; |
| 959 | if (PJMEDIA_ADVERTISE_RTCP && !pjsua_var.media_cfg.ice_no_rtcp) |
| 960 | ++comp_cnt; |
Benny Prijono | f76e139 | 2008-06-06 14:51:48 +0000 | [diff] [blame] | 961 | |
Benny Prijono | bd6613f | 2011-04-11 17:27:14 +0000 | [diff] [blame] | 962 | status = pjmedia_ice_create3(pjsua_var.med_endpt, name, comp_cnt, |
| 963 | &ice_cfg, &ice_cb, 0, call_med, |
| 964 | &call_med->tp); |
Benny Prijono | 0bc99a9 | 2011-03-17 04:34:43 +0000 | [diff] [blame] | 965 | if (status != PJ_SUCCESS) { |
| 966 | pjsua_perror(THIS_FILE, "Unable to create ICE media transport", |
| 967 | status); |
| 968 | goto on_error; |
Benny Prijono | c97608e | 2007-03-23 16:34:20 +0000 | [diff] [blame] | 969 | } |
Benny Prijono | eebe9af | 2006-06-13 22:57:13 +0000 | [diff] [blame] | 970 | |
Benny Prijono | 0bc99a9 | 2011-03-17 04:34:43 +0000 | [diff] [blame] | 971 | /* Wait until transport is initialized, or time out */ |
Sauw Ming | 73ecfe8 | 2011-09-21 10:20:01 +0000 | [diff] [blame] | 972 | if (!async) { |
| 973 | PJSUA_UNLOCK(); |
| 974 | while (call_med->tp_ready == PJ_EPENDING) { |
| 975 | pjsua_handle_events(100); |
| 976 | } |
| 977 | PJSUA_LOCK(); |
Benny Prijono | 0bc99a9 | 2011-03-17 04:34:43 +0000 | [diff] [blame] | 978 | } |
Sauw Ming | 73ecfe8 | 2011-09-21 10:20:01 +0000 | [diff] [blame] | 979 | |
| 980 | if (async && call_med->tp_ready == PJ_EPENDING) { |
| 981 | return PJ_EPENDING; |
| 982 | } else if (call_med->tp_ready != PJ_SUCCESS) { |
Benny Prijono | 0bc99a9 | 2011-03-17 04:34:43 +0000 | [diff] [blame] | 983 | pjsua_perror(THIS_FILE, "Error initializing ICE media transport", |
| 984 | call_med->tp_ready); |
| 985 | status = call_med->tp_ready; |
| 986 | goto on_error; |
| 987 | } |
| 988 | |
| 989 | pjmedia_transport_simulate_lost(call_med->tp, PJMEDIA_DIR_ENCODING, |
| 990 | pjsua_var.media_cfg.tx_drop_pct); |
| 991 | |
| 992 | pjmedia_transport_simulate_lost(call_med->tp, PJMEDIA_DIR_DECODING, |
| 993 | pjsua_var.media_cfg.rx_drop_pct); |
| 994 | |
Benny Prijono | eebe9af | 2006-06-13 22:57:13 +0000 | [diff] [blame] | 995 | return PJ_SUCCESS; |
| 996 | |
| 997 | on_error: |
Benny Prijono | 0bc99a9 | 2011-03-17 04:34:43 +0000 | [diff] [blame] | 998 | if (call_med->tp != NULL) { |
| 999 | pjmedia_transport_close(call_med->tp); |
| 1000 | call_med->tp = NULL; |
Benny Prijono | eebe9af | 2006-06-13 22:57:13 +0000 | [diff] [blame] | 1001 | } |
| 1002 | |
Benny Prijono | c97608e | 2007-03-23 16:34:20 +0000 | [diff] [blame] | 1003 | return status; |
| 1004 | } |
| 1005 | |
Benny Prijono | 0bc99a9 | 2011-03-17 04:34:43 +0000 | [diff] [blame] | 1006 | #if DISABLED_FOR_TICKET_1185 |
| 1007 | /* Create ICE media transports (when ice is enabled) */ |
| 1008 | static pj_status_t create_ice_media_transports(pjsua_transport_config *cfg) |
| 1009 | { |
| 1010 | unsigned i; |
| 1011 | pj_status_t status; |
Benny Prijono | c97608e | 2007-03-23 16:34:20 +0000 | [diff] [blame] | 1012 | |
Benny Prijono | 0bc99a9 | 2011-03-17 04:34:43 +0000 | [diff] [blame] | 1013 | for (i=0; i < pjsua_var.ua_cfg.max_calls; ++i) { |
| 1014 | pjsua_call *call = &pjsua_var.calls[i]; |
| 1015 | unsigned strm_idx; |
| 1016 | |
| 1017 | for (strm_idx=0; strm_idx < call->med_cnt; ++strm_idx) { |
| 1018 | pjsua_call_media *call_med = &call->media[strm_idx]; |
| 1019 | |
| 1020 | status = create_ice_media_transport(cfg, call_med); |
| 1021 | if (status != PJ_SUCCESS) |
| 1022 | goto on_error; |
| 1023 | } |
| 1024 | } |
| 1025 | |
| 1026 | return PJ_SUCCESS; |
| 1027 | |
| 1028 | on_error: |
| 1029 | for (i=0; i < pjsua_var.ua_cfg.max_calls; ++i) { |
| 1030 | pjsua_call *call = &pjsua_var.calls[i]; |
| 1031 | unsigned strm_idx; |
| 1032 | |
| 1033 | for (strm_idx=0; strm_idx < call->med_cnt; ++strm_idx) { |
| 1034 | pjsua_call_media *call_med = &call->media[strm_idx]; |
| 1035 | |
| 1036 | if (call_med->tp) { |
| 1037 | pjmedia_transport_close(call_med->tp); |
| 1038 | call_med->tp = NULL; |
| 1039 | } |
| 1040 | } |
| 1041 | } |
| 1042 | return status; |
| 1043 | } |
| 1044 | #endif |
| 1045 | |
| 1046 | #if DISABLED_FOR_TICKET_1185 |
Benny Prijono | c97608e | 2007-03-23 16:34:20 +0000 | [diff] [blame] | 1047 | /* |
Benny Prijono | 0bc99a9 | 2011-03-17 04:34:43 +0000 | [diff] [blame] | 1048 | * Create media transports for all the calls. This function creates |
Benny Prijono | c97608e | 2007-03-23 16:34:20 +0000 | [diff] [blame] | 1049 | * one UDP media transport for each call. |
| 1050 | */ |
Benny Prijono | 1f61a8f | 2007-08-16 10:11:44 +0000 | [diff] [blame] | 1051 | PJ_DEF(pj_status_t) pjsua_media_transports_create( |
| 1052 | const pjsua_transport_config *app_cfg) |
Benny Prijono | c97608e | 2007-03-23 16:34:20 +0000 | [diff] [blame] | 1053 | { |
| 1054 | pjsua_transport_config cfg; |
| 1055 | unsigned i; |
| 1056 | pj_status_t status; |
| 1057 | |
| 1058 | |
| 1059 | /* Make sure pjsua_init() has been called */ |
| 1060 | PJ_ASSERT_RETURN(pjsua_var.ua_cfg.max_calls>0, PJ_EINVALIDOP); |
| 1061 | |
| 1062 | PJSUA_LOCK(); |
| 1063 | |
| 1064 | /* Delete existing media transports */ |
| 1065 | for (i=0; i<pjsua_var.ua_cfg.max_calls; ++i) { |
Benny Prijono | 0bc99a9 | 2011-03-17 04:34:43 +0000 | [diff] [blame] | 1066 | pjsua_call *call = &pjsua_var.calls[i]; |
| 1067 | unsigned strm_idx; |
| 1068 | |
| 1069 | for (strm_idx=0; strm_idx < call->med_cnt; ++strm_idx) { |
| 1070 | pjsua_call_media *call_med = &call->media[strm_idx]; |
| 1071 | |
| 1072 | if (call_med->tp && call_med->tp_auto_del) { |
| 1073 | pjmedia_transport_close(call_med->tp); |
| 1074 | call_med->tp = NULL; |
| 1075 | call_med->tp_orig = NULL; |
| 1076 | } |
Benny Prijono | c97608e | 2007-03-23 16:34:20 +0000 | [diff] [blame] | 1077 | } |
| 1078 | } |
| 1079 | |
| 1080 | /* Copy config */ |
| 1081 | pjsua_transport_config_dup(pjsua_var.pool, &cfg, app_cfg); |
| 1082 | |
Benny Prijono | 40860c3 | 2008-09-04 13:55:33 +0000 | [diff] [blame] | 1083 | /* Create the transports */ |
Benny Prijono | c97608e | 2007-03-23 16:34:20 +0000 | [diff] [blame] | 1084 | if (pjsua_var.media_cfg.enable_ice) { |
Benny Prijono | 4d79b0f | 2009-10-25 09:02:07 +0000 | [diff] [blame] | 1085 | status = create_ice_media_transports(&cfg); |
Benny Prijono | c97608e | 2007-03-23 16:34:20 +0000 | [diff] [blame] | 1086 | } else { |
| 1087 | status = create_udp_media_transports(&cfg); |
| 1088 | } |
| 1089 | |
Benny Prijono | 40860c3 | 2008-09-04 13:55:33 +0000 | [diff] [blame] | 1090 | /* Set media transport auto_delete to True */ |
| 1091 | for (i=0; i<pjsua_var.ua_cfg.max_calls; ++i) { |
Benny Prijono | 0bc99a9 | 2011-03-17 04:34:43 +0000 | [diff] [blame] | 1092 | pjsua_call *call = &pjsua_var.calls[i]; |
| 1093 | unsigned strm_idx; |
| 1094 | |
| 1095 | for (strm_idx=0; strm_idx < call->med_cnt; ++strm_idx) { |
| 1096 | pjsua_call_media *call_med = &call->media[strm_idx]; |
| 1097 | |
| 1098 | call_med->tp_auto_del = PJ_TRUE; |
| 1099 | } |
Benny Prijono | 40860c3 | 2008-09-04 13:55:33 +0000 | [diff] [blame] | 1100 | } |
Benny Prijono | c97608e | 2007-03-23 16:34:20 +0000 | [diff] [blame] | 1101 | |
Benny Prijono | eebe9af | 2006-06-13 22:57:13 +0000 | [diff] [blame] | 1102 | PJSUA_UNLOCK(); |
| 1103 | |
| 1104 | return status; |
| 1105 | } |
| 1106 | |
Benny Prijono | 40860c3 | 2008-09-04 13:55:33 +0000 | [diff] [blame] | 1107 | /* |
| 1108 | * Attach application's created media transports. |
| 1109 | */ |
| 1110 | PJ_DEF(pj_status_t) pjsua_media_transports_attach(pjsua_media_transport tp[], |
| 1111 | unsigned count, |
| 1112 | pj_bool_t auto_delete) |
| 1113 | { |
| 1114 | unsigned i; |
| 1115 | |
| 1116 | PJ_ASSERT_RETURN(tp && count==pjsua_var.ua_cfg.max_calls, PJ_EINVAL); |
| 1117 | |
| 1118 | /* Assign the media transports */ |
| 1119 | for (i=0; i<pjsua_var.ua_cfg.max_calls; ++i) { |
Benny Prijono | 0bc99a9 | 2011-03-17 04:34:43 +0000 | [diff] [blame] | 1120 | pjsua_call *call = &pjsua_var.calls[i]; |
| 1121 | unsigned strm_idx; |
| 1122 | |
| 1123 | for (strm_idx=0; strm_idx < call->med_cnt; ++strm_idx) { |
| 1124 | pjsua_call_media *call_med = &call->media[strm_idx]; |
| 1125 | |
| 1126 | if (call_med->tp && call_med->tp_auto_del) { |
| 1127 | pjmedia_transport_close(call_med->tp); |
| 1128 | call_med->tp = NULL; |
| 1129 | call_med->tp_orig = NULL; |
| 1130 | } |
Benny Prijono | 40860c3 | 2008-09-04 13:55:33 +0000 | [diff] [blame] | 1131 | } |
| 1132 | |
Benny Prijono | 0bc99a9 | 2011-03-17 04:34:43 +0000 | [diff] [blame] | 1133 | PJ_TODO(remove_pjsua_media_transports_attach); |
| 1134 | |
| 1135 | call->media[0].tp = tp[i].transport; |
| 1136 | call->media[0].tp_auto_del = auto_delete; |
Benny Prijono | 40860c3 | 2008-09-04 13:55:33 +0000 | [diff] [blame] | 1137 | } |
| 1138 | |
| 1139 | return PJ_SUCCESS; |
| 1140 | } |
Benny Prijono | 0bc99a9 | 2011-03-17 04:34:43 +0000 | [diff] [blame] | 1141 | #endif |
Benny Prijono | 40860c3 | 2008-09-04 13:55:33 +0000 | [diff] [blame] | 1142 | |
Benny Prijono | 0bc99a9 | 2011-03-17 04:34:43 +0000 | [diff] [blame] | 1143 | /* Go through the list of media in the SDP, find acceptable media, and |
| 1144 | * sort them based on the "quality" of the media, and store the indexes |
| 1145 | * in the specified array. Media with the best quality will be listed |
| 1146 | * first in the array. The quality factors considered currently is |
| 1147 | * encryption. |
| 1148 | */ |
| 1149 | static void sort_media(const pjmedia_sdp_session *sdp, |
| 1150 | const pj_str_t *type, |
| 1151 | pjmedia_srtp_use use_srtp, |
| 1152 | pj_uint8_t midx[], |
| 1153 | unsigned *p_count) |
Nanang Izzuddin | 1e952a8 | 2010-10-05 16:32:04 +0000 | [diff] [blame] | 1154 | { |
| 1155 | unsigned i; |
Benny Prijono | 0bc99a9 | 2011-03-17 04:34:43 +0000 | [diff] [blame] | 1156 | unsigned count = 0; |
| 1157 | int score[PJSUA_MAX_CALL_MEDIA]; |
Nanang Izzuddin | 1e952a8 | 2010-10-05 16:32:04 +0000 | [diff] [blame] | 1158 | |
Benny Prijono | 0bc99a9 | 2011-03-17 04:34:43 +0000 | [diff] [blame] | 1159 | pj_assert(*p_count >= PJSUA_MAX_CALL_MEDIA); |
| 1160 | |
| 1161 | *p_count = 0; |
Benny Prijono | 09c0d67 | 2011-04-11 05:03:24 +0000 | [diff] [blame] | 1162 | for (i=0; i<PJSUA_MAX_CALL_MEDIA; ++i) |
| 1163 | score[i] = 1; |
Benny Prijono | 0bc99a9 | 2011-03-17 04:34:43 +0000 | [diff] [blame] | 1164 | |
| 1165 | /* Score each media */ |
| 1166 | for (i=0; i<sdp->media_count && count<PJSUA_MAX_CALL_MEDIA; ++i) { |
Nanang Izzuddin | 1e952a8 | 2010-10-05 16:32:04 +0000 | [diff] [blame] | 1167 | const pjmedia_sdp_media *m = sdp->media[i]; |
Nanang Izzuddin | a641429 | 2011-04-08 04:26:18 +0000 | [diff] [blame] | 1168 | const pjmedia_sdp_conn *c; |
Nanang Izzuddin | 1e952a8 | 2010-10-05 16:32:04 +0000 | [diff] [blame] | 1169 | |
Benny Prijono | 0bc99a9 | 2011-03-17 04:34:43 +0000 | [diff] [blame] | 1170 | /* Skip different media */ |
| 1171 | if (pj_stricmp(&m->desc.media, type) != 0) { |
| 1172 | score[count++] = -22000; |
Nanang Izzuddin | 1e952a8 | 2010-10-05 16:32:04 +0000 | [diff] [blame] | 1173 | continue; |
| 1174 | } |
| 1175 | |
Nanang Izzuddin | a641429 | 2011-04-08 04:26:18 +0000 | [diff] [blame] | 1176 | c = m->conn? m->conn : sdp->conn; |
| 1177 | |
Benny Prijono | 0bc99a9 | 2011-03-17 04:34:43 +0000 | [diff] [blame] | 1178 | /* Supported transports */ |
| 1179 | if (pj_stricmp2(&m->desc.transport, "RTP/SAVP")==0) { |
| 1180 | switch (use_srtp) { |
| 1181 | case PJMEDIA_SRTP_MANDATORY: |
| 1182 | case PJMEDIA_SRTP_OPTIONAL: |
| 1183 | ++score[i]; |
Nanang Izzuddin | 1e952a8 | 2010-10-05 16:32:04 +0000 | [diff] [blame] | 1184 | break; |
Benny Prijono | 0bc99a9 | 2011-03-17 04:34:43 +0000 | [diff] [blame] | 1185 | case PJMEDIA_SRTP_DISABLED: |
Nanang Izzuddin | d8c33d8 | 2011-08-18 18:30:55 +0000 | [diff] [blame] | 1186 | //--score[i]; |
| 1187 | score[i] -= 5; |
Benny Prijono | 0bc99a9 | 2011-03-17 04:34:43 +0000 | [diff] [blame] | 1188 | break; |
Nanang Izzuddin | 1e952a8 | 2010-10-05 16:32:04 +0000 | [diff] [blame] | 1189 | } |
Benny Prijono | 0bc99a9 | 2011-03-17 04:34:43 +0000 | [diff] [blame] | 1190 | } else if (pj_stricmp2(&m->desc.transport, "RTP/AVP")==0) { |
| 1191 | switch (use_srtp) { |
| 1192 | case PJMEDIA_SRTP_MANDATORY: |
Nanang Izzuddin | d8c33d8 | 2011-08-18 18:30:55 +0000 | [diff] [blame] | 1193 | //--score[i]; |
| 1194 | score[i] -= 5; |
Benny Prijono | 0bc99a9 | 2011-03-17 04:34:43 +0000 | [diff] [blame] | 1195 | break; |
| 1196 | case PJMEDIA_SRTP_OPTIONAL: |
| 1197 | /* No change in score */ |
| 1198 | break; |
| 1199 | case PJMEDIA_SRTP_DISABLED: |
| 1200 | ++score[i]; |
| 1201 | break; |
| 1202 | } |
| 1203 | } else { |
| 1204 | score[i] -= 10; |
| 1205 | } |
| 1206 | |
| 1207 | /* Is media disabled? */ |
| 1208 | if (m->desc.port == 0) |
| 1209 | score[i] -= 10; |
| 1210 | |
| 1211 | /* Is media inactive? */ |
Nanang Izzuddin | a641429 | 2011-04-08 04:26:18 +0000 | [diff] [blame] | 1212 | if (pjmedia_sdp_media_find_attr2(m, "inactive", NULL) || |
| 1213 | pj_strcmp2(&c->addr, "0.0.0.0") == 0) |
| 1214 | { |
| 1215 | //score[i] -= 10; |
| 1216 | score[i] -= 1; |
| 1217 | } |
Benny Prijono | 0bc99a9 | 2011-03-17 04:34:43 +0000 | [diff] [blame] | 1218 | |
| 1219 | ++count; |
| 1220 | } |
| 1221 | |
| 1222 | /* Created sorted list based on quality */ |
| 1223 | for (i=0; i<count; ++i) { |
| 1224 | unsigned j; |
| 1225 | int best = 0; |
| 1226 | |
| 1227 | for (j=1; j<count; ++j) { |
| 1228 | if (score[j] > score[best]) |
| 1229 | best = j; |
| 1230 | } |
| 1231 | /* Don't put media with negative score, that media is unacceptable |
| 1232 | * for us. |
| 1233 | */ |
| 1234 | if (score[best] >= 0) { |
| 1235 | midx[*p_count] = (pj_uint8_t)best; |
| 1236 | (*p_count)++; |
| 1237 | } |
| 1238 | |
| 1239 | score[best] = -22000; |
| 1240 | |
| 1241 | } |
| 1242 | } |
| 1243 | |
Benny Prijono | ee0ba18 | 2011-07-15 06:18:29 +0000 | [diff] [blame] | 1244 | /* Callback to receive media events */ |
| 1245 | static pj_status_t call_media_on_event(pjmedia_event_subscription *esub, |
| 1246 | pjmedia_event *event) |
| 1247 | { |
| 1248 | pjsua_call_media *call_med = (pjsua_call_media*)esub->user_data; |
| 1249 | pjsua_call *call = call_med->call; |
| 1250 | |
| 1251 | if (pjsua_var.ua_cfg.cb.on_call_media_event && call) { |
| 1252 | ++event->proc_cnt; |
| 1253 | (*pjsua_var.ua_cfg.cb.on_call_media_event)(call->index, |
| 1254 | call_med->idx, event); |
Benny Prijono | 53a7c70 | 2008-04-14 02:57:29 +0000 | [diff] [blame] | 1255 | } |
| 1256 | |
| 1257 | return PJ_SUCCESS; |
| 1258 | } |
| 1259 | |
Sauw Ming | 73ecfe8 | 2011-09-21 10:20:01 +0000 | [diff] [blame] | 1260 | /* Set media transport state and notify the application via the callback. */ |
| 1261 | void set_media_tp_state(pjsua_call_media *call_med, |
| 1262 | pjsua_med_tp_st tp_st) |
| 1263 | { |
| 1264 | if (pjsua_var.ua_cfg.cb.on_call_media_transport_state && |
| 1265 | call_med->tp_st != tp_st) |
| 1266 | { |
| 1267 | pjsua_med_tp_state_info info; |
| 1268 | |
| 1269 | pj_bzero(&info, sizeof(info)); |
| 1270 | info.med_idx = call_med->idx; |
| 1271 | info.state = tp_st; |
| 1272 | info.status = call_med->tp_ready; |
| 1273 | (*pjsua_var.ua_cfg.cb.on_call_media_transport_state)( |
| 1274 | call_med->call->index, &info); |
| 1275 | } |
| 1276 | |
| 1277 | call_med->tp_st = tp_st; |
| 1278 | } |
| 1279 | |
| 1280 | /* Callback to resume pjsua_call_media_init() after media transport |
| 1281 | * creation is completed. |
| 1282 | */ |
| 1283 | static pj_status_t call_media_init_cb(pjsua_call_media *call_med, |
| 1284 | pj_status_t status, |
| 1285 | int security_level, |
| 1286 | int *sip_err_code) |
Nanang Izzuddin | 1e952a8 | 2010-10-05 16:32:04 +0000 | [diff] [blame] | 1287 | { |
Benny Prijono | 0bc99a9 | 2011-03-17 04:34:43 +0000 | [diff] [blame] | 1288 | pjsua_acc *acc = &pjsua_var.acc[call_med->call->acc_id]; |
Sauw Ming | 73ecfe8 | 2011-09-21 10:20:01 +0000 | [diff] [blame] | 1289 | int err_code = 0; |
Nanang Izzuddin | 1e952a8 | 2010-10-05 16:32:04 +0000 | [diff] [blame] | 1290 | |
Sauw Ming | 73ecfe8 | 2011-09-21 10:20:01 +0000 | [diff] [blame] | 1291 | if (status != PJ_SUCCESS) |
| 1292 | goto on_error; |
Nanang Izzuddin | 1e952a8 | 2010-10-05 16:32:04 +0000 | [diff] [blame] | 1293 | |
Sauw Ming | 73ecfe8 | 2011-09-21 10:20:01 +0000 | [diff] [blame] | 1294 | if (call_med->tp_st == PJSUA_MED_TP_CREATING) |
| 1295 | set_media_tp_state(call_med, PJSUA_MED_TP_IDLE); |
Nanang Izzuddin | 1e952a8 | 2010-10-05 16:32:04 +0000 | [diff] [blame] | 1296 | |
Benny Prijono | 0bc99a9 | 2011-03-17 04:34:43 +0000 | [diff] [blame] | 1297 | #if defined(PJMEDIA_HAS_SRTP) && (PJMEDIA_HAS_SRTP != 0) |
| 1298 | /* This function may be called when SRTP transport already exists |
| 1299 | * (e.g: in re-invite, update), don't need to destroy/re-create. |
| 1300 | */ |
| 1301 | if (!call_med->tp_orig || call_med->tp == call_med->tp_orig) { |
| 1302 | pjmedia_srtp_setting srtp_opt; |
| 1303 | pjmedia_transport *srtp = NULL; |
Nanang Izzuddin | 1e952a8 | 2010-10-05 16:32:04 +0000 | [diff] [blame] | 1304 | |
Benny Prijono | 0bc99a9 | 2011-03-17 04:34:43 +0000 | [diff] [blame] | 1305 | /* Check if SRTP requires secure signaling */ |
| 1306 | if (acc->cfg.use_srtp != PJMEDIA_SRTP_DISABLED) { |
| 1307 | if (security_level < acc->cfg.srtp_secure_signaling) { |
Sauw Ming | 73ecfe8 | 2011-09-21 10:20:01 +0000 | [diff] [blame] | 1308 | err_code = PJSIP_SC_NOT_ACCEPTABLE; |
Benny Prijono | 0bc99a9 | 2011-03-17 04:34:43 +0000 | [diff] [blame] | 1309 | status = PJSIP_ESESSIONINSECURE; |
| 1310 | goto on_error; |
| 1311 | } |
| 1312 | } |
| 1313 | |
| 1314 | /* Always create SRTP adapter */ |
| 1315 | pjmedia_srtp_setting_default(&srtp_opt); |
Nanang Izzuddin | b6c239c | 2011-05-10 05:42:28 +0000 | [diff] [blame] | 1316 | srtp_opt.close_member_tp = PJ_TRUE; |
Benny Prijono | 0bc99a9 | 2011-03-17 04:34:43 +0000 | [diff] [blame] | 1317 | /* If media session has been ever established, let's use remote's |
| 1318 | * preference in SRTP usage policy, especially when it is stricter. |
| 1319 | */ |
| 1320 | if (call_med->rem_srtp_use > acc->cfg.use_srtp) |
| 1321 | srtp_opt.use = call_med->rem_srtp_use; |
| 1322 | else |
| 1323 | srtp_opt.use = acc->cfg.use_srtp; |
| 1324 | |
| 1325 | status = pjmedia_transport_srtp_create(pjsua_var.med_endpt, |
| 1326 | call_med->tp, |
| 1327 | &srtp_opt, &srtp); |
| 1328 | if (status != PJ_SUCCESS) { |
Sauw Ming | 73ecfe8 | 2011-09-21 10:20:01 +0000 | [diff] [blame] | 1329 | err_code = PJSIP_SC_INTERNAL_SERVER_ERROR; |
Benny Prijono | 0bc99a9 | 2011-03-17 04:34:43 +0000 | [diff] [blame] | 1330 | goto on_error; |
| 1331 | } |
| 1332 | |
| 1333 | /* Set SRTP as current media transport */ |
| 1334 | call_med->tp_orig = call_med->tp; |
| 1335 | call_med->tp = srtp; |
| 1336 | } |
| 1337 | #else |
Benny Prijono | 7df1934 | 2011-07-23 02:54:03 +0000 | [diff] [blame] | 1338 | call_med->tp_orig = call_med->tp; |
Benny Prijono | 0bc99a9 | 2011-03-17 04:34:43 +0000 | [diff] [blame] | 1339 | PJ_UNUSED_ARG(security_level); |
| 1340 | #endif |
| 1341 | |
Benny Prijono | 1fe04ee | 2011-07-15 07:27:05 +0000 | [diff] [blame] | 1342 | pjmedia_event_subscription_init(&call_med->esub_rend, &call_media_on_event, |
Benny Prijono | ee0ba18 | 2011-07-15 06:18:29 +0000 | [diff] [blame] | 1343 | call_med); |
Benny Prijono | 1fe04ee | 2011-07-15 07:27:05 +0000 | [diff] [blame] | 1344 | pjmedia_event_subscription_init(&call_med->esub_cap, &call_media_on_event, |
Sauw Ming | 73ecfe8 | 2011-09-21 10:20:01 +0000 | [diff] [blame] | 1345 | call_med); |
Benny Prijono | 0bc99a9 | 2011-03-17 04:34:43 +0000 | [diff] [blame] | 1346 | |
| 1347 | on_error: |
Sauw Ming | 73ecfe8 | 2011-09-21 10:20:01 +0000 | [diff] [blame] | 1348 | if (status != PJ_SUCCESS && call_med->tp) { |
Benny Prijono | 0bc99a9 | 2011-03-17 04:34:43 +0000 | [diff] [blame] | 1349 | pjmedia_transport_close(call_med->tp); |
| 1350 | call_med->tp = NULL; |
| 1351 | } |
Sauw Ming | 73ecfe8 | 2011-09-21 10:20:01 +0000 | [diff] [blame] | 1352 | |
| 1353 | if (sip_err_code) |
| 1354 | *sip_err_code = err_code; |
| 1355 | |
| 1356 | if (call_med->med_init_cb) { |
| 1357 | pjsua_med_tp_state_info info; |
| 1358 | |
| 1359 | pj_bzero(&info, sizeof(info)); |
| 1360 | info.status = status; |
| 1361 | info.state = call_med->tp_st; |
| 1362 | info.med_idx = call_med->idx; |
| 1363 | info.sip_err_code = err_code; |
| 1364 | (*call_med->med_init_cb)(call_med->call->index, &info); |
| 1365 | } |
| 1366 | |
| 1367 | return status; |
| 1368 | } |
| 1369 | |
| 1370 | /* Initialize the media line */ |
| 1371 | pj_status_t pjsua_call_media_init(pjsua_call_media *call_med, |
| 1372 | pjmedia_type type, |
| 1373 | const pjsua_transport_config *tcfg, |
| 1374 | int security_level, |
| 1375 | int *sip_err_code, |
| 1376 | pj_bool_t async, |
| 1377 | pjsua_med_tp_state_cb cb) |
| 1378 | { |
| 1379 | pjsua_acc *acc = &pjsua_var.acc[call_med->call->acc_id]; |
| 1380 | pj_status_t status = PJ_SUCCESS; |
| 1381 | |
| 1382 | /* |
| 1383 | * Note: this function may be called when the media already exists |
| 1384 | * (e.g. in reinvites, updates, etc.) |
| 1385 | */ |
| 1386 | call_med->type = type; |
| 1387 | |
| 1388 | /* Create the media transport for initial call. */ |
| 1389 | if (call_med->tp == NULL) { |
| 1390 | #if defined(PJMEDIA_HAS_VIDEO) && (PJMEDIA_HAS_VIDEO != 0) |
| 1391 | /* While in initial call, set default video devices */ |
| 1392 | if (type == PJMEDIA_TYPE_VIDEO) { |
| 1393 | call_med->strm.v.rdr_dev = acc->cfg.vid_rend_dev; |
| 1394 | call_med->strm.v.cap_dev = acc->cfg.vid_cap_dev; |
| 1395 | if (call_med->strm.v.rdr_dev == PJMEDIA_VID_DEFAULT_RENDER_DEV) { |
| 1396 | pjmedia_vid_dev_info info; |
| 1397 | pjmedia_vid_dev_get_info(call_med->strm.v.rdr_dev, &info); |
| 1398 | call_med->strm.v.rdr_dev = info.id; |
| 1399 | } |
| 1400 | if (call_med->strm.v.cap_dev == PJMEDIA_VID_DEFAULT_CAPTURE_DEV) { |
| 1401 | pjmedia_vid_dev_info info; |
| 1402 | pjmedia_vid_dev_get_info(call_med->strm.v.cap_dev, &info); |
| 1403 | call_med->strm.v.cap_dev = info.id; |
| 1404 | } |
| 1405 | } |
| 1406 | #endif |
| 1407 | |
| 1408 | set_media_tp_state(call_med, PJSUA_MED_TP_CREATING); |
| 1409 | |
| 1410 | if (async) { |
| 1411 | call_med->med_create_cb = &call_media_init_cb; |
| 1412 | call_med->med_init_cb = cb; |
| 1413 | } |
| 1414 | |
| 1415 | if (pjsua_var.media_cfg.enable_ice) { |
| 1416 | status = create_ice_media_transport(tcfg, call_med, async); |
| 1417 | } else { |
| 1418 | status = create_udp_media_transport(tcfg, call_med); |
| 1419 | } |
| 1420 | |
| 1421 | if (status == PJ_EPENDING) { |
| 1422 | /* We will resume call media initialization in the |
| 1423 | * on_ice_complete() callback. |
| 1424 | */ |
| 1425 | return PJ_EPENDING; |
| 1426 | } else if (status != PJ_SUCCESS) { |
| 1427 | PJ_PERROR(1,(THIS_FILE, status, "Error creating media transport")); |
| 1428 | return status; |
| 1429 | } |
| 1430 | |
| 1431 | /* Media transport creation completed immediately, so |
| 1432 | * we don't need to call the callback. |
| 1433 | */ |
| 1434 | call_med->med_init_cb = NULL; |
| 1435 | |
| 1436 | } else if (call_med->tp_st == PJSUA_MED_TP_DISABLED) { |
| 1437 | /* Media is being reenabled. */ |
| 1438 | set_media_tp_state(call_med, PJSUA_MED_TP_INIT); |
| 1439 | } |
| 1440 | |
| 1441 | return call_media_init_cb(call_med, status, security_level, |
| 1442 | sip_err_code); |
| 1443 | } |
| 1444 | |
| 1445 | /* Callback to resume pjsua_media_channel_init() after media transport |
| 1446 | * initialization is completed. |
| 1447 | */ |
| 1448 | static pj_status_t media_channel_init_cb(pjsua_call_id call_id, |
| 1449 | const pjsua_med_tp_state_info *info) |
| 1450 | { |
| 1451 | pjsua_call *call = &pjsua_var.calls[call_id]; |
| 1452 | pj_status_t status = (info? info->status : PJ_SUCCESS); |
| 1453 | unsigned mi; |
| 1454 | |
| 1455 | if (info) { |
| 1456 | pj_mutex_lock(call->med_ch_mutex); |
| 1457 | |
| 1458 | /* Set the callback to NULL to indicate that the async operation |
| 1459 | * has completed. |
| 1460 | */ |
| 1461 | call->media[info->med_idx].med_init_cb = NULL; |
| 1462 | |
| 1463 | /* In case of failure, save the information to be returned |
| 1464 | * by the last media transport to finish. |
| 1465 | */ |
| 1466 | if (info->status != PJ_SUCCESS) |
| 1467 | pj_memcpy(&call->med_ch_info, info, sizeof(info)); |
| 1468 | |
| 1469 | /* Check whether all the call's medias have finished calling their |
| 1470 | * callbacks. |
| 1471 | */ |
| 1472 | for (mi=0; mi < call->med_cnt; ++mi) { |
| 1473 | pjsua_call_media *call_med = &call->media[mi]; |
| 1474 | |
| 1475 | if (call_med->med_init_cb) { |
| 1476 | pj_mutex_unlock(call->med_ch_mutex); |
| 1477 | return PJ_SUCCESS; |
| 1478 | } |
| 1479 | |
| 1480 | if (call_med->tp_ready != PJ_SUCCESS) |
| 1481 | status = call_med->tp_ready; |
| 1482 | } |
| 1483 | |
| 1484 | /* OK, we are called by the last media transport finished. */ |
| 1485 | pj_mutex_unlock(call->med_ch_mutex); |
| 1486 | } |
| 1487 | |
| 1488 | if (call->med_ch_mutex) { |
| 1489 | pj_mutex_destroy(call->med_ch_mutex); |
| 1490 | call->med_ch_mutex = NULL; |
| 1491 | } |
| 1492 | |
| 1493 | if (status != PJ_SUCCESS) { |
| 1494 | pjsua_media_channel_deinit(call_id); |
| 1495 | goto on_error; |
| 1496 | } |
| 1497 | |
| 1498 | /* Tell the media transport of a new offer/answer session */ |
| 1499 | for (mi=0; mi < call->med_cnt; ++mi) { |
| 1500 | pjsua_call_media *call_med = &call->media[mi]; |
| 1501 | |
| 1502 | /* Note: tp may be NULL if this media line is disabled */ |
| 1503 | if (call_med->tp && call_med->tp_st == PJSUA_MED_TP_IDLE) { |
Sauw Ming | 99cc8ff | 2011-09-22 04:24:56 +0000 | [diff] [blame^] | 1504 | pj_pool_t *tmp_pool = call->async_call.pool_prov; |
| 1505 | |
| 1506 | if (!tmp_pool) { |
| 1507 | tmp_pool = (call->inv? call->inv->pool_prov: |
| 1508 | call->async_call.dlg->pool); |
| 1509 | } |
Sauw Ming | 73ecfe8 | 2011-09-21 10:20:01 +0000 | [diff] [blame] | 1510 | |
| 1511 | status = pjmedia_transport_media_create( |
| 1512 | call_med->tp, tmp_pool, |
| 1513 | 0, call->async_call.rem_sdp, mi); |
| 1514 | if (status != PJ_SUCCESS) { |
| 1515 | call->med_ch_info.status = status; |
| 1516 | call->med_ch_info.med_idx = mi; |
| 1517 | call->med_ch_info.state = call_med->tp_st; |
| 1518 | call->med_ch_info.sip_err_code = PJSIP_SC_NOT_ACCEPTABLE; |
| 1519 | pjsua_media_channel_deinit(call_id); |
| 1520 | goto on_error; |
| 1521 | } |
| 1522 | |
| 1523 | set_media_tp_state(call_med, PJSUA_MED_TP_INIT); |
| 1524 | } |
| 1525 | } |
| 1526 | |
| 1527 | call->med_ch_info.status = PJ_SUCCESS; |
| 1528 | |
| 1529 | on_error: |
| 1530 | if (call->med_ch_cb) |
| 1531 | (*call->med_ch_cb)(call->index, &call->med_ch_info); |
| 1532 | |
Benny Prijono | 0bc99a9 | 2011-03-17 04:34:43 +0000 | [diff] [blame] | 1533 | return status; |
| 1534 | } |
Nanang Izzuddin | 1e952a8 | 2010-10-05 16:32:04 +0000 | [diff] [blame] | 1535 | |
Benny Prijono | d817965 | 2008-01-23 20:39:07 +0000 | [diff] [blame] | 1536 | pj_status_t pjsua_media_channel_init(pjsua_call_id call_id, |
| 1537 | pjsip_role_e role, |
| 1538 | int security_level, |
| 1539 | pj_pool_t *tmp_pool, |
| 1540 | const pjmedia_sdp_session *rem_sdp, |
Sauw Ming | 73ecfe8 | 2011-09-21 10:20:01 +0000 | [diff] [blame] | 1541 | int *sip_err_code, |
| 1542 | pj_bool_t async, |
| 1543 | pjsua_med_tp_state_cb cb) |
Benny Prijono | c97608e | 2007-03-23 16:34:20 +0000 | [diff] [blame] | 1544 | { |
Benny Prijono | 0bc99a9 | 2011-03-17 04:34:43 +0000 | [diff] [blame] | 1545 | const pj_str_t STR_AUDIO = { "audio", 5 }; |
| 1546 | const pj_str_t STR_VIDEO = { "video", 5 }; |
Benny Prijono | d817965 | 2008-01-23 20:39:07 +0000 | [diff] [blame] | 1547 | pjsua_call *call = &pjsua_var.calls[call_id]; |
Benny Prijono | d817965 | 2008-01-23 20:39:07 +0000 | [diff] [blame] | 1548 | pjsua_acc *acc = &pjsua_var.acc[call->acc_id]; |
Benny Prijono | 0bc99a9 | 2011-03-17 04:34:43 +0000 | [diff] [blame] | 1549 | pj_uint8_t maudidx[PJSUA_MAX_CALL_MEDIA]; |
| 1550 | unsigned maudcnt = PJ_ARRAY_SIZE(maudidx); |
| 1551 | pj_uint8_t mvididx[PJSUA_MAX_CALL_MEDIA]; |
| 1552 | unsigned mvidcnt = PJ_ARRAY_SIZE(mvididx); |
| 1553 | pjmedia_type media_types[PJSUA_MAX_CALL_MEDIA]; |
| 1554 | unsigned mi; |
Sauw Ming | 73ecfe8 | 2011-09-21 10:20:01 +0000 | [diff] [blame] | 1555 | pj_bool_t pending_med_tp = PJ_FALSE; |
Benny Prijono | 0bc99a9 | 2011-03-17 04:34:43 +0000 | [diff] [blame] | 1556 | pj_status_t status; |
Benny Prijono | c97608e | 2007-03-23 16:34:20 +0000 | [diff] [blame] | 1557 | |
Benny Prijono | d817965 | 2008-01-23 20:39:07 +0000 | [diff] [blame] | 1558 | PJ_UNUSED_ARG(role); |
| 1559 | |
Nanang Izzuddin | b6c239c | 2011-05-10 05:42:28 +0000 | [diff] [blame] | 1560 | /* |
| 1561 | * Note: this function may be called when the media already exists |
| 1562 | * (e.g. in reinvites, updates, etc). |
| 1563 | */ |
| 1564 | |
Benny Prijono | 0bc99a9 | 2011-03-17 04:34:43 +0000 | [diff] [blame] | 1565 | if (pjsua_get_state() != PJSUA_STATE_RUNNING) |
| 1566 | return PJ_EBUSY; |
| 1567 | |
Sauw Ming | 73ecfe8 | 2011-09-21 10:20:01 +0000 | [diff] [blame] | 1568 | if (async) { |
| 1569 | pj_pool_t *tmppool = (call->inv? call->inv->pool_prov: |
| 1570 | call->async_call.dlg->pool); |
| 1571 | |
| 1572 | status = pj_mutex_create_simple(tmppool, NULL, &call->med_ch_mutex); |
| 1573 | if (status != PJ_SUCCESS) |
| 1574 | return status; |
| 1575 | } |
| 1576 | |
Benny Prijono | b90fd38 | 2011-09-18 14:59:56 +0000 | [diff] [blame] | 1577 | PJ_LOG(4,(THIS_FILE, "Call %d: initializing media..", call_id)); |
| 1578 | pj_log_push_indent(); |
| 1579 | |
Benny Prijono | 0bc99a9 | 2011-03-17 04:34:43 +0000 | [diff] [blame] | 1580 | #if DISABLED_FOR_TICKET_1185 |
Benny Prijono | d817965 | 2008-01-23 20:39:07 +0000 | [diff] [blame] | 1581 | /* Return error if media transport has not been created yet |
Benny Prijono | 68f9e4f | 2008-03-21 08:56:02 +0000 | [diff] [blame] | 1582 | * (e.g. application is starting) |
| 1583 | */ |
Benny Prijono | 0bc99a9 | 2011-03-17 04:34:43 +0000 | [diff] [blame] | 1584 | for (i=0; i<call->med_cnt; ++i) { |
| 1585 | if (call->media[i].tp == NULL) { |
Benny Prijono | b90fd38 | 2011-09-18 14:59:56 +0000 | [diff] [blame] | 1586 | status = PJ_EBUSY; |
| 1587 | goto on_error; |
Benny Prijono | d817965 | 2008-01-23 20:39:07 +0000 | [diff] [blame] | 1588 | } |
Benny Prijono | c97608e | 2007-03-23 16:34:20 +0000 | [diff] [blame] | 1589 | } |
Benny Prijono | d817965 | 2008-01-23 20:39:07 +0000 | [diff] [blame] | 1590 | #endif |
Benny Prijono | c97608e | 2007-03-23 16:34:20 +0000 | [diff] [blame] | 1591 | |
Benny Prijono | 0bc99a9 | 2011-03-17 04:34:43 +0000 | [diff] [blame] | 1592 | if (rem_sdp) { |
| 1593 | sort_media(rem_sdp, &STR_AUDIO, acc->cfg.use_srtp, |
| 1594 | maudidx, &maudcnt); |
Nanang Izzuddin | d8c33d8 | 2011-08-18 18:30:55 +0000 | [diff] [blame] | 1595 | // Don't apply media count limitation until SDP negotiation is done. |
| 1596 | //if (maudcnt > acc->cfg.max_audio_cnt) |
| 1597 | // maudcnt = acc->cfg.max_audio_cnt; |
Benny Prijono | c97608e | 2007-03-23 16:34:20 +0000 | [diff] [blame] | 1598 | |
Benny Prijono | 0bc99a9 | 2011-03-17 04:34:43 +0000 | [diff] [blame] | 1599 | if (maudcnt==0) { |
| 1600 | /* Expecting audio in the offer */ |
| 1601 | if (sip_err_code) *sip_err_code = PJSIP_SC_NOT_ACCEPTABLE_HERE; |
| 1602 | pjsua_media_channel_deinit(call_id); |
Benny Prijono | b90fd38 | 2011-09-18 14:59:56 +0000 | [diff] [blame] | 1603 | status = PJSIP_ERRNO_FROM_SIP_STATUS(PJSIP_SC_NOT_ACCEPTABLE_HERE); |
| 1604 | goto on_error; |
Benny Prijono | c97608e | 2007-03-23 16:34:20 +0000 | [diff] [blame] | 1605 | } |
| 1606 | |
Nanang Izzuddin | d8c33d8 | 2011-08-18 18:30:55 +0000 | [diff] [blame] | 1607 | #if PJMEDIA_HAS_VIDEO |
Benny Prijono | 0bc99a9 | 2011-03-17 04:34:43 +0000 | [diff] [blame] | 1608 | sort_media(rem_sdp, &STR_VIDEO, acc->cfg.use_srtp, |
| 1609 | mvididx, &mvidcnt); |
Nanang Izzuddin | d8c33d8 | 2011-08-18 18:30:55 +0000 | [diff] [blame] | 1610 | // Don't apply media count limitation until SDP negotiation is done. |
| 1611 | //if (mvidcnt > acc->cfg.max_video_cnt) |
| 1612 | //mvidcnt = acc->cfg.max_video_cnt; |
| 1613 | #else |
| 1614 | mvidcnt = 0; |
| 1615 | #endif |
Nanang Izzuddin | b6c239c | 2011-05-10 05:42:28 +0000 | [diff] [blame] | 1616 | |
| 1617 | /* Update media count only when remote add any media, this media count |
| 1618 | * must never decrease. |
Benny Prijono | d817965 | 2008-01-23 20:39:07 +0000 | [diff] [blame] | 1619 | */ |
Nanang Izzuddin | b6c239c | 2011-05-10 05:42:28 +0000 | [diff] [blame] | 1620 | if (call->med_cnt < rem_sdp->media_count) |
| 1621 | call->med_cnt = PJ_MIN(rem_sdp->media_count, PJSUA_MAX_CALL_MEDIA); |
Benny Prijono | d817965 | 2008-01-23 20:39:07 +0000 | [diff] [blame] | 1622 | |
Benny Prijono | 0bc99a9 | 2011-03-17 04:34:43 +0000 | [diff] [blame] | 1623 | } else { |
| 1624 | maudcnt = acc->cfg.max_audio_cnt; |
| 1625 | for (mi=0; mi<maudcnt; ++mi) { |
Nanang Izzuddin | bf26db1 | 2011-03-18 07:54:50 +0000 | [diff] [blame] | 1626 | maudidx[mi] = (pj_uint8_t)mi; |
Benny Prijono | 0bc99a9 | 2011-03-17 04:34:43 +0000 | [diff] [blame] | 1627 | media_types[mi] = PJMEDIA_TYPE_AUDIO; |
Benny Prijono | d817965 | 2008-01-23 20:39:07 +0000 | [diff] [blame] | 1628 | } |
Nanang Izzuddin | d8c33d8 | 2011-08-18 18:30:55 +0000 | [diff] [blame] | 1629 | #if PJMEDIA_HAS_VIDEO |
Benny Prijono | 0bc99a9 | 2011-03-17 04:34:43 +0000 | [diff] [blame] | 1630 | mvidcnt = acc->cfg.max_video_cnt; |
| 1631 | for (mi=0; mi<mvidcnt; ++mi) { |
| 1632 | media_types[maudcnt + mi] = PJMEDIA_TYPE_VIDEO; |
| 1633 | } |
Nanang Izzuddin | d8c33d8 | 2011-08-18 18:30:55 +0000 | [diff] [blame] | 1634 | #else |
| 1635 | mvidcnt = 0; |
| 1636 | #endif |
Nanang Izzuddin | b6c239c | 2011-05-10 05:42:28 +0000 | [diff] [blame] | 1637 | call->med_cnt = maudcnt + mvidcnt; |
Benny Prijono | a310bd2 | 2008-06-27 21:19:44 +0000 | [diff] [blame] | 1638 | } |
| 1639 | |
Benny Prijono | 0bc99a9 | 2011-03-17 04:34:43 +0000 | [diff] [blame] | 1640 | if (call->med_cnt == 0) { |
| 1641 | /* Expecting at least one media */ |
Benny Prijono | ab8dba9 | 2008-06-27 21:59:15 +0000 | [diff] [blame] | 1642 | if (sip_err_code) *sip_err_code = PJSIP_SC_NOT_ACCEPTABLE_HERE; |
Benny Prijono | a310bd2 | 2008-06-27 21:19:44 +0000 | [diff] [blame] | 1643 | pjsua_media_channel_deinit(call_id); |
Benny Prijono | b90fd38 | 2011-09-18 14:59:56 +0000 | [diff] [blame] | 1644 | status = PJSIP_ERRNO_FROM_SIP_STATUS(PJSIP_SC_NOT_ACCEPTABLE_HERE); |
| 1645 | goto on_error; |
Benny Prijono | a310bd2 | 2008-06-27 21:19:44 +0000 | [diff] [blame] | 1646 | } |
| 1647 | |
Sauw Ming | 73ecfe8 | 2011-09-21 10:20:01 +0000 | [diff] [blame] | 1648 | if (async) { |
| 1649 | call->med_ch_cb = cb; |
| 1650 | if (rem_sdp) { |
| 1651 | /* TODO: change rem_sdp to non-const parameter. */ |
| 1652 | call->async_call.rem_sdp = |
| 1653 | pjmedia_sdp_session_clone(call->inv->pool_prov, rem_sdp); |
| 1654 | } |
| 1655 | } |
| 1656 | |
Sauw Ming | 99cc8ff | 2011-09-22 04:24:56 +0000 | [diff] [blame^] | 1657 | call->async_call.pool_prov = tmp_pool; |
| 1658 | |
Benny Prijono | 0bc99a9 | 2011-03-17 04:34:43 +0000 | [diff] [blame] | 1659 | /* Initialize each media line */ |
| 1660 | for (mi=0; mi < call->med_cnt; ++mi) { |
| 1661 | pjsua_call_media *call_med = &call->media[mi]; |
| 1662 | pj_bool_t enabled = PJ_FALSE; |
| 1663 | pjmedia_type media_type = PJMEDIA_TYPE_NONE; |
Benny Prijono | a310bd2 | 2008-06-27 21:19:44 +0000 | [diff] [blame] | 1664 | |
Benny Prijono | 0bc99a9 | 2011-03-17 04:34:43 +0000 | [diff] [blame] | 1665 | if (rem_sdp) { |
Nanang Izzuddin | b6c239c | 2011-05-10 05:42:28 +0000 | [diff] [blame] | 1666 | if (mi >= rem_sdp->media_count) { |
| 1667 | /* Media has been removed in remote re-offer */ |
| 1668 | media_type = call_med->type; |
| 1669 | } else if (!pj_stricmp(&rem_sdp->media[mi]->desc.media, &STR_AUDIO)) { |
Benny Prijono | 0bc99a9 | 2011-03-17 04:34:43 +0000 | [diff] [blame] | 1670 | media_type = PJMEDIA_TYPE_AUDIO; |
| 1671 | if (pj_memchr(maudidx, mi, maudcnt * sizeof(maudidx[0]))) { |
| 1672 | enabled = PJ_TRUE; |
| 1673 | } |
| 1674 | } |
| 1675 | else if (!pj_stricmp(&rem_sdp->media[mi]->desc.media, &STR_VIDEO)) { |
| 1676 | media_type = PJMEDIA_TYPE_VIDEO; |
| 1677 | if (pj_memchr(mvididx, mi, mvidcnt * sizeof(mvididx[0]))) { |
| 1678 | enabled = PJ_TRUE; |
| 1679 | } |
| 1680 | } |
| 1681 | |
| 1682 | } else { |
| 1683 | enabled = PJ_TRUE; |
| 1684 | media_type = media_types[mi]; |
| 1685 | } |
| 1686 | |
| 1687 | if (enabled) { |
| 1688 | status = pjsua_call_media_init(call_med, media_type, |
| 1689 | &acc->cfg.rtp_cfg, |
Sauw Ming | 73ecfe8 | 2011-09-21 10:20:01 +0000 | [diff] [blame] | 1690 | security_level, sip_err_code, |
| 1691 | async, |
| 1692 | (async? (pjsua_med_tp_state_cb) |
| 1693 | &media_channel_init_cb: NULL)); |
| 1694 | if (status == PJ_EPENDING) { |
| 1695 | pending_med_tp = PJ_TRUE; |
| 1696 | } else if (status != PJ_SUCCESS) { |
| 1697 | if (pending_med_tp) { |
| 1698 | /* Save failure information. */ |
| 1699 | call_med->tp_ready = status; |
| 1700 | pj_bzero(&call->med_ch_info, sizeof(call->med_ch_info)); |
| 1701 | call->med_ch_info.status = status; |
| 1702 | call->med_ch_info.state = call_med->tp_st; |
| 1703 | call->med_ch_info.med_idx = call_med->idx; |
| 1704 | if (sip_err_code) |
| 1705 | call->med_ch_info.sip_err_code = *sip_err_code; |
| 1706 | |
| 1707 | /* We will return failure in the callback later. */ |
| 1708 | return PJ_EPENDING; |
| 1709 | } |
| 1710 | |
| 1711 | pjsua_media_channel_deinit(call_id); |
Benny Prijono | b90fd38 | 2011-09-18 14:59:56 +0000 | [diff] [blame] | 1712 | goto on_error; |
Benny Prijono | 0bc99a9 | 2011-03-17 04:34:43 +0000 | [diff] [blame] | 1713 | } |
| 1714 | } else { |
Nanang Izzuddin | b6c239c | 2011-05-10 05:42:28 +0000 | [diff] [blame] | 1715 | /* By convention, the media is disabled if transport is NULL |
| 1716 | * or transport state is PJSUA_MED_TP_DISABLED. |
| 1717 | */ |
Benny Prijono | 0bc99a9 | 2011-03-17 04:34:43 +0000 | [diff] [blame] | 1718 | if (call_med->tp) { |
Nanang Izzuddin | b6c239c | 2011-05-10 05:42:28 +0000 | [diff] [blame] | 1719 | // Don't close transport here, as SDP negotiation has not been |
| 1720 | // done and stream may be still active. |
| 1721 | //pjmedia_transport_close(call_med->tp); |
| 1722 | //call_med->tp = NULL; |
| 1723 | pj_assert(call_med->tp_st == PJSUA_MED_TP_INIT || |
| 1724 | call_med->tp_st == PJSUA_MED_TP_RUNNING); |
Sauw Ming | 73ecfe8 | 2011-09-21 10:20:01 +0000 | [diff] [blame] | 1725 | set_media_tp_state(call_med, PJSUA_MED_TP_DISABLED); |
Benny Prijono | 0bc99a9 | 2011-03-17 04:34:43 +0000 | [diff] [blame] | 1726 | } |
Nanang Izzuddin | b6c239c | 2011-05-10 05:42:28 +0000 | [diff] [blame] | 1727 | |
| 1728 | /* Put media type just for info */ |
| 1729 | call_med->type = media_type; |
Benny Prijono | 0bc99a9 | 2011-03-17 04:34:43 +0000 | [diff] [blame] | 1730 | } |
Benny Prijono | 224b4e2 | 2008-06-19 14:10:28 +0000 | [diff] [blame] | 1731 | } |
| 1732 | |
Benny Prijono | 0bc99a9 | 2011-03-17 04:34:43 +0000 | [diff] [blame] | 1733 | call->audio_idx = maudidx[0]; |
| 1734 | |
| 1735 | PJ_LOG(4,(THIS_FILE, "Media index %d selected for audio call %d", |
| 1736 | call->audio_idx, call->index)); |
| 1737 | |
Sauw Ming | 73ecfe8 | 2011-09-21 10:20:01 +0000 | [diff] [blame] | 1738 | if (pending_med_tp) { |
Sauw Ming | 99cc8ff | 2011-09-22 04:24:56 +0000 | [diff] [blame^] | 1739 | /* We shouldn't use temporary pool anymore. */ |
| 1740 | call->async_call.pool_prov = NULL; |
Sauw Ming | 73ecfe8 | 2011-09-21 10:20:01 +0000 | [diff] [blame] | 1741 | /* We have a pending media transport initialization. */ |
| 1742 | pj_log_pop_indent(); |
| 1743 | return PJ_EPENDING; |
Benny Prijono | 0bc99a9 | 2011-03-17 04:34:43 +0000 | [diff] [blame] | 1744 | } |
| 1745 | |
Sauw Ming | 73ecfe8 | 2011-09-21 10:20:01 +0000 | [diff] [blame] | 1746 | /* Media transport initialization completed immediately, so |
| 1747 | * we don't need to call the callback. |
| 1748 | */ |
| 1749 | call->med_ch_cb = NULL; |
| 1750 | |
| 1751 | status = media_channel_init_cb(call_id, NULL); |
| 1752 | if (status != PJ_SUCCESS && sip_err_code) |
| 1753 | *sip_err_code = call->med_ch_info.sip_err_code; |
| 1754 | |
Benny Prijono | b90fd38 | 2011-09-18 14:59:56 +0000 | [diff] [blame] | 1755 | pj_log_pop_indent(); |
Sauw Ming | 73ecfe8 | 2011-09-21 10:20:01 +0000 | [diff] [blame] | 1756 | return status; |
Benny Prijono | b90fd38 | 2011-09-18 14:59:56 +0000 | [diff] [blame] | 1757 | |
| 1758 | on_error: |
Sauw Ming | 73ecfe8 | 2011-09-21 10:20:01 +0000 | [diff] [blame] | 1759 | if (call->med_ch_mutex) { |
| 1760 | pj_mutex_destroy(call->med_ch_mutex); |
| 1761 | call->med_ch_mutex = NULL; |
| 1762 | } |
| 1763 | |
Benny Prijono | b90fd38 | 2011-09-18 14:59:56 +0000 | [diff] [blame] | 1764 | pj_log_pop_indent(); |
| 1765 | return status; |
Benny Prijono | c97608e | 2007-03-23 16:34:20 +0000 | [diff] [blame] | 1766 | } |
| 1767 | |
| 1768 | pj_status_t pjsua_media_channel_create_sdp(pjsua_call_id call_id, |
| 1769 | pj_pool_t *pool, |
Benny Prijono | d817965 | 2008-01-23 20:39:07 +0000 | [diff] [blame] | 1770 | const pjmedia_sdp_session *rem_sdp, |
Benny Prijono | 25b2ea1 | 2008-01-24 19:20:54 +0000 | [diff] [blame] | 1771 | pjmedia_sdp_session **p_sdp, |
Benny Prijono | 0bc99a9 | 2011-03-17 04:34:43 +0000 | [diff] [blame] | 1772 | int *sip_err_code) |
Benny Prijono | c97608e | 2007-03-23 16:34:20 +0000 | [diff] [blame] | 1773 | { |
Benny Prijono | 0bc99a9 | 2011-03-17 04:34:43 +0000 | [diff] [blame] | 1774 | enum { MAX_MEDIA = PJSUA_MAX_CALL_MEDIA }; |
Benny Prijono | c97608e | 2007-03-23 16:34:20 +0000 | [diff] [blame] | 1775 | pjmedia_sdp_session *sdp; |
Benny Prijono | 0bc99a9 | 2011-03-17 04:34:43 +0000 | [diff] [blame] | 1776 | pj_sockaddr origin; |
Benny Prijono | c97608e | 2007-03-23 16:34:20 +0000 | [diff] [blame] | 1777 | pjsua_call *call = &pjsua_var.calls[call_id]; |
Nanang Izzuddin | 1e952a8 | 2010-10-05 16:32:04 +0000 | [diff] [blame] | 1778 | pjmedia_sdp_neg_state sdp_neg_state = PJMEDIA_SDP_NEG_STATE_NULL; |
Benny Prijono | 0bc99a9 | 2011-03-17 04:34:43 +0000 | [diff] [blame] | 1779 | unsigned mi; |
Benny Prijono | c97608e | 2007-03-23 16:34:20 +0000 | [diff] [blame] | 1780 | pj_status_t status; |
| 1781 | |
Benny Prijono | 0bc99a9 | 2011-03-17 04:34:43 +0000 | [diff] [blame] | 1782 | if (pjsua_get_state() != PJSUA_STATE_RUNNING) |
Benny Prijono | 55e8235 | 2007-05-10 20:49:08 +0000 | [diff] [blame] | 1783 | return PJ_EBUSY; |
Benny Prijono | 55e8235 | 2007-05-10 20:49:08 +0000 | [diff] [blame] | 1784 | |
Benny Prijono | 0bc99a9 | 2011-03-17 04:34:43 +0000 | [diff] [blame] | 1785 | if (rem_sdp) { |
Nanang Izzuddin | b6c239c | 2011-05-10 05:42:28 +0000 | [diff] [blame] | 1786 | /* If this is a re-offer, let's re-initialize media as remote may |
| 1787 | * add or remove media |
| 1788 | */ |
| 1789 | if (call->inv && call->inv->state == PJSIP_INV_STATE_CONFIRMED) { |
| 1790 | status = pjsua_media_channel_init(call_id, PJSIP_ROLE_UAS, |
| 1791 | call->secure_level, pool, |
Sauw Ming | 73ecfe8 | 2011-09-21 10:20:01 +0000 | [diff] [blame] | 1792 | rem_sdp, sip_err_code, |
| 1793 | PJ_FALSE, NULL); |
Nanang Izzuddin | b6c239c | 2011-05-10 05:42:28 +0000 | [diff] [blame] | 1794 | if (status != PJ_SUCCESS) |
| 1795 | return status; |
Benny Prijono | 0324ba5 | 2010-12-02 10:41:46 +0000 | [diff] [blame] | 1796 | } |
Nanang Izzuddin | b6c239c | 2011-05-10 05:42:28 +0000 | [diff] [blame] | 1797 | |
| 1798 | #if 0 |
| 1799 | pjsua_acc *acc = &pjsua_var.acc[call->acc_id]; |
Benny Prijono | 0bc99a9 | 2011-03-17 04:34:43 +0000 | [diff] [blame] | 1800 | pj_uint8_t maudidx[PJSUA_MAX_CALL_MEDIA]; |
| 1801 | unsigned maudcnt = PJ_ARRAY_SIZE(maudidx); |
Nanang Izzuddin | 3150d8b | 2010-12-01 08:20:28 +0000 | [diff] [blame] | 1802 | |
Benny Prijono | 0bc99a9 | 2011-03-17 04:34:43 +0000 | [diff] [blame] | 1803 | sort_media(rem_sdp, &STR_AUDIO, acc->cfg.use_srtp, |
| 1804 | maudidx, &maudcnt); |
Nanang Izzuddin | 3150d8b | 2010-12-01 08:20:28 +0000 | [diff] [blame] | 1805 | |
Benny Prijono | 0bc99a9 | 2011-03-17 04:34:43 +0000 | [diff] [blame] | 1806 | if (maudcnt==0) { |
| 1807 | /* Expecting audio in the offer */ |
| 1808 | if (sip_err_code) *sip_err_code = PJSIP_SC_NOT_ACCEPTABLE_HERE; |
| 1809 | pjsua_media_channel_deinit(call_id); |
| 1810 | return PJSIP_ERRNO_FROM_SIP_STATUS(PJSIP_SC_NOT_ACCEPTABLE_HERE); |
Benny Prijono | 224b4e2 | 2008-06-19 14:10:28 +0000 | [diff] [blame] | 1811 | } |
Benny Prijono | 224b4e2 | 2008-06-19 14:10:28 +0000 | [diff] [blame] | 1812 | |
Benny Prijono | 0bc99a9 | 2011-03-17 04:34:43 +0000 | [diff] [blame] | 1813 | call->audio_idx = maudidx[0]; |
Nanang Izzuddin | b6c239c | 2011-05-10 05:42:28 +0000 | [diff] [blame] | 1814 | #endif |
Benny Prijono | 0bc99a9 | 2011-03-17 04:34:43 +0000 | [diff] [blame] | 1815 | } else { |
| 1816 | /* Audio is first in our offer, by convention */ |
Nanang Izzuddin | b6c239c | 2011-05-10 05:42:28 +0000 | [diff] [blame] | 1817 | // The audio_idx should not be changed here, as this function may be |
| 1818 | // called in generating re-offer and the current active audio index |
| 1819 | // can be anywhere. |
| 1820 | //call->audio_idx = 0; |
Benny Prijono | 224b4e2 | 2008-06-19 14:10:28 +0000 | [diff] [blame] | 1821 | } |
| 1822 | |
Nanang Izzuddin | b6c239c | 2011-05-10 05:42:28 +0000 | [diff] [blame] | 1823 | #if 0 |
| 1824 | // Since r3512, old-style hold should have got transport, created by |
| 1825 | // pjsua_media_channel_init() in initial offer/answer or remote reoffer. |
Benny Prijono | 224b4e2 | 2008-06-19 14:10:28 +0000 | [diff] [blame] | 1826 | /* Create media if it's not created. This could happen when call is |
Benny Prijono | 0bc99a9 | 2011-03-17 04:34:43 +0000 | [diff] [blame] | 1827 | * currently on-hold (with the old style hold) |
Benny Prijono | 224b4e2 | 2008-06-19 14:10:28 +0000 | [diff] [blame] | 1828 | */ |
Benny Prijono | 0bc99a9 | 2011-03-17 04:34:43 +0000 | [diff] [blame] | 1829 | if (call->media[call->audio_idx].tp == NULL) { |
Benny Prijono | 224b4e2 | 2008-06-19 14:10:28 +0000 | [diff] [blame] | 1830 | pjsip_role_e role; |
| 1831 | role = (rem_sdp ? PJSIP_ROLE_UAS : PJSIP_ROLE_UAC); |
| 1832 | status = pjsua_media_channel_init(call_id, role, call->secure_level, |
Benny Prijono | 0bc99a9 | 2011-03-17 04:34:43 +0000 | [diff] [blame] | 1833 | pool, rem_sdp, sip_err_code); |
Benny Prijono | 224b4e2 | 2008-06-19 14:10:28 +0000 | [diff] [blame] | 1834 | if (status != PJ_SUCCESS) |
| 1835 | return status; |
| 1836 | } |
Nanang Izzuddin | b6c239c | 2011-05-10 05:42:28 +0000 | [diff] [blame] | 1837 | #endif |
Benny Prijono | 224b4e2 | 2008-06-19 14:10:28 +0000 | [diff] [blame] | 1838 | |
Nanang Izzuddin | 1e952a8 | 2010-10-05 16:32:04 +0000 | [diff] [blame] | 1839 | /* Get SDP negotiator state */ |
| 1840 | if (call->inv && call->inv->neg) |
| 1841 | sdp_neg_state = pjmedia_sdp_neg_get_state(call->inv->neg); |
| 1842 | |
Benny Prijono | 0bc99a9 | 2011-03-17 04:34:43 +0000 | [diff] [blame] | 1843 | /* Get one address to use in the origin field */ |
| 1844 | pj_bzero(&origin, sizeof(origin)); |
| 1845 | for (mi=0; mi<call->med_cnt; ++mi) { |
| 1846 | pjmedia_transport_info tpinfo; |
Benny Prijono | 617c5bc | 2007-04-02 19:51:21 +0000 | [diff] [blame] | 1847 | |
Benny Prijono | 0bc99a9 | 2011-03-17 04:34:43 +0000 | [diff] [blame] | 1848 | if (call->media[mi].tp == NULL) |
| 1849 | continue; |
| 1850 | |
| 1851 | pjmedia_transport_info_init(&tpinfo); |
| 1852 | pjmedia_transport_get_info(call->media[mi].tp, &tpinfo); |
| 1853 | pj_sockaddr_cp(&origin, &tpinfo.sock_info.rtp_addr_name); |
| 1854 | break; |
Benny Prijono | 25b2ea1 | 2008-01-24 19:20:54 +0000 | [diff] [blame] | 1855 | } |
Benny Prijono | c97608e | 2007-03-23 16:34:20 +0000 | [diff] [blame] | 1856 | |
Benny Prijono | 0bc99a9 | 2011-03-17 04:34:43 +0000 | [diff] [blame] | 1857 | /* Create the base (blank) SDP */ |
| 1858 | status = pjmedia_endpt_create_base_sdp(pjsua_var.med_endpt, pool, NULL, |
| 1859 | &origin, &sdp); |
| 1860 | if (status != PJ_SUCCESS) |
| 1861 | return status; |
Benny Prijono | a310bd2 | 2008-06-27 21:19:44 +0000 | [diff] [blame] | 1862 | |
Benny Prijono | 0bc99a9 | 2011-03-17 04:34:43 +0000 | [diff] [blame] | 1863 | /* Process each media line */ |
| 1864 | for (mi=0; mi<call->med_cnt; ++mi) { |
| 1865 | pjsua_call_media *call_med = &call->media[mi]; |
| 1866 | pjmedia_sdp_media *m = NULL; |
| 1867 | pjmedia_transport_info tpinfo; |
| 1868 | |
Nanang Izzuddin | b6c239c | 2011-05-10 05:42:28 +0000 | [diff] [blame] | 1869 | if (rem_sdp && mi >= rem_sdp->media_count) { |
| 1870 | /* Remote might have removed some media lines. */ |
| 1871 | break; |
Nanang Izzuddin | 1e952a8 | 2010-10-05 16:32:04 +0000 | [diff] [blame] | 1872 | } |
| 1873 | |
Nanang Izzuddin | b6c239c | 2011-05-10 05:42:28 +0000 | [diff] [blame] | 1874 | if (call_med->tp == NULL || call_med->tp_st == PJSUA_MED_TP_DISABLED) |
| 1875 | { |
Benny Prijono | 0bc99a9 | 2011-03-17 04:34:43 +0000 | [diff] [blame] | 1876 | /* |
Nanang Izzuddin | b6c239c | 2011-05-10 05:42:28 +0000 | [diff] [blame] | 1877 | * This media is disabled. Just create a valid SDP with zero |
Benny Prijono | 0bc99a9 | 2011-03-17 04:34:43 +0000 | [diff] [blame] | 1878 | * port. |
| 1879 | */ |
Nanang Izzuddin | d8c33d8 | 2011-08-18 18:30:55 +0000 | [diff] [blame] | 1880 | if (rem_sdp) { |
| 1881 | /* Just clone the remote media and deactivate it */ |
| 1882 | m = pjmedia_sdp_media_clone_deactivate(pool, |
| 1883 | rem_sdp->media[mi]); |
| 1884 | } else { |
| 1885 | m = PJ_POOL_ZALLOC_T(pool, pjmedia_sdp_media); |
| 1886 | m->desc.transport = pj_str("RTP/AVP"); |
| 1887 | m->desc.fmt_count = 1; |
| 1888 | m->conn = PJ_POOL_ZALLOC_T(pool, pjmedia_sdp_conn); |
| 1889 | m->conn->net_type = pj_str("IN"); |
| 1890 | m->conn->addr_type = pj_str("IP4"); |
| 1891 | m->conn->addr = pj_str("127.0.0.1"); |
Benny Prijono | a310bd2 | 2008-06-27 21:19:44 +0000 | [diff] [blame] | 1892 | |
Nanang Izzuddin | d8c33d8 | 2011-08-18 18:30:55 +0000 | [diff] [blame] | 1893 | switch (call_med->type) { |
| 1894 | case PJMEDIA_TYPE_AUDIO: |
| 1895 | m->desc.media = pj_str("audio"); |
| 1896 | m->desc.fmt[0] = pj_str("0"); |
| 1897 | break; |
| 1898 | case PJMEDIA_TYPE_VIDEO: |
| 1899 | m->desc.media = pj_str("video"); |
| 1900 | m->desc.fmt[0] = pj_str("31"); |
| 1901 | break; |
| 1902 | default: |
| 1903 | if (rem_sdp) { |
| 1904 | pj_strdup(pool, &m->desc.media, |
| 1905 | &rem_sdp->media[mi]->desc.media); |
| 1906 | pj_strdup(pool, &m->desc.fmt[0], |
| 1907 | &rem_sdp->media[mi]->desc.fmt[0]); |
| 1908 | } else { |
| 1909 | pj_assert(!"Invalid call_med media type"); |
| 1910 | return PJ_EBUG; |
| 1911 | } |
Benny Prijono | 0bc99a9 | 2011-03-17 04:34:43 +0000 | [diff] [blame] | 1912 | } |
Benny Prijono | a310bd2 | 2008-06-27 21:19:44 +0000 | [diff] [blame] | 1913 | } |
Benny Prijono | 0bc99a9 | 2011-03-17 04:34:43 +0000 | [diff] [blame] | 1914 | |
| 1915 | sdp->media[sdp->media_count++] = m; |
| 1916 | continue; |
Benny Prijono | a310bd2 | 2008-06-27 21:19:44 +0000 | [diff] [blame] | 1917 | } |
| 1918 | |
Benny Prijono | 0bc99a9 | 2011-03-17 04:34:43 +0000 | [diff] [blame] | 1919 | /* Get transport address info */ |
| 1920 | pjmedia_transport_info_init(&tpinfo); |
| 1921 | pjmedia_transport_get_info(call_med->tp, &tpinfo); |
Benny Prijono | a310bd2 | 2008-06-27 21:19:44 +0000 | [diff] [blame] | 1922 | |
Benny Prijono | 0bc99a9 | 2011-03-17 04:34:43 +0000 | [diff] [blame] | 1923 | /* Ask pjmedia endpoint to create SDP media line */ |
| 1924 | switch (call_med->type) { |
| 1925 | case PJMEDIA_TYPE_AUDIO: |
| 1926 | status = pjmedia_endpt_create_audio_sdp(pjsua_var.med_endpt, pool, |
| 1927 | &tpinfo.sock_info, 0, &m); |
| 1928 | break; |
Nanang Izzuddin | 63b3c13 | 2011-07-19 11:11:07 +0000 | [diff] [blame] | 1929 | #if defined(PJMEDIA_HAS_VIDEO) && (PJMEDIA_HAS_VIDEO != 0) |
Benny Prijono | 0bc99a9 | 2011-03-17 04:34:43 +0000 | [diff] [blame] | 1930 | case PJMEDIA_TYPE_VIDEO: |
| 1931 | status = pjmedia_endpt_create_video_sdp(pjsua_var.med_endpt, pool, |
| 1932 | &tpinfo.sock_info, 0, &m); |
| 1933 | break; |
Nanang Izzuddin | 63b3c13 | 2011-07-19 11:11:07 +0000 | [diff] [blame] | 1934 | #endif |
Benny Prijono | 0bc99a9 | 2011-03-17 04:34:43 +0000 | [diff] [blame] | 1935 | default: |
| 1936 | pj_assert(!"Invalid call_med media type"); |
| 1937 | return PJ_EBUG; |
| 1938 | } |
Benny Prijono | a310bd2 | 2008-06-27 21:19:44 +0000 | [diff] [blame] | 1939 | |
Benny Prijono | 0bc99a9 | 2011-03-17 04:34:43 +0000 | [diff] [blame] | 1940 | if (status != PJ_SUCCESS) |
| 1941 | return status; |
| 1942 | |
| 1943 | sdp->media[sdp->media_count++] = m; |
| 1944 | |
| 1945 | /* Give to transport */ |
| 1946 | status = pjmedia_transport_encode_sdp(call_med->tp, pool, |
| 1947 | sdp, rem_sdp, mi); |
| 1948 | if (status != PJ_SUCCESS) { |
| 1949 | if (sip_err_code) *sip_err_code = PJSIP_SC_NOT_ACCEPTABLE; |
| 1950 | return status; |
Benny Prijono | a310bd2 | 2008-06-27 21:19:44 +0000 | [diff] [blame] | 1951 | } |
Nanang Izzuddin | 91ba2a2 | 2011-04-11 19:59:09 +0000 | [diff] [blame] | 1952 | |
| 1953 | /* Copy c= line of the first media to session level, |
| 1954 | * if there's none. |
| 1955 | */ |
| 1956 | if (sdp->conn == NULL) { |
| 1957 | sdp->conn = pjmedia_sdp_conn_clone(pool, m->conn); |
Benny Prijono | a310bd2 | 2008-06-27 21:19:44 +0000 | [diff] [blame] | 1958 | } |
| 1959 | } |
| 1960 | |
Benny Prijono | 6ba8c54 | 2007-10-16 01:34:14 +0000 | [diff] [blame] | 1961 | /* Add NAT info in the SDP */ |
| 1962 | if (pjsua_var.ua_cfg.nat_type_in_sdp) { |
| 1963 | pjmedia_sdp_attr *a; |
| 1964 | pj_str_t value; |
| 1965 | char nat_info[80]; |
| 1966 | |
| 1967 | value.ptr = nat_info; |
| 1968 | if (pjsua_var.ua_cfg.nat_type_in_sdp == 1) { |
| 1969 | value.slen = pj_ansi_snprintf(nat_info, sizeof(nat_info), |
| 1970 | "%d", pjsua_var.nat_type); |
| 1971 | } else { |
| 1972 | const char *type_name = pj_stun_get_nat_name(pjsua_var.nat_type); |
| 1973 | value.slen = pj_ansi_snprintf(nat_info, sizeof(nat_info), |
| 1974 | "%d %s", |
| 1975 | pjsua_var.nat_type, |
| 1976 | type_name); |
| 1977 | } |
| 1978 | |
| 1979 | a = pjmedia_sdp_attr_create(pool, "X-nat", &value); |
| 1980 | |
| 1981 | pjmedia_sdp_attr_add(&sdp->attr_count, sdp->attr, a); |
| 1982 | |
| 1983 | } |
| 1984 | |
Benny Prijono | c97608e | 2007-03-23 16:34:20 +0000 | [diff] [blame] | 1985 | |
Benny Prijono | 0bc99a9 | 2011-03-17 04:34:43 +0000 | [diff] [blame] | 1986 | #if DISABLED_FOR_TICKET_1185 && defined(PJMEDIA_HAS_SRTP) && (PJMEDIA_HAS_SRTP != 0) |
Nanang Izzuddin | d89cc3a | 2010-05-13 05:22:51 +0000 | [diff] [blame] | 1987 | /* Check if SRTP is in optional mode and configured to use duplicated |
| 1988 | * media, i.e: secured and unsecured version, in the SDP offer. |
| 1989 | */ |
| 1990 | if (!rem_sdp && |
| 1991 | pjsua_var.acc[call->acc_id].cfg.use_srtp == PJMEDIA_SRTP_OPTIONAL && |
| 1992 | pjsua_var.acc[call->acc_id].cfg.srtp_optional_dup_offer) |
| 1993 | { |
| 1994 | unsigned i; |
| 1995 | |
| 1996 | for (i = 0; i < sdp->media_count; ++i) { |
| 1997 | pjmedia_sdp_media *m = sdp->media[i]; |
| 1998 | |
Benny Prijono | 0bc99a9 | 2011-03-17 04:34:43 +0000 | [diff] [blame] | 1999 | /* Check if this media is unsecured but has SDP "crypto" |
Nanang Izzuddin | d89cc3a | 2010-05-13 05:22:51 +0000 | [diff] [blame] | 2000 | * attribute. |
| 2001 | */ |
| 2002 | if (pj_stricmp2(&m->desc.transport, "RTP/AVP") == 0 && |
| 2003 | pjmedia_sdp_media_find_attr2(m, "crypto", NULL) != NULL) |
| 2004 | { |
Benny Prijono | 0bc99a9 | 2011-03-17 04:34:43 +0000 | [diff] [blame] | 2005 | if (i == (unsigned)call->audio_idx && |
Nanang Izzuddin | 1e952a8 | 2010-10-05 16:32:04 +0000 | [diff] [blame] | 2006 | sdp_neg_state == PJMEDIA_SDP_NEG_STATE_DONE) |
| 2007 | { |
| 2008 | /* This is a session update, and peer has chosen the |
| 2009 | * unsecured version, so let's make this unsecured too. |
| 2010 | */ |
| 2011 | pjmedia_sdp_media_remove_all_attr(m, "crypto"); |
| 2012 | } else { |
| 2013 | /* This is new offer, duplicate media so we'll have |
| 2014 | * secured (with "RTP/SAVP" transport) and and unsecured |
| 2015 | * versions. |
| 2016 | */ |
| 2017 | pjmedia_sdp_media *new_m; |
Nanang Izzuddin | d89cc3a | 2010-05-13 05:22:51 +0000 | [diff] [blame] | 2018 | |
Nanang Izzuddin | 1e952a8 | 2010-10-05 16:32:04 +0000 | [diff] [blame] | 2019 | /* Duplicate this media and apply secured transport */ |
| 2020 | new_m = pjmedia_sdp_media_clone(pool, m); |
| 2021 | pj_strdup2(pool, &new_m->desc.transport, "RTP/SAVP"); |
Nanang Izzuddin | d89cc3a | 2010-05-13 05:22:51 +0000 | [diff] [blame] | 2022 | |
Nanang Izzuddin | 1e952a8 | 2010-10-05 16:32:04 +0000 | [diff] [blame] | 2023 | /* Remove the "crypto" attribute in the unsecured media */ |
| 2024 | pjmedia_sdp_media_remove_all_attr(m, "crypto"); |
Nanang Izzuddin | d89cc3a | 2010-05-13 05:22:51 +0000 | [diff] [blame] | 2025 | |
Nanang Izzuddin | 1e952a8 | 2010-10-05 16:32:04 +0000 | [diff] [blame] | 2026 | /* Insert the new media before the unsecured media */ |
| 2027 | if (sdp->media_count < PJMEDIA_MAX_SDP_MEDIA) { |
Benny Prijono | 0bc99a9 | 2011-03-17 04:34:43 +0000 | [diff] [blame] | 2028 | pj_array_insert(sdp->media, sizeof(new_m), |
Nanang Izzuddin | 1e952a8 | 2010-10-05 16:32:04 +0000 | [diff] [blame] | 2029 | sdp->media_count, i, &new_m); |
| 2030 | ++sdp->media_count; |
| 2031 | ++i; |
| 2032 | } |
Nanang Izzuddin | d89cc3a | 2010-05-13 05:22:51 +0000 | [diff] [blame] | 2033 | } |
| 2034 | } |
| 2035 | } |
| 2036 | } |
| 2037 | #endif |
| 2038 | |
Benny Prijono | c97608e | 2007-03-23 16:34:20 +0000 | [diff] [blame] | 2039 | *p_sdp = sdp; |
| 2040 | return PJ_SUCCESS; |
Benny Prijono | c97608e | 2007-03-23 16:34:20 +0000 | [diff] [blame] | 2041 | } |
| 2042 | |
| 2043 | |
| 2044 | static void stop_media_session(pjsua_call_id call_id) |
| 2045 | { |
| 2046 | pjsua_call *call = &pjsua_var.calls[call_id]; |
Benny Prijono | 0bc99a9 | 2011-03-17 04:34:43 +0000 | [diff] [blame] | 2047 | unsigned mi; |
Benny Prijono | c97608e | 2007-03-23 16:34:20 +0000 | [diff] [blame] | 2048 | |
Benny Prijono | b90fd38 | 2011-09-18 14:59:56 +0000 | [diff] [blame] | 2049 | pj_log_push_indent(); |
| 2050 | |
Benny Prijono | 0bc99a9 | 2011-03-17 04:34:43 +0000 | [diff] [blame] | 2051 | for (mi=0; mi<call->med_cnt; ++mi) { |
| 2052 | pjsua_call_media *call_med = &call->media[mi]; |
| 2053 | |
| 2054 | if (call_med->type == PJMEDIA_TYPE_AUDIO) { |
| 2055 | pjmedia_stream *strm = call_med->strm.a.stream; |
| 2056 | pjmedia_rtcp_stat stat; |
| 2057 | |
| 2058 | if (strm) { |
| 2059 | if (call_med->strm.a.conf_slot != PJSUA_INVALID_ID) { |
| 2060 | if (pjsua_var.mconf) { |
| 2061 | pjsua_conf_remove_port(call_med->strm.a.conf_slot); |
| 2062 | } |
| 2063 | call_med->strm.a.conf_slot = PJSUA_INVALID_ID; |
| 2064 | } |
| 2065 | |
| 2066 | if ((call_med->dir & PJMEDIA_DIR_ENCODING) && |
| 2067 | (pjmedia_stream_get_stat(strm, &stat) == PJ_SUCCESS)) |
| 2068 | { |
| 2069 | /* Save RTP timestamp & sequence, so when media session is |
| 2070 | * restarted, those values will be restored as the initial |
| 2071 | * RTP timestamp & sequence of the new media session. So in |
| 2072 | * the same call session, RTP timestamp and sequence are |
| 2073 | * guaranteed to be contigue. |
| 2074 | */ |
| 2075 | call_med->rtp_tx_seq_ts_set = 1 | (1 << 1); |
| 2076 | call_med->rtp_tx_seq = stat.rtp_tx_last_seq; |
| 2077 | call_med->rtp_tx_ts = stat.rtp_tx_last_ts; |
| 2078 | } |
| 2079 | |
| 2080 | if (pjsua_var.ua_cfg.cb.on_stream_destroyed) { |
| 2081 | pjsua_var.ua_cfg.cb.on_stream_destroyed(call_id, strm, mi); |
| 2082 | } |
| 2083 | |
| 2084 | pjmedia_stream_destroy(strm); |
| 2085 | call_med->strm.a.stream = NULL; |
Nanang Izzuddin | bf26db1 | 2011-03-18 07:54:50 +0000 | [diff] [blame] | 2086 | } |
Nanang Izzuddin | fd461eb | 2008-06-09 09:35:59 +0000 | [diff] [blame] | 2087 | } |
Nanang Izzuddin | 50fae73 | 2011-03-22 09:49:23 +0000 | [diff] [blame] | 2088 | |
| 2089 | #if PJMEDIA_HAS_VIDEO |
| 2090 | else if (call_med->type == PJMEDIA_TYPE_VIDEO) { |
Nanang Izzuddin | 62053a6 | 2011-07-12 11:08:32 +0000 | [diff] [blame] | 2091 | stop_video_stream(call_med); |
Benny Prijono | c97608e | 2007-03-23 16:34:20 +0000 | [diff] [blame] | 2092 | } |
Nanang Izzuddin | 50fae73 | 2011-03-22 09:49:23 +0000 | [diff] [blame] | 2093 | #endif |
Nanang Izzuddin | bf26db1 | 2011-03-18 07:54:50 +0000 | [diff] [blame] | 2094 | |
| 2095 | PJ_LOG(4,(THIS_FILE, "Media session call%02d:%d is destroyed", |
| 2096 | call_id, mi)); |
Benny Prijono | 0bc99a9 | 2011-03-17 04:34:43 +0000 | [diff] [blame] | 2097 | call_med->state = PJSUA_CALL_MEDIA_NONE; |
Benny Prijono | c97608e | 2007-03-23 16:34:20 +0000 | [diff] [blame] | 2098 | } |
Benny Prijono | b90fd38 | 2011-09-18 14:59:56 +0000 | [diff] [blame] | 2099 | |
| 2100 | pj_log_pop_indent(); |
Benny Prijono | c97608e | 2007-03-23 16:34:20 +0000 | [diff] [blame] | 2101 | } |
| 2102 | |
| 2103 | pj_status_t pjsua_media_channel_deinit(pjsua_call_id call_id) |
| 2104 | { |
| 2105 | pjsua_call *call = &pjsua_var.calls[call_id]; |
Benny Prijono | 0bc99a9 | 2011-03-17 04:34:43 +0000 | [diff] [blame] | 2106 | unsigned mi; |
Benny Prijono | c97608e | 2007-03-23 16:34:20 +0000 | [diff] [blame] | 2107 | |
Benny Prijono | b90fd38 | 2011-09-18 14:59:56 +0000 | [diff] [blame] | 2108 | PJ_LOG(4,(THIS_FILE, "Call %d: deinitializing media..", call_id)); |
| 2109 | pj_log_push_indent(); |
| 2110 | |
Benny Prijono | c97608e | 2007-03-23 16:34:20 +0000 | [diff] [blame] | 2111 | stop_media_session(call_id); |
| 2112 | |
Benny Prijono | 0bc99a9 | 2011-03-17 04:34:43 +0000 | [diff] [blame] | 2113 | for (mi=0; mi<call->med_cnt; ++mi) { |
| 2114 | pjsua_call_media *call_med = &call->media[mi]; |
Benny Prijono | c97608e | 2007-03-23 16:34:20 +0000 | [diff] [blame] | 2115 | |
Sauw Ming | 73ecfe8 | 2011-09-21 10:20:01 +0000 | [diff] [blame] | 2116 | if (call_med->tp_st > PJSUA_MED_TP_IDLE) { |
Benny Prijono | 0bc99a9 | 2011-03-17 04:34:43 +0000 | [diff] [blame] | 2117 | pjmedia_transport_media_stop(call_med->tp); |
Sauw Ming | 73ecfe8 | 2011-09-21 10:20:01 +0000 | [diff] [blame] | 2118 | set_media_tp_state(call_med, PJSUA_MED_TP_IDLE); |
Benny Prijono | 0bc99a9 | 2011-03-17 04:34:43 +0000 | [diff] [blame] | 2119 | } |
| 2120 | |
Nanang Izzuddin | b6c239c | 2011-05-10 05:42:28 +0000 | [diff] [blame] | 2121 | //if (call_med->tp_orig && call_med->tp && |
| 2122 | // call_med->tp != call_med->tp_orig) |
| 2123 | //{ |
| 2124 | // pjmedia_transport_close(call_med->tp); |
| 2125 | // call_med->tp = call_med->tp_orig; |
| 2126 | //} |
| 2127 | if (call_med->tp) { |
Benny Prijono | 0bc99a9 | 2011-03-17 04:34:43 +0000 | [diff] [blame] | 2128 | pjmedia_transport_close(call_med->tp); |
Nanang Izzuddin | b6c239c | 2011-05-10 05:42:28 +0000 | [diff] [blame] | 2129 | call_med->tp = call_med->tp_orig = NULL; |
Benny Prijono | 0bc99a9 | 2011-03-17 04:34:43 +0000 | [diff] [blame] | 2130 | } |
Benny Prijono | d817965 | 2008-01-23 20:39:07 +0000 | [diff] [blame] | 2131 | } |
Nanang Izzuddin | 670f71b | 2009-01-20 14:05:54 +0000 | [diff] [blame] | 2132 | |
| 2133 | check_snd_dev_idle(); |
Benny Prijono | b90fd38 | 2011-09-18 14:59:56 +0000 | [diff] [blame] | 2134 | pj_log_pop_indent(); |
Nanang Izzuddin | 670f71b | 2009-01-20 14:05:54 +0000 | [diff] [blame] | 2135 | |
Benny Prijono | c97608e | 2007-03-23 16:34:20 +0000 | [diff] [blame] | 2136 | return PJ_SUCCESS; |
| 2137 | } |
| 2138 | |
| 2139 | |
| 2140 | /* |
| 2141 | * DTMF callback from the stream. |
| 2142 | */ |
| 2143 | static void dtmf_callback(pjmedia_stream *strm, void *user_data, |
| 2144 | int digit) |
| 2145 | { |
| 2146 | PJ_UNUSED_ARG(strm); |
| 2147 | |
Benny Prijono | b90fd38 | 2011-09-18 14:59:56 +0000 | [diff] [blame] | 2148 | pj_log_push_indent(); |
| 2149 | |
Benny Prijono | 0c06826 | 2008-02-14 14:38:52 +0000 | [diff] [blame] | 2150 | /* For discussions about call mutex protection related to this |
| 2151 | * callback, please see ticket #460: |
| 2152 | * http://trac.pjsip.org/repos/ticket/460#comment:4 |
| 2153 | */ |
Benny Prijono | c97608e | 2007-03-23 16:34:20 +0000 | [diff] [blame] | 2154 | if (pjsua_var.ua_cfg.cb.on_dtmf_digit) { |
| 2155 | pjsua_call_id call_id; |
| 2156 | |
Benny Prijono | d817965 | 2008-01-23 20:39:07 +0000 | [diff] [blame] | 2157 | call_id = (pjsua_call_id)(long)user_data; |
Benny Prijono | c97608e | 2007-03-23 16:34:20 +0000 | [diff] [blame] | 2158 | pjsua_var.ua_cfg.cb.on_dtmf_digit(call_id, digit); |
| 2159 | } |
Benny Prijono | b90fd38 | 2011-09-18 14:59:56 +0000 | [diff] [blame] | 2160 | |
| 2161 | pj_log_pop_indent(); |
Benny Prijono | c97608e | 2007-03-23 16:34:20 +0000 | [diff] [blame] | 2162 | } |
| 2163 | |
| 2164 | |
Benny Prijono | 0bc99a9 | 2011-03-17 04:34:43 +0000 | [diff] [blame] | 2165 | static pj_status_t audio_channel_update(pjsua_call_media *call_med, |
| 2166 | pj_pool_t *tmp_pool, |
| 2167 | const pjmedia_sdp_session *local_sdp, |
| 2168 | const pjmedia_sdp_session *remote_sdp) |
Benny Prijono | c97608e | 2007-03-23 16:34:20 +0000 | [diff] [blame] | 2169 | { |
Benny Prijono | 0bc99a9 | 2011-03-17 04:34:43 +0000 | [diff] [blame] | 2170 | pjsua_call *call = call_med->call; |
| 2171 | pjmedia_stream_info the_si, *si = &the_si; |
Benny Prijono | c97608e | 2007-03-23 16:34:20 +0000 | [diff] [blame] | 2172 | pjmedia_port *media_port; |
Benny Prijono | 0bc99a9 | 2011-03-17 04:34:43 +0000 | [diff] [blame] | 2173 | unsigned strm_idx = call_med->idx; |
Benny Prijono | c97608e | 2007-03-23 16:34:20 +0000 | [diff] [blame] | 2174 | pj_status_t status; |
Benny Prijono | b90fd38 | 2011-09-18 14:59:56 +0000 | [diff] [blame] | 2175 | |
| 2176 | PJ_LOG(4,(THIS_FILE,"Audio channel update..")); |
| 2177 | pj_log_push_indent(); |
Benny Prijono | 0bc99a9 | 2011-03-17 04:34:43 +0000 | [diff] [blame] | 2178 | |
| 2179 | status = pjmedia_stream_info_from_sdp(si, tmp_pool, pjsua_var.med_endpt, |
| 2180 | local_sdp, remote_sdp, strm_idx); |
Benny Prijono | c97608e | 2007-03-23 16:34:20 +0000 | [diff] [blame] | 2181 | if (status != PJ_SUCCESS) |
Benny Prijono | b90fd38 | 2011-09-18 14:59:56 +0000 | [diff] [blame] | 2182 | goto on_return; |
Benny Prijono | c97608e | 2007-03-23 16:34:20 +0000 | [diff] [blame] | 2183 | |
Nanang Izzuddin | 99d6952 | 2008-08-04 15:01:38 +0000 | [diff] [blame] | 2184 | /* Check if no media is active */ |
Benny Prijono | 0bc99a9 | 2011-03-17 04:34:43 +0000 | [diff] [blame] | 2185 | if (si->dir == PJMEDIA_DIR_NONE) { |
Nanang Izzuddin | 99d6952 | 2008-08-04 15:01:38 +0000 | [diff] [blame] | 2186 | /* Call media state */ |
Benny Prijono | 0bc99a9 | 2011-03-17 04:34:43 +0000 | [diff] [blame] | 2187 | call_med->state = PJSUA_CALL_MEDIA_NONE; |
Benny Prijono | c97608e | 2007-03-23 16:34:20 +0000 | [diff] [blame] | 2188 | |
Nanang Izzuddin | 99d6952 | 2008-08-04 15:01:38 +0000 | [diff] [blame] | 2189 | /* Call media direction */ |
Benny Prijono | 0bc99a9 | 2011-03-17 04:34:43 +0000 | [diff] [blame] | 2190 | call_med->dir = PJMEDIA_DIR_NONE; |
Benny Prijono | 68f9e4f | 2008-03-21 08:56:02 +0000 | [diff] [blame] | 2191 | |
Benny Prijono | c97608e | 2007-03-23 16:34:20 +0000 | [diff] [blame] | 2192 | } else { |
Nanang Izzuddin | 4375f90 | 2008-06-26 19:12:09 +0000 | [diff] [blame] | 2193 | pjmedia_transport_info tp_info; |
| 2194 | |
Benny Prijono | 224b4e2 | 2008-06-19 14:10:28 +0000 | [diff] [blame] | 2195 | /* Start/restart media transport */ |
Benny Prijono | 0bc99a9 | 2011-03-17 04:34:43 +0000 | [diff] [blame] | 2196 | status = pjmedia_transport_media_start(call_med->tp, |
| 2197 | tmp_pool, local_sdp, |
| 2198 | remote_sdp, strm_idx); |
Benny Prijono | d817965 | 2008-01-23 20:39:07 +0000 | [diff] [blame] | 2199 | if (status != PJ_SUCCESS) |
Benny Prijono | b90fd38 | 2011-09-18 14:59:56 +0000 | [diff] [blame] | 2200 | goto on_return; |
Benny Prijono | c97608e | 2007-03-23 16:34:20 +0000 | [diff] [blame] | 2201 | |
Sauw Ming | 73ecfe8 | 2011-09-21 10:20:01 +0000 | [diff] [blame] | 2202 | set_media_tp_state(call_med, PJSUA_MED_TP_RUNNING); |
Benny Prijono | 224b4e2 | 2008-06-19 14:10:28 +0000 | [diff] [blame] | 2203 | |
Nanang Izzuddin | 4375f90 | 2008-06-26 19:12:09 +0000 | [diff] [blame] | 2204 | /* Get remote SRTP usage policy */ |
| 2205 | pjmedia_transport_info_init(&tp_info); |
Benny Prijono | 0bc99a9 | 2011-03-17 04:34:43 +0000 | [diff] [blame] | 2206 | pjmedia_transport_get_info(call_med->tp, &tp_info); |
Nanang Izzuddin | 4375f90 | 2008-06-26 19:12:09 +0000 | [diff] [blame] | 2207 | if (tp_info.specific_info_cnt > 0) { |
Benny Prijono | f5d9f1f | 2009-10-14 13:13:18 +0000 | [diff] [blame] | 2208 | unsigned i; |
Nanang Izzuddin | 4375f90 | 2008-06-26 19:12:09 +0000 | [diff] [blame] | 2209 | for (i = 0; i < tp_info.specific_info_cnt; ++i) { |
| 2210 | if (tp_info.spc_info[i].type == PJMEDIA_TRANSPORT_TYPE_SRTP) |
| 2211 | { |
| 2212 | pjmedia_srtp_info *srtp_info = |
| 2213 | (pjmedia_srtp_info*) tp_info.spc_info[i].buffer; |
| 2214 | |
Benny Prijono | 0bc99a9 | 2011-03-17 04:34:43 +0000 | [diff] [blame] | 2215 | call_med->rem_srtp_use = srtp_info->peer_use; |
Nanang Izzuddin | 4375f90 | 2008-06-26 19:12:09 +0000 | [diff] [blame] | 2216 | break; |
| 2217 | } |
| 2218 | } |
| 2219 | } |
| 2220 | |
Benny Prijono | c97608e | 2007-03-23 16:34:20 +0000 | [diff] [blame] | 2221 | /* Override ptime, if this option is specified. */ |
| 2222 | if (pjsua_var.media_cfg.ptime != 0) { |
Benny Prijono | 91e567e | 2007-12-28 08:51:58 +0000 | [diff] [blame] | 2223 | si->param->setting.frm_per_pkt = (pj_uint8_t) |
| 2224 | (pjsua_var.media_cfg.ptime / si->param->info.frm_ptime); |
| 2225 | if (si->param->setting.frm_per_pkt == 0) |
| 2226 | si->param->setting.frm_per_pkt = 1; |
Benny Prijono | c97608e | 2007-03-23 16:34:20 +0000 | [diff] [blame] | 2227 | } |
| 2228 | |
| 2229 | /* Disable VAD, if this option is specified. */ |
| 2230 | if (pjsua_var.media_cfg.no_vad) { |
Benny Prijono | 91e567e | 2007-12-28 08:51:58 +0000 | [diff] [blame] | 2231 | si->param->setting.vad = 0; |
Benny Prijono | c97608e | 2007-03-23 16:34:20 +0000 | [diff] [blame] | 2232 | } |
| 2233 | |
| 2234 | |
| 2235 | /* Optionally, application may modify other stream settings here |
| 2236 | * (such as jitter buffer parameters, codec ptime, etc.) |
| 2237 | */ |
Benny Prijono | 91e567e | 2007-12-28 08:51:58 +0000 | [diff] [blame] | 2238 | si->jb_init = pjsua_var.media_cfg.jb_init; |
| 2239 | si->jb_min_pre = pjsua_var.media_cfg.jb_min_pre; |
| 2240 | si->jb_max_pre = pjsua_var.media_cfg.jb_max_pre; |
| 2241 | si->jb_max = pjsua_var.media_cfg.jb_max; |
Benny Prijono | c97608e | 2007-03-23 16:34:20 +0000 | [diff] [blame] | 2242 | |
Benny Prijono | 8147f40 | 2007-11-21 14:50:07 +0000 | [diff] [blame] | 2243 | /* Set SSRC */ |
Benny Prijono | 0bc99a9 | 2011-03-17 04:34:43 +0000 | [diff] [blame] | 2244 | si->ssrc = call_med->ssrc; |
Benny Prijono | 8147f40 | 2007-11-21 14:50:07 +0000 | [diff] [blame] | 2245 | |
Nanang Izzuddin | a815ceb | 2008-08-26 16:51:28 +0000 | [diff] [blame] | 2246 | /* Set RTP timestamp & sequence, normally these value are intialized |
| 2247 | * automatically when stream session created, but for some cases (e.g: |
| 2248 | * call reinvite, call update) timestamp and sequence need to be kept |
| 2249 | * contigue. |
| 2250 | */ |
Benny Prijono | 0bc99a9 | 2011-03-17 04:34:43 +0000 | [diff] [blame] | 2251 | si->rtp_ts = call_med->rtp_tx_ts; |
| 2252 | si->rtp_seq = call_med->rtp_tx_seq; |
| 2253 | si->rtp_seq_ts_set = call_med->rtp_tx_seq_ts_set; |
Nanang Izzuddin | a815ceb | 2008-08-26 16:51:28 +0000 | [diff] [blame] | 2254 | |
Nanang Izzuddin | 5e39a2b | 2010-09-20 06:13:02 +0000 | [diff] [blame] | 2255 | #if defined(PJMEDIA_STREAM_ENABLE_KA) && PJMEDIA_STREAM_ENABLE_KA!=0 |
| 2256 | /* Enable/disable stream keep-alive and NAT hole punch. */ |
| 2257 | si->use_ka = pjsua_var.acc[call->acc_id].cfg.use_stream_ka; |
| 2258 | #endif |
| 2259 | |
Benny Prijono | c97608e | 2007-03-23 16:34:20 +0000 | [diff] [blame] | 2260 | /* Create session based on session info. */ |
Benny Prijono | 0bc99a9 | 2011-03-17 04:34:43 +0000 | [diff] [blame] | 2261 | status = pjmedia_stream_create(pjsua_var.med_endpt, NULL, si, |
| 2262 | call_med->tp, NULL, |
| 2263 | &call_med->strm.a.stream); |
| 2264 | if (status != PJ_SUCCESS) { |
Benny Prijono | b90fd38 | 2011-09-18 14:59:56 +0000 | [diff] [blame] | 2265 | goto on_return; |
Benny Prijono | 0bc99a9 | 2011-03-17 04:34:43 +0000 | [diff] [blame] | 2266 | } |
| 2267 | |
| 2268 | /* Start stream */ |
| 2269 | status = pjmedia_stream_start(call_med->strm.a.stream); |
Benny Prijono | c97608e | 2007-03-23 16:34:20 +0000 | [diff] [blame] | 2270 | if (status != PJ_SUCCESS) { |
Benny Prijono | b90fd38 | 2011-09-18 14:59:56 +0000 | [diff] [blame] | 2271 | goto on_return; |
Benny Prijono | c97608e | 2007-03-23 16:34:20 +0000 | [diff] [blame] | 2272 | } |
| 2273 | |
| 2274 | /* If DTMF callback is installed by application, install our |
| 2275 | * callback to the session. |
| 2276 | */ |
| 2277 | if (pjsua_var.ua_cfg.cb.on_dtmf_digit) { |
Benny Prijono | 0bc99a9 | 2011-03-17 04:34:43 +0000 | [diff] [blame] | 2278 | pjmedia_stream_set_dtmf_callback(call_med->strm.a.stream, |
| 2279 | &dtmf_callback, |
| 2280 | (void*)(long)(call->index)); |
Benny Prijono | c97608e | 2007-03-23 16:34:20 +0000 | [diff] [blame] | 2281 | } |
| 2282 | |
| 2283 | /* Get the port interface of the first stream in the session. |
| 2284 | * We need the port interface to add to the conference bridge. |
| 2285 | */ |
Benny Prijono | 0bc99a9 | 2011-03-17 04:34:43 +0000 | [diff] [blame] | 2286 | pjmedia_stream_get_port(call_med->strm.a.stream, &media_port); |
Benny Prijono | c97608e | 2007-03-23 16:34:20 +0000 | [diff] [blame] | 2287 | |
Benny Prijono | fc13bf6 | 2008-02-20 08:56:15 +0000 | [diff] [blame] | 2288 | /* Notify application about stream creation. |
| 2289 | * Note: application may modify media_port to point to different |
| 2290 | * media port |
| 2291 | */ |
| 2292 | if (pjsua_var.ua_cfg.cb.on_stream_created) { |
Benny Prijono | 0bc99a9 | 2011-03-17 04:34:43 +0000 | [diff] [blame] | 2293 | pjsua_var.ua_cfg.cb.on_stream_created(call->index, |
| 2294 | call_med->strm.a.stream, |
| 2295 | strm_idx, &media_port); |
Benny Prijono | fc13bf6 | 2008-02-20 08:56:15 +0000 | [diff] [blame] | 2296 | } |
Benny Prijono | c97608e | 2007-03-23 16:34:20 +0000 | [diff] [blame] | 2297 | |
| 2298 | /* |
| 2299 | * Add the call to conference bridge. |
| 2300 | */ |
Benny Prijono | 38fb3ea | 2008-01-02 08:27:03 +0000 | [diff] [blame] | 2301 | { |
| 2302 | char tmp[PJSIP_MAX_URL_SIZE]; |
| 2303 | pj_str_t port_name; |
| 2304 | |
| 2305 | port_name.ptr = tmp; |
| 2306 | port_name.slen = pjsip_uri_print(PJSIP_URI_IN_REQ_URI, |
| 2307 | call->inv->dlg->remote.info->uri, |
| 2308 | tmp, sizeof(tmp)); |
| 2309 | if (port_name.slen < 1) { |
| 2310 | port_name = pj_str("call"); |
| 2311 | } |
Benny Prijono | 40d62b6 | 2009-08-12 17:53:47 +0000 | [diff] [blame] | 2312 | status = pjmedia_conf_add_port( pjsua_var.mconf, |
| 2313 | call->inv->pool_prov, |
Benny Prijono | 38fb3ea | 2008-01-02 08:27:03 +0000 | [diff] [blame] | 2314 | media_port, |
| 2315 | &port_name, |
Benny Prijono | 0bc99a9 | 2011-03-17 04:34:43 +0000 | [diff] [blame] | 2316 | (unsigned*) |
| 2317 | &call_med->strm.a.conf_slot); |
Benny Prijono | 38fb3ea | 2008-01-02 08:27:03 +0000 | [diff] [blame] | 2318 | if (status != PJ_SUCCESS) { |
Benny Prijono | b90fd38 | 2011-09-18 14:59:56 +0000 | [diff] [blame] | 2319 | goto on_return; |
Benny Prijono | 38fb3ea | 2008-01-02 08:27:03 +0000 | [diff] [blame] | 2320 | } |
Benny Prijono | c97608e | 2007-03-23 16:34:20 +0000 | [diff] [blame] | 2321 | } |
| 2322 | |
Nanang Izzuddin | 99d6952 | 2008-08-04 15:01:38 +0000 | [diff] [blame] | 2323 | /* Call media direction */ |
Benny Prijono | 0bc99a9 | 2011-03-17 04:34:43 +0000 | [diff] [blame] | 2324 | call_med->dir = si->dir; |
Nanang Izzuddin | 99d6952 | 2008-08-04 15:01:38 +0000 | [diff] [blame] | 2325 | |
| 2326 | /* Call media state */ |
| 2327 | if (call->local_hold) |
Benny Prijono | 0bc99a9 | 2011-03-17 04:34:43 +0000 | [diff] [blame] | 2328 | call_med->state = PJSUA_CALL_MEDIA_LOCAL_HOLD; |
| 2329 | else if (call_med->dir == PJMEDIA_DIR_DECODING) |
| 2330 | call_med->state = PJSUA_CALL_MEDIA_REMOTE_HOLD; |
Nanang Izzuddin | 99d6952 | 2008-08-04 15:01:38 +0000 | [diff] [blame] | 2331 | else |
Benny Prijono | 0bc99a9 | 2011-03-17 04:34:43 +0000 | [diff] [blame] | 2332 | call_med->state = PJSUA_CALL_MEDIA_ACTIVE; |
Benny Prijono | c97608e | 2007-03-23 16:34:20 +0000 | [diff] [blame] | 2333 | } |
| 2334 | |
| 2335 | /* Print info. */ |
| 2336 | { |
| 2337 | char info[80]; |
| 2338 | int info_len = 0; |
Benny Prijono | 0bc99a9 | 2011-03-17 04:34:43 +0000 | [diff] [blame] | 2339 | int len; |
| 2340 | const char *dir; |
Benny Prijono | c97608e | 2007-03-23 16:34:20 +0000 | [diff] [blame] | 2341 | |
Benny Prijono | 0bc99a9 | 2011-03-17 04:34:43 +0000 | [diff] [blame] | 2342 | switch (si->dir) { |
| 2343 | case PJMEDIA_DIR_NONE: |
| 2344 | dir = "inactive"; |
| 2345 | break; |
| 2346 | case PJMEDIA_DIR_ENCODING: |
| 2347 | dir = "sendonly"; |
| 2348 | break; |
| 2349 | case PJMEDIA_DIR_DECODING: |
| 2350 | dir = "recvonly"; |
| 2351 | break; |
| 2352 | case PJMEDIA_DIR_ENCODING_DECODING: |
| 2353 | dir = "sendrecv"; |
| 2354 | break; |
| 2355 | default: |
| 2356 | dir = "unknown"; |
| 2357 | break; |
Benny Prijono | c97608e | 2007-03-23 16:34:20 +0000 | [diff] [blame] | 2358 | } |
Benny Prijono | 0bc99a9 | 2011-03-17 04:34:43 +0000 | [diff] [blame] | 2359 | len = pj_ansi_sprintf( info+info_len, |
| 2360 | ", stream #%d: %.*s (%s)", strm_idx, |
| 2361 | (int)si->fmt.encoding_name.slen, |
| 2362 | si->fmt.encoding_name.ptr, |
| 2363 | dir); |
| 2364 | if (len > 0) |
| 2365 | info_len += len; |
Benny Prijono | b90fd38 | 2011-09-18 14:59:56 +0000 | [diff] [blame] | 2366 | PJ_LOG(4,(THIS_FILE,"Audio updated%s", info)); |
Benny Prijono | c97608e | 2007-03-23 16:34:20 +0000 | [diff] [blame] | 2367 | } |
| 2368 | |
Benny Prijono | b90fd38 | 2011-09-18 14:59:56 +0000 | [diff] [blame] | 2369 | on_return: |
| 2370 | pj_log_pop_indent(); |
| 2371 | return status; |
Benny Prijono | c97608e | 2007-03-23 16:34:20 +0000 | [diff] [blame] | 2372 | } |
| 2373 | |
Benny Prijono | 0bc99a9 | 2011-03-17 04:34:43 +0000 | [diff] [blame] | 2374 | pj_status_t pjsua_media_channel_update(pjsua_call_id call_id, |
| 2375 | const pjmedia_sdp_session *local_sdp, |
| 2376 | const pjmedia_sdp_session *remote_sdp) |
| 2377 | { |
| 2378 | pjsua_call *call = &pjsua_var.calls[call_id]; |
Nanang Izzuddin | d8c33d8 | 2011-08-18 18:30:55 +0000 | [diff] [blame] | 2379 | pjsua_acc *acc = &pjsua_var.acc[call->acc_id]; |
Benny Prijono | 0bc99a9 | 2011-03-17 04:34:43 +0000 | [diff] [blame] | 2380 | pj_pool_t *tmp_pool = call->inv->pool_prov; |
| 2381 | unsigned mi; |
Nanang Izzuddin | b6c239c | 2011-05-10 05:42:28 +0000 | [diff] [blame] | 2382 | pj_bool_t got_media = PJ_FALSE; |
Nanang Izzuddin | bf26db1 | 2011-03-18 07:54:50 +0000 | [diff] [blame] | 2383 | pj_status_t status = PJ_SUCCESS; |
Benny Prijono | 0bc99a9 | 2011-03-17 04:34:43 +0000 | [diff] [blame] | 2384 | |
Nanang Izzuddin | d8c33d8 | 2011-08-18 18:30:55 +0000 | [diff] [blame] | 2385 | const pj_str_t STR_AUDIO = { "audio", 5 }; |
| 2386 | const pj_str_t STR_VIDEO = { "video", 5 }; |
| 2387 | pj_uint8_t maudidx[PJSUA_MAX_CALL_MEDIA]; |
| 2388 | unsigned maudcnt = PJ_ARRAY_SIZE(maudidx); |
| 2389 | pj_uint8_t mvididx[PJSUA_MAX_CALL_MEDIA]; |
| 2390 | unsigned mvidcnt = PJ_ARRAY_SIZE(mvididx); |
| 2391 | pj_bool_t need_renego_sdp = PJ_FALSE; |
| 2392 | |
Benny Prijono | 0bc99a9 | 2011-03-17 04:34:43 +0000 | [diff] [blame] | 2393 | if (pjsua_get_state() != PJSUA_STATE_RUNNING) |
| 2394 | return PJ_EBUSY; |
| 2395 | |
Benny Prijono | b90fd38 | 2011-09-18 14:59:56 +0000 | [diff] [blame] | 2396 | PJ_LOG(4,(THIS_FILE, "Call %d: updating media..", call_id)); |
| 2397 | pj_log_push_indent(); |
| 2398 | |
Benny Prijono | 0bc99a9 | 2011-03-17 04:34:43 +0000 | [diff] [blame] | 2399 | /* Destroy existing media session, if any. */ |
| 2400 | stop_media_session(call->index); |
| 2401 | |
Nanang Izzuddin | d8c33d8 | 2011-08-18 18:30:55 +0000 | [diff] [blame] | 2402 | /* Call media count must be at least equal to SDP media. Note that |
| 2403 | * it may not be equal when remote removed any SDP media line. |
| 2404 | */ |
| 2405 | pj_assert(call->med_cnt >= local_sdp->media_count); |
| 2406 | |
Benny Prijono | 0bc99a9 | 2011-03-17 04:34:43 +0000 | [diff] [blame] | 2407 | /* Reset audio_idx first */ |
| 2408 | call->audio_idx = -1; |
| 2409 | |
Nanang Izzuddin | d8c33d8 | 2011-08-18 18:30:55 +0000 | [diff] [blame] | 2410 | /* Apply maximum audio/video count of the account */ |
| 2411 | sort_media(local_sdp, &STR_AUDIO, acc->cfg.use_srtp, |
| 2412 | maudidx, &maudcnt); |
| 2413 | #if PJMEDIA_HAS_VIDEO |
| 2414 | sort_media(local_sdp, &STR_VIDEO, acc->cfg.use_srtp, |
| 2415 | mvididx, &mvidcnt); |
| 2416 | #else |
| 2417 | PJ_UNUSED_ARG(STR_VIDEO); |
| 2418 | mvidcnt = 0; |
| 2419 | #endif |
| 2420 | if (maudcnt > acc->cfg.max_audio_cnt || mvidcnt > acc->cfg.max_video_cnt) |
| 2421 | { |
| 2422 | pjmedia_sdp_session *local_sdp2; |
| 2423 | |
| 2424 | maudcnt = PJ_MIN(maudcnt, acc->cfg.max_audio_cnt); |
| 2425 | mvidcnt = PJ_MIN(mvidcnt, acc->cfg.max_video_cnt); |
| 2426 | local_sdp2 = pjmedia_sdp_session_clone(tmp_pool, local_sdp); |
| 2427 | |
| 2428 | for (mi=0; mi < local_sdp2->media_count; ++mi) { |
| 2429 | pjmedia_sdp_media *m = local_sdp2->media[mi]; |
| 2430 | |
| 2431 | if (m->desc.port == 0 || |
| 2432 | pj_memchr(maudidx, mi, maudcnt*sizeof(maudidx[0])) || |
| 2433 | pj_memchr(mvididx, mi, mvidcnt*sizeof(mvididx[0]))) |
| 2434 | { |
| 2435 | continue; |
| 2436 | } |
| 2437 | |
| 2438 | /* Deactivate this media */ |
| 2439 | pjmedia_sdp_media_deactivate(tmp_pool, m); |
| 2440 | } |
| 2441 | |
| 2442 | local_sdp = local_sdp2; |
| 2443 | need_renego_sdp = PJ_TRUE; |
| 2444 | } |
| 2445 | |
Benny Prijono | 0bc99a9 | 2011-03-17 04:34:43 +0000 | [diff] [blame] | 2446 | /* Process each media stream */ |
| 2447 | for (mi=0; mi < call->med_cnt; ++mi) { |
| 2448 | pjsua_call_media *call_med = &call->media[mi]; |
| 2449 | |
Nanang Izzuddin | b6c239c | 2011-05-10 05:42:28 +0000 | [diff] [blame] | 2450 | if (mi >= local_sdp->media_count || |
| 2451 | mi >= remote_sdp->media_count) |
Benny Prijono | 0bc99a9 | 2011-03-17 04:34:43 +0000 | [diff] [blame] | 2452 | { |
Nanang Izzuddin | b6c239c | 2011-05-10 05:42:28 +0000 | [diff] [blame] | 2453 | /* This may happen when remote removed any SDP media lines in |
| 2454 | * its re-offer. |
| 2455 | */ |
| 2456 | continue; |
| 2457 | #if 0 |
Benny Prijono | 0bc99a9 | 2011-03-17 04:34:43 +0000 | [diff] [blame] | 2458 | /* Something is wrong */ |
| 2459 | PJ_LOG(1,(THIS_FILE, "Error updating media for call %d: " |
| 2460 | "invalid media index %d in SDP", call_id, mi)); |
Benny Prijono | b90fd38 | 2011-09-18 14:59:56 +0000 | [diff] [blame] | 2461 | status = PJMEDIA_SDP_EINSDP; |
| 2462 | goto on_error; |
Nanang Izzuddin | b6c239c | 2011-05-10 05:42:28 +0000 | [diff] [blame] | 2463 | #endif |
Benny Prijono | 0bc99a9 | 2011-03-17 04:34:43 +0000 | [diff] [blame] | 2464 | } |
| 2465 | |
| 2466 | switch (call_med->type) { |
| 2467 | case PJMEDIA_TYPE_AUDIO: |
| 2468 | status = audio_channel_update(call_med, tmp_pool, |
| 2469 | local_sdp, remote_sdp); |
| 2470 | if (call->audio_idx==-1 && status==PJ_SUCCESS && |
Nanang Izzuddin | bf26db1 | 2011-03-18 07:54:50 +0000 | [diff] [blame] | 2471 | call_med->strm.a.stream) |
Benny Prijono | 0bc99a9 | 2011-03-17 04:34:43 +0000 | [diff] [blame] | 2472 | { |
| 2473 | call->audio_idx = mi; |
| 2474 | } |
| 2475 | break; |
Nanang Izzuddin | 63b3c13 | 2011-07-19 11:11:07 +0000 | [diff] [blame] | 2476 | #if defined(PJMEDIA_HAS_VIDEO) && (PJMEDIA_HAS_VIDEO != 0) |
Benny Prijono | 0bc99a9 | 2011-03-17 04:34:43 +0000 | [diff] [blame] | 2477 | case PJMEDIA_TYPE_VIDEO: |
Nanang Izzuddin | bf26db1 | 2011-03-18 07:54:50 +0000 | [diff] [blame] | 2478 | status = video_channel_update(call_med, tmp_pool, |
| 2479 | local_sdp, remote_sdp); |
Benny Prijono | 0bc99a9 | 2011-03-17 04:34:43 +0000 | [diff] [blame] | 2480 | break; |
Nanang Izzuddin | 50fae73 | 2011-03-22 09:49:23 +0000 | [diff] [blame] | 2481 | #endif |
Benny Prijono | 0bc99a9 | 2011-03-17 04:34:43 +0000 | [diff] [blame] | 2482 | default: |
Nanang Izzuddin | b6c239c | 2011-05-10 05:42:28 +0000 | [diff] [blame] | 2483 | status = PJMEDIA_EINVALIMEDIATYPE; |
Benny Prijono | 0bc99a9 | 2011-03-17 04:34:43 +0000 | [diff] [blame] | 2484 | break; |
| 2485 | } |
| 2486 | |
Nanang Izzuddin | d8c33d8 | 2011-08-18 18:30:55 +0000 | [diff] [blame] | 2487 | /* Close the transport of deactivated media, need this here as media |
| 2488 | * can be deactivated by the SDP negotiation and the max media count |
| 2489 | * (account) setting. |
| 2490 | */ |
| 2491 | if (local_sdp->media[mi]->desc.port==0 && call_med->tp) { |
| 2492 | pjmedia_transport_close(call_med->tp); |
| 2493 | call_med->tp = call_med->tp_orig = NULL; |
Sauw Ming | 73ecfe8 | 2011-09-21 10:20:01 +0000 | [diff] [blame] | 2494 | set_media_tp_state(call_med, PJSUA_MED_TP_IDLE); |
Nanang Izzuddin | d8c33d8 | 2011-08-18 18:30:55 +0000 | [diff] [blame] | 2495 | } |
| 2496 | |
Benny Prijono | 0bc99a9 | 2011-03-17 04:34:43 +0000 | [diff] [blame] | 2497 | if (status != PJ_SUCCESS) { |
Nanang Izzuddin | b6c239c | 2011-05-10 05:42:28 +0000 | [diff] [blame] | 2498 | PJ_PERROR(1,(THIS_FILE, status, "Error updating media call%02d:%d", |
Benny Prijono | 0bc99a9 | 2011-03-17 04:34:43 +0000 | [diff] [blame] | 2499 | call_id, mi)); |
Nanang Izzuddin | b6c239c | 2011-05-10 05:42:28 +0000 | [diff] [blame] | 2500 | } else { |
| 2501 | got_media = PJ_TRUE; |
Benny Prijono | 0bc99a9 | 2011-03-17 04:34:43 +0000 | [diff] [blame] | 2502 | } |
| 2503 | } |
| 2504 | |
Nanang Izzuddin | d8c33d8 | 2011-08-18 18:30:55 +0000 | [diff] [blame] | 2505 | /* Perform SDP re-negotiation if needed. */ |
| 2506 | if (got_media && need_renego_sdp) { |
| 2507 | pjmedia_sdp_neg *neg = call->inv->neg; |
| 2508 | |
| 2509 | /* This should only happen when we are the answerer. */ |
| 2510 | PJ_ASSERT_RETURN(neg && !pjmedia_sdp_neg_was_answer_remote(neg), |
| 2511 | PJMEDIA_SDPNEG_EINSTATE); |
| 2512 | |
| 2513 | status = pjmedia_sdp_neg_set_remote_offer(tmp_pool, neg, remote_sdp); |
| 2514 | if (status != PJ_SUCCESS) |
Benny Prijono | b90fd38 | 2011-09-18 14:59:56 +0000 | [diff] [blame] | 2515 | goto on_error; |
Nanang Izzuddin | d8c33d8 | 2011-08-18 18:30:55 +0000 | [diff] [blame] | 2516 | |
| 2517 | status = pjmedia_sdp_neg_set_local_answer(tmp_pool, neg, local_sdp); |
| 2518 | if (status != PJ_SUCCESS) |
Benny Prijono | b90fd38 | 2011-09-18 14:59:56 +0000 | [diff] [blame] | 2519 | goto on_error; |
Nanang Izzuddin | d8c33d8 | 2011-08-18 18:30:55 +0000 | [diff] [blame] | 2520 | |
| 2521 | status = pjmedia_sdp_neg_negotiate(tmp_pool, neg, 0); |
| 2522 | if (status != PJ_SUCCESS) |
Benny Prijono | b90fd38 | 2011-09-18 14:59:56 +0000 | [diff] [blame] | 2523 | goto on_error; |
Nanang Izzuddin | d8c33d8 | 2011-08-18 18:30:55 +0000 | [diff] [blame] | 2524 | } |
| 2525 | |
Benny Prijono | b90fd38 | 2011-09-18 14:59:56 +0000 | [diff] [blame] | 2526 | pj_log_pop_indent(); |
Nanang Izzuddin | b6c239c | 2011-05-10 05:42:28 +0000 | [diff] [blame] | 2527 | return (got_media? PJ_SUCCESS : PJMEDIA_SDPNEG_ENOMEDIA); |
Benny Prijono | b90fd38 | 2011-09-18 14:59:56 +0000 | [diff] [blame] | 2528 | |
| 2529 | on_error: |
| 2530 | pj_log_pop_indent(); |
| 2531 | return status; |
Benny Prijono | 0bc99a9 | 2011-03-17 04:34:43 +0000 | [diff] [blame] | 2532 | } |
| 2533 | |
Benny Prijono | eebe9af | 2006-06-13 22:57:13 +0000 | [diff] [blame] | 2534 | /* |
| 2535 | * Get maxinum number of conference ports. |
| 2536 | */ |
| 2537 | PJ_DEF(unsigned) pjsua_conf_get_max_ports(void) |
| 2538 | { |
| 2539 | return pjsua_var.media_cfg.max_media_ports; |
| 2540 | } |
| 2541 | |
| 2542 | |
| 2543 | /* |
| 2544 | * Get current number of active ports in the bridge. |
| 2545 | */ |
| 2546 | PJ_DEF(unsigned) pjsua_conf_get_active_ports(void) |
| 2547 | { |
Benny Prijono | 38fb3ea | 2008-01-02 08:27:03 +0000 | [diff] [blame] | 2548 | unsigned ports[PJSUA_MAX_CONF_PORTS]; |
Benny Prijono | eebe9af | 2006-06-13 22:57:13 +0000 | [diff] [blame] | 2549 | unsigned count = PJ_ARRAY_SIZE(ports); |
| 2550 | pj_status_t status; |
| 2551 | |
| 2552 | status = pjmedia_conf_enum_ports(pjsua_var.mconf, ports, &count); |
| 2553 | if (status != PJ_SUCCESS) |
| 2554 | count = 0; |
| 2555 | |
| 2556 | return count; |
| 2557 | } |
| 2558 | |
| 2559 | |
| 2560 | /* |
| 2561 | * Enumerate all conference ports. |
| 2562 | */ |
| 2563 | PJ_DEF(pj_status_t) pjsua_enum_conf_ports(pjsua_conf_port_id id[], |
| 2564 | unsigned *count) |
| 2565 | { |
| 2566 | return pjmedia_conf_enum_ports(pjsua_var.mconf, (unsigned*)id, count); |
| 2567 | } |
| 2568 | |
| 2569 | |
| 2570 | /* |
| 2571 | * Get information about the specified conference port |
| 2572 | */ |
| 2573 | PJ_DEF(pj_status_t) pjsua_conf_get_port_info( pjsua_conf_port_id id, |
| 2574 | pjsua_conf_port_info *info) |
| 2575 | { |
| 2576 | pjmedia_conf_port_info cinfo; |
Benny Prijono | 0498d90 | 2006-06-19 14:49:14 +0000 | [diff] [blame] | 2577 | unsigned i; |
Benny Prijono | eebe9af | 2006-06-13 22:57:13 +0000 | [diff] [blame] | 2578 | pj_status_t status; |
| 2579 | |
| 2580 | status = pjmedia_conf_get_port_info( pjsua_var.mconf, id, &cinfo); |
| 2581 | if (status != PJ_SUCCESS) |
| 2582 | return status; |
| 2583 | |
Benny Prijono | ac623b3 | 2006-07-03 15:19:31 +0000 | [diff] [blame] | 2584 | pj_bzero(info, sizeof(*info)); |
Benny Prijono | eebe9af | 2006-06-13 22:57:13 +0000 | [diff] [blame] | 2585 | info->slot_id = id; |
| 2586 | info->name = cinfo.name; |
| 2587 | info->clock_rate = cinfo.clock_rate; |
| 2588 | info->channel_count = cinfo.channel_count; |
| 2589 | info->samples_per_frame = cinfo.samples_per_frame; |
| 2590 | info->bits_per_sample = cinfo.bits_per_sample; |
| 2591 | |
| 2592 | /* Build array of listeners */ |
Benny Prijono | c78c3a3 | 2006-06-16 15:54:43 +0000 | [diff] [blame] | 2593 | info->listener_cnt = cinfo.listener_cnt; |
| 2594 | for (i=0; i<cinfo.listener_cnt; ++i) { |
| 2595 | info->listeners[i] = cinfo.listener_slots[i]; |
Benny Prijono | eebe9af | 2006-06-13 22:57:13 +0000 | [diff] [blame] | 2596 | } |
| 2597 | |
| 2598 | return PJ_SUCCESS; |
| 2599 | } |
| 2600 | |
| 2601 | |
| 2602 | /* |
Benny Prijono | e909eac | 2006-07-27 22:04:56 +0000 | [diff] [blame] | 2603 | * Add arbitrary media port to PJSUA's conference bridge. |
| 2604 | */ |
| 2605 | PJ_DEF(pj_status_t) pjsua_conf_add_port( pj_pool_t *pool, |
| 2606 | pjmedia_port *port, |
| 2607 | pjsua_conf_port_id *p_id) |
| 2608 | { |
| 2609 | pj_status_t status; |
| 2610 | |
| 2611 | status = pjmedia_conf_add_port(pjsua_var.mconf, pool, |
| 2612 | port, NULL, (unsigned*)p_id); |
| 2613 | if (status != PJ_SUCCESS) { |
| 2614 | if (p_id) |
| 2615 | *p_id = PJSUA_INVALID_ID; |
| 2616 | } |
| 2617 | |
| 2618 | return status; |
| 2619 | } |
| 2620 | |
| 2621 | |
| 2622 | /* |
| 2623 | * Remove arbitrary slot from the conference bridge. |
| 2624 | */ |
| 2625 | PJ_DEF(pj_status_t) pjsua_conf_remove_port(pjsua_conf_port_id id) |
| 2626 | { |
Nanang Izzuddin | 148fd39 | 2008-06-16 09:52:50 +0000 | [diff] [blame] | 2627 | pj_status_t status; |
| 2628 | |
| 2629 | status = pjmedia_conf_remove_port(pjsua_var.mconf, (unsigned)id); |
| 2630 | check_snd_dev_idle(); |
| 2631 | |
| 2632 | return status; |
Benny Prijono | e909eac | 2006-07-27 22:04:56 +0000 | [diff] [blame] | 2633 | } |
| 2634 | |
| 2635 | |
| 2636 | /* |
Benny Prijono | eebe9af | 2006-06-13 22:57:13 +0000 | [diff] [blame] | 2637 | * Establish unidirectional media flow from souce to sink. |
| 2638 | */ |
| 2639 | PJ_DEF(pj_status_t) pjsua_conf_connect( pjsua_conf_port_id source, |
| 2640 | pjsua_conf_port_id sink) |
| 2641 | { |
Benny Prijono | b90fd38 | 2011-09-18 14:59:56 +0000 | [diff] [blame] | 2642 | pj_status_t status = PJ_SUCCESS; |
| 2643 | |
| 2644 | PJ_LOG(4,(THIS_FILE, "%s connect: %d --> %d", |
| 2645 | (pjsua_var.is_mswitch ? "Switch" : "Conf"), |
| 2646 | source, sink)); |
| 2647 | pj_log_push_indent(); |
| 2648 | |
Nanang Izzuddin | 68559c3 | 2008-06-13 17:01:46 +0000 | [diff] [blame] | 2649 | /* If sound device idle timer is active, cancel it first. */ |
Benny Prijono | 0f711b4 | 2009-05-06 19:08:43 +0000 | [diff] [blame] | 2650 | PJSUA_LOCK(); |
Nanang Izzuddin | 68559c3 | 2008-06-13 17:01:46 +0000 | [diff] [blame] | 2651 | if (pjsua_var.snd_idle_timer.id) { |
| 2652 | pjsip_endpt_cancel_timer(pjsua_var.endpt, &pjsua_var.snd_idle_timer); |
| 2653 | pjsua_var.snd_idle_timer.id = PJ_FALSE; |
| 2654 | } |
Benny Prijono | 0f711b4 | 2009-05-06 19:08:43 +0000 | [diff] [blame] | 2655 | PJSUA_UNLOCK(); |
Nanang Izzuddin | 68559c3 | 2008-06-13 17:01:46 +0000 | [diff] [blame] | 2656 | |
Nanang Izzuddin | fe02a06 | 2009-02-18 14:28:49 +0000 | [diff] [blame] | 2657 | |
Benny Prijono | f798e50 | 2009-03-09 13:08:16 +0000 | [diff] [blame] | 2658 | /* For audio switchboard (i.e. APS-Direct): |
| 2659 | * Check if sound device need to be reopened, i.e: its attributes |
Nanang Izzuddin | fe02a06 | 2009-02-18 14:28:49 +0000 | [diff] [blame] | 2660 | * (format, clock rate, channel count) must match to peer's. |
| 2661 | * Note that sound device can be reopened only if it doesn't have |
| 2662 | * any connection. |
| 2663 | */ |
Benny Prijono | f798e50 | 2009-03-09 13:08:16 +0000 | [diff] [blame] | 2664 | if (pjsua_var.is_mswitch) { |
Nanang Izzuddin | fe02a06 | 2009-02-18 14:28:49 +0000 | [diff] [blame] | 2665 | pjmedia_conf_port_info port0_info; |
| 2666 | pjmedia_conf_port_info peer_info; |
| 2667 | unsigned peer_id; |
| 2668 | pj_bool_t need_reopen = PJ_FALSE; |
Nanang Izzuddin | fe02a06 | 2009-02-18 14:28:49 +0000 | [diff] [blame] | 2669 | |
| 2670 | peer_id = (source!=0)? source : sink; |
| 2671 | status = pjmedia_conf_get_port_info(pjsua_var.mconf, peer_id, |
| 2672 | &peer_info); |
| 2673 | pj_assert(status == PJ_SUCCESS); |
| 2674 | |
| 2675 | status = pjmedia_conf_get_port_info(pjsua_var.mconf, 0, &port0_info); |
| 2676 | pj_assert(status == PJ_SUCCESS); |
| 2677 | |
| 2678 | /* Check if sound device is instantiated. */ |
| 2679 | need_reopen = (pjsua_var.snd_port==NULL && pjsua_var.null_snd==NULL && |
| 2680 | !pjsua_var.no_snd); |
| 2681 | |
| 2682 | /* Check if sound device need to reopen because it needs to modify |
| 2683 | * settings to match its peer. Sound device must be idle in this case |
| 2684 | * though. |
| 2685 | */ |
| 2686 | if (!need_reopen && |
| 2687 | port0_info.listener_cnt==0 && port0_info.transmitter_cnt==0) |
| 2688 | { |
| 2689 | need_reopen = (peer_info.format.id != port0_info.format.id || |
Benny Prijono | c45d951 | 2010-12-10 11:04:30 +0000 | [diff] [blame] | 2690 | peer_info.format.det.aud.avg_bps != |
| 2691 | port0_info.format.det.aud.avg_bps || |
Nanang Izzuddin | fe02a06 | 2009-02-18 14:28:49 +0000 | [diff] [blame] | 2692 | peer_info.clock_rate != port0_info.clock_rate || |
Benny Prijono | c45d951 | 2010-12-10 11:04:30 +0000 | [diff] [blame] | 2693 | peer_info.channel_count!=port0_info.channel_count); |
Nanang Izzuddin | fe02a06 | 2009-02-18 14:28:49 +0000 | [diff] [blame] | 2694 | } |
| 2695 | |
| 2696 | if (need_reopen) { |
Benny Prijono | d65f78c | 2009-06-03 18:59:37 +0000 | [diff] [blame] | 2697 | if (pjsua_var.cap_dev != NULL_SND_DEV_ID) { |
Sauw Ming | 98766c7 | 2011-03-11 06:57:24 +0000 | [diff] [blame] | 2698 | pjmedia_snd_port_param param; |
Nanang Izzuddin | fe02a06 | 2009-02-18 14:28:49 +0000 | [diff] [blame] | 2699 | |
Benny Prijono | d65f78c | 2009-06-03 18:59:37 +0000 | [diff] [blame] | 2700 | /* Create parameter based on peer info */ |
Sauw Ming | 98766c7 | 2011-03-11 06:57:24 +0000 | [diff] [blame] | 2701 | status = create_aud_param(¶m.base, pjsua_var.cap_dev, |
Benny Prijono | d65f78c | 2009-06-03 18:59:37 +0000 | [diff] [blame] | 2702 | pjsua_var.play_dev, |
| 2703 | peer_info.clock_rate, |
| 2704 | peer_info.channel_count, |
| 2705 | peer_info.samples_per_frame, |
| 2706 | peer_info.bits_per_sample); |
| 2707 | if (status != PJ_SUCCESS) { |
Benny Prijono | c45d951 | 2010-12-10 11:04:30 +0000 | [diff] [blame] | 2708 | pjsua_perror(THIS_FILE, "Error opening sound device", |
| 2709 | status); |
Benny Prijono | b90fd38 | 2011-09-18 14:59:56 +0000 | [diff] [blame] | 2710 | goto on_return; |
Benny Prijono | d65f78c | 2009-06-03 18:59:37 +0000 | [diff] [blame] | 2711 | } |
Benny Prijono | f798e50 | 2009-03-09 13:08:16 +0000 | [diff] [blame] | 2712 | |
Benny Prijono | d65f78c | 2009-06-03 18:59:37 +0000 | [diff] [blame] | 2713 | /* And peer format */ |
| 2714 | if (peer_info.format.id != PJMEDIA_FORMAT_PCM) { |
Sauw Ming | 98766c7 | 2011-03-11 06:57:24 +0000 | [diff] [blame] | 2715 | param.base.flags |= PJMEDIA_AUD_DEV_CAP_EXT_FORMAT; |
| 2716 | param.base.ext_fmt = peer_info.format; |
Benny Prijono | d65f78c | 2009-06-03 18:59:37 +0000 | [diff] [blame] | 2717 | } |
Benny Prijono | 10454dc | 2009-02-21 14:21:59 +0000 | [diff] [blame] | 2718 | |
Sauw Ming | 98766c7 | 2011-03-11 06:57:24 +0000 | [diff] [blame] | 2719 | param.options = 0; |
Benny Prijono | d65f78c | 2009-06-03 18:59:37 +0000 | [diff] [blame] | 2720 | status = open_snd_dev(¶m); |
| 2721 | if (status != PJ_SUCCESS) { |
Benny Prijono | c45d951 | 2010-12-10 11:04:30 +0000 | [diff] [blame] | 2722 | pjsua_perror(THIS_FILE, "Error opening sound device", |
| 2723 | status); |
Benny Prijono | b90fd38 | 2011-09-18 14:59:56 +0000 | [diff] [blame] | 2724 | goto on_return; |
Benny Prijono | d65f78c | 2009-06-03 18:59:37 +0000 | [diff] [blame] | 2725 | } |
| 2726 | } else { |
| 2727 | /* Null-audio */ |
Benny Prijono | c45d951 | 2010-12-10 11:04:30 +0000 | [diff] [blame] | 2728 | status = pjsua_set_snd_dev(pjsua_var.cap_dev, |
| 2729 | pjsua_var.play_dev); |
Benny Prijono | d65f78c | 2009-06-03 18:59:37 +0000 | [diff] [blame] | 2730 | if (status != PJ_SUCCESS) { |
Benny Prijono | c45d951 | 2010-12-10 11:04:30 +0000 | [diff] [blame] | 2731 | pjsua_perror(THIS_FILE, "Error opening sound device", |
| 2732 | status); |
Benny Prijono | b90fd38 | 2011-09-18 14:59:56 +0000 | [diff] [blame] | 2733 | goto on_return; |
Benny Prijono | d65f78c | 2009-06-03 18:59:37 +0000 | [diff] [blame] | 2734 | } |
Nanang Izzuddin | fe02a06 | 2009-02-18 14:28:49 +0000 | [diff] [blame] | 2735 | } |
Benny Prijono | 2d64772 | 2011-07-13 03:05:22 +0000 | [diff] [blame] | 2736 | } else if (pjsua_var.no_snd) { |
| 2737 | if (!pjsua_var.snd_is_on) { |
| 2738 | pjsua_var.snd_is_on = PJ_TRUE; |
| 2739 | /* Notify app */ |
| 2740 | if (pjsua_var.ua_cfg.cb.on_snd_dev_operation) { |
| 2741 | (*pjsua_var.ua_cfg.cb.on_snd_dev_operation)(1); |
| 2742 | } |
| 2743 | } |
Nanang Izzuddin | fe02a06 | 2009-02-18 14:28:49 +0000 | [diff] [blame] | 2744 | } |
Nanang Izzuddin | fe02a06 | 2009-02-18 14:28:49 +0000 | [diff] [blame] | 2745 | |
Benny Prijono | f798e50 | 2009-03-09 13:08:16 +0000 | [diff] [blame] | 2746 | } else { |
| 2747 | /* The bridge version */ |
Nanang Izzuddin | fe02a06 | 2009-02-18 14:28:49 +0000 | [diff] [blame] | 2748 | |
Benny Prijono | f798e50 | 2009-03-09 13:08:16 +0000 | [diff] [blame] | 2749 | /* Create sound port if none is instantiated */ |
| 2750 | if (pjsua_var.snd_port==NULL && pjsua_var.null_snd==NULL && |
| 2751 | !pjsua_var.no_snd) |
| 2752 | { |
| 2753 | pj_status_t status; |
Nanang Izzuddin | 68559c3 | 2008-06-13 17:01:46 +0000 | [diff] [blame] | 2754 | |
Benny Prijono | f798e50 | 2009-03-09 13:08:16 +0000 | [diff] [blame] | 2755 | status = pjsua_set_snd_dev(pjsua_var.cap_dev, pjsua_var.play_dev); |
| 2756 | if (status != PJ_SUCCESS) { |
| 2757 | pjsua_perror(THIS_FILE, "Error opening sound device", status); |
Benny Prijono | b90fd38 | 2011-09-18 14:59:56 +0000 | [diff] [blame] | 2758 | goto on_return; |
Benny Prijono | f798e50 | 2009-03-09 13:08:16 +0000 | [diff] [blame] | 2759 | } |
Benny Prijono | 2d64772 | 2011-07-13 03:05:22 +0000 | [diff] [blame] | 2760 | } else if (pjsua_var.no_snd && !pjsua_var.snd_is_on) { |
| 2761 | pjsua_var.snd_is_on = PJ_TRUE; |
| 2762 | /* Notify app */ |
| 2763 | if (pjsua_var.ua_cfg.cb.on_snd_dev_operation) { |
| 2764 | (*pjsua_var.ua_cfg.cb.on_snd_dev_operation)(1); |
| 2765 | } |
Nanang Izzuddin | 68559c3 | 2008-06-13 17:01:46 +0000 | [diff] [blame] | 2766 | } |
Benny Prijono | f798e50 | 2009-03-09 13:08:16 +0000 | [diff] [blame] | 2767 | } |
Nanang Izzuddin | fe02a06 | 2009-02-18 14:28:49 +0000 | [diff] [blame] | 2768 | |
Benny Prijono | b90fd38 | 2011-09-18 14:59:56 +0000 | [diff] [blame] | 2769 | status = pjmedia_conf_connect_port(pjsua_var.mconf, source, sink, 0); |
| 2770 | |
| 2771 | on_return: |
| 2772 | pj_log_pop_indent(); |
| 2773 | return status; |
Benny Prijono | eebe9af | 2006-06-13 22:57:13 +0000 | [diff] [blame] | 2774 | } |
| 2775 | |
| 2776 | |
| 2777 | /* |
| 2778 | * Disconnect media flow from the source to destination port. |
| 2779 | */ |
| 2780 | PJ_DEF(pj_status_t) pjsua_conf_disconnect( pjsua_conf_port_id source, |
| 2781 | pjsua_conf_port_id sink) |
| 2782 | { |
Nanang Izzuddin | 68559c3 | 2008-06-13 17:01:46 +0000 | [diff] [blame] | 2783 | pj_status_t status; |
| 2784 | |
Benny Prijono | b90fd38 | 2011-09-18 14:59:56 +0000 | [diff] [blame] | 2785 | PJ_LOG(4,(THIS_FILE, "%s disconnect: %d -x- %d", |
| 2786 | (pjsua_var.is_mswitch ? "Switch" : "Conf"), |
| 2787 | source, sink)); |
| 2788 | pj_log_push_indent(); |
| 2789 | |
Nanang Izzuddin | 68559c3 | 2008-06-13 17:01:46 +0000 | [diff] [blame] | 2790 | status = pjmedia_conf_disconnect_port(pjsua_var.mconf, source, sink); |
Nanang Izzuddin | 148fd39 | 2008-06-16 09:52:50 +0000 | [diff] [blame] | 2791 | check_snd_dev_idle(); |
Nanang Izzuddin | 68559c3 | 2008-06-13 17:01:46 +0000 | [diff] [blame] | 2792 | |
Benny Prijono | b90fd38 | 2011-09-18 14:59:56 +0000 | [diff] [blame] | 2793 | pj_log_pop_indent(); |
Nanang Izzuddin | 68559c3 | 2008-06-13 17:01:46 +0000 | [diff] [blame] | 2794 | return status; |
Benny Prijono | eebe9af | 2006-06-13 22:57:13 +0000 | [diff] [blame] | 2795 | } |
| 2796 | |
| 2797 | |
Benny Prijono | 6dd967c | 2006-12-26 02:27:14 +0000 | [diff] [blame] | 2798 | /* |
| 2799 | * Adjust the signal level to be transmitted from the bridge to the |
| 2800 | * specified port by making it louder or quieter. |
| 2801 | */ |
| 2802 | PJ_DEF(pj_status_t) pjsua_conf_adjust_tx_level(pjsua_conf_port_id slot, |
| 2803 | float level) |
| 2804 | { |
| 2805 | return pjmedia_conf_adjust_tx_level(pjsua_var.mconf, slot, |
| 2806 | (int)((level-1) * 128)); |
| 2807 | } |
| 2808 | |
| 2809 | /* |
| 2810 | * Adjust the signal level to be received from the specified port (to |
| 2811 | * the bridge) by making it louder or quieter. |
| 2812 | */ |
| 2813 | PJ_DEF(pj_status_t) pjsua_conf_adjust_rx_level(pjsua_conf_port_id slot, |
| 2814 | float level) |
| 2815 | { |
| 2816 | return pjmedia_conf_adjust_rx_level(pjsua_var.mconf, slot, |
| 2817 | (int)((level-1) * 128)); |
| 2818 | } |
| 2819 | |
| 2820 | |
| 2821 | /* |
| 2822 | * Get last signal level transmitted to or received from the specified port. |
| 2823 | */ |
| 2824 | PJ_DEF(pj_status_t) pjsua_conf_get_signal_level(pjsua_conf_port_id slot, |
| 2825 | unsigned *tx_level, |
| 2826 | unsigned *rx_level) |
| 2827 | { |
| 2828 | return pjmedia_conf_get_signal_level(pjsua_var.mconf, slot, |
| 2829 | tx_level, rx_level); |
| 2830 | } |
| 2831 | |
Benny Prijono | eebe9af | 2006-06-13 22:57:13 +0000 | [diff] [blame] | 2832 | /***************************************************************************** |
| 2833 | * File player. |
| 2834 | */ |
| 2835 | |
Benny Prijono | d5696da | 2007-07-17 16:25:45 +0000 | [diff] [blame] | 2836 | static char* get_basename(const char *path, unsigned len) |
| 2837 | { |
| 2838 | char *p = ((char*)path) + len; |
| 2839 | |
| 2840 | if (len==0) |
| 2841 | return p; |
| 2842 | |
Benny Prijono | 1f61a8f | 2007-08-16 10:11:44 +0000 | [diff] [blame] | 2843 | for (--p; p!=path && *p!='/' && *p!='\\'; ) --p; |
Benny Prijono | d5696da | 2007-07-17 16:25:45 +0000 | [diff] [blame] | 2844 | |
| 2845 | return (p==path) ? p : p+1; |
| 2846 | } |
| 2847 | |
| 2848 | |
Benny Prijono | eebe9af | 2006-06-13 22:57:13 +0000 | [diff] [blame] | 2849 | /* |
| 2850 | * Create a file player, and automatically connect this player to |
| 2851 | * the conference bridge. |
| 2852 | */ |
| 2853 | PJ_DEF(pj_status_t) pjsua_player_create( const pj_str_t *filename, |
| 2854 | unsigned options, |
Benny Prijono | eebe9af | 2006-06-13 22:57:13 +0000 | [diff] [blame] | 2855 | pjsua_player_id *p_id) |
| 2856 | { |
| 2857 | unsigned slot, file_id; |
Benny Prijono | a66c331 | 2007-01-21 23:12:40 +0000 | [diff] [blame] | 2858 | char path[PJ_MAXPATH]; |
Benny Prijono | b90fd38 | 2011-09-18 14:59:56 +0000 | [diff] [blame] | 2859 | pj_pool_t *pool = NULL; |
Benny Prijono | eebe9af | 2006-06-13 22:57:13 +0000 | [diff] [blame] | 2860 | pjmedia_port *port; |
Benny Prijono | b90fd38 | 2011-09-18 14:59:56 +0000 | [diff] [blame] | 2861 | pj_status_t status = PJ_SUCCESS; |
Benny Prijono | eebe9af | 2006-06-13 22:57:13 +0000 | [diff] [blame] | 2862 | |
| 2863 | if (pjsua_var.player_cnt >= PJ_ARRAY_SIZE(pjsua_var.player)) |
| 2864 | return PJ_ETOOMANY; |
| 2865 | |
Benny Prijono | b90fd38 | 2011-09-18 14:59:56 +0000 | [diff] [blame] | 2866 | PJ_LOG(4,(THIS_FILE, "Creating file player: %.*s..", |
| 2867 | (int)filename->slen, filename->ptr)); |
| 2868 | pj_log_push_indent(); |
| 2869 | |
Benny Prijono | eebe9af | 2006-06-13 22:57:13 +0000 | [diff] [blame] | 2870 | PJSUA_LOCK(); |
| 2871 | |
| 2872 | for (file_id=0; file_id<PJ_ARRAY_SIZE(pjsua_var.player); ++file_id) { |
| 2873 | if (pjsua_var.player[file_id].port == NULL) |
| 2874 | break; |
| 2875 | } |
| 2876 | |
| 2877 | if (file_id == PJ_ARRAY_SIZE(pjsua_var.player)) { |
| 2878 | /* This is unexpected */ |
Benny Prijono | eebe9af | 2006-06-13 22:57:13 +0000 | [diff] [blame] | 2879 | pj_assert(0); |
Benny Prijono | b90fd38 | 2011-09-18 14:59:56 +0000 | [diff] [blame] | 2880 | status = PJ_EBUG; |
| 2881 | goto on_error; |
Benny Prijono | eebe9af | 2006-06-13 22:57:13 +0000 | [diff] [blame] | 2882 | } |
| 2883 | |
| 2884 | pj_memcpy(path, filename->ptr, filename->slen); |
| 2885 | path[filename->slen] = '\0'; |
Benny Prijono | d5696da | 2007-07-17 16:25:45 +0000 | [diff] [blame] | 2886 | |
| 2887 | pool = pjsua_pool_create(get_basename(path, filename->slen), 1000, 1000); |
| 2888 | if (!pool) { |
Benny Prijono | b90fd38 | 2011-09-18 14:59:56 +0000 | [diff] [blame] | 2889 | status = PJ_ENOMEM; |
| 2890 | goto on_error; |
Benny Prijono | d5696da | 2007-07-17 16:25:45 +0000 | [diff] [blame] | 2891 | } |
| 2892 | |
Nanang Izzuddin | 81e9bd5 | 2008-06-27 12:52:51 +0000 | [diff] [blame] | 2893 | status = pjmedia_wav_player_port_create( |
| 2894 | pool, path, |
| 2895 | pjsua_var.mconf_cfg.samples_per_frame * |
Benny Prijono | c45d951 | 2010-12-10 11:04:30 +0000 | [diff] [blame] | 2896 | 1000 / pjsua_var.media_cfg.channel_count / |
Nanang Izzuddin | 81e9bd5 | 2008-06-27 12:52:51 +0000 | [diff] [blame] | 2897 | pjsua_var.media_cfg.clock_rate, |
| 2898 | options, 0, &port); |
Benny Prijono | eebe9af | 2006-06-13 22:57:13 +0000 | [diff] [blame] | 2899 | if (status != PJ_SUCCESS) { |
Benny Prijono | eebe9af | 2006-06-13 22:57:13 +0000 | [diff] [blame] | 2900 | pjsua_perror(THIS_FILE, "Unable to open file for playback", status); |
Benny Prijono | b90fd38 | 2011-09-18 14:59:56 +0000 | [diff] [blame] | 2901 | goto on_error; |
Benny Prijono | eebe9af | 2006-06-13 22:57:13 +0000 | [diff] [blame] | 2902 | } |
| 2903 | |
Benny Prijono | 5297af9 | 2008-03-18 13:40:40 +0000 | [diff] [blame] | 2904 | status = pjmedia_conf_add_port(pjsua_var.mconf, pool, |
Benny Prijono | eebe9af | 2006-06-13 22:57:13 +0000 | [diff] [blame] | 2905 | port, filename, &slot); |
| 2906 | if (status != PJ_SUCCESS) { |
| 2907 | pjmedia_port_destroy(port); |
Benny Prijono | 32e4f49 | 2007-01-24 00:44:26 +0000 | [diff] [blame] | 2908 | pjsua_perror(THIS_FILE, "Unable to add file to conference bridge", |
| 2909 | status); |
Benny Prijono | b90fd38 | 2011-09-18 14:59:56 +0000 | [diff] [blame] | 2910 | goto on_error; |
Benny Prijono | eebe9af | 2006-06-13 22:57:13 +0000 | [diff] [blame] | 2911 | } |
| 2912 | |
Benny Prijono | a66c331 | 2007-01-21 23:12:40 +0000 | [diff] [blame] | 2913 | pjsua_var.player[file_id].type = 0; |
Benny Prijono | d5696da | 2007-07-17 16:25:45 +0000 | [diff] [blame] | 2914 | pjsua_var.player[file_id].pool = pool; |
Benny Prijono | eebe9af | 2006-06-13 22:57:13 +0000 | [diff] [blame] | 2915 | pjsua_var.player[file_id].port = port; |
| 2916 | pjsua_var.player[file_id].slot = slot; |
| 2917 | |
| 2918 | if (p_id) *p_id = file_id; |
| 2919 | |
| 2920 | ++pjsua_var.player_cnt; |
| 2921 | |
| 2922 | PJSUA_UNLOCK(); |
Benny Prijono | b90fd38 | 2011-09-18 14:59:56 +0000 | [diff] [blame] | 2923 | |
| 2924 | PJ_LOG(4,(THIS_FILE, "Player created, id=%d, slot=%d", file_id, slot)); |
| 2925 | |
| 2926 | pj_log_pop_indent(); |
Benny Prijono | eebe9af | 2006-06-13 22:57:13 +0000 | [diff] [blame] | 2927 | return PJ_SUCCESS; |
Benny Prijono | b90fd38 | 2011-09-18 14:59:56 +0000 | [diff] [blame] | 2928 | |
| 2929 | on_error: |
| 2930 | PJSUA_UNLOCK(); |
| 2931 | if (pool) pj_pool_release(pool); |
| 2932 | pj_log_pop_indent(); |
| 2933 | return status; |
Benny Prijono | eebe9af | 2006-06-13 22:57:13 +0000 | [diff] [blame] | 2934 | } |
| 2935 | |
| 2936 | |
| 2937 | /* |
Benny Prijono | a66c331 | 2007-01-21 23:12:40 +0000 | [diff] [blame] | 2938 | * Create a file playlist media port, and automatically add the port |
| 2939 | * to the conference bridge. |
| 2940 | */ |
| 2941 | PJ_DEF(pj_status_t) pjsua_playlist_create( const pj_str_t file_names[], |
| 2942 | unsigned file_count, |
| 2943 | const pj_str_t *label, |
| 2944 | unsigned options, |
| 2945 | pjsua_player_id *p_id) |
| 2946 | { |
| 2947 | unsigned slot, file_id, ptime; |
Benny Prijono | b90fd38 | 2011-09-18 14:59:56 +0000 | [diff] [blame] | 2948 | pj_pool_t *pool = NULL; |
Benny Prijono | a66c331 | 2007-01-21 23:12:40 +0000 | [diff] [blame] | 2949 | pjmedia_port *port; |
Benny Prijono | b90fd38 | 2011-09-18 14:59:56 +0000 | [diff] [blame] | 2950 | pj_status_t status = PJ_SUCCESS; |
Benny Prijono | a66c331 | 2007-01-21 23:12:40 +0000 | [diff] [blame] | 2951 | |
| 2952 | if (pjsua_var.player_cnt >= PJ_ARRAY_SIZE(pjsua_var.player)) |
| 2953 | return PJ_ETOOMANY; |
| 2954 | |
Benny Prijono | b90fd38 | 2011-09-18 14:59:56 +0000 | [diff] [blame] | 2955 | PJ_LOG(4,(THIS_FILE, "Creating playlist with %d file(s)..", file_count)); |
| 2956 | pj_log_push_indent(); |
| 2957 | |
Benny Prijono | a66c331 | 2007-01-21 23:12:40 +0000 | [diff] [blame] | 2958 | PJSUA_LOCK(); |
| 2959 | |
| 2960 | for (file_id=0; file_id<PJ_ARRAY_SIZE(pjsua_var.player); ++file_id) { |
| 2961 | if (pjsua_var.player[file_id].port == NULL) |
| 2962 | break; |
| 2963 | } |
| 2964 | |
| 2965 | if (file_id == PJ_ARRAY_SIZE(pjsua_var.player)) { |
| 2966 | /* This is unexpected */ |
Benny Prijono | a66c331 | 2007-01-21 23:12:40 +0000 | [diff] [blame] | 2967 | pj_assert(0); |
Benny Prijono | b90fd38 | 2011-09-18 14:59:56 +0000 | [diff] [blame] | 2968 | status = PJ_EBUG; |
| 2969 | goto on_error; |
Benny Prijono | a66c331 | 2007-01-21 23:12:40 +0000 | [diff] [blame] | 2970 | } |
| 2971 | |
| 2972 | |
| 2973 | ptime = pjsua_var.mconf_cfg.samples_per_frame * 1000 / |
| 2974 | pjsua_var.media_cfg.clock_rate; |
| 2975 | |
Benny Prijono | d5696da | 2007-07-17 16:25:45 +0000 | [diff] [blame] | 2976 | pool = pjsua_pool_create("playlist", 1000, 1000); |
| 2977 | if (!pool) { |
Benny Prijono | b90fd38 | 2011-09-18 14:59:56 +0000 | [diff] [blame] | 2978 | status = PJ_ENOMEM; |
| 2979 | goto on_error; |
Benny Prijono | d5696da | 2007-07-17 16:25:45 +0000 | [diff] [blame] | 2980 | } |
| 2981 | |
| 2982 | status = pjmedia_wav_playlist_create(pool, label, |
Benny Prijono | a66c331 | 2007-01-21 23:12:40 +0000 | [diff] [blame] | 2983 | file_names, file_count, |
| 2984 | ptime, options, 0, &port); |
| 2985 | if (status != PJ_SUCCESS) { |
Benny Prijono | a66c331 | 2007-01-21 23:12:40 +0000 | [diff] [blame] | 2986 | pjsua_perror(THIS_FILE, "Unable to create playlist", status); |
Benny Prijono | b90fd38 | 2011-09-18 14:59:56 +0000 | [diff] [blame] | 2987 | goto on_error; |
Benny Prijono | a66c331 | 2007-01-21 23:12:40 +0000 | [diff] [blame] | 2988 | } |
| 2989 | |
Benny Prijono | d5696da | 2007-07-17 16:25:45 +0000 | [diff] [blame] | 2990 | status = pjmedia_conf_add_port(pjsua_var.mconf, pool, |
Benny Prijono | a66c331 | 2007-01-21 23:12:40 +0000 | [diff] [blame] | 2991 | port, &port->info.name, &slot); |
| 2992 | if (status != PJ_SUCCESS) { |
| 2993 | pjmedia_port_destroy(port); |
Benny Prijono | a66c331 | 2007-01-21 23:12:40 +0000 | [diff] [blame] | 2994 | pjsua_perror(THIS_FILE, "Unable to add port", status); |
Benny Prijono | b90fd38 | 2011-09-18 14:59:56 +0000 | [diff] [blame] | 2995 | goto on_error; |
Benny Prijono | a66c331 | 2007-01-21 23:12:40 +0000 | [diff] [blame] | 2996 | } |
| 2997 | |
| 2998 | pjsua_var.player[file_id].type = 1; |
Benny Prijono | d5696da | 2007-07-17 16:25:45 +0000 | [diff] [blame] | 2999 | pjsua_var.player[file_id].pool = pool; |
Benny Prijono | a66c331 | 2007-01-21 23:12:40 +0000 | [diff] [blame] | 3000 | pjsua_var.player[file_id].port = port; |
| 3001 | pjsua_var.player[file_id].slot = slot; |
| 3002 | |
| 3003 | if (p_id) *p_id = file_id; |
| 3004 | |
| 3005 | ++pjsua_var.player_cnt; |
| 3006 | |
| 3007 | PJSUA_UNLOCK(); |
Benny Prijono | b90fd38 | 2011-09-18 14:59:56 +0000 | [diff] [blame] | 3008 | |
| 3009 | PJ_LOG(4,(THIS_FILE, "Playlist created, id=%d, slot=%d", file_id, slot)); |
| 3010 | |
| 3011 | pj_log_pop_indent(); |
| 3012 | |
Benny Prijono | a66c331 | 2007-01-21 23:12:40 +0000 | [diff] [blame] | 3013 | return PJ_SUCCESS; |
| 3014 | |
Benny Prijono | b90fd38 | 2011-09-18 14:59:56 +0000 | [diff] [blame] | 3015 | on_error: |
| 3016 | PJSUA_UNLOCK(); |
| 3017 | if (pool) pj_pool_release(pool); |
| 3018 | pj_log_pop_indent(); |
| 3019 | |
| 3020 | return status; |
Benny Prijono | a66c331 | 2007-01-21 23:12:40 +0000 | [diff] [blame] | 3021 | } |
| 3022 | |
| 3023 | |
| 3024 | /* |
Benny Prijono | eebe9af | 2006-06-13 22:57:13 +0000 | [diff] [blame] | 3025 | * Get conference port ID associated with player. |
| 3026 | */ |
| 3027 | PJ_DEF(pjsua_conf_port_id) pjsua_player_get_conf_port(pjsua_player_id id) |
| 3028 | { |
Benny Prijono | a1e6968 | 2007-05-11 15:14:34 +0000 | [diff] [blame] | 3029 | 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] | 3030 | PJ_ASSERT_RETURN(pjsua_var.player[id].port != NULL, PJ_EINVAL); |
| 3031 | |
| 3032 | return pjsua_var.player[id].slot; |
| 3033 | } |
| 3034 | |
Benny Prijono | 469b152 | 2006-12-26 03:05:17 +0000 | [diff] [blame] | 3035 | /* |
| 3036 | * Get the media port for the player. |
| 3037 | */ |
Benny Prijono | be41d86 | 2008-01-18 13:24:28 +0000 | [diff] [blame] | 3038 | 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] | 3039 | pjmedia_port **p_port) |
| 3040 | { |
Benny Prijono | a1e6968 | 2007-05-11 15:14:34 +0000 | [diff] [blame] | 3041 | 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] | 3042 | PJ_ASSERT_RETURN(pjsua_var.player[id].port != NULL, PJ_EINVAL); |
| 3043 | PJ_ASSERT_RETURN(p_port != NULL, PJ_EINVAL); |
| 3044 | |
| 3045 | *p_port = pjsua_var.player[id].port; |
| 3046 | |
| 3047 | return PJ_SUCCESS; |
| 3048 | } |
Benny Prijono | eebe9af | 2006-06-13 22:57:13 +0000 | [diff] [blame] | 3049 | |
| 3050 | /* |
| 3051 | * Set playback position. |
| 3052 | */ |
| 3053 | PJ_DEF(pj_status_t) pjsua_player_set_pos( pjsua_player_id id, |
| 3054 | pj_uint32_t samples) |
| 3055 | { |
Benny Prijono | a1e6968 | 2007-05-11 15:14:34 +0000 | [diff] [blame] | 3056 | 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] | 3057 | PJ_ASSERT_RETURN(pjsua_var.player[id].port != NULL, PJ_EINVAL); |
Benny Prijono | a66c331 | 2007-01-21 23:12:40 +0000 | [diff] [blame] | 3058 | PJ_ASSERT_RETURN(pjsua_var.player[id].type == 0, PJ_EINVAL); |
Benny Prijono | eebe9af | 2006-06-13 22:57:13 +0000 | [diff] [blame] | 3059 | |
| 3060 | return pjmedia_wav_player_port_set_pos(pjsua_var.player[id].port, samples); |
| 3061 | } |
| 3062 | |
| 3063 | |
| 3064 | /* |
| 3065 | * Close the file, remove the player from the bridge, and free |
| 3066 | * resources associated with the file player. |
| 3067 | */ |
| 3068 | PJ_DEF(pj_status_t) pjsua_player_destroy(pjsua_player_id id) |
| 3069 | { |
Benny Prijono | a1e6968 | 2007-05-11 15:14:34 +0000 | [diff] [blame] | 3070 | 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] | 3071 | PJ_ASSERT_RETURN(pjsua_var.player[id].port != NULL, PJ_EINVAL); |
| 3072 | |
Benny Prijono | b90fd38 | 2011-09-18 14:59:56 +0000 | [diff] [blame] | 3073 | PJ_LOG(4,(THIS_FILE, "Destroying player %d..", id)); |
| 3074 | pj_log_push_indent(); |
| 3075 | |
Benny Prijono | eebe9af | 2006-06-13 22:57:13 +0000 | [diff] [blame] | 3076 | PJSUA_LOCK(); |
| 3077 | |
| 3078 | if (pjsua_var.player[id].port) { |
Nanang Izzuddin | 148fd39 | 2008-06-16 09:52:50 +0000 | [diff] [blame] | 3079 | pjsua_conf_remove_port(pjsua_var.player[id].slot); |
Benny Prijono | eebe9af | 2006-06-13 22:57:13 +0000 | [diff] [blame] | 3080 | pjmedia_port_destroy(pjsua_var.player[id].port); |
| 3081 | pjsua_var.player[id].port = NULL; |
| 3082 | pjsua_var.player[id].slot = 0xFFFF; |
Benny Prijono | d5696da | 2007-07-17 16:25:45 +0000 | [diff] [blame] | 3083 | pj_pool_release(pjsua_var.player[id].pool); |
| 3084 | pjsua_var.player[id].pool = NULL; |
Benny Prijono | eebe9af | 2006-06-13 22:57:13 +0000 | [diff] [blame] | 3085 | pjsua_var.player_cnt--; |
| 3086 | } |
| 3087 | |
| 3088 | PJSUA_UNLOCK(); |
Benny Prijono | b90fd38 | 2011-09-18 14:59:56 +0000 | [diff] [blame] | 3089 | pj_log_pop_indent(); |
Benny Prijono | eebe9af | 2006-06-13 22:57:13 +0000 | [diff] [blame] | 3090 | |
| 3091 | return PJ_SUCCESS; |
| 3092 | } |
| 3093 | |
| 3094 | |
| 3095 | /***************************************************************************** |
| 3096 | * File recorder. |
| 3097 | */ |
| 3098 | |
| 3099 | /* |
| 3100 | * Create a file recorder, and automatically connect this recorder to |
| 3101 | * the conference bridge. |
| 3102 | */ |
| 3103 | 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] | 3104 | unsigned enc_type, |
| 3105 | void *enc_param, |
Benny Prijono | eebe9af | 2006-06-13 22:57:13 +0000 | [diff] [blame] | 3106 | pj_ssize_t max_size, |
| 3107 | unsigned options, |
Benny Prijono | eebe9af | 2006-06-13 22:57:13 +0000 | [diff] [blame] | 3108 | pjsua_recorder_id *p_id) |
| 3109 | { |
Benny Prijono | b3cdb2b | 2006-10-19 15:49:47 +0000 | [diff] [blame] | 3110 | enum Format |
| 3111 | { |
| 3112 | FMT_UNKNOWN, |
| 3113 | FMT_WAV, |
| 3114 | FMT_MP3, |
| 3115 | }; |
Benny Prijono | eebe9af | 2006-06-13 22:57:13 +0000 | [diff] [blame] | 3116 | unsigned slot, file_id; |
Benny Prijono | a66c331 | 2007-01-21 23:12:40 +0000 | [diff] [blame] | 3117 | char path[PJ_MAXPATH]; |
Benny Prijono | b3cdb2b | 2006-10-19 15:49:47 +0000 | [diff] [blame] | 3118 | pj_str_t ext; |
Benny Prijono | 8f31052 | 2006-10-20 11:08:49 +0000 | [diff] [blame] | 3119 | int file_format; |
Benny Prijono | b90fd38 | 2011-09-18 14:59:56 +0000 | [diff] [blame] | 3120 | pj_pool_t *pool = NULL; |
Benny Prijono | eebe9af | 2006-06-13 22:57:13 +0000 | [diff] [blame] | 3121 | pjmedia_port *port; |
Benny Prijono | b90fd38 | 2011-09-18 14:59:56 +0000 | [diff] [blame] | 3122 | pj_status_t status = PJ_SUCCESS; |
Benny Prijono | eebe9af | 2006-06-13 22:57:13 +0000 | [diff] [blame] | 3123 | |
Benny Prijono | b3cdb2b | 2006-10-19 15:49:47 +0000 | [diff] [blame] | 3124 | /* Filename must present */ |
| 3125 | PJ_ASSERT_RETURN(filename != NULL, PJ_EINVAL); |
| 3126 | |
Benny Prijono | 00cae61 | 2006-07-31 15:19:36 +0000 | [diff] [blame] | 3127 | /* Don't support max_size at present */ |
Benny Prijono | b3cdb2b | 2006-10-19 15:49:47 +0000 | [diff] [blame] | 3128 | PJ_ASSERT_RETURN(max_size == 0 || max_size == -1, PJ_EINVAL); |
Benny Prijono | 00cae61 | 2006-07-31 15:19:36 +0000 | [diff] [blame] | 3129 | |
Benny Prijono | 8f31052 | 2006-10-20 11:08:49 +0000 | [diff] [blame] | 3130 | /* Don't support encoding type at present */ |
| 3131 | PJ_ASSERT_RETURN(enc_type == 0, PJ_EINVAL); |
Benny Prijono | 00cae61 | 2006-07-31 15:19:36 +0000 | [diff] [blame] | 3132 | |
Benny Prijono | b90fd38 | 2011-09-18 14:59:56 +0000 | [diff] [blame] | 3133 | PJ_LOG(4,(THIS_FILE, "Creating recorder %.*s..", |
| 3134 | (int)filename->slen, filename->ptr)); |
| 3135 | pj_log_push_indent(); |
| 3136 | |
| 3137 | if (pjsua_var.rec_cnt >= PJ_ARRAY_SIZE(pjsua_var.recorder)) { |
| 3138 | pj_log_pop_indent(); |
Benny Prijono | eebe9af | 2006-06-13 22:57:13 +0000 | [diff] [blame] | 3139 | return PJ_ETOOMANY; |
Benny Prijono | b90fd38 | 2011-09-18 14:59:56 +0000 | [diff] [blame] | 3140 | } |
Benny Prijono | eebe9af | 2006-06-13 22:57:13 +0000 | [diff] [blame] | 3141 | |
Benny Prijono | b3cdb2b | 2006-10-19 15:49:47 +0000 | [diff] [blame] | 3142 | /* Determine the file format */ |
| 3143 | ext.ptr = filename->ptr + filename->slen - 4; |
| 3144 | ext.slen = 4; |
| 3145 | |
| 3146 | if (pj_stricmp2(&ext, ".wav") == 0) |
| 3147 | file_format = FMT_WAV; |
| 3148 | else if (pj_stricmp2(&ext, ".mp3") == 0) |
| 3149 | file_format = FMT_MP3; |
| 3150 | else { |
| 3151 | PJ_LOG(1,(THIS_FILE, "pjsua_recorder_create() error: unable to " |
| 3152 | "determine file format for %.*s", |
| 3153 | (int)filename->slen, filename->ptr)); |
Benny Prijono | b90fd38 | 2011-09-18 14:59:56 +0000 | [diff] [blame] | 3154 | pj_log_pop_indent(); |
Benny Prijono | b3cdb2b | 2006-10-19 15:49:47 +0000 | [diff] [blame] | 3155 | return PJ_ENOTSUP; |
| 3156 | } |
| 3157 | |
Benny Prijono | eebe9af | 2006-06-13 22:57:13 +0000 | [diff] [blame] | 3158 | PJSUA_LOCK(); |
| 3159 | |
| 3160 | for (file_id=0; file_id<PJ_ARRAY_SIZE(pjsua_var.recorder); ++file_id) { |
| 3161 | if (pjsua_var.recorder[file_id].port == NULL) |
| 3162 | break; |
| 3163 | } |
| 3164 | |
| 3165 | if (file_id == PJ_ARRAY_SIZE(pjsua_var.recorder)) { |
| 3166 | /* This is unexpected */ |
Benny Prijono | eebe9af | 2006-06-13 22:57:13 +0000 | [diff] [blame] | 3167 | pj_assert(0); |
Benny Prijono | b90fd38 | 2011-09-18 14:59:56 +0000 | [diff] [blame] | 3168 | status = PJ_EBUG; |
| 3169 | goto on_return; |
Benny Prijono | eebe9af | 2006-06-13 22:57:13 +0000 | [diff] [blame] | 3170 | } |
| 3171 | |
| 3172 | pj_memcpy(path, filename->ptr, filename->slen); |
| 3173 | path[filename->slen] = '\0'; |
Benny Prijono | b3cdb2b | 2006-10-19 15:49:47 +0000 | [diff] [blame] | 3174 | |
Benny Prijono | d5696da | 2007-07-17 16:25:45 +0000 | [diff] [blame] | 3175 | pool = pjsua_pool_create(get_basename(path, filename->slen), 1000, 1000); |
| 3176 | if (!pool) { |
Benny Prijono | b90fd38 | 2011-09-18 14:59:56 +0000 | [diff] [blame] | 3177 | status = PJ_ENOMEM; |
| 3178 | goto on_return; |
Benny Prijono | d5696da | 2007-07-17 16:25:45 +0000 | [diff] [blame] | 3179 | } |
| 3180 | |
Benny Prijono | b3cdb2b | 2006-10-19 15:49:47 +0000 | [diff] [blame] | 3181 | if (file_format == FMT_WAV) { |
Benny Prijono | d5696da | 2007-07-17 16:25:45 +0000 | [diff] [blame] | 3182 | status = pjmedia_wav_writer_port_create(pool, path, |
Benny Prijono | b3cdb2b | 2006-10-19 15:49:47 +0000 | [diff] [blame] | 3183 | pjsua_var.media_cfg.clock_rate, |
| 3184 | pjsua_var.mconf_cfg.channel_count, |
| 3185 | pjsua_var.mconf_cfg.samples_per_frame, |
| 3186 | pjsua_var.mconf_cfg.bits_per_sample, |
| 3187 | options, 0, &port); |
Benny Prijono | b3cdb2b | 2006-10-19 15:49:47 +0000 | [diff] [blame] | 3188 | } else { |
Benny Prijono | c95a0f0 | 2007-04-09 07:06:08 +0000 | [diff] [blame] | 3189 | PJ_UNUSED_ARG(enc_param); |
Benny Prijono | b3cdb2b | 2006-10-19 15:49:47 +0000 | [diff] [blame] | 3190 | port = NULL; |
| 3191 | status = PJ_ENOTSUP; |
| 3192 | } |
| 3193 | |
Benny Prijono | eebe9af | 2006-06-13 22:57:13 +0000 | [diff] [blame] | 3194 | if (status != PJ_SUCCESS) { |
Benny Prijono | eebe9af | 2006-06-13 22:57:13 +0000 | [diff] [blame] | 3195 | pjsua_perror(THIS_FILE, "Unable to open file for recording", status); |
Benny Prijono | b90fd38 | 2011-09-18 14:59:56 +0000 | [diff] [blame] | 3196 | goto on_return; |
Benny Prijono | eebe9af | 2006-06-13 22:57:13 +0000 | [diff] [blame] | 3197 | } |
| 3198 | |
Benny Prijono | d5696da | 2007-07-17 16:25:45 +0000 | [diff] [blame] | 3199 | status = pjmedia_conf_add_port(pjsua_var.mconf, pool, |
Benny Prijono | eebe9af | 2006-06-13 22:57:13 +0000 | [diff] [blame] | 3200 | port, filename, &slot); |
| 3201 | if (status != PJ_SUCCESS) { |
| 3202 | pjmedia_port_destroy(port); |
Benny Prijono | b90fd38 | 2011-09-18 14:59:56 +0000 | [diff] [blame] | 3203 | goto on_return; |
Benny Prijono | eebe9af | 2006-06-13 22:57:13 +0000 | [diff] [blame] | 3204 | } |
| 3205 | |
| 3206 | pjsua_var.recorder[file_id].port = port; |
| 3207 | pjsua_var.recorder[file_id].slot = slot; |
Benny Prijono | d5696da | 2007-07-17 16:25:45 +0000 | [diff] [blame] | 3208 | pjsua_var.recorder[file_id].pool = pool; |
Benny Prijono | eebe9af | 2006-06-13 22:57:13 +0000 | [diff] [blame] | 3209 | |
| 3210 | if (p_id) *p_id = file_id; |
| 3211 | |
| 3212 | ++pjsua_var.rec_cnt; |
| 3213 | |
| 3214 | PJSUA_UNLOCK(); |
Benny Prijono | b90fd38 | 2011-09-18 14:59:56 +0000 | [diff] [blame] | 3215 | |
| 3216 | PJ_LOG(4,(THIS_FILE, "Recorder created, id=%d, slot=%d", file_id, slot)); |
| 3217 | |
| 3218 | pj_log_pop_indent(); |
Benny Prijono | eebe9af | 2006-06-13 22:57:13 +0000 | [diff] [blame] | 3219 | return PJ_SUCCESS; |
Benny Prijono | b90fd38 | 2011-09-18 14:59:56 +0000 | [diff] [blame] | 3220 | |
| 3221 | on_return: |
| 3222 | PJSUA_UNLOCK(); |
| 3223 | if (pool) pj_pool_release(pool); |
| 3224 | pj_log_pop_indent(); |
| 3225 | return status; |
Benny Prijono | eebe9af | 2006-06-13 22:57:13 +0000 | [diff] [blame] | 3226 | } |
| 3227 | |
| 3228 | |
| 3229 | /* |
| 3230 | * Get conference port associated with recorder. |
| 3231 | */ |
| 3232 | PJ_DEF(pjsua_conf_port_id) pjsua_recorder_get_conf_port(pjsua_recorder_id id) |
| 3233 | { |
Benny Prijono | a1e6968 | 2007-05-11 15:14:34 +0000 | [diff] [blame] | 3234 | PJ_ASSERT_RETURN(id>=0 && id<(int)PJ_ARRAY_SIZE(pjsua_var.recorder), |
| 3235 | PJ_EINVAL); |
Benny Prijono | eebe9af | 2006-06-13 22:57:13 +0000 | [diff] [blame] | 3236 | PJ_ASSERT_RETURN(pjsua_var.recorder[id].port != NULL, PJ_EINVAL); |
| 3237 | |
| 3238 | return pjsua_var.recorder[id].slot; |
| 3239 | } |
| 3240 | |
Benny Prijono | 469b152 | 2006-12-26 03:05:17 +0000 | [diff] [blame] | 3241 | /* |
| 3242 | * Get the media port for the recorder. |
| 3243 | */ |
| 3244 | PJ_DEF(pj_status_t) pjsua_recorder_get_port( pjsua_recorder_id id, |
| 3245 | pjmedia_port **p_port) |
| 3246 | { |
Benny Prijono | a1e6968 | 2007-05-11 15:14:34 +0000 | [diff] [blame] | 3247 | PJ_ASSERT_RETURN(id>=0 && id<(int)PJ_ARRAY_SIZE(pjsua_var.recorder), |
| 3248 | PJ_EINVAL); |
Benny Prijono | 469b152 | 2006-12-26 03:05:17 +0000 | [diff] [blame] | 3249 | PJ_ASSERT_RETURN(pjsua_var.recorder[id].port != NULL, PJ_EINVAL); |
| 3250 | PJ_ASSERT_RETURN(p_port != NULL, PJ_EINVAL); |
| 3251 | |
| 3252 | *p_port = pjsua_var.recorder[id].port; |
| 3253 | return PJ_SUCCESS; |
| 3254 | } |
Benny Prijono | eebe9af | 2006-06-13 22:57:13 +0000 | [diff] [blame] | 3255 | |
| 3256 | /* |
| 3257 | * Destroy recorder (this will complete recording). |
| 3258 | */ |
| 3259 | PJ_DEF(pj_status_t) pjsua_recorder_destroy(pjsua_recorder_id id) |
| 3260 | { |
Benny Prijono | a1e6968 | 2007-05-11 15:14:34 +0000 | [diff] [blame] | 3261 | PJ_ASSERT_RETURN(id>=0 && id<(int)PJ_ARRAY_SIZE(pjsua_var.recorder), |
| 3262 | PJ_EINVAL); |
Benny Prijono | eebe9af | 2006-06-13 22:57:13 +0000 | [diff] [blame] | 3263 | PJ_ASSERT_RETURN(pjsua_var.recorder[id].port != NULL, PJ_EINVAL); |
| 3264 | |
Benny Prijono | b90fd38 | 2011-09-18 14:59:56 +0000 | [diff] [blame] | 3265 | PJ_LOG(4,(THIS_FILE, "Destroying recorder %d..", id)); |
| 3266 | pj_log_push_indent(); |
| 3267 | |
Benny Prijono | eebe9af | 2006-06-13 22:57:13 +0000 | [diff] [blame] | 3268 | PJSUA_LOCK(); |
| 3269 | |
| 3270 | if (pjsua_var.recorder[id].port) { |
Nanang Izzuddin | 148fd39 | 2008-06-16 09:52:50 +0000 | [diff] [blame] | 3271 | pjsua_conf_remove_port(pjsua_var.recorder[id].slot); |
Benny Prijono | eebe9af | 2006-06-13 22:57:13 +0000 | [diff] [blame] | 3272 | pjmedia_port_destroy(pjsua_var.recorder[id].port); |
| 3273 | pjsua_var.recorder[id].port = NULL; |
| 3274 | pjsua_var.recorder[id].slot = 0xFFFF; |
Benny Prijono | d5696da | 2007-07-17 16:25:45 +0000 | [diff] [blame] | 3275 | pj_pool_release(pjsua_var.recorder[id].pool); |
| 3276 | pjsua_var.recorder[id].pool = NULL; |
Benny Prijono | eebe9af | 2006-06-13 22:57:13 +0000 | [diff] [blame] | 3277 | pjsua_var.rec_cnt--; |
| 3278 | } |
| 3279 | |
| 3280 | PJSUA_UNLOCK(); |
Benny Prijono | b90fd38 | 2011-09-18 14:59:56 +0000 | [diff] [blame] | 3281 | pj_log_pop_indent(); |
Benny Prijono | eebe9af | 2006-06-13 22:57:13 +0000 | [diff] [blame] | 3282 | |
| 3283 | return PJ_SUCCESS; |
| 3284 | } |
| 3285 | |
| 3286 | |
| 3287 | /***************************************************************************** |
| 3288 | * Sound devices. |
| 3289 | */ |
| 3290 | |
| 3291 | /* |
| 3292 | * Enum sound devices. |
| 3293 | */ |
Benny Prijono | f798e50 | 2009-03-09 13:08:16 +0000 | [diff] [blame] | 3294 | |
| 3295 | 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] | 3296 | unsigned *count) |
| 3297 | { |
| 3298 | unsigned i, dev_count; |
| 3299 | |
Benny Prijono | 10454dc | 2009-02-21 14:21:59 +0000 | [diff] [blame] | 3300 | dev_count = pjmedia_aud_dev_count(); |
Benny Prijono | eebe9af | 2006-06-13 22:57:13 +0000 | [diff] [blame] | 3301 | |
| 3302 | if (dev_count > *count) dev_count = *count; |
| 3303 | |
| 3304 | for (i=0; i<dev_count; ++i) { |
Benny Prijono | 10454dc | 2009-02-21 14:21:59 +0000 | [diff] [blame] | 3305 | pj_status_t status; |
Benny Prijono | eebe9af | 2006-06-13 22:57:13 +0000 | [diff] [blame] | 3306 | |
Benny Prijono | 10454dc | 2009-02-21 14:21:59 +0000 | [diff] [blame] | 3307 | status = pjmedia_aud_dev_get_info(i, &info[i]); |
| 3308 | if (status != PJ_SUCCESS) |
| 3309 | return status; |
Benny Prijono | eebe9af | 2006-06-13 22:57:13 +0000 | [diff] [blame] | 3310 | } |
| 3311 | |
| 3312 | *count = dev_count; |
| 3313 | |
| 3314 | return PJ_SUCCESS; |
| 3315 | } |
Benny Prijono | f798e50 | 2009-03-09 13:08:16 +0000 | [diff] [blame] | 3316 | |
| 3317 | |
Benny Prijono | 10454dc | 2009-02-21 14:21:59 +0000 | [diff] [blame] | 3318 | PJ_DEF(pj_status_t) pjsua_enum_snd_devs( pjmedia_snd_dev_info info[], |
| 3319 | unsigned *count) |
| 3320 | { |
| 3321 | unsigned i, dev_count; |
| 3322 | |
| 3323 | dev_count = pjmedia_aud_dev_count(); |
| 3324 | |
| 3325 | if (dev_count > *count) dev_count = *count; |
| 3326 | pj_bzero(info, dev_count * sizeof(pjmedia_snd_dev_info)); |
| 3327 | |
| 3328 | for (i=0; i<dev_count; ++i) { |
| 3329 | pjmedia_aud_dev_info ai; |
| 3330 | pj_status_t status; |
| 3331 | |
| 3332 | status = pjmedia_aud_dev_get_info(i, &ai); |
| 3333 | if (status != PJ_SUCCESS) |
| 3334 | return status; |
| 3335 | |
| 3336 | strncpy(info[i].name, ai.name, sizeof(info[i].name)); |
| 3337 | info[i].name[sizeof(info[i].name)-1] = '\0'; |
| 3338 | info[i].input_count = ai.input_count; |
| 3339 | info[i].output_count = ai.output_count; |
| 3340 | info[i].default_samples_per_sec = ai.default_samples_per_sec; |
| 3341 | } |
| 3342 | |
| 3343 | *count = dev_count; |
| 3344 | |
| 3345 | return PJ_SUCCESS; |
| 3346 | } |
Benny Prijono | 10454dc | 2009-02-21 14:21:59 +0000 | [diff] [blame] | 3347 | |
Benny Prijono | f798e50 | 2009-03-09 13:08:16 +0000 | [diff] [blame] | 3348 | /* Create audio device parameter to open the device */ |
| 3349 | static pj_status_t create_aud_param(pjmedia_aud_param *param, |
| 3350 | pjmedia_aud_dev_index capture_dev, |
| 3351 | pjmedia_aud_dev_index playback_dev, |
| 3352 | unsigned clock_rate, |
| 3353 | unsigned channel_count, |
| 3354 | unsigned samples_per_frame, |
| 3355 | unsigned bits_per_sample) |
Benny Prijono | eebe9af | 2006-06-13 22:57:13 +0000 | [diff] [blame] | 3356 | { |
Nanang Izzuddin | 8465c68 | 2009-03-04 17:23:25 +0000 | [diff] [blame] | 3357 | pj_status_t status; |
| 3358 | |
Benny Prijono | 96e74f3 | 2009-02-22 12:00:12 +0000 | [diff] [blame] | 3359 | /* Normalize device ID with new convention about default device ID */ |
| 3360 | if (playback_dev == PJMEDIA_AUD_DEFAULT_CAPTURE_DEV) |
| 3361 | playback_dev = PJMEDIA_AUD_DEFAULT_PLAYBACK_DEV; |
| 3362 | |
Benny Prijono | 10454dc | 2009-02-21 14:21:59 +0000 | [diff] [blame] | 3363 | /* Create default parameters for the device */ |
Benny Prijono | f798e50 | 2009-03-09 13:08:16 +0000 | [diff] [blame] | 3364 | status = pjmedia_aud_dev_default_param(capture_dev, param); |
Benny Prijono | 10454dc | 2009-02-21 14:21:59 +0000 | [diff] [blame] | 3365 | if (status != PJ_SUCCESS) { |
Benny Prijono | 96e74f3 | 2009-02-22 12:00:12 +0000 | [diff] [blame] | 3366 | pjsua_perror(THIS_FILE, "Error retrieving default audio " |
| 3367 | "device parameters", status); |
Benny Prijono | 10454dc | 2009-02-21 14:21:59 +0000 | [diff] [blame] | 3368 | return status; |
| 3369 | } |
Benny Prijono | f798e50 | 2009-03-09 13:08:16 +0000 | [diff] [blame] | 3370 | param->dir = PJMEDIA_DIR_CAPTURE_PLAYBACK; |
| 3371 | param->rec_id = capture_dev; |
| 3372 | param->play_id = playback_dev; |
| 3373 | param->clock_rate = clock_rate; |
| 3374 | param->channel_count = channel_count; |
| 3375 | param->samples_per_frame = samples_per_frame; |
| 3376 | param->bits_per_sample = bits_per_sample; |
| 3377 | |
| 3378 | /* Update the setting with user preference */ |
| 3379 | #define update_param(cap, field) \ |
| 3380 | if (pjsua_var.aud_param.flags & cap) { \ |
| 3381 | param->flags |= cap; \ |
| 3382 | param->field = pjsua_var.aud_param.field; \ |
| 3383 | } |
| 3384 | update_param( PJMEDIA_AUD_DEV_CAP_INPUT_VOLUME_SETTING, input_vol); |
| 3385 | update_param( PJMEDIA_AUD_DEV_CAP_OUTPUT_VOLUME_SETTING, output_vol); |
| 3386 | update_param( PJMEDIA_AUD_DEV_CAP_INPUT_ROUTE, input_route); |
| 3387 | update_param( PJMEDIA_AUD_DEV_CAP_OUTPUT_ROUTE, output_route); |
| 3388 | #undef update_param |
| 3389 | |
Benny Prijono | 10454dc | 2009-02-21 14:21:59 +0000 | [diff] [blame] | 3390 | /* Latency settings */ |
Benny Prijono | f798e50 | 2009-03-09 13:08:16 +0000 | [diff] [blame] | 3391 | param->flags |= (PJMEDIA_AUD_DEV_CAP_INPUT_LATENCY | |
| 3392 | PJMEDIA_AUD_DEV_CAP_OUTPUT_LATENCY); |
| 3393 | param->input_latency_ms = pjsua_var.media_cfg.snd_rec_latency; |
| 3394 | param->output_latency_ms = pjsua_var.media_cfg.snd_play_latency; |
| 3395 | |
Benny Prijono | 10454dc | 2009-02-21 14:21:59 +0000 | [diff] [blame] | 3396 | /* EC settings */ |
| 3397 | if (pjsua_var.media_cfg.ec_tail_len) { |
Benny Prijono | f798e50 | 2009-03-09 13:08:16 +0000 | [diff] [blame] | 3398 | param->flags |= (PJMEDIA_AUD_DEV_CAP_EC | PJMEDIA_AUD_DEV_CAP_EC_TAIL); |
| 3399 | param->ec_enabled = PJ_TRUE; |
| 3400 | param->ec_tail_ms = pjsua_var.media_cfg.ec_tail_len; |
Benny Prijono | 10454dc | 2009-02-21 14:21:59 +0000 | [diff] [blame] | 3401 | } else { |
Benny Prijono | f798e50 | 2009-03-09 13:08:16 +0000 | [diff] [blame] | 3402 | 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] | 3403 | } |
| 3404 | |
Benny Prijono | f798e50 | 2009-03-09 13:08:16 +0000 | [diff] [blame] | 3405 | return PJ_SUCCESS; |
| 3406 | } |
Benny Prijono | 26056d8 | 2006-10-11 16:03:41 +0000 | [diff] [blame] | 3407 | |
Benny Prijono | f798e50 | 2009-03-09 13:08:16 +0000 | [diff] [blame] | 3408 | /* Internal: the first time the audio device is opened (during app |
| 3409 | * startup), retrieve the audio settings such as volume level |
| 3410 | * so that aud_get_settings() will work. |
| 3411 | */ |
| 3412 | static pj_status_t update_initial_aud_param() |
| 3413 | { |
| 3414 | pjmedia_aud_stream *strm; |
| 3415 | pjmedia_aud_param param; |
| 3416 | pj_status_t status; |
Benny Prijono | 26056d8 | 2006-10-11 16:03:41 +0000 | [diff] [blame] | 3417 | |
Benny Prijono | f798e50 | 2009-03-09 13:08:16 +0000 | [diff] [blame] | 3418 | PJ_ASSERT_RETURN(pjsua_var.snd_port != NULL, PJ_EBUG); |
Nanang Izzuddin | 3c1ae63 | 2008-08-21 15:04:20 +0000 | [diff] [blame] | 3419 | |
Benny Prijono | f798e50 | 2009-03-09 13:08:16 +0000 | [diff] [blame] | 3420 | strm = pjmedia_snd_port_get_snd_stream(pjsua_var.snd_port); |
Benny Prijono | 26056d8 | 2006-10-11 16:03:41 +0000 | [diff] [blame] | 3421 | |
Benny Prijono | f798e50 | 2009-03-09 13:08:16 +0000 | [diff] [blame] | 3422 | status = pjmedia_aud_stream_get_param(strm, ¶m); |
Benny Prijono | eebe9af | 2006-06-13 22:57:13 +0000 | [diff] [blame] | 3423 | if (status != PJ_SUCCESS) { |
Benny Prijono | f798e50 | 2009-03-09 13:08:16 +0000 | [diff] [blame] | 3424 | pjsua_perror(THIS_FILE, "Error audio stream " |
| 3425 | "device parameters", status); |
Benny Prijono | eebe9af | 2006-06-13 22:57:13 +0000 | [diff] [blame] | 3426 | return status; |
| 3427 | } |
| 3428 | |
Benny Prijono | f798e50 | 2009-03-09 13:08:16 +0000 | [diff] [blame] | 3429 | #define update_saved_param(cap, field) \ |
| 3430 | if (param.flags & cap) { \ |
| 3431 | pjsua_var.aud_param.flags |= cap; \ |
| 3432 | pjsua_var.aud_param.field = param.field; \ |
| 3433 | } |
| 3434 | |
| 3435 | update_saved_param(PJMEDIA_AUD_DEV_CAP_INPUT_VOLUME_SETTING, input_vol); |
| 3436 | update_saved_param(PJMEDIA_AUD_DEV_CAP_OUTPUT_VOLUME_SETTING, output_vol); |
| 3437 | update_saved_param(PJMEDIA_AUD_DEV_CAP_INPUT_ROUTE, input_route); |
| 3438 | update_saved_param(PJMEDIA_AUD_DEV_CAP_OUTPUT_ROUTE, output_route); |
| 3439 | #undef update_saved_param |
| 3440 | |
| 3441 | return PJ_SUCCESS; |
| 3442 | } |
| 3443 | |
| 3444 | /* Get format name */ |
| 3445 | static const char *get_fmt_name(pj_uint32_t id) |
| 3446 | { |
| 3447 | static char name[8]; |
| 3448 | |
| 3449 | if (id == PJMEDIA_FORMAT_L16) |
| 3450 | return "PCM"; |
| 3451 | pj_memcpy(name, &id, 4); |
| 3452 | name[4] = '\0'; |
| 3453 | return name; |
| 3454 | } |
| 3455 | |
| 3456 | /* Open sound device with the setting. */ |
Sauw Ming | 98766c7 | 2011-03-11 06:57:24 +0000 | [diff] [blame] | 3457 | static pj_status_t open_snd_dev(pjmedia_snd_port_param *param) |
Benny Prijono | f798e50 | 2009-03-09 13:08:16 +0000 | [diff] [blame] | 3458 | { |
| 3459 | pjmedia_port *conf_port; |
| 3460 | pj_status_t status; |
| 3461 | |
| 3462 | PJ_ASSERT_RETURN(param, PJ_EINVAL); |
| 3463 | |
| 3464 | /* Check if NULL sound device is used */ |
Sauw Ming | 98766c7 | 2011-03-11 06:57:24 +0000 | [diff] [blame] | 3465 | if (NULL_SND_DEV_ID==param->base.rec_id || |
| 3466 | NULL_SND_DEV_ID==param->base.play_id) |
| 3467 | { |
Benny Prijono | f798e50 | 2009-03-09 13:08:16 +0000 | [diff] [blame] | 3468 | return pjsua_set_null_snd_dev(); |
| 3469 | } |
| 3470 | |
| 3471 | /* Close existing sound port */ |
| 3472 | close_snd_dev(); |
| 3473 | |
Benny Prijono | 2d64772 | 2011-07-13 03:05:22 +0000 | [diff] [blame] | 3474 | /* Notify app */ |
| 3475 | if (pjsua_var.ua_cfg.cb.on_snd_dev_operation) { |
| 3476 | (*pjsua_var.ua_cfg.cb.on_snd_dev_operation)(1); |
| 3477 | } |
| 3478 | |
Benny Prijono | f798e50 | 2009-03-09 13:08:16 +0000 | [diff] [blame] | 3479 | /* Create memory pool for sound device. */ |
| 3480 | pjsua_var.snd_pool = pjsua_pool_create("pjsua_snd", 4000, 4000); |
| 3481 | PJ_ASSERT_RETURN(pjsua_var.snd_pool, PJ_ENOMEM); |
| 3482 | |
| 3483 | |
| 3484 | PJ_LOG(4,(THIS_FILE, "Opening sound device %s@%d/%d/%dms", |
Sauw Ming | 98766c7 | 2011-03-11 06:57:24 +0000 | [diff] [blame] | 3485 | get_fmt_name(param->base.ext_fmt.id), |
| 3486 | param->base.clock_rate, param->base.channel_count, |
| 3487 | param->base.samples_per_frame / param->base.channel_count * |
| 3488 | 1000 / param->base.clock_rate)); |
Benny Prijono | b90fd38 | 2011-09-18 14:59:56 +0000 | [diff] [blame] | 3489 | pj_log_push_indent(); |
Benny Prijono | f798e50 | 2009-03-09 13:08:16 +0000 | [diff] [blame] | 3490 | |
| 3491 | status = pjmedia_snd_port_create2( pjsua_var.snd_pool, |
Sauw Ming | 98766c7 | 2011-03-11 06:57:24 +0000 | [diff] [blame] | 3492 | param, &pjsua_var.snd_port); |
Benny Prijono | f798e50 | 2009-03-09 13:08:16 +0000 | [diff] [blame] | 3493 | if (status != PJ_SUCCESS) |
Benny Prijono | b90fd38 | 2011-09-18 14:59:56 +0000 | [diff] [blame] | 3494 | goto on_error; |
Benny Prijono | f798e50 | 2009-03-09 13:08:16 +0000 | [diff] [blame] | 3495 | |
| 3496 | /* Get the port0 of the conference bridge. */ |
| 3497 | conf_port = pjmedia_conf_get_master_port(pjsua_var.mconf); |
| 3498 | pj_assert(conf_port != NULL); |
| 3499 | |
| 3500 | /* For conference bridge, resample if necessary if the bridge's |
| 3501 | * clock rate is different than the sound device's clock rate. |
| 3502 | */ |
| 3503 | if (!pjsua_var.is_mswitch && |
Sauw Ming | 98766c7 | 2011-03-11 06:57:24 +0000 | [diff] [blame] | 3504 | param->base.ext_fmt.id == PJMEDIA_FORMAT_PCM && |
Nanang Izzuddin | fe68f1d | 2011-07-19 03:42:28 +0000 | [diff] [blame] | 3505 | PJMEDIA_PIA_SRATE(&conf_port->info) != param->base.clock_rate) |
Benny Prijono | f798e50 | 2009-03-09 13:08:16 +0000 | [diff] [blame] | 3506 | { |
| 3507 | pjmedia_port *resample_port; |
| 3508 | unsigned resample_opt = 0; |
| 3509 | |
| 3510 | if (pjsua_var.media_cfg.quality >= 3 && |
| 3511 | pjsua_var.media_cfg.quality <= 4) |
| 3512 | { |
| 3513 | resample_opt |= PJMEDIA_RESAMPLE_USE_SMALL_FILTER; |
| 3514 | } |
| 3515 | else if (pjsua_var.media_cfg.quality < 3) { |
| 3516 | resample_opt |= PJMEDIA_RESAMPLE_USE_LINEAR; |
| 3517 | } |
| 3518 | |
| 3519 | status = pjmedia_resample_port_create(pjsua_var.snd_pool, |
| 3520 | conf_port, |
Sauw Ming | 98766c7 | 2011-03-11 06:57:24 +0000 | [diff] [blame] | 3521 | param->base.clock_rate, |
Benny Prijono | f798e50 | 2009-03-09 13:08:16 +0000 | [diff] [blame] | 3522 | resample_opt, |
| 3523 | &resample_port); |
| 3524 | if (status != PJ_SUCCESS) { |
| 3525 | char errmsg[PJ_ERR_MSG_SIZE]; |
| 3526 | pj_strerror(status, errmsg, sizeof(errmsg)); |
| 3527 | PJ_LOG(4, (THIS_FILE, |
| 3528 | "Error creating resample port: %s", |
| 3529 | errmsg)); |
| 3530 | close_snd_dev(); |
Benny Prijono | b90fd38 | 2011-09-18 14:59:56 +0000 | [diff] [blame] | 3531 | goto on_error; |
Benny Prijono | f798e50 | 2009-03-09 13:08:16 +0000 | [diff] [blame] | 3532 | } |
| 3533 | |
| 3534 | conf_port = resample_port; |
| 3535 | } |
| 3536 | |
| 3537 | /* Otherwise for audio switchboard, the switch's port0 setting is |
| 3538 | * derived from the sound device setting, so update the setting. |
| 3539 | */ |
| 3540 | if (pjsua_var.is_mswitch) { |
Nanang Izzuddin | fe68f1d | 2011-07-19 03:42:28 +0000 | [diff] [blame] | 3541 | pj_memcpy(&conf_port->info.fmt, ¶m->base.ext_fmt, |
Benny Prijono | c45d951 | 2010-12-10 11:04:30 +0000 | [diff] [blame] | 3542 | sizeof(conf_port->info.fmt)); |
Nanang Izzuddin | fe68f1d | 2011-07-19 03:42:28 +0000 | [diff] [blame] | 3543 | conf_port->info.fmt.det.aud.clock_rate = param->base.clock_rate; |
| 3544 | conf_port->info.fmt.det.aud.frame_time_usec = param->base.samples_per_frame* |
Benny Prijono | c45d951 | 2010-12-10 11:04:30 +0000 | [diff] [blame] | 3545 | 1000000 / |
Nanang Izzuddin | fe68f1d | 2011-07-19 03:42:28 +0000 | [diff] [blame] | 3546 | param->base.clock_rate; |
| 3547 | conf_port->info.fmt.det.aud.channel_count = param->base.channel_count; |
Benny Prijono | c45d951 | 2010-12-10 11:04:30 +0000 | [diff] [blame] | 3548 | conf_port->info.fmt.det.aud.bits_per_sample = 16; |
Benny Prijono | f798e50 | 2009-03-09 13:08:16 +0000 | [diff] [blame] | 3549 | } |
| 3550 | |
Benny Prijono | c45d951 | 2010-12-10 11:04:30 +0000 | [diff] [blame] | 3551 | |
Benny Prijono | f798e50 | 2009-03-09 13:08:16 +0000 | [diff] [blame] | 3552 | /* Connect sound port to the bridge */ |
Benny Prijono | 52a9391 | 2006-08-04 20:54:37 +0000 | [diff] [blame] | 3553 | status = pjmedia_snd_port_connect(pjsua_var.snd_port, |
| 3554 | conf_port ); |
| 3555 | if (status != PJ_SUCCESS) { |
| 3556 | pjsua_perror(THIS_FILE, "Unable to connect conference port to " |
| 3557 | "sound device", status); |
| 3558 | pjmedia_snd_port_destroy(pjsua_var.snd_port); |
| 3559 | pjsua_var.snd_port = NULL; |
Benny Prijono | b90fd38 | 2011-09-18 14:59:56 +0000 | [diff] [blame] | 3560 | goto on_error; |
Benny Prijono | 52a9391 | 2006-08-04 20:54:37 +0000 | [diff] [blame] | 3561 | } |
| 3562 | |
Benny Prijono | eebe9af | 2006-06-13 22:57:13 +0000 | [diff] [blame] | 3563 | /* Save the device IDs */ |
Sauw Ming | 98766c7 | 2011-03-11 06:57:24 +0000 | [diff] [blame] | 3564 | pjsua_var.cap_dev = param->base.rec_id; |
| 3565 | pjsua_var.play_dev = param->base.play_id; |
Benny Prijono | eebe9af | 2006-06-13 22:57:13 +0000 | [diff] [blame] | 3566 | |
Benny Prijono | c53c6d7 | 2006-11-27 09:54:03 +0000 | [diff] [blame] | 3567 | /* Update sound device name. */ |
Benny Prijono | f798e50 | 2009-03-09 13:08:16 +0000 | [diff] [blame] | 3568 | { |
| 3569 | pjmedia_aud_dev_info rec_info; |
| 3570 | pjmedia_aud_stream *strm; |
| 3571 | pjmedia_aud_param si; |
| 3572 | pj_str_t tmp; |
Benny Prijono | c53c6d7 | 2006-11-27 09:54:03 +0000 | [diff] [blame] | 3573 | |
Benny Prijono | f798e50 | 2009-03-09 13:08:16 +0000 | [diff] [blame] | 3574 | strm = pjmedia_snd_port_get_snd_stream(pjsua_var.snd_port); |
| 3575 | status = pjmedia_aud_stream_get_param(strm, &si); |
| 3576 | if (status == PJ_SUCCESS) |
| 3577 | status = pjmedia_aud_dev_get_info(si.rec_id, &rec_info); |
Benny Prijono | f3758ee | 2008-02-26 15:32:16 +0000 | [diff] [blame] | 3578 | |
Benny Prijono | f798e50 | 2009-03-09 13:08:16 +0000 | [diff] [blame] | 3579 | if (status==PJ_SUCCESS) { |
Sauw Ming | 98766c7 | 2011-03-11 06:57:24 +0000 | [diff] [blame] | 3580 | if (param->base.clock_rate != pjsua_var.media_cfg.clock_rate) { |
Benny Prijono | f798e50 | 2009-03-09 13:08:16 +0000 | [diff] [blame] | 3581 | char tmp_buf[128]; |
| 3582 | int tmp_buf_len = sizeof(tmp_buf); |
| 3583 | |
| 3584 | tmp_buf_len = pj_ansi_snprintf(tmp_buf, sizeof(tmp_buf)-1, |
| 3585 | "%s (%dKHz)", |
| 3586 | rec_info.name, |
Sauw Ming | 98766c7 | 2011-03-11 06:57:24 +0000 | [diff] [blame] | 3587 | param->base.clock_rate/1000); |
Benny Prijono | f798e50 | 2009-03-09 13:08:16 +0000 | [diff] [blame] | 3588 | pj_strset(&tmp, tmp_buf, tmp_buf_len); |
| 3589 | pjmedia_conf_set_port0_name(pjsua_var.mconf, &tmp); |
| 3590 | } else { |
| 3591 | pjmedia_conf_set_port0_name(pjsua_var.mconf, |
| 3592 | pj_cstr(&tmp, rec_info.name)); |
| 3593 | } |
| 3594 | } |
| 3595 | |
| 3596 | /* Any error is not major, let it through */ |
| 3597 | status = PJ_SUCCESS; |
Benny Prijono | b90fd38 | 2011-09-18 14:59:56 +0000 | [diff] [blame] | 3598 | } |
Benny Prijono | f798e50 | 2009-03-09 13:08:16 +0000 | [diff] [blame] | 3599 | |
| 3600 | /* If this is the first time the audio device is open, retrieve some |
| 3601 | * settings from the device (such as volume settings) so that the |
| 3602 | * pjsua_snd_get_setting() work. |
| 3603 | */ |
| 3604 | if (pjsua_var.aud_open_cnt == 0) { |
| 3605 | update_initial_aud_param(); |
| 3606 | ++pjsua_var.aud_open_cnt; |
Benny Prijono | f3758ee | 2008-02-26 15:32:16 +0000 | [diff] [blame] | 3607 | } |
Benny Prijono | c53c6d7 | 2006-11-27 09:54:03 +0000 | [diff] [blame] | 3608 | |
Benny Prijono | b90fd38 | 2011-09-18 14:59:56 +0000 | [diff] [blame] | 3609 | pj_log_pop_indent(); |
Benny Prijono | eebe9af | 2006-06-13 22:57:13 +0000 | [diff] [blame] | 3610 | return PJ_SUCCESS; |
Benny Prijono | b90fd38 | 2011-09-18 14:59:56 +0000 | [diff] [blame] | 3611 | |
| 3612 | on_error: |
| 3613 | pj_log_pop_indent(); |
| 3614 | return status; |
Benny Prijono | f798e50 | 2009-03-09 13:08:16 +0000 | [diff] [blame] | 3615 | } |
Nanang Izzuddin | 8465c68 | 2009-03-04 17:23:25 +0000 | [diff] [blame] | 3616 | |
Nanang Izzuddin | 8465c68 | 2009-03-04 17:23:25 +0000 | [diff] [blame] | 3617 | |
Benny Prijono | f798e50 | 2009-03-09 13:08:16 +0000 | [diff] [blame] | 3618 | /* Close existing sound device */ |
| 3619 | static void close_snd_dev(void) |
| 3620 | { |
Benny Prijono | b90fd38 | 2011-09-18 14:59:56 +0000 | [diff] [blame] | 3621 | pj_log_push_indent(); |
| 3622 | |
Benny Prijono | 2d64772 | 2011-07-13 03:05:22 +0000 | [diff] [blame] | 3623 | /* Notify app */ |
| 3624 | if (pjsua_var.snd_is_on && pjsua_var.ua_cfg.cb.on_snd_dev_operation) { |
| 3625 | (*pjsua_var.ua_cfg.cb.on_snd_dev_operation)(0); |
| 3626 | } |
| 3627 | |
Benny Prijono | f798e50 | 2009-03-09 13:08:16 +0000 | [diff] [blame] | 3628 | /* Close sound device */ |
| 3629 | if (pjsua_var.snd_port) { |
| 3630 | pjmedia_aud_dev_info cap_info, play_info; |
| 3631 | pjmedia_aud_stream *strm; |
| 3632 | pjmedia_aud_param param; |
| 3633 | |
| 3634 | strm = pjmedia_snd_port_get_snd_stream(pjsua_var.snd_port); |
| 3635 | pjmedia_aud_stream_get_param(strm, ¶m); |
| 3636 | |
| 3637 | if (pjmedia_aud_dev_get_info(param.rec_id, &cap_info) != PJ_SUCCESS) |
| 3638 | cap_info.name[0] = '\0'; |
| 3639 | if (pjmedia_aud_dev_get_info(param.play_id, &play_info) != PJ_SUCCESS) |
| 3640 | play_info.name[0] = '\0'; |
| 3641 | |
| 3642 | PJ_LOG(4,(THIS_FILE, "Closing %s sound playback device and " |
| 3643 | "%s sound capture device", |
| 3644 | play_info.name, cap_info.name)); |
| 3645 | |
| 3646 | pjmedia_snd_port_disconnect(pjsua_var.snd_port); |
| 3647 | pjmedia_snd_port_destroy(pjsua_var.snd_port); |
| 3648 | pjsua_var.snd_port = NULL; |
| 3649 | } |
| 3650 | |
| 3651 | /* Close null sound device */ |
| 3652 | if (pjsua_var.null_snd) { |
| 3653 | PJ_LOG(4,(THIS_FILE, "Closing null sound device..")); |
| 3654 | pjmedia_master_port_destroy(pjsua_var.null_snd, PJ_FALSE); |
| 3655 | pjsua_var.null_snd = NULL; |
| 3656 | } |
| 3657 | |
| 3658 | if (pjsua_var.snd_pool) |
| 3659 | pj_pool_release(pjsua_var.snd_pool); |
| 3660 | pjsua_var.snd_pool = NULL; |
Benny Prijono | 2d64772 | 2011-07-13 03:05:22 +0000 | [diff] [blame] | 3661 | pjsua_var.snd_is_on = PJ_FALSE; |
Benny Prijono | b90fd38 | 2011-09-18 14:59:56 +0000 | [diff] [blame] | 3662 | |
| 3663 | pj_log_pop_indent(); |
Benny Prijono | f798e50 | 2009-03-09 13:08:16 +0000 | [diff] [blame] | 3664 | } |
| 3665 | |
| 3666 | |
| 3667 | /* |
| 3668 | * Select or change sound device. Application may call this function at |
| 3669 | * any time to replace current sound device. |
| 3670 | */ |
| 3671 | PJ_DEF(pj_status_t) pjsua_set_snd_dev( int capture_dev, |
| 3672 | int playback_dev) |
| 3673 | { |
| 3674 | unsigned alt_cr_cnt = 1; |
| 3675 | unsigned alt_cr[] = {0, 44100, 48000, 32000, 16000, 8000}; |
| 3676 | unsigned i; |
| 3677 | pj_status_t status = -1; |
| 3678 | |
Benny Prijono | b90fd38 | 2011-09-18 14:59:56 +0000 | [diff] [blame] | 3679 | PJ_LOG(4,(THIS_FILE, "Set sound device: capture=%d, playback=%d", |
| 3680 | capture_dev, playback_dev)); |
| 3681 | pj_log_push_indent(); |
| 3682 | |
Benny Prijono | 23ea21a | 2009-06-03 12:43:06 +0000 | [diff] [blame] | 3683 | /* Null-sound */ |
| 3684 | if (capture_dev==NULL_SND_DEV_ID && playback_dev==NULL_SND_DEV_ID) { |
Benny Prijono | b90fd38 | 2011-09-18 14:59:56 +0000 | [diff] [blame] | 3685 | status = pjsua_set_null_snd_dev(); |
| 3686 | pj_log_pop_indent(); |
| 3687 | return status; |
Benny Prijono | 23ea21a | 2009-06-03 12:43:06 +0000 | [diff] [blame] | 3688 | } |
| 3689 | |
Benny Prijono | f798e50 | 2009-03-09 13:08:16 +0000 | [diff] [blame] | 3690 | /* Set default clock rate */ |
| 3691 | alt_cr[0] = pjsua_var.media_cfg.snd_clock_rate; |
| 3692 | if (alt_cr[0] == 0) |
| 3693 | alt_cr[0] = pjsua_var.media_cfg.clock_rate; |
| 3694 | |
| 3695 | /* Allow retrying of different clock rate if we're using conference |
| 3696 | * bridge (meaning audio format is always PCM), otherwise lock on |
| 3697 | * to one clock rate. |
| 3698 | */ |
| 3699 | if (pjsua_var.is_mswitch) { |
| 3700 | alt_cr_cnt = 1; |
| 3701 | } else { |
| 3702 | alt_cr_cnt = PJ_ARRAY_SIZE(alt_cr); |
| 3703 | } |
| 3704 | |
| 3705 | /* Attempts to open the sound device with different clock rates */ |
| 3706 | for (i=0; i<alt_cr_cnt; ++i) { |
Sauw Ming | 98766c7 | 2011-03-11 06:57:24 +0000 | [diff] [blame] | 3707 | pjmedia_snd_port_param param; |
Benny Prijono | f798e50 | 2009-03-09 13:08:16 +0000 | [diff] [blame] | 3708 | unsigned samples_per_frame; |
| 3709 | |
| 3710 | /* Create the default audio param */ |
| 3711 | samples_per_frame = alt_cr[i] * |
| 3712 | pjsua_var.media_cfg.audio_frame_ptime * |
| 3713 | pjsua_var.media_cfg.channel_count / 1000; |
Sauw Ming | 98766c7 | 2011-03-11 06:57:24 +0000 | [diff] [blame] | 3714 | status = create_aud_param(¶m.base, capture_dev, playback_dev, |
Benny Prijono | f798e50 | 2009-03-09 13:08:16 +0000 | [diff] [blame] | 3715 | alt_cr[i], pjsua_var.media_cfg.channel_count, |
| 3716 | samples_per_frame, 16); |
| 3717 | if (status != PJ_SUCCESS) |
Benny Prijono | b90fd38 | 2011-09-18 14:59:56 +0000 | [diff] [blame] | 3718 | goto on_error; |
Benny Prijono | f798e50 | 2009-03-09 13:08:16 +0000 | [diff] [blame] | 3719 | |
| 3720 | /* Open! */ |
Sauw Ming | 98766c7 | 2011-03-11 06:57:24 +0000 | [diff] [blame] | 3721 | param.options = 0; |
Benny Prijono | f798e50 | 2009-03-09 13:08:16 +0000 | [diff] [blame] | 3722 | status = open_snd_dev(¶m); |
| 3723 | if (status == PJ_SUCCESS) |
| 3724 | break; |
| 3725 | } |
| 3726 | |
| 3727 | if (status != PJ_SUCCESS) { |
| 3728 | pjsua_perror(THIS_FILE, "Unable to open sound device", status); |
Benny Prijono | b90fd38 | 2011-09-18 14:59:56 +0000 | [diff] [blame] | 3729 | goto on_error; |
Benny Prijono | f798e50 | 2009-03-09 13:08:16 +0000 | [diff] [blame] | 3730 | } |
| 3731 | |
Benny Prijono | e25fe6f | 2009-07-16 17:52:08 +0000 | [diff] [blame] | 3732 | pjsua_var.no_snd = PJ_FALSE; |
Benny Prijono | 2d64772 | 2011-07-13 03:05:22 +0000 | [diff] [blame] | 3733 | pjsua_var.snd_is_on = PJ_TRUE; |
Benny Prijono | e25fe6f | 2009-07-16 17:52:08 +0000 | [diff] [blame] | 3734 | |
Benny Prijono | b90fd38 | 2011-09-18 14:59:56 +0000 | [diff] [blame] | 3735 | pj_log_pop_indent(); |
Benny Prijono | f798e50 | 2009-03-09 13:08:16 +0000 | [diff] [blame] | 3736 | return PJ_SUCCESS; |
Benny Prijono | b90fd38 | 2011-09-18 14:59:56 +0000 | [diff] [blame] | 3737 | |
| 3738 | on_error: |
| 3739 | pj_log_pop_indent(); |
| 3740 | return status; |
Benny Prijono | eebe9af | 2006-06-13 22:57:13 +0000 | [diff] [blame] | 3741 | } |
| 3742 | |
| 3743 | |
| 3744 | /* |
Benny Prijono | ebdf877 | 2007-02-01 19:25:50 +0000 | [diff] [blame] | 3745 | * Get currently active sound devices. If sound devices has not been created |
| 3746 | * (for example when pjsua_start() is not called), it is possible that |
| 3747 | * the function returns PJ_SUCCESS with -1 as device IDs. |
| 3748 | */ |
| 3749 | PJ_DEF(pj_status_t) pjsua_get_snd_dev(int *capture_dev, |
| 3750 | int *playback_dev) |
| 3751 | { |
| 3752 | if (capture_dev) { |
| 3753 | *capture_dev = pjsua_var.cap_dev; |
| 3754 | } |
| 3755 | if (playback_dev) { |
| 3756 | *playback_dev = pjsua_var.play_dev; |
| 3757 | } |
| 3758 | |
| 3759 | return PJ_SUCCESS; |
| 3760 | } |
| 3761 | |
| 3762 | |
| 3763 | /* |
Benny Prijono | eebe9af | 2006-06-13 22:57:13 +0000 | [diff] [blame] | 3764 | * Use null sound device. |
| 3765 | */ |
| 3766 | PJ_DEF(pj_status_t) pjsua_set_null_snd_dev(void) |
| 3767 | { |
| 3768 | pjmedia_port *conf_port; |
| 3769 | pj_status_t status; |
| 3770 | |
Benny Prijono | b90fd38 | 2011-09-18 14:59:56 +0000 | [diff] [blame] | 3771 | PJ_LOG(4,(THIS_FILE, "Setting null sound device..")); |
| 3772 | pj_log_push_indent(); |
| 3773 | |
| 3774 | |
Benny Prijono | eebe9af | 2006-06-13 22:57:13 +0000 | [diff] [blame] | 3775 | /* Close existing sound device */ |
| 3776 | close_snd_dev(); |
| 3777 | |
Benny Prijono | 2d64772 | 2011-07-13 03:05:22 +0000 | [diff] [blame] | 3778 | /* Notify app */ |
| 3779 | if (pjsua_var.ua_cfg.cb.on_snd_dev_operation) { |
| 3780 | (*pjsua_var.ua_cfg.cb.on_snd_dev_operation)(1); |
| 3781 | } |
| 3782 | |
Nanang Izzuddin | 68559c3 | 2008-06-13 17:01:46 +0000 | [diff] [blame] | 3783 | /* Create memory pool for sound device. */ |
| 3784 | pjsua_var.snd_pool = pjsua_pool_create("pjsua_snd", 4000, 4000); |
| 3785 | PJ_ASSERT_RETURN(pjsua_var.snd_pool, PJ_ENOMEM); |
| 3786 | |
Benny Prijono | eebe9af | 2006-06-13 22:57:13 +0000 | [diff] [blame] | 3787 | PJ_LOG(4,(THIS_FILE, "Opening null sound device..")); |
| 3788 | |
| 3789 | /* Get the port0 of the conference bridge. */ |
| 3790 | conf_port = pjmedia_conf_get_master_port(pjsua_var.mconf); |
| 3791 | pj_assert(conf_port != NULL); |
| 3792 | |
| 3793 | /* Create master port, connecting port0 of the conference bridge to |
| 3794 | * a null port. |
| 3795 | */ |
Nanang Izzuddin | 68559c3 | 2008-06-13 17:01:46 +0000 | [diff] [blame] | 3796 | 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] | 3797 | conf_port, 0, &pjsua_var.null_snd); |
| 3798 | if (status != PJ_SUCCESS) { |
| 3799 | pjsua_perror(THIS_FILE, "Unable to create null sound device", |
| 3800 | status); |
Benny Prijono | b90fd38 | 2011-09-18 14:59:56 +0000 | [diff] [blame] | 3801 | pj_log_pop_indent(); |
Benny Prijono | eebe9af | 2006-06-13 22:57:13 +0000 | [diff] [blame] | 3802 | return status; |
| 3803 | } |
| 3804 | |
| 3805 | /* Start the master port */ |
| 3806 | status = pjmedia_master_port_start(pjsua_var.null_snd); |
| 3807 | PJ_ASSERT_RETURN(status == PJ_SUCCESS, status); |
| 3808 | |
Nanang Izzuddin | 68559c3 | 2008-06-13 17:01:46 +0000 | [diff] [blame] | 3809 | pjsua_var.cap_dev = NULL_SND_DEV_ID; |
| 3810 | pjsua_var.play_dev = NULL_SND_DEV_ID; |
| 3811 | |
Benny Prijono | e25fe6f | 2009-07-16 17:52:08 +0000 | [diff] [blame] | 3812 | pjsua_var.no_snd = PJ_FALSE; |
Benny Prijono | 2d64772 | 2011-07-13 03:05:22 +0000 | [diff] [blame] | 3813 | pjsua_var.snd_is_on = PJ_TRUE; |
Benny Prijono | e25fe6f | 2009-07-16 17:52:08 +0000 | [diff] [blame] | 3814 | |
Benny Prijono | b90fd38 | 2011-09-18 14:59:56 +0000 | [diff] [blame] | 3815 | pj_log_pop_indent(); |
Benny Prijono | eebe9af | 2006-06-13 22:57:13 +0000 | [diff] [blame] | 3816 | return PJ_SUCCESS; |
| 3817 | } |
| 3818 | |
| 3819 | |
Benny Prijono | e909eac | 2006-07-27 22:04:56 +0000 | [diff] [blame] | 3820 | |
| 3821 | /* |
| 3822 | * Use no device! |
| 3823 | */ |
| 3824 | PJ_DEF(pjmedia_port*) pjsua_set_no_snd_dev(void) |
| 3825 | { |
| 3826 | /* Close existing sound device */ |
| 3827 | close_snd_dev(); |
| 3828 | |
| 3829 | pjsua_var.no_snd = PJ_TRUE; |
| 3830 | return pjmedia_conf_get_master_port(pjsua_var.mconf); |
| 3831 | } |
| 3832 | |
| 3833 | |
Benny Prijono | f20687a | 2006-08-04 18:27:19 +0000 | [diff] [blame] | 3834 | /* |
| 3835 | * Configure the AEC settings of the sound port. |
| 3836 | */ |
Benny Prijono | 5da5043 | 2006-08-07 10:24:52 +0000 | [diff] [blame] | 3837 | 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] | 3838 | { |
| 3839 | pjsua_var.media_cfg.ec_tail_len = tail_ms; |
| 3840 | |
| 3841 | if (pjsua_var.snd_port) |
Benny Prijono | 5da5043 | 2006-08-07 10:24:52 +0000 | [diff] [blame] | 3842 | return pjmedia_snd_port_set_ec( pjsua_var.snd_port, pjsua_var.pool, |
| 3843 | tail_ms, options); |
Benny Prijono | f20687a | 2006-08-04 18:27:19 +0000 | [diff] [blame] | 3844 | |
| 3845 | return PJ_SUCCESS; |
| 3846 | } |
| 3847 | |
| 3848 | |
| 3849 | /* |
| 3850 | * Get current AEC tail length. |
| 3851 | */ |
Benny Prijono | 22dfe59 | 2006-08-06 12:07:13 +0000 | [diff] [blame] | 3852 | 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] | 3853 | { |
| 3854 | *p_tail_ms = pjsua_var.media_cfg.ec_tail_len; |
| 3855 | return PJ_SUCCESS; |
| 3856 | } |
| 3857 | |
Nanang Izzuddin | fe02a06 | 2009-02-18 14:28:49 +0000 | [diff] [blame] | 3858 | |
Nanang Izzuddin | 0cb3b02 | 2009-02-27 17:37:35 +0000 | [diff] [blame] | 3859 | /* |
Benny Prijono | f798e50 | 2009-03-09 13:08:16 +0000 | [diff] [blame] | 3860 | * Check whether the sound device is currently active. |
Nanang Izzuddin | 0cb3b02 | 2009-02-27 17:37:35 +0000 | [diff] [blame] | 3861 | */ |
Benny Prijono | f798e50 | 2009-03-09 13:08:16 +0000 | [diff] [blame] | 3862 | PJ_DEF(pj_bool_t) pjsua_snd_is_active(void) |
Nanang Izzuddin | 0cb3b02 | 2009-02-27 17:37:35 +0000 | [diff] [blame] | 3863 | { |
Benny Prijono | f798e50 | 2009-03-09 13:08:16 +0000 | [diff] [blame] | 3864 | return pjsua_var.snd_port != NULL; |
| 3865 | } |
Nanang Izzuddin | 0cb3b02 | 2009-02-27 17:37:35 +0000 | [diff] [blame] | 3866 | |
Nanang Izzuddin | 0cb3b02 | 2009-02-27 17:37:35 +0000 | [diff] [blame] | 3867 | |
Benny Prijono | f798e50 | 2009-03-09 13:08:16 +0000 | [diff] [blame] | 3868 | /* |
| 3869 | * Configure sound device setting to the sound device being used. |
| 3870 | */ |
| 3871 | PJ_DEF(pj_status_t) pjsua_snd_set_setting( pjmedia_aud_dev_cap cap, |
| 3872 | const void *pval, |
| 3873 | pj_bool_t keep) |
| 3874 | { |
Benny Prijono | 09b0ff6 | 2009-03-10 12:07:51 +0000 | [diff] [blame] | 3875 | pj_status_t status; |
| 3876 | |
Benny Prijono | f798e50 | 2009-03-09 13:08:16 +0000 | [diff] [blame] | 3877 | /* Check if we are allowed to set the cap */ |
Benny Prijono | 09b0ff6 | 2009-03-10 12:07:51 +0000 | [diff] [blame] | 3878 | if ((cap & pjsua_var.aud_svmask) == 0) { |
Benny Prijono | f798e50 | 2009-03-09 13:08:16 +0000 | [diff] [blame] | 3879 | return PJMEDIA_EAUD_INVCAP; |
| 3880 | } |
| 3881 | |
Benny Prijono | 09b0ff6 | 2009-03-10 12:07:51 +0000 | [diff] [blame] | 3882 | /* If sound is active, set it immediately */ |
Benny Prijono | f798e50 | 2009-03-09 13:08:16 +0000 | [diff] [blame] | 3883 | if (pjsua_snd_is_active()) { |
Benny Prijono | f798e50 | 2009-03-09 13:08:16 +0000 | [diff] [blame] | 3884 | pjmedia_aud_stream *strm; |
| 3885 | |
| 3886 | strm = pjmedia_snd_port_get_snd_stream(pjsua_var.snd_port); |
Benny Prijono | 09b0ff6 | 2009-03-10 12:07:51 +0000 | [diff] [blame] | 3887 | status = pjmedia_aud_stream_set_cap(strm, cap, pval); |
Benny Prijono | f798e50 | 2009-03-09 13:08:16 +0000 | [diff] [blame] | 3888 | } else { |
Benny Prijono | 09b0ff6 | 2009-03-10 12:07:51 +0000 | [diff] [blame] | 3889 | status = PJ_SUCCESS; |
Benny Prijono | f798e50 | 2009-03-09 13:08:16 +0000 | [diff] [blame] | 3890 | } |
Benny Prijono | 09b0ff6 | 2009-03-10 12:07:51 +0000 | [diff] [blame] | 3891 | |
| 3892 | if (status != PJ_SUCCESS) |
| 3893 | return status; |
| 3894 | |
| 3895 | /* Save in internal param for later device open */ |
| 3896 | if (keep) { |
| 3897 | status = pjmedia_aud_param_set_cap(&pjsua_var.aud_param, |
| 3898 | cap, pval); |
| 3899 | } |
| 3900 | |
| 3901 | return status; |
Benny Prijono | f798e50 | 2009-03-09 13:08:16 +0000 | [diff] [blame] | 3902 | } |
| 3903 | |
| 3904 | /* |
| 3905 | * Retrieve a sound device setting. |
| 3906 | */ |
| 3907 | PJ_DEF(pj_status_t) pjsua_snd_get_setting( pjmedia_aud_dev_cap cap, |
| 3908 | void *pval) |
| 3909 | { |
| 3910 | /* If sound device has never been opened before, open it to |
| 3911 | * retrieve the initial setting from the device (e.g. audio |
| 3912 | * volume) |
| 3913 | */ |
Benny Prijono | 09b0ff6 | 2009-03-10 12:07:51 +0000 | [diff] [blame] | 3914 | if (pjsua_var.aud_open_cnt==0) { |
| 3915 | PJ_LOG(4,(THIS_FILE, "Opening sound device to get initial settings")); |
Benny Prijono | f798e50 | 2009-03-09 13:08:16 +0000 | [diff] [blame] | 3916 | pjsua_set_snd_dev(pjsua_var.cap_dev, pjsua_var.play_dev); |
Benny Prijono | 09b0ff6 | 2009-03-10 12:07:51 +0000 | [diff] [blame] | 3917 | close_snd_dev(); |
| 3918 | } |
Benny Prijono | f798e50 | 2009-03-09 13:08:16 +0000 | [diff] [blame] | 3919 | |
| 3920 | if (pjsua_snd_is_active()) { |
| 3921 | /* Sound is active, retrieve from device directly */ |
| 3922 | pjmedia_aud_stream *strm; |
| 3923 | |
| 3924 | strm = pjmedia_snd_port_get_snd_stream(pjsua_var.snd_port); |
| 3925 | return pjmedia_aud_stream_get_cap(strm, cap, pval); |
| 3926 | } else { |
| 3927 | /* Otherwise retrieve from internal param */ |
| 3928 | return pjmedia_aud_param_get_cap(&pjsua_var.aud_param, |
| 3929 | cap, pval); |
| 3930 | } |
Nanang Izzuddin | 0cb3b02 | 2009-02-27 17:37:35 +0000 | [diff] [blame] | 3931 | } |
| 3932 | |
| 3933 | |
Benny Prijono | eebe9af | 2006-06-13 22:57:13 +0000 | [diff] [blame] | 3934 | /***************************************************************************** |
| 3935 | * Codecs. |
| 3936 | */ |
| 3937 | |
| 3938 | /* |
| 3939 | * Enum all supported codecs in the system. |
| 3940 | */ |
| 3941 | PJ_DEF(pj_status_t) pjsua_enum_codecs( pjsua_codec_info id[], |
| 3942 | unsigned *p_count ) |
| 3943 | { |
| 3944 | pjmedia_codec_mgr *codec_mgr; |
| 3945 | pjmedia_codec_info info[32]; |
| 3946 | unsigned i, count, prio[32]; |
| 3947 | pj_status_t status; |
| 3948 | |
| 3949 | codec_mgr = pjmedia_endpt_get_codec_mgr(pjsua_var.med_endpt); |
| 3950 | count = PJ_ARRAY_SIZE(info); |
| 3951 | status = pjmedia_codec_mgr_enum_codecs( codec_mgr, &count, info, prio); |
| 3952 | if (status != PJ_SUCCESS) { |
| 3953 | *p_count = 0; |
| 3954 | return status; |
| 3955 | } |
| 3956 | |
| 3957 | if (count > *p_count) count = *p_count; |
| 3958 | |
| 3959 | for (i=0; i<count; ++i) { |
Nanang Izzuddin | 56b2ce4 | 2011-04-06 13:55:01 +0000 | [diff] [blame] | 3960 | pj_bzero(&id[i], sizeof(pjsua_codec_info)); |
| 3961 | |
Benny Prijono | eebe9af | 2006-06-13 22:57:13 +0000 | [diff] [blame] | 3962 | pjmedia_codec_info_to_id(&info[i], id[i].buf_, sizeof(id[i].buf_)); |
| 3963 | id[i].codec_id = pj_str(id[i].buf_); |
| 3964 | id[i].priority = (pj_uint8_t) prio[i]; |
| 3965 | } |
| 3966 | |
| 3967 | *p_count = count; |
| 3968 | |
| 3969 | return PJ_SUCCESS; |
| 3970 | } |
| 3971 | |
| 3972 | |
| 3973 | /* |
| 3974 | * Change codec priority. |
| 3975 | */ |
| 3976 | PJ_DEF(pj_status_t) pjsua_codec_set_priority( const pj_str_t *codec_id, |
| 3977 | pj_uint8_t priority ) |
| 3978 | { |
Benny Prijono | 88accae | 2008-06-26 15:48:14 +0000 | [diff] [blame] | 3979 | const pj_str_t all = { NULL, 0 }; |
Benny Prijono | eebe9af | 2006-06-13 22:57:13 +0000 | [diff] [blame] | 3980 | pjmedia_codec_mgr *codec_mgr; |
| 3981 | |
| 3982 | codec_mgr = pjmedia_endpt_get_codec_mgr(pjsua_var.med_endpt); |
| 3983 | |
Benny Prijono | 88accae | 2008-06-26 15:48:14 +0000 | [diff] [blame] | 3984 | if (codec_id->slen==1 && *codec_id->ptr=='*') |
| 3985 | codec_id = &all; |
| 3986 | |
Benny Prijono | eebe9af | 2006-06-13 22:57:13 +0000 | [diff] [blame] | 3987 | return pjmedia_codec_mgr_set_codec_priority(codec_mgr, codec_id, |
| 3988 | priority); |
| 3989 | } |
| 3990 | |
| 3991 | |
| 3992 | /* |
| 3993 | * Get codec parameters. |
| 3994 | */ |
| 3995 | PJ_DEF(pj_status_t) pjsua_codec_get_param( const pj_str_t *codec_id, |
| 3996 | pjmedia_codec_param *param ) |
| 3997 | { |
Benny Prijono | 88accae | 2008-06-26 15:48:14 +0000 | [diff] [blame] | 3998 | const pj_str_t all = { NULL, 0 }; |
Benny Prijono | eebe9af | 2006-06-13 22:57:13 +0000 | [diff] [blame] | 3999 | const pjmedia_codec_info *info; |
| 4000 | pjmedia_codec_mgr *codec_mgr; |
| 4001 | unsigned count = 1; |
| 4002 | pj_status_t status; |
| 4003 | |
| 4004 | codec_mgr = pjmedia_endpt_get_codec_mgr(pjsua_var.med_endpt); |
| 4005 | |
Benny Prijono | 88accae | 2008-06-26 15:48:14 +0000 | [diff] [blame] | 4006 | if (codec_id->slen==1 && *codec_id->ptr=='*') |
| 4007 | codec_id = &all; |
| 4008 | |
Benny Prijono | eebe9af | 2006-06-13 22:57:13 +0000 | [diff] [blame] | 4009 | status = pjmedia_codec_mgr_find_codecs_by_id(codec_mgr, codec_id, |
| 4010 | &count, &info, NULL); |
| 4011 | if (status != PJ_SUCCESS) |
| 4012 | return status; |
| 4013 | |
| 4014 | if (count != 1) |
Nanang Izzuddin | 50fae73 | 2011-03-22 09:49:23 +0000 | [diff] [blame] | 4015 | return (count > 1? PJ_ETOOMANY : PJ_ENOTFOUND); |
Benny Prijono | eebe9af | 2006-06-13 22:57:13 +0000 | [diff] [blame] | 4016 | |
| 4017 | status = pjmedia_codec_mgr_get_default_param( codec_mgr, info, param); |
| 4018 | return status; |
| 4019 | } |
| 4020 | |
| 4021 | |
| 4022 | /* |
| 4023 | * Set codec parameters. |
| 4024 | */ |
Nanang Izzuddin | 06839e7 | 2010-01-27 11:48:31 +0000 | [diff] [blame] | 4025 | PJ_DEF(pj_status_t) pjsua_codec_set_param( const pj_str_t *codec_id, |
Benny Prijono | eebe9af | 2006-06-13 22:57:13 +0000 | [diff] [blame] | 4026 | const pjmedia_codec_param *param) |
| 4027 | { |
Nanang Izzuddin | 06839e7 | 2010-01-27 11:48:31 +0000 | [diff] [blame] | 4028 | const pjmedia_codec_info *info[2]; |
| 4029 | pjmedia_codec_mgr *codec_mgr; |
| 4030 | unsigned count = 2; |
| 4031 | pj_status_t status; |
| 4032 | |
| 4033 | codec_mgr = pjmedia_endpt_get_codec_mgr(pjsua_var.med_endpt); |
| 4034 | |
| 4035 | status = pjmedia_codec_mgr_find_codecs_by_id(codec_mgr, codec_id, |
| 4036 | &count, info, NULL); |
| 4037 | if (status != PJ_SUCCESS) |
| 4038 | return status; |
| 4039 | |
| 4040 | /* Codec ID should be specific, except for G.722.1 */ |
| 4041 | if (count > 1 && |
| 4042 | pj_strnicmp2(codec_id, "G7221/16", 8) != 0 && |
| 4043 | pj_strnicmp2(codec_id, "G7221/32", 8) != 0) |
| 4044 | { |
| 4045 | pj_assert(!"Codec ID is not specific"); |
| 4046 | return PJ_ETOOMANY; |
| 4047 | } |
| 4048 | |
| 4049 | status = pjmedia_codec_mgr_set_default_param(codec_mgr, info[0], param); |
| 4050 | return status; |
Benny Prijono | eebe9af | 2006-06-13 22:57:13 +0000 | [diff] [blame] | 4051 | } |
Nanang Izzuddin | 50fae73 | 2011-03-22 09:49:23 +0000 | [diff] [blame] | 4052 | |
| 4053 | |