niklase@google.com | 470e71d | 2011-07-07 08:21:25 +0000 | [diff] [blame] | 1 | /* |
mikhal@webrtc.org | a2031d5 | 2012-07-31 15:53:44 +0000 | [diff] [blame] | 2 | * Copyright (c) 2012 The WebRTC project authors. All Rights Reserved. |
niklase@google.com | 470e71d | 2011-07-07 08:21:25 +0000 | [diff] [blame] | 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 | |
Mirko Bonadei | 92ea95e | 2017-09-15 06:47:31 +0200 | [diff] [blame] | 11 | #include "modules/video_coding/generic_decoder.h" |
tommi | 5b7fc8c | 2017-07-05 16:45:57 -0700 | [diff] [blame] | 12 | |
Yves Gerey | 3e70781 | 2018-11-28 16:47:49 +0100 | [diff] [blame] | 13 | #include <stddef.h> |
Jonas Olsson | a4d8737 | 2019-07-05 19:08:33 +0200 | [diff] [blame] | 14 | |
ilnik | 2edc684 | 2017-07-06 03:06:50 -0700 | [diff] [blame] | 15 | #include <algorithm> |
Johannes Kron | 111e981 | 2020-10-26 13:54:40 +0100 | [diff] [blame] | 16 | #include <cmath> |
ilnik | 2edc684 | 2017-07-06 03:06:50 -0700 | [diff] [blame] | 17 | |
Yves Gerey | 3e70781 | 2018-11-28 16:47:49 +0100 | [diff] [blame] | 18 | #include "api/video/video_timing.h" |
| 19 | #include "modules/video_coding/include/video_error_codes.h" |
Mirko Bonadei | 92ea95e | 2017-09-15 06:47:31 +0200 | [diff] [blame] | 20 | #include "rtc_base/checks.h" |
| 21 | #include "rtc_base/logging.h" |
Johannes Kron | 7ddea57 | 2019-09-11 12:00:22 +0200 | [diff] [blame] | 22 | #include "rtc_base/thread.h" |
Steve Anton | 10542f2 | 2019-01-11 09:11:00 -0800 | [diff] [blame] | 23 | #include "rtc_base/time_utils.h" |
Mirko Bonadei | 92ea95e | 2017-09-15 06:47:31 +0200 | [diff] [blame] | 24 | #include "rtc_base/trace_event.h" |
| 25 | #include "system_wrappers/include/clock.h" |
Johannes Kron | 7ddea57 | 2019-09-11 12:00:22 +0200 | [diff] [blame] | 26 | #include "system_wrappers/include/field_trial.h" |
niklase@google.com | 470e71d | 2011-07-07 08:21:25 +0000 | [diff] [blame] | 27 | |
| 28 | namespace webrtc { |
| 29 | |
philipel | 9d3ab61 | 2015-12-21 04:12:39 -0800 | [diff] [blame] | 30 | VCMDecodedFrameCallback::VCMDecodedFrameCallback(VCMTiming* timing, |
stefan@webrtc.org | a678a3b | 2013-01-21 07:42:11 +0000 | [diff] [blame] | 31 | Clock* clock) |
Johannes Kron | 7ddea57 | 2019-09-11 12:00:22 +0200 | [diff] [blame] | 32 | : _clock(clock), |
| 33 | _timing(timing), |
| 34 | _timestampMap(kDecoderFrameMemoryLength), |
Johannes Kron | 111e981 | 2020-10-26 13:54:40 +0100 | [diff] [blame] | 35 | _extra_decode_time("t", absl::nullopt), |
| 36 | low_latency_renderer_enabled_("enabled", true), |
| 37 | low_latency_renderer_include_predecode_buffer_("include_predecode_buffer", |
| 38 | true) { |
ilnik | 04f4d12 | 2017-06-19 07:18:55 -0700 | [diff] [blame] | 39 | ntp_offset_ = |
| 40 | _clock->CurrentNtpInMilliseconds() - _clock->TimeInMilliseconds(); |
Johannes Kron | 7ddea57 | 2019-09-11 12:00:22 +0200 | [diff] [blame] | 41 | |
| 42 | ParseFieldTrial({&_extra_decode_time}, |
| 43 | field_trial::FindFullName("WebRTC-SlowDownDecoder")); |
Johannes Kron | 111e981 | 2020-10-26 13:54:40 +0100 | [diff] [blame] | 44 | ParseFieldTrial({&low_latency_renderer_enabled_, |
| 45 | &low_latency_renderer_include_predecode_buffer_}, |
| 46 | field_trial::FindFullName("WebRTC-LowLatencyRenderer")); |
ilnik | 04f4d12 | 2017-06-19 07:18:55 -0700 | [diff] [blame] | 47 | } |
niklase@google.com | 470e71d | 2011-07-07 08:21:25 +0000 | [diff] [blame] | 48 | |
Yves Gerey | 665174f | 2018-06-19 15:03:05 +0200 | [diff] [blame] | 49 | VCMDecodedFrameCallback::~VCMDecodedFrameCallback() {} |
niklase@google.com | 470e71d | 2011-07-07 08:21:25 +0000 | [diff] [blame] | 50 | |
stefan@webrtc.org | 06887ae | 2011-10-10 14:17:46 +0000 | [diff] [blame] | 51 | void VCMDecodedFrameCallback::SetUserReceiveCallback( |
philipel | 9d3ab61 | 2015-12-21 04:12:39 -0800 | [diff] [blame] | 52 | VCMReceiveCallback* receiveCallback) { |
Sebastian Jansson | c01367d | 2019-04-08 15:20:44 +0200 | [diff] [blame] | 53 | RTC_DCHECK(construction_thread_.IsCurrent()); |
tommi | d0a71ba | 2017-03-14 04:16:20 -0700 | [diff] [blame] | 54 | RTC_DCHECK((!_receiveCallback && receiveCallback) || |
| 55 | (_receiveCallback && !receiveCallback)); |
philipel | 9d3ab61 | 2015-12-21 04:12:39 -0800 | [diff] [blame] | 56 | _receiveCallback = receiveCallback; |
niklase@google.com | 470e71d | 2011-07-07 08:21:25 +0000 | [diff] [blame] | 57 | } |
| 58 | |
philipel | 9d3ab61 | 2015-12-21 04:12:39 -0800 | [diff] [blame] | 59 | VCMReceiveCallback* VCMDecodedFrameCallback::UserReceiveCallback() { |
tommi | d0a71ba | 2017-03-14 04:16:20 -0700 | [diff] [blame] | 60 | // Called on the decode thread via VCMCodecDataBase::GetDecoder. |
| 61 | // The callback must always have been set before this happens. |
| 62 | RTC_DCHECK(_receiveCallback); |
philipel | 9d3ab61 | 2015-12-21 04:12:39 -0800 | [diff] [blame] | 63 | return _receiveCallback; |
wuchengli@chromium.org | 0d94c2f | 2013-08-12 14:20:49 +0000 | [diff] [blame] | 64 | } |
| 65 | |
Miguel Casas-Sanchez | 4765070 | 2015-05-29 17:21:40 -0700 | [diff] [blame] | 66 | int32_t VCMDecodedFrameCallback::Decoded(VideoFrame& decodedImage) { |
Tommi | 553c869 | 2020-05-05 15:35:45 +0200 | [diff] [blame] | 67 | // This function may be called on the decode TaskQueue, but may also be called |
| 68 | // on an OS provided queue such as on iOS (see e.g. b/153465112). |
Per | 327d8ba | 2015-11-10 14:00:27 +0100 | [diff] [blame] | 69 | return Decoded(decodedImage, -1); |
| 70 | } |
| 71 | |
| 72 | int32_t VCMDecodedFrameCallback::Decoded(VideoFrame& decodedImage, |
| 73 | int64_t decode_time_ms) { |
sakal | cc452e1 | 2017-02-09 04:53:45 -0800 | [diff] [blame] | 74 | Decoded(decodedImage, |
Danil Chapovalov | 0040b66 | 2018-06-18 10:48:16 +0200 | [diff] [blame] | 75 | decode_time_ms >= 0 ? absl::optional<int32_t>(decode_time_ms) |
| 76 | : absl::nullopt, |
| 77 | absl::nullopt); |
sakal | cc452e1 | 2017-02-09 04:53:45 -0800 | [diff] [blame] | 78 | return WEBRTC_VIDEO_CODEC_OK; |
| 79 | } |
| 80 | |
| 81 | void VCMDecodedFrameCallback::Decoded(VideoFrame& decodedImage, |
Danil Chapovalov | 0040b66 | 2018-06-18 10:48:16 +0200 | [diff] [blame] | 82 | absl::optional<int32_t> decode_time_ms, |
| 83 | absl::optional<uint8_t> qp) { |
Johannes Kron | 7ddea57 | 2019-09-11 12:00:22 +0200 | [diff] [blame] | 84 | // Wait some extra time to simulate a slow decoder. |
| 85 | if (_extra_decode_time) { |
| 86 | rtc::Thread::SleepMs(_extra_decode_time->ms()); |
| 87 | } |
| 88 | |
tommi | d0a71ba | 2017-03-14 04:16:20 -0700 | [diff] [blame] | 89 | RTC_DCHECK(_receiveCallback) << "Callback must not be null at this point"; |
philipel | 9d3ab61 | 2015-12-21 04:12:39 -0800 | [diff] [blame] | 90 | TRACE_EVENT_INSTANT1("webrtc", "VCMDecodedFrameCallback::Decoded", |
| 91 | "timestamp", decodedImage.timestamp()); |
| 92 | // TODO(holmer): We should improve this so that we can handle multiple |
| 93 | // callbacks from one call to Decode(). |
Johannes Kron | b6b782d | 2021-03-03 14:39:44 +0100 | [diff] [blame] | 94 | absl::optional<VCMFrameInformation> frameInfo; |
Johannes Kron | 111e981 | 2020-10-26 13:54:40 +0100 | [diff] [blame] | 95 | int timestamp_map_size = 0; |
Johannes Kron | fc5d276 | 2021-04-09 16:03:22 +0200 | [diff] [blame] | 96 | int dropped_frames = 0; |
Lu Liu | 352314a | 2018-02-21 19:38:59 +0000 | [diff] [blame] | 97 | { |
Markus Handell | 6deec38 | 2020-07-07 12:17:12 +0200 | [diff] [blame] | 98 | MutexLock lock(&lock_); |
Johannes Kron | fc5d276 | 2021-04-09 16:03:22 +0200 | [diff] [blame] | 99 | int initial_timestamp_map_size = _timestampMap.Size(); |
Lu Liu | 352314a | 2018-02-21 19:38:59 +0000 | [diff] [blame] | 100 | frameInfo = _timestampMap.Pop(decodedImage.timestamp()); |
Johannes Kron | 111e981 | 2020-10-26 13:54:40 +0100 | [diff] [blame] | 101 | timestamp_map_size = _timestampMap.Size(); |
Johannes Kron | fc5d276 | 2021-04-09 16:03:22 +0200 | [diff] [blame] | 102 | // _timestampMap.Pop() erases all frame upto the specified timestamp and |
| 103 | // return the frame info for this timestamp if it exists. Thus, the |
| 104 | // difference in the _timestampMap size before and after Pop() will show |
| 105 | // internally dropped frames. |
| 106 | dropped_frames = |
| 107 | initial_timestamp_map_size - timestamp_map_size - (frameInfo ? 1 : 0); |
| 108 | } |
| 109 | |
| 110 | if (dropped_frames > 0) { |
| 111 | _receiveCallback->OnDroppedFrames(dropped_frames); |
Lu Liu | 352314a | 2018-02-21 19:38:59 +0000 | [diff] [blame] | 112 | } |
philipel | 9d3ab61 | 2015-12-21 04:12:39 -0800 | [diff] [blame] | 113 | |
Johannes Kron | b6b782d | 2021-03-03 14:39:44 +0100 | [diff] [blame] | 114 | if (!frameInfo) { |
Mirko Bonadei | 675513b | 2017-11-09 11:09:25 +0100 | [diff] [blame] | 115 | RTC_LOG(LS_WARNING) << "Too many frames backed up in the decoder, dropping " |
Johannes Kron | ac82bd3 | 2021-06-17 10:50:56 +0200 | [diff] [blame] | 116 | "frame with timestamp " |
| 117 | << decodedImage.timestamp(); |
sakal | cc452e1 | 2017-02-09 04:53:45 -0800 | [diff] [blame] | 118 | return; |
philipel | 9d3ab61 | 2015-12-21 04:12:39 -0800 | [diff] [blame] | 119 | } |
| 120 | |
Ilya Nikolaevskiy | 2ebf523 | 2019-05-13 16:13:36 +0200 | [diff] [blame] | 121 | decodedImage.set_ntp_time_ms(frameInfo->ntp_time_ms); |
Chen Xing | f00bf42 | 2019-06-20 10:05:55 +0200 | [diff] [blame] | 122 | decodedImage.set_packet_infos(frameInfo->packet_infos); |
Ilya Nikolaevskiy | 2ebf523 | 2019-05-13 16:13:36 +0200 | [diff] [blame] | 123 | decodedImage.set_rotation(frameInfo->rotation); |
| 124 | |
Johannes Kron | 0093a38 | 2021-02-23 22:59:40 +0100 | [diff] [blame] | 125 | if (low_latency_renderer_enabled_) { |
Johannes Kron | 111e981 | 2020-10-26 13:54:40 +0100 | [diff] [blame] | 126 | absl::optional<int> max_composition_delay_in_frames = |
| 127 | _timing->MaxCompositionDelayInFrames(); |
| 128 | if (max_composition_delay_in_frames) { |
| 129 | // Subtract frames that are in flight. |
| 130 | if (low_latency_renderer_include_predecode_buffer_) { |
| 131 | *max_composition_delay_in_frames -= timestamp_map_size; |
| 132 | *max_composition_delay_in_frames = |
| 133 | std::max(0, *max_composition_delay_in_frames); |
| 134 | } |
| 135 | decodedImage.set_max_composition_delay_in_frames( |
| 136 | max_composition_delay_in_frames); |
| 137 | } |
| 138 | } |
| 139 | |
Johannes Kron | 05f8487 | 2020-01-16 14:09:33 +0100 | [diff] [blame] | 140 | RTC_DCHECK(frameInfo->decodeStart); |
philipel | 85ddb23 | 2020-10-02 14:46:14 +0200 | [diff] [blame] | 141 | const Timestamp now = _clock->CurrentTime(); |
| 142 | const TimeDelta decode_time = decode_time_ms |
| 143 | ? TimeDelta::Millis(*decode_time_ms) |
| 144 | : now - *frameInfo->decodeStart; |
| 145 | _timing->StopDecodeTimer(decode_time.ms(), now.ms()); |
| 146 | decodedImage.set_processing_time( |
| 147 | {*frameInfo->decodeStart, *frameInfo->decodeStart + decode_time}); |
philipel | 9d3ab61 | 2015-12-21 04:12:39 -0800 | [diff] [blame] | 148 | |
ilnik | 04f4d12 | 2017-06-19 07:18:55 -0700 | [diff] [blame] | 149 | // Report timing information. |
Benjamin Wright | 3f10ca8 | 2018-12-07 03:45:19 -0800 | [diff] [blame] | 150 | TimingFrameInfo timing_frame_info; |
Ilya Nikolaevskiy | b6c462d | 2018-06-05 15:21:32 +0200 | [diff] [blame] | 151 | if (frameInfo->timing.flags != VideoSendTiming::kInvalid) { |
ilnik | 2edc684 | 2017-07-06 03:06:50 -0700 | [diff] [blame] | 152 | int64_t capture_time_ms = decodedImage.ntp_time_ms() - ntp_offset_; |
ilnik | 04f4d12 | 2017-06-19 07:18:55 -0700 | [diff] [blame] | 153 | // Convert remote timestamps to local time from ntp timestamps. |
| 154 | frameInfo->timing.encode_start_ms -= ntp_offset_; |
| 155 | frameInfo->timing.encode_finish_ms -= ntp_offset_; |
| 156 | frameInfo->timing.packetization_finish_ms -= ntp_offset_; |
| 157 | frameInfo->timing.pacer_exit_ms -= ntp_offset_; |
| 158 | frameInfo->timing.network_timestamp_ms -= ntp_offset_; |
| 159 | frameInfo->timing.network2_timestamp_ms -= ntp_offset_; |
ilnik | 2edc684 | 2017-07-06 03:06:50 -0700 | [diff] [blame] | 160 | |
| 161 | int64_t sender_delta_ms = 0; |
| 162 | if (decodedImage.ntp_time_ms() < 0) { |
| 163 | // Sender clock is not estimated yet. Make sure that sender times are all |
| 164 | // negative to indicate that. Yet they still should be relatively correct. |
| 165 | sender_delta_ms = |
| 166 | std::max({capture_time_ms, frameInfo->timing.encode_start_ms, |
| 167 | frameInfo->timing.encode_finish_ms, |
| 168 | frameInfo->timing.packetization_finish_ms, |
| 169 | frameInfo->timing.pacer_exit_ms, |
| 170 | frameInfo->timing.network_timestamp_ms, |
| 171 | frameInfo->timing.network2_timestamp_ms}) + |
| 172 | 1; |
| 173 | } |
| 174 | |
ilnik | 2edc684 | 2017-07-06 03:06:50 -0700 | [diff] [blame] | 175 | timing_frame_info.capture_time_ms = capture_time_ms - sender_delta_ms; |
| 176 | timing_frame_info.encode_start_ms = |
| 177 | frameInfo->timing.encode_start_ms - sender_delta_ms; |
| 178 | timing_frame_info.encode_finish_ms = |
| 179 | frameInfo->timing.encode_finish_ms - sender_delta_ms; |
| 180 | timing_frame_info.packetization_finish_ms = |
| 181 | frameInfo->timing.packetization_finish_ms - sender_delta_ms; |
| 182 | timing_frame_info.pacer_exit_ms = |
| 183 | frameInfo->timing.pacer_exit_ms - sender_delta_ms; |
| 184 | timing_frame_info.network_timestamp_ms = |
| 185 | frameInfo->timing.network_timestamp_ms - sender_delta_ms; |
| 186 | timing_frame_info.network2_timestamp_ms = |
| 187 | frameInfo->timing.network2_timestamp_ms - sender_delta_ms; |
ilnik | 04f4d12 | 2017-06-19 07:18:55 -0700 | [diff] [blame] | 188 | } |
| 189 | |
Benjamin Wright | 3f10ca8 | 2018-12-07 03:45:19 -0800 | [diff] [blame] | 190 | timing_frame_info.flags = frameInfo->timing.flags; |
Johannes Kron | 05f8487 | 2020-01-16 14:09:33 +0100 | [diff] [blame] | 191 | timing_frame_info.decode_start_ms = frameInfo->decodeStart->ms(); |
| 192 | timing_frame_info.decode_finish_ms = now.ms(); |
Benjamin Wright | 3f10ca8 | 2018-12-07 03:45:19 -0800 | [diff] [blame] | 193 | timing_frame_info.render_time_ms = frameInfo->renderTimeMs; |
| 194 | timing_frame_info.rtp_timestamp = decodedImage.timestamp(); |
| 195 | timing_frame_info.receive_start_ms = frameInfo->timing.receive_start_ms; |
| 196 | timing_frame_info.receive_finish_ms = frameInfo->timing.receive_finish_ms; |
| 197 | _timing->SetTimingFrameInfo(timing_frame_info); |
| 198 | |
Yves Gerey | 665174f | 2018-06-19 15:03:05 +0200 | [diff] [blame] | 199 | decodedImage.set_timestamp_us(frameInfo->renderTimeMs * |
| 200 | rtc::kNumMicrosecsPerMillisec); |
philipel | 85ddb23 | 2020-10-02 14:46:14 +0200 | [diff] [blame] | 201 | _receiveCallback->FrameToRender(decodedImage, qp, decode_time.ms(), |
Johannes Kron | bfd343b | 2019-07-01 10:07:50 +0200 | [diff] [blame] | 202 | frameInfo->content_type); |
niklase@google.com | 470e71d | 2011-07-07 08:21:25 +0000 | [diff] [blame] | 203 | } |
| 204 | |
Peter Boström | b7d9a97 | 2015-12-18 16:01:11 +0100 | [diff] [blame] | 205 | void VCMDecodedFrameCallback::OnDecoderImplementationName( |
| 206 | const char* implementation_name) { |
tommi | d0a71ba | 2017-03-14 04:16:20 -0700 | [diff] [blame] | 207 | _receiveCallback->OnDecoderImplementationName(implementation_name); |
Peter Boström | b7d9a97 | 2015-12-18 16:01:11 +0100 | [diff] [blame] | 208 | } |
| 209 | |
pbos | 1968d3f | 2015-09-28 08:52:18 -0700 | [diff] [blame] | 210 | void VCMDecodedFrameCallback::Map(uint32_t timestamp, |
Johannes Kron | b6b782d | 2021-03-03 14:39:44 +0100 | [diff] [blame] | 211 | const VCMFrameInformation& frameInfo) { |
Johannes Kron | fc5d276 | 2021-04-09 16:03:22 +0200 | [diff] [blame] | 212 | int dropped_frames = 0; |
| 213 | { |
| 214 | MutexLock lock(&lock_); |
| 215 | int initial_size = _timestampMap.Size(); |
| 216 | _timestampMap.Add(timestamp, frameInfo); |
Artem Titov | dcd7fc7 | 2021-08-09 13:02:57 +0200 | [diff] [blame] | 217 | // If no frame is dropped, the new size should be `initial_size` + 1 |
Johannes Kron | fc5d276 | 2021-04-09 16:03:22 +0200 | [diff] [blame] | 218 | dropped_frames = (initial_size + 1) - _timestampMap.Size(); |
| 219 | } |
| 220 | if (dropped_frames > 0) { |
| 221 | _receiveCallback->OnDroppedFrames(dropped_frames); |
| 222 | } |
niklase@google.com | 470e71d | 2011-07-07 08:21:25 +0000 | [diff] [blame] | 223 | } |
| 224 | |
Johannes Kron | fc5d276 | 2021-04-09 16:03:22 +0200 | [diff] [blame] | 225 | void VCMDecodedFrameCallback::ClearTimestampMap() { |
| 226 | int dropped_frames = 0; |
| 227 | { |
| 228 | MutexLock lock(&lock_); |
| 229 | dropped_frames = _timestampMap.Size(); |
| 230 | _timestampMap.Clear(); |
Lu Liu | 352314a | 2018-02-21 19:38:59 +0000 | [diff] [blame] | 231 | } |
Johannes Kron | fc5d276 | 2021-04-09 16:03:22 +0200 | [diff] [blame] | 232 | if (dropped_frames > 0) { |
| 233 | _receiveCallback->OnDroppedFrames(dropped_frames); |
| 234 | } |
niklase@google.com | 470e71d | 2011-07-07 08:21:25 +0000 | [diff] [blame] | 235 | } |
| 236 | |
Danil Chapovalov | 7b78a31 | 2021-08-06 12:30:02 +0200 | [diff] [blame] | 237 | VCMGenericDecoder::VCMGenericDecoder(VideoDecoder* decoder) |
Peter Boström | 187db63 | 2015-12-01 17:20:01 +0100 | [diff] [blame] | 238 | : _callback(NULL), |
tommi | 5b7fc8c | 2017-07-05 16:45:57 -0700 | [diff] [blame] | 239 | decoder_(decoder), |
tommi | 5b7fc8c | 2017-07-05 16:45:57 -0700 | [diff] [blame] | 240 | _last_keyframe_content_type(VideoContentType::UNSPECIFIED) { |
| 241 | RTC_DCHECK(decoder_); |
| 242 | } |
niklase@google.com | 470e71d | 2011-07-07 08:21:25 +0000 | [diff] [blame] | 243 | |
tommi | 5b7fc8c | 2017-07-05 16:45:57 -0700 | [diff] [blame] | 244 | VCMGenericDecoder::~VCMGenericDecoder() { |
| 245 | decoder_->Release(); |
tommi | 5b7fc8c | 2017-07-05 16:45:57 -0700 | [diff] [blame] | 246 | } |
niklase@google.com | 470e71d | 2011-07-07 08:21:25 +0000 | [diff] [blame] | 247 | |
Danil Chapovalov | 355b8d2 | 2021-08-13 16:50:37 +0200 | [diff] [blame^] | 248 | bool VCMGenericDecoder::Configure(const VideoDecoder::Settings& settings) { |
| 249 | TRACE_EVENT0("webrtc", "VCMGenericDecoder::Configure"); |
niklase@google.com | 470e71d | 2011-07-07 08:21:25 +0000 | [diff] [blame] | 250 | |
Danil Chapovalov | 355b8d2 | 2021-08-13 16:50:37 +0200 | [diff] [blame^] | 251 | bool ok = decoder_->Configure(settings); |
Erik Språng | c12f625 | 2021-01-13 21:49:59 +0100 | [diff] [blame] | 252 | decoder_info_ = decoder_->GetDecoderInfo(); |
| 253 | RTC_LOG(LS_INFO) << "Decoder implementation: " << decoder_info_.ToString(); |
| 254 | if (_callback) { |
| 255 | _callback->OnDecoderImplementationName( |
| 256 | decoder_info_.implementation_name.c_str()); |
| 257 | } |
Danil Chapovalov | 355b8d2 | 2021-08-13 16:50:37 +0200 | [diff] [blame^] | 258 | return ok; |
niklase@google.com | 470e71d | 2011-07-07 08:21:25 +0000 | [diff] [blame] | 259 | } |
| 260 | |
Johannes Kron | 05f8487 | 2020-01-16 14:09:33 +0100 | [diff] [blame] | 261 | int32_t VCMGenericDecoder::Decode(const VCMEncodedFrame& frame, Timestamp now) { |
Yves Gerey | 665174f | 2018-06-19 15:03:05 +0200 | [diff] [blame] | 262 | TRACE_EVENT1("webrtc", "VCMGenericDecoder::Decode", "timestamp", |
Niels Möller | 2377588 | 2018-08-16 10:24:12 +0200 | [diff] [blame] | 263 | frame.Timestamp()); |
Johannes Kron | b6b782d | 2021-03-03 14:39:44 +0100 | [diff] [blame] | 264 | VCMFrameInformation frame_info; |
| 265 | frame_info.decodeStart = now; |
| 266 | frame_info.renderTimeMs = frame.RenderTimeMs(); |
| 267 | frame_info.rotation = frame.rotation(); |
| 268 | frame_info.timing = frame.video_timing(); |
| 269 | frame_info.ntp_time_ms = frame.EncodedImage().ntp_time_ms_; |
| 270 | frame_info.packet_infos = frame.PacketInfos(); |
Ilya Nikolaevskiy | 2ebf523 | 2019-05-13 16:13:36 +0200 | [diff] [blame] | 271 | |
Yves Gerey | 665174f | 2018-06-19 15:03:05 +0200 | [diff] [blame] | 272 | // Set correctly only for key frames. Thus, use latest key frame |
| 273 | // content type. If the corresponding key frame was lost, decode will fail |
| 274 | // and content type will be ignored. |
Niels Möller | 8f7ce22 | 2019-03-21 15:43:58 +0100 | [diff] [blame] | 275 | if (frame.FrameType() == VideoFrameType::kVideoFrameKey) { |
Johannes Kron | b6b782d | 2021-03-03 14:39:44 +0100 | [diff] [blame] | 276 | frame_info.content_type = frame.contentType(); |
Yves Gerey | 665174f | 2018-06-19 15:03:05 +0200 | [diff] [blame] | 277 | _last_keyframe_content_type = frame.contentType(); |
| 278 | } else { |
Johannes Kron | b6b782d | 2021-03-03 14:39:44 +0100 | [diff] [blame] | 279 | frame_info.content_type = _last_keyframe_content_type; |
Yves Gerey | 665174f | 2018-06-19 15:03:05 +0200 | [diff] [blame] | 280 | } |
Johannes Kron | b6b782d | 2021-03-03 14:39:44 +0100 | [diff] [blame] | 281 | _callback->Map(frame.Timestamp(), frame_info); |
niklase@google.com | 470e71d | 2011-07-07 08:21:25 +0000 | [diff] [blame] | 282 | |
Yves Gerey | 665174f | 2018-06-19 15:03:05 +0200 | [diff] [blame] | 283 | int32_t ret = decoder_->Decode(frame.EncodedImage(), frame.MissingFrame(), |
Niels Möller | 7aacdd9 | 2019-03-25 09:11:40 +0100 | [diff] [blame] | 284 | frame.RenderTimeMs()); |
Erik Språng | c12f625 | 2021-01-13 21:49:59 +0100 | [diff] [blame] | 285 | VideoDecoder::DecoderInfo decoder_info = decoder_->GetDecoderInfo(); |
| 286 | if (decoder_info != decoder_info_) { |
Ilya Nikolaevskiy | 43c108b | 2020-05-15 12:24:29 +0200 | [diff] [blame] | 287 | RTC_LOG(LS_INFO) << "Changed decoder implementation to: " |
Erik Språng | c12f625 | 2021-01-13 21:49:59 +0100 | [diff] [blame] | 288 | << decoder_info.ToString(); |
Erik Språng | c80f955 | 2021-03-12 16:36:49 +0100 | [diff] [blame] | 289 | decoder_info_ = decoder_info; |
Erik Språng | c12f625 | 2021-01-13 21:49:59 +0100 | [diff] [blame] | 290 | _callback->OnDecoderImplementationName( |
| 291 | decoder_info.implementation_name.empty() |
| 292 | ? "unknown" |
| 293 | : decoder_info.implementation_name.c_str()); |
Ilya Nikolaevskiy | 43c108b | 2020-05-15 12:24:29 +0200 | [diff] [blame] | 294 | } |
Yves Gerey | 665174f | 2018-06-19 15:03:05 +0200 | [diff] [blame] | 295 | if (ret < WEBRTC_VIDEO_CODEC_OK) { |
| 296 | RTC_LOG(LS_WARNING) << "Failed to decode frame with timestamp " |
Niels Möller | 2377588 | 2018-08-16 10:24:12 +0200 | [diff] [blame] | 297 | << frame.Timestamp() << ", error code: " << ret; |
Johannes Kron | fc5d276 | 2021-04-09 16:03:22 +0200 | [diff] [blame] | 298 | _callback->ClearTimestampMap(); |
Niels Möller | 9cccf85 | 2018-12-04 11:01:26 +0100 | [diff] [blame] | 299 | } else if (ret == WEBRTC_VIDEO_CODEC_NO_OUTPUT) { |
Johannes Kron | fc5d276 | 2021-04-09 16:03:22 +0200 | [diff] [blame] | 300 | // No output. |
| 301 | _callback->ClearTimestampMap(); |
Yves Gerey | 665174f | 2018-06-19 15:03:05 +0200 | [diff] [blame] | 302 | } |
| 303 | return ret; |
guidou | c337258 | 2017-04-04 07:16:21 -0700 | [diff] [blame] | 304 | } |
niklase@google.com | 470e71d | 2011-07-07 08:21:25 +0000 | [diff] [blame] | 305 | |
Peter Boström | 187db63 | 2015-12-01 17:20:01 +0100 | [diff] [blame] | 306 | int32_t VCMGenericDecoder::RegisterDecodeCompleteCallback( |
| 307 | VCMDecodedFrameCallback* callback) { |
| 308 | _callback = callback; |
Erik Språng | c12f625 | 2021-01-13 21:49:59 +0100 | [diff] [blame] | 309 | int32_t ret = decoder_->RegisterDecodeCompleteCallback(callback); |
| 310 | if (callback && !decoder_info_.implementation_name.empty()) { |
| 311 | callback->OnDecoderImplementationName( |
| 312 | decoder_info_.implementation_name.c_str()); |
| 313 | } |
| 314 | return ret; |
niklase@google.com | 470e71d | 2011-07-07 08:21:25 +0000 | [diff] [blame] | 315 | } |
| 316 | |
philipel | 9d3ab61 | 2015-12-21 04:12:39 -0800 | [diff] [blame] | 317 | } // namespace webrtc |