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., 59 Temple Place - Suite 330,
17 * Boston, MA 02111-1307, USA.
26 #include <gst/base/gstbitreader.h>
27 #include <gst/rtp/gstrtpbuffer.h>
29 #include "gstrtpmp4gpay.h"
31 GST_DEBUG_CATEGORY_STATIC (rtpmp4gpay_debug);
32 #define GST_CAT_DEFAULT (rtpmp4gpay_debug)
34 static GstStaticPadTemplate gst_rtp_mp4g_pay_sink_template =
35 GST_STATIC_PAD_TEMPLATE ("sink",
38 GST_STATIC_CAPS ("video/mpeg,"
39 "mpegversion=(int) 4,"
40 "systemstream=(boolean)false;"
41 "audio/mpeg," "mpegversion=(int) 4, " "stream-format=(string) raw")
44 static GstStaticPadTemplate gst_rtp_mp4g_pay_src_template =
45 GST_STATIC_PAD_TEMPLATE ("src",
48 GST_STATIC_CAPS ("application/x-rtp, "
49 "media = (string) { \"video\", \"audio\", \"application\" }, "
50 "payload = (int) " GST_RTP_PAYLOAD_DYNAMIC_STRING ", "
51 "clock-rate = (int) [1, MAX ], "
52 "encoding-name = (string) \"MPEG4-GENERIC\", "
53 /* required string params */
54 "streamtype = (string) { \"4\", \"5\" }, " /* 4 = video, 5 = audio */
55 /* "profile-level-id = (string) [1,MAX], " */
56 /* "config = (string) [1,MAX]" */
57 "mode = (string) { \"generic\", \"CELP-cbr\", \"CELP-vbr\", \"AAC-lbr\", \"AAC-hbr\" } "
58 /* Optional general parameters */
59 /* "objecttype = (string) [1,MAX], " */
60 /* "constantsize = (string) [1,MAX], " *//* constant size of each AU */
61 /* "constantduration = (string) [1,MAX], " *//* constant duration of each AU */
62 /* "maxdisplacement = (string) [1,MAX], " */
63 /* "de-interleavebuffersize = (string) [1,MAX], " */
64 /* Optional configuration parameters */
65 /* "sizelength = (string) [1, 16], " *//* max 16 bits, should be enough... */
66 /* "indexlength = (string) [1, 8], " */
67 /* "indexdeltalength = (string) [1, 8], " */
68 /* "ctsdeltalength = (string) [1, 64], " */
69 /* "dtsdeltalength = (string) [1, 64], " */
70 /* "randomaccessindication = (string) {0, 1}, " */
71 /* "streamstateindication = (string) [0, 64], " */
72 /* "auxiliarydatasizelength = (string) [0, 64]" */ )
76 static void gst_rtp_mp4g_pay_finalize (GObject * object);
78 static GstStateChangeReturn gst_rtp_mp4g_pay_change_state (GstElement * element,
79 GstStateChange transition);
81 static gboolean gst_rtp_mp4g_pay_setcaps (GstBaseRTPPayload * payload,
83 static GstFlowReturn gst_rtp_mp4g_pay_handle_buffer (GstBaseRTPPayload *
84 payload, GstBuffer * buffer);
85 static gboolean gst_rtp_mp4g_pay_handle_event (GstPad * pad, GstEvent * event);
87 GST_BOILERPLATE (GstRtpMP4GPay, gst_rtp_mp4g_pay, GstBaseRTPPayload,
88 GST_TYPE_BASE_RTP_PAYLOAD)
90 static void gst_rtp_mp4g_pay_base_init (gpointer klass)
92 GstElementClass *element_class = GST_ELEMENT_CLASS (klass);
94 gst_element_class_add_static_pad_template (element_class,
95 &gst_rtp_mp4g_pay_src_template);
96 gst_element_class_add_static_pad_template (element_class,
97 &gst_rtp_mp4g_pay_sink_template);
99 gst_element_class_set_details_simple (element_class, "RTP MPEG4 ES payloader",
100 "Codec/Payloader/Network/RTP",
101 "Payload MPEG4 elementary streams as RTP packets (RFC 3640)",
102 "Wim Taymans <wim.taymans@gmail.com>");
106 gst_rtp_mp4g_pay_class_init (GstRtpMP4GPayClass * klass)
108 GObjectClass *gobject_class;
109 GstElementClass *gstelement_class;
110 GstBaseRTPPayloadClass *gstbasertppayload_class;
112 gobject_class = (GObjectClass *) klass;
113 gstelement_class = (GstElementClass *) klass;
114 gstbasertppayload_class = (GstBaseRTPPayloadClass *) klass;
116 gobject_class->finalize = gst_rtp_mp4g_pay_finalize;
118 gstelement_class->change_state = gst_rtp_mp4g_pay_change_state;
120 gstbasertppayload_class->set_caps = gst_rtp_mp4g_pay_setcaps;
121 gstbasertppayload_class->handle_buffer = gst_rtp_mp4g_pay_handle_buffer;
122 gstbasertppayload_class->handle_event = gst_rtp_mp4g_pay_handle_event;
124 GST_DEBUG_CATEGORY_INIT (rtpmp4gpay_debug, "rtpmp4gpay", 0,
125 "MP4-generic RTP Payloader");
129 gst_rtp_mp4g_pay_init (GstRtpMP4GPay * rtpmp4gpay, GstRtpMP4GPayClass * klass)
131 rtpmp4gpay->adapter = gst_adapter_new ();
135 gst_rtp_mp4g_pay_reset (GstRtpMP4GPay * rtpmp4gpay)
137 GST_DEBUG_OBJECT (rtpmp4gpay, "reset");
139 gst_adapter_clear (rtpmp4gpay->adapter);
140 rtpmp4gpay->offset = 0;
144 gst_rtp_mp4g_pay_cleanup (GstRtpMP4GPay * rtpmp4gpay)
146 gst_rtp_mp4g_pay_reset (rtpmp4gpay);
148 g_free (rtpmp4gpay->params);
149 rtpmp4gpay->params = NULL;
151 if (rtpmp4gpay->config)
152 gst_buffer_unref (rtpmp4gpay->config);
153 rtpmp4gpay->config = NULL;
155 g_free (rtpmp4gpay->profile);
156 rtpmp4gpay->profile = NULL;
158 rtpmp4gpay->streamtype = NULL;
159 rtpmp4gpay->mode = NULL;
161 rtpmp4gpay->frame_len = 0;
165 gst_rtp_mp4g_pay_finalize (GObject * object)
167 GstRtpMP4GPay *rtpmp4gpay;
169 rtpmp4gpay = GST_RTP_MP4G_PAY (object);
171 gst_rtp_mp4g_pay_cleanup (rtpmp4gpay);
173 g_object_unref (rtpmp4gpay->adapter);
174 rtpmp4gpay->adapter = NULL;
176 G_OBJECT_CLASS (parent_class)->finalize (object);
179 static const unsigned int sampling_table[16] = {
180 96000, 88200, 64000, 48000, 44100, 32000, 24000, 22050,
181 16000, 12000, 11025, 8000, 7350, 0, 0, 0
185 gst_rtp_mp4g_pay_parse_audio_config (GstRtpMP4GPay * rtpmp4gpay,
190 guint8 objectType = 0;
191 guint8 samplingIdx = 0;
192 guint8 channelCfg = 0;
195 data = GST_BUFFER_DATA (buffer);
196 size = GST_BUFFER_SIZE (buffer);
198 gst_bit_reader_init (&br, data, size);
200 /* any object type is fine, we need to copy it to the profile-level-id field. */
201 if (!gst_bit_reader_get_bits_uint8 (&br, &objectType, 5))
206 if (!gst_bit_reader_get_bits_uint8 (&br, &samplingIdx, 4))
208 /* only fixed values for now */
209 if (samplingIdx > 12 && samplingIdx != 15)
212 if (!gst_bit_reader_get_bits_uint8 (&br, &channelCfg, 4))
217 /* rtp rate depends on sampling rate of the audio */
218 if (samplingIdx == 15) {
221 /* index of 15 means we get the rate in the next 24 bits */
222 if (!gst_bit_reader_get_bits_uint32 (&br, &rate, 24))
225 rtpmp4gpay->rate = rate;
227 /* else use the rate from the table */
228 rtpmp4gpay->rate = sampling_table[samplingIdx];
231 rtpmp4gpay->frame_len = 1024;
233 switch (objectType) {
241 guint8 frameLenFlag = 0;
243 if (gst_bit_reader_get_bits_uint8 (&br, &frameLenFlag, 1))
245 rtpmp4gpay->frame_len = 960;
253 /* extra rtp params contain the number of channels */
254 g_free (rtpmp4gpay->params);
255 rtpmp4gpay->params = g_strdup_printf ("%d", channelCfg);
256 /* audio stream type */
257 rtpmp4gpay->streamtype = "5";
258 /* mode only high bitrate for now */
259 rtpmp4gpay->mode = "AAC-hbr";
261 g_free (rtpmp4gpay->profile);
262 rtpmp4gpay->profile = g_strdup_printf ("%d", objectType);
264 GST_DEBUG_OBJECT (rtpmp4gpay,
265 "objectType: %d, samplingIdx: %d (%d), channelCfg: %d, frame_len %d",
266 objectType, samplingIdx, rtpmp4gpay->rate, channelCfg,
267 rtpmp4gpay->frame_len);
274 GST_ELEMENT_ERROR (rtpmp4gpay, STREAM, FORMAT,
275 (NULL), ("config string too short"));
280 GST_ELEMENT_ERROR (rtpmp4gpay, STREAM, FORMAT,
281 (NULL), ("invalid object type"));
286 GST_ELEMENT_ERROR (rtpmp4gpay, STREAM, NOT_IMPLEMENTED,
287 (NULL), ("unsupported frequency index %d", samplingIdx));
292 GST_ELEMENT_ERROR (rtpmp4gpay, STREAM, NOT_IMPLEMENTED,
293 (NULL), ("unsupported number of channels %d, must < 8", channelCfg));
298 #define VOS_STARTCODE 0x000001B0
301 gst_rtp_mp4g_pay_parse_video_config (GstRtpMP4GPay * rtpmp4gpay,
308 data = GST_BUFFER_DATA (buffer);
309 size = GST_BUFFER_SIZE (buffer);
314 code = GST_READ_UINT32_BE (data);
316 g_free (rtpmp4gpay->profile);
317 if (code == VOS_STARTCODE) {
319 rtpmp4gpay->profile = g_strdup_printf ("%d", (gint) data[4]);
321 GST_ELEMENT_WARNING (rtpmp4gpay, STREAM, FORMAT,
322 (NULL), ("profile not found in config string, assuming \'1\'"));
323 rtpmp4gpay->profile = g_strdup ("1");
327 rtpmp4gpay->rate = 90000;
328 /* video stream type */
329 rtpmp4gpay->streamtype = "4";
330 /* no params for video */
331 rtpmp4gpay->params = NULL;
333 rtpmp4gpay->mode = "generic";
335 GST_LOG_OBJECT (rtpmp4gpay, "profile %s", rtpmp4gpay->profile);
342 GST_ELEMENT_ERROR (rtpmp4gpay, STREAM, FORMAT,
343 (NULL), ("config string too short"));
349 gst_rtp_mp4g_pay_new_caps (GstRtpMP4GPay * rtpmp4gpay)
356 "streamtype", G_TYPE_STRING, rtpmp4gpay->streamtype, \
357 "profile-level-id", G_TYPE_STRING, rtpmp4gpay->profile, \
358 "mode", G_TYPE_STRING, rtpmp4gpay->mode, \
359 "config", G_TYPE_STRING, config, \
360 "sizelength", G_TYPE_STRING, "13", \
361 "indexlength", G_TYPE_STRING, "3", \
362 "indexdeltalength", G_TYPE_STRING, "3", \
365 g_value_init (&v, GST_TYPE_BUFFER);
366 gst_value_set_buffer (&v, rtpmp4gpay->config);
367 config = gst_value_serialize (&v);
370 if (rtpmp4gpay->params) {
371 res = gst_basertppayload_set_outcaps (GST_BASE_RTP_PAYLOAD (rtpmp4gpay),
372 "encoding-params", G_TYPE_STRING, rtpmp4gpay->params, MP4GCAPS);
374 res = gst_basertppayload_set_outcaps (GST_BASE_RTP_PAYLOAD (rtpmp4gpay),
386 gst_rtp_mp4g_pay_setcaps (GstBaseRTPPayload * payload, GstCaps * caps)
388 GstRtpMP4GPay *rtpmp4gpay;
389 GstStructure *structure;
390 const GValue *codec_data;
391 const gchar *media_type = NULL;
394 rtpmp4gpay = GST_RTP_MP4G_PAY (payload);
396 structure = gst_caps_get_structure (caps, 0);
398 codec_data = gst_structure_get_value (structure, "codec_data");
400 GST_LOG_OBJECT (rtpmp4gpay, "got codec_data");
401 if (G_VALUE_TYPE (codec_data) == GST_TYPE_BUFFER) {
405 buffer = gst_value_get_buffer (codec_data);
406 GST_LOG_OBJECT (rtpmp4gpay, "configuring codec_data");
408 name = gst_structure_get_name (structure);
411 if (!strcmp (name, "audio/mpeg")) {
412 res = gst_rtp_mp4g_pay_parse_audio_config (rtpmp4gpay, buffer);
413 media_type = "audio";
414 } else if (!strcmp (name, "video/mpeg")) {
415 res = gst_rtp_mp4g_pay_parse_video_config (rtpmp4gpay, buffer);
416 media_type = "video";
423 /* now we can configure the buffer */
424 if (rtpmp4gpay->config)
425 gst_buffer_unref (rtpmp4gpay->config);
427 rtpmp4gpay->config = gst_buffer_copy (buffer);
430 if (media_type == NULL)
433 gst_basertppayload_set_options (payload, media_type, TRUE, "MPEG4-GENERIC",
436 res = gst_rtp_mp4g_pay_new_caps (rtpmp4gpay);
443 GST_DEBUG_OBJECT (rtpmp4gpay, "failed to parse config");
449 gst_rtp_mp4g_pay_flush (GstRtpMP4GPay * rtpmp4gpay)
456 /* the data available in the adapter is either smaller
457 * than the MTU or bigger. In the case it is smaller, the complete
458 * adapter contents can be put in one packet. In the case the
459 * adapter has more than one MTU, we need to fragment the MPEG data
460 * over multiple packets. */
461 total = avail = gst_adapter_available (rtpmp4gpay->adapter);
464 mtu = GST_BASE_RTP_PAYLOAD_MTU (rtpmp4gpay);
472 /* this will be the total lenght of the packet */
473 packet_len = gst_rtp_buffer_calc_packet_len (avail, 0, 0);
475 /* fill one MTU or all available bytes, we need 4 spare bytes for
477 towrite = MIN (packet_len, mtu - 4);
479 /* this is the payload length */
480 payload_len = gst_rtp_buffer_calc_payload_len (towrite, 0, 0);
482 GST_DEBUG_OBJECT (rtpmp4gpay,
483 "avail %d, towrite %d, packet_len %d, payload_len %d", avail, towrite,
484 packet_len, payload_len);
486 /* create buffer to hold the payload, also make room for the 4 header bytes. */
487 outbuf = gst_rtp_buffer_new_allocate (payload_len + 4, 0, 0);
490 payload = gst_rtp_buffer_get_payload (outbuf);
492 /* +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+- .. -+-+-+-+-+-+-+-+-+-+
493 * |AU-headers-length|AU-header|AU-header| |AU-header|padding|
494 * | | (1) | (2) | | (n) | bits |
495 * +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+- .. -+-+-+-+-+-+-+-+-+-+
497 /* AU-headers-length, we only have 1 AU-header */
499 payload[1] = 0x10; /* we use 16 bits for the header */
501 /* +---------------------------------------+
503 * +---------------------------------------+
504 * | AU-Index / AU-Index-delta |
505 * +---------------------------------------+
507 * +---------------------------------------+
509 * +---------------------------------------+
511 * +---------------------------------------+
513 * +---------------------------------------+
515 * +---------------------------------------+
517 * +---------------------------------------+
519 /* The AU-header, no CTS, DTS, RAP, Stream-state
521 * AU-size is always the total size of the AU, not the fragmented size
523 payload[2] = (total & 0x1fe0) >> 5;
524 payload[3] = (total & 0x1f) << 3; /* we use 13 bits for the size, 3 bits index */
526 /* copy stuff from adapter to payload */
527 gst_adapter_copy (rtpmp4gpay->adapter, &payload[4], 0, payload_len);
528 gst_adapter_flush (rtpmp4gpay->adapter, payload_len);
530 /* marker only if the packet is complete */
531 gst_rtp_buffer_set_marker (outbuf, avail <= payload_len);
533 GST_BUFFER_TIMESTAMP (outbuf) = rtpmp4gpay->first_timestamp;
534 GST_BUFFER_DURATION (outbuf) = rtpmp4gpay->first_duration;
536 if (rtpmp4gpay->frame_len) {
537 GST_BUFFER_OFFSET (outbuf) = rtpmp4gpay->offset;
538 rtpmp4gpay->offset += rtpmp4gpay->frame_len;
541 ret = gst_basertppayload_push (GST_BASE_RTP_PAYLOAD (rtpmp4gpay), outbuf);
543 avail -= payload_len;
549 /* we expect buffers as exactly one complete AU
552 gst_rtp_mp4g_pay_handle_buffer (GstBaseRTPPayload * basepayload,
555 GstRtpMP4GPay *rtpmp4gpay;
557 rtpmp4gpay = GST_RTP_MP4G_PAY (basepayload);
559 rtpmp4gpay->first_timestamp = GST_BUFFER_TIMESTAMP (buffer);
560 rtpmp4gpay->first_duration = GST_BUFFER_DURATION (buffer);
562 /* we always encode and flush a full AU */
563 gst_adapter_push (rtpmp4gpay->adapter, buffer);
565 return gst_rtp_mp4g_pay_flush (rtpmp4gpay);
569 gst_rtp_mp4g_pay_handle_event (GstPad * pad, GstEvent * event)
571 GstRtpMP4GPay *rtpmp4gpay;
573 rtpmp4gpay = GST_RTP_MP4G_PAY (gst_pad_get_parent (pad));
575 GST_DEBUG ("Got event: %s", GST_EVENT_TYPE_NAME (event));
577 switch (GST_EVENT_TYPE (event)) {
578 case GST_EVENT_NEWSEGMENT:
580 /* This flush call makes sure that the last buffer is always pushed
581 * to the base payloader */
582 gst_rtp_mp4g_pay_flush (rtpmp4gpay);
584 case GST_EVENT_FLUSH_STOP:
585 gst_rtp_mp4g_pay_reset (rtpmp4gpay);
591 g_object_unref (rtpmp4gpay);
593 /* let parent handle event too */
597 static GstStateChangeReturn
598 gst_rtp_mp4g_pay_change_state (GstElement * element, GstStateChange transition)
600 GstStateChangeReturn ret;
601 GstRtpMP4GPay *rtpmp4gpay;
603 rtpmp4gpay = GST_RTP_MP4G_PAY (element);
605 switch (transition) {
606 case GST_STATE_CHANGE_READY_TO_PAUSED:
607 gst_rtp_mp4g_pay_cleanup (rtpmp4gpay);
613 ret = GST_ELEMENT_CLASS (parent_class)->change_state (element, transition);
615 switch (transition) {
616 case GST_STATE_CHANGE_PAUSED_TO_READY:
617 gst_rtp_mp4g_pay_cleanup (rtpmp4gpay);
627 gst_rtp_mp4g_pay_plugin_init (GstPlugin * plugin)
629 return gst_element_register (plugin, "rtpmp4gpay",
630 GST_RANK_SECONDARY, GST_TYPE_RTP_MP4G_PAY);