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 <set> | 8 #include <set> |
9 #include <vector> | 9 #include <vector> |
10 | 10 |
(...skipping 43 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
54 virtual void ReceiveMidiData(uint32 port_index, | 54 virtual void ReceiveMidiData(uint32 port_index, |
55 const uint8* data, | 55 const uint8* data, |
56 size_t length, | 56 size_t length, |
57 double timestamp) = 0; | 57 double timestamp) = 0; |
58 | 58 |
59 // AccumulateMidiBytesSent() is called to acknowledge when bytes have | 59 // AccumulateMidiBytesSent() is called to acknowledge when bytes have |
60 // successfully been sent to the hardware. | 60 // successfully been sent to the hardware. |
61 // This happens as a result of the client having previously called | 61 // This happens as a result of the client having previously called |
62 // MidiManager::DispatchSendMidiData(). | 62 // MidiManager::DispatchSendMidiData(). |
63 virtual void AccumulateMidiBytesSent(size_t n) = 0; | 63 virtual void AccumulateMidiBytesSent(size_t n) = 0; |
| 64 |
| 65 // Detach() is called when MidiManager is going to shutdown immediately. |
| 66 // Client should not touch MidiManager instance after Detach() is called. |
| 67 virtual void Detach() = 0; |
64 }; | 68 }; |
65 | 69 |
66 // Manages access to all MIDI hardware. | 70 // Manages access to all MIDI hardware. |
67 class MIDI_EXPORT MidiManager { | 71 class MIDI_EXPORT MidiManager { |
68 public: | 72 public: |
69 static const size_t kMaxPendingClientCount = 128; | 73 static const size_t kMaxPendingClientCount = 128; |
70 | 74 |
71 MidiManager(); | 75 MidiManager(); |
72 virtual ~MidiManager(); | 76 virtual ~MidiManager(); |
73 | 77 |
74 // The constructor and the destructor will be called on the CrBrowserMain | 78 // The constructor and the destructor will be called on the CrBrowserMain |
75 // thread. | 79 // thread. |
76 static MidiManager* Create(); | 80 static MidiManager* Create(); |
77 | 81 |
| 82 // Called on the CrBrowserMain thread to notify the Chrome_IOThread will stop |
| 83 // and the instance will be destructed on the CrBrowserMain thread soon. |
| 84 void Shutdown(); |
| 85 |
78 // A client calls StartSession() to receive and send MIDI data. | 86 // A client calls StartSession() to receive and send MIDI data. |
79 // If the session is ready to start, the MIDI system is lazily initialized | 87 // If the session is ready to start, the MIDI system is lazily initialized |
80 // and the client is registered to receive MIDI data. | 88 // and the client is registered to receive MIDI data. |
81 // CompleteStartSession() is called with Result::OK if the session is started. | 89 // CompleteStartSession() is called with Result::OK if the session is started. |
82 // Otherwise CompleteStartSession() is called with proper Result code. | 90 // Otherwise CompleteStartSession() is called with proper Result code. |
83 // StartSession() and EndSession() can be called on the Chrome_IOThread. | 91 // StartSession() and EndSession() can be called on the Chrome_IOThread. |
84 // CompleteStartSession() will be invoked on the same Chrome_IOThread. | 92 // CompleteStartSession() will be invoked on the same Chrome_IOThread. |
85 void StartSession(MidiManagerClient* client); | 93 void StartSession(MidiManagerClient* client); |
86 | 94 |
87 // A client calls EndSession() to stop receiving MIDI data. | 95 // A client calls EndSession() to stop receiving MIDI data. |
(...skipping 24 matching lines...) Expand all Loading... |
112 // default implementation that synchronously calls CompleteInitialization() | 120 // default implementation that synchronously calls CompleteInitialization() |
113 // with Result::NOT_SUPPORTED on the caller thread. A derived class for a | 121 // with Result::NOT_SUPPORTED on the caller thread. A derived class for a |
114 // specific platform should override this method correctly. | 122 // specific platform should override this method correctly. |
115 // This method is called on Chrome_IOThread thread inside StartSession(). | 123 // This method is called on Chrome_IOThread thread inside StartSession(). |
116 // Platform dependent initialization can be processed synchronously or | 124 // Platform dependent initialization can be processed synchronously or |
117 // asynchronously. When the initialization is completed, | 125 // asynchronously. When the initialization is completed, |
118 // CompleteInitialization() should be called with |result|. | 126 // CompleteInitialization() should be called with |result|. |
119 // |result| should be Result::OK on success, otherwise a proper Result. | 127 // |result| should be Result::OK on success, otherwise a proper Result. |
120 virtual void StartInitialization(); | 128 virtual void StartInitialization(); |
121 | 129 |
| 130 // Finalizes the platform dependent MIDI system. Called on Chrome_IOThread |
| 131 // thread and the thread will stop immediately after this call. |
| 132 // Platform dependent resources that were allocated on the Chrome_IOThread |
| 133 // should be disposed inside this method. |
| 134 virtual void Finalize() {} |
| 135 |
122 // Called from a platform dependent implementation of StartInitialization(). | 136 // Called from a platform dependent implementation of StartInitialization(). |
123 // It invokes CompleteInitializationInternal() on the thread that calls | 137 // It invokes CompleteInitializationInternal() on the thread that calls |
124 // StartSession() and distributes |result| to MIDIManagerClient objects in | 138 // StartSession() and distributes |result| to MIDIManagerClient objects in |
125 // |pending_clients_|. | 139 // |pending_clients_|. |
126 void CompleteInitialization(Result result); | 140 void CompleteInitialization(Result result); |
127 | 141 |
128 void AddInputPort(const MidiPortInfo& info); | 142 void AddInputPort(const MidiPortInfo& info); |
129 void AddOutputPort(const MidiPortInfo& info); | 143 void AddOutputPort(const MidiPortInfo& info); |
130 void SetInputPortState(uint32 port_index, MidiPortState state); | 144 void SetInputPortState(uint32 port_index, MidiPortState state); |
131 void SetOutputPortState(uint32 port_index, MidiPortState state); | 145 void SetOutputPortState(uint32 port_index, MidiPortState state); |
(...skipping 18 matching lines...) Expand all Loading... |
150 size_t pending_clients_size_for_testing() const { | 164 size_t pending_clients_size_for_testing() const { |
151 return pending_clients_.size(); | 165 return pending_clients_.size(); |
152 } | 166 } |
153 | 167 |
154 const MidiPortInfoList& input_ports() const { return input_ports_; } | 168 const MidiPortInfoList& input_ports() const { return input_ports_; } |
155 const MidiPortInfoList& output_ports() const { return output_ports_; } | 169 const MidiPortInfoList& output_ports() const { return output_ports_; } |
156 | 170 |
157 private: | 171 private: |
158 void CompleteInitializationInternal(Result result); | 172 void CompleteInitializationInternal(Result result); |
159 void AddInitialPorts(MidiManagerClient* client); | 173 void AddInitialPorts(MidiManagerClient* client); |
| 174 void ShutdownOnSessionThread(); |
160 | 175 |
161 // Keeps track of all clients who wish to receive MIDI data. | 176 // Keeps track of all clients who wish to receive MIDI data. |
162 typedef std::set<MidiManagerClient*> ClientSet; | 177 typedef std::set<MidiManagerClient*> ClientSet; |
163 ClientSet clients_; | 178 ClientSet clients_; |
164 | 179 |
165 // Keeps track of all clients who are waiting for CompleteStartSession(). | 180 // Keeps track of all clients who are waiting for CompleteStartSession(). |
166 ClientSet pending_clients_; | 181 ClientSet pending_clients_; |
167 | 182 |
168 // Keeps a SingleThreadTaskRunner of the thread that calls StartSession in | 183 // Keeps a SingleThreadTaskRunner of the thread that calls StartSession in |
169 // order to invoke CompleteStartSession() on the thread. | 184 // order to invoke CompleteStartSession() on the thread. |
170 scoped_refptr<base::SingleThreadTaskRunner> session_thread_runner_; | 185 scoped_refptr<base::SingleThreadTaskRunner> session_thread_runner_; |
171 | 186 |
172 // Keeps true if platform dependent initialization is already completed. | 187 // Keeps true if platform dependent initialization is already completed. |
173 bool initialized_; | 188 bool initialized_; |
174 | 189 |
| 190 // Keeps false until Finalize() is called. |
| 191 bool finalized_; |
| 192 |
175 // Keeps the platform dependent initialization result if initialization is | 193 // Keeps the platform dependent initialization result if initialization is |
176 // completed. Otherwise keeps Result::NOT_INITIALIZED. | 194 // completed. Otherwise keeps Result::NOT_INITIALIZED. |
177 Result result_; | 195 Result result_; |
178 | 196 |
179 // Keeps all MidiPortInfo. | 197 // Keeps all MidiPortInfo. |
180 MidiPortInfoList input_ports_; | 198 MidiPortInfoList input_ports_; |
181 MidiPortInfoList output_ports_; | 199 MidiPortInfoList output_ports_; |
182 | 200 |
183 // Protects access to |clients_|, |pending_clients_|, |initialized_|, | 201 // Protects access to |clients_|, |pending_clients_|, |
184 // |result_|, |input_ports_| and |output_ports_|. | 202 // |session_thread_runner_|, |initialized_|, |finalize_|, |result_|, |
| 203 // |input_ports_| and |output_ports_|. |
185 base::Lock lock_; | 204 base::Lock lock_; |
186 | 205 |
187 DISALLOW_COPY_AND_ASSIGN(MidiManager); | 206 DISALLOW_COPY_AND_ASSIGN(MidiManager); |
188 }; | 207 }; |
189 | 208 |
190 } // namespace midi | 209 } // namespace midi |
191 } // namespace media | 210 } // namespace media |
192 | 211 |
193 #endif // MEDIA_MIDI_MIDI_MANAGER_H_ | 212 #endif // MEDIA_MIDI_MIDI_MANAGER_H_ |
OLD | NEW |