blob: 3410edc624316eef6564165e404efc5ca5337575 [file] [log] [blame]
Niels Möllerf9063782018-02-20 16:09:48 +01001/*
2 * Copyright (c) 2018 The WebRTC project authors. All Rights Reserved.
3 *
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
11#include "modules/video_coding/decoder_database.h"
Jonas Olssona4d87372019-07-05 19:08:33 +020012
Tommi73009ec2022-09-29 21:02:04 +020013#include <memory>
14#include <utility>
15
Niels Möllerf9063782018-02-20 16:09:48 +010016#include "rtc_base/checks.h"
17#include "rtc_base/logging.h"
18
19namespace webrtc {
20
Tommi20b32712022-09-29 14:13:15 +020021VCMDecoderDatabase::VCMDecoderDatabase() {
Erik Språng79c96de2022-08-19 11:48:10 +020022 decoder_sequence_checker_.Detach();
23}
24
Tommi73009ec2022-09-29 21:02:04 +020025void VCMDecoderDatabase::DeregisterExternalDecoder(uint8_t payload_type) {
Erik Språng79c96de2022-08-19 11:48:10 +020026 RTC_DCHECK_RUN_ON(&decoder_sequence_checker_);
Danil Chapovalov7b78a312021-08-06 12:30:02 +020027 auto it = decoders_.find(payload_type);
28 if (it == decoders_.end()) {
Tommi73009ec2022-09-29 21:02:04 +020029 return;
Niels Möllerf9063782018-02-20 16:09:48 +010030 }
Tommibd02e702022-08-22 12:56:38 +020031
Niels Möllerf9063782018-02-20 16:09:48 +010032 // We can't use payload_type to check if the decoder is currently in use,
33 // because payload type may be out of date (e.g. before we decode the first
Åsa Persson6cb74fd2018-06-01 15:13:42 +020034 // frame after RegisterReceiveCodec).
Tommi73009ec2022-09-29 21:02:04 +020035 if (current_decoder_ && current_decoder_->IsSameDecoder(it->second.get())) {
Niels Möllerf9063782018-02-20 16:09:48 +010036 // Release it if it was registered and in use.
Danil Chapovalov7b78a312021-08-06 12:30:02 +020037 current_decoder_ = absl::nullopt;
Niels Möllerf9063782018-02-20 16:09:48 +010038 }
Danil Chapovalov7b78a312021-08-06 12:30:02 +020039 decoders_.erase(it);
Niels Möllerf9063782018-02-20 16:09:48 +010040}
41
Åsa Persson6cb74fd2018-06-01 15:13:42 +020042// Add the external decoder object to the list of external decoders.
Niels Möllerf9063782018-02-20 16:09:48 +010043// Won't be registered as a receive codec until RegisterReceiveCodec is called.
Tommi20b32712022-09-29 14:13:15 +020044void VCMDecoderDatabase::RegisterExternalDecoder(
Danil Chapovalov7b78a312021-08-06 12:30:02 +020045 uint8_t payload_type,
Tommi73009ec2022-09-29 21:02:04 +020046 std::unique_ptr<VideoDecoder> external_decoder) {
Erik Språng79c96de2022-08-19 11:48:10 +020047 RTC_DCHECK_RUN_ON(&decoder_sequence_checker_);
Åsa Persson6cb74fd2018-06-01 15:13:42 +020048 // If payload value already exists, erase old and insert new.
Niels Möllerf9063782018-02-20 16:09:48 +010049 DeregisterExternalDecoder(payload_type);
Tommi96c1a9b2022-09-29 12:24:02 +020050 if (external_decoder) {
Tommi73009ec2022-09-29 21:02:04 +020051 decoders_.emplace(
52 std::make_pair(payload_type, std::move(external_decoder)));
Tommi96c1a9b2022-09-29 12:24:02 +020053 }
Niels Möllerf9063782018-02-20 16:09:48 +010054}
55
Tommi20b32712022-09-29 14:13:15 +020056bool VCMDecoderDatabase::IsExternalDecoderRegistered(
Johannes Kron16359f62021-02-18 23:37:22 +010057 uint8_t payload_type) const {
Erik Språng79c96de2022-08-19 11:48:10 +020058 RTC_DCHECK_RUN_ON(&decoder_sequence_checker_);
Tommi96c1a9b2022-09-29 12:24:02 +020059 return decoders_.find(payload_type) != decoders_.end();
Johannes Kron16359f62021-02-18 23:37:22 +010060}
61
Tommi20b32712022-09-29 14:13:15 +020062void VCMDecoderDatabase::RegisterReceiveCodec(
Danil Chapovalov355b8d22021-08-13 16:50:37 +020063 uint8_t payload_type,
64 const VideoDecoder::Settings& settings) {
Åsa Persson6cb74fd2018-06-01 15:13:42 +020065 // If payload value already exists, erase old and insert new.
Danil Chapovalov7b78a312021-08-06 12:30:02 +020066 if (payload_type == current_payload_type_) {
67 current_payload_type_ = absl::nullopt;
68 }
Danil Chapovalov355b8d22021-08-13 16:50:37 +020069 decoder_settings_[payload_type] = settings;
Niels Möllerf9063782018-02-20 16:09:48 +010070}
71
Tommi20b32712022-09-29 14:13:15 +020072bool VCMDecoderDatabase::DeregisterReceiveCodec(uint8_t payload_type) {
Danil Chapovalov7b78a312021-08-06 12:30:02 +020073 if (decoder_settings_.erase(payload_type) == 0) {
Niels Möllerf9063782018-02-20 16:09:48 +010074 return false;
75 }
Niels Möller5401bad2020-08-11 12:17:42 +020076 if (payload_type == current_payload_type_) {
Niels Möllerf9063782018-02-20 16:09:48 +010077 // This codec is currently in use.
Danil Chapovalov7b78a312021-08-06 12:30:02 +020078 current_payload_type_ = absl::nullopt;
Niels Möllerf9063782018-02-20 16:09:48 +010079 }
80 return true;
81}
82
Tommi20b32712022-09-29 14:13:15 +020083void VCMDecoderDatabase::DeregisterReceiveCodecs() {
Tommi96c1a9b2022-09-29 12:24:02 +020084 current_payload_type_ = absl::nullopt;
85 decoder_settings_.clear();
86}
87
Tommi20b32712022-09-29 14:13:15 +020088VCMGenericDecoder* VCMDecoderDatabase::GetDecoder(
Niels Möllerf9063782018-02-20 16:09:48 +010089 const VCMEncodedFrame& frame,
90 VCMDecodedFrameCallback* decoded_frame_callback) {
Erik Språng79c96de2022-08-19 11:48:10 +020091 RTC_DCHECK_RUN_ON(&decoder_sequence_checker_);
Niels Möllerf9063782018-02-20 16:09:48 +010092 RTC_DCHECK(decoded_frame_callback->UserReceiveCallback());
93 uint8_t payload_type = frame.PayloadType();
Niels Möller5401bad2020-08-11 12:17:42 +020094 if (payload_type == current_payload_type_ || payload_type == 0) {
Danil Chapovalov7b78a312021-08-06 12:30:02 +020095 return current_decoder_.has_value() ? &*current_decoder_ : nullptr;
Niels Möllerf9063782018-02-20 16:09:48 +010096 }
Åsa Persson6cb74fd2018-06-01 15:13:42 +020097 // If decoder exists - delete.
Danil Chapovalov7b78a312021-08-06 12:30:02 +020098 if (current_decoder_.has_value()) {
99 current_decoder_ = absl::nullopt;
100 current_payload_type_ = absl::nullopt;
Niels Möllerf9063782018-02-20 16:09:48 +0100101 }
Danil Chapovalov7b78a312021-08-06 12:30:02 +0200102
103 CreateAndInitDecoder(frame);
104 if (current_decoder_ == absl::nullopt) {
Niels Möllerf9063782018-02-20 16:09:48 +0100105 return nullptr;
106 }
Danil Chapovalov7b78a312021-08-06 12:30:02 +0200107
Niels Möllerf9063782018-02-20 16:09:48 +0100108 VCMReceiveCallback* callback = decoded_frame_callback->UserReceiveCallback();
Danil Chapovalov7b78a312021-08-06 12:30:02 +0200109 callback->OnIncomingPayloadType(payload_type);
110 if (current_decoder_->RegisterDecodeCompleteCallback(decoded_frame_callback) <
Niels Möllerf9063782018-02-20 16:09:48 +0100111 0) {
Danil Chapovalov7b78a312021-08-06 12:30:02 +0200112 current_decoder_ = absl::nullopt;
Niels Möllerf9063782018-02-20 16:09:48 +0100113 return nullptr;
114 }
Danil Chapovalov7b78a312021-08-06 12:30:02 +0200115
116 current_payload_type_ = payload_type;
117 return &*current_decoder_;
Niels Möllerf9063782018-02-20 16:09:48 +0100118}
119
Tommi20b32712022-09-29 14:13:15 +0200120void VCMDecoderDatabase::CreateAndInitDecoder(const VCMEncodedFrame& frame) {
Niels Möllerf9063782018-02-20 16:09:48 +0100121 uint8_t payload_type = frame.PayloadType();
Tommi96c1a9b2022-09-29 12:24:02 +0200122 RTC_DLOG(LS_INFO) << "Initializing decoder with payload type '"
123 << int{payload_type} << "'.";
Danil Chapovalov7b78a312021-08-06 12:30:02 +0200124 auto decoder_item = decoder_settings_.find(payload_type);
125 if (decoder_item == decoder_settings_.end()) {
Niels Möllerf9063782018-02-20 16:09:48 +0100126 RTC_LOG(LS_ERROR) << "Can't find a decoder associated with payload type: "
Danil Chapovalov7b78a312021-08-06 12:30:02 +0200127 << int{payload_type};
128 return;
Niels Möllerf9063782018-02-20 16:09:48 +0100129 }
Danil Chapovalov7b78a312021-08-06 12:30:02 +0200130 auto external_dec_item = decoders_.find(payload_type);
131 if (external_dec_item == decoders_.end()) {
Niels Möllerf9063782018-02-20 16:09:48 +0100132 RTC_LOG(LS_ERROR) << "No decoder of this type exists.";
Danil Chapovalov7b78a312021-08-06 12:30:02 +0200133 return;
Niels Möllerf9063782018-02-20 16:09:48 +0100134 }
Tommi73009ec2022-09-29 21:02:04 +0200135 current_decoder_.emplace(external_dec_item->second.get());
Niels Möllerf9063782018-02-20 16:09:48 +0100136
137 // Copy over input resolutions to prevent codec reinitialization due to
138 // the first frame being of a different resolution than the database values.
139 // This is best effort, since there's no guarantee that width/height have been
140 // parsed yet (and may be zero).
Danil Chapovalov355b8d22021-08-13 16:50:37 +0200141 RenderResolution frame_resolution(frame.EncodedImage()._encodedWidth,
142 frame.EncodedImage()._encodedHeight);
143 if (frame_resolution.Valid()) {
144 decoder_item->second.set_max_render_resolution(frame_resolution);
Niels Möllerf9063782018-02-20 16:09:48 +0100145 }
Danil Chapovalov355b8d22021-08-13 16:50:37 +0200146 if (!current_decoder_->Configure(decoder_item->second)) {
Danil Chapovalov7b78a312021-08-06 12:30:02 +0200147 current_decoder_ = absl::nullopt;
Danil Chapovalov355b8d22021-08-13 16:50:37 +0200148 RTC_LOG(LS_ERROR) << "Failed to initialize decoder.";
Niels Möllerf9063782018-02-20 16:09:48 +0100149 }
Niels Möllerf9063782018-02-20 16:09:48 +0100150}
151
152} // namespace webrtc