You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

262 lines
9.9 KiB

​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
  1. /*
  2. * Copyright (c) 2012 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. #include "modules/video_coding/generic_decoder.h"
  11. #include <stddef.h>
  12. #include <algorithm>
  13. #include "api/video/video_timing.h"
  14. #include "modules/video_coding/include/video_error_codes.h"
  15. #include "rtc_base/checks.h"
  16. #include "rtc_base/logging.h"
  17. #include "rtc_base/thread.h"
  18. #include "rtc_base/time_utils.h"
  19. #include "rtc_base/trace_event.h"
  20. #include "system_wrappers/include/clock.h"
  21. #include "system_wrappers/include/field_trial.h"
  22. namespace webrtc {
  23. VCMDecodedFrameCallback::VCMDecodedFrameCallback(VCMTiming* timing,
  24. Clock* clock)
  25. : _clock(clock),
  26. _timing(timing),
  27. _timestampMap(kDecoderFrameMemoryLength),
  28. _extra_decode_time("t", absl::nullopt) {
  29. ntp_offset_ =
  30. _clock->CurrentNtpInMilliseconds() - _clock->TimeInMilliseconds();
  31. ParseFieldTrial({&_extra_decode_time},
  32. field_trial::FindFullName("WebRTC-SlowDownDecoder"));
  33. }
  34. VCMDecodedFrameCallback::~VCMDecodedFrameCallback() {}
  35. void VCMDecodedFrameCallback::SetUserReceiveCallback(
  36. VCMReceiveCallback* receiveCallback) {
  37. RTC_DCHECK(construction_thread_.IsCurrent());
  38. RTC_DCHECK((!_receiveCallback && receiveCallback) ||
  39. (_receiveCallback && !receiveCallback));
  40. _receiveCallback = receiveCallback;
  41. }
  42. VCMReceiveCallback* VCMDecodedFrameCallback::UserReceiveCallback() {
  43. // Called on the decode thread via VCMCodecDataBase::GetDecoder.
  44. // The callback must always have been set before this happens.
  45. RTC_DCHECK(_receiveCallback);
  46. return _receiveCallback;
  47. }
  48. int32_t VCMDecodedFrameCallback::Decoded(VideoFrame& decodedImage) {
  49. return Decoded(decodedImage, -1);
  50. }
  51. int32_t VCMDecodedFrameCallback::Decoded(VideoFrame& decodedImage,
  52. int64_t decode_time_ms) {
  53. Decoded(decodedImage,
  54. decode_time_ms >= 0 ? absl::optional<int32_t>(decode_time_ms)
  55. : absl::nullopt,
  56. absl::nullopt);
  57. return WEBRTC_VIDEO_CODEC_OK;
  58. }
  59. void VCMDecodedFrameCallback::Decoded(VideoFrame& decodedImage,
  60. absl::optional<int32_t> decode_time_ms,
  61. absl::optional<uint8_t> qp) {
  62. // Wait some extra time to simulate a slow decoder.
  63. if (_extra_decode_time) {
  64. rtc::Thread::SleepMs(_extra_decode_time->ms());
  65. }
  66. RTC_DCHECK(_receiveCallback) << "Callback must not be null at this point";
  67. TRACE_EVENT_INSTANT1("webrtc", "VCMDecodedFrameCallback::Decoded",
  68. "timestamp", decodedImage.timestamp());
  69. // TODO(holmer): We should improve this so that we can handle multiple
  70. // callbacks from one call to Decode().
  71. VCMFrameInformation* frameInfo;
  72. {
  73. rtc::CritScope cs(&lock_);
  74. frameInfo = _timestampMap.Pop(decodedImage.timestamp());
  75. }
  76. if (frameInfo == NULL) {
  77. RTC_LOG(LS_WARNING) << "Too many frames backed up in the decoder, dropping "
  78. "this one.";
  79. _receiveCallback->OnDroppedFrames(1);
  80. return;
  81. }
  82. decodedImage.set_ntp_time_ms(frameInfo->ntp_time_ms);
  83. decodedImage.set_packet_infos(frameInfo->packet_infos);
  84. decodedImage.set_rotation(frameInfo->rotation);
  85. const int64_t now_ms = _clock->TimeInMilliseconds();
  86. if (!decode_time_ms) {
  87. decode_time_ms = now_ms - frameInfo->decodeStartTimeMs;
  88. }
  89. _timing->StopDecodeTimer(*decode_time_ms, now_ms);
  90. // Report timing information.
  91. TimingFrameInfo timing_frame_info;
  92. if (frameInfo->timing.flags != VideoSendTiming::kInvalid) {
  93. int64_t capture_time_ms = decodedImage.ntp_time_ms() - ntp_offset_;
  94. // Convert remote timestamps to local time from ntp timestamps.
  95. frameInfo->timing.encode_start_ms -= ntp_offset_;
  96. frameInfo->timing.encode_finish_ms -= ntp_offset_;
  97. frameInfo->timing.packetization_finish_ms -= ntp_offset_;
  98. frameInfo->timing.pacer_exit_ms -= ntp_offset_;
  99. frameInfo->timing.network_timestamp_ms -= ntp_offset_;
  100. frameInfo->timing.network2_timestamp_ms -= ntp_offset_;
  101. int64_t sender_delta_ms = 0;
  102. if (decodedImage.ntp_time_ms() < 0) {
  103. // Sender clock is not estimated yet. Make sure that sender times are all
  104. // negative to indicate that. Yet they still should be relatively correct.
  105. sender_delta_ms =
  106. std::max({capture_time_ms, frameInfo->timing.encode_start_ms,
  107. frameInfo->timing.encode_finish_ms,
  108. frameInfo->timing.packetization_finish_ms,
  109. frameInfo->timing.pacer_exit_ms,
  110. frameInfo->timing.network_timestamp_ms,
  111. frameInfo->timing.network2_timestamp_ms}) +
  112. 1;
  113. }
  114. timing_frame_info.capture_time_ms = capture_time_ms - sender_delta_ms;
  115. timing_frame_info.encode_start_ms =
  116. frameInfo->timing.encode_start_ms - sender_delta_ms;
  117. timing_frame_info.encode_finish_ms =
  118. frameInfo->timing.encode_finish_ms - sender_delta_ms;
  119. timing_frame_info.packetization_finish_ms =
  120. frameInfo->timing.packetization_finish_ms - sender_delta_ms;
  121. timing_frame_info.pacer_exit_ms =
  122. frameInfo->timing.pacer_exit_ms - sender_delta_ms;
  123. timing_frame_info.network_timestamp_ms =
  124. frameInfo->timing.network_timestamp_ms - sender_delta_ms;
  125. timing_frame_info.network2_timestamp_ms =
  126. frameInfo->timing.network2_timestamp_ms - sender_delta_ms;
  127. }
  128. timing_frame_info.flags = frameInfo->timing.flags;
  129. timing_frame_info.decode_start_ms = frameInfo->decodeStartTimeMs;
  130. timing_frame_info.decode_finish_ms = now_ms;
  131. timing_frame_info.render_time_ms = frameInfo->renderTimeMs;
  132. timing_frame_info.rtp_timestamp = decodedImage.timestamp();
  133. timing_frame_info.receive_start_ms = frameInfo->timing.receive_start_ms;
  134. timing_frame_info.receive_finish_ms = frameInfo->timing.receive_finish_ms;
  135. _timing->SetTimingFrameInfo(timing_frame_info);
  136. decodedImage.set_timestamp_us(frameInfo->renderTimeMs *
  137. rtc::kNumMicrosecsPerMillisec);
  138. _receiveCallback->FrameToRender(decodedImage, qp, *decode_time_ms,
  139. frameInfo->content_type);
  140. }
  141. void VCMDecodedFrameCallback::OnDecoderImplementationName(
  142. const char* implementation_name) {
  143. _receiveCallback->OnDecoderImplementationName(implementation_name);
  144. }
  145. void VCMDecodedFrameCallback::Map(uint32_t timestamp,
  146. VCMFrameInformation* frameInfo) {
  147. rtc::CritScope cs(&lock_);
  148. _timestampMap.Add(timestamp, frameInfo);
  149. }
  150. int32_t VCMDecodedFrameCallback::Pop(uint32_t timestamp) {
  151. rtc::CritScope cs(&lock_);
  152. if (_timestampMap.Pop(timestamp) == NULL) {
  153. return VCM_GENERAL_ERROR;
  154. }
  155. _receiveCallback->OnDroppedFrames(1);
  156. return VCM_OK;
  157. }
  158. VCMGenericDecoder::VCMGenericDecoder(std::unique_ptr<VideoDecoder> decoder)
  159. : VCMGenericDecoder(decoder.release(), false /* isExternal */) {}
  160. VCMGenericDecoder::VCMGenericDecoder(VideoDecoder* decoder, bool isExternal)
  161. : _callback(NULL),
  162. _frameInfos(),
  163. _nextFrameInfoIdx(0),
  164. decoder_(decoder),
  165. _codecType(kVideoCodecGeneric),
  166. _isExternal(isExternal),
  167. _last_keyframe_content_type(VideoContentType::UNSPECIFIED) {
  168. RTC_DCHECK(decoder_);
  169. }
  170. VCMGenericDecoder::~VCMGenericDecoder() {
  171. decoder_->Release();
  172. if (_isExternal)
  173. decoder_.release();
  174. RTC_DCHECK(_isExternal || decoder_);
  175. }
  176. int32_t VCMGenericDecoder::InitDecode(const VideoCodec* settings,
  177. int32_t numberOfCores) {
  178. TRACE_EVENT0("webrtc", "VCMGenericDecoder::InitDecode");
  179. _codecType = settings->codecType;
  180. return decoder_->InitDecode(settings, numberOfCores);
  181. }
  182. int32_t VCMGenericDecoder::Decode(const VCMEncodedFrame& frame, int64_t nowMs) {
  183. TRACE_EVENT1("webrtc", "VCMGenericDecoder::Decode", "timestamp",
  184. frame.Timestamp());
  185. _frameInfos[_nextFrameInfoIdx].decodeStartTimeMs = nowMs;
  186. _frameInfos[_nextFrameInfoIdx].renderTimeMs = frame.RenderTimeMs();
  187. _frameInfos[_nextFrameInfoIdx].rotation = frame.rotation();
  188. _frameInfos[_nextFrameInfoIdx].timing = frame.video_timing();
  189. _frameInfos[_nextFrameInfoIdx].ntp_time_ms =
  190. frame.EncodedImage().ntp_time_ms_;
  191. _frameInfos[_nextFrameInfoIdx].packet_infos = frame.PacketInfos();
  192. // Set correctly only for key frames. Thus, use latest key frame
  193. // content type. If the corresponding key frame was lost, decode will fail
  194. // and content type will be ignored.
  195. if (frame.FrameType() == VideoFrameType::kVideoFrameKey) {
  196. _frameInfos[_nextFrameInfoIdx].content_type = frame.contentType();
  197. _last_keyframe_content_type = frame.contentType();
  198. } else {
  199. _frameInfos[_nextFrameInfoIdx].content_type = _last_keyframe_content_type;
  200. }
  201. _callback->Map(frame.Timestamp(), &_frameInfos[_nextFrameInfoIdx]);
  202. _nextFrameInfoIdx = (_nextFrameInfoIdx + 1) % kDecoderFrameMemoryLength;
  203. int32_t ret = decoder_->Decode(frame.EncodedImage(), frame.MissingFrame(),
  204. frame.RenderTimeMs());
  205. _callback->OnDecoderImplementationName(decoder_->ImplementationName());
  206. if (ret < WEBRTC_VIDEO_CODEC_OK) {
  207. RTC_LOG(LS_WARNING) << "Failed to decode frame with timestamp "
  208. << frame.Timestamp() << ", error code: " << ret;
  209. _callback->Pop(frame.Timestamp());
  210. return ret;
  211. } else if (ret == WEBRTC_VIDEO_CODEC_NO_OUTPUT) {
  212. // No output
  213. _callback->Pop(frame.Timestamp());
  214. }
  215. return ret;
  216. }
  217. int32_t VCMGenericDecoder::RegisterDecodeCompleteCallback(
  218. VCMDecodedFrameCallback* callback) {
  219. _callback = callback;
  220. return decoder_->RegisterDecodeCompleteCallback(callback);
  221. }
  222. bool VCMGenericDecoder::PrefersLateDecoding() const {
  223. return decoder_->PrefersLateDecoding();
  224. }
  225. } // namespace webrtc