summaryrefslogtreecommitdiff
path: root/audio/gstrtpsbcpay.c
blob: 1159bfedfb44a9de637586b5abf2e3b6eb236f66 (plain)
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
/*
 *
 *  BlueZ - Bluetooth protocol stack for Linux
 *
 *  Copyright (C) 2004-2010  Marcel Holtmann <marcel@holtmann.org>
 *
 *
 *  This library is free software; you can redistribute it and/or
 *  modify it under the terms of the GNU Lesser General Public
 *  License as published by the Free Software Foundation; either
 *  version 2.1 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
 *  Lesser General Public License for more details.
 *
 *  You should have received a copy of the GNU Lesser General Public
 *  License along with this library; if not, write to the Free Software
 *  Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA  02110-1301  USA
 *
 */

#ifdef HAVE_CONFIG_H
#include <config.h>
#endif

#include "gstpragma.h"
#include "gstrtpsbcpay.h"
#include <math.h>
#include <string.h>

#define RTP_SBC_PAYLOAD_HEADER_SIZE 1
#define DEFAULT_MIN_FRAMES 0
#define RTP_SBC_HEADER_TOTAL (12 + RTP_SBC_PAYLOAD_HEADER_SIZE)

#if __BYTE_ORDER == __LITTLE_ENDIAN

struct rtp_payload {
	guint8 frame_count:4;
	guint8 rfa0:1;
	guint8 is_last_fragment:1;
	guint8 is_first_fragment:1;
	guint8 is_fragmented:1;
} __attribute__ ((packed));

#elif __BYTE_ORDER == __BIG_ENDIAN

struct rtp_payload {
	guint8 is_fragmented:1;
	guint8 is_first_fragment:1;
	guint8 is_last_fragment:1;
	guint8 rfa0:1;
	guint8 frame_count:4;
} __attribute__ ((packed));

#else
#error "Unknown byte order"
#endif

enum {
	PROP_0,
	PROP_MIN_FRAMES
};

GST_DEBUG_CATEGORY_STATIC(gst_rtp_sbc_pay_debug);
#define GST_CAT_DEFAULT gst_rtp_sbc_pay_debug

GST_BOILERPLATE(GstRtpSBCPay, gst_rtp_sbc_pay, GstBaseRTPPayload,
		GST_TYPE_BASE_RTP_PAYLOAD);

static const GstElementDetails gst_rtp_sbc_pay_details =
	GST_ELEMENT_DETAILS("RTP packet payloader",
				"Codec/Payloader/Network",
				"Payload SBC audio as RTP packets",
				"Thiago Sousa Santos "
				"<thiagoss@lcc.ufcg.edu.br>");

static GstStaticPadTemplate gst_rtp_sbc_pay_sink_factory =
	GST_STATIC_PAD_TEMPLATE("sink", GST_PAD_SINK, GST_PAD_ALWAYS,
		GST_STATIC_CAPS("audio/x-sbc, "
				"rate = (int) { 16000, 32000, 44100, 48000 }, "
				"channels = (int) [ 1, 2 ], "
				"mode = (string) { \"mono\", \"dual\", \"stereo\", \"joint\" }, "
				"blocks = (int) { 4, 8, 12, 16 }, "
				"subbands = (int) { 4, 8 }, "
				"allocation = (string) { \"snr\", \"loudness\" }, "
				"bitpool = (int) [ 2, 64 ]")
	);

static GstStaticPadTemplate gst_rtp_sbc_pay_src_factory =
	GST_STATIC_PAD_TEMPLATE("src", GST_PAD_SRC, GST_PAD_ALWAYS,
		GST_STATIC_CAPS(
			"application/x-rtp, "
			"media = (string) \"audio\","
			"payload = (int) " GST_RTP_PAYLOAD_DYNAMIC_STRING ", "
			"clock-rate = (int) { 16000, 32000, 44100, 48000 },"
			"encoding-name = (string) \"SBC\"")
	);

static void gst_rtp_sbc_pay_set_property(GObject *object, guint prop_id,
				const GValue *value, GParamSpec *pspec);
static void gst_rtp_sbc_pay_get_property(GObject *object, guint prop_id,
				GValue *value, GParamSpec *pspec);

static gint gst_rtp_sbc_pay_get_frame_len(gint subbands, gint channels,
		gint blocks, gint bitpool, const gchar *channel_mode)
{
	gint len;
	gint join;

	len = 4 + (4 * subbands * channels)/8;

	if (strcmp(channel_mode, "mono") == 0 ||
		strcmp(channel_mode, "dual") == 0)
		len += ((blocks * channels * bitpool) + 7) / 8;
	else {
		join = strcmp(channel_mode, "joint") == 0 ? 1 : 0;
		len += ((join * subbands + blocks * bitpool) + 7) / 8;
	}

	return len;
}

