gstrtspsrc.c 159 KB
Newer Older
Wim Taymans's avatar
Wim Taymans committed
1
/* GStreamer
2
3
 * Copyright (C) <2005,2006> Wim Taymans <wim at fluendo dot com>
 *               <2006> Lutz Mueller <lutz at topfrose dot de>
Wim Taymans's avatar
Wim Taymans committed
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
 *
 * 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.
 */
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
/*
 * 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.
 */
43
44
45
46
47
48
/**
 * SECTION:element-rtspsrc
 *
 * Makes a connection to an RTSP server and read the data.
 * rtspsrc strictly follows RFC 2326 and therefore does not (yet) support
 * RealMedia/Quicktime/Microsoft extensions.
49
 *
50
51
52
 * RTSP supports transport over TCP or UDP in unicast or multicast mode. By
 * default rtspsrc will negotiate a connection in the following order:
 * UDP unicast/UDP multicast/TCP. The order cannot be changed but the allowed
53
54
 * protocols can be controlled with the #GstRTSPSrc:protocols property.
 *
55
 * rtspsrc currently understands SDP as the format of the session description.
56
 * For each stream listed in the SDP a new rtp_stream%d pad will be created
57
 * with caps derived from the SDP media description. This is a caps of mime type
58
 * "application/x-rtp" that can be connected to any available RTP depayloader
59
 * element. 
60
 *
61
62
63
 * rtspsrc will internally instantiate an RTP session manager element
 * that will handle the RTCP messages to and from the server, jitter removal,
 * packet reordering along with providing a clock for the pipeline. 
64
65
 * This feature is currently fully implemented with the gstrtpbin in the
 * gst-plugins-bad module.
66
 *
67
68
 * rtspsrc acts like a live source and will therefore only generate data in the 
 * PLAYING state.
69
70
 *
 * <refsect2>
71
 * <title>Example launch line</title>
72
 * |[
73
 * gst-launch rtspsrc location=rtsp://some.server/url ! fakesink
74
75
 * ]| Establish a connection to an RTSP server and send the raw RTP packets to a
 * fakesink.
76
77
 * </refsect2>
 *
Wim Taymans's avatar
Wim Taymans committed
78
 * Last reviewed on 2006-08-18 (0.10.5)
79
 */
Wim Taymans's avatar
Wim Taymans committed
80
81
82
83
84

#ifdef HAVE_CONFIG_H
#include "config.h"
#endif

85
#ifdef HAVE_UNISTD_H
Wim Taymans's avatar
Wim Taymans committed
86
#include <unistd.h>
87
#endif /* HAVE_UNISTD_H */
88
#include <stdlib.h>
Wim Taymans's avatar
Wim Taymans committed
89
#include <string.h>
90
91
92
#include <locale.h>
#include <stdio.h>
#include <stdarg.h>
Wim Taymans's avatar
Wim Taymans committed
93

94
#include <gst/sdp/gstsdpmessage.h>
95
#include <gst/rtp/gstrtppayloads.h>
96

97
98
#include "gst/gst-i18n-plugin.h"

Wim Taymans's avatar
Wim Taymans committed
99
100
#include "gstrtspsrc.h"

101
102
103
104
#ifdef G_OS_WIN32
#include <winsock2.h>
#endif

105
GST_DEBUG_CATEGORY_STATIC (rtspsrc_debug);
Wim Taymans's avatar
Wim Taymans committed
106
107
#define GST_CAT_DEFAULT (rtspsrc_debug)

Wim Taymans's avatar
Wim Taymans committed
108
/* elementfactory information */
109
static const GstElementDetails gst_rtspsrc_details =
Wim Taymans's avatar
Wim Taymans committed
110
111
GST_ELEMENT_DETAILS ("RTSP packet receiver",
    "Source/Network",
Wim Taymans's avatar
Wim Taymans committed
112
    "Receive data over the network via RTSP (RFC 2326)",
113
114
115
    "Wim Taymans <wim@fluendo.com>\n"
    "Thijs Vermeir <thijs.vermeir@barco.com>\n"
    "Lutz Mueller <lutz@topfrose.de>");
Wim Taymans's avatar
Wim Taymans committed
116

117
static GstStaticPadTemplate rtptemplate = GST_STATIC_PAD_TEMPLATE ("stream%d",
Wim Taymans's avatar
Wim Taymans committed
118
119
    GST_PAD_SRC,
    GST_PAD_SOMETIMES,
120
    GST_STATIC_CAPS ("application/x-rtp; application/x-rdt"));
Wim Taymans's avatar
Wim Taymans committed
121

122
123
124
/* templates used internally */
static GstStaticPadTemplate anysrctemplate =
GST_STATIC_PAD_TEMPLATE ("internalsrc%d",
125
126
127
128
    GST_PAD_SRC,
    GST_PAD_SOMETIMES,
    GST_STATIC_CAPS_ANY);

129
130
131
132
133
134
static GstStaticPadTemplate anysinktemplate =
GST_STATIC_PAD_TEMPLATE ("internalsink%d",
    GST_PAD_SINK,
    GST_PAD_SOMETIMES,
    GST_STATIC_CAPS_ANY);

Wim Taymans's avatar
Wim Taymans committed
135
136
137
138
139
140
enum
{
  /* FILL ME */
  LAST_SIGNAL
};

141
142
143
144
145
146
#define DEFAULT_LOCATION         NULL
#define DEFAULT_PROTOCOLS        GST_RTSP_LOWER_TRANS_UDP | GST_RTSP_LOWER_TRANS_UDP_MCAST | GST_RTSP_LOWER_TRANS_TCP
#define DEFAULT_DEBUG            FALSE
#define DEFAULT_RETRY            20
#define DEFAULT_TIMEOUT          5000000
#define DEFAULT_TCP_TIMEOUT      20000000
147
#define DEFAULT_LATENCY_MS       2000
148
#define DEFAULT_CONNECTION_SPEED 0
149
#define DEFAULT_NAT_METHOD       GST_RTSP_NAT_DUMMY
150
#define DEFAULT_DO_RTCP          TRUE
Wim Taymans's avatar
Wim Taymans committed
151
#define DEFAULT_PROXY            NULL
152
#define DEFAULT_RTP_BLOCKSIZE    0
Wim Taymans's avatar
Wim Taymans committed
153
154
#define DEFAULT_USER_ID          NULL
#define DEFAULT_USER_PW          NULL
Wim Taymans's avatar
Wim Taymans committed
155
156
157
158
159
160
161

