gstrtspsrc.c 174 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)

108
static GstStaticPadTemplate rtptemplate = GST_STATIC_PAD_TEMPLATE ("stream%d",
Wim Taymans's avatar
Wim Taymans committed
109
110
    GST_PAD_SRC,
    GST_PAD_SOMETIMES,
111
    GST_STATIC_CAPS ("application/x-rtp; application/x-rdt"));
Wim Taymans's avatar
Wim Taymans committed
112

113
114
115
/* templates used internally */
static GstStaticPadTemplate anysrctemplate =
GST_STATIC_PAD_TEMPLATE ("internalsrc%d",
116
117
118
119
    GST_PAD_SRC,
    GST_PAD_SOMETIMES,
    GST_STATIC_CAPS_ANY);

120
121
122
123
124
125
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
126
127
128
129
130
131
enum
{
  /* FILL ME */
  LAST_SIGNAL
};

132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
#define GST_TYPE_RTSP_SRC_BUFFER_MODE (gst_rtsp_src_buffer_mode_get_type())
static GType
gst_rtsp_src_buffer_mode_get_type (void)
{
  static GType buffer_mode_type = 0;
  static const GEnumValue buffer_modes[] = {
    {0, "Only use RTP timestamps", "none"},
    {1, "Slave receiver to sender clock", "slave"},
    {2, "Do low/high watermark buffering", "buffer"},
    {0, NULL, NULL},
  };

  if (!buffer_mode_type) {
    buffer_mode_type =
        g_enum_register_static ("GstRTSPSrcBufferMode", buffer_modes);
  }
  return buffer_mode_type;
}

151
152
153
154
155
156
#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
157
#define DEFAULT_LATENCY_MS       2000
158
#define DEFAULT_CONNECTION_SPEED 0
159
#define DEFAULT_NAT_METHOD       GST_RTSP_NAT_DUMMY
160
#define DEFAULT_DO_RTCP          TRUE
Wim Taymans's avatar
Wim Taymans committed
161
#define DEFAULT_PROXY            NULL
162
#define DEFAULT_RTP_BLOCKSIZE    0
Wim Taymans's avatar
Wim Taymans committed
163
164
#define DEFAULT_USER_ID          NULL
#define DEFAULT_USER_PW          NULL
165
#define DEFAULT_BUFFER_MODE      1
Wim Taymans's avatar
Wim Taymans committed
166
167
168
169
170
171
172

enum
{
  PROP_0,
  PROP_LOCATION,
  PROP_PROTOCOLS,
  PROP_DEBUG,
Wim Taymans's avatar
Wim Taymans committed
173
  PROP_RETRY,
174
  PROP_TIMEOUT,
175
  PROP_TCP_TIMEOUT,
176
  PROP_LATENCY,
177
178
  PROP_CONNECTION_SPEED,
  PROP_NAT_METHOD,
179
  PROP_DO_RTCP,
Wim Taymans's avatar
Wim Taymans committed
180
  PROP_PROXY,
181
  PROP_RTP_BLOCKSIZE,
182
183
  PROP_USER_ID,
  PROP_USER_PW,
184
  PROP_BUFFER_MODE,
185
  PROP_LAST
Wim Taymans's avatar
Wim Taymans committed
186
187
};

188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
#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;
}

206
static void gst_rtspsrc_finalize (GObject * object);
Wim Taymans's avatar
Wim Taymans committed
207

208
209
210
211
212
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
213
214
static void gst_rtspsrc_uri_handler_init (gpointer g_iface,
    gpointer iface_data);
215
216
217
218

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

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

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

224
225
static GstStateChangeReturn gst_rtspsrc_change_state (GstElement * element,
    GstStateChange transition);
226
static void gst_rtspsrc_handle_message (GstBin * bin, GstMessage * message);
Wim Taymans's avatar
Wim Taymans committed
227

228
229
static void gst_rtspsrc_loop_send_cmd (GstRTSPSrc * src, gint cmd,
    gboolean flush);
230
231
232
static GstRTSPResult gst_rtspsrc_send_cb (GstRTSPExtension * ext,
    GstRTSPMessage * request, GstRTSPMessage * response, GstRTSPSrc * src);

