| 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 #include "content/browser/media/midi_host.h" | 5 #include "content/browser/media/midi_host.h" |
| 6 | 6 |
| 7 #include "base/bind.h" | 7 #include "base/bind.h" |
| 8 #include "base/bind_helpers.h" | 8 #include "base/bind_helpers.h" |
| 9 #include "base/process/process.h" | 9 #include "base/process/process.h" |
| 10 #include "base/trace_event/trace_event.h" | 10 #include "base/trace_event/trace_event.h" |
| 11 #include "content/browser/browser_main_loop.h" | 11 #include "content/browser/browser_main_loop.h" |
| 12 #include "content/browser/child_process_security_policy_impl.h" | 12 #include "content/browser/child_process_security_policy_impl.h" |
| 13 #include "content/browser/media/media_internals.h" | 13 #include "content/browser/media/media_internals.h" |
| 14 #include "content/common/media/midi_messages.h" | 14 #include "content/common/media/midi_messages.h" |
| 15 #include "content/public/browser/content_browser_client.h" | 15 #include "content/public/browser/content_browser_client.h" |
| 16 #include "content/public/browser/media_observer.h" | 16 #include "content/public/browser/media_observer.h" |
| 17 #include "content/public/browser/user_metrics.h" | 17 #include "content/public/browser/user_metrics.h" |
| 18 #include "media/midi/midi_manager.h" | 18 #include "media/midi/midi_manager.h" |
| 19 #include "media/midi/midi_message_queue.h" | 19 #include "media/midi/midi_message_queue.h" |
| 20 #include "media/midi/midi_message_util.h" | 20 #include "media/midi/midi_message_util.h" |
| 21 | 21 |
| 22 using media::MidiManager; | |
| 23 using media::MidiPortInfoList; | |
| 24 | |
| 25 namespace content { | 22 namespace content { |
| 26 namespace { | 23 namespace { |
| 27 | 24 |
| 28 // The total number of bytes which we're allowed to send to the OS | 25 // The total number of bytes which we're allowed to send to the OS |
| 29 // before knowing that they have been successfully sent. | 26 // before knowing that they have been successfully sent. |
| 30 const size_t kMaxInFlightBytes = 10 * 1024 * 1024; // 10 MB. | 27 const size_t kMaxInFlightBytes = 10 * 1024 * 1024; // 10 MB. |
| 31 | 28 |
| 32 // We keep track of the number of bytes successfully sent to | 29 // We keep track of the number of bytes successfully sent to |
| 33 // the hardware. Every once in a while we report back to the renderer | 30 // the hardware. Every once in a while we report back to the renderer |
| 34 // the number of bytes sent since the last report. This threshold determines | 31 // the number of bytes sent since the last report. This threshold determines |
| 35 // how many bytes will be sent before reporting back to the renderer. | 32 // how many bytes will be sent before reporting back to the renderer. |
| 36 const size_t kAcknowledgementThresholdBytes = 1024 * 1024; // 1 MB. | 33 const size_t kAcknowledgementThresholdBytes = 1024 * 1024; // 1 MB. |
| 37 | 34 |
| 38 bool IsDataByte(uint8 data) { | 35 bool IsDataByte(uint8 data) { |
| 39 return (data & 0x80) == 0; | 36 return (data & 0x80) == 0; |
| 40 } | 37 } |
| 41 | 38 |
| 42 bool IsSystemRealTimeMessage(uint8 data) { | 39 bool IsSystemRealTimeMessage(uint8 data) { |
| 43 return 0xf8 <= data && data <= 0xff; | 40 return 0xf8 <= data && data <= 0xff; |
| 44 } | 41 } |
| 45 | 42 |
| 46 } // namespace | 43 } // namespace |
| 47 | 44 |
| 48 using media::kSysExByte; | 45 using media::midi::kSysExByte; |
| 49 using media::kEndOfSysExByte; | 46 using media::midi::kEndOfSysExByte; |
| 50 | 47 |
| 51 MidiHost::MidiHost(int renderer_process_id, media::MidiManager* midi_manager) | 48 MidiHost::MidiHost(int renderer_process_id, |
| 49 media::midi::MidiManager* midi_manager) |
| 52 : BrowserMessageFilter(MidiMsgStart), | 50 : BrowserMessageFilter(MidiMsgStart), |
| 53 renderer_process_id_(renderer_process_id), | 51 renderer_process_id_(renderer_process_id), |
| 54 has_sys_ex_permission_(false), | 52 has_sys_ex_permission_(false), |
| 55 is_session_requested_(false), | 53 is_session_requested_(false), |
| 56 midi_manager_(midi_manager), | 54 midi_manager_(midi_manager), |
| 57 sent_bytes_in_flight_(0), | 55 sent_bytes_in_flight_(0), |
| 58 bytes_sent_since_last_acknowledgement_(0), | 56 bytes_sent_since_last_acknowledgement_(0), |
| 59 output_port_count_(0) { | 57 output_port_count_(0) { |
| 60 CHECK(midi_manager_); | 58 CHECK(midi_manager_); |
| 61 } | 59 } |
| (...skipping 64 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 126 sent_bytes_in_flight_ += data.size(); | 124 sent_bytes_in_flight_ += data.size(); |
| 127 } | 125 } |
| 128 midi_manager_->DispatchSendMidiData(this, port, data, timestamp); | 126 midi_manager_->DispatchSendMidiData(this, port, data, timestamp); |
| 129 } | 127 } |
| 130 | 128 |
| 131 void MidiHost::OnEndSession() { | 129 void MidiHost::OnEndSession() { |
| 132 is_session_requested_ = false; | 130 is_session_requested_ = false; |
| 133 midi_manager_->EndSession(this); | 131 midi_manager_->EndSession(this); |
| 134 } | 132 } |
| 135 | 133 |
| 136 void MidiHost::CompleteStartSession(media::MidiResult result) { | 134 void MidiHost::CompleteStartSession(media::midi::MidiResult result) { |
| 137 DCHECK(is_session_requested_); | 135 DCHECK(is_session_requested_); |
| 138 if (result == media::MIDI_OK) { | 136 if (result == media::midi::MIDI_OK) { |
| 139 // ChildSecurityPolicy is set just before OnStartSession by | 137 // ChildSecurityPolicy is set just before OnStartSession by |
| 140 // MidiDispatcherHost. So we can safely cache the policy. | 138 // MidiDispatcherHost. So we can safely cache the policy. |
| 141 has_sys_ex_permission_ = ChildProcessSecurityPolicyImpl::GetInstance()-> | 139 has_sys_ex_permission_ = ChildProcessSecurityPolicyImpl::GetInstance()-> |
| 142 CanSendMidiSysExMessage(renderer_process_id_); | 140 CanSendMidiSysExMessage(renderer_process_id_); |
| 143 } | 141 } |
| 144 Send(new MidiMsg_SessionStarted(result)); | 142 Send(new MidiMsg_SessionStarted(result)); |
| 145 } | 143 } |
| 146 | 144 |
| 147 void MidiHost::AddInputPort(const media::MidiPortInfo& info) { | 145 void MidiHost::AddInputPort(const media::midi::MidiPortInfo& info) { |
| 148 base::AutoLock auto_lock(messages_queues_lock_); | 146 base::AutoLock auto_lock(messages_queues_lock_); |
| 149 // MidiMessageQueue is created later in ReceiveMidiData(). | 147 // MidiMessageQueue is created later in ReceiveMidiData(). |
| 150 received_messages_queues_.push_back(nullptr); | 148 received_messages_queues_.push_back(nullptr); |
| 151 Send(new MidiMsg_AddInputPort(info)); | 149 Send(new MidiMsg_AddInputPort(info)); |
| 152 } | 150 } |
| 153 | 151 |
| 154 void MidiHost::AddOutputPort(const media::MidiPortInfo& info) { | 152 void MidiHost::AddOutputPort(const media::midi::MidiPortInfo& info) { |
| 155 base::AutoLock auto_lock(output_port_count_lock_); | 153 base::AutoLock auto_lock(output_port_count_lock_); |
| 156 output_port_count_++; | 154 output_port_count_++; |
| 157 Send(new MidiMsg_AddOutputPort(info)); | 155 Send(new MidiMsg_AddOutputPort(info)); |
| 158 } | 156 } |
| 159 | 157 |
| 160 void MidiHost::SetInputPortState(uint32 port, media::MidiPortState state) { | 158 void MidiHost::SetInputPortState(uint32 port, |
| 159 media::midi::MidiPortState state) { |
| 161 Send(new MidiMsg_SetInputPortState(port, state)); | 160 Send(new MidiMsg_SetInputPortState(port, state)); |
| 162 } | 161 } |
| 163 | 162 |
| 164 void MidiHost::SetOutputPortState(uint32 port, media::MidiPortState state) { | 163 void MidiHost::SetOutputPortState(uint32 port, |
| 164 media::midi::MidiPortState state) { |
| 165 Send(new MidiMsg_SetOutputPortState(port, state)); | 165 Send(new MidiMsg_SetOutputPortState(port, state)); |
| 166 } | 166 } |
| 167 | 167 |
| 168 void MidiHost::ReceiveMidiData( | 168 void MidiHost::ReceiveMidiData( |
| 169 uint32 port, | 169 uint32 port, |
| 170 const uint8* data, | 170 const uint8* data, |
| 171 size_t length, | 171 size_t length, |
| 172 double timestamp) { | 172 double timestamp) { |
| 173 TRACE_EVENT0("midi", "MidiHost::ReceiveMidiData"); | 173 TRACE_EVENT0("midi", "MidiHost::ReceiveMidiData"); |
| 174 | 174 |
| 175 base::AutoLock auto_lock(messages_queues_lock_); | 175 base::AutoLock auto_lock(messages_queues_lock_); |
| 176 if (received_messages_queues_.size() <= port) | 176 if (received_messages_queues_.size() <= port) |
| 177 return; | 177 return; |
| 178 | 178 |
| 179 // Lazy initialization | 179 // Lazy initialization |
| 180 if (received_messages_queues_[port] == nullptr) | 180 if (received_messages_queues_[port] == nullptr) |
| 181 received_messages_queues_[port] = new media::MidiMessageQueue(true); | 181 received_messages_queues_[port] = new media::midi::MidiMessageQueue(true); |
| 182 | 182 |
| 183 received_messages_queues_[port]->Add(data, length); | 183 received_messages_queues_[port]->Add(data, length); |
| 184 std::vector<uint8> message; | 184 std::vector<uint8> message; |
| 185 while (true) { | 185 while (true) { |
| 186 received_messages_queues_[port]->Get(&message); | 186 received_messages_queues_[port]->Get(&message); |
| 187 if (message.empty()) | 187 if (message.empty()) |
| 188 break; | 188 break; |
| 189 | 189 |
| 190 // MIDI devices may send a system exclusive messages even if the renderer | 190 // MIDI devices may send a system exclusive messages even if the renderer |
| 191 // doesn't have a permission to receive it. Don't kill the renderer as | 191 // doesn't have a permission to receive it. Don't kill the renderer as |
| (...skipping 43 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 235 if (data[i] == kEndOfSysExByte) | 235 if (data[i] == kEndOfSysExByte) |
| 236 in_sysex = false; | 236 in_sysex = false; |
| 237 else if (!IsDataByte(current)) | 237 else if (!IsDataByte(current)) |
| 238 return false; // Error: |current| should have been data byte. | 238 return false; // Error: |current| should have been data byte. |
| 239 continue; // Found data byte as expected. | 239 continue; // Found data byte as expected. |
| 240 } | 240 } |
| 241 if (current == kSysExByte) { | 241 if (current == kSysExByte) { |
| 242 in_sysex = true; | 242 in_sysex = true; |
| 243 continue; // Found SysEX | 243 continue; // Found SysEX |
| 244 } | 244 } |
| 245 waiting_data_length = media::GetMidiMessageLength(current); | 245 waiting_data_length = media::midi::GetMidiMessageLength(current); |
| 246 if (waiting_data_length == 0) | 246 if (waiting_data_length == 0) |
| 247 return false; // Error: |current| should have been a valid status byte. | 247 return false; // Error: |current| should have been a valid status byte. |
| 248 --waiting_data_length; // Found status byte | 248 --waiting_data_length; // Found status byte |
| 249 } | 249 } |
| 250 return waiting_data_length == 0 && !in_sysex; | 250 return waiting_data_length == 0 && !in_sysex; |
| 251 } | 251 } |
| 252 | 252 |
| 253 } // namespace content | 253 } // namespace content |
| OLD | NEW |