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