gstrtspsrc.c 110 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_LATENCY_MS      3000
Wim Taymans's avatar
Wim Taymans committed
149
150
151
152
153
154
155

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

161
#define GST_TYPE_RTSP_LOWER_TRANS (gst_rtsp_lower_trans_get_type())
Wim Taymans's avatar
Wim Taymans committed
162
static GType
163
gst_rtsp_lower_trans_get_type (void)
Wim Taymans's avatar
Wim Taymans committed
164
{
165
166
  static GType rtsp_lower_trans_type = 0;
  static const GFlagsValue rtsp_lower_trans[] = {
167
168
169
    {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
170
171
172
    {0, NULL, NULL},
  };

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

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

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

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

196
197
198
199
200
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);

201
202
203
static gboolean gst_rtspsrc_uri_set_uri (GstURIHandler * handler,
    const gchar * uri);

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

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

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

230
231
GST_BOILERPLATE_FULL (GstRTSPSrc, gst_rtspsrc, GstBin, GST_TYPE_BIN, _do_init);

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

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

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

257
258
  gobject_class->finalize = gst_rtspsrc_finalize;

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

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

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

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

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

286
287
288
289
290
  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
291
  gstelement_class->change_state = gst_rtspsrc_change_state;
292
293

  gstbin_class->handle_message = gst_rtspsrc_handle_message;
Wim Taymans's avatar
Wim Taymans committed
294
295
296
}

static void
297
gst_rtspsrc_init (GstRTSPSrc * src, GstRTSPSrcClass * g_class)
Wim Taymans's avatar
Wim Taymans committed
298
{
299
300
  src->stream_rec_lock = g_new (GStaticRecMutex, 1);
  g_static_rec_mutex_init (src->stream_rec_lock);
301

302
  src->location = g_strdup (DEFAULT_LOCATION);
303
  src->url = NULL;
304

305
306
#ifdef WITH_EXT_REAL
  src->extension = rtsp_ext_real_get_context ();
307
308
309
#else
  /* install WMS extension by default */
  src->extension = rtsp_ext_wms_get_context ();
310
#endif
311
  src->extension->src = (gpointer) src;
312

313
314
  src->state_rec_lock = g_new (GStaticRecMutex, 1);
  g_static_rec_mutex_init (src->state_rec_lock);
315
  src->state = RTSP_STATE_INVALID;
316
317
318
319
320
321
322
323
324
325
326
}

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);
327
  g_free (rtspsrc->location);
328
  g_free (rtspsrc->req_location);
329
330
  g_free (rtspsrc->content_base);
  rtsp_url_free (rtspsrc->url);
331
332
  g_static_rec_mutex_free (rtspsrc->state_rec_lock);
  g_free (rtspsrc->state_rec_lock);
333

334
335
336
337
338
339
340
341
  if (rtspsrc->extension) {
#ifdef WITH_EXT_REAL
    rtsp_ext_real_free_context (rtspsrc->extension);
#else
    rtsp_ext_wms_free_context (rtspsrc->extension);
#endif
  }

342
  G_OBJECT_CLASS (parent_class)->finalize (object);
Wim Taymans's avatar
Wim Taymans committed
343
344
345
346
347
348
349
350
351
352
353
354
}

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:
355
356
      gst_rtspsrc_uri_set_uri (GST_URI_HANDLER (rtspsrc),
          g_value_get_string (value));
Wim Taymans's avatar
Wim Taymans committed
357
358
359
360
361
362
363
      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
364
365
366
    case PROP_RETRY:
      rtspsrc->retry = g_value_get_uint (value);
      break;
367
368
369
    case PROP_TIMEOUT:
      rtspsrc->timeout = g_value_get_uint64 (value);
      break;
370
371
372
    case PROP_LATENCY:
      rtspsrc->latency = g_value_get_uint (value);
      break;
Wim Taymans's avatar
Wim Taymans committed
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
    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
