gstsdpmessage.c 48 KB
Newer Older
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
/* GStreamer
 * Copyright (C) <2005,2006> Wim Taymans <wim@fluendo.com>
 *
 * This library is free software; you can redistribute it and/or
 * modify it under the terms of the GNU Library General Public
 * License as published by the Free Software Foundation; either
 * version 2 of the License, or (at your option) any later version.
 *
 * This library 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
 * Library General Public License for more details.
 *
 * You should have received a copy of the GNU Library General Public
 * License along with this library; if not, write to the
 * Free Software Foundation, Inc., 59 Temple Place - Suite 330,
 * Boston, MA 02111-1307, USA.
 */
/*
 * Unless otherwise indicated, Source Code is licensed under MIT license.
 * See further explanation attached in License Statement (distributed in the file
 * LICENSE).
 *
 * Permission is hereby granted, free of charge, to any person obtaining a copy of
 * this software and associated documentation files (the "Software"), to deal in
 * the Software without restriction, including without limitation the rights to
 * use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies
 * of the Software, and to permit persons to whom the Software is furnished to do
 * so, subject to the following conditions:
 *
 * The above copyright notice and this permission notice shall be included in all
 * copies or substantial portions of the Software.
 *
 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
 * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
 * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
 * SOFTWARE.
 */

Wim Taymans's avatar
Wim Taymans committed
43
44
45
46
47
48
49
/**
 * SECTION:gstsdpmessage
 * @short_description: Helper methods for dealing with SDP messages
 *
 * <refsect2>
 * <para>
 * The GstSDPMessage helper functions makes it easy to parse and create SDP
50
 * messages.
Wim Taymans's avatar
Wim Taymans committed
51
52
53
54
55
56
57
 * </para>
 * </refsect2>
 *
 * Last reviewed on 2007-07-24 (0.10.14)
 */


58
59
#include <stdlib.h>
#include <string.h>
60
61
62
#include <sys/types.h>
#include <sys/socket.h>
#include <netdb.h>
63
64
65
66

#include "gstsdpmessage.h"

/* FIXME, is currently allocated on the stack */
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
#define MAX_LINE_LEN    1024 * 16

#define FREE_STRING(field)              g_free (field); (field) = NULL
#define REPLACE_STRING(field, val)      FREE_STRING(field); (field) = g_strdup (val)

#define INIT_ARRAY(field, type, init_func)              \
G_STMT_START {                                          \
  if (field) {                                          \
    guint i;                                            \
    for(i = 0; i < (field)->len; i++)                   \
      init_func (&g_array_index ((field), type, i));    \
    g_array_set_size ((field), 0);                      \
  }                                                     \
  else                                                  \
    (field) = g_array_new (FALSE, TRUE, sizeof (type)); \
82
} G_STMT_END
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100

#define FREE_ARRAY(field)         \
G_STMT_START {                    \
  if (field)                      \
    g_array_free ((field), TRUE); \
  (field) = NULL;                 \
} G_STMT_END

#define INIT_PTR_ARRAY(field, type, init_func)          \
G_STMT_START {                                          \
  if (field) {                                          \
    guint i;                                            \
    for(i = 0; i < (field)->len; i++)                   \
      init_func (g_array_index ((field), type, i));     \
    g_array_set_size ((field), 0);                      \
  }                                                     \
  else                                                  \
    (field) = g_array_new (FALSE, TRUE, sizeof (type)); \
101
102
} G_STMT_END

103
104
#define FREE_PTR_ARRAY(field) FREE_ARRAY(field)

105
#define DEFINE_STRING_SETTER(field)                                     \
106
GstSDPResult gst_sdp_message_set_##field (GstSDPMessage *msg, const gchar *val) { \
107
108
  g_free (msg->field);                                                  \
  msg->field = g_strdup (val);                                          \
109
  return GST_SDP_OK;                                                    \
110
111
}
#define DEFINE_STRING_GETTER(field)                                     \
112
const gchar* gst_sdp_message_get_##field (const GstSDPMessage *msg) {   \
113
114
115
116
  return msg->field;                                                    \
}

#define DEFINE_ARRAY_LEN(field)                                         \
117
118
guint gst_sdp_message_##field##_len (const GstSDPMessage *msg) {        \
  return msg->field->len;                                               \
119
}
120
121
122
#define DEFINE_ARRAY_GETTER(method, field, type)                        \
type * gst_sdp_message_get_##method (const GstSDPMessage *msg, guint idx) {  \
  return &g_array_index (msg->field, type, idx);                        \
123
}
124
125
126
127
128

#define DEFINE_PTR_ARRAY_LEN(field) DEFINE_ARRAY_LEN(field)
#define DEFINE_PTR_ARRAY_GETTER(method, field, type)                    \
type gst_sdp_message_get_##method (const GstSDPMessage *msg, guint idx) {    \
  return g_array_index (msg->field, type, idx);                         \
129
}
130
131
132
133
134
#define DEFINE_PTR_ARRAY_ADDER(method, field, type, dup_method)         \
GstSDPResult gst_sdp_message_add_##method (GstSDPMessage *msg, type val) {   \
  type v = dup_method (val);                                            \
  g_array_append_val (msg->field, v);                                   \
  return GST_SDP_OK;                                                    \
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
}

static void
gst_sdp_origin_init (GstSDPOrigin * origin)
{
  FREE_STRING (origin->username);
  FREE_STRING (origin->sess_id);
  FREE_STRING (origin->sess_version);
  FREE_STRING (origin->nettype);
  FREE_STRING (origin->addrtype);
  FREE_STRING (origin->addr);
}

static void
gst_sdp_connection_init (GstSDPConnection * connection)
{
  FREE_STRING (connection->nettype);
  FREE_STRING (connection->addrtype);
  FREE_STRING (connection->address);
  connection->ttl = 0;
  connection->addr_number = 0;
}

static void
gst_sdp_bandwidth_init (GstSDPBandwidth * bandwidth)
{
  FREE_STRING (bandwidth->bwtype);
  bandwidth->bandwidth = 0;
}

static void
166
gst_sdp_time_init (GstSDPTime * t)
167
{
168
169
170
171
  FREE_STRING (t->start);
  FREE_STRING (t->stop);
  INIT_PTR_ARRAY (t->repeat, gchar *, g_free);
  FREE_PTR_ARRAY (t->repeat);
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
}

static void
gst_sdp_zone_init (GstSDPZone * zone)
{
  FREE_STRING (zone->time);
  FREE_STRING (zone->typed_time);
}

static void
gst_sdp_key_init (GstSDPKey * key)
{
  FREE_STRING (key->type);
  FREE_STRING (key->data);
}

static void
gst_sdp_attribute_init (GstSDPAttribute * attr)
{
  FREE_STRING (attr->key);
  FREE_STRING (attr->value);
}

/**
 * gst_sdp_message_new:
 * @msg: pointer to new #GstSDPMessage
 *
 * Allocate a new GstSDPMessage and store the result in @msg.
 *
 * Returns: a #GstSDPResult.
 */
GstSDPResult
gst_sdp_message_new (GstSDPMessage ** msg)
{
  GstSDPMessage *newmsg;

  g_return_val_if_fail (msg != NULL, GST_SDP_EINVAL);

  newmsg = g_new0 (GstSDPMessage, 1);

  *msg = newmsg;

  return gst_sdp_message_init (newmsg);
}

/**
 * gst_sdp_message_init:
Wim Taymans's avatar
Wim Taymans committed
219
 * @msg: a #GstSDPMessage
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
 *
 * Initialize @msg so that its contents are as if it was freshly allocated
 * with gst_sdp_message_new(). This function is mostly used to initialize a message
 * allocated on the stack. gst_sdp_message_uninit() undoes this operation.
 *
 * When this function is invoked on newly allocated data (with malloc or on the
 * stack), its contents should be set to 0 before calling this function.
 *
 * Returns: a #GstSDPResult.
 */
GstSDPResult
gst_sdp_message_init (GstSDPMessage * msg)
{
  g_return_val_if_fail (msg != NULL, GST_SDP_EINVAL);

  FREE_STRING (msg->version);
  gst_sdp_origin_init (&msg->origin);
  FREE_STRING (msg->session_name);
  FREE_STRING (msg->information);
  FREE_STRING (msg->uri);
240
241
  INIT_PTR_ARRAY (msg->emails, gchar *, g_free);
  INIT_PTR_ARRAY (msg->phones, gchar *, g_free);
242
243
244
245
246
247
248
249
250
251
252
253
254
  gst_sdp_connection_init (&msg->connection);
  INIT_ARRAY (msg->bandwidths, GstSDPBandwidth, gst_sdp_bandwidth_init);
  INIT_ARRAY (msg->times, GstSDPTime, gst_sdp_time_init);
  INIT_ARRAY (msg->zones, GstSDPZone, gst_sdp_zone_init);
  gst_sdp_key_init (&msg->key);
  INIT_ARRAY (msg->attributes, GstSDPAttribute, gst_sdp_attribute_init);
  INIT_ARRAY (msg->medias, GstSDPMedia, gst_sdp_media_uninit);

  return GST_SDP_OK;
}

