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

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 comment on OnFirstFrameCB and OnEncodedVideoCB 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 using OnEncodedVideoCB =
34 base::Callback<void(const base::StringPiece&, base::TimeDelta, bool)>;
35
36 virtual OnEncodedVideoCB OnFirstFrame(const gfx::Size& frame_size,
37 double frame_rate) = 0;
38 virtual void OnEncodedVideo(const base::StringPiece& encoded_data,
39 base::TimeDelta timestamp,
40 bool keyframe) = 0;
41 virtual ~EncodedVideoHandlerInterface() {}
42 };
43
44 class VideoTrackRecorderTest : public testing::Test,
45 public EncodedVideoHandlerInterface {
46 public:
47 VideoTrackRecorderTest()
48 : mock_source_(new MockMediaStreamVideoSource(false)) {
49 const blink::WebString webkit_track_id(base::UTF8ToUTF16("dummy"));
50 blink_source_.initialize(webkit_track_id,
51 blink::WebMediaStreamSource::TypeVideo,
52 webkit_track_id);
53 blink_source_.setExtraData(mock_source_);
54 blink_track_.initialize(blink_source_);
55
56 blink::WebMediaConstraints constraints;
57 constraints.initialize();
58 track_ = new MediaStreamVideoTrack(mock_source_, constraints,
59 MediaStreamSource::ConstraintsCallback(),
60 true /* enabled */);
61 blink_track_.setExtraData(track_);
62
63 video_track_recorder_.reset(new VideoTrackRecorder(
64 blink_track_,
65 base::Bind(&VideoTrackRecorderTest::OnFirstFrame,
66 base::Unretained(this))));
67
68 // Paranoia checks.
69 EXPECT_EQ(blink_track_.source().extraData(), blink_source_.extraData());
70 EXPECT_TRUE(message_loop_.IsCurrent());
71 }
72
73 MOCK_METHOD2(OnFirstFrame,
74 OnEncodedVideoCB(const gfx::Size& frame_size,
75 double frame_rate));
76 MOCK_METHOD3(OnEncodedVideo,
77 void(const base::StringPiece& encoded_data,
78 base::TimeDelta timestamp,
79 bool keyframe));
80
81 void Encode(const scoped_refptr<media::VideoFrame>& frame,
82 const base::TimeTicks& capture_time) {
83 EXPECT_TRUE(message_loop_.IsCurrent());
84 video_track_recorder_->StartFrameEncodeForTesting(frame, capture_time);
85 }
86
87 // A ChildProcess and a MessageLoopForUI are both needed to fool the Tracks
88 // and Sources below into believing they are on the right threads.
89 const base::MessageLoopForUI message_loop_;
90 ChildProcess child_process_;
91
92 // All members are non-const due to the series of initialize() calls needed.
93 // |mock_source_| is owned by |blink_source_|, |track_| by |blink_track_|.
94 MockMediaStreamVideoSource* mock_source_;
95 blink::WebMediaStreamSource blink_source_;
96 MediaStreamVideoTrack* track_;
97 blink::WebMediaStreamTrack blink_track_;
98
99 scoped_ptr<VideoTrackRecorder> video_track_recorder_;
100
101 private:
102 DISALLOW_COPY_AND_ASSIGN(VideoTrackRecorderTest);
103 };
104
105 // Creates the encoder and encodes 2 frames of the same size; the encoder should
106 // be initialised and produce a keyframe, then a non-keyframe. Finally a frame
107 // of a different size is sent and is expected to be encoded as a keyframe.
108 TEST_F(VideoTrackRecorderTest, VideoEncoding) {
109 // |frame_size| cannot be arbitrarily small, should be reasonable.
110 const gfx::Size frame_size(160, 80);
111 const scoped_refptr<media::VideoFrame> video_frame =
112 media::VideoFrame::CreateBlackFrame(frame_size);
113 const double kFrameRate = 60.0f;
114 video_frame->metadata()->SetDouble(media::VideoFrameMetadata::FRAME_RATE,
115 kFrameRate);
116 const base::TimeDelta timedelta = base::TimeDelta::FromMilliseconds(0);
117
118 InSequence s;
119 EXPECT_CALL(*this, OnFirstFrame(frame_size, kFrameRate))
120 .Times(1)
121 .WillOnce(Return(base::Bind(&EncodedVideoHandlerInterface::OnEncodedVideo,
122 base::Unretained(this))));
123 base::StringPiece first_frame_encoded_data;
124 EXPECT_CALL(*this, OnEncodedVideo(_, timedelta, true))
125 .Times(1)
126 .WillOnce(SaveArg<0>(&first_frame_encoded_data));
127 Encode(video_frame, base::TimeTicks::Now());
128
129 // Send another Video Frame and expect only an OnEncodedVideo() callback.
130 EXPECT_CALL(*this, OnFirstFrame(frame_size, kFrameRate)).Times(0);
131 base::StringPiece second_frame_encoded_data;
132 EXPECT_CALL(*this, OnEncodedVideo(_, _, false))
133 .Times(1)
134 .WillOnce(SaveArg<0>(&second_frame_encoded_data));
135 Encode(video_frame, base::TimeTicks::Now());
136
137 // Send another Video Frame and expect only an OnEncodedVideo() callback.
138 const gfx::Size frame_size2(140, 80);
139 const scoped_refptr<media::VideoFrame> video_frame2 =
140 media::VideoFrame::CreateBlackFrame(frame_size2);
141
142 EXPECT_CALL(*this, OnFirstFrame(frame_size2, kFrameRate)).Times(0);
143 base::StringPiece third_frame_encoded_data;
144 EXPECT_CALL(*this, OnEncodedVideo(_, _, true))
145 .Times(1)
146 .WillOnce(SaveArg<0>(&third_frame_encoded_data));
147 Encode(video_frame2, base::TimeTicks::Now());
148
149 child_process_.io_message_loop()->RunUntilIdle();
150 base::RunLoop().RunUntilIdle();
151
152 const size_t kFirstEncodedDataSize = 52;
153 EXPECT_EQ(first_frame_encoded_data.size(), kFirstEncodedDataSize);
154 const size_t kSecondEncodedDataSize = 33;
155 EXPECT_EQ(second_frame_encoded_data.size(), kSecondEncodedDataSize);
156 const size_t kThirdEncodedDataSize = 50;
157 EXPECT_EQ(third_frame_encoded_data.size(), kThirdEncodedDataSize);
158
159 Mock::VerifyAndClearExpectations(this);
160 }
161
162 } // namespace content
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698