Merge Chromium + Blink git repositories
[chromium-blink-merge.git] / media / capture / webm_muxer_unittest.cc
blobbde9b4fb49a6d398240d17bb34f220f09bd84b48
1 // Copyright 2015 The Chromium Authors. All rights reserved.
2 // Use of this source code is governed by a BSD-style license that can be
3 // found in the LICENSE file.
5 #include "base/bind.h"
6 #include "base/location.h"
7 #include "base/macros.h"
8 #include "base/memory/ref_counted.h"
9 #include "base/memory/scoped_ptr.h"
10 #include "media/base/video_frame.h"
11 #include "media/capture/webm_muxer.h"
12 #include "testing/gmock/include/gmock/gmock.h"
13 #include "testing/gtest/include/gtest/gtest.h"
15 using ::testing::_;
16 using ::testing::AtLeast;
17 using ::testing::Mock;
18 using ::testing::WithArgs;
20 namespace media {
22 // Dummy interface class to be able to MOCK its only function below.
23 class EventHandlerInterface {
24 public:
25 virtual void WriteCallback(const base::StringPiece& encoded_data) = 0;
26 virtual ~EventHandlerInterface() {}
29 class WebmMuxerTest : public testing::Test, public EventHandlerInterface {
30 public:
31 WebmMuxerTest()
32 : webm_muxer_(base::Bind(&WebmMuxerTest::WriteCallback,
33 base::Unretained(this))),
34 last_encoded_length_(0),
35 accumulated_position_(0) {
36 EXPECT_EQ(webm_muxer_.Position(), 0);
37 const mkvmuxer::int64 kRandomNewPosition = 333;
38 EXPECT_EQ(webm_muxer_.Position(kRandomNewPosition), -1);
39 EXPECT_FALSE(webm_muxer_.Seekable());
42 MOCK_METHOD1(WriteCallback, void(const base::StringPiece&));
44 void SaveEncodedDataLen(const base::StringPiece& encoded_data) {
45 last_encoded_length_ = encoded_data.size();
46 accumulated_position_ += encoded_data.size();
49 mkvmuxer::int64 GetWebmMuxerPosition() const {
50 return webm_muxer_.Position();
53 mkvmuxer::Segment::Mode GetWebmSegmentMode() const {
54 return webm_muxer_.segment_.mode();
57 mkvmuxer::int32 WebmMuxerWrite(const void* buf, mkvmuxer::uint32 len) {
58 return webm_muxer_.Write(buf, len);
61 WebmMuxer webm_muxer_;
63 size_t last_encoded_length_;
64 int64_t accumulated_position_;
66 private:
67 DISALLOW_COPY_AND_ASSIGN(WebmMuxerTest);
70 // Checks that the WriteCallback is called with appropriate params when
71 // WebmMuxer::Write() method is called.
72 TEST_F(WebmMuxerTest, Write) {
73 const base::StringPiece encoded_data("abcdefghijklmnopqrstuvwxyz");
75 EXPECT_CALL(*this, WriteCallback(encoded_data));
76 WebmMuxerWrite(encoded_data.data(), encoded_data.size());
78 EXPECT_EQ(GetWebmMuxerPosition(), static_cast<int64_t>(encoded_data.size()));
81 // This test sends two frames and checks that the WriteCallback is called with
82 // appropriate params in both cases.
83 TEST_F(WebmMuxerTest, OnEncodedVideoTwoFrames) {
84 const gfx::Size frame_size(160, 80);
85 const scoped_refptr<VideoFrame> video_frame =
86 VideoFrame::CreateBlackFrame(frame_size);
87 const base::StringPiece encoded_data("abcdefghijklmnopqrstuvwxyz");
89 EXPECT_CALL(*this, WriteCallback(_))
90 .Times(AtLeast(1))
91 .WillRepeatedly(WithArgs<0>(
92 Invoke(this, &WebmMuxerTest::SaveEncodedDataLen)));
93 webm_muxer_.OnEncodedVideo(video_frame,
94 encoded_data,
95 base::TimeTicks::Now(),
96 false /* keyframe */);
98 // First time around WriteCallback() is pinged a number of times to write the
99 // Matroska header, but at the end it dumps |encoded_data|.
100 EXPECT_EQ(last_encoded_length_, encoded_data.size());
101 EXPECT_EQ(GetWebmMuxerPosition(), accumulated_position_);
102 EXPECT_GE(GetWebmMuxerPosition(), static_cast<int64_t>(last_encoded_length_));
103 EXPECT_EQ(GetWebmSegmentMode(), mkvmuxer::Segment::kLive);
105 const int64_t begin_of_second_block = accumulated_position_;
106 EXPECT_CALL(*this, WriteCallback(_))
107 .Times(AtLeast(1))
108 .WillRepeatedly(WithArgs<0>(
109 Invoke(this, &WebmMuxerTest::SaveEncodedDataLen)));
110 webm_muxer_.OnEncodedVideo(video_frame,
111 encoded_data,
112 base::TimeTicks::Now(),
113 false /* keyframe */);
115 // The second time around the callbacks should include a SimpleBlock header,
116 // namely the track index, a timestamp and a flags byte, for a total of 6B.
117 EXPECT_EQ(last_encoded_length_, encoded_data.size());
118 EXPECT_EQ(GetWebmMuxerPosition(), accumulated_position_);
119 const uint32_t kSimpleBlockSize = 6u;
120 EXPECT_EQ(static_cast<int64_t>(begin_of_second_block + kSimpleBlockSize +
121 encoded_data.size()),
122 accumulated_position_);
125 } // namespace media