static gboolean gst_rtp_sbc_pay_set_caps(GstBaseRTPPayload *payload,
			GstCaps *caps)
{
	GstRtpSBCPay *sbcpay;
	gint rate, subbands, channels, blocks, bitpool;
	gint frame_len;
	const gchar *channel_mode;
	GstStructure *structure;

	sbcpay = GST_RTP_SBC_PAY(payload);

	structure = gst_caps_get_structure(caps, 0);
	if (!gst_structure_get_int(structure, "rate", &rate))
		return FALSE;
	if (!gst_structure_get_int(structure, "channels", &channels))
		return FALSE;
	if (!gst_structure_get_int(structure, "blocks", &blocks))
		return FALSE;
	if (!gst_structure_get_int(structure, "bitpool", &bitpool))
		return FALSE;
	if (!gst_structure_get_int(structure, "subbands", &subbands))
		return FALSE;

	channel_mode = gst_structure_get_string(structure, "mode");
	if (!channel_mode)
		return FALSE;

	frame_len = gst_rtp_sbc_pay_get_frame_len(subbands, channels, blocks,
				bitpool, channel_mode);

	sbcpay->frame_length = frame_len;

	gst_basertppayload_set_options(payload, "audio", TRUE, "SBC", rate);

	GST_DEBUG_OBJECT(payload, "calculated frame length: %d ", frame_len);

	return gst_basertppayload_set_outcaps(payload, NULL);
}

static GstFlowReturn gst_rtp_sbc_pay_flush_buffers(GstRtpSBCPay *sbcpay)
{
	guint available;
	guint max_payload;
	GstBuffer *outbuf;
	guint8 *payload_data;
	guint frame_count;
	guint payload_length;
	struct rtp_payload *payload;

	if (sbcpay->frame_length == 0) {
		GST_ERROR_OBJECT(sbcpay, "Frame length is 0");
		return GST_FLOW_ERROR;
	}

	available = gst_adapter_available(sbcpay->adapter);

	max_payload = gst_rtp_buffer_calc_payload_len(
		GST_BASE_RTP_PAYLOAD_MTU(sbcpay) - RTP_SBC_PAYLOAD_HEADER_SIZE,
		0, 0);

	max_payload = MIN(max_payload, available);
	frame_count = max_payload / sbcpay->frame_length;
	payload_length = frame_count * sbcpay->frame_length;
	if (payload_length == 0) /* Nothing to send */
		return GST_FLOW_OK;

	outbuf = gst_rtp_buffer_new_allocate(payload_length +
			RTP_SBC_PAYLOAD_HEADER_SIZE, 0, 0);

	gst_rtp_buffer_set_payload_type(outbuf,
			GST_BASE_RTP_PAYLOAD_PT(sbcpay));

	payload_data = gst_rtp_buffer_get_payload(outbuf);
	payload = (struct rtp_payload *) payload_data;
	memset(payload, 0, sizeof(struct rtp_payload));
	payload->frame_count = frame_count;

	gst_adapter_copy(sbcpay->adapter, payload_data +
			RTP_SBC_PAYLOAD_HEADER_SIZE, 0, payload_length);
	gst_adapter_flush(sbcpay->adapter, payload_length);

	GST_BUFFER_TIMESTAMP(outbuf) = sbcpay->timestamp;
	GST_DEBUG_OBJECT(sbcpay, "Pushing %d bytes", payload_length);

	return gst_basertppayload_push(GST_BASE_RTP_PAYLOAD(sbcpay), outbuf);
}

static GstFlowReturn gst_rtp_sbc_pay_handle_buffer(GstBaseRTPPayload *payload,
			GstBuffer *buffer)
{
	GstRtpSBCPay *sbcpay;
	guint available;

	/* FIXME check for negotiation */

	sbcpay = GST_RTP_SBC_PAY(payload);
	sbcpay->timestamp = GST_BUFFER_TIMESTAMP(buffer);

	gst_adapter_push(sbcpay->adapter, buffer);

	available = gst_adapter_available(sbcpay->adapter);
	if (available + RTP_SBC_HEADER_TOTAL >=
				GST_BASE_RTP_PAYLOAD_MTU(sbcpay) ||
			(available >
				(sbcpay->min_frames * sbcpay->frame_length)))
		return gst_rtp_sbc_pay_flush_buffers(sbcpay);

	return GST_FLOW_OK;
}