233
static gboolean gst_rtspsrc_open (GstRTSPSrc * src);
234
static gboolean gst_rtspsrc_play (GstRTSPSrc * src, GstSegment * segment);
235
static gboolean gst_rtspsrc_pause (GstRTSPSrc * src, gboolean idle);
236
237
static gboolean gst_rtspsrc_close (GstRTSPSrc * src);

238
239
240
static gboolean gst_rtspsrc_uri_set_uri (GstURIHandler * handler,
    const gchar * uri);

241
static gboolean gst_rtspsrc_activate_streams (GstRTSPSrc * src);
Wim Taymans's avatar
Wim Taymans committed
242
static void gst_rtspsrc_loop (GstRTSPSrc * src);
243
244
static void gst_rtspsrc_stream_push_event (GstRTSPSrc * src,
    GstRTSPStream * stream, GstEvent * event);
245
static void gst_rtspsrc_push_event (GstRTSPSrc * src, GstEvent * event);
246
static gchar *gst_rtspsrc_dup_printf (const gchar * format, ...);
Wim Taymans's avatar
Wim Taymans committed
247

248
249
250
251
252
/* 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
253
254
/*static guint gst_rtspsrc_signals[LAST_SIGNAL] = { 0 }; */

255
256
static void
_do_init (GType rtspsrc_type)
Wim Taymans's avatar
Wim Taymans committed
257
{
258
259
260
261
262
263
264
265
266
267
  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
268
269
}

270
271
GST_BOILERPLATE_FULL (GstRTSPSrc, gst_rtspsrc, GstBin, GST_TYPE_BIN, _do_init);

Wim Taymans's avatar
Wim Taymans committed
272
273
274
275
276
277
278
279
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));

280
281
282
283
284
285
  gst_element_class_set_details_simple (element_class, "RTSP packet receiver",
      "Source/Network",
      "Receive data over the network via RTSP (RFC 2326)",
      "Wim Taymans <wim@fluendo.com>, "
      "Thijs Vermeir <thijs.vermeir@barco.com>, "
      "Lutz Mueller <lutz@topfrose.de>");
Wim Taymans's avatar
Wim Taymans committed
286
287
288
}

static void
289
gst_rtspsrc_class_init (GstRTSPSrcClass * klass)
Wim Taymans's avatar
Wim Taymans committed
290
291
292
{
  GObjectClass *gobject_class;
  GstElementClass *gstelement_class;
293
  GstBinClass *gstbin_class;
Wim Taymans's avatar
Wim Taymans committed
294
295
296

  gobject_class = (GObjectClass *) klass;
  gstelement_class = (GstElementClass *) klass;
297
  gstbin_class = (GstBinClass *) klass;
Wim Taymans's avatar
Wim Taymans committed
298
299
300
301

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

302
303
  gobject_class->finalize = gst_rtspsrc_finalize;

Wim Taymans's avatar
Wim Taymans committed
304
  g_object_class_install_property (gobject_class, PROP_LOCATION,
Wim Taymans's avatar
Wim Taymans committed
305
306
      g_param_spec_string ("location", "RTSP Location",
          "Location of the RTSP url to read",
Wim Taymans's avatar
Wim Taymans committed
307
          DEFAULT_LOCATION, G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS));
Wim Taymans's avatar
Wim Taymans committed
308

Wim Taymans's avatar
Wim Taymans committed
309
  g_object_class_install_property (gobject_class, PROP_PROTOCOLS,
310
      g_param_spec_flags ("protocols", "Protocols",
311
          "Allowed lower transport protocols", GST_TYPE_RTSP_LOWER_TRANS,
Wim Taymans's avatar
Wim Taymans committed
312
          DEFAULT_PROTOCOLS, G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS));
Wim Taymans's avatar
Wim Taymans committed
313

Wim Taymans's avatar
Wim Taymans committed
314
  g_object_class_install_property (gobject_class, PROP_DEBUG,
Wim Taymans's avatar
Wim Taymans committed
315
      g_param_spec_boolean ("debug", "Debug",
316
          "Dump request and response messages to stdout",
Wim Taymans's avatar
Wim Taymans committed
317
          DEFAULT_DEBUG, G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS));
Wim Taymans's avatar
Wim Taymans committed
318

Wim Taymans's avatar
Wim Taymans committed
319
320
321
322
  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
323
          G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS));
