Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(1092)

Side by Side Diff: content/browser/renderer_host/media/audio_sync_reader.cc

Issue 7755001: Workaround for race condition between audio host and renderer (Closed) Base URL: http://src.chromium.org/svn/trunk/src/
Patch Set: '' Created 9 years, 3 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch | Annotate | Revision Log
« no previous file with comments | « content/browser/renderer_host/media/audio_sync_reader.h ('k') | media/audio/audio_util.h » ('j') | no next file with comments »
Toggle Intra-line Diffs ('i') | Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
OLDNEW
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 "media/audio/audio_buffers_state.h" 12 #include "media/audio/audio_buffers_state.h"
12 #include "media/audio/audio_util.h" 13 #include "media/audio/audio_util.h"
13 14
15 const int kMinIntervalBetweenReadCallsInMs = 10;
16
14 AudioSyncReader::AudioSyncReader(base::SharedMemory* shared_memory) 17 AudioSyncReader::AudioSyncReader(base::SharedMemory* shared_memory)
15 : shared_memory_(shared_memory) { 18 : shared_memory_(shared_memory) {
16 } 19 }
17 20
18 AudioSyncReader::~AudioSyncReader() { 21 AudioSyncReader::~AudioSyncReader() {
19 } 22 }
20 23
21 // media::AudioOutputController::SyncReader implementations. 24 // media::AudioOutputController::SyncReader implementations.
22 void AudioSyncReader::UpdatePendingBytes(uint32 bytes) { 25 void AudioSyncReader::UpdatePendingBytes(uint32 bytes) {
23 socket_->Send(&bytes, sizeof(bytes)); 26 socket_->Send(&bytes, sizeof(bytes));
24 } 27 }
25 28
26 uint32 AudioSyncReader::Read(void* data, uint32 size) { 29 uint32 AudioSyncReader::Read(void* data, uint32 size) {
27 uint32 max_size = media::PacketSizeSizeInBytes( 30 uint32 max_size = media::PacketSizeSizeInBytes(
28 shared_memory_->created_size()); 31 shared_memory_->created_size());
32
33 #if defined(OS_WIN)
34 // HACK: yield if reader is called too often.
35 // 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
37 // 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
39 // stop yielding the moment length is written -- not ideal solution,
40 // but better than nothing.
41 while (media::IsUnknownDataSize(shared_memory_, max_size) &&
42 ((base::Time::Now() - previous_call_time_).InMilliseconds() <
43 kMinIntervalBetweenReadCallsInMs)) {
44 base::PlatformThread::YieldCurrentThread();
45 }
46 previous_call_time_ = base::Time::Now();
47 #endif
48
29 uint32 read_size = std::min(media::GetActualDataSizeInBytes(shared_memory_, 49 uint32 read_size = std::min(media::GetActualDataSizeInBytes(shared_memory_,
30 max_size), 50 max_size),
31 size); 51 size);
32 52
33 // Get the data from the buffer. 53 // Get the data from the buffer.
34 memcpy(data, shared_memory_->memory(), read_size); 54 memcpy(data, shared_memory_->memory(), read_size);
35 55
36 // If amount read was less than requested, then zero out the remainder. 56 // If amount read was less than requested, then zero out the remainder.
37 if (read_size < size) 57 if (read_size < size)
38 memset(static_cast<char*>(data) + read_size, 0, size - read_size); 58 memset(static_cast<char*>(data) + read_size, 0, size - read_size);
39 59
40 // Zero out the entire buffer. 60 // Zero out the entire buffer.
41 memset(shared_memory_->memory(), 0, max_size); 61 memset(shared_memory_->memory(), 0, max_size);
42 62
43 // Store max length of data into buffer, in case client does not do that. 63 // Store unknown length of data into buffer, in case renderer does not store
44 media::SetActualDataSizeInBytes(shared_memory_, max_size, max_size); 64 // the length itself. It also helps in decision if we need to yield.
65 media::SetUnknownDataSize(shared_memory_, max_size);
45 66
46 return read_size; 67 return read_size;
47 } 68 }
48 69
49 void AudioSyncReader::Close() { 70 void AudioSyncReader::Close() {
50 socket_->Close(); 71 socket_->Close();
51 } 72 }
52 73
53 bool AudioSyncReader::Init() { 74 bool AudioSyncReader::Init() {
54 base::SyncSocket* sockets[2] = {0}; 75 base::SyncSocket* sockets[2] = {0};
(...skipping 20 matching lines...) Expand all
75 bool AudioSyncReader::PrepareForeignSocketHandle( 96 bool AudioSyncReader::PrepareForeignSocketHandle(
76 base::ProcessHandle process_handle, 97 base::ProcessHandle process_handle,
77 base::FileDescriptor* foreign_handle) { 98 base::FileDescriptor* foreign_handle) {
78 foreign_handle->fd = foreign_socket_->handle(); 99 foreign_handle->fd = foreign_socket_->handle();
79 foreign_handle->auto_close = false; 100 foreign_handle->auto_close = false;
80 if (foreign_handle->fd != -1) 101 if (foreign_handle->fd != -1)
81 return true; 102 return true;
82 return false; 103 return false;
83 } 104 }
84 #endif 105 #endif
OLDNEW
« no previous file with comments | « content/browser/renderer_host/media/audio_sync_reader.h ('k') | media/audio/audio_util.h » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698