summaryrefslogtreecommitdiff
path: root/tpsip/codec-param-formats.c
blob: a0be89a8d862add08512ef73f816ae066e572893 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
/*
 * codec-param-formats.c - Implementation of codec parameter formatter infra
 * Copyright (C) 2009, 2010 Nokia Corporation
 *   @author Mikhail Zabaluev <mikhail.zabaluev@nokia.com>
 *
 * This work is free software; you can redistribute it and/or
 * modify it under the terms of the GNU Lesser General Public
 * License as published by the Free Software Foundation; either
 * version 2.1 of the License, or (at your option) any later version.
 *
 * This work is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
 * Lesser General Public License for more details.
 *
 * You should have received a copy of the GNU Lesser General Public
 * License along with this work; if not, write to the Free Software
 * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA  02110-1301  USA
 */

#include "codec-param-formats.h"

#include <string.h>

#include <tpsip/util.h>

/* Regexps for the name and the value parts of the parameter syntax */
#define FMTP_TOKEN_PARAM "[-A-Za-z0-9!#$%&'*+.^_`{|}~]+"
#define FMTP_TOKEN_VALUE "[^;\"\\s]+|\"([^\"\\\\]|\\\\.)*\""
/* Indexes of the respective match groups in the whole regexp below */
#define FMTP_MATCH_NAME_PARAM "p"
#define FMTP_MATCH_NAME_VALUE "v"

typedef struct _TpsipCodecParamFormatting {
  TpsipCodecParamFormatFunc format;
  TpsipCodecParamParseFunc parse;
} TpsipCodecParamFormatting;

static GRegex *fmtp_attr_regex = NULL;
static GRegex *dtmf_events_regex = NULL;

static GHashTable *codec_param_formats[NUM_TP_MEDIA_STREAM_TYPES];

static void tpsip_codec_param_formats_init ();

/**
 * tpsip_codec_param_format:
 * @media: the media type
 * @name: name of the codec, as per its MIME subtype registration
 * @params: the map of codec parameters
 * @out: a #GString for the output
 *
 * Formats the parameters passed in the @params into a string suitable for
 * <literal>a=fmtp</literal> attribute for an RTP payload description,
 * as specified for the media type defined by @media and @name.
 */
void
tpsip_codec_param_format (TpMediaStreamType media, const char *name,
                          GHashTable *params, GString *out)
{
  TpsipCodecParamFormatting *fmt;

  tpsip_codec_param_formats_init ();

  /* XXX: thread unsafe, we don't care for now */
  fmt = g_hash_table_lookup (codec_param_formats[media], name);

  if (fmt != NULL && fmt->format != NULL)
    fmt->format (params, out);
  else
    tpsip_codec_param_format_generic (params, out);
}

/**
 * tpsip_codec_param_parse:
 * @media: the media type
 * @name: name of the codec, as per its MIME subtype registration
 * @fmtp: a string with the codec-specific parameter data. May be #NULL.
 * @out: the parameter map to populate
 *
 * Parses the payload-specific parameter description as coming from an
 * <literal>a=fmtp</literal> attribute of an RTP payload description.
 * The media type is defined by @media and @name.
 */
void
tpsip_codec_param_parse (TpMediaStreamType media, const char *name,
                         const gchar *fmtp, GHashTable *out)
{
  TpsipCodecParamFormatting *fmt;

  if (fmtp == NULL)
    return;

  tpsip_codec_param_formats_init ();

  /* XXX: thread unsafe, we don't care for now */
  fmt = g_hash_table_lookup (codec_param_formats[media], name);

  if (fmt != NULL && fmt->parse != NULL)
    fmt->parse (fmtp, out);
  else
    tpsip_codec_param_parse_generic (fmtp, out);
}

/**
 * tpsip_codec_param_register_format:
 * @media: the media type
 * @name: name of the codec, as per its MIME subtype registration. Must be a static string.
 * @format: pointer to the formatting function
 * @parse: pointer to the parsing function
 *
 * Registers custom SDP payload parameter formatting routines for a media
 * type.
 */
void
tpsip_codec_param_register_format (TpMediaStreamType media, const char *name,
                                   TpsipCodecParamFormatFunc format,
                                   TpsipCodecParamParseFunc parse)
{
  TpsipCodecParamFormatting *fmt;

  tpsip_codec_param_formats_init ();

  fmt = g_slice_new (TpsipCodecParamFormatting);
  fmt->format = format;
  fmt->parse = parse;

  /* XXX: thread unsafe, we don't care for now */
  g_hash_table_insert (codec_param_formats[media], (gpointer) name, fmt);
}

static void
format_param_generic (gpointer key, gpointer val, gpointer user_data)
{
  const gchar *name = key;
  const gchar *value = val;
  GString *out = user_data;

  /* Ignore freaky parameters */
  g_return_if_fail (name != NULL && name[0]);
  g_return_if_fail (value != NULL && value[0]);

  if (out->len != 0)
    g_string_append_c (out, ';');

  if (strpbrk (value, "; \t") == NULL)
    g_string_append_printf (out, "%s=%s", name, value);
  else
    {
      g_string_append (out, name);
      g_string_append_c (out, '=');
      tpsip_string_append_quoted (out, value);
    }
}

