Chromium Code Reviews| Index: media/midi/java/src/org/chromium/media/midi/MidiInputPortAndroid.java |
| diff --git a/media/midi/java/src/org/chromium/media/midi/MidiInputPortAndroid.java b/media/midi/java/src/org/chromium/media/midi/MidiInputPortAndroid.java |
| new file mode 100644 |
| index 0000000000000000000000000000000000000000..c8bbab42695226de14fb3d09f0d23142a59915ee |
| --- /dev/null |
| +++ b/media/midi/java/src/org/chromium/media/midi/MidiInputPortAndroid.java |
| @@ -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 WebMIDI manner. It corresponds to MidiOutputPort class in the |
|
Takashi Toyoshima
2015/09/07 16:42:19
s/WebMIDI/Web MIDI/
yhirano
2015/09/10 11:43:50
Done.
|
| +// 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 MidiDevice mDevice; |
| + /** |
| + * The index of the port in the associated device. |
| + */ |
| + private 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); |
| +} |