summaryrefslogtreecommitdiff
path: root/gst/gdp/gstgdppay.c
blob: 616fef9e67391a530c83b8f1cd0515a0b9977409 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
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
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
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
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
/* GStreamer
 * Copyright (C) 2006 Thomas Vander Stichele <thomas at apestaart dot org>
 *
 * 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.
 */

/**
 * SECTION:element-gdppay
 * @see_also: gdpdepay
 *
 * <refsect2>
 * <para>
 * This element payloads GStreamer buffers and events using the
 * GStreamer Data Protocol.
 * </para>
 * </refsect2>
 */

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

#include <gst/dataprotocol/dataprotocol.h>

#include "gstgdppay.h"

/* elementfactory information */
static const GstElementDetails gdp_pay_details =
GST_ELEMENT_DETAILS ("GDP Payloader",
    "GDP/Payloader",
    "Payloads GStreamer Data Protocol buffers",
    "Thomas Vander Stichele <thomas at apestaart dot org>");

static GstStaticPadTemplate gdp_pay_sink_template =
GST_STATIC_PAD_TEMPLATE ("sink",
    GST_PAD_SINK,
    GST_PAD_ALWAYS,
    GST_STATIC_CAPS_ANY);

static GstStaticPadTemplate gdp_pay_src_template =
GST_STATIC_PAD_TEMPLATE ("src",
    GST_PAD_SRC,
    GST_PAD_ALWAYS,
    GST_STATIC_CAPS ("application/x-gdp"));

GST_DEBUG_CATEGORY (gst_gdp_pay_debug);
#define GST_CAT_DEFAULT gst_gdp_pay_debug

#define DEFAULT_CRC_HEADER TRUE
#define DEFAULT_CRC_PAYLOAD FALSE
#define DEFAULT_VERSION GST_DP_VERSION_1_0

enum
{
  PROP_0,
  PROP_CRC_HEADER,
  PROP_CRC_PAYLOAD,
  PROP_VERSION,
};

#define _do_init(x) \
    GST_DEBUG_CATEGORY_INIT (gst_gdp_pay_debug, "gdppay", 0, \
    "GDP payloader");

GST_BOILERPLATE_FULL (GstGDPPay, gst_gdp_pay, GstElement,
    GST_TYPE_ELEMENT, _do_init);

static GstFlowReturn gst_gdp_pay_chain (GstPad * pad, GstBuffer * buffer);
static gboolean gst_gdp_pay_sink_event (GstPad * pad, GstEvent * event);
static GstStateChangeReturn gst_gdp_pay_change_state (GstElement *
    element, GstStateChange transition);

static void gst_gdp_pay_set_property (GObject * object, guint prop_id,
    const GValue * value, GParamSpec * pspec);
static void gst_gdp_pay_get_property (GObject * object, guint prop_id,
    GValue * value, GParamSpec * pspec);

static void gst_gdp_pay_dispose (GObject * gobject);

static void
gst_gdp_pay_base_init (gpointer g_class)
{
  GstElementClass *element_class = GST_ELEMENT_CLASS (g_class);

  gst_element_class_set_details (element_class, &gdp_pay_details);

  gst_element_class_add_pad_template (element_class,
      gst_static_pad_template_get (&gdp_pay_sink_template));
  gst_element_class_add_pad_template (element_class,
      gst_static_pad_template_get (&gdp_pay_src_template));
}

static void
gst_gdp_pay_class_init (GstGDPPayClass * klass)
{
  GObjectClass *gobject_class;
  GstElementClass *gstelement_class;

  gobject_class = (GObjectClass *) klass;
  gstelement_class = (GstElementClass *) klass;

  parent_class = g_type_class_peek_parent (klass);

  gobject_class->set_property = GST_DEBUG_FUNCPTR (gst_gdp_pay_set_property);
  gobject_class->get_property = GST_DEBUG_FUNCPTR (gst_gdp_pay_get_property);
  gobject_class->dispose = GST_DEBUG_FUNCPTR (gst_gdp_pay_dispose);
  gstelement_class->change_state = GST_DEBUG_FUNCPTR (gst_gdp_pay_change_state);

  g_object_class_install_property (gobject_class, PROP_CRC_HEADER,
      g_param_spec_boolean ("crc-header", "CRC Header",
          "Calculate and store a CRC checksum on the header",
          DEFAULT_CRC_HEADER, G_PARAM_READWRITE));
  g_object_class_install_property (gobject_class, PROP_CRC_PAYLOAD,
      g_param_spec_boolean ("crc-payload", "CRC Payload",
          "Calculate and store a CRC checksum on the payload",
          DEFAULT_CRC_PAYLOAD, G_PARAM_READWRITE));
  g_object_class_install_property (gobject_class, PROP_VERSION,
      g_param_spec_enum ("version", "Version",
          "Version of the GStreamer Data Protocol",
          GST_TYPE_DP_VERSION, DEFAULT_VERSION, G_PARAM_READWRITE));
}

