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., 51 Franklin St, Fifth Floor,
17 * Boston, MA 02110-1301, USA.
26 #include <gst/rtp/gstrtpbuffer.h>
27 #include <gst/video/video.h>
30 #include "gstrtptheorapay.h"
31 #include "gstrtputils.h"
33 #define THEORA_ID_LEN 42
35 GST_DEBUG_CATEGORY_STATIC (rtptheorapay_debug);
36 #define GST_CAT_DEFAULT (rtptheorapay_debug)
39 * http://svn.xiph.org/trunk/theora/doc/draft-ietf-avt-rtp-theora-01.txt
42 static GstStaticPadTemplate gst_rtp_theora_pay_src_template =
43 GST_STATIC_PAD_TEMPLATE ("src",
46 GST_STATIC_CAPS ("application/x-rtp, "
47 "media = (string) \"video\", "
48 "payload = (int) " GST_RTP_PAYLOAD_DYNAMIC_STRING ", "
49 "clock-rate = (int) 90000, " "encoding-name = (string) \"THEORA\""
50 /* All required parameters
52 * "sampling = (string) { "YCbCr-4:2:0", "YCbCr-4:2:2", "YCbCr-4:4:4" } "
53 * "width = (string) [1, 1048561] (multiples of 16) "
54 * "height = (string) [1, 1048561] (multiples of 16) "
55 * "configuration = (string) ANY"
57 /* All optional parameters
59 * "configuration-uri ="
60 * "delivery-method = (string) { inline, in_band, out_band/<specific_name> } "
65 static GstStaticPadTemplate gst_rtp_theora_pay_sink_template =
66 GST_STATIC_PAD_TEMPLATE ("sink",
69 GST_STATIC_CAPS ("video/x-theora")
72 #define DEFAULT_CONFIG_INTERVAL 0
80 #define gst_rtp_theora_pay_parent_class parent_class
81 G_DEFINE_TYPE (GstRtpTheoraPay, gst_rtp_theora_pay, GST_TYPE_RTP_BASE_PAYLOAD);
83 static gboolean gst_rtp_theora_pay_setcaps (GstRTPBasePayload * basepayload,
85 static GstStateChangeReturn gst_rtp_theora_pay_change_state (GstElement *
86 element, GstStateChange transition);
87 static GstFlowReturn gst_rtp_theora_pay_handle_buffer (GstRTPBasePayload * pad,
89 static gboolean gst_rtp_theora_pay_sink_event (GstRTPBasePayload * payload,
92 static gboolean gst_rtp_theora_pay_parse_id (GstRTPBasePayload * basepayload,
93 guint8 * data, guint size);
94 static gboolean gst_rtp_theora_pay_finish_headers (GstRTPBasePayload *
97 static void gst_rtp_theora_pay_set_property (GObject * object, guint prop_id,
98 const GValue * value, GParamSpec * pspec);
99 static void gst_rtp_theora_pay_get_property (GObject * object, guint prop_id,
100 GValue * value, GParamSpec * pspec);
103 gst_rtp_theora_pay_class_init (GstRtpTheoraPayClass * klass)
105 GObjectClass *gobject_class;
106 GstElementClass *gstelement_class;
107 GstRTPBasePayloadClass *gstrtpbasepayload_class;
109 gobject_class = (GObjectClass *) klass;
110 gstelement_class = (GstElementClass *) klass;
111 gstrtpbasepayload_class = (GstRTPBasePayloadClass *) klass;
113 gstelement_class->change_state = gst_rtp_theora_pay_change_state;
115 gstrtpbasepayload_class->set_caps = gst_rtp_theora_pay_setcaps;
116 gstrtpbasepayload_class->handle_buffer = gst_rtp_theora_pay_handle_buffer;
117 gstrtpbasepayload_class->sink_event = gst_rtp_theora_pay_sink_event;
119 gobject_class->set_property = gst_rtp_theora_pay_set_property;
120 gobject_class->get_property = gst_rtp_theora_pay_get_property;
122 gst_element_class_add_static_pad_template (gstelement_class,
123 &gst_rtp_theora_pay_src_template);
124 gst_element_class_add_static_pad_template (gstelement_class,
125 &gst_rtp_theora_pay_sink_template);
127 gst_element_class_set_static_metadata (gstelement_class,
128 "RTP Theora payloader", "Codec/Payloader/Network/RTP",
129 "Payload-encode Theora video into RTP packets (draft-01 RFC XXXX)",
130 "Wim Taymans <wim.taymans@gmail.com>");
132 GST_DEBUG_CATEGORY_INIT (rtptheorapay_debug, "rtptheorapay", 0,
133 "Theora RTP Payloader");
135 g_object_class_install_property (G_OBJECT_CLASS (klass), PROP_CONFIG_INTERVAL,
136 g_param_spec_uint ("config-interval", "Config Send Interval",
137 "Send Config Insertion Interval in seconds (configuration headers "
138 "will be multiplexed in the data stream when detected.) (0 = disabled)",
139 0, 3600, DEFAULT_CONFIG_INTERVAL,
140 G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS)
145 gst_rtp_theora_pay_init (GstRtpTheoraPay * rtptheorapay)
147 rtptheorapay->last_config = GST_CLOCK_TIME_NONE;
151 gst_rtp_theora_pay_clear_packet (GstRtpTheoraPay * rtptheorapay)
153 if (rtptheorapay->packet)
154 gst_buffer_unref (rtptheorapay->packet);
155 rtptheorapay->packet = NULL;
156 g_list_free_full (rtptheorapay->packet_buffers,
157 (GDestroyNotify) gst_buffer_unref);
158 rtptheorapay->packet_buffers = NULL;
162 gst_rtp_theora_pay_cleanup (GstRtpTheoraPay * rtptheorapay)
164 gst_rtp_theora_pay_clear_packet (rtptheorapay);
165 g_list_free_full (rtptheorapay->headers, (GDestroyNotify) gst_buffer_unref);
166 rtptheorapay->headers = NULL;
167 g_free (rtptheorapay->config_data);
168 rtptheorapay->config_data = NULL;
169 rtptheorapay->last_config = GST_CLOCK_TIME_NONE;
173 gst_rtp_theora_pay_setcaps (GstRTPBasePayload * basepayload, GstCaps * caps)
175 GstRtpTheoraPay *rtptheorapay;
182 rtptheorapay = GST_RTP_THEORA_PAY (basepayload);
184 s = gst_caps_get_structure (caps, 0);
186 rtptheorapay->need_headers = TRUE;
188 if ((array = gst_structure_get_value (s, "streamheader")) == NULL)
191 if (G_VALUE_TYPE (array) != GST_TYPE_ARRAY)
194 if ((asize = gst_value_array_get_size (array)) < 3)
197 for (i = 0; i < asize; i++) {
200 value = gst_value_array_get_value (array, i);
201 if ((buf = gst_value_get_buffer (value)) == NULL)
204 gst_buffer_map (buf, &map, GST_MAP_READ);
205 /* no data packets allowed */
207 goto invalid_streamheader;
209 /* we need packets with id 0x80, 0x81, 0x82 */
210 if (map.data[0] != 0x80 + i)
211 goto invalid_streamheader;
214 /* identification, we need to parse this in order to get the clock rate. */
215 if (G_UNLIKELY (!gst_rtp_theora_pay_parse_id (basepayload, map.data,
217 goto parse_id_failed;
219 GST_DEBUG_OBJECT (rtptheorapay, "collecting header %d", i);
220 rtptheorapay->headers =
221 g_list_append (rtptheorapay->headers, gst_buffer_ref (buf));
222 gst_buffer_unmap (buf, &map);
224 if (!gst_rtp_theora_pay_finish_headers (basepayload))
233 GST_WARNING_OBJECT (rtptheorapay, "streamheader with null buffer received");
236 invalid_streamheader:
238 GST_WARNING_OBJECT (rtptheorapay, "unable to parse initial header");
239 gst_buffer_unmap (buf, &map);
244 GST_WARNING_OBJECT (rtptheorapay, "unable to parse initial header");
245 gst_buffer_unmap (buf, &map);
250 GST_WARNING_OBJECT (rtptheorapay, "unable to finish headers");
256 gst_rtp_theora_pay_reset_packet (GstRtpTheoraPay * rtptheorapay, guint8 TDT)
259 GstRTPBuffer rtp = { NULL };
261 GST_DEBUG_OBJECT (rtptheorapay, "reset packet");
263 rtptheorapay->payload_pos = 4;
264 gst_rtp_buffer_map (rtptheorapay->packet, GST_MAP_READ, &rtp);
265 payload_len = gst_rtp_buffer_get_payload_len (&rtp);
266 gst_rtp_buffer_unmap (&rtp);
267 rtptheorapay->payload_left = payload_len - 4;
268 rtptheorapay->payload_duration = 0;
269 rtptheorapay->payload_F = 0;
270 rtptheorapay->payload_TDT = TDT;
271 rtptheorapay->payload_pkts = 0;
275 gst_rtp_theora_pay_init_packet (GstRtpTheoraPay * rtptheorapay, guint8 TDT,
276 GstClockTime timestamp)
278 GST_DEBUG_OBJECT (rtptheorapay, "starting new packet, TDT: %d", TDT);
280 gst_rtp_theora_pay_clear_packet (rtptheorapay);
282 /* new packet allocate max packet size */
283 rtptheorapay->packet =
284 gst_rtp_buffer_new_allocate_len (GST_RTP_BASE_PAYLOAD_MTU
285 (rtptheorapay), 0, 0);
286 gst_rtp_theora_pay_reset_packet (rtptheorapay, TDT);
288 GST_BUFFER_PTS (rtptheorapay->packet) = timestamp;
292 gst_rtp_theora_pay_flush_packet (GstRtpTheoraPay * rtptheorapay)
297 GstRTPBuffer rtp = { NULL };
300 /* check for empty packet */
301 if (!rtptheorapay->packet || rtptheorapay->payload_pos <= 4)
304 GST_DEBUG_OBJECT (rtptheorapay, "flushing packet");
306 gst_rtp_buffer_map (rtptheorapay->packet, GST_MAP_WRITE, &rtp);
309 payload = gst_rtp_buffer_get_payload (&rtp);
312 * 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
313 * +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
314 * | Ident | F |TDT|# pkts.|
315 * +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
317 * F: Fragment type (0=none, 1=start, 2=cont, 3=end)
318 * TDT: Theora data type (0=theora, 1=config, 2=comment, 3=reserved)
319 * pkts: number of packets.
321 payload[0] = (rtptheorapay->payload_ident >> 16) & 0xff;
322 payload[1] = (rtptheorapay->payload_ident >> 8) & 0xff;
323 payload[2] = (rtptheorapay->payload_ident) & 0xff;
324 payload[3] = (rtptheorapay->payload_F & 0x3) << 6 |
325 (rtptheorapay->payload_TDT & 0x3) << 4 |
326 (rtptheorapay->payload_pkts & 0xf);
328 gst_rtp_buffer_unmap (&rtp);
330 /* shrink the buffer size to the last written byte */
331 hlen = gst_rtp_buffer_calc_header_len (0);
332 gst_buffer_resize (rtptheorapay->packet, 0, hlen + rtptheorapay->payload_pos);
334 GST_BUFFER_DURATION (rtptheorapay->packet) = rtptheorapay->payload_duration;
336 for (l = g_list_last (rtptheorapay->packet_buffers); l; l = l->prev) {
337 GstBuffer *buf = GST_BUFFER_CAST (l->data);
338 gst_rtp_copy_meta (GST_ELEMENT_CAST (rtptheorapay), rtptheorapay->packet,
339 buf, g_quark_from_static_string (GST_META_TAG_VIDEO_STR));
340 gst_buffer_unref (buf);
342 g_list_free (rtptheorapay->packet_buffers);
343 rtptheorapay->packet_buffers = NULL;
345 /* push, this gives away our ref to the packet, so clear it. */
347 gst_rtp_base_payload_push (GST_RTP_BASE_PAYLOAD (rtptheorapay),
348 rtptheorapay->packet);
349 rtptheorapay->packet = NULL;
355 gst_rtp_theora_pay_finish_headers (GstRTPBasePayload * basepayload)
357 GstRtpTheoraPay *rtptheorapay = GST_RTP_THEORA_PAY (basepayload);
359 guint length, size, n_headers, configlen, extralen;
360 gchar *wstr, *hstr, *configuration;
361 guint8 *data, *config;
364 const gchar *sampling = NULL;
366 GST_DEBUG_OBJECT (rtptheorapay, "finish headers");
368 if (!rtptheorapay->headers) {
369 GST_DEBUG_OBJECT (rtptheorapay, "We need 2 headers but have none");
373 /* +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
374 * | Number of packed headers |
375 * +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
376 * +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
378 * +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
379 * +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
381 * +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
382 * +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
384 * +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
386 * We only construct a config containing 1 packed header like this:
389 * 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
390 * +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
391 * | Ident | length ..
392 * +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
393 * .. | n. of headers | length1 | length2 ..
394 * +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
395 * .. | Identification Header ..
396 * +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
397 * .................................................................
398 * +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
399 * .. | Comment Header ..
400 * +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
401 * .................................................................
402 * +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
403 * .. Comment Header |
404 * +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
406 * +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
407 * .................................................................
408 * +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
410 * +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
413 /* we need 4 bytes for the number of headers (which is always 1), 3 bytes for
414 * the ident, 2 bytes for length, 1 byte for n. of headers. */
415 size = 4 + 3 + 2 + 1;
417 /* count the size of the headers first and update the hash */
420 ident = fnv1_hash_32_new ();
422 for (walk = rtptheorapay->headers; walk; walk = g_list_next (walk)) {
423 GstBuffer *buf = GST_BUFFER_CAST (walk->data);
427 bsize = gst_buffer_get_size (buf);
431 /* count number of bytes needed for length fields, we don't need this for
432 * the last header. */
433 if (g_list_next (walk)) {
441 gst_buffer_map (buf, &map, GST_MAP_READ);
442 ident = fnv1_hash_32_update (ident, map.data, map.size);
443 gst_buffer_unmap (buf, &map);
446 /* packet length is header size + packet length */
447 configlen = size + length;
448 config = data = g_malloc (configlen);
450 /* number of packed headers, we only pack 1 header */
456 ident = fnv1_hash_32_to_24 (ident);
457 rtptheorapay->payload_ident = ident;
458 GST_DEBUG_OBJECT (rtptheorapay, "ident 0x%08x", ident);
460 /* take lower 3 bytes */
461 data[4] = (ident >> 16) & 0xff;
462 data[5] = (ident >> 8) & 0xff;
463 data[6] = ident & 0xff;
465 /* store length of all theora headers */
466 data[7] = ((length) >> 8) & 0xff;
467 data[8] = (length) & 0xff;
469 /* store number of headers minus one. */
470 data[9] = n_headers - 1;
473 /* store length for each header */
474 for (walk = rtptheorapay->headers; walk; walk = g_list_next (walk)) {
475 GstBuffer *buf = GST_BUFFER_CAST (walk->data);
476 guint bsize, size, temp;
479 /* only need to store the length when it's not the last header */
480 if (!g_list_next (walk))
483 bsize = gst_buffer_get_size (buf);
493 bsize = gst_buffer_get_size (buf);
494 /* write the size backwards */
498 data[size] = (bsize & 0x7f) | flag;
500 flag = 0x80; /* Flag bit on all bytes of the length except the last */
505 /* copy header data */
506 for (walk = rtptheorapay->headers; walk; walk = g_list_next (walk)) {
507 GstBuffer *buf = GST_BUFFER_CAST (walk->data);
509 gst_buffer_extract (buf, 0, data, gst_buffer_get_size (buf));
510 data += gst_buffer_get_size (buf);
512 rtptheorapay->need_headers = FALSE;
514 /* serialize to base64 */
515 configuration = g_base64_encode (config, configlen);
517 /* store for later re-sending */
518 g_free (rtptheorapay->config_data);
519 rtptheorapay->config_size = configlen - 4 - 3 - 2;
520 rtptheorapay->config_data = g_malloc (rtptheorapay->config_size);
521 rtptheorapay->config_extra_len = extralen;
522 memcpy (rtptheorapay->config_data, config + 4 + 3 + 2,
523 rtptheorapay->config_size);
527 /* configure payloader settings */
528 switch (rtptheorapay->pixel_format) {
530 sampling = "YCbCr-4:2:2";
533 sampling = "YCbCr-4:4:4";
537 sampling = "YCbCr-4:2:0";
542 wstr = g_strdup_printf ("%d", rtptheorapay->width);
543 hstr = g_strdup_printf ("%d", rtptheorapay->height);
544 gst_rtp_base_payload_set_options (basepayload, "video", TRUE, "THEORA",
547 gst_rtp_base_payload_set_outcaps (basepayload, "sampling", G_TYPE_STRING,
548 sampling, "width", G_TYPE_STRING, wstr, "height", G_TYPE_STRING,
549 hstr, "configuration", G_TYPE_STRING, configuration, "delivery-method",
550 G_TYPE_STRING, "inline",
551 /* don't set the other defaults
556 g_free (configuration);
563 GST_DEBUG_OBJECT (rtptheorapay, "finish headers");
569 gst_rtp_theora_pay_parse_id (GstRTPBasePayload * basepayload, guint8 * data,
572 GstRtpTheoraPay *rtptheorapay;
573 gint width, height, pixel_format;
575 rtptheorapay = GST_RTP_THEORA_PAY (basepayload);
577 if (G_UNLIKELY (size < 42))
580 if (G_UNLIKELY (memcmp (data, "\200theora", 7)))
584 if (G_UNLIKELY (data[0] != 3))
585 goto invalid_version;
586 if (G_UNLIKELY (data[1] != 2))
587 goto invalid_version;
590 width = GST_READ_UINT16_BE (data) << 4;
592 height = GST_READ_UINT16_BE (data) << 4;
595 pixel_format = (GST_READ_UINT8 (data) >> 3) & 0x03;
598 rtptheorapay->pixel_format = pixel_format;
599 rtptheorapay->width = width;
600 rtptheorapay->height = height;
607 GST_ELEMENT_ERROR (basepayload, STREAM, DECODE,
609 ("Identification packet is too short, need at least 42, got %d", size));
614 GST_ELEMENT_ERROR (basepayload, STREAM, DECODE,
615 (NULL), ("Invalid header start in identification packet"));
620 GST_ELEMENT_ERROR (basepayload, STREAM, DECODE,
621 (NULL), ("Invalid version"));
627 gst_rtp_theora_pay_payload_buffer (GstRtpTheoraPay * rtptheorapay, guint8 TDT,
628 GstBuffer * buffer, guint8 * data, guint size, GstClockTime timestamp,
629 GstClockTime duration, guint not_in_length)
631 GstFlowReturn ret = GST_FLOW_OK;
634 GstClockTime newduration;
637 guint8 *ppos, *payload;
639 GstRTPBuffer rtp = { NULL };
641 /* size increases with packet length and 2 bytes size eader. */
642 newduration = rtptheorapay->payload_duration;
643 if (duration != GST_CLOCK_TIME_NONE)
644 newduration += duration;
646 newsize = rtptheorapay->payload_pos + 2 + size;
647 packet_len = gst_rtp_buffer_calc_packet_len (newsize, 0, 0);
649 /* check buffer filled against length and max latency */
650 flush = gst_rtp_base_payload_is_filled (GST_RTP_BASE_PAYLOAD (rtptheorapay),
651 packet_len, newduration);
652 /* we can store up to 15 theora packets in one RTP packet. */
653 flush |= (rtptheorapay->payload_pkts == 15);
654 /* flush if we have a new TDT */
655 if (rtptheorapay->packet)
656 flush |= (rtptheorapay->payload_TDT != TDT);
658 ret = gst_rtp_theora_pay_flush_packet (rtptheorapay);
660 if (ret != GST_FLOW_OK)
663 /* create new packet if we must */
664 if (!rtptheorapay->packet) {
665 gst_rtp_theora_pay_init_packet (rtptheorapay, TDT, timestamp);
668 gst_rtp_buffer_map (rtptheorapay->packet, GST_MAP_WRITE, &rtp);
669 payload = gst_rtp_buffer_get_payload (&rtp);
670 ppos = payload + rtptheorapay->payload_pos;
673 /* put buffer in packet, it either fits completely or needs to be fragmented
674 * over multiple RTP packets. */
676 plen = MIN (rtptheorapay->payload_left - 2, size);
678 GST_DEBUG_OBJECT (rtptheorapay, "append %u bytes", plen);
680 /* data is copied in the payload with a 2 byte length header */
681 ppos[0] = ((plen - not_in_length) >> 8) & 0xff;
682 ppos[1] = ((plen - not_in_length) & 0xff);
684 memcpy (&ppos[2], data, plen);
687 if (!rtptheorapay->packet_buffers
688 || rtptheorapay->packet_buffers->data != (gpointer) buffer)
689 rtptheorapay->packet_buffers =
690 g_list_prepend (rtptheorapay->packet_buffers,
691 gst_buffer_ref (buffer));
695 for (l = rtptheorapay->headers; l; l = l->next)
696 rtptheorapay->packet_buffers =
697 g_list_prepend (rtptheorapay->packet_buffers,
698 gst_buffer_ref (l->data));
701 /* only first (only) configuration cuts length field */
702 /* NOTE: spec (if any) is not clear on this ... */
708 rtptheorapay->payload_pos += plen + 2;
709 rtptheorapay->payload_left -= plen + 2;
713 /* last fragment, set F to 0x3. */
714 rtptheorapay->payload_F = 0x3;
716 /* fragment continues, set F to 0x2. */
717 rtptheorapay->payload_F = 0x2;
720 /* fragmented packet starts, set F to 0x1, mark ourselves as
722 rtptheorapay->payload_F = 0x1;
727 gst_rtp_buffer_unmap (&rtp);
728 /* fragmented packets are always flushed and have ptks of 0 */
729 rtptheorapay->payload_pkts = 0;
730 ret = gst_rtp_theora_pay_flush_packet (rtptheorapay);
733 /* start new packet and get pointers. TDT stays the same. */
734 gst_rtp_theora_pay_init_packet (rtptheorapay,
735 rtptheorapay->payload_TDT, timestamp);
736 gst_rtp_buffer_map (rtptheorapay->packet, GST_MAP_WRITE, &rtp);
737 payload = gst_rtp_buffer_get_payload (&rtp);
738 ppos = payload + rtptheorapay->payload_pos;
741 /* unfragmented packet, update stats for next packet, size == 0 and we
742 * exit the while loop */
743 rtptheorapay->payload_pkts++;
744 if (duration != GST_CLOCK_TIME_NONE)
745 rtptheorapay->payload_duration += duration;
747 } while (size && ret == GST_FLOW_OK);
750 gst_rtp_buffer_unmap (&rtp);
757 gst_rtp_theora_pay_handle_buffer (GstRTPBasePayload * basepayload,
760 GstRtpTheoraPay *rtptheorapay;
765 GstClockTime duration, timestamp;
767 gboolean keyframe = FALSE;
769 rtptheorapay = GST_RTP_THEORA_PAY (basepayload);
771 gst_buffer_map (buffer, &map, GST_MAP_READ);
774 duration = GST_BUFFER_DURATION (buffer);
775 timestamp = GST_BUFFER_PTS (buffer);
777 GST_DEBUG_OBJECT (rtptheorapay, "size %" G_GSIZE_FORMAT
778 ", duration %" GST_TIME_FORMAT, size, GST_TIME_ARGS (duration));
780 /* find packet type */
784 } else if (data[0] & 0x80) {
786 if (data[0] == 0x80) {
787 /* identification, we need to parse this in order to get the clock rate.
789 if (G_UNLIKELY (!gst_rtp_theora_pay_parse_id (basepayload, data, size)))
790 goto parse_id_failed;
792 } else if (data[0] == 0x81) {
795 } else if (data[0] == 0x82) {
803 keyframe = ((data[0] & 0x40) == 0);
806 /* we need to collect the headers and construct a config string from them */
808 GST_DEBUG_OBJECT (rtptheorapay, "collecting header, buffer %p", buffer);
809 /* append header to the list of headers */
810 gst_buffer_unmap (buffer, &map);
811 rtptheorapay->headers = g_list_append (rtptheorapay->headers, buffer);
814 } else if (rtptheorapay->headers && rtptheorapay->need_headers) {
815 if (!gst_rtp_theora_pay_finish_headers (basepayload))
819 /* there is a config request, see if we need to insert it */
820 if (keyframe && (rtptheorapay->config_interval > 0) &&
821 rtptheorapay->config_data) {
822 gboolean send_config = FALSE;
824 if (rtptheorapay->last_config != -1) {
827 GST_LOG_OBJECT (rtptheorapay,
828 "now %" GST_TIME_FORMAT ", last VOP-I %" GST_TIME_FORMAT,
829 GST_TIME_ARGS (timestamp), GST_TIME_ARGS (rtptheorapay->last_config));
831 /* calculate diff between last config in milliseconds */
832 if (timestamp > rtptheorapay->last_config) {
833 diff = timestamp - rtptheorapay->last_config;
838 GST_DEBUG_OBJECT (rtptheorapay,
839 "interval since last config %" GST_TIME_FORMAT, GST_TIME_ARGS (diff));
841 /* bigger than interval, queue config */
842 /* FIXME should convert timestamps to running time */
843 if (GST_TIME_AS_SECONDS (diff) >= rtptheorapay->config_interval) {
844 GST_DEBUG_OBJECT (rtptheorapay, "time to send config");
848 /* no known previous config time, send now */
849 GST_DEBUG_OBJECT (rtptheorapay, "no previous config time, send now");
854 /* we need to send config now first */
855 /* different TDT type forces flush */
856 gst_rtp_theora_pay_payload_buffer (rtptheorapay, 1,
857 NULL, rtptheorapay->config_data, rtptheorapay->config_size,
858 timestamp, GST_CLOCK_TIME_NONE, rtptheorapay->config_extra_len);
860 if (timestamp != -1) {
861 rtptheorapay->last_config = timestamp;
867 gst_rtp_theora_pay_payload_buffer (rtptheorapay, TDT, buffer, data, size,
868 timestamp, duration, 0);
870 gst_buffer_unmap (buffer, &map);
871 gst_buffer_unref (buffer);
879 gst_buffer_unmap (buffer, &map);
880 gst_buffer_unref (buffer);
881 return GST_FLOW_ERROR;
885 GST_ELEMENT_WARNING (rtptheorapay, STREAM, DECODE,
886 (NULL), ("Ignoring unknown header received"));
887 gst_buffer_unmap (buffer, &map);
888 gst_buffer_unref (buffer);
893 GST_ELEMENT_WARNING (rtptheorapay, STREAM, DECODE,
894 (NULL), ("Error initializing header config"));
895 gst_buffer_unmap (buffer, &map);
896 gst_buffer_unref (buffer);
902 gst_rtp_theora_pay_sink_event (GstRTPBasePayload * payload, GstEvent * event)
904 GstRtpTheoraPay *rtptheorapay = GST_RTP_THEORA_PAY (payload);
906 switch (GST_EVENT_TYPE (event)) {
907 case GST_EVENT_FLUSH_STOP:
908 gst_rtp_theora_pay_clear_packet (rtptheorapay);
913 /* false to let parent handle event as well */
914 return GST_RTP_BASE_PAYLOAD_CLASS (parent_class)->sink_event (payload, event);
917 static GstStateChangeReturn
918 gst_rtp_theora_pay_change_state (GstElement * element,
919 GstStateChange transition)
921 GstRtpTheoraPay *rtptheorapay;
923 GstStateChangeReturn ret;
925 rtptheorapay = GST_RTP_THEORA_PAY (element);
927 switch (transition) {
932 ret = GST_ELEMENT_CLASS (parent_class)->change_state (element, transition);
934 switch (transition) {
935 case GST_STATE_CHANGE_PAUSED_TO_READY:
936 gst_rtp_theora_pay_cleanup (rtptheorapay);
945 gst_rtp_theora_pay_set_property (GObject * object, guint prop_id,
946 const GValue * value, GParamSpec * pspec)
948 GstRtpTheoraPay *rtptheorapay;
950 rtptheorapay = GST_RTP_THEORA_PAY (object);
953 case PROP_CONFIG_INTERVAL:
954 rtptheorapay->config_interval = g_value_get_uint (value);
962 gst_rtp_theora_pay_get_property (GObject * object, guint prop_id,
963 GValue * value, GParamSpec * pspec)
965 GstRtpTheoraPay *rtptheorapay;
967 rtptheorapay = GST_RTP_THEORA_PAY (object);
970 case PROP_CONFIG_INTERVAL:
971 g_value_set_uint (value, rtptheorapay->config_interval);
979 gst_rtp_theora_pay_plugin_init (GstPlugin * plugin)
981 return gst_element_register (plugin, "rtptheorapay",
982 GST_RANK_SECONDARY, GST_TYPE_RTP_THEORA_PAY);