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
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
|
// Copyright (c) 2012 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 <stdlib.h>
#include <string.h>
#include <algorithm>
#include <limits>
#include <vector>
#include "ppapi/cpp/audio_config.h"
#include "ppapi/cpp/dev/audio_input_dev.h"
#include "ppapi/cpp/dev/device_ref_dev.h"
#include "ppapi/cpp/graphics_2d.h"
#include "ppapi/cpp/image_data.h"
#include "ppapi/cpp/instance.h"
#include "ppapi/cpp/logging.h"
#include "ppapi/cpp/module.h"
#include "ppapi/cpp/rect.h"
#include "ppapi/cpp/size.h"
#include "ppapi/utility/completion_callback_factory.h"
#include "ppapi/utility/threading/lock.h"
// When compiling natively on Windows, PostMessage can be #define-d to
// something else.
#ifdef PostMessage
#undef PostMessage
#endif
namespace {
// This sample frequency is guaranteed to work.
const PP_AudioSampleRate kSampleFrequency = PP_AUDIOSAMPLERATE_44100;
const uint32_t kSampleCount = 1024;
const uint32_t kChannelCount = 1;
const char* const kDelimiter = "#__#";
} // namespace
class MyInstance : public pp::Instance {
public:
explicit MyInstance(PP_Instance instance)
: pp::Instance(instance),
callback_factory_(this),
sample_count_(0),
channel_count_(0),
samples_(NULL),
latency_(0),
timer_interval_(0),
pending_paint_(false),
waiting_for_flush_completion_(false) {
}
virtual ~MyInstance() {
device_detector_.MonitorDeviceChange(NULL, NULL);
audio_input_.Close();
// The audio input thread has exited before the previous call returned, so
// it is safe to do so now.
delete[] samples_;
}
virtual bool Init(uint32_t argc, const char* argn[], const char* argv[]) {
sample_count_ = pp::AudioConfig::RecommendSampleFrameCount(this,
kSampleFrequency,
kSampleCount);
PP_DCHECK(sample_count_ > 0);
channel_count_ = kChannelCount;
samples_ = new int16_t[sample_count_ * channel_count_];
memset(samples_, 0, sample_count_ * channel_count_ * sizeof(int16_t));
device_detector_ = pp::AudioInput_Dev(this);
// Try to ensure that we pick up a new set of samples between each
// timer-generated repaint.
timer_interval_ = (sample_count_ * 1000) / kSampleFrequency + 5;
ScheduleNextTimer();
return true;
}
virtual void DidChangeView(const pp::Rect& position, const pp::Rect& clip) {
if (position.size() == size_)
return;
size_ = position.size();
device_context_ = pp::Graphics2D(this, size_, false);
if (!BindGraphics(device_context_))
return;
Paint();
}
virtual void HandleMessage(const pp::Var& message_data) {
if (message_data.is_string()) {
std::string event = message_data.AsString();
if (event == "PageInitialized") {
int32_t result = device_detector_.MonitorDeviceChange(
&MyInstance::MonitorDeviceChangeCallback, this);
if (result != PP_OK)
PostMessage(pp::Var("MonitorDeviceChangeFailed"));
pp::CompletionCallbackWithOutput<std::vector<pp::DeviceRef_Dev> >
callback = callback_factory_.NewCallbackWithOutput(
&MyInstance::EnumerateDevicesFinished);
result = device_detector_.EnumerateDevices(callback);
if (result != PP_OK_COMPLETIONPENDING)
PostMessage(pp::Var("EnumerationFailed"));
} else if (event == "UseDefault") {
Open(pp::DeviceRef_Dev());
} else if (event == "Stop") {
Stop();
} else if (event == "Start") {
Start();
} else if (event.find("Monitor:") == 0) {
std::string index_str = event.substr(strlen("Monitor:"));
int index = atoi(index_str.c_str());
if (index >= 0 && index < static_cast<int>(monitor_devices_.size()))
Open(monitor_devices_[index]);
else
PP_NOTREACHED();
} else if (event.find("Enumerate:") == 0) {
std::string index_str = event.substr(strlen("Enumerate:"));
int index = atoi(index_str.c_str());
if (index >= 0 && index < static_cast<int>(enumerate_devices_.size()))
Open(enumerate_devices_[index]);
else
PP_NOTREACHED();
}
}
}
private:
void ScheduleNextTimer() {
PP_DCHECK(timer_interval_ > 0);
pp::Module::Get()->core()->CallOnMainThread(
timer_interval_,
callback_factory_.NewCallback(&MyInstance::OnTimer),
0);
}
void OnTimer(int32_t) {
ScheduleNextTimer();
Paint();
}
void DidFlush(int32_t result) {
waiting_for_flush_completion_ = false;
if (pending_paint_)
Paint();
}
void Paint() {
if (waiting_for_flush_completion_) {
pending_paint_ = true;
return;
}
pending_paint_ = false;
if (size_.IsEmpty())
return; // Nothing to do.
pp::ImageData image = PaintImage(size_);
if (!image.is_null()) {
device_context_.ReplaceContents(&image);
waiting_for_flush_completion_ = true;
device_context_.Flush(
callback_factory_.NewCallback(&MyInstance::DidFlush));
}
}
pp::ImageData PaintImage(const pp::Size& size) {
pp::ImageData image(this, PP_IMAGEDATAFORMAT_BGRA_PREMUL, size, false);
if (image.is_null())
return image;
// Clear to dark grey.
for (int y = 0; y < size.height(); y++) {
for (int x = 0; x < size.width(); x++)
*image.GetAddr32(pp::Point(x, y)) = 0xff202020;
}
int mid_height = size.height() / 2;
int max_amplitude = size.height() * 4 / 10;
// Draw some lines.
for (int x = 0; x < size.width(); x++) {
*image.GetAddr32(pp::Point(x, mid_height)) = 0xff606060;
*image.GetAddr32(pp::Point(x, mid_height + max_amplitude)) = 0xff404040;
*image.GetAddr32(pp::Point(x, mid_height - max_amplitude)) = 0xff404040;
}
{
pp::AutoLock auto_lock(lock_);
// Draw the latency as a red bar at the bottom.
PP_DCHECK(latency_ >= 0);
int latency_bar_length =
latency_ < 1 ? size.width() * latency_ : size.width();
for (int x = 0; x < latency_bar_length; ++x) {
*image.GetAddr32(pp::Point(x, mid_height + max_amplitude)) = 0xffff0000;
}
// Draw our samples.
for (int x = 0, i = 0;
x < std::min(size.width(), static_cast<int>(sample_count_));
x++, i += channel_count_) {
int y = samples_[i] * max_amplitude /
(std::numeric_limits<int16_t>::max() + 1) + mid_height;
*image.GetAddr32(pp::Point(x, y)) = 0xffffffff;
}
}
return image;
}
void Open(const pp::DeviceRef_Dev& device) {
audio_input_.Close();
audio_input_ = pp::AudioInput_Dev(this);
pp::AudioConfig config = pp::AudioConfig(this,
kSampleFrequency,
sample_count_);
pp::CompletionCallback callback = callback_factory_.NewCallback(
&MyInstance::OpenFinished);
int32_t result = audio_input_.Open(device, config, CaptureCallback, this,
callback);
if (result != PP_OK_COMPLETIONPENDING)
PostMessage(pp::Var("OpenFailed"));
}
void Stop() {
if (!audio_input_.StopCapture())
PostMessage(pp::Var("StopFailed"));
}
void Start() {
if (!audio_input_.StartCapture())
PostMessage(pp::Var("StartFailed"));
}
void EnumerateDevicesFinished(int32_t result,
std::vector<pp::DeviceRef_Dev>& devices) {
if (result == PP_OK) {
enumerate_devices_.swap(devices);
std::string device_names = "Enumerate:";
for (size_t index = 0; index < enumerate_devices_.size(); ++index) {
pp::Var name = enumerate_devices_[index].GetName();
PP_DCHECK(name.is_string());
if (index != 0)
device_names += kDelimiter;
device_names += name.AsString();
}
PostMessage(pp::Var(device_names));
} else {
PostMessage(pp::Var("EnumerationFailed"));
}
}
void OpenFinished(int32_t result) {
if (result == PP_OK) {
if (!audio_input_.StartCapture())
PostMessage(pp::Var("StartFailed"));
} else {
PostMessage(pp::Var("OpenFailed"));
}
}
static void CaptureCallback(const void* samples,
uint32_t num_bytes,
PP_TimeDelta latency,
void* ctx) {
MyInstance* thiz = static_cast<MyInstance*>(ctx);
pp::AutoLock auto_lock(thiz->lock_);
thiz->latency_ = latency;
uint32_t buffer_size =
thiz->sample_count_ * thiz->channel_count_ * sizeof(int16_t);
PP_DCHECK(num_bytes <= buffer_size);
PP_DCHECK(num_bytes % (thiz->channel_count_ * sizeof(int16_t)) == 0);
memcpy(thiz->samples_, samples, num_bytes);
memset(reinterpret_cast<char*>(thiz->samples_) + num_bytes, 0,
buffer_size - num_bytes);
}
static void MonitorDeviceChangeCallback(void* user_data,
uint32_t device_count,
const PP_Resource devices[]) {
MyInstance* thiz = static_cast<MyInstance*>(user_data);
std::string device_names = "Monitor:";
thiz->monitor_devices_.clear();
thiz->monitor_devices_.reserve(device_count);
for (size_t index = 0; index < device_count; ++index) {
thiz->monitor_devices_.push_back(pp::DeviceRef_Dev(devices[index]));
pp::Var name = thiz->monitor_devices_.back().GetName();
PP_DCHECK(name.is_string());
if (index != 0)
device_names += kDelimiter;
device_names += name.AsString();
}
thiz->PostMessage(pp::Var(device_names));
}
pp::CompletionCallbackFactory<MyInstance> callback_factory_;
uint32_t sample_count_;
uint32_t channel_count_;
int16_t* samples_;
PP_TimeDelta latency_;
int32_t timer_interval_;
// Painting stuff.
pp::Size size_;
pp::Graphics2D device_context_;
bool pending_paint_;
bool waiting_for_flush_completion_;
// There is no need to have two resources to do capturing and device detecting
// separately. However, this makes the code of monitoring device change
// easier.
pp::AudioInput_Dev audio_input_;
pp::AudioInput_Dev device_detector_;
std::vector<pp::DeviceRef_Dev> enumerate_devices_;
std::vector<pp::DeviceRef_Dev> monitor_devices_;
// Protects |samples_| and |latency_|.
pp::Lock lock_;
};
class MyModule : public pp::Module {
public:
virtual pp::Instance* CreateInstance(PP_Instance instance) {
return new MyInstance(instance);
}
};
namespace pp {
// Factory function for your specialization of the Module object.
Module* CreateModule() {
return new MyModule();
}
} // namespace pp
|