static void
gst_gdp_pay_init (GstGDPPay * gdppay, GstGDPPayClass * g_class)
{
  gdppay->sinkpad =
      gst_pad_new_from_static_template (&gdp_pay_sink_template, "sink");
  gst_pad_set_chain_function (gdppay->sinkpad,
      GST_DEBUG_FUNCPTR (gst_gdp_pay_chain));
  gst_pad_set_event_function (gdppay->sinkpad,
      GST_DEBUG_FUNCPTR (gst_gdp_pay_sink_event));
  gst_element_add_pad (GST_ELEMENT (gdppay), gdppay->sinkpad);

  gdppay->srcpad =
      gst_pad_new_from_static_template (&gdp_pay_src_template, "src");
  gst_element_add_pad (GST_ELEMENT (gdppay), gdppay->srcpad);

  gdppay->offset = 0;

  gdppay->crc_header = DEFAULT_CRC_HEADER;
  gdppay->crc_payload = DEFAULT_CRC_PAYLOAD;
  gdppay->header_flag = gdppay->crc_header | gdppay->crc_payload;
  gdppay->version = DEFAULT_VERSION;
}

static void
gst_gdp_pay_dispose (GObject * gobject)
{
  GstGDPPay *this = GST_GDP_PAY (gobject);

  if (this->caps_buf) {
    gst_buffer_unref (this->caps_buf);
    this->caps_buf = NULL;
  }
  if (this->new_segment_buf) {
    gst_buffer_unref (this->new_segment_buf);
    this->new_segment_buf = NULL;
  }
  GST_CALL_PARENT (G_OBJECT_CLASS, dispose, (gobject));
}

/* set OFFSET and OFFSET_END with running count */
static void
gst_gdp_stamp_buffer (GstGDPPay * this, GstBuffer * buffer)
{
  GST_BUFFER_OFFSET (buffer) = this->offset;
  GST_BUFFER_OFFSET_END (buffer) = this->offset + GST_BUFFER_SIZE (buffer);
  this->offset = GST_BUFFER_OFFSET_END (buffer);
}

static GstBuffer *
gst_gdp_buffer_from_caps (GstGDPPay * this, GstCaps * caps)
{
  GstBuffer *headerbuf;
  GstBuffer *payloadbuf;
  guint8 *header, *payload;
  guint len;

  if (!this->packetizer->packet_from_caps (caps, this->header_flag, &len,
          &header, &payload)) {
    GST_WARNING_OBJECT (this, "could not create GDP header from caps");
    return NULL;
  }

  GST_LOG_OBJECT (this, "creating GDP header and payload buffer from caps");
  headerbuf = gst_buffer_new ();
  gst_buffer_set_data (headerbuf, header, len);
  GST_BUFFER_MALLOCDATA (headerbuf) = header;

  payloadbuf = gst_buffer_new ();
  gst_buffer_set_data (payloadbuf, payload,
      gst_dp_header_payload_length (header));
  GST_BUFFER_MALLOCDATA (payloadbuf) = payload;

  return gst_buffer_join (headerbuf, payloadbuf);
}

static GstBuffer *
gst_gdp_pay_buffer_from_buffer (GstGDPPay * this, GstBuffer * buffer)
{
  GstBuffer *headerbuf;
  guint8 *header;
  guint len;

  if (!this->packetizer->header_from_buffer (buffer, this->header_flag, &len,
          &header)) {
    GST_WARNING_OBJECT (this, "could not create GDP header from buffer");
    return NULL;
  }

  GST_LOG_OBJECT (this, "creating GDP header and payload buffer from buffer");
  headerbuf = gst_buffer_new ();
  gst_buffer_set_data (headerbuf, header, len);
  GST_BUFFER_MALLOCDATA (headerbuf) = header;

  /* we do not want to lose the ref on the incoming buffer */
  gst_buffer_ref (buffer);
  return gst_buffer_join (headerbuf, buffer);
}

