1
/* $Id: amr_sdp_match.c 3911 2011-12-15 06:45:23Z nanang $ */
3
* Copyright (C) 2008-2011 Teluu Inc. (http://www.teluu.com)
4
* Copyright (C) 2003-2008 Benny Prijono <benny@prijono.org>
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.
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.
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
20
#include <pjmedia-codec/amr_sdp_match.h>
21
#include <pjmedia/errno.h>
23
#include <pj/string.h>
26
#define GET_FMTP_IVAL_BASE(ival, base, fmtp, param, default_val) \
30
p = pj_stristr(&fmtp.fmt_param, ¶m); \
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); \
40
#define GET_FMTP_IVAL(ival, fmtp, param, default_val) \
41
GET_FMTP_IVAL_BASE(ival, 10, fmtp, param, default_val)
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,
49
pjmedia_sdp_attr *attr;
50
pjmedia_sdp_fmtp fmtp;
51
const pj_str_t STR_OCTET_ALIGN = {"octet-align=", 12};
53
enum { MAX_FMTP_STR_LEN = 160 };
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 */
62
status = pjmedia_sdp_attr_get_fmtp(attr, &fmtp);
63
if (status != PJ_SUCCESS)
66
/* Check if the fmtp has octet-align field. */
67
p = pj_stristr(&fmtp.fmt_param, &STR_OCTET_ALIGN);
69
/* It has, just toggle the value */
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');
78
/* It doesn't, append octet-align field */
79
char buf[MAX_FMTP_STR_LEN];
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);
86
/* Add new attribute for the AMR media format with octet-align
89
char buf[MAX_FMTP_STR_LEN];
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;
104
PJ_DEF(pj_status_t) pjmedia_codec_amr_match_sdp( pj_pool_t *pool,
105
pjmedia_sdp_media *offer,
107
pjmedia_sdp_media *answer,
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};
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;
123
const pjmedia_sdp_attr *attr_ans;
124
const pjmedia_sdp_attr *attr_ofr;
125
pjmedia_sdp_fmtp fmtp;
128
/* Parse offerer FMTP */
129
attr_ofr = pjmedia_sdp_media_find_attr2(offer, "fmtp",
130
&offer->desc.fmt[o_fmt_idx]);
132
status = pjmedia_sdp_attr_get_fmtp(attr_ofr, &fmtp);
133
if (status != PJ_SUCCESS)
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);
142
/* Parse answerer FMTP */
143
attr_ans = pjmedia_sdp_media_find_attr2(answer, "fmtp",
144
&answer->desc.fmt[a_fmt_idx]);
146
status = pjmedia_sdp_attr_get_fmtp(attr_ans, &fmtp);
147
if (status != PJ_SUCCESS)
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);
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)
161
return PJMEDIA_SDP_EFORMATNOTEQUAL;
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);
171
return PJMEDIA_SDP_EFORMATNOTEQUAL;