pbos@webrtc.org | 0181b5f | 2013-09-09 08:26:30 +0000 | [diff] [blame] | 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 | |
Mirko Bonadei | 92ea95e | 2017-09-15 06:47:31 +0200 | [diff] [blame] | 11 | #include "test/fake_decoder.h" |
pbos@webrtc.org | 0181b5f | 2013-09-09 08:26:30 +0000 | [diff] [blame] | 12 | |
Yves Gerey | 3e70781 | 2018-11-28 16:47:49 +0100 | [diff] [blame] | 13 | #include <string.h> |
| 14 | |
Ilya Nikolaevskiy | 2ebf523 | 2019-05-13 16:13:36 +0200 | [diff] [blame] | 15 | #include "absl/memory/memory.h" |
Mirko Bonadei | d970807 | 2019-01-25 20:26:48 +0100 | [diff] [blame] | 16 | #include "api/scoped_refptr.h" |
Mirko Bonadei | 92ea95e | 2017-09-15 06:47:31 +0200 | [diff] [blame] | 17 | #include "api/video/i420_buffer.h" |
Yves Gerey | 3e70781 | 2018-11-28 16:47:49 +0100 | [diff] [blame] | 18 | #include "api/video/video_frame.h" |
| 19 | #include "api/video/video_frame_buffer.h" |
| 20 | #include "api/video/video_rotation.h" |
| 21 | #include "modules/video_coding/include/video_error_codes.h" |
| 22 | #include "rtc_base/checks.h" |
Ilya Nikolaevskiy | 2ebf523 | 2019-05-13 16:13:36 +0200 | [diff] [blame] | 23 | #include "rtc_base/task_queue.h" |
Steve Anton | 10542f2 | 2019-01-11 09:11:00 -0800 | [diff] [blame] | 24 | #include "rtc_base/time_utils.h" |
pbos@webrtc.org | 0181b5f | 2013-09-09 08:26:30 +0000 | [diff] [blame] | 25 | |
| 26 | namespace webrtc { |
| 27 | namespace test { |
| 28 | |
Ilya Nikolaevskiy | b0588e6 | 2018-08-27 14:12:27 +0200 | [diff] [blame] | 29 | namespace { |
| 30 | const int kDefaultWidth = 320; |
| 31 | const int kDefaultHeight = 180; |
| 32 | } // namespace |
| 33 | |
Ilya Nikolaevskiy | 2ebf523 | 2019-05-13 16:13:36 +0200 | [diff] [blame] | 34 | FakeDecoder::FakeDecoder() : FakeDecoder(nullptr) {} |
| 35 | |
| 36 | FakeDecoder::FakeDecoder(TaskQueueFactory* task_queue_factory) |
| 37 | : callback_(nullptr), |
| 38 | width_(kDefaultWidth), |
| 39 | height_(kDefaultHeight), |
| 40 | task_queue_factory_(task_queue_factory), |
| 41 | decode_delay_ms_(0) {} |
pbos@webrtc.org | 0181b5f | 2013-09-09 08:26:30 +0000 | [diff] [blame] | 42 | |
| 43 | int32_t FakeDecoder::InitDecode(const VideoCodec* config, |
| 44 | int32_t number_of_cores) { |
pbos@webrtc.org | 0181b5f | 2013-09-09 08:26:30 +0000 | [diff] [blame] | 45 | return WEBRTC_VIDEO_CODEC_OK; |
| 46 | } |
| 47 | |
| 48 | int32_t FakeDecoder::Decode(const EncodedImage& input, |
| 49 | bool missing_frames, |
pbos@webrtc.org | 0181b5f | 2013-09-09 08:26:30 +0000 | [diff] [blame] | 50 | int64_t render_time_ms) { |
philipel | 0e07572 | 2018-04-05 13:04:42 +0200 | [diff] [blame] | 51 | if (input._encodedWidth > 0 && input._encodedHeight > 0) { |
| 52 | width_ = input._encodedWidth; |
| 53 | height_ = input._encodedHeight; |
| 54 | } |
| 55 | |
Ilya Nikolaevskiy | 2ebf523 | 2019-05-13 16:13:36 +0200 | [diff] [blame] | 56 | rtc::scoped_refptr<I420Buffer> buffer = I420Buffer::Create(width_, height_); |
| 57 | I420Buffer::SetBlack(buffer); |
| 58 | VideoFrame frame = VideoFrame::Builder() |
| 59 | .set_video_frame_buffer(buffer) |
| 60 | .set_rotation(webrtc::kVideoRotation_0) |
| 61 | .set_timestamp_ms(render_time_ms) |
| 62 | .build(); |
Niels Möller | 2377588 | 2018-08-16 10:24:12 +0200 | [diff] [blame] | 63 | frame.set_timestamp(input.Timestamp()); |
Artem Titarenko | 84ae2b6 | 2019-04-24 12:56:36 +0000 | [diff] [blame] | 64 | frame.set_ntp_time_ms(input.ntp_time_ms_); |
pbos@webrtc.org | 0181b5f | 2013-09-09 08:26:30 +0000 | [diff] [blame] | 65 | |
Ilya Nikolaevskiy | 2ebf523 | 2019-05-13 16:13:36 +0200 | [diff] [blame] | 66 | if (decode_delay_ms_ == 0 || !task_queue_) { |
| 67 | callback_->Decoded(frame); |
| 68 | } else { |
| 69 | task_queue_->PostDelayedTask( |
| 70 | [frame, this]() { |
| 71 | VideoFrame copy = frame; |
| 72 | callback_->Decoded(copy); |
| 73 | }, |
| 74 | decode_delay_ms_); |
| 75 | } |
pbos@webrtc.org | 0181b5f | 2013-09-09 08:26:30 +0000 | [diff] [blame] | 76 | |
| 77 | return WEBRTC_VIDEO_CODEC_OK; |
| 78 | } |
| 79 | |
Ilya Nikolaevskiy | 2ebf523 | 2019-05-13 16:13:36 +0200 | [diff] [blame] | 80 | void FakeDecoder::SetDelayedDecoding(int decode_delay_ms) { |
| 81 | RTC_CHECK(task_queue_factory_); |
| 82 | if (!task_queue_) { |
| 83 | task_queue_ = |
| 84 | absl::make_unique<rtc::TaskQueue>(task_queue_factory_->CreateTaskQueue( |
| 85 | "fake_decoder", TaskQueueFactory::Priority::NORMAL)); |
| 86 | } |
| 87 | decode_delay_ms_ = decode_delay_ms; |
| 88 | } |
| 89 | |
pbos@webrtc.org | 0181b5f | 2013-09-09 08:26:30 +0000 | [diff] [blame] | 90 | int32_t FakeDecoder::RegisterDecodeCompleteCallback( |
| 91 | DecodedImageCallback* callback) { |
| 92 | callback_ = callback; |
| 93 | return WEBRTC_VIDEO_CODEC_OK; |
| 94 | } |
| 95 | |
| 96 | int32_t FakeDecoder::Release() { |
| 97 | return WEBRTC_VIDEO_CODEC_OK; |
| 98 | } |
Peter Boström | b7d9a97 | 2015-12-18 16:01:11 +0100 | [diff] [blame] | 99 | |
Peter Boström | b7d9a97 | 2015-12-18 16:01:11 +0100 | [diff] [blame] | 100 | const char* FakeDecoder::kImplementationName = "fake_decoder"; |
| 101 | const char* FakeDecoder::ImplementationName() const { |
| 102 | return kImplementationName; |
| 103 | } |
| 104 | |
stefan@webrtc.org | 79c3359 | 2014-08-06 09:24:53 +0000 | [diff] [blame] | 105 | int32_t FakeH264Decoder::Decode(const EncodedImage& input, |
| 106 | bool missing_frames, |
stefan@webrtc.org | 79c3359 | 2014-08-06 09:24:53 +0000 | [diff] [blame] | 107 | int64_t render_time_ms) { |
| 108 | uint8_t value = 0; |
Niels Möller | 77536a2 | 2019-01-15 08:50:01 +0100 | [diff] [blame] | 109 | for (size_t i = 0; i < input.size(); ++i) { |
stefan@webrtc.org | 79c3359 | 2014-08-06 09:24:53 +0000 | [diff] [blame] | 110 | uint8_t kStartCode[] = {0, 0, 0, 1}; |
Niels Möller | 77536a2 | 2019-01-15 08:50:01 +0100 | [diff] [blame] | 111 | if (i < input.size() - sizeof(kStartCode) && |
Niels Möller | 24871e4 | 2019-01-17 11:31:13 +0100 | [diff] [blame] | 112 | !memcmp(&input.data()[i], kStartCode, sizeof(kStartCode))) { |
stefan@webrtc.org | 79c3359 | 2014-08-06 09:24:53 +0000 | [diff] [blame] | 113 | i += sizeof(kStartCode) + 1; // Skip start code and NAL header. |
| 114 | } |
Niels Möller | 24871e4 | 2019-01-17 11:31:13 +0100 | [diff] [blame] | 115 | if (input.data()[i] != value) { |
| 116 | RTC_CHECK_EQ(value, input.data()[i]) |
stefan@webrtc.org | 79c3359 | 2014-08-06 09:24:53 +0000 | [diff] [blame] | 117 | << "Bitstream mismatch between sender and receiver."; |
| 118 | return -1; |
| 119 | } |
| 120 | ++value; |
| 121 | } |
Niels Möller | 7aacdd9 | 2019-03-25 09:11:40 +0100 | [diff] [blame] | 122 | return FakeDecoder::Decode(input, missing_frames, render_time_ms); |
stefan@webrtc.org | 79c3359 | 2014-08-06 09:24:53 +0000 | [diff] [blame] | 123 | } |
| 124 | |
pbos@webrtc.org | 0181b5f | 2013-09-09 08:26:30 +0000 | [diff] [blame] | 125 | } // namespace test |
| 126 | } // namespace webrtc |