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 | |
guoweis@webrtc.org | 00c509a | 2015-03-12 21:37:26 +0000 | [diff] [blame] | 28 | class VideoTrackTest : public testing::Test { |
| 29 | public: |
| 30 | VideoTrackTest() { |
| 31 | static const char kVideoTrackId[] = "track_id"; |
perkj | c8f952d | 2016-03-23 00:33:56 -0700 | [diff] [blame] | 32 | video_track_source_ = new rtc::RefCountedObject<VideoTrackSource>( |
nisse | 5b68ab5 | 2016-04-07 07:45:54 -0700 | [diff] [blame] | 33 | &capturer_, true /* remote */); |
perkj | 773be36 | 2017-07-31 23:22:01 -0700 | [diff] [blame] | 34 | video_track_ = VideoTrack::Create(kVideoTrackId, video_track_source_, |
| 35 | rtc::Thread::Current()); |
perkj | f0dcfe2 | 2016-03-10 18:32:00 +0100 | [diff] [blame] | 36 | capturer_.Start( |
| 37 | cricket::VideoFormat(640, 480, cricket::VideoFormat::FpsToInterval(30), |
| 38 | cricket::FOURCC_I420)); |
guoweis@webrtc.org | 00c509a | 2015-03-12 21:37:26 +0000 | [diff] [blame] | 39 | } |
| 40 | |
| 41 | protected: |
Tommi | 8595993 | 2018-02-07 19:26:06 +0100 | [diff] [blame] | 42 | cricket::FakeVideoCapturerWithTaskQueue capturer_; |
perkj | c8f952d | 2016-03-23 00:33:56 -0700 | [diff] [blame] | 43 | rtc::scoped_refptr<VideoTrackSource> video_track_source_; |
guoweis@webrtc.org | 00c509a | 2015-03-12 21:37:26 +0000 | [diff] [blame] | 44 | rtc::scoped_refptr<VideoTrackInterface> video_track_; |
| 45 | }; |
| 46 | |
perkj | c8f952d | 2016-03-23 00:33:56 -0700 | [diff] [blame] | 47 | // Test changing the source state also changes the track state. |
| 48 | TEST_F(VideoTrackTest, SourceStateChangeTrackState) { |
| 49 | EXPECT_EQ(MediaStreamTrackInterface::kLive, video_track_->state()); |
| 50 | video_track_source_->SetState(MediaSourceInterface::kEnded); |
| 51 | EXPECT_EQ(MediaStreamTrackInterface::kEnded, video_track_->state()); |
| 52 | } |
| 53 | |
henrike@webrtc.org | 28e2075 | 2013-07-10 00:45:36 +0000 | [diff] [blame] | 54 | // 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] | 55 | // frames to the source. |
guoweis@webrtc.org | 00c509a | 2015-03-12 21:37:26 +0000 | [diff] [blame] | 56 | TEST_F(VideoTrackTest, RenderVideo) { |
| 57 | // FakeVideoTrackRenderer register itself to |video_track_| |
kwiberg | d1fe281 | 2016-04-27 06:47:29 -0700 | [diff] [blame] | 58 | std::unique_ptr<FakeVideoTrackRenderer> renderer_1( |
guoweis@webrtc.org | 00c509a | 2015-03-12 21:37:26 +0000 | [diff] [blame] | 59 | new FakeVideoTrackRenderer(video_track_.get())); |
guoweis@webrtc.org | f9a75d9 | 2015-03-10 06:37:00 +0000 | [diff] [blame] | 60 | |
perkj | f0dcfe2 | 2016-03-10 18:32:00 +0100 | [diff] [blame] | 61 | capturer_.CaptureFrame(); |
henrike@webrtc.org | 28e2075 | 2013-07-10 00:45:36 +0000 | [diff] [blame] | 62 | EXPECT_EQ(1, renderer_1->num_rendered_frames()); |
| 63 | |
guoweis@webrtc.org | 00c509a | 2015-03-12 21:37:26 +0000 | [diff] [blame] | 64 | // FakeVideoTrackRenderer register itself to |video_track_| |
kwiberg | d1fe281 | 2016-04-27 06:47:29 -0700 | [diff] [blame] | 65 | std::unique_ptr<FakeVideoTrackRenderer> renderer_2( |
guoweis@webrtc.org | 00c509a | 2015-03-12 21:37:26 +0000 | [diff] [blame] | 66 | new FakeVideoTrackRenderer(video_track_.get())); |
perkj | f0dcfe2 | 2016-03-10 18:32:00 +0100 | [diff] [blame] | 67 | capturer_.CaptureFrame(); |
henrike@webrtc.org | 28e2075 | 2013-07-10 00:45:36 +0000 | [diff] [blame] | 68 | EXPECT_EQ(2, renderer_1->num_rendered_frames()); |
| 69 | EXPECT_EQ(1, renderer_2->num_rendered_frames()); |
| 70 | |
perkj | d6c3954 | 2016-03-17 10:35:23 +0100 | [diff] [blame] | 71 | renderer_1.reset(nullptr); |
perkj | f0dcfe2 | 2016-03-10 18:32:00 +0100 | [diff] [blame] | 72 | capturer_.CaptureFrame(); |
nisse | db25d2e | 2016-02-26 01:24:58 -0800 | [diff] [blame] | 73 | EXPECT_EQ(2, renderer_2->num_rendered_frames()); |
| 74 | } |
| 75 | |
nisse | 81354f5 | 2016-01-19 00:23:24 -0800 | [diff] [blame] | 76 | // Test that disabling the track results in blacked out frames. |
| 77 | TEST_F(VideoTrackTest, DisableTrackBlackout) { |
kwiberg | d1fe281 | 2016-04-27 06:47:29 -0700 | [diff] [blame] | 78 | std::unique_ptr<FakeVideoTrackRenderer> renderer( |
nisse | 81354f5 | 2016-01-19 00:23:24 -0800 | [diff] [blame] | 79 | new FakeVideoTrackRenderer(video_track_.get())); |
| 80 | |
perkj | f0dcfe2 | 2016-03-10 18:32:00 +0100 | [diff] [blame] | 81 | capturer_.CaptureFrame(); |
nisse | 81354f5 | 2016-01-19 00:23:24 -0800 | [diff] [blame] | 82 | EXPECT_EQ(1, renderer->num_rendered_frames()); |
| 83 | EXPECT_FALSE(renderer->black_frame()); |
nisse | 81354f5 | 2016-01-19 00:23:24 -0800 | [diff] [blame] | 84 | |
| 85 | video_track_->set_enabled(false); |
perkj | f0dcfe2 | 2016-03-10 18:32:00 +0100 | [diff] [blame] | 86 | capturer_.CaptureFrame(); |
nisse | 81354f5 | 2016-01-19 00:23:24 -0800 | [diff] [blame] | 87 | EXPECT_EQ(2, renderer->num_rendered_frames()); |
| 88 | EXPECT_TRUE(renderer->black_frame()); |
nisse | 81354f5 | 2016-01-19 00:23:24 -0800 | [diff] [blame] | 89 | |
| 90 | video_track_->set_enabled(true); |
perkj | f0dcfe2 | 2016-03-10 18:32:00 +0100 | [diff] [blame] | 91 | capturer_.CaptureFrame(); |
nisse | 81354f5 | 2016-01-19 00:23:24 -0800 | [diff] [blame] | 92 | EXPECT_EQ(3, renderer->num_rendered_frames()); |
| 93 | EXPECT_FALSE(renderer->black_frame()); |
nisse | 81354f5 | 2016-01-19 00:23:24 -0800 | [diff] [blame] | 94 | } |