1/*
2 *  Copyright 2016 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 "RTCRtpParameters+Private.h"
12
13#import "RTCRtcpParameters+Private.h"
14#import "RTCRtpCodecParameters+Private.h"
15#import "RTCRtpEncodingParameters+Private.h"
16#import "RTCRtpHeaderExtension+Private.h"
17#import "helpers/NSString+StdString.h"
18
19@implementation RTC_OBJC_TYPE (RTCRtpParameters)
20
21@synthesize transactionId = _transactionId;
22@synthesize rtcp = _rtcp;
23@synthesize headerExtensions = _headerExtensions;
24@synthesize encodings = _encodings;
25@synthesize codecs = _codecs;
26@synthesize degradationPreference = _degradationPreference;
27
28- (instancetype)init {
29  return [super init];
30}
31
32- (instancetype)initWithNativeParameters:
33    (const webrtc::RtpParameters &)nativeParameters {
34  if (self = [self init]) {
35    _transactionId = [NSString stringForStdString:nativeParameters.transaction_id];
36    _rtcp =
37        [[RTC_OBJC_TYPE(RTCRtcpParameters) alloc] initWithNativeParameters:nativeParameters.rtcp];
38
39    NSMutableArray *headerExtensions = [[NSMutableArray alloc] init];
40    for (const auto &headerExtension : nativeParameters.header_extensions) {
41      [headerExtensions addObject:[[RTC_OBJC_TYPE(RTCRtpHeaderExtension) alloc]
42                                      initWithNativeParameters:headerExtension]];
43    }
44    _headerExtensions = headerExtensions;
45
46    NSMutableArray *encodings = [[NSMutableArray alloc] init];
47    for (const auto &encoding : nativeParameters.encodings) {
48      [encodings addObject:[[RTC_OBJC_TYPE(RTCRtpEncodingParameters) alloc]
49                               initWithNativeParameters:encoding]];
50    }
51    _encodings = encodings;
52
53    NSMutableArray *codecs = [[NSMutableArray alloc] init];
54    for (const auto &codec : nativeParameters.codecs) {
55      [codecs
56          addObject:[[RTC_OBJC_TYPE(RTCRtpCodecParameters) alloc] initWithNativeParameters:codec]];
57    }
58    _codecs = codecs;
59
60    _degradationPreference = [RTC_OBJC_TYPE(RTCRtpParameters)
61        degradationPreferenceFromNativeDegradationPreference:nativeParameters
62                                                                 .degradation_preference];
63  }
64  return self;
65}
66
67- (webrtc::RtpParameters)nativeParameters {
68  webrtc::RtpParameters parameters;
69  parameters.transaction_id = [NSString stdStringForString:_transactionId];
70  parameters.rtcp = [_rtcp nativeParameters];
71  for (RTC_OBJC_TYPE(RTCRtpHeaderExtension) * headerExtension in _headerExtensions) {
72    parameters.header_extensions.push_back(headerExtension.nativeParameters);
73  }
74  for (RTC_OBJC_TYPE(RTCRtpEncodingParameters) * encoding in _encodings) {
75    parameters.encodings.push_back(encoding.nativeParameters);
76  }
77  for (RTC_OBJC_TYPE(RTCRtpCodecParameters) * codec in _codecs) {
78    parameters.codecs.push_back(codec.nativeParameters);
79  }
80  if (_degradationPreference) {
81    parameters.degradation_preference = [RTC_OBJC_TYPE(RTCRtpParameters)
82        nativeDegradationPreferenceFromDegradationPreference:(RTCDegradationPreference)
83                                                                 _degradationPreference.intValue];
84  }
85  return parameters;
86}
87
88+ (webrtc::DegradationPreference)nativeDegradationPreferenceFromDegradationPreference:
89    (RTCDegradationPreference)degradationPreference {
90  switch (degradationPreference) {
91    case RTCDegradationPreferenceDisabled:
92      return webrtc::DegradationPreference::DISABLED;
93    case RTCDegradationPreferenceMaintainFramerate:
94      return webrtc::DegradationPreference::MAINTAIN_FRAMERATE;
95    case RTCDegradationPreferenceMaintainResolution:
96      return webrtc::DegradationPreference::MAINTAIN_RESOLUTION;
97    case RTCDegradationPreferenceBalanced:
98      return webrtc::DegradationPreference::BALANCED;
99  }
100}
101
102+ (NSNumber *)degradationPreferenceFromNativeDegradationPreference:
103    (absl::optional<webrtc::DegradationPreference>)nativeDegradationPreference {
104  if (!nativeDegradationPreference.has_value()) {
105    return nil;
106  }
107
108  switch (*nativeDegradationPreference) {
109    case webrtc::DegradationPreference::DISABLED:
110      return @(RTCDegradationPreferenceDisabled);
111    case webrtc::DegradationPreference::MAINTAIN_FRAMERATE:
112      return @(RTCDegradationPreferenceMaintainFramerate);
113    case webrtc::DegradationPreference::MAINTAIN_RESOLUTION:
114      return @(RTCDegradationPreferenceMaintainResolution);
115    case webrtc::DegradationPreference::BALANCED:
116      return @(RTCDegradationPreferenceBalanced);
117  }
118}
119
120@end
121