pbos@webrtc.org | 29d5839 | 2013-05-16 12:08:03 +0000 | [diff] [blame] | 1 | /* |
| 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 Bonadei | 92ea95e | 2017-09-15 06:47:31 +0200 | [diff] [blame] | 11 | #include "video/video_receive_stream.h" |
pbos@webrtc.org | 29d5839 | 2013-05-16 12:08:03 +0000 | [diff] [blame] | 12 | |
pbos@webrtc.org | 12dc1a3 | 2013-08-05 16:22:53 +0000 | [diff] [blame] | 13 | #include <stdlib.h> |
Yves Gerey | 3e70781 | 2018-11-28 16:47:49 +0100 | [diff] [blame] | 14 | #include <string.h> |
Jonas Olsson | a4d8737 | 2019-07-05 19:08:33 +0200 | [diff] [blame] | 15 | |
Niels Möller | 4687915 | 2019-01-07 15:54:47 +0100 | [diff] [blame] | 16 | #include <algorithm> |
Mirko Bonadei | 317a1f0 | 2019-09-17 17:06:18 +0200 | [diff] [blame] | 17 | #include <memory> |
mflodman | d1590b2 | 2015-12-09 07:07:59 -0800 | [diff] [blame] | 18 | #include <set> |
mflodman@webrtc.org | b429e51 | 2013-12-18 09:46:22 +0000 | [diff] [blame] | 19 | #include <string> |
Erik Språng | 737336d | 2016-07-29 12:59:36 +0200 | [diff] [blame] | 20 | #include <utility> |
mflodman@webrtc.org | b429e51 | 2013-12-18 09:46:22 +0000 | [diff] [blame] | 21 | |
Steve Anton | bd631a0 | 2019-03-28 10:51:27 -0700 | [diff] [blame] | 22 | #include "absl/algorithm/container.h" |
Danil Chapovalov | b9b146c | 2018-06-15 12:28:07 +0200 | [diff] [blame] | 23 | #include "absl/types/optional.h" |
Yves Gerey | 3e70781 | 2018-11-28 16:47:49 +0100 | [diff] [blame] | 24 | #include "api/array_view.h" |
Steve Anton | 10542f2 | 2019-01-11 09:11:00 -0800 | [diff] [blame] | 25 | #include "api/crypto/frame_decryptor_interface.h" |
Yves Gerey | 3e70781 | 2018-11-28 16:47:49 +0100 | [diff] [blame] | 26 | #include "api/video/encoded_image.h" |
| 27 | #include "api/video_codecs/sdp_video_format.h" |
| 28 | #include "api/video_codecs/video_codec.h" |
Niels Möller | cbcbc22 | 2018-09-28 09:07:24 +0200 | [diff] [blame] | 29 | #include "api/video_codecs/video_decoder_factory.h" |
Yves Gerey | 3e70781 | 2018-11-28 16:47:49 +0100 | [diff] [blame] | 30 | #include "api/video_codecs/video_encoder.h" |
Mirko Bonadei | 92ea95e | 2017-09-15 06:47:31 +0200 | [diff] [blame] | 31 | #include "call/rtp_stream_receiver_controller_interface.h" |
| 32 | #include "call/rtx_receive_stream.h" |
Patrik Höglund | be214a2 | 2018-01-04 12:14:35 +0100 | [diff] [blame] | 33 | #include "common_video/include/incoming_video_stream.h" |
Yves Gerey | 3e70781 | 2018-11-28 16:47:49 +0100 | [diff] [blame] | 34 | #include "media/base/h264_profile_level_id.h" |
Mirko Bonadei | 92ea95e | 2017-09-15 06:47:31 +0200 | [diff] [blame] | 35 | #include "modules/utility/include/process_thread.h" |
Yves Gerey | 3e70781 | 2018-11-28 16:47:49 +0100 | [diff] [blame] | 36 | #include "modules/video_coding/include/video_codec_interface.h" |
| 37 | #include "modules/video_coding/include/video_coding_defines.h" |
| 38 | #include "modules/video_coding/include/video_error_codes.h" |
Mirko Bonadei | 92ea95e | 2017-09-15 06:47:31 +0200 | [diff] [blame] | 39 | #include "modules/video_coding/timing.h" |
Niels Möller | 147013a | 2018-10-01 15:56:33 +0200 | [diff] [blame] | 40 | #include "modules/video_coding/utility/vp8_header_parser.h" |
Mirko Bonadei | 92ea95e | 2017-09-15 06:47:31 +0200 | [diff] [blame] | 41 | #include "rtc_base/checks.h" |
Rasmus Brandt | 3dde450 | 2019-03-21 11:46:17 +0100 | [diff] [blame] | 42 | #include "rtc_base/experiments/keyframe_interval_settings.h" |
Mirko Bonadei | 92ea95e | 2017-09-15 06:47:31 +0200 | [diff] [blame] | 43 | #include "rtc_base/location.h" |
| 44 | #include "rtc_base/logging.h" |
Erik Språng | 96965ae | 2018-10-23 15:42:37 +0200 | [diff] [blame] | 45 | #include "rtc_base/strings/string_builder.h" |
Karl Wiberg | ab03638 | 2019-03-12 18:01:51 +0100 | [diff] [blame] | 46 | #include "rtc_base/system/thread_registry.h" |
Steve Anton | 10542f2 | 2019-01-11 09:11:00 -0800 | [diff] [blame] | 47 | #include "rtc_base/time_utils.h" |
Mirko Bonadei | 92ea95e | 2017-09-15 06:47:31 +0200 | [diff] [blame] | 48 | #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ång | 96965ae | 2018-10-23 15:42:37 +0200 | [diff] [blame] | 52 | #include "video/frame_dumping_decoder.h" |
Mirko Bonadei | 92ea95e | 2017-09-15 06:47:31 +0200 | [diff] [blame] | 53 | #include "video/receive_statistics_proxy.h" |
pbos@webrtc.org | 29d5839 | 2013-05-16 12:08:03 +0000 | [diff] [blame] | 54 | |
| 55 | namespace webrtc { |
mflodman | a20de20 | 2015-10-18 22:08:19 -0700 | [diff] [blame] | 56 | |
pbos@webrtc.org | 776e6f2 | 2014-10-29 15:28:39 +0000 | [diff] [blame] | 57 | namespace { |
Sebastian Jansson | 1c747f5 | 2019-04-04 13:01:39 +0200 | [diff] [blame] | 58 | |
| 59 | using video_coding::EncodedFrame; |
| 60 | using ReturnReason = video_coding::FrameBuffer::ReturnReason; |
| 61 | |
Ruslan Burakov | 493a650 | 2019-02-27 15:32:48 +0100 | [diff] [blame] | 62 | constexpr int kMinBaseMinimumDelayMs = 0; |
| 63 | constexpr int kMaxBaseMinimumDelayMs = 10000; |
| 64 | |
Rasmus Brandt | 3dde450 | 2019-03-21 11:46:17 +0100 | [diff] [blame] | 65 | constexpr int kMaxWaitForKeyFrameMs = 200; |
| 66 | constexpr int kMaxWaitForFrameMs = 3000; |
| 67 | |
pbos@webrtc.org | 776e6f2 | 2014-10-29 15:28:39 +0000 | [diff] [blame] | 68 | VideoCodec CreateDecoderVideoCodec(const VideoReceiveStream::Decoder& decoder) { |
| 69 | VideoCodec codec; |
| 70 | memset(&codec, 0, sizeof(codec)); |
| 71 | |
| 72 | codec.plType = decoder.payload_type; |
Niels Möller | cb7e1d2 | 2018-09-11 15:56:04 +0200 | [diff] [blame] | 73 | codec.codecType = PayloadStringToCodecType(decoder.video_format.name); |
pbos@webrtc.org | 776e6f2 | 2014-10-29 15:28:39 +0000 | [diff] [blame] | 74 | |
| 75 | if (codec.codecType == kVideoCodecVP8) { |
hta | 257dc39 | 2016-10-25 09:05:06 -0700 | [diff] [blame] | 76 | *(codec.VP8()) = VideoEncoder::GetDefaultVp8Settings(); |
ivica | 05cfcd3 | 2015-09-07 06:04:16 -0700 | [diff] [blame] | 77 | } else if (codec.codecType == kVideoCodecVP9) { |
hta | 257dc39 | 2016-10-25 09:05:06 -0700 | [diff] [blame] | 78 | *(codec.VP9()) = VideoEncoder::GetDefaultVp9Settings(); |
pbos@webrtc.org | 776e6f2 | 2014-10-29 15:28:39 +0000 | [diff] [blame] | 79 | } else if (codec.codecType == kVideoCodecH264) { |
hta | 257dc39 | 2016-10-25 09:05:06 -0700 | [diff] [blame] | 80 | *(codec.H264()) = VideoEncoder::GetDefaultH264Settings(); |
Emircan Uysaler | d7ae3c3 | 2018-01-25 13:01:09 -0800 | [diff] [blame] | 81 | } else if (codec.codecType == kVideoCodecMultiplex) { |
Emircan Uysaler | 0a37547 | 2017-12-11 12:21:02 +0530 | [diff] [blame] | 82 | VideoReceiveStream::Decoder associated_decoder = decoder; |
Niels Möller | cb7e1d2 | 2018-09-11 15:56:04 +0200 | [diff] [blame] | 83 | associated_decoder.video_format = |
| 84 | SdpVideoFormat(CodecTypeToPayloadString(kVideoCodecVP9)); |
Emircan Uysaler | 0a37547 | 2017-12-11 12:21:02 +0530 | [diff] [blame] | 85 | VideoCodec associated_codec = CreateDecoderVideoCodec(associated_decoder); |
Emircan Uysaler | d7ae3c3 | 2018-01-25 13:01:09 -0800 | [diff] [blame] | 86 | associated_codec.codecType = kVideoCodecMultiplex; |
Emircan Uysaler | 0a37547 | 2017-12-11 12:21:02 +0530 | [diff] [blame] | 87 | return associated_codec; |
pbos@webrtc.org | 776e6f2 | 2014-10-29 15:28:39 +0000 | [diff] [blame] | 88 | } |
| 89 | |
| 90 | codec.width = 320; |
| 91 | codec.height = 180; |
stefan | b4bc65b | 2016-11-02 10:10:20 -0700 | [diff] [blame] | 92 | const int kDefaultStartBitrate = 300; |
pbos@webrtc.org | 776e6f2 | 2014-10-29 15:28:39 +0000 | [diff] [blame] | 93 | codec.startBitrate = codec.minBitrate = codec.maxBitrate = |
stefan | b4bc65b | 2016-11-02 10:10:20 -0700 | [diff] [blame] | 94 | kDefaultStartBitrate; |
pbos@webrtc.org | 776e6f2 | 2014-10-29 15:28:39 +0000 | [diff] [blame] | 95 | |
| 96 | return codec; |
| 97 | } |
Niels Möller | cbcbc22 | 2018-09-28 09:07:24 +0200 | [diff] [blame] | 98 | |
| 99 | // Video decoder class to be used for unknown codecs. Doesn't support decoding |
| 100 | // but logs messages to LS_ERROR. |
| 101 | class 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öller | cbcbc22 | 2018-09-28 09:07:24 +0200 | [diff] [blame] | 111 | 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öller | 4687915 | 2019-01-07 15:54:47 +0100 | [diff] [blame] | 128 | // 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. |
| 131 | class EncodedFrameForMediaTransport : public video_coding::EncodedFrame { |
| 132 | public: |
| 133 | explicit EncodedFrameForMediaTransport( |
| 134 | MediaTransportEncodedVideoFrame frame) { |
Niels Möller | 938dd9f | 2019-02-07 00:02:17 +0100 | [diff] [blame] | 135 | // 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öller | 4687915 | 2019-01-07 15:54:47 +0100 | [diff] [blame] | 139 | *static_cast<class EncodedImage*>(this) = frame.encoded_image(); |
Niels Möller | 4687915 | 2019-01-07 15:54:47 +0100 | [diff] [blame] | 140 | |
Niels Möller | 69bb3af | 2019-02-13 10:05:49 +0100 | [diff] [blame] | 141 | // If we don't already own the buffer, make a copy. |
| 142 | Retain(); |
Niels Möller | 4687915 | 2019-01-07 15:54:47 +0100 | [diff] [blame] | 143 | |
| 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 Nikolaevskiy | 815e00c | 2019-11-12 15:20:21 +0000 | [diff] [blame] | 162 | // TODO(https://bugs.webrtc.org/9974): Consider removing this workaround. |
Ilya Nikolaevskiy | e6a2d94 | 2018-11-07 14:32:28 +0100 | [diff] [blame] | 163 | // Maximum time between frames before resetting the FrameBuffer to avoid RTP |
Ilya Nikolaevskiy | 815e00c | 2019-11-12 15:20:21 +0000 | [diff] [blame] | 164 | // timestamps wraparound to affect FrameBuffer. |
| 165 | constexpr int kInactiveStreamThresholdMs = 600000; // 10 minutes. |
Ilya Nikolaevskiy | e6a2d94 | 2018-11-07 14:32:28 +0100 | [diff] [blame] | 166 | |
pbos@webrtc.org | 776e6f2 | 2014-10-29 15:28:39 +0000 | [diff] [blame] | 167 | } // namespace |
pbos@webrtc.org | 29d5839 | 2013-05-16 12:08:03 +0000 | [diff] [blame] | 168 | |
Peter Boström | ca83525 | 2016-02-11 15:59:46 +0100 | [diff] [blame] | 169 | namespace internal { |
tommi | 2e82f38 | 2016-06-21 00:26:43 -0700 | [diff] [blame] | 170 | |
nisse | 0f15f92 | 2017-06-21 01:05:22 -0700 | [diff] [blame] | 171 | VideoReceiveStream::VideoReceiveStream( |
Sebastian Jansson | 74682c1 | 2019-03-01 11:50:20 +0100 | [diff] [blame] | 172 | TaskQueueFactory* task_queue_factory, |
nisse | 0f15f92 | 2017-06-21 01:05:22 -0700 | [diff] [blame] | 173 | RtpStreamReceiverControllerInterface* receiver_controller, |
| 174 | int num_cpu_cores, |
| 175 | PacketRouter* packet_router, |
| 176 | VideoReceiveStream::Config config, |
| 177 | ProcessThread* process_thread, |
Ruslan Burakov | 493a650 | 2019-02-27 15:32:48 +0100 | [diff] [blame] | 178 | CallStats* call_stats, |
| 179 | Clock* clock, |
| 180 | VCMTiming* timing) |
Sebastian Jansson | 74682c1 | 2019-03-01 11:50:20 +0100 | [diff] [blame] | 181 | : task_queue_factory_(task_queue_factory), |
| 182 | transport_adapter_(config.rtcp_send_transport), |
Tommi | 733b547 | 2016-06-10 17:58:01 +0200 | [diff] [blame] | 183 | config_(std::move(config)), |
sprang | 113bdca | 2016-10-11 03:10:10 -0700 | [diff] [blame] | 184 | num_cpu_cores_(num_cpu_cores), |
Peter Boström | 1d04ac6 | 2016-02-05 11:25:46 +0100 | [diff] [blame] | 185 | process_thread_(process_thread), |
Ruslan Burakov | 493a650 | 2019-02-27 15:32:48 +0100 | [diff] [blame] | 186 | clock_(clock), |
Peter Boström | 1d04ac6 | 2016-02-05 11:25:46 +0100 | [diff] [blame] | 187 | call_stats_(call_stats), |
Chen Xing | 90f3b89 | 2019-06-25 10:16:14 +0200 | [diff] [blame] | 188 | source_tracker_(clock_), |
Danil Chapovalov | 8ce0d2b | 2018-11-23 11:03:25 +0100 | [diff] [blame] | 189 | stats_proxy_(&config_, clock_), |
Niels Möller | d781965 | 2019-08-13 14:43:02 +0200 | [diff] [blame] | 190 | rtp_receive_statistics_(ReceiveStatistics::Create(clock_)), |
Ruslan Burakov | 493a650 | 2019-02-27 15:32:48 +0100 | [diff] [blame] | 191 | timing_(timing), |
Niels Möller | db64d99 | 2019-03-29 14:30:53 +0100 | [diff] [blame] | 192 | video_receiver_(clock_, timing_.get()), |
Sebastian Jansson | 8026d60 | 2019-03-04 19:39:01 +0100 | [diff] [blame] | 193 | rtp_video_stream_receiver_(clock_, |
| 194 | &transport_adapter_, |
Tommi | 38c5d93 | 2018-03-27 23:11:09 +0200 | [diff] [blame] | 195 | call_stats, |
nisse | b1f2ff9 | 2017-06-09 04:01:55 -0700 | [diff] [blame] | 196 | packet_router, |
| 197 | &config_, |
nisse | ca5706d | 2017-09-11 02:32:16 -0700 | [diff] [blame] | 198 | rtp_receive_statistics_.get(), |
nisse | b1f2ff9 | 2017-06-09 04:01:55 -0700 | [diff] [blame] | 199 | &stats_proxy_, |
| 200 | process_thread_, |
Niels Möller | 2f5554d | 2019-05-29 13:35:14 +0200 | [diff] [blame] | 201 | this, // NackSender |
| 202 | nullptr, // Use default KeyFrameRequestSender |
| 203 | this, // OnCompleteFrameCallback |
Benjamin Wright | 192eeec | 2018-10-17 17:27:25 -0700 | [diff] [blame] | 204 | config_.frame_decryptor), |
Rasmus Brandt | 3dde450 | 2019-03-21 11:46:17 +0100 | [diff] [blame] | 205 | rtp_stream_sync_(this), |
| 206 | max_wait_for_keyframe_ms_(KeyframeIntervalSettings::ParseFromFieldTrials() |
| 207 | .MaxWaitForKeyframeMs() |
| 208 | .value_or(kMaxWaitForKeyFrameMs)), |
| 209 | max_wait_for_frame_ms_(KeyframeIntervalSettings::ParseFromFieldTrials() |
| 210 | .MaxWaitForFrameMs() |
Sebastian Jansson | 11d0d7b | 2019-04-11 12:39:34 +0200 | [diff] [blame] | 211 | .value_or(kMaxWaitForFrameMs)), |
| 212 | decode_queue_(task_queue_factory_->CreateTaskQueue( |
| 213 | "DecodingQueue", |
| 214 | TaskQueueFactory::Priority::HIGH)) { |
Mirko Bonadei | 675513b | 2017-11-09 11:09:25 +0100 | [diff] [blame] | 215 | RTC_LOG(LS_INFO) << "VideoReceiveStream: " << config_.ToString(); |
pbos@webrtc.org | 29d5839 | 2013-05-16 12:08:03 +0000 | [diff] [blame] | 216 | |
Rasmus Brandt | 1e27fec | 2019-01-23 09:47:50 +0100 | [diff] [blame] | 217 | RTC_DCHECK(config_.renderer); |
Stefan Holmer | 58c664c | 2016-02-08 14:31:30 +0100 | [diff] [blame] | 218 | RTC_DCHECK(process_thread_); |
Stefan Holmer | 58c664c | 2016-02-08 14:31:30 +0100 | [diff] [blame] | 219 | RTC_DCHECK(call_stats_); |
mflodman | cfc8e3b | 2016-05-03 21:22:04 -0700 | [diff] [blame] | 220 | |
eladalon | a28122f | 2017-08-18 04:02:48 -0700 | [diff] [blame] | 221 | module_process_sequence_checker_.Detach(); |
Ruslan Burakov | 493a650 | 2019-02-27 15:32:48 +0100 | [diff] [blame] | 222 | network_sequence_checker_.Detach(); |
solenberg | 3ebbcb5 | 2017-01-31 03:58:40 -0800 | [diff] [blame] | 223 | |
henrikg | 91d6ede | 2015-09-17 00:24:34 -0700 | [diff] [blame] | 224 | RTC_DCHECK(!config_.decoders.empty()); |
Peter Boström | 521af4e | 2015-11-27 16:35:04 +0100 | [diff] [blame] | 225 | std::set<int> decoder_payload_types; |
Peter Boström | b1ae3a4 | 2016-02-15 17:52:40 +0100 | [diff] [blame] | 226 | for (const Decoder& decoder : config_.decoders) { |
Niels Möller | cbcbc22 | 2018-09-28 09:07:24 +0200 | [diff] [blame] | 227 | RTC_CHECK(decoder.decoder_factory); |
Peter Boström | 521af4e | 2015-11-27 16:35:04 +0100 | [diff] [blame] | 228 | RTC_CHECK(decoder_payload_types.find(decoder.payload_type) == |
| 229 | decoder_payload_types.end()) |
| 230 | << "Duplicate payload type (" << decoder.payload_type |
| 231 | << ") for different decoders."; |
| 232 | decoder_payload_types.insert(decoder.payload_type); |
pbos@webrtc.org | 0181b5f | 2013-09-09 08:26:30 +0000 | [diff] [blame] | 233 | } |
| 234 | |
Niels Möller | bcb7c20 | 2019-04-18 12:54:04 +0200 | [diff] [blame] | 235 | timing_->set_render_delay(config_.render_delay_ms); |
Peter Boström | 1d04ac6 | 2016-02-05 11:25:46 +0100 | [diff] [blame] | 236 | |
Niels Möller | d9c2d94 | 2019-04-30 09:16:36 +0200 | [diff] [blame] | 237 | frame_buffer_.reset( |
| 238 | new video_coding::FrameBuffer(clock_, timing_.get(), &stats_proxy_)); |
philipel | fd5a20f | 2016-11-15 00:57:57 -0800 | [diff] [blame] | 239 | |
tommi | dea489f | 2017-03-03 03:20:24 -0800 | [diff] [blame] | 240 | process_thread_->RegisterModule(&rtp_stream_sync_, RTC_FROM_HERE); |
Anton Sukhanov | 4f08faa | 2019-05-21 11:12:57 -0700 | [diff] [blame] | 241 | if (config_.media_transport()) { |
| 242 | config_.media_transport()->SetReceiveVideoSink(this); |
| 243 | config_.media_transport()->AddRttObserver(this); |
Niels Möller | 5304a32 | 2018-08-27 13:27:05 +0200 | [diff] [blame] | 244 | } else { |
Niels Möller | 4687915 | 2019-01-07 15:54:47 +0100 | [diff] [blame] | 245 | // Register with RtpStreamReceiverController. |
| 246 | media_receiver_ = receiver_controller->CreateReceiver( |
| 247 | config_.rtp.remote_ssrc, &rtp_video_stream_receiver_); |
| 248 | if (config_.rtp.rtx_ssrc) { |
Mirko Bonadei | 317a1f0 | 2019-09-17 17:06:18 +0200 | [diff] [blame] | 249 | rtx_receive_stream_ = std::make_unique<RtxReceiveStream>( |
Niels Möller | 4687915 | 2019-01-07 15:54:47 +0100 | [diff] [blame] | 250 | &rtp_video_stream_receiver_, config.rtp.rtx_associated_payload_types, |
| 251 | config_.rtp.remote_ssrc, rtp_receive_statistics_.get()); |
| 252 | rtx_receiver_ = receiver_controller->CreateReceiver( |
| 253 | config_.rtp.rtx_ssrc, rtx_receive_stream_.get()); |
| 254 | } else { |
| 255 | rtp_receive_statistics_->EnableRetransmitDetection(config.rtp.remote_ssrc, |
| 256 | true); |
| 257 | } |
nisse | 0f15f92 | 2017-06-21 01:05:22 -0700 | [diff] [blame] | 258 | } |
pbos@webrtc.org | 29d5839 | 2013-05-16 12:08:03 +0000 | [diff] [blame] | 259 | } |
| 260 | |
Ruslan Burakov | 493a650 | 2019-02-27 15:32:48 +0100 | [diff] [blame] | 261 | VideoReceiveStream::VideoReceiveStream( |
Sebastian Jansson | 74682c1 | 2019-03-01 11:50:20 +0100 | [diff] [blame] | 262 | TaskQueueFactory* task_queue_factory, |
Ruslan Burakov | 493a650 | 2019-02-27 15:32:48 +0100 | [diff] [blame] | 263 | RtpStreamReceiverControllerInterface* receiver_controller, |
| 264 | int num_cpu_cores, |
| 265 | PacketRouter* packet_router, |
| 266 | VideoReceiveStream::Config config, |
| 267 | ProcessThread* process_thread, |
Sebastian Jansson | 8026d60 | 2019-03-04 19:39:01 +0100 | [diff] [blame] | 268 | CallStats* call_stats, |
| 269 | Clock* clock) |
Sebastian Jansson | 74682c1 | 2019-03-01 11:50:20 +0100 | [diff] [blame] | 270 | : VideoReceiveStream(task_queue_factory, |
| 271 | receiver_controller, |
Ruslan Burakov | 493a650 | 2019-02-27 15:32:48 +0100 | [diff] [blame] | 272 | num_cpu_cores, |
| 273 | packet_router, |
| 274 | std::move(config), |
| 275 | process_thread, |
| 276 | call_stats, |
Sebastian Jansson | 8026d60 | 2019-03-04 19:39:01 +0100 | [diff] [blame] | 277 | clock, |
| 278 | new VCMTiming(clock)) {} |
Ruslan Burakov | 493a650 | 2019-02-27 15:32:48 +0100 | [diff] [blame] | 279 | |
pbos@webrtc.org | 29d5839 | 2013-05-16 12:08:03 +0000 | [diff] [blame] | 280 | VideoReceiveStream::~VideoReceiveStream() { |
Sebastian Jansson | b55015e | 2019-04-09 13:44:04 +0200 | [diff] [blame] | 281 | RTC_DCHECK_RUN_ON(&worker_sequence_checker_); |
Mirko Bonadei | 675513b | 2017-11-09 11:09:25 +0100 | [diff] [blame] | 282 | RTC_LOG(LS_INFO) << "~VideoReceiveStream: " << config_.ToString(); |
Peter Boström | ca83525 | 2016-02-11 15:59:46 +0100 | [diff] [blame] | 283 | Stop(); |
Anton Sukhanov | 4f08faa | 2019-05-21 11:12:57 -0700 | [diff] [blame] | 284 | if (config_.media_transport()) { |
| 285 | config_.media_transport()->SetReceiveVideoSink(nullptr); |
| 286 | config_.media_transport()->RemoveRttObserver(this); |
Niels Möller | 4687915 | 2019-01-07 15:54:47 +0100 | [diff] [blame] | 287 | } |
mflodman | 4cd2790 | 2016-08-05 06:28:45 -0700 | [diff] [blame] | 288 | process_thread_->DeRegisterModule(&rtp_stream_sync_); |
pbos@webrtc.org | 29d5839 | 2013-05-16 12:08:03 +0000 | [diff] [blame] | 289 | } |
| 290 | |
pbos | 1ba8d39 | 2016-05-01 20:18:34 -0700 | [diff] [blame] | 291 | void VideoReceiveStream::SignalNetworkState(NetworkState state) { |
Sebastian Jansson | b55015e | 2019-04-09 13:44:04 +0200 | [diff] [blame] | 292 | RTC_DCHECK_RUN_ON(&worker_sequence_checker_); |
nisse | b1f2ff9 | 2017-06-09 04:01:55 -0700 | [diff] [blame] | 293 | rtp_video_stream_receiver_.SignalNetworkState(state); |
pbos | 1ba8d39 | 2016-05-01 20:18:34 -0700 | [diff] [blame] | 294 | } |
| 295 | |
pbos | 1ba8d39 | 2016-05-01 20:18:34 -0700 | [diff] [blame] | 296 | bool VideoReceiveStream::DeliverRtcp(const uint8_t* packet, size_t length) { |
nisse | b1f2ff9 | 2017-06-09 04:01:55 -0700 | [diff] [blame] | 297 | return rtp_video_stream_receiver_.DeliverRtcp(packet, length); |
pbos | 1ba8d39 | 2016-05-01 20:18:34 -0700 | [diff] [blame] | 298 | } |
| 299 | |
solenberg | 3ebbcb5 | 2017-01-31 03:58:40 -0800 | [diff] [blame] | 300 | void VideoReceiveStream::SetSync(Syncable* audio_syncable) { |
Sebastian Jansson | b55015e | 2019-04-09 13:44:04 +0200 | [diff] [blame] | 301 | RTC_DCHECK_RUN_ON(&worker_sequence_checker_); |
solenberg | 3ebbcb5 | 2017-01-31 03:58:40 -0800 | [diff] [blame] | 302 | rtp_stream_sync_.ConfigureSync(audio_syncable); |
brandtr | 090c940 | 2017-01-25 08:28:02 -0800 | [diff] [blame] | 303 | } |
| 304 | |
pbos@webrtc.org | a5c8d2c | 2014-04-24 11:13:21 +0000 | [diff] [blame] | 305 | void VideoReceiveStream::Start() { |
Sebastian Jansson | b55015e | 2019-04-09 13:44:04 +0200 | [diff] [blame] | 306 | RTC_DCHECK_RUN_ON(&worker_sequence_checker_); |
brandtr | fb45c6c | 2017-01-27 06:47:55 -0800 | [diff] [blame] | 307 | |
Sebastian Jansson | 11d0d7b | 2019-04-11 12:39:34 +0200 | [diff] [blame] | 308 | if (decoder_running_) { |
Rasmus Brandt | 1e27fec | 2019-01-23 09:47:50 +0100 | [diff] [blame] | 309 | return; |
| 310 | } |
| 311 | |
| 312 | const bool protected_by_fec = config_.rtp.protected_by_flexfec || |
| 313 | rtp_video_stream_receiver_.IsUlpfecEnabled(); |
brandtr | fb45c6c | 2017-01-27 06:47:55 -0800 | [diff] [blame] | 314 | |
philipel | a45102f | 2017-02-22 05:30:39 -0800 | [diff] [blame] | 315 | frame_buffer_->Start(); |
philipel | fd5a20f | 2016-11-15 00:57:57 -0800 | [diff] [blame] | 316 | |
nisse | b1f2ff9 | 2017-06-09 04:01:55 -0700 | [diff] [blame] | 317 | if (rtp_video_stream_receiver_.IsRetransmissionsEnabled() && |
| 318 | protected_by_fec) { |
philipel | a45102f | 2017-02-22 05:30:39 -0800 | [diff] [blame] | 319 | frame_buffer_->SetProtectionMode(kProtectionNackFEC); |
philipel | fd5a20f | 2016-11-15 00:57:57 -0800 | [diff] [blame] | 320 | } |
philipel | a45102f | 2017-02-22 05:30:39 -0800 | [diff] [blame] | 321 | |
sprang@webrtc.org | d9b9560 | 2014-01-27 13:03:02 +0000 | [diff] [blame] | 322 | transport_adapter_.Enable(); |
tommi | 2e82f38 | 2016-06-21 00:26:43 -0700 | [diff] [blame] | 323 | rtc::VideoSinkInterface<VideoFrame>* renderer = nullptr; |
Rasmus Brandt | 1e27fec | 2019-01-23 09:47:50 +0100 | [diff] [blame] | 324 | if (config_.enable_prerenderer_smoothing) { |
Sebastian Jansson | 74682c1 | 2019-03-01 11:50:20 +0100 | [diff] [blame] | 325 | incoming_video_stream_.reset(new IncomingVideoStream( |
| 326 | task_queue_factory_, config_.render_delay_ms, this)); |
Rasmus Brandt | 1e27fec | 2019-01-23 09:47:50 +0100 | [diff] [blame] | 327 | renderer = incoming_video_stream_.get(); |
| 328 | } else { |
| 329 | renderer = this; |
tommi | 2e82f38 | 2016-06-21 00:26:43 -0700 | [diff] [blame] | 330 | } |
| 331 | |
sprang | 113bdca | 2016-10-11 03:10:10 -0700 | [diff] [blame] | 332 | for (const Decoder& decoder : config_.decoders) { |
Niels Möller | cbcbc22 | 2018-09-28 09:07:24 +0200 | [diff] [blame] | 333 | std::unique_ptr<VideoDecoder> video_decoder = |
| 334 | decoder.decoder_factory->LegacyCreateVideoDecoder(decoder.video_format, |
| 335 | config_.stream_id); |
| 336 | // If we still have no valid decoder, we have to create a "Null" decoder |
| 337 | // that ignores all calls. The reason we can get into this state is that the |
| 338 | // old decoder factory interface doesn't have a way to query supported |
| 339 | // codecs. |
| 340 | if (!video_decoder) { |
Mirko Bonadei | 317a1f0 | 2019-09-17 17:06:18 +0200 | [diff] [blame] | 341 | video_decoder = std::make_unique<NullVideoDecoder>(); |
Niels Möller | cbcbc22 | 2018-09-28 09:07:24 +0200 | [diff] [blame] | 342 | } |
Erik Språng | 96965ae | 2018-10-23 15:42:37 +0200 | [diff] [blame] | 343 | |
| 344 | std::string decoded_output_file = |
| 345 | field_trial::FindFullName("WebRTC-DecoderDataDumpDirectory"); |
Ilya Nikolaevskiy | 9a57350 | 2019-05-28 12:58:28 +0200 | [diff] [blame] | 346 | // Because '/' can't be used inside a field trial parameter, we use ';' |
Ilya Nikolaevskiy | c032109 | 2019-03-11 11:18:26 +0100 | [diff] [blame] | 347 | // instead. |
Ilya Nikolaevskiy | 9a57350 | 2019-05-28 12:58:28 +0200 | [diff] [blame] | 348 | // This is only relevant to WebRTC-DecoderDataDumpDirectory |
| 349 | // field trial. ';' is chosen arbitrary. Even though it's a legal character |
| 350 | // in some file systems, we can sacrifice ability to use it in the path to |
| 351 | // dumped video, since it's developers-only feature for debugging. |
| 352 | absl::c_replace(decoded_output_file, ';', '/'); |
Erik Språng | 96965ae | 2018-10-23 15:42:37 +0200 | [diff] [blame] | 353 | if (!decoded_output_file.empty()) { |
| 354 | char filename_buffer[256]; |
| 355 | rtc::SimpleStringBuilder ssb(filename_buffer); |
| 356 | ssb << decoded_output_file << "/webrtc_receive_stream_" |
Ilya Nikolaevskiy | c032109 | 2019-03-11 11:18:26 +0100 | [diff] [blame] | 357 | << this->config_.rtp.remote_ssrc << "-" << rtc::TimeMicros() |
| 358 | << ".ivf"; |
Markus Handell | 1c2f637 | 2019-08-20 20:21:37 +0200 | [diff] [blame] | 359 | video_decoder = CreateFrameDumpingDecoderWrapper( |
Niels Möller | b7edf69 | 2019-02-08 16:40:53 +0100 | [diff] [blame] | 360 | std::move(video_decoder), FileWrapper::OpenWriteOnly(ssb.str())); |
Erik Språng | 96965ae | 2018-10-23 15:42:37 +0200 | [diff] [blame] | 361 | } |
| 362 | |
Niels Möller | cbcbc22 | 2018-09-28 09:07:24 +0200 | [diff] [blame] | 363 | video_decoders_.push_back(std::move(video_decoder)); |
| 364 | |
| 365 | video_receiver_.RegisterExternalDecoder(video_decoders_.back().get(), |
sprang | 113bdca | 2016-10-11 03:10:10 -0700 | [diff] [blame] | 366 | decoder.payload_type); |
sprang | 113bdca | 2016-10-11 03:10:10 -0700 | [diff] [blame] | 367 | VideoCodec codec = CreateDecoderVideoCodec(decoder); |
Mirta Dvornicic | fe68daa | 2019-05-23 13:21:12 +0200 | [diff] [blame] | 368 | |
| 369 | const bool raw_payload = |
| 370 | config_.rtp.raw_payload_types.count(codec.plType) > 0; |
| 371 | rtp_video_stream_receiver_.AddReceiveCodec( |
| 372 | codec, decoder.video_format.parameters, raw_payload); |
sprang | 113bdca | 2016-10-11 03:10:10 -0700 | [diff] [blame] | 373 | RTC_CHECK_EQ(VCM_OK, video_receiver_.RegisterReceiveCodec( |
| 374 | &codec, num_cpu_cores_, false)); |
| 375 | } |
| 376 | |
Rasmus Brandt | 1e27fec | 2019-01-23 09:47:50 +0100 | [diff] [blame] | 377 | RTC_DCHECK(renderer != nullptr); |
Niels Möller | c29fa1b | 2019-04-16 16:47:08 +0200 | [diff] [blame] | 378 | video_stream_decoder_.reset( |
| 379 | new VideoStreamDecoder(&video_receiver_, &stats_proxy_, renderer)); |
tommi | c8ece43 | 2017-06-20 02:44:38 -0700 | [diff] [blame] | 380 | |
Tommi | ef3e28a | 2018-03-30 08:42:39 +0200 | [diff] [blame] | 381 | // Make sure we register as a stats observer *after* we've prepared the |
| 382 | // |video_stream_decoder_|. |
| 383 | call_stats_->RegisterStatsObserver(this); |
| 384 | |
Sebastian Jansson | de5f639 | 2019-09-10 10:31:07 +0200 | [diff] [blame] | 385 | // Start decoding on task queue. |
Tommi | fbf3bce | 2018-02-21 15:56:05 +0100 | [diff] [blame] | 386 | video_receiver_.DecoderThreadStarting(); |
Tommi | 132e28e | 2018-02-24 17:57:33 +0100 | [diff] [blame] | 387 | stats_proxy_.DecoderThreadStarting(); |
Sebastian Jansson | de5f639 | 2019-09-10 10:31:07 +0200 | [diff] [blame] | 388 | decode_queue_.PostTask([this] { |
| 389 | RTC_DCHECK_RUN_ON(&decode_queue_); |
| 390 | decoder_stopped_ = false; |
| 391 | StartNextDecode(); |
| 392 | }); |
Sebastian Jansson | 11d0d7b | 2019-04-11 12:39:34 +0200 | [diff] [blame] | 393 | decoder_running_ = true; |
nisse | b1f2ff9 | 2017-06-09 04:01:55 -0700 | [diff] [blame] | 394 | rtp_video_stream_receiver_.StartReceive(); |
pbos@webrtc.org | 29d5839 | 2013-05-16 12:08:03 +0000 | [diff] [blame] | 395 | } |
| 396 | |
pbos@webrtc.org | a5c8d2c | 2014-04-24 11:13:21 +0000 | [diff] [blame] | 397 | void VideoReceiveStream::Stop() { |
Sebastian Jansson | b55015e | 2019-04-09 13:44:04 +0200 | [diff] [blame] | 398 | RTC_DCHECK_RUN_ON(&worker_sequence_checker_); |
nisse | b1f2ff9 | 2017-06-09 04:01:55 -0700 | [diff] [blame] | 399 | rtp_video_stream_receiver_.StopReceive(); |
philipel | fd5a20f | 2016-11-15 00:57:57 -0800 | [diff] [blame] | 400 | |
Ilya Nikolaevskiy | d397a0d | 2018-02-21 15:57:09 +0100 | [diff] [blame] | 401 | stats_proxy_.OnUniqueFramesCounted( |
| 402 | rtp_video_stream_receiver_.GetUniqueFramesSeen()); |
| 403 | |
Sebastian Jansson | de5f639 | 2019-09-10 10:31:07 +0200 | [diff] [blame] | 404 | decode_queue_.PostTask([this] { frame_buffer_->Stop(); }); |
| 405 | |
philipel | e21be1d | 2017-09-25 06:37:12 -0700 | [diff] [blame] | 406 | call_stats_->DeregisterStatsObserver(this); |
philipel | fd5a20f | 2016-11-15 00:57:57 -0800 | [diff] [blame] | 407 | |
Sebastian Jansson | 11d0d7b | 2019-04-11 12:39:34 +0200 | [diff] [blame] | 408 | if (decoder_running_) { |
Sebastian Jansson | de5f639 | 2019-09-10 10:31:07 +0200 | [diff] [blame] | 409 | rtc::Event done; |
| 410 | decode_queue_.PostTask([this, &done] { |
| 411 | RTC_DCHECK_RUN_ON(&decode_queue_); |
| 412 | decoder_stopped_ = true; |
| 413 | done.Set(); |
| 414 | }); |
| 415 | done.Wait(rtc::Event::kForever); |
| 416 | |
Sebastian Jansson | 11d0d7b | 2019-04-11 12:39:34 +0200 | [diff] [blame] | 417 | decoder_running_ = false; |
Tommi | fbf3bce | 2018-02-21 15:56:05 +0100 | [diff] [blame] | 418 | video_receiver_.DecoderThreadStopped(); |
Tommi | 132e28e | 2018-02-24 17:57:33 +0100 | [diff] [blame] | 419 | stats_proxy_.DecoderThreadStopped(); |
sprang | 0d348d6 | 2016-10-07 08:28:39 -0700 | [diff] [blame] | 420 | // Deregister external decoders so they are no longer running during |
| 421 | // destruction. This effectively stops the VCM since the decoder thread is |
| 422 | // stopped, the VCM is deregistered and no asynchronous decoder threads are |
| 423 | // running. |
| 424 | for (const Decoder& decoder : config_.decoders) |
| 425 | video_receiver_.RegisterExternalDecoder(nullptr, decoder.payload_type); |
Niels Möller | 9a9f18a | 2019-08-02 13:52:37 +0200 | [diff] [blame] | 426 | |
| 427 | UpdateHistograms(); |
sprang | 0d348d6 | 2016-10-07 08:28:39 -0700 | [diff] [blame] | 428 | } |
philipel | fd5a20f | 2016-11-15 00:57:57 -0800 | [diff] [blame] | 429 | |
tommi | 2e82f38 | 2016-06-21 00:26:43 -0700 | [diff] [blame] | 430 | video_stream_decoder_.reset(); |
| 431 | incoming_video_stream_.reset(); |
sprang@webrtc.org | d9b9560 | 2014-01-27 13:03:02 +0000 | [diff] [blame] | 432 | transport_adapter_.Disable(); |
pbos@webrtc.org | 29d5839 | 2013-05-16 12:08:03 +0000 | [diff] [blame] | 433 | } |
| 434 | |
sprang@webrtc.org | 9510e53 | 2014-02-07 15:32:45 +0000 | [diff] [blame] | 435 | VideoReceiveStream::Stats VideoReceiveStream::GetStats() const { |
Niels Möller | 12ebfa6 | 2019-08-06 16:04:12 +0200 | [diff] [blame] | 436 | VideoReceiveStream::Stats stats = stats_proxy_.GetStats(); |
Niels Möller | a52e9bd | 2019-08-09 08:31:50 +0200 | [diff] [blame] | 437 | stats.total_bitrate_bps = 0; |
Niels Möller | 12ebfa6 | 2019-08-06 16:04:12 +0200 | [diff] [blame] | 438 | StreamStatistician* statistician = |
| 439 | rtp_receive_statistics_->GetStatistician(stats.ssrc); |
Niels Möller | a52e9bd | 2019-08-09 08:31:50 +0200 | [diff] [blame] | 440 | if (statistician) { |
Niels Möller | d77cc24 | 2019-08-22 09:40:25 +0200 | [diff] [blame] | 441 | stats.rtp_stats = statistician->GetStats(); |
Niels Möller | a52e9bd | 2019-08-09 08:31:50 +0200 | [diff] [blame] | 442 | stats.total_bitrate_bps = statistician->BitrateReceived(); |
| 443 | } |
| 444 | if (config_.rtp.rtx_ssrc) { |
| 445 | StreamStatistician* rtx_statistician = |
| 446 | rtp_receive_statistics_->GetStatistician(config_.rtp.rtx_ssrc); |
| 447 | if (rtx_statistician) |
| 448 | stats.total_bitrate_bps += rtx_statistician->BitrateReceived(); |
| 449 | } |
Niels Möller | 12ebfa6 | 2019-08-06 16:04:12 +0200 | [diff] [blame] | 450 | return stats; |
sprang@webrtc.org | 0931570 | 2014-02-07 12:06:29 +0000 | [diff] [blame] | 451 | } |
| 452 | |
Niels Möller | 9a9f18a | 2019-08-02 13:52:37 +0200 | [diff] [blame] | 453 | void VideoReceiveStream::UpdateHistograms() { |
| 454 | absl::optional<int> fraction_lost; |
Niels Möller | d781965 | 2019-08-13 14:43:02 +0200 | [diff] [blame] | 455 | StreamDataCounters rtp_stats; |
Niels Möller | 9a9f18a | 2019-08-02 13:52:37 +0200 | [diff] [blame] | 456 | StreamStatistician* statistician = |
| 457 | rtp_receive_statistics_->GetStatistician(config_.rtp.remote_ssrc); |
| 458 | if (statistician) { |
| 459 | fraction_lost = statistician->GetFractionLostInPercent(); |
Niels Möller | d781965 | 2019-08-13 14:43:02 +0200 | [diff] [blame] | 460 | rtp_stats = statistician->GetReceiveStreamDataCounters(); |
Niels Möller | 9a9f18a | 2019-08-02 13:52:37 +0200 | [diff] [blame] | 461 | } |
Niels Möller | d781965 | 2019-08-13 14:43:02 +0200 | [diff] [blame] | 462 | if (config_.rtp.rtx_ssrc) { |
| 463 | StreamStatistician* rtx_statistician = |
| 464 | rtp_receive_statistics_->GetStatistician(config_.rtp.rtx_ssrc); |
| 465 | if (rtx_statistician) { |
| 466 | StreamDataCounters rtx_stats = |
| 467 | rtx_statistician->GetReceiveStreamDataCounters(); |
| 468 | stats_proxy_.UpdateHistograms(fraction_lost, rtp_stats, &rtx_stats); |
| 469 | return; |
| 470 | } |
| 471 | } |
| 472 | stats_proxy_.UpdateHistograms(fraction_lost, rtp_stats, nullptr); |
Niels Möller | 9a9f18a | 2019-08-02 13:52:37 +0200 | [diff] [blame] | 473 | } |
| 474 | |
eladalon | c0d481a | 2017-08-02 07:39:07 -0700 | [diff] [blame] | 475 | void VideoReceiveStream::AddSecondarySink(RtpPacketSinkInterface* sink) { |
| 476 | rtp_video_stream_receiver_.AddSecondarySink(sink); |
| 477 | } |
| 478 | |
| 479 | void VideoReceiveStream::RemoveSecondarySink( |
| 480 | const RtpPacketSinkInterface* sink) { |
| 481 | rtp_video_stream_receiver_.RemoveSecondarySink(sink); |
| 482 | } |
| 483 | |
Ruslan Burakov | 493a650 | 2019-02-27 15:32:48 +0100 | [diff] [blame] | 484 | bool VideoReceiveStream::SetBaseMinimumPlayoutDelayMs(int delay_ms) { |
Sebastian Jansson | b55015e | 2019-04-09 13:44:04 +0200 | [diff] [blame] | 485 | RTC_DCHECK_RUN_ON(&worker_sequence_checker_); |
Ruslan Burakov | 493a650 | 2019-02-27 15:32:48 +0100 | [diff] [blame] | 486 | if (delay_ms < kMinBaseMinimumDelayMs || delay_ms > kMaxBaseMinimumDelayMs) { |
| 487 | return false; |
| 488 | } |
| 489 | |
| 490 | rtc::CritScope cs(&playout_delay_lock_); |
| 491 | base_minimum_playout_delay_ms_ = delay_ms; |
| 492 | UpdatePlayoutDelays(); |
| 493 | return true; |
| 494 | } |
| 495 | |
| 496 | int VideoReceiveStream::GetBaseMinimumPlayoutDelayMs() const { |
Sebastian Jansson | b55015e | 2019-04-09 13:44:04 +0200 | [diff] [blame] | 497 | RTC_DCHECK_RUN_ON(&worker_sequence_checker_); |
Ruslan Burakov | 493a650 | 2019-02-27 15:32:48 +0100 | [diff] [blame] | 498 | |
| 499 | rtc::CritScope cs(&playout_delay_lock_); |
| 500 | return base_minimum_playout_delay_ms_; |
| 501 | } |
| 502 | |
tommi | 2e82f38 | 2016-06-21 00:26:43 -0700 | [diff] [blame] | 503 | // TODO(tommi): This method grabs a lock 6 times. |
Tommi | bd3380f | 2016-06-10 17:38:17 +0200 | [diff] [blame] | 504 | void VideoReceiveStream::OnFrame(const VideoFrame& video_frame) { |
Åsa Persson | fcf79cc | 2019-10-22 15:23:44 +0200 | [diff] [blame] | 505 | int64_t video_playout_ntp_ms; |
asapersson | f8cdd18 | 2016-03-15 01:00:47 -0700 | [diff] [blame] | 506 | int64_t sync_offset_ms; |
asapersson | de9e5ff | 2016-11-02 07:14:03 -0700 | [diff] [blame] | 507 | double estimated_freq_khz; |
tommi | 2e82f38 | 2016-06-21 00:26:43 -0700 | [diff] [blame] | 508 | // TODO(tommi): GetStreamSyncOffsetInMs grabs three locks. One inside the |
| 509 | // function itself, another in GetChannel() and a third in |
| 510 | // GetPlayoutTimestamp. Seems excessive. Anyhow, I'm assuming the function |
| 511 | // succeeds most of the time, which leads to grabbing a fourth lock. |
Yves Gerey | 665174f | 2018-06-19 15:03:05 +0200 | [diff] [blame] | 512 | if (rtp_stream_sync_.GetStreamSyncOffsetInMs( |
| 513 | video_frame.timestamp(), video_frame.render_time_ms(), |
Åsa Persson | fcf79cc | 2019-10-22 15:23:44 +0200 | [diff] [blame] | 514 | &video_playout_ntp_ms, &sync_offset_ms, &estimated_freq_khz)) { |
tommi | 2e82f38 | 2016-06-21 00:26:43 -0700 | [diff] [blame] | 515 | // TODO(tommi): OnSyncOffsetUpdated grabs a lock. |
Åsa Persson | fcf79cc | 2019-10-22 15:23:44 +0200 | [diff] [blame] | 516 | stats_proxy_.OnSyncOffsetUpdated(video_playout_ntp_ms, sync_offset_ms, |
| 517 | estimated_freq_khz); |
tommi | 2e82f38 | 2016-06-21 00:26:43 -0700 | [diff] [blame] | 518 | } |
Jonas Oreland | 9755f97 | 2019-08-16 15:54:05 +0200 | [diff] [blame] | 519 | source_tracker_.OnFrameDelivered(video_frame.packet_infos()); |
| 520 | |
tommi | 2e82f38 | 2016-06-21 00:26:43 -0700 | [diff] [blame] | 521 | config_.renderer->OnFrame(video_frame); |
pbos@webrtc.org | 29d5839 | 2013-05-16 12:08:03 +0000 | [diff] [blame] | 522 | |
tommi | 2e82f38 | 2016-06-21 00:26:43 -0700 | [diff] [blame] | 523 | // TODO(tommi): OnRenderFrame grabs a lock too. |
asapersson | 1490f7a | 2016-09-23 02:09:46 -0700 | [diff] [blame] | 524 | stats_proxy_.OnRenderedFrame(video_frame); |
pbos@webrtc.org | 29d5839 | 2013-05-16 12:08:03 +0000 | [diff] [blame] | 525 | } |
pbos@webrtc.org | 26c0c41 | 2014-09-03 16:17:12 +0000 | [diff] [blame] | 526 | |
Benjamin Wright | a556448 | 2019-04-03 10:44:18 -0700 | [diff] [blame] | 527 | void VideoReceiveStream::SetFrameDecryptor( |
| 528 | rtc::scoped_refptr<webrtc::FrameDecryptorInterface> frame_decryptor) { |
| 529 | rtp_video_stream_receiver_.SetFrameDecryptor(std::move(frame_decryptor)); |
| 530 | } |
| 531 | |
Elad Alon | ef09c5b | 2019-05-31 13:25:50 +0200 | [diff] [blame] | 532 | void VideoReceiveStream::SendNack(const std::vector<uint16_t>& sequence_numbers, |
| 533 | bool buffering_allowed) { |
| 534 | RTC_DCHECK(buffering_allowed); |
nisse | b1f2ff9 | 2017-06-09 04:01:55 -0700 | [diff] [blame] | 535 | rtp_video_stream_receiver_.RequestPacketRetransmit(sequence_numbers); |
brandtr | 090c940 | 2017-01-25 08:28:02 -0800 | [diff] [blame] | 536 | } |
| 537 | |
Markus Handell | 4186603 | 2019-11-18 11:10:11 +0100 | [diff] [blame] | 538 | void VideoReceiveStream::RequestKeyFrame(int64_t timestamp_ms) { |
Anton Sukhanov | 4f08faa | 2019-05-21 11:12:57 -0700 | [diff] [blame] | 539 | if (config_.media_transport()) { |
| 540 | config_.media_transport()->RequestKeyFrame(config_.rtp.remote_ssrc); |
Niels Möller | 4687915 | 2019-01-07 15:54:47 +0100 | [diff] [blame] | 541 | } else { |
| 542 | rtp_video_stream_receiver_.RequestKeyFrame(); |
| 543 | } |
Markus Handell | 4186603 | 2019-11-18 11:10:11 +0100 | [diff] [blame] | 544 | last_keyframe_request_ms_ = timestamp_ms; |
brandtr | 090c940 | 2017-01-25 08:28:02 -0800 | [diff] [blame] | 545 | } |
| 546 | |
| 547 | void VideoReceiveStream::OnCompleteFrame( |
philipel | e7c891f | 2018-02-22 14:35:06 +0100 | [diff] [blame] | 548 | std::unique_ptr<video_coding::EncodedFrame> frame) { |
Sebastian Jansson | b55015e | 2019-04-09 13:44:04 +0200 | [diff] [blame] | 549 | RTC_DCHECK_RUN_ON(&network_sequence_checker_); |
Ilya Nikolaevskiy | 815e00c | 2019-11-12 15:20:21 +0000 | [diff] [blame] | 550 | // TODO(https://bugs.webrtc.org/9974): Consider removing this workaround. |
Ilya Nikolaevskiy | e6a2d94 | 2018-11-07 14:32:28 +0100 | [diff] [blame] | 551 | int64_t time_now_ms = rtc::TimeMillis(); |
| 552 | if (last_complete_frame_time_ms_ > 0 && |
| 553 | time_now_ms - last_complete_frame_time_ms_ > kInactiveStreamThresholdMs) { |
| 554 | frame_buffer_->Clear(); |
| 555 | } |
| 556 | last_complete_frame_time_ms_ = time_now_ms; |
| 557 | |
Ruslan Burakov | 493a650 | 2019-02-27 15:32:48 +0100 | [diff] [blame] | 558 | const PlayoutDelay& playout_delay = frame->EncodedImage().playout_delay_; |
Johannes Kron | 3b69817 | 2019-08-28 12:41:11 +0000 | [diff] [blame] | 559 | if (playout_delay.min_ms >= 0) { |
Ruslan Burakov | 493a650 | 2019-02-27 15:32:48 +0100 | [diff] [blame] | 560 | rtc::CritScope cs(&playout_delay_lock_); |
| 561 | frame_minimum_playout_delay_ms_ = playout_delay.min_ms; |
Johannes Kron | 3b69817 | 2019-08-28 12:41:11 +0000 | [diff] [blame] | 562 | UpdatePlayoutDelays(); |
| 563 | } |
| 564 | |
| 565 | if (playout_delay.max_ms >= 0) { |
| 566 | rtc::CritScope cs(&playout_delay_lock_); |
Ruslan Burakov | 493a650 | 2019-02-27 15:32:48 +0100 | [diff] [blame] | 567 | frame_maximum_playout_delay_ms_ = playout_delay.max_ms; |
| 568 | UpdatePlayoutDelays(); |
| 569 | } |
Henrik Boström | c680c4a | 2019-04-03 10:27:36 +0000 | [diff] [blame] | 570 | |
| 571 | int64_t last_continuous_pid = frame_buffer_->InsertFrame(std::move(frame)); |
| 572 | if (last_continuous_pid != -1) |
| 573 | rtp_video_stream_receiver_.FrameContinuous(last_continuous_pid); |
brandtr | 090c940 | 2017-01-25 08:28:02 -0800 | [diff] [blame] | 574 | } |
| 575 | |
Niels Möller | 4687915 | 2019-01-07 15:54:47 +0100 | [diff] [blame] | 576 | void VideoReceiveStream::OnData(uint64_t channel_id, |
| 577 | MediaTransportEncodedVideoFrame frame) { |
| 578 | OnCompleteFrame( |
Mirko Bonadei | 317a1f0 | 2019-09-17 17:06:18 +0200 | [diff] [blame] | 579 | std::make_unique<EncodedFrameForMediaTransport>(std::move(frame))); |
Niels Möller | 4687915 | 2019-01-07 15:54:47 +0100 | [diff] [blame] | 580 | } |
| 581 | |
philipel | e21be1d | 2017-09-25 06:37:12 -0700 | [diff] [blame] | 582 | void VideoReceiveStream::OnRttUpdate(int64_t avg_rtt_ms, int64_t max_rtt_ms) { |
Sebastian Jansson | b55015e | 2019-04-09 13:44:04 +0200 | [diff] [blame] | 583 | RTC_DCHECK_RUN_ON(&module_process_sequence_checker_); |
philipel | e21be1d | 2017-09-25 06:37:12 -0700 | [diff] [blame] | 584 | frame_buffer_->UpdateRtt(max_rtt_ms); |
Tommi | 81de14f | 2018-03-25 22:19:25 +0200 | [diff] [blame] | 585 | rtp_video_stream_receiver_.UpdateRtt(max_rtt_ms); |
philipel | e21be1d | 2017-09-25 06:37:12 -0700 | [diff] [blame] | 586 | } |
| 587 | |
Niels Möller | 4687915 | 2019-01-07 15:54:47 +0100 | [diff] [blame] | 588 | void VideoReceiveStream::OnRttUpdated(int64_t rtt_ms) { |
| 589 | frame_buffer_->UpdateRtt(rtt_ms); |
| 590 | } |
| 591 | |
solenberg | 3ebbcb5 | 2017-01-31 03:58:40 -0800 | [diff] [blame] | 592 | int VideoReceiveStream::id() const { |
Sebastian Jansson | b55015e | 2019-04-09 13:44:04 +0200 | [diff] [blame] | 593 | RTC_DCHECK_RUN_ON(&worker_sequence_checker_); |
solenberg | 3ebbcb5 | 2017-01-31 03:58:40 -0800 | [diff] [blame] | 594 | return config_.rtp.remote_ssrc; |
| 595 | } |
| 596 | |
Danil Chapovalov | b9b146c | 2018-06-15 12:28:07 +0200 | [diff] [blame] | 597 | absl::optional<Syncable::Info> VideoReceiveStream::GetInfo() const { |
Sebastian Jansson | b55015e | 2019-04-09 13:44:04 +0200 | [diff] [blame] | 598 | RTC_DCHECK_RUN_ON(&module_process_sequence_checker_); |
Niels Möller | df9e9ae | 2018-07-31 08:29:53 +0200 | [diff] [blame] | 599 | absl::optional<Syncable::Info> info = |
| 600 | rtp_video_stream_receiver_.GetSyncInfo(); |
solenberg | 3ebbcb5 | 2017-01-31 03:58:40 -0800 | [diff] [blame] | 601 | |
Niels Möller | df9e9ae | 2018-07-31 08:29:53 +0200 | [diff] [blame] | 602 | if (!info) |
Danil Chapovalov | b9b146c | 2018-06-15 12:28:07 +0200 | [diff] [blame] | 603 | return absl::nullopt; |
solenberg | 3ebbcb5 | 2017-01-31 03:58:40 -0800 | [diff] [blame] | 604 | |
Niels Möller | bcb7c20 | 2019-04-18 12:54:04 +0200 | [diff] [blame] | 605 | info->current_delay_ms = timing_->TargetVideoDelay(); |
Oskar Sundbom | 8e07c13 | 2018-01-08 16:45:42 +0100 | [diff] [blame] | 606 | return info; |
solenberg | 3ebbcb5 | 2017-01-31 03:58:40 -0800 | [diff] [blame] | 607 | } |
| 608 | |
Åsa Persson | fcf79cc | 2019-10-22 15:23:44 +0200 | [diff] [blame] | 609 | bool VideoReceiveStream::GetPlayoutRtpTimestamp(uint32_t* rtp_timestamp, |
| 610 | int64_t* time_ms) const { |
solenberg | 3ebbcb5 | 2017-01-31 03:58:40 -0800 | [diff] [blame] | 611 | RTC_NOTREACHED(); |
| 612 | return 0; |
| 613 | } |
| 614 | |
Åsa Persson | fcf79cc | 2019-10-22 15:23:44 +0200 | [diff] [blame] | 615 | void VideoReceiveStream::SetEstimatedPlayoutNtpTimestampMs( |
| 616 | int64_t ntp_timestamp_ms, |
| 617 | int64_t time_ms) { |
| 618 | RTC_NOTREACHED(); |
| 619 | } |
| 620 | |
solenberg | 3ebbcb5 | 2017-01-31 03:58:40 -0800 | [diff] [blame] | 621 | void VideoReceiveStream::SetMinimumPlayoutDelay(int delay_ms) { |
Sebastian Jansson | b55015e | 2019-04-09 13:44:04 +0200 | [diff] [blame] | 622 | RTC_DCHECK_RUN_ON(&module_process_sequence_checker_); |
Ruslan Burakov | 493a650 | 2019-02-27 15:32:48 +0100 | [diff] [blame] | 623 | rtc::CritScope cs(&playout_delay_lock_); |
| 624 | syncable_minimum_playout_delay_ms_ = delay_ms; |
| 625 | UpdatePlayoutDelays(); |
solenberg | 3ebbcb5 | 2017-01-31 03:58:40 -0800 | [diff] [blame] | 626 | } |
| 627 | |
Sebastian Jansson | 1c747f5 | 2019-04-04 13:01:39 +0200 | [diff] [blame] | 628 | int64_t VideoReceiveStream::GetWaitMs() const { |
| 629 | return keyframe_required_ ? max_wait_for_keyframe_ms_ |
| 630 | : max_wait_for_frame_ms_; |
| 631 | } |
Sebastian Jansson | 11d0d7b | 2019-04-11 12:39:34 +0200 | [diff] [blame] | 632 | |
| 633 | void VideoReceiveStream::StartNextDecode() { |
Sebastian Jansson | 11d0d7b | 2019-04-11 12:39:34 +0200 | [diff] [blame] | 634 | TRACE_EVENT0("webrtc", "VideoReceiveStream::StartNextDecode"); |
Sebastian Jansson | 11d0d7b | 2019-04-11 12:39:34 +0200 | [diff] [blame] | 635 | frame_buffer_->NextFrame( |
philipel | cd936fd | 2019-05-02 13:53:10 +0200 | [diff] [blame] | 636 | GetWaitMs(), keyframe_required_, &decode_queue_, |
Sebastian Jansson | ee5ec9a | 2019-09-17 20:34:03 +0200 | [diff] [blame] | 637 | /* encoded frame handler */ |
Sebastian Jansson | 11d0d7b | 2019-04-11 12:39:34 +0200 | [diff] [blame] | 638 | [this](std::unique_ptr<EncodedFrame> frame, ReturnReason res) { |
| 639 | RTC_DCHECK_EQ(frame == nullptr, res == ReturnReason::kTimeout); |
| 640 | RTC_DCHECK_EQ(frame != nullptr, res == ReturnReason::kFrameFound); |
Sebastian Jansson | ee5ec9a | 2019-09-17 20:34:03 +0200 | [diff] [blame] | 641 | decode_queue_.PostTask([this, frame = std::move(frame)]() mutable { |
| 642 | RTC_DCHECK_RUN_ON(&decode_queue_); |
| 643 | if (decoder_stopped_) |
| 644 | return; |
| 645 | if (frame) { |
| 646 | HandleEncodedFrame(std::move(frame)); |
| 647 | } else { |
| 648 | HandleFrameBufferTimeout(); |
| 649 | } |
| 650 | StartNextDecode(); |
| 651 | }); |
Sebastian Jansson | 11d0d7b | 2019-04-11 12:39:34 +0200 | [diff] [blame] | 652 | }); |
| 653 | } |
| 654 | |
Sebastian Jansson | 1c747f5 | 2019-04-04 13:01:39 +0200 | [diff] [blame] | 655 | void VideoReceiveStream::HandleEncodedFrame( |
| 656 | std::unique_ptr<EncodedFrame> frame) { |
| 657 | int64_t now_ms = clock_->TimeInMilliseconds(); |
| 658 | |
| 659 | // Current OnPreDecode only cares about QP for VP8. |
| 660 | int qp = -1; |
| 661 | if (frame->CodecSpecific()->codecType == kVideoCodecVP8) { |
| 662 | if (!vp8::GetQp(frame->data(), frame->size(), &qp)) { |
| 663 | RTC_LOG(LS_WARNING) << "Failed to extract QP from VP8 video frame"; |
| 664 | } |
| 665 | } |
| 666 | stats_proxy_.OnPreDecode(frame->CodecSpecific()->codecType, qp); |
| 667 | |
| 668 | int decode_result = video_receiver_.Decode(frame.get()); |
| 669 | if (decode_result == WEBRTC_VIDEO_CODEC_OK || |
| 670 | decode_result == WEBRTC_VIDEO_CODEC_OK_REQUEST_KEYFRAME) { |
| 671 | keyframe_required_ = false; |
| 672 | frame_decoded_ = true; |
| 673 | rtp_video_stream_receiver_.FrameDecoded(frame->id.picture_id); |
| 674 | |
| 675 | if (decode_result == WEBRTC_VIDEO_CODEC_OK_REQUEST_KEYFRAME) |
Markus Handell | 4186603 | 2019-11-18 11:10:11 +0100 | [diff] [blame] | 676 | RequestKeyFrame(now_ms); |
Sebastian Jansson | 1c747f5 | 2019-04-04 13:01:39 +0200 | [diff] [blame] | 677 | } else if (!frame_decoded_ || !keyframe_required_ || |
| 678 | (last_keyframe_request_ms_ + max_wait_for_keyframe_ms_ < now_ms)) { |
| 679 | keyframe_required_ = true; |
| 680 | // TODO(philipel): Remove this keyframe request when downstream project |
| 681 | // has been fixed. |
Markus Handell | 4186603 | 2019-11-18 11:10:11 +0100 | [diff] [blame] | 682 | RequestKeyFrame(now_ms); |
Sebastian Jansson | 1c747f5 | 2019-04-04 13:01:39 +0200 | [diff] [blame] | 683 | } |
| 684 | } |
| 685 | |
| 686 | void VideoReceiveStream::HandleFrameBufferTimeout() { |
| 687 | int64_t now_ms = clock_->TimeInMilliseconds(); |
| 688 | absl::optional<int64_t> last_packet_ms = |
| 689 | rtp_video_stream_receiver_.LastReceivedPacketMs(); |
| 690 | absl::optional<int64_t> last_keyframe_packet_ms = |
| 691 | rtp_video_stream_receiver_.LastReceivedKeyframePacketMs(); |
| 692 | |
| 693 | // To avoid spamming keyframe requests for a stream that is not active we |
| 694 | // check if we have received a packet within the last 5 seconds. |
| 695 | bool stream_is_active = last_packet_ms && now_ms - *last_packet_ms < 5000; |
| 696 | if (!stream_is_active) |
| 697 | stats_proxy_.OnStreamInactive(); |
| 698 | |
| 699 | // If we recently have been receiving packets belonging to a keyframe then |
| 700 | // we assume a keyframe is currently being received. |
| 701 | bool receiving_keyframe = |
| 702 | last_keyframe_packet_ms && |
| 703 | now_ms - *last_keyframe_packet_ms < max_wait_for_keyframe_ms_; |
| 704 | |
| 705 | if (stream_is_active && !receiving_keyframe && |
| 706 | (!config_.crypto_options.sframe.require_frame_encryption || |
| 707 | rtp_video_stream_receiver_.IsDecryptable())) { |
| 708 | RTC_LOG(LS_WARNING) << "No decodable frame in " << GetWaitMs() |
| 709 | << " ms, requesting keyframe."; |
Markus Handell | 4186603 | 2019-11-18 11:10:11 +0100 | [diff] [blame] | 710 | RequestKeyFrame(now_ms); |
Sebastian Jansson | 1c747f5 | 2019-04-04 13:01:39 +0200 | [diff] [blame] | 711 | } |
| 712 | } |
| 713 | |
Ruslan Burakov | 493a650 | 2019-02-27 15:32:48 +0100 | [diff] [blame] | 714 | void VideoReceiveStream::UpdatePlayoutDelays() const { |
Johannes Kron | 3b69817 | 2019-08-28 12:41:11 +0000 | [diff] [blame] | 715 | const int minimum_delay_ms = |
Ruslan Burakov | 493a650 | 2019-02-27 15:32:48 +0100 | [diff] [blame] | 716 | std::max({frame_minimum_playout_delay_ms_, base_minimum_playout_delay_ms_, |
| 717 | syncable_minimum_playout_delay_ms_}); |
Johannes Kron | 87bed47 | 2019-08-28 12:40:45 +0000 | [diff] [blame] | 718 | if (minimum_delay_ms >= 0) { |
| 719 | timing_->set_min_playout_delay(minimum_delay_ms); |
| 720 | } |
Johannes Kron | 3b69817 | 2019-08-28 12:41:11 +0000 | [diff] [blame] | 721 | |
| 722 | const int maximum_delay_ms = frame_maximum_playout_delay_ms_; |
| 723 | if (maximum_delay_ms >= 0) { |
| 724 | timing_->set_max_playout_delay(maximum_delay_ms); |
| 725 | } |
Ruslan Burakov | 493a650 | 2019-02-27 15:32:48 +0100 | [diff] [blame] | 726 | } |
| 727 | |
Jonas Oreland | 49ac595 | 2018-09-26 16:04:32 +0200 | [diff] [blame] | 728 | std::vector<webrtc::RtpSource> VideoReceiveStream::GetSources() const { |
Chen Xing | 90f3b89 | 2019-06-25 10:16:14 +0200 | [diff] [blame] | 729 | return source_tracker_.GetSources(); |
Jonas Oreland | 49ac595 | 2018-09-26 16:04:32 +0200 | [diff] [blame] | 730 | } |
| 731 | |
mflodman@webrtc.org | f3973e8 | 2013-12-13 09:40:45 +0000 | [diff] [blame] | 732 | } // namespace internal |
| 733 | } // namespace webrtc |