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 <map> | 8 #include <map> |
9 #include <set> | 9 #include <set> |
10 #include <vector> | 10 #include <vector> |
11 | 11 |
12 #include "base/basictypes.h" | 12 #include "base/basictypes.h" |
13 #include "base/memory/ref_counted.h" | |
13 #include "base/synchronization/lock.h" | 14 #include "base/synchronization/lock.h" |
14 #include "base/time/time.h" | 15 #include "base/time/time.h" |
15 #include "media/base/media_export.h" | 16 #include "media/base/media_export.h" |
16 #include "media/midi/midi_port_info.h" | 17 #include "media/midi/midi_port_info.h" |
17 #include "media/midi/midi_result.h" | 18 #include "media/midi/midi_result.h" |
18 | 19 |
20 namespace base { | |
21 class SingleThreadTaskRunner; | |
22 } // namespace base | |
23 | |
19 namespace media { | 24 namespace media { |
20 | 25 |
21 // A MidiManagerClient registers with the MidiManager to receive MIDI data. | 26 // A MidiManagerClient registers with the MidiManager to receive MIDI data. |
22 // See MidiManager::RequestAccess() and MidiManager::ReleaseAccess() | 27 // See MidiManager::RequestAccess() and MidiManager::ReleaseAccess() |
23 // for details. | 28 // for details. |
24 class MEDIA_EXPORT MidiManagerClient { | 29 class MEDIA_EXPORT MidiManagerClient { |
25 public: | 30 public: |
26 virtual ~MidiManagerClient() {} | 31 virtual ~MidiManagerClient() {} |
27 | 32 |
28 // CompleteStartSession() is called when platform dependent preparation is | 33 // CompleteStartSession() is called when platform dependent preparation is |
(...skipping 14 matching lines...) Expand all Loading... | |
43 // AccumulateMidiBytesSent() is called to acknowledge when bytes have | 48 // AccumulateMidiBytesSent() is called to acknowledge when bytes have |
44 // successfully been sent to the hardware. | 49 // successfully been sent to the hardware. |
45 // This happens as a result of the client having previously called | 50 // This happens as a result of the client having previously called |
46 // MidiManager::DispatchSendMidiData(). | 51 // MidiManager::DispatchSendMidiData(). |
47 virtual void AccumulateMidiBytesSent(size_t n) = 0; | 52 virtual void AccumulateMidiBytesSent(size_t n) = 0; |
48 }; | 53 }; |
49 | 54 |
50 // Manages access to all MIDI hardware. | 55 // Manages access to all MIDI hardware. |
51 class MEDIA_EXPORT MidiManager { | 56 class MEDIA_EXPORT MidiManager { |
52 public: | 57 public: |
58 // The constructor and the destructor will be called on the CrBrowserMain | |
59 // thread. | |
53 static MidiManager* Create(); | 60 static MidiManager* Create(); |
54 | 61 |
55 MidiManager(); | 62 MidiManager(); |
56 virtual ~MidiManager(); | 63 virtual ~MidiManager(); |
57 | 64 |
58 // A client calls StartSession() to receive and send MIDI data. | 65 // A client calls StartSession() to receive and send MIDI data. |
59 // If the session is ready to start, the MIDI system is lazily initialized | 66 // If the session is ready to start, the MIDI system is lazily initialized |
60 // and the client is registered to receive MIDI data. | 67 // and the client is registered to receive MIDI data. |
61 // CompleteStartSession() is called with MIDI_OK if the session is started. | 68 // CompleteStartSession() is called with MIDI_OK if the session is started. |
62 // Otherwise CompleteStartSession() is called with proper MidiResult code. | 69 // Otherwise CompleteStartSession() is called with proper MidiResult code. |
70 // StartSession() and EndSession() can be called on the Chrome_IOThread. | |
71 // CompleteStartSession() will be invoked on the same Chrome_IOThread. | |
63 void StartSession(MidiManagerClient* client, int client_id); | 72 void StartSession(MidiManagerClient* client, int client_id); |
64 | 73 |
65 // A client calls EndSession() to stop receiving MIDI data. | 74 // A client calls EndSession() to stop receiving MIDI data. |
66 void EndSession(MidiManagerClient* client); | 75 void EndSession(MidiManagerClient* client); |
67 | 76 |
68 // DispatchSendMidiData() is called when MIDI data should be sent to the MIDI | 77 // DispatchSendMidiData() is called when MIDI data should be sent to the MIDI |
69 // system. | 78 // system. |
70 // This method is supposed to return immediately and should not block. | 79 // This method is supposed to return immediately and should not block. |
71 // |port_index| represents the specific output port from output_ports(). | 80 // |port_index| represents the specific output port from output_ports(). |
72 // |data| represents a series of bytes encoding one or more MIDI messages. | 81 // |data| represents a series of bytes encoding one or more MIDI messages. |
(...skipping 12 matching lines...) Expand all Loading... | |
85 const MidiPortInfoList& input_ports() { return input_ports_; } | 94 const MidiPortInfoList& input_ports() { return input_ports_; } |
86 | 95 |
87 // output_ports() is a list of MIDI ports for sending MIDI data. | 96 // output_ports() is a list of MIDI ports for sending MIDI data. |
88 // Each individual port in this list can be identified by its | 97 // Each individual port in this list can be identified by its |
89 // integer index into this list. | 98 // integer index into this list. |
90 const MidiPortInfoList& output_ports() { return output_ports_; } | 99 const MidiPortInfoList& output_ports() { return output_ports_; } |
91 | 100 |
92 protected: | 101 protected: |
93 friend class MidiManagerUsb; | 102 friend class MidiManagerUsb; |
94 | 103 |
95 // Initializes the platform dependent MIDI system. It will call | 104 // Initializes the platform dependent MIDI system. MidiManager class has a |
96 // CompleteInitialization() asynchronously when initialization is finished. | 105 // default implementation that synchronously calls CompleteInitialization() |
97 // |result| of CompleteInitialization() will be MIDI_OK on success. | 106 // with MIDI_NOT_SUPPORTED on the caller thread. A derived class for a |
98 // MidiManager has a default implementation that calls | 107 // specific platform should override this method correctly. |
99 // CompleteInitialization() with MIDI_NOT_SUPPORTED. | 108 // This method is called on Chrome_IOThread thread inside StartSession(). |
109 // Platform dependent initialization can be processed synchronously or | |
110 // asynchronously. When the initialization is completed, | |
111 // CompleteInitialization() should be called with |result|. | |
112 // |result| should be MIDI_OK on success, otherwise a proper MidiResult. | |
100 virtual void StartInitialization(); | 113 virtual void StartInitialization(); |
101 | 114 |
102 // Called from a platform dependent implementation of StartInitialization(). | 115 // Called from a platform dependent implementation of StartInitialization(). |
103 // It will distribute |result| to MIDIManagerClient objects in | 116 // It invokes CompleteInitializationInternal() on the thread that calls |
117 // StartSession() and distributes |result| to MIDIManagerClient objects in | |
104 // |pending_clients_|. | 118 // |pending_clients_|. |
105 void CompleteInitialization(MidiResult result); | 119 void CompleteInitialization(MidiResult result); |
106 | 120 |
107 void AddInputPort(const MidiPortInfo& info); | 121 void AddInputPort(const MidiPortInfo& info); |
108 void AddOutputPort(const MidiPortInfo& info); | 122 void AddOutputPort(const MidiPortInfo& info); |
109 | 123 |
110 // Dispatches to all clients. | 124 // Dispatches to all clients. |
111 // TODO(toyoshim): Fix the mac implementation to use | 125 // TODO(toyoshim): Fix the mac implementation to use |
112 // |ReceiveMidiData(..., base::TimeTicks)|. | 126 // |ReceiveMidiData(..., base::TimeTicks)|. |
113 void ReceiveMidiData(uint32 port_index, | 127 void ReceiveMidiData(uint32 port_index, |
114 const uint8* data, | 128 const uint8* data, |
115 size_t length, | 129 size_t length, |
116 double timestamp); | 130 double timestamp); |
117 | 131 |
118 void ReceiveMidiData(uint32 port_index, | 132 void ReceiveMidiData(uint32 port_index, |
119 const uint8* data, | 133 const uint8* data, |
120 size_t length, | 134 size_t length, |
121 base::TimeTicks time) { | 135 base::TimeTicks time) { |
122 ReceiveMidiData(port_index, data, length, | 136 ReceiveMidiData(port_index, data, length, |
123 (time - base::TimeTicks()).InSecondsF()); | 137 (time - base::TimeTicks()).InSecondsF()); |
124 } | 138 } |
125 | 139 |
126 bool initialized_; | 140 size_t get_clients_size_for_testing() { return clients_.size(); } |
yukawa
2014/05/06 02:46:02
nit: size_t get_clients_size_for_testing() const {
Takashi Toyoshima
2014/05/06 11:11:52
Done.
| |
127 MidiResult result_; | 141 size_t get_pending_clients_size_for_testing() { |
yukawa
2014/05/06 02:46:02
nit: size_t get_pending_clients_size_for_testing()
Takashi Toyoshima
2014/05/06 11:11:52
Done.
| |
142 return pending_clients_.size(); | |
143 } | |
144 | |
145 private: | |
146 void CompleteInitializationInternal(MidiResult result); | |
128 | 147 |
129 // Keeps track of all clients who wish to receive MIDI data. | 148 // Keeps track of all clients who wish to receive MIDI data. |
130 typedef std::set<MidiManagerClient*> ClientList; | 149 typedef std::set<MidiManagerClient*> ClientList; |
131 ClientList clients_; | 150 ClientList clients_; |
132 | 151 |
133 // Keeps track of all clients who are waiting for CompleteStartSession(). | 152 // Keeps track of all clients who are waiting for CompleteStartSession(). |
134 typedef std::map<int, MidiManagerClient*> PendingClientMap; | 153 typedef std::map<int, MidiManagerClient*> PendingClientMap; |
135 PendingClientMap pending_clients_; | 154 PendingClientMap pending_clients_; |
136 | 155 |
137 // Protects access to our clients, |clients_| and |pending_clients_|. | 156 // Keeps a SingleThreadTaskRunner of the thread that calls StartSession in |
138 base::Lock clients_lock_; | 157 // order to invoke CompleteStartSession() on the thread. |
158 scoped_refptr<base::SingleThreadTaskRunner> session_thread_runner_; | |
159 | |
160 // Keeps true if platform dependent initialization is already completed. | |
161 bool initialized_; | |
162 | |
163 // Keeps the platform dependent initialization result if initialization is | |
164 // completed. Otherwise keeps MIDI_NOT_SUPPORTED. | |
165 MidiResult result_; | |
166 | |
167 // Protects access to |clients_|, |pending_clients_|, |initialized_|, and | |
168 // |result_|. | |
169 base::Lock lock_; | |
139 | 170 |
140 MidiPortInfoList input_ports_; | 171 MidiPortInfoList input_ports_; |
141 MidiPortInfoList output_ports_; | 172 MidiPortInfoList output_ports_; |
142 | 173 |
143 private: | |
144 DISALLOW_COPY_AND_ASSIGN(MidiManager); | 174 DISALLOW_COPY_AND_ASSIGN(MidiManager); |
145 }; | 175 }; |
146 | 176 |
147 } // namespace media | 177 } // namespace media |
148 | 178 |
149 #endif // MEDIA_MIDI_MIDI_MANAGER_H_ | 179 #endif // MEDIA_MIDI_MIDI_MANAGER_H_ |
OLD | NEW |