blob: d7ed3b42e8ad07e9ce400e633b8e6ec121ed4a15 [file] [log] [blame]
Zeke Chin71f6f442015-06-29 14:34:58 -07001/*
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 Bonadei92ea95e2017-09-15 06:47:31 +020012#include "modules/video_coding/codecs/h264/include/h264.h"
Zeke Chin71f6f442015-06-29 14:34:58 -070013
Magnus Jedvert849b3ae2017-09-29 17:54:09 +020014#include "api/video_codecs/sdp_video_format.h"
Magnus Jedvert8deb8182017-10-05 13:13:32 +020015#include "media/base/h264_profile_level_id.h"
Magnus Jedvert849b3ae2017-09-29 17:54:09 +020016
hbos9dc59282016-02-03 05:09:37 -080017#if defined(WEBRTC_USE_H264)
Mirko Bonadei92ea95e2017-09-15 06:47:31 +020018#include "modules/video_coding/codecs/h264/h264_decoder_impl.h"
19#include "modules/video_coding/codecs/h264/h264_encoder_impl.h"
hbosbab934b2016-01-27 01:36:03 -080020#endif
Zeke Chin71f6f442015-06-29 14:34:58 -070021
Mirko Bonadei92ea95e2017-09-15 06:47:31 +020022#include "rtc_base/checks.h"
23#include "rtc_base/logging.h"
Zeke Chin71f6f442015-06-29 14:34:58 -070024
25namespace webrtc {
26
hbos9dc59282016-02-03 05:09:37 -080027namespace {
28
29#if defined(WEBRTC_USE_H264)
30bool g_rtc_use_h264 = true;
31#endif
32
Magnus Jedvert8deb8182017-10-05 13:13:32 +020033// If H.264 OpenH264/FFmpeg codec is supported.
Magnus Jedvert849b3ae2017-09-29 17:54:09 +020034bool IsH264CodecSupported() {
35#if defined(WEBRTC_USE_H264)
36 return g_rtc_use_h264;
37#else
38 return false;
39#endif
40}
41
Magnus Jedvert8deb8182017-10-05 13:13:32 +020042SdpVideoFormat 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
hbos9dc59282016-02-03 05:09:37 -080052} // namespace
53
54void DisableRtcUseH264() {
55#if defined(WEBRTC_USE_H264)
56 g_rtc_use_h264 = false;
57#endif
58}
59
Magnus Jedvert849b3ae2017-09-29 17:54:09 +020060std::vector<SdpVideoFormat> SupportedH264Codecs() {
61 if (!IsH264CodecSupported())
62 return std::vector<SdpVideoFormat>();
Magnus Jedvert8deb8182017-10-05 13:13:32 +020063 // 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.
69 return {CreateH264Format(H264::kProfileHigh, H264::kLevel3_1),
70 CreateH264Format(H264::kProfileConstrainedBaseline, H264::kLevel3_1),
71 CreateH264Format(H264::kProfileBaseline, H264::kLevel3_1)};
Zeke Chin71f6f442015-06-29 14:34:58 -070072}
73
magjedceecea42016-11-28 07:20:21 -080074H264Encoder* H264Encoder::Create(const cricket::VideoCodec& codec) {
henrikg91d6ede2015-09-17 00:24:34 -070075 RTC_DCHECK(H264Encoder::IsSupported());
hbos9dc59282016-02-03 05:09:37 -080076#if defined(WEBRTC_USE_H264)
77 RTC_CHECK(g_rtc_use_h264);
hbosbab934b2016-01-27 01:36:03 -080078 LOG(LS_INFO) << "Creating H264EncoderImpl.";
hta9aa96882016-12-06 05:36:03 -080079 return new H264EncoderImpl(codec);
Zeke Chin71f6f442015-06-29 14:34:58 -070080#else
81 RTC_NOTREACHED();
82 return nullptr;
83#endif
84}
85
86bool H264Encoder::IsSupported() {
87 return IsH264CodecSupported();
88}
89
90H264Decoder* H264Decoder::Create() {
henrikg91d6ede2015-09-17 00:24:34 -070091 RTC_DCHECK(H264Decoder::IsSupported());
hbos9dc59282016-02-03 05:09:37 -080092#if defined(WEBRTC_USE_H264)
93 RTC_CHECK(g_rtc_use_h264);
hbosbab934b2016-01-27 01:36:03 -080094 LOG(LS_INFO) << "Creating H264DecoderImpl.";
95 return new H264DecoderImpl();
Zeke Chin71f6f442015-06-29 14:34:58 -070096#else
97 RTC_NOTREACHED();
98 return nullptr;
99#endif
100}
101
102bool H264Decoder::IsSupported() {
103 return IsH264CodecSupported();
104}
105
106} // namespace webrtc