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