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" |
Zeke Chin | 71f6f44 | 2015-06-29 14:34:58 -0700 | [diff] [blame] | 24 | |
| 25 | namespace webrtc { |
| 26 | |
hbos | 9dc5928 | 2016-02-03 05:09:37 -0800 | [diff] [blame] | 27 | namespace { |
| 28 | |
| 29 | #if defined(WEBRTC_USE_H264) |
| 30 | bool g_rtc_use_h264 = true; |
| 31 | #endif |
| 32 | |
Magnus Jedvert | 8deb818 | 2017-10-05 13:13:32 +0200 | [diff] [blame] | 33 | // If H.264 OpenH264/FFmpeg codec is supported. |
Magnus Jedvert | 849b3ae | 2017-09-29 17:54:09 +0200 | [diff] [blame] | 34 | bool IsH264CodecSupported() { |
| 35 | #if defined(WEBRTC_USE_H264) |
| 36 | return g_rtc_use_h264; |
| 37 | #else |
| 38 | return false; |
| 39 | #endif |
| 40 | } |
| 41 | |
Magnus Jedvert | 8deb818 | 2017-10-05 13:13:32 +0200 | [diff] [blame] | 42 | SdpVideoFormat CreateH264Format(H264::Profile profile, H264::Level level) { |
| 43 | const rtc::Optional<std::string> profile_string = |
| 44 | H264::ProfileLevelIdToString(H264::ProfileLevelId(profile, level)); |
| 45 | RTC_CHECK(profile_string); |
| 46 | return SdpVideoFormat(cricket::kH264CodecName, |
| 47 | {{cricket::kH264FmtpProfileLevelId, *profile_string}, |
| 48 | {cricket::kH264FmtpLevelAsymmetryAllowed, "1"}, |
| 49 | {cricket::kH264FmtpPacketizationMode, "1"}}); |
| 50 | } |
| 51 | |
hbos | 9dc5928 | 2016-02-03 05:09:37 -0800 | [diff] [blame] | 52 | } // namespace |
| 53 | |
| 54 | void DisableRtcUseH264() { |
| 55 | #if defined(WEBRTC_USE_H264) |
| 56 | g_rtc_use_h264 = false; |
| 57 | #endif |
| 58 | } |
| 59 | |
Magnus Jedvert | 849b3ae | 2017-09-29 17:54:09 +0200 | [diff] [blame] | 60 | std::vector<SdpVideoFormat> SupportedH264Codecs() { |
| 61 | if (!IsH264CodecSupported()) |
| 62 | return std::vector<SdpVideoFormat>(); |
Magnus Jedvert | 8deb818 | 2017-10-05 13:13:32 +0200 | [diff] [blame] | 63 | // We only support encoding Constrained Baseline Profile (CBP), but the |
| 64 | // decoder supports more profiles. We can list all profiles here that are |
| 65 | // supported by the decoder and that are also supersets of CBP, i.e. the |
| 66 | // decoder for that profile is required to be able to decode CBP. This means |
| 67 | // we can encode and send CBP even though we negotiated a potentially |
| 68 | // higher profile. See the H264 spec for more information. |
Magnus Jedvert | a750333 | 2017-11-01 08:52:03 +0100 | [diff] [blame^] | 69 | return {CreateH264Format(H264::kProfileBaseline, H264::kLevel3_1), |
| 70 | CreateH264Format(H264::kProfileConstrainedBaseline, H264::kLevel3_1)}; |
Zeke Chin | 71f6f44 | 2015-06-29 14:34:58 -0700 | [diff] [blame] | 71 | } |
| 72 | |
magjed | ceecea4 | 2016-11-28 07:20:21 -0800 | [diff] [blame] | 73 | H264Encoder* H264Encoder::Create(const cricket::VideoCodec& codec) { |
henrikg | 91d6ede | 2015-09-17 00:24:34 -0700 | [diff] [blame] | 74 | RTC_DCHECK(H264Encoder::IsSupported()); |
hbos | 9dc5928 | 2016-02-03 05:09:37 -0800 | [diff] [blame] | 75 | #if defined(WEBRTC_USE_H264) |
| 76 | RTC_CHECK(g_rtc_use_h264); |
hbos | bab934b | 2016-01-27 01:36:03 -0800 | [diff] [blame] | 77 | LOG(LS_INFO) << "Creating H264EncoderImpl."; |
hta | 9aa9688 | 2016-12-06 05:36:03 -0800 | [diff] [blame] | 78 | return new H264EncoderImpl(codec); |
Zeke Chin | 71f6f44 | 2015-06-29 14:34:58 -0700 | [diff] [blame] | 79 | #else |
| 80 | RTC_NOTREACHED(); |
| 81 | return nullptr; |
| 82 | #endif |
| 83 | } |
| 84 | |
| 85 | bool H264Encoder::IsSupported() { |
| 86 | return IsH264CodecSupported(); |
| 87 | } |
| 88 | |
| 89 | H264Decoder* H264Decoder::Create() { |
henrikg | 91d6ede | 2015-09-17 00:24:34 -0700 | [diff] [blame] | 90 | RTC_DCHECK(H264Decoder::IsSupported()); |
hbos | 9dc5928 | 2016-02-03 05:09:37 -0800 | [diff] [blame] | 91 | #if defined(WEBRTC_USE_H264) |
| 92 | RTC_CHECK(g_rtc_use_h264); |
hbos | bab934b | 2016-01-27 01:36:03 -0800 | [diff] [blame] | 93 | LOG(LS_INFO) << "Creating H264DecoderImpl."; |
| 94 | return new H264DecoderImpl(); |
Zeke Chin | 71f6f44 | 2015-06-29 14:34:58 -0700 | [diff] [blame] | 95 | #else |
| 96 | RTC_NOTREACHED(); |
| 97 | return nullptr; |
| 98 | #endif |
| 99 | } |
| 100 | |
| 101 | bool H264Decoder::IsSupported() { |
| 102 | return IsH264CodecSupported(); |
| 103 | } |
| 104 | |
| 105 | } // namespace webrtc |