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 | |
Niels Möller | dd6633b | 2019-01-18 14:23:12 +0100 | [diff] [blame^] | 13 | #include "media/base/fake_frame_source.h" |
Steve Anton | 10542f2 | 2019-01-11 09:11:00 -0800 | [diff] [blame] | 14 | #include "media/base/video_common.h" |
| 15 | #include "pc/test/fake_video_track_renderer.h" |
Niels Möller | dd6633b | 2019-01-18 14:23:12 +0100 | [diff] [blame^] | 16 | #include "pc/test/fake_video_track_source.h" |
Steve Anton | 10542f2 | 2019-01-11 09:11:00 -0800 | [diff] [blame] | 17 | #include "pc/video_track.h" |
| 18 | #include "pc/video_track_source.h" |
| 19 | #include "rtc_base/ref_counted_object.h" |
Yves Gerey | 3e70781 | 2018-11-28 16:47:49 +0100 | [diff] [blame] | 20 | #include "test/gtest.h" |
henrike@webrtc.org | 28e2075 | 2013-07-10 00:45:36 +0000 | [diff] [blame] | 21 | |
| 22 | using webrtc::FakeVideoTrackRenderer; |
Niels Möller | dd6633b | 2019-01-18 14:23:12 +0100 | [diff] [blame^] | 23 | using webrtc::FakeVideoTrackSource; |
perkj | c8f952d | 2016-03-23 00:33:56 -0700 | [diff] [blame] | 24 | using webrtc::MediaSourceInterface; |
| 25 | using webrtc::MediaStreamTrackInterface; |
henrike@webrtc.org | 28e2075 | 2013-07-10 00:45:36 +0000 | [diff] [blame] | 26 | using webrtc::VideoTrack; |
| 27 | using webrtc::VideoTrackInterface; |
Niels Möller | dd6633b | 2019-01-18 14:23:12 +0100 | [diff] [blame^] | 28 | using webrtc::VideoTrackSource; |
henrike@webrtc.org | 28e2075 | 2013-07-10 00:45:36 +0000 | [diff] [blame] | 29 | |
guoweis@webrtc.org | 00c509a | 2015-03-12 21:37:26 +0000 | [diff] [blame] | 30 | class VideoTrackTest : public testing::Test { |
| 31 | public: |
Niels Möller | dd6633b | 2019-01-18 14:23:12 +0100 | [diff] [blame^] | 32 | VideoTrackTest() : frame_source_(640, 480, rtc::kNumMicrosecsPerSec / 30) { |
guoweis@webrtc.org | 00c509a | 2015-03-12 21:37:26 +0000 | [diff] [blame] | 33 | static const char kVideoTrackId[] = "track_id"; |
Niels Möller | dd6633b | 2019-01-18 14:23:12 +0100 | [diff] [blame^] | 34 | video_track_source_ = new rtc::RefCountedObject<FakeVideoTrackSource>( |
| 35 | /*is_screencast=*/false); |
perkj | 773be36 | 2017-07-31 23:22:01 -0700 | [diff] [blame] | 36 | video_track_ = VideoTrack::Create(kVideoTrackId, video_track_source_, |
| 37 | rtc::Thread::Current()); |
guoweis@webrtc.org | 00c509a | 2015-03-12 21:37:26 +0000 | [diff] [blame] | 38 | } |
| 39 | |
| 40 | protected: |
Niels Möller | dd6633b | 2019-01-18 14:23:12 +0100 | [diff] [blame^] | 41 | rtc::scoped_refptr<FakeVideoTrackSource> video_track_source_; |
guoweis@webrtc.org | 00c509a | 2015-03-12 21:37:26 +0000 | [diff] [blame] | 42 | rtc::scoped_refptr<VideoTrackInterface> video_track_; |
Niels Möller | dd6633b | 2019-01-18 14:23:12 +0100 | [diff] [blame^] | 43 | cricket::FakeFrameSource frame_source_; |
guoweis@webrtc.org | 00c509a | 2015-03-12 21:37:26 +0000 | [diff] [blame] | 44 | }; |
| 45 | |
perkj | c8f952d | 2016-03-23 00:33:56 -0700 | [diff] [blame] | 46 | // Test changing the source state also changes the track state. |
| 47 | TEST_F(VideoTrackTest, SourceStateChangeTrackState) { |
| 48 | EXPECT_EQ(MediaStreamTrackInterface::kLive, video_track_->state()); |
| 49 | video_track_source_->SetState(MediaSourceInterface::kEnded); |
| 50 | EXPECT_EQ(MediaStreamTrackInterface::kEnded, video_track_->state()); |
| 51 | } |
| 52 | |
henrike@webrtc.org | 28e2075 | 2013-07-10 00:45:36 +0000 | [diff] [blame] | 53 | // 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] | 54 | // frames to the source. |
guoweis@webrtc.org | 00c509a | 2015-03-12 21:37:26 +0000 | [diff] [blame] | 55 | TEST_F(VideoTrackTest, RenderVideo) { |
| 56 | // FakeVideoTrackRenderer register itself to |video_track_| |
kwiberg | d1fe281 | 2016-04-27 06:47:29 -0700 | [diff] [blame] | 57 | std::unique_ptr<FakeVideoTrackRenderer> renderer_1( |
guoweis@webrtc.org | 00c509a | 2015-03-12 21:37:26 +0000 | [diff] [blame] | 58 | new FakeVideoTrackRenderer(video_track_.get())); |
guoweis@webrtc.org | f9a75d9 | 2015-03-10 06:37:00 +0000 | [diff] [blame] | 59 | |
Niels Möller | dd6633b | 2019-01-18 14:23:12 +0100 | [diff] [blame^] | 60 | video_track_source_->InjectFrame(frame_source_.GetFrame()); |
henrike@webrtc.org | 28e2075 | 2013-07-10 00:45:36 +0000 | [diff] [blame] | 61 | EXPECT_EQ(1, renderer_1->num_rendered_frames()); |
| 62 | |
guoweis@webrtc.org | 00c509a | 2015-03-12 21:37:26 +0000 | [diff] [blame] | 63 | // FakeVideoTrackRenderer register itself to |video_track_| |
kwiberg | d1fe281 | 2016-04-27 06:47:29 -0700 | [diff] [blame] | 64 | std::unique_ptr<FakeVideoTrackRenderer> renderer_2( |
guoweis@webrtc.org | 00c509a | 2015-03-12 21:37:26 +0000 | [diff] [blame] | 65 | new FakeVideoTrackRenderer(video_track_.get())); |
Niels Möller | dd6633b | 2019-01-18 14:23:12 +0100 | [diff] [blame^] | 66 | video_track_source_->InjectFrame(frame_source_.GetFrame()); |
henrike@webrtc.org | 28e2075 | 2013-07-10 00:45:36 +0000 | [diff] [blame] | 67 | EXPECT_EQ(2, renderer_1->num_rendered_frames()); |
| 68 | EXPECT_EQ(1, renderer_2->num_rendered_frames()); |
| 69 | |
perkj | d6c3954 | 2016-03-17 10:35:23 +0100 | [diff] [blame] | 70 | renderer_1.reset(nullptr); |
Niels Möller | dd6633b | 2019-01-18 14:23:12 +0100 | [diff] [blame^] | 71 | video_track_source_->InjectFrame(frame_source_.GetFrame()); |
nisse | db25d2e | 2016-02-26 01:24:58 -0800 | [diff] [blame] | 72 | EXPECT_EQ(2, renderer_2->num_rendered_frames()); |
| 73 | } |
| 74 | |
nisse | 81354f5 | 2016-01-19 00:23:24 -0800 | [diff] [blame] | 75 | // Test that disabling the track results in blacked out frames. |
| 76 | TEST_F(VideoTrackTest, DisableTrackBlackout) { |
kwiberg | d1fe281 | 2016-04-27 06:47:29 -0700 | [diff] [blame] | 77 | std::unique_ptr<FakeVideoTrackRenderer> renderer( |
nisse | 81354f5 | 2016-01-19 00:23:24 -0800 | [diff] [blame] | 78 | new FakeVideoTrackRenderer(video_track_.get())); |
| 79 | |
Niels Möller | dd6633b | 2019-01-18 14:23:12 +0100 | [diff] [blame^] | 80 | video_track_source_->InjectFrame(frame_source_.GetFrame()); |
nisse | 81354f5 | 2016-01-19 00:23:24 -0800 | [diff] [blame] | 81 | EXPECT_EQ(1, renderer->num_rendered_frames()); |
| 82 | EXPECT_FALSE(renderer->black_frame()); |
nisse | 81354f5 | 2016-01-19 00:23:24 -0800 | [diff] [blame] | 83 | |
| 84 | video_track_->set_enabled(false); |
Niels Möller | dd6633b | 2019-01-18 14:23:12 +0100 | [diff] [blame^] | 85 | video_track_source_->InjectFrame(frame_source_.GetFrame()); |
nisse | 81354f5 | 2016-01-19 00:23:24 -0800 | [diff] [blame] | 86 | EXPECT_EQ(2, renderer->num_rendered_frames()); |
| 87 | EXPECT_TRUE(renderer->black_frame()); |
nisse | 81354f5 | 2016-01-19 00:23:24 -0800 | [diff] [blame] | 88 | |
| 89 | video_track_->set_enabled(true); |
Niels Möller | dd6633b | 2019-01-18 14:23:12 +0100 | [diff] [blame^] | 90 | video_track_source_->InjectFrame(frame_source_.GetFrame()); |
nisse | 81354f5 | 2016-01-19 00:23:24 -0800 | [diff] [blame] | 91 | EXPECT_EQ(3, renderer->num_rendered_frames()); |
| 92 | EXPECT_FALSE(renderer->black_frame()); |
nisse | 81354f5 | 2016-01-19 00:23:24 -0800 | [diff] [blame] | 93 | } |