blob: f3f6093f603f838854dcbb56df32502fad9a9e8a [file] [log] [blame]
toyoshimd28b59c2017-02-20 11:07:37 -08001// 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
toyoshim63e32a52017-04-25 07:20:10 -07005#ifndef MEDIA_MIDI_MIDI_MANAGER_WIN_H_
6#define MEDIA_MIDI_MIDI_MANAGER_WIN_H_
toyoshimd28b59c2017-02-20 11:07:37 -08007
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
17namespace base {
18class SingleThreadTaskRunner;
toyoshim6d87aaa2017-02-28 22:36:44 -080019class TimeDelta;
toyoshimd28b59c2017-02-20 11:07:37 -080020} // namespace base
21
22namespace midi {
23
24// New backend for legacy Windows that support dynamic instantiation.
toyoshim63e32a52017-04-25 07:20:10 -070025class MidiManagerWin final
toyoshimd28b59c2017-02-20 11:07:37 -080026 : public MidiManager,
27 public base::SystemMonitor::DevicesChangedObserver {
28 public:
toyoshim8a5ad422017-02-28 21:16:18 -080029 class PortManager;
30
toyoshim63e32a52017-04-25 07:20:10 -070031 explicit MidiManagerWin(MidiService* service);
32 ~MidiManagerWin() override;
toyoshimd28b59c2017-02-20 11:07:37 -080033
toyoshim8a5ad422017-02-28 21:16:18 -080034 // 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(); }
toyoshimd28b59c2017-02-20 11:07:37 -080037
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,
tzik925e2c62018-02-02 07:39:45 +000044 base::TimeTicks timestamp) override;
toyoshimd28b59c2017-02-20 11:07:37 -080045
46 // base::SystemMonitor::DevicesChangedObserver overrides:
47 void OnDevicesChanged(base::SystemMonitor::DeviceType device_type) override;
48
49 private:
50 class InPort;
51 class OutPort;
52
toyoshim8a5ad422017-02-28 21:16:18 -080053 // 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
toyoshimd28b59c2017-02-20 11:07:37 -080058 // Posts a task to TaskRunner, and ensures that the instance keeps alive while
59 // the task is running.
Takashi Toyoshimad2bdc592017-09-13 10:02:54 +000060 void PostTask(base::OnceClosure);
61 void PostDelayedTask(base::OnceClosure, base::TimeDelta delay);
toyoshimd28b59c2017-02-20 11:07:37 -080062
toyoshim8a5ad422017-02-28 21:16:18 -080063 // 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.
Takashi Toyoshimad2bdc592017-09-13 10:02:54 +000066 void PostReplyTask(base::OnceClosure);
toyoshim8a5ad422017-02-28 21:16:18 -080067
toyoshimd28b59c2017-02-20 11:07:37 -080068 // 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>
toyoshim63e32a52017-04-25 07:20:10 -070077 void ReflectActiveDeviceList(MidiManagerWin* manager,
toyoshimd28b59c2017-02-20 11:07:37 -080078 std::vector<T>* known_ports,
79 std::vector<T>* active_ports);
80
toyoshim6d87aaa2017-02-28 22:36:44 -080081 // Sends MIDI data on TaskRunner.
82 void SendOnTaskRunner(MidiManagerClient* client,
83 uint32_t port_index,
84 const std::vector<uint8_t>& data);
85
toyoshimd28b59c2017-02-20 11:07:37 -080086 // 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
toyoshim8a5ad422017-02-28 21:16:18 -080092 // Manages platform dependent implementation for port managegent. Should be
93 // accessed with the task lock.
94 std::unique_ptr<PortManager> port_manager_;
toyoshimd28b59c2017-02-20 11:07:37 -080095
toyoshim63e32a52017-04-25 07:20:10 -070096 DISALLOW_COPY_AND_ASSIGN(MidiManagerWin);
toyoshimd28b59c2017-02-20 11:07:37 -080097};
98
99} // namespace midi
100
toyoshim63e32a52017-04-25 07:20:10 -0700101#endif // MEDIA_MIDI_MIDI_MANAGER_WIN_H_