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> | |
9 #include <utility> | |
8 | 10 |
9 #include "base/command_line.h" | 11 #include "base/command_line.h" |
10 #include "base/format_macros.h" | 12 #include "base/format_macros.h" |
13 #include "base/memory/ptr_util.h" | |
11 #include "base/memory/shared_memory.h" | 14 #include "base/memory/shared_memory.h" |
12 #include "base/metrics/histogram_macros.h" | 15 #include "base/metrics/histogram_macros.h" |
13 #include "base/strings/stringprintf.h" | 16 #include "base/strings/stringprintf.h" |
14 #include "base/trace_event/trace_event.h" | 17 #include "base/trace_event/trace_event.h" |
15 #include "build/build_config.h" | 18 #include "build/build_config.h" |
16 #include "content/browser/renderer_host/media/media_stream_manager.h" | 19 #include "content/browser/renderer_host/media/media_stream_manager.h" |
17 #include "content/public/common/content_switches.h" | 20 #include "content/public/common/content_switches.h" |
18 #include "media/base/audio_parameters.h" | 21 #include "media/base/audio_parameters.h" |
19 | 22 |
20 using media::AudioBus; | 23 using media::AudioBus; |
(...skipping 12 matching lines...) Expand all Loading... | |
33 void LogAudioGlitchResult(AudioGlitchResult result) { | 36 void LogAudioGlitchResult(AudioGlitchResult result) { |
34 UMA_HISTOGRAM_ENUMERATION("Media.AudioRendererAudioGlitches", | 37 UMA_HISTOGRAM_ENUMERATION("Media.AudioRendererAudioGlitches", |
35 result, | 38 result, |
36 AUDIO_RENDERER_AUDIO_GLITCHES_MAX + 1); | 39 AUDIO_RENDERER_AUDIO_GLITCHES_MAX + 1); |
37 } | 40 } |
38 | 41 |
39 } // namespace | 42 } // namespace |
40 | 43 |
41 namespace content { | 44 namespace content { |
42 | 45 |
43 AudioSyncReader::AudioSyncReader(base::SharedMemory* shared_memory, | 46 AudioSyncReader::AudioSyncReader( |
44 const media::AudioParameters& params) | 47 const media::AudioParameters& params, |
45 : shared_memory_(shared_memory), | 48 std::unique_ptr<base::SharedMemory> shared_memory, |
49 std::unique_ptr<base::CancelableSyncSocket> socket, | |
50 std::unique_ptr<base::CancelableSyncSocket> foreign_socket) | |
51 : shared_memory_(std::move(shared_memory)), | |
46 mute_audio_(base::CommandLine::ForCurrentProcess()->HasSwitch( | 52 mute_audio_(base::CommandLine::ForCurrentProcess()->HasSwitch( |
47 switches::kMuteAudio)), | 53 switches::kMuteAudio)), |
54 socket_(std::move(socket)), | |
55 foreign_socket_(std::move(foreign_socket)), | |
48 packet_size_(shared_memory_->requested_size()), | 56 packet_size_(shared_memory_->requested_size()), |
49 renderer_callback_count_(0), | 57 renderer_callback_count_(0), |
50 renderer_missed_callback_count_(0), | 58 renderer_missed_callback_count_(0), |
51 trailing_renderer_missed_callback_count_(0), | 59 trailing_renderer_missed_callback_count_(0), |
52 #if defined(OS_MACOSX) | 60 #if defined(OS_MACOSX) |
53 maximum_wait_time_(params.GetBufferDuration() / 2), | 61 maximum_wait_time_(params.GetBufferDuration() / 2), |
54 #else | 62 #else |
55 // TODO(dalecurtis): Investigate if we can reduce this on all platforms. | 63 // TODO(dalecurtis): Investigate if we can reduce this on all platforms. |
56 maximum_wait_time_(base::TimeDelta::FromMilliseconds(20)), | 64 maximum_wait_time_(base::TimeDelta::FromMilliseconds(20)), |
57 #endif | 65 #endif |
(...skipping 42 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
100 renderer_missed_callback_count_ > 0 ? | 108 renderer_missed_callback_count_ > 0 ? |
101 LogAudioGlitchResult(AUDIO_RENDERER_AUDIO_GLITCHES) : | 109 LogAudioGlitchResult(AUDIO_RENDERER_AUDIO_GLITCHES) : |
102 LogAudioGlitchResult(AUDIO_RENDERER_NO_AUDIO_GLITCHES); | 110 LogAudioGlitchResult(AUDIO_RENDERER_NO_AUDIO_GLITCHES); |
103 std::string log_string = base::StringPrintf( | 111 std::string log_string = base::StringPrintf( |
104 "ASR: number of detected audio glitches: %" PRIuS " out of %" PRIuS, | 112 "ASR: number of detected audio glitches: %" PRIuS " out of %" PRIuS, |
105 renderer_missed_callback_count_, renderer_callback_count_); | 113 renderer_missed_callback_count_, renderer_callback_count_); |
106 MediaStreamManager::SendMessageToNativeLog(log_string); | 114 MediaStreamManager::SendMessageToNativeLog(log_string); |
107 DVLOG(1) << log_string; | 115 DVLOG(1) << log_string; |
108 } | 116 } |
109 | 117 |
118 // static | |
119 std::unique_ptr<AudioSyncReader> AudioSyncReader::Create( | |
120 const media::AudioParameters& params) { | |
121 std::unique_ptr<base::SharedMemory> shared_memory(new base::SharedMemory()); | |
122 std::unique_ptr<base::CancelableSyncSocket> socket( | |
123 new base::CancelableSyncSocket()); | |
124 std::unique_ptr<base::CancelableSyncSocket> foreign_socket( | |
125 new base::CancelableSyncSocket()); | |
126 | |
127 base::CheckedNumeric<size_t> memory_size = | |
DaleCurtis
2016/09/16 23:29:12
Move above the unique_ptr allocations.
Max Morin
2016/09/19 10:13:01
Done.
| |
128 sizeof(media::AudioOutputBufferParameters); | |
129 memory_size += AudioBus::CalculateMemorySize(params); | |
130 | |
131 if (!memory_size.IsValid() || | |
132 !shared_memory->CreateAndMapAnonymous(memory_size.ValueOrDie()) || | |
133 !base::CancelableSyncSocket::CreatePair(socket.get(), | |
134 foreign_socket.get())) { | |
135 return {}; | |
DaleCurtis
2016/09/16 23:29:12
Ditto return nullptr.
Max Morin
2016/09/19 10:13:01
Done.
| |
136 } | |
137 return base::WrapUnique(new AudioSyncReader(params, std::move(shared_memory), | |
138 std::move(socket), | |
139 std::move(foreign_socket))); | |
140 } | |
141 | |
110 // media::AudioOutputController::SyncReader implementations. | 142 // media::AudioOutputController::SyncReader implementations. |
111 void AudioSyncReader::UpdatePendingBytes(uint32_t bytes, | 143 void AudioSyncReader::UpdatePendingBytes(uint32_t bytes, |
112 uint32_t frames_skipped) { | 144 uint32_t frames_skipped) { |
113 // 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 |
114 // 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 |
115 // descheduled. The reading side will zero it when consumed. | 147 // descheduled. The reading side will zero it when consumed. |
116 AudioOutputBuffer* buffer = | 148 AudioOutputBuffer* buffer = |
117 reinterpret_cast<AudioOutputBuffer*>(shared_memory_->memory()); | 149 reinterpret_cast<AudioOutputBuffer*>(shared_memory_->memory()); |
118 buffer->params.frames_skipped += frames_skipped; | 150 buffer->params.frames_skipped += frames_skipped; |
119 | 151 |
(...skipping 25 matching lines...) Expand all Loading... | |
145 if (mute_audio_) | 177 if (mute_audio_) |
146 dest->Zero(); | 178 dest->Zero(); |
147 else | 179 else |
148 output_bus_->CopyTo(dest); | 180 output_bus_->CopyTo(dest); |
149 } | 181 } |
150 | 182 |
151 void AudioSyncReader::Close() { | 183 void AudioSyncReader::Close() { |
152 socket_->Close(); | 184 socket_->Close(); |
153 } | 185 } |
154 | 186 |
155 bool AudioSyncReader::Init() { | |
156 socket_.reset(new base::CancelableSyncSocket()); | |
157 foreign_socket_.reset(new base::CancelableSyncSocket()); | |
158 return base::CancelableSyncSocket::CreatePair(socket_.get(), | |
159 foreign_socket_.get()); | |
160 } | |
161 | |
162 bool AudioSyncReader::PrepareForeignSocket( | |
163 base::ProcessHandle process_handle, | |
164 base::SyncSocket::TransitDescriptor* descriptor) { | |
165 return foreign_socket_->PrepareTransitDescriptor(process_handle, descriptor); | |
166 } | |
167 | |
168 bool AudioSyncReader::WaitUntilDataIsReady() { | 187 bool AudioSyncReader::WaitUntilDataIsReady() { |
169 TRACE_EVENT0("audio", "AudioSyncReader::WaitUntilDataIsReady"); | 188 TRACE_EVENT0("audio", "AudioSyncReader::WaitUntilDataIsReady"); |
170 base::TimeDelta timeout = maximum_wait_time_; | 189 base::TimeDelta timeout = maximum_wait_time_; |
171 const base::TimeTicks start_time = base::TimeTicks::Now(); | 190 const base::TimeTicks start_time = base::TimeTicks::Now(); |
172 const base::TimeTicks finish_time = start_time + timeout; | 191 const base::TimeTicks finish_time = start_time + timeout; |
173 | 192 |
174 // Check if data is ready and if not, wait a reasonable amount of time for it. | 193 // Check if data is ready and if not, wait a reasonable amount of time for it. |
175 // | 194 // |
176 // Data readiness is achieved via parallel counters, one on the renderer side | 195 // Data readiness is achieved via parallel counters, one on the renderer side |
177 // and one here. Every time a buffer is requested via UpdatePendingBytes(), | 196 // and one here. Every time a buffer is requested via UpdatePendingBytes(), |
(...skipping 35 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
213 base::TimeDelta::FromMilliseconds(1), | 232 base::TimeDelta::FromMilliseconds(1), |
214 base::TimeDelta::FromMilliseconds(1000), | 233 base::TimeDelta::FromMilliseconds(1000), |
215 50); | 234 50); |
216 return false; | 235 return false; |
217 } | 236 } |
218 | 237 |
219 return true; | 238 return true; |
220 } | 239 } |
221 | 240 |
222 } // namespace content | 241 } // namespace content |
OLD | NEW |