Wim Taymans's avatar
Wim Taymans committed
324

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

331
332
333
334
  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
335
          G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS));
336

337
338
339
  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
340
          G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS));
341

342
343
344
345
  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
346
          G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS));
347

348
349
350
351
  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
352
          G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS));
353

354
355
356
357
358
359
360
361
362
363
  /**
   * 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",
364
          "Send RTCP packets, disable for old incompatible server.",
Wim Taymans's avatar
Wim Taymans committed
365
          DEFAULT_DO_RTCP, G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS));
366

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

380
381
382
383
384
385
386
387
388
389
390
  /**
   * 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
391
          G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS));
392

393
394
395
  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
396
397
          "RTSP location URI user id for authentication", DEFAULT_USER_ID,
          G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS));
398
399
  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
400
401
          "RTSP location URI user password for authentication", DEFAULT_USER_PW,
          G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS));
402

403
404
405
406
407
408
409
410
411
412
413
414
415
  /**
   * GstRTSPSrc::buffer-mode:
   *
   * Control the buffering and timestamping mode used by the jitterbuffer.
   *
   * Since: 0.10.22
   */
  g_object_class_install_property (gobject_class, PROP_BUFFER_MODE,
      g_param_spec_enum ("buffer-mode", "Buffer Mode",
          "Control the buffering algorithm in use",
          GST_TYPE_RTSP_SRC_BUFFER_MODE, DEFAULT_BUFFER_MODE,
          G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS));

Wim Taymans's avatar
Wim Taymans committed
416
  gstelement_class->change_state = gst_rtspsrc_change_state;
417
418

  gstbin_class->handle_message = gst_rtspsrc_handle_message;
419
420

  gst_rtsp_ext_list_init ();
Wim Taymans's avatar
Wim Taymans committed
421
422
423
}

static void
424
gst_rtspsrc_init (GstRTSPSrc * src, GstRTSPSrcClass * g_class)
Wim Taymans's avatar
Wim Taymans committed
425
{
426
427
428
429
430
431
432
433
#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

434
  src->conninfo.location = g_strdup (DEFAULT_LOCATION);
Wim Taymans's avatar
Wim Taymans committed
435
436
437
438
439
440
441
442
443
444
445
446
447
  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);
448
  src->buffer_mode = DEFAULT_BUFFER_MODE;
449

450
451
452
  /* get a list of all extensions */
  src->extensions = gst_rtsp_ext_list_get ();

453
454
455
  /* connect to send signal */
  gst_rtsp_ext_list_connect (src->extensions, "send",
      (GCallback) gst_rtspsrc_send_cb, src);
456

457
458
459
460
461
462
  /* 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 */
463
464
  src->state_rec_lock = g_new (GStaticRecMutex, 1);
  g_static_rec_mutex_init (src->state_rec_lock);
465
466
467
468
469

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

470
  src->state = GST_RTSP_STATE_INVALID;
471
472
473
474
475
476
477
478
479
}

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

  rtspsrc = GST_RTSPSRC (object);

480
  gst_rtsp_ext_list_free (rtspsrc->extensions);
481
482
  g_free (rtspsrc->conninfo.location);
  gst_rtsp_url_free (rtspsrc->conninfo.url);
483
484
  g_free (rtspsrc->user_id);
  g_free (rtspsrc->user_pw);
485
486
487
488

  /* free locks */
  g_static_rec_mutex_free (rtspsrc->stream_rec_lock);
  g_free (rtspsrc->stream_rec_lock);
489
490
  g_static_rec_mutex_free (rtspsrc->state_rec_lock);
  g_free (rtspsrc->state_rec_lock);
491
492
  g_static_rec_mutex_free (rtspsrc->conn_rec_lock);
  g_free (rtspsrc->conn_rec_lock);
493

494
495
496
497
#ifdef G_OS_WIN32
  WSACleanup ();
#endif

498
  G_OBJECT_CLASS (parent_class)->finalize (object);
Wim Taymans's avatar
Wim Taymans committed
499
500
}

Wim Taymans's avatar
Wim Taymans committed
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
/* 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;

520
521
522
523
  /* we allow http:// in front but ignore it */
  if (g_str_has_prefix (p, "http://"))
    p += 7;

