blob: 88c930bbc41d6a0f61733694859f5a60e4ede23f [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"
toyoshim@chromium.orgf77a1e62014-04-11 13:16:24 +000015#include "media/midi/midi_result.h"
crogers@google.com27356e42013-06-22 04:03:03 +000016
17namespace media {
18
toyoshim@chromium.orgc82e66e2014-02-04 07:05:47 +000019// A MidiManagerClient registers with the MidiManager to receive MIDI data.
20// See MidiManager::RequestAccess() and MidiManager::ReleaseAccess()
crogers@google.com27356e42013-06-22 04:03:03 +000021// for details.
toyoshim@chromium.orgc82e66e2014-02-04 07:05:47 +000022class MEDIA_EXPORT MidiManagerClient {
crogers@google.com27356e42013-06-22 04:03:03 +000023 public:
toyoshim@chromium.orgf77a1e62014-04-11 13:16:24 +000024 virtual ~MidiManagerClient() {}
25
26 // CompleteStartSession() is called when platform dependent preparation is
27 // finished.
28 virtual void CompleteStartSession(int client_id, MidiResult result) = 0;
crogers@google.com27356e42013-06-22 04:03:03 +000029
toyoshim@chromium.orgc82e66e2014-02-04 07:05:47 +000030 // ReceiveMidiData() is called when MIDI data has been received from the
crogers@google.com27356e42013-06-22 04:03:03 +000031 // MIDI system.
32 // |port_index| represents the specific input port from input_ports().
33 // |data| represents a series of bytes encoding one or more MIDI messages.
34 // |length| is the number of bytes in |data|.
35 // |timestamp| is the time the data was received, in seconds.
toyoshim@chromium.orgc82e66e2014-02-04 07:05:47 +000036 virtual void ReceiveMidiData(uint32 port_index,
crogers@google.com27356e42013-06-22 04:03:03 +000037 const uint8* data,
38 size_t length,
39 double timestamp) = 0;
crogers@google.com542a43a2013-07-31 05:16:49 +000040
toyoshim@chromium.orgc82e66e2014-02-04 07:05:47 +000041 // AccumulateMidiBytesSent() is called to acknowledge when bytes have
crogers@google.com542a43a2013-07-31 05:16:49 +000042 // successfully been sent to the hardware.
43 // This happens as a result of the client having previously called
toyoshim@chromium.orgc82e66e2014-02-04 07:05:47 +000044 // MidiManager::DispatchSendMidiData().
45 virtual void AccumulateMidiBytesSent(size_t n) = 0;
crogers@google.com27356e42013-06-22 04:03:03 +000046};
47
48// Manages access to all MIDI hardware.
toyoshim@chromium.orgc82e66e2014-02-04 07:05:47 +000049class MEDIA_EXPORT MidiManager {
crogers@google.com27356e42013-06-22 04:03:03 +000050 public:
toyoshim@chromium.orgc82e66e2014-02-04 07:05:47 +000051 static MidiManager* Create();
crogers@google.com27356e42013-06-22 04:03:03 +000052
toyoshim@chromium.orgc82e66e2014-02-04 07:05:47 +000053 MidiManager();
54 virtual ~MidiManager();
crogers@google.com27356e42013-06-22 04:03:03 +000055
toyoshim@chromium.org235b09e2013-07-25 16:23:14 +000056 // A client calls StartSession() to receive and send MIDI data.
57 // If the session is ready to start, the MIDI system is lazily initialized
crogers@google.com27356e42013-06-22 04:03:03 +000058 // and the client is registered to receive MIDI data.
toyoshim@chromium.orgf77a1e62014-04-11 13:16:24 +000059 // CompleteStartSession() is called with MIDI_OK if the session is started.
60 // Otherwise CompleteStartSession() is called with proper MidiResult code.
61 void StartSession(MidiManagerClient* client, int client_id);
crogers@google.com27356e42013-06-22 04:03:03 +000062
toyoshim@chromium.orgf77a1e62014-04-11 13:16:24 +000063 // A client calls EndSession() to stop receiving MIDI data.
toyoshim@chromium.orgc82e66e2014-02-04 07:05:47 +000064 void EndSession(MidiManagerClient* client);
crogers@google.com27356e42013-06-22 04:03:03 +000065
toyoshim@chromium.orgc82e66e2014-02-04 07:05:47 +000066 // DispatchSendMidiData() is called when MIDI data should be sent to the MIDI
yukawa@chromium.orgdb7ad8b2013-12-04 15:42:41 +000067 // system.
68 // This method is supposed to return immediately and should not block.
crogers@google.com27356e42013-06-22 04:03:03 +000069 // |port_index| represents the specific output port from output_ports().
70 // |data| represents a series of bytes encoding one or more MIDI messages.
71 // |length| is the number of bytes in |data|.
crogers@google.com542a43a2013-07-31 05:16:49 +000072 // |timestamp| is the time to send the data, in seconds. A value of 0
73 // means send "now" or as soon as possible.
yhirano@chromium.orgc6d5b7b2013-12-20 07:27:23 +000074 // The default implementation is for unsupported platforms.
toyoshim@chromium.orgc82e66e2014-02-04 07:05:47 +000075 virtual void DispatchSendMidiData(MidiManagerClient* client,
yukawa@chromium.orgdb7ad8b2013-12-04 15:42:41 +000076 uint32 port_index,
77 const std::vector<uint8>& data,
yhirano@chromium.orgc6d5b7b2013-12-20 07:27:23 +000078 double timestamp);
crogers@google.com27356e42013-06-22 04:03:03 +000079
80 // input_ports() is a list of MIDI ports for receiving MIDI data.
81 // Each individual port in this list can be identified by its
82 // integer index into this list.
toyoshim@chromium.orgc82e66e2014-02-04 07:05:47 +000083 const MidiPortInfoList& input_ports() { return input_ports_; }
crogers@google.com27356e42013-06-22 04:03:03 +000084
85 // output_ports() is a list of MIDI ports for sending MIDI data.
86 // Each individual port in this list can be identified by its
87 // integer index into this list.
toyoshim@chromium.orgc82e66e2014-02-04 07:05:47 +000088 const MidiPortInfoList& output_ports() { return output_ports_; }
crogers@google.com27356e42013-06-22 04:03:03 +000089
90 protected:
91 // Initializes the MIDI system, returning |true| on success.
yhirano@chromium.orgc6d5b7b2013-12-20 07:27:23 +000092 // The default implementation is for unsupported platforms.
toyoshim@chromium.orgf77a1e62014-04-11 13:16:24 +000093 virtual MidiResult Initialize();
crogers@google.com27356e42013-06-22 04:03:03 +000094
toyoshim@chromium.orgc82e66e2014-02-04 07:05:47 +000095 void AddInputPort(const MidiPortInfo& info);
96 void AddOutputPort(const MidiPortInfo& info);
crogers@google.com27356e42013-06-22 04:03:03 +000097
98 // Dispatches to all clients.
toyoshim@chromium.orgc82e66e2014-02-04 07:05:47 +000099 void ReceiveMidiData(uint32 port_index,
toyoshim@chromium.orgae6ad362013-08-27 15:30:20 +0000100 const uint8* data,
101 size_t length,
102 double timestamp);
103
crogers@google.com27356e42013-06-22 04:03:03 +0000104 bool initialized_;
toyoshim@chromium.orgf77a1e62014-04-11 13:16:24 +0000105 MidiResult result_;
crogers@google.com27356e42013-06-22 04:03:03 +0000106
107 // Keeps track of all clients who wish to receive MIDI data.
toyoshim@chromium.orgc82e66e2014-02-04 07:05:47 +0000108 typedef std::set<MidiManagerClient*> ClientList;
crogers@google.com27356e42013-06-22 04:03:03 +0000109 ClientList clients_;
110
111 // Protects access to our clients.
112 base::Lock clients_lock_;
113
toyoshim@chromium.orgc82e66e2014-02-04 07:05:47 +0000114 MidiPortInfoList input_ports_;
115 MidiPortInfoList output_ports_;
crogers@google.com27356e42013-06-22 04:03:03 +0000116
toyoshim@chromium.orgc82e66e2014-02-04 07:05:47 +0000117 DISALLOW_COPY_AND_ASSIGN(MidiManager);
crogers@google.com27356e42013-06-22 04:03:03 +0000118};
119
120} // namespace media
121
122#endif // MEDIA_MIDI_MIDI_MANAGER_H_