| OLD | NEW |
| 1 // Copyright (c) 2011 The Chromium Authors. All rights reserved. | 1 // Copyright (c) 2011 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" | 11 #include "base/threading/platform_thread.h" |
| 12 #include "media/audio/audio_buffers_state.h" | 12 #include "media/audio/audio_buffers_state.h" |
| 13 #include "media/audio/audio_util.h" | 13 #include "media/audio/audio_util.h" |
| 14 | 14 |
| 15 const int kMinIntervalBetweenReadCallsInMs = 10; | 15 const int kMinIntervalBetweenReadCallsInMs = 10; |
| 16 | 16 |
| 17 AudioSyncReader::AudioSyncReader(base::SharedMemory* shared_memory) | 17 AudioSyncReader::AudioSyncReader(base::SharedMemory* shared_memory) |
| 18 : shared_memory_(shared_memory) { | 18 : shared_memory_(shared_memory) { |
| 19 } | 19 } |
| 20 | 20 |
| 21 AudioSyncReader::~AudioSyncReader() { | 21 AudioSyncReader::~AudioSyncReader() { |
| 22 } | 22 } |
| 23 | 23 |
| 24 bool AudioSyncReader::DataReady() { |
| 25 return !media::IsUnknownDataSize( |
| 26 shared_memory_, |
| 27 media::PacketSizeSizeInBytes(shared_memory_->created_size())); |
| 28 } |
| 29 |
| 24 // media::AudioOutputController::SyncReader implementations. | 30 // media::AudioOutputController::SyncReader implementations. |
| 25 void AudioSyncReader::UpdatePendingBytes(uint32 bytes) { | 31 void AudioSyncReader::UpdatePendingBytes(uint32 bytes) { |
| 32 if (bytes != static_cast<uint32>(media::AudioOutputController::kPauseMark)) { |
| 33 // Store unknown length of data into buffer, so we later |
| 34 // can find out if data became available. |
| 35 media::SetUnknownDataSize( |
| 36 shared_memory_, |
| 37 media::PacketSizeSizeInBytes(shared_memory_->created_size())); |
| 38 } |
| 26 socket_->Send(&bytes, sizeof(bytes)); | 39 socket_->Send(&bytes, sizeof(bytes)); |
| 27 } | 40 } |
| 28 | 41 |
| 29 uint32 AudioSyncReader::Read(void* data, uint32 size) { | 42 uint32 AudioSyncReader::Read(void* data, uint32 size) { |
| 30 uint32 max_size = media::PacketSizeSizeInBytes( | 43 uint32 max_size = media::PacketSizeSizeInBytes( |
| 31 shared_memory_->created_size()); | 44 shared_memory_->created_size()); |
| 32 | 45 |
| 33 #if defined(OS_WIN) | 46 #if defined(OS_WIN) |
| 34 // HACK: yield if reader is called too often. | 47 // HACK: yield if reader is called too often. |
| 35 // Problem is lack of synchronization between host and renderer. We cannot be | 48 // Problem is lack of synchronization between host and renderer. We cannot be |
| 36 // sure if renderer already filled the buffer, and due to all the plugins we | 49 // sure if renderer already filled the buffer, and due to all the plugins we |
| 37 // cannot change the API, so we yield if previous call was too recent. | 50 // cannot change the API, so we yield if previous call was too recent. |
| 38 // Optimization: if renderer is "new" one that writes length of data we can | 51 // Optimization: if renderer is "new" one that writes length of data we can |
| 39 // stop yielding the moment length is written -- not ideal solution, | 52 // stop yielding the moment length is written -- not ideal solution, |
| 40 // but better than nothing. | 53 // but better than nothing. |
| 41 while (media::IsUnknownDataSize(shared_memory_, max_size) && | 54 while (!DataReady() && |
| 42 ((base::Time::Now() - previous_call_time_).InMilliseconds() < | 55 ((base::Time::Now() - previous_call_time_).InMilliseconds() < |
| 43 kMinIntervalBetweenReadCallsInMs)) { | 56 kMinIntervalBetweenReadCallsInMs)) { |
| 44 base::PlatformThread::YieldCurrentThread(); | 57 base::PlatformThread::YieldCurrentThread(); |
| 45 } | 58 } |
| 46 previous_call_time_ = base::Time::Now(); | 59 previous_call_time_ = base::Time::Now(); |
| 47 #endif | 60 #endif |
| 48 | 61 |
| 49 uint32 read_size = std::min(media::GetActualDataSizeInBytes(shared_memory_, | 62 uint32 read_size = std::min(media::GetActualDataSizeInBytes(shared_memory_, |
| 50 max_size), | 63 max_size), |
| 51 size); | 64 size); |
| (...skipping 44 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 96 bool AudioSyncReader::PrepareForeignSocketHandle( | 109 bool AudioSyncReader::PrepareForeignSocketHandle( |
| 97 base::ProcessHandle process_handle, | 110 base::ProcessHandle process_handle, |
| 98 base::FileDescriptor* foreign_handle) { | 111 base::FileDescriptor* foreign_handle) { |
| 99 foreign_handle->fd = foreign_socket_->handle(); | 112 foreign_handle->fd = foreign_socket_->handle(); |
| 100 foreign_handle->auto_close = false; | 113 foreign_handle->auto_close = false; |
| 101 if (foreign_handle->fd != -1) | 114 if (foreign_handle->fd != -1) |
| 102 return true; | 115 return true; |
| 103 return false; | 116 return false; |
| 104 } | 117 } |
| 105 #endif | 118 #endif |
| OLD | NEW |