blob: 85dc3142277652d0197222a4a83528b2b742051f [file] [log] [blame]
toyoshim@chromium.orga97eebf2014-01-03 07:52:39 +00001// 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.org9f2a6f02014-01-03 21:25:00 +00005#include "media/midi/midi_manager_alsa.h"
toyoshim@chromium.orga97eebf2014-01-03 07:52:39 +00006
mostynbbd693172015-11-17 19:06:54 -08007#include <errno.h>
agoode975043d2015-05-11 00:46:17 -07008#include <poll.h>
avi793390d2015-12-22 22:22:36 -08009#include <stddef.h>
toyoshim@chromium.org4a8657c2014-02-06 11:23:09 +000010#include <stdlib.h>
danakj75afea02016-04-25 20:36:04 -070011
yhirano@chromium.orgcfa642c2014-05-01 08:54:41 +000012#include <algorithm>
13#include <string>
limasdfe59d0392015-11-19 20:28:57 -080014#include <utility>
toyoshim@chromium.orga97eebf2014-01-03 07:52:39 +000015
16#include "base/bind.h"
agoodef212b2a2015-03-19 12:53:23 -070017#include "base/json/json_string_value_serializer.h"
toyoshim@chromium.orga97eebf2014-01-03 07:52:39 +000018#include "base/logging.h"
avi793390d2015-12-22 22:22:36 -080019#include "base/macros.h"
toyoshim@chromium.org4a8657c2014-02-06 11:23:09 +000020#include "base/posix/eintr_wrapper.h"
brettwf7f870f2015-06-09 11:05:24 -070021#include "base/posix/safe_strerror.h"
fdoray8baaff02016-08-25 08:36:37 -070022#include "base/single_thread_task_runner.h"
agoodef212b2a2015-03-19 12:53:23 -070023#include "base/strings/string_number_conversions.h"
toyoshim@chromium.orga97eebf2014-01-03 07:52:39 +000024#include "base/strings/stringprintf.h"
toyoshim@chromium.org4a8657c2014-02-06 11:23:09 +000025#include "base/time/time.h"
Takashi Toyoshima70e44ee2017-07-19 08:38:20 +000026#include "build/build_config.h"
agoodef212b2a2015-03-19 12:53:23 -070027#include "crypto/sha2.h"
toyoshimf4d61522017-02-10 02:03:32 -080028#include "media/midi/midi_service.h"
Adithya Srinivasan33252732018-10-17 15:59:40 +000029#include "media/midi/midi_service.mojom.h"
toyoshimefcee5e2017-06-14 07:14:39 -070030#include "media/midi/task_service.h"
toyoshim@chromium.orga97eebf2014-01-03 07:52:39 +000031
toyoshime147c5e2015-05-07 21:58:31 -070032namespace midi {
toyoshim@chromium.orga97eebf2014-01-03 07:52:39 +000033
toyoshim@chromium.org4a8657c2014-02-06 11:23:09 +000034namespace {
35
toyoshimec2570a2016-10-21 02:15:27 -070036using mojom::PortState;
37using mojom::Result;
toyoshim2f3a48f2016-10-17 01:54:13 -070038
toyoshimefcee5e2017-06-14 07:14:39 -070039enum {
40 kDefaultRunnerNotUsedOnAlsa = TaskService::kDefaultRunnerId,
41 kEventTaskRunner,
42 kSendTaskRunner
43};
toyoshimf4d61522017-02-10 02:03:32 -080044
agoode@chromium.org25227512014-06-08 05:12:05 +000045// Per-output buffer. This can be smaller, but then large sysex messages
46// will be (harmlessly) split across multiple seq events. This should
47// not have any real practical effect, except perhaps to slightly reorder
48// realtime messages with respect to sysex.
toyoshimefcee5e2017-06-14 07:14:39 -070049constexpr size_t kSendBufferSize = 256;
agoode@chromium.org25227512014-06-08 05:12:05 +000050
agoodeb09423b2015-05-11 11:39:57 -070051// Minimum client id for which we will have ALSA card devices for. When we
52// are searching for card devices (used to get the path, id, and manufacturer),
53// we don't want to get confused by kernel clients that do not have a card.
54// See seq_clientmgr.c in the ALSA code for this.
55// TODO(agoode): Add proper client -> card export from the kernel to avoid
56// hardcoding.
toyoshimefcee5e2017-06-14 07:14:39 -070057constexpr int kMinimumClientIdForCards = 16;
agoodeb09423b2015-05-11 11:39:57 -070058
agoodef212b2a2015-03-19 12:53:23 -070059// ALSA constants.
60const char kAlsaHw[] = "hw";
61
agoode975043d2015-05-11 00:46:17 -070062// udev constants.
63const char kUdev[] = "udev";
64const char kUdevSubsystemSound[] = "sound";
65const char kUdevPropertySoundInitialized[] = "SOUND_INITIALIZED";
66const char kUdevActionChange[] = "change";
67const char kUdevActionRemove[] = "remove";
68
agoodeb09423b2015-05-11 11:39:57 -070069const char kUdevIdVendor[] = "ID_VENDOR";
70const char kUdevIdVendorEnc[] = "ID_VENDOR_ENC";
71const char kUdevIdVendorFromDatabase[] = "ID_VENDOR_FROM_DATABASE";
72const char kUdevIdVendorId[] = "ID_VENDOR_ID";
73const char kUdevIdModelId[] = "ID_MODEL_ID";
74const char kUdevIdBus[] = "ID_BUS";
75const char kUdevIdPath[] = "ID_PATH";
76const char kUdevIdUsbInterfaceNum[] = "ID_USB_INTERFACE_NUM";
77const char kUdevIdSerialShort[] = "ID_SERIAL_SHORT";
78
79const char kSysattrVendorName[] = "vendor_name";
80const char kSysattrVendor[] = "vendor";
81const char kSysattrModel[] = "model";
82const char kSysattrGuid[] = "guid";
83
84const char kCardSyspath[] = "/card";
85
agoode@chromium.org25227512014-06-08 05:12:05 +000086// Constants for the capabilities we search for in inputs and outputs.
87// See http://www.alsa-project.org/alsa-doc/alsa-lib/seq.html.
toyoshimefcee5e2017-06-14 07:14:39 -070088constexpr unsigned int kRequiredInputPortCaps =
agoode@chromium.org25227512014-06-08 05:12:05 +000089 SND_SEQ_PORT_CAP_READ | SND_SEQ_PORT_CAP_SUBS_READ;
toyoshimefcee5e2017-06-14 07:14:39 -070090constexpr unsigned int kRequiredOutputPortCaps =
agoode@chromium.org25227512014-06-08 05:12:05 +000091 SND_SEQ_PORT_CAP_WRITE | SND_SEQ_PORT_CAP_SUBS_WRITE;
92
toyoshimefcee5e2017-06-14 07:14:39 -070093constexpr unsigned int kCreateOutputPortCaps =
agoode99d63292015-04-13 08:39:25 -070094 SND_SEQ_PORT_CAP_READ | SND_SEQ_PORT_CAP_NO_EXPORT;
toyoshimefcee5e2017-06-14 07:14:39 -070095constexpr unsigned int kCreateInputPortCaps =
agoode99d63292015-04-13 08:39:25 -070096 SND_SEQ_PORT_CAP_WRITE | SND_SEQ_PORT_CAP_NO_EXPORT;
toyoshimefcee5e2017-06-14 07:14:39 -070097constexpr unsigned int kCreatePortType =
agoode99d63292015-04-13 08:39:25 -070098 SND_SEQ_PORT_TYPE_MIDI_GENERIC | SND_SEQ_PORT_TYPE_APPLICATION;
agoode@chromium.org25227512014-06-08 05:12:05 +000099
agoode99d63292015-04-13 08:39:25 -0700100int AddrToInt(int client, int port) {
101 return (client << 8) | port;
agoodef212b2a2015-03-19 12:53:23 -0700102}
agoodef212b2a2015-03-19 12:53:23 -0700103
agoodeb09423b2015-05-11 11:39:57 -0700104// Returns true if this client has an ALSA card associated with it.
105bool IsCardClient(snd_seq_client_type_t type, int client_id) {
106 return (type == SND_SEQ_KERNEL_CLIENT) &&
107 (client_id >= kMinimumClientIdForCards);
108}
109
110// TODO(agoode): Move this to device/udev_linux.
111const std::string UdevDeviceGetPropertyOrSysattr(
112 struct udev_device* udev_device,
113 const char* property_key,
114 const char* sysattr_key) {
115 // First try the property.
116 std::string value =
117 device::UdevDeviceGetPropertyValue(udev_device, property_key);
118
119 // If no property, look for sysattrs and walk up the parent devices too.
120 while (value.empty() && udev_device) {
121 value = device::UdevDeviceGetSysattrValue(udev_device, sysattr_key);
122 udev_device = device::udev_device_get_parent(udev_device);
123 }
124 return value;
125}
126
127int GetCardNumber(udev_device* dev) {
128 const char* syspath = device::udev_device_get_syspath(dev);
129 if (!syspath)
130 return -1;
131
132 std::string syspath_str(syspath);
133 size_t i = syspath_str.rfind(kCardSyspath);
134 if (i == std::string::npos)
135 return -1;
136
137 int number;
138 if (!base::StringToInt(syspath_str.substr(i + strlen(kCardSyspath)), &number))
139 return -1;
140 return number;
141}
142
agooded87fc0f2015-05-21 08:29:31 -0700143std::string GetVendor(udev_device* dev) {
144 // Try to get the vendor string. Sometimes it is encoded.
145 std::string vendor = device::UdevDecodeString(
146 device::UdevDeviceGetPropertyValue(dev, kUdevIdVendorEnc));
147 // Sometimes it is not encoded.
148 if (vendor.empty())
149 vendor =
150 UdevDeviceGetPropertyOrSysattr(dev, kUdevIdVendor, kSysattrVendorName);
151 return vendor;
152}
153
agoode8caab0b2015-03-23 18:48:02 -0700154void SetStringIfNonEmpty(base::DictionaryValue* value,
155 const std::string& path,
156 const std::string& in_value) {
157 if (!in_value.empty())
158 value->SetString(path, in_value);
159}
160
toyoshim@chromium.org4a8657c2014-02-06 11:23:09 +0000161} // namespace
162
toyoshimf4d61522017-02-10 02:03:32 -0800163MidiManagerAlsa::MidiManagerAlsa(MidiService* service) : MidiManager(service) {}
agoodeb2ead822016-03-11 12:14:35 -0800164
165MidiManagerAlsa::~MidiManagerAlsa() {
Takashi Toyoshimae8240ab2018-10-03 09:30:11 +0000166 {
167 base::AutoLock lock(out_client_lock_);
168 // Close the out client. This will trigger the event thread to stop,
169 // because of SND_SEQ_EVENT_CLIENT_EXIT.
170 out_client_.reset();
171 }
Takashi Toyoshima9687b7c2017-07-19 09:54:14 +0000172
Takashi Toyoshimae8240ab2018-10-03 09:30:11 +0000173 // Ensure that no task is running any more.
174 bool result = service()->task_service()->UnbindInstance();
175 CHECK(result);
toyoshim@chromium.orga97eebf2014-01-03 07:52:39 +0000176}
177
toyoshim@chromium.org51c7f532014-05-01 17:17:32 +0000178void MidiManagerAlsa::StartInitialization() {
toyoshimefcee5e2017-06-14 07:14:39 -0700179 if (!service()->task_service()->BindInstance()) {
180 NOTREACHED();
181 return CompleteInitialization(Result::INITIALIZATION_ERROR);
toyoshimf4d61522017-02-10 02:03:32 -0800182 }
183
Takashi Toyoshimae8240ab2018-10-03 09:30:11 +0000184 // Create client handles and name the clients.
185 int err;
186 {
187 snd_seq_t* in_seq = nullptr;
188 err = snd_seq_open(&in_seq, kAlsaHw, SND_SEQ_OPEN_INPUT, SND_SEQ_NONBLOCK);
189 if (err != 0) {
190 VLOG(1) << "snd_seq_open fails: " << snd_strerror(err);
191 return CompleteInitialization(Result::INITIALIZATION_ERROR);
192 }
193 in_client_ = ScopedSndSeqPtr(in_seq);
194 in_client_id_ = snd_seq_client_id(in_client_.get());
195 err = snd_seq_set_client_name(in_client_.get(), "Chrome (input)");
196 if (err != 0) {
197 VLOG(1) << "snd_seq_set_client_name fails: " << snd_strerror(err);
198 return CompleteInitialization(Result::INITIALIZATION_ERROR);
199 }
agoode@chromium.org25227512014-06-08 05:12:05 +0000200 }
agoodeb2ead822016-03-11 12:14:35 -0800201
Takashi Toyoshimae8240ab2018-10-03 09:30:11 +0000202 {
203 snd_seq_t* out_seq = nullptr;
204 err = snd_seq_open(&out_seq, kAlsaHw, SND_SEQ_OPEN_OUTPUT, 0);
205 if (err != 0) {
206 VLOG(1) << "snd_seq_open fails: " << snd_strerror(err);
207 return CompleteInitialization(Result::INITIALIZATION_ERROR);
208 }
209 base::AutoLock lock(out_client_lock_);
210 out_client_ = ScopedSndSeqPtr(out_seq);
211 out_client_id_ = snd_seq_client_id(out_client_.get());
212 err = snd_seq_set_client_name(out_client_.get(), "Chrome (output)");
213 if (err != 0) {
214 VLOG(1) << "snd_seq_set_client_name fails: " << snd_strerror(err);
215 return CompleteInitialization(Result::INITIALIZATION_ERROR);
216 }
agoode@chromium.org25227512014-06-08 05:12:05 +0000217 }
218
219 // Create input port.
agoode99d63292015-04-13 08:39:25 -0700220 in_port_id_ = snd_seq_create_simple_port(
Takashi Toyoshimae8240ab2018-10-03 09:30:11 +0000221 in_client_.get(), NULL, kCreateInputPortCaps, kCreatePortType);
agoode99d63292015-04-13 08:39:25 -0700222 if (in_port_id_ < 0) {
223 VLOG(1) << "snd_seq_create_simple_port fails: "
224 << snd_strerror(in_port_id_);
toyoshimf1b88962015-07-09 14:14:51 -0700225 return CompleteInitialization(Result::INITIALIZATION_ERROR);
agoode@chromium.org25227512014-06-08 05:12:05 +0000226 }
227
228 // Subscribe to the announce port.
229 snd_seq_port_subscribe_t* subs;
230 snd_seq_port_subscribe_alloca(&subs);
231 snd_seq_addr_t announce_sender;
232 snd_seq_addr_t announce_dest;
233 announce_sender.client = SND_SEQ_CLIENT_SYSTEM;
234 announce_sender.port = SND_SEQ_PORT_SYSTEM_ANNOUNCE;
agoode99d63292015-04-13 08:39:25 -0700235 announce_dest.client = in_client_id_;
236 announce_dest.port = in_port_id_;
agoode@chromium.org25227512014-06-08 05:12:05 +0000237 snd_seq_port_subscribe_set_sender(subs, &announce_sender);
238 snd_seq_port_subscribe_set_dest(subs, &announce_dest);
Takashi Toyoshimae8240ab2018-10-03 09:30:11 +0000239 err = snd_seq_subscribe_port(in_client_.get(), subs);
agoode@chromium.org25227512014-06-08 05:12:05 +0000240 if (err != 0) {
241 VLOG(1) << "snd_seq_subscribe_port on the announce port fails: "
242 << snd_strerror(err);
toyoshimf1b88962015-07-09 14:14:51 -0700243 return CompleteInitialization(Result::INITIALIZATION_ERROR);
agoode@chromium.org25227512014-06-08 05:12:05 +0000244 }
245
agoodeb2ead822016-03-11 12:14:35 -0800246 // Initialize decoder.
Takashi Toyoshimae8240ab2018-10-03 09:30:11 +0000247 decoder_ = CreateScopedSndMidiEventPtr(0);
248 snd_midi_event_no_status(decoder_.get(), 1);
agoode@chromium.org25227512014-06-08 05:12:05 +0000249
agoodeb2ead822016-03-11 12:14:35 -0800250 // Initialize udev and monitor.
Takashi Toyoshimae8240ab2018-10-03 09:30:11 +0000251 udev_ = device::ScopedUdevPtr(device::udev_new());
252 udev_monitor_ = device::ScopedUdevMonitorPtr(
253 device::udev_monitor_new_from_netlink(udev_.get(), kUdev));
254 if (!udev_monitor_.get()) {
agoode975043d2015-05-11 00:46:17 -0700255 VLOG(1) << "udev_monitor_new_from_netlink fails";
toyoshimf1b88962015-07-09 14:14:51 -0700256 return CompleteInitialization(Result::INITIALIZATION_ERROR);
agoode975043d2015-05-11 00:46:17 -0700257 }
258 err = device::udev_monitor_filter_add_match_subsystem_devtype(
Takashi Toyoshimae8240ab2018-10-03 09:30:11 +0000259 udev_monitor_.get(), kUdevSubsystemSound, nullptr);
agoode975043d2015-05-11 00:46:17 -0700260 if (err != 0) {
261 VLOG(1) << "udev_monitor_add_match_subsystem fails: "
brettwf7f870f2015-06-09 11:05:24 -0700262 << base::safe_strerror(-err);
toyoshimf1b88962015-07-09 14:14:51 -0700263 return CompleteInitialization(Result::INITIALIZATION_ERROR);
agoode975043d2015-05-11 00:46:17 -0700264 }
Takashi Toyoshimae8240ab2018-10-03 09:30:11 +0000265 err = device::udev_monitor_enable_receiving(udev_monitor_.get());
agoode975043d2015-05-11 00:46:17 -0700266 if (err != 0) {
brettwf7f870f2015-06-09 11:05:24 -0700267 VLOG(1) << "udev_monitor_enable_receiving fails: "
268 << base::safe_strerror(-err);
toyoshimf1b88962015-07-09 14:14:51 -0700269 return CompleteInitialization(Result::INITIALIZATION_ERROR);
agoode975043d2015-05-11 00:46:17 -0700270 }
271
agoodeb2ead822016-03-11 12:14:35 -0800272 // Generate hotplug events for existing ports.
273 // TODO(agoode): Check the return value for failure.
274 EnumerateAlsaPorts();
275
276 // Generate hotplug events for existing udev devices. This must be done
277 // after udev_monitor_enable_receiving() is called. See the algorithm
278 // at http://www.signal11.us/oss/udev/.
agoode975043d2015-05-11 00:46:17 -0700279 EnumerateUdevCards();
280
agoodeb2ead822016-03-11 12:14:35 -0800281 // Start processing events. Don't do this before enumeration of both
282 // ALSA and udev.
toyoshimefcee5e2017-06-14 07:14:39 -0700283 service()->task_service()->PostBoundTask(
284 kEventTaskRunner,
285 base::BindOnce(&MidiManagerAlsa::EventLoop, base::Unretained(this)));
agoode@chromium.org25227512014-06-08 05:12:05 +0000286
toyoshimf1b88962015-07-09 14:14:51 -0700287 CompleteInitialization(Result::OK);
toyoshim@chromium.orga97eebf2014-01-03 07:52:39 +0000288}
289
toyoshim@chromium.orgc82e66e2014-02-04 07:05:47 +0000290void MidiManagerAlsa::DispatchSendMidiData(MidiManagerClient* client,
Avi Drissman3528fd02015-12-18 20:11:31 -0500291 uint32_t port_index,
292 const std::vector<uint8_t>& data,
tzik925e2c62018-02-02 07:39:45 +0000293 base::TimeTicks timestamp) {
toyoshimefcee5e2017-06-14 07:14:39 -0700294 service()->task_service()->PostBoundDelayedTask(
295 kSendTaskRunner,
296 base::BindOnce(&MidiManagerAlsa::SendMidiData, base::Unretained(this),
297 client, port_index, data),
Takashi Toyoshimaafb27d52017-09-13 11:50:41 +0000298 MidiService::TimestampToTimeDeltaDelay(timestamp));
toyoshim@chromium.orga97eebf2014-01-03 07:52:39 +0000299}
300
agooded87fc0f2015-05-21 08:29:31 -0700301MidiManagerAlsa::MidiPort::Id::Id() = default;
302
303MidiManagerAlsa::MidiPort::Id::Id(const std::string& bus,
304 const std::string& vendor_id,
305 const std::string& model_id,
306 const std::string& usb_interface_num,
307 const std::string& serial)
308 : bus_(bus),
309 vendor_id_(vendor_id),
310 model_id_(model_id),
311 usb_interface_num_(usb_interface_num),
312 serial_(serial) {
313}
314
315MidiManagerAlsa::MidiPort::Id::Id(const Id&) = default;
316
317MidiManagerAlsa::MidiPort::Id::~Id() = default;
318
319bool MidiManagerAlsa::MidiPort::Id::operator==(const Id& rhs) const {
320 return (bus_ == rhs.bus_) && (vendor_id_ == rhs.vendor_id_) &&
321 (model_id_ == rhs.model_id_) &&
322 (usb_interface_num_ == rhs.usb_interface_num_) &&
323 (serial_ == rhs.serial_);
324}
325
326bool MidiManagerAlsa::MidiPort::Id::empty() const {
327 return bus_.empty() && vendor_id_.empty() && model_id_.empty() &&
328 usb_interface_num_.empty() && serial_.empty();
329}
330
agoode99d63292015-04-13 08:39:25 -0700331MidiManagerAlsa::MidiPort::MidiPort(const std::string& path,
agooded87fc0f2015-05-21 08:29:31 -0700332 const Id& id,
agoode99d63292015-04-13 08:39:25 -0700333 int client_id,
334 int port_id,
335 int midi_device,
336 const std::string& client_name,
337 const std::string& port_name,
338 const std::string& manufacturer,
339 const std::string& version,
340 Type type)
341 : id_(id),
342 midi_device_(midi_device),
343 type_(type),
344 path_(path),
345 client_id_(client_id),
346 port_id_(port_id),
347 client_name_(client_name),
348 port_name_(port_name),
349 manufacturer_(manufacturer),
agoode5a1aa112015-06-21 20:51:00 -0700350 version_(version) {
toyoshim@chromium.org4a8657c2014-02-06 11:23:09 +0000351}
352
agoodeb0582872015-05-20 05:22:24 -0700353MidiManagerAlsa::MidiPort::~MidiPort() = default;
toyoshim@chromium.org4a8657c2014-02-06 11:23:09 +0000354
agoode99d63292015-04-13 08:39:25 -0700355// Note: keep synchronized with the MidiPort::Match* methods.
danakj75afea02016-04-25 20:36:04 -0700356std::unique_ptr<base::Value> MidiManagerAlsa::MidiPort::Value() const {
357 std::unique_ptr<base::DictionaryValue> value(new base::DictionaryValue);
agoode99d63292015-04-13 08:39:25 -0700358
359 std::string type;
360 switch (type_) {
361 case Type::kInput:
362 type = "input";
363 break;
agoode99d63292015-04-13 08:39:25 -0700364 case Type::kOutput:
365 type = "output";
366 break;
367 }
368 value->SetString("type", type);
369 SetStringIfNonEmpty(value.get(), "path", path_);
agoode99d63292015-04-13 08:39:25 -0700370 SetStringIfNonEmpty(value.get(), "clientName", client_name_);
371 SetStringIfNonEmpty(value.get(), "portName", port_name_);
372 value->SetInteger("clientId", client_id_);
373 value->SetInteger("portId", port_id_);
374 value->SetInteger("midiDevice", midi_device_);
375
agooded87fc0f2015-05-21 08:29:31 -0700376 // Flatten id fields.
377 SetStringIfNonEmpty(value.get(), "bus", id_.bus());
378 SetStringIfNonEmpty(value.get(), "vendorId", id_.vendor_id());
379 SetStringIfNonEmpty(value.get(), "modelId", id_.model_id());
380 SetStringIfNonEmpty(value.get(), "usbInterfaceNum", id_.usb_interface_num());
381 SetStringIfNonEmpty(value.get(), "serial", id_.serial());
382
dchengc2aeece2015-12-27 00:54:00 -0800383 return std::move(value);
agoodebd4be9b2015-03-16 19:17:25 -0700384}
agoode@chromium.org25227512014-06-08 05:12:05 +0000385
agoode99d63292015-04-13 08:39:25 -0700386std::string MidiManagerAlsa::MidiPort::JSONValue() const {
387 std::string json;
388 JSONStringValueSerializer serializer(&json);
389 serializer.Serialize(*Value().get());
390 return json;
agoodef212b2a2015-03-19 12:53:23 -0700391}
392
agoode99d63292015-04-13 08:39:25 -0700393// TODO(agoode): Do not use SHA256 here. Instead store a persistent
394// mapping and just use a UUID or other random string.
395// http://crbug.com/465320
396std::string MidiManagerAlsa::MidiPort::OpaqueKey() const {
Avi Drissman3528fd02015-12-18 20:11:31 -0500397 uint8_t hash[crypto::kSHA256Length];
agoode99d63292015-04-13 08:39:25 -0700398 crypto::SHA256HashString(JSONValue(), &hash, sizeof(hash));
399 return base::HexEncode(&hash, sizeof(hash));
agoodebd4be9b2015-03-16 19:17:25 -0700400}
toyoshim@chromium.org4a8657c2014-02-06 11:23:09 +0000401
agoode99d63292015-04-13 08:39:25 -0700402bool MidiManagerAlsa::MidiPort::MatchConnected(const MidiPort& query) const {
403 // Matches on:
404 // connected == true
405 // type
406 // path
407 // id
408 // client_id
409 // port_id
410 // midi_device
411 // client_name
412 // port_name
413 return connected() && (type() == query.type()) && (path() == query.path()) &&
414 (id() == query.id()) && (client_id() == query.client_id()) &&
415 (port_id() == query.port_id()) &&
416 (midi_device() == query.midi_device()) &&
417 (client_name() == query.client_name()) &&
418 (port_name() == query.port_name());
agoodebd4be9b2015-03-16 19:17:25 -0700419}
420
agoode99d63292015-04-13 08:39:25 -0700421bool MidiManagerAlsa::MidiPort::MatchCardPass1(const MidiPort& query) const {
422 // Matches on:
423 // connected == false
424 // type
425 // path
426 // id
427 // port_id
428 // midi_device
429 return MatchCardPass2(query) && (path() == query.path());
agoodebd4be9b2015-03-16 19:17:25 -0700430}
431
agoode99d63292015-04-13 08:39:25 -0700432bool MidiManagerAlsa::MidiPort::MatchCardPass2(const MidiPort& query) const {
433 // Matches on:
434 // connected == false
435 // type
436 // id
437 // port_id
438 // midi_device
439 return !connected() && (type() == query.type()) && (id() == query.id()) &&
440 (port_id() == query.port_id()) &&
441 (midi_device() == query.midi_device());
agoodef212b2a2015-03-19 12:53:23 -0700442}
443
agoode99d63292015-04-13 08:39:25 -0700444bool MidiManagerAlsa::MidiPort::MatchNoCardPass1(const MidiPort& query) const {
445 // Matches on:
446 // connected == false
447 // type
448 // path.empty(), for both this and query
449 // id.empty(), for both this and query
450 // client_id
451 // port_id
452 // client_name
453 // port_name
454 // midi_device == -1, for both this and query
455 return MatchNoCardPass2(query) && (client_id() == query.client_id());
agoodef212b2a2015-03-19 12:53:23 -0700456}
457
agoode99d63292015-04-13 08:39:25 -0700458bool MidiManagerAlsa::MidiPort::MatchNoCardPass2(const MidiPort& query) const {
459 // Matches on:
460 // connected == false
461 // type
462 // path.empty(), for both this and query
463 // id.empty(), for both this and query
464 // port_id
465 // client_name
466 // port_name
467 // midi_device == -1, for both this and query
468 return !connected() && (type() == query.type()) && path().empty() &&
469 query.path().empty() && id().empty() && query.id().empty() &&
470 (port_id() == query.port_id()) &&
471 (client_name() == query.client_name()) &&
472 (port_name() == query.port_name()) && (midi_device() == -1) &&
473 (query.midi_device() == -1);
toyoshim@chromium.org4a8657c2014-02-06 11:23:09 +0000474}
475
agoodeb0582872015-05-20 05:22:24 -0700476MidiManagerAlsa::MidiPortStateBase::~MidiPortStateBase() = default;
agoode99d63292015-04-13 08:39:25 -0700477
478MidiManagerAlsa::MidiPortStateBase::iterator
479MidiManagerAlsa::MidiPortStateBase::Find(
480 const MidiManagerAlsa::MidiPort& port) {
481 auto result = FindConnected(port);
482 if (result == end())
483 result = FindDisconnected(port);
484 return result;
485}
486
487MidiManagerAlsa::MidiPortStateBase::iterator
488MidiManagerAlsa::MidiPortStateBase::FindConnected(
489 const MidiManagerAlsa::MidiPort& port) {
490 // Exact match required for connected ports.
danakj75afea02016-04-25 20:36:04 -0700491 auto it = std::find_if(ports_.begin(), ports_.end(),
492 [&port](std::unique_ptr<MidiPort>& p) {
493 return p->MatchConnected(port);
494 });
agoode99d63292015-04-13 08:39:25 -0700495 return it;
496}
497
498MidiManagerAlsa::MidiPortStateBase::iterator
499MidiManagerAlsa::MidiPortStateBase::FindDisconnected(
500 const MidiManagerAlsa::MidiPort& port) {
501 // Always match on:
502 // type
503 // Possible things to match on:
504 // path
505 // id
506 // client_id
507 // port_id
508 // midi_device
509 // client_name
510 // port_name
511
512 if (!port.path().empty()) {
513 // If path is present, then we have a card-based client.
514
515 // Pass 1. Match on path, id, midi_device, port_id.
516 // This is the best possible match for hardware card-based clients.
517 // This will also match the empty id correctly for devices without an id.
danakj75afea02016-04-25 20:36:04 -0700518 auto it = std::find_if(ports_.begin(), ports_.end(),
519 [&port](std::unique_ptr<MidiPort>& p) {
520 return p->MatchCardPass1(port);
521 });
agoode99d63292015-04-13 08:39:25 -0700522 if (it != ports_.end())
523 return it;
524
525 if (!port.id().empty()) {
526 // Pass 2. Match on id, midi_device, port_id.
527 // This will give us a high-confidence match when a user moves a device to
528 // another USB/Firewire/Thunderbolt/etc port, but only works if the device
529 // has a hardware id.
danakj75afea02016-04-25 20:36:04 -0700530 it = std::find_if(ports_.begin(), ports_.end(),
531 [&port](std::unique_ptr<MidiPort>& p) {
532 return p->MatchCardPass2(port);
533 });
agoode99d63292015-04-13 08:39:25 -0700534 if (it != ports_.end())
535 return it;
536 }
537 } else {
538 // Else, we have a non-card-based client.
539 // Pass 1. Match on client_id, port_id, client_name, port_name.
540 // This will give us a reasonably good match.
danakj75afea02016-04-25 20:36:04 -0700541 auto it = std::find_if(ports_.begin(), ports_.end(),
542 [&port](std::unique_ptr<MidiPort>& p) {
543 return p->MatchNoCardPass1(port);
544 });
agoode99d63292015-04-13 08:39:25 -0700545 if (it != ports_.end())
546 return it;
547
548 // Pass 2. Match on port_id, client_name, port_name.
549 // This is weaker but similar to pass 2 in the hardware card-based clients
550 // match.
danakj75afea02016-04-25 20:36:04 -0700551 it = std::find_if(ports_.begin(), ports_.end(),
552 [&port](std::unique_ptr<MidiPort>& p) {
553 return p->MatchNoCardPass2(port);
554 });
agoode99d63292015-04-13 08:39:25 -0700555 if (it != ports_.end())
556 return it;
557 }
558
559 // No match.
560 return ports_.end();
561}
562
agoodeb0582872015-05-20 05:22:24 -0700563MidiManagerAlsa::MidiPortStateBase::MidiPortStateBase() = default;
agoode99d63292015-04-13 08:39:25 -0700564
agoodedf1b9ff2015-06-25 18:14:50 -0700565MidiManagerAlsa::MidiPortState::MidiPortState() = default;
agoode99d63292015-04-13 08:39:25 -0700566
danakj75afea02016-04-25 20:36:04 -0700567uint32_t MidiManagerAlsa::MidiPortState::push_back(
568 std::unique_ptr<MidiPort> port) {
agoode99d63292015-04-13 08:39:25 -0700569 // Add the web midi index.
Avi Drissman3528fd02015-12-18 20:11:31 -0500570 uint32_t web_port_index = 0;
agoode99d63292015-04-13 08:39:25 -0700571 switch (port->type()) {
572 case MidiPort::Type::kInput:
573 web_port_index = num_input_ports_++;
574 break;
575 case MidiPort::Type::kOutput:
576 web_port_index = num_output_ports_++;
577 break;
578 }
579 port->set_web_port_index(web_port_index);
dchengc2aeece2015-12-27 00:54:00 -0800580 MidiPortStateBase::push_back(std::move(port));
agoode99d63292015-04-13 08:39:25 -0700581 return web_port_index;
582}
583
agoodedf1b9ff2015-06-25 18:14:50 -0700584MidiManagerAlsa::AlsaSeqState::AlsaSeqState() = default;
agoode99d63292015-04-13 08:39:25 -0700585
agoodeb0582872015-05-20 05:22:24 -0700586MidiManagerAlsa::AlsaSeqState::~AlsaSeqState() = default;
agoode99d63292015-04-13 08:39:25 -0700587
588void MidiManagerAlsa::AlsaSeqState::ClientStart(int client_id,
589 const std::string& client_name,
590 snd_seq_client_type_t type) {
591 ClientExit(client_id);
ricea37e45762016-08-25 02:43:39 -0700592 clients_.insert(
Gyuyoung Kim34e191a2018-01-10 09:48:42 +0000593 std::make_pair(client_id, std::make_unique<Client>(client_name, type)));
agoodeb09423b2015-05-11 11:39:57 -0700594 if (IsCardClient(type, client_id))
595 ++card_client_count_;
agoode99d63292015-04-13 08:39:25 -0700596}
597
598bool MidiManagerAlsa::AlsaSeqState::ClientStarted(int client_id) {
599 return clients_.find(client_id) != clients_.end();
600}
601
602void MidiManagerAlsa::AlsaSeqState::ClientExit(int client_id) {
603 auto it = clients_.find(client_id);
604 if (it != clients_.end()) {
agoodeb09423b2015-05-11 11:39:57 -0700605 if (IsCardClient(it->second->type(), client_id))
606 --card_client_count_;
agoode99d63292015-04-13 08:39:25 -0700607 clients_.erase(it);
608 }
609}
610
611void MidiManagerAlsa::AlsaSeqState::PortStart(
612 int client_id,
613 int port_id,
614 const std::string& port_name,
615 MidiManagerAlsa::AlsaSeqState::PortDirection direction,
616 bool midi) {
617 auto it = clients_.find(client_id);
618 if (it != clients_.end())
619 it->second->AddPort(port_id,
Gyuyoung Kim34e191a2018-01-10 09:48:42 +0000620 std::make_unique<Port>(port_name, direction, midi));
agoode99d63292015-04-13 08:39:25 -0700621}
622
623void MidiManagerAlsa::AlsaSeqState::PortExit(int client_id, int port_id) {
624 auto it = clients_.find(client_id);
625 if (it != clients_.end())
626 it->second->RemovePort(port_id);
627}
628
629snd_seq_client_type_t MidiManagerAlsa::AlsaSeqState::ClientType(
630 int client_id) const {
631 auto it = clients_.find(client_id);
632 if (it == clients_.end())
633 return SND_SEQ_USER_CLIENT;
634 return it->second->type();
635}
636
danakj75afea02016-04-25 20:36:04 -0700637std::unique_ptr<MidiManagerAlsa::TemporaryMidiPortState>
agoodeb09423b2015-05-11 11:39:57 -0700638MidiManagerAlsa::AlsaSeqState::ToMidiPortState(const AlsaCardMap& alsa_cards) {
danakj75afea02016-04-25 20:36:04 -0700639 std::unique_ptr<MidiManagerAlsa::TemporaryMidiPortState> midi_ports(
agoode99d63292015-04-13 08:39:25 -0700640 new TemporaryMidiPortState);
agoodeb09423b2015-05-11 11:39:57 -0700641 auto card_it = alsa_cards.begin();
agoode99d63292015-04-13 08:39:25 -0700642
agoodeb09423b2015-05-11 11:39:57 -0700643 int card_midi_device = -1;
agoode99d63292015-04-13 08:39:25 -0700644 for (const auto& client_pair : clients_) {
645 int client_id = client_pair.first;
vmpstr0205abb2016-06-28 18:50:56 -0700646 auto* client = client_pair.second.get();
agoode99d63292015-04-13 08:39:25 -0700647
648 // Get client metadata.
649 const std::string client_name = client->name();
650 std::string manufacturer;
651 std::string driver;
652 std::string path;
agooded87fc0f2015-05-21 08:29:31 -0700653 MidiPort::Id id;
agoode99d63292015-04-13 08:39:25 -0700654 std::string card_name;
655 std::string card_longname;
656 int midi_device = -1;
657
agoodeb09423b2015-05-11 11:39:57 -0700658 if (IsCardClient(client->type(), client_id)) {
659 auto& card = card_it->second;
660 if (card_midi_device == -1)
661 card_midi_device = 0;
662
663 manufacturer = card->manufacturer();
agooded87fc0f2015-05-21 08:29:31 -0700664 path = card->path();
665 id = MidiPort::Id(card->bus(), card->vendor_id(), card->model_id(),
666 card->usb_interface_num(), card->serial());
667 card_name = card->name();
668 card_longname = card->longname();
agoodeb09423b2015-05-11 11:39:57 -0700669 midi_device = card_midi_device;
670
671 ++card_midi_device;
672 if (card_midi_device >= card->midi_device_count()) {
673 card_midi_device = -1;
674 ++card_it;
675 }
676 }
677
agoode99d63292015-04-13 08:39:25 -0700678 for (const auto& port_pair : *client) {
679 int port_id = port_pair.first;
680 const auto& port = port_pair.second;
681
682 if (port->midi()) {
683 std::string version;
684 if (!driver.empty()) {
685 version = driver + " / ";
686 }
687 version +=
688 base::StringPrintf("ALSA library version %d.%d.%d", SND_LIB_MAJOR,
689 SND_LIB_MINOR, SND_LIB_SUBMINOR);
690 PortDirection direction = port->direction();
691 if (direction == PortDirection::kInput ||
692 direction == PortDirection::kDuplex) {
Gyuyoung Kim34e191a2018-01-10 09:48:42 +0000693 midi_ports->push_back(std::make_unique<MidiPort>(
agoode99d63292015-04-13 08:39:25 -0700694 path, id, client_id, port_id, midi_device, client->name(),
ricea37e45762016-08-25 02:43:39 -0700695 port->name(), manufacturer, version, MidiPort::Type::kInput));
agoode99d63292015-04-13 08:39:25 -0700696 }
697 if (direction == PortDirection::kOutput ||
698 direction == PortDirection::kDuplex) {
Gyuyoung Kim34e191a2018-01-10 09:48:42 +0000699 midi_ports->push_back(std::make_unique<MidiPort>(
agoode99d63292015-04-13 08:39:25 -0700700 path, id, client_id, port_id, midi_device, client->name(),
ricea37e45762016-08-25 02:43:39 -0700701 port->name(), manufacturer, version, MidiPort::Type::kOutput));
agoode99d63292015-04-13 08:39:25 -0700702 }
703 }
704 }
705 }
706
dchengc2aeece2015-12-27 00:54:00 -0800707 return midi_ports;
agoode99d63292015-04-13 08:39:25 -0700708}
709
710MidiManagerAlsa::AlsaSeqState::Port::Port(
711 const std::string& name,
712 MidiManagerAlsa::AlsaSeqState::PortDirection direction,
713 bool midi)
714 : name_(name), direction_(direction), midi_(midi) {
715}
716
agoodeb0582872015-05-20 05:22:24 -0700717MidiManagerAlsa::AlsaSeqState::Port::~Port() = default;
agoode99d63292015-04-13 08:39:25 -0700718
719MidiManagerAlsa::AlsaSeqState::Client::Client(const std::string& name,
720 snd_seq_client_type_t type)
mgiucad9af8452015-06-25 02:11:57 -0700721 : name_(name), type_(type) {
agoode99d63292015-04-13 08:39:25 -0700722}
723
agoodeb0582872015-05-20 05:22:24 -0700724MidiManagerAlsa::AlsaSeqState::Client::~Client() = default;
agoode99d63292015-04-13 08:39:25 -0700725
danakj75afea02016-04-25 20:36:04 -0700726void MidiManagerAlsa::AlsaSeqState::Client::AddPort(
727 int addr,
728 std::unique_ptr<Port> port) {
limasdfe59d0392015-11-19 20:28:57 -0800729 ports_[addr] = std::move(port);
agoode99d63292015-04-13 08:39:25 -0700730}
731
732void MidiManagerAlsa::AlsaSeqState::Client::RemovePort(int addr) {
mgiucad9af8452015-06-25 02:11:57 -0700733 ports_.erase(addr);
agoode99d63292015-04-13 08:39:25 -0700734}
735
736MidiManagerAlsa::AlsaSeqState::Client::PortMap::const_iterator
737MidiManagerAlsa::AlsaSeqState::Client::begin() const {
738 return ports_.begin();
739}
740
741MidiManagerAlsa::AlsaSeqState::Client::PortMap::const_iterator
742MidiManagerAlsa::AlsaSeqState::Client::end() const {
743 return ports_.end();
agoodeaf6e9f52015-03-24 10:23:49 -0700744}
745
agoodeb09423b2015-05-11 11:39:57 -0700746MidiManagerAlsa::AlsaCard::AlsaCard(udev_device* dev,
agooded87fc0f2015-05-21 08:29:31 -0700747 const std::string& name,
748 const std::string& longname,
749 const std::string& driver,
agoodeb09423b2015-05-11 11:39:57 -0700750 int midi_device_count)
agooded87fc0f2015-05-21 08:29:31 -0700751 : 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)) {
agoodeb09423b2015-05-11 11:39:57 -0700772}
773
agoodeb0582872015-05-20 05:22:24 -0700774MidiManagerAlsa::AlsaCard::~AlsaCard() = default;
agoodeb09423b2015-05-11 11:39:57 -0700775
agoode55a8b522015-03-08 12:40:17 -0700776// static
agoodeb09423b2015-05-11 11:39:57 -0700777std::string MidiManagerAlsa::AlsaCard::ExtractManufacturerString(
agoode5e4e9cd2015-03-09 12:34:24 -0700778 const std::string& udev_id_vendor,
agoode55a8b522015-03-08 12:40:17 -0700779 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:
agoodef212b2a2015-03-19 12:53:23 -0700785 // 1. Vendor name from the hardware device string, from udev properties
786 // or sysattrs.
agoode5e4e9cd2015-03-09 12:34:24 -0700787 // 2. Vendor name from the udev database (property ID_VENDOR_FROM_DATABASE).
agoode55a8b522015-03-08 12:40:17 -0700788 // 3. Heuristic from ALSA.
789
agoodee83758c2015-03-23 22:07:54 -0700790 // 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)) {
agoode55a8b522015-03-08 12:40:17 -0700792 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 ");
agoodef212b2a2015-03-19 12:53:23 -0700805 if (at_index && at_index != std::string::npos) {
agoode55a8b522015-03-08 12:40:17 -0700806 size_t name_index = alsa_longname.rfind(alsa_name, at_index - 1);
agoodef212b2a2015-03-19 12:53:23 -0700807 if (name_index && name_index != std::string::npos)
agoode55a8b522015-03-08 12:40:17 -0700808 return alsa_longname.substr(0, name_index - 1);
809 }
810
811 // Failure.
812 return "";
813}
814
toyoshimefcee5e2017-06-14 07:14:39 -0700815void MidiManagerAlsa::SendMidiData(MidiManagerClient* client,
toyoshimf4d61522017-02-10 02:03:32 -0800816 uint32_t port_index,
Avi Drissman3528fd02015-12-18 20:11:31 -0500817 const std::vector<uint8_t>& data) {
Takashi Toyoshima0ae49702017-09-01 04:59:51 +0000818 ScopedSndMidiEventPtr encoder = CreateScopedSndMidiEventPtr(kSendBufferSize);
agoode5a1aa112015-06-21 20:51:00 -0700819 for (const auto datum : data) {
agoodebd4be9b2015-03-16 19:17:25 -0700820 snd_seq_event_t event;
Takashi Toyoshima0ae49702017-09-01 04:59:51 +0000821 int result = snd_midi_event_encode_byte(encoder.get(), datum, &event);
agoodebd4be9b2015-03-16 19:17:25 -0700822 if (result == 1) {
823 // Full event, send it.
agoode99d63292015-04-13 08:39:25 -0700824 base::AutoLock lock(out_ports_lock_);
825 auto it = out_ports_.find(port_index);
826 if (it != out_ports_.end()) {
Takashi Toyoshima9687b7c2017-07-19 09:54:14 +0000827 base::AutoLock lock(out_client_lock_);
828 if (!out_client_)
829 return;
agoode99d63292015-04-13 08:39:25 -0700830 snd_seq_ev_set_source(&event, it->second);
831 snd_seq_ev_set_subs(&event);
832 snd_seq_ev_set_direct(&event);
agoode5a1aa112015-06-21 20:51:00 -0700833 snd_seq_event_output_direct(out_client_.get(), &event);
agoode99d63292015-04-13 08:39:25 -0700834 }
agoodebd4be9b2015-03-16 19:17:25 -0700835 }
836 }
toyoshimf4d61522017-02-10 02:03:32 -0800837
838 // Acknowledge send.
839 AccumulateMidiBytesSent(client, data.size());
agoodebd4be9b2015-03-16 19:17:25 -0700840}
841
toyoshimefcee5e2017-06-14 07:14:39 -0700842void MidiManagerAlsa::EventLoop() {
agoode975043d2015-05-11 00:46:17 -0700843 bool loop_again = true;
agoodebd4be9b2015-03-16 19:17:25 -0700844
agoode975043d2015-05-11 00:46:17 -0700845 struct pollfd pfd[2];
agoode5a1aa112015-06-21 20:51:00 -0700846 snd_seq_poll_descriptors(in_client_.get(), &pfd[0], 1, POLLIN);
agoode975043d2015-05-11 00:46:17 -0700847 pfd[1].fd = device::udev_monitor_get_fd(udev_monitor_.get());
848 pfd[1].events = POLLIN;
agoodebd4be9b2015-03-16 19:17:25 -0700849
agoode975043d2015-05-11 00:46:17 -0700850 int err = HANDLE_EINTR(poll(pfd, arraysize(pfd), -1));
851 if (err < 0) {
brettwf7f870f2015-06-09 11:05:24 -0700852 VLOG(1) << "poll fails: " << base::safe_strerror(errno);
agoode975043d2015-05-11 00:46:17 -0700853 loop_again = false;
agoodeaf6e9f52015-03-24 10:23:49 -0700854 } else {
agoode975043d2015-05-11 00:46:17 -0700855 if (pfd[0].revents & POLLIN) {
856 // Read available incoming MIDI data.
857 int remaining;
tzik925e2c62018-02-02 07:39:45 +0000858 base::TimeTicks timestamp = base::TimeTicks::Now();
agoode975043d2015-05-11 00:46:17 -0700859 do {
860 snd_seq_event_t* event;
agoode5a1aa112015-06-21 20:51:00 -0700861 err = snd_seq_event_input(in_client_.get(), &event);
862 remaining = snd_seq_event_input_pending(in_client_.get(), 0);
agoode975043d2015-05-11 00:46:17 -0700863
864 if (err == -ENOSPC) {
865 // Handle out of space error.
866 VLOG(1) << "snd_seq_event_input detected buffer overrun";
867 // We've lost events: check another way to see if we need to shut
868 // down.
agoode975043d2015-05-11 00:46:17 -0700869 } else if (err == -EAGAIN) {
870 // We've read all the data.
871 } else if (err < 0) {
872 // Handle other errors.
873 VLOG(1) << "snd_seq_event_input fails: " << snd_strerror(err);
874 // TODO(agoode): Use RecordAction() or similar to log this.
875 loop_again = false;
876 } else if (event->source.client == SND_SEQ_CLIENT_SYSTEM &&
877 event->source.port == SND_SEQ_PORT_SYSTEM_ANNOUNCE) {
878 // Handle announce events.
879 switch (event->type) {
880 case SND_SEQ_EVENT_PORT_START:
881 // Don't use SND_SEQ_EVENT_CLIENT_START because the
882 // client name may not be set by the time we query
883 // it. It should be set by the time ports are made.
884 ProcessClientStartEvent(event->data.addr.client);
885 ProcessPortStartEvent(event->data.addr);
886 break;
887 case SND_SEQ_EVENT_CLIENT_EXIT:
888 // Check for disconnection of our "out" client. This means "shut
889 // down".
890 if (event->data.addr.client == out_client_id_) {
891 loop_again = false;
892 remaining = 0;
893 } else
894 ProcessClientExitEvent(event->data.addr);
895 break;
896 case SND_SEQ_EVENT_PORT_EXIT:
897 ProcessPortExitEvent(event->data.addr);
898 break;
899 }
900 } else {
901 // Normal operation.
902 ProcessSingleEvent(event, timestamp);
903 }
904 } while (remaining > 0);
905 }
906 if (pfd[1].revents & POLLIN) {
907 device::ScopedUdevDevicePtr dev(
908 device::udev_monitor_receive_device(udev_monitor_.get()));
909 if (dev.get())
910 ProcessUdevEvent(dev.get());
911 else
912 VLOG(1) << "udev_monitor_receive_device fails";
913 }
agoodebd4be9b2015-03-16 19:17:25 -0700914 }
915
agoodebd4be9b2015-03-16 19:17:25 -0700916 // Do again.
toyoshimf4d61522017-02-10 02:03:32 -0800917 if (loop_again) {
toyoshimefcee5e2017-06-14 07:14:39 -0700918 service()->task_service()->PostBoundTask(
919 kEventTaskRunner,
920 base::BindOnce(&MidiManagerAlsa::EventLoop, base::Unretained(this)));
toyoshimf4d61522017-02-10 02:03:32 -0800921 }
agoodebd4be9b2015-03-16 19:17:25 -0700922}
923
924void MidiManagerAlsa::ProcessSingleEvent(snd_seq_event_t* event,
tzik925e2c62018-02-02 07:39:45 +0000925 base::TimeTicks timestamp) {
agoode99d63292015-04-13 08:39:25 -0700926 auto source_it =
927 source_map_.find(AddrToInt(event->source.client, event->source.port));
agoodebd4be9b2015-03-16 19:17:25 -0700928 if (source_it != source_map_.end()) {
Avi Drissman3528fd02015-12-18 20:11:31 -0500929 uint32_t source = source_it->second;
agoodebd4be9b2015-03-16 19:17:25 -0700930 if (event->type == SND_SEQ_EVENT_SYSEX) {
931 // Special! Variable-length sysex.
Avi Drissman3528fd02015-12-18 20:11:31 -0500932 ReceiveMidiData(source, static_cast<const uint8_t*>(event->data.ext.ptr),
agoodebd4be9b2015-03-16 19:17:25 -0700933 event->data.ext.len, timestamp);
934 } else {
935 // Otherwise, decode this and send that on.
936 unsigned char buf[12];
agoode5a1aa112015-06-21 20:51:00 -0700937 long count =
938 snd_midi_event_decode(decoder_.get(), buf, sizeof(buf), event);
agoodebd4be9b2015-03-16 19:17:25 -0700939 if (count <= 0) {
940 if (count != -ENOENT) {
941 // ENOENT means that it's not a MIDI message, which is not an
942 // error, but other negative values are errors for us.
943 VLOG(1) << "snd_midi_event_decoder fails " << snd_strerror(count);
944 // TODO(agoode): Record this failure.
945 }
946 } else {
947 ReceiveMidiData(source, buf, count, timestamp);
948 }
949 }
950 }
951}
952
agoode99d63292015-04-13 08:39:25 -0700953void MidiManagerAlsa::ProcessClientStartEvent(int client_id) {
954 // Ignore if client is already started.
955 if (alsa_seq_state_.ClientStarted(client_id))
956 return;
957
958 snd_seq_client_info_t* client_info;
959 snd_seq_client_info_alloca(&client_info);
agoode5a1aa112015-06-21 20:51:00 -0700960 int err =
961 snd_seq_get_any_client_info(in_client_.get(), client_id, client_info);
agoode99d63292015-04-13 08:39:25 -0700962 if (err != 0)
963 return;
964
965 // Skip our own clients.
966 if ((client_id == in_client_id_) || (client_id == out_client_id_))
967 return;
968
969 // Update our view of ALSA seq state.
970 alsa_seq_state_.ClientStart(client_id,
971 snd_seq_client_info_get_name(client_info),
972 snd_seq_client_info_get_type(client_info));
973
974 // Generate Web MIDI events.
975 UpdatePortStateAndGenerateEvents();
976}
977
978void MidiManagerAlsa::ProcessPortStartEvent(const snd_seq_addr_t& addr) {
979 snd_seq_port_info_t* port_info;
980 snd_seq_port_info_alloca(&port_info);
agoode5a1aa112015-06-21 20:51:00 -0700981 int err = snd_seq_get_any_port_info(in_client_.get(), addr.client, addr.port,
982 port_info);
agoode99d63292015-04-13 08:39:25 -0700983 if (err != 0)
984 return;
985
986 unsigned int caps = snd_seq_port_info_get_capability(port_info);
987 bool input = (caps & kRequiredInputPortCaps) == kRequiredInputPortCaps;
988 bool output = (caps & kRequiredOutputPortCaps) == kRequiredOutputPortCaps;
989 AlsaSeqState::PortDirection direction;
990 if (input && output)
991 direction = AlsaSeqState::PortDirection::kDuplex;
992 else if (input)
993 direction = AlsaSeqState::PortDirection::kInput;
994 else if (output)
995 direction = AlsaSeqState::PortDirection::kOutput;
996 else
997 return;
998
999 // Update our view of ALSA seq state.
1000 alsa_seq_state_.PortStart(
1001 addr.client, addr.port, snd_seq_port_info_get_name(port_info), direction,
1002 snd_seq_port_info_get_type(port_info) & SND_SEQ_PORT_TYPE_MIDI_GENERIC);
1003 // Generate Web MIDI events.
1004 UpdatePortStateAndGenerateEvents();
1005}
1006
1007void MidiManagerAlsa::ProcessClientExitEvent(const snd_seq_addr_t& addr) {
1008 // Update our view of ALSA seq state.
1009 alsa_seq_state_.ClientExit(addr.client);
1010 // Generate Web MIDI events.
1011 UpdatePortStateAndGenerateEvents();
1012}
1013
1014void MidiManagerAlsa::ProcessPortExitEvent(const snd_seq_addr_t& addr) {
1015 // Update our view of ALSA seq state.
1016 alsa_seq_state_.PortExit(addr.client, addr.port);
1017 // Generate Web MIDI events.
1018 UpdatePortStateAndGenerateEvents();
1019}
1020
agoode975043d2015-05-11 00:46:17 -07001021void MidiManagerAlsa::ProcessUdevEvent(udev_device* dev) {
1022 // Only card devices have this property set, and only when they are
1023 // fully initialized.
1024 if (!device::udev_device_get_property_value(dev,
1025 kUdevPropertySoundInitialized))
1026 return;
1027
1028 // Get the action. If no action, then we are doing first time enumeration
1029 // and the device is treated as new.
1030 const char* action = device::udev_device_get_action(dev);
1031 if (!action)
1032 action = kUdevActionChange;
1033
1034 if (strcmp(action, kUdevActionChange) == 0) {
agoodeb09423b2015-05-11 11:39:57 -07001035 AddCard(dev);
1036 // Generate Web MIDI events.
1037 UpdatePortStateAndGenerateEvents();
agoode975043d2015-05-11 00:46:17 -07001038 } else if (strcmp(action, kUdevActionRemove) == 0) {
agoodeb09423b2015-05-11 11:39:57 -07001039 RemoveCard(GetCardNumber(dev));
1040 // Generate Web MIDI events.
1041 UpdatePortStateAndGenerateEvents();
agoode975043d2015-05-11 00:46:17 -07001042 }
1043}
1044
agoodeb09423b2015-05-11 11:39:57 -07001045void MidiManagerAlsa::AddCard(udev_device* dev) {
1046 int number = GetCardNumber(dev);
1047 if (number == -1)
1048 return;
1049
1050 RemoveCard(number);
1051
1052 snd_ctl_card_info_t* card;
1053 snd_hwdep_info_t* hwdep;
1054 snd_ctl_card_info_alloca(&card);
1055 snd_hwdep_info_alloca(&hwdep);
1056 const std::string id = base::StringPrintf("hw:CARD=%i", number);
1057 snd_ctl_t* handle;
1058 int err = snd_ctl_open(&handle, id.c_str(), 0);
1059 if (err != 0) {
1060 VLOG(1) << "snd_ctl_open fails: " << snd_strerror(err);
1061 return;
1062 }
1063 err = snd_ctl_card_info(handle, card);
1064 if (err != 0) {
1065 VLOG(1) << "snd_ctl_card_info fails: " << snd_strerror(err);
1066 snd_ctl_close(handle);
1067 return;
1068 }
1069 std::string name = snd_ctl_card_info_get_name(card);
1070 std::string longname = snd_ctl_card_info_get_longname(card);
1071 std::string driver = snd_ctl_card_info_get_driver(card);
1072
1073 // Count rawmidi devices (not subdevices).
1074 int midi_count = 0;
1075 for (int device = -1;
1076 !snd_ctl_rawmidi_next_device(handle, &device) && device >= 0;)
1077 ++midi_count;
1078
1079 // Count any hwdep synths that become MIDI devices outside of rawmidi.
1080 //
1081 // Explanation:
1082 // Any kernel driver can create an ALSA client (visible to us).
1083 // With modern hardware, only rawmidi devices do this. Kernel
1084 // drivers create rawmidi devices and the rawmidi subsystem makes
1085 // the seq clients. But the OPL3 driver is special, it does not
1086 // make a rawmidi device but a seq client directly. (This is the
1087 // only one to worry about in the kernel code, as of 2015-03-23.)
1088 //
1089 // OPL3 is very old (but still possible to get in new
1090 // hardware). It is unlikely that new drivers would not use
1091 // rawmidi and defeat our heuristic.
1092 //
1093 // Longer term, support should be added in the kernel to expose a
1094 // direct link from card->client (or client->card) so that all
1095 // these heuristics will be obsolete. Once that is there, we can
1096 // assume our old heuristics will work on old kernels and the new
1097 // robust code will be used on new. Then we will not need to worry
1098 // about changes to kernel internals breaking our code.
1099 // See the TODO above at kMinimumClientIdForCards.
1100 for (int device = -1;
1101 !snd_ctl_hwdep_next_device(handle, &device) && device >= 0;) {
1102 err = snd_ctl_hwdep_info(handle, hwdep);
1103 if (err != 0) {
1104 VLOG(1) << "snd_ctl_hwdep_info fails: " << snd_strerror(err);
1105 continue;
1106 }
1107 snd_hwdep_iface_t iface = snd_hwdep_info_get_iface(hwdep);
1108 if (iface == SND_HWDEP_IFACE_OPL2 || iface == SND_HWDEP_IFACE_OPL3 ||
1109 iface == SND_HWDEP_IFACE_OPL4)
1110 ++midi_count;
1111 }
1112 snd_ctl_close(handle);
1113
mgiucad9af8452015-06-25 02:11:57 -07001114 if (midi_count > 0) {
danakj75afea02016-04-25 20:36:04 -07001115 std::unique_ptr<AlsaCard> card(
mgiucad9af8452015-06-25 02:11:57 -07001116 new AlsaCard(dev, name, longname, driver, midi_count));
limasdfe59d0392015-11-19 20:28:57 -08001117 alsa_cards_.insert(std::make_pair(number, std::move(card)));
mgiucad9af8452015-06-25 02:11:57 -07001118 alsa_card_midi_count_ += midi_count;
1119 }
agoodeb09423b2015-05-11 11:39:57 -07001120}
1121
1122void MidiManagerAlsa::RemoveCard(int number) {
1123 auto it = alsa_cards_.find(number);
1124 if (it == alsa_cards_.end())
1125 return;
1126
1127 alsa_card_midi_count_ -= it->second->midi_device_count();
agoodeb09423b2015-05-11 11:39:57 -07001128 alsa_cards_.erase(it);
1129}
1130
agoode99d63292015-04-13 08:39:25 -07001131void MidiManagerAlsa::UpdatePortStateAndGenerateEvents() {
agoodeb09423b2015-05-11 11:39:57 -07001132 // Verify that our information from ALSA and udev are in sync. If
1133 // not, we cannot generate events right now.
1134 if (alsa_card_midi_count_ != alsa_seq_state_.card_client_count())
1135 return;
1136
agoode99d63292015-04-13 08:39:25 -07001137 // Generate new port state.
agoodeb09423b2015-05-11 11:39:57 -07001138 auto new_port_state = alsa_seq_state_.ToMidiPortState(alsa_cards_);
agoode99d63292015-04-13 08:39:25 -07001139
1140 // Disconnect any connected old ports that are now missing.
agoode5ebc4932015-12-01 08:25:12 -08001141 for (auto& old_port : port_state_) {
agoode99d63292015-04-13 08:39:25 -07001142 if (old_port->connected() &&
1143 (new_port_state->FindConnected(*old_port) == new_port_state->end())) {
1144 old_port->set_connected(false);
Avi Drissman3528fd02015-12-18 20:11:31 -05001145 uint32_t web_port_index = old_port->web_port_index();
agoode99d63292015-04-13 08:39:25 -07001146 switch (old_port->type()) {
1147 case MidiPort::Type::kInput:
1148 source_map_.erase(
1149 AddrToInt(old_port->client_id(), old_port->port_id()));
toyoshimec2570a2016-10-21 02:15:27 -07001150 SetInputPortState(web_port_index, PortState::DISCONNECTED);
agoode99d63292015-04-13 08:39:25 -07001151 break;
1152 case MidiPort::Type::kOutput:
1153 DeleteAlsaOutputPort(web_port_index);
toyoshimec2570a2016-10-21 02:15:27 -07001154 SetOutputPortState(web_port_index, PortState::DISCONNECTED);
agoode99d63292015-04-13 08:39:25 -07001155 break;
1156 }
1157 }
1158 }
1159
1160 // Reconnect or add new ports.
1161 auto it = new_port_state->begin();
1162 while (it != new_port_state->end()) {
agoode5ebc4932015-12-01 08:25:12 -08001163 auto& new_port = *it;
agoode99d63292015-04-13 08:39:25 -07001164 auto old_port = port_state_.Find(*new_port);
1165 if (old_port == port_state_.end()) {
1166 // Add new port.
agoode5ebc4932015-12-01 08:25:12 -08001167 const auto& opaque_key = new_port->OpaqueKey();
1168 const auto& manufacturer = new_port->manufacturer();
1169 const auto& port_name = new_port->port_name();
1170 const auto& version = new_port->version();
1171 const auto& type = new_port->type();
1172 const auto& client_id = new_port->client_id();
1173 const auto& port_id = new_port->port_id();
1174
dchengc2aeece2015-12-27 00:54:00 -08001175 uint32_t web_port_index = port_state_.push_back(std::move(new_port));
agoode5ebc4932015-12-01 08:25:12 -08001176 it = new_port_state->erase(it);
1177
Adithya Srinivasan33252732018-10-17 15:59:40 +00001178 mojom::PortInfo info(opaque_key, manufacturer, port_name, version,
1179 PortState::OPENED);
agoode5ebc4932015-12-01 08:25:12 -08001180 switch (type) {
agoode99d63292015-04-13 08:39:25 -07001181 case MidiPort::Type::kInput:
agoode5ebc4932015-12-01 08:25:12 -08001182 if (Subscribe(web_port_index, client_id, port_id))
agoode99d63292015-04-13 08:39:25 -07001183 AddInputPort(info);
1184 break;
agoode99d63292015-04-13 08:39:25 -07001185 case MidiPort::Type::kOutput:
agoode5ebc4932015-12-01 08:25:12 -08001186 if (CreateAlsaOutputPort(web_port_index, client_id, port_id))
agoode99d63292015-04-13 08:39:25 -07001187 AddOutputPort(info);
1188 break;
1189 }
agoode99d63292015-04-13 08:39:25 -07001190 } else if (!(*old_port)->connected()) {
1191 // Reconnect.
Avi Drissman3528fd02015-12-18 20:11:31 -05001192 uint32_t web_port_index = (*old_port)->web_port_index();
agoode99d63292015-04-13 08:39:25 -07001193 (*old_port)->Update(new_port->path(), new_port->client_id(),
1194 new_port->port_id(), new_port->client_name(),
1195 new_port->port_name(), new_port->manufacturer(),
1196 new_port->version());
1197 switch ((*old_port)->type()) {
1198 case MidiPort::Type::kInput:
1199 if (Subscribe(web_port_index, (*old_port)->client_id(),
1200 (*old_port)->port_id()))
toyoshimec2570a2016-10-21 02:15:27 -07001201 SetInputPortState(web_port_index, PortState::OPENED);
agoode99d63292015-04-13 08:39:25 -07001202 break;
agoode99d63292015-04-13 08:39:25 -07001203 case MidiPort::Type::kOutput:
1204 if (CreateAlsaOutputPort(web_port_index, (*old_port)->client_id(),
1205 (*old_port)->port_id()))
toyoshimec2570a2016-10-21 02:15:27 -07001206 SetOutputPortState(web_port_index, PortState::OPENED);
agoode99d63292015-04-13 08:39:25 -07001207 break;
1208 }
1209 (*old_port)->set_connected(true);
1210 ++it;
1211 } else {
1212 ++it;
1213 }
1214 }
1215}
1216
agoode975043d2015-05-11 00:46:17 -07001217// TODO(agoode): return false on failure.
agoode99d63292015-04-13 08:39:25 -07001218void MidiManagerAlsa::EnumerateAlsaPorts() {
1219 snd_seq_client_info_t* client_info;
1220 snd_seq_client_info_alloca(&client_info);
1221 snd_seq_port_info_t* port_info;
1222 snd_seq_port_info_alloca(&port_info);
1223
1224 // Enumerate clients.
1225 snd_seq_client_info_set_client(client_info, -1);
agoode5a1aa112015-06-21 20:51:00 -07001226 while (!snd_seq_query_next_client(in_client_.get(), client_info)) {
agoode99d63292015-04-13 08:39:25 -07001227 int client_id = snd_seq_client_info_get_client(client_info);
1228 ProcessClientStartEvent(client_id);
1229
1230 // Enumerate ports.
1231 snd_seq_port_info_set_client(port_info, client_id);
1232 snd_seq_port_info_set_port(port_info, -1);
agoode5a1aa112015-06-21 20:51:00 -07001233 while (!snd_seq_query_next_port(in_client_.get(), port_info)) {
agoode99d63292015-04-13 08:39:25 -07001234 const snd_seq_addr_t* addr = snd_seq_port_info_get_addr(port_info);
1235 ProcessPortStartEvent(*addr);
1236 }
1237 }
1238}
1239
agoode975043d2015-05-11 00:46:17 -07001240bool MidiManagerAlsa::EnumerateUdevCards() {
1241 int err;
1242
1243 device::ScopedUdevEnumeratePtr enumerate(
1244 device::udev_enumerate_new(udev_.get()));
1245 if (!enumerate.get()) {
1246 VLOG(1) << "udev_enumerate_new fails";
1247 return false;
1248 }
1249
1250 err = device::udev_enumerate_add_match_subsystem(enumerate.get(),
1251 kUdevSubsystemSound);
1252 if (err) {
1253 VLOG(1) << "udev_enumerate_add_match_subsystem fails: "
brettwf7f870f2015-06-09 11:05:24 -07001254 << base::safe_strerror(-err);
agoode975043d2015-05-11 00:46:17 -07001255 return false;
1256 }
1257
1258 err = device::udev_enumerate_scan_devices(enumerate.get());
1259 if (err) {
brettwf7f870f2015-06-09 11:05:24 -07001260 VLOG(1) << "udev_enumerate_scan_devices fails: "
1261 << base::safe_strerror(-err);
agoode975043d2015-05-11 00:46:17 -07001262 return false;
1263 }
1264
1265 udev_list_entry* list_entry;
1266 auto* devices = device::udev_enumerate_get_list_entry(enumerate.get());
1267 udev_list_entry_foreach(list_entry, devices) {
1268 const char* path = device::udev_list_entry_get_name(list_entry);
1269 device::ScopedUdevDevicePtr dev(
1270 device::udev_device_new_from_syspath(udev_.get(), path));
1271 if (dev.get())
1272 ProcessUdevEvent(dev.get());
1273 }
1274
1275 return true;
1276}
1277
Avi Drissman3528fd02015-12-18 20:11:31 -05001278bool MidiManagerAlsa::CreateAlsaOutputPort(uint32_t port_index,
agoode99d63292015-04-13 08:39:25 -07001279 int client_id,
1280 int port_id) {
1281 // Create the port.
Takashi Toyoshimae8240ab2018-10-03 09:30:11 +00001282 int out_port;
1283 {
1284 base::AutoLock lock(out_client_lock_);
1285 out_port = snd_seq_create_simple_port(
1286 out_client_.get(), NULL, kCreateOutputPortCaps, kCreatePortType);
1287
1288 if (out_port < 0) {
1289 VLOG(1) << "snd_seq_create_simple_port fails: " << snd_strerror(out_port);
1290 return false;
1291 }
1292
1293 // Activate port subscription.
1294 snd_seq_port_subscribe_t* subs;
1295 snd_seq_port_subscribe_alloca(&subs);
1296 snd_seq_addr_t sender;
1297 sender.client = out_client_id_;
1298 sender.port = out_port;
1299 snd_seq_port_subscribe_set_sender(subs, &sender);
1300 snd_seq_addr_t dest;
1301 dest.client = client_id;
1302 dest.port = port_id;
1303 snd_seq_port_subscribe_set_dest(subs, &dest);
1304 int err = snd_seq_subscribe_port(out_client_.get(), subs);
1305 if (err != 0) {
1306 VLOG(1) << "snd_seq_subscribe_port fails: " << snd_strerror(err);
1307 snd_seq_delete_simple_port(out_client_.get(), out_port);
1308 return false;
1309 }
agoode99d63292015-04-13 08:39:25 -07001310 }
1311
1312 // Update our map.
1313 base::AutoLock lock(out_ports_lock_);
1314 out_ports_[port_index] = out_port;
1315 return true;
1316}
1317
Avi Drissman3528fd02015-12-18 20:11:31 -05001318void MidiManagerAlsa::DeleteAlsaOutputPort(uint32_t port_index) {
Takashi Toyoshimae8240ab2018-10-03 09:30:11 +00001319 int alsa_port;
1320 {
1321 base::AutoLock lock(out_ports_lock_);
1322 auto it = out_ports_.find(port_index);
1323 if (it == out_ports_.end())
1324 return;
1325 alsa_port = it->second;
1326 out_ports_.erase(it);
1327 }
1328 {
1329 base::AutoLock lock(out_client_lock_);
1330 snd_seq_delete_simple_port(out_client_.get(), alsa_port);
1331 }
agoode99d63292015-04-13 08:39:25 -07001332}
1333
Avi Drissman3528fd02015-12-18 20:11:31 -05001334bool MidiManagerAlsa::Subscribe(uint32_t port_index,
1335 int client_id,
1336 int port_id) {
agoode99d63292015-04-13 08:39:25 -07001337 // Activate port subscription.
1338 snd_seq_port_subscribe_t* subs;
1339 snd_seq_port_subscribe_alloca(&subs);
1340 snd_seq_addr_t sender;
1341 sender.client = client_id;
1342 sender.port = port_id;
1343 snd_seq_port_subscribe_set_sender(subs, &sender);
1344 snd_seq_addr_t dest;
1345 dest.client = in_client_id_;
1346 dest.port = in_port_id_;
1347 snd_seq_port_subscribe_set_dest(subs, &dest);
agoode5a1aa112015-06-21 20:51:00 -07001348 int err = snd_seq_subscribe_port(in_client_.get(), subs);
agoode99d63292015-04-13 08:39:25 -07001349 if (err != 0) {
1350 VLOG(1) << "snd_seq_subscribe_port fails: " << snd_strerror(err);
1351 return false;
1352 }
1353
1354 // Update our map.
1355 source_map_[AddrToInt(client_id, port_id)] = port_index;
1356 return true;
1357}
1358
Takashi Toyoshima0ae49702017-09-01 04:59:51 +00001359MidiManagerAlsa::ScopedSndMidiEventPtr
1360MidiManagerAlsa::CreateScopedSndMidiEventPtr(size_t size) {
1361 snd_midi_event_t* coder;
1362 snd_midi_event_new(size, &coder);
1363 return ScopedSndMidiEventPtr(coder);
1364}
1365
toyoshimf4d61522017-02-10 02:03:32 -08001366MidiManager* MidiManager::Create(MidiService* service) {
1367 return new MidiManagerAlsa(service);
toyoshim@chromium.orga97eebf2014-01-03 07:52:39 +00001368}
1369
toyoshime147c5e2015-05-07 21:58:31 -07001370} // namespace midi