gstrtpbasedepayload.c 20.2 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
};

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

enum
{
62
  PROP_0,
63
  PROP_LAST
64 65
};

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

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

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

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

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

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

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

Wim Taymans's avatar
Wim Taymans committed
109 110 111
    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));
112
  }
Wim Taymans's avatar
Wim Taymans committed
113
  return rtp_base_depayload_type;
114 115 116
}

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

  gobject_class = G_OBJECT_CLASS (klass);
123
  gstelement_class = (GstElementClass *) klass;
124
  parent_class = g_type_class_peek_parent (klass);
125

Wim Taymans's avatar
Wim Taymans committed
126
  g_type_class_add_private (klass, sizeof (GstRTPBaseDepayloadPrivate));
127

Wim Taymans's avatar
Wim Taymans committed
128 129 130
  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;
131

Wim Taymans's avatar
Wim Taymans committed
132
  gstelement_class->change_state = gst_rtp_base_depayload_change_state;
133

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

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

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

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

151
  GST_DEBUG_OBJECT (filter, "init");
152 153

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

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

169
  gst_segment_init (&filter->segment, GST_FORMAT_UNDEFINED);
170 171 172
}

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

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

187
  priv = filter->priv;
188

Wim Taymans's avatar
Wim Taymans committed
189
  bclass = GST_RTP_BASE_DEPAYLOAD_GET_CLASS (filter);
190

191 192 193 194
  GST_DEBUG_OBJECT (filter, "Set caps");

  caps_struct = gst_caps_get_structure (caps, 0);

195 196 197 198 199 200
  /* 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;
201
  GST_DEBUG_OBJECT (filter, "NPT start %" G_GUINT64_FORMAT, priv->npt_start);
202 203 204 205 206 207 208

  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;

209
  GST_DEBUG_OBJECT (filter, "NPT stop %" G_GUINT64_FORMAT, priv->npt_stop);
210

211 212 213 214 215 216 217 218 219 220 221 222
  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;

223
  if (bclass->set_caps) {
224
    res = bclass->set_caps (filter, caps);
225 226 227 228 229
    if (!res) {
      GST_WARNING_OBJECT (filter, "Subclass rejected caps %" GST_PTR_FORMAT,
          caps);
    }
  } else {
230
    res = TRUE;
231
  }
232

233 234
  priv->negotiated = res;

235
  return res;
236 237 238
}

static GstFlowReturn
Wim Taymans's avatar
Wim Taymans committed
239
gst_rtp_base_depayload_chain (GstPad * pad, GstObject * parent, GstBuffer * in)
240
{
Wim Taymans's avatar
Wim Taymans committed
241 242 243
  GstRTPBaseDepayload *filter;
  GstRTPBaseDepayloadPrivate *priv;
  GstRTPBaseDepayloadClass *bclass;
244
  GstFlowReturn ret = GST_FLOW_OK;
245
  GstBuffer *out_buf;
Wim Taymans's avatar
Wim Taymans committed
246
  GstClockTime pts, dts;
247
  guint16 seqnum;
248
  guint32 rtptime;
Bastien Nocera's avatar
Bastien Nocera committed
249
  gboolean discont;
250
  gint gap;
251
  GstRTPBuffer rtp = { NULL };
252

Wim Taymans's avatar
Wim Taymans committed
253
  filter = GST_RTP_BASE_DEPAYLOAD (parent);
254 255 256 257 258
  priv = filter->priv;

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

260
  if (G_UNLIKELY (!gst_rtp_buffer_map (in, GST_MAP_READ, &rtp)))
261 262
    goto invalid_buffer;

263 264
  if (!priv->discont)
    priv->discont = GST_BUFFER_IS_DISCONT (in);
265

Wim Taymans's avatar
Wim Taymans committed
266 267
  pts = GST_BUFFER_PTS (in);
  dts = GST_BUFFER_DTS (in);
268 269
  /* convert to running_time and save the timestamp, this is the timestamp
   * we put on outgoing buffers. */
Wim Taymans's avatar
Wim Taymans committed
270 271 272 273
  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;
274
  priv->duration = GST_BUFFER_DURATION (in);
275

