diff options
Diffstat (limited to 'media/base')
4 files changed, 0 insertions, 567 deletions
diff --git a/media/base/android/BUILD.gn b/media/base/android/BUILD.gn index f6f910e..e000a09 100644 --- a/media/base/android/BUILD.gn +++ b/media/base/android/BUILD.gn @@ -81,8 +81,6 @@ generate_jni("media_jni_headers") { "java/src/org/chromium/media/MediaDrmBridge.java", "java/src/org/chromium/media/MediaPlayerBridge.java", "java/src/org/chromium/media/MediaPlayerListener.java", - "java/src/org/chromium/media/UsbMidiDeviceAndroid.java", - "java/src/org/chromium/media/UsbMidiDeviceFactoryAndroid.java", "java/src/org/chromium/media/WebAudioMediaCodecBridge.java", ] jni_package = "media" diff --git a/media/base/android/java/src/org/chromium/media/UsbMidiDeviceAndroid.java b/media/base/android/java/src/org/chromium/media/UsbMidiDeviceAndroid.java deleted file mode 100644 index 2f80b96..0000000 --- a/media/base/android/java/src/org/chromium/media/UsbMidiDeviceAndroid.java +++ /dev/null @@ -1,295 +0,0 @@ -// Copyright 2014 The Chromium Authors. All rights reserved. -// Use of this source code is governed by a BSD-style license that can be -// found in the LICENSE file. - -package org.chromium.media; - -import android.annotation.TargetApi; -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.Build; -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; - -/** - * Owned by its native counterpart declared in usb_midi_device_android.h. - * Refer to that class for general comments. - */ -@JNINamespace("media") -class UsbMidiDeviceAndroid { - /** - * A connection handle for this device. - */ - private final UsbDeviceConnection mConnection; - - /** - * A map from endpoint number to UsbEndpoint. - */ - private final SparseArray<UsbEndpoint> mEndpointMap; - - /** - * A map from UsbEndpoint to UsbRequest associated to it. - */ - private final Map<UsbEndpoint, UsbRequest> mRequestMap; - - /** - * The handler used for posting events on the main thread. - */ - private final Handler mHandler; - - /** - * True if this device is closed. - */ - private boolean mIsClosed; - - /** - * True if there is a thread processing input data. - */ - private boolean mHasInputThread; - - /** - * The identifier of this device. - */ - private long mNativePointer; - - /** - * The underlying USB device. - */ - private UsbDevice mUsbDevice; - - /** - * Audio interface subclass code for MIDI. - */ - static final int MIDI_SUBCLASS = 3; - - /** - * Constructs a UsbMidiDeviceAndroid. - * @param manager - * @param device The USB device which this object is assocated with. - */ - UsbMidiDeviceAndroid(UsbManager manager, UsbDevice device) { - mConnection = manager.openDevice(device); - mEndpointMap = new SparseArray<UsbEndpoint>(); - mRequestMap = new HashMap<UsbEndpoint, UsbRequest>(); - mHandler = new Handler(); - mUsbDevice = device; - mIsClosed = false; - mHasInputThread = 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); - } - } - } - // Start listening for input endpoints. - // This function will create and run a thread if there is USB-MIDI endpoints in the - // device. Note that because UsbMidiDevice is shared among all tabs and the thread - // will be terminated when the device is disconnected, at most one thread can be created - // for each connected USB-MIDI device. - startListen(device); - } - - /** - * Starts listening for input endpoints. - */ - 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; - } - mHasInputThread = true; - // bufferForEndpoints must not be accessed hereafter on this thread. - new Thread() { - @Override - public void run() { - while (true) { - UsbRequest request = mConnection.requestWait(); - if (request == null) { - // When the device is closed requestWait will fail. - 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(); - } - - /** - * Posts a data input event to the main thread. - */ - private void postOnDataEvent(final int endpointNumber, final byte[] bs) { - mHandler.post(new Runnable() { - @Override - public void run() { - if (mIsClosed) { - return; - } - nativeOnData(mNativePointer, endpointNumber, bs); - } - }); - } - - UsbDevice getUsbDevice() { - return mUsbDevice; - } - - boolean isClosed() { - return mIsClosed; - } - - /** - * Register the own native pointer. - */ - @CalledByNative - void registerSelf(long nativePointer) { - mNativePointer = nativePointer; - } - - /** - * Sends a USB-MIDI data to the device. - * @param endpointNumber The endpoint number of the destination endpoint. - * @param bs The data to be sent. - */ - @TargetApi(Build.VERSION_CODES.JELLY_BEAN_MR2) - @CalledByNative - void send(int endpointNumber, byte[] bs) { - if (mIsClosed) { - return; - } - UsbEndpoint endpoint = mEndpointMap.get(endpointNumber); - if (endpoint == null) { - return; - } - if (shouldUseBulkTransfer()) { - // We use bulkTransfer instead of UsbRequest.queue because queueing - // a UsbRequest is currently not thread safe. - // Note that this is not exactly correct because we don't care - // about the transfer attribute (bmAttribute) of the endpoint. - // See also: - // http://stackoverflow.com/questions/9644415/ - // https://code.google.com/p/android/issues/detail?id=59467 - // - // TODO(yhirano): Delete this block once the problem is fixed. - final int timeout = 100; - mConnection.bulkTransfer(endpoint, bs, bs.length, timeout); - } else { - 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); - } - } - - /** - * Returns true if |bulkTransfer| should be used in |send|. - * See comments in |send|. - */ - private boolean shouldUseBulkTransfer() { - return mHasInputThread; - } - - /** - * Returns the descriptors bytes of this device. - * @return The descriptors bytes of this device. - */ - @CalledByNative - byte[] getDescriptors() { - if (mConnection == null) { - return new byte[0]; - } - return mConnection.getRawDescriptors(); - } - - /** - * Closes the device connection. - */ - @CalledByNative - void close() { - mEndpointMap.clear(); - for (UsbRequest request : mRequestMap.values()) { - request.close(); - } - mRequestMap.clear(); - mConnection.close(); - mNativePointer = 0; - mIsClosed = true; - } - - /** - * Returns the length of a USB-MIDI input. - * Since the Android API doesn't provide us the length, - * we calculate it manually. - */ - private static int getInputDataLength(ByteBuffer buffer) { - int position = buffer.position(); - // We assume that the data length is always divisable by 4. - for (int i = 0; i < position; i += 4) { - // Since Code Index Number 0 is reserved, it is not a valid USB-MIDI data. - if (buffer.get(i) == 0) { - return i; - } - } - return position; - } - - private static native void nativeOnData(long nativeUsbMidiDeviceAndroid, - int endpointNumber, - byte[] data); -} diff --git a/media/base/android/java/src/org/chromium/media/UsbMidiDeviceFactoryAndroid.java b/media/base/android/java/src/org/chromium/media/UsbMidiDeviceFactoryAndroid.java deleted file mode 100644 index 814bb17..0000000 --- a/media/base/android/java/src/org/chromium/media/UsbMidiDeviceFactoryAndroid.java +++ /dev/null @@ -1,264 +0,0 @@ -// Copyright 2014 The Chromium Authors. All rights reserved. -// Use of this source code is governed by a BSD-style license that can be -// found in the LICENSE file. - -package org.chromium.media; - -import android.app.PendingIntent; -import android.content.BroadcastReceiver; -import android.content.Context; -import android.content.Intent; -import android.content.IntentFilter; -import android.hardware.usb.UsbConstants; -import android.hardware.usb.UsbDevice; -import android.hardware.usb.UsbInterface; -import android.hardware.usb.UsbManager; -import android.os.Parcelable; - -import org.chromium.base.CalledByNative; -import org.chromium.base.JNINamespace; - -import java.util.ArrayList; -import java.util.HashSet; -import java.util.List; -import java.util.Map; -import java.util.Set; - -/** - * Owned by its native counterpart declared in - * usb_midi_device_factory_android.h. Refer to that class for general comments. - */ -@JNINamespace("media") -class UsbMidiDeviceFactoryAndroid { - /** - * The UsbManager of this system. - */ - private UsbManager mUsbManager; - - /** - * A BroadcastReceiver for USB device events. - */ - private BroadcastReceiver mReceiver; - - /** - * Accessible USB-MIDI devices got so far. - */ - private final List<UsbMidiDeviceAndroid> mDevices = new ArrayList<UsbMidiDeviceAndroid>(); - - /** - * Devices whose access permission requested but not resolved so far. - */ - private Set<UsbDevice> mRequestedDevices; - - /** - * True when the enumeration is in progress. - */ - private boolean mIsEnumeratingDevices; - - /** - * The identifier of this factory. - */ - private long mNativePointer; - - private static final String ACTION_USB_PERMISSION = - "org.chromium.media.USB_PERMISSION"; - - /** - * Constructs a UsbMidiDeviceAndroid. - * @param context - * @param nativePointer The native pointer to which the created factory is associated. - */ - UsbMidiDeviceFactoryAndroid(Context context, long nativePointer) { - mUsbManager = (UsbManager) context.getSystemService(Context.USB_SERVICE); - mNativePointer = nativePointer; - mReceiver = new BroadcastReceiver() { - @Override - public void onReceive(Context context, Intent intent) { - Parcelable extra = intent.getParcelableExtra(UsbManager.EXTRA_DEVICE); - if (UsbManager.ACTION_USB_DEVICE_ATTACHED.equals(intent.getAction())) { - requestDevicePermissionIfNecessary(context, (UsbDevice) extra); - } - if (UsbManager.ACTION_USB_DEVICE_DETACHED.equals(intent.getAction())) { - onUsbDeviceDetached((UsbDevice) extra); - } - if (ACTION_USB_PERMISSION.equals(intent.getAction())) { - onUsbDevicePermissionRequestDone(context, intent); - } - } - }; - IntentFilter filter = new IntentFilter(); - filter.addAction(UsbManager.ACTION_USB_DEVICE_ATTACHED); - filter.addAction(UsbManager.ACTION_USB_DEVICE_DETACHED); - filter.addAction(ACTION_USB_PERMISSION); - context.registerReceiver(mReceiver, filter); - mRequestedDevices = new HashSet<UsbDevice>(); - } - - /** - * Constructs a UsbMidiDeviceAndroid. - * @param context - * @param nativePointer The native pointer to which the created factory is associated. - */ - @CalledByNative - static UsbMidiDeviceFactoryAndroid create(Context context, long nativePointer) { - return new UsbMidiDeviceFactoryAndroid(context, nativePointer); - } - - /** - * Enumerates USB-MIDI devices. - * If there are devices having USB-MIDI interfaces, this function requests permission for - * accessing the device to the user. - * When the permission request is accepted or rejected, nativeOnUsbMidiDeviceRequestDone - * will be called. - * - * If there are no USB-MIDI interfaces, this function returns false. - * @param context - * @return true if some permission requests are in progress. - */ - @CalledByNative - boolean enumerateDevices(Context context) { - assert !mIsEnumeratingDevices; - mIsEnumeratingDevices = true; - Map<String, UsbDevice> devices = mUsbManager.getDeviceList(); - if (devices.isEmpty()) { - // No USB-MIDI devices are found. - mIsEnumeratingDevices = false; - return false; - } - for (UsbDevice device: devices.values()) { - requestDevicePermissionIfNecessary(context, device); - } - return true; - } - - /** - * Request a device access permission if there is a MIDI interface in the device. - * - * @param context - * @param device a USB device - */ - private void requestDevicePermissionIfNecessary(Context context, UsbDevice device) { - for (UsbDevice d: mRequestedDevices) { - if (d.getDeviceId() == device.getDeviceId()) { - // It is already requested. - return; - } - } - - for (int i = 0; i < device.getInterfaceCount(); ++i) { - UsbInterface iface = device.getInterface(i); - if (iface.getInterfaceClass() == UsbConstants.USB_CLASS_AUDIO - && iface.getInterfaceSubclass() == UsbMidiDeviceAndroid.MIDI_SUBCLASS) { - // There is at least one interface supporting MIDI. - mUsbManager.requestPermission(device, PendingIntent.getBroadcast( - context, 0, new Intent(ACTION_USB_PERMISSION), 0)); - mRequestedDevices.add(device); - break; - } - } - } - - /** - * Called when a USB device is detached. - * - * @param device a USB device - */ - private void onUsbDeviceDetached(UsbDevice device) { - for (UsbDevice usbDevice: mRequestedDevices) { - if (usbDevice.getDeviceId() == device.getDeviceId()) { - mRequestedDevices.remove(usbDevice); - break; - } - } - for (int i = 0; i < mDevices.size(); ++i) { - UsbMidiDeviceAndroid midiDevice = mDevices.get(i); - if (midiDevice.isClosed()) { - // Once a device is disconnected, the system may reassign its device ID to - // another device. So we should ignore disconnected ones. - continue; - } - if (midiDevice.getUsbDevice().getDeviceId() == device.getDeviceId()) { - midiDevice.close(); - if (mIsEnumeratingDevices) { - // In this case, we don't have to keep mDevices sync with the devices list - // in MidiManagerUsb. - mDevices.remove(i); - return; - } - if (mNativePointer != 0) { - nativeOnUsbMidiDeviceDetached(mNativePointer, i); - } - return; - } - } - } - - /** - * Called when the user accepts or rejects the permission request requested by - * EnumerateDevices. - * - * @param context - * @param intent - */ - private void onUsbDevicePermissionRequestDone(Context context, Intent intent) { - UsbDevice device = (UsbDevice) intent.getParcelableExtra(UsbManager.EXTRA_DEVICE); - UsbMidiDeviceAndroid midiDevice = null; - if (mRequestedDevices.contains(device)) { - mRequestedDevices.remove(device); - if (!intent.getBooleanExtra(UsbManager.EXTRA_PERMISSION_GRANTED, false)) { - // The request was rejected. - device = null; - } - } else { - device = null; - } - - if (device != null) { - for (UsbMidiDeviceAndroid registered: mDevices) { - if (!registered.isClosed() - && registered.getUsbDevice().getDeviceId() == device.getDeviceId()) { - // The device is already registered. - device = null; - break; - } - } - } - - if (device != null) { - // Now we can add the device. - midiDevice = new UsbMidiDeviceAndroid(mUsbManager, device); - mDevices.add(midiDevice); - } - - if (!mRequestedDevices.isEmpty()) { - return; - } - if (mNativePointer == 0) { - return; - } - - if (mIsEnumeratingDevices) { - nativeOnUsbMidiDeviceRequestDone(mNativePointer, mDevices.toArray()); - mIsEnumeratingDevices = false; - } else if (midiDevice != null) { - nativeOnUsbMidiDeviceAttached(mNativePointer, midiDevice); - } - } - - /** - * Disconnects the native object. - * @param context - */ - @CalledByNative - void close(Context context) { - mNativePointer = 0; - context.unregisterReceiver(mReceiver); - } - - private static native void nativeOnUsbMidiDeviceRequestDone( - long nativeUsbMidiDeviceFactoryAndroid, Object[] devices); - private static native void nativeOnUsbMidiDeviceAttached( - long nativeUsbMidiDeviceFactoryAndroid, Object device); - private static native void nativeOnUsbMidiDeviceDetached( - long nativeUsbMidiDeviceFactoryAndroid, int index); -} diff --git a/media/base/android/media_jni_registrar.cc b/media/base/android/media_jni_registrar.cc index c9b8489..9ab40e4 100644 --- a/media/base/android/media_jni_registrar.cc +++ b/media/base/android/media_jni_registrar.cc @@ -15,8 +15,6 @@ #include "media/base/android/media_player_bridge.h" #include "media/base/android/media_player_listener.h" #include "media/base/android/webaudio_media_codec_bridge.h" -#include "media/midi/usb_midi_device_android.h" -#include "media/midi/usb_midi_device_factory_android.h" #include "media/video/capture/android/video_capture_device_android.h" #include "media/video/capture/android/video_capture_device_factory_android.h" @@ -35,10 +33,6 @@ static base::android::RegistrationMethod kMediaRegisteredMethods[] = { MediaPlayerBridge::RegisterMediaPlayerBridge }, { "MediaPlayerListener", MediaPlayerListener::RegisterMediaPlayerListener }, - { "UsbMidiDevice", - UsbMidiDeviceAndroid::RegisterUsbMidiDevice }, - { "UsbMidiDeviceFactory", - UsbMidiDeviceFactoryAndroid::RegisterUsbMidiDeviceFactory }, { "VideoCaptureDevice", VideoCaptureDeviceAndroid::RegisterVideoCaptureDevice }, { "VideoCaptureDeviceFactory", |