forked from chromium/chromium
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
This CL introduces WebMIDI implementation using Android native MIDI API. The implementation is enabled only when the device has a newer sdk version and the experimental flag "use-android-midi-api" is enabled. BUG=486584 Review URL: https://codereview.chromium.org/1177973003 Cr-Commit-Position: refs/heads/master@{#349419}
- Loading branch information
1 parent
33e9087
commit e8b2e7d
Showing
18 changed files
with
1,075 additions
and
20 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
132 changes: 132 additions & 0 deletions
132
media/midi/java/src/org/chromium/media/midi/MidiDeviceAndroid.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,132 @@ | ||
// Copyright 2015 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.midi; | ||
|
||
import android.media.midi.MidiDevice; | ||
import android.media.midi.MidiDeviceInfo; | ||
|
||
import org.chromium.base.annotations.CalledByNative; | ||
import org.chromium.base.annotations.JNINamespace; | ||
|
||
@JNINamespace("media::midi") | ||
/** | ||
* A class implementing media::midi::MidiDeviceAndroid functionality. | ||
*/ | ||
class MidiDeviceAndroid { | ||
/** | ||
* The underlying device. | ||
*/ | ||
private final MidiDevice mDevice; | ||
/** | ||
* The input ports in the device. | ||
*/ | ||
private final MidiInputPortAndroid[] mInputPorts; | ||
/** | ||
* The output ports in the device. | ||
*/ | ||
private final MidiOutputPortAndroid[] mOutputPorts; | ||
/** | ||
* True when the device is open. | ||
*/ | ||
private boolean mIsOpen; | ||
|
||
/** | ||
* constructor | ||
* @param device the underlying device | ||
*/ | ||
MidiDeviceAndroid(MidiDevice device) { | ||
mIsOpen = true; | ||
mDevice = device; | ||
// Note we use "input" and "output" in the Web MIDI manner. | ||
|
||
mOutputPorts = new MidiOutputPortAndroid[getInfo().getInputPortCount()]; | ||
for (int i = 0; i < mOutputPorts.length; ++i) { | ||
mOutputPorts[i] = new MidiOutputPortAndroid(device, i); | ||
} | ||
|
||
mInputPorts = new MidiInputPortAndroid[getInfo().getOutputPortCount()]; | ||
for (int i = 0; i < mInputPorts.length; ++i) { | ||
mInputPorts[i] = new MidiInputPortAndroid(device, i); | ||
} | ||
} | ||
|
||
/** | ||
* Returns true when the device is open. | ||
*/ | ||
boolean isOpen() { | ||
return mIsOpen; | ||
} | ||
|
||
/** | ||
* Closes the device. | ||
*/ | ||
void close() { | ||
mIsOpen = false; | ||
for (MidiInputPortAndroid port : mInputPorts) { | ||
port.close(); | ||
} | ||
for (MidiOutputPortAndroid port : mOutputPorts) { | ||
port.close(); | ||
} | ||
} | ||
|
||
/** | ||
* Returns the underlying device. | ||
*/ | ||
MidiDevice getDevice() { | ||
return mDevice; | ||
} | ||
|
||
/** | ||
* Returns the underlying device information. | ||
*/ | ||
MidiDeviceInfo getInfo() { | ||
return mDevice.getInfo(); | ||
} | ||
|
||
/** | ||
* Returns the manufacturer name. | ||
*/ | ||
@CalledByNative | ||
String getManufacturer() { | ||
return getProperty(MidiDeviceInfo.PROPERTY_MANUFACTURER); | ||
} | ||
|
||
/** | ||
* Returns the product name. | ||
*/ | ||
@CalledByNative | ||
String getProduct() { | ||
return getProperty(MidiDeviceInfo.PROPERTY_PRODUCT); | ||
} | ||
|
||
/** | ||
* Returns the version string. | ||
*/ | ||
@CalledByNative | ||
String getVersion() { | ||
return getProperty(MidiDeviceInfo.PROPERTY_VERSION); | ||
} | ||
|
||
/** | ||
* Returns the associated input ports. | ||
*/ | ||
@CalledByNative | ||
MidiInputPortAndroid[] getInputPorts() { | ||
return mInputPorts; | ||
} | ||
|
||
/** | ||
* Returns the associated output ports. | ||
*/ | ||
@CalledByNative | ||
MidiOutputPortAndroid[] getOutputPorts() { | ||
return mOutputPorts; | ||
} | ||
|
||
private String getProperty(String name) { | ||
return mDevice.getInfo().getProperties().getString(name); | ||
} | ||
} |
94 changes: 94 additions & 0 deletions
94
media/midi/java/src/org/chromium/media/midi/MidiInputPortAndroid.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,94 @@ | ||
// Copyright 2015 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.midi; | ||
|
||
import android.media.midi.MidiDevice; | ||
import android.media.midi.MidiOutputPort; | ||
import android.media.midi.MidiReceiver; | ||
|
||
import org.chromium.base.annotations.CalledByNative; | ||
import org.chromium.base.annotations.JNINamespace; | ||
|
||
import java.io.IOException; | ||
|
||
// Note "InputPort" is named in the Web MIDI manner. It corresponds to MidiOutputPort class in the | ||
// Android API. | ||
/** | ||
* A MidiInputPortAndroid provides data to the associated media::midi::MidiInputPortAndroid object. | ||
*/ | ||
@JNINamespace("media::midi") | ||
class MidiInputPortAndroid { | ||
/** | ||
* The underlying port. | ||
*/ | ||
private MidiOutputPort mPort; | ||
/** | ||
* A pointer to a media::midi::MidiInputPortAndroid object. | ||
*/ | ||
private long mNativeReceiverPointer; | ||
/** | ||
* The device this port belongs to. | ||
*/ | ||
private final MidiDevice mDevice; | ||
/** | ||
* The index of the port in the associated device. | ||
*/ | ||
private final int mIndex; | ||
|
||
/** | ||
* constructor | ||
* @param device the device this port belongs to. | ||
* @param index the index of the port in the associated device. | ||
*/ | ||
MidiInputPortAndroid(MidiDevice device, int index) { | ||
mDevice = device; | ||
mIndex = index; | ||
} | ||
|
||
/** | ||
* Registers this object to the underlying port so as to the C++ function will be called with | ||
* the given C++ object when data arrives. | ||
* @param nativeReceiverPointer a pointer to a media::midi::MidiInputPortAndroid object. | ||
* @return true if this operation succeeds or the port is already open. | ||
*/ | ||
@CalledByNative | ||
boolean open(long nativeReceiverPointer) { | ||
if (mPort != null) { | ||
return true; | ||
} | ||
mPort = mDevice.openOutputPort(mIndex); | ||
if (mPort == null) { | ||
return false; | ||
} | ||
mNativeReceiverPointer = nativeReceiverPointer; | ||
mPort.connect(new MidiReceiver() { | ||
@Override | ||
public void onSend(byte[] bs, int offset, int count, long timestamp) { | ||
nativeOnData(mNativeReceiverPointer, bs, offset, count, timestamp); | ||
} | ||
}); | ||
return true; | ||
} | ||
|
||
/** | ||
* Closes the port. | ||
*/ | ||
@CalledByNative | ||
void close() { | ||
if (mPort == null) { | ||
return; | ||
} | ||
try { | ||
mPort.close(); | ||
} catch (IOException e) { | ||
// We can do nothing here. Just ignore the error. | ||
} | ||
mNativeReceiverPointer = 0; | ||
mPort = null; | ||
} | ||
|
||
private static native void nativeOnData( | ||
long nativeMidiInputPortAndroid, byte[] bs, int offset, int count, long timestamp); | ||
} |
Oops, something went wrong.