397
398
399
    case PROP_RETRY:
      g_value_set_uint (value, rtspsrc->retry);
      break;
400
401
402
    case PROP_TIMEOUT:
      g_value_set_uint64 (value, rtspsrc->timeout);
      break;
403
404
405
    case PROP_LATENCY:
      g_value_set_uint (value, rtspsrc->latency);
      break;
Wim Taymans's avatar
Wim Taymans committed
406
407
408
409
410
411
    default:
      G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
      break;
  }
}

412
413
414
415
416
417
418
419
420
421
422
static gint
find_stream_by_id (GstRTSPStream * stream, gconstpointer a)
{
  gint id = GPOINTER_TO_INT (a);

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

  return -1;
}

423
424
425
426
427
428
429
430
431
432
433
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;
}

434
435
436
437
438
439
440
441
442
443
444
static gint
find_stream_by_pt (GstRTSPStream * stream, gconstpointer a)
{
  gint pt = GPOINTER_TO_INT (a);

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

  return -1;
}

445
446
447
448
449
450
451
static gint
find_stream_by_udpsrc (GstRTSPStream * stream, gconstpointer a)
{
  GstElement *src = (GstElement *) a;

  if (stream->udpsrc[0] == src)
    return 0;
452
453
  if (stream->udpsrc[1] == src)
    return 0;
454
455
456
457

  return -1;
}

458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
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
475
static GstRTSPStream *
476
gst_rtspsrc_create_stream (GstRTSPSrc * src, SDPMessage * sdp, gint idx)
Wim Taymans's avatar
Wim Taymans committed
477
{
478
479
480
  GstRTSPStream *stream;
  gchar *control_url;
  gchar *payload;
481
482
483
484
485
486
  SDPMedia *media;

  /* get media, should not return NULL */
  media = sdp_message_get_media (sdp, idx);
  if (media == NULL)
    return NULL;
487
488
489
490
491
492

  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;
493
  stream->added = FALSE;
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
  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
513

514
515
516
517
  /* 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
518

519
520
521
522
523
  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
524

525
  if (control_url != NULL) {
526
527
528
529
    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
530
531
     * likely build a URL that the server will fail to understand, this is ok,
     * we will fail then. */
532
533
    if (g_str_has_prefix (control_url, "rtsp://"))
      stream->setup_url = g_strdup (control_url);
534
535
536
    else if (src->content_base)
      stream->setup_url =
          g_strdup_printf ("%s%s", src->content_base, control_url);
537
    else
538
539
      stream->setup_url =
          g_strdup_printf ("%s/%s", src->req_location, control_url);
540
541
542
543
544
545
546
  }
  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;
547
548

  /* ERRORS */
Wim Taymans's avatar
Wim Taymans committed
549
550
}

551
static void
552
gst_rtspsrc_stream_free (GstRTSPSrc * src, GstRTSPStream * stream)
553
{
554
555
556
557
558
  gint i;

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

  if (stream->caps)
559
    gst_caps_unref (stream->caps);
560

561
  g_free (stream->control_url);
562
  g_free (stream->setup_url);
563

564
565
566
567
568
569
570
571
572
573
574
575
576
  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;
      }
577

578
579
580
581
582
583
      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;
    }
  }
584
585
586
587
588
589
  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;
  }
590
591
592
593
594
595
596
597
  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;
  }
598
599
  g_free (stream);
}
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614

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;
615
616
617
618
619
620
621
622
623
  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;
  }
624
625
626
627
628
629
  src->numstreams = 0;
  if (src->props)
    gst_structure_free (src->props);
  src->props = NULL;
}