enum
{
  PROP_0,
  PROP_LOCATION,
  PROP_PROTOCOLS,
  PROP_DEBUG,
Wim Taymans's avatar
Wim Taymans committed
162
  PROP_RETRY,
163
  PROP_TIMEOUT,
164
  PROP_TCP_TIMEOUT,
165
  PROP_LATENCY,
166
167
  PROP_CONNECTION_SPEED,
  PROP_NAT_METHOD,
168
  PROP_DO_RTCP,
Wim Taymans's avatar
Wim Taymans committed
169
  PROP_PROXY,
170
  PROP_RTP_BLOCKSIZE,
171
172
  PROP_USER_ID,
  PROP_USER_PW,
173
  PROP_LAST
Wim Taymans's avatar
Wim Taymans committed
174
175
};

176
#define GST_TYPE_RTSP_LOWER_TRANS (gst_rtsp_lower_trans_get_type())
Wim Taymans's avatar
Wim Taymans committed
177
static GType
178
gst_rtsp_lower_trans_get_type (void)
Wim Taymans's avatar
Wim Taymans committed
179
{
180
181
  static GType rtsp_lower_trans_type = 0;
  static const GFlagsValue rtsp_lower_trans[] = {
182
183
184
    {GST_RTSP_LOWER_TRANS_UDP, "UDP Unicast Mode", "udp-unicast"},
    {GST_RTSP_LOWER_TRANS_UDP_MCAST, "UDP Multicast Mode", "udp-multicast"},
    {GST_RTSP_LOWER_TRANS_TCP, "TCP interleaved mode", "tcp"},
185
    {GST_RTSP_LOWER_TRANS_HTTP, "HTTP tunneled mode", "http"},
Wim Taymans's avatar
Wim Taymans committed
186
187
188
    {0, NULL, NULL},
  };

189
190
191
  if (!rtsp_lower_trans_type) {
    rtsp_lower_trans_type =
        g_flags_register_static ("GstRTSPLowerTrans", rtsp_lower_trans);
Wim Taymans's avatar
Wim Taymans committed
192
  }
193
  return rtsp_lower_trans_type;
Wim Taymans's avatar
Wim Taymans committed
194
195
}

196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
#define GST_TYPE_RTSP_NAT_METHOD (gst_rtsp_nat_method_get_type())
static GType
gst_rtsp_nat_method_get_type (void)
{
  static GType rtsp_nat_method_type = 0;
  static const GEnumValue rtsp_nat_method[] = {
    {GST_RTSP_NAT_NONE, "None", "none"},
    {GST_RTSP_NAT_DUMMY, "Send Dummy packets", "dummy"},
    {0, NULL, NULL},
  };

  if (!rtsp_nat_method_type) {
    rtsp_nat_method_type =
        g_enum_register_static ("GstRTSPNatMethod", rtsp_nat_method);
  }
  return rtsp_nat_method_type;
}

Wim Taymans's avatar
Wim Taymans committed
214
static void gst_rtspsrc_base_init (gpointer g_class);
215
static void gst_rtspsrc_finalize (GObject * object);
Wim Taymans's avatar
Wim Taymans committed
216

217
218
219
220
221
static void gst_rtspsrc_set_property (GObject * object, guint prop_id,
    const GValue * value, GParamSpec * pspec);
static void gst_rtspsrc_get_property (GObject * object, guint prop_id,
    GValue * value, GParamSpec * pspec);

Wim Taymans's avatar
Wim Taymans committed
222
223
static void gst_rtspsrc_uri_handler_init (gpointer g_iface,
    gpointer iface_data);
224
225
226
227

static void gst_rtspsrc_sdp_attributes_to_caps (GArray * attributes,
    GstCaps * caps);

Wim Taymans's avatar
Wim Taymans committed
228
229
230
static gboolean gst_rtspsrc_set_proxy (GstRTSPSrc * rtsp, const gchar * proxy);
static void gst_rtspsrc_set_tcp_timeout (GstRTSPSrc * rtspsrc, guint64 timeout);

231
static GstCaps *gst_rtspsrc_media_to_caps (gint pt, const GstSDPMedia * media);
Wim Taymans's avatar
Wim Taymans committed
232

233
234
static GstStateChangeReturn gst_rtspsrc_change_state (GstElement * element,
    GstStateChange transition);
235
static void gst_rtspsrc_handle_message (GstBin * bin, GstMessage * message);
Wim Taymans's avatar
Wim Taymans committed
236

237
238
static void gst_rtspsrc_loop_send_cmd (GstRTSPSrc * src, gint cmd,
    gboolean flush);
239
240
241
static GstRTSPResult gst_rtspsrc_send_cb (GstRTSPExtension * ext,
    GstRTSPMessage * request, GstRTSPMessage * response, GstRTSPSrc * src);

242
static gboolean gst_rtspsrc_open (GstRTSPSrc * src);
243
static gboolean gst_rtspsrc_play (GstRTSPSrc * src, GstSegment * segment);
244
245
246
static gboolean gst_rtspsrc_pause (GstRTSPSrc * src);
static gboolean gst_rtspsrc_close (GstRTSPSrc * src);

247
248
249
static gboolean gst_rtspsrc_uri_set_uri (GstURIHandler * handler,
    const gchar * uri);

250
static gboolean gst_rtspsrc_activate_streams (GstRTSPSrc * src);
Wim Taymans's avatar
Wim Taymans committed
251
static void gst_rtspsrc_loop (GstRTSPSrc * src);
252
253
static void gst_rtspsrc_stream_push_event (GstRTSPSrc * src,
    GstRTSPStream * stream, GstEvent * event);
254
static void gst_rtspsrc_push_event (GstRTSPSrc * src, GstEvent * event);
255
static gchar *gst_rtspsrc_dup_printf (const gchar * format, ...);
Wim Taymans's avatar
Wim Taymans committed
256

257
258
259
260
261
/* commands we send to out loop to notify it of events */
#define CMD_WAIT	0
#define CMD_RECONNECT	1
#define CMD_STOP	2

Wim Taymans's avatar
Wim Taymans committed
262
263
/*static guint gst_rtspsrc_signals[LAST_SIGNAL] = { 0 }; */

264
265
static void
_do_init (GType rtspsrc_type)
Wim Taymans's avatar
Wim Taymans committed
266
{
267
268
269
270
271
272
273
274
275
276
  static const GInterfaceInfo urihandler_info = {
    gst_rtspsrc_uri_handler_init,
    NULL,
    NULL
  };

  GST_DEBUG_CATEGORY_INIT (rtspsrc_debug, "rtspsrc", 0, "RTSP src");

  g_type_add_interface_static (rtspsrc_type, GST_TYPE_URI_HANDLER,
      &urihandler_info);
Wim Taymans's avatar
Wim Taymans committed
277
278
}

279
280
GST_BOILERPLATE_FULL (GstRTSPSrc, gst_rtspsrc, GstBin, GST_TYPE_BIN, _do_init);