Wim Taymans's avatar
Wim Taymans committed
276 277 278 279
  seqnum = gst_rtp_buffer_get_seq (&rtp);
  rtptime = gst_rtp_buffer_get_timestamp (&rtp);
  gst_rtp_buffer_unmap (&rtp);

280 281
  discont = FALSE;

Wim Taymans's avatar
Wim Taymans committed
282 283 284
  GST_LOG_OBJECT (filter, "discont %d, seqnum %u, rtptime %u, pts %"
      GST_TIME_FORMAT ", dts %" GST_TIME_FORMAT, priv->discont, seqnum, rtptime,
      GST_TIME_ARGS (pts), GST_TIME_ARGS (dts));
285 286 287 288

  /* 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. */
289
  if (G_LIKELY (priv->next_seqnum != -1)) {
290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316
    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;

317
  if (G_UNLIKELY (discont && !priv->discont)) {
318 319 320 321
    GST_LOG_OBJECT (filter, "mark DISCONT on input buffer");
    /* 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. */
    priv->discont = TRUE;
Wim Taymans's avatar
Wim Taymans committed
322
    in = gst_buffer_make_writable (in);
323 324 325
    GST_BUFFER_FLAG_SET (in, GST_BUFFER_FLAG_DISCONT);
  }

Wim Taymans's avatar
Wim Taymans committed
326
  bclass = GST_RTP_BASE_DEPAYLOAD_GET_CLASS (filter);
327

328 329 330
  if (G_UNLIKELY (bclass->process == NULL))
    goto no_process;

331 332 333
  /* let's send it out to processing */
  out_buf = bclass->process (filter, in);
  if (out_buf) {
Wim Taymans's avatar
Wim Taymans committed
334
    ret = gst_rtp_base_depayload_push (filter, out_buf);
335
  }
336 337
  gst_buffer_unref (in);

338
  return ret;
339 340

  /* ERRORS */
341 342 343
not_negotiated:
  {
    /* this is not fatal but should be filtered earlier */
Wim Taymans's avatar
Wim Taymans committed
344 345 346 347 348 349 350 351
    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"));
352 353 354
    gst_buffer_unref (in);
    return GST_FLOW_NOT_NEGOTIATED;
  }
355 356 357 358 359 360 361 362
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;
  }
363 364 365 366 367 368 369 370 371 372 373 374 375 376
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;
  }
377 378
}

379
static gboolean
Wim Taymans's avatar
Wim Taymans committed
380
gst_rtp_base_depayload_handle_event (GstRTPBaseDepayload * filter,
381
    GstEvent * event)
382 383
{
  gboolean res = TRUE;
384
  gboolean forward = TRUE;
385 386

  switch (GST_EVENT_TYPE (event)) {
387 388 389
    case GST_EVENT_FLUSH_STOP:
      gst_segment_init (&filter->segment, GST_FORMAT_UNDEFINED);
      filter->need_newsegment = TRUE;
390
      filter->priv->next_seqnum = -1;
391
      break;
392 393 394 395 396 397
    case GST_EVENT_CAPS:
    {
      GstCaps *caps;

      gst_event_parse_caps (event, &caps);

Wim Taymans's avatar
Wim Taymans committed
398
      res = gst_rtp_base_depayload_setcaps (filter, caps);
399 400 401
      forward = FALSE;
      break;
    }
Wim Taymans's avatar
Wim Taymans committed
402
    case GST_EVENT_SEGMENT:
403
    {
404
      gst_event_copy_segment (event, &filter->segment);
405 406
      /* don't pass the event downstream, we generate our own segment including
       * the NTP time and other things we receive in caps */
407
      forward = FALSE;
408
      break;
409
    }
410 411
    case GST_EVENT_CUSTOM_DOWNSTREAM:
    {
Wim Taymans's avatar
Wim Taymans committed
412
      GstRTPBaseDepayloadClass *bclass;
413

Wim Taymans's avatar
Wim Taymans committed
414
      bclass = GST_RTP_BASE_DEPAYLOAD_GET_CLASS (filter);
415 416 417 418

      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
419 420
         * implementation will make time progress by pushing out a GAP event.
         * Subclasses can override and to one of the following:
421 422 423 424 425 426 427 428
         *  - 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);
429
        forward = FALSE;
430 431 432
      }
      break;
    }
