• 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 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 <vector>
17 
18 #include "webrtc/base/thread_annotations.h"
19 #include "webrtc/base/thread_checker.h"
20 #include "webrtc/modules/video_coding/codec_database.h"
21 #include "webrtc/modules/video_coding/frame_buffer.h"
22 #include "webrtc/modules/video_coding/generic_decoder.h"
23 #include "webrtc/modules/video_coding/generic_encoder.h"
24 #include "webrtc/modules/video_coding/jitter_buffer.h"
25 #include "webrtc/modules/video_coding/media_optimization.h"
26 #include "webrtc/modules/video_coding/receiver.h"
27 #include "webrtc/modules/video_coding/timing.h"
28 #include "webrtc/modules/video_coding/utility/qp_parser.h"
29 #include "webrtc/system_wrappers/include/clock.h"
30 #include "webrtc/system_wrappers/include/critical_section_wrapper.h"
31 
32 namespace webrtc {
33 
34 class EncodedFrameObserver;
35 
36 namespace vcm {
37 
38 class VCMProcessTimer {
39  public:
VCMProcessTimer(int64_t periodMs,Clock * clock)40   VCMProcessTimer(int64_t periodMs, Clock* clock)
41       : _clock(clock),
42         _periodMs(periodMs),
43         _latestMs(_clock->TimeInMilliseconds()) {}
44   int64_t Period() const;
45   int64_t TimeUntilProcess() const;
46   void Processed();
47 
48  private:
49   Clock* _clock;
50   int64_t _periodMs;
51   int64_t _latestMs;
52 };
53 
54 class VideoSender {
55  public:
56   typedef VideoCodingModule::SenderNackMode SenderNackMode;
57 
58   VideoSender(Clock* clock,
59               EncodedImageCallback* post_encode_callback,
60               VideoEncoderRateObserver* encoder_rate_observer,
61               VCMQMSettingsCallback* qm_settings_callback);
62 
63   ~VideoSender();
64 
65   // Register the send codec to be used.
66   // This method must be called on the construction thread.
67   int32_t RegisterSendCodec(const VideoCodec* sendCodec,
68                             uint32_t numberOfCores,
69                             uint32_t maxPayloadSize);
70 
71   void RegisterExternalEncoder(VideoEncoder* externalEncoder,
72                                uint8_t payloadType,
73                                bool internalSource);
74 
75   int Bitrate(unsigned int* bitrate) const;
76   int FrameRate(unsigned int* framerate) const;
77 
78   int32_t SetChannelParameters(uint32_t target_bitrate,  // bits/s.
79                                uint8_t lossRate,
80                                int64_t rtt);
81 
82   int32_t RegisterTransportCallback(VCMPacketizationCallback* transport);
83   int32_t RegisterSendStatisticsCallback(VCMSendStatisticsCallback* sendStats);
84   int32_t RegisterProtectionCallback(VCMProtectionCallback* protection);
85   void SetVideoProtection(VCMVideoProtection videoProtection);
86 
87   int32_t AddVideoFrame(const VideoFrame& videoFrame,
88                         const VideoContentMetrics* _contentMetrics,
89                         const CodecSpecificInfo* codecSpecificInfo);
90 
91   int32_t IntraFrameRequest(int stream_index);
92   int32_t EnableFrameDropper(bool enable);
93 
94   void SuspendBelowMinBitrate();
95   bool VideoSuspended() const;
96 
97   int64_t TimeUntilNextProcess();
98   int32_t Process();
99 
100  private:
101   void SetEncoderParameters(EncoderParameters params)
102       EXCLUSIVE_LOCKS_REQUIRED(send_crit_);
103 
104   Clock* const clock_;
105 
106   rtc::scoped_ptr<CriticalSectionWrapper> process_crit_sect_;
107   mutable rtc::CriticalSection send_crit_;
108   VCMGenericEncoder* _encoder;
109   VCMEncodedFrameCallback _encodedFrameCallback;
110   std::vector<FrameType> _nextFrameTypes;
111   media_optimization::MediaOptimization _mediaOpt;
112   VCMSendStatisticsCallback* _sendStatsCallback GUARDED_BY(process_crit_sect_);
113   VCMCodecDataBase _codecDataBase GUARDED_BY(send_crit_);
114   bool frame_dropper_enabled_ GUARDED_BY(send_crit_);
115   VCMProcessTimer _sendStatsTimer;
116 
117   // Must be accessed on the construction thread of VideoSender.
118   VideoCodec current_codec_;
119   rtc::ThreadChecker main_thread_;
120 
121   VCMQMSettingsCallback* const qm_settings_callback_;
122   VCMProtectionCallback* protection_callback_;
123 
124   rtc::CriticalSection params_lock_;
125   EncoderParameters encoder_params_ GUARDED_BY(params_lock_);
126 };
127 
128 class VideoReceiver {
129  public:
130   typedef VideoCodingModule::ReceiverRobustness ReceiverRobustness;
131 
132   VideoReceiver(Clock* clock, EventFactory* event_factory);
133   ~VideoReceiver();
134 
135   int32_t RegisterReceiveCodec(const VideoCodec* receiveCodec,
136                                int32_t numberOfCores,
137                                bool requireKeyFrame);
138 
139   void RegisterExternalDecoder(VideoDecoder* externalDecoder,
140                                uint8_t payloadType);
141   int32_t RegisterReceiveCallback(VCMReceiveCallback* receiveCallback);
142   int32_t RegisterReceiveStatisticsCallback(
143       VCMReceiveStatisticsCallback* receiveStats);
144   int32_t RegisterDecoderTimingCallback(
145       VCMDecoderTimingCallback* decoderTiming);
146   int32_t RegisterFrameTypeCallback(VCMFrameTypeCallback* frameTypeCallback);
147   int32_t RegisterPacketRequestCallback(VCMPacketRequestCallback* callback);
148   int RegisterRenderBufferSizeCallback(VCMRenderBufferSizeCallback* callback);
149 
150   int32_t Decode(uint16_t maxWaitTimeMs);
151   int32_t ResetDecoder();
152 
153   int32_t ReceiveCodec(VideoCodec* currentReceiveCodec) const;
154   VideoCodecType ReceiveCodec() const;
155 
156   int32_t IncomingPacket(const uint8_t* incomingPayload,
157                          size_t payloadLength,
158                          const WebRtcRTPHeader& rtpInfo);
159   int32_t SetMinimumPlayoutDelay(uint32_t minPlayoutDelayMs);
160   int32_t SetRenderDelay(uint32_t timeMS);
161   int32_t Delay() const;
162   uint32_t DiscardedPackets() const;
163 
164   int SetReceiverRobustnessMode(ReceiverRobustness robustnessMode,
165                                 VCMDecodeErrorMode errorMode);
166   void SetNackSettings(size_t max_nack_list_size,
167                        int max_packet_age_to_nack,
168                        int max_incomplete_time_ms);
169 
170   void SetDecodeErrorMode(VCMDecodeErrorMode decode_error_mode);
171   int SetMinReceiverDelay(int desired_delay_ms);
172 
173   int32_t SetReceiveChannelParameters(int64_t rtt);
174   int32_t SetVideoProtection(VCMVideoProtection videoProtection, bool enable);
175 
176   int64_t TimeUntilNextProcess();
177   int32_t Process();
178 
179   void RegisterPreDecodeImageCallback(EncodedImageCallback* observer);
180   void TriggerDecoderShutdown();
181 
182  protected:
183   int32_t Decode(const webrtc::VCMEncodedFrame& frame)
184       EXCLUSIVE_LOCKS_REQUIRED(_receiveCritSect);
185   int32_t RequestKeyFrame();
186   int32_t RequestSliceLossIndication(const uint64_t pictureID) const;
187 
188  private:
189   Clock* const clock_;
190   rtc::scoped_ptr<CriticalSectionWrapper> process_crit_sect_;
191   CriticalSectionWrapper* _receiveCritSect;
192   VCMTiming _timing;
193   VCMReceiver _receiver;
194   VCMDecodedFrameCallback _decodedFrameCallback;
195   VCMFrameTypeCallback* _frameTypeCallback GUARDED_BY(process_crit_sect_);
196   VCMReceiveStatisticsCallback* _receiveStatsCallback
197       GUARDED_BY(process_crit_sect_);
198   VCMDecoderTimingCallback* _decoderTimingCallback
199       GUARDED_BY(process_crit_sect_);
200   VCMPacketRequestCallback* _packetRequestCallback
201       GUARDED_BY(process_crit_sect_);
202   VCMRenderBufferSizeCallback* render_buffer_callback_
203       GUARDED_BY(process_crit_sect_);
204   VCMGenericDecoder* _decoder;
205 #ifdef DEBUG_DECODER_BIT_STREAM
206   FILE* _bitStreamBeforeDecoder;
207 #endif
208   VCMFrameBuffer _frameFromFile;
209   bool _scheduleKeyRequest GUARDED_BY(process_crit_sect_);
210   size_t max_nack_list_size_ GUARDED_BY(process_crit_sect_);
211   EncodedImageCallback* pre_decode_image_callback_ GUARDED_BY(_receiveCritSect);
212 
213   VCMCodecDataBase _codecDataBase GUARDED_BY(_receiveCritSect);
214   VCMProcessTimer _receiveStatsTimer;
215   VCMProcessTimer _retransmissionTimer;
216   VCMProcessTimer _keyRequestTimer;
217   QpParser qp_parser_;
218 };
219 
220 }  // namespace vcm
221 }  // namespace webrtc
222 #endif  // WEBRTC_MODULES_VIDEO_CODING_VIDEO_CODING_IMPL_H_
223