blob: e5d17beddc55655e4b0a158bea76af7cbc485026 [file] [log] [blame]
crogers@google.com27356e42013-06-22 04:03:03 +00001// 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.
4
5#ifndef MEDIA_MIDI_MIDI_MANAGER_H_
6#define MEDIA_MIDI_MIDI_MANAGER_H_
7
8#include <set>
toyoshim@chromium.orgae6ad362013-08-27 15:30:20 +00009#include <vector>
crogers@google.com27356e42013-06-22 04:03:03 +000010
toyoshim@chromium.orgc1e05fb2014-05-06 16:39:20 +000011#include "base/memory/ref_counted.h"
crogers@google.com27356e42013-06-22 04:03:03 +000012#include "base/synchronization/lock.h"
yhirano@chromium.orgcfa642c2014-05-01 08:54:41 +000013#include "base/time/time.h"
brettw49ff0172015-05-05 12:43:04 -070014#include "media/midi/midi_export.h"
crogers@google.com27356e42013-06-22 04:03:03 +000015#include "media/midi/midi_port_info.h"
toyoshimf1b88962015-07-09 14:14:51 -070016#include "media/midi/result.h"
crogers@google.com27356e42013-06-22 04:03:03 +000017
toyoshim@chromium.orgc1e05fb2014-05-06 16:39:20 +000018namespace base {
19class SingleThreadTaskRunner;
20} // namespace base
21
crogers@google.com27356e42013-06-22 04:03:03 +000022namespace media {
toyoshime147c5e2015-05-07 21:58:31 -070023namespace midi {
crogers@google.com27356e42013-06-22 04:03:03 +000024
toyoshim@chromium.orgc82e66e2014-02-04 07:05:47 +000025// A MidiManagerClient registers with the MidiManager to receive MIDI data.
26// See MidiManager::RequestAccess() and MidiManager::ReleaseAccess()
crogers@google.com27356e42013-06-22 04:03:03 +000027// for details.
brettw49ff0172015-05-05 12:43:04 -070028class MIDI_EXPORT MidiManagerClient {
crogers@google.com27356e42013-06-22 04:03:03 +000029 public:
toyoshim@chromium.orgf77a1e62014-04-11 13:16:24 +000030 virtual ~MidiManagerClient() {}
31
toyoshima62d6742014-10-23 09:05:03 -070032 // 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;
37
toyoshim5c6fe4b2015-02-18 23:28:09 -080038 // SetInputPortState() and SetOutputPortState() are called to notify a known
39 // device gets disconnected, or connected again.
Avi Drissman3528fd02015-12-18 20:11:31 -050040 virtual void SetInputPortState(uint32_t port_index, MidiPortState state) = 0;
41 virtual void SetOutputPortState(uint32_t port_index, MidiPortState state) = 0;
toyoshima62d6742014-10-23 09:05:03 -070042
toyoshim@chromium.orgf77a1e62014-04-11 13:16:24 +000043 // CompleteStartSession() is called when platform dependent preparation is
44 // finished.
toyoshimf1b88962015-07-09 14:14:51 -070045 virtual void CompleteStartSession(Result result) = 0;
crogers@google.com27356e42013-06-22 04:03:03 +000046
toyoshim@chromium.orgc82e66e2014-02-04 07:05:47 +000047 // ReceiveMidiData() is called when MIDI data has been received from the
crogers@google.com27356e42013-06-22 04:03:03 +000048 // MIDI system.
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.
Avi Drissman3528fd02015-12-18 20:11:31 -050053 virtual void ReceiveMidiData(uint32_t port_index,
54 const uint8_t* data,
crogers@google.com27356e42013-06-22 04:03:03 +000055 size_t length,
56 double timestamp) = 0;
crogers@google.com542a43a2013-07-31 05:16:49 +000057
toyoshim@chromium.orgc82e66e2014-02-04 07:05:47 +000058 // AccumulateMidiBytesSent() is called to acknowledge when bytes have
crogers@google.com542a43a2013-07-31 05:16:49 +000059 // successfully been sent to the hardware.
60 // This happens as a result of the client having previously called
toyoshim@chromium.orgc82e66e2014-02-04 07:05:47 +000061 // MidiManager::DispatchSendMidiData().
62 virtual void AccumulateMidiBytesSent(size_t n) = 0;
toyoshim7a809662015-10-06 00:54:21 -070063
64 // Detach() is called when MidiManager is going to shutdown immediately.
65 // Client should not touch MidiManager instance after Detach() is called.
66 virtual void Detach() = 0;
crogers@google.com27356e42013-06-22 04:03:03 +000067};
68
69// Manages access to all MIDI hardware.
brettw49ff0172015-05-05 12:43:04 -070070class MIDI_EXPORT MidiManager {
crogers@google.com27356e42013-06-22 04:03:03 +000071 public:
toyoshim@chromium.orgfc2002e2014-05-07 08:10:34 +000072 static const size_t kMaxPendingClientCount = 128;
crogers@google.com27356e42013-06-22 04:03:03 +000073
toyoshim@chromium.orgc82e66e2014-02-04 07:05:47 +000074 MidiManager();
75 virtual ~MidiManager();
crogers@google.com27356e42013-06-22 04:03:03 +000076
toyoshim@chromium.orgfc2002e2014-05-07 08:10:34 +000077 // The constructor and the destructor will be called on the CrBrowserMain
78 // thread.
79 static MidiManager* Create();
80
toyoshim7a809662015-10-06 00:54:21 -070081 // Called on the CrBrowserMain thread to notify the Chrome_IOThread will stop
82 // and the instance will be destructed on the CrBrowserMain thread soon.
83 void Shutdown();
84
toyoshim@chromium.org235b09e2013-07-25 16:23:14 +000085 // A client calls StartSession() to receive and send MIDI data.
86 // If the session is ready to start, the MIDI system is lazily initialized
crogers@google.com27356e42013-06-22 04:03:03 +000087 // and the client is registered to receive MIDI data.
toyoshimf1b88962015-07-09 14:14:51 -070088 // CompleteStartSession() is called with Result::OK if the session is started.
89 // Otherwise CompleteStartSession() is called with proper Result code.
toyoshim@chromium.orgc1e05fb2014-05-06 16:39:20 +000090 // StartSession() and EndSession() can be called on the Chrome_IOThread.
91 // CompleteStartSession() will be invoked on the same Chrome_IOThread.
toyoshima485ff92014-10-23 00:17:59 -070092 void StartSession(MidiManagerClient* client);
crogers@google.com27356e42013-06-22 04:03:03 +000093
toyoshim@chromium.orgf77a1e62014-04-11 13:16:24 +000094 // A client calls EndSession() to stop receiving MIDI data.
toyoshim@chromium.orgc82e66e2014-02-04 07:05:47 +000095 void EndSession(MidiManagerClient* client);
crogers@google.com27356e42013-06-22 04:03:03 +000096
toyoshim7dd12482015-04-07 07:02:49 -070097 // Invoke AccumulateMidiBytesSent() for |client| safely. If the session was
98 // already closed, do nothing.
99 void AccumulateMidiBytesSent(MidiManagerClient* client, size_t n);
100
toyoshim@chromium.orgc82e66e2014-02-04 07:05:47 +0000101 // DispatchSendMidiData() is called when MIDI data should be sent to the MIDI
yukawa@chromium.orgdb7ad8b2013-12-04 15:42:41 +0000102 // system.
103 // This method is supposed to return immediately and should not block.
crogers@google.com27356e42013-06-22 04:03:03 +0000104 // |port_index| represents the specific output port from output_ports().
105 // |data| represents a series of bytes encoding one or more MIDI messages.
106 // |length| is the number of bytes in |data|.
crogers@google.com542a43a2013-07-31 05:16:49 +0000107 // |timestamp| is the time to send the data, in seconds. A value of 0
108 // means send "now" or as soon as possible.
yhirano@chromium.orgc6d5b7b2013-12-20 07:27:23 +0000109 // The default implementation is for unsupported platforms.
toyoshim@chromium.orgc82e66e2014-02-04 07:05:47 +0000110 virtual void DispatchSendMidiData(MidiManagerClient* client,
Avi Drissman3528fd02015-12-18 20:11:31 -0500111 uint32_t port_index,
112 const std::vector<uint8_t>& data,
yhirano@chromium.orgc6d5b7b2013-12-20 07:27:23 +0000113 double timestamp);
crogers@google.com27356e42013-06-22 04:03:03 +0000114
crogers@google.com27356e42013-06-22 04:03:03 +0000115 protected:
toyoshim@chromium.org51c7f532014-05-01 17:17:32 +0000116 friend class MidiManagerUsb;
117
toyoshim@chromium.orgc1e05fb2014-05-06 16:39:20 +0000118 // Initializes the platform dependent MIDI system. MidiManager class has a
119 // default implementation that synchronously calls CompleteInitialization()
toyoshimf1b88962015-07-09 14:14:51 -0700120 // with Result::NOT_SUPPORTED on the caller thread. A derived class for a
toyoshim@chromium.orgc1e05fb2014-05-06 16:39:20 +0000121 // specific platform should override this method correctly.
122 // This method is called on Chrome_IOThread thread inside StartSession().
123 // Platform dependent initialization can be processed synchronously or
124 // asynchronously. When the initialization is completed,
125 // CompleteInitialization() should be called with |result|.
toyoshimf1b88962015-07-09 14:14:51 -0700126 // |result| should be Result::OK on success, otherwise a proper Result.
toyoshim@chromium.org51c7f532014-05-01 17:17:32 +0000127 virtual void StartInitialization();
128
toyoshim7a809662015-10-06 00:54:21 -0700129 // Finalizes the platform dependent MIDI system. Called on Chrome_IOThread
130 // thread and the thread will stop immediately after this call.
131 // Platform dependent resources that were allocated on the Chrome_IOThread
132 // should be disposed inside this method.
133 virtual void Finalize() {}
134
toyoshim@chromium.org51c7f532014-05-01 17:17:32 +0000135 // Called from a platform dependent implementation of StartInitialization().
toyoshim@chromium.orgc1e05fb2014-05-06 16:39:20 +0000136 // It invokes CompleteInitializationInternal() on the thread that calls
137 // StartSession() and distributes |result| to MIDIManagerClient objects in
toyoshim@chromium.org51c7f532014-05-01 17:17:32 +0000138 // |pending_clients_|.
toyoshimf1b88962015-07-09 14:14:51 -0700139 void CompleteInitialization(Result result);
crogers@google.com27356e42013-06-22 04:03:03 +0000140
toyoshim@chromium.orgc82e66e2014-02-04 07:05:47 +0000141 void AddInputPort(const MidiPortInfo& info);
142 void AddOutputPort(const MidiPortInfo& info);
Avi Drissman3528fd02015-12-18 20:11:31 -0500143 void SetInputPortState(uint32_t port_index, MidiPortState state);
144 void SetOutputPortState(uint32_t port_index, MidiPortState state);
crogers@google.com27356e42013-06-22 04:03:03 +0000145
146 // Dispatches to all clients.
yhirano@chromium.orgcfa642c2014-05-01 08:54:41 +0000147 // TODO(toyoshim): Fix the mac implementation to use
148 // |ReceiveMidiData(..., base::TimeTicks)|.
Avi Drissman3528fd02015-12-18 20:11:31 -0500149 void ReceiveMidiData(uint32_t port_index,
150 const uint8_t* data,
toyoshim@chromium.orgae6ad362013-08-27 15:30:20 +0000151 size_t length,
152 double timestamp);
153
Avi Drissman3528fd02015-12-18 20:11:31 -0500154 void ReceiveMidiData(uint32_t port_index,
155 const uint8_t* data,
yhirano@chromium.orgcfa642c2014-05-01 08:54:41 +0000156 size_t length,
157 base::TimeTicks time) {
158 ReceiveMidiData(port_index, data, length,
159 (time - base::TimeTicks()).InSecondsF());
160 }
161
toyoshim@chromium.orga25e6832014-05-07 18:06:25 +0000162 size_t clients_size_for_testing() const { return clients_.size(); }
163 size_t pending_clients_size_for_testing() const {
toyoshim@chromium.orgc1e05fb2014-05-06 16:39:20 +0000164 return pending_clients_.size();
165 }
166
yhiranobc742d82015-09-17 07:41:44 -0700167 const MidiPortInfoList& input_ports() const { return input_ports_; }
168 const MidiPortInfoList& output_ports() const { return output_ports_; }
169
toyoshim@chromium.orgc1e05fb2014-05-06 16:39:20 +0000170 private:
toyoshimf1b88962015-07-09 14:14:51 -0700171 void CompleteInitializationInternal(Result result);
toyoshima62d6742014-10-23 09:05:03 -0700172 void AddInitialPorts(MidiManagerClient* client);
toyoshim7a809662015-10-06 00:54:21 -0700173 void ShutdownOnSessionThread();
crogers@google.com27356e42013-06-22 04:03:03 +0000174
175 // Keeps track of all clients who wish to receive MIDI data.
toyoshima485ff92014-10-23 00:17:59 -0700176 typedef std::set<MidiManagerClient*> ClientSet;
177 ClientSet clients_;
crogers@google.com27356e42013-06-22 04:03:03 +0000178
toyoshim@chromium.org51c7f532014-05-01 17:17:32 +0000179 // Keeps track of all clients who are waiting for CompleteStartSession().
toyoshima485ff92014-10-23 00:17:59 -0700180 ClientSet pending_clients_;
toyoshim@chromium.org51c7f532014-05-01 17:17:32 +0000181
toyoshim@chromium.orgc1e05fb2014-05-06 16:39:20 +0000182 // Keeps a SingleThreadTaskRunner of the thread that calls StartSession in
183 // order to invoke CompleteStartSession() on the thread.
184 scoped_refptr<base::SingleThreadTaskRunner> session_thread_runner_;
crogers@google.com27356e42013-06-22 04:03:03 +0000185
toyoshim@chromium.orgc1e05fb2014-05-06 16:39:20 +0000186 // Keeps true if platform dependent initialization is already completed.
187 bool initialized_;
crogers@google.com27356e42013-06-22 04:03:03 +0000188
toyoshim7a809662015-10-06 00:54:21 -0700189 // Keeps false until Finalize() is called.
190 bool finalized_;
191
toyoshim@chromium.orgc1e05fb2014-05-06 16:39:20 +0000192 // Keeps the platform dependent initialization result if initialization is
toyoshimf1b88962015-07-09 14:14:51 -0700193 // completed. Otherwise keeps Result::NOT_INITIALIZED.
194 Result result_;
toyoshim@chromium.orgc1e05fb2014-05-06 16:39:20 +0000195
toyoshima62d6742014-10-23 09:05:03 -0700196 // Keeps all MidiPortInfo.
toyoshim@chromium.orga25e6832014-05-07 18:06:25 +0000197 MidiPortInfoList input_ports_;
198 MidiPortInfoList output_ports_;
199
toyoshim7a809662015-10-06 00:54:21 -0700200 // Protects access to |clients_|, |pending_clients_|,
201 // |session_thread_runner_|, |initialized_|, |finalize_|, |result_|,
202 // |input_ports_| and |output_ports_|.
toyoshima62d6742014-10-23 09:05:03 -0700203 base::Lock lock_;
204
toyoshim@chromium.orgc82e66e2014-02-04 07:05:47 +0000205 DISALLOW_COPY_AND_ASSIGN(MidiManager);
crogers@google.com27356e42013-06-22 04:03:03 +0000206};
207
toyoshime147c5e2015-05-07 21:58:31 -0700208} // namespace midi
crogers@google.com27356e42013-06-22 04:03:03 +0000209} // namespace media
210
211#endif // MEDIA_MIDI_MIDI_MANAGER_H_