update for new memory api
[platform/upstream/gstreamer.git] / gst / gdp / gstgdppay.c
1 /* GStreamer
2  * Copyright (C) 2006 Thomas Vander Stichele <thomas at apestaart dot org>
3  *
4  * This library is free software; you can redistribute it and/or
5  * modify it under the terms of the GNU Library General Public
6  * License as published by the Free Software Foundation; either
7  * version 2 of the License, or (at your option) any later version.
8  *
9  * This library is distributed in the hope that it will be useful,
10  * but WITHOUT ANY WARRANTY; without even the implied warranty of
11  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
12  * Library General Public License for more details.
13  *
14  * You should have received a copy of the GNU Library General Public
15  * License along with this library; if not, write to the
16  * Free Software Foundation, Inc., 59 Temple Place - Suite 330,
17  * Boston, MA 02111-1307, USA.
18  */
19
20 /**
21  * SECTION:element-gdppay
22  * @see_also: gdpdepay
23  *
24  * This element payloads GStreamer buffers and events using the
25  * GStreamer Data Protocol.
26  *
27  * <refsect2>
28  * |[
29  * gst-launch -v -m videotestsrc num-buffers=50 ! gdppay ! filesink location=test.gdp
30  * ]| This pipeline creates a serialized video stream that can be played back
31  * with the example shown in gdpdepay.
32  * </refsect2>
33  */
34
35 #ifdef HAVE_CONFIG_H
36 #include "config.h"
37 #endif
38
39 #include "dataprotocol.h"
40
41 #include "gstgdppay.h"
42
43 static GstStaticPadTemplate gdp_pay_sink_template =
44 GST_STATIC_PAD_TEMPLATE ("sink",
45     GST_PAD_SINK,
46     GST_PAD_ALWAYS,
47     GST_STATIC_CAPS_ANY);
48
49 static GstStaticPadTemplate gdp_pay_src_template =
50 GST_STATIC_PAD_TEMPLATE ("src",
51     GST_PAD_SRC,
52     GST_PAD_ALWAYS,
53     GST_STATIC_CAPS ("application/x-gdp"));
54
55 GST_DEBUG_CATEGORY_STATIC (gst_gdp_pay_debug);
56 #define GST_CAT_DEFAULT gst_gdp_pay_debug
57
58 #define DEFAULT_CRC_HEADER TRUE
59 #define DEFAULT_CRC_PAYLOAD FALSE
60 #define DEFAULT_VERSION GST_DP_VERSION_1_0
61
62 enum
63 {
64   PROP_0,
65   PROP_CRC_HEADER,
66   PROP_CRC_PAYLOAD,
67   PROP_VERSION,
68 };
69
70 #define _do_init \
71     GST_DEBUG_CATEGORY_INIT (gst_gdp_pay_debug, "gdppay", 0, \
72     "GDP payloader");
73 #define gst_gdp_pay_parent_class parent_class
74 G_DEFINE_TYPE_WITH_CODE (GstGDPPay, gst_gdp_pay, GST_TYPE_ELEMENT, _do_init);
75
76 static void gst_gdp_pay_reset (GstGDPPay * this);
77
78 static GstFlowReturn gst_gdp_pay_chain (GstPad * pad, GstObject * parent,
79     GstBuffer * buffer);
80 static gboolean gst_gdp_pay_src_event (GstPad * pad, GstObject * parent,
81     GstEvent * event);
82 static gboolean gst_gdp_pay_sink_event (GstPad * pad, GstObject * parent,
83     GstEvent * event);
84
85 static GstStateChangeReturn gst_gdp_pay_change_state (GstElement *
86     element, GstStateChange transition);
87
88 static void gst_gdp_pay_set_property (GObject * object, guint prop_id,
89     const GValue * value, GParamSpec * pspec);
90 static void gst_gdp_pay_get_property (GObject * object, guint prop_id,
91     GValue * value, GParamSpec * pspec);
92
93 static void gst_gdp_pay_finalize (GObject * gobject);
94
95 static void
96 gst_gdp_pay_class_init (GstGDPPayClass * klass)
97 {
98   GObjectClass *gobject_class;
99   GstElementClass *gstelement_class;
100
101   gobject_class = (GObjectClass *) klass;
102   gstelement_class = (GstElementClass *) klass;
103
104   gobject_class->set_property = gst_gdp_pay_set_property;
105   gobject_class->get_property = gst_gdp_pay_get_property;
106   gobject_class->finalize = gst_gdp_pay_finalize;
107
108   g_object_class_install_property (gobject_class, PROP_CRC_HEADER,
109       g_param_spec_boolean ("crc-header", "CRC Header",
110           "Calculate and store a CRC checksum on the header",
111           DEFAULT_CRC_HEADER, G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS));
112   g_object_class_install_property (gobject_class, PROP_CRC_PAYLOAD,
113       g_param_spec_boolean ("crc-payload", "CRC Payload",
114           "Calculate and store a CRC checksum on the payload",
115           DEFAULT_CRC_PAYLOAD, G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS));
116   g_object_class_install_property (gobject_class, PROP_VERSION,
117       g_param_spec_enum ("version", "Version",
118           "Version of the GStreamer Data Protocol",
119           GST_TYPE_DP_VERSION, DEFAULT_VERSION,
120           G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS));
121
122   gst_element_class_set_details_simple (gstelement_class,
123       "GDP Payloader", "GDP/Payloader",
124       "Payloads GStreamer Data Protocol buffers",
125       "Thomas Vander Stichele <thomas at apestaart dot org>");
126
127   gst_element_class_add_pad_template (gstelement_class,
128       gst_static_pad_template_get (&gdp_pay_sink_template));
129   gst_element_class_add_pad_template (gstelement_class,
130       gst_static_pad_template_get (&gdp_pay_src_template));
131
132   gstelement_class->change_state = GST_DEBUG_FUNCPTR (gst_gdp_pay_change_state);
133 }
134
135 static void
136 gst_gdp_pay_init (GstGDPPay * gdppay)
137 {
138   gdppay->sinkpad =
139       gst_pad_new_from_static_template (&gdp_pay_sink_template, "sink");
140   gst_pad_set_chain_function (gdppay->sinkpad,
141       GST_DEBUG_FUNCPTR (gst_gdp_pay_chain));
142   gst_pad_set_event_function (gdppay->sinkpad,
143       GST_DEBUG_FUNCPTR (gst_gdp_pay_sink_event));
144   gst_element_add_pad (GST_ELEMENT (gdppay), gdppay->sinkpad);
145
146   gdppay->srcpad =
147       gst_pad_new_from_static_template (&gdp_pay_src_template, "src");
148   gst_pad_set_event_function (gdppay->srcpad,
149       GST_DEBUG_FUNCPTR (gst_gdp_pay_src_event));
150   gst_element_add_pad (GST_ELEMENT (gdppay), gdppay->srcpad);
151
152   gdppay->crc_header = DEFAULT_CRC_HEADER;
153   gdppay->crc_payload = DEFAULT_CRC_PAYLOAD;
154   gdppay->header_flag = gdppay->crc_header | gdppay->crc_payload;
155   gdppay->version = DEFAULT_VERSION;
156   gdppay->offset = 0;
157
158   gdppay->packetizer = gst_dp_packetizer_new (gdppay->version);
159 }
160
161 static void
162 gst_gdp_pay_finalize (GObject * gobject)
163 {
164   GstGDPPay *this = GST_GDP_PAY (gobject);
165
166   gst_gdp_pay_reset (this);
167   gst_dp_packetizer_free (this->packetizer);
168
169   GST_CALL_PARENT (G_OBJECT_CLASS, finalize, (gobject));
170 }
171
172 static void
173 gst_gdp_pay_reset (GstGDPPay * this)
174 {
175   GST_DEBUG_OBJECT (this, "Resetting GDP object");
176   /* clear the queued buffers */
177   while (this->queue) {
178     GstBuffer *buffer;
179
180     buffer = GST_BUFFER_CAST (this->queue->data);
181
182     /* delete buffer from queue now */
183     this->queue = g_list_delete_link (this->queue, this->queue);
184
185     gst_buffer_unref (buffer);
186   }
187   if (this->caps) {
188     gst_caps_unref (this->caps);
189     this->caps = NULL;
190   }
191   if (this->caps_buf) {
192     gst_buffer_unref (this->caps_buf);
193     this->caps_buf = NULL;
194   }
195   if (this->tag_buf) {
196     gst_buffer_unref (this->tag_buf);
197     this->tag_buf = NULL;
198   }
199   if (this->new_segment_buf) {
200     gst_buffer_unref (this->new_segment_buf);
201     this->new_segment_buf = NULL;
202   }
203   this->sent_streamheader = FALSE;
204   this->offset = 0;
205 }
206
207 /* set OFFSET and OFFSET_END with running count */
208 static void
209 gst_gdp_stamp_buffer (GstGDPPay * this, GstBuffer * buffer)
210 {
211   GST_BUFFER_OFFSET (buffer) = this->offset;
212   GST_BUFFER_OFFSET_END (buffer) = this->offset + gst_buffer_get_size (buffer);
213   this->offset = GST_BUFFER_OFFSET_END (buffer);
214 }
215
216 static GstBuffer *
217 gst_gdp_buffer_from_caps (GstGDPPay * this, GstCaps * caps)
218 {
219   GstBuffer *headerbuf;
220   GstBuffer *payloadbuf;
221   guint8 *header, *payload;
222   guint len, plen;
223
224   if (!this->packetizer->packet_from_caps (caps, this->header_flag, &len,
225           &header, &payload))
226     goto packet_failed;
227
228   GST_LOG_OBJECT (this, "creating GDP header and payload buffer from caps");
229   headerbuf = gst_buffer_new ();
230   gst_buffer_take_memory (headerbuf, -1,
231       gst_memory_new_wrapped (0, header, len, 0, len, header, g_free));
232
233   payloadbuf = gst_buffer_new ();
234   plen = gst_dp_header_payload_length (header);
235   gst_buffer_take_memory (payloadbuf, -1,
236       gst_memory_new_wrapped (0, payload, plen, 0, plen, payload, g_free));
237
238   return gst_buffer_join (headerbuf, payloadbuf);
239
240   /* ERRORS */
241 packet_failed:
242   {
243     GST_WARNING_OBJECT (this, "could not create GDP header from caps");
244     return NULL;
245   }
246 }
247
248 static GstBuffer *
249 gst_gdp_pay_buffer_from_buffer (GstGDPPay * this, GstBuffer * buffer)
250 {
251   GstBuffer *headerbuf;
252   guint8 *header;
253   guint len;
254
255   if (!this->packetizer->header_from_buffer (buffer, this->header_flag, &len,
256           &header))
257     goto no_buffer;
258
259   GST_LOG_OBJECT (this, "creating GDP header and payload buffer from buffer");
260   headerbuf = gst_buffer_new ();
261   gst_buffer_take_memory (headerbuf, -1,
262       gst_memory_new_wrapped (0, header, len, 0, len, header, g_free));
263
264   /* we do not want to lose the ref on the incoming buffer */
265   gst_buffer_ref (buffer);
266
267   return gst_buffer_join (headerbuf, buffer);
268
269   /* ERRORS */
270 no_buffer:
271   {
272     GST_WARNING_OBJECT (this, "could not create GDP header from buffer");
273     return NULL;
274   }
275 }
276
277 static GstBuffer *
278 gst_gdp_buffer_from_event (GstGDPPay * this, GstEvent * event)
279 {
280   GstBuffer *headerbuf;
281   GstBuffer *payloadbuf;
282   guint8 *header, *payload;
283   guint len, plen;
284   gboolean ret;
285
286   ret =
287       this->packetizer->packet_from_event (event, this->header_flag, &len,
288       &header, &payload);
289   if (!ret)
290     goto no_event;
291
292   GST_LOG_OBJECT (this, "creating GDP header and payload buffer from event");
293   headerbuf = gst_buffer_new ();
294   gst_buffer_take_memory (headerbuf, -1,
295       gst_memory_new_wrapped (0, header, len, 0, len, header, g_free));
296
297   payloadbuf = gst_buffer_new ();
298   plen = gst_dp_header_payload_length (header);
299   if (plen && payload != NULL) {
300     gst_buffer_take_memory (payloadbuf, -1,
301         gst_memory_new_wrapped (0, payload, plen, 0, plen, payload, g_free));
302   }
303
304   return gst_buffer_join (headerbuf, payloadbuf);
305
306   /* ERRORS */
307 no_event:
308   {
309     GST_WARNING_OBJECT (this, "could not create GDP header from event %s (%d)",
310         gst_event_type_get_name (event->type), event->type);
311     return NULL;
312   }
313 }
314
315
316 /* set our caps with streamheader, based on the latest newsegment and caps,
317  * and (possibly) GDP-serialized buffers of the streamheaders on the src pad */
318 static GstFlowReturn
319 gst_gdp_pay_reset_streamheader (GstGDPPay * this)
320 {
321   GstCaps *caps;
322   /* We use copies of these to avoid circular refcounts */
323   GstBuffer *new_segment_buf, *caps_buf, *tag_buf;
324   GstStructure *structure;
325   GstFlowReturn r = GST_FLOW_OK;
326   gboolean version_one_zero = TRUE;
327
328   GValue array = { 0 };
329   GValue value = { 0 };
330
331   GST_DEBUG_OBJECT (this, "start");
332   /* In version 0.2, we didn't need or send new segment or tags */
333   if (this->version == GST_DP_VERSION_0_2)
334     version_one_zero = FALSE;
335
336   if (version_one_zero) {
337     if (!this->new_segment_buf || !this->caps_buf) {
338       GST_DEBUG_OBJECT (this, "1.0, missing new_segment or caps, returning");
339       return GST_FLOW_OK;
340     }
341   } else {
342     if (!this->caps_buf) {
343       GST_DEBUG_OBJECT (this, "0.2, missing caps, returning");
344       return GST_FLOW_OK;
345     }
346   }
347
348   /* put copies of the buffers in a fixed list
349    * Stamp the buffers with offset and offset_end as well.
350    * We do this here so the offsets match the order the buffers go out in */
351   g_value_init (&array, GST_TYPE_ARRAY);
352
353   if (version_one_zero) {
354     gst_gdp_stamp_buffer (this, this->new_segment_buf);
355     GST_DEBUG_OBJECT (this, "1.0, appending copy of new segment buffer %p",
356         this->new_segment_buf);
357     new_segment_buf = gst_buffer_copy (this->new_segment_buf);
358     g_value_init (&value, GST_TYPE_BUFFER);
359     gst_value_set_buffer (&value, new_segment_buf);
360     gst_value_array_append_value (&array, &value);
361     g_value_unset (&value);
362     gst_buffer_unref (new_segment_buf);
363
364     if (this->tag_buf) {
365       gst_gdp_stamp_buffer (this, this->tag_buf);
366       GST_DEBUG_OBJECT (this, "1.0, appending current tags buffer %p",
367           this->tag_buf);
368       tag_buf = this->tag_buf;
369       this->tag_buf = NULL;
370
371       g_value_init (&value, GST_TYPE_BUFFER);
372       gst_value_set_buffer (&value, tag_buf);
373       gst_value_array_append_value (&array, &value);
374       g_value_unset (&value);
375       gst_buffer_unref (tag_buf);
376     }
377   }
378
379   gst_gdp_stamp_buffer (this, this->caps_buf);
380   GST_DEBUG_OBJECT (this, "appending copy of caps buffer %p", this->caps_buf);
381   caps_buf = gst_buffer_copy (this->caps_buf);
382   g_value_init (&value, GST_TYPE_BUFFER);
383   gst_value_set_buffer (&value, caps_buf);
384   gst_value_array_append_value (&array, &value);
385   g_value_unset (&value);
386   gst_buffer_unref (caps_buf);
387
388   /* we also need to add GDP serializations of the streamheaders of the
389    * incoming caps */
390   structure = gst_caps_get_structure (this->caps, 0);
391   if (gst_structure_has_field (structure, "streamheader")) {
392     const GValue *sh;
393
394     GArray *buffers;
395
396     GstBuffer *buffer;
397
398     int i;
399
400     sh = gst_structure_get_value (structure, "streamheader");
401     buffers = g_value_peek_pointer (sh);
402     GST_DEBUG_OBJECT (this,
403         "Need to serialize %d incoming streamheader buffers on ours",
404         buffers->len);
405     for (i = 0; i < buffers->len; ++i) {
406       GValue *bufval;
407
408       GstBuffer *outbuffer;
409
410       bufval = &g_array_index (buffers, GValue, i);
411       buffer = g_value_peek_pointer (bufval);
412       /* this buffer is deserialized by gdpdepay as a regular buffer,
413          it needs HEADER, because it's a streamheader - otherwise it
414          is mixed with regular data buffers */
415       GST_BUFFER_FLAG_SET (buffer, GST_BUFFER_FLAG_HEADER);
416       GST_BUFFER_OFFSET (buffer) = GST_BUFFER_OFFSET_NONE;
417       GST_BUFFER_OFFSET_END (buffer) = GST_BUFFER_OFFSET_NONE;
418       GST_BUFFER_TIMESTAMP (buffer) = GST_CLOCK_TIME_NONE;
419
420       outbuffer = gst_gdp_pay_buffer_from_buffer (this, buffer);
421       if (!outbuffer) {
422         g_value_unset (&array);
423         goto no_buffer;
424       }
425
426       /* Setting HEADER as other GDP event buffers */
427       GST_DEBUG_OBJECT (this,
428           "Setting HEADER flag on outgoing buffer %" GST_PTR_FORMAT, outbuffer);
429       GST_BUFFER_FLAG_SET (outbuffer, GST_BUFFER_FLAG_HEADER);
430       GST_BUFFER_OFFSET (outbuffer) = GST_BUFFER_OFFSET_NONE;
431       GST_BUFFER_OFFSET_END (outbuffer) = GST_BUFFER_OFFSET_NONE;
432       GST_BUFFER_TIMESTAMP (outbuffer) = GST_CLOCK_TIME_NONE;
433
434       g_value_init (&value, GST_TYPE_BUFFER);
435       gst_value_set_buffer (&value, outbuffer);
436       gst_value_array_append_value (&array, &value);
437       g_value_unset (&value);
438
439       gst_buffer_unref (outbuffer);
440     }
441   } else {
442     GST_DEBUG_OBJECT (this, "no streamheader to serialize");
443   }
444
445   GST_DEBUG_OBJECT (this, "%d serialized buffers on streamheaders",
446       gst_value_array_get_size (&array));
447   caps = gst_caps_from_string ("application/x-gdp");
448   structure = gst_caps_get_structure (caps, 0);
449
450   gst_structure_set_value (structure, "streamheader", &array);
451   g_value_unset (&array);
452
453   GST_DEBUG_OBJECT (this, "Setting caps on src pad %" GST_PTR_FORMAT, caps);
454   gst_pad_set_caps (this->srcpad, caps);
455
456   /* if these are our first ever buffers, send out new_segment first */
457   if (!this->sent_streamheader) {
458     GstEvent *event;
459     GstSegment segment;
460
461     gst_segment_init (&segment, GST_FORMAT_BYTES);
462     event = gst_event_new_segment (&segment);
463
464     GST_DEBUG_OBJECT (this, "Sending out new_segment event %p", event);
465     if (!gst_pad_push_event (this->srcpad, event)) {
466       GST_WARNING_OBJECT (this, "pushing new segment failed");
467       r = GST_FLOW_ERROR;
468       goto done;
469     }
470   }
471
472   /* push out these streamheader buffers, then flush our internal queue */
473   GST_DEBUG_OBJECT (this, "Pushing GDP new_segment buffer %p with offset %"
474       G_GINT64_FORMAT ", offset_end %" G_GINT64_FORMAT, this->new_segment_buf,
475       GST_BUFFER_OFFSET (this->new_segment_buf),
476       GST_BUFFER_OFFSET_END (this->new_segment_buf));
477   /* we stored these bufs with refcount 1, so make sure we keep a ref */
478   r = gst_pad_push (this->srcpad, gst_buffer_ref (this->new_segment_buf));
479   if (r != GST_FLOW_OK) {
480     GST_WARNING_OBJECT (this, "pushing GDP newsegment buffer returned %d", r);
481     goto done;
482   }
483   if (this->tag_buf) {
484     GST_DEBUG_OBJECT (this, "Pushing GDP tag buffer %p", this->tag_buf);
485     /* we stored these bufs with refcount 1, so make sure we keep a ref */
486     r = gst_pad_push (this->srcpad, gst_buffer_ref (this->tag_buf));
487     if (r != GST_FLOW_OK) {
488       GST_WARNING_OBJECT (this, "pushing GDP tag buffer returned %d", r);
489       goto done;
490     }
491   }
492   GST_DEBUG_OBJECT (this, "Pushing GDP caps buffer %p", this->caps_buf);
493   r = gst_pad_push (this->srcpad, gst_buffer_ref (this->caps_buf));
494   if (r != GST_FLOW_OK) {
495     GST_WARNING_OBJECT (this, "pushing GDP caps buffer returned %d", r);
496     goto done;
497   }
498   this->sent_streamheader = TRUE;
499   GST_DEBUG_OBJECT (this, "need to push %d queued buffers",
500       g_list_length (this->queue));
501   while (this->queue) {
502     GstBuffer *buffer;
503
504     buffer = GST_BUFFER_CAST (this->queue->data);
505     GST_DEBUG_OBJECT (this, "Pushing queued GDP buffer %p", buffer);
506
507     /* delete buffer from queue now */
508     this->queue = g_list_delete_link (this->queue, this->queue);
509
510     /* set caps and push */
511     r = gst_pad_push (this->srcpad, buffer);
512     if (r != GST_FLOW_OK) {
513       GST_WARNING_OBJECT (this, "pushing queued GDP buffer returned %d", r);
514       goto done;
515     }
516   }
517
518 done:
519   gst_caps_unref (caps);
520   GST_DEBUG_OBJECT (this, "stop");
521   return r;
522
523   /* ERRORS */
524 no_buffer:
525   {
526     GST_ELEMENT_ERROR (this, STREAM, FORMAT, (NULL),
527         ("failed to create GDP buffer from streamheader"));
528     return GST_FLOW_ERROR;
529   }
530 }
531
532 /* queue a buffer internally if we haven't sent streamheader buffers yet;
533  * otherwise, just push on, this takes ownership of the buffer. */
534 static GstFlowReturn
535 gst_gdp_queue_buffer (GstGDPPay * this, GstBuffer * buffer)
536 {
537   if (this->sent_streamheader) {
538     GST_LOG_OBJECT (this, "Pushing GDP buffer %p, caps %" GST_PTR_FORMAT,
539         buffer, this->caps);
540     return gst_pad_push (this->srcpad, buffer);
541   }
542
543   /* store it on an internal queue. buffer remains reffed. */
544   this->queue = g_list_append (this->queue, buffer);
545   GST_DEBUG_OBJECT (this, "streamheader not sent yet, "
546       "queued buffer %p, now %d buffers queued",
547       buffer, g_list_length (this->queue));
548
549   gst_gdp_pay_reset_streamheader (this);
550
551   return GST_FLOW_OK;
552 }
553
554 static GstFlowReturn
555 gst_gdp_pay_chain (GstPad * pad, GstObject * parent, GstBuffer * buffer)
556 {
557   GstGDPPay *this;
558 #if 0
559   GstCaps *caps;
560 #endif
561   GstBuffer *outbuffer;
562   GstFlowReturn ret;
563
564   this = GST_GDP_PAY (parent);
565
566   /* we should have received a new_segment before, otherwise it's a bug.
567    * fake one in that case */
568   if (!this->new_segment_buf) {
569     GstEvent *event;
570     GstSegment segment;
571
572     GST_WARNING_OBJECT (this,
573         "did not receive new-segment before first buffer");
574     gst_segment_init (&segment, GST_FORMAT_BYTES);
575     event = gst_event_new_segment (&segment);
576     outbuffer = gst_gdp_buffer_from_event (this, event);
577     gst_event_unref (event);
578
579     /* GDP 0.2 doesn't know about new-segment, so this is not fatal */
580     if (!outbuffer) {
581       GST_ELEMENT_WARNING (this, STREAM, ENCODE, (NULL),
582           ("Could not create GDP buffer from new segment event"));
583     } else {
584       GST_BUFFER_TIMESTAMP (outbuffer) = GST_BUFFER_TIMESTAMP (buffer);
585       GST_BUFFER_DURATION (outbuffer) = 0;
586       GST_BUFFER_FLAG_SET (outbuffer, GST_BUFFER_FLAG_HEADER);
587       GST_DEBUG_OBJECT (this, "Storing buffer %p as new_segment_buf",
588           outbuffer);
589       this->new_segment_buf = outbuffer;
590     }
591   }
592   /* make sure we've received caps before */
593   if (!this->caps)
594     goto no_caps;
595
596   /* create a GDP header packet,
597    * then create a GST buffer of the header packet and the buffer contents */
598   outbuffer = gst_gdp_pay_buffer_from_buffer (this, buffer);
599   if (!outbuffer)
600     goto no_buffer;
601
602   /* If the incoming buffer is HEADER, that means we have it on the caps
603    * as streamheader, and we have serialized a GDP version of it and put it
604    * on our caps */
605   if (GST_BUFFER_FLAG_IS_SET (buffer, GST_BUFFER_FLAG_HEADER)) {
606     GST_DEBUG_OBJECT (this, "Setting HEADER flag on outgoing buffer %p",
607         outbuffer);
608     GST_BUFFER_FLAG_SET (outbuffer, GST_BUFFER_FLAG_HEADER);
609   }
610
611   gst_gdp_stamp_buffer (this, outbuffer);
612   GST_BUFFER_TIMESTAMP (outbuffer) = GST_BUFFER_TIMESTAMP (buffer);
613   GST_BUFFER_DURATION (outbuffer) = GST_BUFFER_DURATION (buffer);
614
615   ret = gst_gdp_queue_buffer (this, outbuffer);
616
617 done:
618   gst_buffer_unref (buffer);
619
620   return ret;
621
622   /* ERRORS */
623 no_caps:
624   {
625     /* when returning a fatal error as a GstFlowReturn we must post an error
626      * message */
627     GST_ELEMENT_ERROR (this, STREAM, FORMAT, (NULL),
628         ("first received buffer does not have caps set"));
629     ret = GST_FLOW_NOT_NEGOTIATED;
630     goto done;
631   }
632 #if 0
633 no_caps_buffer:
634   {
635     GST_ELEMENT_ERROR (this, STREAM, ENCODE, (NULL),
636         ("Could not create GDP buffer from caps %" GST_PTR_FORMAT, caps));
637     gst_caps_unref (caps);
638     ret = GST_FLOW_ERROR;
639     goto done;
640   }
641 #endif
642 no_buffer:
643   {
644     GST_ELEMENT_ERROR (this, STREAM, ENCODE, (NULL),
645         ("Could not create GDP buffer from buffer"));
646     ret = GST_FLOW_ERROR;
647     goto done;
648   }
649 }
650
651 static gboolean
652 gst_gdp_pay_sink_event (GstPad * pad, GstObject * parent, GstEvent * event)
653 {
654   GstBuffer *outbuffer;
655   GstGDPPay *this = GST_GDP_PAY (parent);
656   GstFlowReturn flowret;
657   GstCaps *caps;
658   gboolean ret = TRUE;
659
660   GST_DEBUG_OBJECT (this, "received event %p of type %s (%d)",
661       event, gst_event_type_get_name (event->type), event->type);
662
663   /* now turn the event into a buffer */
664   outbuffer = gst_gdp_buffer_from_event (this, event);
665   if (!outbuffer)
666     goto no_outbuffer;
667
668   GST_BUFFER_TIMESTAMP (outbuffer) = GST_EVENT_TIMESTAMP (event);
669   GST_BUFFER_DURATION (outbuffer) = 0;
670
671   /* if we got a new segment or tag event, we should put it on our streamheader,
672    * and not send it on */
673   switch (GST_EVENT_TYPE (event)) {
674     case GST_EVENT_SEGMENT:
675       GST_DEBUG_OBJECT (this, "Storing in caps buffer %p as new_segment_buf",
676           outbuffer);
677
678       if (this->new_segment_buf)
679         gst_buffer_unref (this->new_segment_buf);
680       this->new_segment_buf = outbuffer;
681
682       GST_BUFFER_FLAG_SET (outbuffer, GST_BUFFER_FLAG_HEADER);
683       gst_gdp_pay_reset_streamheader (this);
684       break;
685     case GST_EVENT_CAPS:{
686       gst_event_parse_caps (event, &caps);
687       if (this->caps == NULL || !gst_caps_is_equal (this->caps, caps)) {
688         GST_INFO_OBJECT (pad, "caps changed to %" GST_PTR_FORMAT, caps);
689         gst_caps_replace (&this->caps, caps);
690         outbuffer = gst_gdp_buffer_from_caps (this, caps);
691         if (outbuffer == NULL)
692           goto no_buffer_from_caps;
693
694         GST_BUFFER_DURATION (outbuffer) = 0;
695         GST_BUFFER_FLAG_SET (outbuffer, GST_BUFFER_FLAG_HEADER);
696         if (this->caps_buf)
697           gst_buffer_unref (this->caps_buf);
698         this->caps_buf = outbuffer;
699         gst_gdp_pay_reset_streamheader (this);
700       }
701       break;
702     }
703     case GST_EVENT_TAG:
704       GST_DEBUG_OBJECT (this, "Storing in caps buffer %p as tag_buf",
705           outbuffer);
706
707       if (this->tag_buf)
708         gst_buffer_unref (this->tag_buf);
709       this->tag_buf = outbuffer;
710
711       GST_BUFFER_FLAG_SET (outbuffer, GST_BUFFER_FLAG_HEADER);
712       gst_gdp_pay_reset_streamheader (this);
713       break;
714     default:
715       GST_DEBUG_OBJECT (this, "queuing GDP buffer %p of event %p", outbuffer,
716           event);
717       flowret = gst_gdp_queue_buffer (this, outbuffer);
718       if (flowret != GST_FLOW_OK)
719         goto push_error;
720       break;
721   }
722
723   /* if we have EOS, we should send on EOS ourselves */
724   if (GST_EVENT_TYPE (event) == GST_EVENT_EOS) {
725     GST_DEBUG_OBJECT (this, "Sending on EOS event %p", event);
726     /* ref, we unref later again */
727     ret = gst_pad_push_event (this->srcpad, gst_event_ref (event));
728   }
729
730 done:
731   gst_event_unref (event);
732
733   return ret;
734
735   /* ERRORS */
736 no_outbuffer:
737   {
738     GST_ELEMENT_WARNING (this, STREAM, ENCODE, (NULL),
739         ("Could not create GDP buffer from received event (type %s)",
740             gst_event_type_get_name (event->type)));
741     ret = FALSE;
742     goto done;
743   }
744 no_buffer_from_caps:
745   {
746     GST_ELEMENT_ERROR (this, STREAM, ENCODE, (NULL),
747         ("Could not create GDP buffer from caps %" GST_PTR_FORMAT, caps));
748     ret = FALSE;
749     goto done;
750   }
751 push_error:
752   {
753     GST_WARNING_OBJECT (this, "queueing GDP event buffer returned %d", flowret);
754     ret = FALSE;
755     goto done;
756   }
757 }
758
759 static gboolean
760 gst_gdp_pay_src_event (GstPad * pad, GstObject * parent, GstEvent * event)
761 {
762   GstGDPPay *this;
763   gboolean res = TRUE;
764
765   this = GST_GDP_PAY (parent);
766
767   switch (GST_EVENT_TYPE (event)) {
768     case GST_EVENT_SEEK:
769       /* we refuse seek for now. */
770       gst_event_unref (event);
771       res = FALSE;
772       break;
773     case GST_EVENT_QOS:
774     case GST_EVENT_NAVIGATION:
775     default:
776       /* everything else is passed */
777       res = gst_pad_push_event (this->sinkpad, event);
778       break;
779   }
780
781   return res;
782 }
783
784 static void
785 gst_gdp_pay_set_property (GObject * object, guint prop_id,
786     const GValue * value, GParamSpec * pspec)
787 {
788   GstGDPPay *this;
789
790   g_return_if_fail (GST_IS_GDP_PAY (object));
791   this = GST_GDP_PAY (object);
792
793   switch (prop_id) {
794     case PROP_CRC_HEADER:
795       this->crc_header =
796           g_value_get_boolean (value) ? GST_DP_HEADER_FLAG_CRC_HEADER : 0;
797       this->header_flag = this->crc_header | this->crc_payload;
798       break;
799     case PROP_CRC_PAYLOAD:
800       this->crc_payload =
801           g_value_get_boolean (value) ? GST_DP_HEADER_FLAG_CRC_PAYLOAD : 0;
802       this->header_flag = this->crc_header | this->crc_payload;
803       break;
804     case PROP_VERSION:
805       this->version = g_value_get_enum (value);
806       break;
807     default:
808       G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
809       break;
810   }
811 }
812
813 static void
814 gst_gdp_pay_get_property (GObject * object, guint prop_id,
815     GValue * value, GParamSpec * pspec)
816 {
817   GstGDPPay *this;
818
819   g_return_if_fail (GST_IS_GDP_PAY (object));
820   this = GST_GDP_PAY (object);
821
822   switch (prop_id) {
823     case PROP_CRC_HEADER:
824       g_value_set_boolean (value, this->crc_header);
825       break;
826     case PROP_CRC_PAYLOAD:
827       g_value_set_boolean (value, this->crc_payload);
828       break;
829     case PROP_VERSION:
830       g_value_set_enum (value, this->version);
831       break;
832     default:
833       G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
834       break;
835   }
836 }
837
838 static GstStateChangeReturn
839 gst_gdp_pay_change_state (GstElement * element, GstStateChange transition)
840 {
841   GstStateChangeReturn ret;
842   GstGDPPay *this = GST_GDP_PAY (element);
843
844   switch (transition) {
845     case GST_STATE_CHANGE_READY_TO_PAUSED:
846       break;
847     default:
848       break;
849   }
850
851   ret = GST_ELEMENT_CLASS (parent_class)->change_state (element, transition);
852
853   switch (transition) {
854     case GST_STATE_CHANGE_PAUSED_TO_READY:
855       gst_gdp_pay_reset (this);
856       break;
857     default:
858       break;
859   }
860
861   return ret;
862 }
863
864 gboolean
865 gst_gdp_pay_plugin_init (GstPlugin * plugin)
866 {
867   if (!gst_element_register (plugin, "gdppay", GST_RANK_NONE, GST_TYPE_GDP_PAY))
868     return FALSE;
869
870   return TRUE;
871 }