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 "ppapi/shared_impl/ppb_audio_shared.h" | 5 #include "ppapi/shared_impl/ppb_audio_shared.h" |
6 | 6 |
7 #include "base/logging.h" | 7 #include "base/logging.h" |
8 #include "media/audio/shared_memory_util.h" | |
9 #include "ppapi/shared_impl/ppapi_globals.h" | 8 #include "ppapi/shared_impl/ppapi_globals.h" |
10 | 9 |
| 10 using base::subtle::Atomic32; |
| 11 |
11 namespace ppapi { | 12 namespace ppapi { |
12 | 13 |
13 #if defined(OS_NACL) | 14 #if defined(OS_NACL) |
14 namespace { | 15 namespace { |
15 // Because this is static, the function pointers will be NULL initially. | 16 // Because this is static, the function pointers will be NULL initially. |
16 PP_ThreadFunctions thread_functions; | 17 PP_ThreadFunctions thread_functions; |
17 } | 18 } |
18 #endif // defined(OS_NACL) | 19 #endif // defined(OS_NACL) |
19 | 20 |
| 21 // FIXME: The following two functions (TotalSharedMemorySizeInBytes, |
| 22 // SetActualDataSizeInBytes) are copied from audio_util.cc. |
| 23 // Remove these functions once a minimal media library is provided for them. |
| 24 // code.google.com/p/chromium/issues/detail?id=123203 |
| 25 |
| 26 uint32 TotalSharedMemorySizeInBytes(uint32 packet_size) { |
| 27 // Need to reserve extra 4 bytes for size of data. |
| 28 return packet_size + sizeof(Atomic32); |
| 29 } |
| 30 |
| 31 void SetActualDataSizeInBytes(base::SharedMemory* shared_memory, |
| 32 uint32 shared_memory_size, |
| 33 uint32 actual_data_size) { |
| 34 char* ptr = static_cast<char*>(shared_memory->memory()) + shared_memory_size; |
| 35 DCHECK_EQ(0u, reinterpret_cast<size_t>(ptr) & 3); |
| 36 |
| 37 // Set actual data size at the end of the buffer. |
| 38 base::subtle::Release_Store(reinterpret_cast<volatile Atomic32*>(ptr), |
| 39 actual_data_size); |
| 40 } |
| 41 |
| 42 const int PPB_Audio_Shared::kPauseMark = -1; |
| 43 |
20 PPB_Audio_Shared::PPB_Audio_Shared() | 44 PPB_Audio_Shared::PPB_Audio_Shared() |
21 : playing_(false), | 45 : playing_(false), |
22 shared_memory_size_(0), | 46 shared_memory_size_(0), |
23 #if defined(OS_NACL) | 47 #if defined(OS_NACL) |
24 thread_id_(0), | 48 thread_id_(0), |
25 thread_active_(false), | 49 thread_active_(false), |
26 #endif | 50 #endif |
27 callback_(NULL), | 51 callback_(NULL), |
28 user_data_(NULL) { | 52 user_data_(NULL) { |
29 } | 53 } |
(...skipping 32 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
62 | 86 |
63 void PPB_Audio_Shared::SetStreamInfo( | 87 void PPB_Audio_Shared::SetStreamInfo( |
64 PP_Instance instance, | 88 PP_Instance instance, |
65 base::SharedMemoryHandle shared_memory_handle, | 89 base::SharedMemoryHandle shared_memory_handle, |
66 size_t shared_memory_size, | 90 size_t shared_memory_size, |
67 base::SyncSocket::Handle socket_handle) { | 91 base::SyncSocket::Handle socket_handle) { |
68 socket_.reset(new base::CancelableSyncSocket(socket_handle)); | 92 socket_.reset(new base::CancelableSyncSocket(socket_handle)); |
69 shared_memory_.reset(new base::SharedMemory(shared_memory_handle, false)); | 93 shared_memory_.reset(new base::SharedMemory(shared_memory_handle, false)); |
70 shared_memory_size_ = shared_memory_size; | 94 shared_memory_size_ = shared_memory_size; |
71 | 95 |
72 if (!shared_memory_->Map( | 96 if (!shared_memory_->Map(TotalSharedMemorySizeInBytes(shared_memory_size_))) { |
73 media::TotalSharedMemorySizeInBytes(shared_memory_size_))) { | |
74 PpapiGlobals::Get()->LogWithSource(instance, PP_LOGLEVEL_WARNING, "", | 97 PpapiGlobals::Get()->LogWithSource(instance, PP_LOGLEVEL_WARNING, "", |
75 "Failed to map shared memory for PPB_Audio_Shared."); | 98 "Failed to map shared memory for PPB_Audio_Shared."); |
76 } | 99 } |
77 | 100 |
78 StartThread(); | 101 StartThread(); |
79 } | 102 } |
80 | 103 |
81 void PPB_Audio_Shared::StartThread() { | 104 void PPB_Audio_Shared::StartThread() { |
82 // Don't start the thread unless all our state is set up correctly. | 105 // Don't start the thread unless all our state is set up correctly. |
83 if (!playing_ || !callback_ || !socket_.get() || !shared_memory_->memory()) | 106 if (!playing_ || !callback_ || !socket_.get() || !shared_memory_->memory()) |
(...skipping 53 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
137 audio->Run(); | 160 audio->Run(); |
138 } | 161 } |
139 #endif | 162 #endif |
140 | 163 |
141 void PPB_Audio_Shared::Run() { | 164 void PPB_Audio_Shared::Run() { |
142 int pending_data; | 165 int pending_data; |
143 void* buffer = shared_memory_->memory(); | 166 void* buffer = shared_memory_->memory(); |
144 | 167 |
145 while (sizeof(pending_data) == | 168 while (sizeof(pending_data) == |
146 socket_->Receive(&pending_data, sizeof(pending_data)) && | 169 socket_->Receive(&pending_data, sizeof(pending_data)) && |
147 pending_data != media::kPauseMark) { | 170 pending_data != kPauseMark) { |
148 callback_(buffer, shared_memory_size_, user_data_); | 171 callback_(buffer, shared_memory_size_, user_data_); |
149 | 172 |
150 // Let the host know we are done. | 173 // Let the host know we are done. |
151 media::SetActualDataSizeInBytes( | 174 SetActualDataSizeInBytes(shared_memory_.get(), shared_memory_size_, |
152 shared_memory_.get(), shared_memory_size_, shared_memory_size_); | 175 shared_memory_size_); |
153 } | 176 } |
154 } | 177 } |
155 | 178 |
156 } // namespace ppapi | 179 } // namespace ppapi |
OLD | NEW |