henrike@webrtc.org | 28e2075 | 2013-07-10 00:45:36 +0000 | [diff] [blame] | 1 | /* |
kjellander | b24317b | 2016-02-10 07:54:43 -0800 | [diff] [blame] | 2 | * Copyright 2012 The WebRTC project authors. All Rights Reserved. |
henrike@webrtc.org | 28e2075 | 2013-07-10 00:45:36 +0000 | [diff] [blame] | 3 | * |
kjellander | b24317b | 2016-02-10 07:54:43 -0800 | [diff] [blame] | 4 | * Use of this source code is governed by a BSD-style license |
| 5 | * that can be found in the LICENSE file in the root of the source |
| 6 | * tree. An additional intellectual property rights grant can be found |
| 7 | * in the file PATENTS. All contributing project authors may |
| 8 | * be found in the AUTHORS file in the root of the source tree. |
henrike@webrtc.org | 28e2075 | 2013-07-10 00:45:36 +0000 | [diff] [blame] | 9 | */ |
| 10 | |
kwiberg | d1fe281 | 2016-04-27 06:47:29 -0700 | [diff] [blame] | 11 | #include <memory> |
henrike@webrtc.org | 28e2075 | 2013-07-10 00:45:36 +0000 | [diff] [blame] | 12 | #include <string> |
| 13 | |
Mirko Bonadei | 92ea95e | 2017-09-15 06:47:31 +0200 | [diff] [blame] | 14 | #include "media/base/fakemediaengine.h" |
| 15 | #include "media/base/fakevideocapturer.h" |
| 16 | #include "pc/test/fakevideotrackrenderer.h" |
| 17 | #include "pc/videocapturertracksource.h" |
| 18 | #include "pc/videotrack.h" |
| 19 | #include "rtc_base/gunit.h" |
henrike@webrtc.org | 28e2075 | 2013-07-10 00:45:36 +0000 | [diff] [blame] | 20 | |
| 21 | using webrtc::FakeVideoTrackRenderer; |
perkj | c8f952d | 2016-03-23 00:33:56 -0700 | [diff] [blame] | 22 | using webrtc::MediaSourceInterface; |
| 23 | using webrtc::MediaStreamTrackInterface; |
perkj | f0dcfe2 | 2016-03-10 18:32:00 +0100 | [diff] [blame] | 24 | using webrtc::VideoTrackSource; |
henrike@webrtc.org | 28e2075 | 2013-07-10 00:45:36 +0000 | [diff] [blame] | 25 | using webrtc::VideoTrack; |
| 26 | using webrtc::VideoTrackInterface; |
| 27 | |
Niels Möller | 5d67f82 | 2018-05-23 16:28:17 +0200 | [diff] [blame] | 28 | class TestVideoTrackSource : public VideoTrackSource { |
| 29 | public: |
| 30 | TestVideoTrackSource() : VideoTrackSource(true /* remote */) {} |
| 31 | rtc::VideoSourceInterface<webrtc::VideoFrame>* source() override { |
| 32 | return &capturer_; |
| 33 | } |
| 34 | cricket::FakeVideoCapturerWithTaskQueue* capturer() { return &capturer_; } |
| 35 | |
| 36 | private: |
| 37 | cricket::FakeVideoCapturerWithTaskQueue capturer_; |
| 38 | }; |
guoweis@webrtc.org | 00c509a | 2015-03-12 21:37:26 +0000 | [diff] [blame] | 39 | class VideoTrackTest : public testing::Test { |
| 40 | public: |
| 41 | VideoTrackTest() { |
| 42 | static const char kVideoTrackId[] = "track_id"; |
Niels Möller | 5d67f82 | 2018-05-23 16:28:17 +0200 | [diff] [blame] | 43 | video_track_source_ = new rtc::RefCountedObject<TestVideoTrackSource>(); |
perkj | 773be36 | 2017-07-31 23:22:01 -0700 | [diff] [blame] | 44 | video_track_ = VideoTrack::Create(kVideoTrackId, video_track_source_, |
| 45 | rtc::Thread::Current()); |
Niels Möller | 5d67f82 | 2018-05-23 16:28:17 +0200 | [diff] [blame] | 46 | video_track_source_->capturer()->Start( |
perkj | f0dcfe2 | 2016-03-10 18:32:00 +0100 | [diff] [blame] | 47 | cricket::VideoFormat(640, 480, cricket::VideoFormat::FpsToInterval(30), |
| 48 | cricket::FOURCC_I420)); |
guoweis@webrtc.org | 00c509a | 2015-03-12 21:37:26 +0000 | [diff] [blame] | 49 | } |
| 50 | |
| 51 | protected: |
Niels Möller | 5d67f82 | 2018-05-23 16:28:17 +0200 | [diff] [blame] | 52 | rtc::scoped_refptr<TestVideoTrackSource> video_track_source_; |
guoweis@webrtc.org | 00c509a | 2015-03-12 21:37:26 +0000 | [diff] [blame] | 53 | rtc::scoped_refptr<VideoTrackInterface> video_track_; |
| 54 | }; |
| 55 | |
perkj | c8f952d | 2016-03-23 00:33:56 -0700 | [diff] [blame] | 56 | // Test changing the source state also changes the track state. |
| 57 | TEST_F(VideoTrackTest, SourceStateChangeTrackState) { |
| 58 | EXPECT_EQ(MediaStreamTrackInterface::kLive, video_track_->state()); |
| 59 | video_track_source_->SetState(MediaSourceInterface::kEnded); |
| 60 | EXPECT_EQ(MediaStreamTrackInterface::kEnded, video_track_->state()); |
| 61 | } |
| 62 | |
henrike@webrtc.org | 28e2075 | 2013-07-10 00:45:36 +0000 | [diff] [blame] | 63 | // Test adding renderers to a video track and render to them by providing |
wu@webrtc.org | 967bfff | 2013-09-19 05:49:50 +0000 | [diff] [blame] | 64 | // frames to the source. |
guoweis@webrtc.org | 00c509a | 2015-03-12 21:37:26 +0000 | [diff] [blame] | 65 | TEST_F(VideoTrackTest, RenderVideo) { |
| 66 | // FakeVideoTrackRenderer register itself to |video_track_| |
kwiberg | d1fe281 | 2016-04-27 06:47:29 -0700 | [diff] [blame] | 67 | std::unique_ptr<FakeVideoTrackRenderer> renderer_1( |
guoweis@webrtc.org | 00c509a | 2015-03-12 21:37:26 +0000 | [diff] [blame] | 68 | new FakeVideoTrackRenderer(video_track_.get())); |
guoweis@webrtc.org | f9a75d9 | 2015-03-10 06:37:00 +0000 | [diff] [blame] | 69 | |
Niels Möller | 5d67f82 | 2018-05-23 16:28:17 +0200 | [diff] [blame] | 70 | video_track_source_->capturer()->CaptureFrame(); |
henrike@webrtc.org | 28e2075 | 2013-07-10 00:45:36 +0000 | [diff] [blame] | 71 | EXPECT_EQ(1, renderer_1->num_rendered_frames()); |
| 72 | |
guoweis@webrtc.org | 00c509a | 2015-03-12 21:37:26 +0000 | [diff] [blame] | 73 | // FakeVideoTrackRenderer register itself to |video_track_| |
kwiberg | d1fe281 | 2016-04-27 06:47:29 -0700 | [diff] [blame] | 74 | std::unique_ptr<FakeVideoTrackRenderer> renderer_2( |
guoweis@webrtc.org | 00c509a | 2015-03-12 21:37:26 +0000 | [diff] [blame] | 75 | new FakeVideoTrackRenderer(video_track_.get())); |
Niels Möller | 5d67f82 | 2018-05-23 16:28:17 +0200 | [diff] [blame] | 76 | video_track_source_->capturer()->CaptureFrame(); |
henrike@webrtc.org | 28e2075 | 2013-07-10 00:45:36 +0000 | [diff] [blame] | 77 | EXPECT_EQ(2, renderer_1->num_rendered_frames()); |
| 78 | EXPECT_EQ(1, renderer_2->num_rendered_frames()); |
| 79 | |
perkj | d6c3954 | 2016-03-17 10:35:23 +0100 | [diff] [blame] | 80 | renderer_1.reset(nullptr); |
Niels Möller | 5d67f82 | 2018-05-23 16:28:17 +0200 | [diff] [blame] | 81 | video_track_source_->capturer()->CaptureFrame(); |
nisse | db25d2e | 2016-02-26 01:24:58 -0800 | [diff] [blame] | 82 | EXPECT_EQ(2, renderer_2->num_rendered_frames()); |
| 83 | } |
| 84 | |
nisse | 81354f5 | 2016-01-19 00:23:24 -0800 | [diff] [blame] | 85 | // Test that disabling the track results in blacked out frames. |
| 86 | TEST_F(VideoTrackTest, DisableTrackBlackout) { |
kwiberg | d1fe281 | 2016-04-27 06:47:29 -0700 | [diff] [blame] | 87 | std::unique_ptr<FakeVideoTrackRenderer> renderer( |
nisse | 81354f5 | 2016-01-19 00:23:24 -0800 | [diff] [blame] | 88 | new FakeVideoTrackRenderer(video_track_.get())); |
| 89 | |
Niels Möller | 5d67f82 | 2018-05-23 16:28:17 +0200 | [diff] [blame] | 90 | video_track_source_->capturer()->CaptureFrame(); |
nisse | 81354f5 | 2016-01-19 00:23:24 -0800 | [diff] [blame] | 91 | EXPECT_EQ(1, renderer->num_rendered_frames()); |
| 92 | EXPECT_FALSE(renderer->black_frame()); |
nisse | 81354f5 | 2016-01-19 00:23:24 -0800 | [diff] [blame] | 93 | |
| 94 | video_track_->set_enabled(false); |
Niels Möller | 5d67f82 | 2018-05-23 16:28:17 +0200 | [diff] [blame] | 95 | video_track_source_->capturer()->CaptureFrame(); |
nisse | 81354f5 | 2016-01-19 00:23:24 -0800 | [diff] [blame] | 96 | EXPECT_EQ(2, renderer->num_rendered_frames()); |
| 97 | EXPECT_TRUE(renderer->black_frame()); |
nisse | 81354f5 | 2016-01-19 00:23:24 -0800 | [diff] [blame] | 98 | |
| 99 | video_track_->set_enabled(true); |
Niels Möller | 5d67f82 | 2018-05-23 16:28:17 +0200 | [diff] [blame] | 100 | video_track_source_->capturer()->CaptureFrame(); |
nisse | 81354f5 | 2016-01-19 00:23:24 -0800 | [diff] [blame] | 101 | EXPECT_EQ(3, renderer->num_rendered_frames()); |
| 102 | EXPECT_FALSE(renderer->black_frame()); |
nisse | 81354f5 | 2016-01-19 00:23:24 -0800 | [diff] [blame] | 103 | } |