blob: 38fe9ed710b55688e5cc5fb6d6e6d7c42f561ed8 [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
34class EncodedFrameObserver;
35
andresp@webrtc.orgf7eb75b2013-09-14 00:25:28 +000036namespace vcm {
niklase@google.com470e71d2011-07-07 08:21:25 +000037
andresp@webrtc.orgf7eb75b2013-09-14 00:25:28 +000038class VCMProcessTimer {
39 public:
pkasting@chromium.org0b1534c2014-12-15 22:09:40 +000040 VCMProcessTimer(int64_t periodMs, Clock* clock)
andresp@webrtc.orgf7eb75b2013-09-14 00:25:28 +000041 : _clock(clock),
42 _periodMs(periodMs),
43 _latestMs(_clock->TimeInMilliseconds()) {}
pkasting@chromium.org0b1534c2014-12-15 22:09:40 +000044 int64_t Period() const;
45 int64_t TimeUntilProcess() const;
andresp@webrtc.orgf7eb75b2013-09-14 00:25:28 +000046 void Processed();
niklase@google.com470e71d2011-07-07 08:21:25 +000047
andresp@webrtc.orgf7eb75b2013-09-14 00:25:28 +000048 private:
49 Clock* _clock;
pkasting@chromium.org0b1534c2014-12-15 22:09:40 +000050 int64_t _periodMs;
andresp@webrtc.orgf7eb75b2013-09-14 00:25:28 +000051 int64_t _latestMs;
niklase@google.com470e71d2011-07-07 08:21:25 +000052};
53
andresp@webrtc.orgf7eb75b2013-09-14 00:25:28 +000054class VideoSender {
55 public:
56 typedef VideoCodingModule::SenderNackMode SenderNackMode;
57
pbos@webrtc.org891d4832015-02-26 13:15:22 +000058 VideoSender(Clock* clock,
59 EncodedImageCallback* post_encode_callback,
mflodmanfcf54bd2015-04-14 21:28:08 +020060 VideoEncoderRateObserver* encoder_rate_observer,
61 VCMQMSettingsCallback* qm_settings_callback);
andresp@webrtc.org1df9dc32014-01-09 08:01:57 +000062
andresp@webrtc.orgf7eb75b2013-09-14 00:25:28 +000063 ~VideoSender();
64
andresp@webrtc.orgf7eb75b2013-09-14 00:25:28 +000065 // Register the send codec to be used.
tommi@webrtc.orge07710c2015-02-19 17:43:25 +000066 // This method must be called on the construction thread.
andresp@webrtc.orgf7eb75b2013-09-14 00:25:28 +000067 int32_t RegisterSendCodec(const VideoCodec* sendCodec,
68 uint32_t numberOfCores,
69 uint32_t maxPayloadSize);
tommi@webrtc.orge07710c2015-02-19 17:43:25 +000070 // Non-blocking access to the currently active send codec configuration.
71 // Must be called from the same thread as the VideoSender instance was
72 // created on.
73 const VideoCodec& GetSendCodec() const;
andresp@webrtc.orgf7eb75b2013-09-14 00:25:28 +000074
tommi@webrtc.orge07710c2015-02-19 17:43:25 +000075 // Get a copy of the currently configured send codec.
76 // This method acquires a lock to copy the current configuration out,
77 // so it can block and the returned information is not guaranteed to be
78 // accurate upon return. Consider using GetSendCodec() instead and make
79 // decisions on that thread with regards to the current codec.
80 int32_t SendCodecBlocking(VideoCodec* currentSendCodec) const;
81
82 // Same as SendCodecBlocking. Try to use GetSendCodec() instead.
83 VideoCodecType SendCodecBlocking() const;
84
andresp@webrtc.orgf7eb75b2013-09-14 00:25:28 +000085 int32_t RegisterExternalEncoder(VideoEncoder* externalEncoder,
86 uint8_t payloadType,
87 bool internalSource);
88
andresp@webrtc.orgf7eb75b2013-09-14 00:25:28 +000089 int Bitrate(unsigned int* bitrate) const;
90 int FrameRate(unsigned int* framerate) const;
91
92 int32_t SetChannelParameters(uint32_t target_bitrate, // bits/s.
93 uint8_t lossRate,
pkasting@chromium.org16825b12015-01-12 21:51:21 +000094 int64_t rtt);
andresp@webrtc.orgf7eb75b2013-09-14 00:25:28 +000095
96 int32_t RegisterTransportCallback(VCMPacketizationCallback* transport);
97 int32_t RegisterSendStatisticsCallback(VCMSendStatisticsCallback* sendStats);
andresp@webrtc.orgf7eb75b2013-09-14 00:25:28 +000098 int32_t RegisterProtectionCallback(VCMProtectionCallback* protection);
pbosba8c15b2015-07-14 09:36:34 -070099 void SetVideoProtection(VCMVideoProtection videoProtection);
andresp@webrtc.orgf7eb75b2013-09-14 00:25:28 +0000100
Miguel Casas-Sanchez47650702015-05-29 17:21:40 -0700101 int32_t AddVideoFrame(const VideoFrame& videoFrame,
andresp@webrtc.orgf7eb75b2013-09-14 00:25:28 +0000102 const VideoContentMetrics* _contentMetrics,
103 const CodecSpecificInfo* codecSpecificInfo);
104
105 int32_t IntraFrameRequest(int stream_index);
106 int32_t EnableFrameDropper(bool enable);
andresp@webrtc.orgf7eb75b2013-09-14 00:25:28 +0000107
henrik.lundin@webrtc.orgce8e0932013-11-18 12:18:43 +0000108 void SuspendBelowMinBitrate();
109 bool VideoSuspended() const;
henrik.lundin@webrtc.org572699d2013-09-30 12:16:08 +0000110
pkasting@chromium.org0b1534c2014-12-15 22:09:40 +0000111 int64_t TimeUntilNextProcess();
andresp@webrtc.orgf7eb75b2013-09-14 00:25:28 +0000112 int32_t Process();
113
114 private:
Peter Boströmdcb89982015-09-15 14:43:47 +0200115 void SetEncoderParameters(EncoderParameters params)
116 EXCLUSIVE_LOCKS_REQUIRED(send_crit_);
117
118 Clock* const clock_;
andresp@webrtc.orgf7eb75b2013-09-14 00:25:28 +0000119
kwiberg@webrtc.org00b8f6b2015-02-26 14:34:55 +0000120 rtc::scoped_ptr<CriticalSectionWrapper> process_crit_sect_;
Peter Boströmdcb89982015-09-15 14:43:47 +0200121 mutable rtc::CriticalSection send_crit_;
andresp@webrtc.orgf7eb75b2013-09-14 00:25:28 +0000122 VCMGenericEncoder* _encoder;
123 VCMEncodedFrameCallback _encodedFrameCallback;
124 std::vector<FrameType> _nextFrameTypes;
henrik.lundin@webrtc.orgbec11ef2013-09-23 19:54:25 +0000125 media_optimization::MediaOptimization _mediaOpt;
Peter Boströmdcb89982015-09-15 14:43:47 +0200126 VCMSendStatisticsCallback* _sendStatsCallback GUARDED_BY(process_crit_sect_);
127 VCMCodecDataBase _codecDataBase GUARDED_BY(send_crit_);
128 bool frame_dropper_enabled_ GUARDED_BY(send_crit_);
andresp@webrtc.orgf7eb75b2013-09-14 00:25:28 +0000129 VCMProcessTimer _sendStatsTimer;
andresp@webrtc.orge682aa52013-12-19 10:59:48 +0000130
tommi@webrtc.orge07710c2015-02-19 17:43:25 +0000131 // Must be accessed on the construction thread of VideoSender.
132 VideoCodec current_codec_;
133 rtc::ThreadChecker main_thread_;
134
mflodmanfcf54bd2015-04-14 21:28:08 +0200135 VCMQMSettingsCallback* const qm_settings_callback_;
andresp@webrtc.orge682aa52013-12-19 10:59:48 +0000136 VCMProtectionCallback* protection_callback_;
Erik SprĂ¥ng66a641a2015-06-11 14:20:07 +0200137
138 rtc::CriticalSection params_lock_;
Peter Boströmdcb89982015-09-15 14:43:47 +0200139 EncoderParameters encoder_params_ GUARDED_BY(params_lock_);
andresp@webrtc.orgf7eb75b2013-09-14 00:25:28 +0000140};
141
142class VideoReceiver {
143 public:
144 typedef VideoCodingModule::ReceiverRobustness ReceiverRobustness;
145
stefan@webrtc.org34c5da62014-04-11 14:08:35 +0000146 VideoReceiver(Clock* clock, EventFactory* event_factory);
andresp@webrtc.orgf7eb75b2013-09-14 00:25:28 +0000147 ~VideoReceiver();
148
andresp@webrtc.orgf7eb75b2013-09-14 00:25:28 +0000149 int32_t RegisterReceiveCodec(const VideoCodec* receiveCodec,
150 int32_t numberOfCores,
151 bool requireKeyFrame);
152
153 int32_t RegisterExternalDecoder(VideoDecoder* externalDecoder,
154 uint8_t payloadType,
155 bool internalRenderTiming);
156 int32_t RegisterReceiveCallback(VCMReceiveCallback* receiveCallback);
157 int32_t RegisterReceiveStatisticsCallback(
158 VCMReceiveStatisticsCallback* receiveStats);
fischman@webrtc.org37bb4972013-10-23 23:59:45 +0000159 int32_t RegisterDecoderTimingCallback(
160 VCMDecoderTimingCallback* decoderTiming);
andresp@webrtc.orgf7eb75b2013-09-14 00:25:28 +0000161 int32_t RegisterFrameTypeCallback(VCMFrameTypeCallback* frameTypeCallback);
162 int32_t RegisterPacketRequestCallback(VCMPacketRequestCallback* callback);
163 int RegisterRenderBufferSizeCallback(VCMRenderBufferSizeCallback* callback);
164
165 int32_t Decode(uint16_t maxWaitTimeMs);
andresp@webrtc.orgf7eb75b2013-09-14 00:25:28 +0000166 int32_t ResetDecoder();
167
168 int32_t ReceiveCodec(VideoCodec* currentReceiveCodec) const;
169 VideoCodecType ReceiveCodec() const;
170
171 int32_t IncomingPacket(const uint8_t* incomingPayload,
pkasting@chromium.org4591fbd2014-11-20 22:28:14 +0000172 size_t payloadLength,
andresp@webrtc.orgf7eb75b2013-09-14 00:25:28 +0000173 const WebRtcRTPHeader& rtpInfo);
174 int32_t SetMinimumPlayoutDelay(uint32_t minPlayoutDelayMs);
175 int32_t SetRenderDelay(uint32_t timeMS);
176 int32_t Delay() const;
andresp@webrtc.orgf7eb75b2013-09-14 00:25:28 +0000177 uint32_t DiscardedPackets() const;
178
179 int SetReceiverRobustnessMode(ReceiverRobustness robustnessMode,
180 VCMDecodeErrorMode errorMode);
181 void SetNackSettings(size_t max_nack_list_size,
182 int max_packet_age_to_nack,
183 int max_incomplete_time_ms);
184
185 void SetDecodeErrorMode(VCMDecodeErrorMode decode_error_mode);
186 int SetMinReceiverDelay(int desired_delay_ms);
187
pkasting@chromium.org16825b12015-01-12 21:51:21 +0000188 int32_t SetReceiveChannelParameters(int64_t rtt);
andresp@webrtc.orgf7eb75b2013-09-14 00:25:28 +0000189 int32_t SetVideoProtection(VCMVideoProtection videoProtection, bool enable);
190
pkasting@chromium.org0b1534c2014-12-15 22:09:40 +0000191 int64_t TimeUntilNextProcess();
andresp@webrtc.orgf7eb75b2013-09-14 00:25:28 +0000192 int32_t Process();
193
sprang@webrtc.org40709352013-11-26 11:41:59 +0000194 void RegisterPreDecodeImageCallback(EncodedImageCallback* observer);
pbos@webrtc.org4dd40d62015-02-17 13:22:43 +0000195 void TriggerDecoderShutdown();
sprang@webrtc.org40709352013-11-26 11:41:59 +0000196
andresp@webrtc.orgf7eb75b2013-09-14 00:25:28 +0000197 protected:
pbos@webrtc.org776e6f22014-10-29 15:28:39 +0000198 int32_t Decode(const webrtc::VCMEncodedFrame& frame)
199 EXCLUSIVE_LOCKS_REQUIRED(_receiveCritSect);
andresp@webrtc.orgf7eb75b2013-09-14 00:25:28 +0000200 int32_t RequestKeyFrame();
201 int32_t RequestSliceLossIndication(const uint64_t pictureID) const;
andresp@webrtc.orgf7eb75b2013-09-14 00:25:28 +0000202
203 private:
pbos@webrtc.org20c1f562014-07-04 10:58:12 +0000204 Clock* const clock_;
kwiberg@webrtc.org00b8f6b2015-02-26 14:34:55 +0000205 rtc::scoped_ptr<CriticalSectionWrapper> process_crit_sect_;
andresp@webrtc.orgf7eb75b2013-09-14 00:25:28 +0000206 CriticalSectionWrapper* _receiveCritSect;
andresp@webrtc.orgf7eb75b2013-09-14 00:25:28 +0000207 VCMTiming _timing;
andresp@webrtc.orgf7eb75b2013-09-14 00:25:28 +0000208 VCMReceiver _receiver;
andresp@webrtc.orgf7eb75b2013-09-14 00:25:28 +0000209 VCMDecodedFrameCallback _decodedFrameCallback;
pbos@webrtc.org20c1f562014-07-04 10:58:12 +0000210 VCMFrameTypeCallback* _frameTypeCallback GUARDED_BY(process_crit_sect_);
211 VCMReceiveStatisticsCallback* _receiveStatsCallback
212 GUARDED_BY(process_crit_sect_);
213 VCMDecoderTimingCallback* _decoderTimingCallback
214 GUARDED_BY(process_crit_sect_);
215 VCMPacketRequestCallback* _packetRequestCallback
216 GUARDED_BY(process_crit_sect_);
217 VCMRenderBufferSizeCallback* render_buffer_callback_
218 GUARDED_BY(process_crit_sect_);
andresp@webrtc.orgf7eb75b2013-09-14 00:25:28 +0000219 VCMGenericDecoder* _decoder;
tommi@webrtc.orga9da4c52012-07-20 11:17:23 +0000220#ifdef DEBUG_DECODER_BIT_STREAM
andresp@webrtc.orgf7eb75b2013-09-14 00:25:28 +0000221 FILE* _bitStreamBeforeDecoder;
tommi@webrtc.orga9da4c52012-07-20 11:17:23 +0000222#endif
andresp@webrtc.orgf7eb75b2013-09-14 00:25:28 +0000223 VCMFrameBuffer _frameFromFile;
pbos@webrtc.org20c1f562014-07-04 10:58:12 +0000224 bool _scheduleKeyRequest GUARDED_BY(process_crit_sect_);
225 size_t max_nack_list_size_ GUARDED_BY(process_crit_sect_);
226 EncodedImageCallback* pre_decode_image_callback_ GUARDED_BY(_receiveCritSect);
niklase@google.com470e71d2011-07-07 08:21:25 +0000227
pbos@webrtc.org776e6f22014-10-29 15:28:39 +0000228 VCMCodecDataBase _codecDataBase GUARDED_BY(_receiveCritSect);
andresp@webrtc.orgf7eb75b2013-09-14 00:25:28 +0000229 VCMProcessTimer _receiveStatsTimer;
230 VCMProcessTimer _retransmissionTimer;
231 VCMProcessTimer _keyRequestTimer;
asapersson86b01602015-10-20 23:55:26 -0700232 QpParser qp_parser_;
niklase@google.com470e71d2011-07-07 08:21:25 +0000233};
andresp@webrtc.orgf7eb75b2013-09-14 00:25:28 +0000234
235} // namespace vcm
pbos@webrtc.orgd900e8b2013-07-03 15:12:26 +0000236} // namespace webrtc
andresp@webrtc.orgf7eb75b2013-09-14 00:25:28 +0000237#endif // WEBRTC_MODULES_VIDEO_CODING_VIDEO_CODING_IMPL_H_