gstrtspsrc.c 60.2 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
97
#include <string.h>

#include "gstrtspsrc.h"
#include "sdp.h"

98
#include "rtspextwms.h"
99
100
101
#ifdef WITH_EXT_REAL
#include "rtspextreal.h"
#endif
102

103
GST_DEBUG_CATEGORY_STATIC (rtspsrc_debug);
Wim Taymans's avatar
Wim Taymans committed
104
105
#define GST_CAT_DEFAULT (rtspsrc_debug)

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

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

enum
{
  /* FILL ME */
  LAST_SIGNAL
};

Thomas Vander Stichele's avatar
Thomas Vander Stichele committed
126
#define DEFAULT_LOCATION        NULL
127
#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
128
#define DEFAULT_DEBUG           FALSE
Wim Taymans's avatar
Wim Taymans committed
129
#define DEFAULT_RETRY           20
130
#define DEFAULT_TIMEOUT         5000000
Wim Taymans's avatar
Wim Taymans committed
131
132
133
134
135
136
137

enum
{
  PROP_0,
  PROP_LOCATION,
  PROP_PROTOCOLS,
  PROP_DEBUG,
Wim Taymans's avatar
Wim Taymans committed
138
  PROP_RETRY,
139
  PROP_TIMEOUT,
Wim Taymans's avatar
Wim Taymans committed
140
141
142
143
144
145
146
};

#define GST_TYPE_RTSP_PROTO (gst_rtsp_proto_get_type())
static GType
gst_rtsp_proto_get_type (void)
{
  static GType rtsp_proto_type = 0;
147
  static const GFlagsValue rtsp_proto[] = {
148
149
150
    {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
151
152
153
154
155
156
157
158
159
160
    {0, NULL, NULL},
  };

  if (!rtsp_proto_type) {
    rtsp_proto_type = g_flags_register_static ("GstRTSPProto", rtsp_proto);
  }
  return rtsp_proto_type;
}

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

Wim Taymans's avatar
Wim Taymans committed
163
164
static void gst_rtspsrc_uri_handler_init (gpointer g_iface,
    gpointer iface_data);
165
static GstCaps *gst_rtspsrc_media_to_caps (gint pt, SDPMedia * media);
Wim Taymans's avatar
Wim Taymans committed
166

167
168
static GstStateChangeReturn gst_rtspsrc_change_state (GstElement * element,
    GstStateChange transition);
169
static void gst_rtspsrc_handle_message (GstBin * bin, GstMessage * message);
Wim Taymans's avatar
Wim Taymans committed
170
171
172
173
174
175

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

176
177
178
static gboolean gst_rtspsrc_uri_set_uri (GstURIHandler * handler,
    const gchar * uri);

Wim Taymans's avatar
Wim Taymans committed
179
180
static void gst_rtspsrc_loop (GstRTSPSrc * src);

181
182
183
184
185
/* 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
186
187
/*static guint gst_rtspsrc_signals[LAST_SIGNAL] = { 0 }; */

188
189
static void
_do_init (GType rtspsrc_type)
Wim Taymans's avatar
Wim Taymans committed
190
{
191
192
193
194
195
196
197
198
199
200
  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
201
202
}

203
204
GST_BOILERPLATE_FULL (GstRTSPSrc, gst_rtspsrc, GstBin, GST_TYPE_BIN, _do_init);

Wim Taymans's avatar
Wim Taymans committed
205
206
207
208
209
210
211
212
213
214
215
216
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
217
gst_rtspsrc_class_init (GstRTSPSrcClass * klass)
Wim Taymans's avatar
Wim Taymans committed
218
219
220
{
  GObjectClass *gobject_class;
  GstElementClass *gstelement_class;
221
  GstBinClass *gstbin_class;
Wim Taymans's avatar
Wim Taymans committed
222
223
224

  gobject_class = (GObjectClass *) klass;
  gstelement_class = (GstElementClass *) klass;
225
  gstbin_class = (GstBinClass *) klass;
Wim Taymans's avatar
Wim Taymans committed
226
227
228
229

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

230
231
  gobject_class->finalize = gst_rtspsrc_finalize;

Wim Taymans's avatar
Wim Taymans committed
232
  g_object_class_install_property (gobject_class, PROP_LOCATION,
Wim Taymans's avatar
Wim Taymans committed
233
234
      g_param_spec_string ("location", "RTSP Location",
          "Location of the RTSP url to read",
235
          DEFAULT_LOCATION, G_PARAM_READWRITE));
Wim Taymans's avatar
Wim Taymans committed
236

Wim Taymans's avatar
Wim Taymans committed
237
  g_object_class_install_property (gobject_class, PROP_PROTOCOLS,
238
239
240
      g_param_spec_flags ("protocols", "Protocols",
          "Allowed lower transport protocols", GST_TYPE_RTSP_PROTO,
          DEFAULT_PROTOCOLS, G_PARAM_READWRITE | G_PARAM_CONSTRUCT));
Wim Taymans's avatar
Wim Taymans committed
241

Wim Taymans's avatar
Wim Taymans committed
242
  g_object_class_install_property (gobject_class, PROP_DEBUG,
Wim Taymans's avatar
Wim Taymans committed
243
      g_param_spec_boolean ("debug", "Debug",
244
          "Dump request and response messages to stdout",
Wim Taymans's avatar
Wim Taymans committed
245
246
          DEFAULT_DEBUG, G_PARAM_READWRITE | G_PARAM_CONSTRUCT));

Wim Taymans's avatar
Wim Taymans committed
247
248
249
250
251
252
  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));

