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
|
// 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 "media/video/capture/fake_video_capture_device.h"
#include <string>
#include "base/bind.h"
#include "base/memory/scoped_ptr.h"
#include "base/stringprintf.h"
#include "media/audio/fake_audio_input_stream.h"
#include "third_party/skia/include/core/SkBitmap.h"
#include "third_party/skia/include/core/SkCanvas.h"
#include "third_party/skia/include/core/SkPaint.h"
namespace media {
static const int kFakeCaptureTimeoutMs = 50;
static const int kFakeCaptureBeepCycle = 20; // Visual beep every 1s.
enum { kNumberOfFakeDevices = 2 };
bool FakeVideoCaptureDevice::fail_next_create_ = false;
void FakeVideoCaptureDevice::GetDeviceNames(Names* const device_names) {
// Empty the name list.
device_names->erase(device_names->begin(), device_names->end());
for (int n = 0; n < kNumberOfFakeDevices; n++) {
Name name;
name.unique_id = StringPrintf("/dev/video%d", n);
name.device_name = StringPrintf("fake_device_%d", n);
device_names->push_back(name);
}
}
VideoCaptureDevice* FakeVideoCaptureDevice::Create(const Name& device_name) {
if (fail_next_create_) {
fail_next_create_ = false;
return NULL;
}
for (int n = 0; n < kNumberOfFakeDevices; ++n) {
std::string possible_id = StringPrintf("/dev/video%d", n);
if (device_name.unique_id.compare(possible_id) == 0) {
return new FakeVideoCaptureDevice(device_name);
}
}
return NULL;
}
void FakeVideoCaptureDevice::SetFailNextCreate() {
fail_next_create_ = true;
}
FakeVideoCaptureDevice::FakeVideoCaptureDevice(const Name& device_name)
: device_name_(device_name),
observer_(NULL),
state_(kIdle),
capture_thread_("CaptureThread"),
frame_size_(0),
frame_count_(0),
frame_width_(0),
frame_height_(0) {
}
FakeVideoCaptureDevice::~FakeVideoCaptureDevice() {
// Check if the thread is running.
// This means that the device have not been DeAllocated properly.
DCHECK(!capture_thread_.IsRunning());
}
void FakeVideoCaptureDevice::Allocate(int width,
int height,
int frame_rate,
EventHandler* observer) {
if (state_ != kIdle) {
return; // Wrong state.
}
observer_ = observer;
VideoCaptureCapability current_settings;
current_settings.color = VideoCaptureCapability::kI420;
current_settings.expected_capture_delay = 0;
current_settings.interlaced = false;
if (width > 320) { // VGA
current_settings.width = 640;
current_settings.height = 480;
current_settings.frame_rate = 30;
} else { // QVGA
current_settings.width = 320;
current_settings.height = 240;
current_settings.frame_rate = 30;
}
size_t fake_frame_size =
current_settings.width * current_settings.height * 3 / 2;
fake_frame_.reset(new uint8[fake_frame_size]);
memset(fake_frame_.get(), 0, fake_frame_size);
frame_size_ = fake_frame_size;
frame_width_ = current_settings.width;
frame_height_ = current_settings.height;
state_ = kAllocated;
observer_->OnFrameInfo(current_settings);
}
void FakeVideoCaptureDevice::Start() {
if (state_ != kAllocated) {
return; // Wrong state.
}
state_ = kCapturing;
capture_thread_.Start();
capture_thread_.message_loop()->PostTask(
FROM_HERE,
base::Bind(&FakeVideoCaptureDevice::OnCaptureTask,
base::Unretained(this)));
}
void FakeVideoCaptureDevice::Stop() {
if (state_ != kCapturing) {
return; // Wrong state.
}
capture_thread_.Stop();
state_ = kAllocated;
}
void FakeVideoCaptureDevice::DeAllocate() {
if (state_ != kAllocated && state_ != kCapturing) {
return; // Wrong state.
}
capture_thread_.Stop();
state_ = kIdle;
}
const VideoCaptureDevice::Name& FakeVideoCaptureDevice::device_name() {
return device_name_;
}
void FakeVideoCaptureDevice::OnCaptureTask() {
if (state_ != kCapturing) {
return;
}
memset(fake_frame_.get(), 0, frame_size_);
SkBitmap bitmap;
bitmap.setConfig(SkBitmap::kA8_Config, frame_width_, frame_height_,
frame_width_);
bitmap.setPixels(fake_frame_.get());
SkCanvas canvas(bitmap);
// Draw a sweeping circle to show an animation.
int radius = std::min(frame_width_, frame_height_) / 4;
SkRect rect = SkRect::MakeXYWH(
frame_width_ / 2 - radius, frame_height_ / 2 - radius,
2 * radius, 2 * radius);
SkPaint paint;
paint.setStyle(SkPaint::kFill_Style);
// Only Y plane is being drawn and this gives 50% grey on the Y
// plane. The result is a light green color in RGB space.
paint.setAlpha(128);
int end_angle = (frame_count_ % kFakeCaptureBeepCycle * 360) /
kFakeCaptureBeepCycle;
if (!end_angle)
end_angle = 360;
canvas.drawArc(rect, 0, end_angle, true, paint);
// Draw current time.
int elapsed_ms = kFakeCaptureTimeoutMs * frame_count_;
int milliseconds = elapsed_ms % 1000;
int seconds = (elapsed_ms / 1000) % 60;
int minutes = (elapsed_ms / 1000 / 60) % 60;
int hours = (elapsed_ms / 1000 / 60 / 60) % 60;
std::string time_string =
base::StringPrintf("%d:%02d:%02d:%03d %d", hours, minutes,
seconds, milliseconds, frame_count_);
canvas.scale(3, 3);
canvas.drawText(time_string.data(), time_string.length(), 30, 20,
paint);
if (frame_count_ % kFakeCaptureBeepCycle == 0) {
// Generate a synchronized beep sound if there is one audio input
// stream created.
FakeAudioInputStream::BeepOnce();
}
frame_count_++;
// Give the captured frame to the observer.
observer_->OnIncomingCapturedFrame(
fake_frame_.get(), frame_size_, base::Time::Now(), 0, false, false);
// Reschedule next CaptureTask.
capture_thread_.message_loop()->PostDelayedTask(
FROM_HERE,
base::Bind(&FakeVideoCaptureDevice::OnCaptureTask,
base::Unretained(this)),
base::TimeDelta::FromMilliseconds(kFakeCaptureTimeoutMs));
}
} // namespace media
|