gstrtspsrc.c 113 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
49
50
51
52
53
54
55
56
57
58
59
/**
 * SECTION:element-rtspsrc
 *
 * <refsect2>
 * <para>
 * 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.
 * </para>
 * <para>
 * 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
 * protocols can be controlled with the "protocols" property.
 * </para>
 * <para>
 * rtspsrc currently understands SDP as the format of the session description.
60
 * For each stream listed in the SDP a new rtp_stream%d pad will be created
61
 * with caps derived from the SDP media description. This is a caps of mime type
62
 * "application/x-rtp" that can be connected to any available RTP depayloader
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
 * element. 
 * </para>
 * <para>
 * 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. 
 * This feature is however currently not yet implemented.
 * </para>
 * <para>
 * rtspsrc acts like a live source and will therefore only generate data in the 
 * PLAYING state.
 * </para>
 * <title>Example launch line</title>
 * <para>
 * <programlisting>
 * gst-launch rtspsrc location=rtsp://some.server/url ! fakesink
 * </programlisting>
80
 * Establish a connection to an RTSP server and send the raw RTP packets to a fakesink.
81
82
83
 * </para>
 * </refsect2>
 *
Wim Taymans's avatar
Wim Taymans committed
84
 * Last reviewed on 2006-08-18 (0.10.5)
85
 */
Wim Taymans's avatar
Wim Taymans committed
86
87
88
89
90
91

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

#include <unistd.h>
92
#include <stdlib.h>
Wim Taymans's avatar
Wim Taymans committed
93
94
95
96
#include <string.h>

#include "gstrtspsrc.h"
#include "sdp.h"
97
#include "rtsprange.h"
Wim Taymans's avatar
Wim Taymans committed
98

99
100
/* define for experimental real support */
#undef WITH_EXT_REAL
101

102
#include "rtspextwms.h"
103
104
105
#ifdef WITH_EXT_REAL
#include "rtspextreal.h"
#endif
106

107
GST_DEBUG_CATEGORY_STATIC (rtspsrc_debug);
Wim Taymans's avatar
Wim Taymans committed
108
109
#define GST_CAT_DEFAULT (rtspsrc_debug)

Wim Taymans's avatar
Wim Taymans committed
110
/* elementfactory information */
111
static const GstElementDetails gst_rtspsrc_details =
Wim Taymans's avatar
Wim Taymans committed
112
113
GST_ELEMENT_DETAILS ("RTSP packet receiver",
    "Source/Network",
Wim Taymans's avatar
Wim Taymans committed
114
    "Receive data over the network via RTSP (RFC 2326)",
115
116
117
    "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
118

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

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

131
132
133
134
135
136
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
137
138
139
140
141
142
enum
{
  /* FILL ME */
  LAST_SIGNAL
};

Thomas Vander Stichele's avatar
Thomas Vander Stichele committed
143
#define DEFAULT_LOCATION        NULL
144
#define DEFAULT_PROTOCOLS       RTSP_LOWER_TRANS_UDP | RTSP_LOWER_TRANS_UDP_MCAST | RTSP_LOWER_TRANS_TCP
Thomas Vander Stichele's avatar
Thomas Vander Stichele committed
145
#define DEFAULT_DEBUG           FALSE
Wim Taymans's avatar
Wim Taymans committed
146
#define DEFAULT_RETRY           20
147
#define DEFAULT_TIMEOUT         5000000
148
#define DEFAULT_TCP_TIMEOUT     20000000
149
#define DEFAULT_LATENCY_MS      3000
Wim Taymans's avatar
Wim Taymans committed
150
151
152
153
154
155
156

enum
{
  PROP_0,
  PROP_LOCATION,
  PROP_PROTOCOLS,
  PROP_DEBUG,
Wim Taymans's avatar
Wim Taymans committed
157
  PROP_RETRY,
158
  PROP_TIMEOUT,
159
  PROP_TCP_TIMEOUT,
160
  PROP_LATENCY,
Wim Taymans's avatar
Wim Taymans committed
161
162
};

163
#define GST_TYPE_RTSP_LOWER_TRANS (gst_rtsp_lower_trans_get_type())
Wim Taymans's avatar
Wim Taymans committed
164
static GType
165
gst_rtsp_lower_trans_get_type (void)
Wim Taymans's avatar
Wim Taymans committed
166
{
167
168
  static GType rtsp_lower_trans_type = 0;
  static const GFlagsValue rtsp_lower_trans[] = {
169
170
171
    {RTSP_LOWER_TRANS_UDP, "UDP Unicast Mode", "udp-unicast"},
    {RTSP_LOWER_TRANS_UDP_MCAST, "UDP Multicast Mode", "udp-multicast"},
    {RTSP_LOWER_TRANS_TCP, "TCP interleaved mode", "tcp"},
Wim Taymans's avatar
Wim Taymans committed
172
173
174
    {0, NULL, NULL},
  };

175
176
177
  if (!rtsp_lower_trans_type) {
    rtsp_lower_trans_type =
        g_flags_register_static ("GstRTSPLowerTrans", rtsp_lower_trans);
Wim Taymans's avatar
Wim Taymans committed
178
  }
179
  return rtsp_lower_trans_type;
Wim Taymans's avatar
Wim Taymans committed
180
181
182
}

static void gst_rtspsrc_base_init (gpointer g_class);
183
static void gst_rtspsrc_finalize (GObject * object);
Wim Taymans's avatar
Wim Taymans committed
184

185
186
187
188
189
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
190
191
static void gst_rtspsrc_uri_handler_init (gpointer g_iface,
    gpointer iface_data);
192
static GstCaps *gst_rtspsrc_media_to_caps (gint pt, SDPMedia * media);
Wim Taymans's avatar
Wim Taymans committed
193

194
195
static GstStateChangeReturn gst_rtspsrc_change_state (GstElement * element,
    GstStateChange transition);
196
static void gst_rtspsrc_handle_message (GstBin * bin, GstMessage * message);
Wim Taymans's avatar
Wim Taymans committed
197

198
199
200
201
202
static gboolean gst_rtspsrc_open (GstRTSPSrc * src);
static gboolean gst_rtspsrc_play (GstRTSPSrc * src);
static gboolean gst_rtspsrc_pause (GstRTSPSrc * src);
static gboolean gst_rtspsrc_close (GstRTSPSrc * src);

203
204
205
static gboolean gst_rtspsrc_uri_set_uri (GstURIHandler * handler,
    const gchar * uri);

206
static gboolean gst_rtspsrc_activate_streams (GstRTSPSrc * src);
Wim Taymans's avatar
Wim Taymans committed
207
static void gst_rtspsrc_loop (GstRTSPSrc * src);
208
static void gst_rtspsrc_push_event (GstRTSPSrc * src, GstEvent * event);
Wim Taymans's avatar
Wim Taymans committed
209

210
211
212
213
214
/* 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
215
216
/*static guint gst_rtspsrc_signals[LAST_SIGNAL] = { 0 }; */

217
218
static void
_do_init (GType rtspsrc_type)
Wim Taymans's avatar
Wim Taymans committed
219
{
220
221
222
223
224
225
226
227
228
229
  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
230
231
}

232
233
GST_BOILERPLATE_FULL (GstRTSPSrc, gst_rtspsrc, GstBin, GST_TYPE_BIN, _do_init);

Wim Taymans's avatar
Wim Taymans committed
234
235
236
237
238
239
240
241
242
243
244
245
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
246
gst_rtspsrc_class_init (GstRTSPSrcClass * klass)
Wim Taymans's avatar
Wim Taymans committed
247
248
249
{
  GObjectClass *gobject_class;
  GstElementClass *gstelement_class;
250
  GstBinClass *gstbin_class;
Wim Taymans's avatar
Wim Taymans committed
251
252
253

  gobject_class = (GObjectClass *) klass;
  gstelement_class = (GstElementClass *) klass;
254
  gstbin_class = (GstBinClass *) klass;
Wim Taymans's avatar
Wim Taymans committed
255
256
257
258

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

259
260
  gobject_class->finalize = gst_rtspsrc_finalize;

Wim Taymans's avatar
Wim Taymans committed
261
  g_object_class_install_property (gobject_class, PROP_LOCATION,
Wim Taymans's avatar
Wim Taymans committed
262
263
      g_param_spec_string ("location", "RTSP Location",
          "Location of the RTSP url to read",
264
          DEFAULT_LOCATION, G_PARAM_READWRITE));
Wim Taymans's avatar
Wim Taymans committed
265

Wim Taymans's avatar
Wim Taymans committed
266
  g_object_class_install_property (gobject_class, PROP_PROTOCOLS,
267
      g_param_spec_flags ("protocols", "Protocols",
268
          "Allowed lower transport protocols", GST_TYPE_RTSP_LOWER_TRANS,
269
          DEFAULT_PROTOCOLS, G_PARAM_READWRITE | G_PARAM_CONSTRUCT));
Wim Taymans's avatar
Wim Taymans committed
270

Wim Taymans's avatar
Wim Taymans committed
271
  g_object_class_install_property (gobject_class, PROP_DEBUG,
Wim Taymans's avatar
Wim Taymans committed
272
      g_param_spec_boolean ("debug", "Debug",
273
          "Dump request and response messages to stdout",
Wim Taymans's avatar
Wim Taymans committed
274
275
          DEFAULT_DEBUG, G_PARAM_READWRITE | G_PARAM_CONSTRUCT));

Wim Taymans's avatar
Wim Taymans committed
276
277
278
279
280
281
  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,
          G_PARAM_READWRITE | G_PARAM_CONSTRUCT));