static GstBuffer *
gst_gdp_buffer_from_event (GstGDPPay * this, GstEvent * event)
{
  GstBuffer *headerbuf;
  GstBuffer *payloadbuf;
  guint8 *header, *payload;
  guint len;
  gboolean ret;

  ret =
      this->packetizer->packet_from_event (event, this->header_flag, &len,
      &header, &payload);

  if (!ret) {
    GST_WARNING_OBJECT (this, "could not create GDP header from event %s (%d)",
        gst_event_type_get_name (event->type), event->type);
    return NULL;
  }

  GST_LOG_OBJECT (this, "creating GDP header and payload buffer from event");
  headerbuf = gst_buffer_new ();
  gst_buffer_set_data (headerbuf, header, len);
  GST_BUFFER_MALLOCDATA (headerbuf) = header;

  payloadbuf = gst_buffer_new ();
  gst_buffer_set_data (payloadbuf, payload,
      gst_dp_header_payload_length (header));
  GST_BUFFER_MALLOCDATA (payloadbuf) = payload;

  return gst_buffer_join (headerbuf, payloadbuf);
}


/* set our caps with streamheader, based on the latest newsegment and caps,
 * and (possibly) GDP-serialized buffers of the streamheaders on the src pad */
static GstFlowReturn
gst_gdp_pay_reset_streamheader (GstGDPPay * this)
{
  GstCaps *caps;
  GstStructure *structure;
  GstBuffer *new_segment_buf = NULL, *caps_buf, *tag_buf = NULL;
  GstFlowReturn r = GST_FLOW_OK;
  gboolean version_one_zero = TRUE;

  GValue array = { 0 };
  GValue value = { 0 };

  /* In version 0.2, we didn't need or send new segment or tags */
  if (this->version == GST_DP_VERSION_0_2)
    version_one_zero = FALSE;

  if (version_one_zero) {
    if (!this->new_segment_buf || !this->caps_buf)
      return GST_FLOW_OK;
  } else {
    if (!this->caps_buf)
      return GST_FLOW_OK;
  }

  /* put copies of the buffers in a fixed list */
  g_value_init (&array, GST_TYPE_ARRAY);

  if (version_one_zero) {
    new_segment_buf = gst_buffer_copy (this->new_segment_buf);
    g_value_init (&value, GST_TYPE_BUFFER);
    gst_value_set_buffer (&value, new_segment_buf);
    gst_value_array_append_value (&array, &value);
    g_value_unset (&value);

    if (this->tag_buf) {
      tag_buf = gst_buffer_copy (this->tag_buf);
      g_value_init (&value, GST_TYPE_BUFFER);
      gst_value_set_buffer (&value, tag_buf);
      gst_value_array_append_value (&array, &value);
      g_value_unset (&value);
    }
  }

  caps_buf = gst_buffer_copy (this->caps_buf);
  g_value_init (&value, GST_TYPE_BUFFER);
  gst_value_set_buffer (&value, caps_buf);
  gst_value_array_append_value (&array, &value);
  g_value_unset (&value);

  /* we also need to add GDP serializations of the streamheaders of the
   * incoming caps */
  structure = gst_caps_get_structure (this->caps, 0);
  if (gst_structure_has_field (structure, "streamheader")) {
    const GValue *sh;
    GArray *buffers;
    GstBuffer *buffer;
    int i;

    sh = gst_structure_get_value (structure, "streamheader");
    buffers = g_value_peek_pointer (sh);
    GST_DEBUG_OBJECT (this,
        "Need to serialize %d incoming streamheader buffers on ours",
        buffers->len);
    for (i = 0; i < buffers->len; ++i) {
      GValue *bufval;
      GstBuffer *outbuffer;

      bufval = &g_array_index (buffers, GValue, i);
      buffer = g_value_peek_pointer (bufval);
      outbuffer = gst_gdp_pay_buffer_from_buffer (this, buffer);
      if (outbuffer) {
        g_value_init (&value, GST_TYPE_BUFFER);
        gst_value_set_buffer (&value, outbuffer);
        gst_value_array_append_value (&array, &value);
        g_value_unset (&value);
      }
      /* FIXME: if one or more in this loop fail to produce and outbuffer,
       * should we error out ? Once ? Every time ? */
    }
  }

  caps = gst_caps_from_string ("application/x-gdp");
  structure = gst_caps_get_structure (caps, 0);

  gst_structure_set_value (structure, "streamheader", &array);
  g_value_unset (&array);

  /* Unref our copies */
  if (new_segment_buf)
    gst_buffer_unref (new_segment_buf);
  gst_buffer_unref (caps_buf);

  GST_DEBUG_OBJECT (this, "Setting caps on src pad %" GST_PTR_FORMAT, caps);
  gst_pad_set_caps (this->srcpad, caps);
  gst_buffer_set_caps (this->caps_buf, caps);
  gst_buffer_set_caps (this->new_segment_buf, caps);

  /* if these are our first ever buffers, send out new_segment first */
  if (!this->sent_streamheader) {
    GstEvent *event =
        gst_event_new_new_segment (TRUE, 1.0, GST_FORMAT_BYTES, 0, -1, 0);
    GST_DEBUG_OBJECT (this, "Sending out new_segment event %p", event);
    if (!gst_pad_push_event (this->srcpad, event)) {
      GST_WARNING_OBJECT (this, "pushing new segment failed");
      return GST_FLOW_ERROR;
    }
  }

  /* push out these streamheader buffers, then flush our internal queue */
  GST_DEBUG_OBJECT (this, "Pushing GDP new_segment buffer %p",
      this->new_segment_buf);
  /* we stored these bufs with refcount 1, so make sure we keep a ref */
  r = gst_pad_push (this->srcpad, gst_buffer_ref (this->new_segment_buf));
  if (r != GST_FLOW_OK) {
    GST_WARNING_OBJECT (this, "pushing GDP newsegment buffer returned %d", r);
    return r;
  }
  if (this->tag_buf) {
    GST_DEBUG_OBJECT (this, "Pushing GDP tag buffer %p", this->tag_buf);
    /* we stored these bufs with refcount 1, so make sure we keep a ref */
    r = gst_pad_push (this->srcpad, gst_buffer_ref (this->tag_buf));
    if (r != GST_FLOW_OK) {
      GST_WARNING_OBJECT (this, "pushing GDP tag buffer returned %d", r);
      return r;
    }
  }
  GST_DEBUG_OBJECT (this, "Pushing GDP caps buffer %p", this->new_segment_buf);
  r = gst_pad_push (this->srcpad, gst_buffer_ref (this->caps_buf));
  if (r != GST_FLOW_OK) {
    GST_WARNING_OBJECT (this, "pushing GDP caps buffer returned %d", r);
    return r;
  }
  this->sent_streamheader = TRUE;
  GST_DEBUG_OBJECT (this, "need to push %d queued buffers",
      g_list_length (this->queue));
  if (this->queue) {
    GList *l;

    for (l = this->queue; l; l = g_list_next (l)) {
      GST_DEBUG_OBJECT (this, "Pushing queued GDP buffer %p", l->data);
      gst_buffer_set_caps (l->data, caps);
      r = gst_pad_push (this->srcpad, l->data);
      if (r != GST_FLOW_OK) {
        GST_WARNING_OBJECT (this, "pushing queued GDP buffer returned %d", r);
        return r;
      }
    }
  }

  return r;
}

