niklase@google.com | 470e71d | 2011-07-07 08:21:25 +0000 | [diff] [blame] | 1 | /* |
pwestin@webrtc.org | 52fd98d | 2012-02-13 09:03:53 +0000 | [diff] [blame] | 2 | * Copyright (c) 2012 The WebRTC project authors. All Rights Reserved. |
niklase@google.com | 470e71d | 2011-07-07 08:21:25 +0000 | [diff] [blame] | 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 Bonadei | 92ea95e | 2017-09-15 06:47:31 +0200 | [diff] [blame] | 11 | #ifndef MODULES_VIDEO_CODING_VIDEO_CODING_IMPL_H_ |
| 12 | #define MODULES_VIDEO_CODING_VIDEO_CODING_IMPL_H_ |
niklase@google.com | 470e71d | 2011-07-07 08:21:25 +0000 | [diff] [blame] | 13 | |
Mirko Bonadei | 92ea95e | 2017-09-15 06:47:31 +0200 | [diff] [blame] | 14 | #include "modules/video_coding/include/video_coding.h" |
niklase@google.com | 470e71d | 2011-07-07 08:21:25 +0000 | [diff] [blame] | 15 | |
kwiberg | 3f55dea | 2016-02-29 05:51:59 -0800 | [diff] [blame] | 16 | #include <memory> |
perkj | 376b192 | 2016-05-02 11:35:24 -0700 | [diff] [blame] | 17 | #include <string> |
stefan@webrtc.org | c530043 | 2012-10-08 07:06:53 +0000 | [diff] [blame] | 18 | #include <vector> |
| 19 | |
Erik Språng | eee3920 | 2018-11-15 17:52:43 +0100 | [diff] [blame] | 20 | #include "absl/types/optional.h" |
Niels Möller | f906378 | 2018-02-20 16:09:48 +0100 | [diff] [blame] | 21 | #include "modules/video_coding/decoder_database.h" |
| 22 | #include "modules/video_coding/encoder_database.h" |
Mirko Bonadei | 92ea95e | 2017-09-15 06:47:31 +0200 | [diff] [blame] | 23 | #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" |
Mirko Bonadei | 92ea95e | 2017-09-15 06:47:31 +0200 | [diff] [blame] | 27 | #include "modules/video_coding/receiver.h" |
| 28 | #include "modules/video_coding/timing.h" |
Steve Anton | 10542f2 | 2019-01-11 09:11:00 -0800 | [diff] [blame^] | 29 | #include "rtc_base/one_time_event.h" |
Mirko Bonadei | 92ea95e | 2017-09-15 06:47:31 +0200 | [diff] [blame] | 30 | #include "rtc_base/sequenced_task_checker.h" |
| 31 | #include "rtc_base/thread_annotations.h" |
| 32 | #include "rtc_base/thread_checker.h" |
| 33 | #include "system_wrappers/include/clock.h" |
niklase@google.com | 470e71d | 2011-07-07 08:21:25 +0000 | [diff] [blame] | 34 | |
andresp@webrtc.org | f7eb75b | 2013-09-14 00:25:28 +0000 | [diff] [blame] | 35 | namespace webrtc { |
sprang@webrtc.org | 4070935 | 2013-11-26 11:41:59 +0000 | [diff] [blame] | 36 | |
Erik Språng | 08127a9 | 2016-11-16 16:41:30 +0100 | [diff] [blame] | 37 | class VideoBitrateAllocator; |
sprang | 1a646ee | 2016-12-01 06:34:11 -0800 | [diff] [blame] | 38 | class VideoBitrateAllocationObserver; |
Erik Språng | 08127a9 | 2016-11-16 16:41:30 +0100 | [diff] [blame] | 39 | |
andresp@webrtc.org | f7eb75b | 2013-09-14 00:25:28 +0000 | [diff] [blame] | 40 | namespace vcm { |
niklase@google.com | 470e71d | 2011-07-07 08:21:25 +0000 | [diff] [blame] | 41 | |
andresp@webrtc.org | f7eb75b | 2013-09-14 00:25:28 +0000 | [diff] [blame] | 42 | class VCMProcessTimer { |
| 43 | public: |
sprang | 40217c3 | 2016-11-21 05:41:52 -0800 | [diff] [blame] | 44 | static const int64_t kDefaultProcessIntervalMs = 1000; |
| 45 | |
pkasting@chromium.org | 0b1534c | 2014-12-15 22:09:40 +0000 | [diff] [blame] | 46 | VCMProcessTimer(int64_t periodMs, Clock* clock) |
andresp@webrtc.org | f7eb75b | 2013-09-14 00:25:28 +0000 | [diff] [blame] | 47 | : _clock(clock), |
| 48 | _periodMs(periodMs), |
| 49 | _latestMs(_clock->TimeInMilliseconds()) {} |
pkasting@chromium.org | 0b1534c | 2014-12-15 22:09:40 +0000 | [diff] [blame] | 50 | int64_t Period() const; |
| 51 | int64_t TimeUntilProcess() const; |
andresp@webrtc.org | f7eb75b | 2013-09-14 00:25:28 +0000 | [diff] [blame] | 52 | void Processed(); |
niklase@google.com | 470e71d | 2011-07-07 08:21:25 +0000 | [diff] [blame] | 53 | |
andresp@webrtc.org | f7eb75b | 2013-09-14 00:25:28 +0000 | [diff] [blame] | 54 | private: |
| 55 | Clock* _clock; |
pkasting@chromium.org | 0b1534c | 2014-12-15 22:09:40 +0000 | [diff] [blame] | 56 | int64_t _periodMs; |
andresp@webrtc.org | f7eb75b | 2013-09-14 00:25:28 +0000 | [diff] [blame] | 57 | int64_t _latestMs; |
niklase@google.com | 470e71d | 2011-07-07 08:21:25 +0000 | [diff] [blame] | 58 | }; |
| 59 | |
Niels Möller | a056599 | 2017-10-24 11:37:08 +0200 | [diff] [blame] | 60 | class VideoSender { |
andresp@webrtc.org | f7eb75b | 2013-09-14 00:25:28 +0000 | [diff] [blame] | 61 | public: |
| 62 | typedef VideoCodingModule::SenderNackMode SenderNackMode; |
| 63 | |
Yves Gerey | 665174f | 2018-06-19 15:03:05 +0200 | [diff] [blame] | 64 | VideoSender(Clock* clock, EncodedImageCallback* post_encode_callback); |
andresp@webrtc.org | f7eb75b | 2013-09-14 00:25:28 +0000 | [diff] [blame] | 65 | ~VideoSender(); |
| 66 | |
andresp@webrtc.org | f7eb75b | 2013-09-14 00:25:28 +0000 | [diff] [blame] | 67 | // Register the send codec to be used. |
tommi@webrtc.org | e07710c | 2015-02-19 17:43:25 +0000 | [diff] [blame] | 68 | // This method must be called on the construction thread. |
andresp@webrtc.org | f7eb75b | 2013-09-14 00:25:28 +0000 | [diff] [blame] | 69 | int32_t RegisterSendCodec(const VideoCodec* sendCodec, |
| 70 | uint32_t numberOfCores, |
| 71 | uint32_t maxPayloadSize); |
tommi@webrtc.org | e07710c | 2015-02-19 17:43:25 +0000 | [diff] [blame] | 72 | |
Peter Boström | 795dbe4 | 2015-11-27 14:09:07 +0100 | [diff] [blame] | 73 | void RegisterExternalEncoder(VideoEncoder* externalEncoder, |
Peter Boström | 795dbe4 | 2015-11-27 14:09:07 +0100 | [diff] [blame] | 74 | bool internalSource); |
andresp@webrtc.org | f7eb75b | 2013-09-14 00:25:28 +0000 | [diff] [blame] | 75 | |
Niels Möller | 6bb5ab9 | 2019-01-11 11:11:10 +0100 | [diff] [blame] | 76 | // Update the the encoder with new bitrate allocation and framerate. |
| 77 | int32_t SetChannelParameters(const VideoBitrateAllocation& bitrate_allocation, |
| 78 | uint32_t framerate_fps); |
Erik Språng | 08127a9 | 2016-11-16 16:41:30 +0100 | [diff] [blame] | 79 | |
Miguel Casas-Sanchez | 4765070 | 2015-05-29 17:21:40 -0700 | [diff] [blame] | 80 | int32_t AddVideoFrame(const VideoFrame& videoFrame, |
Erik Språng | eee3920 | 2018-11-15 17:52:43 +0100 | [diff] [blame] | 81 | const CodecSpecificInfo* codecSpecificInfo, |
| 82 | absl::optional<VideoEncoder::EncoderInfo> encoder_info); |
andresp@webrtc.org | f7eb75b | 2013-09-14 00:25:28 +0000 | [diff] [blame] | 83 | |
perkj | 600246e | 2016-05-04 11:26:51 -0700 | [diff] [blame] | 84 | int32_t IntraFrameRequest(size_t stream_index); |
andresp@webrtc.org | f7eb75b | 2013-09-14 00:25:28 +0000 | [diff] [blame] | 85 | |
andresp@webrtc.org | f7eb75b | 2013-09-14 00:25:28 +0000 | [diff] [blame] | 86 | private: |
pbos | 5ad935c | 2016-01-25 03:52:44 -0800 | [diff] [blame] | 87 | rtc::CriticalSection encoder_crit_; |
andresp@webrtc.org | f7eb75b | 2013-09-14 00:25:28 +0000 | [diff] [blame] | 88 | VCMGenericEncoder* _encoder; |
danilchap | 56359be | 2017-09-07 07:53:45 -0700 | [diff] [blame] | 89 | VCMEncodedFrameCallback _encodedFrameCallback RTC_GUARDED_BY(encoder_crit_); |
Niels Möller | f906378 | 2018-02-20 16:09:48 +0100 | [diff] [blame] | 90 | VCMEncoderDataBase _codecDataBase RTC_GUARDED_BY(encoder_crit_); |
Erik Språng | d3438aa | 2018-11-08 16:56:43 +0100 | [diff] [blame] | 91 | |
tommi@webrtc.org | e07710c | 2015-02-19 17:43:25 +0000 | [diff] [blame] | 92 | // Must be accessed on the construction thread of VideoSender. |
| 93 | VideoCodec current_codec_; |
perkj | 4e417b2 | 2016-07-14 23:35:55 -0700 | [diff] [blame] | 94 | rtc::SequencedTaskChecker sequenced_checker_; |
Erik Språng | 66a641a | 2015-06-11 14:20:07 +0200 | [diff] [blame] | 95 | |
Peter Boström | 233bfd2 | 2016-01-18 20:23:40 +0100 | [diff] [blame] | 96 | rtc::CriticalSection params_crit_; |
danilchap | 56359be | 2017-09-07 07:53:45 -0700 | [diff] [blame] | 97 | bool encoder_has_internal_source_ RTC_GUARDED_BY(params_crit_); |
| 98 | std::vector<FrameType> next_frame_types_ RTC_GUARDED_BY(params_crit_); |
andresp@webrtc.org | f7eb75b | 2013-09-14 00:25:28 +0000 | [diff] [blame] | 99 | }; |
| 100 | |
Peter Boström | 0b25072 | 2016-04-22 18:23:15 +0200 | [diff] [blame] | 101 | class VideoReceiver : public Module { |
andresp@webrtc.org | f7eb75b | 2013-09-14 00:25:28 +0000 | [diff] [blame] | 102 | public: |
philipel | 83f831a | 2016-03-12 03:30:23 -0800 | [diff] [blame] | 103 | VideoReceiver(Clock* clock, |
philipel | 721d402 | 2016-12-15 07:10:57 -0800 | [diff] [blame] | 104 | VCMTiming* timing, |
philipel | 83f831a | 2016-03-12 03:30:23 -0800 | [diff] [blame] | 105 | NackSender* nack_sender = nullptr, |
| 106 | KeyFrameRequestSender* keyframe_request_sender = nullptr); |
Tommi | fbf3bce | 2018-02-21 15:56:05 +0100 | [diff] [blame] | 107 | ~VideoReceiver() override; |
andresp@webrtc.org | f7eb75b | 2013-09-14 00:25:28 +0000 | [diff] [blame] | 108 | |
andresp@webrtc.org | f7eb75b | 2013-09-14 00:25:28 +0000 | [diff] [blame] | 109 | int32_t RegisterReceiveCodec(const VideoCodec* receiveCodec, |
| 110 | int32_t numberOfCores, |
| 111 | bool requireKeyFrame); |
| 112 | |
Peter Boström | 795dbe4 | 2015-11-27 14:09:07 +0100 | [diff] [blame] | 113 | void RegisterExternalDecoder(VideoDecoder* externalDecoder, |
perkj | 796cfaf | 2015-12-10 09:27:38 -0800 | [diff] [blame] | 114 | uint8_t payloadType); |
andresp@webrtc.org | f7eb75b | 2013-09-14 00:25:28 +0000 | [diff] [blame] | 115 | int32_t RegisterReceiveCallback(VCMReceiveCallback* receiveCallback); |
| 116 | int32_t RegisterReceiveStatisticsCallback( |
| 117 | VCMReceiveStatisticsCallback* receiveStats); |
| 118 | int32_t RegisterFrameTypeCallback(VCMFrameTypeCallback* frameTypeCallback); |
| 119 | int32_t RegisterPacketRequestCallback(VCMPacketRequestCallback* callback); |
andresp@webrtc.org | f7eb75b | 2013-09-14 00:25:28 +0000 | [diff] [blame] | 120 | |
| 121 | int32_t Decode(uint16_t maxWaitTimeMs); |
andresp@webrtc.org | f7eb75b | 2013-09-14 00:25:28 +0000 | [diff] [blame] | 122 | |
philipel | fd5a20f | 2016-11-15 00:57:57 -0800 | [diff] [blame] | 123 | int32_t Decode(const webrtc::VCMEncodedFrame* frame); |
| 124 | |
andresp@webrtc.org | f7eb75b | 2013-09-14 00:25:28 +0000 | [diff] [blame] | 125 | int32_t IncomingPacket(const uint8_t* incomingPayload, |
pkasting@chromium.org | 4591fbd | 2014-11-20 22:28:14 +0000 | [diff] [blame] | 126 | size_t payloadLength, |
andresp@webrtc.org | f7eb75b | 2013-09-14 00:25:28 +0000 | [diff] [blame] | 127 | const WebRtcRTPHeader& rtpInfo); |
| 128 | int32_t SetMinimumPlayoutDelay(uint32_t minPlayoutDelayMs); |
| 129 | int32_t SetRenderDelay(uint32_t timeMS); |
| 130 | int32_t Delay() const; |
andresp@webrtc.org | f7eb75b | 2013-09-14 00:25:28 +0000 | [diff] [blame] | 131 | |
tommi | a5c18d7 | 2017-03-20 10:43:23 -0700 | [diff] [blame] | 132 | // DEPRECATED. |
| 133 | int SetReceiverRobustnessMode( |
| 134 | VideoCodingModule::ReceiverRobustness robustnessMode, |
| 135 | VCMDecodeErrorMode errorMode); |
| 136 | |
andresp@webrtc.org | f7eb75b | 2013-09-14 00:25:28 +0000 | [diff] [blame] | 137 | void SetNackSettings(size_t max_nack_list_size, |
| 138 | int max_packet_age_to_nack, |
| 139 | int max_incomplete_time_ms); |
| 140 | |
| 141 | void SetDecodeErrorMode(VCMDecodeErrorMode decode_error_mode); |
andresp@webrtc.org | f7eb75b | 2013-09-14 00:25:28 +0000 | [diff] [blame] | 142 | |
pkasting@chromium.org | 16825b1 | 2015-01-12 21:51:21 +0000 | [diff] [blame] | 143 | int32_t SetReceiveChannelParameters(int64_t rtt); |
andresp@webrtc.org | f7eb75b | 2013-09-14 00:25:28 +0000 | [diff] [blame] | 144 | int32_t SetVideoProtection(VCMVideoProtection videoProtection, bool enable); |
| 145 | |
Peter Boström | 0b25072 | 2016-04-22 18:23:15 +0200 | [diff] [blame] | 146 | int64_t TimeUntilNextProcess() override; |
| 147 | void Process() override; |
Tommi | fbf3bce | 2018-02-21 15:56:05 +0100 | [diff] [blame] | 148 | void ProcessThreadAttached(ProcessThread* process_thread) override; |
andresp@webrtc.org | f7eb75b | 2013-09-14 00:25:28 +0000 | [diff] [blame] | 149 | |
pbos@webrtc.org | 4dd40d6 | 2015-02-17 13:22:43 +0000 | [diff] [blame] | 150 | void TriggerDecoderShutdown(); |
sprang@webrtc.org | 4070935 | 2013-11-26 11:41:59 +0000 | [diff] [blame] | 151 | |
Tommi | fbf3bce | 2018-02-21 15:56:05 +0100 | [diff] [blame] | 152 | // Notification methods that are used to check our internal state and validate |
| 153 | // threading assumptions. These are called by VideoReceiveStream. |
| 154 | // See |IsDecoderThreadRunning()| for more details. |
| 155 | void DecoderThreadStarting(); |
| 156 | void DecoderThreadStopped(); |
| 157 | |
andresp@webrtc.org | f7eb75b | 2013-09-14 00:25:28 +0000 | [diff] [blame] | 158 | protected: |
Tommi | fbf3bce | 2018-02-21 15:56:05 +0100 | [diff] [blame] | 159 | int32_t Decode(const webrtc::VCMEncodedFrame& frame); |
andresp@webrtc.org | f7eb75b | 2013-09-14 00:25:28 +0000 | [diff] [blame] | 160 | int32_t RequestKeyFrame(); |
andresp@webrtc.org | f7eb75b | 2013-09-14 00:25:28 +0000 | [diff] [blame] | 161 | |
| 162 | private: |
Tommi | fbf3bce | 2018-02-21 15:56:05 +0100 | [diff] [blame] | 163 | // Used for DCHECKing thread correctness. |
| 164 | // In build where DCHECKs are enabled, will return false before |
| 165 | // DecoderThreadStarting is called, then true until DecoderThreadStopped |
| 166 | // is called. |
| 167 | // In builds where DCHECKs aren't enabled, it will return true. |
| 168 | bool IsDecoderThreadRunning(); |
| 169 | |
| 170 | rtc::ThreadChecker construction_thread_checker_; |
| 171 | rtc::ThreadChecker decoder_thread_checker_; |
| 172 | rtc::ThreadChecker module_thread_checker_; |
pbos@webrtc.org | 20c1f56 | 2014-07-04 10:58:12 +0000 | [diff] [blame] | 173 | Clock* const clock_; |
sprang | 3911c26 | 2016-04-15 01:24:14 -0700 | [diff] [blame] | 174 | rtc::CriticalSection process_crit_; |
philipel | 721d402 | 2016-12-15 07:10:57 -0800 | [diff] [blame] | 175 | VCMTiming* _timing; |
andresp@webrtc.org | f7eb75b | 2013-09-14 00:25:28 +0000 | [diff] [blame] | 176 | VCMReceiver _receiver; |
andresp@webrtc.org | f7eb75b | 2013-09-14 00:25:28 +0000 | [diff] [blame] | 177 | VCMDecodedFrameCallback _decodedFrameCallback; |
niklase@google.com | 470e71d | 2011-07-07 08:21:25 +0000 | [diff] [blame] | 178 | |
Tommi | fbf3bce | 2018-02-21 15:56:05 +0100 | [diff] [blame] | 179 | // These callbacks are set on the construction thread before being attached |
| 180 | // to the module thread or decoding started, so a lock is not required. |
| 181 | VCMFrameTypeCallback* _frameTypeCallback; |
| 182 | VCMReceiveStatisticsCallback* _receiveStatsCallback; |
| 183 | VCMPacketRequestCallback* _packetRequestCallback; |
| 184 | |
| 185 | // Used on both the module and decoder thread. |
danilchap | 56359be | 2017-09-07 07:53:45 -0700 | [diff] [blame] | 186 | bool _scheduleKeyRequest RTC_GUARDED_BY(process_crit_); |
| 187 | bool drop_frames_until_keyframe_ RTC_GUARDED_BY(process_crit_); |
sprang | 3911c26 | 2016-04-15 01:24:14 -0700 | [diff] [blame] | 188 | |
Tommi | fbf3bce | 2018-02-21 15:56:05 +0100 | [diff] [blame] | 189 | // Modified on the construction thread while not attached to the process |
| 190 | // thread. Once attached to the process thread, its value is only read |
| 191 | // so a lock is not required. |
| 192 | size_t max_nack_list_size_; |
Peter Boström | ed3277b | 2016-02-02 15:40:04 +0100 | [diff] [blame] | 193 | |
Tommi | fbf3bce | 2018-02-21 15:56:05 +0100 | [diff] [blame] | 194 | // Callbacks are set before the decoder thread starts. |
| 195 | // Once the decoder thread has been started, usage of |_codecDataBase| moves |
| 196 | // over to the decoder thread. |
| 197 | VCMDecoderDataBase _codecDataBase; |
Tommi | fbf3bce | 2018-02-21 15:56:05 +0100 | [diff] [blame] | 198 | |
| 199 | VCMProcessTimer _receiveStatsTimer RTC_GUARDED_BY(module_thread_checker_); |
| 200 | VCMProcessTimer _retransmissionTimer RTC_GUARDED_BY(module_thread_checker_); |
| 201 | VCMProcessTimer _keyRequestTimer RTC_GUARDED_BY(module_thread_checker_); |
Tommi | fbf3bce | 2018-02-21 15:56:05 +0100 | [diff] [blame] | 202 | ThreadUnsafeOneTimeEvent first_frame_received_ |
| 203 | RTC_GUARDED_BY(decoder_thread_checker_); |
| 204 | // Modified on the construction thread. Can be read without a lock and assumed |
| 205 | // to be non-null on the module and decoder threads. |
| 206 | ProcessThread* process_thread_ = nullptr; |
| 207 | bool is_attached_to_process_thread_ |
| 208 | RTC_GUARDED_BY(construction_thread_checker_) = false; |
| 209 | #if RTC_DCHECK_IS_ON |
| 210 | bool decoder_thread_is_running_ = false; |
| 211 | #endif |
niklase@google.com | 470e71d | 2011-07-07 08:21:25 +0000 | [diff] [blame] | 212 | }; |
andresp@webrtc.org | f7eb75b | 2013-09-14 00:25:28 +0000 | [diff] [blame] | 213 | |
| 214 | } // namespace vcm |
pbos@webrtc.org | d900e8b | 2013-07-03 15:12:26 +0000 | [diff] [blame] | 215 | } // namespace webrtc |
Mirko Bonadei | 92ea95e | 2017-09-15 06:47:31 +0200 | [diff] [blame] | 216 | #endif // MODULES_VIDEO_CODING_VIDEO_CODING_IMPL_H_ |