Android Record audio from Bluetooth A2DP source - android

I have a custom bluetooth wearable that essentially has two microphones, streams A2DP and handles AVRCP commands. I would like to connect this device to an Android phone and record audio either with AudioRecord or in native code. I need to do some signal processing on the data so I can't have it record directly to a file.
I have managed to build a version of Android using AOSP which will pair with the device and appears to receive the A2DP stream but I have not been able to build a version of the OS/SDK that allows me to use AudioRecord. I have gotten to the point where I can add a source for A2DP but I'm missing something at the native layer to fully make the connection.
Ideally, I would also like to control the input with AVRCP.
I am currently testing with a hammerhead device, but am will to move to another device if that works better.
Any input appreciated.

Related

Multichannel Audio recording in Android

I have a multichannel microphone connected to an android phone (pixel 3) through usb. I'm trying to use MediaRecorder API for recording audio. However, there doesn't seem to be a way to setup multichannel recording with MediaRecorder even though Android USB audio documentation seems to say it allows multichannel input. I know recording multi channel audio is possible, as there are apps that already do that (see ntrack). I'm trying to figure out how to do this and hoping it doesn't involve writing usb driver (like these developers).
If you can see your microphones as separate input devices using audioService.getDevices(AudioManager.GET_DEVICES_INPUTS) then you can use the AAudio API (preferably using Oboe library) to access each of these microphones by opening stream for each.

Android- stream live mic audio from one device to multiple devices via WiFi

I am currently researching on building a live broadcasting module as a part of my app and this is the usecase: One user A will record his voice using the mic and this data has to be streamed to a server while the other devices should be able to receive this audio and play it.All of them will be connected to the same network. I would like to preferably use a server that is not java based. I have currently looked into the solutions provided in the following links:
[1]: Stream Live Android Audio to Server [2]: Streaming voice between Android Phones over WiFi [3]: Android streaming live mic audio between two android devices via wifi
I have also considered some 3rd part libraries but I do not wish to use them since they are expensive.

Audio input through USB in Android

I have been wondering, on how to capture Audio inputs through USB in Android.
My scenario is to receive audio through external hardware and play that received audio through android app. This transmission is to be done over USB.
Is there any way to do this using Android SDK / Android NDK.
Any suggestion will be helpful to me.
Task Done Right by time I am able to interact with Hardware using CDC class and also able to play some random noisy audio through USB in my app. Neither I am able to get clear sound by that approach, nor there is consistency within the transmission of audio.
Thanks.
Regards, Vivek
Most modern Android devices can act as USB host. So you can connect e.g. USB microphone for capturing the audio. Android also contains support for usb_audio class. Use that to get access to the audio on the device.
Since you have already experimented with Communication Device Class (CDC), you are aware of Android's USB host functionality. Now you need to ensure your peripheral has implemented USB audio class (the audio source part) and make your app to use the audio class to obtain the audio. This pretty well explained here, so it does not make sense to copy all the information to this post. If you are already using audio class, that page may explain some of the issues you have (e.g. using wrong format).
USB Audio class specifications can be found at USB.org website. The problem with those is that Audio class is pretty large and Android probably does not support everything.

Streaming audio between external bluetooth device and Android phone

