blob: d6e722db3e04ee9e13a6d84d56fd79d578fbbada [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
11#ifndef WEBRTC_MODULES_VIDEO_CODING_VIDEO_CODING_IMPL_H_
12#define WEBRTC_MODULES_VIDEO_CODING_VIDEO_CODING_IMPL_H_
13
Henrik Kjellander2557b862015-11-18 22:00:21 +010014#include "webrtc/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
skvlad98bb6642016-04-07 15:36:45 -070020#include "webrtc/base/onetimeevent.h"
perkj4e417b22016-07-14 23:35:55 -070021#include "webrtc/base/sequenced_task_checker.h"
tommid0a71ba2017-03-14 04:16:20 -070022#include "webrtc/base/thread_annotations.h"
23#include "webrtc/base/thread_checker.h"
pbosa96b60b2016-04-18 21:12:48 -070024#include "webrtc/common_video/include/frame_callback.h"
Henrik Kjellander2557b862015-11-18 22:00:21 +010025#include "webrtc/modules/video_coding/codec_database.h"
26#include "webrtc/modules/video_coding/frame_buffer.h"
27#include "webrtc/modules/video_coding/generic_decoder.h"
28#include "webrtc/modules/video_coding/generic_encoder.h"
29#include "webrtc/modules/video_coding/jitter_buffer.h"
30#include "webrtc/modules/video_coding/media_optimization.h"
kjellandera8d8aad2017-03-08 05:42:26 -080031#include "webrtc/modules/video_coding/qp_parser.h"
Henrik Kjellander2557b862015-11-18 22:00:21 +010032#include "webrtc/modules/video_coding/receiver.h"
33#include "webrtc/modules/video_coding/timing.h"
Henrik Kjellander98f53512015-10-28 18:17:40 +010034#include "webrtc/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
tommie3aa88b2017-04-04 03:53:02 -070038class ProcessThread;
Erik Språng08127a92016-11-16 16:41:30 +010039class VideoBitrateAllocator;
sprang1a646ee2016-12-01 06:34:11 -080040class VideoBitrateAllocationObserver;
Erik Språng08127a92016-11-16 16:41:30 +010041
andresp@webrtc.orgf7eb75b2013-09-14 00:25:28 +000042namespace vcm {
niklase@google.com470e71d2011-07-07 08:21:25 +000043
andresp@webrtc.orgf7eb75b2013-09-14 00:25:28 +000044class VCMProcessTimer {
45 public:
sprang40217c32016-11-21 05:41:52 -080046 static const int64_t kDefaultProcessIntervalMs = 1000;
47
pkasting@chromium.org0b1534c2014-12-15 22:09:40 +000048 VCMProcessTimer(int64_t periodMs, Clock* clock)
andresp@webrtc.orgf7eb75b2013-09-14 00:25:28 +000049 : _clock(clock),
50 _periodMs(periodMs),
51 _latestMs(_clock->TimeInMilliseconds()) {}
pkasting@chromium.org0b1534c2014-12-15 22:09:40 +000052 int64_t Period() const;
53 int64_t TimeUntilProcess() const;
andresp@webrtc.orgf7eb75b2013-09-14 00:25:28 +000054 void Processed();
niklase@google.com470e71d2011-07-07 08:21:25 +000055
andresp@webrtc.orgf7eb75b2013-09-14 00:25:28 +000056 private:
57 Clock* _clock;
pkasting@chromium.org0b1534c2014-12-15 22:09:40 +000058 int64_t _periodMs;
andresp@webrtc.orgf7eb75b2013-09-14 00:25:28 +000059 int64_t _latestMs;
niklase@google.com470e71d2011-07-07 08:21:25 +000060};
61
Peter Boströmcd5c25c2016-04-21 16:48:08 +020062class VideoSender : public Module {
andresp@webrtc.orgf7eb75b2013-09-14 00:25:28 +000063 public:
64 typedef VideoCodingModule::SenderNackMode SenderNackMode;
65
pbos@webrtc.org891d4832015-02-26 13:15:22 +000066 VideoSender(Clock* clock,
67 EncodedImageCallback* post_encode_callback,
perkj376b1922016-05-02 11:35:24 -070068 VCMSendStatisticsCallback* send_stats_callback);
andresp@webrtc.org1df9dc32014-01-09 08:01:57 +000069
andresp@webrtc.orgf7eb75b2013-09-14 00:25:28 +000070 ~VideoSender();
71
andresp@webrtc.orgf7eb75b2013-09-14 00:25:28 +000072 // Register the send codec to be used.
tommi@webrtc.orge07710c2015-02-19 17:43:25 +000073 // This method must be called on the construction thread.
andresp@webrtc.orgf7eb75b2013-09-14 00:25:28 +000074 int32_t RegisterSendCodec(const VideoCodec* sendCodec,
75 uint32_t numberOfCores,
76 uint32_t maxPayloadSize);
tommi@webrtc.orge07710c2015-02-19 17:43:25 +000077
Peter Boström795dbe42015-11-27 14:09:07 +010078 void RegisterExternalEncoder(VideoEncoder* externalEncoder,
79 uint8_t payloadType,
80 bool internalSource);
andresp@webrtc.orgf7eb75b2013-09-14 00:25:28 +000081
andresp@webrtc.orgf7eb75b2013-09-14 00:25:28 +000082 int Bitrate(unsigned int* bitrate) const;
83 int FrameRate(unsigned int* framerate) const;
84
sprang1a646ee2016-12-01 06:34:11 -080085 // Update the channel parameters based on new rates and rtt. This will also
86 // cause an immediate call to VideoEncoder::SetRateAllocation().
87 int32_t SetChannelParameters(
88 uint32_t target_bitrate_bps,
89 uint8_t loss_rate,
90 int64_t rtt,
91 VideoBitrateAllocator* bitrate_allocator,
92 VideoBitrateAllocationObserver* bitrate_updated_callback);
93
94 // Updates the channel parameters with a new bitrate allocation, but using the
95 // current targit_bitrate, loss rate and rtt. That is, the distribution or
96 // caps may be updated to a change to a new VideoCodec or allocation mode.
97 // The new parameters will be stored as pending EncoderParameters, and the
98 // encoder will only be updated on the next frame.
99 void UpdateChannelParemeters(
100 VideoBitrateAllocator* bitrate_allocator,
101 VideoBitrateAllocationObserver* bitrate_updated_callback);
Erik Språng08127a92016-11-16 16:41:30 +0100102
Per69b332d2016-06-02 15:45:42 +0200103 // Deprecated:
104 // TODO(perkj): Remove once no projects use it.
andresp@webrtc.orgf7eb75b2013-09-14 00:25:28 +0000105 int32_t RegisterProtectionCallback(VCMProtectionCallback* protection);
andresp@webrtc.orgf7eb75b2013-09-14 00:25:28 +0000106
Miguel Casas-Sanchez47650702015-05-29 17:21:40 -0700107 int32_t AddVideoFrame(const VideoFrame& videoFrame,
andresp@webrtc.orgf7eb75b2013-09-14 00:25:28 +0000108 const CodecSpecificInfo* codecSpecificInfo);
109
perkj600246e2016-05-04 11:26:51 -0700110 int32_t IntraFrameRequest(size_t stream_index);
andresp@webrtc.orgf7eb75b2013-09-14 00:25:28 +0000111 int32_t EnableFrameDropper(bool enable);
andresp@webrtc.orgf7eb75b2013-09-14 00:25:28 +0000112
Peter Boströmcd5c25c2016-04-21 16:48:08 +0200113 int64_t TimeUntilNextProcess() override;
114 void Process() override;
andresp@webrtc.orgf7eb75b2013-09-14 00:25:28 +0000115
116 private:
Erik Språng08127a92016-11-16 16:41:30 +0100117 EncoderParameters UpdateEncoderParameters(
118 const EncoderParameters& params,
119 VideoBitrateAllocator* bitrate_allocator,
120 uint32_t target_bitrate_bps);
perkj57c21f92016-06-17 07:27:16 -0700121 void SetEncoderParameters(EncoderParameters params, bool has_internal_source)
Peter Boström233bfd22016-01-18 20:23:40 +0100122 EXCLUSIVE_LOCKS_REQUIRED(encoder_crit_);
Peter Boströmdcb89982015-09-15 14:43:47 +0200123
124 Clock* const clock_;
andresp@webrtc.orgf7eb75b2013-09-14 00:25:28 +0000125
pbos5ad935c2016-01-25 03:52:44 -0800126 rtc::CriticalSection encoder_crit_;
andresp@webrtc.orgf7eb75b2013-09-14 00:25:28 +0000127 VCMGenericEncoder* _encoder;
henrik.lundin@webrtc.orgbec11ef2013-09-23 19:54:25 +0000128 media_optimization::MediaOptimization _mediaOpt;
perkj376b1922016-05-02 11:35:24 -0700129 VCMEncodedFrameCallback _encodedFrameCallback GUARDED_BY(encoder_crit_);
kthelgason876222f2016-11-29 01:44:11 -0800130 EncodedImageCallback* const post_encode_callback_;
perkj376b1922016-05-02 11:35:24 -0700131 VCMSendStatisticsCallback* const send_stats_callback_;
Peter Boström233bfd22016-01-18 20:23:40 +0100132 VCMCodecDataBase _codecDataBase GUARDED_BY(encoder_crit_);
133 bool frame_dropper_enabled_ GUARDED_BY(encoder_crit_);
andresp@webrtc.orgf7eb75b2013-09-14 00:25:28 +0000134 VCMProcessTimer _sendStatsTimer;
andresp@webrtc.orge682aa52013-12-19 10:59:48 +0000135
tommi@webrtc.orge07710c2015-02-19 17:43:25 +0000136 // Must be accessed on the construction thread of VideoSender.
137 VideoCodec current_codec_;
perkj4e417b22016-07-14 23:35:55 -0700138 rtc::SequencedTaskChecker sequenced_checker_;
Erik Språng66a641a2015-06-11 14:20:07 +0200139
Peter Boström233bfd22016-01-18 20:23:40 +0100140 rtc::CriticalSection params_crit_;
141 EncoderParameters encoder_params_ GUARDED_BY(params_crit_);
142 bool encoder_has_internal_source_ GUARDED_BY(params_crit_);
143 std::vector<FrameType> next_frame_types_ GUARDED_BY(params_crit_);
andresp@webrtc.orgf7eb75b2013-09-14 00:25:28 +0000144};
145
Peter Boström0b250722016-04-22 18:23:15 +0200146class VideoReceiver : public Module {
andresp@webrtc.orgf7eb75b2013-09-14 00:25:28 +0000147 public:
philipel83f831a2016-03-12 03:30:23 -0800148 VideoReceiver(Clock* clock,
149 EventFactory* event_factory,
sprang3911c262016-04-15 01:24:14 -0700150 EncodedImageCallback* pre_decode_image_callback,
philipel721d4022016-12-15 07:10:57 -0800151 VCMTiming* timing,
philipel83f831a2016-03-12 03:30:23 -0800152 NackSender* nack_sender = nullptr,
153 KeyFrameRequestSender* keyframe_request_sender = nullptr);
tommie3aa88b2017-04-04 03:53:02 -0700154 ~VideoReceiver() override;
andresp@webrtc.orgf7eb75b2013-09-14 00:25:28 +0000155
andresp@webrtc.orgf7eb75b2013-09-14 00:25:28 +0000156 int32_t RegisterReceiveCodec(const VideoCodec* receiveCodec,
157 int32_t numberOfCores,
158 bool requireKeyFrame);
159
Peter Boström795dbe42015-11-27 14:09:07 +0100160 void RegisterExternalDecoder(VideoDecoder* externalDecoder,
perkj796cfaf2015-12-10 09:27:38 -0800161 uint8_t payloadType);
andresp@webrtc.orgf7eb75b2013-09-14 00:25:28 +0000162 int32_t RegisterReceiveCallback(VCMReceiveCallback* receiveCallback);
163 int32_t RegisterReceiveStatisticsCallback(
164 VCMReceiveStatisticsCallback* receiveStats);
165 int32_t RegisterFrameTypeCallback(VCMFrameTypeCallback* frameTypeCallback);
166 int32_t RegisterPacketRequestCallback(VCMPacketRequestCallback* callback);
andresp@webrtc.orgf7eb75b2013-09-14 00:25:28 +0000167
168 int32_t Decode(uint16_t maxWaitTimeMs);
andresp@webrtc.orgf7eb75b2013-09-14 00:25:28 +0000169
philipelfd5a20f2016-11-15 00:57:57 -0800170 int32_t Decode(const webrtc::VCMEncodedFrame* frame);
171
tommie3aa88b2017-04-04 03:53:02 -0700172#if defined(WEBRTC_ANDROID)
173 // See https://bugs.chromium.org/p/webrtc/issues/detail?id=7361
174 void PollDecodedFrames();
175#endif
tommid0a71ba2017-03-14 04:16:20 -0700176
andresp@webrtc.orgf7eb75b2013-09-14 00:25:28 +0000177 int32_t IncomingPacket(const uint8_t* incomingPayload,
pkasting@chromium.org4591fbd2014-11-20 22:28:14 +0000178 size_t payloadLength,
andresp@webrtc.orgf7eb75b2013-09-14 00:25:28 +0000179 const WebRtcRTPHeader& rtpInfo);
180 int32_t SetMinimumPlayoutDelay(uint32_t minPlayoutDelayMs);
181 int32_t SetRenderDelay(uint32_t timeMS);
182 int32_t Delay() const;
andresp@webrtc.orgf7eb75b2013-09-14 00:25:28 +0000183
tommia5c18d72017-03-20 10:43:23 -0700184 // DEPRECATED.
185 int SetReceiverRobustnessMode(
186 VideoCodingModule::ReceiverRobustness robustnessMode,
187 VCMDecodeErrorMode errorMode);
188
andresp@webrtc.orgf7eb75b2013-09-14 00:25:28 +0000189 void SetNackSettings(size_t max_nack_list_size,
190 int max_packet_age_to_nack,
191 int max_incomplete_time_ms);
192
193 void SetDecodeErrorMode(VCMDecodeErrorMode decode_error_mode);
194 int SetMinReceiverDelay(int desired_delay_ms);
195
pkasting@chromium.org16825b12015-01-12 21:51:21 +0000196 int32_t SetReceiveChannelParameters(int64_t rtt);
andresp@webrtc.orgf7eb75b2013-09-14 00:25:28 +0000197 int32_t SetVideoProtection(VCMVideoProtection videoProtection, bool enable);
198
Peter Boström0b250722016-04-22 18:23:15 +0200199 int64_t TimeUntilNextProcess() override;
200 void Process() override;
tommie3aa88b2017-04-04 03:53:02 -0700201 void ProcessThreadAttached(ProcessThread* process_thread) override;
andresp@webrtc.orgf7eb75b2013-09-14 00:25:28 +0000202
pbos@webrtc.org4dd40d62015-02-17 13:22:43 +0000203 void TriggerDecoderShutdown();
tommie3aa88b2017-04-04 03:53:02 -0700204 void DecoderThreadStarting();
205 void DecoderThreadStopped();
sprang@webrtc.org40709352013-11-26 11:41:59 +0000206
andresp@webrtc.orgf7eb75b2013-09-14 00:25:28 +0000207 protected:
tommie3aa88b2017-04-04 03:53:02 -0700208 int32_t Decode(const webrtc::VCMEncodedFrame& frame);
andresp@webrtc.orgf7eb75b2013-09-14 00:25:28 +0000209 int32_t RequestKeyFrame();
andresp@webrtc.orgf7eb75b2013-09-14 00:25:28 +0000210
211 private:
tommie3aa88b2017-04-04 03:53:02 -0700212 // Used for DCHECKing thread correctness.
213 // In build where DCHECKs are enabled, will return false before
214 // DecoderThreadStarting is called, then true until DecoderThreadStopped
215 // is called.
216 // In builds where DCHECKs aren't enabled, it will return true.
217 bool IsDecoderThreadRunning();
218
tommid0a71ba2017-03-14 04:16:20 -0700219 rtc::ThreadChecker construction_thread_;
tommie3aa88b2017-04-04 03:53:02 -0700220 rtc::ThreadChecker decoder_thread_;
221 rtc::ThreadChecker module_thread_;
pbos@webrtc.org20c1f562014-07-04 10:58:12 +0000222 Clock* const clock_;
sprang3911c262016-04-15 01:24:14 -0700223 rtc::CriticalSection process_crit_;
philipel721d4022016-12-15 07:10:57 -0800224 VCMTiming* _timing;
andresp@webrtc.orgf7eb75b2013-09-14 00:25:28 +0000225 VCMReceiver _receiver;
andresp@webrtc.orgf7eb75b2013-09-14 00:25:28 +0000226 VCMDecodedFrameCallback _decodedFrameCallback;
niklase@google.com470e71d2011-07-07 08:21:25 +0000227
tommie3aa88b2017-04-04 03:53:02 -0700228 // These callbacks are set on the construction thread before being attached
229 // to the module thread or decoding started, so a lock is not required.
230 VCMFrameTypeCallback* _frameTypeCallback;
231 VCMReceiveStatisticsCallback* _receiveStatsCallback;
232 VCMPacketRequestCallback* _packetRequestCallback;
233
234 // Used on both the module and decoder thread.
sprang3911c262016-04-15 01:24:14 -0700235 bool _scheduleKeyRequest GUARDED_BY(process_crit_);
236 bool drop_frames_until_keyframe_ GUARDED_BY(process_crit_);
sprang3911c262016-04-15 01:24:14 -0700237
tommie3aa88b2017-04-04 03:53:02 -0700238 // Modified on the construction thread while not attached to the process
239 // thread. Once attached to the process thread, its value is only read
240 // so a lock is not required.
241 size_t max_nack_list_size_;
Peter Boströmed3277b2016-02-02 15:40:04 +0100242
tommie3aa88b2017-04-04 03:53:02 -0700243 // Callbacks are set before the decoder thread starts.
244 // Once the decoder thread has been started, usage of |_codecDataBase| moves
245 // over to the decoder thread.
246 VCMCodecDataBase _codecDataBase;
247 EncodedImageCallback* const pre_decode_image_callback_;
248
249 VCMProcessTimer _receiveStatsTimer ACCESS_ON(module_thread_);
250 VCMProcessTimer _retransmissionTimer ACCESS_ON(module_thread_);
251 VCMProcessTimer _keyRequestTimer ACCESS_ON(module_thread_);
252 QpParser qp_parser_ ACCESS_ON(decoder_thread_);
253 ThreadUnsafeOneTimeEvent first_frame_received_ ACCESS_ON(decoder_thread_);
254 // Modified on the construction thread. Can be read without a lock and assumed
255 // to be non-null on the module and decoder threads.
256 ProcessThread* process_thread_ = nullptr;
257 bool is_attached_to_process_thread_ ACCESS_ON(construction_thread_) = false;
258#if RTC_DCHECK_IS_ON
259 bool decoder_thread_is_running_ = false;
260#endif
niklase@google.com470e71d2011-07-07 08:21:25 +0000261};
andresp@webrtc.orgf7eb75b2013-09-14 00:25:28 +0000262
263} // namespace vcm
pbos@webrtc.orgd900e8b2013-07-03 15:12:26 +0000264} // namespace webrtc
andresp@webrtc.orgf7eb75b2013-09-14 00:25:28 +0000265#endif // WEBRTC_MODULES_VIDEO_CODING_VIDEO_CODING_IMPL_H_