/* queue a buffer internally if we haven't sent streamheader buffers yet;
 * otherwise, just push on */
static GstFlowReturn
gst_gdp_queue_buffer (GstGDPPay * this, GstBuffer * buffer)
{
  if (this->sent_streamheader) {
    GST_LOG_OBJECT (this, "Pushing GDP buffer %p", buffer);
    GST_LOG_OBJECT (this, "set caps %" GST_PTR_FORMAT, this->caps);
    return gst_pad_push (this->srcpad, buffer);
  }

  /* store it on an internal queue */
  this->queue = g_list_append (this->queue, buffer);
  GST_DEBUG_OBJECT (this, "queued buffer %p, now %d buffers queued",
      buffer, g_list_length (this->queue));
  return GST_FLOW_OK;
}

static GstFlowReturn
gst_gdp_pay_chain (GstPad * pad, GstBuffer * buffer)
{
  GstGDPPay *this;
  GstCaps *caps;
  GstBuffer *outbuffer;
  GstFlowReturn ret;

  this = GST_GDP_PAY (gst_pad_get_parent (pad));

  /* we should have received a new_segment before, otherwise it's a bug.
   * fake one in that case */
  if (!this->new_segment_buf) {
    GstEvent *event;

    GST_WARNING_OBJECT (this,
        "did not receive new-segment before first buffer");
    event = gst_event_new_new_segment (TRUE, 1.0, GST_FORMAT_BYTES, 0, -1, 0);
    outbuffer = gst_gdp_buffer_from_event (this, event);
    gst_event_unref (event);

    /* GDP 0.2 doesn't know about new-segment, so this is not fatal */
    if (!outbuffer) {
      GST_ELEMENT_WARNING (this, STREAM, ENCODE, (NULL),
          ("Could not create GDP buffer from new segment event"));
/*
      ret = GST_FLOW_ERROR;
      goto done;
*/
    } else {

      gst_gdp_stamp_buffer (this, outbuffer);
      GST_BUFFER_TIMESTAMP (outbuffer) = GST_BUFFER_TIMESTAMP (buffer);
      GST_BUFFER_DURATION (outbuffer) = 0;
      GST_DEBUG_OBJECT (this, "Storing buffer %p as new_segment_buf",
          outbuffer);
      this->new_segment_buf = outbuffer;
    }
  }

  /* make sure we've received caps before */
  caps = gst_buffer_get_caps (buffer);
  if (!this->caps && !caps) {
    GST_WARNING_OBJECT (this, "first received buffer does not have caps set");
    if (caps)
      gst_caps_unref (caps);
    ret = GST_FLOW_NOT_NEGOTIATED;
    goto done;
  }

  /* if the caps have changed, process caps first */
  if (caps && !gst_caps_is_equal (this->caps, caps)) {
    GST_LOG_OBJECT (this, "caps changed to %p, %" GST_PTR_FORMAT, caps, caps);
    gst_caps_replace (&(this->caps), caps);
    outbuffer = gst_gdp_buffer_from_caps (this, caps);
    if (!outbuffer) {
      GST_ELEMENT_ERROR (this, STREAM, ENCODE, (NULL),
          ("Could not create GDP buffer from caps %" GST_PTR_FORMAT, caps));
      gst_caps_unref (caps);
      ret = GST_FLOW_ERROR;
      goto done;
    }

    gst_gdp_stamp_buffer (this, outbuffer);
    GST_BUFFER_TIMESTAMP (outbuffer) = GST_BUFFER_TIMESTAMP (buffer);
    GST_BUFFER_DURATION (outbuffer) = 0;
    GST_BUFFER_FLAG_SET (outbuffer, GST_BUFFER_FLAG_IN_CAPS);
    this->caps_buf = outbuffer;
    gst_gdp_pay_reset_streamheader (this);
  }

  /* create a GDP header packet,
   * then create a GST buffer of the header packet and the buffer contents */
  outbuffer = gst_gdp_pay_buffer_from_buffer (this, buffer);
  if (!outbuffer) {
    GST_ELEMENT_ERROR (this, STREAM, ENCODE, (NULL),
        ("Could not create GDP buffer from buffer"));
    ret = GST_FLOW_ERROR;
    goto done;
  }

  gst_gdp_stamp_buffer (this, outbuffer);
  GST_BUFFER_TIMESTAMP (outbuffer) = GST_BUFFER_TIMESTAMP (buffer);
  GST_BUFFER_DURATION (outbuffer) = GST_BUFFER_DURATION (buffer);

  ret = gst_gdp_queue_buffer (this, outbuffer);

done:
  gst_buffer_unref (buffer);
  gst_object_unref (this);
  return ret;
}

