Steve Anton | 8cb344a | 2018-02-27 15:34:53 -0800 | [diff] [blame] | 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 | #import "RTCRtpTransceiver+Private.h" |
| 12 | |
Steve Anton | 8cb344a | 2018-02-27 15:34:53 -0800 | [diff] [blame] | 13 | #import "RTCRtpEncodingParameters+Private.h" |
| 14 | #import "RTCRtpParameters+Private.h" |
| 15 | #import "RTCRtpReceiver+Private.h" |
| 16 | #import "RTCRtpSender+Private.h" |
Anders Carlsson | 7bca8ca | 2018-08-30 09:30:29 +0200 | [diff] [blame] | 17 | #import "base/RTCLogging.h" |
| 18 | #import "helpers/NSString+StdString.h" |
Steve Anton | 8cb344a | 2018-02-27 15:34:53 -0800 | [diff] [blame] | 19 | |
Harald Alvestrand | fcf5e7b | 2020-08-17 10:07:26 +0200 | [diff] [blame] | 20 | NSString *const kRTCRtpTransceiverErrorDomain = @"org.webrtc.RTCRtpTranceiver"; |
| 21 | |
Mirko Bonadei | a81e9c8 | 2020-05-04 16:14:32 +0200 | [diff] [blame] | 22 | @implementation RTC_OBJC_TYPE (RTCRtpTransceiverInit) |
Steve Anton | 8cb344a | 2018-02-27 15:34:53 -0800 | [diff] [blame] | 23 | |
| 24 | @synthesize direction = _direction; |
Seth Hampson | 513449e | 2018-03-06 09:35:56 -0800 | [diff] [blame] | 25 | @synthesize streamIds = _streamIds; |
Steve Anton | 8cb344a | 2018-02-27 15:34:53 -0800 | [diff] [blame] | 26 | @synthesize sendEncodings = _sendEncodings; |
| 27 | |
| 28 | - (instancetype)init { |
| 29 | if (self = [super init]) { |
| 30 | _direction = RTCRtpTransceiverDirectionSendRecv; |
| 31 | } |
| 32 | return self; |
| 33 | } |
| 34 | |
| 35 | - (webrtc::RtpTransceiverInit)nativeInit { |
| 36 | webrtc::RtpTransceiverInit init; |
Mirko Bonadei | a81e9c8 | 2020-05-04 16:14:32 +0200 | [diff] [blame] | 37 | init.direction = |
| 38 | [RTC_OBJC_TYPE(RTCRtpTransceiver) nativeRtpTransceiverDirectionFromDirection:_direction]; |
Seth Hampson | 513449e | 2018-03-06 09:35:56 -0800 | [diff] [blame] | 39 | for (NSString *streamId in _streamIds) { |
| 40 | init.stream_ids.push_back([streamId UTF8String]); |
Steve Anton | 8cb344a | 2018-02-27 15:34:53 -0800 | [diff] [blame] | 41 | } |
Mirko Bonadei | a81e9c8 | 2020-05-04 16:14:32 +0200 | [diff] [blame] | 42 | for (RTC_OBJC_TYPE(RTCRtpEncodingParameters) * sendEncoding in _sendEncodings) { |
Steve Anton | 8cb344a | 2018-02-27 15:34:53 -0800 | [diff] [blame] | 43 | init.send_encodings.push_back(sendEncoding.nativeParameters); |
| 44 | } |
| 45 | return init; |
| 46 | } |
| 47 | |
| 48 | @end |
| 49 | |
Mirko Bonadei | a81e9c8 | 2020-05-04 16:14:32 +0200 | [diff] [blame] | 50 | @implementation RTC_OBJC_TYPE (RTCRtpTransceiver) { |
| 51 | RTC_OBJC_TYPE(RTCPeerConnectionFactory) * _factory; |
Steve Anton | 8cb344a | 2018-02-27 15:34:53 -0800 | [diff] [blame] | 52 | rtc::scoped_refptr<webrtc::RtpTransceiverInterface> _nativeRtpTransceiver; |
| 53 | } |
| 54 | |
| 55 | - (RTCRtpMediaType)mediaType { |
Mirko Bonadei | a81e9c8 | 2020-05-04 16:14:32 +0200 | [diff] [blame] | 56 | return [RTC_OBJC_TYPE(RTCRtpReceiver) |
| 57 | mediaTypeForNativeMediaType:_nativeRtpTransceiver->media_type()]; |
Steve Anton | 8cb344a | 2018-02-27 15:34:53 -0800 | [diff] [blame] | 58 | } |
| 59 | |
| 60 | - (NSString *)mid { |
| 61 | if (_nativeRtpTransceiver->mid()) { |
| 62 | return [NSString stringForStdString:*_nativeRtpTransceiver->mid()]; |
| 63 | } else { |
| 64 | return nil; |
| 65 | } |
| 66 | } |
| 67 | |
| 68 | @synthesize sender = _sender; |
| 69 | @synthesize receiver = _receiver; |
| 70 | |
| 71 | - (BOOL)isStopped { |
| 72 | return _nativeRtpTransceiver->stopped(); |
| 73 | } |
| 74 | |
| 75 | - (RTCRtpTransceiverDirection)direction { |
Mirko Bonadei | a81e9c8 | 2020-05-04 16:14:32 +0200 | [diff] [blame] | 76 | return [RTC_OBJC_TYPE(RTCRtpTransceiver) |
Steve Anton | 8cb344a | 2018-02-27 15:34:53 -0800 | [diff] [blame] | 77 | rtpTransceiverDirectionFromNativeDirection:_nativeRtpTransceiver->direction()]; |
| 78 | } |
| 79 | |
Harald Alvestrand | fcf5e7b | 2020-08-17 10:07:26 +0200 | [diff] [blame] | 80 | - (void)setDirection:(RTCRtpTransceiverDirection)direction error:(NSError **)error { |
| 81 | webrtc::RTCError nativeError = _nativeRtpTransceiver->SetDirectionWithError( |
Mirko Bonadei | a81e9c8 | 2020-05-04 16:14:32 +0200 | [diff] [blame] | 82 | [RTC_OBJC_TYPE(RTCRtpTransceiver) nativeRtpTransceiverDirectionFromDirection:direction]); |
Harald Alvestrand | fcf5e7b | 2020-08-17 10:07:26 +0200 | [diff] [blame] | 83 | |
| 84 | if (!nativeError.ok() && error) { |
| 85 | *error = [NSError errorWithDomain:kRTCRtpTransceiverErrorDomain |
| 86 | code:static_cast<int>(nativeError.type()) |
| 87 | userInfo:@{ |
| 88 | @"message" : [NSString stringWithCString:nativeError.message() |
| 89 | encoding:NSUTF8StringEncoding] |
| 90 | }]; |
| 91 | } |
Steve Anton | 8cb344a | 2018-02-27 15:34:53 -0800 | [diff] [blame] | 92 | } |
| 93 | |
| 94 | - (BOOL)currentDirection:(RTCRtpTransceiverDirection *)currentDirectionOut { |
| 95 | if (_nativeRtpTransceiver->current_direction()) { |
Mirko Bonadei | a81e9c8 | 2020-05-04 16:14:32 +0200 | [diff] [blame] | 96 | *currentDirectionOut = [RTC_OBJC_TYPE(RTCRtpTransceiver) |
Steve Anton | 8cb344a | 2018-02-27 15:34:53 -0800 | [diff] [blame] | 97 | rtpTransceiverDirectionFromNativeDirection:*_nativeRtpTransceiver->current_direction()]; |
| 98 | return YES; |
| 99 | } else { |
| 100 | return NO; |
| 101 | } |
| 102 | } |
| 103 | |
Harald Alvestrand | 6060df5 | 2020-08-11 09:54:02 +0200 | [diff] [blame] | 104 | - (void)stopInternal { |
| 105 | _nativeRtpTransceiver->StopInternal(); |
Steve Anton | 8cb344a | 2018-02-27 15:34:53 -0800 | [diff] [blame] | 106 | } |
| 107 | |
| 108 | - (NSString *)description { |
| 109 | return [NSString |
Mirko Bonadei | a81e9c8 | 2020-05-04 16:14:32 +0200 | [diff] [blame] | 110 | stringWithFormat:@"RTC_OBJC_TYPE(RTCRtpTransceiver) {\n sender: %@\n receiver: %@\n}", |
| 111 | _sender, |
| 112 | _receiver]; |
Steve Anton | 8cb344a | 2018-02-27 15:34:53 -0800 | [diff] [blame] | 113 | } |
| 114 | |
| 115 | - (BOOL)isEqual:(id)object { |
| 116 | if (self == object) { |
| 117 | return YES; |
| 118 | } |
| 119 | if (object == nil) { |
| 120 | return NO; |
| 121 | } |
| 122 | if (![object isMemberOfClass:[self class]]) { |
| 123 | return NO; |
| 124 | } |
Mirko Bonadei | a81e9c8 | 2020-05-04 16:14:32 +0200 | [diff] [blame] | 125 | RTC_OBJC_TYPE(RTCRtpTransceiver) *transceiver = (RTC_OBJC_TYPE(RTCRtpTransceiver) *)object; |
Steve Anton | 8cb344a | 2018-02-27 15:34:53 -0800 | [diff] [blame] | 126 | return _nativeRtpTransceiver == transceiver.nativeRtpTransceiver; |
| 127 | } |
| 128 | |
| 129 | - (NSUInteger)hash { |
| 130 | return (NSUInteger)_nativeRtpTransceiver.get(); |
| 131 | } |
| 132 | |
| 133 | #pragma mark - Private |
| 134 | |
| 135 | - (rtc::scoped_refptr<webrtc::RtpTransceiverInterface>)nativeRtpTransceiver { |
| 136 | return _nativeRtpTransceiver; |
| 137 | } |
| 138 | |
Mirko Bonadei | a81e9c8 | 2020-05-04 16:14:32 +0200 | [diff] [blame] | 139 | - (instancetype)initWithFactory:(RTC_OBJC_TYPE(RTCPeerConnectionFactory) *)factory |
Yura Yaroshevich | 08f14dd | 2018-07-09 11:56:06 +0300 | [diff] [blame] | 140 | nativeRtpTransceiver: |
| 141 | (rtc::scoped_refptr<webrtc::RtpTransceiverInterface>)nativeRtpTransceiver { |
| 142 | NSParameterAssert(factory); |
Steve Anton | 8cb344a | 2018-02-27 15:34:53 -0800 | [diff] [blame] | 143 | NSParameterAssert(nativeRtpTransceiver); |
| 144 | if (self = [super init]) { |
Yura Yaroshevich | 08f14dd | 2018-07-09 11:56:06 +0300 | [diff] [blame] | 145 | _factory = factory; |
Steve Anton | 8cb344a | 2018-02-27 15:34:53 -0800 | [diff] [blame] | 146 | _nativeRtpTransceiver = nativeRtpTransceiver; |
Mirko Bonadei | a81e9c8 | 2020-05-04 16:14:32 +0200 | [diff] [blame] | 147 | _sender = [[RTC_OBJC_TYPE(RTCRtpSender) alloc] initWithFactory:_factory |
| 148 | nativeRtpSender:nativeRtpTransceiver->sender()]; |
| 149 | _receiver = |
| 150 | [[RTC_OBJC_TYPE(RTCRtpReceiver) alloc] initWithFactory:_factory |
| 151 | nativeRtpReceiver:nativeRtpTransceiver->receiver()]; |
| 152 | RTCLogInfo( |
| 153 | @"RTC_OBJC_TYPE(RTCRtpTransceiver)(%p): created transceiver: %@", self, self.description); |
Steve Anton | 8cb344a | 2018-02-27 15:34:53 -0800 | [diff] [blame] | 154 | } |
| 155 | return self; |
| 156 | } |
| 157 | |
| 158 | + (webrtc::RtpTransceiverDirection)nativeRtpTransceiverDirectionFromDirection: |
| 159 | (RTCRtpTransceiverDirection)direction { |
| 160 | switch (direction) { |
| 161 | case RTCRtpTransceiverDirectionSendRecv: |
| 162 | return webrtc::RtpTransceiverDirection::kSendRecv; |
| 163 | case RTCRtpTransceiverDirectionSendOnly: |
| 164 | return webrtc::RtpTransceiverDirection::kSendOnly; |
| 165 | case RTCRtpTransceiverDirectionRecvOnly: |
| 166 | return webrtc::RtpTransceiverDirection::kRecvOnly; |
| 167 | case RTCRtpTransceiverDirectionInactive: |
| 168 | return webrtc::RtpTransceiverDirection::kInactive; |
Markus Handell | 45c104b | 2020-03-11 10:51:13 +0100 | [diff] [blame] | 169 | case RTCRtpTransceiverDirectionStopped: |
| 170 | return webrtc::RtpTransceiverDirection::kStopped; |
Steve Anton | 8cb344a | 2018-02-27 15:34:53 -0800 | [diff] [blame] | 171 | } |
| 172 | } |
| 173 | |
| 174 | + (RTCRtpTransceiverDirection)rtpTransceiverDirectionFromNativeDirection: |
| 175 | (webrtc::RtpTransceiverDirection)nativeDirection { |
| 176 | switch (nativeDirection) { |
| 177 | case webrtc::RtpTransceiverDirection::kSendRecv: |
| 178 | return RTCRtpTransceiverDirectionSendRecv; |
| 179 | case webrtc::RtpTransceiverDirection::kSendOnly: |
| 180 | return RTCRtpTransceiverDirectionSendOnly; |
| 181 | case webrtc::RtpTransceiverDirection::kRecvOnly: |
| 182 | return RTCRtpTransceiverDirectionRecvOnly; |
| 183 | case webrtc::RtpTransceiverDirection::kInactive: |
| 184 | return RTCRtpTransceiverDirectionInactive; |
Markus Handell | 45c104b | 2020-03-11 10:51:13 +0100 | [diff] [blame] | 185 | case webrtc::RtpTransceiverDirection::kStopped: |
| 186 | return RTCRtpTransceiverDirectionStopped; |
Steve Anton | 8cb344a | 2018-02-27 15:34:53 -0800 | [diff] [blame] | 187 | } |
| 188 | } |
| 189 | |
| 190 | @end |