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