/**
 * gst_sdp_message_uninit:
Wim Taymans's avatar
Wim Taymans committed
255
 * @msg: a #GstSDPMessage
256
257
258
259
260
261
262
263
264
265
266
267
268
269
 *
 * Free all resources allocated in @msg. @msg should not be used anymore after
 * this function. This function should be used when @msg was allocated on the
 * stack and initialized with gst_sdp_message_init().
 *
 * Returns: a #GstSDPResult.
 */
GstSDPResult
gst_sdp_message_uninit (GstSDPMessage * msg)
{
  g_return_val_if_fail (msg != NULL, GST_SDP_EINVAL);

  gst_sdp_message_init (msg);

270
271
  FREE_PTR_ARRAY (msg->emails);
  FREE_PTR_ARRAY (msg->phones);
272
273
274
275
276
277
278
279
280
281
282
  FREE_ARRAY (msg->bandwidths);
  FREE_ARRAY (msg->times);
  FREE_ARRAY (msg->zones);
  FREE_ARRAY (msg->attributes);
  FREE_ARRAY (msg->medias);

  return GST_SDP_OK;
}

/**
 * gst_sdp_message_free:
Wim Taymans's avatar
Wim Taymans committed
283
 * @msg: a #GstSDPMessage
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
 *
 * Free all resources allocated by @msg. @msg should not be used anymore after
 * this function. This function should be used when @msg was dynamically
 * allocated with gst_sdp_message_new().
 *
 * Returns: a #GstSDPResult.
 */
GstSDPResult
gst_sdp_message_free (GstSDPMessage * msg)
{
  g_return_val_if_fail (msg != NULL, GST_SDP_EINVAL);

  gst_sdp_message_uninit (msg);
  g_free (msg);

  return GST_SDP_OK;
}

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
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
static gboolean
is_multicast_address (const gchar * host_name, guint * family)
{
  struct addrinfo hints = {
    .ai_socktype = SOCK_DGRAM
  };
  struct addrinfo *ai;
  struct addrinfo *res;
  gboolean ret = FALSE;
  int err;

  g_return_val_if_fail (host_name, FALSE);

  if ((err = getaddrinfo (host_name, NULL, &hints, &res)) < 0)
    return FALSE;

  for (ai = res; !ret && ai; ai = ai->ai_next) {
    if (ai->ai_family == AF_INET)
      ret =
          IN_MULTICAST (ntohl (((struct sockaddr_in *) ai->ai_addr)->sin_addr.
              s_addr));
    else
      ret =
          IN6_IS_ADDR_MULTICAST (&((struct sockaddr_in6 *) ai->ai_addr)->
          sin6_addr);
    if (ret && family)
      *family = ai->ai_family;
  }

  freeaddrinfo (res);

  return ret;
}

/**
 * gst_sdp_message_as_text:
 * @msg: a #GstSDPMessage
 *
 * Convert the contents of @msg to a text string.
 *
 * Returns: A dynamically allocated string representing the SDP description.
 */
gchar *
gst_sdp_message_as_text (const GstSDPMessage * msg)
{
  /* change all vars so they match rfc? */
  GString *lines;
  guint i;

  g_return_val_if_fail (msg != NULL, NULL);

  lines = g_string_new ("");

  if (msg->version)
    g_string_append_printf (lines, "v=%s\r\n", msg->version);

  if (msg->origin.sess_id && msg->origin.sess_version && msg->origin.nettype &&
      msg->origin.addrtype && msg->origin.addr)
    g_string_append_printf (lines, "o=%s %s %s %s %s %s\r\n",
        msg->origin.username ? msg->origin.username : "-", msg->origin.sess_id,
        msg->origin.sess_version, msg->origin.nettype, msg->origin.addrtype,
        msg->origin.addr);

  if (msg->session_name)
    g_string_append_printf (lines, "s=%s\r\n", msg->session_name);

  if (msg->information)
    g_string_append_printf (lines, "i=%s\r\n", msg->information);

  if (msg->uri)
    g_string_append_printf (lines, "u=%s\r\n", msg->uri);

  for (i = 0; i < gst_sdp_message_emails_len (msg); i++)
    g_string_append_printf (lines, "e=%s\r\n",
        gst_sdp_message_get_email (msg, i));

  for (i = 0; i < gst_sdp_message_phones_len (msg); i++)
    g_string_append_printf (lines, "p=%s\r\n",
        gst_sdp_message_get_phone (msg, i));

  if (gst_sdp_message_emails_len (msg) == 0 &&
      gst_sdp_message_phones_len (msg) == 0)
    g_string_append_printf (lines, "e=NONE\r\n");

  if (msg->connection.nettype && msg->connection.addrtype &&
      msg->connection.address) {
    guint family;

    g_string_append_printf (lines, "c=%s %s %s", msg->connection.nettype,
        msg->connection.addrtype, msg->connection.address);
    if (is_multicast_address (msg->connection.address, &family)) {
      if (family == AF_INET)
        g_string_append_printf (lines, "/%u", msg->connection.ttl);
      if (msg->connection.addr_number > 1)
        g_string_append_printf (lines, "/%u", msg->connection.addr_number);
    }
    g_string_append_printf (lines, "\r\n");
  }

  for (i = 0; i < gst_sdp_message_bandwidths_len (msg); i++) {
    const GstSDPBandwidth *bandwidth = gst_sdp_message_get_bandwidth (msg, i);

    g_string_append_printf (lines, "b=%s:%u\r\n", bandwidth->bwtype,
        bandwidth->bandwidth);
  }

  for (i = 0; i < gst_sdp_message_times_len (msg); i++) {
    const GstSDPTime *times = gst_sdp_message_get_time (msg, i);

    g_string_append_printf (lines, "t=%s %s\r\n", times->start, times->stop);

    if (times->repeat != NULL) {
      guint j;

      g_string_append_printf (lines, "r=%s",
          g_array_index (times->repeat, gchar *, 0));
      for (j = 1; j < times->repeat->len; j++)
        g_string_append_printf (lines, " %s",
            g_array_index (times->repeat, gchar *, j));
      g_string_append_printf (lines, "\r\n");
    }
  }

  if (gst_sdp_message_zones_len (msg) > 0) {
    const GstSDPZone *zone = gst_sdp_message_get_zone (msg, 0);

    g_string_append_printf (lines, "z=%s %s", zone->time, zone->typed_time);
    for (i = 1; i < gst_sdp_message_zones_len (msg); i++) {
      zone = gst_sdp_message_get_zone (msg, i);
      g_string_append_printf (lines, " %s %s", zone->time, zone->typed_time);
    }
    g_string_append_printf (lines, "\r\n");
  }

  if (msg->key.type) {
    g_string_append_printf (lines, "k=%s", msg->key.type);
    if (msg->key.data)
      g_string_append_printf (lines, ":%s", msg->key.data);
    g_string_append_printf (lines, "\r\n");
  }

  for (i = 0; i < gst_sdp_message_attributes_len (msg); i++) {
    const GstSDPAttribute *attr = gst_sdp_message_get_attribute (msg, i);

    if (attr->key) {
      g_string_append_printf (lines, "a=%s", attr->key);
      if (attr->value)
        g_string_append_printf (lines, ":%s", attr->value);
      g_string_append_printf (lines, "\r\n");
    }
  }

  for (i = 0; i < gst_sdp_message_medias_len (msg); i++) {
    const GstSDPMedia *media = gst_sdp_message_get_media (msg, i);
    gchar *sdp_media_str;

    sdp_media_str = gst_sdp_media_as_text (media);
    g_string_append_printf (lines, "%s", sdp_media_str);
    g_free (sdp_media_str);
  }

  return g_string_free (lines, FALSE);
}

466
/**
Wim Taymans's avatar
Wim Taymans committed
467
468
469
 * gst_sdp_message_set_version:
 * @msg: a #GstSDPMessage
 * @version: the version
470
 *
Wim Taymans's avatar
Wim Taymans committed
471
 * Set the version in @msg.
472
473
474
 *
 * Returns: a #GstSDPResult.
 */
Wim Taymans's avatar
Wim Taymans committed
475
DEFINE_STRING_SETTER (version);
476
/**
Wim Taymans's avatar
Wim Taymans committed
477
478
 * gst_sdp_message_get_version:
 * @msg: a #GstSDPMessage
479
 *
Wim Taymans's avatar
Wim Taymans committed
480
 * Get the version in @msg.
481
482
483
 *
 * Returns: a #GstSDPResult.
 */
