gstrtpbasedepayload.c 20.6 KB
Newer Older
1
/* GStreamer
Stefan Kost's avatar
Stefan Kost committed
2
 * Copyright (C) <2005> Philippe Khalaf <burger@speedy.org>
3
 * Copyright (C) <2005> Nokia Corporation <kai.vehmanen@nokia.com>
4 5 6 7 8 9 10 11 12 13 14 15 16
 *
 * 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
Tim-Philipp Müller's avatar
Tim-Philipp Müller committed
17 18
 * Free Software Foundation, Inc., 51 Franklin St, Fifth Floor,
 * Boston, MA 02110-1301, USA.
19 20
 */

21
/**
22
 * SECTION:gstrtpbasedepayload
23 24 25 26 27
 * @short_description: Base class for RTP depayloader
 *
 * Provides a base class for RTP depayloaders
 */

28
#include "gstrtpbasedepayload.h"
29

30 31
GST_DEBUG_CATEGORY_STATIC (rtpbasedepayload_debug);
#define GST_CAT_DEFAULT (rtpbasedepayload_debug)
32

Wim Taymans's avatar
Wim Taymans committed
33 34
#define GST_RTP_BASE_DEPAYLOAD_GET_PRIVATE(obj)  \
   (G_TYPE_INSTANCE_GET_PRIVATE ((obj), GST_TYPE_RTP_BASE_DEPAYLOAD, GstRTPBaseDepayloadPrivate))
35

Wim Taymans's avatar
Wim Taymans committed
36
struct _GstRTPBaseDepayloadPrivate
37
{
38 39 40 41
  GstClockTime npt_start;
  GstClockTime npt_stop;
  gdouble play_speed;
  gdouble play_scale;
42

43
  gboolean discont;
Wim Taymans's avatar
Wim Taymans committed
44 45
  GstClockTime pts;
  GstClockTime dts;
46
  GstClockTime duration;
47 48

  guint32 next_seqnum;
49 50

  gboolean negotiated;
51 52

  GstCaps *last_caps;
53 54
};

55 56 57 58 59 60 61 62 63
/* Filter signals and args */
enum
{
  /* FILL ME */
  LAST_SIGNAL
};

enum
{
64
  PROP_0,
65
  PROP_LAST
66 67
};

Wim Taymans's avatar
Wim Taymans committed
68 69
static void gst_rtp_base_depayload_finalize (GObject * object);
static void gst_rtp_base_depayload_set_property (GObject * object,
70
    guint prop_id, const GValue * value, GParamSpec * pspec);
Wim Taymans's avatar
Wim Taymans committed
71
static void gst_rtp_base_depayload_get_property (GObject * object,
72 73
    guint prop_id, GValue * value, GParamSpec * pspec);

Wim Taymans's avatar
Wim Taymans committed
74
static GstFlowReturn gst_rtp_base_depayload_chain (GstPad * pad,
Wim Taymans's avatar
Wim Taymans committed
75
    GstObject * parent, GstBuffer * in);
Wim Taymans's avatar
Wim Taymans committed
76
static gboolean gst_rtp_base_depayload_handle_sink_event (GstPad * pad,
Wim Taymans's avatar
Wim Taymans committed
77
    GstObject * parent, GstEvent * event);
78

Wim Taymans's avatar
Wim Taymans committed
79
static GstStateChangeReturn gst_rtp_base_depayload_change_state (GstElement *
80
    element, GstStateChange transition);
81

Wim Taymans's avatar
Wim Taymans committed
82
static gboolean gst_rtp_base_depayload_packet_lost (GstRTPBaseDepayload *
83
    filter, GstEvent * event);
Wim Taymans's avatar
Wim Taymans committed
84
static gboolean gst_rtp_base_depayload_handle_event (GstRTPBaseDepayload *
85
    filter, GstEvent * event);
86

87
static GstElementClass *parent_class = NULL;
Wim Taymans's avatar
Wim Taymans committed
88
static void gst_rtp_base_depayload_class_init (GstRTPBaseDepayloadClass *
89
    klass);
90
static void gst_rtp_base_depayload_init (GstRTPBaseDepayload * rtpbasepayload,
Wim Taymans's avatar
Wim Taymans committed
91
    GstRTPBaseDepayloadClass * klass);
