1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
|
// Copyright (c) 2010 The Chromium Authors. All rights reserved.
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.
#include "webkit/glue/plugins/pepper_audio.h"
#include "base/logging.h"
#include "third_party/ppapi/c/dev/ppb_audio_dev.h"
#include "third_party/ppapi/c/dev/ppb_audio_trusted_dev.h"
namespace pepper {
namespace {
// PPB_AudioConfig functions
PP_Resource CreateStereo16bit(PP_Module module_id, uint32_t sample_rate,
uint32_t sample_frame_count) {
PluginModule* module = PluginModule::FromPPModule(module_id);
if (!module)
return 0;
scoped_refptr<AudioConfig> config(new AudioConfig(module, sample_rate,
sample_frame_count));
return config->GetReference();
}
uint32_t GetSampleRate(PP_Resource config_id) {
scoped_refptr<AudioConfig> config = Resource::GetAs<AudioConfig>(config_id);
return config ? config->sample_rate() : 0;
}
uint32_t GetSampleFrameCount(PP_Resource config_id) {
scoped_refptr<AudioConfig> config = Resource::GetAs<AudioConfig>(config_id);
return config ? config->sample_frame_count() : 0;
}
// PPB_Audio functions
PP_Resource Create(PP_Instance instance_id, PP_Resource config_id,
PPB_Audio_Callback callback, void* user_data) {
PluginInstance* instance = PluginInstance::FromPPInstance(instance_id);
if (!instance)
return 0;
// TODO(neb): Require callback to be present for untrusted plugins.
scoped_refptr<Audio> audio(new Audio(instance->module()));
if (!audio->Init(instance->delegate(), config_id, callback, user_data))
return 0;
return audio->GetReference();
}
PP_Resource GetCurrentConfiguration(PP_Resource audio_id) {
scoped_refptr<Audio> audio = Resource::GetAs<Audio>(audio_id);
return audio ? audio->GetCurrentConfiguration() : 0;
}
bool StartPlayback(PP_Resource audio_id) {
scoped_refptr<Audio> audio = Resource::GetAs<Audio>(audio_id);
return audio ? audio->StartPlayback() : false;
}
bool StopPlayback(PP_Resource audio_id) {
scoped_refptr<Audio> audio = Resource::GetAs<Audio>(audio_id);
return audio ? audio->StopPlayback() : false;
}
// PPB_AudioTrusted functions
PP_Resource GetBuffer(PP_Resource audio_id) {
// TODO(neb): Implement me!
return 0;
}
int GetOSDescriptor(PP_Resource audio_id) {
// TODO(neb): Implement me!
return -1;
}
const PPB_AudioConfig_Dev ppb_audioconfig = {
&CreateStereo16bit,
&GetSampleRate,
&GetSampleFrameCount
};
const PPB_Audio_Dev ppb_audio = {
&Create,
&GetCurrentConfiguration,
&StartPlayback,
&StopPlayback,
};
const PPB_AudioTrusted_Dev ppb_audiotrusted = {
&GetBuffer,
&GetOSDescriptor
};
} // namespace
AudioConfig::AudioConfig(PluginModule* module, int32_t sample_rate,
int32_t sample_frame_count)
: Resource(module),
sample_rate_(sample_rate),
sample_frame_count_(sample_frame_count) {
}
const PPB_AudioConfig_Dev* AudioConfig::GetInterface() {
return &ppb_audioconfig;
}
AudioConfig* AudioConfig::AsAudioConfig() {
return this;
}
Audio::Audio(PluginModule* module)
: Resource(module),
socket_(NULL),
shared_memory_(NULL),
shared_memory_size_(0),
callback_(NULL),
user_data_(NULL) {
}
Audio::~Audio() {
// Calling ShutDown() makes sure StreamCreated cannot be called anymore.
audio_->ShutDown();
// Closing the socket causes the thread to exit - wait for it.
socket_->Close();
if (audio_thread_.get()) {
audio_thread_->Join();
audio_thread_.reset();
}
// Shared memory destructor will unmap the memory automatically.
}
const PPB_Audio_Dev* Audio::GetInterface() {
return &ppb_audio;
}
const PPB_AudioTrusted_Dev* Audio::GetTrustedInterface() {
return &ppb_audiotrusted;
}
Audio* Audio::AsAudio() {
return this;
}
bool Audio::Init(PluginDelegate* plugin_delegate, PP_Resource config_id,
PPB_Audio_Callback callback, void* user_data) {
CHECK(!audio_.get());
config_ = Resource::GetAs<AudioConfig>(config_id);
if (!config_)
return false;
callback_ = callback;
user_data_ = user_data;
// When the stream is created, we'll get called back in StreamCreated().
audio_.reset(plugin_delegate->CreateAudio(config_->sample_rate(),
config_->sample_frame_count(),
this));
return audio_.get() != NULL;
}
void Audio::StreamCreated(base::SharedMemoryHandle shared_memory_handle,
size_t shared_memory_size,
base::SyncSocket::Handle socket_handle) {
socket_.reset(new base::SyncSocket(socket_handle));
shared_memory_.reset(new base::SharedMemory(shared_memory_handle, false));
shared_memory_size_ = shared_memory_size;
if (callback_) {
shared_memory_->Map(shared_memory_size_);
audio_thread_.reset(new base::DelegateSimpleThread(this,
"plugin_audio_thread"));
audio_thread_->Start();
}
}
void Audio::Run() {
int pending_data;
void* buffer = shared_memory_->memory();
while (sizeof(pending_data) ==
socket_->Receive(&pending_data, sizeof(pending_data)) &&
pending_data >= 0) {
callback_(buffer, user_data_);
}
}
} // namespace pepper
|