blob: 66d1a2aa1b6e22e4ae529c8e8127e52f2e14ae6f [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>
Yves Gerey3e707812018-11-28 16:47:49 +010014#include <memory>
philipel5908c712015-12-21 08:23:20 -080015
Yves Gerey3e707812018-11-28 16:47:49 +010016#include "api/video/encoded_image.h"
Mirko Bonadei71207422017-09-15 13:58:09 +020017#include "common_types.h" // NOLINT(build/include)
Mirko Bonadei92ea95e2017-09-15 06:47:31 +020018#include "modules/video_coding/include/video_codec_interface.h"
Mirko Bonadei92ea95e2017-09-15 06:47:31 +020019#include "modules/video_coding/timing.h"
20#include "rtc_base/criticalsection.h"
21#include "rtc_base/thread_checker.h"
22#include "system_wrappers/include/clock.h"
niklase@google.com470e71d2011-07-07 08:21:25 +000023
agalusza@google.coma7e360e2013-08-01 03:15:08 +000024namespace webrtc {
andresp@webrtc.orgf7eb75b2013-09-14 00:25:28 +000025namespace vcm {
niklase@google.com470e71d2011-07-07 08:21:25 +000026
philipel5908c712015-12-21 08:23:20 -080027int64_t VCMProcessTimer::Period() const {
28 return _periodMs;
niklase@google.com470e71d2011-07-07 08:21:25 +000029}
30
philipel5908c712015-12-21 08:23:20 -080031int64_t VCMProcessTimer::TimeUntilProcess() const {
32 const int64_t time_since_process = _clock->TimeInMilliseconds() - _latestMs;
33 const int64_t time_until_process = _periodMs - time_since_process;
34 return std::max<int64_t>(time_until_process, 0);
niklase@google.com470e71d2011-07-07 08:21:25 +000035}
36
philipel5908c712015-12-21 08:23:20 -080037void VCMProcessTimer::Processed() {
38 _latestMs = _clock->TimeInMilliseconds();
niklase@google.com470e71d2011-07-07 08:21:25 +000039}
andresp@webrtc.orgf7eb75b2013-09-14 00:25:28 +000040} // namespace vcm
niklase@google.com470e71d2011-07-07 08:21:25 +000041
andresp@webrtc.orgf7eb75b2013-09-14 00:25:28 +000042namespace {
andresp@webrtc.org1df9dc32014-01-09 08:01:57 +000043
andresp@webrtc.orgf7eb75b2013-09-14 00:25:28 +000044class VideoCodingModuleImpl : public VideoCodingModule {
45 public:
stefan@webrtc.org34c5da62014-04-11 14:08:35 +000046 VideoCodingModuleImpl(Clock* clock,
philipel83f831a2016-03-12 03:30:23 -080047 NackSender* nack_sender,
Niels Möller9eb44ac2018-10-02 12:47:47 +020048 KeyFrameRequestSender* keyframe_request_sender)
andresp@webrtc.orgf7eb75b2013-09-14 00:25:28 +000049 : VideoCodingModule(),
philipel721d4022016-12-15 07:10:57 -080050 timing_(new VCMTiming(clock)),
Niels Möller689983f2018-11-07 16:36:22 +010051 receiver_(clock, timing_.get(), nack_sender, keyframe_request_sender) {}
andresp@webrtc.orgf7eb75b2013-09-14 00:25:28 +000052
Peter Boström0b250722016-04-22 18:23:15 +020053 virtual ~VideoCodingModuleImpl() {}
andresp@webrtc.orgf7eb75b2013-09-14 00:25:28 +000054
kjellander@webrtc.org14665ff2015-03-04 12:58:35 +000055 int64_t TimeUntilNextProcess() override {
pbos0fa9b222015-11-13 05:59:57 -080056 int64_t receiver_time = receiver_.TimeUntilNextProcess();
tommid0a71ba2017-03-14 04:16:20 -070057 RTC_DCHECK_GE(receiver_time, 0);
Niels Möllera0565992017-10-24 11:37:08 +020058 return receiver_time;
andresp@webrtc.orgf7eb75b2013-09-14 00:25:28 +000059 }
60
Yves Gerey665174f2018-06-19 15:03:05 +020061 void Process() override { receiver_.Process(); }
andresp@webrtc.orgf7eb75b2013-09-14 00:25:28 +000062
kjellander@webrtc.org14665ff2015-03-04 12:58:35 +000063 int32_t SetVideoProtection(VCMVideoProtection videoProtection,
64 bool enable) override {
pbosba8c15b2015-07-14 09:36:34 -070065 // TODO(pbos): Remove enable from receive-side protection modes as well.
pbos0fa9b222015-11-13 05:59:57 -080066 return receiver_.SetVideoProtection(videoProtection, enable);
andresp@webrtc.orgf7eb75b2013-09-14 00:25:28 +000067 }
68
kjellander@webrtc.org14665ff2015-03-04 12:58:35 +000069 int32_t RegisterReceiveCodec(const VideoCodec* receiveCodec,
70 int32_t numberOfCores,
71 bool requireKeyFrame) override {
pbos0fa9b222015-11-13 05:59:57 -080072 return receiver_.RegisterReceiveCodec(receiveCodec, numberOfCores,
73 requireKeyFrame);
andresp@webrtc.orgf7eb75b2013-09-14 00:25:28 +000074 }
75
Anders Carlsson7eb8e9f2018-05-18 10:33:04 +020076 void RegisterExternalDecoder(VideoDecoder* externalDecoder,
77 uint8_t payloadType) override {
78 receiver_.RegisterExternalDecoder(externalDecoder, payloadType);
79 }
80
kjellander@webrtc.org14665ff2015-03-04 12:58:35 +000081 int32_t RegisterReceiveCallback(
82 VCMReceiveCallback* receiveCallback) override {
tommid0a71ba2017-03-14 04:16:20 -070083 RTC_DCHECK(construction_thread_.CalledOnValidThread());
pbos0fa9b222015-11-13 05:59:57 -080084 return receiver_.RegisterReceiveCallback(receiveCallback);
andresp@webrtc.orgf7eb75b2013-09-14 00:25:28 +000085 }
86
kjellander@webrtc.org14665ff2015-03-04 12:58:35 +000087 int32_t RegisterFrameTypeCallback(
88 VCMFrameTypeCallback* frameTypeCallback) override {
pbos0fa9b222015-11-13 05:59:57 -080089 return receiver_.RegisterFrameTypeCallback(frameTypeCallback);
andresp@webrtc.orgf7eb75b2013-09-14 00:25:28 +000090 }
91
kjellander@webrtc.org14665ff2015-03-04 12:58:35 +000092 int32_t RegisterPacketRequestCallback(
93 VCMPacketRequestCallback* callback) override {
tommid0a71ba2017-03-14 04:16:20 -070094 RTC_DCHECK(construction_thread_.CalledOnValidThread());
pbos0fa9b222015-11-13 05:59:57 -080095 return receiver_.RegisterPacketRequestCallback(callback);
andresp@webrtc.orgf7eb75b2013-09-14 00:25:28 +000096 }
97
kjellander@webrtc.org14665ff2015-03-04 12:58:35 +000098 int32_t Decode(uint16_t maxWaitTimeMs) override {
pbos0fa9b222015-11-13 05:59:57 -080099 return receiver_.Decode(maxWaitTimeMs);
andresp@webrtc.orgf7eb75b2013-09-14 00:25:28 +0000100 }
101
kjellander@webrtc.org14665ff2015-03-04 12:58:35 +0000102 int32_t IncomingPacket(const uint8_t* incomingPayload,
103 size_t payloadLength,
104 const WebRtcRTPHeader& rtpInfo) override {
pbos0fa9b222015-11-13 05:59:57 -0800105 return receiver_.IncomingPacket(incomingPayload, payloadLength, rtpInfo);
andresp@webrtc.orgf7eb75b2013-09-14 00:25:28 +0000106 }
107
kjellander@webrtc.org14665ff2015-03-04 12:58:35 +0000108 int SetReceiverRobustnessMode(ReceiverRobustness robustnessMode,
109 VCMDecodeErrorMode errorMode) override {
pbos0fa9b222015-11-13 05:59:57 -0800110 return receiver_.SetReceiverRobustnessMode(robustnessMode, errorMode);
andresp@webrtc.orgf7eb75b2013-09-14 00:25:28 +0000111 }
112
kjellander@webrtc.org14665ff2015-03-04 12:58:35 +0000113 void SetNackSettings(size_t max_nack_list_size,
114 int max_packet_age_to_nack,
115 int max_incomplete_time_ms) override {
pbos0fa9b222015-11-13 05:59:57 -0800116 return receiver_.SetNackSettings(max_nack_list_size, max_packet_age_to_nack,
117 max_incomplete_time_ms);
andresp@webrtc.orgf7eb75b2013-09-14 00:25:28 +0000118 }
119
andresp@webrtc.orgf7eb75b2013-09-14 00:25:28 +0000120 private:
tommid0a71ba2017-03-14 04:16:20 -0700121 rtc::ThreadChecker construction_thread_;
Jiawei Ouc2ebe212018-11-08 10:02:56 -0800122 const std::unique_ptr<VCMTiming> timing_;
pbos0fa9b222015-11-13 05:59:57 -0800123 vcm::VideoReceiver receiver_;
andresp@webrtc.orgf7eb75b2013-09-14 00:25:28 +0000124};
125} // namespace
126
tommia5c18d72017-03-20 10:43:23 -0700127// DEPRECATED. Create method for current interface, will be removed when the
philipel83f831a2016-03-12 03:30:23 -0800128// new jitter buffer is in place.
Niels Möller689983f2018-11-07 16:36:22 +0100129VideoCodingModule* VideoCodingModule::Create(Clock* clock) {
tommid0a71ba2017-03-14 04:16:20 -0700130 RTC_DCHECK(clock);
Niels Möllerc4e98252018-11-08 13:08:26 +0100131 return new VideoCodingModuleImpl(clock, nullptr, nullptr);
niklase@google.com470e71d2011-07-07 08:21:25 +0000132}
133
stefan@webrtc.org791eec72011-10-11 07:53:43 +0000134} // namespace webrtc