Add a name to the proxy resolver utility process and display it in the task manager.
[chromium-blink-merge.git] / media / midi / midi_manager_alsa.h
blob4ee47bf92c1b5dc311f0805948e72a1a55acf9c8
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>
9 #include <map>
10 #include <vector>
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"
20 #if defined(USE_UDEV)
21 #include "device/udev_linux/scoped_udev.h"
22 #endif // defined(USE_UDEV)
24 namespace media {
26 class MEDIA_EXPORT MidiManagerAlsa : public MidiManager {
27 public:
28 MidiManagerAlsa();
29 ~MidiManagerAlsa() override;
31 // MidiManager implementation.
32 void StartInitialization() override;
33 void DispatchSendMidiData(MidiManagerClient* client,
34 uint32 port_index,
35 const std::vector<uint8>& data,
36 double timestamp) override;
38 private:
39 FRIEND_TEST_ALL_PREFIXES(MidiManagerAlsaTest, ExtractManufacturer);
40 FRIEND_TEST_ALL_PREFIXES(MidiManagerAlsaTest, JSONPortMetadata);
42 class AlsaRawmidi {
43 public:
44 AlsaRawmidi(const MidiManagerAlsa* outer,
45 const std::string& alsa_name,
46 const std::string& alsa_longname,
47 const std::string& alsa_driver,
48 int card_index);
49 ~AlsaRawmidi();
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;
60 private:
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_;
75 std::string path_;
76 std::string bus_;
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 {
85 public:
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,
96 Type type);
97 ~AlsaPortMetadata();
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
107 // the full state.
108 std::string OpaqueKey() const;
110 private:
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_;
122 const Type type_;
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();
138 void EventLoop();
139 void ProcessSingleEvent(snd_seq_event_t* event, double timestamp);
141 // ALSA seq handles.
142 snd_seq_t* in_client_;
143 snd_seq_t* out_client_;
144 int out_client_id_;
146 // One input port, many output ports.
147 int in_port_;
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);
171 } // namespace media
173 #endif // MEDIA_MIDI_MIDI_MANAGER_ALSA_H_