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> | |
10 #include <vector> | 9 #include <vector> |
11 | 10 |
12 #include "base/basictypes.h" | 11 #include "base/basictypes.h" |
| 12 #include "base/containers/scoped_ptr_map.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/memory/scoped_vector.h" | 15 #include "base/memory/scoped_vector.h" |
16 #include "base/stl_util.h" | |
17 #include "base/synchronization/lock.h" | 16 #include "base/synchronization/lock.h" |
18 #include "base/threading/thread.h" | 17 #include "base/threading/thread.h" |
19 #include "base/values.h" | 18 #include "base/values.h" |
20 #include "device/udev_linux/scoped_udev.h" | 19 #include "device/udev_linux/scoped_udev.h" |
21 #include "media/midi/midi_export.h" | 20 #include "media/midi/midi_export.h" |
22 #include "media/midi/midi_manager.h" | 21 #include "media/midi/midi_manager.h" |
23 | 22 |
24 namespace media { | 23 namespace media { |
25 namespace midi { | 24 namespace midi { |
26 | 25 |
27 class MIDI_EXPORT MidiManagerAlsa final : public MidiManager { | 26 class MIDI_EXPORT MidiManagerAlsa final : public MidiManager { |
28 public: | 27 public: |
29 MidiManagerAlsa(); | 28 MidiManagerAlsa(); |
30 ~MidiManagerAlsa() override; | 29 ~MidiManagerAlsa() override; |
31 | 30 |
32 // MidiManager implementation. | 31 // MidiManager implementation. |
33 void StartInitialization() override; | 32 void StartInitialization() override; |
34 void DispatchSendMidiData(MidiManagerClient* client, | 33 void DispatchSendMidiData(MidiManagerClient* client, |
35 uint32 port_index, | 34 uint32 port_index, |
36 const std::vector<uint8>& data, | 35 const std::vector<uint8>& data, |
37 double timestamp) override; | 36 double timestamp) override; |
38 | 37 |
39 private: | 38 private: |
40 friend class MidiManagerAlsaTest; | 39 friend class MidiManagerAlsaTest; |
41 FRIEND_TEST_ALL_PREFIXES(MidiManagerAlsaTest, ExtractManufacturer); | 40 FRIEND_TEST_ALL_PREFIXES(MidiManagerAlsaTest, ExtractManufacturer); |
42 FRIEND_TEST_ALL_PREFIXES(MidiManagerAlsaTest, ToMidiPortState); | 41 FRIEND_TEST_ALL_PREFIXES(MidiManagerAlsaTest, ToMidiPortState); |
43 | 42 |
44 class AlsaCard; | 43 class AlsaCard; |
45 typedef std::map<int, AlsaCard*> AlsaCardMap; | 44 typedef ScopedPtrMap<int, scoped_ptr<AlsaCard>> AlsaCardMap; |
46 | 45 |
47 class MidiPort { | 46 class MidiPort { |
48 public: | 47 public: |
49 enum class Type { kInput, kOutput }; | 48 enum class Type { kInput, kOutput }; |
50 | 49 |
51 // The Id class is used to keep the multiple strings separate | 50 // The Id class is used to keep the multiple strings separate |
52 // but compare them all together for equality purposes. | 51 // but compare them all together for equality purposes. |
53 // The individual strings that make up the Id can theoretically contain | 52 // The individual strings that make up the Id can theoretically contain |
54 // arbitrary characters, so unfortunately there is no simple way to | 53 // arbitrary characters, so unfortunately there is no simple way to |
55 // concatenate them into a single string. | 54 // concatenate them into a single string. |
(...skipping 208 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
264 private: | 263 private: |
265 const std::string name_; | 264 const std::string name_; |
266 const PortDirection direction_; | 265 const PortDirection direction_; |
267 const bool midi_; | 266 const bool midi_; |
268 | 267 |
269 DISALLOW_COPY_AND_ASSIGN(Port); | 268 DISALLOW_COPY_AND_ASSIGN(Port); |
270 }; | 269 }; |
271 | 270 |
272 class Client { | 271 class Client { |
273 public: | 272 public: |
274 typedef std::map<int, Port*> PortMap; | 273 typedef ScopedPtrMap<int, scoped_ptr<Port>> PortMap; |
275 | 274 |
276 Client(const std::string& name, snd_seq_client_type_t type); | 275 Client(const std::string& name, snd_seq_client_type_t type); |
277 ~Client(); | 276 ~Client(); |
278 | 277 |
279 std::string name() const { return name_; } | 278 std::string name() const { return name_; } |
280 snd_seq_client_type_t type() const { return type_; } | 279 snd_seq_client_type_t type() const { return type_; } |
281 void AddPort(int addr, scoped_ptr<Port> port); | 280 void AddPort(int addr, scoped_ptr<Port> port); |
282 void RemovePort(int addr); | 281 void RemovePort(int addr); |
283 PortMap::const_iterator begin() const; | 282 PortMap::const_iterator begin() const; |
284 PortMap::const_iterator end() const; | 283 PortMap::const_iterator end() const; |
285 | 284 |
286 private: | 285 private: |
287 const std::string name_; | 286 const std::string name_; |
288 const snd_seq_client_type_t type_; | 287 const snd_seq_client_type_t type_; |
289 PortMap ports_; | 288 PortMap ports_; |
290 STLValueDeleter<PortMap> ports_deleter_; | |
291 | 289 |
292 DISALLOW_COPY_AND_ASSIGN(Client); | 290 DISALLOW_COPY_AND_ASSIGN(Client); |
293 }; | 291 }; |
294 | 292 |
295 typedef std::map<int, Client*> ClientMap; | 293 typedef ScopedPtrMap<int, scoped_ptr<Client>> ClientMap; |
296 | 294 |
297 ClientMap clients_; | 295 ClientMap clients_; |
298 STLValueDeleter<ClientMap> clients_deleter_; | |
299 | 296 |
300 // This is the current number of clients we know about that have | 297 // This is the current number of clients we know about that have |
301 // cards. When this number matches alsa_card_midi_count_, we know | 298 // cards. When this number matches alsa_card_midi_count_, we know |
302 // we are in sync between ALSA and udev. Until then, we cannot generate | 299 // we are in sync between ALSA and udev. Until then, we cannot generate |
303 // MIDIConnectionEvents to web clients. | 300 // MIDIConnectionEvents to web clients. |
304 int card_client_count_; | 301 int card_client_count_; |
305 | 302 |
306 DISALLOW_COPY_AND_ASSIGN(AlsaSeqState); | 303 DISALLOW_COPY_AND_ASSIGN(AlsaSeqState); |
307 }; | 304 }; |
308 | 305 |
(...skipping 85 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
394 // One input port, many output ports. | 391 // One input port, many output ports. |
395 int in_port_id_; | 392 int in_port_id_; |
396 OutPortMap out_ports_; // guarded by out_ports_lock_ | 393 OutPortMap out_ports_; // guarded by out_ports_lock_ |
397 base::Lock out_ports_lock_; // guards out_ports_ | 394 base::Lock out_ports_lock_; // guards out_ports_ |
398 | 395 |
399 // Mapping from ALSA client:port to our index. | 396 // Mapping from ALSA client:port to our index. |
400 SourceMap source_map_; | 397 SourceMap source_map_; |
401 | 398 |
402 // Mapping from card to devices. | 399 // Mapping from card to devices. |
403 AlsaCardMap alsa_cards_; | 400 AlsaCardMap alsa_cards_; |
404 STLValueDeleter<AlsaCardMap> alsa_cards_deleter_; | |
405 | 401 |
406 // This is the current count of midi devices across all cards we know | 402 // This is the current count of midi devices across all cards we know |
407 // about. When this number matches card_client_count_ in AlsaSeqState, | 403 // about. When this number matches card_client_count_ in AlsaSeqState, |
408 // we are safe to generate MIDIConnectionEvents. Otherwise we need to | 404 // we are safe to generate MIDIConnectionEvents. Otherwise we need to |
409 // wait for our information from ALSA and udev to get back in sync. | 405 // wait for our information from ALSA and udev to get back in sync. |
410 int alsa_card_midi_count_; | 406 int alsa_card_midi_count_; |
411 | 407 |
412 // ALSA event -> MIDI coder. | 408 // ALSA event -> MIDI coder. |
413 snd_midi_event_t* decoder_; | 409 snd_midi_event_t* decoder_; |
414 | 410 |
415 // udev, for querying hardware devices. | 411 // udev, for querying hardware devices. |
416 device::ScopedUdevPtr udev_; | 412 device::ScopedUdevPtr udev_; |
417 device::ScopedUdevMonitorPtr udev_monitor_; | 413 device::ScopedUdevMonitorPtr udev_monitor_; |
418 | 414 |
419 base::Thread send_thread_; | 415 base::Thread send_thread_; |
420 base::Thread event_thread_; | 416 base::Thread event_thread_; |
421 | 417 |
422 bool event_thread_shutdown_; // guarded by shutdown_lock_ | 418 bool event_thread_shutdown_; // guarded by shutdown_lock_ |
423 base::Lock shutdown_lock_; // guards event_thread_shutdown_ | 419 base::Lock shutdown_lock_; // guards event_thread_shutdown_ |
424 | 420 |
425 DISALLOW_COPY_AND_ASSIGN(MidiManagerAlsa); | 421 DISALLOW_COPY_AND_ASSIGN(MidiManagerAlsa); |
426 }; | 422 }; |
427 | 423 |
428 } // namespace midi | 424 } // namespace midi |
429 } // namespace media | 425 } // namespace media |
430 | 426 |
431 #endif // MEDIA_MIDI_MIDI_MANAGER_ALSA_H_ | 427 #endif // MEDIA_MIDI_MIDI_MANAGER_ALSA_H_ |
OLD | NEW |