blob: b76c61753a5dd3396169ed433a76911a0d24e558 [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
kwiberg3f55dea2016-02-29 05:51:59 -080016#include <memory>
perkj376b1922016-05-02 11:35:24 -070017#include <string>
stefan@webrtc.orgc5300432012-10-08 07:06:53 +000018#include <vector>
19
skvlad98bb6642016-04-07 15:36:45 -070020#include "webrtc/base/onetimeevent.h"
pbos@webrtc.org38344ed2014-09-24 06:05:00 +000021#include "webrtc/base/thread_annotations.h"
perkj4e417b22016-07-14 23:35:55 -070022#include "webrtc/base/sequenced_task_checker.h"
pbosa96b60b2016-04-18 21:12:48 -070023#include "webrtc/common_video/include/frame_callback.h"
Henrik Kjellander2557b862015-11-18 22:00:21 +010024#include "webrtc/modules/video_coding/codec_database.h"
25#include "webrtc/modules/video_coding/frame_buffer.h"
26#include "webrtc/modules/video_coding/generic_decoder.h"
27#include "webrtc/modules/video_coding/generic_encoder.h"
28#include "webrtc/modules/video_coding/jitter_buffer.h"
29#include "webrtc/modules/video_coding/media_optimization.h"
30#include "webrtc/modules/video_coding/receiver.h"
31#include "webrtc/modules/video_coding/timing.h"
kjellander@webrtc.orgb7ce9642015-11-18 23:04:10 +010032#include "webrtc/modules/video_coding/utility/qp_parser.h"
Henrik Kjellander98f53512015-10-28 18:17:40 +010033#include "webrtc/system_wrappers/include/clock.h"
niklase@google.com470e71d2011-07-07 08:21:25 +000034
andresp@webrtc.orgf7eb75b2013-09-14 00:25:28 +000035namespace webrtc {
sprang@webrtc.org40709352013-11-26 11:41:59 +000036
Erik Språng08127a92016-11-16 16:41:30 +010037class VideoBitrateAllocator;
38
andresp@webrtc.orgf7eb75b2013-09-14 00:25:28 +000039namespace vcm {
niklase@google.com470e71d2011-07-07 08:21:25 +000040
andresp@webrtc.orgf7eb75b2013-09-14 00:25:28 +000041class VCMProcessTimer {
42 public:
sprang40217c32016-11-21 05:41:52 -080043 static const int64_t kDefaultProcessIntervalMs = 1000;
44
pkasting@chromium.org0b1534c2014-12-15 22:09:40 +000045 VCMProcessTimer(int64_t periodMs, Clock* clock)
andresp@webrtc.orgf7eb75b2013-09-14 00:25:28 +000046 : _clock(clock),
47 _periodMs(periodMs),
48 _latestMs(_clock->TimeInMilliseconds()) {}
pkasting@chromium.org0b1534c2014-12-15 22:09:40 +000049 int64_t Period() const;
50 int64_t TimeUntilProcess() const;
andresp@webrtc.orgf7eb75b2013-09-14 00:25:28 +000051 void Processed();
niklase@google.com470e71d2011-07-07 08:21:25 +000052
andresp@webrtc.orgf7eb75b2013-09-14 00:25:28 +000053 private:
54 Clock* _clock;
pkasting@chromium.org0b1534c2014-12-15 22:09:40 +000055 int64_t _periodMs;
andresp@webrtc.orgf7eb75b2013-09-14 00:25:28 +000056 int64_t _latestMs;
niklase@google.com470e71d2011-07-07 08:21:25 +000057};
58
Peter Boströmcd5c25c2016-04-21 16:48:08 +020059class VideoSender : public Module {
andresp@webrtc.orgf7eb75b2013-09-14 00:25:28 +000060 public:
61 typedef VideoCodingModule::SenderNackMode SenderNackMode;
62
pbos@webrtc.org891d4832015-02-26 13:15:22 +000063 VideoSender(Clock* clock,
64 EncodedImageCallback* post_encode_callback,
perkj376b1922016-05-02 11:35:24 -070065 VCMSendStatisticsCallback* send_stats_callback);
andresp@webrtc.org1df9dc32014-01-09 08:01:57 +000066
andresp@webrtc.orgf7eb75b2013-09-14 00:25:28 +000067 ~VideoSender();
68
andresp@webrtc.orgf7eb75b2013-09-14 00:25:28 +000069 // Register the send codec to be used.
tommi@webrtc.orge07710c2015-02-19 17:43:25 +000070 // This method must be called on the construction thread.
andresp@webrtc.orgf7eb75b2013-09-14 00:25:28 +000071 int32_t RegisterSendCodec(const VideoCodec* sendCodec,
72 uint32_t numberOfCores,
73 uint32_t maxPayloadSize);
tommi@webrtc.orge07710c2015-02-19 17:43:25 +000074
Peter Boström795dbe42015-11-27 14:09:07 +010075 void RegisterExternalEncoder(VideoEncoder* externalEncoder,
76 uint8_t payloadType,
77 bool internalSource);
andresp@webrtc.orgf7eb75b2013-09-14 00:25:28 +000078
andresp@webrtc.orgf7eb75b2013-09-14 00:25:28 +000079 int Bitrate(unsigned int* bitrate) const;
80 int FrameRate(unsigned int* framerate) const;
81
Erik Språng08127a92016-11-16 16:41:30 +010082 int32_t SetChannelParameters(uint32_t target_bitrate_bps,
andresp@webrtc.orgf7eb75b2013-09-14 00:25:28 +000083 uint8_t lossRate,
Erik Språng08127a92016-11-16 16:41:30 +010084 int64_t rtt,
85 VideoBitrateAllocator* bitrate_allocator);
86 // Updates the channel parameters, with a reallocated bitrate based on a
87 // presumably updated codec configuration, but does not update the encoder
88 // itself (it will be updated on the next frame).
89 void UpdateChannelParemeters(VideoBitrateAllocator* bitrate_allocator);
90
Per69b332d2016-06-02 15:45:42 +020091 // Deprecated:
92 // TODO(perkj): Remove once no projects use it.
andresp@webrtc.orgf7eb75b2013-09-14 00:25:28 +000093 int32_t RegisterProtectionCallback(VCMProtectionCallback* protection);
andresp@webrtc.orgf7eb75b2013-09-14 00:25:28 +000094
Miguel Casas-Sanchez47650702015-05-29 17:21:40 -070095 int32_t AddVideoFrame(const VideoFrame& videoFrame,
andresp@webrtc.orgf7eb75b2013-09-14 00:25:28 +000096 const CodecSpecificInfo* codecSpecificInfo);
97
perkj600246e2016-05-04 11:26:51 -070098 int32_t IntraFrameRequest(size_t stream_index);
andresp@webrtc.orgf7eb75b2013-09-14 00:25:28 +000099 int32_t EnableFrameDropper(bool enable);
andresp@webrtc.orgf7eb75b2013-09-14 00:25:28 +0000100
Peter Boströmcd5c25c2016-04-21 16:48:08 +0200101 int64_t TimeUntilNextProcess() override;
102 void Process() override;
andresp@webrtc.orgf7eb75b2013-09-14 00:25:28 +0000103
104 private:
Erik Språng08127a92016-11-16 16:41:30 +0100105 EncoderParameters UpdateEncoderParameters(
106 const EncoderParameters& params,
107 VideoBitrateAllocator* bitrate_allocator,
108 uint32_t target_bitrate_bps);
perkj57c21f92016-06-17 07:27:16 -0700109 void SetEncoderParameters(EncoderParameters params, bool has_internal_source)
Peter Boström233bfd22016-01-18 20:23:40 +0100110 EXCLUSIVE_LOCKS_REQUIRED(encoder_crit_);
Peter Boströmdcb89982015-09-15 14:43:47 +0200111
112 Clock* const clock_;
andresp@webrtc.orgf7eb75b2013-09-14 00:25:28 +0000113
pbos5ad935c2016-01-25 03:52:44 -0800114 rtc::CriticalSection encoder_crit_;
andresp@webrtc.orgf7eb75b2013-09-14 00:25:28 +0000115 VCMGenericEncoder* _encoder;
henrik.lundin@webrtc.orgbec11ef2013-09-23 19:54:25 +0000116 media_optimization::MediaOptimization _mediaOpt;
perkj376b1922016-05-02 11:35:24 -0700117 VCMEncodedFrameCallback _encodedFrameCallback GUARDED_BY(encoder_crit_);
118 VCMSendStatisticsCallback* const send_stats_callback_;
Peter Boström233bfd22016-01-18 20:23:40 +0100119 VCMCodecDataBase _codecDataBase GUARDED_BY(encoder_crit_);
120 bool frame_dropper_enabled_ GUARDED_BY(encoder_crit_);
andresp@webrtc.orgf7eb75b2013-09-14 00:25:28 +0000121 VCMProcessTimer _sendStatsTimer;
andresp@webrtc.orge682aa52013-12-19 10:59:48 +0000122
tommi@webrtc.orge07710c2015-02-19 17:43:25 +0000123 // Must be accessed on the construction thread of VideoSender.
124 VideoCodec current_codec_;
perkj4e417b22016-07-14 23:35:55 -0700125 rtc::SequencedTaskChecker sequenced_checker_;
Erik Språng66a641a2015-06-11 14:20:07 +0200126
Peter Boström233bfd22016-01-18 20:23:40 +0100127 rtc::CriticalSection params_crit_;
128 EncoderParameters encoder_params_ GUARDED_BY(params_crit_);
129 bool encoder_has_internal_source_ GUARDED_BY(params_crit_);
130 std::vector<FrameType> next_frame_types_ GUARDED_BY(params_crit_);
andresp@webrtc.orgf7eb75b2013-09-14 00:25:28 +0000131};
132
Peter Boström0b250722016-04-22 18:23:15 +0200133class VideoReceiver : public Module {
andresp@webrtc.orgf7eb75b2013-09-14 00:25:28 +0000134 public:
135 typedef VideoCodingModule::ReceiverRobustness ReceiverRobustness;
136
philipel83f831a2016-03-12 03:30:23 -0800137 VideoReceiver(Clock* clock,
138 EventFactory* event_factory,
sprang3911c262016-04-15 01:24:14 -0700139 EncodedImageCallback* pre_decode_image_callback,
philipel83f831a2016-03-12 03:30:23 -0800140 NackSender* nack_sender = nullptr,
141 KeyFrameRequestSender* keyframe_request_sender = nullptr);
andresp@webrtc.orgf7eb75b2013-09-14 00:25:28 +0000142 ~VideoReceiver();
143
andresp@webrtc.orgf7eb75b2013-09-14 00:25:28 +0000144 int32_t RegisterReceiveCodec(const VideoCodec* receiveCodec,
145 int32_t numberOfCores,
146 bool requireKeyFrame);
147
Peter Boström795dbe42015-11-27 14:09:07 +0100148 void RegisterExternalDecoder(VideoDecoder* externalDecoder,
perkj796cfaf2015-12-10 09:27:38 -0800149 uint8_t payloadType);
andresp@webrtc.orgf7eb75b2013-09-14 00:25:28 +0000150 int32_t RegisterReceiveCallback(VCMReceiveCallback* receiveCallback);
151 int32_t RegisterReceiveStatisticsCallback(
152 VCMReceiveStatisticsCallback* receiveStats);
fischman@webrtc.org37bb4972013-10-23 23:59:45 +0000153 int32_t RegisterDecoderTimingCallback(
154 VCMDecoderTimingCallback* decoderTiming);
andresp@webrtc.orgf7eb75b2013-09-14 00:25:28 +0000155 int32_t RegisterFrameTypeCallback(VCMFrameTypeCallback* frameTypeCallback);
156 int32_t RegisterPacketRequestCallback(VCMPacketRequestCallback* callback);
andresp@webrtc.orgf7eb75b2013-09-14 00:25:28 +0000157
158 int32_t Decode(uint16_t maxWaitTimeMs);
andresp@webrtc.orgf7eb75b2013-09-14 00:25:28 +0000159
philipelfd5a20f2016-11-15 00:57:57 -0800160 int32_t Decode(const webrtc::VCMEncodedFrame* frame);
161
andresp@webrtc.orgf7eb75b2013-09-14 00:25:28 +0000162 int32_t ReceiveCodec(VideoCodec* currentReceiveCodec) const;
163 VideoCodecType ReceiveCodec() const;
164
165 int32_t IncomingPacket(const uint8_t* incomingPayload,
pkasting@chromium.org4591fbd2014-11-20 22:28:14 +0000166 size_t payloadLength,
andresp@webrtc.orgf7eb75b2013-09-14 00:25:28 +0000167 const WebRtcRTPHeader& rtpInfo);
168 int32_t SetMinimumPlayoutDelay(uint32_t minPlayoutDelayMs);
169 int32_t SetRenderDelay(uint32_t timeMS);
170 int32_t Delay() const;
andresp@webrtc.orgf7eb75b2013-09-14 00:25:28 +0000171 uint32_t DiscardedPackets() const;
172
173 int SetReceiverRobustnessMode(ReceiverRobustness robustnessMode,
174 VCMDecodeErrorMode errorMode);
175 void SetNackSettings(size_t max_nack_list_size,
176 int max_packet_age_to_nack,
177 int max_incomplete_time_ms);
178
179 void SetDecodeErrorMode(VCMDecodeErrorMode decode_error_mode);
180 int SetMinReceiverDelay(int desired_delay_ms);
181
pkasting@chromium.org16825b12015-01-12 21:51:21 +0000182 int32_t SetReceiveChannelParameters(int64_t rtt);
andresp@webrtc.orgf7eb75b2013-09-14 00:25:28 +0000183 int32_t SetVideoProtection(VCMVideoProtection videoProtection, bool enable);
184
Peter Boström0b250722016-04-22 18:23:15 +0200185 int64_t TimeUntilNextProcess() override;
186 void Process() override;
andresp@webrtc.orgf7eb75b2013-09-14 00:25:28 +0000187
pbos@webrtc.org4dd40d62015-02-17 13:22:43 +0000188 void TriggerDecoderShutdown();
sprang@webrtc.org40709352013-11-26 11:41:59 +0000189
andresp@webrtc.orgf7eb75b2013-09-14 00:25:28 +0000190 protected:
pbos@webrtc.org776e6f22014-10-29 15:28:39 +0000191 int32_t Decode(const webrtc::VCMEncodedFrame& frame)
sprang3911c262016-04-15 01:24:14 -0700192 EXCLUSIVE_LOCKS_REQUIRED(receive_crit_);
andresp@webrtc.orgf7eb75b2013-09-14 00:25:28 +0000193 int32_t RequestKeyFrame();
194 int32_t RequestSliceLossIndication(const uint64_t pictureID) const;
andresp@webrtc.orgf7eb75b2013-09-14 00:25:28 +0000195
196 private:
pbos@webrtc.org20c1f562014-07-04 10:58:12 +0000197 Clock* const clock_;
sprang3911c262016-04-15 01:24:14 -0700198 rtc::CriticalSection process_crit_;
199 rtc::CriticalSection receive_crit_;
andresp@webrtc.orgf7eb75b2013-09-14 00:25:28 +0000200 VCMTiming _timing;
andresp@webrtc.orgf7eb75b2013-09-14 00:25:28 +0000201 VCMReceiver _receiver;
andresp@webrtc.orgf7eb75b2013-09-14 00:25:28 +0000202 VCMDecodedFrameCallback _decodedFrameCallback;
sprang3911c262016-04-15 01:24:14 -0700203 VCMFrameTypeCallback* _frameTypeCallback GUARDED_BY(process_crit_);
204 VCMReceiveStatisticsCallback* _receiveStatsCallback GUARDED_BY(process_crit_);
205 VCMDecoderTimingCallback* _decoderTimingCallback GUARDED_BY(process_crit_);
206 VCMPacketRequestCallback* _packetRequestCallback GUARDED_BY(process_crit_);
andresp@webrtc.orgf7eb75b2013-09-14 00:25:28 +0000207 VCMGenericDecoder* _decoder;
niklase@google.com470e71d2011-07-07 08:21:25 +0000208
sprang3911c262016-04-15 01:24:14 -0700209 VCMFrameBuffer _frameFromFile;
210 bool _scheduleKeyRequest GUARDED_BY(process_crit_);
211 bool drop_frames_until_keyframe_ GUARDED_BY(process_crit_);
212 size_t max_nack_list_size_ GUARDED_BY(process_crit_);
213
214 VCMCodecDataBase _codecDataBase GUARDED_BY(receive_crit_);
215 EncodedImageCallback* pre_decode_image_callback_;
Peter Boströmed3277b2016-02-02 15:40:04 +0100216
andresp@webrtc.orgf7eb75b2013-09-14 00:25:28 +0000217 VCMProcessTimer _receiveStatsTimer;
218 VCMProcessTimer _retransmissionTimer;
219 VCMProcessTimer _keyRequestTimer;
asapersson86b01602015-10-20 23:55:26 -0700220 QpParser qp_parser_;
skvlad98bb6642016-04-07 15:36:45 -0700221 ThreadUnsafeOneTimeEvent first_frame_received_;
niklase@google.com470e71d2011-07-07 08:21:25 +0000222};
andresp@webrtc.orgf7eb75b2013-09-14 00:25:28 +0000223
224} // namespace vcm
pbos@webrtc.orgd900e8b2013-07-03 15:12:26 +0000225} // namespace webrtc
andresp@webrtc.orgf7eb75b2013-09-14 00:25:28 +0000226#endif // WEBRTC_MODULES_VIDEO_CODING_VIDEO_CODING_IMPL_H_