Wim Taymans's avatar
Wim Taymans committed
484
DEFINE_STRING_GETTER (version);
485
486

/**
Wim Taymans's avatar
Wim Taymans committed
487
488
489
490
491
492
493
494
 * gst_sdp_message_set_origin:
 * @msg: a #GstSDPMessage
 * @username: the user name
 * @sess_id: a session id
 * @sess_version: a session version
 * @nettype: a network type
 * @addrtype: an address type
 * @addr: an address
495
 *
Wim Taymans's avatar
Wim Taymans committed
496
 * Configure the SDP origin in @msg with the given parameters.
497
 *
Wim Taymans's avatar
Wim Taymans committed
498
 * Returns: #GST_SDP_OK.
499
500
 */
GstSDPResult
Wim Taymans's avatar
Wim Taymans committed
501
502
503
gst_sdp_message_set_origin (GstSDPMessage * msg, const gchar * username,
    const gchar * sess_id, const gchar * sess_version, const gchar * nettype,
    const gchar * addrtype, const gchar * addr)
504
505
506
507
508
509
510
{
  REPLACE_STRING (msg->origin.username, username);
  REPLACE_STRING (msg->origin.sess_id, sess_id);
  REPLACE_STRING (msg->origin.sess_version, sess_version);
  REPLACE_STRING (msg->origin.nettype, nettype);
  REPLACE_STRING (msg->origin.addrtype, addrtype);
  REPLACE_STRING (msg->origin.addr, addr);
Wim Taymans's avatar
Wim Taymans committed
511

512
513
514
  return GST_SDP_OK;
}

Wim Taymans's avatar
Wim Taymans committed
515
516
517
518
519
520
521
522
523
/**
 * gst_sdp_message_get_origin:
 * @msg: a #GstSDPMessage
 *
 * Get the origin of @msg.
 *
 * Returns: a #GstSDPOrigin. The result remains valid as long as @msg is valid.
 */
const GstSDPOrigin *
524
gst_sdp_message_get_origin (const GstSDPMessage * msg)
525
526
527
528
{
  return &msg->origin;
}

Wim Taymans's avatar
Wim Taymans committed
529
530
531
532
533
534
535
536
537
/**
 * gst_sdp_message_set_session_name:
 * @msg: a #GstSDPMessage
 * @session_name: the session name
 *
 * Set the session name in @msg.
 *
 * Returns: a #GstSDPResult.
 */
538
DEFINE_STRING_SETTER (session_name);
Wim Taymans's avatar
Wim Taymans committed
539
540
541
542
543
544
545
546
/**
 * gst_sdp_message_get_session_name:
 * @msg: a #GstSDPMessage
 *
 * Get the session name in @msg.
 *
 * Returns: a #GstSDPResult.
 */
547
DEFINE_STRING_GETTER (session_name);
Wim Taymans's avatar
Wim Taymans committed
548
549
550
551
552
553
554
555
556
/**
 * gst_sdp_message_set_information:
 * @msg: a #GstSDPMessage
 * @information: the information
 *
 * Set the information in @msg.
 *
 * Returns: a #GstSDPResult.
 */
557
DEFINE_STRING_SETTER (information);
Wim Taymans's avatar
Wim Taymans committed
558
559
560
561
562
563
564
565
/**
 * gst_sdp_message_get_information:
 * @msg: a #GstSDPMessage
 *
 * Get the information in @msg.
 *
 * Returns: a #GstSDPResult.
 */
566
DEFINE_STRING_GETTER (information);
Wim Taymans's avatar
Wim Taymans committed
567
568
569
570
571
572
573
574
575
/**
 * gst_sdp_message_set_uri:
 * @msg: a #GstSDPMessage
 * @uri: the URI
 *
 * Set the URI in @msg.
 *
 * Returns: a #GstSDPResult.
 */
576
DEFINE_STRING_SETTER (uri);
Wim Taymans's avatar
Wim Taymans committed
577
578
579
580
581
582
583
584
/**
 * gst_sdp_message_get_uri:
 * @msg: a #GstSDPMessage
 *
 * Get the URI in @msg.
 *
 * Returns: a #GstSDPResult.
 */
585
586
DEFINE_STRING_GETTER (uri);

Wim Taymans's avatar
Wim Taymans committed
587
588
589
590
591
592
593
594
/**
 * gst_sdp_message_emails_len:
 * @msg: a #GstSDPMessage
 *
 * Get the number of emails in @msg.
 *
 * Returns: the number of emails in @msg.
 */
595
DEFINE_ARRAY_LEN (emails);
Wim Taymans's avatar
Wim Taymans committed
596
597
598
599
600
601
602
/**
 * gst_sdp_message_get_email:
 * @msg: a #GstSDPMessage
 * @idx: an email index
 *
 * Get the email with number @idx from @msg.
 *
603
 * Returns: the email at position @idx.
Wim Taymans's avatar
Wim Taymans committed
604
 */
605
DEFINE_PTR_ARRAY_GETTER (email, emails, const gchar *);
606

Wim Taymans's avatar
Wim Taymans committed
607
608
609
610
611
612
613
614
615
/**
 * gst_sdp_message_add_email:
 * @msg: a #GstSDPMessage
 * @email: an email
 *
 * Add @email to the list of emails in @msg.
 *
 * Returns: a #GstSDPResult.
 */
616
DEFINE_PTR_ARRAY_ADDER (email, emails, const gchar *, g_strdup);
Wim Taymans's avatar
Wim Taymans committed
617
618
619
620
621
622
623
624
625

/**
 * gst_sdp_message_phones_len:
 * @msg: a #GstSDPMessage
 *
 * Get the number of phones in @msg.
 *
 * Returns: the number of phones in @msg.
 */
626
DEFINE_ARRAY_LEN (phones);
Wim Taymans's avatar
Wim Taymans committed
627
628
629
630
631
632
633
/**
 * gst_sdp_message_get_phone:
 * @msg: a #GstSDPMessage
 * @idx: a phone index
 *
 * Get the phone with number @idx from @msg.
 *
634
 * Returns: the phone at position @idx.
Wim Taymans's avatar
Wim Taymans committed
635
 */
636
DEFINE_PTR_ARRAY_GETTER (phone, phones, const gchar *);
Wim Taymans's avatar
Wim Taymans committed
637
638
639
640
641
642
643
644
645
646

/**
 * gst_sdp_message_add_phone:
 * @msg: a #GstSDPMessage
 * @phone: a phone
 *
 * Add @phone to the list of phones in @msg.
 *
 * Returns: a #GstSDPResult.
 */
647
DEFINE_PTR_ARRAY_ADDER (phone, phones, const gchar *, g_strdup);
648

Wim Taymans's avatar
Wim Taymans committed
649
650
651
652
653
654
655
656
657
658
659
660
661
662
/**
 * gst_sdp_message_set_connection:
 * @msg: a #GstSDPMessage
 * @nettype: the type of network. "IN" is defined to have the meaning
 * "Internet".
 * @addrtype: the type of address.
 * @address: the address
 * @ttl: the time to live of the address
 * @addr_number: the number of layers
 *
 * Configure the SDP connection in @msg with the given parameters.
 *
 * Returns: a #GstSDPResult.
 */
663
GstSDPResult
Wim Taymans's avatar
Wim Taymans committed
664
gst_sdp_message_set_connection (GstSDPMessage * msg, const gchar * nettype,
665
    const gchar * addrtype, const gchar * address, guint ttl, guint addr_number)
666
667
668
669
670
671
{
  REPLACE_STRING (msg->connection.nettype, nettype);
  REPLACE_STRING (msg->connection.addrtype, addrtype);
  REPLACE_STRING (msg->connection.address, address);
  msg->connection.ttl = ttl;
  msg->connection.addr_number = addr_number;
Wim Taymans's avatar
Wim Taymans committed
672

673
674
675
  return GST_SDP_OK;
}

Wim Taymans's avatar
Wim Taymans committed
676
677
678
679
680
681
682
683
684
/**
 * gst_sdp_message_get_connection:
 * @msg: a #GstSDPMessage
 *
 * Get the connection of @msg.
 *
 * Returns: a #GstSDPConnection. The result remains valid as long as @msg is valid.
 */
const GstSDPConnection *
685
gst_sdp_message_get_connection (const GstSDPMessage * msg)
686
687
688
689
{
  return &msg->connection;
}

Wim Taymans's avatar
Wim Taymans committed
690
691
692
693
694
695
696
697
/**
 * gst_sdp_message_bandwidths_len:
 * @msg: a #GstSDPMessage
 *
 * Get the number of bandwidth information in @msg.
 *
 * Returns: the number of bandwidth information in @msg.
 */
