summaryrefslogtreecommitdiffstats
path: root/audio/gsta2dpsink.c
blob: 8a6a44f19a4990a490eaa555fedf0222e2390420 (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
/*
 *
 *  BlueZ - Bluetooth protocol stack for Linux
 *
 *  Copyright (C) 2004-2007  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 <unistd.h>
#include <sys/un.h>
#include <sys/socket.h>

#include "ipc.h"

#include "gsta2dpsink.h"

GST_DEBUG_CATEGORY_STATIC(a2dp_sink_debug);
#define GST_CAT_DEFAULT a2dp_sink_debug

#define DEFAULT_DEVICE "default"

enum {
	PROP_0,
	PROP_DEVICE,
};

GST_BOILERPLATE(GstA2dpSink, gst_a2dp_sink, GstAudioSink, GST_TYPE_AUDIO_SINK);

static const GstElementDetails a2dp_sink_details =
	GST_ELEMENT_DETAILS("Bluetooth A2DP sink",
				"Sink/Audio",
				"Plays audio to an A2DP device",
				"Marcel Holtmann <marcel@holtmann.org>");

static GstStaticPadTemplate a2dp_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 }; "

				"audio/mpeg, "
				"mpegversion = (int) 1, "
				"layer = (int) [ 1, 3 ], "
				"rate = (int) { 16000, 22050, 24000, 32000, 44100, 48000 }, "
				"channels = (int) [ 1, 2 ]"));

static void gst_a2dp_sink_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(&a2dp_sink_factory));

	gst_element_class_set_details(element_class, &a2dp_sink_details);
}

static void gst_a2dp_sink_finalize(GObject *object)
{
	GstA2dpSink *sink = GST_A2DP_SINK(object);

	g_io_channel_close(sink->server);

	g_free(sink->device);

	G_OBJECT_CLASS(parent_class)->finalize(object);
}

static void gst_a2dp_sink_set_property(GObject *object, guint prop_id,
					const GValue *value, GParamSpec *pspec)
{
	GstA2dpSink *sink = GST_A2DP_SINK(object);

	switch (prop_id) {
	case PROP_DEVICE:
		g_free(sink->device);
		sink->device = g_value_dup_string(value);

		if (sink->device == NULL)
			sink->device = g_strdup(DEFAULT_DEVICE);
		break;

	default:
		G_OBJECT_WARN_INVALID_PROPERTY_ID(object, prop_id, pspec);
		break;
	}
}

static void gst_a2dp_sink_get_property(GObject *object, guint prop_id,
					GValue *value, GParamSpec *pspec)
{
	GstA2dpSink *sink = GST_A2DP_SINK(object);

	switch (prop_id) {
	case PROP_DEVICE:
		g_value_set_string(value, sink->device);
		break;

	default:
		G_OBJECT_WARN_INVALID_PROPERTY_ID(object, prop_id, pspec);
		break;
	}
}

static gboolean gst_a2dp_sink_open(GstAudioSink *self)
{
	GstA2dpSink *sink = GST_A2DP_SINK(self);

	printf("device %s\n", sink->device);
	printf("open\n");

	return TRUE;
}

static gboolean gst_a2dp_sink_prepare(GstAudioSink *self,
						GstRingBufferSpec *spec)
{
	printf("perpare\n");
	printf("rate %d\n", spec->rate);
	printf("channels %d\n", spec->channels);

	return TRUE;
}

static gboolean gst_a2dp_sink_unprepare(GstAudioSink *self)
{
	printf("unprepare\n");

	return TRUE;
}

static gboolean gst_a2dp_sink_close(GstAudioSink *self)
{
	printf("close\n");

	return TRUE;
}

static guint gst_a2dp_sink_write(GstAudioSink *self,
					gpointer data, guint length)
{
	return 0;
}

static guint gst_a2dp_sink_delay(GstAudioSink *audiosink)
{
	printf("delay\n");

	return 0;
}

static void gst_a2dp_sink_reset(GstAudioSink *audiosink)
{
	printf("reset\n");
}

static gboolean server_callback(GIOChannel *chan,
					GIOCondition cond, gpointer data)
{
	printf("callback\n");

	return TRUE;
}

static void gst_a2dp_sink_class_init(GstA2dpSinkClass *klass)
{
	GObjectClass *object_class = G_OBJECT_CLASS(klass);
	GstAudioSinkClass *audiosink_class = GST_AUDIO_SINK_CLASS(klass);

	parent_class = g_type_class_peek_parent(klass);

	object_class->finalize = GST_DEBUG_FUNCPTR(gst_a2dp_sink_finalize);
	object_class->set_property = GST_DEBUG_FUNCPTR(gst_a2dp_sink_set_property);
	object_class->get_property = GST_DEBUG_FUNCPTR(gst_a2dp_sink_get_property);

	audiosink_class->open = GST_DEBUG_FUNCPTR(gst_a2dp_sink_open);
	audiosink_class->prepare = GST_DEBUG_FUNCPTR(gst_a2dp_sink_prepare);
	audiosink_class->unprepare = GST_DEBUG_FUNCPTR(gst_a2dp_sink_unprepare);
	audiosink_class->close = GST_DEBUG_FUNCPTR(gst_a2dp_sink_close);
	audiosink_class->write = GST_DEBUG_FUNCPTR(gst_a2dp_sink_write);
	audiosink_class->delay = GST_DEBUG_FUNCPTR(gst_a2dp_sink_delay);
	audiosink_class->reset = GST_DEBUG_FUNCPTR(gst_a2dp_sink_reset);

	g_object_class_install_property(object_class, PROP_DEVICE,
				g_param_spec_string("device", "Device",
					"Bluetooth remote device",
					DEFAULT_DEVICE, G_PARAM_READWRITE));

	GST_DEBUG_CATEGORY_INIT(a2dp_sink_debug, "a2dpsink", 0,
						"A2DP sink element");
}

static void gst_a2dp_sink_init(GstA2dpSink *self, GstA2dpSinkClass *klass)
{
	struct sockaddr_un addr = { AF_UNIX, IPC_SOCKET_NAME };
	int sk;

	self->device = g_strdup(DEFAULT_DEVICE);

	sk = socket(PF_LOCAL, SOCK_STREAM, 0);
	if (sk < 0)
		return;

	if (connect(sk, (struct sockaddr *) &addr, sizeof(addr)) < 0) {
		close(sk);
		return;
	}

	self->server = g_io_channel_unix_new(sk);

	g_io_add_watch(self->server, G_IO_IN | G_IO_HUP | G_IO_ERR | G_IO_NVAL,
							server_callback, self);

	g_io_channel_unref(self->server);
}