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/run_loop.h" | 10 #include "base/run_loop.h" |
11 #include "base/strings/utf_string_conversions.h" | 11 #include "base/strings/utf_string_conversions.h" |
12 #include "content/child/child_process.h" | 12 #include "content/child/child_process.h" |
13 #include "content/renderer/media/media_stream_video_track.h" | 13 #include "content/renderer/media/media_stream_video_track.h" |
14 #include "content/renderer/media/mock_media_stream_video_source.h" | 14 #include "content/renderer/media/mock_media_stream_video_source.h" |
15 #include "content/renderer/media/video_track_recorder.h" | 15 #include "content/renderer/media/video_track_recorder.h" |
16 #include "media/base/video_frame.h" | 16 #include "media/base/video_frame.h" |
17 #include "testing/gmock/include/gmock/gmock.h" | 17 #include "testing/gmock/include/gmock/gmock.h" |
18 #include "testing/gtest/include/gtest/gtest.h" | 18 #include "testing/gtest/include/gtest/gtest.h" |
19 #include "third_party/WebKit/public/platform/WebString.h" | 19 #include "third_party/WebKit/public/platform/WebString.h" |
20 #include "third_party/WebKit/public/web/WebHeap.h" | 20 #include "third_party/WebKit/public/web/WebHeap.h" |
21 | 21 |
| 22 using media::VideoFrame; |
| 23 |
22 using ::testing::_; | 24 using ::testing::_; |
23 using ::testing::DoAll; | 25 using ::testing::DoAll; |
24 using ::testing::InSequence; | 26 using ::testing::InSequence; |
25 using ::testing::Mock; | 27 using ::testing::Mock; |
26 using ::testing::Return; | 28 using ::testing::Return; |
27 using ::testing::SaveArg; | 29 using ::testing::SaveArg; |
| 30 using ::testing::TestWithParam; |
| 31 using ::testing::ValuesIn; |
28 | 32 |
29 namespace content { | 33 namespace content { |
30 | 34 |
31 ACTION_P(RunClosure, closure) { | 35 ACTION_P(RunClosure, closure) { |
32 closure.Run(); | 36 closure.Run(); |
33 } | 37 } |
34 | 38 |
35 // Dummy interface class to be able to MOCK its methods. | 39 struct TrackRecorderTestParams { |
36 class EncodedVideoHandlerInterface { | 40 const bool use_vp9; |
37 public: | 41 const size_t first_encoded_frame_size; |
38 virtual void OnEncodedVideo( | 42 const size_t second_encoded_frame_size; |
39 const scoped_refptr<media::VideoFrame>& video_frame, | 43 const size_t third_encoded_frame_size; |
40 scoped_ptr<std::string> encoded_data, | |
41 base::TimeTicks timestamp, | |
42 bool is_key_frame) = 0; | |
43 virtual ~EncodedVideoHandlerInterface() {} | |
44 }; | 44 }; |
45 | 45 |
46 class VideoTrackRecorderTest : public testing::Test, | 46 const TrackRecorderTestParams kTrackRecorderTestParams[] = {{false, 52, 32, 57}, |
47 public EncodedVideoHandlerInterface { | 47 {true, 33, 18, 33}}; |
| 48 |
| 49 class VideoTrackRecorderTest : public TestWithParam<TrackRecorderTestParams> { |
48 public: | 50 public: |
49 VideoTrackRecorderTest() | 51 VideoTrackRecorderTest() |
50 : mock_source_(new MockMediaStreamVideoSource(false)) { | 52 : mock_source_(new MockMediaStreamVideoSource(false)) { |
51 const blink::WebString webkit_track_id(base::UTF8ToUTF16("dummy")); | 53 const blink::WebString webkit_track_id(base::UTF8ToUTF16("dummy")); |
52 blink_source_.initialize(webkit_track_id, | 54 blink_source_.initialize(webkit_track_id, |
53 blink::WebMediaStreamSource::TypeVideo, | 55 blink::WebMediaStreamSource::TypeVideo, |
54 webkit_track_id); | 56 webkit_track_id); |
55 blink_source_.setExtraData(mock_source_); | 57 blink_source_.setExtraData(mock_source_); |
56 blink_track_.initialize(blink_source_); | 58 blink_track_.initialize(blink_source_); |
57 | 59 |
58 blink::WebMediaConstraints constraints; | 60 blink::WebMediaConstraints constraints; |
59 constraints.initialize(); | 61 constraints.initialize(); |
60 track_ = new MediaStreamVideoTrack(mock_source_, constraints, | 62 track_ = new MediaStreamVideoTrack(mock_source_, constraints, |
61 MediaStreamSource::ConstraintsCallback(), | 63 MediaStreamSource::ConstraintsCallback(), |
62 true /* enabled */); | 64 true /* enabled */); |
63 blink_track_.setExtraData(track_); | 65 blink_track_.setExtraData(track_); |
64 | 66 |
65 video_track_recorder_.reset(new VideoTrackRecorder( | 67 video_track_recorder_.reset(new VideoTrackRecorder( |
| 68 GetParam().use_vp9 /* use_vp9 */, |
66 blink_track_, | 69 blink_track_, |
67 base::Bind(&VideoTrackRecorderTest::OnEncodedVideo, | 70 base::Bind(&VideoTrackRecorderTest::OnEncodedVideo, |
68 base::Unretained(this)))); | 71 base::Unretained(this)))); |
69 | |
70 // Paranoia checks. | 72 // Paranoia checks. |
71 EXPECT_EQ(blink_track_.source().extraData(), blink_source_.extraData()); | 73 EXPECT_EQ(blink_track_.source().extraData(), blink_source_.extraData()); |
72 EXPECT_TRUE(message_loop_.IsCurrent()); | 74 EXPECT_TRUE(message_loop_.IsCurrent()); |
73 } | 75 } |
74 | 76 |
75 ~VideoTrackRecorderTest() { | 77 ~VideoTrackRecorderTest() { |
76 blink_track_.reset(); | 78 blink_track_.reset(); |
77 blink_source_.reset(); | 79 blink_source_.reset(); |
78 video_track_recorder_.reset(); | 80 video_track_recorder_.reset(); |
79 blink::WebHeap::collectAllGarbageForTesting(); | 81 blink::WebHeap::collectAllGarbageForTesting(); |
80 } | 82 } |
81 | 83 |
82 MOCK_METHOD4(DoOnEncodedVideo, | 84 MOCK_METHOD4(DoOnEncodedVideo, |
83 void(const scoped_refptr<media::VideoFrame>& frame, | 85 void(const scoped_refptr<VideoFrame>& frame, |
84 std::string encoded_data, | 86 std::string encoded_data, |
85 base::TimeTicks timestamp, | 87 base::TimeTicks timestamp, |
86 bool keyframe)); | 88 bool keyframe)); |
87 void OnEncodedVideo( | 89 void OnEncodedVideo(const scoped_refptr<VideoFrame>& video_frame, |
88 const scoped_refptr<media::VideoFrame>& video_frame, | 90 scoped_ptr<std::string> encoded_data, |
89 scoped_ptr<std::string> encoded_data, | 91 base::TimeTicks timestamp, |
90 base::TimeTicks timestamp, | 92 bool is_key_frame) { |
91 bool is_key_frame) override { | |
92 DoOnEncodedVideo(video_frame, *encoded_data, timestamp, is_key_frame); | 93 DoOnEncodedVideo(video_frame, *encoded_data, timestamp, is_key_frame); |
93 } | 94 } |
94 | 95 |
95 void Encode(const scoped_refptr<media::VideoFrame>& frame, | 96 void Encode(const scoped_refptr<VideoFrame>& frame, |
96 base::TimeTicks capture_time) { | 97 base::TimeTicks capture_time) { |
97 EXPECT_TRUE(message_loop_.IsCurrent()); | 98 EXPECT_TRUE(message_loop_.IsCurrent()); |
98 video_track_recorder_->OnVideoFrameForTesting(frame, capture_time); | 99 video_track_recorder_->OnVideoFrameForTesting(frame, capture_time); |
99 } | 100 } |
100 | 101 |
101 // A ChildProcess and a MessageLoopForUI are both needed to fool the Tracks | 102 // A ChildProcess and a MessageLoopForUI are both needed to fool the Tracks |
102 // and Sources below into believing they are on the right threads. | 103 // and Sources below into believing they are on the right threads. |
103 const base::MessageLoopForUI message_loop_; | 104 const base::MessageLoopForUI message_loop_; |
104 const ChildProcess child_process_; | 105 const ChildProcess child_process_; |
105 | 106 |
106 // All members are non-const due to the series of initialize() calls needed. | 107 // All members are non-const due to the series of initialize() calls needed. |
107 // |mock_source_| is owned by |blink_source_|, |track_| by |blink_track_|. | 108 // |mock_source_| is owned by |blink_source_|, |track_| by |blink_track_|. |
108 MockMediaStreamVideoSource* mock_source_; | 109 MockMediaStreamVideoSource* mock_source_; |
109 blink::WebMediaStreamSource blink_source_; | 110 blink::WebMediaStreamSource blink_source_; |
110 MediaStreamVideoTrack* track_; | 111 MediaStreamVideoTrack* track_; |
111 blink::WebMediaStreamTrack blink_track_; | 112 blink::WebMediaStreamTrack blink_track_; |
112 | 113 |
113 scoped_ptr<VideoTrackRecorder> video_track_recorder_; | 114 scoped_ptr<VideoTrackRecorder> video_track_recorder_; |
114 | 115 |
115 private: | 116 private: |
116 DISALLOW_COPY_AND_ASSIGN(VideoTrackRecorderTest); | 117 DISALLOW_COPY_AND_ASSIGN(VideoTrackRecorderTest); |
117 }; | 118 }; |
118 | 119 |
119 // Construct and destruct all objects, in particular |video_track_recorder_| and | 120 // Construct and destruct all objects, in particular |video_track_recorder_| and |
120 // its inner object(s). This is a non trivial sequence. | 121 // its inner object(s). This is a non trivial sequence. |
121 TEST_F(VideoTrackRecorderTest, ConstructAndDestruct) {} | 122 TEST_P(VideoTrackRecorderTest, ConstructAndDestruct) {} |
122 | 123 |
123 // Creates the encoder and encodes 2 frames of the same size; the encoder should | 124 // Creates the encoder and encodes 2 frames of the same size; the encoder should |
124 // be initialised and produce a keyframe, then a non-keyframe. Finally a frame | 125 // be initialised and produce a keyframe, then a non-keyframe. Finally a frame |
125 // of larger size is sent and is expected to be encoded as a keyframe. | 126 // of larger size is sent and is expected to be encoded as a keyframe. |
126 TEST_F(VideoTrackRecorderTest, VideoEncoding) { | 127 TEST_P(VideoTrackRecorderTest, VideoEncoding) { |
127 // |frame_size| cannot be arbitrarily small, should be reasonable. | 128 // |frame_size| cannot be arbitrarily small, should be reasonable. |
128 const gfx::Size frame_size(160, 80); | 129 const gfx::Size frame_size(160, 80); |
129 const scoped_refptr<media::VideoFrame> video_frame = | 130 const scoped_refptr<VideoFrame> video_frame = |
130 media::VideoFrame::CreateBlackFrame(frame_size); | 131 VideoFrame::CreateBlackFrame(frame_size); |
131 const double kFrameRate = 60.0f; | 132 const double kFrameRate = 60.0f; |
132 video_frame->metadata()->SetDouble(media::VideoFrameMetadata::FRAME_RATE, | 133 video_frame->metadata()->SetDouble(media::VideoFrameMetadata::FRAME_RATE, |
133 kFrameRate); | 134 kFrameRate); |
134 | 135 |
135 InSequence s; | 136 InSequence s; |
136 const base::TimeTicks timeticks_now = base::TimeTicks::Now(); | 137 const base::TimeTicks timeticks_now = base::TimeTicks::Now(); |
137 base::StringPiece first_frame_encoded_data; | 138 base::StringPiece first_frame_encoded_data; |
138 EXPECT_CALL(*this, DoOnEncodedVideo(video_frame, _, timeticks_now, true)) | 139 EXPECT_CALL(*this, DoOnEncodedVideo(video_frame, _, timeticks_now, true)) |
139 .Times(1) | 140 .Times(1) |
140 .WillOnce(SaveArg<1>(&first_frame_encoded_data)); | 141 .WillOnce(SaveArg<1>(&first_frame_encoded_data)); |
141 Encode(video_frame, timeticks_now); | 142 Encode(video_frame, timeticks_now); |
142 | 143 |
143 // Send another Video Frame. | 144 // Send another Video Frame. |
144 const base::TimeTicks timeticks_later = base::TimeTicks::Now(); | 145 const base::TimeTicks timeticks_later = base::TimeTicks::Now(); |
145 base::StringPiece second_frame_encoded_data; | 146 base::StringPiece second_frame_encoded_data; |
146 EXPECT_CALL(*this, DoOnEncodedVideo(video_frame, _, timeticks_later, false)) | 147 EXPECT_CALL(*this, DoOnEncodedVideo(video_frame, _, timeticks_later, false)) |
147 .Times(1) | 148 .Times(1) |
148 .WillOnce(SaveArg<1>(&second_frame_encoded_data)); | 149 .WillOnce(SaveArg<1>(&second_frame_encoded_data)); |
149 Encode(video_frame, timeticks_later); | 150 Encode(video_frame, timeticks_later); |
150 | 151 |
151 // Send another Video Frame and expect only an DoOnEncodedVideo() callback. | 152 // Send another Video Frame and expect only an DoOnEncodedVideo() callback. |
152 const gfx::Size frame_size2(180, 80); | 153 const gfx::Size frame_size2(180, 80); |
153 const scoped_refptr<media::VideoFrame> video_frame2 = | 154 const scoped_refptr<VideoFrame> video_frame2 = |
154 media::VideoFrame::CreateBlackFrame(frame_size2); | 155 VideoFrame::CreateBlackFrame(frame_size2); |
155 | 156 |
156 base::RunLoop run_loop; | 157 base::RunLoop run_loop; |
157 base::Closure quit_closure = run_loop.QuitClosure(); | 158 base::Closure quit_closure = run_loop.QuitClosure(); |
158 | 159 |
159 base::StringPiece third_frame_encoded_data; | 160 base::StringPiece third_frame_encoded_data; |
160 EXPECT_CALL(*this, DoOnEncodedVideo(video_frame2, _, _, true)) | 161 EXPECT_CALL(*this, DoOnEncodedVideo(video_frame2, _, _, true)) |
161 .Times(1) | 162 .Times(1) |
162 .WillOnce(DoAll(SaveArg<1>(&third_frame_encoded_data), | 163 .WillOnce(DoAll(SaveArg<1>(&third_frame_encoded_data), |
163 RunClosure(quit_closure))); | 164 RunClosure(quit_closure))); |
164 Encode(video_frame2, base::TimeTicks::Now()); | 165 Encode(video_frame2, base::TimeTicks::Now()); |
165 | 166 |
166 run_loop.Run(); | 167 run_loop.Run(); |
167 | 168 |
168 const size_t kFirstEncodedDataSize = 52; | 169 EXPECT_EQ(GetParam().first_encoded_frame_size, |
169 EXPECT_EQ(first_frame_encoded_data.size(), kFirstEncodedDataSize); | 170 first_frame_encoded_data.size()); |
170 const size_t kSecondEncodedDataSize = 32; | 171 EXPECT_EQ(GetParam().second_encoded_frame_size, |
171 EXPECT_EQ(second_frame_encoded_data.size(), kSecondEncodedDataSize); | 172 second_frame_encoded_data.size()); |
172 const size_t kThirdEncodedDataSize = 57; | 173 EXPECT_EQ(GetParam().third_encoded_frame_size, |
173 EXPECT_EQ(third_frame_encoded_data.size(), kThirdEncodedDataSize); | 174 third_frame_encoded_data.size()); |
174 | 175 |
175 Mock::VerifyAndClearExpectations(this); | 176 Mock::VerifyAndClearExpectations(this); |
176 } | 177 } |
177 | 178 |
| 179 INSTANTIATE_TEST_CASE_P(, |
| 180 VideoTrackRecorderTest, |
| 181 ValuesIn(kTrackRecorderTestParams)); |
| 182 |
178 } // namespace content | 183 } // namespace content |
OLD | NEW |