blob: 58397ad386d3221e03fb72b6dbfe0716578c83b6 [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 Ouc2ebe212018-11-08 10:02:56 -080016#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 {
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,
philipel83f831a2016-03-12 03:30:23 -080081 NackSender* nack_sender,
Niels Möller9eb44ac2018-10-02 12:47:47 +020082 KeyFrameRequestSender* keyframe_request_sender)
andresp@webrtc.orgf7eb75b2013-09-14 00:25:28 +000083 : VideoCodingModule(),
Niels Möllera0565992017-10-24 11:37:08 +020084 sender_(clock, &post_encode_callback_),
Jiawei Ouc2ebe212018-11-08 10:02:56 -080085 rate_allocator_factory_(CreateBuiltinVideoBitrateAllocatorFactory()),
philipel721d4022016-12-15 07:10:57 -080086 timing_(new VCMTiming(clock)),
Niels Möller689983f2018-11-07 16:36:22 +010087 receiver_(clock, timing_.get(), nack_sender, keyframe_request_sender) {}
andresp@webrtc.orgf7eb75b2013-09-14 00:25:28 +000088
Peter Boström0b250722016-04-22 18:23:15 +020089 virtual ~VideoCodingModuleImpl() {}
andresp@webrtc.orgf7eb75b2013-09-14 00:25:28 +000090
kjellander@webrtc.org14665ff2015-03-04 12:58:35 +000091 int64_t TimeUntilNextProcess() override {
pbos0fa9b222015-11-13 05:59:57 -080092 int64_t receiver_time = receiver_.TimeUntilNextProcess();
tommid0a71ba2017-03-14 04:16:20 -070093 RTC_DCHECK_GE(receiver_time, 0);
Niels Möllera0565992017-10-24 11:37:08 +020094 return receiver_time;
andresp@webrtc.orgf7eb75b2013-09-14 00:25:28 +000095 }
96
Yves Gerey665174f2018-06-19 15:03:05 +020097 void Process() override { receiver_.Process(); }
andresp@webrtc.orgf7eb75b2013-09-14 00:25:28 +000098
kjellander@webrtc.org14665ff2015-03-04 12:58:35 +000099 int32_t RegisterSendCodec(const VideoCodec* sendCodec,
100 uint32_t numberOfCores,
101 uint32_t maxPayloadSize) override {
Sergio Garcia Murillo43800f92018-06-21 16:16:38 +0200102 if (sendCodec != nullptr && ((sendCodec->codecType == kVideoCodecVP8) ||
103 (sendCodec->codecType == kVideoCodecH264))) {
104 // Set up a rate allocator and temporal layers factory for this codec
Erik Språng08127a92016-11-16 16:41:30 +0100105 // instance. The codec impl will have a raw pointer to the TL factory,
106 // and will call it when initializing. Since this can happen
107 // asynchronously keep the instance alive until destruction or until a
108 // new send codec is registered.
Sergio Garcia Murillo43800f92018-06-21 16:16:38 +0200109 VideoCodec codec = *sendCodec;
Jiawei Ouc2ebe212018-11-08 10:02:56 -0800110 rate_allocator_ =
111 rate_allocator_factory_->CreateVideoBitrateAllocator(codec);
Sergio Garcia Murillo43800f92018-06-21 16:16:38 +0200112 return sender_.RegisterSendCodec(&codec, numberOfCores, maxPayloadSize);
Erik Språng08127a92016-11-16 16:41:30 +0100113 }
pbos0fa9b222015-11-13 05:59:57 -0800114 return sender_.RegisterSendCodec(sendCodec, numberOfCores, maxPayloadSize);
andresp@webrtc.orgf7eb75b2013-09-14 00:25:28 +0000115 }
116
kjellander@webrtc.org14665ff2015-03-04 12:58:35 +0000117 int32_t RegisterExternalEncoder(VideoEncoder* externalEncoder,
Niels Möllerbf3dbb42018-03-16 13:38:46 +0100118 uint8_t /* payloadType */,
kjellander@webrtc.org14665ff2015-03-04 12:58:35 +0000119 bool internalSource) override {
Yves Gerey665174f2018-06-19 15:03:05 +0200120 sender_.RegisterExternalEncoder(externalEncoder, internalSource);
Peter Boström795dbe42015-11-27 14:09:07 +0100121 return 0;
andresp@webrtc.orgf7eb75b2013-09-14 00:25:28 +0000122 }
123
kjellander@webrtc.org14665ff2015-03-04 12:58:35 +0000124 int32_t SetChannelParameters(uint32_t target_bitrate, // bits/s.
125 uint8_t lossRate,
126 int64_t rtt) override {
Erik Språng08127a92016-11-16 16:41:30 +0100127 return sender_.SetChannelParameters(target_bitrate, lossRate, rtt,
sprang1a646ee2016-12-01 06:34:11 -0800128 rate_allocator_.get(), nullptr);
andresp@webrtc.orgf7eb75b2013-09-14 00:25:28 +0000129 }
130
kjellander@webrtc.org14665ff2015-03-04 12:58:35 +0000131 int32_t SetVideoProtection(VCMVideoProtection videoProtection,
132 bool enable) override {
pbosba8c15b2015-07-14 09:36:34 -0700133 // TODO(pbos): Remove enable from receive-side protection modes as well.
pbos0fa9b222015-11-13 05:59:57 -0800134 return receiver_.SetVideoProtection(videoProtection, enable);
andresp@webrtc.orgf7eb75b2013-09-14 00:25:28 +0000135 }
136
Miguel Casas-Sanchez47650702015-05-29 17:21:40 -0700137 int32_t AddVideoFrame(const VideoFrame& videoFrame,
kjellander@webrtc.org14665ff2015-03-04 12:58:35 +0000138 const CodecSpecificInfo* codecSpecificInfo) override {
Erik Språngeee39202018-11-15 17:52:43 +0100139 return sender_.AddVideoFrame(videoFrame, codecSpecificInfo, absl::nullopt);
andresp@webrtc.orgf7eb75b2013-09-14 00:25:28 +0000140 }
141
perkj600246e2016-05-04 11:26:51 -0700142 int32_t IntraFrameRequest(size_t stream_index) override {
pbos0fa9b222015-11-13 05:59:57 -0800143 return sender_.IntraFrameRequest(stream_index);
andresp@webrtc.orgf7eb75b2013-09-14 00:25:28 +0000144 }
145
kjellander@webrtc.org14665ff2015-03-04 12:58:35 +0000146 int32_t EnableFrameDropper(bool enable) override {
pbos0fa9b222015-11-13 05:59:57 -0800147 return sender_.EnableFrameDropper(enable);
andresp@webrtc.orgf7eb75b2013-09-14 00:25:28 +0000148 }
149
kjellander@webrtc.org14665ff2015-03-04 12:58:35 +0000150 int32_t RegisterReceiveCodec(const VideoCodec* receiveCodec,
151 int32_t numberOfCores,
152 bool requireKeyFrame) override {
pbos0fa9b222015-11-13 05:59:57 -0800153 return receiver_.RegisterReceiveCodec(receiveCodec, numberOfCores,
154 requireKeyFrame);
andresp@webrtc.orgf7eb75b2013-09-14 00:25:28 +0000155 }
156
Anders Carlsson7eb8e9f2018-05-18 10:33:04 +0200157 void RegisterExternalDecoder(VideoDecoder* externalDecoder,
158 uint8_t payloadType) override {
159 receiver_.RegisterExternalDecoder(externalDecoder, payloadType);
160 }
161
kjellander@webrtc.org14665ff2015-03-04 12:58:35 +0000162 int32_t RegisterReceiveCallback(
163 VCMReceiveCallback* receiveCallback) override {
tommid0a71ba2017-03-14 04:16:20 -0700164 RTC_DCHECK(construction_thread_.CalledOnValidThread());
pbos0fa9b222015-11-13 05:59:57 -0800165 return receiver_.RegisterReceiveCallback(receiveCallback);
andresp@webrtc.orgf7eb75b2013-09-14 00:25:28 +0000166 }
167
kjellander@webrtc.org14665ff2015-03-04 12:58:35 +0000168 int32_t RegisterFrameTypeCallback(
169 VCMFrameTypeCallback* frameTypeCallback) override {
pbos0fa9b222015-11-13 05:59:57 -0800170 return receiver_.RegisterFrameTypeCallback(frameTypeCallback);
andresp@webrtc.orgf7eb75b2013-09-14 00:25:28 +0000171 }
172
kjellander@webrtc.org14665ff2015-03-04 12:58:35 +0000173 int32_t RegisterPacketRequestCallback(
174 VCMPacketRequestCallback* callback) override {
tommid0a71ba2017-03-14 04:16:20 -0700175 RTC_DCHECK(construction_thread_.CalledOnValidThread());
pbos0fa9b222015-11-13 05:59:57 -0800176 return receiver_.RegisterPacketRequestCallback(callback);
andresp@webrtc.orgf7eb75b2013-09-14 00:25:28 +0000177 }
178
kjellander@webrtc.org14665ff2015-03-04 12:58:35 +0000179 int32_t Decode(uint16_t maxWaitTimeMs) override {
pbos0fa9b222015-11-13 05:59:57 -0800180 return receiver_.Decode(maxWaitTimeMs);
andresp@webrtc.orgf7eb75b2013-09-14 00:25:28 +0000181 }
182
kjellander@webrtc.org14665ff2015-03-04 12:58:35 +0000183 int32_t IncomingPacket(const uint8_t* incomingPayload,
184 size_t payloadLength,
185 const WebRtcRTPHeader& rtpInfo) override {
pbos0fa9b222015-11-13 05:59:57 -0800186 return receiver_.IncomingPacket(incomingPayload, payloadLength, rtpInfo);
andresp@webrtc.orgf7eb75b2013-09-14 00:25:28 +0000187 }
188
kjellander@webrtc.org14665ff2015-03-04 12:58:35 +0000189 int SetReceiverRobustnessMode(ReceiverRobustness robustnessMode,
190 VCMDecodeErrorMode errorMode) override {
pbos0fa9b222015-11-13 05:59:57 -0800191 return receiver_.SetReceiverRobustnessMode(robustnessMode, errorMode);
andresp@webrtc.orgf7eb75b2013-09-14 00:25:28 +0000192 }
193
kjellander@webrtc.org14665ff2015-03-04 12:58:35 +0000194 void SetNackSettings(size_t max_nack_list_size,
195 int max_packet_age_to_nack,
196 int max_incomplete_time_ms) override {
pbos0fa9b222015-11-13 05:59:57 -0800197 return receiver_.SetNackSettings(max_nack_list_size, max_packet_age_to_nack,
198 max_incomplete_time_ms);
andresp@webrtc.orgf7eb75b2013-09-14 00:25:28 +0000199 }
200
kjellander@webrtc.org14665ff2015-03-04 12:58:35 +0000201 void RegisterPostEncodeImageCallback(
202 EncodedImageCallback* observer) override {
andresp@webrtc.org1df9dc32014-01-09 08:01:57 +0000203 post_encode_callback_.Register(observer);
sprang@webrtc.org40709352013-11-26 11:41:59 +0000204 }
205
andresp@webrtc.orgf7eb75b2013-09-14 00:25:28 +0000206 private:
tommid0a71ba2017-03-14 04:16:20 -0700207 rtc::ThreadChecker construction_thread_;
andresp@webrtc.org1df9dc32014-01-09 08:01:57 +0000208 EncodedImageCallbackWrapper post_encode_callback_;
pbos0fa9b222015-11-13 05:59:57 -0800209 vcm::VideoSender sender_;
Jiawei Ouc2ebe212018-11-08 10:02:56 -0800210 const std::unique_ptr<VideoBitrateAllocatorFactory> rate_allocator_factory_;
Erik Språng08127a92016-11-16 16:41:30 +0100211 std::unique_ptr<VideoBitrateAllocator> rate_allocator_;
Jiawei Ouc2ebe212018-11-08 10:02:56 -0800212 const std::unique_ptr<VCMTiming> timing_;
pbos0fa9b222015-11-13 05:59:57 -0800213 vcm::VideoReceiver receiver_;
andresp@webrtc.orgf7eb75b2013-09-14 00:25:28 +0000214};
215} // namespace
216
tommia5c18d72017-03-20 10:43:23 -0700217// DEPRECATED. Create method for current interface, will be removed when the
philipel83f831a2016-03-12 03:30:23 -0800218// new jitter buffer is in place.
Niels Möller689983f2018-11-07 16:36:22 +0100219VideoCodingModule* VideoCodingModule::Create(Clock* clock) {
tommid0a71ba2017-03-14 04:16:20 -0700220 RTC_DCHECK(clock);
Niels Möllerc4e98252018-11-08 13:08:26 +0100221 return new VideoCodingModuleImpl(clock, nullptr, nullptr);
niklase@google.com470e71d2011-07-07 08:21:25 +0000222}
223
stefan@webrtc.org791eec72011-10-11 07:53:43 +0000224} // namespace webrtc