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