blob: e833d6ddf01624d1d462de0d9726f78767d76f06 [file] [log] [blame]
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001/*
kjellanderb24317b2016-02-10 07:54:43 -08002 * Copyright 2012 The WebRTC project authors. All Rights Reserved.
henrike@webrtc.org28e20752013-07-10 00:45:36 +00003 *
kjellanderb24317b2016-02-10 07:54:43 -08004 * 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.org28e20752013-07-10 00:45:36 +00009 */
10
kwibergd1fe2812016-04-27 06:47:29 -070011#include <memory>
henrike@webrtc.org28e20752013-07-10 00:45:36 +000012#include <string>
13
Mirko Bonadei92ea95e2017-09-15 06:47:31 +020014#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.org28e20752013-07-10 00:45:36 +000020
21using webrtc::FakeVideoTrackRenderer;
perkjc8f952d2016-03-23 00:33:56 -070022using webrtc::MediaSourceInterface;
23using webrtc::MediaStreamTrackInterface;
perkjf0dcfe22016-03-10 18:32:00 +010024using webrtc::VideoTrackSource;
henrike@webrtc.org28e20752013-07-10 00:45:36 +000025using webrtc::VideoTrack;
26using webrtc::VideoTrackInterface;
27
Niels Möller5d67f822018-05-23 16:28:17 +020028class 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.org00c509a2015-03-12 21:37:26 +000039class VideoTrackTest : public testing::Test {
40 public:
41 VideoTrackTest() {
42 static const char kVideoTrackId[] = "track_id";
Niels Möller5d67f822018-05-23 16:28:17 +020043 video_track_source_ = new rtc::RefCountedObject<TestVideoTrackSource>();
perkj773be362017-07-31 23:22:01 -070044 video_track_ = VideoTrack::Create(kVideoTrackId, video_track_source_,
45 rtc::Thread::Current());
Niels Möller5d67f822018-05-23 16:28:17 +020046 video_track_source_->capturer()->Start(
perkjf0dcfe22016-03-10 18:32:00 +010047 cricket::VideoFormat(640, 480, cricket::VideoFormat::FpsToInterval(30),
48 cricket::FOURCC_I420));
guoweis@webrtc.org00c509a2015-03-12 21:37:26 +000049 }
50
51 protected:
Niels Möller5d67f822018-05-23 16:28:17 +020052 rtc::scoped_refptr<TestVideoTrackSource> video_track_source_;
guoweis@webrtc.org00c509a2015-03-12 21:37:26 +000053 rtc::scoped_refptr<VideoTrackInterface> video_track_;
54};
55
perkjc8f952d2016-03-23 00:33:56 -070056// Test changing the source state also changes the track state.
57TEST_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.org28e20752013-07-10 00:45:36 +000063// Test adding renderers to a video track and render to them by providing
wu@webrtc.org967bfff2013-09-19 05:49:50 +000064// frames to the source.
guoweis@webrtc.org00c509a2015-03-12 21:37:26 +000065TEST_F(VideoTrackTest, RenderVideo) {
66 // FakeVideoTrackRenderer register itself to |video_track_|
kwibergd1fe2812016-04-27 06:47:29 -070067 std::unique_ptr<FakeVideoTrackRenderer> renderer_1(
guoweis@webrtc.org00c509a2015-03-12 21:37:26 +000068 new FakeVideoTrackRenderer(video_track_.get()));
guoweis@webrtc.orgf9a75d92015-03-10 06:37:00 +000069
Niels Möller5d67f822018-05-23 16:28:17 +020070 video_track_source_->capturer()->CaptureFrame();
henrike@webrtc.org28e20752013-07-10 00:45:36 +000071 EXPECT_EQ(1, renderer_1->num_rendered_frames());
72
guoweis@webrtc.org00c509a2015-03-12 21:37:26 +000073 // FakeVideoTrackRenderer register itself to |video_track_|
kwibergd1fe2812016-04-27 06:47:29 -070074 std::unique_ptr<FakeVideoTrackRenderer> renderer_2(
guoweis@webrtc.org00c509a2015-03-12 21:37:26 +000075 new FakeVideoTrackRenderer(video_track_.get()));
Niels Möller5d67f822018-05-23 16:28:17 +020076 video_track_source_->capturer()->CaptureFrame();
henrike@webrtc.org28e20752013-07-10 00:45:36 +000077 EXPECT_EQ(2, renderer_1->num_rendered_frames());
78 EXPECT_EQ(1, renderer_2->num_rendered_frames());
79
perkjd6c39542016-03-17 10:35:23 +010080 renderer_1.reset(nullptr);
Niels Möller5d67f822018-05-23 16:28:17 +020081 video_track_source_->capturer()->CaptureFrame();
nissedb25d2e2016-02-26 01:24:58 -080082 EXPECT_EQ(2, renderer_2->num_rendered_frames());
83}
84
nisse81354f52016-01-19 00:23:24 -080085// Test that disabling the track results in blacked out frames.
86TEST_F(VideoTrackTest, DisableTrackBlackout) {
kwibergd1fe2812016-04-27 06:47:29 -070087 std::unique_ptr<FakeVideoTrackRenderer> renderer(
nisse81354f52016-01-19 00:23:24 -080088 new FakeVideoTrackRenderer(video_track_.get()));
89
Niels Möller5d67f822018-05-23 16:28:17 +020090 video_track_source_->capturer()->CaptureFrame();
nisse81354f52016-01-19 00:23:24 -080091 EXPECT_EQ(1, renderer->num_rendered_frames());
92 EXPECT_FALSE(renderer->black_frame());
nisse81354f52016-01-19 00:23:24 -080093
94 video_track_->set_enabled(false);
Niels Möller5d67f822018-05-23 16:28:17 +020095 video_track_source_->capturer()->CaptureFrame();
nisse81354f52016-01-19 00:23:24 -080096 EXPECT_EQ(2, renderer->num_rendered_frames());
97 EXPECT_TRUE(renderer->black_frame());
nisse81354f52016-01-19 00:23:24 -080098
99 video_track_->set_enabled(true);
Niels Möller5d67f822018-05-23 16:28:17 +0200100 video_track_source_->capturer()->CaptureFrame();
nisse81354f52016-01-19 00:23:24 -0800101 EXPECT_EQ(3, renderer->num_rendered_frames());
102 EXPECT_FALSE(renderer->black_frame());
nisse81354f52016-01-19 00:23:24 -0800103}