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 AlsaCard(const MidiManagerAlsa
* outer
,
45 const std::string
& alsa_name
,
46 const std::string
& alsa_longname
,
47 const std::string
& alsa_driver
,
52 const std::string
alsa_name() const;
53 const std::string
alsa_longname() const;
54 const std::string
manufacturer() const;
55 const std::string
alsa_driver() const;
56 const std::string
path() const;
57 const std::string
bus() const;
58 const std::string
vendor_id() const;
59 const std::string
id() const;
60 const int midi_count() const;
63 FRIEND_TEST_ALL_PREFIXES(MidiManagerAlsaTest
, ExtractManufacturer
);
65 // Extracts the manufacturer using heuristics and a variety of sources.
66 static std::string
ExtractManufacturerString(
67 const std::string
& udev_id_vendor
,
68 const std::string
& udev_id_vendor_id
,
69 const std::string
& udev_id_vendor_from_database
,
70 const std::string
& alsa_name
,
71 const std::string
& alsa_longname
);
73 std::string alsa_name_
;
74 std::string alsa_longname_
;
75 std::string manufacturer_
;
76 std::string alsa_driver_
;
79 std::string vendor_id_
;
80 std::string model_id_
;
81 std::string usb_interface_num_
;
84 DISALLOW_COPY_AND_ASSIGN(AlsaCard
);
87 class AlsaPortMetadata
{
89 enum class Type
{ kInput
, kOutput
};
91 AlsaPortMetadata(const std::string
& path
,
92 const std::string
& bus
,
93 const std::string
& id
,
94 const snd_seq_addr_t
* address
,
95 const std::string
& client_name
,
96 const std::string
& port_name
,
97 const std::string
& card_name
,
98 const std::string
& card_longname
,
102 // Gets a Value representation of this object, suitable for serialization.
103 scoped_ptr
<base::Value
> Value() const;
105 // Gets a string version of Value in JSON format.
106 std::string
JSONValue() const;
108 // Gets an opaque identifier for this object, suitable for using as the id
109 // field in MidiPort.id on the web. Note that this string does not store
111 std::string
OpaqueKey() const;
114 FRIEND_TEST_ALL_PREFIXES(MidiManagerAlsaTest
, JSONPortMetadata
);
116 const std::string path_
;
117 const std::string bus_
;
118 const std::string id_
;
119 const int client_addr_
;
120 const int port_addr_
;
121 const std::string client_name_
;
122 const std::string port_name_
;
123 const std::string card_name_
;
124 const std::string card_longname_
;
127 DISALLOW_COPY_AND_ASSIGN(AlsaPortMetadata
);
130 // Returns an ordered vector of all the cards with MIDI capabilities.
131 ScopedVector
<AlsaCard
> AllMidiCards();
133 // Enumerate all the ports for initial setup.
134 void EnumeratePorts();
136 // An internal callback that runs on MidiSendThread.
137 void SendMidiData(uint32 port_index
,
138 const std::vector
<uint8
>& data
);
140 void ScheduleEventLoop();
142 void ProcessSingleEvent(snd_seq_event_t
* event
, double timestamp
);
145 snd_seq_t
* in_client_
;
146 snd_seq_t
* out_client_
;
149 // One input port, many output ports.
151 std::vector
<int> out_ports_
;
153 // Mapping from ALSA client:port to our index.
154 typedef std::map
<int, uint32
> SourceMap
;
155 SourceMap source_map_
;
157 // ALSA event <-> MIDI coders.
158 snd_midi_event_t
* decoder_
;
160 // udev, for querying hardware devices.
161 #if defined(USE_UDEV)
162 device::ScopedUdevPtr udev_
;
163 #endif // defined(USE_UDEV)
165 base::Thread send_thread_
;
166 base::Thread event_thread_
;
168 bool event_thread_shutdown_
; // guarded by shutdown_lock_
169 base::Lock shutdown_lock_
; // guards event_thread_shutdown_
171 DISALLOW_COPY_AND_ASSIGN(MidiManagerAlsa
);
176 #endif // MEDIA_MIDI_MIDI_MANAGER_ALSA_H_