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 |
11 #include "base/basictypes.h" | 11 #include "base/basictypes.h" |
12 #include "base/memory/ref_counted.h" | 12 #include "base/memory/ref_counted.h" |
13 #include "base/synchronization/lock.h" | 13 #include "base/synchronization/lock.h" |
14 #include "base/time/time.h" | 14 #include "base/time/time.h" |
15 #include "media/midi/midi_export.h" | 15 #include "media/midi/midi_export.h" |
16 #include "media/midi/midi_port_info.h" | 16 #include "media/midi/midi_port_info.h" |
17 #include "media/midi/midi_result.h" | 17 #include "media/midi/result.h" |
18 | 18 |
19 namespace base { | 19 namespace base { |
20 class SingleThreadTaskRunner; | 20 class SingleThreadTaskRunner; |
21 } // namespace base | 21 } // namespace base |
22 | 22 |
23 namespace media { | 23 namespace media { |
24 namespace midi { | 24 namespace midi { |
25 | 25 |
26 // A MidiManagerClient registers with the MidiManager to receive MIDI data. | 26 // A MidiManagerClient registers with the MidiManager to receive MIDI data. |
27 // See MidiManager::RequestAccess() and MidiManager::ReleaseAccess() | 27 // See MidiManager::RequestAccess() and MidiManager::ReleaseAccess() |
28 // for details. | 28 // for details. |
29 class MIDI_EXPORT MidiManagerClient { | 29 class MIDI_EXPORT MidiManagerClient { |
30 public: | 30 public: |
31 virtual ~MidiManagerClient() {} | 31 virtual ~MidiManagerClient() {} |
32 | 32 |
33 // AddInputPort() and AddOutputPort() are called before CompleteStartSession() | 33 // AddInputPort() and AddOutputPort() are called before CompleteStartSession() |
34 // is called to notify existing MIDI ports, and also called after that to | 34 // is called to notify existing MIDI ports, and also called after that to |
35 // notify new MIDI ports are added. | 35 // notify new MIDI ports are added. |
36 virtual void AddInputPort(const MidiPortInfo& info) = 0; | 36 virtual void AddInputPort(const MidiPortInfo& info) = 0; |
37 virtual void AddOutputPort(const MidiPortInfo& info) = 0; | 37 virtual void AddOutputPort(const MidiPortInfo& info) = 0; |
38 | 38 |
39 // SetInputPortState() and SetOutputPortState() are called to notify a known | 39 // SetInputPortState() and SetOutputPortState() are called to notify a known |
40 // device gets disconnected, or connected again. | 40 // device gets disconnected, or connected again. |
41 virtual void SetInputPortState(uint32 port_index, MidiPortState state) = 0; | 41 virtual void SetInputPortState(uint32 port_index, MidiPortState state) = 0; |
42 virtual void SetOutputPortState(uint32 port_index, MidiPortState state) = 0; | 42 virtual void SetOutputPortState(uint32 port_index, MidiPortState state) = 0; |
43 | 43 |
44 // CompleteStartSession() is called when platform dependent preparation is | 44 // CompleteStartSession() is called when platform dependent preparation is |
45 // finished. | 45 // finished. |
46 virtual void CompleteStartSession(MidiResult result) = 0; | 46 virtual void CompleteStartSession(Result result) = 0; |
47 | 47 |
48 // ReceiveMidiData() is called when MIDI data has been received from the | 48 // ReceiveMidiData() is called when MIDI data has been received from the |
49 // MIDI system. | 49 // MIDI system. |
50 // |port_index| represents the specific input port from input_ports(). | 50 // |port_index| represents the specific input port from input_ports(). |
51 // |data| represents a series of bytes encoding one or more MIDI messages. | 51 // |data| represents a series of bytes encoding one or more MIDI messages. |
52 // |length| is the number of bytes in |data|. | 52 // |length| is the number of bytes in |data|. |
53 // |timestamp| is the time the data was received, in seconds. | 53 // |timestamp| is the time the data was received, in seconds. |
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, |
(...skipping 14 matching lines...) Expand all Loading... |
71 MidiManager(); | 71 MidiManager(); |
72 virtual ~MidiManager(); | 72 virtual ~MidiManager(); |
73 | 73 |
74 // The constructor and the destructor will be called on the CrBrowserMain | 74 // The constructor and the destructor will be called on the CrBrowserMain |
75 // thread. | 75 // thread. |
76 static MidiManager* Create(); | 76 static MidiManager* Create(); |
77 | 77 |
78 // A client calls StartSession() to receive and send MIDI data. | 78 // A client calls StartSession() to receive and send MIDI data. |
79 // If the session is ready to start, the MIDI system is lazily initialized | 79 // If the session is ready to start, the MIDI system is lazily initialized |
80 // and the client is registered to receive MIDI data. | 80 // and the client is registered to receive MIDI data. |
81 // CompleteStartSession() is called with MIDI_OK if the session is started. | 81 // CompleteStartSession() is called with Result::OK if the session is started. |
82 // Otherwise CompleteStartSession() is called with proper MidiResult code. | 82 // Otherwise CompleteStartSession() is called with proper Result code. |
83 // StartSession() and EndSession() can be called on the Chrome_IOThread. | 83 // StartSession() and EndSession() can be called on the Chrome_IOThread. |
84 // CompleteStartSession() will be invoked on the same Chrome_IOThread. | 84 // CompleteStartSession() will be invoked on the same Chrome_IOThread. |
85 void StartSession(MidiManagerClient* client); | 85 void StartSession(MidiManagerClient* client); |
86 | 86 |
87 // A client calls EndSession() to stop receiving MIDI data. | 87 // A client calls EndSession() to stop receiving MIDI data. |
88 void EndSession(MidiManagerClient* client); | 88 void EndSession(MidiManagerClient* client); |
89 | 89 |
90 // Invoke AccumulateMidiBytesSent() for |client| safely. If the session was | 90 // Invoke AccumulateMidiBytesSent() for |client| safely. If the session was |
91 // already closed, do nothing. | 91 // already closed, do nothing. |
92 void AccumulateMidiBytesSent(MidiManagerClient* client, size_t n); | 92 void AccumulateMidiBytesSent(MidiManagerClient* client, size_t n); |
(...skipping 10 matching lines...) Expand all Loading... |
103 virtual void DispatchSendMidiData(MidiManagerClient* client, | 103 virtual void DispatchSendMidiData(MidiManagerClient* client, |
104 uint32 port_index, | 104 uint32 port_index, |
105 const std::vector<uint8>& data, | 105 const std::vector<uint8>& data, |
106 double timestamp); | 106 double timestamp); |
107 | 107 |
108 protected: | 108 protected: |
109 friend class MidiManagerUsb; | 109 friend class MidiManagerUsb; |
110 | 110 |
111 // Initializes the platform dependent MIDI system. MidiManager class has a | 111 // Initializes the platform dependent MIDI system. MidiManager class has a |
112 // default implementation that synchronously calls CompleteInitialization() | 112 // default implementation that synchronously calls CompleteInitialization() |
113 // with MIDI_NOT_SUPPORTED on the caller thread. A derived class for a | 113 // with Result::NOT_SUPPORTED on the caller thread. A derived class for a |
114 // specific platform should override this method correctly. | 114 // specific platform should override this method correctly. |
115 // This method is called on Chrome_IOThread thread inside StartSession(). | 115 // This method is called on Chrome_IOThread thread inside StartSession(). |
116 // Platform dependent initialization can be processed synchronously or | 116 // Platform dependent initialization can be processed synchronously or |
117 // asynchronously. When the initialization is completed, | 117 // asynchronously. When the initialization is completed, |
118 // CompleteInitialization() should be called with |result|. | 118 // CompleteInitialization() should be called with |result|. |
119 // |result| should be MIDI_OK on success, otherwise a proper MidiResult. | 119 // |result| should be Result::OK on success, otherwise a proper Result. |
120 virtual void StartInitialization(); | 120 virtual void StartInitialization(); |
121 | 121 |
122 // Called from a platform dependent implementation of StartInitialization(). | 122 // Called from a platform dependent implementation of StartInitialization(). |
123 // It invokes CompleteInitializationInternal() on the thread that calls | 123 // It invokes CompleteInitializationInternal() on the thread that calls |
124 // StartSession() and distributes |result| to MIDIManagerClient objects in | 124 // StartSession() and distributes |result| to MIDIManagerClient objects in |
125 // |pending_clients_|. | 125 // |pending_clients_|. |
126 void CompleteInitialization(MidiResult result); | 126 void CompleteInitialization(Result result); |
127 | 127 |
128 void AddInputPort(const MidiPortInfo& info); | 128 void AddInputPort(const MidiPortInfo& info); |
129 void AddOutputPort(const MidiPortInfo& info); | 129 void AddOutputPort(const MidiPortInfo& info); |
130 void SetInputPortState(uint32 port_index, MidiPortState state); | 130 void SetInputPortState(uint32 port_index, MidiPortState state); |
131 void SetOutputPortState(uint32 port_index, MidiPortState state); | 131 void SetOutputPortState(uint32 port_index, MidiPortState state); |
132 | 132 |
133 // Dispatches to all clients. | 133 // Dispatches to all clients. |
134 // TODO(toyoshim): Fix the mac implementation to use | 134 // TODO(toyoshim): Fix the mac implementation to use |
135 // |ReceiveMidiData(..., base::TimeTicks)|. | 135 // |ReceiveMidiData(..., base::TimeTicks)|. |
136 void ReceiveMidiData(uint32 port_index, | 136 void ReceiveMidiData(uint32 port_index, |
137 const uint8* data, | 137 const uint8* data, |
138 size_t length, | 138 size_t length, |
139 double timestamp); | 139 double timestamp); |
140 | 140 |
141 void ReceiveMidiData(uint32 port_index, | 141 void ReceiveMidiData(uint32 port_index, |
142 const uint8* data, | 142 const uint8* data, |
143 size_t length, | 143 size_t length, |
144 base::TimeTicks time) { | 144 base::TimeTicks time) { |
145 ReceiveMidiData(port_index, data, length, | 145 ReceiveMidiData(port_index, data, length, |
146 (time - base::TimeTicks()).InSecondsF()); | 146 (time - base::TimeTicks()).InSecondsF()); |
147 } | 147 } |
148 | 148 |
149 size_t clients_size_for_testing() const { return clients_.size(); } | 149 size_t clients_size_for_testing() const { return clients_.size(); } |
150 size_t pending_clients_size_for_testing() const { | 150 size_t pending_clients_size_for_testing() const { |
151 return pending_clients_.size(); | 151 return pending_clients_.size(); |
152 } | 152 } |
153 | 153 |
154 private: | 154 private: |
155 void CompleteInitializationInternal(MidiResult result); | 155 void CompleteInitializationInternal(Result result); |
156 void AddInitialPorts(MidiManagerClient* client); | 156 void AddInitialPorts(MidiManagerClient* client); |
157 | 157 |
158 // Keeps track of all clients who wish to receive MIDI data. | 158 // Keeps track of all clients who wish to receive MIDI data. |
159 typedef std::set<MidiManagerClient*> ClientSet; | 159 typedef std::set<MidiManagerClient*> ClientSet; |
160 ClientSet clients_; | 160 ClientSet clients_; |
161 | 161 |
162 // Keeps track of all clients who are waiting for CompleteStartSession(). | 162 // Keeps track of all clients who are waiting for CompleteStartSession(). |
163 ClientSet pending_clients_; | 163 ClientSet pending_clients_; |
164 | 164 |
165 // Keeps a SingleThreadTaskRunner of the thread that calls StartSession in | 165 // Keeps a SingleThreadTaskRunner of the thread that calls StartSession in |
166 // order to invoke CompleteStartSession() on the thread. | 166 // order to invoke CompleteStartSession() on the thread. |
167 scoped_refptr<base::SingleThreadTaskRunner> session_thread_runner_; | 167 scoped_refptr<base::SingleThreadTaskRunner> session_thread_runner_; |
168 | 168 |
169 // Keeps true if platform dependent initialization is already completed. | 169 // Keeps true if platform dependent initialization is already completed. |
170 bool initialized_; | 170 bool initialized_; |
171 | 171 |
172 // Keeps the platform dependent initialization result if initialization is | 172 // Keeps the platform dependent initialization result if initialization is |
173 // completed. Otherwise keeps MIDI_NOT_SUPPORTED. | 173 // completed. Otherwise keeps Result::NOT_INITIALIZED. |
174 MidiResult result_; | 174 Result result_; |
175 | 175 |
176 // Keeps all MidiPortInfo. | 176 // Keeps all MidiPortInfo. |
177 MidiPortInfoList input_ports_; | 177 MidiPortInfoList input_ports_; |
178 MidiPortInfoList output_ports_; | 178 MidiPortInfoList output_ports_; |
179 | 179 |
180 // Protects access to |clients_|, |pending_clients_|, |initialized_|, | 180 // Protects access to |clients_|, |pending_clients_|, |initialized_|, |
181 // |result_|, |input_ports_| and |output_ports_|. | 181 // |result_|, |input_ports_| and |output_ports_|. |
182 base::Lock lock_; | 182 base::Lock lock_; |
183 | 183 |
184 DISALLOW_COPY_AND_ASSIGN(MidiManager); | 184 DISALLOW_COPY_AND_ASSIGN(MidiManager); |
185 }; | 185 }; |
186 | 186 |
187 } // namespace midi | 187 } // namespace midi |
188 } // namespace media | 188 } // namespace media |
189 | 189 |
190 #endif // MEDIA_MIDI_MIDI_MANAGER_H_ | 190 #endif // MEDIA_MIDI_MIDI_MANAGER_H_ |
OLD | NEW |