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