282
283
  g_object_class_install_property (gobject_class, PROP_TIMEOUT,
      g_param_spec_uint64 ("timeout", "Timeout",
284
          "Retry TCP transport after UDP timeout microseconds (0 = disabled)",
285
286
287
          0, G_MAXUINT64, DEFAULT_TIMEOUT,
          G_PARAM_READWRITE | G_PARAM_CONSTRUCT));

288
289
290
291
292
293
  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,
          G_PARAM_READWRITE | G_PARAM_CONSTRUCT));

294
295
296
297
298
  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,
          G_PARAM_READWRITE | G_PARAM_CONSTRUCT));

Wim Taymans's avatar
Wim Taymans committed
299
  gstelement_class->change_state = gst_rtspsrc_change_state;
300
301

  gstbin_class->handle_message = gst_rtspsrc_handle_message;
Wim Taymans's avatar
Wim Taymans committed
302
303
304
}

static void
305
gst_rtspsrc_init (GstRTSPSrc * src, GstRTSPSrcClass * g_class)
Wim Taymans's avatar
Wim Taymans committed
306
{
307
308
  src->stream_rec_lock = g_new (GStaticRecMutex, 1);
  g_static_rec_mutex_init (src->stream_rec_lock);
309

310
  src->location = g_strdup (DEFAULT_LOCATION);
311
  src->url = NULL;
312

313
314
#ifdef WITH_EXT_REAL
  src->extension = rtsp_ext_real_get_context ();
315
316
317
#else
  /* install WMS extension by default */
  src->extension = rtsp_ext_wms_get_context ();
318
#endif
319
  src->extension->src = (gpointer) src;
320

321
322
  src->state_rec_lock = g_new (GStaticRecMutex, 1);
  g_static_rec_mutex_init (src->state_rec_lock);
323
  src->state = RTSP_STATE_INVALID;
324
325
326
327
328
329
330
331
332
333
334
}

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

  rtspsrc = GST_RTSPSRC (object);

  g_static_rec_mutex_free (rtspsrc->stream_rec_lock);
  g_free (rtspsrc->stream_rec_lock);
335
  g_free (rtspsrc->location);
336
  g_free (rtspsrc->req_location);
337
338
  g_free (rtspsrc->content_base);
  rtsp_url_free (rtspsrc->url);
339
340
  g_static_rec_mutex_free (rtspsrc->state_rec_lock);
  g_free (rtspsrc->state_rec_lock);
341

342
343
344
345
346
347
348
349
  if (rtspsrc->extension) {
#ifdef WITH_EXT_REAL
    rtsp_ext_real_free_context (rtspsrc->extension);
#else
    rtsp_ext_wms_free_context (rtspsrc->extension);
#endif
  }

350
  G_OBJECT_CLASS (parent_class)->finalize (object);
Wim Taymans's avatar
Wim Taymans committed
351
352
353
354
355
356
357
358
359
360
361
362
}

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:
363
364
      gst_rtspsrc_uri_set_uri (GST_URI_HANDLER (rtspsrc),
          g_value_get_string (value));
