| OLD | NEW |
| 1 // Copyright 2014 The Chromium Authors. All rights reserved. | 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 | 2 // Use of this source code is governed by a BSD-style license that can be |
| 3 // found in the LICENSE file. | 3 // found in the LICENSE file. |
| 4 | 4 |
| 5 #ifndef MEDIA_MIDI_MIDI_MANAGER_ALSA_H_ | 5 #ifndef MEDIA_MIDI_MIDI_MANAGER_ALSA_H_ |
| 6 #define MEDIA_MIDI_MIDI_MANAGER_ALSA_H_ | 6 #define MEDIA_MIDI_MIDI_MANAGER_ALSA_H_ |
| 7 | 7 |
| 8 #include <alsa/asoundlib.h> | 8 #include <alsa/asoundlib.h> |
| 9 #include <map> | 9 #include <map> |
| 10 #include <vector> | 10 #include <vector> |
| 11 | 11 |
| 12 #include "base/basictypes.h" | 12 #include "base/basictypes.h" |
| 13 #include "base/gtest_prod_util.h" | 13 #include "base/gtest_prod_util.h" |
| 14 #include "base/memory/scoped_ptr.h" | 14 #include "base/memory/scoped_ptr.h" |
| 15 #include "base/synchronization/lock.h" | 15 #include "base/synchronization/lock.h" |
| 16 #include "base/threading/thread.h" | 16 #include "base/threading/thread.h" |
| 17 #include "base/values.h" |
| 17 #include "media/midi/midi_manager.h" | 18 #include "media/midi/midi_manager.h" |
| 18 | 19 |
| 19 #if defined(USE_UDEV) | 20 #if defined(USE_UDEV) |
| 20 #include "device/udev_linux/scoped_udev.h" | 21 #include "device/udev_linux/scoped_udev.h" |
| 21 #endif // defined(USE_UDEV) | 22 #endif // defined(USE_UDEV) |
| 22 | 23 |
| 23 namespace media { | 24 namespace media { |
| 24 | 25 |
| 25 class MEDIA_EXPORT MidiManagerAlsa : public MidiManager { | 26 class MEDIA_EXPORT MidiManagerAlsa : public MidiManager { |
| 26 public: | 27 public: |
| 27 MidiManagerAlsa(); | 28 MidiManagerAlsa(); |
| 28 ~MidiManagerAlsa() override; | 29 ~MidiManagerAlsa() override; |
| 29 | 30 |
| 30 // MidiManager implementation. | 31 // MidiManager implementation. |
| 31 void StartInitialization() override; | 32 void StartInitialization() override; |
| 32 void DispatchSendMidiData(MidiManagerClient* client, | 33 void DispatchSendMidiData(MidiManagerClient* client, |
| 33 uint32 port_index, | 34 uint32 port_index, |
| 34 const std::vector<uint8>& data, | 35 const std::vector<uint8>& data, |
| 35 double timestamp) override; | 36 double timestamp) override; |
| 36 | 37 |
| 37 private: | 38 private: |
| 38 FRIEND_TEST_ALL_PREFIXES(MidiManagerAlsaTest, ExtractManufacturer); | 39 FRIEND_TEST_ALL_PREFIXES(MidiManagerAlsaTest, ExtractManufacturer); |
| 40 FRIEND_TEST_ALL_PREFIXES(MidiManagerAlsaTest, JSONPortMetadata); |
| 39 | 41 |
| 40 class MidiDevice { | 42 class AlsaRawmidi { |
| 41 public: | 43 public: |
| 42 MidiDevice(const MidiManagerAlsa* outer, | 44 AlsaRawmidi(const MidiManagerAlsa* outer, |
| 43 const std::string& alsa_name, | 45 const std::string& alsa_name, |
| 44 const std::string& alsa_longname, | 46 const std::string& alsa_longname, |
| 45 const std::string& alsa_driver, | 47 const std::string& alsa_driver, |
| 46 int card_index); | 48 int card_index); |
| 47 ~MidiDevice(); | 49 ~AlsaRawmidi(); |
| 48 | 50 |
| 49 const std::string alsa_name() const; | 51 const std::string alsa_name() const; |
| 52 const std::string alsa_longname() const; |
| 50 const std::string manufacturer() const; | 53 const std::string manufacturer() const; |
| 51 const std::string alsa_driver() const; | 54 const std::string alsa_driver() const; |
| 52 const std::string udev_id_path() const; | 55 const std::string path() const; |
| 53 const std::string udev_id_id() const; | 56 const std::string bus() const; |
| 57 const std::string vendor_id() const; |
| 58 const std::string id() const; |
| 54 | 59 |
| 55 private: | 60 private: |
| 56 FRIEND_TEST_ALL_PREFIXES(MidiManagerAlsaTest, ExtractManufacturer); | 61 FRIEND_TEST_ALL_PREFIXES(MidiManagerAlsaTest, ExtractManufacturer); |
| 57 | 62 |
| 58 // Extracts the manufacturer using heuristics and a variety of sources. | 63 // Extracts the manufacturer using heuristics and a variety of sources. |
| 59 static std::string ExtractManufacturerString( | 64 static std::string ExtractManufacturerString( |
| 60 const std::string& udev_id_vendor, | 65 const std::string& udev_id_vendor, |
| 61 const std::string& udev_id_vendor_id, | 66 const std::string& udev_id_vendor_id, |
| 62 const std::string& udev_id_vendor_from_database, | 67 const std::string& udev_id_vendor_from_database, |
| 63 const std::string& alsa_name, | 68 const std::string& alsa_name, |
| 64 const std::string& alsa_longname); | 69 const std::string& alsa_longname); |
| 65 | 70 |
| 66 std::string alsa_name_; | 71 std::string alsa_name_; |
| 72 std::string alsa_longname_; |
| 67 std::string manufacturer_; | 73 std::string manufacturer_; |
| 68 std::string alsa_driver_; | 74 std::string alsa_driver_; |
| 69 std::string udev_id_path_; | 75 std::string path_; |
| 70 std::string udev_id_id_; | 76 std::string bus_; |
| 77 std::string vendor_id_; |
| 78 std::string model_id_; |
| 79 std::string usb_interface_num_; |
| 71 | 80 |
| 72 DISALLOW_COPY_AND_ASSIGN(MidiDevice); | 81 DISALLOW_COPY_AND_ASSIGN(AlsaRawmidi); |
| 82 }; |
| 83 |
| 84 class AlsaPortMetadata { |
| 85 public: |
| 86 enum class Type { kInput, kOutput }; |
| 87 |
| 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(); |
| 98 |
| 99 // Gets a Value representation of this object, suitable for serialization. |
| 100 scoped_ptr<base::Value> Value() const; |
| 101 |
| 102 // Gets a string version of Value in JSON format. |
| 103 std::string JSONValue() const; |
| 104 |
| 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; |
| 109 |
| 110 private: |
| 111 FRIEND_TEST_ALL_PREFIXES(MidiManagerAlsaTest, JSONPortMetadata); |
| 112 |
| 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_; |
| 123 |
| 124 DISALLOW_COPY_AND_ASSIGN(AlsaPortMetadata); |
| 73 }; | 125 }; |
| 74 | 126 |
| 75 // Returns an ordered vector of all the rawmidi devices on the system. | 127 // Returns an ordered vector of all the rawmidi devices on the system. |
| 76 ScopedVector<MidiDevice> AllMidiDevices(); | 128 ScopedVector<AlsaRawmidi> AllAlsaRawmidis(); |
| 77 | 129 |
| 78 // Enumerate all the ports for initial setup. | 130 // Enumerate all the ports for initial setup. |
| 79 void EnumeratePorts(); | 131 void EnumeratePorts(); |
| 80 | 132 |
| 81 // An internal callback that runs on MidiSendThread. | 133 // An internal callback that runs on MidiSendThread. |
| 82 void SendMidiData(uint32 port_index, | 134 void SendMidiData(uint32 port_index, |
| 83 const std::vector<uint8>& data); | 135 const std::vector<uint8>& data); |
| 84 | 136 |
| 85 void ScheduleEventLoop(); | 137 void ScheduleEventLoop(); |
| 86 void EventLoop(); | 138 void EventLoop(); |
| 87 void ProcessSingleEvent(snd_seq_event_t* event, double timestamp); | 139 void ProcessSingleEvent(snd_seq_event_t* event, double timestamp); |
| 88 | 140 |
| 89 // ALSA seq handles. | 141 // ALSA seq handles. |
| 90 snd_seq_t* in_client_; | 142 snd_seq_t* in_client_; |
| 91 snd_seq_t* out_client_; | 143 snd_seq_t* out_client_; |
| 92 int out_client_id_; | 144 int out_client_id_; |
| 93 | 145 |
| 94 // One input port, many output ports. | 146 // One input port, many output ports. |
| 95 int in_port_; | 147 int in_port_; |
| 96 std::vector<int> out_ports_; | 148 std::vector<int> out_ports_; |
| 97 | 149 |
| 98 // Mapping from ALSA client:port to our index. | 150 // Mapping from ALSA client:port to our index. |
| 99 typedef std::map<int, uint32> SourceMap; | 151 typedef std::map<int, uint32> SourceMap; |
| 100 SourceMap source_map_; | 152 SourceMap source_map_; |
| 101 | 153 |
| 102 // ALSA event <-> MIDI coders. | 154 // ALSA event <-> MIDI coders. |
| 103 snd_midi_event_t* decoder_; | 155 snd_midi_event_t* decoder_; |
| 104 typedef std::vector<snd_midi_event_t*> EncoderList; | |
| 105 EncoderList encoders_; | |
| 106 | 156 |
| 107 // udev, for querying hardware devices. | 157 // udev, for querying hardware devices. |
| 108 #if defined(USE_UDEV) | 158 #if defined(USE_UDEV) |
| 109 device::ScopedUdevPtr udev_; | 159 device::ScopedUdevPtr udev_; |
| 110 #endif // defined(USE_UDEV) | 160 #endif // defined(USE_UDEV) |
| 111 | 161 |
| 112 base::Thread send_thread_; | 162 base::Thread send_thread_; |
| 113 base::Thread event_thread_; | 163 base::Thread event_thread_; |
| 114 | 164 |
| 115 bool event_thread_shutdown_; // guarded by shutdown_lock_ | 165 bool event_thread_shutdown_; // guarded by shutdown_lock_ |
| 116 base::Lock shutdown_lock_; // guards event_thread_shutdown_ | 166 base::Lock shutdown_lock_; // guards event_thread_shutdown_ |
| 117 | 167 |
| 118 DISALLOW_COPY_AND_ASSIGN(MidiManagerAlsa); | 168 DISALLOW_COPY_AND_ASSIGN(MidiManagerAlsa); |
| 119 }; | 169 }; |
| 120 | 170 |
| 121 } // namespace media | 171 } // namespace media |
| 122 | 172 |
| 123 #endif // MEDIA_MIDI_MIDI_MANAGER_ALSA_H_ | 173 #endif // MEDIA_MIDI_MIDI_MANAGER_ALSA_H_ |
| OLD | NEW |