| 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 ACTION_P(RunClosure, closure) { |
| 31 closure.Run(); |
| 32 } |
| 33 |
| 34 // Dummy interface class to be able to MOCK its methods. |
| 35 class EncodedVideoHandlerInterface { |
| 36 public: |
| 37 virtual uint64_t OnFirstFrame(const gfx::Size& frame_size, |
| 38 double frame_rate) = 0; |
| 39 virtual void OnEncodedVideo(uint64_t track_number, |
| 40 const base::StringPiece& encoded_data, |
| 41 base::TimeDelta timestamp, |
| 42 bool keyframe) = 0; |
| 43 virtual ~EncodedVideoHandlerInterface() {} |
| 44 }; |
| 45 |
| 46 class VideoTrackRecorderTest : public testing::Test, |
| 47 public EncodedVideoHandlerInterface { |
| 48 public: |
| 49 VideoTrackRecorderTest() |
| 50 : child_process_(new ChildProcess()), |
| 51 mock_source_(new MockMediaStreamVideoSource(false)) { |
| 52 const blink::WebString webkit_track_id(base::UTF8ToUTF16("dummy")); |
| 53 blink_source_.initialize(webkit_track_id, |
| 54 blink::WebMediaStreamSource::TypeVideo, |
| 55 webkit_track_id); |
| 56 blink_source_.setExtraData(mock_source_); |
| 57 blink_track_.initialize(blink_source_); |
| 58 |
| 59 blink::WebMediaConstraints constraints; |
| 60 constraints.initialize(); |
| 61 track_ = new MediaStreamVideoTrack(mock_source_, constraints, |
| 62 MediaStreamSource::ConstraintsCallback(), |
| 63 true /* enabled */); |
| 64 blink_track_.setExtraData(track_); |
| 65 |
| 66 video_track_recorder_.reset(new VideoTrackRecorder( |
| 67 blink_track_, base::Bind(&VideoTrackRecorderTest::OnFirstFrame, |
| 68 base::Unretained(this)), |
| 69 base::Bind(&VideoTrackRecorderTest::OnEncodedVideo, |
| 70 base::Unretained(this)))); |
| 71 |
| 72 // Paranoia checks. |
| 73 EXPECT_EQ(blink_track_.source().extraData(), blink_source_.extraData()); |
| 74 EXPECT_TRUE(message_loop_.IsCurrent()); |
| 75 } |
| 76 |
| 77 MOCK_METHOD2(OnFirstFrame, |
| 78 uint64_t(const gfx::Size& frame_size, double frame_rate)); |
| 79 MOCK_METHOD4(OnEncodedVideo, |
| 80 void(uint64_t track_index, |
| 81 const base::StringPiece& encoded_data, |
| 82 base::TimeDelta timestamp, |
| 83 bool keyframe)); |
| 84 |
| 85 void Encode(const scoped_refptr<media::VideoFrame>& frame, |
| 86 const base::TimeTicks& estimated_capture_time) { |
| 87 child_process_->io_message_loop()->PostTask( |
| 88 FROM_HERE, base::Bind(&VideoTrackRecorder::EncodeOnIoForTesting, |
| 89 base::Unretained(video_track_recorder_.get()), |
| 90 frame, estimated_capture_time)); |
| 91 } |
| 92 |
| 93 bool IsEncoderConfigurationDone() const { |
| 94 return video_track_recorder_->IsEncoderConfigurationDoneForTesting(); |
| 95 } |
| 96 |
| 97 // A ChildProcess and a MessageLoopForUI are both needed to fool the tracks |
| 98 // and sources below into believing they are on the right threads. |
| 99 const base::MessageLoopForUI message_loop_; |
| 100 const scoped_ptr<ChildProcess> child_process_; |
| 101 // All members are non-const due to the series of initialize() calls needed. |
| 102 // |mock_source_| is owned by |blink_source_|, |track_| by |blink_track_|. |
| 103 MockMediaStreamVideoSource* mock_source_; |
| 104 blink::WebMediaStreamSource blink_source_; |
| 105 MediaStreamVideoTrack* track_; |
| 106 blink::WebMediaStreamTrack blink_track_; |
| 107 |
| 108 scoped_ptr<VideoTrackRecorder> video_track_recorder_; |
| 109 |
| 110 private: |
| 111 DISALLOW_COPY_AND_ASSIGN(VideoTrackRecorderTest); |
| 112 }; |
| 113 |
| 114 // Creates the encoder and encodes a frame; the encoder should be initialised |
| 115 // and produce a keyframe. |
| 116 TEST_F(VideoTrackRecorderTest, VideoEncoding) { |
| 117 const int kWidth = 160; // Cannot be arbitrarily small, should be reasonable. |
| 118 const int kHeight = 80; |
| 119 const gfx::Size frame_size(kWidth, kHeight); |
| 120 const scoped_refptr<media::VideoFrame> video_frame = |
| 121 media::VideoFrame::CreateBlackFrame(frame_size); |
| 122 const double kFrameRate = 60.0f; |
| 123 video_frame->metadata()->SetDouble(media::VideoFrameMetadata::FRAME_RATE, |
| 124 kFrameRate); |
| 125 const base::TimeTicks capture_time = base::TimeTicks::Now(); |
| 126 const int kTrackIndex = 5; |
| 127 const base::TimeDelta timedelta = base::TimeDelta::FromMilliseconds(0); |
| 128 |
| 129 EXPECT_FALSE(IsEncoderConfigurationDone()); |
| 130 |
| 131 InSequence s; |
| 132 EXPECT_CALL(*this, OnFirstFrame(frame_size, kFrameRate)) |
| 133 .Times(1) |
| 134 .WillOnce(Return(kTrackIndex)); |
| 135 base::StringPiece encoded_data; |
| 136 EXPECT_CALL(*this, OnEncodedVideo(kTrackIndex, _, timedelta, true)) |
| 137 .Times(1) |
| 138 .WillOnce(SaveArg<1>(&encoded_data)); |
| 139 Encode(video_frame, capture_time); |
| 140 |
| 141 base::MessageLoopForUI::current()->RunUntilIdle(); |
| 142 child_process_->io_message_loop()->RunUntilIdle(); |
| 143 |
| 144 EXPECT_TRUE(IsEncoderConfigurationDone()); |
| 145 const size_t kEncodedDataSize = 52; |
| 146 EXPECT_EQ(encoded_data.size(), kEncodedDataSize); |
| 147 |
| 148 Mock::VerifyAndClearExpectations(this); |
| 149 } |
| 150 |
| 151 } // namespace content |
| OLD | NEW |