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.
5 #ifndef CONTENT_BROWSER_MEDIA_MIDI_HOST_H_
6 #define CONTENT_BROWSER_MEDIA_MIDI_HOST_H_
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 "base/synchronization/lock.h"
15 #include "content/common/content_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"
19 #include "media/midi/midi_port_info.h"
24 class MidiMessageQueue
;
30 class CONTENT_EXPORT MidiHost
: public BrowserMessageFilter
,
31 public media::midi::MidiManagerClient
{
33 // Called from UI thread from the owner of this object.
34 MidiHost(int renderer_process_id
, media::midi::MidiManager
* midi_manager
);
36 // BrowserMessageFilter implementation.
37 void OnDestruct() const override
;
38 bool OnMessageReceived(const IPC::Message
& message
) override
;
40 // MidiManagerClient implementation.
41 void CompleteStartSession(media::midi::Result result
) override
;
42 void AddInputPort(const media::midi::MidiPortInfo
& info
) override
;
43 void AddOutputPort(const media::midi::MidiPortInfo
& info
) override
;
44 void SetInputPortState(uint32 port
,
45 media::midi::MidiPortState state
) override
;
46 void SetOutputPortState(uint32 port
,
47 media::midi::MidiPortState state
) override
;
48 void ReceiveMidiData(uint32 port
,
51 double timestamp
) override
;
52 void AccumulateMidiBytesSent(size_t n
) override
;
54 // Start session to access MIDI hardware.
55 void OnStartSession();
57 // Data to be sent to a MIDI output port.
58 void OnSendData(uint32 port
,
59 const std::vector
<uint8
>& data
,
68 FRIEND_TEST_ALL_PREFIXES(MidiHostTest
, IsValidWebMIDIData
);
69 friend class base::DeleteHelper
<MidiHost
>;
70 friend class BrowserThread
;
72 // Returns true if |data| fulfills the requirements of MidiOutput.send API
73 // defined in the WebMIDI spec.
74 // - |data| must be any number of complete MIDI messages (data abbreviation
75 // called "running status" is disallowed).
76 // - 1-byte MIDI realtime messages can be placed at any position of |data|.
77 static bool IsValidWebMIDIData(const std::vector
<uint8
>& data
);
79 int renderer_process_id_
;
81 // Represents if the renderer has a permission to send/receive MIDI SysEX
83 bool has_sys_ex_permission_
;
85 // Represents if a session is requested to start.
86 bool is_session_requested_
;
88 // |midi_manager_| talks to the platform-specific MIDI APIs.
89 // It can be NULL if the platform (or our current implementation)
90 // does not support MIDI. If not supported then a call to
91 // OnRequestAccess() will always refuse access and a call to
92 // OnSendData() will do nothing.
93 media::midi::MidiManager
* const midi_manager_
;
95 // Buffers where data sent from each MIDI input port is stored.
96 ScopedVector
<media::midi::MidiMessageQueue
> received_messages_queues_
;
98 // Protects access to |received_messages_queues_|;
99 base::Lock messages_queues_lock_
;
101 // The number of bytes sent to the platform-specific MIDI sending
102 // system, but not yet completed.
103 size_t sent_bytes_in_flight_
;
105 // The number of bytes successfully sent since the last time
106 // we've acknowledged back to the renderer.
107 size_t bytes_sent_since_last_acknowledgement_
;
109 // Protects access to |sent_bytes_in_flight_|.
110 base::Lock in_flight_lock_
;
112 // How many output port exists.
113 uint32 output_port_count_
;
115 // Protects access to |output_port_count_|.
116 base::Lock output_port_count_lock_
;
118 DISALLOW_COPY_AND_ASSIGN(MidiHost
);
121 } // namespace content
123 #endif // CONTENT_BROWSER_MEDIA_MIDI_HOST_H_