blob: 1ded35f0175afa93ccb1c5cc6a6357f77b5b7944 [file] [log] [blame]
pbos@webrtc.org29d58392013-05-16 12:08:03 +00001/*
2 * Copyright (c) 2013 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
Mirko Bonadei92ea95e2017-09-15 06:47:31 +020011#include "video/video_receive_stream.h"
pbos@webrtc.org29d58392013-05-16 12:08:03 +000012
pbos@webrtc.org12dc1a32013-08-05 16:22:53 +000013#include <stdlib.h>
Yves Gerey3e707812018-11-28 16:47:49 +010014#include <string.h>
Niels Möller46879152019-01-07 15:54:47 +010015#include <algorithm>
mflodmand1590b22015-12-09 07:07:59 -080016#include <set>
mflodman@webrtc.orgb429e512013-12-18 09:46:22 +000017#include <string>
Erik Språng737336d2016-07-29 12:59:36 +020018#include <utility>
mflodman@webrtc.orgb429e512013-12-18 09:46:22 +000019
Steve Antonbd631a02019-03-28 10:51:27 -070020#include "absl/algorithm/container.h"
Karl Wiberg918f50c2018-07-05 11:40:33 +020021#include "absl/memory/memory.h"
Danil Chapovalovb9b146c2018-06-15 12:28:07 +020022#include "absl/types/optional.h"
Yves Gerey3e707812018-11-28 16:47:49 +010023#include "api/array_view.h"
Steve Anton10542f22019-01-11 09:11:00 -080024#include "api/crypto/frame_decryptor_interface.h"
Yves Gerey3e707812018-11-28 16:47:49 +010025#include "api/video/encoded_image.h"
26#include "api/video_codecs/sdp_video_format.h"
27#include "api/video_codecs/video_codec.h"
Niels Möllercbcbc222018-09-28 09:07:24 +020028#include "api/video_codecs/video_decoder_factory.h"
Yves Gerey3e707812018-11-28 16:47:49 +010029#include "api/video_codecs/video_encoder.h"
Mirko Bonadei92ea95e2017-09-15 06:47:31 +020030#include "call/rtp_stream_receiver_controller_interface.h"
31#include "call/rtx_receive_stream.h"
Patrik Höglundbe214a22018-01-04 12:14:35 +010032#include "common_video/include/incoming_video_stream.h"
Yves Gerey3e707812018-11-28 16:47:49 +010033#include "media/base/h264_profile_level_id.h"
Mirko Bonadei92ea95e2017-09-15 06:47:31 +020034#include "modules/utility/include/process_thread.h"
Yves Gerey3e707812018-11-28 16:47:49 +010035#include "modules/video_coding/include/video_codec_interface.h"
36#include "modules/video_coding/include/video_coding_defines.h"
37#include "modules/video_coding/include/video_error_codes.h"
Mirko Bonadei92ea95e2017-09-15 06:47:31 +020038#include "modules/video_coding/jitter_estimator.h"
39#include "modules/video_coding/timing.h"
Niels Möller147013a2018-10-01 15:56:33 +020040#include "modules/video_coding/utility/vp8_header_parser.h"
Mirko Bonadei92ea95e2017-09-15 06:47:31 +020041#include "rtc_base/checks.h"
Rasmus Brandt3dde4502019-03-21 11:46:17 +010042#include "rtc_base/experiments/keyframe_interval_settings.h"
Mirko Bonadei92ea95e2017-09-15 06:47:31 +020043#include "rtc_base/location.h"
44#include "rtc_base/logging.h"
Erik Språng96965ae2018-10-23 15:42:37 +020045#include "rtc_base/platform_file.h"
46#include "rtc_base/strings/string_builder.h"
Karl Wibergab036382019-03-12 18:01:51 +010047#include "rtc_base/system/thread_registry.h"
Steve Anton10542f22019-01-11 09:11:00 -080048#include "rtc_base/time_utils.h"
Mirko Bonadei92ea95e2017-09-15 06:47:31 +020049#include "rtc_base/trace_event.h"
50#include "system_wrappers/include/clock.h"
51#include "system_wrappers/include/field_trial.h"
52#include "video/call_stats.h"
Erik Språng96965ae2018-10-23 15:42:37 +020053#include "video/frame_dumping_decoder.h"
Mirko Bonadei92ea95e2017-09-15 06:47:31 +020054#include "video/receive_statistics_proxy.h"
pbos@webrtc.org29d58392013-05-16 12:08:03 +000055
56namespace webrtc {
mflodmana20de202015-10-18 22:08:19 -070057
pbos@webrtc.org776e6f22014-10-29 15:28:39 +000058namespace {
Sebastian Jansson13943b72019-04-02 15:08:14 +020059
60using video_coding::EncodedFrame;
61using ReturnReason = video_coding::FrameBuffer::ReturnReason;
62
Ruslan Burakov493a6502019-02-27 15:32:48 +010063constexpr int kMinBaseMinimumDelayMs = 0;
64constexpr int kMaxBaseMinimumDelayMs = 10000;
65
Rasmus Brandt3dde4502019-03-21 11:46:17 +010066constexpr int kMaxWaitForKeyFrameMs = 200;
67constexpr int kMaxWaitForFrameMs = 3000;
68
pbos@webrtc.org776e6f22014-10-29 15:28:39 +000069VideoCodec CreateDecoderVideoCodec(const VideoReceiveStream::Decoder& decoder) {
70 VideoCodec codec;
71 memset(&codec, 0, sizeof(codec));
72
73 codec.plType = decoder.payload_type;
Niels Möllercb7e1d22018-09-11 15:56:04 +020074 codec.codecType = PayloadStringToCodecType(decoder.video_format.name);
pbos@webrtc.org776e6f22014-10-29 15:28:39 +000075
76 if (codec.codecType == kVideoCodecVP8) {
hta257dc392016-10-25 09:05:06 -070077 *(codec.VP8()) = VideoEncoder::GetDefaultVp8Settings();
ivica05cfcd32015-09-07 06:04:16 -070078 } else if (codec.codecType == kVideoCodecVP9) {
hta257dc392016-10-25 09:05:06 -070079 *(codec.VP9()) = VideoEncoder::GetDefaultVp9Settings();
pbos@webrtc.org776e6f22014-10-29 15:28:39 +000080 } else if (codec.codecType == kVideoCodecH264) {
hta257dc392016-10-25 09:05:06 -070081 *(codec.H264()) = VideoEncoder::GetDefaultH264Settings();
magjede69a1a92016-11-25 10:06:31 -080082 codec.H264()->profile =
Niels Möllercb7e1d22018-09-11 15:56:04 +020083 H264::ParseSdpProfileLevelId(decoder.video_format.parameters)->profile;
Emircan Uysalerd7ae3c32018-01-25 13:01:09 -080084 } else if (codec.codecType == kVideoCodecMultiplex) {
Emircan Uysaler0a375472017-12-11 12:21:02 +053085 VideoReceiveStream::Decoder associated_decoder = decoder;
Niels Möllercb7e1d22018-09-11 15:56:04 +020086 associated_decoder.video_format =
87 SdpVideoFormat(CodecTypeToPayloadString(kVideoCodecVP9));
Emircan Uysaler0a375472017-12-11 12:21:02 +053088 VideoCodec associated_codec = CreateDecoderVideoCodec(associated_decoder);
Emircan Uysalerd7ae3c32018-01-25 13:01:09 -080089 associated_codec.codecType = kVideoCodecMultiplex;
Emircan Uysaler0a375472017-12-11 12:21:02 +053090 return associated_codec;
pbos@webrtc.org776e6f22014-10-29 15:28:39 +000091 }
92
93 codec.width = 320;
94 codec.height = 180;
stefanb4bc65b2016-11-02 10:10:20 -070095 const int kDefaultStartBitrate = 300;
pbos@webrtc.org776e6f22014-10-29 15:28:39 +000096 codec.startBitrate = codec.minBitrate = codec.maxBitrate =
stefanb4bc65b2016-11-02 10:10:20 -070097 kDefaultStartBitrate;
pbos@webrtc.org776e6f22014-10-29 15:28:39 +000098
99 return codec;
100}
Niels Möllercbcbc222018-09-28 09:07:24 +0200101
102// Video decoder class to be used for unknown codecs. Doesn't support decoding
103// but logs messages to LS_ERROR.
104class NullVideoDecoder : public webrtc::VideoDecoder {
105 public:
106 int32_t InitDecode(const webrtc::VideoCodec* codec_settings,
107 int32_t number_of_cores) override {
108 RTC_LOG(LS_ERROR) << "Can't initialize NullVideoDecoder.";
109 return WEBRTC_VIDEO_CODEC_OK;
110 }
111
112 int32_t Decode(const webrtc::EncodedImage& input_image,
113 bool missing_frames,
Jeroen de Borst2c7b9822019-03-07 19:40:07 +0000114 const webrtc::CodecSpecificInfo* codec_specific_info,
Niels Möllercbcbc222018-09-28 09:07:24 +0200115 int64_t render_time_ms) override {
116 RTC_LOG(LS_ERROR) << "The NullVideoDecoder doesn't support decoding.";
117 return WEBRTC_VIDEO_CODEC_OK;
118 }
119
120 int32_t RegisterDecodeCompleteCallback(
121 webrtc::DecodedImageCallback* callback) override {
122 RTC_LOG(LS_ERROR)
123 << "Can't register decode complete callback on NullVideoDecoder.";
124 return WEBRTC_VIDEO_CODEC_OK;
125 }
126
127 int32_t Release() override { return WEBRTC_VIDEO_CODEC_OK; }
128
129 const char* ImplementationName() const override { return "NullVideoDecoder"; }
130};
131
Niels Möller46879152019-01-07 15:54:47 +0100132// Inherit video_coding::EncodedFrame, which is the class used by
133// video_coding::FrameBuffer and other components in the receive pipeline. It's
134// a subclass of EncodedImage, and it always owns the buffer.
135class EncodedFrameForMediaTransport : public video_coding::EncodedFrame {
136 public:
137 explicit EncodedFrameForMediaTransport(
138 MediaTransportEncodedVideoFrame frame) {
Niels Möller938dd9f2019-02-07 00:02:17 +0100139 // TODO(nisse): This is ugly. We copy the EncodedImage (a base class of
140 // ours, in several steps), to get all the meta data. We should be using
141 // std::move in some way. Then we also need to handle the case of an unowned
142 // buffer, in which case we need to make an owned copy.
Niels Möller46879152019-01-07 15:54:47 +0100143 *static_cast<class EncodedImage*>(this) = frame.encoded_image();
Niels Möller46879152019-01-07 15:54:47 +0100144
Niels Möller69bb3af2019-02-13 10:05:49 +0100145 // If we don't already own the buffer, make a copy.
146 Retain();
Niels Möller46879152019-01-07 15:54:47 +0100147
148 _payloadType = static_cast<uint8_t>(frame.payload_type());
149
150 // TODO(nisse): frame_id and picture_id are probably not the same thing. For
151 // a single layer, this should be good enough.
152 id.picture_id = frame.frame_id();
153 id.spatial_layer = frame.encoded_image().SpatialIndex().value_or(0);
154 num_references = std::min(static_cast<size_t>(kMaxFrameReferences),
155 frame.referenced_frame_ids().size());
156 for (size_t i = 0; i < num_references; i++) {
157 references[i] = frame.referenced_frame_ids()[i];
158 }
159 }
160
161 // TODO(nisse): Implement. Not sure how they are used.
162 int64_t ReceivedTime() const override { return 0; }
163 int64_t RenderTime() const override { return 0; }
164};
165
Ilya Nikolaevskiye6a2d942018-11-07 14:32:28 +0100166// TODO(https://bugs.webrtc.org/9974): Consider removing this workaround.
167// Maximum time between frames before resetting the FrameBuffer to avoid RTP
168// timestamps wraparound to affect FrameBuffer.
169constexpr int kInactiveStreamThresholdMs = 600000; // 10 minutes.
170
pbos@webrtc.org776e6f22014-10-29 15:28:39 +0000171} // namespace
pbos@webrtc.org29d58392013-05-16 12:08:03 +0000172
Peter Boströmca835252016-02-11 15:59:46 +0100173namespace internal {
tommi2e82f382016-06-21 00:26:43 -0700174
nisse0f15f922017-06-21 01:05:22 -0700175VideoReceiveStream::VideoReceiveStream(
Sebastian Jansson74682c12019-03-01 11:50:20 +0100176 TaskQueueFactory* task_queue_factory,
nisse0f15f922017-06-21 01:05:22 -0700177 RtpStreamReceiverControllerInterface* receiver_controller,
178 int num_cpu_cores,
179 PacketRouter* packet_router,
180 VideoReceiveStream::Config config,
181 ProcessThread* process_thread,
Ruslan Burakov493a6502019-02-27 15:32:48 +0100182 CallStats* call_stats,
183 Clock* clock,
184 VCMTiming* timing)
Sebastian Jansson74682c12019-03-01 11:50:20 +0100185 : task_queue_factory_(task_queue_factory),
186 transport_adapter_(config.rtcp_send_transport),
Tommi733b5472016-06-10 17:58:01 +0200187 config_(std::move(config)),
sprang113bdca2016-10-11 03:10:10 -0700188 num_cpu_cores_(num_cpu_cores),
Peter Boström1d04ac62016-02-05 11:25:46 +0100189 process_thread_(process_thread),
Ruslan Burakov493a6502019-02-27 15:32:48 +0100190 clock_(clock),
Sebastian Jansson13943b72019-04-02 15:08:14 +0200191 use_task_queue_(
192 !field_trial::IsDisabled("WebRTC-Video-DecodeOnTaskQueue")),
tommic8ece432017-06-20 02:44:38 -0700193 decode_thread_(&DecodeThreadFunction,
194 this,
195 "DecodingThread",
196 rtc::kHighestPriority),
Peter Boström1d04ac62016-02-05 11:25:46 +0100197 call_stats_(call_stats),
Danil Chapovalov8ce0d2b2018-11-23 11:03:25 +0100198 stats_proxy_(&config_, clock_),
199 rtp_receive_statistics_(
200 ReceiveStatistics::Create(clock_, &stats_proxy_, &stats_proxy_)),
Ruslan Burakov493a6502019-02-27 15:32:48 +0100201 timing_(timing),
Niels Möllerdb64d992019-03-29 14:30:53 +0100202 video_receiver_(clock_, timing_.get()),
Sebastian Jansson8026d602019-03-04 19:39:01 +0100203 rtp_video_stream_receiver_(clock_,
204 &transport_adapter_,
Tommi38c5d932018-03-27 23:11:09 +0200205 call_stats,
nisseb1f2ff92017-06-09 04:01:55 -0700206 packet_router,
207 &config_,
nisseca5706d2017-09-11 02:32:16 -0700208 rtp_receive_statistics_.get(),
nisseb1f2ff92017-06-09 04:01:55 -0700209 &stats_proxy_,
210 process_thread_,
Benjamin Wright192eeec2018-10-17 17:27:25 -0700211 this, // NackSender
212 this, // KeyFrameRequestSender
213 this, // OnCompleteFrameCallback
214 config_.frame_decryptor),
Rasmus Brandt3dde4502019-03-21 11:46:17 +0100215 rtp_stream_sync_(this),
216 max_wait_for_keyframe_ms_(KeyframeIntervalSettings::ParseFromFieldTrials()
217 .MaxWaitForKeyframeMs()
218 .value_or(kMaxWaitForKeyFrameMs)),
219 max_wait_for_frame_ms_(KeyframeIntervalSettings::ParseFromFieldTrials()
220 .MaxWaitForFrameMs()
Sebastian Jansson13943b72019-04-02 15:08:14 +0200221 .value_or(kMaxWaitForFrameMs)),
222 decode_queue_(task_queue_factory_->CreateTaskQueue(
223 "DecodingQueue",
224 TaskQueueFactory::Priority::HIGH)) {
Mirko Bonadei675513b2017-11-09 11:09:25 +0100225 RTC_LOG(LS_INFO) << "VideoReceiveStream: " << config_.ToString();
pbos@webrtc.org29d58392013-05-16 12:08:03 +0000226
Rasmus Brandt1e27fec2019-01-23 09:47:50 +0100227 RTC_DCHECK(config_.renderer);
Stefan Holmer58c664c2016-02-08 14:31:30 +0100228 RTC_DCHECK(process_thread_);
Stefan Holmer58c664c2016-02-08 14:31:30 +0100229 RTC_DCHECK(call_stats_);
mflodmancfc8e3b2016-05-03 21:22:04 -0700230
eladalona28122f2017-08-18 04:02:48 -0700231 module_process_sequence_checker_.Detach();
Ruslan Burakov493a6502019-02-27 15:32:48 +0100232 network_sequence_checker_.Detach();
solenberg3ebbcb52017-01-31 03:58:40 -0800233
henrikg91d6ede2015-09-17 00:24:34 -0700234 RTC_DCHECK(!config_.decoders.empty());
Peter Boström521af4e2015-11-27 16:35:04 +0100235 std::set<int> decoder_payload_types;
Peter Boströmb1ae3a42016-02-15 17:52:40 +0100236 for (const Decoder& decoder : config_.decoders) {
Niels Möllercbcbc222018-09-28 09:07:24 +0200237 RTC_CHECK(decoder.decoder_factory);
Peter Boström521af4e2015-11-27 16:35:04 +0100238 RTC_CHECK(decoder_payload_types.find(decoder.payload_type) ==
239 decoder_payload_types.end())
240 << "Duplicate payload type (" << decoder.payload_type
241 << ") for different decoders.";
242 decoder_payload_types.insert(decoder.payload_type);
pbos@webrtc.org0181b5f2013-09-09 08:26:30 +0000243 }
244
nisseca5706d2017-09-11 02:32:16 -0700245 video_receiver_.SetRenderDelay(config_.render_delay_ms);
Peter Boström1d04ac62016-02-05 11:25:46 +0100246
philipela45102f2017-02-22 05:30:39 -0800247 jitter_estimator_.reset(new VCMJitterEstimator(clock_));
248 frame_buffer_.reset(new video_coding::FrameBuffer(
Sebastian Jansson13943b72019-04-02 15:08:14 +0200249 clock_, use_task_queue_ ? task_queue_factory_ : nullptr,
250 jitter_estimator_.get(), timing_.get(), &stats_proxy_));
philipelfd5a20f2016-11-15 00:57:57 -0800251
tommidea489f2017-03-03 03:20:24 -0800252 process_thread_->RegisterModule(&rtp_stream_sync_, RTC_FROM_HERE);
nisse0f15f922017-06-21 01:05:22 -0700253
Niels Möller46879152019-01-07 15:54:47 +0100254 if (config_.media_transport) {
255 config_.media_transport->SetReceiveVideoSink(this);
256 config_.media_transport->AddRttObserver(this);
Niels Möller5304a322018-08-27 13:27:05 +0200257 } else {
Niels Möller46879152019-01-07 15:54:47 +0100258 // Register with RtpStreamReceiverController.
259 media_receiver_ = receiver_controller->CreateReceiver(
260 config_.rtp.remote_ssrc, &rtp_video_stream_receiver_);
261 if (config_.rtp.rtx_ssrc) {
262 rtx_receive_stream_ = absl::make_unique<RtxReceiveStream>(
263 &rtp_video_stream_receiver_, config.rtp.rtx_associated_payload_types,
264 config_.rtp.remote_ssrc, rtp_receive_statistics_.get());
265 rtx_receiver_ = receiver_controller->CreateReceiver(
266 config_.rtp.rtx_ssrc, rtx_receive_stream_.get());
267 } else {
268 rtp_receive_statistics_->EnableRetransmitDetection(config.rtp.remote_ssrc,
269 true);
270 }
nisse0f15f922017-06-21 01:05:22 -0700271 }
pbos@webrtc.org29d58392013-05-16 12:08:03 +0000272}
273
Ruslan Burakov493a6502019-02-27 15:32:48 +0100274VideoReceiveStream::VideoReceiveStream(
Sebastian Jansson74682c12019-03-01 11:50:20 +0100275 TaskQueueFactory* task_queue_factory,
Ruslan Burakov493a6502019-02-27 15:32:48 +0100276 RtpStreamReceiverControllerInterface* receiver_controller,
277 int num_cpu_cores,
278 PacketRouter* packet_router,
279 VideoReceiveStream::Config config,
280 ProcessThread* process_thread,
Sebastian Jansson8026d602019-03-04 19:39:01 +0100281 CallStats* call_stats,
282 Clock* clock)
Sebastian Jansson74682c12019-03-01 11:50:20 +0100283 : VideoReceiveStream(task_queue_factory,
284 receiver_controller,
Ruslan Burakov493a6502019-02-27 15:32:48 +0100285 num_cpu_cores,
286 packet_router,
287 std::move(config),
288 process_thread,
289 call_stats,
Sebastian Jansson8026d602019-03-04 19:39:01 +0100290 clock,
291 new VCMTiming(clock)) {}
Ruslan Burakov493a6502019-02-27 15:32:48 +0100292
pbos@webrtc.org29d58392013-05-16 12:08:03 +0000293VideoReceiveStream::~VideoReceiveStream() {
eladalona28122f2017-08-18 04:02:48 -0700294 RTC_DCHECK_CALLED_SEQUENTIALLY(&worker_sequence_checker_);
Mirko Bonadei675513b2017-11-09 11:09:25 +0100295 RTC_LOG(LS_INFO) << "~VideoReceiveStream: " << config_.ToString();
Peter Boströmca835252016-02-11 15:59:46 +0100296 Stop();
Niels Möller46879152019-01-07 15:54:47 +0100297 if (config_.media_transport) {
298 config_.media_transport->SetReceiveVideoSink(nullptr);
299 config_.media_transport->RemoveRttObserver(this);
300 }
mflodman4cd27902016-08-05 06:28:45 -0700301 process_thread_->DeRegisterModule(&rtp_stream_sync_);
pbos@webrtc.org29d58392013-05-16 12:08:03 +0000302}
303
pbos1ba8d392016-05-01 20:18:34 -0700304void VideoReceiveStream::SignalNetworkState(NetworkState state) {
eladalona28122f2017-08-18 04:02:48 -0700305 RTC_DCHECK_CALLED_SEQUENTIALLY(&worker_sequence_checker_);
nisseb1f2ff92017-06-09 04:01:55 -0700306 rtp_video_stream_receiver_.SignalNetworkState(state);
pbos1ba8d392016-05-01 20:18:34 -0700307}
308
pbos1ba8d392016-05-01 20:18:34 -0700309bool VideoReceiveStream::DeliverRtcp(const uint8_t* packet, size_t length) {
nisseb1f2ff92017-06-09 04:01:55 -0700310 return rtp_video_stream_receiver_.DeliverRtcp(packet, length);
pbos1ba8d392016-05-01 20:18:34 -0700311}
312
solenberg3ebbcb52017-01-31 03:58:40 -0800313void VideoReceiveStream::SetSync(Syncable* audio_syncable) {
eladalona28122f2017-08-18 04:02:48 -0700314 RTC_DCHECK_CALLED_SEQUENTIALLY(&worker_sequence_checker_);
solenberg3ebbcb52017-01-31 03:58:40 -0800315 rtp_stream_sync_.ConfigureSync(audio_syncable);
brandtr090c9402017-01-25 08:28:02 -0800316}
317
pbos@webrtc.orga5c8d2c2014-04-24 11:13:21 +0000318void VideoReceiveStream::Start() {
eladalona28122f2017-08-18 04:02:48 -0700319 RTC_DCHECK_CALLED_SEQUENTIALLY(&worker_sequence_checker_);
brandtrfb45c6c2017-01-27 06:47:55 -0800320
Sebastian Jansson13943b72019-04-02 15:08:14 +0200321 if (decoder_running_) {
Rasmus Brandt1e27fec2019-01-23 09:47:50 +0100322 return;
323 }
324
325 const bool protected_by_fec = config_.rtp.protected_by_flexfec ||
326 rtp_video_stream_receiver_.IsUlpfecEnabled();
brandtrfb45c6c2017-01-27 06:47:55 -0800327
philipela45102f2017-02-22 05:30:39 -0800328 frame_buffer_->Start();
philipelfd5a20f2016-11-15 00:57:57 -0800329
nisseb1f2ff92017-06-09 04:01:55 -0700330 if (rtp_video_stream_receiver_.IsRetransmissionsEnabled() &&
331 protected_by_fec) {
philipela45102f2017-02-22 05:30:39 -0800332 frame_buffer_->SetProtectionMode(kProtectionNackFEC);
philipelfd5a20f2016-11-15 00:57:57 -0800333 }
philipela45102f2017-02-22 05:30:39 -0800334
sprang@webrtc.orgd9b95602014-01-27 13:03:02 +0000335 transport_adapter_.Enable();
tommi2e82f382016-06-21 00:26:43 -0700336 rtc::VideoSinkInterface<VideoFrame>* renderer = nullptr;
Rasmus Brandt1e27fec2019-01-23 09:47:50 +0100337 if (config_.enable_prerenderer_smoothing) {
Sebastian Jansson74682c12019-03-01 11:50:20 +0100338 incoming_video_stream_.reset(new IncomingVideoStream(
339 task_queue_factory_, config_.render_delay_ms, this));
Rasmus Brandt1e27fec2019-01-23 09:47:50 +0100340 renderer = incoming_video_stream_.get();
341 } else {
342 renderer = this;
tommi2e82f382016-06-21 00:26:43 -0700343 }
344
sprang113bdca2016-10-11 03:10:10 -0700345 for (const Decoder& decoder : config_.decoders) {
Niels Möllercbcbc222018-09-28 09:07:24 +0200346 std::unique_ptr<VideoDecoder> video_decoder =
347 decoder.decoder_factory->LegacyCreateVideoDecoder(decoder.video_format,
348 config_.stream_id);
349 // If we still have no valid decoder, we have to create a "Null" decoder
350 // that ignores all calls. The reason we can get into this state is that the
351 // old decoder factory interface doesn't have a way to query supported
352 // codecs.
353 if (!video_decoder) {
354 video_decoder = absl::make_unique<NullVideoDecoder>();
355 }
Erik Språng96965ae2018-10-23 15:42:37 +0200356
357 std::string decoded_output_file =
358 field_trial::FindFullName("WebRTC-DecoderDataDumpDirectory");
Ilya Nikolaevskiyc0321092019-03-11 11:18:26 +0100359 // Because '/' can't be used inside a field trial parameter, we use ':'
360 // instead.
Steve Antonbd631a02019-03-28 10:51:27 -0700361 absl::c_replace(decoded_output_file, ':', '/');
Erik Språng96965ae2018-10-23 15:42:37 +0200362 if (!decoded_output_file.empty()) {
363 char filename_buffer[256];
364 rtc::SimpleStringBuilder ssb(filename_buffer);
365 ssb << decoded_output_file << "/webrtc_receive_stream_"
Ilya Nikolaevskiyc0321092019-03-11 11:18:26 +0100366 << this->config_.rtp.remote_ssrc << "-" << rtc::TimeMicros()
367 << ".ivf";
Erik Språng96965ae2018-10-23 15:42:37 +0200368 video_decoder = absl::make_unique<FrameDumpingDecoder>(
Niels Möllerb7edf692019-02-08 16:40:53 +0100369 std::move(video_decoder), FileWrapper::OpenWriteOnly(ssb.str()));
Erik Språng96965ae2018-10-23 15:42:37 +0200370 }
371
Niels Möllercbcbc222018-09-28 09:07:24 +0200372 video_decoders_.push_back(std::move(video_decoder));
373
374 video_receiver_.RegisterExternalDecoder(video_decoders_.back().get(),
sprang113bdca2016-10-11 03:10:10 -0700375 decoder.payload_type);
sprang113bdca2016-10-11 03:10:10 -0700376 VideoCodec codec = CreateDecoderVideoCodec(decoder);
Niels Möllercb7e1d22018-09-11 15:56:04 +0200377 rtp_video_stream_receiver_.AddReceiveCodec(codec,
378 decoder.video_format.parameters);
sprang113bdca2016-10-11 03:10:10 -0700379 RTC_CHECK_EQ(VCM_OK, video_receiver_.RegisterReceiveCodec(
380 &codec, num_cpu_cores_, false));
381 }
382
Rasmus Brandt1e27fec2019-01-23 09:47:50 +0100383 RTC_DCHECK(renderer != nullptr);
tommi2e82f382016-06-21 00:26:43 -0700384 video_stream_decoder_.reset(new VideoStreamDecoder(
nisseb1f2ff92017-06-09 04:01:55 -0700385 &video_receiver_, &rtp_video_stream_receiver_,
nisseb1f2ff92017-06-09 04:01:55 -0700386 rtp_video_stream_receiver_.IsRetransmissionsEnabled(), protected_by_fec,
nisse76bc8e82017-02-07 09:37:41 -0800387 &stats_proxy_, renderer));
tommic8ece432017-06-20 02:44:38 -0700388
Tommief3e28a2018-03-30 08:42:39 +0200389 // Make sure we register as a stats observer *after* we've prepared the
390 // |video_stream_decoder_|.
391 call_stats_->RegisterStatsObserver(this);
392
Niels Möllerad31c982019-03-14 11:20:30 +0100393 // NOTE: *Not* registering video_receiver_ on process_thread_. Its Process
394 // method does nothing that is useful for us, since we no longer use the old
395 // jitter buffer.
tommic8ece432017-06-20 02:44:38 -0700396
Peter Boströmca835252016-02-11 15:59:46 +0100397 // Start the decode thread
Tommifbf3bce2018-02-21 15:56:05 +0100398 video_receiver_.DecoderThreadStarting();
Tommi132e28e2018-02-24 17:57:33 +0100399 stats_proxy_.DecoderThreadStarting();
Sebastian Jansson13943b72019-04-02 15:08:14 +0200400 if (!use_task_queue_) {
401 decode_thread_.Start();
402 } else {
403 decode_queue_.PostTask([this] {
404 RTC_DCHECK_RUN_ON(&decode_queue_);
405 RTC_DCHECK(decoder_stopped_);
406 decoder_stopped_ = false;
407 StartNextDecode();
408 });
409 }
410 decoder_running_ = true;
nisseb1f2ff92017-06-09 04:01:55 -0700411 rtp_video_stream_receiver_.StartReceive();
pbos@webrtc.org29d58392013-05-16 12:08:03 +0000412}
413
pbos@webrtc.orga5c8d2c2014-04-24 11:13:21 +0000414void VideoReceiveStream::Stop() {
eladalona28122f2017-08-18 04:02:48 -0700415 RTC_DCHECK_CALLED_SEQUENTIALLY(&worker_sequence_checker_);
nisseb1f2ff92017-06-09 04:01:55 -0700416 rtp_video_stream_receiver_.StopReceive();
philipelfd5a20f2016-11-15 00:57:57 -0800417
Ilya Nikolaevskiyd397a0d2018-02-21 15:57:09 +0100418 stats_proxy_.OnUniqueFramesCounted(
419 rtp_video_stream_receiver_.GetUniqueFramesSeen());
420
philipela45102f2017-02-22 05:30:39 -0800421 frame_buffer_->Stop();
philipele21be1d2017-09-25 06:37:12 -0700422 call_stats_->DeregisterStatsObserver(this);
philipelfd5a20f2016-11-15 00:57:57 -0800423
Sebastian Jansson13943b72019-04-02 15:08:14 +0200424 if (decoder_running_) {
tommic8ece432017-06-20 02:44:38 -0700425 // TriggerDecoderShutdown will release any waiting decoder thread and make
426 // it stop immediately, instead of waiting for a timeout. Needs to be called
427 // before joining the decoder thread.
428 video_receiver_.TriggerDecoderShutdown();
Sebastian Jansson13943b72019-04-02 15:08:14 +0200429 if (!use_task_queue_) {
430 decode_thread_.Stop();
431 } else {
432 rtc::Event done;
433 decode_queue_.PostTask([this, &done] {
434 RTC_DCHECK_RUN_ON(&decode_queue_);
435 decoder_stopped_ = true;
436 done.Set();
437 });
438 done.Wait(rtc::Event::kForever);
439 }
440 decoder_running_ = false;
tommic8ece432017-06-20 02:44:38 -0700441
Tommifbf3bce2018-02-21 15:56:05 +0100442 video_receiver_.DecoderThreadStopped();
Tommi132e28e2018-02-24 17:57:33 +0100443 stats_proxy_.DecoderThreadStopped();
sprang0d348d62016-10-07 08:28:39 -0700444 // Deregister external decoders so they are no longer running during
445 // destruction. This effectively stops the VCM since the decoder thread is
446 // stopped, the VCM is deregistered and no asynchronous decoder threads are
447 // running.
448 for (const Decoder& decoder : config_.decoders)
449 video_receiver_.RegisterExternalDecoder(nullptr, decoder.payload_type);
450 }
philipelfd5a20f2016-11-15 00:57:57 -0800451
tommi2e82f382016-06-21 00:26:43 -0700452 video_stream_decoder_.reset();
453 incoming_video_stream_.reset();
sprang@webrtc.orgd9b95602014-01-27 13:03:02 +0000454 transport_adapter_.Disable();
pbos@webrtc.org29d58392013-05-16 12:08:03 +0000455}
456
sprang@webrtc.org9510e532014-02-07 15:32:45 +0000457VideoReceiveStream::Stats VideoReceiveStream::GetStats() const {
Peter Boströmf751bf82016-02-05 14:00:53 +0100458 return stats_proxy_.GetStats();
sprang@webrtc.org09315702014-02-07 12:06:29 +0000459}
460
eladalonc0d481a2017-08-02 07:39:07 -0700461void VideoReceiveStream::AddSecondarySink(RtpPacketSinkInterface* sink) {
462 rtp_video_stream_receiver_.AddSecondarySink(sink);
463}
464
465void VideoReceiveStream::RemoveSecondarySink(
466 const RtpPacketSinkInterface* sink) {
467 rtp_video_stream_receiver_.RemoveSecondarySink(sink);
468}
469
Ruslan Burakov493a6502019-02-27 15:32:48 +0100470bool VideoReceiveStream::SetBaseMinimumPlayoutDelayMs(int delay_ms) {
471 RTC_DCHECK_CALLED_SEQUENTIALLY(&worker_sequence_checker_);
472 if (delay_ms < kMinBaseMinimumDelayMs || delay_ms > kMaxBaseMinimumDelayMs) {
473 return false;
474 }
475
476 rtc::CritScope cs(&playout_delay_lock_);
477 base_minimum_playout_delay_ms_ = delay_ms;
478 UpdatePlayoutDelays();
479 return true;
480}
481
482int VideoReceiveStream::GetBaseMinimumPlayoutDelayMs() const {
483 RTC_DCHECK_CALLED_SEQUENTIALLY(&worker_sequence_checker_);
484
485 rtc::CritScope cs(&playout_delay_lock_);
486 return base_minimum_playout_delay_ms_;
487}
488
tommi2e82f382016-06-21 00:26:43 -0700489// TODO(tommi): This method grabs a lock 6 times.
Tommibd3380f2016-06-10 17:38:17 +0200490void VideoReceiveStream::OnFrame(const VideoFrame& video_frame) {
asaperssonf8cdd182016-03-15 01:00:47 -0700491 int64_t sync_offset_ms;
asaperssonde9e5ff2016-11-02 07:14:03 -0700492 double estimated_freq_khz;
tommi2e82f382016-06-21 00:26:43 -0700493 // TODO(tommi): GetStreamSyncOffsetInMs grabs three locks. One inside the
494 // function itself, another in GetChannel() and a third in
495 // GetPlayoutTimestamp. Seems excessive. Anyhow, I'm assuming the function
496 // succeeds most of the time, which leads to grabbing a fourth lock.
Yves Gerey665174f2018-06-19 15:03:05 +0200497 if (rtp_stream_sync_.GetStreamSyncOffsetInMs(
498 video_frame.timestamp(), video_frame.render_time_ms(),
499 &sync_offset_ms, &estimated_freq_khz)) {
tommi2e82f382016-06-21 00:26:43 -0700500 // TODO(tommi): OnSyncOffsetUpdated grabs a lock.
asaperssonde9e5ff2016-11-02 07:14:03 -0700501 stats_proxy_.OnSyncOffsetUpdated(sync_offset_ms, estimated_freq_khz);
tommi2e82f382016-06-21 00:26:43 -0700502 }
tommi2e82f382016-06-21 00:26:43 -0700503 config_.renderer->OnFrame(video_frame);
pbos@webrtc.org29d58392013-05-16 12:08:03 +0000504
tommi2e82f382016-06-21 00:26:43 -0700505 // TODO(tommi): OnRenderFrame grabs a lock too.
asapersson1490f7a2016-09-23 02:09:46 -0700506 stats_proxy_.OnRenderedFrame(video_frame);
pbos@webrtc.org29d58392013-05-16 12:08:03 +0000507}
pbos@webrtc.org26c0c412014-09-03 16:17:12 +0000508
brandtr090c9402017-01-25 08:28:02 -0800509void VideoReceiveStream::SendNack(
510 const std::vector<uint16_t>& sequence_numbers) {
nisseb1f2ff92017-06-09 04:01:55 -0700511 rtp_video_stream_receiver_.RequestPacketRetransmit(sequence_numbers);
brandtr090c9402017-01-25 08:28:02 -0800512}
513
514void VideoReceiveStream::RequestKeyFrame() {
Niels Möller46879152019-01-07 15:54:47 +0100515 if (config_.media_transport) {
516 config_.media_transport->RequestKeyFrame(config_.rtp.remote_ssrc);
517 } else {
518 rtp_video_stream_receiver_.RequestKeyFrame();
519 }
brandtr090c9402017-01-25 08:28:02 -0800520}
521
522void VideoReceiveStream::OnCompleteFrame(
philipele7c891f2018-02-22 14:35:06 +0100523 std::unique_ptr<video_coding::EncodedFrame> frame) {
Ruslan Burakov493a6502019-02-27 15:32:48 +0100524 RTC_DCHECK_CALLED_SEQUENTIALLY(&network_sequence_checker_);
Ilya Nikolaevskiye6a2d942018-11-07 14:32:28 +0100525 // TODO(https://bugs.webrtc.org/9974): Consider removing this workaround.
526 int64_t time_now_ms = rtc::TimeMillis();
527 if (last_complete_frame_time_ms_ > 0 &&
528 time_now_ms - last_complete_frame_time_ms_ > kInactiveStreamThresholdMs) {
529 frame_buffer_->Clear();
530 }
531 last_complete_frame_time_ms_ = time_now_ms;
532
Ruslan Burakov493a6502019-02-27 15:32:48 +0100533 const PlayoutDelay& playout_delay = frame->EncodedImage().playout_delay_;
534 if (playout_delay.min_ms >= 0) {
535 rtc::CritScope cs(&playout_delay_lock_);
536 frame_minimum_playout_delay_ms_ = playout_delay.min_ms;
537 UpdatePlayoutDelays();
538 }
539
540 if (playout_delay.max_ms >= 0) {
541 rtc::CritScope cs(&playout_delay_lock_);
542 frame_maximum_playout_delay_ms_ = playout_delay.max_ms;
543 UpdatePlayoutDelays();
544 }
Sebastian Jansson13943b72019-04-02 15:08:14 +0200545 if (!use_task_queue_) {
546 int64_t last_continuous_pid = frame_buffer_->InsertFrame(std::move(frame));
547 if (last_continuous_pid != -1)
548 rtp_video_stream_receiver_.FrameContinuous(last_continuous_pid);
549 } else {
550 frame_buffer_->InsertFrame(
551 std::move(frame), [this](int64_t last_continuous_pid) {
552 if (last_continuous_pid != -1)
553 rtp_video_stream_receiver_.FrameContinuous(last_continuous_pid);
554 });
555 }
brandtr090c9402017-01-25 08:28:02 -0800556}
557
Niels Möller46879152019-01-07 15:54:47 +0100558void VideoReceiveStream::OnData(uint64_t channel_id,
559 MediaTransportEncodedVideoFrame frame) {
560 OnCompleteFrame(
561 absl::make_unique<EncodedFrameForMediaTransport>(std::move(frame)));
562}
563
philipele21be1d2017-09-25 06:37:12 -0700564void VideoReceiveStream::OnRttUpdate(int64_t avg_rtt_ms, int64_t max_rtt_ms) {
Tommi81de14f2018-03-25 22:19:25 +0200565 RTC_DCHECK_CALLED_SEQUENTIALLY(&module_process_sequence_checker_);
philipele21be1d2017-09-25 06:37:12 -0700566 frame_buffer_->UpdateRtt(max_rtt_ms);
Tommi81de14f2018-03-25 22:19:25 +0200567 rtp_video_stream_receiver_.UpdateRtt(max_rtt_ms);
philipele21be1d2017-09-25 06:37:12 -0700568}
569
Niels Möller46879152019-01-07 15:54:47 +0100570void VideoReceiveStream::OnRttUpdated(int64_t rtt_ms) {
571 frame_buffer_->UpdateRtt(rtt_ms);
572}
573
solenberg3ebbcb52017-01-31 03:58:40 -0800574int VideoReceiveStream::id() const {
eladalona28122f2017-08-18 04:02:48 -0700575 RTC_DCHECK_CALLED_SEQUENTIALLY(&worker_sequence_checker_);
solenberg3ebbcb52017-01-31 03:58:40 -0800576 return config_.rtp.remote_ssrc;
577}
578
Danil Chapovalovb9b146c2018-06-15 12:28:07 +0200579absl::optional<Syncable::Info> VideoReceiveStream::GetInfo() const {
eladalona28122f2017-08-18 04:02:48 -0700580 RTC_DCHECK_CALLED_SEQUENTIALLY(&module_process_sequence_checker_);
Niels Möllerdf9e9ae2018-07-31 08:29:53 +0200581 absl::optional<Syncable::Info> info =
582 rtp_video_stream_receiver_.GetSyncInfo();
solenberg3ebbcb52017-01-31 03:58:40 -0800583
Niels Möllerdf9e9ae2018-07-31 08:29:53 +0200584 if (!info)
Danil Chapovalovb9b146c2018-06-15 12:28:07 +0200585 return absl::nullopt;
solenberg3ebbcb52017-01-31 03:58:40 -0800586
Niels Möllerdf9e9ae2018-07-31 08:29:53 +0200587 info->current_delay_ms = video_receiver_.Delay();
Oskar Sundbom8e07c132018-01-08 16:45:42 +0100588 return info;
solenberg3ebbcb52017-01-31 03:58:40 -0800589}
590
591uint32_t VideoReceiveStream::GetPlayoutTimestamp() const {
592 RTC_NOTREACHED();
593 return 0;
594}
595
596void VideoReceiveStream::SetMinimumPlayoutDelay(int delay_ms) {
eladalona28122f2017-08-18 04:02:48 -0700597 RTC_DCHECK_CALLED_SEQUENTIALLY(&module_process_sequence_checker_);
Ruslan Burakov493a6502019-02-27 15:32:48 +0100598 rtc::CritScope cs(&playout_delay_lock_);
599 syncable_minimum_playout_delay_ms_ = delay_ms;
600 UpdatePlayoutDelays();
solenberg3ebbcb52017-01-31 03:58:40 -0800601}
602
Sebastian Jansson13943b72019-04-02 15:08:14 +0200603int64_t VideoReceiveStream::GetWaitMs() const {
604 return keyframe_required_ ? max_wait_for_keyframe_ms_
605 : max_wait_for_frame_ms_;
606}
607
608void VideoReceiveStream::StartNextDecode() {
609 RTC_DCHECK(use_task_queue_);
610 TRACE_EVENT0("webrtc", "VideoReceiveStream::StartNextDecode");
611
612 struct DecodeTask {
613 void operator()() {
614 RTC_DCHECK_RUN_ON(&stream->decode_queue_);
615 if (stream->decoder_stopped_)
616 return;
617 if (frame) {
618 stream->HandleEncodedFrame(std::move(frame));
619 } else {
620 stream->HandleFrameBufferTimeout();
621 }
622 }
623 VideoReceiveStream* stream;
624 std::unique_ptr<EncodedFrame> frame;
625 };
626
627 // TODO(philipel): Call NextFrame with |keyframe_required| argument set when
628 // downstream project has been fixed.
629 frame_buffer_->NextFrame(
630 GetWaitMs(), /*keyframe_required*/ false,
631 [this](std::unique_ptr<EncodedFrame> frame, ReturnReason res) {
632 RTC_DCHECK_EQ(frame == nullptr, res == ReturnReason::kTimeout);
633 RTC_DCHECK_EQ(frame != nullptr, res == ReturnReason::kFrameFound);
634 decode_queue_.PostTask(DecodeTask{this, std::move(frame)});
635 // Start the next decode after a delay or when the previous decode is
636 // finished (as it will be blocked by the queue).
637 constexpr int kMinDecodeIntervalMs = 1;
638 decode_queue_.PostDelayedTask(
639 [this] {
640 RTC_DCHECK_RUN_ON(&decode_queue_);
641 if (!decoder_stopped_)
642 StartNextDecode();
643 },
644 kMinDecodeIntervalMs);
645 });
646}
647
tommic8ece432017-06-20 02:44:38 -0700648void VideoReceiveStream::DecodeThreadFunction(void* ptr) {
Karl Wibergab036382019-03-12 18:01:51 +0100649 ScopedRegisterThreadForDebugging thread_dbg(RTC_FROM_HERE);
tommic8ece432017-06-20 02:44:38 -0700650 while (static_cast<VideoReceiveStream*>(ptr)->Decode()) {
651 }
Peter Boströmca835252016-02-11 15:59:46 +0100652}
653
philipel2dfea3e2017-02-28 07:19:43 -0800654bool VideoReceiveStream::Decode() {
Sebastian Jansson13943b72019-04-02 15:08:14 +0200655 RTC_DCHECK(!use_task_queue_);
tommidb23ea62017-03-03 07:21:18 -0800656 TRACE_EVENT0("webrtc", "VideoReceiveStream::Decode");
philipel3042c2d2017-08-18 04:55:02 -0700657
philipele7c891f2018-02-22 14:35:06 +0100658 std::unique_ptr<video_coding::EncodedFrame> frame;
philipel3042c2d2017-08-18 04:55:02 -0700659 // TODO(philipel): Call NextFrame with |keyframe_required| argument when
660 // downstream project has been fixed.
guidouc3372582017-04-04 07:16:21 -0700661 video_coding::FrameBuffer::ReturnReason res =
Sebastian Jansson13943b72019-04-02 15:08:14 +0200662 frame_buffer_->NextFrame(GetWaitMs(), &frame);
663 if (res == ReturnReason::kStopped) {
philipel2dfea3e2017-02-28 07:19:43 -0800664 return false;
guidouc3372582017-04-04 07:16:21 -0700665 }
philipela45102f2017-02-22 05:30:39 -0800666 if (frame) {
Sebastian Jansson13943b72019-04-02 15:08:14 +0200667 RTC_DCHECK_EQ(res, ReturnReason::kFrameFound);
668 HandleEncodedFrame(std::move(frame));
philipelfd5a20f2016-11-15 00:57:57 -0800669 } else {
Sebastian Jansson13943b72019-04-02 15:08:14 +0200670 RTC_DCHECK_EQ(res, ReturnReason::kTimeout);
671 HandleFrameBufferTimeout();
philipelfd5a20f2016-11-15 00:57:57 -0800672 }
philipel2dfea3e2017-02-28 07:19:43 -0800673 return true;
Peter Boströmca835252016-02-11 15:59:46 +0100674}
Jonas Oreland49ac5952018-09-26 16:04:32 +0200675
Sebastian Jansson13943b72019-04-02 15:08:14 +0200676void VideoReceiveStream::HandleEncodedFrame(
677 std::unique_ptr<EncodedFrame> frame) {
678 int64_t now_ms = clock_->TimeInMilliseconds();
679
680 // Current OnPreDecode only cares about QP for VP8.
681 int qp = -1;
682 if (frame->CodecSpecific()->codecType == kVideoCodecVP8) {
683 if (!vp8::GetQp(frame->data(), frame->size(), &qp)) {
684 RTC_LOG(LS_WARNING) << "Failed to extract QP from VP8 video frame";
685 }
686 }
687 stats_proxy_.OnPreDecode(frame->CodecSpecific()->codecType, qp);
688
689 int decode_result = video_receiver_.Decode(frame.get());
690 if (decode_result == WEBRTC_VIDEO_CODEC_OK ||
691 decode_result == WEBRTC_VIDEO_CODEC_OK_REQUEST_KEYFRAME) {
692 keyframe_required_ = false;
693 frame_decoded_ = true;
694 rtp_video_stream_receiver_.FrameDecoded(frame->id.picture_id);
695
696 if (decode_result == WEBRTC_VIDEO_CODEC_OK_REQUEST_KEYFRAME)
697 RequestKeyFrame();
698 } else if (!frame_decoded_ || !keyframe_required_ ||
699 (last_keyframe_request_ms_ + max_wait_for_keyframe_ms_ < now_ms)) {
700 keyframe_required_ = true;
701 // TODO(philipel): Remove this keyframe request when downstream project
702 // has been fixed.
703 RequestKeyFrame();
704 last_keyframe_request_ms_ = now_ms;
705 }
706}
707
708void VideoReceiveStream::HandleFrameBufferTimeout() {
709 int64_t now_ms = clock_->TimeInMilliseconds();
710 absl::optional<int64_t> last_packet_ms =
711 rtp_video_stream_receiver_.LastReceivedPacketMs();
712 absl::optional<int64_t> last_keyframe_packet_ms =
713 rtp_video_stream_receiver_.LastReceivedKeyframePacketMs();
714
715 // To avoid spamming keyframe requests for a stream that is not active we
716 // check if we have received a packet within the last 5 seconds.
717 bool stream_is_active = last_packet_ms && now_ms - *last_packet_ms < 5000;
718 if (!stream_is_active)
719 stats_proxy_.OnStreamInactive();
720
721 // If we recently have been receiving packets belonging to a keyframe then
722 // we assume a keyframe is currently being received.
723 bool receiving_keyframe =
724 last_keyframe_packet_ms &&
725 now_ms - *last_keyframe_packet_ms < max_wait_for_keyframe_ms_;
726
727 if (stream_is_active && !receiving_keyframe &&
728 (!config_.crypto_options.sframe.require_frame_encryption ||
729 rtp_video_stream_receiver_.IsDecryptable())) {
730 RTC_LOG(LS_WARNING) << "No decodable frame in " << GetWaitMs()
731 << " ms, requesting keyframe.";
732 RequestKeyFrame();
733 }
734}
735
Ruslan Burakov493a6502019-02-27 15:32:48 +0100736void VideoReceiveStream::UpdatePlayoutDelays() const {
737 const int minimum_delay_ms =
738 std::max({frame_minimum_playout_delay_ms_, base_minimum_playout_delay_ms_,
739 syncable_minimum_playout_delay_ms_});
740 if (minimum_delay_ms >= 0) {
741 timing_->set_min_playout_delay(minimum_delay_ms);
742 }
743
744 const int maximum_delay_ms = frame_maximum_playout_delay_ms_;
745 if (maximum_delay_ms >= 0) {
746 timing_->set_max_playout_delay(maximum_delay_ms);
747 }
748}
749
Jonas Oreland49ac5952018-09-26 16:04:32 +0200750std::vector<webrtc::RtpSource> VideoReceiveStream::GetSources() const {
751 return rtp_video_stream_receiver_.GetSources();
752}
753
mflodman@webrtc.orgf3973e82013-12-13 09:40:45 +0000754} // namespace internal
755} // namespace webrtc