kthelgason | fb14312 | 2017-07-25 07:55:58 -0700 | [diff] [blame] | 1 | /* |
| 2 | * Copyright 2017 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 | #import "WebRTC/RTCVideoCodec.h" |
| 12 | |
| 13 | #import "NSString+StdString.h" |
| 14 | #import "RTCVideoCodec+Private.h" |
Yura Yaroshevich | 0f77fea | 2018-04-26 15:41:01 +0300 | [diff] [blame] | 15 | #if defined(WEBRTC_IOS) |
| 16 | #import "UIDevice+H264Profile.h" |
| 17 | #endif |
kthelgason | fb14312 | 2017-07-25 07:55:58 -0700 | [diff] [blame] | 18 | #import "WebRTC/RTCVideoCodecFactory.h" |
| 19 | |
VladimirTechMan | 80adac0 | 2017-11-23 12:22:11 -0500 | [diff] [blame] | 20 | #include "media/base/mediaconstants.h" |
| 21 | |
Yura Yaroshevich | 0f77fea | 2018-04-26 15:41:01 +0300 | [diff] [blame] | 22 | namespace { |
| 23 | |
| 24 | NSString *MaxSupportedProfileLevelConstrainedHigh(); |
| 25 | NSString *MaxSupportedProfileLevelConstrainedBaseline(); |
| 26 | |
| 27 | } // namespace |
| 28 | |
VladimirTechMan | 80adac0 | 2017-11-23 12:22:11 -0500 | [diff] [blame] | 29 | NSString *const kRTCVideoCodecVp8Name = @(cricket::kVp8CodecName); |
| 30 | NSString *const kRTCVideoCodecVp9Name = @(cricket::kVp9CodecName); |
| 31 | NSString *const kRTCVideoCodecH264Name = @(cricket::kH264CodecName); |
Kári Tristan Helgason | fc313dc | 2017-10-20 11:01:22 +0200 | [diff] [blame] | 32 | NSString *const kRTCLevel31ConstrainedHigh = @"640c1f"; |
| 33 | NSString *const kRTCLevel31ConstrainedBaseline = @"42e01f"; |
Yura Yaroshevich | 0f77fea | 2018-04-26 15:41:01 +0300 | [diff] [blame] | 34 | NSString *const kRTCMaxSupportedH264ProfileLevelConstrainedHigh = |
| 35 | MaxSupportedProfileLevelConstrainedHigh(); |
| 36 | NSString *const kRTCMaxSupportedH264ProfileLevelConstrainedBaseline = |
| 37 | MaxSupportedProfileLevelConstrainedBaseline(); |
| 38 | |
| 39 | namespace { |
| 40 | |
| 41 | #if defined(WEBRTC_IOS) |
| 42 | |
| 43 | using namespace webrtc::H264; |
| 44 | |
| 45 | NSString *MaxSupportedLevelForProfile(Profile profile) { |
Danil Chapovalov | 196100e | 2018-06-21 10:17:24 +0200 | [diff] [blame] | 46 | const absl::optional<ProfileLevelId> profileLevelId = [UIDevice maxSupportedH264Profile]; |
Yura Yaroshevich | 0f77fea | 2018-04-26 15:41:01 +0300 | [diff] [blame] | 47 | if (profileLevelId && profileLevelId->profile >= profile) { |
Danil Chapovalov | 196100e | 2018-06-21 10:17:24 +0200 | [diff] [blame] | 48 | const absl::optional<std::string> profileString = |
Yura Yaroshevich | 0f77fea | 2018-04-26 15:41:01 +0300 | [diff] [blame] | 49 | ProfileLevelIdToString(ProfileLevelId(profile, profileLevelId->level)); |
| 50 | if (profileString) { |
| 51 | return [NSString stringForStdString:*profileString]; |
| 52 | } |
| 53 | } |
| 54 | return nil; |
| 55 | } |
| 56 | #endif |
| 57 | |
| 58 | NSString *MaxSupportedProfileLevelConstrainedBaseline() { |
| 59 | #if defined(WEBRTC_IOS) |
| 60 | NSString *profile = MaxSupportedLevelForProfile(webrtc::H264::kProfileConstrainedBaseline); |
| 61 | if (profile != nil) { |
| 62 | return profile; |
| 63 | } |
| 64 | #endif |
| 65 | return kRTCLevel31ConstrainedBaseline; |
| 66 | } |
| 67 | |
| 68 | NSString *MaxSupportedProfileLevelConstrainedHigh() { |
| 69 | #if defined(WEBRTC_IOS) |
| 70 | NSString *profile = MaxSupportedLevelForProfile(webrtc::H264::kProfileConstrainedHigh); |
| 71 | if (profile != nil) { |
| 72 | return profile; |
| 73 | } |
| 74 | #endif |
| 75 | return kRTCLevel31ConstrainedHigh; |
| 76 | } |
| 77 | |
| 78 | } // namespace |
Kári Tristan Helgason | 117c482 | 2017-10-18 14:22:22 +0200 | [diff] [blame] | 79 | |
kthelgason | fb14312 | 2017-07-25 07:55:58 -0700 | [diff] [blame] | 80 | @implementation RTCVideoCodecInfo |
| 81 | |
kthelgason | fb14312 | 2017-07-25 07:55:58 -0700 | [diff] [blame] | 82 | @synthesize name = _name; |
| 83 | @synthesize parameters = _parameters; |
| 84 | |
Kári Tristan Helgason | e71f367 | 2017-10-02 14:59:59 +0200 | [diff] [blame] | 85 | - (instancetype)initWithName:(NSString *)name { |
| 86 | return [self initWithName:name parameters:nil]; |
| 87 | } |
| 88 | |
andersc | 81bc523 | 2017-08-18 06:34:09 -0700 | [diff] [blame] | 89 | - (instancetype)initWithName:(NSString *)name |
| 90 | parameters:(nullable NSDictionary<NSString *, NSString *> *)parameters { |
kthelgason | fb14312 | 2017-07-25 07:55:58 -0700 | [diff] [blame] | 91 | if (self = [super init]) { |
kthelgason | fb14312 | 2017-07-25 07:55:58 -0700 | [diff] [blame] | 92 | _name = name; |
andersc | 81bc523 | 2017-08-18 06:34:09 -0700 | [diff] [blame] | 93 | _parameters = (parameters ? parameters : @{}); |
kthelgason | fb14312 | 2017-07-25 07:55:58 -0700 | [diff] [blame] | 94 | } |
| 95 | |
| 96 | return self; |
| 97 | } |
| 98 | |
Anders Carlsson | 7e04281 | 2017-10-05 16:55:38 +0200 | [diff] [blame] | 99 | - (instancetype)initWithNativeSdpVideoFormat:(webrtc::SdpVideoFormat)format { |
kthelgason | fb14312 | 2017-07-25 07:55:58 -0700 | [diff] [blame] | 100 | NSMutableDictionary *params = [NSMutableDictionary dictionary]; |
Anders Carlsson | 7e04281 | 2017-10-05 16:55:38 +0200 | [diff] [blame] | 101 | for (auto it = format.parameters.begin(); it != format.parameters.end(); ++it) { |
kthelgason | fb14312 | 2017-07-25 07:55:58 -0700 | [diff] [blame] | 102 | [params setObject:[NSString stringForStdString:it->second] |
| 103 | forKey:[NSString stringForStdString:it->first]]; |
| 104 | } |
Anders Carlsson | 7e04281 | 2017-10-05 16:55:38 +0200 | [diff] [blame] | 105 | return [self initWithName:[NSString stringForStdString:format.name] parameters:params]; |
kthelgason | fb14312 | 2017-07-25 07:55:58 -0700 | [diff] [blame] | 106 | } |
| 107 | |
Kári Tristan Helgason | 3935c34 | 2017-09-28 15:08:47 +0200 | [diff] [blame] | 108 | - (BOOL)isEqualToCodecInfo:(RTCVideoCodecInfo *)info { |
| 109 | if (!info || |
Kári Tristan Helgason | 3935c34 | 2017-09-28 15:08:47 +0200 | [diff] [blame] | 110 | ![self.name isEqualToString:info.name] || |
| 111 | ![self.parameters isEqualToDictionary:info.parameters]) { |
| 112 | return NO; |
| 113 | } |
| 114 | return YES; |
| 115 | } |
| 116 | |
| 117 | - (BOOL)isEqual:(id)object { |
| 118 | if (self == object) |
| 119 | return YES; |
| 120 | if (![object isKindOfClass:[self class]]) |
| 121 | return NO; |
| 122 | return [self isEqualToCodecInfo:object]; |
| 123 | } |
| 124 | |
| 125 | - (NSUInteger)hash { |
Anders Carlsson | 7e04281 | 2017-10-05 16:55:38 +0200 | [diff] [blame] | 126 | return [self.name hash] ^ [self.parameters hash]; |
| 127 | } |
| 128 | |
| 129 | - (webrtc::SdpVideoFormat)nativeSdpVideoFormat { |
| 130 | std::map<std::string, std::string> parameters; |
| 131 | for (NSString *paramKey in _parameters.allKeys) { |
| 132 | std::string key = [NSString stdStringForString:paramKey]; |
| 133 | std::string value = [NSString stdStringForString:_parameters[paramKey]]; |
| 134 | parameters[key] = value; |
| 135 | } |
| 136 | |
| 137 | return webrtc::SdpVideoFormat([NSString stdStringForString:_name], parameters); |
| 138 | } |
| 139 | |
Anders Carlsson | 6bf43d2 | 2017-10-16 13:51:43 +0200 | [diff] [blame] | 140 | #pragma mark - NSCoding |
| 141 | |
| 142 | - (instancetype)initWithCoder:(NSCoder *)decoder { |
| 143 | return [self initWithName:[decoder decodeObjectForKey:@"name"] |
| 144 | parameters:[decoder decodeObjectForKey:@"parameters"]]; |
| 145 | } |
| 146 | |
| 147 | - (void)encodeWithCoder:(NSCoder *)encoder { |
| 148 | [encoder encodeObject:_name forKey:@"name"]; |
| 149 | [encoder encodeObject:_parameters forKey:@"parameters"]; |
| 150 | } |
| 151 | |
kthelgason | fb14312 | 2017-07-25 07:55:58 -0700 | [diff] [blame] | 152 | @end |
magjed | 5dfac33 | 2017-08-01 08:07:59 -0700 | [diff] [blame] | 153 | |
| 154 | @implementation RTCVideoEncoderQpThresholds |
| 155 | |
| 156 | @synthesize low = _low; |
| 157 | @synthesize high = _high; |
| 158 | |
| 159 | - (instancetype)initWithThresholdsLow:(NSInteger)low high:(NSInteger)high { |
| 160 | if (self = [super init]) { |
| 161 | _low = low; |
| 162 | _high = high; |
| 163 | } |
| 164 | return self; |
| 165 | } |
| 166 | |
| 167 | @end |