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
|
/*
* Copyright (C) 2014 The Android Open Source Project
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions an
* limitations under the License.
*/
package com.android.server.usb;
import android.alsa.AlsaCardsParser;
import android.alsa.AlsaDevicesParser;
import android.content.Context;
import android.content.Intent;
import android.hardware.usb.UsbConstants;
import android.hardware.usb.UsbDevice;
import android.hardware.usb.UsbInterface;
import android.media.AudioManager;
import android.os.UserHandle;
import android.util.Slog;
import java.io.File;
import java.io.FileDescriptor;
import java.io.PrintWriter;
import java.util.HashMap;
/**
* UsbAudioManager manages USB audio devices.
*/
public class UsbAudioManager {
private static final String TAG = UsbAudioManager.class.getSimpleName();
private static final boolean DEBUG = false;
private final Context mContext;
private final class AudioDevice {
public int mCard;
public int mDevice;
public boolean mHasPlayback;
public boolean mHasCapture;
public boolean mHasMIDI;
public AudioDevice(int card, int device,
boolean hasPlayback, boolean hasCapture, boolean hasMidi) {
mCard = card;
mDevice = device;
mHasPlayback = hasPlayback;
mHasCapture = hasCapture;
mHasMIDI = hasMidi;
}
public String toString() {
StringBuilder sb = new StringBuilder();
sb.append("AudioDevice: [card: " + mCard);
sb.append(", device: " + mDevice);
sb.append(", hasPlayback: " + mHasPlayback);
sb.append(", hasCapture: " + mHasCapture);
sb.append(", hasMidi: " + mHasMIDI);
sb.append("]");
return sb.toString();
}
}
private final HashMap<UsbDevice,AudioDevice> mAudioDevices
= new HashMap<UsbDevice,AudioDevice>();
/* package */ UsbAudioManager(Context context) {
mContext = context;
}
// Broadcasts the arrival/departure of a USB audio interface
// audioDevice - the AudioDevice that was added or removed
// enabled - if true, we're connecting a device (it's arrived), else disconnecting
private void sendDeviceNotification(AudioDevice audioDevice, boolean enabled) {
// send a sticky broadcast containing current USB state
Intent intent = new Intent(AudioManager.ACTION_USB_AUDIO_DEVICE_PLUG);
intent.addFlags(Intent.FLAG_RECEIVER_REPLACE_PENDING);
intent.addFlags(Intent.FLAG_RECEIVER_REGISTERED_ONLY);
intent.putExtra("state", enabled ? 1 : 0);
intent.putExtra("card", audioDevice.mCard);
intent.putExtra("device", audioDevice.mDevice);
intent.putExtra("hasPlayback", audioDevice.mHasPlayback);
intent.putExtra("hasCapture", audioDevice.mHasCapture);
intent.putExtra("hasMIDI", audioDevice.mHasMIDI);
mContext.sendStickyBroadcastAsUser(intent, UserHandle.ALL);
}
private boolean waitForAlsaFile(int card, int device, boolean capture) {
// These values were empirically determined.
final int kNumRetries = 5;
final int kSleepTime = 500; // ms
String alsaDevPath = "/dev/snd/pcmC" + card + "D" + device + (capture ? "c" : "p");
File alsaDevFile = new File(alsaDevPath);
boolean exists = false;
for (int retry = 0; !exists && retry < kNumRetries; retry++) {
exists = alsaDevFile.exists();
if (!exists) {
try {
Thread.sleep(kSleepTime);
} catch (IllegalThreadStateException ex) {
Slog.d(TAG, "usb: IllegalThreadStateException while waiting for ALSA file.");
} catch (java.lang.InterruptedException ex) {
Slog.d(TAG, "usb: InterruptedException while waiting for ALSA file.");
}
}
}
return exists;
}
/* package */ void deviceAdded(UsbDevice usbDevice) {
// Is there an audio interface in there?
boolean isAudioDevice = false;
// FIXME - handle multiple configurations?
int interfaceCount = usbDevice.getInterfaceCount();
for (int ntrfaceIndex = 0; !isAudioDevice && ntrfaceIndex < interfaceCount;
ntrfaceIndex++) {
UsbInterface ntrface = usbDevice.getInterface(ntrfaceIndex);
if (ntrface.getInterfaceClass() == UsbConstants.USB_CLASS_AUDIO) {
isAudioDevice = true;
}
}
if (!isAudioDevice) {
return;
}
//TODO(pmclean) The "Parser" objects inspect files in "/proc/asound" which we presume is
// present, unlike the waitForAlsaFile() which waits on a file in /dev/snd. It is not
// clear why this works, or that it can be relied on going forward. Needs further
// research.
AlsaCardsParser cardsParser = new AlsaCardsParser();
cardsParser.scan();
// cardsParser.Log();
// But we need to parse the device to determine its capabilities.
AlsaDevicesParser devicesParser = new AlsaDevicesParser();
devicesParser.scan();
// devicesParser.Log();
// The protocol for now will be to select the last-connected (highest-numbered)
// Alsa Card.
int card = cardsParser.getNumCardRecords() - 1;
int device = 0;
boolean hasPlayback = devicesParser.hasPlaybackDevices(card);
boolean hasCapture = devicesParser.hasCaptureDevices(card);
boolean hasMidi = devicesParser.hasMIDIDevices(card);
// Playback device file needed/present?
if (hasPlayback &&
!waitForAlsaFile(card, device, false)) {
return;
}
// Capture device file needed/present?
if (hasCapture &&
!waitForAlsaFile(card, device, true)) {
return;
}
if (DEBUG) {
Slog.d(TAG,
"usb: hasPlayback:" + hasPlayback + " hasCapture:" + hasCapture);
}
AudioDevice audioDevice = new AudioDevice(card, device, hasPlayback, hasCapture, hasMidi);
mAudioDevices.put(usbDevice, audioDevice);
sendDeviceNotification(audioDevice, true);
}
/* package */ void deviceRemoved(UsbDevice device) {
if (DEBUG) {
Slog.d(TAG, "deviceRemoved(): " + device);
}
AudioDevice audioDevice = mAudioDevices.remove(device);
if (audioDevice != null) {
sendDeviceNotification(audioDevice, false);
}
}
public void dump(FileDescriptor fd, PrintWriter pw) {
pw.println(" USB AudioDevices:");
for (UsbDevice device : mAudioDevices.keySet()) {
pw.println(" " + device.getDeviceName() + ": " + mAudioDevices.get(device));
}
}
}
|