blob: 433b37ab241f70d4a673cb8312c7d521825545c9 [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"
Mirko Bonadei92ea95e2017-09-15 06:47:31 +020022#include "modules/video_coding/frame_buffer.h"
23#include "modules/video_coding/generic_decoder.h"
Mirko Bonadei92ea95e2017-09-15 06:47:31 +020024#include "modules/video_coding/jitter_buffer.h"
Mirko Bonadei92ea95e2017-09-15 06:47:31 +020025#include "modules/video_coding/receiver.h"
26#include "modules/video_coding/timing.h"
Steve Anton10542f22019-01-11 09:11:00 -080027#include "rtc_base/one_time_event.h"
Mirko Bonadei92ea95e2017-09-15 06:47:31 +020028#include "rtc_base/sequenced_task_checker.h"
29#include "rtc_base/thread_annotations.h"
30#include "rtc_base/thread_checker.h"
31#include "system_wrappers/include/clock.h"
niklase@google.com470e71d2011-07-07 08:21:25 +000032
andresp@webrtc.orgf7eb75b2013-09-14 00:25:28 +000033namespace webrtc {
sprang@webrtc.org40709352013-11-26 11:41:59 +000034
Erik Språng08127a92016-11-16 16:41:30 +010035class VideoBitrateAllocator;
sprang1a646ee2016-12-01 06:34:11 -080036class VideoBitrateAllocationObserver;
Erik Språng08127a92016-11-16 16:41:30 +010037
andresp@webrtc.orgf7eb75b2013-09-14 00:25:28 +000038namespace vcm {
niklase@google.com470e71d2011-07-07 08:21:25 +000039
andresp@webrtc.orgf7eb75b2013-09-14 00:25:28 +000040class VCMProcessTimer {
41 public:
sprang40217c32016-11-21 05:41:52 -080042 static const int64_t kDefaultProcessIntervalMs = 1000;
43
pkasting@chromium.org0b1534c2014-12-15 22:09:40 +000044 VCMProcessTimer(int64_t periodMs, Clock* clock)
andresp@webrtc.orgf7eb75b2013-09-14 00:25:28 +000045 : _clock(clock),
46 _periodMs(periodMs),
47 _latestMs(_clock->TimeInMilliseconds()) {}
pkasting@chromium.org0b1534c2014-12-15 22:09:40 +000048 int64_t Period() const;
49 int64_t TimeUntilProcess() const;
andresp@webrtc.orgf7eb75b2013-09-14 00:25:28 +000050 void Processed();
niklase@google.com470e71d2011-07-07 08:21:25 +000051
andresp@webrtc.orgf7eb75b2013-09-14 00:25:28 +000052 private:
53 Clock* _clock;
pkasting@chromium.org0b1534c2014-12-15 22:09:40 +000054 int64_t _periodMs;
andresp@webrtc.orgf7eb75b2013-09-14 00:25:28 +000055 int64_t _latestMs;
niklase@google.com470e71d2011-07-07 08:21:25 +000056};
57
Peter Boström0b250722016-04-22 18:23:15 +020058class VideoReceiver : public Module {
andresp@webrtc.orgf7eb75b2013-09-14 00:25:28 +000059 public:
philipel83f831a2016-03-12 03:30:23 -080060 VideoReceiver(Clock* clock,
philipel721d4022016-12-15 07:10:57 -080061 VCMTiming* timing,
philipel83f831a2016-03-12 03:30:23 -080062 NackSender* nack_sender = nullptr,
63 KeyFrameRequestSender* keyframe_request_sender = nullptr);
Tommifbf3bce2018-02-21 15:56:05 +010064 ~VideoReceiver() override;
andresp@webrtc.orgf7eb75b2013-09-14 00:25:28 +000065
andresp@webrtc.orgf7eb75b2013-09-14 00:25:28 +000066 int32_t RegisterReceiveCodec(const VideoCodec* receiveCodec,
67 int32_t numberOfCores,
68 bool requireKeyFrame);
69
Peter Boström795dbe42015-11-27 14:09:07 +010070 void RegisterExternalDecoder(VideoDecoder* externalDecoder,
perkj796cfaf2015-12-10 09:27:38 -080071 uint8_t payloadType);
andresp@webrtc.orgf7eb75b2013-09-14 00:25:28 +000072 int32_t RegisterReceiveCallback(VCMReceiveCallback* receiveCallback);
73 int32_t RegisterReceiveStatisticsCallback(
74 VCMReceiveStatisticsCallback* receiveStats);
75 int32_t RegisterFrameTypeCallback(VCMFrameTypeCallback* frameTypeCallback);
76 int32_t RegisterPacketRequestCallback(VCMPacketRequestCallback* callback);
andresp@webrtc.orgf7eb75b2013-09-14 00:25:28 +000077
78 int32_t Decode(uint16_t maxWaitTimeMs);
andresp@webrtc.orgf7eb75b2013-09-14 00:25:28 +000079
philipelfd5a20f2016-11-15 00:57:57 -080080 int32_t Decode(const webrtc::VCMEncodedFrame* frame);
81
andresp@webrtc.orgf7eb75b2013-09-14 00:25:28 +000082 int32_t IncomingPacket(const uint8_t* incomingPayload,
pkasting@chromium.org4591fbd2014-11-20 22:28:14 +000083 size_t payloadLength,
andresp@webrtc.orgf7eb75b2013-09-14 00:25:28 +000084 const WebRtcRTPHeader& rtpInfo);
85 int32_t SetMinimumPlayoutDelay(uint32_t minPlayoutDelayMs);
86 int32_t SetRenderDelay(uint32_t timeMS);
87 int32_t Delay() const;
andresp@webrtc.orgf7eb75b2013-09-14 00:25:28 +000088
tommia5c18d72017-03-20 10:43:23 -070089 // DEPRECATED.
90 int SetReceiverRobustnessMode(
Niels Möller375b3462019-01-10 15:35:56 +010091 VideoCodingModule::ReceiverRobustness robustnessMode);
tommia5c18d72017-03-20 10:43:23 -070092
andresp@webrtc.orgf7eb75b2013-09-14 00:25:28 +000093 void SetNackSettings(size_t max_nack_list_size,
94 int max_packet_age_to_nack,
95 int max_incomplete_time_ms);
96
pkasting@chromium.org16825b12015-01-12 21:51:21 +000097 int32_t SetReceiveChannelParameters(int64_t rtt);
andresp@webrtc.orgf7eb75b2013-09-14 00:25:28 +000098 int32_t SetVideoProtection(VCMVideoProtection videoProtection, bool enable);
99
Peter Boström0b250722016-04-22 18:23:15 +0200100 int64_t TimeUntilNextProcess() override;
101 void Process() override;
Tommifbf3bce2018-02-21 15:56:05 +0100102 void ProcessThreadAttached(ProcessThread* process_thread) override;
andresp@webrtc.orgf7eb75b2013-09-14 00:25:28 +0000103
pbos@webrtc.org4dd40d62015-02-17 13:22:43 +0000104 void TriggerDecoderShutdown();
sprang@webrtc.org40709352013-11-26 11:41:59 +0000105
Tommifbf3bce2018-02-21 15:56:05 +0100106 // Notification methods that are used to check our internal state and validate
107 // threading assumptions. These are called by VideoReceiveStream.
108 // See |IsDecoderThreadRunning()| for more details.
109 void DecoderThreadStarting();
110 void DecoderThreadStopped();
111
andresp@webrtc.orgf7eb75b2013-09-14 00:25:28 +0000112 protected:
Tommifbf3bce2018-02-21 15:56:05 +0100113 int32_t Decode(const webrtc::VCMEncodedFrame& frame);
andresp@webrtc.orgf7eb75b2013-09-14 00:25:28 +0000114 int32_t RequestKeyFrame();
andresp@webrtc.orgf7eb75b2013-09-14 00:25:28 +0000115
116 private:
Tommifbf3bce2018-02-21 15:56:05 +0100117 // Used for DCHECKing thread correctness.
118 // In build where DCHECKs are enabled, will return false before
119 // DecoderThreadStarting is called, then true until DecoderThreadStopped
120 // is called.
121 // In builds where DCHECKs aren't enabled, it will return true.
122 bool IsDecoderThreadRunning();
123
124 rtc::ThreadChecker construction_thread_checker_;
125 rtc::ThreadChecker decoder_thread_checker_;
126 rtc::ThreadChecker module_thread_checker_;
pbos@webrtc.org20c1f562014-07-04 10:58:12 +0000127 Clock* const clock_;
sprang3911c262016-04-15 01:24:14 -0700128 rtc::CriticalSection process_crit_;
philipel721d4022016-12-15 07:10:57 -0800129 VCMTiming* _timing;
andresp@webrtc.orgf7eb75b2013-09-14 00:25:28 +0000130 VCMReceiver _receiver;
andresp@webrtc.orgf7eb75b2013-09-14 00:25:28 +0000131 VCMDecodedFrameCallback _decodedFrameCallback;
niklase@google.com470e71d2011-07-07 08:21:25 +0000132
Tommifbf3bce2018-02-21 15:56:05 +0100133 // These callbacks are set on the construction thread before being attached
134 // to the module thread or decoding started, so a lock is not required.
135 VCMFrameTypeCallback* _frameTypeCallback;
136 VCMReceiveStatisticsCallback* _receiveStatsCallback;
137 VCMPacketRequestCallback* _packetRequestCallback;
138
139 // Used on both the module and decoder thread.
danilchap56359be2017-09-07 07:53:45 -0700140 bool _scheduleKeyRequest RTC_GUARDED_BY(process_crit_);
141 bool drop_frames_until_keyframe_ RTC_GUARDED_BY(process_crit_);
sprang3911c262016-04-15 01:24:14 -0700142
Tommifbf3bce2018-02-21 15:56:05 +0100143 // Modified on the construction thread while not attached to the process
144 // thread. Once attached to the process thread, its value is only read
145 // so a lock is not required.
146 size_t max_nack_list_size_;
Peter Boströmed3277b2016-02-02 15:40:04 +0100147
Tommifbf3bce2018-02-21 15:56:05 +0100148 // Callbacks are set before the decoder thread starts.
149 // Once the decoder thread has been started, usage of |_codecDataBase| moves
150 // over to the decoder thread.
151 VCMDecoderDataBase _codecDataBase;
Tommifbf3bce2018-02-21 15:56:05 +0100152
153 VCMProcessTimer _receiveStatsTimer RTC_GUARDED_BY(module_thread_checker_);
154 VCMProcessTimer _retransmissionTimer RTC_GUARDED_BY(module_thread_checker_);
155 VCMProcessTimer _keyRequestTimer RTC_GUARDED_BY(module_thread_checker_);
Tommifbf3bce2018-02-21 15:56:05 +0100156 ThreadUnsafeOneTimeEvent first_frame_received_
157 RTC_GUARDED_BY(decoder_thread_checker_);
158 // Modified on the construction thread. Can be read without a lock and assumed
159 // to be non-null on the module and decoder threads.
160 ProcessThread* process_thread_ = nullptr;
161 bool is_attached_to_process_thread_
162 RTC_GUARDED_BY(construction_thread_checker_) = false;
163#if RTC_DCHECK_IS_ON
164 bool decoder_thread_is_running_ = false;
165#endif
niklase@google.com470e71d2011-07-07 08:21:25 +0000166};
andresp@webrtc.orgf7eb75b2013-09-14 00:25:28 +0000167
168} // namespace vcm
pbos@webrtc.orgd900e8b2013-07-03 15:12:26 +0000169} // namespace webrtc
Mirko Bonadei92ea95e2017-09-15 06:47:31 +0200170#endif // MODULES_VIDEO_CODING_VIDEO_CODING_IMPL_H_