blob: e5205afd91f219625201107d3cef956e9ea247ee [file] [log] [blame]
niklase@google.com470e71d2011-07-07 08:21:25 +00001/*
stefan@webrtc.org91c63082012-01-31 10:49:08 +00002 * Copyright (c) 2012 The WebRTC project authors. All Rights Reserved.
niklase@google.com470e71d2011-07-07 08:21:25 +00003 *
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 "modules/video_coding/receiver.h"
niklase@google.com470e71d2011-07-07 08:21:25 +000012
13#include <assert.h>
Yves Gerey3e707812018-11-28 16:47:49 +010014#include <cstdint>
pbos@webrtc.org3f655aa2014-03-18 11:10:11 +000015#include <cstdlib>
kwiberg0eb15ed2015-12-17 03:04:15 -080016#include <utility>
philipel9d3ab612015-12-21 04:12:39 -080017#include <vector>
pbos@webrtc.org3f655aa2014-03-18 11:10:11 +000018
Yves Gerey3e707812018-11-28 16:47:49 +010019#include "absl/memory/memory.h"
20#include "api/video/encoded_image.h"
21#include "common_types.h" // NOLINT(build/include)
Mirko Bonadei92ea95e2017-09-15 06:47:31 +020022#include "modules/video_coding/encoded_frame.h"
23#include "modules/video_coding/internal_defines.h"
Yves Gerey3e707812018-11-28 16:47:49 +010024#include "modules/video_coding/jitter_buffer_common.h"
Mirko Bonadei92ea95e2017-09-15 06:47:31 +020025#include "rtc_base/logging.h"
Ilya Nikolaevskiy8c4fe162018-02-27 15:49:47 +010026#include "rtc_base/numerics/safe_conversions.h"
Mirko Bonadei92ea95e2017-09-15 06:47:31 +020027#include "rtc_base/trace_event.h"
28#include "system_wrappers/include/clock.h"
stefan@webrtc.org91c63082012-01-31 10:49:08 +000029
niklase@google.com470e71d2011-07-07 08:21:25 +000030namespace webrtc {
31
mikhal@webrtc.orgef9f76a2013-02-15 23:22:18 +000032enum { kMaxReceiverDelayMs = 10000 };
33
Niels Möller689983f2018-11-07 16:36:22 +010034VCMReceiver::VCMReceiver(VCMTiming* timing, Clock* clock)
philipel83f831a2016-03-12 03:30:23 -080035 : VCMReceiver::VCMReceiver(timing,
36 clock,
Niels Möller689983f2018-11-07 16:36:22 +010037 absl::WrapUnique(EventWrapper::Create()),
38 absl::WrapUnique(EventWrapper::Create()),
philipel83f831a2016-03-12 03:30:23 -080039 nullptr, // NackSender
40 nullptr) // KeyframeRequestSender
41{}
42
43VCMReceiver::VCMReceiver(VCMTiming* timing,
44 Clock* clock,
philipel83f831a2016-03-12 03:30:23 -080045 NackSender* nack_sender,
46 KeyFrameRequestSender* keyframe_request_sender)
Niels Möller689983f2018-11-07 16:36:22 +010047 : VCMReceiver(timing,
48 clock,
49 absl::WrapUnique(EventWrapper::Create()),
50 absl::WrapUnique(EventWrapper::Create()),
51 nack_sender,
52 keyframe_request_sender) {}
Qiang Chend4cec152015-06-19 09:17:00 -070053
54VCMReceiver::VCMReceiver(VCMTiming* timing,
55 Clock* clock,
kwiberg3f55dea2016-02-29 05:51:59 -080056 std::unique_ptr<EventWrapper> receiver_event,
57 std::unique_ptr<EventWrapper> jitter_buffer_event)
philipel83f831a2016-03-12 03:30:23 -080058 : VCMReceiver::VCMReceiver(timing,
59 clock,
60 std::move(receiver_event),
61 std::move(jitter_buffer_event),
62 nullptr, // NackSender
63 nullptr) // KeyframeRequestSender
64{}
65
66VCMReceiver::VCMReceiver(VCMTiming* timing,
67 Clock* clock,
68 std::unique_ptr<EventWrapper> receiver_event,
69 std::unique_ptr<EventWrapper> jitter_buffer_event,
70 NackSender* nack_sender,
71 KeyFrameRequestSender* keyframe_request_sender)
kthelgasond701dfd2017-03-27 07:24:57 -070072 : clock_(clock),
philipel83f831a2016-03-12 03:30:23 -080073 jitter_buffer_(clock_,
74 std::move(jitter_buffer_event),
75 nack_sender,
76 keyframe_request_sender),
stefan@webrtc.org1ea4b502013-01-07 08:49:41 +000077 timing_(timing),
kwiberg0eb15ed2015-12-17 03:04:15 -080078 render_wait_event_(std::move(receiver_event)),
Peter Boström5464a6e2015-04-21 16:35:50 +020079 max_video_delay_ms_(kMaxVideoDelayMs) {
80 Reset();
81}
niklase@google.com470e71d2011-07-07 08:21:25 +000082
stefan@webrtc.org1ea4b502013-01-07 08:49:41 +000083VCMReceiver::~VCMReceiver() {
stefan@webrtc.org2baf5f52013-03-13 08:46:25 +000084 render_wait_event_->Set();
niklase@google.com470e71d2011-07-07 08:21:25 +000085}
86
stefan@webrtc.org1ea4b502013-01-07 08:49:41 +000087void VCMReceiver::Reset() {
kthelgasond701dfd2017-03-27 07:24:57 -070088 rtc::CritScope cs(&crit_sect_);
stefan@webrtc.org1ea4b502013-01-07 08:49:41 +000089 if (!jitter_buffer_.Running()) {
90 jitter_buffer_.Start();
91 } else {
92 jitter_buffer_.Flush();
93 }
henrik.lundin@webrtc.orgbaf6db52011-11-02 18:58:39 +000094}
95
pkasting@chromium.org16825b12015-01-12 21:51:21 +000096void VCMReceiver::UpdateRtt(int64_t rtt) {
stefan@webrtc.org1ea4b502013-01-07 08:49:41 +000097 jitter_buffer_.UpdateRtt(rtt);
98}
99
Johan Ahlers95348f72016-06-28 11:11:28 +0200100int32_t VCMReceiver::InsertPacket(const VCMPacket& packet) {
stefan@webrtc.org3417eb42013-05-21 15:25:53 +0000101 // Insert the packet into the jitter buffer. The packet can either be empty or
102 // contain media at this point.
103 bool retransmitted = false;
philipel9d3ab612015-12-21 04:12:39 -0800104 const VCMFrameBufferEnum ret =
105 jitter_buffer_.InsertPacket(packet, &retransmitted);
stefan@webrtc.org3417eb42013-05-21 15:25:53 +0000106 if (ret == kOldPacket) {
niklase@google.com470e71d2011-07-07 08:21:25 +0000107 return VCM_OK;
stefan@webrtc.org3417eb42013-05-21 15:25:53 +0000108 } else if (ret == kFlushIndicator) {
109 return VCM_FLUSH_INDICATOR;
110 } else if (ret < 0) {
stefan@webrtc.org3417eb42013-05-21 15:25:53 +0000111 return VCM_JITTER_BUFFER_ERROR;
stefan@webrtc.org1ea4b502013-01-07 08:49:41 +0000112 }
stefan@webrtc.org3417eb42013-05-21 15:25:53 +0000113 if (ret == kCompleteSession && !retransmitted) {
114 // We don't want to include timestamps which have suffered from
115 // retransmission here, since we compensate with extra retransmission
116 // delay within the jitter estimate.
117 timing_->IncomingTimestamp(packet.timestamp, clock_->TimeInMilliseconds());
118 }
stefan@webrtc.org1ea4b502013-01-07 08:49:41 +0000119 return VCM_OK;
niklase@google.com470e71d2011-07-07 08:21:25 +0000120}
121
pbos@webrtc.org4dd40d62015-02-17 13:22:43 +0000122void VCMReceiver::TriggerDecoderShutdown() {
123 jitter_buffer_.Stop();
124 render_wait_event_->Set();
125}
126
pbos@webrtc.org4f16c872014-11-24 09:06:48 +0000127VCMEncodedFrame* VCMReceiver::FrameForDecoding(uint16_t max_wait_time_ms,
perkj796cfaf2015-12-10 09:27:38 -0800128 bool prefer_late_decoding) {
stefan@webrtc.orga678a3b2013-01-21 07:42:11 +0000129 const int64_t start_time_ms = clock_->TimeInMilliseconds();
mikhal@webrtc.org759b0412013-05-07 16:36:00 +0000130 uint32_t frame_timestamp = 0;
isheriff6b4b5f32016-06-08 00:24:21 -0700131 int min_playout_delay_ms = -1;
132 int max_playout_delay_ms = -1;
Johan Ahlers31b2ec42016-06-28 13:32:49 +0200133 int64_t render_time_ms = 0;
mikhal@webrtc.org759b0412013-05-07 16:36:00 +0000134 // Exhaust wait time to get a complete frame for decoding.
isheriff6b4b5f32016-06-08 00:24:21 -0700135 VCMEncodedFrame* found_frame =
136 jitter_buffer_.NextCompleteFrame(max_wait_time_ms);
mikhal@webrtc.org759b0412013-05-07 16:36:00 +0000137
isheriff6b4b5f32016-06-08 00:24:21 -0700138 if (found_frame) {
Niels Möller23775882018-08-16 10:24:12 +0200139 frame_timestamp = found_frame->Timestamp();
isheriff6b4b5f32016-06-08 00:24:21 -0700140 min_playout_delay_ms = found_frame->EncodedImage().playout_delay_.min_ms;
141 max_playout_delay_ms = found_frame->EncodedImage().playout_delay_.max_ms;
142 } else {
Niels Möller375b3462019-01-10 15:35:56 +0100143 return nullptr;
isheriff6b4b5f32016-06-08 00:24:21 -0700144 }
mikhal@webrtc.org759b0412013-05-07 16:36:00 +0000145
isheriff6b4b5f32016-06-08 00:24:21 -0700146 if (min_playout_delay_ms >= 0)
147 timing_->set_min_playout_delay(min_playout_delay_ms);
148
149 if (max_playout_delay_ms >= 0)
150 timing_->set_max_playout_delay(max_playout_delay_ms);
mikhal@webrtc.orgd3cd5652013-05-03 17:54:18 +0000151
mikhal@webrtc.org759b0412013-05-07 16:36:00 +0000152 // We have a frame - Set timing and render timestamp.
mikhal@webrtc.orgadc64a72013-05-30 16:20:18 +0000153 timing_->SetJitterDelay(jitter_buffer_.EstimatedJitterMs());
mikhal@webrtc.org759b0412013-05-07 16:36:00 +0000154 const int64_t now_ms = clock_->TimeInMilliseconds();
155 timing_->UpdateCurrentDelay(frame_timestamp);
Johan Ahlers31b2ec42016-06-28 13:32:49 +0200156 render_time_ms = timing_->RenderTimeMs(frame_timestamp, now_ms);
mikhal@webrtc.org759b0412013-05-07 16:36:00 +0000157 // Check render timing.
158 bool timing_error = false;
159 // Assume that render timing errors are due to changes in the video stream.
Johan Ahlers31b2ec42016-06-28 13:32:49 +0200160 if (render_time_ms < 0) {
mikhal@webrtc.org759b0412013-05-07 16:36:00 +0000161 timing_error = true;
Johan Ahlers31b2ec42016-06-28 13:32:49 +0200162 } else if (std::abs(render_time_ms - now_ms) > max_video_delay_ms_) {
163 int frame_delay = static_cast<int>(std::abs(render_time_ms - now_ms));
Mirko Bonadei675513b2017-11-09 11:09:25 +0100164 RTC_LOG(LS_WARNING)
165 << "A frame about to be decoded is out of the configured "
166 << "delay bounds (" << frame_delay << " > " << max_video_delay_ms_
167 << "). Resetting the video jitter buffer.";
mikhal@webrtc.org759b0412013-05-07 16:36:00 +0000168 timing_error = true;
169 } else if (static_cast<int>(timing_->TargetVideoDelay()) >
170 max_video_delay_ms_) {
Mirko Bonadei675513b2017-11-09 11:09:25 +0100171 RTC_LOG(LS_WARNING) << "The video target delay has grown larger than "
172 << max_video_delay_ms_
173 << " ms. Resetting jitter buffer.";
mikhal@webrtc.org759b0412013-05-07 16:36:00 +0000174 timing_error = true;
stefan@webrtc.org4ce19b12013-05-06 13:16:51 +0000175 }
176
mikhal@webrtc.org759b0412013-05-07 16:36:00 +0000177 if (timing_error) {
178 // Timing error => reset timing and flush the jitter buffer.
179 jitter_buffer_.Flush();
stefan@webrtc.org9f557c12013-05-17 12:55:07 +0000180 timing_->Reset();
mikhal@webrtc.org759b0412013-05-07 16:36:00 +0000181 return NULL;
182 }
183
perkj796cfaf2015-12-10 09:27:38 -0800184 if (prefer_late_decoding) {
mikhal@webrtc.org759b0412013-05-07 16:36:00 +0000185 // Decode frame as close as possible to the render timestamp.
philipel9d3ab612015-12-21 04:12:39 -0800186 const int32_t available_wait_time =
187 max_wait_time_ms -
mikhal@webrtc.org759b0412013-05-07 16:36:00 +0000188 static_cast<int32_t>(clock_->TimeInMilliseconds() - start_time_ms);
philipel9d3ab612015-12-21 04:12:39 -0800189 uint16_t new_max_wait_time =
190 static_cast<uint16_t>(VCM_MAX(available_wait_time, 0));
Ilya Nikolaevskiy8c4fe162018-02-27 15:49:47 +0100191 uint32_t wait_time_ms = rtc::saturated_cast<uint32_t>(
192 timing_->MaxWaitingTime(render_time_ms, clock_->TimeInMilliseconds()));
mikhal@webrtc.org759b0412013-05-07 16:36:00 +0000193 if (new_max_wait_time < wait_time_ms) {
194 // We're not allowed to wait until the frame is supposed to be rendered,
195 // waiting as long as we're allowed to avoid busy looping, and then return
196 // NULL. Next call to this function might return the frame.
Niklas Enbomb4c5eaa2015-06-03 09:34:25 -0700197 render_wait_event_->Wait(new_max_wait_time);
mikhal@webrtc.org759b0412013-05-07 16:36:00 +0000198 return NULL;
199 }
200 // Wait until it's time to render.
201 render_wait_event_->Wait(wait_time_ms);
202 }
203
204 // Extract the frame from the jitter buffer and set the render time.
205 VCMEncodedFrame* frame = jitter_buffer_.ExtractAndSetDecode(frame_timestamp);
mikhal@webrtc.org8f86cc82013-05-07 18:05:21 +0000206 if (frame == NULL) {
207 return NULL;
208 }
Johan Ahlers31b2ec42016-06-28 13:32:49 +0200209 frame->SetRenderTime(render_time_ms);
Niels Möller23775882018-08-16 10:24:12 +0200210 TRACE_EVENT_ASYNC_STEP1("webrtc", "Video", frame->Timestamp(), "SetRenderTS",
Johan Ahlers31b2ec42016-06-28 13:32:49 +0200211 "render_time", frame->RenderTimeMs());
mikhal@webrtc.org759b0412013-05-07 16:36:00 +0000212 if (!frame->Complete()) {
213 // Update stats for incomplete frames.
stefan@webrtc.org4ce19b12013-05-06 13:16:51 +0000214 bool retransmitted = false;
215 const int64_t last_packet_time_ms =
mikhal@webrtc.org759b0412013-05-07 16:36:00 +0000216 jitter_buffer_.LastPacketTime(frame, &retransmitted);
stefan@webrtc.org4ce19b12013-05-06 13:16:51 +0000217 if (last_packet_time_ms >= 0 && !retransmitted) {
218 // We don't want to include timestamps which have suffered from
219 // retransmission here, since we compensate with extra retransmission
220 // delay within the jitter estimate.
mikhal@webrtc.org759b0412013-05-07 16:36:00 +0000221 timing_->IncomingTimestamp(frame_timestamp, last_packet_time_ms);
stefan@webrtc.org4ce19b12013-05-06 13:16:51 +0000222 }
stefan@webrtc.org1ea4b502013-01-07 08:49:41 +0000223 }
224 return frame;
niklase@google.com470e71d2011-07-07 08:21:25 +0000225}
226
stefan@webrtc.org1ea4b502013-01-07 08:49:41 +0000227void VCMReceiver::ReleaseFrame(VCMEncodedFrame* frame) {
228 jitter_buffer_.ReleaseFrame(frame);
niklase@google.com470e71d2011-07-07 08:21:25 +0000229}
230
philipel9d3ab612015-12-21 04:12:39 -0800231void VCMReceiver::ReceiveStatistics(uint32_t* bitrate, uint32_t* framerate) {
stefan@webrtc.org1ea4b502013-01-07 08:49:41 +0000232 assert(bitrate);
233 assert(framerate);
234 jitter_buffer_.IncomingRateStatistics(framerate, bitrate);
niklase@google.com470e71d2011-07-07 08:21:25 +0000235}
236
stefan@webrtc.orga64300a2013-03-04 15:24:40 +0000237void VCMReceiver::SetNackMode(VCMNackMode nackMode,
pkasting@chromium.org16825b12015-01-12 21:51:21 +0000238 int64_t low_rtt_nack_threshold_ms,
239 int64_t high_rtt_nack_threshold_ms) {
kthelgasond701dfd2017-03-27 07:24:57 -0700240 rtc::CritScope cs(&crit_sect_);
stefan@webrtc.org1ea4b502013-01-07 08:49:41 +0000241 // Default to always having NACK enabled in hybrid mode.
stefan@webrtc.orga64300a2013-03-04 15:24:40 +0000242 jitter_buffer_.SetNackMode(nackMode, low_rtt_nack_threshold_ms,
243 high_rtt_nack_threshold_ms);
niklase@google.com470e71d2011-07-07 08:21:25 +0000244}
245
stefan@webrtc.orgbecf9c82013-02-01 15:09:57 +0000246void VCMReceiver::SetNackSettings(size_t max_nack_list_size,
stefan@webrtc.orgef144882013-05-07 19:16:33 +0000247 int max_packet_age_to_nack,
248 int max_incomplete_time_ms) {
philipel9d3ab612015-12-21 04:12:39 -0800249 jitter_buffer_.SetNackSettings(max_nack_list_size, max_packet_age_to_nack,
stefan@webrtc.orgef144882013-05-07 19:16:33 +0000250 max_incomplete_time_ms);
stefan@webrtc.orgbecf9c82013-02-01 15:09:57 +0000251}
252
stefan@webrtc.org1ea4b502013-01-07 08:49:41 +0000253VCMNackMode VCMReceiver::NackMode() const {
kthelgasond701dfd2017-03-27 07:24:57 -0700254 rtc::CritScope cs(&crit_sect_);
stefan@webrtc.org1ea4b502013-01-07 08:49:41 +0000255 return jitter_buffer_.nack_mode();
stefan@webrtc.org791eec72011-10-11 07:53:43 +0000256}
257
Wan-Teh Changb1825a42015-06-03 15:03:35 -0700258std::vector<uint16_t> VCMReceiver::NackList(bool* request_key_frame) {
259 return jitter_buffer_.GetNackList(request_key_frame);
niklase@google.com470e71d2011-07-07 08:21:25 +0000260}
261
pbos@webrtc.org55707692014-12-19 15:45:03 +0000262void VCMReceiver::RegisterStatsCallback(
263 VCMReceiveStatisticsCallback* callback) {
264 jitter_buffer_.RegisterStatsCallback(callback);
pbos@webrtc.orgce4e9a32014-12-18 13:50:16 +0000265}
266
stefan@webrtc.org1ea4b502013-01-07 08:49:41 +0000267} // namespace webrtc