OLD | NEW |
1 // Copyright (c) 2013 The Chromium Authors. All rights reserved. | 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 | 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_RENDERER_MEDIA_MIDI_MESSAGE_FILTER_H_ | 5 #ifndef CONTENT_RENDERER_MEDIA_MIDI_MESSAGE_FILTER_H_ |
6 #define CONTENT_RENDERER_MEDIA_MIDI_MESSAGE_FILTER_H_ | 6 #define CONTENT_RENDERER_MEDIA_MIDI_MESSAGE_FILTER_H_ |
7 | 7 |
8 #include <map> | 8 #include <set> |
9 #include <vector> | 9 #include <vector> |
10 | 10 |
11 #include "base/memory/scoped_ptr.h" | 11 #include "base/memory/scoped_ptr.h" |
| 12 #include "base/synchronization/lock.h" |
12 #include "content/common/content_export.h" | 13 #include "content/common/content_export.h" |
13 #include "ipc/message_filter.h" | 14 #include "ipc/message_filter.h" |
14 #include "media/midi/midi_port_info.h" | 15 #include "media/midi/midi_port_info.h" |
15 #include "media/midi/midi_result.h" | 16 #include "media/midi/midi_result.h" |
16 #include "third_party/WebKit/public/platform/WebMIDIAccessorClient.h" | 17 #include "third_party/WebKit/public/platform/WebMIDIAccessorClient.h" |
17 | 18 |
18 namespace base { | 19 namespace base { |
19 class MessageLoopProxy; | 20 class MessageLoopProxy; |
20 } | 21 } |
21 | 22 |
22 namespace content { | 23 namespace content { |
23 | 24 |
24 // MessageFilter that handles MIDI messages. | 25 // MessageFilter that handles MIDI messages. |
25 class CONTENT_EXPORT MidiMessageFilter : public IPC::MessageFilter { | 26 class CONTENT_EXPORT MidiMessageFilter : public IPC::MessageFilter { |
26 public: | 27 public: |
27 explicit MidiMessageFilter( | 28 explicit MidiMessageFilter( |
28 const scoped_refptr<base::MessageLoopProxy>& io_message_loop); | 29 const scoped_refptr<base::MessageLoopProxy>& io_message_loop); |
29 | 30 |
30 // Each client registers for MIDI access here. | 31 // Each client registers for MIDI access here. |
31 // If permission is granted, then the client's | 32 // If permission is granted, then the client's |
32 // addInputPort() and addOutputPort() methods will be called, | 33 void AddClient(blink::WebMIDIAccessorClient* client); |
33 // giving the client access to receive and send data. | |
34 void StartSession(blink::WebMIDIAccessorClient* client); | |
35 void RemoveClient(blink::WebMIDIAccessorClient* client); | 34 void RemoveClient(blink::WebMIDIAccessorClient* client); |
36 | 35 |
37 // A client will only be able to call this method if it has a suitable | 36 // A client will only be able to call this method if it has a suitable |
38 // output port (from addOutputPort()). | 37 // output port (from addOutputPort()). |
39 void SendMidiData(uint32 port, | 38 void SendMidiData(uint32 port, |
40 const uint8* data, | 39 const uint8* data, |
41 size_t length, | 40 size_t length, |
42 double timestamp); | 41 double timestamp); |
43 | 42 |
44 // IO message loop associated with this message filter. | 43 // IO message loop associated with this message filter. |
45 scoped_refptr<base::MessageLoopProxy> io_message_loop() const { | 44 scoped_refptr<base::MessageLoopProxy> io_message_loop() const { |
46 return io_message_loop_; | 45 return io_message_loop_; |
47 } | 46 } |
48 | 47 |
49 protected: | 48 protected: |
50 virtual ~MidiMessageFilter(); | 49 virtual ~MidiMessageFilter(); |
51 | 50 |
52 private: | 51 private: |
| 52 void StartSessionOnIOThread(); |
| 53 |
| 54 void SendMidiDataOnIOThread(uint32 port, |
| 55 const std::vector<uint8>& data, |
| 56 double timestamp); |
| 57 |
| 58 void EndSessionOnIOThread(); |
| 59 |
53 // Sends an IPC message using |sender_|. | 60 // Sends an IPC message using |sender_|. |
54 void Send(IPC::Message* message); | 61 void Send(IPC::Message* message); |
55 | 62 |
56 // IPC::MessageFilter override. Called on |io_message_loop|. | 63 // IPC::MessageFilter override. Called on |io_message_loop|. |
57 virtual bool OnMessageReceived(const IPC::Message& message) override; | 64 virtual bool OnMessageReceived(const IPC::Message& message) override; |
58 virtual void OnFilterAdded(IPC::Sender* sender) override; | 65 virtual void OnFilterAdded(IPC::Sender* sender) override; |
59 virtual void OnFilterRemoved() override; | 66 virtual void OnFilterRemoved() override; |
60 virtual void OnChannelClosing() override; | 67 virtual void OnChannelClosing() override; |
61 | 68 |
62 // Called when the browser process has approved (or denied) access to | 69 // Called when the browser process has approved (or denied) access to |
63 // MIDI hardware. | 70 // MIDI hardware. |
64 void OnSessionStarted(int client_id, | 71 // TODO(toyoshim): MidiPortInfoList objects should be notified separately |
65 media::MidiResult result, | 72 // port by port. |
| 73 void OnSessionStarted(media::MidiResult result, |
66 media::MidiPortInfoList inputs, | 74 media::MidiPortInfoList inputs, |
67 media::MidiPortInfoList outputs); | 75 media::MidiPortInfoList outputs); |
68 | 76 |
69 // Called when the browser process has sent MIDI data containing one or | 77 // Called when the browser process has sent MIDI data containing one or |
70 // more messages. | 78 // more messages. |
71 void OnDataReceived(uint32 port, | 79 void OnDataReceived(uint32 port, |
72 const std::vector<uint8>& data, | 80 const std::vector<uint8>& data, |
73 double timestamp); | 81 double timestamp); |
74 | 82 |
75 // From time-to-time, the browser incrementally informs us of how many bytes | 83 // From time-to-time, the browser incrementally informs us of how many bytes |
76 // it has successfully sent. This is part of our throttling process to avoid | 84 // it has successfully sent. This is part of our throttling process to avoid |
77 // sending too much data before knowing how much has already been sent. | 85 // sending too much data before knowing how much has already been sent. |
78 void OnAcknowledgeSentData(size_t bytes_sent); | 86 void OnAcknowledgeSentData(size_t bytes_sent); |
79 | 87 |
80 void HandleSessionStarted(int client_id, | 88 void HandleClientAdded(blink::WebMIDIAccessorClient* client); |
81 media::MidiResult result, | |
82 media::MidiPortInfoList inputs, | |
83 media::MidiPortInfoList outputs); | |
84 | 89 |
85 void HandleDataReceived(uint32 port, | 90 void HandleDataReceived(uint32 port, |
86 const std::vector<uint8>& data, | 91 const std::vector<uint8>& data, |
87 double timestamp); | 92 double timestamp); |
88 | 93 |
89 void StartSessionOnIOThread(int client_id); | |
90 | |
91 void SendMidiDataOnIOThread(uint32 port, | |
92 const std::vector<uint8>& data, | |
93 double timestamp); | |
94 | |
95 blink::WebMIDIAccessorClient* GetClientFromId(int client_id); | |
96 | |
97 // IPC sender for Send(); must only be accessed on |io_message_loop_|. | 94 // IPC sender for Send(); must only be accessed on |io_message_loop_|. |
98 IPC::Sender* sender_; | 95 IPC::Sender* sender_; |
99 | 96 |
100 // Message loop on which IPC calls are driven. | 97 // Message loop on which IPC calls are driven. |
101 const scoped_refptr<base::MessageLoopProxy> io_message_loop_; | 98 const scoped_refptr<base::MessageLoopProxy> io_message_loop_; |
102 | 99 |
103 // Main thread's message loop. | 100 // Main thread's message loop. |
104 scoped_refptr<base::MessageLoopProxy> main_message_loop_; | 101 scoped_refptr<base::MessageLoopProxy> main_message_loop_; |
105 | 102 |
| 103 // Protects access to |clients_|, |clients_waiting_session_|, and |
| 104 // |session_results_|. |
| 105 base::Lock lock_; |
| 106 |
106 // Keeps track of all MIDI clients. | 107 // Keeps track of all MIDI clients. |
107 // We map client to "client id" used to track permission. | 108 typedef std::set<blink::WebMIDIAccessorClient*> ClientsSet; |
108 // When access has been approved, we add the input and output ports to | 109 ClientsSet clients_; |
109 // the client, allowing it to actually receive and send MIDI data. | |
110 typedef std::map<blink::WebMIDIAccessorClient*, int> ClientsMap; | |
111 ClientsMap clients_; | |
112 | 110 |
113 // Dishes out client ids. | 111 // Represents clients that are waiting for a session being open. |
114 int next_available_id_; | 112 typedef std::vector<blink::WebMIDIAccessorClient*> ClientsQueue; |
| 113 ClientsQueue clients_waiting_session_queue_; |
| 114 |
| 115 // Represents a result on starting a session. |
| 116 media::MidiResult session_result_; |
| 117 |
| 118 // Holds MidiPortInfoList for input ports and output ports. |
| 119 media::MidiPortInfoList inputs_; |
| 120 media::MidiPortInfoList outputs_; |
115 | 121 |
116 size_t unacknowledged_bytes_sent_; | 122 size_t unacknowledged_bytes_sent_; |
117 | 123 |
118 DISALLOW_COPY_AND_ASSIGN(MidiMessageFilter); | 124 DISALLOW_COPY_AND_ASSIGN(MidiMessageFilter); |
119 }; | 125 }; |
120 | 126 |
121 } // namespace content | 127 } // namespace content |
122 | 128 |
123 #endif // CONTENT_RENDERER_MEDIA_MIDI_MESSAGE_FILTER_H_ | 129 #endif // CONTENT_RENDERER_MEDIA_MIDI_MESSAGE_FILTER_H_ |
OLD | NEW |