Wim Taymans's avatar
Wim Taymans committed
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
  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
552
553
554
555
556
557
558
559
560
561
562
563
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
564
565
566
567
568
569
570
571
572
573
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:
574
575
      gst_rtspsrc_uri_set_uri (GST_URI_HANDLER (rtspsrc),
          g_value_get_string (value));
Wim Taymans's avatar
Wim Taymans committed
576
577
578
579
580
581
582
      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
583
584
585
    case PROP_RETRY:
      rtspsrc->retry = g_value_get_uint (value);
      break;
586
    case PROP_TIMEOUT:
587
      rtspsrc->udp_timeout = g_value_get_uint64 (value);
588
      break;
589
    case PROP_TCP_TIMEOUT:
Wim Taymans's avatar
Wim Taymans committed
590
      gst_rtspsrc_set_tcp_timeout (rtspsrc, g_value_get_uint64 (value));
591
      break;
592
593
594
    case PROP_LATENCY:
      rtspsrc->latency = g_value_get_uint (value);
      break;
595
596
597
    case PROP_CONNECTION_SPEED:
      rtspsrc->connection_speed = g_value_get_uint (value);
      break;
598
599
600
    case PROP_NAT_METHOD:
      rtspsrc->nat_method = g_value_get_enum (value);
      break;
601
602
603
    case PROP_DO_RTCP:
      rtspsrc->do_rtcp = g_value_get_boolean (value);
      break;
Wim Taymans's avatar
Wim Taymans committed
604
605
606
    case PROP_PROXY:
      gst_rtspsrc_set_proxy (rtspsrc, g_value_get_string (value));
      break;
607
608
609
    case PROP_RTP_BLOCKSIZE:
      rtspsrc->rtp_blocksize = g_value_get_uint (value);
      break;
610
611
612
613
614
615
616
617
618
619
    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;
620
621
622
    case PROP_BUFFER_MODE:
      rtspsrc->buffer_mode = g_value_get_enum (value);
      break;
Wim Taymans's avatar
Wim Taymans committed
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
    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:
639
      g_value_set_string (value, rtspsrc->conninfo.location);
Wim Taymans's avatar
Wim Taymans committed
640
641
642
643
644
645
646
      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
647
648
649
    case PROP_RETRY:
      g_value_set_uint (value, rtspsrc->retry);
      break;
650
    case PROP_TIMEOUT:
651
652
653
654
655
656
657
658
659
      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);
660
      break;
661
    }
662
663
664
    case PROP_LATENCY:
      g_value_set_uint (value, rtspsrc->latency);
      break;
665
666
667
    case PROP_CONNECTION_SPEED:
      g_value_set_uint (value, rtspsrc->connection_speed);
      break;
668
669
670
    case PROP_NAT_METHOD:
      g_value_set_enum (value, rtspsrc->nat_method);
      break;
671
672
673
    case PROP_DO_RTCP:
      g_value_set_boolean (value, rtspsrc->do_rtcp);
      break;
Wim Taymans's avatar
Wim Taymans committed
674
675
676
677
678
679
680
681
682
683
684
685
686
    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;
    }
687
688
689
    case PROP_RTP_BLOCKSIZE:
      g_value_set_uint (value, rtspsrc->rtp_blocksize);
      break;
690
691
692
693
694
695
    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;
696
697
698
    case PROP_BUFFER_MODE:
      g_value_set_enum (value, rtspsrc->buffer_mode);
      break;
Wim Taymans's avatar
Wim Taymans committed
699
700
701
702
703
704
    default:
      G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
      break;
  }
}

705
static gint
706
find_stream_by_id (GstRTSPStream * stream, gint * id)
707
{
708
  if (stream->id == *id)
709
710
711
712
713
    return 0;

  return -1;
}

714
static gint
715
find_stream_by_channel (GstRTSPStream * stream, gint * channel)
716
{
717
  if (stream->channel[0] == *channel || stream->channel[1] == *channel)
718
719
720
721
722
    return 0;

  return -1;
}

723
static gint
724
find_stream_by_pt (GstRTSPStream * stream, gint * pt)
725
{
726
  if (stream->pt == *pt)
727
728
729
730
731
    return 0;

  return -1;
}

