/* Match format in the SDP media offer and answer. */ PJ_DEF(pj_status_t) pjmedia_sdp_neg_fmt_match(pj_pool_t *pool, pjmedia_sdp_media *offer, unsigned o_fmt_idx, pjmedia_sdp_media *answer, unsigned a_fmt_idx, unsigned option) { const pjmedia_sdp_attr *attr; pjmedia_sdp_rtpmap o_rtpmap, a_rtpmap; unsigned o_pt; unsigned a_pt; o_pt = pj_strtoul(&offer->desc.fmt[o_fmt_idx]); a_pt = pj_strtoul(&answer->desc.fmt[a_fmt_idx]); if (o_pt < 96 || a_pt < 96) { if (o_pt == a_pt) return PJ_SUCCESS; else return PJMEDIA_SDP_EFORMATNOTEQUAL; } /* Get the format rtpmap from the offer. */ attr = pjmedia_sdp_media_find_attr2(offer, "rtpmap", &offer->desc.fmt[o_fmt_idx]); if (!attr) { pj_assert(!"Bug! Offer haven't been validated"); return PJ_EBUG; } pjmedia_sdp_attr_get_rtpmap(attr, &o_rtpmap); /* Get the format rtpmap from the answer. */ attr = pjmedia_sdp_media_find_attr2(answer, "rtpmap", &answer->desc.fmt[a_fmt_idx]); if (!attr) { pj_assert(!"Bug! Answer haven't been validated"); return PJ_EBUG; } pjmedia_sdp_attr_get_rtpmap(attr, &a_rtpmap); if (pj_stricmp(&o_rtpmap.enc_name, &a_rtpmap.enc_name) != 0 || (o_rtpmap.clock_rate != a_rtpmap.clock_rate) || (!(pj_stricmp(&o_rtpmap.param, &a_rtpmap.param) == 0 || (a_rtpmap.param.slen == 0 && o_rtpmap.param.slen == 1 && *o_rtpmap.param.ptr == '1') || (o_rtpmap.param.slen == 0 && a_rtpmap.param.slen == 1 && *a_rtpmap.param.ptr=='1')))) { return PJMEDIA_SDP_EFORMATNOTEQUAL; } return custom_fmt_match(pool, &o_rtpmap.enc_name, offer, o_fmt_idx, answer, a_fmt_idx, option); }
/* Internal function to apply symmetric PT for the local answer. */ static void apply_answer_symmetric_pt(pj_pool_t *pool, pjmedia_sdp_media *answer, unsigned pt_cnt, const pj_str_t pt_offer[], const pj_str_t pt_answer[]) { pjmedia_sdp_attr *a_tmp[PJMEDIA_MAX_SDP_ATTR]; unsigned i, a_tmp_cnt = 0; /* Rewrite the payload types in the answer if different to * the ones in the offer. */ for (i = 0; i < pt_cnt; ++i) { pjmedia_sdp_attr *a; /* Skip if the PTs are the same already, e.g: static PT. */ if (pj_strcmp(&pt_answer[i], &pt_offer[i]) == 0) continue; /* Rewrite payload type in the answer to match to the offer */ pj_strdup(pool, &answer->desc.fmt[i], &pt_offer[i]); /* Also update payload type in rtpmap */ a = pjmedia_sdp_media_find_attr2(answer, "rtpmap", &pt_answer[i]); if (a) { rewrite_pt(pool, &a->value, &pt_answer[i], &pt_offer[i]); /* Temporarily remove the attribute in case the new payload * type is being used by another format in the media. */ pjmedia_sdp_media_remove_attr(answer, a); a_tmp[a_tmp_cnt++] = a; } /* Also update payload type in fmtp */ a = pjmedia_sdp_media_find_attr2(answer, "fmtp", &pt_answer[i]); if (a) { rewrite_pt(pool, &a->value, &pt_answer[i], &pt_offer[i]); /* Temporarily remove the attribute in case the new payload * type is being used by another format in the media. */ pjmedia_sdp_media_remove_attr(answer, a); a_tmp[a_tmp_cnt++] = a; } } /* Return back 'rtpmap' and 'fmtp' attributes */ for (i = 0; i < a_tmp_cnt; ++i) pjmedia_sdp_media_add_attr(answer, a_tmp[i]); }
static void apply_packetization(struct ast_sip_session *session, struct ast_sip_session_media *session_media, const struct pjmedia_sdp_media *remote_stream) { pjmedia_sdp_attr *attr; pj_str_t value; unsigned long framing; int codec; struct ast_codec_pref *pref = &ast_rtp_instance_get_codecs(session_media->rtp)->pref; /* Apply packetization if available and configured to do so */ if (!session->endpoint->media.rtp.use_ptime || !(attr = pjmedia_sdp_media_find_attr2(remote_stream, "ptime", NULL))) { return; } value = attr->value; framing = pj_strtoul(pj_strltrim(&value)); for (codec = 0; codec < AST_RTP_MAX_PT; codec++) { struct ast_rtp_payload_type format = ast_rtp_codecs_payload_lookup(ast_rtp_instance_get_codecs( session_media->rtp), codec); if (!format.asterisk_format) { continue; } ast_codec_pref_setsize(pref, &format.format, framing); } ast_rtp_codecs_packetization_set(ast_rtp_instance_get_codecs(session_media->rtp), session_media->rtp, pref); }
PJ_DEF(pj_status_t) pjmedia_codec_g7221_match_sdp(pj_pool_t *pool, pjmedia_sdp_media *offer, unsigned o_fmt_idx, pjmedia_sdp_media *answer, unsigned a_fmt_idx, unsigned option) { const pjmedia_sdp_attr *attr_ans; const pjmedia_sdp_attr *attr_ofr; pjmedia_sdp_fmtp fmtp; unsigned a_bitrate, o_bitrate; const pj_str_t bitrate = {"bitrate=", 8}; pj_status_t status; PJ_UNUSED_ARG(pool); PJ_UNUSED_ARG(option); /* Parse offer */ attr_ofr = pjmedia_sdp_media_find_attr2(offer, "fmtp", &offer->desc.fmt[o_fmt_idx]); if (!attr_ofr) return PJMEDIA_SDP_EINFMTP; status = pjmedia_sdp_attr_get_fmtp(attr_ofr, &fmtp); if (status != PJ_SUCCESS) return status; GET_FMTP_IVAL(o_bitrate, fmtp, bitrate, 0); /* Parse answer */ attr_ans = pjmedia_sdp_media_find_attr2(answer, "fmtp", &answer->desc.fmt[a_fmt_idx]); if (!attr_ans) return PJMEDIA_SDP_EINFMTP; status = pjmedia_sdp_attr_get_fmtp(attr_ans, &fmtp); if (status != PJ_SUCCESS) return status; GET_FMTP_IVAL(a_bitrate, fmtp, bitrate, 0); /* Compare bitrate in answer and offer. */ if (a_bitrate != o_bitrate) return PJMEDIA_SDP_EFORMATNOTEQUAL; return PJ_SUCCESS; }
static void get_codecs(struct ast_sip_session *session, const struct pjmedia_sdp_media *stream, struct ast_rtp_codecs *codecs) { pjmedia_sdp_attr *attr; pjmedia_sdp_rtpmap *rtpmap; pjmedia_sdp_fmtp fmtp; struct ast_format *format; int i, num = 0; char name[256]; char media[20]; char fmt_param[256]; ast_rtp_codecs_payloads_initialize(codecs); /* Iterate through provided formats */ for (i = 0; i < stream->desc.fmt_count; ++i) { /* The payload is kept as a string for things like t38 but for video it is always numerical */ ast_rtp_codecs_payloads_set_m_type(codecs, NULL, pj_strtoul(&stream->desc.fmt[i])); /* Look for the optional rtpmap attribute */ if (!(attr = pjmedia_sdp_media_find_attr2(stream, "rtpmap", &stream->desc.fmt[i]))) { continue; } /* Interpret the attribute as an rtpmap */ if ((pjmedia_sdp_attr_to_rtpmap(session->inv_session->pool_prov, attr, &rtpmap)) != PJ_SUCCESS) { continue; } ast_copy_pj_str(name, &rtpmap->enc_name, sizeof(name)); ast_copy_pj_str(media, (pj_str_t*)&stream->desc.media, sizeof(media)); ast_rtp_codecs_payloads_set_rtpmap_type_rate(codecs, NULL, pj_strtoul(&stream->desc.fmt[i]), media, name, 0, rtpmap->clock_rate); /* Look for an optional associated fmtp attribute */ if (!(attr = pjmedia_sdp_media_find_attr2(stream, "fmtp", &rtpmap->pt))) { continue; } if ((pjmedia_sdp_attr_get_fmtp(attr, &fmtp)) == PJ_SUCCESS) { sscanf(pj_strbuf(&fmtp.fmt), "%d", &num); if ((format = ast_rtp_codecs_get_payload_format(codecs, num))) { ast_copy_pj_str(fmt_param, &fmtp.fmt_param, sizeof(fmt_param)); ast_format_sdp_parse(format, fmt_param); } } } }
/* Toggle AMR octet-align setting in the fmtp. */ static pj_status_t amr_toggle_octet_align(pj_pool_t *pool, pjmedia_sdp_media *media, unsigned fmt_idx) { pjmedia_sdp_attr *attr; pjmedia_sdp_fmtp fmtp; const pj_str_t STR_OCTET_ALIGN = {"octet-align=", 12}; enum { MAX_FMTP_STR_LEN = 160 }; attr = pjmedia_sdp_media_find_attr2(media, "fmtp", &media->desc.fmt[fmt_idx]); /* Check if the AMR media format has FMTP attribute */ if (attr) { char *p; pj_status_t status; status = pjmedia_sdp_attr_get_fmtp(attr, &fmtp); if (status != PJ_SUCCESS) return status; /* Check if the fmtp has octet-align field. */ p = pj_stristr(&fmtp.fmt_param, &STR_OCTET_ALIGN); if (p) { /* It has, just toggle the value */ unsigned octet_align; pj_str_t s; pj_strset(&s, p + STR_OCTET_ALIGN.slen, fmtp.fmt_param.slen - (p - fmtp.fmt_param.ptr) - STR_OCTET_ALIGN.slen); octet_align = pj_strtoul(&s); *(p + STR_OCTET_ALIGN.slen) = (char)(octet_align? '0' : '1'); } else { /* It doesn't, append octet-align field */ char buf[MAX_FMTP_STR_LEN]; pj_ansi_snprintf(buf, MAX_FMTP_STR_LEN, "%.*s;octet-align=1", (int)fmtp.fmt_param.slen, fmtp.fmt_param.ptr); attr->value = pj_strdup3(pool, buf); } } else { /* Add new attribute for the AMR media format with octet-align * field set. */ char buf[MAX_FMTP_STR_LEN]; attr = PJ_POOL_ZALLOC_T(pool, pjmedia_sdp_attr); attr->name = pj_str("fmtp"); pj_ansi_snprintf(buf, MAX_FMTP_STR_LEN, "%.*s octet-align=1", (int)media->desc.fmt[fmt_idx].slen, media->desc.fmt[fmt_idx].ptr); attr->value = pj_strdup3(pool, buf); media->attr[media->attr_count++] = attr; } return PJ_SUCCESS; }
/* Matching G722.1 bitrates between offer and answer. */ static pj_bool_t match_g7221( const pjmedia_sdp_media *offer, unsigned o_fmt_idx, const pjmedia_sdp_media *answer, unsigned a_fmt_idx) { const pjmedia_sdp_attr *a_ans; const pjmedia_sdp_attr *a_off; pjmedia_sdp_fmtp fmtp; unsigned a_bitrate = 0, o_bitrate = 0; const pj_str_t bitrate = {"bitrate=", 8}; const char *p; a_ans = pjmedia_sdp_media_find_attr2(answer, "fmtp", &answer->desc.fmt[a_fmt_idx]); if (!a_ans) return PJ_FALSE; if (pjmedia_sdp_attr_get_fmtp(a_ans, &fmtp) != PJ_SUCCESS) return PJ_FALSE; p = pj_stristr(&fmtp.fmt_param, &bitrate); if (p == NULL) return PJ_FALSE; a_bitrate = atoi(p + bitrate.slen); a_off = pjmedia_sdp_media_find_attr2(offer, "fmtp", &offer->desc.fmt[o_fmt_idx]); if (!a_off) return PJ_FALSE; if (pjmedia_sdp_attr_get_fmtp(a_off, &fmtp) != PJ_SUCCESS) return PJ_FALSE; p = pj_stristr(&fmtp.fmt_param, &bitrate); if (p == NULL) return PJ_FALSE; o_bitrate = atoi(p + bitrate.slen); return (a_bitrate == o_bitrate); }
/* Matching G722.1 bitrates between offer and answer. */ static pj_bool_t match_g7221( const pjmedia_sdp_media *offer, unsigned o_fmt_idx, const pjmedia_sdp_media *answer, unsigned a_fmt_idx) { const pjmedia_sdp_attr *attr_ans; const pjmedia_sdp_attr *attr_ofr; pjmedia_sdp_fmtp fmtp; unsigned a_bitrate, o_bitrate; const pj_str_t bitrate = {"bitrate=", 8}; /* Parse offer */ attr_ofr = pjmedia_sdp_media_find_attr2(offer, "fmtp", &offer->desc.fmt[o_fmt_idx]); if (!attr_ofr) return PJ_FALSE; if (pjmedia_sdp_attr_get_fmtp(attr_ofr, &fmtp) != PJ_SUCCESS) return PJ_FALSE; GET_FMTP_IVAL(o_bitrate, fmtp, bitrate, 0); /* Parse answer */ attr_ans = pjmedia_sdp_media_find_attr2(answer, "fmtp", &answer->desc.fmt[a_fmt_idx]); if (!attr_ans) return PJ_FALSE; if (pjmedia_sdp_attr_get_fmtp(attr_ans, &fmtp) != PJ_SUCCESS) return PJ_FALSE; GET_FMTP_IVAL(a_bitrate, fmtp, bitrate, 0); /* Compare bitrate in answer and offer. */ return (a_bitrate == o_bitrate); }
/* * Get fmtp mode parameter associated with the codec. */ static pj_status_t get_fmtp_mode(const pjmedia_sdp_media *m, const pj_str_t *fmt, int *p_mode) { const pjmedia_sdp_attr *attr; pjmedia_sdp_fmtp fmtp; const pj_str_t str_mode = { "mode=", 5 }; char *pos; /* Get "fmtp" attribute for the format */ attr = pjmedia_sdp_media_find_attr2(m, "fmtp", fmt); if (attr == NULL) return -1; /* Parse "fmtp" attribute */ if (pjmedia_sdp_attr_get_fmtp(attr, &fmtp) != PJ_SUCCESS) return -1; /* Look for "mode=" string in the fmtp */ while (fmtp.fmt_param.slen >= str_mode.slen + 1) { if (pj_strnicmp(&fmtp.fmt_param, &str_mode, str_mode.slen)==0) { /* Found "mode=" string */ break; } fmtp.fmt_param.ptr++; fmtp.fmt_param.slen--; } if (fmtp.fmt_param.slen < str_mode.slen + 1) { /* "mode=" param not found */ return -1; } /* Get the mode */ pos = fmtp.fmt_param.ptr + str_mode.slen; *p_mode = 0; while (pj_isdigit(*pos)) { *p_mode = *p_mode * 10 + (*pos - '0'); ++pos; } return PJ_SUCCESS; }
/* Try to match offer with answer. */ static pj_status_t match_offer(pj_pool_t *pool, const pjmedia_sdp_media *offer, const pjmedia_sdp_media *preanswer, const pjmedia_sdp_media *orig_local, pjmedia_sdp_media **p_answer) { unsigned i; pj_bool_t offer_has_codec = 0, offer_has_telephone_event = 0, offer_has_other = 0, found_matching_codec = 0, found_matching_telephone_event = 0, found_matching_other = 0; unsigned pt_answer_count = 0; pj_str_t pt_answer[PJMEDIA_MAX_SDP_FMT]; pjmedia_sdp_media *answer; /* With the addition of telephone-event and dodgy MS RTC SDP, * the answer generation algorithm looks really shitty... */ for (i=0; i<offer->desc.fmt_count; ++i) { unsigned j; if (pj_isdigit(*offer->desc.fmt[i].ptr)) { /* This is normal/standard payload type, where it's identified * by payload number. */ unsigned pt; pt = pj_strtoul(&offer->desc.fmt[i]); if (pt < 96) { /* For static payload type, it's enough to compare just * the payload number. */ offer_has_codec = 1; /* We just need to select one codec. * Continue if we have selected matching codec for previous * payload. */ if (found_matching_codec) continue; /* Find matching codec in local descriptor. */ for (j=0; j<preanswer->desc.fmt_count; ++j) { unsigned p; p = pj_strtoul(&preanswer->desc.fmt[j]); if (p == pt && pj_isdigit(*preanswer->desc.fmt[j].ptr)) { found_matching_codec = 1; pt_answer[pt_answer_count++] = preanswer->desc.fmt[j]; break; } } } else { /* This is dynamic payload type. * For dynamic payload type, we must look the rtpmap and * compare the encoding name. */ const pjmedia_sdp_attr *a; pjmedia_sdp_rtpmap or_; pj_bool_t is_codec; /* Get the rtpmap for the payload type in the offer. */ a = pjmedia_sdp_media_find_attr2(offer, "rtpmap", &offer->desc.fmt[i]); if (!a) { pj_assert(!"Bug! Offer should have been validated"); return PJMEDIA_SDP_EMISSINGRTPMAP; } pjmedia_sdp_attr_get_rtpmap(a, &or_); if (!pj_strcmp2(&or_.enc_name, "telephone-event")) { offer_has_telephone_event = 1; if (found_matching_telephone_event) continue; is_codec = 0; } else { offer_has_codec = 1; if (found_matching_codec) continue; is_codec = 1; } /* Find paylaod in our initial SDP with matching * encoding name and clock rate. */ for (j=0; j<preanswer->desc.fmt_count; ++j) { a = pjmedia_sdp_media_find_attr2(preanswer, "rtpmap", &preanswer->desc.fmt[j]); if (a) { pjmedia_sdp_rtpmap lr; pjmedia_sdp_attr_get_rtpmap(a, &lr); /* See if encoding name, clock rate, and * channel count match */ if (!pj_stricmp(&or_.enc_name, &lr.enc_name) && or_.clock_rate == lr.clock_rate && (pj_strcmp(&or_.param, &lr.param)==0 || (or_.param.slen==1 && *or_.param.ptr=='1'))) { /* Match! */ if (is_codec) { /* Further check for G7221, negotiate bitrate. */ if (pj_strcmp2(&or_.enc_name, "G7221") == 0 && match_g7221(offer, i, preanswer, j) == 0) { continue; } found_matching_codec = 1; } else { found_matching_telephone_event = 1; } pt_answer[pt_answer_count++] = preanswer->desc.fmt[j]; break; } } } } } else { /* This is a non-standard, brain damaged SDP where the payload * type is non-numeric. It exists e.g. in Microsoft RTC based * UA, to indicate instant messaging capability. * Example: * - m=x-ms-message 5060 sip null */ offer_has_other = 1; if (found_matching_other) continue; for (j=0; j<preanswer->desc.fmt_count; ++j) { if (!pj_strcmp(&offer->desc.fmt[i], &preanswer->desc.fmt[j])) { /* Match */ found_matching_other = 1; pt_answer[pt_answer_count++] = preanswer->desc.fmt[j]; break; } } } } /* See if all types of offer can be matched. */ if (offer_has_codec && !found_matching_codec) { return PJMEDIA_SDPNEG_NOANSCODEC; } /* If this comment is removed, negotiation will fail if remote has offered telephone-event and local is not configured with telephone-event if (offer_has_telephone_event && !found_matching_telephone_event) { return PJMEDIA_SDPNEG_NOANSTELEVENT; } */ if (offer_has_other && !found_matching_other) { return PJMEDIA_SDPNEG_NOANSUNKNOWN; } /* Seems like everything is in order. * Build the answer by cloning from local media, but rearrange the payload * to suit the offer. */ answer = pjmedia_sdp_media_clone(pool, orig_local); for (i=0; i<pt_answer_count; ++i) { unsigned j; for (j=i; j<answer->desc.fmt_count; ++j) { if (!pj_strcmp(&answer->desc.fmt[j], &pt_answer[i])) break; } pj_assert(j != answer->desc.fmt_count); str_swap(&answer->desc.fmt[i], &answer->desc.fmt[j]); } /* Remove unwanted local formats. */ for (i=pt_answer_count; i<answer->desc.fmt_count; ++i) { pjmedia_sdp_attr *a; /* Remove rtpmap for this format */ a = pjmedia_sdp_media_find_attr2(answer, "rtpmap", &answer->desc.fmt[i]); if (a) { pjmedia_sdp_media_remove_attr(answer, a); } /* Remove fmtp for this format */ a = pjmedia_sdp_media_find_attr2(answer, "fmtp", &answer->desc.fmt[i]); if (a) { pjmedia_sdp_media_remove_attr(answer, a); } } answer->desc.fmt_count = pt_answer_count; /* If offer has zero port, set our answer with zero port too */ if (offer->desc.port==0) answer->desc.port = 0; /* Update media direction. */ update_media_direction(pool, offer, answer); *p_answer = answer; return PJ_SUCCESS; }
/* Update single local media description to after receiving answer * from remote. */ static pj_status_t process_m_answer( pj_pool_t *pool, pjmedia_sdp_media *offer, pjmedia_sdp_media *answer, pj_bool_t allow_asym) { unsigned i; /* Check that the media type match our offer. */ if (pj_strcmp(&answer->desc.media, &offer->desc.media)!=0) { /* The media type in the answer is different than the offer! */ return PJMEDIA_SDPNEG_EINVANSMEDIA; } /* Check that transport in the answer match our offer. */ /* At this point, transport type must be compatible, * the transport instance will do more validation later. */ if (pjmedia_sdp_transport_cmp(&answer->desc.transport, &offer->desc.transport) != PJ_SUCCESS) { return PJMEDIA_SDPNEG_EINVANSTP; } /* Check if remote has rejected our offer */ if (answer->desc.port == 0) { /* Remote has rejected our offer. * Set our port to zero too in active SDP. */ offer->desc.port = 0; } /* Process direction attributes */ update_media_direction(pool, answer, offer); /* If asymetric media is allowed, then just check that remote answer has * codecs that are within the offer. * * Otherwise if asymetric media is not allowed, then we will choose only * one codec in our initial offer to match the answer. */ if (allow_asym) { for (i=0; i<answer->desc.fmt_count; ++i) { unsigned j; pj_str_t *rem_fmt = &answer->desc.fmt[i]; for (j=0; j<offer->desc.fmt_count; ++j) { if (pj_strcmp(rem_fmt, &answer->desc.fmt[j])==0) break; } if (j != offer->desc.fmt_count) { /* Found at least one common codec. */ break; } } if (i == answer->desc.fmt_count) { /* No common codec in the answer! */ return PJMEDIA_SDPNEG_EANSNOMEDIA; } PJ_TODO(CHECK_SDP_NEGOTIATION_WHEN_ASYMETRIC_MEDIA_IS_ALLOWED); } else { /* Remove all format in the offer that has no matching answer */ for (i=0; i<offer->desc.fmt_count;) { unsigned pt; pj_uint32_t j; pj_str_t *fmt = &offer->desc.fmt[i]; /* Find matching answer */ pt = pj_strtoul(fmt); if (pt < 96) { for (j=0; j<answer->desc.fmt_count; ++j) { if (pj_strcmp(fmt, &answer->desc.fmt[j])==0) break; } } else { /* This is dynamic payload type. * For dynamic payload type, we must look the rtpmap and * compare the encoding name. */ const pjmedia_sdp_attr *a; pjmedia_sdp_rtpmap or_; /* Get the rtpmap for the payload type in the offer. */ a = pjmedia_sdp_media_find_attr2(offer, "rtpmap", fmt); if (!a) { pj_assert(!"Bug! Offer should have been validated"); return PJ_EBUG; } pjmedia_sdp_attr_get_rtpmap(a, &or_); /* Find paylaod in answer SDP with matching * encoding name and clock rate. */ for (j=0; j<answer->desc.fmt_count; ++j) { a = pjmedia_sdp_media_find_attr2(answer, "rtpmap", &answer->desc.fmt[j]); if (a) { pjmedia_sdp_rtpmap ar; pjmedia_sdp_attr_get_rtpmap(a, &ar); /* See if encoding name, clock rate, and channel * count match */ if (!pj_stricmp(&or_.enc_name, &ar.enc_name) && or_.clock_rate == ar.clock_rate && (pj_stricmp(&or_.param, &ar.param)==0 || (ar.param.slen==1 && *ar.param.ptr=='1'))) { /* Further check for G7221, negotiate bitrate. */ if (pj_strcmp2(&or_.enc_name, "G7221") == 0) { if (match_g7221(offer, i, answer, j)) break; } else { /* Match! */ break; } } } } } if (j == answer->desc.fmt_count) { /* This format has no matching answer. * Remove it from our offer. */ pjmedia_sdp_attr *a; /* Remove rtpmap associated with this format */ a = pjmedia_sdp_media_find_attr2(offer, "rtpmap", fmt); if (a) pjmedia_sdp_media_remove_attr(offer, a); /* Remove fmtp associated with this format */ a = pjmedia_sdp_media_find_attr2(offer, "fmtp", fmt); if (a) pjmedia_sdp_media_remove_attr(offer, a); /* Remove this format from offer's array */ pj_array_erase(offer->desc.fmt, sizeof(offer->desc.fmt[0]), offer->desc.fmt_count, i); --offer->desc.fmt_count; } else { ++i; } } /* Arrange format in the offer so the order match the priority * in the answer */ for (i=0; i<answer->desc.fmt_count; ++i) { unsigned j; pj_str_t *fmt = &answer->desc.fmt[i]; for (j=i; j<offer->desc.fmt_count; ++j) { if (pj_strcmp(fmt, &offer->desc.fmt[j])==0) { str_swap(&offer->desc.fmt[i], &offer->desc.fmt[j]); break; } } } } /* Looks okay */ return PJ_SUCCESS; }
/* Update media direction based on peer's media direction */ static void update_media_direction(pj_pool_t *pool, const pjmedia_sdp_media *remote, pjmedia_sdp_media *local) { pjmedia_dir old_dir = PJMEDIA_DIR_ENCODING_DECODING, new_dir; /* Get the media direction of local SDP */ if (pjmedia_sdp_media_find_attr2(local, "sendonly", NULL)) old_dir = PJMEDIA_DIR_ENCODING; else if (pjmedia_sdp_media_find_attr2(local, "recvonly", NULL)) old_dir = PJMEDIA_DIR_DECODING; else if (pjmedia_sdp_media_find_attr2(local, "inactive", NULL)) old_dir = PJMEDIA_DIR_NONE; new_dir = old_dir; /* Adjust local media direction based on remote media direction */ if (pjmedia_sdp_media_find_attr2(remote, "inactive", NULL) != NULL) { /* If remote has "a=inactive", then local is inactive too */ new_dir = PJMEDIA_DIR_NONE; } else if(pjmedia_sdp_media_find_attr2(remote, "sendonly", NULL) != NULL) { /* If remote has "a=sendonly", then set local to "recvonly" if * it is currently "sendrecv". Otherwise if local is NOT "recvonly", * then set local direction to "inactive". */ switch (old_dir) { case PJMEDIA_DIR_ENCODING_DECODING: new_dir = PJMEDIA_DIR_DECODING; break; case PJMEDIA_DIR_DECODING: /* No change */ break; default: new_dir = PJMEDIA_DIR_NONE; break; } } else if(pjmedia_sdp_media_find_attr2(remote, "recvonly", NULL) != NULL) { /* If remote has "a=recvonly", then set local to "sendonly" if * it is currently "sendrecv". Otherwise if local is NOT "sendonly", * then set local direction to "inactive" */ switch (old_dir) { case PJMEDIA_DIR_ENCODING_DECODING: new_dir = PJMEDIA_DIR_ENCODING; break; case PJMEDIA_DIR_ENCODING: /* No change */ break; default: new_dir = PJMEDIA_DIR_NONE; break; } } else { /* Remote indicates "sendrecv" capability. No change to local * direction */ } if (new_dir != old_dir) { pjmedia_sdp_attr *a = NULL; remove_all_media_directions(local); switch (new_dir) { case PJMEDIA_DIR_NONE: a = pjmedia_sdp_attr_create(pool, "inactive", NULL); break; case PJMEDIA_DIR_ENCODING: a = pjmedia_sdp_attr_create(pool, "sendonly", NULL); break; case PJMEDIA_DIR_DECODING: a = pjmedia_sdp_attr_create(pool, "recvonly", NULL); break; default: /* sendrecv */ break; } if (a) { pjmedia_sdp_media_add_attr(local, a); } } }
PJ_DEF(pj_status_t) pjmedia_codec_amr_match_sdp( pj_pool_t *pool, pjmedia_sdp_media *offer, unsigned o_fmt_idx, pjmedia_sdp_media *answer, unsigned a_fmt_idx, unsigned option) { /* Negotiated format-param field-names constants. */ const pj_str_t STR_OCTET_ALIGN = {"octet-align=", 12}; const pj_str_t STR_CRC = {"crc=", 4}; const pj_str_t STR_ROBUST_SORTING = {"robust-sorting=", 15}; const pj_str_t STR_INTERLEAVING = {"interleaving=", 13}; /* Negotiated params and their default values. */ unsigned a_octet_align = 0, o_octet_align = 0; unsigned a_crc = 0, o_crc = 0; unsigned a_robust_sorting = 0, o_robust_sorting = 0; unsigned a_interleaving = 0, o_interleaving = 0; const pjmedia_sdp_attr *attr_ans; const pjmedia_sdp_attr *attr_ofr; pjmedia_sdp_fmtp fmtp; pj_status_t status; /* Parse offerer FMTP */ attr_ofr = pjmedia_sdp_media_find_attr2(offer, "fmtp", &offer->desc.fmt[o_fmt_idx]); if (attr_ofr) { status = pjmedia_sdp_attr_get_fmtp(attr_ofr, &fmtp); if (status != PJ_SUCCESS) return status; GET_FMTP_IVAL(o_octet_align, fmtp, STR_OCTET_ALIGN, 0); GET_FMTP_IVAL(o_crc, fmtp, STR_CRC, 0); GET_FMTP_IVAL(o_robust_sorting, fmtp, STR_ROBUST_SORTING, 0); GET_FMTP_IVAL(o_interleaving, fmtp, STR_INTERLEAVING, 0); } /* Parse answerer FMTP */ attr_ans = pjmedia_sdp_media_find_attr2(answer, "fmtp", &answer->desc.fmt[a_fmt_idx]); if (attr_ans) { status = pjmedia_sdp_attr_get_fmtp(attr_ans, &fmtp); if (status != PJ_SUCCESS) return status; GET_FMTP_IVAL(a_octet_align, fmtp, STR_OCTET_ALIGN, 0); GET_FMTP_IVAL(a_crc, fmtp, STR_CRC, 0); GET_FMTP_IVAL(a_robust_sorting, fmtp, STR_ROBUST_SORTING, 0); GET_FMTP_IVAL(a_interleaving, fmtp, STR_INTERLEAVING, 0); } /* First, match crc, robust-sorting, interleaving settings */ if (a_crc != o_crc || a_robust_sorting != o_robust_sorting || a_interleaving != o_interleaving) { return PJMEDIA_SDP_EFORMATNOTEQUAL; } /* Match octet-align setting */ if (a_octet_align != o_octet_align) { /* Check if answer can be modified to match to the offer */ if (option & PJMEDIA_SDP_NEG_FMT_MATCH_ALLOW_MODIFY_ANSWER) { status = amr_toggle_octet_align(pool, answer, a_fmt_idx); return status; } else { return PJMEDIA_SDP_EFORMATNOTEQUAL; } } return PJ_SUCCESS; }
/* * Parse fmtp for specified format/payload type. */ static void parse_fmtp( pj_pool_t *pool, const pjmedia_sdp_media *m, unsigned pt, pjmedia_codec_fmtp *fmtp) { const pjmedia_sdp_attr *attr; pjmedia_sdp_fmtp sdp_fmtp; char *p, *p_end, fmt_buf[8]; pj_str_t fmt; pj_assert(m && fmtp); pj_bzero(fmtp, sizeof(pjmedia_codec_fmtp)); /* Get "fmtp" attribute for the format */ pj_ansi_sprintf(fmt_buf, "%d", pt); fmt = pj_str(fmt_buf); attr = pjmedia_sdp_media_find_attr2(m, "fmtp", &fmt); if (attr == NULL) return; /* Parse "fmtp" attribute */ if (pjmedia_sdp_attr_get_fmtp(attr, &sdp_fmtp) != PJ_SUCCESS) return; /* Prepare parsing */ p = sdp_fmtp.fmt_param.ptr; p_end = p + sdp_fmtp.fmt_param.slen; /* Parse */ while (p < p_end) { char *token, *start, *end; /* Skip whitespaces */ while (p < p_end && (*p == ' ' || *p == '\t')) ++p; if (p == p_end) break; /* Get token */ start = p; while (p < p_end && *p != ';' && *p != '=') ++p; end = p - 1; /* Right trim */ while (end >= start && (*end == ' ' || *end == '\t' || *end == '\r' || *end == '\n' )) --end; /* Forward a char after trimming */ ++end; /* Store token */ if (end > start) { token = (char*)pj_pool_alloc(pool, end - start); pj_ansi_strncpy(token, start, end - start); if (*p == '=') /* Got param name */ pj_strset(&fmtp->param[fmtp->cnt].name, token, end - start); else /* Got param value */ pj_strset(&fmtp->param[fmtp->cnt++].val, token, end - start); } else if (*p != '=') { ++fmtp->cnt; } /* Next */ ++p; } }
/* Negotiate AMR format params between offer and answer. Format params * to be matched are: octet-align, crc, robust-sorting, interleaving, * and channels (channels is negotiated by rtpmap line negotiation). * Note: For answerer, octet-align mode setting is adaptable to offerer * setting. In the case that octet-align mode need to be adjusted, * pt_need_adapt will be set to the format ID. * */ static pj_bool_t match_amr( const pjmedia_sdp_media *offer, unsigned o_fmt_idx, const pjmedia_sdp_media *answer, unsigned a_fmt_idx, pj_bool_t answerer, pj_str_t *pt_need_adapt) { /* Negotiated format-param field-names constants. */ const pj_str_t STR_OCTET_ALIGN = {"octet-align=", 12}; const pj_str_t STR_CRC = {"crc=", 4}; const pj_str_t STR_ROBUST_SORTING = {"robust-sorting=", 15}; const pj_str_t STR_INTERLEAVING = {"interleaving=", 13}; /* Negotiated params and their default values. */ unsigned a_octet_align = 0, o_octet_align = 0; unsigned a_crc = 0, o_crc = 0; unsigned a_robust_sorting = 0, o_robust_sorting = 0; unsigned a_interleaving = 0, o_interleaving = 0; const pjmedia_sdp_attr *attr_ans; const pjmedia_sdp_attr *attr_ofr; pjmedia_sdp_fmtp fmtp; pj_bool_t match; /* Parse offerer FMTP */ attr_ofr = pjmedia_sdp_media_find_attr2(offer, "fmtp", &offer->desc.fmt[o_fmt_idx]); if (attr_ofr) { if (pjmedia_sdp_attr_get_fmtp(attr_ofr, &fmtp) != PJ_SUCCESS) /* Invalid fmtp format. */ return PJ_FALSE; GET_FMTP_IVAL(o_octet_align, fmtp, STR_OCTET_ALIGN, 0); GET_FMTP_IVAL(o_crc, fmtp, STR_CRC, 0); GET_FMTP_IVAL(o_robust_sorting, fmtp, STR_ROBUST_SORTING, 0); GET_FMTP_IVAL(o_interleaving, fmtp, STR_INTERLEAVING, 0); } /* Parse answerer FMTP */ attr_ans = pjmedia_sdp_media_find_attr2(answer, "fmtp", &answer->desc.fmt[a_fmt_idx]); if (attr_ans) { if (pjmedia_sdp_attr_get_fmtp(attr_ans, &fmtp) != PJ_SUCCESS) /* Invalid fmtp format. */ return PJ_FALSE; GET_FMTP_IVAL(a_octet_align, fmtp, STR_OCTET_ALIGN, 0); GET_FMTP_IVAL(a_crc, fmtp, STR_CRC, 0); GET_FMTP_IVAL(a_robust_sorting, fmtp, STR_ROBUST_SORTING, 0); GET_FMTP_IVAL(a_interleaving, fmtp, STR_INTERLEAVING, 0); } if (answerer) { match = a_crc == o_crc && a_robust_sorting == o_robust_sorting && a_interleaving == o_interleaving; /* If answerer octet-align setting doesn't match to the offerer's, * set pt_need_adapt to this media format ID to signal the caller * that this format ID needs to be adjusted. */ if (a_octet_align != o_octet_align && match) { pj_assert(pt_need_adapt != NULL); *pt_need_adapt = answer->desc.fmt[a_fmt_idx]; } } else { match = (a_octet_align == o_octet_align && a_crc == o_crc && a_robust_sorting == o_robust_sorting && a_interleaving == o_interleaving); } return match; }
static void get_codecs(struct ast_sip_session *session, const struct pjmedia_sdp_media *stream, struct ast_rtp_codecs *codecs, struct ast_sip_session_media *session_media) { pjmedia_sdp_attr *attr; pjmedia_sdp_rtpmap *rtpmap; pjmedia_sdp_fmtp fmtp; struct ast_format *format; int i, num = 0, tel_event = 0; char name[256]; char media[20]; char fmt_param[256]; enum ast_rtp_options options = session->endpoint->media.g726_non_standard ? AST_RTP_OPT_G726_NONSTANDARD : 0; ast_rtp_codecs_payloads_initialize(codecs); /* Iterate through provided formats */ for (i = 0; i < stream->desc.fmt_count; ++i) { /* The payload is kept as a string for things like t38 but for video it is always numerical */ ast_rtp_codecs_payloads_set_m_type(codecs, NULL, pj_strtoul(&stream->desc.fmt[i])); /* Look for the optional rtpmap attribute */ if (!(attr = pjmedia_sdp_media_find_attr2(stream, "rtpmap", &stream->desc.fmt[i]))) { continue; } /* Interpret the attribute as an rtpmap */ if ((pjmedia_sdp_attr_to_rtpmap(session->inv_session->pool_prov, attr, &rtpmap)) != PJ_SUCCESS) { continue; } ast_copy_pj_str(name, &rtpmap->enc_name, sizeof(name)); if (strcmp(name,"telephone-event") == 0) { tel_event++; } ast_copy_pj_str(media, (pj_str_t*)&stream->desc.media, sizeof(media)); ast_rtp_codecs_payloads_set_rtpmap_type_rate(codecs, NULL, pj_strtoul(&stream->desc.fmt[i]), media, name, options, rtpmap->clock_rate); /* Look for an optional associated fmtp attribute */ if (!(attr = pjmedia_sdp_media_find_attr2(stream, "fmtp", &rtpmap->pt))) { continue; } if ((pjmedia_sdp_attr_get_fmtp(attr, &fmtp)) == PJ_SUCCESS) { ast_copy_pj_str(fmt_param, &fmtp.fmt, sizeof(fmt_param)); if (sscanf(fmt_param, "%30d", &num) != 1) { continue; } if ((format = ast_rtp_codecs_get_payload_format(codecs, num))) { struct ast_format *format_parsed; ast_copy_pj_str(fmt_param, &fmtp.fmt_param, sizeof(fmt_param)); format_parsed = ast_format_parse_sdp_fmtp(format, fmt_param); if (format_parsed) { ast_rtp_codecs_payload_replace_format(codecs, num, format_parsed); ao2_ref(format_parsed, -1); } ao2_ref(format, -1); } } } if ((tel_event==0) && (session->endpoint->dtmf == AST_SIP_DTMF_AUTO)) { ast_rtp_instance_dtmf_mode_set(session_media->rtp, AST_RTP_DTMF_MODE_INBAND); } /* Get the packetization, if it exists */ if ((attr = pjmedia_sdp_media_find_attr2(stream, "ptime", NULL))) { unsigned long framing = pj_strtoul(pj_strltrim(&attr->value)); if (framing && session->endpoint->media.rtp.use_ptime) { ast_rtp_codecs_set_framing(codecs, framing); } } }
/* Try to match offer with answer. */ static pj_status_t match_offer(pj_pool_t *pool, pj_bool_t prefer_remote_codec_order, pj_bool_t answer_with_multiple_codecs, const pjmedia_sdp_media *offer, const pjmedia_sdp_media *preanswer, const pjmedia_sdp_session *preanswer_sdp, pjmedia_sdp_media **p_answer) { unsigned i; pj_bool_t master_has_codec = 0, master_has_other = 0, found_matching_codec = 0, found_matching_telephone_event = 0, found_matching_other = 0; unsigned pt_answer_count = 0; pj_str_t pt_answer[PJMEDIA_MAX_SDP_FMT]; pj_str_t pt_offer[PJMEDIA_MAX_SDP_FMT]; pjmedia_sdp_media *answer; const pjmedia_sdp_media *master, *slave; /* If offer has zero port, just clone the offer */ if (offer->desc.port == 0) { answer = sdp_media_clone_deactivate(pool, offer, preanswer, preanswer_sdp); *p_answer = answer; return PJ_SUCCESS; } /* If the preanswer define zero port, this media is being rejected, * just clone the preanswer. */ if (preanswer->desc.port == 0) { answer = pjmedia_sdp_media_clone(pool, preanswer); *p_answer = answer; return PJ_SUCCESS; } /* Set master/slave negotiator based on prefer_remote_codec_order. */ if (prefer_remote_codec_order) { master = offer; slave = preanswer; } else { master = preanswer; slave = offer; } /* With the addition of telephone-event and dodgy MS RTC SDP, * the answer generation algorithm looks really shitty... */ for (i=0; i<master->desc.fmt_count; ++i) { unsigned j; if (pj_isdigit(*master->desc.fmt[i].ptr)) { /* This is normal/standard payload type, where it's identified * by payload number. */ unsigned pt; pt = pj_strtoul(&master->desc.fmt[i]); if (pt < 96) { /* For static payload type, it's enough to compare just * the payload number. */ master_has_codec = 1; /* We just need to select one codec if not allowing multiple. * Continue if we have selected matching codec for previous * payload. */ if (!answer_with_multiple_codecs && found_matching_codec) continue; /* Find matching codec in local descriptor. */ for (j=0; j<slave->desc.fmt_count; ++j) { unsigned p; p = pj_strtoul(&slave->desc.fmt[j]); if (p == pt && pj_isdigit(*slave->desc.fmt[j].ptr)) { found_matching_codec = 1; pt_offer[pt_answer_count] = slave->desc.fmt[j]; pt_answer[pt_answer_count++] = slave->desc.fmt[j]; break; } } } else { /* This is dynamic payload type. * For dynamic payload type, we must look the rtpmap and * compare the encoding name. */ const pjmedia_sdp_attr *a; pjmedia_sdp_rtpmap or_; pj_bool_t is_codec; /* Get the rtpmap for the payload type in the master. */ a = pjmedia_sdp_media_find_attr2(master, "rtpmap", &master->desc.fmt[i]); if (!a) { pj_assert(!"Bug! Offer should have been validated"); return PJMEDIA_SDP_EMISSINGRTPMAP; } pjmedia_sdp_attr_get_rtpmap(a, &or_); if (!pj_stricmp2(&or_.enc_name, "telephone-event")) { if (found_matching_telephone_event) continue; is_codec = 0; } else { master_has_codec = 1; if (!answer_with_multiple_codecs && found_matching_codec) continue; is_codec = 1; } /* Find paylaod in our initial SDP with matching * encoding name and clock rate. */ for (j=0; j<slave->desc.fmt_count; ++j) { a = pjmedia_sdp_media_find_attr2(slave, "rtpmap", &slave->desc.fmt[j]); if (a) { pjmedia_sdp_rtpmap lr; pjmedia_sdp_attr_get_rtpmap(a, &lr); /* See if encoding name, clock rate, and * channel count match */ if (!pj_stricmp(&or_.enc_name, &lr.enc_name) && or_.clock_rate == lr.clock_rate && (pj_stricmp(&or_.param, &lr.param)==0 || (lr.param.slen==0 && or_.param.slen==1 && *or_.param.ptr=='1') || (or_.param.slen==0 && lr.param.slen==1 && *lr.param.ptr=='1'))) { /* Match! */ if (is_codec) { pjmedia_sdp_media *o_med, *a_med; unsigned o_fmt_idx, a_fmt_idx; o_med = (pjmedia_sdp_media*)offer; a_med = (pjmedia_sdp_media*)preanswer; o_fmt_idx = prefer_remote_codec_order? i:j; a_fmt_idx = prefer_remote_codec_order? j:i; /* Call custom format matching callbacks */ if (custom_fmt_match(pool, &or_.enc_name, o_med, o_fmt_idx, a_med, a_fmt_idx, ALLOW_MODIFY_ANSWER) != PJ_SUCCESS) { continue; } found_matching_codec = 1; } else { found_matching_telephone_event = 1; } pt_offer[pt_answer_count] = prefer_remote_codec_order? offer->desc.fmt[i]: offer->desc.fmt[j]; pt_answer[pt_answer_count++] = prefer_remote_codec_order? preanswer->desc.fmt[j]: preanswer->desc.fmt[i]; break; } } } } } else { /* This is a non-standard, brain damaged SDP where the payload * type is non-numeric. It exists e.g. in Microsoft RTC based * UA, to indicate instant messaging capability. * Example: * - m=x-ms-message 5060 sip null */ master_has_other = 1; if (found_matching_other) continue; for (j=0; j<slave->desc.fmt_count; ++j) { if (!pj_strcmp(&master->desc.fmt[i], &slave->desc.fmt[j])) { /* Match */ found_matching_other = 1; pt_offer[pt_answer_count] = prefer_remote_codec_order? offer->desc.fmt[i]: offer->desc.fmt[j]; pt_answer[pt_answer_count++] = prefer_remote_codec_order? preanswer->desc.fmt[j]: preanswer->desc.fmt[i]; break; } } } } /* See if all types of master can be matched. */ if (master_has_codec && !found_matching_codec) { return PJMEDIA_SDPNEG_NOANSCODEC; } /* If this comment is removed, negotiation will fail if remote has offered telephone-event and local is not configured with telephone-event if (offer_has_telephone_event && !found_matching_telephone_event) { return PJMEDIA_SDPNEG_NOANSTELEVENT; } */ if (master_has_other && !found_matching_other) { return PJMEDIA_SDPNEG_NOANSUNKNOWN; } /* Seems like everything is in order. * Build the answer by cloning from preanswer, but rearrange the payload * to suit the offer. */ answer = pjmedia_sdp_media_clone(pool, preanswer); for (i=0; i<pt_answer_count; ++i) { unsigned j; for (j=i; j<answer->desc.fmt_count; ++j) { if (!pj_strcmp(&answer->desc.fmt[j], &pt_answer[i])) break; } pj_assert(j != answer->desc.fmt_count); str_swap(&answer->desc.fmt[i], &answer->desc.fmt[j]); } /* Remove unwanted local formats. */ for (i=pt_answer_count; i<answer->desc.fmt_count; ++i) { pjmedia_sdp_attr *a; /* Remove rtpmap for this format */ a = pjmedia_sdp_media_find_attr2(answer, "rtpmap", &answer->desc.fmt[i]); if (a) { pjmedia_sdp_media_remove_attr(answer, a); } /* Remove fmtp for this format */ a = pjmedia_sdp_media_find_attr2(answer, "fmtp", &answer->desc.fmt[i]); if (a) { pjmedia_sdp_media_remove_attr(answer, a); } } answer->desc.fmt_count = pt_answer_count; #if PJMEDIA_SDP_NEG_ANSWER_SYMMETRIC_PT apply_answer_symmetric_pt(pool, answer, pt_answer_count, pt_offer, pt_answer); #endif /* Update media direction. */ update_media_direction(pool, offer, answer); *p_answer = answer; return PJ_SUCCESS; }
/*! \brief Function which processes ICE attributes in an audio stream */ static void process_ice_attributes(struct ast_sip_session *session, struct ast_sip_session_media *session_media, const struct pjmedia_sdp_session *remote, const struct pjmedia_sdp_media *remote_stream) { struct ast_rtp_engine_ice *ice; const pjmedia_sdp_attr *attr; char attr_value[256]; unsigned int attr_i; /* If ICE support is not enabled or available exit early */ if (!session->endpoint->media.rtp.ice_support || !(ice = ast_rtp_instance_get_ice(session_media->rtp))) { return; } attr = pjmedia_sdp_media_find_attr2(remote_stream, "ice-ufrag", NULL); if (!attr) { attr = pjmedia_sdp_attr_find2(remote->attr_count, remote->attr, "ice-ufrag", NULL); } if (attr) { ast_copy_pj_str(attr_value, (pj_str_t*)&attr->value, sizeof(attr_value)); ice->set_authentication(session_media->rtp, attr_value, NULL); } else { return; } attr = pjmedia_sdp_media_find_attr2(remote_stream, "ice-pwd", NULL); if (!attr) { attr = pjmedia_sdp_attr_find2(remote->attr_count, remote->attr, "ice-pwd", NULL); } if (attr) { ast_copy_pj_str(attr_value, (pj_str_t*)&attr->value, sizeof(attr_value)); ice->set_authentication(session_media->rtp, NULL, attr_value); } else { return; } if (pjmedia_sdp_media_find_attr2(remote_stream, "ice-lite", NULL)) { ice->ice_lite(session_media->rtp); } /* Find all of the candidates */ for (attr_i = 0; attr_i < remote_stream->attr_count; ++attr_i) { char foundation[32], transport[32], address[PJ_INET6_ADDRSTRLEN + 1], cand_type[6], relay_address[PJ_INET6_ADDRSTRLEN + 1] = ""; unsigned int port, relay_port = 0; struct ast_rtp_engine_ice_candidate candidate = { 0, }; attr = remote_stream->attr[attr_i]; /* If this is not a candidate line skip it */ if (pj_strcmp2(&attr->name, "candidate")) { continue; } ast_copy_pj_str(attr_value, (pj_str_t*)&attr->value, sizeof(attr_value)); if (sscanf(attr_value, "%31s %30u %31s %30u %46s %30u typ %5s %*s %23s %*s %30u", foundation, &candidate.id, transport, (unsigned *)&candidate.priority, address, &port, cand_type, relay_address, &relay_port) < 7) { /* Candidate did not parse properly */ continue; } candidate.foundation = foundation; candidate.transport = transport; ast_sockaddr_parse(&candidate.address, address, PARSE_PORT_FORBID); ast_sockaddr_set_port(&candidate.address, port); if (!strcasecmp(cand_type, "host")) { candidate.type = AST_RTP_ICE_CANDIDATE_TYPE_HOST; } else if (!strcasecmp(cand_type, "srflx")) { candidate.type = AST_RTP_ICE_CANDIDATE_TYPE_SRFLX; } else if (!strcasecmp(cand_type, "relay")) { candidate.type = AST_RTP_ICE_CANDIDATE_TYPE_RELAYED; } else { continue; } if (!ast_strlen_zero(relay_address)) { ast_sockaddr_parse(&candidate.relay_address, relay_address, PARSE_PORT_FORBID); } if (relay_port) { ast_sockaddr_set_port(&candidate.relay_address, relay_port); } ice->add_remote_candidate(session_media->rtp, &candidate); } ice->set_role(session_media->rtp, pjmedia_sdp_neg_was_answer_remote(session->inv_session->neg) == PJ_TRUE ? AST_RTP_ICE_ROLE_CONTROLLING : AST_RTP_ICE_ROLE_CONTROLLED); ice->start(session_media->rtp); }
/* Update single local media description to after receiving answer * from remote. */ static pj_status_t process_m_answer( pj_pool_t *pool, pjmedia_sdp_media *offer, pjmedia_sdp_media *answer, pj_bool_t allow_asym) { unsigned i; /* Check that the media type match our offer. */ if (pj_strcmp(&answer->desc.media, &offer->desc.media)!=0) { /* The media type in the answer is different than the offer! */ return PJMEDIA_SDPNEG_EINVANSMEDIA; } /* Check that transport in the answer match our offer. */ /* At this point, transport type must be compatible, * the transport instance will do more validation later. */ if (pjmedia_sdp_transport_cmp(&answer->desc.transport, &offer->desc.transport) != PJ_SUCCESS) { return PJMEDIA_SDPNEG_EINVANSTP; } /* Check if remote has rejected our offer */ if (answer->desc.port == 0) { /* Remote has rejected our offer. * Deactivate our media too. */ pjmedia_sdp_media_deactivate(pool, offer); /* Don't need to proceed */ return PJ_SUCCESS; } /* Ticket #1148: check if remote answer does not set port to zero when * offered with port zero. Let's just tolerate it. */ if (offer->desc.port == 0) { /* Don't need to proceed */ return PJ_SUCCESS; } /* Process direction attributes */ update_media_direction(pool, answer, offer); /* If asymetric media is allowed, then just check that remote answer has * codecs that are within the offer. * * Otherwise if asymetric media is not allowed, then we will choose only * one codec in our initial offer to match the answer. */ if (allow_asym) { for (i=0; i<answer->desc.fmt_count; ++i) { unsigned j; pj_str_t *rem_fmt = &answer->desc.fmt[i]; for (j=0; j<offer->desc.fmt_count; ++j) { if (pj_strcmp(rem_fmt, &answer->desc.fmt[j])==0) break; } if (j != offer->desc.fmt_count) { /* Found at least one common codec. */ break; } } if (i == answer->desc.fmt_count) { /* No common codec in the answer! */ return PJMEDIA_SDPNEG_EANSNOMEDIA; } PJ_TODO(CHECK_SDP_NEGOTIATION_WHEN_ASYMETRIC_MEDIA_IS_ALLOWED); } else { /* Offer format priority based on answer format index/priority */ unsigned offer_fmt_prior[PJMEDIA_MAX_SDP_FMT]; /* Remove all format in the offer that has no matching answer */ for (i=0; i<offer->desc.fmt_count;) { unsigned pt; pj_uint32_t j; pj_str_t *fmt = &offer->desc.fmt[i]; /* Find matching answer */ pt = pj_strtoul(fmt); if (pt < 96) { for (j=0; j<answer->desc.fmt_count; ++j) { if (pj_strcmp(fmt, &answer->desc.fmt[j])==0) break; } } else { /* This is dynamic payload type. * For dynamic payload type, we must look the rtpmap and * compare the encoding name. */ const pjmedia_sdp_attr *a; pjmedia_sdp_rtpmap or_; /* Get the rtpmap for the payload type in the offer. */ a = pjmedia_sdp_media_find_attr2(offer, "rtpmap", fmt); if (!a) { pj_assert(!"Bug! Offer should have been validated"); return PJ_EBUG; } pjmedia_sdp_attr_get_rtpmap(a, &or_); /* Find paylaod in answer SDP with matching * encoding name and clock rate. */ for (j=0; j<answer->desc.fmt_count; ++j) { a = pjmedia_sdp_media_find_attr2(answer, "rtpmap", &answer->desc.fmt[j]); if (a) { pjmedia_sdp_rtpmap ar; pjmedia_sdp_attr_get_rtpmap(a, &ar); /* See if encoding name, clock rate, and channel * count match */ if (!pj_stricmp(&or_.enc_name, &ar.enc_name) && or_.clock_rate == ar.clock_rate && (pj_stricmp(&or_.param, &ar.param)==0 || (ar.param.slen==1 && *ar.param.ptr=='1'))) { /* Call custom format matching callbacks */ if (custom_fmt_match(pool, &or_.enc_name, offer, i, answer, j, 0) == PJ_SUCCESS) { /* Match! */ break; } } } } } if (j == answer->desc.fmt_count) { /* This format has no matching answer. * Remove it from our offer. */ pjmedia_sdp_attr *a; /* Remove rtpmap associated with this format */ a = pjmedia_sdp_media_find_attr2(offer, "rtpmap", fmt); if (a) pjmedia_sdp_media_remove_attr(offer, a); /* Remove fmtp associated with this format */ a = pjmedia_sdp_media_find_attr2(offer, "fmtp", fmt); if (a) pjmedia_sdp_media_remove_attr(offer, a); /* Remove this format from offer's array */ pj_array_erase(offer->desc.fmt, sizeof(offer->desc.fmt[0]), offer->desc.fmt_count, i); --offer->desc.fmt_count; } else { offer_fmt_prior[i] = j; ++i; } } if (0 == offer->desc.fmt_count) { /* No common codec in the answer! */ return PJMEDIA_SDPNEG_EANSNOMEDIA; } /* Post process: * - Resort offer formats so the order match to the answer. * - Remove answer formats that unmatches to the offer. */ /* Resort offer formats */ for (i=0; i<offer->desc.fmt_count; ++i) { unsigned j; for (j=i+1; j<offer->desc.fmt_count; ++j) { if (offer_fmt_prior[i] > offer_fmt_prior[j]) { unsigned tmp = offer_fmt_prior[i]; offer_fmt_prior[i] = offer_fmt_prior[j]; offer_fmt_prior[j] = tmp; str_swap(&offer->desc.fmt[i], &offer->desc.fmt[j]); } } } /* Remove unmatched answer formats */ { unsigned del_cnt = 0; for (i=0; i<answer->desc.fmt_count;) { /* The offer is ordered now, also the offer_fmt_prior */ if (i >= offer->desc.fmt_count || offer_fmt_prior[i]-del_cnt != i) { pj_str_t *fmt = &answer->desc.fmt[i]; pjmedia_sdp_attr *a; /* Remove rtpmap associated with this format */ a = pjmedia_sdp_media_find_attr2(answer, "rtpmap", fmt); if (a) pjmedia_sdp_media_remove_attr(answer, a); /* Remove fmtp associated with this format */ a = pjmedia_sdp_media_find_attr2(answer, "fmtp", fmt); if (a) pjmedia_sdp_media_remove_attr(answer, a); /* Remove this format from answer's array */ pj_array_erase(answer->desc.fmt, sizeof(answer->desc.fmt[0]), answer->desc.fmt_count, i); --answer->desc.fmt_count; ++del_cnt; } else { ++i; } } } } /* Looks okay */ return PJ_SUCCESS; }