blob: 95606d6ff71c0c32d85aa37d0cfc3b768c1ed4bd [file] [log] [blame]
Niels Möllerb7180c02018-12-06 12:07:111/*
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 12:07:1118#include "api/audio_codecs/audio_decoder.h"
19#include "api/audio_codecs/audio_decoder_factory.h"
20
21namespace webrtc {
22namespace test {
23
Hua, Chunbob2611182020-04-27 03:55:5624// A decoder factory with a single underlying AudioDecoder object, intended for
Niels Möllerb7180c02018-12-06 12:07:1125// 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 {
Artem Titovd3251962021-11-15 15:57:0734 RTC_DCHECK_NOTREACHED();
Niels Möllerb7180c02018-12-06 12:07:1135 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 15:06:1845 return std::make_unique<DecoderProxy>(decoder_);
Niels Möllerb7180c02018-12-06 12:07:1146 }
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 14:24:4261 bool HasDecodePlc() const override { return decoder_->HasDecodePlc(); }
62
63 int ErrorCode() override { return decoder_->ErrorCode(); }
64
Niels Möllerb7180c02018-12-06 12:07:1165 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 13:43:2176 // 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 12:07:1183 }
84
Niels Möller29a935a2018-12-17 12:06:1085 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 12:07:1190 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_