Tristan Matthews | 0a329cc | 2013-07-17 13:20:14 -0400 | [diff] [blame] | 1 | /* $Id$ */ |
| 2 | /* |
| 3 | * Copyright (C) 2008-2011 Teluu Inc. (http://www.teluu.com) |
| 4 | * Copyright (C) 2003-2008 Benny Prijono <benny@prijono.org> |
| 5 | * |
| 6 | * This program is free software; you can redistribute it and/or modify |
| 7 | * it under the terms of the GNU General Public License as published by |
| 8 | * the Free Software Foundation; either version 2 of the License, or |
| 9 | * (at your option) any later version. |
| 10 | * |
| 11 | * This program is distributed in the hope that it will be useful, |
| 12 | * but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 13 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
| 14 | * GNU General Public License for more details. |
| 15 | * |
| 16 | * You should have received a copy of the GNU General Public License |
| 17 | * along with this program; if not, write to the Free Software |
| 18 | * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA |
| 19 | */ |
| 20 | #include <pjmedia-codec/amr_sdp_match.h> |
| 21 | #include <pjmedia/errno.h> |
| 22 | #include <pj/pool.h> |
| 23 | #include <pj/string.h> |
| 24 | |
| 25 | |
| 26 | #define GET_FMTP_IVAL_BASE(ival, base, fmtp, param, default_val) \ |
| 27 | do { \ |
| 28 | pj_str_t s; \ |
| 29 | char *p; \ |
| 30 | p = pj_stristr(&fmtp.fmt_param, ¶m); \ |
| 31 | if (!p) { \ |
| 32 | ival = default_val; \ |
| 33 | break; \ |
| 34 | } \ |
| 35 | pj_strset(&s, p + param.slen, fmtp.fmt_param.slen - \ |
| 36 | (p - fmtp.fmt_param.ptr) - param.slen); \ |
| 37 | ival = pj_strtoul2(&s, NULL, base); \ |
| 38 | } while (0) |
| 39 | |
| 40 | #define GET_FMTP_IVAL(ival, fmtp, param, default_val) \ |
| 41 | GET_FMTP_IVAL_BASE(ival, 10, fmtp, param, default_val) |
| 42 | |
| 43 | |
| 44 | /* Toggle AMR octet-align setting in the fmtp. */ |
| 45 | static pj_status_t amr_toggle_octet_align(pj_pool_t *pool, |
| 46 | pjmedia_sdp_media *media, |
| 47 | unsigned fmt_idx) |
| 48 | { |
| 49 | pjmedia_sdp_attr *attr; |
| 50 | pjmedia_sdp_fmtp fmtp; |
| 51 | const pj_str_t STR_OCTET_ALIGN = {"octet-align=", 12}; |
| 52 | |
| 53 | enum { MAX_FMTP_STR_LEN = 160 }; |
| 54 | |
| 55 | attr = pjmedia_sdp_media_find_attr2(media, "fmtp", |
| 56 | &media->desc.fmt[fmt_idx]); |
| 57 | /* Check if the AMR media format has FMTP attribute */ |
| 58 | if (attr) { |
| 59 | char *p; |
| 60 | pj_status_t status; |
| 61 | |
| 62 | status = pjmedia_sdp_attr_get_fmtp(attr, &fmtp); |
| 63 | if (status != PJ_SUCCESS) |
| 64 | return status; |
| 65 | |
| 66 | /* Check if the fmtp has octet-align field. */ |
| 67 | p = pj_stristr(&fmtp.fmt_param, &STR_OCTET_ALIGN); |
| 68 | if (p) { |
| 69 | /* It has, just toggle the value */ |
| 70 | unsigned octet_align; |
| 71 | pj_str_t s; |
| 72 | |
| 73 | pj_strset(&s, p + STR_OCTET_ALIGN.slen, fmtp.fmt_param.slen - |
| 74 | (p - fmtp.fmt_param.ptr) - STR_OCTET_ALIGN.slen); |
| 75 | octet_align = pj_strtoul(&s); |
| 76 | *(p + STR_OCTET_ALIGN.slen) = (char)(octet_align? '0' : '1'); |
| 77 | } else { |
| 78 | /* It doesn't, append octet-align field */ |
| 79 | char buf[MAX_FMTP_STR_LEN]; |
| 80 | |
| 81 | pj_ansi_snprintf(buf, MAX_FMTP_STR_LEN, "%.*s;octet-align=1", |
| 82 | (int)fmtp.fmt_param.slen, fmtp.fmt_param.ptr); |
| 83 | attr->value = pj_strdup3(pool, buf); |
| 84 | } |
| 85 | } else { |
| 86 | /* Add new attribute for the AMR media format with octet-align |
| 87 | * field set. |
| 88 | */ |
| 89 | char buf[MAX_FMTP_STR_LEN]; |
| 90 | |
| 91 | attr = PJ_POOL_ZALLOC_T(pool, pjmedia_sdp_attr); |
| 92 | attr->name = pj_str("fmtp"); |
| 93 | pj_ansi_snprintf(buf, MAX_FMTP_STR_LEN, "%.*s octet-align=1", |
| 94 | (int)media->desc.fmt[fmt_idx].slen, |
| 95 | media->desc.fmt[fmt_idx].ptr); |
| 96 | attr->value = pj_strdup3(pool, buf); |
| 97 | media->attr[media->attr_count++] = attr; |
| 98 | } |
| 99 | |
| 100 | return PJ_SUCCESS; |
| 101 | } |
| 102 | |
| 103 | |
| 104 | PJ_DEF(pj_status_t) pjmedia_codec_amr_match_sdp( pj_pool_t *pool, |
| 105 | pjmedia_sdp_media *offer, |
| 106 | unsigned o_fmt_idx, |
| 107 | pjmedia_sdp_media *answer, |
| 108 | unsigned a_fmt_idx, |
| 109 | unsigned option) |
| 110 | { |
| 111 | /* Negotiated format-param field-names constants. */ |
| 112 | const pj_str_t STR_OCTET_ALIGN = {"octet-align=", 12}; |
| 113 | const pj_str_t STR_CRC = {"crc=", 4}; |
| 114 | const pj_str_t STR_ROBUST_SORTING = {"robust-sorting=", 15}; |
| 115 | const pj_str_t STR_INTERLEAVING = {"interleaving=", 13}; |
| 116 | |
| 117 | /* Negotiated params and their default values. */ |
| 118 | unsigned a_octet_align = 0, o_octet_align = 0; |
| 119 | unsigned a_crc = 0, o_crc = 0; |
| 120 | unsigned a_robust_sorting = 0, o_robust_sorting = 0; |
| 121 | unsigned a_interleaving = 0, o_interleaving = 0; |
| 122 | |
| 123 | const pjmedia_sdp_attr *attr_ans; |
| 124 | const pjmedia_sdp_attr *attr_ofr; |
| 125 | pjmedia_sdp_fmtp fmtp; |
| 126 | pj_status_t status; |
| 127 | |
| 128 | /* Parse offerer FMTP */ |
| 129 | attr_ofr = pjmedia_sdp_media_find_attr2(offer, "fmtp", |
| 130 | &offer->desc.fmt[o_fmt_idx]); |
| 131 | if (attr_ofr) { |
| 132 | status = pjmedia_sdp_attr_get_fmtp(attr_ofr, &fmtp); |
| 133 | if (status != PJ_SUCCESS) |
| 134 | return status; |
| 135 | |
| 136 | GET_FMTP_IVAL(o_octet_align, fmtp, STR_OCTET_ALIGN, 0); |
| 137 | GET_FMTP_IVAL(o_crc, fmtp, STR_CRC, 0); |
| 138 | GET_FMTP_IVAL(o_robust_sorting, fmtp, STR_ROBUST_SORTING, 0); |
| 139 | GET_FMTP_IVAL(o_interleaving, fmtp, STR_INTERLEAVING, 0); |
| 140 | } |
| 141 | |
| 142 | /* Parse answerer FMTP */ |
| 143 | attr_ans = pjmedia_sdp_media_find_attr2(answer, "fmtp", |
| 144 | &answer->desc.fmt[a_fmt_idx]); |
| 145 | if (attr_ans) { |
| 146 | status = pjmedia_sdp_attr_get_fmtp(attr_ans, &fmtp); |
| 147 | if (status != PJ_SUCCESS) |
| 148 | return status; |
| 149 | |
| 150 | GET_FMTP_IVAL(a_octet_align, fmtp, STR_OCTET_ALIGN, 0); |
| 151 | GET_FMTP_IVAL(a_crc, fmtp, STR_CRC, 0); |
| 152 | GET_FMTP_IVAL(a_robust_sorting, fmtp, STR_ROBUST_SORTING, 0); |
| 153 | GET_FMTP_IVAL(a_interleaving, fmtp, STR_INTERLEAVING, 0); |
| 154 | } |
| 155 | |
| 156 | /* First, match crc, robust-sorting, interleaving settings */ |
| 157 | if (a_crc != o_crc || |
| 158 | a_robust_sorting != o_robust_sorting || |
| 159 | a_interleaving != o_interleaving) |
| 160 | { |
| 161 | return PJMEDIA_SDP_EFORMATNOTEQUAL; |
| 162 | } |
| 163 | |
| 164 | /* Match octet-align setting */ |
| 165 | if (a_octet_align != o_octet_align) { |
| 166 | /* Check if answer can be modified to match to the offer */ |
| 167 | if (option & PJMEDIA_SDP_NEG_FMT_MATCH_ALLOW_MODIFY_ANSWER) { |
| 168 | status = amr_toggle_octet_align(pool, answer, a_fmt_idx); |
| 169 | return status; |
| 170 | } else { |
| 171 | return PJMEDIA_SDP_EFORMATNOTEQUAL; |
| 172 | } |
| 173 | } |
| 174 | |
| 175 | return PJ_SUCCESS; |
| 176 | } |