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_
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/base/media_export.h"
16 #include "media/midi/midi_port_info.h"
17 #include "media/midi/midi_result.h"
20 class SingleThreadTaskRunner
;
25 // A MidiManagerClient registers with the MidiManager to receive MIDI data.
26 // See MidiManager::RequestAccess() and MidiManager::ReleaseAccess()
28 class MEDIA_EXPORT MidiManagerClient
{
30 virtual ~MidiManagerClient() {}
32 // AddInputPort() and AddOutputPort() are called before CompleteStartSession()
33 // is called to notify existing MIDI ports, and also called after that to
34 // notify new MIDI ports are added.
35 virtual void AddInputPort(const MidiPortInfo
& info
) = 0;
36 virtual void AddOutputPort(const MidiPortInfo
& info
) = 0;
38 // SetInputPortState() and SetOutputPortState() are called to notify a known
39 // device gets disconnected, or connected again.
40 virtual void SetInputPortState(uint32 port_index
, MidiPortState state
) = 0;
41 virtual void SetOutputPortState(uint32 port_index
, MidiPortState state
) = 0;
43 // CompleteStartSession() is called when platform dependent preparation is
45 virtual void CompleteStartSession(MidiResult result
) = 0;
47 // ReceiveMidiData() is called when MIDI data has been received from the
49 // |port_index| represents the specific input port from input_ports().
50 // |data| represents a series of bytes encoding one or more MIDI messages.
51 // |length| is the number of bytes in |data|.
52 // |timestamp| is the time the data was received, in seconds.
53 virtual void ReceiveMidiData(uint32 port_index
,
56 double timestamp
) = 0;
58 // AccumulateMidiBytesSent() is called to acknowledge when bytes have
59 // successfully been sent to the hardware.
60 // This happens as a result of the client having previously called
61 // MidiManager::DispatchSendMidiData().
62 virtual void AccumulateMidiBytesSent(size_t n
) = 0;
65 // Manages access to all MIDI hardware.
66 class MEDIA_EXPORT MidiManager
{
68 static const size_t kMaxPendingClientCount
= 128;
71 virtual ~MidiManager();
73 // The constructor and the destructor will be called on the CrBrowserMain
75 static MidiManager
* Create();
77 // A client calls StartSession() to receive and send MIDI data.
78 // If the session is ready to start, the MIDI system is lazily initialized
79 // and the client is registered to receive MIDI data.
80 // CompleteStartSession() is called with MIDI_OK if the session is started.
81 // Otherwise CompleteStartSession() is called with proper MidiResult code.
82 // StartSession() and EndSession() can be called on the Chrome_IOThread.
83 // CompleteStartSession() will be invoked on the same Chrome_IOThread.
84 void StartSession(MidiManagerClient
* client
);
86 // A client calls EndSession() to stop receiving MIDI data.
87 void EndSession(MidiManagerClient
* client
);
89 // Invoke AccumulateMidiBytesSent() for |client| safely. If the session was
90 // already closed, do nothing.
91 void AccumulateMidiBytesSent(MidiManagerClient
* client
, size_t n
);
93 // DispatchSendMidiData() is called when MIDI data should be sent to the MIDI
95 // This method is supposed to return immediately and should not block.
96 // |port_index| represents the specific output port from output_ports().
97 // |data| represents a series of bytes encoding one or more MIDI messages.
98 // |length| is the number of bytes in |data|.
99 // |timestamp| is the time to send the data, in seconds. A value of 0
100 // means send "now" or as soon as possible.
101 // The default implementation is for unsupported platforms.
102 virtual void DispatchSendMidiData(MidiManagerClient
* client
,
104 const std::vector
<uint8
>& data
,
108 friend class MidiManagerUsb
;
110 // Initializes the platform dependent MIDI system. MidiManager class has a
111 // default implementation that synchronously calls CompleteInitialization()
112 // with MIDI_NOT_SUPPORTED on the caller thread. A derived class for a
113 // specific platform should override this method correctly.
114 // This method is called on Chrome_IOThread thread inside StartSession().
115 // Platform dependent initialization can be processed synchronously or
116 // asynchronously. When the initialization is completed,
117 // CompleteInitialization() should be called with |result|.
118 // |result| should be MIDI_OK on success, otherwise a proper MidiResult.
119 virtual void StartInitialization();
121 // Called from a platform dependent implementation of StartInitialization().
122 // It invokes CompleteInitializationInternal() on the thread that calls
123 // StartSession() and distributes |result| to MIDIManagerClient objects in
124 // |pending_clients_|.
125 void CompleteInitialization(MidiResult result
);
127 void AddInputPort(const MidiPortInfo
& info
);
128 void AddOutputPort(const MidiPortInfo
& info
);
129 void SetInputPortState(uint32 port_index
, MidiPortState state
);
130 void SetOutputPortState(uint32 port_index
, MidiPortState state
);
132 // Dispatches to all clients.
133 // TODO(toyoshim): Fix the mac implementation to use
134 // |ReceiveMidiData(..., base::TimeTicks)|.
135 void ReceiveMidiData(uint32 port_index
,
140 void ReceiveMidiData(uint32 port_index
,
143 base::TimeTicks time
) {
144 ReceiveMidiData(port_index
, data
, length
,
145 (time
- base::TimeTicks()).InSecondsF());
148 size_t clients_size_for_testing() const { return clients_
.size(); }
149 size_t pending_clients_size_for_testing() const {
150 return pending_clients_
.size();
154 void CompleteInitializationInternal(MidiResult result
);
155 void AddInitialPorts(MidiManagerClient
* client
);
157 // Keeps track of all clients who wish to receive MIDI data.
158 typedef std::set
<MidiManagerClient
*> ClientSet
;
161 // Keeps track of all clients who are waiting for CompleteStartSession().
162 ClientSet pending_clients_
;
164 // Keeps a SingleThreadTaskRunner of the thread that calls StartSession in
165 // order to invoke CompleteStartSession() on the thread.
166 scoped_refptr
<base::SingleThreadTaskRunner
> session_thread_runner_
;
168 // Keeps true if platform dependent initialization is already completed.
171 // Keeps the platform dependent initialization result if initialization is
172 // completed. Otherwise keeps MIDI_NOT_SUPPORTED.
175 // Keeps all MidiPortInfo.
176 MidiPortInfoList input_ports_
;
177 MidiPortInfoList output_ports_
;
179 // Protects access to |clients_|, |pending_clients_|, |initialized_|,
180 // |result_|, |input_ports_| and |output_ports_|.
183 DISALLOW_COPY_AND_ASSIGN(MidiManager
);
188 #endif // MEDIA_MIDI_MIDI_MANAGER_H_