2 * Copyright (C) <2005> 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>
28 #include "gstrtpmp4vpay.h"
30 GST_DEBUG_CATEGORY_STATIC (rtpmp4vpay_debug);
31 #define GST_CAT_DEFAULT (rtpmp4vpay_debug)
33 static GstStaticPadTemplate gst_rtp_mp4v_pay_sink_template =
34 GST_STATIC_PAD_TEMPLATE ("sink",
37 GST_STATIC_CAPS ("video/mpeg,"
38 "mpegversion=(int) 4," "systemstream=(boolean)false;" "video/x-xvid")
41 static GstStaticPadTemplate gst_rtp_mp4v_pay_src_template =
42 GST_STATIC_PAD_TEMPLATE ("src",
45 GST_STATIC_CAPS ("application/x-rtp, "
46 "media = (string) \"video\", "
47 "payload = (int) " GST_RTP_PAYLOAD_DYNAMIC_STRING ", "
48 "clock-rate = (int) [1, MAX ], " "encoding-name = (string) \"MP4V-ES\""
51 "profile-level-id = (string) [1,MAX]"
52 "config = (string) [1,MAX]"
57 #define DEFAULT_SEND_CONFIG FALSE
58 #define DEFAULT_BUFFER_LIST FALSE
59 #define DEFAULT_CONFIG_INTERVAL 0
70 static void gst_rtp_mp4v_pay_finalize (GObject * object);
72 static void gst_rtp_mp4v_pay_set_property (GObject * object, guint prop_id,
73 const GValue * value, GParamSpec * pspec);
74 static void gst_rtp_mp4v_pay_get_property (GObject * object, guint prop_id,
75 GValue * value, GParamSpec * pspec);
77 static gboolean gst_rtp_mp4v_pay_setcaps (GstRTPBasePayload * payload,
79 static GstFlowReturn gst_rtp_mp4v_pay_handle_buffer (GstRTPBasePayload *
80 payload, GstBuffer * buffer);
81 static gboolean gst_rtp_mp4v_pay_sink_event (GstRTPBasePayload * pay,
84 #define gst_rtp_mp4v_pay_parent_class parent_class
85 G_DEFINE_TYPE (GstRtpMP4VPay, gst_rtp_mp4v_pay, GST_TYPE_RTP_BASE_PAYLOAD)
87 static void gst_rtp_mp4v_pay_class_init (GstRtpMP4VPayClass * klass)
89 GObjectClass *gobject_class;
90 GstElementClass *gstelement_class;
91 GstRTPBasePayloadClass *gstrtpbasepayload_class;
93 gobject_class = (GObjectClass *) klass;
94 gstelement_class = (GstElementClass *) klass;
95 gstrtpbasepayload_class = (GstRTPBasePayloadClass *) klass;
97 gobject_class->set_property = gst_rtp_mp4v_pay_set_property;
98 gobject_class->get_property = gst_rtp_mp4v_pay_get_property;
100 gst_element_class_add_pad_template (gstelement_class,
101 gst_static_pad_template_get (&gst_rtp_mp4v_pay_src_template));
102 gst_element_class_add_pad_template (gstelement_class,
103 gst_static_pad_template_get (&gst_rtp_mp4v_pay_sink_template));
105 gst_element_class_set_details_simple (gstelement_class,
106 "RTP MPEG4 Video payloader", "Codec/Payloader/Network/RTP",
107 "Payload MPEG-4 video as RTP packets (RFC 3016)",
108 "Wim Taymans <wim.taymans@gmail.com>");
110 g_object_class_install_property (G_OBJECT_CLASS (klass), ARG_SEND_CONFIG,
111 g_param_spec_boolean ("send-config", "Send Config",
112 "Send the config parameters in RTP packets as well(deprecated "
113 "see config-interval)",
114 DEFAULT_SEND_CONFIG, G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS));
116 g_object_class_install_property (G_OBJECT_CLASS (klass), ARG_BUFFER_LIST,
117 g_param_spec_boolean ("buffer-list", "Buffer Array",
119 DEFAULT_BUFFER_LIST, G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS));
121 g_object_class_install_property (G_OBJECT_CLASS (klass), ARG_CONFIG_INTERVAL,
122 g_param_spec_uint ("config-interval", "Config Send Interval",
123 "Send Config Insertion Interval in seconds (configuration headers "
124 "will be multiplexed in the data stream when detected.) (0 = disabled)",
125 0, 3600, DEFAULT_CONFIG_INTERVAL,
126 G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS)
129 gobject_class->finalize = gst_rtp_mp4v_pay_finalize;
131 gstrtpbasepayload_class->set_caps = gst_rtp_mp4v_pay_setcaps;
132 gstrtpbasepayload_class->handle_buffer = gst_rtp_mp4v_pay_handle_buffer;
133 gstrtpbasepayload_class->sink_event = gst_rtp_mp4v_pay_sink_event;
135 GST_DEBUG_CATEGORY_INIT (rtpmp4vpay_debug, "rtpmp4vpay", 0,
136 "MP4 video RTP Payloader");
140 gst_rtp_mp4v_pay_init (GstRtpMP4VPay * rtpmp4vpay)
142 rtpmp4vpay->adapter = gst_adapter_new ();
143 rtpmp4vpay->rate = 90000;
144 rtpmp4vpay->profile = 1;
145 rtpmp4vpay->buffer_list = DEFAULT_BUFFER_LIST;
146 rtpmp4vpay->send_config = DEFAULT_SEND_CONFIG;
147 rtpmp4vpay->need_config = TRUE;
148 rtpmp4vpay->config_interval = DEFAULT_CONFIG_INTERVAL;
149 rtpmp4vpay->last_config = -1;
151 rtpmp4vpay->config = NULL;
155 gst_rtp_mp4v_pay_finalize (GObject * object)
157 GstRtpMP4VPay *rtpmp4vpay;
159 rtpmp4vpay = GST_RTP_MP4V_PAY (object);
161 if (rtpmp4vpay->config) {
162 gst_buffer_unref (rtpmp4vpay->config);
163 rtpmp4vpay->config = NULL;
165 g_object_unref (rtpmp4vpay->adapter);
166 rtpmp4vpay->adapter = NULL;
168 G_OBJECT_CLASS (parent_class)->finalize (object);
172 gst_rtp_mp4v_pay_new_caps (GstRtpMP4VPay * rtpmp4vpay)
174 gchar *profile, *config;
178 profile = g_strdup_printf ("%d", rtpmp4vpay->profile);
179 g_value_init (&v, GST_TYPE_BUFFER);
180 gst_value_set_buffer (&v, rtpmp4vpay->config);
181 config = gst_value_serialize (&v);
183 res = gst_rtp_base_payload_set_outcaps (GST_RTP_BASE_PAYLOAD (rtpmp4vpay),
184 "profile-level-id", G_TYPE_STRING, profile,
185 "config", G_TYPE_STRING, config, NULL);
196 gst_rtp_mp4v_pay_setcaps (GstRTPBasePayload * payload, GstCaps * caps)
198 GstRtpMP4VPay *rtpmp4vpay;
199 GstStructure *structure;
200 const GValue *codec_data;
203 rtpmp4vpay = GST_RTP_MP4V_PAY (payload);
205 gst_rtp_base_payload_set_options (payload, "video", TRUE, "MP4V-ES",
210 structure = gst_caps_get_structure (caps, 0);
211 codec_data = gst_structure_get_value (structure, "codec_data");
213 GST_LOG_OBJECT (rtpmp4vpay, "got codec_data");
214 if (G_VALUE_TYPE (codec_data) == GST_TYPE_BUFFER) {
217 buffer = gst_value_get_buffer (codec_data);
219 if (gst_buffer_get_size (buffer) < 5)
222 gst_buffer_extract (buffer, 4, &rtpmp4vpay->profile, 1);
223 GST_LOG_OBJECT (rtpmp4vpay, "configuring codec_data, profile %d",
224 rtpmp4vpay->profile);
226 if (rtpmp4vpay->config)
227 gst_buffer_unref (rtpmp4vpay->config);
228 rtpmp4vpay->config = gst_buffer_copy (buffer);
229 res = gst_rtp_mp4v_pay_new_caps (rtpmp4vpay);
238 gst_rtp_mp4v_pay_empty (GstRtpMP4VPay * rtpmp4vpay)
240 gst_adapter_clear (rtpmp4vpay->adapter);
244 gst_rtp_mp4v_pay_flush (GstRtpMP4VPay * rtpmp4vpay)
248 GstBuffer *outbuf_data = NULL;
250 GstBufferList *list = NULL;
252 /* the data available in the adapter is either smaller
253 * than the MTU or bigger. In the case it is smaller, the complete
254 * adapter contents can be put in one packet. In the case the
255 * adapter has more than one MTU, we need to split the MP4V data
256 * over multiple packets. */
257 avail = gst_adapter_available (rtpmp4vpay->adapter);
259 if (rtpmp4vpay->config == NULL && rtpmp4vpay->need_config) {
260 /* when we don't have a config yet, flush things out */
261 gst_adapter_flush (rtpmp4vpay->adapter, avail);
270 if (rtpmp4vpay->buffer_list) {
271 /* Use buffer lists. Each frame will be put into a list
272 * of buffers and the whole list will be pushed downstream
274 list = gst_buffer_list_new ();
281 GstRTPBuffer rtp = { NULL };
283 /* this will be the total lenght of the packet */
284 packet_len = gst_rtp_buffer_calc_packet_len (avail, 0, 0);
286 /* fill one MTU or all available bytes */
287 towrite = MIN (packet_len, GST_RTP_BASE_PAYLOAD_MTU (rtpmp4vpay));
289 /* this is the payload length */
290 payload_len = gst_rtp_buffer_calc_payload_len (towrite, 0, 0);
292 /* create buffer without payload. The payload will be put
293 * in next buffer instead. Both buffers will be merged */
294 outbuf = gst_rtp_buffer_new_allocate (0, 0, 0);
296 /* Take buffer with the payload from the adapter */
297 outbuf_data = gst_adapter_take_buffer (rtpmp4vpay->adapter, payload_len);
299 avail -= payload_len;
301 gst_rtp_buffer_map (outbuf, GST_MAP_WRITE, &rtp);
302 gst_rtp_buffer_set_marker (&rtp, avail == 0);
303 gst_rtp_buffer_unmap (&rtp);
305 outbuf = gst_buffer_join (outbuf, outbuf_data);
307 GST_BUFFER_TIMESTAMP (outbuf) = rtpmp4vpay->first_timestamp;
309 if (rtpmp4vpay->buffer_list) {
311 gst_buffer_list_insert (list, -1, outbuf);
314 gst_rtp_base_payload_push (GST_RTP_BASE_PAYLOAD (rtpmp4vpay), outbuf);
318 if (rtpmp4vpay->buffer_list) {
319 /* push the whole buffer list at once */
321 gst_rtp_base_payload_push_list (GST_RTP_BASE_PAYLOAD (rtpmp4vpay),
328 #define VOS_STARTCODE 0x000001B0
329 #define VOS_ENDCODE 0x000001B1
330 #define USER_DATA_STARTCODE 0x000001B2
331 #define GOP_STARTCODE 0x000001B3
332 #define VISUAL_OBJECT_STARTCODE 0x000001B5
333 #define VOP_STARTCODE 0x000001B6
336 gst_rtp_mp4v_pay_depay_data (GstRtpMP4VPay * enc, guint8 * data, guint size,
337 gint * strip, gboolean * vopi)
348 code = GST_READ_UINT32_BE (data);
349 GST_DEBUG_OBJECT (enc, "start code 0x%08x", code);
357 gboolean newprofile = FALSE;
360 if (code == VOS_STARTCODE) {
361 /* profile_and_level_indication */
364 GST_DEBUG_OBJECT (enc, "VOS profile 0x%08x", profile);
366 if (profile != enc->profile) {
368 enc->profile = profile;
372 /* up to the next GOP_STARTCODE or VOP_STARTCODE is
373 * the config information */
375 for (i = 5; i < size - 4; i++) {
376 code = (code << 8) | data[i];
377 if (code == GOP_STARTCODE || code == VOP_STARTCODE)
381 /* see if config changed */
384 if (gst_buffer_get_size (enc->config) == i) {
385 equal = gst_buffer_memcmp (enc->config, 0, data, i) == 0;
388 /* if config string changed or new profile, make new caps */
389 if (!equal || newprofile) {
391 gst_buffer_unref (enc->config);
392 enc->config = gst_buffer_new_and_alloc (i);
394 gst_buffer_fill (enc->config, 0, data, i);
396 gst_rtp_mp4v_pay_new_caps (enc);
399 /* we need to flush out the current packet. */
404 GST_DEBUG_OBJECT (enc, "VOP");
405 /* VOP startcode, we don't have to flush the packet */
407 /* vop-coding-type == I-frame */
408 if (size > 4 && (data[4] >> 6 == 0)) {
409 GST_DEBUG_OBJECT (enc, "VOP-I");
414 GST_DEBUG_OBJECT (enc, "GOP");
419 enc->need_config = FALSE;
423 if (code >= 0x20 && code <= 0x2f) {
424 GST_DEBUG_OBJECT (enc, "short header");
427 GST_DEBUG_OBJECT (enc, "other startcode");
428 /* all other startcodes need a flush */
436 /* we expect buffers starting on startcodes.
439 gst_rtp_mp4v_pay_handle_buffer (GstRTPBasePayload * basepayload,
442 GstRtpMP4VPay *rtpmp4vpay;
450 GstClockTime timestamp, duration;
452 gboolean send_config;
457 rtpmp4vpay = GST_RTP_MP4V_PAY (basepayload);
459 gst_buffer_map (buffer, &map, GST_MAP_READ);
461 timestamp = GST_BUFFER_TIMESTAMP (buffer);
462 duration = GST_BUFFER_DURATION (buffer);
463 avail = gst_adapter_available (rtpmp4vpay->adapter);
468 /* empty buffer, take timestamp */
470 rtpmp4vpay->first_timestamp = timestamp;
471 rtpmp4vpay->duration = 0;
474 /* depay incomming data and see if we need to start a new RTP
477 gst_rtp_mp4v_pay_depay_data (rtpmp4vpay, map.data, size, &strip, &vopi);
478 gst_buffer_unmap (buffer, &map);
481 /* strip off config if requested */
482 if (!(rtpmp4vpay->config_interval > 0)) {
485 GST_LOG_OBJECT (rtpmp4vpay, "stripping config at %d, size %d", strip,
486 (gint) size - strip);
488 /* strip off header */
489 subbuf = gst_buffer_copy_region (buffer, GST_BUFFER_COPY_MEMORY, strip,
491 GST_BUFFER_TIMESTAMP (subbuf) = timestamp;
492 gst_buffer_unref (buffer);
495 size = gst_buffer_get_size (buffer);
497 GST_LOG_OBJECT (rtpmp4vpay, "found config in stream");
498 rtpmp4vpay->last_config = timestamp;
502 /* there is a config request, see if we need to insert it */
503 if (vopi && (rtpmp4vpay->config_interval > 0) && rtpmp4vpay->config) {
504 if (rtpmp4vpay->last_config != -1) {
507 GST_LOG_OBJECT (rtpmp4vpay,
508 "now %" GST_TIME_FORMAT ", last VOP-I %" GST_TIME_FORMAT,
509 GST_TIME_ARGS (timestamp), GST_TIME_ARGS (rtpmp4vpay->last_config));
511 /* calculate diff between last config in milliseconds */
512 if (timestamp > rtpmp4vpay->last_config) {
513 diff = timestamp - rtpmp4vpay->last_config;
518 GST_DEBUG_OBJECT (rtpmp4vpay,
519 "interval since last config %" GST_TIME_FORMAT, GST_TIME_ARGS (diff));
521 /* bigger than interval, queue config */
522 /* FIXME should convert timestamps to running time */
523 if (GST_TIME_AS_SECONDS (diff) >= rtpmp4vpay->config_interval) {
524 GST_DEBUG_OBJECT (rtpmp4vpay, "time to send config");
528 /* no known previous config time, send now */
529 GST_DEBUG_OBJECT (rtpmp4vpay, "no previous config time, send now");
534 /* we need to send config now first */
537 GST_LOG_OBJECT (rtpmp4vpay, "inserting config in stream");
540 superbuf = gst_buffer_merge (rtpmp4vpay->config, buffer);
542 GST_BUFFER_TIMESTAMP (superbuf) = timestamp;
543 gst_buffer_unref (buffer);
546 size = gst_buffer_get_size (buffer);
548 if (timestamp != -1) {
549 rtpmp4vpay->last_config = timestamp;
554 /* if we need to flush, do so now */
556 ret = gst_rtp_mp4v_pay_flush (rtpmp4vpay);
557 rtpmp4vpay->first_timestamp = timestamp;
558 rtpmp4vpay->duration = 0;
562 /* get packet length of data and see if we exceeded MTU. */
563 packet_len = gst_rtp_buffer_calc_packet_len (avail + size, 0, 0);
565 if (gst_rtp_base_payload_is_filled (basepayload,
566 packet_len, rtpmp4vpay->duration + duration)) {
567 ret = gst_rtp_mp4v_pay_flush (rtpmp4vpay);
568 rtpmp4vpay->first_timestamp = timestamp;
569 rtpmp4vpay->duration = 0;
573 gst_adapter_push (rtpmp4vpay->adapter, buffer);
575 rtpmp4vpay->duration += duration;
581 gst_rtp_mp4v_pay_sink_event (GstRTPBasePayload * pay, GstEvent * event)
583 GstRtpMP4VPay *rtpmp4vpay;
585 rtpmp4vpay = GST_RTP_MP4V_PAY (pay);
587 GST_DEBUG ("Got event: %s", GST_EVENT_TYPE_NAME (event));
589 switch (GST_EVENT_TYPE (event)) {
590 case GST_EVENT_SEGMENT:
592 /* This flush call makes sure that the last buffer is always pushed
593 * to the base payloader */
594 gst_rtp_mp4v_pay_flush (rtpmp4vpay);
596 case GST_EVENT_FLUSH_STOP:
597 gst_rtp_mp4v_pay_empty (rtpmp4vpay);
603 /* let parent handle event too */
604 return GST_RTP_BASE_PAYLOAD_CLASS (parent_class)->sink_event (pay, event);
608 gst_rtp_mp4v_pay_set_property (GObject * object, guint prop_id,
609 const GValue * value, GParamSpec * pspec)
611 GstRtpMP4VPay *rtpmp4vpay;
613 rtpmp4vpay = GST_RTP_MP4V_PAY (object);
616 case ARG_SEND_CONFIG:
617 rtpmp4vpay->send_config = g_value_get_boolean (value);
618 /* send the configuration once every minute */
619 if (rtpmp4vpay->send_config && !(rtpmp4vpay->config_interval > 0)) {
620 rtpmp4vpay->config_interval = 60;
623 case ARG_BUFFER_LIST:
624 rtpmp4vpay->buffer_list = g_value_get_boolean (value);
626 case ARG_CONFIG_INTERVAL:
627 rtpmp4vpay->config_interval = g_value_get_uint (value);
635 gst_rtp_mp4v_pay_get_property (GObject * object, guint prop_id,
636 GValue * value, GParamSpec * pspec)
638 GstRtpMP4VPay *rtpmp4vpay;
640 rtpmp4vpay = GST_RTP_MP4V_PAY (object);
643 case ARG_SEND_CONFIG:
644 g_value_set_boolean (value, rtpmp4vpay->send_config);
646 case ARG_BUFFER_LIST:
647 g_value_set_boolean (value, rtpmp4vpay->buffer_list);
649 case ARG_CONFIG_INTERVAL:
650 g_value_set_uint (value, rtpmp4vpay->config_interval);
658 gst_rtp_mp4v_pay_plugin_init (GstPlugin * plugin)
660 return gst_element_register (plugin, "rtpmp4vpay",
661 GST_RANK_SECONDARY, GST_TYPE_RTP_MP4V_PAY);