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() const { return clients_.size(); } |
127 MidiResult result_; | 141 size_t get_pending_clients_size_for_testing() const { |
| 142 return pending_clients_.size(); |
| 143 } |
| 144 |
| 145 // TODO(toyoshim): Make |input_ports_| and |output_ports_| private members. |
| 146 MidiPortInfoList input_ports_; |
| 147 MidiPortInfoList output_ports_; |
| 148 |
| 149 private: |
| 150 void CompleteInitializationInternal(MidiResult result); |
128 | 151 |
129 // Keeps track of all clients who wish to receive MIDI data. | 152 // Keeps track of all clients who wish to receive MIDI data. |
130 typedef std::set<MidiManagerClient*> ClientList; | 153 typedef std::set<MidiManagerClient*> ClientList; |
131 ClientList clients_; | 154 ClientList clients_; |
132 | 155 |
133 // Keeps track of all clients who are waiting for CompleteStartSession(). | 156 // Keeps track of all clients who are waiting for CompleteStartSession(). |
134 typedef std::map<int, MidiManagerClient*> PendingClientMap; | 157 typedef std::map<int, MidiManagerClient*> PendingClientMap; |
135 PendingClientMap pending_clients_; | 158 PendingClientMap pending_clients_; |
136 | 159 |
137 // Protects access to our clients, |clients_| and |pending_clients_|. | 160 // Keeps a SingleThreadTaskRunner of the thread that calls StartSession in |
138 base::Lock clients_lock_; | 161 // order to invoke CompleteStartSession() on the thread. |
| 162 scoped_refptr<base::SingleThreadTaskRunner> session_thread_runner_; |
139 | 163 |
140 MidiPortInfoList input_ports_; | 164 // Keeps true if platform dependent initialization is already completed. |
141 MidiPortInfoList output_ports_; | 165 bool initialized_; |
142 | 166 |
143 private: | 167 // Keeps the platform dependent initialization result if initialization is |
| 168 // completed. Otherwise keeps MIDI_NOT_SUPPORTED. |
| 169 MidiResult result_; |
| 170 |
| 171 // Protects access to |clients_|, |pending_clients_|, |initialized_|, and |
| 172 // |result_|. |
| 173 base::Lock lock_; |
| 174 |
144 DISALLOW_COPY_AND_ASSIGN(MidiManager); | 175 DISALLOW_COPY_AND_ASSIGN(MidiManager); |
145 }; | 176 }; |
146 | 177 |
147 } // namespace media | 178 } // namespace media |
148 | 179 |
149 #endif // MEDIA_MIDI_MIDI_MANAGER_H_ | 180 #endif // MEDIA_MIDI_MIDI_MANAGER_H_ |
OLD | NEW |