Niels Möller | a0f4430 | 2018-11-30 09:45:12 | [diff] [blame] | 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" |
Danil Chapovalov | e0fe420 | 2024-08-14 17:17:05 | [diff] [blame] | 22 | #include "api/environment/environment.h" |
Niels Möller | a0f4430 | 2018-11-30 09:45:12 | [diff] [blame] | 23 | #include "rtc_base/checks.h" |
| 24 | |
| 25 | namespace webrtc { |
| 26 | namespace test { |
| 27 | |
| 28 | // A decoder factory producing decoders by calling a supplied create function. |
| 29 | class FunctionAudioDecoderFactory : public AudioDecoderFactory { |
| 30 | public: |
| 31 | explicit FunctionAudioDecoderFactory( |
| 32 | std::function<std::unique_ptr<AudioDecoder>()> create) |
Danil Chapovalov | e0fe420 | 2024-08-14 17:17:05 | [diff] [blame] | 33 | : create_([create](const Environment&, |
| 34 | const SdpAudioFormat&, |
Florent Castelli | 8037fc6 | 2024-08-29 13:00:40 | [diff] [blame] | 35 | std::optional<AudioCodecPairId> codec_pair_id) { |
Niels Möller | a0f4430 | 2018-11-30 09:45:12 | [diff] [blame] | 36 | return create(); |
| 37 | }) {} |
| 38 | explicit FunctionAudioDecoderFactory( |
| 39 | std::function<std::unique_ptr<AudioDecoder>( |
Danil Chapovalov | e0fe420 | 2024-08-14 17:17:05 | [diff] [blame] | 40 | const Environment&, |
Niels Möller | a0f4430 | 2018-11-30 09:45:12 | [diff] [blame] | 41 | const SdpAudioFormat&, |
Florent Castelli | 8037fc6 | 2024-08-29 13:00:40 | [diff] [blame] | 42 | std::optional<AudioCodecPairId> codec_pair_id)> create) |
Niels Möller | a0f4430 | 2018-11-30 09:45:12 | [diff] [blame] | 43 | : create_(std::move(create)) {} |
| 44 | |
| 45 | // Unused by tests. |
| 46 | std::vector<AudioCodecSpec> GetSupportedDecoders() override { |
Artem Titov | d325196 | 2021-11-15 15:57:07 | [diff] [blame] | 47 | RTC_DCHECK_NOTREACHED(); |
Niels Möller | a0f4430 | 2018-11-30 09:45:12 | [diff] [blame] | 48 | return {}; |
| 49 | } |
| 50 | |
| 51 | bool IsSupportedDecoder(const SdpAudioFormat& format) override { |
| 52 | return true; |
| 53 | } |
| 54 | |
Danil Chapovalov | e0fe420 | 2024-08-14 17:17:05 | [diff] [blame] | 55 | std::unique_ptr<AudioDecoder> Create( |
| 56 | const Environment& env, |
Niels Möller | a0f4430 | 2018-11-30 09:45:12 | [diff] [blame] | 57 | const SdpAudioFormat& format, |
Florent Castelli | 8037fc6 | 2024-08-29 13:00:40 | [diff] [blame] | 58 | std::optional<AudioCodecPairId> codec_pair_id) override { |
Danil Chapovalov | e0fe420 | 2024-08-14 17:17:05 | [diff] [blame] | 59 | return create_(env, format, codec_pair_id); |
Niels Möller | a0f4430 | 2018-11-30 09:45:12 | [diff] [blame] | 60 | } |
| 61 | |
| 62 | private: |
| 63 | const std::function<std::unique_ptr<AudioDecoder>( |
Danil Chapovalov | e0fe420 | 2024-08-14 17:17:05 | [diff] [blame] | 64 | const Environment&, |
Niels Möller | a0f4430 | 2018-11-30 09:45:12 | [diff] [blame] | 65 | const SdpAudioFormat&, |
Florent Castelli | 8037fc6 | 2024-08-29 13:00:40 | [diff] [blame] | 66 | std::optional<AudioCodecPairId> codec_pair_id)> |
Niels Möller | a0f4430 | 2018-11-30 09:45:12 | [diff] [blame] | 67 | create_; |
| 68 | }; |
| 69 | |
| 70 | } // namespace test |
| 71 | } // namespace webrtc |
| 72 | |
| 73 | #endif // TEST_FUNCTION_AUDIO_DECODER_FACTORY_H_ |