1 /*
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_FUNCTION_AUDIO_DECODER_FACTORY_H_
12 #define TEST_FUNCTION_AUDIO_DECODER_FACTORY_H_
13 
14 #include <functional>
15 #include <memory>
16 #include <utility>
17 #include <vector>
18 
19 #include "absl/memory/memory.h"
20 #include "api/audio_codecs/audio_decoder_factory.h"
21 #include "api/audio_codecs/audio_format.h"
22 #include "rtc_base/checks.h"
23 
24 namespace webrtc {
25 namespace test {
26 
27 // A decoder factory producing decoders by calling a supplied create function.
28 class FunctionAudioDecoderFactory : public AudioDecoderFactory {
29  public:
FunctionAudioDecoderFactory(std::function<std::unique_ptr<AudioDecoder> ()> create)30   explicit FunctionAudioDecoderFactory(
31       std::function<std::unique_ptr<AudioDecoder>()> create)
32       : create_([create](const SdpAudioFormat&,
33                          absl::optional<AudioCodecPairId> codec_pair_id) {
34           return create();
35         }) {}
FunctionAudioDecoderFactory(std::function<std::unique_ptr<AudioDecoder> (const SdpAudioFormat &,absl::optional<AudioCodecPairId> codec_pair_id)> create)36   explicit FunctionAudioDecoderFactory(
37       std::function<std::unique_ptr<AudioDecoder>(
38           const SdpAudioFormat&,
39           absl::optional<AudioCodecPairId> codec_pair_id)> create)
40       : create_(std::move(create)) {}
41 
42   // Unused by tests.
GetSupportedDecoders()43   std::vector<AudioCodecSpec> GetSupportedDecoders() override {
44     RTC_NOTREACHED();
45     return {};
46   }
47 
IsSupportedDecoder(const SdpAudioFormat & format)48   bool IsSupportedDecoder(const SdpAudioFormat& format) override {
49     return true;
50   }
51 
MakeAudioDecoder(const SdpAudioFormat & format,absl::optional<AudioCodecPairId> codec_pair_id)52   std::unique_ptr<AudioDecoder> MakeAudioDecoder(
53       const SdpAudioFormat& format,
54       absl::optional<AudioCodecPairId> codec_pair_id) override {
55     return create_(format, codec_pair_id);
56   }
57 
58  private:
59   const std::function<std::unique_ptr<AudioDecoder>(
60       const SdpAudioFormat&,
61       absl::optional<AudioCodecPairId> codec_pair_id)>
62       create_;
63 };
64 
65 }  // namespace test
66 }  // namespace webrtc
67 
68 #endif  // TEST_FUNCTION_AUDIO_DECODER_FACTORY_H_
69