630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
/* 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;
  }
}
670

Thomas Vander Stichele's avatar
Thomas Vander Stichele committed
671
672
673
674
675
676
677
678
679
680
681
#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
682
683
} G_STMT_END

Thomas Vander Stichele's avatar
Thomas Vander Stichele committed
684
685
686
687
688
689
690
691
692
693
694
#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
695
696
} G_STMT_END

Thomas Vander Stichele's avatar
Thomas Vander Stichele committed
697
698
#define SKIP_SPACES(p)                  \
  while (*p && g_ascii_isspace (*p))    \
Wim Taymans's avatar
Wim Taymans committed
699
700
    p++;

701
702
703
704
/* rtpmap contains:
 *
 *  <payload> <encoding_name>/<clock_rate>[/<encoding_params>]
 */
Wim Taymans's avatar
Wim Taymans committed
705
static gboolean
Wim Taymans's avatar
Wim Taymans committed
706
707
708
709
710
711
712
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
713
714
  PARSE_INT (p, " ", *payload);
  if (*payload == -1)
Wim Taymans's avatar
Wim Taymans committed
715
716
    return FALSE;

Wim Taymans's avatar
Wim Taymans committed
717
  SKIP_SPACES (p);
Wim Taymans's avatar
Wim Taymans committed
718
719
720
  if (*p == '\0')
    return FALSE;

Wim Taymans's avatar
Wim Taymans committed
721
  PARSE_STRING (p, "/", *name);
722
  if (*name == NULL) {
723
    /* no rate, assume -1 then */
724
725
726
727
    *name = p;
    *rate = -1;
    return TRUE;
  }
Wim Taymans's avatar
Wim Taymans committed
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749

  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:
 *
750
 *   m=<media> <UDP port> RTP/AVP <payload> 
Wim Taymans's avatar
Wim Taymans committed
751
 *   a=rtpmap:<payload> <encoding_name>/<clock_rate>[/<encoding_params>]
752
 *   a=fmtp:<payload> <param>[=<value>];...
Wim Taymans's avatar
Wim Taymans committed
753
754
 */
static GstCaps *
755
gst_rtspsrc_media_to_caps (gint pt, SDPMedia * media)
Wim Taymans's avatar
Wim Taymans committed
756
757
758
{
  GstCaps *caps;
  gchar *rtpmap;
Wim Taymans's avatar
Wim Taymans committed
759
  gchar *fmtp;
Wim Taymans's avatar
Wim Taymans committed
760
761
762
  gchar *name = NULL;
  gint rate = -1;
  gchar *params = NULL;
763
  gchar *tmp;
Wim Taymans's avatar
Wim Taymans committed
764
  GstStructure *s;
765
766
767
768
769
770
771
772
773
774
775
776
777
  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
778
      }
779
780
781
782
783
784
785
786
787
788
789
    } 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)
790
      goto no_rtpmap;
Wim Taymans's avatar
Wim Taymans committed
791
  }
792
793
794
795
796
797
798
799
  /* 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
800

801
  tmp = g_ascii_strdown (media->media, -1);
802
  caps = gst_caps_new_simple ("application/x-unknown",
803
804
      "media", G_TYPE_STRING, tmp, "payload", G_TYPE_INT, pt, NULL);
  g_free (tmp);
Wim Taymans's avatar
Wim Taymans committed
805
806
807
808
809
  s = gst_caps_get_structure (caps, 0);

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

810
  /* encoding name must be upper case */
811
  if (name != NULL) {
812
813
814
    tmp = g_ascii_strup (name, -1);
    gst_structure_set (s, "encoding-name", G_TYPE_STRING, tmp, NULL);
    g_free (tmp);
815
  }
Wim Taymans's avatar
Wim Taymans committed
816

817
  /* params must be lower case */
818
  if (params != NULL) {
819
820
821
    tmp = g_ascii_strdown (params, -1);
    gst_structure_set (s, "encoding-params", G_TYPE_STRING, tmp, NULL);
    g_free (tmp);
822
  }
Wim Taymans's avatar
Wim Taymans committed
823

