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);
86 GST_BOILERPLATE (GstRtpMP4GPay, gst_rtp_mp4g_pay, GstBaseRTPPayload,
87 GST_TYPE_BASE_RTP_PAYLOAD)
89 static void gst_rtp_mp4g_pay_base_init (gpointer klass)
91 GstElementClass *element_class = GST_ELEMENT_CLASS (klass);
93 gst_element_class_add_pad_template (element_class,
94 gst_static_pad_template_get (&gst_rtp_mp4g_pay_src_template));
95 gst_element_class_add_pad_template (element_class,
96 gst_static_pad_template_get (&gst_rtp_mp4g_pay_sink_template));
98 gst_element_class_set_details_simple (element_class, "RTP MPEG4 ES payloader",
99 "Codec/Payloader/Network/RTP",
100 "Payload MPEG4 elementary streams as RTP packets (RFC 3640)",
101 "Wim Taymans <wim.taymans@gmail.com>");
105 gst_rtp_mp4g_pay_class_init (GstRtpMP4GPayClass * klass)
107 GObjectClass *gobject_class;
108 GstElementClass *gstelement_class;
109 GstBaseRTPPayloadClass *gstbasertppayload_class;
111 gobject_class = (GObjectClass *) klass;
112 gstelement_class = (GstElementClass *) klass;
113 gstbasertppayload_class = (GstBaseRTPPayloadClass *) klass;
115 gobject_class->finalize = gst_rtp_mp4g_pay_finalize;
117 gstelement_class->change_state = gst_rtp_mp4g_pay_change_state;
119 gstbasertppayload_class->set_caps = gst_rtp_mp4g_pay_setcaps;
120 gstbasertppayload_class->handle_buffer = gst_rtp_mp4g_pay_handle_buffer;
122 GST_DEBUG_CATEGORY_INIT (rtpmp4gpay_debug, "rtpmp4gpay", 0,
123 "MP4-generic RTP Payloader");
127 gst_rtp_mp4g_pay_init (GstRtpMP4GPay * rtpmp4gpay, GstRtpMP4GPayClass * klass)
129 rtpmp4gpay->adapter = gst_adapter_new ();
133 gst_rtp_mp4g_pay_reset (GstRtpMP4GPay * rtpmp4gpay)
135 GST_DEBUG_OBJECT (rtpmp4gpay, "reset");
137 gst_adapter_clear (rtpmp4gpay->adapter);
139 g_free (rtpmp4gpay->params);
140 rtpmp4gpay->params = NULL;
142 if (rtpmp4gpay->config)
143 gst_buffer_unref (rtpmp4gpay->config);
144 rtpmp4gpay->config = NULL;
146 g_free (rtpmp4gpay->profile);
147 rtpmp4gpay->profile = NULL;
149 rtpmp4gpay->streamtype = NULL;
150 rtpmp4gpay->mode = NULL;
152 rtpmp4gpay->frame_len = 0;
153 rtpmp4gpay->offset = 0;
157 gst_rtp_mp4g_pay_finalize (GObject * object)
159 GstRtpMP4GPay *rtpmp4gpay;
161 rtpmp4gpay = GST_RTP_MP4G_PAY (object);
163 gst_rtp_mp4g_pay_reset (rtpmp4gpay);
165 g_object_unref (rtpmp4gpay->adapter);
166 rtpmp4gpay->adapter = NULL;
168 G_OBJECT_CLASS (parent_class)->finalize (object);
171 static const unsigned int sampling_table[16] = {
172 96000, 88200, 64000, 48000, 44100, 32000, 24000, 22050,
173 16000, 12000, 11025, 8000, 7350, 0, 0, 0
177 gst_rtp_mp4g_pay_parse_audio_config (GstRtpMP4GPay * rtpmp4gpay,
182 guint8 objectType = 0;
183 guint8 samplingIdx = 0;
184 guint8 channelCfg = 0;
187 data = GST_BUFFER_DATA (buffer);
188 size = GST_BUFFER_SIZE (buffer);
190 gst_bit_reader_init (&br, data, size);
192 /* any object type is fine, we need to copy it to the profile-level-id field. */
193 if (!gst_bit_reader_get_bits_uint8 (&br, &objectType, 5))
198 if (!gst_bit_reader_get_bits_uint8 (&br, &samplingIdx, 4))
200 /* only fixed values for now */
201 if (samplingIdx > 12 && samplingIdx != 15)
204 if (!gst_bit_reader_get_bits_uint8 (&br, &channelCfg, 4))
209 /* rtp rate depends on sampling rate of the audio */
210 if (samplingIdx == 15) {
213 /* index of 15 means we get the rate in the next 24 bits */
214 if (!gst_bit_reader_get_bits_uint32 (&br, &rate, 24))
217 rtpmp4gpay->rate = rate;
219 /* else use the rate from the table */
220 rtpmp4gpay->rate = sampling_table[samplingIdx];
223 rtpmp4gpay->frame_len = 1024;
225 switch (objectType) {
233 guint8 frameLenFlag = 0;
235 if (gst_bit_reader_get_bits_uint8 (&br, &frameLenFlag, 1))
237 rtpmp4gpay->frame_len = 960;
245 /* extra rtp params contain the number of channels */
246 g_free (rtpmp4gpay->params);
247 rtpmp4gpay->params = g_strdup_printf ("%d", channelCfg);
248 /* audio stream type */
249 rtpmp4gpay->streamtype = "5";
250 /* mode only high bitrate for now */
251 rtpmp4gpay->mode = "AAC-hbr";
253 g_free (rtpmp4gpay->profile);
254 rtpmp4gpay->profile = g_strdup_printf ("%d", objectType);
256 GST_DEBUG_OBJECT (rtpmp4gpay,
257 "objectType: %d, samplingIdx: %d (%d), channelCfg: %d, frame_len %d",
258 objectType, samplingIdx, rtpmp4gpay->rate, channelCfg,
259 rtpmp4gpay->frame_len);
266 GST_ELEMENT_ERROR (rtpmp4gpay, STREAM, FORMAT,
267 (NULL), ("config string too short"));
272 GST_ELEMENT_ERROR (rtpmp4gpay, STREAM, FORMAT,
273 (NULL), ("invalid object type"));
278 GST_ELEMENT_ERROR (rtpmp4gpay, STREAM, NOT_IMPLEMENTED,
279 (NULL), ("unsupported frequency index %d", samplingIdx));
284 GST_ELEMENT_ERROR (rtpmp4gpay, STREAM, NOT_IMPLEMENTED,
285 (NULL), ("unsupported number of channels %d, must < 8", channelCfg));
290 #define VOS_STARTCODE 0x000001B0
293 gst_rtp_mp4g_pay_parse_video_config (GstRtpMP4GPay * rtpmp4gpay,
300 data = GST_BUFFER_DATA (buffer);
301 size = GST_BUFFER_SIZE (buffer);
306 code = GST_READ_UINT32_BE (data);
308 g_free (rtpmp4gpay->profile);
309 if (code == VOS_STARTCODE) {
311 rtpmp4gpay->profile = g_strdup_printf ("%d", (gint) data[4]);
313 GST_ELEMENT_WARNING (rtpmp4gpay, STREAM, FORMAT,
314 (NULL), ("profile not found in config string, assuming \'1\'"));
315 rtpmp4gpay->profile = g_strdup ("1");
319 rtpmp4gpay->rate = 90000;
320 /* video stream type */
321 rtpmp4gpay->streamtype = "4";
322 /* no params for video */
323 rtpmp4gpay->params = NULL;
325 rtpmp4gpay->mode = "generic";
327 GST_LOG_OBJECT (rtpmp4gpay, "profile %s", rtpmp4gpay->profile);
334 GST_ELEMENT_ERROR (rtpmp4gpay, STREAM, FORMAT,
335 (NULL), ("config string too short"));
341 gst_rtp_mp4g_pay_new_caps (GstRtpMP4GPay * rtpmp4gpay)
348 "streamtype", G_TYPE_STRING, rtpmp4gpay->streamtype, \
349 "profile-level-id", G_TYPE_STRING, rtpmp4gpay->profile, \
350 "mode", G_TYPE_STRING, rtpmp4gpay->mode, \
351 "config", G_TYPE_STRING, config, \
352 "sizelength", G_TYPE_STRING, "13", \
353 "indexlength", G_TYPE_STRING, "3", \
354 "indexdeltalength", G_TYPE_STRING, "3", \
357 g_value_init (&v, GST_TYPE_BUFFER);
358 gst_value_set_buffer (&v, rtpmp4gpay->config);
359 config = gst_value_serialize (&v);
362 if (rtpmp4gpay->params) {
363 res = gst_basertppayload_set_outcaps (GST_BASE_RTP_PAYLOAD (rtpmp4gpay),
364 "encoding-params", G_TYPE_STRING, rtpmp4gpay->params, MP4GCAPS);
366 res = gst_basertppayload_set_outcaps (GST_BASE_RTP_PAYLOAD (rtpmp4gpay),
378 gst_rtp_mp4g_pay_setcaps (GstBaseRTPPayload * payload, GstCaps * caps)
380 GstRtpMP4GPay *rtpmp4gpay;
381 GstStructure *structure;
382 const GValue *codec_data;
383 const gchar *media_type = NULL;
386 rtpmp4gpay = GST_RTP_MP4G_PAY (payload);
388 structure = gst_caps_get_structure (caps, 0);
390 codec_data = gst_structure_get_value (structure, "codec_data");
392 GST_LOG_OBJECT (rtpmp4gpay, "got codec_data");
393 if (G_VALUE_TYPE (codec_data) == GST_TYPE_BUFFER) {
397 buffer = gst_value_get_buffer (codec_data);
398 GST_LOG_OBJECT (rtpmp4gpay, "configuring codec_data");
400 name = gst_structure_get_name (structure);
403 if (!strcmp (name, "audio/mpeg")) {
404 res = gst_rtp_mp4g_pay_parse_audio_config (rtpmp4gpay, buffer);
405 media_type = "audio";
406 } else if (!strcmp (name, "video/mpeg")) {
407 res = gst_rtp_mp4g_pay_parse_video_config (rtpmp4gpay, buffer);
408 media_type = "video";
415 /* now we can configure the buffer */
416 if (rtpmp4gpay->config)
417 gst_buffer_unref (rtpmp4gpay->config);
419 rtpmp4gpay->config = gst_buffer_copy (buffer);
422 if (media_type == NULL)
425 gst_basertppayload_set_options (payload, media_type, TRUE, "MPEG4-GENERIC",
428 res = gst_rtp_mp4g_pay_new_caps (rtpmp4gpay);
435 GST_DEBUG_OBJECT (rtpmp4gpay, "failed to parse config");
441 gst_rtp_mp4g_pay_flush (GstRtpMP4GPay * rtpmp4gpay)
451 /* the data available in the adapter is either smaller
452 * than the MTU or bigger. In the case it is smaller, the complete
453 * adapter contents can be put in one packet. In the case the
454 * adapter has more than one MTU, we need to fragment the MPEG data
455 * over multiple packets. */
456 total = avail = gst_adapter_available (rtpmp4gpay->adapter);
459 mtu = GST_BASE_RTP_PAYLOAD_MTU (rtpmp4gpay);
467 /* this will be the total lenght of the packet */
468 packet_len = gst_rtp_buffer_calc_packet_len (avail, 0, 0);
470 /* fill one MTU or all available bytes, we need 4 spare bytes for
472 towrite = MIN (packet_len, mtu - 4);
474 /* this is the payload length */
475 payload_len = gst_rtp_buffer_calc_payload_len (towrite, 0, 0);
477 GST_DEBUG_OBJECT (rtpmp4gpay,
478 "avail %d, towrite %d, packet_len %d, payload_len %d", avail, towrite,
479 packet_len, payload_len);
481 /* create buffer to hold the payload, also make room for the 4 header bytes. */
482 outbuf = gst_rtp_buffer_new_allocate (payload_len + 4, 0, 0);
485 payload = gst_rtp_buffer_get_payload (outbuf);
487 /* +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+- .. -+-+-+-+-+-+-+-+-+-+
488 * |AU-headers-length|AU-header|AU-header| |AU-header|padding|
489 * | | (1) | (2) | | (n) | bits |
490 * +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+- .. -+-+-+-+-+-+-+-+-+-+
492 /* AU-headers-length, we only have 1 AU-header */
494 payload[1] = 0x10; /* we use 16 bits for the header */
496 /* +---------------------------------------+
498 * +---------------------------------------+
499 * | AU-Index / AU-Index-delta |
500 * +---------------------------------------+
502 * +---------------------------------------+
504 * +---------------------------------------+
506 * +---------------------------------------+
508 * +---------------------------------------+
510 * +---------------------------------------+
512 * +---------------------------------------+
514 /* The AU-header, no CTS, DTS, RAP, Stream-state
516 * AU-size is always the total size of the AU, not the fragmented size
518 payload[2] = (total & 0x1fe0) >> 5;
519 payload[3] = (total & 0x1f) << 3; /* we use 13 bits for the size, 3 bits index */
521 /* copy stuff from adapter to payload */
522 gst_adapter_copy (rtpmp4gpay->adapter, &payload[4], 0, payload_len);
523 gst_adapter_flush (rtpmp4gpay->adapter, payload_len);
525 /* marker only if the packet is complete */
526 gst_rtp_buffer_set_marker (outbuf, avail <= payload_len);
528 GST_BUFFER_TIMESTAMP (outbuf) = rtpmp4gpay->first_timestamp;
529 GST_BUFFER_DURATION (outbuf) = rtpmp4gpay->first_duration;
531 if (rtpmp4gpay->frame_len) {
532 GST_BUFFER_OFFSET (outbuf) = rtpmp4gpay->offset;
533 rtpmp4gpay->offset += rtpmp4gpay->frame_len;
536 ret = gst_basertppayload_push (GST_BASE_RTP_PAYLOAD (rtpmp4gpay), outbuf);
538 avail -= payload_len;
545 /* we expect buffers as exactly one complete AU
548 gst_rtp_mp4g_pay_handle_buffer (GstBaseRTPPayload * basepayload,
551 GstRtpMP4GPay *rtpmp4gpay;
553 rtpmp4gpay = GST_RTP_MP4G_PAY (basepayload);
555 rtpmp4gpay->first_timestamp = GST_BUFFER_TIMESTAMP (buffer);
556 rtpmp4gpay->first_duration = GST_BUFFER_DURATION (buffer);
558 /* we always encode and flush a full AU */
559 gst_adapter_push (rtpmp4gpay->adapter, buffer);
561 return gst_rtp_mp4g_pay_flush (rtpmp4gpay);
564 static GstStateChangeReturn
565 gst_rtp_mp4g_pay_change_state (GstElement * element, GstStateChange transition)
567 GstStateChangeReturn ret;
568 GstRtpMP4GPay *rtpmp4gpay;
570 rtpmp4gpay = GST_RTP_MP4G_PAY (element);
572 switch (transition) {
573 case GST_STATE_CHANGE_READY_TO_PAUSED:
574 gst_rtp_mp4g_pay_reset (rtpmp4gpay);
580 ret = GST_ELEMENT_CLASS (parent_class)->change_state (element, transition);
586 gst_rtp_mp4g_pay_plugin_init (GstPlugin * plugin)
588 return gst_element_register (plugin, "rtpmp4gpay",
589 GST_RANK_SECONDARY, GST_TYPE_RTP_MP4G_PAY);