2 * Copyright (C) <2009> Edward Hervey <bilboed@bilboed.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/rtp/gstrtpbuffer.h>
27 #include <gst/audio/audio.h>
28 #include "gstrtpqdmdepay.h"
29 #include "gstrtputils.h"
31 GST_DEBUG_CATEGORY (rtpqdm2depay_debug);
32 #define GST_CAT_DEFAULT rtpqdm2depay_debug
34 static GstStaticPadTemplate gst_rtp_qdm2_depay_src_template =
35 GST_STATIC_PAD_TEMPLATE ("src",
38 GST_STATIC_CAPS ("audio/x-qdm2")
41 static GstStaticPadTemplate gst_rtp_qdm2_depay_sink_template =
42 GST_STATIC_PAD_TEMPLATE ("sink",
45 GST_STATIC_CAPS ("application/x-rtp, "
46 "media = (string) \"audio\", " "encoding-name = (string)\"X-QDM\"")
49 #define gst_rtp_qdm2_depay_parent_class parent_class
50 G_DEFINE_TYPE (GstRtpQDM2Depay, gst_rtp_qdm2_depay,
51 GST_TYPE_RTP_BASE_DEPAYLOAD);
53 static const guint8 headheader[20] = {
54 0x0, 0x0, 0x0, 0xc, 0x66, 0x72, 0x6d, 0x61,
55 0x51, 0x44, 0x4d, 0x32, 0x0, 0x0, 0x0, 0x24,
56 0x51, 0x44, 0x43, 0x41
59 static void gst_rtp_qdm2_depay_finalize (GObject * object);
61 static GstStateChangeReturn gst_rtp_qdm2_depay_change_state (GstElement *
62 element, GstStateChange transition);
64 static GstBuffer *gst_rtp_qdm2_depay_process (GstRTPBaseDepayload * depayload,
66 gboolean gst_rtp_qdm2_depay_setcaps (GstRTPBaseDepayload * filter,
70 gst_rtp_qdm2_depay_class_init (GstRtpQDM2DepayClass * klass)
72 GObjectClass *gobject_class;
73 GstElementClass *gstelement_class;
74 GstRTPBaseDepayloadClass *gstrtpbasedepayload_class;
76 gobject_class = (GObjectClass *) klass;
77 gstelement_class = (GstElementClass *) klass;
78 gstrtpbasedepayload_class = (GstRTPBaseDepayloadClass *) klass;
80 gstrtpbasedepayload_class->process_rtp_packet = gst_rtp_qdm2_depay_process;
81 gstrtpbasedepayload_class->set_caps = gst_rtp_qdm2_depay_setcaps;
83 gobject_class->finalize = gst_rtp_qdm2_depay_finalize;
85 gstelement_class->change_state = gst_rtp_qdm2_depay_change_state;
87 gst_element_class_add_static_pad_template (gstelement_class,
88 &gst_rtp_qdm2_depay_src_template);
89 gst_element_class_add_static_pad_template (gstelement_class,
90 &gst_rtp_qdm2_depay_sink_template);
92 gst_element_class_set_static_metadata (gstelement_class,
93 "RTP QDM2 depayloader",
94 "Codec/Depayloader/Network/RTP",
95 "Extracts QDM2 audio from RTP packets (no RFC)",
96 "Edward Hervey <bilboed@bilboed.com>");
100 gst_rtp_qdm2_depay_init (GstRtpQDM2Depay * rtpqdm2depay)
102 rtpqdm2depay->adapter = gst_adapter_new ();
106 gst_rtp_qdm2_depay_finalize (GObject * object)
108 GstRtpQDM2Depay *rtpqdm2depay;
110 rtpqdm2depay = GST_RTP_QDM2_DEPAY (object);
112 g_object_unref (rtpqdm2depay->adapter);
113 rtpqdm2depay->adapter = NULL;
115 G_OBJECT_CLASS (parent_class)->finalize (object);
118 /* only on the sink */
120 gst_rtp_qdm2_depay_setcaps (GstRTPBaseDepayload * filter, GstCaps * caps)
122 GstStructure *structure = gst_caps_get_structure (caps, 0);
125 if (!gst_structure_get_int (structure, "clock-rate", &clock_rate))
126 clock_rate = 44100; /* default */
127 filter->clock_rate = clock_rate;
129 /* will set caps later */
135 flush_data (GstRtpQDM2Depay * depay)
140 if ((avail = gst_adapter_available (depay->adapter)))
141 gst_adapter_flush (depay->adapter, avail);
143 GST_DEBUG ("Flushing %d packets", depay->nbpackets);
145 for (i = 0; depay->packets[i]; i++) {
146 QDM2Packet *pack = depay->packets[i];
152 /* CRC is the sum of everything (including first bytes) */
156 if (G_UNLIKELY (data == NULL))
159 /* If the packet size is bigger than 0xff, we need 2 bytes to store the size */
160 if (depay->packetsize > 0xff) {
161 /* Expanded size 0x02 | 0x80 */
163 GST_WRITE_UINT16_BE (data + 1, depay->packetsize - 3);
166 data[1] = depay->packetsize - 2;
170 for (; i < depay->packetsize; i++)
173 GST_DEBUG ("CRC is 0x%x", crc);
176 if (depay->packetsize > 0xff)
177 GST_WRITE_UINT16_BE (data + 3, crc);
179 GST_WRITE_UINT16_BE (data + 2, crc);
181 GST_MEMDUMP ("Extracted packet", data, depay->packetsize);
183 buf = gst_buffer_new ();
184 gst_buffer_append_memory (buf,
185 gst_memory_new_wrapped (0, data, depay->packetsize, 0,
186 depay->packetsize, data, g_free));
188 gst_adapter_push (depay->adapter, buf);
195 add_packet (GstRtpQDM2Depay * depay, guint32 pid, guint32 len, guint8 * data)
199 if (G_UNLIKELY (!depay->configured))
202 GST_DEBUG ("pid:%d, len:%d, data:%p", pid, len, data);
204 if (G_UNLIKELY (depay->packets[pid] == NULL)) {
205 depay->packets[pid] = g_malloc0 (sizeof (QDM2Packet));
206 depay->nbpackets = MAX (depay->nbpackets, pid + 1);
208 packet = depay->packets[pid];
210 GST_DEBUG ("packet:%p", packet);
211 GST_DEBUG ("packet->data:%p", packet->data);
213 if (G_UNLIKELY (packet->data == NULL)) {
214 packet->data = g_malloc0 (depay->packetsize);
215 /* We leave space for the header/crc */
216 if (depay->packetsize > 0xff)
222 /* Finally copy the data over */
223 memcpy (packet->data + packet->offs, data, len);
228 gst_rtp_qdm2_depay_process (GstRTPBaseDepayload * depayload, GstRTPBuffer * rtp)
230 GstRtpQDM2Depay *rtpqdm2depay;
231 GstBuffer *outbuf = NULL;
234 rtpqdm2depay = GST_RTP_QDM2_DEPAY (depayload);
242 payload_len = gst_rtp_buffer_get_payload_len (rtp);
246 payload = gst_rtp_buffer_get_payload (rtp);
247 seq = gst_rtp_buffer_get_seq (rtp);
248 if (G_UNLIKELY (seq != rtpqdm2depay->nextseq)) {
249 GST_DEBUG ("GAP in sequence number, Resetting data !");
250 /* Flush previous data */
251 flush_data (rtpqdm2depay);
252 /* And store new timestamp */
253 rtpqdm2depay->ptimestamp = rtpqdm2depay->timestamp;
254 rtpqdm2depay->timestamp = GST_BUFFER_PTS (rtp->buffer);
255 /* And that previous data will be pushed at the bottom */
257 rtpqdm2depay->nextseq = seq + 1;
259 GST_DEBUG ("Payload size %d 0x%x sequence:%d", payload_len, payload_len,
262 GST_MEMDUMP ("Incoming payload", payload, payload_len);
264 while (pos < payload_len) {
265 switch (payload[pos]) {
267 GST_DEBUG ("Unrecognized 0x80 marker, skipping 12 bytes");
273 GST_DEBUG ("Headers");
274 /* Store the incoming timestamp */
275 rtpqdm2depay->ptimestamp = rtpqdm2depay->timestamp;
276 rtpqdm2depay->timestamp = GST_BUFFER_PTS (rtp->buffer);
277 /* flush the internal data if needed */
278 flush_data (rtpqdm2depay);
279 if (G_UNLIKELY (!rtpqdm2depay->configured)) {
281 GstBuffer *codecdata;
285 /* First bytes are unknown */
286 GST_MEMDUMP ("Header", payload + pos, 32);
287 ourdata = payload + pos + 10;
289 rtpqdm2depay->channs = GST_READ_UINT32_BE (payload + pos + 4);
290 rtpqdm2depay->samplerate = GST_READ_UINT32_BE (payload + pos + 8);
291 rtpqdm2depay->bitrate = GST_READ_UINT32_BE (payload + pos + 12);
292 rtpqdm2depay->blocksize = GST_READ_UINT32_BE (payload + pos + 16);
293 rtpqdm2depay->framesize = GST_READ_UINT32_BE (payload + pos + 20);
294 rtpqdm2depay->packetsize = GST_READ_UINT32_BE (payload + pos + 24);
295 /* 16 bit empty block (0x02 0x00) */
298 ("channs:%d, samplerate:%d, bitrate:%d, blocksize:%d, framesize:%d, packetsize:%d",
299 rtpqdm2depay->channs, rtpqdm2depay->samplerate,
300 rtpqdm2depay->bitrate, rtpqdm2depay->blocksize,
301 rtpqdm2depay->framesize, rtpqdm2depay->packetsize);
304 codecdata = gst_buffer_new_and_alloc (48);
305 gst_buffer_map (codecdata, &cmap, GST_MAP_WRITE);
306 memcpy (cmap.data, headheader, 20);
307 memcpy (cmap.data + 20, ourdata, 28);
308 gst_buffer_unmap (codecdata, &cmap);
310 caps = gst_caps_new_simple ("audio/x-qdm2",
311 "samplesize", G_TYPE_INT, 16,
312 "rate", G_TYPE_INT, rtpqdm2depay->samplerate,
313 "channels", G_TYPE_INT, rtpqdm2depay->channs,
314 "codec_data", GST_TYPE_BUFFER, codecdata, NULL);
315 gst_pad_set_caps (GST_RTP_BASE_DEPAYLOAD_SRCPAD (depayload), caps);
316 gst_caps_unref (caps);
317 rtpqdm2depay->configured = TRUE;
319 GST_DEBUG ("Already configured, skipping headers");
324 /* Shuffled packet contents */
325 guint packetid = payload[pos++];
326 guint packettype = payload[pos++];
327 guint packlen = payload[pos++];
330 GST_DEBUG ("Packet id:%d, type:0x%x, len:%d",
331 packetid, packettype, packlen);
333 /* Packets bigger than 0xff bytes have a type with the high bit set */
334 if (G_UNLIKELY (packettype & 0x80)) {
337 packlen |= payload[pos++];
339 GST_DEBUG ("Packet id:%d, type:0x%x, len:%d",
340 packetid, packettype, packlen);
343 if (packettype > 0x7f) {
344 GST_ERROR ("HOUSTON WE HAVE A PROBLEM !!!!");
346 add_packet (rtpqdm2depay, packetid, packlen + hsize,
347 payload + pos - hsize);
353 GST_DEBUG ("final pos %d", pos);
355 avail = gst_adapter_available (rtpqdm2depay->adapter);
356 if (G_UNLIKELY (avail)) {
357 GST_DEBUG ("Pushing out %d bytes of collected data", avail);
358 outbuf = gst_adapter_take_buffer (rtpqdm2depay->adapter, avail);
359 GST_BUFFER_PTS (outbuf) = rtpqdm2depay->ptimestamp;
360 GST_DEBUG ("Outgoing buffer timestamp %" GST_TIME_FORMAT,
361 GST_TIME_ARGS (rtpqdm2depay->ptimestamp));
370 GST_ELEMENT_WARNING (rtpqdm2depay, STREAM, DECODE,
371 (NULL), ("Packet was too short"));
376 static GstStateChangeReturn
377 gst_rtp_qdm2_depay_change_state (GstElement * element,
378 GstStateChange transition)
380 GstRtpQDM2Depay *rtpqdm2depay;
381 GstStateChangeReturn ret;
383 rtpqdm2depay = GST_RTP_QDM2_DEPAY (element);
385 switch (transition) {
386 case GST_STATE_CHANGE_NULL_TO_READY:
388 case GST_STATE_CHANGE_READY_TO_PAUSED:
389 gst_adapter_clear (rtpqdm2depay->adapter);
395 ret = GST_ELEMENT_CLASS (parent_class)->change_state (element, transition);
397 switch (transition) {
398 case GST_STATE_CHANGE_READY_TO_NULL:
407 gst_rtp_qdm2_depay_plugin_init (GstPlugin * plugin)
409 GST_DEBUG_CATEGORY_INIT (rtpqdm2depay_debug, "rtpqdm2depay", 0,
410 "RTP QDM2 depayloader");
412 return gst_element_register (plugin, "rtpqdm2depay",
413 GST_RANK_SECONDARY, GST_TYPE_RTP_QDM2_DEPAY);