Prashant Malani | fd1e200 | 2017-08-09 13:22:59 -0700 | [diff] [blame] | 1 | // Copyright 2017 The Chromium OS Authors. All rights reserved. |
| 2 | // Use of this source code is governed by a BSD-style license that can be |
| 3 | // found in the LICENSE file. |
| 4 | |
| 5 | #include "midis/seq_handler.h" |
| 6 | |
| 7 | #include <map> |
Ben Chan | d496e61 | 2017-09-29 00:20:50 -0700 | [diff] [blame] | 8 | #include <memory> |
Prashant Malani | fd1e200 | 2017-08-09 13:22:59 -0700 | [diff] [blame] | 9 | #include <string> |
| 10 | #include <utility> |
| 11 | |
| 12 | #include <base/bind.h> |
Prashant Malani | fd1e200 | 2017-08-09 13:22:59 -0700 | [diff] [blame] | 13 | #include <poll.h> |
| 14 | |
Prashant Malani | 3c54036 | 2017-09-28 13:35:14 -0700 | [diff] [blame] | 15 | #include "midis/constants.h" |
| 16 | |
Prashant Malani | fd1e200 | 2017-08-09 13:22:59 -0700 | [diff] [blame] | 17 | namespace { |
| 18 | |
| 19 | const unsigned int kCreateInputPortCaps = |
| 20 | SND_SEQ_PORT_CAP_WRITE | SND_SEQ_PORT_CAP_NO_EXPORT; |
| 21 | const unsigned int kCreateOutputPortCaps = |
| 22 | SND_SEQ_PORT_CAP_READ | SND_SEQ_PORT_CAP_NO_EXPORT; |
| 23 | const unsigned int kCreatePortType = |
| 24 | SND_SEQ_PORT_TYPE_MIDI_GENERIC | SND_SEQ_PORT_TYPE_APPLICATION; |
| 25 | const char kSndSeqName[] = "hw"; |
| 26 | |
| 27 | } // namespace |
| 28 | |
| 29 | namespace midis { |
| 30 | |
Prashant Malani | 3c54036 | 2017-09-28 13:35:14 -0700 | [diff] [blame] | 31 | SeqHandler::SeqHandler() : weak_factory_(this) {} |
| 32 | |
Prashant Malani | fd1e200 | 2017-08-09 13:22:59 -0700 | [diff] [blame] | 33 | SeqHandler::SeqHandler(AddDeviceCallback add_device_cb, |
| 34 | RemoveDeviceCallback remove_device_cb, |
| 35 | HandleReceiveDataCallback handle_rx_data_cb, |
| 36 | IsDevicePresentCallback is_device_present_cb, |
| 37 | IsPortPresentCallback is_port_present_cb) |
| 38 | : add_device_cb_(add_device_cb), |
| 39 | remove_device_cb_(remove_device_cb), |
| 40 | handle_rx_data_cb_(handle_rx_data_cb), |
| 41 | is_device_present_cb_(is_device_present_cb), |
| 42 | is_port_present_cb_(is_port_present_cb), |
| 43 | weak_factory_(this) {} |
| 44 | |
| 45 | bool SeqHandler::InitSeq() { |
| 46 | // Create client handles. |
| 47 | snd_seq_t* tmp_seq = nullptr; |
| 48 | int err = |
| 49 | snd_seq_open(&tmp_seq, kSndSeqName, SND_SEQ_OPEN_INPUT, SND_SEQ_NONBLOCK); |
| 50 | if (err != 0) { |
| 51 | LOG(ERROR) << "snd_seq_open fails: " << snd_strerror(err); |
| 52 | return false; |
| 53 | } |
| 54 | ScopedSeqPtr in_client(tmp_seq); |
| 55 | tmp_seq = nullptr; |
| 56 | in_client_id_ = snd_seq_client_id(in_client.get()); |
| 57 | |
| 58 | err = snd_seq_open(&tmp_seq, kSndSeqName, SND_SEQ_OPEN_OUTPUT, 0); |
| 59 | if (err != 0) { |
| 60 | LOG(ERROR) << "snd_seq_open fails: " << snd_strerror(err); |
| 61 | return false; |
| 62 | } |
| 63 | |
| 64 | ScopedSeqPtr out_client(tmp_seq); |
| 65 | tmp_seq = nullptr; |
| 66 | out_client_id_ = snd_seq_client_id(out_client.get()); |
| 67 | |
| 68 | // Name the clients. |
| 69 | err = snd_seq_set_client_name(in_client.get(), "midis (input)"); |
| 70 | if (err != 0) { |
| 71 | LOG(ERROR) << "snd_seq_set_client_name fails: " << snd_strerror(err); |
| 72 | return false; |
| 73 | } |
| 74 | err = snd_seq_set_client_name(out_client.get(), "midis (output)"); |
| 75 | if (err != 0) { |
| 76 | LOG(ERROR) << "snd_seq_set_client_name fails: " << snd_strerror(err); |
| 77 | return false; |
| 78 | } |
| 79 | |
| 80 | // Create input port. |
| 81 | in_port_id_ = snd_seq_create_simple_port( |
| 82 | in_client.get(), NULL, kCreateInputPortCaps, kCreatePortType); |
| 83 | if (in_port_id_ < 0) { |
| 84 | LOG(ERROR) << "snd_seq_create_simple_port fails: " |
| 85 | << snd_strerror(in_port_id_); |
| 86 | return false; |
| 87 | } |
| 88 | |
| 89 | // Subscribe to the announce port. |
| 90 | snd_seq_port_subscribe_t* subs; |
| 91 | snd_seq_port_subscribe_alloca(&subs); |
| 92 | snd_seq_addr_t announce_sender; |
| 93 | snd_seq_addr_t announce_dest; |
| 94 | announce_sender.client = SND_SEQ_CLIENT_SYSTEM; |
| 95 | announce_sender.port = SND_SEQ_PORT_SYSTEM_ANNOUNCE; |
| 96 | announce_dest.client = in_client_id_; |
| 97 | announce_dest.port = in_port_id_; |
| 98 | snd_seq_port_subscribe_set_sender(subs, &announce_sender); |
| 99 | snd_seq_port_subscribe_set_dest(subs, &announce_dest); |
| 100 | err = snd_seq_subscribe_port(in_client.get(), subs); |
| 101 | if (err != 0) { |
| 102 | LOG(ERROR) << "snd_seq_subscribe_port on the announce port fails: " |
| 103 | << snd_strerror(err); |
| 104 | return false; |
| 105 | } |
| 106 | |
| 107 | // Initialize decoder. |
| 108 | snd_midi_event_t* tmp_decoder = nullptr; |
| 109 | snd_midi_event_new(0, &tmp_decoder); |
| 110 | ScopedMidiEventPtr decoder(tmp_decoder); |
| 111 | tmp_decoder = nullptr; |
| 112 | snd_midi_event_no_status(decoder.get(), 1); |
| 113 | |
| 114 | in_client_.reset(in_client.release()); |
| 115 | out_client_.reset(out_client.release()); |
| 116 | decoder_.reset(decoder.release()); |
| 117 | |
| 118 | // Obtain the poll file descriptor to watch. |
Ben Chan | d496e61 | 2017-09-29 00:20:50 -0700 | [diff] [blame] | 119 | pfd_ = std::make_unique<pollfd>(); |
Prashant Malani | fd1e200 | 2017-08-09 13:22:59 -0700 | [diff] [blame] | 120 | snd_seq_poll_descriptors(in_client_.get(), pfd_.get(), 1, POLLIN); |
| 121 | |
| 122 | taskid_ = brillo::MessageLoop::current()->WatchFileDescriptor( |
Prashant Malani | 3c54036 | 2017-09-28 13:35:14 -0700 | [diff] [blame] | 123 | FROM_HERE, pfd_.get()->fd, brillo::MessageLoop::kWatchRead, true, |
Prashant Malani | fd1e200 | 2017-08-09 13:22:59 -0700 | [diff] [blame] | 124 | base::Bind(&SeqHandler::ProcessAlsaClientFd, weak_factory_.GetWeakPtr())); |
| 125 | |
| 126 | if (taskid_ == brillo::MessageLoop::kTaskIdNull) { |
| 127 | in_client_.reset(); |
| 128 | out_client_.reset(); |
| 129 | decoder_.reset(); |
| 130 | pfd_.reset(); |
| 131 | return false; |
| 132 | } |
| 133 | |
| 134 | return true; |
| 135 | } |
| 136 | |
| 137 | void SeqHandler::ProcessAlsaClientFd() { |
| 138 | int remaining; |
| 139 | do { |
| 140 | snd_seq_event_t* event; |
| 141 | int err = snd_seq_event_input(in_client_.get(), &event); |
| 142 | remaining = snd_seq_event_input_pending(in_client_.get(), 0); |
| 143 | |
| 144 | if (err == -ENOSPC) { |
| 145 | // Handle out of space error. |
| 146 | LOG(ERROR) << "snd_seq_event_input detected buffer overrun"; |
| 147 | // We've lost events: check another way to see if we need to shut |
| 148 | // down. |
| 149 | } else if (err == -EAGAIN) { |
| 150 | // We've read all the data. |
| 151 | } else if (err < 0) { |
| 152 | // Handle other errors. |
| 153 | LOG(ERROR) << "snd_seq_event_input fails: " << snd_strerror(err); |
| 154 | // TODO(pmalani): Stop the message loop here then. |
| 155 | } else if (event->source.client == SND_SEQ_CLIENT_SYSTEM && |
| 156 | event->source.port == SND_SEQ_PORT_SYSTEM_ANNOUNCE) { |
| 157 | // Handle announce events. |
| 158 | switch (event->type) { |
| 159 | case SND_SEQ_EVENT_PORT_START: |
| 160 | // Don't use SND_SEQ_EVENT_CLIENT_START because the |
| 161 | // client name may not be set by the time we query |
| 162 | // it. It should be set by the time ports are made. |
| 163 | AddSeqDevice(event->data.addr.client); |
| 164 | AddSeqPort(event->data.addr.client, event->data.addr.port); |
| 165 | break; |
| 166 | case SND_SEQ_EVENT_CLIENT_EXIT: |
| 167 | // Check for disconnection of our "out" client. This means "shut |
| 168 | // down". |
| 169 | if (event->data.addr.client == out_client_id_) { |
| 170 | // TODO(pmalani): Stop the message loop here then. |
| 171 | remaining = 0; |
| 172 | } else { |
| 173 | RemoveSeqDevice(event->data.addr.client); |
| 174 | } |
| 175 | break; |
| 176 | case SND_SEQ_EVENT_PORT_EXIT: |
| 177 | RemoveSeqPort(event->data.addr.client, event->data.addr.port); |
| 178 | break; |
| 179 | } |
| 180 | } else { |
| 181 | // Normal operation. |
| 182 | ProcessMidiEvent(event); |
| 183 | } |
| 184 | } while (remaining > 0); |
| 185 | } |
| 186 | |
| 187 | void SeqHandler::AddSeqDevice(uint32_t device_id) { |
Prashant Malani | 3c54036 | 2017-09-28 13:35:14 -0700 | [diff] [blame] | 188 | if (is_device_present_cb_.Run(0 /* TODO(pmalani): Remove card number */, |
| 189 | device_id)) { |
Prashant Malani | fd1e200 | 2017-08-09 13:22:59 -0700 | [diff] [blame] | 190 | LOG(INFO) << "Device: " << device_id << " already exists."; |
| 191 | return; |
| 192 | } |
| 193 | |
| 194 | // Check that the device isn't our own in/our client. |
| 195 | if (device_id == in_client_id_ || device_id == out_client_id_) { |
| 196 | return; |
| 197 | } |
| 198 | |
| 199 | snd_seq_client_info_t* client_info; |
| 200 | snd_seq_client_info_alloca(&client_info); |
| 201 | int err = |
| 202 | snd_seq_get_any_client_info(in_client_.get(), device_id, client_info); |
| 203 | if (err != 0) { |
| 204 | LOG(ERROR) << "Failed to get client info."; |
| 205 | return; |
| 206 | } |
| 207 | |
| 208 | std::string name(snd_seq_client_info_get_name(client_info)); |
| 209 | uint32_t num_subdevices = snd_seq_client_info_get_num_ports(client_info); |
| 210 | |
| 211 | // Store the list of MIDI ports and corresponding capabilities in a map. |
| 212 | std::map<uint32_t, unsigned int> port_caps; |
| 213 | snd_seq_port_info_t* port_info; |
| 214 | snd_seq_port_info_alloca(&port_info); |
| 215 | snd_seq_port_info_set_client(port_info, device_id); |
| 216 | snd_seq_port_info_set_port(port_info, -1); |
| 217 | while (!snd_seq_query_next_port(in_client_.get(), port_info)) { |
| 218 | if (!(snd_seq_port_info_get_type(port_info) & |
| 219 | SND_SEQ_PORT_TYPE_MIDI_GENERIC)) { |
| 220 | LOG(INFO) << "Skipping non-MIDI port."; |
| 221 | continue; |
| 222 | } |
| 223 | port_caps.emplace(snd_seq_port_info_get_port(port_info), |
| 224 | snd_seq_port_info_get_capability(port_info)); |
| 225 | } |
| 226 | |
Ben Chan | 2a3a8e3 | 2017-10-05 11:15:11 -0700 | [diff] [blame^] | 227 | auto dev = std::make_unique<Device>( |
Prashant Malani | 3c54036 | 2017-09-28 13:35:14 -0700 | [diff] [blame] | 228 | name, std::string(), |
| 229 | 0 /* card number; TODO(pmalani) remove card number */, device_id, |
| 230 | num_subdevices, 0 /* device flags TODO(pmalani): flags not needed. */, |
Prashant Malani | fd1e200 | 2017-08-09 13:22:59 -0700 | [diff] [blame] | 231 | base::Bind(&SeqHandler::SubscribeInPort, base::Unretained(this)), |
| 232 | base::Bind(&SeqHandler::SubscribeOutPort, base::Unretained(this)), |
| 233 | base::Bind(&SeqHandler::UnsubscribeInPort, weak_factory_.GetWeakPtr()), |
| 234 | base::Bind(&SeqHandler::UnsubscribeOutPort, weak_factory_.GetWeakPtr()), |
| 235 | base::Bind(&SeqHandler::SendMidiData, weak_factory_.GetWeakPtr()), |
| 236 | std::move(port_caps)); |
| 237 | add_device_cb_.Run(std::move(dev)); |
| 238 | } |
| 239 | |
| 240 | void SeqHandler::AddSeqPort(uint32_t device_id, uint32_t port_id) { |
| 241 | if (!is_port_present_cb_.Run(0, device_id, port_id)) { |
| 242 | LOG(WARNING) << "Received port start event for new port: " << port_id |
| 243 | << " on device: " << device_id << "; ignoring"; |
| 244 | } |
| 245 | } |
| 246 | |
| 247 | void SeqHandler::RemoveSeqDevice(uint32_t device_id) { |
| 248 | remove_device_cb_.Run(0 /* FIXME remove card number */, device_id); |
| 249 | } |
| 250 | |
| 251 | void SeqHandler::RemoveSeqPort(uint32_t device_id, uint32_t port_id) { |
| 252 | if (!is_port_present_cb_.Run(0, device_id, port_id)) { |
| 253 | LOG(WARNING) << "Received port start event for new port: " << port_id |
| 254 | << " on device: " << device_id << "; ignoring"; |
| 255 | } |
| 256 | } |
| 257 | |
| 258 | bool SeqHandler::SubscribeInPort(uint32_t device_id, uint32_t port_id) { |
| 259 | snd_seq_port_subscribe_t* subs; |
| 260 | snd_seq_port_subscribe_alloca(&subs); |
| 261 | snd_seq_addr_t sender; |
| 262 | sender.client = device_id; |
| 263 | sender.port = port_id; |
| 264 | snd_seq_port_subscribe_set_sender(subs, &sender); |
| 265 | |
| 266 | snd_seq_addr_t dest; |
| 267 | dest.client = in_client_id_; |
| 268 | dest.port = in_port_id_; |
| 269 | snd_seq_port_subscribe_set_dest(subs, &dest); |
| 270 | |
| 271 | int err = snd_seq_subscribe_port(in_client_.get(), subs); |
| 272 | if (err != 0) { |
| 273 | LOG(ERROR) << "snd_seq_subscribe_port fails: " << snd_strerror(err); |
| 274 | return false; |
| 275 | } |
| 276 | |
| 277 | return true; |
| 278 | } |
| 279 | |
| 280 | int SeqHandler::SubscribeOutPort(uint32_t device_id, uint32_t port_id) { |
| 281 | int out_port; |
Prashant Malani | 3c54036 | 2017-09-28 13:35:14 -0700 | [diff] [blame] | 282 | out_port = snd_seq_create_simple_port(out_client_.get(), NULL, |
| 283 | kCreateOutputPortCaps, kCreatePortType); |
Prashant Malani | fd1e200 | 2017-08-09 13:22:59 -0700 | [diff] [blame] | 284 | if (out_port < 0) { |
| 285 | LOG(INFO) << "snd_seq_creat_simple_port (output) failed: " |
| 286 | << snd_strerror(out_port); |
| 287 | return -1; |
| 288 | } |
| 289 | |
| 290 | snd_seq_port_subscribe_t* subs; |
| 291 | snd_seq_port_subscribe_alloca(&subs); |
| 292 | snd_seq_addr_t sender; |
| 293 | sender.client = out_client_id_; |
| 294 | sender.port = out_port; |
| 295 | snd_seq_port_subscribe_set_sender(subs, &sender); |
| 296 | |
| 297 | snd_seq_addr_t dest; |
| 298 | dest.client = device_id; |
| 299 | dest.port = port_id; |
| 300 | snd_seq_port_subscribe_set_dest(subs, &dest); |
| 301 | |
| 302 | int err = snd_seq_subscribe_port(out_client_.get(), subs); |
| 303 | if (err != 0) { |
| 304 | snd_seq_delete_simple_port(out_client_.get(), out_port); |
| 305 | LOG(ERROR) << "snd_seq_subscribe_port fails: " << snd_strerror(err); |
| 306 | return -1; |
| 307 | } |
| 308 | |
| 309 | return out_port; |
| 310 | } |
| 311 | |
| 312 | void SeqHandler::UnsubscribeInPort(uint32_t device_id, uint32_t port_id) { |
| 313 | snd_seq_port_subscribe_t* subs; |
| 314 | snd_seq_port_subscribe_alloca(&subs); |
| 315 | snd_seq_addr_t sender; |
| 316 | sender.client = device_id; |
| 317 | sender.port = port_id; |
| 318 | snd_seq_port_subscribe_set_sender(subs, &sender); |
| 319 | snd_seq_addr_t dest; |
| 320 | dest.client = in_client_id_; |
| 321 | dest.port = in_port_id_; |
| 322 | snd_seq_port_subscribe_set_dest(subs, &dest); |
| 323 | |
| 324 | int err = snd_seq_unsubscribe_port(in_client_.get(), subs); |
| 325 | if (err != 0) { |
| 326 | LOG(WARNING) << "snd_seq_unsubscribe_port fails: " << snd_strerror(err); |
| 327 | return; |
| 328 | } |
| 329 | } |
| 330 | |
| 331 | void SeqHandler::UnsubscribeOutPort(int out_port_id) { |
| 332 | snd_seq_delete_simple_port(out_client_.get(), out_port_id); |
| 333 | } |
| 334 | |
Prashant Malani | 3c54036 | 2017-09-28 13:35:14 -0700 | [diff] [blame] | 335 | bool SeqHandler::EncodeMidiBytes(int out_port_id, |
| 336 | snd_seq_t* out_client, |
| 337 | const uint8_t* buffer, |
| 338 | size_t buf_len, |
| 339 | snd_midi_event_t* encoder) { |
| 340 | if (buf_len == 0 || buf_len > kMaxBufSize) { |
| 341 | return false; |
| 342 | } |
| 343 | |
Prashant Malani | fd1e200 | 2017-08-09 13:22:59 -0700 | [diff] [blame] | 344 | for (int i = 0; i < buf_len; i++) { |
| 345 | snd_seq_event_t event; |
| 346 | int result = snd_midi_event_encode_byte(encoder, buffer[i], &event); |
Prashant Malani | 3c54036 | 2017-09-28 13:35:14 -0700 | [diff] [blame] | 347 | if (result < 0) { |
| 348 | LOG(ERROR) << "Error snd_midi_event_encode_byte(): " << result; |
| 349 | return false; |
| 350 | } |
Prashant Malani | fd1e200 | 2017-08-09 13:22:59 -0700 | [diff] [blame] | 351 | if (result == 1) { |
| 352 | // Send the message. |
| 353 | snd_seq_ev_set_source(&event, out_port_id); |
| 354 | snd_seq_ev_set_subs(&event); |
| 355 | snd_seq_ev_set_direct(&event); |
Prashant Malani | 3c54036 | 2017-09-28 13:35:14 -0700 | [diff] [blame] | 356 | int expected_length = snd_seq_event_length(&event); |
| 357 | result = SndSeqEventOutputDirect(out_client, &event); |
| 358 | if (result != expected_length) { |
| 359 | LOG(ERROR) << "Error in snd_seq_event_output_direct(): " << result; |
| 360 | return false; |
| 361 | } |
| 362 | return true; |
Prashant Malani | fd1e200 | 2017-08-09 13:22:59 -0700 | [diff] [blame] | 363 | } |
| 364 | } |
Prashant Malani | 3c54036 | 2017-09-28 13:35:14 -0700 | [diff] [blame] | 365 | |
| 366 | // If we reached here, something went wrong. |
| 367 | return false; |
| 368 | } |
| 369 | |
| 370 | void SeqHandler::SendMidiData(int out_port_id, |
| 371 | const uint8_t* buffer, |
| 372 | size_t buf_len) { |
| 373 | snd_midi_event_t* encoder; |
| 374 | int ret = snd_midi_event_new(buf_len, &encoder); |
| 375 | if (ret != 0) { |
| 376 | LOG(ERROR) << "Error snd_midi_event_new(): " << ret; |
| 377 | return; |
| 378 | } |
| 379 | bool success = |
| 380 | EncodeMidiBytes(out_port_id, out_client_.get(), buffer, buf_len, encoder); |
| 381 | if (!success) { |
| 382 | LOG(ERROR) << "Failed to send MIDI data to output port: " << out_port_id; |
| 383 | } |
Prashant Malani | fd1e200 | 2017-08-09 13:22:59 -0700 | [diff] [blame] | 384 | snd_midi_event_free(encoder); |
| 385 | } |
| 386 | |
| 387 | void SeqHandler::ProcessMidiEvent(snd_seq_event_t* event) { |
| 388 | uint32_t device_id = event->source.client; |
| 389 | uint32_t subdevice_num = event->source.port; |
| 390 | |
| 391 | if (event->type == SND_SEQ_EVENT_SYSEX) { |
| 392 | // SysEX, so pass it through without decoding. |
Prashant Malani | 3c54036 | 2017-09-28 13:35:14 -0700 | [diff] [blame] | 393 | handle_rx_data_cb_.Run(0, device_id, subdevice_num, |
Prashant Malani | fd1e200 | 2017-08-09 13:22:59 -0700 | [diff] [blame] | 394 | static_cast<char*>(event->data.ext.ptr), |
| 395 | event->data.ext.len); |
| 396 | } else { |
| 397 | // Normal message, so decode and send. |
| 398 | unsigned char buf[12]; |
| 399 | int64_t count = |
| 400 | snd_midi_event_decode(decoder_.get(), buf, sizeof(buf), event); |
| 401 | if (count <= 0) { |
| 402 | if (count != -ENOENT) { |
| 403 | LOG(ERROR) << "snd_midi_event_decoder failed: " << snd_strerror(count); |
| 404 | } |
| 405 | } else { |
Prashant Malani | 3c54036 | 2017-09-28 13:35:14 -0700 | [diff] [blame] | 406 | handle_rx_data_cb_.Run(0, device_id, subdevice_num, |
| 407 | reinterpret_cast<char*>(buf), count); |
Prashant Malani | fd1e200 | 2017-08-09 13:22:59 -0700 | [diff] [blame] | 408 | } |
| 409 | } |
| 410 | } |
| 411 | |
Prashant Malani | 3c54036 | 2017-09-28 13:35:14 -0700 | [diff] [blame] | 412 | int SeqHandler::SndSeqEventOutputDirect(snd_seq_t* out_client, |
| 413 | snd_seq_event_t* event) { |
| 414 | return snd_seq_event_output_direct(out_client, event); |
| 415 | } |
| 416 | |
Prashant Malani | fd1e200 | 2017-08-09 13:22:59 -0700 | [diff] [blame] | 417 | } // namespace midis |