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 #include "api/video_codecs/builtin_video_encoder_factory.h" 12 13 #include <memory> 14 #include <vector> 15 16 #include "absl/strings/match.h" 17 #include "api/video_codecs/sdp_video_format.h" 18 #include "api/video_codecs/video_encoder.h" 19 #include "media/base/codec.h" 20 #include "media/base/media_constants.h" 21 #include "media/engine/encoder_simulcast_proxy.h" 22 #include "media/engine/internal_encoder_factory.h" 23 #include "rtc_base/checks.h" 24 25 namespace webrtc { 26 27 namespace { 28 IsFormatSupported(const std::vector<SdpVideoFormat> & supported_formats,const SdpVideoFormat & format)29bool IsFormatSupported(const std::vector<SdpVideoFormat>& supported_formats, 30 const SdpVideoFormat& format) { 31 for (const SdpVideoFormat& supported_format : supported_formats) { 32 if (cricket::IsSameCodec(format.name, format.parameters, 33 supported_format.name, 34 supported_format.parameters)) { 35 return true; 36 } 37 } 38 return false; 39 } 40 41 // This class wraps the internal factory and adds simulcast. 42 class BuiltinVideoEncoderFactory : public VideoEncoderFactory { 43 public: BuiltinVideoEncoderFactory()44 BuiltinVideoEncoderFactory() 45 : internal_encoder_factory_(new InternalEncoderFactory()) {} 46 QueryVideoEncoder(const SdpVideoFormat & format) const47 VideoEncoderFactory::CodecInfo QueryVideoEncoder( 48 const SdpVideoFormat& format) const override { 49 // Format must be one of the internal formats. 50 RTC_DCHECK(IsFormatSupported( 51 internal_encoder_factory_->GetSupportedFormats(), format)); 52 VideoEncoderFactory::CodecInfo info; 53 info.has_internal_source = false; 54 info.is_hardware_accelerated = false; 55 return info; 56 } 57 CreateVideoEncoder(const SdpVideoFormat & format)58 std::unique_ptr<VideoEncoder> CreateVideoEncoder( 59 const SdpVideoFormat& format) override { 60 // Try creating internal encoder. 61 std::unique_ptr<VideoEncoder> internal_encoder; 62 if (IsFormatSupported(internal_encoder_factory_->GetSupportedFormats(), 63 format)) { 64 internal_encoder = std::make_unique<EncoderSimulcastProxy>( 65 internal_encoder_factory_.get(), format); 66 } 67 68 return internal_encoder; 69 } 70 GetSupportedFormats() const71 std::vector<SdpVideoFormat> GetSupportedFormats() const override { 72 return internal_encoder_factory_->GetSupportedFormats(); 73 } 74 75 private: 76 const std::unique_ptr<VideoEncoderFactory> internal_encoder_factory_; 77 }; 78 79 } // namespace 80 CreateBuiltinVideoEncoderFactory()81std::unique_ptr<VideoEncoderFactory> CreateBuiltinVideoEncoderFactory() { 82 return std::make_unique<BuiltinVideoEncoderFactory>(); 83 } 84 85 } // namespace webrtc 86