732
733
734
735
736
737
738
static gint
find_stream_by_udpsrc (GstRTSPStream * stream, gconstpointer a)
{
  GstElement *src = (GstElement *) a;

  if (stream->udpsrc[0] == src)
    return 0;
739
740
  if (stream->udpsrc[1] == src)
    return 0;
741
742
743
744

  return -1;
}

745
746
747
748
static gint
find_stream_by_setup (GstRTSPStream * stream, gconstpointer a)
{
  /* check qualified setup_url */
749
  if (!strcmp (stream->conninfo.location, (gchar *) a))
750
751
752
753
754
755
756
757
758
759
760
761
    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;
}

762
static GstRTSPStream *
763
764
765
766
767
768
769
770
771
772
773
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;
}

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
807
808
809
810
811
812
813
814
815
816
817
818
819
820
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;
}

821
822
823
824
static void
gst_rtspsrc_do_stream_connection (GstRTSPSrc * src, GstRTSPStream * stream,
    const GstSDPConnection * conn)
{
825
826
827
828
  if (conn->nettype == NULL || strcmp (conn->nettype, "IN") != 0)
    return;

  if (conn->addrtype == NULL)
829
830
831
832
833
834
835
836
837
838
    return;

  /* check for IPV6 */
  if (strcmp (conn->addrtype, "IP4") == 0)
    stream->is_ipv6 = FALSE;
  else if (strcmp (conn->addrtype, "IP6") == 0)
    stream->is_ipv6 = TRUE;
  else
    return;

839
840
841
842
  /* save address, FIXME, check for multicast */
  g_free (stream->destination);
  stream->destination = g_strdup (conn->address);
  stream->ttl = conn->ttl;
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
}

/* Go over the connections for a stream.
 * - If we are dealing with IPV6, we will setup IPV6 sockets for sending and
 *   receiving.
 * - If we are dealing with a localhost address, we disable multicast
 */
static void
gst_rtspsrc_collect_connections (GstRTSPSrc * src, const GstSDPMessage * sdp,
    const GstSDPMedia * media, GstRTSPStream * stream)
{
  const GstSDPConnection *conn;
  guint i, len;

  /* first look in the media specific section */
  len = gst_sdp_media_connections_len (media);
  for (i = 0; i < len; i++) {
    conn = gst_sdp_media_get_connection (media, i);

    gst_rtspsrc_do_stream_connection (src, stream, conn);
  }
  /* then look in the message specific section */
  if ((conn = gst_sdp_message_get_connection (sdp))) {
    gst_rtspsrc_do_stream_connection (src, stream, conn);
  }
}

Wim Taymans's avatar
Wim Taymans committed
870
static GstRTSPStream *
871
gst_rtspsrc_create_stream (GstRTSPSrc * src, GstSDPMessage * sdp, gint idx)
Wim Taymans's avatar
Wim Taymans committed
872
{
873
  GstRTSPStream *stream;
874
875
876
  const gchar *control_url;
  const gchar *payload;
  const GstSDPMedia *media;
877
878

  /* get media, should not return NULL */
879
  media = gst_sdp_message_get_media (sdp, idx);
880
881
  if (media == NULL)
    return NULL;
882
883
884
885
886
887

  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;
888
  stream->added = FALSE;
889
  stream->disabled = FALSE;
890
  stream->id = src->numstreams++;
891
892
  stream->eos = FALSE;
  stream->discont = TRUE;
893
894
  stream->seqbase = -1;
  stream->timebase = -1;
895

896
897
  /* collect bandwidth information for this steam. FIXME, configure in the RTP
   * session manager to scale RTCP. */
898
899
  gst_rtspsrc_collect_bandwidth (src, sdp, media, stream);

900
901
902
  /* collect connection info */
  gst_rtspsrc_collect_connections (src, sdp, media, stream);

903
  /* we must have a payload. No payload means we cannot create caps */
904
905
906
907
908
  /* 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. */
909
  if ((payload = gst_sdp_media_get_format (media, 0))) {
910
911
912
913
    stream->pt = atoi (payload);
    /* convert caps */
    stream->caps = gst_rtspsrc_media_to_caps (stream->pt, media);

914
915
916
917
918
    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);

919
920
921
922
    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. */
923
      if (find_stream (src, &stream->pt, (gpointer) find_stream_by_pt)) {
924
        stream->container = TRUE;
925
926
        GST_DEBUG ("found another stream with pt %d, marking as container",
            stream->pt);
927
928
929
      }
    }
  }
