midi_manager.h 8.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218
  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. #ifndef MEDIA_MIDI_MIDI_MANAGER_H_
  5. #define MEDIA_MIDI_MIDI_MANAGER_H_
  6. #include <stddef.h>
  7. #include <stdint.h>
  8. #include <set>
  9. #include <vector>
  10. #include "base/memory/raw_ptr.h"
  11. #include "base/memory/ref_counted.h"
  12. #include "base/synchronization/lock.h"
  13. #include "base/thread_annotations.h"
  14. #include "base/time/time.h"
  15. #include "media/midi/midi_export.h"
  16. #include "media/midi/midi_service.mojom.h"
  17. namespace base {
  18. class SingleThreadTaskRunner;
  19. } // namespace base
  20. namespace midi {
  21. class MidiService;
  22. // A MidiManagerClient registers with the MidiManager to receive MIDI data.
  23. // See MidiManager::RequestAccess() and MidiManager::ReleaseAccess()
  24. // for details.
  25. // TODO(toyoshim): Consider to have a MidiServiceClient interface.
  26. class MIDI_EXPORT MidiManagerClient {
  27. public:
  28. virtual ~MidiManagerClient() {}
  29. // AddInputPort() and AddOutputPort() are called before CompleteStartSession()
  30. // is called to notify existing MIDI ports, and also called after that to
  31. // notify new MIDI ports are added.
  32. virtual void AddInputPort(const mojom::PortInfo& info) = 0;
  33. virtual void AddOutputPort(const mojom::PortInfo& info) = 0;
  34. // SetInputPortState() and SetOutputPortState() are called to notify a known
  35. // device gets disconnected, or connected again.
  36. virtual void SetInputPortState(uint32_t port_index,
  37. mojom::PortState state) = 0;
  38. virtual void SetOutputPortState(uint32_t port_index,
  39. mojom::PortState state) = 0;
  40. // CompleteStartSession() is called when platform dependent preparation is
  41. // finished.
  42. virtual void CompleteStartSession(mojom::Result result) = 0;
  43. // ReceiveMidiData() is called when MIDI data has been received from the
  44. // MIDI system.
  45. // |port_index| represents the specific input port from input_ports().
  46. // |data| represents a series of bytes encoding one or more MIDI messages.
  47. // |length| is the number of bytes in |data|.
  48. // |timestamp| is the time the data was received, in seconds.
  49. virtual void ReceiveMidiData(uint32_t port_index,
  50. const uint8_t* data,
  51. size_t length,
  52. base::TimeTicks timestamp) = 0;
  53. // AccumulateMidiBytesSent() is called to acknowledge when bytes have
  54. // successfully been sent to the hardware.
  55. // This happens as a result of the client having previously called
  56. // MidiManager::DispatchSendMidiData().
  57. virtual void AccumulateMidiBytesSent(size_t n) = 0;
  58. // Detach() is called when MidiManager is going to shutdown immediately.
  59. // Client should not touch MidiManager instance after Detach() is called.
  60. virtual void Detach() = 0;
  61. };
  62. // Manages access to all MIDI hardware. MidiManager runs on the I/O thread.
  63. //
  64. // Note: We will eventually remove utility functions that are shared among
  65. // platform dependent MidiManager inheritances such as MidiManagerClient
  66. // management. MidiService should provide such shareable utility functions as
  67. // it does TaskService.
  68. class MIDI_EXPORT MidiManager {
  69. public:
  70. static const size_t kMaxPendingClientCount = 128;
  71. explicit MidiManager(MidiService* service);
  72. MidiManager(const MidiManager&) = delete;
  73. MidiManager& operator=(const MidiManager&) = delete;
  74. virtual ~MidiManager();
  75. static MidiManager* Create(MidiService* service);
  76. // A client calls StartSession() to receive and send MIDI data.
  77. // If the session is ready to start, the MIDI system is lazily initialized
  78. // and the client is registered to receive MIDI data.
  79. // CompleteStartSession() is called with mojom::Result::OK if the session is
  80. // started. Otherwise CompleteStartSession() is called with a proper
  81. // mojom::Result code.
  82. void StartSession(MidiManagerClient* client);
  83. // A client calls EndSession() to stop receiving MIDI data.
  84. // Returns false if |client| did not start a session.
  85. bool EndSession(MidiManagerClient* client);
  86. // Returns true if there is at least one client that keep a session open.
  87. bool HasOpenSession();
  88. // DispatchSendMidiData() is called when MIDI data should be sent to the MIDI
  89. // system.
  90. // This method is supposed to return immediately and should not block.
  91. // |port_index| represents the specific output port from output_ports().
  92. // |data| represents a series of bytes encoding one or more MIDI messages.
  93. // |length| is the number of bytes in |data|.
  94. // |timestamp| is the time to send the data. A value of 0 means send "now" or
  95. // as soon as possible. The default implementation is for unsupported
  96. // platforms.
  97. virtual void DispatchSendMidiData(MidiManagerClient* client,
  98. uint32_t port_index,
  99. const std::vector<uint8_t>& data,
  100. base::TimeTicks timestamp);
  101. // This method ends all sessions by detaching and removing all registered
  102. // clients. This method can be called from any thread.
  103. void EndAllSessions();
  104. protected:
  105. friend class MidiManagerUsb;
  106. // Initializes the platform dependent MIDI system. MidiManager class has a
  107. // default implementation that synchronously calls CompleteInitialization()
  108. // with mojom::Result::NOT_SUPPORTED. A derived class for a specific platform
  109. // should override this method correctly.
  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 mojom::Result::OK on success, otherwise a proper
  114. // mojom::Result.
  115. virtual void StartInitialization();
  116. // Called from a platform dependent implementation of StartInitialization().
  117. // The method distributes |result| to MIDIManagerClient objects in
  118. // |pending_clients_|.
  119. void CompleteInitialization(mojom::Result result);
  120. // The following five methods can be called on any thread to notify clients of
  121. // status changes on ports, or to obtain port status.
  122. void AddInputPort(const mojom::PortInfo& info);
  123. void AddOutputPort(const mojom::PortInfo& info);
  124. void SetInputPortState(uint32_t port_index, mojom::PortState state);
  125. void SetOutputPortState(uint32_t port_index, mojom::PortState state);
  126. mojom::PortState GetOutputPortState(uint32_t port_index);
  127. // Invoke AccumulateMidiBytesSent() for |client| safely. If the session was
  128. // already closed, do nothing. Can be called on any thread.
  129. void AccumulateMidiBytesSent(MidiManagerClient* client, size_t n);
  130. // Dispatches to all clients. Can be called on any thread.
  131. void ReceiveMidiData(uint32_t port_index,
  132. const uint8_t* data,
  133. size_t length,
  134. base::TimeTicks time);
  135. // Only for testing.
  136. size_t GetClientCountForTesting();
  137. size_t GetPendingClientCountForTesting();
  138. MidiService* service() { return service_; }
  139. private:
  140. enum class InitializationState {
  141. NOT_STARTED,
  142. STARTED,
  143. COMPLETED,
  144. };
  145. // Note: Members that are not protected by any lock should be accessed only on
  146. // the I/O thread.
  147. // Tracks platform dependent initialization state.
  148. InitializationState initialization_state_ = InitializationState::NOT_STARTED;
  149. // Keeps the platform dependent initialization result if initialization is
  150. // completed. Otherwise keeps mojom::Result::NOT_INITIALIZED.
  151. mojom::Result result_ = mojom::Result::NOT_INITIALIZED;
  152. // Keeps track of all clients who are waiting for CompleteStartSession().
  153. std::set<MidiManagerClient*> pending_clients_ GUARDED_BY(lock_);
  154. // Keeps track of all clients who wish to receive MIDI data.
  155. std::set<MidiManagerClient*> clients_ GUARDED_BY(lock_);
  156. // Keeps a SingleThreadTaskRunner of the thread that calls StartSession in
  157. // order to invoke CompleteStartSession() on the thread. This is touched only
  158. // on the IO thread usually, but to be guarded by |lock_| for thread checks.
  159. scoped_refptr<base::SingleThreadTaskRunner> session_thread_runner_
  160. GUARDED_BY(lock_);
  161. // Keeps all PortInfo.
  162. std::vector<mojom::PortInfo> input_ports_ GUARDED_BY(lock_);
  163. std::vector<mojom::PortInfo> output_ports_ GUARDED_BY(lock_);
  164. // Tracks if actual data transmission happens.
  165. bool data_sent_ GUARDED_BY(lock_) = false;
  166. bool data_received_ GUARDED_BY(lock_) = false;
  167. // Protects members above.
  168. base::Lock lock_;
  169. // MidiService outlives MidiManager.
  170. const raw_ptr<MidiService> service_;
  171. };
  172. } // namespace midi
  173. #endif // MEDIA_MIDI_MIDI_MANAGER_H_