Wim Taymans's avatar
Wim Taymans committed
824
825
826
827
828
829
830
  /* parse optional fmtp: field */
  if ((fmtp = sdp_media_get_attribute_val (media, "fmtp"))) {
    gchar *p;
    gint payload = 0;

    p = fmtp;

831
    /* p is now of the format <payload> <param>[=<value>];... */
Wim Taymans's avatar
Wim Taymans committed
832
833
834
835
836
    PARSE_INT (p, " ", payload);
    if (payload != -1 && payload == pt) {
      gchar **pairs;
      gint i;

837
      /* <param>[=<value>] are separated with ';' */
Wim Taymans's avatar
Wim Taymans committed
838
839
      pairs = g_strsplit (p, ";", 0);
      for (i = 0; pairs[i]; i++) {
840
841
842
843
844
845
846
847
        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
848
          /* value is everything between '=' and ';'. FIXME, strip? */
849
850
851
852
          val = g_strstrip (valpos + 1);
        } else {
          /* simple <param>;.. is translated into <param>=1;... */
          val = "1";
Wim Taymans's avatar
Wim Taymans committed
853
        }
854
        /* strip the key of spaces, convert key to lowercase but not the value. */
855
        key = g_strstrip (pairs[i]);
856
857
858
859
860
        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
861
862
863
864
      }
      g_strfreev (pairs);
    }
  }
Wim Taymans's avatar
Wim Taymans committed
865
  return caps;
866
867
868
869
870
871
872

  /* ERRORS */
no_rtpmap:
  {
    g_warning ("rtpmap type not given for dynamic payload %d", pt);
    return NULL;
  }
873
874
875
876
877
no_rate:
  {
    g_warning ("rate unknown for payload type %d", pt);
    return NULL;
  }
Wim Taymans's avatar
Wim Taymans committed
878
879
880
}

static gboolean
881
gst_rtspsrc_alloc_udp_ports (GstRTSPStream * stream,
Wim Taymans's avatar
Wim Taymans committed
882
    gint * rtpport, gint * rtcpport)
Wim Taymans's avatar
Wim Taymans committed
883
884
{
  GstRTSPSrc *src;
885
886
  GstStateChangeReturn ret;
  GstElement *tmp, *udpsrc0, *udpsrc1;
Wim Taymans's avatar
Wim Taymans committed
887
888
  gint tmp_rtp, tmp_rtcp;
  guint count;
Wim Taymans's avatar
Wim Taymans committed
889
890
891

  src = stream->parent;

Wim Taymans's avatar
Wim Taymans committed
892
  tmp = NULL;
893
894
  udpsrc0 = NULL;
  udpsrc1 = NULL;
Wim Taymans's avatar
Wim Taymans committed
895
  count = 0;
Wim Taymans's avatar
Wim Taymans committed
896

897
  /* try to allocate 2 UDP ports, the RTP port should be an even
Wim Taymans's avatar
Wim Taymans committed
898
899
   * number and the RTCP port should be the next (uneven) port */
again:
900
901
902
  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
903

904
  ret = gst_element_set_state (udpsrc0, GST_STATE_PAUSED);
905
  if (ret == GST_STATE_CHANGE_FAILURE)
906
    goto start_udp_failure;
Wim Taymans's avatar
Wim Taymans committed
907

908
  g_object_get (G_OBJECT (udpsrc0), "port", &tmp_rtp, NULL);
Wim Taymans's avatar
Wim Taymans committed
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
  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);
    }
925
    tmp = udpsrc0;
Wim Taymans's avatar
Wim Taymans committed
926
927
928
929
930
931
932
933
934
935
936
    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 */
937
938
  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
939
940
    goto no_udp_rtcp_protocol;

Wim Taymans's avatar
Wim Taymans committed
941
942
  /* set port */
  tmp_rtcp = tmp_rtp + 1;
943
  g_object_set (G_OBJECT (udpsrc1), "port", tmp_rtcp, NULL);
Wim Taymans's avatar
Wim Taymans committed
944