Wim Taymans's avatar
Wim Taymans committed
365
366
367
368
369
370
371
      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
372
373
374
    case PROP_RETRY:
      rtspsrc->retry = g_value_get_uint (value);
      break;
375
    case PROP_TIMEOUT:
376
      rtspsrc->udp_timeout = g_value_get_uint64 (value);
377
      break;
378
379
380
381
382
383
384
385
    case PROP_TCP_TIMEOUT:
    {
      guint64 timeout = g_value_get_uint64 (value);

      rtspsrc->tcp_timeout.tv_sec = timeout / G_USEC_PER_SEC;
      rtspsrc->tcp_timeout.tv_usec = timeout % G_USEC_PER_SEC;
      break;
    }
386
387
388
    case PROP_LATENCY:
      rtspsrc->latency = g_value_get_uint (value);
      break;
Wim Taymans's avatar
Wim Taymans committed
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
    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
413
414
415
    case PROP_RETRY:
      g_value_set_uint (value, rtspsrc->retry);
      break;
416
    case PROP_TIMEOUT:
417
418
419
420
421
422
423
424
425
      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);
426
      break;
427
    }
428
429
430
    case PROP_LATENCY:
      g_value_set_uint (value, rtspsrc->latency);
      break;
Wim Taymans's avatar
Wim Taymans committed
431
432
433
434
435
436
    default:
      G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
      break;
  }
}

437
438
439
440
441
442
443
444
445
446
447
static gint
find_stream_by_id (GstRTSPStream * stream, gconstpointer a)
{
  gint id = GPOINTER_TO_INT (a);

  if (stream->id == id)
    return 0;

  return -1;
}

448
449
450
451
452
453
454
455
456
457
458
static gint
find_stream_by_channel (GstRTSPStream * stream, gconstpointer a)
{
  gint channel = GPOINTER_TO_INT (a);

  if (stream->channel[0] == channel || stream->channel[1] == channel)
    return 0;

  return -1;
}

459
460
461
462
463
464
465
466
467
468
469
static gint
find_stream_by_pt (GstRTSPStream * stream, gconstpointer a)
{
  gint pt = GPOINTER_TO_INT (a);

  if (stream->pt == pt)
    return 0;

  return -1;
}

470
471
472
473
474
475
476
static gint
find_stream_by_udpsrc (GstRTSPStream * stream, gconstpointer a)
{
  GstElement *src = (GstElement *) a;

  if (stream->udpsrc[0] == src)
    return 0;
477
478
  if (stream->udpsrc[1] == src)
    return 0;
479
480
481
482

  return -1;
}

483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
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;
}

Wim Taymans's avatar
Wim Taymans committed
500
static GstRTSPStream *
501
gst_rtspsrc_create_stream (GstRTSPSrc * src, SDPMessage * sdp, gint idx)
Wim Taymans's avatar
Wim Taymans committed
502
{
503
504
505
  GstRTSPStream *stream;
  gchar *control_url;
  gchar *payload;
506
507
508
509
510
511
  SDPMedia *media;

  /* get media, should not return NULL */
  media = sdp_message_get_media (sdp, idx);
  if (media == NULL)
    return NULL;
512
513
514
515
516
517

  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;
518
  stream->added = FALSE;
519
  stream->disabled = FALSE;
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
  stream->id = src->numstreams++;

  /* we must have a payload. No payload means we cannot create caps */
  /* FIXME, handle multiple formats. */
  if ((payload = sdp_media_get_format (media, 0))) {
    stream->pt = atoi (payload);
    /* convert caps */
    stream->caps = gst_rtspsrc_media_to_caps (stream->pt, media);

    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. */
      if (g_list_find_custom (src->streams, GINT_TO_POINTER (stream->pt),
              (GCompareFunc) find_stream_by_pt)) {
        stream->container = TRUE;
      }
    }
  }
Wim Taymans's avatar
Wim Taymans committed
539

540
541
542
543
  /* 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. */
  control_url = sdp_media_get_attribute_val (media, "control");
Wim Taymans's avatar
Wim Taymans committed
544

545
546
547
548
549
  GST_DEBUG_OBJECT (src, "stream %d", stream->id);
  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
550

551
  if (control_url != NULL) {
552
553
554
555
    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
556
557
     * likely build a URL that the server will fail to understand, this is ok,
     * we will fail then. */
558
559
    if (g_str_has_prefix (control_url, "rtsp://"))
      stream->setup_url = g_strdup (control_url);
560
561
562
    else if (src->content_base)
      stream->setup_url =
          g_strdup_printf ("%s%s", src->content_base, control_url);
563
    else
564
565
      stream->setup_url =
          g_strdup_printf ("%s/%s", src->req_location, control_url);
566
567
568
569
570
571
572
  }
  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;
573
574

  /* ERRORS */
Wim Taymans's avatar
Wim Taymans committed
575
576
}

577
static void
578
gst_rtspsrc_stream_free (GstRTSPSrc * src, GstRTSPStream * stream)
579
{
580
581
582
583
584
  gint i;

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

  if (stream->caps)
585
    gst_caps_unref (stream->caps);
586

587
  g_free (stream->control_url);
588
  g_free (stream->setup_url);
589

590
591
592
593
594
595
596
597
598
599
600
601
602
  for (i = 0; i < 2; i++) {
    GstElement *udpsrc = stream->udpsrc[i];

    if (udpsrc) {
      GstPad *pad;

      /* unlink the pad */
      pad = gst_element_get_pad (udpsrc, "src");
      if (stream->channelpad[i]) {
        gst_pad_unlink (pad, stream->channelpad[i]);
        gst_object_unref (stream->channelpad[i]);
        stream->channelpad[i] = NULL;
      }
603

604
605
606
607
608
609
      gst_element_set_state (udpsrc, GST_STATE_NULL);
      gst_bin_remove (GST_BIN_CAST (src), udpsrc);
      gst_object_unref (stream->udpsrc[i]);
      stream->udpsrc[i] = NULL;
    }
  }
610
611
612
613
614
615
  if (stream->udpsink) {
    gst_element_set_state (stream->udpsink, GST_STATE_NULL);
    gst_bin_remove (GST_BIN_CAST (src), stream->udpsink);
    gst_object_unref (stream->udpsink);
    stream->udpsink = NULL;
  }
616
617
618
619
620
621
622
623
  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;
  }
