1 // Copyright 2014 The Chromium Authors. All rights reserved.
2 // Use of this source code is governed by a BSD-style license that can be
3 // found in the LICENSE file.
5 package org.chromium.media;
7 import android.hardware.usb.UsbConstants;
8 import android.hardware.usb.UsbDevice;
9 import android.hardware.usb.UsbDeviceConnection;
10 import android.hardware.usb.UsbEndpoint;
11 import android.hardware.usb.UsbInterface;
12 import android.hardware.usb.UsbManager;
13 import android.hardware.usb.UsbRequest;
15 import org.chromium.base.CalledByNative;
16 import org.chromium.base.JNINamespace;
18 import java.nio.ByteBuffer;
19 import java.util.HashMap;
23 * Owned by its native counterpart declared in usb_midi_device_android.h.
24 * Refer to that class for general comments.
26 @JNINamespace("media")
27 class UsbMidiDeviceAndroid {
29 * A connection handle for this device.
31 private UsbDeviceConnection mConnection;
34 * A map from endpoint number to UsbEndpoint.
36 private final Map<Integer, UsbEndpoint> mEndpointMap;
39 * A map from UsbEndpoint to UsbRequest associated to it.
41 private final Map<UsbEndpoint, UsbRequest> mRequestMap;
44 * Audio interface subclass code for MIDI.
46 static final int MIDI_SUBCLASS = 3;
49 * Constructs a UsbMidiDeviceAndroid.
51 * @param device The USB device which this object is assocated with.
53 UsbMidiDeviceAndroid(UsbManager manager, UsbDevice device) {
54 mConnection = manager.openDevice(device);
55 mEndpointMap = new HashMap<Integer, UsbEndpoint>();
56 mRequestMap = new HashMap<UsbEndpoint, UsbRequest>();
58 for (int i = 0; i < device.getInterfaceCount(); ++i) {
59 UsbInterface iface = device.getInterface(i);
60 if (iface.getInterfaceClass() != UsbConstants.USB_CLASS_AUDIO ||
61 iface.getInterfaceSubclass() != MIDI_SUBCLASS) {
64 mConnection.claimInterface(iface, true);
65 for (int j = 0; j < iface.getEndpointCount(); ++j) {
66 UsbEndpoint endpoint = iface.getEndpoint(j);
67 if (endpoint.getDirection() == UsbConstants.USB_DIR_OUT) {
68 mEndpointMap.put(endpoint.getEndpointNumber(), endpoint);
75 * Sends a USB-MIDI data to the device.
76 * @param endpointNumber The endpoint number of the destination endpoint.
77 * @param bs The data to be sent.
80 void send(int endpointNumber, byte[] bs) {
81 if (mConnection == null) {
84 if (!mEndpointMap.containsKey(endpointNumber)) {
87 UsbEndpoint endpoint = mEndpointMap.get(endpointNumber);
89 if (mRequestMap.containsKey(endpoint)) {
90 request = mRequestMap.get(endpoint);
92 request = new UsbRequest();
93 request.initialize(mConnection, endpoint);
94 mRequestMap.put(endpoint, request);
96 request.queue(ByteBuffer.wrap(bs), bs.length);
100 * Returns the descriptors bytes of this device.
101 * @return The descriptors bytes of this device.
104 byte[] getDescriptors() {
105 if (mConnection == null) {
108 return mConnection.getRawDescriptors();
112 * Closes the device connection.
116 mEndpointMap.clear();
117 for (UsbRequest request : mRequestMap.values()) {
121 if (mConnection != null) {