253
254
255
256
257
258
  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));

Wim Taymans's avatar
Wim Taymans committed
259
  gstelement_class->change_state = gst_rtspsrc_change_state;
260
261

  gstbin_class->handle_message = gst_rtspsrc_handle_message;
Wim Taymans's avatar
Wim Taymans committed
262
263
264
}

static void
265
gst_rtspsrc_init (GstRTSPSrc * src, GstRTSPSrcClass * g_class)
Wim Taymans's avatar
Wim Taymans committed
266
{
267
268
  src->stream_rec_lock = g_new (GStaticRecMutex, 1);
  g_static_rec_mutex_init (src->stream_rec_lock);
269

270
271
  src->loop_cond = g_cond_new ();

272
  src->location = g_strdup (DEFAULT_LOCATION);
273
  src->url = NULL;
274
275
276

  /* install WMS extension by default */
  src->extension = rtsp_ext_wms_get_context ();
277
278
279
#ifdef WITH_EXT_REAL
  src->extension = rtsp_ext_real_get_context ();
#endif
280
  src->extension->src = (gpointer) src;
281
282
283
284
285
286
287
288
289
290
291
}

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);
292
  g_cond_free (rtspsrc->loop_cond);
293
294
295
  g_free (rtspsrc->location);
  g_free (rtspsrc->content_base);
  rtsp_url_free (rtspsrc->url);
296
297

  G_OBJECT_CLASS (parent_class)->finalize (object);
Wim Taymans's avatar
Wim Taymans committed
298
299
300
301
302
303
304
305
306
307
308
309
}

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:
310
311
      gst_rtspsrc_uri_set_uri (GST_URI_HANDLER (rtspsrc),
          g_value_get_string (value));
Wim Taymans's avatar
Wim Taymans committed
312
313
314
315
316
317
318
      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
319
320
321
    case PROP_RETRY:
      rtspsrc->retry = g_value_get_uint (value);
      break;
322
323
324
    case PROP_TIMEOUT:
      rtspsrc->timeout = g_value_get_uint64 (value);
      break;
Wim Taymans's avatar
Wim Taymans committed
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
    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
349
350
351
    case PROP_RETRY:
      g_value_set_uint (value, rtspsrc->retry);
      break;
352
353
354
    case PROP_TIMEOUT:
      g_value_set_uint64 (value, rtspsrc->timeout);
      break;
Wim Taymans's avatar
Wim Taymans committed
355
356
357
358
359
360
    default:
      G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
      break;
  }
}

361
362
363
364
365
366
367
368
369
370
371
static gint
find_stream_by_pt (GstRTSPStream * stream, gconstpointer a)
{
  gint pt = GPOINTER_TO_INT (a);

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

  return -1;
}

Wim Taymans's avatar
Wim Taymans committed
372
static GstRTSPStream *
373
gst_rtspsrc_create_stream (GstRTSPSrc * src, SDPMessage * sdp, gint idx)
Wim Taymans's avatar
Wim Taymans committed
374
{
375
376
377
  GstRTSPStream *stream;
  gchar *control_url;
  gchar *payload;
378
379
380
381
382
383
  SDPMedia *media;

  /* get media, should not return NULL */
  media = sdp_message_get_media (sdp, idx);
  if (media == NULL)
    return NULL;
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408

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

410
411
412
413
  /* 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
414

415
416
417
418
419
  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
420

421
  if (control_url != NULL) {
422
423
424
    /* If the control_url starts with a '/' or a non rtsp: protocol we will most
     * likely build a URL that the server will fail to understand, this is ok,
     * we will fail then. */
425
426
    if (g_str_has_prefix (control_url, "rtsp://"))
      stream->setup_url = g_strdup (control_url);
427
428
429
    else if (src->content_base)
      stream->setup_url =
          g_strdup_printf ("%s%s", src->content_base, control_url);
430
431
432
433
434
435
436
437
438
    else
      stream->setup_url = g_strdup_printf ("%s/%s", src->location, control_url);
  }
  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;
Wim Taymans's avatar
Wim Taymans committed
439
440
}

441
442
443
444
445
446
447
#if 0
static void
gst_rtspsrc_free_stream (GstRTSPSrc * src, GstRTSPStream * stream)
{
  if (stream->caps) {
    gst_caps_unref (stream->caps);
  }
448
  g_free (stream->setup_url);
449
450
451
452
453
454
455
456

  src->streams = g_list_remove (src->streams, stream);
  src->numstreams--;

  g_free (stream);
}
#endif

Thomas Vander Stichele's avatar
Thomas Vander Stichele committed
457
458
459
460
461
462
463
464
465
466
467
#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
468
469
} G_STMT_END

Thomas Vander Stichele's avatar
Thomas Vander Stichele committed
470
471
472
473
474
475
476
477
478
479
480
#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
481
482
} G_STMT_END

Thomas Vander Stichele's avatar
Thomas Vander Stichele committed
483
484
#define SKIP_SPACES(p)                  \
  while (*p && g_ascii_isspace (*p))    \
Wim Taymans's avatar
Wim Taymans committed
485
486
    p++;

487
488
489
490
/* rtpmap contains:
 *
 *  <payload> <encoding_name>/<clock_rate>[/<encoding_params>]
 */
Wim Taymans's avatar
Wim Taymans committed
491
static gboolean
Wim Taymans's avatar
Wim Taymans committed
492
493
494
495
496
497
498
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
499
500
  PARSE_INT (p, " ", *payload);
  if (*payload == -1)
