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