niklase@google.com | 470e71d | 2011-07-07 08:21:25 | [diff] [blame] | 1 | /* |
pwestin@webrtc.org | 52fd98d | 2012-02-13 09:03:53 | [diff] [blame] | 2 | * Copyright (c) 2012 The WebRTC project authors. All Rights Reserved. |
niklase@google.com | 470e71d | 2011-07-07 08:21:25 | [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 04:47:31 | [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 | [diff] [blame] | 13 | |
Tommi | 20b3271 | 2022-09-29 12:13:15 | [diff] [blame] | 14 | #include <map> |
kwiberg | 3f55dea | 2016-02-29 13:51:59 | [diff] [blame] | 15 | #include <memory> |
perkj | 376b192 | 2016-05-02 18:35:24 | [diff] [blame] | 16 | #include <string> |
stefan@webrtc.org | c530043 | 2012-10-08 07:06:53 | [diff] [blame] | 17 | #include <vector> |
| 18 | |
Erik Språng | eee3920 | 2018-11-15 16:52:43 | [diff] [blame] | 19 | #include "absl/types/optional.h" |
Jonas Oreland | e62c2f2 | 2022-03-29 09:04:48 | [diff] [blame] | 20 | #include "api/field_trials_view.h" |
Artem Titov | d15a575 | 2021-02-10 13:31:24 | [diff] [blame] | 21 | #include "api/sequence_checker.h" |
Rasmus Brandt | 5a54800 | 2023-03-07 10:20:46 | [diff] [blame] | 22 | #include "modules/video_coding/deprecated/frame_buffer.h" |
Rasmus Brandt | 59d09ae | 2023-04-17 12:02:49 | [diff] [blame] | 23 | #include "modules/video_coding/deprecated/jitter_buffer.h" |
Rasmus Brandt | 9dfb531 | 2023-05-03 09:08:11 | [diff] [blame] | 24 | #include "modules/video_coding/deprecated/receiver.h" |
Mirko Bonadei | 92ea95e | 2017-09-15 04:47:31 | [diff] [blame] | 25 | #include "modules/video_coding/generic_decoder.h" |
Jonas Olsson | a4d8737 | 2019-07-05 17:08:33 | [diff] [blame] | 26 | #include "modules/video_coding/include/video_coding.h" |
Rasmus Brandt | c4d253c | 2022-05-25 10:03:35 | [diff] [blame] | 27 | #include "modules/video_coding/timing/timing.h" |
Steve Anton | 10542f2 | 2019-01-11 17:11:00 | [diff] [blame] | 28 | #include "rtc_base/one_time_event.h" |
Markus Handell | 6deec38 | 2020-07-07 10:17:12 | [diff] [blame] | 29 | #include "rtc_base/synchronization/mutex.h" |
Mirko Bonadei | 92ea95e | 2017-09-15 04:47:31 | [diff] [blame] | 30 | #include "rtc_base/thread_annotations.h" |
Mirko Bonadei | 92ea95e | 2017-09-15 04:47:31 | [diff] [blame] | 31 | #include "system_wrappers/include/clock.h" |
niklase@google.com | 470e71d | 2011-07-07 08:21:25 | [diff] [blame] | 32 | |
andresp@webrtc.org | f7eb75b | 2013-09-14 00:25:28 | [diff] [blame] | 33 | namespace webrtc { |
sprang@webrtc.org | 4070935 | 2013-11-26 11:41:59 | [diff] [blame] | 34 | |
Erik Språng | 08127a9 | 2016-11-16 15:41:30 | [diff] [blame] | 35 | class VideoBitrateAllocator; |
sprang | 1a646ee | 2016-12-01 14:34:11 | [diff] [blame] | 36 | class VideoBitrateAllocationObserver; |
Erik Språng | 08127a9 | 2016-11-16 15:41:30 | [diff] [blame] | 37 | |
andresp@webrtc.org | f7eb75b | 2013-09-14 00:25:28 | [diff] [blame] | 38 | namespace vcm { |
niklase@google.com | 470e71d | 2011-07-07 08:21:25 | [diff] [blame] | 39 | |
andresp@webrtc.org | f7eb75b | 2013-09-14 00:25:28 | [diff] [blame] | 40 | class VCMProcessTimer { |
| 41 | public: |
sprang | 40217c3 | 2016-11-21 13:41:52 | [diff] [blame] | 42 | static const int64_t kDefaultProcessIntervalMs = 1000; |
| 43 | |
pkasting@chromium.org | 0b1534c | 2014-12-15 22:09:40 | [diff] [blame] | 44 | VCMProcessTimer(int64_t periodMs, Clock* clock) |
andresp@webrtc.org | f7eb75b | 2013-09-14 00:25:28 | [diff] [blame] | 45 | : _clock(clock), |
| 46 | _periodMs(periodMs), |
| 47 | _latestMs(_clock->TimeInMilliseconds()) {} |
pkasting@chromium.org | 0b1534c | 2014-12-15 22:09:40 | [diff] [blame] | 48 | int64_t Period() const; |
| 49 | int64_t TimeUntilProcess() const; |
andresp@webrtc.org | f7eb75b | 2013-09-14 00:25:28 | [diff] [blame] | 50 | void Processed(); |
niklase@google.com | 470e71d | 2011-07-07 08:21:25 | [diff] [blame] | 51 | |
andresp@webrtc.org | f7eb75b | 2013-09-14 00:25:28 | [diff] [blame] | 52 | private: |
| 53 | Clock* _clock; |
pkasting@chromium.org | 0b1534c | 2014-12-15 22:09:40 | [diff] [blame] | 54 | int64_t _periodMs; |
andresp@webrtc.org | f7eb75b | 2013-09-14 00:25:28 | [diff] [blame] | 55 | int64_t _latestMs; |
niklase@google.com | 470e71d | 2011-07-07 08:21:25 | [diff] [blame] | 56 | }; |
| 57 | |
Tommi | 20b3271 | 2022-09-29 12:13:15 | [diff] [blame] | 58 | class DEPRECATED_VCMDecoderDataBase { |
| 59 | public: |
| 60 | DEPRECATED_VCMDecoderDataBase(); |
| 61 | DEPRECATED_VCMDecoderDataBase(const DEPRECATED_VCMDecoderDataBase&) = delete; |
| 62 | DEPRECATED_VCMDecoderDataBase& operator=( |
| 63 | const DEPRECATED_VCMDecoderDataBase&) = delete; |
| 64 | ~DEPRECATED_VCMDecoderDataBase() = default; |
| 65 | |
| 66 | // Returns a pointer to the previously registered decoder or nullptr if none |
| 67 | // was registered for the `payload_type`. |
| 68 | VideoDecoder* DeregisterExternalDecoder(uint8_t payload_type); |
| 69 | void RegisterExternalDecoder(uint8_t payload_type, |
| 70 | VideoDecoder* external_decoder); |
| 71 | bool IsExternalDecoderRegistered(uint8_t payload_type) const; |
| 72 | |
| 73 | void RegisterReceiveCodec(uint8_t payload_type, |
| 74 | const VideoDecoder::Settings& settings); |
| 75 | bool DeregisterReceiveCodec(uint8_t payload_type); |
| 76 | |
| 77 | // Returns a decoder specified by frame.PayloadType. The decoded frame |
| 78 | // callback of the decoder is set to `decoded_frame_callback`. If no such |
| 79 | // decoder already exists an instance will be created and initialized. |
| 80 | // nullptr is returned if no decoder with the specified payload type was found |
| 81 | // and the function failed to create one. |
| 82 | VCMGenericDecoder* GetDecoder( |
| 83 | const VCMEncodedFrame& frame, |
| 84 | VCMDecodedFrameCallback* decoded_frame_callback); |
| 85 | |
| 86 | private: |
| 87 | void CreateAndInitDecoder(const VCMEncodedFrame& frame) |
| 88 | RTC_RUN_ON(decoder_sequence_checker_); |
| 89 | |
| 90 | SequenceChecker decoder_sequence_checker_; |
| 91 | |
| 92 | absl::optional<uint8_t> current_payload_type_; |
| 93 | absl::optional<VCMGenericDecoder> current_decoder_ |
| 94 | RTC_GUARDED_BY(decoder_sequence_checker_); |
| 95 | // Initialization paramaters for decoders keyed by payload type. |
| 96 | std::map<uint8_t, VideoDecoder::Settings> decoder_settings_; |
| 97 | // Decoders keyed by payload type. |
| 98 | std::map<uint8_t, VideoDecoder*> decoders_ |
| 99 | RTC_GUARDED_BY(decoder_sequence_checker_); |
| 100 | }; |
| 101 | |
Danil Chapovalov | ce80886 | 2022-06-22 13:48:36 | [diff] [blame] | 102 | class VideoReceiver { |
andresp@webrtc.org | f7eb75b | 2013-09-14 00:25:28 | [diff] [blame] | 103 | public: |
Jonas Oreland | e02f9ee | 2022-03-25 11:43:14 | [diff] [blame] | 104 | VideoReceiver(Clock* clock, |
| 105 | VCMTiming* timing, |
Jonas Oreland | e62c2f2 | 2022-03-29 09:04:48 | [diff] [blame] | 106 | const FieldTrialsView& field_trials); |
Danil Chapovalov | ce80886 | 2022-06-22 13:48:36 | [diff] [blame] | 107 | ~VideoReceiver(); |
andresp@webrtc.org | f7eb75b | 2013-09-14 00:25:28 | [diff] [blame] | 108 | |
Danil Chapovalov | ba0a306 | 2021-08-13 16:15:55 | [diff] [blame] | 109 | void RegisterReceiveCodec(uint8_t payload_type, |
Danil Chapovalov | 355b8d2 | 2021-08-13 14:50:37 | [diff] [blame] | 110 | const VideoDecoder::Settings& settings); |
andresp@webrtc.org | f7eb75b | 2013-09-14 00:25:28 | [diff] [blame] | 111 | |
Peter Boström | 795dbe4 | 2015-11-27 13:09:07 | [diff] [blame] | 112 | void RegisterExternalDecoder(VideoDecoder* externalDecoder, |
perkj | 796cfaf | 2015-12-10 17:27:38 | [diff] [blame] | 113 | uint8_t payloadType); |
andresp@webrtc.org | f7eb75b | 2013-09-14 00:25:28 | [diff] [blame] | 114 | int32_t RegisterReceiveCallback(VCMReceiveCallback* receiveCallback); |
andresp@webrtc.org | f7eb75b | 2013-09-14 00:25:28 | [diff] [blame] | 115 | int32_t RegisterFrameTypeCallback(VCMFrameTypeCallback* frameTypeCallback); |
| 116 | int32_t RegisterPacketRequestCallback(VCMPacketRequestCallback* callback); |
andresp@webrtc.org | f7eb75b | 2013-09-14 00:25:28 | [diff] [blame] | 117 | |
| 118 | int32_t Decode(uint16_t maxWaitTimeMs); |
andresp@webrtc.org | f7eb75b | 2013-09-14 00:25:28 | [diff] [blame] | 119 | |
andresp@webrtc.org | f7eb75b | 2013-09-14 00:25:28 | [diff] [blame] | 120 | int32_t IncomingPacket(const uint8_t* incomingPayload, |
pkasting@chromium.org | 4591fbd | 2014-11-20 22:28:14 | [diff] [blame] | 121 | size_t payloadLength, |
Niels Möller | be7a0ec | 2019-04-25 08:02:52 | [diff] [blame] | 122 | const RTPHeader& rtp_header, |
| 123 | const RTPVideoHeader& video_header); |
andresp@webrtc.org | f7eb75b | 2013-09-14 00:25:28 | [diff] [blame] | 124 | |
andresp@webrtc.org | f7eb75b | 2013-09-14 00:25:28 | [diff] [blame] | 125 | void SetNackSettings(size_t max_nack_list_size, |
| 126 | int max_packet_age_to_nack, |
| 127 | int max_incomplete_time_ms); |
| 128 | |
Danil Chapovalov | ce80886 | 2022-06-22 13:48:36 | [diff] [blame] | 129 | void Process(); |
andresp@webrtc.org | f7eb75b | 2013-09-14 00:25:28 | [diff] [blame] | 130 | |
| 131 | protected: |
Tommi | fbf3bce | 2018-02-21 14:56:05 | [diff] [blame] | 132 | int32_t Decode(const webrtc::VCMEncodedFrame& frame); |
andresp@webrtc.org | f7eb75b | 2013-09-14 00:25:28 | [diff] [blame] | 133 | int32_t RequestKeyFrame(); |
andresp@webrtc.org | f7eb75b | 2013-09-14 00:25:28 | [diff] [blame] | 134 | |
| 135 | private: |
Tommi | fbf3bce | 2018-02-21 14:56:05 | [diff] [blame] | 136 | // Used for DCHECKing thread correctness. |
| 137 | // In build where DCHECKs are enabled, will return false before |
| 138 | // DecoderThreadStarting is called, then true until DecoderThreadStopped |
| 139 | // is called. |
| 140 | // In builds where DCHECKs aren't enabled, it will return true. |
| 141 | bool IsDecoderThreadRunning(); |
| 142 | |
Artem Titov | c8421c4 | 2021-02-02 09:57:19 | [diff] [blame] | 143 | SequenceChecker construction_thread_checker_; |
| 144 | SequenceChecker decoder_thread_checker_; |
| 145 | SequenceChecker module_thread_checker_; |
pbos@webrtc.org | 20c1f56 | 2014-07-04 10:58:12 | [diff] [blame] | 146 | Clock* const clock_; |
Markus Handell | 6deec38 | 2020-07-07 10:17:12 | [diff] [blame] | 147 | Mutex process_mutex_; |
philipel | 721d402 | 2016-12-15 15:10:57 | [diff] [blame] | 148 | VCMTiming* _timing; |
andresp@webrtc.org | f7eb75b | 2013-09-14 00:25:28 | [diff] [blame] | 149 | VCMReceiver _receiver; |
andresp@webrtc.org | f7eb75b | 2013-09-14 00:25:28 | [diff] [blame] | 150 | VCMDecodedFrameCallback _decodedFrameCallback; |
niklase@google.com | 470e71d | 2011-07-07 08:21:25 | [diff] [blame] | 151 | |
Tommi | fbf3bce | 2018-02-21 14:56:05 | [diff] [blame] | 152 | // These callbacks are set on the construction thread before being attached |
| 153 | // to the module thread or decoding started, so a lock is not required. |
| 154 | VCMFrameTypeCallback* _frameTypeCallback; |
Tommi | fbf3bce | 2018-02-21 14:56:05 | [diff] [blame] | 155 | VCMPacketRequestCallback* _packetRequestCallback; |
| 156 | |
| 157 | // Used on both the module and decoder thread. |
Markus Handell | 6deec38 | 2020-07-07 10:17:12 | [diff] [blame] | 158 | bool _scheduleKeyRequest RTC_GUARDED_BY(process_mutex_); |
| 159 | bool drop_frames_until_keyframe_ RTC_GUARDED_BY(process_mutex_); |
sprang | 3911c26 | 2016-04-15 08:24:14 | [diff] [blame] | 160 | |
Tommi | fbf3bce | 2018-02-21 14:56:05 | [diff] [blame] | 161 | // Modified on the construction thread while not attached to the process |
| 162 | // thread. Once attached to the process thread, its value is only read |
| 163 | // so a lock is not required. |
| 164 | size_t max_nack_list_size_; |
Peter Boström | ed3277b | 2016-02-02 14:40:04 | [diff] [blame] | 165 | |
Tommi | fbf3bce | 2018-02-21 14:56:05 | [diff] [blame] | 166 | // Callbacks are set before the decoder thread starts. |
Artem Titov | dcd7fc7 | 2021-08-09 11:02:57 | [diff] [blame] | 167 | // Once the decoder thread has been started, usage of `_codecDataBase` moves |
Tommi | fbf3bce | 2018-02-21 14:56:05 | [diff] [blame] | 168 | // over to the decoder thread. |
Tommi | 20b3271 | 2022-09-29 12:13:15 | [diff] [blame] | 169 | DEPRECATED_VCMDecoderDataBase _codecDataBase; |
Tommi | fbf3bce | 2018-02-21 14:56:05 | [diff] [blame] | 170 | |
Tommi | fbf3bce | 2018-02-21 14:56:05 | [diff] [blame] | 171 | VCMProcessTimer _retransmissionTimer RTC_GUARDED_BY(module_thread_checker_); |
| 172 | VCMProcessTimer _keyRequestTimer RTC_GUARDED_BY(module_thread_checker_); |
Tommi | fbf3bce | 2018-02-21 14:56:05 | [diff] [blame] | 173 | ThreadUnsafeOneTimeEvent first_frame_received_ |
| 174 | RTC_GUARDED_BY(decoder_thread_checker_); |
niklase@google.com | 470e71d | 2011-07-07 08:21:25 | [diff] [blame] | 175 | }; |
andresp@webrtc.org | f7eb75b | 2013-09-14 00:25:28 | [diff] [blame] | 176 | |
| 177 | } // namespace vcm |
pbos@webrtc.org | d900e8b | 2013-07-03 15:12:26 | [diff] [blame] | 178 | } // namespace webrtc |
Mirko Bonadei | 92ea95e | 2017-09-15 04:47:31 | [diff] [blame] | 179 | #endif // MODULES_VIDEO_CODING_VIDEO_CODING_IMPL_H_ |