static gboolean gst_rtp_sbc_pay_handle_event(GstPad *pad,
				GstEvent *event)
{
	GstRtpSBCPay *sbcpay = GST_RTP_SBC_PAY(GST_PAD_PARENT(pad));

	switch (GST_EVENT_TYPE(event)) {
	case GST_EVENT_EOS:
		gst_rtp_sbc_pay_flush_buffers(sbcpay);
		break;
	default:
		break;
	}

	return FALSE;
}

static void gst_rtp_sbc_pay_base_init(gpointer g_class)
{
	GstElementClass *element_class = GST_ELEMENT_CLASS(g_class);

	gst_element_class_add_pad_template(element_class,
		gst_static_pad_template_get(&gst_rtp_sbc_pay_sink_factory));
	gst_element_class_add_pad_template(element_class,
		gst_static_pad_template_get(&gst_rtp_sbc_pay_src_factory));

	gst_element_class_set_details(element_class, &gst_rtp_sbc_pay_details);
}

static void gst_rtp_sbc_pay_finalize(GObject *object)
{
	GstRtpSBCPay *sbcpay = GST_RTP_SBC_PAY(object);
	g_object_unref(sbcpay->adapter);

	GST_CALL_PARENT(G_OBJECT_CLASS, finalize, (object));
}

static void gst_rtp_sbc_pay_class_init(GstRtpSBCPayClass *klass)
{
	GObjectClass *gobject_class;
	GstBaseRTPPayloadClass *payload_class =
		GST_BASE_RTP_PAYLOAD_CLASS(klass);

	gobject_class = G_OBJECT_CLASS(klass);
	parent_class = g_type_class_peek_parent(klass);

	gobject_class->finalize = GST_DEBUG_FUNCPTR(gst_rtp_sbc_pay_finalize);
	gobject_class->set_property = GST_DEBUG_FUNCPTR(
			gst_rtp_sbc_pay_set_property);
	gobject_class->get_property = GST_DEBUG_FUNCPTR(
			gst_rtp_sbc_pay_get_property);

	payload_class->set_caps = GST_DEBUG_FUNCPTR(gst_rtp_sbc_pay_set_caps);
	payload_class->handle_buffer = GST_DEBUG_FUNCPTR(
			gst_rtp_sbc_pay_handle_buffer);
	payload_class->handle_event = GST_DEBUG_FUNCPTR(
			gst_rtp_sbc_pay_handle_event);

	/* properties */
	g_object_class_install_property(G_OBJECT_CLASS(klass),
		PROP_MIN_FRAMES,
		g_param_spec_int("min-frames", "minimum frame number",
		"Minimum quantity of frames to send in one packet "
		"(-1 for maximum allowed by the mtu)",
		-1, G_MAXINT, DEFAULT_MIN_FRAMES, G_PARAM_READWRITE));

	GST_DEBUG_CATEGORY_INIT(gst_rtp_sbc_pay_debug, "rtpsbcpay", 0,
				"RTP SBC payloader");
}

static void gst_rtp_sbc_pay_set_property(GObject *object, guint prop_id,
					const GValue *value, GParamSpec *pspec)
{
	GstRtpSBCPay *sbcpay;

	sbcpay = GST_RTP_SBC_PAY(object);

	switch (prop_id) {
	case PROP_MIN_FRAMES:
		sbcpay->min_frames = g_value_get_int(value);
		break;
	default:
		G_OBJECT_WARN_INVALID_PROPERTY_ID(object, prop_id, pspec);
	break;
	}
}

static void gst_rtp_sbc_pay_get_property(GObject *object, guint prop_id,
					GValue *value, GParamSpec *pspec)
{
	GstRtpSBCPay *sbcpay;

	sbcpay = GST_RTP_SBC_PAY(object);

	switch (prop_id) {
	case PROP_MIN_FRAMES:
		g_value_set_int(value, sbcpay->min_frames);
		break;
	default:
		G_OBJECT_WARN_INVALID_PROPERTY_ID(object, prop_id, pspec);
	break;
	}
}

static void gst_rtp_sbc_pay_init(GstRtpSBCPay *self, GstRtpSBCPayClass *klass)
{
	self->adapter = gst_adapter_new();
	self->frame_length = 0;
	self->timestamp = 0;

	self->min_frames = DEFAULT_MIN_FRAMES;
}

gboolean gst_rtp_sbc_pay_plugin_init(GstPlugin *plugin)
{
	return gst_element_register(plugin, "rtpsbcpay", GST_RANK_NONE,
							GST_TYPE_RTP_SBC_PAY);
}