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