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

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
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
14 AudioSyncReader::AudioSyncReader(base::SharedMemory* shared_memory) 15 AudioSyncReader::AudioSyncReader(base::SharedMemory* shared_memory)
15 : shared_memory_(shared_memory) { 16 : shared_memory_(shared_memory),
17 previous_call_time_(base::Time()) {
scherkus (not reviewing) 2011/09/01 03:16:48 no need to use declare this as you're using defaul
16 } 18 }
17 19
18 AudioSyncReader::~AudioSyncReader() { 20 AudioSyncReader::~AudioSyncReader() {
19 } 21 }
20 22
21 // media::AudioOutputController::SyncReader implementations. 23 // media::AudioOutputController::SyncReader implementations.
22 void AudioSyncReader::UpdatePendingBytes(uint32 bytes) { 24 void AudioSyncReader::UpdatePendingBytes(uint32 bytes) {
23 socket_->Send(&bytes, sizeof(bytes)); 25 socket_->Send(&bytes, sizeof(bytes));
24 } 26 }
25 27
26 uint32 AudioSyncReader::Read(void* data, uint32 size) { 28 uint32 AudioSyncReader::Read(void* data, uint32 size) {
27 uint32 max_size = media::PacketSizeSizeInBytes( 29 uint32 max_size = media::PacketSizeSizeInBytes(
28 shared_memory_->created_size()); 30 shared_memory_->created_size());
31
32 // HACK: yield if reader is called too often.
33 // Problem is lack of synchronization between host and renderer. We cannot be
34 // sure if renderer already filled the buffer, and due to all the plugins we
35 // cannot change the API, so we yield if previos call was too recently.
scherkus (not reviewing) 2011/09/01 03:16:48 previos -> previous too recently -> too recent
36 // Optimization: if renderer is "new" one that writes length of data we can
37 // stop yielding the moment length is written -- not ideal solution,
38 // but better than nothing.
39 while (media::IsUnknownDataSize(shared_memory_, max_size) &&
40 (base::Time::Now() - previous_call_time_).InMilliseconds() < 10) {
scherkus (not reviewing) 2011/09/01 03:16:48 what's 10 ? care to make it a constant?
41 base::PlatformThread::YieldCurrentThread();
42 }
29 uint32 read_size = std::min(media::GetActualDataSizeInBytes(shared_memory_, 43 uint32 read_size = std::min(media::GetActualDataSizeInBytes(shared_memory_,
30 max_size), 44 max_size),
31 size); 45 size);
32 46
33 // Get the data from the buffer. 47 // Get the data from the buffer.
34 memcpy(data, shared_memory_->memory(), read_size); 48 memcpy(data, shared_memory_->memory(), read_size);
35 49
36 // If amount read was less than requested, then zero out the remainder. 50 // If amount read was less than requested, then zero out the remainder.
37 if (read_size < size) 51 if (read_size < size)
38 memset(static_cast<char*>(data) + read_size, 0, size - read_size); 52 memset(static_cast<char*>(data) + read_size, 0, size - read_size);
39 53
40 // Zero out the entire buffer. 54 // Zero out the entire buffer.
41 memset(shared_memory_->memory(), 0, max_size); 55 memset(shared_memory_->memory(), 0, max_size);
42 56
43 // Store max length of data into buffer, in case client does not do that. 57 // Store unknown length of data into buffer, in case renderer does not store
44 media::SetActualDataSizeInBytes(shared_memory_, max_size, max_size); 58 // the length itself. It also helps in decision if we need to yield.
59 media::SetUnknownDataSize(shared_memory_, max_size);
45 60
61 previous_call_time_ = base::Time::Now();
46 return read_size; 62 return read_size;
47 } 63 }
48 64
49 void AudioSyncReader::Close() { 65 void AudioSyncReader::Close() {
50 socket_->Close(); 66 socket_->Close();
51 } 67 }
52 68
53 bool AudioSyncReader::Init() { 69 bool AudioSyncReader::Init() {
54 base::SyncSocket* sockets[2] = {0}; 70 base::SyncSocket* sockets[2] = {0};
55 if (base::SyncSocket::CreatePair(sockets)) { 71 if (base::SyncSocket::CreatePair(sockets)) {
(...skipping 19 matching lines...) Expand all
75 bool AudioSyncReader::PrepareForeignSocketHandle( 91 bool AudioSyncReader::PrepareForeignSocketHandle(
76 base::ProcessHandle process_handle, 92 base::ProcessHandle process_handle,
77 base::FileDescriptor* foreign_handle) { 93 base::FileDescriptor* foreign_handle) {
78 foreign_handle->fd = foreign_socket_->handle(); 94 foreign_handle->fd = foreign_socket_->handle();
79 foreign_handle->auto_close = false; 95 foreign_handle->auto_close = false;
80 if (foreign_handle->fd != -1) 96 if (foreign_handle->fd != -1)
81 return true; 97 return true;
82 return false; 98 return false;
83 } 99 }
84 #endif 100 #endif
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698