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