Mikhail Naganov | c276c59 | 2016-08-31 18:08:10 -0700 | [diff] [blame] | 1 | /* |
Paul McLean | 8a3e33b | 2017-03-14 15:20:51 -0700 | [diff] [blame] | 2 | * Copyright (C) 2018 The Android Open Source Project |
Mikhail Naganov | c276c59 | 2016-08-31 18:08:10 -0700 | [diff] [blame] | 3 | * |
| 4 | * Licensed under the Apache License, Version 2.0 (the "License"); |
| 5 | * you may not use this file except in compliance with the License. |
| 6 | * You may obtain a copy of the License at |
| 7 | * |
| 8 | * http://www.apache.org/licenses/LICENSE-2.0 |
| 9 | * |
| 10 | * Unless required by applicable law or agreed to in writing, software |
| 11 | * distributed under the License is distributed on an "AS IS" BASIS, |
| 12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 13 | * See the License for the specific language governing permissions and |
| 14 | * limitations under the License. |
| 15 | */ |
| 16 | |
| 17 | #define LOG_TAG "NativeMIDI" |
| 18 | |
| 19 | #include <poll.h> |
| 20 | #include <unistd.h> |
| 21 | |
| 22 | #include <binder/Binder.h> |
Paul McLean | 8a3e33b | 2017-03-14 15:20:51 -0700 | [diff] [blame] | 23 | #include <android_util_Binder.h> |
Mikhail Naganov | c276c59 | 2016-08-31 18:08:10 -0700 | [diff] [blame] | 24 | #include <utils/Log.h> |
| 25 | |
Paul McLean | 8a3e33b | 2017-03-14 15:20:51 -0700 | [diff] [blame] | 26 | #include <core_jni_helpers.h> |
| 27 | |
Mikhail Naganov | c276c59 | 2016-08-31 18:08:10 -0700 | [diff] [blame] | 28 | #include "android/media/midi/BpMidiDeviceServer.h" |
Marco Nelissen | 9267280 | 2019-10-22 13:44:43 -0700 | [diff] [blame] | 29 | #include "MidiDeviceInfo.h" |
Mikhail Naganov | c276c59 | 2016-08-31 18:08:10 -0700 | [diff] [blame] | 30 | |
Paul McLean | f79b8d1 | 2019-02-06 13:27:43 -0700 | [diff] [blame] | 31 | #include "include/amidi/AMidi.h" |
| 32 | #include "amidi_internal.h" |
Mikhail Naganov | c276c59 | 2016-08-31 18:08:10 -0700 | [diff] [blame] | 33 | |
Paul McLean | 8a3e33b | 2017-03-14 15:20:51 -0700 | [diff] [blame] | 34 | using namespace android::media::midi; |
| 35 | |
Mikhail Naganov | c276c59 | 2016-08-31 18:08:10 -0700 | [diff] [blame] | 36 | using android::IBinder; |
| 37 | using android::BBinder; |
| 38 | using android::OK; |
| 39 | using android::sp; |
| 40 | using android::status_t; |
| 41 | using android::base::unique_fd; |
| 42 | using android::binder::Status; |
Paul McLean | 71f672b | 2017-03-05 08:12:18 -0700 | [diff] [blame] | 43 | |
| 44 | struct AMIDI_Port { |
Paul McLean | 8a3e33b | 2017-03-14 15:20:51 -0700 | [diff] [blame] | 45 | std::atomic_int state; // One of the port status constants below. |
| 46 | const AMidiDevice *device; // Points to the AMidiDevice associated with the port. |
| 47 | sp<IBinder> binderToken;// The Binder token associated with the port. |
| 48 | unique_fd ufd; // The unique file descriptor associated with the port. |
Paul McLean | 71f672b | 2017-03-05 08:12:18 -0700 | [diff] [blame] | 49 | }; |
Mikhail Naganov | c276c59 | 2016-08-31 18:08:10 -0700 | [diff] [blame] | 50 | |
Paul McLean | 8a3e33b | 2017-03-14 15:20:51 -0700 | [diff] [blame] | 51 | /* |
| 52 | * Port Status Constants |
| 53 | */ |
Paul McLean | 71f672b | 2017-03-05 08:12:18 -0700 | [diff] [blame] | 54 | enum { |
| 55 | MIDI_PORT_STATE_CLOSED = 0, |
| 56 | MIDI_PORT_STATE_OPEN_IDLE, |
| 57 | MIDI_PORT_STATE_OPEN_ACTIVE |
| 58 | }; |
| 59 | |
Paul McLean | 8a3e33b | 2017-03-14 15:20:51 -0700 | [diff] [blame] | 60 | /* |
| 61 | * Port Type Constants |
| 62 | */ |
Paul McLean | 71f672b | 2017-03-05 08:12:18 -0700 | [diff] [blame] | 63 | enum { |
| 64 | PORTTYPE_OUTPUT = 0, |
| 65 | PORTTYPE_INPUT = 1 |
| 66 | }; |
| 67 | |
Mikhail Naganov | c276c59 | 2016-08-31 18:08:10 -0700 | [diff] [blame] | 68 | /* TRANSFER PACKET FORMAT (as defined in MidiPortImpl.java) |
| 69 | * |
| 70 | * Transfer packet format is as follows (see MidiOutputPort.mThread.run() to see decomposition): |
| 71 | * |oc|md|md| ......... |md|ts|ts|ts|ts|ts|ts|ts|ts| |
| 72 | * ^ +--------------------+-----------------------+ |
| 73 | * | ^ ^ |
| 74 | * | | | |
| 75 | * | | + timestamp (8 bytes) |
| 76 | * | | |
| 77 | * | + MIDI data bytes (numBytes bytes) |
| 78 | * | |
| 79 | * + OpCode (AMIDI_OPCODE_DATA) |
| 80 | * |
| 81 | * NOTE: The socket pair is configured to use SOCK_SEQPACKET mode. |
| 82 | * SOCK_SEQPACKET, for a sequenced-packet socket that is connection-oriented, preserves message |
| 83 | * boundaries, and delivers messages in the order that they were sent. |
| 84 | * So 'read()' always returns a whole message. |
| 85 | */ |
Paul McLean | 8a3e33b | 2017-03-14 15:20:51 -0700 | [diff] [blame] | 86 | #define AMIDI_PACKET_SIZE 1024 |
| 87 | #define AMIDI_PACKET_OVERHEAD 9 |
| 88 | #define AMIDI_BUFFER_SIZE (AMIDI_PACKET_SIZE - AMIDI_PACKET_OVERHEAD) |
| 89 | |
Paul McLean | 84b49130 | 2018-05-09 10:03:57 -0600 | [diff] [blame] | 90 | // JNI IDs (see android_media_midi.cpp) |
| 91 | namespace android { namespace midi { |
Paul McLean | 8a3e33b | 2017-03-14 15:20:51 -0700 | [diff] [blame] | 92 | // MidiDevice Fields |
Paul McLean | 84b49130 | 2018-05-09 10:03:57 -0600 | [diff] [blame] | 93 | extern jfieldID gFidMidiNativeHandle; // MidiDevice.mNativeHandle |
| 94 | extern jfieldID gFidMidiDeviceServerBinder; // MidiDevice.mDeviceServerBinder |
| 95 | extern jfieldID gFidMidiDeviceInfo; // MidiDevice.mDeviceInfo |
Paul McLean | 8a3e33b | 2017-03-14 15:20:51 -0700 | [diff] [blame] | 96 | |
| 97 | // MidiDeviceInfo Fields |
Paul McLean | 84b49130 | 2018-05-09 10:03:57 -0600 | [diff] [blame] | 98 | extern jfieldID mFidMidiDeviceId; // MidiDeviceInfo.mId |
| 99 | }} |
| 100 | using namespace android::midi; |
Paul McLean | 8a3e33b | 2017-03-14 15:20:51 -0700 | [diff] [blame] | 101 | |
| 102 | static std::mutex openMutex; // Ensure that the device can be connected just once to 1 thread |
| 103 | |
Paul McLean | 8a3e33b | 2017-03-14 15:20:51 -0700 | [diff] [blame] | 104 | //// Handy debugging function. |
| 105 | //static void AMIDI_logBuffer(const uint8_t *data, size_t numBytes) { |
| 106 | // for (size_t index = 0; index < numBytes; index++) { |
| 107 | // ALOGI(" data @%zu [0x%X]", index, data[index]); |
| 108 | // } |
| 109 | //} |
Mikhail Naganov | c276c59 | 2016-08-31 18:08:10 -0700 | [diff] [blame] | 110 | |
Paul McLean | 71f672b | 2017-03-05 08:12:18 -0700 | [diff] [blame] | 111 | /* |
| 112 | * Device Functions |
| 113 | */ |
Paul McLean | 8a3e33b | 2017-03-14 15:20:51 -0700 | [diff] [blame] | 114 | /** |
| 115 | * Retrieves information for the native MIDI device. |
| 116 | * |
| 117 | * device The Native API token for the device. This value is obtained from the |
| 118 | * AMidiDevice_fromJava(). |
| 119 | * outDeviceInfoPtr Receives the associated device info. |
| 120 | * |
| 121 | * Returns AMEDIA_OK or a negative error code. |
| 122 | * - AMEDIA_ERROR_INVALID_PARAMETER |
| 123 | * AMEDIA_ERROR_UNKNOWN |
| 124 | */ |
Elliott Hughes | 4797e71 | 2019-07-22 16:39:52 -0700 | [diff] [blame] | 125 | static media_status_t AMIDI_getDeviceInfo(const AMidiDevice *device, |
Paul McLean | 8a3e33b | 2017-03-14 15:20:51 -0700 | [diff] [blame] | 126 | AMidiDeviceInfo *outDeviceInfoPtr) { |
| 127 | if (device == nullptr) { |
| 128 | return AMEDIA_ERROR_INVALID_PARAMETER; |
| 129 | } |
| 130 | |
Mikhail Naganov | c276c59 | 2016-08-31 18:08:10 -0700 | [diff] [blame] | 131 | MidiDeviceInfo deviceInfo; |
Paul McLean | 71f672b | 2017-03-05 08:12:18 -0700 | [diff] [blame] | 132 | Status txResult = device->server->getDeviceInfo(&deviceInfo); |
Mikhail Naganov | c276c59 | 2016-08-31 18:08:10 -0700 | [diff] [blame] | 133 | if (!txResult.isOk()) { |
Andy Hung | 0a8c907 | 2020-12-10 21:56:01 -0800 | [diff] [blame] | 134 | ALOGE("%s server exception code: %d", __func__, txResult.exceptionCode()); |
Paul McLean | 8a3e33b | 2017-03-14 15:20:51 -0700 | [diff] [blame] | 135 | return AMEDIA_ERROR_UNKNOWN; |
Mikhail Naganov | c276c59 | 2016-08-31 18:08:10 -0700 | [diff] [blame] | 136 | } |
| 137 | |
Paul McLean | 8a3e33b | 2017-03-14 15:20:51 -0700 | [diff] [blame] | 138 | outDeviceInfoPtr->type = deviceInfo.getType(); |
| 139 | outDeviceInfoPtr->inputPortCount = deviceInfo.getInputPortNames().size(); |
| 140 | outDeviceInfoPtr->outputPortCount = deviceInfo.getOutputPortNames().size(); |
Robert Wu | a936a25 | 2021-12-22 19:47:20 +0000 | [diff] [blame] | 141 | outDeviceInfoPtr->defaultProtocol = deviceInfo.getDefaultProtocol(); |
Paul McLean | 71f672b | 2017-03-05 08:12:18 -0700 | [diff] [blame] | 142 | |
Paul McLean | 8a3e33b | 2017-03-14 15:20:51 -0700 | [diff] [blame] | 143 | return AMEDIA_OK; |
| 144 | } |
| 145 | |
Paul McLean | 5090a6d | 2019-08-05 12:30:56 -0600 | [diff] [blame] | 146 | media_status_t AMIDI_API AMidiDevice_fromJava(JNIEnv *env, jobject j_midiDeviceObj, |
Paul McLean | 8a3e33b | 2017-03-14 15:20:51 -0700 | [diff] [blame] | 147 | AMidiDevice** devicePtrPtr) |
| 148 | { |
Paul McLean | 8a3e33b | 2017-03-14 15:20:51 -0700 | [diff] [blame] | 149 | if (j_midiDeviceObj == nullptr) { |
| 150 | ALOGE("AMidiDevice_fromJava() invalid MidiDevice object."); |
| 151 | return AMEDIA_ERROR_INVALID_OBJECT; |
| 152 | } |
| 153 | |
| 154 | { |
| 155 | std::lock_guard<std::mutex> guard(openMutex); |
| 156 | |
Paul McLean | 84b49130 | 2018-05-09 10:03:57 -0600 | [diff] [blame] | 157 | long handle = env->GetLongField(j_midiDeviceObj, gFidMidiNativeHandle); |
Paul McLean | 8a3e33b | 2017-03-14 15:20:51 -0700 | [diff] [blame] | 158 | if (handle != 0) { |
| 159 | // Already opened by someone. |
| 160 | return AMEDIA_ERROR_INVALID_OBJECT; |
| 161 | } |
| 162 | |
Paul McLean | 84b49130 | 2018-05-09 10:03:57 -0600 | [diff] [blame] | 163 | jobject serverBinderObj = env->GetObjectField(j_midiDeviceObj, gFidMidiDeviceServerBinder); |
Paul McLean | 8a3e33b | 2017-03-14 15:20:51 -0700 | [diff] [blame] | 164 | sp<IBinder> serverBinder = android::ibinderForJavaObject(env, serverBinderObj); |
| 165 | if (serverBinder.get() == nullptr) { |
| 166 | ALOGE("AMidiDevice_fromJava couldn't connect to native MIDI server."); |
| 167 | return AMEDIA_ERROR_UNKNOWN; |
| 168 | } |
| 169 | |
| 170 | // don't check allocation failures, just abort.. |
| 171 | AMidiDevice* devicePtr = new AMidiDevice; |
| 172 | devicePtr->server = new BpMidiDeviceServer(serverBinder); |
Paul McLean | 84b49130 | 2018-05-09 10:03:57 -0600 | [diff] [blame] | 173 | jobject midiDeviceInfoObj = env->GetObjectField(j_midiDeviceObj, gFidMidiDeviceInfo); |
| 174 | devicePtr->deviceId = env->GetIntField(midiDeviceInfoObj, mFidMidiDeviceId); |
Paul McLean | 8a3e33b | 2017-03-14 15:20:51 -0700 | [diff] [blame] | 175 | |
| 176 | // Synchronize with the associated Java MidiDevice. |
Paul McLean | 84b49130 | 2018-05-09 10:03:57 -0600 | [diff] [blame] | 177 | env->SetLongField(j_midiDeviceObj, gFidMidiNativeHandle, (long)devicePtr); |
Paul McLean | 8a3e33b | 2017-03-14 15:20:51 -0700 | [diff] [blame] | 178 | env->GetJavaVM(&devicePtr->javaVM); |
| 179 | devicePtr->midiDeviceObj = env->NewGlobalRef(j_midiDeviceObj); |
| 180 | |
| 181 | if (AMIDI_getDeviceInfo(devicePtr, &devicePtr->deviceInfo) != AMEDIA_OK) { |
| 182 | // This is weird, but maybe not fatal? |
| 183 | ALOGE("AMidiDevice_fromJava couldn't retrieve attributes of native device."); |
| 184 | } |
| 185 | |
| 186 | *devicePtrPtr = devicePtr; |
| 187 | } |
| 188 | |
| 189 | return AMEDIA_OK; |
| 190 | } |
| 191 | |
Paul McLean | 5090a6d | 2019-08-05 12:30:56 -0600 | [diff] [blame] | 192 | media_status_t AMIDI_API AMidiDevice_release(const AMidiDevice *device) |
Paul McLean | 8a3e33b | 2017-03-14 15:20:51 -0700 | [diff] [blame] | 193 | { |
| 194 | if (device == nullptr || device->midiDeviceObj == nullptr) { |
| 195 | return AMEDIA_ERROR_INVALID_PARAMETER; |
| 196 | } |
| 197 | |
| 198 | JNIEnv* env; |
| 199 | jint err = device->javaVM->GetEnv((void**)&env, JNI_VERSION_1_6); |
| 200 | LOG_ALWAYS_FATAL_IF(err != JNI_OK, "AMidiDevice_release Error accessing JNIEnv err:%d", err); |
| 201 | |
| 202 | // Synchronize with the associated Java MidiDevice. |
Paul McLean | 8a3e33b | 2017-03-14 15:20:51 -0700 | [diff] [blame] | 203 | { |
| 204 | std::lock_guard<std::mutex> guard(openMutex); |
Paul McLean | 84b49130 | 2018-05-09 10:03:57 -0600 | [diff] [blame] | 205 | long handle = env->GetLongField(device->midiDeviceObj, gFidMidiNativeHandle); |
Paul McLean | 8a3e33b | 2017-03-14 15:20:51 -0700 | [diff] [blame] | 206 | if (handle == 0) { |
| 207 | // Not opened as native. |
| 208 | ALOGE("AMidiDevice_release() device not opened in native client."); |
| 209 | return AMEDIA_ERROR_INVALID_OBJECT; |
| 210 | } |
| 211 | |
Paul McLean | 84b49130 | 2018-05-09 10:03:57 -0600 | [diff] [blame] | 212 | env->SetLongField(device->midiDeviceObj, gFidMidiNativeHandle, 0L); |
Paul McLean | 8a3e33b | 2017-03-14 15:20:51 -0700 | [diff] [blame] | 213 | } |
| 214 | env->DeleteGlobalRef(device->midiDeviceObj); |
| 215 | |
| 216 | delete device; |
| 217 | |
| 218 | return AMEDIA_OK; |
| 219 | } |
| 220 | |
Paul McLean | 5090a6d | 2019-08-05 12:30:56 -0600 | [diff] [blame] | 221 | int32_t AMIDI_API AMidiDevice_getType(const AMidiDevice *device) { |
Paul McLean | 8a3e33b | 2017-03-14 15:20:51 -0700 | [diff] [blame] | 222 | if (device == nullptr) { |
| 223 | return AMEDIA_ERROR_INVALID_PARAMETER; |
| 224 | } |
| 225 | return device->deviceInfo.type; |
| 226 | } |
| 227 | |
Paul McLean | 5090a6d | 2019-08-05 12:30:56 -0600 | [diff] [blame] | 228 | ssize_t AMIDI_API AMidiDevice_getNumInputPorts(const AMidiDevice *device) { |
Paul McLean | 8a3e33b | 2017-03-14 15:20:51 -0700 | [diff] [blame] | 229 | if (device == nullptr) { |
| 230 | return AMEDIA_ERROR_INVALID_PARAMETER; |
| 231 | } |
| 232 | return device->deviceInfo.inputPortCount; |
| 233 | } |
| 234 | |
Paul McLean | 5090a6d | 2019-08-05 12:30:56 -0600 | [diff] [blame] | 235 | ssize_t AMIDI_API AMidiDevice_getNumOutputPorts(const AMidiDevice *device) { |
Paul McLean | 8a3e33b | 2017-03-14 15:20:51 -0700 | [diff] [blame] | 236 | if (device == nullptr) { |
| 237 | return AMEDIA_ERROR_INVALID_PARAMETER; |
| 238 | } |
| 239 | return device->deviceInfo.outputPortCount; |
Paul McLean | 71f672b | 2017-03-05 08:12:18 -0700 | [diff] [blame] | 240 | } |
| 241 | |
Robert Wu | a936a25 | 2021-12-22 19:47:20 +0000 | [diff] [blame] | 242 | AMidiDevice_Protocol AMIDI_API AMidiDevice_getDefaultProtocol(const AMidiDevice *device) { |
| 243 | if (device == nullptr) { |
| 244 | return AMIDI_DEVICE_PROTOCOL_UNKNOWN; |
| 245 | } |
| 246 | return static_cast<AMidiDevice_Protocol>(device->deviceInfo.defaultProtocol); |
| 247 | } |
| 248 | |
Paul McLean | 71f672b | 2017-03-05 08:12:18 -0700 | [diff] [blame] | 249 | /* |
| 250 | * Port Helpers |
| 251 | */ |
Paul McLean | 8a3e33b | 2017-03-14 15:20:51 -0700 | [diff] [blame] | 252 | static media_status_t AMIDI_openPort(const AMidiDevice *device, int32_t portNumber, int type, |
Paul McLean | 71f672b | 2017-03-05 08:12:18 -0700 | [diff] [blame] | 253 | AMIDI_Port **portPtr) { |
Paul McLean | 8a3e33b | 2017-03-14 15:20:51 -0700 | [diff] [blame] | 254 | if (device == nullptr) { |
| 255 | return AMEDIA_ERROR_INVALID_PARAMETER; |
| 256 | } |
| 257 | |
Paul McLean | 71f672b | 2017-03-05 08:12:18 -0700 | [diff] [blame] | 258 | sp<BBinder> portToken(new BBinder()); |
| 259 | unique_fd ufd; |
| 260 | Status txResult = type == PORTTYPE_OUTPUT |
| 261 | ? device->server->openOutputPort(portToken, portNumber, &ufd) |
| 262 | : device->server->openInputPort(portToken, portNumber, &ufd); |
| 263 | if (!txResult.isOk()) { |
Andy Hung | 0a8c907 | 2020-12-10 21:56:01 -0800 | [diff] [blame] | 264 | ALOGE("%s server exception code: %d", __func__, txResult.exceptionCode()); |
Paul McLean | 8a3e33b | 2017-03-14 15:20:51 -0700 | [diff] [blame] | 265 | return AMEDIA_ERROR_UNKNOWN; |
Paul McLean | 71f672b | 2017-03-05 08:12:18 -0700 | [diff] [blame] | 266 | } |
| 267 | |
Paul McLean | 8a3e33b | 2017-03-14 15:20:51 -0700 | [diff] [blame] | 268 | AMIDI_Port *port = new AMIDI_Port; |
Paul McLean | 71f672b | 2017-03-05 08:12:18 -0700 | [diff] [blame] | 269 | port->state = MIDI_PORT_STATE_OPEN_IDLE; |
| 270 | port->device = device; |
| 271 | port->binderToken = portToken; |
| 272 | port->ufd = std::move(ufd); |
| 273 | |
| 274 | *portPtr = port; |
| 275 | |
Paul McLean | 8a3e33b | 2017-03-14 15:20:51 -0700 | [diff] [blame] | 276 | return AMEDIA_OK; |
Paul McLean | 71f672b | 2017-03-05 08:12:18 -0700 | [diff] [blame] | 277 | } |
| 278 | |
Paul McLean | 8a3e33b | 2017-03-14 15:20:51 -0700 | [diff] [blame] | 279 | static void AMIDI_closePort(AMIDI_Port *port) { |
| 280 | if (port == nullptr) { |
| 281 | return; |
| 282 | } |
| 283 | |
Paul McLean | 71f672b | 2017-03-05 08:12:18 -0700 | [diff] [blame] | 284 | int portState = MIDI_PORT_STATE_OPEN_IDLE; |
| 285 | while (!port->state.compare_exchange_weak(portState, MIDI_PORT_STATE_CLOSED)) { |
| 286 | if (portState == MIDI_PORT_STATE_CLOSED) { |
Paul McLean | 8a3e33b | 2017-03-14 15:20:51 -0700 | [diff] [blame] | 287 | return; // Already closed |
Paul McLean | 71f672b | 2017-03-05 08:12:18 -0700 | [diff] [blame] | 288 | } |
| 289 | } |
| 290 | |
| 291 | Status txResult = port->device->server->closePort(port->binderToken); |
| 292 | if (!txResult.isOk()) { |
Andy Hung | 0a8c907 | 2020-12-10 21:56:01 -0800 | [diff] [blame] | 293 | ALOGE("%s server exception code: %d", __func__, txResult.exceptionCode()); |
Paul McLean | 71f672b | 2017-03-05 08:12:18 -0700 | [diff] [blame] | 294 | } |
| 295 | |
| 296 | delete port; |
Mikhail Naganov | c276c59 | 2016-08-31 18:08:10 -0700 | [diff] [blame] | 297 | } |
| 298 | |
| 299 | /* |
| 300 | * Output (receiving) API |
| 301 | */ |
Paul McLean | 5090a6d | 2019-08-05 12:30:56 -0600 | [diff] [blame] | 302 | media_status_t AMIDI_API AMidiOutputPort_open(const AMidiDevice *device, int32_t portNumber, |
Paul McLean | 8a3e33b | 2017-03-14 15:20:51 -0700 | [diff] [blame] | 303 | AMidiOutputPort **outOutputPortPtr) { |
| 304 | return AMIDI_openPort(device, portNumber, PORTTYPE_OUTPUT, (AMIDI_Port**)outOutputPortPtr); |
Mikhail Naganov | c276c59 | 2016-08-31 18:08:10 -0700 | [diff] [blame] | 305 | } |
| 306 | |
Paul McLean | 8a3e33b | 2017-03-14 15:20:51 -0700 | [diff] [blame] | 307 | /* |
| 308 | * A little RAII (https://en.wikipedia.org/wiki/Resource_acquisition_is_initialization) |
| 309 | * class to ensure that the port state is correct irrespective of errors. |
| 310 | */ |
| 311 | class MidiReceiver { |
| 312 | public: |
| 313 | MidiReceiver(AMIDI_Port *port) : mPort(port) {} |
| 314 | |
| 315 | ~MidiReceiver() { |
| 316 | // flag the port state to idle |
| 317 | mPort->state.store(MIDI_PORT_STATE_OPEN_IDLE); |
Mikhail Naganov | c276c59 | 2016-08-31 18:08:10 -0700 | [diff] [blame] | 318 | } |
| 319 | |
Paul McLean | 8a3e33b | 2017-03-14 15:20:51 -0700 | [diff] [blame] | 320 | ssize_t receive(int32_t *opcodePtr, uint8_t *buffer, size_t maxBytes, |
| 321 | size_t *numBytesReceivedPtr, int64_t *timestampPtr) { |
| 322 | int portState = MIDI_PORT_STATE_OPEN_IDLE; |
| 323 | // check to see if the port is idle, then set to active |
| 324 | if (!mPort->state.compare_exchange_strong(portState, MIDI_PORT_STATE_OPEN_ACTIVE)) { |
| 325 | // The port not idle or has been closed. |
| 326 | return AMEDIA_ERROR_UNKNOWN; |
Mikhail Naganov | c276c59 | 2016-08-31 18:08:10 -0700 | [diff] [blame] | 327 | } |
| 328 | |
Paul McLean | 8a3e33b | 2017-03-14 15:20:51 -0700 | [diff] [blame] | 329 | struct pollfd checkFds[1] = { { mPort->ufd, POLLIN, 0 } }; |
| 330 | if (poll(checkFds, 1, 0) < 1) { |
| 331 | // Nothing there |
| 332 | return 0; |
| 333 | } |
| 334 | |
Mikhail Naganov | c276c59 | 2016-08-31 18:08:10 -0700 | [diff] [blame] | 335 | uint8_t readBuffer[AMIDI_PACKET_SIZE]; |
Keith Mok | 57e0882 | 2021-11-12 23:55:16 +0000 | [diff] [blame] | 336 | ssize_t readCount = TEMP_FAILURE_RETRY(read(mPort->ufd, readBuffer, sizeof(readBuffer))); |
| 337 | if (readCount < 1) { |
Paul McLean | 8a3e33b | 2017-03-14 15:20:51 -0700 | [diff] [blame] | 338 | return AMEDIA_ERROR_UNKNOWN; |
Mikhail Naganov | c276c59 | 2016-08-31 18:08:10 -0700 | [diff] [blame] | 339 | } |
| 340 | |
Paul McLean | 8a3e33b | 2017-03-14 15:20:51 -0700 | [diff] [blame] | 341 | // see Packet Format definition at the top of this file. |
| 342 | size_t numMessageBytes = 0; |
| 343 | *opcodePtr = readBuffer[0]; |
| 344 | if (*opcodePtr == AMIDI_OPCODE_DATA && readCount >= AMIDI_PACKET_OVERHEAD) { |
| 345 | numMessageBytes = readCount - AMIDI_PACKET_OVERHEAD; |
| 346 | numMessageBytes = std::min(maxBytes, numMessageBytes); |
| 347 | memcpy(buffer, readBuffer + 1, numMessageBytes); |
| 348 | if (timestampPtr != nullptr) { |
Mikhail Naganov | 25f51c6 | 2018-11-07 09:26:30 -0800 | [diff] [blame] | 349 | memcpy(timestampPtr, readBuffer + readCount - sizeof(uint64_t), |
| 350 | sizeof(*timestampPtr)); |
Mikhail Naganov | c276c59 | 2016-08-31 18:08:10 -0700 | [diff] [blame] | 351 | } |
Mikhail Naganov | c276c59 | 2016-08-31 18:08:10 -0700 | [diff] [blame] | 352 | } |
Paul McLean | 8a3e33b | 2017-03-14 15:20:51 -0700 | [diff] [blame] | 353 | *numBytesReceivedPtr = numMessageBytes; |
| 354 | return 1; |
Mikhail Naganov | c276c59 | 2016-08-31 18:08:10 -0700 | [diff] [blame] | 355 | } |
| 356 | |
Paul McLean | 8a3e33b | 2017-03-14 15:20:51 -0700 | [diff] [blame] | 357 | private: |
| 358 | AMIDI_Port *mPort; |
| 359 | }; |
Paul McLean | 71f672b | 2017-03-05 08:12:18 -0700 | [diff] [blame] | 360 | |
Paul McLean | 5090a6d | 2019-08-05 12:30:56 -0600 | [diff] [blame] | 361 | ssize_t AMIDI_API AMidiOutputPort_receive(const AMidiOutputPort *outputPort, int32_t *opcodePtr, |
Paul McLean | 8a3e33b | 2017-03-14 15:20:51 -0700 | [diff] [blame] | 362 | uint8_t *buffer, size_t maxBytes, size_t* numBytesReceivedPtr, int64_t *timestampPtr) { |
| 363 | |
| 364 | if (outputPort == nullptr || buffer == nullptr) { |
| 365 | return -EINVAL; |
| 366 | } |
| 367 | |
| 368 | return MidiReceiver((AMIDI_Port*)outputPort).receive(opcodePtr, buffer, maxBytes, |
| 369 | numBytesReceivedPtr, timestampPtr); |
Mikhail Naganov | c276c59 | 2016-08-31 18:08:10 -0700 | [diff] [blame] | 370 | } |
| 371 | |
Paul McLean | 5090a6d | 2019-08-05 12:30:56 -0600 | [diff] [blame] | 372 | void AMIDI_API AMidiOutputPort_close(const AMidiOutputPort *outputPort) { |
Paul McLean | 8a3e33b | 2017-03-14 15:20:51 -0700 | [diff] [blame] | 373 | AMIDI_closePort((AMIDI_Port*)outputPort); |
Mikhail Naganov | c276c59 | 2016-08-31 18:08:10 -0700 | [diff] [blame] | 374 | } |
| 375 | |
| 376 | /* |
| 377 | * Input (sending) API |
| 378 | */ |
Paul McLean | 5090a6d | 2019-08-05 12:30:56 -0600 | [diff] [blame] | 379 | media_status_t AMIDI_API AMidiInputPort_open(const AMidiDevice *device, int32_t portNumber, |
Paul McLean | 8a3e33b | 2017-03-14 15:20:51 -0700 | [diff] [blame] | 380 | AMidiInputPort **outInputPortPtr) { |
| 381 | return AMIDI_openPort(device, portNumber, PORTTYPE_INPUT, (AMIDI_Port**)outInputPortPtr); |
Mikhail Naganov | c276c59 | 2016-08-31 18:08:10 -0700 | [diff] [blame] | 382 | } |
| 383 | |
Paul McLean | 5090a6d | 2019-08-05 12:30:56 -0600 | [diff] [blame] | 384 | void AMIDI_API AMidiInputPort_close(const AMidiInputPort *inputPort) { |
Paul McLean | 8a3e33b | 2017-03-14 15:20:51 -0700 | [diff] [blame] | 385 | AMIDI_closePort((AMIDI_Port*)inputPort); |
Mikhail Naganov | c276c59 | 2016-08-31 18:08:10 -0700 | [diff] [blame] | 386 | } |
| 387 | |
Paul McLean | 71f672b | 2017-03-05 08:12:18 -0700 | [diff] [blame] | 388 | static ssize_t AMIDI_makeSendBuffer( |
Paul McLean | 8a3e33b | 2017-03-14 15:20:51 -0700 | [diff] [blame] | 389 | uint8_t *buffer, const uint8_t *data, size_t numBytes, uint64_t timestamp) { |
| 390 | // Error checking will happen in the caller since this isn't an API function. |
Mikhail Naganov | c276c59 | 2016-08-31 18:08:10 -0700 | [diff] [blame] | 391 | buffer[0] = AMIDI_OPCODE_DATA; |
| 392 | memcpy(buffer + 1, data, numBytes); |
| 393 | memcpy(buffer + 1 + numBytes, ×tamp, sizeof(timestamp)); |
| 394 | return numBytes + AMIDI_PACKET_OVERHEAD; |
| 395 | } |
| 396 | |
Paul McLean | 5090a6d | 2019-08-05 12:30:56 -0600 | [diff] [blame] | 397 | ssize_t AMIDI_API AMidiInputPort_send(const AMidiInputPort *inputPort, const uint8_t *buffer, |
Paul McLean | 8a3e33b | 2017-03-14 15:20:51 -0700 | [diff] [blame] | 398 | size_t numBytes) { |
| 399 | return AMidiInputPort_sendWithTimestamp(inputPort, buffer, numBytes, 0); |
Mikhail Naganov | c276c59 | 2016-08-31 18:08:10 -0700 | [diff] [blame] | 400 | } |
| 401 | |
Paul McLean | 5090a6d | 2019-08-05 12:30:56 -0600 | [diff] [blame] | 402 | ssize_t AMIDI_API AMidiInputPort_sendWithTimestamp(const AMidiInputPort *inputPort, |
Paul McLean | 8a3e33b | 2017-03-14 15:20:51 -0700 | [diff] [blame] | 403 | const uint8_t *data, size_t numBytes, int64_t timestamp) { |
Robert Wu | 2136d8d | 2022-01-25 19:36:02 +0000 | [diff] [blame] | 404 | if (inputPort == nullptr || data == nullptr || numBytes < 0 || timestamp < 0) { |
Paul McLean | 8a3e33b | 2017-03-14 15:20:51 -0700 | [diff] [blame] | 405 | return AMEDIA_ERROR_INVALID_PARAMETER; |
Mikhail Naganov | c276c59 | 2016-08-31 18:08:10 -0700 | [diff] [blame] | 406 | } |
| 407 | |
Robert Wu | 2136d8d | 2022-01-25 19:36:02 +0000 | [diff] [blame] | 408 | if (numBytes == 0) { |
| 409 | return 0; |
| 410 | } |
| 411 | |
Mikhail Naganov | c276c59 | 2016-08-31 18:08:10 -0700 | [diff] [blame] | 412 | // AMIDI_logBuffer(data, numBytes); |
| 413 | |
Paul McLean | 8a3e33b | 2017-03-14 15:20:51 -0700 | [diff] [blame] | 414 | uint8_t writeBuffer[AMIDI_BUFFER_SIZE + AMIDI_PACKET_OVERHEAD]; |
| 415 | size_t numSent = 0; |
| 416 | while (numSent < numBytes) { |
| 417 | size_t blockSize = AMIDI_BUFFER_SIZE; |
| 418 | blockSize = std::min(blockSize, numBytes - numSent); |
Mikhail Naganov | c276c59 | 2016-08-31 18:08:10 -0700 | [diff] [blame] | 419 | |
Paul McLean | 8a3e33b | 2017-03-14 15:20:51 -0700 | [diff] [blame] | 420 | ssize_t numTransferBytes = |
| 421 | AMIDI_makeSendBuffer(writeBuffer, data + numSent, blockSize, timestamp); |
Keith Mok | 57e0882 | 2021-11-12 23:55:16 +0000 | [diff] [blame] | 422 | ssize_t numWritten = TEMP_FAILURE_RETRY(write(((AMIDI_Port*)inputPort)->ufd, writeBuffer, |
| 423 | numTransferBytes)); |
Paul McLean | 8a3e33b | 2017-03-14 15:20:51 -0700 | [diff] [blame] | 424 | if (numWritten < 0) { |
| 425 | break; // error so bail out. |
| 426 | } |
| 427 | if (numWritten < numTransferBytes) { |
| 428 | ALOGE("AMidiInputPort_sendWithTimestamp Couldn't write MIDI data buffer." |
| 429 | " requested:%zu, written%zu",numTransferBytes, numWritten); |
| 430 | break; // bail |
| 431 | } |
| 432 | |
| 433 | numSent += numWritten - AMIDI_PACKET_OVERHEAD; |
Mikhail Naganov | c276c59 | 2016-08-31 18:08:10 -0700 | [diff] [blame] | 434 | } |
| 435 | |
Paul McLean | 8a3e33b | 2017-03-14 15:20:51 -0700 | [diff] [blame] | 436 | return numSent; |
Mikhail Naganov | c276c59 | 2016-08-31 18:08:10 -0700 | [diff] [blame] | 437 | } |
| 438 | |
Paul McLean | 5090a6d | 2019-08-05 12:30:56 -0600 | [diff] [blame] | 439 | media_status_t AMIDI_API AMidiInputPort_sendFlush(const AMidiInputPort *inputPort) { |
Paul McLean | 8a3e33b | 2017-03-14 15:20:51 -0700 | [diff] [blame] | 440 | if (inputPort == nullptr) { |
| 441 | return AMEDIA_ERROR_INVALID_PARAMETER; |
| 442 | } |
| 443 | |
Mikhail Naganov | c276c59 | 2016-08-31 18:08:10 -0700 | [diff] [blame] | 444 | uint8_t opCode = AMIDI_OPCODE_FLUSH; |
| 445 | ssize_t numTransferBytes = 1; |
Keith Mok | 57e0882 | 2021-11-12 23:55:16 +0000 | [diff] [blame] | 446 | ssize_t numWritten = TEMP_FAILURE_RETRY(write(((AMIDI_Port*)inputPort)->ufd, &opCode, |
| 447 | numTransferBytes)); |
Mikhail Naganov | c276c59 | 2016-08-31 18:08:10 -0700 | [diff] [blame] | 448 | |
| 449 | if (numWritten < numTransferBytes) { |
Paul McLean | 8a3e33b | 2017-03-14 15:20:51 -0700 | [diff] [blame] | 450 | ALOGE("AMidiInputPort_flush Couldn't write MIDI flush. requested:%zd, written:%zd", |
Mikhail Naganov | c276c59 | 2016-08-31 18:08:10 -0700 | [diff] [blame] | 451 | numTransferBytes, numWritten); |
Paul McLean | 8a3e33b | 2017-03-14 15:20:51 -0700 | [diff] [blame] | 452 | return AMEDIA_ERROR_UNSUPPORTED; |
Mikhail Naganov | c276c59 | 2016-08-31 18:08:10 -0700 | [diff] [blame] | 453 | } |
| 454 | |
Paul McLean | 8a3e33b | 2017-03-14 15:20:51 -0700 | [diff] [blame] | 455 | return AMEDIA_OK; |
Mikhail Naganov | c276c59 | 2016-08-31 18:08:10 -0700 | [diff] [blame] | 456 | } |
| 457 | |