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