blob: 1c1c402b5ff8fc0cae4247ada110ab5f59ad5a1b [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
Karl Wiberg918f50c2018-07-05 11:40:33 +020020#include "absl/memory/memory.h"
Danil Chapovalovb9b146c2018-06-15 12:28:07 +020021#include "absl/types/optional.h"
Yves Gerey3e707812018-11-28 16:47:49 +010022#include "api/array_view.h"
Steve Anton10542f22019-01-11 09:11:00 -080023#include "api/crypto/frame_decryptor_interface.h"
Yves Gerey3e707812018-11-28 16:47:49 +010024#include "api/video/encoded_image.h"
25#include "api/video_codecs/sdp_video_format.h"
26#include "api/video_codecs/video_codec.h"
Niels Möllercbcbc222018-09-28 09:07:24 +020027#include "api/video_codecs/video_decoder_factory.h"
Yves Gerey3e707812018-11-28 16:47:49 +010028#include "api/video_codecs/video_encoder.h"
Mirko Bonadei92ea95e2017-09-15 06:47:31 +020029#include "call/rtp_stream_receiver_controller_interface.h"
30#include "call/rtx_receive_stream.h"
Yves Gerey3e707812018-11-28 16:47:49 +010031#include "common_types.h" // NOLINT(build/include)
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"
42#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 {
Ruslan Burakov493a6502019-02-27 15:32:48 +010058constexpr int kMinBaseMinimumDelayMs = 0;
59constexpr int kMaxBaseMinimumDelayMs = 10000;
60
pbos@webrtc.org776e6f22014-10-29 15:28:39 +000061VideoCodec CreateDecoderVideoCodec(const VideoReceiveStream::Decoder& decoder) {
62 VideoCodec codec;
63 memset(&codec, 0, sizeof(codec));
64
65 codec.plType = decoder.payload_type;
Niels Möllercb7e1d22018-09-11 15:56:04 +020066 codec.codecType = PayloadStringToCodecType(decoder.video_format.name);
pbos@webrtc.org776e6f22014-10-29 15:28:39 +000067
68 if (codec.codecType == kVideoCodecVP8) {
hta257dc392016-10-25 09:05:06 -070069 *(codec.VP8()) = VideoEncoder::GetDefaultVp8Settings();
ivica05cfcd32015-09-07 06:04:16 -070070 } else if (codec.codecType == kVideoCodecVP9) {
hta257dc392016-10-25 09:05:06 -070071 *(codec.VP9()) = VideoEncoder::GetDefaultVp9Settings();
pbos@webrtc.org776e6f22014-10-29 15:28:39 +000072 } else if (codec.codecType == kVideoCodecH264) {
hta257dc392016-10-25 09:05:06 -070073 *(codec.H264()) = VideoEncoder::GetDefaultH264Settings();
magjede69a1a92016-11-25 10:06:31 -080074 codec.H264()->profile =
Niels Möllercb7e1d22018-09-11 15:56:04 +020075 H264::ParseSdpProfileLevelId(decoder.video_format.parameters)->profile;
Emircan Uysalerd7ae3c32018-01-25 13:01:09 -080076 } else if (codec.codecType == kVideoCodecMultiplex) {
Emircan Uysaler0a375472017-12-11 12:21:02 +053077 VideoReceiveStream::Decoder associated_decoder = decoder;
Niels Möllercb7e1d22018-09-11 15:56:04 +020078 associated_decoder.video_format =
79 SdpVideoFormat(CodecTypeToPayloadString(kVideoCodecVP9));
Emircan Uysaler0a375472017-12-11 12:21:02 +053080 VideoCodec associated_codec = CreateDecoderVideoCodec(associated_decoder);
Emircan Uysalerd7ae3c32018-01-25 13:01:09 -080081 associated_codec.codecType = kVideoCodecMultiplex;
Emircan Uysaler0a375472017-12-11 12:21:02 +053082 return associated_codec;
pbos@webrtc.org776e6f22014-10-29 15:28:39 +000083 }
84
85 codec.width = 320;
86 codec.height = 180;
stefanb4bc65b2016-11-02 10:10:20 -070087 const int kDefaultStartBitrate = 300;
pbos@webrtc.org776e6f22014-10-29 15:28:39 +000088 codec.startBitrate = codec.minBitrate = codec.maxBitrate =
stefanb4bc65b2016-11-02 10:10:20 -070089 kDefaultStartBitrate;
pbos@webrtc.org776e6f22014-10-29 15:28:39 +000090
91 return codec;
92}
Niels Möllercbcbc222018-09-28 09:07:24 +020093
94// Video decoder class to be used for unknown codecs. Doesn't support decoding
95// but logs messages to LS_ERROR.
96class NullVideoDecoder : public webrtc::VideoDecoder {
97 public:
98 int32_t InitDecode(const webrtc::VideoCodec* codec_settings,
99 int32_t number_of_cores) override {
100 RTC_LOG(LS_ERROR) << "Can't initialize NullVideoDecoder.";
101 return WEBRTC_VIDEO_CODEC_OK;
102 }
103
104 int32_t Decode(const webrtc::EncodedImage& input_image,
105 bool missing_frames,
Jeroen de Borst2c7b9822019-03-07 19:40:07 +0000106 const webrtc::CodecSpecificInfo* codec_specific_info,
Niels Möllercbcbc222018-09-28 09:07:24 +0200107 int64_t render_time_ms) override {
108 RTC_LOG(LS_ERROR) << "The NullVideoDecoder doesn't support decoding.";
109 return WEBRTC_VIDEO_CODEC_OK;
110 }
111
112 int32_t RegisterDecodeCompleteCallback(
113 webrtc::DecodedImageCallback* callback) override {
114 RTC_LOG(LS_ERROR)
115 << "Can't register decode complete callback on NullVideoDecoder.";
116 return WEBRTC_VIDEO_CODEC_OK;
117 }
118
119 int32_t Release() override { return WEBRTC_VIDEO_CODEC_OK; }
120
121 const char* ImplementationName() const override { return "NullVideoDecoder"; }
122};
123
Niels Möller46879152019-01-07 15:54:47 +0100124// Inherit video_coding::EncodedFrame, which is the class used by
125// video_coding::FrameBuffer and other components in the receive pipeline. It's
126// a subclass of EncodedImage, and it always owns the buffer.
127class EncodedFrameForMediaTransport : public video_coding::EncodedFrame {
128 public:
129 explicit EncodedFrameForMediaTransport(
130 MediaTransportEncodedVideoFrame frame) {
Niels Möller938dd9f2019-02-07 00:02:17 +0100131 // TODO(nisse): This is ugly. We copy the EncodedImage (a base class of
132 // ours, in several steps), to get all the meta data. We should be using
133 // std::move in some way. Then we also need to handle the case of an unowned
134 // buffer, in which case we need to make an owned copy.
Niels Möller46879152019-01-07 15:54:47 +0100135 *static_cast<class EncodedImage*>(this) = frame.encoded_image();
Niels Möller46879152019-01-07 15:54:47 +0100136
Niels Möller69bb3af2019-02-13 10:05:49 +0100137 // If we don't already own the buffer, make a copy.
138 Retain();
Niels Möller46879152019-01-07 15:54:47 +0100139
140 _payloadType = static_cast<uint8_t>(frame.payload_type());
141
142 // TODO(nisse): frame_id and picture_id are probably not the same thing. For
143 // a single layer, this should be good enough.
144 id.picture_id = frame.frame_id();
145 id.spatial_layer = frame.encoded_image().SpatialIndex().value_or(0);
146 num_references = std::min(static_cast<size_t>(kMaxFrameReferences),
147 frame.referenced_frame_ids().size());
148 for (size_t i = 0; i < num_references; i++) {
149 references[i] = frame.referenced_frame_ids()[i];
150 }
151 }
152
153 // TODO(nisse): Implement. Not sure how they are used.
154 int64_t ReceivedTime() const override { return 0; }
155 int64_t RenderTime() const override { return 0; }
156};
157
Ilya Nikolaevskiye6a2d942018-11-07 14:32:28 +0100158// TODO(https://bugs.webrtc.org/9974): Consider removing this workaround.
159// Maximum time between frames before resetting the FrameBuffer to avoid RTP
160// timestamps wraparound to affect FrameBuffer.
161constexpr int kInactiveStreamThresholdMs = 600000; // 10 minutes.
162
pbos@webrtc.org776e6f22014-10-29 15:28:39 +0000163} // namespace
pbos@webrtc.org29d58392013-05-16 12:08:03 +0000164
Peter Boströmca835252016-02-11 15:59:46 +0100165namespace internal {
tommi2e82f382016-06-21 00:26:43 -0700166
nisse0f15f922017-06-21 01:05:22 -0700167VideoReceiveStream::VideoReceiveStream(
Sebastian Jansson74682c12019-03-01 11:50:20 +0100168 TaskQueueFactory* task_queue_factory,
nisse0f15f922017-06-21 01:05:22 -0700169 RtpStreamReceiverControllerInterface* receiver_controller,
170 int num_cpu_cores,
171 PacketRouter* packet_router,
172 VideoReceiveStream::Config config,
173 ProcessThread* process_thread,
Ruslan Burakov493a6502019-02-27 15:32:48 +0100174 CallStats* call_stats,
175 Clock* clock,
176 VCMTiming* timing)
Sebastian Jansson74682c12019-03-01 11:50:20 +0100177 : task_queue_factory_(task_queue_factory),
178 transport_adapter_(config.rtcp_send_transport),
Tommi733b5472016-06-10 17:58:01 +0200179 config_(std::move(config)),
sprang113bdca2016-10-11 03:10:10 -0700180 num_cpu_cores_(num_cpu_cores),
Peter Boström1d04ac62016-02-05 11:25:46 +0100181 process_thread_(process_thread),
Ruslan Burakov493a6502019-02-27 15:32:48 +0100182 clock_(clock),
tommic8ece432017-06-20 02:44:38 -0700183 decode_thread_(&DecodeThreadFunction,
184 this,
185 "DecodingThread",
186 rtc::kHighestPriority),
Peter Boström1d04ac62016-02-05 11:25:46 +0100187 call_stats_(call_stats),
Danil Chapovalov8ce0d2b2018-11-23 11:03:25 +0100188 stats_proxy_(&config_, clock_),
189 rtp_receive_statistics_(
190 ReceiveStatistics::Create(clock_, &stats_proxy_, &stats_proxy_)),
Ruslan Burakov493a6502019-02-27 15:32:48 +0100191 timing_(timing),
192 video_receiver_(clock_,
193 timing_.get(),
194 this, // NackSender
195 this), // KeyFrameRequestSender
Sebastian Jansson8026d602019-03-04 19:39:01 +0100196 rtp_video_stream_receiver_(clock_,
197 &transport_adapter_,
Tommi38c5d932018-03-27 23:11:09 +0200198 call_stats,
nisseb1f2ff92017-06-09 04:01:55 -0700199 packet_router,
200 &config_,
nisseca5706d2017-09-11 02:32:16 -0700201 rtp_receive_statistics_.get(),
nisseb1f2ff92017-06-09 04:01:55 -0700202 &stats_proxy_,
203 process_thread_,
Benjamin Wright192eeec2018-10-17 17:27:25 -0700204 this, // NackSender
205 this, // KeyFrameRequestSender
206 this, // OnCompleteFrameCallback
207 config_.frame_decryptor),
philipela45102f2017-02-22 05:30:39 -0800208 rtp_stream_sync_(this) {
Mirko Bonadei675513b2017-11-09 11:09:25 +0100209 RTC_LOG(LS_INFO) << "VideoReceiveStream: " << config_.ToString();
pbos@webrtc.org29d58392013-05-16 12:08:03 +0000210
Rasmus Brandt1e27fec2019-01-23 09:47:50 +0100211 RTC_DCHECK(config_.renderer);
Stefan Holmer58c664c2016-02-08 14:31:30 +0100212 RTC_DCHECK(process_thread_);
Stefan Holmer58c664c2016-02-08 14:31:30 +0100213 RTC_DCHECK(call_stats_);
mflodmancfc8e3b2016-05-03 21:22:04 -0700214
eladalona28122f2017-08-18 04:02:48 -0700215 module_process_sequence_checker_.Detach();
Ruslan Burakov493a6502019-02-27 15:32:48 +0100216 network_sequence_checker_.Detach();
solenberg3ebbcb52017-01-31 03:58:40 -0800217
henrikg91d6ede2015-09-17 00:24:34 -0700218 RTC_DCHECK(!config_.decoders.empty());
Peter Boström521af4e2015-11-27 16:35:04 +0100219 std::set<int> decoder_payload_types;
Peter Boströmb1ae3a42016-02-15 17:52:40 +0100220 for (const Decoder& decoder : config_.decoders) {
Niels Möllercbcbc222018-09-28 09:07:24 +0200221 RTC_CHECK(decoder.decoder_factory);
Peter Boström521af4e2015-11-27 16:35:04 +0100222 RTC_CHECK(decoder_payload_types.find(decoder.payload_type) ==
223 decoder_payload_types.end())
224 << "Duplicate payload type (" << decoder.payload_type
225 << ") for different decoders.";
226 decoder_payload_types.insert(decoder.payload_type);
pbos@webrtc.org0181b5f2013-09-09 08:26:30 +0000227 }
228
nisseca5706d2017-09-11 02:32:16 -0700229 video_receiver_.SetRenderDelay(config_.render_delay_ms);
Peter Boström1d04ac62016-02-05 11:25:46 +0100230
philipela45102f2017-02-22 05:30:39 -0800231 jitter_estimator_.reset(new VCMJitterEstimator(clock_));
232 frame_buffer_.reset(new video_coding::FrameBuffer(
233 clock_, jitter_estimator_.get(), timing_.get(), &stats_proxy_));
philipelfd5a20f2016-11-15 00:57:57 -0800234
tommidea489f2017-03-03 03:20:24 -0800235 process_thread_->RegisterModule(&rtp_stream_sync_, RTC_FROM_HERE);
nisse0f15f922017-06-21 01:05:22 -0700236
Niels Möller46879152019-01-07 15:54:47 +0100237 if (config_.media_transport) {
238 config_.media_transport->SetReceiveVideoSink(this);
239 config_.media_transport->AddRttObserver(this);
Niels Möller5304a322018-08-27 13:27:05 +0200240 } else {
Niels Möller46879152019-01-07 15:54:47 +0100241 // Register with RtpStreamReceiverController.
242 media_receiver_ = receiver_controller->CreateReceiver(
243 config_.rtp.remote_ssrc, &rtp_video_stream_receiver_);
244 if (config_.rtp.rtx_ssrc) {
245 rtx_receive_stream_ = absl::make_unique<RtxReceiveStream>(
246 &rtp_video_stream_receiver_, config.rtp.rtx_associated_payload_types,
247 config_.rtp.remote_ssrc, rtp_receive_statistics_.get());
248 rtx_receiver_ = receiver_controller->CreateReceiver(
249 config_.rtp.rtx_ssrc, rtx_receive_stream_.get());
250 } else {
251 rtp_receive_statistics_->EnableRetransmitDetection(config.rtp.remote_ssrc,
252 true);
253 }
nisse0f15f922017-06-21 01:05:22 -0700254 }
pbos@webrtc.org29d58392013-05-16 12:08:03 +0000255}
256
Ruslan Burakov493a6502019-02-27 15:32:48 +0100257VideoReceiveStream::VideoReceiveStream(
Sebastian Jansson74682c12019-03-01 11:50:20 +0100258 TaskQueueFactory* task_queue_factory,
Ruslan Burakov493a6502019-02-27 15:32:48 +0100259 RtpStreamReceiverControllerInterface* receiver_controller,
260 int num_cpu_cores,
261 PacketRouter* packet_router,
262 VideoReceiveStream::Config config,
263 ProcessThread* process_thread,
Sebastian Jansson8026d602019-03-04 19:39:01 +0100264 CallStats* call_stats,
265 Clock* clock)
Sebastian Jansson74682c12019-03-01 11:50:20 +0100266 : VideoReceiveStream(task_queue_factory,
267 receiver_controller,
Ruslan Burakov493a6502019-02-27 15:32:48 +0100268 num_cpu_cores,
269 packet_router,
270 std::move(config),
271 process_thread,
272 call_stats,
Sebastian Jansson8026d602019-03-04 19:39:01 +0100273 clock,
274 new VCMTiming(clock)) {}
Ruslan Burakov493a6502019-02-27 15:32:48 +0100275
pbos@webrtc.org29d58392013-05-16 12:08:03 +0000276VideoReceiveStream::~VideoReceiveStream() {
eladalona28122f2017-08-18 04:02:48 -0700277 RTC_DCHECK_CALLED_SEQUENTIALLY(&worker_sequence_checker_);
Mirko Bonadei675513b2017-11-09 11:09:25 +0100278 RTC_LOG(LS_INFO) << "~VideoReceiveStream: " << config_.ToString();
Peter Boströmca835252016-02-11 15:59:46 +0100279 Stop();
Niels Möller46879152019-01-07 15:54:47 +0100280 if (config_.media_transport) {
281 config_.media_transport->SetReceiveVideoSink(nullptr);
282 config_.media_transport->RemoveRttObserver(this);
283 }
mflodman4cd27902016-08-05 06:28:45 -0700284 process_thread_->DeRegisterModule(&rtp_stream_sync_);
pbos@webrtc.org29d58392013-05-16 12:08:03 +0000285}
286
pbos1ba8d392016-05-01 20:18:34 -0700287void VideoReceiveStream::SignalNetworkState(NetworkState state) {
eladalona28122f2017-08-18 04:02:48 -0700288 RTC_DCHECK_CALLED_SEQUENTIALLY(&worker_sequence_checker_);
nisseb1f2ff92017-06-09 04:01:55 -0700289 rtp_video_stream_receiver_.SignalNetworkState(state);
pbos1ba8d392016-05-01 20:18:34 -0700290}
291
pbos1ba8d392016-05-01 20:18:34 -0700292bool VideoReceiveStream::DeliverRtcp(const uint8_t* packet, size_t length) {
nisseb1f2ff92017-06-09 04:01:55 -0700293 return rtp_video_stream_receiver_.DeliverRtcp(packet, length);
pbos1ba8d392016-05-01 20:18:34 -0700294}
295
solenberg3ebbcb52017-01-31 03:58:40 -0800296void VideoReceiveStream::SetSync(Syncable* audio_syncable) {
eladalona28122f2017-08-18 04:02:48 -0700297 RTC_DCHECK_CALLED_SEQUENTIALLY(&worker_sequence_checker_);
solenberg3ebbcb52017-01-31 03:58:40 -0800298 rtp_stream_sync_.ConfigureSync(audio_syncable);
brandtr090c9402017-01-25 08:28:02 -0800299}
300
pbos@webrtc.orga5c8d2c2014-04-24 11:13:21 +0000301void VideoReceiveStream::Start() {
eladalona28122f2017-08-18 04:02:48 -0700302 RTC_DCHECK_CALLED_SEQUENTIALLY(&worker_sequence_checker_);
brandtrfb45c6c2017-01-27 06:47:55 -0800303
Rasmus Brandt1e27fec2019-01-23 09:47:50 +0100304 if (decode_thread_.IsRunning()) {
305 return;
306 }
307
308 const bool protected_by_fec = config_.rtp.protected_by_flexfec ||
309 rtp_video_stream_receiver_.IsUlpfecEnabled();
brandtrfb45c6c2017-01-27 06:47:55 -0800310
philipela45102f2017-02-22 05:30:39 -0800311 frame_buffer_->Start();
philipelfd5a20f2016-11-15 00:57:57 -0800312
nisseb1f2ff92017-06-09 04:01:55 -0700313 if (rtp_video_stream_receiver_.IsRetransmissionsEnabled() &&
314 protected_by_fec) {
philipela45102f2017-02-22 05:30:39 -0800315 frame_buffer_->SetProtectionMode(kProtectionNackFEC);
philipelfd5a20f2016-11-15 00:57:57 -0800316 }
philipela45102f2017-02-22 05:30:39 -0800317
sprang@webrtc.orgd9b95602014-01-27 13:03:02 +0000318 transport_adapter_.Enable();
tommi2e82f382016-06-21 00:26:43 -0700319 rtc::VideoSinkInterface<VideoFrame>* renderer = nullptr;
Rasmus Brandt1e27fec2019-01-23 09:47:50 +0100320 if (config_.enable_prerenderer_smoothing) {
Sebastian Jansson74682c12019-03-01 11:50:20 +0100321 incoming_video_stream_.reset(new IncomingVideoStream(
322 task_queue_factory_, config_.render_delay_ms, this));
Rasmus Brandt1e27fec2019-01-23 09:47:50 +0100323 renderer = incoming_video_stream_.get();
324 } else {
325 renderer = this;
tommi2e82f382016-06-21 00:26:43 -0700326 }
327
sprang113bdca2016-10-11 03:10:10 -0700328 for (const Decoder& decoder : config_.decoders) {
Niels Möllercbcbc222018-09-28 09:07:24 +0200329 std::unique_ptr<VideoDecoder> video_decoder =
330 decoder.decoder_factory->LegacyCreateVideoDecoder(decoder.video_format,
331 config_.stream_id);
332 // If we still have no valid decoder, we have to create a "Null" decoder
333 // that ignores all calls. The reason we can get into this state is that the
334 // old decoder factory interface doesn't have a way to query supported
335 // codecs.
336 if (!video_decoder) {
337 video_decoder = absl::make_unique<NullVideoDecoder>();
338 }
Erik Språng96965ae2018-10-23 15:42:37 +0200339
340 std::string decoded_output_file =
341 field_trial::FindFullName("WebRTC-DecoderDataDumpDirectory");
Ilya Nikolaevskiyc0321092019-03-11 11:18:26 +0100342 // Because '/' can't be used inside a field trial parameter, we use ':'
343 // instead.
344 std::replace(decoded_output_file.begin(), decoded_output_file.end(), ':',
345 '/');
Erik Språng96965ae2018-10-23 15:42:37 +0200346 if (!decoded_output_file.empty()) {
347 char filename_buffer[256];
348 rtc::SimpleStringBuilder ssb(filename_buffer);
349 ssb << decoded_output_file << "/webrtc_receive_stream_"
Ilya Nikolaevskiyc0321092019-03-11 11:18:26 +0100350 << this->config_.rtp.remote_ssrc << "-" << rtc::TimeMicros()
351 << ".ivf";
Erik Språng96965ae2018-10-23 15:42:37 +0200352 video_decoder = absl::make_unique<FrameDumpingDecoder>(
Niels Möllerb7edf692019-02-08 16:40:53 +0100353 std::move(video_decoder), FileWrapper::OpenWriteOnly(ssb.str()));
Erik Språng96965ae2018-10-23 15:42:37 +0200354 }
355
Niels Möllercbcbc222018-09-28 09:07:24 +0200356 video_decoders_.push_back(std::move(video_decoder));
357
358 video_receiver_.RegisterExternalDecoder(video_decoders_.back().get(),
sprang113bdca2016-10-11 03:10:10 -0700359 decoder.payload_type);
sprang113bdca2016-10-11 03:10:10 -0700360 VideoCodec codec = CreateDecoderVideoCodec(decoder);
Niels Möllercb7e1d22018-09-11 15:56:04 +0200361 rtp_video_stream_receiver_.AddReceiveCodec(codec,
362 decoder.video_format.parameters);
sprang113bdca2016-10-11 03:10:10 -0700363 RTC_CHECK_EQ(VCM_OK, video_receiver_.RegisterReceiveCodec(
364 &codec, num_cpu_cores_, false));
365 }
366
Rasmus Brandt1e27fec2019-01-23 09:47:50 +0100367 RTC_DCHECK(renderer != nullptr);
tommi2e82f382016-06-21 00:26:43 -0700368 video_stream_decoder_.reset(new VideoStreamDecoder(
nisseb1f2ff92017-06-09 04:01:55 -0700369 &video_receiver_, &rtp_video_stream_receiver_,
370 &rtp_video_stream_receiver_,
371 rtp_video_stream_receiver_.IsRetransmissionsEnabled(), protected_by_fec,
nisse76bc8e82017-02-07 09:37:41 -0800372 &stats_proxy_, renderer));
tommic8ece432017-06-20 02:44:38 -0700373
Tommief3e28a2018-03-30 08:42:39 +0200374 // Make sure we register as a stats observer *after* we've prepared the
375 // |video_stream_decoder_|.
376 call_stats_->RegisterStatsObserver(this);
377
tommic8ece432017-06-20 02:44:38 -0700378 process_thread_->RegisterModule(&video_receiver_, RTC_FROM_HERE);
379
Peter Boströmca835252016-02-11 15:59:46 +0100380 // Start the decode thread
Tommifbf3bce2018-02-21 15:56:05 +0100381 video_receiver_.DecoderThreadStarting();
Tommi132e28e2018-02-24 17:57:33 +0100382 stats_proxy_.DecoderThreadStarting();
Peter Boströmca835252016-02-11 15:59:46 +0100383 decode_thread_.Start();
nisseb1f2ff92017-06-09 04:01:55 -0700384 rtp_video_stream_receiver_.StartReceive();
pbos@webrtc.org29d58392013-05-16 12:08:03 +0000385}
386
pbos@webrtc.orga5c8d2c2014-04-24 11:13:21 +0000387void VideoReceiveStream::Stop() {
eladalona28122f2017-08-18 04:02:48 -0700388 RTC_DCHECK_CALLED_SEQUENTIALLY(&worker_sequence_checker_);
nisseb1f2ff92017-06-09 04:01:55 -0700389 rtp_video_stream_receiver_.StopReceive();
philipelfd5a20f2016-11-15 00:57:57 -0800390
Ilya Nikolaevskiyd397a0d2018-02-21 15:57:09 +0100391 stats_proxy_.OnUniqueFramesCounted(
392 rtp_video_stream_receiver_.GetUniqueFramesSeen());
393
philipela45102f2017-02-22 05:30:39 -0800394 frame_buffer_->Stop();
philipele21be1d2017-09-25 06:37:12 -0700395 call_stats_->DeregisterStatsObserver(this);
tommic8ece432017-06-20 02:44:38 -0700396 process_thread_->DeRegisterModule(&video_receiver_);
philipelfd5a20f2016-11-15 00:57:57 -0800397
sprang0d348d62016-10-07 08:28:39 -0700398 if (decode_thread_.IsRunning()) {
tommic8ece432017-06-20 02:44:38 -0700399 // TriggerDecoderShutdown will release any waiting decoder thread and make
400 // it stop immediately, instead of waiting for a timeout. Needs to be called
401 // before joining the decoder thread.
402 video_receiver_.TriggerDecoderShutdown();
403
sprang0d348d62016-10-07 08:28:39 -0700404 decode_thread_.Stop();
Tommifbf3bce2018-02-21 15:56:05 +0100405 video_receiver_.DecoderThreadStopped();
Tommi132e28e2018-02-24 17:57:33 +0100406 stats_proxy_.DecoderThreadStopped();
sprang0d348d62016-10-07 08:28:39 -0700407 // Deregister external decoders so they are no longer running during
408 // destruction. This effectively stops the VCM since the decoder thread is
409 // stopped, the VCM is deregistered and no asynchronous decoder threads are
410 // running.
411 for (const Decoder& decoder : config_.decoders)
412 video_receiver_.RegisterExternalDecoder(nullptr, decoder.payload_type);
413 }
philipelfd5a20f2016-11-15 00:57:57 -0800414
tommi2e82f382016-06-21 00:26:43 -0700415 video_stream_decoder_.reset();
416 incoming_video_stream_.reset();
sprang@webrtc.orgd9b95602014-01-27 13:03:02 +0000417 transport_adapter_.Disable();
pbos@webrtc.org29d58392013-05-16 12:08:03 +0000418}
419
sprang@webrtc.org9510e532014-02-07 15:32:45 +0000420VideoReceiveStream::Stats VideoReceiveStream::GetStats() const {
Peter Boströmf751bf82016-02-05 14:00:53 +0100421 return stats_proxy_.GetStats();
sprang@webrtc.org09315702014-02-07 12:06:29 +0000422}
423
eladalonc0d481a2017-08-02 07:39:07 -0700424void VideoReceiveStream::AddSecondarySink(RtpPacketSinkInterface* sink) {
425 rtp_video_stream_receiver_.AddSecondarySink(sink);
426}
427
428void VideoReceiveStream::RemoveSecondarySink(
429 const RtpPacketSinkInterface* sink) {
430 rtp_video_stream_receiver_.RemoveSecondarySink(sink);
431}
432
Ruslan Burakov493a6502019-02-27 15:32:48 +0100433bool VideoReceiveStream::SetBaseMinimumPlayoutDelayMs(int delay_ms) {
434 RTC_DCHECK_CALLED_SEQUENTIALLY(&worker_sequence_checker_);
435 if (delay_ms < kMinBaseMinimumDelayMs || delay_ms > kMaxBaseMinimumDelayMs) {
436 return false;
437 }
438
439 rtc::CritScope cs(&playout_delay_lock_);
440 base_minimum_playout_delay_ms_ = delay_ms;
441 UpdatePlayoutDelays();
442 return true;
443}
444
445int VideoReceiveStream::GetBaseMinimumPlayoutDelayMs() const {
446 RTC_DCHECK_CALLED_SEQUENTIALLY(&worker_sequence_checker_);
447
448 rtc::CritScope cs(&playout_delay_lock_);
449 return base_minimum_playout_delay_ms_;
450}
451
tommi2e82f382016-06-21 00:26:43 -0700452// TODO(tommi): This method grabs a lock 6 times.
Tommibd3380f2016-06-10 17:38:17 +0200453void VideoReceiveStream::OnFrame(const VideoFrame& video_frame) {
asaperssonf8cdd182016-03-15 01:00:47 -0700454 int64_t sync_offset_ms;
asaperssonde9e5ff2016-11-02 07:14:03 -0700455 double estimated_freq_khz;
tommi2e82f382016-06-21 00:26:43 -0700456 // TODO(tommi): GetStreamSyncOffsetInMs grabs three locks. One inside the
457 // function itself, another in GetChannel() and a third in
458 // GetPlayoutTimestamp. Seems excessive. Anyhow, I'm assuming the function
459 // succeeds most of the time, which leads to grabbing a fourth lock.
Yves Gerey665174f2018-06-19 15:03:05 +0200460 if (rtp_stream_sync_.GetStreamSyncOffsetInMs(
461 video_frame.timestamp(), video_frame.render_time_ms(),
462 &sync_offset_ms, &estimated_freq_khz)) {
tommi2e82f382016-06-21 00:26:43 -0700463 // TODO(tommi): OnSyncOffsetUpdated grabs a lock.
asaperssonde9e5ff2016-11-02 07:14:03 -0700464 stats_proxy_.OnSyncOffsetUpdated(sync_offset_ms, estimated_freq_khz);
tommi2e82f382016-06-21 00:26:43 -0700465 }
tommi2e82f382016-06-21 00:26:43 -0700466 config_.renderer->OnFrame(video_frame);
pbos@webrtc.org29d58392013-05-16 12:08:03 +0000467
tommi2e82f382016-06-21 00:26:43 -0700468 // TODO(tommi): OnRenderFrame grabs a lock too.
asapersson1490f7a2016-09-23 02:09:46 -0700469 stats_proxy_.OnRenderedFrame(video_frame);
pbos@webrtc.org29d58392013-05-16 12:08:03 +0000470}
pbos@webrtc.org26c0c412014-09-03 16:17:12 +0000471
brandtr090c9402017-01-25 08:28:02 -0800472void VideoReceiveStream::SendNack(
473 const std::vector<uint16_t>& sequence_numbers) {
nisseb1f2ff92017-06-09 04:01:55 -0700474 rtp_video_stream_receiver_.RequestPacketRetransmit(sequence_numbers);
brandtr090c9402017-01-25 08:28:02 -0800475}
476
477void VideoReceiveStream::RequestKeyFrame() {
Niels Möller46879152019-01-07 15:54:47 +0100478 if (config_.media_transport) {
479 config_.media_transport->RequestKeyFrame(config_.rtp.remote_ssrc);
480 } else {
481 rtp_video_stream_receiver_.RequestKeyFrame();
482 }
brandtr090c9402017-01-25 08:28:02 -0800483}
484
485void VideoReceiveStream::OnCompleteFrame(
philipele7c891f2018-02-22 14:35:06 +0100486 std::unique_ptr<video_coding::EncodedFrame> frame) {
Ruslan Burakov493a6502019-02-27 15:32:48 +0100487 RTC_DCHECK_CALLED_SEQUENTIALLY(&network_sequence_checker_);
Ilya Nikolaevskiye6a2d942018-11-07 14:32:28 +0100488 // TODO(https://bugs.webrtc.org/9974): Consider removing this workaround.
489 int64_t time_now_ms = rtc::TimeMillis();
490 if (last_complete_frame_time_ms_ > 0 &&
491 time_now_ms - last_complete_frame_time_ms_ > kInactiveStreamThresholdMs) {
492 frame_buffer_->Clear();
493 }
494 last_complete_frame_time_ms_ = time_now_ms;
495
Ruslan Burakov493a6502019-02-27 15:32:48 +0100496 const PlayoutDelay& playout_delay = frame->EncodedImage().playout_delay_;
497 if (playout_delay.min_ms >= 0) {
498 rtc::CritScope cs(&playout_delay_lock_);
499 frame_minimum_playout_delay_ms_ = playout_delay.min_ms;
500 UpdatePlayoutDelays();
501 }
502
503 if (playout_delay.max_ms >= 0) {
504 rtc::CritScope cs(&playout_delay_lock_);
505 frame_maximum_playout_delay_ms_ = playout_delay.max_ms;
506 UpdatePlayoutDelays();
507 }
508
philipel1610f942017-12-12 13:58:31 +0100509 int64_t last_continuous_pid = frame_buffer_->InsertFrame(std::move(frame));
brandtr090c9402017-01-25 08:28:02 -0800510 if (last_continuous_pid != -1)
nisseb1f2ff92017-06-09 04:01:55 -0700511 rtp_video_stream_receiver_.FrameContinuous(last_continuous_pid);
brandtr090c9402017-01-25 08:28:02 -0800512}
513
Niels Möller46879152019-01-07 15:54:47 +0100514void VideoReceiveStream::OnData(uint64_t channel_id,
515 MediaTransportEncodedVideoFrame frame) {
516 OnCompleteFrame(
517 absl::make_unique<EncodedFrameForMediaTransport>(std::move(frame)));
518}
519
philipele21be1d2017-09-25 06:37:12 -0700520void VideoReceiveStream::OnRttUpdate(int64_t avg_rtt_ms, int64_t max_rtt_ms) {
Tommi81de14f2018-03-25 22:19:25 +0200521 RTC_DCHECK_CALLED_SEQUENTIALLY(&module_process_sequence_checker_);
philipele21be1d2017-09-25 06:37:12 -0700522 frame_buffer_->UpdateRtt(max_rtt_ms);
Tommi81de14f2018-03-25 22:19:25 +0200523 rtp_video_stream_receiver_.UpdateRtt(max_rtt_ms);
philipele21be1d2017-09-25 06:37:12 -0700524}
525
Niels Möller46879152019-01-07 15:54:47 +0100526void VideoReceiveStream::OnRttUpdated(int64_t rtt_ms) {
527 frame_buffer_->UpdateRtt(rtt_ms);
528}
529
solenberg3ebbcb52017-01-31 03:58:40 -0800530int VideoReceiveStream::id() const {
eladalona28122f2017-08-18 04:02:48 -0700531 RTC_DCHECK_CALLED_SEQUENTIALLY(&worker_sequence_checker_);
solenberg3ebbcb52017-01-31 03:58:40 -0800532 return config_.rtp.remote_ssrc;
533}
534
Danil Chapovalovb9b146c2018-06-15 12:28:07 +0200535absl::optional<Syncable::Info> VideoReceiveStream::GetInfo() const {
eladalona28122f2017-08-18 04:02:48 -0700536 RTC_DCHECK_CALLED_SEQUENTIALLY(&module_process_sequence_checker_);
Niels Möllerdf9e9ae2018-07-31 08:29:53 +0200537 absl::optional<Syncable::Info> info =
538 rtp_video_stream_receiver_.GetSyncInfo();
solenberg3ebbcb52017-01-31 03:58:40 -0800539
Niels Möllerdf9e9ae2018-07-31 08:29:53 +0200540 if (!info)
Danil Chapovalovb9b146c2018-06-15 12:28:07 +0200541 return absl::nullopt;
solenberg3ebbcb52017-01-31 03:58:40 -0800542
Niels Möllerdf9e9ae2018-07-31 08:29:53 +0200543 info->current_delay_ms = video_receiver_.Delay();
Oskar Sundbom8e07c132018-01-08 16:45:42 +0100544 return info;
solenberg3ebbcb52017-01-31 03:58:40 -0800545}
546
547uint32_t VideoReceiveStream::GetPlayoutTimestamp() const {
548 RTC_NOTREACHED();
549 return 0;
550}
551
552void VideoReceiveStream::SetMinimumPlayoutDelay(int delay_ms) {
eladalona28122f2017-08-18 04:02:48 -0700553 RTC_DCHECK_CALLED_SEQUENTIALLY(&module_process_sequence_checker_);
Ruslan Burakov493a6502019-02-27 15:32:48 +0100554 rtc::CritScope cs(&playout_delay_lock_);
555 syncable_minimum_playout_delay_ms_ = delay_ms;
556 UpdatePlayoutDelays();
solenberg3ebbcb52017-01-31 03:58:40 -0800557}
558
tommic8ece432017-06-20 02:44:38 -0700559void VideoReceiveStream::DecodeThreadFunction(void* ptr) {
Karl Wibergab036382019-03-12 18:01:51 +0100560 ScopedRegisterThreadForDebugging thread_dbg(RTC_FROM_HERE);
tommic8ece432017-06-20 02:44:38 -0700561 while (static_cast<VideoReceiveStream*>(ptr)->Decode()) {
562 }
Peter Boströmca835252016-02-11 15:59:46 +0100563}
564
philipel2dfea3e2017-02-28 07:19:43 -0800565bool VideoReceiveStream::Decode() {
tommidb23ea62017-03-03 07:21:18 -0800566 TRACE_EVENT0("webrtc", "VideoReceiveStream::Decode");
philipela45102f2017-02-22 05:30:39 -0800567 static const int kMaxWaitForFrameMs = 3000;
philipel3042c2d2017-08-18 04:55:02 -0700568 static const int kMaxWaitForKeyFrameMs = 200;
569
570 int wait_ms = keyframe_required_ ? kMaxWaitForKeyFrameMs : kMaxWaitForFrameMs;
philipele7c891f2018-02-22 14:35:06 +0100571 std::unique_ptr<video_coding::EncodedFrame> frame;
philipel3042c2d2017-08-18 04:55:02 -0700572 // TODO(philipel): Call NextFrame with |keyframe_required| argument when
573 // downstream project has been fixed.
guidouc3372582017-04-04 07:16:21 -0700574 video_coding::FrameBuffer::ReturnReason res =
philipel3042c2d2017-08-18 04:55:02 -0700575 frame_buffer_->NextFrame(wait_ms, &frame);
philipelfd5a20f2016-11-15 00:57:57 -0800576
guidouc3372582017-04-04 07:16:21 -0700577 if (res == video_coding::FrameBuffer::ReturnReason::kStopped) {
philipel2dfea3e2017-02-28 07:19:43 -0800578 return false;
guidouc3372582017-04-04 07:16:21 -0700579 }
philipelfd5a20f2016-11-15 00:57:57 -0800580
philipela45102f2017-02-22 05:30:39 -0800581 if (frame) {
philipel48462b62017-09-26 02:54:58 -0700582 int64_t now_ms = clock_->TimeInMilliseconds();
tommic8ece432017-06-20 02:44:38 -0700583 RTC_DCHECK_EQ(res, video_coding::FrameBuffer::ReturnReason::kFrameFound);
Niels Möller147013a2018-10-01 15:56:33 +0200584
585 // Current OnPreDecode only cares about QP for VP8.
586 int qp = -1;
587 if (frame->CodecSpecific()->codecType == kVideoCodecVP8) {
Niels Möller9c843902019-01-11 10:21:35 +0100588 if (!vp8::GetQp(frame->data(), frame->size(), &qp)) {
Niels Möller147013a2018-10-01 15:56:33 +0200589 RTC_LOG(LS_WARNING) << "Failed to extract QP from VP8 video frame";
590 }
591 }
592 stats_proxy_.OnPreDecode(frame->CodecSpecific()->codecType, qp);
593
philipel49b46e02017-12-11 14:52:58 +0100594 int decode_result = video_receiver_.Decode(frame.get());
595 if (decode_result == WEBRTC_VIDEO_CODEC_OK ||
596 decode_result == WEBRTC_VIDEO_CODEC_OK_REQUEST_KEYFRAME) {
philipel3042c2d2017-08-18 04:55:02 -0700597 keyframe_required_ = false;
598 frame_decoded_ = true;
philipel0fa82a62018-03-19 15:34:53 +0100599 rtp_video_stream_receiver_.FrameDecoded(frame->id.picture_id);
philipel49b46e02017-12-11 14:52:58 +0100600
601 if (decode_result == WEBRTC_VIDEO_CODEC_OK_REQUEST_KEYFRAME)
602 RequestKeyFrame();
philipel48462b62017-09-26 02:54:58 -0700603 } else if (!frame_decoded_ || !keyframe_required_ ||
604 (last_keyframe_request_ms_ + kMaxWaitForKeyFrameMs < now_ms)) {
philipel3042c2d2017-08-18 04:55:02 -0700605 keyframe_required_ = true;
606 // TODO(philipel): Remove this keyframe request when downstream project
607 // has been fixed.
608 RequestKeyFrame();
philipel48462b62017-09-26 02:54:58 -0700609 last_keyframe_request_ms_ = now_ms;
philipel3042c2d2017-08-18 04:55:02 -0700610 }
philipelfd5a20f2016-11-15 00:57:57 -0800611 } else {
tommic8ece432017-06-20 02:44:38 -0700612 RTC_DCHECK_EQ(res, video_coding::FrameBuffer::ReturnReason::kTimeout);
philipel3184f8e2017-05-18 08:08:53 -0700613 int64_t now_ms = clock_->TimeInMilliseconds();
Danil Chapovalovb9b146c2018-06-15 12:28:07 +0200614 absl::optional<int64_t> last_packet_ms =
nisseb1f2ff92017-06-09 04:01:55 -0700615 rtp_video_stream_receiver_.LastReceivedPacketMs();
Danil Chapovalovb9b146c2018-06-15 12:28:07 +0200616 absl::optional<int64_t> last_keyframe_packet_ms =
nisseb1f2ff92017-06-09 04:01:55 -0700617 rtp_video_stream_receiver_.LastReceivedKeyframePacketMs();
philipel3184f8e2017-05-18 08:08:53 -0700618
619 // To avoid spamming keyframe requests for a stream that is not active we
620 // check if we have received a packet within the last 5 seconds.
621 bool stream_is_active = last_packet_ms && now_ms - *last_packet_ms < 5000;
sprang3e86e7e2017-08-22 09:23:28 -0700622 if (!stream_is_active)
623 stats_proxy_.OnStreamInactive();
philipel3184f8e2017-05-18 08:08:53 -0700624
philipel3042c2d2017-08-18 04:55:02 -0700625 // If we recently have been receiving packets belonging to a keyframe then
626 // we assume a keyframe is currently being received.
philipel3184f8e2017-05-18 08:08:53 -0700627 bool receiving_keyframe =
628 last_keyframe_packet_ms &&
philipel3042c2d2017-08-18 04:55:02 -0700629 now_ms - *last_keyframe_packet_ms < kMaxWaitForKeyFrameMs;
philipel3184f8e2017-05-18 08:08:53 -0700630
Benjamin Wright52426ed2019-03-01 11:01:59 -0800631 if (stream_is_active && !receiving_keyframe &&
632 (!config_.crypto_options.sframe.require_frame_encryption ||
633 rtp_video_stream_receiver_.IsDecryptable())) {
Mirko Bonadei675513b2017-11-09 11:09:25 +0100634 RTC_LOG(LS_WARNING) << "No decodable frame in " << wait_ms
635 << " ms, requesting keyframe.";
philipel3184f8e2017-05-18 08:08:53 -0700636 RequestKeyFrame();
637 }
philipelfd5a20f2016-11-15 00:57:57 -0800638 }
philipel2dfea3e2017-02-28 07:19:43 -0800639 return true;
Peter Boströmca835252016-02-11 15:59:46 +0100640}
Jonas Oreland49ac5952018-09-26 16:04:32 +0200641
Ruslan Burakov493a6502019-02-27 15:32:48 +0100642void VideoReceiveStream::UpdatePlayoutDelays() const {
643 const int minimum_delay_ms =
644 std::max({frame_minimum_playout_delay_ms_, base_minimum_playout_delay_ms_,
645 syncable_minimum_playout_delay_ms_});
646 if (minimum_delay_ms >= 0) {
647 timing_->set_min_playout_delay(minimum_delay_ms);
648 }
649
650 const int maximum_delay_ms = frame_maximum_playout_delay_ms_;
651 if (maximum_delay_ms >= 0) {
652 timing_->set_max_playout_delay(maximum_delay_ms);
653 }
654}
655
Jonas Oreland49ac5952018-09-26 16:04:32 +0200656std::vector<webrtc::RtpSource> VideoReceiveStream::GetSources() const {
657 return rtp_video_stream_receiver_.GetSources();
658}
659
mflodman@webrtc.orgf3973e82013-12-13 09:40:45 +0000660} // namespace internal
661} // namespace webrtc