blob: 0c76c862148718be7485e0e84378ff97185afc8c [file] [log] [blame]
johanf2183ff2017-02-28 01:33:09 -08001/*
2 * Copyright 2017 The WebRTC Project Authors. All rights reserved.
3 *
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.
9 */
10
Ruslan Burakov493a6502019-02-27 15:32:48 +010011#include <utility>
johanf2183ff2017-02-28 01:33:09 -080012#include <vector>
13
Mirko Bonadei92ea95e2017-09-15 06:47:31 +020014#include "test/gmock.h"
Yves Gerey665174f2018-06-19 15:03:05 +020015#include "test/gtest.h"
johanf2183ff2017-02-28 01:33:09 -080016
Mirko Bonadei92ea95e2017-09-15 06:47:31 +020017#include "api/video_codecs/video_decoder.h"
18#include "call/rtp_stream_receiver_controller.h"
Steve Anton10542f22019-01-11 09:11:00 -080019#include "media/base/fake_video_renderer.h"
Mirko Bonadei92ea95e2017-09-15 06:47:31 +020020#include "modules/pacing/packet_router.h"
21#include "modules/rtp_rtcp/source/rtp_packet_to_send.h"
22#include "modules/utility/include/process_thread.h"
Steve Anton10542f22019-01-11 09:11:00 -080023#include "rtc_base/critical_section.h"
Mirko Bonadei92ea95e2017-09-15 06:47:31 +020024#include "rtc_base/event.h"
25#include "system_wrappers/include/clock.h"
26#include "test/field_trial.h"
Niels Möllercbcbc222018-09-28 09:07:24 +020027#include "test/video_decoder_proxy_factory.h"
Mirko Bonadei92ea95e2017-09-15 06:47:31 +020028#include "video/call_stats.h"
29#include "video/video_receive_stream.h"
johanf2183ff2017-02-28 01:33:09 -080030
nisse0f15f922017-06-21 01:05:22 -070031namespace webrtc {
32namespace {
33
johanf2183ff2017-02-28 01:33:09 -080034using testing::_;
35using testing::Invoke;
36
37constexpr int kDefaultTimeOutMs = 50;
38
johanf2183ff2017-02-28 01:33:09 -080039class MockTransport : public Transport {
40 public:
41 MOCK_METHOD3(SendRtp,
42 bool(const uint8_t* packet,
43 size_t length,
44 const PacketOptions& options));
45 MOCK_METHOD2(SendRtcp, bool(const uint8_t* packet, size_t length));
46};
47
48class MockVideoDecoder : public VideoDecoder {
49 public:
50 MOCK_METHOD2(InitDecode,
51 int32_t(const VideoCodec* config, int32_t number_of_cores));
Niels Möller8df3a382018-05-07 16:10:01 +020052 MOCK_METHOD4(Decode,
johanf2183ff2017-02-28 01:33:09 -080053 int32_t(const EncodedImage& input,
54 bool missing_frames,
johanf2183ff2017-02-28 01:33:09 -080055 const CodecSpecificInfo* codec_specific_info,
56 int64_t render_time_ms));
57 MOCK_METHOD1(RegisterDecodeCompleteCallback,
58 int32_t(DecodedImageCallback* callback));
59 MOCK_METHOD0(Release, int32_t(void));
60 const char* ImplementationName() const { return "MockVideoDecoder"; }
61};
62
Ruslan Burakov493a6502019-02-27 15:32:48 +010063class FrameObjectFake : public video_coding::EncodedFrame {
64 public:
65 int64_t ReceivedTime() const override { return 0; }
66
67 int64_t RenderTime() const override { return _renderTimeMs; }
68};
69
johanf2183ff2017-02-28 01:33:09 -080070} // namespace
71
72class VideoReceiveStreamTest : public testing::Test {
73 public:
74 VideoReceiveStreamTest()
Tommi38c5d932018-03-27 23:11:09 +020075 : process_thread_(ProcessThread::Create("TestThread")),
johanf2183ff2017-02-28 01:33:09 -080076 config_(&mock_transport_),
Niels Möllercbcbc222018-09-28 09:07:24 +020077 call_stats_(Clock::GetRealTimeClock(), process_thread_.get()),
78 h264_decoder_factory_(&mock_h264_video_decoder_),
79 null_decoder_factory_(&mock_null_video_decoder_) {}
johanf2183ff2017-02-28 01:33:09 -080080
81 void SetUp() {
82 constexpr int kDefaultNumCpuCores = 2;
83 config_.rtp.remote_ssrc = 1111;
84 config_.rtp.local_ssrc = 2222;
85 config_.renderer = &fake_renderer_;
86 VideoReceiveStream::Decoder h264_decoder;
87 h264_decoder.payload_type = 99;
Niels Möllercb7e1d22018-09-11 15:56:04 +020088 h264_decoder.video_format = SdpVideoFormat("H264");
89 h264_decoder.video_format.parameters.insert(
johanf2183ff2017-02-28 01:33:09 -080090 {"sprop-parameter-sets", "Z0IACpZTBYmI,aMljiA=="});
Niels Möllercbcbc222018-09-28 09:07:24 +020091 h264_decoder.decoder_factory = &h264_decoder_factory_;
johanf2183ff2017-02-28 01:33:09 -080092 config_.decoders.push_back(h264_decoder);
93 VideoReceiveStream::Decoder null_decoder;
94 null_decoder.payload_type = 98;
Niels Möllercb7e1d22018-09-11 15:56:04 +020095 null_decoder.video_format = SdpVideoFormat("null");
Niels Möllercbcbc222018-09-28 09:07:24 +020096 null_decoder.decoder_factory = &null_decoder_factory_;
johanf2183ff2017-02-28 01:33:09 -080097 config_.decoders.push_back(null_decoder);
98
Ruslan Burakov493a6502019-02-27 15:32:48 +010099 Clock* clock = Clock::GetRealTimeClock();
100 timing_ = new VCMTiming(clock);
101
johanf2183ff2017-02-28 01:33:09 -0800102 video_receive_stream_.reset(new webrtc::internal::VideoReceiveStream(
Yves Gerey665174f2018-06-19 15:03:05 +0200103 &rtp_stream_receiver_controller_, kDefaultNumCpuCores, &packet_router_,
Ruslan Burakov493a6502019-02-27 15:32:48 +0100104 config_.Copy(), process_thread_.get(), &call_stats_, clock, timing_));
johanf2183ff2017-02-28 01:33:09 -0800105 }
106
107 protected:
Tommi38c5d932018-03-27 23:11:09 +0200108 std::unique_ptr<ProcessThread> process_thread_;
johanf2183ff2017-02-28 01:33:09 -0800109 VideoReceiveStream::Config config_;
110 CallStats call_stats_;
111 MockVideoDecoder mock_h264_video_decoder_;
112 MockVideoDecoder mock_null_video_decoder_;
Niels Möllercbcbc222018-09-28 09:07:24 +0200113 test::VideoDecoderProxyFactory h264_decoder_factory_;
114 test::VideoDecoderProxyFactory null_decoder_factory_;
johanf2183ff2017-02-28 01:33:09 -0800115 cricket::FakeVideoRenderer fake_renderer_;
116 MockTransport mock_transport_;
117 PacketRouter packet_router_;
nisse0f15f922017-06-21 01:05:22 -0700118 RtpStreamReceiverController rtp_stream_receiver_controller_;
johanf2183ff2017-02-28 01:33:09 -0800119 std::unique_ptr<webrtc::internal::VideoReceiveStream> video_receive_stream_;
Ruslan Burakov493a6502019-02-27 15:32:48 +0100120 VCMTiming* timing_;
johanf2183ff2017-02-28 01:33:09 -0800121};
122
123TEST_F(VideoReceiveStreamTest, CreateFrameFromH264FmtpSpropAndIdr) {
124 constexpr uint8_t idr_nalu[] = {0x05, 0xFF, 0xFF, 0xFF};
125 RtpPacketToSend rtppacket(nullptr);
126 uint8_t* payload = rtppacket.AllocatePayload(sizeof(idr_nalu));
127 memcpy(payload, idr_nalu, sizeof(idr_nalu));
128 rtppacket.SetMarker(true);
129 rtppacket.SetSsrc(1111);
130 rtppacket.SetPayloadType(99);
131 rtppacket.SetSequenceNumber(1);
132 rtppacket.SetTimestamp(0);
Niels Möllerc572ff32018-11-07 08:43:50 +0100133 rtc::Event init_decode_event_;
johanf2183ff2017-02-28 01:33:09 -0800134 EXPECT_CALL(mock_h264_video_decoder_, InitDecode(_, _))
135 .WillOnce(Invoke([&init_decode_event_](const VideoCodec* config,
136 int32_t number_of_cores) {
137 init_decode_event_.Set();
138 return 0;
139 }));
140 EXPECT_CALL(mock_h264_video_decoder_, RegisterDecodeCompleteCallback(_));
141 video_receive_stream_->Start();
Niels Möller8df3a382018-05-07 16:10:01 +0200142 EXPECT_CALL(mock_h264_video_decoder_, Decode(_, false, _, _));
nissed2ef3142017-05-11 08:00:58 -0700143 RtpPacketReceived parsed_packet;
144 ASSERT_TRUE(parsed_packet.Parse(rtppacket.data(), rtppacket.size()));
nisse0f15f922017-06-21 01:05:22 -0700145 rtp_stream_receiver_controller_.OnRtpPacket(parsed_packet);
johanf2183ff2017-02-28 01:33:09 -0800146 EXPECT_CALL(mock_h264_video_decoder_, Release());
147 // Make sure the decoder thread had a chance to run.
148 init_decode_event_.Wait(kDefaultTimeOutMs);
149}
nisse0f15f922017-06-21 01:05:22 -0700150
Ruslan Burakov493a6502019-02-27 15:32:48 +0100151TEST_F(VideoReceiveStreamTest, PlayoutDelay) {
152 const PlayoutDelay kPlayoutDelayMs = {123, 321};
153 std::unique_ptr<FrameObjectFake> test_frame(new FrameObjectFake());
154 test_frame->id.picture_id = 0;
155 test_frame->SetPlayoutDelay(kPlayoutDelayMs);
156
157 video_receive_stream_->OnCompleteFrame(std::move(test_frame));
158 EXPECT_EQ(kPlayoutDelayMs.min_ms, timing_->min_playout_delay());
159 EXPECT_EQ(kPlayoutDelayMs.max_ms, timing_->max_playout_delay());
160
161 // Check that the biggest minimum delay is chosen.
162 video_receive_stream_->SetMinimumPlayoutDelay(400);
163 EXPECT_EQ(400, timing_->min_playout_delay());
164
165 // Check base minimum delay validation.
166 EXPECT_FALSE(video_receive_stream_->SetBaseMinimumPlayoutDelayMs(12345));
167 EXPECT_FALSE(video_receive_stream_->SetBaseMinimumPlayoutDelayMs(-1));
168 EXPECT_TRUE(video_receive_stream_->SetBaseMinimumPlayoutDelayMs(500));
169 EXPECT_EQ(500, timing_->min_playout_delay());
170
171 // Check that intermidiate values are remembered and the biggest remembered
172 // is chosen.
173 video_receive_stream_->SetBaseMinimumPlayoutDelayMs(0);
174 EXPECT_EQ(400, timing_->min_playout_delay());
175
176 video_receive_stream_->SetMinimumPlayoutDelay(0);
177 EXPECT_EQ(123, timing_->min_playout_delay());
178}
179
180TEST_F(VideoReceiveStreamTest, PlayoutDelayPreservesDefaultMaxValue) {
181 const int default_max_playout_latency = timing_->max_playout_delay();
182 const PlayoutDelay kPlayoutDelayMs = {123, -1};
183
184 std::unique_ptr<FrameObjectFake> test_frame(new FrameObjectFake());
185 test_frame->id.picture_id = 0;
186 test_frame->SetPlayoutDelay(kPlayoutDelayMs);
187
188 video_receive_stream_->OnCompleteFrame(std::move(test_frame));
189
190 // Ensure that -1 preserves default maximum value from |timing_|.
191 EXPECT_EQ(kPlayoutDelayMs.min_ms, timing_->min_playout_delay());
192 EXPECT_NE(kPlayoutDelayMs.max_ms, timing_->max_playout_delay());
193 EXPECT_EQ(default_max_playout_latency, timing_->max_playout_delay());
194}
195
196TEST_F(VideoReceiveStreamTest, PlayoutDelayPreservesDefaultMinValue) {
197 const int default_min_playout_latency = timing_->min_playout_delay();
198 const PlayoutDelay kPlayoutDelayMs = {-1, 321};
199
200 std::unique_ptr<FrameObjectFake> test_frame(new FrameObjectFake());
201 test_frame->id.picture_id = 0;
202 test_frame->SetPlayoutDelay(kPlayoutDelayMs);
203
204 video_receive_stream_->OnCompleteFrame(std::move(test_frame));
205
206 // Ensure that -1 preserves default minimum value from |timing_|.
207 EXPECT_NE(kPlayoutDelayMs.min_ms, timing_->min_playout_delay());
208 EXPECT_EQ(kPlayoutDelayMs.max_ms, timing_->max_playout_delay());
209 EXPECT_EQ(default_min_playout_latency, timing_->min_playout_delay());
210}
211
johanf2183ff2017-02-28 01:33:09 -0800212} // namespace webrtc