static gboolean
gst_gdp_pay_sink_event (GstPad * pad, GstEvent * event)
{
  GstBuffer *outbuffer;
  GstGDPPay *this = GST_GDP_PAY (gst_pad_get_parent (pad));
  GstFlowReturn flowret;
  gboolean ret = TRUE;

  GST_DEBUG_OBJECT (this, "received event %p of type %s (%d)",
      event, gst_event_type_get_name (event->type), event->type);

  /* now turn the event into a buffer */
  outbuffer = gst_gdp_buffer_from_event (this, event);
  if (!outbuffer) {
    GST_ELEMENT_WARNING (this, STREAM, ENCODE, (NULL),
        ("Could not create GDP buffer from received event (type %s)",
            gst_event_type_get_name (event->type)));
    ret = FALSE;
    goto done;
  }
  gst_gdp_stamp_buffer (this, outbuffer);
  GST_BUFFER_TIMESTAMP (outbuffer) = GST_EVENT_TIMESTAMP (event);
  GST_BUFFER_DURATION (outbuffer) = 0;

  /* if we got a new segment, we should put it on our streamheader,
   * and not send it on */
  switch (GST_EVENT_TYPE (event)) {
    case GST_EVENT_NEWSEGMENT:
      GST_DEBUG_OBJECT (this, "received new_segment event");
      if (this->new_segment_buf) {
        gst_buffer_unref (this->new_segment_buf);
      }
      GST_DEBUG_OBJECT (this, "Storing buffer %p as new_segment_buf",
          outbuffer);
      this->new_segment_buf = outbuffer;
      gst_gdp_pay_reset_streamheader (this);
      break;
    case GST_EVENT_TAG:
      GST_DEBUG_OBJECT (this, "received tag event");
      if (this->tag_buf) {
        gst_buffer_unref (this->tag_buf);
      }
      GST_DEBUG_OBJECT (this, "Storing buffer %p as tag_buf", outbuffer);
      this->tag_buf = outbuffer;
      gst_gdp_pay_reset_streamheader (this);
      break;
    default:
      GST_DEBUG_OBJECT (this, "queuing GDP buffer %p of event %p", outbuffer,
          event);
      flowret = gst_gdp_queue_buffer (this, outbuffer);
      if (flowret != GST_FLOW_OK) {
        GST_WARNING_OBJECT (this, "queueing GDP event buffer returned %d",
            flowret);
        ret = FALSE;
        goto done;
      }
      break;
  }

  /* if we have EOS, we should send on EOS ourselves */
  if (GST_EVENT_TYPE (event) == GST_EVENT_EOS) {
    GST_DEBUG_OBJECT (this, "Sending on EOS event %p", event);
    return gst_pad_push_event (this->srcpad, event);
  };

done:
  gst_object_unref (this);
  gst_event_unref (event);
  return ret;
}