433 434 435
    default:
      break;
  }
436 437 438 439 440 441

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

442 443 444
  return res;
}

445
static gboolean
Wim Taymans's avatar
Wim Taymans committed
446 447
gst_rtp_base_depayload_handle_sink_event (GstPad * pad, GstObject * parent,
    GstEvent * event)
448 449
{
  gboolean res = FALSE;
Wim Taymans's avatar
Wim Taymans committed
450 451
  GstRTPBaseDepayload *filter;
  GstRTPBaseDepayloadClass *bclass;
452

Wim Taymans's avatar
Wim Taymans committed
453
  filter = GST_RTP_BASE_DEPAYLOAD (parent);
Wim Taymans's avatar
Wim Taymans committed
454
  bclass = GST_RTP_BASE_DEPAYLOAD_GET_CLASS (filter);
455 456
  if (bclass->handle_event)
    res = bclass->handle_event (filter, event);
457 458
  else
    gst_event_unref (event);
459 460 461 462

  return res;
}

463
static GstEvent *
464
create_segment_event (GstRTPBaseDepayload * filter, GstClockTime position)
465 466 467
{
  GstEvent *event;
  GstClockTime stop;
Wim Taymans's avatar
Wim Taymans committed
468
  GstRTPBaseDepayloadPrivate *priv;
Wim Taymans's avatar
Wim Taymans committed
469
  GstSegment segment;
470 471 472 473 474 475 476 477

  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
478 479 480 481 482 483 484 485 486
  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);
487 488 489 490

  return event;
}

491
typedef struct
492
{
Wim Taymans's avatar
Wim Taymans committed
493 494
  GstRTPBaseDepayload *depayload;
  GstRTPBaseDepayloadClass *bclass;
495
} HeaderData;
496