624
625
  g_free (stream);
}
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640

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;
641
642
643
644
645
646
647
648
649
  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;
  }
650
651
652
653
654
655
  src->numstreams = 0;
  if (src->props)
    gst_structure_free (src->props);
  src->props = NULL;
}

656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
/* FIXME, this should go somewhere else, ideally 
 */
static guint
get_default_rate_for_pt (gint pt)
{
  switch (pt) {
    case 0:
    case 3:
    case 4:
    case 5:
    case 7:
    case 8:
    case 9:
    case 12:
    case 13:
    case 15:
    case 18:
      return 8000;
    case 16:
      return 11025;
    case 17:
      return 22050;
    case 6:
      return 16000;
    case 10:
    case 11:
      return 44100;
    case 14:
    case 25:
    case 26:
    case 28:
    case 31:
    case 32:
    case 33:
    case 34:
      return 90000;
    default:
      return -1;
  }
}
696

Thomas Vander Stichele's avatar
Thomas Vander Stichele committed
697
698
699
700
701
702
703
704
705
706
707
#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
708
709
} G_STMT_END

Thomas Vander Stichele's avatar
Thomas Vander Stichele committed
710
711
712
713
714
715
716
717
718
719
720
#define PARSE_STRING(p, del, res)       \
G_STMT_START {                          \
  gchar *t = p;                         \
  p = strstr (p, del);                  \
  if (p == NULL)                        \
    res = NULL;                         \
  else {                                \
    *p = '\0';                          \
    p++;                                \
    res = t;                            \
  }                                     \
Wim Taymans's avatar
Wim Taymans committed
721
722
} G_STMT_END

Thomas Vander Stichele's avatar
Thomas Vander Stichele committed
723
724
#define SKIP_SPACES(p)                  \
  while (*p && g_ascii_isspace (*p))    \
Wim Taymans's avatar
Wim Taymans committed
725
726
    p++;

727
728
729
730
/* rtpmap contains:
 *
 *  <payload> <encoding_name>/<clock_rate>[/<encoding_params>]
 */
Wim Taymans's avatar
Wim Taymans committed
731
static gboolean
Wim Taymans's avatar
Wim Taymans committed
732
733
734
735
736
737
738
gst_rtspsrc_parse_rtpmap (gchar * rtpmap, gint * payload, gchar ** name,
    gint * rate, gchar ** params)
{
  gchar *p, *t;

  t = p = rtpmap;

Wim Taymans's avatar
Wim Taymans committed
739
740
  PARSE_INT (p, " ", *payload);
  if (*payload == -1)
Wim Taymans's avatar
Wim Taymans committed
741
742
    return FALSE;

Wim Taymans's avatar
Wim Taymans committed
743
  SKIP_SPACES (p);
Wim Taymans's avatar
Wim Taymans committed
744
745
746
  if (*p == '\0')
    return FALSE;

Wim Taymans's avatar
Wim Taymans committed
747
  PARSE_STRING (p, "/", *name);
748
  if (*name == NULL) {
749
    /* no rate, assume -1 then */
750
751
752
753
    *name = p;
    *rate = -1;
    return TRUE;
  }
Wim Taymans's avatar
Wim Taymans committed
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775

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

/*
 *  Mapping of caps to and from SDP fields:
 *
776
 *   m=<media> <UDP port> RTP/AVP <payload> 
Wim Taymans's avatar
Wim Taymans committed
777
 *   a=rtpmap:<payload> <encoding_name>/<clock_rate>[/<encoding_params>]
778
 *   a=fmtp:<payload> <param>[=<value>];...
Wim Taymans's avatar
Wim Taymans committed
779
780
 */
static GstCaps *
781
gst_rtspsrc_media_to_caps (gint pt, SDPMedia * media)
Wim Taymans's avatar
Wim Taymans committed
782
783
784
{
  GstCaps *caps;
  gchar *rtpmap;
Wim Taymans's avatar
Wim Taymans committed
785
  gchar *fmtp;
Wim Taymans's avatar
Wim Taymans committed
786
787
788
  gchar *name = NULL;
  gint rate = -1;
  gchar *params = NULL;
789
  gchar *tmp;
Wim Taymans's avatar
Wim Taymans committed
790
  GstStructure *s;
791
792
793
794
795
796
797
798
799
800
801
802
803
  gint payload = 0;
  gboolean ret;

  /* get and parse rtpmap */
  if ((rtpmap = sdp_media_get_attribute_val (media, "rtpmap"))) {
    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
804
      }
805
806
807
808
809
810
811
812
813
814
815
    } 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)
816
      goto no_rtpmap;
Wim Taymans's avatar
Wim Taymans committed
817
  }
818
819
820
821
822
823
824
825
  /* 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) {
    rate = get_default_rate_for_pt (pt);
    /* we fail if we cannot find one */
    if (rate == -1)
      goto no_rate;
  }
Wim Taymans's avatar
Wim Taymans committed
826

827
  tmp = g_ascii_strdown (media->media, -1);
828
  caps = gst_caps_new_simple ("application/x-unknown",
829
830
      "media", G_TYPE_STRING, tmp, "payload", G_TYPE_INT, pt, NULL);
  g_free (tmp);
Wim Taymans's avatar
Wim Taymans committed
831
832
833
834
835
  s = gst_caps_get_structure (caps, 0);

  if (rate != -1)
    gst_structure_set (s, "clock-rate", G_TYPE_INT, rate, NULL);

836
  /* encoding name must be upper case */
837
  if (name != NULL) {
838
839
840
    tmp = g_ascii_strup (name, -1);
    gst_structure_set (s, "encoding-name", G_TYPE_STRING, tmp, NULL);
    g_free (tmp);
841
  }
Wim Taymans's avatar
Wim Taymans committed
842

843
  /* params must be lower case */
844
  if (params != NULL) {
845
846
847
    tmp = g_ascii_strdown (params, -1);
    gst_structure_set (s, "encoding-params", G_TYPE_STRING, tmp, NULL);
    g_free (tmp);
848
  }
Wim Taymans's avatar
Wim Taymans committed
849

