blob: 99b83c9855e2a75e5de024798961dcda129e559f [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/timing.h"
Niels Möller147013a2018-10-01 15:56:33 +020039#include "modules/video_coding/utility/vp8_header_parser.h"
Mirko Bonadei92ea95e2017-09-15 06:47:31 +020040#include "rtc_base/checks.h"
Rasmus Brandt3dde4502019-03-21 11:46:17 +010041#include "rtc_base/experiments/keyframe_interval_settings.h"
Mirko Bonadei92ea95e2017-09-15 06:47:31 +020042#include "rtc_base/location.h"
43#include "rtc_base/logging.h"
Erik Språng96965ae2018-10-23 15:42:37 +020044#include "rtc_base/platform_file.h"
45#include "rtc_base/strings/string_builder.h"
Karl Wibergab036382019-03-12 18:01:51 +010046#include "rtc_base/system/thread_registry.h"
Steve Anton10542f22019-01-11 09:11:00 -080047#include "rtc_base/time_utils.h"
Mirko Bonadei92ea95e2017-09-15 06:47:31 +020048#include "rtc_base/trace_event.h"
49#include "system_wrappers/include/clock.h"
50#include "system_wrappers/include/field_trial.h"
51#include "video/call_stats.h"
Erik Språng96965ae2018-10-23 15:42:37 +020052#include "video/frame_dumping_decoder.h"
Mirko Bonadei92ea95e2017-09-15 06:47:31 +020053#include "video/receive_statistics_proxy.h"
pbos@webrtc.org29d58392013-05-16 12:08:03 +000054
55namespace webrtc {
mflodmana20de202015-10-18 22:08:19 -070056
pbos@webrtc.org776e6f22014-10-29 15:28:39 +000057namespace {
Sebastian Jansson1c747f52019-04-04 13:01:39 +020058
59using video_coding::EncodedFrame;
60using ReturnReason = video_coding::FrameBuffer::ReturnReason;
61
Ruslan Burakov493a6502019-02-27 15:32:48 +010062constexpr int kMinBaseMinimumDelayMs = 0;
63constexpr int kMaxBaseMinimumDelayMs = 10000;
64
Rasmus Brandt3dde4502019-03-21 11:46:17 +010065constexpr int kMaxWaitForKeyFrameMs = 200;
66constexpr int kMaxWaitForFrameMs = 3000;
67
pbos@webrtc.org776e6f22014-10-29 15:28:39 +000068VideoCodec CreateDecoderVideoCodec(const VideoReceiveStream::Decoder& decoder) {
69 VideoCodec codec;
70 memset(&codec, 0, sizeof(codec));
71
72 codec.plType = decoder.payload_type;
Niels Möllercb7e1d22018-09-11 15:56:04 +020073 codec.codecType = PayloadStringToCodecType(decoder.video_format.name);
pbos@webrtc.org776e6f22014-10-29 15:28:39 +000074
75 if (codec.codecType == kVideoCodecVP8) {
hta257dc392016-10-25 09:05:06 -070076 *(codec.VP8()) = VideoEncoder::GetDefaultVp8Settings();
ivica05cfcd32015-09-07 06:04:16 -070077 } else if (codec.codecType == kVideoCodecVP9) {
hta257dc392016-10-25 09:05:06 -070078 *(codec.VP9()) = VideoEncoder::GetDefaultVp9Settings();
pbos@webrtc.org776e6f22014-10-29 15:28:39 +000079 } else if (codec.codecType == kVideoCodecH264) {
hta257dc392016-10-25 09:05:06 -070080 *(codec.H264()) = VideoEncoder::GetDefaultH264Settings();
Emircan Uysalerd7ae3c32018-01-25 13:01:09 -080081 } else if (codec.codecType == kVideoCodecMultiplex) {
Emircan Uysaler0a375472017-12-11 12:21:02 +053082 VideoReceiveStream::Decoder associated_decoder = decoder;
Niels Möllercb7e1d22018-09-11 15:56:04 +020083 associated_decoder.video_format =
84 SdpVideoFormat(CodecTypeToPayloadString(kVideoCodecVP9));
Emircan Uysaler0a375472017-12-11 12:21:02 +053085 VideoCodec associated_codec = CreateDecoderVideoCodec(associated_decoder);
Emircan Uysalerd7ae3c32018-01-25 13:01:09 -080086 associated_codec.codecType = kVideoCodecMultiplex;
Emircan Uysaler0a375472017-12-11 12:21:02 +053087 return associated_codec;
pbos@webrtc.org776e6f22014-10-29 15:28:39 +000088 }
89
90 codec.width = 320;
91 codec.height = 180;
stefanb4bc65b2016-11-02 10:10:20 -070092 const int kDefaultStartBitrate = 300;
pbos@webrtc.org776e6f22014-10-29 15:28:39 +000093 codec.startBitrate = codec.minBitrate = codec.maxBitrate =
stefanb4bc65b2016-11-02 10:10:20 -070094 kDefaultStartBitrate;
pbos@webrtc.org776e6f22014-10-29 15:28:39 +000095
96 return codec;
97}
Niels Möllercbcbc222018-09-28 09:07:24 +020098
99// Video decoder class to be used for unknown codecs. Doesn't support decoding
100// but logs messages to LS_ERROR.
101class NullVideoDecoder : public webrtc::VideoDecoder {
102 public:
103 int32_t InitDecode(const webrtc::VideoCodec* codec_settings,
104 int32_t number_of_cores) override {
105 RTC_LOG(LS_ERROR) << "Can't initialize NullVideoDecoder.";
106 return WEBRTC_VIDEO_CODEC_OK;
107 }
108
109 int32_t Decode(const webrtc::EncodedImage& input_image,
110 bool missing_frames,
Niels Möllercbcbc222018-09-28 09:07:24 +0200111 int64_t render_time_ms) override {
112 RTC_LOG(LS_ERROR) << "The NullVideoDecoder doesn't support decoding.";
113 return WEBRTC_VIDEO_CODEC_OK;
114 }
115
116 int32_t RegisterDecodeCompleteCallback(
117 webrtc::DecodedImageCallback* callback) override {
118 RTC_LOG(LS_ERROR)
119 << "Can't register decode complete callback on NullVideoDecoder.";
120 return WEBRTC_VIDEO_CODEC_OK;
121 }
122
123 int32_t Release() override { return WEBRTC_VIDEO_CODEC_OK; }
124
125 const char* ImplementationName() const override { return "NullVideoDecoder"; }
126};
127
Niels Möller46879152019-01-07 15:54:47 +0100128// Inherit video_coding::EncodedFrame, which is the class used by
129// video_coding::FrameBuffer and other components in the receive pipeline. It's
130// a subclass of EncodedImage, and it always owns the buffer.
131class EncodedFrameForMediaTransport : public video_coding::EncodedFrame {
132 public:
133 explicit EncodedFrameForMediaTransport(
134 MediaTransportEncodedVideoFrame frame) {
Niels Möller938dd9f2019-02-07 00:02:17 +0100135 // TODO(nisse): This is ugly. We copy the EncodedImage (a base class of
136 // ours, in several steps), to get all the meta data. We should be using
137 // std::move in some way. Then we also need to handle the case of an unowned
138 // buffer, in which case we need to make an owned copy.
Niels Möller46879152019-01-07 15:54:47 +0100139 *static_cast<class EncodedImage*>(this) = frame.encoded_image();
Niels Möller46879152019-01-07 15:54:47 +0100140
Niels Möller69bb3af2019-02-13 10:05:49 +0100141 // If we don't already own the buffer, make a copy.
142 Retain();
Niels Möller46879152019-01-07 15:54:47 +0100143
144 _payloadType = static_cast<uint8_t>(frame.payload_type());
145
146 // TODO(nisse): frame_id and picture_id are probably not the same thing. For
147 // a single layer, this should be good enough.
148 id.picture_id = frame.frame_id();
149 id.spatial_layer = frame.encoded_image().SpatialIndex().value_or(0);
150 num_references = std::min(static_cast<size_t>(kMaxFrameReferences),
151 frame.referenced_frame_ids().size());
152 for (size_t i = 0; i < num_references; i++) {
153 references[i] = frame.referenced_frame_ids()[i];
154 }
155 }
156
157 // TODO(nisse): Implement. Not sure how they are used.
158 int64_t ReceivedTime() const override { return 0; }
159 int64_t RenderTime() const override { return 0; }
160};
161
Ilya Nikolaevskiye6a2d942018-11-07 14:32:28 +0100162// TODO(https://bugs.webrtc.org/9974): Consider removing this workaround.
163// Maximum time between frames before resetting the FrameBuffer to avoid RTP
164// timestamps wraparound to affect FrameBuffer.
165constexpr int kInactiveStreamThresholdMs = 600000; // 10 minutes.
166
pbos@webrtc.org776e6f22014-10-29 15:28:39 +0000167} // namespace
pbos@webrtc.org29d58392013-05-16 12:08:03 +0000168
Peter Boströmca835252016-02-11 15:59:46 +0100169namespace internal {
tommi2e82f382016-06-21 00:26:43 -0700170
nisse0f15f922017-06-21 01:05:22 -0700171VideoReceiveStream::VideoReceiveStream(
Sebastian Jansson74682c12019-03-01 11:50:20 +0100172 TaskQueueFactory* task_queue_factory,
nisse0f15f922017-06-21 01:05:22 -0700173 RtpStreamReceiverControllerInterface* receiver_controller,
174 int num_cpu_cores,
175 PacketRouter* packet_router,
176 VideoReceiveStream::Config config,
177 ProcessThread* process_thread,
Ruslan Burakov493a6502019-02-27 15:32:48 +0100178 CallStats* call_stats,
179 Clock* clock,
180 VCMTiming* timing)
Sebastian Jansson74682c12019-03-01 11:50:20 +0100181 : task_queue_factory_(task_queue_factory),
182 transport_adapter_(config.rtcp_send_transport),
Tommi733b5472016-06-10 17:58:01 +0200183 config_(std::move(config)),
sprang113bdca2016-10-11 03:10:10 -0700184 num_cpu_cores_(num_cpu_cores),
Peter Boström1d04ac62016-02-05 11:25:46 +0100185 process_thread_(process_thread),
Ruslan Burakov493a6502019-02-27 15:32:48 +0100186 clock_(clock),
Sebastian Janssona7e78f22019-04-16 10:14:32 +0200187 use_task_queue_(
188 !field_trial::IsDisabled("WebRTC-Video-DecodeOnTaskQueue")),
tommic8ece432017-06-20 02:44:38 -0700189 decode_thread_(&DecodeThreadFunction,
190 this,
191 "DecodingThread",
192 rtc::kHighestPriority),
Peter Boström1d04ac62016-02-05 11:25:46 +0100193 call_stats_(call_stats),
Danil Chapovalov8ce0d2b2018-11-23 11:03:25 +0100194 stats_proxy_(&config_, clock_),
195 rtp_receive_statistics_(
196 ReceiveStatistics::Create(clock_, &stats_proxy_, &stats_proxy_)),
Ruslan Burakov493a6502019-02-27 15:32:48 +0100197 timing_(timing),
Niels Möllerdb64d992019-03-29 14:30:53 +0100198 video_receiver_(clock_, timing_.get()),
Sebastian Jansson8026d602019-03-04 19:39:01 +0100199 rtp_video_stream_receiver_(clock_,
200 &transport_adapter_,
Tommi38c5d932018-03-27 23:11:09 +0200201 call_stats,
nisseb1f2ff92017-06-09 04:01:55 -0700202 packet_router,
203 &config_,
nisseca5706d2017-09-11 02:32:16 -0700204 rtp_receive_statistics_.get(),
nisseb1f2ff92017-06-09 04:01:55 -0700205 &stats_proxy_,
206 process_thread_,
Niels Möller2f5554d2019-05-29 13:35:14 +0200207 this, // NackSender
208 nullptr, // Use default KeyFrameRequestSender
209 this, // OnCompleteFrameCallback
Benjamin Wright192eeec2018-10-17 17:27:25 -0700210 config_.frame_decryptor),
Rasmus Brandt3dde4502019-03-21 11:46:17 +0100211 rtp_stream_sync_(this),
212 max_wait_for_keyframe_ms_(KeyframeIntervalSettings::ParseFromFieldTrials()
213 .MaxWaitForKeyframeMs()
214 .value_or(kMaxWaitForKeyFrameMs)),
215 max_wait_for_frame_ms_(KeyframeIntervalSettings::ParseFromFieldTrials()
216 .MaxWaitForFrameMs()
Sebastian Jansson11d0d7b2019-04-11 12:39:34 +0200217 .value_or(kMaxWaitForFrameMs)),
218 decode_queue_(task_queue_factory_->CreateTaskQueue(
219 "DecodingQueue",
220 TaskQueueFactory::Priority::HIGH)) {
Mirko Bonadei675513b2017-11-09 11:09:25 +0100221 RTC_LOG(LS_INFO) << "VideoReceiveStream: " << config_.ToString();
pbos@webrtc.org29d58392013-05-16 12:08:03 +0000222
Rasmus Brandt1e27fec2019-01-23 09:47:50 +0100223 RTC_DCHECK(config_.renderer);
Stefan Holmer58c664c2016-02-08 14:31:30 +0100224 RTC_DCHECK(process_thread_);
Stefan Holmer58c664c2016-02-08 14:31:30 +0100225 RTC_DCHECK(call_stats_);
mflodmancfc8e3b2016-05-03 21:22:04 -0700226
eladalona28122f2017-08-18 04:02:48 -0700227 module_process_sequence_checker_.Detach();
Ruslan Burakov493a6502019-02-27 15:32:48 +0100228 network_sequence_checker_.Detach();
solenberg3ebbcb52017-01-31 03:58:40 -0800229
henrikg91d6ede2015-09-17 00:24:34 -0700230 RTC_DCHECK(!config_.decoders.empty());
Peter Boström521af4e2015-11-27 16:35:04 +0100231 std::set<int> decoder_payload_types;
Peter Boströmb1ae3a42016-02-15 17:52:40 +0100232 for (const Decoder& decoder : config_.decoders) {
Niels Möllercbcbc222018-09-28 09:07:24 +0200233 RTC_CHECK(decoder.decoder_factory);
Peter Boström521af4e2015-11-27 16:35:04 +0100234 RTC_CHECK(decoder_payload_types.find(decoder.payload_type) ==
235 decoder_payload_types.end())
236 << "Duplicate payload type (" << decoder.payload_type
237 << ") for different decoders.";
238 decoder_payload_types.insert(decoder.payload_type);
pbos@webrtc.org0181b5f2013-09-09 08:26:30 +0000239 }
240
Niels Möllerbcb7c202019-04-18 12:54:04 +0200241 timing_->set_render_delay(config_.render_delay_ms);
Peter Boström1d04ac62016-02-05 11:25:46 +0100242
Niels Möllerd9c2d942019-04-30 09:16:36 +0200243 frame_buffer_.reset(
244 new video_coding::FrameBuffer(clock_, timing_.get(), &stats_proxy_));
philipelfd5a20f2016-11-15 00:57:57 -0800245
tommidea489f2017-03-03 03:20:24 -0800246 process_thread_->RegisterModule(&rtp_stream_sync_, RTC_FROM_HERE);
Anton Sukhanov4f08faa2019-05-21 11:12:57 -0700247 if (config_.media_transport()) {
248 config_.media_transport()->SetReceiveVideoSink(this);
249 config_.media_transport()->AddRttObserver(this);
Niels Möller5304a322018-08-27 13:27:05 +0200250 } else {
Niels Möller46879152019-01-07 15:54:47 +0100251 // Register with RtpStreamReceiverController.
252 media_receiver_ = receiver_controller->CreateReceiver(
253 config_.rtp.remote_ssrc, &rtp_video_stream_receiver_);
254 if (config_.rtp.rtx_ssrc) {
255 rtx_receive_stream_ = absl::make_unique<RtxReceiveStream>(
256 &rtp_video_stream_receiver_, config.rtp.rtx_associated_payload_types,
257 config_.rtp.remote_ssrc, rtp_receive_statistics_.get());
258 rtx_receiver_ = receiver_controller->CreateReceiver(
259 config_.rtp.rtx_ssrc, rtx_receive_stream_.get());
260 } else {
261 rtp_receive_statistics_->EnableRetransmitDetection(config.rtp.remote_ssrc,
262 true);
263 }
nisse0f15f922017-06-21 01:05:22 -0700264 }
pbos@webrtc.org29d58392013-05-16 12:08:03 +0000265}
266
Ruslan Burakov493a6502019-02-27 15:32:48 +0100267VideoReceiveStream::VideoReceiveStream(
Sebastian Jansson74682c12019-03-01 11:50:20 +0100268 TaskQueueFactory* task_queue_factory,
Ruslan Burakov493a6502019-02-27 15:32:48 +0100269 RtpStreamReceiverControllerInterface* receiver_controller,
270 int num_cpu_cores,
271 PacketRouter* packet_router,
272 VideoReceiveStream::Config config,
273 ProcessThread* process_thread,
Sebastian Jansson8026d602019-03-04 19:39:01 +0100274 CallStats* call_stats,
275 Clock* clock)
Sebastian Jansson74682c12019-03-01 11:50:20 +0100276 : VideoReceiveStream(task_queue_factory,
277 receiver_controller,
Ruslan Burakov493a6502019-02-27 15:32:48 +0100278 num_cpu_cores,
279 packet_router,
280 std::move(config),
281 process_thread,
282 call_stats,
Sebastian Jansson8026d602019-03-04 19:39:01 +0100283 clock,
284 new VCMTiming(clock)) {}
Ruslan Burakov493a6502019-02-27 15:32:48 +0100285
pbos@webrtc.org29d58392013-05-16 12:08:03 +0000286VideoReceiveStream::~VideoReceiveStream() {
Sebastian Janssonb55015e2019-04-09 13:44:04 +0200287 RTC_DCHECK_RUN_ON(&worker_sequence_checker_);
Mirko Bonadei675513b2017-11-09 11:09:25 +0100288 RTC_LOG(LS_INFO) << "~VideoReceiveStream: " << config_.ToString();
Peter Boströmca835252016-02-11 15:59:46 +0100289 Stop();
Anton Sukhanov4f08faa2019-05-21 11:12:57 -0700290 if (config_.media_transport()) {
291 config_.media_transport()->SetReceiveVideoSink(nullptr);
292 config_.media_transport()->RemoveRttObserver(this);
Niels Möller46879152019-01-07 15:54:47 +0100293 }
mflodman4cd27902016-08-05 06:28:45 -0700294 process_thread_->DeRegisterModule(&rtp_stream_sync_);
pbos@webrtc.org29d58392013-05-16 12:08:03 +0000295}
296
pbos1ba8d392016-05-01 20:18:34 -0700297void VideoReceiveStream::SignalNetworkState(NetworkState state) {
Sebastian Janssonb55015e2019-04-09 13:44:04 +0200298 RTC_DCHECK_RUN_ON(&worker_sequence_checker_);
nisseb1f2ff92017-06-09 04:01:55 -0700299 rtp_video_stream_receiver_.SignalNetworkState(state);
pbos1ba8d392016-05-01 20:18:34 -0700300}
301
pbos1ba8d392016-05-01 20:18:34 -0700302bool VideoReceiveStream::DeliverRtcp(const uint8_t* packet, size_t length) {
nisseb1f2ff92017-06-09 04:01:55 -0700303 return rtp_video_stream_receiver_.DeliverRtcp(packet, length);
pbos1ba8d392016-05-01 20:18:34 -0700304}
305
solenberg3ebbcb52017-01-31 03:58:40 -0800306void VideoReceiveStream::SetSync(Syncable* audio_syncable) {
Sebastian Janssonb55015e2019-04-09 13:44:04 +0200307 RTC_DCHECK_RUN_ON(&worker_sequence_checker_);
solenberg3ebbcb52017-01-31 03:58:40 -0800308 rtp_stream_sync_.ConfigureSync(audio_syncable);
brandtr090c9402017-01-25 08:28:02 -0800309}
310
pbos@webrtc.orga5c8d2c2014-04-24 11:13:21 +0000311void VideoReceiveStream::Start() {
Sebastian Janssonb55015e2019-04-09 13:44:04 +0200312 RTC_DCHECK_RUN_ON(&worker_sequence_checker_);
brandtrfb45c6c2017-01-27 06:47:55 -0800313
Sebastian Jansson11d0d7b2019-04-11 12:39:34 +0200314 if (decoder_running_) {
Rasmus Brandt1e27fec2019-01-23 09:47:50 +0100315 return;
316 }
317
318 const bool protected_by_fec = config_.rtp.protected_by_flexfec ||
319 rtp_video_stream_receiver_.IsUlpfecEnabled();
brandtrfb45c6c2017-01-27 06:47:55 -0800320
philipela45102f2017-02-22 05:30:39 -0800321 frame_buffer_->Start();
philipelfd5a20f2016-11-15 00:57:57 -0800322
nisseb1f2ff92017-06-09 04:01:55 -0700323 if (rtp_video_stream_receiver_.IsRetransmissionsEnabled() &&
324 protected_by_fec) {
philipela45102f2017-02-22 05:30:39 -0800325 frame_buffer_->SetProtectionMode(kProtectionNackFEC);
philipelfd5a20f2016-11-15 00:57:57 -0800326 }
philipela45102f2017-02-22 05:30:39 -0800327
sprang@webrtc.orgd9b95602014-01-27 13:03:02 +0000328 transport_adapter_.Enable();
tommi2e82f382016-06-21 00:26:43 -0700329 rtc::VideoSinkInterface<VideoFrame>* renderer = nullptr;
Rasmus Brandt1e27fec2019-01-23 09:47:50 +0100330 if (config_.enable_prerenderer_smoothing) {
Sebastian Jansson74682c12019-03-01 11:50:20 +0100331 incoming_video_stream_.reset(new IncomingVideoStream(
332 task_queue_factory_, config_.render_delay_ms, this));
Rasmus Brandt1e27fec2019-01-23 09:47:50 +0100333 renderer = incoming_video_stream_.get();
334 } else {
335 renderer = this;
tommi2e82f382016-06-21 00:26:43 -0700336 }
337
sprang113bdca2016-10-11 03:10:10 -0700338 for (const Decoder& decoder : config_.decoders) {
Niels Möllercbcbc222018-09-28 09:07:24 +0200339 std::unique_ptr<VideoDecoder> video_decoder =
340 decoder.decoder_factory->LegacyCreateVideoDecoder(decoder.video_format,
341 config_.stream_id);
342 // If we still have no valid decoder, we have to create a "Null" decoder
343 // that ignores all calls. The reason we can get into this state is that the
344 // old decoder factory interface doesn't have a way to query supported
345 // codecs.
346 if (!video_decoder) {
347 video_decoder = absl::make_unique<NullVideoDecoder>();
348 }
Erik Språng96965ae2018-10-23 15:42:37 +0200349
350 std::string decoded_output_file =
351 field_trial::FindFullName("WebRTC-DecoderDataDumpDirectory");
Ilya Nikolaevskiy9a573502019-05-28 12:58:28 +0200352 // Because '/' can't be used inside a field trial parameter, we use ';'
Ilya Nikolaevskiyc0321092019-03-11 11:18:26 +0100353 // instead.
Ilya Nikolaevskiy9a573502019-05-28 12:58:28 +0200354 // This is only relevant to WebRTC-DecoderDataDumpDirectory
355 // field trial. ';' is chosen arbitrary. Even though it's a legal character
356 // in some file systems, we can sacrifice ability to use it in the path to
357 // dumped video, since it's developers-only feature for debugging.
358 absl::c_replace(decoded_output_file, ';', '/');
Erik Språng96965ae2018-10-23 15:42:37 +0200359 if (!decoded_output_file.empty()) {
360 char filename_buffer[256];
361 rtc::SimpleStringBuilder ssb(filename_buffer);
362 ssb << decoded_output_file << "/webrtc_receive_stream_"
Ilya Nikolaevskiyc0321092019-03-11 11:18:26 +0100363 << this->config_.rtp.remote_ssrc << "-" << rtc::TimeMicros()
364 << ".ivf";
Erik Språng96965ae2018-10-23 15:42:37 +0200365 video_decoder = absl::make_unique<FrameDumpingDecoder>(
Niels Möllerb7edf692019-02-08 16:40:53 +0100366 std::move(video_decoder), FileWrapper::OpenWriteOnly(ssb.str()));
Erik Språng96965ae2018-10-23 15:42:37 +0200367 }
368
Niels Möllercbcbc222018-09-28 09:07:24 +0200369 video_decoders_.push_back(std::move(video_decoder));
370
371 video_receiver_.RegisterExternalDecoder(video_decoders_.back().get(),
sprang113bdca2016-10-11 03:10:10 -0700372 decoder.payload_type);
sprang113bdca2016-10-11 03:10:10 -0700373 VideoCodec codec = CreateDecoderVideoCodec(decoder);
Mirta Dvornicicfe68daa2019-05-23 13:21:12 +0200374
375 const bool raw_payload =
376 config_.rtp.raw_payload_types.count(codec.plType) > 0;
377 rtp_video_stream_receiver_.AddReceiveCodec(
378 codec, decoder.video_format.parameters, raw_payload);
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);
Niels Möllerc29fa1b2019-04-16 16:47:08 +0200384 video_stream_decoder_.reset(
385 new VideoStreamDecoder(&video_receiver_, &stats_proxy_, renderer));
tommic8ece432017-06-20 02:44:38 -0700386
Tommief3e28a2018-03-30 08:42:39 +0200387 // Make sure we register as a stats observer *after* we've prepared the
388 // |video_stream_decoder_|.
389 call_stats_->RegisterStatsObserver(this);
390
Niels Möllerad31c982019-03-14 11:20:30 +0100391 // NOTE: *Not* registering video_receiver_ on process_thread_. Its Process
392 // method does nothing that is useful for us, since we no longer use the old
393 // jitter buffer.
tommic8ece432017-06-20 02:44:38 -0700394
Peter Boströmca835252016-02-11 15:59:46 +0100395 // Start the decode thread
Tommifbf3bce2018-02-21 15:56:05 +0100396 video_receiver_.DecoderThreadStarting();
Tommi132e28e2018-02-24 17:57:33 +0100397 stats_proxy_.DecoderThreadStarting();
Sebastian Jansson11d0d7b2019-04-11 12:39:34 +0200398 if (!use_task_queue_) {
399 decode_thread_.Start();
400 } else {
401 decode_queue_.PostTask([this] {
402 RTC_DCHECK_RUN_ON(&decode_queue_);
403 decoder_stopped_ = false;
404 StartNextDecode();
405 });
406 }
407 decoder_running_ = true;
nisseb1f2ff92017-06-09 04:01:55 -0700408 rtp_video_stream_receiver_.StartReceive();
pbos@webrtc.org29d58392013-05-16 12:08:03 +0000409}
410
pbos@webrtc.orga5c8d2c2014-04-24 11:13:21 +0000411void VideoReceiveStream::Stop() {
Sebastian Janssonb55015e2019-04-09 13:44:04 +0200412 RTC_DCHECK_RUN_ON(&worker_sequence_checker_);
nisseb1f2ff92017-06-09 04:01:55 -0700413 rtp_video_stream_receiver_.StopReceive();
philipelfd5a20f2016-11-15 00:57:57 -0800414
Ilya Nikolaevskiyd397a0d2018-02-21 15:57:09 +0100415 stats_proxy_.OnUniqueFramesCounted(
416 rtp_video_stream_receiver_.GetUniqueFramesSeen());
417
Sebastian Jansson11d0d7b2019-04-11 12:39:34 +0200418 if (!use_task_queue_) {
419 frame_buffer_->Stop();
420 } else {
421 decode_queue_.PostTask([this] { frame_buffer_->Stop(); });
422 }
philipele21be1d2017-09-25 06:37:12 -0700423 call_stats_->DeregisterStatsObserver(this);
philipelfd5a20f2016-11-15 00:57:57 -0800424
Sebastian Jansson11d0d7b2019-04-11 12:39:34 +0200425 if (decoder_running_) {
tommic8ece432017-06-20 02:44:38 -0700426 // TriggerDecoderShutdown will release any waiting decoder thread and make
427 // it stop immediately, instead of waiting for a timeout. Needs to be called
428 // before joining the decoder thread.
429 video_receiver_.TriggerDecoderShutdown();
430
Sebastian Jansson11d0d7b2019-04-11 12:39:34 +0200431 if (!use_task_queue_) {
432 decode_thread_.Stop();
433 } else {
434 rtc::Event done;
435 decode_queue_.PostTask([this, &done] {
436 RTC_DCHECK_RUN_ON(&decode_queue_);
437 decoder_stopped_ = true;
438 done.Set();
439 });
440 done.Wait(rtc::Event::kForever);
441 }
442 decoder_running_ = false;
Tommifbf3bce2018-02-21 15:56:05 +0100443 video_receiver_.DecoderThreadStopped();
Tommi132e28e2018-02-24 17:57:33 +0100444 stats_proxy_.DecoderThreadStopped();
sprang0d348d62016-10-07 08:28:39 -0700445 // Deregister external decoders so they are no longer running during
446 // destruction. This effectively stops the VCM since the decoder thread is
447 // stopped, the VCM is deregistered and no asynchronous decoder threads are
448 // running.
449 for (const Decoder& decoder : config_.decoders)
450 video_receiver_.RegisterExternalDecoder(nullptr, decoder.payload_type);
451 }
philipelfd5a20f2016-11-15 00:57:57 -0800452
tommi2e82f382016-06-21 00:26:43 -0700453 video_stream_decoder_.reset();
454 incoming_video_stream_.reset();
sprang@webrtc.orgd9b95602014-01-27 13:03:02 +0000455 transport_adapter_.Disable();
pbos@webrtc.org29d58392013-05-16 12:08:03 +0000456}
457
sprang@webrtc.org9510e532014-02-07 15:32:45 +0000458VideoReceiveStream::Stats VideoReceiveStream::GetStats() const {
Peter Boströmf751bf82016-02-05 14:00:53 +0100459 return stats_proxy_.GetStats();
sprang@webrtc.org09315702014-02-07 12:06:29 +0000460}
461
eladalonc0d481a2017-08-02 07:39:07 -0700462void VideoReceiveStream::AddSecondarySink(RtpPacketSinkInterface* sink) {
463 rtp_video_stream_receiver_.AddSecondarySink(sink);
464}
465
466void VideoReceiveStream::RemoveSecondarySink(
467 const RtpPacketSinkInterface* sink) {
468 rtp_video_stream_receiver_.RemoveSecondarySink(sink);
469}
470
Ruslan Burakov493a6502019-02-27 15:32:48 +0100471bool VideoReceiveStream::SetBaseMinimumPlayoutDelayMs(int delay_ms) {
Sebastian Janssonb55015e2019-04-09 13:44:04 +0200472 RTC_DCHECK_RUN_ON(&worker_sequence_checker_);
Ruslan Burakov493a6502019-02-27 15:32:48 +0100473 if (delay_ms < kMinBaseMinimumDelayMs || delay_ms > kMaxBaseMinimumDelayMs) {
474 return false;
475 }
476
477 rtc::CritScope cs(&playout_delay_lock_);
478 base_minimum_playout_delay_ms_ = delay_ms;
479 UpdatePlayoutDelays();
480 return true;
481}
482
483int VideoReceiveStream::GetBaseMinimumPlayoutDelayMs() const {
Sebastian Janssonb55015e2019-04-09 13:44:04 +0200484 RTC_DCHECK_RUN_ON(&worker_sequence_checker_);
Ruslan Burakov493a6502019-02-27 15:32:48 +0100485
486 rtc::CritScope cs(&playout_delay_lock_);
487 return base_minimum_playout_delay_ms_;
488}
489
tommi2e82f382016-06-21 00:26:43 -0700490// TODO(tommi): This method grabs a lock 6 times.
Tommibd3380f2016-06-10 17:38:17 +0200491void VideoReceiveStream::OnFrame(const VideoFrame& video_frame) {
asaperssonf8cdd182016-03-15 01:00:47 -0700492 int64_t sync_offset_ms;
asaperssonde9e5ff2016-11-02 07:14:03 -0700493 double estimated_freq_khz;
tommi2e82f382016-06-21 00:26:43 -0700494 // TODO(tommi): GetStreamSyncOffsetInMs grabs three locks. One inside the
495 // function itself, another in GetChannel() and a third in
496 // GetPlayoutTimestamp. Seems excessive. Anyhow, I'm assuming the function
497 // succeeds most of the time, which leads to grabbing a fourth lock.
Yves Gerey665174f2018-06-19 15:03:05 +0200498 if (rtp_stream_sync_.GetStreamSyncOffsetInMs(
499 video_frame.timestamp(), video_frame.render_time_ms(),
500 &sync_offset_ms, &estimated_freq_khz)) {
tommi2e82f382016-06-21 00:26:43 -0700501 // TODO(tommi): OnSyncOffsetUpdated grabs a lock.
asaperssonde9e5ff2016-11-02 07:14:03 -0700502 stats_proxy_.OnSyncOffsetUpdated(sync_offset_ms, estimated_freq_khz);
tommi2e82f382016-06-21 00:26:43 -0700503 }
tommi2e82f382016-06-21 00:26:43 -0700504 config_.renderer->OnFrame(video_frame);
pbos@webrtc.org29d58392013-05-16 12:08:03 +0000505
tommi2e82f382016-06-21 00:26:43 -0700506 // TODO(tommi): OnRenderFrame grabs a lock too.
asapersson1490f7a2016-09-23 02:09:46 -0700507 stats_proxy_.OnRenderedFrame(video_frame);
pbos@webrtc.org29d58392013-05-16 12:08:03 +0000508}
pbos@webrtc.org26c0c412014-09-03 16:17:12 +0000509
Benjamin Wrighta5564482019-04-03 10:44:18 -0700510void VideoReceiveStream::SetFrameDecryptor(
511 rtc::scoped_refptr<webrtc::FrameDecryptorInterface> frame_decryptor) {
512 rtp_video_stream_receiver_.SetFrameDecryptor(std::move(frame_decryptor));
513}
514
brandtr090c9402017-01-25 08:28:02 -0800515void VideoReceiveStream::SendNack(
516 const std::vector<uint16_t>& sequence_numbers) {
nisseb1f2ff92017-06-09 04:01:55 -0700517 rtp_video_stream_receiver_.RequestPacketRetransmit(sequence_numbers);
brandtr090c9402017-01-25 08:28:02 -0800518}
519
520void VideoReceiveStream::RequestKeyFrame() {
Anton Sukhanov4f08faa2019-05-21 11:12:57 -0700521 if (config_.media_transport()) {
522 config_.media_transport()->RequestKeyFrame(config_.rtp.remote_ssrc);
Niels Möller46879152019-01-07 15:54:47 +0100523 } else {
524 rtp_video_stream_receiver_.RequestKeyFrame();
525 }
brandtr090c9402017-01-25 08:28:02 -0800526}
527
528void VideoReceiveStream::OnCompleteFrame(
philipele7c891f2018-02-22 14:35:06 +0100529 std::unique_ptr<video_coding::EncodedFrame> frame) {
Sebastian Janssonb55015e2019-04-09 13:44:04 +0200530 RTC_DCHECK_RUN_ON(&network_sequence_checker_);
Ilya Nikolaevskiye6a2d942018-11-07 14:32:28 +0100531 // TODO(https://bugs.webrtc.org/9974): Consider removing this workaround.
532 int64_t time_now_ms = rtc::TimeMillis();
533 if (last_complete_frame_time_ms_ > 0 &&
534 time_now_ms - last_complete_frame_time_ms_ > kInactiveStreamThresholdMs) {
535 frame_buffer_->Clear();
536 }
537 last_complete_frame_time_ms_ = time_now_ms;
538
Ruslan Burakov493a6502019-02-27 15:32:48 +0100539 const PlayoutDelay& playout_delay = frame->EncodedImage().playout_delay_;
540 if (playout_delay.min_ms >= 0) {
541 rtc::CritScope cs(&playout_delay_lock_);
542 frame_minimum_playout_delay_ms_ = playout_delay.min_ms;
543 UpdatePlayoutDelays();
544 }
545
546 if (playout_delay.max_ms >= 0) {
547 rtc::CritScope cs(&playout_delay_lock_);
548 frame_maximum_playout_delay_ms_ = playout_delay.max_ms;
549 UpdatePlayoutDelays();
550 }
Henrik Boströmc680c4a2019-04-03 10:27:36 +0000551
552 int64_t last_continuous_pid = frame_buffer_->InsertFrame(std::move(frame));
553 if (last_continuous_pid != -1)
554 rtp_video_stream_receiver_.FrameContinuous(last_continuous_pid);
brandtr090c9402017-01-25 08:28:02 -0800555}
556
Niels Möller46879152019-01-07 15:54:47 +0100557void VideoReceiveStream::OnData(uint64_t channel_id,
558 MediaTransportEncodedVideoFrame frame) {
559 OnCompleteFrame(
560 absl::make_unique<EncodedFrameForMediaTransport>(std::move(frame)));
561}
562
philipele21be1d2017-09-25 06:37:12 -0700563void VideoReceiveStream::OnRttUpdate(int64_t avg_rtt_ms, int64_t max_rtt_ms) {
Sebastian Janssonb55015e2019-04-09 13:44:04 +0200564 RTC_DCHECK_RUN_ON(&module_process_sequence_checker_);
philipele21be1d2017-09-25 06:37:12 -0700565 frame_buffer_->UpdateRtt(max_rtt_ms);
Tommi81de14f2018-03-25 22:19:25 +0200566 rtp_video_stream_receiver_.UpdateRtt(max_rtt_ms);
philipele21be1d2017-09-25 06:37:12 -0700567}
568
Niels Möller46879152019-01-07 15:54:47 +0100569void VideoReceiveStream::OnRttUpdated(int64_t rtt_ms) {
570 frame_buffer_->UpdateRtt(rtt_ms);
571}
572
solenberg3ebbcb52017-01-31 03:58:40 -0800573int VideoReceiveStream::id() const {
Sebastian Janssonb55015e2019-04-09 13:44:04 +0200574 RTC_DCHECK_RUN_ON(&worker_sequence_checker_);
solenberg3ebbcb52017-01-31 03:58:40 -0800575 return config_.rtp.remote_ssrc;
576}
577
Danil Chapovalovb9b146c2018-06-15 12:28:07 +0200578absl::optional<Syncable::Info> VideoReceiveStream::GetInfo() const {
Sebastian Janssonb55015e2019-04-09 13:44:04 +0200579 RTC_DCHECK_RUN_ON(&module_process_sequence_checker_);
Niels Möllerdf9e9ae2018-07-31 08:29:53 +0200580 absl::optional<Syncable::Info> info =
581 rtp_video_stream_receiver_.GetSyncInfo();
solenberg3ebbcb52017-01-31 03:58:40 -0800582
Niels Möllerdf9e9ae2018-07-31 08:29:53 +0200583 if (!info)
Danil Chapovalovb9b146c2018-06-15 12:28:07 +0200584 return absl::nullopt;
solenberg3ebbcb52017-01-31 03:58:40 -0800585
Niels Möllerbcb7c202019-04-18 12:54:04 +0200586 info->current_delay_ms = timing_->TargetVideoDelay();
Oskar Sundbom8e07c132018-01-08 16:45:42 +0100587 return info;
solenberg3ebbcb52017-01-31 03:58:40 -0800588}
589
590uint32_t VideoReceiveStream::GetPlayoutTimestamp() const {
591 RTC_NOTREACHED();
592 return 0;
593}
594
595void VideoReceiveStream::SetMinimumPlayoutDelay(int delay_ms) {
Sebastian Janssonb55015e2019-04-09 13:44:04 +0200596 RTC_DCHECK_RUN_ON(&module_process_sequence_checker_);
Ruslan Burakov493a6502019-02-27 15:32:48 +0100597 rtc::CritScope cs(&playout_delay_lock_);
598 syncable_minimum_playout_delay_ms_ = delay_ms;
599 UpdatePlayoutDelays();
solenberg3ebbcb52017-01-31 03:58:40 -0800600}
601
Sebastian Jansson1c747f52019-04-04 13:01:39 +0200602int64_t VideoReceiveStream::GetWaitMs() const {
603 return keyframe_required_ ? max_wait_for_keyframe_ms_
604 : max_wait_for_frame_ms_;
605}
Sebastian Jansson11d0d7b2019-04-11 12:39:34 +0200606
607void VideoReceiveStream::StartNextDecode() {
608 RTC_DCHECK(use_task_queue_);
609 TRACE_EVENT0("webrtc", "VideoReceiveStream::StartNextDecode");
610
611 struct DecodeTask {
612 void operator()() {
613 RTC_DCHECK_RUN_ON(&stream->decode_queue_);
614 if (stream->decoder_stopped_)
615 return;
616 if (frame) {
617 stream->HandleEncodedFrame(std::move(frame));
618 } else {
619 stream->HandleFrameBufferTimeout();
620 }
621 stream->StartNextDecode();
622 }
623 VideoReceiveStream* stream;
624 std::unique_ptr<EncodedFrame> frame;
625 };
626
Sebastian Jansson11d0d7b2019-04-11 12:39:34 +0200627 frame_buffer_->NextFrame(
philipelcd936fd2019-05-02 13:53:10 +0200628 GetWaitMs(), keyframe_required_, &decode_queue_,
Sebastian Jansson11d0d7b2019-04-11 12:39:34 +0200629 [this](std::unique_ptr<EncodedFrame> frame, ReturnReason res) {
630 RTC_DCHECK_EQ(frame == nullptr, res == ReturnReason::kTimeout);
631 RTC_DCHECK_EQ(frame != nullptr, res == ReturnReason::kFrameFound);
632 decode_queue_.PostTask(DecodeTask{this, std::move(frame)});
633 });
634}
635
tommic8ece432017-06-20 02:44:38 -0700636void VideoReceiveStream::DecodeThreadFunction(void* ptr) {
Karl Wibergab036382019-03-12 18:01:51 +0100637 ScopedRegisterThreadForDebugging thread_dbg(RTC_FROM_HERE);
tommic8ece432017-06-20 02:44:38 -0700638 while (static_cast<VideoReceiveStream*>(ptr)->Decode()) {
639 }
Peter Boströmca835252016-02-11 15:59:46 +0100640}
641
philipel2dfea3e2017-02-28 07:19:43 -0800642bool VideoReceiveStream::Decode() {
Sebastian Jansson11d0d7b2019-04-11 12:39:34 +0200643 RTC_DCHECK(!use_task_queue_);
tommidb23ea62017-03-03 07:21:18 -0800644 TRACE_EVENT0("webrtc", "VideoReceiveStream::Decode");
philipel3042c2d2017-08-18 04:55:02 -0700645
philipele7c891f2018-02-22 14:35:06 +0100646 std::unique_ptr<video_coding::EncodedFrame> frame;
guidouc3372582017-04-04 07:16:21 -0700647 video_coding::FrameBuffer::ReturnReason res =
philipelcd936fd2019-05-02 13:53:10 +0200648 frame_buffer_->NextFrame(GetWaitMs(), &frame, keyframe_required_);
649
Sebastian Jansson1c747f52019-04-04 13:01:39 +0200650 if (res == ReturnReason::kStopped) {
philipel2dfea3e2017-02-28 07:19:43 -0800651 return false;
guidouc3372582017-04-04 07:16:21 -0700652 }
Henrik Boströmc680c4a2019-04-03 10:27:36 +0000653
philipela45102f2017-02-22 05:30:39 -0800654 if (frame) {
Sebastian Jansson1c747f52019-04-04 13:01:39 +0200655 RTC_DCHECK_EQ(res, ReturnReason::kFrameFound);
656 HandleEncodedFrame(std::move(frame));
philipelfd5a20f2016-11-15 00:57:57 -0800657 } else {
Sebastian Jansson1c747f52019-04-04 13:01:39 +0200658 RTC_DCHECK_EQ(res, ReturnReason::kTimeout);
659 HandleFrameBufferTimeout();
Sebastian Jansson13943b72019-04-02 15:08:14 +0200660 }
Henrik Boströmc680c4a2019-04-03 10:27:36 +0000661 return true;
Sebastian Jansson13943b72019-04-02 15:08:14 +0200662}
663
Sebastian Jansson1c747f52019-04-04 13:01:39 +0200664void VideoReceiveStream::HandleEncodedFrame(
665 std::unique_ptr<EncodedFrame> frame) {
666 int64_t now_ms = clock_->TimeInMilliseconds();
667
668 // Current OnPreDecode only cares about QP for VP8.
669 int qp = -1;
670 if (frame->CodecSpecific()->codecType == kVideoCodecVP8) {
671 if (!vp8::GetQp(frame->data(), frame->size(), &qp)) {
672 RTC_LOG(LS_WARNING) << "Failed to extract QP from VP8 video frame";
673 }
674 }
675 stats_proxy_.OnPreDecode(frame->CodecSpecific()->codecType, qp);
676
677 int decode_result = video_receiver_.Decode(frame.get());
678 if (decode_result == WEBRTC_VIDEO_CODEC_OK ||
679 decode_result == WEBRTC_VIDEO_CODEC_OK_REQUEST_KEYFRAME) {
680 keyframe_required_ = false;
681 frame_decoded_ = true;
682 rtp_video_stream_receiver_.FrameDecoded(frame->id.picture_id);
683
684 if (decode_result == WEBRTC_VIDEO_CODEC_OK_REQUEST_KEYFRAME)
685 RequestKeyFrame();
686 } else if (!frame_decoded_ || !keyframe_required_ ||
687 (last_keyframe_request_ms_ + max_wait_for_keyframe_ms_ < now_ms)) {
688 keyframe_required_ = true;
689 // TODO(philipel): Remove this keyframe request when downstream project
690 // has been fixed.
691 RequestKeyFrame();
692 last_keyframe_request_ms_ = now_ms;
693 }
694}
695
696void VideoReceiveStream::HandleFrameBufferTimeout() {
697 int64_t now_ms = clock_->TimeInMilliseconds();
698 absl::optional<int64_t> last_packet_ms =
699 rtp_video_stream_receiver_.LastReceivedPacketMs();
700 absl::optional<int64_t> last_keyframe_packet_ms =
701 rtp_video_stream_receiver_.LastReceivedKeyframePacketMs();
702
703 // To avoid spamming keyframe requests for a stream that is not active we
704 // check if we have received a packet within the last 5 seconds.
705 bool stream_is_active = last_packet_ms && now_ms - *last_packet_ms < 5000;
706 if (!stream_is_active)
707 stats_proxy_.OnStreamInactive();
708
709 // If we recently have been receiving packets belonging to a keyframe then
710 // we assume a keyframe is currently being received.
711 bool receiving_keyframe =
712 last_keyframe_packet_ms &&
713 now_ms - *last_keyframe_packet_ms < max_wait_for_keyframe_ms_;
714
715 if (stream_is_active && !receiving_keyframe &&
716 (!config_.crypto_options.sframe.require_frame_encryption ||
717 rtp_video_stream_receiver_.IsDecryptable())) {
718 RTC_LOG(LS_WARNING) << "No decodable frame in " << GetWaitMs()
719 << " ms, requesting keyframe.";
720 RequestKeyFrame();
721 }
722}
723
Ruslan Burakov493a6502019-02-27 15:32:48 +0100724void VideoReceiveStream::UpdatePlayoutDelays() const {
725 const int minimum_delay_ms =
726 std::max({frame_minimum_playout_delay_ms_, base_minimum_playout_delay_ms_,
727 syncable_minimum_playout_delay_ms_});
728 if (minimum_delay_ms >= 0) {
729 timing_->set_min_playout_delay(minimum_delay_ms);
730 }
731
732 const int maximum_delay_ms = frame_maximum_playout_delay_ms_;
733 if (maximum_delay_ms >= 0) {
734 timing_->set_max_playout_delay(maximum_delay_ms);
735 }
736}
737
Jonas Oreland49ac5952018-09-26 16:04:32 +0200738std::vector<webrtc::RtpSource> VideoReceiveStream::GetSources() const {
739 return rtp_video_stream_receiver_.GetSources();
740}
741
mflodman@webrtc.orgf3973e82013-12-13 09:40:45 +0000742} // namespace internal
743} // namespace webrtc