| 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/metrics/histogram_macros.h" | 9 #include "base/metrics/histogram_macros.h" |
| 10 #include "base/process/process.h" | 10 #include "base/process/process.h" |
| 11 #include "base/trace_event/trace_event.h" | 11 #include "base/trace_event/trace_event.h" |
| 12 #include "content/browser/bad_message.h" | 12 #include "content/browser/bad_message.h" |
| 13 #include "content/browser/browser_main_loop.h" | 13 #include "content/browser/browser_main_loop.h" |
| 14 #include "content/browser/child_process_security_policy_impl.h" | 14 #include "content/browser/child_process_security_policy_impl.h" |
| 15 #include "content/browser/media/media_internals.h" | |
| 16 #include "content/common/media/midi_messages.h" | 15 #include "content/common/media/midi_messages.h" |
| 17 #include "content/public/browser/content_browser_client.h" | 16 #include "content/public/browser/content_browser_client.h" |
| 18 #include "content/public/browser/media_observer.h" | |
| 19 #include "content/public/browser/user_metrics.h" | 17 #include "content/public/browser/user_metrics.h" |
| 20 #include "media/midi/midi_manager.h" | 18 #include "media/midi/midi_manager.h" |
| 21 #include "media/midi/midi_message_queue.h" | 19 #include "media/midi/midi_message_queue.h" |
| 22 #include "media/midi/midi_message_util.h" | 20 #include "media/midi/midi_message_util.h" |
| 23 | 21 |
| 24 namespace content { | 22 namespace content { |
| 25 namespace { | 23 namespace { |
| 26 | 24 |
| 27 // 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 |
| 28 // before knowing that they have been successfully sent. | 26 // before knowing that they have been successfully sent. |
| 29 const size_t kMaxInFlightBytes = 10 * 1024 * 1024; // 10 MB. | 27 const size_t kMaxInFlightBytes = 10 * 1024 * 1024; // 10 MB. |
| 30 | 28 |
| 31 // We keep track of the number of bytes successfully sent to | 29 // We keep track of the number of bytes successfully sent to |
| 32 // 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 |
| 33 // 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 |
| 34 // 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. |
| 35 const size_t kAcknowledgementThresholdBytes = 1024 * 1024; // 1 MB. | 33 const size_t kAcknowledgementThresholdBytes = 1024 * 1024; // 1 MB. |
| 36 | 34 |
| 37 bool IsDataByte(uint8_t data) { | 35 bool IsDataByte(uint8_t data) { |
| 38 return (data & 0x80) == 0; | 36 return (data & 0x80) == 0; |
| 39 } | 37 } |
| 40 | 38 |
| 41 bool IsSystemRealTimeMessage(uint8_t data) { | 39 bool IsSystemRealTimeMessage(uint8_t data) { |
| 42 return 0xf8 <= data && data <= 0xff; | 40 return 0xf8 <= data && data <= 0xff; |
| 43 } | 41 } |
| 44 | 42 |
| 45 } // namespace | 43 } // namespace |
| 46 | 44 |
| 47 using media::midi::kSysExByte; | 45 using midi::kSysExByte; |
| 48 using media::midi::kEndOfSysExByte; | 46 using midi::kEndOfSysExByte; |
| 49 | 47 |
| 50 MidiHost::MidiHost(int renderer_process_id, | 48 MidiHost::MidiHost(int renderer_process_id, |
| 51 media::midi::MidiManager* midi_manager) | 49 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 DCHECK(midi_manager_); | 58 DCHECK(midi_manager_); |
| 61 } | 59 } |
| (...skipping 74 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 136 if (midi_manager_) | 134 if (midi_manager_) |
| 137 midi_manager_->DispatchSendMidiData(this, port, data, timestamp); | 135 midi_manager_->DispatchSendMidiData(this, port, data, timestamp); |
| 138 } | 136 } |
| 139 | 137 |
| 140 void MidiHost::OnEndSession() { | 138 void MidiHost::OnEndSession() { |
| 141 is_session_requested_ = false; | 139 is_session_requested_ = false; |
| 142 if (midi_manager_) | 140 if (midi_manager_) |
| 143 midi_manager_->EndSession(this); | 141 midi_manager_->EndSession(this); |
| 144 } | 142 } |
| 145 | 143 |
| 146 void MidiHost::CompleteStartSession(media::midi::Result result) { | 144 void MidiHost::CompleteStartSession(midi::Result result) { |
| 147 DCHECK(is_session_requested_); | 145 DCHECK(is_session_requested_); |
| 148 if (result == media::midi::Result::OK) { | 146 if (result == midi::Result::OK) { |
| 149 // ChildSecurityPolicy is set just before OnStartSession by | 147 // ChildSecurityPolicy is set just before OnStartSession by |
| 150 // MidiDispatcherHost. So we can safely cache the policy. | 148 // MidiDispatcherHost. So we can safely cache the policy. |
| 151 has_sys_ex_permission_ = ChildProcessSecurityPolicyImpl::GetInstance()-> | 149 has_sys_ex_permission_ = ChildProcessSecurityPolicyImpl::GetInstance()-> |
| 152 CanSendMidiSysExMessage(renderer_process_id_); | 150 CanSendMidiSysExMessage(renderer_process_id_); |
| 153 } | 151 } |
| 154 Send(new MidiMsg_SessionStarted(result)); | 152 Send(new MidiMsg_SessionStarted(result)); |
| 155 } | 153 } |
| 156 | 154 |
| 157 void MidiHost::AddInputPort(const media::midi::MidiPortInfo& info) { | 155 void MidiHost::AddInputPort(const midi::MidiPortInfo& info) { |
| 158 base::AutoLock auto_lock(messages_queues_lock_); | 156 base::AutoLock auto_lock(messages_queues_lock_); |
| 159 // MidiMessageQueue is created later in ReceiveMidiData(). | 157 // MidiMessageQueue is created later in ReceiveMidiData(). |
| 160 received_messages_queues_.push_back(nullptr); | 158 received_messages_queues_.push_back(nullptr); |
| 161 Send(new MidiMsg_AddInputPort(info)); | 159 Send(new MidiMsg_AddInputPort(info)); |
| 162 } | 160 } |
| 163 | 161 |
| 164 void MidiHost::AddOutputPort(const media::midi::MidiPortInfo& info) { | 162 void MidiHost::AddOutputPort(const midi::MidiPortInfo& info) { |
| 165 base::AutoLock auto_lock(output_port_count_lock_); | 163 base::AutoLock auto_lock(output_port_count_lock_); |
| 166 output_port_count_++; | 164 output_port_count_++; |
| 167 Send(new MidiMsg_AddOutputPort(info)); | 165 Send(new MidiMsg_AddOutputPort(info)); |
| 168 } | 166 } |
| 169 | 167 |
| 170 void MidiHost::SetInputPortState(uint32_t port, | 168 void MidiHost::SetInputPortState(uint32_t port, |
| 171 media::midi::MidiPortState state) { | 169 midi::MidiPortState state) { |
| 172 Send(new MidiMsg_SetInputPortState(port, state)); | 170 Send(new MidiMsg_SetInputPortState(port, state)); |
| 173 } | 171 } |
| 174 | 172 |
| 175 void MidiHost::SetOutputPortState(uint32_t port, | 173 void MidiHost::SetOutputPortState(uint32_t port, |
| 176 media::midi::MidiPortState state) { | 174 midi::MidiPortState state) { |
| 177 Send(new MidiMsg_SetOutputPortState(port, state)); | 175 Send(new MidiMsg_SetOutputPortState(port, state)); |
| 178 } | 176 } |
| 179 | 177 |
| 180 void MidiHost::ReceiveMidiData(uint32_t port, | 178 void MidiHost::ReceiveMidiData(uint32_t port, |
| 181 const uint8_t* data, | 179 const uint8_t* data, |
| 182 size_t length, | 180 size_t length, |
| 183 double timestamp) { | 181 double timestamp) { |
| 184 TRACE_EVENT0("midi", "MidiHost::ReceiveMidiData"); | 182 TRACE_EVENT0("midi", "MidiHost::ReceiveMidiData"); |
| 185 | 183 |
| 186 base::AutoLock auto_lock(messages_queues_lock_); | 184 base::AutoLock auto_lock(messages_queues_lock_); |
| 187 if (received_messages_queues_.size() <= port) | 185 if (received_messages_queues_.size() <= port) |
| 188 return; | 186 return; |
| 189 | 187 |
| 190 // Lazy initialization | 188 // Lazy initialization |
| 191 if (received_messages_queues_[port] == nullptr) | 189 if (received_messages_queues_[port] == nullptr) |
| 192 received_messages_queues_[port] = new media::midi::MidiMessageQueue(true); | 190 received_messages_queues_[port] = new midi::MidiMessageQueue(true); |
| 193 | 191 |
| 194 received_messages_queues_[port]->Add(data, length); | 192 received_messages_queues_[port]->Add(data, length); |
| 195 std::vector<uint8_t> message; | 193 std::vector<uint8_t> message; |
| 196 while (true) { | 194 while (true) { |
| 197 received_messages_queues_[port]->Get(&message); | 195 received_messages_queues_[port]->Get(&message); |
| 198 if (message.empty()) | 196 if (message.empty()) |
| 199 break; | 197 break; |
| 200 | 198 |
| 201 // MIDI devices may send a system exclusive messages even if the renderer | 199 // MIDI devices may send a system exclusive messages even if the renderer |
| 202 // doesn't have a permission to receive it. Don't kill the renderer as | 200 // doesn't have a permission to receive it. Don't kill the renderer as |
| (...skipping 52 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 255 } else if (!IsDataByte(current)) { | 253 } else if (!IsDataByte(current)) { |
| 256 return false; // Error: |current| should have been data byte. | 254 return false; // Error: |current| should have been data byte. |
| 257 } | 255 } |
| 258 continue; // Found data byte as expected. | 256 continue; // Found data byte as expected. |
| 259 } | 257 } |
| 260 if (current == kSysExByte) { | 258 if (current == kSysExByte) { |
| 261 in_sysex = true; | 259 in_sysex = true; |
| 262 sysex_start_offset = i; | 260 sysex_start_offset = i; |
| 263 continue; // Found SysEX | 261 continue; // Found SysEX |
| 264 } | 262 } |
| 265 waiting_data_length = media::midi::GetMidiMessageLength(current); | 263 waiting_data_length = midi::GetMidiMessageLength(current); |
| 266 if (waiting_data_length == 0) | 264 if (waiting_data_length == 0) |
| 267 return false; // Error: |current| should have been a valid status byte. | 265 return false; // Error: |current| should have been a valid status byte. |
| 268 --waiting_data_length; // Found status byte | 266 --waiting_data_length; // Found status byte |
| 269 } | 267 } |
| 270 return waiting_data_length == 0 && !in_sysex; | 268 return waiting_data_length == 0 && !in_sysex; |
| 271 } | 269 } |
| 272 | 270 |
| 273 } // namespace content | 271 } // namespace content |
| OLD | NEW |