Wim Taymans's avatar
Wim Taymans committed
850
851
852
853
854
855
856
  /* parse optional fmtp: field */
  if ((fmtp = sdp_media_get_attribute_val (media, "fmtp"))) {
    gchar *p;
    gint payload = 0;

    p = fmtp;

857
    /* p is now of the format <payload> <param>[=<value>];... */
Wim Taymans's avatar
Wim Taymans committed
858
859
860
861
862
    PARSE_INT (p, " ", payload);
    if (payload != -1 && payload == pt) {
      gchar **pairs;
      gint i;

863
      /* <param>[=<value>] are separated with ';' */
Wim Taymans's avatar
Wim Taymans committed
864
865
      pairs = g_strsplit (p, ";", 0);
      for (i = 0; pairs[i]; i++) {
866
867
868
869
870
871
872
873
        gchar *valpos;
        gchar *val, *key;

        /* the key may not have a '=', the value can have other '='s */
        valpos = strstr (pairs[i], "=");
        if (valpos) {
          /* we have a '=' and thus a value, remove the '=' with \0 */
          *valpos = '\0';
Wim Taymans's avatar
Wim Taymans committed
874
          /* value is everything between '=' and ';'. FIXME, strip? */
875
876
877
878
          val = g_strstrip (valpos + 1);
        } else {
          /* simple <param>;.. is translated into <param>=1;... */
          val = "1";
Wim Taymans's avatar
Wim Taymans committed
879
        }
880
        /* strip the key of spaces, convert key to lowercase but not the value. */
881
        key = g_strstrip (pairs[i]);
882
883
884
885
886
        if (strlen (key) > 1) {
          tmp = g_ascii_strdown (key, -1);
          gst_structure_set (s, tmp, G_TYPE_STRING, val, NULL);
          g_free (tmp);
        }
Wim Taymans's avatar
Wim Taymans committed
887
888
889
890
      }
      g_strfreev (pairs);
    }
  }
Wim Taymans's avatar
Wim Taymans committed
891
  return caps;
892
893
894
895
896
897
898

  /* ERRORS */
no_rtpmap:
  {
    g_warning ("rtpmap type not given for dynamic payload %d", pt);
    return NULL;
  }
899
900
901
902
903
no_rate:
  {
    g_warning ("rate unknown for payload type %d", pt);
    return NULL;
  }
Wim Taymans's avatar
Wim Taymans committed
904
905
906
}

static gboolean
907
gst_rtspsrc_alloc_udp_ports (GstRTSPStream * stream,
Wim Taymans's avatar
Wim Taymans committed
908
    gint * rtpport, gint * rtcpport)
