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