92 93

GType
Wim Taymans's avatar
Wim Taymans committed
94
gst_rtp_base_depayload_get_type (void)
95
{
Wim Taymans's avatar
Wim Taymans committed
96
  static GType rtp_base_depayload_type = 0;
97

Wim Taymans's avatar
Wim Taymans committed
98 99 100
  if (g_once_init_enter ((gsize *) & rtp_base_depayload_type)) {
    static const GTypeInfo rtp_base_depayload_info = {
      sizeof (GstRTPBaseDepayloadClass),
101 102
      NULL,
      NULL,
Wim Taymans's avatar
Wim Taymans committed
103
      (GClassInitFunc) gst_rtp_base_depayload_class_init,
104 105
      NULL,
      NULL,
Wim Taymans's avatar
Wim Taymans committed
106
      sizeof (GstRTPBaseDepayload),
107
      0,
Wim Taymans's avatar
Wim Taymans committed
108
      (GInstanceInitFunc) gst_rtp_base_depayload_init,
109 110
    };

Wim Taymans's avatar
Wim Taymans committed
111 112 113
    g_once_init_leave ((gsize *) & rtp_base_depayload_type,
        g_type_register_static (GST_TYPE_ELEMENT, "GstRTPBaseDepayload",
            &rtp_base_depayload_info, G_TYPE_FLAG_ABSTRACT));
114
  }
Wim Taymans's avatar
Wim Taymans committed
115
  return rtp_base_depayload_type;
116 117 118
}

static void
Wim Taymans's avatar
Wim Taymans committed
119
gst_rtp_base_depayload_class_init (GstRTPBaseDepayloadClass * klass)
120 121
{
  GObjectClass *gobject_class;
122
  GstElementClass *gstelement_class;
123 124

  gobject_class = G_OBJECT_CLASS (klass);
125
  gstelement_class = (GstElementClass *) klass;
126
  parent_class = g_type_class_peek_parent (klass);
127

Wim Taymans's avatar
Wim Taymans committed
128
  g_type_class_add_private (klass, sizeof (GstRTPBaseDepayloadPrivate));
129

Wim Taymans's avatar
Wim Taymans committed
130 131 132
  gobject_class->finalize = gst_rtp_base_depayload_finalize;
  gobject_class->set_property = gst_rtp_base_depayload_set_property;
  gobject_class->get_property = gst_rtp_base_depayload_get_property;
133

Wim Taymans's avatar
Wim Taymans committed
134
  gstelement_class->change_state = gst_rtp_base_depayload_change_state;
135

Wim Taymans's avatar
Wim Taymans committed
136 137
  klass->packet_lost = gst_rtp_base_depayload_packet_lost;
  klass->handle_event = gst_rtp_base_depayload_handle_event;
138

139
  GST_DEBUG_CATEGORY_INIT (rtpbasedepayload_debug, "rtpbasedepayload", 0,
140 141 142 143
      "Base class for RTP Depayloaders");
}

static void
Wim Taymans's avatar
Wim Taymans committed
144 145
gst_rtp_base_depayload_init (GstRTPBaseDepayload * filter,
    GstRTPBaseDepayloadClass * klass)
146 147
{
  GstPadTemplate *pad_template;
Wim Taymans's avatar
Wim Taymans committed
148
  GstRTPBaseDepayloadPrivate *priv;
149

Wim Taymans's avatar
Wim Taymans committed
150
  priv = GST_RTP_BASE_DEPAYLOAD_GET_PRIVATE (filter);
151
  filter->priv = priv;
152

153
  GST_DEBUG_OBJECT (filter, "init");
154 155

  pad_template =
156
      gst_element_class_get_pad_template (GST_ELEMENT_CLASS (klass), "sink");
157 158
  g_return_if_fail (pad_template != NULL);
  filter->sinkpad = gst_pad_new_from_template (pad_template, "sink");
Wim Taymans's avatar
Wim Taymans committed
159
  gst_pad_set_chain_function (filter->sinkpad, gst_rtp_base_depayload_chain);
160
  gst_pad_set_event_function (filter->sinkpad,
Wim Taymans's avatar
Wim Taymans committed
161
      gst_rtp_base_depayload_handle_sink_event);
162 163 164
  gst_element_add_pad (GST_ELEMENT (filter), filter->sinkpad);

  pad_template =
165
      gst_element_class_get_pad_template (GST_ELEMENT_CLASS (klass), "src");
166 167
  g_return_if_fail (pad_template != NULL);
  filter->srcpad = gst_pad_new_from_template (pad_template, "src");
168
  gst_pad_use_fixed_caps (filter->srcpad);
169 170
  gst_element_add_pad (GST_ELEMENT (filter), filter->srcpad);

171
  gst_segment_init (&filter->segment, GST_FORMAT_UNDEFINED);
172 173 174
}

