OLD | NEW |
| (Empty) |
1 // Copyright 2015 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. | |
4 | |
5 package org.chromium.media.midi; | |
6 | |
7 import android.annotation.TargetApi; | |
8 import android.media.midi.MidiDevice; | |
9 import android.media.midi.MidiOutputPort; | |
10 import android.media.midi.MidiReceiver; | |
11 import android.os.Build; | |
12 | |
13 import org.chromium.base.annotations.CalledByNative; | |
14 import org.chromium.base.annotations.JNINamespace; | |
15 | |
16 import java.io.IOException; | |
17 | |
18 // Note "InputPort" is named in the Web MIDI manner. It corresponds to MidiOutpu
tPort class in the | |
19 // Android API. | |
20 /** | |
21 * A MidiInputPortAndroid provides data to the associated media::midi::MidiInput
PortAndroid object. | |
22 */ | |
23 @JNINamespace("media::midi") | |
24 @TargetApi(Build.VERSION_CODES.M) | |
25 class MidiInputPortAndroid { | |
26 /** | |
27 * The underlying port. | |
28 */ | |
29 private MidiOutputPort mPort; | |
30 /** | |
31 * A pointer to a media::midi::MidiInputPortAndroid object. | |
32 */ | |
33 private long mNativeReceiverPointer; | |
34 /** | |
35 * The device this port belongs to. | |
36 */ | |
37 private final MidiDevice mDevice; | |
38 /** | |
39 * The index of the port in the associated device. | |
40 */ | |
41 private final int mIndex; | |
42 | |
43 /** | |
44 * constructor | |
45 * @param device the device this port belongs to. | |
46 * @param index the index of the port in the associated device. | |
47 */ | |
48 MidiInputPortAndroid(MidiDevice device, int index) { | |
49 mDevice = device; | |
50 mIndex = index; | |
51 } | |
52 | |
53 /** | |
54 * Registers this object to the underlying port so as to the C++ function wi
ll be called with | |
55 * the given C++ object when data arrives. | |
56 * @param nativeReceiverPointer a pointer to a media::midi::MidiInputPortAnd
roid object. | |
57 * @return true if this operation succeeds or the port is already open. | |
58 */ | |
59 @CalledByNative | |
60 boolean open(long nativeReceiverPointer) { | |
61 if (mPort != null) { | |
62 return true; | |
63 } | |
64 mPort = mDevice.openOutputPort(mIndex); | |
65 if (mPort == null) { | |
66 return false; | |
67 } | |
68 mNativeReceiverPointer = nativeReceiverPointer; | |
69 mPort.connect(new MidiReceiver() { | |
70 @Override | |
71 public void onSend(byte[] bs, int offset, int count, long timestamp)
{ | |
72 nativeOnData(mNativeReceiverPointer, bs, offset, count, timestam
p); | |
73 } | |
74 }); | |
75 return true; | |
76 } | |
77 | |
78 /** | |
79 * Closes the port. | |
80 */ | |
81 @CalledByNative | |
82 void close() { | |
83 if (mPort == null) { | |
84 return; | |
85 } | |
86 try { | |
87 mPort.close(); | |
88 } catch (IOException e) { | |
89 // We can do nothing here. Just ignore the error. | |
90 } | |
91 mNativeReceiverPointer = 0; | |
92 mPort = null; | |
93 } | |
94 | |
95 private static native void nativeOnData( | |
96 long nativeMidiInputPortAndroid, byte[] bs, int offset, int count, l
ong timestamp); | |
97 } | |
OLD | NEW |