blob: 14260575b4e098774194b24738c34fbc9bcfb152 [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
avieea95e82015-12-18 20:27:08 -080011#include "base/macros.h"
toyoshim@chromium.orgc1e05fb2014-05-06 16:39:20 +000012#include "base/memory/ref_counted.h"
crogers@google.com27356e42013-06-22 04:03:03 +000013#include "base/synchronization/lock.h"
yhirano@chromium.orgcfa642c2014-05-01 08:54:41 +000014#include "base/time/time.h"
brettw49ff0172015-05-05 12:43:04 -070015#include "media/midi/midi_export.h"
crogers@google.com27356e42013-06-22 04:03:03 +000016#include "media/midi/midi_port_info.h"
toyoshimf1b88962015-07-09 14:14:51 -070017#include "media/midi/result.h"
crogers@google.com27356e42013-06-22 04:03:03 +000018
toyoshim@chromium.orgc1e05fb2014-05-06 16:39:20 +000019namespace base {
20class SingleThreadTaskRunner;
21} // namespace base
22
crogers@google.com27356e42013-06-22 04:03:03 +000023namespace media {
toyoshime147c5e2015-05-07 21:58:31 -070024namespace midi {
crogers@google.com27356e42013-06-22 04:03:03 +000025
toyoshim@chromium.orgc82e66e2014-02-04 07:05:47 +000026// A MidiManagerClient registers with the MidiManager to receive MIDI data.
27// See MidiManager::RequestAccess() and MidiManager::ReleaseAccess()
crogers@google.com27356e42013-06-22 04:03:03 +000028// for details.
brettw49ff0172015-05-05 12:43:04 -070029class MIDI_EXPORT MidiManagerClient {
crogers@google.com27356e42013-06-22 04:03:03 +000030 public:
toyoshim@chromium.orgf77a1e62014-04-11 13:16:24 +000031 virtual ~MidiManagerClient() {}
32
toyoshima62d6742014-10-23 09:05:03 -070033 // 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;
38
toyoshim5c6fe4b2015-02-18 23:28:09 -080039 // SetInputPortState() and SetOutputPortState() are called to notify a known
40 // device gets disconnected, or connected again.
Avi Drissman3528fd02015-12-18 20:11:31 -050041 virtual void SetInputPortState(uint32_t port_index, MidiPortState state) = 0;
42 virtual void SetOutputPortState(uint32_t port_index, MidiPortState state) = 0;
toyoshima62d6742014-10-23 09:05:03 -070043
toyoshim@chromium.orgf77a1e62014-04-11 13:16:24 +000044 // CompleteStartSession() is called when platform dependent preparation is
45 // finished.
toyoshimf1b88962015-07-09 14:14:51 -070046 virtual void CompleteStartSession(Result result) = 0;
crogers@google.com27356e42013-06-22 04:03:03 +000047
toyoshim@chromium.orgc82e66e2014-02-04 07:05:47 +000048 // ReceiveMidiData() is called when MIDI data has been received from the
crogers@google.com27356e42013-06-22 04:03:03 +000049 // 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.
Avi Drissman3528fd02015-12-18 20:11:31 -050054 virtual void ReceiveMidiData(uint32_t port_index,
55 const uint8_t* data,
crogers@google.com27356e42013-06-22 04:03:03 +000056 size_t length,
57 double timestamp) = 0;
crogers@google.com542a43a2013-07-31 05:16:49 +000058
toyoshim@chromium.orgc82e66e2014-02-04 07:05:47 +000059 // AccumulateMidiBytesSent() is called to acknowledge when bytes have
crogers@google.com542a43a2013-07-31 05:16:49 +000060 // successfully been sent to the hardware.
61 // This happens as a result of the client having previously called
toyoshim@chromium.orgc82e66e2014-02-04 07:05:47 +000062 // MidiManager::DispatchSendMidiData().
63 virtual void AccumulateMidiBytesSent(size_t n) = 0;
toyoshim7a809662015-10-06 00:54:21 -070064
65 // Detach() is called when MidiManager is going to shutdown immediately.
66 // Client should not touch MidiManager instance after Detach() is called.
67 virtual void Detach() = 0;
crogers@google.com27356e42013-06-22 04:03:03 +000068};
69
70// Manages access to all MIDI hardware.
brettw49ff0172015-05-05 12:43:04 -070071class MIDI_EXPORT MidiManager {
crogers@google.com27356e42013-06-22 04:03:03 +000072 public:
toyoshim@chromium.orgfc2002e2014-05-07 08:10:34 +000073 static const size_t kMaxPendingClientCount = 128;
crogers@google.com27356e42013-06-22 04:03:03 +000074
toyoshim@chromium.orgc82e66e2014-02-04 07:05:47 +000075 MidiManager();
76 virtual ~MidiManager();
crogers@google.com27356e42013-06-22 04:03:03 +000077
toyoshim@chromium.orgfc2002e2014-05-07 08:10:34 +000078 // The constructor and the destructor will be called on the CrBrowserMain
79 // thread.
80 static MidiManager* Create();
81
toyoshim7a809662015-10-06 00:54:21 -070082 // Called on the CrBrowserMain thread to notify the Chrome_IOThread will stop
83 // and the instance will be destructed on the CrBrowserMain thread soon.
84 void Shutdown();
85
toyoshim@chromium.org235b09e2013-07-25 16:23:14 +000086 // A client calls StartSession() to receive and send MIDI data.
87 // If the session is ready to start, the MIDI system is lazily initialized
crogers@google.com27356e42013-06-22 04:03:03 +000088 // and the client is registered to receive MIDI data.
toyoshimf1b88962015-07-09 14:14:51 -070089 // CompleteStartSession() is called with Result::OK if the session is started.
90 // Otherwise CompleteStartSession() is called with proper Result code.
toyoshim@chromium.orgc1e05fb2014-05-06 16:39:20 +000091 // StartSession() and EndSession() can be called on the Chrome_IOThread.
92 // CompleteStartSession() will be invoked on the same Chrome_IOThread.
toyoshima485ff92014-10-23 00:17:59 -070093 void StartSession(MidiManagerClient* client);
crogers@google.com27356e42013-06-22 04:03:03 +000094
toyoshim@chromium.orgf77a1e62014-04-11 13:16:24 +000095 // A client calls EndSession() to stop receiving MIDI data.
toyoshim@chromium.orgc82e66e2014-02-04 07:05:47 +000096 void EndSession(MidiManagerClient* client);
crogers@google.com27356e42013-06-22 04:03:03 +000097
toyoshim7dd12482015-04-07 07:02:49 -070098 // Invoke AccumulateMidiBytesSent() for |client| safely. If the session was
99 // already closed, do nothing.
100 void AccumulateMidiBytesSent(MidiManagerClient* client, size_t n);
101
toyoshim@chromium.orgc82e66e2014-02-04 07:05:47 +0000102 // DispatchSendMidiData() is called when MIDI data should be sent to the MIDI
yukawa@chromium.orgdb7ad8b2013-12-04 15:42:41 +0000103 // system.
104 // This method is supposed to return immediately and should not block.
crogers@google.com27356e42013-06-22 04:03:03 +0000105 // |port_index| represents the specific output port from output_ports().
106 // |data| represents a series of bytes encoding one or more MIDI messages.
107 // |length| is the number of bytes in |data|.
crogers@google.com542a43a2013-07-31 05:16:49 +0000108 // |timestamp| is the time to send the data, in seconds. A value of 0
109 // means send "now" or as soon as possible.
yhirano@chromium.orgc6d5b7b2013-12-20 07:27:23 +0000110 // The default implementation is for unsupported platforms.
toyoshim@chromium.orgc82e66e2014-02-04 07:05:47 +0000111 virtual void DispatchSendMidiData(MidiManagerClient* client,
Avi Drissman3528fd02015-12-18 20:11:31 -0500112 uint32_t port_index,
113 const std::vector<uint8_t>& data,
yhirano@chromium.orgc6d5b7b2013-12-20 07:27:23 +0000114 double timestamp);
crogers@google.com27356e42013-06-22 04:03:03 +0000115
crogers@google.com27356e42013-06-22 04:03:03 +0000116 protected:
toyoshim@chromium.org51c7f532014-05-01 17:17:32 +0000117 friend class MidiManagerUsb;
118
toyoshim@chromium.orgc1e05fb2014-05-06 16:39:20 +0000119 // Initializes the platform dependent MIDI system. MidiManager class has a
120 // default implementation that synchronously calls CompleteInitialization()
toyoshimf1b88962015-07-09 14:14:51 -0700121 // with Result::NOT_SUPPORTED on the caller thread. A derived class for a
toyoshim@chromium.orgc1e05fb2014-05-06 16:39:20 +0000122 // specific platform should override this method correctly.
123 // This method is called on Chrome_IOThread thread inside StartSession().
124 // Platform dependent initialization can be processed synchronously or
125 // asynchronously. When the initialization is completed,
126 // CompleteInitialization() should be called with |result|.
toyoshimf1b88962015-07-09 14:14:51 -0700127 // |result| should be Result::OK on success, otherwise a proper Result.
toyoshim@chromium.org51c7f532014-05-01 17:17:32 +0000128 virtual void StartInitialization();
129
toyoshim7a809662015-10-06 00:54:21 -0700130 // Finalizes the platform dependent MIDI system. Called on Chrome_IOThread
131 // thread and the thread will stop immediately after this call.
132 // Platform dependent resources that were allocated on the Chrome_IOThread
133 // should be disposed inside this method.
134 virtual void Finalize() {}
135
toyoshim@chromium.org51c7f532014-05-01 17:17:32 +0000136 // Called from a platform dependent implementation of StartInitialization().
toyoshim@chromium.orgc1e05fb2014-05-06 16:39:20 +0000137 // It invokes CompleteInitializationInternal() on the thread that calls
138 // StartSession() and distributes |result| to MIDIManagerClient objects in
toyoshim@chromium.org51c7f532014-05-01 17:17:32 +0000139 // |pending_clients_|.
toyoshimf1b88962015-07-09 14:14:51 -0700140 void CompleteInitialization(Result result);
crogers@google.com27356e42013-06-22 04:03:03 +0000141
toyoshim@chromium.orgc82e66e2014-02-04 07:05:47 +0000142 void AddInputPort(const MidiPortInfo& info);
143 void AddOutputPort(const MidiPortInfo& info);
Avi Drissman3528fd02015-12-18 20:11:31 -0500144 void SetInputPortState(uint32_t port_index, MidiPortState state);
145 void SetOutputPortState(uint32_t port_index, MidiPortState state);
crogers@google.com27356e42013-06-22 04:03:03 +0000146
147 // Dispatches to all clients.
yhirano@chromium.orgcfa642c2014-05-01 08:54:41 +0000148 // TODO(toyoshim): Fix the mac implementation to use
149 // |ReceiveMidiData(..., base::TimeTicks)|.
Avi Drissman3528fd02015-12-18 20:11:31 -0500150 void ReceiveMidiData(uint32_t port_index,
151 const uint8_t* data,
toyoshim@chromium.orgae6ad362013-08-27 15:30:20 +0000152 size_t length,
153 double timestamp);
154
Avi Drissman3528fd02015-12-18 20:11:31 -0500155 void ReceiveMidiData(uint32_t port_index,
156 const uint8_t* data,
yhirano@chromium.orgcfa642c2014-05-01 08:54:41 +0000157 size_t length,
158 base::TimeTicks time) {
159 ReceiveMidiData(port_index, data, length,
160 (time - base::TimeTicks()).InSecondsF());
161 }
162
toyoshim@chromium.orga25e6832014-05-07 18:06:25 +0000163 size_t clients_size_for_testing() const { return clients_.size(); }
164 size_t pending_clients_size_for_testing() const {
toyoshim@chromium.orgc1e05fb2014-05-06 16:39:20 +0000165 return pending_clients_.size();
166 }
167
yhiranobc742d82015-09-17 07:41:44 -0700168 const MidiPortInfoList& input_ports() const { return input_ports_; }
169 const MidiPortInfoList& output_ports() const { return output_ports_; }
170
toyoshim@chromium.orgc1e05fb2014-05-06 16:39:20 +0000171 private:
toyoshimf1b88962015-07-09 14:14:51 -0700172 void CompleteInitializationInternal(Result result);
toyoshima62d6742014-10-23 09:05:03 -0700173 void AddInitialPorts(MidiManagerClient* client);
toyoshim7a809662015-10-06 00:54:21 -0700174 void ShutdownOnSessionThread();
crogers@google.com27356e42013-06-22 04:03:03 +0000175
176 // Keeps track of all clients who wish to receive MIDI data.
toyoshima485ff92014-10-23 00:17:59 -0700177 typedef std::set<MidiManagerClient*> ClientSet;
178 ClientSet clients_;
crogers@google.com27356e42013-06-22 04:03:03 +0000179
toyoshim@chromium.org51c7f532014-05-01 17:17:32 +0000180 // Keeps track of all clients who are waiting for CompleteStartSession().
toyoshima485ff92014-10-23 00:17:59 -0700181 ClientSet pending_clients_;
toyoshim@chromium.org51c7f532014-05-01 17:17:32 +0000182
toyoshim@chromium.orgc1e05fb2014-05-06 16:39:20 +0000183 // Keeps a SingleThreadTaskRunner of the thread that calls StartSession in
184 // order to invoke CompleteStartSession() on the thread.
185 scoped_refptr<base::SingleThreadTaskRunner> session_thread_runner_;
crogers@google.com27356e42013-06-22 04:03:03 +0000186
toyoshim@chromium.orgc1e05fb2014-05-06 16:39:20 +0000187 // Keeps true if platform dependent initialization is already completed.
188 bool initialized_;
crogers@google.com27356e42013-06-22 04:03:03 +0000189
toyoshim7a809662015-10-06 00:54:21 -0700190 // Keeps false until Finalize() is called.
191 bool finalized_;
192
toyoshim@chromium.orgc1e05fb2014-05-06 16:39:20 +0000193 // Keeps the platform dependent initialization result if initialization is
toyoshimf1b88962015-07-09 14:14:51 -0700194 // completed. Otherwise keeps Result::NOT_INITIALIZED.
195 Result result_;
toyoshim@chromium.orgc1e05fb2014-05-06 16:39:20 +0000196
toyoshima62d6742014-10-23 09:05:03 -0700197 // Keeps all MidiPortInfo.
toyoshim@chromium.orga25e6832014-05-07 18:06:25 +0000198 MidiPortInfoList input_ports_;
199 MidiPortInfoList output_ports_;
200
toyoshim7a809662015-10-06 00:54:21 -0700201 // Protects access to |clients_|, |pending_clients_|,
202 // |session_thread_runner_|, |initialized_|, |finalize_|, |result_|,
203 // |input_ports_| and |output_ports_|.
toyoshima62d6742014-10-23 09:05:03 -0700204 base::Lock lock_;
205
toyoshim@chromium.orgc82e66e2014-02-04 07:05:47 +0000206 DISALLOW_COPY_AND_ASSIGN(MidiManager);
crogers@google.com27356e42013-06-22 04:03:03 +0000207};
208
toyoshime147c5e2015-05-07 21:58:31 -0700209} // namespace midi
crogers@google.com27356e42013-06-22 04:03:03 +0000210} // namespace media
211
212#endif // MEDIA_MIDI_MIDI_MANAGER_H_