crogers@google.com | 27356e4 | 2013-06-22 04:03:03 +0000 | [diff] [blame] | 1 | // 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 | |
avi | 793390d | 2015-12-22 22:22:36 -0800 | [diff] [blame] | 8 | #include <stddef.h> |
| 9 | #include <stdint.h> |
| 10 | |
crogers@google.com | 27356e4 | 2013-06-22 04:03:03 +0000 | [diff] [blame] | 11 | #include <set> |
toyoshim@chromium.org | ae6ad36 | 2013-08-27 15:30:20 +0000 | [diff] [blame] | 12 | #include <vector> |
crogers@google.com | 27356e4 | 2013-06-22 04:03:03 +0000 | [diff] [blame] | 13 | |
avi | eea95e8 | 2015-12-18 20:27:08 -0800 | [diff] [blame] | 14 | #include "base/macros.h" |
toyoshim@chromium.org | c1e05fb | 2014-05-06 16:39:20 +0000 | [diff] [blame] | 15 | #include "base/memory/ref_counted.h" |
crogers@google.com | 27356e4 | 2013-06-22 04:03:03 +0000 | [diff] [blame] | 16 | #include "base/synchronization/lock.h" |
Takashi Toyoshima | 5a6e6a3 | 2018-09-27 11:20:52 +0000 | [diff] [blame] | 17 | #include "base/thread_annotations.h" |
yhirano@chromium.org | cfa642c | 2014-05-01 08:54:41 +0000 | [diff] [blame] | 18 | #include "base/time/time.h" |
brettw | 49ff017 | 2015-05-05 12:43:04 -0700 | [diff] [blame] | 19 | #include "media/midi/midi_export.h" |
crogers@google.com | 27356e4 | 2013-06-22 04:03:03 +0000 | [diff] [blame] | 20 | #include "media/midi/midi_port_info.h" |
toyoshim | 2f3a48f | 2016-10-17 01:54:13 -0700 | [diff] [blame] | 21 | #include "media/midi/midi_service.mojom.h" |
crogers@google.com | 27356e4 | 2013-06-22 04:03:03 +0000 | [diff] [blame] | 22 | |
toyoshim@chromium.org | c1e05fb | 2014-05-06 16:39:20 +0000 | [diff] [blame] | 23 | namespace base { |
| 24 | class SingleThreadTaskRunner; |
| 25 | } // namespace base |
| 26 | |
toyoshim | e147c5e | 2015-05-07 21:58:31 -0700 | [diff] [blame] | 27 | namespace midi { |
crogers@google.com | 27356e4 | 2013-06-22 04:03:03 +0000 | [diff] [blame] | 28 | |
toyoshim | f4d6152 | 2017-02-10 02:03:32 -0800 | [diff] [blame] | 29 | class MidiService; |
| 30 | |
toyoshim@chromium.org | c82e66e | 2014-02-04 07:05:47 +0000 | [diff] [blame] | 31 | // A MidiManagerClient registers with the MidiManager to receive MIDI data. |
| 32 | // See MidiManager::RequestAccess() and MidiManager::ReleaseAccess() |
crogers@google.com | 27356e4 | 2013-06-22 04:03:03 +0000 | [diff] [blame] | 33 | // for details. |
toyoshim | 14a3234 | 2016-12-14 22:18:29 -0800 | [diff] [blame] | 34 | // TODO(toyoshim): Consider to have a MidiServiceClient interface. |
brettw | 49ff017 | 2015-05-05 12:43:04 -0700 | [diff] [blame] | 35 | class MIDI_EXPORT MidiManagerClient { |
crogers@google.com | 27356e4 | 2013-06-22 04:03:03 +0000 | [diff] [blame] | 36 | public: |
toyoshim@chromium.org | f77a1e6 | 2014-04-11 13:16:24 +0000 | [diff] [blame] | 37 | virtual ~MidiManagerClient() {} |
| 38 | |
toyoshim | a62d674 | 2014-10-23 09:05:03 -0700 | [diff] [blame] | 39 | // 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 | |
toyoshim | 5c6fe4b | 2015-02-18 23:28:09 -0800 | [diff] [blame] | 45 | // SetInputPortState() and SetOutputPortState() are called to notify a known |
| 46 | // device gets disconnected, or connected again. |
toyoshim | ec2570a | 2016-10-21 02:15:27 -0700 | [diff] [blame] | 47 | 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; |
toyoshim | a62d674 | 2014-10-23 09:05:03 -0700 | [diff] [blame] | 51 | |
toyoshim@chromium.org | f77a1e6 | 2014-04-11 13:16:24 +0000 | [diff] [blame] | 52 | // CompleteStartSession() is called when platform dependent preparation is |
| 53 | // finished. |
toyoshim | 2f3a48f | 2016-10-17 01:54:13 -0700 | [diff] [blame] | 54 | virtual void CompleteStartSession(mojom::Result result) = 0; |
crogers@google.com | 27356e4 | 2013-06-22 04:03:03 +0000 | [diff] [blame] | 55 | |
toyoshim@chromium.org | c82e66e | 2014-02-04 07:05:47 +0000 | [diff] [blame] | 56 | // ReceiveMidiData() is called when MIDI data has been received from the |
crogers@google.com | 27356e4 | 2013-06-22 04:03:03 +0000 | [diff] [blame] | 57 | // 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 Drissman | 3528fd0 | 2015-12-18 20:11:31 -0500 | [diff] [blame] | 62 | virtual void ReceiveMidiData(uint32_t port_index, |
| 63 | const uint8_t* data, |
crogers@google.com | 27356e4 | 2013-06-22 04:03:03 +0000 | [diff] [blame] | 64 | size_t length, |
tzik | 925e2c6 | 2018-02-02 07:39:45 +0000 | [diff] [blame] | 65 | base::TimeTicks timestamp) = 0; |
crogers@google.com | 542a43a | 2013-07-31 05:16:49 +0000 | [diff] [blame] | 66 | |
toyoshim@chromium.org | c82e66e | 2014-02-04 07:05:47 +0000 | [diff] [blame] | 67 | // AccumulateMidiBytesSent() is called to acknowledge when bytes have |
crogers@google.com | 542a43a | 2013-07-31 05:16:49 +0000 | [diff] [blame] | 68 | // successfully been sent to the hardware. |
| 69 | // This happens as a result of the client having previously called |
toyoshim@chromium.org | c82e66e | 2014-02-04 07:05:47 +0000 | [diff] [blame] | 70 | // MidiManager::DispatchSendMidiData(). |
| 71 | virtual void AccumulateMidiBytesSent(size_t n) = 0; |
toyoshim | 7a80966 | 2015-10-06 00:54:21 -0700 | [diff] [blame] | 72 | |
| 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.com | 27356e4 | 2013-06-22 04:03:03 +0000 | [diff] [blame] | 76 | }; |
| 77 | |
Takashi Toyoshima | 408b79f | 2018-01-09 20:30:07 +0900 | [diff] [blame] | 78 | // 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. |
brettw | 49ff017 | 2015-05-05 12:43:04 -0700 | [diff] [blame] | 84 | class MIDI_EXPORT MidiManager { |
crogers@google.com | 27356e4 | 2013-06-22 04:03:03 +0000 | [diff] [blame] | 85 | public: |
toyoshim@chromium.org | fc2002e | 2014-05-07 08:10:34 +0000 | [diff] [blame] | 86 | static const size_t kMaxPendingClientCount = 128; |
crogers@google.com | 27356e4 | 2013-06-22 04:03:03 +0000 | [diff] [blame] | 87 | |
toyoshim | f4d6152 | 2017-02-10 02:03:32 -0800 | [diff] [blame] | 88 | explicit MidiManager(MidiService* service); |
toyoshim@chromium.org | c82e66e | 2014-02-04 07:05:47 +0000 | [diff] [blame] | 89 | virtual ~MidiManager(); |
crogers@google.com | 27356e4 | 2013-06-22 04:03:03 +0000 | [diff] [blame] | 90 | |
toyoshim | f4d6152 | 2017-02-10 02:03:32 -0800 | [diff] [blame] | 91 | static MidiManager* Create(MidiService* service); |
toyoshim@chromium.org | fc2002e | 2014-05-07 08:10:34 +0000 | [diff] [blame] | 92 | |
Takashi Toyoshima | 408b79f | 2018-01-09 20:30:07 +0900 | [diff] [blame] | 93 | // Shuts down this manager. This function is split from the destructor |
| 94 | // because it calls a virtual function. |
toyoshim | 7a80966 | 2015-10-06 00:54:21 -0700 | [diff] [blame] | 95 | void Shutdown(); |
| 96 | |
toyoshim@chromium.org | 235b09e | 2013-07-25 16:23:14 +0000 | [diff] [blame] | 97 | // A client calls StartSession() to receive and send MIDI data. |
| 98 | // If the session is ready to start, the MIDI system is lazily initialized |
crogers@google.com | 27356e4 | 2013-06-22 04:03:03 +0000 | [diff] [blame] | 99 | // and the client is registered to receive MIDI data. |
toyoshim | 2f3a48f | 2016-10-17 01:54:13 -0700 | [diff] [blame] | 100 | // CompleteStartSession() is called with mojom::Result::OK if the session is |
| 101 | // started. Otherwise CompleteStartSession() is called with a proper |
| 102 | // mojom::Result code. |
toyoshim | a485ff9 | 2014-10-23 00:17:59 -0700 | [diff] [blame] | 103 | void StartSession(MidiManagerClient* client); |
crogers@google.com | 27356e4 | 2013-06-22 04:03:03 +0000 | [diff] [blame] | 104 | |
toyoshim@chromium.org | f77a1e6 | 2014-04-11 13:16:24 +0000 | [diff] [blame] | 105 | // A client calls EndSession() to stop receiving MIDI data. |
Takashi Toyoshima | 150b443 | 2017-08-21 12:08:09 +0000 | [diff] [blame] | 106 | // Returns false if |client| did not start a session. |
| 107 | bool EndSession(MidiManagerClient* client); |
| 108 | |
| 109 | // Returns true if there is at least one client that keep a session open. |
| 110 | bool HasOpenSession(); |
crogers@google.com | 27356e4 | 2013-06-22 04:03:03 +0000 | [diff] [blame] | 111 | |
toyoshim@chromium.org | c82e66e | 2014-02-04 07:05:47 +0000 | [diff] [blame] | 112 | // DispatchSendMidiData() is called when MIDI data should be sent to the MIDI |
yukawa@chromium.org | db7ad8b | 2013-12-04 15:42:41 +0000 | [diff] [blame] | 113 | // system. |
| 114 | // This method is supposed to return immediately and should not block. |
crogers@google.com | 27356e4 | 2013-06-22 04:03:03 +0000 | [diff] [blame] | 115 | // |port_index| represents the specific output port from output_ports(). |
| 116 | // |data| represents a series of bytes encoding one or more MIDI messages. |
| 117 | // |length| is the number of bytes in |data|. |
tzik | 925e2c6 | 2018-02-02 07:39:45 +0000 | [diff] [blame] | 118 | // |timestamp| is the time to send the data. A value of 0 means send "now" or |
| 119 | // as soon as possible. The default implementation is for unsupported |
| 120 | // platforms. |
toyoshim@chromium.org | c82e66e | 2014-02-04 07:05:47 +0000 | [diff] [blame] | 121 | virtual void DispatchSendMidiData(MidiManagerClient* client, |
Avi Drissman | 3528fd0 | 2015-12-18 20:11:31 -0500 | [diff] [blame] | 122 | uint32_t port_index, |
| 123 | const std::vector<uint8_t>& data, |
tzik | 925e2c6 | 2018-02-02 07:39:45 +0000 | [diff] [blame] | 124 | base::TimeTicks timestamp); |
crogers@google.com | 27356e4 | 2013-06-22 04:03:03 +0000 | [diff] [blame] | 125 | |
crogers@google.com | 27356e4 | 2013-06-22 04:03:03 +0000 | [diff] [blame] | 126 | protected: |
toyoshim@chromium.org | 51c7f53 | 2014-05-01 17:17:32 +0000 | [diff] [blame] | 127 | friend class MidiManagerUsb; |
| 128 | |
toyoshim@chromium.org | c1e05fb | 2014-05-06 16:39:20 +0000 | [diff] [blame] | 129 | // Initializes the platform dependent MIDI system. MidiManager class has a |
| 130 | // default implementation that synchronously calls CompleteInitialization() |
Takashi Toyoshima | 408b79f | 2018-01-09 20:30:07 +0900 | [diff] [blame] | 131 | // with mojom::Result::NOT_SUPPORTED. A derived class for a specific platform |
| 132 | // should override this method correctly. |
toyoshim@chromium.org | c1e05fb | 2014-05-06 16:39:20 +0000 | [diff] [blame] | 133 | // Platform dependent initialization can be processed synchronously or |
| 134 | // asynchronously. When the initialization is completed, |
| 135 | // CompleteInitialization() should be called with |result|. |
toyoshim | 2f3a48f | 2016-10-17 01:54:13 -0700 | [diff] [blame] | 136 | // |result| should be mojom::Result::OK on success, otherwise a proper |
| 137 | // mojom::Result. |
toyoshim@chromium.org | 51c7f53 | 2014-05-01 17:17:32 +0000 | [diff] [blame] | 138 | virtual void StartInitialization(); |
| 139 | |
Takashi Toyoshima | 408b79f | 2018-01-09 20:30:07 +0900 | [diff] [blame] | 140 | // Finalizes the platform dependent MIDI system. After this method call, |
| 141 | // destructor will be called immediately and the I/O thread may stop. |
toyoshim | 7a80966 | 2015-10-06 00:54:21 -0700 | [diff] [blame] | 142 | virtual void Finalize() {} |
| 143 | |
toyoshim@chromium.org | 51c7f53 | 2014-05-01 17:17:32 +0000 | [diff] [blame] | 144 | // Called from a platform dependent implementation of StartInitialization(). |
Takashi Toyoshima | 5a6e6a3 | 2018-09-27 11:20:52 +0000 | [diff] [blame] | 145 | // The method distributes |result| to MIDIManagerClient objects in |
toyoshim@chromium.org | 51c7f53 | 2014-05-01 17:17:32 +0000 | [diff] [blame] | 146 | // |pending_clients_|. |
toyoshim | 2f3a48f | 2016-10-17 01:54:13 -0700 | [diff] [blame] | 147 | void CompleteInitialization(mojom::Result result); |
crogers@google.com | 27356e4 | 2013-06-22 04:03:03 +0000 | [diff] [blame] | 148 | |
Takashi Toyoshima | 5a6e6a3 | 2018-09-27 11:20:52 +0000 | [diff] [blame] | 149 | // The following five methods can be called on any thread to notify clients of |
| 150 | // status changes on ports, or to obtain port status. |
toyoshim@chromium.org | c82e66e | 2014-02-04 07:05:47 +0000 | [diff] [blame] | 151 | void AddInputPort(const MidiPortInfo& info); |
| 152 | void AddOutputPort(const MidiPortInfo& info); |
toyoshim | ec2570a | 2016-10-21 02:15:27 -0700 | [diff] [blame] | 153 | void SetInputPortState(uint32_t port_index, mojom::PortState state); |
| 154 | void SetOutputPortState(uint32_t port_index, mojom::PortState state); |
Takashi Toyoshima | 5a6e6a3 | 2018-09-27 11:20:52 +0000 | [diff] [blame] | 155 | mojom::PortState GetOutputPortState(uint32_t port_index); |
crogers@google.com | 27356e4 | 2013-06-22 04:03:03 +0000 | [diff] [blame] | 156 | |
Takashi Toyoshima | 5a6e6a3 | 2018-09-27 11:20:52 +0000 | [diff] [blame] | 157 | // Invoke AccumulateMidiBytesSent() for |client| safely. If the session was |
| 158 | // already closed, do nothing. Can be called on any thread. |
| 159 | void AccumulateMidiBytesSent(MidiManagerClient* client, size_t n); |
| 160 | |
| 161 | // Dispatches to all clients. Can be called on any thread. |
Avi Drissman | 3528fd0 | 2015-12-18 20:11:31 -0500 | [diff] [blame] | 162 | void ReceiveMidiData(uint32_t port_index, |
| 163 | const uint8_t* data, |
toyoshim@chromium.org | ae6ad36 | 2013-08-27 15:30:20 +0000 | [diff] [blame] | 164 | size_t length, |
tzik | 925e2c6 | 2018-02-02 07:39:45 +0000 | [diff] [blame] | 165 | base::TimeTicks time); |
yhirano@chromium.org | cfa642c | 2014-05-01 08:54:41 +0000 | [diff] [blame] | 166 | |
Takashi Toyoshima | 5a6e6a3 | 2018-09-27 11:20:52 +0000 | [diff] [blame] | 167 | // Only for testing. |
Takashi Toyoshima | d4f37cd | 2018-10-01 07:43:39 +0000 | [diff] [blame^] | 168 | size_t GetClientCountForTesting(); |
| 169 | size_t GetPendingClientCountForTesting(); |
toyoshim@chromium.org | c1e05fb | 2014-05-06 16:39:20 +0000 | [diff] [blame] | 170 | |
toyoshim | f4d6152 | 2017-02-10 02:03:32 -0800 | [diff] [blame] | 171 | MidiService* service() { return service_; } |
yhirano | bc742d8 | 2015-09-17 07:41:44 -0700 | [diff] [blame] | 172 | |
toyoshim@chromium.org | c1e05fb | 2014-05-06 16:39:20 +0000 | [diff] [blame] | 173 | private: |
shaochuan | 8ba1cb6 | 2016-10-24 04:34:31 -0700 | [diff] [blame] | 174 | enum class InitializationState { |
| 175 | NOT_STARTED, |
| 176 | STARTED, |
| 177 | COMPLETED, |
| 178 | }; |
| 179 | |
Takashi Toyoshima | d4f37cd | 2018-10-01 07:43:39 +0000 | [diff] [blame^] | 180 | // Note: Members that are not protected by any lock should be accessed only on |
| 181 | // the I/O thread. |
| 182 | |
| 183 | // Tracks platform dependent initialization state. |
| 184 | InitializationState initialization_state_ = InitializationState::NOT_STARTED; |
| 185 | |
| 186 | // Keeps false until Finalize() is called. |
| 187 | bool finalized_ = false; |
| 188 | |
| 189 | // Keeps the platform dependent initialization result if initialization is |
| 190 | // completed. Otherwise keeps mojom::Result::NOT_INITIALIZED. |
| 191 | mojom::Result result_ = mojom::Result::NOT_INITIALIZED; |
crogers@google.com | 27356e4 | 2013-06-22 04:03:03 +0000 | [diff] [blame] | 192 | |
toyoshim@chromium.org | 51c7f53 | 2014-05-01 17:17:32 +0000 | [diff] [blame] | 193 | // Keeps track of all clients who are waiting for CompleteStartSession(). |
Takashi Toyoshima | 408b79f | 2018-01-09 20:30:07 +0900 | [diff] [blame] | 194 | std::set<MidiManagerClient*> pending_clients_; |
toyoshim@chromium.org | 51c7f53 | 2014-05-01 17:17:32 +0000 | [diff] [blame] | 195 | |
Takashi Toyoshima | d4f37cd | 2018-10-01 07:43:39 +0000 | [diff] [blame^] | 196 | // Keeps track of all clients who wish to receive MIDI data. |
| 197 | std::set<MidiManagerClient*> clients_ GUARDED_BY(lock_); |
| 198 | |
toyoshim@chromium.org | c1e05fb | 2014-05-06 16:39:20 +0000 | [diff] [blame] | 199 | // Keeps a SingleThreadTaskRunner of the thread that calls StartSession in |
Takashi Toyoshima | d4f37cd | 2018-10-01 07:43:39 +0000 | [diff] [blame^] | 200 | // order to invoke CompleteStartSession() on the thread. This is touched only |
| 201 | // on the IO thread usually, but to be guarded by |lock_| for thread checks. |
| 202 | scoped_refptr<base::SingleThreadTaskRunner> session_thread_runner_ |
| 203 | GUARDED_BY(lock_); |
toyoshim@chromium.org | c1e05fb | 2014-05-06 16:39:20 +0000 | [diff] [blame] | 204 | |
toyoshim | a62d674 | 2014-10-23 09:05:03 -0700 | [diff] [blame] | 205 | // Keeps all MidiPortInfo. |
Takashi Toyoshima | 5a6e6a3 | 2018-09-27 11:20:52 +0000 | [diff] [blame] | 206 | MidiPortInfoList input_ports_ GUARDED_BY(lock_); |
| 207 | MidiPortInfoList output_ports_ GUARDED_BY(lock_); |
toyoshim@chromium.org | a25e683 | 2014-05-07 18:06:25 +0000 | [diff] [blame] | 208 | |
Takashi Toyoshima | 6fb495c | 2017-06-27 15:25:17 +0900 | [diff] [blame] | 209 | // Tracks if actual data transmission happens. |
Takashi Toyoshima | d4f37cd | 2018-10-01 07:43:39 +0000 | [diff] [blame^] | 210 | bool data_sent_ GUARDED_BY(lock_) = false; |
| 211 | bool data_received_ GUARDED_BY(lock_) = false; |
Takashi Toyoshima | 6fb495c | 2017-06-27 15:25:17 +0900 | [diff] [blame] | 212 | |
Takashi Toyoshima | 408b79f | 2018-01-09 20:30:07 +0900 | [diff] [blame] | 213 | // Protects members above. |
toyoshim | a62d674 | 2014-10-23 09:05:03 -0700 | [diff] [blame] | 214 | base::Lock lock_; |
| 215 | |
toyoshim | f4d6152 | 2017-02-10 02:03:32 -0800 | [diff] [blame] | 216 | // MidiService outlives MidiManager. |
| 217 | MidiService* const service_; |
| 218 | |
toyoshim@chromium.org | c82e66e | 2014-02-04 07:05:47 +0000 | [diff] [blame] | 219 | DISALLOW_COPY_AND_ASSIGN(MidiManager); |
crogers@google.com | 27356e4 | 2013-06-22 04:03:03 +0000 | [diff] [blame] | 220 | }; |
| 221 | |
toyoshim | e147c5e | 2015-05-07 21:58:31 -0700 | [diff] [blame] | 222 | } // namespace midi |
crogers@google.com | 27356e4 | 2013-06-22 04:03:03 +0000 | [diff] [blame] | 223 | |
| 224 | #endif // MEDIA_MIDI_MIDI_MANAGER_H_ |