1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
|
/* GStreamer
* Copyright (C) <2009> Edward Hervey <bilboed@bilboed.com>
*
* This library is free software; you can redistribute it and/or
* modify it under the terms of the GNU Library General Public
* License as published by the Free Software Foundation; either
* version 2 of the License, or (at your option) any later version.
*
* This library is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
* Library General Public License for more details.
*
* You should have received a copy of the GNU Library General Public
* License along with this library; if not, write to the
* Free Software Foundation, Inc., 59 Temple Place - Suite 330,
* Boston, MA 02111-1307, USA.
*/
#ifdef HAVE_CONFIG_H
# include "config.h"
#endif
#include <string.h>
#include <gst/rtp/gstrtpbuffer.h>
#include "gstrtpqdmdepay.h"
GST_DEBUG_CATEGORY (rtpqdm2depay_debug);
#define GST_CAT_DEFAULT rtpqdm2depay_debug
/* elementfactory information */
static const GstElementDetails gst_rtp_qdm2depay_details =
GST_ELEMENT_DETAILS ("RTP QDM2 depayloader",
"Codec/Depayloader/Network",
"Extracts QDM2 audio from RTP packets (no RFC)",
"Edward Hervey <bilboed@bilboed.com>");
static GstStaticPadTemplate gst_rtp_qdm2_depay_src_template =
GST_STATIC_PAD_TEMPLATE ("src",
GST_PAD_SRC,
GST_PAD_ALWAYS,
GST_STATIC_CAPS ("audio/x-qdm2")
);
static GstStaticPadTemplate gst_rtp_qdm2_depay_sink_template =
GST_STATIC_PAD_TEMPLATE ("sink",
GST_PAD_SINK,
GST_PAD_ALWAYS,
GST_STATIC_CAPS ("application/x-rtp, "
"media = (string) \"audio\", "
"payload = (int) " GST_RTP_PAYLOAD_DYNAMIC_STRING ", "
"encoding-name = (string)\"X-QDM\"")
);
GST_BOILERPLATE (GstRtpQDM2Depay, gst_rtp_qdm2_depay, GstBaseRTPDepayload,
GST_TYPE_BASE_RTP_DEPAYLOAD);
static const guint8 headheader[20] = {
0x0, 0x0, 0x0, 0xc, 0x66, 0x72, 0x6d, 0x61,
0x51, 0x44, 0x4d, 0x32, 0x0, 0x0, 0x0, 0x24,
0x51, 0x44, 0x43, 0x41
};
static void gst_rtp_qdm2_depay_finalize (GObject * object);
static GstStateChangeReturn gst_rtp_qdm2_depay_change_state (GstElement *
element, GstStateChange transition);
static GstBuffer *gst_rtp_qdm2_depay_process (GstBaseRTPDepayload * depayload,
GstBuffer * buf);
gboolean gst_rtp_qdm2_depay_setcaps (GstBaseRTPDepayload * filter,
GstCaps * caps);
static void
gst_rtp_qdm2_depay_base_init (gpointer klass)
{
GstElementClass *element_class = GST_ELEMENT_CLASS (klass);
gst_element_class_add_pad_template (element_class,
gst_static_pad_template_get (&gst_rtp_qdm2_depay_src_template));
gst_element_class_add_pad_template (element_class,
gst_static_pad_template_get (&gst_rtp_qdm2_depay_sink_template));
gst_element_class_set_details (element_class, &gst_rtp_qdm2depay_details);
}
static void
gst_rtp_qdm2_depay_class_init (GstRtpQDM2DepayClass * klass)
{
GObjectClass *gobject_class;
GstElementClass *gstelement_class;
GstBaseRTPDepayloadClass *gstbasertpdepayload_class;
gobject_class = (GObjectClass *) klass;
gstelement_class = (GstElementClass *) klass;
gstbasertpdepayload_class = (GstBaseRTPDepayloadClass *) klass;
parent_class = g_type_class_peek_parent (klass);
gstbasertpdepayload_class->process = gst_rtp_qdm2_depay_process;
gstbasertpdepayload_class->set_caps = gst_rtp_qdm2_depay_setcaps;
gobject_class->finalize = gst_rtp_qdm2_depay_finalize;
gstelement_class->change_state = gst_rtp_qdm2_depay_change_state;
}
static void
gst_rtp_qdm2_depay_init (GstRtpQDM2Depay * rtpqdm2depay,
GstRtpQDM2DepayClass * klass)
{
rtpqdm2depay->adapter = gst_adapter_new ();
}
static void
gst_rtp_qdm2_depay_finalize (GObject * object)
{
GstRtpQDM2Depay *rtpqdm2depay;
rtpqdm2depay = GST_RTP_QDM2_DEPAY (object);
g_object_unref (rtpqdm2depay->adapter);
rtpqdm2depay->adapter = NULL;
G_OBJECT_CLASS (parent_class)->finalize (object);
}
// only on the sink
gboolean
gst_rtp_qdm2_depay_setcaps (GstBaseRTPDepayload * filter, GstCaps * caps)
{
GstStructure *structure = gst_caps_get_structure (caps, 0);
gint clock_rate;
if (!gst_structure_get_int (structure, "clock-rate", &clock_rate))
clock_rate = 44100; // default
filter->clock_rate = clock_rate;
/* will set caps later */
return TRUE;
}
static void
flush_data (GstRtpQDM2Depay * depay)
{
guint i;
guint avail;
if ((avail = gst_adapter_available (depay->adapter)))
gst_adapter_flush (depay->adapter, avail);
GST_DEBUG ("Flushing %d packets", depay->nbpackets);
for (i = 0; depay->packets[i]; i++) {
QDM2Packet *pack = depay->packets[i];
guint32 crc = 0;
int i = 0;
GstBuffer *buf;
guint8 *data;
/* CRC is the sum of everything (including first bytes) */
data = pack->data;
if (G_UNLIKELY (data == NULL))
continue;
/* If the packet size is bigger than 0xff, we need 2 bytes to store the size */
if (depay->packetsize > 0xff) {
/* Expanded size 0x02 | 0x80 */
data[0] = 0x82;
GST_WRITE_UINT16_BE (data + 1, depay->packetsize - 3);
} else {
data[0] = 0x2;
data[1] = depay->packetsize - 2;
}
/* Calculate CRC */
for (; i < depay->packetsize; i++)
crc += data[i];
GST_DEBUG ("CRC is 0x%x", crc);
/* Write CRC */
if (depay->packetsize > 0xff)
GST_WRITE_UINT16_BE (data + 3, crc);
else
GST_WRITE_UINT16_BE (data + 2, crc);
GST_MEMDUMP ("Extracted packet", data, depay->packetsize);
buf = gst_buffer_new ();
GST_BUFFER_DATA (buf) = data;
GST_BUFFER_MALLOCDATA (buf) = data;
GST_BUFFER_SIZE (buf) = depay->packetsize;
gst_adapter_push (depay->adapter, buf);
if (pack->data) {
pack->data = NULL;
}
}
}
static void
add_packet (GstRtpQDM2Depay * depay, guint32 pid, guint32 len, guint8 * data)
{
QDM2Packet *packet;
if (G_UNLIKELY (!depay->configured))
return;
GST_DEBUG ("pid:%d, len:%d, data:%p", pid, len, data);
if (G_UNLIKELY (depay->packets[pid] == NULL)) {
depay->packets[pid] = g_malloc0 (sizeof (QDM2Packet));
depay->nbpackets = MAX (depay->nbpackets, pid + 1);
}
packet = depay->packets[pid];
GST_DEBUG ("packet:%p", packet);
GST_DEBUG ("packet->data:%p", packet->data);
if (G_UNLIKELY (packet->data == NULL)) {
packet->data = g_malloc0 (depay->packetsize);
/* We leave space for the header/crc */
if (depay->packetsize > 0xff)
packet->offs = 5;
else
packet->offs = 4;
}
/* Finally copy the data over */
memcpy (packet->data + packet->offs, data, len);
packet->offs += len;
}
static GstBuffer *
gst_rtp_qdm2_depay_process (GstBaseRTPDepayload * depayload, GstBuffer * buf)
{
GstRtpQDM2Depay *rtpqdm2depay;
GstBuffer *outbuf;
guint16 seq;
rtpqdm2depay = GST_RTP_QDM2_DEPAY (depayload);
{
gint payload_len;
guint8 *payload;
guint avail;
guint pos = 0;
payload_len = gst_rtp_buffer_get_payload_len (buf);
if (payload_len < 3)
goto bad_packet;
payload = gst_rtp_buffer_get_payload (buf);
seq = gst_rtp_buffer_get_seq (buf);
if (G_UNLIKELY (seq != rtpqdm2depay->nextseq)) {
GST_DEBUG ("GAP in sequence number, Resetting data !");
/* Flush previous data */
flush_data (rtpqdm2depay);
/* And store new timestamp */
rtpqdm2depay->ptimestamp = rtpqdm2depay->timestamp;
rtpqdm2depay->timestamp = GST_BUFFER_TIMESTAMP (buf);
/* And that previous data will be pushed at the bottom */
}
rtpqdm2depay->nextseq = seq + 1;
GST_DEBUG ("Payload size %d 0x%x sequence:%d", payload_len, payload_len,
seq);
GST_MEMDUMP ("Incoming payload", payload, payload_len);
while (pos < payload_len) {
switch (payload[pos]) {
case 0x80:{
GST_DEBUG ("Unrecognized 0x80 marker, skipping 12 bytes");
pos += 12;
}
break;
case 0xff:
/* HEADERS */
GST_DEBUG ("Headers");
/* Store the incoming timestamp */
rtpqdm2depay->ptimestamp = rtpqdm2depay->timestamp;
rtpqdm2depay->timestamp = GST_BUFFER_TIMESTAMP (buf);
/* flush the internal data if needed */
flush_data (rtpqdm2depay);
if (G_UNLIKELY (!rtpqdm2depay->configured)) {
guint8 *ourdata;
GstBuffer *codecdata;
GstCaps *caps;
/* First bytes are unknown */
GST_MEMDUMP ("Header", payload + pos, 32);
ourdata = payload + pos + 10;
pos += 10;
rtpqdm2depay->channs = GST_READ_UINT32_BE (payload + pos + 4);
rtpqdm2depay->samplerate = GST_READ_UINT32_BE (payload + pos + 8);
rtpqdm2depay->bitrate = GST_READ_UINT32_BE (payload + pos + 12);
rtpqdm2depay->blocksize = GST_READ_UINT32_BE (payload + pos + 16);
rtpqdm2depay->framesize = GST_READ_UINT32_BE (payload + pos + 20);
rtpqdm2depay->packetsize = GST_READ_UINT32_BE (payload + pos + 24);
/* 16 bit empty block (0x02 0x00) */
pos += 30;
GST_DEBUG
("channs:%d, samplerate:%d, bitrate:%d, blocksize:%d, framesize:%d, packetsize:%d",
rtpqdm2depay->channs, rtpqdm2depay->samplerate,
rtpqdm2depay->bitrate, rtpqdm2depay->blocksize,
rtpqdm2depay->framesize, rtpqdm2depay->packetsize);
/* Caps */
codecdata = gst_buffer_new_and_alloc (48);
memcpy (GST_BUFFER_DATA (codecdata), headheader, 20);
memcpy (GST_BUFFER_DATA (codecdata) + 20, ourdata, 28);
caps = gst_caps_new_simple ("audio/x-qdm2",
"samplesize", G_TYPE_INT, 16,
"rate", G_TYPE_INT, rtpqdm2depay->samplerate,
"channels", G_TYPE_INT, rtpqdm2depay->channs,
"codec_data", GST_TYPE_BUFFER, codecdata, NULL);
gst_pad_set_caps (GST_BASE_RTP_DEPAYLOAD_SRCPAD (depayload), caps);
gst_caps_unref (caps);
rtpqdm2depay->configured = TRUE;
} else {
GST_DEBUG ("Already configured, skipping headers");
pos += 40;
}
break;
default:{
/* Shuffled packet contents */
guint packetid = payload[pos++];
guint packettype = payload[pos++];
guint packlen = payload[pos++];
guint hsize = 2;
GST_DEBUG ("Packet id:%d, type:0x%x, len:%d",
packetid, packettype, packlen);
/* Packets bigger than 0xff bytes have a type with the high bit set */
if (G_UNLIKELY (packettype & 0x80)) {
packettype &= 0x7f;
packlen <<= 8;
packlen |= payload[pos++];
hsize = 3;
GST_DEBUG ("Packet id:%d, type:0x%x, len:%d",
packetid, packettype, packlen);
}
if (packettype > 0x7f) {
GST_ERROR ("HOUSTON WE HAVE A PROBLEM !!!!");
}
add_packet (rtpqdm2depay, packetid, packlen + hsize,
payload + pos - hsize);
pos += packlen;
}
}
}
GST_DEBUG ("final pos %d", pos);
avail = gst_adapter_available (rtpqdm2depay->adapter);
if (G_UNLIKELY (avail)) {
GST_DEBUG ("Pushing out %d bytes of collected data", avail);
outbuf = gst_adapter_take_buffer (rtpqdm2depay->adapter, avail);
GST_BUFFER_TIMESTAMP (outbuf) = rtpqdm2depay->ptimestamp;
GST_DEBUG ("Outgoing buffer timestamp %" GST_TIME_FORMAT,
GST_TIME_ARGS (rtpqdm2depay->ptimestamp));
return outbuf;
}
}
return NULL;
/* ERRORS */
bad_packet:
{
GST_ELEMENT_WARNING (rtpqdm2depay, STREAM, DECODE,
(NULL), ("Packet was too short"));
return NULL;
}
}
static GstStateChangeReturn
gst_rtp_qdm2_depay_change_state (GstElement * element,
GstStateChange transition)
{
GstRtpQDM2Depay *rtpqdm2depay;
GstStateChangeReturn ret;
rtpqdm2depay = GST_RTP_QDM2_DEPAY (element);
switch (transition) {
case GST_STATE_CHANGE_NULL_TO_READY:
break;
case GST_STATE_CHANGE_READY_TO_PAUSED:
gst_adapter_clear (rtpqdm2depay->adapter);
break;
default:
break;
}
ret = GST_ELEMENT_CLASS (parent_class)->change_state (element, transition);
switch (transition) {
case GST_STATE_CHANGE_READY_TO_NULL:
break;
default:
break;
}
return ret;
}
gboolean
gst_rtp_qdm2_depay_plugin_init (GstPlugin * plugin)
{
GST_DEBUG_CATEGORY_INIT (rtpqdm2depay_debug, "rtpqdm2depay", 0,
"RTP QDM2 depayloader");
return gst_element_register (plugin, "rtpqdm2depay",
GST_RANK_MARGINAL, GST_TYPE_RTP_QDM2_DEPAY);
}
|