blob: 70b816d70657acafd8207fa43775856056196bc0 [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"
Steve Anton10542f22019-01-11 09:11:00 -080020#include "rtc_base/critical_section.h"
Mirko Bonadei92ea95e2017-09-15 06:47:31 +020021#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 RegisterReceiveCodec(const VideoCodec* receiveCodec,
64 int32_t numberOfCores,
65 bool requireKeyFrame) override {
pbos0fa9b222015-11-13 05:59:57 -080066 return receiver_.RegisterReceiveCodec(receiveCodec, numberOfCores,
67 requireKeyFrame);
andresp@webrtc.orgf7eb75b2013-09-14 00:25:28 +000068 }
69
Anders Carlsson7eb8e9f2018-05-18 10:33:04 +020070 void RegisterExternalDecoder(VideoDecoder* externalDecoder,
71 uint8_t payloadType) override {
72 receiver_.RegisterExternalDecoder(externalDecoder, payloadType);
73 }
74
kjellander@webrtc.org14665ff2015-03-04 12:58:35 +000075 int32_t RegisterReceiveCallback(
76 VCMReceiveCallback* receiveCallback) override {
tommid0a71ba2017-03-14 04:16:20 -070077 RTC_DCHECK(construction_thread_.CalledOnValidThread());
pbos0fa9b222015-11-13 05:59:57 -080078 return receiver_.RegisterReceiveCallback(receiveCallback);
andresp@webrtc.orgf7eb75b2013-09-14 00:25:28 +000079 }
80
kjellander@webrtc.org14665ff2015-03-04 12:58:35 +000081 int32_t RegisterFrameTypeCallback(
82 VCMFrameTypeCallback* frameTypeCallback) override {
pbos0fa9b222015-11-13 05:59:57 -080083 return receiver_.RegisterFrameTypeCallback(frameTypeCallback);
andresp@webrtc.orgf7eb75b2013-09-14 00:25:28 +000084 }
85
kjellander@webrtc.org14665ff2015-03-04 12:58:35 +000086 int32_t RegisterPacketRequestCallback(
87 VCMPacketRequestCallback* callback) override {
tommid0a71ba2017-03-14 04:16:20 -070088 RTC_DCHECK(construction_thread_.CalledOnValidThread());
pbos0fa9b222015-11-13 05:59:57 -080089 return receiver_.RegisterPacketRequestCallback(callback);
andresp@webrtc.orgf7eb75b2013-09-14 00:25:28 +000090 }
91
kjellander@webrtc.org14665ff2015-03-04 12:58:35 +000092 int32_t Decode(uint16_t maxWaitTimeMs) override {
pbos0fa9b222015-11-13 05:59:57 -080093 return receiver_.Decode(maxWaitTimeMs);
andresp@webrtc.orgf7eb75b2013-09-14 00:25:28 +000094 }
95
kjellander@webrtc.org14665ff2015-03-04 12:58:35 +000096 int32_t IncomingPacket(const uint8_t* incomingPayload,
97 size_t payloadLength,
98 const WebRtcRTPHeader& rtpInfo) override {
pbos0fa9b222015-11-13 05:59:57 -080099 return receiver_.IncomingPacket(incomingPayload, payloadLength, rtpInfo);
andresp@webrtc.orgf7eb75b2013-09-14 00:25:28 +0000100 }
101
kjellander@webrtc.org14665ff2015-03-04 12:58:35 +0000102 int SetReceiverRobustnessMode(ReceiverRobustness robustnessMode,
103 VCMDecodeErrorMode errorMode) override {
pbos0fa9b222015-11-13 05:59:57 -0800104 return receiver_.SetReceiverRobustnessMode(robustnessMode, errorMode);
andresp@webrtc.orgf7eb75b2013-09-14 00:25:28 +0000105 }
106
kjellander@webrtc.org14665ff2015-03-04 12:58:35 +0000107 void SetNackSettings(size_t max_nack_list_size,
108 int max_packet_age_to_nack,
109 int max_incomplete_time_ms) override {
pbos0fa9b222015-11-13 05:59:57 -0800110 return receiver_.SetNackSettings(max_nack_list_size, max_packet_age_to_nack,
111 max_incomplete_time_ms);
andresp@webrtc.orgf7eb75b2013-09-14 00:25:28 +0000112 }
113
andresp@webrtc.orgf7eb75b2013-09-14 00:25:28 +0000114 private:
tommid0a71ba2017-03-14 04:16:20 -0700115 rtc::ThreadChecker construction_thread_;
Jiawei Ouc2ebe212018-11-08 10:02:56 -0800116 const std::unique_ptr<VCMTiming> timing_;
pbos0fa9b222015-11-13 05:59:57 -0800117 vcm::VideoReceiver receiver_;
andresp@webrtc.orgf7eb75b2013-09-14 00:25:28 +0000118};
119} // namespace
120
tommia5c18d72017-03-20 10:43:23 -0700121// DEPRECATED. Create method for current interface, will be removed when the
philipel83f831a2016-03-12 03:30:23 -0800122// new jitter buffer is in place.
Niels Möller689983f2018-11-07 16:36:22 +0100123VideoCodingModule* VideoCodingModule::Create(Clock* clock) {
tommid0a71ba2017-03-14 04:16:20 -0700124 RTC_DCHECK(clock);
Niels Möllerc4e98252018-11-08 13:08:26 +0100125 return new VideoCodingModuleImpl(clock, nullptr, nullptr);
niklase@google.com470e71d2011-07-07 08:21:25 +0000126}
127
stefan@webrtc.org791eec72011-10-11 07:53:43 +0000128} // namespace webrtc