Wim Taymans's avatar
Wim Taymans committed
945
  GST_DEBUG_OBJECT (src, "starting RTCP on port %d", tmp_rtcp);
946
  ret = gst_element_set_state (udpsrc1, GST_STATE_PAUSED);
Wim Taymans's avatar
Wim Taymans committed
947
948
  /* FIXME, this could fail if the next port is not free, we
   * should retry with another port then */
949
  if (ret == GST_STATE_CHANGE_FAILURE)
Wim Taymans's avatar
Wim Taymans committed
950
951
    goto start_rtcp_failure;

Wim Taymans's avatar
Wim Taymans committed
952
  /* all fine, do port check */
953
954
  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
955

956
  /* this should not happen... */
Wim Taymans's avatar
Wim Taymans committed
957
958
959
  if (*rtpport != tmp_rtp || *rtcpport != tmp_rtcp)
    goto port_error;

960
961
  /* we keep these elements, we configure all in configure_transport when the
   * server told us to really use the UDP ports. */
962
963
964
965
966
967
  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
968

Wim Taymans's avatar
Wim Taymans committed
969
970
  return TRUE;

Wim Taymans's avatar
Wim Taymans committed
971
  /* ERRORS */
972
no_udp_protocol:
Wim Taymans's avatar
Wim Taymans committed
973
  {
974
    GST_DEBUG_OBJECT (src, "could not get UDP source");
Wim Taymans's avatar
Wim Taymans committed
975
    goto cleanup;
Wim Taymans's avatar
Wim Taymans committed
976
  }
977
start_udp_failure:
Wim Taymans's avatar
Wim Taymans committed
978
  {
979
    GST_DEBUG_OBJECT (src, "could not start UDP source");
Wim Taymans's avatar
Wim Taymans committed
980
    goto cleanup;
Wim Taymans's avatar
Wim Taymans committed
981
  }
Wim Taymans's avatar
Wim Taymans committed
982
no_ports:
Wim Taymans's avatar
Wim Taymans committed
983
  {
984
985
    GST_DEBUG_OBJECT (src, "could not allocate UDP port pair after %d retries",
        count);
Wim Taymans's avatar
Wim Taymans committed
986
987
988
989
    goto cleanup;
  }
no_udp_rtcp_protocol:
  {
990
    GST_DEBUG_OBJECT (src, "could not get UDP source for RTCP");
Wim Taymans's avatar
Wim Taymans committed
991
    goto cleanup;
Wim Taymans's avatar
Wim Taymans committed
992
993
994
  }
start_rtcp_failure:
  {
995
    GST_DEBUG_OBJECT (src, "could not start UDP source for RTCP");
Wim Taymans's avatar
Wim Taymans committed
996
997
998
999
    goto cleanup;
  }
port_error:
  {
1000
    GST_DEBUG_OBJECT (src, "ports don't match rtp: %d<->%d, rtcp: %d<->%d",
Wim Taymans's avatar
Wim Taymans committed
1001
1002
1003
1004
1005
1006
1007
1008
1009
        tmp_rtp, *rtpport, tmp_rtcp, *rtcpport);
    goto cleanup;
  }
cleanup:
  {
    if (tmp) {
      gst_element_set_state (tmp, GST_STATE_NULL);
      gst_object_unref (tmp);
    }
1010
1011
1012
    if (udpsrc0) {
      gst_element_set_state (udpsrc0, GST_STATE_NULL);
      gst_object_unref (udpsrc0);
Wim Taymans's avatar
Wim Taymans committed
1013
    }
1014
1015
1016
    if (udpsrc1) {
      gst_element_set_state (udpsrc1, GST_STATE_NULL);
      gst_object_unref (udpsrc1);
Wim Taymans's avatar
Wim Taymans committed
1017
    }
Wim Taymans's avatar
Wim Taymans committed
1018
1019
1020
1021
    return FALSE;
  }
}