static void
Wim Taymans's avatar
Wim Taymans committed
175
gst_rtp_base_depayload_finalize (GObject * object)
176
{
177
  G_OBJECT_CLASS (parent_class)->finalize (object);
178 179 180
}

static gboolean
Wim Taymans's avatar
Wim Taymans committed
181
gst_rtp_base_depayload_setcaps (GstRTPBaseDepayload * filter, GstCaps * caps)
182
{
Wim Taymans's avatar
Wim Taymans committed
183 184
  GstRTPBaseDepayloadClass *bclass;
  GstRTPBaseDepayloadPrivate *priv;
185
  gboolean res;
186 187
  GstStructure *caps_struct;
  const GValue *value;
188

189
  priv = filter->priv;
190

Wim Taymans's avatar
Wim Taymans committed
191
  bclass = GST_RTP_BASE_DEPAYLOAD_GET_CLASS (filter);
192

193 194 195 196 197 198 199 200 201 202 203
  GST_DEBUG_OBJECT (filter, "Set caps %" GST_PTR_FORMAT, caps);

  if (priv->last_caps) {
    if (gst_caps_is_equal (priv->last_caps, caps)) {
      res = TRUE;
      goto caps_not_changed;
    } else {
      gst_caps_unref (priv->last_caps);
      priv->last_caps = NULL;
    }
  }
204 205 206

  caps_struct = gst_caps_get_structure (caps, 0);

207 208 209 210 211 212
  /* get other values for newsegment */
  value = gst_structure_get_value (caps_struct, "npt-start");
  if (value && G_VALUE_HOLDS_UINT64 (value))
    priv->npt_start = g_value_get_uint64 (value);
  else
    priv->npt_start = 0;
213
  GST_DEBUG_OBJECT (filter, "NPT start %" G_GUINT64_FORMAT, priv->npt_start);
214 215 216 217 218 219 220

  value = gst_structure_get_value (caps_struct, "npt-stop");
  if (value && G_VALUE_HOLDS_UINT64 (value))
    priv->npt_stop = g_value_get_uint64 (value);
  else
    priv->npt_stop = -1;

221
  GST_DEBUG_OBJECT (filter, "NPT stop %" G_GUINT64_FORMAT, priv->npt_stop);
222

223 224 225 226 227 228 229 230 231 232 233 234
  value = gst_structure_get_value (caps_struct, "play-speed");
  if (value && G_VALUE_HOLDS_DOUBLE (value))
    priv->play_speed = g_value_get_double (value);
  else
    priv->play_speed = 1.0;

  value = gst_structure_get_value (caps_struct, "play-scale");
  if (value && G_VALUE_HOLDS_DOUBLE (value))
    priv->play_scale = g_value_get_double (value);
  else
    priv->play_scale = 1.0;

235
  if (bclass->set_caps) {
236
    res = bclass->set_caps (filter, caps);
237 238 239 240 241
    if (!res) {
      GST_WARNING_OBJECT (filter, "Subclass rejected caps %" GST_PTR_FORMAT,
          caps);
    }
  } else {
242
    res = TRUE;
243
  }
244

245 246
  priv->negotiated = res;

247 248 249
  if (priv->negotiated)
    priv->last_caps = gst_caps_ref (caps);

250
  return res;
251 252 253 254 255 256

caps_not_changed:
  {
    GST_DEBUG_OBJECT (filter, "Caps did not change");
    return res;
  }
257 258 259
}

