blob: 3516a14364ae8a55bc750301665760a79b2c6cd6 [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"
Sebastien Marchand21848772018-10-05 15:29:27 +000014#include "base/system/system_monitor.h"
toyoshimd28b59c2017-02-20 11:07:37 -080015#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;
toyoshimd28b59c2017-02-20 11:07:37 -080040 void DispatchSendMidiData(MidiManagerClient* client,
41 uint32_t port_index,
42 const std::vector<uint8_t>& data,
tzik925e2c62018-02-02 07:39:45 +000043 base::TimeTicks timestamp) override;
toyoshimd28b59c2017-02-20 11:07:37 -080044
45 // base::SystemMonitor::DevicesChangedObserver overrides:
46 void OnDevicesChanged(base::SystemMonitor::DeviceType device_type) override;
47
48 private:
49 class InPort;
50 class OutPort;
51
toyoshim8a5ad422017-02-28 21:16:18 -080052 // 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
toyoshimd28b59c2017-02-20 11:07:37 -080057 // Posts a task to TaskRunner, and ensures that the instance keeps alive while
58 // the task is running.
Takashi Toyoshimad2bdc592017-09-13 10:02:54 +000059 void PostTask(base::OnceClosure);
60 void PostDelayedTask(base::OnceClosure, base::TimeDelta delay);
toyoshimd28b59c2017-02-20 11:07:37 -080061
toyoshim8a5ad422017-02-28 21:16:18 -080062 // 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 Toyoshimad2bdc592017-09-13 10:02:54 +000065 void PostReplyTask(base::OnceClosure);
toyoshim8a5ad422017-02-28 21:16:18 -080066
toyoshimd28b59c2017-02-20 11:07:37 -080067 // 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>
toyoshim63e32a52017-04-25 07:20:10 -070076 void ReflectActiveDeviceList(MidiManagerWin* manager,
toyoshimd28b59c2017-02-20 11:07:37 -080077 std::vector<T>* known_ports,
78 std::vector<T>* active_ports);
79
toyoshim6d87aaa2017-02-28 22:36:44 -080080 // Sends MIDI data on TaskRunner.
81 void SendOnTaskRunner(MidiManagerClient* client,
82 uint32_t port_index,
83 const std::vector<uint8_t>& data);
84
toyoshimd28b59c2017-02-20 11:07:37 -080085 // 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
toyoshim8a5ad422017-02-28 21:16:18 -080091 // Manages platform dependent implementation for port managegent. Should be
92 // accessed with the task lock.
93 std::unique_ptr<PortManager> port_manager_;
toyoshimd28b59c2017-02-20 11:07:37 -080094
toyoshim63e32a52017-04-25 07:20:10 -070095 DISALLOW_COPY_AND_ASSIGN(MidiManagerWin);
toyoshimd28b59c2017-02-20 11:07:37 -080096};
97
98} // namespace midi
99
toyoshim63e32a52017-04-25 07:20:10 -0700100#endif // MEDIA_MIDI_MIDI_MANAGER_WIN_H_