1 /* GStreamer
2 * Copyright (C) <2009> Edward Hervey <bilboed@bilboed.com>
3 *
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.
8 *
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.
13 *
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.
18 */
19
20 #ifdef HAVE_CONFIG_H
21 # include "config.h"
22 #endif
23
24 #include <string.h>
25
26 #include <gst/rtp/gstrtpbuffer.h>
27 #include <gst/audio/audio.h>
28 #include "gstrtpelements.h"
29 #include "gstrtpqdmdepay.h"
30 #include "gstrtputils.h"
31
32 GST_DEBUG_CATEGORY (rtpqdm2depay_debug);
33 #define GST_CAT_DEFAULT rtpqdm2depay_debug
34
35 static GstStaticPadTemplate gst_rtp_qdm2_depay_src_template =
36 GST_STATIC_PAD_TEMPLATE ("src",
37 GST_PAD_SRC,
38 GST_PAD_ALWAYS,
39 GST_STATIC_CAPS ("audio/x-qdm2")
40 );
41
42 static GstStaticPadTemplate gst_rtp_qdm2_depay_sink_template =
43 GST_STATIC_PAD_TEMPLATE ("sink",
44 GST_PAD_SINK,
45 GST_PAD_ALWAYS,
46 GST_STATIC_CAPS ("application/x-rtp, "
47 "media = (string) \"audio\", " "encoding-name = (string)\"X-QDM\"")
48 );
49
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);
53 #define _do_init \
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);
59
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
64 };
65
66 static void gst_rtp_qdm2_depay_finalize (GObject * object);
67
68 static GstStateChangeReturn gst_rtp_qdm2_depay_change_state (GstElement *
69 element, GstStateChange transition);
70
71 static GstBuffer *gst_rtp_qdm2_depay_process (GstRTPBaseDepayload * depayload,
72 GstRTPBuffer * rtp);
73 gboolean gst_rtp_qdm2_depay_setcaps (GstRTPBaseDepayload * filter,
74 GstCaps * caps);
75
76 static void
gst_rtp_qdm2_depay_class_init(GstRtpQDM2DepayClass * klass)77 gst_rtp_qdm2_depay_class_init (GstRtpQDM2DepayClass * klass)
78 {
79 GObjectClass *gobject_class;
80 GstElementClass *gstelement_class;
81 GstRTPBaseDepayloadClass *gstrtpbasedepayload_class;
82
83 gobject_class = (GObjectClass *) klass;
84 gstelement_class = (GstElementClass *) klass;
85 gstrtpbasedepayload_class = (GstRTPBaseDepayloadClass *) klass;
86
87 gstrtpbasedepayload_class->process_rtp_packet = gst_rtp_qdm2_depay_process;
88 gstrtpbasedepayload_class->set_caps = gst_rtp_qdm2_depay_setcaps;
89
90 gobject_class->finalize = gst_rtp_qdm2_depay_finalize;
91
92 gstelement_class->change_state = gst_rtp_qdm2_depay_change_state;
93
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);
98
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>");
104 }
105
106 static void
gst_rtp_qdm2_depay_init(GstRtpQDM2Depay * rtpqdm2depay)107 gst_rtp_qdm2_depay_init (GstRtpQDM2Depay * rtpqdm2depay)
108 {
109 rtpqdm2depay->adapter = gst_adapter_new ();
110 }
111
112 static void
gst_rtp_qdm2_depay_finalize(GObject * object)113 gst_rtp_qdm2_depay_finalize (GObject * object)
114 {
115 GstRtpQDM2Depay *rtpqdm2depay;
116
117 rtpqdm2depay = GST_RTP_QDM2_DEPAY (object);
118
119 g_object_unref (rtpqdm2depay->adapter);
120 rtpqdm2depay->adapter = NULL;
121
122 G_OBJECT_CLASS (parent_class)->finalize (object);
123 }
124
125 /* only on the sink */
126 gboolean
gst_rtp_qdm2_depay_setcaps(GstRTPBaseDepayload * filter,GstCaps * caps)127 gst_rtp_qdm2_depay_setcaps (GstRTPBaseDepayload * filter, GstCaps * caps)
128 {
129 GstStructure *structure = gst_caps_get_structure (caps, 0);
130 gint clock_rate;
131
132 if (!gst_structure_get_int (structure, "clock-rate", &clock_rate))
133 clock_rate = 44100; /* default */
134 filter->clock_rate = clock_rate;
135
136 /* will set caps later */
137
138 return TRUE;
139 }
140
141 static void
flush_data(GstRtpQDM2Depay * depay)142 flush_data (GstRtpQDM2Depay * depay)
143 {
144 guint i;
145 guint avail;
146
147 if ((avail = gst_adapter_available (depay->adapter)))
148 gst_adapter_flush (depay->adapter, avail);
149
150 GST_DEBUG ("Flushing %d packets", depay->nbpackets);
151
152 for (i = 0; depay->packets[i]; i++) {
153 QDM2Packet *pack = depay->packets[i];
154 guint32 crc = 0;
155 int i = 0;
156 GstBuffer *buf;
157 guint8 *data;
158
159 /* CRC is the sum of everything (including first bytes) */
160
161 data = pack->data;
162
163 if (G_UNLIKELY (data == NULL))
164 continue;
165
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 */
169 data[0] = 0x82;
170 GST_WRITE_UINT16_BE (data + 1, depay->packetsize - 3);
171 } else {
172 data[0] = 0x2;
173 data[1] = depay->packetsize - 2;
174 }
175
176 /* Calculate CRC */
177 for (; i < depay->packetsize; i++)
178 crc += data[i];
179
180 GST_DEBUG ("CRC is 0x%x", crc);
181
182 /* Write CRC */
183 if (depay->packetsize > 0xff)
184 GST_WRITE_UINT16_BE (data + 3, crc);
185 else
186 GST_WRITE_UINT16_BE (data + 2, crc);
187
188 GST_MEMDUMP ("Extracted packet", data, depay->packetsize);
189
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));
194
195 gst_adapter_push (depay->adapter, buf);
196
197 pack->data = NULL;
198 }
199 }
200
201 static void
add_packet(GstRtpQDM2Depay * depay,guint32 pid,guint32 len,guint8 * data)202 add_packet (GstRtpQDM2Depay * depay, guint32 pid, guint32 len, guint8 * data)
203 {
204 QDM2Packet *packet;
205
206 if (G_UNLIKELY (!depay->configured))
207 return;
208
209 GST_DEBUG ("pid:%d, len:%d, data:%p", pid, len, data);
210
211 if (G_UNLIKELY (depay->packets[pid] == NULL)) {
212 depay->packets[pid] = g_malloc0 (sizeof (QDM2Packet));
213 depay->nbpackets = MAX (depay->nbpackets, pid + 1);
214 }
215 packet = depay->packets[pid];
216
217 GST_DEBUG ("packet:%p", packet);
218 GST_DEBUG ("packet->data:%p", packet->data);
219
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)
224 packet->offs = 5;
225 else
226 packet->offs = 4;
227 }
228
229 /* Finally copy the data over */
230 memcpy (packet->data + packet->offs, data, len);
231 packet->offs += len;
232 }
233
234 static GstBuffer *
gst_rtp_qdm2_depay_process(GstRTPBaseDepayload * depayload,GstRTPBuffer * rtp)235 gst_rtp_qdm2_depay_process (GstRTPBaseDepayload * depayload, GstRTPBuffer * rtp)
236 {
237 GstRtpQDM2Depay *rtpqdm2depay;
238 GstBuffer *outbuf = NULL;
239 guint16 seq;
240
241 rtpqdm2depay = GST_RTP_QDM2_DEPAY (depayload);
242
243 {
244 gint payload_len;
245 guint8 *payload;
246 guint avail;
247 guint pos = 0;
248
249 payload_len = gst_rtp_buffer_get_payload_len (rtp);
250 if (payload_len < 3)
251 goto bad_packet;
252
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 */
263 }
264 rtpqdm2depay->nextseq = seq + 1;
265
266 GST_DEBUG ("Payload size %d 0x%x sequence:%d", payload_len, payload_len,
267 seq);
268
269 GST_MEMDUMP ("Incoming payload", payload, payload_len);
270
271 while (pos < payload_len) {
272 switch (payload[pos]) {
273 case 0x80:{
274 GST_DEBUG ("Unrecognized 0x80 marker, skipping 12 bytes");
275 pos += 12;
276 }
277 break;
278 case 0xff:
279 /* HEADERS */
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)) {
287 guint8 *ourdata;
288 GstBuffer *codecdata;
289 GstMapInfo cmap;
290 GstCaps *caps;
291
292 /* First bytes are unknown */
293 GST_MEMDUMP ("Header", payload + pos, 32);
294 ourdata = payload + pos + 10;
295 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) */
303 pos += 30;
304 GST_DEBUG
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);
309
310 /* Caps */
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);
316
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;
325 } else {
326 GST_DEBUG ("Already configured, skipping headers");
327 pos += 40;
328 }
329 break;
330 default:{
331 /* Shuffled packet contents */
332 guint packetid = payload[pos++];
333 guint packettype = payload[pos++];
334 guint packlen = payload[pos++];
335 guint hsize = 2;
336
337 GST_DEBUG ("Packet id:%d, type:0x%x, len:%d",
338 packetid, packettype, packlen);
339
340 /* Packets bigger than 0xff bytes have a type with the high bit set */
341 if (G_UNLIKELY (packettype & 0x80)) {
342 packettype &= 0x7f;
343 packlen <<= 8;
344 packlen |= payload[pos++];
345 hsize = 3;
346 GST_DEBUG ("Packet id:%d, type:0x%x, len:%d",
347 packetid, packettype, packlen);
348 }
349
350 if (packettype > 0x7f) {
351 GST_ERROR ("HOUSTON WE HAVE A PROBLEM !!!!");
352 }
353 add_packet (rtpqdm2depay, packetid, packlen + hsize,
354 payload + pos - hsize);
355 pos += packlen;
356 }
357 }
358 }
359
360 GST_DEBUG ("final pos %d", pos);
361
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));
369 }
370 }
371
372 return outbuf;
373
374 /* ERRORS */
375 bad_packet:
376 {
377 GST_ELEMENT_WARNING (rtpqdm2depay, STREAM, DECODE,
378 (NULL), ("Packet was too short"));
379 return NULL;
380 }
381 }
382
383 static GstStateChangeReturn
gst_rtp_qdm2_depay_change_state(GstElement * element,GstStateChange transition)384 gst_rtp_qdm2_depay_change_state (GstElement * element,
385 GstStateChange transition)
386 {
387 GstRtpQDM2Depay *rtpqdm2depay;
388 GstStateChangeReturn ret;
389
390 rtpqdm2depay = GST_RTP_QDM2_DEPAY (element);
391
392 switch (transition) {
393 case GST_STATE_CHANGE_NULL_TO_READY:
394 break;
395 case GST_STATE_CHANGE_READY_TO_PAUSED:
396 gst_adapter_clear (rtpqdm2depay->adapter);
397 break;
398 default:
399 break;
400 }
401
402 ret = GST_ELEMENT_CLASS (parent_class)->change_state (element, transition);
403
404 switch (transition) {
405 case GST_STATE_CHANGE_READY_TO_NULL:
406 break;
407 default:
408 break;
409 }
410 return ret;
411 }
412