blob: 821e6c6fb804b6f1ae5cd9c97648387ab4a46dbf [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
Niels Möllerb7180c02018-12-06 13:07:11 +010018#include "api/audio_codecs/audio_decoder.h"
19#include "api/audio_codecs/audio_decoder_factory.h"
20
21namespace webrtc {
22namespace test {
23
24// An 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.
27class AudioDecoderProxyFactory : public AudioDecoderFactory {
28 public:
29 explicit AudioDecoderProxyFactory(AudioDecoder* decoder)
30 : decoder_(decoder) {}
31
32 // Unused by tests.
33 std::vector<AudioCodecSpec> GetSupportedDecoders() override {
34 RTC_NOTREACHED();
35 return {};
36 }
37
38 bool IsSupportedDecoder(const SdpAudioFormat& format) override {
39 return true;
40 }
41
42 std::unique_ptr<AudioDecoder> MakeAudioDecoder(
43 const SdpAudioFormat& /* format */,
44 absl::optional<AudioCodecPairId> /* codec_pair_id */) override {
Mirko Bonadei317a1f02019-09-17 17:06:18 +020045 return std::make_unique<DecoderProxy>(decoder_);
Niels Möllerb7180c02018-12-06 13:07:11 +010046 }
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:
53 explicit DecoderProxy(AudioDecoder* decoder) : decoder_(decoder) {}
54
55 private:
56 std::vector<ParseResult> ParsePayload(rtc::Buffer&& payload,
57 uint32_t timestamp) override {
58 return decoder_->ParsePayload(std::move(payload), timestamp);
59 }
60
Niels Möllera1eb9c72018-12-07 15:24:42 +010061 bool HasDecodePlc() const override { return decoder_->HasDecodePlc(); }
62
63 int ErrorCode() override { return decoder_->ErrorCode(); }
64
Niels Möllerb7180c02018-12-06 13:07:11 +010065 void Reset() override { decoder_->Reset(); }
66
67 int SampleRateHz() const override { return decoder_->SampleRateHz(); }
68
69 size_t Channels() const override { return decoder_->Channels(); }
70
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 {
Niels Möller50b66d52018-12-11 14:43:21 +010076 // 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);
Niels Möllerb7180c02018-12-06 13:07:11 +010083 }
84
Niels Möller29a935a2018-12-17 13:06:10 +010085 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
Niels Möllerb7180c02018-12-06 13:07:11 +010090 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_