Wim Taymans's avatar
Wim Taymans committed
501
502
    return FALSE;

Wim Taymans's avatar
Wim Taymans committed
503
  SKIP_SPACES (p);
Wim Taymans's avatar
Wim Taymans committed
504
505
506
  if (*p == '\0')
    return FALSE;

Wim Taymans's avatar
Wim Taymans committed
507
  PARSE_STRING (p, "/", *name);
508
509
510
511
512
513
  if (*name == NULL) {
    /* no rate, assume 0 then */
    *name = p;
    *rate = -1;
    return TRUE;
  }
Wim Taymans's avatar
Wim Taymans committed
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535

  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:
 *
536
 *   m=<media> <UDP port> RTP/AVP <payload> 
Wim Taymans's avatar
Wim Taymans committed
537
 *   a=rtpmap:<payload> <encoding_name>/<clock_rate>[/<encoding_params>]
538
 *   a=fmtp:<payload> <param>[=<value>];...
Wim Taymans's avatar
Wim Taymans committed
539
540
 */
static GstCaps *
541
gst_rtspsrc_media_to_caps (gint pt, SDPMedia * media)
Wim Taymans's avatar
Wim Taymans committed
542
543
544
{
  GstCaps *caps;
  gchar *rtpmap;
Wim Taymans's avatar
Wim Taymans committed
545
  gchar *fmtp;
Wim Taymans's avatar
Wim Taymans committed
546
547
548
549
550
  gchar *name = NULL;
  gint rate = -1;
  gchar *params = NULL;
  GstStructure *s;

551
  /* dynamic payloads need rtpmap */
Wim Taymans's avatar
Wim Taymans committed
552
553
554
555
  if (pt >= 96) {
    gint payload = 0;
    gboolean ret;

Wim Taymans's avatar
Wim Taymans committed
556
    if ((rtpmap = sdp_media_get_attribute_val (media, "rtpmap"))) {
557
558
      ret = gst_rtspsrc_parse_rtpmap (rtpmap, &payload, &name, &rate, &params);
      if (ret) {
Wim Taymans's avatar
Wim Taymans committed
559
        if (payload != pt) {
560
          /* FIXME, not fatal? */
Wim Taymans's avatar
Wim Taymans committed
561
562
563
564
565
566
          g_warning ("rtpmap of wrong payload type");
          name = NULL;
          rate = -1;
          params = NULL;
        }
      } else {
567
        /* FIXME, not fatal? */
Wim Taymans's avatar
Wim Taymans committed
568
569
        g_warning ("error parsing rtpmap");
      }
570
571
    } else
      goto no_rtpmap;
Wim Taymans's avatar
Wim Taymans committed
572
573
  }

574
  caps = gst_caps_new_simple ("application/x-unknown",
Wim Taymans's avatar
Wim Taymans committed
575
576
577
578
579
580
581
582
583
584
585
586
      "media", G_TYPE_STRING, media->media, "payload", G_TYPE_INT, pt, NULL);
  s = gst_caps_get_structure (caps, 0);

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

  if (name != NULL)
    gst_structure_set (s, "encoding-name", G_TYPE_STRING, name, NULL);

  if (params != NULL)
    gst_structure_set (s, "encoding-params", G_TYPE_STRING, params, NULL);

Wim Taymans's avatar
Wim Taymans committed
587
588
589
590
591
592
593
  /* parse optional fmtp: field */
  if ((fmtp = sdp_media_get_attribute_val (media, "fmtp"))) {
    gchar *p;
    gint payload = 0;

    p = fmtp;

594
    /* p is now of the format <payload> <param>[=<value>];... */
Wim Taymans's avatar
Wim Taymans committed
595
596
597
598
599
    PARSE_INT (p, " ", payload);
    if (payload != -1 && payload == pt) {
      gchar **pairs;
      gint i;

600
      /* <param>[=<value>] are separated with ';' */
Wim Taymans's avatar
Wim Taymans committed
601
602
      pairs = g_strsplit (p, ";", 0);
      for (i = 0; pairs[i]; i++) {
603
604
605
606
607
608
609
610
        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
611
          /* value is everything between '=' and ';'. FIXME, strip? */
612
613
614
615
          val = g_strstrip (valpos + 1);
        } else {
          /* simple <param>;.. is translated into <param>=1;... */
          val = "1";
Wim Taymans's avatar
Wim Taymans committed
616
        }
617
618
619
620
        /* strip the key of spaces */
        key = g_strstrip (pairs[i]);

        gst_structure_set (s, key, G_TYPE_STRING, val, NULL);
Wim Taymans's avatar
Wim Taymans committed
621
622
623
624
      }
      g_strfreev (pairs);
    }
  }
Wim Taymans's avatar
Wim Taymans committed
625
  return caps;
626
627
628
629
630
631
632

  /* ERRORS */
no_rtpmap:
  {
    g_warning ("rtpmap type not given for dynamic payload %d", pt);
    return NULL;
  }
Wim Taymans's avatar
Wim Taymans committed
633
634
635
}

static gboolean
636
gst_rtspsrc_alloc_udp_ports (GstRTSPStream * stream,
Wim Taymans's avatar
Wim Taymans committed
637
    gint * rtpport, gint * rtcpport)
