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