blob: dc766b1ed2dddbb9ef7bc90b7b8a17c11bcca58e [file] [log] [blame]
niklase@google.com470e71d2011-07-07 08:21:25 +00001/*
pwestin@webrtc.org52fd98d2012-02-13 09:03:53 +00002 * Copyright (c) 2012 The WebRTC project authors. All Rights Reserved.
niklase@google.com470e71d2011-07-07 08:21:25 +00003 *
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
Henrik Kjellander2557b862015-11-18 22:00:21 +010014#include "webrtc/modules/video_coding/include/video_coding.h"
niklase@google.com470e71d2011-07-07 08:21:25 +000015
stefan@webrtc.orgc5300432012-10-08 07:06:53 +000016#include <vector>
17
pbos@webrtc.org38344ed2014-09-24 06:05:00 +000018#include "webrtc/base/thread_annotations.h"
tommi@webrtc.orge07710c2015-02-19 17:43:25 +000019#include "webrtc/base/thread_checker.h"
Henrik Kjellander2557b862015-11-18 22:00:21 +010020#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"
kjellander@webrtc.orgb7ce9642015-11-18 23:04:10 +010028#include "webrtc/modules/video_coding/utility/qp_parser.h"
Henrik Kjellander98f53512015-10-28 18:17:40 +010029#include "webrtc/system_wrappers/include/clock.h"
30#include "webrtc/system_wrappers/include/critical_section_wrapper.h"
niklase@google.com470e71d2011-07-07 08:21:25 +000031
andresp@webrtc.orgf7eb75b2013-09-14 00:25:28 +000032namespace webrtc {
sprang@webrtc.org40709352013-11-26 11:41:59 +000033
andresp@webrtc.orgf7eb75b2013-09-14 00:25:28 +000034namespace vcm {
niklase@google.com470e71d2011-07-07 08:21:25 +000035
andresp@webrtc.orgf7eb75b2013-09-14 00:25:28 +000036class VCMProcessTimer {
37 public:
pkasting@chromium.org0b1534c2014-12-15 22:09:40 +000038 VCMProcessTimer(int64_t periodMs, Clock* clock)
andresp@webrtc.orgf7eb75b2013-09-14 00:25:28 +000039 : _clock(clock),
40 _periodMs(periodMs),
41 _latestMs(_clock->TimeInMilliseconds()) {}
pkasting@chromium.org0b1534c2014-12-15 22:09:40 +000042 int64_t Period() const;
43 int64_t TimeUntilProcess() const;
andresp@webrtc.orgf7eb75b2013-09-14 00:25:28 +000044 void Processed();
niklase@google.com470e71d2011-07-07 08:21:25 +000045
andresp@webrtc.orgf7eb75b2013-09-14 00:25:28 +000046 private:
47 Clock* _clock;
pkasting@chromium.org0b1534c2014-12-15 22:09:40 +000048 int64_t _periodMs;
andresp@webrtc.orgf7eb75b2013-09-14 00:25:28 +000049 int64_t _latestMs;
niklase@google.com470e71d2011-07-07 08:21:25 +000050};
51
andresp@webrtc.orgf7eb75b2013-09-14 00:25:28 +000052class VideoSender {
53 public:
54 typedef VideoCodingModule::SenderNackMode SenderNackMode;
55
pbos@webrtc.org891d4832015-02-26 13:15:22 +000056 VideoSender(Clock* clock,
57 EncodedImageCallback* post_encode_callback,
mflodmanfcf54bd2015-04-14 21:28:08 +020058 VideoEncoderRateObserver* encoder_rate_observer,
59 VCMQMSettingsCallback* qm_settings_callback);
andresp@webrtc.org1df9dc32014-01-09 08:01:57 +000060
andresp@webrtc.orgf7eb75b2013-09-14 00:25:28 +000061 ~VideoSender();
62
andresp@webrtc.orgf7eb75b2013-09-14 00:25:28 +000063 // Register the send codec to be used.
tommi@webrtc.orge07710c2015-02-19 17:43:25 +000064 // This method must be called on the construction thread.
andresp@webrtc.orgf7eb75b2013-09-14 00:25:28 +000065 int32_t RegisterSendCodec(const VideoCodec* sendCodec,
66 uint32_t numberOfCores,
67 uint32_t maxPayloadSize);
tommi@webrtc.orge07710c2015-02-19 17:43:25 +000068
Peter Boström795dbe42015-11-27 14:09:07 +010069 void RegisterExternalEncoder(VideoEncoder* externalEncoder,
70 uint8_t payloadType,
71 bool internalSource);
andresp@webrtc.orgf7eb75b2013-09-14 00:25:28 +000072
andresp@webrtc.orgf7eb75b2013-09-14 00:25:28 +000073 int Bitrate(unsigned int* bitrate) const;
74 int FrameRate(unsigned int* framerate) const;
75
76 int32_t SetChannelParameters(uint32_t target_bitrate, // bits/s.
77 uint8_t lossRate,
pkasting@chromium.org16825b12015-01-12 21:51:21 +000078 int64_t rtt);
andresp@webrtc.orgf7eb75b2013-09-14 00:25:28 +000079
80 int32_t RegisterTransportCallback(VCMPacketizationCallback* transport);
81 int32_t RegisterSendStatisticsCallback(VCMSendStatisticsCallback* sendStats);
andresp@webrtc.orgf7eb75b2013-09-14 00:25:28 +000082 int32_t RegisterProtectionCallback(VCMProtectionCallback* protection);
pbosba8c15b2015-07-14 09:36:34 -070083 void SetVideoProtection(VCMVideoProtection videoProtection);
andresp@webrtc.orgf7eb75b2013-09-14 00:25:28 +000084
Miguel Casas-Sanchez47650702015-05-29 17:21:40 -070085 int32_t AddVideoFrame(const VideoFrame& videoFrame,
andresp@webrtc.orgf7eb75b2013-09-14 00:25:28 +000086 const VideoContentMetrics* _contentMetrics,
87 const CodecSpecificInfo* codecSpecificInfo);
88
89 int32_t IntraFrameRequest(int stream_index);
90 int32_t EnableFrameDropper(bool enable);
andresp@webrtc.orgf7eb75b2013-09-14 00:25:28 +000091
henrik.lundin@webrtc.orgce8e0932013-11-18 12:18:43 +000092 void SuspendBelowMinBitrate();
93 bool VideoSuspended() const;
henrik.lundin@webrtc.org572699d2013-09-30 12:16:08 +000094
pkasting@chromium.org0b1534c2014-12-15 22:09:40 +000095 int64_t TimeUntilNextProcess();
pbosa26ac922016-02-25 04:50:01 -080096 void Process();
andresp@webrtc.orgf7eb75b2013-09-14 00:25:28 +000097
98 private:
Peter Boströmdcb89982015-09-15 14:43:47 +020099 void SetEncoderParameters(EncoderParameters params)
Peter Boström233bfd22016-01-18 20:23:40 +0100100 EXCLUSIVE_LOCKS_REQUIRED(encoder_crit_);
Peter Boströmdcb89982015-09-15 14:43:47 +0200101
102 Clock* const clock_;
andresp@webrtc.orgf7eb75b2013-09-14 00:25:28 +0000103
kwiberg@webrtc.org00b8f6b2015-02-26 14:34:55 +0000104 rtc::scoped_ptr<CriticalSectionWrapper> process_crit_sect_;
pbos5ad935c2016-01-25 03:52:44 -0800105 rtc::CriticalSection encoder_crit_;
andresp@webrtc.orgf7eb75b2013-09-14 00:25:28 +0000106 VCMGenericEncoder* _encoder;
Peter Boström233bfd22016-01-18 20:23:40 +0100107 VCMEncodedFrameCallback _encodedFrameCallback GUARDED_BY(encoder_crit_);
henrik.lundin@webrtc.orgbec11ef2013-09-23 19:54:25 +0000108 media_optimization::MediaOptimization _mediaOpt;
Peter Boströmdcb89982015-09-15 14:43:47 +0200109 VCMSendStatisticsCallback* _sendStatsCallback GUARDED_BY(process_crit_sect_);
Peter Boström233bfd22016-01-18 20:23:40 +0100110 VCMCodecDataBase _codecDataBase GUARDED_BY(encoder_crit_);
111 bool frame_dropper_enabled_ GUARDED_BY(encoder_crit_);
andresp@webrtc.orgf7eb75b2013-09-14 00:25:28 +0000112 VCMProcessTimer _sendStatsTimer;
andresp@webrtc.orge682aa52013-12-19 10:59:48 +0000113
tommi@webrtc.orge07710c2015-02-19 17:43:25 +0000114 // Must be accessed on the construction thread of VideoSender.
115 VideoCodec current_codec_;
116 rtc::ThreadChecker main_thread_;
117
mflodmanfcf54bd2015-04-14 21:28:08 +0200118 VCMQMSettingsCallback* const qm_settings_callback_;
andresp@webrtc.orge682aa52013-12-19 10:59:48 +0000119 VCMProtectionCallback* protection_callback_;
Erik Språng66a641a2015-06-11 14:20:07 +0200120
Peter Boström233bfd22016-01-18 20:23:40 +0100121 rtc::CriticalSection params_crit_;
122 EncoderParameters encoder_params_ GUARDED_BY(params_crit_);
123 bool encoder_has_internal_source_ GUARDED_BY(params_crit_);
124 std::vector<FrameType> next_frame_types_ GUARDED_BY(params_crit_);
andresp@webrtc.orgf7eb75b2013-09-14 00:25:28 +0000125};
126
127class VideoReceiver {
128 public:
129 typedef VideoCodingModule::ReceiverRobustness ReceiverRobustness;
130
stefan@webrtc.org34c5da62014-04-11 14:08:35 +0000131 VideoReceiver(Clock* clock, EventFactory* event_factory);
andresp@webrtc.orgf7eb75b2013-09-14 00:25:28 +0000132 ~VideoReceiver();
133
andresp@webrtc.orgf7eb75b2013-09-14 00:25:28 +0000134 int32_t RegisterReceiveCodec(const VideoCodec* receiveCodec,
135 int32_t numberOfCores,
136 bool requireKeyFrame);
137
Peter Boström795dbe42015-11-27 14:09:07 +0100138 void RegisterExternalDecoder(VideoDecoder* externalDecoder,
perkj796cfaf2015-12-10 09:27:38 -0800139 uint8_t payloadType);
andresp@webrtc.orgf7eb75b2013-09-14 00:25:28 +0000140 int32_t RegisterReceiveCallback(VCMReceiveCallback* receiveCallback);
141 int32_t RegisterReceiveStatisticsCallback(
142 VCMReceiveStatisticsCallback* receiveStats);
fischman@webrtc.org37bb4972013-10-23 23:59:45 +0000143 int32_t RegisterDecoderTimingCallback(
144 VCMDecoderTimingCallback* decoderTiming);
andresp@webrtc.orgf7eb75b2013-09-14 00:25:28 +0000145 int32_t RegisterFrameTypeCallback(VCMFrameTypeCallback* frameTypeCallback);
146 int32_t RegisterPacketRequestCallback(VCMPacketRequestCallback* callback);
147 int RegisterRenderBufferSizeCallback(VCMRenderBufferSizeCallback* callback);
148
149 int32_t Decode(uint16_t maxWaitTimeMs);
andresp@webrtc.orgf7eb75b2013-09-14 00:25:28 +0000150
151 int32_t ReceiveCodec(VideoCodec* currentReceiveCodec) const;
152 VideoCodecType ReceiveCodec() const;
153
154 int32_t IncomingPacket(const uint8_t* incomingPayload,
pkasting@chromium.org4591fbd2014-11-20 22:28:14 +0000155 size_t payloadLength,
andresp@webrtc.orgf7eb75b2013-09-14 00:25:28 +0000156 const WebRtcRTPHeader& rtpInfo);
157 int32_t SetMinimumPlayoutDelay(uint32_t minPlayoutDelayMs);
158 int32_t SetRenderDelay(uint32_t timeMS);
159 int32_t Delay() const;
andresp@webrtc.orgf7eb75b2013-09-14 00:25:28 +0000160 uint32_t DiscardedPackets() const;
161
162 int SetReceiverRobustnessMode(ReceiverRobustness robustnessMode,
163 VCMDecodeErrorMode errorMode);
164 void SetNackSettings(size_t max_nack_list_size,
165 int max_packet_age_to_nack,
166 int max_incomplete_time_ms);
167
168 void SetDecodeErrorMode(VCMDecodeErrorMode decode_error_mode);
169 int SetMinReceiverDelay(int desired_delay_ms);
170
pkasting@chromium.org16825b12015-01-12 21:51:21 +0000171 int32_t SetReceiveChannelParameters(int64_t rtt);
andresp@webrtc.orgf7eb75b2013-09-14 00:25:28 +0000172 int32_t SetVideoProtection(VCMVideoProtection videoProtection, bool enable);
173
pkasting@chromium.org0b1534c2014-12-15 22:09:40 +0000174 int64_t TimeUntilNextProcess();
pbosa26ac922016-02-25 04:50:01 -0800175 void Process();
andresp@webrtc.orgf7eb75b2013-09-14 00:25:28 +0000176
sprang@webrtc.org40709352013-11-26 11:41:59 +0000177 void RegisterPreDecodeImageCallback(EncodedImageCallback* observer);
pbos@webrtc.org4dd40d62015-02-17 13:22:43 +0000178 void TriggerDecoderShutdown();
sprang@webrtc.org40709352013-11-26 11:41:59 +0000179
andresp@webrtc.orgf7eb75b2013-09-14 00:25:28 +0000180 protected:
pbos@webrtc.org776e6f22014-10-29 15:28:39 +0000181 int32_t Decode(const webrtc::VCMEncodedFrame& frame)
182 EXCLUSIVE_LOCKS_REQUIRED(_receiveCritSect);
andresp@webrtc.orgf7eb75b2013-09-14 00:25:28 +0000183 int32_t RequestKeyFrame();
184 int32_t RequestSliceLossIndication(const uint64_t pictureID) const;
andresp@webrtc.orgf7eb75b2013-09-14 00:25:28 +0000185
186 private:
pbos@webrtc.org20c1f562014-07-04 10:58:12 +0000187 Clock* const clock_;
kwiberg@webrtc.org00b8f6b2015-02-26 14:34:55 +0000188 rtc::scoped_ptr<CriticalSectionWrapper> process_crit_sect_;
andresp@webrtc.orgf7eb75b2013-09-14 00:25:28 +0000189 CriticalSectionWrapper* _receiveCritSect;
andresp@webrtc.orgf7eb75b2013-09-14 00:25:28 +0000190 VCMTiming _timing;
andresp@webrtc.orgf7eb75b2013-09-14 00:25:28 +0000191 VCMReceiver _receiver;
andresp@webrtc.orgf7eb75b2013-09-14 00:25:28 +0000192 VCMDecodedFrameCallback _decodedFrameCallback;
pbos@webrtc.org20c1f562014-07-04 10:58:12 +0000193 VCMFrameTypeCallback* _frameTypeCallback GUARDED_BY(process_crit_sect_);
194 VCMReceiveStatisticsCallback* _receiveStatsCallback
195 GUARDED_BY(process_crit_sect_);
196 VCMDecoderTimingCallback* _decoderTimingCallback
197 GUARDED_BY(process_crit_sect_);
198 VCMPacketRequestCallback* _packetRequestCallback
199 GUARDED_BY(process_crit_sect_);
200 VCMRenderBufferSizeCallback* render_buffer_callback_
201 GUARDED_BY(process_crit_sect_);
andresp@webrtc.orgf7eb75b2013-09-14 00:25:28 +0000202 VCMGenericDecoder* _decoder;
tommi@webrtc.orga9da4c52012-07-20 11:17:23 +0000203#ifdef DEBUG_DECODER_BIT_STREAM
andresp@webrtc.orgf7eb75b2013-09-14 00:25:28 +0000204 FILE* _bitStreamBeforeDecoder;
tommi@webrtc.orga9da4c52012-07-20 11:17:23 +0000205#endif
andresp@webrtc.orgf7eb75b2013-09-14 00:25:28 +0000206 VCMFrameBuffer _frameFromFile;
pbos@webrtc.org20c1f562014-07-04 10:58:12 +0000207 bool _scheduleKeyRequest GUARDED_BY(process_crit_sect_);
Peter Boströmed3277b2016-02-02 15:40:04 +0100208 bool drop_frames_until_keyframe_ GUARDED_BY(process_crit_sect_);
pbos@webrtc.org20c1f562014-07-04 10:58:12 +0000209 size_t max_nack_list_size_ GUARDED_BY(process_crit_sect_);
niklase@google.com470e71d2011-07-07 08:21:25 +0000210
pbos@webrtc.org776e6f22014-10-29 15:28:39 +0000211 VCMCodecDataBase _codecDataBase GUARDED_BY(_receiveCritSect);
Peter Boströmed3277b2016-02-02 15:40:04 +0100212 EncodedImageCallback* pre_decode_image_callback_ GUARDED_BY(_receiveCritSect);
213
andresp@webrtc.orgf7eb75b2013-09-14 00:25:28 +0000214 VCMProcessTimer _receiveStatsTimer;
215 VCMProcessTimer _retransmissionTimer;
216 VCMProcessTimer _keyRequestTimer;
asapersson86b01602015-10-20 23:55:26 -0700217 QpParser qp_parser_;
niklase@google.com470e71d2011-07-07 08:21:25 +0000218};
andresp@webrtc.orgf7eb75b2013-09-14 00:25:28 +0000219
220} // namespace vcm
pbos@webrtc.orgd900e8b2013-07-03 15:12:26 +0000221} // namespace webrtc
andresp@webrtc.orgf7eb75b2013-09-14 00:25:28 +0000222#endif // WEBRTC_MODULES_VIDEO_CODING_VIDEO_CODING_IMPL_H_