blob: a47689a4bcb5461545c8e181d6b135ac36a5690e [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"
toyoshimd28b59c2017-02-20 11:07:37 -080012#include "base/memory/ref_counted.h"
Sebastien Marchand21848772018-10-05 15:29:27 +000013#include "base/system/system_monitor.h"
Takashi Toyoshima88b4ac02019-02-12 11:25:56 +000014#include "media/midi/midi_export.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
Takashi Toyoshima88b4ac02019-02-12 11:25:56 +000031 MIDI_EXPORT static void OverflowInstanceIdForTesting();
32
toyoshim63e32a52017-04-25 07:20:10 -070033 explicit MidiManagerWin(MidiService* service);
Peter Boström53634032021-09-22 20:24:34 +000034
35 MidiManagerWin(const MidiManagerWin&) = delete;
36 MidiManagerWin& operator=(const MidiManagerWin&) = delete;
37
toyoshim63e32a52017-04-25 07:20:10 -070038 ~MidiManagerWin() override;
toyoshimd28b59c2017-02-20 11:07:37 -080039
toyoshim8a5ad422017-02-28 21:16:18 -080040 // 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(); }
toyoshimd28b59c2017-02-20 11:07:37 -080043
44 // MidiManager overrides:
45 void StartInitialization() override;
toyoshimd28b59c2017-02-20 11:07:37 -080046 void DispatchSendMidiData(MidiManagerClient* client,
47 uint32_t port_index,
48 const std::vector<uint8_t>& data,
tzik925e2c62018-02-02 07:39:45 +000049 base::TimeTicks timestamp) override;
toyoshimd28b59c2017-02-20 11:07:37 -080050
51 // base::SystemMonitor::DevicesChangedObserver overrides:
52 void OnDevicesChanged(base::SystemMonitor::DeviceType device_type) override;
53
54 private:
55 class InPort;
56 class OutPort;
57
toyoshim8a5ad422017-02-28 21:16:18 -080058 // 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
toyoshimd28b59c2017-02-20 11:07:37 -080063 // Posts a task to TaskRunner, and ensures that the instance keeps alive while
64 // the task is running.
Takashi Toyoshimad2bdc592017-09-13 10:02:54 +000065 void PostTask(base::OnceClosure);
66 void PostDelayedTask(base::OnceClosure, base::TimeDelta delay);
toyoshimd28b59c2017-02-20 11:07:37 -080067
toyoshim8a5ad422017-02-28 21:16:18 -080068 // 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 Toyoshimad2bdc592017-09-13 10:02:54 +000071 void PostReplyTask(base::OnceClosure);
toyoshim8a5ad422017-02-28 21:16:18 -080072
toyoshimd28b59c2017-02-20 11:07:37 -080073 // 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>
toyoshim63e32a52017-04-25 07:20:10 -070082 void ReflectActiveDeviceList(MidiManagerWin* manager,
toyoshimd28b59c2017-02-20 11:07:37 -080083 std::vector<T>* known_ports,
84 std::vector<T>* active_ports);
85
toyoshim6d87aaa2017-02-28 22:36:44 -080086 // Sends MIDI data on TaskRunner.
87 void SendOnTaskRunner(MidiManagerClient* client,
88 uint32_t port_index,
89 const std::vector<uint8_t>& data);
90
toyoshimd28b59c2017-02-20 11:07:37 -080091 // Holds an unique instance ID.
Takashi Toyoshima88b4ac02019-02-12 11:25:56 +000092 const int64_t instance_id_;
toyoshimd28b59c2017-02-20 11:07:37 -080093
94 // Keeps a TaskRunner for the I/O thread.
95 scoped_refptr<base::SingleThreadTaskRunner> thread_runner_;
96
toyoshim8a5ad422017-02-28 21:16:18 -080097 // Manages platform dependent implementation for port managegent. Should be
98 // accessed with the task lock.
99 std::unique_ptr<PortManager> port_manager_;
toyoshimd28b59c2017-02-20 11:07:37 -0800100};
101
102} // namespace midi
103
toyoshim63e32a52017-04-25 07:20:10 -0700104#endif // MEDIA_MIDI_MIDI_MANAGER_WIN_H_