blob: 27e24f92cd9c86937622e5f311977d5e59be2252 [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
toyoshim@chromium.orgc82e66e2014-02-04 07:05:47 +000028// A MidiManagerClient registers with the MidiManager to receive MIDI data.
29// See MidiManager::RequestAccess() and MidiManager::ReleaseAccess()
crogers@google.com27356e42013-06-22 04:03:03 +000030// for details.
toyoshim14a32342016-12-14 22:18:29 -080031// TODO(toyoshim): Consider to have a MidiServiceClient interface.
brettw49ff0172015-05-05 12:43:04 -070032class MIDI_EXPORT MidiManagerClient {
crogers@google.com27356e42013-06-22 04:03:03 +000033 public:
toyoshim@chromium.orgf77a1e62014-04-11 13:16:24 +000034 virtual ~MidiManagerClient() {}
35
toyoshima62d6742014-10-23 09:05:03 -070036 // AddInputPort() and AddOutputPort() are called before CompleteStartSession()
37 // is called to notify existing MIDI ports, and also called after that to
38 // notify new MIDI ports are added.
39 virtual void AddInputPort(const MidiPortInfo& info) = 0;
40 virtual void AddOutputPort(const MidiPortInfo& info) = 0;
41
toyoshim5c6fe4b2015-02-18 23:28:09 -080042 // SetInputPortState() and SetOutputPortState() are called to notify a known
43 // device gets disconnected, or connected again.
toyoshimec2570a2016-10-21 02:15:27 -070044 virtual void SetInputPortState(uint32_t port_index,
45 mojom::PortState state) = 0;
46 virtual void SetOutputPortState(uint32_t port_index,
47 mojom::PortState state) = 0;
toyoshima62d6742014-10-23 09:05:03 -070048
toyoshim@chromium.orgf77a1e62014-04-11 13:16:24 +000049 // CompleteStartSession() is called when platform dependent preparation is
50 // finished.
toyoshim2f3a48f2016-10-17 01:54:13 -070051 virtual void CompleteStartSession(mojom::Result result) = 0;
crogers@google.com27356e42013-06-22 04:03:03 +000052
toyoshim@chromium.orgc82e66e2014-02-04 07:05:47 +000053 // ReceiveMidiData() is called when MIDI data has been received from the
crogers@google.com27356e42013-06-22 04:03:03 +000054 // MIDI system.
55 // |port_index| represents the specific input port from input_ports().
56 // |data| represents a series of bytes encoding one or more MIDI messages.
57 // |length| is the number of bytes in |data|.
58 // |timestamp| is the time the data was received, in seconds.
Avi Drissman3528fd02015-12-18 20:11:31 -050059 virtual void ReceiveMidiData(uint32_t port_index,
60 const uint8_t* data,
crogers@google.com27356e42013-06-22 04:03:03 +000061 size_t length,
62 double timestamp) = 0;
crogers@google.com542a43a2013-07-31 05:16:49 +000063
toyoshim@chromium.orgc82e66e2014-02-04 07:05:47 +000064 // AccumulateMidiBytesSent() is called to acknowledge when bytes have
crogers@google.com542a43a2013-07-31 05:16:49 +000065 // successfully been sent to the hardware.
66 // This happens as a result of the client having previously called
toyoshim@chromium.orgc82e66e2014-02-04 07:05:47 +000067 // MidiManager::DispatchSendMidiData().
68 virtual void AccumulateMidiBytesSent(size_t n) = 0;
toyoshim7a809662015-10-06 00:54:21 -070069
70 // Detach() is called when MidiManager is going to shutdown immediately.
71 // Client should not touch MidiManager instance after Detach() is called.
72 virtual void Detach() = 0;
crogers@google.com27356e42013-06-22 04:03:03 +000073};
74
75// Manages access to all MIDI hardware.
brettw49ff0172015-05-05 12:43:04 -070076class MIDI_EXPORT MidiManager {
crogers@google.com27356e42013-06-22 04:03:03 +000077 public:
toyoshim@chromium.orgfc2002e2014-05-07 08:10:34 +000078 static const size_t kMaxPendingClientCount = 128;
crogers@google.com27356e42013-06-22 04:03:03 +000079
toyoshim@chromium.orgc82e66e2014-02-04 07:05:47 +000080 MidiManager();
81 virtual ~MidiManager();
crogers@google.com27356e42013-06-22 04:03:03 +000082
toyoshim@chromium.orgfc2002e2014-05-07 08:10:34 +000083 // The constructor and the destructor will be called on the CrBrowserMain
84 // thread.
85 static MidiManager* Create();
86
toyoshim7a809662015-10-06 00:54:21 -070087 // Called on the CrBrowserMain thread to notify the Chrome_IOThread will stop
88 // and the instance will be destructed on the CrBrowserMain thread soon.
89 void Shutdown();
90
toyoshim@chromium.org235b09e2013-07-25 16:23:14 +000091 // A client calls StartSession() to receive and send MIDI data.
92 // If the session is ready to start, the MIDI system is lazily initialized
crogers@google.com27356e42013-06-22 04:03:03 +000093 // and the client is registered to receive MIDI data.
toyoshim2f3a48f2016-10-17 01:54:13 -070094 // CompleteStartSession() is called with mojom::Result::OK if the session is
95 // started. Otherwise CompleteStartSession() is called with a proper
96 // mojom::Result code.
toyoshim@chromium.orgc1e05fb2014-05-06 16:39:20 +000097 // StartSession() and EndSession() can be called on the Chrome_IOThread.
98 // CompleteStartSession() will be invoked on the same Chrome_IOThread.
toyoshima485ff92014-10-23 00:17:59 -070099 void StartSession(MidiManagerClient* client);
crogers@google.com27356e42013-06-22 04:03:03 +0000100
toyoshim@chromium.orgf77a1e62014-04-11 13:16:24 +0000101 // A client calls EndSession() to stop receiving MIDI data.
toyoshim@chromium.orgc82e66e2014-02-04 07:05:47 +0000102 void EndSession(MidiManagerClient* client);
crogers@google.com27356e42013-06-22 04:03:03 +0000103
toyoshim7dd12482015-04-07 07:02:49 -0700104 // Invoke AccumulateMidiBytesSent() for |client| safely. If the session was
105 // already closed, do nothing.
106 void AccumulateMidiBytesSent(MidiManagerClient* client, size_t n);
107
toyoshim@chromium.orgc82e66e2014-02-04 07:05:47 +0000108 // DispatchSendMidiData() is called when MIDI data should be sent to the MIDI
yukawa@chromium.orgdb7ad8b2013-12-04 15:42:41 +0000109 // system.
110 // This method is supposed to return immediately and should not block.
crogers@google.com27356e42013-06-22 04:03:03 +0000111 // |port_index| represents the specific output port from output_ports().
112 // |data| represents a series of bytes encoding one or more MIDI messages.
113 // |length| is the number of bytes in |data|.
crogers@google.com542a43a2013-07-31 05:16:49 +0000114 // |timestamp| is the time to send the data, in seconds. A value of 0
115 // means send "now" or as soon as possible.
yhirano@chromium.orgc6d5b7b2013-12-20 07:27:23 +0000116 // The default implementation is for unsupported platforms.
toyoshim@chromium.orgc82e66e2014-02-04 07:05:47 +0000117 virtual void DispatchSendMidiData(MidiManagerClient* client,
Avi Drissman3528fd02015-12-18 20:11:31 -0500118 uint32_t port_index,
119 const std::vector<uint8_t>& data,
yhirano@chromium.orgc6d5b7b2013-12-20 07:27:23 +0000120 double timestamp);
crogers@google.com27356e42013-06-22 04:03:03 +0000121
crogers@google.com27356e42013-06-22 04:03:03 +0000122 protected:
toyoshim@chromium.org51c7f532014-05-01 17:17:32 +0000123 friend class MidiManagerUsb;
124
toyoshim@chromium.orgc1e05fb2014-05-06 16:39:20 +0000125 // Initializes the platform dependent MIDI system. MidiManager class has a
126 // default implementation that synchronously calls CompleteInitialization()
toyoshim2f3a48f2016-10-17 01:54:13 -0700127 // with mojom::Result::NOT_SUPPORTED on the caller thread. A derived class for
128 // a specific platform should override this method correctly.
toyoshim@chromium.orgc1e05fb2014-05-06 16:39:20 +0000129 // This method is called on Chrome_IOThread thread inside StartSession().
130 // Platform dependent initialization can be processed synchronously or
131 // asynchronously. When the initialization is completed,
132 // CompleteInitialization() should be called with |result|.
toyoshim2f3a48f2016-10-17 01:54:13 -0700133 // |result| should be mojom::Result::OK on success, otherwise a proper
134 // mojom::Result.
toyoshim@chromium.org51c7f532014-05-01 17:17:32 +0000135 virtual void StartInitialization();
136
toyoshim7a809662015-10-06 00:54:21 -0700137 // Finalizes the platform dependent MIDI system. Called on Chrome_IOThread
138 // thread and the thread will stop immediately after this call.
139 // Platform dependent resources that were allocated on the Chrome_IOThread
140 // should be disposed inside this method.
141 virtual void Finalize() {}
142
toyoshim@chromium.org51c7f532014-05-01 17:17:32 +0000143 // Called from a platform dependent implementation of StartInitialization().
toyoshim@chromium.orgc1e05fb2014-05-06 16:39:20 +0000144 // It invokes CompleteInitializationInternal() on the thread that calls
145 // StartSession() and distributes |result| to MIDIManagerClient objects in
toyoshim@chromium.org51c7f532014-05-01 17:17:32 +0000146 // |pending_clients_|.
toyoshim2f3a48f2016-10-17 01:54:13 -0700147 void CompleteInitialization(mojom::Result result);
crogers@google.com27356e42013-06-22 04:03:03 +0000148
toyoshim@chromium.orgc82e66e2014-02-04 07:05:47 +0000149 void AddInputPort(const MidiPortInfo& info);
150 void AddOutputPort(const MidiPortInfo& info);
toyoshimec2570a2016-10-21 02:15:27 -0700151 void SetInputPortState(uint32_t port_index, mojom::PortState state);
152 void SetOutputPortState(uint32_t port_index, mojom::PortState state);
crogers@google.com27356e42013-06-22 04:03:03 +0000153
154 // Dispatches to all clients.
yhirano@chromium.orgcfa642c2014-05-01 08:54:41 +0000155 // TODO(toyoshim): Fix the mac implementation to use
156 // |ReceiveMidiData(..., base::TimeTicks)|.
Avi Drissman3528fd02015-12-18 20:11:31 -0500157 void ReceiveMidiData(uint32_t port_index,
158 const uint8_t* data,
toyoshim@chromium.orgae6ad362013-08-27 15:30:20 +0000159 size_t length,
160 double timestamp);
161
Avi Drissman3528fd02015-12-18 20:11:31 -0500162 void ReceiveMidiData(uint32_t port_index,
163 const uint8_t* data,
yhirano@chromium.orgcfa642c2014-05-01 08:54:41 +0000164 size_t length,
165 base::TimeTicks time) {
166 ReceiveMidiData(port_index, data, length,
167 (time - base::TimeTicks()).InSecondsF());
168 }
169
toyoshim@chromium.orga25e6832014-05-07 18:06:25 +0000170 size_t clients_size_for_testing() const { return clients_.size(); }
171 size_t pending_clients_size_for_testing() const {
toyoshim@chromium.orgc1e05fb2014-05-06 16:39:20 +0000172 return pending_clients_.size();
173 }
174
yhiranobc742d82015-09-17 07:41:44 -0700175 const MidiPortInfoList& input_ports() const { return input_ports_; }
176 const MidiPortInfoList& output_ports() const { return output_ports_; }
177
toyoshim@chromium.orgc1e05fb2014-05-06 16:39:20 +0000178 private:
shaochuan8ba1cb62016-10-24 04:34:31 -0700179 enum class InitializationState {
180 NOT_STARTED,
181 STARTED,
182 COMPLETED,
183 };
184
toyoshim2f3a48f2016-10-17 01:54:13 -0700185 void CompleteInitializationInternal(mojom::Result result);
toyoshima62d6742014-10-23 09:05:03 -0700186 void AddInitialPorts(MidiManagerClient* client);
toyoshim7a809662015-10-06 00:54:21 -0700187 void ShutdownOnSessionThread();
crogers@google.com27356e42013-06-22 04:03:03 +0000188
189 // Keeps track of all clients who wish to receive MIDI data.
toyoshima485ff92014-10-23 00:17:59 -0700190 typedef std::set<MidiManagerClient*> ClientSet;
191 ClientSet clients_;
crogers@google.com27356e42013-06-22 04:03:03 +0000192
toyoshim@chromium.org51c7f532014-05-01 17:17:32 +0000193 // Keeps track of all clients who are waiting for CompleteStartSession().
toyoshima485ff92014-10-23 00:17:59 -0700194 ClientSet pending_clients_;
toyoshim@chromium.org51c7f532014-05-01 17:17:32 +0000195
toyoshim@chromium.orgc1e05fb2014-05-06 16:39:20 +0000196 // Keeps a SingleThreadTaskRunner of the thread that calls StartSession in
197 // order to invoke CompleteStartSession() on the thread.
198 scoped_refptr<base::SingleThreadTaskRunner> session_thread_runner_;
crogers@google.com27356e42013-06-22 04:03:03 +0000199
shaochuan8ba1cb62016-10-24 04:34:31 -0700200 // Tracks platform dependent initialization state.
201 InitializationState initialization_state_;
crogers@google.com27356e42013-06-22 04:03:03 +0000202
toyoshim7a809662015-10-06 00:54:21 -0700203 // Keeps false until Finalize() is called.
204 bool finalized_;
205
toyoshim@chromium.orgc1e05fb2014-05-06 16:39:20 +0000206 // Keeps the platform dependent initialization result if initialization is
toyoshim2f3a48f2016-10-17 01:54:13 -0700207 // completed. Otherwise keeps mojom::Result::NOT_INITIALIZED.
208 mojom::Result result_;
toyoshim@chromium.orgc1e05fb2014-05-06 16:39:20 +0000209
toyoshima62d6742014-10-23 09:05:03 -0700210 // Keeps all MidiPortInfo.
toyoshim@chromium.orga25e6832014-05-07 18:06:25 +0000211 MidiPortInfoList input_ports_;
212 MidiPortInfoList output_ports_;
213
toyoshim7a809662015-10-06 00:54:21 -0700214 // Protects access to |clients_|, |pending_clients_|,
shaochuan8ba1cb62016-10-24 04:34:31 -0700215 // |session_thread_runner_|, |initialization_state_|, |finalize_|, |result_|,
toyoshim7a809662015-10-06 00:54:21 -0700216 // |input_ports_| and |output_ports_|.
toyoshima62d6742014-10-23 09:05:03 -0700217 base::Lock lock_;
218
toyoshim@chromium.orgc82e66e2014-02-04 07:05:47 +0000219 DISALLOW_COPY_AND_ASSIGN(MidiManager);
crogers@google.com27356e42013-06-22 04:03:03 +0000220};
221
toyoshime147c5e2015-05-07 21:58:31 -0700222} // namespace midi
crogers@google.com27356e42013-06-22 04:03:03 +0000223
224#endif // MEDIA_MIDI_MIDI_MANAGER_H_