blob: 1460f6abbc30baef41589afd2bc33d188854dd91 [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
Jiawei Oube142172018-10-10 01:16:20 -070016#include "api/video/builtin_video_bitrate_allocator_factory.h"
Jiawei Ou4206a0a2018-07-20 15:49:43 -070017#include "api/video/video_bitrate_allocator.h"
Mirko Bonadei71207422017-09-15 13:58:09 +020018#include "common_types.h" // NOLINT(build/include)
Mirko Bonadei92ea95e2017-09-15 06:47:31 +020019#include "common_video/libyuv/include/webrtc_libyuv.h"
Mirko Bonadei92ea95e2017-09-15 06:47:31 +020020#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 {
Stefan Holmerdbdb3a02018-07-17 16:03:46 +020031EventWrapper* EventFactoryImpl::CreateEvent() {
32 return EventWrapper::Create();
33}
34
andresp@webrtc.orgf7eb75b2013-09-14 00:25:28 +000035namespace vcm {
niklase@google.com470e71d2011-07-07 08:21:25 +000036
philipel5908c712015-12-21 08:23:20 -080037int64_t VCMProcessTimer::Period() const {
38 return _periodMs;
niklase@google.com470e71d2011-07-07 08:21:25 +000039}
40
philipel5908c712015-12-21 08:23:20 -080041int64_t VCMProcessTimer::TimeUntilProcess() const {
42 const int64_t time_since_process = _clock->TimeInMilliseconds() - _latestMs;
43 const int64_t time_until_process = _periodMs - time_since_process;
44 return std::max<int64_t>(time_until_process, 0);
niklase@google.com470e71d2011-07-07 08:21:25 +000045}
46
philipel5908c712015-12-21 08:23:20 -080047void VCMProcessTimer::Processed() {
48 _latestMs = _clock->TimeInMilliseconds();
niklase@google.com470e71d2011-07-07 08:21:25 +000049}
andresp@webrtc.orgf7eb75b2013-09-14 00:25:28 +000050} // namespace vcm
niklase@google.com470e71d2011-07-07 08:21:25 +000051
andresp@webrtc.orgf7eb75b2013-09-14 00:25:28 +000052namespace {
andresp@webrtc.org1df9dc32014-01-09 08:01:57 +000053// This wrapper provides a way to modify the callback without the need to expose
54// a register method all the way down to the function calling it.
55class EncodedImageCallbackWrapper : public EncodedImageCallback {
56 public:
kthelgasond701dfd2017-03-27 07:24:57 -070057 EncodedImageCallbackWrapper() : callback_(nullptr) {}
andresp@webrtc.org1df9dc32014-01-09 08:01:57 +000058
59 virtual ~EncodedImageCallbackWrapper() {}
60
61 void Register(EncodedImageCallback* callback) {
kthelgasond701dfd2017-03-27 07:24:57 -070062 rtc::CritScope lock(&cs_);
andresp@webrtc.org1df9dc32014-01-09 08:01:57 +000063 callback_ = callback;
64 }
65
Sergey Ulanov525df3f2016-08-02 17:46:41 -070066 virtual Result OnEncodedImage(const EncodedImage& encoded_image,
67 const CodecSpecificInfo* codec_specific_info,
68 const RTPFragmentationHeader* fragmentation) {
kthelgasond701dfd2017-03-27 07:24:57 -070069 rtc::CritScope lock(&cs_);
Sergey Ulanov525df3f2016-08-02 17:46:41 -070070 if (callback_) {
71 return callback_->OnEncodedImage(encoded_image, codec_specific_info,
72 fragmentation);
73 }
74 return Result(Result::ERROR_SEND_FAILED);
andresp@webrtc.org1df9dc32014-01-09 08:01:57 +000075 }
76
77 private:
kthelgasond701dfd2017-03-27 07:24:57 -070078 rtc::CriticalSection cs_;
danilchap56359be2017-09-07 07:53:45 -070079 EncodedImageCallback* callback_ RTC_GUARDED_BY(cs_);
andresp@webrtc.org1df9dc32014-01-09 08:01:57 +000080};
81
andresp@webrtc.orgf7eb75b2013-09-14 00:25:28 +000082class VideoCodingModuleImpl : public VideoCodingModule {
83 public:
stefan@webrtc.org34c5da62014-04-11 14:08:35 +000084 VideoCodingModuleImpl(Clock* clock,
andresp@webrtc.orgf7eb75b2013-09-14 00:25:28 +000085 EventFactory* event_factory,
philipel83f831a2016-03-12 03:30:23 -080086 NackSender* nack_sender,
Niels Möller9eb44ac2018-10-02 12:47:47 +020087 KeyFrameRequestSender* keyframe_request_sender)
andresp@webrtc.orgf7eb75b2013-09-14 00:25:28 +000088 : VideoCodingModule(),
Niels Möllera0565992017-10-24 11:37:08 +020089 sender_(clock, &post_encode_callback_),
Jiawei Oube142172018-10-10 01:16:20 -070090 rate_allocator_factory_(CreateBuiltinVideoBitrateAllocatorFactory()),
philipel721d4022016-12-15 07:10:57 -080091 timing_(new VCMTiming(clock)),
philipel83f831a2016-03-12 03:30:23 -080092 receiver_(clock,
93 event_factory,
philipel721d4022016-12-15 07:10:57 -080094 timing_.get(),
philipel83f831a2016-03-12 03:30:23 -080095 nack_sender,
Peter Boström0b250722016-04-22 18:23:15 +020096 keyframe_request_sender) {}
andresp@webrtc.orgf7eb75b2013-09-14 00:25:28 +000097
Peter Boström0b250722016-04-22 18:23:15 +020098 virtual ~VideoCodingModuleImpl() {}
andresp@webrtc.orgf7eb75b2013-09-14 00:25:28 +000099
kjellander@webrtc.org14665ff2015-03-04 12:58:35 +0000100 int64_t TimeUntilNextProcess() override {
pbos0fa9b222015-11-13 05:59:57 -0800101 int64_t receiver_time = receiver_.TimeUntilNextProcess();
tommid0a71ba2017-03-14 04:16:20 -0700102 RTC_DCHECK_GE(receiver_time, 0);
Niels Möllera0565992017-10-24 11:37:08 +0200103 return receiver_time;
andresp@webrtc.orgf7eb75b2013-09-14 00:25:28 +0000104 }
105
Yves Gerey665174f2018-06-19 15:03:05 +0200106 void Process() override { receiver_.Process(); }
andresp@webrtc.orgf7eb75b2013-09-14 00:25:28 +0000107
kjellander@webrtc.org14665ff2015-03-04 12:58:35 +0000108 int32_t RegisterSendCodec(const VideoCodec* sendCodec,
109 uint32_t numberOfCores,
110 uint32_t maxPayloadSize) override {
Sergio Garcia Murillo43800f92018-06-21 16:16:38 +0200111 if (sendCodec != nullptr && ((sendCodec->codecType == kVideoCodecVP8) ||
112 (sendCodec->codecType == kVideoCodecH264))) {
113 // Set up a rate allocator and temporal layers factory for this codec
Erik Språng08127a92016-11-16 16:41:30 +0100114 // instance. The codec impl will have a raw pointer to the TL factory,
115 // and will call it when initializing. Since this can happen
116 // asynchronously keep the instance alive until destruction or until a
117 // new send codec is registered.
Sergio Garcia Murillo43800f92018-06-21 16:16:38 +0200118 VideoCodec codec = *sendCodec;
Jiawei Oube142172018-10-10 01:16:20 -0700119 rate_allocator_ =
120 rate_allocator_factory_->CreateVideoBitrateAllocator(codec);
Sergio Garcia Murillo43800f92018-06-21 16:16:38 +0200121 return sender_.RegisterSendCodec(&codec, numberOfCores, maxPayloadSize);
Erik Språng08127a92016-11-16 16:41:30 +0100122 }
pbos0fa9b222015-11-13 05:59:57 -0800123 return sender_.RegisterSendCodec(sendCodec, numberOfCores, maxPayloadSize);
andresp@webrtc.orgf7eb75b2013-09-14 00:25:28 +0000124 }
125
kjellander@webrtc.org14665ff2015-03-04 12:58:35 +0000126 int32_t RegisterExternalEncoder(VideoEncoder* externalEncoder,
Niels Möllerbf3dbb42018-03-16 13:38:46 +0100127 uint8_t /* payloadType */,
kjellander@webrtc.org14665ff2015-03-04 12:58:35 +0000128 bool internalSource) override {
Yves Gerey665174f2018-06-19 15:03:05 +0200129 sender_.RegisterExternalEncoder(externalEncoder, internalSource);
Peter Boström795dbe42015-11-27 14:09:07 +0100130 return 0;
andresp@webrtc.orgf7eb75b2013-09-14 00:25:28 +0000131 }
132
kjellander@webrtc.org14665ff2015-03-04 12:58:35 +0000133 int32_t SetChannelParameters(uint32_t target_bitrate, // bits/s.
134 uint8_t lossRate,
135 int64_t rtt) override {
Erik Språng08127a92016-11-16 16:41:30 +0100136 return sender_.SetChannelParameters(target_bitrate, lossRate, rtt,
sprang1a646ee2016-12-01 06:34:11 -0800137 rate_allocator_.get(), nullptr);
andresp@webrtc.orgf7eb75b2013-09-14 00:25:28 +0000138 }
139
kjellander@webrtc.org14665ff2015-03-04 12:58:35 +0000140 int32_t SetVideoProtection(VCMVideoProtection videoProtection,
141 bool enable) override {
pbosba8c15b2015-07-14 09:36:34 -0700142 // TODO(pbos): Remove enable from receive-side protection modes as well.
pbos0fa9b222015-11-13 05:59:57 -0800143 return receiver_.SetVideoProtection(videoProtection, enable);
andresp@webrtc.orgf7eb75b2013-09-14 00:25:28 +0000144 }
145
Miguel Casas-Sanchez47650702015-05-29 17:21:40 -0700146 int32_t AddVideoFrame(const VideoFrame& videoFrame,
kjellander@webrtc.org14665ff2015-03-04 12:58:35 +0000147 const CodecSpecificInfo* codecSpecificInfo) override {
Peter Boströmad6fc5a2016-05-12 03:01:31 +0200148 return sender_.AddVideoFrame(videoFrame, codecSpecificInfo);
andresp@webrtc.orgf7eb75b2013-09-14 00:25:28 +0000149 }
150
perkj600246e2016-05-04 11:26:51 -0700151 int32_t IntraFrameRequest(size_t stream_index) override {
pbos0fa9b222015-11-13 05:59:57 -0800152 return sender_.IntraFrameRequest(stream_index);
andresp@webrtc.orgf7eb75b2013-09-14 00:25:28 +0000153 }
154
kjellander@webrtc.org14665ff2015-03-04 12:58:35 +0000155 int32_t EnableFrameDropper(bool enable) override {
pbos0fa9b222015-11-13 05:59:57 -0800156 return sender_.EnableFrameDropper(enable);
andresp@webrtc.orgf7eb75b2013-09-14 00:25:28 +0000157 }
158
kjellander@webrtc.org14665ff2015-03-04 12:58:35 +0000159 int32_t RegisterReceiveCodec(const VideoCodec* receiveCodec,
160 int32_t numberOfCores,
161 bool requireKeyFrame) override {
pbos0fa9b222015-11-13 05:59:57 -0800162 return receiver_.RegisterReceiveCodec(receiveCodec, numberOfCores,
163 requireKeyFrame);
andresp@webrtc.orgf7eb75b2013-09-14 00:25:28 +0000164 }
165
Anders Carlsson7eb8e9f2018-05-18 10:33:04 +0200166 void RegisterExternalDecoder(VideoDecoder* externalDecoder,
167 uint8_t payloadType) override {
168 receiver_.RegisterExternalDecoder(externalDecoder, payloadType);
169 }
170
kjellander@webrtc.org14665ff2015-03-04 12:58:35 +0000171 int32_t RegisterReceiveCallback(
172 VCMReceiveCallback* receiveCallback) override {
tommid0a71ba2017-03-14 04:16:20 -0700173 RTC_DCHECK(construction_thread_.CalledOnValidThread());
pbos0fa9b222015-11-13 05:59:57 -0800174 return receiver_.RegisterReceiveCallback(receiveCallback);
andresp@webrtc.orgf7eb75b2013-09-14 00:25:28 +0000175 }
176
kjellander@webrtc.org14665ff2015-03-04 12:58:35 +0000177 int32_t RegisterFrameTypeCallback(
178 VCMFrameTypeCallback* frameTypeCallback) override {
pbos0fa9b222015-11-13 05:59:57 -0800179 return receiver_.RegisterFrameTypeCallback(frameTypeCallback);
andresp@webrtc.orgf7eb75b2013-09-14 00:25:28 +0000180 }
181
kjellander@webrtc.org14665ff2015-03-04 12:58:35 +0000182 int32_t RegisterPacketRequestCallback(
183 VCMPacketRequestCallback* callback) override {
tommid0a71ba2017-03-14 04:16:20 -0700184 RTC_DCHECK(construction_thread_.CalledOnValidThread());
pbos0fa9b222015-11-13 05:59:57 -0800185 return receiver_.RegisterPacketRequestCallback(callback);
andresp@webrtc.orgf7eb75b2013-09-14 00:25:28 +0000186 }
187
kjellander@webrtc.org14665ff2015-03-04 12:58:35 +0000188 int32_t Decode(uint16_t maxWaitTimeMs) override {
pbos0fa9b222015-11-13 05:59:57 -0800189 return receiver_.Decode(maxWaitTimeMs);
andresp@webrtc.orgf7eb75b2013-09-14 00:25:28 +0000190 }
191
kjellander@webrtc.org14665ff2015-03-04 12:58:35 +0000192 int32_t IncomingPacket(const uint8_t* incomingPayload,
193 size_t payloadLength,
194 const WebRtcRTPHeader& rtpInfo) override {
pbos0fa9b222015-11-13 05:59:57 -0800195 return receiver_.IncomingPacket(incomingPayload, payloadLength, rtpInfo);
andresp@webrtc.orgf7eb75b2013-09-14 00:25:28 +0000196 }
197
kjellander@webrtc.org14665ff2015-03-04 12:58:35 +0000198 int SetReceiverRobustnessMode(ReceiverRobustness robustnessMode,
199 VCMDecodeErrorMode errorMode) override {
pbos0fa9b222015-11-13 05:59:57 -0800200 return receiver_.SetReceiverRobustnessMode(robustnessMode, errorMode);
andresp@webrtc.orgf7eb75b2013-09-14 00:25:28 +0000201 }
202
kjellander@webrtc.org14665ff2015-03-04 12:58:35 +0000203 void SetNackSettings(size_t max_nack_list_size,
204 int max_packet_age_to_nack,
205 int max_incomplete_time_ms) override {
pbos0fa9b222015-11-13 05:59:57 -0800206 return receiver_.SetNackSettings(max_nack_list_size, max_packet_age_to_nack,
207 max_incomplete_time_ms);
andresp@webrtc.orgf7eb75b2013-09-14 00:25:28 +0000208 }
209
kjellander@webrtc.org14665ff2015-03-04 12:58:35 +0000210 void RegisterPostEncodeImageCallback(
211 EncodedImageCallback* observer) override {
andresp@webrtc.org1df9dc32014-01-09 08:01:57 +0000212 post_encode_callback_.Register(observer);
sprang@webrtc.org40709352013-11-26 11:41:59 +0000213 }
214
andresp@webrtc.orgf7eb75b2013-09-14 00:25:28 +0000215 private:
tommid0a71ba2017-03-14 04:16:20 -0700216 rtc::ThreadChecker construction_thread_;
andresp@webrtc.org1df9dc32014-01-09 08:01:57 +0000217 EncodedImageCallbackWrapper post_encode_callback_;
pbos0fa9b222015-11-13 05:59:57 -0800218 vcm::VideoSender sender_;
Jiawei Oube142172018-10-10 01:16:20 -0700219 const std::unique_ptr<VideoBitrateAllocatorFactory> rate_allocator_factory_;
Erik Språng08127a92016-11-16 16:41:30 +0100220 std::unique_ptr<VideoBitrateAllocator> rate_allocator_;
Jiawei Oube142172018-10-10 01:16:20 -0700221 const std::unique_ptr<VCMTiming> timing_;
pbos0fa9b222015-11-13 05:59:57 -0800222 vcm::VideoReceiver receiver_;
andresp@webrtc.orgf7eb75b2013-09-14 00:25:28 +0000223};
224} // namespace
225
tommia5c18d72017-03-20 10:43:23 -0700226// DEPRECATED. Create method for current interface, will be removed when the
philipel83f831a2016-03-12 03:30:23 -0800227// new jitter buffer is in place.
philipel5908c712015-12-21 08:23:20 -0800228VideoCodingModule* VideoCodingModule::Create(Clock* clock,
229 EventFactory* event_factory) {
tommid0a71ba2017-03-14 04:16:20 -0700230 RTC_DCHECK(clock);
231 RTC_DCHECK(event_factory);
Niels Möller9eb44ac2018-10-02 12:47:47 +0200232 return new VideoCodingModuleImpl(clock, event_factory, nullptr, nullptr);
niklase@google.com470e71d2011-07-07 08:21:25 +0000233}
234
stefan@webrtc.org791eec72011-10-11 07:53:43 +0000235} // namespace webrtc