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