Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(176)

Side by Side Diff: media/capture/webm_muxer_unittest.cc

Issue 1225123006: media/capture: Adding WebmMuxer class and unittests (Closed) Base URL: https://chromium.googlesource.com/chromium/src.git@m_crbug262211__MSRecorder__2__libwebm_reland_in_third_party
Patch Set: dalecurtis@ and tomfinegan@ comments Created 5 years, 5 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch
OLDNEW
(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) {}
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:
52 WebmMuxerTest()
53 : mock_handler_(new MockWebmMuxerEventHandler()),
54 webm_muxer_(new WebmMuxer(
55 base::Bind(&MockWebmMuxerEventHandler::WriteCallback,
56 mock_handler_))),
57 last_encoded_length_(0),
58 accumulated_position_(0) {}
59
60 void SetUp() override {
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 const scoped_refptr<MockWebmMuxerEventHandler> mock_handler_;
67 const scoped_ptr<WebmMuxer> webm_muxer_;
68
69 size_t last_encoded_length_;
70 int64_t accumulated_position_;
71
72 private:
73 DISALLOW_COPY_AND_ASSIGN(WebmMuxerTest);
74 };
75 // Checks that AddVideoTrack adds a Track.
76 TEST_F(WebmMuxerTest, AddVideoTrack) {
77 const uint64_t track_number = webm_muxer_->AddVideoTrack(gfx::Size(320, 240),
78 30.0f);
79 EXPECT_NE(nullptr, webm_muxer_->segment_.GetTrackByNumber(track_number));
80 }
81
82 // Checks that the WriteCallback is called with appropriate params when
83 // WebmMuxer::Write() method is called.
84 TEST_F(WebmMuxerTest, Write) {
85 const char encoded_data[] = "abcdefghijklmnopqrstuvwxyz";
86 const int encoded_len = arraysize(encoded_data);
87
88 EXPECT_CALL(*mock_handler_.get(), WriteCallback(encoded_data, encoded_len));
89 webm_muxer_->Write(encoded_data, encoded_len);
90
91 EXPECT_EQ(webm_muxer_->Position(), encoded_len);
92 }
93
94 // This test sends an empty frame, checks that the file header is sent to the
95 // WriteCallback(), then sends a non-empty frame and checks that the Track
96 // header and a SimpleBlock are sent to the callback.
97 TEST_F(WebmMuxerTest, OnEncodedVideoEmptyAndNormalFrames) {
98 const uint64_t track_number = webm_muxer_->AddVideoTrack(gfx::Size(320, 240),
99 30.0f);
100
101 EXPECT_CALL(*mock_handler_.get(), WriteCallback(_, _))
102 .Times(AtLeast(1))
103 .WillRepeatedly(WithArgs<1>(
104 Invoke(this, &WebmMuxerTest::SaveEncodedDataLen)));
105 webm_muxer_->OnEncodedVideo(track_number,
106 std::string() /* Passing no data */,
107 base::TimeDelta::FromMicroseconds(0),
108 false /* keyframe */);
109 EXPECT_EQ(webm_muxer_->Position(), accumulated_position_);
110
111 const uint32_t begin_of_second_block = accumulated_position_;
112 const std::string encoded_data("abcdefghijklmnopqrstuvwxyz");
113 EXPECT_CALL(*mock_handler_.get(), WriteCallback(_, _))
114 .Times(AtLeast(1))
115 .WillRepeatedly(WithArgs<1>(
116 Invoke(this, &WebmMuxerTest::SaveEncodedDataLen)));
117 webm_muxer_->OnEncodedVideo(track_number,
118 encoded_data,
119 base::TimeDelta::FromMicroseconds(1),
120 false /* keyframe */);
121 EXPECT_EQ(last_encoded_length_, encoded_data.length());
122 EXPECT_EQ(webm_muxer_->Position(), accumulated_position_);
123 const uint32_t kTrackInfoSize = 15u;
124 const uint32_t kSimpleBlockSize = 6u;
125 EXPECT_EQ(static_cast<int64_t>(begin_of_second_block + kTrackInfoSize +
126 kSimpleBlockSize + encoded_data.length()),
127 accumulated_position_);
128 }
129
130 // This test sends two frames and checks that the WriteCallback is called with
131 // appropriate params in both cases.
132 TEST_F(WebmMuxerTest, OnEncodedVideoNormalFrames) {
133 const std::string encoded_data("abcdefghijklmnopqrstuvwxyz");
134 const uint64_t track_number = webm_muxer_->AddVideoTrack(gfx::Size(320, 240),
135 30.0f);
136
137 EXPECT_CALL(*mock_handler_.get(), WriteCallback(_, _))
138 .Times(AtLeast(1))
139 .WillRepeatedly(WithArgs<1>(
140 Invoke(this, &WebmMuxerTest::SaveEncodedDataLen)));
141 webm_muxer_->OnEncodedVideo(track_number,
142 encoded_data,
143 base::TimeDelta::FromMicroseconds(0),
144 false /* keyframe */);
145
146 // First time around WriteCallback() is pinged a number of times to write the
147 // Matroska header, but at the end it dumps |encoded_data|.
148 EXPECT_EQ(last_encoded_length_, encoded_data.length());
149 EXPECT_EQ(webm_muxer_->Position(), accumulated_position_);
150 EXPECT_GE(webm_muxer_->Position(),
151 static_cast<int64_t>(last_encoded_length_));
152
153 const int64_t begin_of_second_block = accumulated_position_;
154 EXPECT_CALL(*mock_handler_.get(), WriteCallback(_, _))
155 .Times(AtLeast(1))
156 .WillRepeatedly(WithArgs<1>(
157 Invoke(this, &WebmMuxerTest::SaveEncodedDataLen)));
158 webm_muxer_->OnEncodedVideo(track_number,
159 encoded_data,
160 base::TimeDelta::FromMicroseconds(1),
161 false /* keyframe */);
162
163 // The second time around the callbacks should include a SimpleBlock header,
164 // namely the track index, a timestamp and a flags byte, for a total of 6B.
165 EXPECT_EQ(last_encoded_length_, encoded_data.length());
166 EXPECT_EQ(webm_muxer_->Position(), accumulated_position_);
167 const uint32_t kSimpleBlockSize = 6u;
168 EXPECT_EQ(static_cast<int64_t>(begin_of_second_block + kSimpleBlockSize +
169 encoded_data.length()),
170 accumulated_position_);
171 }
172
173 } // namespace media
OLDNEW
« media/capture/webm_muxer.cc ('K') | « media/capture/webm_muxer.cc ('k') | media/media.gyp » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698