OLD | NEW |
| (Empty) |
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 | |
3 // found in the LICENSE file. | |
4 | |
5 #ifndef MEDIA_MIDI_DYNAMICALLY_INITIALIZED_MIDI_MANAGER_WIN_H_ | |
6 #define MEDIA_MIDI_DYNAMICALLY_INITIALIZED_MIDI_MANAGER_WIN_H_ | |
7 | |
8 #include <memory> | |
9 #include <vector> | |
10 | |
11 #include "base/callback_forward.h" | |
12 #include "base/macros.h" | |
13 #include "base/memory/ref_counted.h" | |
14 #include "base/system_monitor/system_monitor.h" | |
15 #include "media/midi/midi_manager.h" | |
16 | |
17 namespace base { | |
18 class SingleThreadTaskRunner; | |
19 class TimeDelta; | |
20 } // namespace base | |
21 | |
22 namespace midi { | |
23 | |
24 // New backend for legacy Windows that support dynamic instantiation. | |
25 class DynamicallyInitializedMidiManagerWin final | |
26 : public MidiManager, | |
27 public base::SystemMonitor::DevicesChangedObserver { | |
28 public: | |
29 class PortManager; | |
30 | |
31 explicit DynamicallyInitializedMidiManagerWin(MidiService* service); | |
32 ~DynamicallyInitializedMidiManagerWin() override; | |
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 | |
38 // MidiManager overrides: | |
39 void StartInitialization() override; | |
40 void Finalize() override; | |
41 void DispatchSendMidiData(MidiManagerClient* client, | |
42 uint32_t port_index, | |
43 const std::vector<uint8_t>& data, | |
44 double timestamp) override; | |
45 | |
46 // base::SystemMonitor::DevicesChangedObserver overrides: | |
47 void OnDevicesChanged(base::SystemMonitor::DeviceType device_type) override; | |
48 | |
49 private: | |
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(DynamicallyInitializedMidiManagerWin* 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 | |
96 DISALLOW_COPY_AND_ASSIGN(DynamicallyInitializedMidiManagerWin); | |
97 }; | |
98 | |
99 } // namespace midi | |
100 | |
101 #endif // MEDIA_MIDI_DYNAMICALLY_INITIALIZED_MIDI_MANAGER_WIN_H_ | |
OLD | NEW |