blob: 1127b0f34070ff055ea20b7f11edb590a17c6e81 [file] [log] [blame]
niklase@google.com470e71d2011-07-07 08:21:25 +00001/*
mflodman@webrtc.orgc80d9d92012-02-06 10:11:25 +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
Mirko Bonadei92ea95e2017-09-15 06:47:31 +020011#include "modules/video_coding/video_coding_impl.h"
philipel5908c712015-12-21 08:23:20 -080012
13#include <algorithm>
Erik Språng08127a92016-11-16 16:41:30 +010014#include <utility>
philipel5908c712015-12-21 08:23:20 -080015
Mirko Bonadei71207422017-09-15 13:58:09 +020016#include "common_types.h" // NOLINT(build/include)
Mirko Bonadei92ea95e2017-09-15 06:47:31 +020017#include "common_video/include/video_bitrate_allocator.h"
18#include "common_video/libyuv/include/webrtc_libyuv.h"
19#include "modules/video_coding/codecs/vp8/temporal_layers.h"
20#include "modules/video_coding/encoded_frame.h"
21#include "modules/video_coding/include/video_codec_initializer.h"
22#include "modules/video_coding/include/video_codec_interface.h"
23#include "modules/video_coding/jitter_buffer.h"
24#include "modules/video_coding/packet.h"
25#include "modules/video_coding/timing.h"
26#include "rtc_base/criticalsection.h"
27#include "rtc_base/thread_checker.h"
28#include "system_wrappers/include/clock.h"
niklase@google.com470e71d2011-07-07 08:21:25 +000029
agalusza@google.coma7e360e2013-08-01 03:15:08 +000030namespace webrtc {
andresp@webrtc.orgf7eb75b2013-09-14 00:25:28 +000031namespace vcm {
niklase@google.com470e71d2011-07-07 08:21:25 +000032
philipel5908c712015-12-21 08:23:20 -080033int64_t VCMProcessTimer::Period() const {
34 return _periodMs;
niklase@google.com470e71d2011-07-07 08:21:25 +000035}
36
philipel5908c712015-12-21 08:23:20 -080037int64_t VCMProcessTimer::TimeUntilProcess() const {
38 const int64_t time_since_process = _clock->TimeInMilliseconds() - _latestMs;
39 const int64_t time_until_process = _periodMs - time_since_process;
40 return std::max<int64_t>(time_until_process, 0);
niklase@google.com470e71d2011-07-07 08:21:25 +000041}
42
philipel5908c712015-12-21 08:23:20 -080043void VCMProcessTimer::Processed() {
44 _latestMs = _clock->TimeInMilliseconds();
niklase@google.com470e71d2011-07-07 08:21:25 +000045}
andresp@webrtc.orgf7eb75b2013-09-14 00:25:28 +000046} // namespace vcm
niklase@google.com470e71d2011-07-07 08:21:25 +000047
andresp@webrtc.orgf7eb75b2013-09-14 00:25:28 +000048namespace {
andresp@webrtc.org1df9dc32014-01-09 08:01:57 +000049// This wrapper provides a way to modify the callback without the need to expose
50// a register method all the way down to the function calling it.
51class EncodedImageCallbackWrapper : public EncodedImageCallback {
52 public:
kthelgasond701dfd2017-03-27 07:24:57 -070053 EncodedImageCallbackWrapper() : callback_(nullptr) {}
andresp@webrtc.org1df9dc32014-01-09 08:01:57 +000054
55 virtual ~EncodedImageCallbackWrapper() {}
56
57 void Register(EncodedImageCallback* callback) {
kthelgasond701dfd2017-03-27 07:24:57 -070058 rtc::CritScope lock(&cs_);
andresp@webrtc.org1df9dc32014-01-09 08:01:57 +000059 callback_ = callback;
60 }
61
Sergey Ulanov525df3f2016-08-02 17:46:41 -070062 virtual Result OnEncodedImage(const EncodedImage& encoded_image,
63 const CodecSpecificInfo* codec_specific_info,
64 const RTPFragmentationHeader* fragmentation) {
kthelgasond701dfd2017-03-27 07:24:57 -070065 rtc::CritScope lock(&cs_);
Sergey Ulanov525df3f2016-08-02 17:46:41 -070066 if (callback_) {
67 return callback_->OnEncodedImage(encoded_image, codec_specific_info,
68 fragmentation);
69 }
70 return Result(Result::ERROR_SEND_FAILED);
andresp@webrtc.org1df9dc32014-01-09 08:01:57 +000071 }
72
73 private:
kthelgasond701dfd2017-03-27 07:24:57 -070074 rtc::CriticalSection cs_;
danilchap56359be2017-09-07 07:53:45 -070075 EncodedImageCallback* callback_ RTC_GUARDED_BY(cs_);
andresp@webrtc.org1df9dc32014-01-09 08:01:57 +000076};
77
andresp@webrtc.orgf7eb75b2013-09-14 00:25:28 +000078class VideoCodingModuleImpl : public VideoCodingModule {
79 public:
stefan@webrtc.org34c5da62014-04-11 14:08:35 +000080 VideoCodingModuleImpl(Clock* clock,
andresp@webrtc.orgf7eb75b2013-09-14 00:25:28 +000081 EventFactory* event_factory,
philipel83f831a2016-03-12 03:30:23 -080082 NackSender* nack_sender,
sprang3911c262016-04-15 01:24:14 -070083 KeyFrameRequestSender* keyframe_request_sender,
84 EncodedImageCallback* pre_decode_image_callback)
andresp@webrtc.orgf7eb75b2013-09-14 00:25:28 +000085 : VideoCodingModule(),
Niels Möllera0565992017-10-24 11:37:08 +020086 sender_(clock, &post_encode_callback_),
philipel721d4022016-12-15 07:10:57 -080087 timing_(new VCMTiming(clock)),
philipel83f831a2016-03-12 03:30:23 -080088 receiver_(clock,
89 event_factory,
sprang3911c262016-04-15 01:24:14 -070090 pre_decode_image_callback,
philipel721d4022016-12-15 07:10:57 -080091 timing_.get(),
philipel83f831a2016-03-12 03:30:23 -080092 nack_sender,
Peter Boström0b250722016-04-22 18:23:15 +020093 keyframe_request_sender) {}
andresp@webrtc.orgf7eb75b2013-09-14 00:25:28 +000094
Peter Boström0b250722016-04-22 18:23:15 +020095 virtual ~VideoCodingModuleImpl() {}
andresp@webrtc.orgf7eb75b2013-09-14 00:25:28 +000096
kjellander@webrtc.org14665ff2015-03-04 12:58:35 +000097 int64_t TimeUntilNextProcess() override {
pbos0fa9b222015-11-13 05:59:57 -080098 int64_t receiver_time = receiver_.TimeUntilNextProcess();
tommid0a71ba2017-03-14 04:16:20 -070099 RTC_DCHECK_GE(receiver_time, 0);
Niels Möllera0565992017-10-24 11:37:08 +0200100 return receiver_time;
andresp@webrtc.orgf7eb75b2013-09-14 00:25:28 +0000101 }
102
Yves Gerey665174f2018-06-19 15:03:05 +0200103 void Process() override { receiver_.Process(); }
andresp@webrtc.orgf7eb75b2013-09-14 00:25:28 +0000104
kjellander@webrtc.org14665ff2015-03-04 12:58:35 +0000105 int32_t RegisterSendCodec(const VideoCodec* sendCodec,
106 uint32_t numberOfCores,
107 uint32_t maxPayloadSize) override {
Erik Språng08127a92016-11-16 16:41:30 +0100108 if (sendCodec != nullptr && sendCodec->codecType == kVideoCodecVP8) {
109 // Set up a rate allocator and temporal layers factory for this vp8
110 // instance. The codec impl will have a raw pointer to the TL factory,
111 // and will call it when initializing. Since this can happen
112 // asynchronously keep the instance alive until destruction or until a
113 // new send codec is registered.
114 VideoCodec vp8_codec = *sendCodec;
Erik Språng82fad3d2018-03-21 09:57:23 +0100115 rate_allocator_ =
116 VideoCodecInitializer::CreateBitrateAllocator(vp8_codec);
Erik Språng08127a92016-11-16 16:41:30 +0100117 return sender_.RegisterSendCodec(&vp8_codec, numberOfCores,
118 maxPayloadSize);
119 }
pbos0fa9b222015-11-13 05:59:57 -0800120 return sender_.RegisterSendCodec(sendCodec, numberOfCores, maxPayloadSize);
andresp@webrtc.orgf7eb75b2013-09-14 00:25:28 +0000121 }
122
kjellander@webrtc.org14665ff2015-03-04 12:58:35 +0000123 int32_t RegisterExternalEncoder(VideoEncoder* externalEncoder,
Niels Möllerbf3dbb42018-03-16 13:38:46 +0100124 uint8_t /* payloadType */,
kjellander@webrtc.org14665ff2015-03-04 12:58:35 +0000125 bool internalSource) override {
Yves Gerey665174f2018-06-19 15:03:05 +0200126 sender_.RegisterExternalEncoder(externalEncoder, internalSource);
Peter Boström795dbe42015-11-27 14:09:07 +0100127 return 0;
andresp@webrtc.orgf7eb75b2013-09-14 00:25:28 +0000128 }
129
kjellander@webrtc.org14665ff2015-03-04 12:58:35 +0000130 int32_t SetChannelParameters(uint32_t target_bitrate, // bits/s.
131 uint8_t lossRate,
132 int64_t rtt) override {
Erik Språng08127a92016-11-16 16:41:30 +0100133 return sender_.SetChannelParameters(target_bitrate, lossRate, rtt,
sprang1a646ee2016-12-01 06:34:11 -0800134 rate_allocator_.get(), nullptr);
andresp@webrtc.orgf7eb75b2013-09-14 00:25:28 +0000135 }
136
kjellander@webrtc.org14665ff2015-03-04 12:58:35 +0000137 int32_t SetVideoProtection(VCMVideoProtection videoProtection,
138 bool enable) override {
pbosba8c15b2015-07-14 09:36:34 -0700139 // TODO(pbos): Remove enable from receive-side protection modes as well.
pbos0fa9b222015-11-13 05:59:57 -0800140 return receiver_.SetVideoProtection(videoProtection, enable);
andresp@webrtc.orgf7eb75b2013-09-14 00:25:28 +0000141 }
142
Miguel Casas-Sanchez47650702015-05-29 17:21:40 -0700143 int32_t AddVideoFrame(const VideoFrame& videoFrame,
kjellander@webrtc.org14665ff2015-03-04 12:58:35 +0000144 const CodecSpecificInfo* codecSpecificInfo) override {
Peter Boströmad6fc5a2016-05-12 03:01:31 +0200145 return sender_.AddVideoFrame(videoFrame, codecSpecificInfo);
andresp@webrtc.orgf7eb75b2013-09-14 00:25:28 +0000146 }
147
perkj600246e2016-05-04 11:26:51 -0700148 int32_t IntraFrameRequest(size_t stream_index) override {
pbos0fa9b222015-11-13 05:59:57 -0800149 return sender_.IntraFrameRequest(stream_index);
andresp@webrtc.orgf7eb75b2013-09-14 00:25:28 +0000150 }
151
kjellander@webrtc.org14665ff2015-03-04 12:58:35 +0000152 int32_t EnableFrameDropper(bool enable) override {
pbos0fa9b222015-11-13 05:59:57 -0800153 return sender_.EnableFrameDropper(enable);
andresp@webrtc.orgf7eb75b2013-09-14 00:25:28 +0000154 }
155
kjellander@webrtc.org14665ff2015-03-04 12:58:35 +0000156 int32_t RegisterReceiveCodec(const VideoCodec* receiveCodec,
157 int32_t numberOfCores,
158 bool requireKeyFrame) override {
pbos0fa9b222015-11-13 05:59:57 -0800159 return receiver_.RegisterReceiveCodec(receiveCodec, numberOfCores,
160 requireKeyFrame);
andresp@webrtc.orgf7eb75b2013-09-14 00:25:28 +0000161 }
162
Anders Carlsson7eb8e9f2018-05-18 10:33:04 +0200163 void RegisterExternalDecoder(VideoDecoder* externalDecoder,
164 uint8_t payloadType) override {
165 receiver_.RegisterExternalDecoder(externalDecoder, payloadType);
166 }
167
kjellander@webrtc.org14665ff2015-03-04 12:58:35 +0000168 int32_t RegisterReceiveCallback(
169 VCMReceiveCallback* receiveCallback) override {
tommid0a71ba2017-03-14 04:16:20 -0700170 RTC_DCHECK(construction_thread_.CalledOnValidThread());
pbos0fa9b222015-11-13 05:59:57 -0800171 return receiver_.RegisterReceiveCallback(receiveCallback);
andresp@webrtc.orgf7eb75b2013-09-14 00:25:28 +0000172 }
173
kjellander@webrtc.org14665ff2015-03-04 12:58:35 +0000174 int32_t RegisterFrameTypeCallback(
175 VCMFrameTypeCallback* frameTypeCallback) override {
pbos0fa9b222015-11-13 05:59:57 -0800176 return receiver_.RegisterFrameTypeCallback(frameTypeCallback);
andresp@webrtc.orgf7eb75b2013-09-14 00:25:28 +0000177 }
178
kjellander@webrtc.org14665ff2015-03-04 12:58:35 +0000179 int32_t RegisterPacketRequestCallback(
180 VCMPacketRequestCallback* callback) override {
tommid0a71ba2017-03-14 04:16:20 -0700181 RTC_DCHECK(construction_thread_.CalledOnValidThread());
pbos0fa9b222015-11-13 05:59:57 -0800182 return receiver_.RegisterPacketRequestCallback(callback);
andresp@webrtc.orgf7eb75b2013-09-14 00:25:28 +0000183 }
184
kjellander@webrtc.org14665ff2015-03-04 12:58:35 +0000185 int32_t Decode(uint16_t maxWaitTimeMs) override {
pbos0fa9b222015-11-13 05:59:57 -0800186 return receiver_.Decode(maxWaitTimeMs);
andresp@webrtc.orgf7eb75b2013-09-14 00:25:28 +0000187 }
188
kjellander@webrtc.org14665ff2015-03-04 12:58:35 +0000189 int32_t IncomingPacket(const uint8_t* incomingPayload,
190 size_t payloadLength,
191 const WebRtcRTPHeader& rtpInfo) override {
pbos0fa9b222015-11-13 05:59:57 -0800192 return receiver_.IncomingPacket(incomingPayload, payloadLength, rtpInfo);
andresp@webrtc.orgf7eb75b2013-09-14 00:25:28 +0000193 }
194
kjellander@webrtc.org14665ff2015-03-04 12:58:35 +0000195 int SetReceiverRobustnessMode(ReceiverRobustness robustnessMode,
196 VCMDecodeErrorMode errorMode) override {
pbos0fa9b222015-11-13 05:59:57 -0800197 return receiver_.SetReceiverRobustnessMode(robustnessMode, errorMode);
andresp@webrtc.orgf7eb75b2013-09-14 00:25:28 +0000198 }
199
kjellander@webrtc.org14665ff2015-03-04 12:58:35 +0000200 void SetNackSettings(size_t max_nack_list_size,
201 int max_packet_age_to_nack,
202 int max_incomplete_time_ms) override {
pbos0fa9b222015-11-13 05:59:57 -0800203 return receiver_.SetNackSettings(max_nack_list_size, max_packet_age_to_nack,
204 max_incomplete_time_ms);
andresp@webrtc.orgf7eb75b2013-09-14 00:25:28 +0000205 }
206
kjellander@webrtc.org14665ff2015-03-04 12:58:35 +0000207 void RegisterPostEncodeImageCallback(
208 EncodedImageCallback* observer) override {
andresp@webrtc.org1df9dc32014-01-09 08:01:57 +0000209 post_encode_callback_.Register(observer);
sprang@webrtc.org40709352013-11-26 11:41:59 +0000210 }
211
andresp@webrtc.orgf7eb75b2013-09-14 00:25:28 +0000212 private:
tommid0a71ba2017-03-14 04:16:20 -0700213 rtc::ThreadChecker construction_thread_;
andresp@webrtc.org1df9dc32014-01-09 08:01:57 +0000214 EncodedImageCallbackWrapper post_encode_callback_;
pbos0fa9b222015-11-13 05:59:57 -0800215 vcm::VideoSender sender_;
Erik Språng08127a92016-11-16 16:41:30 +0100216 std::unique_ptr<VideoBitrateAllocator> rate_allocator_;
philipel721d4022016-12-15 07:10:57 -0800217 std::unique_ptr<VCMTiming> timing_;
pbos0fa9b222015-11-13 05:59:57 -0800218 vcm::VideoReceiver receiver_;
andresp@webrtc.orgf7eb75b2013-09-14 00:25:28 +0000219};
220} // namespace
221
tommia5c18d72017-03-20 10:43:23 -0700222// DEPRECATED. Create method for current interface, will be removed when the
philipel83f831a2016-03-12 03:30:23 -0800223// new jitter buffer is in place.
philipel5908c712015-12-21 08:23:20 -0800224VideoCodingModule* VideoCodingModule::Create(Clock* clock,
225 EventFactory* event_factory) {
tommid0a71ba2017-03-14 04:16:20 -0700226 RTC_DCHECK(clock);
227 RTC_DCHECK(event_factory);
tommia5c18d72017-03-20 10:43:23 -0700228 return new VideoCodingModuleImpl(clock, event_factory, nullptr, nullptr,
229 nullptr);
niklase@google.com470e71d2011-07-07 08:21:25 +0000230}
231
stefan@webrtc.org791eec72011-10-11 07:53:43 +0000232} // namespace webrtc