698
DEFINE_ARRAY_LEN (bandwidths);
Wim Taymans's avatar
Wim Taymans committed
699
700
701
702
703
704
705
706
707
/**
 * gst_sdp_message_get_bandwidth:
 * @msg: a #GstSDPMessage
 * @idx: the bandwidth index
 *
 * Get the bandwidth at index @idx from @msg.
 *
 * Returns: a #GstSDPBandwidth.
 */
708
DEFINE_ARRAY_GETTER (bandwidth, bandwidths, const GstSDPBandwidth);
709

Wim Taymans's avatar
Wim Taymans committed
710
711
712
/**
 * gst_sdp_message_add_bandwidth:
 * @msg: a #GstSDPMessage
713
 * @bwtype: the bandwidth modifier type
Wim Taymans's avatar
Wim Taymans committed
714
715
716
717
718
719
 * @bandwidth: the bandwidth in kilobits per second
 *
 * Add the specified bandwidth information to @msg.
 *
 * Returns: a #GstSDPResult.
 */
720

721
GstSDPResult
Wim Taymans's avatar
Wim Taymans committed
722
gst_sdp_message_add_bandwidth (GstSDPMessage * msg, const gchar * bwtype,
723
    guint bandwidth)
724
725
726
727
728
729
730
731
732
733
734
{
  GstSDPBandwidth bw;

  bw.bwtype = g_strdup (bwtype);
  bw.bandwidth = bandwidth;

  g_array_append_val (msg->bandwidths, bw);

  return GST_SDP_OK;
}

Wim Taymans's avatar
Wim Taymans committed
735
736
737
738
739
740
741
742
/**
 * gst_sdp_message_times_len:
 * @msg: a #GstSDPMessage
 *
 * Get the number of time information entries in @msg.
 *
 * Returns: the number of time information entries in @msg.
 */
743
744
DEFINE_ARRAY_LEN (times);

Wim Taymans's avatar
Wim Taymans committed
745
746
747
748
749
750
751
752
753
/**
 * gst_sdp_message_get_time:
 * @msg: a #GstSDPMessage
 * @idx: the time index
 *
 * Get time information with index @idx from @msg.
 *
 * Returns: a #GstSDPTime.
 */
754
DEFINE_ARRAY_GETTER (time, times, const GstSDPTime);
Wim Taymans's avatar
Wim Taymans committed
755
756
757
758

/**
 * gst_sdp_message_add_time:
 * @msg: a #GstSDPMessage
759
760
761
 * @start: the start time
 * @stop: the stop time
 * @repeat: the repeat times
Wim Taymans's avatar
Wim Taymans committed
762
 *
763
 * Add time information @start and @stop to @msg.
Wim Taymans's avatar
Wim Taymans committed
764
765
766
 *
 * Returns: a #GstSDPResult.
 */
767
GstSDPResult
768
769
gst_sdp_message_add_time (GstSDPMessage * msg, const gchar * start,
    const gchar * stop, const gchar ** repeat)
770
{
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
  GstSDPTime times;

  times.start = g_strdup (start);
  times.stop = g_strdup (stop);
  if (repeat) {
    times.repeat = g_array_new (FALSE, TRUE, sizeof (gchar *));
    for (; *repeat; repeat++) {
      gchar *r = g_strdup (*repeat);

      g_array_append_val (times.repeat, r);
    }
  } else
    times.repeat = NULL;
  g_array_append_val (msg->times, times);

786
787
788
  return GST_SDP_OK;
}

Wim Taymans's avatar
Wim Taymans committed
789
790
791
792
793
794
795
796
/**
 * gst_sdp_message_zones_len:
 * @msg: a #GstSDPMessage
 *
 * Get the number of time zone information entries in @msg.
 *
 * Returns: the number of time zone information entries in @msg.
 */
797
DEFINE_ARRAY_LEN (zones);
Wim Taymans's avatar
Wim Taymans committed
798
799
800
801
802
803
804
805
806
/**
 * gst_sdp_message_get_zone:
 * @msg: a #GstSDPMessage
 * @idx: the zone index
 *
 * Get time zone information with index @idx from @msg.
 *
 * Returns: a #GstSDPZone.
 */
807
DEFINE_ARRAY_GETTER (zone, zones, const GstSDPZone);
Wim Taymans's avatar
Wim Taymans committed
808
809
810
811
812

/**
 * gst_sdp_message_add_zone:
 * @msg: a #GstSDPMessage
 * @time: the NTP time that a time zone adjustment happens
813
 * @typed_time: the offset from the time when the session was first scheduled
Wim Taymans's avatar
Wim Taymans committed
814
815
816
817
818
 *
 * Add time zone information to @msg.
 *
 * Returns: a #GstSDPResult.
 */
819
GstSDPResult
820
gst_sdp_message_add_zone (GstSDPMessage * msg, const gchar * adj_time,
Wim Taymans's avatar
Wim Taymans committed
821
    const gchar * typed_time)
822
823
824
{
  GstSDPZone zone;

825
  zone.time = g_strdup (adj_time);
826
827
828
829
830
831
832
  zone.typed_time = g_strdup (typed_time);

  g_array_append_val (msg->zones, zone);

  return GST_SDP_OK;
}

Wim Taymans's avatar
Wim Taymans committed
833
834
835
836
837
838
839
840
841
842
/**
 * gst_sdp_message_set_key:
 * @msg: a #GstSDPMessage
 * @type: the encryption type
 * @data: the encryption data
 *
 * Adds the encryption information to @msg.
 *
 * Returns: a #GstSDPResult.
 */
843
GstSDPResult
Wim Taymans's avatar
Wim Taymans committed
844
845
gst_sdp_message_set_key (GstSDPMessage * msg, const gchar * type,
    const gchar * data)
846
847
848
{
  REPLACE_STRING (msg->key.type, type);
  REPLACE_STRING (msg->key.data, data);
Wim Taymans's avatar
Wim Taymans committed
849

850
851
852
  return GST_SDP_OK;
}

Wim Taymans's avatar
Wim Taymans committed
853
854
855
856
857
858
859
860
861
/**
 * gst_sdp_message_get_key:
 * @msg: a #GstSDPMessage
 *
 * Get the encryption information from @msg.
 *
 * Returns: a #GstSDPKey.
 */
const GstSDPKey *
862
gst_sdp_message_get_key (const GstSDPMessage * msg)
863
864
865
866
{
  return &msg->key;
}

Wim Taymans's avatar
Wim Taymans committed
867
868
869
870
871
872
873
874
/**
 * gst_sdp_message_attributes_len:
 * @msg: a #GstSDPMessage
 *
 * Get the number of attributes in @msg.
 *
 * Returns: the number of attributes in @msg.
 */
875
DEFINE_ARRAY_LEN (attributes);
876

Wim Taymans's avatar
Wim Taymans committed
877
878
879
880
881
882
883
884
885
/**
 * gst_sdp_message_get_attribute:
 * @msg: a #GstSDPMessage
 * @idx: the index
 *
 * Get the attribute at position @idx in @msg.
 *
 * Returns: the #GstSDPAttribute at position @idx.
 */
886
887
DEFINE_ARRAY_GETTER (attribute, attributes, const GstSDPAttribute);

Wim Taymans's avatar
Wim Taymans committed
888
889
890
891
892
893
894
895
896
897
898
/**
 * gst_sdp_message_get_attribute_val_n:
 * @msg: a #GstSDPMessage
 * @key: the key
 * @nth: the index
 *
 * Get the @nth attribute with key @key in @msg.
 *
 * Returns: the attribute value of the @nth attribute with @key.
 */
const gchar *
899
900
gst_sdp_message_get_attribute_val_n (const GstSDPMessage * msg,
    const gchar * key, guint nth)
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
{
  guint i;

  for (i = 0; i < msg->attributes->len; i++) {
    GstSDPAttribute *attr;

    attr = &g_array_index (msg->attributes, GstSDPAttribute, i);
    if (!strcmp (attr->key, key)) {
      if (nth == 0)
        return attr->value;
      else
        nth--;
    }
  }
  return NULL;
}

Wim Taymans's avatar
Wim Taymans committed
918
919
920
921
922
923
924
925
926
927
/**
 * gst_sdp_message_get_attribute_val:
 * @msg: a #GstSDPMessage
 * @key: the key
 *
 * Get the first attribute with key @key in @msg.
 *
 * Returns: the attribute value of the first attribute with @key.
 */
const gchar *
928
gst_sdp_message_get_attribute_val (const GstSDPMessage * msg, const gchar * key)
929
930
931
932
{
  return gst_sdp_message_get_attribute_val_n (msg, key, 0);
}

Wim Taymans's avatar
Wim Taymans committed
933
934
935
936
937
938
939
940
941
942
/**
 * gst_sdp_message_add_attribute:
 * @msg: a #GstSDPMessage
 * @key: the key
 * @value: the value
 *
 * Add the attribute with @key and @value to @msg.
 *
 * Returns: @GST_SDP_OK.
 */
