blob: e80c31cc40b425e6de5f19f2ddb0cb73cfcfceeb [file] [log] [blame]
pbos@webrtc.org0181b5f2013-09-09 08:26:30 +00001/*
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 06:47:31 +020011#include "test/fake_decoder.h"
pbos@webrtc.org0181b5f2013-09-09 08:26:30 +000012
Yves Gerey3e707812018-11-28 16:47:49 +010013#include <string.h>
14
Mirko Bonadei317a1f02019-09-17 17:06:18 +020015#include <memory>
16
Mirko Bonadeid9708072019-01-25 20:26:48 +010017#include "api/scoped_refptr.h"
Mirko Bonadei92ea95e2017-09-15 06:47:31 +020018#include "api/video/i420_buffer.h"
Yves Gerey3e707812018-11-28 16:47:49 +010019#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 16:13:36 +020024#include "rtc_base/task_queue.h"
Steve Anton10542f22019-01-11 09:11:00 -080025#include "rtc_base/time_utils.h"
pbos@webrtc.org0181b5f2013-09-09 08:26:30 +000026
27namespace webrtc {
28namespace test {
29
Ilya Nikolaevskiyb0588e62018-08-27 14:12:27 +020030namespace {
31const int kDefaultWidth = 320;
32const int kDefaultHeight = 180;
33} // namespace
34
Ilya Nikolaevskiy2ebf5232019-05-13 16:13:36 +020035FakeDecoder::FakeDecoder() : FakeDecoder(nullptr) {}
36
37FakeDecoder::FakeDecoder(TaskQueueFactory* task_queue_factory)
38 : callback_(nullptr),
39 width_(kDefaultWidth),
40 height_(kDefaultHeight),
41 task_queue_factory_(task_queue_factory),
42 decode_delay_ms_(0) {}
pbos@webrtc.org0181b5f2013-09-09 08:26:30 +000043
44int32_t FakeDecoder::InitDecode(const VideoCodec* config,
45 int32_t number_of_cores) {
pbos@webrtc.org0181b5f2013-09-09 08:26:30 +000046 return WEBRTC_VIDEO_CODEC_OK;
47}
48
49int32_t FakeDecoder::Decode(const EncodedImage& input,
50 bool missing_frames,
pbos@webrtc.org0181b5f2013-09-09 08:26:30 +000051 int64_t render_time_ms) {
philipel0e075722018-04-05 13:04:42 +020052 if (input._encodedWidth > 0 && input._encodedHeight > 0) {
53 width_ = input._encodedWidth;
54 height_ = input._encodedHeight;
55 }
56
Ilya Nikolaevskiy2ebf5232019-05-13 16:13:36 +020057 rtc::scoped_refptr<I420Buffer> buffer = I420Buffer::Create(width_, height_);
58 I420Buffer::SetBlack(buffer);
59 VideoFrame frame = VideoFrame::Builder()
60 .set_video_frame_buffer(buffer)
61 .set_rotation(webrtc::kVideoRotation_0)
62 .set_timestamp_ms(render_time_ms)
63 .build();
Niels Möller23775882018-08-16 10:24:12 +020064 frame.set_timestamp(input.Timestamp());
Artem Titarenko84ae2b62019-04-24 12:56:36 +000065 frame.set_ntp_time_ms(input.ntp_time_ms_);
pbos@webrtc.org0181b5f2013-09-09 08:26:30 +000066
Ilya Nikolaevskiy2ebf5232019-05-13 16:13:36 +020067 if (decode_delay_ms_ == 0 || !task_queue_) {
68 callback_->Decoded(frame);
69 } else {
70 task_queue_->PostDelayedTask(
71 [frame, this]() {
72 VideoFrame copy = frame;
73 callback_->Decoded(copy);
74 },
75 decode_delay_ms_);
76 }
pbos@webrtc.org0181b5f2013-09-09 08:26:30 +000077
78 return WEBRTC_VIDEO_CODEC_OK;
79}
80
Ilya Nikolaevskiy2ebf5232019-05-13 16:13:36 +020081void FakeDecoder::SetDelayedDecoding(int decode_delay_ms) {
82 RTC_CHECK(task_queue_factory_);
83 if (!task_queue_) {
84 task_queue_ =
Mirko Bonadei317a1f02019-09-17 17:06:18 +020085 std::make_unique<rtc::TaskQueue>(task_queue_factory_->CreateTaskQueue(
Ilya Nikolaevskiy2ebf5232019-05-13 16:13:36 +020086 "fake_decoder", TaskQueueFactory::Priority::NORMAL));
87 }
88 decode_delay_ms_ = decode_delay_ms;
89}
90
pbos@webrtc.org0181b5f2013-09-09 08:26:30 +000091int32_t FakeDecoder::RegisterDecodeCompleteCallback(
92 DecodedImageCallback* callback) {
93 callback_ = callback;
94 return WEBRTC_VIDEO_CODEC_OK;
95}
96
97int32_t FakeDecoder::Release() {
98 return WEBRTC_VIDEO_CODEC_OK;
99}
Peter Boströmb7d9a972015-12-18 16:01:11 +0100100
Peter Boströmb7d9a972015-12-18 16:01:11 +0100101const char* FakeDecoder::kImplementationName = "fake_decoder";
102const char* FakeDecoder::ImplementationName() const {
103 return kImplementationName;
104}
105
stefan@webrtc.org79c33592014-08-06 09:24:53 +0000106int32_t FakeH264Decoder::Decode(const EncodedImage& input,
107 bool missing_frames,
stefan@webrtc.org79c33592014-08-06 09:24:53 +0000108 int64_t render_time_ms) {
109 uint8_t value = 0;
Niels Möller77536a22019-01-15 08:50:01 +0100110 for (size_t i = 0; i < input.size(); ++i) {
stefan@webrtc.org79c33592014-08-06 09:24:53 +0000111 uint8_t kStartCode[] = {0, 0, 0, 1};
Niels Möller77536a22019-01-15 08:50:01 +0100112 if (i < input.size() - sizeof(kStartCode) &&
Niels Möller24871e42019-01-17 11:31:13 +0100113 !memcmp(&input.data()[i], kStartCode, sizeof(kStartCode))) {
stefan@webrtc.org79c33592014-08-06 09:24:53 +0000114 i += sizeof(kStartCode) + 1; // Skip start code and NAL header.
115 }
Niels Möller24871e42019-01-17 11:31:13 +0100116 if (input.data()[i] != value) {
117 RTC_CHECK_EQ(value, input.data()[i])
stefan@webrtc.org79c33592014-08-06 09:24:53 +0000118 << "Bitstream mismatch between sender and receiver.";
119 return -1;
120 }
121 ++value;
122 }
Niels Möller7aacdd92019-03-25 09:11:40 +0100123 return FakeDecoder::Decode(input, missing_frames, render_time_ms);
stefan@webrtc.org79c33592014-08-06 09:24:53 +0000124}
125
pbos@webrtc.org0181b5f2013-09-09 08:26:30 +0000126} // namespace test
127} // namespace webrtc