blob: c21c66af5b06469bca41ff39e3b5d27e71353f5d [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"
yhirano@chromium.orgcfa642c2014-05-01 08:54:41 +000013#include "base/time/time.h"
crogers@google.com27356e42013-06-22 04:03:03 +000014#include "media/base/media_export.h"
15#include "media/midi/midi_port_info.h"
toyoshim@chromium.orgf77a1e62014-04-11 13:16:24 +000016#include "media/midi/midi_result.h"
crogers@google.com27356e42013-06-22 04:03:03 +000017
18namespace media {
19
toyoshim@chromium.orgc82e66e2014-02-04 07:05:47 +000020// A MidiManagerClient registers with the MidiManager to receive MIDI data.
21// See MidiManager::RequestAccess() and MidiManager::ReleaseAccess()
crogers@google.com27356e42013-06-22 04:03:03 +000022// for details.
toyoshim@chromium.orgc82e66e2014-02-04 07:05:47 +000023class MEDIA_EXPORT MidiManagerClient {
crogers@google.com27356e42013-06-22 04:03:03 +000024 public:
toyoshim@chromium.orgf77a1e62014-04-11 13:16:24 +000025 virtual ~MidiManagerClient() {}
26
27 // CompleteStartSession() is called when platform dependent preparation is
28 // finished.
29 virtual void CompleteStartSession(int client_id, MidiResult result) = 0;
crogers@google.com27356e42013-06-22 04:03:03 +000030
toyoshim@chromium.orgc82e66e2014-02-04 07:05:47 +000031 // ReceiveMidiData() is called when MIDI data has been received from the
crogers@google.com27356e42013-06-22 04:03:03 +000032 // MIDI system.
33 // |port_index| represents the specific input port from input_ports().
34 // |data| represents a series of bytes encoding one or more MIDI messages.
35 // |length| is the number of bytes in |data|.
36 // |timestamp| is the time the data was received, in seconds.
toyoshim@chromium.orgc82e66e2014-02-04 07:05:47 +000037 virtual void ReceiveMidiData(uint32 port_index,
crogers@google.com27356e42013-06-22 04:03:03 +000038 const uint8* data,
39 size_t length,
40 double timestamp) = 0;
crogers@google.com542a43a2013-07-31 05:16:49 +000041
toyoshim@chromium.orgc82e66e2014-02-04 07:05:47 +000042 // AccumulateMidiBytesSent() is called to acknowledge when bytes have
crogers@google.com542a43a2013-07-31 05:16:49 +000043 // successfully been sent to the hardware.
44 // This happens as a result of the client having previously called
toyoshim@chromium.orgc82e66e2014-02-04 07:05:47 +000045 // MidiManager::DispatchSendMidiData().
46 virtual void AccumulateMidiBytesSent(size_t n) = 0;
crogers@google.com27356e42013-06-22 04:03:03 +000047};
48
49// Manages access to all MIDI hardware.
toyoshim@chromium.orgc82e66e2014-02-04 07:05:47 +000050class MEDIA_EXPORT MidiManager {
crogers@google.com27356e42013-06-22 04:03:03 +000051 public:
toyoshim@chromium.orgc82e66e2014-02-04 07:05:47 +000052 static MidiManager* Create();
crogers@google.com27356e42013-06-22 04:03:03 +000053
toyoshim@chromium.orgc82e66e2014-02-04 07:05:47 +000054 MidiManager();
55 virtual ~MidiManager();
crogers@google.com27356e42013-06-22 04:03:03 +000056
toyoshim@chromium.org235b09e2013-07-25 16:23:14 +000057 // A client calls StartSession() to receive and send MIDI data.
58 // If the session is ready to start, the MIDI system is lazily initialized
crogers@google.com27356e42013-06-22 04:03:03 +000059 // and the client is registered to receive MIDI data.
toyoshim@chromium.orgf77a1e62014-04-11 13:16:24 +000060 // CompleteStartSession() is called with MIDI_OK if the session is started.
61 // Otherwise CompleteStartSession() is called with proper MidiResult code.
62 void StartSession(MidiManagerClient* client, int client_id);
crogers@google.com27356e42013-06-22 04:03:03 +000063
toyoshim@chromium.orgf77a1e62014-04-11 13:16:24 +000064 // A client calls EndSession() to stop receiving MIDI data.
toyoshim@chromium.orgc82e66e2014-02-04 07:05:47 +000065 void EndSession(MidiManagerClient* client);
crogers@google.com27356e42013-06-22 04:03:03 +000066
toyoshim@chromium.orgc82e66e2014-02-04 07:05:47 +000067 // DispatchSendMidiData() is called when MIDI data should be sent to the MIDI
yukawa@chromium.orgdb7ad8b2013-12-04 15:42:41 +000068 // system.
69 // This method is supposed to return immediately and should not block.
crogers@google.com27356e42013-06-22 04:03:03 +000070 // |port_index| represents the specific output port from output_ports().
71 // |data| represents a series of bytes encoding one or more MIDI messages.
72 // |length| is the number of bytes in |data|.
crogers@google.com542a43a2013-07-31 05:16:49 +000073 // |timestamp| is the time to send the data, in seconds. A value of 0
74 // means send "now" or as soon as possible.
yhirano@chromium.orgc6d5b7b2013-12-20 07:27:23 +000075 // The default implementation is for unsupported platforms.
toyoshim@chromium.orgc82e66e2014-02-04 07:05:47 +000076 virtual void DispatchSendMidiData(MidiManagerClient* client,
yukawa@chromium.orgdb7ad8b2013-12-04 15:42:41 +000077 uint32 port_index,
78 const std::vector<uint8>& data,
yhirano@chromium.orgc6d5b7b2013-12-20 07:27:23 +000079 double timestamp);
crogers@google.com27356e42013-06-22 04:03:03 +000080
81 // input_ports() is a list of MIDI ports for receiving MIDI data.
82 // Each individual port in this list can be identified by its
83 // integer index into this list.
toyoshim@chromium.orgc82e66e2014-02-04 07:05:47 +000084 const MidiPortInfoList& input_ports() { return input_ports_; }
crogers@google.com27356e42013-06-22 04:03:03 +000085
86 // output_ports() is a list of MIDI ports for sending MIDI data.
87 // Each individual port in this list can be identified by its
88 // integer index into this list.
toyoshim@chromium.orgc82e66e2014-02-04 07:05:47 +000089 const MidiPortInfoList& output_ports() { return output_ports_; }
crogers@google.com27356e42013-06-22 04:03:03 +000090
91 protected:
92 // Initializes the MIDI system, returning |true| on success.
yhirano@chromium.orgc6d5b7b2013-12-20 07:27:23 +000093 // The default implementation is for unsupported platforms.
toyoshim@chromium.orgf77a1e62014-04-11 13:16:24 +000094 virtual MidiResult Initialize();
crogers@google.com27356e42013-06-22 04:03:03 +000095
toyoshim@chromium.orgc82e66e2014-02-04 07:05:47 +000096 void AddInputPort(const MidiPortInfo& info);
97 void AddOutputPort(const MidiPortInfo& info);
crogers@google.com27356e42013-06-22 04:03:03 +000098
99 // Dispatches to all clients.
yhirano@chromium.orgcfa642c2014-05-01 08:54:41 +0000100 // TODO(toyoshim): Fix the mac implementation to use
101 // |ReceiveMidiData(..., base::TimeTicks)|.
toyoshim@chromium.orgc82e66e2014-02-04 07:05:47 +0000102 void ReceiveMidiData(uint32 port_index,
toyoshim@chromium.orgae6ad362013-08-27 15:30:20 +0000103 const uint8* data,
104 size_t length,
105 double timestamp);
106
yhirano@chromium.orgcfa642c2014-05-01 08:54:41 +0000107 void ReceiveMidiData(uint32 port_index,
108 const uint8* data,
109 size_t length,
110 base::TimeTicks time) {
111 ReceiveMidiData(port_index, data, length,
112 (time - base::TimeTicks()).InSecondsF());
113 }
114
crogers@google.com27356e42013-06-22 04:03:03 +0000115 bool initialized_;
toyoshim@chromium.orgf77a1e62014-04-11 13:16:24 +0000116 MidiResult result_;
crogers@google.com27356e42013-06-22 04:03:03 +0000117
118 // Keeps track of all clients who wish to receive MIDI data.
toyoshim@chromium.orgc82e66e2014-02-04 07:05:47 +0000119 typedef std::set<MidiManagerClient*> ClientList;
crogers@google.com27356e42013-06-22 04:03:03 +0000120 ClientList clients_;
121
122 // Protects access to our clients.
123 base::Lock clients_lock_;
124
toyoshim@chromium.orgc82e66e2014-02-04 07:05:47 +0000125 MidiPortInfoList input_ports_;
126 MidiPortInfoList output_ports_;
crogers@google.com27356e42013-06-22 04:03:03 +0000127
yhirano@chromium.orgcfa642c2014-05-01 08:54:41 +0000128 private:
toyoshim@chromium.orgc82e66e2014-02-04 07:05:47 +0000129 DISALLOW_COPY_AND_ASSIGN(MidiManager);
crogers@google.com27356e42013-06-22 04:03:03 +0000130};
131
132} // namespace media
133
134#endif // MEDIA_MIDI_MIDI_MANAGER_H_