| 1 |
|
| 2 |
|
| 3 |
|
| 4 |
|
| 5 |
|
| 6 |
|
| 7 |
|
| 8 |
|
| 9 |
|
| 10 |
|
| 11 |
|
| 12 |
|
| 13 |
|
| 14 |
|
| 15 |
|
| 16 |
|
| 17 |
|
| 18 |
|
| 19 |
|
| 20 |
|
| 21 |
|
| 22 |
|
| 23 |
|
| 24 |
|
| 25 |
|
| 26 |
|
| 27 |
|
| 28 |
|
| 29 |
|
| 30 |
|
| 31 |
|
| 32 |
|
| 33 |
|
| 34 |
#include "asterisk.h" |
| 35 |
|
| 36 |
#include <pjsip.h> |
| 37 |
#include <pjsip_ua.h> |
| 38 |
#include <pjmedia.h> |
| 39 |
#include <pjlib.h> |
| 40 |
|
| 41 |
#include "asterisk/utils.h" |
| 42 |
#include "asterisk/module.h" |
| 43 |
#include "asterisk/format.h" |
| 44 |
#include "asterisk/format_cap.h" |
| 45 |
#include "asterisk/rtp_engine.h" |
| 46 |
#include "asterisk/netsock2.h" |
| 47 |
#include "asterisk/channel.h" |
| 48 |
#include "asterisk/causes.h" |
| 49 |
#include "asterisk/sched.h" |
| 50 |
#include "asterisk/acl.h" |
| 51 |
#include "asterisk/sdp_srtp.h" |
| 52 |
#include "asterisk/dsp.h" |
| 53 |
#include "asterisk/linkedlists.h" |
| 54 |
#include "asterisk/stream.h" |
| 55 |
#include "asterisk/logger_category.h" |
| 56 |
#include "asterisk/format_cache.h" |
| 57 |
|
| 58 |
#include "asterisk/res_pjsip.h" |
| 59 |
#include "asterisk/res_pjsip_session.h" |
| 60 |
#include "asterisk/res_pjsip_session_caps.h" |
| 61 |
|
| 62 |
|
| 63 |
static struct ast_sched_context *sched; |
| 64 |
|
| 65 |
|
| 66 |
static struct ast_sockaddr address_rtp; |
| 67 |
|
| 68 |
static const char STR_AUDIO[] = "audio"; |
| 69 |
static const char STR_VIDEO[] = "video"; |
| 70 |
|
| 71 |
static int send_keepalive(const void *data) |
| 72 |
{ |
| 73 |
struct ast_sip_session_media *session_media = (struct ast_sip_session_media *) data; |
| 74 |
struct ast_rtp_instance *rtp = session_media->rtp; |
| 75 |
int keepalive; |
| 76 |
time_t interval; |
| 77 |
int send_keepalive; |
| 78 |
|
| 79 |
if (!rtp) { |
| 80 |
return 0; |
| 81 |
} |
| 82 |
|
| 83 |
keepalive = ast_rtp_instance_get_keepalive(rtp); |
| 84 |
|
| 85 |
if (!ast_sockaddr_isnull(&session_media->direct_media_addr)) { |
| 86 |
ast_debug_rtp(3, "(%p) RTP not sending keepalive since direct media is in use\n", rtp); |
| 87 |
return keepalive * 1000; |
| 88 |
} |
| 89 |
|
| 90 |
interval = time(NULL) - ast_rtp_instance_get_last_tx(rtp); |
| 91 |
send_keepalive = interval >= keepalive; |
| 92 |
|
| 93 |
ast_debug_rtp(3, "(%p) RTP it has been %d seconds since RTP was last sent. %sending keepalive\n", |
| 94 |
rtp, (int) interval, send_keepalive ? "S" : "Not s"); |
| 95 |
|
| 96 |
if (send_keepalive) { |
| 97 |
ast_rtp_instance_sendcng(rtp, 0); |
| 98 |
return keepalive * 1000; |
| 99 |
} |
| 100 |
|
| 101 |
return (keepalive - interval) * 1000; |
| 102 |
} |
| 103 |
|
| 104 |
|
| 105 |
static int rtp_check_timeout(const void *data) |
| 106 |
{ |
| 107 |
struct ast_sip_session_media *session_media = (struct ast_sip_session_media *)data; |
| 108 |
struct ast_rtp_instance *rtp = session_media->rtp; |
| 109 |
struct ast_channel *chan; |
| 110 |
int elapsed; |
| 111 |
int now; |
| 112 |
int timeout; |
| 113 |
|
| 114 |
if (!rtp) { |
| 115 |
return 0; |
| 116 |
} |
| 117 |
|
| 118 |
chan = ast_channel_get_by_name(ast_rtp_instance_get_channel_id(rtp)); |
| 119 |
if (!chan) { |
| 120 |
return 0; |
| 121 |
} |
| 122 |
|
| 123 |
|
| 124 |
now = time(NULL); |
| 125 |
timeout = ast_rtp_instance_get_timeout(rtp); |
| 126 |
|
| 127 |
|
| 128 |
|
| 129 |
|
| 130 |
if (ast_channel_state(chan) != AST_STATE_UP || !ast_sockaddr_isnull(&session_media->direct_media_addr)) { |
| 131 |
|
| 132 |
ast_rtp_instance_set_last_rx(rtp, now); |
| 133 |
ast_channel_unref(chan); |
| 134 |
|
| 135 |
|
| 136 |
|
| 137 |
return timeout * 500; |
| 138 |
} |
| 139 |
|
| 140 |
elapsed = now - ast_rtp_instance_get_last_rx(rtp); |
| 141 |
if (elapsed < timeout) { |
| 142 |
ast_channel_unref(chan); |
| 143 |
return (timeout - elapsed) * 1000; |
| 144 |
} |
| 145 |
|
| 146 |
ast_log(LOG_NOTICE, "Disconnecting channel '%s' for lack of %s RTP activity in %d seconds\n", |
| 147 |
ast_channel_name(chan), ast_codec_media_type2str(session_media->type), elapsed); |
| 148 |
|
| 149 |
ast_channel_lock(chan); |
| 150 |
ast_channel_hangupcause_set(chan, AST_CAUSE_REQUESTED_CHAN_UNAVAIL); |
| 151 |
ast_channel_unlock(chan); |
| 152 |
|
| 153 |
ast_softhangup(chan, AST_SOFTHANGUP_DEV); |
| 154 |
ast_channel_unref(chan); |
| 155 |
|
| 156 |
return 0; |
| 157 |
} |
| 158 |
|
| 159 |
|
| 160 |
|
| 161 |
|
| 162 |
static void enable_rtcp(struct ast_sip_session *session, struct ast_sip_session_media *session_media, |
| 163 |
const struct pjmedia_sdp_media *remote_media) |
| 164 |
{ |
| 165 |
enum ast_rtp_instance_rtcp rtcp_type; |
| 166 |
|
| 167 |
if (session->endpoint->media.rtcp_mux && session_media->remote_rtcp_mux) { |
| 168 |
rtcp_type = AST_RTP_INSTANCE_RTCP_MUX; |
| 169 |
} else { |
| 170 |
rtcp_type = AST_RTP_INSTANCE_RTCP_STANDARD; |
| 171 |
} |
| 172 |
|
| 173 |
ast_rtp_instance_set_prop(session_media->rtp, AST_RTP_PROPERTY_RTCP, rtcp_type); |
| 174 |
} |
| 175 |
|
| 176 |
|
| 177 |
|
| 178 |
|
| 179 |
static void enable_rtp_extension(struct ast_sip_session *session, struct ast_sip_session_media *session_media, |
| 180 |
enum ast_rtp_extension extension, enum ast_rtp_extension_direction direction, |
| 181 |
const pjmedia_sdp_session *sdp) |
| 182 |
{ |
| 183 |
int id = -1; |
| 184 |
|
| 185 |
|
| 186 |
|
| 187 |
|
| 188 |
if (session_media->bundle_group != -1) { |
| 189 |
int index; |
| 190 |
|
| 191 |
for (index = 0; index < sdp->media_count; ++index) { |
| 192 |
struct ast_sip_session_media *other_session_media; |
| 193 |
int other_id; |
| 194 |
|
| 195 |
if (index >= AST_VECTOR_SIZE(&session->pending_media_state->sessions)) { |
| 196 |
break; |
| 197 |
} |
| 198 |
|
| 199 |
other_session_media = AST_VECTOR_GET(&session->pending_media_state->sessions, index); |
| 200 |
if (!other_session_media->rtp || other_session_media->bundle_group != session_media->bundle_group) { |
| 201 |
continue; |
| 202 |
} |
| 203 |
|
| 204 |
other_id = ast_rtp_instance_extmap_get_id(other_session_media->rtp, extension); |
| 205 |
if (other_id == -1) { |
| 206 |
|
| 207 |
|
| 208 |
|
| 209 |
other_id = ast_rtp_instance_extmap_count(other_session_media->rtp) + 1; |
| 210 |
if (id < other_id) { |
| 211 |
id = other_id; |
| 212 |
} |
| 213 |
continue; |
| 214 |
} |
| 215 |
|
| 216 |
id = other_id; |
| 217 |
break; |
| 218 |
} |
| 219 |
} |
| 220 |
|
| 221 |
ast_rtp_instance_extmap_enable(session_media->rtp, id, extension, direction); |
| 222 |
} |
| 223 |
|
| 224 |
|
| 225 |
static int create_rtp(struct ast_sip_session *session, struct ast_sip_session_media *session_media, |
| 226 |
const pjmedia_sdp_session *sdp) |
| 227 |
{ |
| 228 |
struct ast_rtp_engine_ice *ice; |
| 229 |
struct ast_sockaddr temp_media_address; |
| 230 |
struct ast_sockaddr *media_address = &address_rtp; |
| 231 |
|
| 232 |
if (session->endpoint->media.bind_rtp_to_media_address && !ast_strlen_zero(session->endpoint->media.address)) { |
| 233 |
if (ast_sockaddr_parse(&temp_media_address, session->endpoint->media.address, 0)) { |
| 234 |
ast_debug_rtp(1, "Endpoint %s: Binding RTP media to %s\n", |
| 235 |
ast_sorcery_object_get_id(session->endpoint), |
| 236 |
session->endpoint->media.address); |
| 237 |
media_address = &temp_media_address; |
| 238 |
} else { |
| 239 |
ast_debug_rtp(1, "Endpoint %s: RTP media address invalid: %s\n", |
| 240 |
ast_sorcery_object_get_id(session->endpoint), |
| 241 |
session->endpoint->media.address); |
| 242 |
} |
| 243 |
} else { |
| 244 |
struct ast_sip_transport *transport; |
| 245 |
|
| 246 |
transport = ast_sorcery_retrieve_by_id(ast_sip_get_sorcery(), "transport", |
| 247 |
session->endpoint->transport); |
| 248 |
if (transport) { |
| 249 |
struct ast_sip_transport_state *trans_state; |
| 250 |
|
| 251 |
trans_state = ast_sip_get_transport_state(ast_sorcery_object_get_id(transport)); |
| 252 |
if (trans_state) { |
| 253 |
char hoststr[PJ_INET6_ADDRSTRLEN]; |
| 254 |
|
| 255 |
pj_sockaddr_print(&trans_state->host, hoststr, sizeof(hoststr), 0); |
| 256 |
if (ast_sockaddr_parse(&temp_media_address, hoststr, 0)) { |
| 257 |
ast_debug_rtp(1, "Transport %s bound to %s: Using it for RTP media.\n", |
| 258 |
session->endpoint->transport, hoststr); |
| 259 |
media_address = &temp_media_address; |
| 260 |
} else { |
| 261 |
ast_debug_rtp(1, "Transport %s bound to %s: Invalid for RTP media.\n", |
| 262 |
session->endpoint->transport, hoststr); |
| 263 |
} |
| 264 |
ao2_ref(trans_state, -1); |
| 265 |
} |
| 266 |
ao2_ref(transport, -1); |
| 267 |
} |
| 268 |
} |
| 269 |
|
| 270 |
if (session->endpoint->media.rtp.port_start && session->endpoint->media.rtp.port_end) { |
| 271 |
struct ast_rtp_instance_options options = { |
| 272 |
.port_start = session->endpoint->media.rtp.port_start, |
| 273 |
.port_end = session->endpoint->media.rtp.port_end, |
| 274 |
}; |
| 275 |
if (!(session_media->rtp = ast_rtp_instance_new_with_options( |
| 276 |
session->endpoint->media.rtp.engine, sched, media_address, NULL, |
| 277 |
&options))) { |
| 278 |
ast_log(LOG_ERROR, "Unable to create RTP instance using RTP engine '%s' with port range %u-%u\n", |
| 279 |
session->endpoint->media.rtp.engine, |
| 280 |
session->endpoint->media.rtp.port_start, |
| 281 |
session->endpoint->media.rtp.port_end); |
| 282 |
return -1; |
| 283 |
} |
| 284 |
} else { |
| 285 |
if (!(session_media->rtp = ast_rtp_instance_new(session->endpoint->media.rtp.engine, sched, media_address, NULL))) { |
| 286 |
ast_log(LOG_ERROR, "Unable to create RTP instance using RTP engine '%s'\n", session->endpoint->media.rtp.engine); |
| 287 |
return -1; |
| 288 |
} |
| 289 |
} |
| 290 |
|
| 291 |
ast_rtp_instance_set_prop(session_media->rtp, AST_RTP_PROPERTY_NAT, session->endpoint->media.rtp.symmetric); |
| 292 |
ast_rtp_instance_set_prop(session_media->rtp, AST_RTP_PROPERTY_ASYMMETRIC_CODEC, session->endpoint->asymmetric_rtp_codec); |
| 293 |
|
| 294 |
if (!session->endpoint->media.rtp.ice_support && (ice = ast_rtp_instance_get_ice(session_media->rtp))) { |
| 295 |
ice->stop(session_media->rtp); |
| 296 |
} |
| 297 |
|
| 298 |
if (session->dtmf == AST_SIP_DTMF_RFC_4733 || session->dtmf == AST_SIP_DTMF_AUTO || session->dtmf == AST_SIP_DTMF_AUTO_INFO) { |
| 299 |
ast_rtp_instance_dtmf_mode_set(session_media->rtp, AST_RTP_DTMF_MODE_RFC2833); |
| 300 |
ast_rtp_instance_set_prop(session_media->rtp, AST_RTP_PROPERTY_DTMF, 1); |
| 301 |
} else if (session->dtmf == AST_SIP_DTMF_INBAND) { |
| 302 |
ast_rtp_instance_dtmf_mode_set(session_media->rtp, AST_RTP_DTMF_MODE_INBAND); |
| 303 |
} |
| 304 |
|
| 305 |
if (session_media->type == AST_MEDIA_TYPE_AUDIO && |
| 306 |
(session->endpoint->media.tos_audio || session->endpoint->media.cos_audio)) { |
| 307 |
ast_rtp_instance_set_qos(session_media->rtp, session->endpoint->media.tos_audio, |
| 308 |
session->endpoint->media.cos_audio, "SIP RTP Audio"); |
| 309 |
} else if (session_media->type == AST_MEDIA_TYPE_VIDEO) { |
| 310 |
ast_rtp_instance_set_prop(session_media->rtp, AST_RTP_PROPERTY_RETRANS_RECV, session->endpoint->media.webrtc); |
| 311 |
ast_rtp_instance_set_prop(session_media->rtp, AST_RTP_PROPERTY_RETRANS_SEND, session->endpoint->media.webrtc); |
| 312 |
ast_rtp_instance_set_prop(session_media->rtp, AST_RTP_PROPERTY_REMB, session->endpoint->media.webrtc); |
| 313 |
if (session->endpoint->media.webrtc) { |
| 314 |
enable_rtp_extension(session, session_media, AST_RTP_EXTENSION_ABS_SEND_TIME, AST_RTP_EXTENSION_DIRECTION_SENDRECV, sdp); |
| 315 |
enable_rtp_extension(session, session_media, AST_RTP_EXTENSION_TRANSPORT_WIDE_CC, AST_RTP_EXTENSION_DIRECTION_SENDRECV, sdp); |
| 316 |
} |
| 317 |
if (session->endpoint->media.tos_video || session->endpoint->media.cos_video) { |
| 318 |
ast_rtp_instance_set_qos(session_media->rtp, session->endpoint->media.tos_video, |
| 319 |
session->endpoint->media.cos_video, "SIP RTP Video"); |
| 320 |
} |
| 321 |
} |
| 322 |
|
| 323 |
ast_rtp_instance_set_last_rx(session_media->rtp, time(NULL)); |
| 324 |
|
| 325 |
return 0; |
| 326 |
} |
| 327 |
|
| 328 |
static void get_codecs(struct ast_sip_session *session, const struct pjmedia_sdp_media *stream, struct ast_rtp_codecs *codecs, |
| 329 |
struct ast_sip_session_media *session_media, struct ast_format_cap *astformats) |
| 330 |
{ |
| 331 |
pjmedia_sdp_attr *attr; |
| 332 |
pjmedia_sdp_rtpmap *rtpmap; |
| 333 |
pjmedia_sdp_fmtp fmtp; |
| 334 |
struct ast_format *format; |
| 335 |
int i, num = 0, tel_event = 0; |
| 336 |
char name[256]; |
| 337 |
char media[20]; |
| 338 |
char fmt_param[256]; |
| 339 |
enum ast_rtp_options options = session->endpoint->media.g726_non_standard ? |
| 340 |
AST_RTP_OPT_G726_NONSTANDARD : 0; |
| 341 |
SCOPE_ENTER(1, "%s\n", ast_sip_session_get_name(session)); |
| 342 |
|
| 343 |
ast_rtp_codecs_payloads_initialize(codecs); |
| 344 |
|
| 345 |
ast_format_cap_remove_by_type(astformats, AST_MEDIA_TYPE_UNKNOWN); |
| 346 |
|
| 347 |
|
| 348 |
for (i = 0; i < stream->desc.fmt_count; ++i) { |
| 349 |
|
| 350 |
ast_rtp_codecs_payloads_set_m_type(codecs, NULL, pj_strtoul(&stream->desc.fmt[i])); |
| 351 |
|
| 352 |
if (!(attr = pjmedia_sdp_media_find_attr2(stream, "rtpmap", &stream->desc.fmt[i]))) { |
| 353 |
continue; |
| 354 |
} |
| 355 |
|
| 356 |
|
| 357 |
if ((pjmedia_sdp_attr_to_rtpmap(session->inv_session->pool_prov, attr, &rtpmap)) != PJ_SUCCESS) { |
| 358 |
continue; |
| 359 |
} |
| 360 |
|
| 361 |
ast_copy_pj_str(name, &rtpmap->enc_name, sizeof(name)); |
| 362 |
if (strcmp(name, "telephone-event") == 0) { |
| 363 |
if (tel_event == 0) { |
| 364 |
int dtmf_rate = 0, dtmf_code = 0; |
| 365 |
char dtmf_pt[8]; |
| 366 |
ast_copy_pj_str(dtmf_pt, &rtpmap->pt, sizeof(dtmf_pt)); |
| 367 |
dtmf_code = atoi(dtmf_pt); |
| 368 |
dtmf_rate = rtpmap->clock_rate; |
| 369 |
ast_rtp_codecs_set_preferred_dtmf_format(codecs, dtmf_code, dtmf_rate); |
| 370 |
} |
| 371 |
tel_event++; |
| 372 |
} |
| 373 |
|
| 374 |
ast_copy_pj_str(media, (pj_str_t*)&stream->desc.media, sizeof(media)); |
| 375 |
ast_rtp_codecs_payloads_set_rtpmap_type_rate(codecs, NULL, |
| 376 |
pj_strtoul(&stream->desc.fmt[i]), media, name, options, rtpmap->clock_rate); |
| 377 |
|
| 378 |
if (!(attr = pjmedia_sdp_media_find_attr2(stream, "fmtp", &rtpmap->pt))) { |
| 379 |
continue; |
| 380 |
} |
| 381 |
|
| 382 |
if ((pjmedia_sdp_attr_get_fmtp(attr, &fmtp)) == PJ_SUCCESS) { |
| 383 |
ast_copy_pj_str(fmt_param, &fmtp.fmt, sizeof(fmt_param)); |
| 384 |
if (sscanf(fmt_param, "%30d", &num) != 1) { |
| 385 |
continue; |
| 386 |
} |
| 387 |
|
| 388 |
if ((format = ast_rtp_codecs_get_payload_format(codecs, num))) { |
| 389 |
struct ast_format *format_parsed; |
| 390 |
|
| 391 |
ast_copy_pj_str(fmt_param, &fmtp.fmt_param, sizeof(fmt_param)); |
| 392 |
|
| 393 |
format_parsed = ast_format_parse_sdp_fmtp(format, fmt_param); |
| 394 |
if (format_parsed) { |
| 395 |
ast_rtp_codecs_payload_replace_format(codecs, num, format_parsed); |
| 396 |
ao2_ref(format_parsed, -1); |
| 397 |
} |
| 398 |
ao2_ref(format, -1); |
| 399 |
} |
| 400 |
} |
| 401 |
} |
| 402 |
|
| 403 |
|
| 404 |
for (i = 0; i < stream->desc.fmt_count; ++i) { |
| 405 |
if ((format = ast_rtp_codecs_get_payload_format(codecs, pj_strtoul(&stream->desc.fmt[i])))) { |
| 406 |
ast_format_cap_append(astformats, format, 0); |
| 407 |
ao2_ref(format, -1); |
| 408 |
} |
| 409 |
} |
| 410 |
|
| 411 |
if (session->dtmf == AST_SIP_DTMF_AUTO) { |
| 412 |
if (tel_event) { |
| 413 |
ast_rtp_instance_dtmf_mode_set(session_media->rtp, AST_RTP_DTMF_MODE_RFC2833); |
| 414 |
ast_rtp_instance_set_prop(session_media->rtp, AST_RTP_PROPERTY_DTMF, 1); |
| 415 |
} else { |
| 416 |
ast_rtp_instance_dtmf_mode_set(session_media->rtp, AST_RTP_DTMF_MODE_INBAND); |
| 417 |
ast_rtp_instance_set_prop(session_media->rtp, AST_RTP_PROPERTY_DTMF, 0); |
| 418 |
} |
| 419 |
} |
| 420 |
|
| 421 |
if (session->dtmf == AST_SIP_DTMF_AUTO_INFO) { |
| 422 |
if (tel_event) { |
| 423 |
ast_rtp_instance_dtmf_mode_set(session_media->rtp, AST_RTP_DTMF_MODE_RFC2833); |
| 424 |
ast_rtp_instance_set_prop(session_media->rtp, AST_RTP_PROPERTY_DTMF, 1); |
| 425 |
} else { |
| 426 |
ast_rtp_instance_dtmf_mode_set(session_media->rtp, AST_RTP_DTMF_MODE_NONE); |
| 427 |
ast_rtp_instance_set_prop(session_media->rtp, AST_RTP_PROPERTY_DTMF, 0); |
| 428 |
} |
| 429 |
} |
| 430 |
|
| 431 |
|
| 432 |
|
| 433 |
if ((attr = pjmedia_sdp_media_find_attr2(stream, "ptime", NULL))) { |
| 434 |
unsigned long framing = pj_strtoul(pj_strltrim(&attr->value)); |
| 435 |
if (framing && session->endpoint->media.rtp.use_ptime) { |
| 436 |
ast_rtp_codecs_set_framing(codecs, framing); |
| 437 |
ast_format_cap_set_framing(astformats, framing); |
| 438 |
} |
| 439 |
} |
| 440 |
|
| 441 |
SCOPE_EXIT_RTN(); |
| 442 |
} |
| 443 |
|
| 444 |
static int apply_cap_to_bundled(struct ast_sip_session_media *session_media, |
| 445 |
struct ast_sip_session_media *session_media_transport, |
| 446 |
struct ast_stream *asterisk_stream, struct ast_format_cap *joint) |
| 447 |
{ |
| 448 |
if (!joint) { |
| 449 |
return -1; |
| 450 |
} |
| 451 |
|
| 452 |
ast_stream_set_formats(asterisk_stream, joint); |
| 453 |
|
| 454 |
|
| 455 |
if (session_media_transport != session_media && session_media->bundled) { |
| 456 |
int index; |
| 457 |
|
| 458 |
for (index = 0; index < ast_format_cap_count(joint); ++index) { |
| 459 |
struct ast_format *format = ast_format_cap_get_format(joint, index); |
| 460 |
int rtp_code; |
| 461 |
|
| 462 |
|
| 463 |
|
| 464 |
|
| 465 |
rtp_code = ast_rtp_codecs_payload_code(ast_rtp_instance_get_codecs(session_media->rtp), 1, format, 0); |
| 466 |
ast_rtp_codecs_payload_set_rx(ast_rtp_instance_get_codecs(session_media_transport->rtp), rtp_code, format); |
| 467 |
|
| 468 |
ao2_ref(format, -1); |
| 469 |
} |
| 470 |
} |
| 471 |
|
| 472 |
return 0; |
| 473 |
} |
| 474 |
|
| 475 |
static struct ast_format_cap *set_incoming_call_offer_cap( |
| 476 |
struct ast_sip_session *session, struct ast_sip_session_media *session_media, |
| 477 |
const struct pjmedia_sdp_media *stream) |
| 478 |
{ |
| 479 |
struct ast_format_cap *incoming_call_offer_cap; |
| 480 |
struct ast_format_cap *remote; |
| 481 |
struct ast_rtp_codecs codecs = AST_RTP_CODECS_NULL_INIT; |
| 482 |
SCOPE_ENTER(1, "%s\n", ast_sip_session_get_name(session)); |
| 483 |
|
| 484 |
|
| 485 |
remote = ast_format_cap_alloc(AST_FORMAT_CAP_FLAG_DEFAULT); |
| 486 |
if (!remote) { |
| 487 |
ast_log(LOG_ERROR, "Failed to allocate %s incoming remote capabilities\n", |
| 488 |
ast_codec_media_type2str(session_media->type)); |
| 489 |
SCOPE_EXIT_RTN_VALUE(NULL, "Couldn't allocate caps\n"); |
| 490 |
} |
| 491 |
|
| 492 |
|
| 493 |
get_codecs(session, stream, &codecs, session_media, remote); |
| 494 |
|
| 495 |
incoming_call_offer_cap = ast_sip_session_create_joint_call_cap( |
| 496 |
session, session_media->type, remote); |
| 497 |
|
| 498 |
ao2_ref(remote, -1); |
| 499 |
|
| 500 |
if (!incoming_call_offer_cap || ast_format_cap_empty(incoming_call_offer_cap)) { |
| 501 |
ao2_cleanup(incoming_call_offer_cap); |
| 502 |
ast_rtp_codecs_payloads_destroy(&codecs); |
| 503 |
SCOPE_EXIT_RTN_VALUE(NULL, "No incoming call offer caps\n"); |
| 504 |
} |
| 505 |
|
| 506 |
|
| 507 |
|
| 508 |
|
| 509 |
|
| 510 |
ast_rtp_codecs_payloads_xover(&codecs, &codecs, NULL); |
| 511 |
|
| 512 |
ast_rtp_codecs_payloads_copy(&codecs, |
| 513 |
ast_rtp_instance_get_codecs(session_media->rtp), session_media->rtp); |
| 514 |
|
| 515 |
ast_rtp_codecs_payloads_destroy(&codecs); |
| 516 |
|
| 517 |
SCOPE_EXIT_RTN_VALUE(incoming_call_offer_cap); |
| 518 |
} |
| 519 |
|
| 520 |
static int set_caps(struct ast_sip_session *session, |
| 521 |
struct ast_sip_session_media *session_media, |
| 522 |
struct ast_sip_session_media *session_media_transport, |
| 523 |
const struct pjmedia_sdp_media *stream, |
| 524 |
int is_offer, struct ast_stream *asterisk_stream) |
| 525 |
{ |
| 526 |
RAII_VAR(struct ast_format_cap *, caps, NULL, ao2_cleanup); |
| 527 |
RAII_VAR(struct ast_format_cap *, peer, NULL, ao2_cleanup); |
| 528 |
RAII_VAR(struct ast_format_cap *, joint, NULL, ao2_cleanup); |
| 529 |
enum ast_media_type media_type = session_media->type; |
| 530 |
struct ast_rtp_codecs codecs = AST_RTP_CODECS_NULL_INIT; |
| 531 |
int direct_media_enabled = !ast_sockaddr_isnull(&session_media->direct_media_addr) && |
| 532 |
ast_format_cap_count(session->direct_media_cap); |
| 533 |
int dsp_features = 0; |
| 534 |
SCOPE_ENTER(1, "%s %s\n", ast_sip_session_get_name(session), is_offer ? "OFFER" : "ANSWER"); |
| 535 |
|
| 536 |
if (!(caps = ast_format_cap_alloc(AST_FORMAT_CAP_FLAG_DEFAULT)) || |
| 537 |
!(peer = ast_format_cap_alloc(AST_FORMAT_CAP_FLAG_DEFAULT)) || |
| 538 |
!(joint = ast_format_cap_alloc(AST_FORMAT_CAP_FLAG_DEFAULT))) { |
| 539 |
ast_log(LOG_ERROR, "Failed to allocate %s capabilities\n", |
| 540 |
ast_codec_media_type2str(session_media->type)); |
| 541 |
SCOPE_EXIT_RTN_VALUE(-1, "Couldn't create %s capabilities\n", |
| 542 |
ast_codec_media_type2str(session_media->type)); |
| 543 |
} |
| 544 |
|
| 545 |
|
| 546 |
if (direct_media_enabled) { |
| 547 |
ast_format_cap_get_compatible(session->endpoint->media.codecs, session->direct_media_cap, caps); |
| 548 |
} else { |
| 549 |
ast_format_cap_append_from_cap(caps, session->endpoint->media.codecs, media_type); |
| 550 |
} |
| 551 |
|
| 552 |
|
| 553 |
get_codecs(session, stream, &codecs, session_media, peer); |
| 554 |
|
| 555 |
|
| 556 |
ast_format_cap_get_compatible(caps, peer, joint); |
| 557 |
if (!ast_format_cap_count(joint)) { |
| 558 |
struct ast_str *usbuf = ast_str_alloca(AST_FORMAT_CAP_NAMES_LEN); |
| 559 |
struct ast_str *thembuf = ast_str_alloca(AST_FORMAT_CAP_NAMES_LEN); |
| 560 |
|
| 561 |
ast_rtp_codecs_payloads_destroy(&codecs); |
| 562 |
ast_log(LOG_NOTICE, "No joint capabilities for '%s' media stream between our configuration(%s) and incoming SDP(%s)\n", |
| 563 |
ast_codec_media_type2str(session_media->type), |
| 564 |
ast_format_cap_get_names(caps, &usbuf), |
| 565 |
ast_format_cap_get_names(peer, &thembuf)); |
| 566 |
SCOPE_EXIT_RTN_VALUE(-1, "No joint capabilities for '%s' media stream between our configuration(%s) and incoming SDP(%s)\n", |
| 567 |
ast_codec_media_type2str(session_media->type), |
| 568 |
ast_format_cap_get_names(caps, &usbuf), |
| 569 |
ast_format_cap_get_names(peer, &thembuf)); |
| 570 |
} else { |
| 571 |
struct ast_format *preferred_fmt = ast_format_cap_get_format(joint, 0); |
| 572 |
|
| 573 |
ast_rtp_codecs_set_preferred_format(&codecs, preferred_fmt); |
| 574 |
ao2_ref(preferred_fmt, -1); |
| 575 |
} |
| 576 |
|
| 577 |
if (is_offer) { |
| 578 |
|
| 579 |
|
| 580 |
|
| 581 |
|
| 582 |
ast_rtp_codecs_payloads_xover(&codecs, &codecs, NULL); |
| 583 |
} |
| 584 |
ast_rtp_codecs_payloads_copy(&codecs, ast_rtp_instance_get_codecs(session_media->rtp), |
| 585 |
session_media->rtp); |
| 586 |
|
| 587 |
apply_cap_to_bundled(session_media, session_media_transport, asterisk_stream, joint); |
| 588 |
|
| 589 |
if (session->channel && ast_sip_session_is_pending_stream_default(session, asterisk_stream)) { |
| 590 |
ast_channel_lock(session->channel); |
| 591 |
ast_format_cap_remove_by_type(caps, AST_MEDIA_TYPE_UNKNOWN); |
| 592 |
ast_format_cap_append_from_cap(caps, ast_channel_nativeformats(session->channel), |
| 593 |
AST_MEDIA_TYPE_UNKNOWN); |
| 594 |
ast_format_cap_remove_by_type(caps, media_type); |
| 595 |
|
| 596 |
if (session->endpoint->preferred_codec_only) { |
| 597 |
struct ast_format *preferred_fmt = ast_format_cap_get_format(joint, 0); |
| 598 |
ast_format_cap_append(caps, preferred_fmt, 0); |
| 599 |
ao2_ref(preferred_fmt, -1); |
| 600 |
} else if (!session->endpoint->asymmetric_rtp_codec) { |
| 601 |
struct ast_format *best; |
| 602 |
|
| 603 |
|
| 604 |
|
| 605 |
|
| 606 |
|
| 607 |
|
| 608 |
|
| 609 |
best = ast_format_cap_get_best_by_type(joint, media_type); |
| 610 |
if (best) { |
| 611 |
ast_format_cap_append(caps, best, ast_format_cap_get_framing(joint)); |
| 612 |
ao2_ref(best, -1); |
| 613 |
} |
| 614 |
} else { |
| 615 |
ast_format_cap_append_from_cap(caps, joint, media_type); |
| 616 |
} |
| 617 |
|
| 618 |
|
| 619 |
|
| 620 |
|
| 621 |
|
| 622 |
ast_channel_nativeformats_set(session->channel, caps); |
| 623 |
if (media_type == AST_MEDIA_TYPE_AUDIO) { |
| 624 |
ast_set_read_format(session->channel, ast_channel_readformat(session->channel)); |
| 625 |
ast_set_write_format(session->channel, ast_channel_writeformat(session->channel)); |
| 626 |
} |
| 627 |
|
| 628 |
if ( ((session->dtmf == AST_SIP_DTMF_AUTO) || (session->dtmf == AST_SIP_DTMF_AUTO_INFO) ) |
| 629 |
&& (ast_rtp_instance_dtmf_mode_get(session_media->rtp) == AST_RTP_DTMF_MODE_RFC2833) |
| 630 |
&& (session->dsp)) { |
| 631 |
dsp_features = ast_dsp_get_features(session->dsp); |
| 632 |
dsp_features &= ~DSP_FEATURE_DIGIT_DETECT; |
| 633 |
if (dsp_features) { |
| 634 |
ast_dsp_set_features(session->dsp, dsp_features); |
| 635 |
} else { |
| 636 |
ast_dsp_free(session->dsp); |
| 637 |
session->dsp = NULL; |
| 638 |
} |
| 639 |
} |
| 640 |
|
| 641 |
if (ast_channel_is_bridged(session->channel)) { |
| 642 |
ast_channel_set_unbridged_nolock(session->channel, 1); |
| 643 |
} |
| 644 |
|
| 645 |
ast_channel_unlock(session->channel); |
| 646 |
} |
| 647 |
|
| 648 |
ast_rtp_codecs_payloads_destroy(&codecs); |
| 649 |
SCOPE_EXIT_RTN_VALUE(0); |
| 650 |
} |
| 651 |
|
| 652 |
static pjmedia_sdp_attr* generate_rtpmap_attr(struct ast_sip_session *session, pjmedia_sdp_media *media, pj_pool_t *pool, |
| 653 |
int rtp_code, int asterisk_format, struct ast_format *format, int code) |
| 654 |
{ |
| 655 |
#ifndef HAVE_PJSIP_ENDPOINT_COMPACT_FORM |
| 656 |
extern pj_bool_t pjsip_use_compact_form; |
| 657 |
#else |
| 658 |
pj_bool_t pjsip_use_compact_form = pjsip_cfg()->endpt.use_compact_form; |
| 659 |
#endif |
| 660 |
pjmedia_sdp_rtpmap rtpmap; |
| 661 |
pjmedia_sdp_attr *attr = NULL; |
| 662 |
char tmp[64]; |
| 663 |
enum ast_rtp_options options = session->endpoint->media.g726_non_standard ? |
| 664 |
AST_RTP_OPT_G726_NONSTANDARD : 0; |
| 665 |
|
| 666 |
snprintf(tmp, sizeof(tmp), "%d", rtp_code); |
| 667 |
pj_strdup2(pool, &media->desc.fmt[media->desc.fmt_count++], tmp); |
| 668 |
|
| 669 |
if (rtp_code <= AST_RTP_PT_LAST_STATIC && pjsip_use_compact_form) { |
| 670 |
return NULL; |
| 671 |
} |
| 672 |
|
| 673 |
rtpmap.pt = media->desc.fmt[media->desc.fmt_count - 1]; |
| 674 |
rtpmap.clock_rate = ast_rtp_lookup_sample_rate2(asterisk_format, format, code); |
| 675 |
pj_strdup2(pool, &rtpmap.enc_name, ast_rtp_lookup_mime_subtype2(asterisk_format, format, code, options)); |
| 676 |
if (!pj_stricmp2(&rtpmap.enc_name, "opus")) { |
| 677 |
pj_cstr(&rtpmap.param, "2"); |
| 678 |
} else { |
| 679 |
pj_cstr(&rtpmap.param, NULL); |
| 680 |
} |
| 681 |
|
| 682 |
pjmedia_sdp_rtpmap_to_attr(pool, &rtpmap, &attr); |
| 683 |
|
| 684 |
return attr; |
| 685 |
} |
| 686 |
|
| 687 |
|
| 688 |
static pjmedia_sdp_attr* generate_rtpmap_attr2(struct ast_sip_session *session, pjmedia_sdp_media *media, pj_pool_t *pool, |
| 689 |
int rtp_code, int asterisk_format, struct ast_format *format, int code, int sample_rate) |
| 690 |
{ |
| 691 |
#ifndef HAVE_PJSIP_ENDPOINT_COMPACT_FORM |
| 692 |
extern pj_bool_t pjsip_use_compact_form; |
| 693 |
#else |
| 694 |
pj_bool_t pjsip_use_compact_form = pjsip_cfg()->endpt.use_compact_form; |
| 695 |
#endif |
| 696 |
pjmedia_sdp_rtpmap rtpmap; |
| 697 |
pjmedia_sdp_attr *attr = NULL; |
| 698 |
char tmp[64]; |
| 699 |
enum ast_rtp_options options = session->endpoint->media.g726_non_standard ? |
| 700 |
AST_RTP_OPT_G726_NONSTANDARD : 0; |
| 701 |
|
| 702 |
snprintf(tmp, sizeof(tmp), "%d", rtp_code); |
| 703 |
pj_strdup2(pool, &media->desc.fmt[media->desc.fmt_count++], tmp); |
| 704 |
|
| 705 |
if (rtp_code <= AST_RTP_PT_LAST_STATIC && pjsip_use_compact_form) { |
| 706 |
return NULL; |
| 707 |
} |
| 708 |
|
| 709 |
rtpmap.pt = media->desc.fmt[media->desc.fmt_count - 1]; |
| 710 |
rtpmap.clock_rate = sample_rate; |
| 711 |
pj_strdup2(pool, &rtpmap.enc_name, ast_rtp_lookup_mime_subtype2(asterisk_format, format, code, options)); |
| 712 |
if (!pj_stricmp2(&rtpmap.enc_name, "opus")) { |
| 713 |
pj_cstr(&rtpmap.param, "2"); |
| 714 |
} else { |
| 715 |
pj_cstr(&rtpmap.param, NULL); |
| 716 |
} |
| 717 |
|
| 718 |
pjmedia_sdp_rtpmap_to_attr(pool, &rtpmap, &attr); |
| 719 |
|
| 720 |
return attr; |
| 721 |
} |
| 722 |
|
| 723 |
static pjmedia_sdp_attr* generate_fmtp_attr(pj_pool_t *pool, struct ast_format *format, int rtp_code) |
| 724 |
{ |
| 725 |
struct ast_str *fmtp0 = ast_str_alloca(256); |
| 726 |
pj_str_t fmtp1; |
| 727 |
pjmedia_sdp_attr *attr = NULL; |
| 728 |
char *tmp; |
| 729 |
|
| 730 |
ast_format_generate_sdp_fmtp(format, rtp_code, &fmtp0); |
| 731 |
if (ast_str_strlen(fmtp0)) { |
| 732 |
tmp = ast_str_buffer(fmtp0) + ast_str_strlen(fmtp0) - 1; |
| 733 |
|
| 734 |
while (*tmp == '\r' || *tmp == '\n') --tmp; |
| 735 |
*++tmp = '\0'; |
| 736 |
|
| 737 |
tmp = strchr(ast_str_buffer(fmtp0), ':'); |
| 738 |
if (tmp && tmp[1] != '\0') { |
| 739 |
fmtp1 = pj_str(tmp + 1); |
| 740 |
} else { |
| 741 |
fmtp1 = pj_str(ast_str_buffer(fmtp0)); |
| 742 |
} |
| 743 |
attr = pjmedia_sdp_attr_create(pool, "fmtp", &fmtp1); |
| 744 |
} |
| 745 |
return attr; |
| 746 |
} |
| 747 |
|
| 748 |
|
| 749 |
static void add_ice_to_stream(struct ast_sip_session *session, struct ast_sip_session_media *session_media, pj_pool_t *pool, pjmedia_sdp_media *media, |
| 750 |
unsigned int include_candidates) |
| 751 |
{ |
| 752 |
struct ast_rtp_engine_ice *ice; |
| 753 |
struct ao2_container *candidates; |
| 754 |
const char *username, *password; |
| 755 |
pj_str_t stmp; |
| 756 |
pjmedia_sdp_attr *attr; |
| 757 |
struct ao2_iterator it_candidates; |
| 758 |
struct ast_rtp_engine_ice_candidate *candidate; |
| 759 |
|
| 760 |
if (!session->endpoint->media.rtp.ice_support || !(ice = ast_rtp_instance_get_ice(session_media->rtp))) { |
| 761 |
return; |
| 762 |
} |
| 763 |
|
| 764 |
if (!session_media->remote_ice) { |
| 765 |
ice->stop(session_media->rtp); |
| 766 |
return; |
| 767 |
} |
| 768 |
|
| 769 |
if ((username = ice->get_ufrag(session_media->rtp))) { |
| 770 |
attr = pjmedia_sdp_attr_create(pool, "ice-ufrag", pj_cstr(&stmp, username)); |
| 771 |
media->attr[media->attr_count++] = attr; |
| 772 |
} |
| 773 |
|
| 774 |
if ((password = ice->get_password(session_media->rtp))) { |
| 775 |
attr = pjmedia_sdp_attr_create(pool, "ice-pwd", pj_cstr(&stmp, password)); |
| 776 |
media->attr[media->attr_count++] = attr; |
| 777 |
} |
| 778 |
|
| 779 |
if (!include_candidates) { |
| 780 |
return; |
| 781 |
} |
| 782 |
|
| 783 |
candidates = ice->get_local_candidates(session_media->rtp); |
| 784 |
if (!candidates) { |
| 785 |
return; |
| 786 |
} |
| 787 |
|
| 788 |
it_candidates = ao2_iterator_init(candidates, 0); |
| 789 |
for (; (candidate = ao2_iterator_next(&it_candidates)); ao2_ref(candidate, -1)) { |
| 790 |
struct ast_str *attr_candidate = ast_str_create(128); |
| 791 |
|
| 792 |
ast_str_set(&attr_candidate, -1, "%s %u %s %d %s ", candidate->foundation, candidate->id, candidate->transport, |
| 793 |
candidate->priority, ast_sockaddr_stringify_addr_remote(&candidate->address)); |
| 794 |
ast_str_append(&attr_candidate, -1, "%s typ ", ast_sockaddr_stringify_port(&candidate->address)); |
| 795 |
|
| 796 |
switch (candidate->type) { |
| 797 |
case AST_RTP_ICE_CANDIDATE_TYPE_HOST: |
| 798 |
ast_str_append(&attr_candidate, -1, "host"); |
| 799 |
break; |
| 800 |
case AST_RTP_ICE_CANDIDATE_TYPE_SRFLX: |
| 801 |
ast_str_append(&attr_candidate, -1, "srflx"); |
| 802 |
break; |
| 803 |
case AST_RTP_ICE_CANDIDATE_TYPE_RELAYED: |
| 804 |
ast_str_append(&attr_candidate, -1, "relay"); |
| 805 |
break; |
| 806 |
} |
| 807 |
|
| 808 |
if (!ast_sockaddr_isnull(&candidate->relay_address)) { |
| 809 |
ast_str_append(&attr_candidate, -1, " raddr %s rport", ast_sockaddr_stringify_addr_remote(&candidate->relay_address)); |
| 810 |
ast_str_append(&attr_candidate, -1, " %s", ast_sockaddr_stringify_port(&candidate->relay_address)); |
| 811 |
} |
| 812 |
|
| 813 |
attr = pjmedia_sdp_attr_create(pool, "candidate", pj_cstr(&stmp, ast_str_buffer(attr_candidate))); |
| 814 |
media->attr[media->attr_count++] = attr; |
| 815 |
|
| 816 |
ast_free(attr_candidate); |
| 817 |
} |
| 818 |
|
| 819 |
ao2_iterator_destroy(&it_candidates); |
| 820 |
ao2_ref(candidates, -1); |
| 821 |
} |
| 822 |
|
| 823 |
|
| 824 |
static void check_ice_support(struct ast_sip_session *session, struct ast_sip_session_media *session_media, |
| 825 |
const struct pjmedia_sdp_media *remote_stream) |
| 826 |
{ |
| 827 |
struct ast_rtp_engine_ice *ice; |
| 828 |
const pjmedia_sdp_attr *attr; |
| 829 |
unsigned int attr_i; |
| 830 |
|
| 831 |
if (!session->endpoint->media.rtp.ice_support || !(ice = ast_rtp_instance_get_ice(session_media->rtp))) { |
| 832 |
session_media->remote_ice = 0; |
| 833 |
return; |
| 834 |
} |
| 835 |
|
| 836 |
|
| 837 |
for (attr_i = 0; attr_i < remote_stream->attr_count; ++attr_i) { |
| 838 |
attr = remote_stream->attr[attr_i]; |
| 839 |
if (!pj_strcmp2(&attr->name, "candidate")) { |
| 840 |
session_media->remote_ice = 1; |
| 841 |
break; |
| 842 |
} |
| 843 |
} |
| 844 |
|
| 845 |
if (attr_i == remote_stream->attr_count) { |
| 846 |
session_media->remote_ice = 0; |
| 847 |
} |
| 848 |
} |
| 849 |
|
| 850 |
static void process_ice_auth_attrb(struct ast_sip_session *session, struct ast_sip_session_media *session_media, |
| 851 |
const struct pjmedia_sdp_session *remote, const struct pjmedia_sdp_media *remote_stream) |
| 852 |
{ |
| 853 |
struct ast_rtp_engine_ice *ice; |
| 854 |
const pjmedia_sdp_attr *ufrag_attr, *passwd_attr; |
| 855 |
char ufrag_attr_value[256]; |
| 856 |
char passwd_attr_value[256]; |
| 857 |
|
| 858 |
|
| 859 |
if (!session->endpoint->media.rtp.ice_support || !(ice = ast_rtp_instance_get_ice(session_media->rtp))) { |
| 860 |
return; |
| 861 |
} |
| 862 |
|
| 863 |
ufrag_attr = pjmedia_sdp_media_find_attr2(remote_stream, "ice-ufrag", NULL); |
| 864 |
if (!ufrag_attr) { |
| 865 |
ufrag_attr = pjmedia_sdp_attr_find2(remote->attr_count, remote->attr, "ice-ufrag", NULL); |
| 866 |
} |
| 867 |
if (ufrag_attr) { |
| 868 |
ast_copy_pj_str(ufrag_attr_value, (pj_str_t*)&ufrag_attr->value, sizeof(ufrag_attr_value)); |
| 869 |
} else { |
| 870 |
return; |
| 871 |
} |
| 872 |
passwd_attr = pjmedia_sdp_media_find_attr2(remote_stream, "ice-pwd", NULL); |
| 873 |
if (!passwd_attr) { |
| 874 |
passwd_attr = pjmedia_sdp_attr_find2(remote->attr_count, remote->attr, "ice-pwd", NULL); |
| 875 |
} |
| 876 |
if (passwd_attr) { |
| 877 |
ast_copy_pj_str(passwd_attr_value, (pj_str_t*)&passwd_attr->value, sizeof(passwd_attr_value)); |
| 878 |
} else { |
| 879 |
return; |
| 880 |
} |
| 881 |
|
| 882 |
if (ufrag_attr && passwd_attr) { |
| 883 |
ice->set_authentication(session_media->rtp, ufrag_attr_value, passwd_attr_value); |
| 884 |
} |
| 885 |
} |
| 886 |
|
| 887 |
|
| 888 |
static void process_ice_attributes(struct ast_sip_session *session, struct ast_sip_session_media *session_media, |
| 889 |
const struct pjmedia_sdp_session *remote, const struct pjmedia_sdp_media *remote_stream) |
| 890 |
{ |
| 891 |
struct ast_rtp_engine_ice *ice; |
| 892 |
const pjmedia_sdp_attr *attr; |
| 893 |
char attr_value[256]; |
| 894 |
unsigned int attr_i; |
| 895 |
|
| 896 |
|
| 897 |
if (!session->endpoint->media.rtp.ice_support || !(ice = ast_rtp_instance_get_ice(session_media->rtp))) { |
| 898 |
return; |
| 899 |
} |
| 900 |
|
| 901 |
ast_debug_ice(2, "(%p) ICE process attributes\n", session_media->rtp); |
| 902 |
|
| 903 |
attr = pjmedia_sdp_media_find_attr2(remote_stream, "ice-ufrag", NULL); |
| 904 |
if (!attr) { |
| 905 |
attr = pjmedia_sdp_attr_find2(remote->attr_count, remote->attr, "ice-ufrag", NULL); |
| 906 |
} |
| 907 |
if (attr) { |
| 908 |
ast_copy_pj_str(attr_value, (pj_str_t*)&attr->value, sizeof(attr_value)); |
| 909 |
ice->set_authentication(session_media->rtp, attr_value, NULL); |
| 910 |
} else { |
| 911 |
ast_debug_ice(2, "(%p) ICE no, or invalid ice-ufrag\n", session_media->rtp); |
| 912 |
return; |
| 913 |
} |
| 914 |
|
| 915 |
attr = pjmedia_sdp_media_find_attr2(remote_stream, "ice-pwd", NULL); |
| 916 |
if (!attr) { |
| 917 |
attr = pjmedia_sdp_attr_find2(remote->attr_count, remote->attr, "ice-pwd", NULL); |
| 918 |
} |
| 919 |
if (attr) { |
| 920 |
ast_copy_pj_str(attr_value, (pj_str_t*)&attr->value, sizeof(attr_value)); |
| 921 |
ice->set_authentication(session_media->rtp, NULL, attr_value); |
| 922 |
} else { |
| 923 |
ast_debug_ice(2, "(%p) ICE no, or invalid ice-pwd\n", session_media->rtp); |
| 924 |
return; |
| 925 |
} |
| 926 |
|
| 927 |
if (pjmedia_sdp_media_find_attr2(remote_stream, "ice-lite", NULL)) { |
| 928 |
ice->ice_lite(session_media->rtp); |
| 929 |
} |
| 930 |
|
| 931 |
|
| 932 |
for (attr_i = 0; attr_i < remote_stream->attr_count; ++attr_i) { |
| 933 |
char foundation[33], transport[32], address[PJ_INET6_ADDRSTRLEN + 1], cand_type[6], relay_address[PJ_INET6_ADDRSTRLEN + 1] = ""; |
| 934 |
unsigned int port, relay_port = 0; |
| 935 |
struct ast_rtp_engine_ice_candidate candidate = { 0, }; |
| 936 |
|
| 937 |
attr = remote_stream->attr[attr_i]; |
| 938 |
|
| 939 |
|
| 940 |
if (pj_strcmp2(&attr->name, "candidate")) { |
| 941 |
continue; |
| 942 |
} |
| 943 |
|
| 944 |
ast_copy_pj_str(attr_value, (pj_str_t*)&attr->value, sizeof(attr_value)); |
| 945 |
|
| 946 |
if (sscanf(attr_value, "%32s %30u %31s %30u %46s %30u typ %5s %*s %23s %*s %30u", foundation, &candidate.id, transport, |
| 947 |
(unsigned *)&candidate.priority, address, &port, cand_type, relay_address, &relay_port) < 7) { |
| 948 |
|
| 949 |
continue; |
| 950 |
} |
| 951 |
|
| 952 |
if (session->endpoint->media.rtcp_mux && session_media->remote_rtcp_mux && candidate.id > 1) { |
| 953 |
|
| 954 |
|
| 955 |
|
| 956 |
continue; |
| 957 |
} |
| 958 |
|
| 959 |
candidate.foundation = foundation; |
| 960 |
candidate.transport = transport; |
| 961 |
|
| 962 |
ast_sockaddr_parse(&candidate.address, address, PARSE_PORT_FORBID); |
| 963 |
ast_sockaddr_set_port(&candidate.address, port); |
| 964 |
|
| 965 |
if (!strcasecmp(cand_type, "host")) { |
| 966 |
candidate.type = AST_RTP_ICE_CANDIDATE_TYPE_HOST; |
| 967 |
} else if (!strcasecmp(cand_type, "srflx")) { |
| 968 |
candidate.type = AST_RTP_ICE_CANDIDATE_TYPE_SRFLX; |
| 969 |
} else if (!strcasecmp(cand_type, "relay")) { |
| 970 |
candidate.type = AST_RTP_ICE_CANDIDATE_TYPE_RELAYED; |
| 971 |
} else { |
| 972 |
continue; |
| 973 |
} |
| 974 |
|
| 975 |
if (!ast_strlen_zero(relay_address)) { |
| 976 |
ast_sockaddr_parse(&candidate.relay_address, relay_address, PARSE_PORT_FORBID); |
| 977 |
} |
| 978 |
|
| 979 |
if (relay_port) { |
| 980 |
ast_sockaddr_set_port(&candidate.relay_address, relay_port); |
| 981 |
} |
| 982 |
|
| 983 |
ice->add_remote_candidate(session_media->rtp, &candidate); |
| 984 |
} |
| 985 |
|
| 986 |
ice->set_role(session_media->rtp, pjmedia_sdp_neg_was_answer_remote(session->inv_session->neg) == PJ_TRUE ? |
| 987 |
AST_RTP_ICE_ROLE_CONTROLLING : AST_RTP_ICE_ROLE_CONTROLLED); |
| 988 |
ice->start(session_media->rtp); |
| 989 |
} |
| 990 |
|
| 991 |
|
| 992 |
static int media_stream_has_crypto(const struct pjmedia_sdp_media *stream) |
| 993 |
{ |
| 994 |
int i; |
| 995 |
|
| 996 |
for (i = 0; i < stream->attr_count; i++) { |
| 997 |
pjmedia_sdp_attr *attr; |
| 998 |
|
| 999 |
|
| 1000 |
attr = stream->attr[i]; |
| 1001 |
if (pj_strcmp2(&attr->name, "crypto")) { |
| 1002 |
continue; |
| 1003 |
} |
| 1004 |
|
| 1005 |
return 1; |
| 1006 |
} |
| 1007 |
|
| 1008 |
return 0; |
| 1009 |
} |
| 1010 |
|
| 1011 |
|
| 1012 |
static enum ast_sip_session_media_encryption get_media_encryption_type(pj_str_t transport, |
| 1013 |
const struct pjmedia_sdp_media *stream, unsigned int *optimistic) |
| 1014 |
{ |
| 1015 |
RAII_VAR(char *, transport_str, ast_strndup(transport.ptr, transport.slen), ast_free); |
| 1016 |
|
| 1017 |
*optimistic = 0; |
| 1018 |
|
| 1019 |
if (!transport_str) { |
| 1020 |
return AST_SIP_MEDIA_TRANSPORT_INVALID; |
| 1021 |
} |
| 1022 |
if (strstr(transport_str, "UDP/TLS")) { |
| 1023 |
return AST_SIP_MEDIA_ENCRYPT_DTLS; |
| 1024 |
} else if (strstr(transport_str, "SAVP")) { |
| 1025 |
return AST_SIP_MEDIA_ENCRYPT_SDES; |
| 1026 |
} else if (media_stream_has_crypto(stream)) { |
| 1027 |
*optimistic = 1; |
| 1028 |
return AST_SIP_MEDIA_ENCRYPT_SDES; |
| 1029 |
} else { |
| 1030 |
return AST_SIP_MEDIA_ENCRYPT_NONE; |
| 1031 |
} |
| 1032 |
} |
| 1033 |
|
| 1034 |
|
| 1035 |
|
| 1036 |
|
| 1037 |
|
| 1038 |
|
| 1039 |
|
| 1040 |
|
| 1041 |
|
| 1042 |
|
| 1043 |
|
| 1044 |
static enum ast_sip_session_media_encryption check_endpoint_media_transport( |
| 1045 |
struct ast_sip_endpoint *endpoint, |
| 1046 |
const struct pjmedia_sdp_media *stream) |
| 1047 |
{ |
| 1048 |
enum ast_sip_session_media_encryption incoming_encryption; |
| 1049 |
char transport_end = stream->desc.transport.ptr[stream->desc.transport.slen - 1]; |
| 1050 |
unsigned int optimistic; |
| 1051 |
|
| 1052 |
if ((transport_end == 'F' && !endpoint->media.rtp.use_avpf) |
| 1053 |
|| (transport_end != 'F' && endpoint->media.rtp.use_avpf)) { |
| 1054 |
return AST_SIP_MEDIA_TRANSPORT_INVALID; |
| 1055 |
} |
| 1056 |
|
| 1057 |
incoming_encryption = get_media_encryption_type(stream->desc.transport, stream, &optimistic); |
| 1058 |
|
| 1059 |
if (incoming_encryption == endpoint->media.rtp.encryption) { |
| 1060 |
return incoming_encryption; |
| 1061 |
} |
| 1062 |
|
| 1063 |
if (endpoint->media.rtp.force_avp || |
| 1064 |
endpoint->media.rtp.encryption_optimistic) { |
| 1065 |
return incoming_encryption; |
| 1066 |
} |
| 1067 |
|
| 1068 |
|
| 1069 |
|
| 1070 |
|
| 1071 |
if (optimistic) { |
| 1072 |
return AST_SIP_MEDIA_ENCRYPT_NONE; |
| 1073 |
} |
| 1074 |
|
| 1075 |
return AST_SIP_MEDIA_TRANSPORT_INVALID; |
| 1076 |
} |
| 1077 |
|
| 1078 |
static int setup_srtp(struct ast_sip_session_media *session_media) |
| 1079 |
{ |
| 1080 |
if (!session_media->srtp) { |
| 1081 |
session_media->srtp = ast_sdp_srtp_alloc(); |
| 1082 |
if (!session_media->srtp) { |
| 1083 |
return -1; |
| 1084 |
} |
| 1085 |
} |
| 1086 |
|
| 1087 |
if (!session_media->srtp->crypto) { |
| 1088 |
session_media->srtp->crypto = ast_sdp_crypto_alloc(); |
| 1089 |
if (!session_media->srtp->crypto) { |
| 1090 |
return -1; |
| 1091 |
} |
| 1092 |
} |
| 1093 |
|
| 1094 |
return 0; |
| 1095 |
} |
| 1096 |
|
| 1097 |
static int setup_dtls_srtp(struct ast_sip_session *session, |
| 1098 |
struct ast_sip_session_media *session_media) |
| 1099 |
{ |
| 1100 |
struct ast_rtp_engine_dtls *dtls; |
| 1101 |
|
| 1102 |
if (!session->endpoint->media.rtp.dtls_cfg.enabled || !session_media->rtp) { |
| 1103 |
return -1; |
| 1104 |
} |
| 1105 |
|
| 1106 |
dtls = ast_rtp_instance_get_dtls(session_media->rtp); |
| 1107 |
if (!dtls) { |
| 1108 |
return -1; |
| 1109 |
} |
| 1110 |
|
| 1111 |
session->endpoint->media.rtp.dtls_cfg.suite = ((session->endpoint->media.rtp.srtp_tag_32) ? AST_AES_CM_128_HMAC_SHA1_32 : AST_AES_CM_128_HMAC_SHA1_80); |
| 1112 |
if (dtls->set_configuration(session_media->rtp, &session->endpoint->media.rtp.dtls_cfg)) { |
| 1113 |
ast_log(LOG_ERROR, "Attempted to set an invalid DTLS-SRTP configuration on RTP instance '%p'\n", |
| 1114 |
session_media->rtp); |
| 1115 |
return -1; |
| 1116 |
} |
| 1117 |
|
| 1118 |
if (setup_srtp(session_media)) { |
| 1119 |
return -1; |
| 1120 |
} |
| 1121 |
return 0; |
| 1122 |
} |
| 1123 |
|
| 1124 |
static void apply_dtls_attrib(struct ast_sip_session_media *session_media, |
| 1125 |
pjmedia_sdp_attr *attr) |
| 1126 |
{ |
| 1127 |
struct ast_rtp_engine_dtls *dtls = ast_rtp_instance_get_dtls(session_media->rtp); |
| 1128 |
pj_str_t *value; |
| 1129 |
|
| 1130 |
if (!attr->value.ptr || !dtls) { |
| 1131 |
return; |
| 1132 |
} |
| 1133 |
|
| 1134 |
value = pj_strtrim(&attr->value); |
| 1135 |
|
| 1136 |
if (!pj_strcmp2(&attr->name, "setup")) { |
| 1137 |
if (!pj_stricmp2(value, "active")) { |
| 1138 |
dtls->set_setup(session_media->rtp, AST_RTP_DTLS_SETUP_ACTIVE); |
| 1139 |
} else if (!pj_stricmp2(value, "passive")) { |
| 1140 |
dtls->set_setup(session_media->rtp, AST_RTP_DTLS_SETUP_PASSIVE); |
| 1141 |
} else if (!pj_stricmp2(value, "actpass")) { |
| 1142 |
dtls->set_setup(session_media->rtp, AST_RTP_DTLS_SETUP_ACTPASS); |
| 1143 |
} else if (!pj_stricmp2(value, "holdconn")) { |
| 1144 |
dtls->set_setup(session_media->rtp, AST_RTP_DTLS_SETUP_HOLDCONN); |
| 1145 |
} else { |
| 1146 |
ast_log(LOG_WARNING, "Unsupported setup attribute value '%*s'\n", (int)value->slen, value->ptr); |
| 1147 |
} |
| 1148 |
} else if (!pj_strcmp2(&attr->name, "connection")) { |
| 1149 |
if (!pj_stricmp2(value, "new")) { |
| 1150 |
dtls->reset(session_media->rtp); |
| 1151 |
} else if (!pj_stricmp2(value, "existing")) { |
| 1152 |
|
| 1153 |
} else { |
| 1154 |
ast_log(LOG_WARNING, "Unsupported connection attribute value '%*s'\n", (int)value->slen, value->ptr); |
| 1155 |
} |
| 1156 |
} else if (!pj_strcmp2(&attr->name, "fingerprint")) { |
| 1157 |
char hash_value[256], hash[32]; |
| 1158 |
char fingerprint_text[value->slen + 1]; |
| 1159 |
ast_copy_pj_str(fingerprint_text, value, sizeof(fingerprint_text)); |
| 1160 |
if (sscanf(fingerprint_text, "%31s %255s", hash, hash_value) == 2) { |
| 1161 |
if (!strcasecmp(hash, "sha-1")) { |
| 1162 |
dtls->set_fingerprint(session_media->rtp, AST_RTP_DTLS_HASH_SHA1, hash_value); |
| 1163 |
} else if (!strcasecmp(hash, "sha-256")) { |
| 1164 |
dtls->set_fingerprint(session_media->rtp, AST_RTP_DTLS_HASH_SHA256, hash_value); |
| 1165 |
} else { |
| 1166 |
ast_log(LOG_WARNING, "Unsupported fingerprint hash type '%s'\n", |
| 1167 |
hash); |
| 1168 |
} |
| 1169 |
} |
| 1170 |
} |
| 1171 |
} |
| 1172 |
|
| 1173 |
static int parse_dtls_attrib(struct ast_sip_session_media *session_media, |
| 1174 |
const struct pjmedia_sdp_session *sdp, |
| 1175 |
const struct pjmedia_sdp_media *stream) |
| 1176 |
{ |
| 1177 |
int i; |
| 1178 |
|
| 1179 |
for (i = 0; i < sdp->attr_count; i++) { |
| 1180 |
apply_dtls_attrib(session_media, sdp->attr[i]); |
| 1181 |
} |
| 1182 |
|
| 1183 |
for (i = 0; i < stream->attr_count; i++) { |
| 1184 |
apply_dtls_attrib(session_media, stream->attr[i]); |
| 1185 |
} |
| 1186 |
|
| 1187 |
ast_set_flag(session_media->srtp, AST_SRTP_CRYPTO_OFFER_OK); |
| 1188 |
|
| 1189 |
return 0; |
| 1190 |
} |
| 1191 |
|
| 1192 |
static int setup_sdes_srtp(struct ast_sip_session_media *session_media, |
| 1193 |
const struct pjmedia_sdp_media *stream) |
| 1194 |
{ |
| 1195 |
int i; |
| 1196 |
|
| 1197 |
for (i = 0; i < stream->attr_count; i++) { |
| 1198 |
pjmedia_sdp_attr *attr; |
| 1199 |
RAII_VAR(char *, crypto_str, NULL, ast_free); |
| 1200 |
|
| 1201 |
|
| 1202 |
attr = stream->attr[i]; |
| 1203 |
if (pj_strcmp2(&attr->name, "crypto")) { |
| 1204 |
continue; |
| 1205 |
} |
| 1206 |
|
| 1207 |
crypto_str = ast_strndup(attr->value.ptr, attr->value.slen); |
| 1208 |
if (!crypto_str) { |
| 1209 |
return -1; |
| 1210 |
} |
| 1211 |
|
| 1212 |
if (setup_srtp(session_media)) { |
| 1213 |
return -1; |
| 1214 |
} |
| 1215 |
|
| 1216 |
if (!ast_sdp_crypto_process(session_media->rtp, session_media->srtp, crypto_str)) { |
| 1217 |
|
| 1218 |
return 0; |
| 1219 |
} |
| 1220 |
|
| 1221 |
ast_debug(1, "Ignoring crypto offer with unsupported parameters: %s\n", crypto_str); |
| 1222 |
} |
| 1223 |
|
| 1224 |
|
| 1225 |
return -1; |
| 1226 |
} |
| 1227 |
|
| 1228 |
static int setup_media_encryption(struct ast_sip_session *session, |
| 1229 |
struct ast_sip_session_media *session_media, |
| 1230 |
const struct pjmedia_sdp_session *sdp, |
| 1231 |
const struct pjmedia_sdp_media *stream) |
| 1232 |
{ |
| 1233 |
switch (session_media->encryption) { |
| 1234 |
case AST_SIP_MEDIA_ENCRYPT_SDES: |
| 1235 |
if (setup_sdes_srtp(session_media, stream)) { |
| 1236 |
return -1; |
| 1237 |
} |
| 1238 |
break; |
| 1239 |
case AST_SIP_MEDIA_ENCRYPT_DTLS: |
| 1240 |
if (setup_dtls_srtp(session, session_media)) { |
| 1241 |
return -1; |
| 1242 |
} |
| 1243 |
if (parse_dtls_attrib(session_media, sdp, stream)) { |
| 1244 |
return -1; |
| 1245 |
} |
| 1246 |
break; |
| 1247 |
case AST_SIP_MEDIA_TRANSPORT_INVALID: |
| 1248 |
case AST_SIP_MEDIA_ENCRYPT_NONE: |
| 1249 |
break; |
| 1250 |
} |
| 1251 |
|
| 1252 |
return 0; |
| 1253 |
} |
| 1254 |
|
| 1255 |
static void set_ice_components(struct ast_sip_session *session, struct ast_sip_session_media *session_media) |
| 1256 |
{ |
| 1257 |
struct ast_rtp_engine_ice *ice; |
| 1258 |
|
| 1259 |
ast_assert(session_media->rtp != NULL); |
| 1260 |
|
| 1261 |
ice = ast_rtp_instance_get_ice(session_media->rtp); |
| 1262 |
if (!session->endpoint->media.rtp.ice_support || !ice) { |
| 1263 |
return; |
| 1264 |
} |
| 1265 |
|
| 1266 |
if (session->endpoint->media.rtcp_mux && session_media->remote_rtcp_mux) { |
| 1267 |
|
| 1268 |
ice->change_components(session_media->rtp, 1); |
| 1269 |
} else { |
| 1270 |
|
| 1271 |
ice->change_components(session_media->rtp, 2); |
| 1272 |
} |
| 1273 |
} |
| 1274 |
|
| 1275 |
|
| 1276 |
static void add_ssrc_to_stream(struct ast_sip_session *session, struct ast_sip_session_media *session_media, pj_pool_t *pool, pjmedia_sdp_media *media) |
| 1277 |
{ |
| 1278 |
pj_str_t stmp; |
| 1279 |
pjmedia_sdp_attr *attr; |
| 1280 |
char tmp[128]; |
| 1281 |
|
| 1282 |
if (!session->endpoint->media.bundle || session_media->bundle_group == -1) { |
| 1283 |
return; |
| 1284 |
} |
| 1285 |
|
| 1286 |
snprintf(tmp, sizeof(tmp), "%u cname:%s", ast_rtp_instance_get_ssrc(session_media->rtp), ast_rtp_instance_get_cname(session_media->rtp)); |
| 1287 |
attr = pjmedia_sdp_attr_create(pool, "ssrc", pj_cstr(&stmp, tmp)); |
| 1288 |
media->attr[media->attr_count++] = attr; |
| 1289 |
} |
| 1290 |
|
| 1291 |
|
| 1292 |
static void process_ssrc_attributes(struct ast_sip_session *session, struct ast_sip_session_media *session_media, |
| 1293 |
const struct pjmedia_sdp_media *remote_stream) |
| 1294 |
{ |
| 1295 |
int index; |
| 1296 |
|
| 1297 |
if (!session->endpoint->media.bundle) { |
| 1298 |
return; |
| 1299 |
} |
| 1300 |
|
| 1301 |
for (index = 0; index < remote_stream->attr_count; ++index) { |
| 1302 |
pjmedia_sdp_attr *attr = remote_stream->attr[index]; |
| 1303 |
char attr_value[pj_strlen(&attr->value) + 1]; |
| 1304 |
char *ssrc_attribute_name, *ssrc_attribute_value = NULL; |
| 1305 |
unsigned int ssrc; |
| 1306 |
|
| 1307 |
|
| 1308 |
if (pj_strcmp2(&attr->name, "ssrc")) { |
| 1309 |
continue; |
| 1310 |
} |
| 1311 |
|
| 1312 |
ast_copy_pj_str(attr_value, &attr->value, sizeof(attr_value)); |
| 1313 |
|
| 1314 |
if ((ssrc_attribute_name = strchr(attr_value, ' '))) { |
| 1315 |
|
| 1316 |
*ssrc_attribute_name++ = '\0'; |
| 1317 |
ssrc_attribute_value = strchr(ssrc_attribute_name, ':'); |
| 1318 |
if (ssrc_attribute_value) { |
| 1319 |
|
| 1320 |
*ssrc_attribute_value++ = '\0'; |
| 1321 |
} |
| 1322 |
} |
| 1323 |
|
| 1324 |
if (sscanf(attr_value, "%30u", &ssrc) < 1) { |
| 1325 |
continue; |
| 1326 |
} |
| 1327 |
|
| 1328 |
|
| 1329 |
|
| 1330 |
|
| 1331 |
if (pjmedia_sdp_neg_get_state(session->inv_session->neg) == PJMEDIA_SDP_NEG_STATE_REMOTE_OFFER && |
| 1332 |
session->active_media_state) { |
| 1333 |
struct ast_rtp_instance_stats stats = { 0, }; |
| 1334 |
|
| 1335 |
if (!ast_rtp_instance_get_stats(session_media->rtp, &stats, AST_RTP_INSTANCE_STAT_REMOTE_SSRC) && |
| 1336 |
stats.remote_ssrc != ssrc) { |
| 1337 |
session_media->changed = 1; |
| 1338 |
} |
| 1339 |
} |
| 1340 |
|
| 1341 |
ast_rtp_instance_set_remote_ssrc(session_media->rtp, ssrc); |
| 1342 |
break; |
| 1343 |
} |
| 1344 |
} |
| 1345 |
|
| 1346 |
static void add_msid_to_stream(struct ast_sip_session *session, |
| 1347 |
struct ast_sip_session_media *session_media, pj_pool_t *pool, pjmedia_sdp_media *media, |
| 1348 |
struct ast_stream *stream) |
| 1349 |
{ |
| 1350 |
pj_str_t stmp; |
| 1351 |
pjmedia_sdp_attr *attr; |
| 1352 |
char msid[(AST_UUID_STR_LEN * 2) + 2]; |
| 1353 |
const char *stream_label = ast_stream_get_metadata(stream, "SDP:LABEL"); |
| 1354 |
|
| 1355 |
if (!session->endpoint->media.webrtc) { |
| 1356 |
return; |
| 1357 |
} |
| 1358 |
|
| 1359 |
if (ast_strlen_zero(session_media->mslabel)) { |
| 1360 |
|
| 1361 |
if (ast_stream_get_group(stream) != -1) { |
| 1362 |
struct ast_sip_session_media *group_session_media = AST_VECTOR_GET(&session->pending_media_state->sessions, ast_stream_get_group(stream)); |
| 1363 |
|
| 1364 |
ast_copy_string(session_media->mslabel, group_session_media->mslabel, sizeof(session_media->mslabel)); |
| 1365 |
} |
| 1366 |
|
| 1367 |
if (ast_strlen_zero(session_media->mslabel)) { |
| 1368 |
ast_uuid_generate_str(session_media->mslabel, sizeof(session_media->mslabel)); |
| 1369 |
} |
| 1370 |
} |
| 1371 |
|
| 1372 |
if (ast_strlen_zero(session_media->label)) { |
| 1373 |
ast_uuid_generate_str(session_media->label, sizeof(session_media->label)); |
| 1374 |
|
| 1375 |
ast_stream_set_metadata(stream, "MSID:LABEL", session_media->label); |
| 1376 |
} |
| 1377 |
|
| 1378 |
snprintf(msid, sizeof(msid), "%s %s", session_media->mslabel, session_media->label); |
| 1379 |
ast_debug(3, "Stream msid: %p %s %s\n", stream, |
| 1380 |
ast_codec_media_type2str(ast_stream_get_type(stream)), msid); |
| 1381 |
attr = pjmedia_sdp_attr_create(pool, "msid", pj_cstr(&stmp, msid)); |
| 1382 |
pjmedia_sdp_attr_add(&media->attr_count, media->attr, attr); |
| 1383 |
|
| 1384 |
|
| 1385 |
if (!ast_strlen_zero(stream_label)) { |
| 1386 |
ast_debug(3, "Stream Label: %p %s %s\n", stream, |
| 1387 |
ast_codec_media_type2str(ast_stream_get_type(stream)), stream_label); |
| 1388 |
attr = pjmedia_sdp_attr_create(pool, "label", pj_cstr(&stmp, stream_label)); |
| 1389 |
pjmedia_sdp_attr_add(&media->attr_count, media->attr, attr); |
| 1390 |
} |
| 1391 |
} |
| 1392 |
|
| 1393 |
static void add_rtcp_fb_to_stream(struct ast_sip_session *session, |
| 1394 |
struct ast_sip_session_media *session_media, pj_pool_t *pool, pjmedia_sdp_media *media) |
| 1395 |
{ |
| 1396 |
pj_str_t stmp; |
| 1397 |
pjmedia_sdp_attr *attr; |
| 1398 |
|
| 1399 |
if (!session->endpoint->media.webrtc) { |
| 1400 |
return; |
| 1401 |
} |
| 1402 |
|
| 1403 |
|
| 1404 |
|
| 1405 |
|
| 1406 |
|
| 1407 |
attr = pjmedia_sdp_attr_create(pool, "rtcp-fb", pj_cstr(&stmp, "* transport-cc")); |
| 1408 |
pjmedia_sdp_attr_add(&media->attr_count, media->attr, attr); |
| 1409 |
|
| 1410 |
if (session_media->type != AST_MEDIA_TYPE_VIDEO) { |
| 1411 |
return; |
| 1412 |
} |
| 1413 |
|
| 1414 |
|
| 1415 |
|
| 1416 |
|
| 1417 |
|
| 1418 |
attr = pjmedia_sdp_attr_create(pool, "rtcp-fb", pj_cstr(&stmp, "* ccm fir")); |
| 1419 |
pjmedia_sdp_attr_add(&media->attr_count, media->attr, attr); |
| 1420 |
|
| 1421 |
attr = pjmedia_sdp_attr_create(pool, "rtcp-fb", pj_cstr(&stmp, "* goog-remb")); |
| 1422 |
pjmedia_sdp_attr_add(&media->attr_count, media->attr, attr); |
| 1423 |
|
| 1424 |
attr = pjmedia_sdp_attr_create(pool, "rtcp-fb", pj_cstr(&stmp, "* nack")); |
| 1425 |
pjmedia_sdp_attr_add(&media->attr_count, media->attr, attr); |
| 1426 |
} |
| 1427 |
|
| 1428 |
static void add_extmap_to_stream(struct ast_sip_session *session, |
| 1429 |
struct ast_sip_session_media *session_media, pj_pool_t *pool, pjmedia_sdp_media *media) |
| 1430 |
{ |
| 1431 |
int idx; |
| 1432 |
char extmap_value[256]; |
| 1433 |
|
| 1434 |
if (!session->endpoint->media.webrtc || session_media->type != AST_MEDIA_TYPE_VIDEO) { |
| 1435 |
return; |
| 1436 |
} |
| 1437 |
|
| 1438 |
|
| 1439 |
for (idx = 1; idx <= ast_rtp_instance_extmap_count(session_media->rtp); ++idx) { |
| 1440 |
enum ast_rtp_extension extension = ast_rtp_instance_extmap_get_extension(session_media->rtp, idx); |
| 1441 |
const char *direction_str = ""; |
| 1442 |
pj_str_t stmp; |
| 1443 |
pjmedia_sdp_attr *attr; |
| 1444 |
|
| 1445 |
|
| 1446 |
if (extension == AST_RTP_EXTENSION_UNSUPPORTED) { |
| 1447 |
continue; |
| 1448 |
} |
| 1449 |
|
| 1450 |
switch (ast_rtp_instance_extmap_get_direction(session_media->rtp, idx)) { |
| 1451 |
case AST_RTP_EXTENSION_DIRECTION_SENDRECV: |
| 1452 |
|
| 1453 |
direction_str = ""; |
| 1454 |
break; |
| 1455 |
case AST_RTP_EXTENSION_DIRECTION_SENDONLY: |
| 1456 |
direction_str = "/sendonly"; |
| 1457 |
break; |
| 1458 |
case AST_RTP_EXTENSION_DIRECTION_RECVONLY: |
| 1459 |
direction_str = "/recvonly"; |
| 1460 |
break; |
| 1461 |
case AST_RTP_EXTENSION_DIRECTION_NONE: |
| 1462 |
|
| 1463 |
|
| 1464 |
|
| 1465 |
case AST_RTP_EXTENSION_DIRECTION_INACTIVE: |
| 1466 |
direction_str = "/inactive"; |
| 1467 |
break; |
| 1468 |
} |
| 1469 |
|
| 1470 |
snprintf(extmap_value, sizeof(extmap_value), "%d%s %s", idx, direction_str, |
| 1471 |
ast_rtp_instance_extmap_get_uri(session_media->rtp, idx)); |
| 1472 |
attr = pjmedia_sdp_attr_create(pool, "extmap", pj_cstr(&stmp, extmap_value)); |
| 1473 |
pjmedia_sdp_attr_add(&media->attr_count, media->attr, attr); |
| 1474 |
} |
| 1475 |
} |
| 1476 |
|
| 1477 |
|
| 1478 |
static void process_extmap_attributes(struct ast_sip_session *session, struct ast_sip_session_media *session_media, |
| 1479 |
const struct pjmedia_sdp_media *remote_stream) |
| 1480 |
{ |
| 1481 |
int index; |
| 1482 |
|
| 1483 |
if (!session->endpoint->media.webrtc || session_media->type != AST_MEDIA_TYPE_VIDEO) { |
| 1484 |
return; |
| 1485 |
} |
| 1486 |
|
| 1487 |
ast_rtp_instance_extmap_clear(session_media->rtp); |
| 1488 |
|
| 1489 |
for (index = 0; index < remote_stream->attr_count; ++index) { |
| 1490 |
pjmedia_sdp_attr *attr = remote_stream->attr[index]; |
| 1491 |
char attr_value[pj_strlen(&attr->value) + 1]; |
| 1492 |
char *uri; |
| 1493 |
int id; |
| 1494 |
char direction_str[10] = ""; |
| 1495 |
char *attributes; |
| 1496 |
enum ast_rtp_extension_direction direction = AST_RTP_EXTENSION_DIRECTION_SENDRECV; |
| 1497 |
|
| 1498 |
|
| 1499 |
if (pj_strcmp2(&attr->name, "extmap")) { |
| 1500 |
continue; |
| 1501 |
} |
| 1502 |
|
| 1503 |
ast_copy_pj_str(attr_value, &attr->value, sizeof(attr_value)); |
| 1504 |
|
| 1505 |
|
| 1506 |
uri = strchr(attr_value, ' '); |
| 1507 |
if (ast_strlen_zero(uri)) { |
| 1508 |
continue; |
| 1509 |
} |
| 1510 |
*uri++ = '\0'; |
| 1511 |
|
| 1512 |
if ((sscanf(attr_value, "%30d%9s", &id, direction_str) < 1) || (id < 1)) { |
| 1513 |
|
| 1514 |
continue; |
| 1515 |
} |
| 1516 |
|
| 1517 |
|
| 1518 |
if (!strcasecmp(direction_str, "/sendonly")) { |
| 1519 |
direction = AST_RTP_EXTENSION_DIRECTION_SENDONLY; |
| 1520 |
} else if (!strcasecmp(direction_str, "/recvonly")) { |
| 1521 |
direction = AST_RTP_EXTENSION_DIRECTION_RECVONLY; |
| 1522 |
} else if (!strcasecmp(direction_str, "/inactive")) { |
| 1523 |
direction = AST_RTP_EXTENSION_DIRECTION_INACTIVE; |
| 1524 |
} |
| 1525 |
|
| 1526 |
attributes = strchr(uri, ' '); |
| 1527 |
if (!ast_strlen_zero(attributes)) { |
| 1528 |
*attributes++ = '\0'; |
| 1529 |
} |
| 1530 |
|
| 1531 |
ast_rtp_instance_extmap_negotiate(session_media->rtp, id, direction, uri, attributes); |
| 1532 |
} |
| 1533 |
} |
| 1534 |
|
| 1535 |
static void set_session_media_remotely_held(struct ast_sip_session_media *session_media, |
| 1536 |
const struct ast_sip_session *session, |
| 1537 |
const pjmedia_sdp_media *media, |
| 1538 |
const struct ast_stream *stream, |
| 1539 |
const struct ast_sockaddr *addrs) |
| 1540 |
{ |
| 1541 |
if (ast_sip_session_is_pending_stream_default(session, stream) && |
| 1542 |
(session_media->type == AST_MEDIA_TYPE_AUDIO)) { |
| 1543 |
if (((addrs != NULL) && ast_sockaddr_isnull(addrs)) || |
| 1544 |
((addrs != NULL) && ast_sockaddr_is_any(addrs)) || |
| 1545 |
pjmedia_sdp_media_find_attr2(media, "sendonly", NULL) || |
| 1546 |
pjmedia_sdp_media_find_attr2(media, "inactive", NULL)) { |
| 1547 |
if (!session_media->remotely_held) { |
| 1548 |
session_media->remotely_held = 1; |
| 1549 |
session_media->remotely_held_changed = 1; |
| 1550 |
} |
| 1551 |
} else if (session_media->remotely_held) { |
| 1552 |
session_media->remotely_held = 0; |
| 1553 |
session_media->remotely_held_changed = 1; |
| 1554 |
} |
| 1555 |
} |
| 1556 |
} |
| 1557 |
|
| 1558 |
|
| 1559 |
static int negotiate_incoming_sdp_stream(struct ast_sip_session *session, |
| 1560 |
struct ast_sip_session_media *session_media, const pjmedia_sdp_session *sdp, |
| 1561 |
int index, struct ast_stream *asterisk_stream) |
| 1562 |
{ |
| 1563 |
char host[NI_MAXHOST]; |
| 1564 |
RAII_VAR(struct ast_sockaddr *, addrs, NULL, ast_free); |
| 1565 |
pjmedia_sdp_media *stream = sdp->media[index]; |
| 1566 |
struct ast_sip_session_media *session_media_transport; |
| 1567 |
enum ast_media_type media_type = session_media->type; |
| 1568 |
enum ast_sip_session_media_encryption encryption = AST_SIP_MEDIA_ENCRYPT_NONE; |
| 1569 |
struct ast_format_cap *joint; |
| 1570 |
int res; |
| 1571 |
SCOPE_ENTER(1, "%s\n", ast_sip_session_get_name(session)); |
| 1572 |
|
| 1573 |
|
| 1574 |
if (!ast_format_cap_has_type(session->endpoint->media.codecs, media_type)) { |
| 1575 |
ast_debug(3, "Endpoint has no codecs for media type '%s', declining stream\n", |
| 1576 |
ast_codec_media_type2str(session_media->type)); |
| 1577 |
SCOPE_EXIT_RTN_VALUE(0, "Endpoint has no codecs\n"); |
| 1578 |
} |
| 1579 |
|
| 1580 |
|
| 1581 |
if (!session->endpoint->media.rtp.use_received_transport) { |
| 1582 |
encryption = check_endpoint_media_transport(session->endpoint, stream); |
| 1583 |
|
| 1584 |
if (encryption == AST_SIP_MEDIA_TRANSPORT_INVALID) { |
| 1585 |
SCOPE_EXIT_RTN_VALUE(-1, "Incompatible transport\n"); |
| 1586 |
} |
| 1587 |
} |
| 1588 |
|
| 1589 |
ast_copy_pj_str(host, stream->conn ? &stream->conn->addr : &sdp->conn->addr, sizeof(host)); |
| 1590 |
|
| 1591 |
|
| 1592 |
if (ast_sockaddr_resolve(&addrs, host, PARSE_PORT_FORBID, AST_AF_UNSPEC) <= 0) { |
| 1593 |
|
| 1594 |
SCOPE_EXIT_RTN_VALUE(-1, "Invalid host\n"); |
| 1595 |
} |
| 1596 |
|
| 1597 |
|
| 1598 |
if (!session_media->rtp && create_rtp(session, session_media, sdp)) { |
| 1599 |
SCOPE_EXIT_RTN_VALUE(-1, "Couldn't create rtp\n"); |
| 1600 |
} |
| 1601 |
|
| 1602 |
process_ssrc_attributes(session, session_media, stream); |
| 1603 |
process_extmap_attributes(session, session_media, stream); |
| 1604 |
session_media_transport = ast_sip_session_media_get_transport(session, session_media); |
| 1605 |
|
| 1606 |
if (session_media_transport == session_media || !session_media->bundled) { |
| 1607 |
|
| 1608 |
session_media->remote_rtcp_mux = (pjmedia_sdp_media_find_attr2(stream, "rtcp-mux", NULL) != NULL); |
| 1609 |
set_ice_components(session, session_media); |
| 1610 |
|
| 1611 |
enable_rtcp(session, session_media, stream); |
| 1612 |
|
| 1613 |
res = setup_media_encryption(session, session_media, sdp, stream); |
| 1614 |
if (res) { |
| 1615 |
if (!session->endpoint->media.rtp.encryption_optimistic || |
| 1616 |
!pj_strncmp2(&stream->desc.transport, "RTP/SAVP", 8)) { |
| 1617 |
|
| 1618 |
|
| 1619 |
|
| 1620 |
|
| 1621 |
SCOPE_EXIT_RTN_VALUE(-1, "Incompatible crypto\n"); |
| 1622 |
} |
| 1623 |
|
| 1624 |
session_media->encryption = AST_SIP_MEDIA_ENCRYPT_NONE; |
| 1625 |
} |
| 1626 |
|
| 1627 |
|
| 1628 |
|
| 1629 |
|
| 1630 |
|
| 1631 |
|
| 1632 |
if ((session->endpoint->media.rtp.use_received_transport) || |
| 1633 |
((encryption == AST_SIP_MEDIA_ENCRYPT_SDES) && !res)) { |
| 1634 |
pj_strdup(session->inv_session->pool, &session_media->transport, &stream->desc.transport); |
| 1635 |
} |
| 1636 |
} else { |
| 1637 |
|
| 1638 |
ast_rtp_instance_bundle(session_media->rtp, session_media_transport->rtp); |
| 1639 |
|
| 1640 |
enable_rtcp(session, session_media, stream); |
| 1641 |
} |
| 1642 |
|
| 1643 |
|
| 1644 |
check_ice_support(session, session_media, stream); |
| 1645 |
|
| 1646 |
|
| 1647 |
if (session_media->remote_ice) { |
| 1648 |
process_ice_auth_attrb(session, session_media, sdp, stream); |
| 1649 |
} |
| 1650 |
|
| 1651 |
|
| 1652 |
set_session_media_remotely_held(session_media, session, stream, asterisk_stream, addrs); |
| 1653 |
|
| 1654 |
joint = set_incoming_call_offer_cap(session, session_media, stream); |
| 1655 |
res = apply_cap_to_bundled(session_media, session_media_transport, asterisk_stream, joint); |
| 1656 |
ao2_cleanup(joint); |
| 1657 |
if (res != 0) { |
| 1658 |
SCOPE_EXIT_RTN_VALUE(0, "Something failed\n"); |
| 1659 |
} |
| 1660 |
|
| 1661 |
SCOPE_EXIT_RTN_VALUE(1); |
| 1662 |
} |
| 1663 |
|
| 1664 |
static int add_crypto_to_stream(struct ast_sip_session *session, |
| 1665 |
struct ast_sip_session_media *session_media, |
| 1666 |
pj_pool_t *pool, pjmedia_sdp_media *media) |
| 1667 |
{ |
| 1668 |
pj_str_t stmp; |
| 1669 |
pjmedia_sdp_attr *attr; |
| 1670 |
enum ast_rtp_dtls_hash hash; |
| 1671 |
const char *crypto_attribute; |
| 1672 |
struct ast_rtp_engine_dtls *dtls; |
| 1673 |
struct ast_sdp_srtp *tmp; |
| 1674 |
static const pj_str_t STR_NEW = { "new", 3 }; |
| 1675 |
static const pj_str_t STR_EXISTING = { "existing", 8 }; |
| 1676 |
static const pj_str_t STR_ACTIVE = { "active", 6 }; |
| 1677 |
static const pj_str_t STR_PASSIVE = { "passive", 7 }; |
| 1678 |
static const pj_str_t STR_ACTPASS = { "actpass", 7 }; |
| 1679 |
static const pj_str_t STR_HOLDCONN = { "holdconn", 8 }; |
| 1680 |
static const pj_str_t STR_MEDSECREQ = { "requested", 9 }; |
| 1681 |
enum ast_rtp_dtls_setup setup; |
| 1682 |
|
| 1683 |
switch (session_media->encryption) { |
| 1684 |
case AST_SIP_MEDIA_ENCRYPT_NONE: |
| 1685 |
case AST_SIP_MEDIA_TRANSPORT_INVALID: |
| 1686 |
break; |
| 1687 |
case AST_SIP_MEDIA_ENCRYPT_SDES: |
| 1688 |
if (!session_media->srtp) { |
| 1689 |
session_media->srtp = ast_sdp_srtp_alloc(); |
| 1690 |
if (!session_media->srtp) { |
| 1691 |
return -1; |
| 1692 |
} |
| 1693 |
} |
| 1694 |
|
| 1695 |
tmp = session_media->srtp; |
| 1696 |
|
| 1697 |
do { |
| 1698 |
crypto_attribute = ast_sdp_srtp_get_attrib(tmp, |
| 1699 |
0 , |
| 1700 |
session->endpoint->media.rtp.srtp_tag_32 ); |
| 1701 |
if (!crypto_attribute) { |
| 1702 |
|
| 1703 |
return -1; |
| 1704 |
} |
| 1705 |
|
| 1706 |
attr = pjmedia_sdp_attr_create(pool, "crypto", |
| 1707 |
pj_cstr(&stmp, crypto_attribute)); |
| 1708 |
media->attr[media->attr_count++] = attr; |
| 1709 |
} while ((tmp = AST_LIST_NEXT(tmp, sdp_srtp_list))); |
| 1710 |
|
| 1711 |
if (session->endpoint->security_negotiation == AST_SIP_SECURITY_NEG_MEDIASEC) { |
| 1712 |
attr = pjmedia_sdp_attr_create(pool, "3ge2ae", &STR_MEDSECREQ); |
| 1713 |
media->attr[media->attr_count++] = attr; |
| 1714 |
} |
| 1715 |
|
| 1716 |
break; |
| 1717 |
case AST_SIP_MEDIA_ENCRYPT_DTLS: |
| 1718 |
if (setup_dtls_srtp(session, session_media)) { |
| 1719 |
return -1; |
| 1720 |
} |
| 1721 |
|
| 1722 |
dtls = ast_rtp_instance_get_dtls(session_media->rtp); |
| 1723 |
if (!dtls) { |
| 1724 |
return -1; |
| 1725 |
} |
| 1726 |
|
| 1727 |
switch (dtls->get_connection(session_media->rtp)) { |
| 1728 |
case AST_RTP_DTLS_CONNECTION_NEW: |
| 1729 |
attr = pjmedia_sdp_attr_create(pool, "connection", &STR_NEW); |
| 1730 |
media->attr[media->attr_count++] = attr; |
| 1731 |
break; |
| 1732 |
case AST_RTP_DTLS_CONNECTION_EXISTING: |
| 1733 |
attr = pjmedia_sdp_attr_create(pool, "connection", &STR_EXISTING); |
| 1734 |
media->attr[media->attr_count++] = attr; |
| 1735 |
break; |
| 1736 |
default: |
| 1737 |
break; |
| 1738 |
} |
| 1739 |
|
| 1740 |
|
| 1741 |
|
| 1742 |
|
| 1743 |
if (session->inv_session->neg |
| 1744 |
&& pjmedia_sdp_neg_get_state(session->inv_session->neg) != PJMEDIA_SDP_NEG_STATE_DONE) { |
| 1745 |
setup = dtls->get_setup(session_media->rtp); |
| 1746 |
} else { |
| 1747 |
setup = session->endpoint->media.rtp.dtls_cfg.default_setup; |
| 1748 |
} |
| 1749 |
|
| 1750 |
switch (setup) { |
| 1751 |
case AST_RTP_DTLS_SETUP_ACTIVE: |
| 1752 |
attr = pjmedia_sdp_attr_create(pool, "setup", &STR_ACTIVE); |
| 1753 |
media->attr[media->attr_count++] = attr; |
| 1754 |
break; |
| 1755 |
case AST_RTP_DTLS_SETUP_PASSIVE: |
| 1756 |
attr = pjmedia_sdp_attr_create(pool, "setup", &STR_PASSIVE); |
| 1757 |
media->attr[media->attr_count++] = attr; |
| 1758 |
break; |
| 1759 |
case AST_RTP_DTLS_SETUP_ACTPASS: |
| 1760 |
attr = pjmedia_sdp_attr_create(pool, "setup", &STR_ACTPASS); |
| 1761 |
media->attr[media->attr_count++] = attr; |
| 1762 |
break; |
| 1763 |
case AST_RTP_DTLS_SETUP_HOLDCONN: |
| 1764 |
attr = pjmedia_sdp_attr_create(pool, "setup", &STR_HOLDCONN); |
| 1765 |
break; |
| 1766 |
default: |
| 1767 |
break; |
| 1768 |
} |
| 1769 |
|
| 1770 |
hash = dtls->get_fingerprint_hash(session_media->rtp); |
| 1771 |
crypto_attribute = dtls->get_fingerprint(session_media->rtp); |
| 1772 |
if (crypto_attribute && (hash == AST_RTP_DTLS_HASH_SHA1 || hash == AST_RTP_DTLS_HASH_SHA256)) { |
| 1773 |
RAII_VAR(struct ast_str *, fingerprint, ast_str_create(64), ast_free); |
| 1774 |
if (!fingerprint) { |
| 1775 |
return -1; |
| 1776 |
} |
| 1777 |
|
| 1778 |
if (hash == AST_RTP_DTLS_HASH_SHA1) { |
| 1779 |
ast_str_set(&fingerprint, 0, "SHA-1 %s", crypto_attribute); |
| 1780 |
} else { |
| 1781 |
ast_str_set(&fingerprint, 0, "SHA-256 %s", crypto_attribute); |
| 1782 |
} |
| 1783 |
|
| 1784 |
attr = pjmedia_sdp_attr_create(pool, "fingerprint", pj_cstr(&stmp, ast_str_buffer(fingerprint))); |
| 1785 |
media->attr[media->attr_count++] = attr; |
| 1786 |
} |
| 1787 |
break; |
| 1788 |
} |
| 1789 |
|
| 1790 |
return 0; |
| 1791 |
} |
| 1792 |
|
| 1793 |
|
| 1794 |
static int create_outgoing_sdp_stream(struct ast_sip_session *session, struct ast_sip_session_media *session_media, |
| 1795 |
struct pjmedia_sdp_session *sdp, const struct pjmedia_sdp_session *remote, struct ast_stream *stream) |
| 1796 |
{ |
| 1797 |
pj_pool_t *pool = session->inv_session->pool_prov; |
| 1798 |
static const pj_str_t STR_RTP_AVP = { "RTP/AVP", 7 }; |
| 1799 |
static const pj_str_t STR_IN = { "IN", 2 }; |
| 1800 |
static const pj_str_t STR_IP4 = { "IP4", 3}; |
| 1801 |
static const pj_str_t STR_IP6 = { "IP6", 3}; |
| 1802 |
static const pj_str_t STR_SENDRECV = { "sendrecv", 8 }; |
| 1803 |
static const pj_str_t STR_SENDONLY = { "sendonly", 8 }; |
| 1804 |
static const pj_str_t STR_INACTIVE = { "inactive", 8 }; |
| 1805 |
static const pj_str_t STR_RECVONLY = { "recvonly", 8 }; |
| 1806 |
pjmedia_sdp_media *media; |
| 1807 |
const char *hostip = NULL; |
| 1808 |
struct ast_sockaddr addr; |
| 1809 |
char tmp[512]; |
| 1810 |
pj_str_t stmp; |
| 1811 |
pjmedia_sdp_attr *attr; |
| 1812 |
int index = 0; |
| 1813 |
int noncodec = (session->dtmf == AST_SIP_DTMF_RFC_4733 || session->dtmf == AST_SIP_DTMF_AUTO || session->dtmf == AST_SIP_DTMF_AUTO_INFO) ? AST_RTP_DTMF : 0; |
| 1814 |
int min_packet_size = 0, max_packet_size = 0; |
| 1815 |
int rtp_code; |
| 1816 |
RAII_VAR(struct ast_format_cap *, caps, NULL, ao2_cleanup); |
| 1817 |
enum ast_media_type media_type = session_media->type; |
| 1818 |
struct ast_sip_session_media *session_media_transport; |
| 1819 |
pj_sockaddr ip; |
| 1820 |
int direct_media_enabled = !ast_sockaddr_isnull(&session_media->direct_media_addr) && |
| 1821 |
ast_format_cap_count(session->direct_media_cap); |
| 1822 |
|
| 1823 |
|
| 1824 |
|
| 1825 |
AST_VECTOR(, int) sample_rates; |
| 1826 |
|
| 1827 |
int build_dtmf_sample_rates = 1; |
| 1828 |
|
| 1829 |
SCOPE_ENTER(1, "%s Type: %s %s\n", ast_sip_session_get_name(session), |
| 1830 |
ast_codec_media_type2str(media_type), ast_str_tmp(128, ast_stream_to_str(stream, &STR_TMP))); |
| 1831 |
|
| 1832 |
media = pj_pool_zalloc(pool, sizeof(struct pjmedia_sdp_media)); |
| 1833 |
if (!media) { |
| 1834 |
SCOPE_EXIT_RTN_VALUE(-1, "Pool alloc failure\n"); |
| 1835 |
} |
| 1836 |
pj_strdup2(pool, &media->desc.media, ast_codec_media_type2str(session_media->type)); |
| 1837 |
|
| 1838 |
|
| 1839 |
if (ast_stream_get_state(stream) == AST_STREAM_STATE_REMOVED || !ast_stream_get_formats(stream) || |
| 1840 |
!ast_format_cap_count(ast_stream_get_formats(stream))) { |
| 1841 |
media->desc.port = 0; |
| 1842 |
media->desc.port_count = 1; |
| 1843 |
|
| 1844 |
if (remote && remote->media[ast_stream_get_position(stream)]) { |
| 1845 |
pjmedia_sdp_media *remote_media = remote->media[ast_stream_get_position(stream)]; |
| 1846 |
int index; |
| 1847 |
|
| 1848 |
media->desc.transport = remote_media->desc.transport; |
| 1849 |
|
| 1850 |
|
| 1851 |
for (index = 0; index < remote_media->desc.fmt_count; ++index) { |
| 1852 |
media->desc.fmt[index] = remote_media->desc.fmt[index]; |
| 1853 |
} |
| 1854 |
media->desc.fmt_count = remote_media->desc.fmt_count; |
| 1855 |
} else { |
| 1856 |
|
| 1857 |
media->desc.transport = STR_RTP_AVP; |
| 1858 |
pj_strdup2(pool, &media->desc.fmt[media->desc.fmt_count++], "32"); |
| 1859 |
} |
| 1860 |
|
| 1861 |
sdp->media[sdp->media_count++] = media; |
| 1862 |
ast_stream_set_state(stream, AST_STREAM_STATE_REMOVED); |
| 1863 |
|
| 1864 |
SCOPE_EXIT_RTN_VALUE(1, "Stream removed or no formats\n"); |
| 1865 |
} |
| 1866 |
|
| 1867 |
if (!session_media->rtp && create_rtp(session, session_media, sdp)) { |
| 1868 |
SCOPE_EXIT_RTN_VALUE(-1, "Couldn't create rtp\n"); |
| 1869 |
} |
| 1870 |
|
| 1871 |
|
| 1872 |
if (session_media->bundle_group != -1 && !session_media->bundled) { |
| 1873 |
for (index = 0; index < sdp->media_count; ++index) { |
| 1874 |
struct ast_sip_session_media *other_session_media; |
| 1875 |
|
| 1876 |
other_session_media = AST_VECTOR_GET(&session->pending_media_state->sessions, index); |
| 1877 |
if (!other_session_media->rtp || other_session_media->bundle_group != session_media->bundle_group) { |
| 1878 |
continue; |
| 1879 |
} |
| 1880 |
|
| 1881 |
if (ast_rtp_instance_get_ssrc(session_media->rtp) == ast_rtp_instance_get_ssrc(other_session_media->rtp)) { |
| 1882 |
ast_rtp_instance_change_source(session_media->rtp); |
| 1883 |
|
| 1884 |
index = -1; |
| 1885 |
continue; |
| 1886 |
} |
| 1887 |
} |
| 1888 |
} |
| 1889 |
|
| 1890 |
session_media_transport = ast_sip_session_media_get_transport(session, session_media); |
| 1891 |
|
| 1892 |
if (session_media_transport == session_media || !session_media->bundled) { |
| 1893 |
set_ice_components(session, session_media); |
| 1894 |
enable_rtcp(session, session_media, NULL); |
| 1895 |
|
| 1896 |
|
| 1897 |
|
| 1898 |
|
| 1899 |
if (add_crypto_to_stream(session, session_media, pool, media)) { |
| 1900 |
SCOPE_EXIT_RTN_VALUE(-1, "Couldn't add crypto\n"); |
| 1901 |
} |
| 1902 |
|
| 1903 |
if (pj_strlen(&session_media->transport)) { |
| 1904 |
|
| 1905 |
media->desc.transport = session_media->transport; |
| 1906 |
} else { |
| 1907 |
media->desc.transport = pj_str(ast_sdp_get_rtp_profile( |
| 1908 |
|
| 1909 |
!session->endpoint->media.rtp.encryption_optimistic && |
| 1910 |
(session_media->encryption == AST_SIP_MEDIA_ENCRYPT_SDES), |
| 1911 |
session_media->rtp, session->endpoint->media.rtp.use_avpf, |
| 1912 |
session->endpoint->media.rtp.force_avp)); |
| 1913 |
} |
| 1914 |
|
| 1915 |
media->conn = pj_pool_zalloc(pool, sizeof(struct pjmedia_sdp_conn)); |
| 1916 |
if (!media->conn) { |
| 1917 |
SCOPE_EXIT_RTN_VALUE(-1, "Pool alloc failure\n"); |
| 1918 |
} |
| 1919 |
|
| 1920 |
|
| 1921 |
if (direct_media_enabled) { |
| 1922 |
hostip = ast_sockaddr_stringify_fmt(&session_media->direct_media_addr, AST_SOCKADDR_STR_ADDR); |
| 1923 |
} else if (ast_strlen_zero(session->endpoint->media.address)) { |
| 1924 |
hostip = ast_sip_get_host_ip_string(session->endpoint->media.rtp.ipv6 ? pj_AF_INET6() : pj_AF_INET()); |
| 1925 |
} else { |
| 1926 |
hostip = session->endpoint->media.address; |
| 1927 |
} |
| 1928 |
|
| 1929 |
if (ast_strlen_zero(hostip)) { |
| 1930 |
ast_log(LOG_ERROR, "No local host IP available for stream %s\n", |
| 1931 |
ast_codec_media_type2str(session_media->type)); |
| 1932 |
SCOPE_EXIT_RTN_VALUE(-1, "No local host ip\n"); |
| 1933 |
} |
| 1934 |
|
| 1935 |
media->conn->net_type = STR_IN; |
| 1936 |
|
| 1937 |
media->conn->addr_type = STR_IP4; |
| 1938 |
pj_strdup2(pool, &media->conn->addr, hostip); |
| 1939 |
|
| 1940 |
if ((pj_sockaddr_parse(pj_AF_UNSPEC(), 0, &media->conn->addr, &ip) == PJ_SUCCESS) && |
| 1941 |
(ip.addr.sa_family == pj_AF_INET6())) { |
| 1942 |
media->conn->addr_type = STR_IP6; |
| 1943 |
} |
| 1944 |
|
| 1945 |
|
| 1946 |
add_ice_to_stream(session, session_media, pool, media, 1); |
| 1947 |
|
| 1948 |
ast_rtp_instance_get_local_address(session_media->rtp, &addr); |
| 1949 |
media->desc.port = direct_media_enabled ? ast_sockaddr_port(&session_media->direct_media_addr) : (pj_uint16_t) ast_sockaddr_port(&addr); |
| 1950 |
media->desc.port_count = 1; |
| 1951 |
} else { |
| 1952 |
pjmedia_sdp_media *bundle_group_stream = sdp->media[session_media_transport->stream_num]; |
| 1953 |
|
| 1954 |
|
| 1955 |
media->desc.transport = bundle_group_stream->desc.transport; |
| 1956 |
media->conn = bundle_group_stream->conn; |
| 1957 |
media->desc.port = bundle_group_stream->desc.port; |
| 1958 |
|
| 1959 |
if (add_crypto_to_stream(session, session_media_transport, pool, media)) { |
| 1960 |
SCOPE_EXIT_RTN_VALUE(-1, "Couldn't add crypto\n"); |
| 1961 |
} |
| 1962 |
|
| 1963 |
add_ice_to_stream(session, session_media_transport, pool, media, 0); |
| 1964 |
|
| 1965 |
enable_rtcp(session, session_media, NULL); |
| 1966 |
} |
| 1967 |
|
| 1968 |
if (!(caps = ast_format_cap_alloc(AST_FORMAT_CAP_FLAG_DEFAULT))) { |
| 1969 |
ast_log(LOG_ERROR, "Failed to allocate %s capabilities\n", |
| 1970 |
ast_codec_media_type2str(session_media->type)); |
| 1971 |
SCOPE_EXIT_RTN_VALUE(-1, "Couldn't create caps\n"); |
| 1972 |
} |
| 1973 |
|
| 1974 |
if (direct_media_enabled) { |
| 1975 |
ast_format_cap_get_compatible(session->endpoint->media.codecs, session->direct_media_cap, caps); |
| 1976 |
} else { |
| 1977 |
ast_format_cap_append_from_cap(caps, ast_stream_get_formats(stream), media_type); |
| 1978 |
} |
| 1979 |
|
| 1980 |
|
| 1981 |
if (AST_VECTOR_INIT(&sample_rates, 1)) { |
| 1982 |
ast_log(LOG_ERROR, "Unable to add dtmf formats to SDP!\n"); |
| 1983 |
build_dtmf_sample_rates = 0; |
| 1984 |
} |
| 1985 |
|
| 1986 |
for (index = 0; index < ast_format_cap_count(caps); ++index) { |
| 1987 |
struct ast_format *format = ast_format_cap_get_format(caps, index); |
| 1988 |
|
| 1989 |
if (ast_format_get_type(format) != media_type) { |
| 1990 |
ao2_ref(format, -1); |
| 1991 |
continue; |
| 1992 |
} |
| 1993 |
|
| 1994 |
|
| 1995 |
|
| 1996 |
|
| 1997 |
if (!ast_rtp_lookup_sample_rate2(1, format, 0)) { |
| 1998 |
ast_log(LOG_WARNING, "Format '%s' can not be added to SDP, consider disallowing it on endpoint '%s'\n", |
| 1999 |
ast_format_get_name(format), ast_sorcery_object_get_id(session->endpoint)); |
| 2000 |
ao2_ref(format, -1); |
| 2001 |
continue; |
| 2002 |
} |
| 2003 |
|
| 2004 |
|
| 2005 |
|
| 2006 |
|
| 2007 |
if (session_media_transport != session_media) { |
| 2008 |
if ((rtp_code = ast_rtp_codecs_payload_code(ast_rtp_instance_get_codecs(session_media_transport->rtp), 1, format, 0)) == -1) { |
| 2009 |
ast_log(LOG_WARNING,"Unable to get rtp codec payload code for %s\n", ast_format_get_name(format)); |
| 2010 |
ao2_ref(format, -1); |
| 2011 |
continue; |
| 2012 |
} |
| 2013 |
|
| 2014 |
ast_rtp_codecs_payload_set_rx(ast_rtp_instance_get_codecs(session_media->rtp), rtp_code, format); |
| 2015 |
} else { |
| 2016 |
if ((rtp_code = ast_rtp_codecs_payload_code(ast_rtp_instance_get_codecs(session_media->rtp), 1, format, 0)) == -1) { |
| 2017 |
ast_log(LOG_WARNING,"Unable to get rtp codec payload code for %s\n", ast_format_get_name(format)); |
| 2018 |
ao2_ref(format, -1); |
| 2019 |
continue; |
| 2020 |
} |
| 2021 |
} |
| 2022 |
|
| 2023 |
if ((attr = generate_rtpmap_attr(session, media, pool, rtp_code, 1, format, 0))) { |
| 2024 |
int i, added = 0; |
| 2025 |
int newrate = ast_rtp_lookup_sample_rate2(1, format, 0); |
| 2026 |
if (build_dtmf_sample_rates) { |
| 2027 |
for (i = 0; i < AST_VECTOR_SIZE(&sample_rates); i++) { |
| 2028 |
|
| 2029 |
|
| 2030 |
if (newrate == AST_VECTOR_GET(&sample_rates, i)) { |
| 2031 |
added = 1; |
| 2032 |
break; |
| 2033 |
} |
| 2034 |
} |
| 2035 |
|
| 2036 |
if (!added) { |
| 2037 |
AST_VECTOR_APPEND(&sample_rates, newrate); |
| 2038 |
} |
| 2039 |
} |
| 2040 |
media->attr[media->attr_count++] = attr; |
| 2041 |
} |
| 2042 |
|
| 2043 |
if ((attr = generate_fmtp_attr(pool, format, rtp_code))) { |
| 2044 |
media->attr[media->attr_count++] = attr; |
| 2045 |
} |
| 2046 |
|
| 2047 |
if (ast_format_get_maximum_ms(format) && |
| 2048 |
((ast_format_get_maximum_ms(format) < max_packet_size) || !max_packet_size)) { |
| 2049 |
max_packet_size = ast_format_get_maximum_ms(format); |
| 2050 |
} |
| 2051 |
ao2_ref(format, -1); |
| 2052 |
|
| 2053 |
if (media->desc.fmt_count == PJMEDIA_MAX_SDP_FMT) { |
| 2054 |
break; |
| 2055 |
} |
| 2056 |
} |
| 2057 |
|
| 2058 |
|
| 2059 |
if (ast_sip_session_is_pending_stream_default(session, stream) && media_type != AST_MEDIA_TYPE_VIDEO |
| 2060 |
&& media->desc.fmt_count < PJMEDIA_MAX_SDP_FMT) { |
| 2061 |
for (index = 1LL; index <= AST_RTP_MAX; index <<= 1) { |
| 2062 |
if (!(noncodec & index)) { |
| 2063 |
continue; |
| 2064 |
} |
| 2065 |
|
| 2066 |
if (index != AST_RTP_DTMF) { |
| 2067 |
rtp_code = ast_rtp_codecs_payload_code( |
| 2068 |
ast_rtp_instance_get_codecs(session_media->rtp), 0, NULL, index); |
| 2069 |
if (rtp_code == -1) { |
| 2070 |
continue; |
| 2071 |
} else if ((attr = generate_rtpmap_attr(session, media, pool, rtp_code, 0, NULL, index))) { |
| 2072 |
media->attr[media->attr_count++] = attr; |
| 2073 |
} |
| 2074 |
} else if (build_dtmf_sample_rates) { |
| 2075 |
|
| 2076 |
|
| 2077 |
|
| 2078 |
|
| 2079 |
int i, found_default_offer = 0; |
| 2080 |
for (i = 0; i < AST_VECTOR_SIZE(&sample_rates); i++) { |
| 2081 |
rtp_code = ast_rtp_codecs_payload_code_sample_rate( |
| 2082 |
ast_rtp_instance_get_codecs(session_media->rtp), 0, NULL, index, AST_VECTOR_GET(&sample_rates, i)); |
| 2083 |
|
| 2084 |
if (rtp_code == -1) { |
| 2085 |
continue; |
| 2086 |
} |
| 2087 |
|
| 2088 |
if (AST_VECTOR_GET(&sample_rates, i) == DEFAULT_DTMF_SAMPLE_RATE_MS) { |
| 2089 |
|
| 2090 |
found_default_offer = 1; |
| 2091 |
} |
| 2092 |
|
| 2093 |
if ((attr = generate_rtpmap_attr2(session, media, pool, rtp_code, 0, NULL, index, AST_VECTOR_GET(&sample_rates, i)))) { |
| 2094 |
media->attr[media->attr_count++] = attr; |
| 2095 |
snprintf(tmp, sizeof(tmp), "%d 0-16", (rtp_code)); |
| 2096 |
attr = pjmedia_sdp_attr_create(pool, "fmtp", pj_cstr(&stmp, tmp)); |
| 2097 |
media->attr[media->attr_count++] = attr; |
| 2098 |
} |
| 2099 |
} |
| 2100 |
|
| 2101 |
|
| 2102 |
|
| 2103 |
|
| 2104 |
if (!found_default_offer) { |
| 2105 |
rtp_code = ast_rtp_codecs_payload_code_sample_rate( |
| 2106 |
ast_rtp_instance_get_codecs(session_media->rtp), 0, NULL, index, DEFAULT_DTMF_SAMPLE_RATE_MS); |
| 2107 |
|
| 2108 |
if (rtp_code != -1 && (attr = generate_rtpmap_attr2(session, media, pool, rtp_code, 0, NULL, index, DEFAULT_DTMF_SAMPLE_RATE_MS))) { |
| 2109 |
media->attr[media->attr_count++] = attr; |
| 2110 |
snprintf(tmp, sizeof(tmp), "%d 0-16", (rtp_code)); |
| 2111 |
attr = pjmedia_sdp_attr_create(pool, "fmtp", pj_cstr(&stmp, tmp)); |
| 2112 |
media->attr[media->attr_count++] = attr; |
| 2113 |
} |
| 2114 |
} |
| 2115 |
} |
| 2116 |
|
| 2117 |
if (media->desc.fmt_count == PJMEDIA_MAX_SDP_FMT) { |
| 2118 |
break; |
| 2119 |
} |
| 2120 |
} |
| 2121 |
} |
| 2122 |
|
| 2123 |
|
| 2124 |
AST_VECTOR_FREE(&sample_rates); |
| 2125 |
|
| 2126 |
|
| 2127 |
if (!media->desc.fmt_count) { |
| 2128 |
SCOPE_EXIT_RTN_VALUE(1, "No formats added to stream\n"); |
| 2129 |
} |
| 2130 |
|
| 2131 |
|
| 2132 |
min_packet_size = ast_rtp_codecs_get_framing(ast_rtp_instance_get_codecs(session_media->rtp)); |
| 2133 |
if (!min_packet_size) { |
| 2134 |
min_packet_size = ast_format_cap_get_framing(caps); |
| 2135 |
} |
| 2136 |
if (min_packet_size) { |
| 2137 |
snprintf(tmp, sizeof(tmp), "%d", min_packet_size); |
| 2138 |
attr = pjmedia_sdp_attr_create(pool, "ptime", pj_cstr(&stmp, tmp)); |
| 2139 |
media->attr[media->attr_count++] = attr; |
| 2140 |
} |
| 2141 |
|
| 2142 |
if (max_packet_size) { |
| 2143 |
snprintf(tmp, sizeof(tmp), "%d", max_packet_size); |
| 2144 |
attr = pjmedia_sdp_attr_create(pool, "maxptime", pj_cstr(&stmp, tmp)); |
| 2145 |
media->attr[media->attr_count++] = attr; |
| 2146 |
} |
| 2147 |
|
| 2148 |
attr = PJ_POOL_ZALLOC_T(pool, pjmedia_sdp_attr); |
| 2149 |
if (session_media->locally_held) { |
| 2150 |
if (session_media->remotely_held) { |
| 2151 |
attr->name = STR_INACTIVE; |
| 2152 |
} else { |
| 2153 |
attr->name = STR_SENDONLY; |
| 2154 |
} |
| 2155 |
} else { |
| 2156 |
if (session_media->remotely_held) { |
| 2157 |
attr->name = STR_RECVONLY; |
| 2158 |
} else if (ast_stream_get_state(stream) == AST_STREAM_STATE_SENDONLY) { |
| 2159 |
attr->name = STR_SENDONLY; |
| 2160 |
} else if (ast_stream_get_state(stream) == AST_STREAM_STATE_RECVONLY) { |
| 2161 |
attr->name = STR_RECVONLY; |
| 2162 |
} else if (ast_stream_get_state(stream) == AST_STREAM_STATE_INACTIVE) { |
| 2163 |
attr->name = STR_INACTIVE; |
| 2164 |
} else { |
| 2165 |
attr->name = STR_SENDRECV; |
| 2166 |
} |
| 2167 |
} |
| 2168 |
media->attr[media->attr_count++] = attr; |
| 2169 |
|
| 2170 |
|
| 2171 |
if (session->endpoint->media.rtcp_mux && session_media->remote_rtcp_mux) { |
| 2172 |
attr = pjmedia_sdp_attr_create(pool, "rtcp-mux", NULL); |
| 2173 |
pjmedia_sdp_attr_add(&media->attr_count, media->attr, attr); |
| 2174 |
} |
| 2175 |
|
| 2176 |
add_ssrc_to_stream(session, session_media, pool, media); |
| 2177 |
add_msid_to_stream(session, session_media, pool, media, stream); |
| 2178 |
add_rtcp_fb_to_stream(session, session_media, pool, media); |
| 2179 |
add_extmap_to_stream(session, session_media, pool, media); |
| 2180 |
|
| 2181 |
|
| 2182 |
sdp->media[sdp->media_count++] = media; |
| 2183 |
|
| 2184 |
SCOPE_EXIT_RTN_VALUE(1, "RC: 1\n"); |
| 2185 |
} |
| 2186 |
|
| 2187 |
static struct ast_frame *media_session_rtp_read_callback(struct ast_sip_session *session, struct ast_sip_session_media *session_media) |
| 2188 |
{ |
| 2189 |
struct ast_frame *f; |
| 2190 |
|
| 2191 |
if (!session_media->rtp) { |
| 2192 |
return &ast_null_frame; |
| 2193 |
} |
| 2194 |
|
| 2195 |
f = ast_rtp_instance_read(session_media->rtp, 0); |
| 2196 |
if (!f) { |
| 2197 |
return NULL; |
| 2198 |
} |
| 2199 |
|
| 2200 |
ast_rtp_instance_set_last_rx(session_media->rtp, time(NULL)); |
| 2201 |
|
| 2202 |
return f; |
| 2203 |
} |
| 2204 |
|
| 2205 |
static struct ast_frame *media_session_rtcp_read_callback(struct ast_sip_session *session, struct ast_sip_session_media *session_media) |
| 2206 |
{ |
| 2207 |
struct ast_frame *f; |
| 2208 |
|
| 2209 |
if (!session_media->rtp) { |
| 2210 |
return &ast_null_frame; |
| 2211 |
} |
| 2212 |
|
| 2213 |
f = ast_rtp_instance_read(session_media->rtp, 1); |
| 2214 |
if (!f) { |
| 2215 |
return NULL; |
| 2216 |
} |
| 2217 |
|
| 2218 |
ast_rtp_instance_set_last_rx(session_media->rtp, time(NULL)); |
| 2219 |
|
| 2220 |
return f; |
| 2221 |
} |
| 2222 |
|
| 2223 |
static int media_session_rtp_write_callback(struct ast_sip_session *session, struct ast_sip_session_media *session_media, struct ast_frame *frame) |
| 2224 |
{ |
| 2225 |
if (!session_media->rtp) { |
| 2226 |
return 0; |
| 2227 |
} |
| 2228 |
|
| 2229 |
return ast_rtp_instance_write(session_media->rtp, frame); |
| 2230 |
} |
| 2231 |
|
| 2232 |
static int apply_negotiated_sdp_stream(struct ast_sip_session *session, |
| 2233 |
struct ast_sip_session_media *session_media, const struct pjmedia_sdp_session *local, |
| 2234 |
const struct pjmedia_sdp_session *remote, int index, struct ast_stream *asterisk_stream) |
| 2235 |
{ |
| 2236 |
RAII_VAR(struct ast_sockaddr *, addrs, NULL, ast_free); |
| 2237 |
struct pjmedia_sdp_media *remote_stream = remote->media[index]; |
| 2238 |
enum ast_media_type media_type = session_media->type; |
| 2239 |
char host[NI_MAXHOST]; |
| 2240 |
int res; |
| 2241 |
int rtp_timeout; |
| 2242 |
struct ast_sip_session_media *session_media_transport; |
| 2243 |
SCOPE_ENTER(1, "%s Stream: %s\n", ast_sip_session_get_name(session), |
| 2244 |
ast_str_tmp(128, ast_stream_to_str(asterisk_stream, &STR_TMP))); |
| 2245 |
|
| 2246 |
if (!session->channel) { |
| 2247 |
SCOPE_EXIT_RTN_VALUE(1, "No channel\n"); |
| 2248 |
} |
| 2249 |
|
| 2250 |
|
| 2251 |
if (!session->endpoint->media.rtp.use_received_transport && |
| 2252 |
check_endpoint_media_transport(session->endpoint, remote_stream) == AST_SIP_MEDIA_TRANSPORT_INVALID) { |
| 2253 |
SCOPE_EXIT_RTN_VALUE(-1, "Incompatible transport\n"); |
| 2254 |
} |
| 2255 |
|
| 2256 |
|
| 2257 |
if (!session_media->rtp && create_rtp(session, session_media, local)) { |
| 2258 |
SCOPE_EXIT_RTN_VALUE(-1, "Couldn't create rtp\n"); |
| 2259 |
} |
| 2260 |
|
| 2261 |
process_ssrc_attributes(session, session_media, remote_stream); |
| 2262 |
process_extmap_attributes(session, session_media, remote_stream); |
| 2263 |
|
| 2264 |
session_media_transport = ast_sip_session_media_get_transport(session, session_media); |
| 2265 |
|
| 2266 |
if (session_media_transport == session_media || !session_media->bundled) { |
| 2267 |
session_media->remote_rtcp_mux = (pjmedia_sdp_media_find_attr2(remote_stream, "rtcp-mux", NULL) != NULL); |
| 2268 |
set_ice_components(session, session_media); |
| 2269 |
|
| 2270 |
enable_rtcp(session, session_media, remote_stream); |
| 2271 |
|
| 2272 |
res = setup_media_encryption(session, session_media, remote, remote_stream); |
| 2273 |
if (!session->endpoint->media.rtp.encryption_optimistic && res) { |
| 2274 |
|
| 2275 |
|
| 2276 |
|
| 2277 |
SCOPE_EXIT_RTN_VALUE(-1, "Incompatible crypto\n"); |
| 2278 |
} |
| 2279 |
|
| 2280 |
if (!remote_stream->conn && !remote->conn) { |
| 2281 |
SCOPE_EXIT_RTN_VALUE(1, "No connection info\n"); |
| 2282 |
} |
| 2283 |
|
| 2284 |
ast_copy_pj_str(host, remote_stream->conn ? &remote_stream->conn->addr : &remote->conn->addr, sizeof(host)); |
| 2285 |
|
| 2286 |
|
| 2287 |
if (ast_sockaddr_resolve(&addrs, host, PARSE_PORT_FORBID, AST_AF_UNSPEC) <= 0) { |
| 2288 |
|
| 2289 |
SCOPE_EXIT_RTN_VALUE(-1, "Host invalid\n"); |
| 2290 |
} |
| 2291 |
|
| 2292 |
|
| 2293 |
ast_sockaddr_set_port(addrs, remote_stream->desc.port); |
| 2294 |
ast_rtp_instance_set_remote_address(session_media->rtp, addrs); |
| 2295 |
|
| 2296 |
ast_sip_session_media_set_write_callback(session, session_media, media_session_rtp_write_callback); |
| 2297 |
ast_sip_session_media_add_read_callback(session, session_media, ast_rtp_instance_fd(session_media->rtp, 0), |
| 2298 |
media_session_rtp_read_callback); |
| 2299 |
if (!session->endpoint->media.rtcp_mux || !session_media->remote_rtcp_mux) { |
| 2300 |
ast_sip_session_media_add_read_callback(session, session_media, ast_rtp_instance_fd(session_media->rtp, 1), |
| 2301 |
media_session_rtcp_read_callback); |
| 2302 |
} |
| 2303 |
|
| 2304 |
|
| 2305 |
process_ice_attributes(session, session_media, remote, remote_stream); |
| 2306 |
} else { |
| 2307 |
|
| 2308 |
ast_rtp_instance_bundle(session_media->rtp, session_media_transport->rtp); |
| 2309 |
ast_sip_session_media_set_write_callback(session, session_media, media_session_rtp_write_callback); |
| 2310 |
enable_rtcp(session, session_media, remote_stream); |
| 2311 |
} |
| 2312 |
|
| 2313 |
if (set_caps(session, session_media, session_media_transport, remote_stream, 0, asterisk_stream)) { |
| 2314 |
SCOPE_EXIT_RTN_VALUE(-1, "set_caps failed\n"); |
| 2315 |
} |
| 2316 |
|
| 2317 |
|
| 2318 |
ast_channel_lock(session->channel); |
| 2319 |
ast_rtp_instance_set_channel_id(session_media->rtp, ast_channel_uniqueid(session->channel)); |
| 2320 |
ast_channel_unlock(session->channel); |
| 2321 |
|
| 2322 |
|
| 2323 |
ast_rtp_instance_set_stream_num(session_media->rtp, ast_stream_get_position(asterisk_stream)); |
| 2324 |
ast_rtp_instance_activate(session_media->rtp); |
| 2325 |
|
| 2326 |
|
| 2327 |
if (media_type != AST_MEDIA_TYPE_AUDIO && media_type != AST_MEDIA_TYPE_VIDEO) { |
| 2328 |
if ((pjmedia_sdp_neg_was_answer_remote(session->inv_session->neg) == PJ_FALSE) |
| 2329 |
&& (session->inv_session->state == PJSIP_INV_STATE_CONFIRMED)) { |
| 2330 |
ast_queue_control(session->channel, AST_CONTROL_UPDATE_RTP_PEER); |
| 2331 |
} |
| 2332 |
SCOPE_EXIT_RTN_VALUE(1, "moh\n"); |
| 2333 |
} |
| 2334 |
|
| 2335 |
set_session_media_remotely_held(session_media, session, remote_stream, asterisk_stream, addrs); |
| 2336 |
|
| 2337 |
if (session_media->remotely_held_changed) { |
| 2338 |
if (session_media->remotely_held) { |
| 2339 |
|
| 2340 |
if (!session->endpoint->suppress_moh_on_sendonly) { |
| 2341 |
ast_queue_hold(session->channel, session->endpoint->mohsuggest); |
| 2342 |
ast_rtp_instance_stop(session_media->rtp); |
| 2343 |
ast_queue_frame(session->channel, &ast_null_frame); |
| 2344 |
} |
| 2345 |
session_media->remotely_held_changed = 0; |
| 2346 |
} else { |
| 2347 |
|
| 2348 |
if (!session->endpoint->suppress_moh_on_sendonly) { |
| 2349 |
ast_queue_unhold(session->channel); |
| 2350 |
ast_queue_frame(session->channel, &ast_null_frame); |
| 2351 |
} |
| 2352 |
session_media->remotely_held_changed = 0; |
| 2353 |
} |
| 2354 |
} else if ((pjmedia_sdp_neg_was_answer_remote(session->inv_session->neg) == PJ_FALSE) |
| 2355 |
&& (session->inv_session->state == PJSIP_INV_STATE_CONFIRMED)) { |
| 2356 |
ast_queue_control(session->channel, AST_CONTROL_UPDATE_RTP_PEER); |
| 2357 |
} |
| 2358 |
|
| 2359 |
|
| 2360 |
session_media->encryption = session->endpoint->media.rtp.encryption; |
| 2361 |
|
| 2362 |
if (session->endpoint->media.rtp.keepalive > 0 && |
| 2363 |
(session_media->type == AST_MEDIA_TYPE_AUDIO || |
| 2364 |
session_media->type == AST_MEDIA_TYPE_VIDEO)) { |
| 2365 |
ast_rtp_instance_set_keepalive(session_media->rtp, session->endpoint->media.rtp.keepalive); |
| 2366 |
|
| 2367 |
|
| 2368 |
|
| 2369 |
|
| 2370 |
AST_SCHED_DEL(sched, session_media->keepalive_sched_id); |
| 2371 |
session_media->keepalive_sched_id = ast_sched_add_variable(sched, 500, send_keepalive, |
| 2372 |
session_media, 1); |
| 2373 |
} |
| 2374 |
|
| 2375 |
|
| 2376 |
|
| 2377 |
|
| 2378 |
AST_SCHED_DEL(sched, session_media->timeout_sched_id); |
| 2379 |
|
| 2380 |
|
| 2381 |
|
| 2382 |
|
| 2383 |
|
| 2384 |
ast_rtp_instance_set_timeout(session_media->rtp, 0); |
| 2385 |
if (session->endpoint->media.rtp.timeout && !session_media->remotely_held && !session_media->locally_held) { |
| 2386 |
ast_rtp_instance_set_timeout(session_media->rtp, session->endpoint->media.rtp.timeout); |
| 2387 |
} else if (session->endpoint->media.rtp.timeout_hold && (session_media->remotely_held || session_media->locally_held)) { |
| 2388 |
ast_rtp_instance_set_timeout(session_media->rtp, session->endpoint->media.rtp.timeout_hold); |
| 2389 |
} |
| 2390 |
|
| 2391 |
rtp_timeout = ast_rtp_instance_get_timeout(session_media->rtp); |
| 2392 |
|
| 2393 |
if (rtp_timeout) { |
| 2394 |
session_media->timeout_sched_id = ast_sched_add_variable(sched, rtp_timeout*1000, rtp_check_timeout, |
| 2395 |
session_media, 1); |
| 2396 |
} |
| 2397 |
|
| 2398 |
SCOPE_EXIT_RTN_VALUE(1, "Handled\n"); |
| 2399 |
} |
| 2400 |
|
| 2401 |
|
| 2402 |
static void change_outgoing_sdp_stream_media_address(pjsip_tx_data *tdata, struct pjmedia_sdp_media *stream, struct ast_sip_transport *transport) |
| 2403 |
{ |
| 2404 |
RAII_VAR(struct ast_sip_transport_state *, transport_state, ast_sip_get_transport_state(ast_sorcery_object_get_id(transport)), ao2_cleanup); |
| 2405 |
char host[NI_MAXHOST]; |
| 2406 |
struct ast_sockaddr our_sdp_addr = { { 0, } }; |
| 2407 |
|
| 2408 |
|
| 2409 |
if (!stream->conn || !transport_state) { |
| 2410 |
return; |
| 2411 |
} |
| 2412 |
|
| 2413 |
ast_copy_pj_str(host, &stream->conn->addr, sizeof(host)); |
| 2414 |
ast_sockaddr_parse(&our_sdp_addr, host, PARSE_PORT_FORBID); |
| 2415 |
|
| 2416 |
|
| 2417 |
|
| 2418 |
|
| 2419 |
|
| 2420 |
if (ast_sip_transport_is_nonlocal(transport_state, &our_sdp_addr) && transport_state->localnet) { |
| 2421 |
return; |
| 2422 |
} |
| 2423 |
ast_debug(5, "Setting media address to %s\n", ast_sockaddr_stringify_addr_remote(&transport_state->external_media_address)); |
| 2424 |
pj_strdup2(tdata->pool, &stream->conn->addr, ast_sockaddr_stringify_addr_remote(&transport_state->external_media_address)); |
| 2425 |
} |
| 2426 |
|
| 2427 |
|
| 2428 |
static void stream_stop(struct ast_sip_session_media *session_media) |
| 2429 |
{ |
| 2430 |
if (!session_media->rtp) { |
| 2431 |
return; |
| 2432 |
} |
| 2433 |
|
| 2434 |
AST_SCHED_DEL(sched, session_media->keepalive_sched_id); |
| 2435 |
AST_SCHED_DEL(sched, session_media->timeout_sched_id); |
| 2436 |
ast_rtp_instance_stop(session_media->rtp); |
| 2437 |
} |
| 2438 |
|
| 2439 |
|
| 2440 |
static void stream_destroy(struct ast_sip_session_media *session_media) |
| 2441 |
{ |
| 2442 |
if (session_media->rtp) { |
| 2443 |
stream_stop(session_media); |
| 2444 |
ast_rtp_instance_destroy(session_media->rtp); |
| 2445 |
} |
| 2446 |
session_media->rtp = NULL; |
| 2447 |
} |
| 2448 |
|
| 2449 |
|
| 2450 |
static struct ast_sip_session_sdp_handler audio_sdp_handler = { |
| 2451 |
.id = STR_AUDIO, |
| 2452 |
.negotiate_incoming_sdp_stream = negotiate_incoming_sdp_stream, |
| 2453 |
.create_outgoing_sdp_stream = create_outgoing_sdp_stream, |
| 2454 |
.apply_negotiated_sdp_stream = apply_negotiated_sdp_stream, |
| 2455 |
.change_outgoing_sdp_stream_media_address = change_outgoing_sdp_stream_media_address, |
| 2456 |
.stream_stop = stream_stop, |
| 2457 |
.stream_destroy = stream_destroy, |
| 2458 |
}; |
| 2459 |
|
| 2460 |
|
| 2461 |
static struct ast_sip_session_sdp_handler video_sdp_handler = { |
| 2462 |
.id = STR_VIDEO, |
| 2463 |
.negotiate_incoming_sdp_stream = negotiate_incoming_sdp_stream, |
| 2464 |
.create_outgoing_sdp_stream = create_outgoing_sdp_stream, |
| 2465 |
.apply_negotiated_sdp_stream = apply_negotiated_sdp_stream, |
| 2466 |
.change_outgoing_sdp_stream_media_address = change_outgoing_sdp_stream_media_address, |
| 2467 |
.stream_stop = stream_stop, |
| 2468 |
.stream_destroy = stream_destroy, |
| 2469 |
}; |
| 2470 |
|
| 2471 |
static int video_info_incoming_request(struct ast_sip_session *session, struct pjsip_rx_data *rdata) |
| 2472 |
{ |
| 2473 |
struct pjsip_transaction *tsx; |
| 2474 |
pjsip_tx_data *tdata; |
| 2475 |
|
| 2476 |
if (!session->channel |
| 2477 |
|| !ast_sip_are_media_types_equal(&rdata->msg_info.msg->body->content_type, |
| 2478 |
&pjsip_media_type_application_media_control_xml)) { |
| 2479 |
return 0; |
| 2480 |
} |
| 2481 |
|
| 2482 |
tsx = pjsip_rdata_get_tsx(rdata); |
| 2483 |
|
| 2484 |
ast_queue_control(session->channel, AST_CONTROL_VIDUPDATE); |
| 2485 |
|
| 2486 |
if (pjsip_dlg_create_response(session->inv_session->dlg, rdata, 200, NULL, &tdata) == PJ_SUCCESS) { |
| 2487 |
pjsip_dlg_send_response(session->inv_session->dlg, tsx, tdata); |
| 2488 |
} |
| 2489 |
|
| 2490 |
return 0; |
| 2491 |
} |
| 2492 |
|
| 2493 |
static struct ast_sip_session_supplement video_info_supplement = { |
| 2494 |
.method = "INFO", |
| 2495 |
.incoming_request = video_info_incoming_request, |
| 2496 |
}; |
| 2497 |
|
| 2498 |
|
| 2499 |
static int unload_module(void) |
| 2500 |
{ |
| 2501 |
ast_sip_session_unregister_supplement(&video_info_supplement); |
| 2502 |
ast_sip_session_unregister_sdp_handler(&video_sdp_handler, STR_VIDEO); |
| 2503 |
ast_sip_session_unregister_sdp_handler(&audio_sdp_handler, STR_AUDIO); |
| 2504 |
|
| 2505 |
if (sched) { |
| 2506 |
ast_sched_context_destroy(sched); |
| 2507 |
} |
| 2508 |
|
| 2509 |
return 0; |
| 2510 |
} |
| 2511 |
|
| 2512 |
|
| 2513 |
|
| 2514 |
|
| 2515 |
|
| 2516 |
|
| 2517 |
|
| 2518 |
|
| 2519 |
|
| 2520 |
|
| 2521 |
|
| 2522 |
static int load_module(void) |
| 2523 |
{ |
| 2524 |
if (ast_check_ipv6()) { |
| 2525 |
ast_sockaddr_parse(&address_rtp, "::", 0); |
| 2526 |
} else { |
| 2527 |
ast_sockaddr_parse(&address_rtp, "0.0.0.0", 0); |
| 2528 |
} |
| 2529 |
|
| 2530 |
if (!(sched = ast_sched_context_create())) { |
| 2531 |
ast_log(LOG_ERROR, "Unable to create scheduler context.\n"); |
| 2532 |
goto end; |
| 2533 |
} |
| 2534 |
|
| 2535 |
if (ast_sched_start_thread(sched)) { |
| 2536 |
ast_log(LOG_ERROR, "Unable to create scheduler context thread.\n"); |
| 2537 |
goto end; |
| 2538 |
} |
| 2539 |
|
| 2540 |
if (ast_sip_session_register_sdp_handler(&audio_sdp_handler, STR_AUDIO)) { |
| 2541 |
ast_log(LOG_ERROR, "Unable to register SDP handler for %s stream type\n", STR_AUDIO); |
| 2542 |
goto end; |
| 2543 |
} |
| 2544 |
|
| 2545 |
if (ast_sip_session_register_sdp_handler(&video_sdp_handler, STR_VIDEO)) { |
| 2546 |
ast_log(LOG_ERROR, "Unable to register SDP handler for %s stream type\n", STR_VIDEO); |
| 2547 |
goto end; |
| 2548 |
} |
| 2549 |
|
| 2550 |
ast_sip_session_register_supplement(&video_info_supplement); |
| 2551 |
|
| 2552 |
return AST_MODULE_LOAD_SUCCESS; |
| 2553 |
end: |
| 2554 |
unload_module(); |
| 2555 |
|
| 2556 |
return AST_MODULE_LOAD_DECLINE; |
| 2557 |
} |
| 2558 |
|
| 2559 |
AST_MODULE_INFO(ASTERISK_GPL_KEY, AST_MODFLAG_LOAD_ORDER, "PJSIP SDP RTP/AVP stream handler", |
| 2560 |
.support_level = AST_MODULE_SUPPORT_CORE, |
| 2561 |
.load = load_module, |
| 2562 |
.unload = unload_module, |
| 2563 |
.load_pri = AST_MODPRI_CHANNEL_DRIVER, |
| 2564 |
.requires = "res_pjsip,res_pjsip_session", |
| 2565 |
); |