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 CONTENT_BROWSER_RENDERER_HOST_MEDIA_MIDI_HOST_H_ 6 #define CONTENT_BROWSER_RENDERER_HOST_MEDIA_MIDI_HOST_H_ 7 8 #include <vector> 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" 14 #include "content/common/content_export.h" 15 #include "content/public/browser/browser_message_filter.h" 16 #include "content/public/browser/browser_thread.h" 17 #include "media/midi/midi_manager.h" 18 19 namespace media { 20 class MIDIManager; 21 class MIDIMessageQueue; 22 } 23 24 namespace content { 25 26 class CONTENT_EXPORT MIDIHost 27 : public BrowserMessageFilter, 28 public media::MIDIManagerClient { 29 public: 30 // Called from UI thread from the owner of this object. 31 MIDIHost(int renderer_process_id, media::MIDIManager* midi_manager); 32 33 // BrowserMessageFilter implementation. 34 virtual void OnDestruct() const OVERRIDE; 35 virtual bool OnMessageReceived(const IPC::Message& message, 36 bool* message_was_ok) OVERRIDE; 37 38 // MIDIManagerClient implementation. 39 virtual void ReceiveMIDIData(uint32 port, 40 const uint8* data, 41 size_t length, 42 double timestamp) OVERRIDE; 43 virtual void AccumulateMIDIBytesSent(size_t n) OVERRIDE; 44 45 // Start session to access MIDI hardware. 46 void OnStartSession(int client_id); 47 48 // Data to be sent to a MIDI output port. 49 void OnSendData(uint32 port, 50 const std::vector<uint8>& data, 51 double timestamp); 52 53 private: 54 FRIEND_TEST_ALL_PREFIXES(MIDIHostTest, IsValidWebMIDIData); 55 friend class base::DeleteHelper<MIDIHost>; 56 friend class BrowserThread; 57 58 virtual ~MIDIHost(); 59 60 // Returns true if |data| fulfills the requirements of MIDIOutput.send API 61 // defined in the WebMIDI spec. 62 // - |data| must be any number of complete MIDI messages (data abbreviation 63 // called "running status" is disallowed). 64 // - 1-byte MIDI realtime messages can be placed at any position of |data|. 65 static bool IsValidWebMIDIData(const std::vector<uint8>& data); 66 67 int renderer_process_id_; 68 69 // Represents if the renderer has a permission to send/receive MIDI SysEX 70 // messages. 71 bool has_sys_ex_permission_; 72 73 // |midi_manager_| talks to the platform-specific MIDI APIs. 74 // It can be NULL if the platform (or our current implementation) 75 // does not support MIDI. If not supported then a call to 76 // OnRequestAccess() will always refuse access and a call to 77 // OnSendData() will do nothing. 78 media::MIDIManager* const midi_manager_; 79 80 // Buffers where data sent from each MIDI input port is stored. 81 ScopedVector<media::MIDIMessageQueue> received_messages_queues_; 82 83 // The number of bytes sent to the platform-specific MIDI sending 84 // system, but not yet completed. 85 size_t sent_bytes_in_flight_; 86 87 // The number of bytes successfully sent since the last time 88 // we've acknowledged back to the renderer. 89 size_t bytes_sent_since_last_acknowledgement_; 90 91 // Protects access to |sent_bytes_in_flight_|. 92 base::Lock in_flight_lock_; 93 94 DISALLOW_COPY_AND_ASSIGN(MIDIHost); 95 }; 96 97 } // namespace content 98 99 #endif // CONTENT_BROWSER_RENDERER_HOST_MEDIA_MIDI_HOST_H_ 100