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 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 24 // A decoder factory with a single underlying AudioDecoder object, intended for 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: AudioDecoderProxyFactory(AudioDecoder * decoder)29 explicit AudioDecoderProxyFactory(AudioDecoder* decoder) 30 : decoder_(decoder) {} 31 32 // Unused by tests. GetSupportedDecoders()33 std::vector<AudioCodecSpec> GetSupportedDecoders() override { 34 RTC_NOTREACHED(); 35 return {}; 36 } 37 IsSupportedDecoder(const SdpAudioFormat & format)38 bool IsSupportedDecoder(const SdpAudioFormat& format) override { 39 return true; 40 } 41 MakeAudioDecoder(const SdpAudioFormat &,absl::optional<AudioCodecPairId>)42 std::unique_ptr<AudioDecoder> MakeAudioDecoder( 43 const SdpAudioFormat& /* format */, 44 absl::optional<AudioCodecPairId> /* codec_pair_id */) override { 45 return std::make_unique<DecoderProxy>(decoder_); 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: DecoderProxy(AudioDecoder * decoder)53 explicit DecoderProxy(AudioDecoder* decoder) : decoder_(decoder) {} 54 55 private: ParsePayload(rtc::Buffer && payload,uint32_t timestamp)56 std::vector<ParseResult> ParsePayload(rtc::Buffer&& payload, 57 uint32_t timestamp) override { 58 return decoder_->ParsePayload(std::move(payload), timestamp); 59 } 60 HasDecodePlc()61 bool HasDecodePlc() const override { return decoder_->HasDecodePlc(); } 62 ErrorCode()63 int ErrorCode() override { return decoder_->ErrorCode(); } 64 Reset()65 void Reset() override { decoder_->Reset(); } 66 SampleRateHz()67 int SampleRateHz() const override { return decoder_->SampleRateHz(); } 68 Channels()69 size_t Channels() const override { return decoder_->Channels(); } 70 DecodeInternal(const uint8_t * encoded,size_t encoded_len,int sample_rate_hz,int16_t * decoded,SpeechType * speech_type)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 { 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); 83 } 84 GeneratePlc(size_t requested_samples_per_channel,rtc::BufferT<int16_t> * concealment_audio)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 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_ 100