blob: 53fce37de13840cbb5c9a107a86be115356d7296 [file] [log] [blame]
pbos@webrtc.org0181b5f2013-09-09 08:26:301/*
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
Mirko Bonadei92ea95e2017-09-15 04:47:3111#include "test/fake_decoder.h"
pbos@webrtc.org0181b5f2013-09-09 08:26:3012
Yves Gerey3e707812018-11-28 15:47:4913#include <string.h>
14
Mirko Bonadei317a1f02019-09-17 15:06:1815#include <memory>
16
Mirko Bonadeid9708072019-01-25 19:26:4817#include "api/scoped_refptr.h"
Mirko Bonadei92ea95e2017-09-15 04:47:3118#include "api/video/i420_buffer.h"
Yves Gerey3e707812018-11-28 15:47:4919#include "api/video/video_frame.h"
20#include "api/video/video_frame_buffer.h"
21#include "api/video/video_rotation.h"
22#include "modules/video_coding/include/video_error_codes.h"
23#include "rtc_base/checks.h"
Ilya Nikolaevskiy2ebf5232019-05-13 14:13:3624#include "rtc_base/task_queue.h"
Steve Anton10542f22019-01-11 17:11:0025#include "rtc_base/time_utils.h"
pbos@webrtc.org0181b5f2013-09-09 08:26:3026
27namespace webrtc {
28namespace test {
29
Ilya Nikolaevskiy2ebf5232019-05-13 14:13:3630FakeDecoder::FakeDecoder() : FakeDecoder(nullptr) {}
31
32FakeDecoder::FakeDecoder(TaskQueueFactory* task_queue_factory)
33 : callback_(nullptr),
34 width_(kDefaultWidth),
35 height_(kDefaultHeight),
36 task_queue_factory_(task_queue_factory),
37 decode_delay_ms_(0) {}
pbos@webrtc.org0181b5f2013-09-09 08:26:3038
Danil Chapovalovd08930d2021-08-12 11:26:5539bool FakeDecoder::Configure(const Settings& settings) {
40 return true;
pbos@webrtc.org0181b5f2013-09-09 08:26:3041}
42
43int32_t FakeDecoder::Decode(const EncodedImage& input,
44 bool missing_frames,
pbos@webrtc.org0181b5f2013-09-09 08:26:3045 int64_t render_time_ms) {
philipel0e075722018-04-05 11:04:4246 if (input._encodedWidth > 0 && input._encodedHeight > 0) {
47 width_ = input._encodedWidth;
48 height_ = input._encodedHeight;
49 }
50
Ilya Nikolaevskiy2ebf5232019-05-13 14:13:3651 rtc::scoped_refptr<I420Buffer> buffer = I420Buffer::Create(width_, height_);
Niels Möller3c4f9c12022-04-22 09:14:3452 I420Buffer::SetBlack(buffer.get());
Ilya Nikolaevskiy2ebf5232019-05-13 14:13:3653 VideoFrame frame = VideoFrame::Builder()
54 .set_video_frame_buffer(buffer)
55 .set_rotation(webrtc::kVideoRotation_0)
56 .set_timestamp_ms(render_time_ms)
57 .build();
Niels Möller23775882018-08-16 08:24:1258 frame.set_timestamp(input.Timestamp());
Artem Titarenko84ae2b62019-04-24 12:56:3659 frame.set_ntp_time_ms(input.ntp_time_ms_);
pbos@webrtc.org0181b5f2013-09-09 08:26:3060
Ilya Nikolaevskiy2ebf5232019-05-13 14:13:3661 if (decode_delay_ms_ == 0 || !task_queue_) {
62 callback_->Decoded(frame);
63 } else {
Danil Chapovalov9c125c62022-07-07 18:29:3064 task_queue_->PostDelayedHighPrecisionTask(
65 [frame, this]() {
66 VideoFrame copy = frame;
67 callback_->Decoded(copy);
68 },
69 TimeDelta::Millis(decode_delay_ms_));
Ilya Nikolaevskiy2ebf5232019-05-13 14:13:3670 }
pbos@webrtc.org0181b5f2013-09-09 08:26:3071
72 return WEBRTC_VIDEO_CODEC_OK;
73}
74
Ilya Nikolaevskiy2ebf5232019-05-13 14:13:3675void FakeDecoder::SetDelayedDecoding(int decode_delay_ms) {
76 RTC_CHECK(task_queue_factory_);
77 if (!task_queue_) {
Danil Chapovalov9c125c62022-07-07 18:29:3078 task_queue_ = task_queue_factory_->CreateTaskQueue(
79 "fake_decoder", TaskQueueFactory::Priority::NORMAL);
Ilya Nikolaevskiy2ebf5232019-05-13 14:13:3680 }
81 decode_delay_ms_ = decode_delay_ms;
82}
83
pbos@webrtc.org0181b5f2013-09-09 08:26:3084int32_t FakeDecoder::RegisterDecodeCompleteCallback(
85 DecodedImageCallback* callback) {
86 callback_ = callback;
87 return WEBRTC_VIDEO_CODEC_OK;
88}
89
90int32_t FakeDecoder::Release() {
91 return WEBRTC_VIDEO_CODEC_OK;
92}
Peter Boströmb7d9a972015-12-18 15:01:1193
Peter Boströmb7d9a972015-12-18 15:01:1194const char* FakeDecoder::kImplementationName = "fake_decoder";
Erik Språngc12f6252021-01-13 20:49:5995VideoDecoder::DecoderInfo FakeDecoder::GetDecoderInfo() const {
96 DecoderInfo info;
97 info.implementation_name = kImplementationName;
Evan Shrubsole09da10e2022-10-14 14:38:3198 info.is_hardware_accelerated = true;
Erik Språngc12f6252021-01-13 20:49:5999 return info;
100}
Peter Boströmb7d9a972015-12-18 15:01:11101const char* FakeDecoder::ImplementationName() const {
102 return kImplementationName;
103}
104
stefan@webrtc.org79c33592014-08-06 09:24:53105int32_t FakeH264Decoder::Decode(const EncodedImage& input,
106 bool missing_frames,
stefan@webrtc.org79c33592014-08-06 09:24:53107 int64_t render_time_ms) {
108 uint8_t value = 0;
Niels Möller77536a22019-01-15 07:50:01109 for (size_t i = 0; i < input.size(); ++i) {
stefan@webrtc.org79c33592014-08-06 09:24:53110 uint8_t kStartCode[] = {0, 0, 0, 1};
Niels Möller77536a22019-01-15 07:50:01111 if (i < input.size() - sizeof(kStartCode) &&
Niels Möller24871e42019-01-17 10:31:13112 !memcmp(&input.data()[i], kStartCode, sizeof(kStartCode))) {
stefan@webrtc.org79c33592014-08-06 09:24:53113 i += sizeof(kStartCode) + 1; // Skip start code and NAL header.
114 }
Niels Möller24871e42019-01-17 10:31:13115 if (input.data()[i] != value) {
116 RTC_CHECK_EQ(value, input.data()[i])
stefan@webrtc.org79c33592014-08-06 09:24:53117 << "Bitstream mismatch between sender and receiver.";
118 return -1;
119 }
120 ++value;
121 }
Niels Möller7aacdd92019-03-25 08:11:40122 return FakeDecoder::Decode(input, missing_frames, render_time_ms);
stefan@webrtc.org79c33592014-08-06 09:24:53123}
124
pbos@webrtc.org0181b5f2013-09-09 08:26:30125} // namespace test
126} // namespace webrtc