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