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 /* elementfactory information */
34 static const GstElementDetails gst_rtp_mp4vpay_details =
35 GST_ELEMENT_DETAILS ("RTP MPEG4 Video payloader",
36 "Codec/Payloader/Network",
37 "Payload MPEG-4 video as RTP packets (RFC 3016)",
38 "Wim Taymans <wim.taymans@gmail.com>");
40 static GstStaticPadTemplate gst_rtp_mp4v_pay_sink_template =
41 GST_STATIC_PAD_TEMPLATE ("sink",
44 GST_STATIC_CAPS ("video/mpeg,"
45 "mpegversion=(int) 4," "systemstream=(boolean)false")
48 static GstStaticPadTemplate gst_rtp_mp4v_pay_src_template =
49 GST_STATIC_PAD_TEMPLATE ("src",
52 GST_STATIC_CAPS ("application/x-rtp, "
53 "media = (string) \"video\", "
54 "payload = (int) " GST_RTP_PAYLOAD_DYNAMIC_STRING ", "
55 "clock-rate = (int) [1, MAX ], " "encoding-name = (string) \"MP4V-ES\""
58 "profile-level-id = (string) [1,MAX]"
59 "config = (string) [1,MAX]"
64 #define DEFAULT_SEND_CONFIG FALSE
73 static void gst_rtp_mp4v_pay_class_init (GstRtpMP4VPayClass * klass);
74 static void gst_rtp_mp4v_pay_base_init (GstRtpMP4VPayClass * klass);
75 static void gst_rtp_mp4v_pay_init (GstRtpMP4VPay * rtpmp4vpay);
76 static void gst_rtp_mp4v_pay_finalize (GObject * object);
78 static void gst_rtp_mp4v_pay_set_property (GObject * object, guint prop_id,
79 const GValue * value, GParamSpec * pspec);
80 static void gst_rtp_mp4v_pay_get_property (GObject * object, guint prop_id,
81 GValue * value, GParamSpec * pspec);
83 static gboolean gst_rtp_mp4v_pay_setcaps (GstBaseRTPPayload * payload,
85 static GstFlowReturn gst_rtp_mp4v_pay_handle_buffer (GstBaseRTPPayload *
86 payload, GstBuffer * buffer);
87 static gboolean gst_rtp_mp4v_pay_event (GstPad * pad, GstEvent * event);
89 static GstBaseRTPPayloadClass *parent_class = NULL;
92 gst_rtp_mp4v_pay_get_type (void)
94 static GType rtpmp4vpay_type = 0;
96 if (!rtpmp4vpay_type) {
97 static const GTypeInfo rtpmp4vpay_info = {
98 sizeof (GstRtpMP4VPayClass),
99 (GBaseInitFunc) gst_rtp_mp4v_pay_base_init,
101 (GClassInitFunc) gst_rtp_mp4v_pay_class_init,
104 sizeof (GstRtpMP4VPay),
106 (GInstanceInitFunc) gst_rtp_mp4v_pay_init,
110 g_type_register_static (GST_TYPE_BASE_RTP_PAYLOAD, "GstRtpMP4VPay",
111 &rtpmp4vpay_info, 0);
113 return rtpmp4vpay_type;
117 gst_rtp_mp4v_pay_base_init (GstRtpMP4VPayClass * klass)
119 GstElementClass *element_class = GST_ELEMENT_CLASS (klass);
121 gst_element_class_add_pad_template (element_class,
122 gst_static_pad_template_get (&gst_rtp_mp4v_pay_src_template));
123 gst_element_class_add_pad_template (element_class,
124 gst_static_pad_template_get (&gst_rtp_mp4v_pay_sink_template));
126 gst_element_class_set_details (element_class, &gst_rtp_mp4vpay_details);
130 gst_rtp_mp4v_pay_class_init (GstRtpMP4VPayClass * klass)
132 GObjectClass *gobject_class;
133 GstElementClass *gstelement_class;
134 GstBaseRTPPayloadClass *gstbasertppayload_class;
136 gobject_class = (GObjectClass *) klass;
137 gstelement_class = (GstElementClass *) klass;
138 gstbasertppayload_class = (GstBaseRTPPayloadClass *) klass;
140 parent_class = g_type_class_peek_parent (klass);
142 gobject_class->set_property = gst_rtp_mp4v_pay_set_property;
143 gobject_class->get_property = gst_rtp_mp4v_pay_get_property;
145 g_object_class_install_property (G_OBJECT_CLASS (klass), ARG_SEND_CONFIG,
146 g_param_spec_boolean ("send-config", "Send Config",
147 "Send the config parameters in RTP packets as well",
148 DEFAULT_SEND_CONFIG, G_PARAM_READWRITE));
150 gobject_class->finalize = gst_rtp_mp4v_pay_finalize;
152 gstbasertppayload_class->set_caps = gst_rtp_mp4v_pay_setcaps;
153 gstbasertppayload_class->handle_buffer = gst_rtp_mp4v_pay_handle_buffer;
155 GST_DEBUG_CATEGORY_INIT (rtpmp4vpay_debug, "rtpmp4vpay", 0,
156 "MP4 video RTP Payloader");
161 gst_rtp_mp4v_pay_init (GstRtpMP4VPay * rtpmp4vpay)
165 rtpmp4vpay->adapter = gst_adapter_new ();
166 rtpmp4vpay->rate = 90000;
167 rtpmp4vpay->profile = 1;
168 rtpmp4vpay->send_config = DEFAULT_SEND_CONFIG;
170 rtpmp4vpay->config = NULL;
172 sinkpad = GST_BASE_RTP_PAYLOAD_SINKPAD (rtpmp4vpay);
174 rtpmp4vpay->old_event_func = sinkpad->eventfunc;
175 gst_pad_set_event_function (sinkpad, gst_rtp_mp4v_pay_event);
179 gst_rtp_mp4v_pay_finalize (GObject * object)
181 GstRtpMP4VPay *rtpmp4vpay;
183 rtpmp4vpay = GST_RTP_MP4V_PAY (object);
185 if (rtpmp4vpay->config) {
186 gst_buffer_unref (rtpmp4vpay->config);
187 rtpmp4vpay->config = NULL;
189 g_object_unref (rtpmp4vpay->adapter);
190 rtpmp4vpay->adapter = NULL;
192 G_OBJECT_CLASS (parent_class)->finalize (object);
196 gst_rtp_mp4v_pay_new_caps (GstRtpMP4VPay * rtpmp4vpay)
198 gchar *profile, *config;
202 profile = g_strdup_printf ("%d", rtpmp4vpay->profile);
203 g_value_init (&v, GST_TYPE_BUFFER);
204 gst_value_set_buffer (&v, rtpmp4vpay->config);
205 config = gst_value_serialize (&v);
207 res = gst_basertppayload_set_outcaps (GST_BASE_RTP_PAYLOAD (rtpmp4vpay),
208 "profile-level-id", G_TYPE_STRING, profile,
209 "config", G_TYPE_STRING, config, NULL);
220 gst_rtp_mp4v_pay_setcaps (GstBaseRTPPayload * payload, GstCaps * caps)
222 GstRtpMP4VPay *rtpmp4vpay;
223 GstStructure *structure;
224 const GValue *codec_data;
227 rtpmp4vpay = GST_RTP_MP4V_PAY (payload);
229 gst_basertppayload_set_options (payload, "video", TRUE, "MP4V-ES",
234 structure = gst_caps_get_structure (caps, 0);
235 codec_data = gst_structure_get_value (structure, "codec_data");
237 GST_LOG_OBJECT (rtpmp4vpay, "got codec_data");
238 if (G_VALUE_TYPE (codec_data) == GST_TYPE_BUFFER) {
243 buffer = gst_value_get_buffer (codec_data);
245 data = GST_BUFFER_DATA (buffer);
246 size = GST_BUFFER_SIZE (buffer);
251 rtpmp4vpay->profile = data[4];
252 GST_LOG_OBJECT (rtpmp4vpay, "configuring codec_data, profile %d",
255 if (rtpmp4vpay->config)
256 gst_buffer_unref (rtpmp4vpay->config);
257 rtpmp4vpay->config = gst_buffer_copy (buffer);
258 res = gst_rtp_mp4v_pay_new_caps (rtpmp4vpay);
267 gst_rtp_mp4v_pay_empty (GstRtpMP4VPay * rtpmp4vpay)
269 gst_adapter_clear (rtpmp4vpay->adapter);
273 gst_rtp_mp4v_pay_flush (GstRtpMP4VPay * rtpmp4vpay)
279 /* the data available in the adapter is either smaller
280 * than the MTU or bigger. In the case it is smaller, the complete
281 * adapter contents can be put in one packet. In the case the
282 * adapter has more than one MTU, we need to split the MP4V data
283 * over multiple packets. */
284 avail = gst_adapter_available (rtpmp4vpay->adapter);
294 /* this will be the total lenght of the packet */
295 packet_len = gst_rtp_buffer_calc_packet_len (avail, 0, 0);
297 /* fill one MTU or all available bytes */
298 towrite = MIN (packet_len, GST_BASE_RTP_PAYLOAD_MTU (rtpmp4vpay));
300 /* this is the payload length */
301 payload_len = gst_rtp_buffer_calc_payload_len (towrite, 0, 0);
303 /* create buffer to hold the payload */
304 outbuf = gst_rtp_buffer_new_allocate (payload_len, 0, 0);
307 payload = gst_rtp_buffer_get_payload (outbuf);
309 gst_adapter_copy (rtpmp4vpay->adapter, payload, 0, payload_len);
310 gst_adapter_flush (rtpmp4vpay->adapter, payload_len);
312 avail -= payload_len;
314 gst_rtp_buffer_set_marker (outbuf, avail == 0);
316 GST_BUFFER_TIMESTAMP (outbuf) = rtpmp4vpay->first_timestamp;
318 ret = gst_basertppayload_push (GST_BASE_RTP_PAYLOAD (rtpmp4vpay), outbuf);
324 #define VOS_STARTCODE 0x000001B0
325 #define VOS_ENDCODE 0x000001B1
326 #define USER_DATA_STARTCODE 0x000001B2
327 #define GOP_STARTCODE 0x000001B3
328 #define VISUAL_OBJECT_STARTCODE 0x000001B5
329 #define VOP_STARTCODE 0x000001B6
332 gst_rtp_mp4v_pay_depay_data (GstRtpMP4VPay * enc, guint8 * data, guint size,
343 code = GST_READ_UINT32_BE (data);
344 GST_DEBUG_OBJECT (enc, "start code 0x%08x", code);
351 gboolean newprofile = FALSE;
354 /* profile_and_level_indication */
357 GST_DEBUG_OBJECT (enc, "VOS profile 0x%08x", profile);
359 if (profile != enc->profile) {
361 enc->profile = profile;
364 /* up to the next GOP_STARTCODE or VOP_STARTCODE is
365 * the config information */
367 for (i = 5; i < size - 4; i++) {
368 code = (code << 8) | data[i];
369 if (code == GOP_STARTCODE || code == VOP_STARTCODE)
373 /* see if config changed */
376 if (GST_BUFFER_SIZE (enc->config) == i) {
377 equal = memcmp (GST_BUFFER_DATA (enc->config), data, i) == 0;
380 /* if config string changed or new profile, make new caps */
381 if (!equal || newprofile) {
383 gst_buffer_unref (enc->config);
384 enc->config = gst_buffer_new_and_alloc (i);
385 memcpy (GST_BUFFER_DATA (enc->config), data, i);
386 gst_rtp_mp4v_pay_new_caps (enc);
389 /* we need to flush out the current packet. */
394 GST_DEBUG_OBJECT (enc, "VOP");
395 /* VOP startcode, we don't have to flush the packet */
399 GST_DEBUG_OBJECT (enc, "other startcode");
400 /* all other startcodes need a flush */
407 /* we expect buffers starting on startcodes.
410 gst_rtp_mp4v_pay_handle_buffer (GstBaseRTPPayload * basepayload,
413 GstRtpMP4VPay *rtpmp4vpay;
420 GstClockTime timestamp, duration;
424 rtpmp4vpay = GST_RTP_MP4V_PAY (basepayload);
426 size = GST_BUFFER_SIZE (buffer);
427 data = GST_BUFFER_DATA (buffer);
428 timestamp = GST_BUFFER_TIMESTAMP (buffer);
429 duration = GST_BUFFER_DURATION (buffer);
430 avail = gst_adapter_available (rtpmp4vpay->adapter);
435 /* empty buffer, take timestamp */
437 rtpmp4vpay->first_timestamp = timestamp;
438 rtpmp4vpay->duration = 0;
441 /* depay incomming data and see if we need to start a new RTP
443 flush = gst_rtp_mp4v_pay_depay_data (rtpmp4vpay, data, size, &strip);
445 /* strip off config if requested */
446 if (!rtpmp4vpay->send_config) {
449 /* strip off header */
450 subbuf = gst_buffer_create_sub (buffer, strip, size - strip);
451 GST_BUFFER_TIMESTAMP (subbuf) = timestamp;
452 gst_buffer_unref (buffer);
455 size = GST_BUFFER_SIZE (buffer);
456 data = GST_BUFFER_DATA (buffer);
460 /* if we need to flush, do so now */
462 ret = gst_rtp_mp4v_pay_flush (rtpmp4vpay);
463 rtpmp4vpay->first_timestamp = timestamp;
464 rtpmp4vpay->duration = 0;
468 /* get packet length of data and see if we exceeded MTU. */
469 packet_len = gst_rtp_buffer_calc_packet_len (avail + size, 0, 0);
471 if (gst_basertppayload_is_filled (basepayload,
472 packet_len, rtpmp4vpay->duration + duration)) {
473 ret = gst_rtp_mp4v_pay_flush (rtpmp4vpay);
474 rtpmp4vpay->first_timestamp = timestamp;
475 rtpmp4vpay->duration = 0;
479 gst_adapter_push (rtpmp4vpay->adapter, buffer);
481 rtpmp4vpay->duration += duration;
487 gst_rtp_mp4v_pay_event (GstPad * pad, GstEvent * event)
489 GstRtpMP4VPay *rtpmp4vpay;
492 rtpmp4vpay = GST_RTP_MP4V_PAY (gst_pad_get_parent (pad));
494 GST_DEBUG ("Got event: %s", GST_EVENT_TYPE_NAME (event));
496 switch (GST_EVENT_TYPE (event)) {
497 case GST_EVENT_NEWSEGMENT:
499 /* This flush call makes sure that the last buffer is always pushed
500 * to the base payloader */
501 gst_rtp_mp4v_pay_flush (rtpmp4vpay);
503 case GST_EVENT_FLUSH_STOP:
504 gst_rtp_mp4v_pay_empty (rtpmp4vpay);
510 ret = rtpmp4vpay->old_event_func (pad, event);
512 g_object_unref (rtpmp4vpay);
518 gst_rtp_mp4v_pay_set_property (GObject * object, guint prop_id,
519 const GValue * value, GParamSpec * pspec)
521 GstRtpMP4VPay *rtpmp4vpay;
523 rtpmp4vpay = GST_RTP_MP4V_PAY (object);
526 case ARG_SEND_CONFIG:
527 rtpmp4vpay->send_config = g_value_get_boolean (value);
535 gst_rtp_mp4v_pay_get_property (GObject * object, guint prop_id,
536 GValue * value, GParamSpec * pspec)
538 GstRtpMP4VPay *rtpmp4vpay;
540 rtpmp4vpay = GST_RTP_MP4V_PAY (object);
543 case ARG_SEND_CONFIG:
544 g_value_set_boolean (value, rtpmp4vpay->send_config);
552 gst_rtp_mp4v_pay_plugin_init (GstPlugin * plugin)
554 return gst_element_register (plugin, "rtpmp4vpay",
555 GST_RANK_NONE, GST_TYPE_RTP_MP4V_PAY);