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 (GstBaseRTPPayload * payload,
79 static GstFlowReturn gst_rtp_mp4v_pay_handle_buffer (GstBaseRTPPayload *
80 payload, GstBuffer * buffer);
81 static gboolean gst_rtp_mp4v_pay_handle_event (GstBaseRTPPayload * pay,
84 #define gst_rtp_mp4v_pay_parent_class parent_class
85 G_DEFINE_TYPE (GstRtpMP4VPay, gst_rtp_mp4v_pay, GST_TYPE_BASE_RTP_PAYLOAD)
87 static void gst_rtp_mp4v_pay_class_init (GstRtpMP4VPayClass * klass)
89 GObjectClass *gobject_class;
90 GstElementClass *gstelement_class;
91 GstBaseRTPPayloadClass *gstbasertppayload_class;
93 gobject_class = (GObjectClass *) klass;
94 gstelement_class = (GstElementClass *) klass;
95 gstbasertppayload_class = (GstBaseRTPPayloadClass *) 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 gstbasertppayload_class->set_caps = gst_rtp_mp4v_pay_setcaps;
132 gstbasertppayload_class->handle_buffer = gst_rtp_mp4v_pay_handle_buffer;
133 gstbasertppayload_class->handle_event = gst_rtp_mp4v_pay_handle_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_basertppayload_set_outcaps (GST_BASE_RTP_PAYLOAD (rtpmp4vpay),
184 "profile-level-id", G_TYPE_STRING, profile,
185 "config", G_TYPE_STRING, config, NULL);
196 gst_rtp_mp4v_pay_setcaps (GstBaseRTPPayload * payload, GstCaps * caps)
198 GstRtpMP4VPay *rtpmp4vpay;
199 GstStructure *structure;
200 const GValue *codec_data;
203 rtpmp4vpay = GST_RTP_MP4V_PAY (payload);
205 gst_basertppayload_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 ();
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_BASE_RTP_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);
313 ret = gst_basertppayload_push (GST_BASE_RTP_PAYLOAD (rtpmp4vpay), outbuf);
317 if (rtpmp4vpay->buffer_list) {
318 /* push the whole buffer list at once */
320 gst_basertppayload_push_list (GST_BASE_RTP_PAYLOAD (rtpmp4vpay), list);
326 #define VOS_STARTCODE 0x000001B0
327 #define VOS_ENDCODE 0x000001B1
328 #define USER_DATA_STARTCODE 0x000001B2
329 #define GOP_STARTCODE 0x000001B3
330 #define VISUAL_OBJECT_STARTCODE 0x000001B5
331 #define VOP_STARTCODE 0x000001B6
334 gst_rtp_mp4v_pay_depay_data (GstRtpMP4VPay * enc, guint8 * data, guint size,
335 gint * strip, gboolean * vopi)
346 code = GST_READ_UINT32_BE (data);
347 GST_DEBUG_OBJECT (enc, "start code 0x%08x", code);
355 gboolean newprofile = FALSE;
358 if (code == VOS_STARTCODE) {
359 /* profile_and_level_indication */
362 GST_DEBUG_OBJECT (enc, "VOS profile 0x%08x", profile);
364 if (profile != enc->profile) {
366 enc->profile = profile;
370 /* up to the next GOP_STARTCODE or VOP_STARTCODE is
371 * the config information */
373 for (i = 5; i < size - 4; i++) {
374 code = (code << 8) | data[i];
375 if (code == GOP_STARTCODE || code == VOP_STARTCODE)
379 /* see if config changed */
382 if (gst_buffer_get_size (enc->config) == i) {
383 equal = gst_buffer_memcmp (enc->config, 0, data, i) == 0;
386 /* if config string changed or new profile, make new caps */
387 if (!equal || newprofile) {
391 gst_buffer_unref (enc->config);
392 enc->config = gst_buffer_new_and_alloc (i);
393 bdata = gst_buffer_map (enc->config, NULL, NULL, GST_MAP_WRITE);
394 memcpy (bdata, data, i);
395 gst_buffer_unmap (enc->config, bdata, -1);
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 (GstBaseRTPPayload * basepayload,
442 GstRtpMP4VPay *rtpmp4vpay;
450 GstClockTime timestamp, duration;
452 gboolean send_config;
457 rtpmp4vpay = GST_RTP_MP4V_PAY (basepayload);
459 data = gst_buffer_map (buffer, &size, NULL, GST_MAP_READ);
460 timestamp = GST_BUFFER_TIMESTAMP (buffer);
461 duration = GST_BUFFER_DURATION (buffer);
462 avail = gst_adapter_available (rtpmp4vpay->adapter);
467 /* empty buffer, take timestamp */
469 rtpmp4vpay->first_timestamp = timestamp;
470 rtpmp4vpay->duration = 0;
473 /* depay incomming data and see if we need to start a new RTP
475 flush = gst_rtp_mp4v_pay_depay_data (rtpmp4vpay, data, size, &strip, &vopi);
476 gst_buffer_unmap (buffer, data, -1);
480 /* strip off config if requested */
481 if (!(rtpmp4vpay->config_interval > 0)) {
484 GST_LOG_OBJECT (rtpmp4vpay, "stripping config at %d, size %d", strip,
485 (gint) size - strip);
487 /* strip off header */
488 subbuf = gst_buffer_copy_region (buffer, GST_BUFFER_COPY_MEMORY, strip,
490 GST_BUFFER_TIMESTAMP (subbuf) = timestamp;
491 gst_buffer_unref (buffer);
494 size = gst_buffer_get_size (buffer);
496 GST_LOG_OBJECT (rtpmp4vpay, "found config in stream");
497 rtpmp4vpay->last_config = timestamp;
501 /* there is a config request, see if we need to insert it */
502 if (vopi && (rtpmp4vpay->config_interval > 0) && rtpmp4vpay->config) {
503 if (rtpmp4vpay->last_config != -1) {
506 GST_LOG_OBJECT (rtpmp4vpay,
507 "now %" GST_TIME_FORMAT ", last VOP-I %" GST_TIME_FORMAT,
508 GST_TIME_ARGS (timestamp), GST_TIME_ARGS (rtpmp4vpay->last_config));
510 /* calculate diff between last config in milliseconds */
511 if (timestamp > rtpmp4vpay->last_config) {
512 diff = timestamp - rtpmp4vpay->last_config;
517 GST_DEBUG_OBJECT (rtpmp4vpay,
518 "interval since last config %" GST_TIME_FORMAT, GST_TIME_ARGS (diff));
520 /* bigger than interval, queue config */
521 /* FIXME should convert timestamps to running time */
522 if (GST_TIME_AS_SECONDS (diff) >= rtpmp4vpay->config_interval) {
523 GST_DEBUG_OBJECT (rtpmp4vpay, "time to send config");
527 /* no known previous config time, send now */
528 GST_DEBUG_OBJECT (rtpmp4vpay, "no previous config time, send now");
533 /* we need to send config now first */
536 GST_LOG_OBJECT (rtpmp4vpay, "inserting config in stream");
539 superbuf = gst_buffer_merge (rtpmp4vpay->config, buffer);
541 GST_BUFFER_TIMESTAMP (superbuf) = timestamp;
542 gst_buffer_unref (buffer);
545 size = gst_buffer_get_size (buffer);
547 if (timestamp != -1) {
548 rtpmp4vpay->last_config = timestamp;
553 /* if we need to flush, do so now */
555 ret = gst_rtp_mp4v_pay_flush (rtpmp4vpay);
556 rtpmp4vpay->first_timestamp = timestamp;
557 rtpmp4vpay->duration = 0;
561 /* get packet length of data and see if we exceeded MTU. */
562 packet_len = gst_rtp_buffer_calc_packet_len (avail + size, 0, 0);
564 if (gst_basertppayload_is_filled (basepayload,
565 packet_len, rtpmp4vpay->duration + duration)) {
566 ret = gst_rtp_mp4v_pay_flush (rtpmp4vpay);
567 rtpmp4vpay->first_timestamp = timestamp;
568 rtpmp4vpay->duration = 0;
572 gst_adapter_push (rtpmp4vpay->adapter, buffer);
574 rtpmp4vpay->duration += duration;
580 gst_rtp_mp4v_pay_handle_event (GstBaseRTPPayload * pay, GstEvent * event)
582 GstRtpMP4VPay *rtpmp4vpay;
584 rtpmp4vpay = GST_RTP_MP4V_PAY (pay);
586 GST_DEBUG ("Got event: %s", GST_EVENT_TYPE_NAME (event));
588 switch (GST_EVENT_TYPE (event)) {
589 case GST_EVENT_SEGMENT:
591 /* This flush call makes sure that the last buffer is always pushed
592 * to the base payloader */
593 gst_rtp_mp4v_pay_flush (rtpmp4vpay);
595 case GST_EVENT_FLUSH_STOP:
596 gst_rtp_mp4v_pay_empty (rtpmp4vpay);
602 /* let parent handle event too */
607 gst_rtp_mp4v_pay_set_property (GObject * object, guint prop_id,
608 const GValue * value, GParamSpec * pspec)
610 GstRtpMP4VPay *rtpmp4vpay;
612 rtpmp4vpay = GST_RTP_MP4V_PAY (object);
615 case ARG_SEND_CONFIG:
616 rtpmp4vpay->send_config = g_value_get_boolean (value);
617 /* send the configuration once every minute */
618 if (rtpmp4vpay->send_config && !(rtpmp4vpay->config_interval > 0)) {
619 rtpmp4vpay->config_interval = 60;
622 case ARG_BUFFER_LIST:
623 rtpmp4vpay->buffer_list = g_value_get_boolean (value);
625 case ARG_CONFIG_INTERVAL:
626 rtpmp4vpay->config_interval = g_value_get_uint (value);
634 gst_rtp_mp4v_pay_get_property (GObject * object, guint prop_id,
635 GValue * value, GParamSpec * pspec)
637 GstRtpMP4VPay *rtpmp4vpay;
639 rtpmp4vpay = GST_RTP_MP4V_PAY (object);
642 case ARG_SEND_CONFIG:
643 g_value_set_boolean (value, rtpmp4vpay->send_config);
645 case ARG_BUFFER_LIST:
646 g_value_set_boolean (value, rtpmp4vpay->buffer_list);
648 case ARG_CONFIG_INTERVAL:
649 g_value_set_uint (value, rtpmp4vpay->config_interval);
657 gst_rtp_mp4v_pay_plugin_init (GstPlugin * plugin)
659 return gst_element_register (plugin, "rtpmp4vpay",
660 GST_RANK_SECONDARY, GST_TYPE_RTP_MP4V_PAY);