VideoEncoderFactoryTemplate cleanup.

Bug: webrtc:13573
Change-Id: Id70e64adba6c5d76132dc0edb0c93937e3e894f8
Reviewed-on: https://webrtc-review.googlesource.com/c/src/+/268542
Reviewed-by: Tomas Gunnarsson <tommi@webrtc.org>
Commit-Queue: Philip Eliasson <philipel@webrtc.org>
Cr-Commit-Position: refs/heads/main@{#37524}
diff --git a/api/video_codecs/video_encoder_factory_template.h b/api/video_codecs/video_encoder_factory_template.h
index b167ccd..643096db 100644
--- a/api/video_codecs/video_encoder_factory_template.h
+++ b/api/video_codecs/video_encoder_factory_template.h
@@ -46,9 +46,7 @@
 class VideoEncoderFactoryTemplate : public VideoEncoderFactory {
  public:
   std::vector<SdpVideoFormat> GetSupportedFormats() const override {
-    std::vector<SdpVideoFormat> formats;
-    GetSupportedFormatsInternal<Ts...>(formats);
-    return formats;
+    return GetSupportedFormatsInternal<Ts...>();
   }
 
   std::unique_ptr<VideoEncoder> CreateVideoEncoder(
@@ -86,17 +84,20 @@
   }
 
   template <typename V, typename... Vs>
-  void GetSupportedFormatsInternal(std::vector<SdpVideoFormat>& formats) const {
+  std::vector<SdpVideoFormat> GetSupportedFormatsInternal() const {
     auto supported_formats = V::SupportedFormats();
-    for (const auto& format : supported_formats) {
-      if (!IsFormatInList(format, formats)) {
-        formats.push_back(format);
+
+    if constexpr (sizeof...(Vs) > 0) {
+      // Supported formats may overlap between implementations, so duplicates
+      // should be filtered out.
+      for (const auto& other_format : GetSupportedFormatsInternal<Vs...>()) {
+        if (!IsFormatInList(other_format, supported_formats)) {
+          supported_formats.push_back(other_format);
+        }
       }
     }
 
-    if constexpr (sizeof...(Vs) > 0) {
-      return GetSupportedFormatsInternal<Vs...>(formats);
-    }
+    return supported_formats;
   }
 
   template <typename V, typename... Vs>