Wim Taymans's avatar
Wim Taymans committed
909
910
{
  GstRTSPSrc *src;
911
912
  GstStateChangeReturn ret;
  GstElement *tmp, *udpsrc0, *udpsrc1;
Wim Taymans's avatar
Wim Taymans committed
913
914
  gint tmp_rtp, tmp_rtcp;
  guint count;
Wim Taymans's avatar
Wim Taymans committed
915
916
917

  src = stream->parent;

Wim Taymans's avatar
Wim Taymans committed
918
  tmp = NULL;
919
920
  udpsrc0 = NULL;
  udpsrc1 = NULL;
Wim Taymans's avatar
Wim Taymans committed
921
  count = 0;
Wim Taymans's avatar
Wim Taymans committed
922

923
  /* try to allocate 2 UDP ports, the RTP port should be an even
Wim Taymans's avatar
Wim Taymans committed
924
925
   * number and the RTCP port should be the next (uneven) port */
again:
926
927
928
  udpsrc0 = gst_element_make_from_uri (GST_URI_SRC, "udp://0.0.0.0:0", NULL);
  if (udpsrc0 == NULL)
    goto no_udp_protocol;
Wim Taymans's avatar
Wim Taymans committed
929

930
  ret = gst_element_set_state (udpsrc0, GST_STATE_PAUSED);
931
  if (ret == GST_STATE_CHANGE_FAILURE)
932
    goto start_udp_failure;
Wim Taymans's avatar
Wim Taymans committed
933

934
  g_object_get (G_OBJECT (udpsrc0), "port", &tmp_rtp, NULL);
Wim Taymans's avatar
Wim Taymans committed
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
  GST_DEBUG_OBJECT (src, "got RTP port %d", tmp_rtp);

  /* check if port is even */
  if ((tmp_rtp & 0x01) != 0) {
    /* port not even, close and allocate another */
    count++;
    if (count > src->retry)
      goto no_ports;

    GST_DEBUG_OBJECT (src, "RTP port not even, retry %d", count);
    /* have to keep port allocated so we can get a new one */
    if (tmp != NULL) {
      GST_DEBUG_OBJECT (src, "free temp");
      gst_element_set_state (tmp, GST_STATE_NULL);
      gst_object_unref (tmp);
    }
951
    tmp = udpsrc0;
Wim Taymans's avatar
Wim Taymans committed
952
953
954
955
956
957
958
959
960
961
962
    GST_DEBUG_OBJECT (src, "retry %d", count);
    goto again;
  }
  /* free leftover temp element/port */
  if (tmp) {
    gst_element_set_state (tmp, GST_STATE_NULL);
    gst_object_unref (tmp);
    tmp = NULL;
  }

  /* allocate port+1 for RTCP now */
963
964
  udpsrc1 = gst_element_make_from_uri (GST_URI_SRC, "udp://0.0.0.0", NULL);
  if (udpsrc1 == NULL)
Wim Taymans's avatar
Wim Taymans committed
965
966
    goto no_udp_rtcp_protocol;

Wim Taymans's avatar
Wim Taymans committed
967
968
  /* set port */
  tmp_rtcp = tmp_rtp + 1;
969
  g_object_set (G_OBJECT (udpsrc1), "port", tmp_rtcp, NULL);
Wim Taymans's avatar
Wim Taymans committed
970

Wim Taymans's avatar
Wim Taymans committed
971
  GST_DEBUG_OBJECT (src, "starting RTCP on port %d", tmp_rtcp);
972
  ret = gst_element_set_state (udpsrc1, GST_STATE_PAUSED);
Wim Taymans's avatar
Wim Taymans committed
973
974
  /* FIXME, this could fail if the next port is not free, we
   * should retry with another port then */
975
  if (ret == GST_STATE_CHANGE_FAILURE)
Wim Taymans's avatar
Wim Taymans committed
976
977
    goto start_rtcp_failure;

Wim Taymans's avatar
Wim Taymans committed
978
  /* all fine, do port check */
979
980
  g_object_get (G_OBJECT (udpsrc0), "port", rtpport, NULL);
  g_object_get (G_OBJECT (udpsrc1), "port", rtcpport, NULL);
Wim Taymans's avatar
Wim Taymans committed
981

982
  /* this should not happen... */
Wim Taymans's avatar
Wim Taymans committed
983
984
985
  if (*rtpport != tmp_rtp || *rtcpport != tmp_rtcp)
    goto port_error;

986
987
  /* we keep these elements, we configure all in configure_transport when the
   * server told us to really use the UDP ports. */
988
989
990
991
992
993
  stream->udpsrc[0] = gst_object_ref (udpsrc0);
  stream->udpsrc[1] = gst_object_ref (udpsrc1);

  /* they are ours now */
  gst_object_sink (udpsrc0);
  gst_object_sink (udpsrc1);
Wim Taymans's avatar
Wim Taymans committed
994

Wim Taymans's avatar
Wim Taymans committed
995
996
  return TRUE;

Wim Taymans's avatar
Wim Taymans committed
997
  /* ERRORS */
998
no_udp_protocol:
Wim Taymans's avatar
Wim Taymans committed
999
  {
1000
    GST_DEBUG_OBJECT (src, "could not get UDP source");
Wim Taymans's avatar
Wim Taymans committed
1001
    goto cleanup;
Wim Taymans's avatar
Wim Taymans committed
1002
  }
1003
start_udp_failure:
Wim Taymans's avatar
Wim Taymans committed
1004
  {
1005
    GST_DEBUG_OBJECT (src, "could not start UDP source");
Wim Taymans's avatar
Wim Taymans committed
1006
    goto cleanup;
Wim Taymans's avatar
Wim Taymans committed
1007
  }
Wim Taymans's avatar
Wim Taymans committed
1008
no_ports:
Wim Taymans's avatar
Wim Taymans committed
1009
  {
1010
1011
    GST_DEBUG_OBJECT (src, "could not allocate UDP port pair after %d retries",
        count);
Wim Taymans's avatar
Wim Taymans committed
1012
1013
1014
1015
    goto cleanup;
  }
no_udp_rtcp_protocol:
  {
1016
    GST_DEBUG_OBJECT (src, "could not get UDP source for RTCP");
Wim Taymans's avatar
Wim Taymans committed
1017
    goto cleanup;
Wim Taymans's avatar
Wim Taymans committed
1018
1019
1020
  }
start_rtcp_failure:
  {
1021
    GST_DEBUG_OBJECT (src, "could not start UDP source for RTCP");
Wim Taymans's avatar
Wim Taymans committed
1022
1023
1024
1025
    goto cleanup;
  }
port_error:
  {
1026
    GST_DEBUG_OBJECT (src, "ports don't match rtp: %d<->%d, rtcp: %d<->%d",
Wim Taymans's avatar
Wim Taymans committed
1027
1028
1029
1030
1031
1032
1033
1034
1035
        tmp_rtp, *rtpport, tmp_rtcp, *rtcpport);
    goto cleanup;
  }
cleanup:
  {
    if (tmp) {
      gst_element_set_state (tmp, GST_STATE_NULL);
      gst_object_unref (tmp);
    }
1036
1037
1038
    if (udpsrc0) {
      gst_element_set_state (udpsrc0, GST_STATE_NULL);
      gst_object_unref (udpsrc0);
Wim Taymans's avatar
Wim Taymans committed
1039
    }
1040
1041
1042
    if (udpsrc1) {
      gst_element_set_state (udpsrc1, GST_STATE_NULL);
      gst_object_unref (udpsrc1);
Wim Taymans's avatar
Wim Taymans committed
1043
    }
Wim Taymans's avatar
Wim Taymans committed
1044
1045
1046
1047
    return FALSE;
  }
}

1048
1049
1050
1051
1052
1053
1054
1055
1056
1057
1058
1059
1060
1061
1062
1063
1064
1065
1066
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
1099
1100
1101
1102
1103
1104
1105
1106
1107
1108
1109
1110
1111
1112
1113
1114
1115
1116
1117
1118
1119
1120
1121
1122
1123
1124
1125
1126
1127
1128
1129
1130
1131
1132
1133
1134
1135
1136
1137
1138
1139
1140
1141
1142
1143
1144
1145
1146
1147
1148
1149
1150
1151
1152
1153
1154
1155
1156
1157
1158
1159
1160
1161
1162
1163
1164
1165
1166
1167
1168
1169
1170
1171
1172
1173
1174
1175
1176
1177
1178
1179
1180
1181
1182
1183
1184
1185
1186
1187
1188
1189
1190
1191
1192
1193
1194
1195
1196
1197
1198
1199
1200
1201
1202
1203
1204
1205
1206
1207
1208
1209
1210
1211
1212
1213
1214
1215
1216
1217
1218
1219
1220
1221
1222
1223
1224
1225
1226
1227
1228
static void
gst_rtspsrc_flush (GstRTSPSrc * src, gboolean flush)
{
  GstEvent *event;

  if (flush) {
    event = gst_event_new_flush_start ();
  } else {
    event = gst_event_new_flush_stop ();
  }

  rtsp_connection_flush (src->connection, flush);

  gst_rtspsrc_push_event (src, event);
}

static gboolean
gst_rtspsrc_do_seek (GstRTSPSrc * src, GstSegment * segment)
{
  gboolean res;

  /* PLAY from new position, we are flushing now */
  src->position = ((gdouble) segment->last_stop) / GST_SECOND;

  src->state = RTSP_STATE_SEEKING;

  res = gst_rtspsrc_play (src);

  return res;
}

