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