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