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.

127 lines
3.8 KiB

​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
  1. /*
  2. * Copyright (c) 2013 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 "test/fake_decoder.h"
  11. #include <string.h>
  12. #include <memory>
  13. #include "api/scoped_refptr.h"
  14. #include "api/video/i420_buffer.h"
  15. #include "api/video/video_frame.h"
  16. #include "api/video/video_frame_buffer.h"
  17. #include "api/video/video_rotation.h"
  18. #include "modules/video_coding/include/video_error_codes.h"
  19. #include "rtc_base/checks.h"
  20. #include "rtc_base/task_queue.h"
  21. #include "rtc_base/time_utils.h"
  22. namespace webrtc {
  23. namespace test {
  24. namespace {
  25. const int kDefaultWidth = 320;
  26. const int kDefaultHeight = 180;
  27. } // namespace
  28. FakeDecoder::FakeDecoder() : FakeDecoder(nullptr) {}
  29. FakeDecoder::FakeDecoder(TaskQueueFactory* task_queue_factory)
  30. : callback_(nullptr),
  31. width_(kDefaultWidth),
  32. height_(kDefaultHeight),
  33. task_queue_factory_(task_queue_factory),
  34. decode_delay_ms_(0) {}
  35. int32_t FakeDecoder::InitDecode(const VideoCodec* config,
  36. int32_t number_of_cores) {
  37. return WEBRTC_VIDEO_CODEC_OK;
  38. }
  39. int32_t FakeDecoder::Decode(const EncodedImage& input,
  40. bool missing_frames,
  41. int64_t render_time_ms) {
  42. if (input._encodedWidth > 0 && input._encodedHeight > 0) {
  43. width_ = input._encodedWidth;
  44. height_ = input._encodedHeight;
  45. }
  46. rtc::scoped_refptr<I420Buffer> buffer = I420Buffer::Create(width_, height_);
  47. I420Buffer::SetBlack(buffer);
  48. VideoFrame frame = VideoFrame::Builder()
  49. .set_video_frame_buffer(buffer)
  50. .set_rotation(webrtc::kVideoRotation_0)
  51. .set_timestamp_ms(render_time_ms)
  52. .build();
  53. frame.set_timestamp(input.Timestamp());
  54. frame.set_ntp_time_ms(input.ntp_time_ms_);
  55. if (decode_delay_ms_ == 0 || !task_queue_) {
  56. callback_->Decoded(frame);
  57. } else {
  58. task_queue_->PostDelayedTask(
  59. [frame, this]() {
  60. VideoFrame copy = frame;
  61. callback_->Decoded(copy);
  62. },
  63. decode_delay_ms_);
  64. }
  65. return WEBRTC_VIDEO_CODEC_OK;
  66. }
  67. void FakeDecoder::SetDelayedDecoding(int decode_delay_ms) {
  68. RTC_CHECK(task_queue_factory_);
  69. if (!task_queue_) {
  70. task_queue_ =
  71. std::make_unique<rtc::TaskQueue>(task_queue_factory_->CreateTaskQueue(
  72. "fake_decoder", TaskQueueFactory::Priority::NORMAL));
  73. }
  74. decode_delay_ms_ = decode_delay_ms;
  75. }
  76. int32_t FakeDecoder::RegisterDecodeCompleteCallback(
  77. DecodedImageCallback* callback) {
  78. callback_ = callback;
  79. return WEBRTC_VIDEO_CODEC_OK;
  80. }
  81. int32_t FakeDecoder::Release() {
  82. return WEBRTC_VIDEO_CODEC_OK;
  83. }
  84. const char* FakeDecoder::kImplementationName = "fake_decoder";
  85. const char* FakeDecoder::ImplementationName() const {
  86. return kImplementationName;
  87. }
  88. int32_t FakeH264Decoder::Decode(const EncodedImage& input,
  89. bool missing_frames,
  90. int64_t render_time_ms) {
  91. uint8_t value = 0;
  92. for (size_t i = 0; i < input.size(); ++i) {
  93. uint8_t kStartCode[] = {0, 0, 0, 1};
  94. if (i < input.size() - sizeof(kStartCode) &&
  95. !memcmp(&input.data()[i], kStartCode, sizeof(kStartCode))) {
  96. i += sizeof(kStartCode) + 1; // Skip start code and NAL header.
  97. }
  98. if (input.data()[i] != value) {
  99. RTC_CHECK_EQ(value, input.data()[i])
  100. << "Bitstream mismatch between sender and receiver.";
  101. return -1;
  102. }
  103. ++value;
  104. }
  105. return FakeDecoder::Decode(input, missing_frames, render_time_ms);
  106. }
  107. } // namespace test
  108. } // namespace webrtc