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., 51 Franklin St, Fifth Floor,
17 * Boston, MA 02110-1301, 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 (GstRTPBasePayload * payload,
83 static GstFlowReturn gst_rtp_mp4g_pay_handle_buffer (GstRTPBasePayload *
84 payload, GstBuffer * buffer);
85 static gboolean gst_rtp_mp4g_pay_sink_event (GstRTPBasePayload * payload,
88 #define gst_rtp_mp4g_pay_parent_class parent_class
89 G_DEFINE_TYPE (GstRtpMP4GPay, gst_rtp_mp4g_pay, GST_TYPE_RTP_BASE_PAYLOAD)
91 static void gst_rtp_mp4g_pay_class_init (GstRtpMP4GPayClass * klass)
93 GObjectClass *gobject_class;
94 GstElementClass *gstelement_class;
95 GstRTPBasePayloadClass *gstrtpbasepayload_class;
97 gobject_class = (GObjectClass *) klass;
98 gstelement_class = (GstElementClass *) klass;
99 gstrtpbasepayload_class = (GstRTPBasePayloadClass *) klass;
101 gobject_class->finalize = gst_rtp_mp4g_pay_finalize;
103 gstelement_class->change_state = gst_rtp_mp4g_pay_change_state;
105 gstrtpbasepayload_class->set_caps = gst_rtp_mp4g_pay_setcaps;
106 gstrtpbasepayload_class->handle_buffer = gst_rtp_mp4g_pay_handle_buffer;
107 gstrtpbasepayload_class->sink_event = gst_rtp_mp4g_pay_sink_event;
109 gst_element_class_add_pad_template (gstelement_class,
110 gst_static_pad_template_get (&gst_rtp_mp4g_pay_src_template));
111 gst_element_class_add_pad_template (gstelement_class,
112 gst_static_pad_template_get (&gst_rtp_mp4g_pay_sink_template));
114 gst_element_class_set_static_metadata (gstelement_class,
115 "RTP MPEG4 ES payloader",
116 "Codec/Payloader/Network/RTP",
117 "Payload MPEG4 elementary streams as RTP packets (RFC 3640)",
118 "Wim Taymans <wim.taymans@gmail.com>");
120 GST_DEBUG_CATEGORY_INIT (rtpmp4gpay_debug, "rtpmp4gpay", 0,
121 "MP4-generic RTP Payloader");
125 gst_rtp_mp4g_pay_init (GstRtpMP4GPay * rtpmp4gpay)
127 rtpmp4gpay->adapter = gst_adapter_new ();
131 gst_rtp_mp4g_pay_reset (GstRtpMP4GPay * rtpmp4gpay)
133 GST_DEBUG_OBJECT (rtpmp4gpay, "reset");
135 gst_adapter_clear (rtpmp4gpay->adapter);
136 rtpmp4gpay->offset = 0;
140 gst_rtp_mp4g_pay_cleanup (GstRtpMP4GPay * rtpmp4gpay)
142 gst_rtp_mp4g_pay_reset (rtpmp4gpay);
144 g_free (rtpmp4gpay->params);
145 rtpmp4gpay->params = NULL;
147 if (rtpmp4gpay->config)
148 gst_buffer_unref (rtpmp4gpay->config);
149 rtpmp4gpay->config = NULL;
151 g_free (rtpmp4gpay->profile);
152 rtpmp4gpay->profile = NULL;
154 rtpmp4gpay->streamtype = NULL;
155 rtpmp4gpay->mode = NULL;
157 rtpmp4gpay->frame_len = 0;
161 gst_rtp_mp4g_pay_finalize (GObject * object)
163 GstRtpMP4GPay *rtpmp4gpay;
165 rtpmp4gpay = GST_RTP_MP4G_PAY (object);
167 gst_rtp_mp4g_pay_cleanup (rtpmp4gpay);
169 g_object_unref (rtpmp4gpay->adapter);
170 rtpmp4gpay->adapter = NULL;
172 G_OBJECT_CLASS (parent_class)->finalize (object);
175 static const unsigned int sampling_table[16] = {
176 96000, 88200, 64000, 48000, 44100, 32000, 24000, 22050,
177 16000, 12000, 11025, 8000, 7350, 0, 0, 0
181 gst_rtp_mp4g_pay_parse_audio_config (GstRtpMP4GPay * rtpmp4gpay,
185 guint8 objectType = 0;
186 guint8 samplingIdx = 0;
187 guint8 channelCfg = 0;
190 gst_buffer_map (buffer, &map, GST_MAP_READ);
192 gst_bit_reader_init (&br, map.data, map.size);
194 /* any object type is fine, we need to copy it to the profile-level-id field. */
195 if (!gst_bit_reader_get_bits_uint8 (&br, &objectType, 5))
200 if (!gst_bit_reader_get_bits_uint8 (&br, &samplingIdx, 4))
202 /* only fixed values for now */
203 if (samplingIdx > 12 && samplingIdx != 15)
206 if (!gst_bit_reader_get_bits_uint8 (&br, &channelCfg, 4))
211 /* rtp rate depends on sampling rate of the audio */
212 if (samplingIdx == 15) {
215 /* index of 15 means we get the rate in the next 24 bits */
216 if (!gst_bit_reader_get_bits_uint32 (&br, &rate, 24))
219 rtpmp4gpay->rate = rate;
221 /* else use the rate from the table */
222 rtpmp4gpay->rate = sampling_table[samplingIdx];
225 rtpmp4gpay->frame_len = 1024;
227 switch (objectType) {
235 guint8 frameLenFlag = 0;
237 if (gst_bit_reader_get_bits_uint8 (&br, &frameLenFlag, 1))
239 rtpmp4gpay->frame_len = 960;
247 /* extra rtp params contain the number of channels */
248 g_free (rtpmp4gpay->params);
249 rtpmp4gpay->params = g_strdup_printf ("%d", channelCfg);
250 /* audio stream type */
251 rtpmp4gpay->streamtype = "5";
252 /* mode only high bitrate for now */
253 rtpmp4gpay->mode = "AAC-hbr";
255 g_free (rtpmp4gpay->profile);
256 rtpmp4gpay->profile = g_strdup_printf ("%d", objectType);
258 GST_DEBUG_OBJECT (rtpmp4gpay,
259 "objectType: %d, samplingIdx: %d (%d), channelCfg: %d, frame_len %d",
260 objectType, samplingIdx, rtpmp4gpay->rate, channelCfg,
261 rtpmp4gpay->frame_len);
263 gst_buffer_unmap (buffer, &map);
269 GST_ELEMENT_ERROR (rtpmp4gpay, STREAM, FORMAT,
270 (NULL), ("config string too short"));
271 gst_buffer_unmap (buffer, &map);
276 GST_ELEMENT_ERROR (rtpmp4gpay, STREAM, FORMAT,
277 (NULL), ("invalid object type"));
278 gst_buffer_unmap (buffer, &map);
283 GST_ELEMENT_ERROR (rtpmp4gpay, STREAM, NOT_IMPLEMENTED,
284 (NULL), ("unsupported frequency index %d", samplingIdx));
285 gst_buffer_unmap (buffer, &map);
290 GST_ELEMENT_ERROR (rtpmp4gpay, STREAM, NOT_IMPLEMENTED,
291 (NULL), ("unsupported number of channels %d, must < 8", channelCfg));
292 gst_buffer_unmap (buffer, &map);
297 #define VOS_STARTCODE 0x000001B0
300 gst_rtp_mp4g_pay_parse_video_config (GstRtpMP4GPay * rtpmp4gpay,
306 gst_buffer_map (buffer, &map, GST_MAP_READ);
311 code = GST_READ_UINT32_BE (map.data);
313 g_free (rtpmp4gpay->profile);
314 if (code == VOS_STARTCODE) {
316 rtpmp4gpay->profile = g_strdup_printf ("%d", (gint) map.data[4]);
318 GST_ELEMENT_WARNING (rtpmp4gpay, STREAM, FORMAT,
319 (NULL), ("profile not found in config string, assuming \'1\'"));
320 rtpmp4gpay->profile = g_strdup ("1");
324 rtpmp4gpay->rate = 90000;
325 /* video stream type */
326 rtpmp4gpay->streamtype = "4";
327 /* no params for video */
328 rtpmp4gpay->params = NULL;
330 rtpmp4gpay->mode = "generic";
332 GST_LOG_OBJECT (rtpmp4gpay, "profile %s", rtpmp4gpay->profile);
334 gst_buffer_unmap (buffer, &map);
341 GST_ELEMENT_ERROR (rtpmp4gpay, STREAM, FORMAT,
342 (NULL), ("config string too short"));
343 gst_buffer_unmap (buffer, &map);
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_rtp_base_payload_set_outcaps (GST_RTP_BASE_PAYLOAD (rtpmp4gpay),
372 "encoding-params", G_TYPE_STRING, rtpmp4gpay->params, MP4GCAPS);
374 res = gst_rtp_base_payload_set_outcaps (GST_RTP_BASE_PAYLOAD (rtpmp4gpay),
386 gst_rtp_mp4g_pay_setcaps (GstRTPBasePayload * 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_rtp_base_payload_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_RTP_BASE_PAYLOAD_MTU (rtpmp4gpay);
471 GstRTPBuffer rtp = { NULL };
474 /* this will be the total lenght of the packet */
475 packet_len = gst_rtp_buffer_calc_packet_len (avail, 0, 0);
477 /* fill one MTU or all available bytes, we need 4 spare bytes for
479 towrite = MIN (packet_len, mtu - 4);
481 /* this is the payload length */
482 payload_len = gst_rtp_buffer_calc_payload_len (towrite, 0, 0);
484 GST_DEBUG_OBJECT (rtpmp4gpay,
485 "avail %d, towrite %d, packet_len %d, payload_len %d", avail, towrite,
486 packet_len, payload_len);
488 /* create buffer to hold the payload, also make room for the 4 header bytes. */
489 outbuf = gst_rtp_buffer_new_allocate (4, 0, 0);
491 gst_rtp_buffer_map (outbuf, GST_MAP_WRITE, &rtp);
494 payload = gst_rtp_buffer_get_payload (&rtp);
496 /* +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+- .. -+-+-+-+-+-+-+-+-+-+
497 * |AU-headers-length|AU-header|AU-header| |AU-header|padding|
498 * | | (1) | (2) | | (n) | bits |
499 * +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+- .. -+-+-+-+-+-+-+-+-+-+
501 /* AU-headers-length, we only have 1 AU-header */
503 payload[1] = 0x10; /* we use 16 bits for the header */
505 /* +---------------------------------------+
507 * +---------------------------------------+
508 * | AU-Index / AU-Index-delta |
509 * +---------------------------------------+
511 * +---------------------------------------+
513 * +---------------------------------------+
515 * +---------------------------------------+
517 * +---------------------------------------+
519 * +---------------------------------------+
521 * +---------------------------------------+
523 /* The AU-header, no CTS, DTS, RAP, Stream-state
525 * AU-size is always the total size of the AU, not the fragmented size
527 payload[2] = (total & 0x1fe0) >> 5;
528 payload[3] = (total & 0x1f) << 3; /* we use 13 bits for the size, 3 bits index */
530 /* marker only if the packet is complete */
531 gst_rtp_buffer_set_marker (&rtp, avail <= payload_len);
533 gst_rtp_buffer_unmap (&rtp);
535 paybuf = gst_adapter_take_buffer_fast (rtpmp4gpay->adapter, payload_len);
536 outbuf = gst_buffer_append (outbuf, paybuf);
538 GST_BUFFER_TIMESTAMP (outbuf) = rtpmp4gpay->first_timestamp;
539 GST_BUFFER_DURATION (outbuf) = rtpmp4gpay->first_duration;
541 if (rtpmp4gpay->frame_len) {
542 GST_BUFFER_OFFSET (outbuf) = rtpmp4gpay->offset;
543 rtpmp4gpay->offset += rtpmp4gpay->frame_len;
546 ret = gst_rtp_base_payload_push (GST_RTP_BASE_PAYLOAD (rtpmp4gpay), outbuf);
548 avail -= payload_len;
554 /* we expect buffers as exactly one complete AU
557 gst_rtp_mp4g_pay_handle_buffer (GstRTPBasePayload * basepayload,
560 GstRtpMP4GPay *rtpmp4gpay;
562 rtpmp4gpay = GST_RTP_MP4G_PAY (basepayload);
564 rtpmp4gpay->first_timestamp = GST_BUFFER_TIMESTAMP (buffer);
565 rtpmp4gpay->first_duration = GST_BUFFER_DURATION (buffer);
567 /* we always encode and flush a full AU */
568 gst_adapter_push (rtpmp4gpay->adapter, buffer);
570 return gst_rtp_mp4g_pay_flush (rtpmp4gpay);
574 gst_rtp_mp4g_pay_sink_event (GstRTPBasePayload * payload, GstEvent * event)
576 GstRtpMP4GPay *rtpmp4gpay;
578 rtpmp4gpay = GST_RTP_MP4G_PAY (payload);
580 GST_DEBUG ("Got event: %s", GST_EVENT_TYPE_NAME (event));
582 switch (GST_EVENT_TYPE (event)) {
583 case GST_EVENT_SEGMENT:
585 /* This flush call makes sure that the last buffer is always pushed
586 * to the base payloader */
587 gst_rtp_mp4g_pay_flush (rtpmp4gpay);
589 case GST_EVENT_FLUSH_STOP:
590 gst_rtp_mp4g_pay_reset (rtpmp4gpay);
596 /* let parent handle event too */
597 return GST_RTP_BASE_PAYLOAD_CLASS (parent_class)->sink_event (payload, event);
600 static GstStateChangeReturn
601 gst_rtp_mp4g_pay_change_state (GstElement * element, GstStateChange transition)
603 GstStateChangeReturn ret;
604 GstRtpMP4GPay *rtpmp4gpay;
606 rtpmp4gpay = GST_RTP_MP4G_PAY (element);
608 switch (transition) {
609 case GST_STATE_CHANGE_READY_TO_PAUSED:
610 gst_rtp_mp4g_pay_cleanup (rtpmp4gpay);
616 ret = GST_ELEMENT_CLASS (parent_class)->change_state (element, transition);
618 switch (transition) {
619 case GST_STATE_CHANGE_PAUSED_TO_READY:
620 gst_rtp_mp4g_pay_cleanup (rtpmp4gpay);
630 gst_rtp_mp4g_pay_plugin_init (GstPlugin * plugin)
632 return gst_element_register (plugin, "rtpmp4gpay",
633 GST_RANK_SECONDARY, GST_TYPE_RTP_MP4G_PAY);