blob: fe7f63f113672de377c535453a9d1447bf533838 [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
62 void Reset() override { decoder_->Reset(); }
63
64 int SampleRateHz() const override { return decoder_->SampleRateHz(); }
65
66 size_t Channels() const override { return decoder_->Channels(); }
67
68 int DecodeInternal(const uint8_t* encoded,
69 size_t encoded_len,
70 int sample_rate_hz,
71 int16_t* decoded,
72 SpeechType* speech_type) override {
73 RTC_NOTREACHED();
74 return -1;
75 }
76
77 AudioDecoder* const decoder_;
78 };
79
80 AudioDecoder* const decoder_;
81};
82
83} // namespace test
84} // namespace webrtc
85
86#endif // TEST_AUDIO_DECODER_PROXY_FACTORY_H_