Make MidiSender and MidiReceiver abstract classes, rename MidiReceiver.post() to receive()

Change-Id: I1cef3bd48ca0acf2968c9de223f78445f3434404
This commit is contained in:
Mike Lockwood
2015-03-04 14:50:39 -08:00
parent 20821ecbe8
commit 3b7664589b
7 changed files with 34 additions and 44 deletions

View File

@@ -85,10 +85,10 @@ public final class MidiDeviceServer implements Closeable {
outputPort.connect(new MidiReceiver() {
@Override
public void post(byte[] msg, int offset, int count, long timestamp)
public void receive(byte[] msg, int offset, int count, long timestamp)
throws IOException {
try {
inputPortReceviver.post(msg, offset, count, timestamp);
inputPortReceviver.receive(msg, offset, count, timestamp);
} catch (IOException e) {
IoUtils.closeQuietly(mInputPortOutputPorts[portNumberF]);
mInputPortOutputPorts[portNumberF] = null;
@@ -125,10 +125,10 @@ public final class MidiDeviceServer implements Closeable {
final MidiSender sender = mOutputPortDispatchers[portNumber].getSender();
sender.connect(new MidiReceiver() {
@Override
public void post(byte[] msg, int offset, int count, long timestamp)
public void receive(byte[] msg, int offset, int count, long timestamp)
throws IOException {
try {
inputPort.post(msg, offset, count, timestamp);
inputPort.receive(msg, offset, count, timestamp);
} catch (IOException e) {
IoUtils.closeQuietly(inputPort);
sender.disconnect(this);

View File

@@ -24,12 +24,12 @@ import java.util.ArrayList;
* This class subclasses {@link MidiReceiver} and dispatches any data it receives
* to its receiver list. Any receivers that throw an exception upon receiving data will
* be automatically removed from the receiver list, but no IOException will be returned
* from the dispatcher's {@link #post} in that case.
* from the dispatcher's {@link #receive} in that case.
*
* CANDIDATE FOR PUBLIC API
* @hide
*/
public class MidiDispatcher implements MidiReceiver {
public class MidiDispatcher extends MidiReceiver {
private final ArrayList<MidiReceiver> mReceivers = new ArrayList<MidiReceiver>();
@@ -71,12 +71,12 @@ public class MidiDispatcher implements MidiReceiver {
}
@Override
public void post(byte[] msg, int offset, int count, long timestamp) throws IOException {
public void receive(byte[] msg, int offset, int count, long timestamp) throws IOException {
synchronized (mReceivers) {
for (int i = 0; i < mReceivers.size(); ) {
MidiReceiver receiver = mReceivers.get(i);
try {
receiver.post(msg, offset, count, timestamp);
receiver.receive(msg, offset, count, timestamp);
i++; // increment only on success. on failure we remove the receiver
// so i should not be incremented
} catch (IOException e) {

View File

@@ -30,12 +30,12 @@ import java.io.IOException;
* CANDIDATE FOR PUBLIC API
* @hide
*/
public class MidiInputPort implements MidiReceiver, Closeable {
public class MidiInputPort extends MidiReceiver implements Closeable {
private final int mPortNumber;
private final FileOutputStream mOutputStream;
// buffer to use for sending messages out our output stream
// buffer to use for sending data out our output stream
private final byte[] mBuffer = new byte[MidiPortImpl.MAX_PACKET_SIZE];
/* package */ MidiInputPort(ParcelFileDescriptor pfd, int portNumber) {
@@ -52,38 +52,27 @@ public class MidiInputPort implements MidiReceiver, Closeable {
return mPortNumber;
}
//FIXME
public void onIOException() {
}
/**
* Writes a MIDI message to the input port
* Writes MIDI data to the input port
*
* @param msg byte array containing the message
* @param offset offset of first byte of the message in msg byte array
* @param count size of the message in bytes
* @param timestamp future time to post the message (based on
* @param msg byte array containing the data
* @param offset offset of first byte of the data in msg byte array
* @param count size of the data in bytes
* @param timestamp future time to post the data (based on
* {@link java.lang.System#nanoTime}
*/
public void post(byte[] msg, int offset, int count, long timestamp) throws IOException {
public void receive(byte[] msg, int offset, int count, long timestamp) throws IOException {
assert(offset >= 0 && count >= 0 && offset + count <= msg.length);
synchronized (mBuffer) {
try {
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);
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;
}
} catch (IOException e) {
IoUtils.closeQuietly(mOutputStream);
// report I/O failure
onIOException();
throw e;
offset += sent;
count -= sent;
}
}
}

View File

@@ -31,7 +31,7 @@ import java.io.IOException;
* CANDIDATE FOR PUBLIC API
* @hide
*/
public class MidiOutputPort implements MidiSender, Closeable {
public class MidiOutputPort extends MidiSender implements Closeable {
private static final String TAG = "MidiOutputPort";
private final int mPortNumber;
@@ -59,7 +59,7 @@ public class MidiOutputPort implements MidiSender, Closeable {
long timestamp = MidiPortImpl.getMessageTimeStamp(buffer, count);
// dispatch to all our receivers
mDispatcher.post(buffer, offset, size, timestamp);
mDispatcher.receive(buffer, offset, size, timestamp);
}
} catch (IOException e) {
// FIXME report I/O failure?

View File

@@ -24,7 +24,7 @@ import java.io.IOException;
* CANDIDATE FOR PUBLIC API
* @hide
*/
public interface MidiReceiver {
abstract public class MidiReceiver {
/**
* Called to pass MIDI data to the receiver.
*
@@ -32,7 +32,7 @@ public interface MidiReceiver {
* The msg bytes should be copied by the receiver rather than retaining a reference
* to this parameter.
* Also, modifying the contents of the msg array parameter may result in other receivers
* in the same application receiving incorrect values in their post() method.
* in the same application receiving incorrect values in their receive() method.
*
* @param msg a byte array containing the MIDI data
* @param offset the offset of the first byte of the data in the byte array
@@ -40,5 +40,6 @@ public interface MidiReceiver {
* @param timestamp the timestamp of the message (based on {@link java.lang.System#nanoTime}
* @throws IOException
*/
public void post(byte[] msg, int offset, int count, long timestamp) throws IOException;
abstract public void receive(byte[] msg, int offset, int count, long timestamp)
throws IOException;
}

View File

@@ -23,18 +23,18 @@ package android.media.midi;
* CANDIDATE FOR PUBLIC API
* @hide
*/
public interface MidiSender {
abstract public class MidiSender {
/**
* Called to connect a {@link MidiReceiver} to the sender
*
* @param receiver the receiver to connect
*/
public void connect(MidiReceiver receiver);
abstract public void connect(MidiReceiver receiver);
/**
* Called to disconnect a {@link MidiReceiver} from the sender
*
* @param receiver the receiver to disconnect
*/
public void disconnect(MidiReceiver receiver);
abstract public void disconnect(MidiReceiver receiver);
}

View File

@@ -103,7 +103,7 @@ public final class UsbMidiDevice implements Closeable {
final int portF = port;
mInputPortReceivers[port] = new MidiReceiver() {
@Override
public void post(byte[] data, int offset, int count, long timestamp)
public void receive(byte[] data, int offset, int count, long timestamp)
throws IOException {
// FIXME - timestamps are ignored, future posting not supported yet.
mOutputStreams[portF].write(data, offset, count);
@@ -144,7 +144,7 @@ public final class UsbMidiDevice implements Closeable {
int count = mInputStreams[index].read(buffer);
long timestamp = System.nanoTime();
outputReceivers[index].post(buffer, 0, count, timestamp);
outputReceivers[index].receive(buffer, 0, count, timestamp);
} else if ((pfd.revents & (OsConstants.POLLERR
| OsConstants.POLLHUP)) != 0) {
done = true;