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