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 | |
| 8 | #include <set> |
toyoshim@chromium.org | ae6ad36 | 2013-08-27 15:30:20 +0000 | [diff] [blame] | 9 | #include <vector> |
crogers@google.com | 27356e4 | 2013-06-22 04:03:03 +0000 | [diff] [blame] | 10 | |
toyoshim@chromium.org | c1e05fb | 2014-05-06 16:39:20 +0000 | [diff] [blame] | 11 | #include "base/memory/ref_counted.h" |
crogers@google.com | 27356e4 | 2013-06-22 04:03:03 +0000 | [diff] [blame] | 12 | #include "base/synchronization/lock.h" |
yhirano@chromium.org | cfa642c | 2014-05-01 08:54:41 +0000 | [diff] [blame] | 13 | #include "base/time/time.h" |
brettw | 49ff017 | 2015-05-05 12:43:04 -0700 | [diff] [blame] | 14 | #include "media/midi/midi_export.h" |
crogers@google.com | 27356e4 | 2013-06-22 04:03:03 +0000 | [diff] [blame] | 15 | #include "media/midi/midi_port_info.h" |
toyoshim | f1b8896 | 2015-07-09 14:14:51 -0700 | [diff] [blame] | 16 | #include "media/midi/result.h" |
crogers@google.com | 27356e4 | 2013-06-22 04:03:03 +0000 | [diff] [blame] | 17 | |
toyoshim@chromium.org | c1e05fb | 2014-05-06 16:39:20 +0000 | [diff] [blame] | 18 | namespace base { |
| 19 | class SingleThreadTaskRunner; |
| 20 | } // namespace base |
| 21 | |
crogers@google.com | 27356e4 | 2013-06-22 04:03:03 +0000 | [diff] [blame] | 22 | namespace media { |
toyoshim | e147c5e | 2015-05-07 21:58:31 -0700 | [diff] [blame] | 23 | namespace midi { |
crogers@google.com | 27356e4 | 2013-06-22 04:03:03 +0000 | [diff] [blame] | 24 | |
toyoshim@chromium.org | c82e66e | 2014-02-04 07:05:47 +0000 | [diff] [blame] | 25 | // A MidiManagerClient registers with the MidiManager to receive MIDI data. |
| 26 | // See MidiManager::RequestAccess() and MidiManager::ReleaseAccess() |
crogers@google.com | 27356e4 | 2013-06-22 04:03:03 +0000 | [diff] [blame] | 27 | // for details. |
brettw | 49ff017 | 2015-05-05 12:43:04 -0700 | [diff] [blame] | 28 | class MIDI_EXPORT MidiManagerClient { |
crogers@google.com | 27356e4 | 2013-06-22 04:03:03 +0000 | [diff] [blame] | 29 | public: |
toyoshim@chromium.org | f77a1e6 | 2014-04-11 13:16:24 +0000 | [diff] [blame] | 30 | virtual ~MidiManagerClient() {} |
| 31 | |
toyoshim | a62d674 | 2014-10-23 09:05:03 -0700 | [diff] [blame] | 32 | // AddInputPort() and AddOutputPort() are called before CompleteStartSession() |
| 33 | // is called to notify existing MIDI ports, and also called after that to |
| 34 | // notify new MIDI ports are added. |
| 35 | virtual void AddInputPort(const MidiPortInfo& info) = 0; |
| 36 | virtual void AddOutputPort(const MidiPortInfo& info) = 0; |
| 37 | |
toyoshim | 5c6fe4b | 2015-02-18 23:28:09 -0800 | [diff] [blame] | 38 | // SetInputPortState() and SetOutputPortState() are called to notify a known |
| 39 | // device gets disconnected, or connected again. |
Avi Drissman | 3528fd0 | 2015-12-18 20:11:31 -0500 | [diff] [blame^] | 40 | virtual void SetInputPortState(uint32_t port_index, MidiPortState state) = 0; |
| 41 | virtual void SetOutputPortState(uint32_t port_index, MidiPortState state) = 0; |
toyoshim | a62d674 | 2014-10-23 09:05:03 -0700 | [diff] [blame] | 42 | |
toyoshim@chromium.org | f77a1e6 | 2014-04-11 13:16:24 +0000 | [diff] [blame] | 43 | // CompleteStartSession() is called when platform dependent preparation is |
| 44 | // finished. |
toyoshim | f1b8896 | 2015-07-09 14:14:51 -0700 | [diff] [blame] | 45 | virtual void CompleteStartSession(Result result) = 0; |
crogers@google.com | 27356e4 | 2013-06-22 04:03:03 +0000 | [diff] [blame] | 46 | |
toyoshim@chromium.org | c82e66e | 2014-02-04 07:05:47 +0000 | [diff] [blame] | 47 | // ReceiveMidiData() is called when MIDI data has been received from the |
crogers@google.com | 27356e4 | 2013-06-22 04:03:03 +0000 | [diff] [blame] | 48 | // MIDI system. |
| 49 | // |port_index| represents the specific input port from input_ports(). |
| 50 | // |data| represents a series of bytes encoding one or more MIDI messages. |
| 51 | // |length| is the number of bytes in |data|. |
| 52 | // |timestamp| is the time the data was received, in seconds. |
Avi Drissman | 3528fd0 | 2015-12-18 20:11:31 -0500 | [diff] [blame^] | 53 | virtual void ReceiveMidiData(uint32_t port_index, |
| 54 | const uint8_t* data, |
crogers@google.com | 27356e4 | 2013-06-22 04:03:03 +0000 | [diff] [blame] | 55 | size_t length, |
| 56 | double timestamp) = 0; |
crogers@google.com | 542a43a | 2013-07-31 05:16:49 +0000 | [diff] [blame] | 57 | |
toyoshim@chromium.org | c82e66e | 2014-02-04 07:05:47 +0000 | [diff] [blame] | 58 | // AccumulateMidiBytesSent() is called to acknowledge when bytes have |
crogers@google.com | 542a43a | 2013-07-31 05:16:49 +0000 | [diff] [blame] | 59 | // successfully been sent to the hardware. |
| 60 | // This happens as a result of the client having previously called |
toyoshim@chromium.org | c82e66e | 2014-02-04 07:05:47 +0000 | [diff] [blame] | 61 | // MidiManager::DispatchSendMidiData(). |
| 62 | virtual void AccumulateMidiBytesSent(size_t n) = 0; |
toyoshim | 7a80966 | 2015-10-06 00:54:21 -0700 | [diff] [blame] | 63 | |
| 64 | // Detach() is called when MidiManager is going to shutdown immediately. |
| 65 | // Client should not touch MidiManager instance after Detach() is called. |
| 66 | virtual void Detach() = 0; |
crogers@google.com | 27356e4 | 2013-06-22 04:03:03 +0000 | [diff] [blame] | 67 | }; |
| 68 | |
| 69 | // Manages access to all MIDI hardware. |
brettw | 49ff017 | 2015-05-05 12:43:04 -0700 | [diff] [blame] | 70 | class MIDI_EXPORT MidiManager { |
crogers@google.com | 27356e4 | 2013-06-22 04:03:03 +0000 | [diff] [blame] | 71 | public: |
toyoshim@chromium.org | fc2002e | 2014-05-07 08:10:34 +0000 | [diff] [blame] | 72 | static const size_t kMaxPendingClientCount = 128; |
crogers@google.com | 27356e4 | 2013-06-22 04:03:03 +0000 | [diff] [blame] | 73 | |
toyoshim@chromium.org | c82e66e | 2014-02-04 07:05:47 +0000 | [diff] [blame] | 74 | MidiManager(); |
| 75 | virtual ~MidiManager(); |
crogers@google.com | 27356e4 | 2013-06-22 04:03:03 +0000 | [diff] [blame] | 76 | |
toyoshim@chromium.org | fc2002e | 2014-05-07 08:10:34 +0000 | [diff] [blame] | 77 | // The constructor and the destructor will be called on the CrBrowserMain |
| 78 | // thread. |
| 79 | static MidiManager* Create(); |
| 80 | |
toyoshim | 7a80966 | 2015-10-06 00:54:21 -0700 | [diff] [blame] | 81 | // Called on the CrBrowserMain thread to notify the Chrome_IOThread will stop |
| 82 | // and the instance will be destructed on the CrBrowserMain thread soon. |
| 83 | void Shutdown(); |
| 84 | |
toyoshim@chromium.org | 235b09e | 2013-07-25 16:23:14 +0000 | [diff] [blame] | 85 | // A client calls StartSession() to receive and send MIDI data. |
| 86 | // 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] | 87 | // and the client is registered to receive MIDI data. |
toyoshim | f1b8896 | 2015-07-09 14:14:51 -0700 | [diff] [blame] | 88 | // CompleteStartSession() is called with Result::OK if the session is started. |
| 89 | // Otherwise CompleteStartSession() is called with proper Result code. |
toyoshim@chromium.org | c1e05fb | 2014-05-06 16:39:20 +0000 | [diff] [blame] | 90 | // StartSession() and EndSession() can be called on the Chrome_IOThread. |
| 91 | // CompleteStartSession() will be invoked on the same Chrome_IOThread. |
toyoshim | a485ff9 | 2014-10-23 00:17:59 -0700 | [diff] [blame] | 92 | void StartSession(MidiManagerClient* client); |
crogers@google.com | 27356e4 | 2013-06-22 04:03:03 +0000 | [diff] [blame] | 93 | |
toyoshim@chromium.org | f77a1e6 | 2014-04-11 13:16:24 +0000 | [diff] [blame] | 94 | // A client calls EndSession() to stop receiving MIDI data. |
toyoshim@chromium.org | c82e66e | 2014-02-04 07:05:47 +0000 | [diff] [blame] | 95 | void EndSession(MidiManagerClient* client); |
crogers@google.com | 27356e4 | 2013-06-22 04:03:03 +0000 | [diff] [blame] | 96 | |
toyoshim | 7dd1248 | 2015-04-07 07:02:49 -0700 | [diff] [blame] | 97 | // Invoke AccumulateMidiBytesSent() for |client| safely. If the session was |
| 98 | // already closed, do nothing. |
| 99 | void AccumulateMidiBytesSent(MidiManagerClient* client, size_t n); |
| 100 | |
toyoshim@chromium.org | c82e66e | 2014-02-04 07:05:47 +0000 | [diff] [blame] | 101 | // 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] | 102 | // system. |
| 103 | // This method is supposed to return immediately and should not block. |
crogers@google.com | 27356e4 | 2013-06-22 04:03:03 +0000 | [diff] [blame] | 104 | // |port_index| represents the specific output port from output_ports(). |
| 105 | // |data| represents a series of bytes encoding one or more MIDI messages. |
| 106 | // |length| is the number of bytes in |data|. |
crogers@google.com | 542a43a | 2013-07-31 05:16:49 +0000 | [diff] [blame] | 107 | // |timestamp| is the time to send the data, in seconds. A value of 0 |
| 108 | // means send "now" or as soon as possible. |
yhirano@chromium.org | c6d5b7b | 2013-12-20 07:27:23 +0000 | [diff] [blame] | 109 | // The default implementation is for unsupported platforms. |
toyoshim@chromium.org | c82e66e | 2014-02-04 07:05:47 +0000 | [diff] [blame] | 110 | virtual void DispatchSendMidiData(MidiManagerClient* client, |
Avi Drissman | 3528fd0 | 2015-12-18 20:11:31 -0500 | [diff] [blame^] | 111 | uint32_t port_index, |
| 112 | const std::vector<uint8_t>& data, |
yhirano@chromium.org | c6d5b7b | 2013-12-20 07:27:23 +0000 | [diff] [blame] | 113 | double timestamp); |
crogers@google.com | 27356e4 | 2013-06-22 04:03:03 +0000 | [diff] [blame] | 114 | |
crogers@google.com | 27356e4 | 2013-06-22 04:03:03 +0000 | [diff] [blame] | 115 | protected: |
toyoshim@chromium.org | 51c7f53 | 2014-05-01 17:17:32 +0000 | [diff] [blame] | 116 | friend class MidiManagerUsb; |
| 117 | |
toyoshim@chromium.org | c1e05fb | 2014-05-06 16:39:20 +0000 | [diff] [blame] | 118 | // Initializes the platform dependent MIDI system. MidiManager class has a |
| 119 | // default implementation that synchronously calls CompleteInitialization() |
toyoshim | f1b8896 | 2015-07-09 14:14:51 -0700 | [diff] [blame] | 120 | // with Result::NOT_SUPPORTED on the caller thread. A derived class for a |
toyoshim@chromium.org | c1e05fb | 2014-05-06 16:39:20 +0000 | [diff] [blame] | 121 | // specific platform should override this method correctly. |
| 122 | // This method is called on Chrome_IOThread thread inside StartSession(). |
| 123 | // Platform dependent initialization can be processed synchronously or |
| 124 | // asynchronously. When the initialization is completed, |
| 125 | // CompleteInitialization() should be called with |result|. |
toyoshim | f1b8896 | 2015-07-09 14:14:51 -0700 | [diff] [blame] | 126 | // |result| should be Result::OK on success, otherwise a proper Result. |
toyoshim@chromium.org | 51c7f53 | 2014-05-01 17:17:32 +0000 | [diff] [blame] | 127 | virtual void StartInitialization(); |
| 128 | |
toyoshim | 7a80966 | 2015-10-06 00:54:21 -0700 | [diff] [blame] | 129 | // Finalizes the platform dependent MIDI system. Called on Chrome_IOThread |
| 130 | // thread and the thread will stop immediately after this call. |
| 131 | // Platform dependent resources that were allocated on the Chrome_IOThread |
| 132 | // should be disposed inside this method. |
| 133 | virtual void Finalize() {} |
| 134 | |
toyoshim@chromium.org | 51c7f53 | 2014-05-01 17:17:32 +0000 | [diff] [blame] | 135 | // Called from a platform dependent implementation of StartInitialization(). |
toyoshim@chromium.org | c1e05fb | 2014-05-06 16:39:20 +0000 | [diff] [blame] | 136 | // It invokes CompleteInitializationInternal() on the thread that calls |
| 137 | // StartSession() and distributes |result| to MIDIManagerClient objects in |
toyoshim@chromium.org | 51c7f53 | 2014-05-01 17:17:32 +0000 | [diff] [blame] | 138 | // |pending_clients_|. |
toyoshim | f1b8896 | 2015-07-09 14:14:51 -0700 | [diff] [blame] | 139 | void CompleteInitialization(Result result); |
crogers@google.com | 27356e4 | 2013-06-22 04:03:03 +0000 | [diff] [blame] | 140 | |
toyoshim@chromium.org | c82e66e | 2014-02-04 07:05:47 +0000 | [diff] [blame] | 141 | void AddInputPort(const MidiPortInfo& info); |
| 142 | void AddOutputPort(const MidiPortInfo& info); |
Avi Drissman | 3528fd0 | 2015-12-18 20:11:31 -0500 | [diff] [blame^] | 143 | void SetInputPortState(uint32_t port_index, MidiPortState state); |
| 144 | void SetOutputPortState(uint32_t port_index, MidiPortState state); |
crogers@google.com | 27356e4 | 2013-06-22 04:03:03 +0000 | [diff] [blame] | 145 | |
| 146 | // Dispatches to all clients. |
yhirano@chromium.org | cfa642c | 2014-05-01 08:54:41 +0000 | [diff] [blame] | 147 | // TODO(toyoshim): Fix the mac implementation to use |
| 148 | // |ReceiveMidiData(..., base::TimeTicks)|. |
Avi Drissman | 3528fd0 | 2015-12-18 20:11:31 -0500 | [diff] [blame^] | 149 | void ReceiveMidiData(uint32_t port_index, |
| 150 | const uint8_t* data, |
toyoshim@chromium.org | ae6ad36 | 2013-08-27 15:30:20 +0000 | [diff] [blame] | 151 | size_t length, |
| 152 | double timestamp); |
| 153 | |
Avi Drissman | 3528fd0 | 2015-12-18 20:11:31 -0500 | [diff] [blame^] | 154 | void ReceiveMidiData(uint32_t port_index, |
| 155 | const uint8_t* data, |
yhirano@chromium.org | cfa642c | 2014-05-01 08:54:41 +0000 | [diff] [blame] | 156 | size_t length, |
| 157 | base::TimeTicks time) { |
| 158 | ReceiveMidiData(port_index, data, length, |
| 159 | (time - base::TimeTicks()).InSecondsF()); |
| 160 | } |
| 161 | |
toyoshim@chromium.org | a25e683 | 2014-05-07 18:06:25 +0000 | [diff] [blame] | 162 | size_t clients_size_for_testing() const { return clients_.size(); } |
| 163 | size_t pending_clients_size_for_testing() const { |
toyoshim@chromium.org | c1e05fb | 2014-05-06 16:39:20 +0000 | [diff] [blame] | 164 | return pending_clients_.size(); |
| 165 | } |
| 166 | |
yhirano | bc742d8 | 2015-09-17 07:41:44 -0700 | [diff] [blame] | 167 | const MidiPortInfoList& input_ports() const { return input_ports_; } |
| 168 | const MidiPortInfoList& output_ports() const { return output_ports_; } |
| 169 | |
toyoshim@chromium.org | c1e05fb | 2014-05-06 16:39:20 +0000 | [diff] [blame] | 170 | private: |
toyoshim | f1b8896 | 2015-07-09 14:14:51 -0700 | [diff] [blame] | 171 | void CompleteInitializationInternal(Result result); |
toyoshim | a62d674 | 2014-10-23 09:05:03 -0700 | [diff] [blame] | 172 | void AddInitialPorts(MidiManagerClient* client); |
toyoshim | 7a80966 | 2015-10-06 00:54:21 -0700 | [diff] [blame] | 173 | void ShutdownOnSessionThread(); |
crogers@google.com | 27356e4 | 2013-06-22 04:03:03 +0000 | [diff] [blame] | 174 | |
| 175 | // Keeps track of all clients who wish to receive MIDI data. |
toyoshim | a485ff9 | 2014-10-23 00:17:59 -0700 | [diff] [blame] | 176 | typedef std::set<MidiManagerClient*> ClientSet; |
| 177 | ClientSet clients_; |
crogers@google.com | 27356e4 | 2013-06-22 04:03:03 +0000 | [diff] [blame] | 178 | |
toyoshim@chromium.org | 51c7f53 | 2014-05-01 17:17:32 +0000 | [diff] [blame] | 179 | // Keeps track of all clients who are waiting for CompleteStartSession(). |
toyoshim | a485ff9 | 2014-10-23 00:17:59 -0700 | [diff] [blame] | 180 | ClientSet pending_clients_; |
toyoshim@chromium.org | 51c7f53 | 2014-05-01 17:17:32 +0000 | [diff] [blame] | 181 | |
toyoshim@chromium.org | c1e05fb | 2014-05-06 16:39:20 +0000 | [diff] [blame] | 182 | // Keeps a SingleThreadTaskRunner of the thread that calls StartSession in |
| 183 | // order to invoke CompleteStartSession() on the thread. |
| 184 | scoped_refptr<base::SingleThreadTaskRunner> session_thread_runner_; |
crogers@google.com | 27356e4 | 2013-06-22 04:03:03 +0000 | [diff] [blame] | 185 | |
toyoshim@chromium.org | c1e05fb | 2014-05-06 16:39:20 +0000 | [diff] [blame] | 186 | // Keeps true if platform dependent initialization is already completed. |
| 187 | bool initialized_; |
crogers@google.com | 27356e4 | 2013-06-22 04:03:03 +0000 | [diff] [blame] | 188 | |
toyoshim | 7a80966 | 2015-10-06 00:54:21 -0700 | [diff] [blame] | 189 | // Keeps false until Finalize() is called. |
| 190 | bool finalized_; |
| 191 | |
toyoshim@chromium.org | c1e05fb | 2014-05-06 16:39:20 +0000 | [diff] [blame] | 192 | // Keeps the platform dependent initialization result if initialization is |
toyoshim | f1b8896 | 2015-07-09 14:14:51 -0700 | [diff] [blame] | 193 | // completed. Otherwise keeps Result::NOT_INITIALIZED. |
| 194 | Result result_; |
toyoshim@chromium.org | c1e05fb | 2014-05-06 16:39:20 +0000 | [diff] [blame] | 195 | |
toyoshim | a62d674 | 2014-10-23 09:05:03 -0700 | [diff] [blame] | 196 | // Keeps all MidiPortInfo. |
toyoshim@chromium.org | a25e683 | 2014-05-07 18:06:25 +0000 | [diff] [blame] | 197 | MidiPortInfoList input_ports_; |
| 198 | MidiPortInfoList output_ports_; |
| 199 | |
toyoshim | 7a80966 | 2015-10-06 00:54:21 -0700 | [diff] [blame] | 200 | // Protects access to |clients_|, |pending_clients_|, |
| 201 | // |session_thread_runner_|, |initialized_|, |finalize_|, |result_|, |
| 202 | // |input_ports_| and |output_ports_|. |
toyoshim | a62d674 | 2014-10-23 09:05:03 -0700 | [diff] [blame] | 203 | base::Lock lock_; |
| 204 | |
toyoshim@chromium.org | c82e66e | 2014-02-04 07:05:47 +0000 | [diff] [blame] | 205 | DISALLOW_COPY_AND_ASSIGN(MidiManager); |
crogers@google.com | 27356e4 | 2013-06-22 04:03:03 +0000 | [diff] [blame] | 206 | }; |
| 207 | |
toyoshim | e147c5e | 2015-05-07 21:58:31 -0700 | [diff] [blame] | 208 | } // namespace midi |
crogers@google.com | 27356e4 | 2013-06-22 04:03:03 +0000 | [diff] [blame] | 209 | } // namespace media |
| 210 | |
| 211 | #endif // MEDIA_MIDI_MIDI_MANAGER_H_ |