1 /*
2  *  Copyright (c) 2016 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 "media/engine/internal_decoder_factory.h"
12 
13 #include "absl/strings/match.h"
14 #include "api/video_codecs/sdp_video_format.h"
15 #include "media/base/codec.h"
16 #include "media/base/media_constants.h"
17 #include "modules/video_coding/codecs/av1/libaom_av1_decoder.h"
18 #include "modules/video_coding/codecs/h264/include/h264.h"
19 #include "modules/video_coding/codecs/vp8/include/vp8.h"
20 #include "modules/video_coding/codecs/vp9/include/vp9.h"
21 #include "rtc_base/checks.h"
22 #include "rtc_base/logging.h"
23 
24 namespace webrtc {
25 
26 namespace {
27 
IsFormatSupported(const std::vector<webrtc::SdpVideoFormat> & supported_formats,const webrtc::SdpVideoFormat & format)28 bool IsFormatSupported(
29     const std::vector<webrtc::SdpVideoFormat>& supported_formats,
30     const webrtc::SdpVideoFormat& format) {
31   for (const webrtc::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 }  // namespace
42 
GetSupportedFormats() const43 std::vector<SdpVideoFormat> InternalDecoderFactory::GetSupportedFormats()
44     const {
45   std::vector<SdpVideoFormat> formats;
46   formats.push_back(SdpVideoFormat(cricket::kVp8CodecName));
47   for (const SdpVideoFormat& format : SupportedVP9DecoderCodecs())
48     formats.push_back(format);
49   for (const SdpVideoFormat& h264_format : SupportedH264Codecs())
50     formats.push_back(h264_format);
51   if (kIsLibaomAv1DecoderSupported)
52     formats.push_back(SdpVideoFormat(cricket::kAv1CodecName));
53   return formats;
54 }
55 
CreateVideoDecoder(const SdpVideoFormat & format)56 std::unique_ptr<VideoDecoder> InternalDecoderFactory::CreateVideoDecoder(
57     const SdpVideoFormat& format) {
58   if (!IsFormatSupported(GetSupportedFormats(), format)) {
59     RTC_LOG(LS_ERROR) << "Trying to create decoder for unsupported format";
60     return nullptr;
61   }
62 
63   if (absl::EqualsIgnoreCase(format.name, cricket::kVp8CodecName))
64     return VP8Decoder::Create();
65   if (absl::EqualsIgnoreCase(format.name, cricket::kVp9CodecName))
66     return VP9Decoder::Create();
67   if (absl::EqualsIgnoreCase(format.name, cricket::kH264CodecName))
68     return H264Decoder::Create();
69   if (kIsLibaomAv1DecoderSupported &&
70       absl::EqualsIgnoreCase(format.name, cricket::kAv1CodecName))
71     return CreateLibaomAv1Decoder();
72 
73   RTC_NOTREACHED();
74   return nullptr;
75 }
76 
77 }  // namespace webrtc
78