Ignore:
Timestamp:
Oct 23, 2011 6:59:48 AM (12 years ago)
Author:
nanang
Message:

Re #1300: Implemented symmetric payload type in generating SDP answer in SDP negotiator.
This should work for all codecs, audio & video. Can be disabled at compile-time
using PJMEDIA_SDP_NEG_REWRITE_ANSWER_PT macro setting.

File:
1 edited

Legend:

Unmodified
Added
Removed
  • pjproject/trunk/pjmedia/src/pjmedia/sdp_neg.c

    r3714 r3837  
    11211121} 
    11221122 
     1123 
     1124/* Internal function to rewrite the format string in SDP attribute rtpmap 
     1125 * and fmtp. 
     1126 */ 
     1127PJ_INLINE(void) rewrite_pt(pj_pool_t *pool, pj_str_t *attr_val, 
     1128                           const pj_str_t *old_pt, const pj_str_t *new_pt) 
     1129{ 
     1130    int len_diff = new_pt->slen - old_pt->slen; 
     1131 
     1132    /* Note that attribute value should be null-terminated. */ 
     1133    if (len_diff > 0) { 
     1134        pj_str_t new_val; 
     1135        new_val.ptr = (char*)pj_pool_alloc(pool, attr_val->slen+len_diff+1); 
     1136        new_val.slen = attr_val->slen + len_diff; 
     1137        pj_memcpy(new_val.ptr + len_diff, attr_val->ptr, attr_val->slen + 1); 
     1138        *attr_val = new_val; 
     1139    } else if (len_diff < 0) { 
     1140        pj_memmove(attr_val->ptr, attr_val->ptr - len_diff, 
     1141                   attr_val->slen + len_diff + 1); 
     1142    } 
     1143    pj_memcpy(attr_val->ptr, new_pt->ptr, new_pt->slen); 
     1144} 
     1145 
     1146 
     1147/* Internal function to apply symmetric PT for the local answer. */ 
     1148static void apply_answer_symmetric_pt(pj_pool_t *pool, 
     1149                                      pjmedia_sdp_media *answer, 
     1150                                      unsigned pt_cnt, 
     1151                                      const pj_str_t pt_offer[], 
     1152                                      const pj_str_t pt_answer[]) 
     1153{ 
     1154    pjmedia_sdp_attr *a_tmp[PJMEDIA_MAX_SDP_ATTR]; 
     1155    unsigned i, a_tmp_cnt = 0; 
     1156 
     1157    /* Rewrite the payload types in the answer if different to 
     1158     * the ones in the offer. 
     1159     */ 
     1160    for (i = 0; i < pt_cnt; ++i) { 
     1161        pjmedia_sdp_attr *a; 
     1162 
     1163        /* Skip if the PTs are the same already, e.g: static PT. */ 
     1164        if (pj_strcmp(&pt_answer[i], &pt_offer[i]) == 0) 
     1165            continue; 
     1166 
     1167        /* Rewrite payload type in the answer to match to the offer */ 
     1168        pj_strdup(pool, &answer->desc.fmt[i], &pt_offer[i]); 
     1169 
     1170        /* Also update payload type in rtpmap */ 
     1171        a = pjmedia_sdp_media_find_attr2(answer, "rtpmap", &pt_answer[i]); 
     1172        if (a) { 
     1173            rewrite_pt(pool, &a->value, &pt_answer[i], &pt_offer[i]); 
     1174            /* Temporarily remove the attribute in case the new payload 
     1175             * type is being used by another format in the media. 
     1176             */ 
     1177            pjmedia_sdp_media_remove_attr(answer, a); 
     1178            a_tmp[a_tmp_cnt++] = a; 
     1179        } 
     1180 
     1181        /* Also update payload type in fmtp */ 
     1182        a = pjmedia_sdp_media_find_attr2(answer, "fmtp", &pt_answer[i]); 
     1183        if (a) { 
     1184            rewrite_pt(pool, &a->value, &pt_answer[i], &pt_offer[i]); 
     1185            /* Temporarily remove the attribute in case the new payload 
     1186             * type is being used by another format in the media. 
     1187             */ 
     1188            pjmedia_sdp_media_remove_attr(answer, a); 
     1189            a_tmp[a_tmp_cnt++] = a; 
     1190        } 
     1191    } 
     1192 
     1193    /* Return back 'rtpmap' and 'fmtp' attributes */ 
     1194    for (i = 0; i < a_tmp_cnt; ++i) 
     1195        pjmedia_sdp_media_add_attr(answer, a_tmp[i]); 
     1196} 
     1197 
     1198 
    11231199/* Try to match offer with answer. */ 
    11241200static pj_status_t match_offer(pj_pool_t *pool, 
     
    11381214    unsigned pt_answer_count = 0; 
    11391215    pj_str_t pt_answer[PJMEDIA_MAX_SDP_FMT]; 
     1216    pj_str_t pt_offer[PJMEDIA_MAX_SDP_FMT]; 
    11401217    pjmedia_sdp_media *answer; 
    11411218    const pjmedia_sdp_media *master, *slave; 
     
    12021279                    if (p == pt && pj_isdigit(*slave->desc.fmt[j].ptr)) { 
    12031280                        found_matching_codec = 1; 
     1281                        pt_offer[pt_answer_count] = slave->desc.fmt[j]; 
    12041282                        pt_answer[pt_answer_count++] = slave->desc.fmt[j]; 
    12051283                        break; 
     
    13011379                            } 
    13021380 
     1381                            pt_offer[pt_answer_count] =  
     1382                                                prefer_remote_codec_order? 
     1383                                                offer->desc.fmt[i]: 
     1384                                                offer->desc.fmt[j]; 
    13031385                            pt_answer[pt_answer_count++] =  
    13041386                                                prefer_remote_codec_order?  
     
    13261408                    /* Match */ 
    13271409                    found_matching_other = 1; 
     1410                    pt_offer[pt_answer_count] = prefer_remote_codec_order? 
     1411                                                offer->desc.fmt[i]: 
     1412                                                offer->desc.fmt[j]; 
    13281413                    pt_answer[pt_answer_count++] = prefer_remote_codec_order?  
    13291414                                                   preanswer->desc.fmt[j]: 
     
    13901475    } 
    13911476    answer->desc.fmt_count = pt_answer_count; 
     1477 
     1478#if PJMEDIA_SDP_NEG_ANSWER_SYMMETRIC_PT 
     1479    apply_answer_symmetric_pt(pool, answer, pt_answer_count, 
     1480                              pt_offer, pt_answer); 
     1481#endif 
    13921482 
    13931483    /* Update media direction. */ 
Note: See TracChangeset for help on using the changeset viewer.