[WebRTC][GStreamer] Build and use the openh264 based encoder if present on the system
[WebKit-https.git] / Source / ThirdParty / libwebrtc / Source / webrtc / modules / video_coding / codecs / h264 / h264.cc
1 /*
2  *  Copyright (c) 2015 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
12 #include "modules/video_coding/codecs/h264/include/h264.h"
13
14 #include <memory>
15 #include <string>
16
17 #include "absl/types/optional.h"
18 #include "api/video_codecs/sdp_video_format.h"
19 #include "media/base/media_constants.h"
20
21 #if defined(WEBRTC_USE_H264)
22 #if !defined(WEBRTC_WEBKIT_BUILD)
23 #include "modules/video_coding/codecs/h264/h264_decoder_impl.h"
24 #endif
25 #include "modules/video_coding/codecs/h264/h264_encoder_impl.h"
26 #endif
27
28 #include "rtc_base/checks.h"
29 #include "rtc_base/logging.h"
30
31 namespace webrtc {
32
33 namespace {
34
35 #if defined(WEBRTC_USE_H264)
36 bool g_rtc_use_h264 = true;
37 #endif
38
39 // If H.264 OpenH264/FFmpeg codec is supported.
40 bool IsH264CodecSupported() {
41 #if defined(WEBRTC_USE_H264)
42   return g_rtc_use_h264;
43 #else
44   return false;
45 #endif
46 }
47
48 }  // namespace
49
50 SdpVideoFormat CreateH264Format(H264::Profile profile,
51                                 H264::Level level,
52                                 const std::string& packetization_mode) {
53   const absl::optional<std::string> profile_string =
54       H264::ProfileLevelIdToString(H264::ProfileLevelId(profile, level));
55   RTC_CHECK(profile_string);
56   return SdpVideoFormat(
57       cricket::kH264CodecName,
58       {{cricket::kH264FmtpProfileLevelId, *profile_string},
59        {cricket::kH264FmtpLevelAsymmetryAllowed, "1"},
60        {cricket::kH264FmtpPacketizationMode, packetization_mode}});
61 }
62
63 void DisableRtcUseH264() {
64 #if defined(WEBRTC_USE_H264)
65   g_rtc_use_h264 = false;
66 #endif
67 }
68
69 std::vector<SdpVideoFormat> SupportedH264Codecs() {
70   if (!IsH264CodecSupported())
71     return std::vector<SdpVideoFormat>();
72   // We only support encoding Constrained Baseline Profile (CBP), but the
73   // decoder supports more profiles. We can list all profiles here that are
74   // supported by the decoder and that are also supersets of CBP, i.e. the
75   // decoder for that profile is required to be able to decode CBP. This means
76   // we can encode and send CBP even though we negotiated a potentially
77   // higher profile. See the H264 spec for more information.
78   //
79   // We support both packetization modes 0 (mandatory) and 1 (optional,
80   // preferred).
81   return {
82       CreateH264Format(H264::kProfileBaseline, H264::kLevel3_1, "1"),
83       CreateH264Format(H264::kProfileBaseline, H264::kLevel3_1, "0"),
84       CreateH264Format(H264::kProfileConstrainedBaseline, H264::kLevel3_1, "1"),
85       CreateH264Format(H264::kProfileConstrainedBaseline, H264::kLevel3_1,
86                        "0")};
87 }
88
89 std::unique_ptr<H264Encoder> H264Encoder::Create(
90     const cricket::VideoCodec& codec) {
91   RTC_DCHECK(H264Encoder::IsSupported());
92 #if defined(WEBRTC_USE_H264)
93   RTC_CHECK(g_rtc_use_h264);
94   RTC_LOG(LS_INFO) << "Creating H264EncoderImpl.";
95   return std::make_unique<H264EncoderImpl>(codec);
96 #else
97   RTC_NOTREACHED();
98   return nullptr;
99 #endif
100 }
101
102 bool H264Encoder::IsSupported() {
103   return IsH264CodecSupported();
104 }
105
106 std::unique_ptr<H264Decoder> H264Decoder::Create() {
107   RTC_DCHECK(H264Decoder::IsSupported());
108 #if defined(WEBRTC_USE_H264) && !defined(WEBRTC_WEBKIT_BUILD)
109   RTC_CHECK(g_rtc_use_h264);
110   RTC_LOG(LS_INFO) << "Creating H264DecoderImpl.";
111   return std::make_unique<H264DecoderImpl>();
112 #else
113   RTC_NOTREACHED();
114   return nullptr;
115 #endif
116 }
117
118 bool H264Decoder::IsSupported() {
119   return IsH264CodecSupported();
120 }
121
122 }  // namespace webrtc