blob: 46602c1df16bef9d293e99808bf81ec7747bb106 [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
toyoshim@chromium.org51c7f532014-05-01 17:17:32 +00008#include <map>
crogers@google.com27356e42013-06-22 04:03:03 +00009#include <set>
toyoshim@chromium.orgae6ad362013-08-27 15:30:20 +000010#include <vector>
crogers@google.com27356e42013-06-22 04:03:03 +000011
12#include "base/basictypes.h"
toyoshim@chromium.orgc1e05fb2014-05-06 16:39:20 +000013#include "base/memory/ref_counted.h"
crogers@google.com27356e42013-06-22 04:03:03 +000014#include "base/synchronization/lock.h"
yhirano@chromium.orgcfa642c2014-05-01 08:54:41 +000015#include "base/time/time.h"
crogers@google.com27356e42013-06-22 04:03:03 +000016#include "media/base/media_export.h"
17#include "media/midi/midi_port_info.h"
toyoshim@chromium.orgf77a1e62014-04-11 13:16:24 +000018#include "media/midi/midi_result.h"
crogers@google.com27356e42013-06-22 04:03:03 +000019
toyoshim@chromium.orgc1e05fb2014-05-06 16:39:20 +000020namespace base {
21class SingleThreadTaskRunner;
22} // namespace base
23
crogers@google.com27356e42013-06-22 04:03:03 +000024namespace media {
25
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.
toyoshim@chromium.orgc82e66e2014-02-04 07:05:47 +000029class MEDIA_EXPORT MidiManagerClient {
crogers@google.com27356e42013-06-22 04:03:03 +000030 public:
toyoshim@chromium.orgf77a1e62014-04-11 13:16:24 +000031 virtual ~MidiManagerClient() {}
32
33 // CompleteStartSession() is called when platform dependent preparation is
34 // finished.
35 virtual void CompleteStartSession(int client_id, MidiResult result) = 0;
crogers@google.com27356e42013-06-22 04:03:03 +000036
toyoshim@chromium.orgc82e66e2014-02-04 07:05:47 +000037 // ReceiveMidiData() is called when MIDI data has been received from the
crogers@google.com27356e42013-06-22 04:03:03 +000038 // MIDI system.
39 // |port_index| represents the specific input port from input_ports().
40 // |data| represents a series of bytes encoding one or more MIDI messages.
41 // |length| is the number of bytes in |data|.
42 // |timestamp| is the time the data was received, in seconds.
toyoshim@chromium.orgc82e66e2014-02-04 07:05:47 +000043 virtual void ReceiveMidiData(uint32 port_index,
crogers@google.com27356e42013-06-22 04:03:03 +000044 const uint8* data,
45 size_t length,
46 double timestamp) = 0;
crogers@google.com542a43a2013-07-31 05:16:49 +000047
toyoshim@chromium.orgc82e66e2014-02-04 07:05:47 +000048 // AccumulateMidiBytesSent() is called to acknowledge when bytes have
crogers@google.com542a43a2013-07-31 05:16:49 +000049 // successfully been sent to the hardware.
50 // This happens as a result of the client having previously called
toyoshim@chromium.orgc82e66e2014-02-04 07:05:47 +000051 // MidiManager::DispatchSendMidiData().
52 virtual void AccumulateMidiBytesSent(size_t n) = 0;
crogers@google.com27356e42013-06-22 04:03:03 +000053};
54
55// Manages access to all MIDI hardware.
toyoshim@chromium.orgc82e66e2014-02-04 07:05:47 +000056class MEDIA_EXPORT MidiManager {
crogers@google.com27356e42013-06-22 04:03:03 +000057 public:
toyoshim@chromium.orgc1e05fb2014-05-06 16:39:20 +000058 // The constructor and the destructor will be called on the CrBrowserMain
59 // thread.
toyoshim@chromium.orgc82e66e2014-02-04 07:05:47 +000060 static MidiManager* Create();
crogers@google.com27356e42013-06-22 04:03:03 +000061
toyoshim@chromium.orgc82e66e2014-02-04 07:05:47 +000062 MidiManager();
63 virtual ~MidiManager();
crogers@google.com27356e42013-06-22 04:03:03 +000064
toyoshim@chromium.org235b09e2013-07-25 16:23:14 +000065 // A client calls StartSession() to receive and send MIDI data.
66 // If the session is ready to start, the MIDI system is lazily initialized
crogers@google.com27356e42013-06-22 04:03:03 +000067 // and the client is registered to receive MIDI data.
toyoshim@chromium.orgf77a1e62014-04-11 13:16:24 +000068 // CompleteStartSession() is called with MIDI_OK if the session is started.
69 // Otherwise CompleteStartSession() is called with proper MidiResult code.
toyoshim@chromium.orgc1e05fb2014-05-06 16:39:20 +000070 // StartSession() and EndSession() can be called on the Chrome_IOThread.
71 // CompleteStartSession() will be invoked on the same Chrome_IOThread.
toyoshim@chromium.orgf77a1e62014-04-11 13:16:24 +000072 void StartSession(MidiManagerClient* client, int client_id);
crogers@google.com27356e42013-06-22 04:03:03 +000073
toyoshim@chromium.orgf77a1e62014-04-11 13:16:24 +000074 // A client calls EndSession() to stop receiving MIDI data.
toyoshim@chromium.orgc82e66e2014-02-04 07:05:47 +000075 void EndSession(MidiManagerClient* client);
crogers@google.com27356e42013-06-22 04:03:03 +000076
toyoshim@chromium.orgc82e66e2014-02-04 07:05:47 +000077 // DispatchSendMidiData() is called when MIDI data should be sent to the MIDI
yukawa@chromium.orgdb7ad8b2013-12-04 15:42:41 +000078 // system.
79 // This method is supposed to return immediately and should not block.
crogers@google.com27356e42013-06-22 04:03:03 +000080 // |port_index| represents the specific output port from output_ports().
81 // |data| represents a series of bytes encoding one or more MIDI messages.
82 // |length| is the number of bytes in |data|.
crogers@google.com542a43a2013-07-31 05:16:49 +000083 // |timestamp| is the time to send the data, in seconds. A value of 0
84 // means send "now" or as soon as possible.
yhirano@chromium.orgc6d5b7b2013-12-20 07:27:23 +000085 // The default implementation is for unsupported platforms.
toyoshim@chromium.orgc82e66e2014-02-04 07:05:47 +000086 virtual void DispatchSendMidiData(MidiManagerClient* client,
yukawa@chromium.orgdb7ad8b2013-12-04 15:42:41 +000087 uint32 port_index,
88 const std::vector<uint8>& data,
yhirano@chromium.orgc6d5b7b2013-12-20 07:27:23 +000089 double timestamp);
crogers@google.com27356e42013-06-22 04:03:03 +000090
91 // input_ports() is a list of MIDI ports for receiving MIDI data.
92 // Each individual port in this list can be identified by its
93 // integer index into this list.
toyoshim@chromium.orgc82e66e2014-02-04 07:05:47 +000094 const MidiPortInfoList& input_ports() { return input_ports_; }
crogers@google.com27356e42013-06-22 04:03:03 +000095
96 // output_ports() is a list of MIDI ports for sending MIDI data.
97 // Each individual port in this list can be identified by its
98 // integer index into this list.
toyoshim@chromium.orgc82e66e2014-02-04 07:05:47 +000099 const MidiPortInfoList& output_ports() { return output_ports_; }
crogers@google.com27356e42013-06-22 04:03:03 +0000100
101 protected:
toyoshim@chromium.org51c7f532014-05-01 17:17:32 +0000102 friend class MidiManagerUsb;
103
toyoshim@chromium.orgc1e05fb2014-05-06 16:39:20 +0000104 // Initializes the platform dependent MIDI system. MidiManager class has a
105 // default implementation that synchronously calls CompleteInitialization()
106 // with MIDI_NOT_SUPPORTED on the caller thread. A derived class for a
107 // specific platform should override this method correctly.
108 // This method is called on Chrome_IOThread thread inside StartSession().
109 // Platform dependent initialization can be processed synchronously or
110 // asynchronously. When the initialization is completed,
111 // CompleteInitialization() should be called with |result|.
112 // |result| should be MIDI_OK on success, otherwise a proper MidiResult.
toyoshim@chromium.org51c7f532014-05-01 17:17:32 +0000113 virtual void StartInitialization();
114
115 // Called from a platform dependent implementation of StartInitialization().
toyoshim@chromium.orgc1e05fb2014-05-06 16:39:20 +0000116 // It invokes CompleteInitializationInternal() on the thread that calls
117 // StartSession() and distributes |result| to MIDIManagerClient objects in
toyoshim@chromium.org51c7f532014-05-01 17:17:32 +0000118 // |pending_clients_|.
119 void CompleteInitialization(MidiResult result);
crogers@google.com27356e42013-06-22 04:03:03 +0000120
toyoshim@chromium.orgc82e66e2014-02-04 07:05:47 +0000121 void AddInputPort(const MidiPortInfo& info);
122 void AddOutputPort(const MidiPortInfo& info);
crogers@google.com27356e42013-06-22 04:03:03 +0000123
124 // Dispatches to all clients.
yhirano@chromium.orgcfa642c2014-05-01 08:54:41 +0000125 // TODO(toyoshim): Fix the mac implementation to use
126 // |ReceiveMidiData(..., base::TimeTicks)|.
toyoshim@chromium.orgc82e66e2014-02-04 07:05:47 +0000127 void ReceiveMidiData(uint32 port_index,
toyoshim@chromium.orgae6ad362013-08-27 15:30:20 +0000128 const uint8* data,
129 size_t length,
130 double timestamp);
131
yhirano@chromium.orgcfa642c2014-05-01 08:54:41 +0000132 void ReceiveMidiData(uint32 port_index,
133 const uint8* data,
134 size_t length,
135 base::TimeTicks time) {
136 ReceiveMidiData(port_index, data, length,
137 (time - base::TimeTicks()).InSecondsF());
138 }
139
toyoshim@chromium.orgc1e05fb2014-05-06 16:39:20 +0000140 size_t get_clients_size_for_testing() const { return clients_.size(); }
141 size_t get_pending_clients_size_for_testing() const {
142 return pending_clients_.size();
143 }
144
145 // TODO(toyoshim): Make |input_ports_| and |output_ports_| private members.
146 MidiPortInfoList input_ports_;
147 MidiPortInfoList output_ports_;
148
149 private:
150 void CompleteInitializationInternal(MidiResult result);
crogers@google.com27356e42013-06-22 04:03:03 +0000151
152 // Keeps track of all clients who wish to receive MIDI data.
toyoshim@chromium.orgc82e66e2014-02-04 07:05:47 +0000153 typedef std::set<MidiManagerClient*> ClientList;
crogers@google.com27356e42013-06-22 04:03:03 +0000154 ClientList clients_;
155
toyoshim@chromium.org51c7f532014-05-01 17:17:32 +0000156 // Keeps track of all clients who are waiting for CompleteStartSession().
157 typedef std::map<int, MidiManagerClient*> PendingClientMap;
158 PendingClientMap pending_clients_;
159
toyoshim@chromium.orgc1e05fb2014-05-06 16:39:20 +0000160 // Keeps a SingleThreadTaskRunner of the thread that calls StartSession in
161 // order to invoke CompleteStartSession() on the thread.
162 scoped_refptr<base::SingleThreadTaskRunner> session_thread_runner_;
crogers@google.com27356e42013-06-22 04:03:03 +0000163
toyoshim@chromium.orgc1e05fb2014-05-06 16:39:20 +0000164 // Keeps true if platform dependent initialization is already completed.
165 bool initialized_;
crogers@google.com27356e42013-06-22 04:03:03 +0000166
toyoshim@chromium.orgc1e05fb2014-05-06 16:39:20 +0000167 // Keeps the platform dependent initialization result if initialization is
168 // completed. Otherwise keeps MIDI_NOT_SUPPORTED.
169 MidiResult result_;
170
171 // Protects access to |clients_|, |pending_clients_|, |initialized_|, and
172 // |result_|.
173 base::Lock lock_;
174
toyoshim@chromium.orgc82e66e2014-02-04 07:05:47 +0000175 DISALLOW_COPY_AND_ASSIGN(MidiManager);
crogers@google.com27356e42013-06-22 04:03:03 +0000176};
177
178} // namespace media
179
180#endif // MEDIA_MIDI_MIDI_MANAGER_H_