| 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/process_util.h" | 9 #include "base/process_util.h" |
| 10 #include "base/shared_memory.h" | 10 #include "base/shared_memory.h" |
| 11 #include "base/threading/platform_thread.h" | |
| 12 #include "media/audio/audio_buffers_state.h" | 11 #include "media/audio/audio_buffers_state.h" |
| 13 #include "media/audio/audio_parameters.h" | 12 #include "media/audio/audio_parameters.h" |
| 14 #include "media/audio/shared_memory_util.h" | 13 #include "media/audio/shared_memory_util.h" |
| 15 | 14 |
| 16 #if defined(OS_WIN) | |
| 17 const int kMinIntervalBetweenReadCallsInMs = 10; | |
| 18 #endif | |
| 19 | |
| 20 using media::AudioBus; | 15 using media::AudioBus; |
| 21 | 16 |
| 22 namespace content { | 17 namespace content { |
| 23 | 18 |
| 24 AudioSyncReader::AudioSyncReader(base::SharedMemory* shared_memory, | 19 AudioSyncReader::AudioSyncReader(base::SharedMemory* shared_memory, |
| 25 const media::AudioParameters& params, | 20 const media::AudioParameters& params, |
| 26 int input_channels) | 21 int input_channels) |
| 27 : shared_memory_(shared_memory), | 22 : shared_memory_(shared_memory), |
| 28 input_channels_(input_channels) { | 23 input_channels_(input_channels) { |
| 29 packet_size_ = media::PacketSizeInBytes(shared_memory_->created_size()); | 24 packet_size_ = media::PacketSizeInBytes(shared_memory_->created_size()); |
| (...skipping 25 matching lines...) Expand all Loading... |
| 55 // can find out if data became available. | 50 // can find out if data became available. |
| 56 media::SetUnknownDataSize(shared_memory_, packet_size_); | 51 media::SetUnknownDataSize(shared_memory_, packet_size_); |
| 57 } | 52 } |
| 58 | 53 |
| 59 if (socket_.get()) { | 54 if (socket_.get()) { |
| 60 socket_->Send(&bytes, sizeof(bytes)); | 55 socket_->Send(&bytes, sizeof(bytes)); |
| 61 } | 56 } |
| 62 } | 57 } |
| 63 | 58 |
| 64 int AudioSyncReader::Read(AudioBus* source, AudioBus* dest) { | 59 int AudioSyncReader::Read(AudioBus* source, AudioBus* dest) { |
| 65 #if defined(OS_WIN) | |
| 66 // HACK: yield if reader is called too often. | |
| 67 // Problem is lack of synchronization between host and renderer. We cannot be | |
| 68 // sure if renderer already filled the buffer, and due to all the plugins we | |
| 69 // cannot change the API, so we yield if previous call was too recent. | |
| 70 // Optimization: if renderer is "new" one that writes length of data we can | |
| 71 // stop yielding the moment length is written -- not ideal solution, | |
| 72 // but better than nothing. | |
| 73 while (!DataReady() && | |
| 74 ((base::Time::Now() - previous_call_time_).InMilliseconds() < | |
| 75 kMinIntervalBetweenReadCallsInMs)) { | |
| 76 base::PlatformThread::YieldCurrentThread(); | |
| 77 } | |
| 78 previous_call_time_ = base::Time::Now(); | |
| 79 #endif | |
| 80 | |
| 81 // Copy optional synchronized live audio input for consumption by renderer | 60 // Copy optional synchronized live audio input for consumption by renderer |
| 82 // process. | 61 // process. |
| 83 if (source && input_bus_.get()) { | 62 if (source && input_bus_.get()) { |
| 84 DCHECK_EQ(source->channels(), input_bus_->channels()); | 63 DCHECK_EQ(source->channels(), input_bus_->channels()); |
| 85 DCHECK_LE(source->frames(), input_bus_->frames()); | 64 DCHECK_LE(source->frames(), input_bus_->frames()); |
| 86 source->CopyTo(input_bus_.get()); | 65 source->CopyTo(input_bus_.get()); |
| 87 } | 66 } |
| 88 | 67 |
| 89 // Retrieve the actual number of bytes available from the shared memory. If | 68 // Retrieve the actual number of bytes available from the shared memory. If |
| 90 // the renderer has not completed rendering this value will be invalid (still | 69 // 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... |
| 153 base::FileDescriptor* foreign_handle) { | 132 base::FileDescriptor* foreign_handle) { |
| 154 foreign_handle->fd = foreign_socket_->handle(); | 133 foreign_handle->fd = foreign_socket_->handle(); |
| 155 foreign_handle->auto_close = false; | 134 foreign_handle->auto_close = false; |
| 156 if (foreign_handle->fd != -1) | 135 if (foreign_handle->fd != -1) |
| 157 return true; | 136 return true; |
| 158 return false; | 137 return false; |
| 159 } | 138 } |
| 160 #endif | 139 #endif |
| 161 | 140 |
| 162 } // namespace content | 141 } // namespace content |
| OLD | NEW |