| 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 base::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 206 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 262 private: | 261 private: |
| 263 const std::string name_; | 262 const std::string name_; |
| 264 const PortDirection direction_; | 263 const PortDirection direction_; |
| 265 const bool midi_; | 264 const bool midi_; |
| 266 | 265 |
| 267 DISALLOW_COPY_AND_ASSIGN(Port); | 266 DISALLOW_COPY_AND_ASSIGN(Port); |
| 268 }; | 267 }; |
| 269 | 268 |
| 270 class Client { | 269 class Client { |
| 271 public: | 270 public: |
| 272 typedef std::map<int, Port*> PortMap; | 271 typedef base::ScopedPtrMap<int, scoped_ptr<Port>> PortMap; |
| 273 | 272 |
| 274 Client(const std::string& name, snd_seq_client_type_t type); | 273 Client(const std::string& name, snd_seq_client_type_t type); |
| 275 ~Client(); | 274 ~Client(); |
| 276 | 275 |
| 277 std::string name() const { return name_; } | 276 std::string name() const { return name_; } |
| 278 snd_seq_client_type_t type() const { return type_; } | 277 snd_seq_client_type_t type() const { return type_; } |
| 279 void AddPort(int addr, scoped_ptr<Port> port); | 278 void AddPort(int addr, scoped_ptr<Port> port); |
| 280 void RemovePort(int addr); | 279 void RemovePort(int addr); |
| 281 PortMap::const_iterator begin() const; | 280 PortMap::const_iterator begin() const; |
| 282 PortMap::const_iterator end() const; | 281 PortMap::const_iterator end() const; |
| 283 | 282 |
| 284 private: | 283 private: |
| 285 const std::string name_; | 284 const std::string name_; |
| 286 const snd_seq_client_type_t type_; | 285 const snd_seq_client_type_t type_; |
| 287 PortMap ports_; | 286 PortMap ports_; |
| 288 STLValueDeleter<PortMap> ports_deleter_; | |
| 289 | 287 |
| 290 DISALLOW_COPY_AND_ASSIGN(Client); | 288 DISALLOW_COPY_AND_ASSIGN(Client); |
| 291 }; | 289 }; |
| 292 | 290 |
| 293 typedef std::map<int, Client*> ClientMap; | 291 typedef base::ScopedPtrMap<int, scoped_ptr<Client>> ClientMap; |
| 294 | 292 |
| 295 ClientMap clients_; | 293 ClientMap clients_; |
| 296 STLValueDeleter<ClientMap> clients_deleter_; | |
| 297 | 294 |
| 298 // This is the current number of clients we know about that have | 295 // This is the current number of clients we know about that have |
| 299 // cards. When this number matches alsa_card_midi_count_, we know | 296 // cards. When this number matches alsa_card_midi_count_, we know |
| 300 // we are in sync between ALSA and udev. Until then, we cannot generate | 297 // we are in sync between ALSA and udev. Until then, we cannot generate |
| 301 // MIDIConnectionEvents to web clients. | 298 // MIDIConnectionEvents to web clients. |
| 302 int card_client_count_; | 299 int card_client_count_; |
| 303 | 300 |
| 304 DISALLOW_COPY_AND_ASSIGN(AlsaSeqState); | 301 DISALLOW_COPY_AND_ASSIGN(AlsaSeqState); |
| 305 }; | 302 }; |
| 306 | 303 |
| (...skipping 95 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 402 // One input port, many output ports. | 399 // One input port, many output ports. |
| 403 int in_port_id_ = -1; | 400 int in_port_id_ = -1; |
| 404 OutPortMap out_ports_; // guarded by out_ports_lock_ | 401 OutPortMap out_ports_; // guarded by out_ports_lock_ |
| 405 base::Lock out_ports_lock_; // guards out_ports_ | 402 base::Lock out_ports_lock_; // guards out_ports_ |
| 406 | 403 |
| 407 // Mapping from ALSA client:port to our index. | 404 // Mapping from ALSA client:port to our index. |
| 408 SourceMap source_map_; | 405 SourceMap source_map_; |
| 409 | 406 |
| 410 // Mapping from card to devices. | 407 // Mapping from card to devices. |
| 411 AlsaCardMap alsa_cards_; | 408 AlsaCardMap alsa_cards_; |
| 412 STLValueDeleter<AlsaCardMap> alsa_cards_deleter_; | |
| 413 | 409 |
| 414 // This is the current count of midi devices across all cards we know | 410 // This is the current count of midi devices across all cards we know |
| 415 // about. When this number matches card_client_count_ in AlsaSeqState, | 411 // about. When this number matches card_client_count_ in AlsaSeqState, |
| 416 // we are safe to generate MIDIConnectionEvents. Otherwise we need to | 412 // we are safe to generate MIDIConnectionEvents. Otherwise we need to |
| 417 // wait for our information from ALSA and udev to get back in sync. | 413 // wait for our information from ALSA and udev to get back in sync. |
| 418 int alsa_card_midi_count_ = 0; | 414 int alsa_card_midi_count_ = 0; |
| 419 | 415 |
| 420 // ALSA event -> MIDI coder. | 416 // ALSA event -> MIDI coder. |
| 421 scoped_ptr<snd_midi_event_t, SndMidiEventDeleter> decoder_; | 417 scoped_ptr<snd_midi_event_t, SndMidiEventDeleter> decoder_; |
| 422 | 418 |
| 423 // udev, for querying hardware devices. | 419 // udev, for querying hardware devices. |
| 424 device::ScopedUdevPtr udev_; | 420 device::ScopedUdevPtr udev_; |
| 425 device::ScopedUdevMonitorPtr udev_monitor_; | 421 device::ScopedUdevMonitorPtr udev_monitor_; |
| 426 | 422 |
| 427 base::Thread send_thread_; | 423 base::Thread send_thread_; |
| 428 base::Thread event_thread_; | 424 base::Thread event_thread_; |
| 429 | 425 |
| 430 bool event_thread_shutdown_ = false; // guarded by shutdown_lock_ | 426 bool event_thread_shutdown_ = false; // guarded by shutdown_lock_ |
| 431 base::Lock shutdown_lock_; // guards event_thread_shutdown_ | 427 base::Lock shutdown_lock_; // guards event_thread_shutdown_ |
| 432 | 428 |
| 433 DISALLOW_COPY_AND_ASSIGN(MidiManagerAlsa); | 429 DISALLOW_COPY_AND_ASSIGN(MidiManagerAlsa); |
| 434 }; | 430 }; |
| 435 | 431 |
| 436 } // namespace midi | 432 } // namespace midi |
| 437 } // namespace media | 433 } // namespace media |
| 438 | 434 |
| 439 #endif // MEDIA_MIDI_MIDI_MANAGER_ALSA_H_ | 435 #endif // MEDIA_MIDI_MIDI_MANAGER_ALSA_H_ |
| OLD | NEW |