| 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" |
| 8 #include "ppapi/shared_impl/ppapi_globals.h" | 9 #include "ppapi/shared_impl/ppapi_globals.h" |
| 9 | 10 |
| 10 using base::subtle::Atomic32; | |
| 11 | |
| 12 namespace ppapi { | 11 namespace ppapi { |
| 13 | 12 |
| 14 #if defined(OS_NACL) | 13 #if defined(OS_NACL) |
| 15 namespace { | 14 namespace { |
| 16 // Because this is static, the function pointers will be NULL initially. | 15 // Because this is static, the function pointers will be NULL initially. |
| 17 PP_ThreadFunctions thread_functions; | 16 PP_ThreadFunctions thread_functions; |
| 18 } | 17 } |
| 19 #endif // defined(OS_NACL) | 18 #endif // defined(OS_NACL) |
| 20 | 19 |
| 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 | |
| 44 PPB_Audio_Shared::PPB_Audio_Shared() | 20 PPB_Audio_Shared::PPB_Audio_Shared() |
| 45 : playing_(false), | 21 : playing_(false), |
| 46 shared_memory_size_(0), | 22 shared_memory_size_(0), |
| 47 #if defined(OS_NACL) | 23 #if defined(OS_NACL) |
| 48 thread_id_(0), | 24 thread_id_(0), |
| 49 thread_active_(false), | 25 thread_active_(false), |
| 50 #endif | 26 #endif |
| 51 callback_(NULL), | 27 callback_(NULL), |
| 52 user_data_(NULL) { | 28 user_data_(NULL) { |
| 53 } | 29 } |
| (...skipping 32 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 86 | 62 |
| 87 void PPB_Audio_Shared::SetStreamInfo( | 63 void PPB_Audio_Shared::SetStreamInfo( |
| 88 PP_Instance instance, | 64 PP_Instance instance, |
| 89 base::SharedMemoryHandle shared_memory_handle, | 65 base::SharedMemoryHandle shared_memory_handle, |
| 90 size_t shared_memory_size, | 66 size_t shared_memory_size, |
| 91 base::SyncSocket::Handle socket_handle) { | 67 base::SyncSocket::Handle socket_handle) { |
| 92 socket_.reset(new base::CancelableSyncSocket(socket_handle)); | 68 socket_.reset(new base::CancelableSyncSocket(socket_handle)); |
| 93 shared_memory_.reset(new base::SharedMemory(shared_memory_handle, false)); | 69 shared_memory_.reset(new base::SharedMemory(shared_memory_handle, false)); |
| 94 shared_memory_size_ = shared_memory_size; | 70 shared_memory_size_ = shared_memory_size; |
| 95 | 71 |
| 96 if (!shared_memory_->Map(TotalSharedMemorySizeInBytes(shared_memory_size_))) { | 72 if (!shared_memory_->Map( |
| 73 media::TotalSharedMemorySizeInBytes(shared_memory_size_))) { |
| 97 PpapiGlobals::Get()->LogWithSource(instance, PP_LOGLEVEL_WARNING, "", | 74 PpapiGlobals::Get()->LogWithSource(instance, PP_LOGLEVEL_WARNING, "", |
| 98 "Failed to map shared memory for PPB_Audio_Shared."); | 75 "Failed to map shared memory for PPB_Audio_Shared."); |
| 99 } | 76 } |
| 100 | 77 |
| 101 StartThread(); | 78 StartThread(); |
| 102 } | 79 } |
| 103 | 80 |
| 104 void PPB_Audio_Shared::StartThread() { | 81 void PPB_Audio_Shared::StartThread() { |
| 105 // Don't start the thread unless all our state is set up correctly. | 82 // Don't start the thread unless all our state is set up correctly. |
| 106 if (!playing_ || !callback_ || !socket_.get() || !shared_memory_->memory()) | 83 if (!playing_ || !callback_ || !socket_.get() || !shared_memory_->memory()) |
| (...skipping 53 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 160 audio->Run(); | 137 audio->Run(); |
| 161 } | 138 } |
| 162 #endif | 139 #endif |
| 163 | 140 |
| 164 void PPB_Audio_Shared::Run() { | 141 void PPB_Audio_Shared::Run() { |
| 165 int pending_data; | 142 int pending_data; |
| 166 void* buffer = shared_memory_->memory(); | 143 void* buffer = shared_memory_->memory(); |
| 167 | 144 |
| 168 while (sizeof(pending_data) == | 145 while (sizeof(pending_data) == |
| 169 socket_->Receive(&pending_data, sizeof(pending_data)) && | 146 socket_->Receive(&pending_data, sizeof(pending_data)) && |
| 170 pending_data != kPauseMark) { | 147 pending_data != media::kPauseMark) { |
| 171 callback_(buffer, shared_memory_size_, user_data_); | 148 callback_(buffer, shared_memory_size_, user_data_); |
| 172 | 149 |
| 173 // Let the host know we are done. | 150 // Let the host know we are done. |
| 174 SetActualDataSizeInBytes(shared_memory_.get(), shared_memory_size_, | 151 media::SetActualDataSizeInBytes( |
| 175 shared_memory_size_); | 152 shared_memory_.get(), shared_memory_size_, shared_memory_size_); |
| 176 } | 153 } |
| 177 } | 154 } |
| 178 | 155 |
| 179 } // namespace ppapi | 156 } // namespace ppapi |
| OLD | NEW |