diff options
author | Mike Lockwood <lockwood@google.com> | 2015-03-11 10:55:41 +0000 |
---|---|---|
committer | Android (Google) Code Review <android-gerrit@google.com> | 2015-03-11 10:55:43 +0000 |
commit | 22e78c65beb9370d4c14a09eb56a94810e7c7375 (patch) | |
tree | 7dfac297d88b761173e77b948eabd88c98c2f18d | |
parent | 38bcb8c9ab2354769723a41ff9bff13a3ddfd031 (diff) | |
parent | 0c7342f0153076c88ba8e6a1647999c248787906 (diff) | |
download | frameworks_base-22e78c65beb9370d4c14a09eb56a94810e7c7375.zip frameworks_base-22e78c65beb9370d4c14a09eb56a94810e7c7375.tar.gz frameworks_base-22e78c65beb9370d4c14a09eb56a94810e7c7375.tar.bz2 |
Merge "Add MidiReceiver.send() and MidiReceiver.getMaxMessageSize()"
5 files changed, 48 insertions, 14 deletions
diff --git a/media/java/android/media/midi/MidiDispatcher.java b/media/java/android/media/midi/MidiDispatcher.java index a5193f1..90789e5 100644 --- a/media/java/android/media/midi/MidiDispatcher.java +++ b/media/java/android/media/midi/MidiDispatcher.java @@ -75,9 +75,9 @@ public final class MidiDispatcher extends MidiReceiver { public void receive(byte[] msg, int offset, int count, long timestamp) throws IOException { for (MidiReceiver receiver : mReceivers) { try { - receiver.receive(msg, offset, count, timestamp); + receiver.send(msg, offset, count, timestamp); } catch (IOException e) { - // if the receiver fails we remove the receiver but do not propogate the exception + // if the receiver fails we remove the receiver but do not propagate the exception mReceivers.remove(receiver); } } diff --git a/media/java/android/media/midi/MidiInputPort.java b/media/java/android/media/midi/MidiInputPort.java index 8674969..74e1fa4 100644 --- a/media/java/android/media/midi/MidiInputPort.java +++ b/media/java/android/media/midi/MidiInputPort.java @@ -81,22 +81,25 @@ public final class MidiInputPort extends MidiReceiver implements Closeable { * {@link java.lang.System#nanoTime} */ public void receive(byte[] msg, int offset, int count, long timestamp) throws IOException { - assert(offset >= 0 && count >= 0 && offset + count <= msg.length); + if (offset < 0 || count < 0 || offset + count > msg.length) { + throw new IllegalArgumentException("offset or count out of range"); + } + if (count > MidiPortImpl.MAX_PACKET_DATA_SIZE) { + throw new IllegalArgumentException("count exceeds max message size"); + } synchronized (mBuffer) { - while (count > 0) { - int length = MidiPortImpl.packMessage(msg, offset, count, timestamp, mBuffer); - mOutputStream.write(mBuffer, 0, length); - int sent = MidiPortImpl.getMessageSize(mBuffer, length); - assert(sent >= 0 && sent <= length); - - offset += sent; - count -= sent; - } + int length = MidiPortImpl.packMessage(msg, offset, count, timestamp, mBuffer); + mOutputStream.write(mBuffer, 0, length); } } @Override + public int getMaxMessageSize() { + return MidiPortImpl.MAX_PACKET_DATA_SIZE; + } + + @Override public void close() throws IOException { synchronized (mGuard) { if (mIsClosed) return; diff --git a/media/java/android/media/midi/MidiOutputPort.java b/media/java/android/media/midi/MidiOutputPort.java index b31cdd3..dcfb4ff 100644 --- a/media/java/android/media/midi/MidiOutputPort.java +++ b/media/java/android/media/midi/MidiOutputPort.java @@ -68,7 +68,7 @@ public final class MidiOutputPort extends MidiSender implements Closeable { long timestamp = MidiPortImpl.getMessageTimeStamp(buffer, count); // dispatch to all our receivers - mDispatcher.receive(buffer, offset, size, timestamp); + mDispatcher.send(buffer, offset, size, timestamp); } } catch (IOException e) { // FIXME report I/O failure? diff --git a/media/java/android/media/midi/MidiReceiver.java b/media/java/android/media/midi/MidiReceiver.java index 674c974..7212be9 100644 --- a/media/java/android/media/midi/MidiReceiver.java +++ b/media/java/android/media/midi/MidiReceiver.java @@ -27,6 +27,7 @@ import java.io.IOException; abstract public class MidiReceiver { /** * Called to pass MIDI data to the receiver. + * May fail if count exceeds {@link getMaxMessageSize}. * * NOTE: the msg array parameter is only valid within the context of this call. * The msg bytes should be copied by the receiver rather than retaining a reference @@ -42,4 +43,34 @@ abstract public class MidiReceiver { */ abstract public void receive(byte[] msg, int offset, int count, long timestamp) throws IOException; + + /** + * Returns the maximum size of a message this receiver can receive. + * Defaults to {@link java.lang.Integer#MAX_VALUE} unless overridden. + * @return maximum message size + */ + public int getMaxMessageSize() { + return Integer.MAX_VALUE; + } + + /** + * Called to send MIDI data to the receiver + * Data will get split into multiple calls to {@link receive} if count exceeds + * {@link getMaxMessageSize}. + * + * @param msg a byte array containing the MIDI data + * @param offset the offset of the first byte of the data in the byte array + * @param count the number of bytes of MIDI data in the array + * @param timestamp the timestamp of the message (based on {@link java.lang.System#nanoTime} + * @throws IOException + */ + public void send(byte[] msg, int offset, int count, long timestamp) throws IOException { + int messageSize = getMaxMessageSize(); + while (count > 0) { + int length = (count > messageSize ? messageSize : count); + receive(msg, offset, length, timestamp); + offset += length; + count -= length; + } + } } diff --git a/services/usb/java/com/android/server/usb/UsbMidiDevice.java b/services/usb/java/com/android/server/usb/UsbMidiDevice.java index 51d61bb..f927965 100644 --- a/services/usb/java/com/android/server/usb/UsbMidiDevice.java +++ b/services/usb/java/com/android/server/usb/UsbMidiDevice.java @@ -144,7 +144,7 @@ public final class UsbMidiDevice implements Closeable { int count = mInputStreams[index].read(buffer); long timestamp = System.nanoTime(); - outputReceivers[index].receive(buffer, 0, count, timestamp); + outputReceivers[index].send(buffer, 0, count, timestamp); } else if ((pfd.revents & (OsConstants.POLLERR | OsConstants.POLLHUP)) != 0) { done = true; |