blob: aa9a0d5bd5abe49fee19464467e88d9218a804e7 [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"
Mirko Bonadei92ea95e2017-09-15 06:47:31 +020019#include "modules/video_coding/encoded_frame.h"
20#include "modules/video_coding/include/video_codec_initializer.h"
21#include "modules/video_coding/include/video_codec_interface.h"
22#include "modules/video_coding/jitter_buffer.h"
23#include "modules/video_coding/packet.h"
24#include "modules/video_coding/timing.h"
25#include "rtc_base/criticalsection.h"
26#include "rtc_base/thread_checker.h"
27#include "system_wrappers/include/clock.h"
niklase@google.com470e71d2011-07-07 08:21:25 +000028
agalusza@google.coma7e360e2013-08-01 03:15:08 +000029namespace webrtc {
andresp@webrtc.orgf7eb75b2013-09-14 00:25:28 +000030namespace vcm {
niklase@google.com470e71d2011-07-07 08:21:25 +000031
philipel5908c712015-12-21 08:23:20 -080032int64_t VCMProcessTimer::Period() const {
33 return _periodMs;
niklase@google.com470e71d2011-07-07 08:21:25 +000034}
35
philipel5908c712015-12-21 08:23:20 -080036int64_t VCMProcessTimer::TimeUntilProcess() const {
37 const int64_t time_since_process = _clock->TimeInMilliseconds() - _latestMs;
38 const int64_t time_until_process = _periodMs - time_since_process;
39 return std::max<int64_t>(time_until_process, 0);
niklase@google.com470e71d2011-07-07 08:21:25 +000040}
41
philipel5908c712015-12-21 08:23:20 -080042void VCMProcessTimer::Processed() {
43 _latestMs = _clock->TimeInMilliseconds();
niklase@google.com470e71d2011-07-07 08:21:25 +000044}
andresp@webrtc.orgf7eb75b2013-09-14 00:25:28 +000045} // namespace vcm
niklase@google.com470e71d2011-07-07 08:21:25 +000046
andresp@webrtc.orgf7eb75b2013-09-14 00:25:28 +000047namespace {
andresp@webrtc.org1df9dc32014-01-09 08:01:57 +000048// This wrapper provides a way to modify the callback without the need to expose
49// a register method all the way down to the function calling it.
50class EncodedImageCallbackWrapper : public EncodedImageCallback {
51 public:
kthelgasond701dfd2017-03-27 07:24:57 -070052 EncodedImageCallbackWrapper() : callback_(nullptr) {}
andresp@webrtc.org1df9dc32014-01-09 08:01:57 +000053
54 virtual ~EncodedImageCallbackWrapper() {}
55
56 void Register(EncodedImageCallback* callback) {
kthelgasond701dfd2017-03-27 07:24:57 -070057 rtc::CritScope lock(&cs_);
andresp@webrtc.org1df9dc32014-01-09 08:01:57 +000058 callback_ = callback;
59 }
60
Sergey Ulanov525df3f2016-08-02 17:46:41 -070061 virtual Result OnEncodedImage(const EncodedImage& encoded_image,
62 const CodecSpecificInfo* codec_specific_info,
63 const RTPFragmentationHeader* fragmentation) {
kthelgasond701dfd2017-03-27 07:24:57 -070064 rtc::CritScope lock(&cs_);
Sergey Ulanov525df3f2016-08-02 17:46:41 -070065 if (callback_) {
66 return callback_->OnEncodedImage(encoded_image, codec_specific_info,
67 fragmentation);
68 }
69 return Result(Result::ERROR_SEND_FAILED);
andresp@webrtc.org1df9dc32014-01-09 08:01:57 +000070 }
71
72 private:
kthelgasond701dfd2017-03-27 07:24:57 -070073 rtc::CriticalSection cs_;
danilchap56359be2017-09-07 07:53:45 -070074 EncodedImageCallback* callback_ RTC_GUARDED_BY(cs_);
andresp@webrtc.org1df9dc32014-01-09 08:01:57 +000075};
76
andresp@webrtc.orgf7eb75b2013-09-14 00:25:28 +000077class VideoCodingModuleImpl : public VideoCodingModule {
78 public:
stefan@webrtc.org34c5da62014-04-11 14:08:35 +000079 VideoCodingModuleImpl(Clock* clock,
andresp@webrtc.orgf7eb75b2013-09-14 00:25:28 +000080 EventFactory* event_factory,
philipel83f831a2016-03-12 03:30:23 -080081 NackSender* nack_sender,
sprang3911c262016-04-15 01:24:14 -070082 KeyFrameRequestSender* keyframe_request_sender,
83 EncodedImageCallback* pre_decode_image_callback)
andresp@webrtc.orgf7eb75b2013-09-14 00:25:28 +000084 : VideoCodingModule(),
Niels Möllera0565992017-10-24 11:37:08 +020085 sender_(clock, &post_encode_callback_),
philipel721d4022016-12-15 07:10:57 -080086 timing_(new VCMTiming(clock)),
philipel83f831a2016-03-12 03:30:23 -080087 receiver_(clock,
88 event_factory,
sprang3911c262016-04-15 01:24:14 -070089 pre_decode_image_callback,
philipel721d4022016-12-15 07:10:57 -080090 timing_.get(),
philipel83f831a2016-03-12 03:30:23 -080091 nack_sender,
Peter Boström0b250722016-04-22 18:23:15 +020092 keyframe_request_sender) {}
andresp@webrtc.orgf7eb75b2013-09-14 00:25:28 +000093
Peter Boström0b250722016-04-22 18:23:15 +020094 virtual ~VideoCodingModuleImpl() {}
andresp@webrtc.orgf7eb75b2013-09-14 00:25:28 +000095
kjellander@webrtc.org14665ff2015-03-04 12:58:35 +000096 int64_t TimeUntilNextProcess() override {
pbos0fa9b222015-11-13 05:59:57 -080097 int64_t receiver_time = receiver_.TimeUntilNextProcess();
tommid0a71ba2017-03-14 04:16:20 -070098 RTC_DCHECK_GE(receiver_time, 0);
Niels Möllera0565992017-10-24 11:37:08 +020099 return receiver_time;
andresp@webrtc.orgf7eb75b2013-09-14 00:25:28 +0000100 }
101
Yves Gerey665174f2018-06-19 15:03:05 +0200102 void Process() override { receiver_.Process(); }
andresp@webrtc.orgf7eb75b2013-09-14 00:25:28 +0000103
kjellander@webrtc.org14665ff2015-03-04 12:58:35 +0000104 int32_t RegisterSendCodec(const VideoCodec* sendCodec,
105 uint32_t numberOfCores,
106 uint32_t maxPayloadSize) override {
Sergio Garcia Murillo43800f92018-06-21 16:16:38 +0200107 if (sendCodec != nullptr && ((sendCodec->codecType == kVideoCodecVP8) ||
108 (sendCodec->codecType == kVideoCodecH264))) {
109 // Set up a rate allocator and temporal layers factory for this codec
Erik Språng08127a92016-11-16 16:41:30 +0100110 // 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.
Sergio Garcia Murillo43800f92018-06-21 16:16:38 +0200114 VideoCodec codec = *sendCodec;
115 rate_allocator_ = VideoCodecInitializer::CreateBitrateAllocator(codec);
116 return sender_.RegisterSendCodec(&codec, numberOfCores, maxPayloadSize);
Erik Språng08127a92016-11-16 16:41:30 +0100117 }
pbos0fa9b222015-11-13 05:59:57 -0800118 return sender_.RegisterSendCodec(sendCodec, numberOfCores, maxPayloadSize);
andresp@webrtc.orgf7eb75b2013-09-14 00:25:28 +0000119 }
120
kjellander@webrtc.org14665ff2015-03-04 12:58:35 +0000121 int32_t RegisterExternalEncoder(VideoEncoder* externalEncoder,
Niels Möllerbf3dbb42018-03-16 13:38:46 +0100122 uint8_t /* payloadType */,
kjellander@webrtc.org14665ff2015-03-04 12:58:35 +0000123 bool internalSource) override {
Yves Gerey665174f2018-06-19 15:03:05 +0200124 sender_.RegisterExternalEncoder(externalEncoder, internalSource);
Peter Boström795dbe42015-11-27 14:09:07 +0100125 return 0;
andresp@webrtc.orgf7eb75b2013-09-14 00:25:28 +0000126 }
127
kjellander@webrtc.org14665ff2015-03-04 12:58:35 +0000128 int32_t SetChannelParameters(uint32_t target_bitrate, // bits/s.
129 uint8_t lossRate,
130 int64_t rtt) override {
Erik Språng08127a92016-11-16 16:41:30 +0100131 return sender_.SetChannelParameters(target_bitrate, lossRate, rtt,
sprang1a646ee2016-12-01 06:34:11 -0800132 rate_allocator_.get(), nullptr);
andresp@webrtc.orgf7eb75b2013-09-14 00:25:28 +0000133 }
134
kjellander@webrtc.org14665ff2015-03-04 12:58:35 +0000135 int32_t SetVideoProtection(VCMVideoProtection videoProtection,
136 bool enable) override {
pbosba8c15b2015-07-14 09:36:34 -0700137 // TODO(pbos): Remove enable from receive-side protection modes as well.
pbos0fa9b222015-11-13 05:59:57 -0800138 return receiver_.SetVideoProtection(videoProtection, enable);
andresp@webrtc.orgf7eb75b2013-09-14 00:25:28 +0000139 }
140
Miguel Casas-Sanchez47650702015-05-29 17:21:40 -0700141 int32_t AddVideoFrame(const VideoFrame& videoFrame,
kjellander@webrtc.org14665ff2015-03-04 12:58:35 +0000142 const CodecSpecificInfo* codecSpecificInfo) override {
Peter Boströmad6fc5a2016-05-12 03:01:31 +0200143 return sender_.AddVideoFrame(videoFrame, codecSpecificInfo);
andresp@webrtc.orgf7eb75b2013-09-14 00:25:28 +0000144 }
145
perkj600246e2016-05-04 11:26:51 -0700146 int32_t IntraFrameRequest(size_t stream_index) override {
pbos0fa9b222015-11-13 05:59:57 -0800147 return sender_.IntraFrameRequest(stream_index);
andresp@webrtc.orgf7eb75b2013-09-14 00:25:28 +0000148 }
149
kjellander@webrtc.org14665ff2015-03-04 12:58:35 +0000150 int32_t EnableFrameDropper(bool enable) override {
pbos0fa9b222015-11-13 05:59:57 -0800151 return sender_.EnableFrameDropper(enable);
andresp@webrtc.orgf7eb75b2013-09-14 00:25:28 +0000152 }
153
kjellander@webrtc.org14665ff2015-03-04 12:58:35 +0000154 int32_t RegisterReceiveCodec(const VideoCodec* receiveCodec,
155 int32_t numberOfCores,
156 bool requireKeyFrame) override {
pbos0fa9b222015-11-13 05:59:57 -0800157 return receiver_.RegisterReceiveCodec(receiveCodec, numberOfCores,
158 requireKeyFrame);
andresp@webrtc.orgf7eb75b2013-09-14 00:25:28 +0000159 }
160
Anders Carlsson7eb8e9f2018-05-18 10:33:04 +0200161 void RegisterExternalDecoder(VideoDecoder* externalDecoder,
162 uint8_t payloadType) override {
163 receiver_.RegisterExternalDecoder(externalDecoder, payloadType);
164 }
165
kjellander@webrtc.org14665ff2015-03-04 12:58:35 +0000166 int32_t RegisterReceiveCallback(
167 VCMReceiveCallback* receiveCallback) override {
tommid0a71ba2017-03-14 04:16:20 -0700168 RTC_DCHECK(construction_thread_.CalledOnValidThread());
pbos0fa9b222015-11-13 05:59:57 -0800169 return receiver_.RegisterReceiveCallback(receiveCallback);
andresp@webrtc.orgf7eb75b2013-09-14 00:25:28 +0000170 }
171
kjellander@webrtc.org14665ff2015-03-04 12:58:35 +0000172 int32_t RegisterFrameTypeCallback(
173 VCMFrameTypeCallback* frameTypeCallback) override {
pbos0fa9b222015-11-13 05:59:57 -0800174 return receiver_.RegisterFrameTypeCallback(frameTypeCallback);
andresp@webrtc.orgf7eb75b2013-09-14 00:25:28 +0000175 }
176
kjellander@webrtc.org14665ff2015-03-04 12:58:35 +0000177 int32_t RegisterPacketRequestCallback(
178 VCMPacketRequestCallback* callback) override {
tommid0a71ba2017-03-14 04:16:20 -0700179 RTC_DCHECK(construction_thread_.CalledOnValidThread());
pbos0fa9b222015-11-13 05:59:57 -0800180 return receiver_.RegisterPacketRequestCallback(callback);
andresp@webrtc.orgf7eb75b2013-09-14 00:25:28 +0000181 }
182
kjellander@webrtc.org14665ff2015-03-04 12:58:35 +0000183 int32_t Decode(uint16_t maxWaitTimeMs) override {
pbos0fa9b222015-11-13 05:59:57 -0800184 return receiver_.Decode(maxWaitTimeMs);
andresp@webrtc.orgf7eb75b2013-09-14 00:25:28 +0000185 }
186
kjellander@webrtc.org14665ff2015-03-04 12:58:35 +0000187 int32_t IncomingPacket(const uint8_t* incomingPayload,
188 size_t payloadLength,
189 const WebRtcRTPHeader& rtpInfo) override {
pbos0fa9b222015-11-13 05:59:57 -0800190 return receiver_.IncomingPacket(incomingPayload, payloadLength, rtpInfo);
andresp@webrtc.orgf7eb75b2013-09-14 00:25:28 +0000191 }
192
kjellander@webrtc.org14665ff2015-03-04 12:58:35 +0000193 int SetReceiverRobustnessMode(ReceiverRobustness robustnessMode,
194 VCMDecodeErrorMode errorMode) override {
pbos0fa9b222015-11-13 05:59:57 -0800195 return receiver_.SetReceiverRobustnessMode(robustnessMode, errorMode);
andresp@webrtc.orgf7eb75b2013-09-14 00:25:28 +0000196 }
197
kjellander@webrtc.org14665ff2015-03-04 12:58:35 +0000198 void SetNackSettings(size_t max_nack_list_size,
199 int max_packet_age_to_nack,
200 int max_incomplete_time_ms) override {
pbos0fa9b222015-11-13 05:59:57 -0800201 return receiver_.SetNackSettings(max_nack_list_size, max_packet_age_to_nack,
202 max_incomplete_time_ms);
andresp@webrtc.orgf7eb75b2013-09-14 00:25:28 +0000203 }
204
kjellander@webrtc.org14665ff2015-03-04 12:58:35 +0000205 void RegisterPostEncodeImageCallback(
206 EncodedImageCallback* observer) override {
andresp@webrtc.org1df9dc32014-01-09 08:01:57 +0000207 post_encode_callback_.Register(observer);
sprang@webrtc.org40709352013-11-26 11:41:59 +0000208 }
209
andresp@webrtc.orgf7eb75b2013-09-14 00:25:28 +0000210 private:
tommid0a71ba2017-03-14 04:16:20 -0700211 rtc::ThreadChecker construction_thread_;
andresp@webrtc.org1df9dc32014-01-09 08:01:57 +0000212 EncodedImageCallbackWrapper post_encode_callback_;
pbos0fa9b222015-11-13 05:59:57 -0800213 vcm::VideoSender sender_;
Erik Språng08127a92016-11-16 16:41:30 +0100214 std::unique_ptr<VideoBitrateAllocator> rate_allocator_;
philipel721d4022016-12-15 07:10:57 -0800215 std::unique_ptr<VCMTiming> timing_;
pbos0fa9b222015-11-13 05:59:57 -0800216 vcm::VideoReceiver receiver_;
andresp@webrtc.orgf7eb75b2013-09-14 00:25:28 +0000217};
218} // namespace
219
tommia5c18d72017-03-20 10:43:23 -0700220// DEPRECATED. Create method for current interface, will be removed when the
philipel83f831a2016-03-12 03:30:23 -0800221// new jitter buffer is in place.
philipel5908c712015-12-21 08:23:20 -0800222VideoCodingModule* VideoCodingModule::Create(Clock* clock,
223 EventFactory* event_factory) {
tommid0a71ba2017-03-14 04:16:20 -0700224 RTC_DCHECK(clock);
225 RTC_DCHECK(event_factory);
tommia5c18d72017-03-20 10:43:23 -0700226 return new VideoCodingModuleImpl(clock, event_factory, nullptr, nullptr,
227 nullptr);
niklase@google.com470e71d2011-07-07 08:21:25 +0000228}
229
stefan@webrtc.org791eec72011-10-11 07:53:43 +0000230} // namespace webrtc