| OLD | NEW |
| 1 // Copyright (c) 2012 The Chromium Authors. All rights reserved. | 1 // Copyright (c) 2012 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/renderer_host/media/audio_sync_reader.h" | 5 #include "content/browser/renderer_host/media/audio_sync_reader.h" |
| 6 | 6 |
| 7 #include <algorithm> | 7 #include <algorithm> |
| 8 #include <string> | 8 #include <string> |
| 9 #include <utility> | 9 #include <utility> |
| 10 | 10 |
| 11 #include "base/command_line.h" | 11 #include "base/command_line.h" |
| 12 #include "base/format_macros.h" | 12 #include "base/format_macros.h" |
| 13 #include "base/memory/ptr_util.h" | 13 #include "base/memory/ptr_util.h" |
| 14 #include "base/memory/shared_memory.h" | 14 #include "base/memory/shared_memory.h" |
| 15 #include "base/metrics/histogram_macros.h" | 15 #include "base/metrics/histogram_macros.h" |
| 16 #include "base/strings/stringprintf.h" | 16 #include "base/strings/stringprintf.h" |
| 17 #include "base/trace_event/trace_event.h" | 17 #include "base/trace_event/trace_event.h" |
| 18 #include "build/build_config.h" | 18 #include "build/build_config.h" |
| 19 #include "content/browser/renderer_host/media/media_stream_manager.h" | 19 #include "content/browser/renderer_host/media/media_stream_manager.h" |
| 20 #include "content/public/common/content_switches.h" | 20 #include "content/public/common/content_switches.h" |
| 21 #include "media/audio/audio_device_thread.h" | |
| 22 #include "media/base/audio_parameters.h" | 21 #include "media/base/audio_parameters.h" |
| 23 | 22 |
| 24 using media::AudioBus; | 23 using media::AudioBus; |
| 25 using media::AudioOutputBuffer; | 24 using media::AudioOutputBuffer; |
| 26 using Packet = media::AudioDeviceThread::Packet; | |
| 27 | 25 |
| 28 namespace { | 26 namespace { |
| 29 | 27 |
| 30 // Used to log if any audio glitches have been detected during an audio session. | 28 // Used to log if any audio glitches have been detected during an audio session. |
| 31 // Elements in this enum should not be added, deleted or rearranged. | 29 // Elements in this enum should not be added, deleted or rearranged. |
| 32 enum AudioGlitchResult { | 30 enum AudioGlitchResult { |
| 33 AUDIO_RENDERER_NO_AUDIO_GLITCHES = 0, | 31 AUDIO_RENDERER_NO_AUDIO_GLITCHES = 0, |
| 34 AUDIO_RENDERER_AUDIO_GLITCHES = 1, | 32 AUDIO_RENDERER_AUDIO_GLITCHES = 1, |
| 35 AUDIO_RENDERER_AUDIO_GLITCHES_MAX = AUDIO_RENDERER_AUDIO_GLITCHES | 33 AUDIO_RENDERER_AUDIO_GLITCHES_MAX = AUDIO_RENDERER_AUDIO_GLITCHES |
| 36 }; | 34 }; |
| (...skipping 98 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 135 !base::CancelableSyncSocket::CreatePair(socket.get(), | 133 !base::CancelableSyncSocket::CreatePair(socket.get(), |
| 136 foreign_socket.get())) { | 134 foreign_socket.get())) { |
| 137 return nullptr; | 135 return nullptr; |
| 138 } | 136 } |
| 139 return base::WrapUnique(new AudioSyncReader(params, std::move(shared_memory), | 137 return base::WrapUnique(new AudioSyncReader(params, std::move(shared_memory), |
| 140 std::move(socket), | 138 std::move(socket), |
| 141 std::move(foreign_socket))); | 139 std::move(foreign_socket))); |
| 142 } | 140 } |
| 143 | 141 |
| 144 // media::AudioOutputController::SyncReader implementations. | 142 // media::AudioOutputController::SyncReader implementations. |
| 145 void AudioSyncReader::RequestMoreData(base::TimeDelta delay, | 143 void AudioSyncReader::UpdatePendingBytes(uint32_t bytes, |
| 146 base::TimeTicks delay_timestamp, | 144 uint32_t frames_skipped) { |
| 147 int prior_frames_skipped) { | |
| 148 // Increase the number of skipped frames stored in shared memory. We don't | 145 // Increase the number of skipped frames stored in shared memory. We don't |
| 149 // send it over the socket since sending more than 4 bytes might lead to being | 146 // send it over the socket since sending more than 4 bytes might lead to being |
| 150 // descheduled. The reading side will zero it when consumed. | 147 // descheduled. The reading side will zero it when consumed. |
| 151 AudioOutputBuffer* buffer = | 148 AudioOutputBuffer* buffer = |
| 152 reinterpret_cast<AudioOutputBuffer*>(shared_memory_->memory()); | 149 reinterpret_cast<AudioOutputBuffer*>(shared_memory_->memory()); |
| 153 buffer->params.frames_skipped += prior_frames_skipped; | 150 buffer->params.frames_skipped += frames_skipped; |
| 154 | 151 |
| 155 // Zero out the entire output buffer to avoid stuttering/repeating-buffers | 152 // Zero out the entire output buffer to avoid stuttering/repeating-buffers |
| 156 // in the anomalous case if the renderer is unable to keep up with real-time. | 153 // in the anomalous case if the renderer is unable to keep up with real-time. |
| 157 output_bus_->Zero(); | 154 output_bus_->Zero(); |
| 158 | 155 |
| 159 Packet packet = {delay.InMicroseconds(), | 156 socket_->Send(&bytes, sizeof(bytes)); |
| 160 (delay_timestamp - base::TimeTicks()).InMicroseconds()}; | |
| 161 socket_->Send(&packet, sizeof(packet)); | |
| 162 ++buffer_index_; | 157 ++buffer_index_; |
| 163 } | 158 } |
| 164 | 159 |
| 165 void AudioSyncReader::Read(AudioBus* dest) { | 160 void AudioSyncReader::Read(AudioBus* dest) { |
| 166 ++renderer_callback_count_; | 161 ++renderer_callback_count_; |
| 167 if (!WaitUntilDataIsReady()) { | 162 if (!WaitUntilDataIsReady()) { |
| 168 ++trailing_renderer_missed_callback_count_; | 163 ++trailing_renderer_missed_callback_count_; |
| 169 ++renderer_missed_callback_count_; | 164 ++renderer_missed_callback_count_; |
| 170 if (renderer_missed_callback_count_ <= 100) { | 165 if (renderer_missed_callback_count_ <= 100) { |
| 171 LOG(WARNING) << "AudioSyncReader::Read timed out, audio glitch count=" | 166 LOG(WARNING) << "AudioSyncReader::Read timed out, audio glitch count=" |
| (...skipping 65 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 237 base::TimeDelta::FromMilliseconds(1), | 232 base::TimeDelta::FromMilliseconds(1), |
| 238 base::TimeDelta::FromMilliseconds(1000), | 233 base::TimeDelta::FromMilliseconds(1000), |
| 239 50); | 234 50); |
| 240 return false; | 235 return false; |
| 241 } | 236 } |
| 242 | 237 |
| 243 return true; | 238 return true; |
| 244 } | 239 } |
| 245 | 240 |
| 246 } // namespace content | 241 } // namespace content |
| OLD | NEW |