toyoshim@chromium.org | a97eebf | 2014-01-03 07:52:39 +0000 | [diff] [blame] | 1 | // Copyright 2014 The Chromium 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 | |
dnicoara@chromium.org | 9f2a6f0 | 2014-01-03 21:25:00 +0000 | [diff] [blame] | 5 | #include "media/midi/midi_manager_alsa.h" |
toyoshim@chromium.org | a97eebf | 2014-01-03 07:52:39 +0000 | [diff] [blame] | 6 | |
mostynb | bd69317 | 2015-11-17 19:06:54 -0800 | [diff] [blame] | 7 | #include <errno.h> |
agoode | 975043d | 2015-05-11 00:46:17 -0700 | [diff] [blame] | 8 | #include <poll.h> |
avi | 793390d | 2015-12-22 22:22:36 -0800 | [diff] [blame] | 9 | #include <stddef.h> |
toyoshim@chromium.org | 4a8657c | 2014-02-06 11:23:09 +0000 | [diff] [blame] | 10 | #include <stdlib.h> |
danakj | 75afea0 | 2016-04-25 20:36:04 -0700 | [diff] [blame] | 11 | |
yhirano@chromium.org | cfa642c | 2014-05-01 08:54:41 +0000 | [diff] [blame] | 12 | #include <algorithm> |
| 13 | #include <string> |
limasdf | e59d039 | 2015-11-19 20:28:57 -0800 | [diff] [blame] | 14 | #include <utility> |
toyoshim@chromium.org | a97eebf | 2014-01-03 07:52:39 +0000 | [diff] [blame] | 15 | |
| 16 | #include "base/bind.h" |
agoode | f212b2a | 2015-03-19 12:53:23 -0700 | [diff] [blame] | 17 | #include "base/json/json_string_value_serializer.h" |
toyoshim@chromium.org | a97eebf | 2014-01-03 07:52:39 +0000 | [diff] [blame] | 18 | #include "base/logging.h" |
avi | 793390d | 2015-12-22 22:22:36 -0800 | [diff] [blame] | 19 | #include "base/macros.h" |
danakj | 75afea0 | 2016-04-25 20:36:04 -0700 | [diff] [blame] | 20 | #include "base/memory/ptr_util.h" |
toyoshim@chromium.org | a97eebf | 2014-01-03 07:52:39 +0000 | [diff] [blame] | 21 | #include "base/message_loop/message_loop.h" |
toyoshim@chromium.org | 4a8657c | 2014-02-06 11:23:09 +0000 | [diff] [blame] | 22 | #include "base/posix/eintr_wrapper.h" |
brettw | f7f870f | 2015-06-09 11:05:24 -0700 | [diff] [blame] | 23 | #include "base/posix/safe_strerror.h" |
fdoray | 8baaff0 | 2016-08-25 08:36:37 -0700 | [diff] [blame] | 24 | #include "base/single_thread_task_runner.h" |
agoode | f212b2a | 2015-03-19 12:53:23 -0700 | [diff] [blame] | 25 | #include "base/strings/string_number_conversions.h" |
toyoshim@chromium.org | a97eebf | 2014-01-03 07:52:39 +0000 | [diff] [blame] | 26 | #include "base/strings/stringprintf.h" |
toyoshim@chromium.org | 4a8657c | 2014-02-06 11:23:09 +0000 | [diff] [blame] | 27 | #include "base/time/time.h" |
agoode | f212b2a | 2015-03-19 12:53:23 -0700 | [diff] [blame] | 28 | #include "crypto/sha2.h" |
toyoshim@chromium.org | a97eebf | 2014-01-03 07:52:39 +0000 | [diff] [blame] | 29 | #include "media/midi/midi_port_info.h" |
| 30 | |
toyoshim | e147c5e | 2015-05-07 21:58:31 -0700 | [diff] [blame] | 31 | namespace midi { |
toyoshim@chromium.org | a97eebf | 2014-01-03 07:52:39 +0000 | [diff] [blame] | 32 | |
toyoshim@chromium.org | 4a8657c | 2014-02-06 11:23:09 +0000 | [diff] [blame] | 33 | namespace { |
| 34 | |
toyoshim | 2f3a48f | 2016-10-17 01:54:13 -0700 | [diff] [blame^] | 35 | using midi::mojom::Result; |
| 36 | |
agoode@chromium.org | 2522751 | 2014-06-08 05:12:05 +0000 | [diff] [blame] | 37 | // Per-output buffer. This can be smaller, but then large sysex messages |
| 38 | // will be (harmlessly) split across multiple seq events. This should |
| 39 | // not have any real practical effect, except perhaps to slightly reorder |
| 40 | // realtime messages with respect to sysex. |
| 41 | const size_t kSendBufferSize = 256; |
| 42 | |
agoode | b09423b | 2015-05-11 11:39:57 -0700 | [diff] [blame] | 43 | // Minimum client id for which we will have ALSA card devices for. When we |
| 44 | // are searching for card devices (used to get the path, id, and manufacturer), |
| 45 | // we don't want to get confused by kernel clients that do not have a card. |
| 46 | // See seq_clientmgr.c in the ALSA code for this. |
| 47 | // TODO(agoode): Add proper client -> card export from the kernel to avoid |
| 48 | // hardcoding. |
| 49 | const int kMinimumClientIdForCards = 16; |
| 50 | |
agoode | f212b2a | 2015-03-19 12:53:23 -0700 | [diff] [blame] | 51 | // ALSA constants. |
| 52 | const char kAlsaHw[] = "hw"; |
| 53 | |
agoode | 975043d | 2015-05-11 00:46:17 -0700 | [diff] [blame] | 54 | // udev constants. |
| 55 | const char kUdev[] = "udev"; |
| 56 | const char kUdevSubsystemSound[] = "sound"; |
| 57 | const char kUdevPropertySoundInitialized[] = "SOUND_INITIALIZED"; |
| 58 | const char kUdevActionChange[] = "change"; |
| 59 | const char kUdevActionRemove[] = "remove"; |
| 60 | |
agoode | b09423b | 2015-05-11 11:39:57 -0700 | [diff] [blame] | 61 | const char kUdevIdVendor[] = "ID_VENDOR"; |
| 62 | const char kUdevIdVendorEnc[] = "ID_VENDOR_ENC"; |
| 63 | const char kUdevIdVendorFromDatabase[] = "ID_VENDOR_FROM_DATABASE"; |
| 64 | const char kUdevIdVendorId[] = "ID_VENDOR_ID"; |
| 65 | const char kUdevIdModelId[] = "ID_MODEL_ID"; |
| 66 | const char kUdevIdBus[] = "ID_BUS"; |
| 67 | const char kUdevIdPath[] = "ID_PATH"; |
| 68 | const char kUdevIdUsbInterfaceNum[] = "ID_USB_INTERFACE_NUM"; |
| 69 | const char kUdevIdSerialShort[] = "ID_SERIAL_SHORT"; |
| 70 | |
| 71 | const char kSysattrVendorName[] = "vendor_name"; |
| 72 | const char kSysattrVendor[] = "vendor"; |
| 73 | const char kSysattrModel[] = "model"; |
| 74 | const char kSysattrGuid[] = "guid"; |
| 75 | |
| 76 | const char kCardSyspath[] = "/card"; |
| 77 | |
agoode@chromium.org | 2522751 | 2014-06-08 05:12:05 +0000 | [diff] [blame] | 78 | // Constants for the capabilities we search for in inputs and outputs. |
| 79 | // See http://www.alsa-project.org/alsa-doc/alsa-lib/seq.html. |
| 80 | const unsigned int kRequiredInputPortCaps = |
| 81 | SND_SEQ_PORT_CAP_READ | SND_SEQ_PORT_CAP_SUBS_READ; |
| 82 | const unsigned int kRequiredOutputPortCaps = |
| 83 | SND_SEQ_PORT_CAP_WRITE | SND_SEQ_PORT_CAP_SUBS_WRITE; |
| 84 | |
agoode | 99d6329 | 2015-04-13 08:39:25 -0700 | [diff] [blame] | 85 | const unsigned int kCreateOutputPortCaps = |
| 86 | SND_SEQ_PORT_CAP_READ | SND_SEQ_PORT_CAP_NO_EXPORT; |
| 87 | const unsigned int kCreateInputPortCaps = |
| 88 | SND_SEQ_PORT_CAP_WRITE | SND_SEQ_PORT_CAP_NO_EXPORT; |
| 89 | const unsigned int kCreatePortType = |
| 90 | SND_SEQ_PORT_TYPE_MIDI_GENERIC | SND_SEQ_PORT_TYPE_APPLICATION; |
agoode@chromium.org | 2522751 | 2014-06-08 05:12:05 +0000 | [diff] [blame] | 91 | |
agoode | 99d6329 | 2015-04-13 08:39:25 -0700 | [diff] [blame] | 92 | int AddrToInt(int client, int port) { |
| 93 | return (client << 8) | port; |
agoode | f212b2a | 2015-03-19 12:53:23 -0700 | [diff] [blame] | 94 | } |
agoode | f212b2a | 2015-03-19 12:53:23 -0700 | [diff] [blame] | 95 | |
agoode | b09423b | 2015-05-11 11:39:57 -0700 | [diff] [blame] | 96 | // Returns true if this client has an ALSA card associated with it. |
| 97 | bool IsCardClient(snd_seq_client_type_t type, int client_id) { |
| 98 | return (type == SND_SEQ_KERNEL_CLIENT) && |
| 99 | (client_id >= kMinimumClientIdForCards); |
| 100 | } |
| 101 | |
| 102 | // TODO(agoode): Move this to device/udev_linux. |
| 103 | const std::string UdevDeviceGetPropertyOrSysattr( |
| 104 | struct udev_device* udev_device, |
| 105 | const char* property_key, |
| 106 | const char* sysattr_key) { |
| 107 | // First try the property. |
| 108 | std::string value = |
| 109 | device::UdevDeviceGetPropertyValue(udev_device, property_key); |
| 110 | |
| 111 | // If no property, look for sysattrs and walk up the parent devices too. |
| 112 | while (value.empty() && udev_device) { |
| 113 | value = device::UdevDeviceGetSysattrValue(udev_device, sysattr_key); |
| 114 | udev_device = device::udev_device_get_parent(udev_device); |
| 115 | } |
| 116 | return value; |
| 117 | } |
| 118 | |
| 119 | int GetCardNumber(udev_device* dev) { |
| 120 | const char* syspath = device::udev_device_get_syspath(dev); |
| 121 | if (!syspath) |
| 122 | return -1; |
| 123 | |
| 124 | std::string syspath_str(syspath); |
| 125 | size_t i = syspath_str.rfind(kCardSyspath); |
| 126 | if (i == std::string::npos) |
| 127 | return -1; |
| 128 | |
| 129 | int number; |
| 130 | if (!base::StringToInt(syspath_str.substr(i + strlen(kCardSyspath)), &number)) |
| 131 | return -1; |
| 132 | return number; |
| 133 | } |
| 134 | |
agoode | d87fc0f | 2015-05-21 08:29:31 -0700 | [diff] [blame] | 135 | std::string GetVendor(udev_device* dev) { |
| 136 | // Try to get the vendor string. Sometimes it is encoded. |
| 137 | std::string vendor = device::UdevDecodeString( |
| 138 | device::UdevDeviceGetPropertyValue(dev, kUdevIdVendorEnc)); |
| 139 | // Sometimes it is not encoded. |
| 140 | if (vendor.empty()) |
| 141 | vendor = |
| 142 | UdevDeviceGetPropertyOrSysattr(dev, kUdevIdVendor, kSysattrVendorName); |
| 143 | return vendor; |
| 144 | } |
| 145 | |
agoode | 8caab0b | 2015-03-23 18:48:02 -0700 | [diff] [blame] | 146 | void SetStringIfNonEmpty(base::DictionaryValue* value, |
| 147 | const std::string& path, |
| 148 | const std::string& in_value) { |
| 149 | if (!in_value.empty()) |
| 150 | value->SetString(path, in_value); |
| 151 | } |
| 152 | |
toyoshim@chromium.org | 4a8657c | 2014-02-06 11:23:09 +0000 | [diff] [blame] | 153 | } // namespace |
| 154 | |
toyoshim@chromium.org | c82e66e | 2014-02-04 07:05:47 +0000 | [diff] [blame] | 155 | MidiManagerAlsa::MidiManagerAlsa() |
agoode | b2ead82 | 2016-03-11 12:14:35 -0800 | [diff] [blame] | 156 | : event_thread_("MidiEventThread"), send_thread_("MidiSendThread") {} |
| 157 | |
| 158 | MidiManagerAlsa::~MidiManagerAlsa() { |
| 159 | // Take lock to ensure that the members initialized on the IO thread |
| 160 | // are not destructed here. |
| 161 | base::AutoLock lock(lazy_init_member_lock_); |
| 162 | |
toyoshim | 131beb5 | 2016-07-25 00:42:41 -0700 | [diff] [blame] | 163 | // Extra CHECK to verify all members are already reset. |
| 164 | CHECK(!initialization_thread_checker_); |
| 165 | CHECK(!in_client_); |
| 166 | CHECK(!out_client_); |
| 167 | CHECK(!decoder_); |
| 168 | CHECK(!udev_); |
| 169 | CHECK(!udev_monitor_); |
| 170 | |
| 171 | CHECK(!send_thread_.IsRunning()); |
| 172 | CHECK(!event_thread_.IsRunning()); |
toyoshim@chromium.org | a97eebf | 2014-01-03 07:52:39 +0000 | [diff] [blame] | 173 | } |
| 174 | |
toyoshim@chromium.org | 51c7f53 | 2014-05-01 17:17:32 +0000 | [diff] [blame] | 175 | void MidiManagerAlsa::StartInitialization() { |
agoode | b2ead82 | 2016-03-11 12:14:35 -0800 | [diff] [blame] | 176 | base::AutoLock lock(lazy_init_member_lock_); |
agoode | 5a1aa11 | 2015-06-21 20:51:00 -0700 | [diff] [blame] | 177 | |
agoode | b2ead82 | 2016-03-11 12:14:35 -0800 | [diff] [blame] | 178 | initialization_thread_checker_.reset(new base::ThreadChecker()); |
| 179 | |
| 180 | // Create client handles. |
| 181 | snd_seq_t* tmp_seq = nullptr; |
| 182 | int err = |
| 183 | snd_seq_open(&tmp_seq, kAlsaHw, SND_SEQ_OPEN_INPUT, SND_SEQ_NONBLOCK); |
agoode@chromium.org | 2522751 | 2014-06-08 05:12:05 +0000 | [diff] [blame] | 184 | if (err != 0) { |
| 185 | VLOG(1) << "snd_seq_open fails: " << snd_strerror(err); |
toyoshim | f1b8896 | 2015-07-09 14:14:51 -0700 | [diff] [blame] | 186 | return CompleteInitialization(Result::INITIALIZATION_ERROR); |
agoode@chromium.org | 2522751 | 2014-06-08 05:12:05 +0000 | [diff] [blame] | 187 | } |
agoode | b2ead82 | 2016-03-11 12:14:35 -0800 | [diff] [blame] | 188 | ScopedSndSeqPtr in_client(tmp_seq); |
| 189 | tmp_seq = nullptr; |
| 190 | in_client_id_ = snd_seq_client_id(in_client.get()); |
| 191 | |
| 192 | err = snd_seq_open(&tmp_seq, kAlsaHw, SND_SEQ_OPEN_OUTPUT, 0); |
| 193 | if (err != 0) { |
| 194 | VLOG(1) << "snd_seq_open fails: " << snd_strerror(err); |
| 195 | return CompleteInitialization(Result::INITIALIZATION_ERROR); |
| 196 | } |
| 197 | ScopedSndSeqPtr out_client(tmp_seq); |
| 198 | tmp_seq = nullptr; |
| 199 | out_client_id_ = snd_seq_client_id(out_client.get()); |
agoode@chromium.org | 2522751 | 2014-06-08 05:12:05 +0000 | [diff] [blame] | 200 | |
| 201 | // Name the clients. |
agoode | b2ead82 | 2016-03-11 12:14:35 -0800 | [diff] [blame] | 202 | err = snd_seq_set_client_name(in_client.get(), "Chrome (input)"); |
agoode@chromium.org | 2522751 | 2014-06-08 05:12:05 +0000 | [diff] [blame] | 203 | if (err != 0) { |
| 204 | VLOG(1) << "snd_seq_set_client_name fails: " << snd_strerror(err); |
toyoshim | f1b8896 | 2015-07-09 14:14:51 -0700 | [diff] [blame] | 205 | return CompleteInitialization(Result::INITIALIZATION_ERROR); |
agoode@chromium.org | 2522751 | 2014-06-08 05:12:05 +0000 | [diff] [blame] | 206 | } |
agoode | b2ead82 | 2016-03-11 12:14:35 -0800 | [diff] [blame] | 207 | err = snd_seq_set_client_name(out_client.get(), "Chrome (output)"); |
agoode@chromium.org | 2522751 | 2014-06-08 05:12:05 +0000 | [diff] [blame] | 208 | if (err != 0) { |
| 209 | VLOG(1) << "snd_seq_set_client_name fails: " << snd_strerror(err); |
toyoshim | f1b8896 | 2015-07-09 14:14:51 -0700 | [diff] [blame] | 210 | return CompleteInitialization(Result::INITIALIZATION_ERROR); |
agoode@chromium.org | 2522751 | 2014-06-08 05:12:05 +0000 | [diff] [blame] | 211 | } |
| 212 | |
| 213 | // Create input port. |
agoode | 99d6329 | 2015-04-13 08:39:25 -0700 | [diff] [blame] | 214 | in_port_id_ = snd_seq_create_simple_port( |
agoode | b2ead82 | 2016-03-11 12:14:35 -0800 | [diff] [blame] | 215 | in_client.get(), NULL, kCreateInputPortCaps, kCreatePortType); |
agoode | 99d6329 | 2015-04-13 08:39:25 -0700 | [diff] [blame] | 216 | if (in_port_id_ < 0) { |
| 217 | VLOG(1) << "snd_seq_create_simple_port fails: " |
| 218 | << snd_strerror(in_port_id_); |
toyoshim | f1b8896 | 2015-07-09 14:14:51 -0700 | [diff] [blame] | 219 | return CompleteInitialization(Result::INITIALIZATION_ERROR); |
agoode@chromium.org | 2522751 | 2014-06-08 05:12:05 +0000 | [diff] [blame] | 220 | } |
| 221 | |
| 222 | // Subscribe to the announce port. |
| 223 | snd_seq_port_subscribe_t* subs; |
| 224 | snd_seq_port_subscribe_alloca(&subs); |
| 225 | snd_seq_addr_t announce_sender; |
| 226 | snd_seq_addr_t announce_dest; |
| 227 | announce_sender.client = SND_SEQ_CLIENT_SYSTEM; |
| 228 | announce_sender.port = SND_SEQ_PORT_SYSTEM_ANNOUNCE; |
agoode | 99d6329 | 2015-04-13 08:39:25 -0700 | [diff] [blame] | 229 | announce_dest.client = in_client_id_; |
| 230 | announce_dest.port = in_port_id_; |
agoode@chromium.org | 2522751 | 2014-06-08 05:12:05 +0000 | [diff] [blame] | 231 | snd_seq_port_subscribe_set_sender(subs, &announce_sender); |
| 232 | snd_seq_port_subscribe_set_dest(subs, &announce_dest); |
agoode | b2ead82 | 2016-03-11 12:14:35 -0800 | [diff] [blame] | 233 | err = snd_seq_subscribe_port(in_client.get(), subs); |
agoode@chromium.org | 2522751 | 2014-06-08 05:12:05 +0000 | [diff] [blame] | 234 | if (err != 0) { |
| 235 | VLOG(1) << "snd_seq_subscribe_port on the announce port fails: " |
| 236 | << snd_strerror(err); |
toyoshim | f1b8896 | 2015-07-09 14:14:51 -0700 | [diff] [blame] | 237 | return CompleteInitialization(Result::INITIALIZATION_ERROR); |
agoode@chromium.org | 2522751 | 2014-06-08 05:12:05 +0000 | [diff] [blame] | 238 | } |
| 239 | |
agoode | b2ead82 | 2016-03-11 12:14:35 -0800 | [diff] [blame] | 240 | // Initialize decoder. |
| 241 | snd_midi_event_t* tmp_decoder = nullptr; |
| 242 | snd_midi_event_new(0, &tmp_decoder); |
| 243 | ScopedSndMidiEventPtr decoder(tmp_decoder); |
| 244 | tmp_decoder = nullptr; |
| 245 | snd_midi_event_no_status(decoder.get(), 1); |
agoode@chromium.org | 2522751 | 2014-06-08 05:12:05 +0000 | [diff] [blame] | 246 | |
agoode | b2ead82 | 2016-03-11 12:14:35 -0800 | [diff] [blame] | 247 | // Initialize udev and monitor. |
| 248 | device::ScopedUdevPtr udev(device::udev_new()); |
| 249 | device::ScopedUdevMonitorPtr udev_monitor( |
| 250 | device::udev_monitor_new_from_netlink(udev.get(), kUdev)); |
| 251 | if (!udev_monitor.get()) { |
agoode | 975043d | 2015-05-11 00:46:17 -0700 | [diff] [blame] | 252 | VLOG(1) << "udev_monitor_new_from_netlink fails"; |
toyoshim | f1b8896 | 2015-07-09 14:14:51 -0700 | [diff] [blame] | 253 | return CompleteInitialization(Result::INITIALIZATION_ERROR); |
agoode | 975043d | 2015-05-11 00:46:17 -0700 | [diff] [blame] | 254 | } |
| 255 | err = device::udev_monitor_filter_add_match_subsystem_devtype( |
agoode | b2ead82 | 2016-03-11 12:14:35 -0800 | [diff] [blame] | 256 | udev_monitor.get(), kUdevSubsystemSound, nullptr); |
agoode | 975043d | 2015-05-11 00:46:17 -0700 | [diff] [blame] | 257 | if (err != 0) { |
| 258 | VLOG(1) << "udev_monitor_add_match_subsystem fails: " |
brettw | f7f870f | 2015-06-09 11:05:24 -0700 | [diff] [blame] | 259 | << base::safe_strerror(-err); |
toyoshim | f1b8896 | 2015-07-09 14:14:51 -0700 | [diff] [blame] | 260 | return CompleteInitialization(Result::INITIALIZATION_ERROR); |
agoode | 975043d | 2015-05-11 00:46:17 -0700 | [diff] [blame] | 261 | } |
agoode | b2ead82 | 2016-03-11 12:14:35 -0800 | [diff] [blame] | 262 | err = device::udev_monitor_enable_receiving(udev_monitor.get()); |
agoode | 975043d | 2015-05-11 00:46:17 -0700 | [diff] [blame] | 263 | if (err != 0) { |
brettw | f7f870f | 2015-06-09 11:05:24 -0700 | [diff] [blame] | 264 | VLOG(1) << "udev_monitor_enable_receiving fails: " |
| 265 | << base::safe_strerror(-err); |
toyoshim | f1b8896 | 2015-07-09 14:14:51 -0700 | [diff] [blame] | 266 | return CompleteInitialization(Result::INITIALIZATION_ERROR); |
agoode | 975043d | 2015-05-11 00:46:17 -0700 | [diff] [blame] | 267 | } |
| 268 | |
agoode | b2ead82 | 2016-03-11 12:14:35 -0800 | [diff] [blame] | 269 | // Success! Now, initialize members from the temporaries. Do not |
| 270 | // initialize these earlier, since they need to be destroyed by the |
| 271 | // thread that calls Finalize(), not the destructor thread (and we |
| 272 | // check this in the destructor). |
| 273 | in_client_.reset(in_client.release()); |
| 274 | out_client_.reset(out_client.release()); |
| 275 | decoder_.reset(decoder.release()); |
| 276 | udev_.reset(udev.release()); |
agoode | 740f522 | 2016-07-06 23:46:32 -0700 | [diff] [blame] | 277 | udev_monitor_.reset(udev_monitor.release()); |
agoode | b2ead82 | 2016-03-11 12:14:35 -0800 | [diff] [blame] | 278 | |
| 279 | // Generate hotplug events for existing ports. |
| 280 | // TODO(agoode): Check the return value for failure. |
| 281 | EnumerateAlsaPorts(); |
| 282 | |
| 283 | // Generate hotplug events for existing udev devices. This must be done |
| 284 | // after udev_monitor_enable_receiving() is called. See the algorithm |
| 285 | // at http://www.signal11.us/oss/udev/. |
agoode | 975043d | 2015-05-11 00:46:17 -0700 | [diff] [blame] | 286 | EnumerateUdevCards(); |
| 287 | |
agoode | b2ead82 | 2016-03-11 12:14:35 -0800 | [diff] [blame] | 288 | // Start processing events. Don't do this before enumeration of both |
| 289 | // ALSA and udev. |
agoode@chromium.org | 2522751 | 2014-06-08 05:12:05 +0000 | [diff] [blame] | 290 | event_thread_.Start(); |
fdoray | 8baaff0 | 2016-08-25 08:36:37 -0700 | [diff] [blame] | 291 | event_thread_.task_runner()->PostTask( |
agoode@chromium.org | 2522751 | 2014-06-08 05:12:05 +0000 | [diff] [blame] | 292 | FROM_HERE, |
agoode | bd4be9b | 2015-03-16 19:17:25 -0700 | [diff] [blame] | 293 | base::Bind(&MidiManagerAlsa::ScheduleEventLoop, base::Unretained(this))); |
agoode | b2ead82 | 2016-03-11 12:14:35 -0800 | [diff] [blame] | 294 | send_thread_.Start(); |
agoode@chromium.org | 2522751 | 2014-06-08 05:12:05 +0000 | [diff] [blame] | 295 | |
toyoshim | f1b8896 | 2015-07-09 14:14:51 -0700 | [diff] [blame] | 296 | CompleteInitialization(Result::OK); |
toyoshim@chromium.org | a97eebf | 2014-01-03 07:52:39 +0000 | [diff] [blame] | 297 | } |
| 298 | |
toyoshim | 8e7d6e0 | 2015-10-06 08:47:17 -0700 | [diff] [blame] | 299 | void MidiManagerAlsa::Finalize() { |
agoode | b2ead82 | 2016-03-11 12:14:35 -0800 | [diff] [blame] | 300 | base::AutoLock lock(lazy_init_member_lock_); |
| 301 | DCHECK(initialization_thread_checker_->CalledOnValidThread()); |
| 302 | |
toyoshim | 8e7d6e0 | 2015-10-06 08:47:17 -0700 | [diff] [blame] | 303 | // Tell the event thread it will soon be time to shut down. This gives |
| 304 | // us assurance the thread will stop in case the SND_SEQ_EVENT_CLIENT_EXIT |
| 305 | // message is lost. |
| 306 | { |
| 307 | base::AutoLock lock(shutdown_lock_); |
| 308 | event_thread_shutdown_ = true; |
| 309 | } |
| 310 | |
| 311 | // Stop the send thread. |
| 312 | send_thread_.Stop(); |
| 313 | |
| 314 | // Close the out client. This will trigger the event thread to stop, |
| 315 | // because of SND_SEQ_EVENT_CLIENT_EXIT. |
agoode | b2ead82 | 2016-03-11 12:14:35 -0800 | [diff] [blame] | 316 | out_client_.reset(); |
toyoshim | 8e7d6e0 | 2015-10-06 08:47:17 -0700 | [diff] [blame] | 317 | |
| 318 | // Wait for the event thread to stop. |
| 319 | event_thread_.Stop(); |
agoode | b2ead82 | 2016-03-11 12:14:35 -0800 | [diff] [blame] | 320 | |
| 321 | // Destruct the other stuff we initialized in StartInitialization(). |
| 322 | udev_monitor_.reset(); |
| 323 | udev_.reset(); |
| 324 | decoder_.reset(); |
| 325 | in_client_.reset(); |
| 326 | initialization_thread_checker_.reset(); |
toyoshim | 8e7d6e0 | 2015-10-06 08:47:17 -0700 | [diff] [blame] | 327 | } |
| 328 | |
toyoshim@chromium.org | c82e66e | 2014-02-04 07:05:47 +0000 | [diff] [blame] | 329 | void MidiManagerAlsa::DispatchSendMidiData(MidiManagerClient* client, |
Avi Drissman | 3528fd0 | 2015-12-18 20:11:31 -0500 | [diff] [blame] | 330 | uint32_t port_index, |
| 331 | const std::vector<uint8_t>& data, |
dnicoara@chromium.org | 9f2a6f0 | 2014-01-03 21:25:00 +0000 | [diff] [blame] | 332 | double timestamp) { |
toyoshim@chromium.org | a97eebf | 2014-01-03 07:52:39 +0000 | [diff] [blame] | 333 | base::TimeDelta delay; |
| 334 | if (timestamp != 0.0) { |
| 335 | base::TimeTicks time_to_send = |
| 336 | base::TimeTicks() + base::TimeDelta::FromMicroseconds( |
agoode | bd4be9b | 2015-03-16 19:17:25 -0700 | [diff] [blame] | 337 | timestamp * base::Time::kMicrosecondsPerSecond); |
toyoshim@chromium.org | a97eebf | 2014-01-03 07:52:39 +0000 | [diff] [blame] | 338 | delay = std::max(time_to_send - base::TimeTicks::Now(), base::TimeDelta()); |
| 339 | } |
| 340 | |
fdoray | 8baaff0 | 2016-08-25 08:36:37 -0700 | [diff] [blame] | 341 | send_thread_.task_runner()->PostDelayedTask( |
agoode | bd4be9b | 2015-03-16 19:17:25 -0700 | [diff] [blame] | 342 | FROM_HERE, base::Bind(&MidiManagerAlsa::SendMidiData, |
| 343 | base::Unretained(this), port_index, data), |
| 344 | delay); |
agoode@chromium.org | 2522751 | 2014-06-08 05:12:05 +0000 | [diff] [blame] | 345 | |
| 346 | // Acknowledge send. |
fdoray | 8baaff0 | 2016-08-25 08:36:37 -0700 | [diff] [blame] | 347 | send_thread_.task_runner()->PostTask( |
agoode | ad116b2 | 2015-12-07 00:00:35 -0800 | [diff] [blame] | 348 | FROM_HERE, base::Bind(&MidiManagerAlsa::AccumulateMidiBytesSent, |
| 349 | base::Unretained(this), client, data.size())); |
toyoshim@chromium.org | a97eebf | 2014-01-03 07:52:39 +0000 | [diff] [blame] | 350 | } |
| 351 | |
agoode | d87fc0f | 2015-05-21 08:29:31 -0700 | [diff] [blame] | 352 | MidiManagerAlsa::MidiPort::Id::Id() = default; |
| 353 | |
| 354 | MidiManagerAlsa::MidiPort::Id::Id(const std::string& bus, |
| 355 | const std::string& vendor_id, |
| 356 | const std::string& model_id, |
| 357 | const std::string& usb_interface_num, |
| 358 | const std::string& serial) |
| 359 | : bus_(bus), |
| 360 | vendor_id_(vendor_id), |
| 361 | model_id_(model_id), |
| 362 | usb_interface_num_(usb_interface_num), |
| 363 | serial_(serial) { |
| 364 | } |
| 365 | |
| 366 | MidiManagerAlsa::MidiPort::Id::Id(const Id&) = default; |
| 367 | |
| 368 | MidiManagerAlsa::MidiPort::Id::~Id() = default; |
| 369 | |
| 370 | bool MidiManagerAlsa::MidiPort::Id::operator==(const Id& rhs) const { |
| 371 | return (bus_ == rhs.bus_) && (vendor_id_ == rhs.vendor_id_) && |
| 372 | (model_id_ == rhs.model_id_) && |
| 373 | (usb_interface_num_ == rhs.usb_interface_num_) && |
| 374 | (serial_ == rhs.serial_); |
| 375 | } |
| 376 | |
| 377 | bool MidiManagerAlsa::MidiPort::Id::empty() const { |
| 378 | return bus_.empty() && vendor_id_.empty() && model_id_.empty() && |
| 379 | usb_interface_num_.empty() && serial_.empty(); |
| 380 | } |
| 381 | |
agoode | 99d6329 | 2015-04-13 08:39:25 -0700 | [diff] [blame] | 382 | MidiManagerAlsa::MidiPort::MidiPort(const std::string& path, |
agoode | d87fc0f | 2015-05-21 08:29:31 -0700 | [diff] [blame] | 383 | const Id& id, |
agoode | 99d6329 | 2015-04-13 08:39:25 -0700 | [diff] [blame] | 384 | int client_id, |
| 385 | int port_id, |
| 386 | int midi_device, |
| 387 | const std::string& client_name, |
| 388 | const std::string& port_name, |
| 389 | const std::string& manufacturer, |
| 390 | const std::string& version, |
| 391 | Type type) |
| 392 | : id_(id), |
| 393 | midi_device_(midi_device), |
| 394 | type_(type), |
| 395 | path_(path), |
| 396 | client_id_(client_id), |
| 397 | port_id_(port_id), |
| 398 | client_name_(client_name), |
| 399 | port_name_(port_name), |
| 400 | manufacturer_(manufacturer), |
agoode | 5a1aa11 | 2015-06-21 20:51:00 -0700 | [diff] [blame] | 401 | version_(version) { |
toyoshim@chromium.org | 4a8657c | 2014-02-06 11:23:09 +0000 | [diff] [blame] | 402 | } |
| 403 | |
agoode | b058287 | 2015-05-20 05:22:24 -0700 | [diff] [blame] | 404 | MidiManagerAlsa::MidiPort::~MidiPort() = default; |
toyoshim@chromium.org | 4a8657c | 2014-02-06 11:23:09 +0000 | [diff] [blame] | 405 | |
agoode | 99d6329 | 2015-04-13 08:39:25 -0700 | [diff] [blame] | 406 | // Note: keep synchronized with the MidiPort::Match* methods. |
danakj | 75afea0 | 2016-04-25 20:36:04 -0700 | [diff] [blame] | 407 | std::unique_ptr<base::Value> MidiManagerAlsa::MidiPort::Value() const { |
| 408 | std::unique_ptr<base::DictionaryValue> value(new base::DictionaryValue); |
agoode | 99d6329 | 2015-04-13 08:39:25 -0700 | [diff] [blame] | 409 | |
| 410 | std::string type; |
| 411 | switch (type_) { |
| 412 | case Type::kInput: |
| 413 | type = "input"; |
| 414 | break; |
agoode | 99d6329 | 2015-04-13 08:39:25 -0700 | [diff] [blame] | 415 | case Type::kOutput: |
| 416 | type = "output"; |
| 417 | break; |
| 418 | } |
| 419 | value->SetString("type", type); |
| 420 | SetStringIfNonEmpty(value.get(), "path", path_); |
agoode | 99d6329 | 2015-04-13 08:39:25 -0700 | [diff] [blame] | 421 | SetStringIfNonEmpty(value.get(), "clientName", client_name_); |
| 422 | SetStringIfNonEmpty(value.get(), "portName", port_name_); |
| 423 | value->SetInteger("clientId", client_id_); |
| 424 | value->SetInteger("portId", port_id_); |
| 425 | value->SetInteger("midiDevice", midi_device_); |
| 426 | |
agoode | d87fc0f | 2015-05-21 08:29:31 -0700 | [diff] [blame] | 427 | // Flatten id fields. |
| 428 | SetStringIfNonEmpty(value.get(), "bus", id_.bus()); |
| 429 | SetStringIfNonEmpty(value.get(), "vendorId", id_.vendor_id()); |
| 430 | SetStringIfNonEmpty(value.get(), "modelId", id_.model_id()); |
| 431 | SetStringIfNonEmpty(value.get(), "usbInterfaceNum", id_.usb_interface_num()); |
| 432 | SetStringIfNonEmpty(value.get(), "serial", id_.serial()); |
| 433 | |
dcheng | c2aeece | 2015-12-27 00:54:00 -0800 | [diff] [blame] | 434 | return std::move(value); |
agoode | bd4be9b | 2015-03-16 19:17:25 -0700 | [diff] [blame] | 435 | } |
agoode@chromium.org | 2522751 | 2014-06-08 05:12:05 +0000 | [diff] [blame] | 436 | |
agoode | 99d6329 | 2015-04-13 08:39:25 -0700 | [diff] [blame] | 437 | std::string MidiManagerAlsa::MidiPort::JSONValue() const { |
| 438 | std::string json; |
| 439 | JSONStringValueSerializer serializer(&json); |
| 440 | serializer.Serialize(*Value().get()); |
| 441 | return json; |
agoode | f212b2a | 2015-03-19 12:53:23 -0700 | [diff] [blame] | 442 | } |
| 443 | |
agoode | 99d6329 | 2015-04-13 08:39:25 -0700 | [diff] [blame] | 444 | // TODO(agoode): Do not use SHA256 here. Instead store a persistent |
| 445 | // mapping and just use a UUID or other random string. |
| 446 | // http://crbug.com/465320 |
| 447 | std::string MidiManagerAlsa::MidiPort::OpaqueKey() const { |
Avi Drissman | 3528fd0 | 2015-12-18 20:11:31 -0500 | [diff] [blame] | 448 | uint8_t hash[crypto::kSHA256Length]; |
agoode | 99d6329 | 2015-04-13 08:39:25 -0700 | [diff] [blame] | 449 | crypto::SHA256HashString(JSONValue(), &hash, sizeof(hash)); |
| 450 | return base::HexEncode(&hash, sizeof(hash)); |
agoode | bd4be9b | 2015-03-16 19:17:25 -0700 | [diff] [blame] | 451 | } |
toyoshim@chromium.org | 4a8657c | 2014-02-06 11:23:09 +0000 | [diff] [blame] | 452 | |
agoode | 99d6329 | 2015-04-13 08:39:25 -0700 | [diff] [blame] | 453 | bool MidiManagerAlsa::MidiPort::MatchConnected(const MidiPort& query) const { |
| 454 | // Matches on: |
| 455 | // connected == true |
| 456 | // type |
| 457 | // path |
| 458 | // id |
| 459 | // client_id |
| 460 | // port_id |
| 461 | // midi_device |
| 462 | // client_name |
| 463 | // port_name |
| 464 | return connected() && (type() == query.type()) && (path() == query.path()) && |
| 465 | (id() == query.id()) && (client_id() == query.client_id()) && |
| 466 | (port_id() == query.port_id()) && |
| 467 | (midi_device() == query.midi_device()) && |
| 468 | (client_name() == query.client_name()) && |
| 469 | (port_name() == query.port_name()); |
agoode | bd4be9b | 2015-03-16 19:17:25 -0700 | [diff] [blame] | 470 | } |
| 471 | |
agoode | 99d6329 | 2015-04-13 08:39:25 -0700 | [diff] [blame] | 472 | bool MidiManagerAlsa::MidiPort::MatchCardPass1(const MidiPort& query) const { |
| 473 | // Matches on: |
| 474 | // connected == false |
| 475 | // type |
| 476 | // path |
| 477 | // id |
| 478 | // port_id |
| 479 | // midi_device |
| 480 | return MatchCardPass2(query) && (path() == query.path()); |
agoode | bd4be9b | 2015-03-16 19:17:25 -0700 | [diff] [blame] | 481 | } |
| 482 | |
agoode | 99d6329 | 2015-04-13 08:39:25 -0700 | [diff] [blame] | 483 | bool MidiManagerAlsa::MidiPort::MatchCardPass2(const MidiPort& query) const { |
| 484 | // Matches on: |
| 485 | // connected == false |
| 486 | // type |
| 487 | // id |
| 488 | // port_id |
| 489 | // midi_device |
| 490 | return !connected() && (type() == query.type()) && (id() == query.id()) && |
| 491 | (port_id() == query.port_id()) && |
| 492 | (midi_device() == query.midi_device()); |
agoode | f212b2a | 2015-03-19 12:53:23 -0700 | [diff] [blame] | 493 | } |
| 494 | |
agoode | 99d6329 | 2015-04-13 08:39:25 -0700 | [diff] [blame] | 495 | bool MidiManagerAlsa::MidiPort::MatchNoCardPass1(const MidiPort& query) const { |
| 496 | // Matches on: |
| 497 | // connected == false |
| 498 | // type |
| 499 | // path.empty(), for both this and query |
| 500 | // id.empty(), for both this and query |
| 501 | // client_id |
| 502 | // port_id |
| 503 | // client_name |
| 504 | // port_name |
| 505 | // midi_device == -1, for both this and query |
| 506 | return MatchNoCardPass2(query) && (client_id() == query.client_id()); |
agoode | f212b2a | 2015-03-19 12:53:23 -0700 | [diff] [blame] | 507 | } |
| 508 | |
agoode | 99d6329 | 2015-04-13 08:39:25 -0700 | [diff] [blame] | 509 | bool MidiManagerAlsa::MidiPort::MatchNoCardPass2(const MidiPort& query) const { |
| 510 | // Matches on: |
| 511 | // connected == false |
| 512 | // type |
| 513 | // path.empty(), for both this and query |
| 514 | // id.empty(), for both this and query |
| 515 | // port_id |
| 516 | // client_name |
| 517 | // port_name |
| 518 | // midi_device == -1, for both this and query |
| 519 | return !connected() && (type() == query.type()) && path().empty() && |
| 520 | query.path().empty() && id().empty() && query.id().empty() && |
| 521 | (port_id() == query.port_id()) && |
| 522 | (client_name() == query.client_name()) && |
| 523 | (port_name() == query.port_name()) && (midi_device() == -1) && |
| 524 | (query.midi_device() == -1); |
toyoshim@chromium.org | 4a8657c | 2014-02-06 11:23:09 +0000 | [diff] [blame] | 525 | } |
| 526 | |
agoode | b058287 | 2015-05-20 05:22:24 -0700 | [diff] [blame] | 527 | MidiManagerAlsa::MidiPortStateBase::~MidiPortStateBase() = default; |
agoode | 99d6329 | 2015-04-13 08:39:25 -0700 | [diff] [blame] | 528 | |
| 529 | MidiManagerAlsa::MidiPortStateBase::iterator |
| 530 | MidiManagerAlsa::MidiPortStateBase::Find( |
| 531 | const MidiManagerAlsa::MidiPort& port) { |
| 532 | auto result = FindConnected(port); |
| 533 | if (result == end()) |
| 534 | result = FindDisconnected(port); |
| 535 | return result; |
| 536 | } |
| 537 | |
| 538 | MidiManagerAlsa::MidiPortStateBase::iterator |
| 539 | MidiManagerAlsa::MidiPortStateBase::FindConnected( |
| 540 | const MidiManagerAlsa::MidiPort& port) { |
| 541 | // Exact match required for connected ports. |
danakj | 75afea0 | 2016-04-25 20:36:04 -0700 | [diff] [blame] | 542 | auto it = std::find_if(ports_.begin(), ports_.end(), |
| 543 | [&port](std::unique_ptr<MidiPort>& p) { |
| 544 | return p->MatchConnected(port); |
| 545 | }); |
agoode | 99d6329 | 2015-04-13 08:39:25 -0700 | [diff] [blame] | 546 | return it; |
| 547 | } |
| 548 | |
| 549 | MidiManagerAlsa::MidiPortStateBase::iterator |
| 550 | MidiManagerAlsa::MidiPortStateBase::FindDisconnected( |
| 551 | const MidiManagerAlsa::MidiPort& port) { |
| 552 | // Always match on: |
| 553 | // type |
| 554 | // Possible things to match on: |
| 555 | // path |
| 556 | // id |
| 557 | // client_id |
| 558 | // port_id |
| 559 | // midi_device |
| 560 | // client_name |
| 561 | // port_name |
| 562 | |
| 563 | if (!port.path().empty()) { |
| 564 | // If path is present, then we have a card-based client. |
| 565 | |
| 566 | // Pass 1. Match on path, id, midi_device, port_id. |
| 567 | // This is the best possible match for hardware card-based clients. |
| 568 | // This will also match the empty id correctly for devices without an id. |
danakj | 75afea0 | 2016-04-25 20:36:04 -0700 | [diff] [blame] | 569 | auto it = std::find_if(ports_.begin(), ports_.end(), |
| 570 | [&port](std::unique_ptr<MidiPort>& p) { |
| 571 | return p->MatchCardPass1(port); |
| 572 | }); |
agoode | 99d6329 | 2015-04-13 08:39:25 -0700 | [diff] [blame] | 573 | if (it != ports_.end()) |
| 574 | return it; |
| 575 | |
| 576 | if (!port.id().empty()) { |
| 577 | // Pass 2. Match on id, midi_device, port_id. |
| 578 | // This will give us a high-confidence match when a user moves a device to |
| 579 | // another USB/Firewire/Thunderbolt/etc port, but only works if the device |
| 580 | // has a hardware id. |
danakj | 75afea0 | 2016-04-25 20:36:04 -0700 | [diff] [blame] | 581 | it = std::find_if(ports_.begin(), ports_.end(), |
| 582 | [&port](std::unique_ptr<MidiPort>& p) { |
| 583 | return p->MatchCardPass2(port); |
| 584 | }); |
agoode | 99d6329 | 2015-04-13 08:39:25 -0700 | [diff] [blame] | 585 | if (it != ports_.end()) |
| 586 | return it; |
| 587 | } |
| 588 | } else { |
| 589 | // Else, we have a non-card-based client. |
| 590 | // Pass 1. Match on client_id, port_id, client_name, port_name. |
| 591 | // This will give us a reasonably good match. |
danakj | 75afea0 | 2016-04-25 20:36:04 -0700 | [diff] [blame] | 592 | auto it = std::find_if(ports_.begin(), ports_.end(), |
| 593 | [&port](std::unique_ptr<MidiPort>& p) { |
| 594 | return p->MatchNoCardPass1(port); |
| 595 | }); |
agoode | 99d6329 | 2015-04-13 08:39:25 -0700 | [diff] [blame] | 596 | if (it != ports_.end()) |
| 597 | return it; |
| 598 | |
| 599 | // Pass 2. Match on port_id, client_name, port_name. |
| 600 | // This is weaker but similar to pass 2 in the hardware card-based clients |
| 601 | // match. |
danakj | 75afea0 | 2016-04-25 20:36:04 -0700 | [diff] [blame] | 602 | it = std::find_if(ports_.begin(), ports_.end(), |
| 603 | [&port](std::unique_ptr<MidiPort>& p) { |
| 604 | return p->MatchNoCardPass2(port); |
| 605 | }); |
agoode | 99d6329 | 2015-04-13 08:39:25 -0700 | [diff] [blame] | 606 | if (it != ports_.end()) |
| 607 | return it; |
| 608 | } |
| 609 | |
| 610 | // No match. |
| 611 | return ports_.end(); |
| 612 | } |
| 613 | |
agoode | b058287 | 2015-05-20 05:22:24 -0700 | [diff] [blame] | 614 | MidiManagerAlsa::MidiPortStateBase::MidiPortStateBase() = default; |
agoode | 99d6329 | 2015-04-13 08:39:25 -0700 | [diff] [blame] | 615 | |
agoode | df1b9ff | 2015-06-25 18:14:50 -0700 | [diff] [blame] | 616 | MidiManagerAlsa::MidiPortState::MidiPortState() = default; |
agoode | 99d6329 | 2015-04-13 08:39:25 -0700 | [diff] [blame] | 617 | |
danakj | 75afea0 | 2016-04-25 20:36:04 -0700 | [diff] [blame] | 618 | uint32_t MidiManagerAlsa::MidiPortState::push_back( |
| 619 | std::unique_ptr<MidiPort> port) { |
agoode | 99d6329 | 2015-04-13 08:39:25 -0700 | [diff] [blame] | 620 | // Add the web midi index. |
Avi Drissman | 3528fd0 | 2015-12-18 20:11:31 -0500 | [diff] [blame] | 621 | uint32_t web_port_index = 0; |
agoode | 99d6329 | 2015-04-13 08:39:25 -0700 | [diff] [blame] | 622 | switch (port->type()) { |
| 623 | case MidiPort::Type::kInput: |
| 624 | web_port_index = num_input_ports_++; |
| 625 | break; |
| 626 | case MidiPort::Type::kOutput: |
| 627 | web_port_index = num_output_ports_++; |
| 628 | break; |
| 629 | } |
| 630 | port->set_web_port_index(web_port_index); |
dcheng | c2aeece | 2015-12-27 00:54:00 -0800 | [diff] [blame] | 631 | MidiPortStateBase::push_back(std::move(port)); |
agoode | 99d6329 | 2015-04-13 08:39:25 -0700 | [diff] [blame] | 632 | return web_port_index; |
| 633 | } |
| 634 | |
agoode | df1b9ff | 2015-06-25 18:14:50 -0700 | [diff] [blame] | 635 | MidiManagerAlsa::AlsaSeqState::AlsaSeqState() = default; |
agoode | 99d6329 | 2015-04-13 08:39:25 -0700 | [diff] [blame] | 636 | |
agoode | b058287 | 2015-05-20 05:22:24 -0700 | [diff] [blame] | 637 | MidiManagerAlsa::AlsaSeqState::~AlsaSeqState() = default; |
agoode | 99d6329 | 2015-04-13 08:39:25 -0700 | [diff] [blame] | 638 | |
| 639 | void MidiManagerAlsa::AlsaSeqState::ClientStart(int client_id, |
| 640 | const std::string& client_name, |
| 641 | snd_seq_client_type_t type) { |
| 642 | ClientExit(client_id); |
ricea | 37e4576 | 2016-08-25 02:43:39 -0700 | [diff] [blame] | 643 | clients_.insert( |
| 644 | std::make_pair(client_id, base::MakeUnique<Client>(client_name, type))); |
agoode | b09423b | 2015-05-11 11:39:57 -0700 | [diff] [blame] | 645 | if (IsCardClient(type, client_id)) |
| 646 | ++card_client_count_; |
agoode | 99d6329 | 2015-04-13 08:39:25 -0700 | [diff] [blame] | 647 | } |
| 648 | |
| 649 | bool MidiManagerAlsa::AlsaSeqState::ClientStarted(int client_id) { |
| 650 | return clients_.find(client_id) != clients_.end(); |
| 651 | } |
| 652 | |
| 653 | void MidiManagerAlsa::AlsaSeqState::ClientExit(int client_id) { |
| 654 | auto it = clients_.find(client_id); |
| 655 | if (it != clients_.end()) { |
agoode | b09423b | 2015-05-11 11:39:57 -0700 | [diff] [blame] | 656 | if (IsCardClient(it->second->type(), client_id)) |
| 657 | --card_client_count_; |
agoode | 99d6329 | 2015-04-13 08:39:25 -0700 | [diff] [blame] | 658 | clients_.erase(it); |
| 659 | } |
| 660 | } |
| 661 | |
| 662 | void MidiManagerAlsa::AlsaSeqState::PortStart( |
| 663 | int client_id, |
| 664 | int port_id, |
| 665 | const std::string& port_name, |
| 666 | MidiManagerAlsa::AlsaSeqState::PortDirection direction, |
| 667 | bool midi) { |
| 668 | auto it = clients_.find(client_id); |
| 669 | if (it != clients_.end()) |
| 670 | it->second->AddPort(port_id, |
ricea | 37e4576 | 2016-08-25 02:43:39 -0700 | [diff] [blame] | 671 | base::MakeUnique<Port>(port_name, direction, midi)); |
agoode | 99d6329 | 2015-04-13 08:39:25 -0700 | [diff] [blame] | 672 | } |
| 673 | |
| 674 | void MidiManagerAlsa::AlsaSeqState::PortExit(int client_id, int port_id) { |
| 675 | auto it = clients_.find(client_id); |
| 676 | if (it != clients_.end()) |
| 677 | it->second->RemovePort(port_id); |
| 678 | } |
| 679 | |
| 680 | snd_seq_client_type_t MidiManagerAlsa::AlsaSeqState::ClientType( |
| 681 | int client_id) const { |
| 682 | auto it = clients_.find(client_id); |
| 683 | if (it == clients_.end()) |
| 684 | return SND_SEQ_USER_CLIENT; |
| 685 | return it->second->type(); |
| 686 | } |
| 687 | |
danakj | 75afea0 | 2016-04-25 20:36:04 -0700 | [diff] [blame] | 688 | std::unique_ptr<MidiManagerAlsa::TemporaryMidiPortState> |
agoode | b09423b | 2015-05-11 11:39:57 -0700 | [diff] [blame] | 689 | MidiManagerAlsa::AlsaSeqState::ToMidiPortState(const AlsaCardMap& alsa_cards) { |
danakj | 75afea0 | 2016-04-25 20:36:04 -0700 | [diff] [blame] | 690 | std::unique_ptr<MidiManagerAlsa::TemporaryMidiPortState> midi_ports( |
agoode | 99d6329 | 2015-04-13 08:39:25 -0700 | [diff] [blame] | 691 | new TemporaryMidiPortState); |
agoode | b09423b | 2015-05-11 11:39:57 -0700 | [diff] [blame] | 692 | auto card_it = alsa_cards.begin(); |
agoode | 99d6329 | 2015-04-13 08:39:25 -0700 | [diff] [blame] | 693 | |
agoode | b09423b | 2015-05-11 11:39:57 -0700 | [diff] [blame] | 694 | int card_midi_device = -1; |
agoode | 99d6329 | 2015-04-13 08:39:25 -0700 | [diff] [blame] | 695 | for (const auto& client_pair : clients_) { |
| 696 | int client_id = client_pair.first; |
vmpstr | 0205abb | 2016-06-28 18:50:56 -0700 | [diff] [blame] | 697 | auto* client = client_pair.second.get(); |
agoode | 99d6329 | 2015-04-13 08:39:25 -0700 | [diff] [blame] | 698 | |
| 699 | // Get client metadata. |
| 700 | const std::string client_name = client->name(); |
| 701 | std::string manufacturer; |
| 702 | std::string driver; |
| 703 | std::string path; |
agoode | d87fc0f | 2015-05-21 08:29:31 -0700 | [diff] [blame] | 704 | MidiPort::Id id; |
agoode | 99d6329 | 2015-04-13 08:39:25 -0700 | [diff] [blame] | 705 | std::string card_name; |
| 706 | std::string card_longname; |
| 707 | int midi_device = -1; |
| 708 | |
agoode | b09423b | 2015-05-11 11:39:57 -0700 | [diff] [blame] | 709 | if (IsCardClient(client->type(), client_id)) { |
| 710 | auto& card = card_it->second; |
| 711 | if (card_midi_device == -1) |
| 712 | card_midi_device = 0; |
| 713 | |
| 714 | manufacturer = card->manufacturer(); |
agoode | d87fc0f | 2015-05-21 08:29:31 -0700 | [diff] [blame] | 715 | path = card->path(); |
| 716 | id = MidiPort::Id(card->bus(), card->vendor_id(), card->model_id(), |
| 717 | card->usb_interface_num(), card->serial()); |
| 718 | card_name = card->name(); |
| 719 | card_longname = card->longname(); |
agoode | b09423b | 2015-05-11 11:39:57 -0700 | [diff] [blame] | 720 | midi_device = card_midi_device; |
| 721 | |
| 722 | ++card_midi_device; |
| 723 | if (card_midi_device >= card->midi_device_count()) { |
| 724 | card_midi_device = -1; |
| 725 | ++card_it; |
| 726 | } |
| 727 | } |
| 728 | |
agoode | 99d6329 | 2015-04-13 08:39:25 -0700 | [diff] [blame] | 729 | for (const auto& port_pair : *client) { |
| 730 | int port_id = port_pair.first; |
| 731 | const auto& port = port_pair.second; |
| 732 | |
| 733 | if (port->midi()) { |
| 734 | std::string version; |
| 735 | if (!driver.empty()) { |
| 736 | version = driver + " / "; |
| 737 | } |
| 738 | version += |
| 739 | base::StringPrintf("ALSA library version %d.%d.%d", SND_LIB_MAJOR, |
| 740 | SND_LIB_MINOR, SND_LIB_SUBMINOR); |
| 741 | PortDirection direction = port->direction(); |
| 742 | if (direction == PortDirection::kInput || |
| 743 | direction == PortDirection::kDuplex) { |
ricea | 37e4576 | 2016-08-25 02:43:39 -0700 | [diff] [blame] | 744 | midi_ports->push_back(base::MakeUnique<MidiPort>( |
agoode | 99d6329 | 2015-04-13 08:39:25 -0700 | [diff] [blame] | 745 | path, id, client_id, port_id, midi_device, client->name(), |
ricea | 37e4576 | 2016-08-25 02:43:39 -0700 | [diff] [blame] | 746 | port->name(), manufacturer, version, MidiPort::Type::kInput)); |
agoode | 99d6329 | 2015-04-13 08:39:25 -0700 | [diff] [blame] | 747 | } |
| 748 | if (direction == PortDirection::kOutput || |
| 749 | direction == PortDirection::kDuplex) { |
ricea | 37e4576 | 2016-08-25 02:43:39 -0700 | [diff] [blame] | 750 | midi_ports->push_back(base::MakeUnique<MidiPort>( |
agoode | 99d6329 | 2015-04-13 08:39:25 -0700 | [diff] [blame] | 751 | path, id, client_id, port_id, midi_device, client->name(), |
ricea | 37e4576 | 2016-08-25 02:43:39 -0700 | [diff] [blame] | 752 | port->name(), manufacturer, version, MidiPort::Type::kOutput)); |
agoode | 99d6329 | 2015-04-13 08:39:25 -0700 | [diff] [blame] | 753 | } |
| 754 | } |
| 755 | } |
| 756 | } |
| 757 | |
dcheng | c2aeece | 2015-12-27 00:54:00 -0800 | [diff] [blame] | 758 | return midi_ports; |
agoode | 99d6329 | 2015-04-13 08:39:25 -0700 | [diff] [blame] | 759 | } |
| 760 | |
| 761 | MidiManagerAlsa::AlsaSeqState::Port::Port( |
| 762 | const std::string& name, |
| 763 | MidiManagerAlsa::AlsaSeqState::PortDirection direction, |
| 764 | bool midi) |
| 765 | : name_(name), direction_(direction), midi_(midi) { |
| 766 | } |
| 767 | |
agoode | b058287 | 2015-05-20 05:22:24 -0700 | [diff] [blame] | 768 | MidiManagerAlsa::AlsaSeqState::Port::~Port() = default; |
agoode | 99d6329 | 2015-04-13 08:39:25 -0700 | [diff] [blame] | 769 | |
| 770 | MidiManagerAlsa::AlsaSeqState::Client::Client(const std::string& name, |
| 771 | snd_seq_client_type_t type) |
mgiuca | d9af845 | 2015-06-25 02:11:57 -0700 | [diff] [blame] | 772 | : name_(name), type_(type) { |
agoode | 99d6329 | 2015-04-13 08:39:25 -0700 | [diff] [blame] | 773 | } |
| 774 | |
agoode | b058287 | 2015-05-20 05:22:24 -0700 | [diff] [blame] | 775 | MidiManagerAlsa::AlsaSeqState::Client::~Client() = default; |
agoode | 99d6329 | 2015-04-13 08:39:25 -0700 | [diff] [blame] | 776 | |
danakj | 75afea0 | 2016-04-25 20:36:04 -0700 | [diff] [blame] | 777 | void MidiManagerAlsa::AlsaSeqState::Client::AddPort( |
| 778 | int addr, |
| 779 | std::unique_ptr<Port> port) { |
limasdf | e59d039 | 2015-11-19 20:28:57 -0800 | [diff] [blame] | 780 | ports_[addr] = std::move(port); |
agoode | 99d6329 | 2015-04-13 08:39:25 -0700 | [diff] [blame] | 781 | } |
| 782 | |
| 783 | void MidiManagerAlsa::AlsaSeqState::Client::RemovePort(int addr) { |
mgiuca | d9af845 | 2015-06-25 02:11:57 -0700 | [diff] [blame] | 784 | ports_.erase(addr); |
agoode | 99d6329 | 2015-04-13 08:39:25 -0700 | [diff] [blame] | 785 | } |
| 786 | |
| 787 | MidiManagerAlsa::AlsaSeqState::Client::PortMap::const_iterator |
| 788 | MidiManagerAlsa::AlsaSeqState::Client::begin() const { |
| 789 | return ports_.begin(); |
| 790 | } |
| 791 | |
| 792 | MidiManagerAlsa::AlsaSeqState::Client::PortMap::const_iterator |
| 793 | MidiManagerAlsa::AlsaSeqState::Client::end() const { |
| 794 | return ports_.end(); |
agoode | af6e9f5 | 2015-03-24 10:23:49 -0700 | [diff] [blame] | 795 | } |
| 796 | |
agoode | b09423b | 2015-05-11 11:39:57 -0700 | [diff] [blame] | 797 | MidiManagerAlsa::AlsaCard::AlsaCard(udev_device* dev, |
agoode | d87fc0f | 2015-05-21 08:29:31 -0700 | [diff] [blame] | 798 | const std::string& name, |
| 799 | const std::string& longname, |
| 800 | const std::string& driver, |
agoode | b09423b | 2015-05-11 11:39:57 -0700 | [diff] [blame] | 801 | int midi_device_count) |
agoode | d87fc0f | 2015-05-21 08:29:31 -0700 | [diff] [blame] | 802 | : name_(name), |
| 803 | longname_(longname), |
| 804 | driver_(driver), |
| 805 | path_(device::UdevDeviceGetPropertyValue(dev, kUdevIdPath)), |
| 806 | bus_(device::UdevDeviceGetPropertyValue(dev, kUdevIdBus)), |
| 807 | vendor_id_( |
| 808 | UdevDeviceGetPropertyOrSysattr(dev, kUdevIdVendorId, kSysattrVendor)), |
| 809 | model_id_( |
| 810 | UdevDeviceGetPropertyOrSysattr(dev, kUdevIdModelId, kSysattrModel)), |
| 811 | usb_interface_num_( |
| 812 | device::UdevDeviceGetPropertyValue(dev, kUdevIdUsbInterfaceNum)), |
| 813 | serial_(UdevDeviceGetPropertyOrSysattr(dev, |
| 814 | kUdevIdSerialShort, |
| 815 | kSysattrGuid)), |
| 816 | midi_device_count_(midi_device_count), |
| 817 | manufacturer_(ExtractManufacturerString( |
| 818 | GetVendor(dev), |
| 819 | vendor_id_, |
| 820 | device::UdevDeviceGetPropertyValue(dev, kUdevIdVendorFromDatabase), |
| 821 | name, |
| 822 | longname)) { |
agoode | b09423b | 2015-05-11 11:39:57 -0700 | [diff] [blame] | 823 | } |
| 824 | |
agoode | b058287 | 2015-05-20 05:22:24 -0700 | [diff] [blame] | 825 | MidiManagerAlsa::AlsaCard::~AlsaCard() = default; |
agoode | b09423b | 2015-05-11 11:39:57 -0700 | [diff] [blame] | 826 | |
agoode | 55a8b52 | 2015-03-08 12:40:17 -0700 | [diff] [blame] | 827 | // static |
agoode | b09423b | 2015-05-11 11:39:57 -0700 | [diff] [blame] | 828 | std::string MidiManagerAlsa::AlsaCard::ExtractManufacturerString( |
agoode | 5e4e9cd | 2015-03-09 12:34:24 -0700 | [diff] [blame] | 829 | const std::string& udev_id_vendor, |
agoode | 55a8b52 | 2015-03-08 12:40:17 -0700 | [diff] [blame] | 830 | const std::string& udev_id_vendor_id, |
| 831 | const std::string& udev_id_vendor_from_database, |
| 832 | const std::string& alsa_name, |
| 833 | const std::string& alsa_longname) { |
| 834 | // Let's try to determine the manufacturer. Here is the ordered preference |
| 835 | // in extraction: |
agoode | f212b2a | 2015-03-19 12:53:23 -0700 | [diff] [blame] | 836 | // 1. Vendor name from the hardware device string, from udev properties |
| 837 | // or sysattrs. |
agoode | 5e4e9cd | 2015-03-09 12:34:24 -0700 | [diff] [blame] | 838 | // 2. Vendor name from the udev database (property ID_VENDOR_FROM_DATABASE). |
agoode | 55a8b52 | 2015-03-08 12:40:17 -0700 | [diff] [blame] | 839 | // 3. Heuristic from ALSA. |
| 840 | |
agoode | e83758c | 2015-03-23 22:07:54 -0700 | [diff] [blame] | 841 | // Is the vendor string present and not just the vendor hex id? |
| 842 | if (!udev_id_vendor.empty() && (udev_id_vendor != udev_id_vendor_id)) { |
agoode | 55a8b52 | 2015-03-08 12:40:17 -0700 | [diff] [blame] | 843 | return udev_id_vendor; |
| 844 | } |
| 845 | |
| 846 | // Is there a vendor string in the hardware database? |
| 847 | if (!udev_id_vendor_from_database.empty()) { |
| 848 | return udev_id_vendor_from_database; |
| 849 | } |
| 850 | |
| 851 | // Ok, udev gave us nothing useful, or was unavailable. So try a heuristic. |
| 852 | // We assume that card longname is in the format of |
| 853 | // "<manufacturer> <name> at <bus>". Otherwise, we give up to detect |
| 854 | // a manufacturer name here. |
| 855 | size_t at_index = alsa_longname.rfind(" at "); |
agoode | f212b2a | 2015-03-19 12:53:23 -0700 | [diff] [blame] | 856 | if (at_index && at_index != std::string::npos) { |
agoode | 55a8b52 | 2015-03-08 12:40:17 -0700 | [diff] [blame] | 857 | size_t name_index = alsa_longname.rfind(alsa_name, at_index - 1); |
agoode | f212b2a | 2015-03-19 12:53:23 -0700 | [diff] [blame] | 858 | if (name_index && name_index != std::string::npos) |
agoode | 55a8b52 | 2015-03-08 12:40:17 -0700 | [diff] [blame] | 859 | return alsa_longname.substr(0, name_index - 1); |
| 860 | } |
| 861 | |
| 862 | // Failure. |
| 863 | return ""; |
| 864 | } |
| 865 | |
Avi Drissman | 3528fd0 | 2015-12-18 20:11:31 -0500 | [diff] [blame] | 866 | void MidiManagerAlsa::SendMidiData(uint32_t port_index, |
| 867 | const std::vector<uint8_t>& data) { |
skyostil | 93e2ec2 | 2015-06-17 08:49:09 -0700 | [diff] [blame] | 868 | DCHECK(send_thread_.task_runner()->BelongsToCurrentThread()); |
agoode | bd4be9b | 2015-03-16 19:17:25 -0700 | [diff] [blame] | 869 | |
agoode | f212b2a | 2015-03-19 12:53:23 -0700 | [diff] [blame] | 870 | snd_midi_event_t* encoder; |
| 871 | snd_midi_event_new(kSendBufferSize, &encoder); |
agoode | 5a1aa11 | 2015-06-21 20:51:00 -0700 | [diff] [blame] | 872 | for (const auto datum : data) { |
agoode | bd4be9b | 2015-03-16 19:17:25 -0700 | [diff] [blame] | 873 | snd_seq_event_t event; |
agoode | 5a1aa11 | 2015-06-21 20:51:00 -0700 | [diff] [blame] | 874 | int result = snd_midi_event_encode_byte(encoder, datum, &event); |
agoode | bd4be9b | 2015-03-16 19:17:25 -0700 | [diff] [blame] | 875 | if (result == 1) { |
| 876 | // Full event, send it. |
agoode | 99d6329 | 2015-04-13 08:39:25 -0700 | [diff] [blame] | 877 | base::AutoLock lock(out_ports_lock_); |
| 878 | auto it = out_ports_.find(port_index); |
| 879 | if (it != out_ports_.end()) { |
| 880 | snd_seq_ev_set_source(&event, it->second); |
| 881 | snd_seq_ev_set_subs(&event); |
| 882 | snd_seq_ev_set_direct(&event); |
agoode | 5a1aa11 | 2015-06-21 20:51:00 -0700 | [diff] [blame] | 883 | snd_seq_event_output_direct(out_client_.get(), &event); |
agoode | 99d6329 | 2015-04-13 08:39:25 -0700 | [diff] [blame] | 884 | } |
agoode | bd4be9b | 2015-03-16 19:17:25 -0700 | [diff] [blame] | 885 | } |
| 886 | } |
agoode | f212b2a | 2015-03-19 12:53:23 -0700 | [diff] [blame] | 887 | snd_midi_event_free(encoder); |
agoode | bd4be9b | 2015-03-16 19:17:25 -0700 | [diff] [blame] | 888 | } |
| 889 | |
| 890 | void MidiManagerAlsa::ScheduleEventLoop() { |
fdoray | 8baaff0 | 2016-08-25 08:36:37 -0700 | [diff] [blame] | 891 | event_thread_.task_runner()->PostTask( |
agoode | bd4be9b | 2015-03-16 19:17:25 -0700 | [diff] [blame] | 892 | FROM_HERE, |
| 893 | base::Bind(&MidiManagerAlsa::EventLoop, base::Unretained(this))); |
| 894 | } |
| 895 | |
| 896 | void MidiManagerAlsa::EventLoop() { |
agoode | 975043d | 2015-05-11 00:46:17 -0700 | [diff] [blame] | 897 | bool loop_again = true; |
agoode | bd4be9b | 2015-03-16 19:17:25 -0700 | [diff] [blame] | 898 | |
agoode | 975043d | 2015-05-11 00:46:17 -0700 | [diff] [blame] | 899 | struct pollfd pfd[2]; |
agoode | 5a1aa11 | 2015-06-21 20:51:00 -0700 | [diff] [blame] | 900 | snd_seq_poll_descriptors(in_client_.get(), &pfd[0], 1, POLLIN); |
agoode | 975043d | 2015-05-11 00:46:17 -0700 | [diff] [blame] | 901 | pfd[1].fd = device::udev_monitor_get_fd(udev_monitor_.get()); |
| 902 | pfd[1].events = POLLIN; |
agoode | bd4be9b | 2015-03-16 19:17:25 -0700 | [diff] [blame] | 903 | |
agoode | 975043d | 2015-05-11 00:46:17 -0700 | [diff] [blame] | 904 | int err = HANDLE_EINTR(poll(pfd, arraysize(pfd), -1)); |
| 905 | if (err < 0) { |
brettw | f7f870f | 2015-06-09 11:05:24 -0700 | [diff] [blame] | 906 | VLOG(1) << "poll fails: " << base::safe_strerror(errno); |
agoode | 975043d | 2015-05-11 00:46:17 -0700 | [diff] [blame] | 907 | loop_again = false; |
agoode | af6e9f5 | 2015-03-24 10:23:49 -0700 | [diff] [blame] | 908 | } else { |
agoode | 975043d | 2015-05-11 00:46:17 -0700 | [diff] [blame] | 909 | if (pfd[0].revents & POLLIN) { |
| 910 | // Read available incoming MIDI data. |
| 911 | int remaining; |
| 912 | double timestamp = |
| 913 | (base::TimeTicks::Now() - base::TimeTicks()).InSecondsF(); |
| 914 | do { |
| 915 | snd_seq_event_t* event; |
agoode | 5a1aa11 | 2015-06-21 20:51:00 -0700 | [diff] [blame] | 916 | err = snd_seq_event_input(in_client_.get(), &event); |
| 917 | remaining = snd_seq_event_input_pending(in_client_.get(), 0); |
agoode | 975043d | 2015-05-11 00:46:17 -0700 | [diff] [blame] | 918 | |
| 919 | if (err == -ENOSPC) { |
| 920 | // Handle out of space error. |
| 921 | VLOG(1) << "snd_seq_event_input detected buffer overrun"; |
| 922 | // We've lost events: check another way to see if we need to shut |
| 923 | // down. |
| 924 | base::AutoLock lock(shutdown_lock_); |
| 925 | if (event_thread_shutdown_) |
| 926 | loop_again = false; |
| 927 | } else if (err == -EAGAIN) { |
| 928 | // We've read all the data. |
| 929 | } else if (err < 0) { |
| 930 | // Handle other errors. |
| 931 | VLOG(1) << "snd_seq_event_input fails: " << snd_strerror(err); |
| 932 | // TODO(agoode): Use RecordAction() or similar to log this. |
| 933 | loop_again = false; |
| 934 | } else if (event->source.client == SND_SEQ_CLIENT_SYSTEM && |
| 935 | event->source.port == SND_SEQ_PORT_SYSTEM_ANNOUNCE) { |
| 936 | // Handle announce events. |
| 937 | switch (event->type) { |
| 938 | case SND_SEQ_EVENT_PORT_START: |
| 939 | // Don't use SND_SEQ_EVENT_CLIENT_START because the |
| 940 | // client name may not be set by the time we query |
| 941 | // it. It should be set by the time ports are made. |
| 942 | ProcessClientStartEvent(event->data.addr.client); |
| 943 | ProcessPortStartEvent(event->data.addr); |
| 944 | break; |
| 945 | case SND_SEQ_EVENT_CLIENT_EXIT: |
| 946 | // Check for disconnection of our "out" client. This means "shut |
| 947 | // down". |
| 948 | if (event->data.addr.client == out_client_id_) { |
| 949 | loop_again = false; |
| 950 | remaining = 0; |
| 951 | } else |
| 952 | ProcessClientExitEvent(event->data.addr); |
| 953 | break; |
| 954 | case SND_SEQ_EVENT_PORT_EXIT: |
| 955 | ProcessPortExitEvent(event->data.addr); |
| 956 | break; |
| 957 | } |
| 958 | } else { |
| 959 | // Normal operation. |
| 960 | ProcessSingleEvent(event, timestamp); |
| 961 | } |
| 962 | } while (remaining > 0); |
| 963 | } |
| 964 | if (pfd[1].revents & POLLIN) { |
| 965 | device::ScopedUdevDevicePtr dev( |
| 966 | device::udev_monitor_receive_device(udev_monitor_.get())); |
| 967 | if (dev.get()) |
| 968 | ProcessUdevEvent(dev.get()); |
| 969 | else |
| 970 | VLOG(1) << "udev_monitor_receive_device fails"; |
| 971 | } |
agoode | bd4be9b | 2015-03-16 19:17:25 -0700 | [diff] [blame] | 972 | } |
| 973 | |
agoode | bd4be9b | 2015-03-16 19:17:25 -0700 | [diff] [blame] | 974 | // Do again. |
agoode | 975043d | 2015-05-11 00:46:17 -0700 | [diff] [blame] | 975 | if (loop_again) |
| 976 | ScheduleEventLoop(); |
agoode | bd4be9b | 2015-03-16 19:17:25 -0700 | [diff] [blame] | 977 | } |
| 978 | |
| 979 | void MidiManagerAlsa::ProcessSingleEvent(snd_seq_event_t* event, |
| 980 | double timestamp) { |
agoode | 99d6329 | 2015-04-13 08:39:25 -0700 | [diff] [blame] | 981 | auto source_it = |
| 982 | source_map_.find(AddrToInt(event->source.client, event->source.port)); |
agoode | bd4be9b | 2015-03-16 19:17:25 -0700 | [diff] [blame] | 983 | if (source_it != source_map_.end()) { |
Avi Drissman | 3528fd0 | 2015-12-18 20:11:31 -0500 | [diff] [blame] | 984 | uint32_t source = source_it->second; |
agoode | bd4be9b | 2015-03-16 19:17:25 -0700 | [diff] [blame] | 985 | if (event->type == SND_SEQ_EVENT_SYSEX) { |
| 986 | // Special! Variable-length sysex. |
Avi Drissman | 3528fd0 | 2015-12-18 20:11:31 -0500 | [diff] [blame] | 987 | ReceiveMidiData(source, static_cast<const uint8_t*>(event->data.ext.ptr), |
agoode | bd4be9b | 2015-03-16 19:17:25 -0700 | [diff] [blame] | 988 | event->data.ext.len, timestamp); |
| 989 | } else { |
| 990 | // Otherwise, decode this and send that on. |
| 991 | unsigned char buf[12]; |
agoode | 5a1aa11 | 2015-06-21 20:51:00 -0700 | [diff] [blame] | 992 | long count = |
| 993 | snd_midi_event_decode(decoder_.get(), buf, sizeof(buf), event); |
agoode | bd4be9b | 2015-03-16 19:17:25 -0700 | [diff] [blame] | 994 | if (count <= 0) { |
| 995 | if (count != -ENOENT) { |
| 996 | // ENOENT means that it's not a MIDI message, which is not an |
| 997 | // error, but other negative values are errors for us. |
| 998 | VLOG(1) << "snd_midi_event_decoder fails " << snd_strerror(count); |
| 999 | // TODO(agoode): Record this failure. |
| 1000 | } |
| 1001 | } else { |
| 1002 | ReceiveMidiData(source, buf, count, timestamp); |
| 1003 | } |
| 1004 | } |
| 1005 | } |
| 1006 | } |
| 1007 | |
agoode | 99d6329 | 2015-04-13 08:39:25 -0700 | [diff] [blame] | 1008 | void MidiManagerAlsa::ProcessClientStartEvent(int client_id) { |
| 1009 | // Ignore if client is already started. |
| 1010 | if (alsa_seq_state_.ClientStarted(client_id)) |
| 1011 | return; |
| 1012 | |
| 1013 | snd_seq_client_info_t* client_info; |
| 1014 | snd_seq_client_info_alloca(&client_info); |
agoode | 5a1aa11 | 2015-06-21 20:51:00 -0700 | [diff] [blame] | 1015 | int err = |
| 1016 | snd_seq_get_any_client_info(in_client_.get(), client_id, client_info); |
agoode | 99d6329 | 2015-04-13 08:39:25 -0700 | [diff] [blame] | 1017 | if (err != 0) |
| 1018 | return; |
| 1019 | |
| 1020 | // Skip our own clients. |
| 1021 | if ((client_id == in_client_id_) || (client_id == out_client_id_)) |
| 1022 | return; |
| 1023 | |
| 1024 | // Update our view of ALSA seq state. |
| 1025 | alsa_seq_state_.ClientStart(client_id, |
| 1026 | snd_seq_client_info_get_name(client_info), |
| 1027 | snd_seq_client_info_get_type(client_info)); |
| 1028 | |
| 1029 | // Generate Web MIDI events. |
| 1030 | UpdatePortStateAndGenerateEvents(); |
| 1031 | } |
| 1032 | |
| 1033 | void MidiManagerAlsa::ProcessPortStartEvent(const snd_seq_addr_t& addr) { |
| 1034 | snd_seq_port_info_t* port_info; |
| 1035 | snd_seq_port_info_alloca(&port_info); |
agoode | 5a1aa11 | 2015-06-21 20:51:00 -0700 | [diff] [blame] | 1036 | int err = snd_seq_get_any_port_info(in_client_.get(), addr.client, addr.port, |
| 1037 | port_info); |
agoode | 99d6329 | 2015-04-13 08:39:25 -0700 | [diff] [blame] | 1038 | if (err != 0) |
| 1039 | return; |
| 1040 | |
| 1041 | unsigned int caps = snd_seq_port_info_get_capability(port_info); |
| 1042 | bool input = (caps & kRequiredInputPortCaps) == kRequiredInputPortCaps; |
| 1043 | bool output = (caps & kRequiredOutputPortCaps) == kRequiredOutputPortCaps; |
| 1044 | AlsaSeqState::PortDirection direction; |
| 1045 | if (input && output) |
| 1046 | direction = AlsaSeqState::PortDirection::kDuplex; |
| 1047 | else if (input) |
| 1048 | direction = AlsaSeqState::PortDirection::kInput; |
| 1049 | else if (output) |
| 1050 | direction = AlsaSeqState::PortDirection::kOutput; |
| 1051 | else |
| 1052 | return; |
| 1053 | |
| 1054 | // Update our view of ALSA seq state. |
| 1055 | alsa_seq_state_.PortStart( |
| 1056 | addr.client, addr.port, snd_seq_port_info_get_name(port_info), direction, |
| 1057 | snd_seq_port_info_get_type(port_info) & SND_SEQ_PORT_TYPE_MIDI_GENERIC); |
| 1058 | // Generate Web MIDI events. |
| 1059 | UpdatePortStateAndGenerateEvents(); |
| 1060 | } |
| 1061 | |
| 1062 | void MidiManagerAlsa::ProcessClientExitEvent(const snd_seq_addr_t& addr) { |
| 1063 | // Update our view of ALSA seq state. |
| 1064 | alsa_seq_state_.ClientExit(addr.client); |
| 1065 | // Generate Web MIDI events. |
| 1066 | UpdatePortStateAndGenerateEvents(); |
| 1067 | } |
| 1068 | |
| 1069 | void MidiManagerAlsa::ProcessPortExitEvent(const snd_seq_addr_t& addr) { |
| 1070 | // Update our view of ALSA seq state. |
| 1071 | alsa_seq_state_.PortExit(addr.client, addr.port); |
| 1072 | // Generate Web MIDI events. |
| 1073 | UpdatePortStateAndGenerateEvents(); |
| 1074 | } |
| 1075 | |
agoode | 975043d | 2015-05-11 00:46:17 -0700 | [diff] [blame] | 1076 | void MidiManagerAlsa::ProcessUdevEvent(udev_device* dev) { |
| 1077 | // Only card devices have this property set, and only when they are |
| 1078 | // fully initialized. |
| 1079 | if (!device::udev_device_get_property_value(dev, |
| 1080 | kUdevPropertySoundInitialized)) |
| 1081 | return; |
| 1082 | |
| 1083 | // Get the action. If no action, then we are doing first time enumeration |
| 1084 | // and the device is treated as new. |
| 1085 | const char* action = device::udev_device_get_action(dev); |
| 1086 | if (!action) |
| 1087 | action = kUdevActionChange; |
| 1088 | |
| 1089 | if (strcmp(action, kUdevActionChange) == 0) { |
agoode | b09423b | 2015-05-11 11:39:57 -0700 | [diff] [blame] | 1090 | AddCard(dev); |
| 1091 | // Generate Web MIDI events. |
| 1092 | UpdatePortStateAndGenerateEvents(); |
agoode | 975043d | 2015-05-11 00:46:17 -0700 | [diff] [blame] | 1093 | } else if (strcmp(action, kUdevActionRemove) == 0) { |
agoode | b09423b | 2015-05-11 11:39:57 -0700 | [diff] [blame] | 1094 | RemoveCard(GetCardNumber(dev)); |
| 1095 | // Generate Web MIDI events. |
| 1096 | UpdatePortStateAndGenerateEvents(); |
agoode | 975043d | 2015-05-11 00:46:17 -0700 | [diff] [blame] | 1097 | } |
| 1098 | } |
| 1099 | |
agoode | b09423b | 2015-05-11 11:39:57 -0700 | [diff] [blame] | 1100 | void MidiManagerAlsa::AddCard(udev_device* dev) { |
| 1101 | int number = GetCardNumber(dev); |
| 1102 | if (number == -1) |
| 1103 | return; |
| 1104 | |
| 1105 | RemoveCard(number); |
| 1106 | |
| 1107 | snd_ctl_card_info_t* card; |
| 1108 | snd_hwdep_info_t* hwdep; |
| 1109 | snd_ctl_card_info_alloca(&card); |
| 1110 | snd_hwdep_info_alloca(&hwdep); |
| 1111 | const std::string id = base::StringPrintf("hw:CARD=%i", number); |
| 1112 | snd_ctl_t* handle; |
| 1113 | int err = snd_ctl_open(&handle, id.c_str(), 0); |
| 1114 | if (err != 0) { |
| 1115 | VLOG(1) << "snd_ctl_open fails: " << snd_strerror(err); |
| 1116 | return; |
| 1117 | } |
| 1118 | err = snd_ctl_card_info(handle, card); |
| 1119 | if (err != 0) { |
| 1120 | VLOG(1) << "snd_ctl_card_info fails: " << snd_strerror(err); |
| 1121 | snd_ctl_close(handle); |
| 1122 | return; |
| 1123 | } |
| 1124 | std::string name = snd_ctl_card_info_get_name(card); |
| 1125 | std::string longname = snd_ctl_card_info_get_longname(card); |
| 1126 | std::string driver = snd_ctl_card_info_get_driver(card); |
| 1127 | |
| 1128 | // Count rawmidi devices (not subdevices). |
| 1129 | int midi_count = 0; |
| 1130 | for (int device = -1; |
| 1131 | !snd_ctl_rawmidi_next_device(handle, &device) && device >= 0;) |
| 1132 | ++midi_count; |
| 1133 | |
| 1134 | // Count any hwdep synths that become MIDI devices outside of rawmidi. |
| 1135 | // |
| 1136 | // Explanation: |
| 1137 | // Any kernel driver can create an ALSA client (visible to us). |
| 1138 | // With modern hardware, only rawmidi devices do this. Kernel |
| 1139 | // drivers create rawmidi devices and the rawmidi subsystem makes |
| 1140 | // the seq clients. But the OPL3 driver is special, it does not |
| 1141 | // make a rawmidi device but a seq client directly. (This is the |
| 1142 | // only one to worry about in the kernel code, as of 2015-03-23.) |
| 1143 | // |
| 1144 | // OPL3 is very old (but still possible to get in new |
| 1145 | // hardware). It is unlikely that new drivers would not use |
| 1146 | // rawmidi and defeat our heuristic. |
| 1147 | // |
| 1148 | // Longer term, support should be added in the kernel to expose a |
| 1149 | // direct link from card->client (or client->card) so that all |
| 1150 | // these heuristics will be obsolete. Once that is there, we can |
| 1151 | // assume our old heuristics will work on old kernels and the new |
| 1152 | // robust code will be used on new. Then we will not need to worry |
| 1153 | // about changes to kernel internals breaking our code. |
| 1154 | // See the TODO above at kMinimumClientIdForCards. |
| 1155 | for (int device = -1; |
| 1156 | !snd_ctl_hwdep_next_device(handle, &device) && device >= 0;) { |
| 1157 | err = snd_ctl_hwdep_info(handle, hwdep); |
| 1158 | if (err != 0) { |
| 1159 | VLOG(1) << "snd_ctl_hwdep_info fails: " << snd_strerror(err); |
| 1160 | continue; |
| 1161 | } |
| 1162 | snd_hwdep_iface_t iface = snd_hwdep_info_get_iface(hwdep); |
| 1163 | if (iface == SND_HWDEP_IFACE_OPL2 || iface == SND_HWDEP_IFACE_OPL3 || |
| 1164 | iface == SND_HWDEP_IFACE_OPL4) |
| 1165 | ++midi_count; |
| 1166 | } |
| 1167 | snd_ctl_close(handle); |
| 1168 | |
mgiuca | d9af845 | 2015-06-25 02:11:57 -0700 | [diff] [blame] | 1169 | if (midi_count > 0) { |
danakj | 75afea0 | 2016-04-25 20:36:04 -0700 | [diff] [blame] | 1170 | std::unique_ptr<AlsaCard> card( |
mgiuca | d9af845 | 2015-06-25 02:11:57 -0700 | [diff] [blame] | 1171 | new AlsaCard(dev, name, longname, driver, midi_count)); |
limasdf | e59d039 | 2015-11-19 20:28:57 -0800 | [diff] [blame] | 1172 | alsa_cards_.insert(std::make_pair(number, std::move(card))); |
mgiuca | d9af845 | 2015-06-25 02:11:57 -0700 | [diff] [blame] | 1173 | alsa_card_midi_count_ += midi_count; |
| 1174 | } |
agoode | b09423b | 2015-05-11 11:39:57 -0700 | [diff] [blame] | 1175 | } |
| 1176 | |
| 1177 | void MidiManagerAlsa::RemoveCard(int number) { |
| 1178 | auto it = alsa_cards_.find(number); |
| 1179 | if (it == alsa_cards_.end()) |
| 1180 | return; |
| 1181 | |
| 1182 | alsa_card_midi_count_ -= it->second->midi_device_count(); |
agoode | b09423b | 2015-05-11 11:39:57 -0700 | [diff] [blame] | 1183 | alsa_cards_.erase(it); |
| 1184 | } |
| 1185 | |
agoode | 99d6329 | 2015-04-13 08:39:25 -0700 | [diff] [blame] | 1186 | void MidiManagerAlsa::UpdatePortStateAndGenerateEvents() { |
agoode | b09423b | 2015-05-11 11:39:57 -0700 | [diff] [blame] | 1187 | // Verify that our information from ALSA and udev are in sync. If |
| 1188 | // not, we cannot generate events right now. |
| 1189 | if (alsa_card_midi_count_ != alsa_seq_state_.card_client_count()) |
| 1190 | return; |
| 1191 | |
agoode | 99d6329 | 2015-04-13 08:39:25 -0700 | [diff] [blame] | 1192 | // Generate new port state. |
agoode | b09423b | 2015-05-11 11:39:57 -0700 | [diff] [blame] | 1193 | auto new_port_state = alsa_seq_state_.ToMidiPortState(alsa_cards_); |
agoode | 99d6329 | 2015-04-13 08:39:25 -0700 | [diff] [blame] | 1194 | |
| 1195 | // Disconnect any connected old ports that are now missing. |
agoode | 5ebc493 | 2015-12-01 08:25:12 -0800 | [diff] [blame] | 1196 | for (auto& old_port : port_state_) { |
agoode | 99d6329 | 2015-04-13 08:39:25 -0700 | [diff] [blame] | 1197 | if (old_port->connected() && |
| 1198 | (new_port_state->FindConnected(*old_port) == new_port_state->end())) { |
| 1199 | old_port->set_connected(false); |
Avi Drissman | 3528fd0 | 2015-12-18 20:11:31 -0500 | [diff] [blame] | 1200 | uint32_t web_port_index = old_port->web_port_index(); |
agoode | 99d6329 | 2015-04-13 08:39:25 -0700 | [diff] [blame] | 1201 | switch (old_port->type()) { |
| 1202 | case MidiPort::Type::kInput: |
| 1203 | source_map_.erase( |
| 1204 | AddrToInt(old_port->client_id(), old_port->port_id())); |
| 1205 | SetInputPortState(web_port_index, MIDI_PORT_DISCONNECTED); |
| 1206 | break; |
| 1207 | case MidiPort::Type::kOutput: |
| 1208 | DeleteAlsaOutputPort(web_port_index); |
| 1209 | SetOutputPortState(web_port_index, MIDI_PORT_DISCONNECTED); |
| 1210 | break; |
| 1211 | } |
| 1212 | } |
| 1213 | } |
| 1214 | |
| 1215 | // Reconnect or add new ports. |
| 1216 | auto it = new_port_state->begin(); |
| 1217 | while (it != new_port_state->end()) { |
agoode | 5ebc493 | 2015-12-01 08:25:12 -0800 | [diff] [blame] | 1218 | auto& new_port = *it; |
agoode | 99d6329 | 2015-04-13 08:39:25 -0700 | [diff] [blame] | 1219 | auto old_port = port_state_.Find(*new_port); |
| 1220 | if (old_port == port_state_.end()) { |
| 1221 | // Add new port. |
agoode | 5ebc493 | 2015-12-01 08:25:12 -0800 | [diff] [blame] | 1222 | const auto& opaque_key = new_port->OpaqueKey(); |
| 1223 | const auto& manufacturer = new_port->manufacturer(); |
| 1224 | const auto& port_name = new_port->port_name(); |
| 1225 | const auto& version = new_port->version(); |
| 1226 | const auto& type = new_port->type(); |
| 1227 | const auto& client_id = new_port->client_id(); |
| 1228 | const auto& port_id = new_port->port_id(); |
| 1229 | |
dcheng | c2aeece | 2015-12-27 00:54:00 -0800 | [diff] [blame] | 1230 | uint32_t web_port_index = port_state_.push_back(std::move(new_port)); |
agoode | 5ebc493 | 2015-12-01 08:25:12 -0800 | [diff] [blame] | 1231 | it = new_port_state->erase(it); |
| 1232 | |
| 1233 | MidiPortInfo info(opaque_key, manufacturer, port_name, version, |
agoode | 99d6329 | 2015-04-13 08:39:25 -0700 | [diff] [blame] | 1234 | MIDI_PORT_OPENED); |
agoode | 5ebc493 | 2015-12-01 08:25:12 -0800 | [diff] [blame] | 1235 | switch (type) { |
agoode | 99d6329 | 2015-04-13 08:39:25 -0700 | [diff] [blame] | 1236 | case MidiPort::Type::kInput: |
agoode | 5ebc493 | 2015-12-01 08:25:12 -0800 | [diff] [blame] | 1237 | if (Subscribe(web_port_index, client_id, port_id)) |
agoode | 99d6329 | 2015-04-13 08:39:25 -0700 | [diff] [blame] | 1238 | AddInputPort(info); |
| 1239 | break; |
agoode | 99d6329 | 2015-04-13 08:39:25 -0700 | [diff] [blame] | 1240 | case MidiPort::Type::kOutput: |
agoode | 5ebc493 | 2015-12-01 08:25:12 -0800 | [diff] [blame] | 1241 | if (CreateAlsaOutputPort(web_port_index, client_id, port_id)) |
agoode | 99d6329 | 2015-04-13 08:39:25 -0700 | [diff] [blame] | 1242 | AddOutputPort(info); |
| 1243 | break; |
| 1244 | } |
agoode | 99d6329 | 2015-04-13 08:39:25 -0700 | [diff] [blame] | 1245 | } else if (!(*old_port)->connected()) { |
| 1246 | // Reconnect. |
Avi Drissman | 3528fd0 | 2015-12-18 20:11:31 -0500 | [diff] [blame] | 1247 | uint32_t web_port_index = (*old_port)->web_port_index(); |
agoode | 99d6329 | 2015-04-13 08:39:25 -0700 | [diff] [blame] | 1248 | (*old_port)->Update(new_port->path(), new_port->client_id(), |
| 1249 | new_port->port_id(), new_port->client_name(), |
| 1250 | new_port->port_name(), new_port->manufacturer(), |
| 1251 | new_port->version()); |
| 1252 | switch ((*old_port)->type()) { |
| 1253 | case MidiPort::Type::kInput: |
| 1254 | if (Subscribe(web_port_index, (*old_port)->client_id(), |
| 1255 | (*old_port)->port_id())) |
| 1256 | SetInputPortState(web_port_index, MIDI_PORT_OPENED); |
| 1257 | break; |
agoode | 99d6329 | 2015-04-13 08:39:25 -0700 | [diff] [blame] | 1258 | case MidiPort::Type::kOutput: |
| 1259 | if (CreateAlsaOutputPort(web_port_index, (*old_port)->client_id(), |
| 1260 | (*old_port)->port_id())) |
| 1261 | SetOutputPortState(web_port_index, MIDI_PORT_OPENED); |
| 1262 | break; |
| 1263 | } |
| 1264 | (*old_port)->set_connected(true); |
| 1265 | ++it; |
| 1266 | } else { |
| 1267 | ++it; |
| 1268 | } |
| 1269 | } |
| 1270 | } |
| 1271 | |
agoode | 975043d | 2015-05-11 00:46:17 -0700 | [diff] [blame] | 1272 | // TODO(agoode): return false on failure. |
agoode | 99d6329 | 2015-04-13 08:39:25 -0700 | [diff] [blame] | 1273 | void MidiManagerAlsa::EnumerateAlsaPorts() { |
| 1274 | snd_seq_client_info_t* client_info; |
| 1275 | snd_seq_client_info_alloca(&client_info); |
| 1276 | snd_seq_port_info_t* port_info; |
| 1277 | snd_seq_port_info_alloca(&port_info); |
| 1278 | |
| 1279 | // Enumerate clients. |
| 1280 | snd_seq_client_info_set_client(client_info, -1); |
agoode | 5a1aa11 | 2015-06-21 20:51:00 -0700 | [diff] [blame] | 1281 | while (!snd_seq_query_next_client(in_client_.get(), client_info)) { |
agoode | 99d6329 | 2015-04-13 08:39:25 -0700 | [diff] [blame] | 1282 | int client_id = snd_seq_client_info_get_client(client_info); |
| 1283 | ProcessClientStartEvent(client_id); |
| 1284 | |
| 1285 | // Enumerate ports. |
| 1286 | snd_seq_port_info_set_client(port_info, client_id); |
| 1287 | snd_seq_port_info_set_port(port_info, -1); |
agoode | 5a1aa11 | 2015-06-21 20:51:00 -0700 | [diff] [blame] | 1288 | while (!snd_seq_query_next_port(in_client_.get(), port_info)) { |
agoode | 99d6329 | 2015-04-13 08:39:25 -0700 | [diff] [blame] | 1289 | const snd_seq_addr_t* addr = snd_seq_port_info_get_addr(port_info); |
| 1290 | ProcessPortStartEvent(*addr); |
| 1291 | } |
| 1292 | } |
| 1293 | } |
| 1294 | |
agoode | 975043d | 2015-05-11 00:46:17 -0700 | [diff] [blame] | 1295 | bool MidiManagerAlsa::EnumerateUdevCards() { |
| 1296 | int err; |
| 1297 | |
| 1298 | device::ScopedUdevEnumeratePtr enumerate( |
| 1299 | device::udev_enumerate_new(udev_.get())); |
| 1300 | if (!enumerate.get()) { |
| 1301 | VLOG(1) << "udev_enumerate_new fails"; |
| 1302 | return false; |
| 1303 | } |
| 1304 | |
| 1305 | err = device::udev_enumerate_add_match_subsystem(enumerate.get(), |
| 1306 | kUdevSubsystemSound); |
| 1307 | if (err) { |
| 1308 | VLOG(1) << "udev_enumerate_add_match_subsystem fails: " |
brettw | f7f870f | 2015-06-09 11:05:24 -0700 | [diff] [blame] | 1309 | << base::safe_strerror(-err); |
agoode | 975043d | 2015-05-11 00:46:17 -0700 | [diff] [blame] | 1310 | return false; |
| 1311 | } |
| 1312 | |
| 1313 | err = device::udev_enumerate_scan_devices(enumerate.get()); |
| 1314 | if (err) { |
brettw | f7f870f | 2015-06-09 11:05:24 -0700 | [diff] [blame] | 1315 | VLOG(1) << "udev_enumerate_scan_devices fails: " |
| 1316 | << base::safe_strerror(-err); |
agoode | 975043d | 2015-05-11 00:46:17 -0700 | [diff] [blame] | 1317 | return false; |
| 1318 | } |
| 1319 | |
| 1320 | udev_list_entry* list_entry; |
| 1321 | auto* devices = device::udev_enumerate_get_list_entry(enumerate.get()); |
| 1322 | udev_list_entry_foreach(list_entry, devices) { |
| 1323 | const char* path = device::udev_list_entry_get_name(list_entry); |
| 1324 | device::ScopedUdevDevicePtr dev( |
| 1325 | device::udev_device_new_from_syspath(udev_.get(), path)); |
| 1326 | if (dev.get()) |
| 1327 | ProcessUdevEvent(dev.get()); |
| 1328 | } |
| 1329 | |
| 1330 | return true; |
| 1331 | } |
| 1332 | |
Avi Drissman | 3528fd0 | 2015-12-18 20:11:31 -0500 | [diff] [blame] | 1333 | bool MidiManagerAlsa::CreateAlsaOutputPort(uint32_t port_index, |
agoode | 99d6329 | 2015-04-13 08:39:25 -0700 | [diff] [blame] | 1334 | int client_id, |
| 1335 | int port_id) { |
| 1336 | // Create the port. |
| 1337 | int out_port = snd_seq_create_simple_port( |
agoode | 5a1aa11 | 2015-06-21 20:51:00 -0700 | [diff] [blame] | 1338 | out_client_.get(), NULL, kCreateOutputPortCaps, kCreatePortType); |
agoode | 99d6329 | 2015-04-13 08:39:25 -0700 | [diff] [blame] | 1339 | if (out_port < 0) { |
| 1340 | VLOG(1) << "snd_seq_create_simple_port fails: " << snd_strerror(out_port); |
| 1341 | return false; |
| 1342 | } |
| 1343 | // Activate port subscription. |
| 1344 | snd_seq_port_subscribe_t* subs; |
| 1345 | snd_seq_port_subscribe_alloca(&subs); |
| 1346 | snd_seq_addr_t sender; |
| 1347 | sender.client = out_client_id_; |
| 1348 | sender.port = out_port; |
| 1349 | snd_seq_port_subscribe_set_sender(subs, &sender); |
| 1350 | snd_seq_addr_t dest; |
| 1351 | dest.client = client_id; |
| 1352 | dest.port = port_id; |
| 1353 | snd_seq_port_subscribe_set_dest(subs, &dest); |
agoode | 5a1aa11 | 2015-06-21 20:51:00 -0700 | [diff] [blame] | 1354 | int err = snd_seq_subscribe_port(out_client_.get(), subs); |
agoode | 99d6329 | 2015-04-13 08:39:25 -0700 | [diff] [blame] | 1355 | if (err != 0) { |
| 1356 | VLOG(1) << "snd_seq_subscribe_port fails: " << snd_strerror(err); |
agoode | 5a1aa11 | 2015-06-21 20:51:00 -0700 | [diff] [blame] | 1357 | snd_seq_delete_simple_port(out_client_.get(), out_port); |
agoode | 99d6329 | 2015-04-13 08:39:25 -0700 | [diff] [blame] | 1358 | return false; |
| 1359 | } |
| 1360 | |
| 1361 | // Update our map. |
| 1362 | base::AutoLock lock(out_ports_lock_); |
| 1363 | out_ports_[port_index] = out_port; |
| 1364 | return true; |
| 1365 | } |
| 1366 | |
Avi Drissman | 3528fd0 | 2015-12-18 20:11:31 -0500 | [diff] [blame] | 1367 | void MidiManagerAlsa::DeleteAlsaOutputPort(uint32_t port_index) { |
agoode | 99d6329 | 2015-04-13 08:39:25 -0700 | [diff] [blame] | 1368 | base::AutoLock lock(out_ports_lock_); |
| 1369 | auto it = out_ports_.find(port_index); |
| 1370 | if (it == out_ports_.end()) |
| 1371 | return; |
| 1372 | |
| 1373 | int alsa_port = it->second; |
agoode | 5a1aa11 | 2015-06-21 20:51:00 -0700 | [diff] [blame] | 1374 | snd_seq_delete_simple_port(out_client_.get(), alsa_port); |
agoode | 99d6329 | 2015-04-13 08:39:25 -0700 | [diff] [blame] | 1375 | out_ports_.erase(it); |
| 1376 | } |
| 1377 | |
Avi Drissman | 3528fd0 | 2015-12-18 20:11:31 -0500 | [diff] [blame] | 1378 | bool MidiManagerAlsa::Subscribe(uint32_t port_index, |
| 1379 | int client_id, |
| 1380 | int port_id) { |
agoode | 99d6329 | 2015-04-13 08:39:25 -0700 | [diff] [blame] | 1381 | // Activate port subscription. |
| 1382 | snd_seq_port_subscribe_t* subs; |
| 1383 | snd_seq_port_subscribe_alloca(&subs); |
| 1384 | snd_seq_addr_t sender; |
| 1385 | sender.client = client_id; |
| 1386 | sender.port = port_id; |
| 1387 | snd_seq_port_subscribe_set_sender(subs, &sender); |
| 1388 | snd_seq_addr_t dest; |
| 1389 | dest.client = in_client_id_; |
| 1390 | dest.port = in_port_id_; |
| 1391 | snd_seq_port_subscribe_set_dest(subs, &dest); |
agoode | 5a1aa11 | 2015-06-21 20:51:00 -0700 | [diff] [blame] | 1392 | int err = snd_seq_subscribe_port(in_client_.get(), subs); |
agoode | 99d6329 | 2015-04-13 08:39:25 -0700 | [diff] [blame] | 1393 | if (err != 0) { |
| 1394 | VLOG(1) << "snd_seq_subscribe_port fails: " << snd_strerror(err); |
| 1395 | return false; |
| 1396 | } |
| 1397 | |
| 1398 | // Update our map. |
| 1399 | source_map_[AddrToInt(client_id, port_id)] = port_index; |
| 1400 | return true; |
| 1401 | } |
| 1402 | |
toyoshim@chromium.org | c82e66e | 2014-02-04 07:05:47 +0000 | [diff] [blame] | 1403 | MidiManager* MidiManager::Create() { |
| 1404 | return new MidiManagerAlsa(); |
toyoshim@chromium.org | a97eebf | 2014-01-03 07:52:39 +0000 | [diff] [blame] | 1405 | } |
| 1406 | |
toyoshim | e147c5e | 2015-05-07 21:58:31 -0700 | [diff] [blame] | 1407 | } // namespace midi |