Wim Taymans's avatar
Wim Taymans committed
638
639
{
  GstRTSPSrc *src;
640
641
  GstStateChangeReturn ret;
  GstElement *tmp, *udpsrc0, *udpsrc1;
Wim Taymans's avatar
Wim Taymans committed
642
643
  gint tmp_rtp, tmp_rtcp;
  guint count;
Wim Taymans's avatar
Wim Taymans committed
644
645
646

  src = stream->parent;

Wim Taymans's avatar
Wim Taymans committed
647
  tmp = NULL;
648
649
  udpsrc0 = NULL;
  udpsrc1 = NULL;
Wim Taymans's avatar
Wim Taymans committed
650
  count = 0;
Wim Taymans's avatar
Wim Taymans committed
651

652
  /* try to allocate 2 UDP ports, the RTP port should be an even
Wim Taymans's avatar
Wim Taymans committed
653
654
   * number and the RTCP port should be the next (uneven) port */
again:
655
656
657
  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
658

659
  ret = gst_element_set_state (udpsrc0, GST_STATE_PAUSED);
660
  if (ret == GST_STATE_CHANGE_FAILURE)
661
    goto start_udp_failure;
Wim Taymans's avatar
Wim Taymans committed
662

663
  g_object_get (G_OBJECT (udpsrc0), "port", &tmp_rtp, NULL);
Wim Taymans's avatar
Wim Taymans committed
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
  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);
    }
680
    tmp = udpsrc0;
Wim Taymans's avatar
Wim Taymans committed
681
682
683
684
685
686
687
688
689
690
691
    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 */
692
693
  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
694
695
    goto no_udp_rtcp_protocol;

Wim Taymans's avatar
Wim Taymans committed
696
697
  /* set port */
  tmp_rtcp = tmp_rtp + 1;
698
  g_object_set (G_OBJECT (udpsrc1), "port", tmp_rtcp, NULL);
Wim Taymans's avatar
Wim Taymans committed
699

Wim Taymans's avatar
Wim Taymans committed
700
  GST_DEBUG_OBJECT (src, "starting RTCP on port %d", tmp_rtcp);
701
  ret = gst_element_set_state (udpsrc1, GST_STATE_PAUSED);
Wim Taymans's avatar
Wim Taymans committed
702
703
  /* FIXME, this could fail if the next port is not free, we
   * should retry with another port then */
704
  if (ret == GST_STATE_CHANGE_FAILURE)
Wim Taymans's avatar
Wim Taymans committed
705
706
    goto start_rtcp_failure;

Wim Taymans's avatar
Wim Taymans committed
707
  /* all fine, do port check */
708
709
  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
710
711
712
713
714

  /* this should not happen */
  if (*rtpport != tmp_rtp || *rtcpport != tmp_rtcp)
    goto port_error;

715
716
717
718
  /* we keep these elements, we configure all in configure_transport when the
   * server told us to really use the UDP ports. */
  stream->udpsrc[0] = udpsrc0;
  stream->udpsrc[1] = udpsrc1;
Wim Taymans's avatar
Wim Taymans committed
719

Wim Taymans's avatar
Wim Taymans committed
720
721
  return TRUE;

Wim Taymans's avatar
Wim Taymans committed
722
  /* ERRORS */
723
no_udp_protocol:
Wim Taymans's avatar
Wim Taymans committed
724
  {
725
    GST_DEBUG_OBJECT (src, "could not get UDP source");
Wim Taymans's avatar
Wim Taymans committed
726
    goto cleanup;
Wim Taymans's avatar
Wim Taymans committed
727
  }
728
start_udp_failure:
Wim Taymans's avatar
Wim Taymans committed
729
  {
730
    GST_DEBUG_OBJECT (src, "could not start UDP source");
Wim Taymans's avatar
Wim Taymans committed
731
    goto cleanup;
Wim Taymans's avatar
Wim Taymans committed
732
  }
Wim Taymans's avatar
Wim Taymans committed
733
no_ports:
Wim Taymans's avatar
Wim Taymans committed
734
  {
735
736
    GST_DEBUG_OBJECT (src, "could not allocate UDP port pair after %d retries",
        count);
Wim Taymans's avatar
Wim Taymans committed
737
738
739
740
    goto cleanup;
  }
no_udp_rtcp_protocol:
  {
741
    GST_DEBUG_OBJECT (src, "could not get UDP source for RTCP");
Wim Taymans's avatar
Wim Taymans committed
742
    goto cleanup;
Wim Taymans's avatar
Wim Taymans committed
743
744
745
  }
start_rtcp_failure:
  {
746
    GST_DEBUG_OBJECT (src, "could not start UDP source for RTCP");
Wim Taymans's avatar
Wim Taymans committed
747
748
749
750
    goto cleanup;
  }
port_error:
  {
751
    GST_DEBUG_OBJECT (src, "ports don't match rtp: %d<->%d, rtcp: %d<->%d",
Wim Taymans's avatar
Wim Taymans committed
752
753
754
755
756
757
758
759
760
        tmp_rtp, *rtpport, tmp_rtcp, *rtcpport);
    goto cleanup;
  }
cleanup:
  {
    if (tmp) {
      gst_element_set_state (tmp, GST_STATE_NULL);
      gst_object_unref (tmp);
    }
761
762
763
    if (udpsrc0) {
      gst_element_set_state (udpsrc0, GST_STATE_NULL);
      gst_object_unref (udpsrc0);
Wim Taymans's avatar
Wim Taymans committed
764
    }
765
766
767
    if (udpsrc1) {
      gst_element_set_state (udpsrc1, GST_STATE_NULL);
      gst_object_unref (udpsrc1);
Wim Taymans's avatar
Wim Taymans committed
768
    }
Wim Taymans's avatar
Wim Taymans committed
769
770
771
772
773
774
    return FALSE;
  }
}

