blob: 1e2bcb8dc793ae76ecb5d1787ffb541ec6f9b6f8 [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
11#include "base/basictypes.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"
crogers@google.com27356e42013-06-22 04:03:03 +000015#include "media/base/media_export.h"
16#include "media/midi/midi_port_info.h"
toyoshim@chromium.orgf77a1e62014-04-11 13:16:24 +000017#include "media/midi/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 {
24
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.
toyoshim@chromium.orgc82e66e2014-02-04 07:05:47 +000028class MEDIA_EXPORT MidiManagerClient {
crogers@google.com27356e42013-06-22 04:03:03 +000029 public:
toyoshim@chromium.orgf77a1e62014-04-11 13:16:24 +000030 virtual ~MidiManagerClient() {}
31
32 // CompleteStartSession() is called when platform dependent preparation is
33 // finished.
toyoshima485ff92014-10-23 00:17:59 -070034 virtual void CompleteStartSession(MidiResult result) = 0;
crogers@google.com27356e42013-06-22 04:03:03 +000035
toyoshim@chromium.orgc82e66e2014-02-04 07:05:47 +000036 // ReceiveMidiData() is called when MIDI data has been received from the
crogers@google.com27356e42013-06-22 04:03:03 +000037 // MIDI system.
38 // |port_index| represents the specific input port from input_ports().
39 // |data| represents a series of bytes encoding one or more MIDI messages.
40 // |length| is the number of bytes in |data|.
41 // |timestamp| is the time the data was received, in seconds.
toyoshim@chromium.orgc82e66e2014-02-04 07:05:47 +000042 virtual void ReceiveMidiData(uint32 port_index,
crogers@google.com27356e42013-06-22 04:03:03 +000043 const uint8* data,
44 size_t length,
45 double timestamp) = 0;
crogers@google.com542a43a2013-07-31 05:16:49 +000046
toyoshim@chromium.orgc82e66e2014-02-04 07:05:47 +000047 // AccumulateMidiBytesSent() is called to acknowledge when bytes have
crogers@google.com542a43a2013-07-31 05:16:49 +000048 // successfully been sent to the hardware.
49 // This happens as a result of the client having previously called
toyoshim@chromium.orgc82e66e2014-02-04 07:05:47 +000050 // MidiManager::DispatchSendMidiData().
51 virtual void AccumulateMidiBytesSent(size_t n) = 0;
crogers@google.com27356e42013-06-22 04:03:03 +000052};
53
54// Manages access to all MIDI hardware.
toyoshim@chromium.orgc82e66e2014-02-04 07:05:47 +000055class MEDIA_EXPORT MidiManager {
crogers@google.com27356e42013-06-22 04:03:03 +000056 public:
toyoshim@chromium.orgfc2002e2014-05-07 08:10:34 +000057 static const size_t kMaxPendingClientCount = 128;
crogers@google.com27356e42013-06-22 04:03:03 +000058
toyoshim@chromium.orgc82e66e2014-02-04 07:05:47 +000059 MidiManager();
60 virtual ~MidiManager();
crogers@google.com27356e42013-06-22 04:03:03 +000061
toyoshim@chromium.orgfc2002e2014-05-07 08:10:34 +000062 // The constructor and the destructor will be called on the CrBrowserMain
63 // thread.
64 static MidiManager* Create();
65
toyoshim@chromium.org235b09e2013-07-25 16:23:14 +000066 // A client calls StartSession() to receive and send MIDI data.
67 // If the session is ready to start, the MIDI system is lazily initialized
crogers@google.com27356e42013-06-22 04:03:03 +000068 // and the client is registered to receive MIDI data.
toyoshim@chromium.orgf77a1e62014-04-11 13:16:24 +000069 // CompleteStartSession() is called with MIDI_OK if the session is started.
70 // Otherwise CompleteStartSession() is called with proper MidiResult code.
toyoshim@chromium.orgc1e05fb2014-05-06 16:39:20 +000071 // StartSession() and EndSession() can be called on the Chrome_IOThread.
72 // CompleteStartSession() will be invoked on the same Chrome_IOThread.
toyoshima485ff92014-10-23 00:17:59 -070073 void StartSession(MidiManagerClient* client);
crogers@google.com27356e42013-06-22 04:03:03 +000074
toyoshim@chromium.orgf77a1e62014-04-11 13:16:24 +000075 // A client calls EndSession() to stop receiving MIDI data.
toyoshim@chromium.orgc82e66e2014-02-04 07:05:47 +000076 void EndSession(MidiManagerClient* client);
crogers@google.com27356e42013-06-22 04:03:03 +000077
toyoshim@chromium.orgc82e66e2014-02-04 07:05:47 +000078 // DispatchSendMidiData() is called when MIDI data should be sent to the MIDI
yukawa@chromium.orgdb7ad8b2013-12-04 15:42:41 +000079 // system.
80 // This method is supposed to return immediately and should not block.
crogers@google.com27356e42013-06-22 04:03:03 +000081 // |port_index| represents the specific output port from output_ports().
82 // |data| represents a series of bytes encoding one or more MIDI messages.
83 // |length| is the number of bytes in |data|.
crogers@google.com542a43a2013-07-31 05:16:49 +000084 // |timestamp| is the time to send the data, in seconds. A value of 0
85 // means send "now" or as soon as possible.
yhirano@chromium.orgc6d5b7b2013-12-20 07:27:23 +000086 // The default implementation is for unsupported platforms.
toyoshim@chromium.orgc82e66e2014-02-04 07:05:47 +000087 virtual void DispatchSendMidiData(MidiManagerClient* client,
yukawa@chromium.orgdb7ad8b2013-12-04 15:42:41 +000088 uint32 port_index,
89 const std::vector<uint8>& data,
yhirano@chromium.orgc6d5b7b2013-12-20 07:27:23 +000090 double timestamp);
crogers@google.com27356e42013-06-22 04:03:03 +000091
92 // input_ports() is a list of MIDI ports for receiving MIDI data.
93 // Each individual port in this list can be identified by its
94 // integer index into this list.
toyoshim@chromium.orga25e6832014-05-07 18:06:25 +000095 const MidiPortInfoList& input_ports() const { return input_ports_; }
crogers@google.com27356e42013-06-22 04:03:03 +000096
97 // output_ports() is a list of MIDI ports for sending MIDI data.
98 // Each individual port in this list can be identified by its
99 // integer index into this list.
toyoshim@chromium.orga25e6832014-05-07 18:06:25 +0000100 const MidiPortInfoList& output_ports() const { return output_ports_; }
crogers@google.com27356e42013-06-22 04:03:03 +0000101
102 protected:
toyoshim@chromium.org51c7f532014-05-01 17:17:32 +0000103 friend class MidiManagerUsb;
104
toyoshim@chromium.orgc1e05fb2014-05-06 16:39:20 +0000105 // Initializes the platform dependent MIDI system. MidiManager class has a
106 // default implementation that synchronously calls CompleteInitialization()
107 // with MIDI_NOT_SUPPORTED on the caller thread. A derived class for a
108 // specific platform should override this method correctly.
109 // This method is called on Chrome_IOThread thread inside StartSession().
110 // Platform dependent initialization can be processed synchronously or
111 // asynchronously. When the initialization is completed,
112 // CompleteInitialization() should be called with |result|.
113 // |result| should be MIDI_OK on success, otherwise a proper MidiResult.
toyoshim@chromium.org51c7f532014-05-01 17:17:32 +0000114 virtual void StartInitialization();
115
116 // Called from a platform dependent implementation of StartInitialization().
toyoshim@chromium.orgc1e05fb2014-05-06 16:39:20 +0000117 // It invokes CompleteInitializationInternal() on the thread that calls
118 // StartSession() and distributes |result| to MIDIManagerClient objects in
toyoshim@chromium.org51c7f532014-05-01 17:17:32 +0000119 // |pending_clients_|.
120 void CompleteInitialization(MidiResult result);
crogers@google.com27356e42013-06-22 04:03:03 +0000121
toyoshim@chromium.orgc82e66e2014-02-04 07:05:47 +0000122 void AddInputPort(const MidiPortInfo& info);
123 void AddOutputPort(const MidiPortInfo& info);
crogers@google.com27356e42013-06-22 04:03:03 +0000124
125 // Dispatches to all clients.
yhirano@chromium.orgcfa642c2014-05-01 08:54:41 +0000126 // TODO(toyoshim): Fix the mac implementation to use
127 // |ReceiveMidiData(..., base::TimeTicks)|.
toyoshim@chromium.orgc82e66e2014-02-04 07:05:47 +0000128 void ReceiveMidiData(uint32 port_index,
toyoshim@chromium.orgae6ad362013-08-27 15:30:20 +0000129 const uint8* data,
130 size_t length,
131 double timestamp);
132
yhirano@chromium.orgcfa642c2014-05-01 08:54:41 +0000133 void ReceiveMidiData(uint32 port_index,
134 const uint8* data,
135 size_t length,
136 base::TimeTicks time) {
137 ReceiveMidiData(port_index, data, length,
138 (time - base::TimeTicks()).InSecondsF());
139 }
140
toyoshim@chromium.orga25e6832014-05-07 18:06:25 +0000141 size_t clients_size_for_testing() const { return clients_.size(); }
142 size_t pending_clients_size_for_testing() const {
toyoshim@chromium.orgc1e05fb2014-05-06 16:39:20 +0000143 return pending_clients_.size();
144 }
145
toyoshim@chromium.orgc1e05fb2014-05-06 16:39:20 +0000146 private:
147 void CompleteInitializationInternal(MidiResult result);
crogers@google.com27356e42013-06-22 04:03:03 +0000148
149 // Keeps track of all clients who wish to receive MIDI data.
toyoshima485ff92014-10-23 00:17:59 -0700150 typedef std::set<MidiManagerClient*> ClientSet;
151 ClientSet clients_;
crogers@google.com27356e42013-06-22 04:03:03 +0000152
toyoshim@chromium.org51c7f532014-05-01 17:17:32 +0000153 // Keeps track of all clients who are waiting for CompleteStartSession().
toyoshima485ff92014-10-23 00:17:59 -0700154 ClientSet pending_clients_;
toyoshim@chromium.org51c7f532014-05-01 17:17:32 +0000155
toyoshim@chromium.orgc1e05fb2014-05-06 16:39:20 +0000156 // Keeps a SingleThreadTaskRunner of the thread that calls StartSession in
157 // order to invoke CompleteStartSession() on the thread.
158 scoped_refptr<base::SingleThreadTaskRunner> session_thread_runner_;
crogers@google.com27356e42013-06-22 04:03:03 +0000159
toyoshim@chromium.orgc1e05fb2014-05-06 16:39:20 +0000160 // Keeps true if platform dependent initialization is already completed.
161 bool initialized_;
crogers@google.com27356e42013-06-22 04:03:03 +0000162
toyoshim@chromium.orgc1e05fb2014-05-06 16:39:20 +0000163 // Keeps the platform dependent initialization result if initialization is
164 // completed. Otherwise keeps MIDI_NOT_SUPPORTED.
165 MidiResult result_;
166
167 // Protects access to |clients_|, |pending_clients_|, |initialized_|, and
168 // |result_|.
169 base::Lock lock_;
170
toyoshim@chromium.orga25e6832014-05-07 18:06:25 +0000171 MidiPortInfoList input_ports_;
172 MidiPortInfoList output_ports_;
173
toyoshim@chromium.orgc82e66e2014-02-04 07:05:47 +0000174 DISALLOW_COPY_AND_ASSIGN(MidiManager);
crogers@google.com27356e42013-06-22 04:03:03 +0000175};
176
177} // namespace media
178
179#endif // MEDIA_MIDI_MIDI_MANAGER_H_