static void
gst_gdp_pay_set_property (GObject * object, guint prop_id,
    const GValue * value, GParamSpec * pspec)
{
  GstGDPPay *this;

  g_return_if_fail (GST_IS_GDP_PAY (object));
  this = GST_GDP_PAY (object);

  switch (prop_id) {
    case PROP_CRC_HEADER:
      this->crc_header =
          g_value_get_boolean (value) ? GST_DP_HEADER_FLAG_CRC_HEADER : 0;
      this->header_flag = this->crc_header | this->crc_payload;
      break;
    case PROP_CRC_PAYLOAD:
      this->crc_payload =
          g_value_get_boolean (value) ? GST_DP_HEADER_FLAG_CRC_PAYLOAD : 0;
      this->header_flag = this->crc_header | this->crc_payload;
      break;
    case PROP_VERSION:
      this->version = g_value_get_enum (value);
      break;
    default:
      G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
      break;
  }
}

static void
gst_gdp_pay_get_property (GObject * object, guint prop_id,
    GValue * value, GParamSpec * pspec)
{
  GstGDPPay *this;

  g_return_if_fail (GST_IS_GDP_PAY (object));
  this = GST_GDP_PAY (object);

  switch (prop_id) {
    case PROP_CRC_HEADER:
      g_value_set_boolean (value, this->crc_header);
      break;
    case PROP_CRC_PAYLOAD:
      g_value_set_boolean (value, this->crc_payload);
      break;
    case PROP_VERSION:
      g_value_set_enum (value, this->version);
      break;
    default:
      G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
      break;
  }
}

static GstStateChangeReturn
gst_gdp_pay_change_state (GstElement * element, GstStateChange transition)
{
  GstStateChangeReturn ret;
  GstGDPPay *this = GST_GDP_PAY (element);

  switch (transition) {
    case GST_STATE_CHANGE_READY_TO_PAUSED:
      this->packetizer = gst_dp_get_packetizer (this->version);
      break;
    default:
      break;
  }

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

  switch (transition) {
    case GST_STATE_CHANGE_PAUSED_TO_READY:
      if (this->packetizer) {
        g_free (this->packetizer);
        this->packetizer = NULL;
      }
      break;
    case GST_STATE_CHANGE_READY_TO_NULL:
      if (this->caps) {
        gst_caps_unref (this->caps);
        this->caps = NULL;
      }
      break;
    default:
      break;
  }

  return ret;
}

gboolean
gst_gdp_pay_plugin_init (GstPlugin * plugin)
{
  if (!gst_element_register (plugin, "gdppay", GST_RANK_NONE, GST_TYPE_GDP_PAY))
    return FALSE;

  return TRUE;
}