android-midisuite icon indicating copy to clipboard operation
android-midisuite copied to clipboard

review closing of a MidiDevice by MidiPortSelector

Open philburk opened this issue 7 years ago • 3 comments

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.

philburk avatar Feb 06 '18 02:02 philburk

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.

robertwu1 avatar May 16 '22 21:05 robertwu1

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;
            }
        }

robertwu1 avatar May 16 '22 21:05 robertwu1

Or we could cache open devices and not reopen the device if we find it in the cache.

philburk avatar May 22 '22 18:05 philburk