blob: f097710630663d96b2dd41602a22d463ce256515 [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#ifndef VIDEO_VIDEO_RECEIVE_STREAM_H_
12#define VIDEO_VIDEO_RECEIVE_STREAM_H_
pbos@webrtc.org29d58392013-05-16 12:08:03 +000013
kwiberg27f982b2016-03-01 11:52:33 -080014#include <memory>
pbos@webrtc.org29d58392013-05-16 12:08:03 +000015#include <vector>
16
Sebastian Jansson74682c12019-03-01 11:50:20 +010017#include "api/task_queue/task_queue_factory.h"
Markus Handell269ac812019-12-03 14:31:45 +010018#include "api/transport/media/media_transport_interface.h"
19#include "api/video/recordable_encoded_frame.h"
Mirko Bonadei92ea95e2017-09-15 06:47:31 +020020#include "call/rtp_packet_sink_interface.h"
21#include "call/syncable.h"
22#include "call/video_receive_stream.h"
Mirko Bonadei92ea95e2017-09-15 06:47:31 +020023#include "modules/rtp_rtcp/include/flexfec_receiver.h"
Chen Xing90f3b892019-06-25 10:16:14 +020024#include "modules/rtp_rtcp/source/source_tracker.h"
Mirko Bonadei92ea95e2017-09-15 06:47:31 +020025#include "modules/video_coding/frame_buffer2.h"
Niels Mölleree3d9952019-09-09 12:51:55 +020026#include "modules/video_coding/video_receiver2.h"
Sebastian Janssonb55015e2019-04-09 13:44:04 +020027#include "rtc_base/synchronization/sequence_checker.h"
Sebastian Jansson11d0d7b2019-04-11 12:39:34 +020028#include "rtc_base/task_queue.h"
Mirko Bonadei92ea95e2017-09-15 06:47:31 +020029#include "system_wrappers/include/clock.h"
30#include "video/receive_statistics_proxy.h"
31#include "video/rtp_streams_synchronizer.h"
32#include "video/rtp_video_stream_receiver.h"
33#include "video/transport_adapter.h"
34#include "video/video_stream_decoder.h"
pbos@webrtc.org29d58392013-05-16 12:08:03 +000035
36namespace webrtc {
37
mflodmane3787022015-10-21 13:24:28 +020038class CallStats;
Peter Boströmd1d66ba2016-02-08 14:07:14 +010039class ProcessThread;
mflodman4cd27902016-08-05 06:28:45 -070040class RTPFragmentationHeader;
nisse0f15f922017-06-21 01:05:22 -070041class RtpStreamReceiverInterface;
42class RtpStreamReceiverControllerInterface;
nisseca5706d2017-09-11 02:32:16 -070043class RtxReceiveStream;
philipelfd5a20f2016-11-15 00:57:57 -080044class VCMTiming;
pbos@webrtc.org29d58392013-05-16 12:08:03 +000045
46namespace internal {
47
pbos@webrtc.org74fa4892013-08-23 09:19:30 +000048class VideoReceiveStream : public webrtc::VideoReceiveStream,
nisse30f118e2016-05-03 01:09:11 -070049 public rtc::VideoSinkInterface<VideoFrame>,
philipel83f831a2016-03-12 03:30:23 -080050 public NackSender,
solenberg3ebbcb52017-01-31 03:58:40 -080051 public video_coding::OnCompleteFrameCallback,
philipele21be1d2017-09-25 06:37:12 -070052 public Syncable,
Bjorn A Mellem7a9a0922019-11-26 09:19:40 -080053 public CallStatsObserver {
pbos@webrtc.org29d58392013-05-16 12:08:03 +000054 public:
Markus Handell269ac812019-12-03 14:31:45 +010055 // The default number of milliseconds to pass before re-requesting a key frame
56 // to be sent.
57 static constexpr int kMaxWaitForKeyFrameMs = 200;
58
Sebastian Jansson74682c12019-03-01 11:50:20 +010059 VideoReceiveStream(TaskQueueFactory* task_queue_factory,
60 RtpStreamReceiverControllerInterface* receiver_controller,
nisse0f15f922017-06-21 01:05:22 -070061 int num_cpu_cores,
nisse0245da02016-11-30 03:35:20 -080062 PacketRouter* packet_router,
Tommi733b5472016-06-10 17:58:01 +020063 VideoReceiveStream::Config config,
mflodmane3787022015-10-21 13:24:28 +020064 ProcessThread* process_thread,
Ruslan Burakov493a6502019-02-27 15:32:48 +010065 CallStats* call_stats,
66 Clock* clock,
67 VCMTiming* timing);
Sebastian Jansson74682c12019-03-01 11:50:20 +010068 VideoReceiveStream(TaskQueueFactory* task_queue_factory,
69 RtpStreamReceiverControllerInterface* receiver_controller,
Ruslan Burakov493a6502019-02-27 15:32:48 +010070 int num_cpu_cores,
71 PacketRouter* packet_router,
72 VideoReceiveStream::Config config,
73 ProcessThread* process_thread,
Sebastian Jansson8026d602019-03-04 19:39:01 +010074 CallStats* call_stats,
75 Clock* clock);
Jelena Marusiccd670222015-07-16 09:30:09 +020076 ~VideoReceiveStream() override;
pbos@webrtc.org29d58392013-05-16 12:08:03 +000077
brandtr090c9402017-01-25 08:28:02 -080078 const Config& config() const { return config_; }
79
pbos1ba8d392016-05-01 20:18:34 -070080 void SignalNetworkState(NetworkState state);
81 bool DeliverRtcp(const uint8_t* packet, size_t length);
Jelena Marusiccd670222015-07-16 09:30:09 +020082
solenberg3ebbcb52017-01-31 03:58:40 -080083 void SetSync(Syncable* audio_syncable);
brandtr090c9402017-01-25 08:28:02 -080084
85 // Implements webrtc::VideoReceiveStream.
pbos1ba8d392016-05-01 20:18:34 -070086 void Start() override;
87 void Stop() override;
88
Jelena Marusiccd670222015-07-16 09:30:09 +020089 webrtc::VideoReceiveStream::Stats GetStats() const override;
pbos@webrtc.org29d58392013-05-16 12:08:03 +000090
eladalonc0d481a2017-08-02 07:39:07 -070091 void AddSecondarySink(RtpPacketSinkInterface* sink) override;
92 void RemoveSecondarySink(const RtpPacketSinkInterface* sink) override;
93
Ruslan Burakov493a6502019-02-27 15:32:48 +010094 // SetBaseMinimumPlayoutDelayMs and GetBaseMinimumPlayoutDelayMs are called
95 // from webrtc/api level and requested by user code. For e.g. blink/js layer
96 // in Chromium.
97 bool SetBaseMinimumPlayoutDelayMs(int delay_ms) override;
98 int GetBaseMinimumPlayoutDelayMs() const override;
99
Benjamin Wrighta5564482019-04-03 10:44:18 -0700100 void SetFrameDecryptor(
101 rtc::scoped_refptr<FrameDecryptorInterface> frame_decryptor) override;
102
brandtr090c9402017-01-25 08:28:02 -0800103 // Implements rtc::VideoSinkInterface<VideoFrame>.
104 void OnFrame(const VideoFrame& video_frame) override;
105
brandtr090c9402017-01-25 08:28:02 -0800106 // Implements NackSender.
Elad Alonef09c5b2019-05-31 13:25:50 +0200107 // For this particular override of the interface,
108 // only (buffering_allowed == true) is acceptable.
109 void SendNack(const std::vector<uint16_t>& sequence_numbers,
110 bool buffering_allowed) override;
brandtr090c9402017-01-25 08:28:02 -0800111
brandtr090c9402017-01-25 08:28:02 -0800112 // Implements video_coding::OnCompleteFrameCallback.
113 void OnCompleteFrame(
philipele7c891f2018-02-22 14:35:06 +0100114 std::unique_ptr<video_coding::EncodedFrame> frame) override;
brandtr090c9402017-01-25 08:28:02 -0800115
philipele21be1d2017-09-25 06:37:12 -0700116 // Implements CallStatsObserver::OnRttUpdate
117 void OnRttUpdate(int64_t avg_rtt_ms, int64_t max_rtt_ms) override;
118
solenberg3ebbcb52017-01-31 03:58:40 -0800119 // Implements Syncable.
120 int id() const override;
Danil Chapovalovb9b146c2018-06-15 12:28:07 +0200121 absl::optional<Syncable::Info> GetInfo() const override;
Åsa Perssonfcf79cc2019-10-22 15:23:44 +0200122 bool GetPlayoutRtpTimestamp(uint32_t* rtp_timestamp,
123 int64_t* time_ms) const override;
124 void SetEstimatedPlayoutNtpTimestampMs(int64_t ntp_timestamp_ms,
125 int64_t time_ms) override;
Ruslan Burakov493a6502019-02-27 15:32:48 +0100126
127 // SetMinimumPlayoutDelay is only called by A/V sync.
solenberg3ebbcb52017-01-31 03:58:40 -0800128 void SetMinimumPlayoutDelay(int delay_ms) override;
129
Jonas Oreland49ac5952018-09-26 16:04:32 +0200130 std::vector<webrtc::RtpSource> GetSources() const override;
131
Markus Handell269ac812019-12-03 14:31:45 +0100132 RecordingState SetAndGetRecordingState(RecordingState state,
133 bool generate_key_frame) override;
134 void GenerateKeyFrame() override;
135
pbos@webrtc.org29d58392013-05-16 12:08:03 +0000136 private:
Sebastian Jansson1c747f52019-04-04 13:01:39 +0200137 int64_t GetWaitMs() const;
Sebastian Jansson11d0d7b2019-04-11 12:39:34 +0200138 void StartNextDecode() RTC_RUN_ON(decode_queue_);
Markus Handell269ac812019-12-03 14:31:45 +0100139 void HandleEncodedFrame(std::unique_ptr<video_coding::EncodedFrame> frame)
140 RTC_RUN_ON(decode_queue_);
141 void HandleFrameBufferTimeout() RTC_RUN_ON(decode_queue_);
Ruslan Burakov493a6502019-02-27 15:32:48 +0100142 void UpdatePlayoutDelays() const
143 RTC_EXCLUSIVE_LOCKS_REQUIRED(playout_delay_lock_);
Markus Handell269ac812019-12-03 14:31:45 +0100144 void RequestKeyFrame(int64_t timestamp_ms) RTC_RUN_ON(decode_queue_);
145 void HandleKeyFrameGeneration(bool received_frame_is_keyframe, int64_t now_ms)
146 RTC_RUN_ON(decode_queue_);
147 bool IsReceivingKeyFrame(int64_t timestamp_ms) const
148 RTC_RUN_ON(decode_queue_);
Peter Boströmca835252016-02-11 15:59:46 +0100149
Niels Möller9a9f18a2019-08-02 13:52:37 +0200150 void UpdateHistograms();
151
Sebastian Janssonb55015e2019-04-09 13:44:04 +0200152 SequenceChecker worker_sequence_checker_;
153 SequenceChecker module_process_sequence_checker_;
154 SequenceChecker network_sequence_checker_;
solenberg3ebbcb52017-01-31 03:58:40 -0800155
Sebastian Jansson74682c12019-03-01 11:50:20 +0100156 TaskQueueFactory* const task_queue_factory_;
157
pbos@webrtc.orge75a1bf2013-09-18 11:52:42 +0000158 TransportAdapter transport_adapter_;
pbos@webrtc.org6ae48c62014-06-06 10:49:19 +0000159 const VideoReceiveStream::Config config_;
sprang113bdca2016-10-11 03:10:10 -0700160 const int num_cpu_cores_;
Peter Boström1d04ac62016-02-05 11:25:46 +0100161 ProcessThread* const process_thread_;
pbos@webrtc.orgde1429e2014-04-28 13:00:21 +0000162 Clock* const clock_;
pbos@webrtc.org29d58392013-05-16 12:08:03 +0000163
mflodmane3787022015-10-21 13:24:28 +0200164 CallStats* const call_stats_;
Peter Boström45553ae2015-05-08 13:54:38 +0200165
Sebastian Jansson11d0d7b2019-04-11 12:39:34 +0200166 bool decoder_running_ RTC_GUARDED_BY(worker_sequence_checker_) = false;
167 bool decoder_stopped_ RTC_GUARDED_BY(decode_queue_) = true;
168
Chen Xing90f3b892019-06-25 10:16:14 +0200169 SourceTracker source_tracker_;
Danil Chapovalov8ce0d2b2018-11-23 11:03:25 +0100170 ReceiveStatisticsProxy stats_proxy_;
nisseca5706d2017-09-11 02:32:16 -0700171 // Shared by media and rtx stream receivers, since the latter has no RtpRtcp
172 // module of its own.
173 const std::unique_ptr<ReceiveStatistics> rtp_receive_statistics_;
174
philipel721d4022016-12-15 07:10:57 -0800175 std::unique_ptr<VCMTiming> timing_; // Jitter buffer experiment.
Niels Mölleree3d9952019-09-09 12:51:55 +0200176 VideoReceiver2 video_receiver_;
tommi2e82f382016-06-21 00:26:43 -0700177 std::unique_ptr<rtc::VideoSinkInterface<VideoFrame>> incoming_video_stream_;
nisseb1f2ff92017-06-09 04:01:55 -0700178 RtpVideoStreamReceiver rtp_video_stream_receiver_;
tommi2e82f382016-06-21 00:26:43 -0700179 std::unique_ptr<VideoStreamDecoder> video_stream_decoder_;
mflodman4cd27902016-08-05 06:28:45 -0700180 RtpStreamsSynchronizer rtp_stream_sync_;
sprang3911c262016-04-15 01:24:14 -0700181
Niels Möllercbcbc222018-09-28 09:07:24 +0200182 // TODO(nisse, philipel): Creation and ownership of video encoders should be
183 // moved to the new VideoStreamDecoder.
184 std::vector<std::unique_ptr<VideoDecoder>> video_decoders_;
185
philipelfd5a20f2016-11-15 00:57:57 -0800186 // Members for the new jitter buffer experiment.
Henrik Boströmc680c4a2019-04-03 10:27:36 +0000187 std::unique_ptr<video_coding::FrameBuffer> frame_buffer_;
nisse0f15f922017-06-21 01:05:22 -0700188
189 std::unique_ptr<RtpStreamReceiverInterface> media_receiver_;
nisseca5706d2017-09-11 02:32:16 -0700190 std::unique_ptr<RtxReceiveStream> rtx_receive_stream_;
nisse0f15f922017-06-21 01:05:22 -0700191 std::unique_ptr<RtpStreamReceiverInterface> rtx_receiver_;
philipel3042c2d2017-08-18 04:55:02 -0700192
193 // Whenever we are in an undecodable state (stream has just started or due to
194 // a decoding error) we require a keyframe to restart the stream.
195 bool keyframe_required_ = true;
196
197 // If we have successfully decoded any frame.
198 bool frame_decoded_ = false;
philipel48462b62017-09-26 02:54:58 -0700199
200 int64_t last_keyframe_request_ms_ = 0;
Ilya Nikolaevskiye6a2d942018-11-07 14:32:28 +0100201 int64_t last_complete_frame_time_ms_ = 0;
Ruslan Burakov493a6502019-02-27 15:32:48 +0100202
Rasmus Brandt3dde4502019-03-21 11:46:17 +0100203 // Keyframe request intervals are configurable through field trials.
204 const int max_wait_for_keyframe_ms_;
205 const int max_wait_for_frame_ms_;
206
Ruslan Burakov493a6502019-02-27 15:32:48 +0100207 rtc::CriticalSection playout_delay_lock_;
208
209 // All of them tries to change current min_playout_delay on |timing_| but
210 // source of the change request is different in each case. Among them the
211 // biggest delay is used. -1 means use default value from the |timing_|.
212 //
213 // Minimum delay as decided by the RTP playout delay extension.
214 int frame_minimum_playout_delay_ms_ RTC_GUARDED_BY(playout_delay_lock_) = -1;
215 // Minimum delay as decided by the setLatency function in "webrtc/api".
216 int base_minimum_playout_delay_ms_ RTC_GUARDED_BY(playout_delay_lock_) = -1;
217 // Minimum delay as decided by the A/V synchronization feature.
218 int syncable_minimum_playout_delay_ms_ RTC_GUARDED_BY(playout_delay_lock_) =
219 -1;
220
221 // Maximum delay as decided by the RTP playout delay extension.
222 int frame_maximum_playout_delay_ms_ RTC_GUARDED_BY(playout_delay_lock_) = -1;
Sebastian Jansson11d0d7b2019-04-11 12:39:34 +0200223
Markus Handell269ac812019-12-03 14:31:45 +0100224 // Function that is triggered with encoded frames, if not empty.
225 std::function<void(const RecordableEncodedFrame&)>
226 encoded_frame_buffer_function_ RTC_GUARDED_BY(decode_queue_);
227 // Set to true while we're requesting keyframes but not yet received one.
228 bool keyframe_generation_requested_ RTC_GUARDED_BY(decode_queue_) = false;
229
Sebastian Jansson11d0d7b2019-04-11 12:39:34 +0200230 // Defined last so they are destroyed before all other members.
231 rtc::TaskQueue decode_queue_;
pbos@webrtc.org29d58392013-05-16 12:08:03 +0000232};
mflodman@webrtc.orgf3973e82013-12-13 09:40:45 +0000233} // namespace internal
234} // namespace webrtc
pbos@webrtc.org29d58392013-05-16 12:08:03 +0000235
Mirko Bonadei92ea95e2017-09-15 06:47:31 +0200236#endif // VIDEO_VIDEO_RECEIVE_STREAM_H_