cc: Make picture pile base thread safe.
[chromium-blink-merge.git] / content / browser / media / midi_host.h
blob3a469bf93b9e2eef4ab6a2656f7e899877e15736
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_
8 #include <vector>
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"
20 namespace media {
21 class MidiManager;
22 class MidiMessageQueue;
25 namespace content {
27 class CONTENT_EXPORT MidiHost
28 : public BrowserMessageFilter,
29 public media::MidiManagerClient {
30 public:
31 // Called from UI thread from the owner of this object.
32 MidiHost(int renderer_process_id, media::MidiManager* midi_manager);
34 // BrowserMessageFilter implementation.
35 void OnDestruct() const override;
36 bool OnMessageReceived(const IPC::Message& message) override;
38 // MidiManagerClient implementation.
39 void CompleteStartSession(media::MidiResult result) override;
40 void AddInputPort(const media::MidiPortInfo& info) override;
41 void AddOutputPort(const media::MidiPortInfo& info) override;
42 void ReceiveMidiData(uint32 port,
43 const uint8* data,
44 size_t length,
45 double timestamp) override;
46 void AccumulateMidiBytesSent(size_t n) override;
48 // Start session to access MIDI hardware.
49 void OnStartSession();
51 // Data to be sent to a MIDI output port.
52 void OnSendData(uint32 port,
53 const std::vector<uint8>& data,
54 double timestamp);
56 void OnEndSession();
58 private:
59 FRIEND_TEST_ALL_PREFIXES(MidiHostTest, IsValidWebMIDIData);
60 friend class base::DeleteHelper<MidiHost>;
61 friend class BrowserThread;
63 ~MidiHost() override;
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);
72 int renderer_process_id_;
74 // Represents if the renderer has a permission to send/receive MIDI SysEX
75 // messages.
76 bool has_sys_ex_permission_;
78 // Represents if a session is requested to start.
79 bool is_session_requested_;
81 // |midi_manager_| talks to the platform-specific MIDI APIs.
82 // It can be NULL if the platform (or our current implementation)
83 // does not support MIDI. If not supported then a call to
84 // OnRequestAccess() will always refuse access and a call to
85 // OnSendData() will do nothing.
86 media::MidiManager* const midi_manager_;
88 // Buffers where data sent from each MIDI input port is stored.
89 ScopedVector<media::MidiMessageQueue> received_messages_queues_;
91 // Protects access to |received_messages_queues_|;
92 base::Lock messages_queues_lock_;
94 // The number of bytes sent to the platform-specific MIDI sending
95 // system, but not yet completed.
96 size_t sent_bytes_in_flight_;
98 // The number of bytes successfully sent since the last time
99 // we've acknowledged back to the renderer.
100 size_t bytes_sent_since_last_acknowledgement_;
102 // Protects access to |sent_bytes_in_flight_|.
103 base::Lock in_flight_lock_;
105 DISALLOW_COPY_AND_ASSIGN(MidiHost);
108 } // namespace content
110 #endif // CONTENT_BROWSER_MEDIA_MIDI_HOST_H_