Wim Taymans's avatar
Wim Taymans committed
281
282
283
284
285
286
287
288
289
290
291
292
static void
gst_rtspsrc_base_init (gpointer g_class)
{
  GstElementClass *element_class = GST_ELEMENT_CLASS (g_class);

  gst_element_class_add_pad_template (element_class,
      gst_static_pad_template_get (&rtptemplate));

  gst_element_class_set_details (element_class, &gst_rtspsrc_details);
}

static void
293
gst_rtspsrc_class_init (GstRTSPSrcClass * klass)
Wim Taymans's avatar
Wim Taymans committed
294
295
296
{
  GObjectClass *gobject_class;
  GstElementClass *gstelement_class;
297
  GstBinClass *gstbin_class;
Wim Taymans's avatar
Wim Taymans committed
298
299
300

  gobject_class = (GObjectClass *) klass;
  gstelement_class = (GstElementClass *) klass;
301
  gstbin_class = (GstBinClass *) klass;
Wim Taymans's avatar
Wim Taymans committed
302
303
304
305

  gobject_class->set_property = gst_rtspsrc_set_property;
  gobject_class->get_property = gst_rtspsrc_get_property;

306
307
  gobject_class->finalize = gst_rtspsrc_finalize;

Wim Taymans's avatar
Wim Taymans committed
308
  g_object_class_install_property (gobject_class, PROP_LOCATION,
Wim Taymans's avatar
Wim Taymans committed
309
310
      g_param_spec_string ("location", "RTSP Location",
          "Location of the RTSP url to read",
Wim Taymans's avatar
Wim Taymans committed
311
          DEFAULT_LOCATION, G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS));
Wim Taymans's avatar
Wim Taymans committed
312

Wim Taymans's avatar
Wim Taymans committed
313
  g_object_class_install_property (gobject_class, PROP_PROTOCOLS,
314
      g_param_spec_flags ("protocols", "Protocols",
315
          "Allowed lower transport protocols", GST_TYPE_RTSP_LOWER_TRANS,
Wim Taymans's avatar
Wim Taymans committed
316
          DEFAULT_PROTOCOLS, G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS));
Wim Taymans's avatar
Wim Taymans committed
317

Wim Taymans's avatar
Wim Taymans committed
318
  g_object_class_install_property (gobject_class, PROP_DEBUG,
Wim Taymans's avatar
Wim Taymans committed
319
      g_param_spec_boolean ("debug", "Debug",
320
          "Dump request and response messages to stdout",
Wim Taymans's avatar
Wim Taymans committed
321
          DEFAULT_DEBUG, G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS));
Wim Taymans's avatar
Wim Taymans committed
322

Wim Taymans's avatar
Wim Taymans committed
323
324
325
326
  g_object_class_install_property (gobject_class, PROP_RETRY,
      g_param_spec_uint ("retry", "Retry",
          "Max number of retries when allocating RTP ports.",
          0, G_MAXUINT16, DEFAULT_RETRY,
Wim Taymans's avatar
Wim Taymans committed
327
          G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS));
Wim Taymans's avatar
Wim Taymans committed
328

329
330
  g_object_class_install_property (gobject_class, PROP_TIMEOUT,
      g_param_spec_uint64 ("timeout", "Timeout",
331
          "Retry TCP transport after UDP timeout microseconds (0 = disabled)",
332
          0, G_MAXUINT64, DEFAULT_TIMEOUT,
Wim Taymans's avatar
Wim Taymans committed
333
          G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS));
334

335
336
337
338
  g_object_class_install_property (gobject_class, PROP_TCP_TIMEOUT,
      g_param_spec_uint64 ("tcp-timeout", "TCP Timeout",
          "Fail after timeout microseconds on TCP connections (0 = disabled)",
          0, G_MAXUINT64, DEFAULT_TCP_TIMEOUT,
Wim Taymans's avatar
Wim Taymans committed
339
          G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS));
340

341
342
343
  g_object_class_install_property (gobject_class, PROP_LATENCY,
      g_param_spec_uint ("latency", "Buffer latency in ms",
          "Amount of ms to buffer", 0, G_MAXUINT, DEFAULT_LATENCY_MS,
Wim Taymans's avatar
Wim Taymans committed
344
          G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS));
345

346
347
348
349
  g_object_class_install_property (gobject_class, PROP_CONNECTION_SPEED,
      g_param_spec_uint ("connection-speed", "Connection Speed",
          "Network connection speed in kbps (0 = unknown)",
          0, G_MAXINT / 1000, DEFAULT_CONNECTION_SPEED,
Wim Taymans's avatar
Wim Taymans committed
350
          G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS));
351

352
353
354
355
  g_object_class_install_property (gobject_class, PROP_NAT_METHOD,
      g_param_spec_enum ("nat-method", "NAT Method",
          "Method to use for traversing firewalls and NAT",
          GST_TYPE_RTSP_NAT_METHOD, DEFAULT_NAT_METHOD,
Wim Taymans's avatar
Wim Taymans committed
356
          G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS));
357

358
359
360
361
362
363
364
365
366
367
  /**
   * GstRTSPSrc::do-rtcp
   *
   * Enable RTCP support. Some old server don't like RTCP and then this property
   * needs to be set to FALSE.
   *
   * Since: 0.10.15
   */
  g_object_class_install_property (gobject_class, PROP_DO_RTCP,
      g_param_spec_boolean ("do-rtcp", "Do RTCP",
368
          "Send RTCP packets, disable for old incompatible server.",
Wim Taymans's avatar
Wim Taymans committed
369
          DEFAULT_DO_RTCP, G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS));
370

Wim Taymans's avatar
Wim Taymans committed
371
372
373
374
  /**
   * GstRTSPSrc::proxy
   *
   * Set the proxy parameters. This has to be a string of the format
375
   * [http://][user:passwd@]host[:port].
Wim Taymans's avatar
Wim Taymans committed
376
377
378
379
380
   *
   * Since: 0.10.15
   */
  g_object_class_install_property (gobject_class, PROP_PROXY,
      g_param_spec_string ("proxy", "Proxy",
381
          "Proxy settings for HTTP tunneling. Format: [http://][user:passwd@]host[:port]",
Wim Taymans's avatar
Wim Taymans committed
382
          DEFAULT_PROXY, G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS));
Wim Taymans's avatar
Wim Taymans committed
383

384
385
386
387
388
389
390
391
392
393
394
  /**
   * GstRTSPSrc::rtp_blocksize
   *
   * RTP package size to suggest to server.
   *
   * Since: 0.10.16
   */
  g_object_class_install_property (gobject_class, PROP_RTP_BLOCKSIZE,
      g_param_spec_uint ("rtp-blocksize", "RTP Blocksize",
          "RTP package size to suggest to server (0 = disabled)",
          0, 65536, DEFAULT_RTP_BLOCKSIZE,
Wim Taymans's avatar
Wim Taymans committed
395
          G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS));
396

397
398
399
  g_object_class_install_property (gobject_class,
      PROP_USER_ID,
      g_param_spec_string ("user-id", "user-id",
Wim Taymans's avatar
Wim Taymans committed
400
401
          "RTSP location URI user id for authentication", DEFAULT_USER_ID,
          G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS));
402
403
  g_object_class_install_property (gobject_class, PROP_USER_PW,
      g_param_spec_string ("user-pw", "user-pw",
Wim Taymans's avatar
Wim Taymans committed
404
405
          "RTSP location URI user password for authentication", DEFAULT_USER_PW,
          G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS));
406

Wim Taymans's avatar
Wim Taymans committed
407
  gstelement_class->change_state = gst_rtspsrc_change_state;
408
409

  gstbin_class->handle_message = gst_rtspsrc_handle_message;
410
411

  gst_rtsp_ext_list_init ();
Wim Taymans's avatar
Wim Taymans committed
412
413
414
}

