| 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 <stdint.h> | 9 #include <stdint.h> |
| 10 | 10 |
| 11 #include <map> | 11 #include <map> |
| 12 #include <memory> | 12 #include <memory> |
| 13 #include <utility> | 13 #include <utility> |
| 14 #include <vector> | 14 #include <vector> |
| 15 | 15 |
| 16 #include "base/containers/hash_tables.h" | 16 #include "base/containers/hash_tables.h" |
| 17 #include "base/gtest_prod_util.h" | 17 #include "base/gtest_prod_util.h" |
| 18 #include "base/macros.h" | 18 #include "base/macros.h" |
| 19 #include "base/synchronization/lock.h" | 19 #include "base/synchronization/lock.h" |
| 20 #include "base/threading/thread.h" | 20 #include "base/threading/thread.h" |
| 21 #include "base/values.h" | 21 #include "base/values.h" |
| 22 #include "device/udev_linux/scoped_udev.h" | 22 #include "device/udev_linux/scoped_udev.h" |
| 23 #include "media/midi/midi_export.h" | 23 #include "media/midi/midi_export.h" |
| 24 #include "media/midi/midi_manager.h" | 24 #include "media/midi/midi_manager.h" |
| 25 | 25 |
| 26 namespace base { | |
| 27 class ThreadChecker; | |
| 28 } | |
| 29 | |
| 30 namespace midi { | 26 namespace midi { |
| 31 | 27 |
| 32 class MIDI_EXPORT MidiManagerAlsa final : public MidiManager { | 28 class MIDI_EXPORT MidiManagerAlsa final : public MidiManager { |
| 33 public: | 29 public: |
| 34 explicit MidiManagerAlsa(MidiService* service); | 30 explicit MidiManagerAlsa(MidiService* service); |
| 35 ~MidiManagerAlsa() override; | 31 ~MidiManagerAlsa() override; |
| 36 | 32 |
| 37 // MidiManager implementation. | 33 // MidiManager implementation. |
| 38 void StartInitialization() override; | 34 void StartInitialization() override; |
| 39 void Finalize() override; | 35 void Finalize() override; |
| (...skipping 326 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 366 }; | 362 }; |
| 367 }; | 363 }; |
| 368 | 364 |
| 369 using SourceMap = base::hash_map<int, uint32_t>; | 365 using SourceMap = base::hash_map<int, uint32_t>; |
| 370 using OutPortMap = base::hash_map<uint32_t, int>; | 366 using OutPortMap = base::hash_map<uint32_t, int>; |
| 371 using ScopedSndSeqPtr = std::unique_ptr<snd_seq_t, SndSeqDeleter>; | 367 using ScopedSndSeqPtr = std::unique_ptr<snd_seq_t, SndSeqDeleter>; |
| 372 using ScopedSndMidiEventPtr = | 368 using ScopedSndMidiEventPtr = |
| 373 std::unique_ptr<snd_midi_event_t, SndMidiEventDeleter>; | 369 std::unique_ptr<snd_midi_event_t, SndMidiEventDeleter>; |
| 374 | 370 |
| 375 // An internal callback that runs on MidiSendThread. | 371 // An internal callback that runs on MidiSendThread. |
| 376 void SendMidiData(int instance_id, | 372 void SendMidiData(MidiManagerClient* client, |
| 377 MidiManagerClient* client, | |
| 378 uint32_t port_index, | 373 uint32_t port_index, |
| 379 const std::vector<uint8_t>& data); | 374 const std::vector<uint8_t>& data); |
| 380 | 375 |
| 381 void EventLoop(int instance_id); | 376 void EventLoop(); |
| 382 void ProcessSingleEvent(snd_seq_event_t* event, double timestamp); | 377 void ProcessSingleEvent(snd_seq_event_t* event, double timestamp); |
| 383 void ProcessClientStartEvent(int client_id); | 378 void ProcessClientStartEvent(int client_id); |
| 384 void ProcessPortStartEvent(const snd_seq_addr_t& addr); | 379 void ProcessPortStartEvent(const snd_seq_addr_t& addr); |
| 385 void ProcessClientExitEvent(const snd_seq_addr_t& addr); | 380 void ProcessClientExitEvent(const snd_seq_addr_t& addr); |
| 386 void ProcessPortExitEvent(const snd_seq_addr_t& addr); | 381 void ProcessPortExitEvent(const snd_seq_addr_t& addr); |
| 387 void ProcessUdevEvent(udev_device* dev); | 382 void ProcessUdevEvent(udev_device* dev); |
| 388 void AddCard(udev_device* dev); | 383 void AddCard(udev_device* dev); |
| 389 void RemoveCard(int number); | 384 void RemoveCard(int number); |
| 390 | 385 |
| 391 // Updates port_state_ and Web MIDI state from alsa_seq_state_. | 386 // Updates port_state_ and Web MIDI state from alsa_seq_state_. |
| (...skipping 23 matching lines...) Expand all Loading... |
| 415 | 410 |
| 416 // Mapping from card to devices. | 411 // Mapping from card to devices. |
| 417 AlsaCardMap alsa_cards_; | 412 AlsaCardMap alsa_cards_; |
| 418 | 413 |
| 419 // This is the current count of midi devices across all cards we know | 414 // This is the current count of midi devices across all cards we know |
| 420 // about. When this number matches card_client_count_ in AlsaSeqState, | 415 // about. When this number matches card_client_count_ in AlsaSeqState, |
| 421 // we are safe to generate MIDIConnectionEvents. Otherwise we need to | 416 // we are safe to generate MIDIConnectionEvents. Otherwise we need to |
| 422 // wait for our information from ALSA and udev to get back in sync. | 417 // wait for our information from ALSA and udev to get back in sync. |
| 423 int alsa_card_midi_count_ = 0; | 418 int alsa_card_midi_count_ = 0; |
| 424 | 419 |
| 425 // This lock is needed to ensure that members destroyed in Finalize | |
| 426 // will be visibly destroyed before the destructor is run in the | |
| 427 // other thread. Otherwise, the same objects may have their destructors | |
| 428 // run multiple times in different threads. | |
| 429 base::Lock lazy_init_member_lock_; // guards members below | |
| 430 | |
| 431 // Members initialized in StartInitialization() are below. | |
| 432 // Make sure to destroy these in Finalize()! | |
| 433 std::unique_ptr<base::ThreadChecker> initialization_thread_checker_; | |
| 434 | |
| 435 // ALSA seq handles and ids. | 420 // ALSA seq handles and ids. |
| 436 ScopedSndSeqPtr in_client_; | 421 ScopedSndSeqPtr in_client_; |
| 437 int in_client_id_; | 422 int in_client_id_; |
| 438 ScopedSndSeqPtr out_client_; | 423 ScopedSndSeqPtr out_client_; |
| 439 int out_client_id_; | 424 int out_client_id_; |
| 440 int in_port_id_; | 425 int in_port_id_; |
| 441 | 426 |
| 442 // ALSA event -> MIDI coder. | 427 // ALSA event -> MIDI coder. |
| 443 ScopedSndMidiEventPtr decoder_; | 428 ScopedSndMidiEventPtr decoder_; |
| 444 | 429 |
| 445 // udev, for querying hardware devices. | 430 // udev, for querying hardware devices. |
| 446 device::ScopedUdevPtr udev_; | 431 device::ScopedUdevPtr udev_; |
| 447 device::ScopedUdevMonitorPtr udev_monitor_; | 432 device::ScopedUdevMonitorPtr udev_monitor_; |
| 448 | 433 |
| 449 int instance_id_; | |
| 450 | |
| 451 DISALLOW_COPY_AND_ASSIGN(MidiManagerAlsa); | 434 DISALLOW_COPY_AND_ASSIGN(MidiManagerAlsa); |
| 452 }; | 435 }; |
| 453 | 436 |
| 454 } // namespace midi | 437 } // namespace midi |
| 455 | 438 |
| 456 #endif // MEDIA_MIDI_MIDI_MANAGER_ALSA_H_ | 439 #endif // MEDIA_MIDI_MIDI_MANAGER_ALSA_H_ |
| OLD | NEW |