Line data Source code
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 WEBRTC_MODULES_VIDEO_CODING_VIDEO_CODING_IMPL_H_
12 : #define WEBRTC_MODULES_VIDEO_CODING_VIDEO_CODING_IMPL_H_
13 :
14 : #include "webrtc/modules/video_coding/include/video_coding.h"
15 :
16 : #include <memory>
17 : #include <string>
18 : #include <vector>
19 :
20 : #include "webrtc/base/onetimeevent.h"
21 : #include "webrtc/base/thread_annotations.h"
22 : #include "webrtc/base/sequenced_task_checker.h"
23 : #include "webrtc/common_video/include/frame_callback.h"
24 : #include "webrtc/modules/video_coding/codec_database.h"
25 : #include "webrtc/modules/video_coding/frame_buffer.h"
26 : #include "webrtc/modules/video_coding/generic_decoder.h"
27 : #include "webrtc/modules/video_coding/generic_encoder.h"
28 : #include "webrtc/modules/video_coding/jitter_buffer.h"
29 : #include "webrtc/modules/video_coding/media_optimization.h"
30 : #include "webrtc/modules/video_coding/receiver.h"
31 : #include "webrtc/modules/video_coding/timing.h"
32 : #include "webrtc/modules/video_coding/utility/qp_parser.h"
33 : #include "webrtc/system_wrappers/include/clock.h"
34 :
35 : namespace webrtc {
36 :
37 : class VideoBitrateAllocator;
38 : class VideoBitrateAllocationObserver;
39 :
40 : namespace vcm {
41 :
42 : class VCMProcessTimer {
43 : public:
44 : static const int64_t kDefaultProcessIntervalMs = 1000;
45 :
46 0 : VCMProcessTimer(int64_t periodMs, Clock* clock)
47 0 : : _clock(clock),
48 : _periodMs(periodMs),
49 0 : _latestMs(_clock->TimeInMilliseconds()) {}
50 : int64_t Period() const;
51 : int64_t TimeUntilProcess() const;
52 : void Processed();
53 :
54 : private:
55 : Clock* _clock;
56 : int64_t _periodMs;
57 : int64_t _latestMs;
58 : };
59 :
60 : class VideoSender : public Module {
61 : public:
62 : typedef VideoCodingModule::SenderNackMode SenderNackMode;
63 :
64 : VideoSender(Clock* clock,
65 : EncodedImageCallback* post_encode_callback,
66 : VCMSendStatisticsCallback* send_stats_callback);
67 :
68 : ~VideoSender();
69 :
70 : // Register the send codec to be used.
71 : // This method must be called on the construction thread.
72 : int32_t RegisterSendCodec(const VideoCodec* sendCodec,
73 : uint32_t numberOfCores,
74 : uint32_t maxPayloadSize);
75 :
76 : void RegisterExternalEncoder(VideoEncoder* externalEncoder,
77 : uint8_t payloadType,
78 : bool internalSource);
79 :
80 : int Bitrate(unsigned int* bitrate) const;
81 : int FrameRate(unsigned int* framerate) const;
82 :
83 : // Update the channel parameters based on new rates and rtt. This will also
84 : // cause an immediate call to VideoEncoder::SetRateAllocation().
85 : int32_t SetChannelParameters(
86 : uint32_t target_bitrate_bps,
87 : uint8_t loss_rate,
88 : int64_t rtt,
89 : VideoBitrateAllocator* bitrate_allocator,
90 : VideoBitrateAllocationObserver* bitrate_updated_callback);
91 :
92 : // Updates the channel parameters with a new bitrate allocation, but using the
93 : // current targit_bitrate, loss rate and rtt. That is, the distribution or
94 : // caps may be updated to a change to a new VideoCodec or allocation mode.
95 : // The new parameters will be stored as pending EncoderParameters, and the
96 : // encoder will only be updated on the next frame.
97 : void UpdateChannelParemeters(
98 : VideoBitrateAllocator* bitrate_allocator,
99 : VideoBitrateAllocationObserver* bitrate_updated_callback);
100 :
101 : // Deprecated:
102 : // TODO(perkj): Remove once no projects use it.
103 : int32_t RegisterProtectionCallback(VCMProtectionCallback* protection);
104 :
105 : int32_t AddVideoFrame(const VideoFrame& videoFrame,
106 : const CodecSpecificInfo* codecSpecificInfo);
107 :
108 : int32_t IntraFrameRequest(size_t stream_index);
109 : int32_t EnableFrameDropper(bool enable);
110 :
111 : void SetCPULoadState(CPULoadState state);
112 :
113 : int64_t TimeUntilNextProcess() override;
114 : void Process() override;
115 :
116 : private:
117 : EncoderParameters UpdateEncoderParameters(
118 : const EncoderParameters& params,
119 : VideoBitrateAllocator* bitrate_allocator,
120 : uint32_t target_bitrate_bps);
121 : void SetEncoderParameters(EncoderParameters params, bool has_internal_source)
122 : EXCLUSIVE_LOCKS_REQUIRED(encoder_crit_);
123 :
124 : Clock* const clock_;
125 :
126 : rtc::CriticalSection encoder_crit_;
127 : VCMGenericEncoder* _encoder;
128 : media_optimization::MediaOptimization _mediaOpt;
129 : VCMEncodedFrameCallback _encodedFrameCallback GUARDED_BY(encoder_crit_);
130 : EncodedImageCallback* const post_encode_callback_;
131 : VCMSendStatisticsCallback* const send_stats_callback_;
132 : VCMCodecDataBase _codecDataBase GUARDED_BY(encoder_crit_);
133 : bool frame_dropper_enabled_ GUARDED_BY(encoder_crit_);
134 : VCMProcessTimer _sendStatsTimer;
135 :
136 : // Must be accessed on the construction thread of VideoSender.
137 : VideoCodec current_codec_;
138 : rtc::SequencedTaskChecker sequenced_checker_;
139 :
140 : rtc::CriticalSection params_crit_;
141 : EncoderParameters encoder_params_ GUARDED_BY(params_crit_);
142 : bool encoder_has_internal_source_ GUARDED_BY(params_crit_);
143 : std::vector<FrameType> next_frame_types_ GUARDED_BY(params_crit_);
144 : };
145 :
146 : class VideoReceiver : public Module {
147 : public:
148 : typedef VideoCodingModule::ReceiverRobustness ReceiverRobustness;
149 :
150 : VideoReceiver(Clock* clock,
151 : EventFactory* event_factory,
152 : EncodedImageCallback* pre_decode_image_callback,
153 : VCMTiming* timing,
154 : NackSender* nack_sender = nullptr,
155 : KeyFrameRequestSender* keyframe_request_sender = nullptr);
156 : ~VideoReceiver();
157 :
158 : void SetReceiveState(VideoReceiveState state);
159 : int32_t RegisterReceiveCodec(const VideoCodec* receiveCodec,
160 : int32_t numberOfCores,
161 : bool requireKeyFrame);
162 :
163 : void RegisterExternalDecoder(VideoDecoder* externalDecoder,
164 : uint8_t payloadType);
165 : int32_t RegisterReceiveCallback(VCMReceiveCallback* receiveCallback);
166 : int32_t RegisterReceiveStatisticsCallback(
167 : VCMReceiveStatisticsCallback* receiveStats);
168 : int32_t RegisterDecoderTimingCallback(
169 : VCMDecoderTimingCallback* decoderTiming);
170 : int32_t RegisterFrameTypeCallback(VCMFrameTypeCallback* frameTypeCallback);
171 : int32_t RegisterPacketRequestCallback(VCMPacketRequestCallback* callback);
172 : int32_t RegisterReceiveStateCallback(VCMReceiveStateCallback* callback);
173 :
174 : int32_t Decode(uint16_t maxWaitTimeMs);
175 :
176 : int32_t Decode(const webrtc::VCMEncodedFrame* frame);
177 :
178 : int32_t ReceiveCodec(VideoCodec* currentReceiveCodec) const;
179 : VideoCodecType ReceiveCodec() const;
180 :
181 : int32_t IncomingPacket(const uint8_t* incomingPayload,
182 : size_t payloadLength,
183 : const WebRtcRTPHeader& rtpInfo);
184 : int32_t SetMinimumPlayoutDelay(uint32_t minPlayoutDelayMs);
185 : int32_t SetRenderDelay(uint32_t timeMS);
186 : int32_t Delay() const;
187 : uint32_t DiscardedPackets() const;
188 :
189 : int SetReceiverRobustnessMode(ReceiverRobustness robustnessMode,
190 : VCMDecodeErrorMode errorMode);
191 : void SetNackSettings(size_t max_nack_list_size,
192 : int max_packet_age_to_nack,
193 : int max_incomplete_time_ms);
194 :
195 : void SetDecodeErrorMode(VCMDecodeErrorMode decode_error_mode);
196 : int SetMinReceiverDelay(int desired_delay_ms);
197 :
198 : int32_t SetReceiveChannelParameters(int64_t rtt);
199 : int32_t SetVideoProtection(VCMVideoProtection videoProtection, bool enable);
200 :
201 : int64_t TimeUntilNextProcess() override;
202 : void Process() override;
203 :
204 : void TriggerDecoderShutdown();
205 : void Reset();
206 :
207 : protected:
208 : int32_t Decode(const webrtc::VCMEncodedFrame& frame)
209 : EXCLUSIVE_LOCKS_REQUIRED(receive_crit_);
210 : int32_t RequestKeyFrame();
211 : int32_t RequestSliceLossIndication(const uint64_t pictureID) const;
212 :
213 : private:
214 : Clock* const clock_;
215 : rtc::CriticalSection process_crit_;
216 : rtc::CriticalSection receive_crit_;
217 : VideoReceiveState _receiveState;
218 : VCMTiming* _timing;
219 : VCMReceiver _receiver;
220 : VCMDecodedFrameCallback _decodedFrameCallback;
221 : VCMFrameTypeCallback* _frameTypeCallback GUARDED_BY(process_crit_);
222 : VCMReceiveStatisticsCallback* _receiveStatsCallback GUARDED_BY(process_crit_);
223 : VCMDecoderTimingCallback* _decoderTimingCallback GUARDED_BY(process_crit_);
224 : VCMPacketRequestCallback* _packetRequestCallback GUARDED_BY(process_crit_);
225 : VCMReceiveStateCallback* _receiveStateCallback GUARDED_BY(process_crit_);
226 : VCMGenericDecoder* _decoder;
227 :
228 : VCMFrameBuffer _frameFromFile;
229 : bool _scheduleKeyRequest GUARDED_BY(process_crit_);
230 : bool drop_frames_until_keyframe_ GUARDED_BY(process_crit_);
231 : size_t max_nack_list_size_ GUARDED_BY(process_crit_);
232 :
233 : VCMCodecDataBase _codecDataBase GUARDED_BY(receive_crit_);
234 : EncodedImageCallback* pre_decode_image_callback_;
235 :
236 : VCMProcessTimer _receiveStatsTimer;
237 : VCMProcessTimer _retransmissionTimer;
238 : VCMProcessTimer _keyRequestTimer;
239 : QpParser qp_parser_;
240 : ThreadUnsafeOneTimeEvent first_frame_received_;
241 : };
242 :
243 : } // namespace vcm
244 : } // namespace webrtc
245 : #endif // WEBRTC_MODULES_VIDEO_CODING_VIDEO_CODING_IMPL_H_
|