static GstFlowReturn
Wim Taymans's avatar
Wim Taymans committed
260
gst_rtp_base_depayload_chain (GstPad * pad, GstObject * parent, GstBuffer * in)
261
{
Wim Taymans's avatar
Wim Taymans committed
262 263 264
  GstRTPBaseDepayload *filter;
  GstRTPBaseDepayloadPrivate *priv;
  GstRTPBaseDepayloadClass *bclass;
265
  GstFlowReturn ret = GST_FLOW_OK;
266
  GstBuffer *out_buf;
Wim Taymans's avatar
Wim Taymans committed
267
  GstClockTime pts, dts;
268
  guint16 seqnum;
269
  guint32 rtptime;
270
  gboolean discont, buf_discont;
271
  gint gap;
272
  GstRTPBuffer rtp = { NULL };
273

Wim Taymans's avatar
Wim Taymans committed
274
  filter = GST_RTP_BASE_DEPAYLOAD (parent);
275 276 277 278 279
  priv = filter->priv;

  /* we must have a setcaps first */
  if (G_UNLIKELY (!priv->negotiated))
    goto not_negotiated;
280

281
  if (G_UNLIKELY (!gst_rtp_buffer_map (in, GST_MAP_READ, &rtp)))
282 283
    goto invalid_buffer;

284
  buf_discont = GST_BUFFER_IS_DISCONT (in);
285

Wim Taymans's avatar
Wim Taymans committed
286 287
  pts = GST_BUFFER_PTS (in);
  dts = GST_BUFFER_DTS (in);
288 289
  /* convert to running_time and save the timestamp, this is the timestamp
   * we put on outgoing buffers. */
Wim Taymans's avatar
Wim Taymans committed
290 291 292 293
  pts = gst_segment_to_running_time (&filter->segment, GST_FORMAT_TIME, pts);
  dts = gst_segment_to_running_time (&filter->segment, GST_FORMAT_TIME, dts);
  priv->pts = pts;
  priv->dts = dts;
294
  priv->duration = GST_BUFFER_DURATION (in);
295

Wim Taymans's avatar
Wim Taymans committed
296 297 298 299
  seqnum = gst_rtp_buffer_get_seq (&rtp);
  rtptime = gst_rtp_buffer_get_timestamp (&rtp);
  gst_rtp_buffer_unmap (&rtp);

300
  discont = buf_discont;
301

Wim Taymans's avatar
Wim Taymans committed
302
  GST_LOG_OBJECT (filter, "discont %d, seqnum %u, rtptime %u, pts %"
303
      GST_TIME_FORMAT ", dts %" GST_TIME_FORMAT, buf_discont, seqnum, rtptime,
Wim Taymans's avatar
Wim Taymans committed
304
      GST_TIME_ARGS (pts), GST_TIME_ARGS (dts));
305 306 307 308

  /* Check seqnum. This is a very simple check that makes sure that the seqnums
   * are striclty increasing, dropping anything that is out of the ordinary. We
   * can only do this when the next_seqnum is known. */
309
  if (G_LIKELY (priv->next_seqnum != -1)) {
310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336
    gap = gst_rtp_buffer_compare_seqnum (seqnum, priv->next_seqnum);

    /* if we have no gap, all is fine */
    if (G_UNLIKELY (gap != 0)) {
      GST_LOG_OBJECT (filter, "got packet %u, expected %u, gap %d", seqnum,
          priv->next_seqnum, gap);
      if (gap < 0) {
        /* seqnum > next_seqnum, we are missing some packets, this is always a
         * DISCONT. */
        GST_LOG_OBJECT (filter, "%d missing packets", gap);
        discont = TRUE;
      } else {
        /* seqnum < next_seqnum, we have seen this packet before or the sender
         * could be restarted. If the packet is not too old, we throw it away as
         * a duplicate, otherwise we mark discont and continue. 100 misordered
         * packets is a good threshold. See also RFC 4737. */
        if (gap < 100)
          goto dropping;

        GST_LOG_OBJECT (filter,
            "%d > 100, packet too old, sender likely restarted", gap);
        discont = TRUE;
      }
    }
  }
  priv->next_seqnum = (seqnum + 1) & 0xffff;

337
  if (G_UNLIKELY (discont)) {
338
    priv->discont = TRUE;
339 340 341 342 343 344 345
    if (!buf_discont) {
      /* we detected a seqnum discont but the buffer was not flagged with a discont,
       * set the discont flag so that the subclass can throw away old data. */
      GST_LOG_OBJECT (filter, "mark DISCONT on input buffer");
      in = gst_buffer_make_writable (in);
      GST_BUFFER_FLAG_SET (in, GST_BUFFER_FLAG_DISCONT);
    }
346 347
  }

Wim Taymans's avatar
Wim Taymans committed
348
  bclass = GST_RTP_BASE_DEPAYLOAD_GET_CLASS (filter);
349

350 351 352
  if (G_UNLIKELY (bclass->process == NULL))
    goto no_process;

353 354 355
  /* let's send it out to processing */
  out_buf = bclass->process (filter, in);
  if (out_buf) {
Wim Taymans's avatar
Wim Taymans committed
356
    ret = gst_rtp_base_depayload_push (filter, out_buf);
357
  }
358 359
  gst_buffer_unref (in);

360
  return ret;
361 362

  /* ERRORS */
363 364 365
not_negotiated:
  {
    /* this is not fatal but should be filtered earlier */
Wim Taymans's avatar
Wim Taymans committed
366 367 368 369 370 371 372 373
    GST_ELEMENT_ERROR (filter, CORE, NEGOTIATION,
        ("No RTP format was negotiated."),
        ("Input buffers need to have RTP caps set on them. This is usually "
            "achieved by setting the 'caps' property of the upstream source "
            "element (often udpsrc or appsrc), or by putting a capsfilter "
            "element before the depayloader and setting the 'caps' property "
            "on that. Also see http://cgit.freedesktop.org/gstreamer/"
            "gst-plugins-good/tree/gst/rtp/README"));
374 375 376
    gst_buffer_unref (in);
    return GST_FLOW_NOT_NEGOTIATED;
  }
377 378 379 380 381 382 383 384
invalid_buffer:
  {
    /* this is not fatal but should be filtered earlier */
    GST_ELEMENT_WARNING (filter, STREAM, DECODE, (NULL),
        ("Received invalid RTP payload, dropping"));
    gst_buffer_unref (in);
    return GST_FLOW_OK;
  }
385 386 387 388 389 390 391 392 393 394 395 396 397 398
dropping:
  {
    GST_WARNING_OBJECT (filter, "%d <= 100, dropping old packet", gap);
    gst_buffer_unref (in);
    return GST_FLOW_OK;
  }
no_process:
  {
    /* this is not fatal but should be filtered earlier */
    GST_ELEMENT_ERROR (filter, STREAM, NOT_IMPLEMENTED, (NULL),
        ("The subclass does not have a process method"));
    gst_buffer_unref (in);
    return GST_FLOW_ERROR;
  }
399 400
}

