Tommi | ae4d097 | 2020-05-18 08:45:38 +0200 | [diff] [blame] | 1 | /* |
| 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 | |
| 11 | #include "video/video_receive_stream2.h" |
| 12 | |
| 13 | #include <algorithm> |
Evan Shrubsole | a406272 | 2022-05-02 17:46:07 +0200 | [diff] [blame] | 14 | #include <cstddef> |
Markus Handell | 588f9b3 | 2021-04-08 19:19:50 +0200 | [diff] [blame] | 15 | #include <limits> |
Tommi | ae4d097 | 2020-05-18 08:45:38 +0200 | [diff] [blame] | 16 | #include <memory> |
Evan Shrubsole | 1c18477 | 2022-04-26 09:47:49 +0200 | [diff] [blame] | 17 | #include <ostream> |
Evan Shrubsole | 9a99905 | 2021-12-12 15:27:00 +0100 | [diff] [blame] | 18 | #include <tuple> |
Tommi | ae4d097 | 2020-05-18 08:45:38 +0200 | [diff] [blame] | 19 | #include <utility> |
| 20 | #include <vector> |
| 21 | |
Evan Shrubsole | 6dbc172 | 2022-03-22 12:20:11 +0100 | [diff] [blame] | 22 | #include "absl/memory/memory.h" |
Evan Shrubsole | 44be579 | 2022-04-26 16:24:41 +0200 | [diff] [blame] | 23 | #include "absl/types/optional.h" |
Evan Shrubsole | a406272 | 2022-05-02 17:46:07 +0200 | [diff] [blame] | 24 | #include "api/metronome/test/fake_metronome.h" |
Tommi | ae4d097 | 2020-05-18 08:45:38 +0200 | [diff] [blame] | 25 | #include "api/task_queue/default_task_queue_factory.h" |
Danil Chapovalov | 9cd4d49 | 2021-08-03 14:59:00 +0200 | [diff] [blame] | 26 | #include "api/test/mock_video_decoder.h" |
| 27 | #include "api/test/mock_video_decoder_factory.h" |
Evan Shrubsole | 44be579 | 2022-04-26 16:24:41 +0200 | [diff] [blame] | 28 | #include "api/test/time_controller.h" |
Tommi | ae4d097 | 2020-05-18 08:45:38 +0200 | [diff] [blame] | 29 | #include "api/test/video/function_video_decoder_factory.h" |
Evan Shrubsole | a406272 | 2022-05-02 17:46:07 +0200 | [diff] [blame] | 30 | #include "api/units/frequency.h" |
Evan Shrubsole | 44be579 | 2022-04-26 16:24:41 +0200 | [diff] [blame] | 31 | #include "api/units/time_delta.h" |
Evan Shrubsole | a406272 | 2022-05-02 17:46:07 +0200 | [diff] [blame] | 32 | #include "api/video/encoded_image.h" |
Evan Shrubsole | 44be579 | 2022-04-26 16:24:41 +0200 | [diff] [blame] | 33 | #include "api/video/recordable_encoded_frame.h" |
| 34 | #include "api/video/test/video_frame_matchers.h" |
Markus Handell | 588f9b3 | 2021-04-08 19:19:50 +0200 | [diff] [blame] | 35 | #include "api/video/video_frame.h" |
Evan Shrubsole | 1c18477 | 2022-04-26 09:47:49 +0200 | [diff] [blame] | 36 | #include "api/video_codecs/sdp_video_format.h" |
Tommi | ae4d097 | 2020-05-18 08:45:38 +0200 | [diff] [blame] | 37 | #include "api/video_codecs/video_decoder.h" |
| 38 | #include "call/rtp_stream_receiver_controller.h" |
Evan Shrubsole | 1c18477 | 2022-04-26 09:47:49 +0200 | [diff] [blame] | 39 | #include "call/video_receive_stream.h" |
Tommi | ae4d097 | 2020-05-18 08:45:38 +0200 | [diff] [blame] | 40 | #include "common_video/test/utilities.h" |
| 41 | #include "media/base/fake_video_renderer.h" |
Tommi | 90738dd | 2021-05-31 17:36:47 +0200 | [diff] [blame] | 42 | #include "media/engine/fake_webrtc_call.h" |
Tommi | ae4d097 | 2020-05-18 08:45:38 +0200 | [diff] [blame] | 43 | #include "modules/pacing/packet_router.h" |
| 44 | #include "modules/rtp_rtcp/source/rtp_packet_to_send.h" |
Tommi | ae4d097 | 2020-05-18 08:45:38 +0200 | [diff] [blame] | 45 | #include "modules/video_coding/encoded_frame.h" |
Tommi | ae4d097 | 2020-05-18 08:45:38 +0200 | [diff] [blame] | 46 | #include "rtc_base/event.h" |
Evan Shrubsole | a406272 | 2022-05-02 17:46:07 +0200 | [diff] [blame] | 47 | #include "rtc_base/strings/string_builder.h" |
Tommi | ae4d097 | 2020-05-18 08:45:38 +0200 | [diff] [blame] | 48 | #include "system_wrappers/include/clock.h" |
| 49 | #include "test/fake_decoder.h" |
Evan Shrubsole | a0ee64c | 2022-04-26 10:09:04 +0200 | [diff] [blame] | 50 | #include "test/fake_encoded_frame.h" |
Tommi | ae4d097 | 2020-05-18 08:45:38 +0200 | [diff] [blame] | 51 | #include "test/gmock.h" |
| 52 | #include "test/gtest.h" |
Danil Chapovalov | 9cd4d49 | 2021-08-03 14:59:00 +0200 | [diff] [blame] | 53 | #include "test/mock_transport.h" |
Tommi | ae4d097 | 2020-05-18 08:45:38 +0200 | [diff] [blame] | 54 | #include "test/run_loop.h" |
Jonas Oreland | 8ca0613 | 2022-03-14 12:52:48 +0100 | [diff] [blame] | 55 | #include "test/scoped_key_value_config.h" |
Tommi | ae4d097 | 2020-05-18 08:45:38 +0200 | [diff] [blame] | 56 | #include "test/time_controller/simulated_time_controller.h" |
| 57 | #include "test/video_decoder_proxy_factory.h" |
| 58 | #include "video/call_stats2.h" |
| 59 | |
| 60 | namespace webrtc { |
Evan Shrubsole | 1c18477 | 2022-04-26 09:47:49 +0200 | [diff] [blame] | 61 | |
| 62 | // Printing SdpVideoFormat for gmock argument matchers. |
| 63 | void PrintTo(const SdpVideoFormat& value, std::ostream* os) { |
| 64 | *os << value.ToString(); |
| 65 | } |
| 66 | |
Evan Shrubsole | 44be579 | 2022-04-26 16:24:41 +0200 | [diff] [blame] | 67 | void PrintTo(const RecordableEncodedFrame::EncodedResolution& value, |
| 68 | std::ostream* os) { |
| 69 | *os << value.width << "x" << value.height; |
| 70 | } |
| 71 | |
| 72 | void PrintTo(const RecordableEncodedFrame& value, std::ostream* os) { |
| 73 | *os << "RecordableEncodedFrame(render_time=" << value.render_time() |
| 74 | << " resolution=" << ::testing::PrintToString(value.resolution()) << ")"; |
| 75 | } |
| 76 | |
Evan Shrubsole | a406272 | 2022-05-02 17:46:07 +0200 | [diff] [blame] | 77 | } // namespace webrtc |
| 78 | |
| 79 | namespace webrtc { |
| 80 | |
Tommi | ae4d097 | 2020-05-18 08:45:38 +0200 | [diff] [blame] | 81 | namespace { |
| 82 | |
Evan Shrubsole | 44be579 | 2022-04-26 16:24:41 +0200 | [diff] [blame] | 83 | using test::video_frame_matchers::NtpTimestamp; |
| 84 | using test::video_frame_matchers::PacketInfos; |
| 85 | using test::video_frame_matchers::Rotation; |
Tommi | ae4d097 | 2020-05-18 08:45:38 +0200 | [diff] [blame] | 86 | using ::testing::_; |
Markus Handell | 588f9b3 | 2021-04-08 19:19:50 +0200 | [diff] [blame] | 87 | using ::testing::AllOf; |
Tommi | ae4d097 | 2020-05-18 08:45:38 +0200 | [diff] [blame] | 88 | using ::testing::ElementsAreArray; |
Evan Shrubsole | 44be579 | 2022-04-26 16:24:41 +0200 | [diff] [blame] | 89 | using ::testing::Eq; |
Markus Handell | 588f9b3 | 2021-04-08 19:19:50 +0200 | [diff] [blame] | 90 | using ::testing::Field; |
| 91 | using ::testing::InSequence; |
Tommi | ae4d097 | 2020-05-18 08:45:38 +0200 | [diff] [blame] | 92 | using ::testing::Invoke; |
| 93 | using ::testing::IsEmpty; |
Evan Shrubsole | a406272 | 2022-05-02 17:46:07 +0200 | [diff] [blame] | 94 | using ::testing::Pointee; |
Markus Handell | 588f9b3 | 2021-04-08 19:19:50 +0200 | [diff] [blame] | 95 | using ::testing::Property; |
Evan Shrubsole | 44be579 | 2022-04-26 16:24:41 +0200 | [diff] [blame] | 96 | using ::testing::Return; |
Tommi | ae4d097 | 2020-05-18 08:45:38 +0200 | [diff] [blame] | 97 | using ::testing::SizeIs; |
Danil Chapovalov | d08930d | 2021-08-12 13:26:55 +0200 | [diff] [blame] | 98 | using ::testing::WithoutArgs; |
Tommi | ae4d097 | 2020-05-18 08:45:38 +0200 | [diff] [blame] | 99 | |
Evan Shrubsole | 44be579 | 2022-04-26 16:24:41 +0200 | [diff] [blame] | 100 | auto RenderedFrameWith(::testing::Matcher<VideoFrame> m) { |
| 101 | return Optional(m); |
| 102 | } |
Evan Shrubsole | a406272 | 2022-05-02 17:46:07 +0200 | [diff] [blame] | 103 | auto RenderedFrame() { |
| 104 | return RenderedFrameWith(_); |
| 105 | } |
| 106 | testing::Matcher<absl::optional<VideoFrame>> DidNotReceiveFrame() { |
| 107 | return Eq(absl::nullopt); |
| 108 | } |
Evan Shrubsole | 44be579 | 2022-04-26 16:24:41 +0200 | [diff] [blame] | 109 | |
| 110 | constexpr TimeDelta kDefaultTimeOut = TimeDelta::Millis(50); |
Evan Shrubsole | 14ee803 | 2022-04-26 16:01:44 +0200 | [diff] [blame] | 111 | constexpr int kDefaultNumCpuCores = 2; |
Tommi | ae4d097 | 2020-05-18 08:45:38 +0200 | [diff] [blame] | 112 | |
Evan Shrubsole | 44be579 | 2022-04-26 16:24:41 +0200 | [diff] [blame] | 113 | constexpr Timestamp kStartTime = Timestamp::Millis(1'337'000); |
Evan Shrubsole | a406272 | 2022-05-02 17:46:07 +0200 | [diff] [blame] | 114 | constexpr Frequency k30Fps = Frequency::Hertz(30); |
| 115 | constexpr TimeDelta k30FpsDelay = 1 / k30Fps; |
| 116 | constexpr Frequency kRtpTimestampHz = Frequency::KiloHertz(90); |
| 117 | constexpr uint32_t k30FpsRtpTimestampDelta = kRtpTimestampHz / k30Fps; |
| 118 | constexpr uint32_t kFirstRtpTimestamp = 90000; |
Evan Shrubsole | 44be579 | 2022-04-26 16:24:41 +0200 | [diff] [blame] | 119 | |
| 120 | class FakeVideoRenderer : public rtc::VideoSinkInterface<webrtc::VideoFrame> { |
| 121 | public: |
Evan Shrubsole | a406272 | 2022-05-02 17:46:07 +0200 | [diff] [blame] | 122 | explicit FakeVideoRenderer(TimeController* time_controller, |
| 123 | test::RunLoop* loop) |
| 124 | : time_controller_(time_controller), loop_(loop) {} |
Evan Shrubsole | 44be579 | 2022-04-26 16:24:41 +0200 | [diff] [blame] | 125 | ~FakeVideoRenderer() override = default; |
| 126 | |
| 127 | void OnFrame(const webrtc::VideoFrame& frame) override { |
| 128 | last_frame_ = frame; |
Evan Shrubsole | 44be579 | 2022-04-26 16:24:41 +0200 | [diff] [blame] | 129 | } |
| 130 | |
Evan Shrubsole | a406272 | 2022-05-02 17:46:07 +0200 | [diff] [blame] | 131 | absl::optional<webrtc::VideoFrame> WaitForFrame(TimeDelta timeout) { |
Evan Shrubsole | 44be579 | 2022-04-26 16:24:41 +0200 | [diff] [blame] | 132 | if (!last_frame_) { |
| 133 | loop_->Flush(); |
Evan Shrubsole | a406272 | 2022-05-02 17:46:07 +0200 | [diff] [blame] | 134 | time_controller_->AdvanceTime(TimeDelta::Zero()); |
| 135 | time_controller_->Wait( |
| 136 | [this] { |
| 137 | loop_->Flush(); |
| 138 | return last_frame_.has_value(); |
| 139 | }, |
| 140 | timeout); |
Evan Shrubsole | 44be579 | 2022-04-26 16:24:41 +0200 | [diff] [blame] | 141 | } |
| 142 | absl::optional<webrtc::VideoFrame> ret = std::move(last_frame_); |
| 143 | last_frame_.reset(); |
| 144 | return ret; |
| 145 | } |
| 146 | |
| 147 | private: |
| 148 | absl::optional<webrtc::VideoFrame> last_frame_; |
Evan Shrubsole | a406272 | 2022-05-02 17:46:07 +0200 | [diff] [blame] | 149 | TimeController* const time_controller_; |
Evan Shrubsole | 44be579 | 2022-04-26 16:24:41 +0200 | [diff] [blame] | 150 | test::RunLoop* const loop_; |
Evan Shrubsole | 44be579 | 2022-04-26 16:24:41 +0200 | [diff] [blame] | 151 | }; |
| 152 | |
| 153 | MATCHER_P2(Resolution, w, h, "") { |
| 154 | return arg.resolution().width == w && arg.resolution().height == h; |
| 155 | } |
| 156 | |
Evan Shrubsole | a406272 | 2022-05-02 17:46:07 +0200 | [diff] [blame] | 157 | MATCHER_P(Id, id, "") { |
| 158 | return arg.id() == id; |
| 159 | } |
| 160 | |
| 161 | // Rtp timestamp for in order frame at 30fps. |
| 162 | uint32_t RtpTimestampForFrame(int id) { |
| 163 | return kFirstRtpTimestamp + id * k30FpsRtpTimestampDelta; |
| 164 | } |
| 165 | |
| 166 | // Receive time for in order frame at 30fps. |
| 167 | Timestamp ReceiveTimeForFrame(int id) { |
| 168 | return kStartTime + id * k30FpsDelay; |
| 169 | } |
| 170 | |
Tommi | ae4d097 | 2020-05-18 08:45:38 +0200 | [diff] [blame] | 171 | } // namespace |
| 172 | |
Evan Shrubsole | a406272 | 2022-05-02 17:46:07 +0200 | [diff] [blame] | 173 | class VideoReceiveStream2Test : public ::testing::TestWithParam<bool> { |
Tommi | ae4d097 | 2020-05-18 08:45:38 +0200 | [diff] [blame] | 174 | public: |
Evan Shrubsole | a406272 | 2022-05-02 17:46:07 +0200 | [diff] [blame] | 175 | auto DefaultDecodeAction() { |
| 176 | return testing::Invoke(&fake_decoder_, &test::FakeDecoder::Decode); |
| 177 | } |
| 178 | |
| 179 | bool UseMetronome() const { return GetParam(); } |
| 180 | |
Tommi | ae4d097 | 2020-05-18 08:45:38 +0200 | [diff] [blame] | 181 | VideoReceiveStream2Test() |
Evan Shrubsole | 44be579 | 2022-04-26 16:24:41 +0200 | [diff] [blame] | 182 | : time_controller_(kStartTime), |
| 183 | clock_(time_controller_.GetClock()), |
Evan Shrubsole | 1c18477 | 2022-04-26 09:47:49 +0200 | [diff] [blame] | 184 | config_(&mock_transport_, &mock_h264_decoder_factory_), |
Evan Shrubsole | 44be579 | 2022-04-26 16:24:41 +0200 | [diff] [blame] | 185 | call_stats_(clock_, loop_.task_queue()), |
Evan Shrubsole | a406272 | 2022-05-02 17:46:07 +0200 | [diff] [blame] | 186 | fake_renderer_(&time_controller_, &loop_), |
| 187 | fake_metronome_(time_controller_.GetTaskQueueFactory(), |
| 188 | TimeDelta::Millis(16)), |
| 189 | decode_sync_(clock_, &fake_metronome_, loop_.task_queue()), |
| 190 | h264_decoder_factory_(&mock_decoder_) { |
| 191 | if (UseMetronome()) { |
| 192 | fake_call_.SetFieldTrial("WebRTC-FrameBuffer3/arm:SyncDecoding/"); |
| 193 | } else { |
| 194 | fake_call_.SetFieldTrial("WebRTC-FrameBuffer3/arm:FrameBuffer3/"); |
| 195 | } |
Evan Shrubsole | 1c18477 | 2022-04-26 09:47:49 +0200 | [diff] [blame] | 196 | |
| 197 | // By default, mock decoder factory is backed by VideoDecoderProxyFactory. |
Evan Shrubsole | 14ee803 | 2022-04-26 16:01:44 +0200 | [diff] [blame] | 198 | ON_CALL(mock_h264_decoder_factory_, CreateVideoDecoder) |
Evan Shrubsole | 1c18477 | 2022-04-26 09:47:49 +0200 | [diff] [blame] | 199 | .WillByDefault(testing::Invoke( |
| 200 | &h264_decoder_factory_, |
| 201 | &test::VideoDecoderProxyFactory::CreateVideoDecoder)); |
Evan Shrubsole | 14ee803 | 2022-04-26 16:01:44 +0200 | [diff] [blame] | 202 | |
| 203 | // By default, mock decode will wrap the fake decoder. |
Evan Shrubsole | a406272 | 2022-05-02 17:46:07 +0200 | [diff] [blame] | 204 | ON_CALL(mock_decoder_, Configure) |
Evan Shrubsole | 14ee803 | 2022-04-26 16:01:44 +0200 | [diff] [blame] | 205 | .WillByDefault( |
| 206 | testing::Invoke(&fake_decoder_, &test::FakeDecoder::Configure)); |
Evan Shrubsole | a406272 | 2022-05-02 17:46:07 +0200 | [diff] [blame] | 207 | ON_CALL(mock_decoder_, Decode).WillByDefault(DefaultDecodeAction()); |
| 208 | ON_CALL(mock_decoder_, RegisterDecodeCompleteCallback) |
Evan Shrubsole | 14ee803 | 2022-04-26 16:01:44 +0200 | [diff] [blame] | 209 | .WillByDefault(testing::Invoke( |
| 210 | &fake_decoder_, |
| 211 | &test::FakeDecoder::RegisterDecodeCompleteCallback)); |
Evan Shrubsole | a406272 | 2022-05-02 17:46:07 +0200 | [diff] [blame] | 212 | ON_CALL(mock_decoder_, Release) |
Evan Shrubsole | 14ee803 | 2022-04-26 16:01:44 +0200 | [diff] [blame] | 213 | .WillByDefault( |
| 214 | testing::Invoke(&fake_decoder_, &test::FakeDecoder::Release)); |
Jonas Oreland | 8ca0613 | 2022-03-14 12:52:48 +0100 | [diff] [blame] | 215 | } |
Tommi | 90738dd | 2021-05-31 17:36:47 +0200 | [diff] [blame] | 216 | ~VideoReceiveStream2Test() override { |
Evan Shrubsole | a406272 | 2022-05-02 17:46:07 +0200 | [diff] [blame] | 217 | if (video_receive_stream_) { |
| 218 | video_receive_stream_->Stop(); |
Tommi | 90738dd | 2021-05-31 17:36:47 +0200 | [diff] [blame] | 219 | video_receive_stream_->UnregisterFromTransport(); |
Evan Shrubsole | a406272 | 2022-05-02 17:46:07 +0200 | [diff] [blame] | 220 | } |
| 221 | fake_metronome_.Stop(); |
| 222 | time_controller_.AdvanceTime(TimeDelta::Zero()); |
Tommi | 90738dd | 2021-05-31 17:36:47 +0200 | [diff] [blame] | 223 | } |
Tommi | ae4d097 | 2020-05-18 08:45:38 +0200 | [diff] [blame] | 224 | |
Tommi | 90738dd | 2021-05-31 17:36:47 +0200 | [diff] [blame] | 225 | void SetUp() override { |
Tommi | ae4d097 | 2020-05-18 08:45:38 +0200 | [diff] [blame] | 226 | config_.rtp.remote_ssrc = 1111; |
| 227 | config_.rtp.local_ssrc = 2222; |
| 228 | config_.renderer = &fake_renderer_; |
| 229 | VideoReceiveStream::Decoder h264_decoder; |
| 230 | h264_decoder.payload_type = 99; |
| 231 | h264_decoder.video_format = SdpVideoFormat("H264"); |
| 232 | h264_decoder.video_format.parameters.insert( |
| 233 | {"sprop-parameter-sets", "Z0IACpZTBYmI,aMljiA=="}); |
Evan Shrubsole | 1c18477 | 2022-04-26 09:47:49 +0200 | [diff] [blame] | 234 | VideoReceiveStream::Decoder h265_decoder; |
| 235 | h265_decoder.payload_type = 100; |
| 236 | h265_decoder.video_format = SdpVideoFormat("H265"); |
| 237 | |
| 238 | config_.decoders = {h265_decoder, h264_decoder}; |
Tommi | ae4d097 | 2020-05-18 08:45:38 +0200 | [diff] [blame] | 239 | |
Evan Shrubsole | 44be579 | 2022-04-26 16:24:41 +0200 | [diff] [blame] | 240 | RecreateReceiveStream(); |
Evan Shrubsole | 14ee803 | 2022-04-26 16:01:44 +0200 | [diff] [blame] | 241 | } |
| 242 | |
Evan Shrubsole | 44be579 | 2022-04-26 16:24:41 +0200 | [diff] [blame] | 243 | void RecreateReceiveStream(absl::optional<VideoReceiveStream::RecordingState> |
| 244 | state = absl::nullopt) { |
Evan Shrubsole | 14ee803 | 2022-04-26 16:01:44 +0200 | [diff] [blame] | 245 | if (video_receive_stream_) { |
| 246 | video_receive_stream_->UnregisterFromTransport(); |
| 247 | video_receive_stream_ = nullptr; |
| 248 | } |
| 249 | timing_ = new VCMTiming(clock_, fake_call_.trials()); |
Tommi | ae4d097 | 2020-05-18 08:45:38 +0200 | [diff] [blame] | 250 | video_receive_stream_ = |
| 251 | std::make_unique<webrtc::internal::VideoReceiveStream2>( |
Evan Shrubsole | 44be579 | 2022-04-26 16:24:41 +0200 | [diff] [blame] | 252 | time_controller_.GetTaskQueueFactory(), &fake_call_, |
| 253 | kDefaultNumCpuCores, &packet_router_, config_.Copy(), &call_stats_, |
| 254 | clock_, absl::WrapUnique(timing_), &nack_periodic_processor_, |
Evan Shrubsole | a406272 | 2022-05-02 17:46:07 +0200 | [diff] [blame] | 255 | GetParam() ? &decode_sync_ : nullptr); |
Tommi | 90738dd | 2021-05-31 17:36:47 +0200 | [diff] [blame] | 256 | video_receive_stream_->RegisterWithTransport( |
| 257 | &rtp_stream_receiver_controller_); |
Evan Shrubsole | 44be579 | 2022-04-26 16:24:41 +0200 | [diff] [blame] | 258 | if (state) |
| 259 | video_receive_stream_->SetAndGetRecordingState(std::move(*state), false); |
Tommi | ae4d097 | 2020-05-18 08:45:38 +0200 | [diff] [blame] | 260 | } |
| 261 | |
| 262 | protected: |
Evan Shrubsole | 44be579 | 2022-04-26 16:24:41 +0200 | [diff] [blame] | 263 | GlobalSimulatedTimeController time_controller_; |
| 264 | Clock* const clock_; |
| 265 | // Tasks on the main thread can be controlled with the run loop. |
Tommi | ae4d097 | 2020-05-18 08:45:38 +0200 | [diff] [blame] | 266 | test::RunLoop loop_; |
Markus Handell | 0e62f7a | 2021-07-20 13:32:02 +0200 | [diff] [blame] | 267 | NackPeriodicProcessor nack_periodic_processor_; |
Evan Shrubsole | 1c18477 | 2022-04-26 09:47:49 +0200 | [diff] [blame] | 268 | testing::NiceMock<MockVideoDecoderFactory> mock_h264_decoder_factory_; |
Tommi | ae4d097 | 2020-05-18 08:45:38 +0200 | [diff] [blame] | 269 | VideoReceiveStream::Config config_; |
| 270 | internal::CallStats call_stats_; |
Evan Shrubsole | a406272 | 2022-05-02 17:46:07 +0200 | [diff] [blame] | 271 | testing::NiceMock<MockVideoDecoder> mock_decoder_; |
Evan Shrubsole | 44be579 | 2022-04-26 16:24:41 +0200 | [diff] [blame] | 272 | FakeVideoRenderer fake_renderer_; |
Tommi | 90738dd | 2021-05-31 17:36:47 +0200 | [diff] [blame] | 273 | cricket::FakeCall fake_call_; |
Tommi | ae4d097 | 2020-05-18 08:45:38 +0200 | [diff] [blame] | 274 | MockTransport mock_transport_; |
| 275 | PacketRouter packet_router_; |
| 276 | RtpStreamReceiverController rtp_stream_receiver_controller_; |
| 277 | std::unique_ptr<webrtc::internal::VideoReceiveStream2> video_receive_stream_; |
Tommi | ae4d097 | 2020-05-18 08:45:38 +0200 | [diff] [blame] | 278 | VCMTiming* timing_; |
Evan Shrubsole | a406272 | 2022-05-02 17:46:07 +0200 | [diff] [blame] | 279 | test::FakeMetronome fake_metronome_; |
| 280 | DecodeSynchronizer decode_sync_; |
Evan Shrubsole | 1c18477 | 2022-04-26 09:47:49 +0200 | [diff] [blame] | 281 | |
| 282 | private: |
| 283 | test::VideoDecoderProxyFactory h264_decoder_factory_; |
Evan Shrubsole | 14ee803 | 2022-04-26 16:01:44 +0200 | [diff] [blame] | 284 | test::FakeDecoder fake_decoder_; |
Tommi | ae4d097 | 2020-05-18 08:45:38 +0200 | [diff] [blame] | 285 | }; |
| 286 | |
Evan Shrubsole | a406272 | 2022-05-02 17:46:07 +0200 | [diff] [blame] | 287 | TEST_P(VideoReceiveStream2Test, CreateFrameFromH264FmtpSpropAndIdr) { |
Tommi | ae4d097 | 2020-05-18 08:45:38 +0200 | [diff] [blame] | 288 | constexpr uint8_t idr_nalu[] = {0x05, 0xFF, 0xFF, 0xFF}; |
| 289 | RtpPacketToSend rtppacket(nullptr); |
| 290 | uint8_t* payload = rtppacket.AllocatePayload(sizeof(idr_nalu)); |
| 291 | memcpy(payload, idr_nalu, sizeof(idr_nalu)); |
| 292 | rtppacket.SetMarker(true); |
| 293 | rtppacket.SetSsrc(1111); |
| 294 | rtppacket.SetPayloadType(99); |
| 295 | rtppacket.SetSequenceNumber(1); |
| 296 | rtppacket.SetTimestamp(0); |
Evan Shrubsole | a406272 | 2022-05-02 17:46:07 +0200 | [diff] [blame] | 297 | EXPECT_CALL(mock_decoder_, RegisterDecodeCompleteCallback(_)); |
Tommi | ae4d097 | 2020-05-18 08:45:38 +0200 | [diff] [blame] | 298 | video_receive_stream_->Start(); |
Evan Shrubsole | a406272 | 2022-05-02 17:46:07 +0200 | [diff] [blame] | 299 | EXPECT_CALL(mock_decoder_, Decode(_, false, _)); |
Tommi | ae4d097 | 2020-05-18 08:45:38 +0200 | [diff] [blame] | 300 | RtpPacketReceived parsed_packet; |
| 301 | ASSERT_TRUE(parsed_packet.Parse(rtppacket.data(), rtppacket.size())); |
| 302 | rtp_stream_receiver_controller_.OnRtpPacket(parsed_packet); |
Evan Shrubsole | a406272 | 2022-05-02 17:46:07 +0200 | [diff] [blame] | 303 | EXPECT_CALL(mock_decoder_, Release()); |
Evan Shrubsole | 44be579 | 2022-04-26 16:24:41 +0200 | [diff] [blame] | 304 | |
| 305 | time_controller_.AdvanceTime(TimeDelta::Zero()); |
Tommi | ae4d097 | 2020-05-18 08:45:38 +0200 | [diff] [blame] | 306 | } |
| 307 | |
Evan Shrubsole | a406272 | 2022-05-02 17:46:07 +0200 | [diff] [blame] | 308 | TEST_P(VideoReceiveStream2Test, PlayoutDelay) { |
Niels Möller | d381eed | 2020-09-02 15:34:40 +0200 | [diff] [blame] | 309 | const VideoPlayoutDelay kPlayoutDelayMs = {123, 321}; |
Evan Shrubsole | a0ee64c | 2022-04-26 10:09:04 +0200 | [diff] [blame] | 310 | std::unique_ptr<test::FakeEncodedFrame> test_frame = |
| 311 | test::FakeFrameBuilder().Id(0).AsLast().Build(); |
Tommi | ae4d097 | 2020-05-18 08:45:38 +0200 | [diff] [blame] | 312 | test_frame->SetPlayoutDelay(kPlayoutDelayMs); |
| 313 | |
| 314 | video_receive_stream_->OnCompleteFrame(std::move(test_frame)); |
Evan Shrubsole | 8f1159b | 2022-03-22 12:12:17 +0100 | [diff] [blame] | 315 | auto timings = timing_->GetTimings(); |
| 316 | EXPECT_EQ(kPlayoutDelayMs.min_ms, timings.min_playout_delay.ms()); |
| 317 | EXPECT_EQ(kPlayoutDelayMs.max_ms, timings.max_playout_delay.ms()); |
Tommi | ae4d097 | 2020-05-18 08:45:38 +0200 | [diff] [blame] | 318 | |
| 319 | // Check that the biggest minimum delay is chosen. |
| 320 | video_receive_stream_->SetMinimumPlayoutDelay(400); |
Evan Shrubsole | 8f1159b | 2022-03-22 12:12:17 +0100 | [diff] [blame] | 321 | timings = timing_->GetTimings(); |
| 322 | EXPECT_EQ(400, timings.min_playout_delay.ms()); |
Tommi | ae4d097 | 2020-05-18 08:45:38 +0200 | [diff] [blame] | 323 | |
| 324 | // Check base minimum delay validation. |
| 325 | EXPECT_FALSE(video_receive_stream_->SetBaseMinimumPlayoutDelayMs(12345)); |
| 326 | EXPECT_FALSE(video_receive_stream_->SetBaseMinimumPlayoutDelayMs(-1)); |
| 327 | EXPECT_TRUE(video_receive_stream_->SetBaseMinimumPlayoutDelayMs(500)); |
Evan Shrubsole | 8f1159b | 2022-03-22 12:12:17 +0100 | [diff] [blame] | 328 | timings = timing_->GetTimings(); |
| 329 | EXPECT_EQ(500, timings.min_playout_delay.ms()); |
Tommi | ae4d097 | 2020-05-18 08:45:38 +0200 | [diff] [blame] | 330 | |
| 331 | // Check that intermidiate values are remembered and the biggest remembered |
| 332 | // is chosen. |
| 333 | video_receive_stream_->SetBaseMinimumPlayoutDelayMs(0); |
Evan Shrubsole | 8f1159b | 2022-03-22 12:12:17 +0100 | [diff] [blame] | 334 | timings = timing_->GetTimings(); |
| 335 | EXPECT_EQ(400, timings.min_playout_delay.ms()); |
Tommi | ae4d097 | 2020-05-18 08:45:38 +0200 | [diff] [blame] | 336 | |
| 337 | video_receive_stream_->SetMinimumPlayoutDelay(0); |
Evan Shrubsole | 8f1159b | 2022-03-22 12:12:17 +0100 | [diff] [blame] | 338 | timings = timing_->GetTimings(); |
| 339 | EXPECT_EQ(123, timings.min_playout_delay.ms()); |
Tommi | ae4d097 | 2020-05-18 08:45:38 +0200 | [diff] [blame] | 340 | } |
| 341 | |
Evan Shrubsole | a406272 | 2022-05-02 17:46:07 +0200 | [diff] [blame] | 342 | TEST_P(VideoReceiveStream2Test, PlayoutDelayPreservesDefaultMaxValue) { |
Evan Shrubsole | 8f1159b | 2022-03-22 12:12:17 +0100 | [diff] [blame] | 343 | const TimeDelta default_max_playout_latency = |
| 344 | timing_->GetTimings().max_playout_delay; |
Niels Möller | d381eed | 2020-09-02 15:34:40 +0200 | [diff] [blame] | 345 | const VideoPlayoutDelay kPlayoutDelayMs = {123, -1}; |
Tommi | ae4d097 | 2020-05-18 08:45:38 +0200 | [diff] [blame] | 346 | |
Evan Shrubsole | a0ee64c | 2022-04-26 10:09:04 +0200 | [diff] [blame] | 347 | std::unique_ptr<test::FakeEncodedFrame> test_frame = |
| 348 | test::FakeFrameBuilder().Id(0).AsLast().Build(); |
Tommi | ae4d097 | 2020-05-18 08:45:38 +0200 | [diff] [blame] | 349 | test_frame->SetPlayoutDelay(kPlayoutDelayMs); |
| 350 | |
| 351 | video_receive_stream_->OnCompleteFrame(std::move(test_frame)); |
| 352 | |
Artem Titov | ab30d72 | 2021-07-27 16:22:11 +0200 | [diff] [blame] | 353 | // Ensure that -1 preserves default maximum value from `timing_`. |
Evan Shrubsole | 8f1159b | 2022-03-22 12:12:17 +0100 | [diff] [blame] | 354 | auto timings = timing_->GetTimings(); |
| 355 | EXPECT_EQ(kPlayoutDelayMs.min_ms, timings.min_playout_delay.ms()); |
| 356 | EXPECT_NE(kPlayoutDelayMs.max_ms, timings.max_playout_delay.ms()); |
| 357 | EXPECT_EQ(default_max_playout_latency, timings.max_playout_delay); |
Tommi | ae4d097 | 2020-05-18 08:45:38 +0200 | [diff] [blame] | 358 | } |
| 359 | |
Evan Shrubsole | a406272 | 2022-05-02 17:46:07 +0200 | [diff] [blame] | 360 | TEST_P(VideoReceiveStream2Test, PlayoutDelayPreservesDefaultMinValue) { |
Evan Shrubsole | 8f1159b | 2022-03-22 12:12:17 +0100 | [diff] [blame] | 361 | const TimeDelta default_min_playout_latency = |
| 362 | timing_->GetTimings().min_playout_delay; |
Niels Möller | d381eed | 2020-09-02 15:34:40 +0200 | [diff] [blame] | 363 | const VideoPlayoutDelay kPlayoutDelayMs = {-1, 321}; |
Tommi | ae4d097 | 2020-05-18 08:45:38 +0200 | [diff] [blame] | 364 | |
Evan Shrubsole | a0ee64c | 2022-04-26 10:09:04 +0200 | [diff] [blame] | 365 | std::unique_ptr<test::FakeEncodedFrame> test_frame = |
| 366 | test::FakeFrameBuilder().Id(0).AsLast().Build(); |
Tommi | ae4d097 | 2020-05-18 08:45:38 +0200 | [diff] [blame] | 367 | test_frame->SetPlayoutDelay(kPlayoutDelayMs); |
| 368 | |
| 369 | video_receive_stream_->OnCompleteFrame(std::move(test_frame)); |
| 370 | |
Artem Titov | ab30d72 | 2021-07-27 16:22:11 +0200 | [diff] [blame] | 371 | // Ensure that -1 preserves default minimum value from `timing_`. |
Evan Shrubsole | 8f1159b | 2022-03-22 12:12:17 +0100 | [diff] [blame] | 372 | auto timings = timing_->GetTimings(); |
| 373 | EXPECT_NE(kPlayoutDelayMs.min_ms, timings.min_playout_delay.ms()); |
| 374 | EXPECT_EQ(kPlayoutDelayMs.max_ms, timings.max_playout_delay.ms()); |
| 375 | EXPECT_EQ(default_min_playout_latency, timings.min_playout_delay); |
Tommi | ae4d097 | 2020-05-18 08:45:38 +0200 | [diff] [blame] | 376 | } |
| 377 | |
Evan Shrubsole | a406272 | 2022-05-02 17:46:07 +0200 | [diff] [blame] | 378 | TEST_P(VideoReceiveStream2Test, MaxCompositionDelayNotSetByDefault) { |
Johannes Kron | 111e981 | 2020-10-26 13:54:40 +0100 | [diff] [blame] | 379 | // Default with no playout delay set. |
Evan Shrubsole | a0ee64c | 2022-04-26 10:09:04 +0200 | [diff] [blame] | 380 | std::unique_ptr<test::FakeEncodedFrame> test_frame0 = |
| 381 | test::FakeFrameBuilder().Id(0).AsLast().Build(); |
Johannes Kron | 111e981 | 2020-10-26 13:54:40 +0100 | [diff] [blame] | 382 | video_receive_stream_->OnCompleteFrame(std::move(test_frame0)); |
| 383 | EXPECT_FALSE(timing_->MaxCompositionDelayInFrames()); |
| 384 | |
| 385 | // Max composition delay not set for playout delay 0,0. |
Evan Shrubsole | a0ee64c | 2022-04-26 10:09:04 +0200 | [diff] [blame] | 386 | std::unique_ptr<test::FakeEncodedFrame> test_frame1 = |
| 387 | test::FakeFrameBuilder().Id(1).AsLast().Build(); |
Johannes Kron | 111e981 | 2020-10-26 13:54:40 +0100 | [diff] [blame] | 388 | test_frame1->SetPlayoutDelay({0, 0}); |
| 389 | video_receive_stream_->OnCompleteFrame(std::move(test_frame1)); |
| 390 | EXPECT_FALSE(timing_->MaxCompositionDelayInFrames()); |
| 391 | |
| 392 | // Max composition delay not set for playout delay X,Y, where X,Y>0. |
Evan Shrubsole | a0ee64c | 2022-04-26 10:09:04 +0200 | [diff] [blame] | 393 | std::unique_ptr<test::FakeEncodedFrame> test_frame2 = |
| 394 | test::FakeFrameBuilder().Id(2).AsLast().Build(); |
Johannes Kron | 111e981 | 2020-10-26 13:54:40 +0100 | [diff] [blame] | 395 | test_frame2->SetPlayoutDelay({10, 30}); |
| 396 | video_receive_stream_->OnCompleteFrame(std::move(test_frame2)); |
| 397 | EXPECT_FALSE(timing_->MaxCompositionDelayInFrames()); |
| 398 | } |
| 399 | |
Evan Shrubsole | a406272 | 2022-05-02 17:46:07 +0200 | [diff] [blame] | 400 | TEST_P(VideoReceiveStream2Test, MaxCompositionDelaySetFromMaxPlayoutDelay) { |
Johannes Kron | 111e981 | 2020-10-26 13:54:40 +0100 | [diff] [blame] | 401 | // Max composition delay set if playout delay X,Y, where X=0,Y>0. |
| 402 | const VideoPlayoutDelay kPlayoutDelayMs = {0, 50}; |
| 403 | const int kExpectedMaxCompositionDelayInFrames = 3; // ~50 ms at 60 fps. |
Evan Shrubsole | a0ee64c | 2022-04-26 10:09:04 +0200 | [diff] [blame] | 404 | std::unique_ptr<test::FakeEncodedFrame> test_frame = |
| 405 | test::FakeFrameBuilder().Id(0).AsLast().Build(); |
Johannes Kron | 111e981 | 2020-10-26 13:54:40 +0100 | [diff] [blame] | 406 | test_frame->SetPlayoutDelay(kPlayoutDelayMs); |
| 407 | video_receive_stream_->OnCompleteFrame(std::move(test_frame)); |
| 408 | EXPECT_EQ(kExpectedMaxCompositionDelayInFrames, |
| 409 | timing_->MaxCompositionDelayInFrames()); |
| 410 | } |
| 411 | |
Evan Shrubsole | a406272 | 2022-05-02 17:46:07 +0200 | [diff] [blame] | 412 | TEST_P(VideoReceiveStream2Test, LazyDecoderCreation) { |
Evan Shrubsole | 1c18477 | 2022-04-26 09:47:49 +0200 | [diff] [blame] | 413 | constexpr uint8_t idr_nalu[] = {0x05, 0xFF, 0xFF, 0xFF}; |
| 414 | RtpPacketToSend rtppacket(nullptr); |
| 415 | uint8_t* payload = rtppacket.AllocatePayload(sizeof(idr_nalu)); |
| 416 | memcpy(payload, idr_nalu, sizeof(idr_nalu)); |
| 417 | rtppacket.SetMarker(true); |
| 418 | rtppacket.SetSsrc(1111); |
| 419 | // H265 payload type. |
| 420 | rtppacket.SetPayloadType(99); |
| 421 | rtppacket.SetSequenceNumber(1); |
| 422 | rtppacket.SetTimestamp(0); |
| 423 | |
| 424 | // Only 1 decoder is created by default. It will be H265 since that was the |
| 425 | // first in the decoder list. |
| 426 | EXPECT_CALL(mock_h264_decoder_factory_, CreateVideoDecoder(_)).Times(0); |
| 427 | EXPECT_CALL(mock_h264_decoder_factory_, |
| 428 | CreateVideoDecoder( |
| 429 | testing::Field(&SdpVideoFormat::name, testing::Eq("H265")))) |
| 430 | .Times(1); |
| 431 | video_receive_stream_->Start(); |
| 432 | |
| 433 | EXPECT_TRUE( |
| 434 | testing::Mock::VerifyAndClearExpectations(&mock_h264_decoder_factory_)); |
| 435 | |
| 436 | EXPECT_CALL(mock_h264_decoder_factory_, |
| 437 | CreateVideoDecoder( |
| 438 | testing::Field(&SdpVideoFormat::name, testing::Eq("H264")))) |
| 439 | .Times(1); |
| 440 | rtc::Event init_decode_event; |
Evan Shrubsole | a406272 | 2022-05-02 17:46:07 +0200 | [diff] [blame] | 441 | EXPECT_CALL(mock_decoder_, Configure).WillOnce(WithoutArgs([&] { |
Evan Shrubsole | 1c18477 | 2022-04-26 09:47:49 +0200 | [diff] [blame] | 442 | init_decode_event.Set(); |
| 443 | return true; |
| 444 | })); |
Evan Shrubsole | a406272 | 2022-05-02 17:46:07 +0200 | [diff] [blame] | 445 | EXPECT_CALL(mock_decoder_, RegisterDecodeCompleteCallback(_)); |
| 446 | EXPECT_CALL(mock_decoder_, Decode(_, false, _)); |
Evan Shrubsole | 1c18477 | 2022-04-26 09:47:49 +0200 | [diff] [blame] | 447 | RtpPacketReceived parsed_packet; |
| 448 | ASSERT_TRUE(parsed_packet.Parse(rtppacket.data(), rtppacket.size())); |
| 449 | rtp_stream_receiver_controller_.OnRtpPacket(parsed_packet); |
Evan Shrubsole | a406272 | 2022-05-02 17:46:07 +0200 | [diff] [blame] | 450 | EXPECT_CALL(mock_decoder_, Release()); |
Evan Shrubsole | 1c18477 | 2022-04-26 09:47:49 +0200 | [diff] [blame] | 451 | |
| 452 | // Make sure the decoder thread had a chance to run. |
Evan Shrubsole | 44be579 | 2022-04-26 16:24:41 +0200 | [diff] [blame] | 453 | init_decode_event.Wait(kDefaultTimeOut.ms()); |
Evan Shrubsole | 1c18477 | 2022-04-26 09:47:49 +0200 | [diff] [blame] | 454 | } |
| 455 | |
Evan Shrubsole | a406272 | 2022-05-02 17:46:07 +0200 | [diff] [blame] | 456 | TEST_P(VideoReceiveStream2Test, PassesNtpTime) { |
Evan Shrubsole | a0ee64c | 2022-04-26 10:09:04 +0200 | [diff] [blame] | 457 | const Timestamp kNtpTimestamp = Timestamp::Millis(12345); |
| 458 | std::unique_ptr<test::FakeEncodedFrame> test_frame = |
| 459 | test::FakeFrameBuilder() |
| 460 | .Id(0) |
| 461 | .PayloadType(99) |
| 462 | .NtpTime(kNtpTimestamp) |
| 463 | .AsLast() |
| 464 | .Build(); |
Tommi | ae4d097 | 2020-05-18 08:45:38 +0200 | [diff] [blame] | 465 | |
| 466 | video_receive_stream_->Start(); |
| 467 | video_receive_stream_->OnCompleteFrame(std::move(test_frame)); |
Evan Shrubsole | a406272 | 2022-05-02 17:46:07 +0200 | [diff] [blame] | 468 | EXPECT_THAT(fake_renderer_.WaitForFrame(kDefaultTimeOut), |
Evan Shrubsole | 44be579 | 2022-04-26 16:24:41 +0200 | [diff] [blame] | 469 | RenderedFrameWith(NtpTimestamp(kNtpTimestamp))); |
Tommi | ae4d097 | 2020-05-18 08:45:38 +0200 | [diff] [blame] | 470 | } |
| 471 | |
Evan Shrubsole | a406272 | 2022-05-02 17:46:07 +0200 | [diff] [blame] | 472 | TEST_P(VideoReceiveStream2Test, PassesRotation) { |
Tommi | ae4d097 | 2020-05-18 08:45:38 +0200 | [diff] [blame] | 473 | const webrtc::VideoRotation kRotation = webrtc::kVideoRotation_180; |
Evan Shrubsole | a0ee64c | 2022-04-26 10:09:04 +0200 | [diff] [blame] | 474 | std::unique_ptr<test::FakeEncodedFrame> test_frame = test::FakeFrameBuilder() |
| 475 | .Id(0) |
| 476 | .PayloadType(99) |
| 477 | .Rotation(kRotation) |
| 478 | .AsLast() |
| 479 | .Build(); |
Tommi | ae4d097 | 2020-05-18 08:45:38 +0200 | [diff] [blame] | 480 | |
| 481 | video_receive_stream_->Start(); |
| 482 | video_receive_stream_->OnCompleteFrame(std::move(test_frame)); |
Evan Shrubsole | a406272 | 2022-05-02 17:46:07 +0200 | [diff] [blame] | 483 | EXPECT_THAT(fake_renderer_.WaitForFrame(kDefaultTimeOut), |
Evan Shrubsole | 44be579 | 2022-04-26 16:24:41 +0200 | [diff] [blame] | 484 | RenderedFrameWith(Rotation(kRotation))); |
Tommi | ae4d097 | 2020-05-18 08:45:38 +0200 | [diff] [blame] | 485 | } |
| 486 | |
Evan Shrubsole | a406272 | 2022-05-02 17:46:07 +0200 | [diff] [blame] | 487 | TEST_P(VideoReceiveStream2Test, PassesPacketInfos) { |
Tommi | ae4d097 | 2020-05-18 08:45:38 +0200 | [diff] [blame] | 488 | RtpPacketInfos packet_infos = CreatePacketInfos(3); |
Evan Shrubsole | a0ee64c | 2022-04-26 10:09:04 +0200 | [diff] [blame] | 489 | auto test_frame = test::FakeFrameBuilder() |
| 490 | .Id(0) |
| 491 | .PayloadType(99) |
| 492 | .PacketInfos(packet_infos) |
| 493 | .AsLast() |
| 494 | .Build(); |
Tommi | ae4d097 | 2020-05-18 08:45:38 +0200 | [diff] [blame] | 495 | |
| 496 | video_receive_stream_->Start(); |
| 497 | video_receive_stream_->OnCompleteFrame(std::move(test_frame)); |
Evan Shrubsole | a406272 | 2022-05-02 17:46:07 +0200 | [diff] [blame] | 498 | EXPECT_THAT(fake_renderer_.WaitForFrame(kDefaultTimeOut), |
Evan Shrubsole | 44be579 | 2022-04-26 16:24:41 +0200 | [diff] [blame] | 499 | RenderedFrameWith(PacketInfos(ElementsAreArray(packet_infos)))); |
Tommi | ae4d097 | 2020-05-18 08:45:38 +0200 | [diff] [blame] | 500 | } |
| 501 | |
Evan Shrubsole | a406272 | 2022-05-02 17:46:07 +0200 | [diff] [blame] | 502 | TEST_P(VideoReceiveStream2Test, RenderedFrameUpdatesGetSources) { |
Tommi | ae4d097 | 2020-05-18 08:45:38 +0200 | [diff] [blame] | 503 | constexpr uint32_t kSsrc = 1111; |
| 504 | constexpr uint32_t kCsrc = 9001; |
| 505 | constexpr uint32_t kRtpTimestamp = 12345; |
| 506 | |
| 507 | // Prepare one video frame with per-packet information. |
Evan Shrubsole | a0ee64c | 2022-04-26 10:09:04 +0200 | [diff] [blame] | 508 | auto test_frame = |
| 509 | test::FakeFrameBuilder().Id(0).PayloadType(99).AsLast().Build(); |
Tommi | ae4d097 | 2020-05-18 08:45:38 +0200 | [diff] [blame] | 510 | RtpPacketInfos packet_infos; |
| 511 | { |
| 512 | RtpPacketInfos::vector_type infos; |
| 513 | |
| 514 | RtpPacketInfo info; |
| 515 | info.set_ssrc(kSsrc); |
| 516 | info.set_csrcs({kCsrc}); |
| 517 | info.set_rtp_timestamp(kRtpTimestamp); |
| 518 | |
Johannes Kron | f7de74c | 2021-04-30 13:10:56 +0200 | [diff] [blame] | 519 | info.set_receive_time(clock_->CurrentTime() - TimeDelta::Millis(5000)); |
Tommi | ae4d097 | 2020-05-18 08:45:38 +0200 | [diff] [blame] | 520 | infos.push_back(info); |
| 521 | |
Johannes Kron | f7de74c | 2021-04-30 13:10:56 +0200 | [diff] [blame] | 522 | info.set_receive_time(clock_->CurrentTime() - TimeDelta::Millis(3000)); |
Tommi | ae4d097 | 2020-05-18 08:45:38 +0200 | [diff] [blame] | 523 | infos.push_back(info); |
| 524 | |
Johannes Kron | f7de74c | 2021-04-30 13:10:56 +0200 | [diff] [blame] | 525 | info.set_receive_time(clock_->CurrentTime() - TimeDelta::Millis(2000)); |
Tommi | ae4d097 | 2020-05-18 08:45:38 +0200 | [diff] [blame] | 526 | infos.push_back(info); |
| 527 | |
Johannes Kron | f7de74c | 2021-04-30 13:10:56 +0200 | [diff] [blame] | 528 | info.set_receive_time(clock_->CurrentTime() - TimeDelta::Millis(1000)); |
Tommi | ae4d097 | 2020-05-18 08:45:38 +0200 | [diff] [blame] | 529 | infos.push_back(info); |
| 530 | |
| 531 | packet_infos = RtpPacketInfos(std::move(infos)); |
| 532 | } |
| 533 | test_frame->SetPacketInfos(packet_infos); |
| 534 | |
| 535 | // Start receive stream. |
| 536 | video_receive_stream_->Start(); |
| 537 | EXPECT_THAT(video_receive_stream_->GetSources(), IsEmpty()); |
| 538 | |
| 539 | // Render one video frame. |
| 540 | int64_t timestamp_ms_min = clock_->TimeInMilliseconds(); |
| 541 | video_receive_stream_->OnCompleteFrame(std::move(test_frame)); |
Tommi | ae4d097 | 2020-05-18 08:45:38 +0200 | [diff] [blame] | 542 | // Verify that the per-packet information is passed to the renderer. |
Evan Shrubsole | a406272 | 2022-05-02 17:46:07 +0200 | [diff] [blame] | 543 | EXPECT_THAT(fake_renderer_.WaitForFrame(kDefaultTimeOut), |
Evan Shrubsole | 44be579 | 2022-04-26 16:24:41 +0200 | [diff] [blame] | 544 | RenderedFrameWith(PacketInfos(ElementsAreArray(packet_infos)))); |
| 545 | int64_t timestamp_ms_max = clock_->TimeInMilliseconds(); |
Tommi | ae4d097 | 2020-05-18 08:45:38 +0200 | [diff] [blame] | 546 | |
Artem Titov | ab30d72 | 2021-07-27 16:22:11 +0200 | [diff] [blame] | 547 | // Verify that the per-packet information also updates `GetSources()`. |
Tommi | ae4d097 | 2020-05-18 08:45:38 +0200 | [diff] [blame] | 548 | std::vector<RtpSource> sources = video_receive_stream_->GetSources(); |
| 549 | ASSERT_THAT(sources, SizeIs(2)); |
| 550 | { |
| 551 | auto it = std::find_if(sources.begin(), sources.end(), |
| 552 | [](const RtpSource& source) { |
| 553 | return source.source_type() == RtpSourceType::SSRC; |
| 554 | }); |
| 555 | ASSERT_NE(it, sources.end()); |
| 556 | |
| 557 | EXPECT_EQ(it->source_id(), kSsrc); |
| 558 | EXPECT_EQ(it->source_type(), RtpSourceType::SSRC); |
| 559 | EXPECT_EQ(it->rtp_timestamp(), kRtpTimestamp); |
| 560 | EXPECT_GE(it->timestamp_ms(), timestamp_ms_min); |
| 561 | EXPECT_LE(it->timestamp_ms(), timestamp_ms_max); |
| 562 | } |
| 563 | { |
| 564 | auto it = std::find_if(sources.begin(), sources.end(), |
| 565 | [](const RtpSource& source) { |
| 566 | return source.source_type() == RtpSourceType::CSRC; |
| 567 | }); |
| 568 | ASSERT_NE(it, sources.end()); |
| 569 | |
| 570 | EXPECT_EQ(it->source_id(), kCsrc); |
| 571 | EXPECT_EQ(it->source_type(), RtpSourceType::CSRC); |
| 572 | EXPECT_EQ(it->rtp_timestamp(), kRtpTimestamp); |
| 573 | EXPECT_GE(it->timestamp_ms(), timestamp_ms_min); |
| 574 | EXPECT_LE(it->timestamp_ms(), timestamp_ms_max); |
| 575 | } |
| 576 | } |
| 577 | |
Evan Shrubsole | a0ee64c | 2022-04-26 10:09:04 +0200 | [diff] [blame] | 578 | std::unique_ptr<test::FakeEncodedFrame> MakeFrameWithResolution( |
Markus Handell | 588f9b3 | 2021-04-08 19:19:50 +0200 | [diff] [blame] | 579 | VideoFrameType frame_type, |
| 580 | int picture_id, |
| 581 | int width, |
| 582 | int height) { |
Evan Shrubsole | a0ee64c | 2022-04-26 10:09:04 +0200 | [diff] [blame] | 583 | auto frame = |
| 584 | test::FakeFrameBuilder().Id(picture_id).PayloadType(99).AsLast().Build(); |
Tommi | ae4d097 | 2020-05-18 08:45:38 +0200 | [diff] [blame] | 585 | frame->SetFrameType(frame_type); |
Markus Handell | 588f9b3 | 2021-04-08 19:19:50 +0200 | [diff] [blame] | 586 | frame->_encodedWidth = width; |
| 587 | frame->_encodedHeight = height; |
Tommi | ae4d097 | 2020-05-18 08:45:38 +0200 | [diff] [blame] | 588 | return frame; |
| 589 | } |
| 590 | |
Evan Shrubsole | a0ee64c | 2022-04-26 10:09:04 +0200 | [diff] [blame] | 591 | std::unique_ptr<test::FakeEncodedFrame> MakeFrame(VideoFrameType frame_type, |
| 592 | int picture_id) { |
Markus Handell | 588f9b3 | 2021-04-08 19:19:50 +0200 | [diff] [blame] | 593 | return MakeFrameWithResolution(frame_type, picture_id, 320, 240); |
| 594 | } |
| 595 | |
Evan Shrubsole | a406272 | 2022-05-02 17:46:07 +0200 | [diff] [blame] | 596 | TEST_P(VideoReceiveStream2Test, PassesFrameWhenEncodedFramesCallbackSet) { |
Tommi | ae4d097 | 2020-05-18 08:45:38 +0200 | [diff] [blame] | 597 | testing::MockFunction<void(const RecordableEncodedFrame&)> callback; |
| 598 | video_receive_stream_->Start(); |
| 599 | // Expect a keyframe request to be generated |
| 600 | EXPECT_CALL(mock_transport_, SendRtcp); |
| 601 | EXPECT_CALL(callback, Call); |
| 602 | video_receive_stream_->SetAndGetRecordingState( |
| 603 | VideoReceiveStream::RecordingState(callback.AsStdFunction()), true); |
| 604 | video_receive_stream_->OnCompleteFrame( |
| 605 | MakeFrame(VideoFrameType::kVideoFrameKey, 0)); |
Evan Shrubsole | a406272 | 2022-05-02 17:46:07 +0200 | [diff] [blame] | 606 | EXPECT_TRUE(fake_renderer_.WaitForFrame(kDefaultTimeOut)); |
Tommi | ae4d097 | 2020-05-18 08:45:38 +0200 | [diff] [blame] | 607 | video_receive_stream_->Stop(); |
| 608 | } |
| 609 | |
Evan Shrubsole | a406272 | 2022-05-02 17:46:07 +0200 | [diff] [blame] | 610 | TEST_P(VideoReceiveStream2Test, MovesEncodedFrameDispatchStateWhenReCreating) { |
Tommi | ae4d097 | 2020-05-18 08:45:38 +0200 | [diff] [blame] | 611 | testing::MockFunction<void(const RecordableEncodedFrame&)> callback; |
| 612 | video_receive_stream_->Start(); |
| 613 | // Expect a key frame request over RTCP. |
| 614 | EXPECT_CALL(mock_transport_, SendRtcp).Times(1); |
| 615 | video_receive_stream_->SetAndGetRecordingState( |
| 616 | VideoReceiveStream::RecordingState(callback.AsStdFunction()), true); |
| 617 | video_receive_stream_->Stop(); |
| 618 | VideoReceiveStream::RecordingState old_state = |
| 619 | video_receive_stream_->SetAndGetRecordingState( |
| 620 | VideoReceiveStream::RecordingState(), false); |
Evan Shrubsole | 44be579 | 2022-04-26 16:24:41 +0200 | [diff] [blame] | 621 | RecreateReceiveStream(std::move(old_state)); |
Tommi | ae4d097 | 2020-05-18 08:45:38 +0200 | [diff] [blame] | 622 | video_receive_stream_->Stop(); |
| 623 | } |
| 624 | |
Evan Shrubsole | a406272 | 2022-05-02 17:46:07 +0200 | [diff] [blame] | 625 | TEST_P(VideoReceiveStream2Test, RequestsKeyFramesUntilKeyFrameReceived) { |
Evan Shrubsole | 44be579 | 2022-04-26 16:24:41 +0200 | [diff] [blame] | 626 | // Recreate receive stream with shorter delay to test rtx. |
| 627 | TimeDelta rtx_delay = TimeDelta::Millis(50); |
| 628 | config_.rtp.nack.rtp_history_ms = rtx_delay.ms(); |
| 629 | auto tick = rtx_delay / 2; |
| 630 | RecreateReceiveStream(); |
| 631 | video_receive_stream_->Start(); |
Markus Handell | 588f9b3 | 2021-04-08 19:19:50 +0200 | [diff] [blame] | 632 | |
Evan Shrubsole | 44be579 | 2022-04-26 16:24:41 +0200 | [diff] [blame] | 633 | EXPECT_CALL(mock_transport_, SendRtcp).Times(1).WillOnce(testing::Return(0)); |
| 634 | video_receive_stream_->GenerateKeyFrame(); |
| 635 | video_receive_stream_->OnCompleteFrame( |
| 636 | MakeFrame(VideoFrameType::kVideoFrameDelta, 0)); |
Evan Shrubsole | a406272 | 2022-05-02 17:46:07 +0200 | [diff] [blame] | 637 | fake_renderer_.WaitForFrame(kDefaultTimeOut); |
Tommi | ae4d097 | 2020-05-18 08:45:38 +0200 | [diff] [blame] | 638 | time_controller_.AdvanceTime(tick); |
Evan Shrubsole | 9a99905 | 2021-12-12 15:27:00 +0100 | [diff] [blame] | 639 | loop_.Flush(); |
Evan Shrubsole | 44be579 | 2022-04-26 16:24:41 +0200 | [diff] [blame] | 640 | video_receive_stream_->OnCompleteFrame( |
| 641 | MakeFrame(VideoFrameType::kVideoFrameDelta, 1)); |
Evan Shrubsole | a406272 | 2022-05-02 17:46:07 +0200 | [diff] [blame] | 642 | fake_renderer_.WaitForFrame(kDefaultTimeOut); |
Evan Shrubsole | 44be579 | 2022-04-26 16:24:41 +0200 | [diff] [blame] | 643 | time_controller_.AdvanceTime(TimeDelta::Zero()); |
Tommi | ae4d097 | 2020-05-18 08:45:38 +0200 | [diff] [blame] | 644 | testing::Mock::VerifyAndClearExpectations(&mock_transport_); |
| 645 | |
Philipp Hancke | 006206d | 2021-03-24 17:49:02 +0100 | [diff] [blame] | 646 | // T+keyframetimeout: still no key frame received, expect key frame request |
| 647 | // sent again. |
Evan Shrubsole | 44be579 | 2022-04-26 16:24:41 +0200 | [diff] [blame] | 648 | EXPECT_CALL(mock_transport_, SendRtcp).Times(1).WillOnce(testing::Return(0)); |
Tommi | ae4d097 | 2020-05-18 08:45:38 +0200 | [diff] [blame] | 649 | time_controller_.AdvanceTime(tick); |
Evan Shrubsole | 44be579 | 2022-04-26 16:24:41 +0200 | [diff] [blame] | 650 | video_receive_stream_->OnCompleteFrame( |
| 651 | MakeFrame(VideoFrameType::kVideoFrameDelta, 2)); |
Evan Shrubsole | a406272 | 2022-05-02 17:46:07 +0200 | [diff] [blame] | 652 | EXPECT_THAT(fake_renderer_.WaitForFrame(kDefaultTimeOut), RenderedFrame()); |
Evan Shrubsole | 44be579 | 2022-04-26 16:24:41 +0200 | [diff] [blame] | 653 | loop_.Flush(); |
Tommi | ae4d097 | 2020-05-18 08:45:38 +0200 | [diff] [blame] | 654 | testing::Mock::VerifyAndClearExpectations(&mock_transport_); |
| 655 | |
Philipp Hancke | 006206d | 2021-03-24 17:49:02 +0100 | [diff] [blame] | 656 | // T+keyframetimeout: now send a key frame - we should not observe new key |
| 657 | // frame requests after this. |
Tommi | ae4d097 | 2020-05-18 08:45:38 +0200 | [diff] [blame] | 658 | EXPECT_CALL(mock_transport_, SendRtcp).Times(0); |
Evan Shrubsole | 44be579 | 2022-04-26 16:24:41 +0200 | [diff] [blame] | 659 | video_receive_stream_->OnCompleteFrame( |
| 660 | MakeFrame(VideoFrameType::kVideoFrameKey, 3)); |
Evan Shrubsole | a406272 | 2022-05-02 17:46:07 +0200 | [diff] [blame] | 661 | EXPECT_THAT(fake_renderer_.WaitForFrame(kDefaultTimeOut), RenderedFrame()); |
Tommi | ae4d097 | 2020-05-18 08:45:38 +0200 | [diff] [blame] | 662 | time_controller_.AdvanceTime(2 * tick); |
Evan Shrubsole | 44be579 | 2022-04-26 16:24:41 +0200 | [diff] [blame] | 663 | video_receive_stream_->OnCompleteFrame( |
| 664 | MakeFrame(VideoFrameType::kVideoFrameDelta, 4)); |
Evan Shrubsole | a406272 | 2022-05-02 17:46:07 +0200 | [diff] [blame] | 665 | EXPECT_THAT(fake_renderer_.WaitForFrame(kDefaultTimeOut), RenderedFrame()); |
Evan Shrubsole | 44be579 | 2022-04-26 16:24:41 +0200 | [diff] [blame] | 666 | loop_.Flush(); |
Tommi | ae4d097 | 2020-05-18 08:45:38 +0200 | [diff] [blame] | 667 | } |
| 668 | |
Evan Shrubsole | a406272 | 2022-05-02 17:46:07 +0200 | [diff] [blame] | 669 | TEST_P(VideoReceiveStream2Test, |
Markus Handell | 588f9b3 | 2021-04-08 19:19:50 +0200 | [diff] [blame] | 670 | DispatchesEncodedFrameSequenceStartingWithKeyframeWithoutResolution) { |
Evan Shrubsole | 44be579 | 2022-04-26 16:24:41 +0200 | [diff] [blame] | 671 | video_receive_stream_->Start(); |
Markus Handell | 588f9b3 | 2021-04-08 19:19:50 +0200 | [diff] [blame] | 672 | testing::MockFunction<void(const RecordableEncodedFrame&)> callback; |
Evan Shrubsole | 44be579 | 2022-04-26 16:24:41 +0200 | [diff] [blame] | 673 | video_receive_stream_->SetAndGetRecordingState( |
Markus Handell | 588f9b3 | 2021-04-08 19:19:50 +0200 | [diff] [blame] | 674 | VideoReceiveStream::RecordingState(callback.AsStdFunction()), |
| 675 | /*generate_key_frame=*/false); |
| 676 | |
| 677 | InSequence s; |
Evan Shrubsole | 44be579 | 2022-04-26 16:24:41 +0200 | [diff] [blame] | 678 | EXPECT_CALL(callback, Call(Resolution(test::FakeDecoder::kDefaultWidth, |
| 679 | test::FakeDecoder::kDefaultHeight))); |
Markus Handell | 588f9b3 | 2021-04-08 19:19:50 +0200 | [diff] [blame] | 680 | EXPECT_CALL(callback, Call); |
| 681 | |
Evan Shrubsole | 44be579 | 2022-04-26 16:24:41 +0200 | [diff] [blame] | 682 | video_receive_stream_->OnCompleteFrame( |
Markus Handell | 588f9b3 | 2021-04-08 19:19:50 +0200 | [diff] [blame] | 683 | MakeFrameWithResolution(VideoFrameType::kVideoFrameKey, 0, 0, 0)); |
Evan Shrubsole | a406272 | 2022-05-02 17:46:07 +0200 | [diff] [blame] | 684 | EXPECT_THAT(fake_renderer_.WaitForFrame(kDefaultTimeOut), RenderedFrame()); |
Evan Shrubsole | 44be579 | 2022-04-26 16:24:41 +0200 | [diff] [blame] | 685 | video_receive_stream_->OnCompleteFrame( |
Markus Handell | 588f9b3 | 2021-04-08 19:19:50 +0200 | [diff] [blame] | 686 | MakeFrameWithResolution(VideoFrameType::kVideoFrameDelta, 1, 0, 0)); |
Evan Shrubsole | a406272 | 2022-05-02 17:46:07 +0200 | [diff] [blame] | 687 | EXPECT_THAT(fake_renderer_.WaitForFrame(kDefaultTimeOut), RenderedFrame()); |
Markus Handell | 588f9b3 | 2021-04-08 19:19:50 +0200 | [diff] [blame] | 688 | |
Evan Shrubsole | 44be579 | 2022-04-26 16:24:41 +0200 | [diff] [blame] | 689 | video_receive_stream_->Stop(); |
Markus Handell | 588f9b3 | 2021-04-08 19:19:50 +0200 | [diff] [blame] | 690 | } |
| 691 | |
Evan Shrubsole | a406272 | 2022-05-02 17:46:07 +0200 | [diff] [blame] | 692 | TEST_P(VideoReceiveStream2Test, |
Markus Handell | 588f9b3 | 2021-04-08 19:19:50 +0200 | [diff] [blame] | 693 | DispatchesEncodedFrameSequenceStartingWithKeyframeWithResolution) { |
Evan Shrubsole | 44be579 | 2022-04-26 16:24:41 +0200 | [diff] [blame] | 694 | video_receive_stream_->Start(); |
Markus Handell | 588f9b3 | 2021-04-08 19:19:50 +0200 | [diff] [blame] | 695 | testing::MockFunction<void(const RecordableEncodedFrame&)> callback; |
Evan Shrubsole | 44be579 | 2022-04-26 16:24:41 +0200 | [diff] [blame] | 696 | video_receive_stream_->SetAndGetRecordingState( |
Markus Handell | 588f9b3 | 2021-04-08 19:19:50 +0200 | [diff] [blame] | 697 | VideoReceiveStream::RecordingState(callback.AsStdFunction()), |
| 698 | /*generate_key_frame=*/false); |
| 699 | |
| 700 | InSequence s; |
Evan Shrubsole | 44be579 | 2022-04-26 16:24:41 +0200 | [diff] [blame] | 701 | EXPECT_CALL(callback, Call(Resolution(1080u, 720u))); |
Markus Handell | 588f9b3 | 2021-04-08 19:19:50 +0200 | [diff] [blame] | 702 | EXPECT_CALL(callback, Call); |
| 703 | |
Evan Shrubsole | 44be579 | 2022-04-26 16:24:41 +0200 | [diff] [blame] | 704 | video_receive_stream_->OnCompleteFrame( |
Markus Handell | 588f9b3 | 2021-04-08 19:19:50 +0200 | [diff] [blame] | 705 | MakeFrameWithResolution(VideoFrameType::kVideoFrameKey, 0, 1080, 720)); |
Evan Shrubsole | a406272 | 2022-05-02 17:46:07 +0200 | [diff] [blame] | 706 | EXPECT_THAT(fake_renderer_.WaitForFrame(kDefaultTimeOut), RenderedFrame()); |
Evan Shrubsole | 44be579 | 2022-04-26 16:24:41 +0200 | [diff] [blame] | 707 | video_receive_stream_->OnCompleteFrame( |
Markus Handell | 588f9b3 | 2021-04-08 19:19:50 +0200 | [diff] [blame] | 708 | MakeFrameWithResolution(VideoFrameType::kVideoFrameDelta, 1, 0, 0)); |
Evan Shrubsole | a406272 | 2022-05-02 17:46:07 +0200 | [diff] [blame] | 709 | EXPECT_THAT(fake_renderer_.WaitForFrame(kDefaultTimeOut), RenderedFrame()); |
Markus Handell | 588f9b3 | 2021-04-08 19:19:50 +0200 | [diff] [blame] | 710 | |
Evan Shrubsole | 44be579 | 2022-04-26 16:24:41 +0200 | [diff] [blame] | 711 | video_receive_stream_->Stop(); |
Markus Handell | 588f9b3 | 2021-04-08 19:19:50 +0200 | [diff] [blame] | 712 | } |
| 713 | |
Evan Shrubsole | a406272 | 2022-05-02 17:46:07 +0200 | [diff] [blame] | 714 | TEST_P(VideoReceiveStream2Test, DependantFramesAreScheduled) { |
| 715 | video_receive_stream_->Start(); |
| 716 | |
| 717 | auto key_frame = test::FakeFrameBuilder() |
| 718 | .Id(0) |
| 719 | .PayloadType(99) |
| 720 | .Time(kFirstRtpTimestamp) |
| 721 | .ReceivedTime(kStartTime) |
| 722 | .AsLast() |
| 723 | .Build(); |
| 724 | auto delta_frame = test::FakeFrameBuilder() |
| 725 | .Id(1) |
| 726 | .PayloadType(99) |
| 727 | .Time(RtpTimestampForFrame(1)) |
| 728 | .ReceivedTime(ReceiveTimeForFrame(1)) |
| 729 | .Refs({0}) |
| 730 | .AsLast() |
| 731 | .Build(); |
| 732 | |
| 733 | // Expect frames are decoded in order. |
| 734 | InSequence seq; |
| 735 | EXPECT_CALL(mock_decoder_, |
| 736 | Decode(test::RtpTimestamp(kFirstRtpTimestamp), _, _)); |
| 737 | EXPECT_CALL(mock_decoder_, Decode(test::RtpTimestamp(kFirstRtpTimestamp + |
| 738 | k30FpsRtpTimestampDelta), |
| 739 | _, _)) |
| 740 | .Times(1); |
| 741 | video_receive_stream_->OnCompleteFrame(std::move(key_frame)); |
| 742 | EXPECT_THAT(fake_renderer_.WaitForFrame(TimeDelta::Zero()), RenderedFrame()); |
| 743 | |
| 744 | time_controller_.AdvanceTime(k30FpsDelay); |
| 745 | video_receive_stream_->OnCompleteFrame(std::move(delta_frame)); |
| 746 | EXPECT_THAT(fake_renderer_.WaitForFrame(k30FpsDelay), RenderedFrame()); |
| 747 | |
| 748 | video_receive_stream_->Stop(); |
| 749 | } |
| 750 | |
| 751 | TEST_P(VideoReceiveStream2Test, FramesScheduledInOrder) { |
| 752 | video_receive_stream_->Start(); |
| 753 | |
| 754 | auto key_frame = test::FakeFrameBuilder() |
| 755 | .Id(0) |
| 756 | .PayloadType(99) |
| 757 | .Time(kFirstRtpTimestamp) |
| 758 | .AsLast() |
| 759 | .Build(); |
| 760 | auto delta_frame1 = test::FakeFrameBuilder() |
| 761 | .Id(1) |
| 762 | .PayloadType(99) |
| 763 | .Time(RtpTimestampForFrame(1)) |
| 764 | .Refs({0}) |
| 765 | .AsLast() |
| 766 | .Build(); |
| 767 | auto delta_frame2 = test::FakeFrameBuilder() |
| 768 | .Id(2) |
| 769 | .PayloadType(99) |
| 770 | .Time(RtpTimestampForFrame(2)) |
| 771 | .Refs({1}) |
| 772 | .AsLast() |
| 773 | .Build(); |
| 774 | |
| 775 | // Expect frames are decoded in order despite delta_frame1 arriving first. |
| 776 | InSequence seq; |
| 777 | EXPECT_CALL(mock_decoder_, |
| 778 | Decode(test::RtpTimestamp(kFirstRtpTimestamp), _, _)) |
| 779 | .Times(1); |
| 780 | EXPECT_CALL(mock_decoder_, |
| 781 | Decode(test::RtpTimestamp(RtpTimestampForFrame(1)), _, _)) |
| 782 | .Times(1); |
| 783 | EXPECT_CALL(mock_decoder_, |
| 784 | Decode(test::RtpTimestamp(RtpTimestampForFrame(2)), _, _)) |
| 785 | .Times(1); |
| 786 | key_frame->SetReceivedTime(clock_->CurrentTime().ms()); |
| 787 | video_receive_stream_->OnCompleteFrame(std::move(key_frame)); |
| 788 | EXPECT_THAT(fake_renderer_.WaitForFrame(TimeDelta::Zero()), RenderedFrame()); |
| 789 | |
| 790 | delta_frame2->SetReceivedTime(clock_->CurrentTime().ms()); |
| 791 | video_receive_stream_->OnCompleteFrame(std::move(delta_frame2)); |
| 792 | EXPECT_THAT(fake_renderer_.WaitForFrame(k30FpsDelay), DidNotReceiveFrame()); |
| 793 | // `delta_frame1` arrives late. |
| 794 | delta_frame1->SetReceivedTime(clock_->CurrentTime().ms()); |
| 795 | video_receive_stream_->OnCompleteFrame(std::move(delta_frame1)); |
| 796 | EXPECT_THAT(fake_renderer_.WaitForFrame(k30FpsDelay), RenderedFrame()); |
| 797 | EXPECT_THAT(fake_renderer_.WaitForFrame(k30FpsDelay * 2), RenderedFrame()); |
| 798 | video_receive_stream_->Stop(); |
| 799 | } |
| 800 | |
| 801 | TEST_P(VideoReceiveStream2Test, WaitsforAllSpatialLayers) { |
| 802 | video_receive_stream_->Start(); |
| 803 | auto sl0 = test::FakeFrameBuilder() |
| 804 | .Id(0) |
| 805 | .PayloadType(99) |
| 806 | .Time(kFirstRtpTimestamp) |
| 807 | .ReceivedTime(kStartTime) |
| 808 | .Build(); |
| 809 | auto sl1 = test::FakeFrameBuilder() |
| 810 | .Id(1) |
| 811 | .PayloadType(99) |
| 812 | .ReceivedTime(kStartTime) |
| 813 | .Time(kFirstRtpTimestamp) |
| 814 | .Refs({0}) |
| 815 | .Build(); |
| 816 | auto sl2 = test::FakeFrameBuilder() |
| 817 | .Id(2) |
| 818 | .PayloadType(99) |
| 819 | .ReceivedTime(kStartTime) |
| 820 | .Time(kFirstRtpTimestamp) |
| 821 | .Refs({0, 1}) |
| 822 | .AsLast() |
| 823 | .Build(); |
| 824 | |
| 825 | // No decodes should be called until `sl2` is received. |
| 826 | EXPECT_CALL(mock_decoder_, Decode).Times(0); |
| 827 | sl0->SetReceivedTime(clock_->CurrentTime().ms()); |
| 828 | video_receive_stream_->OnCompleteFrame(std::move(sl0)); |
| 829 | EXPECT_THAT(fake_renderer_.WaitForFrame(TimeDelta::Zero()), |
| 830 | DidNotReceiveFrame()); |
| 831 | video_receive_stream_->OnCompleteFrame(std::move(sl1)); |
| 832 | EXPECT_THAT(fake_renderer_.WaitForFrame(TimeDelta::Zero()), |
| 833 | DidNotReceiveFrame()); |
| 834 | // When `sl2` arrives decode should happen. |
| 835 | EXPECT_CALL(mock_decoder_, |
| 836 | Decode(test::RtpTimestamp(kFirstRtpTimestamp), _, _)) |
| 837 | .Times(1); |
| 838 | video_receive_stream_->OnCompleteFrame(std::move(sl2)); |
| 839 | EXPECT_THAT(fake_renderer_.WaitForFrame(TimeDelta::Zero()), RenderedFrame()); |
| 840 | video_receive_stream_->Stop(); |
| 841 | } |
| 842 | |
| 843 | TEST_P(VideoReceiveStream2Test, FramesFastForwardOnSystemHalt) { |
| 844 | video_receive_stream_->Start(); |
| 845 | |
| 846 | // The frame structure looks like this, |
| 847 | // F1 |
| 848 | // / |
| 849 | // F0 --> F2 |
| 850 | // |
| 851 | // In this case we will have a system halt simulated. By the time the system |
| 852 | // resumes, F1 will be old and so F2 should be decoded. |
| 853 | auto key_frame = test::FakeFrameBuilder() |
| 854 | .Id(0) |
| 855 | .PayloadType(99) |
| 856 | .Time(kFirstRtpTimestamp) |
| 857 | .AsLast() |
| 858 | .Build(); |
| 859 | auto ffwd_frame = test::FakeFrameBuilder() |
| 860 | .Id(1) |
| 861 | .PayloadType(99) |
| 862 | .Time(RtpTimestampForFrame(1)) |
| 863 | .Refs({0}) |
| 864 | .AsLast() |
| 865 | .Build(); |
| 866 | auto rendered_frame = test::FakeFrameBuilder() |
| 867 | .Id(2) |
| 868 | .PayloadType(99) |
| 869 | .Time(RtpTimestampForFrame(2)) |
| 870 | .Refs({0}) |
| 871 | .AsLast() |
| 872 | .Build(); |
| 873 | InSequence seq; |
| 874 | EXPECT_CALL(mock_decoder_, |
| 875 | Decode(test::RtpTimestamp(kFirstRtpTimestamp), _, _)) |
| 876 | .WillOnce(testing::DoAll(testing::Invoke([&] { |
| 877 | // System halt will be simulated in the decode. |
| 878 | time_controller_.AdvanceTime(k30FpsDelay * 2); |
| 879 | }), |
| 880 | DefaultDecodeAction())); |
| 881 | EXPECT_CALL(mock_decoder_, |
| 882 | Decode(test::RtpTimestamp(RtpTimestampForFrame(2)), _, _)); |
| 883 | video_receive_stream_->OnCompleteFrame(std::move(key_frame)); |
| 884 | video_receive_stream_->OnCompleteFrame(std::move(ffwd_frame)); |
| 885 | video_receive_stream_->OnCompleteFrame(std::move(rendered_frame)); |
| 886 | EXPECT_THAT(fake_renderer_.WaitForFrame(TimeDelta::Zero()), RenderedFrame()); |
| 887 | EXPECT_THAT(fake_renderer_.WaitForFrame(TimeDelta::Zero()), RenderedFrame()); |
| 888 | |
| 889 | // Check stats show correct dropped frames. |
| 890 | auto stats = video_receive_stream_->GetStats(); |
| 891 | EXPECT_EQ(stats.frames_dropped, 1u); |
| 892 | |
| 893 | video_receive_stream_->Stop(); |
| 894 | } |
| 895 | |
| 896 | TEST_P(VideoReceiveStream2Test, BetterFrameInsertedWhileWaitingToDecodeFrame) { |
| 897 | video_receive_stream_->Start(); |
| 898 | |
| 899 | auto key_frame = test::FakeFrameBuilder() |
| 900 | .Id(0) |
| 901 | .PayloadType(99) |
| 902 | .Time(kFirstRtpTimestamp) |
| 903 | .ReceivedTime(ReceiveTimeForFrame(0)) |
| 904 | .AsLast() |
| 905 | .Build(); |
| 906 | auto f1 = test::FakeFrameBuilder() |
| 907 | .Id(1) |
| 908 | .PayloadType(99) |
| 909 | .Time(RtpTimestampForFrame(1)) |
| 910 | .ReceivedTime(ReceiveTimeForFrame(1)) |
| 911 | .Refs({0}) |
| 912 | .AsLast() |
| 913 | .Build(); |
| 914 | auto f2 = test::FakeFrameBuilder() |
| 915 | .Id(2) |
| 916 | .PayloadType(99) |
| 917 | .Time(RtpTimestampForFrame(2)) |
| 918 | .ReceivedTime(ReceiveTimeForFrame(2)) |
| 919 | .Refs({0}) |
| 920 | .AsLast() |
| 921 | .Build(); |
| 922 | |
| 923 | video_receive_stream_->OnCompleteFrame(std::move(key_frame)); |
| 924 | EXPECT_THAT(fake_renderer_.WaitForFrame(TimeDelta::Zero()), RenderedFrame()); |
| 925 | |
| 926 | InSequence seq; |
| 927 | EXPECT_CALL(mock_decoder_, |
| 928 | Decode(test::RtpTimestamp(RtpTimestampForFrame(1)), _, _)) |
| 929 | .Times(1); |
| 930 | EXPECT_CALL(mock_decoder_, |
| 931 | Decode(test::RtpTimestamp(RtpTimestampForFrame(2)), _, _)) |
| 932 | .Times(1); |
| 933 | // Simulate f1 arriving after f2 but before f2 is decoded. |
| 934 | video_receive_stream_->OnCompleteFrame(std::move(f2)); |
| 935 | EXPECT_THAT(fake_renderer_.WaitForFrame(k30FpsDelay), DidNotReceiveFrame()); |
| 936 | video_receive_stream_->OnCompleteFrame(std::move(f1)); |
| 937 | EXPECT_THAT(fake_renderer_.WaitForFrame(k30FpsDelay), RenderedFrame()); |
| 938 | EXPECT_THAT(fake_renderer_.WaitForFrame(k30FpsDelay), RenderedFrame()); |
| 939 | |
| 940 | video_receive_stream_->Stop(); |
| 941 | } |
| 942 | |
| 943 | // Note: This test takes a long time (~10s) to run if the fake metronome is |
| 944 | // active. Since the test needs to wait for the timestamp to rollover, it has a |
| 945 | // fake delay of around 6.5 hours. Even though time is simulated, this will be |
| 946 | // around 1,500,000 metronome tick invocations. |
| 947 | TEST_P(VideoReceiveStream2Test, RtpTimestampWrapAround) { |
| 948 | video_receive_stream_->Start(); |
| 949 | |
| 950 | constexpr uint32_t kBaseRtp = std::numeric_limits<uint32_t>::max() / 2; |
| 951 | video_receive_stream_->OnCompleteFrame( |
| 952 | test::FakeFrameBuilder() |
| 953 | .Id(0) |
| 954 | .PayloadType(99) |
| 955 | .Time(kBaseRtp) |
| 956 | .ReceivedTime(clock_->CurrentTime()) |
| 957 | .AsLast() |
| 958 | .Build()); |
| 959 | EXPECT_THAT(fake_renderer_.WaitForFrame(TimeDelta::Zero()), RenderedFrame()); |
| 960 | time_controller_.AdvanceTime(k30FpsDelay); |
| 961 | video_receive_stream_->OnCompleteFrame( |
| 962 | test::FakeFrameBuilder() |
| 963 | .Id(1) |
| 964 | .PayloadType(99) |
| 965 | .Time(kBaseRtp + k30FpsRtpTimestampDelta) |
| 966 | .ReceivedTime(clock_->CurrentTime()) |
| 967 | .AsLast() |
| 968 | .Build()); |
| 969 | EXPECT_THAT(fake_renderer_.WaitForFrame(k30FpsDelay), RenderedFrame()); |
| 970 | |
| 971 | // Pause stream so that RTP timestamp wraps around. |
| 972 | constexpr uint32_t kLastRtp = kBaseRtp + k30FpsRtpTimestampDelta; |
| 973 | constexpr uint32_t kWrapAroundRtp = |
| 974 | kLastRtp + std::numeric_limits<uint32_t>::max() / 2 + 1; |
| 975 | // Pause for corresponding delay such that RTP timestamp would increase this |
| 976 | // much at 30fps. |
| 977 | constexpr TimeDelta kWrapAroundDelay = |
| 978 | (std::numeric_limits<uint32_t>::max() / 2 + 1) / kRtpTimestampHz; |
| 979 | |
| 980 | time_controller_.AdvanceTime(kWrapAroundDelay); |
| 981 | video_receive_stream_->OnCompleteFrame( |
| 982 | test::FakeFrameBuilder() |
| 983 | .Id(2) |
| 984 | .PayloadType(99) |
| 985 | .Time(kWrapAroundRtp) |
| 986 | .ReceivedTime(clock_->CurrentTime()) |
| 987 | .AsLast() |
| 988 | .Build()); |
| 989 | EXPECT_CALL(mock_decoder_, Decode(test::RtpTimestamp(kWrapAroundRtp), _, _)) |
| 990 | .Times(1); |
| 991 | EXPECT_THAT(fake_renderer_.WaitForFrame(TimeDelta::Zero()), RenderedFrame()); |
| 992 | |
| 993 | video_receive_stream_->Stop(); |
| 994 | } |
| 995 | |
| 996 | INSTANTIATE_TEST_SUITE_P(VideoReceiveStream2Test, |
| 997 | VideoReceiveStream2Test, |
| 998 | testing::Bool(), |
| 999 | [](const auto& test_param_info) { |
| 1000 | rtc::StringBuilder ss; |
| 1001 | ss << (test_param_info.param |
| 1002 | ? "ScheduleDecodesWithMetronome" |
| 1003 | : "ScheduleDecodesWithPostTask"); |
| 1004 | return ss.Release(); |
| 1005 | }); |
| 1006 | |
Tommi | ae4d097 | 2020-05-18 08:45:38 +0200 | [diff] [blame] | 1007 | } // namespace webrtc |