Requirement: After the USB headset is inserted, the sound should be switched from the local CODEC to the USB headset output/input.
I searched for USB audio hotplug online. The applicable resources are as follows:
1. hotplugging USB audio devices (howto)
The question seems to be consistent with our problem. In fact, there is not much reference value. The script/etc/hotplug/USB/extigy may capture hot swapping events of the USB audio device, which can be further verified and used. Pay attention to this.
2. Example to map USB ports to ALSA card numbers and
Add each sound card to a combined, single interface device
This is the use of udev to obtain the USB hot swapping event. Although Android does not have udev, the example program can process the hot swapping event string for reference.
3. USB mic on Linux
In fact, our first step is to verify whether the USB headset can play back the recording.
3.1 plug in the USB headset and you can see that ALSA has loaded the USB audio, as shown below:
~ # cat /proc/asound/cards 0 [WMTSOC ]: HWDAC - WMT_SOC WMT_SOC (HWDAC) 1 [default ]: USB-Audio - C-Media USB Headphone Set C-Media USB Headphone Set at usb-0000:00:06.0-1, full speed
3.2 refer to this link and write the following configuration file/etc/asond. conf:
pcm.!default { type asym playback.pcm { type plug slave.pcm "hw:1,0" } capture.pcm { type plug slave.pcm "hw:1,0" } }
After the restart, the sound will come out of the headset.
HW: corresponds to card1, that is, USB-audio-C-media USB headphone set
4. Hot swapping of USB devices in Linux
At this point, you need to consider the implementation of switching USB audio on the Android platform. There are several ways: 1/hotplug/USB; 2/udev; 3/Netlink. Here is the implementation method of Netlink. There is an example program that confirms the availability in the Link. At present, hot swapping event strings may need to be processed.
Difficulties:
The underlying portal for switching Android audio devices is alsa_default.cpp. At present, we need to define the plug of local codec and USB audio in asound. conf; we also need to modify alsa_default.cpp. The most important Android devices need to know USB
Enable the USB audio plug when you plug in audio, and enable the local codec plug when you unplug the USB audio. In this case, the modification scope is quite large. In addition, it cannot be determined whether switching the audio device will affect the playback process? If ALSA allows the configuration of asound. conf to achieve the same purpose, it would be easy. Unfortunately, this information cannot be found at present, and it should not be convenient.
Progress:
: According to the analysis of the above difficulties, the code of the entire android framework layer and the Alsa configuration file are roughly completed, basically realizing the switching of audio devices during USB audio hot swapping. However, there is a big problem: Switching the audio device during playback will lead to the audioflinger service crash (this problem also exists during 2G calls, and it is avoided by other means ). It seems that when switching the audio device, you should stop playing. After switching, You can resume playing.
It is rumored that android3.1 began to support USB audio devices: "Your device will support USB Host Mode, applications can now manage connected USB peripherals such as audio devices. input devices, communications devices, and more .". But when will android3.1 be open-source?
I already have some ideas. I just remembered that android does not support Bluetooth audio? The switching of Bluetooth headset should be similar to that of USB headset. We should carefully study how Bluetooth Audio is implemented. Android audio system 3: audiopolicyservice and audiopolicymanager