xref: /aosp_15_r20/external/webrtc/sdk/objc/components/video_codec/RTCDefaultVideoEncoderFactory.m (revision d9f758449e529ab9291ac668be2861e7a55c2422)
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 "RTCDefaultVideoEncoderFactory.h"
12
13#import "RTCH264ProfileLevelId.h"
14#import "RTCVideoEncoderH264.h"
15#import "api/video_codec/RTCVideoCodecConstants.h"
16#import "api/video_codec/RTCVideoEncoderVP8.h"
17#import "api/video_codec/RTCVideoEncoderVP9.h"
18#import "base/RTCVideoCodecInfo.h"
19
20#if defined(RTC_USE_LIBAOM_AV1_ENCODER)
21#import "api/video_codec/RTCVideoEncoderAV1.h"  // nogncheck
22#endif
23
24@implementation RTC_OBJC_TYPE (RTCDefaultVideoEncoderFactory)
25
26@synthesize preferredCodec;
27
28+ (NSArray<RTC_OBJC_TYPE(RTCVideoCodecInfo) *> *)supportedCodecs {
29  NSDictionary<NSString *, NSString *> *constrainedHighParams = @{
30    @"profile-level-id" : kRTCMaxSupportedH264ProfileLevelConstrainedHigh,
31    @"level-asymmetry-allowed" : @"1",
32    @"packetization-mode" : @"1",
33  };
34  RTC_OBJC_TYPE(RTCVideoCodecInfo) *constrainedHighInfo =
35      [[RTC_OBJC_TYPE(RTCVideoCodecInfo) alloc] initWithName:kRTCVideoCodecH264Name
36                                                  parameters:constrainedHighParams];
37
38  NSDictionary<NSString *, NSString *> *constrainedBaselineParams = @{
39    @"profile-level-id" : kRTCMaxSupportedH264ProfileLevelConstrainedBaseline,
40    @"level-asymmetry-allowed" : @"1",
41    @"packetization-mode" : @"1",
42  };
43  RTC_OBJC_TYPE(RTCVideoCodecInfo) *constrainedBaselineInfo =
44      [[RTC_OBJC_TYPE(RTCVideoCodecInfo) alloc] initWithName:kRTCVideoCodecH264Name
45                                                  parameters:constrainedBaselineParams];
46
47  RTC_OBJC_TYPE(RTCVideoCodecInfo) *vp8Info =
48      [[RTC_OBJC_TYPE(RTCVideoCodecInfo) alloc] initWithName:kRTCVideoCodecVp8Name];
49
50  NSMutableArray<RTC_OBJC_TYPE(RTCVideoCodecInfo) *> *result = [@[
51    constrainedHighInfo,
52    constrainedBaselineInfo,
53    vp8Info,
54  ] mutableCopy];
55
56  if ([RTC_OBJC_TYPE(RTCVideoEncoderVP9) isSupported]) {
57    [result
58        addObject:[[RTC_OBJC_TYPE(RTCVideoCodecInfo) alloc] initWithName:kRTCVideoCodecVp9Name]];
59  }
60
61#if defined(RTC_USE_LIBAOM_AV1_ENCODER)
62  [result addObject:[[RTC_OBJC_TYPE(RTCVideoCodecInfo) alloc] initWithName:kRTCVideoCodecAv1Name]];
63#endif
64
65  return result;
66}
67
68- (id<RTC_OBJC_TYPE(RTCVideoEncoder)>)createEncoder:(RTC_OBJC_TYPE(RTCVideoCodecInfo) *)info {
69  if ([info.name isEqualToString:kRTCVideoCodecH264Name]) {
70    return [[RTC_OBJC_TYPE(RTCVideoEncoderH264) alloc] initWithCodecInfo:info];
71  } else if ([info.name isEqualToString:kRTCVideoCodecVp8Name]) {
72    return [RTC_OBJC_TYPE(RTCVideoEncoderVP8) vp8Encoder];
73  } else if ([info.name isEqualToString:kRTCVideoCodecVp9Name] &&
74             [RTC_OBJC_TYPE(RTCVideoEncoderVP9) isSupported]) {
75    return [RTC_OBJC_TYPE(RTCVideoEncoderVP9) vp9Encoder];
76  }
77
78#if defined(RTC_USE_LIBAOM_AV1_ENCODER)
79  if ([info.name isEqualToString:kRTCVideoCodecAv1Name]) {
80    return [RTC_OBJC_TYPE(RTCVideoEncoderAV1) av1Encoder];
81  }
82#endif
83
84  return nil;
85}
86
87- (NSArray<RTC_OBJC_TYPE(RTCVideoCodecInfo) *> *)supportedCodecs {
88  NSMutableArray<RTC_OBJC_TYPE(RTCVideoCodecInfo) *> *codecs =
89      [[[self class] supportedCodecs] mutableCopy];
90
91  NSMutableArray<RTC_OBJC_TYPE(RTCVideoCodecInfo) *> *orderedCodecs = [NSMutableArray array];
92  NSUInteger index = [codecs indexOfObject:self.preferredCodec];
93  if (index != NSNotFound) {
94    [orderedCodecs addObject:[codecs objectAtIndex:index]];
95    [codecs removeObjectAtIndex:index];
96  }
97  [orderedCodecs addObjectsFromArray:codecs];
98
99  return [orderedCodecs copy];
100}
101
102@end
103