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 "media/audio/shared_memory_util.h" |
9 #include "ppapi/shared_impl/ppapi_globals.h" | 9 #include "ppapi/shared_impl/ppapi_globals.h" |
10 | 10 |
| 11 // Hard coded values from PepperPlatformAudioOutputImpl. |
| 12 // TODO(dalecurtis): PPAPI shouldn't hard code these values for all clients. |
| 13 enum { kChannels = 2, kBytesPerSample = 2 }; |
| 14 |
11 namespace ppapi { | 15 namespace ppapi { |
12 | 16 |
13 #if defined(OS_NACL) | 17 #if defined(OS_NACL) |
14 namespace { | 18 namespace { |
15 // Because this is static, the function pointers will be NULL initially. | 19 // Because this is static, the function pointers will be NULL initially. |
16 PP_ThreadFunctions thread_functions; | 20 PP_ThreadFunctions thread_functions; |
17 } | 21 } |
18 #endif // defined(OS_NACL) | 22 #endif // defined(OS_NACL) |
19 | 23 |
20 PPB_Audio_Shared::PPB_Audio_Shared() | 24 PPB_Audio_Shared::PPB_Audio_Shared() |
21 : playing_(false), | 25 : playing_(false), |
22 shared_memory_size_(0), | 26 shared_memory_size_(0), |
23 #if defined(OS_NACL) | 27 #if defined(OS_NACL) |
24 thread_id_(0), | 28 thread_id_(0), |
25 thread_active_(false), | 29 thread_active_(false), |
26 #endif | 30 #endif |
27 callback_(NULL), | 31 callback_(NULL), |
28 user_data_(NULL) { | 32 user_data_(NULL), |
| 33 client_buffer_size_bytes_(0) { |
29 } | 34 } |
30 | 35 |
31 PPB_Audio_Shared::~PPB_Audio_Shared() { | 36 PPB_Audio_Shared::~PPB_Audio_Shared() { |
32 // Shut down the socket to escape any hanging |Receive|s. | 37 // Shut down the socket to escape any hanging |Receive|s. |
33 if (socket_.get()) | 38 if (socket_.get()) |
34 socket_->Shutdown(); | 39 socket_->Shutdown(); |
35 StopThread(); | 40 StopThread(); |
36 } | 41 } |
37 | 42 |
38 void PPB_Audio_Shared::SetCallback(PPB_Audio_Callback callback, | 43 void PPB_Audio_Shared::SetCallback(PPB_Audio_Callback callback, |
(...skipping 21 matching lines...) Expand all Loading... |
60 void PPB_Audio_Shared::SetStopPlaybackState() { | 65 void PPB_Audio_Shared::SetStopPlaybackState() { |
61 DCHECK(playing_); | 66 DCHECK(playing_); |
62 StopThread(); | 67 StopThread(); |
63 playing_ = false; | 68 playing_ = false; |
64 } | 69 } |
65 | 70 |
66 void PPB_Audio_Shared::SetStreamInfo( | 71 void PPB_Audio_Shared::SetStreamInfo( |
67 PP_Instance instance, | 72 PP_Instance instance, |
68 base::SharedMemoryHandle shared_memory_handle, | 73 base::SharedMemoryHandle shared_memory_handle, |
69 size_t shared_memory_size, | 74 size_t shared_memory_size, |
70 base::SyncSocket::Handle socket_handle) { | 75 base::SyncSocket::Handle socket_handle, |
| 76 int sample_frame_count) { |
71 socket_.reset(new base::CancelableSyncSocket(socket_handle)); | 77 socket_.reset(new base::CancelableSyncSocket(socket_handle)); |
72 shared_memory_.reset(new base::SharedMemory(shared_memory_handle, false)); | 78 shared_memory_.reset(new base::SharedMemory(shared_memory_handle, false)); |
73 shared_memory_size_ = shared_memory_size; | 79 shared_memory_size_ = shared_memory_size; |
74 | 80 |
75 if (!shared_memory_->Map( | 81 if (!shared_memory_->Map( |
76 media::TotalSharedMemorySizeInBytes(shared_memory_size_))) { | 82 media::TotalSharedMemorySizeInBytes(shared_memory_size_))) { |
77 PpapiGlobals::Get()->LogWithSource(instance, PP_LOGLEVEL_WARNING, "", | 83 PpapiGlobals::Get()->LogWithSource(instance, PP_LOGLEVEL_WARNING, "", |
78 "Failed to map shared memory for PPB_Audio_Shared."); | 84 "Failed to map shared memory for PPB_Audio_Shared."); |
| 85 } else { |
| 86 audio_bus_ = media::AudioBus::WrapMemory( |
| 87 kChannels, sample_frame_count, shared_memory_->memory()); |
| 88 // Setup integer audio buffer for user audio data. |
| 89 client_buffer_size_bytes_ = |
| 90 audio_bus_->frames() * audio_bus_->channels() * kBytesPerSample; |
| 91 client_buffer_.reset(new uint8_t[client_buffer_size_bytes_]); |
79 } | 92 } |
80 | 93 |
81 StartThread(); | 94 StartThread(); |
82 } | 95 } |
83 | 96 |
84 void PPB_Audio_Shared::StartThread() { | 97 void PPB_Audio_Shared::StartThread() { |
85 // Don't start the thread unless all our state is set up correctly. | 98 // Don't start the thread unless all our state is set up correctly. |
86 if (!playing_ || !callback_ || !socket_.get() || !shared_memory_->memory()) | 99 if (!playing_ || !callback_ || !socket_.get() || !shared_memory_->memory() || |
| 100 !audio_bus_.get() || !client_buffer_.get()) |
87 return; | 101 return; |
88 // Clear contents of shm buffer before starting audio thread. This will | 102 // Clear contents of shm buffer before starting audio thread. This will |
89 // prevent a burst of static if for some reason the audio thread doesn't | 103 // prevent a burst of static if for some reason the audio thread doesn't |
90 // start up quickly enough. | 104 // start up quickly enough. |
91 memset(shared_memory_->memory(), 0, shared_memory_size_); | 105 memset(shared_memory_->memory(), 0, shared_memory_size_); |
| 106 memset(client_buffer_.get(), 0, client_buffer_size_bytes_); |
92 #if !defined(OS_NACL) | 107 #if !defined(OS_NACL) |
93 DCHECK(!audio_thread_.get()); | 108 DCHECK(!audio_thread_.get()); |
94 audio_thread_.reset(new base::DelegateSimpleThread( | 109 audio_thread_.reset(new base::DelegateSimpleThread( |
95 this, "plugin_audio_thread")); | 110 this, "plugin_audio_thread")); |
96 audio_thread_->Start(); | 111 audio_thread_->Start(); |
97 #else | 112 #else |
98 // Use NaCl's special API for IRT code that creates threads that call back | 113 // Use NaCl's special API for IRT code that creates threads that call back |
99 // into user code. | 114 // into user code. |
100 if (NULL == thread_functions.thread_create || | 115 if (NULL == thread_functions.thread_create || |
101 NULL == thread_functions.thread_join) | 116 NULL == thread_functions.thread_join) |
(...skipping 31 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
133 | 148 |
134 // static | 149 // static |
135 void PPB_Audio_Shared::CallRun(void* self) { | 150 void PPB_Audio_Shared::CallRun(void* self) { |
136 PPB_Audio_Shared* audio = static_cast<PPB_Audio_Shared*>(self); | 151 PPB_Audio_Shared* audio = static_cast<PPB_Audio_Shared*>(self); |
137 audio->Run(); | 152 audio->Run(); |
138 } | 153 } |
139 #endif | 154 #endif |
140 | 155 |
141 void PPB_Audio_Shared::Run() { | 156 void PPB_Audio_Shared::Run() { |
142 int pending_data; | 157 int pending_data; |
143 void* buffer = shared_memory_->memory(); | 158 const int bytes_per_frame = |
| 159 sizeof(*audio_bus_->channel(0)) * audio_bus_->channels(); |
144 | 160 |
145 while (sizeof(pending_data) == | 161 while (sizeof(pending_data) == |
146 socket_->Receive(&pending_data, sizeof(pending_data)) && | 162 socket_->Receive(&pending_data, sizeof(pending_data)) && |
147 pending_data != media::kPauseMark) { | 163 pending_data != media::kPauseMark) { |
148 callback_(buffer, shared_memory_size_, user_data_); | 164 callback_(client_buffer_.get(), client_buffer_size_bytes_, user_data_); |
| 165 |
| 166 // Deinterleave the audio data into the shared memory as float. |
| 167 audio_bus_->FromInterleaved( |
| 168 client_buffer_.get(), audio_bus_->frames(), kBytesPerSample); |
149 | 169 |
150 // Let the host know we are done. | 170 // Let the host know we are done. |
| 171 // TODO(dalecurtis): Technically this is not the exact size. Due to channel |
| 172 // padding for alignment, there may be more data available than this. We're |
| 173 // relying on AudioSyncReader::Read() to parse this with that in mind. |
| 174 // Rename these methods to Set/GetActualFrameCount(). |
151 media::SetActualDataSizeInBytes( | 175 media::SetActualDataSizeInBytes( |
152 shared_memory_.get(), shared_memory_size_, shared_memory_size_); | 176 shared_memory_.get(), shared_memory_size_, |
| 177 audio_bus_->frames() * bytes_per_frame); |
153 } | 178 } |
154 } | 179 } |
155 | 180 |
156 } // namespace ppapi | 181 } // namespace ppapi |
OLD | NEW |