-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathmsaaudio.cpp
244 lines (207 loc) · 9.57 KB
/
msaaudio.cpp
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
/*
* Copyright (c) 2010-2019 Belledonne Communications SARL.
*
* msaaudio.cpp - Android Media plugin for Linphone, based on AAudio APIs.
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* This program 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 General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program. If not, see <http://www.gnu.org/licenses/>.
*/
#include <mediastreamer2/msjava.h>
#include <mediastreamer2/devices.h>
#include <sys/types.h>
#include <jni.h>
#include <dlfcn.h>
#include <msaaudio/msaaudio.h>
static void android_snd_card_device_create(const AndroidSoundUtils *soundUtils, jobject deviceInfo, const char *deviceName, MSSndCardDeviceType type, SoundDeviceDescription *deviceDescription, MSSndCardManager *m);
static void android_snd_card_detect(MSSndCardManager *m) {
JNIEnv *env = ms_get_jni_env();
AndroidSoundUtils *soundUtils = ms_factory_get_android_sound_utils(m->factory);
// Get all devices
jobject devices = ms_android_sound_utils_get_devices(soundUtils, "all");
// extract required information from every device
jobjectArray deviceArray = (jobjectArray) devices;
jsize deviceNumber = (int) env->GetArrayLength(deviceArray);
ms_message("[AAudio] Create soundcards for [%0d] devices", deviceNumber);
MSDevicesInfo *devicesInfo = ms_factory_get_devices_info(m->factory);
SoundDeviceDescription *deviceDescription = ms_devices_info_get_sound_device_description(devicesInfo);
char *device_name = nullptr;
char *a2dp_device_name = nullptr;
for (int idx=0; idx < deviceNumber; idx++) {
jobject deviceInfo = env->GetObjectArrayElement(deviceArray, idx);
char *deviceName = ms_android_sound_utils_get_device_product_name(soundUtils, deviceInfo);
MSSndCardDeviceType type = ms_android_sound_utils_get_device_type(soundUtils, deviceInfo);
android_snd_card_device_create(soundUtils, deviceInfo, deviceName, type, deviceDescription, m);
if (device_name == nullptr &&
(type == MSSndCardDeviceType::MS_SND_CARD_DEVICE_TYPE_EARPIECE
|| type == MSSndCardDeviceType::MS_SND_CARD_DEVICE_TYPE_SPEAKER)) {
device_name = ms_strdup(deviceName);
} else if (a2dp_device_name == nullptr &&
type == MSSndCardDeviceType::MS_SND_CARD_DEVICE_TYPE_BLUETOOTH_A2DP) {
a2dp_device_name = ms_strdup(deviceName);
}
ms_free(deviceName);
}
// Detect scenario where bluetooth SCO device name is not detected but A2DP is and rename the sound card using it
if (device_name != nullptr && a2dp_device_name != nullptr) {
const bctbx_list_t *cards = ms_snd_card_manager_get_list(m);
bctbx_list_t *elem;
for (elem = (bctbx_list_t *)cards; elem != NULL; elem = elem->next) {
MSSndCard *card = (MSSndCard *)elem->data;
if (card != nullptr && ms_snd_card_get_device_type(card) == MSSndCardDeviceType::MS_SND_CARD_DEVICE_TYPE_BLUETOOTH && strcmp(ms_snd_card_get_name(card), device_name) == 0) {
ms_warning("[AAudio] It seems bluetooth SCO device's name [%s] is the same as this device, updating it using A2DP device name [%s]", device_name, a2dp_device_name);
ms_snd_card_set_name(card, a2dp_device_name);
}
}
}
if (device_name != nullptr) {
ms_free(device_name);
}
if (a2dp_device_name != nullptr) {
ms_free(a2dp_device_name);
}
}
static void android_native_snd_card_init(MSSndCard *card) {
AAudioContext* context = new AAudioContext();
card->data = context;
}
static void android_native_snd_card_uninit(MSSndCard *card) {
AAudioContext *ctx = (AAudioContext*)card->data;
ms_message("[AAudio] Deletion of AAudio context [%p]", ctx);
delete ctx;
}
MSSndCardDesc android_native_snd_aaudio_card_desc = {
"AAudio",
android_snd_card_detect,
android_native_snd_card_init,
NULL,
NULL,
NULL,
NULL,
NULL,
android_snd_card_create_reader,
android_snd_card_create_writer,
android_native_snd_card_uninit
};
static void android_snd_card_device_create(const AndroidSoundUtils *soundUtils, jobject deviceInfo, const char *deviceName, MSSndCardDeviceType type, SoundDeviceDescription *deviceDescription, MSSndCardManager *m) {
if (
(type == MSSndCardDeviceType::MS_SND_CARD_DEVICE_TYPE_BLUETOOTH) ||
(type == MSSndCardDeviceType::MS_SND_CARD_DEVICE_TYPE_EARPIECE) ||
(type == MSSndCardDeviceType::MS_SND_CARD_DEVICE_TYPE_SPEAKER) ||
(type == MSSndCardDeviceType::MS_SND_CARD_DEVICE_TYPE_MICROPHONE) ||
(type == MSSndCardDeviceType::MS_SND_CARD_DEVICE_TYPE_HEADSET) ||
(type == MSSndCardDeviceType::MS_SND_CARD_DEVICE_TYPE_HEADPHONES) ||
(type == MSSndCardDeviceType::MS_SND_CARD_DEVICE_TYPE_HEARING_AID) ||
(type == MSSndCardDeviceType::MS_SND_CARD_DEVICE_TYPE_GENERIC_USB)
) {
MSSndCard *card = ms_snd_card_new(&android_native_snd_aaudio_card_desc);
card = ms_snd_card_ref(card);
ms_snd_card_set_name(card, deviceName);
card->internal_id = ms_android_sound_utils_get_device_id(soundUtils, deviceInfo);
card->device_type = type;
card->device_description = deviceDescription;
AAudioContext *card_data = (AAudioContext*)card->data;
// Card capabilities
// Assign the value because the default value is MS_SND_CARD_CAP_CAPTURE|MS_SND_CARD_CAP_PLAYBACK
card->capabilities = ms_android_sound_utils_get_device_capabilities(soundUtils, deviceInfo);
if (deviceDescription->flags & DEVICE_HAS_CRAPPY_AAUDIO) {
ms_warning("[AAudio] Device has been dynamically blacklisted using DEVICE_HAS_CRAPPY_AAUDIO flag");
ms_snd_card_unref(card);
return;
}
bool isBottom = false;
if ((card->capabilities & MS_SND_CARD_CAP_CAPTURE) == MS_SND_CARD_CAP_CAPTURE) {
char *address = ms_android_sound_utils_get_microphone_device_address(soundUtils, deviceInfo);
if (address != NULL && strcmp(address, "bottom") == 0) {
ms_message("[AAudio] Microphone device has [%s] address", address);
isBottom = true;
} else {
ms_message("[AAudio] Microphone device have [%s] address, assuming not bottom (back)", address);
}
if (deviceDescription->flags & DEVICE_HAS_BUILTIN_AEC) {
card->capabilities |= MS_SND_CARD_CAP_BUILTIN_ECHO_CANCELLER;
card_data->builtin_aec = true;
}
if (address != NULL) {
ms_free(address);
}
}
card->latency = deviceDescription->delay;
// Take capabilities into account as the same device type may have different components with different capabilities and IDs
if (!ms_snd_card_is_card_duplicate(m, card, TRUE)) {
ms_snd_card_manager_prepend_card(m, card);
ms_message("[AAudio] Added card with ID [%s], name [%s], device ID [%0d], type [%s] and capabilities [%0d]", card->id, card->name, card->internal_id, ms_snd_card_device_type_to_string(card->device_type), card->capabilities);
} else {
if (ms_snd_card_get_device_type(card) == MSSndCardDeviceType::MS_SND_CARD_DEVICE_TYPE_MICROPHONE) {
MSSndCard *duplicate = ms_snd_card_get_card_duplicate(m, card, TRUE);
if (duplicate) {
if (isBottom) {
ms_warning("[AAudio] Back microphone already added with device ID [%0d], removing it and adding bottom microphone ID [%0d] instead with back ID as alternative", duplicate->internal_id, card->internal_id);
card->alternative_id = duplicate->internal_id;
ms_snd_card_manager_prepend_card(m, card);
ms_snd_card_manager_remove_card(m, duplicate);
} else {
ms_message("[AAudio] Bottom microphone already added with device ID [%0d], storing back microphone ID [%0d] as alternative in it", duplicate->internal_id, card->internal_id);
duplicate->alternative_id = card->internal_id;
}
ms_snd_card_unref(duplicate);
}
} else {
ms_message("[AAudio] Card with ID [%s], name [%s], device ID [%0d], type [%s] and capabilities [%0d] not added, considered as duplicate", card->id, card->name, card->internal_id, ms_snd_card_device_type_to_string(card->device_type), card->capabilities);
}
}
ms_snd_card_unref(card);
} else {
ms_message("[AAudio] SKipped device with type [%s]", ms_snd_card_device_type_to_string(type));
}
}
static bool loadLib() {
void *handle;
const char * libname = "libaaudio.so";
bool success = false;
// open library
if ((handle = dlopen(libname, RTLD_NOW)) == NULL){
ms_warning("[AAudio] Fail to load %s : %s", libname, dlerror());
success = false;
} else {
dlerror(); // Clear previous message if present
ms_message("[AAudio] %s successfully loaded", libname);
success = true;
}
return success;
}
#ifdef _MSC_VER
#define MS_PLUGIN_DECLARE(type) extern "C" __declspec(dllexport) type
#else
#define MS_PLUGIN_DECLARE(type) extern "C" type
#endif
// Called by ms_load_plugins in ms_common.c
MS_PLUGIN_DECLARE(void) libmsaaudio_init(MSFactory* factory) {
register_aaudio_player(factory);
register_aaudio_recorder(factory);
ms_message("[AAudio] libmsaaudio plugin loaded");
const bool loadOk = loadLib();
if (loadOk) {
MSDevicesInfo* devices = ms_factory_get_devices_info(factory);
SoundDeviceDescription* d = ms_devices_info_get_sound_device_description(devices);
if (d->flags & DEVICE_HAS_CRAPPY_AAUDIO) {
ms_error("[AAudio] Device is blacklisted, do not create AAudio soundcard");
return;
}
MSSndCardManager *m = ms_factory_get_snd_card_manager(factory);
// Register card manager so that it can be automaticallty initialized
ms_snd_card_manager_register_desc(m, &android_native_snd_aaudio_card_desc);
ms_message("[AAudio] Soundcard created");
} else {
ms_error("[AAudio] Unable to load AAudio plugin shared object");
}
}