/**
 * tpsip_codec_param_format_generic:
 * @params: the map of codec parameters
 * @out: a #GString for the output
 *
 * Formats the parameters as a semicolon separated list of
 * <replaceable>parameter</replaceable><literal>=</literal><replaceable>value</replaceable>
 * pairs, as recommended in IETF RFC 4855 Section 3.
 */
void
tpsip_codec_param_format_generic (GHashTable *params, GString *out)
{
  g_hash_table_foreach (params, format_param_generic, out);
}

/**
 * tpsip_codec_param_parse_generic:
 * @fmtp: a string value with the parameter description
 * @out: the parameter map to populate
 *
 * Parses parameters formatted as a semicolon separated list of
 * <replaceable>parameter</replaceable><literal>=</literal><replaceable>value</replaceable>
 * pairs, as recommended in IETF RFC 4855 Section 3.
 */
void
tpsip_codec_param_parse_generic (const gchar *fmtp, GHashTable *out)
{
  GMatchInfo *match = NULL;
  gint pos;
  gint value_start;
  gint value_end;

  if (fmtp == NULL)
    return;

  pos = 0;

  /* Fast path for trivial cases, not involving the regex engine */
  while (g_ascii_isspace (fmtp[pos]))
    ++pos;
  if (!fmtp[pos])
    return;

  g_assert (fmtp_attr_regex != NULL);

  g_regex_match_full (fmtp_attr_regex,
      fmtp, -1, pos, G_REGEX_MATCH_ANCHORED, &match, NULL);

  while (g_match_info_matches (match))
    {
      gchar *name;
      gchar *value;

      name = g_match_info_fetch_named (match, FMTP_MATCH_NAME_PARAM);

      g_match_info_fetch_named_pos (match, FMTP_MATCH_NAME_VALUE,
          &value_start, &value_end);

      if (value_end - 1 > value_start
          && fmtp[value_start] == '\"' && fmtp[value_end - 1] == '\"')
        {
          value = tpsip_unquote_string (fmtp + value_start,
                                        value_end - value_start);
        }
      else
        {
          value = g_strndup (fmtp + value_start,
                             value_end - value_start);
        }

      g_hash_table_insert (out, name, value);

      g_match_info_fetch_pos (match, 0, NULL, &pos);
      if (!fmtp[pos])
        break;

      g_match_info_next (match, NULL);
    }

  g_match_info_free (match);

  if (fmtp[pos])
    g_message ("failed to parse part of format parameters"
               " as an attribute-value list: %s", &fmtp[pos]);
}

/* Custom format for audio/telephone-event */

static void
tpsip_codec_param_format_telephone_event (GHashTable *params, GString *out)
{
  const gchar *events;

  /* events parameter value comes first without the parameter name */
  events = g_hash_table_lookup (params, "events");
  if (events != NULL)
    {
      g_string_append (out, events);
      g_hash_table_remove (params, "events");
    }

  /* format the rest of the parameters, if any */
  tpsip_codec_param_format_generic (params, out);
}

static void
tpsip_codec_param_parse_telephone_event (const gchar *fmtp, GHashTable *out)
{
  GMatchInfo *match = NULL;
  gint end_pos = 0;

  g_assert (dtmf_events_regex != NULL);

  /* Parse the events list */

  g_regex_match (dtmf_events_regex, fmtp, 0, &match);

  if (g_match_info_matches (match))
    {
      gchar *events;

      events = g_match_info_fetch (match, 1);
      g_hash_table_insert (out, g_strdup ("events"), events);

      g_match_info_fetch_pos (match, 0, NULL, &end_pos);
    }

  g_match_info_free (match);

  /* Parse the remaining parameters, if any */
  tpsip_codec_param_parse_generic (fmtp + end_pos, out);
}

/*
 * tpsip_codec_param_formats_init:
 *
 * Initializes the codec parameter formatting infrastructure.
 * This function must be called before using any other functions in this module.
 * Calling the function more than once has no effect.
 */
static void
tpsip_codec_param_formats_init ()
{
  static volatile gsize been_here = 0;

  int i;

  if (g_once_init_enter (&been_here))
    g_once_init_leave (&been_here, 1);
  else
    return;

  for (i = 0; i < NUM_TP_MEDIA_STREAM_TYPES; ++i)
    {
      /* XXX: we ignore deallocation of values for now */
      codec_param_formats[i] = g_hash_table_new (g_str_hash, g_str_equal);
    }

  tpsip_codec_param_register_format (
      TP_MEDIA_STREAM_TYPE_AUDIO, "telephone-event",
      tpsip_codec_param_format_telephone_event,
      tpsip_codec_param_parse_telephone_event);

  fmtp_attr_regex = g_regex_new (
      "(?<" FMTP_MATCH_NAME_PARAM ">" FMTP_TOKEN_PARAM ")"
      "\\s*=\\s*"
      "(?<"  FMTP_MATCH_NAME_VALUE ">" FMTP_TOKEN_VALUE ")"
      "\\s*(;\\s*|$)",
      G_REGEX_RAW | G_REGEX_OPTIMIZE,
      0, NULL);
  g_assert (fmtp_attr_regex != NULL);

#define DTMF_RANGE "[0-9]+(-[0-9]+)?"

  dtmf_events_regex = g_regex_new (
      "^(" DTMF_RANGE "(," DTMF_RANGE ")*)\\s*(;|$)",
      G_REGEX_RAW | G_REGEX_OPTIMIZE,
      0, NULL);
  g_assert (dtmf_events_regex != NULL);
}