Niels Möller | b7180c0 | 2018-12-06 12:07:11 | [diff] [blame] | 1 | /* |
| 2 | * Copyright (c) 2018 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 | |
| 11 | #ifndef TEST_AUDIO_DECODER_PROXY_FACTORY_H_ |
| 12 | #define TEST_AUDIO_DECODER_PROXY_FACTORY_H_ |
| 13 | |
| 14 | #include <memory> |
| 15 | #include <utility> |
| 16 | #include <vector> |
| 17 | |
Niels Möller | b7180c0 | 2018-12-06 12:07:11 | [diff] [blame] | 18 | #include "api/audio_codecs/audio_decoder.h" |
| 19 | #include "api/audio_codecs/audio_decoder_factory.h" |
| 20 | |
| 21 | namespace webrtc { |
| 22 | namespace test { |
| 23 | |
Hua, Chunbo | b261118 | 2020-04-27 03:55:56 | [diff] [blame] | 24 | // A decoder factory with a single underlying AudioDecoder object, intended for |
Niels Möller | b7180c0 | 2018-12-06 12:07:11 | [diff] [blame] | 25 | // test purposes. Each call to MakeAudioDecoder returns a proxy for the same |
| 26 | // decoder, typically a mock or fake decoder. |
| 27 | class AudioDecoderProxyFactory : public AudioDecoderFactory { |
| 28 | public: |
| 29 | explicit AudioDecoderProxyFactory(AudioDecoder* decoder) |
| 30 | : decoder_(decoder) {} |
| 31 | |
| 32 | // Unused by tests. |
| 33 | std::vector<AudioCodecSpec> GetSupportedDecoders() override { |
Artem Titov | d325196 | 2021-11-15 15:57:07 | [diff] [blame] | 34 | RTC_DCHECK_NOTREACHED(); |
Niels Möller | b7180c0 | 2018-12-06 12:07:11 | [diff] [blame] | 35 | return {}; |
| 36 | } |
| 37 | |
| 38 | bool IsSupportedDecoder(const SdpAudioFormat& format) override { |
| 39 | return true; |
| 40 | } |
| 41 | |
| 42 | std::unique_ptr<AudioDecoder> MakeAudioDecoder( |
| 43 | const SdpAudioFormat& /* format */, |
| 44 | absl::optional<AudioCodecPairId> /* codec_pair_id */) override { |
Mirko Bonadei | 317a1f0 | 2019-09-17 15:06:18 | [diff] [blame] | 45 | return std::make_unique<DecoderProxy>(decoder_); |
Niels Möller | b7180c0 | 2018-12-06 12:07:11 | [diff] [blame] | 46 | } |
| 47 | |
| 48 | private: |
| 49 | // Wrapper class, since CreateAudioDecoder needs to surrender |
| 50 | // ownership to the object it returns. |
| 51 | class DecoderProxy final : public AudioDecoder { |
| 52 | public: |
| 53 | explicit DecoderProxy(AudioDecoder* decoder) : decoder_(decoder) {} |
| 54 | |
| 55 | private: |
| 56 | std::vector<ParseResult> ParsePayload(rtc::Buffer&& payload, |
| 57 | uint32_t timestamp) override { |
| 58 | return decoder_->ParsePayload(std::move(payload), timestamp); |
| 59 | } |
| 60 | |
Niels Möller | a1eb9c7 | 2018-12-07 14:24:42 | [diff] [blame] | 61 | bool HasDecodePlc() const override { return decoder_->HasDecodePlc(); } |
| 62 | |
| 63 | int ErrorCode() override { return decoder_->ErrorCode(); } |
| 64 | |
Niels Möller | b7180c0 | 2018-12-06 12:07:11 | [diff] [blame] | 65 | void Reset() override { decoder_->Reset(); } |
| 66 | |
| 67 | int SampleRateHz() const override { return decoder_->SampleRateHz(); } |
| 68 | |
| 69 | size_t Channels() const override { return decoder_->Channels(); } |
| 70 | |
| 71 | int DecodeInternal(const uint8_t* encoded, |
| 72 | size_t encoded_len, |
| 73 | int sample_rate_hz, |
| 74 | int16_t* decoded, |
| 75 | SpeechType* speech_type) override { |
Niels Möller | 50b66d5 | 2018-12-11 13:43:21 | [diff] [blame] | 76 | // Needed for tests of NetEqImpl::DecodeCng, which calls the deprecated |
| 77 | // Decode method. |
| 78 | size_t max_decoded_bytes = |
| 79 | decoder_->PacketDuration(encoded, encoded_len) * |
| 80 | decoder_->Channels() * sizeof(int16_t); |
| 81 | return decoder_->Decode(encoded, encoded_len, sample_rate_hz, |
| 82 | max_decoded_bytes, decoded, speech_type); |
Niels Möller | b7180c0 | 2018-12-06 12:07:11 | [diff] [blame] | 83 | } |
| 84 | |
Niels Möller | 29a935a | 2018-12-17 12:06:10 | [diff] [blame] | 85 | void GeneratePlc(size_t requested_samples_per_channel, |
| 86 | rtc::BufferT<int16_t>* concealment_audio) override { |
| 87 | decoder_->GeneratePlc(requested_samples_per_channel, concealment_audio); |
| 88 | } |
| 89 | |
Niels Möller | b7180c0 | 2018-12-06 12:07:11 | [diff] [blame] | 90 | AudioDecoder* const decoder_; |
| 91 | }; |
| 92 | |
| 93 | AudioDecoder* const decoder_; |
| 94 | }; |
| 95 | |
| 96 | } // namespace test |
| 97 | } // namespace webrtc |
| 98 | |
| 99 | #endif // TEST_AUDIO_DECODER_PROXY_FACTORY_H_ |