blob: dd7828bdadb1092edf9c28865f014401afc1d0ab [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"
12#include "base/synchronization/lock.h"
13#include "media/base/media_export.h"
14#include "media/midi/midi_port_info.h"
15
16namespace media {
17
toyoshim@chromium.orgc82e66e2014-02-04 07:05:47 +000018// A MidiManagerClient registers with the MidiManager to receive MIDI data.
19// See MidiManager::RequestAccess() and MidiManager::ReleaseAccess()
crogers@google.com27356e42013-06-22 04:03:03 +000020// for details.
toyoshim@chromium.orgc82e66e2014-02-04 07:05:47 +000021class MEDIA_EXPORT MidiManagerClient {
crogers@google.com27356e42013-06-22 04:03:03 +000022 public:
toyoshim@chromium.orgc82e66e2014-02-04 07:05:47 +000023 virtual ~MidiManagerClient() {}
crogers@google.com27356e42013-06-22 04:03:03 +000024
toyoshim@chromium.orgc82e66e2014-02-04 07:05:47 +000025 // ReceiveMidiData() is called when MIDI data has been received from the
crogers@google.com27356e42013-06-22 04:03:03 +000026 // MIDI system.
27 // |port_index| represents the specific input port from input_ports().
28 // |data| represents a series of bytes encoding one or more MIDI messages.
29 // |length| is the number of bytes in |data|.
30 // |timestamp| is the time the data was received, in seconds.
toyoshim@chromium.orgc82e66e2014-02-04 07:05:47 +000031 virtual void ReceiveMidiData(uint32 port_index,
crogers@google.com27356e42013-06-22 04:03:03 +000032 const uint8* data,
33 size_t length,
34 double timestamp) = 0;
crogers@google.com542a43a2013-07-31 05:16:49 +000035
toyoshim@chromium.orgc82e66e2014-02-04 07:05:47 +000036 // AccumulateMidiBytesSent() is called to acknowledge when bytes have
crogers@google.com542a43a2013-07-31 05:16:49 +000037 // successfully been sent to the hardware.
38 // This happens as a result of the client having previously called
toyoshim@chromium.orgc82e66e2014-02-04 07:05:47 +000039 // MidiManager::DispatchSendMidiData().
40 virtual void AccumulateMidiBytesSent(size_t n) = 0;
crogers@google.com27356e42013-06-22 04:03:03 +000041};
42
43// Manages access to all MIDI hardware.
toyoshim@chromium.orgc82e66e2014-02-04 07:05:47 +000044class MEDIA_EXPORT MidiManager {
crogers@google.com27356e42013-06-22 04:03:03 +000045 public:
toyoshim@chromium.orgc82e66e2014-02-04 07:05:47 +000046 static MidiManager* Create();
crogers@google.com27356e42013-06-22 04:03:03 +000047
toyoshim@chromium.orgc82e66e2014-02-04 07:05:47 +000048 MidiManager();
49 virtual ~MidiManager();
crogers@google.com27356e42013-06-22 04:03:03 +000050
toyoshim@chromium.org235b09e2013-07-25 16:23:14 +000051 // A client calls StartSession() to receive and send MIDI data.
52 // If the session is ready to start, the MIDI system is lazily initialized
crogers@google.com27356e42013-06-22 04:03:03 +000053 // and the client is registered to receive MIDI data.
toyoshim@chromium.org235b09e2013-07-25 16:23:14 +000054 // Returns |true| if the session succeeds to start.
toyoshim@chromium.orgc82e66e2014-02-04 07:05:47 +000055 bool StartSession(MidiManagerClient* client);
crogers@google.com27356e42013-06-22 04:03:03 +000056
toyoshim@chromium.org235b09e2013-07-25 16:23:14 +000057 // A client calls ReleaseSession() to stop receiving MIDI data.
toyoshim@chromium.orgc82e66e2014-02-04 07:05:47 +000058 void EndSession(MidiManagerClient* client);
crogers@google.com27356e42013-06-22 04:03:03 +000059
toyoshim@chromium.orgc82e66e2014-02-04 07:05:47 +000060 // DispatchSendMidiData() is called when MIDI data should be sent to the MIDI
yukawa@chromium.orgdb7ad8b2013-12-04 15:42:41 +000061 // system.
62 // This method is supposed to return immediately and should not block.
crogers@google.com27356e42013-06-22 04:03:03 +000063 // |port_index| represents the specific output port from output_ports().
64 // |data| represents a series of bytes encoding one or more MIDI messages.
65 // |length| is the number of bytes in |data|.
crogers@google.com542a43a2013-07-31 05:16:49 +000066 // |timestamp| is the time to send the data, in seconds. A value of 0
67 // means send "now" or as soon as possible.
yhirano@chromium.orgc6d5b7b2013-12-20 07:27:23 +000068 // The default implementation is for unsupported platforms.
toyoshim@chromium.orgc82e66e2014-02-04 07:05:47 +000069 virtual void DispatchSendMidiData(MidiManagerClient* client,
yukawa@chromium.orgdb7ad8b2013-12-04 15:42:41 +000070 uint32 port_index,
71 const std::vector<uint8>& data,
yhirano@chromium.orgc6d5b7b2013-12-20 07:27:23 +000072 double timestamp);
crogers@google.com27356e42013-06-22 04:03:03 +000073
74 // input_ports() is a list of MIDI ports for receiving MIDI data.
75 // Each individual port in this list can be identified by its
76 // integer index into this list.
toyoshim@chromium.orgc82e66e2014-02-04 07:05:47 +000077 const MidiPortInfoList& input_ports() { return input_ports_; }
crogers@google.com27356e42013-06-22 04:03:03 +000078
79 // output_ports() is a list of MIDI ports for sending MIDI data.
80 // Each individual port in this list can be identified by its
81 // integer index into this list.
toyoshim@chromium.orgc82e66e2014-02-04 07:05:47 +000082 const MidiPortInfoList& output_ports() { return output_ports_; }
crogers@google.com27356e42013-06-22 04:03:03 +000083
84 protected:
85 // Initializes the MIDI system, returning |true| on success.
yhirano@chromium.orgc6d5b7b2013-12-20 07:27:23 +000086 // The default implementation is for unsupported platforms.
87 virtual bool Initialize();
crogers@google.com27356e42013-06-22 04:03:03 +000088
toyoshim@chromium.orgc82e66e2014-02-04 07:05:47 +000089 void AddInputPort(const MidiPortInfo& info);
90 void AddOutputPort(const MidiPortInfo& info);
crogers@google.com27356e42013-06-22 04:03:03 +000091
92 // Dispatches to all clients.
toyoshim@chromium.orgc82e66e2014-02-04 07:05:47 +000093 void ReceiveMidiData(uint32 port_index,
toyoshim@chromium.orgae6ad362013-08-27 15:30:20 +000094 const uint8* data,
95 size_t length,
96 double timestamp);
97
crogers@google.com27356e42013-06-22 04:03:03 +000098 bool initialized_;
99
100 // Keeps track of all clients who wish to receive MIDI data.
toyoshim@chromium.orgc82e66e2014-02-04 07:05:47 +0000101 typedef std::set<MidiManagerClient*> ClientList;
crogers@google.com27356e42013-06-22 04:03:03 +0000102 ClientList clients_;
103
104 // Protects access to our clients.
105 base::Lock clients_lock_;
106
toyoshim@chromium.orgc82e66e2014-02-04 07:05:47 +0000107 MidiPortInfoList input_ports_;
108 MidiPortInfoList output_ports_;
crogers@google.com27356e42013-06-22 04:03:03 +0000109
toyoshim@chromium.orgc82e66e2014-02-04 07:05:47 +0000110 DISALLOW_COPY_AND_ASSIGN(MidiManager);
crogers@google.com27356e42013-06-22 04:03:03 +0000111};
112
113} // namespace media
114
115#endif // MEDIA_MIDI_MIDI_MANAGER_H_