blob: 6f4903e216c97980a2e9fd77fae0285c69d8abf3 [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
Artem Titovd15a5752021-02-10 14:31:24 +010016#include "api/sequence_checker.h"
Yves Gerey3e707812018-11-28 16:47:49 +010017#include "api/video/encoded_image.h"
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"
Mirko Bonadei92ea95e2017-09-15 06:47:31 +020020#include "system_wrappers/include/clock.h"
niklase@google.com470e71d2011-07-07 08:21:25 +000021
agalusza@google.coma7e360e2013-08-01 03:15:08 +000022namespace webrtc {
andresp@webrtc.orgf7eb75b2013-09-14 00:25:28 +000023namespace vcm {
niklase@google.com470e71d2011-07-07 08:21:25 +000024
philipel5908c712015-12-21 08:23:20 -080025int64_t VCMProcessTimer::Period() const {
26 return _periodMs;
niklase@google.com470e71d2011-07-07 08:21:25 +000027}
28
philipel5908c712015-12-21 08:23:20 -080029int64_t VCMProcessTimer::TimeUntilProcess() const {
30 const int64_t time_since_process = _clock->TimeInMilliseconds() - _latestMs;
31 const int64_t time_until_process = _periodMs - time_since_process;
32 return std::max<int64_t>(time_until_process, 0);
niklase@google.com470e71d2011-07-07 08:21:25 +000033}
34
philipel5908c712015-12-21 08:23:20 -080035void VCMProcessTimer::Processed() {
36 _latestMs = _clock->TimeInMilliseconds();
niklase@google.com470e71d2011-07-07 08:21:25 +000037}
andresp@webrtc.orgf7eb75b2013-09-14 00:25:28 +000038} // namespace vcm
niklase@google.com470e71d2011-07-07 08:21:25 +000039
andresp@webrtc.orgf7eb75b2013-09-14 00:25:28 +000040namespace {
andresp@webrtc.org1df9dc32014-01-09 08:01:57 +000041
andresp@webrtc.orgf7eb75b2013-09-14 00:25:28 +000042class VideoCodingModuleImpl : public VideoCodingModule {
43 public:
Niels Möllerdb64d992019-03-29 14:30:53 +010044 explicit VideoCodingModuleImpl(Clock* clock)
andresp@webrtc.orgf7eb75b2013-09-14 00:25:28 +000045 : VideoCodingModule(),
philipel721d4022016-12-15 07:10:57 -080046 timing_(new VCMTiming(clock)),
Niels Möllerdb64d992019-03-29 14:30:53 +010047 receiver_(clock, timing_.get()) {}
andresp@webrtc.orgf7eb75b2013-09-14 00:25:28 +000048
Mirko Bonadeife055c12019-01-29 22:53:28 +010049 ~VideoCodingModuleImpl() override {}
andresp@webrtc.orgf7eb75b2013-09-14 00:25:28 +000050
kjellander@webrtc.org14665ff2015-03-04 12:58:35 +000051 int64_t TimeUntilNextProcess() override {
pbos0fa9b222015-11-13 05:59:57 -080052 int64_t receiver_time = receiver_.TimeUntilNextProcess();
tommid0a71ba2017-03-14 04:16:20 -070053 RTC_DCHECK_GE(receiver_time, 0);
Niels Möllera0565992017-10-24 11:37:08 +020054 return receiver_time;
andresp@webrtc.orgf7eb75b2013-09-14 00:25:28 +000055 }
56
Yves Gerey665174f2018-06-19 15:03:05 +020057 void Process() override { receiver_.Process(); }
andresp@webrtc.orgf7eb75b2013-09-14 00:25:28 +000058
Danil Chapovalov355b8d22021-08-13 16:50:37 +020059 bool RegisterReceiveCodec(
60 uint8_t payload_type,
61 const VideoDecoder::Settings& decoder_settings) override {
62 return receiver_.RegisterReceiveCodec(payload_type, decoder_settings);
63 }
64
Niels Möller582102c2020-08-07 16:19:56 +020065 int32_t RegisterReceiveCodec(uint8_t payload_type,
Danil Chapovalov355b8d22021-08-13 16:50:37 +020066 const VideoCodec* receive_codec,
67 int32_t number_of_cores) override {
68 VideoDecoder::Settings decoder_settings;
69 if (receive_codec != nullptr) {
70 decoder_settings.set_codec_type(receive_codec->codecType);
71 decoder_settings.set_max_render_resolution(
72 {receive_codec->width, receive_codec->height});
73 decoder_settings.set_buffer_pool_size(receive_codec->buffer_pool_size);
74 }
75 decoder_settings.set_number_of_cores(number_of_cores);
76 return receiver_.RegisterReceiveCodec(payload_type, decoder_settings) ? 0
77 : -1;
andresp@webrtc.orgf7eb75b2013-09-14 00:25:28 +000078 }
79
Anders Carlsson7eb8e9f2018-05-18 10:33:04 +020080 void RegisterExternalDecoder(VideoDecoder* externalDecoder,
81 uint8_t payloadType) override {
82 receiver_.RegisterExternalDecoder(externalDecoder, payloadType);
83 }
84
kjellander@webrtc.org14665ff2015-03-04 12:58:35 +000085 int32_t RegisterReceiveCallback(
86 VCMReceiveCallback* receiveCallback) override {
Sebastian Janssonc01367d2019-04-08 15:20:44 +020087 RTC_DCHECK(construction_thread_.IsCurrent());
pbos0fa9b222015-11-13 05:59:57 -080088 return receiver_.RegisterReceiveCallback(receiveCallback);
andresp@webrtc.orgf7eb75b2013-09-14 00:25:28 +000089 }
90
kjellander@webrtc.org14665ff2015-03-04 12:58:35 +000091 int32_t RegisterFrameTypeCallback(
92 VCMFrameTypeCallback* frameTypeCallback) override {
pbos0fa9b222015-11-13 05:59:57 -080093 return receiver_.RegisterFrameTypeCallback(frameTypeCallback);
andresp@webrtc.orgf7eb75b2013-09-14 00:25:28 +000094 }
95
kjellander@webrtc.org14665ff2015-03-04 12:58:35 +000096 int32_t RegisterPacketRequestCallback(
97 VCMPacketRequestCallback* callback) override {
Sebastian Janssonc01367d2019-04-08 15:20:44 +020098 RTC_DCHECK(construction_thread_.IsCurrent());
pbos0fa9b222015-11-13 05:59:57 -080099 return receiver_.RegisterPacketRequestCallback(callback);
andresp@webrtc.orgf7eb75b2013-09-14 00:25:28 +0000100 }
101
kjellander@webrtc.org14665ff2015-03-04 12:58:35 +0000102 int32_t Decode(uint16_t maxWaitTimeMs) override {
pbos0fa9b222015-11-13 05:59:57 -0800103 return receiver_.Decode(maxWaitTimeMs);
andresp@webrtc.orgf7eb75b2013-09-14 00:25:28 +0000104 }
105
kjellander@webrtc.org14665ff2015-03-04 12:58:35 +0000106 int32_t IncomingPacket(const uint8_t* incomingPayload,
107 size_t payloadLength,
Niels Möllerbe7a0ec2019-04-25 10:02:52 +0200108 const RTPHeader& rtp_header,
109 const RTPVideoHeader& video_header) override {
110 return receiver_.IncomingPacket(incomingPayload, payloadLength, rtp_header,
111 video_header);
andresp@webrtc.orgf7eb75b2013-09-14 00:25:28 +0000112 }
113
kjellander@webrtc.org14665ff2015-03-04 12:58:35 +0000114 void SetNackSettings(size_t max_nack_list_size,
115 int max_packet_age_to_nack,
116 int max_incomplete_time_ms) override {
pbos0fa9b222015-11-13 05:59:57 -0800117 return receiver_.SetNackSettings(max_nack_list_size, max_packet_age_to_nack,
118 max_incomplete_time_ms);
andresp@webrtc.orgf7eb75b2013-09-14 00:25:28 +0000119 }
120
andresp@webrtc.orgf7eb75b2013-09-14 00:25:28 +0000121 private:
Artem Titovc8421c42021-02-02 10:57:19 +0100122 SequenceChecker construction_thread_;
Jiawei Ouc2ebe212018-11-08 10:02:56 -0800123 const std::unique_ptr<VCMTiming> timing_;
pbos0fa9b222015-11-13 05:59:57 -0800124 vcm::VideoReceiver receiver_;
andresp@webrtc.orgf7eb75b2013-09-14 00:25:28 +0000125};
126} // namespace
127
tommia5c18d72017-03-20 10:43:23 -0700128// DEPRECATED. Create method for current interface, will be removed when the
philipel83f831a2016-03-12 03:30:23 -0800129// new jitter buffer is in place.
Niels Möller689983f2018-11-07 16:36:22 +0100130VideoCodingModule* VideoCodingModule::Create(Clock* clock) {
tommid0a71ba2017-03-14 04:16:20 -0700131 RTC_DCHECK(clock);
Niels Möllerdb64d992019-03-29 14:30:53 +0100132 return new VideoCodingModuleImpl(clock);
niklase@google.com470e71d2011-07-07 08:21:25 +0000133}
134
stefan@webrtc.org791eec72011-10-11 07:53:43 +0000135} // namespace webrtc