| OLD | NEW |
| 1 // Copyright 2013 The Chromium Authors. All rights reserved. | 1 // Copyright 2017 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_WIN_H_ | 5 #ifndef MEDIA_MIDI_MIDI_MANAGER_WIN_H_ |
| 6 #define MEDIA_MIDI_MIDI_MANAGER_WIN_H_ | 6 #define MEDIA_MIDI_MIDI_MANAGER_WIN_H_ |
| 7 | 7 |
| 8 #include <stdint.h> | |
| 9 | |
| 10 #include <memory> | 8 #include <memory> |
| 11 #include <vector> | 9 #include <vector> |
| 12 | 10 |
| 11 #include "base/callback_forward.h" |
| 13 #include "base/macros.h" | 12 #include "base/macros.h" |
| 14 #include "base/threading/thread.h" | 13 #include "base/memory/ref_counted.h" |
| 15 #include "base/time/time.h" | 14 #include "base/system_monitor/system_monitor.h" |
| 16 #include "media/midi/midi_manager.h" | 15 #include "media/midi/midi_manager.h" |
| 17 #include "media/midi/midi_service.mojom.h" | 16 |
| 17 namespace base { |
| 18 class SingleThreadTaskRunner; |
| 19 class TimeDelta; |
| 20 } // namespace base |
| 18 | 21 |
| 19 namespace midi { | 22 namespace midi { |
| 20 | 23 |
| 21 class MidiService; | 24 // New backend for legacy Windows that support dynamic instantiation. |
| 25 class MidiManagerWin final |
| 26 : public MidiManager, |
| 27 public base::SystemMonitor::DevicesChangedObserver { |
| 28 public: |
| 29 class PortManager; |
| 22 | 30 |
| 23 class MidiServiceWinDelegate { | |
| 24 public: | |
| 25 virtual ~MidiServiceWinDelegate() {} | |
| 26 virtual void OnCompleteInitialization(mojom::Result result) = 0; | |
| 27 virtual void OnAddInputPort(MidiPortInfo info) = 0; | |
| 28 virtual void OnAddOutputPort(MidiPortInfo info) = 0; | |
| 29 virtual void OnSetInputPortState(uint32_t port_index, | |
| 30 mojom::PortState state) = 0; | |
| 31 virtual void OnSetOutputPortState(uint32_t port_index, | |
| 32 mojom::PortState state) = 0; | |
| 33 virtual void OnReceiveMidiData(uint32_t port_index, | |
| 34 const std::vector<uint8_t>& data, | |
| 35 base::TimeTicks time) = 0; | |
| 36 }; | |
| 37 | |
| 38 class MidiServiceWin { | |
| 39 public: | |
| 40 virtual ~MidiServiceWin() {} | |
| 41 // This method may return before the initialization is completed. | |
| 42 virtual void InitializeAsync(MidiServiceWinDelegate* delegate) = 0; | |
| 43 // This method may return before the specified data is actually sent. | |
| 44 virtual void SendMidiDataAsync(uint32_t port_number, | |
| 45 const std::vector<uint8_t>& data, | |
| 46 base::TimeTicks time) = 0; | |
| 47 }; | |
| 48 | |
| 49 class MidiManagerWin final : public MidiManager, public MidiServiceWinDelegate { | |
| 50 public: | |
| 51 explicit MidiManagerWin(MidiService* service); | 31 explicit MidiManagerWin(MidiService* service); |
| 52 ~MidiManagerWin() override; | 32 ~MidiManagerWin() override; |
| 53 | 33 |
| 34 // Returns PortManager that implements interfaces to help implementation. |
| 35 // This hides Windows specific structures, i.e. HMIDIIN in the header. |
| 36 PortManager* port_manager() { return port_manager_.get(); } |
| 37 |
| 54 // MidiManager overrides: | 38 // MidiManager overrides: |
| 55 void StartInitialization() final; | 39 void StartInitialization() override; |
| 56 void Finalize() final; | 40 void Finalize() override; |
| 57 void DispatchSendMidiData(MidiManagerClient* client, | 41 void DispatchSendMidiData(MidiManagerClient* client, |
| 58 uint32_t port_index, | 42 uint32_t port_index, |
| 59 const std::vector<uint8_t>& data, | 43 const std::vector<uint8_t>& data, |
| 60 double timestamp) final; | 44 double timestamp) override; |
| 61 | 45 |
| 62 // MidiServiceWinDelegate overrides: | 46 // base::SystemMonitor::DevicesChangedObserver overrides: |
| 63 void OnCompleteInitialization(mojom::Result result) final; | 47 void OnDevicesChanged(base::SystemMonitor::DeviceType device_type) override; |
| 64 void OnAddInputPort(MidiPortInfo info) final; | |
| 65 void OnAddOutputPort(MidiPortInfo info) final; | |
| 66 void OnSetInputPortState(uint32_t port_index, mojom::PortState state) final; | |
| 67 void OnSetOutputPortState(uint32_t port_index, mojom::PortState state) final; | |
| 68 void OnReceiveMidiData(uint32_t port_index, | |
| 69 const std::vector<uint8_t>& data, | |
| 70 base::TimeTicks time) final; | |
| 71 | 48 |
| 72 private: | 49 private: |
| 73 std::unique_ptr<MidiServiceWin> midi_service_; | 50 class InPort; |
| 51 class OutPort; |
| 52 |
| 53 // Handles MIDI inport event posted from a thread system provides. |
| 54 void ReceiveMidiData(uint32_t index, |
| 55 const std::vector<uint8_t>& data, |
| 56 base::TimeTicks time); |
| 57 |
| 58 // Posts a task to TaskRunner, and ensures that the instance keeps alive while |
| 59 // the task is running. |
| 60 void PostTask(const base::Closure&); |
| 61 void PostDelayedTask(const base::Closure&, base::TimeDelta delay); |
| 62 |
| 63 // Posts a reply task to the I/O thread that hosts MidiManager instance, runs |
| 64 // it safely, and ensures that the instance keeps alive while the task is |
| 65 // running. |
| 66 void PostReplyTask(const base::Closure&); |
| 67 |
| 68 // Initializes instance asynchronously on TaskRunner. |
| 69 void InitializeOnTaskRunner(); |
| 70 |
| 71 // Updates device lists on TaskRunner. |
| 72 // Returns true if device lists were changed. |
| 73 void UpdateDeviceListOnTaskRunner(); |
| 74 |
| 75 // Reflect active port list to a device list. |
| 76 template <typename T> |
| 77 void ReflectActiveDeviceList(MidiManagerWin* manager, |
| 78 std::vector<T>* known_ports, |
| 79 std::vector<T>* active_ports); |
| 80 |
| 81 // Sends MIDI data on TaskRunner. |
| 82 void SendOnTaskRunner(MidiManagerClient* client, |
| 83 uint32_t port_index, |
| 84 const std::vector<uint8_t>& data); |
| 85 |
| 86 // Holds an unique instance ID. |
| 87 const int instance_id_; |
| 88 |
| 89 // Keeps a TaskRunner for the I/O thread. |
| 90 scoped_refptr<base::SingleThreadTaskRunner> thread_runner_; |
| 91 |
| 92 // Manages platform dependent implementation for port managegent. Should be |
| 93 // accessed with the task lock. |
| 94 std::unique_ptr<PortManager> port_manager_; |
| 95 |
| 74 DISALLOW_COPY_AND_ASSIGN(MidiManagerWin); | 96 DISALLOW_COPY_AND_ASSIGN(MidiManagerWin); |
| 75 }; | 97 }; |
| 76 | 98 |
| 77 } // namespace midi | 99 } // namespace midi |
| 78 | 100 |
| 79 #endif // MEDIA_MIDI_MIDI_MANAGER_WIN_H_ | 101 #endif // MEDIA_MIDI_MIDI_MANAGER_WIN_H_ |
| OLD | NEW |