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"
20#include "media/base/media_constants.h"
21
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
41using namespace webrtc::H264;
42
43NSString *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
56NSString *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
66NSString *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 RTC_OBJC_TYPE (RTCH264ProfileLevelId)
79()
80
81    @property(nonatomic, assign) RTCH264Profile profile;
82@property(nonatomic, assign) RTCH264Level level;
83@property(nonatomic, strong) NSString *hexString;
84
85@end
86
87@implementation RTC_OBJC_TYPE (RTCH264ProfileLevelId)
88
89@synthesize profile = _profile;
90@synthesize level = _level;
91@synthesize hexString = _hexString;
92
93- (instancetype)initWithHexString:(NSString *)hexString {
94  if (self = [super init]) {
95    self.hexString = hexString;
96
97    absl::optional<webrtc::H264::ProfileLevelId> profile_level_id =
98        webrtc::H264::ParseProfileLevelId([hexString cStringUsingEncoding:NSUTF8StringEncoding]);
99    if (profile_level_id.has_value()) {
100      self.profile = static_cast<RTCH264Profile>(profile_level_id->profile);
101      self.level = static_cast<RTCH264Level>(profile_level_id->level);
102    }
103  }
104  return self;
105}
106
107- (instancetype)initWithProfile:(RTCH264Profile)profile level:(RTCH264Level)level {
108  if (self = [super init]) {
109    self.profile = profile;
110    self.level = level;
111
112    absl::optional<std::string> hex_string =
113        webrtc::H264::ProfileLevelIdToString(webrtc::H264::ProfileLevelId(
114            static_cast<webrtc::H264::Profile>(profile), static_cast<webrtc::H264::Level>(level)));
115    self.hexString =
116        [NSString stringWithCString:hex_string.value_or("").c_str() encoding:NSUTF8StringEncoding];
117  }
118  return self;
119}
120
121@end
122