diff options
author | Mike Lockwood <lockwood@google.com> | 2015-04-08 08:45:37 -0700 |
---|---|---|
committer | Mike Lockwood <lockwood@google.com> | 2015-04-09 11:49:45 -0700 |
commit | b6f50d357bd3d4d296be6bb047f5ce93a79cbca1 (patch) | |
tree | bd043d6a6eaa2d87ca542206b8a72fc163dd1a77 /services/usb/java | |
parent | 52f57de9f618f4b3303fb9b59084e775f5312984 (diff) | |
download | frameworks_base-b6f50d357bd3d4d296be6bb047f5ce93a79cbca1.zip frameworks_base-b6f50d357bd3d4d296be6bb047f5ce93a79cbca1.tar.gz frameworks_base-b6f50d357bd3d4d296be6bb047f5ce93a79cbca1.tar.bz2 |
Add support for flushing MIDI all scheduled MIDI events on a port
Change-Id: I39d7862540d4d4b9e2df1265f9dd253541adb4c2
Diffstat (limited to 'services/usb/java')
-rw-r--r-- | services/usb/java/com/android/server/usb/UsbMidiDevice.java | 66 |
1 files changed, 38 insertions, 28 deletions
diff --git a/services/usb/java/com/android/server/usb/UsbMidiDevice.java b/services/usb/java/com/android/server/usb/UsbMidiDevice.java index 6ece888..671cf01 100644 --- a/services/usb/java/com/android/server/usb/UsbMidiDevice.java +++ b/services/usb/java/com/android/server/usb/UsbMidiDevice.java @@ -45,7 +45,8 @@ public final class UsbMidiDevice implements Closeable { private MidiDeviceServer mServer; - private final MidiEventScheduler mEventScheduler; + // event schedulers for each output port + private final MidiEventScheduler[] mEventSchedulers; private static final int BUFFER_SIZE = 512; @@ -99,10 +100,11 @@ public final class UsbMidiDevice implements Closeable { } mOutputStreams = new FileOutputStream[outputCount]; + mEventSchedulers = new MidiEventScheduler[outputCount]; for (int i = 0; i < outputCount; i++) { mOutputStreams[i] = new FileOutputStream(fileDescriptors[i]); + mEventSchedulers[i] = new MidiEventScheduler(); } - mEventScheduler = new MidiEventScheduler(inputCount); } private boolean register(Context context, Bundle properties) { @@ -116,7 +118,7 @@ public final class UsbMidiDevice implements Closeable { int outputCount = mOutputStreams.length; MidiReceiver[] inputPortReceivers = new MidiReceiver[inputCount]; for (int port = 0; port < inputCount; port++) { - inputPortReceivers[port] = mEventScheduler.getReceiver(port); + inputPortReceivers[port] = mEventSchedulers[port].getReceiver(); } mServer = midiManager.createDeviceServer(inputPortReceivers, outputCount, @@ -126,7 +128,7 @@ public final class UsbMidiDevice implements Closeable { } final MidiReceiver[] outputReceivers = mServer.getOutputPortReceivers(); - // Create input thread + // Create input thread which will read from all input ports new Thread("UsbMidiDevice input thread") { @Override public void run() { @@ -161,38 +163,46 @@ public final class UsbMidiDevice implements Closeable { } }.start(); - // Create output thread - new Thread("UsbMidiDevice output thread") { - @Override - public void run() { - while (true) { - MidiEvent event; - try { - event = (MidiEvent)mEventScheduler.waitNextEvent(); - } catch (InterruptedException e) { - // try again - continue; - } - if (event == null) { - break; - } - try { - mOutputStreams[event.portNumber].write(event.data, 0, event.count); - } catch (IOException e) { - Log.e(TAG, "write failed for port " + event.portNumber); + // Create output thread for each output port + for (int port = 0; port < outputCount; port++) { + final MidiEventScheduler eventSchedulerF = mEventSchedulers[port]; + final FileOutputStream outputStreamF = mOutputStreams[port]; + final int portF = port; + + new Thread("UsbMidiDevice output thread " + port) { + @Override + public void run() { + while (true) { + MidiEvent event; + try { + event = (MidiEvent)eventSchedulerF.waitNextEvent(); + } catch (InterruptedException e) { + // try again + continue; + } + if (event == null) { + break; + } + try { + outputStreamF.write(event.data, 0, event.count); + } catch (IOException e) { + Log.e(TAG, "write failed for port " + portF); + } + eventSchedulerF.addEventToPool(event); } - mEventScheduler.addEventToPool(event); + Log.d(TAG, "output thread exit"); } - Log.d(TAG, "output thread exit"); - } - }.start(); + }.start(); + } return true; } @Override public void close() throws IOException { - mEventScheduler.close(); + for (int i = 0; i < mEventSchedulers.length; i++) { + mEventSchedulers[i].close(); + } if (mServer != null) { mServer.close(); |