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.MidiInputPort; | |
10 import android.os.Build; | |
11 | |
12 import org.chromium.base.Log; | |
13 import org.chromium.base.annotations.CalledByNative; | |
14 import org.chromium.base.annotations.JNINamespace; | |
15 | |
16 import java.io.IOException; | |
17 | |
18 /** | |
19 * A class implementing media::midi::MidiOutputPortAndroid functionality. | |
20 */ | |
21 // Note "OutputPort" is named in the Web MIDI manner. It corresponds to MidiInpu
tPort class in the | |
22 // Android API. | |
23 @JNINamespace("media::midi") | |
24 @TargetApi(Build.VERSION_CODES.M) | |
25 class MidiOutputPortAndroid { | |
26 /** | |
27 * The underlying port. | |
28 */ | |
29 private MidiInputPort mPort; | |
30 /** | |
31 * The device this port belongs to. | |
32 */ | |
33 private final MidiDevice mDevice; | |
34 /** | |
35 * The index of the port in the associated device. | |
36 */ | |
37 private final int mIndex; | |
38 | |
39 private static final String TAG = "media_midi"; | |
40 | |
41 /** | |
42 * constructor | |
43 * @param device The device this port belongs to. | |
44 * @param index The index of the port in the associated device. | |
45 */ | |
46 MidiOutputPortAndroid(MidiDevice device, int index) { | |
47 mDevice = device; | |
48 mIndex = index; | |
49 } | |
50 | |
51 /** | |
52 * Opens this port. | |
53 * @return true when the operation succeeds or the port is already open. | |
54 */ | |
55 @CalledByNative | |
56 boolean open() { | |
57 if (mPort != null) { | |
58 return true; | |
59 } | |
60 mPort = mDevice.openInputPort(mIndex); | |
61 return mPort != null; | |
62 } | |
63 | |
64 /** | |
65 * Sends the data to the underlying output port. | |
66 */ | |
67 @CalledByNative | |
68 void send(byte[] bs) { | |
69 if (mPort == null) { | |
70 return; | |
71 } | |
72 try { | |
73 mPort.send(bs, 0, bs.length); | |
74 } catch (IOException e) { | |
75 // We can do nothing here. Just ignore the error. | |
76 Log.e(TAG, "MidiOutputPortAndroid.send: " + e); | |
77 } | |
78 } | |
79 | |
80 /** | |
81 * Closes the port. | |
82 */ | |
83 @CalledByNative | |
84 void close() { | |
85 if (mPort == null) { | |
86 return; | |
87 } | |
88 try { | |
89 mPort.close(); | |
90 } catch (IOException e) { | |
91 // We can do nothing here. Just ignore the error. | |
92 } | |
93 mPort = null; | |
94 } | |
95 } | |
OLD | NEW |