401
static gboolean
Wim Taymans's avatar
Wim Taymans committed
402
gst_rtp_base_depayload_handle_event (GstRTPBaseDepayload * filter,
403
    GstEvent * event)
404 405
{
  gboolean res = TRUE;
406
  gboolean forward = TRUE;
407 408

  switch (GST_EVENT_TYPE (event)) {
409 410 411
    case GST_EVENT_FLUSH_STOP:
      gst_segment_init (&filter->segment, GST_FORMAT_UNDEFINED);
      filter->need_newsegment = TRUE;
412
      filter->priv->next_seqnum = -1;
413
      break;
414 415 416 417 418 419
    case GST_EVENT_CAPS:
    {
      GstCaps *caps;

      gst_event_parse_caps (event, &caps);

Wim Taymans's avatar
Wim Taymans committed
420
      res = gst_rtp_base_depayload_setcaps (filter, caps);
421 422 423
      forward = FALSE;
      break;
    }
Wim Taymans's avatar
Wim Taymans committed
424
    case GST_EVENT_SEGMENT:
425
    {
426
      gst_event_copy_segment (event, &filter->segment);
427 428
      /* don't pass the event downstream, we generate our own segment including
       * the NTP time and other things we receive in caps */
429
      forward = FALSE;
430
      break;
431
    }
432 433
    case GST_EVENT_CUSTOM_DOWNSTREAM:
    {
Wim Taymans's avatar
Wim Taymans committed
434
      GstRTPBaseDepayloadClass *bclass;
435

Wim Taymans's avatar
Wim Taymans committed
436
      bclass = GST_RTP_BASE_DEPAYLOAD_GET_CLASS (filter);
437 438 439 440

      if (gst_event_has_name (event, "GstRTPPacketLost")) {
        /* we get this event from the jitterbuffer when it considers a packet as
         * being lost. We send it to our packet_lost vmethod. The default
441
         * implementation will make time progress by pushing out a GAP event.
442
         * Subclasses can override and do one of the following:
443 444 445 446 447 448 449 450
         *  - Adjust timestamp/duration to something more accurate before
         *    calling the parent (default) packet_lost method.
         *  - do some more advanced error concealing on the already received
         *    (fragmented) packets.
         *  - ignore the packet lost.
         */
        if (bclass->packet_lost)
          res = bclass->packet_lost (filter, event);
451
        forward = FALSE;
452 453 454
      }
      break;
    }
455 456 457
    default:
      break;
  }
458 459 460 461 462 463

  if (forward)
    res = gst_pad_push_event (filter->srcpad, event);
  else
    gst_event_unref (event);

464 465 466
  return res;
}

