blob: b715d589ade8e576779f924826a52b26b5984560 [file] [log] [blame]
niklase@google.com470e71d2011-07-07 08:21:251/*
pwestin@webrtc.org52fd98d2012-02-13 09:03:532 * Copyright (c) 2012 The WebRTC project authors. All Rights Reserved.
niklase@google.com470e71d2011-07-07 08:21:253 *
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 04:47:3111#ifndef MODULES_VIDEO_CODING_VIDEO_CODING_IMPL_H_
12#define MODULES_VIDEO_CODING_VIDEO_CODING_IMPL_H_
niklase@google.com470e71d2011-07-07 08:21:2513
Tommi20b32712022-09-29 12:13:1514#include <map>
kwiberg3f55dea2016-02-29 13:51:5915#include <memory>
perkj376b1922016-05-02 18:35:2416#include <string>
stefan@webrtc.orgc5300432012-10-08 07:06:5317#include <vector>
18
Erik Språngeee39202018-11-15 16:52:4319#include "absl/types/optional.h"
Jonas Orelande62c2f22022-03-29 09:04:4820#include "api/field_trials_view.h"
Artem Titovd15a5752021-02-10 13:31:2421#include "api/sequence_checker.h"
Rasmus Brandt5a548002023-03-07 10:20:4622#include "modules/video_coding/deprecated/frame_buffer.h"
Rasmus Brandt59d09ae2023-04-17 12:02:4923#include "modules/video_coding/deprecated/jitter_buffer.h"
Rasmus Brandt9dfb5312023-05-03 09:08:1124#include "modules/video_coding/deprecated/receiver.h"
Mirko Bonadei92ea95e2017-09-15 04:47:3125#include "modules/video_coding/generic_decoder.h"
Jonas Olssona4d87372019-07-05 17:08:3326#include "modules/video_coding/include/video_coding.h"
Rasmus Brandtc4d253c2022-05-25 10:03:3527#include "modules/video_coding/timing/timing.h"
Steve Anton10542f22019-01-11 17:11:0028#include "rtc_base/one_time_event.h"
Markus Handell6deec382020-07-07 10:17:1229#include "rtc_base/synchronization/mutex.h"
Mirko Bonadei92ea95e2017-09-15 04:47:3130#include "rtc_base/thread_annotations.h"
Mirko Bonadei92ea95e2017-09-15 04:47:3131#include "system_wrappers/include/clock.h"
niklase@google.com470e71d2011-07-07 08:21:2532
andresp@webrtc.orgf7eb75b2013-09-14 00:25:2833namespace webrtc {
sprang@webrtc.org40709352013-11-26 11:41:5934
Erik Språng08127a92016-11-16 15:41:3035class VideoBitrateAllocator;
sprang1a646ee2016-12-01 14:34:1136class VideoBitrateAllocationObserver;
Erik Språng08127a92016-11-16 15:41:3037
andresp@webrtc.orgf7eb75b2013-09-14 00:25:2838namespace vcm {
niklase@google.com470e71d2011-07-07 08:21:2539
andresp@webrtc.orgf7eb75b2013-09-14 00:25:2840class VCMProcessTimer {
41 public:
sprang40217c32016-11-21 13:41:5242 static const int64_t kDefaultProcessIntervalMs = 1000;
43
pkasting@chromium.org0b1534c2014-12-15 22:09:4044 VCMProcessTimer(int64_t periodMs, Clock* clock)
andresp@webrtc.orgf7eb75b2013-09-14 00:25:2845 : _clock(clock),
46 _periodMs(periodMs),
47 _latestMs(_clock->TimeInMilliseconds()) {}
pkasting@chromium.org0b1534c2014-12-15 22:09:4048 int64_t Period() const;
49 int64_t TimeUntilProcess() const;
andresp@webrtc.orgf7eb75b2013-09-14 00:25:2850 void Processed();
niklase@google.com470e71d2011-07-07 08:21:2551
andresp@webrtc.orgf7eb75b2013-09-14 00:25:2852 private:
53 Clock* _clock;
pkasting@chromium.org0b1534c2014-12-15 22:09:4054 int64_t _periodMs;
andresp@webrtc.orgf7eb75b2013-09-14 00:25:2855 int64_t _latestMs;
niklase@google.com470e71d2011-07-07 08:21:2556};
57
Tommi20b32712022-09-29 12:13:1558class 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 Chapovalovce808862022-06-22 13:48:36102class VideoReceiver {
andresp@webrtc.orgf7eb75b2013-09-14 00:25:28103 public:
Jonas Orelande02f9ee2022-03-25 11:43:14104 VideoReceiver(Clock* clock,
105 VCMTiming* timing,
Jonas Orelande62c2f22022-03-29 09:04:48106 const FieldTrialsView& field_trials);
Danil Chapovalovce808862022-06-22 13:48:36107 ~VideoReceiver();
andresp@webrtc.orgf7eb75b2013-09-14 00:25:28108
Danil Chapovalovba0a3062021-08-13 16:15:55109 void RegisterReceiveCodec(uint8_t payload_type,
Danil Chapovalov355b8d22021-08-13 14:50:37110 const VideoDecoder::Settings& settings);
andresp@webrtc.orgf7eb75b2013-09-14 00:25:28111
Peter Boström795dbe42015-11-27 13:09:07112 void RegisterExternalDecoder(VideoDecoder* externalDecoder,
perkj796cfaf2015-12-10 17:27:38113 uint8_t payloadType);
andresp@webrtc.orgf7eb75b2013-09-14 00:25:28114 int32_t RegisterReceiveCallback(VCMReceiveCallback* receiveCallback);
andresp@webrtc.orgf7eb75b2013-09-14 00:25:28115 int32_t RegisterFrameTypeCallback(VCMFrameTypeCallback* frameTypeCallback);
116 int32_t RegisterPacketRequestCallback(VCMPacketRequestCallback* callback);
andresp@webrtc.orgf7eb75b2013-09-14 00:25:28117
118 int32_t Decode(uint16_t maxWaitTimeMs);
andresp@webrtc.orgf7eb75b2013-09-14 00:25:28119
andresp@webrtc.orgf7eb75b2013-09-14 00:25:28120 int32_t IncomingPacket(const uint8_t* incomingPayload,
pkasting@chromium.org4591fbd2014-11-20 22:28:14121 size_t payloadLength,
Niels Möllerbe7a0ec2019-04-25 08:02:52122 const RTPHeader& rtp_header,
123 const RTPVideoHeader& video_header);
andresp@webrtc.orgf7eb75b2013-09-14 00:25:28124
andresp@webrtc.orgf7eb75b2013-09-14 00:25:28125 void SetNackSettings(size_t max_nack_list_size,
126 int max_packet_age_to_nack,
127 int max_incomplete_time_ms);
128
Danil Chapovalovce808862022-06-22 13:48:36129 void Process();
andresp@webrtc.orgf7eb75b2013-09-14 00:25:28130
131 protected:
Tommifbf3bce2018-02-21 14:56:05132 int32_t Decode(const webrtc::VCMEncodedFrame& frame);
andresp@webrtc.orgf7eb75b2013-09-14 00:25:28133 int32_t RequestKeyFrame();
andresp@webrtc.orgf7eb75b2013-09-14 00:25:28134
135 private:
Tommifbf3bce2018-02-21 14:56:05136 // 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 Titovc8421c42021-02-02 09:57:19143 SequenceChecker construction_thread_checker_;
144 SequenceChecker decoder_thread_checker_;
145 SequenceChecker module_thread_checker_;
pbos@webrtc.org20c1f562014-07-04 10:58:12146 Clock* const clock_;
Markus Handell6deec382020-07-07 10:17:12147 Mutex process_mutex_;
philipel721d4022016-12-15 15:10:57148 VCMTiming* _timing;
andresp@webrtc.orgf7eb75b2013-09-14 00:25:28149 VCMReceiver _receiver;
andresp@webrtc.orgf7eb75b2013-09-14 00:25:28150 VCMDecodedFrameCallback _decodedFrameCallback;
niklase@google.com470e71d2011-07-07 08:21:25151
Tommifbf3bce2018-02-21 14:56:05152 // 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;
Tommifbf3bce2018-02-21 14:56:05155 VCMPacketRequestCallback* _packetRequestCallback;
156
157 // Used on both the module and decoder thread.
Markus Handell6deec382020-07-07 10:17:12158 bool _scheduleKeyRequest RTC_GUARDED_BY(process_mutex_);
159 bool drop_frames_until_keyframe_ RTC_GUARDED_BY(process_mutex_);
sprang3911c262016-04-15 08:24:14160
Tommifbf3bce2018-02-21 14:56:05161 // 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ömed3277b2016-02-02 14:40:04165
Tommifbf3bce2018-02-21 14:56:05166 // Callbacks are set before the decoder thread starts.
Artem Titovdcd7fc72021-08-09 11:02:57167 // Once the decoder thread has been started, usage of `_codecDataBase` moves
Tommifbf3bce2018-02-21 14:56:05168 // over to the decoder thread.
Tommi20b32712022-09-29 12:13:15169 DEPRECATED_VCMDecoderDataBase _codecDataBase;
Tommifbf3bce2018-02-21 14:56:05170
Tommifbf3bce2018-02-21 14:56:05171 VCMProcessTimer _retransmissionTimer RTC_GUARDED_BY(module_thread_checker_);
172 VCMProcessTimer _keyRequestTimer RTC_GUARDED_BY(module_thread_checker_);
Tommifbf3bce2018-02-21 14:56:05173 ThreadUnsafeOneTimeEvent first_frame_received_
174 RTC_GUARDED_BY(decoder_thread_checker_);
niklase@google.com470e71d2011-07-07 08:21:25175};
andresp@webrtc.orgf7eb75b2013-09-14 00:25:28176
177} // namespace vcm
pbos@webrtc.orgd900e8b2013-07-03 15:12:26178} // namespace webrtc
Mirko Bonadei92ea95e2017-09-15 04:47:31179#endif // MODULES_VIDEO_CODING_VIDEO_CODING_IMPL_H_