blob: af08581d17c4fc04a6903f3f5fc354b7d404e414 [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 {
77 RTC_NOTREACHED();
78 return -1;
79 }
80
81 AudioDecoder* const decoder_;
82 };
83
84 AudioDecoder* const decoder_;
85};
86
87} // namespace test
88} // namespace webrtc
89
90#endif // TEST_AUDIO_DECODER_PROXY_FACTORY_H_