Wim Taymans's avatar
Wim Taymans committed
497 498
static gboolean
set_headers (GstBuffer ** buffer, guint idx, HeaderData * data)
499
{
Wim Taymans's avatar
Wim Taymans committed
500 501
  GstRTPBaseDepayload *depayload = data->depayload;
  GstRTPBaseDepayloadPrivate *priv = depayload->priv;
Wim Taymans's avatar
Wim Taymans committed
502
  GstClockTime pts, dts, duration;
503

Wim Taymans's avatar
Wim Taymans committed
504
  *buffer = gst_buffer_make_writable (*buffer);
505

Wim Taymans's avatar
Wim Taymans committed
506 507 508 509 510 511 512 513 514 515 516 517
  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;
518

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

Wim Taymans's avatar
Wim Taymans committed
525 526 527 528 529
  /* 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
530
  return TRUE;
531 532 533
}

static GstFlowReturn
Wim Taymans's avatar
Wim Taymans committed
534
gst_rtp_base_depayload_prepare_push (GstRTPBaseDepayload * filter,
Wim Taymans's avatar
Wim Taymans committed
535
    gboolean is_list, gpointer obj)
536 537 538 539
{
  HeaderData data;

  data.depayload = filter;
Wim Taymans's avatar
Wim Taymans committed
540
  data.bclass = GST_RTP_BASE_DEPAYLOAD_GET_CLASS (filter);
541 542

  if (is_list) {
Wim Taymans's avatar
Wim Taymans committed
543 544
    GstBufferList **blist = obj;
    gst_buffer_list_foreach (*blist, (GstBufferListFunc) set_headers, &data);
545
  } else {
Wim Taymans's avatar
Wim Taymans committed
546
    GstBuffer **buf = obj;
Wim Taymans's avatar
Wim Taymans committed
547
    set_headers (buf, 0, &data);
548
  }
549

550 551 552 553
  /* if this is the first buffer send a NEWSEGMENT */
  if (G_UNLIKELY (filter->need_newsegment)) {
    GstEvent *event;

554
    event = create_segment_event (filter, 0);
555 556 557 558 559 560 561

    gst_pad_push_event (filter->srcpad, event);

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

562
  return GST_FLOW_OK;
563 564 565
}

/**
Wim Taymans's avatar
Wim Taymans committed
566 567
 * gst_rtp_base_depayload_push:
 * @filter: a #GstRTPBaseDepayload
568 569 570 571 572
 * @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
573 574
 * This function will by default apply the last incomming timestamp on
 * the outgoing buffer when it didn't have a timestamp already.
575
 *
576
 * Returns: a #GstFlowReturn.
577 578
 */
GstFlowReturn
Wim Taymans's avatar
Wim Taymans committed
579
gst_rtp_base_depayload_push (GstRTPBaseDepayload * filter, GstBuffer * out_buf)
580
{
581 582
  GstFlowReturn res;

Wim Taymans's avatar
Wim Taymans committed
583
  res = gst_rtp_base_depayload_prepare_push (filter, FALSE, &out_buf);
584 585 586 587 588 589 590 591 592 593

  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
594 595
 * gst_rtp_base_depayload_push_list:
 * @filter: a #GstRTPBaseDepayload
596 597 598 599 600 601 602 603
 * @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
604
gst_rtp_base_depayload_push_list (GstRTPBaseDepayload * filter,
605 606 607 608
    GstBufferList * out_list)
{
  GstFlowReturn res;

Wim Taymans's avatar
Wim Taymans committed
609
  res = gst_rtp_base_depayload_prepare_push (filter, TRUE, &out_list);
610 611 612 613 614 615 616

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

619
/* convert the PacketLost event form a jitterbuffer to a GAP event.
620 621
 * subclasses can override this.  */
static gboolean
Wim Taymans's avatar
Wim Taymans committed
622
gst_rtp_base_depayload_packet_lost (GstRTPBaseDepayload * filter,
623 624
    GstEvent * event)
{
625
  GstClockTime timestamp, duration;
626 627 628 629 630 631 632 633 634 635 636 637
  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);

638 639
  /* send GAP event */
  sevent = gst_event_new_gap (timestamp, duration);
640 641 642 643

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

644
static GstStateChangeReturn
Wim Taymans's avatar
Wim Taymans committed
645
gst_rtp_base_depayload_change_state (GstElement * element,
646
    GstStateChange transition)
647
{
Wim Taymans's avatar
Wim Taymans committed
648 649
  GstRTPBaseDepayload *filter;
  GstRTPBaseDepayloadPrivate *priv;
650
  GstStateChangeReturn ret;
651

Wim Taymans's avatar
Wim Taymans committed
652
  filter = GST_RTP_BASE_DEPAYLOAD (element);
653
  priv = filter->priv;
654 655

  switch (transition) {
656
    case GST_STATE_CHANGE_NULL_TO_READY:
657
      break;
658
    case GST_STATE_CHANGE_READY_TO_PAUSED:
659
      filter->need_newsegment = TRUE;
660 661 662 663
      priv->npt_start = 0;
      priv->npt_stop = -1;
      priv->play_speed = 1.0;
      priv->play_scale = 1.0;
664 665
      priv->next_seqnum = -1;
      priv->negotiated = FALSE;
666
      priv->discont = FALSE;
667
      break;
668
    case GST_STATE_CHANGE_PAUSED_TO_PLAYING:
669 670 671 672 673
      break;
    default:
      break;
  }

674 675
  ret = GST_ELEMENT_CLASS (parent_class)->change_state (element, transition);

676
  switch (transition) {
677
    case GST_STATE_CHANGE_PLAYING_TO_PAUSED:
678
      break;
679
    case GST_STATE_CHANGE_PAUSED_TO_READY:
680
      break;
681
    case GST_STATE_CHANGE_READY_TO_NULL:
682
      break;
683 684
    default:
      break;
685
  }
686
  return ret;
687 688
}

689
static void
Wim Taymans's avatar
Wim Taymans committed
690
gst_rtp_base_depayload_set_property (GObject * object, guint prop_id,
691 692 693 694 695 696 697 698 699 700
    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
701
gst_rtp_base_depayload_get_property (GObject * object, guint prop_id,
702 703 704 705 706 707 708 709
    GValue * value, GParamSpec * pspec)
{
  switch (prop_id) {
    default:
      G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
      break;
  }
}