943
GstSDPResult
Wim Taymans's avatar
Wim Taymans committed
944
945
gst_sdp_message_add_attribute (GstSDPMessage * msg, const gchar * key,
    const gchar * value)
946
947
948
949
950
951
952
953
954
955
956
{
  GstSDPAttribute attr;

  attr.key = g_strdup (key);
  attr.value = g_strdup (value);

  g_array_append_val (msg->attributes, attr);

  return GST_SDP_OK;
}

Wim Taymans's avatar
Wim Taymans committed
957
958
959
960
961
962
963
964
/**
 * gst_sdp_message_medias_len:
 * @msg: a #GstSDPMessage
 *
 * Get the number of media descriptions in @msg.
 *
 * Returns: the number of media descriptions in @msg.
 */
965
DEFINE_ARRAY_LEN (medias);
Wim Taymans's avatar
Wim Taymans committed
966
967
968
969
970
971
972
973
974
/**
 * gst_sdp_message_get_media:
 * @msg: a #GstSDPMessage
 * @idx: the index
 *
 * Get the media description at index @idx in @msg.
 *
 * Returns: a #GstSDPMedia.
 */
975
DEFINE_ARRAY_GETTER (media, medias, const GstSDPMedia);
976
977
978

/**
 * gst_sdp_message_add_media:
Wim Taymans's avatar
Wim Taymans committed
979
980
 * @msg: a #GstSDPMessage
 * @media: a #GstSDPMedia to add
981
982
983
984
985
 *
 * Adds @media to the array of medias in @msg. This function takes ownership of
 * the contents of @media so that @media will have to be reinitialized with
 * gst_media_init() before it can be used again.
 *
Wim Taymans's avatar
Wim Taymans committed
986
 * Returns: a #GstSDPResult.
987
988
989
990
 */
GstSDPResult
gst_sdp_message_add_media (GstSDPMessage * msg, GstSDPMedia * media)
{
991
  guint len;
992
993
994
995
996
997
998
999
1000
1001
1002
1003
1004
1005
  GstSDPMedia *nmedia;

  len = msg->medias->len;
  g_array_set_size (msg->medias, len + 1);
  nmedia = &g_array_index (msg->medias, GstSDPMedia, len);

  memcpy (nmedia, media, sizeof (GstSDPMedia));
  memset (media, 0, sizeof (GstSDPMedia));

  return GST_SDP_OK;
}

/* media access */

Wim Taymans's avatar
Wim Taymans committed
1006
1007
1008
1009
1010
1011
1012
1013
/**
 * gst_sdp_media_new:
 * @media: pointer to new #GstSDPMedia
 *
 * Allocate a new GstSDPMedia and store the result in @media.
 *
 * Returns: a #GstSDPResult.
 */
1014
GstSDPResult
Wim Taymans's avatar
Wim Taymans committed
1015
gst_sdp_media_new (GstSDPMedia ** media)
1016
{
Wim Taymans's avatar
Wim Taymans committed
1017
  GstSDPMedia *newmedia;
1018

Wim Taymans's avatar
Wim Taymans committed
1019
  g_return_val_if_fail (media != NULL, GST_SDP_EINVAL);
1020

Wim Taymans's avatar
Wim Taymans committed
1021
1022
1023
1024
1025
1026
1027
1028
1029
1030
1031
1032
1033
1034
1035
1036
1037
1038
1039
1040
1041
1042
1043
1044
1045
1046
1047
1048
1049
  newmedia = g_new0 (GstSDPMedia, 1);

  *media = newmedia;

  return gst_sdp_media_init (newmedia);
}

/**
 * gst_sdp_media_init:
 * @media: a #GstSDPMedia
 *
 * Initialize @media so that its contents are as if it was freshly allocated
 * with gst_sdp_media_new(). This function is mostly used to initialize a media
 * allocated on the stack. gst_sdp_media_uninit() undoes this operation.
 *
 * When this function is invoked on newly allocated data (with malloc or on the
 * stack), its contents should be set to 0 before calling this function.
 *
 * Returns: a #GstSDPResult.
 */
GstSDPResult
gst_sdp_media_init (GstSDPMedia * media)
{
  g_return_val_if_fail (media != NULL, GST_SDP_EINVAL);

  FREE_STRING (media->media);
  media->port = 0;
  media->num_ports = 0;
  FREE_STRING (media->proto);
1050
  INIT_PTR_ARRAY (media->fmts, gchar *, g_free);
Wim Taymans's avatar
Wim Taymans committed
1051
1052
1053
1054
1055
  FREE_STRING (media->information);
  INIT_ARRAY (media->connections, GstSDPConnection, gst_sdp_connection_init);
  INIT_ARRAY (media->bandwidths, GstSDPBandwidth, gst_sdp_bandwidth_init);
  gst_sdp_key_init (&media->key);
  INIT_ARRAY (media->attributes, GstSDPAttribute, gst_sdp_attribute_init);
1056
1057
1058
1059

  return GST_SDP_OK;
}

Wim Taymans's avatar
Wim Taymans committed
1060
1061
1062
1063
1064
1065
1066
1067
1068
1069
/**
 * gst_sdp_media_uninit:
 * @media: a #GstSDPMedia
 *
 * Free all resources allocated in @media. @media should not be used anymore after
 * this function. This function should be used when @media was allocated on the
 * stack and initialized with gst_sdp_media_init().
 *
 * Returns: a #GstSDPResult.
 */
1070
GstSDPResult
Wim Taymans's avatar
Wim Taymans committed
1071
1072
1073
1074
1075
gst_sdp_media_uninit (GstSDPMedia * media)
{
  g_return_val_if_fail (media != NULL, GST_SDP_EINVAL);

  gst_sdp_media_init (media);
1076
  FREE_PTR_ARRAY (media->fmts);
Wim Taymans's avatar
Wim Taymans committed
1077
1078
1079
1080
1081
1082
1083
1084
1085
1086
1087
1088
1089
1090
1091
1092
1093
1094
1095
1096
1097
1098
1099
1100
1101
1102
1103
1104
  FREE_ARRAY (media->connections);
  FREE_ARRAY (media->bandwidths);
  FREE_ARRAY (media->attributes);

  return GST_SDP_OK;
}

/**
 * gst_sdp_media_free:
 * @media: a #GstSDPMedia
 *
 * Free all resources allocated by @media. @media should not be used anymore after
 * this function. This function should be used when @media was dynamically
 * allocated with gst_sdp_media_new().
 *
 * Returns: a #GstSDPResult.
 */
GstSDPResult
gst_sdp_media_free (GstSDPMedia * media)
{
  g_return_val_if_fail (media != NULL, GST_SDP_EINVAL);

  gst_sdp_media_uninit (media);
  g_free (media);

  return GST_SDP_OK;
}

1105
1106
1107
1108
1109
1110
1111
1112
1113
1114
1115
1116
1117
1118
1119
1120
1121
1122
1123
1124
1125
1126
1127
1128
1129
1130
1131
1132
1133
1134
1135
1136
1137
1138
1139
1140
1141
1142
1143
1144
1145
1146
1147
1148
1149
1150
1151
1152
1153
1154
1155
1156
1157
1158
1159
1160
1161
1162
1163
1164
1165
1166
1167
1168
1169
1170
1171
1172
1173
1174
1175
1176
1177
1178
1179
1180
1181
1182
1183
1184
1185
/**
 * gst_sdp_media_as_text:
 * @media: a #GstSDPMedia
 *
 * Convert the contents of @media to a text string.
 *
 * Returns: A dynamically allocated string representing the media.
 */
