blob: a674858726ca49b844ec3b82fb8997fe53ec1e59 [file] [log] [blame]
niklase@google.com470e71d2011-07-07 08:21:25 +00001/*
mikhal@webrtc.orga2031d52012-07-31 15:53:44 +00002 * Copyright (c) 2012 The WebRTC project authors. All Rights Reserved.
niklase@google.com470e71d2011-07-07 08:21:25 +00003 *
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 Bonadei92ea95e2017-09-15 06:47:31 +020011#ifndef MODULES_VIDEO_CODING_GENERIC_DECODER_H_
12#define MODULES_VIDEO_CODING_GENERIC_DECODER_H_
niklase@google.com470e71d2011-07-07 08:21:25 +000013
Ilya Nikolaevskiy43c108b2020-05-15 12:24:29 +020014#include <string>
tommi5b7fc8c2017-07-05 16:45:57 -070015
Jonas Orelande62c2f22022-03-29 11:04:48 +020016#include "api/field_trials_view.h"
Artem Titovd15a5752021-02-10 14:31:24 +010017#include "api/sequence_checker.h"
Johannes Kron7ddea572019-09-11 12:00:22 +020018#include "api/units/time_delta.h"
Danil Chapovalov355b8d22021-08-13 16:50:37 +020019#include "api/video_codecs/video_decoder.h"
Mirko Bonadei92ea95e2017-09-15 06:47:31 +020020#include "modules/video_coding/encoded_frame.h"
21#include "modules/video_coding/include/video_codec_interface.h"
22#include "modules/video_coding/timestamp_map.h"
23#include "modules/video_coding/timing.h"
Johannes Kron7ddea572019-09-11 12:00:22 +020024#include "rtc_base/experiments/field_trial_parser.h"
Markus Handell6deec382020-07-07 12:17:12 +020025#include "rtc_base/synchronization/mutex.h"
niklase@google.com470e71d2011-07-07 08:21:25 +000026
philipel9d3ab612015-12-21 04:12:39 -080027namespace webrtc {
niklase@google.com470e71d2011-07-07 08:21:25 +000028
29class VCMReceiveCallback;
30
31enum { kDecoderFrameMemoryLength = 10 };
32
philipel9d3ab612015-12-21 04:12:39 -080033class VCMDecodedFrameCallback : public DecodedImageCallback {
34 public:
Jonas Orelande02f9ee2022-03-25 12:43:14 +010035 VCMDecodedFrameCallback(VCMTiming* timing,
36 Clock* clock,
Jonas Orelande62c2f22022-03-29 11:04:48 +020037 const FieldTrialsView& field_trials);
tommid0a71ba2017-03-14 04:16:20 -070038 ~VCMDecodedFrameCallback() override;
39 void SetUserReceiveCallback(VCMReceiveCallback* receiveCallback);
40 VCMReceiveCallback* UserReceiveCallback();
niklase@google.com470e71d2011-07-07 08:21:25 +000041
tommid0a71ba2017-03-14 04:16:20 -070042 int32_t Decoded(VideoFrame& decodedImage) override;
43 int32_t Decoded(VideoFrame& decodedImage, int64_t decode_time_ms) override;
44 void Decoded(VideoFrame& decodedImage,
Danil Chapovalov0040b662018-06-18 10:48:16 +020045 absl::optional<int32_t> decode_time_ms,
46 absl::optional<uint8_t> qp) override;
niklase@google.com470e71d2011-07-07 08:21:25 +000047
tommid0a71ba2017-03-14 04:16:20 -070048 void OnDecoderImplementationName(const char* implementation_name);
niklase@google.com470e71d2011-07-07 08:21:25 +000049
Johannes Kronb6b782d2021-03-03 14:39:44 +010050 void Map(uint32_t timestamp, const VCMFrameInformation& frameInfo);
Johannes Kronfc5d2762021-04-09 16:03:22 +020051 void ClearTimestampMap();
niklase@google.com470e71d2011-07-07 08:21:25 +000052
philipel9d3ab612015-12-21 04:12:39 -080053 private:
Artem Titovc8421c42021-02-02 10:57:19 +010054 SequenceChecker construction_thread_;
Artem Titovdcd7fc72021-08-09 13:02:57 +020055 // Protect `_timestampMap`.
tommid0a71ba2017-03-14 04:16:20 -070056 Clock* const _clock;
57 // This callback must be set before the decoder thread starts running
58 // and must only be unset when external threads (e.g decoder thread)
59 // have been stopped. Due to that, the variable should regarded as const
60 // while there are more than one threads involved, it must be set
61 // from the same thread, and therfore a lock is not required to access it.
62 VCMReceiveCallback* _receiveCallback = nullptr;
Lu Liu352314a2018-02-21 19:38:59 +000063 VCMTiming* _timing;
Markus Handell6deec382020-07-07 12:17:12 +020064 Mutex lock_;
Lu Liu352314a2018-02-21 19:38:59 +000065 VCMTimestampMap _timestampMap RTC_GUARDED_BY(lock_);
ilnik04f4d122017-06-19 07:18:55 -070066 int64_t ntp_offset_;
Johannes Kron7ddea572019-09-11 12:00:22 +020067 // Set by the field trial WebRTC-SlowDownDecoder to simulate a slow decoder.
68 FieldTrialOptional<TimeDelta> _extra_decode_time;
Johannes Kron111e9812020-10-26 13:54:40 +010069
Artem Titovdcd7fc72021-08-09 13:02:57 +020070 // Set by the field trial WebRTC-LowLatencyRenderer. The parameter `enabled`
Johannes Kron111e9812020-10-26 13:54:40 +010071 // determines if the low-latency renderer algorithm should be used for the
72 // case min playout delay=0 and max playout delay>0.
73 FieldTrialParameter<bool> low_latency_renderer_enabled_;
74 // Set by the field trial WebRTC-LowLatencyRenderer. The parameter
Artem Titovdcd7fc72021-08-09 13:02:57 +020075 // `include_predecode_buffer` determines if the predecode buffer should be
Johannes Kron111e9812020-10-26 13:54:40 +010076 // taken into account when calculating maximum number of frames in composition
77 // queue.
78 FieldTrialParameter<bool> low_latency_renderer_include_predecode_buffer_;
niklase@google.com470e71d2011-07-07 08:21:25 +000079};
80
philipel9d3ab612015-12-21 04:12:39 -080081class VCMGenericDecoder {
philipel9d3ab612015-12-21 04:12:39 -080082 public:
Danil Chapovalov7b78a312021-08-06 12:30:02 +020083 explicit VCMGenericDecoder(VideoDecoder* decoder);
philipel9d3ab612015-12-21 04:12:39 -080084 ~VCMGenericDecoder();
niklase@google.com470e71d2011-07-07 08:21:25 +000085
philipel9d3ab612015-12-21 04:12:39 -080086 /**
Danil Chapovalov355b8d22021-08-13 16:50:37 +020087 * Initialize the decoder with the information from the `settings`
Yves Gerey665174f2018-06-19 15:03:05 +020088 */
Danil Chapovalov355b8d22021-08-13 16:50:37 +020089 bool Configure(const VideoDecoder::Settings& settings);
niklase@google.com470e71d2011-07-07 08:21:25 +000090
philipel9d3ab612015-12-21 04:12:39 -080091 /**
Yves Gerey665174f2018-06-19 15:03:05 +020092 * Decode to a raw I420 frame,
93 *
94 * inputVideoBuffer reference to encoded video frame
95 */
Johannes Kron05f84872020-01-16 14:09:33 +010096 int32_t Decode(const VCMEncodedFrame& inputFrame, Timestamp now);
niklase@google.com470e71d2011-07-07 08:21:25 +000097
philipel9d3ab612015-12-21 04:12:39 -080098 /**
Yves Gerey665174f2018-06-19 15:03:05 +020099 * Set decode callback. Deregistering while decoding is illegal.
100 */
philipel9d3ab612015-12-21 04:12:39 -0800101 int32_t RegisterDecodeCompleteCallback(VCMDecodedFrameCallback* callback);
niklase@google.com470e71d2011-07-07 08:21:25 +0000102
tommi5b7fc8c2017-07-05 16:45:57 -0700103 bool IsSameDecoder(VideoDecoder* decoder) const {
Danil Chapovalov7b78a312021-08-06 12:30:02 +0200104 return decoder_ == decoder;
tommi5b7fc8c2017-07-05 16:45:57 -0700105 }
niklase@google.com470e71d2011-07-07 08:21:25 +0000106
philipel9d3ab612015-12-21 04:12:39 -0800107 private:
Danil Chapovalov7b78a312021-08-06 12:30:02 +0200108 VCMDecodedFrameCallback* _callback = nullptr;
109 VideoDecoder* const decoder_;
ilnik00d802b2017-04-11 10:34:31 -0700110 VideoContentType _last_keyframe_content_type;
Erik Språngc12f6252021-01-13 21:49:59 +0100111 VideoDecoder::DecoderInfo decoder_info_;
niklase@google.com470e71d2011-07-07 08:21:25 +0000112};
113
pbos@webrtc.orgd900e8b2013-07-03 15:12:26 +0000114} // namespace webrtc
niklase@google.com470e71d2011-07-07 08:21:25 +0000115
Mirko Bonadei92ea95e2017-09-15 06:47:31 +0200116#endif // MODULES_VIDEO_CODING_GENERIC_DECODER_H_