xref: /aosp_15_r20/external/webrtc/api/test/video/function_video_encoder_factory.h (revision d9f758449e529ab9291ac668be2861e7a55c2422)
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 API_TEST_VIDEO_FUNCTION_VIDEO_ENCODER_FACTORY_H_
12 #define API_TEST_VIDEO_FUNCTION_VIDEO_ENCODER_FACTORY_H_
13 
14 #include <functional>
15 #include <memory>
16 #include <utility>
17 #include <vector>
18 
19 #include "api/video_codecs/sdp_video_format.h"
20 #include "api/video_codecs/video_encoder.h"
21 #include "api/video_codecs/video_encoder_factory.h"
22 #include "rtc_base/checks.h"
23 
24 namespace webrtc {
25 namespace test {
26 
27 // An encoder factory producing encoders by calling a supplied create
28 // function.
29 class FunctionVideoEncoderFactory final : public VideoEncoderFactory {
30  public:
FunctionVideoEncoderFactory(std::function<std::unique_ptr<VideoEncoder> ()> create)31   explicit FunctionVideoEncoderFactory(
32       std::function<std::unique_ptr<VideoEncoder>()> create)
33       : create_([create = std::move(create)](const SdpVideoFormat&) {
34           return create();
35         }) {}
FunctionVideoEncoderFactory(std::function<std::unique_ptr<VideoEncoder> (const SdpVideoFormat &)> create)36   explicit FunctionVideoEncoderFactory(
37       std::function<std::unique_ptr<VideoEncoder>(const SdpVideoFormat&)>
38           create)
39       : create_(std::move(create)) {}
40 
41   // Unused by tests.
GetSupportedFormats()42   std::vector<SdpVideoFormat> GetSupportedFormats() const override {
43     RTC_DCHECK_NOTREACHED();
44     return {};
45   }
46 
CreateVideoEncoder(const SdpVideoFormat & format)47   std::unique_ptr<VideoEncoder> CreateVideoEncoder(
48       const SdpVideoFormat& format) override {
49     return create_(format);
50   }
51 
52  private:
53   const std::function<std::unique_ptr<VideoEncoder>(const SdpVideoFormat&)>
54       create_;
55 };
56 
57 }  // namespace test
58 }  // namespace webrtc
59 
60 #endif  // API_TEST_VIDEO_FUNCTION_VIDEO_ENCODER_FACTORY_H_
61