gchar *
gst_sdp_media_as_text (const GstSDPMedia * media)
{
  GString *lines;
  guint i;

  g_return_val_if_fail (media != NULL, NULL);

  lines = g_string_new ("");

  if (media->media)
    g_string_append_printf (lines, "m=%s", media->media);

  g_string_append_printf (lines, " %u", media->port);

  if (media->num_ports > 1)
    g_string_append_printf (lines, "/%u", media->num_ports);

  g_string_append_printf (lines, " %s", media->proto);

  for (i = 0; i < gst_sdp_media_formats_len (media); i++)
    g_string_append_printf (lines, " %s", gst_sdp_media_get_format (media, i));
  g_string_append_printf (lines, "\r\n");

  if (media->information)
    g_string_append_printf (lines, "i=%s", media->information);

  for (i = 0; i < gst_sdp_media_connections_len (media); i++) {
    const GstSDPConnection *conn = gst_sdp_media_get_connection (media, i);

    if (conn->nettype && conn->addrtype && conn->address) {
      guint family;

      g_string_append_printf (lines, "c=%s %s %s", conn->nettype,
          conn->addrtype, conn->address);
      if (is_multicast_address (conn->address, &family)) {
        if (family == AF_INET)
          g_string_append_printf (lines, "/%u", conn->ttl);
        if (conn->addr_number > 1)
          g_string_append_printf (lines, "/%u", conn->addr_number);
      }
      g_string_append_printf (lines, "\r\n");
    }
  }

  for (i = 0; i < gst_sdp_media_bandwidths_len (media); i++) {
    const GstSDPBandwidth *bandwidth = gst_sdp_media_get_bandwidth (media, i);

    g_string_append_printf (lines, "b=%s:%u\r\n", bandwidth->bwtype,
        bandwidth->bandwidth);
  }

  if (media->key.type) {
    g_string_append_printf (lines, "k=%s", media->key.type);
    if (media->key.data)
      g_string_append_printf (lines, ":%s", media->key.data);
    g_string_append_printf (lines, "\r\n");
  }

  for (i = 0; i < gst_sdp_media_attributes_len (media); i++) {
    const GstSDPAttribute *attr = gst_sdp_media_get_attribute (media, i);

    if (attr->key) {
      g_string_append_printf (lines, "a=%s", attr->key);
      if (attr->value)
        g_string_append_printf (lines, ":%s", attr->value);
      g_string_append_printf (lines, "\r\n");
    }
  }

  return g_string_free (lines, FALSE);
}

Wim Taymans's avatar
Wim Taymans committed
1186
1187
1188
1189
1190
1191
1192
1193
1194
/**
 * gst_sdp_media_get_media:
 * @media: a #GstSDPMedia
 *
 * Get the media description of @media.
 *
 * Returns: the media description.
 */
const gchar *
1195
gst_sdp_media_get_media (const GstSDPMedia * media)
Wim Taymans's avatar
Wim Taymans committed
1196
1197
1198
1199
1200
1201
1202
1203
1204
1205
1206
1207
1208
1209
1210
1211
1212
1213
1214
1215
1216
1217
1218
1219
1220
1221
1222
1223
1224
1225
{
  return media->media;
}

/**
 * gst_sdp_media_set_media:
 * @media: a #GstSDPMedia
 * @med: the media description
 *
 * Set the media description of @media to @med.
 *
 * Returns: #GST_SDP_OK.
 */
GstSDPResult
gst_sdp_media_set_media (GstSDPMedia * media, const gchar * med)
{
  g_free (media->media);
  media->media = g_strdup (med);

  return GST_SDP_OK;
}

/**
 * gst_sdp_media_get_port:
 * @media: a #GstSDPMedia
 *
 * Get the port number for @media.
 *
 * Returns: the port number of @media.
 */
1226
guint
1227
gst_sdp_media_get_port (const GstSDPMedia * media)
Wim Taymans's avatar
Wim Taymans committed
1228
1229
1230
1231
1232
1233
1234
1235
1236
1237
1238
1239
{
  return media->port;
}

/**
 * gst_sdp_media_get_num_ports:
 * @media: a #GstSDPMedia
 *
 * Get the number of ports for @media.
 *
 * Returns: the number of ports for @media.
 */
1240
guint
1241
gst_sdp_media_get_num_ports (const GstSDPMedia * media)
Wim Taymans's avatar
Wim Taymans committed
1242
1243
1244
1245
1246
1247
1248
1249
1250
1251
1252
1253
1254
1255
1256
{
  return media->num_ports;
}

/**
 * gst_sdp_media_set_port_info:
 * @media: a #GstSDPMedia
 * @port: the port number
 * @num_ports: the number of ports
 *
 * Set the port information in @media.
 *
 * Returns: #GST_SDP_OK.
 */
GstSDPResult
1257
gst_sdp_media_set_port_info (GstSDPMedia * media, guint port, guint num_ports)
Wim Taymans's avatar
Wim Taymans committed
1258
1259
1260
1261
1262
1263
1264
1265
1266
1267
1268
1269
1270
1271
1272
1273
{
  media->port = port;
  media->num_ports = num_ports;

  return GST_SDP_OK;
}

/**
 * gst_sdp_media_get_proto:
 * @media: a #GstSDPMedia
 *
 * Get the transport protocol of @media
 *
 * Returns: the transport protocol of @media.
 */
const gchar *
1274
gst_sdp_media_get_proto (const GstSDPMedia * media)
Wim Taymans's avatar
Wim Taymans committed
1275
1276
1277
1278
1279
1280
1281
1282
1283
1284
1285
1286
1287
1288
1289
1290
1291
1292
1293
1294
1295
1296
1297
1298
1299
1300
1301
1302
1303
1304
{
  return media->proto;
}

/**
 * gst_sdp_media_set_proto:
 * @media: a #GstSDPMedia
 * @proto: the media transport protocol
 *
 * Set the media transport protocol of @media to @proto.
 *
 * Returns: #GST_SDP_OK.
 */
GstSDPResult
gst_sdp_media_set_proto (GstSDPMedia * media, const gchar * proto)
{
  g_free (media->proto);
  media->proto = g_strdup (proto);

  return GST_SDP_OK;
}

/**
 * gst_sdp_media_formats_len:
 * @media: a #GstSDPMedia
 *
 * Get the number of formats in @media.
 *
 * Returns: the number of formats in @media.
 */
1305
guint
1306
gst_sdp_media_formats_len (const GstSDPMedia * media)
Wim Taymans's avatar
Wim Taymans committed
1307
1308
1309
1310
1311
1312
1313
1314
1315
1316
1317
1318
1319
1320
{
  return media->fmts->len;
}

/**
 * gst_sdp_media_get_format:
 * @media: a #GstSDPMedia
 * @idx: an index
 *
 * Get the format information at position @idx in @media.
 *
 * Returns: the format at position @idx.
 */
const gchar *
1321
gst_sdp_media_get_format (const GstSDPMedia * media, guint idx)
Wim Taymans's avatar
Wim Taymans committed
1322
1323
1324
1325
1326
1327
1328
1329
1330
1331
1332
1333
1334
1335
1336
1337
1338
1339
1340
1341
1342
1343
1344
1345
1346
1347
1348
1349
1350
1351
1352
1353
1354
1355
1356
1357
{
  if (idx >= media->fmts->len)
    return NULL;
  return g_array_index (media->fmts, gchar *, idx);
}

/**
 * gst_sdp_media_add_format:
 * @media: a #GstSDPMedia
 * @format: the format
 *
 * Add the format information to @media.
 *
 * Returns: #GST_SDP_OK.
 */
GstSDPResult
gst_sdp_media_add_format (GstSDPMedia * media, const gchar * format)
{
  gchar *fmt;

  fmt = g_strdup (format);

  g_array_append_val (media->fmts, fmt);

  return GST_SDP_OK;
}

/**
 * gst_sdp_media_get_information:
 * @media: a #GstSDPMedia
 *
 * Get the information of @media
 *
 * Returns: the information of @media.
 */
const gchar *
1358
gst_sdp_media_get_information (const GstSDPMedia * media)
Wim Taymans's avatar
Wim Taymans committed
1359
1360
1361
1362
1363
1364
1365
1366
1367
1368
1369
1370
1371
1372
1373
1374
1375
1376
1377
1378
1379
1380
1381
1382
1383
1384
1385
1386
1387
1388
{
  return media->information;
}

/**
 * gst_sdp_media_set_information:
 * @media: a #GstSDPMedia
 * @information: the media information
 *
 * Set the media information of @media to @information.
 *
 * Returns: #GST_SDP_OK.
 */
GstSDPResult
gst_sdp_media_set_information (GstSDPMedia * media, const gchar * information)
{
  g_free (media->information);
  media->information = g_strdup (information);

  return GST_SDP_OK;
}

/**
 * gst_sdp_media_connections_len:
 * @media: a #GstSDPMedia
 *
 * Get the number of connection fields in @media.
 *
 * Returns: the number of connections in @media.
 */
1389
guint
1390
gst_sdp_media_connections_len (const GstSDPMedia * media)
Wim Taymans's avatar
Wim Taymans committed
1391
{
1392
  return media->connections->len;
Wim Taymans's avatar
Wim Taymans committed
1393
1394
1395
1396
1397
1398
1399
1400
1401
1402
1403
1404
}

/**
 * gst_sdp_media_get_connection:
 * @media: a #GstSDPMedia
 * @idx: an index
 *
 * Get the connection at position @idx in @media.
 *
 * Returns: the #GstSDPConnection at position @idx.
 */
const GstSDPConnection *
1405
gst_sdp_media_get_connection (const GstSDPMedia * media, guint idx)
Wim Taymans's avatar
Wim Taymans committed
1406
1407
1408
1409
1410
1411
1412
1413
1414
1415
1416
1417
1418
1419
1420
1421
1422
1423
1424
1425
{
  return &g_array_index (media->connections, GstSDPConnection, idx);
}

