toyoshim | d28b59c | 2017-02-20 11:07:37 -0800 | [diff] [blame] | 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 | |
toyoshim | 63e32a5 | 2017-04-25 07:20:10 -0700 | [diff] [blame] | 5 | #ifndef MEDIA_MIDI_MIDI_MANAGER_WIN_H_ |
| 6 | #define MEDIA_MIDI_MIDI_MANAGER_WIN_H_ |
toyoshim | d28b59c | 2017-02-20 11:07:37 -0800 | [diff] [blame] | 7 | |
| 8 | #include <memory> |
| 9 | #include <vector> |
| 10 | |
| 11 | #include "base/callback_forward.h" |
toyoshim | d28b59c | 2017-02-20 11:07:37 -0800 | [diff] [blame] | 12 | #include "base/memory/ref_counted.h" |
Sebastien Marchand | 2184877 | 2018-10-05 15:29:27 +0000 | [diff] [blame] | 13 | #include "base/system/system_monitor.h" |
Takashi Toyoshima | 88b4ac0 | 2019-02-12 11:25:56 +0000 | [diff] [blame] | 14 | #include "media/midi/midi_export.h" |
toyoshim | d28b59c | 2017-02-20 11:07:37 -0800 | [diff] [blame] | 15 | #include "media/midi/midi_manager.h" |
| 16 | |
| 17 | namespace base { |
| 18 | class SingleThreadTaskRunner; |
toyoshim | 6d87aaa | 2017-02-28 22:36:44 -0800 | [diff] [blame] | 19 | class TimeDelta; |
toyoshim | d28b59c | 2017-02-20 11:07:37 -0800 | [diff] [blame] | 20 | } // namespace base |
| 21 | |
| 22 | namespace midi { |
| 23 | |
| 24 | // New backend for legacy Windows that support dynamic instantiation. |
toyoshim | 63e32a5 | 2017-04-25 07:20:10 -0700 | [diff] [blame] | 25 | class MidiManagerWin final |
toyoshim | d28b59c | 2017-02-20 11:07:37 -0800 | [diff] [blame] | 26 | : public MidiManager, |
| 27 | public base::SystemMonitor::DevicesChangedObserver { |
| 28 | public: |
toyoshim | 8a5ad42 | 2017-02-28 21:16:18 -0800 | [diff] [blame] | 29 | class PortManager; |
| 30 | |
Takashi Toyoshima | 88b4ac0 | 2019-02-12 11:25:56 +0000 | [diff] [blame] | 31 | MIDI_EXPORT static void OverflowInstanceIdForTesting(); |
| 32 | |
toyoshim | 63e32a5 | 2017-04-25 07:20:10 -0700 | [diff] [blame] | 33 | explicit MidiManagerWin(MidiService* service); |
Peter Boström | 5363403 | 2021-09-22 20:24:34 +0000 | [diff] [blame] | 34 | |
| 35 | MidiManagerWin(const MidiManagerWin&) = delete; |
| 36 | MidiManagerWin& operator=(const MidiManagerWin&) = delete; |
| 37 | |
toyoshim | 63e32a5 | 2017-04-25 07:20:10 -0700 | [diff] [blame] | 38 | ~MidiManagerWin() override; |
toyoshim | d28b59c | 2017-02-20 11:07:37 -0800 | [diff] [blame] | 39 | |
toyoshim | 8a5ad42 | 2017-02-28 21:16:18 -0800 | [diff] [blame] | 40 | // Returns PortManager that implements interfaces to help implementation. |
| 41 | // This hides Windows specific structures, i.e. HMIDIIN in the header. |
| 42 | PortManager* port_manager() { return port_manager_.get(); } |
toyoshim | d28b59c | 2017-02-20 11:07:37 -0800 | [diff] [blame] | 43 | |
| 44 | // MidiManager overrides: |
| 45 | void StartInitialization() override; |
toyoshim | d28b59c | 2017-02-20 11:07:37 -0800 | [diff] [blame] | 46 | void DispatchSendMidiData(MidiManagerClient* client, |
| 47 | uint32_t port_index, |
| 48 | const std::vector<uint8_t>& data, |
tzik | 925e2c6 | 2018-02-02 07:39:45 +0000 | [diff] [blame] | 49 | base::TimeTicks timestamp) override; |
toyoshim | d28b59c | 2017-02-20 11:07:37 -0800 | [diff] [blame] | 50 | |
| 51 | // base::SystemMonitor::DevicesChangedObserver overrides: |
| 52 | void OnDevicesChanged(base::SystemMonitor::DeviceType device_type) override; |
| 53 | |
| 54 | private: |
| 55 | class InPort; |
| 56 | class OutPort; |
| 57 | |
toyoshim | 8a5ad42 | 2017-02-28 21:16:18 -0800 | [diff] [blame] | 58 | // Handles MIDI inport event posted from a thread system provides. |
| 59 | void ReceiveMidiData(uint32_t index, |
| 60 | const std::vector<uint8_t>& data, |
| 61 | base::TimeTicks time); |
| 62 | |
toyoshim | d28b59c | 2017-02-20 11:07:37 -0800 | [diff] [blame] | 63 | // Posts a task to TaskRunner, and ensures that the instance keeps alive while |
| 64 | // the task is running. |
Takashi Toyoshima | d2bdc59 | 2017-09-13 10:02:54 +0000 | [diff] [blame] | 65 | void PostTask(base::OnceClosure); |
| 66 | void PostDelayedTask(base::OnceClosure, base::TimeDelta delay); |
toyoshim | d28b59c | 2017-02-20 11:07:37 -0800 | [diff] [blame] | 67 | |
toyoshim | 8a5ad42 | 2017-02-28 21:16:18 -0800 | [diff] [blame] | 68 | // Posts a reply task to the I/O thread that hosts MidiManager instance, runs |
| 69 | // it safely, and ensures that the instance keeps alive while the task is |
| 70 | // running. |
Takashi Toyoshima | d2bdc59 | 2017-09-13 10:02:54 +0000 | [diff] [blame] | 71 | void PostReplyTask(base::OnceClosure); |
toyoshim | 8a5ad42 | 2017-02-28 21:16:18 -0800 | [diff] [blame] | 72 | |
toyoshim | d28b59c | 2017-02-20 11:07:37 -0800 | [diff] [blame] | 73 | // Initializes instance asynchronously on TaskRunner. |
| 74 | void InitializeOnTaskRunner(); |
| 75 | |
| 76 | // Updates device lists on TaskRunner. |
| 77 | // Returns true if device lists were changed. |
| 78 | void UpdateDeviceListOnTaskRunner(); |
| 79 | |
| 80 | // Reflect active port list to a device list. |
| 81 | template <typename T> |
toyoshim | 63e32a5 | 2017-04-25 07:20:10 -0700 | [diff] [blame] | 82 | void ReflectActiveDeviceList(MidiManagerWin* manager, |
toyoshim | d28b59c | 2017-02-20 11:07:37 -0800 | [diff] [blame] | 83 | std::vector<T>* known_ports, |
| 84 | std::vector<T>* active_ports); |
| 85 | |
toyoshim | 6d87aaa | 2017-02-28 22:36:44 -0800 | [diff] [blame] | 86 | // Sends MIDI data on TaskRunner. |
| 87 | void SendOnTaskRunner(MidiManagerClient* client, |
| 88 | uint32_t port_index, |
| 89 | const std::vector<uint8_t>& data); |
| 90 | |
toyoshim | d28b59c | 2017-02-20 11:07:37 -0800 | [diff] [blame] | 91 | // Holds an unique instance ID. |
Takashi Toyoshima | 88b4ac0 | 2019-02-12 11:25:56 +0000 | [diff] [blame] | 92 | const int64_t instance_id_; |
toyoshim | d28b59c | 2017-02-20 11:07:37 -0800 | [diff] [blame] | 93 | |
| 94 | // Keeps a TaskRunner for the I/O thread. |
| 95 | scoped_refptr<base::SingleThreadTaskRunner> thread_runner_; |
| 96 | |
toyoshim | 8a5ad42 | 2017-02-28 21:16:18 -0800 | [diff] [blame] | 97 | // Manages platform dependent implementation for port managegent. Should be |
| 98 | // accessed with the task lock. |
| 99 | std::unique_ptr<PortManager> port_manager_; |
toyoshim | d28b59c | 2017-02-20 11:07:37 -0800 | [diff] [blame] | 100 | }; |
| 101 | |
| 102 | } // namespace midi |
| 103 | |
toyoshim | 63e32a5 | 2017-04-25 07:20:10 -0700 | [diff] [blame] | 104 | #endif // MEDIA_MIDI_MIDI_MANAGER_WIN_H_ |