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> |
toyoshim@chromium.org | ae6ad36 | 2013-08-27 15:30:20 +0000 | [diff] [blame^] | 9 | #include <vector> |
crogers@google.com | 27356e4 | 2013-06-22 04:03:03 +0000 | [diff] [blame] | 10 | |
| 11 | #include "base/basictypes.h" |
crogers@google.com | 542a43a | 2013-07-31 05:16:49 +0000 | [diff] [blame] | 12 | #include "base/memory/scoped_ptr.h" |
| 13 | #include "base/message_loop/message_loop_proxy.h" |
crogers@google.com | 27356e4 | 2013-06-22 04:03:03 +0000 | [diff] [blame] | 14 | #include "base/synchronization/lock.h" |
| 15 | #include "media/base/media_export.h" |
| 16 | #include "media/midi/midi_port_info.h" |
| 17 | |
crogers@google.com | 542a43a | 2013-07-31 05:16:49 +0000 | [diff] [blame] | 18 | namespace base { |
| 19 | class Thread; |
| 20 | } |
| 21 | |
crogers@google.com | 27356e4 | 2013-06-22 04:03:03 +0000 | [diff] [blame] | 22 | namespace media { |
| 23 | |
| 24 | // A MIDIManagerClient registers with the MIDIManager to receive MIDI data. |
| 25 | // See MIDIManager::RequestAccess() and MIDIManager::ReleaseAccess() |
| 26 | // for details. |
| 27 | class MEDIA_EXPORT MIDIManagerClient { |
| 28 | public: |
| 29 | virtual ~MIDIManagerClient() {} |
| 30 | |
| 31 | // ReceiveMIDIData() is called when MIDI data has been received from the |
| 32 | // 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.org | ae6ad36 | 2013-08-27 15:30:20 +0000 | [diff] [blame^] | 37 | virtual void ReceiveMIDIData(uint32 port_index, |
crogers@google.com | 27356e4 | 2013-06-22 04:03:03 +0000 | [diff] [blame] | 38 | const uint8* data, |
| 39 | size_t length, |
| 40 | double timestamp) = 0; |
crogers@google.com | 542a43a | 2013-07-31 05:16:49 +0000 | [diff] [blame] | 41 | |
| 42 | // AccumulateMIDIBytesSent() is called to acknowledge when bytes have |
| 43 | // successfully been sent to the hardware. |
| 44 | // This happens as a result of the client having previously called |
| 45 | // MIDIManager::DispatchSendMIDIData(). |
| 46 | virtual void AccumulateMIDIBytesSent(size_t n) = 0; |
crogers@google.com | 27356e4 | 2013-06-22 04:03:03 +0000 | [diff] [blame] | 47 | }; |
| 48 | |
| 49 | // Manages access to all MIDI hardware. |
| 50 | class MEDIA_EXPORT MIDIManager { |
| 51 | public: |
crogers@google.com | 27356e4 | 2013-06-22 04:03:03 +0000 | [diff] [blame] | 52 | static MIDIManager* Create(); |
| 53 | |
| 54 | MIDIManager(); |
| 55 | virtual ~MIDIManager(); |
| 56 | |
toyoshim@chromium.org | 235b09e | 2013-07-25 16:23:14 +0000 | [diff] [blame] | 57 | // 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.com | 27356e4 | 2013-06-22 04:03:03 +0000 | [diff] [blame] | 59 | // and the client is registered to receive MIDI data. |
toyoshim@chromium.org | 235b09e | 2013-07-25 16:23:14 +0000 | [diff] [blame] | 60 | // Returns |true| if the session succeeds to start. |
| 61 | bool StartSession(MIDIManagerClient* client); |
crogers@google.com | 27356e4 | 2013-06-22 04:03:03 +0000 | [diff] [blame] | 62 | |
toyoshim@chromium.org | 235b09e | 2013-07-25 16:23:14 +0000 | [diff] [blame] | 63 | // A client calls ReleaseSession() to stop receiving MIDI data. |
| 64 | void EndSession(MIDIManagerClient* client); |
crogers@google.com | 27356e4 | 2013-06-22 04:03:03 +0000 | [diff] [blame] | 65 | |
crogers@google.com | 542a43a | 2013-07-31 05:16:49 +0000 | [diff] [blame] | 66 | // DispatchSendMIDIData() schedules one or more messages to be sent |
| 67 | // at the given time on a dedicated thread. |
crogers@google.com | 27356e4 | 2013-06-22 04:03:03 +0000 | [diff] [blame] | 68 | // |port_index| represents the specific output port from output_ports(). |
| 69 | // |data| represents a series of bytes encoding one or more MIDI messages. |
| 70 | // |length| is the number of bytes in |data|. |
crogers@google.com | 542a43a | 2013-07-31 05:16:49 +0000 | [diff] [blame] | 71 | // |timestamp| is the time to send the data, in seconds. A value of 0 |
| 72 | // means send "now" or as soon as possible. |
| 73 | void DispatchSendMIDIData(MIDIManagerClient* client, |
toyoshim@chromium.org | ae6ad36 | 2013-08-27 15:30:20 +0000 | [diff] [blame^] | 74 | uint32 port_index, |
| 75 | const std::vector<uint8>& data, |
crogers@google.com | 542a43a | 2013-07-31 05:16:49 +0000 | [diff] [blame] | 76 | double timestamp); |
crogers@google.com | 27356e4 | 2013-06-22 04:03:03 +0000 | [diff] [blame] | 77 | |
| 78 | // input_ports() is a list of MIDI ports for receiving MIDI data. |
| 79 | // Each individual port in this list can be identified by its |
| 80 | // integer index into this list. |
| 81 | const MIDIPortInfoList& input_ports() { return input_ports_; } |
| 82 | |
| 83 | // output_ports() is a list of MIDI ports for sending MIDI data. |
| 84 | // Each individual port in this list can be identified by its |
| 85 | // integer index into this list. |
| 86 | const MIDIPortInfoList& output_ports() { return output_ports_; } |
| 87 | |
| 88 | protected: |
| 89 | // Initializes the MIDI system, returning |true| on success. |
| 90 | virtual bool Initialize() = 0; |
| 91 | |
crogers@google.com | 542a43a | 2013-07-31 05:16:49 +0000 | [diff] [blame] | 92 | // Implements the platform-specific details of sending MIDI data. |
toyoshim@chromium.org | ae6ad36 | 2013-08-27 15:30:20 +0000 | [diff] [blame^] | 93 | // This function runs on MIDISendThread. |
crogers@google.com | 542a43a | 2013-07-31 05:16:49 +0000 | [diff] [blame] | 94 | virtual void SendMIDIData(MIDIManagerClient* client, |
toyoshim@chromium.org | ae6ad36 | 2013-08-27 15:30:20 +0000 | [diff] [blame^] | 95 | uint32 port_index, |
| 96 | const std::vector<uint8>& data, |
crogers@google.com | 542a43a | 2013-07-31 05:16:49 +0000 | [diff] [blame] | 97 | double timestamp) = 0; |
| 98 | |
crogers@google.com | 27356e4 | 2013-06-22 04:03:03 +0000 | [diff] [blame] | 99 | void AddInputPort(const MIDIPortInfo& info); |
| 100 | void AddOutputPort(const MIDIPortInfo& info); |
| 101 | |
| 102 | // Dispatches to all clients. |
toyoshim@chromium.org | ae6ad36 | 2013-08-27 15:30:20 +0000 | [diff] [blame^] | 103 | void ReceiveMIDIData(uint32 port_index, |
| 104 | const uint8* data, |
| 105 | size_t length, |
| 106 | double timestamp); |
| 107 | |
| 108 | // Checks if current thread is MIDISendThread. |
| 109 | bool CurrentlyOnMIDISendThread(); |
crogers@google.com | 27356e4 | 2013-06-22 04:03:03 +0000 | [diff] [blame] | 110 | |
| 111 | bool initialized_; |
| 112 | |
| 113 | // Keeps track of all clients who wish to receive MIDI data. |
| 114 | typedef std::set<MIDIManagerClient*> ClientList; |
| 115 | ClientList clients_; |
| 116 | |
| 117 | // Protects access to our clients. |
| 118 | base::Lock clients_lock_; |
| 119 | |
| 120 | MIDIPortInfoList input_ports_; |
| 121 | MIDIPortInfoList output_ports_; |
| 122 | |
crogers@google.com | 542a43a | 2013-07-31 05:16:49 +0000 | [diff] [blame] | 123 | // |send_thread_| is used to send MIDI data by calling the platform-specific |
| 124 | // API. |
| 125 | scoped_ptr<base::Thread> send_thread_; |
| 126 | scoped_refptr<base::MessageLoopProxy> send_message_loop_; |
| 127 | |
crogers@google.com | 27356e4 | 2013-06-22 04:03:03 +0000 | [diff] [blame] | 128 | DISALLOW_COPY_AND_ASSIGN(MIDIManager); |
| 129 | }; |
| 130 | |
| 131 | } // namespace media |
| 132 | |
| 133 | #endif // MEDIA_MIDI_MIDI_MANAGER_H_ |