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