static gboolean
gst_rtspsrc_stream_configure_transport (GstRTSPStream * stream,
775
    RTSPTransport * transport)
Wim Taymans's avatar
Wim Taymans committed
776
777
{
  GstRTSPSrc *src;
778
  GstPad *outpad = NULL;
779
  GstPadTemplate *template;
780
  GstStateChangeReturn ret;
781
  gchar *name;
782
783
784
  GstStructure *s;
  const gchar *mime, *manager;
  RTSPResult res;
Wim Taymans's avatar
Wim Taymans committed
785
786
787

  src = stream->parent;

788
789
790
  GST_DEBUG_OBJECT (src, "configuring transport for stream %p", stream);

  s = gst_caps_get_structure (stream->caps, 0);
791

792
793
794
795
  if ((res = rtsp_transport_get_mime (transport->trans, &mime)) < 0)
    goto no_mime;
  if (!mime)
    goto no_mime;
Wim Taymans's avatar
Wim Taymans committed
796

797
798
799
  GST_DEBUG_OBJECT (src, "setting mime to %s", mime);
  /* configure the final mime type */
  gst_structure_set_name (s, mime);
Wim Taymans's avatar
Wim Taymans committed
800

801
802
  if ((res = rtsp_transport_get_manager (transport->trans, &manager)) < 0)
    goto no_manager;
Wim Taymans's avatar
Wim Taymans committed
803

804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
  if (manager) {
    GST_DEBUG_OBJECT (src, "using manager %s", manager);
    /* FIXME, the session manager needs to be shared with all the streams */
    if (!(stream->sess = gst_element_factory_make (manager, NULL)))
      goto no_element;

    /* we manage this element */
    gst_bin_add (GST_BIN_CAST (src), stream->sess);

    ret = gst_element_set_state (stream->sess, GST_STATE_PAUSED);
    if (ret != GST_STATE_CHANGE_SUCCESS)
      goto start_session_failure;

    stream->channelpad[0] = gst_element_get_pad (stream->sess, "sinkrtp");
    stream->channelpad[1] = gst_element_get_pad (stream->sess, "sinkrtcp");
  }
820
821

  if (transport->lower_transport == RTSP_LOWER_TRANS_TCP) {
822
823
    gint i;

824
825
    /* configure for interleaved delivery, nothing needs to be done
     * here, the loop function will call the chain functions of the
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
     * session manager. */
    stream->channel[0] = transport->interleaved.min;
    stream->channel[1] = transport->interleaved.max;
    GST_DEBUG_OBJECT (src, "stream %p on channels %d-%d", stream,
        stream->channel[0], stream->channel[1]);

    /* we can remove the allocated UDP ports now */
    for (i = 0; i < 2; i++) {
      if (stream->udpsrc[i]) {
        gst_element_set_state (stream->udpsrc[i], GST_STATE_NULL);
        gst_object_unref (stream->udpsrc[i]);
        stream->udpsrc[i] = NULL;
      }
    }
    /* no session manager, send data to srcpad directly */
    if (!stream->channelpad[0]) {
      GST_DEBUG_OBJECT (src, "no manager, creating pad");

      name = g_strdup_printf ("stream%d", stream->id);
      template = gst_static_pad_template_get (&rtptemplate);
      stream->srcpad = gst_pad_new_from_template (template, name);
      gst_object_unref (template);
      g_free (name);

      gst_pad_use_fixed_caps (stream->srcpad);
      gst_pad_set_caps (stream->srcpad, stream->caps);

      stream->channelpad[0] = gst_object_ref (stream->srcpad);
854
      gst_pad_set_active (stream->srcpad, TRUE);
855
856
857
858
859
      gst_element_add_pad (GST_ELEMENT_CAST (src), stream->srcpad);
    } else {
      GST_DEBUG_OBJECT (src, "using manager source pad");
      outpad = gst_element_get_pad (stream->sess, "srcrtp");
    }
Wim Taymans's avatar
Wim Taymans committed
860
  } else {
861
    /* multicast was selected, create UDP sources and join the multicast
862
     * group. */
863
    if (transport->lower_transport == RTSP_LOWER_TRANS_UDP_MCAST) {
864
865
      gchar *uri;

866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
      GST_DEBUG_OBJECT (src, "creating UDP sources for multicast");

      /* creating UDP source */
      if (transport->port.min != -1) {
        uri = g_strdup_printf ("udp://%s:%d", transport->destination,
            transport->port.min);
        stream->udpsrc[0] = gst_element_make_from_uri (GST_URI_SRC, uri, NULL);
        g_free (uri);
        if (stream->udpsrc[0] == NULL)
          goto no_element;

        /* change state */
        gst_element_set_state (stream->udpsrc[0], GST_STATE_PAUSED);
      }

      /* creating another UDP source */
      if (transport->port.max != -1) {
        uri = g_strdup_printf ("udp://%s:%d", transport->destination,
            transport->port.max);
        stream->udpsrc[1] = gst_element_make_from_uri (GST_URI_SRC, uri, NULL);
        g_free (uri);
        if (stream->udpsrc[1] == NULL)
          goto no_element;

        gst_element_set_state (stream->udpsrc[1], GST_STATE_PAUSED);
      }
892
893
    }

894
895
896
897
    /* we manage the UDP elements now. For unicast, the UDP sources where
     * allocated in the stream when we suggested a transport. */
    if (stream->udpsrc[0]) {
      GstPad *pad;
898

899
      gst_bin_add (GST_BIN_CAST (src), stream->udpsrc[0]);
900

901
      GST_DEBUG_OBJECT (src, "setting up UDP source");
902

903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
      /* set caps */
      g_object_set (G_OBJECT (stream->udpsrc[0]), "caps", stream->caps, NULL);

      /* configure a timeout on the UDP port */
      g_object_set (G_OBJECT (stream->udpsrc[0]), "timeout", src->timeout,
          NULL);

      if (stream->channelpad[0]) {
        GST_DEBUG_OBJECT (src, "connecting UDP source 0 to manager");
        /* configure for UDP delivery, we need to connect the UDP pads to
         * the session plugin. */
        pad = gst_element_get_pad (stream->udpsrc[0], "src");
        gst_pad_link (pad, stream->channelpad[0]);
        gst_object_unref (pad);

        outpad = gst_element_get_pad (stream->sess, "srcrtp");
      } else {
        GST_DEBUG_OBJECT (src, "using UDP src pad as output");
        outpad = gst_element_get_pad (stream->udpsrc[0], "src");
      }
    }

    if (stream->udpsrc[1]) {
      gst_bin_add (GST_BIN_CAST (src), stream->udpsrc[1]);

      if (stream->channelpad[1]) {
        GstPad *pad;

        GST_DEBUG_OBJECT (src, "connecting UDP source 1 to manager");

        pad = gst_element_get_pad (stream->udpsrc[1], "src");
        gst_pad_link (pad, stream->channelpad[1]);
        gst_object_unref (pad);
      }
    }
938
  }
939

940
941
942
943
944
945
946
947
948
949
950
951
  if (outpad && !stream->srcpad) {
    GST_DEBUG_OBJECT (src, "creating ghostpad");

    gst_pad_use_fixed_caps (outpad);
    gst_pad_set_caps (outpad, stream->caps);

    /* create ghostpad */
    name = g_strdup_printf ("stream%d", stream->id);
    template = gst_static_pad_template_get (&rtptemplate);
    stream->srcpad = gst_ghost_pad_new_from_template (name, outpad, template);
    gst_object_unref (template);
    g_free (name);
952

953
    gst_object_unref (outpad);
954

955
    /* and add */
956
    gst_pad_set_active (stream->srcpad, TRUE);
957
958
    gst_element_add_pad (GST_ELEMENT_CAST (src), stream->srcpad);
  }
959
960
  /* mark pad as ok */
  stream->last_ret = GST_FLOW_OK;
961

Wim Taymans's avatar
Wim Taymans committed
962
963
  return TRUE;

964
  /* ERRORS */
965
966
967
968
969
970
971
972
973
974
no_mime:
  {
    GST_DEBUG_OBJECT (src, "unknown transport");
    return FALSE;
  }
no_manager:
  {
    GST_DEBUG_OBJECT (src, "cannot get a session manager");
    return FALSE;
  }
Wim Taymans's avatar
Wim Taymans committed
975
976
no_element:
  {
977
    GST_DEBUG_OBJECT (src, "no rtpdec element found");
Wim Taymans's avatar
Wim Taymans committed
978
979
    return FALSE;
  }
980
start_session_failure:
981
  {
982
    GST_DEBUG_OBJECT (src, "could not start session");
983
984
    return FALSE;
  }
Wim Taymans's avatar
Wim Taymans committed
985
986
987
}

