blob: f0ef3ec232244a55fc96812d339cd1eae71a1747 [file] [log] [blame]
Anders Carlsson7bca8ca2018-08-30 09:30:29 +02001/*
2 * Copyright 2018 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#import "RTCH264ProfileLevelId.h"
13
14#import "helpers/NSString+StdString.h"
15#if defined(WEBRTC_IOS)
16#import "UIDevice+H264Profile.h"
17#endif
18
Johannes Kronc3fcee72021-04-19 09:09:26 +020019#include "api/video_codecs/h264_profile_level_id.h"
Steve Anton10542f22019-01-11 09:11:00 -080020#include "media/base/media_constants.h"
Anders Carlsson7bca8ca2018-08-30 09:30:29 +020021
22namespace {
23
24NSString *MaxSupportedProfileLevelConstrainedHigh();
25NSString *MaxSupportedProfileLevelConstrainedBaseline();
26
27} // namespace
28
29NSString *const kRTCVideoCodecH264Name = @(cricket::kH264CodecName);
30NSString *const kRTCLevel31ConstrainedHigh = @"640c1f";
31NSString *const kRTCLevel31ConstrainedBaseline = @"42e01f";
32NSString *const kRTCMaxSupportedH264ProfileLevelConstrainedHigh =
33 MaxSupportedProfileLevelConstrainedHigh();
34NSString *const kRTCMaxSupportedH264ProfileLevelConstrainedBaseline =
35 MaxSupportedProfileLevelConstrainedBaseline();
36
37namespace {
38
39#if defined(WEBRTC_IOS)
40
Johannes Kronc3fcee72021-04-19 09:09:26 +020041NSString *MaxSupportedLevelForProfile(webrtc::H264Profile profile) {
42 const absl::optional<webrtc::H264ProfileLevelId> profileLevelId =
43 [UIDevice maxSupportedH264Profile];
Anders Carlsson7bca8ca2018-08-30 09:30:29 +020044 if (profileLevelId && profileLevelId->profile >= profile) {
45 const absl::optional<std::string> profileString =
Johannes Kronc3fcee72021-04-19 09:09:26 +020046 H264ProfileLevelIdToString(webrtc::H264ProfileLevelId(profile, profileLevelId->level));
Anders Carlsson7bca8ca2018-08-30 09:30:29 +020047 if (profileString) {
48 return [NSString stringForStdString:*profileString];
49 }
50 }
51 return nil;
52}
53#endif
54
55NSString *MaxSupportedProfileLevelConstrainedBaseline() {
56#if defined(WEBRTC_IOS)
Johannes Kronc3fcee72021-04-19 09:09:26 +020057 NSString *profile = MaxSupportedLevelForProfile(webrtc::H264Profile::kProfileConstrainedBaseline);
Anders Carlsson7bca8ca2018-08-30 09:30:29 +020058 if (profile != nil) {
59 return profile;
60 }
61#endif
62 return kRTCLevel31ConstrainedBaseline;
63}
64
65NSString *MaxSupportedProfileLevelConstrainedHigh() {
66#if defined(WEBRTC_IOS)
Johannes Kronc3fcee72021-04-19 09:09:26 +020067 NSString *profile = MaxSupportedLevelForProfile(webrtc::H264Profile::kProfileConstrainedHigh);
Anders Carlsson7bca8ca2018-08-30 09:30:29 +020068 if (profile != nil) {
69 return profile;
70 }
71#endif
72 return kRTCLevel31ConstrainedHigh;
73}
74
75} // namespace
76
Mirko Bonadeia81e9c82020-05-04 16:14:32 +020077@interface RTC_OBJC_TYPE (RTCH264ProfileLevelId)
78()
Anders Carlsson7bca8ca2018-08-30 09:30:29 +020079
Mirko Bonadeia81e9c82020-05-04 16:14:32 +020080 @property(nonatomic, assign) RTCH264Profile profile;
Anders Carlsson7bca8ca2018-08-30 09:30:29 +020081@property(nonatomic, assign) RTCH264Level level;
82@property(nonatomic, strong) NSString *hexString;
83
84@end
85
Mirko Bonadeia81e9c82020-05-04 16:14:32 +020086@implementation RTC_OBJC_TYPE (RTCH264ProfileLevelId)
Anders Carlsson7bca8ca2018-08-30 09:30:29 +020087
88@synthesize profile = _profile;
89@synthesize level = _level;
90@synthesize hexString = _hexString;
91
92- (instancetype)initWithHexString:(NSString *)hexString {
93 if (self = [super init]) {
94 self.hexString = hexString;
95
Johannes Kronc3fcee72021-04-19 09:09:26 +020096 absl::optional<webrtc::H264ProfileLevelId> profile_level_id =
97 webrtc::ParseH264ProfileLevelId([hexString cStringUsingEncoding:NSUTF8StringEncoding]);
Anders Carlsson7bca8ca2018-08-30 09:30:29 +020098 if (profile_level_id.has_value()) {
99 self.profile = static_cast<RTCH264Profile>(profile_level_id->profile);
100 self.level = static_cast<RTCH264Level>(profile_level_id->level);
101 }
102 }
103 return self;
104}
105
106- (instancetype)initWithProfile:(RTCH264Profile)profile level:(RTCH264Level)level {
107 if (self = [super init]) {
108 self.profile = profile;
109 self.level = level;
110
111 absl::optional<std::string> hex_string =
Johannes Kronc3fcee72021-04-19 09:09:26 +0200112 webrtc::H264ProfileLevelIdToString(webrtc::H264ProfileLevelId(
113 static_cast<webrtc::H264Profile>(profile), static_cast<webrtc::H264Level>(level)));
Anders Carlsson7bca8ca2018-08-30 09:30:29 +0200114 self.hexString =
115 [NSString stringWithCString:hex_string.value_or("").c_str() encoding:NSUTF8StringEncoding];
116 }
117 return self;
118}
119
120@end