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 | 8 |
9 #include "base/command_line.h" | 9 #include "base/command_line.h" |
10 #include "base/memory/shared_memory.h" | 10 #include "base/memory/shared_memory.h" |
11 #include "base/metrics/histogram.h" | 11 #include "base/metrics/histogram.h" |
12 #include "base/strings/stringprintf.h" | 12 #include "base/strings/stringprintf.h" |
13 #include "content/browser/renderer_host/media/media_stream_manager.h" | 13 #include "content/browser/renderer_host/media/media_stream_manager.h" |
14 #include "content/public/common/content_switches.h" | 14 #include "content/public/common/content_switches.h" |
15 #include "media/audio/audio_parameters.h" | 15 #include "media/audio/audio_parameters.h" |
16 | 16 |
17 using media::AudioBus; | 17 using media::AudioBus; |
| 18 using media::AudioOutputBuffer; |
18 | 19 |
19 namespace { | 20 namespace { |
20 | 21 |
21 // Used to log if any audio glitches have been detected during an audio session. | 22 // Used to log if any audio glitches have been detected during an audio session. |
22 // Elements in this enum should not be added, deleted or rearranged. | 23 // Elements in this enum should not be added, deleted or rearranged. |
23 enum AudioGlitchResult { | 24 enum AudioGlitchResult { |
24 AUDIO_RENDERER_NO_AUDIO_GLITCHES = 0, | 25 AUDIO_RENDERER_NO_AUDIO_GLITCHES = 0, |
25 AUDIO_RENDERER_AUDIO_GLITCHES = 1, | 26 AUDIO_RENDERER_AUDIO_GLITCHES = 1, |
26 AUDIO_RENDERER_AUDIO_GLITCHES_MAX = AUDIO_RENDERER_AUDIO_GLITCHES | 27 AUDIO_RENDERER_AUDIO_GLITCHES_MAX = AUDIO_RENDERER_AUDIO_GLITCHES |
27 }; | 28 }; |
(...skipping 16 matching lines...) Expand all Loading... |
44 packet_size_(shared_memory_->requested_size()), | 45 packet_size_(shared_memory_->requested_size()), |
45 renderer_callback_count_(0), | 46 renderer_callback_count_(0), |
46 renderer_missed_callback_count_(0), | 47 renderer_missed_callback_count_(0), |
47 #if defined(OS_MACOSX) | 48 #if defined(OS_MACOSX) |
48 maximum_wait_time_(params.GetBufferDuration() / 2), | 49 maximum_wait_time_(params.GetBufferDuration() / 2), |
49 #else | 50 #else |
50 // TODO(dalecurtis): Investigate if we can reduce this on all platforms. | 51 // TODO(dalecurtis): Investigate if we can reduce this on all platforms. |
51 maximum_wait_time_(base::TimeDelta::FromMilliseconds(20)), | 52 maximum_wait_time_(base::TimeDelta::FromMilliseconds(20)), |
52 #endif | 53 #endif |
53 buffer_index_(0) { | 54 buffer_index_(0) { |
54 DCHECK_EQ(packet_size_, AudioBus::CalculateMemorySize(params)); | 55 DCHECK_EQ(static_cast<size_t>(packet_size_), |
55 output_bus_ = AudioBus::WrapMemory(params, shared_memory->memory()); | 56 sizeof(media::AudioOutputBufferParameters) + |
| 57 AudioBus::CalculateMemorySize(params)); |
| 58 AudioOutputBuffer* buffer = |
| 59 reinterpret_cast<AudioOutputBuffer*>(shared_memory_->memory()); |
| 60 output_bus_ = AudioBus::WrapMemory(params, buffer->audio); |
56 output_bus_->Zero(); | 61 output_bus_->Zero(); |
57 } | 62 } |
58 | 63 |
59 AudioSyncReader::~AudioSyncReader() { | 64 AudioSyncReader::~AudioSyncReader() { |
60 if (!renderer_callback_count_) | 65 if (!renderer_callback_count_) |
61 return; | 66 return; |
62 | 67 |
63 // Recording the percentage of deadline misses gives us a rough overview of | 68 // Recording the percentage of deadline misses gives us a rough overview of |
64 // how many users might be running into audio glitches. | 69 // how many users might be running into audio glitches. |
65 int percentage_missed = | 70 int percentage_missed = |
66 100.0 * renderer_missed_callback_count_ / renderer_callback_count_; | 71 100.0 * renderer_missed_callback_count_ / renderer_callback_count_; |
67 UMA_HISTOGRAM_PERCENTAGE( | 72 UMA_HISTOGRAM_PERCENTAGE( |
68 "Media.AudioRendererMissedDeadline", percentage_missed); | 73 "Media.AudioRendererMissedDeadline", percentage_missed); |
69 | 74 |
70 // Add more detailed information regarding detected audio glitches where | 75 // Add more detailed information regarding detected audio glitches where |
71 // a non-zero value of |renderer_missed_callback_count_| is added to the | 76 // a non-zero value of |renderer_missed_callback_count_| is added to the |
72 // AUDIO_RENDERER_AUDIO_GLITCHES bin. | 77 // AUDIO_RENDERER_AUDIO_GLITCHES bin. |
73 renderer_missed_callback_count_ > 0 ? | 78 renderer_missed_callback_count_ > 0 ? |
74 LogAudioGlitchResult(AUDIO_RENDERER_AUDIO_GLITCHES) : | 79 LogAudioGlitchResult(AUDIO_RENDERER_AUDIO_GLITCHES) : |
75 LogAudioGlitchResult(AUDIO_RENDERER_NO_AUDIO_GLITCHES); | 80 LogAudioGlitchResult(AUDIO_RENDERER_NO_AUDIO_GLITCHES); |
76 std::string log_string = | 81 std::string log_string = |
77 base::StringPrintf("ASR: number of detected audio glitches=%d", | 82 base::StringPrintf("ASR: number of detected audio glitches=%d", |
78 static_cast<int>(renderer_missed_callback_count_)); | 83 static_cast<int>(renderer_missed_callback_count_)); |
79 MediaStreamManager::SendMessageToNativeLog(log_string); | 84 MediaStreamManager::SendMessageToNativeLog(log_string); |
80 DVLOG(1) << log_string; | 85 DVLOG(1) << log_string; |
81 } | 86 } |
82 | 87 |
83 // media::AudioOutputController::SyncReader implementations. | 88 // media::AudioOutputController::SyncReader implementations. |
84 void AudioSyncReader::UpdatePendingBytes(uint32 bytes) { | 89 void AudioSyncReader::UpdatePendingBytes(uint32_t bytes, |
| 90 uint32_t frames_skipped) { |
| 91 // Increase the number of skipped frames stored in shared memory. We don't |
| 92 // send it over the socket since sending more than 4 bytes might lead to being |
| 93 // descheduled. The reading side will zero it when consumed. |
| 94 AudioOutputBuffer* buffer = |
| 95 reinterpret_cast<AudioOutputBuffer*>(shared_memory_->memory()); |
| 96 buffer->params.frames_skipped += frames_skipped; |
| 97 |
85 // Zero out the entire output buffer to avoid stuttering/repeating-buffers | 98 // Zero out the entire output buffer to avoid stuttering/repeating-buffers |
86 // in the anomalous case if the renderer is unable to keep up with real-time. | 99 // in the anomalous case if the renderer is unable to keep up with real-time. |
87 output_bus_->Zero(); | 100 output_bus_->Zero(); |
| 101 |
88 socket_->Send(&bytes, sizeof(bytes)); | 102 socket_->Send(&bytes, sizeof(bytes)); |
89 ++buffer_index_; | 103 ++buffer_index_; |
90 } | 104 } |
91 | 105 |
92 void AudioSyncReader::Read(AudioBus* dest) { | 106 void AudioSyncReader::Read(AudioBus* dest) { |
93 ++renderer_callback_count_; | 107 ++renderer_callback_count_; |
94 if (!WaitUntilDataIsReady()) { | 108 if (!WaitUntilDataIsReady()) { |
95 ++renderer_missed_callback_count_; | 109 ++renderer_missed_callback_count_; |
96 if (renderer_missed_callback_count_ <= 100) { | 110 if (renderer_missed_callback_count_ <= 100) { |
97 LOG(WARNING) << "AudioSyncReader::Read timed out, audio glitch count=" | 111 LOG(WARNING) << "AudioSyncReader::Read timed out, audio glitch count=" |
(...skipping 74 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
172 base::TimeDelta::FromMilliseconds(1), | 186 base::TimeDelta::FromMilliseconds(1), |
173 base::TimeDelta::FromMilliseconds(1000), | 187 base::TimeDelta::FromMilliseconds(1000), |
174 50); | 188 50); |
175 return false; | 189 return false; |
176 } | 190 } |
177 | 191 |
178 return true; | 192 return true; |
179 } | 193 } |
180 | 194 |
181 } // namespace content | 195 } // namespace content |
OLD | NEW |