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