android-midisuite
android-midisuite copied to clipboard
review closing of a MidiDevice by MidiPortSelector
Mario wrote:
If I have 2 selectors, and they both select (different) ports on the same device, when one of the spinners changes selection, the close() method is called on the device. But this device is being used by the other selector, is it reference counted internally? What does it mean to close it, while a port might be still in use elsewhere?
Good question. This needs to be investigated.
If the same port is being used for both input and output, we should not open the device twice. We should instead open the device only once and use the same device for both the input and output ports.
Below is what close looks like internally. We should not two separate port selectors call close at the same time. We should expose something like MidiInputOutputPortSelector if developers need to use the same port for both input and output in the same app.
@Override
public void close() throws IOException {
synchronized (mGuard) {
if (mIsClosed) return;
mGuard.close();
try {
// close input port
mInputPortDeviceServer.closePort(mInputPortToken);
// close output port
mDeviceServer.closePort(mOutputPortToken);
} catch (RemoteException e) {
Log.e(TAG, "RemoteException in MidiConnection.close");
}
mIsClosed = true;
}
}
Or we could cache open devices and not reopen the device if we find it in the cache.