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" |
toyoshim@chromium.org | c1e05fb | 2014-05-06 16:39:20 +0000 | [diff] [blame] | 12 | #include "base/memory/ref_counted.h" |
crogers@google.com | 27356e4 | 2013-06-22 04:03:03 +0000 | [diff] [blame] | 13 | #include "base/synchronization/lock.h" |
yhirano@chromium.org | cfa642c | 2014-05-01 08:54:41 +0000 | [diff] [blame] | 14 | #include "base/time/time.h" |
crogers@google.com | 27356e4 | 2013-06-22 04:03:03 +0000 | [diff] [blame] | 15 | #include "media/base/media_export.h" |
| 16 | #include "media/midi/midi_port_info.h" |
toyoshim@chromium.org | f77a1e6 | 2014-04-11 13:16:24 +0000 | [diff] [blame] | 17 | #include "media/midi/midi_result.h" |
crogers@google.com | 27356e4 | 2013-06-22 04:03:03 +0000 | [diff] [blame] | 18 | |
toyoshim@chromium.org | c1e05fb | 2014-05-06 16:39:20 +0000 | [diff] [blame] | 19 | namespace base { |
| 20 | class SingleThreadTaskRunner; |
| 21 | } // namespace base |
| 22 | |
crogers@google.com | 27356e4 | 2013-06-22 04:03:03 +0000 | [diff] [blame] | 23 | namespace media { |
| 24 | |
toyoshim@chromium.org | c82e66e | 2014-02-04 07:05:47 +0000 | [diff] [blame] | 25 | // A MidiManagerClient registers with the MidiManager to receive MIDI data. |
| 26 | // See MidiManager::RequestAccess() and MidiManager::ReleaseAccess() |
crogers@google.com | 27356e4 | 2013-06-22 04:03:03 +0000 | [diff] [blame] | 27 | // for details. |
toyoshim@chromium.org | c82e66e | 2014-02-04 07:05:47 +0000 | [diff] [blame] | 28 | class MEDIA_EXPORT MidiManagerClient { |
crogers@google.com | 27356e4 | 2013-06-22 04:03:03 +0000 | [diff] [blame] | 29 | public: |
toyoshim@chromium.org | f77a1e6 | 2014-04-11 13:16:24 +0000 | [diff] [blame] | 30 | virtual ~MidiManagerClient() {} |
| 31 | |
| 32 | // CompleteStartSession() is called when platform dependent preparation is |
| 33 | // finished. |
toyoshim | a485ff9 | 2014-10-23 00:17:59 -0700 | [diff] [blame^] | 34 | virtual void CompleteStartSession(MidiResult result) = 0; |
crogers@google.com | 27356e4 | 2013-06-22 04:03:03 +0000 | [diff] [blame] | 35 | |
toyoshim@chromium.org | c82e66e | 2014-02-04 07:05:47 +0000 | [diff] [blame] | 36 | // ReceiveMidiData() is called when MIDI data has been received from the |
crogers@google.com | 27356e4 | 2013-06-22 04:03:03 +0000 | [diff] [blame] | 37 | // MIDI system. |
| 38 | // |port_index| represents the specific input port from input_ports(). |
| 39 | // |data| represents a series of bytes encoding one or more MIDI messages. |
| 40 | // |length| is the number of bytes in |data|. |
| 41 | // |timestamp| is the time the data was received, in seconds. |
toyoshim@chromium.org | c82e66e | 2014-02-04 07:05:47 +0000 | [diff] [blame] | 42 | virtual void ReceiveMidiData(uint32 port_index, |
crogers@google.com | 27356e4 | 2013-06-22 04:03:03 +0000 | [diff] [blame] | 43 | const uint8* data, |
| 44 | size_t length, |
| 45 | double timestamp) = 0; |
crogers@google.com | 542a43a | 2013-07-31 05:16:49 +0000 | [diff] [blame] | 46 | |
toyoshim@chromium.org | c82e66e | 2014-02-04 07:05:47 +0000 | [diff] [blame] | 47 | // AccumulateMidiBytesSent() is called to acknowledge when bytes have |
crogers@google.com | 542a43a | 2013-07-31 05:16:49 +0000 | [diff] [blame] | 48 | // successfully been sent to the hardware. |
| 49 | // This happens as a result of the client having previously called |
toyoshim@chromium.org | c82e66e | 2014-02-04 07:05:47 +0000 | [diff] [blame] | 50 | // MidiManager::DispatchSendMidiData(). |
| 51 | virtual void AccumulateMidiBytesSent(size_t n) = 0; |
crogers@google.com | 27356e4 | 2013-06-22 04:03:03 +0000 | [diff] [blame] | 52 | }; |
| 53 | |
| 54 | // Manages access to all MIDI hardware. |
toyoshim@chromium.org | c82e66e | 2014-02-04 07:05:47 +0000 | [diff] [blame] | 55 | class MEDIA_EXPORT MidiManager { |
crogers@google.com | 27356e4 | 2013-06-22 04:03:03 +0000 | [diff] [blame] | 56 | public: |
toyoshim@chromium.org | fc2002e | 2014-05-07 08:10:34 +0000 | [diff] [blame] | 57 | static const size_t kMaxPendingClientCount = 128; |
crogers@google.com | 27356e4 | 2013-06-22 04:03:03 +0000 | [diff] [blame] | 58 | |
toyoshim@chromium.org | c82e66e | 2014-02-04 07:05:47 +0000 | [diff] [blame] | 59 | MidiManager(); |
| 60 | virtual ~MidiManager(); |
crogers@google.com | 27356e4 | 2013-06-22 04:03:03 +0000 | [diff] [blame] | 61 | |
toyoshim@chromium.org | fc2002e | 2014-05-07 08:10:34 +0000 | [diff] [blame] | 62 | // The constructor and the destructor will be called on the CrBrowserMain |
| 63 | // thread. |
| 64 | static MidiManager* Create(); |
| 65 | |
toyoshim@chromium.org | 235b09e | 2013-07-25 16:23:14 +0000 | [diff] [blame] | 66 | // A client calls StartSession() to receive and send MIDI data. |
| 67 | // 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] | 68 | // and the client is registered to receive MIDI data. |
toyoshim@chromium.org | f77a1e6 | 2014-04-11 13:16:24 +0000 | [diff] [blame] | 69 | // CompleteStartSession() is called with MIDI_OK if the session is started. |
| 70 | // Otherwise CompleteStartSession() is called with proper MidiResult code. |
toyoshim@chromium.org | c1e05fb | 2014-05-06 16:39:20 +0000 | [diff] [blame] | 71 | // StartSession() and EndSession() can be called on the Chrome_IOThread. |
| 72 | // CompleteStartSession() will be invoked on the same Chrome_IOThread. |
toyoshim | a485ff9 | 2014-10-23 00:17:59 -0700 | [diff] [blame^] | 73 | void StartSession(MidiManagerClient* client); |
crogers@google.com | 27356e4 | 2013-06-22 04:03:03 +0000 | [diff] [blame] | 74 | |
toyoshim@chromium.org | f77a1e6 | 2014-04-11 13:16:24 +0000 | [diff] [blame] | 75 | // A client calls EndSession() to stop receiving MIDI data. |
toyoshim@chromium.org | c82e66e | 2014-02-04 07:05:47 +0000 | [diff] [blame] | 76 | void EndSession(MidiManagerClient* client); |
crogers@google.com | 27356e4 | 2013-06-22 04:03:03 +0000 | [diff] [blame] | 77 | |
toyoshim@chromium.org | c82e66e | 2014-02-04 07:05:47 +0000 | [diff] [blame] | 78 | // DispatchSendMidiData() is called when MIDI data should be sent to the MIDI |
yukawa@chromium.org | db7ad8b | 2013-12-04 15:42:41 +0000 | [diff] [blame] | 79 | // system. |
| 80 | // This method is supposed to return immediately and should not block. |
crogers@google.com | 27356e4 | 2013-06-22 04:03:03 +0000 | [diff] [blame] | 81 | // |port_index| represents the specific output port from output_ports(). |
| 82 | // |data| represents a series of bytes encoding one or more MIDI messages. |
| 83 | // |length| is the number of bytes in |data|. |
crogers@google.com | 542a43a | 2013-07-31 05:16:49 +0000 | [diff] [blame] | 84 | // |timestamp| is the time to send the data, in seconds. A value of 0 |
| 85 | // means send "now" or as soon as possible. |
yhirano@chromium.org | c6d5b7b | 2013-12-20 07:27:23 +0000 | [diff] [blame] | 86 | // The default implementation is for unsupported platforms. |
toyoshim@chromium.org | c82e66e | 2014-02-04 07:05:47 +0000 | [diff] [blame] | 87 | virtual void DispatchSendMidiData(MidiManagerClient* client, |
yukawa@chromium.org | db7ad8b | 2013-12-04 15:42:41 +0000 | [diff] [blame] | 88 | uint32 port_index, |
| 89 | const std::vector<uint8>& data, |
yhirano@chromium.org | c6d5b7b | 2013-12-20 07:27:23 +0000 | [diff] [blame] | 90 | double timestamp); |
crogers@google.com | 27356e4 | 2013-06-22 04:03:03 +0000 | [diff] [blame] | 91 | |
| 92 | // input_ports() is a list of MIDI ports for receiving MIDI data. |
| 93 | // Each individual port in this list can be identified by its |
| 94 | // integer index into this list. |
toyoshim@chromium.org | a25e683 | 2014-05-07 18:06:25 +0000 | [diff] [blame] | 95 | const MidiPortInfoList& input_ports() const { return input_ports_; } |
crogers@google.com | 27356e4 | 2013-06-22 04:03:03 +0000 | [diff] [blame] | 96 | |
| 97 | // output_ports() is a list of MIDI ports for sending MIDI data. |
| 98 | // Each individual port in this list can be identified by its |
| 99 | // integer index into this list. |
toyoshim@chromium.org | a25e683 | 2014-05-07 18:06:25 +0000 | [diff] [blame] | 100 | const MidiPortInfoList& output_ports() const { return output_ports_; } |
crogers@google.com | 27356e4 | 2013-06-22 04:03:03 +0000 | [diff] [blame] | 101 | |
| 102 | protected: |
toyoshim@chromium.org | 51c7f53 | 2014-05-01 17:17:32 +0000 | [diff] [blame] | 103 | friend class MidiManagerUsb; |
| 104 | |
toyoshim@chromium.org | c1e05fb | 2014-05-06 16:39:20 +0000 | [diff] [blame] | 105 | // Initializes the platform dependent MIDI system. MidiManager class has a |
| 106 | // default implementation that synchronously calls CompleteInitialization() |
| 107 | // with MIDI_NOT_SUPPORTED on the caller thread. A derived class for a |
| 108 | // specific platform should override this method correctly. |
| 109 | // This method is called on Chrome_IOThread thread inside StartSession(). |
| 110 | // Platform dependent initialization can be processed synchronously or |
| 111 | // asynchronously. When the initialization is completed, |
| 112 | // CompleteInitialization() should be called with |result|. |
| 113 | // |result| should be MIDI_OK on success, otherwise a proper MidiResult. |
toyoshim@chromium.org | 51c7f53 | 2014-05-01 17:17:32 +0000 | [diff] [blame] | 114 | virtual void StartInitialization(); |
| 115 | |
| 116 | // Called from a platform dependent implementation of StartInitialization(). |
toyoshim@chromium.org | c1e05fb | 2014-05-06 16:39:20 +0000 | [diff] [blame] | 117 | // It invokes CompleteInitializationInternal() on the thread that calls |
| 118 | // StartSession() and distributes |result| to MIDIManagerClient objects in |
toyoshim@chromium.org | 51c7f53 | 2014-05-01 17:17:32 +0000 | [diff] [blame] | 119 | // |pending_clients_|. |
| 120 | void CompleteInitialization(MidiResult result); |
crogers@google.com | 27356e4 | 2013-06-22 04:03:03 +0000 | [diff] [blame] | 121 | |
toyoshim@chromium.org | c82e66e | 2014-02-04 07:05:47 +0000 | [diff] [blame] | 122 | void AddInputPort(const MidiPortInfo& info); |
| 123 | void AddOutputPort(const MidiPortInfo& info); |
crogers@google.com | 27356e4 | 2013-06-22 04:03:03 +0000 | [diff] [blame] | 124 | |
| 125 | // Dispatches to all clients. |
yhirano@chromium.org | cfa642c | 2014-05-01 08:54:41 +0000 | [diff] [blame] | 126 | // TODO(toyoshim): Fix the mac implementation to use |
| 127 | // |ReceiveMidiData(..., base::TimeTicks)|. |
toyoshim@chromium.org | c82e66e | 2014-02-04 07:05:47 +0000 | [diff] [blame] | 128 | void ReceiveMidiData(uint32 port_index, |
toyoshim@chromium.org | ae6ad36 | 2013-08-27 15:30:20 +0000 | [diff] [blame] | 129 | const uint8* data, |
| 130 | size_t length, |
| 131 | double timestamp); |
| 132 | |
yhirano@chromium.org | cfa642c | 2014-05-01 08:54:41 +0000 | [diff] [blame] | 133 | void ReceiveMidiData(uint32 port_index, |
| 134 | const uint8* data, |
| 135 | size_t length, |
| 136 | base::TimeTicks time) { |
| 137 | ReceiveMidiData(port_index, data, length, |
| 138 | (time - base::TimeTicks()).InSecondsF()); |
| 139 | } |
| 140 | |
toyoshim@chromium.org | a25e683 | 2014-05-07 18:06:25 +0000 | [diff] [blame] | 141 | size_t clients_size_for_testing() const { return clients_.size(); } |
| 142 | size_t pending_clients_size_for_testing() const { |
toyoshim@chromium.org | c1e05fb | 2014-05-06 16:39:20 +0000 | [diff] [blame] | 143 | return pending_clients_.size(); |
| 144 | } |
| 145 | |
toyoshim@chromium.org | c1e05fb | 2014-05-06 16:39:20 +0000 | [diff] [blame] | 146 | private: |
| 147 | void CompleteInitializationInternal(MidiResult result); |
crogers@google.com | 27356e4 | 2013-06-22 04:03:03 +0000 | [diff] [blame] | 148 | |
| 149 | // Keeps track of all clients who wish to receive MIDI data. |
toyoshim | a485ff9 | 2014-10-23 00:17:59 -0700 | [diff] [blame^] | 150 | typedef std::set<MidiManagerClient*> ClientSet; |
| 151 | ClientSet clients_; |
crogers@google.com | 27356e4 | 2013-06-22 04:03:03 +0000 | [diff] [blame] | 152 | |
toyoshim@chromium.org | 51c7f53 | 2014-05-01 17:17:32 +0000 | [diff] [blame] | 153 | // Keeps track of all clients who are waiting for CompleteStartSession(). |
toyoshim | a485ff9 | 2014-10-23 00:17:59 -0700 | [diff] [blame^] | 154 | ClientSet pending_clients_; |
toyoshim@chromium.org | 51c7f53 | 2014-05-01 17:17:32 +0000 | [diff] [blame] | 155 | |
toyoshim@chromium.org | c1e05fb | 2014-05-06 16:39:20 +0000 | [diff] [blame] | 156 | // Keeps a SingleThreadTaskRunner of the thread that calls StartSession in |
| 157 | // order to invoke CompleteStartSession() on the thread. |
| 158 | scoped_refptr<base::SingleThreadTaskRunner> session_thread_runner_; |
crogers@google.com | 27356e4 | 2013-06-22 04:03:03 +0000 | [diff] [blame] | 159 | |
toyoshim@chromium.org | c1e05fb | 2014-05-06 16:39:20 +0000 | [diff] [blame] | 160 | // Keeps true if platform dependent initialization is already completed. |
| 161 | bool initialized_; |
crogers@google.com | 27356e4 | 2013-06-22 04:03:03 +0000 | [diff] [blame] | 162 | |
toyoshim@chromium.org | c1e05fb | 2014-05-06 16:39:20 +0000 | [diff] [blame] | 163 | // Keeps the platform dependent initialization result if initialization is |
| 164 | // completed. Otherwise keeps MIDI_NOT_SUPPORTED. |
| 165 | MidiResult result_; |
| 166 | |
| 167 | // Protects access to |clients_|, |pending_clients_|, |initialized_|, and |
| 168 | // |result_|. |
| 169 | base::Lock lock_; |
| 170 | |
toyoshim@chromium.org | a25e683 | 2014-05-07 18:06:25 +0000 | [diff] [blame] | 171 | MidiPortInfoList input_ports_; |
| 172 | MidiPortInfoList output_ports_; |
| 173 | |
toyoshim@chromium.org | c82e66e | 2014-02-04 07:05:47 +0000 | [diff] [blame] | 174 | DISALLOW_COPY_AND_ASSIGN(MidiManager); |
crogers@google.com | 27356e4 | 2013-06-22 04:03:03 +0000 | [diff] [blame] | 175 | }; |
| 176 | |
| 177 | } // namespace media |
| 178 | |
| 179 | #endif // MEDIA_MIDI_MIDI_MANAGER_H_ |