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_GENERIC_DECODER_H_ 12 #define MODULES_VIDEO_CODING_GENERIC_DECODER_H_ 13 14 #include <cstdint> 15 #include <deque> 16 #include <string> 17 #include <utility> 18 19 #include "api/field_trials_view.h" 20 #include "api/sequence_checker.h" 21 #include "api/video_codecs/video_decoder.h" 22 #include "modules/video_coding/encoded_frame.h" 23 #include "modules/video_coding/timing/timing.h" 24 #include "rtc_base/synchronization/mutex.h" 25 26 namespace webrtc { 27 28 class VCMReceiveCallback; 29 30 struct FrameInfo { 31 FrameInfo() = default; 32 FrameInfo(const FrameInfo&) = delete; 33 FrameInfo& operator=(const FrameInfo&) = delete; 34 FrameInfo(FrameInfo&&) = default; 35 FrameInfo& operator=(FrameInfo&&) = default; 36 37 uint32_t rtp_timestamp; 38 // This is likely not optional, but some inputs seem to sometimes be negative. 39 // TODO(bugs.webrtc.org/13756): See if this can be replaced with Timestamp 40 // once all inputs to this field use Timestamp instead of an integer. 41 absl::optional<Timestamp> render_time; 42 absl::optional<Timestamp> decode_start; 43 VideoRotation rotation; 44 VideoContentType content_type; 45 EncodedImage::Timing timing; 46 int64_t ntp_time_ms; 47 RtpPacketInfos packet_infos; 48 // ColorSpace is not stored here, as it might be modified by decoders. 49 }; 50 51 class VCMDecodedFrameCallback : public DecodedImageCallback { 52 public: 53 VCMDecodedFrameCallback(VCMTiming* timing, 54 Clock* clock, 55 const FieldTrialsView& field_trials); 56 ~VCMDecodedFrameCallback() override; 57 void SetUserReceiveCallback(VCMReceiveCallback* receiveCallback); 58 VCMReceiveCallback* UserReceiveCallback(); 59 60 int32_t Decoded(VideoFrame& decodedImage) override; 61 int32_t Decoded(VideoFrame& decodedImage, int64_t decode_time_ms) override; 62 void Decoded(VideoFrame& decodedImage, 63 absl::optional<int32_t> decode_time_ms, 64 absl::optional<uint8_t> qp) override; 65 66 void OnDecoderInfoChanged(const VideoDecoder::DecoderInfo& decoder_info); 67 68 void Map(FrameInfo frameInfo); 69 void ClearTimestampMap(); 70 71 private: 72 std::pair<absl::optional<FrameInfo>, size_t> FindFrameInfo( 73 uint32_t rtp_timestamp) RTC_EXCLUSIVE_LOCKS_REQUIRED(lock_); 74 75 SequenceChecker construction_thread_; 76 Clock* const _clock; 77 // This callback must be set before the decoder thread starts running 78 // and must only be unset when external threads (e.g decoder thread) 79 // have been stopped. Due to that, the variable should regarded as const 80 // while there are more than one threads involved, it must be set 81 // from the same thread, and therfore a lock is not required to access it. 82 VCMReceiveCallback* _receiveCallback = nullptr; 83 VCMTiming* _timing; 84 Mutex lock_; 85 std::deque<FrameInfo> frame_infos_ RTC_GUARDED_BY(lock_); 86 int64_t ntp_offset_; 87 }; 88 89 class VCMGenericDecoder { 90 public: 91 explicit VCMGenericDecoder(VideoDecoder* decoder); 92 ~VCMGenericDecoder(); 93 94 /** 95 * Initialize the decoder with the information from the `settings` 96 */ 97 bool Configure(const VideoDecoder::Settings& settings); 98 99 /** 100 * Decode to a raw I420 frame, 101 * 102 * inputVideoBuffer reference to encoded video frame 103 */ 104 int32_t Decode(const VCMEncodedFrame& inputFrame, Timestamp now); 105 106 /** 107 * Set decode callback. Deregistering while decoding is illegal. 108 */ 109 int32_t RegisterDecodeCompleteCallback(VCMDecodedFrameCallback* callback); 110 IsSameDecoder(VideoDecoder * decoder)111 bool IsSameDecoder(VideoDecoder* decoder) const { 112 return decoder_ == decoder; 113 } 114 115 private: 116 VCMDecodedFrameCallback* _callback = nullptr; 117 VideoDecoder* const decoder_; 118 VideoContentType _last_keyframe_content_type; 119 VideoDecoder::DecoderInfo decoder_info_; 120 }; 121 122 } // namespace webrtc 123 124 #endif // MODULES_VIDEO_CODING_GENERIC_DECODER_H_ 125