blob: a84d5f084aed63d3455df3304736ae3301ee7222 [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#ifndef MEDIA_MIDI_MIDI_MANAGER_ALSA_H_
6#define MEDIA_MIDI_MIDI_MANAGER_ALSA_H_
toyoshim@chromium.orga97eebf2014-01-03 07:52:39 +00007
agoode@chromium.org25227512014-06-08 05:12:05 +00008#include <alsa/asoundlib.h>
avi793390d2015-12-22 22:22:36 -08009#include <stdint.h>
limasdfe59d0392015-11-19 20:28:57 -080010#include <map>
dchengc2aeece2015-12-27 00:54:00 -080011#include <utility>
toyoshim@chromium.orga97eebf2014-01-03 07:52:39 +000012#include <vector>
13
agoode55a8b522015-03-08 12:40:17 -070014#include "base/gtest_prod_util.h"
avieea95e82015-12-18 20:27:08 -080015#include "base/macros.h"
toyoshim@chromium.orga97eebf2014-01-03 07:52:39 +000016#include "base/memory/scoped_ptr.h"
agoode@chromium.org25227512014-06-08 05:12:05 +000017#include "base/synchronization/lock.h"
toyoshim@chromium.orga97eebf2014-01-03 07:52:39 +000018#include "base/threading/thread.h"
agoodef212b2a2015-03-19 12:53:23 -070019#include "base/values.h"
agoode7de413f2015-04-24 00:13:39 -070020#include "device/udev_linux/scoped_udev.h"
brettw49ff0172015-05-05 12:43:04 -070021#include "media/midi/midi_export.h"
toyoshim@chromium.orga97eebf2014-01-03 07:52:39 +000022#include "media/midi/midi_manager.h"
23
24namespace media {
toyoshime147c5e2015-05-07 21:58:31 -070025namespace midi {
toyoshim@chromium.orga97eebf2014-01-03 07:52:39 +000026
brettw49ff0172015-05-05 12:43:04 -070027class MIDI_EXPORT MidiManagerAlsa final : public MidiManager {
toyoshim@chromium.orga97eebf2014-01-03 07:52:39 +000028 public:
toyoshim@chromium.orgc82e66e2014-02-04 07:05:47 +000029 MidiManagerAlsa();
dcheng9e8524d2014-10-27 15:18:50 -070030 ~MidiManagerAlsa() override;
toyoshim@chromium.orga97eebf2014-01-03 07:52:39 +000031
toyoshim@chromium.orgc82e66e2014-02-04 07:05:47 +000032 // MidiManager implementation.
dcheng9e8524d2014-10-27 15:18:50 -070033 void StartInitialization() override;
toyoshim8e7d6e02015-10-06 08:47:17 -070034 void Finalize() override;
dcheng9e8524d2014-10-27 15:18:50 -070035 void DispatchSendMidiData(MidiManagerClient* client,
Avi Drissman3528fd02015-12-18 20:11:31 -050036 uint32_t port_index,
37 const std::vector<uint8_t>& data,
dcheng9e8524d2014-10-27 15:18:50 -070038 double timestamp) override;
toyoshim@chromium.orga97eebf2014-01-03 07:52:39 +000039
40 private:
agoode99d63292015-04-13 08:39:25 -070041 friend class MidiManagerAlsaTest;
agoode55a8b522015-03-08 12:40:17 -070042 FRIEND_TEST_ALL_PREFIXES(MidiManagerAlsaTest, ExtractManufacturer);
agoode99d63292015-04-13 08:39:25 -070043 FRIEND_TEST_ALL_PREFIXES(MidiManagerAlsaTest, ToMidiPortState);
agoode55a8b522015-03-08 12:40:17 -070044
agoodeb09423b2015-05-11 11:39:57 -070045 class AlsaCard;
limasdfe59d0392015-11-19 20:28:57 -080046 using AlsaCardMap = std::map<int, scoped_ptr<AlsaCard>>;
agoodeb09423b2015-05-11 11:39:57 -070047
agoode99d63292015-04-13 08:39:25 -070048 class MidiPort {
agoodef212b2a2015-03-19 12:53:23 -070049 public:
50 enum class Type { kInput, kOutput };
51
agooded87fc0f2015-05-21 08:29:31 -070052 // The Id class is used to keep the multiple strings separate
53 // but compare them all together for equality purposes.
54 // The individual strings that make up the Id can theoretically contain
55 // arbitrary characters, so unfortunately there is no simple way to
56 // concatenate them into a single string.
57 class Id final {
58 public:
59 Id();
60 Id(const std::string& bus,
61 const std::string& vendor_id,
62 const std::string& model_id,
63 const std::string& usb_interface_num,
64 const std::string& serial);
65 Id(const Id&);
66 ~Id();
67 bool operator==(const Id&) const;
68 bool empty() const;
69
70 std::string bus() const { return bus_; }
71 std::string vendor_id() const { return vendor_id_; }
72 std::string model_id() const { return model_id_; }
73 std::string usb_interface_num() const { return usb_interface_num_; }
74 std::string serial() const { return serial_; }
75
76 private:
77 std::string bus_;
78 std::string vendor_id_;
79 std::string model_id_;
80 std::string usb_interface_num_;
81 std::string serial_;
82 };
83
agoode99d63292015-04-13 08:39:25 -070084 MidiPort(const std::string& path,
agooded87fc0f2015-05-21 08:29:31 -070085 const Id& id,
agoode99d63292015-04-13 08:39:25 -070086 int client_id,
87 int port_id,
88 int midi_device,
89 const std::string& client_name,
90 const std::string& port_name,
91 const std::string& manufacturer,
92 const std::string& version,
93 Type type);
94 ~MidiPort();
agoodef212b2a2015-03-19 12:53:23 -070095
96 // Gets a Value representation of this object, suitable for serialization.
97 scoped_ptr<base::Value> Value() const;
98
99 // Gets a string version of Value in JSON format.
100 std::string JSONValue() const;
101
102 // Gets an opaque identifier for this object, suitable for using as the id
103 // field in MidiPort.id on the web. Note that this string does not store
104 // the full state.
105 std::string OpaqueKey() const;
106
agoode99d63292015-04-13 08:39:25 -0700107 // Checks for equality for connected ports.
108 bool MatchConnected(const MidiPort& query) const;
109 // Checks for equality for kernel cards with id, pass 1.
110 bool MatchCardPass1(const MidiPort& query) const;
111 // Checks for equality for kernel cards with id, pass 2.
112 bool MatchCardPass2(const MidiPort& query) const;
113 // Checks for equality for non-card clients, pass 1.
114 bool MatchNoCardPass1(const MidiPort& query) const;
115 // Checks for equality for non-card clients, pass 2.
116 bool MatchNoCardPass2(const MidiPort& query) const;
agoodef212b2a2015-03-19 12:53:23 -0700117
agoode99d63292015-04-13 08:39:25 -0700118 // accessors
119 std::string path() const { return path_; }
agooded87fc0f2015-05-21 08:29:31 -0700120 Id id() const { return id_; }
agoode99d63292015-04-13 08:39:25 -0700121 std::string client_name() const { return client_name_; }
122 std::string port_name() const { return port_name_; }
123 std::string manufacturer() const { return manufacturer_; }
124 std::string version() const { return version_; }
125 int client_id() const { return client_id_; }
126 int port_id() const { return port_id_; }
127 int midi_device() const { return midi_device_; }
128 Type type() const { return type_; }
Avi Drissman3528fd02015-12-18 20:11:31 -0500129 uint32_t web_port_index() const { return web_port_index_; }
agoode99d63292015-04-13 08:39:25 -0700130 bool connected() const { return connected_; }
131
132 // mutators
Avi Drissman3528fd02015-12-18 20:11:31 -0500133 void set_web_port_index(uint32_t web_port_index) {
agoode99d63292015-04-13 08:39:25 -0700134 web_port_index_ = web_port_index;
135 }
136 void set_connected(bool connected) { connected_ = connected; }
137 void Update(const std::string& path,
138 int client_id,
139 int port_id,
140 const std::string& client_name,
141 const std::string& port_name,
142 const std::string& manufacturer,
143 const std::string& version) {
144 path_ = path;
145 client_id_ = client_id;
146 port_id_ = port_id;
147 client_name_ = client_name;
148 port_name_ = port_name;
149 manufacturer_ = manufacturer;
150 version_ = version;
151 }
152
153 private:
154 // Immutable properties.
agooded87fc0f2015-05-21 08:29:31 -0700155 const Id id_;
agoode99d63292015-04-13 08:39:25 -0700156 const int midi_device_;
157
agoodef212b2a2015-03-19 12:53:23 -0700158 const Type type_;
159
agoode99d63292015-04-13 08:39:25 -0700160 // Mutable properties. These will get updated as ports move around or
161 // drivers change.
162 std::string path_;
163 int client_id_;
164 int port_id_;
165 std::string client_name_;
166 std::string port_name_;
167 std::string manufacturer_;
168 std::string version_;
169
170 // Index for MidiManager.
Avi Drissman3528fd02015-12-18 20:11:31 -0500171 uint32_t web_port_index_ = 0;
agoode99d63292015-04-13 08:39:25 -0700172
173 // Port is present in the ALSA system.
agoode5a1aa112015-06-21 20:51:00 -0700174 bool connected_ = true;
agoode99d63292015-04-13 08:39:25 -0700175
176 DISALLOW_COPY_AND_ASSIGN(MidiPort);
agoode55a8b522015-03-08 12:40:17 -0700177 };
178
agoode99d63292015-04-13 08:39:25 -0700179 class MidiPortStateBase {
180 public:
agoode5ebc4932015-12-01 08:25:12 -0800181 typedef std::vector<scoped_ptr<MidiPort>>::iterator iterator;
agoodebd4be9b2015-03-16 19:17:25 -0700182
agoode99d63292015-04-13 08:39:25 -0700183 virtual ~MidiPortStateBase();
184
185 // Given a port, finds a port in the internal store.
186 iterator Find(const MidiPort& port);
187
188 // Given a port, finds a connected port, using exact matching.
189 iterator FindConnected(const MidiPort& port);
190
191 // Given a port, finds a disconnected port, using heuristic matching.
192 iterator FindDisconnected(const MidiPort& port);
193
194 iterator begin() { return ports_.begin(); }
195 iterator end() { return ports_.end(); }
196
197 protected:
198 MidiPortStateBase();
agoode5ebc4932015-12-01 08:25:12 -0800199 iterator erase(iterator position) { return ports_.erase(position); }
dchengc2aeece2015-12-27 00:54:00 -0800200 void push_back(scoped_ptr<MidiPort> port) {
201 ports_.push_back(std::move(port));
202 }
agoode99d63292015-04-13 08:39:25 -0700203
204 private:
agoode5ebc4932015-12-01 08:25:12 -0800205 std::vector<scoped_ptr<MidiPort>> ports_;
agoode99d63292015-04-13 08:39:25 -0700206
207 DISALLOW_COPY_AND_ASSIGN(MidiPortStateBase);
208 };
209
210 class TemporaryMidiPortState final : public MidiPortStateBase {
211 public:
agoode5ebc4932015-12-01 08:25:12 -0800212 iterator erase(iterator position) {
213 return MidiPortStateBase::erase(position);
214 };
215 void push_back(scoped_ptr<MidiPort> port) {
dchengc2aeece2015-12-27 00:54:00 -0800216 MidiPortStateBase::push_back(std::move(port));
agoode5ebc4932015-12-01 08:25:12 -0800217 }
agoode99d63292015-04-13 08:39:25 -0700218 };
219
220 class MidiPortState final : public MidiPortStateBase {
221 public:
222 MidiPortState();
223
agoode5ebc4932015-12-01 08:25:12 -0800224 // Inserts a port at the end. Returns web_port_index.
Avi Drissman3528fd02015-12-18 20:11:31 -0500225 uint32_t push_back(scoped_ptr<MidiPort> port);
agoode99d63292015-04-13 08:39:25 -0700226
227 private:
Avi Drissman3528fd02015-12-18 20:11:31 -0500228 uint32_t num_input_ports_ = 0;
229 uint32_t num_output_ports_ = 0;
agoode99d63292015-04-13 08:39:25 -0700230 };
231
232 class AlsaSeqState {
233 public:
234 enum class PortDirection { kInput, kOutput, kDuplex };
235
236 AlsaSeqState();
237 ~AlsaSeqState();
238
239 void ClientStart(int client_id,
240 const std::string& client_name,
241 snd_seq_client_type_t type);
242 bool ClientStarted(int client_id);
243 void ClientExit(int client_id);
244 void PortStart(int client_id,
245 int port_id,
246 const std::string& port_name,
247 PortDirection direction,
248 bool midi);
249 void PortExit(int client_id, int port_id);
250 snd_seq_client_type_t ClientType(int client_id) const;
agoodeb09423b2015-05-11 11:39:57 -0700251 scoped_ptr<TemporaryMidiPortState> ToMidiPortState(
252 const AlsaCardMap& alsa_cards);
253
254 int card_client_count() { return card_client_count_; }
agoode99d63292015-04-13 08:39:25 -0700255
256 private:
257 class Port {
258 public:
259 Port(const std::string& name, PortDirection direction, bool midi);
260 ~Port();
261
agoodeb0582872015-05-20 05:22:24 -0700262 std::string name() const { return name_; }
263 PortDirection direction() const { return direction_; }
agoode99d63292015-04-13 08:39:25 -0700264 // True if this port is a MIDI port, instead of another kind of ALSA port.
agoodeb0582872015-05-20 05:22:24 -0700265 bool midi() const { return midi_; }
agoode99d63292015-04-13 08:39:25 -0700266
267 private:
268 const std::string name_;
269 const PortDirection direction_;
270 const bool midi_;
271
272 DISALLOW_COPY_AND_ASSIGN(Port);
273 };
274
275 class Client {
276 public:
limasdfe59d0392015-11-19 20:28:57 -0800277 using PortMap = std::map<int, scoped_ptr<Port>>;
agoode99d63292015-04-13 08:39:25 -0700278
279 Client(const std::string& name, snd_seq_client_type_t type);
280 ~Client();
281
agoodeb0582872015-05-20 05:22:24 -0700282 std::string name() const { return name_; }
283 snd_seq_client_type_t type() const { return type_; }
agoode99d63292015-04-13 08:39:25 -0700284 void AddPort(int addr, scoped_ptr<Port> port);
285 void RemovePort(int addr);
286 PortMap::const_iterator begin() const;
287 PortMap::const_iterator end() const;
288
289 private:
290 const std::string name_;
291 const snd_seq_client_type_t type_;
292 PortMap ports_;
agoode99d63292015-04-13 08:39:25 -0700293
294 DISALLOW_COPY_AND_ASSIGN(Client);
295 };
296
limasdfe59d0392015-11-19 20:28:57 -0800297 std::map<int, scoped_ptr<Client>> clients_;
agoode99d63292015-04-13 08:39:25 -0700298
agoodeb09423b2015-05-11 11:39:57 -0700299 // This is the current number of clients we know about that have
300 // cards. When this number matches alsa_card_midi_count_, we know
301 // we are in sync between ALSA and udev. Until then, we cannot generate
302 // MIDIConnectionEvents to web clients.
agoodedf1b9ff2015-06-25 18:14:50 -0700303 int card_client_count_ = 0;
agoodeb09423b2015-05-11 11:39:57 -0700304
agoode99d63292015-04-13 08:39:25 -0700305 DISALLOW_COPY_AND_ASSIGN(AlsaSeqState);
306 };
307
agoodeb09423b2015-05-11 11:39:57 -0700308 class AlsaCard {
309 public:
310 AlsaCard(udev_device* dev,
agooded87fc0f2015-05-21 08:29:31 -0700311 const std::string& name,
312 const std::string& longname,
313 const std::string& driver,
agoodeb09423b2015-05-11 11:39:57 -0700314 int midi_device_count);
315 ~AlsaCard();
agooded87fc0f2015-05-21 08:29:31 -0700316 std::string name() const { return name_; }
317 std::string longname() const { return longname_; }
318 std::string driver() const { return driver_; }
319 std::string path() const { return path_; }
320 std::string bus() const { return bus_; }
321 std::string vendor_id() const { return vendor_id_; }
322 std::string model_id() const { return model_id_; }
323 std::string usb_interface_num() const { return usb_interface_num_; }
324 std::string serial() const { return serial_; }
agoodeb09423b2015-05-11 11:39:57 -0700325 int midi_device_count() const { return midi_device_count_; }
agooded87fc0f2015-05-21 08:29:31 -0700326 std::string manufacturer() const { return manufacturer_; }
agoodeb09423b2015-05-11 11:39:57 -0700327
328 private:
329 FRIEND_TEST_ALL_PREFIXES(MidiManagerAlsaTest, ExtractManufacturer);
330
331 // Extracts the manufacturer using heuristics and a variety of sources.
332 static std::string ExtractManufacturerString(
333 const std::string& udev_id_vendor,
334 const std::string& udev_id_vendor_id,
335 const std::string& udev_id_vendor_from_database,
agooded87fc0f2015-05-21 08:29:31 -0700336 const std::string& name,
337 const std::string& longname);
agoodeb09423b2015-05-11 11:39:57 -0700338
agooded87fc0f2015-05-21 08:29:31 -0700339 const std::string name_;
340 const std::string longname_;
341 const std::string driver_;
342 const std::string path_;
343 const std::string bus_;
344 const std::string vendor_id_;
345 const std::string model_id_;
346 const std::string usb_interface_num_;
347 const std::string serial_;
348 const int midi_device_count_;
349 const std::string manufacturer_;
agoodeb09423b2015-05-11 11:39:57 -0700350
351 DISALLOW_COPY_AND_ASSIGN(AlsaCard);
352 };
353
agoode5a1aa112015-06-21 20:51:00 -0700354 struct SndSeqDeleter {
355 void operator()(snd_seq_t* seq) const { snd_seq_close(seq); }
356 };
357
358 struct SndMidiEventDeleter {
359 void operator()(snd_midi_event_t* coder) const {
360 snd_midi_event_free(coder);
361 };
362 };
363
Avi Drissman3528fd02015-12-18 20:11:31 -0500364 typedef base::hash_map<int, uint32_t> SourceMap;
365 typedef base::hash_map<uint32_t, int> OutPortMap;
agoode99d63292015-04-13 08:39:25 -0700366
agoode@chromium.org25227512014-06-08 05:12:05 +0000367 // An internal callback that runs on MidiSendThread.
Avi Drissman3528fd02015-12-18 20:11:31 -0500368 void SendMidiData(uint32_t port_index, const std::vector<uint8_t>& data);
agoode@chromium.org25227512014-06-08 05:12:05 +0000369
agoodebd4be9b2015-03-16 19:17:25 -0700370 void ScheduleEventLoop();
toyoshim@chromium.org4a8657c2014-02-06 11:23:09 +0000371 void EventLoop();
agoodebd4be9b2015-03-16 19:17:25 -0700372 void ProcessSingleEvent(snd_seq_event_t* event, double timestamp);
agoode99d63292015-04-13 08:39:25 -0700373 void ProcessClientStartEvent(int client_id);
374 void ProcessPortStartEvent(const snd_seq_addr_t& addr);
375 void ProcessClientExitEvent(const snd_seq_addr_t& addr);
376 void ProcessPortExitEvent(const snd_seq_addr_t& addr);
agoode975043d2015-05-11 00:46:17 -0700377 void ProcessUdevEvent(udev_device* dev);
agoodeb09423b2015-05-11 11:39:57 -0700378 void AddCard(udev_device* dev);
379 void RemoveCard(int number);
agoode99d63292015-04-13 08:39:25 -0700380
381 // Updates port_state_ and Web MIDI state from alsa_seq_state_.
382 void UpdatePortStateAndGenerateEvents();
383
384 // Enumerates ports. Call once after subscribing to the announce port.
385 void EnumerateAlsaPorts();
agoode975043d2015-05-11 00:46:17 -0700386 // Enumerates udev cards. Call once after initializing the udev monitor.
387 bool EnumerateUdevCards();
agoode99d63292015-04-13 08:39:25 -0700388 // Returns true if successful.
Avi Drissman3528fd02015-12-18 20:11:31 -0500389 bool CreateAlsaOutputPort(uint32_t port_index, int client_id, int port_id);
390 void DeleteAlsaOutputPort(uint32_t port_index);
agoode99d63292015-04-13 08:39:25 -0700391 // Returns true if successful.
Avi Drissman3528fd02015-12-18 20:11:31 -0500392 bool Subscribe(uint32_t port_index, int client_id, int port_id);
agoode99d63292015-04-13 08:39:25 -0700393
394 AlsaSeqState alsa_seq_state_;
395 MidiPortState port_state_;
toyoshim@chromium.org4a8657c2014-02-06 11:23:09 +0000396
agoodebd4be9b2015-03-16 19:17:25 -0700397 // ALSA seq handles.
agoode5a1aa112015-06-21 20:51:00 -0700398 scoped_ptr<snd_seq_t, SndSeqDeleter> in_client_;
399 int in_client_id_ = -1;
400 scoped_ptr<snd_seq_t, SndSeqDeleter> out_client_;
401 int out_client_id_ = -1;
agoode@chromium.org25227512014-06-08 05:12:05 +0000402
403 // One input port, many output ports.
agoode5a1aa112015-06-21 20:51:00 -0700404 int in_port_id_ = -1;
agoode99d63292015-04-13 08:39:25 -0700405 OutPortMap out_ports_; // guarded by out_ports_lock_
406 base::Lock out_ports_lock_; // guards out_ports_
agoode@chromium.org25227512014-06-08 05:12:05 +0000407
agoodebd4be9b2015-03-16 19:17:25 -0700408 // Mapping from ALSA client:port to our index.
agoode@chromium.org25227512014-06-08 05:12:05 +0000409 SourceMap source_map_;
410
agoodeb09423b2015-05-11 11:39:57 -0700411 // Mapping from card to devices.
412 AlsaCardMap alsa_cards_;
agoodeb09423b2015-05-11 11:39:57 -0700413
414 // This is the current count of midi devices across all cards we know
415 // about. When this number matches card_client_count_ in AlsaSeqState,
416 // we are safe to generate MIDIConnectionEvents. Otherwise we need to
417 // wait for our information from ALSA and udev to get back in sync.
agoode5a1aa112015-06-21 20:51:00 -0700418 int alsa_card_midi_count_ = 0;
agoodeb09423b2015-05-11 11:39:57 -0700419
agoode99d63292015-04-13 08:39:25 -0700420 // ALSA event -> MIDI coder.
agoode5a1aa112015-06-21 20:51:00 -0700421 scoped_ptr<snd_midi_event_t, SndMidiEventDeleter> decoder_;
agoode@chromium.org25227512014-06-08 05:12:05 +0000422
agoode7de413f2015-04-24 00:13:39 -0700423 // udev, for querying hardware devices.
424 device::ScopedUdevPtr udev_;
agoode975043d2015-05-11 00:46:17 -0700425 device::ScopedUdevMonitorPtr udev_monitor_;
agoode7de413f2015-04-24 00:13:39 -0700426
toyoshim@chromium.orga97eebf2014-01-03 07:52:39 +0000427 base::Thread send_thread_;
toyoshim@chromium.org4a8657c2014-02-06 11:23:09 +0000428 base::Thread event_thread_;
429
agoode5a1aa112015-06-21 20:51:00 -0700430 bool event_thread_shutdown_ = false; // guarded by shutdown_lock_
431 base::Lock shutdown_lock_; // guards event_thread_shutdown_
toyoshim@chromium.org4a8657c2014-02-06 11:23:09 +0000432
toyoshim@chromium.orgc82e66e2014-02-04 07:05:47 +0000433 DISALLOW_COPY_AND_ASSIGN(MidiManagerAlsa);
toyoshim@chromium.orga97eebf2014-01-03 07:52:39 +0000434};
435
toyoshime147c5e2015-05-07 21:58:31 -0700436} // namespace midi
toyoshim@chromium.orga97eebf2014-01-03 07:52:39 +0000437} // namespace media
438
dnicoara@chromium.org9f2a6f02014-01-03 21:25:00 +0000439#endif // MEDIA_MIDI_MIDI_MANAGER_ALSA_H_