blob: eccd543144aa0432316432805d87fd589930551c [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>
toyoshim@chromium.orga97eebf2014-01-03 07:52:39 +000011#include <vector>
12
agoode55a8b522015-03-08 12:40:17 -070013#include "base/gtest_prod_util.h"
avieea95e82015-12-18 20:27:08 -080014#include "base/macros.h"
toyoshim@chromium.orga97eebf2014-01-03 07:52:39 +000015#include "base/memory/scoped_ptr.h"
agoode@chromium.org25227512014-06-08 05:12:05 +000016#include "base/synchronization/lock.h"
toyoshim@chromium.orga97eebf2014-01-03 07:52:39 +000017#include "base/threading/thread.h"
agoodef212b2a2015-03-19 12:53:23 -070018#include "base/values.h"
agoode7de413f2015-04-24 00:13:39 -070019#include "device/udev_linux/scoped_udev.h"
brettw49ff0172015-05-05 12:43:04 -070020#include "media/midi/midi_export.h"
toyoshim@chromium.orga97eebf2014-01-03 07:52:39 +000021#include "media/midi/midi_manager.h"
22
23namespace media {
toyoshime147c5e2015-05-07 21:58:31 -070024namespace midi {
toyoshim@chromium.orga97eebf2014-01-03 07:52:39 +000025
brettw49ff0172015-05-05 12:43:04 -070026class MIDI_EXPORT MidiManagerAlsa final : public MidiManager {
toyoshim@chromium.orga97eebf2014-01-03 07:52:39 +000027 public:
toyoshim@chromium.orgc82e66e2014-02-04 07:05:47 +000028 MidiManagerAlsa();
dcheng9e8524d2014-10-27 15:18:50 -070029 ~MidiManagerAlsa() override;
toyoshim@chromium.orga97eebf2014-01-03 07:52:39 +000030
toyoshim@chromium.orgc82e66e2014-02-04 07:05:47 +000031 // MidiManager implementation.
dcheng9e8524d2014-10-27 15:18:50 -070032 void StartInitialization() override;
toyoshim8e7d6e02015-10-06 08:47:17 -070033 void Finalize() override;
dcheng9e8524d2014-10-27 15:18:50 -070034 void DispatchSendMidiData(MidiManagerClient* client,
Avi Drissman3528fd02015-12-18 20:11:31 -050035 uint32_t port_index,
36 const std::vector<uint8_t>& data,
dcheng9e8524d2014-10-27 15:18:50 -070037 double timestamp) override;
toyoshim@chromium.orga97eebf2014-01-03 07:52:39 +000038
39 private:
agoode99d63292015-04-13 08:39:25 -070040 friend class MidiManagerAlsaTest;
agoode55a8b522015-03-08 12:40:17 -070041 FRIEND_TEST_ALL_PREFIXES(MidiManagerAlsaTest, ExtractManufacturer);
agoode99d63292015-04-13 08:39:25 -070042 FRIEND_TEST_ALL_PREFIXES(MidiManagerAlsaTest, ToMidiPortState);
agoode55a8b522015-03-08 12:40:17 -070043
agoodeb09423b2015-05-11 11:39:57 -070044 class AlsaCard;
limasdfe59d0392015-11-19 20:28:57 -080045 using AlsaCardMap = std::map<int, scoped_ptr<AlsaCard>>;
agoodeb09423b2015-05-11 11:39:57 -070046
agoode99d63292015-04-13 08:39:25 -070047 class MidiPort {
agoodef212b2a2015-03-19 12:53:23 -070048 public:
49 enum class Type { kInput, kOutput };
50
agooded87fc0f2015-05-21 08:29:31 -070051 // The Id class is used to keep the multiple strings separate
52 // but compare them all together for equality purposes.
53 // The individual strings that make up the Id can theoretically contain
54 // arbitrary characters, so unfortunately there is no simple way to
55 // concatenate them into a single string.
56 class Id final {
57 public:
58 Id();
59 Id(const std::string& bus,
60 const std::string& vendor_id,
61 const std::string& model_id,
62 const std::string& usb_interface_num,
63 const std::string& serial);
64 Id(const Id&);
65 ~Id();
66 bool operator==(const Id&) const;
67 bool empty() const;
68
69 std::string bus() const { return bus_; }
70 std::string vendor_id() const { return vendor_id_; }
71 std::string model_id() const { return model_id_; }
72 std::string usb_interface_num() const { return usb_interface_num_; }
73 std::string serial() const { return serial_; }
74
75 private:
76 std::string bus_;
77 std::string vendor_id_;
78 std::string model_id_;
79 std::string usb_interface_num_;
80 std::string serial_;
81 };
82
agoode99d63292015-04-13 08:39:25 -070083 MidiPort(const std::string& path,
agooded87fc0f2015-05-21 08:29:31 -070084 const Id& id,
agoode99d63292015-04-13 08:39:25 -070085 int client_id,
86 int port_id,
87 int midi_device,
88 const std::string& client_name,
89 const std::string& port_name,
90 const std::string& manufacturer,
91 const std::string& version,
92 Type type);
93 ~MidiPort();
agoodef212b2a2015-03-19 12:53:23 -070094
95 // Gets a Value representation of this object, suitable for serialization.
96 scoped_ptr<base::Value> Value() const;
97
98 // Gets a string version of Value in JSON format.
99 std::string JSONValue() const;
100
101 // Gets an opaque identifier for this object, suitable for using as the id
102 // field in MidiPort.id on the web. Note that this string does not store
103 // the full state.
104 std::string OpaqueKey() const;
105
agoode99d63292015-04-13 08:39:25 -0700106 // Checks for equality for connected ports.
107 bool MatchConnected(const MidiPort& query) const;
108 // Checks for equality for kernel cards with id, pass 1.
109 bool MatchCardPass1(const MidiPort& query) const;
110 // Checks for equality for kernel cards with id, pass 2.
111 bool MatchCardPass2(const MidiPort& query) const;
112 // Checks for equality for non-card clients, pass 1.
113 bool MatchNoCardPass1(const MidiPort& query) const;
114 // Checks for equality for non-card clients, pass 2.
115 bool MatchNoCardPass2(const MidiPort& query) const;
agoodef212b2a2015-03-19 12:53:23 -0700116
agoode99d63292015-04-13 08:39:25 -0700117 // accessors
118 std::string path() const { return path_; }
agooded87fc0f2015-05-21 08:29:31 -0700119 Id id() const { return id_; }
agoode99d63292015-04-13 08:39:25 -0700120 std::string client_name() const { return client_name_; }
121 std::string port_name() const { return port_name_; }
122 std::string manufacturer() const { return manufacturer_; }
123 std::string version() const { return version_; }
124 int client_id() const { return client_id_; }
125 int port_id() const { return port_id_; }
126 int midi_device() const { return midi_device_; }
127 Type type() const { return type_; }
Avi Drissman3528fd02015-12-18 20:11:31 -0500128 uint32_t web_port_index() const { return web_port_index_; }
agoode99d63292015-04-13 08:39:25 -0700129 bool connected() const { return connected_; }
130
131 // mutators
Avi Drissman3528fd02015-12-18 20:11:31 -0500132 void set_web_port_index(uint32_t web_port_index) {
agoode99d63292015-04-13 08:39:25 -0700133 web_port_index_ = web_port_index;
134 }
135 void set_connected(bool connected) { connected_ = connected; }
136 void Update(const std::string& path,
137 int client_id,
138 int port_id,
139 const std::string& client_name,
140 const std::string& port_name,
141 const std::string& manufacturer,
142 const std::string& version) {
143 path_ = path;
144 client_id_ = client_id;
145 port_id_ = port_id;
146 client_name_ = client_name;
147 port_name_ = port_name;
148 manufacturer_ = manufacturer;
149 version_ = version;
150 }
151
152 private:
153 // Immutable properties.
agooded87fc0f2015-05-21 08:29:31 -0700154 const Id id_;
agoode99d63292015-04-13 08:39:25 -0700155 const int midi_device_;
156
agoodef212b2a2015-03-19 12:53:23 -0700157 const Type type_;
158
agoode99d63292015-04-13 08:39:25 -0700159 // Mutable properties. These will get updated as ports move around or
160 // drivers change.
161 std::string path_;
162 int client_id_;
163 int port_id_;
164 std::string client_name_;
165 std::string port_name_;
166 std::string manufacturer_;
167 std::string version_;
168
169 // Index for MidiManager.
Avi Drissman3528fd02015-12-18 20:11:31 -0500170 uint32_t web_port_index_ = 0;
agoode99d63292015-04-13 08:39:25 -0700171
172 // Port is present in the ALSA system.
agoode5a1aa112015-06-21 20:51:00 -0700173 bool connected_ = true;
agoode99d63292015-04-13 08:39:25 -0700174
175 DISALLOW_COPY_AND_ASSIGN(MidiPort);
agoode55a8b522015-03-08 12:40:17 -0700176 };
177
agoode99d63292015-04-13 08:39:25 -0700178 class MidiPortStateBase {
179 public:
agoode5ebc4932015-12-01 08:25:12 -0800180 typedef std::vector<scoped_ptr<MidiPort>>::iterator iterator;
agoodebd4be9b2015-03-16 19:17:25 -0700181
agoode99d63292015-04-13 08:39:25 -0700182 virtual ~MidiPortStateBase();
183
184 // Given a port, finds a port in the internal store.
185 iterator Find(const MidiPort& port);
186
187 // Given a port, finds a connected port, using exact matching.
188 iterator FindConnected(const MidiPort& port);
189
190 // Given a port, finds a disconnected port, using heuristic matching.
191 iterator FindDisconnected(const MidiPort& port);
192
193 iterator begin() { return ports_.begin(); }
194 iterator end() { return ports_.end(); }
195
196 protected:
197 MidiPortStateBase();
agoode5ebc4932015-12-01 08:25:12 -0800198 iterator erase(iterator position) { return ports_.erase(position); }
199 void push_back(scoped_ptr<MidiPort> port) { ports_.push_back(port.Pass()); }
agoode99d63292015-04-13 08:39:25 -0700200
201 private:
agoode5ebc4932015-12-01 08:25:12 -0800202 std::vector<scoped_ptr<MidiPort>> ports_;
agoode99d63292015-04-13 08:39:25 -0700203
204 DISALLOW_COPY_AND_ASSIGN(MidiPortStateBase);
205 };
206
207 class TemporaryMidiPortState final : public MidiPortStateBase {
208 public:
agoode5ebc4932015-12-01 08:25:12 -0800209 iterator erase(iterator position) {
210 return MidiPortStateBase::erase(position);
211 };
212 void push_back(scoped_ptr<MidiPort> port) {
213 MidiPortStateBase::push_back(port.Pass());
214 }
agoode99d63292015-04-13 08:39:25 -0700215 };
216
217 class MidiPortState final : public MidiPortStateBase {
218 public:
219 MidiPortState();
220
agoode5ebc4932015-12-01 08:25:12 -0800221 // Inserts a port at the end. Returns web_port_index.
Avi Drissman3528fd02015-12-18 20:11:31 -0500222 uint32_t push_back(scoped_ptr<MidiPort> port);
agoode99d63292015-04-13 08:39:25 -0700223
224 private:
Avi Drissman3528fd02015-12-18 20:11:31 -0500225 uint32_t num_input_ports_ = 0;
226 uint32_t num_output_ports_ = 0;
agoode99d63292015-04-13 08:39:25 -0700227 };
228
229 class AlsaSeqState {
230 public:
231 enum class PortDirection { kInput, kOutput, kDuplex };
232
233 AlsaSeqState();
234 ~AlsaSeqState();
235
236 void ClientStart(int client_id,
237 const std::string& client_name,
238 snd_seq_client_type_t type);
239 bool ClientStarted(int client_id);
240 void ClientExit(int client_id);
241 void PortStart(int client_id,
242 int port_id,
243 const std::string& port_name,
244 PortDirection direction,
245 bool midi);
246 void PortExit(int client_id, int port_id);
247 snd_seq_client_type_t ClientType(int client_id) const;
agoodeb09423b2015-05-11 11:39:57 -0700248 scoped_ptr<TemporaryMidiPortState> ToMidiPortState(
249 const AlsaCardMap& alsa_cards);
250
251 int card_client_count() { return card_client_count_; }
agoode99d63292015-04-13 08:39:25 -0700252
253 private:
254 class Port {
255 public:
256 Port(const std::string& name, PortDirection direction, bool midi);
257 ~Port();
258
agoodeb0582872015-05-20 05:22:24 -0700259 std::string name() const { return name_; }
260 PortDirection direction() const { return direction_; }
agoode99d63292015-04-13 08:39:25 -0700261 // True if this port is a MIDI port, instead of another kind of ALSA port.
agoodeb0582872015-05-20 05:22:24 -0700262 bool midi() const { return midi_; }
agoode99d63292015-04-13 08:39:25 -0700263
264 private:
265 const std::string name_;
266 const PortDirection direction_;
267 const bool midi_;
268
269 DISALLOW_COPY_AND_ASSIGN(Port);
270 };
271
272 class Client {
273 public:
limasdfe59d0392015-11-19 20:28:57 -0800274 using PortMap = std::map<int, scoped_ptr<Port>>;
agoode99d63292015-04-13 08:39:25 -0700275
276 Client(const std::string& name, snd_seq_client_type_t type);
277 ~Client();
278
agoodeb0582872015-05-20 05:22:24 -0700279 std::string name() const { return name_; }
280 snd_seq_client_type_t type() const { return type_; }
agoode99d63292015-04-13 08:39:25 -0700281 void AddPort(int addr, scoped_ptr<Port> port);
282 void RemovePort(int addr);
283 PortMap::const_iterator begin() const;
284 PortMap::const_iterator end() const;
285
286 private:
287 const std::string name_;
288 const snd_seq_client_type_t type_;
289 PortMap ports_;
agoode99d63292015-04-13 08:39:25 -0700290
291 DISALLOW_COPY_AND_ASSIGN(Client);
292 };
293
limasdfe59d0392015-11-19 20:28:57 -0800294 std::map<int, scoped_ptr<Client>> clients_;
agoode99d63292015-04-13 08:39:25 -0700295
agoodeb09423b2015-05-11 11:39:57 -0700296 // This is the current number of clients we know about that have
297 // cards. When this number matches alsa_card_midi_count_, we know
298 // we are in sync between ALSA and udev. Until then, we cannot generate
299 // MIDIConnectionEvents to web clients.
agoodedf1b9ff2015-06-25 18:14:50 -0700300 int card_client_count_ = 0;
agoodeb09423b2015-05-11 11:39:57 -0700301
agoode99d63292015-04-13 08:39:25 -0700302 DISALLOW_COPY_AND_ASSIGN(AlsaSeqState);
303 };
304
agoodeb09423b2015-05-11 11:39:57 -0700305 class AlsaCard {
306 public:
307 AlsaCard(udev_device* dev,
agooded87fc0f2015-05-21 08:29:31 -0700308 const std::string& name,
309 const std::string& longname,
310 const std::string& driver,
agoodeb09423b2015-05-11 11:39:57 -0700311 int midi_device_count);
312 ~AlsaCard();
agooded87fc0f2015-05-21 08:29:31 -0700313 std::string name() const { return name_; }
314 std::string longname() const { return longname_; }
315 std::string driver() const { return driver_; }
316 std::string path() const { return path_; }
317 std::string bus() const { return bus_; }
318 std::string vendor_id() const { return vendor_id_; }
319 std::string model_id() const { return model_id_; }
320 std::string usb_interface_num() const { return usb_interface_num_; }
321 std::string serial() const { return serial_; }
agoodeb09423b2015-05-11 11:39:57 -0700322 int midi_device_count() const { return midi_device_count_; }
agooded87fc0f2015-05-21 08:29:31 -0700323 std::string manufacturer() const { return manufacturer_; }
agoodeb09423b2015-05-11 11:39:57 -0700324
325 private:
326 FRIEND_TEST_ALL_PREFIXES(MidiManagerAlsaTest, ExtractManufacturer);
327
328 // Extracts the manufacturer using heuristics and a variety of sources.
329 static std::string ExtractManufacturerString(
330 const std::string& udev_id_vendor,
331 const std::string& udev_id_vendor_id,
332 const std::string& udev_id_vendor_from_database,
agooded87fc0f2015-05-21 08:29:31 -0700333 const std::string& name,
334 const std::string& longname);
agoodeb09423b2015-05-11 11:39:57 -0700335
agooded87fc0f2015-05-21 08:29:31 -0700336 const std::string name_;
337 const std::string longname_;
338 const std::string driver_;
339 const std::string path_;
340 const std::string bus_;
341 const std::string vendor_id_;
342 const std::string model_id_;
343 const std::string usb_interface_num_;
344 const std::string serial_;
345 const int midi_device_count_;
346 const std::string manufacturer_;
agoodeb09423b2015-05-11 11:39:57 -0700347
348 DISALLOW_COPY_AND_ASSIGN(AlsaCard);
349 };
350
agoode5a1aa112015-06-21 20:51:00 -0700351 struct SndSeqDeleter {
352 void operator()(snd_seq_t* seq) const { snd_seq_close(seq); }
353 };
354
355 struct SndMidiEventDeleter {
356 void operator()(snd_midi_event_t* coder) const {
357 snd_midi_event_free(coder);
358 };
359 };
360
Avi Drissman3528fd02015-12-18 20:11:31 -0500361 typedef base::hash_map<int, uint32_t> SourceMap;
362 typedef base::hash_map<uint32_t, int> OutPortMap;
agoode99d63292015-04-13 08:39:25 -0700363
agoode@chromium.org25227512014-06-08 05:12:05 +0000364 // An internal callback that runs on MidiSendThread.
Avi Drissman3528fd02015-12-18 20:11:31 -0500365 void SendMidiData(uint32_t port_index, const std::vector<uint8_t>& data);
agoode@chromium.org25227512014-06-08 05:12:05 +0000366
agoodebd4be9b2015-03-16 19:17:25 -0700367 void ScheduleEventLoop();
toyoshim@chromium.org4a8657c2014-02-06 11:23:09 +0000368 void EventLoop();
agoodebd4be9b2015-03-16 19:17:25 -0700369 void ProcessSingleEvent(snd_seq_event_t* event, double timestamp);
agoode99d63292015-04-13 08:39:25 -0700370 void ProcessClientStartEvent(int client_id);
371 void ProcessPortStartEvent(const snd_seq_addr_t& addr);
372 void ProcessClientExitEvent(const snd_seq_addr_t& addr);
373 void ProcessPortExitEvent(const snd_seq_addr_t& addr);
agoode975043d2015-05-11 00:46:17 -0700374 void ProcessUdevEvent(udev_device* dev);
agoodeb09423b2015-05-11 11:39:57 -0700375 void AddCard(udev_device* dev);
376 void RemoveCard(int number);
agoode99d63292015-04-13 08:39:25 -0700377
378 // Updates port_state_ and Web MIDI state from alsa_seq_state_.
379 void UpdatePortStateAndGenerateEvents();
380
381 // Enumerates ports. Call once after subscribing to the announce port.
382 void EnumerateAlsaPorts();
agoode975043d2015-05-11 00:46:17 -0700383 // Enumerates udev cards. Call once after initializing the udev monitor.
384 bool EnumerateUdevCards();
agoode99d63292015-04-13 08:39:25 -0700385 // Returns true if successful.
Avi Drissman3528fd02015-12-18 20:11:31 -0500386 bool CreateAlsaOutputPort(uint32_t port_index, int client_id, int port_id);
387 void DeleteAlsaOutputPort(uint32_t port_index);
agoode99d63292015-04-13 08:39:25 -0700388 // Returns true if successful.
Avi Drissman3528fd02015-12-18 20:11:31 -0500389 bool Subscribe(uint32_t port_index, int client_id, int port_id);
agoode99d63292015-04-13 08:39:25 -0700390
391 AlsaSeqState alsa_seq_state_;
392 MidiPortState port_state_;
toyoshim@chromium.org4a8657c2014-02-06 11:23:09 +0000393
agoodebd4be9b2015-03-16 19:17:25 -0700394 // ALSA seq handles.
agoode5a1aa112015-06-21 20:51:00 -0700395 scoped_ptr<snd_seq_t, SndSeqDeleter> in_client_;
396 int in_client_id_ = -1;
397 scoped_ptr<snd_seq_t, SndSeqDeleter> out_client_;
398 int out_client_id_ = -1;
agoode@chromium.org25227512014-06-08 05:12:05 +0000399
400 // One input port, many output ports.
agoode5a1aa112015-06-21 20:51:00 -0700401 int in_port_id_ = -1;
agoode99d63292015-04-13 08:39:25 -0700402 OutPortMap out_ports_; // guarded by out_ports_lock_
403 base::Lock out_ports_lock_; // guards out_ports_
agoode@chromium.org25227512014-06-08 05:12:05 +0000404
agoodebd4be9b2015-03-16 19:17:25 -0700405 // Mapping from ALSA client:port to our index.
agoode@chromium.org25227512014-06-08 05:12:05 +0000406 SourceMap source_map_;
407
agoodeb09423b2015-05-11 11:39:57 -0700408 // Mapping from card to devices.
409 AlsaCardMap alsa_cards_;
agoodeb09423b2015-05-11 11:39:57 -0700410
411 // This is the current count of midi devices across all cards we know
412 // about. When this number matches card_client_count_ in AlsaSeqState,
413 // we are safe to generate MIDIConnectionEvents. Otherwise we need to
414 // wait for our information from ALSA and udev to get back in sync.
agoode5a1aa112015-06-21 20:51:00 -0700415 int alsa_card_midi_count_ = 0;
agoodeb09423b2015-05-11 11:39:57 -0700416
agoode99d63292015-04-13 08:39:25 -0700417 // ALSA event -> MIDI coder.
agoode5a1aa112015-06-21 20:51:00 -0700418 scoped_ptr<snd_midi_event_t, SndMidiEventDeleter> decoder_;
agoode@chromium.org25227512014-06-08 05:12:05 +0000419
agoode7de413f2015-04-24 00:13:39 -0700420 // udev, for querying hardware devices.
421 device::ScopedUdevPtr udev_;
agoode975043d2015-05-11 00:46:17 -0700422 device::ScopedUdevMonitorPtr udev_monitor_;
agoode7de413f2015-04-24 00:13:39 -0700423
toyoshim@chromium.orga97eebf2014-01-03 07:52:39 +0000424 base::Thread send_thread_;
toyoshim@chromium.org4a8657c2014-02-06 11:23:09 +0000425 base::Thread event_thread_;
426
agoode5a1aa112015-06-21 20:51:00 -0700427 bool event_thread_shutdown_ = false; // guarded by shutdown_lock_
428 base::Lock shutdown_lock_; // guards event_thread_shutdown_
toyoshim@chromium.org4a8657c2014-02-06 11:23:09 +0000429
toyoshim@chromium.orgc82e66e2014-02-04 07:05:47 +0000430 DISALLOW_COPY_AND_ASSIGN(MidiManagerAlsa);
toyoshim@chromium.orga97eebf2014-01-03 07:52:39 +0000431};
432
toyoshime147c5e2015-05-07 21:58:31 -0700433} // namespace midi
toyoshim@chromium.orga97eebf2014-01-03 07:52:39 +0000434} // namespace media
435
dnicoara@chromium.org9f2a6f02014-01-03 21:25:00 +0000436#endif // MEDIA_MIDI_MIDI_MANAGER_ALSA_H_