static gint
988
find_stream_by_channel (GstRTSPStream * stream, gconstpointer a)
Wim Taymans's avatar
Wim Taymans committed
989
990
991
{
  gint channel = GPOINTER_TO_INT (a);

992
  if (stream->channel[0] == channel || stream->channel[1] == channel)
Wim Taymans's avatar
Wim Taymans committed
993
994
995
996
997
    return 0;

  return -1;
}

998
999
1000
1001
1002
1003
1004
1005
1006
1007
1008
1009
1010
1011
1012
1013
1014
1015
1016
1017
1018
1019
1020
1021
1022
1023
1024
1025
1026
1027
1028
1029
1030
1031
static GstFlowReturn
gst_rtspsrc_combine_flows (GstRTSPSrc * src, GstRTSPStream * stream,
    GstFlowReturn ret)
{
  GList *streams;

  /* store the value */
  stream->last_ret = ret;

  /* if it's success we can return the value right away */
  if (GST_FLOW_IS_SUCCESS (ret))
    goto done;

  /* any other error that is not-linked can be returned right
   * away */
  if (ret != GST_FLOW_NOT_LINKED)
    goto done;

  /* only return NOT_LINKED if all other pads returned NOT_LINKED */
  for (streams = src->streams; streams; streams = g_list_next (streams)) {
    GstRTSPStream *ostream = (GstRTSPStream *) streams->data;

    ret = ostream->last_ret;
    /* some other return value (must be SUCCESS but we can return
     * other values as well) */
    if (ret != GST_FLOW_NOT_LINKED)
      goto done;
  }
  /* if we get here, all other pads were unlinked and we return
   * NOT_LINKED then */
done:
  return ret;
}