467
static gboolean
Wim Taymans's avatar
Wim Taymans committed
468 469
gst_rtp_base_depayload_handle_sink_event (GstPad * pad, GstObject * parent,
    GstEvent * event)
470 471
{
  gboolean res = FALSE;
Wim Taymans's avatar
Wim Taymans committed
472 473
  GstRTPBaseDepayload *filter;
  GstRTPBaseDepayloadClass *bclass;
474

Wim Taymans's avatar
Wim Taymans committed
475
  filter = GST_RTP_BASE_DEPAYLOAD (parent);
Wim Taymans's avatar
Wim Taymans committed
476
  bclass = GST_RTP_BASE_DEPAYLOAD_GET_CLASS (filter);
477 478
  if (bclass->handle_event)
    res = bclass->handle_event (filter, event);
479 480
  else
    gst_event_unref (event);
481 482 483 484

  return res;
}

485
static GstEvent *
486
create_segment_event (GstRTPBaseDepayload * filter, GstClockTime position)
487 488 489
{
  GstEvent *event;
  GstClockTime stop;
Wim Taymans's avatar
Wim Taymans committed
490
  GstRTPBaseDepayloadPrivate *priv;
Wim Taymans's avatar
Wim Taymans committed
491
  GstSegment segment;
492 493 494 495 496 497 498 499

  priv = filter->priv;

  if (priv->npt_stop != -1)
    stop = priv->npt_stop - priv->npt_start;
  else
    stop = -1;

Wim Taymans's avatar
Wim Taymans committed
500 501 502 503 504 505 506 507 508
  gst_segment_init (&segment, GST_FORMAT_TIME);
  segment.rate = priv->play_speed;
  segment.applied_rate = priv->play_scale;
  segment.start = 0;
  segment.stop = stop;
  segment.time = priv->npt_start;
  segment.position = position;

  event = gst_event_new_segment (&segment);
509 510 511 512

  return event;
}

513
typedef struct
514
{
Wim Taymans's avatar
Wim Taymans committed
515 516
  GstRTPBaseDepayload *depayload;
  GstRTPBaseDepayloadClass *bclass;
517
} HeaderData;
518

