2 * Copyright (C) <2006> Wim Taymans <wim@fluendo.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 "gstrtpmp4gpay.h"
30 GST_DEBUG_CATEGORY_STATIC (rtpmp4gpay_debug);
31 #define GST_CAT_DEFAULT (rtpmp4gpay_debug)
33 /* elementfactory information */
34 static const GstElementDetails gst_rtp_mp4gpay_details =
35 GST_ELEMENT_DETAILS ("RTP packet payloader",
36 "Codec/Payloader/Network",
37 "Payload MPEG4 elementary streams as RTP packets (RFC 3640)",
38 "Wim Taymans <wim@fluendo.com>");
40 static GstStaticPadTemplate gst_rtp_mp4g_pay_sink_template =
41 GST_STATIC_PAD_TEMPLATE ("sink",
44 GST_STATIC_CAPS ("video/mpeg,"
45 "mpegversion=(int) 4,"
46 "systemstream=(boolean)false;" "audio/mpeg," "mpegversion=(int) 4")
49 static GstStaticPadTemplate gst_rtp_mp4g_pay_src_template =
50 GST_STATIC_PAD_TEMPLATE ("src",
53 GST_STATIC_CAPS ("application/x-rtp, "
54 "media = (string) { \"video\", \"audio\", \"application\" }, "
55 "payload = (int) " GST_RTP_PAYLOAD_DYNAMIC_STRING ", "
56 "clock-rate = (int) [1, MAX ], "
57 "encoding-name = (string) \"MPEG4-GENERIC\", "
58 /* required string params */
59 "streamtype = (string) { \"4\", \"5\" }, " /* 4 = video, 5 = audio */
60 /* "profile-level-id = (string) [1,MAX], " */
61 /* "config = (string) [1,MAX]" */
62 "mode = (string) { \"generic\", \"celp-cbr\", \"celp-vbr\", \"aac-lbr\", \"aac-hbr\" } "
63 /* Optional general parameters */
64 /* "objecttype = (string) [1,MAX], " */
65 /* "constantsize = (string) [1,MAX], " *//* constant size of each AU */
66 /* "constantduration = (string) [1,MAX], " *//* constant duration of each AU */
67 /* "maxdisplacement = (string) [1,MAX], " */
68 /* "de-interleavebuffersize = (string) [1,MAX], " */
69 /* Optional configuration parameters */
70 /* "sizelength = (string) [1, 16], " *//* max 16 bits, should be enough... */
71 /* "indexlength = (string) [1, 8], " */
72 /* "indexdeltalength = (string) [1, 8], " */
73 /* "ctsdeltalength = (string) [1, 64], " */
74 /* "dtsdeltalength = (string) [1, 64], " */
75 /* "randomaccessindication = (string) {0, 1}, " */
76 /* "streamstateindication = (string) [0, 64], " */
77 /* "auxiliarydatasizelength = (string) [0, 64]" */ )
86 static void gst_rtp_mp4g_pay_class_init (GstRtpMP4GPayClass * klass);
87 static void gst_rtp_mp4g_pay_base_init (GstRtpMP4GPayClass * klass);
88 static void gst_rtp_mp4g_pay_init (GstRtpMP4GPay * rtpmp4gpay);
89 static void gst_rtp_mp4g_pay_finalize (GObject * object);
91 static void gst_rtp_mp4g_pay_set_property (GObject * object, guint prop_id,
92 const GValue * value, GParamSpec * pspec);
93 static void gst_rtp_mp4g_pay_get_property (GObject * object, guint prop_id,
94 GValue * value, GParamSpec * pspec);
96 static gboolean gst_rtp_mp4g_pay_setcaps (GstBaseRTPPayload * payload,
98 static GstFlowReturn gst_rtp_mp4g_pay_handle_buffer (GstBaseRTPPayload *
99 payload, GstBuffer * buffer);
101 static GstBaseRTPPayloadClass *parent_class = NULL;
104 gst_rtp_mp4g_pay_get_type (void)
106 static GType rtpmp4gpay_type = 0;
108 if (!rtpmp4gpay_type) {
109 static const GTypeInfo rtpmp4gpay_info = {
110 sizeof (GstRtpMP4GPayClass),
111 (GBaseInitFunc) gst_rtp_mp4g_pay_base_init,
113 (GClassInitFunc) gst_rtp_mp4g_pay_class_init,
116 sizeof (GstRtpMP4GPay),
118 (GInstanceInitFunc) gst_rtp_mp4g_pay_init,
122 g_type_register_static (GST_TYPE_BASE_RTP_PAYLOAD, "GstRtpMP4GPay",
123 &rtpmp4gpay_info, 0);
125 return rtpmp4gpay_type;
129 gst_rtp_mp4g_pay_base_init (GstRtpMP4GPayClass * klass)
131 GstElementClass *element_class = GST_ELEMENT_CLASS (klass);
133 gst_element_class_add_pad_template (element_class,
134 gst_static_pad_template_get (&gst_rtp_mp4g_pay_src_template));
135 gst_element_class_add_pad_template (element_class,
136 gst_static_pad_template_get (&gst_rtp_mp4g_pay_sink_template));
138 gst_element_class_set_details (element_class, &gst_rtp_mp4gpay_details);
142 gst_rtp_mp4g_pay_class_init (GstRtpMP4GPayClass * klass)
144 GObjectClass *gobject_class;
145 GstElementClass *gstelement_class;
146 GstBaseRTPPayloadClass *gstbasertppayload_class;
148 gobject_class = (GObjectClass *) klass;
149 gstelement_class = (GstElementClass *) klass;
150 gstbasertppayload_class = (GstBaseRTPPayloadClass *) klass;
152 parent_class = g_type_class_peek_parent (klass);
154 gobject_class->set_property = gst_rtp_mp4g_pay_set_property;
155 gobject_class->get_property = gst_rtp_mp4g_pay_get_property;
157 gobject_class->finalize = gst_rtp_mp4g_pay_finalize;
159 gstbasertppayload_class->set_caps = gst_rtp_mp4g_pay_setcaps;
160 gstbasertppayload_class->handle_buffer = gst_rtp_mp4g_pay_handle_buffer;
162 GST_DEBUG_CATEGORY_INIT (rtpmp4gpay_debug, "rtpmp4gpay", 0,
163 "MP4-generic RTP Payloader");
167 gst_rtp_mp4g_pay_init (GstRtpMP4GPay * rtpmp4gpay)
169 rtpmp4gpay->adapter = gst_adapter_new ();
170 rtpmp4gpay->rate = 90000;
171 rtpmp4gpay->profile = g_strdup ("1");
172 rtpmp4gpay->mode = "";
176 gst_rtp_mp4g_pay_finalize (GObject * object)
178 GstRtpMP4GPay *rtpmp4gpay;
180 rtpmp4gpay = GST_RTP_MP4G_PAY (object);
182 g_object_unref (rtpmp4gpay->adapter);
183 rtpmp4gpay->adapter = NULL;
184 g_free (rtpmp4gpay->params);
185 rtpmp4gpay->params = NULL;
187 G_OBJECT_CLASS (parent_class)->finalize (object);
190 static unsigned sampling_table[16] = {
191 96000, 88200, 64000, 48000, 44100, 32000, 24000, 22050,
192 16000, 12000, 11025, 8000, 7350, 0, 0, 0
196 gst_rtp_mp4g_pay_parse_audio_config (GstRtpMP4GPay * rtpmp4gpay,
205 data = GST_BUFFER_DATA (buffer);
206 size = GST_BUFFER_SIZE (buffer);
211 /* any object type is fine, we need to copy it to the profile-level-id field. */
212 objectType = (data[0] & 0xf8) >> 3;
216 samplingIdx = ((data[0] & 0x07) << 1) | ((data[1] & 0x80) >> 7);
217 /* only fixed values for now */
218 if (samplingIdx > 12 && samplingIdx != 15)
221 channelCfg = ((data[1] & 0x78) >> 3);
225 /* rtp rate depends on sampling rate of the audio */
226 if (samplingIdx == 15) {
230 /* index of 15 means we get the rate in the next 24 bits */
231 rtpmp4gpay->rate = ((data[1] & 0x7f) << 17) |
232 ((data[2]) << 9) | ((data[3]) << 1) | ((data[4] & 0x80) >> 7);
234 /* else use the rate from the table */
235 rtpmp4gpay->rate = sampling_table[samplingIdx];
237 /* extra rtp params contain the number of channels */
238 g_free (rtpmp4gpay->params);
239 rtpmp4gpay->params = g_strdup_printf ("%d", channelCfg);
240 /* audio stream type */
241 rtpmp4gpay->streamtype = "5";
242 /* mode only high bitrate for now */
243 rtpmp4gpay->mode = "AAC-hbr";
245 g_free (rtpmp4gpay->profile);
246 rtpmp4gpay->profile = g_strdup_printf ("%d", objectType);
248 GST_DEBUG_OBJECT (rtpmp4gpay,
249 "objectType: %d, samplingIdx: %d (%d), channelCfg: %d", objectType,
250 samplingIdx, rtpmp4gpay->rate, channelCfg);
257 GST_ELEMENT_ERROR (rtpmp4gpay, STREAM, FORMAT,
258 (NULL), ("config string too short, expected 2 bytes, got %d", size));
263 GST_ELEMENT_ERROR (rtpmp4gpay, STREAM, FORMAT,
264 (NULL), ("invalid object type 0"));
269 GST_ELEMENT_ERROR (rtpmp4gpay, STREAM, NOT_IMPLEMENTED,
270 (NULL), ("unsupported frequency index %d", samplingIdx));
275 GST_ELEMENT_ERROR (rtpmp4gpay, STREAM, NOT_IMPLEMENTED,
276 (NULL), ("unsupported number of channels %d, must < 8", channelCfg));
281 #define VOS_STARTCODE 0x000001B0
284 gst_rtp_mp4g_pay_parse_video_config (GstRtpMP4GPay * rtpmp4gpay,
291 data = GST_BUFFER_DATA (buffer);
292 size = GST_BUFFER_SIZE (buffer);
297 code = GST_READ_UINT32_BE (data);
299 g_free (rtpmp4gpay->profile);
300 if (code == VOS_STARTCODE) {
302 rtpmp4gpay->profile = g_strdup_printf ("%d", (gint) data[4]);
304 GST_ELEMENT_WARNING (rtpmp4gpay, STREAM, FORMAT,
305 (NULL), ("profile not found in config string, assuming \'1\'"));
306 rtpmp4gpay->profile = g_strdup ("1");
310 rtpmp4gpay->rate = 90000;
311 /* video stream type */
312 rtpmp4gpay->streamtype = "4";
313 /* no params for video */
314 rtpmp4gpay->params = NULL;
316 rtpmp4gpay->mode = "generic";
318 GST_LOG_OBJECT (rtpmp4gpay, "profile %s", rtpmp4gpay->profile);
325 GST_ELEMENT_ERROR (rtpmp4gpay, STREAM, FORMAT,
326 (NULL), ("config string too short"));
332 gst_rtp_mp4g_pay_new_caps (GstRtpMP4GPay * rtpmp4gpay)
338 "streamtype", G_TYPE_STRING, rtpmp4gpay->streamtype, \
339 "profile-level-id", G_TYPE_STRING, rtpmp4gpay->profile, \
340 "mode", G_TYPE_STRING, rtpmp4gpay->mode, \
341 "config", G_TYPE_STRING, config, \
342 "sizelength", G_TYPE_STRING, "13", \
343 "indexlength", G_TYPE_STRING, "3", \
344 "indexdeltalength", G_TYPE_STRING, "3", \
347 g_value_init (&v, GST_TYPE_BUFFER);
348 gst_value_set_buffer (&v, rtpmp4gpay->config);
349 config = gst_value_serialize (&v);
352 if (rtpmp4gpay->params) {
353 gst_basertppayload_set_outcaps (GST_BASE_RTP_PAYLOAD (rtpmp4gpay),
354 "encoding-params", G_TYPE_STRING, rtpmp4gpay->params, MP4GCAPS);
356 gst_basertppayload_set_outcaps (GST_BASE_RTP_PAYLOAD (rtpmp4gpay),
367 gst_rtp_mp4g_pay_setcaps (GstBaseRTPPayload * payload, GstCaps * caps)
369 GstRtpMP4GPay *rtpmp4gpay;
370 GstStructure *structure;
371 const GValue *codec_data;
372 gchar *media_type = NULL;
374 rtpmp4gpay = GST_RTP_MP4G_PAY (payload);
376 structure = gst_caps_get_structure (caps, 0);
378 codec_data = gst_structure_get_value (structure, "codec_data");
380 GST_LOG_OBJECT (rtpmp4gpay, "got codec_data");
381 if (G_VALUE_TYPE (codec_data) == GST_TYPE_BUFFER) {
386 buffer = gst_value_get_buffer (codec_data);
387 GST_LOG_OBJECT (rtpmp4gpay, "configuring codec_data");
389 name = gst_structure_get_name (structure);
392 if (!strcmp (name, "audio/mpeg")) {
393 res = gst_rtp_mp4g_pay_parse_audio_config (rtpmp4gpay, buffer);
394 media_type = "audio";
395 } else if (!strcmp (name, "video/mpeg")) {
396 res = gst_rtp_mp4g_pay_parse_video_config (rtpmp4gpay, buffer);
397 media_type = "video";
404 /* now we can configure the buffer */
405 if (rtpmp4gpay->config)
406 gst_buffer_unref (rtpmp4gpay->config);
408 rtpmp4gpay->config = gst_buffer_copy (buffer);
411 if (media_type == NULL)
414 gst_basertppayload_set_options (payload, media_type, TRUE, "mpeg4-generic",
417 gst_rtp_mp4g_pay_new_caps (rtpmp4gpay);
424 GST_DEBUG_OBJECT (rtpmp4gpay, "failed to parse config");
430 gst_rtp_mp4g_pay_flush (GstRtpMP4GPay * rtpmp4gpay)
440 /* the data available in the adapter is either smaller
441 * than the MTU or bigger. In the case it is smaller, the complete
442 * adapter contents can be put in one packet. In the case the
443 * adapter has more than one MTU, we need to fragment the MPEG data
444 * over multiple packets. */
445 total = avail = gst_adapter_available (rtpmp4gpay->adapter);
448 mtu = GST_BASE_RTP_PAYLOAD_MTU (rtpmp4gpay);
457 /* this will be the total lenght of the packet */
458 packet_len = gst_rtp_buffer_calc_packet_len (avail, 0, 0);
460 /* fill one MTU or all available bytes, we need 4 spare bytes for
462 towrite = MIN (packet_len, mtu - 4);
464 /* this is the payload length */
465 payload_len = gst_rtp_buffer_calc_payload_len (towrite, 0, 0);
467 GST_DEBUG_OBJECT (rtpmp4gpay,
468 "avail %d, towrite %d, packet_len %d, payload_len %d", avail, towrite,
469 packet_len, payload_len);
471 /* create buffer to hold the payload, also make room for the 4 header bytes. */
472 outbuf = gst_rtp_buffer_new_allocate (payload_len + 4, 0, 0);
475 payload = gst_rtp_buffer_get_payload (outbuf);
477 /* +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+- .. -+-+-+-+-+-+-+-+-+-+
478 * |AU-headers-length|AU-header|AU-header| |AU-header|padding|
479 * | | (1) | (2) | | (n) | bits |
480 * +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+- .. -+-+-+-+-+-+-+-+-+-+
482 /* AU-headers-length, we only have 1 AU-header */
484 payload[1] = 0x10; /* we use 16 bits for the header */
486 /* +---------------------------------------+
488 * +---------------------------------------+
489 * | AU-Index / AU-Index-delta |
490 * +---------------------------------------+
492 * +---------------------------------------+
494 * +---------------------------------------+
496 * +---------------------------------------+
498 * +---------------------------------------+
500 * +---------------------------------------+
502 * +---------------------------------------+
504 /* The AU-header, no CTS, DTS, RAP, Stream-state
506 * AU-size is always the total size of the AU, not the fragmented size
508 payload[2] = (total & 0x1fe0) >> 5;
509 payload[3] = (total & 0x1f) << 3; /* we use 13 bits for the size, 3 bits index */
511 data = (guint8 *) gst_adapter_peek (rtpmp4gpay->adapter, payload_len);
512 memcpy (&payload[4], data, payload_len);
514 gst_adapter_flush (rtpmp4gpay->adapter, payload_len);
516 /* marker only if the packet is complete */
517 gst_rtp_buffer_set_marker (outbuf, avail <= payload_len);
519 GST_BUFFER_TIMESTAMP (outbuf) = rtpmp4gpay->first_ts;
521 ret = gst_basertppayload_push (GST_BASE_RTP_PAYLOAD (rtpmp4gpay), outbuf);
523 avail -= payload_len;
530 /* we expect buffers as exactly one complete AU
533 gst_rtp_mp4g_pay_handle_buffer (GstBaseRTPPayload * basepayload,
536 GstRtpMP4GPay *rtpmp4gpay;
541 rtpmp4gpay = GST_RTP_MP4G_PAY (basepayload);
543 rtpmp4gpay->first_ts = GST_BUFFER_TIMESTAMP (buffer);
545 /* we always encode and flush a full AU */
546 gst_adapter_push (rtpmp4gpay->adapter, buffer);
547 ret = gst_rtp_mp4g_pay_flush (rtpmp4gpay);
553 gst_rtp_mp4g_pay_set_property (GObject * object, guint prop_id,
554 const GValue * value, GParamSpec * pspec)
556 GstRtpMP4GPay *rtpmp4gpay;
558 rtpmp4gpay = GST_RTP_MP4G_PAY (object);
567 gst_rtp_mp4g_pay_get_property (GObject * object, guint prop_id,
568 GValue * value, GParamSpec * pspec)
570 GstRtpMP4GPay *rtpmp4gpay;
572 rtpmp4gpay = GST_RTP_MP4G_PAY (object);
581 gst_rtp_mp4g_pay_plugin_init (GstPlugin * plugin)
583 return gst_element_register (plugin, "rtpmp4gpay",
584 GST_RANK_NONE, GST_TYPE_RTP_MP4G_PAY);