aboutsummaryrefslogtreecommitdiff
path: root/webrtc/modules/video_coding/main/source/video_coding_impl.h
blob: c1d02d94e913cf4e7a29ef35606df898fb5887dd (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
/*
 *  Copyright (c) 2012 The WebRTC project authors. All Rights Reserved.
 *
 *  Use of this source code is governed by a BSD-style license
 *  that can be found in the LICENSE file in the root of the source
 *  tree. An additional intellectual property rights grant can be found
 *  in the file PATENTS.  All contributing project authors may
 *  be found in the AUTHORS file in the root of the source tree.
 */

#ifndef WEBRTC_MODULES_VIDEO_CODING_VIDEO_CODING_IMPL_H_
#define WEBRTC_MODULES_VIDEO_CODING_VIDEO_CODING_IMPL_H_

#include "webrtc/modules/video_coding/main/interface/video_coding.h"

#include <vector>

#include "webrtc/modules/video_coding/main/source/codec_database.h"
#include "webrtc/modules/video_coding/main/source/frame_buffer.h"
#include "webrtc/modules/video_coding/main/source/generic_decoder.h"
#include "webrtc/modules/video_coding/main/source/generic_encoder.h"
#include "webrtc/modules/video_coding/main/source/jitter_buffer.h"
#include "webrtc/modules/video_coding/main/source/media_optimization.h"
#include "webrtc/modules/video_coding/main/source/receiver.h"
#include "webrtc/modules/video_coding/main/source/timing.h"
#include "webrtc/system_wrappers/interface/clock.h"
#include "webrtc/system_wrappers/interface/critical_section_wrapper.h"

namespace webrtc {
namespace vcm {

class VCMProcessTimer {
 public:
  VCMProcessTimer(uint32_t periodMs, Clock* clock)
      : _clock(clock),
        _periodMs(periodMs),
        _latestMs(_clock->TimeInMilliseconds()) {}
  uint32_t Period() const;
  uint32_t TimeUntilProcess() const;
  void Processed();

 private:
  Clock* _clock;
  uint32_t _periodMs;
  int64_t _latestMs;
};

class VideoSender {
 public:
  typedef VideoCodingModule::SenderNackMode SenderNackMode;

  VideoSender(const int32_t id, Clock* clock);
  ~VideoSender();

  int32_t InitializeSender();

  // Register the send codec to be used.
  int32_t RegisterSendCodec(const VideoCodec* sendCodec,
                            uint32_t numberOfCores,
                            uint32_t maxPayloadSize);

  int32_t SendCodec(VideoCodec* currentSendCodec) const;
  VideoCodecType SendCodec() const;
  int32_t RegisterExternalEncoder(VideoEncoder* externalEncoder,
                                  uint8_t payloadType,
                                  bool internalSource);

  int32_t CodecConfigParameters(uint8_t* buffer, int32_t size);
  int Bitrate(unsigned int* bitrate) const;
  int FrameRate(unsigned int* framerate) const;

  int32_t SetChannelParameters(uint32_t target_bitrate,  // bits/s.
                               uint8_t lossRate,
                               uint32_t rtt);

  int32_t RegisterTransportCallback(VCMPacketizationCallback* transport);
  int32_t RegisterSendStatisticsCallback(VCMSendStatisticsCallback* sendStats);
  int32_t RegisterVideoQMCallback(VCMQMSettingsCallback* videoQMSettings);
  int32_t RegisterProtectionCallback(VCMProtectionCallback* protection);
  int32_t SetVideoProtection(VCMVideoProtection videoProtection, bool enable);

  int32_t AddVideoFrame(const I420VideoFrame& videoFrame,
                        const VideoContentMetrics* _contentMetrics,
                        const CodecSpecificInfo* codecSpecificInfo);

  int32_t IntraFrameRequest(int stream_index);
  int32_t EnableFrameDropper(bool enable);
  int32_t SentFrameCount(VCMFrameCount* frameCount) const;

  int SetSenderNackMode(SenderNackMode mode);
  int SetSenderReferenceSelection(bool enable);
  int SetSenderFEC(bool enable);
  int SetSenderKeyFramePeriod(int periodMs);

  int StartDebugRecording(const char* file_name_utf8);
  int StopDebugRecording();

  void SuspendBelowMinBitrate();
  bool VideoSuspended() const;

  int32_t TimeUntilNextProcess();
  int32_t Process();

 private:
  int32_t _id;
  Clock* clock_;

  scoped_ptr<CriticalSectionWrapper> process_crit_sect_;
  CriticalSectionWrapper* _sendCritSect;
  VCMGenericEncoder* _encoder;
  VCMEncodedFrameCallback _encodedFrameCallback;
  std::vector<FrameType> _nextFrameTypes;
  media_optimization::MediaOptimization _mediaOpt;
  VCMSendStatisticsCallback* _sendStatsCallback;
  FILE* _encoderInputFile;
  VCMCodecDataBase _codecDataBase;
  bool frame_dropper_enabled_;
  VCMProcessTimer _sendStatsTimer;
};

class VideoReceiver {
 public:
  typedef VideoCodingModule::ReceiverRobustness ReceiverRobustness;

  VideoReceiver(const int32_t id, Clock* clock, EventFactory* event_factory);
  ~VideoReceiver();

  int32_t InitializeReceiver();
  int32_t RegisterReceiveCodec(const VideoCodec* receiveCodec,
                               int32_t numberOfCores,
                               bool requireKeyFrame);

  int32_t RegisterExternalDecoder(VideoDecoder* externalDecoder,
                                  uint8_t payloadType,
                                  bool internalRenderTiming);
  int32_t RegisterReceiveCallback(VCMReceiveCallback* receiveCallback);
  int32_t RegisterReceiveStatisticsCallback(
      VCMReceiveStatisticsCallback* receiveStats);
  int32_t RegisterDecoderTimingCallback(
      VCMDecoderTimingCallback* decoderTiming);
  int32_t RegisterFrameTypeCallback(VCMFrameTypeCallback* frameTypeCallback);
  int32_t RegisterPacketRequestCallback(VCMPacketRequestCallback* callback);
  int RegisterRenderBufferSizeCallback(VCMRenderBufferSizeCallback* callback);

  int32_t Decode(uint16_t maxWaitTimeMs);
  int32_t DecodeDualFrame(uint16_t maxWaitTimeMs);
  int32_t ResetDecoder();

  int32_t ReceiveCodec(VideoCodec* currentReceiveCodec) const;
  VideoCodecType ReceiveCodec() const;

  int32_t IncomingPacket(const uint8_t* incomingPayload,
                         uint32_t payloadLength,
                         const WebRtcRTPHeader& rtpInfo);
  int32_t SetMinimumPlayoutDelay(uint32_t minPlayoutDelayMs);
  int32_t SetRenderDelay(uint32_t timeMS);
  int32_t Delay() const;
  int32_t ReceivedFrameCount(VCMFrameCount* frameCount) const;
  uint32_t DiscardedPackets() const;

  int SetReceiverRobustnessMode(ReceiverRobustness robustnessMode,
                                VCMDecodeErrorMode errorMode);
  void SetNackSettings(size_t max_nack_list_size,
                       int max_packet_age_to_nack,
                       int max_incomplete_time_ms);

  void SetDecodeErrorMode(VCMDecodeErrorMode decode_error_mode);
  int SetMinReceiverDelay(int desired_delay_ms);

  int32_t SetReceiveChannelParameters(uint32_t rtt);
  int32_t SetVideoProtection(VCMVideoProtection videoProtection, bool enable);

  int32_t TimeUntilNextProcess();
  int32_t Process();

 protected:
  int32_t Decode(const webrtc::VCMEncodedFrame& frame);
  int32_t RequestKeyFrame();
  int32_t RequestSliceLossIndication(const uint64_t pictureID) const;
  int32_t NackList(uint16_t* nackList, uint16_t* size);

 private:
  enum VCMKeyRequestMode {
    kKeyOnError,    // Normal mode, request key frames on decoder error
    kKeyOnKeyLoss,  // Request key frames on decoder error and on packet loss
                    // in key frames.
    kKeyOnLoss,     // Request key frames on decoder error and on packet loss
                    // in any frame
  };

  int32_t _id;
  Clock* clock_;
  scoped_ptr<CriticalSectionWrapper> process_crit_sect_;
  CriticalSectionWrapper* _receiveCritSect;
  bool _receiverInited;
  VCMTiming _timing;
  VCMTiming _dualTiming;
  VCMReceiver _receiver;
  VCMReceiver _dualReceiver;
  VCMDecodedFrameCallback _decodedFrameCallback;
  VCMDecodedFrameCallback _dualDecodedFrameCallback;
  VCMFrameTypeCallback* _frameTypeCallback;
  VCMReceiveStatisticsCallback* _receiveStatsCallback;
  VCMDecoderTimingCallback* _decoderTimingCallback;
  VCMPacketRequestCallback* _packetRequestCallback;
  VCMRenderBufferSizeCallback* render_buffer_callback_;
  VCMGenericDecoder* _decoder;
  VCMGenericDecoder* _dualDecoder;
#ifdef DEBUG_DECODER_BIT_STREAM
  FILE* _bitStreamBeforeDecoder;
#endif
  VCMFrameBuffer _frameFromFile;
  VCMKeyRequestMode _keyRequestMode;
  bool _scheduleKeyRequest;
  size_t max_nack_list_size_;

  VCMCodecDataBase _codecDataBase;
  VCMProcessTimer _receiveStatsTimer;
  VCMProcessTimer _retransmissionTimer;
  VCMProcessTimer _keyRequestTimer;
};

}  // namespace vcm
}  // namespace webrtc
#endif  // WEBRTC_MODULES_VIDEO_CODING_VIDEO_CODING_IMPL_H_