blob: d72e9398fa3927f1f8659841e4becbf37eef2e8c [file] [log] [blame]
crogers@google.com27356e42013-06-22 04:03:03 +00001// Copyright (c) 2013 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
5#ifndef MEDIA_MIDI_MIDI_MANAGER_H_
6#define MEDIA_MIDI_MIDI_MANAGER_H_
7
avi793390d2015-12-22 22:22:36 -08008#include <stddef.h>
9#include <stdint.h>
10
crogers@google.com27356e42013-06-22 04:03:03 +000011#include <set>
toyoshim@chromium.orgae6ad362013-08-27 15:30:20 +000012#include <vector>
crogers@google.com27356e42013-06-22 04:03:03 +000013
avieea95e82015-12-18 20:27:08 -080014#include "base/macros.h"
toyoshim@chromium.orgc1e05fb2014-05-06 16:39:20 +000015#include "base/memory/ref_counted.h"
crogers@google.com27356e42013-06-22 04:03:03 +000016#include "base/synchronization/lock.h"
yhirano@chromium.orgcfa642c2014-05-01 08:54:41 +000017#include "base/time/time.h"
brettw49ff0172015-05-05 12:43:04 -070018#include "media/midi/midi_export.h"
crogers@google.com27356e42013-06-22 04:03:03 +000019#include "media/midi/midi_port_info.h"
toyoshim2f3a48f2016-10-17 01:54:13 -070020#include "media/midi/midi_service.mojom.h"
crogers@google.com27356e42013-06-22 04:03:03 +000021
toyoshim@chromium.orgc1e05fb2014-05-06 16:39:20 +000022namespace base {
23class SingleThreadTaskRunner;
24} // namespace base
25
toyoshime147c5e2015-05-07 21:58:31 -070026namespace midi {
crogers@google.com27356e42013-06-22 04:03:03 +000027
toyoshimf4d61522017-02-10 02:03:32 -080028class MidiService;
29
toyoshim@chromium.orgc82e66e2014-02-04 07:05:47 +000030// A MidiManagerClient registers with the MidiManager to receive MIDI data.
31// See MidiManager::RequestAccess() and MidiManager::ReleaseAccess()
crogers@google.com27356e42013-06-22 04:03:03 +000032// for details.
toyoshim14a32342016-12-14 22:18:29 -080033// TODO(toyoshim): Consider to have a MidiServiceClient interface.
brettw49ff0172015-05-05 12:43:04 -070034class MIDI_EXPORT MidiManagerClient {
crogers@google.com27356e42013-06-22 04:03:03 +000035 public:
toyoshim@chromium.orgf77a1e62014-04-11 13:16:24 +000036 virtual ~MidiManagerClient() {}
37
toyoshima62d6742014-10-23 09:05:03 -070038 // AddInputPort() and AddOutputPort() are called before CompleteStartSession()
39 // is called to notify existing MIDI ports, and also called after that to
40 // notify new MIDI ports are added.
41 virtual void AddInputPort(const MidiPortInfo& info) = 0;
42 virtual void AddOutputPort(const MidiPortInfo& info) = 0;
43
toyoshim5c6fe4b2015-02-18 23:28:09 -080044 // SetInputPortState() and SetOutputPortState() are called to notify a known
45 // device gets disconnected, or connected again.
toyoshimec2570a2016-10-21 02:15:27 -070046 virtual void SetInputPortState(uint32_t port_index,
47 mojom::PortState state) = 0;
48 virtual void SetOutputPortState(uint32_t port_index,
49 mojom::PortState state) = 0;
toyoshima62d6742014-10-23 09:05:03 -070050
toyoshim@chromium.orgf77a1e62014-04-11 13:16:24 +000051 // CompleteStartSession() is called when platform dependent preparation is
52 // finished.
toyoshim2f3a48f2016-10-17 01:54:13 -070053 virtual void CompleteStartSession(mojom::Result result) = 0;
crogers@google.com27356e42013-06-22 04:03:03 +000054
toyoshim@chromium.orgc82e66e2014-02-04 07:05:47 +000055 // ReceiveMidiData() is called when MIDI data has been received from the
crogers@google.com27356e42013-06-22 04:03:03 +000056 // MIDI system.
57 // |port_index| represents the specific input port from input_ports().
58 // |data| represents a series of bytes encoding one or more MIDI messages.
59 // |length| is the number of bytes in |data|.
60 // |timestamp| is the time the data was received, in seconds.
Avi Drissman3528fd02015-12-18 20:11:31 -050061 virtual void ReceiveMidiData(uint32_t port_index,
62 const uint8_t* data,
crogers@google.com27356e42013-06-22 04:03:03 +000063 size_t length,
64 double timestamp) = 0;
crogers@google.com542a43a2013-07-31 05:16:49 +000065
toyoshim@chromium.orgc82e66e2014-02-04 07:05:47 +000066 // AccumulateMidiBytesSent() is called to acknowledge when bytes have
crogers@google.com542a43a2013-07-31 05:16:49 +000067 // successfully been sent to the hardware.
68 // This happens as a result of the client having previously called
toyoshim@chromium.orgc82e66e2014-02-04 07:05:47 +000069 // MidiManager::DispatchSendMidiData().
70 virtual void AccumulateMidiBytesSent(size_t n) = 0;
toyoshim7a809662015-10-06 00:54:21 -070071
72 // Detach() is called when MidiManager is going to shutdown immediately.
73 // Client should not touch MidiManager instance after Detach() is called.
74 virtual void Detach() = 0;
crogers@google.com27356e42013-06-22 04:03:03 +000075};
76
Takashi Toyoshima408b79f2018-01-09 20:30:07 +090077// Manages access to all MIDI hardware. MidiManager runs on the I/O thread.
78//
79// Note: We will eventually remove utility functions that are shared among
80// platform dependent MidiManager inheritances such as MidiManagerClient
81// management. MidiService should provide such shareable utility functions as
82// it does TaskService.
brettw49ff0172015-05-05 12:43:04 -070083class MIDI_EXPORT MidiManager {
crogers@google.com27356e42013-06-22 04:03:03 +000084 public:
toyoshim@chromium.orgfc2002e2014-05-07 08:10:34 +000085 static const size_t kMaxPendingClientCount = 128;
crogers@google.com27356e42013-06-22 04:03:03 +000086
toyoshimf4d61522017-02-10 02:03:32 -080087 explicit MidiManager(MidiService* service);
toyoshim@chromium.orgc82e66e2014-02-04 07:05:47 +000088 virtual ~MidiManager();
crogers@google.com27356e42013-06-22 04:03:03 +000089
toyoshimf4d61522017-02-10 02:03:32 -080090 static MidiManager* Create(MidiService* service);
toyoshim@chromium.orgfc2002e2014-05-07 08:10:34 +000091
Takashi Toyoshima408b79f2018-01-09 20:30:07 +090092 // Shuts down this manager. This function is split from the destructor
93 // because it calls a virtual function.
toyoshim7a809662015-10-06 00:54:21 -070094 void Shutdown();
95
toyoshim@chromium.org235b09e2013-07-25 16:23:14 +000096 // A client calls StartSession() to receive and send MIDI data.
97 // If the session is ready to start, the MIDI system is lazily initialized
crogers@google.com27356e42013-06-22 04:03:03 +000098 // and the client is registered to receive MIDI data.
toyoshim2f3a48f2016-10-17 01:54:13 -070099 // CompleteStartSession() is called with mojom::Result::OK if the session is
100 // started. Otherwise CompleteStartSession() is called with a proper
101 // mojom::Result code.
toyoshima485ff92014-10-23 00:17:59 -0700102 void StartSession(MidiManagerClient* client);
crogers@google.com27356e42013-06-22 04:03:03 +0000103
toyoshim@chromium.orgf77a1e62014-04-11 13:16:24 +0000104 // A client calls EndSession() to stop receiving MIDI data.
Takashi Toyoshima150b4432017-08-21 12:08:09 +0000105 // Returns false if |client| did not start a session.
106 bool EndSession(MidiManagerClient* client);
107
108 // Returns true if there is at least one client that keep a session open.
109 bool HasOpenSession();
crogers@google.com27356e42013-06-22 04:03:03 +0000110
toyoshim7dd12482015-04-07 07:02:49 -0700111 // Invoke AccumulateMidiBytesSent() for |client| safely. If the session was
112 // already closed, do nothing.
113 void AccumulateMidiBytesSent(MidiManagerClient* client, size_t n);
114
toyoshim@chromium.orgc82e66e2014-02-04 07:05:47 +0000115 // DispatchSendMidiData() is called when MIDI data should be sent to the MIDI
yukawa@chromium.orgdb7ad8b2013-12-04 15:42:41 +0000116 // system.
117 // This method is supposed to return immediately and should not block.
crogers@google.com27356e42013-06-22 04:03:03 +0000118 // |port_index| represents the specific output port from output_ports().
119 // |data| represents a series of bytes encoding one or more MIDI messages.
120 // |length| is the number of bytes in |data|.
crogers@google.com542a43a2013-07-31 05:16:49 +0000121 // |timestamp| is the time to send the data, in seconds. A value of 0
122 // means send "now" or as soon as possible.
yhirano@chromium.orgc6d5b7b2013-12-20 07:27:23 +0000123 // The default implementation is for unsupported platforms.
toyoshim@chromium.orgc82e66e2014-02-04 07:05:47 +0000124 virtual void DispatchSendMidiData(MidiManagerClient* client,
Avi Drissman3528fd02015-12-18 20:11:31 -0500125 uint32_t port_index,
126 const std::vector<uint8_t>& data,
yhirano@chromium.orgc6d5b7b2013-12-20 07:27:23 +0000127 double timestamp);
crogers@google.com27356e42013-06-22 04:03:03 +0000128
crogers@google.com27356e42013-06-22 04:03:03 +0000129 protected:
toyoshim@chromium.org51c7f532014-05-01 17:17:32 +0000130 friend class MidiManagerUsb;
131
toyoshim@chromium.orgc1e05fb2014-05-06 16:39:20 +0000132 // Initializes the platform dependent MIDI system. MidiManager class has a
133 // default implementation that synchronously calls CompleteInitialization()
Takashi Toyoshima408b79f2018-01-09 20:30:07 +0900134 // with mojom::Result::NOT_SUPPORTED. A derived class for a specific platform
135 // should override this method correctly.
toyoshim@chromium.orgc1e05fb2014-05-06 16:39:20 +0000136 // Platform dependent initialization can be processed synchronously or
137 // asynchronously. When the initialization is completed,
138 // CompleteInitialization() should be called with |result|.
toyoshim2f3a48f2016-10-17 01:54:13 -0700139 // |result| should be mojom::Result::OK on success, otherwise a proper
140 // mojom::Result.
toyoshim@chromium.org51c7f532014-05-01 17:17:32 +0000141 virtual void StartInitialization();
142
Takashi Toyoshima408b79f2018-01-09 20:30:07 +0900143 // Finalizes the platform dependent MIDI system. After this method call,
144 // destructor will be called immediately and the I/O thread may stop.
toyoshim7a809662015-10-06 00:54:21 -0700145 virtual void Finalize() {}
146
toyoshim@chromium.org51c7f532014-05-01 17:17:32 +0000147 // Called from a platform dependent implementation of StartInitialization().
Takashi Toyoshima408b79f2018-01-09 20:30:07 +0900148 // The method can be called on any thread, and it invokes
149 // CompleteInitializationOnSessionThread() on the thread that ran
toyoshim@chromium.orgc1e05fb2014-05-06 16:39:20 +0000150 // StartSession() and distributes |result| to MIDIManagerClient objects in
toyoshim@chromium.org51c7f532014-05-01 17:17:32 +0000151 // |pending_clients_|.
toyoshim2f3a48f2016-10-17 01:54:13 -0700152 void CompleteInitialization(mojom::Result result);
crogers@google.com27356e42013-06-22 04:03:03 +0000153
Takashi Toyoshima408b79f2018-01-09 20:30:07 +0900154 // The following methods can be called on any thread.
toyoshim@chromium.orgc82e66e2014-02-04 07:05:47 +0000155 void AddInputPort(const MidiPortInfo& info);
156 void AddOutputPort(const MidiPortInfo& info);
toyoshimec2570a2016-10-21 02:15:27 -0700157 void SetInputPortState(uint32_t port_index, mojom::PortState state);
158 void SetOutputPortState(uint32_t port_index, mojom::PortState state);
crogers@google.com27356e42013-06-22 04:03:03 +0000159
160 // Dispatches to all clients.
yhirano@chromium.orgcfa642c2014-05-01 08:54:41 +0000161 // TODO(toyoshim): Fix the mac implementation to use
162 // |ReceiveMidiData(..., base::TimeTicks)|.
Avi Drissman3528fd02015-12-18 20:11:31 -0500163 void ReceiveMidiData(uint32_t port_index,
164 const uint8_t* data,
toyoshim@chromium.orgae6ad362013-08-27 15:30:20 +0000165 size_t length,
166 double timestamp);
167
Avi Drissman3528fd02015-12-18 20:11:31 -0500168 void ReceiveMidiData(uint32_t port_index,
169 const uint8_t* data,
yhirano@chromium.orgcfa642c2014-05-01 08:54:41 +0000170 size_t length,
171 base::TimeTicks time) {
172 ReceiveMidiData(port_index, data, length,
173 (time - base::TimeTicks()).InSecondsF());
174 }
175
toyoshim@chromium.orga25e6832014-05-07 18:06:25 +0000176 size_t clients_size_for_testing() const { return clients_.size(); }
177 size_t pending_clients_size_for_testing() const {
toyoshim@chromium.orgc1e05fb2014-05-06 16:39:20 +0000178 return pending_clients_.size();
179 }
180
yhiranobc742d82015-09-17 07:41:44 -0700181 const MidiPortInfoList& input_ports() const { return input_ports_; }
182 const MidiPortInfoList& output_ports() const { return output_ports_; }
toyoshimf4d61522017-02-10 02:03:32 -0800183 MidiService* service() { return service_; }
yhiranobc742d82015-09-17 07:41:44 -0700184
toyoshim@chromium.orgc1e05fb2014-05-06 16:39:20 +0000185 private:
shaochuan8ba1cb62016-10-24 04:34:31 -0700186 enum class InitializationState {
187 NOT_STARTED,
188 STARTED,
189 COMPLETED,
190 };
191
Takashi Toyoshima408b79f2018-01-09 20:30:07 +0900192 void CompleteInitializationOnSessionThread(mojom::Result result);
toyoshima62d6742014-10-23 09:05:03 -0700193 void AddInitialPorts(MidiManagerClient* client);
crogers@google.com27356e42013-06-22 04:03:03 +0000194
195 // Keeps track of all clients who wish to receive MIDI data.
Takashi Toyoshima408b79f2018-01-09 20:30:07 +0900196 std::set<MidiManagerClient*> clients_;
crogers@google.com27356e42013-06-22 04:03:03 +0000197
toyoshim@chromium.org51c7f532014-05-01 17:17:32 +0000198 // Keeps track of all clients who are waiting for CompleteStartSession().
Takashi Toyoshima408b79f2018-01-09 20:30:07 +0900199 std::set<MidiManagerClient*> pending_clients_;
toyoshim@chromium.org51c7f532014-05-01 17:17:32 +0000200
toyoshim@chromium.orgc1e05fb2014-05-06 16:39:20 +0000201 // Keeps a SingleThreadTaskRunner of the thread that calls StartSession in
202 // order to invoke CompleteStartSession() on the thread.
203 scoped_refptr<base::SingleThreadTaskRunner> session_thread_runner_;
crogers@google.com27356e42013-06-22 04:03:03 +0000204
shaochuan8ba1cb62016-10-24 04:34:31 -0700205 // Tracks platform dependent initialization state.
206 InitializationState initialization_state_;
crogers@google.com27356e42013-06-22 04:03:03 +0000207
toyoshim7a809662015-10-06 00:54:21 -0700208 // Keeps false until Finalize() is called.
209 bool finalized_;
210
toyoshim@chromium.orgc1e05fb2014-05-06 16:39:20 +0000211 // Keeps the platform dependent initialization result if initialization is
toyoshim2f3a48f2016-10-17 01:54:13 -0700212 // completed. Otherwise keeps mojom::Result::NOT_INITIALIZED.
213 mojom::Result result_;
toyoshim@chromium.orgc1e05fb2014-05-06 16:39:20 +0000214
toyoshima62d6742014-10-23 09:05:03 -0700215 // Keeps all MidiPortInfo.
toyoshim@chromium.orga25e6832014-05-07 18:06:25 +0000216 MidiPortInfoList input_ports_;
217 MidiPortInfoList output_ports_;
218
Takashi Toyoshima6fb495c2017-06-27 15:25:17 +0900219 // Tracks if actual data transmission happens.
220 bool data_sent_;
221 bool data_received_;
222
Takashi Toyoshima408b79f2018-01-09 20:30:07 +0900223 // Protects members above.
toyoshima62d6742014-10-23 09:05:03 -0700224 base::Lock lock_;
225
toyoshimf4d61522017-02-10 02:03:32 -0800226 // MidiService outlives MidiManager.
227 MidiService* const service_;
228
toyoshim@chromium.orgc82e66e2014-02-04 07:05:47 +0000229 DISALLOW_COPY_AND_ASSIGN(MidiManager);
crogers@google.com27356e42013-06-22 04:03:03 +0000230};
231
toyoshime147c5e2015-05-07 21:58:31 -0700232} // namespace midi
crogers@google.com27356e42013-06-22 04:03:03 +0000233
234#endif // MEDIA_MIDI_MIDI_MANAGER_H_