audio_decoder_proxy_factory.h 3.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899
  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. #ifndef TEST_AUDIO_DECODER_PROXY_FACTORY_H_
  11. #define TEST_AUDIO_DECODER_PROXY_FACTORY_H_
  12. #include <memory>
  13. #include <utility>
  14. #include <vector>
  15. #include "api/audio_codecs/audio_decoder.h"
  16. #include "api/audio_codecs/audio_decoder_factory.h"
  17. namespace webrtc {
  18. namespace test {
  19. // A decoder factory with a single underlying AudioDecoder object, intended for
  20. // test purposes. Each call to MakeAudioDecoder returns a proxy for the same
  21. // decoder, typically a mock or fake decoder.
  22. class AudioDecoderProxyFactory : public AudioDecoderFactory {
  23. public:
  24. explicit AudioDecoderProxyFactory(AudioDecoder* decoder)
  25. : decoder_(decoder) {}
  26. // Unused by tests.
  27. std::vector<AudioCodecSpec> GetSupportedDecoders() override {
  28. RTC_NOTREACHED();
  29. return {};
  30. }
  31. bool IsSupportedDecoder(const SdpAudioFormat& format) override {
  32. return true;
  33. }
  34. std::unique_ptr<AudioDecoder> MakeAudioDecoder(
  35. const SdpAudioFormat& /* format */,
  36. absl::optional<AudioCodecPairId> /* codec_pair_id */) override {
  37. return std::make_unique<DecoderProxy>(decoder_);
  38. }
  39. private:
  40. // Wrapper class, since CreateAudioDecoder needs to surrender
  41. // ownership to the object it returns.
  42. class DecoderProxy final : public AudioDecoder {
  43. public:
  44. explicit DecoderProxy(AudioDecoder* decoder) : decoder_(decoder) {}
  45. private:
  46. std::vector<ParseResult> ParsePayload(rtc::Buffer&& payload,
  47. uint32_t timestamp) override {
  48. return decoder_->ParsePayload(std::move(payload), timestamp);
  49. }
  50. bool HasDecodePlc() const override { return decoder_->HasDecodePlc(); }
  51. int ErrorCode() override { return decoder_->ErrorCode(); }
  52. void Reset() override { decoder_->Reset(); }
  53. int SampleRateHz() const override { return decoder_->SampleRateHz(); }
  54. size_t Channels() const override { return decoder_->Channels(); }
  55. int DecodeInternal(const uint8_t* encoded,
  56. size_t encoded_len,
  57. int sample_rate_hz,
  58. int16_t* decoded,
  59. SpeechType* speech_type) override {
  60. // Needed for tests of NetEqImpl::DecodeCng, which calls the deprecated
  61. // Decode method.
  62. size_t max_decoded_bytes =
  63. decoder_->PacketDuration(encoded, encoded_len) *
  64. decoder_->Channels() * sizeof(int16_t);
  65. return decoder_->Decode(encoded, encoded_len, sample_rate_hz,
  66. max_decoded_bytes, decoded, speech_type);
  67. }
  68. void GeneratePlc(size_t requested_samples_per_channel,
  69. rtc::BufferT<int16_t>* concealment_audio) override {
  70. decoder_->GeneratePlc(requested_samples_per_channel, concealment_audio);
  71. }
  72. AudioDecoder* const decoder_;
  73. };
  74. AudioDecoder* const decoder_;
  75. };
  76. } // namespace test
  77. } // namespace webrtc
  78. #endif // TEST_AUDIO_DECODER_PROXY_FACTORY_H_