static void
415
gst_rtspsrc_init (GstRTSPSrc * src, GstRTSPSrcClass * g_class)
Wim Taymans's avatar
Wim Taymans committed
416
{
417
418
419
420
421
422
423
424
#ifdef G_OS_WIN32
  WSADATA wsa_data;

  if (WSAStartup (MAKEWORD (2, 2), &wsa_data) != 0) {
    GST_ERROR_OBJECT (src, "WSAStartup failed: 0x%08x", WSAGetLastError ());
  }
#endif

425
  src->location = g_strdup (DEFAULT_LOCATION);
426
  src->url = NULL;
Wim Taymans's avatar
Wim Taymans committed
427
428
429
430
431
432
433
434
435
436
437
438
439
  src->protocols = DEFAULT_PROTOCOLS;
  src->debug = DEFAULT_DEBUG;
  src->retry = DEFAULT_RETRY;
  src->udp_timeout = DEFAULT_TIMEOUT;
  gst_rtspsrc_set_tcp_timeout (src, DEFAULT_TCP_TIMEOUT);
  src->latency = DEFAULT_LATENCY_MS;
  src->connection_speed = DEFAULT_CONNECTION_SPEED;
  src->nat_method = DEFAULT_NAT_METHOD;
  src->do_rtcp = DEFAULT_DO_RTCP;
  gst_rtspsrc_set_proxy (src, DEFAULT_PROXY);
  src->rtp_blocksize = DEFAULT_RTP_BLOCKSIZE;
  src->user_id = g_strdup (DEFAULT_USER_ID);
  src->user_pw = g_strdup (DEFAULT_USER_PW);
440

441
442
443
  /* get a list of all extensions */
  src->extensions = gst_rtsp_ext_list_get ();

444
445
446
  /* connect to send signal */
  gst_rtsp_ext_list_connect (src->extensions, "send",
      (GCallback) gst_rtspsrc_send_cb, src);
447

448
449
450
451
452
453
  /* protects the streaming thread in interleaved mode or the polling
   * thread in UDP mode. */
  src->stream_rec_lock = g_new (GStaticRecMutex, 1);
  g_static_rec_mutex_init (src->stream_rec_lock);

  /* protects our state changes from multiple invocations */
454
455
  src->state_rec_lock = g_new (GStaticRecMutex, 1);
  g_static_rec_mutex_init (src->state_rec_lock);
456
457
458
459
460

  /* protects access to the server connection */
  src->conn_rec_lock = g_new (GStaticRecMutex, 1);
  g_static_rec_mutex_init (src->conn_rec_lock);

461
  src->state = GST_RTSP_STATE_INVALID;
462
463
464
465
466
467
468
469
470
}

static void
gst_rtspsrc_finalize (GObject * object)
{
  GstRTSPSrc *rtspsrc;

  rtspsrc = GST_RTSPSRC (object);

471
  gst_rtsp_ext_list_free (rtspsrc->extensions);
472
  g_free (rtspsrc->location);
473
  g_free (rtspsrc->req_location);
474
  gst_rtsp_url_free (rtspsrc->url);
475
476
  g_free (rtspsrc->user_id);
  g_free (rtspsrc->user_pw);
477
478
479
480

  /* free locks */
  g_static_rec_mutex_free (rtspsrc->stream_rec_lock);
  g_free (rtspsrc->stream_rec_lock);
481
482
  g_static_rec_mutex_free (rtspsrc->state_rec_lock);
  g_free (rtspsrc->state_rec_lock);
483
484
  g_static_rec_mutex_free (rtspsrc->conn_rec_lock);
  g_free (rtspsrc->conn_rec_lock);
485

486
487
488
489
#ifdef G_OS_WIN32
  WSACleanup ();
#endif

490
  G_OBJECT_CLASS (parent_class)->finalize (object);
Wim Taymans's avatar
Wim Taymans committed
491
492
}

Wim Taymans's avatar
Wim Taymans committed
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
/* a proxy string of the format [user:passwd@]host[:port] */
static gboolean
gst_rtspsrc_set_proxy (GstRTSPSrc * rtsp, const gchar * proxy)
{
  gchar *p, *at, *col;

  g_free (rtsp->proxy_user);
  rtsp->proxy_user = NULL;
  g_free (rtsp->proxy_passwd);
  rtsp->proxy_passwd = NULL;
  g_free (rtsp->proxy_host);
  rtsp->proxy_host = NULL;
  rtsp->proxy_port = 0;

  p = (gchar *) proxy;

  if (p == NULL)
    return TRUE;

512
513
514
515
  /* we allow http:// in front but ignore it */
  if (g_str_has_prefix (p, "http://"))
    p += 7;

Wim Taymans's avatar
Wim Taymans committed
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
  at = strchr (p, '@');
  if (at) {
    /* look for user:passwd */
    col = strchr (proxy, ':');
    if (col == NULL || col > at)
      return FALSE;

    rtsp->proxy_user = g_strndup (p, col - p);
    col++;
    rtsp->proxy_passwd = g_strndup (col, at - col);

    /* move to host */
    p = at + 1;
  }
  col = strchr (p, ':');

  if (col) {
    /* everything before the colon is the hostname */
    rtsp->proxy_host = g_strndup (p, col - p);
    p = col + 1;
    rtsp->proxy_port = strtoul (p, (char **) &p, 10);
  } else {
    rtsp->proxy_host = g_strdup (p);
    rtsp->proxy_port = 8080;
  }
  return TRUE;
}

