blob: 0bd5ab7ca011bba0dcee6be0ee0f86811cca80b8 [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
Mirko Bonadei92ea95e2017-09-15 06:47:31 +020020#include "common_video/include/frame_callback.h"
21#include "modules/video_coding/codec_database.h"
22#include "modules/video_coding/frame_buffer.h"
23#include "modules/video_coding/generic_decoder.h"
24#include "modules/video_coding/generic_encoder.h"
25#include "modules/video_coding/jitter_buffer.h"
26#include "modules/video_coding/media_optimization.h"
27#include "modules/video_coding/qp_parser.h"
28#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
pbos@webrtc.org891d4832015-02-26 13:15:22 +000065 VideoSender(Clock* clock,
Niels Möllera0565992017-10-24 11:37:08 +020066 EncodedImageCallback* post_encode_callback);
andresp@webrtc.org1df9dc32014-01-09 08:01:57 +000067
andresp@webrtc.orgf7eb75b2013-09-14 00:25:28 +000068 ~VideoSender();
69
andresp@webrtc.orgf7eb75b2013-09-14 00:25:28 +000070 // Register the send codec to be used.
tommi@webrtc.orge07710c2015-02-19 17:43:25 +000071 // This method must be called on the construction thread.
andresp@webrtc.orgf7eb75b2013-09-14 00:25:28 +000072 int32_t RegisterSendCodec(const VideoCodec* sendCodec,
73 uint32_t numberOfCores,
74 uint32_t maxPayloadSize);
tommi@webrtc.orge07710c2015-02-19 17:43:25 +000075
Peter Boström795dbe42015-11-27 14:09:07 +010076 void RegisterExternalEncoder(VideoEncoder* externalEncoder,
77 uint8_t payloadType,
78 bool internalSource);
andresp@webrtc.orgf7eb75b2013-09-14 00:25:28 +000079
andresp@webrtc.orgf7eb75b2013-09-14 00:25:28 +000080 int Bitrate(unsigned int* bitrate) const;
81 int FrameRate(unsigned int* framerate) const;
82
sprang1a646ee2016-12-01 06:34:11 -080083 // Update the channel parameters based on new rates and rtt. This will also
84 // cause an immediate call to VideoEncoder::SetRateAllocation().
85 int32_t SetChannelParameters(
86 uint32_t target_bitrate_bps,
87 uint8_t loss_rate,
88 int64_t rtt,
89 VideoBitrateAllocator* bitrate_allocator,
90 VideoBitrateAllocationObserver* bitrate_updated_callback);
91
92 // Updates the channel parameters with a new bitrate allocation, but using the
93 // current targit_bitrate, loss rate and rtt. That is, the distribution or
94 // caps may be updated to a change to a new VideoCodec or allocation mode.
95 // The new parameters will be stored as pending EncoderParameters, and the
96 // encoder will only be updated on the next frame.
97 void UpdateChannelParemeters(
98 VideoBitrateAllocator* bitrate_allocator,
99 VideoBitrateAllocationObserver* bitrate_updated_callback);
Erik Språng08127a92016-11-16 16:41:30 +0100100
Per69b332d2016-06-02 15:45:42 +0200101 // Deprecated:
102 // TODO(perkj): Remove once no projects use it.
andresp@webrtc.orgf7eb75b2013-09-14 00:25:28 +0000103 int32_t RegisterProtectionCallback(VCMProtectionCallback* protection);
andresp@webrtc.orgf7eb75b2013-09-14 00:25:28 +0000104
Miguel Casas-Sanchez47650702015-05-29 17:21:40 -0700105 int32_t AddVideoFrame(const VideoFrame& videoFrame,
andresp@webrtc.orgf7eb75b2013-09-14 00:25:28 +0000106 const CodecSpecificInfo* codecSpecificInfo);
107
perkj600246e2016-05-04 11:26:51 -0700108 int32_t IntraFrameRequest(size_t stream_index);
andresp@webrtc.orgf7eb75b2013-09-14 00:25:28 +0000109 int32_t EnableFrameDropper(bool enable);
andresp@webrtc.orgf7eb75b2013-09-14 00:25:28 +0000110
andresp@webrtc.orgf7eb75b2013-09-14 00:25:28 +0000111 private:
Erik Språng08127a92016-11-16 16:41:30 +0100112 EncoderParameters UpdateEncoderParameters(
113 const EncoderParameters& params,
114 VideoBitrateAllocator* bitrate_allocator,
115 uint32_t target_bitrate_bps);
perkj57c21f92016-06-17 07:27:16 -0700116 void SetEncoderParameters(EncoderParameters params, bool has_internal_source)
danilchap56359be2017-09-07 07:53:45 -0700117 RTC_EXCLUSIVE_LOCKS_REQUIRED(encoder_crit_);
Peter Boströmdcb89982015-09-15 14:43:47 +0200118
pbos5ad935c2016-01-25 03:52:44 -0800119 rtc::CriticalSection encoder_crit_;
andresp@webrtc.orgf7eb75b2013-09-14 00:25:28 +0000120 VCMGenericEncoder* _encoder;
henrik.lundin@webrtc.orgbec11ef2013-09-23 19:54:25 +0000121 media_optimization::MediaOptimization _mediaOpt;
danilchap56359be2017-09-07 07:53:45 -0700122 VCMEncodedFrameCallback _encodedFrameCallback RTC_GUARDED_BY(encoder_crit_);
kthelgason876222f2016-11-29 01:44:11 -0800123 EncodedImageCallback* const post_encode_callback_;
danilchap56359be2017-09-07 07:53:45 -0700124 VCMCodecDataBase _codecDataBase RTC_GUARDED_BY(encoder_crit_);
125 bool frame_dropper_enabled_ RTC_GUARDED_BY(encoder_crit_);
andresp@webrtc.orge682aa52013-12-19 10:59:48 +0000126
tommi@webrtc.orge07710c2015-02-19 17:43:25 +0000127 // Must be accessed on the construction thread of VideoSender.
128 VideoCodec current_codec_;
perkj4e417b22016-07-14 23:35:55 -0700129 rtc::SequencedTaskChecker sequenced_checker_;
Erik Språng66a641a2015-06-11 14:20:07 +0200130
Peter Boström233bfd22016-01-18 20:23:40 +0100131 rtc::CriticalSection params_crit_;
danilchap56359be2017-09-07 07:53:45 -0700132 EncoderParameters encoder_params_ RTC_GUARDED_BY(params_crit_);
133 bool encoder_has_internal_source_ RTC_GUARDED_BY(params_crit_);
134 std::vector<FrameType> next_frame_types_ RTC_GUARDED_BY(params_crit_);
andresp@webrtc.orgf7eb75b2013-09-14 00:25:28 +0000135};
136
Peter Boström0b250722016-04-22 18:23:15 +0200137class VideoReceiver : public Module {
andresp@webrtc.orgf7eb75b2013-09-14 00:25:28 +0000138 public:
philipel83f831a2016-03-12 03:30:23 -0800139 VideoReceiver(Clock* clock,
140 EventFactory* event_factory,
sprang3911c262016-04-15 01:24:14 -0700141 EncodedImageCallback* pre_decode_image_callback,
philipel721d4022016-12-15 07:10:57 -0800142 VCMTiming* timing,
philipel83f831a2016-03-12 03:30:23 -0800143 NackSender* nack_sender = nullptr,
144 KeyFrameRequestSender* keyframe_request_sender = nullptr);
guidouc3372582017-04-04 07:16:21 -0700145 ~VideoReceiver();
andresp@webrtc.orgf7eb75b2013-09-14 00:25:28 +0000146
andresp@webrtc.orgf7eb75b2013-09-14 00:25:28 +0000147 int32_t RegisterReceiveCodec(const VideoCodec* receiveCodec,
148 int32_t numberOfCores,
149 bool requireKeyFrame);
150
Peter Boström795dbe42015-11-27 14:09:07 +0100151 void RegisterExternalDecoder(VideoDecoder* externalDecoder,
perkj796cfaf2015-12-10 09:27:38 -0800152 uint8_t payloadType);
andresp@webrtc.orgf7eb75b2013-09-14 00:25:28 +0000153 int32_t RegisterReceiveCallback(VCMReceiveCallback* receiveCallback);
154 int32_t RegisterReceiveStatisticsCallback(
155 VCMReceiveStatisticsCallback* receiveStats);
156 int32_t RegisterFrameTypeCallback(VCMFrameTypeCallback* frameTypeCallback);
157 int32_t RegisterPacketRequestCallback(VCMPacketRequestCallback* callback);
andresp@webrtc.orgf7eb75b2013-09-14 00:25:28 +0000158
159 int32_t Decode(uint16_t maxWaitTimeMs);
andresp@webrtc.orgf7eb75b2013-09-14 00:25:28 +0000160
philipelfd5a20f2016-11-15 00:57:57 -0800161 int32_t Decode(const webrtc::VCMEncodedFrame* frame);
162
guidouc3372582017-04-04 07:16:21 -0700163 // Called on the decoder thread when thread is exiting.
164 void DecodingStopped();
tommid0a71ba2017-03-14 04:16:20 -0700165
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);
183 int SetMinReceiverDelay(int desired_delay_ms);
184
pkasting@chromium.org16825b12015-01-12 21:51:21 +0000185 int32_t SetReceiveChannelParameters(int64_t rtt);
andresp@webrtc.orgf7eb75b2013-09-14 00:25:28 +0000186 int32_t SetVideoProtection(VCMVideoProtection videoProtection, bool enable);
187
Peter Boström0b250722016-04-22 18:23:15 +0200188 int64_t TimeUntilNextProcess() override;
189 void Process() 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
andresp@webrtc.orgf7eb75b2013-09-14 00:25:28 +0000193 protected:
guidouc3372582017-04-04 07:16:21 -0700194 int32_t Decode(const webrtc::VCMEncodedFrame& frame)
danilchap56359be2017-09-07 07:53:45 -0700195 RTC_EXCLUSIVE_LOCKS_REQUIRED(receive_crit_);
andresp@webrtc.orgf7eb75b2013-09-14 00:25:28 +0000196 int32_t RequestKeyFrame();
andresp@webrtc.orgf7eb75b2013-09-14 00:25:28 +0000197
198 private:
tommid0a71ba2017-03-14 04:16:20 -0700199 rtc::ThreadChecker construction_thread_;
pbos@webrtc.org20c1f562014-07-04 10:58:12 +0000200 Clock* const clock_;
sprang3911c262016-04-15 01:24:14 -0700201 rtc::CriticalSection process_crit_;
guidouc3372582017-04-04 07:16:21 -0700202 rtc::CriticalSection receive_crit_;
philipel721d4022016-12-15 07:10:57 -0800203 VCMTiming* _timing;
andresp@webrtc.orgf7eb75b2013-09-14 00:25:28 +0000204 VCMReceiver _receiver;
andresp@webrtc.orgf7eb75b2013-09-14 00:25:28 +0000205 VCMDecodedFrameCallback _decodedFrameCallback;
danilchap56359be2017-09-07 07:53:45 -0700206 VCMFrameTypeCallback* _frameTypeCallback RTC_GUARDED_BY(process_crit_);
207 VCMReceiveStatisticsCallback* _receiveStatsCallback
208 RTC_GUARDED_BY(process_crit_);
209 VCMPacketRequestCallback* _packetRequestCallback
210 RTC_GUARDED_BY(process_crit_);
niklase@google.com470e71d2011-07-07 08:21:25 +0000211
guidouc3372582017-04-04 07:16:21 -0700212 VCMFrameBuffer _frameFromFile;
danilchap56359be2017-09-07 07:53:45 -0700213 bool _scheduleKeyRequest RTC_GUARDED_BY(process_crit_);
214 bool drop_frames_until_keyframe_ RTC_GUARDED_BY(process_crit_);
215 size_t max_nack_list_size_ RTC_GUARDED_BY(process_crit_);
sprang3911c262016-04-15 01:24:14 -0700216
danilchap56359be2017-09-07 07:53:45 -0700217 VCMCodecDataBase _codecDataBase RTC_GUARDED_BY(receive_crit_);
guidouc3372582017-04-04 07:16:21 -0700218 EncodedImageCallback* pre_decode_image_callback_;
Peter Boströmed3277b2016-02-02 15:40:04 +0100219
guidouc3372582017-04-04 07:16:21 -0700220 VCMProcessTimer _receiveStatsTimer;
221 VCMProcessTimer _retransmissionTimer;
222 VCMProcessTimer _keyRequestTimer;
223 QpParser qp_parser_;
224 ThreadUnsafeOneTimeEvent first_frame_received_;
niklase@google.com470e71d2011-07-07 08:21:25 +0000225};
andresp@webrtc.orgf7eb75b2013-09-14 00:25:28 +0000226
227} // namespace vcm
pbos@webrtc.orgd900e8b2013-07-03 15:12:26 +0000228} // namespace webrtc
Mirko Bonadei92ea95e2017-09-15 06:47:31 +0200229#endif // MODULES_VIDEO_CODING_VIDEO_CODING_IMPL_H_