blob: 978abface10e45e354d04e06650a9bb4429bea22 [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"
Takashi Toyoshima88b4ac02019-02-12 11:25:56 +000015#include "media/midi/midi_export.h"
toyoshimd28b59c2017-02-20 11:07:37 -080016#include "media/midi/midi_manager.h"
17
18namespace base {
19class SingleThreadTaskRunner;
toyoshim6d87aaa2017-02-28 22:36:44 -080020class TimeDelta;
toyoshimd28b59c2017-02-20 11:07:37 -080021} // namespace base
22
23namespace midi {
24
25// New backend for legacy Windows that support dynamic instantiation.
toyoshim63e32a52017-04-25 07:20:10 -070026class MidiManagerWin final
toyoshimd28b59c2017-02-20 11:07:37 -080027 : public MidiManager,
28 public base::SystemMonitor::DevicesChangedObserver {
29 public:
toyoshim8a5ad422017-02-28 21:16:18 -080030 class PortManager;
31
Takashi Toyoshima88b4ac02019-02-12 11:25:56 +000032 MIDI_EXPORT static void OverflowInstanceIdForTesting();
33
toyoshim63e32a52017-04-25 07:20:10 -070034 explicit MidiManagerWin(MidiService* service);
35 ~MidiManagerWin() override;
toyoshimd28b59c2017-02-20 11:07:37 -080036
toyoshim8a5ad422017-02-28 21:16:18 -080037 // 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(); }
toyoshimd28b59c2017-02-20 11:07:37 -080040
41 // MidiManager overrides:
42 void StartInitialization() override;
toyoshimd28b59c2017-02-20 11:07:37 -080043 void DispatchSendMidiData(MidiManagerClient* client,
44 uint32_t port_index,
45 const std::vector<uint8_t>& data,
tzik925e2c62018-02-02 07:39:45 +000046 base::TimeTicks timestamp) override;
toyoshimd28b59c2017-02-20 11:07:37 -080047
48 // base::SystemMonitor::DevicesChangedObserver overrides:
49 void OnDevicesChanged(base::SystemMonitor::DeviceType device_type) override;
50
51 private:
52 class InPort;
53 class OutPort;
54
toyoshim8a5ad422017-02-28 21:16:18 -080055 // 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
toyoshimd28b59c2017-02-20 11:07:37 -080060 // Posts a task to TaskRunner, and ensures that the instance keeps alive while
61 // the task is running.
Takashi Toyoshimad2bdc592017-09-13 10:02:54 +000062 void PostTask(base::OnceClosure);
63 void PostDelayedTask(base::OnceClosure, base::TimeDelta delay);
toyoshimd28b59c2017-02-20 11:07:37 -080064
toyoshim8a5ad422017-02-28 21:16:18 -080065 // 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 Toyoshimad2bdc592017-09-13 10:02:54 +000068 void PostReplyTask(base::OnceClosure);
toyoshim8a5ad422017-02-28 21:16:18 -080069
toyoshimd28b59c2017-02-20 11:07:37 -080070 // 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>
toyoshim63e32a52017-04-25 07:20:10 -070079 void ReflectActiveDeviceList(MidiManagerWin* manager,
toyoshimd28b59c2017-02-20 11:07:37 -080080 std::vector<T>* known_ports,
81 std::vector<T>* active_ports);
82
toyoshim6d87aaa2017-02-28 22:36:44 -080083 // Sends MIDI data on TaskRunner.
84 void SendOnTaskRunner(MidiManagerClient* client,
85 uint32_t port_index,
86 const std::vector<uint8_t>& data);
87
toyoshimd28b59c2017-02-20 11:07:37 -080088 // Holds an unique instance ID.
Takashi Toyoshima88b4ac02019-02-12 11:25:56 +000089 const int64_t instance_id_;
toyoshimd28b59c2017-02-20 11:07:37 -080090
91 // Keeps a TaskRunner for the I/O thread.
92 scoped_refptr<base::SingleThreadTaskRunner> thread_runner_;
93
toyoshim8a5ad422017-02-28 21:16:18 -080094 // Manages platform dependent implementation for port managegent. Should be
95 // accessed with the task lock.
96 std::unique_ptr<PortManager> port_manager_;
toyoshimd28b59c2017-02-20 11:07:37 -080097
toyoshim63e32a52017-04-25 07:20:10 -070098 DISALLOW_COPY_AND_ASSIGN(MidiManagerWin);
toyoshimd28b59c2017-02-20 11:07:37 -080099};
100
101} // namespace midi
102
toyoshim63e32a52017-04-25 07:20:10 -0700103#endif // MEDIA_MIDI_MIDI_MANAGER_WIN_H_