Anders Carlsson | 7bca8ca | 2018-08-30 09:30:29 +0200 | [diff] [blame] | 1 | /* |
| 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 | |
| 19 | #include "media/base/h264_profile_level_id.h" |
Steve Anton | 10542f2 | 2019-01-11 09:11:00 -0800 | [diff] [blame^] | 20 | #include "media/base/media_constants.h" |
Anders Carlsson | 7bca8ca | 2018-08-30 09:30:29 +0200 | [diff] [blame] | 21 | |
| 22 | namespace { |
| 23 | |
| 24 | NSString *MaxSupportedProfileLevelConstrainedHigh(); |
| 25 | NSString *MaxSupportedProfileLevelConstrainedBaseline(); |
| 26 | |
| 27 | } // namespace |
| 28 | |
| 29 | NSString *const kRTCVideoCodecH264Name = @(cricket::kH264CodecName); |
| 30 | NSString *const kRTCLevel31ConstrainedHigh = @"640c1f"; |
| 31 | NSString *const kRTCLevel31ConstrainedBaseline = @"42e01f"; |
| 32 | NSString *const kRTCMaxSupportedH264ProfileLevelConstrainedHigh = |
| 33 | MaxSupportedProfileLevelConstrainedHigh(); |
| 34 | NSString *const kRTCMaxSupportedH264ProfileLevelConstrainedBaseline = |
| 35 | MaxSupportedProfileLevelConstrainedBaseline(); |
| 36 | |
| 37 | namespace { |
| 38 | |
| 39 | #if defined(WEBRTC_IOS) |
| 40 | |
| 41 | using namespace webrtc::H264; |
| 42 | |
| 43 | NSString *MaxSupportedLevelForProfile(Profile profile) { |
| 44 | const absl::optional<ProfileLevelId> profileLevelId = [UIDevice maxSupportedH264Profile]; |
| 45 | if (profileLevelId && profileLevelId->profile >= profile) { |
| 46 | const absl::optional<std::string> profileString = |
| 47 | ProfileLevelIdToString(ProfileLevelId(profile, profileLevelId->level)); |
| 48 | if (profileString) { |
| 49 | return [NSString stringForStdString:*profileString]; |
| 50 | } |
| 51 | } |
| 52 | return nil; |
| 53 | } |
| 54 | #endif |
| 55 | |
| 56 | NSString *MaxSupportedProfileLevelConstrainedBaseline() { |
| 57 | #if defined(WEBRTC_IOS) |
| 58 | NSString *profile = MaxSupportedLevelForProfile(webrtc::H264::kProfileConstrainedBaseline); |
| 59 | if (profile != nil) { |
| 60 | return profile; |
| 61 | } |
| 62 | #endif |
| 63 | return kRTCLevel31ConstrainedBaseline; |
| 64 | } |
| 65 | |
| 66 | NSString *MaxSupportedProfileLevelConstrainedHigh() { |
| 67 | #if defined(WEBRTC_IOS) |
| 68 | NSString *profile = MaxSupportedLevelForProfile(webrtc::H264::kProfileConstrainedHigh); |
| 69 | if (profile != nil) { |
| 70 | return profile; |
| 71 | } |
| 72 | #endif |
| 73 | return kRTCLevel31ConstrainedHigh; |
| 74 | } |
| 75 | |
| 76 | } // namespace |
| 77 | |
| 78 | @interface RTCH264ProfileLevelId () |
| 79 | |
| 80 | @property(nonatomic, assign) RTCH264Profile profile; |
| 81 | @property(nonatomic, assign) RTCH264Level level; |
| 82 | @property(nonatomic, strong) NSString *hexString; |
| 83 | |
| 84 | @end |
| 85 | |
| 86 | @implementation RTCH264ProfileLevelId |
| 87 | |
| 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 | |
| 96 | absl::optional<webrtc::H264::ProfileLevelId> profile_level_id = |
| 97 | webrtc::H264::ParseProfileLevelId([hexString cStringUsingEncoding:NSUTF8StringEncoding]); |
| 98 | 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 = |
| 112 | webrtc::H264::ProfileLevelIdToString(webrtc::H264::ProfileLevelId( |
| 113 | static_cast<webrtc::H264::Profile>(profile), static_cast<webrtc::H264::Level>(level))); |
| 114 | self.hexString = |
| 115 | [NSString stringWithCString:hex_string.value_or("").c_str() encoding:NSUTF8StringEncoding]; |
| 116 | } |
| 117 | return self; |
| 118 | } |
| 119 | |
| 120 | @end |