1032
1033
1034
1035
1036
1037
1038
1039
static void
gst_rtspsrc_push_event (GstRTSPSrc * src, GstEvent * event)
{
  GList *streams;

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

1040
1041
1042
1043
    /* only pads that have a connection to the outside world */
    if (ostream->srcpad == NULL)
      continue;

1044
1045
1046
1047
1048
1049
1050
1051
1052
1053
1054
1055
1056
1057
1058
    if (ostream->channelpad[0]) {
      gst_event_ref (event);
      if (GST_PAD_IS_SRC (ostream->channelpad[0]))
        gst_pad_push_event (ostream->channelpad[0], event);
      else
        gst_pad_send_event (ostream->channelpad[0], event);
    }

    if (ostream->channelpad[1]) {
      gst_event_ref (event);
      if (GST_PAD_IS_SRC (ostream->channelpad[1]))
        gst_pad_push_event (ostream->channelpad[1], event);
      else
        gst_pad_send_event (ostream->channelpad[1], event);
    }
1059
1060
1061
1062
  }
  gst_event_unref (event);
}

Wim Taymans's avatar
Wim Taymans committed
1063
static void
1064
gst_rtspsrc_loop_interleaved (GstRTSPSrc * src)
Wim Taymans's avatar
Wim Taymans committed
1065
1066
1067
1068
1069
1070
{
  RTSPMessage response = { 0 };
  RTSPResult res;
  gint channel;
  GList *lstream;
  GstRTSPStream *stream;
1071
  GstPad *outpad = NULL;
Wim Taymans's avatar
Wim Taymans committed
1072
  guint8 *data;
1073
  guint size;
1074
1075
  GstFlowReturn ret = GST_FLOW_OK;
  GstCaps *caps = NULL;
1076
  GstBuffer *buf;
Wim Taymans's avatar
Wim Taymans committed
1077
1078

  do {
1079
    GST_DEBUG_OBJECT (src, "doing receive");
Wim Taymans's avatar
Wim Taymans committed
1080
1081
    if ((res = rtsp_connection_receive (src->connection, &response)) < 0)
      goto receive_error;
1082

1083
    GST_DEBUG_OBJECT (src, "got packet type %d", response.type);
Wim Taymans's avatar
Wim Taymans committed
1084
1085
1086
1087
1088
1089
  }
  while (response.type != RTSP_MESSAGE_DATA);

  channel = response.type_data.data.channel;

  lstream = g_list_find_custom (src->streams, GINT_TO_POINTER (channel),
1090
      (GCompareFunc) find_stream_by_channel);
Wim Taymans's avatar
Wim Taymans committed
1091
1092
1093
1094
  if (!lstream)
    goto unknown_stream;

  stream = (GstRTSPStream *) lstream->data;
1095
1096
  if (channel == stream->channel[0]) {
    outpad = stream->channelpad[0];
1097
    caps = stream->caps;
1098
1099
  } else if (channel == stream->channel[1]) {
    outpad = stream->channelpad[1];
1100
  }
Wim Taymans's avatar
Wim Taymans committed
1101

1102
  /* take a look at the body to figure out what we have */
Wim Taymans's avatar
Wim Taymans committed
1103
  rtsp_message_get_body (&response, &data, &size);
1104
1105
  if (size < 2)
    goto invalid_length;
Wim Taymans's avatar
Wim Taymans committed
1106
1107
1108

  /* channels are not correct on some servers, do extra check */
  if (data[1] >= 200 && data[1] <= 204) {
1109
    /* hmm RTCP message switch to the RTCP pad of the same stream. */
1110
    outpad = stream->channelpad[1];
Wim Taymans's avatar
Wim Taymans committed
1111
1112
  }

1113
1114
1115
1116
  /* we have no clue what this is, just ignore then. */
  if (outpad == NULL)
    goto unknown_stream;

Wim Taymans's avatar
Wim Taymans committed
1117
  /* and chain buffer to internal element */
1118
  rtsp_message_steal_body (&response, &data, &size);
Wim Taymans's avatar
Wim Taymans committed
1119

1120
1121
  /* strip the trailing \0 */
  size -= 1;
1122

1123
1124
1125
1126
  buf = gst_buffer_new ();
  GST_BUFFER_DATA (buf) = data;
  GST_BUFFER_MALLOCDATA (buf) = data;
  GST_BUFFER_SIZE (buf) = size;
1127

1128
1129
  /* don't need message anymore */
  rtsp_message_unset (&response);
Wim Taymans's avatar
Wim Taymans committed
1130

1131
1132
  if (caps)
    gst_buffer_set_caps (buf, caps);
Wim Taymans's avatar
Wim Taymans committed
1133

1134
1135
  GST_DEBUG_OBJECT (src, "pushing data of size %d on channel %d", size,
      channel);
1136

1137
  /* chain to the peer pad */
1138
1139
1140
1141
  if (GST_PAD_IS_SINK (outpad))
    ret = gst_pad_chain (outpad, buf);
  else
    ret = gst_pad_push (outpad, buf);
1142
1143
1144
1145
1146

  /* combine all stream flows */
  ret = gst_rtspsrc_combine_flows (src, stream, ret);
  if (ret != GST_FLOW_OK)
    goto need_pause;
Wim Taymans's avatar
Wim Taymans committed
1147
1148
1149

  return;

1150
1151
1152
1153
  /* ERRORS */
unknown_stream:
  {
    GST_DEBUG_OBJECT (src, "unknown stream on channel %d, ignored", channel);
1154
    rtsp_message_unset (&response);
1155
1156
    return;
  }
Wim Taymans's avatar
Wim Taymans committed
1157
1158
receive_error:
  {
1159
1160
    gchar *str = rtsp_strresult (res);

1161
1162
    GST_ELEMENT_ERROR (src, RESOURCE, READ, (NULL),
        ("Could not receive message. (%s)", str));
1163
    g_free (str);
1164
1165
1166
    if (src->debug)
      rtsp_message_dump (&response);
    rtsp_message_unset (&response);
1167
    ret = GST_FLOW_UNEXPECTED;
Wim Taymans's avatar
Wim Taymans committed
1168
1169
    goto need_pause;
  }
1170
1171
invalid_length:
  {
1172
1173
    GST_ELEMENT_WARNING (src, RESOURCE, READ, (NULL),
        ("Short message received."));
1174
    rtsp_message_unset (&response);
1175
1176
    return;
  }
Wim Taymans's avatar
Wim Taymans committed
1177
1178
need_pause:
  {
1179
1180
1181
1182
    const gchar *reason = gst_flow_get_name (ret);

    GST_DEBUG_OBJECT (src, "pausing task, reason %s", reason);
    src->running = FALSE;
Wim Taymans's avatar
Wim Taymans committed
1183
    gst_task_pause (src->task);
1184
1185
1186
1187
1188
1189
1190
1191
1192
1193
1194
1195
1196
1197
1198
1199
1200
1201
1202
    if (GST_FLOW_IS_FATAL (ret) || ret == GST_FLOW_NOT_LINKED) {
      if (ret == GST_FLOW_UNEXPECTED) {
        /* perform EOS logic */
        if (src->segment.flags & GST_SEEK_FLAG_SEGMENT) {
          gst_element_post_message (GST_ELEMENT_CAST (src),
              gst_message_new_segment_done (GST_OBJECT_CAST (src),
                  src->segment.format, src->segment.last_stop));
        } else {
          gst_rtspsrc_push_event (src, gst_event_new_eos ());
        }
      } else {
        /* for fatal errors we post an error message, post the error
         * first so the app knows about the error first. */
        GST_ELEMENT_ERROR (src, STREAM, FAILED,
            ("Internal data flow error."),
            ("streaming task paused, reason %s (%d)", reason, ret));
        gst_rtspsrc_push_event (src, gst_event_new_eos ());
      }
    }
Wim Taymans's avatar
Wim Taymans committed
1203
1204
1205
1206
    return;
  }
}

