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
|
// Copyright (c) 2013 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/screen/screen_capture_device.h"
#include "base/basictypes.h"
#include "base/sequenced_task_runner.h"
#include "base/synchronization/waitable_event.h"
#include "base/test/test_timeouts.h"
#include "base/threading/sequenced_worker_pool.h"
#include "base/time.h"
#include "media/video/capture/screen/screen_capture_data.h"
#include "testing/gmock/include/gmock/gmock.h"
#include "testing/gtest/include/gtest/gtest.h"
using ::testing::_;
using ::testing::DoAll;
using ::testing::InvokeWithoutArgs;
using ::testing::SaveArg;
namespace media {
namespace {
const int kTestFrameWidth1 = 100;
const int kTestFrameHeight1 = 100;
const int kTestFrameWidth2 = 200;
const int kTestFrameHeight2 = 150;
const int kBufferSize = kTestFrameWidth2 * kTestFrameHeight2 * 4;
const int kFrameRate = 30;
class MockFrameObserver : public VideoCaptureDevice::EventHandler {
public:
MOCK_METHOD0(ReserveOutputBuffer, scoped_refptr<media::VideoFrame>());
MOCK_METHOD0(OnError, void());
MOCK_METHOD1(OnFrameInfo, void(const VideoCaptureCapability& info));
MOCK_METHOD6(OnIncomingCapturedFrame, void(const uint8* data,
int length,
base::Time timestamp,
int rotation,
bool flip_vert,
bool flip_horiz));
MOCK_METHOD2(OnIncomingCapturedVideoFrame,
void(const scoped_refptr<media::VideoFrame>& frame,
base::Time timestamp));
};
// TODO(sergeyu): Move this to a separate file where it can be reused.
class FakeScreenCapturer : public ScreenCapturer {
public:
FakeScreenCapturer()
: delegate_(NULL),
frame_index_(0) {
buffer_.reset(new uint8[kBufferSize]);
frames_[0] = new ScreenCaptureData(
buffer_.get(), kTestFrameWidth1 * ScreenCaptureData::kBytesPerPixel,
SkISize::Make(kTestFrameWidth1, kTestFrameHeight1));
frames_[1] = new ScreenCaptureData(
buffer_.get(), kTestFrameWidth2 * ScreenCaptureData::kBytesPerPixel,
SkISize::Make(kTestFrameWidth2, kTestFrameHeight2));
}
virtual ~FakeScreenCapturer() {}
// VideoFrameCapturer interface.
virtual void Start(Delegate* delegate) OVERRIDE {
delegate_ = delegate;
}
virtual void CaptureFrame() OVERRIDE {
scoped_refptr<ScreenCaptureData> frame =
frames_[frame_index_ % arraysize(frames_)];
frame_index_++;
delegate_->OnCaptureCompleted(frame);
}
private:
Delegate* delegate_;
scoped_ptr<uint8[]> buffer_;
scoped_refptr<ScreenCaptureData> frames_[2];
int frame_index_;
};
class ScreenCaptureDeviceTest : public testing::Test {
public:
virtual void SetUp() OVERRIDE {
worker_pool_ = new base::SequencedWorkerPool(3, "TestCaptureThread");
}
protected:
scoped_refptr<base::SequencedWorkerPool> worker_pool_;
};
} // namespace
TEST_F(ScreenCaptureDeviceTest, Capture) {
ScreenCaptureDevice capture_device(
worker_pool_->GetSequencedTaskRunner(worker_pool_->GetSequenceToken()));
VideoCaptureCapability caps;
base::WaitableEvent done_event(false, false);
int frame_size;
MockFrameObserver frame_observer;
EXPECT_CALL(frame_observer, OnFrameInfo(_))
.WillOnce(SaveArg<0>(&caps));
EXPECT_CALL(frame_observer, OnError())
.Times(0);
EXPECT_CALL(frame_observer, OnIncomingCapturedFrame(_, _, _, _, _, _))
.WillRepeatedly(DoAll(
SaveArg<1>(&frame_size),
InvokeWithoutArgs(&done_event, &base::WaitableEvent::Signal)));
capture_device.Allocate(640, 480, kFrameRate, &frame_observer);
capture_device.Start();
EXPECT_TRUE(done_event.TimedWait(TestTimeouts::action_max_timeout()));
capture_device.Stop();
capture_device.DeAllocate();
EXPECT_GT(caps.width, 0);
EXPECT_GT(caps.height, 0);
EXPECT_EQ(kFrameRate, caps.frame_rate);
EXPECT_EQ(VideoCaptureCapability::kARGB, caps.color);
EXPECT_FALSE(caps.interlaced);
EXPECT_EQ(caps.width * caps.height * 4, frame_size);
}
// Test that screen capturer can handle resolution change without crashing.
TEST_F(ScreenCaptureDeviceTest, ScreenResolutionChange) {
FakeScreenCapturer* mock_capturer = new FakeScreenCapturer();
ScreenCaptureDevice capture_device(
worker_pool_->GetSequencedTaskRunner(worker_pool_->GetSequenceToken()));
capture_device.SetScreenCapturerForTest(
scoped_ptr<ScreenCapturer>(mock_capturer));
VideoCaptureCapability caps;
base::WaitableEvent done_event(false, false);
int frame_size;
MockFrameObserver frame_observer;
EXPECT_CALL(frame_observer, OnFrameInfo(_))
.WillOnce(SaveArg<0>(&caps));
EXPECT_CALL(frame_observer, OnError())
.Times(0);
EXPECT_CALL(frame_observer, OnIncomingCapturedFrame(_, _, _, _, _, _))
.WillRepeatedly(DoAll(
SaveArg<1>(&frame_size),
InvokeWithoutArgs(&done_event, &base::WaitableEvent::Signal)));
capture_device.Allocate(640, 480, kFrameRate, &frame_observer);
capture_device.Start();
// Capture first frame.
EXPECT_TRUE(done_event.TimedWait(TestTimeouts::action_max_timeout()));
done_event.Reset();
// Capture second frame.
EXPECT_TRUE(done_event.TimedWait(TestTimeouts::action_max_timeout()));
capture_device.Stop();
capture_device.DeAllocate();
EXPECT_EQ(kTestFrameWidth1, caps.width);
EXPECT_EQ(kTestFrameHeight1, caps.height);
EXPECT_EQ(kFrameRate, caps.frame_rate);
EXPECT_EQ(VideoCaptureCapability::kARGB, caps.color);
EXPECT_FALSE(caps.interlaced);
EXPECT_EQ(caps.width * caps.height * 4, frame_size);
}
} // namespace media
|