summaryrefslogtreecommitdiffstats
path: root/webkit/media/video_renderer_impl_unittest.cc
blob: ba9573f5c12d7de2f0f11ceca1349eccec2f532d (plain)
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
// Copyright (c) 2011 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 "base/bind.h"
#include "base/synchronization/waitable_event.h"
#include "base/test/test_timeouts.h"
#include "media/base/limits.h"
#include "media/base/mock_filters.h"
#include "media/base/mock_filter_host.h"
#include "testing/gmock/include/gmock/gmock.h"
#include "testing/gtest/include/gtest/gtest.h"
#include "third_party/skia/include/core/SkCanvas.h"
#include "third_party/skia/include/core/SkDevice.h"
#include "webkit/media/video_renderer_impl.h"

using media::PipelineStatistics;
using media::PipelineStatus;
using media::VideoDecoder;
using media::VideoFrame;

using testing::_;
using testing::NiceMock;
using testing::Return;
using testing::ReturnRef;

namespace webkit_media {

static const int64 kFrameDuration = 10;
static const int kWidth = 320;
static const int kHeight = 240;
static const gfx::Size kNaturalSize(kWidth, kHeight);

class VideoRendererImplTest : public testing::Test {
 public:
  VideoRendererImplTest()
      : timeout_(base::TimeDelta::FromMilliseconds(
            TestTimeouts::action_timeout_ms())),
        cv_(&lock_),
        prerolled_(false) {
    // Bind callbacks with various values for TestPainting() tests.
    natural_frame_cb_ = base::Bind(
        &VideoRendererImplTest::DeliverFrame, base::Unretained(this),
        kWidth, kHeight);
    larger_frame_cb_ = base::Bind(
        &VideoRendererImplTest::DeliverFrame, base::Unretained(this),
        kWidth * 2, kHeight * 2);
    smaller_frame_cb_ = base::Bind(
        &VideoRendererImplTest::DeliverFrame, base::Unretained(this),
        kWidth / 2, kHeight / 2);
    fast_paint_cb_ = base::Bind(
        &VideoRendererImplTest::Paint, base::Unretained(this), true);
    slow_paint_cb_ = base::Bind(
        &VideoRendererImplTest::Paint, base::Unretained(this), false);

    // Forward all time requests to lock-protected getter.
    ON_CALL(host_, GetTime())
        .WillByDefault(Invoke(this, &VideoRendererImplTest::GetTime));

    decoder_ = new media::MockVideoDecoder();
    EXPECT_CALL(*decoder_, natural_size())
        .WillOnce(ReturnRef(kNaturalSize));

    // Initialize the renderer.
    base::WaitableEvent event(false, false);
    renderer_ = new VideoRendererImpl(base::Bind(
        &VideoRendererImplTest::OnPaint, base::Unretained(this)));
    renderer_->set_host(&host_);
    renderer_->SetPlaybackRate(1.0f);
    renderer_->Initialize(
        decoder_,
        base::Bind(&base::WaitableEvent::Signal, base::Unretained(&event)),
        base::Bind(&VideoRendererImplTest::OnStatistics,
                   base::Unretained(this)));
    CHECK(event.TimedWait(timeout_)) << "Timed out waiting to initialize.";
  }

  virtual ~VideoRendererImplTest() {
    // Stop the renderer.
    base::WaitableEvent event(false, false);
    renderer_->Stop(
        base::Bind(&base::WaitableEvent::Signal, base::Unretained(&event)));
    CHECK(event.TimedWait(timeout_)) << "Timed out waiting to stop.";
  }

  typedef base::Callback<void(int64)> DeliverCB;
  void TestPainting(const DeliverCB& deliver_first_frame,
                    const DeliverCB& deliver_remaining_frames,
                    const base::Closure& paint) {
    StartPrerolling();
    for (int i = 0; i < media::limits::kMaxVideoFrames; ++i) {
      WaitForRead();
      if (i == 0)
        deliver_first_frame.Run(i * kFrameDuration);
      else
        deliver_remaining_frames.Run(i * kFrameDuration);
    }

    // Wait for prerolling to complete and paint first frame.
    WaitForPrerolled();
    paint.Run();

    // Advance to remaining frames and paint again.
    Play();
    SetTime(kFrameDuration);
    WaitForRead();
    paint.Run();
  }

  const DeliverCB& natural_frame_cb() { return natural_frame_cb_; }
  const DeliverCB& larger_frame_cb() { return larger_frame_cb_; }
  const DeliverCB& smaller_frame_cb() { return smaller_frame_cb_; }
  const base::Closure& fast_paint_cb() { return fast_paint_cb_; }
  const base::Closure& slow_paint_cb() { return slow_paint_cb_; }

 private:
  void OnPaint() {}
  void OnStatistics(const PipelineStatistics&) {}

