• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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 <map>
15 #include <memory>
16 #include <string>
17 #include <vector>
18 
19 #include "absl/types/optional.h"
20 #include "api/field_trials_view.h"
21 #include "api/sequence_checker.h"
22 #include "modules/video_coding/frame_buffer.h"
23 #include "modules/video_coding/generic_decoder.h"
24 #include "modules/video_coding/include/video_coding.h"
25 #include "modules/video_coding/jitter_buffer.h"
26 #include "modules/video_coding/receiver.h"
27 #include "modules/video_coding/timing/timing.h"
28 #include "rtc_base/one_time_event.h"
29 #include "rtc_base/synchronization/mutex.h"
30 #include "rtc_base/thread_annotations.h"
31 #include "system_wrappers/include/clock.h"
32 
33 namespace webrtc {
34 
35 class VideoBitrateAllocator;
36 class VideoBitrateAllocationObserver;
37 
38 namespace vcm {
39 
40 class VCMProcessTimer {
41  public:
42   static const int64_t kDefaultProcessIntervalMs = 1000;
43 
VCMProcessTimer(int64_t periodMs,Clock * clock)44   VCMProcessTimer(int64_t periodMs, Clock* clock)
45       : _clock(clock),
46         _periodMs(periodMs),
47         _latestMs(_clock->TimeInMilliseconds()) {}
48   int64_t Period() const;
49   int64_t TimeUntilProcess() const;
50   void Processed();
51 
52  private:
53   Clock* _clock;
54   int64_t _periodMs;
55   int64_t _latestMs;
56 };
57 
58 class 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 
102 class VideoReceiver {
103  public:
104   VideoReceiver(Clock* clock,
105                 VCMTiming* timing,
106                 const FieldTrialsView& field_trials);
107   ~VideoReceiver();
108 
109   void RegisterReceiveCodec(uint8_t payload_type,
110                             const VideoDecoder::Settings& settings);
111 
112   void RegisterExternalDecoder(VideoDecoder* externalDecoder,
113                                uint8_t payloadType);
114   int32_t RegisterReceiveCallback(VCMReceiveCallback* receiveCallback);
115   int32_t RegisterFrameTypeCallback(VCMFrameTypeCallback* frameTypeCallback);
116   int32_t RegisterPacketRequestCallback(VCMPacketRequestCallback* callback);
117 
118   int32_t Decode(uint16_t maxWaitTimeMs);
119 
120   int32_t IncomingPacket(const uint8_t* incomingPayload,
121                          size_t payloadLength,
122                          const RTPHeader& rtp_header,
123                          const RTPVideoHeader& video_header);
124 
125   void SetNackSettings(size_t max_nack_list_size,
126                        int max_packet_age_to_nack,
127                        int max_incomplete_time_ms);
128 
129   void Process();
130 
131  protected:
132   int32_t Decode(const webrtc::VCMEncodedFrame& frame);
133   int32_t RequestKeyFrame();
134 
135  private:
136   // 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 
143   SequenceChecker construction_thread_checker_;
144   SequenceChecker decoder_thread_checker_;
145   SequenceChecker module_thread_checker_;
146   Clock* const clock_;
147   Mutex process_mutex_;
148   VCMTiming* _timing;
149   VCMReceiver _receiver;
150   VCMDecodedFrameCallback _decodedFrameCallback;
151 
152   // 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;
155   VCMPacketRequestCallback* _packetRequestCallback;
156 
157   // Used on both the module and decoder thread.
158   bool _scheduleKeyRequest RTC_GUARDED_BY(process_mutex_);
159   bool drop_frames_until_keyframe_ RTC_GUARDED_BY(process_mutex_);
160 
161   // 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_;
165 
166   // Callbacks are set before the decoder thread starts.
167   // Once the decoder thread has been started, usage of `_codecDataBase` moves
168   // over to the decoder thread.
169   DEPRECATED_VCMDecoderDataBase _codecDataBase;
170 
171   VCMProcessTimer _retransmissionTimer RTC_GUARDED_BY(module_thread_checker_);
172   VCMProcessTimer _keyRequestTimer RTC_GUARDED_BY(module_thread_checker_);
173   ThreadUnsafeOneTimeEvent first_frame_received_
174       RTC_GUARDED_BY(decoder_thread_checker_);
175 };
176 
177 }  // namespace vcm
178 }  // namespace webrtc
179 #endif  // MODULES_VIDEO_CODING_VIDEO_CODING_IMPL_H_
180