Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(1182)

Side by Side Diff: content/renderer/media/video_track_recorder_unittest.cc

Issue 1233033002: MediaStream: Adding VideoTrackRecorder class and unittests (Closed) Base URL: https://chromium.googlesource.com/chromium/src.git@master
Patch Set: miu@s comments (except param piggybacking and threading stuff) Created 5 years, 4 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch
OLDNEW
(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 "base/run_loop.h"
11 #include "base/strings/utf_string_conversions.h"
12 #include "content/child/child_process.h"
13 #include "content/renderer/media/media_stream_video_track.h"
14 #include "content/renderer/media/mock_media_stream_video_source.h"
15 #include "content/renderer/media/video_track_recorder.h"
16 #include "media/base/video_frame.h"
17 #include "testing/gmock/include/gmock/gmock.h"
18 #include "testing/gtest/include/gtest/gtest.h"
19 #include "third_party/WebKit/public/platform/WebString.h"
20 #include "third_party/WebKit/public/web/WebHeap.h"
21
22 using ::testing::_;
23 using ::testing::Mock;
24 using ::testing::Return;
25 using ::testing::SaveArg;
26 using ::testing::InSequence;
27
28 namespace content {
29
30 // Dummy interface class to be able to MOCK its methods.
31 class EncodedVideoHandlerInterface {
32 public:
33 virtual uint64_t OnFirstFrame(const gfx::Size& frame_size,
34 double frame_rate) = 0;
35 virtual void OnEncodedVideo(uint64_t track_number,
36 const base::StringPiece& encoded_data,
37 base::TimeDelta timestamp,
38 bool keyframe) = 0;
39 virtual ~EncodedVideoHandlerInterface() {}
40 };
41
42 class VideoTrackRecorderTest : public testing::Test,
43 public EncodedVideoHandlerInterface {
44 public:
45 VideoTrackRecorderTest()
46 : child_process_(new ChildProcess()),
47 mock_source_(new MockMediaStreamVideoSource(false)) {
48 const blink::WebString webkit_track_id(base::UTF8ToUTF16("dummy"));
49 blink_source_.initialize(webkit_track_id,
50 blink::WebMediaStreamSource::TypeVideo,
51 webkit_track_id);
52 blink_source_.setExtraData(mock_source_);
53 blink_track_.initialize(blink_source_);
54
55 blink::WebMediaConstraints constraints;
56 constraints.initialize();
57 track_ = new MediaStreamVideoTrack(mock_source_, constraints,
58 MediaStreamSource::ConstraintsCallback(),
59 true /* enabled */);
60 blink_track_.setExtraData(track_);
61
62 video_track_recorder_.reset(new VideoTrackRecorder(
63 child_process_->io_task_runner(),
64 blink_track_,
65 base::Bind(&VideoTrackRecorderTest::OnFirstFrame,
66 base::Unretained(this)),
67 base::Bind(&VideoTrackRecorderTest::OnEncodedVideo,
68 base::Unretained(this))));
69
70 // Paranoia checks.
71 EXPECT_EQ(blink_track_.source().extraData(), blink_source_.extraData());
72 EXPECT_TRUE(message_loop_.IsCurrent());
73 }
74
75 MOCK_METHOD2(OnFirstFrame,
76 uint64_t(const gfx::Size& frame_size, double frame_rate));
77 MOCK_METHOD4(OnEncodedVideo,
78 void(uint64_t track_index,
79 const base::StringPiece& encoded_data,
80 base::TimeDelta timestamp,
81 bool keyframe));
82
83 void Encode(const scoped_refptr<media::VideoFrame>& frame,
84 const base::TimeTicks& estimated_capture_time) {
85 child_process_->io_task_runner()->PostTask(
86 FROM_HERE, base::Bind(&VideoTrackRecorder::StartFrameEncodeForTesting,
87 base::Unretained(video_track_recorder_.get()),
88 frame, estimated_capture_time));
89 }
90
91 // A ChildProcess and a MessageLoopForUI are both needed to fool the tracks
92 // and sources below into believing they are on the right threads.
93 const base::MessageLoopForUI message_loop_;
94 const scoped_ptr<ChildProcess> child_process_;
95
96 // All members are non-const due to the series of initialize() calls needed.
97 // |mock_source_| is owned by |blink_source_|, |track_| by |blink_track_|.
98 MockMediaStreamVideoSource* mock_source_;
99 blink::WebMediaStreamSource blink_source_;
100 MediaStreamVideoTrack* track_;
101 blink::WebMediaStreamTrack blink_track_;
102
103 scoped_ptr<VideoTrackRecorder> video_track_recorder_;
104
105 private:
106 DISALLOW_COPY_AND_ASSIGN(VideoTrackRecorderTest);
107 };
108
109 // Creates the encoder and encodes 2 frames; the encoder should be initialised
110 // and produce a keyframe, then a non-keyframe.
111 TEST_F(VideoTrackRecorderTest, VideoEncoding) {
112 // |frame_size| cannot be arbitrarily small, should be reasonable.
113 const gfx::Size frame_size(160, 80);
114 const scoped_refptr<media::VideoFrame> video_frame =
115 media::VideoFrame::CreateBlackFrame(frame_size);
116 const double kFrameRate = 60.0f;
117 video_frame->metadata()->SetDouble(media::VideoFrameMetadata::FRAME_RATE,
118 kFrameRate);
119 const int kTrackIndex = 5;
120 const base::TimeDelta timedelta = base::TimeDelta::FromMilliseconds(0);
121
122 InSequence s;
123 EXPECT_CALL(*this, OnFirstFrame(frame_size, kFrameRate))
124 .Times(1)
125 .WillOnce(Return(kTrackIndex));
126 base::StringPiece first_frame_encoded_data;
127 EXPECT_CALL(*this, OnEncodedVideo(kTrackIndex, _, timedelta, true))
128 .Times(1)
129 .WillOnce(SaveArg<1>(&first_frame_encoded_data));
130 Encode(video_frame, base::TimeTicks::Now());
131
132 // Send another Video Frame and expect only an OnEncodedVideo() callback.
133 EXPECT_CALL(*this, OnFirstFrame(frame_size, kFrameRate)).Times(0);
134 base::StringPiece second_frame_encoded_data;
135 EXPECT_CALL(*this, OnEncodedVideo(kTrackIndex, _, _, false))
136 .Times(1)
137 .WillOnce(SaveArg<1>(&second_frame_encoded_data));
138 Encode(video_frame, base::TimeTicks::Now());
139
140 base::MessageLoopForUI::current()->RunUntilIdle();
141 child_process_->io_message_loop()->RunUntilIdle();
142
143 const size_t kFirstEncodedDataSize = 52;
144 EXPECT_EQ(first_frame_encoded_data.size(), kFirstEncodedDataSize);
145 const size_t kSecondEncodedDataSize = 33;
146 EXPECT_EQ(second_frame_encoded_data.size(), kSecondEncodedDataSize);
147
148 Mock::VerifyAndClearExpectations(this);
149 }
150
151 // Creates the encoder and encodes 2 frames, changing the resolution in between.
152 TEST_F(VideoTrackRecorderTest, VideoEncodingVariableResolution) {
153 const gfx::Size frame_size(160, 80);
154 const scoped_refptr<media::VideoFrame> video_frame =
155 media::VideoFrame::CreateBlackFrame(frame_size);
156 const double kFrameRate = 60.0f;
157 video_frame->metadata()->SetDouble(media::VideoFrameMetadata::FRAME_RATE,
158 kFrameRate);
159 const int kTrackIndex = 5;
160 const base::TimeDelta timedelta = base::TimeDelta::FromMilliseconds(0);
161
162 InSequence s;
163 EXPECT_CALL(*this, OnFirstFrame(frame_size, kFrameRate))
164 .Times(1)
165 .WillOnce(Return(kTrackIndex));
166 base::StringPiece first_frame_encoded_data;
167 EXPECT_CALL(*this, OnEncodedVideo(kTrackIndex, _, timedelta, true))
168 .Times(1)
169 .WillOnce(SaveArg<1>(&first_frame_encoded_data));
170 Encode(video_frame, base::TimeTicks::Now());
171
172 // Send another Video Frame and expect only an OnEncodedVideo() callback.
173 const gfx::Size frame_size2(140, 80);
174 const scoped_refptr<media::VideoFrame> video_frame2 =
175 media::VideoFrame::CreateBlackFrame(frame_size2);
176
177 EXPECT_CALL(*this, OnFirstFrame(frame_size2, kFrameRate)).Times(0);
178 base::StringPiece second_frame_encoded_data;
179 EXPECT_CALL(*this, OnEncodedVideo(kTrackIndex, _, _, true))
180 .Times(1)
181 .WillOnce(SaveArg<1>(&second_frame_encoded_data));
182 Encode(video_frame2, base::TimeTicks::Now());
183
184 base::MessageLoopForUI::current()->RunUntilIdle();
185 child_process_->io_message_loop()->RunUntilIdle();
186
187 const size_t kFirstEncodedDataSize = 52;
188 EXPECT_EQ(first_frame_encoded_data.size(), kFirstEncodedDataSize);
189 const size_t kSecondEncodedDataSize = 50;
190 EXPECT_EQ(second_frame_encoded_data.size(), kSecondEncodedDataSize);
191
192 Mock::VerifyAndClearExpectations(this);
193 }
194
195 } // namespace content
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698