| 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 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: | 24 public: |
| 31 WebmMuxerTest() | 25 WebmMuxerTest() |
| 32 : webm_muxer_(base::Bind(&WebmMuxerTest::WriteCallback, | 26 : test_task_runner_(new base::TestSimpleTaskRunner()), |
| 33 base::Unretained(this))), | 27 webm_muxer_(new WebmMuxer(test_task_runner_, |
| 28 base::Bind(&WebmMuxerTest::WriteData, |
| 29 base::Unretained(this)))), |
| 34 last_encoded_length_(0), | 30 last_encoded_length_(0), |
| 35 accumulated_position_(0) { | 31 accumulated_position_(0) { |
| 36 EXPECT_EQ(webm_muxer_.Position(), 0); | 32 EXPECT_EQ(webm_muxer_->Position(), 0); |
| 37 const mkvmuxer::int64 kRandomNewPosition = 333; | 33 const mkvmuxer::int64 kRandomNewPosition = 333; |
| 38 EXPECT_EQ(webm_muxer_.Position(kRandomNewPosition), -1); | 34 EXPECT_EQ(webm_muxer_->Position(kRandomNewPosition), -1); |
| 39 EXPECT_FALSE(webm_muxer_.Seekable()); | 35 EXPECT_FALSE(webm_muxer_->Seekable()); |
| 40 } | 36 } |
| 41 | 37 |
| 42 MOCK_METHOD1(WriteCallback, void(const base::StringPiece&)); | 38 MOCK_METHOD1(WriteCallback, void(const std::string&)); |
| 39 void WriteData(scoped_ptr<std::string> data) { |
| 40 WriteCallback(*data); |
| 41 } |
| 43 | 42 |
| 44 void SaveEncodedDataLen(const base::StringPiece& encoded_data) { | 43 void SaveEncodedDataLen(const std::string& encoded_data) { |
| 45 last_encoded_length_ = encoded_data.size(); | 44 last_encoded_length_ = encoded_data.size(); |
| 46 accumulated_position_ += encoded_data.size(); | 45 accumulated_position_ += encoded_data.size(); |
| 47 } | 46 } |
| 48 | 47 |
| 49 mkvmuxer::int64 GetWebmMuxerPosition() const { | 48 mkvmuxer::int64 GetWebmMuxerPosition() const { |
| 50 return webm_muxer_.Position(); | 49 return webm_muxer_->Position(); |
| 51 } | 50 } |
| 52 | 51 |
| 53 mkvmuxer::Segment::Mode GetWebmSegmentMode() const { | 52 mkvmuxer::Segment::Mode GetWebmSegmentMode() const { |
| 54 return webm_muxer_.segment_.mode(); | 53 return webm_muxer_->segment_->mode(); |
| 55 } | 54 } |
| 56 | 55 |
| 57 mkvmuxer::int32 WebmMuxerWrite(const void* buf, mkvmuxer::uint32 len) { | 56 mkvmuxer::int32 WebmMuxerWrite(const void* buf, mkvmuxer::uint32 len) { |
| 58 return webm_muxer_.Write(buf, len); | 57 return webm_muxer_->Write(buf, len); |
| 59 } | 58 } |
| 60 | 59 |
| 61 WebmMuxer webm_muxer_; | 60 const scoped_refptr<base::TestSimpleTaskRunner> test_task_runner_; |
| 61 scoped_refptr<WebmMuxer> webm_muxer_; |
| 62 | 62 |
| 63 size_t last_encoded_length_; | 63 size_t last_encoded_length_; |
| 64 int64_t accumulated_position_; | 64 int64_t accumulated_position_; |
| 65 | 65 |
| 66 private: | 66 private: |
| 67 DISALLOW_COPY_AND_ASSIGN(WebmMuxerTest); | 67 DISALLOW_COPY_AND_ASSIGN(WebmMuxerTest); |
| 68 }; | 68 }; |
| 69 | 69 |
| 70 // Checks that the WriteCallback is called with appropriate params when | 70 // Checks that the WriteCallback is called with appropriate params when |
| 71 // WebmMuxer::Write() method is called. | 71 // WebmMuxer::Write() method is called. |
| 72 TEST_F(WebmMuxerTest, Write) { | 72 TEST_F(WebmMuxerTest, Write) { |
| 73 const base::StringPiece encoded_data("abcdefghijklmnopqrstuvwxyz"); | 73 const std::string encoded_data("abcdefghijklmnopqrstuvwxyz"); |
| 74 | 74 |
| 75 EXPECT_CALL(*this, WriteCallback(encoded_data)); | 75 EXPECT_CALL(*this, WriteCallback(encoded_data)); |
| 76 WebmMuxerWrite(encoded_data.data(), encoded_data.size()); | 76 WebmMuxerWrite(encoded_data.data(), encoded_data.size()); |
| 77 | 77 |
| 78 EXPECT_EQ(GetWebmMuxerPosition(), static_cast<int64_t>(encoded_data.size())); | 78 EXPECT_EQ(GetWebmMuxerPosition(), static_cast<int64_t>(encoded_data.size())); |
| 79 } | 79 } |
| 80 | 80 |
| 81 // This test sends two frames and checks that the WriteCallback is called with | 81 // This test sends two frames and checks that the WriteCallback is called with |
| 82 // appropriate params in both cases. | 82 // appropriate params in both cases. |
| 83 TEST_F(WebmMuxerTest, OnEncodedVideoTwoFrames) { | 83 TEST_F(WebmMuxerTest, OnEncodedVideoTwoFrames) { |
| 84 const gfx::Size frame_size(160, 80); | 84 const gfx::Size frame_size(160, 80); |
| 85 const scoped_refptr<VideoFrame> video_frame = | 85 const scoped_refptr<VideoFrame> video_frame = |
| 86 VideoFrame::CreateBlackFrame(frame_size); | 86 VideoFrame::CreateBlackFrame(frame_size); |
| 87 const base::StringPiece encoded_data("abcdefghijklmnopqrstuvwxyz"); | 87 const base::StringPiece encoded_data("abcdefghijklmnopqrstuvwxyz"); |
| 88 | 88 |
| 89 EXPECT_CALL(*this, WriteCallback(_)) | 89 EXPECT_CALL(*this, WriteCallback(_)) |
| 90 .Times(AtLeast(1)) | 90 .Times(AtLeast(1)) |
| 91 .WillRepeatedly(WithArgs<0>( | 91 .WillRepeatedly(WithArgs<0>( |
| 92 Invoke(this, &WebmMuxerTest::SaveEncodedDataLen))); | 92 Invoke(this, &WebmMuxerTest::SaveEncodedDataLen))); |
| 93 webm_muxer_.OnEncodedVideo(video_frame, | 93 webm_muxer_->OnEncodedVideo(video_frame, |
| 94 encoded_data, | 94 encoded_data, |
| 95 base::TimeTicks::Now(), | 95 base::TimeTicks::Now(), |
| 96 false /* keyframe */); | 96 false /* keyframe */); |
| 97 | 97 |
| 98 // First time around WriteCallback() is pinged a number of times to write the | 98 // First time around WriteCallback() is pinged a number of times to write the |
| 99 // Matroska header, but at the end it dumps |encoded_data|. | 99 // Matroska header, but at the end it dumps |encoded_data|. |
| 100 EXPECT_EQ(last_encoded_length_, encoded_data.size()); | 100 EXPECT_EQ(last_encoded_length_, encoded_data.size()); |
| 101 EXPECT_EQ(GetWebmMuxerPosition(), accumulated_position_); | 101 EXPECT_EQ(GetWebmMuxerPosition(), accumulated_position_); |
| 102 EXPECT_GE(GetWebmMuxerPosition(), static_cast<int64_t>(last_encoded_length_)); | 102 EXPECT_GE(GetWebmMuxerPosition(), static_cast<int64_t>(last_encoded_length_)); |
| 103 EXPECT_EQ(GetWebmSegmentMode(), mkvmuxer::Segment::kLive); | 103 EXPECT_EQ(GetWebmSegmentMode(), mkvmuxer::Segment::kLive); |
| 104 | 104 |
| 105 const int64_t begin_of_second_block = accumulated_position_; | 105 const int64_t begin_of_second_block = accumulated_position_; |
| 106 EXPECT_CALL(*this, WriteCallback(_)) | 106 EXPECT_CALL(*this, WriteCallback(_)) |
| 107 .Times(AtLeast(1)) | 107 .Times(AtLeast(1)) |
| 108 .WillRepeatedly(WithArgs<0>( | 108 .WillRepeatedly(WithArgs<0>( |
| 109 Invoke(this, &WebmMuxerTest::SaveEncodedDataLen))); | 109 Invoke(this, &WebmMuxerTest::SaveEncodedDataLen))); |
| 110 webm_muxer_.OnEncodedVideo(video_frame, | 110 webm_muxer_->OnEncodedVideo(video_frame, |
| 111 encoded_data, | 111 encoded_data, |
| 112 base::TimeTicks::Now(), | 112 base::TimeTicks::Now(), |
| 113 false /* keyframe */); | 113 false /* keyframe */); |
| 114 | 114 |
| 115 // The second time around the callbacks should include a SimpleBlock header, | 115 // 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. | 116 // namely the track index, a timestamp and a flags byte, for a total of 6B. |
| 117 EXPECT_EQ(last_encoded_length_, encoded_data.size()); | 117 EXPECT_EQ(last_encoded_length_, encoded_data.size()); |
| 118 EXPECT_EQ(GetWebmMuxerPosition(), accumulated_position_); | 118 EXPECT_EQ(GetWebmMuxerPosition(), accumulated_position_); |
| 119 const uint32_t kSimpleBlockSize = 6u; | 119 const uint32_t kSimpleBlockSize = 6u; |
| 120 EXPECT_EQ(static_cast<int64_t>(begin_of_second_block + kSimpleBlockSize + | 120 EXPECT_EQ(static_cast<int64_t>(begin_of_second_block + kSimpleBlockSize + |
| 121 encoded_data.size()), | 121 encoded_data.size()), |
| 122 accumulated_position_); | 122 accumulated_position_); |
| 123 } | 123 } |
| 124 | 124 |
| 125 } // namespace media | 125 } // namespace media |
| OLD | NEW |