  void StartPrerolling() {
    EXPECT_CALL(*decoder_, Read(_))
        .WillRepeatedly(Invoke(this, &VideoRendererImplTest::FrameRequested));

    renderer_->Seek(base::TimeDelta::FromMicroseconds(0),
                    base::Bind(&VideoRendererImplTest::PrerollDone,
                               base::Unretained(this),
                               media::PIPELINE_OK));
  }

  void Play() {
    base::WaitableEvent event(false, false);
    renderer_->Play(
        base::Bind(&base::WaitableEvent::Signal, base::Unretained(&event)));
    CHECK(event.TimedWait(timeout_)) << "Timed out waiting to play.";
  }

  void SetTime(int64 timestamp) {
    base::AutoLock l(lock_);
    time_ = base::TimeDelta::FromMicroseconds(timestamp);
  }

  base::TimeDelta GetTime() {
    base::AutoLock l(lock_);
    return time_;
  }

  void WaitForRead() {
    base::AutoLock l(lock_);
    if (!read_cb_.is_null())
      return;
    cv_.TimedWait(timeout_);
    CHECK(!read_cb_.is_null()) << "Timed out waiting for read.";
  }

  void WaitForPrerolled() {
    base::AutoLock l(lock_);
    if (prerolled_)
      return;
    cv_.TimedWait(timeout_);
    CHECK(prerolled_) << "Timed out waiting for prerolled.";
  }

  void FrameRequested(const VideoDecoder::ReadCB& callback) {
    base::AutoLock l(lock_);
    CHECK(read_cb_.is_null());
    read_cb_ = callback;
    cv_.Signal();
  }

  void PrerollDone(media::PipelineStatus expected_status,
                   PipelineStatus status) {
    base::AutoLock l(lock_);
    EXPECT_EQ(status, expected_status);
    prerolled_ = true;
    cv_.Signal();
  }

  // Allocates a frame of |width| and |height| dimensions with |timestamp|
  // and delivers it to the renderer by running |read_cb_|.
  void DeliverFrame(int width, int height, int64 timestamp) {
    scoped_refptr<VideoFrame> video_frame =
        VideoFrame::CreateBlackFrame(width, height);
    video_frame->SetTimestamp(base::TimeDelta::FromMicroseconds(timestamp));
    video_frame->SetDuration(base::TimeDelta::FromMicroseconds(kFrameDuration));

    VideoDecoder::ReadCB read_cb;
    {
      base::AutoLock l(lock_);
      CHECK(!read_cb_.is_null());
      std::swap(read_cb_, read_cb);
    }

    read_cb.Run(video_frame);
  }

  // Triggers the fast painting path when |opaque| is true and the slow
  // painting path when |opaque| is false.
  void Paint(bool opaque) {
    SkDevice device(SkBitmap::kARGB_8888_Config, kWidth, kHeight, opaque);
    SkCanvas canvas(&device);
    gfx::Rect rect(0, 0, kWidth, kHeight);
    renderer_->Paint(&canvas, rect);
  }

  NiceMock<media::MockFilterHost> host_;
  scoped_refptr<VideoRendererImpl> renderer_;
  scoped_refptr<media::MockVideoDecoder> decoder_;

  base::TimeDelta timeout_;

  base::Lock lock_;
  base::ConditionVariable cv_;

  // Protected by |lock_|.
  base::TimeDelta time_;
  VideoDecoder::ReadCB read_cb_;
  bool prerolled_;

  // Prebound closures for use with TestPainting().
  DeliverCB natural_frame_cb_;
  DeliverCB larger_frame_cb_;
  DeliverCB smaller_frame_cb_;
  base::Closure slow_paint_cb_;
  base::Closure fast_paint_cb_;

  DISALLOW_COPY_AND_ASSIGN(VideoRendererImplTest);
};

TEST_F(VideoRendererImplTest, FastPaint_Natural) {
  TestPainting(natural_frame_cb(), natural_frame_cb(), fast_paint_cb());
}

TEST_F(VideoRendererImplTest, SlowPaint_Natural) {
  TestPainting(natural_frame_cb(), natural_frame_cb(), slow_paint_cb());
}

TEST_F(VideoRendererImplTest, FastPaint_Larger) {
  TestPainting(natural_frame_cb(), larger_frame_cb(), fast_paint_cb());
}

TEST_F(VideoRendererImplTest, SlowPaint_Larger) {
  TestPainting(natural_frame_cb(), larger_frame_cb(), slow_paint_cb());
}

TEST_F(VideoRendererImplTest, FastPaint_Smaller) {
  TestPainting(natural_frame_cb(), smaller_frame_cb(), fast_paint_cb());
}

TEST_F(VideoRendererImplTest, SlowPaint_Smaller) {
  TestPainting(natural_frame_cb(), smaller_frame_cb(), slow_paint_cb());
}

}  // namespace webkit_media