/**
 * gst_sdp_media_add_connection:
 * @media: a #GstSDPMedia
 * @nettype: the type of network. "IN" is defined to have the meaning
 * "Internet".
 * @addrtype: the type of address.
 * @address: the address
 * @ttl: the time to live of the address
 * @addr_number: the number of layers
 *
 * Add the given connection parameters to @media.
 *
 * Returns: a #GstSDPResult.
 */
GstSDPResult
gst_sdp_media_add_connection (GstSDPMedia * media, const gchar * nettype,
1426
    const gchar * addrtype, const gchar * address, guint ttl, guint addr_number)
Wim Taymans's avatar
Wim Taymans committed
1427
1428
1429
1430
1431
1432
1433
1434
1435
1436
1437
1438
1439
1440
1441
1442
1443
1444
1445
1446
1447
1448
{
  GstSDPConnection conn;

  conn.nettype = g_strdup (nettype);
  conn.addrtype = g_strdup (addrtype);
  conn.address = g_strdup (address);
  conn.ttl = ttl;
  conn.addr_number = addr_number;

  g_array_append_val (media->connections, conn);

  return GST_SDP_OK;
}

/**
 * gst_sdp_media_bandwidths_len:
 * @media: a #GstSDPMedia
 *
 * Get the number of bandwidth fields in @media.
 *
 * Returns: the number of bandwidths in @media.
 */
1449
guint
1450
gst_sdp_media_bandwidths_len (const GstSDPMedia * media)
Wim Taymans's avatar
Wim Taymans committed
1451
{
1452
  return media->bandwidths->len;
Wim Taymans's avatar
Wim Taymans committed
1453
1454
1455
1456
1457
1458
1459
1460
1461
1462
1463
1464
}

/**
 * gst_sdp_media_get_bandwidth:
 * @media: a #GstSDPMedia
 * @idx: an index
 *
 * Get the bandwidth at position @idx in @media.
 *
 * Returns: the #GstSDPBandwidth at position @idx.
 */
const GstSDPBandwidth *
1465
gst_sdp_media_get_bandwidth (const GstSDPMedia * media, guint idx)
Wim Taymans's avatar
Wim Taymans committed
1466
1467
1468
1469
1470
1471
1472
1473
1474
1475
1476
1477
1478
1479
1480
1481
{
  return &g_array_index (media->bandwidths, GstSDPBandwidth, idx);
}

/**
 * gst_sdp_media_add_bandwidth:
 * @media: a #GstSDPMedia
 * @bwtype: the bandwidth modifier type
 * @bandwidth: the bandwidth in kilobits per second
 *
 * Add the bandwidth information with @bwtype and @bandwidth to @media.
 *
 * Returns: #GST_SDP_OK.
 */
GstSDPResult
gst_sdp_media_add_bandwidth (GstSDPMedia * media, const gchar * bwtype,
1482
    guint bandwidth)
1483
1484
1485
1486
1487
1488
1489
1490
1491
1492
1493
{
  GstSDPBandwidth bw;

  bw.bwtype = g_strdup (bwtype);
  bw.bandwidth = bandwidth;

  g_array_append_val (media->bandwidths, bw);

  return GST_SDP_OK;
}

Wim Taymans's avatar
Wim Taymans committed
1494
1495
1496
1497
1498
1499
1500
1501
1502
1503
/**
 * gst_sdp_media_set_key:
 * @media: a #GstSDPMedia
 * @type: the encryption type
 * @data: the encryption data
 *
 * Adds the encryption information to @media.
 *
 * Returns: a #GstSDPResult.
 */
1504
GstSDPResult
Wim Taymans's avatar
Wim Taymans committed
1505
1506
gst_sdp_media_set_key (GstSDPMedia * media, const gchar * type,
    const gchar * data)
1507
{
Wim Taymans's avatar
Wim Taymans committed
1508
1509
1510
1511
  g_free (media->key.type);
  media->key.type = g_strdup (type);
  g_free (media->key.data);
  media->key.data = g_strdup (data);
1512

Wim Taymans's avatar
Wim Taymans committed
1513
1514
  return GST_SDP_OK;
}
1515

Wim Taymans's avatar
Wim Taymans committed
1516
1517
1518
1519
1520
1521
1522
1523
1524
/**
 * gst_sdp_media_get_key:
 * @media: a #GstSDPMedia
 *
 * Get the encryption information from @media.
 *
 * Returns: a #GstSDPKey.
 */
const GstSDPKey *
1525
gst_sdp_media_get_key (const GstSDPMedia * media)
Wim Taymans's avatar
Wim Taymans committed
1526
1527
1528
1529
1530
1531
1532
1533
1534
1535
1536
1537
{
  return &media->key;
}

/**
 * gst_sdp_media_attributes_len:
 * @media: a #GstSDPMedia
 *
 * Get the number of attribute fields in @media.
 *
 * Returns: the number of attributes in @media.
 */
1538
guint
1539
gst_sdp_media_attributes_len (const GstSDPMedia * media)
Wim Taymans's avatar
Wim Taymans committed
1540
{
1541
  return media->attributes->len;
Wim Taymans's avatar
Wim Taymans committed
1542
1543
1544
1545
1546
1547
1548
1549
1550
1551
1552
1553
1554
1555
1556
1557
1558
1559
1560
1561
1562
1563
}

/**
 * gst_sdp_media_add_attribute:
 * @media: a #GstSDPMedia
 * @key: a key
 * @value: a value
 *
 * Add the attribute with @key and @value to @media.
 *
 * Returns: #GST_SDP_OK.
 */
GstSDPResult
gst_sdp_media_add_attribute (GstSDPMedia * media, const gchar * key,
    const gchar * value)
{
  GstSDPAttribute attr;

  attr.key = g_strdup (key);
  attr.value = g_strdup (value);

  g_array_append_val (media->attributes, attr);
1564
1565
1566
1567

  return GST_SDP_OK;
}

Wim Taymans's avatar
Wim Taymans committed
1568
1569
1570
1571
1572
1573
1574
1575
1576
1577
/**
 * gst_sdp_media_get_attribute:
 * @media: a #GstSDPMedia
 * @idx: an index
 *
 * Get the attribute at position @idx in @media.
 *
 * Returns: the #GstSDPAttribute at position @idx.
 */
const GstSDPAttribute *
1578
gst_sdp_media_get_attribute (const GstSDPMedia * media, guint idx)
1579
1580
1581
1582
{
  return &g_array_index (media->attributes, GstSDPAttribute, idx);
}

Wim Taymans's avatar
Wim Taymans committed
1583
1584
1585
1586
1587
1588
1589
1590
1591
1592
1593
/**
 * gst_sdp_media_get_attribute_val_n:
 * @media: a #GstSDPMedia
 * @key: a key
 * @nth: an index
 *
 * Get the @nth attribute value for @key in @media.
 *
 * Returns: the @nth attribute value.
 */
const gchar *
1594
gst_sdp_media_get_attribute_val_n (const GstSDPMedia * media, const gchar * key,
Wim Taymans's avatar
Wim Taymans committed
1595
    guint nth)
1596
1597
1598
1599
1600
1601
1602
1603
1604
1605
1606
1607
1608
1609
1610
1611
1612
{
  guint i;

  for (i = 0; i < media->attributes->len; i++) {
    GstSDPAttribute *attr;

    attr = &g_array_index (media->attributes, GstSDPAttribute, i);
    if (!strcmp (attr->key, key)) {
      if (nth == 0)
        return attr->value;
      else
        nth--;
    }
  }
  return NULL;
}

Wim Taymans's avatar
Wim Taymans committed
1613
1614
1615
1616
1617
1618
1619
1620
1621
1622
/**
 * gst_sdp_media_get_attribute_val:
 * @media: a #GstSDPMedia
 * @key: a key
 *
 * Get the first attribute value for @key in @media.
 *
 * Returns: the first attribute value for @key.
 */
const gchar *
1623
gst_sdp_media_get_attribute_val (const GstSDPMedia * media, const gchar * key)
1624
1625
1626
1627
1628
1629
1630
1631
1632
1633
1634
1635
1636
1637
1638
1639
1640
1641
1642
1643
1644
1645
1646
1647
1648
1649
1650
1651
1652
1653
1654
1655
1656
1657
1658
1659
1660
1661
1662
1663
1664
1665
1666
1667
1668
1669
1670
1671
1672
1673
{
  return gst_sdp_media_get_attribute_val_n (media, key, 0);
}

static void
read_string (gchar * dest, guint size, gchar ** src)
{
  guint idx;

  idx = 0;
  /* skip spaces */
  while (g_ascii_isspace (**src))
    (*src)++;

  while (!g_ascii_isspace (**src) && **src != '\0') {
    if (idx < size - 1)
      dest[idx++] = **src;
    (*src)++;
  }
  if (size > 0)
    dest[idx] = '\0';
}

