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., 51 Franklin St, Fifth Floor,
17 * Boston, MA 02110-1301, USA.
26 #include <gst/rtp/gstrtpbuffer.h>
27 #include <gst/video/video.h>
29 #include "gstrtpmp4vpay.h"
30 #include "gstrtputils.h"
32 GST_DEBUG_CATEGORY_STATIC (rtpmp4vpay_debug);
33 #define GST_CAT_DEFAULT (rtpmp4vpay_debug)
35 static GstStaticPadTemplate gst_rtp_mp4v_pay_sink_template =
36 GST_STATIC_PAD_TEMPLATE ("sink",
39 GST_STATIC_CAPS ("video/mpeg,"
40 "mpegversion=(int) 4, systemstream=(boolean)false;" "video/x-divx")
43 static GstStaticPadTemplate gst_rtp_mp4v_pay_src_template =
44 GST_STATIC_PAD_TEMPLATE ("src",
47 GST_STATIC_CAPS ("application/x-rtp, "
48 "media = (string) \"video\", "
49 "payload = (int) " GST_RTP_PAYLOAD_DYNAMIC_STRING ", "
50 "clock-rate = (int) [1, MAX ], " "encoding-name = (string) \"MP4V-ES\""
53 "profile-level-id = (string) [1,MAX]"
54 "config = (string) [1,MAX]"
59 #define DEFAULT_CONFIG_INTERVAL 0
68 static void gst_rtp_mp4v_pay_finalize (GObject * object);
70 static void gst_rtp_mp4v_pay_set_property (GObject * object, guint prop_id,
71 const GValue * value, GParamSpec * pspec);
72 static void gst_rtp_mp4v_pay_get_property (GObject * object, guint prop_id,
73 GValue * value, GParamSpec * pspec);
75 static gboolean gst_rtp_mp4v_pay_setcaps (GstRTPBasePayload * payload,
77 static GstFlowReturn gst_rtp_mp4v_pay_handle_buffer (GstRTPBasePayload *
78 payload, GstBuffer * buffer);
79 static gboolean gst_rtp_mp4v_pay_sink_event (GstRTPBasePayload * pay,
82 #define gst_rtp_mp4v_pay_parent_class parent_class
83 G_DEFINE_TYPE (GstRtpMP4VPay, gst_rtp_mp4v_pay, GST_TYPE_RTP_BASE_PAYLOAD);
86 gst_rtp_mp4v_pay_class_init (GstRtpMP4VPayClass * klass)
88 GObjectClass *gobject_class;
89 GstElementClass *gstelement_class;
90 GstRTPBasePayloadClass *gstrtpbasepayload_class;
92 gobject_class = (GObjectClass *) klass;
93 gstelement_class = (GstElementClass *) klass;
94 gstrtpbasepayload_class = (GstRTPBasePayloadClass *) klass;
96 gobject_class->set_property = gst_rtp_mp4v_pay_set_property;
97 gobject_class->get_property = gst_rtp_mp4v_pay_get_property;
99 gst_element_class_add_static_pad_template (gstelement_class,
100 &gst_rtp_mp4v_pay_src_template);
101 gst_element_class_add_static_pad_template (gstelement_class,
102 &gst_rtp_mp4v_pay_sink_template);
104 gst_element_class_set_static_metadata (gstelement_class,
105 "RTP MPEG4 Video payloader", "Codec/Payloader/Network/RTP",
106 "Payload MPEG-4 video as RTP packets (RFC 3016)",
107 "Wim Taymans <wim.taymans@gmail.com>");
109 g_object_class_install_property (G_OBJECT_CLASS (klass), PROP_CONFIG_INTERVAL,
110 g_param_spec_uint ("config-interval", "Config Send Interval",
111 "Send Config Insertion Interval in seconds (configuration headers "
112 "will be multiplexed in the data stream when detected.) (0 = disabled)",
113 0, 3600, DEFAULT_CONFIG_INTERVAL,
114 G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS)
117 gobject_class->finalize = gst_rtp_mp4v_pay_finalize;
119 gstrtpbasepayload_class->set_caps = gst_rtp_mp4v_pay_setcaps;
120 gstrtpbasepayload_class->handle_buffer = gst_rtp_mp4v_pay_handle_buffer;
121 gstrtpbasepayload_class->sink_event = gst_rtp_mp4v_pay_sink_event;
123 GST_DEBUG_CATEGORY_INIT (rtpmp4vpay_debug, "rtpmp4vpay", 0,
124 "MP4 video RTP Payloader");
128 gst_rtp_mp4v_pay_init (GstRtpMP4VPay * rtpmp4vpay)
130 rtpmp4vpay->adapter = gst_adapter_new ();
131 rtpmp4vpay->rate = 90000;
132 rtpmp4vpay->profile = 1;
133 rtpmp4vpay->need_config = TRUE;
134 rtpmp4vpay->config_interval = DEFAULT_CONFIG_INTERVAL;
135 rtpmp4vpay->last_config = -1;
137 rtpmp4vpay->config = NULL;
141 gst_rtp_mp4v_pay_finalize (GObject * object)
143 GstRtpMP4VPay *rtpmp4vpay;
145 rtpmp4vpay = GST_RTP_MP4V_PAY (object);
147 if (rtpmp4vpay->config) {
148 gst_buffer_unref (rtpmp4vpay->config);
149 rtpmp4vpay->config = NULL;
151 g_object_unref (rtpmp4vpay->adapter);
152 rtpmp4vpay->adapter = NULL;
154 G_OBJECT_CLASS (parent_class)->finalize (object);
158 gst_rtp_mp4v_pay_new_caps (GstRtpMP4VPay * rtpmp4vpay)
160 gchar *profile, *config;
164 profile = g_strdup_printf ("%d", rtpmp4vpay->profile);
165 g_value_init (&v, GST_TYPE_BUFFER);
166 gst_value_set_buffer (&v, rtpmp4vpay->config);
167 config = gst_value_serialize (&v);
169 res = gst_rtp_base_payload_set_outcaps (GST_RTP_BASE_PAYLOAD (rtpmp4vpay),
170 "profile-level-id", G_TYPE_STRING, profile,
171 "config", G_TYPE_STRING, config, NULL);
182 gst_rtp_mp4v_pay_setcaps (GstRTPBasePayload * payload, GstCaps * caps)
184 GstRtpMP4VPay *rtpmp4vpay;
185 GstStructure *structure;
186 const GValue *codec_data;
189 rtpmp4vpay = GST_RTP_MP4V_PAY (payload);
191 gst_rtp_base_payload_set_options (payload, "video", TRUE, "MP4V-ES",
196 structure = gst_caps_get_structure (caps, 0);
197 codec_data = gst_structure_get_value (structure, "codec_data");
199 GST_LOG_OBJECT (rtpmp4vpay, "got codec_data");
200 if (G_VALUE_TYPE (codec_data) == GST_TYPE_BUFFER) {
203 buffer = gst_value_get_buffer (codec_data);
205 if (gst_buffer_get_size (buffer) < 5)
208 gst_buffer_extract (buffer, 4, &rtpmp4vpay->profile, 1);
209 GST_LOG_OBJECT (rtpmp4vpay, "configuring codec_data, profile %d",
210 rtpmp4vpay->profile);
212 if (rtpmp4vpay->config)
213 gst_buffer_unref (rtpmp4vpay->config);
214 rtpmp4vpay->config = gst_buffer_copy (buffer);
215 res = gst_rtp_mp4v_pay_new_caps (rtpmp4vpay);
224 gst_rtp_mp4v_pay_empty (GstRtpMP4VPay * rtpmp4vpay)
226 gst_adapter_clear (rtpmp4vpay->adapter);
229 #define RTP_HEADER_LEN 12
232 gst_rtp_mp4v_pay_flush (GstRtpMP4VPay * rtpmp4vpay)
236 GstBuffer *outbuf_data = NULL;
238 GstBufferList *list = NULL;
240 /* the data available in the adapter is either smaller
241 * than the MTU or bigger. In the case it is smaller, the complete
242 * adapter contents can be put in one packet. In the case the
243 * adapter has more than one MTU, we need to split the MP4V data
244 * over multiple packets. */
245 avail = gst_adapter_available (rtpmp4vpay->adapter);
247 if (rtpmp4vpay->config == NULL && rtpmp4vpay->need_config) {
248 /* when we don't have a config yet, flush things out */
249 gst_adapter_flush (rtpmp4vpay->adapter, avail);
256 mtu = GST_RTP_BASE_PAYLOAD_MTU (rtpmp4vpay);
258 /* Use buffer lists. Each frame will be put into a list
259 * of buffers and the whole list will be pushed downstream
261 list = gst_buffer_list_new_sized ((avail / (mtu - RTP_HEADER_LEN)) + 1);
267 GstRTPBuffer rtp = { NULL };
269 /* this will be the total lenght of the packet */
270 packet_len = gst_rtp_buffer_calc_packet_len (avail, 0, 0);
272 /* fill one MTU or all available bytes */
273 towrite = MIN (packet_len, mtu);
275 /* this is the payload length */
276 payload_len = gst_rtp_buffer_calc_payload_len (towrite, 0, 0);
278 /* create buffer without payload. The payload will be put
279 * in next buffer instead. Both buffers will be merged */
280 outbuf = gst_rtp_buffer_new_allocate (0, 0, 0);
282 /* Take buffer with the payload from the adapter */
283 outbuf_data = gst_adapter_take_buffer_fast (rtpmp4vpay->adapter,
286 avail -= payload_len;
288 gst_rtp_buffer_map (outbuf, GST_MAP_WRITE, &rtp);
289 gst_rtp_buffer_set_marker (&rtp, avail == 0);
290 gst_rtp_buffer_unmap (&rtp);
291 gst_rtp_copy_video_meta (rtpmp4vpay, outbuf, outbuf_data);
292 outbuf = gst_buffer_append (outbuf, outbuf_data);
294 GST_BUFFER_PTS (outbuf) = rtpmp4vpay->first_timestamp;
297 gst_buffer_list_insert (list, -1, outbuf);
300 /* push the whole buffer list at once */
302 gst_rtp_base_payload_push_list (GST_RTP_BASE_PAYLOAD (rtpmp4vpay), list);
307 #define VOS_STARTCODE 0x000001B0
308 #define VOS_ENDCODE 0x000001B1
309 #define USER_DATA_STARTCODE 0x000001B2
310 #define GOP_STARTCODE 0x000001B3
311 #define VISUAL_OBJECT_STARTCODE 0x000001B5
312 #define VOP_STARTCODE 0x000001B6
315 gst_rtp_mp4v_pay_depay_data (GstRtpMP4VPay * enc, guint8 * data, guint size,
316 gint * strip, gboolean * vopi)
327 code = GST_READ_UINT32_BE (data);
328 GST_DEBUG_OBJECT (enc, "start code 0x%08x", code);
336 gboolean newprofile = FALSE;
339 if (code == VOS_STARTCODE) {
340 /* profile_and_level_indication */
343 GST_DEBUG_OBJECT (enc, "VOS profile 0x%08x", profile);
345 if (profile != enc->profile) {
347 enc->profile = profile;
351 /* up to the next GOP_STARTCODE or VOP_STARTCODE is
352 * the config information */
354 for (i = 5; i < size - 4; i++) {
355 code = (code << 8) | data[i];
356 if (code == GOP_STARTCODE || code == VOP_STARTCODE)
360 /* see if config changed */
363 if (gst_buffer_get_size (enc->config) == i) {
364 equal = gst_buffer_memcmp (enc->config, 0, data, i) == 0;
367 /* if config string changed or new profile, make new caps */
368 if (!equal || newprofile) {
370 gst_buffer_unref (enc->config);
371 enc->config = gst_buffer_new_and_alloc (i);
373 gst_buffer_fill (enc->config, 0, data, i);
375 gst_rtp_mp4v_pay_new_caps (enc);
378 /* we need to flush out the current packet. */
383 GST_DEBUG_OBJECT (enc, "VOP");
384 /* VOP startcode, we don't have to flush the packet */
386 /* vop-coding-type == I-frame */
387 if (size > 4 && (data[4] >> 6 == 0)) {
388 GST_DEBUG_OBJECT (enc, "VOP-I");
393 GST_DEBUG_OBJECT (enc, "GOP");
398 enc->need_config = FALSE;
402 if (code >= 0x20 && code <= 0x2f) {
403 GST_DEBUG_OBJECT (enc, "short header");
406 GST_DEBUG_OBJECT (enc, "other startcode");
407 /* all other startcodes need a flush */
415 /* we expect buffers starting on startcodes.
418 gst_rtp_mp4v_pay_handle_buffer (GstRTPBasePayload * basepayload,
421 GstRtpMP4VPay *rtpmp4vpay;
429 GstClockTime timestamp, duration;
431 gboolean send_config;
436 rtpmp4vpay = GST_RTP_MP4V_PAY (basepayload);
438 gst_buffer_map (buffer, &map, GST_MAP_READ);
440 timestamp = GST_BUFFER_PTS (buffer);
441 duration = GST_BUFFER_DURATION (buffer);
442 avail = gst_adapter_available (rtpmp4vpay->adapter);
447 /* empty buffer, take timestamp */
449 rtpmp4vpay->first_timestamp = timestamp;
450 rtpmp4vpay->duration = 0;
453 /* depay incoming data and see if we need to start a new RTP
456 gst_rtp_mp4v_pay_depay_data (rtpmp4vpay, map.data, size, &strip, &vopi);
457 gst_buffer_unmap (buffer, &map);
460 /* strip off config if requested */
461 if (!(rtpmp4vpay->config_interval > 0)) {
464 GST_LOG_OBJECT (rtpmp4vpay, "stripping config at %d, size %d", strip,
465 (gint) size - strip);
467 /* strip off header */
468 subbuf = gst_buffer_copy_region (buffer, GST_BUFFER_COPY_ALL, strip,
470 GST_BUFFER_PTS (subbuf) = timestamp;
471 gst_buffer_unref (buffer);
474 size = gst_buffer_get_size (buffer);
476 GstClockTime running_time =
477 gst_segment_to_running_time (&basepayload->segment, GST_FORMAT_TIME,
480 GST_LOG_OBJECT (rtpmp4vpay, "found config in stream");
481 rtpmp4vpay->last_config = running_time;
485 /* there is a config request, see if we need to insert it */
486 if (vopi && (rtpmp4vpay->config_interval > 0) && rtpmp4vpay->config) {
487 GstClockTime running_time =
488 gst_segment_to_running_time (&basepayload->segment, GST_FORMAT_TIME,
491 if (rtpmp4vpay->last_config != -1) {
494 GST_LOG_OBJECT (rtpmp4vpay,
495 "now %" GST_TIME_FORMAT ", last VOP-I %" GST_TIME_FORMAT,
496 GST_TIME_ARGS (running_time),
497 GST_TIME_ARGS (rtpmp4vpay->last_config));
499 /* calculate diff between last config in milliseconds */
500 if (running_time > rtpmp4vpay->last_config) {
501 diff = running_time - rtpmp4vpay->last_config;
506 GST_DEBUG_OBJECT (rtpmp4vpay,
507 "interval since last config %" GST_TIME_FORMAT, GST_TIME_ARGS (diff));
509 /* bigger than interval, queue config */
510 if (GST_TIME_AS_SECONDS (diff) >= rtpmp4vpay->config_interval) {
511 GST_DEBUG_OBJECT (rtpmp4vpay, "time to send config");
515 /* no known previous config time, send now */
516 GST_DEBUG_OBJECT (rtpmp4vpay, "no previous config time, send now");
521 /* we need to send config now first */
522 GST_LOG_OBJECT (rtpmp4vpay, "inserting config in stream");
525 buffer = gst_buffer_append (gst_buffer_ref (rtpmp4vpay->config), buffer);
527 GST_BUFFER_PTS (buffer) = timestamp;
528 size = gst_buffer_get_size (buffer);
530 if (running_time != -1) {
531 rtpmp4vpay->last_config = running_time;
536 /* if we need to flush, do so now */
538 ret = gst_rtp_mp4v_pay_flush (rtpmp4vpay);
539 rtpmp4vpay->first_timestamp = timestamp;
540 rtpmp4vpay->duration = 0;
544 /* get packet length of data and see if we exceeded MTU. */
545 packet_len = gst_rtp_buffer_calc_packet_len (avail + size, 0, 0);
547 if (gst_rtp_base_payload_is_filled (basepayload,
548 packet_len, rtpmp4vpay->duration + duration)) {
549 ret = gst_rtp_mp4v_pay_flush (rtpmp4vpay);
550 rtpmp4vpay->first_timestamp = timestamp;
551 rtpmp4vpay->duration = 0;
555 gst_adapter_push (rtpmp4vpay->adapter, buffer);
557 rtpmp4vpay->duration += duration;
563 gst_rtp_mp4v_pay_sink_event (GstRTPBasePayload * pay, GstEvent * event)
565 GstRtpMP4VPay *rtpmp4vpay;
567 rtpmp4vpay = GST_RTP_MP4V_PAY (pay);
569 GST_DEBUG ("Got event: %s", GST_EVENT_TYPE_NAME (event));
571 switch (GST_EVENT_TYPE (event)) {
572 case GST_EVENT_SEGMENT:
574 /* This flush call makes sure that the last buffer is always pushed
575 * to the base payloader */
576 gst_rtp_mp4v_pay_flush (rtpmp4vpay);
578 case GST_EVENT_FLUSH_STOP:
579 gst_rtp_mp4v_pay_empty (rtpmp4vpay);
585 /* let parent handle event too */
586 return GST_RTP_BASE_PAYLOAD_CLASS (parent_class)->sink_event (pay, event);
590 gst_rtp_mp4v_pay_set_property (GObject * object, guint prop_id,
591 const GValue * value, GParamSpec * pspec)
593 GstRtpMP4VPay *rtpmp4vpay;
595 rtpmp4vpay = GST_RTP_MP4V_PAY (object);
598 case PROP_CONFIG_INTERVAL:
599 rtpmp4vpay->config_interval = g_value_get_uint (value);
607 gst_rtp_mp4v_pay_get_property (GObject * object, guint prop_id,
608 GValue * value, GParamSpec * pspec)
610 GstRtpMP4VPay *rtpmp4vpay;
612 rtpmp4vpay = GST_RTP_MP4V_PAY (object);
615 case PROP_CONFIG_INTERVAL:
616 g_value_set_uint (value, rtpmp4vpay->config_interval);
624 gst_rtp_mp4v_pay_plugin_init (GstPlugin * plugin)
626 /* Note: This element is marked at a "+1" rank to make sure that
627 * auto-plugging of payloaders for MPEG4 elementary streams don't
628 * end up using the 'rtpmp4gpay' element (generic mpeg4) which isn't
629 * as well supported as this RFC */
630 return gst_element_register (plugin, "rtpmp4vpay",
631 GST_RANK_SECONDARY + 1, GST_TYPE_RTP_MP4V_PAY);