static gboolean
gst_rtspsrc_perform_seek (GstRTSPSrc * src, GstEvent * event)
{
  gboolean res;
  gdouble rate;
  GstFormat format;
  GstSeekFlags flags;
  GstSeekType cur_type = GST_SEEK_TYPE_NONE, stop_type;
  gint64 cur, stop;
  gboolean flush;
  gboolean update;
  GstSegment seeksegment = { 0, };
  gint64 last_stop;

  if (event) {
    GST_DEBUG_OBJECT (src, "doing seek with event");

    gst_event_parse_seek (event, &rate, &format, &flags,
        &cur_type, &cur, &stop_type, &stop);

    /* no negative rates yet */
    if (rate < 0.0)
      goto negative_rate;

    /* we need TIME format */
    if (format != src->segment.format)
      goto no_format;
  } else {
    GST_DEBUG_OBJECT (src, "doing seek without event");
    flags = 0;
    cur_type = GST_SEEK_TYPE_SET;
    stop_type = GST_SEEK_TYPE_SET;
  }

  /* get flush flag */
  flush = flags & GST_SEEK_FLAG_FLUSH;

  /* now we need to make sure the streaming thread is stopped. We do this by
   * either sending a FLUSH_START event downstream which will cause the
   * streaming thread to stop with a WRONG_STATE.
   * For a non-flushing seek we simply pause the task, which will happen as soon
   * as it completes one iteration (and thus might block when the sink is
   * blocking in preroll). */
  if (flush) {
    GST_DEBUG_OBJECT (src, "starting flush");
    gst_rtspsrc_flush (src, TRUE);
  } else {
    //gst_pad_pause_task (src->sinkpad);
  }

  /* we should now be able to grab the streaming thread because we stopped it
   * with the above flush/pause code */
  //GST_PAD_STREAM_LOCK (src->sinkpad);

  /* save current position */
  last_stop = src->segment.last_stop;

  GST_DEBUG_OBJECT (src, "stopped streaming at %" G_GINT64_FORMAT, last_stop);

  /* copy segment, we need this because we still need the old
   * segment when we close the current segment. */
  memcpy (&seeksegment, &src->segment, sizeof (GstSegment));

  /* configure the seek parameters in the seeksegment. We will then have the
   * right values in the segment to perform the seek */
  if (event) {
    GST_DEBUG_OBJECT (src, "configuring seek");
    gst_segment_set_seek (&seeksegment, rate, format, flags,
        cur_type, cur, stop_type, stop, &update);
  }

  /* figure out the last position we need to play. If it's configured (stop !=
   * -1), use that, else we play until the total duration of the file */
  if ((stop = seeksegment.stop) == -1)
    stop = seeksegment.duration;

  res = gst_rtspsrc_do_seek (src, &seeksegment);

  /* prepare for streaming again */
  if (flush) {
    /* if we started flush, we stop now */
    GST_DEBUG_OBJECT (src, "stopping flush");
    gst_rtspsrc_flush (src, FALSE);
  } else if (src->running) {
    /* we are running the current segment and doing a non-flushing seek,
     * close the segment first based on the previous last_stop. */
    GST_DEBUG_OBJECT (src, "closing running segment %" G_GINT64_FORMAT
        " to %" G_GINT64_FORMAT, src->segment.accum, src->segment.last_stop);

    /* queue the segment for sending in the stream thread */
    if (src->close_segment)
      gst_event_unref (src->close_segment);
    src->close_segment = gst_event_new_new_segment (TRUE,
        src->segment.rate, src->segment.format,
        src->segment.accum, src->segment.last_stop, src->segment.accum);

    /* keep track of our last_stop */
    seeksegment.accum = src->segment.last_stop;
  }

  /* now we did the seek and can activate the new segment values */
  memcpy (&src->segment, &seeksegment, sizeof (GstSegment));

  /* if we're doing a segment seek, post a SEGMENT_START message */
  if (src->segment.flags & GST_SEEK_FLAG_SEGMENT) {
    gst_element_post_message (GST_ELEMENT_CAST (src),
        gst_message_new_segment_start (GST_OBJECT_CAST (src),
            src->segment.format, src->segment.last_stop));
  }

  /* now create the newsegment */
  GST_DEBUG_OBJECT (src, "Creating newsegment from %" G_GINT64_FORMAT
      " to %" G_GINT64_FORMAT, src->segment.last_stop, stop);

  /* store the newsegment event so it can be sent from the streaming thread. */
  if (src->start_segment)
    gst_event_unref (src->start_segment);
  src->start_segment =
      gst_event_new_new_segment (FALSE, src->segment.rate,
      src->segment.format, src->segment.last_stop, stop,
      src->segment.last_stop);

  /* mark discont if we are going to stream from another position. */
  if (last_stop != src->segment.last_stop) {
    GST_DEBUG_OBJECT (src, "mark DISCONT, we did a seek to another position");
    //src->discont = TRUE;
  }

  /* and start the streaming task again */
  src->running = TRUE;
  //gst_pad_start_task (src->sinkpad, (GstTaskFunction) gst_srcparse_loop,
  //    src->sinkpad);

  //GST_PAD_STREAM_UNLOCK (src->sinkpad);

  return TRUE;

  /* ERRORS */
negative_rate:
  {
    GST_DEBUG_OBJECT (src, "negative playback rates are not supported yet.");
    return FALSE;
  }
no_format:
  {
    GST_DEBUG_OBJECT (src, "unsupported format given, seek aborted.");
    return FALSE;
  }
}

1229
1230
1231
1232
static gboolean
gst_rtspsrc_handle_src_event (GstPad * pad, GstEvent * event)
{
  GstRTSPSrc *src;
1233
  gboolean res = FALSE;
1234
1235
1236
1237
1238
1239
1240
1241
1242
1243

  src = GST_RTSPSRC_CAST (gst_pad_get_element_private (pad));

  GST_DEBUG_OBJECT (src, "pad %s:%s received event %s",
      GST_DEBUG_PAD_NAME (pad), GST_EVENT_TYPE_NAME (event));

  switch (GST_EVENT_TYPE (event)) {
    case GST_EVENT_QOS:
      break;
    case GST_EVENT_SEEK:
1244
      res = gst_rtspsrc_perform_seek (src, event);
1245
1246
1247
1248
1249
1250
1251
1252
1253
1254
1255
1256
1257
1258
1259
1260
1261
1262
1263
1264
1265
1266
1267
1268
1269
1270
1271
1272
1273
1274
1275
1276
1277
1278
1279
      break;
    case GST_EVENT_NAVIGATION:
      break;
    case GST_EVENT_LATENCY:
      break;
    default:
      break;
  }
  return res;
}

