1 /***
2 This file is part of PulseAudio.
3
4 Copyright 2019 Pali Rohár <pali.rohar@gmail.com>
5
6 PulseAudio is free software; you can redistribute it and/or modify
7 it under the terms of the GNU Lesser General Public License as
8 published by the Free Software Foundation; either version 2.1 of the
9 License, or (at your option) any later version.
10
11 PulseAudio is distributed in the hope that it will be useful, but
12 WITHOUT ANY WARRANTY; without even the implied warranty of
13 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
14 General Public License for more details.
15
16 You should have received a copy of the GNU Lesser General Public
17 License along with PulseAudio; if not, see <http://www.gnu.org/licenses/>.
18 ***/
19
20 #ifdef HAVE_CONFIG_H
21 #include <config.h>
22 #endif
23
24 #include <pulsecore/core.h>
25 #include <pulsecore/core-util.h>
26 #if defined(HAVE_GSTAPTX) || defined(HAVE_GSTLDAC)
27 #include <gst/gst.h>
28 #endif
29
30 #include "a2dp-codec-util.h"
31
32 extern const pa_bt_codec pa_bt_codec_msbc;
33 extern const pa_bt_codec pa_bt_codec_cvsd;
34
35 /* List of HSP/HFP codecs.
36 */
37 static const pa_bt_codec *pa_hf_codecs[] = {
38 &pa_bt_codec_cvsd,
39 &pa_bt_codec_msbc,
40 };
41
42 extern const pa_a2dp_endpoint_conf pa_a2dp_endpoint_conf_sbc;
43 extern const pa_a2dp_endpoint_conf pa_a2dp_endpoint_conf_sbc_xq_453;
44 extern const pa_a2dp_endpoint_conf pa_a2dp_endpoint_conf_sbc_xq_512;
45 extern const pa_a2dp_endpoint_conf pa_a2dp_endpoint_conf_sbc_xq_552;
46 #ifdef HAVE_GSTAPTX
47 extern const pa_a2dp_endpoint_conf pa_a2dp_endpoint_conf_aptx;
48 extern const pa_a2dp_endpoint_conf pa_a2dp_endpoint_conf_aptx_hd;
49 #endif
50 #ifdef HAVE_GSTLDAC
51 extern const pa_a2dp_endpoint_conf pa_a2dp_endpoint_conf_ldac_eqmid_hq;
52 extern const pa_a2dp_endpoint_conf pa_a2dp_endpoint_conf_ldac_eqmid_sq;
53 extern const pa_a2dp_endpoint_conf pa_a2dp_endpoint_conf_ldac_eqmid_mq;
54 #endif
55 extern const pa_a2dp_endpoint_conf pa_a2dp_endpoint_conf_faststream;
56
57 /* This is list of supported codecs. Their order is important.
58 * Codec with lower index has higher priority. */
59 static const pa_a2dp_endpoint_conf *pa_a2dp_endpoint_configurations[] = {
60 #ifdef HAVE_GSTLDAC
61 &pa_a2dp_endpoint_conf_ldac_eqmid_hq,
62 &pa_a2dp_endpoint_conf_ldac_eqmid_sq,
63 &pa_a2dp_endpoint_conf_ldac_eqmid_mq,
64 #endif
65 #ifdef HAVE_GSTAPTX
66 &pa_a2dp_endpoint_conf_aptx_hd,
67 &pa_a2dp_endpoint_conf_aptx,
68 #endif
69 &pa_a2dp_endpoint_conf_sbc,
70 &pa_a2dp_endpoint_conf_sbc_xq_453,
71 &pa_a2dp_endpoint_conf_sbc_xq_512,
72 &pa_a2dp_endpoint_conf_sbc_xq_552,
73 &pa_a2dp_endpoint_conf_faststream,
74 };
75
pa_bluetooth_a2dp_endpoint_conf_count(void)76 unsigned int pa_bluetooth_a2dp_endpoint_conf_count(void) {
77 return PA_ELEMENTSOF(pa_a2dp_endpoint_configurations);
78 }
79
pa_bluetooth_a2dp_endpoint_conf_iter(unsigned int i)80 const pa_a2dp_endpoint_conf *pa_bluetooth_a2dp_endpoint_conf_iter(unsigned int i) {
81 pa_assert(i < pa_bluetooth_a2dp_endpoint_conf_count());
82 return pa_a2dp_endpoint_configurations[i];
83 }
84
pa_bluetooth_hf_codec_count(void)85 unsigned int pa_bluetooth_hf_codec_count(void) {
86 return PA_ELEMENTSOF(pa_hf_codecs);
87 }
88
pa_bluetooth_hf_codec_iter(unsigned int i)89 const pa_bt_codec *pa_bluetooth_hf_codec_iter(unsigned int i) {
90 pa_assert(i < pa_bluetooth_hf_codec_count());
91 return pa_hf_codecs[i];
92 }
93
pa_bluetooth_get_hf_codec(const char * name)94 const pa_bt_codec *pa_bluetooth_get_hf_codec(const char *name) {
95 unsigned int i;
96
97 for (i = 0; i < PA_ELEMENTSOF(pa_hf_codecs); ++i) {
98 if (pa_streq(pa_hf_codecs[i]->name, name))
99 return pa_hf_codecs[i];
100 }
101
102 return NULL;
103 }
104
pa_bluetooth_get_a2dp_endpoint_conf(const char * name)105 const pa_a2dp_endpoint_conf *pa_bluetooth_get_a2dp_endpoint_conf(const char *name) {
106 unsigned int i;
107 unsigned int count = pa_bluetooth_a2dp_endpoint_conf_count();
108
109 for (i = 0; i < count; i++) {
110 if (pa_streq(pa_a2dp_endpoint_configurations[i]->bt_codec.name, name))
111 return pa_a2dp_endpoint_configurations[i];
112 }
113
114 return NULL;
115 }
116
pa_bluetooth_a2dp_codec_gst_init(void)117 void pa_bluetooth_a2dp_codec_gst_init(void) {
118 #if defined(HAVE_GSTAPTX) || defined(HAVE_GSTLDAC)
119 GError *error = NULL;
120
121 if (!gst_init_check(NULL, NULL, &error)) {
122 pa_log_error("Could not initialise GStreamer: %s", error->message);
123 g_error_free(error);
124 return;
125 }
126 pa_log_info("GStreamer initialisation done");
127 #endif
128 }
129
pa_bluetooth_a2dp_codec_is_available(const pa_a2dp_codec_id * id,bool is_a2dp_sink)130 bool pa_bluetooth_a2dp_codec_is_available(const pa_a2dp_codec_id *id, bool is_a2dp_sink) {
131 unsigned int i;
132 unsigned int count = pa_bluetooth_a2dp_endpoint_conf_count();
133 const pa_a2dp_endpoint_conf *conf;
134
135 for (i = 0; i < count; i++) {
136 conf = pa_bluetooth_a2dp_endpoint_conf_iter(i);
137 if (memcmp(id, &conf->id, sizeof(pa_a2dp_codec_id)) == 0
138 && conf->can_be_supported(is_a2dp_sink))
139 return true;
140 }
141
142 return false;
143 }
144