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 | #ifndef MEDIA_MIDI_MIDI_MANAGER_ALSA_H_ |
| 6 | #define MEDIA_MIDI_MIDI_MANAGER_ALSA_H_ |
toyoshim@chromium.org | a97eebf | 2014-01-03 07:52:39 +0000 | [diff] [blame] | 7 | |
agoode@chromium.org | 2522751 | 2014-06-08 05:12:05 +0000 | [diff] [blame] | 8 | #include <alsa/asoundlib.h> |
avi | 793390d | 2015-12-22 22:22:36 -0800 | [diff] [blame] | 9 | #include <stdint.h> |
danakj | 75afea0 | 2016-04-25 20:36:04 -0700 | [diff] [blame] | 10 | |
limasdf | e59d039 | 2015-11-19 20:28:57 -0800 | [diff] [blame] | 11 | #include <map> |
danakj | 75afea0 | 2016-04-25 20:36:04 -0700 | [diff] [blame] | 12 | #include <memory> |
Takuto Ikuta | fd795cb | 2019-01-05 00:32:48 +0000 | [diff] [blame] | 13 | #include <unordered_map> |
dcheng | c2aeece | 2015-12-27 00:54:00 -0800 | [diff] [blame] | 14 | #include <utility> |
toyoshim@chromium.org | a97eebf | 2014-01-03 07:52:39 +0000 | [diff] [blame] | 15 | #include <vector> |
| 16 | |
agoode | 55a8b52 | 2015-03-08 12:40:17 -0700 | [diff] [blame] | 17 | #include "base/gtest_prod_util.h" |
avi | eea95e8 | 2015-12-18 20:27:08 -0800 | [diff] [blame] | 18 | #include "base/macros.h" |
agoode@chromium.org | 2522751 | 2014-06-08 05:12:05 +0000 | [diff] [blame] | 19 | #include "base/synchronization/lock.h" |
Takashi Toyoshima | e8240ab | 2018-10-03 09:30:11 +0000 | [diff] [blame] | 20 | #include "base/thread_annotations.h" |
toyoshim@chromium.org | a97eebf | 2014-01-03 07:52:39 +0000 | [diff] [blame] | 21 | #include "base/threading/thread.h" |
agoode | f212b2a | 2015-03-19 12:53:23 -0700 | [diff] [blame] | 22 | #include "base/values.h" |
agoode | 7de413f | 2015-04-24 00:13:39 -0700 | [diff] [blame] | 23 | #include "device/udev_linux/scoped_udev.h" |
brettw | 49ff017 | 2015-05-05 12:43:04 -0700 | [diff] [blame] | 24 | #include "media/midi/midi_export.h" |
toyoshim@chromium.org | a97eebf | 2014-01-03 07:52:39 +0000 | [diff] [blame] | 25 | #include "media/midi/midi_manager.h" |
| 26 | |
toyoshim | e147c5e | 2015-05-07 21:58:31 -0700 | [diff] [blame] | 27 | namespace midi { |
toyoshim@chromium.org | a97eebf | 2014-01-03 07:52:39 +0000 | [diff] [blame] | 28 | |
brettw | 49ff017 | 2015-05-05 12:43:04 -0700 | [diff] [blame] | 29 | class MIDI_EXPORT MidiManagerAlsa final : public MidiManager { |
toyoshim@chromium.org | a97eebf | 2014-01-03 07:52:39 +0000 | [diff] [blame] | 30 | public: |
toyoshim | f4d6152 | 2017-02-10 02:03:32 -0800 | [diff] [blame] | 31 | explicit MidiManagerAlsa(MidiService* service); |
Peter Boström | 5363403 | 2021-09-22 20:24:34 +0000 | [diff] [blame] | 32 | |
| 33 | MidiManagerAlsa(const MidiManagerAlsa&) = delete; |
| 34 | MidiManagerAlsa& operator=(const MidiManagerAlsa&) = delete; |
| 35 | |
dcheng | 9e8524d | 2014-10-27 15:18:50 -0700 | [diff] [blame] | 36 | ~MidiManagerAlsa() override; |
toyoshim@chromium.org | a97eebf | 2014-01-03 07:52:39 +0000 | [diff] [blame] | 37 | |
toyoshim@chromium.org | c82e66e | 2014-02-04 07:05:47 +0000 | [diff] [blame] | 38 | // MidiManager implementation. |
dcheng | 9e8524d | 2014-10-27 15:18:50 -0700 | [diff] [blame] | 39 | void StartInitialization() override; |
| 40 | void DispatchSendMidiData(MidiManagerClient* client, |
Avi Drissman | 3528fd0 | 2015-12-18 20:11:31 -0500 | [diff] [blame] | 41 | uint32_t port_index, |
| 42 | const std::vector<uint8_t>& data, |
tzik | 925e2c6 | 2018-02-02 07:39:45 +0000 | [diff] [blame] | 43 | base::TimeTicks timestamp) override; |
toyoshim@chromium.org | a97eebf | 2014-01-03 07:52:39 +0000 | [diff] [blame] | 44 | |
| 45 | private: |
agoode | 99d6329 | 2015-04-13 08:39:25 -0700 | [diff] [blame] | 46 | friend class MidiManagerAlsaTest; |
agoode | 55a8b52 | 2015-03-08 12:40:17 -0700 | [diff] [blame] | 47 | FRIEND_TEST_ALL_PREFIXES(MidiManagerAlsaTest, ExtractManufacturer); |
agoode | 99d6329 | 2015-04-13 08:39:25 -0700 | [diff] [blame] | 48 | FRIEND_TEST_ALL_PREFIXES(MidiManagerAlsaTest, ToMidiPortState); |
agoode | 55a8b52 | 2015-03-08 12:40:17 -0700 | [diff] [blame] | 49 | |
agoode | b09423b | 2015-05-11 11:39:57 -0700 | [diff] [blame] | 50 | class AlsaCard; |
danakj | 75afea0 | 2016-04-25 20:36:04 -0700 | [diff] [blame] | 51 | using AlsaCardMap = std::map<int, std::unique_ptr<AlsaCard>>; |
agoode | b09423b | 2015-05-11 11:39:57 -0700 | [diff] [blame] | 52 | |
agoode | 99d6329 | 2015-04-13 08:39:25 -0700 | [diff] [blame] | 53 | class MidiPort { |
agoode | f212b2a | 2015-03-19 12:53:23 -0700 | [diff] [blame] | 54 | public: |
| 55 | enum class Type { kInput, kOutput }; |
| 56 | |
agoode | d87fc0f | 2015-05-21 08:29:31 -0700 | [diff] [blame] | 57 | // The Id class is used to keep the multiple strings separate |
| 58 | // but compare them all together for equality purposes. |
| 59 | // The individual strings that make up the Id can theoretically contain |
| 60 | // arbitrary characters, so unfortunately there is no simple way to |
| 61 | // concatenate them into a single string. |
| 62 | class Id final { |
| 63 | public: |
| 64 | Id(); |
| 65 | Id(const std::string& bus, |
| 66 | const std::string& vendor_id, |
| 67 | const std::string& model_id, |
| 68 | const std::string& usb_interface_num, |
| 69 | const std::string& serial); |
| 70 | Id(const Id&); |
| 71 | ~Id(); |
| 72 | bool operator==(const Id&) const; |
| 73 | bool empty() const; |
| 74 | |
| 75 | std::string bus() const { return bus_; } |
| 76 | std::string vendor_id() const { return vendor_id_; } |
| 77 | std::string model_id() const { return model_id_; } |
| 78 | std::string usb_interface_num() const { return usb_interface_num_; } |
| 79 | std::string serial() const { return serial_; } |
| 80 | |
| 81 | private: |
| 82 | std::string bus_; |
| 83 | std::string vendor_id_; |
| 84 | std::string model_id_; |
| 85 | std::string usb_interface_num_; |
| 86 | std::string serial_; |
| 87 | }; |
| 88 | |
agoode | 99d6329 | 2015-04-13 08:39:25 -0700 | [diff] [blame] | 89 | MidiPort(const std::string& path, |
agoode | d87fc0f | 2015-05-21 08:29:31 -0700 | [diff] [blame] | 90 | const Id& id, |
agoode | 99d6329 | 2015-04-13 08:39:25 -0700 | [diff] [blame] | 91 | int client_id, |
| 92 | int port_id, |
| 93 | int midi_device, |
| 94 | const std::string& client_name, |
| 95 | const std::string& port_name, |
| 96 | const std::string& manufacturer, |
| 97 | const std::string& version, |
| 98 | Type type); |
Peter Boström | 5e5c4fa | 2021-10-15 21:43:24 +0000 | [diff] [blame] | 99 | |
| 100 | MidiPort(const MidiPort&) = delete; |
| 101 | MidiPort& operator=(const MidiPort&) = delete; |
| 102 | |
agoode | 99d6329 | 2015-04-13 08:39:25 -0700 | [diff] [blame] | 103 | ~MidiPort(); |
agoode | f212b2a | 2015-03-19 12:53:23 -0700 | [diff] [blame] | 104 | |
| 105 | // Gets a Value representation of this object, suitable for serialization. |
danakj | 75afea0 | 2016-04-25 20:36:04 -0700 | [diff] [blame] | 106 | std::unique_ptr<base::Value> Value() const; |
agoode | f212b2a | 2015-03-19 12:53:23 -0700 | [diff] [blame] | 107 | |
| 108 | // Gets a string version of Value in JSON format. |
| 109 | std::string JSONValue() const; |
| 110 | |
| 111 | // Gets an opaque identifier for this object, suitable for using as the id |
| 112 | // field in MidiPort.id on the web. Note that this string does not store |
| 113 | // the full state. |
| 114 | std::string OpaqueKey() const; |
| 115 | |
agoode | 99d6329 | 2015-04-13 08:39:25 -0700 | [diff] [blame] | 116 | // Checks for equality for connected ports. |
| 117 | bool MatchConnected(const MidiPort& query) const; |
| 118 | // Checks for equality for kernel cards with id, pass 1. |
| 119 | bool MatchCardPass1(const MidiPort& query) const; |
| 120 | // Checks for equality for kernel cards with id, pass 2. |
| 121 | bool MatchCardPass2(const MidiPort& query) const; |
| 122 | // Checks for equality for non-card clients, pass 1. |
| 123 | bool MatchNoCardPass1(const MidiPort& query) const; |
| 124 | // Checks for equality for non-card clients, pass 2. |
| 125 | bool MatchNoCardPass2(const MidiPort& query) const; |
agoode | f212b2a | 2015-03-19 12:53:23 -0700 | [diff] [blame] | 126 | |
agoode | 99d6329 | 2015-04-13 08:39:25 -0700 | [diff] [blame] | 127 | // accessors |
| 128 | std::string path() const { return path_; } |
agoode | d87fc0f | 2015-05-21 08:29:31 -0700 | [diff] [blame] | 129 | Id id() const { return id_; } |
agoode | 99d6329 | 2015-04-13 08:39:25 -0700 | [diff] [blame] | 130 | std::string client_name() const { return client_name_; } |
| 131 | std::string port_name() const { return port_name_; } |
| 132 | std::string manufacturer() const { return manufacturer_; } |
| 133 | std::string version() const { return version_; } |
| 134 | int client_id() const { return client_id_; } |
| 135 | int port_id() const { return port_id_; } |
| 136 | int midi_device() const { return midi_device_; } |
| 137 | Type type() const { return type_; } |
Avi Drissman | 3528fd0 | 2015-12-18 20:11:31 -0500 | [diff] [blame] | 138 | uint32_t web_port_index() const { return web_port_index_; } |
agoode | 99d6329 | 2015-04-13 08:39:25 -0700 | [diff] [blame] | 139 | bool connected() const { return connected_; } |
| 140 | |
| 141 | // mutators |
Avi Drissman | 3528fd0 | 2015-12-18 20:11:31 -0500 | [diff] [blame] | 142 | void set_web_port_index(uint32_t web_port_index) { |
agoode | 99d6329 | 2015-04-13 08:39:25 -0700 | [diff] [blame] | 143 | web_port_index_ = web_port_index; |
| 144 | } |
| 145 | void set_connected(bool connected) { connected_ = connected; } |
| 146 | void Update(const std::string& path, |
| 147 | int client_id, |
| 148 | int port_id, |
| 149 | const std::string& client_name, |
| 150 | const std::string& port_name, |
| 151 | const std::string& manufacturer, |
| 152 | const std::string& version) { |
| 153 | path_ = path; |
| 154 | client_id_ = client_id; |
| 155 | port_id_ = port_id; |
| 156 | client_name_ = client_name; |
| 157 | port_name_ = port_name; |
| 158 | manufacturer_ = manufacturer; |
| 159 | version_ = version; |
| 160 | } |
| 161 | |
| 162 | private: |
| 163 | // Immutable properties. |
agoode | d87fc0f | 2015-05-21 08:29:31 -0700 | [diff] [blame] | 164 | const Id id_; |
agoode | 99d6329 | 2015-04-13 08:39:25 -0700 | [diff] [blame] | 165 | const int midi_device_; |
| 166 | |
agoode | f212b2a | 2015-03-19 12:53:23 -0700 | [diff] [blame] | 167 | const Type type_; |
| 168 | |
agoode | 99d6329 | 2015-04-13 08:39:25 -0700 | [diff] [blame] | 169 | // Mutable properties. These will get updated as ports move around or |
| 170 | // drivers change. |
| 171 | std::string path_; |
| 172 | int client_id_; |
| 173 | int port_id_; |
| 174 | std::string client_name_; |
| 175 | std::string port_name_; |
| 176 | std::string manufacturer_; |
| 177 | std::string version_; |
| 178 | |
| 179 | // Index for MidiManager. |
Avi Drissman | 3528fd0 | 2015-12-18 20:11:31 -0500 | [diff] [blame] | 180 | uint32_t web_port_index_ = 0; |
agoode | 99d6329 | 2015-04-13 08:39:25 -0700 | [diff] [blame] | 181 | |
| 182 | // Port is present in the ALSA system. |
agoode | 5a1aa11 | 2015-06-21 20:51:00 -0700 | [diff] [blame] | 183 | bool connected_ = true; |
agoode | 55a8b52 | 2015-03-08 12:40:17 -0700 | [diff] [blame] | 184 | }; |
| 185 | |
agoode | 99d6329 | 2015-04-13 08:39:25 -0700 | [diff] [blame] | 186 | class MidiPortStateBase { |
| 187 | public: |
danakj | 75afea0 | 2016-04-25 20:36:04 -0700 | [diff] [blame] | 188 | typedef std::vector<std::unique_ptr<MidiPort>>::iterator iterator; |
agoode | bd4be9b | 2015-03-16 19:17:25 -0700 | [diff] [blame] | 189 | |
Peter Boström | 5363403 | 2021-09-22 20:24:34 +0000 | [diff] [blame] | 190 | MidiPortStateBase(const MidiPortStateBase&) = delete; |
| 191 | MidiPortStateBase& operator=(const MidiPortStateBase&) = delete; |
| 192 | |
agoode | 99d6329 | 2015-04-13 08:39:25 -0700 | [diff] [blame] | 193 | virtual ~MidiPortStateBase(); |
| 194 | |
| 195 | // Given a port, finds a port in the internal store. |
| 196 | iterator Find(const MidiPort& port); |
| 197 | |
| 198 | // Given a port, finds a connected port, using exact matching. |
| 199 | iterator FindConnected(const MidiPort& port); |
| 200 | |
| 201 | // Given a port, finds a disconnected port, using heuristic matching. |
| 202 | iterator FindDisconnected(const MidiPort& port); |
| 203 | |
| 204 | iterator begin() { return ports_.begin(); } |
| 205 | iterator end() { return ports_.end(); } |
| 206 | |
| 207 | protected: |
| 208 | MidiPortStateBase(); |
agoode | 5ebc493 | 2015-12-01 08:25:12 -0800 | [diff] [blame] | 209 | iterator erase(iterator position) { return ports_.erase(position); } |
danakj | 75afea0 | 2016-04-25 20:36:04 -0700 | [diff] [blame] | 210 | void push_back(std::unique_ptr<MidiPort> port) { |
dcheng | c2aeece | 2015-12-27 00:54:00 -0800 | [diff] [blame] | 211 | ports_.push_back(std::move(port)); |
| 212 | } |
agoode | 99d6329 | 2015-04-13 08:39:25 -0700 | [diff] [blame] | 213 | |
| 214 | private: |
danakj | 75afea0 | 2016-04-25 20:36:04 -0700 | [diff] [blame] | 215 | std::vector<std::unique_ptr<MidiPort>> ports_; |
agoode | 99d6329 | 2015-04-13 08:39:25 -0700 | [diff] [blame] | 216 | }; |
| 217 | |
| 218 | class TemporaryMidiPortState final : public MidiPortStateBase { |
| 219 | public: |
agoode | 5ebc493 | 2015-12-01 08:25:12 -0800 | [diff] [blame] | 220 | iterator erase(iterator position) { |
| 221 | return MidiPortStateBase::erase(position); |
Nico Weber | a0faaf7 | 2019-02-07 19:07:54 +0000 | [diff] [blame] | 222 | } |
danakj | 75afea0 | 2016-04-25 20:36:04 -0700 | [diff] [blame] | 223 | void push_back(std::unique_ptr<MidiPort> port) { |
dcheng | c2aeece | 2015-12-27 00:54:00 -0800 | [diff] [blame] | 224 | MidiPortStateBase::push_back(std::move(port)); |
agoode | 5ebc493 | 2015-12-01 08:25:12 -0800 | [diff] [blame] | 225 | } |
agoode | 99d6329 | 2015-04-13 08:39:25 -0700 | [diff] [blame] | 226 | }; |
| 227 | |
| 228 | class MidiPortState final : public MidiPortStateBase { |
| 229 | public: |
| 230 | MidiPortState(); |
| 231 | |
agoode | 5ebc493 | 2015-12-01 08:25:12 -0800 | [diff] [blame] | 232 | // Inserts a port at the end. Returns web_port_index. |
danakj | 75afea0 | 2016-04-25 20:36:04 -0700 | [diff] [blame] | 233 | uint32_t push_back(std::unique_ptr<MidiPort> port); |
agoode | 99d6329 | 2015-04-13 08:39:25 -0700 | [diff] [blame] | 234 | |
| 235 | private: |
Avi Drissman | 3528fd0 | 2015-12-18 20:11:31 -0500 | [diff] [blame] | 236 | uint32_t num_input_ports_ = 0; |
| 237 | uint32_t num_output_ports_ = 0; |
agoode | 99d6329 | 2015-04-13 08:39:25 -0700 | [diff] [blame] | 238 | }; |
| 239 | |
| 240 | class AlsaSeqState { |
| 241 | public: |
| 242 | enum class PortDirection { kInput, kOutput, kDuplex }; |
| 243 | |
| 244 | AlsaSeqState(); |
Peter Boström | 5363403 | 2021-09-22 20:24:34 +0000 | [diff] [blame] | 245 | |
| 246 | AlsaSeqState(const AlsaSeqState&) = delete; |
| 247 | AlsaSeqState& operator=(const AlsaSeqState&) = delete; |
| 248 | |
agoode | 99d6329 | 2015-04-13 08:39:25 -0700 | [diff] [blame] | 249 | ~AlsaSeqState(); |
| 250 | |
| 251 | void ClientStart(int client_id, |
| 252 | const std::string& client_name, |
| 253 | snd_seq_client_type_t type); |
| 254 | bool ClientStarted(int client_id); |
| 255 | void ClientExit(int client_id); |
| 256 | void PortStart(int client_id, |
| 257 | int port_id, |
| 258 | const std::string& port_name, |
| 259 | PortDirection direction, |
| 260 | bool midi); |
| 261 | void PortExit(int client_id, int port_id); |
| 262 | snd_seq_client_type_t ClientType(int client_id) const; |
danakj | 75afea0 | 2016-04-25 20:36:04 -0700 | [diff] [blame] | 263 | std::unique_ptr<TemporaryMidiPortState> ToMidiPortState( |
agoode | b09423b | 2015-05-11 11:39:57 -0700 | [diff] [blame] | 264 | const AlsaCardMap& alsa_cards); |
| 265 | |
| 266 | int card_client_count() { return card_client_count_; } |
agoode | 99d6329 | 2015-04-13 08:39:25 -0700 | [diff] [blame] | 267 | |
| 268 | private: |
| 269 | class Port { |
| 270 | public: |
| 271 | Port(const std::string& name, PortDirection direction, bool midi); |
Peter Boström | 5363403 | 2021-09-22 20:24:34 +0000 | [diff] [blame] | 272 | |
| 273 | Port(const Port&) = delete; |
| 274 | Port& operator=(const Port&) = delete; |
| 275 | |
agoode | 99d6329 | 2015-04-13 08:39:25 -0700 | [diff] [blame] | 276 | ~Port(); |
| 277 | |
agoode | b058287 | 2015-05-20 05:22:24 -0700 | [diff] [blame] | 278 | std::string name() const { return name_; } |
| 279 | PortDirection direction() const { return direction_; } |
agoode | 99d6329 | 2015-04-13 08:39:25 -0700 | [diff] [blame] | 280 | // True if this port is a MIDI port, instead of another kind of ALSA port. |
agoode | b058287 | 2015-05-20 05:22:24 -0700 | [diff] [blame] | 281 | bool midi() const { return midi_; } |
agoode | 99d6329 | 2015-04-13 08:39:25 -0700 | [diff] [blame] | 282 | |
| 283 | private: |
| 284 | const std::string name_; |
| 285 | const PortDirection direction_; |
| 286 | const bool midi_; |
agoode | 99d6329 | 2015-04-13 08:39:25 -0700 | [diff] [blame] | 287 | }; |
| 288 | |
| 289 | class Client { |
| 290 | public: |
danakj | 75afea0 | 2016-04-25 20:36:04 -0700 | [diff] [blame] | 291 | using PortMap = std::map<int, std::unique_ptr<Port>>; |
agoode | 99d6329 | 2015-04-13 08:39:25 -0700 | [diff] [blame] | 292 | |
| 293 | Client(const std::string& name, snd_seq_client_type_t type); |
Peter Boström | 5363403 | 2021-09-22 20:24:34 +0000 | [diff] [blame] | 294 | |
| 295 | Client(const Client&) = delete; |
| 296 | Client& operator=(const Client&) = delete; |
| 297 | |
agoode | 99d6329 | 2015-04-13 08:39:25 -0700 | [diff] [blame] | 298 | ~Client(); |
| 299 | |
agoode | b058287 | 2015-05-20 05:22:24 -0700 | [diff] [blame] | 300 | std::string name() const { return name_; } |
| 301 | snd_seq_client_type_t type() const { return type_; } |
danakj | 75afea0 | 2016-04-25 20:36:04 -0700 | [diff] [blame] | 302 | void AddPort(int addr, std::unique_ptr<Port> port); |
agoode | 99d6329 | 2015-04-13 08:39:25 -0700 | [diff] [blame] | 303 | void RemovePort(int addr); |
| 304 | PortMap::const_iterator begin() const; |
| 305 | PortMap::const_iterator end() const; |
| 306 | |
| 307 | private: |
| 308 | const std::string name_; |
| 309 | const snd_seq_client_type_t type_; |
| 310 | PortMap ports_; |
agoode | 99d6329 | 2015-04-13 08:39:25 -0700 | [diff] [blame] | 311 | }; |
| 312 | |
danakj | 75afea0 | 2016-04-25 20:36:04 -0700 | [diff] [blame] | 313 | std::map<int, std::unique_ptr<Client>> clients_; |
agoode | 99d6329 | 2015-04-13 08:39:25 -0700 | [diff] [blame] | 314 | |
agoode | b09423b | 2015-05-11 11:39:57 -0700 | [diff] [blame] | 315 | // This is the current number of clients we know about that have |
| 316 | // cards. When this number matches alsa_card_midi_count_, we know |
| 317 | // we are in sync between ALSA and udev. Until then, we cannot generate |
| 318 | // MIDIConnectionEvents to web clients. |
agoode | df1b9ff | 2015-06-25 18:14:50 -0700 | [diff] [blame] | 319 | int card_client_count_ = 0; |
agoode | 99d6329 | 2015-04-13 08:39:25 -0700 | [diff] [blame] | 320 | }; |
| 321 | |
agoode | b09423b | 2015-05-11 11:39:57 -0700 | [diff] [blame] | 322 | class AlsaCard { |
| 323 | public: |
| 324 | AlsaCard(udev_device* dev, |
agoode | d87fc0f | 2015-05-21 08:29:31 -0700 | [diff] [blame] | 325 | const std::string& name, |
| 326 | const std::string& longname, |
| 327 | const std::string& driver, |
agoode | b09423b | 2015-05-11 11:39:57 -0700 | [diff] [blame] | 328 | int midi_device_count); |
Peter Boström | 5363403 | 2021-09-22 20:24:34 +0000 | [diff] [blame] | 329 | |
| 330 | AlsaCard(const AlsaCard&) = delete; |
| 331 | AlsaCard& operator=(const AlsaCard&) = delete; |
| 332 | |
agoode | b09423b | 2015-05-11 11:39:57 -0700 | [diff] [blame] | 333 | ~AlsaCard(); |
agoode | d87fc0f | 2015-05-21 08:29:31 -0700 | [diff] [blame] | 334 | std::string name() const { return name_; } |
| 335 | std::string longname() const { return longname_; } |
| 336 | std::string driver() const { return driver_; } |
| 337 | std::string path() const { return path_; } |
| 338 | std::string bus() const { return bus_; } |
| 339 | std::string vendor_id() const { return vendor_id_; } |
| 340 | std::string model_id() const { return model_id_; } |
| 341 | std::string usb_interface_num() const { return usb_interface_num_; } |
| 342 | std::string serial() const { return serial_; } |
agoode | b09423b | 2015-05-11 11:39:57 -0700 | [diff] [blame] | 343 | int midi_device_count() const { return midi_device_count_; } |
agoode | d87fc0f | 2015-05-21 08:29:31 -0700 | [diff] [blame] | 344 | std::string manufacturer() const { return manufacturer_; } |
agoode | b09423b | 2015-05-11 11:39:57 -0700 | [diff] [blame] | 345 | |
| 346 | private: |
| 347 | FRIEND_TEST_ALL_PREFIXES(MidiManagerAlsaTest, ExtractManufacturer); |
| 348 | |
| 349 | // Extracts the manufacturer using heuristics and a variety of sources. |
| 350 | static std::string ExtractManufacturerString( |
| 351 | const std::string& udev_id_vendor, |
| 352 | const std::string& udev_id_vendor_id, |
| 353 | const std::string& udev_id_vendor_from_database, |
agoode | d87fc0f | 2015-05-21 08:29:31 -0700 | [diff] [blame] | 354 | const std::string& name, |
| 355 | const std::string& longname); |
agoode | b09423b | 2015-05-11 11:39:57 -0700 | [diff] [blame] | 356 | |
agoode | d87fc0f | 2015-05-21 08:29:31 -0700 | [diff] [blame] | 357 | const std::string name_; |
| 358 | const std::string longname_; |
| 359 | const std::string driver_; |
| 360 | const std::string path_; |
| 361 | const std::string bus_; |
| 362 | const std::string vendor_id_; |
| 363 | const std::string model_id_; |
| 364 | const std::string usb_interface_num_; |
| 365 | const std::string serial_; |
| 366 | const int midi_device_count_; |
| 367 | const std::string manufacturer_; |
agoode | b09423b | 2015-05-11 11:39:57 -0700 | [diff] [blame] | 368 | }; |
| 369 | |
agoode | 5a1aa11 | 2015-06-21 20:51:00 -0700 | [diff] [blame] | 370 | struct SndSeqDeleter { |
| 371 | void operator()(snd_seq_t* seq) const { snd_seq_close(seq); } |
| 372 | }; |
| 373 | |
| 374 | struct SndMidiEventDeleter { |
| 375 | void operator()(snd_midi_event_t* coder) const { |
| 376 | snd_midi_event_free(coder); |
Nico Weber | a0faaf7 | 2019-02-07 19:07:54 +0000 | [diff] [blame] | 377 | } |
agoode | 5a1aa11 | 2015-06-21 20:51:00 -0700 | [diff] [blame] | 378 | }; |
| 379 | |
Takuto Ikuta | fd795cb | 2019-01-05 00:32:48 +0000 | [diff] [blame] | 380 | using SourceMap = std::unordered_map<int, uint32_t>; |
| 381 | using OutPortMap = std::unordered_map<uint32_t, int>; |
danakj | 75afea0 | 2016-04-25 20:36:04 -0700 | [diff] [blame] | 382 | using ScopedSndSeqPtr = std::unique_ptr<snd_seq_t, SndSeqDeleter>; |
agoode | b2ead82 | 2016-03-11 12:14:35 -0800 | [diff] [blame] | 383 | using ScopedSndMidiEventPtr = |
danakj | 75afea0 | 2016-04-25 20:36:04 -0700 | [diff] [blame] | 384 | std::unique_ptr<snd_midi_event_t, SndMidiEventDeleter>; |
agoode | 99d6329 | 2015-04-13 08:39:25 -0700 | [diff] [blame] | 385 | |
agoode@chromium.org | 2522751 | 2014-06-08 05:12:05 +0000 | [diff] [blame] | 386 | // An internal callback that runs on MidiSendThread. |
toyoshim | efcee5e | 2017-06-14 07:14:39 -0700 | [diff] [blame] | 387 | void SendMidiData(MidiManagerClient* client, |
toyoshim | f4d6152 | 2017-02-10 02:03:32 -0800 | [diff] [blame] | 388 | uint32_t port_index, |
| 389 | const std::vector<uint8_t>& data); |
agoode@chromium.org | 2522751 | 2014-06-08 05:12:05 +0000 | [diff] [blame] | 390 | |
toyoshim | efcee5e | 2017-06-14 07:14:39 -0700 | [diff] [blame] | 391 | void EventLoop(); |
tzik | 925e2c6 | 2018-02-02 07:39:45 +0000 | [diff] [blame] | 392 | void ProcessSingleEvent(snd_seq_event_t* event, base::TimeTicks timestamp); |
agoode | 99d6329 | 2015-04-13 08:39:25 -0700 | [diff] [blame] | 393 | void ProcessClientStartEvent(int client_id); |
| 394 | void ProcessPortStartEvent(const snd_seq_addr_t& addr); |
| 395 | void ProcessClientExitEvent(const snd_seq_addr_t& addr); |
| 396 | void ProcessPortExitEvent(const snd_seq_addr_t& addr); |
agoode | 975043d | 2015-05-11 00:46:17 -0700 | [diff] [blame] | 397 | void ProcessUdevEvent(udev_device* dev); |
agoode | b09423b | 2015-05-11 11:39:57 -0700 | [diff] [blame] | 398 | void AddCard(udev_device* dev); |
| 399 | void RemoveCard(int number); |
agoode | 99d6329 | 2015-04-13 08:39:25 -0700 | [diff] [blame] | 400 | |
| 401 | // Updates port_state_ and Web MIDI state from alsa_seq_state_. |
| 402 | void UpdatePortStateAndGenerateEvents(); |
| 403 | |
| 404 | // Enumerates ports. Call once after subscribing to the announce port. |
| 405 | void EnumerateAlsaPorts(); |
agoode | 975043d | 2015-05-11 00:46:17 -0700 | [diff] [blame] | 406 | // Enumerates udev cards. Call once after initializing the udev monitor. |
| 407 | bool EnumerateUdevCards(); |
agoode | 99d6329 | 2015-04-13 08:39:25 -0700 | [diff] [blame] | 408 | // Returns true if successful. |
Avi Drissman | 3528fd0 | 2015-12-18 20:11:31 -0500 | [diff] [blame] | 409 | bool CreateAlsaOutputPort(uint32_t port_index, int client_id, int port_id); |
| 410 | void DeleteAlsaOutputPort(uint32_t port_index); |
agoode | 99d6329 | 2015-04-13 08:39:25 -0700 | [diff] [blame] | 411 | // Returns true if successful. |
Avi Drissman | 3528fd0 | 2015-12-18 20:11:31 -0500 | [diff] [blame] | 412 | bool Subscribe(uint32_t port_index, int client_id, int port_id); |
agoode | 99d6329 | 2015-04-13 08:39:25 -0700 | [diff] [blame] | 413 | |
Takashi Toyoshima | 0ae4970 | 2017-09-01 04:59:51 +0000 | [diff] [blame] | 414 | // Allocates new snd_midi_event_t instance and wraps it to return as |
| 415 | // ScopedSndMidiEventPtr. |
| 416 | ScopedSndMidiEventPtr CreateScopedSndMidiEventPtr(size_t size); |
| 417 | |
agoode | b2ead82 | 2016-03-11 12:14:35 -0800 | [diff] [blame] | 418 | // Members initialized in the constructor are below. |
| 419 | // Our copies of the internal state of the ports of seq and udev. |
agoode | 99d6329 | 2015-04-13 08:39:25 -0700 | [diff] [blame] | 420 | AlsaSeqState alsa_seq_state_; |
| 421 | MidiPortState port_state_; |
toyoshim@chromium.org | 4a8657c | 2014-02-06 11:23:09 +0000 | [diff] [blame] | 422 | |
agoode@chromium.org | 2522751 | 2014-06-08 05:12:05 +0000 | [diff] [blame] | 423 | // One input port, many output ports. |
Takashi Toyoshima | e8240ab | 2018-10-03 09:30:11 +0000 | [diff] [blame] | 424 | base::Lock out_ports_lock_; |
| 425 | OutPortMap out_ports_ GUARDED_BY(out_ports_lock_); |
agoode@chromium.org | 2522751 | 2014-06-08 05:12:05 +0000 | [diff] [blame] | 426 | |
agoode | bd4be9b | 2015-03-16 19:17:25 -0700 | [diff] [blame] | 427 | // Mapping from ALSA client:port to our index. |
agoode@chromium.org | 2522751 | 2014-06-08 05:12:05 +0000 | [diff] [blame] | 428 | SourceMap source_map_; |
| 429 | |
agoode | b09423b | 2015-05-11 11:39:57 -0700 | [diff] [blame] | 430 | // Mapping from card to devices. |
| 431 | AlsaCardMap alsa_cards_; |
agoode | b09423b | 2015-05-11 11:39:57 -0700 | [diff] [blame] | 432 | |
| 433 | // This is the current count of midi devices across all cards we know |
| 434 | // about. When this number matches card_client_count_ in AlsaSeqState, |
| 435 | // we are safe to generate MIDIConnectionEvents. Otherwise we need to |
| 436 | // wait for our information from ALSA and udev to get back in sync. |
agoode | 5a1aa11 | 2015-06-21 20:51:00 -0700 | [diff] [blame] | 437 | int alsa_card_midi_count_ = 0; |
agoode | b09423b | 2015-05-11 11:39:57 -0700 | [diff] [blame] | 438 | |
agoode | b2ead82 | 2016-03-11 12:14:35 -0800 | [diff] [blame] | 439 | // ALSA seq handles and ids. |
| 440 | ScopedSndSeqPtr in_client_; |
| 441 | int in_client_id_; |
Takashi Toyoshima | e8240ab | 2018-10-03 09:30:11 +0000 | [diff] [blame] | 442 | ScopedSndSeqPtr out_client_ GUARDED_BY(out_client_lock_); |
| 443 | base::Lock out_client_lock_; |
agoode | b2ead82 | 2016-03-11 12:14:35 -0800 | [diff] [blame] | 444 | int out_client_id_; |
| 445 | int in_port_id_; |
| 446 | |
agoode | 99d6329 | 2015-04-13 08:39:25 -0700 | [diff] [blame] | 447 | // ALSA event -> MIDI coder. |
agoode | b2ead82 | 2016-03-11 12:14:35 -0800 | [diff] [blame] | 448 | ScopedSndMidiEventPtr decoder_; |
agoode@chromium.org | 2522751 | 2014-06-08 05:12:05 +0000 | [diff] [blame] | 449 | |
agoode | 7de413f | 2015-04-24 00:13:39 -0700 | [diff] [blame] | 450 | // udev, for querying hardware devices. |
| 451 | device::ScopedUdevPtr udev_; |
agoode | 975043d | 2015-05-11 00:46:17 -0700 | [diff] [blame] | 452 | device::ScopedUdevMonitorPtr udev_monitor_; |
toyoshim@chromium.org | a97eebf | 2014-01-03 07:52:39 +0000 | [diff] [blame] | 453 | }; |
| 454 | |
toyoshim | e147c5e | 2015-05-07 21:58:31 -0700 | [diff] [blame] | 455 | } // namespace midi |
toyoshim@chromium.org | a97eebf | 2014-01-03 07:52:39 +0000 | [diff] [blame] | 456 | |
dnicoara@chromium.org | 9f2a6f0 | 2014-01-03 21:25:00 +0000 | [diff] [blame] | 457 | #endif // MEDIA_MIDI_MIDI_MANAGER_ALSA_H_ |