1022
1023
1024
1025
1026
1027
1028
1029
1030
1031
1032
1033
1034
1035
1036
1037
1038
1039
1040
1041
1042
1043
1044
1045
1046
1047
1048
1049
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
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;
  }
}

1203
1204
1205
1206
static gboolean
gst_rtspsrc_handle_src_event (GstPad * pad, GstEvent * event)
{
  GstRTSPSrc *src;
1207
  gboolean res = FALSE;
1208
1209
1210
1211
1212
1213
1214
1215
1216
1217

  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:
1218
      res = gst_rtspsrc_perform_seek (src, event);
1219
1220
1221
1222
1223
1224
1225
1226
1227
1228
1229
1230
1231
1232
1233
1234
1235
1236
1237
1238
1239
1240
1241
1242
1243
1244
1245
1246
1247
1248
1249
1250
1251
1252
1253
      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:
1254
          gst_query_set_duration (query, format, src->segment.duration);
1255
1256
1257
1258
1259
1260
1261
1262
1263
1264
1265
1266
1267
1268
1269
1270
1271
1272
1273
1274
          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;
}

1275
1276
1277
1278
1279
1280
1281
1282
1283
1284
1285
1286
1287
1288
1289
1290
1291
1292
1293
1294
1295
1296
1297
1298
1299
1300
1301
1302
1303
1304
1305
1306
1307
1308
/* 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;
}

1309
1310
1311
static void
pad_unblocked (GstPad * pad, gboolean blocked, GstRTSPSrc * src)
{
1312
  GST_DEBUG_OBJECT (src, "pad %s:%s unblocked", GST_DEBUG_PAD_NAME (pad));
1313
1314
1315
1316
1317
1318
1319
1320
1321
1322
1323
1324
1325
1326
1327
1328
1329
1330
1331
1332
1333
1334
1335
}

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);
1336
    return;
1337
1338
1339
  }
}

1340
1341
1342
1343
1344
1345
1346
1347
1348
1349
/* 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;
1350
  gboolean all_added;
1351
1352
1353

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

1354
  GST_RTSP_STATE_LOCK (src);
1355
1356
1357
1358
1359
1360
1361
1362
1363
1364
1365
1366
1367
1368
1369
1370
1371
1372
1373
1374
1375
1376
1377
1378
1379
1380
  /* 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 */
1381
  all_added = TRUE;
1382
1383
  for (lstream = src->streams; lstream; lstream = g_list_next (lstream)) {
    stream = (GstRTSPStream *) lstream->data;
1384
1385
1386
1387
1388
1389
1390
1391
1392
1393
1394
1395
    if (!stream->added) {
      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));
1396
1397
1398
1399
1400
1401
1402
1403
  }

  return;

  /* ERRORS */
unknown_stream:
  {
    GST_DEBUG_OBJECT (src, "ignoring unknown stream");
1404
    GST_RTSP_STATE_UNLOCK (src);
1405
1406
1407
1408
1409
    g_free (name);
    return;
  }
}

1410
1411
1412
1413
1414
static GstCaps *
request_pt_map (GstElement * sess, guint session, guint pt, GstRTSPSrc * src)
{
  GstRTSPStream *stream;
  GList *lstream;
1415
  GstCaps *caps;
1416

1417
1418
  GST_DEBUG_OBJECT (src, "getting pt map for pt %d in session %d", pt, session);

1419
  GST_RTSP_STATE_LOCK (src);
1420
1421
1422
1423
1424
1425
  lstream = g_list_find_custom (src->streams, GINT_TO_POINTER (session),
      (GCompareFunc) find_stream_by_id);
  if (!lstream)
    goto unknown_stream;

  stream = (GstRTSPStream *) lstream->data;
1426
1427
  caps = stream->caps;
  GST_RTSP_STATE_UNLOCK (src);
1428

1429
  return caps;
1430
1431