blob: 9970cc78f141fe42ee0fed43fb2f8645db35ab37 [file] [log] [blame]
Tristan Matthews0a329cc2013-07-17 13:20:14 -04001/* $Id$ */
2/*
3 * Copyright (C) 2011 Teluu Inc. (http://www.teluu.com)
4 *
5 * This program is free software; you can redistribute it and/or modify
6 * it under the terms of the GNU General Public License as published by
7 * the Free Software Foundation; either version 2 of the License, or
8 * (at your option) any later version.
9 *
10 * This program is distributed in the hope that it will be useful,
11 * but WITHOUT ANY WARRANTY; without even the implied warranty of
12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 * GNU General Public License for more details.
14 *
15 * You should have received a copy of the GNU General Public License
16 * along with this program; if not, write to the Free Software
17 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
18 */
19#include <pjmedia/stream_common.h>
20#include <pj/log.h>
21
22#define THIS_FILE "stream_common.c"
23
24/*
25 * Parse fmtp for specified format/payload type.
26 */
27PJ_DEF(pj_status_t) pjmedia_stream_info_parse_fmtp( pj_pool_t *pool,
28 const pjmedia_sdp_media *m,
29 unsigned pt,
30 pjmedia_codec_fmtp *fmtp)
31{
32 const pjmedia_sdp_attr *attr;
33 pjmedia_sdp_fmtp sdp_fmtp;
34 char *p, *p_end, fmt_buf[8];
35 pj_str_t fmt;
36 pj_status_t status;
37
38 pj_assert(m && fmtp);
39
40 pj_bzero(fmtp, sizeof(pjmedia_codec_fmtp));
41
42 /* Get "fmtp" attribute for the format */
43 pj_ansi_sprintf(fmt_buf, "%d", pt);
44 fmt = pj_str(fmt_buf);
45 attr = pjmedia_sdp_media_find_attr2(m, "fmtp", &fmt);
46 if (attr == NULL)
47 return PJ_SUCCESS;
48
49 /* Parse "fmtp" attribute */
50 status = pjmedia_sdp_attr_get_fmtp(attr, &sdp_fmtp);
51 if (status != PJ_SUCCESS)
52 return status;
53
54 /* Prepare parsing */
55 p = sdp_fmtp.fmt_param.ptr;
56 p_end = p + sdp_fmtp.fmt_param.slen;
57
58 /* Parse */
59 while (p < p_end) {
60 char *token, *start, *end;
61
62 if (fmtp->cnt >= PJMEDIA_CODEC_MAX_FMTP_CNT) {
63 PJ_LOG(4,(THIS_FILE,
64 "Warning: fmtp parameter count exceeds "
65 "PJMEDIA_CODEC_MAX_FMTP_CNT"));
66 return PJ_SUCCESS;
67 }
68
69 /* Skip whitespaces */
70 while (p < p_end && (*p == ' ' || *p == '\t')) ++p;
71 if (p == p_end)
72 break;
73
74 /* Get token */
75 start = p;
76 while (p < p_end && *p != ';' && *p != '=') ++p;
77 end = p - 1;
78
79 /* Right trim */
80 while (end >= start && (*end == ' ' || *end == '\t' ||
81 *end == '\r' || *end == '\n' ))
82 --end;
83
84 /* Forward a char after trimming */
85 ++end;
86
87 /* Store token */
88 if (end > start) {
89 if (pool) {
90 token = (char*)pj_pool_alloc(pool, end - start);
91 pj_ansi_strncpy(token, start, end - start);
92 } else {
93 token = start;
94 }
95 if (*p == '=')
96 /* Got param name */
97 pj_strset(&fmtp->param[fmtp->cnt].name, token, end - start);
98 else
99 /* Got param value */
100 pj_strset(&fmtp->param[fmtp->cnt++].val, token, end - start);
101 } else if (*p != '=') {
102 ++fmtp->cnt;
103 }
104
105 /* Next */
106 ++p;
107 }
108
109 return PJ_SUCCESS;
110}
111