summaryrefslogtreecommitdiffstats
path: root/spa/plugins/bluez5/codec-loader.c
blob: f8363f8880a76e6d69589fa40135831146cfca19 (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
/* Spa A2DP codec API
 *
 * Copyright © 2020 Wim Taymans
 *
 * Permission is hereby granted, free of charge, to any person obtaining a
 * copy of this software and associated documentation files (the "Software"),
 * to deal in the Software without restriction, including without limitation
 * the rights to use, copy, modify, merge, publish, distribute, sublicense,
 * and/or sell copies of the Software, and to permit persons to whom the
 * Software is furnished to do so, subject to the following conditions:
 *
 * The above copyright notice and this permission notice (including the next
 * paragraph) shall be included in all copies or substantial portions of the
 * Software.
 *
 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.  IN NO EVENT SHALL
 * THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
 * FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
 * DEALINGS IN THE SOFTWARE.
 */

#include "config.h"

#include <spa/utils/string.h>

#include "defs.h"
#include "codec-loader.h"

#define MEDIA_CODEC_LIB_BASE	"bluez5/libspa-codec-bluez5-"

/* AVDTP allows 0x3E endpoints, can't have more codecs than that */
#define MAX_CODECS	0x3E
#define MAX_HANDLES	MAX_CODECS

static struct spa_log_topic log_topic = SPA_LOG_TOPIC(0, "spa.bluez5.codecs");
#undef SPA_LOG_TOPIC_DEFAULT
#define SPA_LOG_TOPIC_DEFAULT &log_topic

struct impl {
	const struct media_codec *codecs[MAX_CODECS + 1];
	struct spa_handle *handles[MAX_HANDLES];
	size_t n_codecs;
	size_t n_handles;
	struct spa_plugin_loader *loader;
	struct spa_log *log;
};

static int codec_order(const struct media_codec *c)
{
	static const enum spa_bluetooth_audio_codec order[] = {
		SPA_BLUETOOTH_AUDIO_CODEC_LC3,
		SPA_BLUETOOTH_AUDIO_CODEC_LDAC,
		SPA_BLUETOOTH_AUDIO_CODEC_APTX_HD,
		SPA_BLUETOOTH_AUDIO_CODEC_APTX,
		SPA_BLUETOOTH_AUDIO_CODEC_AAC,
		SPA_BLUETOOTH_AUDIO_CODEC_LC3PLUS_HR,
		SPA_BLUETOOTH_AUDIO_CODEC_MPEG,
		SPA_BLUETOOTH_AUDIO_CODEC_SBC,
		SPA_BLUETOOTH_AUDIO_CODEC_SBC_XQ,
		SPA_BLUETOOTH_AUDIO_CODEC_APTX_LL,
		SPA_BLUETOOTH_AUDIO_CODEC_APTX_LL_DUPLEX,
		SPA_BLUETOOTH_AUDIO_CODEC_FASTSTREAM,
		SPA_BLUETOOTH_AUDIO_CODEC_FASTSTREAM_DUPLEX,
		SPA_BLUETOOTH_AUDIO_CODEC_OPUS_05,
		SPA_BLUETOOTH_AUDIO_CODEC_OPUS_05_51,
		SPA_BLUETOOTH_AUDIO_CODEC_OPUS_05_71,
		SPA_BLUETOOTH_AUDIO_CODEC_OPUS_05_DUPLEX,
		SPA_BLUETOOTH_AUDIO_CODEC_OPUS_05_PRO,
	};
	size_t i;
	for (i = 0; i < SPA_N_ELEMENTS(order); ++i)
		if (c->id == order[i])
			return i;
	return SPA_N_ELEMENTS(order);
}

static int codec_order_cmp(const void *a, const void *b)
{
	const struct media_codec * const *ca = a;
	const struct media_codec * const *cb = b;
	int ia = codec_order(*ca);
	int ib = codec_order(*cb);
	if (*ca == *cb)
		return 0;
	return (ia == ib) ? (*ca < *cb ? -1 : 1) : ia - ib;
}

static int load_media_codecs_from(struct impl *impl, const char *factory_name, const char *libname)
{
	struct spa_handle *handle = NULL;
	void *iface;
	const struct spa_bluez5_codec_a2dp *bluez5_codec_a2dp;
	int n_codecs = 0;
	int res;
	size_t i;
	struct spa_dict_item info_items[] = {
		{ SPA_KEY_LIBRARY_NAME, libname },
	};
	struct spa_dict info = SPA_DICT_INIT_ARRAY(info_items);

	handle = spa_plugin_loader_load(impl->loader, factory_name, &info);
	if (handle == NULL) {
		spa_log_info(impl->log, "Bluetooth codec plugin %s not available", factory_name);
		return -ENOENT;
	}

	spa_log_debug(impl->log, "loading codecs from %s", factory_name);

	if ((res = spa_handle_get_interface(handle, SPA_TYPE_INTERFACE_Bluez5CodecMedia, &iface)) < 0) {
		spa_log_warn(impl->log, "Bluetooth codec plugin %s has no codec interface",
				factory_name);
		goto fail;
	}

	bluez5_codec_a2dp = iface;

	if (bluez5_codec_a2dp->iface.version != SPA_VERSION_BLUEZ5_CODEC_MEDIA) {
		spa_log_warn(impl->log, "codec plugin %s has incompatible ABI version (%d != %d)",
				factory_name, bluez5_codec_a2dp->iface.version, SPA_VERSION_BLUEZ5_CODEC_MEDIA);
		res = -ENOENT;
		goto fail;
	}

	for (i = 0; bluez5_codec_a2dp->codecs[i]; ++i) {
		const struct media_codec *c = bluez5_codec_a2dp->codecs[i];
		const char *ep = c->endpoint_name ? c->endpoint_name : c->name;
		size_t j;

		if (!ep)
			goto next_codec;

		if (impl->n_codecs >= MAX_CODECS) {
			spa_log_error(impl->log, "too many A2DP codecs");
			break;
		}

		/* Don't load duplicate endpoints */
		for (j = 0; j < impl->n_codecs; ++j) {
			const struct media_codec *c2 = impl->codecs[j];
			const char *ep2 = c2->endpoint_name ? c2->endpoint_name : c2->name;
			if (spa_streq(ep, ep2) && c->fill_caps && c2->fill_caps) {
				spa_log_debug(impl->log, "media codec %s from %s duplicate endpoint %s",
						c->name, factory_name, ep);
				goto next_codec;
			}
		}

		spa_log_debug(impl->log, "loaded media codec %s from %s, endpoint:%s",
				c->name, factory_name, ep);

		if (c->set_log)
			c->set_log(impl->log);

		impl->codecs[impl->n_codecs++] = c;
		++n_codecs;

	next_codec:
		continue;
	}

	if (n_codecs > 0)
		impl->handles[impl->n_handles++] = handle;
	else
		spa_plugin_loader_unload(impl->loader, handle);

	return 0;

fail:
	if (handle)
		spa_plugin_loader_unload(impl->loader, handle);
	return res;
}

const struct media_codec * const *load_media_codecs(struct spa_plugin_loader *loader, struct spa_log *log)
{
	struct impl *impl;
	bool has_sbc;
	size_t i;
	const struct { const char *factory; const char *lib; } plugins[] = {
#define MEDIA_CODEC_FACTORY_LIB(basename) \
		{ MEDIA_CODEC_FACTORY_NAME(basename), MEDIA_CODEC_LIB_BASE basename }
		MEDIA_CODEC_FACTORY_LIB("aac"),
		MEDIA_CODEC_FACTORY_LIB("aptx"),
		MEDIA_CODEC_FACTORY_LIB("faststream"),
		MEDIA_CODEC_FACTORY_LIB("ldac"),
		MEDIA_CODEC_FACTORY_LIB("sbc"),
		MEDIA_CODEC_FACTORY_LIB("lc3plus"),
		MEDIA_CODEC_FACTORY_LIB("opus"),
		MEDIA_CODEC_FACTORY_LIB("lc3")
#undef MEDIA_CODEC_FACTORY_LIB
	};

	impl = calloc(sizeof(struct impl), 1);
	if (impl == NULL)
		return NULL;

	impl->loader = loader;
	impl->log = log;

	spa_log_topic_init(impl->log, &log_topic);

	for (i = 0; i < SPA_N_ELEMENTS(plugins); ++i)
		load_media_codecs_from(impl, plugins[i].factory, plugins[i].lib);

	has_sbc = false;
	for (i = 0; i < impl->n_codecs; ++i)
		if (impl->codecs[i]->id == SPA_BLUETOOTH_AUDIO_CODEC_SBC)
			has_sbc = true;

	if (!has_sbc) {
		spa_log_error(impl->log, "failed to load A2DP SBC codec from plugins");
		free_media_codecs(impl->codecs);
		errno = ENOENT;
		return NULL;
	}

	qsort(impl->codecs, impl->n_codecs, sizeof(const struct media_codec *), codec_order_cmp);

	return impl->codecs;
}

void free_media_codecs(const struct media_codec * const *media_codecs)
{
	struct impl *impl = SPA_CONTAINER_OF(media_codecs, struct impl, codecs);
	size_t i;

	for (i = 0; i < impl->n_handles; ++i)
		spa_plugin_loader_unload(impl->loader, impl->handles[i]);

	free(impl);
}