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.
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/capture/webm_muxer.h"
11 #include "testing/gmock/include/gmock/gmock.h"
12 #include "testing/gtest/include/gtest/gtest.h"
15 using ::testing::AtLeast
;
16 using ::testing::Mock
;
17 using ::testing::WithArgs
;
21 // Dummy interface class to be able to MOCK its only function below.
22 class EventHandlerInterface
{
24 virtual void WriteCallback(const base::StringPiece
& encoded_data
) = 0;
25 virtual ~EventHandlerInterface() {}
28 class WebmMuxerTest
: public testing::Test
, public EventHandlerInterface
{
31 : webm_muxer_(base::Bind(&WebmMuxerTest::WriteCallback
,
32 base::Unretained(this))),
33 last_encoded_length_(0),
34 accumulated_position_(0) {
35 EXPECT_EQ(webm_muxer_
.Position(), 0);
36 EXPECT_FALSE(webm_muxer_
.Seekable());
37 EXPECT_EQ(webm_muxer_
.segment_
.mode(), mkvmuxer::Segment::kLive
);
40 MOCK_METHOD1(WriteCallback
, void(const base::StringPiece
&));
42 void SaveEncodedDataLen(const base::StringPiece
& encoded_data
) {
43 last_encoded_length_
= encoded_data
.size();
44 accumulated_position_
+= encoded_data
.size();
47 mkvmuxer::int64
GetWebmMuxerPosition() const {
48 return webm_muxer_
.Position();
51 const mkvmuxer::Segment
& GetWebmMuxerSegment() const {
52 return webm_muxer_
.segment_
;
55 mkvmuxer::int32
WebmMuxerWrite(const void* buf
, mkvmuxer::uint32 len
) {
56 return webm_muxer_
.Write(buf
, len
);
59 WebmMuxer webm_muxer_
;
61 size_t last_encoded_length_
;
62 int64_t accumulated_position_
;
65 DISALLOW_COPY_AND_ASSIGN(WebmMuxerTest
);
68 // Checks that AddVideoTrack adds a Track.
69 TEST_F(WebmMuxerTest
, AddVideoTrack
) {
70 const uint64_t track_number
= webm_muxer_
.AddVideoTrack(gfx::Size(320, 240),
72 EXPECT_TRUE(GetWebmMuxerSegment().GetTrackByNumber(track_number
));
75 // Checks that the WriteCallback is called with appropriate params when
76 // WebmMuxer::Write() method is called.
77 TEST_F(WebmMuxerTest
, Write
) {
78 const base::StringPiece
encoded_data("abcdefghijklmnopqrstuvwxyz");
80 EXPECT_CALL(*this, WriteCallback(encoded_data
));
81 WebmMuxerWrite(encoded_data
.data(), encoded_data
.size());
83 EXPECT_EQ(GetWebmMuxerPosition(), static_cast<int64_t>(encoded_data
.size()));
86 // This test sends two frames and checks that the WriteCallback is called with
87 // appropriate params in both cases.
88 TEST_F(WebmMuxerTest
, OnEncodedVideoNormalFrames
) {
89 const base::StringPiece
encoded_data("abcdefghijklmnopqrstuvwxyz");
90 const uint64_t track_number
= webm_muxer_
.AddVideoTrack(gfx::Size(320, 240),
93 EXPECT_CALL(*this, WriteCallback(_
))
95 .WillRepeatedly(WithArgs
<0>(
96 Invoke(this, &WebmMuxerTest::SaveEncodedDataLen
)));
97 webm_muxer_
.OnEncodedVideo(track_number
,
99 base::TimeDelta::FromMicroseconds(0),
100 false /* keyframe */);
102 // First time around WriteCallback() is pinged a number of times to write the
103 // Matroska header, but at the end it dumps |encoded_data|.
104 EXPECT_EQ(last_encoded_length_
, encoded_data
.size());
105 EXPECT_EQ(GetWebmMuxerPosition(), accumulated_position_
);
106 EXPECT_GE(GetWebmMuxerPosition(), static_cast<int64_t>(last_encoded_length_
));
108 const int64_t begin_of_second_block
= accumulated_position_
;
109 EXPECT_CALL(*this, WriteCallback(_
))
111 .WillRepeatedly(WithArgs
<0>(
112 Invoke(this, &WebmMuxerTest::SaveEncodedDataLen
)));
113 webm_muxer_
.OnEncodedVideo(track_number
,
115 base::TimeDelta::FromMicroseconds(1),
116 false /* keyframe */);
118 // The second time around the callbacks should include a SimpleBlock header,
119 // namely the track index, a timestamp and a flags byte, for a total of 6B.
120 EXPECT_EQ(last_encoded_length_
, encoded_data
.size());
121 EXPECT_EQ(GetWebmMuxerPosition(), accumulated_position_
);
122 const uint32_t kSimpleBlockSize
= 6u;
123 EXPECT_EQ(static_cast<int64_t>(begin_of_second_block
+ kSimpleBlockSize
+
124 encoded_data
.size()),
125 accumulated_position_
);