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