Zeke Chin | 71f6f44 | 2015-06-29 14:34:58 -0700 | [diff] [blame] | 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 | |
Mirko Bonadei | 92ea95e | 2017-09-15 06:47:31 +0200 | [diff] [blame] | 12 | #include "modules/video_coding/codecs/h264/include/h264.h" |
Zeke Chin | 71f6f44 | 2015-06-29 14:34:58 -0700 | [diff] [blame] | 13 | |
Magnus Jedvert | 849b3ae | 2017-09-29 17:54:09 +0200 | [diff] [blame] | 14 | #include "api/video_codecs/sdp_video_format.h" |
Magnus Jedvert | 8deb818 | 2017-10-05 13:13:32 +0200 | [diff] [blame] | 15 | #include "media/base/h264_profile_level_id.h" |
Magnus Jedvert | 849b3ae | 2017-09-29 17:54:09 +0200 | [diff] [blame] | 16 | |
hbos | 9dc5928 | 2016-02-03 05:09:37 -0800 | [diff] [blame] | 17 | #if defined(WEBRTC_USE_H264) |
Mirko Bonadei | 92ea95e | 2017-09-15 06:47:31 +0200 | [diff] [blame] | 18 | #include "modules/video_coding/codecs/h264/h264_decoder_impl.h" |
| 19 | #include "modules/video_coding/codecs/h264/h264_encoder_impl.h" |
hbos | bab934b | 2016-01-27 01:36:03 -0800 | [diff] [blame] | 20 | #endif |
Zeke Chin | 71f6f44 | 2015-06-29 14:34:58 -0700 | [diff] [blame] | 21 | |
Mirko Bonadei | 92ea95e | 2017-09-15 06:47:31 +0200 | [diff] [blame] | 22 | #include "rtc_base/checks.h" |
| 23 | #include "rtc_base/logging.h" |
Magnus Jedvert | 46a2765 | 2017-11-13 14:10:02 +0100 | [diff] [blame] | 24 | #include "rtc_base/ptr_util.h" |
Zeke Chin | 71f6f44 | 2015-06-29 14:34:58 -0700 | [diff] [blame] | 25 | |
| 26 | namespace webrtc { |
| 27 | |
hbos | 9dc5928 | 2016-02-03 05:09:37 -0800 | [diff] [blame] | 28 | namespace { |
| 29 | |
| 30 | #if defined(WEBRTC_USE_H264) |
| 31 | bool g_rtc_use_h264 = true; |
| 32 | #endif |
| 33 | |
Magnus Jedvert | 8deb818 | 2017-10-05 13:13:32 +0200 | [diff] [blame] | 34 | // If H.264 OpenH264/FFmpeg codec is supported. |
Magnus Jedvert | 849b3ae | 2017-09-29 17:54:09 +0200 | [diff] [blame] | 35 | bool IsH264CodecSupported() { |
| 36 | #if defined(WEBRTC_USE_H264) |
| 37 | return g_rtc_use_h264; |
| 38 | #else |
| 39 | return false; |
| 40 | #endif |
| 41 | } |
| 42 | |
Taylor Brandstetter | 28deb90 | 2018-05-30 14:56:50 -0700 | [diff] [blame^] | 43 | SdpVideoFormat CreateH264Format(H264::Profile profile, |
| 44 | H264::Level level, |
| 45 | const std::string& packetization_mode) { |
Magnus Jedvert | 8deb818 | 2017-10-05 13:13:32 +0200 | [diff] [blame] | 46 | const rtc::Optional<std::string> profile_string = |
| 47 | H264::ProfileLevelIdToString(H264::ProfileLevelId(profile, level)); |
| 48 | RTC_CHECK(profile_string); |
Taylor Brandstetter | 28deb90 | 2018-05-30 14:56:50 -0700 | [diff] [blame^] | 49 | return SdpVideoFormat( |
| 50 | cricket::kH264CodecName, |
| 51 | {{cricket::kH264FmtpProfileLevelId, *profile_string}, |
| 52 | {cricket::kH264FmtpLevelAsymmetryAllowed, "1"}, |
| 53 | {cricket::kH264FmtpPacketizationMode, packetization_mode}}); |
Magnus Jedvert | 8deb818 | 2017-10-05 13:13:32 +0200 | [diff] [blame] | 54 | } |
| 55 | |
hbos | 9dc5928 | 2016-02-03 05:09:37 -0800 | [diff] [blame] | 56 | } // namespace |
| 57 | |
| 58 | void DisableRtcUseH264() { |
| 59 | #if defined(WEBRTC_USE_H264) |
| 60 | g_rtc_use_h264 = false; |
| 61 | #endif |
| 62 | } |
| 63 | |
Magnus Jedvert | 849b3ae | 2017-09-29 17:54:09 +0200 | [diff] [blame] | 64 | std::vector<SdpVideoFormat> SupportedH264Codecs() { |
| 65 | if (!IsH264CodecSupported()) |
| 66 | return std::vector<SdpVideoFormat>(); |
Magnus Jedvert | 8deb818 | 2017-10-05 13:13:32 +0200 | [diff] [blame] | 67 | // We only support encoding Constrained Baseline Profile (CBP), but the |
| 68 | // decoder supports more profiles. We can list all profiles here that are |
| 69 | // supported by the decoder and that are also supersets of CBP, i.e. the |
| 70 | // decoder for that profile is required to be able to decode CBP. This means |
| 71 | // we can encode and send CBP even though we negotiated a potentially |
| 72 | // higher profile. See the H264 spec for more information. |
Taylor Brandstetter | 28deb90 | 2018-05-30 14:56:50 -0700 | [diff] [blame^] | 73 | // |
| 74 | // We support both packetization modes 0 (mandatory) and 1 (optional, |
| 75 | // preferred). |
| 76 | return { |
| 77 | CreateH264Format(H264::kProfileBaseline, H264::kLevel3_1, "1"), |
| 78 | CreateH264Format(H264::kProfileBaseline, H264::kLevel3_1, "0"), |
| 79 | CreateH264Format(H264::kProfileConstrainedBaseline, H264::kLevel3_1, "1"), |
| 80 | CreateH264Format(H264::kProfileConstrainedBaseline, H264::kLevel3_1, |
| 81 | "0")}; |
Zeke Chin | 71f6f44 | 2015-06-29 14:34:58 -0700 | [diff] [blame] | 82 | } |
| 83 | |
Magnus Jedvert | 46a2765 | 2017-11-13 14:10:02 +0100 | [diff] [blame] | 84 | std::unique_ptr<H264Encoder> H264Encoder::Create( |
| 85 | const cricket::VideoCodec& codec) { |
henrikg | 91d6ede | 2015-09-17 00:24:34 -0700 | [diff] [blame] | 86 | RTC_DCHECK(H264Encoder::IsSupported()); |
hbos | 9dc5928 | 2016-02-03 05:09:37 -0800 | [diff] [blame] | 87 | #if defined(WEBRTC_USE_H264) |
| 88 | RTC_CHECK(g_rtc_use_h264); |
Mirko Bonadei | 675513b | 2017-11-09 11:09:25 +0100 | [diff] [blame] | 89 | RTC_LOG(LS_INFO) << "Creating H264EncoderImpl."; |
Magnus Jedvert | 46a2765 | 2017-11-13 14:10:02 +0100 | [diff] [blame] | 90 | return rtc::MakeUnique<H264EncoderImpl>(codec); |
Zeke Chin | 71f6f44 | 2015-06-29 14:34:58 -0700 | [diff] [blame] | 91 | #else |
| 92 | RTC_NOTREACHED(); |
| 93 | return nullptr; |
| 94 | #endif |
| 95 | } |
| 96 | |
| 97 | bool H264Encoder::IsSupported() { |
| 98 | return IsH264CodecSupported(); |
| 99 | } |
| 100 | |
Magnus Jedvert | 46a2765 | 2017-11-13 14:10:02 +0100 | [diff] [blame] | 101 | std::unique_ptr<H264Decoder> H264Decoder::Create() { |
henrikg | 91d6ede | 2015-09-17 00:24:34 -0700 | [diff] [blame] | 102 | RTC_DCHECK(H264Decoder::IsSupported()); |
hbos | 9dc5928 | 2016-02-03 05:09:37 -0800 | [diff] [blame] | 103 | #if defined(WEBRTC_USE_H264) |
| 104 | RTC_CHECK(g_rtc_use_h264); |
Mirko Bonadei | 675513b | 2017-11-09 11:09:25 +0100 | [diff] [blame] | 105 | RTC_LOG(LS_INFO) << "Creating H264DecoderImpl."; |
Magnus Jedvert | 46a2765 | 2017-11-13 14:10:02 +0100 | [diff] [blame] | 106 | return rtc::MakeUnique<H264DecoderImpl>(); |
Zeke Chin | 71f6f44 | 2015-06-29 14:34:58 -0700 | [diff] [blame] | 107 | #else |
| 108 | RTC_NOTREACHED(); |
| 109 | return nullptr; |
| 110 | #endif |
| 111 | } |
| 112 | |
| 113 | bool H264Decoder::IsSupported() { |
| 114 | return IsH264CodecSupported(); |
| 115 | } |
| 116 | |
| 117 | } // namespace webrtc |