930
931
  /* collect port number */
  stream->port = gst_sdp_media_get_port (media);
Wim Taymans's avatar
Wim Taymans committed
932

933
934
935
  /* 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. */
936
  control_url = gst_sdp_media_get_attribute_val (media, "control");
Wim Taymans's avatar
Wim Taymans committed
937

938
  GST_DEBUG_OBJECT (src, "stream %d, (%p)", stream->id, stream);
939
  GST_DEBUG_OBJECT (src, " pt: %d", stream->pt);
940
  GST_DEBUG_OBJECT (src, " port: %d", stream->port);
941
942
943
  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
944

945
  if (control_url != NULL) {
946
947
948
949
    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
950
951
     * likely build a URL that the server will fail to understand, this is ok,
     * we will fail then. */
952
    if (g_str_has_prefix (control_url, "rtsp://"))
953
      stream->conninfo.location = g_strdup (control_url);
954
955
956
957
    else {
      const gchar *base;
      gboolean has_slash;

958
959
960
      if (src->control)
        base = src->control;
      else if (src->content_base)
961
        base = src->content_base;
962
963
      else if (src->conninfo.url_str)
        base = src->conninfo.url_str;
964
965
966
967
968
969
970
971
      else
        base = "/";

      /* check if the base ends or control starts with / */
      has_slash = g_str_has_prefix (control_url, "/");
      has_slash = has_slash || g_str_has_suffix (base, "/");

      /* concatenate the two strings, insert / when not present */
972
      stream->conninfo.location =
973
974
          g_strdup_printf ("%s%s%s", base, has_slash ? "" : "/", control_url);
    }
975
  }
976
977
  GST_DEBUG_OBJECT (src, " setup: %s",
      GST_STR_NULL (stream->conninfo.location));
978
979
980
981
982

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

  return stream;
983
984

  /* ERRORS */
Wim Taymans's avatar
Wim Taymans committed
985
986
}

987
static void
988
gst_rtspsrc_stream_free (GstRTSPSrc * src, GstRTSPStream * stream)
989
{
990
991
992
993
994
  gint i;

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

  if (stream->caps)
995
    gst_caps_unref (stream->caps);
996

997
  g_free (stream->destination);
998
  g_free (stream->control_url);
999
  g_free (stream->conninfo.location);
1000

1001
  for (i = 0; i < 2; i++) {
1002
1003
1004
    if (stream->udpsrc[i]) {
      gst_element_set_state (stream->udpsrc[i], GST_STATE_NULL);
      gst_bin_remove (GST_BIN_CAST (src), stream->udpsrc[i]);
1005
1006
1007
      gst_object_unref (stream->udpsrc[i]);
      stream->udpsrc[i] = NULL;
    }
1008
1009
1010
1011
    if (stream->channelpad[i]) {
      gst_object_unref (stream->channelpad[i]);
      stream->channelpad[i] = NULL;
    }
1012
1013
1014
1015
1016
1017
    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;
    }
1018
  }
1019
1020
1021
1022
1023
  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;
1024
  }
1025
1026
1027
1028
1029
1030
1031
1032
  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;
  }
1033
1034
1035
1036
  if (stream->rtcppad) {
    gst_object_unref (stream->rtcppad);
    stream->rtcppad = NULL;
  }
1037
1038
  g_free (stream);
}
1039
1040
1041
1042
1043
1044
1045
1046
1047
1048
1049
1050
1051
1052
1053

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;
1054
1055
1056
1057
1058
1059
1060
1061
1062
  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;
  }
1063
1064
1065
1066
  src->numstreams = 0;
  if (src->props)
    gst_structure_free (src->props);
  src->props = NULL;
Wim Taymans's avatar
Wim Taymans committed
1067
1068
1069
1070

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

1071
1072
1073
  g_free (src->control);
  src->control = NULL;

