This source file includes following definitions.
- JNINamespace
- startListen
- postOnDataEvent
- registerSelf
- send
- getDescriptors
- close
- getInputDataLength
- nativeOnData
package org.chromium.media;
import android.hardware.usb.UsbConstants;
import android.hardware.usb.UsbDevice;
import android.hardware.usb.UsbDeviceConnection;
import android.hardware.usb.UsbEndpoint;
import android.hardware.usb.UsbInterface;
import android.hardware.usb.UsbManager;
import android.hardware.usb.UsbRequest;
import android.os.Handler;
import android.util.SparseArray;
import org.chromium.base.CalledByNative;
import org.chromium.base.JNINamespace;
import java.nio.ByteBuffer;
import java.util.HashMap;
import java.util.Map;
@JNINamespace("media")
class UsbMidiDeviceAndroid {
private final UsbDeviceConnection mConnection;
private final SparseArray<UsbEndpoint> mEndpointMap;
private final Map<UsbEndpoint, UsbRequest> mRequestMap;
private final Handler mHandler;
private boolean mIsClosed;
private long mNativePointer;
static final int MIDI_SUBCLASS = 3;
UsbMidiDeviceAndroid(UsbManager manager, UsbDevice device) {
mConnection = manager.openDevice(device);
mEndpointMap = new SparseArray<UsbEndpoint>();
mRequestMap = new HashMap<UsbEndpoint, UsbRequest>();
mHandler = new Handler();
mIsClosed = false;
mNativePointer = 0;
for (int i = 0; i < device.getInterfaceCount(); ++i) {
UsbInterface iface = device.getInterface(i);
if (iface.getInterfaceClass() != UsbConstants.USB_CLASS_AUDIO ||
iface.getInterfaceSubclass() != MIDI_SUBCLASS) {
continue;
}
mConnection.claimInterface(iface, true);
for (int j = 0; j < iface.getEndpointCount(); ++j) {
UsbEndpoint endpoint = iface.getEndpoint(j);
if (endpoint.getDirection() == UsbConstants.USB_DIR_OUT) {
mEndpointMap.put(endpoint.getEndpointNumber(), endpoint);
}
}
}
startListen(device);
}
private void startListen(final UsbDevice device) {
final Map<UsbEndpoint, ByteBuffer> bufferForEndpoints =
new HashMap<UsbEndpoint, ByteBuffer>();
for (int i = 0; i < device.getInterfaceCount(); ++i) {
UsbInterface iface = device.getInterface(i);
if (iface.getInterfaceClass() != UsbConstants.USB_CLASS_AUDIO ||
iface.getInterfaceSubclass() != MIDI_SUBCLASS) {
continue;
}
for (int j = 0; j < iface.getEndpointCount(); ++j) {
UsbEndpoint endpoint = iface.getEndpoint(j);
if (endpoint.getDirection() == UsbConstants.USB_DIR_IN) {
ByteBuffer buffer = ByteBuffer.allocate(endpoint.getMaxPacketSize());
UsbRequest request = new UsbRequest();
request.initialize(mConnection, endpoint);
request.queue(buffer, buffer.remaining());
bufferForEndpoints.put(endpoint, buffer);
}
}
}
if (bufferForEndpoints.isEmpty()) {
return;
}
new Thread() {
public void run() {
while (true) {
UsbRequest request = mConnection.requestWait();
if (request == null) {
break;
}
UsbEndpoint endpoint = request.getEndpoint();
if (endpoint.getDirection() != UsbConstants.USB_DIR_IN) {
continue;
}
ByteBuffer buffer = bufferForEndpoints.get(endpoint);
int length = getInputDataLength(buffer);
if (length > 0) {
buffer.rewind();
final byte[] bs = new byte[length];
buffer.get(bs, 0, length);
postOnDataEvent(endpoint.getEndpointNumber(), bs);
}
buffer.rewind();
request.queue(buffer, buffer.capacity());
}
}
}.start();
}
private void postOnDataEvent(final int endpointNumber, final byte[] bs) {
mHandler.post(new Runnable() {
public void run() {
if (mIsClosed) {
return;
}
nativeOnData(mNativePointer, endpointNumber, bs);
}
});
}
@CalledByNative
void registerSelf(long nativePointer) {
mNativePointer = nativePointer;
}
@CalledByNative
void send(int endpointNumber, byte[] bs) {
if (mIsClosed) {
return;
}
UsbEndpoint endpoint = mEndpointMap.get(endpointNumber);
if (endpoint == null) {
return;
}
UsbRequest request = mRequestMap.get(endpoint);
if (request == null) {
request = new UsbRequest();
request.initialize(mConnection, endpoint);
mRequestMap.put(endpoint, request);
}
request.queue(ByteBuffer.wrap(bs), bs.length);
}
@CalledByNative
byte[] getDescriptors() {
if (mConnection == null) {
return new byte[0];
}
return mConnection.getRawDescriptors();
}
@CalledByNative
void close() {
mEndpointMap.clear();
for (UsbRequest request : mRequestMap.values()) {
request.close();
}
mRequestMap.clear();
mConnection.close();
mNativePointer = 0;
mIsClosed = true;
}
private static int getInputDataLength(ByteBuffer buffer) {
int position = buffer.position();
for (int i = 0; i < position; i += 4) {
if (buffer.get(i) == 0) {
return i;
}
}
return position;
}
private static native void nativeOnData(long nativeUsbMidiDeviceAndroid,
int endpointNumber,
byte[] data);
}