Eliminate MidiPort base class for MidiInputPort and MidiOutputPort

Change-Id: I628c0468ac980eee909add53a4d6e55e9b358603
This commit is contained in:
Mike Lockwood
2015-02-24 09:06:30 -08:00
parent 27b7c1f526
commit 20821ecbe8
4 changed files with 44 additions and 61 deletions

View File

@@ -20,6 +20,7 @@ import android.os.ParcelFileDescriptor;
import libcore.io.IoUtils;
import java.io.Closeable;
import java.io.FileOutputStream;
import java.io.IOException;
@@ -29,18 +30,32 @@ import java.io.IOException;
* CANDIDATE FOR PUBLIC API
* @hide
*/
public class MidiInputPort extends MidiPort implements MidiReceiver {
public class MidiInputPort implements MidiReceiver, Closeable {
private final int mPortNumber;
private final FileOutputStream mOutputStream;
// buffer to use for sending messages out our output stream
private final byte[] mBuffer = new byte[MAX_PACKET_SIZE];
private final byte[] mBuffer = new byte[MidiPortImpl.MAX_PACKET_SIZE];
/* package */ MidiInputPort(ParcelFileDescriptor pfd, int portNumber) {
super(portNumber);
mPortNumber = portNumber;
mOutputStream = new ParcelFileDescriptor.AutoCloseOutputStream(pfd);
}
/**
* Returns the port number of this port
*
* @return the port's port number
*/
public final int getPortNumber() {
return mPortNumber;
}
//FIXME
public void onIOException() {
}
/**
* Writes a MIDI message to the input port
*
@@ -56,9 +71,9 @@ public class MidiInputPort extends MidiPort implements MidiReceiver {
synchronized (mBuffer) {
try {
while (count > 0) {
int length = packMessage(msg, offset, count, timestamp, mBuffer);
int length = MidiPortImpl.packMessage(msg, offset, count, timestamp, mBuffer);
mOutputStream.write(mBuffer, 0, length);
int sent = getMessageSize(mBuffer, length);
int sent = MidiPortImpl.getMessageSize(mBuffer, length);
assert(sent >= 0 && sent <= length);
offset += sent;

View File

@@ -21,6 +21,7 @@ import android.util.Log;
import libcore.io.IoUtils;
import java.io.Closeable;
import java.io.FileInputStream;
import java.io.IOException;
@@ -30,9 +31,10 @@ import java.io.IOException;
* CANDIDATE FOR PUBLIC API
* @hide
*/
public class MidiOutputPort extends MidiPort implements MidiSender {
public class MidiOutputPort implements MidiSender, Closeable {
private static final String TAG = "MidiOutputPort";
private final int mPortNumber;
private final FileInputStream mInputStream;
private final MidiDispatcher mDispatcher = new MidiDispatcher();
@@ -41,7 +43,7 @@ public class MidiOutputPort extends MidiPort implements MidiSender {
private final Thread mThread = new Thread() {
@Override
public void run() {
byte[] buffer = new byte[MAX_PACKET_SIZE];
byte[] buffer = new byte[MidiPortImpl.MAX_PACKET_SIZE];
try {
while (true) {
@@ -52,9 +54,9 @@ public class MidiOutputPort extends MidiPort implements MidiSender {
// FIXME - inform receivers here?
}
int offset = getMessageOffset(buffer, count);
int size = getMessageSize(buffer, count);
long timestamp = getMessageTimeStamp(buffer, count);
int offset = MidiPortImpl.getMessageOffset(buffer, count);
int size = MidiPortImpl.getMessageSize(buffer, count);
long timestamp = MidiPortImpl.getMessageTimeStamp(buffer, count);
// dispatch to all our receivers
mDispatcher.post(buffer, offset, size, timestamp);
@@ -68,12 +70,21 @@ public class MidiOutputPort extends MidiPort implements MidiSender {
}
};
/* package */ MidiOutputPort(ParcelFileDescriptor pfd, int portNumber) {
super(portNumber);
/* package */ MidiOutputPort(ParcelFileDescriptor pfd, int portNumber) {
mPortNumber = portNumber;
mInputStream = new ParcelFileDescriptor.AutoCloseInputStream(pfd);
mThread.start();
}
/**
* Returns the port number of this port
*
* @return the port's port number
*/
public final int getPortNumber() {
return mPortNumber;
}
@Override
public void connect(MidiReceiver receiver) {
mDispatcher.getSender().connect(receiver);

View File

@@ -19,28 +19,19 @@ package android.media.midi;
import java.io.Closeable;
/**
* This class represents a MIDI input or output port.
* Base class for {@link MidiInputPort} and {@link MidiOutputPort}
*
* CANDIDATE FOR PUBLIC API
* @hide
* This class contains utilities for socket communication between a
* MidiInputPort and MidiOutputPort
*/
abstract public class MidiPort implements Closeable {
/* package */ class MidiPortImpl {
private static final String TAG = "MidiPort";
private final int mPortNumber;
/**
* Maximum size of a packet that can pass through our ParcelFileDescriptor.
* For internal use only. Implementation details may change in the future.
* @hide
*/
public static final int MAX_PACKET_SIZE = 1024;
/**
* size of message timestamp in bytes
* For internal use only. Implementation details may change in the future.
* @hide
*/
private static final int TIMESTAMP_SIZE = 8;
@@ -49,29 +40,6 @@ abstract public class MidiPort implements Closeable {
*/
public static final int MAX_PACKET_DATA_SIZE = MAX_PACKET_SIZE - TIMESTAMP_SIZE;
/* package */ MidiPort(int portNumber) {
mPortNumber = portNumber;
}
/**
* Returns the port number of this port
*
* @return the port's port number
*/
public final int getPortNumber() {
return mPortNumber;
}
/**
* Called when an IOExeption occurs while sending or receiving data.
* Subclasses can override to be notified of such errors
*
* @hide
*/
public void onIOException() {
}
/**
* Utility function for packing a MIDI message to be sent through our ParcelFileDescriptor
*
@@ -80,9 +48,6 @@ abstract public class MidiPort implements Closeable {
* timestamp is message timestamp to pack
* dest is buffer to pack into
* returns size of packed message
*
* For internal use only. Implementation details may change in the future.
* @hide
*/
public static int packMessage(byte[] message, int offset, int size, long timestamp,
byte[] dest) {
@@ -104,9 +69,6 @@ abstract public class MidiPort implements Closeable {
/**
* Utility function for unpacking a MIDI message received from our ParcelFileDescriptor
* returns the offset of the MIDI message in packed buffer
*
* For internal use only. Implementation details may change in the future.
* @hide
*/
public static int getMessageOffset(byte[] buffer, int bufferLength) {
// message is at the beginning
@@ -116,9 +78,6 @@ abstract public class MidiPort implements Closeable {
/**
* Utility function for unpacking a MIDI message received from our ParcelFileDescriptor
* returns size of MIDI data in packed buffer
*
* For internal use only. Implementation details may change in the future.
* @hide
*/
public static int getMessageSize(byte[] buffer, int bufferLength) {
// message length is total buffer length minus size of the timestamp
@@ -128,9 +87,6 @@ abstract public class MidiPort implements Closeable {
/**
* Utility function for unpacking a MIDI message received from our ParcelFileDescriptor
* unpacks timestamp from packed buffer
*
* For internal use only. Implementation details may change in the future.
* @hide
*/
public static long getMessageTimeStamp(byte[] buffer, int bufferLength) {
// timestamp is at end of the packet

View File

@@ -21,7 +21,6 @@ import android.media.midi.MidiDeviceInfo;
import android.media.midi.MidiDeviceServer;
import android.media.midi.MidiDispatcher;
import android.media.midi.MidiManager;
import android.media.midi.MidiPort;
import android.media.midi.MidiReceiver;
import android.media.midi.MidiSender;
import android.os.Bundle;
@@ -46,6 +45,8 @@ public final class UsbMidiDevice implements Closeable {
private final MidiReceiver[] mInputPortReceivers;
private static final int BUFFER_SIZE = 512;
// for polling multiple FileDescriptors for MIDI events
private final StructPollfd[] mPollFDs;
// streams for reading from ALSA driver
@@ -130,7 +131,7 @@ public final class UsbMidiDevice implements Closeable {
new Thread() {
@Override
public void run() {
byte[] buffer = new byte[MidiPort.MAX_PACKET_DATA_SIZE];
byte[] buffer = new byte[BUFFER_SIZE];
try {
boolean done = false;
while (!done) {