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
3 // found in the LICENSE file.
5 #include "ppapi/shared_impl/ppb_audio_shared.h"
7 #include "base/logging.h"
8 #include "base/trace_event/trace_event.h"
9 #include "ppapi/nacl_irt/public/irt_ppapi.h"
10 #include "ppapi/shared_impl/ppapi_globals.h"
11 #include "ppapi/shared_impl/ppb_audio_config_shared.h"
12 #include "ppapi/shared_impl/proxy_lock.h"
17 bool g_nacl_mode
= false;
18 // Because this is static, the function pointers will be NULL initially.
19 PP_ThreadFunctions g_thread_functions
;
22 AudioCallbackCombined::AudioCallbackCombined()
23 : callback_1_0_(NULL
), callback_(NULL
) {}
25 AudioCallbackCombined::AudioCallbackCombined(
26 PPB_Audio_Callback_1_0 callback_1_0
)
27 : callback_1_0_(callback_1_0
), callback_(NULL
) {}
29 AudioCallbackCombined::AudioCallbackCombined(PPB_Audio_Callback callback
)
30 : callback_1_0_(NULL
), callback_(callback
) {}
32 AudioCallbackCombined::~AudioCallbackCombined() {}
34 bool AudioCallbackCombined::IsValid() const {
35 return callback_1_0_
|| callback_
;
38 void AudioCallbackCombined::Run(void* sample_buffer
,
39 uint32_t buffer_size_in_bytes
,
41 void* user_data
) const {
43 callback_(sample_buffer
, buffer_size_in_bytes
, latency
, user_data
);
44 } else if (callback_1_0_
) {
45 callback_1_0_(sample_buffer
, buffer_size_in_bytes
, user_data
);
51 PPB_Audio_Shared::PPB_Audio_Shared()
53 shared_memory_size_(0),
55 nacl_thread_active_(false),
57 client_buffer_size_bytes_(0),
62 PPB_Audio_Shared::~PPB_Audio_Shared() {
63 // Shut down the socket to escape any hanging |Receive|s.
69 void PPB_Audio_Shared::SetCallback(const AudioCallbackCombined
& callback
,
72 user_data_
= user_data
;
75 void PPB_Audio_Shared::SetStartPlaybackState() {
77 DCHECK(!audio_thread_
.get());
78 DCHECK(!nacl_thread_active_
);
79 // If the socket doesn't exist, that means that the plugin has started before
80 // the browser has had a chance to create all the shared memory info and
81 // notify us. This is a common case. In this case, we just set the playing_
82 // flag and the playback will automatically start when that data is available
88 void PPB_Audio_Shared::SetStopPlaybackState() {
94 void PPB_Audio_Shared::SetStreamInfo(
96 base::SharedMemoryHandle shared_memory_handle
,
97 size_t shared_memory_size
,
98 base::SyncSocket::Handle socket_handle
,
99 PP_AudioSampleRate sample_rate
,
100 int sample_frame_count
) {
101 socket_
.reset(new base::CancelableSyncSocket(socket_handle
));
102 shared_memory_
.reset(new base::SharedMemory(shared_memory_handle
, false));
103 shared_memory_size_
= shared_memory_size
;
105 kAudioOutputChannels
* (kBitsPerAudioOutputSample
/ 8) * sample_rate
;
108 if (!shared_memory_
->Map(shared_memory_size_
)) {
109 PpapiGlobals::Get()->LogWithSource(
113 "Failed to map shared memory for PPB_Audio_Shared.");
115 audio_bus_
= media::AudioBus::WrapMemory(
116 kAudioOutputChannels
, sample_frame_count
, shared_memory_
->memory());
117 // Setup integer audio buffer for user audio data.
118 client_buffer_size_bytes_
= audio_bus_
->frames() * audio_bus_
->channels() *
119 kBitsPerAudioOutputSample
/ 8;
120 client_buffer_
.reset(new uint8_t[client_buffer_size_bytes_
]);
126 void PPB_Audio_Shared::StartThread() {
127 // Don't start the thread unless all our state is set up correctly.
128 if (!playing_
|| !callback_
.IsValid() || !socket_
.get() ||
129 !shared_memory_
->memory() || !audio_bus_
.get() || !client_buffer_
.get() ||
130 bytes_per_second_
== 0)
132 // Clear contents of shm buffer before starting audio thread. This will
133 // prevent a burst of static if for some reason the audio thread doesn't
134 // start up quickly enough.
135 memset(shared_memory_
->memory(), 0, shared_memory_size_
);
136 memset(client_buffer_
.get(), 0, client_buffer_size_bytes_
);
139 // Use NaCl's special API for IRT code that creates threads that call back
141 if (!IsThreadFunctionReady())
144 DCHECK(!nacl_thread_active_
);
146 g_thread_functions
.thread_create(&nacl_thread_id_
, CallRun
, this);
147 DCHECK_EQ(0, result
);
148 nacl_thread_active_
= true;
150 DCHECK(!audio_thread_
.get());
152 new base::DelegateSimpleThread(this, "plugin_audio_thread"));
153 audio_thread_
->Start();
157 void PPB_Audio_Shared::StopThread() {
158 // In general, the audio thread should not do Pepper calls, but it might
159 // anyway (for example, our Audio test does CallOnMainThread). If it did a
160 // pepper call which acquires the lock (most of them do), and we try to shut
161 // down the thread and Join it while holding the lock, we would deadlock. So
162 // we give up the lock here so that the thread at least _can_ make Pepper
163 // calls without causing deadlock.
165 if (nacl_thread_active_
) {
167 CallWhileUnlocked(g_thread_functions
.thread_join
, nacl_thread_id_
);
168 DCHECK_EQ(0, result
);
169 nacl_thread_active_
= false;
172 if (audio_thread_
.get()) {
173 CallWhileUnlocked(base::Bind(&base::DelegateSimpleThread::Join
,
174 base::Unretained(audio_thread_
.get())));
175 audio_thread_
.reset();
181 bool PPB_Audio_Shared::IsThreadFunctionReady() {
185 return (g_thread_functions
.thread_create
!= NULL
&&
186 g_thread_functions
.thread_join
!= NULL
);
190 void PPB_Audio_Shared::SetNaClMode() {
195 void PPB_Audio_Shared::SetThreadFunctions(
196 const struct PP_ThreadFunctions
* functions
) {
198 g_thread_functions
= *functions
;
202 void PPB_Audio_Shared::CallRun(void* self
) {
203 PPB_Audio_Shared
* audio
= static_cast<PPB_Audio_Shared
*>(self
);
207 void PPB_Audio_Shared::Run() {
208 int pending_data
= 0;
209 while (sizeof(pending_data
) ==
210 socket_
->Receive(&pending_data
, sizeof(pending_data
))) {
211 // |buffer_index_| must track the number of Receive() calls. See the Send()
212 // call below for why this is important.
214 if (pending_data
< 0)
218 TRACE_EVENT0("audio", "PPB_Audio_Shared::FireRenderCallback");
219 PP_TimeDelta latency
=
220 static_cast<double>(pending_data
) / bytes_per_second_
;
222 client_buffer_
.get(), client_buffer_size_bytes_
, latency
, user_data_
);
225 // Deinterleave the audio data into the shared memory as floats.
226 audio_bus_
->FromInterleaved(client_buffer_
.get(),
227 audio_bus_
->frames(),
228 kBitsPerAudioOutputSample
/ 8);
230 // Let the other end know which buffer we just filled. The buffer index is
231 // used to ensure the other end is getting the buffer it expects. For more
232 // details on how this works see AudioSyncReader::WaitUntilDataIsReady().
233 size_t bytes_sent
= socket_
->Send(&buffer_index_
, sizeof(buffer_index_
));
234 if (bytes_sent
!= sizeof(buffer_index_
))