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 <memory>
15 #include <string>
16 
17 #include "api/units/time_delta.h"
18 #include "modules/video_coding/encoded_frame.h"
19 #include "modules/video_coding/include/video_codec_interface.h"
20 #include "modules/video_coding/timestamp_map.h"
21 #include "modules/video_coding/timing.h"
22 #include "rtc_base/experiments/field_trial_parser.h"
23 #include "rtc_base/synchronization/mutex.h"
24 #include "rtc_base/thread_checker.h"
25 
26 namespace webrtc {
27 
28 class VCMReceiveCallback;
29 
30 enum { kDecoderFrameMemoryLength = 10 };
31 
32 struct VCMFrameInformation {
33   int64_t renderTimeMs;
34   absl::optional<Timestamp> decodeStart;
35   void* userData;
36   VideoRotation rotation;
37   VideoContentType content_type;
38   EncodedImage::Timing timing;
39   int64_t ntp_time_ms;
40   RtpPacketInfos packet_infos;
41   // ColorSpace is not stored here, as it might be modified by decoders.
42 };
43 
44 class VCMDecodedFrameCallback : public DecodedImageCallback {
45  public:
46   VCMDecodedFrameCallback(VCMTiming* timing, Clock* clock);
47   ~VCMDecodedFrameCallback() override;
48   void SetUserReceiveCallback(VCMReceiveCallback* receiveCallback);
49   VCMReceiveCallback* UserReceiveCallback();
50 
51   int32_t Decoded(VideoFrame& decodedImage) override;
52   int32_t Decoded(VideoFrame& decodedImage, int64_t decode_time_ms) override;
53   void Decoded(VideoFrame& decodedImage,
54                absl::optional<int32_t> decode_time_ms,
55                absl::optional<uint8_t> qp) override;
56 
57   void OnDecoderImplementationName(const char* implementation_name);
58 
59   void Map(uint32_t timestamp, VCMFrameInformation* frameInfo);
60   int32_t Pop(uint32_t timestamp);
61 
62  private:
63   rtc::ThreadChecker construction_thread_;
64   // Protect |_timestampMap|.
65   Clock* const _clock;
66   // This callback must be set before the decoder thread starts running
67   // and must only be unset when external threads (e.g decoder thread)
68   // have been stopped. Due to that, the variable should regarded as const
69   // while there are more than one threads involved, it must be set
70   // from the same thread, and therfore a lock is not required to access it.
71   VCMReceiveCallback* _receiveCallback = nullptr;
72   VCMTiming* _timing;
73   Mutex lock_;
74   VCMTimestampMap _timestampMap RTC_GUARDED_BY(lock_);
75   int64_t ntp_offset_;
76   // Set by the field trial WebRTC-SlowDownDecoder to simulate a slow decoder.
77   FieldTrialOptional<TimeDelta> _extra_decode_time;
78 };
79 
80 class VCMGenericDecoder {
81  public:
82   explicit VCMGenericDecoder(std::unique_ptr<VideoDecoder> decoder);
83   explicit VCMGenericDecoder(VideoDecoder* decoder, bool isExternal = false);
84   ~VCMGenericDecoder();
85 
86   /**
87    * Initialize the decoder with the information from the VideoCodec
88    */
89   int32_t InitDecode(const VideoCodec* settings, int32_t numberOfCores);
90 
91   /**
92    * Decode to a raw I420 frame,
93    *
94    * inputVideoBuffer reference to encoded video frame
95    */
96   int32_t Decode(const VCMEncodedFrame& inputFrame, Timestamp now);
97 
98   /**
99    * Set decode callback. Deregistering while decoding is illegal.
100    */
101   int32_t RegisterDecodeCompleteCallback(VCMDecodedFrameCallback* callback);
102 
103   bool PrefersLateDecoding() const;
IsSameDecoder(VideoDecoder * decoder)104   bool IsSameDecoder(VideoDecoder* decoder) const {
105     return decoder_.get() == decoder;
106   }
107 
108  private:
109   VCMDecodedFrameCallback* _callback;
110   VCMFrameInformation _frameInfos[kDecoderFrameMemoryLength];
111   uint32_t _nextFrameInfoIdx;
112   std::unique_ptr<VideoDecoder> decoder_;
113   VideoCodecType _codecType;
114   const bool _isExternal;
115   VideoContentType _last_keyframe_content_type;
116   std::string implementation_name_;
117 };
118 
119 }  // namespace webrtc
120 
121 #endif  // MODULES_VIDEO_CODING_GENERIC_DECODER_H_
122