OLD | NEW |
(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 uint32_t track_number = 27; |
| 78 const uint32_t track_number_plus1 = track_number + 1; |
| 79 |
| 80 EXPECT_EQ(nullptr, |
| 81 webm_muxer_->segment_->GetTrackByNumber(track_number_plus1)); |
| 82 webm_muxer_->AddVideoTrack(track_number, gfx::Size(320, 240), 30.0f); |
| 83 EXPECT_NE(nullptr, |
| 84 webm_muxer_->segment_->GetTrackByNumber(track_number_plus1)); |
| 85 } |
| 86 |
| 87 // Checks that the WriteCallback is called with appropriate params when |
| 88 // WebmMuxer::Write() method is called. |
| 89 TEST_F(WebmMuxerTest, Write) { |
| 90 const char encoded_data[] = "abcdefghijklmnopqrstuvwxyz"; |
| 91 const int encoded_len = arraysize(encoded_data); |
| 92 |
| 93 EXPECT_CALL(*mock_handler_.get(), |
| 94 WriteCallback(encoded_data, encoded_len)).Times(1); |
| 95 webm_muxer_->Write(encoded_data, encoded_len); |
| 96 |
| 97 EXPECT_EQ(webm_muxer_->Position(), encoded_len); |
| 98 } |
| 99 |
| 100 // This test sends an empty frame, checks that the file header is sent to the |
| 101 // WriteCallback(), then sends a non-empty frame and checks that the Track |
| 102 // header and a SimpleBlock are sent to the callback. |
| 103 TEST_F(WebmMuxerTest, OnEncodedVideoEmptyAndNormalFrames) { |
| 104 const uint32_t track_number = 27; |
| 105 webm_muxer_->AddVideoTrack(track_number, gfx::Size(320, 240), 30.0f); |
| 106 |
| 107 EXPECT_CALL(*mock_handler_.get(), WriteCallback(_, _)) |
| 108 .Times(AtLeast(1)) |
| 109 .WillRepeatedly(WithArgs<1>( |
| 110 Invoke(this, &WebmMuxerTest::SaveEncodedDataLen))); |
| 111 webm_muxer_->OnEncodedVideo(track_number, |
| 112 std::string() /* Passing no data */, |
| 113 base::TimeDelta::FromMicroseconds(0), |
| 114 false /* keyframe */); |
| 115 EXPECT_EQ(webm_muxer_->Position(), accumulated_position_); |
| 116 |
| 117 const uint32_t begin_of_second_block = accumulated_position_; |
| 118 const std::string encoded_data("abcdefghijklmnopqrstuvwxyz"); |
| 119 EXPECT_CALL(*mock_handler_.get(), WriteCallback(_, _)) |
| 120 .Times(AtLeast(1)) |
| 121 .WillRepeatedly(WithArgs<1>( |
| 122 Invoke(this, &WebmMuxerTest::SaveEncodedDataLen))); |
| 123 webm_muxer_->OnEncodedVideo(track_number, |
| 124 encoded_data, |
| 125 base::TimeDelta::FromMicroseconds(1), |
| 126 false /* keyframe */); |
| 127 EXPECT_EQ(last_encoded_length_, encoded_data.length()); |
| 128 EXPECT_EQ(webm_muxer_->Position(), accumulated_position_); |
| 129 const uint32_t kTrackInfoSize = 15u; |
| 130 const uint32_t kSimpleBlockSize = 6u; |
| 131 EXPECT_EQ(static_cast<int64_t>(begin_of_second_block + kTrackInfoSize + |
| 132 kSimpleBlockSize + encoded_data.length()), |
| 133 accumulated_position_); |
| 134 } |
| 135 |
| 136 // This test sends two frames and checks that the WriteCallback is called with |
| 137 // appropriate params in both cases. |
| 138 TEST_F(WebmMuxerTest, OnEncodedVideoNormalFrames) { |
| 139 const std::string encoded_data("abcdefghijklmnopqrstuvwxyz"); |
| 140 const uint32_t track_number = 0; |
| 141 webm_muxer_->AddVideoTrack(track_number, gfx::Size(320, 240), 30.0f); |
| 142 |
| 143 EXPECT_CALL(*mock_handler_.get(), WriteCallback(_, _)) |
| 144 .Times(AtLeast(1)) |
| 145 .WillRepeatedly(WithArgs<1>( |
| 146 Invoke(this, &WebmMuxerTest::SaveEncodedDataLen))); |
| 147 webm_muxer_->OnEncodedVideo(track_number, |
| 148 encoded_data, |
| 149 base::TimeDelta::FromMicroseconds(0), |
| 150 false /* keyframe */); |
| 151 |
| 152 // First time around WriteCallback() is pinged a number of times to write the |
| 153 // Matroska header, but at the end it dumps |encoded_data|. |
| 154 EXPECT_EQ(last_encoded_length_, encoded_data.length()); |
| 155 EXPECT_EQ(webm_muxer_->Position(), accumulated_position_); |
| 156 EXPECT_GE(webm_muxer_->Position(), |
| 157 static_cast<int64_t>(last_encoded_length_)); |
| 158 |
| 159 const int64_t begin_of_second_block = accumulated_position_; |
| 160 EXPECT_CALL(*mock_handler_.get(), WriteCallback(_, _)) |
| 161 .Times(AtLeast(1)) |
| 162 .WillRepeatedly(WithArgs<1>( |
| 163 Invoke(this, &WebmMuxerTest::SaveEncodedDataLen))); |
| 164 webm_muxer_->OnEncodedVideo(track_number, |
| 165 encoded_data, |
| 166 base::TimeDelta::FromMicroseconds(1), |
| 167 false /* keyframe */); |
| 168 |
| 169 // The second time around the callbacks should include a SimpleBlock header, |
| 170 // namely the track index, a timestamp and a flags byte, for a total of 6B. |
| 171 EXPECT_EQ(last_encoded_length_, encoded_data.length()); |
| 172 EXPECT_EQ(webm_muxer_->Position(), accumulated_position_); |
| 173 const uint32_t kSimpleBlockSize = 6u; |
| 174 EXPECT_EQ(static_cast<int64_t>(begin_of_second_block + kSimpleBlockSize + |
| 175 encoded_data.length()), |
| 176 accumulated_position_); |
| 177 } |
| 178 |
| 179 } // namespace media |
OLD | NEW |