I'm considering building an Android app that allows for streaming audio via bluetooth to and from the Android phone. I've read similar questions on Stackoverflow before, and based on my understanding, A2DP cannot be used as Android can't become an A2DP sink.
In theory, I believe that a profile such as HFP or HFP AG should allow for bidirectional audio streams. In the Android API, is there a way to force a bluetooth pairing to follow this profile, or does the OS decide which profile to use (such as forcing HFP only when there's a phone call going on)?
I've been able to achieve what I wanted using RFCOMM-based SPP. In order to send audio to the external device, I use the AudioRecord API to record PCM-16 audio to stream audio realtime over Bluetooth. In order to receive audio, I spawn a thread that is responsible for outputting audio to the speaker using the AudioTrack API (in streaming mode)

How To Modify Android's Bluetooth Stack to Enable A2dp Sink

I'm working on an audio recorder app that uses a bluetooth mic to record audio on to an Android device (Nexus 7 - rooted Android 4.4.2). It's currently implemented on HFP and everything is working fine. The bluetooth mic is implemented with Bluegiga's WT32 bluetooth module + a mic input, audio quality via HFP isn't great but it's sufficient for now.
However, I'm now trying to change the bluetooth profile to A2dp, since there are two mic inputs (L/R) and WT32 supports A2dp (source). After much research I found that stock Android doesn't support A2dp (sink), and it's possible to modify Android's bluetooth stack to enable A2dp (sink).
What I don't understand is how does one access and modify the bluetooth stack. It would be nice if someone with an answer is able to break-down the steps to achieve this.
I've tried following the answer to this question:
Receive audio via Bluetooth in Android, yet I can't seem to find the appropriate file to modify. Actually, I don't even know if I'm looking into the right folder. I've looked through the devices file via Android-studio's DDMS-File Explorer.
ps, I'm still fairly new with Android app development, so I may have misused some of the terminologies and I apologies in-advance for that.
So the above answer isn't totally correct.
The following is how it breaks down:
HAL, is the hardware abstraction layer that implements the actual Bluetooth state machines in c/cpp code, as such it controls the various state machines for A2dp, HFP, GATT, SPP, AVRCP, etc. services. Each of these services also reference SMP and ATT files for controlling the actual Bluetooth server or client databases, and there security.
HCI, is where the actual work gets done. the HAL doesn't really do anything, it assembles the complex data messages that get sent along a tty serial (either spi, or UART) to an inter-network connected chip on the PCBA via the methods used in the HCI layer, which can be found in the "BTE" layer in /external/bluetooth/bluedroid/ directory of an android compiling trunk from AOSP 4.2.2 to current. - currently there are several manufacturers of these chips but they are mostly all Broadcom based ic's packaged in a dual, or triple radio package that contains a wifi, Bluetooth 4.0 Smart, and Bluetooth 4.0 radio.
It is possible to do what you are trying to do, but you would need to include hardware.so, and bluetooth_jni.so into an NDK/JNI package/project that goes with your apps, and registers via the calls from the .cpp files for each of the Bluetooth services found in "Packages/apps/Bluetooth/jni", you would then handle the registration in your NDK library of 'com_android_bluetooth_a2dp.cpp', and 'com_android_bluetooth_avrcp.cpp', as their appropriately typed objects.
The other issue is, you will need to implement your own custom A2DP stack, as the Android Bluedroid stack only has bit's and piece's of the Sink role implemented in the frameworks while the A2DP role has a full implementation of Source role. Additionally, depending what you actually intend to do with your Bluetooth A2DP sink implementation you will need to implement AVRCP as well - as per the Bluetooth SIG (special interest group), there are inter-connectivity requirements between Bluetooth devices that will lead to major issues if you implement sink role, without AVRCP "remote control target device" and "remote control control device", as the sink role ATT commands from Bluetooth over A2DP (or any Bluetooth service/profile) execute certain handshakes during the service discovery process, when the associated gateway ( the connecting device ), executes a capabilities request the A2DP service is expected to implement i/o capabilities for Start Stop commands, and possibly skip/track advance commands.
Additional to all of this, when implementing A2DP you will need to choose whether you will be handling PCM streams or AAC streams. If you are handling AAC streams (or DRM protected PCM streams for that matter, which anything like Pandora, spotify, etc. uses), you need to implement the SBC Encoder or Decoder appropriate to you implementation, else all you will have is a bunch of encrypted data. Also, be sure to implement the bitrate at the appropriate speed for your devices AudioManager implementation, some phones use 48,000hZ and some us 44,100hZ, this is important if you want high quality audio as generally most PCM A2DP implementations that are utilizing Surround Sound 7.1+ will require 48,000hZ as well as AAC encoding/decoding.
I hope this provides you with some insight.
https://android-review.googlesource.com/#/c/98161/ implements A2DP Sink. It works on Nexus 5. You can try it.
This is actually what I'm trying to do for a long time...
The reason you can't find the configuration file is because Google replaced the Bluetooth stack from BlueZ with a new stack built by Google and Broadcom. The new stack uses a different configuration file, which I don't know how to tinker with.
If you are serious about it, the closest thing I found to start with is the official introduction for the bluetooth framework on Android:
https://source.android.com/devices/bluetooth.html
There are lots of changes happened in Android OS over the time.
As of Android O (Android 8.*), sink profiles are partially supported by Google. Such as Audio sink will easily work if enabled in profile. It looks like the higher layer of BT is kind of implementation complete at the framework, which is in the form of App i.e. packages/app/Bluetooth (with some bugs but still works). But all profiles are not completed at framework lower layer via HAL interfaces which is btif framework (such as btif_rc.cpp, etc which you can look at Android source) and which is the replacement to older Bluez stack by Google.
As I said BT sink is partially implemented and is in work in progress state. BT sink such as Audio will easily work if enabled as sink profile but not all such as AVRCP will not work. At present, I saw the issue with AOSP code that incoming traffic from the remote device to Android works but outgoing traffic from Android to the remote device doesn't work (upon which AVRCP profile works) as remote device object is not maintained in the stack and so JNI calls from app/Bluetooth fails with null device at btif_*.cpp files. For example, send pass-through commands doesn't work.
So, we may see Bluetooth sink profiles working in future.
If you want to explore more check AOSP,
Services at packages/app/Bluetooth/
HAL's at system/bt/btif/

Categories

Resources