blob: 18988e66f7353e92e467d731811e4d62e17a968f [file] [log] [blame]
Niels Möllerb7180c02018-12-06 13:07:11 +01001/*
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 "absl/memory/memory.h"
19#include "api/audio_codecs/audio_decoder.h"
20#include "api/audio_codecs/audio_decoder_factory.h"
21
22namespace webrtc {
23namespace test {
24
25// An decoder factory with a single underlying AudioDecoder object, intended for
26// test purposes. Each call to MakeAudioDecoder returns a proxy for the same
27// decoder, typically a mock or fake decoder.
28class AudioDecoderProxyFactory : public AudioDecoderFactory {
29 public:
30 explicit AudioDecoderProxyFactory(AudioDecoder* decoder)
31 : decoder_(decoder) {}
32
33 // Unused by tests.
34 std::vector<AudioCodecSpec> GetSupportedDecoders() override {
35 RTC_NOTREACHED();
36 return {};
37 }
38
39 bool IsSupportedDecoder(const SdpAudioFormat& format) override {
40 return true;
41 }
42
43 std::unique_ptr<AudioDecoder> MakeAudioDecoder(
44 const SdpAudioFormat& /* format */,
45 absl::optional<AudioCodecPairId> /* codec_pair_id */) override {
46 return absl::make_unique<DecoderProxy>(decoder_);
47 }
48
49 private:
50 // Wrapper class, since CreateAudioDecoder needs to surrender
51 // ownership to the object it returns.
52 class DecoderProxy final : public AudioDecoder {
53 public:
54 explicit DecoderProxy(AudioDecoder* decoder) : decoder_(decoder) {}
55
56 private:
57 std::vector<ParseResult> ParsePayload(rtc::Buffer&& payload,
58 uint32_t timestamp) override {
59 return decoder_->ParsePayload(std::move(payload), timestamp);
60 }
61
Niels Möllera1eb9c72018-12-07 15:24:42 +010062 bool HasDecodePlc() const override { return decoder_->HasDecodePlc(); }
63
64 int ErrorCode() override { return decoder_->ErrorCode(); }
65
Niels Möllerb7180c02018-12-06 13:07:11 +010066 void Reset() override { decoder_->Reset(); }
67
68 int SampleRateHz() const override { return decoder_->SampleRateHz(); }
69
70 size_t Channels() const override { return decoder_->Channels(); }
71
72 int DecodeInternal(const uint8_t* encoded,
73 size_t encoded_len,
74 int sample_rate_hz,
75 int16_t* decoded,
76 SpeechType* speech_type) override {
Niels Möller50b66d52018-12-11 14:43:21 +010077 // Needed for tests of NetEqImpl::DecodeCng, which calls the deprecated
78 // Decode method.
79 size_t max_decoded_bytes =
80 decoder_->PacketDuration(encoded, encoded_len) *
81 decoder_->Channels() * sizeof(int16_t);
82 return decoder_->Decode(encoded, encoded_len, sample_rate_hz,
83 max_decoded_bytes, decoded, speech_type);
Niels Möllerb7180c02018-12-06 13:07:11 +010084 }
85
86 AudioDecoder* const decoder_;
87 };
88
89 AudioDecoder* const decoder_;
90};
91
92} // namespace test
93} // namespace webrtc
94
95#endif // TEST_AUDIO_DECODER_PROXY_FACTORY_H_