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

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

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