video_coding_impl.h (6589B)
1 /* 2 * Copyright (c) 2012 The WebRTC project authors. All Rights Reserved. 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 11 #ifndef MODULES_VIDEO_CODING_VIDEO_CODING_IMPL_H_ 12 #define MODULES_VIDEO_CODING_VIDEO_CODING_IMPL_H_ 13 14 #include <cstddef> 15 #include <cstdint> 16 #include <map> 17 #include <optional> 18 19 #include "api/field_trials_view.h" 20 #include "api/rtp_headers.h" 21 #include "api/sequence_checker.h" 22 #include "api/video_codecs/video_decoder.h" 23 #include "modules/rtp_rtcp/source/rtp_video_header.h" 24 #include "modules/video_coding/deprecated/jitter_buffer.h" 25 #include "modules/video_coding/deprecated/receiver.h" 26 #include "modules/video_coding/generic_decoder.h" 27 #include "modules/video_coding/include/video_coding_defines.h" 28 #include "modules/video_coding/timing/timing.h" 29 #include "rtc_base/one_time_event.h" 30 #include "rtc_base/synchronization/mutex.h" 31 #include "rtc_base/thread_annotations.h" 32 #include "system_wrappers/include/clock.h" 33 34 namespace webrtc { 35 36 class VideoBitrateAllocator; 37 class VideoBitrateAllocationObserver; 38 39 namespace vcm { 40 41 class VCMProcessTimer { 42 public: 43 static const int64_t kDefaultProcessIntervalMs = 1000; 44 45 VCMProcessTimer(int64_t periodMs, Clock* clock) 46 : _clock(clock), 47 _periodMs(periodMs), 48 _latestMs(_clock->TimeInMilliseconds()) {} 49 int64_t Period() const; 50 int64_t TimeUntilProcess() const; 51 void Processed(); 52 53 private: 54 Clock* _clock; 55 int64_t _periodMs; 56 int64_t _latestMs; 57 }; 58 59 class DEPRECATED_VCMDecoderDataBase { 60 public: 61 DEPRECATED_VCMDecoderDataBase(); 62 DEPRECATED_VCMDecoderDataBase(const DEPRECATED_VCMDecoderDataBase&) = delete; 63 DEPRECATED_VCMDecoderDataBase& operator=( 64 const DEPRECATED_VCMDecoderDataBase&) = delete; 65 ~DEPRECATED_VCMDecoderDataBase() = default; 66 67 // Returns a pointer to the previously registered decoder or nullptr if none 68 // was registered for the `payload_type`. 69 VideoDecoder* DeregisterExternalDecoder(uint8_t payload_type); 70 void RegisterExternalDecoder(uint8_t payload_type, 71 VideoDecoder* external_decoder); 72 bool IsExternalDecoderRegistered(uint8_t payload_type) const; 73 74 void RegisterReceiveCodec(uint8_t payload_type, 75 const VideoDecoder::Settings& settings); 76 bool DeregisterReceiveCodec(uint8_t payload_type); 77 78 // Returns a decoder specified by frame.PayloadType. The decoded frame 79 // callback of the decoder is set to `decoded_frame_callback`. If no such 80 // decoder already exists an instance will be created and initialized. 81 // nullptr is returned if no decoder with the specified payload type was found 82 // and the function failed to create one. 83 VCMGenericDecoder* GetDecoder( 84 const VCMEncodedFrame& frame, 85 VCMDecodedFrameCallback* decoded_frame_callback); 86 87 private: 88 void CreateAndInitDecoder(const VCMEncodedFrame& frame) 89 RTC_RUN_ON(decoder_sequence_checker_); 90 91 SequenceChecker decoder_sequence_checker_; 92 93 std::optional<uint8_t> current_payload_type_; 94 std::optional<VCMGenericDecoder> current_decoder_ 95 RTC_GUARDED_BY(decoder_sequence_checker_); 96 // Initialization paramaters for decoders keyed by payload type. 97 std::map<uint8_t, VideoDecoder::Settings> decoder_settings_; 98 // Decoders keyed by payload type. 99 std::map<uint8_t, VideoDecoder*> decoders_ 100 RTC_GUARDED_BY(decoder_sequence_checker_); 101 }; 102 103 class VideoReceiver { 104 public: 105 VideoReceiver(Clock* clock, 106 VCMTiming* timing, 107 const FieldTrialsView& field_trials); 108 ~VideoReceiver(); 109 110 void RegisterReceiveCodec(uint8_t payload_type, 111 const VideoDecoder::Settings& settings); 112 113 void RegisterExternalDecoder(VideoDecoder* externalDecoder, 114 uint8_t payloadType); 115 int32_t RegisterReceiveCallback(VCMReceiveCallback* receiveCallback); 116 int32_t RegisterFrameTypeCallback(VCMFrameTypeCallback* frameTypeCallback); 117 int32_t RegisterPacketRequestCallback(VCMPacketRequestCallback* callback); 118 119 int32_t Decode(uint16_t maxWaitTimeMs); 120 121 int32_t IncomingPacket(const uint8_t* incomingPayload, 122 size_t payloadLength, 123 const RTPHeader& rtp_header, 124 const RTPVideoHeader& video_header); 125 126 void SetNackSettings(size_t max_nack_list_size, 127 int max_packet_age_to_nack, 128 int max_incomplete_time_ms); 129 130 void Process(); 131 132 protected: 133 int32_t Decode(const webrtc::VCMEncodedFrame& frame); 134 int32_t RequestKeyFrame(); 135 136 private: 137 // Used for DCHECKing thread correctness. 138 // In build where DCHECKs are enabled, will return false before 139 // DecoderThreadStarting is called, then true until DecoderThreadStopped 140 // is called. 141 // In builds where DCHECKs aren't enabled, it will return true. 142 bool IsDecoderThreadRunning(); 143 144 SequenceChecker construction_thread_checker_; 145 SequenceChecker decoder_thread_checker_; 146 SequenceChecker module_thread_checker_; 147 Clock* const clock_; 148 Mutex process_mutex_; 149 VCMTiming* _timing; 150 VCMReceiver _receiver; 151 VCMDecodedFrameCallback _decodedFrameCallback; 152 153 // These callbacks are set on the construction thread before being attached 154 // to the module thread or decoding started, so a lock is not required. 155 VCMFrameTypeCallback* _frameTypeCallback; 156 VCMPacketRequestCallback* _packetRequestCallback; 157 158 // Used on both the module and decoder thread. 159 bool _scheduleKeyRequest RTC_GUARDED_BY(process_mutex_); 160 bool drop_frames_until_keyframe_ RTC_GUARDED_BY(process_mutex_); 161 162 // Modified on the construction thread while not attached to the process 163 // thread. Once attached to the process thread, its value is only read 164 // so a lock is not required. 165 size_t max_nack_list_size_; 166 167 // Callbacks are set before the decoder thread starts. 168 // Once the decoder thread has been started, usage of `_codecDataBase` moves 169 // over to the decoder thread. 170 DEPRECATED_VCMDecoderDataBase _codecDataBase; 171 172 VCMProcessTimer _retransmissionTimer RTC_GUARDED_BY(module_thread_checker_); 173 VCMProcessTimer _keyRequestTimer RTC_GUARDED_BY(module_thread_checker_); 174 ThreadUnsafeOneTimeEvent first_frame_received_ 175 RTC_GUARDED_BY(decoder_thread_checker_); 176 }; 177 178 } // namespace vcm 179 } // namespace webrtc 180 #endif // MODULES_VIDEO_CODING_VIDEO_CODING_IMPL_H_