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