blob: 888ff25cc70d123fbb18cb0444173b742893d5db [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"
Takashi Toyoshima5a6e6a32018-09-27 11:20:52 +000017#include "base/thread_annotations.h"
yhirano@chromium.orgcfa642c2014-05-01 08:54:41 +000018#include "base/time/time.h"
brettw49ff0172015-05-05 12:43:04 -070019#include "media/midi/midi_export.h"
crogers@google.com27356e42013-06-22 04:03:03 +000020#include "media/midi/midi_port_info.h"
toyoshim2f3a48f2016-10-17 01:54:13 -070021#include "media/midi/midi_service.mojom.h"
crogers@google.com27356e42013-06-22 04:03:03 +000022
toyoshim@chromium.orgc1e05fb2014-05-06 16:39:20 +000023namespace base {
24class SingleThreadTaskRunner;
25} // namespace base
26
toyoshime147c5e2015-05-07 21:58:31 -070027namespace midi {
crogers@google.com27356e42013-06-22 04:03:03 +000028
toyoshimf4d61522017-02-10 02:03:32 -080029class MidiService;
30
toyoshim@chromium.orgc82e66e2014-02-04 07:05:47 +000031// A MidiManagerClient registers with the MidiManager to receive MIDI data.
32// See MidiManager::RequestAccess() and MidiManager::ReleaseAccess()
crogers@google.com27356e42013-06-22 04:03:03 +000033// for details.
toyoshim14a32342016-12-14 22:18:29 -080034// TODO(toyoshim): Consider to have a MidiServiceClient interface.
brettw49ff0172015-05-05 12:43:04 -070035class MIDI_EXPORT MidiManagerClient {
crogers@google.com27356e42013-06-22 04:03:03 +000036 public:
toyoshim@chromium.orgf77a1e62014-04-11 13:16:24 +000037 virtual ~MidiManagerClient() {}
38
toyoshima62d6742014-10-23 09:05:03 -070039 // AddInputPort() and AddOutputPort() are called before CompleteStartSession()
40 // is called to notify existing MIDI ports, and also called after that to
41 // notify new MIDI ports are added.
42 virtual void AddInputPort(const MidiPortInfo& info) = 0;
43 virtual void AddOutputPort(const MidiPortInfo& info) = 0;
44
toyoshim5c6fe4b2015-02-18 23:28:09 -080045 // SetInputPortState() and SetOutputPortState() are called to notify a known
46 // device gets disconnected, or connected again.
toyoshimec2570a2016-10-21 02:15:27 -070047 virtual void SetInputPortState(uint32_t port_index,
48 mojom::PortState state) = 0;
49 virtual void SetOutputPortState(uint32_t port_index,
50 mojom::PortState state) = 0;
toyoshima62d6742014-10-23 09:05:03 -070051
toyoshim@chromium.orgf77a1e62014-04-11 13:16:24 +000052 // CompleteStartSession() is called when platform dependent preparation is
53 // finished.
toyoshim2f3a48f2016-10-17 01:54:13 -070054 virtual void CompleteStartSession(mojom::Result result) = 0;
crogers@google.com27356e42013-06-22 04:03:03 +000055
toyoshim@chromium.orgc82e66e2014-02-04 07:05:47 +000056 // ReceiveMidiData() is called when MIDI data has been received from the
crogers@google.com27356e42013-06-22 04:03:03 +000057 // MIDI system.
58 // |port_index| represents the specific input port from input_ports().
59 // |data| represents a series of bytes encoding one or more MIDI messages.
60 // |length| is the number of bytes in |data|.
61 // |timestamp| is the time the data was received, in seconds.
Avi Drissman3528fd02015-12-18 20:11:31 -050062 virtual void ReceiveMidiData(uint32_t port_index,
63 const uint8_t* data,
crogers@google.com27356e42013-06-22 04:03:03 +000064 size_t length,
tzik925e2c62018-02-02 07:39:45 +000065 base::TimeTicks timestamp) = 0;
crogers@google.com542a43a2013-07-31 05:16:49 +000066
toyoshim@chromium.orgc82e66e2014-02-04 07:05:47 +000067 // AccumulateMidiBytesSent() is called to acknowledge when bytes have
crogers@google.com542a43a2013-07-31 05:16:49 +000068 // successfully been sent to the hardware.
69 // This happens as a result of the client having previously called
toyoshim@chromium.orgc82e66e2014-02-04 07:05:47 +000070 // MidiManager::DispatchSendMidiData().
71 virtual void AccumulateMidiBytesSent(size_t n) = 0;
toyoshim7a809662015-10-06 00:54:21 -070072
73 // Detach() is called when MidiManager is going to shutdown immediately.
74 // Client should not touch MidiManager instance after Detach() is called.
75 virtual void Detach() = 0;
crogers@google.com27356e42013-06-22 04:03:03 +000076};
77
Takashi Toyoshima408b79f2018-01-09 20:30:07 +090078// Manages access to all MIDI hardware. MidiManager runs on the I/O thread.
79//
80// Note: We will eventually remove utility functions that are shared among
81// platform dependent MidiManager inheritances such as MidiManagerClient
82// management. MidiService should provide such shareable utility functions as
83// it does TaskService.
brettw49ff0172015-05-05 12:43:04 -070084class MIDI_EXPORT MidiManager {
crogers@google.com27356e42013-06-22 04:03:03 +000085 public:
toyoshim@chromium.orgfc2002e2014-05-07 08:10:34 +000086 static const size_t kMaxPendingClientCount = 128;
crogers@google.com27356e42013-06-22 04:03:03 +000087
toyoshimf4d61522017-02-10 02:03:32 -080088 explicit MidiManager(MidiService* service);
toyoshim@chromium.orgc82e66e2014-02-04 07:05:47 +000089 virtual ~MidiManager();
crogers@google.com27356e42013-06-22 04:03:03 +000090
toyoshimf4d61522017-02-10 02:03:32 -080091 static MidiManager* Create(MidiService* service);
toyoshim@chromium.orgfc2002e2014-05-07 08:10:34 +000092
toyoshim@chromium.org235b09e2013-07-25 16:23:14 +000093 // A client calls StartSession() to receive and send MIDI data.
94 // If the session is ready to start, the MIDI system is lazily initialized
crogers@google.com27356e42013-06-22 04:03:03 +000095 // and the client is registered to receive MIDI data.
toyoshim2f3a48f2016-10-17 01:54:13 -070096 // CompleteStartSession() is called with mojom::Result::OK if the session is
97 // started. Otherwise CompleteStartSession() is called with a proper
98 // mojom::Result code.
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.
Takashi Toyoshima150b4432017-08-21 12:08:09 +0000102 // Returns false if |client| did not start a session.
103 bool EndSession(MidiManagerClient* client);
104
105 // Returns true if there is at least one client that keep a session open.
106 bool HasOpenSession();
crogers@google.com27356e42013-06-22 04:03:03 +0000107
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|.
tzik925e2c62018-02-02 07:39:45 +0000114 // |timestamp| is the time to send the data. A value of 0 means send "now" or
115 // as soon as possible. The default implementation is for unsupported
116 // 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,
tzik925e2c62018-02-02 07:39:45 +0000120 base::TimeTicks 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()
Takashi Toyoshima408b79f2018-01-09 20:30:07 +0900127 // with mojom::Result::NOT_SUPPORTED. A derived class for a specific platform
128 // should override this method correctly.
toyoshim@chromium.orgc1e05fb2014-05-06 16:39:20 +0000129 // Platform dependent initialization can be processed synchronously or
130 // asynchronously. When the initialization is completed,
131 // CompleteInitialization() should be called with |result|.
toyoshim2f3a48f2016-10-17 01:54:13 -0700132 // |result| should be mojom::Result::OK on success, otherwise a proper
133 // mojom::Result.
toyoshim@chromium.org51c7f532014-05-01 17:17:32 +0000134 virtual void StartInitialization();
135
136 // Called from a platform dependent implementation of StartInitialization().
Takashi Toyoshima5a6e6a32018-09-27 11:20:52 +0000137 // The method distributes |result| to MIDIManagerClient objects in
toyoshim@chromium.org51c7f532014-05-01 17:17:32 +0000138 // |pending_clients_|.
toyoshim2f3a48f2016-10-17 01:54:13 -0700139 void CompleteInitialization(mojom::Result result);
crogers@google.com27356e42013-06-22 04:03:03 +0000140
Takashi Toyoshima5a6e6a32018-09-27 11:20:52 +0000141 // The following five methods can be called on any thread to notify clients of
142 // status changes on ports, or to obtain port status.
toyoshim@chromium.orgc82e66e2014-02-04 07:05:47 +0000143 void AddInputPort(const MidiPortInfo& info);
144 void AddOutputPort(const MidiPortInfo& info);
toyoshimec2570a2016-10-21 02:15:27 -0700145 void SetInputPortState(uint32_t port_index, mojom::PortState state);
146 void SetOutputPortState(uint32_t port_index, mojom::PortState state);
Takashi Toyoshima5a6e6a32018-09-27 11:20:52 +0000147 mojom::PortState GetOutputPortState(uint32_t port_index);
crogers@google.com27356e42013-06-22 04:03:03 +0000148
Takashi Toyoshima5a6e6a32018-09-27 11:20:52 +0000149 // Invoke AccumulateMidiBytesSent() for |client| safely. If the session was
150 // already closed, do nothing. Can be called on any thread.
151 void AccumulateMidiBytesSent(MidiManagerClient* client, size_t n);
152
153 // Dispatches to all clients. Can be called on any thread.
Avi Drissman3528fd02015-12-18 20:11:31 -0500154 void ReceiveMidiData(uint32_t port_index,
155 const uint8_t* data,
toyoshim@chromium.orgae6ad362013-08-27 15:30:20 +0000156 size_t length,
tzik925e2c62018-02-02 07:39:45 +0000157 base::TimeTicks time);
yhirano@chromium.orgcfa642c2014-05-01 08:54:41 +0000158
Takashi Toyoshima5a6e6a32018-09-27 11:20:52 +0000159 // Only for testing.
Takashi Toyoshimad4f37cd2018-10-01 07:43:39 +0000160 size_t GetClientCountForTesting();
161 size_t GetPendingClientCountForTesting();
toyoshim@chromium.orgc1e05fb2014-05-06 16:39:20 +0000162
toyoshimf4d61522017-02-10 02:03:32 -0800163 MidiService* service() { return service_; }
yhiranobc742d82015-09-17 07:41:44 -0700164
toyoshim@chromium.orgc1e05fb2014-05-06 16:39:20 +0000165 private:
shaochuan8ba1cb62016-10-24 04:34:31 -0700166 enum class InitializationState {
167 NOT_STARTED,
168 STARTED,
169 COMPLETED,
170 };
171
Takashi Toyoshimad4f37cd2018-10-01 07:43:39 +0000172 // Note: Members that are not protected by any lock should be accessed only on
173 // the I/O thread.
174
175 // Tracks platform dependent initialization state.
176 InitializationState initialization_state_ = InitializationState::NOT_STARTED;
177
Takashi Toyoshimad4f37cd2018-10-01 07:43:39 +0000178 // Keeps the platform dependent initialization result if initialization is
179 // completed. Otherwise keeps mojom::Result::NOT_INITIALIZED.
180 mojom::Result result_ = mojom::Result::NOT_INITIALIZED;
crogers@google.com27356e42013-06-22 04:03:03 +0000181
toyoshim@chromium.org51c7f532014-05-01 17:17:32 +0000182 // Keeps track of all clients who are waiting for CompleteStartSession().
Takashi Toyoshima408b79f2018-01-09 20:30:07 +0900183 std::set<MidiManagerClient*> pending_clients_;
toyoshim@chromium.org51c7f532014-05-01 17:17:32 +0000184
Takashi Toyoshimad4f37cd2018-10-01 07:43:39 +0000185 // Keeps track of all clients who wish to receive MIDI data.
186 std::set<MidiManagerClient*> clients_ GUARDED_BY(lock_);
187
toyoshim@chromium.orgc1e05fb2014-05-06 16:39:20 +0000188 // Keeps a SingleThreadTaskRunner of the thread that calls StartSession in
Takashi Toyoshimad4f37cd2018-10-01 07:43:39 +0000189 // order to invoke CompleteStartSession() on the thread. This is touched only
190 // on the IO thread usually, but to be guarded by |lock_| for thread checks.
191 scoped_refptr<base::SingleThreadTaskRunner> session_thread_runner_
192 GUARDED_BY(lock_);
toyoshim@chromium.orgc1e05fb2014-05-06 16:39:20 +0000193
toyoshima62d6742014-10-23 09:05:03 -0700194 // Keeps all MidiPortInfo.
Takashi Toyoshima5a6e6a32018-09-27 11:20:52 +0000195 MidiPortInfoList input_ports_ GUARDED_BY(lock_);
196 MidiPortInfoList output_ports_ GUARDED_BY(lock_);
toyoshim@chromium.orga25e6832014-05-07 18:06:25 +0000197
Takashi Toyoshima6fb495c2017-06-27 15:25:17 +0900198 // Tracks if actual data transmission happens.
Takashi Toyoshimad4f37cd2018-10-01 07:43:39 +0000199 bool data_sent_ GUARDED_BY(lock_) = false;
200 bool data_received_ GUARDED_BY(lock_) = false;
Takashi Toyoshima6fb495c2017-06-27 15:25:17 +0900201
Takashi Toyoshima408b79f2018-01-09 20:30:07 +0900202 // Protects members above.
toyoshima62d6742014-10-23 09:05:03 -0700203 base::Lock lock_;
204
toyoshimf4d61522017-02-10 02:03:32 -0800205 // MidiService outlives MidiManager.
206 MidiService* const service_;
207
toyoshim@chromium.orgc82e66e2014-02-04 07:05:47 +0000208 DISALLOW_COPY_AND_ASSIGN(MidiManager);
crogers@google.com27356e42013-06-22 04:03:03 +0000209};
210
toyoshime147c5e2015-05-07 21:58:31 -0700211} // namespace midi
crogers@google.com27356e42013-06-22 04:03:03 +0000212
213#endif // MEDIA_MIDI_MIDI_MANAGER_H_