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.
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"
23 using ::testing::DoAll
;
24 using ::testing::InSequence
;
25 using ::testing::Mock
;
26 using ::testing::Return
;
27 using ::testing::SaveArg
;
31 ACTION_P(RunClosure
, closure
) {
35 // Dummy interface class to be able to MOCK its methods.
36 class EncodedVideoHandlerInterface
{
38 virtual void OnEncodedVideo(
39 const scoped_refptr
<media::VideoFrame
>& video_frame
,
40 const base::StringPiece
& encoded_data
,
41 base::TimeTicks timestamp
,
42 bool is_key_frame
) = 0;
43 virtual ~EncodedVideoHandlerInterface() {}
46 class VideoTrackRecorderTest
: public testing::Test
,
47 public EncodedVideoHandlerInterface
{
49 VideoTrackRecorderTest()
50 : mock_source_(new MockMediaStreamVideoSource(false)) {
51 const blink::WebString
webkit_track_id(base::UTF8ToUTF16("dummy"));
52 blink_source_
.initialize(webkit_track_id
,
53 blink::WebMediaStreamSource::TypeVideo
,
55 blink_source_
.setExtraData(mock_source_
);
56 blink_track_
.initialize(blink_source_
);
58 blink::WebMediaConstraints constraints
;
59 constraints
.initialize();
60 track_
= new MediaStreamVideoTrack(mock_source_
, constraints
,
61 MediaStreamSource::ConstraintsCallback(),
63 blink_track_
.setExtraData(track_
);
65 video_track_recorder_
.reset(new VideoTrackRecorder(
67 base::Bind(&VideoTrackRecorderTest::OnEncodedVideo
,
68 base::Unretained(this))));
71 EXPECT_EQ(blink_track_
.source().extraData(), blink_source_
.extraData());
72 EXPECT_TRUE(message_loop_
.IsCurrent());
75 MOCK_METHOD4(OnEncodedVideo
,
76 void(const scoped_refptr
<media::VideoFrame
>& frame
,
77 const base::StringPiece
& encoded_data
,
78 base::TimeTicks timestamp
,
81 void Encode(const scoped_refptr
<media::VideoFrame
>& frame
,
82 base::TimeTicks capture_time
) {
83 EXPECT_TRUE(message_loop_
.IsCurrent());
84 video_track_recorder_
->OnVideoFrameForTesting(frame
, capture_time
);
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 const ChildProcess child_process_
;
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_
;
99 scoped_ptr
<VideoTrackRecorder
> video_track_recorder_
;
102 DISALLOW_COPY_AND_ASSIGN(VideoTrackRecorderTest
);
105 // Construct and destruct all objects, in particular |video_track_recorder_| and
106 // its inner object(s). This is a non trivial sequence.
107 TEST_F(VideoTrackRecorderTest
, ConstructAndDestruct
) {}
109 // Creates the encoder and encodes 2 frames of the same size; the encoder should
110 // be initialised and produce a keyframe, then a non-keyframe. Finally a frame
111 // of larger size is sent and is expected to be encoded as a keyframe.
112 TEST_F(VideoTrackRecorderTest
, VideoEncoding
) {
113 // |frame_size| cannot be arbitrarily small, should be reasonable.
114 const gfx::Size
frame_size(160, 80);
115 const scoped_refptr
<media::VideoFrame
> video_frame
=
116 media::VideoFrame::CreateBlackFrame(frame_size
);
117 const double kFrameRate
= 60.0f
;
118 video_frame
->metadata()->SetDouble(media::VideoFrameMetadata::FRAME_RATE
,
122 const base::TimeTicks timeticks_now
= base::TimeTicks::Now();
123 base::StringPiece first_frame_encoded_data
;
124 EXPECT_CALL(*this, OnEncodedVideo(video_frame
, _
, timeticks_now
, true))
126 .WillOnce(SaveArg
<1>(&first_frame_encoded_data
));
127 Encode(video_frame
, timeticks_now
);
129 // Send another Video Frame.
130 const base::TimeTicks timeticks_later
= base::TimeTicks::Now();
131 base::StringPiece second_frame_encoded_data
;
132 EXPECT_CALL(*this, OnEncodedVideo(video_frame
, _
, timeticks_later
, false))
134 .WillOnce(SaveArg
<1>(&second_frame_encoded_data
));
135 Encode(video_frame
, timeticks_later
);
137 // Send another Video Frame and expect only an OnEncodedVideo() callback.
138 const gfx::Size
frame_size2(180, 80);
139 const scoped_refptr
<media::VideoFrame
> video_frame2
=
140 media::VideoFrame::CreateBlackFrame(frame_size2
);
142 base::RunLoop run_loop
;
143 base::Closure quit_closure
= run_loop
.QuitClosure();
145 base::StringPiece third_frame_encoded_data
;
146 EXPECT_CALL(*this, OnEncodedVideo(video_frame2
, _
, _
, true))
148 .WillOnce(DoAll(SaveArg
<1>(&third_frame_encoded_data
),
149 RunClosure(quit_closure
)));
150 Encode(video_frame2
, base::TimeTicks::Now());
154 const size_t kFirstEncodedDataSize
= 52;
155 EXPECT_EQ(first_frame_encoded_data
.size(), kFirstEncodedDataSize
);
156 const size_t kSecondEncodedDataSize
= 32;
157 EXPECT_EQ(second_frame_encoded_data
.size(), kSecondEncodedDataSize
);
158 const size_t kThirdEncodedDataSize
= 57;
159 EXPECT_EQ(third_frame_encoded_data
.size(), kThirdEncodedDataSize
);
161 Mock::VerifyAndClearExpectations(this);
164 } // namespace content