static gboolean
gst_rtspsrc_handle_src_query (GstPad * pad, GstQuery * query)
{
  GstRTSPSrc *src;
  gboolean res = TRUE;

  src = GST_RTSPSRC_CAST (gst_pad_get_element_private (pad));

  GST_DEBUG_OBJECT (src, "pad %s:%s received query %s",
      GST_DEBUG_PAD_NAME (pad), GST_QUERY_TYPE_NAME (query));

  switch (GST_QUERY_TYPE (query)) {
    case GST_QUERY_POSITION:
    {
      break;
    }
    case GST_QUERY_DURATION:
    {
      GstFormat format;

      gst_query_parse_duration (query, &format, NULL);

      switch (format) {
        case GST_FORMAT_TIME:
1280
          gst_query_set_duration (query, format, src->segment.duration);
1281
1282
1283
1284
1285
1286
1287
1288
1289
1290
1291
1292
1293
1294
1295
1296
1297
1298
1299
1300
          break;
        default:
          res = FALSE;
          break;
      }
      break;
    }
    case GST_QUERY_LATENCY:
    {
      /* we are live with a min latency of 0 and unlimted max latency */
      gst_query_set_latency (query, TRUE, 0, -1);
      break;
    }
    default:
      break;
  }

  return res;
}

1301
1302
1303
1304
1305
1306
1307
1308
1309
1310
1311
1312
1313
1314
1315
1316
1317
1318
1319
1320
1321
1322
1323
1324
1325
1326
1327
1328
1329
1330
1331
1332
1333
1334
/* callback for RTCP messages to be sent to the server when operating in TCP
 * mode. */
static GstFlowReturn
gst_rtspsrc_sink_chain (GstPad * pad, GstBuffer * buffer)
{
  GstRTSPSrc *src;
  GstRTSPStream *stream;
  GstFlowReturn res = GST_FLOW_OK;
  guint8 *data;
  guint size;
  RTSPResult ret;
  RTSPMessage message = { 0 };

  stream = (GstRTSPStream *) gst_pad_get_element_private (pad);
  src = stream->parent;

  data = GST_BUFFER_DATA (buffer);
  size = GST_BUFFER_SIZE (buffer);

  rtsp_message_init_data (&message, stream->channel[1]);

  rtsp_message_take_body (&message, data, size);

  GST_DEBUG_OBJECT (src, "sending %u bytes RTCP", size);
  ret = rtsp_connection_send (src->connection, &message, NULL);
  GST_DEBUG_OBJECT (src, "sent RTCP, %d", ret);

  rtsp_message_steal_body (&message, &data, &size);

  gst_buffer_unref (buffer);

  return res;
}

1335
1336
1337
static void
pad_unblocked (GstPad * pad, gboolean blocked, GstRTSPSrc * src)
{
1338
  GST_DEBUG_OBJECT (src, "pad %s:%s unblocked", GST_DEBUG_PAD_NAME (pad));
1339
1340
1341
1342
1343
1344
1345
1346
1347
1348
1349
1350
1351
1352
1353
1354
1355
1356
1357
1358
1359
1360
1361
}

static void
pad_blocked (GstPad * pad, gboolean blocked, GstRTSPSrc * src)
{
  GST_DEBUG_OBJECT (src, "pad %s:%s blocked, activating streams",
      GST_DEBUG_PAD_NAME (pad));

  /* activate the streams */
  GST_OBJECT_LOCK (src);
  if (!src->need_activate)
    goto was_ok;

  src->need_activate = FALSE;
  GST_OBJECT_UNLOCK (src);

  gst_rtspsrc_activate_streams (src);

  return;

was_ok:
  {
    GST_OBJECT_UNLOCK (src);
1362
    return;
1363
1364
1365
  }
}

1366
1367
1368
1369
1370
1371
1372
1373
1374
1375
/* this callback is called when the session manager generated a new src pad with
 * payloaded RTP packets. We simply ghost the pad here. */
static void
new_session_pad (GstElement * session, GstPad * pad, GstRTSPSrc * src)
{
  gchar *name;
  GstPadTemplate *template;
  gint id, ssrc, pt;
  GList *lstream;
  GstRTSPStream *stream;
1376
  gboolean all_added;
1377
1378
1379

  GST_DEBUG_OBJECT (src, "got new session pad %" GST_PTR_FORMAT, pad);

1380
  GST_RTSP_STATE_LOCK (src);
1381
1382
1383
1384
1385
1386
1387
1388
1389
1390
1391
1392
1393
1394
1395
1396
1397
1398
1399
1400
1401
1402
1403
1404
1405
1406
  /* find stream */
  name = gst_object_get_name (GST_OBJECT_CAST (pad));
  if (sscanf (name, "recv_rtp_src_%d_%d_%d", &id, &ssrc, &pt) != 3)
    goto unknown_stream;

  GST_DEBUG_OBJECT (src, "stream: %u, SSRC %d, PT %d", id, ssrc, pt);

  lstream = g_list_find_custom (src->streams, GINT_TO_POINTER (id),
      (GCompareFunc) find_stream_by_id);
  if (lstream == NULL)
    goto unknown_stream;

  /* get stream */
  stream = (GstRTSPStream *) lstream->data;

  /* create a new pad we will use to stream to */
  template = gst_static_pad_template_get (&rtptemplate);
  stream->srcpad = gst_ghost_pad_new_from_template (name, pad, template);
  gst_object_unref (template);
  g_free (name);

  stream->added = TRUE;
  gst_pad_set_active (stream->srcpad, TRUE);
  gst_element_add_pad (GST_ELEMENT_CAST (src), stream->srcpad);

  /* check if we added all streams */
1407
  all_added = TRUE;
1408
1409
  for (lstream = src->streams; lstream; lstream = g_list_next (lstream)) {
    stream = (GstRTSPStream *) lstream->data;
1410
1411
1412
    /* a container stream only needs one pad added. Also disabled streams don't
     * count */
    if (!stream->container && !stream->disabled && !stream->added) {
1413
1414
1415
1416
1417
1418
1419
1420
1421
1422
1423
      all_added = FALSE;
      break;
    }
  }
  GST_RTSP_STATE_UNLOCK (src);

  if (all_added) {
    GST_DEBUG_OBJECT (src, "We added all streams");
    /* when we get here, all stream are added and we can fire the no-more-pads
     * signal. */
    gst_element_no_more_pads (GST_ELEMENT_CAST (src));
1424
1425
1426
1427