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 (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_details_simple (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,
186 guint8 objectType = 0;
187 guint8 samplingIdx = 0;
188 guint8 channelCfg = 0;
191 data = gst_buffer_map (buffer, &size, NULL, GST_MAP_READ);
193 gst_bit_reader_init (&br, data, size);
195 /* any object type is fine, we need to copy it to the profile-level-id field. */
196 if (!gst_bit_reader_get_bits_uint8 (&br, &objectType, 5))
201 if (!gst_bit_reader_get_bits_uint8 (&br, &samplingIdx, 4))
203 /* only fixed values for now */
204 if (samplingIdx > 12 && samplingIdx != 15)
207 if (!gst_bit_reader_get_bits_uint8 (&br, &channelCfg, 4))
212 /* rtp rate depends on sampling rate of the audio */
213 if (samplingIdx == 15) {
216 /* index of 15 means we get the rate in the next 24 bits */
217 if (!gst_bit_reader_get_bits_uint32 (&br, &rate, 24))
220 rtpmp4gpay->rate = rate;
222 /* else use the rate from the table */
223 rtpmp4gpay->rate = sampling_table[samplingIdx];
226 rtpmp4gpay->frame_len = 1024;
228 switch (objectType) {
236 guint8 frameLenFlag = 0;
238 if (gst_bit_reader_get_bits_uint8 (&br, &frameLenFlag, 1))
240 rtpmp4gpay->frame_len = 960;
248 /* extra rtp params contain the number of channels */
249 g_free (rtpmp4gpay->params);
250 rtpmp4gpay->params = g_strdup_printf ("%d", channelCfg);
251 /* audio stream type */
252 rtpmp4gpay->streamtype = "5";
253 /* mode only high bitrate for now */
254 rtpmp4gpay->mode = "AAC-hbr";
256 g_free (rtpmp4gpay->profile);
257 rtpmp4gpay->profile = g_strdup_printf ("%d", objectType);
259 GST_DEBUG_OBJECT (rtpmp4gpay,
260 "objectType: %d, samplingIdx: %d (%d), channelCfg: %d, frame_len %d",
261 objectType, samplingIdx, rtpmp4gpay->rate, channelCfg,
262 rtpmp4gpay->frame_len);
264 gst_buffer_unmap (buffer, data, -1);
270 GST_ELEMENT_ERROR (rtpmp4gpay, STREAM, FORMAT,
271 (NULL), ("config string too short"));
272 gst_buffer_unmap (buffer, data, -1);
277 GST_ELEMENT_ERROR (rtpmp4gpay, STREAM, FORMAT,
278 (NULL), ("invalid object type"));
279 gst_buffer_unmap (buffer, data, -1);
284 GST_ELEMENT_ERROR (rtpmp4gpay, STREAM, NOT_IMPLEMENTED,
285 (NULL), ("unsupported frequency index %d", samplingIdx));
286 gst_buffer_unmap (buffer, data, -1);
291 GST_ELEMENT_ERROR (rtpmp4gpay, STREAM, NOT_IMPLEMENTED,
292 (NULL), ("unsupported number of channels %d, must < 8", channelCfg));
293 gst_buffer_unmap (buffer, data, -1);
298 #define VOS_STARTCODE 0x000001B0
301 gst_rtp_mp4g_pay_parse_video_config (GstRtpMP4GPay * rtpmp4gpay,
308 data = gst_buffer_map (buffer, &size, NULL, GST_MAP_READ);
313 code = GST_READ_UINT32_BE (data);
315 g_free (rtpmp4gpay->profile);
316 if (code == VOS_STARTCODE) {
318 rtpmp4gpay->profile = g_strdup_printf ("%d", (gint) data[4]);
320 GST_ELEMENT_WARNING (rtpmp4gpay, STREAM, FORMAT,
321 (NULL), ("profile not found in config string, assuming \'1\'"));
322 rtpmp4gpay->profile = g_strdup ("1");
326 rtpmp4gpay->rate = 90000;
327 /* video stream type */
328 rtpmp4gpay->streamtype = "4";
329 /* no params for video */
330 rtpmp4gpay->params = NULL;
332 rtpmp4gpay->mode = "generic";
334 GST_LOG_OBJECT (rtpmp4gpay, "profile %s", rtpmp4gpay->profile);
336 gst_buffer_unmap (buffer, data, -1);
343 GST_ELEMENT_ERROR (rtpmp4gpay, STREAM, FORMAT,
344 (NULL), ("config string too short"));
345 gst_buffer_unmap (buffer, data, -1);
351 gst_rtp_mp4g_pay_new_caps (GstRtpMP4GPay * rtpmp4gpay)
358 "streamtype", G_TYPE_STRING, rtpmp4gpay->streamtype, \
359 "profile-level-id", G_TYPE_STRING, rtpmp4gpay->profile, \
360 "mode", G_TYPE_STRING, rtpmp4gpay->mode, \
361 "config", G_TYPE_STRING, config, \
362 "sizelength", G_TYPE_STRING, "13", \
363 "indexlength", G_TYPE_STRING, "3", \
364 "indexdeltalength", G_TYPE_STRING, "3", \
367 g_value_init (&v, GST_TYPE_BUFFER);
368 gst_value_set_buffer (&v, rtpmp4gpay->config);
369 config = gst_value_serialize (&v);
372 if (rtpmp4gpay->params) {
373 res = gst_rtp_base_payload_set_outcaps (GST_RTP_BASE_PAYLOAD (rtpmp4gpay),
374 "encoding-params", G_TYPE_STRING, rtpmp4gpay->params, MP4GCAPS);
376 res = gst_rtp_base_payload_set_outcaps (GST_RTP_BASE_PAYLOAD (rtpmp4gpay),
388 gst_rtp_mp4g_pay_setcaps (GstRTPBasePayload * payload, GstCaps * caps)
390 GstRtpMP4GPay *rtpmp4gpay;
391 GstStructure *structure;
392 const GValue *codec_data;
393 const gchar *media_type = NULL;
396 rtpmp4gpay = GST_RTP_MP4G_PAY (payload);
398 structure = gst_caps_get_structure (caps, 0);
400 codec_data = gst_structure_get_value (structure, "codec_data");
402 GST_LOG_OBJECT (rtpmp4gpay, "got codec_data");
403 if (G_VALUE_TYPE (codec_data) == GST_TYPE_BUFFER) {
407 buffer = gst_value_get_buffer (codec_data);
408 GST_LOG_OBJECT (rtpmp4gpay, "configuring codec_data");
410 name = gst_structure_get_name (structure);
413 if (!strcmp (name, "audio/mpeg")) {
414 res = gst_rtp_mp4g_pay_parse_audio_config (rtpmp4gpay, buffer);
415 media_type = "audio";
416 } else if (!strcmp (name, "video/mpeg")) {
417 res = gst_rtp_mp4g_pay_parse_video_config (rtpmp4gpay, buffer);
418 media_type = "video";
425 /* now we can configure the buffer */
426 if (rtpmp4gpay->config)
427 gst_buffer_unref (rtpmp4gpay->config);
429 rtpmp4gpay->config = gst_buffer_copy (buffer);
432 if (media_type == NULL)
435 gst_rtp_base_payload_set_options (payload, media_type, TRUE, "MPEG4-GENERIC",
438 res = gst_rtp_mp4g_pay_new_caps (rtpmp4gpay);
445 GST_DEBUG_OBJECT (rtpmp4gpay, "failed to parse config");
451 gst_rtp_mp4g_pay_flush (GstRtpMP4GPay * rtpmp4gpay)
458 /* the data available in the adapter is either smaller
459 * than the MTU or bigger. In the case it is smaller, the complete
460 * adapter contents can be put in one packet. In the case the
461 * adapter has more than one MTU, we need to fragment the MPEG data
462 * over multiple packets. */
463 total = avail = gst_adapter_available (rtpmp4gpay->adapter);
466 mtu = GST_RTP_BASE_PAYLOAD_MTU (rtpmp4gpay);
475 /* this will be the total lenght of the packet */
476 packet_len = gst_rtp_buffer_calc_packet_len (avail, 0, 0);
478 /* fill one MTU or all available bytes, we need 4 spare bytes for
480 towrite = MIN (packet_len, mtu - 4);
482 /* this is the payload length */
483 payload_len = gst_rtp_buffer_calc_payload_len (towrite, 0, 0);
485 GST_DEBUG_OBJECT (rtpmp4gpay,
486 "avail %d, towrite %d, packet_len %d, payload_len %d", avail, towrite,
487 packet_len, payload_len);
489 /* create buffer to hold the payload, also make room for the 4 header bytes. */
490 outbuf = gst_rtp_buffer_new_allocate (payload_len + 4, 0, 0);
492 gst_rtp_buffer_map (outbuf, GST_MAP_WRITE, &rtp);
495 payload = gst_rtp_buffer_get_payload (&rtp);
497 /* +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+- .. -+-+-+-+-+-+-+-+-+-+
498 * |AU-headers-length|AU-header|AU-header| |AU-header|padding|
499 * | | (1) | (2) | | (n) | bits |
500 * +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+- .. -+-+-+-+-+-+-+-+-+-+
502 /* AU-headers-length, we only have 1 AU-header */
504 payload[1] = 0x10; /* we use 16 bits for the header */
506 /* +---------------------------------------+
508 * +---------------------------------------+
509 * | AU-Index / AU-Index-delta |
510 * +---------------------------------------+
512 * +---------------------------------------+
514 * +---------------------------------------+
516 * +---------------------------------------+
518 * +---------------------------------------+
520 * +---------------------------------------+
522 * +---------------------------------------+
524 /* The AU-header, no CTS, DTS, RAP, Stream-state
526 * AU-size is always the total size of the AU, not the fragmented size
528 payload[2] = (total & 0x1fe0) >> 5;
529 payload[3] = (total & 0x1f) << 3; /* we use 13 bits for the size, 3 bits index */
531 /* copy stuff from adapter to payload */
532 gst_adapter_copy (rtpmp4gpay->adapter, &payload[4], 0, payload_len);
533 gst_adapter_flush (rtpmp4gpay->adapter, payload_len);
535 /* marker only if the packet is complete */
536 gst_rtp_buffer_set_marker (&rtp, avail <= payload_len);
538 gst_rtp_buffer_unmap (&rtp);
540 GST_BUFFER_TIMESTAMP (outbuf) = rtpmp4gpay->first_timestamp;
541 GST_BUFFER_DURATION (outbuf) = rtpmp4gpay->first_duration;
543 if (rtpmp4gpay->frame_len) {
544 GST_BUFFER_OFFSET (outbuf) = rtpmp4gpay->offset;
545 rtpmp4gpay->offset += rtpmp4gpay->frame_len;
548 ret = gst_rtp_base_payload_push (GST_RTP_BASE_PAYLOAD (rtpmp4gpay), outbuf);
550 avail -= payload_len;
556 /* we expect buffers as exactly one complete AU
559 gst_rtp_mp4g_pay_handle_buffer (GstRTPBasePayload * basepayload,
562 GstRtpMP4GPay *rtpmp4gpay;
564 rtpmp4gpay = GST_RTP_MP4G_PAY (basepayload);
566 rtpmp4gpay->first_timestamp = GST_BUFFER_TIMESTAMP (buffer);
567 rtpmp4gpay->first_duration = GST_BUFFER_DURATION (buffer);
569 /* we always encode and flush a full AU */
570 gst_adapter_push (rtpmp4gpay->adapter, buffer);
572 return gst_rtp_mp4g_pay_flush (rtpmp4gpay);
576 gst_rtp_mp4g_pay_sink_event (GstRTPBasePayload * payload, GstEvent * event)
578 GstRtpMP4GPay *rtpmp4gpay;
580 rtpmp4gpay = GST_RTP_MP4G_PAY (payload);
582 GST_DEBUG ("Got event: %s", GST_EVENT_TYPE_NAME (event));
584 switch (GST_EVENT_TYPE (event)) {
585 case GST_EVENT_SEGMENT:
587 /* This flush call makes sure that the last buffer is always pushed
588 * to the base payloader */
589 gst_rtp_mp4g_pay_flush (rtpmp4gpay);
591 case GST_EVENT_FLUSH_STOP:
592 gst_rtp_mp4g_pay_reset (rtpmp4gpay);
598 /* let parent handle event too */
599 return GST_RTP_BASE_PAYLOAD_CLASS (parent_class)->sink_event (payload, event);
602 static GstStateChangeReturn
603 gst_rtp_mp4g_pay_change_state (GstElement * element, GstStateChange transition)
605 GstStateChangeReturn ret;
606 GstRtpMP4GPay *rtpmp4gpay;
608 rtpmp4gpay = GST_RTP_MP4G_PAY (element);
610 switch (transition) {
611 case GST_STATE_CHANGE_READY_TO_PAUSED:
612 gst_rtp_mp4g_pay_cleanup (rtpmp4gpay);
618 ret = GST_ELEMENT_CLASS (parent_class)->change_state (element, transition);
620 switch (transition) {
621 case GST_STATE_CHANGE_PAUSED_TO_READY:
622 gst_rtp_mp4g_pay_cleanup (rtpmp4gpay);
632 gst_rtp_mp4g_pay_plugin_init (GstPlugin * plugin)
634 return gst_element_register (plugin, "rtpmp4gpay",
635 GST_RANK_SECONDARY, GST_TYPE_RTP_MP4G_PAY);