2 * Copyright (C) <2006> Wim Taymans <wim.taymans@gmail.com>
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.
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.
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.
26 #include <gst/rtp/gstrtpbuffer.h>
29 #include "gstrtptheorapay.h"
31 #define THEORA_ID_LEN 42
33 GST_DEBUG_CATEGORY_STATIC (rtptheorapay_debug);
34 #define GST_CAT_DEFAULT (rtptheorapay_debug)
37 * http://svn.xiph.org/trunk/theora/doc/draft-ietf-avt-rtp-theora-01.txt
40 static GstStaticPadTemplate gst_rtp_theora_pay_src_template =
41 GST_STATIC_PAD_TEMPLATE ("src",
44 GST_STATIC_CAPS ("application/x-rtp, "
45 "media = (string) \"video\", "
46 "payload = (int) " GST_RTP_PAYLOAD_DYNAMIC_STRING ", "
47 "clock-rate = (int) 90000, " "encoding-name = (string) \"THEORA\""
48 /* All required parameters
50 * "sampling = (string) { "YCbCr-4:2:0", "YCbCr-4:2:2", "YCbCr-4:4:4" } "
51 * "width = (string) [1, 1048561] (multiples of 16) "
52 * "height = (string) [1, 1048561] (multiples of 16) "
53 * "configuration = (string) ANY"
55 /* All optional parameters
57 * "configuration-uri ="
58 * "delivery-method = (string) { inline, in_band, out_band/<specific_name> } "
63 static GstStaticPadTemplate gst_rtp_theora_pay_sink_template =
64 GST_STATIC_PAD_TEMPLATE ("sink",
67 GST_STATIC_CAPS ("video/x-theora")
70 #define DEFAULT_CONFIG_INTERVAL 0
78 #define gst_rtp_theora_pay_parent_class parent_class
79 G_DEFINE_TYPE (GstRtpTheoraPay, gst_rtp_theora_pay, GST_TYPE_RTP_BASE_PAYLOAD);
81 static gboolean gst_rtp_theora_pay_setcaps (GstRTPBasePayload * basepayload,
83 static GstStateChangeReturn gst_rtp_theora_pay_change_state (GstElement *
84 element, GstStateChange transition);
85 static GstFlowReturn gst_rtp_theora_pay_handle_buffer (GstRTPBasePayload * pad,
87 static gboolean gst_rtp_theora_pay_sink_event (GstRTPBasePayload * payload,
91 static void gst_rtp_theora_pay_set_property (GObject * object, guint prop_id,
92 const GValue * value, GParamSpec * pspec);
93 static void gst_rtp_theora_pay_get_property (GObject * object, guint prop_id,
94 GValue * value, GParamSpec * pspec);
97 gst_rtp_theora_pay_class_init (GstRtpTheoraPayClass * klass)
99 GObjectClass *gobject_class;
100 GstElementClass *gstelement_class;
101 GstRTPBasePayloadClass *gstrtpbasepayload_class;
103 gobject_class = (GObjectClass *) klass;
104 gstelement_class = (GstElementClass *) klass;
105 gstrtpbasepayload_class = (GstRTPBasePayloadClass *) klass;
107 gstelement_class->change_state = gst_rtp_theora_pay_change_state;
109 gstrtpbasepayload_class->set_caps = gst_rtp_theora_pay_setcaps;
110 gstrtpbasepayload_class->handle_buffer = gst_rtp_theora_pay_handle_buffer;
111 gstrtpbasepayload_class->sink_event = gst_rtp_theora_pay_sink_event;
113 gobject_class->set_property = gst_rtp_theora_pay_set_property;
114 gobject_class->get_property = gst_rtp_theora_pay_get_property;
116 gst_element_class_add_pad_template (gstelement_class,
117 gst_static_pad_template_get (&gst_rtp_theora_pay_src_template));
118 gst_element_class_add_pad_template (gstelement_class,
119 gst_static_pad_template_get (&gst_rtp_theora_pay_sink_template));
121 gst_element_class_set_static_metadata (gstelement_class,
122 "RTP Theora payloader", "Codec/Payloader/Network/RTP",
123 "Payload-encode Theora video into RTP packets (draft-01 RFC XXXX)",
124 "Wim Taymans <wim.taymans@gmail.com>");
126 GST_DEBUG_CATEGORY_INIT (rtptheorapay_debug, "rtptheorapay", 0,
127 "Theora RTP Payloader");
129 g_object_class_install_property (G_OBJECT_CLASS (klass), PROP_CONFIG_INTERVAL,
130 g_param_spec_uint ("config-interval", "Config Send Interval",
131 "Send Config Insertion Interval in seconds (configuration headers "
132 "will be multiplexed in the data stream when detected.) (0 = disabled)",
133 0, 3600, DEFAULT_CONFIG_INTERVAL,
134 G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS)
139 gst_rtp_theora_pay_init (GstRtpTheoraPay * rtptheorapay)
141 rtptheorapay->last_config = GST_CLOCK_TIME_NONE;
145 gst_rtp_theora_pay_clear_packet (GstRtpTheoraPay * rtptheorapay)
147 if (rtptheorapay->packet)
148 gst_buffer_unref (rtptheorapay->packet);
149 rtptheorapay->packet = NULL;
153 gst_rtp_theora_pay_cleanup (GstRtpTheoraPay * rtptheorapay)
155 g_list_foreach (rtptheorapay->headers, (GFunc) gst_mini_object_unref, NULL);
156 g_list_free (rtptheorapay->headers);
157 rtptheorapay->headers = NULL;
159 gst_rtp_theora_pay_clear_packet (rtptheorapay);
161 if (rtptheorapay->config_data)
162 g_free (rtptheorapay->config_data);
163 rtptheorapay->config_data = NULL;
164 rtptheorapay->last_config = GST_CLOCK_TIME_NONE;
168 gst_rtp_theora_pay_setcaps (GstRTPBasePayload * basepayload, GstCaps * caps)
170 GstRtpTheoraPay *rtptheorapay;
172 rtptheorapay = GST_RTP_THEORA_PAY (basepayload);
174 rtptheorapay->need_headers = TRUE;
180 gst_rtp_theora_pay_reset_packet (GstRtpTheoraPay * rtptheorapay, guint8 TDT)
183 GstRTPBuffer rtp = { NULL };
185 GST_DEBUG_OBJECT (rtptheorapay, "reset packet");
187 rtptheorapay->payload_pos = 4;
188 gst_rtp_buffer_map (rtptheorapay->packet, GST_MAP_READ, &rtp);
189 payload_len = gst_rtp_buffer_get_payload_len (&rtp);
190 gst_rtp_buffer_unmap (&rtp);
191 rtptheorapay->payload_left = payload_len - 4;
192 rtptheorapay->payload_duration = 0;
193 rtptheorapay->payload_F = 0;
194 rtptheorapay->payload_TDT = TDT;
195 rtptheorapay->payload_pkts = 0;
199 gst_rtp_theora_pay_init_packet (GstRtpTheoraPay * rtptheorapay, guint8 TDT,
200 GstClockTime timestamp)
202 GST_DEBUG_OBJECT (rtptheorapay, "starting new packet, TDT: %d", TDT);
204 if (rtptheorapay->packet)
205 gst_buffer_unref (rtptheorapay->packet);
207 /* new packet allocate max packet size */
208 rtptheorapay->packet =
209 gst_rtp_buffer_new_allocate_len (GST_RTP_BASE_PAYLOAD_MTU
210 (rtptheorapay), 0, 0);
211 gst_rtp_theora_pay_reset_packet (rtptheorapay, TDT);
213 GST_BUFFER_TIMESTAMP (rtptheorapay->packet) = timestamp;
217 gst_rtp_theora_pay_flush_packet (GstRtpTheoraPay * rtptheorapay)
222 GstRTPBuffer rtp = { NULL };
224 /* check for empty packet */
225 if (!rtptheorapay->packet || rtptheorapay->payload_pos <= 4)
228 GST_DEBUG_OBJECT (rtptheorapay, "flushing packet");
230 gst_rtp_buffer_map (rtptheorapay->packet, GST_MAP_WRITE, &rtp);
233 payload = gst_rtp_buffer_get_payload (&rtp);
236 * 0 1 2 3 4 5 6 7 8 9 0 1 2 3 4 5 6 7 8 9 0 1 2 3 4 5 6 7 8 9 0 1
237 * +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
238 * | Ident | F |TDT|# pkts.|
239 * +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
241 * F: Fragment type (0=none, 1=start, 2=cont, 3=end)
242 * TDT: Theora data type (0=theora, 1=config, 2=comment, 3=reserved)
243 * pkts: number of packets.
245 payload[0] = (rtptheorapay->payload_ident >> 16) & 0xff;
246 payload[1] = (rtptheorapay->payload_ident >> 8) & 0xff;
247 payload[2] = (rtptheorapay->payload_ident) & 0xff;
248 payload[3] = (rtptheorapay->payload_F & 0x3) << 6 |
249 (rtptheorapay->payload_TDT & 0x3) << 4 |
250 (rtptheorapay->payload_pkts & 0xf);
252 gst_rtp_buffer_unmap (&rtp);
254 /* shrink the buffer size to the last written byte */
255 hlen = gst_rtp_buffer_calc_header_len (0);
256 gst_buffer_resize (rtptheorapay->packet, 0, hlen + rtptheorapay->payload_pos);
258 GST_BUFFER_DURATION (rtptheorapay->packet) = rtptheorapay->payload_duration;
260 /* push, this gives away our ref to the packet, so clear it. */
262 gst_rtp_base_payload_push (GST_RTP_BASE_PAYLOAD (rtptheorapay),
263 rtptheorapay->packet);
264 rtptheorapay->packet = NULL;
270 gst_rtp_theora_pay_finish_headers (GstRTPBasePayload * basepayload)
272 GstRtpTheoraPay *rtptheorapay = GST_RTP_THEORA_PAY (basepayload);
274 guint length, size, n_headers, configlen, extralen;
275 gchar *wstr, *hstr, *configuration;
276 guint8 *data, *config;
280 GST_DEBUG_OBJECT (rtptheorapay, "finish headers");
282 if (!rtptheorapay->headers) {
283 GST_DEBUG_OBJECT (rtptheorapay, "We need 2 headers but have none");
287 /* +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
288 * | Number of packed headers |
289 * +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
290 * +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
292 * +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
293 * +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
295 * +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
296 * +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
298 * +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
300 * We only construct a config containing 1 packed header like this:
303 * 0 1 2 3 4 5 6 7 8 9 0 1 2 3 4 5 6 7 8 9 0 1 2 3 4 5 6 7 8 9 0 1
304 * +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
305 * | Ident | length ..
306 * +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
307 * .. | n. of headers | length1 | length2 ..
308 * +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
309 * .. | Identification Header ..
310 * +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
311 * .................................................................
312 * +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
313 * .. | Comment Header ..
314 * +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
315 * .................................................................
316 * +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
317 * .. Comment Header |
318 * +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
320 * +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
321 * .................................................................
322 * +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
324 * +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
327 /* we need 4 bytes for the number of headers (which is always 1), 3 bytes for
328 * the ident, 2 bytes for length, 1 byte for n. of headers. */
329 size = 4 + 3 + 2 + 1;
331 /* count the size of the headers first and update the hash */
334 ident = fnv1_hash_32_new ();
336 for (walk = rtptheorapay->headers; walk; walk = g_list_next (walk)) {
337 GstBuffer *buf = GST_BUFFER_CAST (walk->data);
341 bsize = gst_buffer_get_size (buf);
345 /* count number of bytes needed for length fields, we don't need this for
346 * the last header. */
347 if (g_list_next (walk)) {
355 gst_buffer_map (buf, &map, GST_MAP_READ);
356 ident = fnv1_hash_32_update (ident, map.data, map.size);
357 gst_buffer_unmap (buf, &map);
360 /* packet length is header size + packet length */
361 configlen = size + length;
362 config = data = g_malloc (configlen);
364 /* number of packed headers, we only pack 1 header */
370 ident = fnv1_hash_32_to_24 (ident);
371 rtptheorapay->payload_ident = ident;
372 GST_DEBUG_OBJECT (rtptheorapay, "ident 0x%08x", ident);
374 /* take lower 3 bytes */
375 data[4] = (ident >> 16) & 0xff;
376 data[5] = (ident >> 8) & 0xff;
377 data[6] = ident & 0xff;
379 /* store length of all theora headers */
380 data[7] = ((length) >> 8) & 0xff;
381 data[8] = (length) & 0xff;
383 /* store number of headers minus one. */
384 data[9] = n_headers - 1;
387 /* store length for each header */
388 for (walk = rtptheorapay->headers; walk; walk = g_list_next (walk)) {
389 GstBuffer *buf = GST_BUFFER_CAST (walk->data);
391 guint bsize, size, temp;
394 /* only need to store the length when it's not the last header */
395 if (!g_list_next (walk))
398 bsize = gst_buffer_get_size (buf);
408 bsize = gst_buffer_get_size (buf);
409 /* write the size backwards */
413 data[size] = (bsize & 0x7f) | flag;
420 /* copy header data */
421 for (walk = rtptheorapay->headers; walk; walk = g_list_next (walk)) {
422 GstBuffer *buf = GST_BUFFER_CAST (walk->data);
425 gst_buffer_map (buf, &map, GST_MAP_READ);
426 memcpy (data, map.data, map.size);
427 gst_buffer_unmap (buf, &map);
431 /* serialize to base64 */
432 configuration = g_base64_encode (config, configlen);
434 /* store for later re-sending */
435 rtptheorapay->config_size = configlen - 4 - 3 - 2;
436 rtptheorapay->config_data = g_malloc (rtptheorapay->config_size);
437 rtptheorapay->config_extra_len = extralen;
438 memcpy (rtptheorapay->config_data, config + 4 + 3 + 2,
439 rtptheorapay->config_size);
443 /* configure payloader settings */
444 wstr = g_strdup_printf ("%d", rtptheorapay->width);
445 hstr = g_strdup_printf ("%d", rtptheorapay->height);
446 gst_rtp_base_payload_set_options (basepayload, "video", TRUE, "THEORA",
449 gst_rtp_base_payload_set_outcaps (basepayload, "sampling", G_TYPE_STRING,
450 "YCbCr-4:2:0", "width", G_TYPE_STRING, wstr, "height", G_TYPE_STRING,
451 hstr, "configuration", G_TYPE_STRING, configuration, "delivery-method",
452 G_TYPE_STRING, "inline",
453 /* don't set the other defaults
458 g_free (configuration);
465 GST_DEBUG_OBJECT (rtptheorapay, "finish headers");
471 gst_rtp_theora_pay_parse_id (GstRTPBasePayload * basepayload, guint8 * data,
474 GstRtpTheoraPay *rtptheorapay;
477 rtptheorapay = GST_RTP_THEORA_PAY (basepayload);
479 if (G_UNLIKELY (size < 42))
482 if (G_UNLIKELY (memcmp (data, "\200theora", 7)))
486 if (G_UNLIKELY (data[0] != 3))
487 goto invalid_version;
488 if (G_UNLIKELY (data[1] != 2))
489 goto invalid_version;
492 width = GST_READ_UINT16_BE (data) << 4;
494 height = GST_READ_UINT16_BE (data) << 4;
497 /* FIXME, parse pixel format */
500 rtptheorapay->width = width;
501 rtptheorapay->height = height;
508 GST_ELEMENT_ERROR (basepayload, STREAM, DECODE,
510 ("Identification packet is too short, need at least 42, got %d", size));
515 GST_ELEMENT_ERROR (basepayload, STREAM, DECODE,
516 (NULL), ("Invalid header start in identification packet"));
521 GST_ELEMENT_ERROR (basepayload, STREAM, DECODE,
522 (NULL), ("Invalid version"));
528 gst_rtp_theora_pay_payload_buffer (GstRtpTheoraPay * rtptheorapay, guint8 TDT,
529 guint8 * data, guint size, GstClockTime timestamp, GstClockTime duration,
532 GstFlowReturn ret = GST_FLOW_OK;
535 GstClockTime newduration;
538 guint8 *ppos, *payload;
540 GstRTPBuffer rtp = { NULL };
542 /* size increases with packet length and 2 bytes size eader. */
543 newduration = rtptheorapay->payload_duration;
544 if (duration != GST_CLOCK_TIME_NONE)
545 newduration += duration;
547 newsize = rtptheorapay->payload_pos + 2 + size;
548 packet_len = gst_rtp_buffer_calc_packet_len (newsize, 0, 0);
550 /* check buffer filled against length and max latency */
551 flush = gst_rtp_base_payload_is_filled (GST_RTP_BASE_PAYLOAD (rtptheorapay),
552 packet_len, newduration);
553 /* we can store up to 15 theora packets in one RTP packet. */
554 flush |= (rtptheorapay->payload_pkts == 15);
555 /* flush if we have a new TDT */
556 if (rtptheorapay->packet)
557 flush |= (rtptheorapay->payload_TDT != TDT);
559 ret = gst_rtp_theora_pay_flush_packet (rtptheorapay);
561 /* create new packet if we must */
562 if (!rtptheorapay->packet) {
563 gst_rtp_theora_pay_init_packet (rtptheorapay, TDT, timestamp);
566 gst_rtp_buffer_map (rtptheorapay->packet, GST_MAP_WRITE, &rtp);
567 payload = gst_rtp_buffer_get_payload (&rtp);
568 ppos = payload + rtptheorapay->payload_pos;
571 /* put buffer in packet, it either fits completely or needs to be fragmented
572 * over multiple RTP packets. */
574 plen = MIN (rtptheorapay->payload_left - 2, size);
576 GST_DEBUG_OBJECT (rtptheorapay, "append %u bytes", plen);
578 /* data is copied in the payload with a 2 byte length header */
579 ppos[0] = ((plen - not_in_length) >> 8) & 0xff;
580 ppos[1] = ((plen - not_in_length) & 0xff);
582 memcpy (&ppos[2], data, plen);
584 /* only first (only) configuration cuts length field */
585 /* NOTE: spec (if any) is not clear on this ... */
591 rtptheorapay->payload_pos += plen + 2;
592 rtptheorapay->payload_left -= plen + 2;
596 /* last fragment, set F to 0x3. */
597 rtptheorapay->payload_F = 0x3;
599 /* fragment continues, set F to 0x2. */
600 rtptheorapay->payload_F = 0x2;
603 /* fragmented packet starts, set F to 0x1, mark ourselves as
605 rtptheorapay->payload_F = 0x1;
610 /* fragmented packets are always flushed and have ptks of 0 */
611 rtptheorapay->payload_pkts = 0;
612 gst_rtp_buffer_unmap (&rtp);
613 ret = gst_rtp_theora_pay_flush_packet (rtptheorapay);
616 /* start new packet and get pointers. TDT stays the same. */
617 gst_rtp_theora_pay_init_packet (rtptheorapay,
618 rtptheorapay->payload_TDT, timestamp);
619 gst_rtp_buffer_map (rtptheorapay->packet, GST_MAP_WRITE, &rtp);
620 payload = gst_rtp_buffer_get_payload (&rtp);
621 ppos = payload + rtptheorapay->payload_pos;
624 /* unfragmented packet, update stats for next packet, size == 0 and we
625 * exit the while loop */
626 rtptheorapay->payload_pkts++;
627 if (duration != GST_CLOCK_TIME_NONE)
628 rtptheorapay->payload_duration += duration;
633 gst_rtp_buffer_unmap (&rtp);
639 gst_rtp_theora_pay_handle_buffer (GstRTPBasePayload * basepayload,
642 GstRtpTheoraPay *rtptheorapay;
647 GstClockTime duration, timestamp;
649 gboolean keyframe = FALSE;
651 rtptheorapay = GST_RTP_THEORA_PAY (basepayload);
653 gst_buffer_map (buffer, &map, GST_MAP_READ);
656 duration = GST_BUFFER_DURATION (buffer);
657 timestamp = GST_BUFFER_TIMESTAMP (buffer);
659 GST_DEBUG_OBJECT (rtptheorapay, "size %" G_GSIZE_FORMAT
660 ", duration %" GST_TIME_FORMAT, size, GST_TIME_ARGS (duration));
662 if (G_UNLIKELY (size > 0xffff))
665 /* find packet type */
669 } else if (data[0] & 0x80) {
671 if (data[0] == 0x80) {
672 /* identification, we need to parse this in order to get the clock rate.
674 if (G_UNLIKELY (!gst_rtp_theora_pay_parse_id (basepayload, data, size)))
675 goto parse_id_failed;
677 } else if (data[0] == 0x81) {
680 } else if (data[0] == 0x82) {
688 keyframe = ((data[0] & 0x40) == 0);
691 if (rtptheorapay->need_headers) {
692 /* we need to collect the headers and construct a config string from them */
694 GST_DEBUG_OBJECT (rtptheorapay, "collecting header, buffer %p", buffer);
695 /* append header to the list of headers */
696 gst_buffer_unmap (buffer, &map);
697 rtptheorapay->headers = g_list_append (rtptheorapay->headers, buffer);
701 if (!gst_rtp_theora_pay_finish_headers (basepayload))
703 rtptheorapay->need_headers = FALSE;
707 /* there is a config request, see if we need to insert it */
708 if (keyframe && (rtptheorapay->config_interval > 0) &&
709 rtptheorapay->config_data) {
710 gboolean send_config = FALSE;
712 if (rtptheorapay->last_config != -1) {
715 GST_LOG_OBJECT (rtptheorapay,
716 "now %" GST_TIME_FORMAT ", last VOP-I %" GST_TIME_FORMAT,
717 GST_TIME_ARGS (timestamp), GST_TIME_ARGS (rtptheorapay->last_config));
719 /* calculate diff between last config in milliseconds */
720 if (timestamp > rtptheorapay->last_config) {
721 diff = timestamp - rtptheorapay->last_config;
726 GST_DEBUG_OBJECT (rtptheorapay,
727 "interval since last config %" GST_TIME_FORMAT, GST_TIME_ARGS (diff));
729 /* bigger than interval, queue config */
730 /* FIXME should convert timestamps to running time */
731 if (GST_TIME_AS_SECONDS (diff) >= rtptheorapay->config_interval) {
732 GST_DEBUG_OBJECT (rtptheorapay, "time to send config");
736 /* no known previous config time, send now */
737 GST_DEBUG_OBJECT (rtptheorapay, "no previous config time, send now");
742 /* we need to send config now first */
743 /* different TDT type forces flush */
744 gst_rtp_theora_pay_payload_buffer (rtptheorapay, 1,
745 rtptheorapay->config_data, rtptheorapay->config_size,
746 timestamp, GST_CLOCK_TIME_NONE, rtptheorapay->config_extra_len);
748 if (timestamp != -1) {
749 rtptheorapay->last_config = timestamp;
754 ret = gst_rtp_theora_pay_payload_buffer (rtptheorapay, TDT, data, size,
755 timestamp, duration, 0);
757 gst_buffer_unmap (buffer, &map);
758 gst_buffer_unref (buffer);
766 GST_ELEMENT_WARNING (rtptheorapay, STREAM, DECODE,
767 ("Invalid packet size (%" G_GSIZE_FORMAT " <= 0xffff)", size), (NULL));
768 gst_buffer_unmap (buffer, &map);
769 gst_buffer_unref (buffer);
774 gst_buffer_unmap (buffer, &map);
775 gst_buffer_unref (buffer);
776 return GST_FLOW_ERROR;
780 GST_ELEMENT_WARNING (rtptheorapay, STREAM, DECODE,
781 (NULL), ("Ignoring unknown header received"));
782 gst_buffer_unmap (buffer, &map);
783 gst_buffer_unref (buffer);
788 GST_ELEMENT_WARNING (rtptheorapay, STREAM, DECODE,
789 (NULL), ("Error initializing header config"));
790 gst_buffer_unmap (buffer, &map);
791 gst_buffer_unref (buffer);
797 gst_rtp_theora_pay_sink_event (GstRTPBasePayload * payload, GstEvent * event)
799 GstRtpTheoraPay *rtptheorapay = GST_RTP_THEORA_PAY (payload);
801 switch (GST_EVENT_TYPE (event)) {
802 case GST_EVENT_FLUSH_STOP:
803 gst_rtp_theora_pay_clear_packet (rtptheorapay);
808 /* false to let parent handle event as well */
809 return GST_RTP_BASE_PAYLOAD_CLASS (parent_class)->sink_event (payload, event);
812 static GstStateChangeReturn
813 gst_rtp_theora_pay_change_state (GstElement * element,
814 GstStateChange transition)
816 GstRtpTheoraPay *rtptheorapay;
818 GstStateChangeReturn ret;
820 rtptheorapay = GST_RTP_THEORA_PAY (element);
822 switch (transition) {
827 ret = GST_ELEMENT_CLASS (parent_class)->change_state (element, transition);
829 switch (transition) {
830 case GST_STATE_CHANGE_PAUSED_TO_READY:
831 gst_rtp_theora_pay_cleanup (rtptheorapay);
840 gst_rtp_theora_pay_set_property (GObject * object, guint prop_id,
841 const GValue * value, GParamSpec * pspec)
843 GstRtpTheoraPay *rtptheorapay;
845 rtptheorapay = GST_RTP_THEORA_PAY (object);
848 case PROP_CONFIG_INTERVAL:
849 rtptheorapay->config_interval = g_value_get_uint (value);
857 gst_rtp_theora_pay_get_property (GObject * object, guint prop_id,
858 GValue * value, GParamSpec * pspec)
860 GstRtpTheoraPay *rtptheorapay;
862 rtptheorapay = GST_RTP_THEORA_PAY (object);
865 case PROP_CONFIG_INTERVAL:
866 g_value_set_uint (value, rtptheorapay->config_interval);
874 gst_rtp_theora_pay_plugin_init (GstPlugin * plugin)
876 return gst_element_register (plugin, "rtptheorapay",
877 GST_RANK_SECONDARY, GST_TYPE_RTP_THEORA_PAY);