Wim Taymans's avatar
Wim Taymans committed
544
545
546
547
548
549
550
551
552
553
554
555
static void
gst_rtspsrc_set_tcp_timeout (GstRTSPSrc * rtspsrc, guint64 timeout)
{
  rtspsrc->tcp_timeout.tv_sec = timeout / G_USEC_PER_SEC;
  rtspsrc->tcp_timeout.tv_usec = timeout % G_USEC_PER_SEC;

  if (timeout != 0)
    rtspsrc->ptcp_timeout = &rtspsrc->tcp_timeout;
  else
    rtspsrc->ptcp_timeout = NULL;
}

Wim Taymans's avatar
Wim Taymans committed
556
557
558
559
560
561
562
563
564
565
static void
gst_rtspsrc_set_property (GObject * object, guint prop_id, const GValue * value,
    GParamSpec * pspec)
{
  GstRTSPSrc *rtspsrc;

  rtspsrc = GST_RTSPSRC (object);

  switch (prop_id) {
    case PROP_LOCATION:
566
567
      gst_rtspsrc_uri_set_uri (GST_URI_HANDLER (rtspsrc),
          g_value_get_string (value));
Wim Taymans's avatar
Wim Taymans committed
568
569
570
571
572
573
574
      break;
    case PROP_PROTOCOLS:
      rtspsrc->protocols = g_value_get_flags (value);
      break;
    case PROP_DEBUG:
      rtspsrc->debug = g_value_get_boolean (value);
      break;
Wim Taymans's avatar
Wim Taymans committed
575
576
577
    case PROP_RETRY:
      rtspsrc->retry = g_value_get_uint (value);
      break;
578
    case PROP_TIMEOUT:
579
      rtspsrc->udp_timeout = g_value_get_uint64 (value);
580
      break;
581
    case PROP_TCP_TIMEOUT:
Wim Taymans's avatar
Wim Taymans committed
582
      gst_rtspsrc_set_tcp_timeout (rtspsrc, g_value_get_uint64 (value));
583
      break;
584
585
586
    case PROP_LATENCY:
      rtspsrc->latency = g_value_get_uint (value);
      break;
587
588
589
    case PROP_CONNECTION_SPEED:
      rtspsrc->connection_speed = g_value_get_uint (value);
      break;
590
591
592
    case PROP_NAT_METHOD:
      rtspsrc->nat_method = g_value_get_enum (value);
      break;
593
594
595
    case PROP_DO_RTCP:
      rtspsrc->do_rtcp = g_value_get_boolean (value);
      break;
Wim Taymans's avatar
Wim Taymans committed
596
597
598
    case PROP_PROXY:
      gst_rtspsrc_set_proxy (rtspsrc, g_value_get_string (value));
      break;
599
600
601
    case PROP_RTP_BLOCKSIZE:
      rtspsrc->rtp_blocksize = g_value_get_uint (value);
      break;
602
603
604
605
606
607
608
609
610
611
    case PROP_USER_ID:
      if (rtspsrc->user_id)
        g_free (rtspsrc->user_id);
      rtspsrc->user_id = g_value_dup_string (value);
      break;
    case PROP_USER_PW:
      if (rtspsrc->user_pw)
        g_free (rtspsrc->user_pw);
      rtspsrc->user_pw = g_value_dup_string (value);
      break;
Wim Taymans's avatar
Wim Taymans committed
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
    default:
      G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
      break;
  }
}

static void
gst_rtspsrc_get_property (GObject * object, guint prop_id, GValue * value,
    GParamSpec * pspec)
{
  GstRTSPSrc *rtspsrc;

  rtspsrc = GST_RTSPSRC (object);

  switch (prop_id) {
    case PROP_LOCATION:
      g_value_set_string (value, rtspsrc->location);
      break;
    case PROP_PROTOCOLS:
      g_value_set_flags (value, rtspsrc->protocols);
      break;
    case PROP_DEBUG:
      g_value_set_boolean (value, rtspsrc->debug);
      break;
Wim Taymans's avatar
Wim Taymans committed
636
637
638
    case PROP_RETRY:
      g_value_set_uint (value, rtspsrc->retry);
      break;
639
    case PROP_TIMEOUT:
640
641
642
643
644
645
646
647
648
      g_value_set_uint64 (value, rtspsrc->udp_timeout);
      break;
    case PROP_TCP_TIMEOUT:
    {
      guint64 timeout;

      timeout = rtspsrc->tcp_timeout.tv_sec * G_USEC_PER_SEC +
          rtspsrc->tcp_timeout.tv_usec;
      g_value_set_uint64 (value, timeout);
649
      break;
650
    }
651
652
653
    case PROP_LATENCY:
      g_value_set_uint (value, rtspsrc->latency);
      break;
654
655
656
    case PROP_CONNECTION_SPEED:
      g_value_set_uint (value, rtspsrc->connection_speed);
      break;
657
658
659
    case PROP_NAT_METHOD:
      g_value_set_enum (value, rtspsrc->nat_method);
      break;
660
661
662
    case PROP_DO_RTCP:
      g_value_set_boolean (value, rtspsrc->do_rtcp);
      break;
Wim Taymans's avatar
Wim Taymans committed
663
664
665
666
667
668
669
670
671
672
673
674
675
    case PROP_PROXY:
    {
      gchar *str;

      if (rtspsrc->proxy_host) {
        str =
            g_strdup_printf ("%s:%d", rtspsrc->proxy_host, rtspsrc->proxy_port);
      } else {
        str = NULL;
      }
      g_value_take_string (value, str);
      break;
    }
676
677
678
    case PROP_RTP_BLOCKSIZE:
      g_value_set_uint (value, rtspsrc->rtp_blocksize);
      break;
679
680
681
682
683
684
    case PROP_USER_ID:
      g_value_set_string (value, rtspsrc->user_id);
      break;
    case PROP_USER_PW:
      g_value_set_string (value, rtspsrc->user_pw);
      break;
Wim Taymans's avatar
Wim Taymans committed
685
686
687
688
689
690
    default:
      G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
      break;
  }
}

691
static gint
692
find_stream_by_id (GstRTSPStream * stream, gint * id)
693
{
694
  if (stream->id == *id)
695
696
697
698
699
    return 0;

  return -1;
}

700
static gint
701
find_stream_by_channel (GstRTSPStream * stream, gint * channel)
702
{
703
  if (stream->channel[0] == *channel || stream->channel[1] == *channel)
704
705
706
707
708
    return 0;

  return -1;
}

709
static gint
710
find_stream_by_pt (GstRTSPStream * stream, gint * pt)
711
{
712
  if (stream->pt == *pt)
713
714
715
716
717
    return 0;

  return -1;
}

718
719
720
721
722
723
724
static gint
find_stream_by_udpsrc (GstRTSPStream * stream, gconstpointer a)
{
  GstElement *src = (GstElement *) a;

  if (stream->udpsrc[0] == src)
    return 0;
725
726
  if (stream->udpsrc[1] == src)
    return 0;
727
728
729
730

  return -1;
}

