Chromium Code Reviews| OLD | NEW |
|---|---|
| (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 blink_track_, | |
| 64 base::Bind(&VideoTrackRecorderTest::OnFirstFrame, | |
| 65 base::Unretained(this)), | |
| 66 base::Bind(&VideoTrackRecorderTest::OnEncodedVideo, | |
| 67 base::Unretained(this)))); | |
| 68 | |
| 69 // Paranoia checks. | |
| 70 EXPECT_EQ(blink_track_.source().extraData(), blink_source_.extraData()); | |
| 71 EXPECT_TRUE(message_loop_.IsCurrent()); | |
| 72 } | |
| 73 | |
| 74 MOCK_METHOD2(OnFirstFrame, | |
| 75 uint64_t(const gfx::Size& frame_size, double frame_rate)); | |
| 76 MOCK_METHOD4(OnEncodedVideo, | |
| 77 void(uint64_t track_index, | |
| 78 const base::StringPiece& encoded_data, | |
| 79 base::TimeDelta timestamp, | |
| 80 bool keyframe)); | |
| 81 | |
| 82 void Encode(const scoped_refptr<media::VideoFrame>& frame, | |
| 83 const base::TimeTicks& capture_time) { | |
| 84 EXPECT_TRUE(message_loop_.IsCurrent()); | |
| 85 video_track_recorder_->StartFrameEncodeForTesting(frame, capture_time); | |
| 86 } | |
| 87 | |
| 88 // A ChildProcess and a MessageLoopForUI are both needed to fool the Tracks | |
| 89 // and Sources below into believing they are on the right threads. | |
| 90 const base::MessageLoopForUI message_loop_; | |
| 91 const scoped_ptr<ChildProcess> child_process_; | |
|
miu
2015/08/04 04:06:30
nit: Can this just be:
ChildProcess child_proce
mcasas
2015/08/11 09:35:17
Done.
| |
| 92 | |
| 93 // All members are non-const due to the series of initialize() calls needed. | |
| 94 // |mock_source_| is owned by |blink_source_|, |track_| by |blink_track_|. | |
| 95 MockMediaStreamVideoSource* mock_source_; | |
| 96 blink::WebMediaStreamSource blink_source_; | |
| 97 MediaStreamVideoTrack* track_; | |
| 98 blink::WebMediaStreamTrack blink_track_; | |
| 99 | |
| 100 scoped_ptr<VideoTrackRecorder> video_track_recorder_; | |
| 101 | |
| 102 private: | |
| 103 DISALLOW_COPY_AND_ASSIGN(VideoTrackRecorderTest); | |
| 104 }; | |
| 105 | |
| 106 // Creates the encoder and encodes 2 frames of the same size; the encoder should | |
| 107 // be initialised and produce a keyframe, then a non-keyframe. Finally a frame | |
| 108 // of a different size is sent and is expected to be encoded as a keyframe. | |
| 109 TEST_F(VideoTrackRecorderTest, VideoEncoding) { | |
| 110 // |frame_size| cannot be arbitrarily small, should be reasonable. | |
| 111 const gfx::Size frame_size(160, 80); | |
| 112 const scoped_refptr<media::VideoFrame> video_frame = | |
| 113 media::VideoFrame::CreateBlackFrame(frame_size); | |
| 114 const double kFrameRate = 60.0f; | |
| 115 video_frame->metadata()->SetDouble(media::VideoFrameMetadata::FRAME_RATE, | |
| 116 kFrameRate); | |
| 117 const int kTrackIndex = 5; | |
| 118 const base::TimeDelta timedelta = base::TimeDelta::FromMilliseconds(0); | |
| 119 | |
| 120 InSequence s; | |
| 121 EXPECT_CALL(*this, OnFirstFrame(frame_size, kFrameRate)) | |
| 122 .Times(1) | |
| 123 .WillOnce(Return(kTrackIndex)); | |
| 124 base::StringPiece first_frame_encoded_data; | |
| 125 EXPECT_CALL(*this, OnEncodedVideo(kTrackIndex, _, timedelta, true)) | |
| 126 .Times(1) | |
| 127 .WillOnce(SaveArg<1>(&first_frame_encoded_data)); | |
| 128 Encode(video_frame, base::TimeTicks::Now()); | |
| 129 | |
| 130 // Send another Video Frame and expect only an OnEncodedVideo() callback. | |
| 131 EXPECT_CALL(*this, OnFirstFrame(frame_size, kFrameRate)).Times(0); | |
| 132 base::StringPiece second_frame_encoded_data; | |
| 133 EXPECT_CALL(*this, OnEncodedVideo(kTrackIndex, _, _, false)) | |
| 134 .Times(1) | |
| 135 .WillOnce(SaveArg<1>(&second_frame_encoded_data)); | |
| 136 Encode(video_frame, base::TimeTicks::Now()); | |
| 137 | |
| 138 // Send another Video Frame and expect only an OnEncodedVideo() callback. | |
| 139 const gfx::Size frame_size2(140, 80); | |
| 140 const scoped_refptr<media::VideoFrame> video_frame2 = | |
| 141 media::VideoFrame::CreateBlackFrame(frame_size2); | |
| 142 | |
| 143 EXPECT_CALL(*this, OnFirstFrame(frame_size2, kFrameRate)).Times(0); | |
| 144 base::StringPiece third_frame_encoded_data; | |
| 145 EXPECT_CALL(*this, OnEncodedVideo(kTrackIndex, _, _, true)) | |
| 146 .Times(1) | |
| 147 .WillOnce(SaveArg<1>(&third_frame_encoded_data)); | |
| 148 Encode(video_frame2, base::TimeTicks::Now()); | |
| 149 | |
| 150 child_process_->io_message_loop()->RunUntilIdle(); | |
| 151 base::RunLoop().RunUntilIdle(); | |
| 152 | |
| 153 const size_t kFirstEncodedDataSize = 52; | |
| 154 EXPECT_EQ(first_frame_encoded_data.size(), kFirstEncodedDataSize); | |
| 155 const size_t kSecondEncodedDataSize = 33; | |
| 156 EXPECT_EQ(second_frame_encoded_data.size(), kSecondEncodedDataSize); | |
| 157 const size_t kThirdEncodedDataSize = 50; | |
| 158 EXPECT_EQ(third_frame_encoded_data.size(), kThirdEncodedDataSize); | |
| 159 | |
| 160 Mock::VerifyAndClearExpectations(this); | |
| 161 } | |
| 162 | |
| 163 } // namespace content | |
| OLD | NEW |