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