crogers@google.com | 27356e4 | 2013-06-22 04:03:03 +0000 | [diff] [blame] | 1 | // 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> |
| 9 | |
| 10 | #include "base/basictypes.h" |
| 11 | #include "base/synchronization/lock.h" |
| 12 | #include "media/base/media_export.h" |
| 13 | #include "media/midi/midi_port_info.h" |
| 14 | |
| 15 | namespace media { |
| 16 | |
| 17 | // A MIDIManagerClient registers with the MIDIManager to receive MIDI data. |
| 18 | // See MIDIManager::RequestAccess() and MIDIManager::ReleaseAccess() |
| 19 | // for details. |
| 20 | class MEDIA_EXPORT MIDIManagerClient { |
| 21 | public: |
| 22 | virtual ~MIDIManagerClient() {} |
| 23 | |
| 24 | // ReceiveMIDIData() is called when MIDI data has been received from the |
| 25 | // MIDI system. |
| 26 | // |port_index| represents the specific input port from input_ports(). |
| 27 | // |data| represents a series of bytes encoding one or more MIDI messages. |
| 28 | // |length| is the number of bytes in |data|. |
| 29 | // |timestamp| is the time the data was received, in seconds. |
| 30 | virtual void ReceiveMIDIData(int port_index, |
| 31 | const uint8* data, |
| 32 | size_t length, |
| 33 | double timestamp) = 0; |
| 34 | }; |
| 35 | |
| 36 | // Manages access to all MIDI hardware. |
| 37 | class MEDIA_EXPORT MIDIManager { |
| 38 | public: |
crogers@google.com | 27356e4 | 2013-06-22 04:03:03 +0000 | [diff] [blame] | 39 | static MIDIManager* Create(); |
| 40 | |
| 41 | MIDIManager(); |
| 42 | virtual ~MIDIManager(); |
| 43 | |
toyoshim@chromium.org | 235b09e | 2013-07-25 16:23:14 +0000 | [diff] [blame^] | 44 | // A client calls StartSession() to receive and send MIDI data. |
| 45 | // If the session is ready to start, the MIDI system is lazily initialized |
crogers@google.com | 27356e4 | 2013-06-22 04:03:03 +0000 | [diff] [blame] | 46 | // and the client is registered to receive MIDI data. |
toyoshim@chromium.org | 235b09e | 2013-07-25 16:23:14 +0000 | [diff] [blame^] | 47 | // Returns |true| if the session succeeds to start. |
| 48 | bool StartSession(MIDIManagerClient* client); |
crogers@google.com | 27356e4 | 2013-06-22 04:03:03 +0000 | [diff] [blame] | 49 | |
toyoshim@chromium.org | 235b09e | 2013-07-25 16:23:14 +0000 | [diff] [blame^] | 50 | // A client calls ReleaseSession() to stop receiving MIDI data. |
| 51 | void EndSession(MIDIManagerClient* client); |
crogers@google.com | 27356e4 | 2013-06-22 04:03:03 +0000 | [diff] [blame] | 52 | |
| 53 | // SendMIDIData() sends one or more messages at the given time. |
| 54 | // |port_index| represents the specific output port from output_ports(). |
| 55 | // |data| represents a series of bytes encoding one or more MIDI messages. |
| 56 | // |length| is the number of bytes in |data|. |
| 57 | // |timestamp| is the time to send the data, in seconds. |
| 58 | virtual void SendMIDIData(int port_index, |
| 59 | const uint8* data, |
| 60 | size_t length, |
| 61 | double timestamp) = 0; |
| 62 | |
| 63 | // input_ports() is a list of MIDI ports for receiving MIDI data. |
| 64 | // Each individual port in this list can be identified by its |
| 65 | // integer index into this list. |
| 66 | const MIDIPortInfoList& input_ports() { return input_ports_; } |
| 67 | |
| 68 | // output_ports() is a list of MIDI ports for sending MIDI data. |
| 69 | // Each individual port in this list can be identified by its |
| 70 | // integer index into this list. |
| 71 | const MIDIPortInfoList& output_ports() { return output_ports_; } |
| 72 | |
| 73 | protected: |
| 74 | // Initializes the MIDI system, returning |true| on success. |
| 75 | virtual bool Initialize() = 0; |
| 76 | |
| 77 | void AddInputPort(const MIDIPortInfo& info); |
| 78 | void AddOutputPort(const MIDIPortInfo& info); |
| 79 | |
| 80 | // Dispatches to all clients. |
| 81 | void ReceiveMIDIData( |
| 82 | int port_index, |
| 83 | const uint8* data, |
| 84 | size_t length, |
| 85 | double timestamp); |
| 86 | |
| 87 | bool initialized_; |
| 88 | |
| 89 | // Keeps track of all clients who wish to receive MIDI data. |
| 90 | typedef std::set<MIDIManagerClient*> ClientList; |
| 91 | ClientList clients_; |
| 92 | |
| 93 | // Protects access to our clients. |
| 94 | base::Lock clients_lock_; |
| 95 | |
| 96 | MIDIPortInfoList input_ports_; |
| 97 | MIDIPortInfoList output_ports_; |
| 98 | |
| 99 | DISALLOW_COPY_AND_ASSIGN(MIDIManager); |
| 100 | }; |
| 101 | |
| 102 | } // namespace media |
| 103 | |
| 104 | #endif // MEDIA_MIDI_MIDI_MANAGER_H_ |