731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
static gint
find_stream_by_setup (GstRTSPStream * stream, gconstpointer a)
{
  /* check qualified setup_url */
  if (!strcmp (stream->setup_url, (gchar *) a))
    return 0;
  /* check original control_url */
  if (!strcmp (stream->control_url, (gchar *) a))
    return 0;

  /* check if qualified setup_url ends with string */
  if (g_str_has_suffix (stream->control_url, (gchar *) a))
    return 0;

  return -1;
}

748
749
750
751
752
753
754
755
756
757
758
759
GstRTSPStream *
find_stream (GstRTSPSrc * src, gconstpointer data, gconstpointer func)
{
  GList *lstream;

  /* find and get stream */
  if ((lstream = g_list_find_custom (src->streams, data, (GCompareFunc) func)))
    return (GstRTSPStream *) lstream->data;

  return NULL;
}

760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
static const GstSDPBandwidth *
gst_rtspsrc_get_bandwidth (GstRTSPSrc * src, const GstSDPMessage * sdp,
    const GstSDPMedia * media, const gchar * type)
{
  guint i, len;

  /* first look in the media specific section */
  len = gst_sdp_media_bandwidths_len (media);
  for (i = 0; i < len; i++) {
    const GstSDPBandwidth *bw = gst_sdp_media_get_bandwidth (media, i);

    if (strcmp (bw->bwtype, type) == 0)
      return bw;
  }
  /* then look in the message specific section */
  len = gst_sdp_message_bandwidths_len (sdp);
  for (i = 0; i < len; i++) {
    const GstSDPBandwidth *bw = gst_sdp_message_get_bandwidth (sdp, i);

    if (strcmp (bw->bwtype, type) == 0)
      return bw;
  }
  return NULL;
}

static void
gst_rtspsrc_collect_bandwidth (GstRTSPSrc * src, const GstSDPMessage * sdp,
    const GstSDPMedia * media, GstRTSPStream * stream)
{
  const GstSDPBandwidth *bw;

  if ((bw = gst_rtspsrc_get_bandwidth (src, sdp, media, GST_SDP_BWTYPE_AS)))
    stream->as_bandwidth = bw->bandwidth;
  else
    stream->as_bandwidth = -1;

  if ((bw = gst_rtspsrc_get_bandwidth (src, sdp, media, GST_SDP_BWTYPE_RR)))
    stream->rr_bandwidth = bw->bandwidth;
  else
    stream->rr_bandwidth = -1;

  if ((bw = gst_rtspsrc_get_bandwidth (src, sdp, media, GST_SDP_BWTYPE_RS)))
    stream->rs_bandwidth = bw->bandwidth;
  else
    stream->rs_bandwidth = -1;
}

Wim Taymans's avatar
Wim Taymans committed
807
static GstRTSPStream *
808
gst_rtspsrc_create_stream (GstRTSPSrc * src, GstSDPMessage * sdp, gint idx)
Wim Taymans's avatar
Wim Taymans committed
809
{
810
  GstRTSPStream *stream;
811
812
813
  const gchar *control_url;
  const gchar *payload;
  const GstSDPMedia *media;
814
815

  /* get media, should not return NULL */
816
  media = gst_sdp_message_get_media (sdp, idx);
817
818
  if (media == NULL)
    return NULL;
819
820
821
822
823
824

  stream = g_new0 (GstRTSPStream, 1);
  stream->parent = src;
  /* we mark the pad as not linked, we will mark it as OK when we add the pad to
   * the element. */
  stream->last_ret = GST_FLOW_NOT_LINKED;
825
  stream->added = FALSE;
826
  stream->disabled = FALSE;
827
  stream->id = src->numstreams++;
828
829
  stream->eos = FALSE;
  stream->discont = TRUE;
830
831
  stream->seqbase = -1;
  stream->timebase = -1;
832

833
834
835
  /* collect bandwidth information for this steam */
  gst_rtspsrc_collect_bandwidth (src, sdp, media, stream);

836
  /* we must have a payload. No payload means we cannot create caps */
837
838
839
840
841
  /* FIXME, handle multiple formats. The problem here is that we just want to
   * take the first available format that we can handle but in order to do that
   * we need to scan for depayloader plugins. Scanning for payloader plugins is
   * also suboptimal because the user maybe just wants to save the raw stream
   * and then we don't care. */
842
  if ((payload = gst_sdp_media_get_format (media, 0))) {
843
844
845
846
    stream->pt = atoi (payload);
    /* convert caps */
    stream->caps = gst_rtspsrc_media_to_caps (stream->pt, media);

847
848
849
850
851
    GST_DEBUG ("mapping sdp session level attributes to caps");
    gst_rtspsrc_sdp_attributes_to_caps (sdp->attributes, stream->caps);
    GST_DEBUG ("mapping sdp media level attributes to caps");
    gst_rtspsrc_sdp_attributes_to_caps (media->attributes, stream->caps);

852
853
854
855
    if (stream->pt >= 96) {
      /* If we have a dynamic payload type, see if we have a stream with the
       * same payload number. If there is one, they are part of the same
       * container and we only need to add one pad. */
856
      if (find_stream (src, &stream->pt, (gpointer) find_stream_by_pt)) {
857
858
859
860
        stream->container = TRUE;
      }
    }
  }
Wim Taymans's avatar
Wim Taymans committed
861

862
863
864
  /* get control url to construct the setup url. The setup url is used to
   * configure the transport of the stream and is used to identity the stream in
   * the RTP-Info header field returned from PLAY. */
865
  control_url = gst_sdp_media_get_attribute_val (media, "control");
Wim Taymans's avatar
Wim Taymans committed
866

867
  GST_DEBUG_OBJECT (src, "stream %d, (%p)", stream->id, stream);
868
869
870
871
  GST_DEBUG_OBJECT (src, " pt: %d", stream->pt);
  GST_DEBUG_OBJECT (src, " container: %d", stream->container);
  GST_DEBUG_OBJECT (src, " caps: %" GST_PTR_FORMAT, stream->caps);
  GST_DEBUG_OBJECT (src, " control: %s", GST_STR_NULL (control_url));
Wim Taymans's avatar
Wim Taymans committed
872

873
  if (control_url != NULL) {
874
875
876
877
    stream->control_url = g_strdup (control_url);
    /* Build a fully qualified url using the content_base if any or by prefixing
     * the original request.
     * If the control_url starts with a '/' or a non rtsp: protocol we will most
878
879
     * likely build a URL that the server will fail to understand, this is ok,
     * we will fail then. */
880
881
    if (g_str_has_prefix (control_url, "rtsp://"))
      stream->setup_url = g_strdup (control_url);
882
883
884
    else if (src->content_base)
      stream->setup_url =
          g_strdup_printf ("%s%s", src->content_base, control_url);
885
    else
886
887
      stream->setup_url =
          g_strdup_printf ("%s/%s", src->req_location, control_url);
888
889
890
891
892
893
894
  }
  GST_DEBUG_OBJECT (src, " setup: %s", GST_STR_NULL (stream->setup_url));

  /* we keep track of all streams */
  src->streams = g_list_append (src->streams, stream);

  return stream;
895
896

  /* ERRORS */
Wim Taymans's avatar
Wim Taymans committed
897
898
}