Wim Taymans's avatar
Wim Taymans committed
519 520
static gboolean
set_headers (GstBuffer ** buffer, guint idx, HeaderData * data)
521
{
Wim Taymans's avatar
Wim Taymans committed
522 523
  GstRTPBaseDepayload *depayload = data->depayload;
  GstRTPBaseDepayloadPrivate *priv = depayload->priv;
Wim Taymans's avatar
Wim Taymans committed
524
  GstClockTime pts, dts, duration;
525

Wim Taymans's avatar
Wim Taymans committed
526
  *buffer = gst_buffer_make_writable (*buffer);
527

Wim Taymans's avatar
Wim Taymans committed
528 529 530 531 532 533 534 535 536 537 538 539
  pts = GST_BUFFER_PTS (*buffer);
  dts = GST_BUFFER_DTS (*buffer);
  duration = GST_BUFFER_DURATION (*buffer);

  /* apply last incomming timestamp and duration to outgoing buffer if
   * not otherwise set. */
  if (!GST_CLOCK_TIME_IS_VALID (pts))
    GST_BUFFER_PTS (*buffer) = priv->pts;
  if (!GST_CLOCK_TIME_IS_VALID (dts))
    GST_BUFFER_DTS (*buffer) = priv->dts;
  if (!GST_CLOCK_TIME_IS_VALID (duration))
    GST_BUFFER_DURATION (*buffer) = priv->duration;
540

541 542 543 544 545
  if (G_UNLIKELY (depayload->priv->discont)) {
    GST_LOG_OBJECT (depayload, "Marking DISCONT on output buffer");
    GST_BUFFER_FLAG_SET (*buffer, GST_BUFFER_FLAG_DISCONT);
    depayload->priv->discont = FALSE;
  }
546

Wim Taymans's avatar
Wim Taymans committed
547 548 549 550 551
  /* make sure we only set the timestamp on the first packet */
  priv->pts = GST_CLOCK_TIME_NONE;
  priv->dts = GST_CLOCK_TIME_NONE;
  priv->duration = GST_CLOCK_TIME_NONE;

Wim Taymans's avatar
Wim Taymans committed
552
  return TRUE;
553 554 555
}

static GstFlowReturn
Wim Taymans's avatar
Wim Taymans committed
556
gst_rtp_base_depayload_prepare_push (GstRTPBaseDepayload * filter,
Wim Taymans's avatar
Wim Taymans committed
557
    gboolean is_list, gpointer obj)
558 559 560 561
{
  HeaderData data;

  data.depayload = filter;
Wim Taymans's avatar
Wim Taymans committed
562
  data.bclass = GST_RTP_BASE_DEPAYLOAD_GET_CLASS (filter);
563 564

  if (is_list) {
Wim Taymans's avatar
Wim Taymans committed
565 566
    GstBufferList **blist = obj;
    gst_buffer_list_foreach (*blist, (GstBufferListFunc) set_headers, &data);
567
  } else {
Wim Taymans's avatar
Wim Taymans committed
568
    GstBuffer **buf = obj;
Wim Taymans's avatar
Wim Taymans committed
569
    set_headers (buf, 0, &data);
570
  }
571

572 573 574 575
  /* if this is the first buffer send a NEWSEGMENT */
  if (G_UNLIKELY (filter->need_newsegment)) {
    GstEvent *event;

576
    event = create_segment_event (filter, 0);
577 578 579 580 581 582 583

    gst_pad_push_event (filter->srcpad, event);

    filter->need_newsegment = FALSE;
    GST_DEBUG_OBJECT (filter, "Pushed newsegment event on this first buffer");
  }

584
  return GST_FLOW_OK;
585 586 587
}

/**
Wim Taymans's avatar
Wim Taymans committed
588 589
 * gst_rtp_base_depayload_push:
 * @filter: a #GstRTPBaseDepayload
590 591 592 593 594
 * @out_buf: a #GstBuffer
 *
 * Push @out_buf to the peer of @filter. This function takes ownership of
 * @out_buf.
 *
Wim Taymans's avatar
Wim Taymans committed
595 596
 * This function will by default apply the last incomming timestamp on
 * the outgoing buffer when it didn't have a timestamp already.
597
 *
598
 * Returns: a #GstFlowReturn.
599 600
 */
GstFlowReturn
Wim Taymans's avatar
Wim Taymans committed
601
gst_rtp_base_depayload_push (GstRTPBaseDepayload * filter, GstBuffer * out_buf)
602
{
603 604
  GstFlowReturn res;

Wim Taymans's avatar
Wim Taymans committed
605
  res = gst_rtp_base_depayload_prepare_push (filter, FALSE, &out_buf);
606 607 608 609 610 611 612 613 614 615

  if (G_LIKELY (res == GST_FLOW_OK))
    res = gst_pad_push (filter->srcpad, out_buf);
  else
    gst_buffer_unref (out_buf);

  return res;
}

/**
Wim Taymans's avatar
Wim Taymans committed
616 617
 * gst_rtp_base_depayload_push_list:
 * @filter: a #GstRTPBaseDepayload
618 619 620 621 622 623 624 625
 * @out_list: a #GstBufferList
 *
 * Push @out_list to the peer of @filter. This function takes ownership of
 * @out_list.
 *
 * Returns: a #GstFlowReturn.
 */
