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. It is called on |
96 // CompleteInitialization() asynchronously when initialization is finished. | 105 // Chrome_IOThread, but will invoke CompleteInitialization() asynchronously |
106 // on the same thread or another thread when initialization is finished. | |
97 // |result| of CompleteInitialization() will be MIDI_OK on success. | 107 // |result| of CompleteInitialization() will be MIDI_OK on success. |
98 // MidiManager has a default implementation that calls | 108 // MidiManager has a default implementation that synchronously calls |
99 // CompleteInitialization() with MIDI_NOT_SUPPORTED. | 109 // CompleteInitialization() with MIDI_NOT_SUPPORTED on the caller thread. |
100 virtual void StartInitialization(); | 110 virtual void StartInitialization(); |
101 | 111 |
102 // Called from a platform dependent implementation of StartInitialization(). | 112 // Called from a platform dependent implementation of StartInitialization(). |
103 // It will distribute |result| to MIDIManagerClient objects in | 113 // It invokes CompleteInitializationInternal() on the thread that calls |
114 // StartSession() and distributes |result| to MIDIManagerClient objects in | |
104 // |pending_clients_|. | 115 // |pending_clients_|. |
105 void CompleteInitialization(MidiResult result); | 116 void CompleteInitialization(MidiResult result); |
106 | 117 |
107 void AddInputPort(const MidiPortInfo& info); | 118 void AddInputPort(const MidiPortInfo& info); |
108 void AddOutputPort(const MidiPortInfo& info); | 119 void AddOutputPort(const MidiPortInfo& info); |
109 | 120 |
110 // Dispatches to all clients. | 121 // Dispatches to all clients. |
111 // TODO(toyoshim): Fix the mac implementation to use | 122 // TODO(toyoshim): Fix the mac implementation to use |
112 // |ReceiveMidiData(..., base::TimeTicks)|. | 123 // |ReceiveMidiData(..., base::TimeTicks)|. |
113 void ReceiveMidiData(uint32 port_index, | 124 void ReceiveMidiData(uint32 port_index, |
114 const uint8* data, | 125 const uint8* data, |
115 size_t length, | 126 size_t length, |
116 double timestamp); | 127 double timestamp); |
117 | 128 |
118 void ReceiveMidiData(uint32 port_index, | 129 void ReceiveMidiData(uint32 port_index, |
119 const uint8* data, | 130 const uint8* data, |
120 size_t length, | 131 size_t length, |
121 base::TimeTicks time) { | 132 base::TimeTicks time) { |
122 ReceiveMidiData(port_index, data, length, | 133 ReceiveMidiData(port_index, data, length, |
123 (time - base::TimeTicks()).InSecondsF()); | 134 (time - base::TimeTicks()).InSecondsF()); |
124 } | 135 } |
125 | 136 |
126 bool initialized_; | |
127 MidiResult result_; | |
128 | |
129 // Keeps track of all clients who wish to receive MIDI data. | 137 // Keeps track of all clients who wish to receive MIDI data. |
130 typedef std::set<MidiManagerClient*> ClientList; | 138 typedef std::set<MidiManagerClient*> ClientList; |
131 ClientList clients_; | 139 ClientList clients_; |
132 | 140 |
133 // Keeps track of all clients who are waiting for CompleteStartSession(). | 141 // Keeps track of all clients who are waiting for CompleteStartSession(). |
134 typedef std::map<int, MidiManagerClient*> PendingClientMap; | 142 typedef std::map<int, MidiManagerClient*> PendingClientMap; |
135 PendingClientMap pending_clients_; | 143 PendingClientMap pending_clients_; |
136 | 144 |
145 private: | |
146 void CompleteInitializationInternal(MidiResult result); | |
147 | |
148 // Keeps a SingleThreadTaskRunner of the thread that calls StartSession in | |
149 // order to invoke CompleteStartSession() on the thread. | |
150 scoped_refptr<base::SingleThreadTaskRunner> session_thread_runner_; | |
151 | |
152 // Keeps true if platform dependent initialization is already completed. | |
yukawa
2014/05/04 23:16:02
It seems that |initialized_| and |result_| are asy
Takashi Toyoshima
2014/05/05 00:45:08
|clients_lock_| is used, so I should rename it to
| |
153 bool initialized_; | |
154 | |
155 // Keeps the platform dependent initialization result if initialization is | |
156 // completed. Otherwise keeps MIDI_NOT_SUPPORTED. | |
157 MidiResult result_; | |
158 | |
137 // Protects access to our clients, |clients_| and |pending_clients_|. | 159 // Protects access to our clients, |clients_| and |pending_clients_|. |
138 base::Lock clients_lock_; | 160 base::Lock clients_lock_; |
139 | 161 |
140 MidiPortInfoList input_ports_; | 162 MidiPortInfoList input_ports_; |
141 MidiPortInfoList output_ports_; | 163 MidiPortInfoList output_ports_; |
142 | 164 |
143 private: | |
144 DISALLOW_COPY_AND_ASSIGN(MidiManager); | 165 DISALLOW_COPY_AND_ASSIGN(MidiManager); |
145 }; | 166 }; |
146 | 167 |
147 } // namespace media | 168 } // namespace media |
148 | 169 |
149 #endif // MEDIA_MIDI_MIDI_MANAGER_H_ | 170 #endif // MEDIA_MIDI_MIDI_MANAGER_H_ |
OLD | NEW |