| 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::DoAll; |
| 24 using ::testing::InSequence; |
| 25 using ::testing::Mock; |
| 26 using ::testing::Return; |
| 27 using ::testing::SaveArg; |
| 28 |
| 29 namespace content { |
| 30 |
| 31 ACTION_P(RunClosure, closure) { |
| 32 closure.Run(); |
| 33 } |
| 34 |
| 35 // Dummy interface class to be able to MOCK its methods. |
| 36 class EncodedVideoHandlerInterface { |
| 37 public: |
| 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() {} |
| 44 }; |
| 45 |
| 46 class VideoTrackRecorderTest : public testing::Test, |
| 47 public EncodedVideoHandlerInterface { |
| 48 public: |
| 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, |
| 54 webkit_track_id); |
| 55 blink_source_.setExtraData(mock_source_); |
| 56 blink_track_.initialize(blink_source_); |
| 57 |
| 58 blink::WebMediaConstraints constraints; |
| 59 constraints.initialize(); |
| 60 track_ = new MediaStreamVideoTrack(mock_source_, constraints, |
| 61 MediaStreamSource::ConstraintsCallback(), |
| 62 true /* enabled */); |
| 63 blink_track_.setExtraData(track_); |
| 64 |
| 65 video_track_recorder_.reset(new VideoTrackRecorder( |
| 66 blink_track_, |
| 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_METHOD4(OnEncodedVideo, |
| 76 void(const scoped_refptr<media::VideoFrame>& frame, |
| 77 const base::StringPiece& encoded_data, |
| 78 base::TimeTicks timestamp, |
| 79 bool keyframe)); |
| 80 |
| 81 void Encode(const scoped_refptr<media::VideoFrame>& frame, |
| 82 base::TimeTicks capture_time) { |
| 83 EXPECT_TRUE(message_loop_.IsCurrent()); |
| 84 video_track_recorder_->OnVideoFrame(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 larger 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 |
| 117 InSequence s; |
| 118 const base::TimeTicks timeticks_now = base::TimeTicks::Now(); |
| 119 base::StringPiece first_frame_encoded_data; |
| 120 EXPECT_CALL(*this, OnEncodedVideo(video_frame, _, timeticks_now, true)) |
| 121 .Times(1) |
| 122 .WillOnce(SaveArg<1>(&first_frame_encoded_data)); |
| 123 Encode(video_frame, timeticks_now); |
| 124 |
| 125 // Send another Video Frame. |
| 126 const base::TimeTicks timeticks_later = base::TimeTicks::Now(); |
| 127 base::StringPiece second_frame_encoded_data; |
| 128 EXPECT_CALL(*this, OnEncodedVideo(video_frame, _, timeticks_later, false)) |
| 129 .Times(1) |
| 130 .WillOnce(SaveArg<1>(&second_frame_encoded_data)); |
| 131 Encode(video_frame, timeticks_later); |
| 132 |
| 133 // Send another Video Frame and expect only an OnEncodedVideo() callback. |
| 134 const gfx::Size frame_size2(180, 80); |
| 135 const scoped_refptr<media::VideoFrame> video_frame2 = |
| 136 media::VideoFrame::CreateBlackFrame(frame_size2); |
| 137 |
| 138 base::RunLoop run_loop; |
| 139 base::Closure quit_closure = run_loop.QuitClosure(); |
| 140 |
| 141 base::StringPiece third_frame_encoded_data; |
| 142 EXPECT_CALL(*this, OnEncodedVideo(video_frame2, _, _, true)) |
| 143 .Times(1) |
| 144 .WillOnce(DoAll(SaveArg<1>(&third_frame_encoded_data), |
| 145 RunClosure(quit_closure))); |
| 146 Encode(video_frame2, base::TimeTicks::Now()); |
| 147 |
| 148 run_loop.Run(); |
| 149 |
| 150 const size_t kFirstEncodedDataSize = 52; |
| 151 EXPECT_EQ(first_frame_encoded_data.size(), kFirstEncodedDataSize); |
| 152 const size_t kSecondEncodedDataSize = 32; |
| 153 EXPECT_EQ(second_frame_encoded_data.size(), kSecondEncodedDataSize); |
| 154 const size_t kThirdEncodedDataSize = 57; |
| 155 EXPECT_EQ(third_frame_encoded_data.size(), kThirdEncodedDataSize); |
| 156 |
| 157 Mock::VerifyAndClearExpectations(this); |
| 158 } |
| 159 |
| 160 } // namespace content |
| OLD | NEW |