GstFlowReturn
Wim Taymans's avatar
Wim Taymans committed
626
gst_rtp_base_depayload_push_list (GstRTPBaseDepayload * filter,
627 628 629 630
    GstBufferList * out_list)
{
  GstFlowReturn res;

Wim Taymans's avatar
Wim Taymans committed
631
  res = gst_rtp_base_depayload_prepare_push (filter, TRUE, &out_list);
632 633 634 635 636 637 638

  if (G_LIKELY (res == GST_FLOW_OK))
    res = gst_pad_push_list (filter->srcpad, out_list);
  else
    gst_buffer_list_unref (out_list);

  return res;
639 640
}

641
/* convert the PacketLost event from a jitterbuffer to a GAP event.
642 643
 * subclasses can override this.  */
static gboolean
Wim Taymans's avatar
Wim Taymans committed
644
gst_rtp_base_depayload_packet_lost (GstRTPBaseDepayload * filter,
645 646
    GstEvent * event)
{
647
  GstClockTime timestamp, duration;
648 649 650 651 652 653 654 655 656 657 658 659
  GstEvent *sevent;
  const GstStructure *s;

  s = gst_event_get_structure (event);

  /* first start by parsing the timestamp and duration */
  timestamp = -1;
  duration = -1;

  gst_structure_get_clock_time (s, "timestamp", &timestamp);
  gst_structure_get_clock_time (s, "duration", &duration);

660 661
  /* send GAP event */
  sevent = gst_event_new_gap (timestamp, duration);
662 663 664 665

  return gst_pad_push_event (filter->srcpad, sevent);
}

666
static GstStateChangeReturn
Wim Taymans's avatar
Wim Taymans committed
667
gst_rtp_base_depayload_change_state (GstElement * element,
668
    GstStateChange transition)
669
{
Wim Taymans's avatar
Wim Taymans committed
670 671
  GstRTPBaseDepayload *filter;
  GstRTPBaseDepayloadPrivate *priv;
672
  GstStateChangeReturn ret;
673

Wim Taymans's avatar
Wim Taymans committed
674
  filter = GST_RTP_BASE_DEPAYLOAD (element);
675
  priv = filter->priv;
676 677

  switch (transition) {
678
    case GST_STATE_CHANGE_NULL_TO_READY:
679
      break;
680
    case GST_STATE_CHANGE_READY_TO_PAUSED:
681
      filter->need_newsegment = TRUE;
682 683 684 685
      priv->npt_start = 0;
      priv->npt_stop = -1;
      priv->play_speed = 1.0;
      priv->play_scale = 1.0;
686 687
      priv->next_seqnum = -1;
      priv->negotiated = FALSE;
688
      priv->discont = FALSE;
689
      break;
690
    case GST_STATE_CHANGE_PAUSED_TO_PLAYING:
691 692 693 694 695
      break;
    default:
      break;
  }

696 697
  ret = GST_ELEMENT_CLASS (parent_class)->change_state (element, transition);

698
  switch (transition) {
699
    case GST_STATE_CHANGE_PLAYING_TO_PAUSED:
700
      break;
701
    case GST_STATE_CHANGE_PAUSED_TO_READY:
702
      gst_caps_replace (&priv->last_caps, NULL);
703
      break;
704
    case GST_STATE_CHANGE_READY_TO_NULL:
705
      break;
706 707
    default:
      break;
708
  }
709
  return ret;
710 711
}

712
static void
Wim Taymans's avatar
Wim Taymans committed
713
gst_rtp_base_depayload_set_property (GObject * object, guint prop_id,
714 715 716 717 718 719 720 721 722 723
    const GValue * value, GParamSpec * pspec)
{
  switch (prop_id) {
    default:
      G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
      break;
  }
}

static void
Wim Taymans's avatar
Wim Taymans committed
724
gst_rtp_base_depayload_get_property (GObject * object, guint prop_id,
725 726 727 728 729 730 731 732
    GValue * value, GParamSpec * pspec)
{
  switch (prop_id) {
    default:
      G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
      break;
  }
}