All instances of addresses as void* are changed to uintptr_t in
[chromium-blink-merge.git] / media / midi / midi_manager.h
blob957c84d427f0dc85b291cd6b9cee188067dca5d7
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
3 // found in the LICENSE file.
5 #ifndef MEDIA_MIDI_MIDI_MANAGER_H_
6 #define MEDIA_MIDI_MIDI_MANAGER_H_
8 #include <set>
9 #include <vector>
11 #include "base/basictypes.h"
12 #include "base/memory/ref_counted.h"
13 #include "base/synchronization/lock.h"
14 #include "base/time/time.h"
15 #include "media/midi/midi_export.h"
16 #include "media/midi/midi_port_info.h"
17 #include "media/midi/result.h"
19 namespace base {
20 class SingleThreadTaskRunner;
21 } // namespace base
23 namespace media {
24 namespace midi {
26 // A MidiManagerClient registers with the MidiManager to receive MIDI data.
27 // See MidiManager::RequestAccess() and MidiManager::ReleaseAccess()
28 // for details.
29 class MIDI_EXPORT MidiManagerClient {
30 public:
31 virtual ~MidiManagerClient() {}
33 // AddInputPort() and AddOutputPort() are called before CompleteStartSession()
34 // is called to notify existing MIDI ports, and also called after that to
35 // notify new MIDI ports are added.
36 virtual void AddInputPort(const MidiPortInfo& info) = 0;
37 virtual void AddOutputPort(const MidiPortInfo& info) = 0;
39 // SetInputPortState() and SetOutputPortState() are called to notify a known
40 // device gets disconnected, or connected again.
41 virtual void SetInputPortState(uint32 port_index, MidiPortState state) = 0;
42 virtual void SetOutputPortState(uint32 port_index, MidiPortState state) = 0;
44 // CompleteStartSession() is called when platform dependent preparation is
45 // finished.
46 virtual void CompleteStartSession(Result result) = 0;
48 // ReceiveMidiData() is called when MIDI data has been received from the
49 // MIDI system.
50 // |port_index| represents the specific input port from input_ports().
51 // |data| represents a series of bytes encoding one or more MIDI messages.
52 // |length| is the number of bytes in |data|.
53 // |timestamp| is the time the data was received, in seconds.
54 virtual void ReceiveMidiData(uint32 port_index,
55 const uint8* data,
56 size_t length,
57 double timestamp) = 0;
59 // AccumulateMidiBytesSent() is called to acknowledge when bytes have
60 // successfully been sent to the hardware.
61 // This happens as a result of the client having previously called
62 // MidiManager::DispatchSendMidiData().
63 virtual void AccumulateMidiBytesSent(size_t n) = 0;
66 // Manages access to all MIDI hardware.
67 class MIDI_EXPORT MidiManager {
68 public:
69 static const size_t kMaxPendingClientCount = 128;
71 MidiManager();
72 virtual ~MidiManager();
74 // The constructor and the destructor will be called on the CrBrowserMain
75 // thread.
76 static MidiManager* Create();
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
80 // and the client is registered to receive MIDI data.
81 // CompleteStartSession() is called with Result::OK if the session is started.
82 // Otherwise CompleteStartSession() is called with proper Result code.
83 // StartSession() and EndSession() can be called on the Chrome_IOThread.
84 // CompleteStartSession() will be invoked on the same Chrome_IOThread.
85 void StartSession(MidiManagerClient* client);
87 // A client calls EndSession() to stop receiving MIDI data.
88 void EndSession(MidiManagerClient* client);
90 // Invoke AccumulateMidiBytesSent() for |client| safely. If the session was
91 // already closed, do nothing.
92 void AccumulateMidiBytesSent(MidiManagerClient* client, size_t n);
94 // DispatchSendMidiData() is called when MIDI data should be sent to the MIDI
95 // system.
96 // This method is supposed to return immediately and should not block.
97 // |port_index| represents the specific output port from output_ports().
98 // |data| represents a series of bytes encoding one or more MIDI messages.
99 // |length| is the number of bytes in |data|.
100 // |timestamp| is the time to send the data, in seconds. A value of 0
101 // means send "now" or as soon as possible.
102 // The default implementation is for unsupported platforms.
103 virtual void DispatchSendMidiData(MidiManagerClient* client,
104 uint32 port_index,
105 const std::vector<uint8>& data,
106 double timestamp);
108 protected:
109 friend class MidiManagerUsb;
111 // Initializes the platform dependent MIDI system. MidiManager class has a
112 // default implementation that synchronously calls CompleteInitialization()
113 // with Result::NOT_SUPPORTED on the caller thread. A derived class for a
114 // specific platform should override this method correctly.
115 // This method is called on Chrome_IOThread thread inside StartSession().
116 // Platform dependent initialization can be processed synchronously or
117 // asynchronously. When the initialization is completed,
118 // CompleteInitialization() should be called with |result|.
119 // |result| should be Result::OK on success, otherwise a proper Result.
120 virtual void StartInitialization();
122 // Called from a platform dependent implementation of StartInitialization().
123 // It invokes CompleteInitializationInternal() on the thread that calls
124 // StartSession() and distributes |result| to MIDIManagerClient objects in
125 // |pending_clients_|.
126 void CompleteInitialization(Result result);
128 void AddInputPort(const MidiPortInfo& info);
129 void AddOutputPort(const MidiPortInfo& info);
130 void SetInputPortState(uint32 port_index, MidiPortState state);
131 void SetOutputPortState(uint32 port_index, MidiPortState state);
133 // Dispatches to all clients.
134 // TODO(toyoshim): Fix the mac implementation to use
135 // |ReceiveMidiData(..., base::TimeTicks)|.
136 void ReceiveMidiData(uint32 port_index,
137 const uint8* data,
138 size_t length,
139 double timestamp);
141 void ReceiveMidiData(uint32 port_index,
142 const uint8* data,
143 size_t length,
144 base::TimeTicks time) {
145 ReceiveMidiData(port_index, data, length,
146 (time - base::TimeTicks()).InSecondsF());
149 size_t clients_size_for_testing() const { return clients_.size(); }
150 size_t pending_clients_size_for_testing() const {
151 return pending_clients_.size();
154 private:
155 void CompleteInitializationInternal(Result result);
156 void AddInitialPorts(MidiManagerClient* client);
158 // Keeps track of all clients who wish to receive MIDI data.
159 typedef std::set<MidiManagerClient*> ClientSet;
160 ClientSet clients_;
162 // Keeps track of all clients who are waiting for CompleteStartSession().
163 ClientSet pending_clients_;
165 // Keeps a SingleThreadTaskRunner of the thread that calls StartSession in
166 // order to invoke CompleteStartSession() on the thread.
167 scoped_refptr<base::SingleThreadTaskRunner> session_thread_runner_;
169 // Keeps true if platform dependent initialization is already completed.
170 bool initialized_;
172 // Keeps the platform dependent initialization result if initialization is
173 // completed. Otherwise keeps Result::NOT_INITIALIZED.
174 Result result_;
176 // Keeps all MidiPortInfo.
177 MidiPortInfoList input_ports_;
178 MidiPortInfoList output_ports_;
180 // Protects access to |clients_|, |pending_clients_|, |initialized_|,
181 // |result_|, |input_ports_| and |output_ports_|.
182 base::Lock lock_;
184 DISALLOW_COPY_AND_ASSIGN(MidiManager);
187 } // namespace midi
188 } // namespace media
190 #endif // MEDIA_MIDI_MIDI_MANAGER_H_