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 "gstrtpelements.h"
29 #include "gstrtpqdmdepay.h"
30 #include "gstrtputils.h"
32 GST_DEBUG_CATEGORY (rtpqdm2depay_debug);
33 #define GST_CAT_DEFAULT rtpqdm2depay_debug
35 static GstStaticPadTemplate gst_rtp_qdm2_depay_src_template =
36 GST_STATIC_PAD_TEMPLATE ("src",
39 GST_STATIC_CAPS ("audio/x-qdm2")
42 static GstStaticPadTemplate gst_rtp_qdm2_depay_sink_template =
43 GST_STATIC_PAD_TEMPLATE ("sink",
46 GST_STATIC_CAPS ("application/x-rtp, "
47 "media = (string) \"audio\", " "encoding-name = (string)\"X-QDM\"")
50 #define gst_rtp_qdm2_depay_parent_class parent_class
51 G_DEFINE_TYPE (GstRtpQDM2Depay, gst_rtp_qdm2_depay,
52 GST_TYPE_RTP_BASE_DEPAYLOAD);
54 GST_DEBUG_CATEGORY_INIT (rtpqdm2depay_debug, "rtpqdm2depay", 0, \
55 "RTP QDM2 depayloader"); \
56 rtp_element_init (plugin)
57 GST_ELEMENT_REGISTER_DEFINE_WITH_CODE (rtpqdm2depay, "rtpqdm2depay",
58 GST_RANK_SECONDARY, GST_TYPE_RTP_QDM2_DEPAY, _do_init);
60 static const guint8 headheader[20] = {
61 0x0, 0x0, 0x0, 0xc, 0x66, 0x72, 0x6d, 0x61,
62 0x51, 0x44, 0x4d, 0x32, 0x0, 0x0, 0x0, 0x24,
63 0x51, 0x44, 0x43, 0x41
66 static void gst_rtp_qdm2_depay_finalize (GObject * object);
68 static GstStateChangeReturn gst_rtp_qdm2_depay_change_state (GstElement *
69 element, GstStateChange transition);
71 static GstBuffer *gst_rtp_qdm2_depay_process (GstRTPBaseDepayload * depayload,
73 gboolean gst_rtp_qdm2_depay_setcaps (GstRTPBaseDepayload * filter,
77 gst_rtp_qdm2_depay_class_init (GstRtpQDM2DepayClass * klass)
79 GObjectClass *gobject_class;
80 GstElementClass *gstelement_class;
81 GstRTPBaseDepayloadClass *gstrtpbasedepayload_class;
83 gobject_class = (GObjectClass *) klass;
84 gstelement_class = (GstElementClass *) klass;
85 gstrtpbasedepayload_class = (GstRTPBaseDepayloadClass *) klass;
87 gstrtpbasedepayload_class->process_rtp_packet = gst_rtp_qdm2_depay_process;
88 gstrtpbasedepayload_class->set_caps = gst_rtp_qdm2_depay_setcaps;
90 gobject_class->finalize = gst_rtp_qdm2_depay_finalize;
92 gstelement_class->change_state = gst_rtp_qdm2_depay_change_state;
94 gst_element_class_add_static_pad_template (gstelement_class,
95 &gst_rtp_qdm2_depay_src_template);
96 gst_element_class_add_static_pad_template (gstelement_class,
97 &gst_rtp_qdm2_depay_sink_template);
99 gst_element_class_set_static_metadata (gstelement_class,
100 "RTP QDM2 depayloader",
101 "Codec/Depayloader/Network/RTP",
102 "Extracts QDM2 audio from RTP packets (no RFC)",
103 "Edward Hervey <bilboed@bilboed.com>");
107 gst_rtp_qdm2_depay_init (GstRtpQDM2Depay * rtpqdm2depay)
109 rtpqdm2depay->adapter = gst_adapter_new ();
113 gst_rtp_qdm2_depay_finalize (GObject * object)
115 GstRtpQDM2Depay *rtpqdm2depay;
117 rtpqdm2depay = GST_RTP_QDM2_DEPAY (object);
119 g_object_unref (rtpqdm2depay->adapter);
120 rtpqdm2depay->adapter = NULL;
122 G_OBJECT_CLASS (parent_class)->finalize (object);
125 /* only on the sink */
127 gst_rtp_qdm2_depay_setcaps (GstRTPBaseDepayload * filter, GstCaps * caps)
129 GstStructure *structure = gst_caps_get_structure (caps, 0);
132 if (!gst_structure_get_int (structure, "clock-rate", &clock_rate))
133 clock_rate = 44100; /* default */
134 filter->clock_rate = clock_rate;
136 /* will set caps later */
142 flush_data (GstRtpQDM2Depay * depay)
147 if ((avail = gst_adapter_available (depay->adapter)))
148 gst_adapter_flush (depay->adapter, avail);
150 GST_DEBUG ("Flushing %d packets", depay->nbpackets);
152 for (i = 0; depay->packets[i]; i++) {
153 QDM2Packet *pack = depay->packets[i];
159 /* CRC is the sum of everything (including first bytes) */
163 if (G_UNLIKELY (data == NULL))
166 /* If the packet size is bigger than 0xff, we need 2 bytes to store the size */
167 if (depay->packetsize > 0xff) {
168 /* Expanded size 0x02 | 0x80 */
170 GST_WRITE_UINT16_BE (data + 1, depay->packetsize - 3);
173 data[1] = depay->packetsize - 2;
177 for (; i < depay->packetsize; i++)
180 GST_DEBUG ("CRC is 0x%x", crc);
183 if (depay->packetsize > 0xff)
184 GST_WRITE_UINT16_BE (data + 3, crc);
186 GST_WRITE_UINT16_BE (data + 2, crc);
188 GST_MEMDUMP ("Extracted packet", data, depay->packetsize);
190 buf = gst_buffer_new ();
191 gst_buffer_append_memory (buf,
192 gst_memory_new_wrapped (0, data, depay->packetsize, 0,
193 depay->packetsize, data, g_free));
195 gst_adapter_push (depay->adapter, buf);
202 add_packet (GstRtpQDM2Depay * depay, guint32 pid, guint32 len, guint8 * data)
206 if (G_UNLIKELY (!depay->configured))
209 GST_DEBUG ("pid:%d, len:%d, data:%p", pid, len, data);
211 if (G_UNLIKELY (depay->packets[pid] == NULL)) {
212 depay->packets[pid] = g_malloc0 (sizeof (QDM2Packet));
213 depay->nbpackets = MAX (depay->nbpackets, pid + 1);
215 packet = depay->packets[pid];
217 GST_DEBUG ("packet:%p", packet);
218 GST_DEBUG ("packet->data:%p", packet->data);
220 if (G_UNLIKELY (packet->data == NULL)) {
221 packet->data = g_malloc0 (depay->packetsize);
222 /* We leave space for the header/crc */
223 if (depay->packetsize > 0xff)
229 /* Finally copy the data over */
230 memcpy (packet->data + packet->offs, data, len);
235 gst_rtp_qdm2_depay_process (GstRTPBaseDepayload * depayload, GstRTPBuffer * rtp)
237 GstRtpQDM2Depay *rtpqdm2depay;
238 GstBuffer *outbuf = NULL;
241 rtpqdm2depay = GST_RTP_QDM2_DEPAY (depayload);
249 payload_len = gst_rtp_buffer_get_payload_len (rtp);
253 payload = gst_rtp_buffer_get_payload (rtp);
254 seq = gst_rtp_buffer_get_seq (rtp);
255 if (G_UNLIKELY (seq != rtpqdm2depay->nextseq)) {
256 GST_DEBUG ("GAP in sequence number, Resetting data !");
257 /* Flush previous data */
258 flush_data (rtpqdm2depay);
259 /* And store new timestamp */
260 rtpqdm2depay->ptimestamp = rtpqdm2depay->timestamp;
261 rtpqdm2depay->timestamp = GST_BUFFER_PTS (rtp->buffer);
262 /* And that previous data will be pushed at the bottom */
264 rtpqdm2depay->nextseq = seq + 1;
266 GST_DEBUG ("Payload size %d 0x%x sequence:%d", payload_len, payload_len,
269 GST_MEMDUMP ("Incoming payload", payload, payload_len);
271 while (pos < payload_len) {
272 switch (payload[pos]) {
274 GST_DEBUG ("Unrecognized 0x80 marker, skipping 12 bytes");
280 GST_DEBUG ("Headers");
281 /* Store the incoming timestamp */
282 rtpqdm2depay->ptimestamp = rtpqdm2depay->timestamp;
283 rtpqdm2depay->timestamp = GST_BUFFER_PTS (rtp->buffer);
284 /* flush the internal data if needed */
285 flush_data (rtpqdm2depay);
286 if (G_UNLIKELY (!rtpqdm2depay->configured)) {
288 GstBuffer *codecdata;
292 /* First bytes are unknown */
293 GST_MEMDUMP ("Header", payload + pos, 32);
294 ourdata = payload + pos + 10;
296 rtpqdm2depay->channs = GST_READ_UINT32_BE (payload + pos + 4);
297 rtpqdm2depay->samplerate = GST_READ_UINT32_BE (payload + pos + 8);
298 rtpqdm2depay->bitrate = GST_READ_UINT32_BE (payload + pos + 12);
299 rtpqdm2depay->blocksize = GST_READ_UINT32_BE (payload + pos + 16);
300 rtpqdm2depay->framesize = GST_READ_UINT32_BE (payload + pos + 20);
301 rtpqdm2depay->packetsize = GST_READ_UINT32_BE (payload + pos + 24);
302 /* 16 bit empty block (0x02 0x00) */
305 ("channs:%d, samplerate:%d, bitrate:%d, blocksize:%d, framesize:%d, packetsize:%d",
306 rtpqdm2depay->channs, rtpqdm2depay->samplerate,
307 rtpqdm2depay->bitrate, rtpqdm2depay->blocksize,
308 rtpqdm2depay->framesize, rtpqdm2depay->packetsize);
311 codecdata = gst_buffer_new_and_alloc (48);
312 gst_buffer_map (codecdata, &cmap, GST_MAP_WRITE);
313 memcpy (cmap.data, headheader, 20);
314 memcpy (cmap.data + 20, ourdata, 28);
315 gst_buffer_unmap (codecdata, &cmap);
317 caps = gst_caps_new_simple ("audio/x-qdm2",
318 "samplesize", G_TYPE_INT, 16,
319 "rate", G_TYPE_INT, rtpqdm2depay->samplerate,
320 "channels", G_TYPE_INT, rtpqdm2depay->channs,
321 "codec_data", GST_TYPE_BUFFER, codecdata, NULL);
322 gst_pad_set_caps (GST_RTP_BASE_DEPAYLOAD_SRCPAD (depayload), caps);
323 gst_caps_unref (caps);
324 rtpqdm2depay->configured = TRUE;
326 GST_DEBUG ("Already configured, skipping headers");
331 /* Shuffled packet contents */
332 guint packetid = payload[pos++];
333 guint packettype = payload[pos++];
334 guint packlen = payload[pos++];
337 GST_DEBUG ("Packet id:%d, type:0x%x, len:%d",
338 packetid, packettype, packlen);
340 /* Packets bigger than 0xff bytes have a type with the high bit set */
341 if (G_UNLIKELY (packettype & 0x80)) {
344 packlen |= payload[pos++];
346 GST_DEBUG ("Packet id:%d, type:0x%x, len:%d",
347 packetid, packettype, packlen);
350 if (packettype > 0x7f) {
351 GST_ERROR ("HOUSTON WE HAVE A PROBLEM !!!!");
353 add_packet (rtpqdm2depay, packetid, packlen + hsize,
354 payload + pos - hsize);
360 GST_DEBUG ("final pos %d", pos);
362 avail = gst_adapter_available (rtpqdm2depay->adapter);
363 if (G_UNLIKELY (avail)) {
364 GST_DEBUG ("Pushing out %d bytes of collected data", avail);
365 outbuf = gst_adapter_take_buffer (rtpqdm2depay->adapter, avail);
366 GST_BUFFER_PTS (outbuf) = rtpqdm2depay->ptimestamp;
367 GST_DEBUG ("Outgoing buffer timestamp %" GST_TIME_FORMAT,
368 GST_TIME_ARGS (rtpqdm2depay->ptimestamp));
377 GST_ELEMENT_WARNING (rtpqdm2depay, STREAM, DECODE,
378 (NULL), ("Packet was too short"));
383 static GstStateChangeReturn
384 gst_rtp_qdm2_depay_change_state (GstElement * element,
385 GstStateChange transition)
387 GstRtpQDM2Depay *rtpqdm2depay;
388 GstStateChangeReturn ret;
390 rtpqdm2depay = GST_RTP_QDM2_DEPAY (element);
392 switch (transition) {
393 case GST_STATE_CHANGE_NULL_TO_READY:
395 case GST_STATE_CHANGE_READY_TO_PAUSED:
396 gst_adapter_clear (rtpqdm2depay->adapter);
402 ret = GST_ELEMENT_CLASS (parent_class)->change_state (element, transition);
404 switch (transition) {
405 case GST_STATE_CHANGE_READY_TO_NULL: