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.
5 #ifndef MEDIA_MIDI_MIDI_MANAGER_ALSA_H_
6 #define MEDIA_MIDI_MIDI_MANAGER_ALSA_H_
8 #include <alsa/asoundlib.h>
12 #include "base/basictypes.h"
13 #include "base/gtest_prod_util.h"
14 #include "base/memory/scoped_ptr.h"
15 #include "base/synchronization/lock.h"
16 #include "base/threading/thread.h"
17 #include "base/values.h"
18 #include "media/midi/midi_manager.h"
21 #include "device/udev_linux/scoped_udev.h"
22 #endif // defined(USE_UDEV)
26 class MEDIA_EXPORT MidiManagerAlsa
: public MidiManager
{
29 ~MidiManagerAlsa() override
;
31 // MidiManager implementation.
32 void StartInitialization() override
;
33 void DispatchSendMidiData(MidiManagerClient
* client
,
35 const std::vector
<uint8
>& data
,
36 double timestamp
) override
;
39 FRIEND_TEST_ALL_PREFIXES(MidiManagerAlsaTest
, ExtractManufacturer
);
40 FRIEND_TEST_ALL_PREFIXES(MidiManagerAlsaTest
, JSONPortMetadata
);
44 AlsaRawmidi(const MidiManagerAlsa
* outer
,
45 const std::string
& alsa_name
,
46 const std::string
& alsa_longname
,
47 const std::string
& alsa_driver
,
51 const std::string
alsa_name() const;
52 const std::string
alsa_longname() const;
53 const std::string
manufacturer() const;
54 const std::string
alsa_driver() const;
55 const std::string
path() const;
56 const std::string
bus() const;
57 const std::string
vendor_id() const;
58 const std::string
id() const;
61 FRIEND_TEST_ALL_PREFIXES(MidiManagerAlsaTest
, ExtractManufacturer
);
63 // Extracts the manufacturer using heuristics and a variety of sources.
64 static std::string
ExtractManufacturerString(
65 const std::string
& udev_id_vendor
,
66 const std::string
& udev_id_vendor_id
,
67 const std::string
& udev_id_vendor_from_database
,
68 const std::string
& alsa_name
,
69 const std::string
& alsa_longname
);
71 std::string alsa_name_
;
72 std::string alsa_longname_
;
73 std::string manufacturer_
;
74 std::string alsa_driver_
;
77 std::string vendor_id_
;
78 std::string model_id_
;
79 std::string usb_interface_num_
;
81 DISALLOW_COPY_AND_ASSIGN(AlsaRawmidi
);
84 class AlsaPortMetadata
{
86 enum class Type
{ kInput
, kOutput
};
88 AlsaPortMetadata(const std::string
& path
,
89 const std::string
& bus
,
90 const std::string
& id
,
91 const snd_seq_addr_t
* address
,
92 const std::string
& client_name
,
93 const std::string
& port_name
,
94 const std::string
& card_name
,
95 const std::string
& card_longname
,
99 // Gets a Value representation of this object, suitable for serialization.
100 scoped_ptr
<base::Value
> Value() const;
102 // Gets a string version of Value in JSON format.
103 std::string
JSONValue() const;
105 // Gets an opaque identifier for this object, suitable for using as the id
106 // field in MidiPort.id on the web. Note that this string does not store
108 std::string
OpaqueKey() const;
111 FRIEND_TEST_ALL_PREFIXES(MidiManagerAlsaTest
, JSONPortMetadata
);
113 const std::string path_
;
114 const std::string bus_
;
115 const std::string id_
;
116 const int client_addr_
;
117 const int port_addr_
;
118 const std::string client_name_
;
119 const std::string port_name_
;
120 const std::string card_name_
;
121 const std::string card_longname_
;
124 DISALLOW_COPY_AND_ASSIGN(AlsaPortMetadata
);
127 // Returns an ordered vector of all the rawmidi devices on the system.
128 ScopedVector
<AlsaRawmidi
> AllAlsaRawmidis();
130 // Enumerate all the ports for initial setup.
131 void EnumeratePorts();
133 // An internal callback that runs on MidiSendThread.
134 void SendMidiData(uint32 port_index
,
135 const std::vector
<uint8
>& data
);
137 void ScheduleEventLoop();
139 void ProcessSingleEvent(snd_seq_event_t
* event
, double timestamp
);
142 snd_seq_t
* in_client_
;
143 snd_seq_t
* out_client_
;
146 // One input port, many output ports.
148 std::vector
<int> out_ports_
;
150 // Mapping from ALSA client:port to our index.
151 typedef std::map
<int, uint32
> SourceMap
;
152 SourceMap source_map_
;
154 // ALSA event <-> MIDI coders.
155 snd_midi_event_t
* decoder_
;
157 // udev, for querying hardware devices.
158 #if defined(USE_UDEV)
159 device::ScopedUdevPtr udev_
;
160 #endif // defined(USE_UDEV)
162 base::Thread send_thread_
;
163 base::Thread event_thread_
;
165 bool event_thread_shutdown_
; // guarded by shutdown_lock_
166 base::Lock shutdown_lock_
; // guards event_thread_shutdown_
168 DISALLOW_COPY_AND_ASSIGN(MidiManagerAlsa
);
173 #endif // MEDIA_MIDI_MIDI_MANAGER_ALSA_H_