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