| 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_(base::Bind(&WebmMuxerTest::WriteCallback, |
| 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(base::StringPiece)); |
| 43 | 36 |
| 44 void SaveEncodedDataLen(const base::StringPiece& encoded_data) { | 37 void SaveEncodedDataLen(const base::StringPiece& encoded_data) { |
| 45 last_encoded_length_ = encoded_data.size(); | 38 last_encoded_length_ = encoded_data.size(); |
| 46 accumulated_position_ += encoded_data.size(); | 39 accumulated_position_ += encoded_data.size(); |
| 47 } | 40 } |
| 48 | 41 |
| 49 mkvmuxer::int64 GetWebmMuxerPosition() const { | 42 mkvmuxer::int64 GetWebmMuxerPosition() const { |
| 50 return webm_muxer_.Position(); | 43 return webm_muxer_.Position(); |
| 51 } | 44 } |
| 52 | 45 |
| (...skipping 24 matching lines...) Expand all Loading... |
| 77 | 70 |
| 78 EXPECT_EQ(GetWebmMuxerPosition(), static_cast<int64_t>(encoded_data.size())); | 71 EXPECT_EQ(GetWebmMuxerPosition(), static_cast<int64_t>(encoded_data.size())); |
| 79 } | 72 } |
| 80 | 73 |
| 81 // This test sends two frames and checks that the WriteCallback is called with | 74 // This test sends two frames and checks that the WriteCallback is called with |
| 82 // appropriate params in both cases. | 75 // appropriate params in both cases. |
| 83 TEST_F(WebmMuxerTest, OnEncodedVideoTwoFrames) { | 76 TEST_F(WebmMuxerTest, OnEncodedVideoTwoFrames) { |
| 84 const gfx::Size frame_size(160, 80); | 77 const gfx::Size frame_size(160, 80); |
| 85 const scoped_refptr<VideoFrame> video_frame = | 78 const scoped_refptr<VideoFrame> video_frame = |
| 86 VideoFrame::CreateBlackFrame(frame_size); | 79 VideoFrame::CreateBlackFrame(frame_size); |
| 87 const base::StringPiece encoded_data("abcdefghijklmnopqrstuvwxyz"); | 80 const std::string encoded_data("abcdefghijklmnopqrstuvwxyz"); |
| 88 | 81 |
| 89 EXPECT_CALL(*this, WriteCallback(_)) | 82 EXPECT_CALL(*this, WriteCallback(_)) |
| 90 .Times(AtLeast(1)) | 83 .Times(AtLeast(1)) |
| 91 .WillRepeatedly(WithArgs<0>( | 84 .WillRepeatedly(WithArgs<0>( |
| 92 Invoke(this, &WebmMuxerTest::SaveEncodedDataLen))); | 85 Invoke(this, &WebmMuxerTest::SaveEncodedDataLen))); |
| 93 webm_muxer_.OnEncodedVideo(video_frame, | 86 webm_muxer_.OnEncodedVideo(video_frame, |
| 94 encoded_data, | 87 make_scoped_ptr(new std::string(encoded_data)), |
| 95 base::TimeTicks::Now(), | 88 base::TimeTicks::Now(), |
| 96 false /* keyframe */); | 89 false /* keyframe */); |
| 97 | 90 |
| 98 // First time around WriteCallback() is pinged a number of times to write the | 91 // First time around WriteCallback() is pinged a number of times to write the |
| 99 // Matroska header, but at the end it dumps |encoded_data|. | 92 // Matroska header, but at the end it dumps |encoded_data|. |
| 100 EXPECT_EQ(last_encoded_length_, encoded_data.size()); | 93 EXPECT_EQ(last_encoded_length_, encoded_data.size()); |
| 101 EXPECT_EQ(GetWebmMuxerPosition(), accumulated_position_); | 94 EXPECT_EQ(GetWebmMuxerPosition(), accumulated_position_); |
| 102 EXPECT_GE(GetWebmMuxerPosition(), static_cast<int64_t>(last_encoded_length_)); | 95 EXPECT_GE(GetWebmMuxerPosition(), static_cast<int64_t>(last_encoded_length_)); |
| 103 EXPECT_EQ(GetWebmSegmentMode(), mkvmuxer::Segment::kLive); | 96 EXPECT_EQ(GetWebmSegmentMode(), mkvmuxer::Segment::kLive); |
| 104 | 97 |
| 105 const int64_t begin_of_second_block = accumulated_position_; | 98 const int64_t begin_of_second_block = accumulated_position_; |
| 106 EXPECT_CALL(*this, WriteCallback(_)) | 99 EXPECT_CALL(*this, WriteCallback(_)) |
| 107 .Times(AtLeast(1)) | 100 .Times(AtLeast(1)) |
| 108 .WillRepeatedly(WithArgs<0>( | 101 .WillRepeatedly(WithArgs<0>( |
| 109 Invoke(this, &WebmMuxerTest::SaveEncodedDataLen))); | 102 Invoke(this, &WebmMuxerTest::SaveEncodedDataLen))); |
| 110 webm_muxer_.OnEncodedVideo(video_frame, | 103 webm_muxer_.OnEncodedVideo(video_frame, |
| 111 encoded_data, | 104 make_scoped_ptr(new std::string(encoded_data)), |
| 112 base::TimeTicks::Now(), | 105 base::TimeTicks::Now(), |
| 113 false /* keyframe */); | 106 false /* keyframe */); |
| 114 | 107 |
| 115 // The second time around the callbacks should include a SimpleBlock header, | 108 // 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. | 109 // namely the track index, a timestamp and a flags byte, for a total of 6B. |
| 117 EXPECT_EQ(last_encoded_length_, encoded_data.size()); | 110 EXPECT_EQ(last_encoded_length_, encoded_data.size()); |
| 118 EXPECT_EQ(GetWebmMuxerPosition(), accumulated_position_); | 111 EXPECT_EQ(GetWebmMuxerPosition(), accumulated_position_); |
| 119 const uint32_t kSimpleBlockSize = 6u; | 112 const uint32_t kSimpleBlockSize = 6u; |
| 120 EXPECT_EQ(static_cast<int64_t>(begin_of_second_block + kSimpleBlockSize + | 113 EXPECT_EQ(static_cast<int64_t>(begin_of_second_block + kSimpleBlockSize + |
| 121 encoded_data.size()), | 114 encoded_data.size()), |
| 122 accumulated_position_); | 115 accumulated_position_); |
| 123 } | 116 } |
| 124 | 117 |
| 125 } // namespace media | 118 } // namespace media |
| OLD | NEW |