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 "RTCRtpEncodingParameters+Private.h"
12
13#import "helpers/NSString+StdString.h"
14
15@implementation RTC_OBJC_TYPE (RTCRtpEncodingParameters)
16
17@synthesize rid = _rid;
18@synthesize isActive = _isActive;
19@synthesize maxBitrateBps = _maxBitrateBps;
20@synthesize minBitrateBps = _minBitrateBps;
21@synthesize maxFramerate = _maxFramerate;
22@synthesize numTemporalLayers = _numTemporalLayers;
23@synthesize scaleResolutionDownBy = _scaleResolutionDownBy;
24@synthesize ssrc = _ssrc;
25@synthesize bitratePriority = _bitratePriority;
26@synthesize networkPriority = _networkPriority;
27
28- (instancetype)init {
29  return [super init];
30}
31
32- (instancetype)initWithNativeParameters:
33    (const webrtc::RtpEncodingParameters &)nativeParameters {
34  if (self = [self init]) {
35    if (!nativeParameters.rid.empty()) {
36      _rid = [NSString stringForStdString:nativeParameters.rid];
37    }
38    _isActive = nativeParameters.active;
39    if (nativeParameters.max_bitrate_bps) {
40      _maxBitrateBps =
41          [NSNumber numberWithInt:*nativeParameters.max_bitrate_bps];
42    }
43    if (nativeParameters.min_bitrate_bps) {
44      _minBitrateBps =
45          [NSNumber numberWithInt:*nativeParameters.min_bitrate_bps];
46    }
47    if (nativeParameters.max_framerate) {
48      _maxFramerate = [NSNumber numberWithInt:*nativeParameters.max_framerate];
49    }
50    if (nativeParameters.num_temporal_layers) {
51      _numTemporalLayers = [NSNumber numberWithInt:*nativeParameters.num_temporal_layers];
52    }
53    if (nativeParameters.scale_resolution_down_by) {
54      _scaleResolutionDownBy =
55          [NSNumber numberWithDouble:*nativeParameters.scale_resolution_down_by];
56    }
57    if (nativeParameters.ssrc) {
58      _ssrc = [NSNumber numberWithUnsignedLong:*nativeParameters.ssrc];
59    }
60    _bitratePriority = nativeParameters.bitrate_priority;
61    _networkPriority = [RTC_OBJC_TYPE(RTCRtpEncodingParameters)
62        priorityFromNativePriority:nativeParameters.network_priority];
63  }
64  return self;
65}
66
67- (webrtc::RtpEncodingParameters)nativeParameters {
68  webrtc::RtpEncodingParameters parameters;
69  if (_rid != nil) {
70    parameters.rid = [NSString stdStringForString:_rid];
71  }
72  parameters.active = _isActive;
73  if (_maxBitrateBps != nil) {
74    parameters.max_bitrate_bps = absl::optional<int>(_maxBitrateBps.intValue);
75  }
76  if (_minBitrateBps != nil) {
77    parameters.min_bitrate_bps = absl::optional<int>(_minBitrateBps.intValue);
78  }
79  if (_maxFramerate != nil) {
80    parameters.max_framerate = absl::optional<int>(_maxFramerate.intValue);
81  }
82  if (_numTemporalLayers != nil) {
83    parameters.num_temporal_layers = absl::optional<int>(_numTemporalLayers.intValue);
84  }
85  if (_scaleResolutionDownBy != nil) {
86    parameters.scale_resolution_down_by =
87        absl::optional<double>(_scaleResolutionDownBy.doubleValue);
88  }
89  if (_ssrc != nil) {
90    parameters.ssrc = absl::optional<uint32_t>(_ssrc.unsignedLongValue);
91  }
92  parameters.bitrate_priority = _bitratePriority;
93  parameters.network_priority =
94      [RTC_OBJC_TYPE(RTCRtpEncodingParameters) nativePriorityFromPriority:_networkPriority];
95  return parameters;
96}
97
98+ (webrtc::Priority)nativePriorityFromPriority:(RTCPriority)networkPriority {
99  switch (networkPriority) {
100    case RTCPriorityVeryLow:
101      return webrtc::Priority::kVeryLow;
102    case RTCPriorityLow:
103      return webrtc::Priority::kLow;
104    case RTCPriorityMedium:
105      return webrtc::Priority::kMedium;
106    case RTCPriorityHigh:
107      return webrtc::Priority::kHigh;
108  }
109}
110
111+ (RTCPriority)priorityFromNativePriority:(webrtc::Priority)nativePriority {
112  switch (nativePriority) {
113    case webrtc::Priority::kVeryLow:
114      return RTCPriorityVeryLow;
115    case webrtc::Priority::kLow:
116      return RTCPriorityLow;
117    case webrtc::Priority::kMedium:
118      return RTCPriorityMedium;
119    case webrtc::Priority::kHigh:
120      return RTCPriorityHigh;
121  }
122}
123
124@end
125