Wim Taymans's avatar
Wim Taymans committed
1074
1075
1076
  if (src->range)
    gst_rtsp_range_free (src->range);
  src->range = NULL;
1077
1078
1079
1080
1081

  if (src->sdp) {
    gst_sdp_message_free (src->sdp);
    src->sdp = NULL;
  }
1082
1083
}

Thomas Vander Stichele's avatar
Thomas Vander Stichele committed
1084
1085
1086
1087
1088
1089
1090
1091
1092
1093
1094
#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
1095
1096
} G_STMT_END

Thomas Vander Stichele's avatar
Thomas Vander Stichele committed
1097
1098
1099
1100
#define PARSE_STRING(p, del, res)       \
G_STMT_START {                          \
  gchar *t = p;                         \
  p = strstr (p, del);                  \
1101
  if (p == NULL) {                      \
Thomas Vander Stichele's avatar
Thomas Vander Stichele committed
1102
    res = NULL;                         \
1103
1104
    p = t;                              \
  }                                     \
Thomas Vander Stichele's avatar
Thomas Vander Stichele committed
1105
1106
1107
1108
1109
  else {                                \
    *p = '\0';                          \
    p++;                                \
    res = t;                            \
  }                                     \
Wim Taymans's avatar
Wim Taymans committed
1110
1111
} G_STMT_END

Thomas Vander Stichele's avatar
Thomas Vander Stichele committed
1112
1113
#define SKIP_SPACES(p)                  \
  while (*p && g_ascii_isspace (*p))    \
Wim Taymans's avatar
Wim Taymans committed
1114
1115
    p++;

1116
1117
1118
1119
/* rtpmap contains:
 *
 *  <payload> <encoding_name>/<clock_rate>[/<encoding_params>]
 */
Wim Taymans's avatar
Wim Taymans committed
1120
static gboolean
1121
gst_rtspsrc_parse_rtpmap (const gchar * rtpmap, gint * payload, gchar ** name,
Wim Taymans's avatar
Wim Taymans committed
1122
1123
1124
1125
    gint * rate, gchar ** params)
{
  gchar *p, *t;

1126
  p = (gchar *) rtpmap;
Wim Taymans's avatar
Wim Taymans committed
1127

Wim Taymans's avatar
Wim Taymans committed
1128
1129
  PARSE_INT (p, " ", *payload);
  if (*payload == -1)
Wim Taymans's avatar
Wim Taymans committed
1130
1131
    return FALSE;

Wim Taymans's avatar
Wim Taymans committed
1132
  SKIP_SPACES (p);
Wim Taymans's avatar
Wim Taymans committed
1133
1134
1135
  if (*p == '\0')
    return FALSE;

Wim Taymans's avatar
Wim Taymans committed
1136
  PARSE_STRING (p, "/", *name);
1137
  if (*name == NULL) {
1138
    GST_DEBUG ("no rate, name %s", p);
1139
1140
    /* no rate, assume -1 then, this is not supposed to happen but RealMedia
     * streams seem to omit the rate. */
1141
1142
1143
1144
    *name = p;
    *rate = -1;
    return TRUE;
  }
Wim Taymans's avatar
Wim Taymans committed
1145
1146
1147
1148
1149
1150
1151
1152
1153
1154
1155
1156
1157
1158
1159
1160
1161
1162
1163

  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;
}

1164
1165
1166
1167
1168
1169
1170
1171
1172
1173
1174
1175
1176
1177
1178
1179
1180
1181
1182
1183
1184
1185
1186
1187
1188
1189
1190
1191
1192
1193
1194
1195
/*
 * 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;

1196
1197
1198
1199
      /* string must be valid UTF8 */
      if (!g_utf8_validate (attr->value, -1, NULL))
        continue;

1200
1201
1202
1203
1204
1205
1206
1207
1208
1209
1210
1211
      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
1212
1213
1214
/*
 *  Mapping of caps to and from SDP fields:
 *
1215
 *   m=<media> <UDP port> RTP/AVP <payload> 
Wim Taymans's avatar
Wim Taymans committed
1216
 *   a=rtpmap:<payload> <encoding_name>/<clock_rate>[/<encoding_params>]
1217
 *   a=fmtp:<payload> <param>[=<value>];...
Wim Taymans's avatar
Wim Taymans committed
1218