blob: ae1cf798646c7e154c3b299fc194344a94e7777f [file] [log] [blame]
Steve Anton8cb344a2018-02-27 15:34:53 -08001/*
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 Anton8cb344a2018-02-27 15:34:53 -080013#import "RTCRtpEncodingParameters+Private.h"
14#import "RTCRtpParameters+Private.h"
15#import "RTCRtpReceiver+Private.h"
16#import "RTCRtpSender+Private.h"
Anders Carlsson7bca8ca2018-08-30 09:30:29 +020017#import "base/RTCLogging.h"
Artem Titov63ee39d2022-05-13 14:46:42 +000018#import "helpers/NSString+StdString.h"
Steve Anton8cb344a2018-02-27 15:34:53 -080019
Harald Alvestrandfcf5e7b2020-08-17 10:07:26 +020020NSString *const kRTCRtpTransceiverErrorDomain = @"org.webrtc.RTCRtpTranceiver";
21
Mirko Bonadeia81e9c82020-05-04 16:14:32 +020022@implementation RTC_OBJC_TYPE (RTCRtpTransceiverInit)
Steve Anton8cb344a2018-02-27 15:34:53 -080023
24@synthesize direction = _direction;
Seth Hampson513449e2018-03-06 09:35:56 -080025@synthesize streamIds = _streamIds;
Steve Anton8cb344a2018-02-27 15:34:53 -080026@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 Bonadeia81e9c82020-05-04 16:14:32 +020037 init.direction =
38 [RTC_OBJC_TYPE(RTCRtpTransceiver) nativeRtpTransceiverDirectionFromDirection:_direction];
Seth Hampson513449e2018-03-06 09:35:56 -080039 for (NSString *streamId in _streamIds) {
40 init.stream_ids.push_back([streamId UTF8String]);
Steve Anton8cb344a2018-02-27 15:34:53 -080041 }
Mirko Bonadeia81e9c82020-05-04 16:14:32 +020042 for (RTC_OBJC_TYPE(RTCRtpEncodingParameters) * sendEncoding in _sendEncodings) {
Steve Anton8cb344a2018-02-27 15:34:53 -080043 init.send_encodings.push_back(sendEncoding.nativeParameters);
44 }
45 return init;
46}
47
48@end
49
Mirko Bonadeia81e9c82020-05-04 16:14:32 +020050@implementation RTC_OBJC_TYPE (RTCRtpTransceiver) {
51 RTC_OBJC_TYPE(RTCPeerConnectionFactory) * _factory;
Steve Anton8cb344a2018-02-27 15:34:53 -080052 rtc::scoped_refptr<webrtc::RtpTransceiverInterface> _nativeRtpTransceiver;
53}
54
55- (RTCRtpMediaType)mediaType {
Mirko Bonadeia81e9c82020-05-04 16:14:32 +020056 return [RTC_OBJC_TYPE(RTCRtpReceiver)
57 mediaTypeForNativeMediaType:_nativeRtpTransceiver->media_type()];
Steve Anton8cb344a2018-02-27 15:34:53 -080058}
59
60- (NSString *)mid {
61 if (_nativeRtpTransceiver->mid()) {
Artem Titov63ee39d2022-05-13 14:46:42 +000062 return [NSString stringForStdString:*_nativeRtpTransceiver->mid()];
Steve Anton8cb344a2018-02-27 15:34:53 -080063 } 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 Bonadeia81e9c82020-05-04 16:14:32 +020076 return [RTC_OBJC_TYPE(RTCRtpTransceiver)
Steve Anton8cb344a2018-02-27 15:34:53 -080077 rtpTransceiverDirectionFromNativeDirection:_nativeRtpTransceiver->direction()];
78}
79
Harald Alvestrandfcf5e7b2020-08-17 10:07:26 +020080- (void)setDirection:(RTCRtpTransceiverDirection)direction error:(NSError **)error {
81 webrtc::RTCError nativeError = _nativeRtpTransceiver->SetDirectionWithError(
Mirko Bonadeia81e9c82020-05-04 16:14:32 +020082 [RTC_OBJC_TYPE(RTCRtpTransceiver) nativeRtpTransceiverDirectionFromDirection:direction]);
Harald Alvestrandfcf5e7b2020-08-17 10:07:26 +020083
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 Anton8cb344a2018-02-27 15:34:53 -080092}
93
94- (BOOL)currentDirection:(RTCRtpTransceiverDirection *)currentDirectionOut {
95 if (_nativeRtpTransceiver->current_direction()) {
Mirko Bonadeia81e9c82020-05-04 16:14:32 +020096 *currentDirectionOut = [RTC_OBJC_TYPE(RTCRtpTransceiver)
Steve Anton8cb344a2018-02-27 15:34:53 -080097 rtpTransceiverDirectionFromNativeDirection:*_nativeRtpTransceiver->current_direction()];
98 return YES;
99 } else {
100 return NO;
101 }
102}
103
Harald Alvestrand6060df52020-08-11 09:54:02 +0200104- (void)stopInternal {
105 _nativeRtpTransceiver->StopInternal();
Steve Anton8cb344a2018-02-27 15:34:53 -0800106}
107
108- (NSString *)description {
109 return [NSString
Mirko Bonadeia81e9c82020-05-04 16:14:32 +0200110 stringWithFormat:@"RTC_OBJC_TYPE(RTCRtpTransceiver) {\n sender: %@\n receiver: %@\n}",
111 _sender,
112 _receiver];
Steve Anton8cb344a2018-02-27 15:34:53 -0800113}
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 Bonadeia81e9c82020-05-04 16:14:32 +0200125 RTC_OBJC_TYPE(RTCRtpTransceiver) *transceiver = (RTC_OBJC_TYPE(RTCRtpTransceiver) *)object;
Steve Anton8cb344a2018-02-27 15:34:53 -0800126 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 Bonadeia81e9c82020-05-04 16:14:32 +0200139- (instancetype)initWithFactory:(RTC_OBJC_TYPE(RTCPeerConnectionFactory) *)factory
Yura Yaroshevich08f14dd2018-07-09 11:56:06 +0300140 nativeRtpTransceiver:
141 (rtc::scoped_refptr<webrtc::RtpTransceiverInterface>)nativeRtpTransceiver {
142 NSParameterAssert(factory);
Steve Anton8cb344a2018-02-27 15:34:53 -0800143 NSParameterAssert(nativeRtpTransceiver);
144 if (self = [super init]) {
Yura Yaroshevich08f14dd2018-07-09 11:56:06 +0300145 _factory = factory;
Steve Anton8cb344a2018-02-27 15:34:53 -0800146 _nativeRtpTransceiver = nativeRtpTransceiver;
Mirko Bonadeia81e9c82020-05-04 16:14:32 +0200147 _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 Anton8cb344a2018-02-27 15:34:53 -0800154 }
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 Handell45c104b2020-03-11 10:51:13 +0100169 case RTCRtpTransceiverDirectionStopped:
170 return webrtc::RtpTransceiverDirection::kStopped;
Steve Anton8cb344a2018-02-27 15:34:53 -0800171 }
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 Handell45c104b2020-03-11 10:51:13 +0100185 case webrtc::RtpTransceiverDirection::kStopped:
186 return RTCRtpTransceiverDirectionStopped;
Steve Anton8cb344a2018-02-27 15:34:53 -0800187 }
188}
189
190@end