blob: daf21d8b9e861d27678f9626f08333bb99eb0a20 [file] [log] [blame]
niklase@google.com470e71d2011-07-07 08:21:25 +00001/*
pwestin@webrtc.org52fd98d2012-02-13 09:03:53 +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#ifndef MODULES_VIDEO_CODING_VIDEO_CODING_IMPL_H_
12#define MODULES_VIDEO_CODING_VIDEO_CODING_IMPL_H_
niklase@google.com470e71d2011-07-07 08:21:25 +000013
Mirko Bonadei92ea95e2017-09-15 06:47:31 +020014#include "modules/video_coding/include/video_coding.h"
niklase@google.com470e71d2011-07-07 08:21:25 +000015
kwiberg3f55dea2016-02-29 05:51:59 -080016#include <memory>
perkj376b1922016-05-02 11:35:24 -070017#include <string>
stefan@webrtc.orgc5300432012-10-08 07:06:53 +000018#include <vector>
19
Erik Språngeee39202018-11-15 17:52:43 +010020#include "absl/types/optional.h"
Niels Möllerf9063782018-02-20 16:09:48 +010021#include "modules/video_coding/decoder_database.h"
22#include "modules/video_coding/encoder_database.h"
Mirko Bonadei92ea95e2017-09-15 06:47:31 +020023#include "modules/video_coding/frame_buffer.h"
24#include "modules/video_coding/generic_decoder.h"
25#include "modules/video_coding/generic_encoder.h"
26#include "modules/video_coding/jitter_buffer.h"
27#include "modules/video_coding/media_optimization.h"
Mirko Bonadei92ea95e2017-09-15 06:47:31 +020028#include "modules/video_coding/receiver.h"
29#include "modules/video_coding/timing.h"
30#include "rtc_base/onetimeevent.h"
31#include "rtc_base/sequenced_task_checker.h"
32#include "rtc_base/thread_annotations.h"
33#include "rtc_base/thread_checker.h"
34#include "system_wrappers/include/clock.h"
niklase@google.com470e71d2011-07-07 08:21:25 +000035
andresp@webrtc.orgf7eb75b2013-09-14 00:25:28 +000036namespace webrtc {
sprang@webrtc.org40709352013-11-26 11:41:59 +000037
Erik Språng08127a92016-11-16 16:41:30 +010038class VideoBitrateAllocator;
sprang1a646ee2016-12-01 06:34:11 -080039class VideoBitrateAllocationObserver;
Erik Språng08127a92016-11-16 16:41:30 +010040
andresp@webrtc.orgf7eb75b2013-09-14 00:25:28 +000041namespace vcm {
niklase@google.com470e71d2011-07-07 08:21:25 +000042
andresp@webrtc.orgf7eb75b2013-09-14 00:25:28 +000043class VCMProcessTimer {
44 public:
sprang40217c32016-11-21 05:41:52 -080045 static const int64_t kDefaultProcessIntervalMs = 1000;
46
pkasting@chromium.org0b1534c2014-12-15 22:09:40 +000047 VCMProcessTimer(int64_t periodMs, Clock* clock)
andresp@webrtc.orgf7eb75b2013-09-14 00:25:28 +000048 : _clock(clock),
49 _periodMs(periodMs),
50 _latestMs(_clock->TimeInMilliseconds()) {}
pkasting@chromium.org0b1534c2014-12-15 22:09:40 +000051 int64_t Period() const;
52 int64_t TimeUntilProcess() const;
andresp@webrtc.orgf7eb75b2013-09-14 00:25:28 +000053 void Processed();
niklase@google.com470e71d2011-07-07 08:21:25 +000054
andresp@webrtc.orgf7eb75b2013-09-14 00:25:28 +000055 private:
56 Clock* _clock;
pkasting@chromium.org0b1534c2014-12-15 22:09:40 +000057 int64_t _periodMs;
andresp@webrtc.orgf7eb75b2013-09-14 00:25:28 +000058 int64_t _latestMs;
niklase@google.com470e71d2011-07-07 08:21:25 +000059};
60
Niels Möllera0565992017-10-24 11:37:08 +020061class VideoSender {
andresp@webrtc.orgf7eb75b2013-09-14 00:25:28 +000062 public:
63 typedef VideoCodingModule::SenderNackMode SenderNackMode;
64
Yves Gerey665174f2018-06-19 15:03:05 +020065 VideoSender(Clock* clock, EncodedImageCallback* post_encode_callback);
andresp@webrtc.org1df9dc32014-01-09 08:01:57 +000066
andresp@webrtc.orgf7eb75b2013-09-14 00:25:28 +000067 ~VideoSender();
68
andresp@webrtc.orgf7eb75b2013-09-14 00:25:28 +000069 // Register the send codec to be used.
tommi@webrtc.orge07710c2015-02-19 17:43:25 +000070 // This method must be called on the construction thread.
andresp@webrtc.orgf7eb75b2013-09-14 00:25:28 +000071 int32_t RegisterSendCodec(const VideoCodec* sendCodec,
72 uint32_t numberOfCores,
73 uint32_t maxPayloadSize);
tommi@webrtc.orge07710c2015-02-19 17:43:25 +000074
Peter Boström795dbe42015-11-27 14:09:07 +010075 void RegisterExternalEncoder(VideoEncoder* externalEncoder,
Peter Boström795dbe42015-11-27 14:09:07 +010076 bool internalSource);
andresp@webrtc.orgf7eb75b2013-09-14 00:25:28 +000077
sprang1a646ee2016-12-01 06:34:11 -080078 // Update the channel parameters based on new rates and rtt. This will also
79 // cause an immediate call to VideoEncoder::SetRateAllocation().
80 int32_t SetChannelParameters(
81 uint32_t target_bitrate_bps,
sprang1a646ee2016-12-01 06:34:11 -080082 VideoBitrateAllocator* bitrate_allocator,
83 VideoBitrateAllocationObserver* bitrate_updated_callback);
84
85 // Updates the channel parameters with a new bitrate allocation, but using the
86 // current targit_bitrate, loss rate and rtt. That is, the distribution or
87 // caps may be updated to a change to a new VideoCodec or allocation mode.
88 // The new parameters will be stored as pending EncoderParameters, and the
89 // encoder will only be updated on the next frame.
Niels Möller96d7f762018-01-30 11:27:16 +010090 void UpdateChannelParameters(
sprang1a646ee2016-12-01 06:34:11 -080091 VideoBitrateAllocator* bitrate_allocator,
92 VideoBitrateAllocationObserver* bitrate_updated_callback);
Erik Språng08127a92016-11-16 16:41:30 +010093
Miguel Casas-Sanchez47650702015-05-29 17:21:40 -070094 int32_t AddVideoFrame(const VideoFrame& videoFrame,
Erik Språngeee39202018-11-15 17:52:43 +010095 const CodecSpecificInfo* codecSpecificInfo,
96 absl::optional<VideoEncoder::EncoderInfo> encoder_info);
andresp@webrtc.orgf7eb75b2013-09-14 00:25:28 +000097
perkj600246e2016-05-04 11:26:51 -070098 int32_t IntraFrameRequest(size_t stream_index);
andresp@webrtc.orgf7eb75b2013-09-14 00:25:28 +000099 int32_t EnableFrameDropper(bool enable);
andresp@webrtc.orgf7eb75b2013-09-14 00:25:28 +0000100
andresp@webrtc.orgf7eb75b2013-09-14 00:25:28 +0000101 private:
Erik Språng08127a92016-11-16 16:41:30 +0100102 EncoderParameters UpdateEncoderParameters(
103 const EncoderParameters& params,
104 VideoBitrateAllocator* bitrate_allocator,
105 uint32_t target_bitrate_bps);
perkj57c21f92016-06-17 07:27:16 -0700106 void SetEncoderParameters(EncoderParameters params, bool has_internal_source)
danilchap56359be2017-09-07 07:53:45 -0700107 RTC_EXCLUSIVE_LOCKS_REQUIRED(encoder_crit_);
Erik Språngd96b2752018-12-13 11:23:27 +0100108 VideoBitrateAllocation GetAllocation(
109 uint32_t bitrate_bps,
110 uint32_t framerate_fps,
111 VideoBitrateAllocator* bitrate_allocator) const;
Peter Boströmdcb89982015-09-15 14:43:47 +0200112
pbos5ad935c2016-01-25 03:52:44 -0800113 rtc::CriticalSection encoder_crit_;
andresp@webrtc.orgf7eb75b2013-09-14 00:25:28 +0000114 VCMGenericEncoder* _encoder;
henrik.lundin@webrtc.orgbec11ef2013-09-23 19:54:25 +0000115 media_optimization::MediaOptimization _mediaOpt;
danilchap56359be2017-09-07 07:53:45 -0700116 VCMEncodedFrameCallback _encodedFrameCallback RTC_GUARDED_BY(encoder_crit_);
kthelgason876222f2016-11-29 01:44:11 -0800117 EncodedImageCallback* const post_encode_callback_;
Niels Möllerf9063782018-02-20 16:09:48 +0100118 VCMEncoderDataBase _codecDataBase RTC_GUARDED_BY(encoder_crit_);
Erik Språngd3438aa2018-11-08 16:56:43 +0100119
120 // |frame_dropper_requested_| specifies if the user of this class has
121 // requested frame dropping to be enabled, via EnableFrameDropper().
122 // Depending on video encoder configuration, this setting may be overridden
123 // and the frame dropper be force disabled. If so,
124 // |force_disable_frame_dropper_| will be set to true.
125 // If frame dropper is requested, and is not force disabled, frame dropping
126 // might still be disabled if VideoEncoder::GetEncoderInfo() indicates that
127 // the encoder has a trusted rate controller. This is determined on a
128 // per-frame basis, as the encoder behavior might dynamically change.
129 bool frame_dropper_requested_ RTC_GUARDED_BY(encoder_crit_);
130 bool force_disable_frame_dropper_ RTC_GUARDED_BY(encoder_crit_);
andresp@webrtc.orge682aa52013-12-19 10:59:48 +0000131
tommi@webrtc.orge07710c2015-02-19 17:43:25 +0000132 // Must be accessed on the construction thread of VideoSender.
133 VideoCodec current_codec_;
perkj4e417b22016-07-14 23:35:55 -0700134 rtc::SequencedTaskChecker sequenced_checker_;
Erik Språng66a641a2015-06-11 14:20:07 +0200135
Peter Boström233bfd22016-01-18 20:23:40 +0100136 rtc::CriticalSection params_crit_;
danilchap56359be2017-09-07 07:53:45 -0700137 EncoderParameters encoder_params_ RTC_GUARDED_BY(params_crit_);
138 bool encoder_has_internal_source_ RTC_GUARDED_BY(params_crit_);
139 std::vector<FrameType> next_frame_types_ RTC_GUARDED_BY(params_crit_);
andresp@webrtc.orgf7eb75b2013-09-14 00:25:28 +0000140};
141
Peter Boström0b250722016-04-22 18:23:15 +0200142class VideoReceiver : public Module {
andresp@webrtc.orgf7eb75b2013-09-14 00:25:28 +0000143 public:
philipel83f831a2016-03-12 03:30:23 -0800144 VideoReceiver(Clock* clock,
philipel721d4022016-12-15 07:10:57 -0800145 VCMTiming* timing,
philipel83f831a2016-03-12 03:30:23 -0800146 NackSender* nack_sender = nullptr,
147 KeyFrameRequestSender* keyframe_request_sender = nullptr);
Tommifbf3bce2018-02-21 15:56:05 +0100148 ~VideoReceiver() override;
andresp@webrtc.orgf7eb75b2013-09-14 00:25:28 +0000149
andresp@webrtc.orgf7eb75b2013-09-14 00:25:28 +0000150 int32_t RegisterReceiveCodec(const VideoCodec* receiveCodec,
151 int32_t numberOfCores,
152 bool requireKeyFrame);
153
Peter Boström795dbe42015-11-27 14:09:07 +0100154 void RegisterExternalDecoder(VideoDecoder* externalDecoder,
perkj796cfaf2015-12-10 09:27:38 -0800155 uint8_t payloadType);
andresp@webrtc.orgf7eb75b2013-09-14 00:25:28 +0000156 int32_t RegisterReceiveCallback(VCMReceiveCallback* receiveCallback);
157 int32_t RegisterReceiveStatisticsCallback(
158 VCMReceiveStatisticsCallback* receiveStats);
159 int32_t RegisterFrameTypeCallback(VCMFrameTypeCallback* frameTypeCallback);
160 int32_t RegisterPacketRequestCallback(VCMPacketRequestCallback* callback);
andresp@webrtc.orgf7eb75b2013-09-14 00:25:28 +0000161
162 int32_t Decode(uint16_t maxWaitTimeMs);
andresp@webrtc.orgf7eb75b2013-09-14 00:25:28 +0000163
philipelfd5a20f2016-11-15 00:57:57 -0800164 int32_t Decode(const webrtc::VCMEncodedFrame* frame);
165
andresp@webrtc.orgf7eb75b2013-09-14 00:25:28 +0000166 int32_t IncomingPacket(const uint8_t* incomingPayload,
pkasting@chromium.org4591fbd2014-11-20 22:28:14 +0000167 size_t payloadLength,
andresp@webrtc.orgf7eb75b2013-09-14 00:25:28 +0000168 const WebRtcRTPHeader& rtpInfo);
169 int32_t SetMinimumPlayoutDelay(uint32_t minPlayoutDelayMs);
170 int32_t SetRenderDelay(uint32_t timeMS);
171 int32_t Delay() const;
andresp@webrtc.orgf7eb75b2013-09-14 00:25:28 +0000172
tommia5c18d72017-03-20 10:43:23 -0700173 // DEPRECATED.
174 int SetReceiverRobustnessMode(
175 VideoCodingModule::ReceiverRobustness robustnessMode,
176 VCMDecodeErrorMode errorMode);
177
andresp@webrtc.orgf7eb75b2013-09-14 00:25:28 +0000178 void SetNackSettings(size_t max_nack_list_size,
179 int max_packet_age_to_nack,
180 int max_incomplete_time_ms);
181
182 void SetDecodeErrorMode(VCMDecodeErrorMode decode_error_mode);
andresp@webrtc.orgf7eb75b2013-09-14 00:25:28 +0000183
pkasting@chromium.org16825b12015-01-12 21:51:21 +0000184 int32_t SetReceiveChannelParameters(int64_t rtt);
andresp@webrtc.orgf7eb75b2013-09-14 00:25:28 +0000185 int32_t SetVideoProtection(VCMVideoProtection videoProtection, bool enable);
186
Peter Boström0b250722016-04-22 18:23:15 +0200187 int64_t TimeUntilNextProcess() override;
188 void Process() override;
Tommifbf3bce2018-02-21 15:56:05 +0100189 void ProcessThreadAttached(ProcessThread* process_thread) override;
andresp@webrtc.orgf7eb75b2013-09-14 00:25:28 +0000190
pbos@webrtc.org4dd40d62015-02-17 13:22:43 +0000191 void TriggerDecoderShutdown();
sprang@webrtc.org40709352013-11-26 11:41:59 +0000192
Tommifbf3bce2018-02-21 15:56:05 +0100193 // Notification methods that are used to check our internal state and validate
194 // threading assumptions. These are called by VideoReceiveStream.
195 // See |IsDecoderThreadRunning()| for more details.
196 void DecoderThreadStarting();
197 void DecoderThreadStopped();
198
andresp@webrtc.orgf7eb75b2013-09-14 00:25:28 +0000199 protected:
Tommifbf3bce2018-02-21 15:56:05 +0100200 int32_t Decode(const webrtc::VCMEncodedFrame& frame);
andresp@webrtc.orgf7eb75b2013-09-14 00:25:28 +0000201 int32_t RequestKeyFrame();
andresp@webrtc.orgf7eb75b2013-09-14 00:25:28 +0000202
203 private:
Tommifbf3bce2018-02-21 15:56:05 +0100204 // Used for DCHECKing thread correctness.
205 // In build where DCHECKs are enabled, will return false before
206 // DecoderThreadStarting is called, then true until DecoderThreadStopped
207 // is called.
208 // In builds where DCHECKs aren't enabled, it will return true.
209 bool IsDecoderThreadRunning();
210
211 rtc::ThreadChecker construction_thread_checker_;
212 rtc::ThreadChecker decoder_thread_checker_;
213 rtc::ThreadChecker module_thread_checker_;
pbos@webrtc.org20c1f562014-07-04 10:58:12 +0000214 Clock* const clock_;
sprang3911c262016-04-15 01:24:14 -0700215 rtc::CriticalSection process_crit_;
philipel721d4022016-12-15 07:10:57 -0800216 VCMTiming* _timing;
andresp@webrtc.orgf7eb75b2013-09-14 00:25:28 +0000217 VCMReceiver _receiver;
andresp@webrtc.orgf7eb75b2013-09-14 00:25:28 +0000218 VCMDecodedFrameCallback _decodedFrameCallback;
niklase@google.com470e71d2011-07-07 08:21:25 +0000219
Tommifbf3bce2018-02-21 15:56:05 +0100220 // These callbacks are set on the construction thread before being attached
221 // to the module thread or decoding started, so a lock is not required.
222 VCMFrameTypeCallback* _frameTypeCallback;
223 VCMReceiveStatisticsCallback* _receiveStatsCallback;
224 VCMPacketRequestCallback* _packetRequestCallback;
225
226 // Used on both the module and decoder thread.
danilchap56359be2017-09-07 07:53:45 -0700227 bool _scheduleKeyRequest RTC_GUARDED_BY(process_crit_);
228 bool drop_frames_until_keyframe_ RTC_GUARDED_BY(process_crit_);
sprang3911c262016-04-15 01:24:14 -0700229
Tommifbf3bce2018-02-21 15:56:05 +0100230 // Modified on the construction thread while not attached to the process
231 // thread. Once attached to the process thread, its value is only read
232 // so a lock is not required.
233 size_t max_nack_list_size_;
Peter Boströmed3277b2016-02-02 15:40:04 +0100234
Tommifbf3bce2018-02-21 15:56:05 +0100235 // Callbacks are set before the decoder thread starts.
236 // Once the decoder thread has been started, usage of |_codecDataBase| moves
237 // over to the decoder thread.
238 VCMDecoderDataBase _codecDataBase;
Tommifbf3bce2018-02-21 15:56:05 +0100239
240 VCMProcessTimer _receiveStatsTimer RTC_GUARDED_BY(module_thread_checker_);
241 VCMProcessTimer _retransmissionTimer RTC_GUARDED_BY(module_thread_checker_);
242 VCMProcessTimer _keyRequestTimer RTC_GUARDED_BY(module_thread_checker_);
Tommifbf3bce2018-02-21 15:56:05 +0100243 ThreadUnsafeOneTimeEvent first_frame_received_
244 RTC_GUARDED_BY(decoder_thread_checker_);
245 // Modified on the construction thread. Can be read without a lock and assumed
246 // to be non-null on the module and decoder threads.
247 ProcessThread* process_thread_ = nullptr;
248 bool is_attached_to_process_thread_
249 RTC_GUARDED_BY(construction_thread_checker_) = false;
250#if RTC_DCHECK_IS_ON
251 bool decoder_thread_is_running_ = false;
252#endif
niklase@google.com470e71d2011-07-07 08:21:25 +0000253};
andresp@webrtc.orgf7eb75b2013-09-14 00:25:28 +0000254
255} // namespace vcm
pbos@webrtc.orgd900e8b2013-07-03 15:12:26 +0000256} // namespace webrtc
Mirko Bonadei92ea95e2017-09-15 06:47:31 +0200257#endif // MODULES_VIDEO_CODING_VIDEO_CODING_IMPL_H_