| OLD | NEW |
| 1 // Copyright (c) 2013 The Chromium Authors. All rights reserved. | 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 | 2 // Use of this source code is governed by a BSD-style license that can be |
| 3 // found in the LICENSE file. | 3 // found in the LICENSE file. |
| 4 | 4 |
| 5 #ifndef MEDIA_MIDI_MIDI_MANAGER_H_ | 5 #ifndef MEDIA_MIDI_MIDI_MANAGER_H_ |
| 6 #define MEDIA_MIDI_MIDI_MANAGER_H_ | 6 #define MEDIA_MIDI_MIDI_MANAGER_H_ |
| 7 | 7 |
| 8 #include <stddef.h> | 8 #include <stddef.h> |
| 9 #include <stdint.h> | 9 #include <stdint.h> |
| 10 | 10 |
| 11 #include <set> | 11 #include <set> |
| 12 #include <vector> | 12 #include <vector> |
| 13 | 13 |
| 14 #include "base/macros.h" | 14 #include "base/macros.h" |
| 15 #include "base/memory/ref_counted.h" | 15 #include "base/memory/ref_counted.h" |
| 16 #include "base/synchronization/lock.h" | 16 #include "base/synchronization/lock.h" |
| 17 #include "base/time/time.h" | 17 #include "base/time/time.h" |
| 18 #include "media/midi/midi_export.h" | 18 #include "media/midi/midi_export.h" |
| 19 #include "media/midi/midi_port_info.h" | 19 #include "media/midi/midi_port_info.h" |
| 20 #include "media/midi/result.h" | 20 #include "media/midi/midi_service.mojom.h" |
| 21 | 21 |
| 22 namespace base { | 22 namespace base { |
| 23 class SingleThreadTaskRunner; | 23 class SingleThreadTaskRunner; |
| 24 } // namespace base | 24 } // namespace base |
| 25 | 25 |
| 26 namespace midi { | 26 namespace midi { |
| 27 | 27 |
| 28 // A MidiManagerClient registers with the MidiManager to receive MIDI data. | 28 // A MidiManagerClient registers with the MidiManager to receive MIDI data. |
| 29 // See MidiManager::RequestAccess() and MidiManager::ReleaseAccess() | 29 // See MidiManager::RequestAccess() and MidiManager::ReleaseAccess() |
| 30 // for details. | 30 // for details. |
| 31 class MIDI_EXPORT MidiManagerClient { | 31 class MIDI_EXPORT MidiManagerClient { |
| 32 public: | 32 public: |
| 33 virtual ~MidiManagerClient() {} | 33 virtual ~MidiManagerClient() {} |
| 34 | 34 |
| 35 // AddInputPort() and AddOutputPort() are called before CompleteStartSession() | 35 // AddInputPort() and AddOutputPort() are called before CompleteStartSession() |
| 36 // is called to notify existing MIDI ports, and also called after that to | 36 // is called to notify existing MIDI ports, and also called after that to |
| 37 // notify new MIDI ports are added. | 37 // notify new MIDI ports are added. |
| 38 virtual void AddInputPort(const MidiPortInfo& info) = 0; | 38 virtual void AddInputPort(const MidiPortInfo& info) = 0; |
| 39 virtual void AddOutputPort(const MidiPortInfo& info) = 0; | 39 virtual void AddOutputPort(const MidiPortInfo& info) = 0; |
| 40 | 40 |
| 41 // SetInputPortState() and SetOutputPortState() are called to notify a known | 41 // SetInputPortState() and SetOutputPortState() are called to notify a known |
| 42 // device gets disconnected, or connected again. | 42 // device gets disconnected, or connected again. |
| 43 virtual void SetInputPortState(uint32_t port_index, MidiPortState state) = 0; | 43 virtual void SetInputPortState(uint32_t port_index, MidiPortState state) = 0; |
| 44 virtual void SetOutputPortState(uint32_t port_index, MidiPortState state) = 0; | 44 virtual void SetOutputPortState(uint32_t port_index, MidiPortState state) = 0; |
| 45 | 45 |
| 46 // CompleteStartSession() is called when platform dependent preparation is | 46 // CompleteStartSession() is called when platform dependent preparation is |
| 47 // finished. | 47 // finished. |
| 48 virtual void CompleteStartSession(Result result) = 0; | 48 virtual void CompleteStartSession(mojom::Result result) = 0; |
| 49 | 49 |
| 50 // ReceiveMidiData() is called when MIDI data has been received from the | 50 // ReceiveMidiData() is called when MIDI data has been received from the |
| 51 // MIDI system. | 51 // MIDI system. |
| 52 // |port_index| represents the specific input port from input_ports(). | 52 // |port_index| represents the specific input port from input_ports(). |
| 53 // |data| represents a series of bytes encoding one or more MIDI messages. | 53 // |data| represents a series of bytes encoding one or more MIDI messages. |
| 54 // |length| is the number of bytes in |data|. | 54 // |length| is the number of bytes in |data|. |
| 55 // |timestamp| is the time the data was received, in seconds. | 55 // |timestamp| is the time the data was received, in seconds. |
| 56 virtual void ReceiveMidiData(uint32_t port_index, | 56 virtual void ReceiveMidiData(uint32_t port_index, |
| 57 const uint8_t* data, | 57 const uint8_t* data, |
| 58 size_t length, | 58 size_t length, |
| (...skipping 22 matching lines...) Expand all Loading... |
| 81 // thread. | 81 // thread. |
| 82 static MidiManager* Create(); | 82 static MidiManager* Create(); |
| 83 | 83 |
| 84 // Called on the CrBrowserMain thread to notify the Chrome_IOThread will stop | 84 // Called on the CrBrowserMain thread to notify the Chrome_IOThread will stop |
| 85 // and the instance will be destructed on the CrBrowserMain thread soon. | 85 // and the instance will be destructed on the CrBrowserMain thread soon. |
| 86 void Shutdown(); | 86 void Shutdown(); |
| 87 | 87 |
| 88 // A client calls StartSession() to receive and send MIDI data. | 88 // A client calls StartSession() to receive and send MIDI data. |
| 89 // If the session is ready to start, the MIDI system is lazily initialized | 89 // If the session is ready to start, the MIDI system is lazily initialized |
| 90 // and the client is registered to receive MIDI data. | 90 // and the client is registered to receive MIDI data. |
| 91 // CompleteStartSession() is called with Result::OK if the session is started. | 91 // CompleteStartSession() is called with mojom::Result::OK if the session is |
| 92 // Otherwise CompleteStartSession() is called with proper Result code. | 92 // started. Otherwise CompleteStartSession() is called with a proper |
| 93 // mojom::Result code. |
| 93 // StartSession() and EndSession() can be called on the Chrome_IOThread. | 94 // StartSession() and EndSession() can be called on the Chrome_IOThread. |
| 94 // CompleteStartSession() will be invoked on the same Chrome_IOThread. | 95 // CompleteStartSession() will be invoked on the same Chrome_IOThread. |
| 95 void StartSession(MidiManagerClient* client); | 96 void StartSession(MidiManagerClient* client); |
| 96 | 97 |
| 97 // A client calls EndSession() to stop receiving MIDI data. | 98 // A client calls EndSession() to stop receiving MIDI data. |
| 98 void EndSession(MidiManagerClient* client); | 99 void EndSession(MidiManagerClient* client); |
| 99 | 100 |
| 100 // Invoke AccumulateMidiBytesSent() for |client| safely. If the session was | 101 // Invoke AccumulateMidiBytesSent() for |client| safely. If the session was |
| 101 // already closed, do nothing. | 102 // already closed, do nothing. |
| 102 void AccumulateMidiBytesSent(MidiManagerClient* client, size_t n); | 103 void AccumulateMidiBytesSent(MidiManagerClient* client, size_t n); |
| (...skipping 10 matching lines...) Expand all Loading... |
| 113 virtual void DispatchSendMidiData(MidiManagerClient* client, | 114 virtual void DispatchSendMidiData(MidiManagerClient* client, |
| 114 uint32_t port_index, | 115 uint32_t port_index, |
| 115 const std::vector<uint8_t>& data, | 116 const std::vector<uint8_t>& data, |
| 116 double timestamp); | 117 double timestamp); |
| 117 | 118 |
| 118 protected: | 119 protected: |
| 119 friend class MidiManagerUsb; | 120 friend class MidiManagerUsb; |
| 120 | 121 |
| 121 // Initializes the platform dependent MIDI system. MidiManager class has a | 122 // Initializes the platform dependent MIDI system. MidiManager class has a |
| 122 // default implementation that synchronously calls CompleteInitialization() | 123 // default implementation that synchronously calls CompleteInitialization() |
| 123 // with Result::NOT_SUPPORTED on the caller thread. A derived class for a | 124 // with mojom::Result::NOT_SUPPORTED on the caller thread. A derived class for |
| 124 // specific platform should override this method correctly. | 125 // a specific platform should override this method correctly. |
| 125 // This method is called on Chrome_IOThread thread inside StartSession(). | 126 // This method is called on Chrome_IOThread thread inside StartSession(). |
| 126 // Platform dependent initialization can be processed synchronously or | 127 // Platform dependent initialization can be processed synchronously or |
| 127 // asynchronously. When the initialization is completed, | 128 // asynchronously. When the initialization is completed, |
| 128 // CompleteInitialization() should be called with |result|. | 129 // CompleteInitialization() should be called with |result|. |
| 129 // |result| should be Result::OK on success, otherwise a proper Result. | 130 // |result| should be mojom::Result::OK on success, otherwise a proper |
| 131 // mojom::Result. |
| 130 virtual void StartInitialization(); | 132 virtual void StartInitialization(); |
| 131 | 133 |
| 132 // Finalizes the platform dependent MIDI system. Called on Chrome_IOThread | 134 // Finalizes the platform dependent MIDI system. Called on Chrome_IOThread |
| 133 // thread and the thread will stop immediately after this call. | 135 // thread and the thread will stop immediately after this call. |
| 134 // Platform dependent resources that were allocated on the Chrome_IOThread | 136 // Platform dependent resources that were allocated on the Chrome_IOThread |
| 135 // should be disposed inside this method. | 137 // should be disposed inside this method. |
| 136 virtual void Finalize() {} | 138 virtual void Finalize() {} |
| 137 | 139 |
| 138 // Called from a platform dependent implementation of StartInitialization(). | 140 // Called from a platform dependent implementation of StartInitialization(). |
| 139 // It invokes CompleteInitializationInternal() on the thread that calls | 141 // It invokes CompleteInitializationInternal() on the thread that calls |
| 140 // StartSession() and distributes |result| to MIDIManagerClient objects in | 142 // StartSession() and distributes |result| to MIDIManagerClient objects in |
| 141 // |pending_clients_|. | 143 // |pending_clients_|. |
| 142 void CompleteInitialization(Result result); | 144 void CompleteInitialization(mojom::Result result); |
| 143 | 145 |
| 144 void AddInputPort(const MidiPortInfo& info); | 146 void AddInputPort(const MidiPortInfo& info); |
| 145 void AddOutputPort(const MidiPortInfo& info); | 147 void AddOutputPort(const MidiPortInfo& info); |
| 146 void SetInputPortState(uint32_t port_index, MidiPortState state); | 148 void SetInputPortState(uint32_t port_index, MidiPortState state); |
| 147 void SetOutputPortState(uint32_t port_index, MidiPortState state); | 149 void SetOutputPortState(uint32_t port_index, MidiPortState state); |
| 148 | 150 |
| 149 // Dispatches to all clients. | 151 // Dispatches to all clients. |
| 150 // TODO(toyoshim): Fix the mac implementation to use | 152 // TODO(toyoshim): Fix the mac implementation to use |
| 151 // |ReceiveMidiData(..., base::TimeTicks)|. | 153 // |ReceiveMidiData(..., base::TimeTicks)|. |
| 152 void ReceiveMidiData(uint32_t port_index, | 154 void ReceiveMidiData(uint32_t port_index, |
| (...skipping 11 matching lines...) Expand all Loading... |
| 164 | 166 |
| 165 size_t clients_size_for_testing() const { return clients_.size(); } | 167 size_t clients_size_for_testing() const { return clients_.size(); } |
| 166 size_t pending_clients_size_for_testing() const { | 168 size_t pending_clients_size_for_testing() const { |
| 167 return pending_clients_.size(); | 169 return pending_clients_.size(); |
| 168 } | 170 } |
| 169 | 171 |
| 170 const MidiPortInfoList& input_ports() const { return input_ports_; } | 172 const MidiPortInfoList& input_ports() const { return input_ports_; } |
| 171 const MidiPortInfoList& output_ports() const { return output_ports_; } | 173 const MidiPortInfoList& output_ports() const { return output_ports_; } |
| 172 | 174 |
| 173 private: | 175 private: |
| 174 void CompleteInitializationInternal(Result result); | 176 void CompleteInitializationInternal(mojom::Result result); |
| 175 void AddInitialPorts(MidiManagerClient* client); | 177 void AddInitialPorts(MidiManagerClient* client); |
| 176 void ShutdownOnSessionThread(); | 178 void ShutdownOnSessionThread(); |
| 177 | 179 |
| 178 // Keeps track of all clients who wish to receive MIDI data. | 180 // Keeps track of all clients who wish to receive MIDI data. |
| 179 typedef std::set<MidiManagerClient*> ClientSet; | 181 typedef std::set<MidiManagerClient*> ClientSet; |
| 180 ClientSet clients_; | 182 ClientSet clients_; |
| 181 | 183 |
| 182 // Keeps track of all clients who are waiting for CompleteStartSession(). | 184 // Keeps track of all clients who are waiting for CompleteStartSession(). |
| 183 ClientSet pending_clients_; | 185 ClientSet pending_clients_; |
| 184 | 186 |
| 185 // Keeps a SingleThreadTaskRunner of the thread that calls StartSession in | 187 // Keeps a SingleThreadTaskRunner of the thread that calls StartSession in |
| 186 // order to invoke CompleteStartSession() on the thread. | 188 // order to invoke CompleteStartSession() on the thread. |
| 187 scoped_refptr<base::SingleThreadTaskRunner> session_thread_runner_; | 189 scoped_refptr<base::SingleThreadTaskRunner> session_thread_runner_; |
| 188 | 190 |
| 189 // Keeps true if platform dependent initialization is already completed. | 191 // Keeps true if platform dependent initialization is already completed. |
| 190 bool initialized_; | 192 bool initialized_; |
| 191 | 193 |
| 192 // Keeps false until Finalize() is called. | 194 // Keeps false until Finalize() is called. |
| 193 bool finalized_; | 195 bool finalized_; |
| 194 | 196 |
| 195 // Keeps the platform dependent initialization result if initialization is | 197 // Keeps the platform dependent initialization result if initialization is |
| 196 // completed. Otherwise keeps Result::NOT_INITIALIZED. | 198 // completed. Otherwise keeps mojom::Result::NOT_INITIALIZED. |
| 197 Result result_; | 199 mojom::Result result_; |
| 198 | 200 |
| 199 // Keeps all MidiPortInfo. | 201 // Keeps all MidiPortInfo. |
| 200 MidiPortInfoList input_ports_; | 202 MidiPortInfoList input_ports_; |
| 201 MidiPortInfoList output_ports_; | 203 MidiPortInfoList output_ports_; |
| 202 | 204 |
| 203 // Protects access to |clients_|, |pending_clients_|, | 205 // Protects access to |clients_|, |pending_clients_|, |
| 204 // |session_thread_runner_|, |initialized_|, |finalize_|, |result_|, | 206 // |session_thread_runner_|, |initialized_|, |finalize_|, |result_|, |
| 205 // |input_ports_| and |output_ports_|. | 207 // |input_ports_| and |output_ports_|. |
| 206 base::Lock lock_; | 208 base::Lock lock_; |
| 207 | 209 |
| 208 DISALLOW_COPY_AND_ASSIGN(MidiManager); | 210 DISALLOW_COPY_AND_ASSIGN(MidiManager); |
| 209 }; | 211 }; |
| 210 | 212 |
| 211 } // namespace midi | 213 } // namespace midi |
| 212 | 214 |
| 213 #endif // MEDIA_MIDI_MIDI_MANAGER_H_ | 215 #endif // MEDIA_MIDI_MIDI_MANAGER_H_ |
| OLD | NEW |