commit
efa6f355b06675aa4d0879fd279e22c16d5c046c
Author: Mikhail Naganov <mnaganov@google.com>
Date: Wed Aug 10 12:25:13 2016 -0700
MIDI: Use server-side socket in blocking mode for virtual devices
Since virtual MIDI servers may misbehave, blocking mode will throttle
them if clients are not coping with their sending speed.
Bug:
29413812
Change-Id: I9c4a2a7a7ea3ea060c93fedc7d0f033427c557c9
commit
755dfb5f83749d3963c63d98d692307f8271c804
Author: Mikhail Naganov <mnaganov@google.com>
Date: Fri Jul 8 13:26:19 2016 -0700
Protect MIDI framework against client blocks in MidiReceiver.onSend
Make the server-side socket non-blocking when creating MidiOutputPort
for clients. Thus if a client ceases to read from its side of the
socket pair, the server will just fail to write instead of blocking.
One drawback is that the MidiOutputPort on the client can't indicate
that it has become dysfunctional, but it's not possible without
changing the API.
Bug:
29413812
Change-Id: I9dfcbdd214a815cea8fd1365324fd78ca459268a
commit
c740b13953761f58233ac651a0b5227733b1bdcc
Author: Mikhail Naganov <mnaganov@google.com>
Date: Fri Jun 17 04:11:25 2016 -0700
UsbMidiDevice: Clean up terminology and fix comments
When working with physical MIDI devices, an *input* stream is used
for reading from *output* port of the device, and vice versa. Thus,
using "input" and "output" without specifying whether it's a stream
or a port is confusing.
Clarify names of counter variables, and fix a couple of comments
that were incorrect due to this confusion. No functional changes.
Change-Id: If561eaca4bade94e9296d2c703c9fcebc91296e2
commit
4269c6417287737624f6165a8bbeb5aa427de9a0
Author: Glenn Kasten <gkasten@google.com>
Date: Thu May 5 18:49:16 2016 -0700
Update MIDI package summary
Bug:
28625060
Change-Id: If552ca8e1a0666d402b5f536699bf3fb09c1e324
commit
862d40b73168bde7d0be5280d997985c18061014
Author: Phil Burk <philburk@google.com>
Date: Tue Apr 19 15:56:24 2016 -0700
MidiDevice: do not open ports on closed device
Fix involves client side mIsDeviceClosed flag.
Bug:
24949216
Change-Id: I666284a787fbb9a710d2372fb424e8e54f6a2825
Signed-off-by: Phil Burk <philburk@google.com>
commit
6f1de358b9f2616e03f4655f01454770915ddd66
Author: Phil Burk <philburk@google.com>
Date: Mon Apr 18 16:05:28 2016 -0700
MidiService: fix resource leak
The proxy object was being used to match when adding or removing objects.
But they are different each time. So now we use an asBinder() object.
Bug:
28153736
Change-Id: I1bccebf1e9464668db757ff08b41902d0cf0e3a7
Signed-off-by: Phil Burk <philburk@google.com>
commit
f7386bd535bb8a1d7f8df8f44a1748ab770c991a
Author: Phil Burk <philburk@google.com>
Date: Tue Apr 5 14:19:53 2016 -0700
MidiDevice: fix connectPorts for same Process
If connectPorts() was called for a device in the same process then
the connection would die when the ParcelFileDescriptor was closed.
Bug:
26406775
Change-Id: Id0538452593b4761ac2a93d366ade76d2e35ce73
Signed-off-by: Phil Burk <philburk@google.com>
Change-Id: I4dfc2a2cbaf04bf1a790ae2cb39bf74fb5bb16ac
* Utility class for dispatching MIDI data to a list of {@link android.media.midi.MidiReceiver}s.
* This class subclasses {@link android.media.midi.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 android.media.midi.MidiReceiver#onSend} in that case.
+ * be automatically removed from the receiver list. If a MidiReceiverFailureHandler has been
+ * provided to the MidiDispatcher, it will be notified about the failure, but the exception
+ * itself will be swallowed.
*/
public final class MidiDispatcher extends MidiReceiver {
+ // MidiDispatcher's client and MidiReceiver's owner can be different
+ // classes (e.g. MidiDeviceService is a client, but MidiDeviceServer is
+ // the owner), and errors occuring during sending need to be reported
+ // to the owner rather than to the sender.
+ //
+ // Note that the callbacks will be called on the sender's thread.
+ public interface MidiReceiverFailureHandler {
+ void onReceiverFailure(MidiReceiver receiver, IOException failure);
+ }
+
+ private final MidiReceiverFailureHandler mFailureHandler;
private final CopyOnWriteArrayList<MidiReceiver> mReceivers
= new CopyOnWriteArrayList<MidiReceiver>();
}
};
+ public MidiDispatcher() {
+ this(null);
+ }
+
+ public MidiDispatcher(MidiReceiverFailureHandler failureHandler) {
+ mFailureHandler = failureHandler;
+ }
+
/**
* Returns the number of {@link android.media.midi.MidiReceiver}s this dispatcher contains.
* @return the number of receivers
try {
receiver.send(msg, offset, count, timestamp);
} catch (IOException e) {
- // if the receiver fails we remove the receiver but do not propagate the exception
+ // If the receiver fails we remove the receiver but do not propagate the exception.
+ // Note that this may also happen if the client code stalls, and thus underlying
+ // MidiInputPort.onSend has raised IOException for EAGAIN / EWOULDBLOCK error.
mReceivers.remove(receiver);
+ if (mFailureHandler != null) {
+ mFailureHandler.onReceiverFailure(receiver, e);
+ }
}
}
}
@Override
public void onFlush() throws IOException {
for (MidiReceiver receiver : mReceivers) {
- receiver.flush();
+ try {
+ receiver.flush();
+ } catch (IOException e) {
+ // This is just a special case of 'send' thus handle in the same way.
+ mReceivers.remove(receiver);
+ if (mFailureHandler != null) {
+ mFailureHandler.onReceiverFailure(receiver, e);
+ }
+ }
}
}
}
void closeDevice();
// connects the input port pfd to the specified output port
- void connectPorts(IBinder token, in ParcelFileDescriptor pfd, int outputPortNumber);
+ // Returns the PID of the called process.
+ int connectPorts(IBinder token, in ParcelFileDescriptor pfd, int outputPortNumber);
MidiDeviceInfo getDeviceInfo();
void setDeviceInfo(in MidiDeviceInfo deviceInfo);
import android.os.Binder;
import android.os.IBinder;
import android.os.ParcelFileDescriptor;
+import android.os.Process;
import android.os.RemoteException;
import android.util.Log;
private final IMidiManager mMidiManager;
private final IBinder mClientToken;
private final IBinder mDeviceToken;
+ private boolean mIsDeviceClosed;
private final CloseGuard mGuard = CloseGuard.get();
* or null in case of failure.
*/
public MidiInputPort openInputPort(int portNumber) {
+ if (mIsDeviceClosed) {
+ return null;
+ }
try {
IBinder token = new Binder();
ParcelFileDescriptor pfd = mDeviceServer.openInputPort(token, portNumber);
* or null in case of failure.
*/
public MidiOutputPort openOutputPort(int portNumber) {
+ if (mIsDeviceClosed) {
+ return null;
+ }
try {
IBinder token = new Binder();
ParcelFileDescriptor pfd = mDeviceServer.openOutputPort(token, portNumber);
if (outputPortNumber < 0 || outputPortNumber >= mDeviceInfo.getOutputPortCount()) {
throw new IllegalArgumentException("outputPortNumber out of range");
}
+ if (mIsDeviceClosed) {
+ return null;
+ }
ParcelFileDescriptor pfd = inputPort.claimFileDescriptor();
if (pfd == null) {
return null;
}
- try {
+ try {
IBinder token = new Binder();
- mDeviceServer.connectPorts(token, pfd, outputPortNumber);
- // close our copy of the file descriptor
- IoUtils.closeQuietly(pfd);
+ int calleePid = mDeviceServer.connectPorts(token, pfd, outputPortNumber);
+ // If the service is a different Process then it will duplicate the pfd
+ // and we can safely close this one.
+ // But if the service is in the same Process then closing the pfd will
+ // kill the connection. So don't do that.
+ if (calleePid != Process.myPid()) {
+ // close our copy of the file descriptor
+ IoUtils.closeQuietly(pfd);
+ }
+
return new MidiConnection(token, inputPort);
} catch (RemoteException e) {
Log.e(TAG, "RemoteException in connectPorts");
@Override
public void close() throws IOException {
synchronized (mGuard) {
- mGuard.close();
- try {
- mMidiManager.closeDevice(mClientToken, mDeviceToken);
- } catch (RemoteException e) {
- Log.e(TAG, "RemoteException in closeDevice");
+ if (!mIsDeviceClosed) {
+ mGuard.close();
+ mIsDeviceClosed = true;
+ try {
+ mMidiManager.closeDevice(mClientToken, mDeviceToken);
+ } catch (RemoteException e) {
+ Log.e(TAG, "RemoteException in closeDevice");
+ }
}
}
}
private final Callback mCallback;
+ private final HashMap<IBinder, PortClient> mPortClients = new HashMap<IBinder, PortClient>();
+ private final HashMap<MidiInputPort, PortClient> mInputPortClients =
+ new HashMap<MidiInputPort, PortClient>();
+
public interface Callback {
/**
* Called to notify when an our device status has changed
abstract void close();
+ MidiInputPort getInputPort() {
+ return null;
+ }
+
@Override
public void binderDied() {
close();
mInputPorts.remove(mInputPort);
IoUtils.closeQuietly(mInputPort);
}
- }
- private final HashMap<IBinder, PortClient> mPortClients = new HashMap<IBinder, PortClient>();
+ @Override
+ MidiInputPort getInputPort() {
+ return mInputPort;
+ }
+ }
// Binder interface stub for receiving connection requests from clients
private final IMidiDeviceServer mServer = new IMidiDeviceServer.Stub() {
ParcelFileDescriptor[] pair = ParcelFileDescriptor.createSocketPair(
OsConstants.SOCK_SEQPACKET);
MidiInputPort inputPort = new MidiInputPort(pair[0], portNumber);
+ // Undo the default blocking-mode of the server-side socket for
+ // physical devices to avoid stalling the Java device handler if
+ // client app code gets stuck inside 'onSend' handler.
+ if (mDeviceInfo.getType() != MidiDeviceInfo.TYPE_VIRTUAL) {
+ IoUtils.setBlocking(pair[0].getFileDescriptor(), false);
+ }
MidiDispatcher dispatcher = mOutputPortDispatchers[portNumber];
synchronized (dispatcher) {
dispatcher.getSender().connect(inputPort);
synchronized (mPortClients) {
mPortClients.put(token, client);
}
+ synchronized (mInputPortClients) {
+ mInputPortClients.put(inputPort, client);
+ }
return pair[1];
} catch (IOException e) {
Log.e(TAG, "unable to create ParcelFileDescriptors in openOutputPort");
@Override
public void closePort(IBinder token) {
+ MidiInputPort inputPort = null;
synchronized (mPortClients) {
PortClient client = mPortClients.remove(token);
if (client != null) {
+ inputPort = client.getInputPort();
client.close();
}
}
+ if (inputPort != null) {
+ synchronized (mInputPortClients) {
+ mInputPortClients.remove(inputPort);
+ }
+ }
}
@Override
}
@Override
- public void connectPorts(IBinder token, ParcelFileDescriptor pfd,
+ public int connectPorts(IBinder token, ParcelFileDescriptor pfd,
int outputPortNumber) {
MidiInputPort inputPort = new MidiInputPort(pfd, outputPortNumber);
MidiDispatcher dispatcher = mOutputPortDispatchers[outputPortNumber];
synchronized (mPortClients) {
mPortClients.put(token, client);
}
+ synchronized (mInputPortClients) {
+ mInputPortClients.put(inputPort, client);
+ }
+ return Process.myPid(); // for caller to detect same process ID
}
@Override
mOutputPortDispatchers = new MidiDispatcher[numOutputPorts];
for (int i = 0; i < numOutputPorts; i++) {
- mOutputPortDispatchers[i] = new MidiDispatcher();
+ mOutputPortDispatchers[i] = new MidiDispatcher(mInputPortFailureHandler);
}
mInputPortOpen = new boolean[mInputPortCount];
mGuard.open("close");
}
+ private final MidiDispatcher.MidiReceiverFailureHandler mInputPortFailureHandler =
+ new MidiDispatcher.MidiReceiverFailureHandler() {
+ public void onReceiverFailure(MidiReceiver receiver, IOException failure) {
+ Log.e(TAG, "MidiInputPort failed to send data", failure);
+ PortClient client = null;
+ synchronized (mInputPortClients) {
+ client = mInputPortClients.remove(receiver);
+ }
+ if (client != null) {
+ client.close();
+ }
+ }
+ };
+
// Constructor for MidiDeviceService.onCreate()
/* package */ MidiDeviceServer(IMidiManager midiManager, MidiReceiver[] inputPortReceivers,
MidiDeviceInfo deviceInfo, Callback callback) {
}
} catch (IOException e) {
// FIXME report I/O failure?
- Log.e(TAG, "read failed");
+ Log.e(TAG, "read failed", e);
} finally {
IoUtils.closeQuietly(mInputStream);
}
<html>
<body>
-<p>Android MIDI User Guide</p>
-
-<h1 id=overview>Overview</h1>
+<p>
+Provides classes for sending and receiving messages using the standard MIDI
+event protocol over USB, Bluetooth LE, and virtual (inter-app) transports.
+</p>
-<p>This document describes how to use the Android MIDI API in Java.</p>
+<h1 id=overview>Overview</h1>
<p>The Android MIDI package allows users to:</p>
// This client's PID
private final int mPid;
// List of all receivers for this client
- private final ArrayList<IMidiDeviceListener> mListeners
- = new ArrayList<IMidiDeviceListener>();
+ private final HashMap<IBinder, IMidiDeviceListener> mListeners
+ = new HashMap<IBinder, IMidiDeviceListener>();
// List of all device connections for this client
private final HashMap<IBinder, DeviceConnection> mDeviceConnections
= new HashMap<IBinder, DeviceConnection>();
}
public void addListener(IMidiDeviceListener listener) {
- mListeners.add(listener);
+ // Use asBinder() so that we can match it in removeListener().
+ // The listener proxy objects themselves do not match.
+ mListeners.put(listener.asBinder(), listener);
}
public void removeListener(IMidiDeviceListener listener) {
- mListeners.remove(listener);
+ mListeners.remove(listener.asBinder());
if (mListeners.size() == 0 && mDeviceConnections.size() == 0) {
close();
}
MidiDeviceInfo deviceInfo = device.getDeviceInfo();
try {
- for (IMidiDeviceListener listener : mListeners) {
+ for (IMidiDeviceListener listener : mListeners.values()) {
listener.onDeviceAdded(deviceInfo);
}
} catch (RemoteException e) {
MidiDeviceInfo deviceInfo = device.getDeviceInfo();
try {
- for (IMidiDeviceListener listener : mListeners) {
+ for (IMidiDeviceListener listener : mListeners.values()) {
listener.onDeviceRemoved(deviceInfo);
}
} catch (RemoteException e) {
if (!device.isUidAllowed(mUid)) return;
try {
- for (IMidiDeviceListener listener : mListeners) {
+ for (IMidiDeviceListener listener : mListeners.values()) {
listener.onDeviceStatusChanged(status);
}
} catch (RemoteException e) {
private MidiDeviceServer mServer;
- // event schedulers for each output port
+ // event schedulers for each input port of the physical device
private MidiEventScheduler[] mEventSchedulers;
private static final int BUFFER_SIZE = 512;
mSubdeviceCount = subdeviceCount;
// FIXME - support devices with different number of input and output ports
- int inputCount = subdeviceCount;
- mInputPortReceivers = new InputReceiverProxy[inputCount];
- for (int port = 0; port < inputCount; port++) {
+ int inputPortCount = subdeviceCount;
+ mInputPortReceivers = new InputReceiverProxy[inputPortCount];
+ for (int port = 0; port < inputPortCount; port++) {
mInputPortReceivers[port] = new InputReceiverProxy();
}
}
}
mFileDescriptors = fileDescriptors;
- int inputCount = fileDescriptors.length;
+ int inputStreamCount = fileDescriptors.length;
// last file descriptor returned from nativeOpen() is only used for unblocking Os.poll()
// in our input thread
- int outputCount = fileDescriptors.length - 1;
+ int outputStreamCount = fileDescriptors.length - 1;
- mPollFDs = new StructPollfd[inputCount];
- mInputStreams = new FileInputStream[inputCount];
- for (int i = 0; i < inputCount; i++) {
+ mPollFDs = new StructPollfd[inputStreamCount];
+ mInputStreams = new FileInputStream[inputStreamCount];
+ for (int i = 0; i < inputStreamCount; i++) {
FileDescriptor fd = fileDescriptors[i];
StructPollfd pollfd = new StructPollfd();
pollfd.fd = fd;
mInputStreams[i] = new FileInputStream(fd);
}
- mOutputStreams = new FileOutputStream[outputCount];
- mEventSchedulers = new MidiEventScheduler[outputCount];
- for (int i = 0; i < outputCount; i++) {
+ mOutputStreams = new FileOutputStream[outputStreamCount];
+ mEventSchedulers = new MidiEventScheduler[outputStreamCount];
+ for (int i = 0; i < outputStreamCount; i++) {
mOutputStreams[i] = new FileOutputStream(fileDescriptors[i]);
MidiEventScheduler scheduler = new MidiEventScheduler();
final MidiReceiver[] outputReceivers = mServer.getOutputPortReceivers();
- // Create input thread which will read from all input ports
+ // Create input thread which will read from all output ports of the physical device
new Thread("UsbMidiDevice input thread") {
@Override
public void run() {
}
}.start();
- // Create output thread for each output port
- for (int port = 0; port < outputCount; port++) {
+ // Create output thread for each input port of the physical device
+ for (int port = 0; port < outputStreamCount; port++) {
final MidiEventScheduler eventSchedulerF = mEventSchedulers[port];
final FileOutputStream outputStreamF = mOutputStreams[port];
final int portF = port;