899
static void
900
gst_rtspsrc_stream_free (GstRTSPSrc * src, GstRTSPStream * stream)
901
{
902
903
904
905
906
  gint i;

  GST_DEBUG_OBJECT (src, "free stream %p", stream);

  if (stream->caps)
907
    gst_caps_unref (stream->caps);
908

909
  g_free (stream->control_url);
910
  g_free (stream->setup_url);
911

912
  for (i = 0; i < 2; i++) {
913
914
915
    if (stream->udpsrc[i]) {
      gst_element_set_state (stream->udpsrc[i], GST_STATE_NULL);
      gst_bin_remove (GST_BIN_CAST (src), stream->udpsrc[i]);
916
917
918
      gst_object_unref (stream->udpsrc[i]);
      stream->udpsrc[i] = NULL;
    }
919
920
921
922
    if (stream->channelpad[i]) {
      gst_object_unref (stream->channelpad[i]);
      stream->channelpad[i] = NULL;
    }
923
924
925
926
927
928
    if (stream->udpsink[i]) {
      gst_element_set_state (stream->udpsink[i], GST_STATE_NULL);
      gst_bin_remove (GST_BIN_CAST (src), stream->udpsink[i]);
      gst_object_unref (stream->udpsink[i]);
      stream->udpsink[i] = NULL;
    }
929
  }
930
931
932
933
934
  if (stream->fakesrc) {
    gst_element_set_state (stream->fakesrc, GST_STATE_NULL);
    gst_bin_remove (GST_BIN_CAST (src), stream->fakesrc);
    gst_object_unref (stream->fakesrc);
    stream->fakesrc = NULL;
935
  }
936
937
938
939
940
941
942
943
  if (stream->srcpad) {
    gst_pad_set_active (stream->srcpad, FALSE);
    if (stream->added) {
      gst_element_remove_pad (GST_ELEMENT_CAST (src), stream->srcpad);
      stream->added = FALSE;
    }
    stream->srcpad = NULL;
  }
944
945
946
947
  if (stream->rtcppad) {
    gst_object_unref (stream->rtcppad);
    stream->rtcppad = NULL;
  }
948
949
  g_free (stream);
}
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964

static void
gst_rtspsrc_cleanup (GstRTSPSrc * src)
{
  GList *walk;

  GST_DEBUG_OBJECT (src, "cleanup");

  for (walk = src->streams; walk; walk = g_list_next (walk)) {
    GstRTSPStream *stream = (GstRTSPStream *) walk->data;

    gst_rtspsrc_stream_free (src, stream);
  }
  g_list_free (src->streams);
  src->streams = NULL;
965
966
967
968
969
970
971
972
973
  if (src->session) {
    if (src->session_sig_id) {
      g_signal_handler_disconnect (src->session, src->session_sig_id);
      src->session_sig_id = 0;
    }
    gst_element_set_state (src->session, GST_STATE_NULL);
    gst_bin_remove (GST_BIN_CAST (src), src->session);
    src->session = NULL;
  }
974
975
976
977
  src->numstreams = 0;
  if (src->props)
    gst_structure_free (src->props);
  src->props = NULL;
Wim Taymans's avatar
Wim Taymans committed
978
979
980
981
982
983
984

  g_free (src->content_base);
  src->content_base = NULL;

  if (src->range)
    gst_rtsp_range_free (src->range);
  src->range = NULL;
985
986
}

Thomas Vander Stichele's avatar
Thomas Vander Stichele committed
987
988
989
990
991
992
993
994
995
996
997
#define PARSE_INT(p, del, res)          \
G_STMT_START {                          \
  gchar *t = p;                         \
  p = strstr (p, del);                  \
  if (p == NULL)                        \
    res = -1;                           \
  else {                                \
    *p = '\0';                          \
    p++;                                \
    res = atoi (t);                     \
  }                                     \
Wim Taymans's avatar
Wim Taymans committed
998
999
} G_STMT_END

Thomas Vander Stichele's avatar
Thomas Vander Stichele committed
1000
1001
1002
1003
#define PARSE_STRING(p, del, res)       \
G_STMT_START {                          \
  gchar *t = p;                         \
  p = strstr (p, del);                  \
1004
  if (p == NULL) {                      \
Thomas Vander Stichele's avatar
Thomas Vander Stichele committed
1005
    res = NULL;                         \
1006
1007
    p = t;                              \
  }                                     \
Thomas Vander Stichele's avatar
Thomas Vander Stichele committed
1008
1009
1010
1011
1012
  else {                                \
    *p = '\0';                          \
    p++;                                \
    res = t;                            \
  }                                     \
Wim Taymans's avatar
Wim Taymans committed
1013
1014
} G_STMT_END

Thomas Vander Stichele's avatar
Thomas Vander Stichele committed
1015
1016
#define SKIP_SPACES(p)                  \
  while (*p && g_ascii_isspace (*p))    \
Wim Taymans's avatar
Wim Taymans committed
1017
1018
    p++;

1019
1020
1021
1022
/* rtpmap contains:
 *
 *  <payload> <encoding_name>/<clock_rate>[/<encoding_params>]
 */
Wim Taymans's avatar
Wim Taymans committed
1023
static gboolean
1024
gst_rtspsrc_parse_rtpmap (const gchar * rtpmap, gint * payload, gchar ** name,
Wim Taymans's avatar
Wim Taymans committed
1025
1026
1027
1028
    gint * rate, gchar ** params)
{
  gchar *p, *t;

1029
  p = (gchar *) rtpmap;
Wim Taymans's avatar
Wim Taymans committed
1030

Wim Taymans's avatar
Wim Taymans committed
1031
1032
  PARSE_INT (p, " ", *payload);
  if (*payload == -1)
Wim Taymans's avatar
Wim Taymans committed
1033
1034
    return FALSE;

Wim Taymans's avatar
Wim Taymans committed
1035
  SKIP_SPACES (p);
Wim Taymans's avatar
Wim Taymans committed
1036
1037
1038
  if (*p == '\0')
    return FALSE;

Wim Taymans's avatar
Wim Taymans committed
1039
  PARSE_STRING (p, "/", *name);
1040
  if (*name == NULL) {
1041
    GST_DEBUG ("no rate, name %s", p);
1042
1043
    /* no rate, assume -1 then, this is not supposed to happen but RealMedia
     * streams seem to omit the rate. */
1044
1045
1046
1047
    *name = p;
    *rate = -1;
    return TRUE;
  }
Wim Taymans's avatar
Wim Taymans committed
1048
1049
1050
1051
1052
1053
1054
1055
1056
1057
1058
1059
1060
1061
1062
1063
1064
1065
1066

  t = p;
  p = strstr (p, "/");
  if (p == NULL) {
    *rate = atoi (t);
    return TRUE;
  }
  *p = '\0';
  p++;
  *rate = atoi (t);

  t = p;
  if (*p == '\0')
    return TRUE;
  *params = t;

  return TRUE;
}

