| OLD | NEW |
| 1 // Copyright (c) 2013 The Chromium Authors. All rights reserved. | 1 // Copyright 2014 The Chromium Authors. All rights reserved. |
| 2 // Use of this source code is governed by a BSD-style license that can be | 2 // Use of this source code is governed by a BSD-style license that can be |
| 3 // found in the LICENSE file. | 3 // found in the LICENSE file. |
| 4 | 4 |
| 5 #ifndef CONTENT_BROWSER_MEDIA_MIDI_HOST_H_ | 5 #ifndef MIDI_SERVICE_IMPL_H_ |
| 6 #define CONTENT_BROWSER_MEDIA_MIDI_HOST_H_ | 6 #define MIDI_SERVICE_IMPL_H_ |
| 7 | 7 |
| 8 #include <vector> | 8 #include "base/macros.h" |
| 9 | |
| 10 #include "base/gtest_prod_util.h" | |
| 11 #include "base/memory/ref_counted.h" | |
| 12 #include "base/memory/scoped_ptr.h" | |
| 13 #include "base/memory/scoped_vector.h" | 9 #include "base/memory/scoped_vector.h" |
| 14 #include "base/synchronization/lock.h" | 10 #include "base/synchronization/lock.h" |
| 15 #include "content/common/content_export.h" | 11 #include "media/base/media_export.h" |
| 16 #include "content/public/browser/browser_message_filter.h" | |
| 17 #include "content/public/browser/browser_thread.h" | |
| 18 #include "media/midi/midi_manager.h" | 12 #include "media/midi/midi_manager.h" |
| 13 #include "media/midi/midi_message_queue.h" |
| 14 #include "media/midi/midi_service.mojom.h" |
| 15 #include "mojo/public/cpp/bindings/strong_binding.h" |
| 19 | 16 |
| 20 namespace media { | 17 namespace media { |
| 21 class MidiManager; | |
| 22 class MidiMessageQueue; | |
| 23 } | |
| 24 | 18 |
| 25 namespace content { | 19 class MidiServiceImpl : public MidiService, |
| 20 public MidiManagerClient { |
| 21 public: |
| 22 MEDIA_EXPORT static void Create( |
| 23 int renderer_process_id, |
| 24 MidiManager* midi_manager, |
| 25 mojo::InterfaceRequest<MidiService> request); |
| 26 | 26 |
| 27 class CONTENT_EXPORT MidiHost | 27 private: |
| 28 : public BrowserMessageFilter, | 28 typedef mojo::Callback<void(MidiResultMojo)> StartSessionCallback; |
| 29 public media::MidiManagerClient { | 29 typedef mojo::Callback<void(uint32_t)> SendDataCallback; |
| 30 public: | |
| 31 // Called from UI thread from the owner of this object. | |
| 32 MidiHost(int renderer_process_id, media::MidiManager* midi_manager); | |
| 33 | 30 |
| 34 // BrowserMessageFilter implementation. | 31 explicit MidiServiceImpl( |
| 35 void OnDestruct() const override; | 32 int renderer_process_id, |
| 36 bool OnMessageReceived(const IPC::Message& message) override; | 33 MidiManager* midi_manager, |
| 34 mojo::InterfaceRequest<MidiService> request); |
| 35 ~MidiServiceImpl() override; |
| 36 |
| 37 // Returns true if |data| fulfills the requirements of MidiOutput.send API |
| 38 // defined in the WebMIDI spec. |
| 39 // - |data| must be any number of complete MIDI messages (data abbreviation |
| 40 // called "running status" is disallowed). |
| 41 // - 1-byte MIDI realtime messages can be placed at any position of |data|. |
| 42 static bool IsValidWebMIDIData(const std::vector<uint8_t>& data); |
| 43 |
| 44 // MidiService methods. |
| 45 void StartSession( |
| 46 MidiServiceClientPtr client, |
| 47 const StartSessionCallback& callback) override; |
| 48 void EndSession() override; |
| 49 void SendData(uint32_t port, |
| 50 mojo::Array<uint8_t> data, |
| 51 double timestamp) override; |
| 37 | 52 |
| 38 // MidiManagerClient implementation. | 53 // MidiManagerClient implementation. |
| 39 void CompleteStartSession(media::MidiResult result) override; | 54 void CompleteStartSession(media::MidiResult result) override; |
| 40 void AddInputPort(const media::MidiPortInfo& info) override; | 55 void AddInputPort(const media::MidiPortInfo& info) override; |
| 41 void AddOutputPort(const media::MidiPortInfo& info) override; | 56 void AddOutputPort(const media::MidiPortInfo& info) override; |
| 42 void ReceiveMidiData(uint32 port, | 57 void ReceiveMidiData(uint32 port, |
| 43 const uint8* data, | 58 const uint8* data, |
| 44 size_t length, | 59 size_t length, |
| 45 double timestamp) override; | 60 double timestamp) override; |
| 46 void AccumulateMidiBytesSent(size_t n) override; | 61 void AccumulateMidiBytesSent(size_t n) override; |
| 47 | 62 |
| 48 // Start session to access MIDI hardware. | 63 mojo::StrongBinding<MidiService> binding_; |
| 49 void OnStartSession(); | 64 MidiServiceClientPtr client_; |
| 50 | 65 StartSessionCallback start_session_callback_; |
| 51 // Data to be sent to a MIDI output port. | |
| 52 void OnSendData(uint32 port, | |
| 53 const std::vector<uint8>& data, | |
| 54 double timestamp); | |
| 55 | |
| 56 void OnEndSession(); | |
| 57 | |
| 58 private: | |
| 59 FRIEND_TEST_ALL_PREFIXES(MidiHostTest, IsValidWebMIDIData); | |
| 60 friend class base::DeleteHelper<MidiHost>; | |
| 61 friend class BrowserThread; | |
| 62 | |
| 63 ~MidiHost() override; | |
| 64 | |
| 65 // Returns true if |data| fulfills the requirements of MidiOutput.send API | |
| 66 // defined in the WebMIDI spec. | |
| 67 // - |data| must be any number of complete MIDI messages (data abbreviation | |
| 68 // called "running status" is disallowed). | |
| 69 // - 1-byte MIDI realtime messages can be placed at any position of |data|. | |
| 70 static bool IsValidWebMIDIData(const std::vector<uint8>& data); | |
| 71 | 66 |
| 72 int renderer_process_id_; | 67 int renderer_process_id_; |
| 73 | 68 |
| 74 // Represents if the renderer has a permission to send/receive MIDI SysEX | 69 // Represents if the renderer has a permission to send/receive MIDI SysEX |
| 75 // messages. | 70 // messages. |
| 76 bool has_sys_ex_permission_; | 71 bool has_sys_ex_permission_ = false; |
| 77 | 72 |
| 78 // Represents if a session is requested to start. | 73 // Represents if a session is requested to start. |
| 79 bool is_session_requested_; | 74 bool is_session_requested_ = false; |
| 80 | 75 |
| 81 // |midi_manager_| talks to the platform-specific MIDI APIs. | 76 // |midi_manager_| talks to the platform-specific MIDI APIs. |
| 82 // It can be NULL if the platform (or our current implementation) | 77 // It can be NULL if the platform (or our current implementation) |
| 83 // does not support MIDI. If not supported then a call to | 78 // does not support MIDI. If not supported then a call to |
| 84 // OnRequestAccess() will always refuse access and a call to | 79 // OnRequestAccess() will always refuse access and a call to |
| 85 // OnSendData() will do nothing. | 80 // OnSendData() will do nothing. |
| 86 media::MidiManager* const midi_manager_; | 81 MidiManager* const midi_manager_ = nullptr; |
| 87 | 82 |
| 88 // Buffers where data sent from each MIDI input port is stored. | 83 // Buffers where data sent from each MIDI input port is stored. |
| 89 ScopedVector<media::MidiMessageQueue> received_messages_queues_; | 84 ScopedVector<MidiMessageQueue> received_messages_queues_; |
| 90 | 85 |
| 91 // Protects access to |received_messages_queues_|; | 86 // Protects access to |received_messages_queues_|; |
| 92 base::Lock messages_queues_lock_; | 87 base::Lock messages_queues_lock_; |
| 93 | 88 |
| 94 // The number of bytes sent to the platform-specific MIDI sending | 89 // The number of bytes sent to the platform-specific MIDI sending |
| 95 // system, but not yet completed. | 90 // system, but not yet completed. |
| 96 size_t sent_bytes_in_flight_; | 91 size_t sent_bytes_in_flight_ = 0; |
| 97 | 92 |
| 98 // The number of bytes successfully sent since the last time | 93 // The number of bytes successfully sent since the last time |
| 99 // we've acknowledged back to the renderer. | 94 // we've acknowledged back to the renderer. |
| 100 size_t bytes_sent_since_last_acknowledgement_; | 95 size_t bytes_sent_since_last_acknowledgement_ = 0; |
| 101 | 96 |
| 102 // Protects access to |sent_bytes_in_flight_|. | 97 // Protects access to |sent_bytes_in_flight_|. |
| 103 base::Lock in_flight_lock_; | 98 base::Lock in_flight_lock_; |
| 104 | 99 |
| 105 DISALLOW_COPY_AND_ASSIGN(MidiHost); | 100 DISALLOW_COPY_AND_ASSIGN(MidiServiceImpl); |
| 106 }; | 101 }; |
| 107 | 102 |
| 108 } // namespace content | 103 } // namespace media |
| 109 | 104 |
| 110 #endif // CONTENT_BROWSER_MEDIA_MIDI_HOST_H_ | 105 #endif // MIDI_SERVICE_IMPL_H_ |
| OLD | NEW |