1207
1208
1209
1210
1211
1212
1213
1214
1215
1216
1217
1218
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
1254
1255
static void
gst_rtspsrc_loop_udp (GstRTSPSrc * src)
{
  GST_OBJECT_LOCK (src);
  if (src->loop_cmd == CMD_STOP)
    goto stopping;
  while (src->loop_cmd == CMD_WAIT) {
    GST_DEBUG_OBJECT (src, "waiting");
    GST_RTSP_LOOP_WAIT (src);
    GST_DEBUG_OBJECT (src, "waiting done");
    if (src->loop_cmd == CMD_STOP)
      goto stopping;
  }
  if (src->loop_cmd == CMD_RECONNECT) {
    /* FIXME, when we get here we have to reconnect using tcp */
    src->loop_cmd = CMD_WAIT;
  }
  GST_OBJECT_UNLOCK (src);

  return;

  /* ERRORS */
stopping:
  {
    GST_OBJECT_UNLOCK (src);
    src->running = FALSE;
    gst_task_pause (src->task);
    return;
  }
}

static void
gst_rtspsrc_loop_send_cmd (GstRTSPSrc * src, gint cmd)
{
  GST_OBJECT_LOCK (src);
  src->loop_cmd = cmd;
  GST_RTSP_LOOP_SIGNAL (src);
  GST_OBJECT_UNLOCK (src);
}

static void
gst_rtspsrc_loop (GstRTSPSrc * src)
{
  if (src->interleaved)
    gst_rtspsrc_loop_interleaved (src);
  else
    gst_rtspsrc_loop_udp (src);
}

1256
1257
1258
1259
1260
1261
1262
1263
1264
1265
1266
1267
1268
1269
1270
1271
1272
/**
 * gst_rtspsrc_send:
 * @src: the rtsp source
 * @request: must point to a valid request
 * @response: must point to an empty #RTSPMessage
 *
 * send @request and retrieve the response in @response. optionally @code can be
 * non-NULL in which case it will contain the status code of the response.
 *
 * If This function returns TRUE, @response will contain a valid response
 * message that should be cleaned with rtsp_message_unset() after usage. 
 *
 * If @code is NULL, this function will return FALSE (with an invalid @response
 * message) if the response code was not 200 (OK).
 *
 * Returns: TRUE if the processing was successful.
 */
1273
gboolean
Wim Taymans's avatar
Wim Taymans committed
1274
gst_rtspsrc_send (GstRTSPSrc * src, RTSPMessage * request,
1275
    RTSPMessage * response, RTSPStatusCode * code)
Wim Taymans's avatar
Wim Taymans committed
1276
1277
{
  RTSPResult res;
1278
  RTSPStatusCode thecode;
1279
1280
1281
1282
  gchar *content_base = NULL;

  if (src->extension && src->extension->before_send)
    src->extension->before_send (src->extension, request);
Wim Taymans's avatar
Wim Taymans committed
1283

1284
  if (src->debug)
Wim Taymans's avatar
Wim Taymans committed
1285
    rtsp_message_dump (request);
1286

Wim Taymans's avatar
Wim Taymans committed
1287
1288
1289
1290
1291
  if ((res = rtsp_connection_send (src->connection, request)) < 0)
    goto send_error;

  if ((res = rtsp_connection_receive (src->connection, response)) < 0)
    goto receive_error;
1292

1293
  if (src->debug)
Wim Taymans's avatar
Wim Taymans committed
1294
    rtsp_message_dump (response);