static void
read_string_del (gchar * dest, guint size, gchar del, gchar ** src)
{
  guint idx;

  idx = 0;
  /* skip spaces */
  while (g_ascii_isspace (**src))
    (*src)++;

  while (**src != del && **src != '\0') {
    if (idx < size - 1)
      dest[idx++] = **src;
    (*src)++;
  }
  if (size > 0)
    dest[idx] = '\0';
}

enum
{
  SDP_SESSION,
  SDP_MEDIA,
};

typedef struct
{
1674
  guint state;
1675
1676
1677
1678
1679
1680
1681
1682
1683
1684
  GstSDPMessage *msg;
  GstSDPMedia *media;
} SDPContext;

static gboolean
gst_sdp_parse_line (SDPContext * c, gchar type, gchar * buffer)
{
  gchar str[8192];
  gchar *p = buffer;

1685
1686
#define READ_STRING(field) read_string (str, sizeof (str), &p); REPLACE_STRING (field, str)
#define READ_UINT(field) read_string (str, sizeof (str), &p); field = strtoul (str, NULL, 10)
1687
1688
1689
1690
1691
1692
1693
1694
1695
1696
1697
1698
1699
1700
1701
1702
1703
1704
1705
1706
1707
1708
1709
1710
1711
1712
1713
1714
1715
1716
1717
1718
1719
1720
1721
1722
1723
1724

  switch (type) {
    case 'v':
      if (buffer[0] != '0')
        g_warning ("wrong SDP version");
      gst_sdp_message_set_version (c->msg, buffer);
      break;
    case 'o':
      READ_STRING (c->msg->origin.username);
      READ_STRING (c->msg->origin.sess_id);
      READ_STRING (c->msg->origin.sess_version);
      READ_STRING (c->msg->origin.nettype);
      READ_STRING (c->msg->origin.addrtype);
      READ_STRING (c->msg->origin.addr);
      break;
    case 's':
      REPLACE_STRING (c->msg->session_name, buffer);
      break;
    case 'i':
      if (c->state == SDP_SESSION) {
        REPLACE_STRING (c->msg->information, buffer);
      } else {
        REPLACE_STRING (c->media->information, buffer);
      }
      break;
    case 'u':
      REPLACE_STRING (c->msg->uri, buffer);
      break;
    case 'e':
      gst_sdp_message_add_email (c->msg, buffer);
      break;
    case 'p':
      gst_sdp_message_add_phone (c->msg, buffer);
      break;
    case 'c':
      READ_STRING (c->msg->connection.nettype);
      READ_STRING (c->msg->connection.addrtype);
      READ_STRING (c->msg->connection.address);
1725
1726
      READ_UINT (c->msg->connection.ttl);
      READ_UINT (c->msg->connection.addr_number);
1727
1728
1729
1730
1731
1732
1733
1734
1735
1736
1737
1738
1739
1740
1741
1742
1743
1744
1745
1746
1747
1748
1749
1750
1751
1752
1753
1754
1755
1756
1757
1758
1759
1760
1761
1762
1763
1764
1765
1766
1767
1768
1769
1770
1771
1772
1773
1774
1775
1776
1777
1778
1779
1780
1781
1782
1783
1784
1785
1786
1787
1788
1789
      break;
    case 'b':
    {
      gchar str2[MAX_LINE_LEN];

      read_string_del (str, sizeof (str), ':', &p);
      read_string (str2, sizeof (str2), &p);
      if (c->state == SDP_SESSION)
        gst_sdp_message_add_bandwidth (c->msg, str, atoi (str2));
      else
        gst_sdp_media_add_bandwidth (c->media, str, atoi (str2));
      break;
    }
    case 't':
      break;
    case 'k':
      break;
    case 'a':
      read_string_del (str, sizeof (str), ':', &p);
      if (*p != '\0')
        p++;
      if (c->state == SDP_SESSION)
        gst_sdp_message_add_attribute (c->msg, str, p);
      else
        gst_sdp_media_add_attribute (c->media, str, p);
      break;
    case 'm':
    {
      gchar *slash;
      GstSDPMedia nmedia;

      c->state = SDP_MEDIA;
      memset (&nmedia, 0, sizeof (nmedia));
      gst_sdp_media_init (&nmedia);

      READ_STRING (nmedia.media);
      read_string (str, sizeof (str), &p);
      slash = g_strrstr (str, "/");
      if (slash) {
        *slash = '\0';
        nmedia.port = atoi (str);
        nmedia.num_ports = atoi (slash + 1);
      } else {
        nmedia.port = atoi (str);
        nmedia.num_ports = -1;
      }
      READ_STRING (nmedia.proto);
      do {
        read_string (str, sizeof (str), &p);
        gst_sdp_media_add_format (&nmedia, str);
      } while (*p != '\0');

      gst_sdp_message_add_media (c->msg, &nmedia);
      c->media =
          &g_array_index (c->msg->medias, GstSDPMedia, c->msg->medias->len - 1);
      break;
    }
    default:
      break;
  }
  return TRUE;
}

Wim Taymans's avatar
Wim Taymans committed
1790
1791
1792
1793
1794
1795
1796
1797
1798
1799
1800
/**
 * gst_sdp_message_parse_buffer:
 * @data: the start of the buffer
 * @size: the size of the buffer
 * @msg: the result #GstSDPMessage
 *
 * Parse the contents of @size bytes pointed to by @data and store the result in
 * @msg.
 *
 * Returns: #GST_SDP_OK on success.
 */
1801
GstSDPResult
Wim Taymans's avatar
Wim Taymans committed
1802
1803
gst_sdp_message_parse_buffer (const guint8 * data, guint size,
    GstSDPMessage * msg)
1804
1805
1806
1807
1808
1809
1810
1811
1812
1813
1814
1815
1816
1817
1818
1819
1820
1821
1822
1823
1824
1825
1826
1827
1828
1829
1830
1831
1832
1833
1834
1835
1836
1837
1838
1839
1840
1841
1842
1843
1844
1845
1846
1847
1848
1849
1850
1851
1852
1853
1854
{
  gchar *p;
  SDPContext c;
  gchar type;
  gchar buffer[MAX_LINE_LEN];
  guint idx = 0;

  g_return_val_if_fail (msg != NULL, GST_SDP_EINVAL);
  g_return_val_if_fail (data != NULL, GST_SDP_EINVAL);
  g_return_val_if_fail (size != 0, GST_SDP_EINVAL);

  c.state = SDP_SESSION;
  c.msg = msg;
  c.media = NULL;

  p = (gchar *) data;
  while (TRUE) {
    while (g_ascii_isspace (*p))
      p++;

    type = *p++;
    if (type == '\0')
      break;

    if (*p != '=')
      goto line_done;
    p++;

    idx = 0;
    while (*p != '\n' && *p != '\r' && *p != '\0') {
      if (idx < sizeof (buffer) - 1)
        buffer[idx++] = *p;
      p++;
    }
    buffer[idx] = '\0';
    gst_sdp_parse_line (&c, type, buffer);

  line_done:
    while (*p != '\n' && *p != '\0')
      p++;
    if (*p == '\n')
      p++;
  }

  return GST_SDP_OK;
}

static void
print_media (GstSDPMedia * media)
{
  g_print ("   media:       '%s'\n", media->media);
1855
1856
  g_print ("   port:        '%u'\n", media->port);
  g_print ("   num_ports:   '%u'\n", media->num_ports);
1857
1858
1859
1860
1861
1862
1863
1864
1865
1866
1867
1868
1869
1870
1871
1872
1873
1874
1875
1876
1877
1878
1879
1880
1881
1882
  g_print ("   proto:       '%s'\n", media->proto);
  if (media->fmts->len > 0) {
    guint i;

    g_print ("   formats:\n");
    for (i = 0; i < media->fmts->len; i++) {
      g_print ("    format  '%s'\n", g_array_index (media->fmts, gchar *, i));
    }
  }
  g_print ("   information: '%s'\n", media->information);
  g_print ("   key:\n");
  g_print ("    type:       '%s'\n", media->key.type);
  g_print ("    data:       '%s'\n", media->key.data);
  if (media->attributes->len > 0) {
    guint i;

    g_print ("   attributes:\n");
    for (i = 0; i < media->attributes->len; i++) {
      GstSDPAttribute *attr =
          &g_array_index (media->attributes, GstSDPAttribute, i);

      g_print ("    attribute '%s' : '%s'\n", attr->key, attr->value);
    }
  }
}

Wim Taymans's avatar
Wim Taymans committed
1883
1884
1885
1886
1887
1888
1889
1890
/**
 * gst_sdp_message_dump:
 * @msg: a #GstSDPMessage
 *
 * Dump the parsed contents of @msg to stdout.
 *
 * Returns: a #GstSDPResult.
 */
1891
GstSDPResult