OLD | NEW |
---|---|
(Empty) | |
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. | |
4 | |
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/capture/webm_muxer.h" | |
11 #include "testing/gmock/include/gmock/gmock.h" | |
12 #include "testing/gtest/include/gtest/gtest.h" | |
13 | |
14 using ::testing::_; | |
15 using ::testing::AtLeast; | |
16 using ::testing::Mock; | |
17 using ::testing::WithArgs; | |
18 | |
19 namespace media { | |
20 | |
21 // Dummy interface class to be able to MOCK its only function below. | |
22 class EventHandlerInterface { | |
23 public: | |
24 virtual void WriteCallback(const char* data, size_t len) {} | |
DaleCurtis
2015/07/21 16:52:31
= 0;
mcasas
2015/07/21 19:47:00
Done.
| |
25 virtual ~EventHandlerInterface() {} | |
26 }; | |
27 | |
28 // Mock reference counted class to EXPECT calls to WriteCallback(). | |
29 class MockWebmMuxerEventHandler | |
30 : public base::RefCounted<MockWebmMuxerEventHandler>, | |
31 public EventHandlerInterface { | |
32 public: | |
33 MockWebmMuxerEventHandler() {} | |
34 | |
35 MOCK_METHOD2(WriteCallback, void(const char* data, size_t len)); | |
36 | |
37 private: | |
38 friend class base::RefCounted<MockWebmMuxerEventHandler>; | |
39 ~MockWebmMuxerEventHandler() override {} | |
40 | |
41 DISALLOW_COPY_AND_ASSIGN(MockWebmMuxerEventHandler); | |
42 }; | |
43 | |
44 class WebmMuxerTest : public testing::Test { | |
45 public: | |
46 void SaveEncodedDataLen(size_t len) { | |
47 last_encoded_length_ = len; | |
48 accumulated_position_ += len; | |
49 } | |
50 | |
51 protected: | |
DaleCurtis
2015/07/21 16:52:31
Just leave everything public, no need to worry abo
mcasas
2015/07/21 19:46:59
Done.
| |
52 WebmMuxerTest() | |
53 : mock_handler_(new MockWebmMuxerEventHandler()), | |
54 webm_muxer_(new WebmMuxer( | |
55 base::Bind(&MockWebmMuxerEventHandler::WriteCallback, | |
DaleCurtis
2015/07/21 16:52:31
Why bind to a whole new class? Just add the MOCK_M
mcasas
2015/07/21 19:47:00
Then I'd need to use base::Unretained since this c
DaleCurtis
2015/07/21 19:52:26
That's fine, we use that all over the place and si
| |
56 mock_handler_))), | |
57 last_encoded_length_(0), | |
58 accumulated_position_(0) {} | |
59 | |
60 void SetUp() override { | |
DaleCurtis
2015/07/21 16:52:31
Choose SetUp() or a constructor, no need for both.
mcasas
2015/07/21 19:47:00
Done.
| |
61 EXPECT_EQ(webm_muxer_->Position(), 0); | |
62 EXPECT_FALSE(webm_muxer_->Seekable()); | |
63 EXPECT_EQ(webm_muxer_->segment_.mode(), mkvmuxer::Segment::kLive); | |
64 } | |
65 | |
66 mkvmuxer::int64 GetWebmMuxerPosition() const { | |
67 return webm_muxer_->Position(); | |
68 } | |
69 | |
70 mkvmuxer::Segment& GetWebmMuxerSegment() const { | |
71 return webm_muxer_->segment_; | |
72 } | |
73 | |
74 mkvmuxer::int32 WebmMuxerWrite(const void* buf, mkvmuxer::uint32 len) { | |
75 return webm_muxer_->Write(buf, len); | |
76 } | |
77 | |
78 const scoped_refptr<MockWebmMuxerEventHandler> mock_handler_; | |
DaleCurtis
2015/07/21 16:52:31
Why is this ref counted? Is this to deal with the
mcasas
2015/07/21 19:47:00
It's to Bind() the callback.
| |
79 const scoped_ptr<WebmMuxer> webm_muxer_; | |
DaleCurtis
2015/07/21 16:52:31
No scoped_ptr, just stack allocate.
mcasas
2015/07/21 19:47:00
Done.
| |
80 | |
81 size_t last_encoded_length_; | |
82 int64_t accumulated_position_; | |
83 | |
84 private: | |
85 DISALLOW_COPY_AND_ASSIGN(WebmMuxerTest); | |
86 }; | |
87 // Checks that AddVideoTrack adds a Track. | |
DaleCurtis
2015/07/21 16:52:31
Newline.
mcasas
2015/07/21 19:47:00
Done.
| |
88 TEST_F(WebmMuxerTest, AddVideoTrack) { | |
89 const uint64_t track_number = webm_muxer_->AddVideoTrack(gfx::Size(320, 240), | |
90 30.0f); | |
91 EXPECT_NE(nullptr, GetWebmMuxerSegment().GetTrackByNumber(track_number)); | |
DaleCurtis
2015/07/21 16:52:31
EXPECT_TRUE
mcasas
2015/07/21 19:47:00
Done.
| |
92 } | |
93 | |
94 // Checks that the WriteCallback is called with appropriate params when | |
95 // WebmMuxer::Write() method is called. | |
96 TEST_F(WebmMuxerTest, Write) { | |
97 const char encoded_data[] = "abcdefghijklmnopqrstuvwxyz"; | |
98 const int encoded_len = arraysize(encoded_data); | |
99 | |
100 EXPECT_CALL(*mock_handler_.get(), WriteCallback(encoded_data, encoded_len)); | |
101 WebmMuxerWrite(encoded_data, encoded_len); | |
102 | |
103 EXPECT_EQ(GetWebmMuxerPosition(), encoded_len); | |
104 } | |
105 | |
106 // This test sends an empty frame, checks that the file header is sent to the | |
107 // WriteCallback(), then sends a non-empty frame and checks that the Track | |
108 // header and a SimpleBlock are sent to the callback. | |
109 TEST_F(WebmMuxerTest, OnEncodedVideoEmptyAndNormalFrames) { | |
110 const uint64_t track_number = webm_muxer_->AddVideoTrack(gfx::Size(320, 240), | |
111 30.0f); | |
112 | |
113 EXPECT_CALL(*mock_handler_.get(), WriteCallback(_, _)) | |
114 .Times(AtLeast(1)) | |
115 .WillRepeatedly(WithArgs<1>( | |
116 Invoke(this, &WebmMuxerTest::SaveEncodedDataLen))); | |
117 const uint8_t empty_data[] = ""; | |
118 webm_muxer_->OnEncodedVideo(track_number, | |
119 empty_data, | |
120 arraysize(empty_data), | |
121 base::TimeDelta::FromMicroseconds(0), | |
122 false /* keyframe */); | |
123 EXPECT_EQ(GetWebmMuxerPosition(), accumulated_position_); | |
124 | |
125 const uint32_t begin_of_second_block = accumulated_position_; | |
126 const uint8_t encoded_data[] = "abcdefghijklmnopqrstuvwxyz"; | |
127 EXPECT_CALL(*mock_handler_.get(), WriteCallback(_, _)) | |
128 .Times(AtLeast(1)) | |
129 .WillRepeatedly(WithArgs<1>( | |
130 Invoke(this, &WebmMuxerTest::SaveEncodedDataLen))); | |
131 webm_muxer_->OnEncodedVideo(track_number, | |
132 encoded_data, | |
133 arraysize(encoded_data), | |
134 base::TimeDelta::FromMicroseconds(1), | |
135 false /* keyframe */); | |
136 EXPECT_EQ(last_encoded_length_, arraysize(encoded_data)); | |
137 EXPECT_EQ(GetWebmMuxerPosition(), accumulated_position_); | |
138 const uint32_t kTrackInfoSize = 15u; | |
139 const uint32_t kSimpleBlockSize = 6u; | |
140 EXPECT_EQ(static_cast<int64_t>(begin_of_second_block + kTrackInfoSize + | |
141 kSimpleBlockSize + arraysize(encoded_data)), | |
142 accumulated_position_); | |
143 } | |
144 | |
145 // This test sends two frames and checks that the WriteCallback is called with | |
146 // appropriate params in both cases. | |
147 TEST_F(WebmMuxerTest, OnEncodedVideoNormalFrames) { | |
148 const uint8_t encoded_data[] = "abcdefghijklmnopqrstuvwxyz"; | |
149 const uint64_t track_number = webm_muxer_->AddVideoTrack(gfx::Size(320, 240), | |
150 30.0f); | |
151 | |
152 EXPECT_CALL(*mock_handler_.get(), WriteCallback(_, _)) | |
153 .Times(AtLeast(1)) | |
154 .WillRepeatedly(WithArgs<1>( | |
155 Invoke(this, &WebmMuxerTest::SaveEncodedDataLen))); | |
156 webm_muxer_->OnEncodedVideo(track_number, | |
157 encoded_data, | |
158 arraysize(encoded_data), | |
159 base::TimeDelta::FromMicroseconds(0), | |
160 false /* keyframe */); | |
161 | |
162 // First time around WriteCallback() is pinged a number of times to write the | |
163 // Matroska header, but at the end it dumps |encoded_data|. | |
164 EXPECT_EQ(last_encoded_length_, arraysize(encoded_data)); | |
165 EXPECT_EQ(GetWebmMuxerPosition(), accumulated_position_); | |
166 EXPECT_GE(GetWebmMuxerPosition(), | |
167 static_cast<int64_t>(last_encoded_length_)); | |
168 | |
169 const int64_t begin_of_second_block = accumulated_position_; | |
170 EXPECT_CALL(*mock_handler_.get(), WriteCallback(_, _)) | |
171 .Times(AtLeast(1)) | |
172 .WillRepeatedly(WithArgs<1>( | |
173 Invoke(this, &WebmMuxerTest::SaveEncodedDataLen))); | |
174 webm_muxer_->OnEncodedVideo(track_number, | |
175 encoded_data, | |
176 arraysize(encoded_data), | |
177 base::TimeDelta::FromMicroseconds(1), | |
178 false /* keyframe */); | |
179 | |
180 // The second time around the callbacks should include a SimpleBlock header, | |
181 // namely the track index, a timestamp and a flags byte, for a total of 6B. | |
182 EXPECT_EQ(last_encoded_length_, arraysize(encoded_data)); | |
183 EXPECT_EQ(GetWebmMuxerPosition(), accumulated_position_); | |
184 const uint32_t kSimpleBlockSize = 6u; | |
185 EXPECT_EQ(static_cast<int64_t>(begin_of_second_block + kSimpleBlockSize + | |
186 arraysize(encoded_data)), | |
187 accumulated_position_); | |
188 } | |
189 | |
190 } // namespace media | |
OLD | NEW |