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/metrics/histogram.h" |
9 #include "base/process_util.h" | 10 #include "base/process_util.h" |
10 #include "base/shared_memory.h" | 11 #include "base/shared_memory.h" |
11 #include "media/audio/audio_buffers_state.h" | 12 #include "media/audio/audio_buffers_state.h" |
12 #include "media/audio/audio_parameters.h" | 13 #include "media/audio/audio_parameters.h" |
13 #include "media/audio/shared_memory_util.h" | 14 #include "media/audio/shared_memory_util.h" |
14 | 15 |
15 using media::AudioBus; | 16 using media::AudioBus; |
16 | 17 |
17 namespace content { | 18 namespace content { |
18 | 19 |
19 AudioSyncReader::AudioSyncReader(base::SharedMemory* shared_memory, | 20 AudioSyncReader::AudioSyncReader(base::SharedMemory* shared_memory, |
20 const media::AudioParameters& params, | 21 const media::AudioParameters& params, |
21 int input_channels) | 22 int input_channels) |
22 : shared_memory_(shared_memory), | 23 : shared_memory_(shared_memory), |
23 input_channels_(input_channels) { | 24 input_channels_(input_channels), |
| 25 renderer_callback_count_(0), |
| 26 renderer_missed_callback_count_(0) { |
24 packet_size_ = media::PacketSizeInBytes(shared_memory_->created_size()); | 27 packet_size_ = media::PacketSizeInBytes(shared_memory_->created_size()); |
25 int input_memory_size = 0; | 28 int input_memory_size = 0; |
26 int output_memory_size = AudioBus::CalculateMemorySize(params); | 29 int output_memory_size = AudioBus::CalculateMemorySize(params); |
27 if (input_channels_ > 0) { | 30 if (input_channels_ > 0) { |
28 // The input storage is after the output storage. | 31 // The input storage is after the output storage. |
29 int frames = params.frames_per_buffer(); | 32 int frames = params.frames_per_buffer(); |
30 input_memory_size = AudioBus::CalculateMemorySize(input_channels_, frames); | 33 input_memory_size = AudioBus::CalculateMemorySize(input_channels_, frames); |
31 char* input_data = | 34 char* input_data = |
32 static_cast<char*>(shared_memory_->memory()) + output_memory_size; | 35 static_cast<char*>(shared_memory_->memory()) + output_memory_size; |
33 input_bus_ = AudioBus::WrapMemory(input_channels_, frames, input_data); | 36 input_bus_ = AudioBus::WrapMemory(input_channels_, frames, input_data); |
34 } | 37 } |
35 DCHECK_EQ(packet_size_, output_memory_size + input_memory_size); | 38 DCHECK_EQ(packet_size_, output_memory_size + input_memory_size); |
36 output_bus_ = AudioBus::WrapMemory(params, shared_memory->memory()); | 39 output_bus_ = AudioBus::WrapMemory(params, shared_memory->memory()); |
37 } | 40 } |
38 | 41 |
39 AudioSyncReader::~AudioSyncReader() { | 42 AudioSyncReader::~AudioSyncReader() { |
| 43 if (!renderer_callback_count_) |
| 44 return; |
| 45 |
| 46 // Recording the percentage of deadline misses gives us a rough overview of |
| 47 // how many users might be running into audio glitches. |
| 48 int percentage_missed = |
| 49 100.0 * renderer_missed_callback_count_ / renderer_callback_count_; |
| 50 UMA_HISTOGRAM_PERCENTAGE( |
| 51 "Media.AudioRendererMissedDeadline", percentage_missed); |
40 } | 52 } |
41 | 53 |
42 bool AudioSyncReader::DataReady() { | 54 bool AudioSyncReader::DataReady() { |
43 return !media::IsUnknownDataSize(shared_memory_, packet_size_); | 55 return !media::IsUnknownDataSize(shared_memory_, packet_size_); |
44 } | 56 } |
45 | 57 |
46 // media::AudioOutputController::SyncReader implementations. | 58 // media::AudioOutputController::SyncReader implementations. |
47 void AudioSyncReader::UpdatePendingBytes(uint32 bytes) { | 59 void AudioSyncReader::UpdatePendingBytes(uint32 bytes) { |
48 if (bytes != static_cast<uint32>(media::kPauseMark)) { | 60 if (bytes != static_cast<uint32>(media::kPauseMark)) { |
49 // Store unknown length of data into buffer, so we later | 61 // Store unknown length of data into buffer, so we later |
50 // can find out if data became available. | 62 // can find out if data became available. |
51 media::SetUnknownDataSize(shared_memory_, packet_size_); | 63 media::SetUnknownDataSize(shared_memory_, packet_size_); |
52 } | 64 } |
53 | 65 |
54 if (socket_.get()) { | 66 if (socket_.get()) { |
55 socket_->Send(&bytes, sizeof(bytes)); | 67 socket_->Send(&bytes, sizeof(bytes)); |
56 } | 68 } |
57 } | 69 } |
58 | 70 |
59 int AudioSyncReader::Read(AudioBus* source, AudioBus* dest) { | 71 int AudioSyncReader::Read(AudioBus* source, AudioBus* dest) { |
| 72 ++renderer_callback_count_; |
| 73 if (!DataReady()) |
| 74 ++renderer_missed_callback_count_; |
| 75 |
60 // Copy optional synchronized live audio input for consumption by renderer | 76 // Copy optional synchronized live audio input for consumption by renderer |
61 // process. | 77 // process. |
62 if (source && input_bus_.get()) { | 78 if (source && input_bus_.get()) { |
63 DCHECK_EQ(source->channels(), input_bus_->channels()); | 79 DCHECK_EQ(source->channels(), input_bus_->channels()); |
64 DCHECK_LE(source->frames(), input_bus_->frames()); | 80 DCHECK_LE(source->frames(), input_bus_->frames()); |
65 source->CopyTo(input_bus_.get()); | 81 source->CopyTo(input_bus_.get()); |
66 } | 82 } |
67 | 83 |
68 // Retrieve the actual number of bytes available from the shared memory. If | 84 // Retrieve the actual number of bytes available from the shared memory. If |
69 // the renderer has not completed rendering this value will be invalid (still | 85 // the renderer has not completed rendering this value will be invalid (still |
(...skipping 62 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
132 base::FileDescriptor* foreign_handle) { | 148 base::FileDescriptor* foreign_handle) { |
133 foreign_handle->fd = foreign_socket_->handle(); | 149 foreign_handle->fd = foreign_socket_->handle(); |
134 foreign_handle->auto_close = false; | 150 foreign_handle->auto_close = false; |
135 if (foreign_handle->fd != -1) | 151 if (foreign_handle->fd != -1) |
136 return true; | 152 return true; |
137 return false; | 153 return false; |
138 } | 154 } |
139 #endif | 155 #endif |
140 | 156 |
141 } // namespace content | 157 } // namespace content |
OLD | NEW |