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