1067
1068
1069
1070
1071
1072
1073
1074
1075
1076
1077
1078
1079
1080
1081
1082
1083
1084
1085
1086
1087
1088
1089
1090
1091
1092
1093
1094
1095
1096
1097
1098
/*
 * Mapping SDP attributes to caps
 *
 * prepend 'a-' to IANA registered sdp attributes names
 * (ie: not prefixed with 'x-') in order to avoid
 * collision with gstreamer standard caps properties names
 */
static void
gst_rtspsrc_sdp_attributes_to_caps (GArray * attributes, GstCaps * caps)
{
  if (attributes->len > 0) {
    GstStructure *s;
    guint i;

    s = gst_caps_get_structure (caps, 0);

    for (i = 0; i < attributes->len; i++) {
      GstSDPAttribute *attr = &g_array_index (attributes, GstSDPAttribute, i);
      gchar *tofree, *key;

      key = attr->key;

      /* skip some of the attribute we already handle */
      if (!strcmp (key, "fmtp"))
        continue;
      if (!strcmp (key, "rtpmap"))
        continue;
      if (!strcmp (key, "control"))
        continue;
      if (!strcmp (key, "range"))
        continue;

1099
1100
1101
1102
      /* string must be valid UTF8 */
      if (!g_utf8_validate (attr->value, -1, NULL))
        continue;

1103
1104
1105
1106
1107
1108
1109
1110
1111
1112
1113
1114
      if (!g_str_has_prefix (key, "x-"))
        tofree = key = g_strdup_printf ("a-%s", key);
      else
        tofree = NULL;

      GST_DEBUG ("adding caps: %s=%s", key, attr->value);
      gst_structure_set (s, key, G_TYPE_STRING, attr->value, NULL);
      g_free (tofree);
    }
  }
}

Wim Taymans's avatar
Wim Taymans committed
1115
1116
1117
/*
 *  Mapping of caps to and from SDP fields:
 *
1118
 *   m=<media> <UDP port> RTP/AVP <payload> 
Wim Taymans's avatar
Wim Taymans committed
1119
 *   a=rtpmap:<payload> <encoding_name>/<clock_rate>[/<encoding_params>]
1120
 *   a=fmtp:<payload> <param>[=<value>];...
Wim Taymans's avatar
Wim Taymans committed
1121
1122
 */
static GstCaps *
1123
gst_rtspsrc_media_to_caps (gint pt, const GstSDPMedia * media)
Wim Taymans's avatar
Wim Taymans committed
1124
1125
{
  GstCaps *caps;
1126
1127
  const gchar *rtpmap;
  const gchar *fmtp;
Wim Taymans's avatar
Wim Taymans committed
1128
1129
1130
  gchar *name = NULL;
  gint rate = -1;
  gchar *params = NULL;
1131
  gchar *tmp;
Wim Taymans's avatar
Wim Taymans committed
1132
  GstStructure *s;
1133
1134
1135
1136
  gint payload = 0;
  gboolean ret;

  /* get and parse rtpmap */
1137
  if ((rtpmap = gst_sdp_media_get_attribute_val (media, "rtpmap"))) {
1138
1139
1140
1141
1142
1143
1144
1145
    ret = gst_rtspsrc_parse_rtpmap (rtpmap, &payload, &name, &rate, &params);
    if (ret) {
      if (payload != pt) {
        /* we ignore the rtpmap if the payload type is different. */
        g_warning ("rtpmap of wrong payload type, ignoring");
        name = NULL;
        rate = -1;
        params = NULL;
Wim Taymans's avatar
Wim Taymans committed
1146
      }
1147
1148
1149
1150
1151
1152
1153
1154
1155
1156
1157
    } else {
      /* if we failed to parse the rtpmap for a dynamic payload type, we have an
       * error */
      if (pt >= 96)
        goto no_rtpmap;
      /* else we can ignore */
      g_warning ("error parsing rtpmap, ignoring");
    }
  } else {
    /* dynamic payloads need rtpmap or we fail */
    if (pt >= 96)
1158
      goto no_rtpmap;
Wim Taymans's avatar
Wim Taymans committed
1159
  }
1160
1161
1162
  /* check if we have a rate, if not, we need to look up the rate from the
   * default rates based on the payload types. */
  if (rate == -1) {
1163
1164
1165
1166
1167
1168
1169
1170
1171
1172
1173
1174
1175
1176
1177
1178
    const GstRTPPayloadInfo *info;

    if (GST_RTP_PAYLOAD_IS_DYNAMIC (pt)) {
      /* dynamic types, use media and encoding_name */
      tmp = g_ascii_strdown (media->media, -1);
      info = gst_rtp_payload_info_for_name (tmp, name);
      g_free (tmp);
    } else {
      /* static types, use payload type */
      info = gst_rtp_payload_info_for_pt (pt);
    }

    if (info) {
      if ((rate = info->clock_rate) == 0)
        rate = -1;
    }
1179
1180
1181
1182
    /* we fail if we cannot find one */
    if (rate == -1)
      goto no_rate;
  }
Wim Taymans's avatar
Wim Taymans committed
1183

1184
  tmp = g_ascii_strdown (media->media, -1);
1185
  caps = gst_caps_new_simple ("application/x-unknown",
1186
1187
      "media", G_TYPE_STRING, tmp, "payload", G_TYPE_INT, pt, NULL);
  g_free (tmp);
Wim Taymans's avatar
Wim Taymans committed
1188
1189
  s = gst_caps_get_structure (caps, 0);

1190
  gst_structure_set (s, "clock-rate", G_TYPE_INT, rate, NULL);
Wim Taymans's avatar
Wim Taymans committed
1191

1192
  /* encoding name must be upper case */
1193
  if (name != NULL) {
1194
1195
1196
    tmp = g_ascii_strup (name, -1);
    gst_structure_set (s, "encoding-name", G_TYPE_STRING, tmp, NULL);
    g_free (tmp);
1197
  }