blob: eb0ad967389746d2d0952d4d117a1edaceb8aefe [file] [log] [blame]
Steve Anton8cb344a2018-02-27 23:34:531/*
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 H1b61c712024-01-04 05:28:4913#import "RTCRtpCodecCapability+Private.h"
Steve Anton8cb344a2018-02-27 23:34:5314#import "RTCRtpEncodingParameters+Private.h"
15#import "RTCRtpParameters+Private.h"
16#import "RTCRtpReceiver+Private.h"
17#import "RTCRtpSender+Private.h"
Anders Carlsson7bca8ca2018-08-30 07:30:2918#import "base/RTCLogging.h"
Artem Titov63ee39d2022-05-13 14:46:4219#import "helpers/NSString+StdString.h"
Steve Anton8cb344a2018-02-27 23:34:5320
Karim H1b61c712024-01-04 05:28:4921#include "api/rtp_parameters.h"
22
Harald Alvestrandfcf5e7b2020-08-17 08:07:2623NSString *const kRTCRtpTransceiverErrorDomain = @"org.webrtc.RTCRtpTranceiver";
24
Mirko Bonadeia81e9c82020-05-04 14:14:3225@implementation RTC_OBJC_TYPE (RTCRtpTransceiverInit)
Steve Anton8cb344a2018-02-27 23:34:5326
27@synthesize direction = _direction;
Seth Hampson513449e2018-03-06 17:35:5628@synthesize streamIds = _streamIds;
Steve Anton8cb344a2018-02-27 23:34:5329@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 Bonadeia81e9c82020-05-04 14:14:3240 init.direction =
41 [RTC_OBJC_TYPE(RTCRtpTransceiver) nativeRtpTransceiverDirectionFromDirection:_direction];
Seth Hampson513449e2018-03-06 17:35:5642 for (NSString *streamId in _streamIds) {
43 init.stream_ids.push_back([streamId UTF8String]);
Steve Anton8cb344a2018-02-27 23:34:5344 }
Mirko Bonadeia81e9c82020-05-04 14:14:3245 for (RTC_OBJC_TYPE(RTCRtpEncodingParameters) * sendEncoding in _sendEncodings) {
Steve Anton8cb344a2018-02-27 23:34:5346 init.send_encodings.push_back(sendEncoding.nativeParameters);
47 }
48 return init;
49}
50
51@end
52
Mirko Bonadeia81e9c82020-05-04 14:14:3253@implementation RTC_OBJC_TYPE (RTCRtpTransceiver) {
54 RTC_OBJC_TYPE(RTCPeerConnectionFactory) * _factory;
Steve Anton8cb344a2018-02-27 23:34:5355 rtc::scoped_refptr<webrtc::RtpTransceiverInterface> _nativeRtpTransceiver;
56}
57
58- (RTCRtpMediaType)mediaType {
Mirko Bonadeia81e9c82020-05-04 14:14:3259 return [RTC_OBJC_TYPE(RTCRtpReceiver)
60 mediaTypeForNativeMediaType:_nativeRtpTransceiver->media_type()];
Steve Anton8cb344a2018-02-27 23:34:5361}
62
63- (NSString *)mid {
64 if (_nativeRtpTransceiver->mid()) {
Artem Titov63ee39d2022-05-13 14:46:4265 return [NSString stringForStdString:*_nativeRtpTransceiver->mid()];
Steve Anton8cb344a2018-02-27 23:34:5366 } 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 Bonadeia81e9c82020-05-04 14:14:3279 return [RTC_OBJC_TYPE(RTCRtpTransceiver)
Steve Anton8cb344a2018-02-27 23:34:5380 rtpTransceiverDirectionFromNativeDirection:_nativeRtpTransceiver->direction()];
81}
82
Harald Alvestrandfcf5e7b2020-08-17 08:07:2683- (void)setDirection:(RTCRtpTransceiverDirection)direction error:(NSError **)error {
84 webrtc::RTCError nativeError = _nativeRtpTransceiver->SetDirectionWithError(
Mirko Bonadeia81e9c82020-05-04 14:14:3285 [RTC_OBJC_TYPE(RTCRtpTransceiver) nativeRtpTransceiverDirectionFromDirection:direction]);
Harald Alvestrandfcf5e7b2020-08-17 08:07:2686
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 Anton8cb344a2018-02-27 23:34:5395}
96
97- (BOOL)currentDirection:(RTCRtpTransceiverDirection *)currentDirectionOut {
98 if (_nativeRtpTransceiver->current_direction()) {
Mirko Bonadeia81e9c82020-05-04 14:14:3299 *currentDirectionOut = [RTC_OBJC_TYPE(RTCRtpTransceiver)
Steve Anton8cb344a2018-02-27 23:34:53100 rtpTransceiverDirectionFromNativeDirection:*_nativeRtpTransceiver->current_direction()];
101 return YES;
102 } else {
103 return NO;
104 }
105}
106
Harald Alvestrand6060df52020-08-11 07:54:02107- (void)stopInternal {
108 _nativeRtpTransceiver->StopInternal();
Steve Anton8cb344a2018-02-27 23:34:53109}
110
Karim H1b61c712024-01-04 05:28:49111- (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 Anton8cb344a2018-02-27 23:34:53119- (NSString *)description {
120 return [NSString
Mirko Bonadeia81e9c82020-05-04 14:14:32121 stringWithFormat:@"RTC_OBJC_TYPE(RTCRtpTransceiver) {\n sender: %@\n receiver: %@\n}",
122 _sender,
123 _receiver];
Steve Anton8cb344a2018-02-27 23:34:53124}
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 Bonadeia81e9c82020-05-04 14:14:32136 RTC_OBJC_TYPE(RTCRtpTransceiver) *transceiver = (RTC_OBJC_TYPE(RTCRtpTransceiver) *)object;
Steve Anton8cb344a2018-02-27 23:34:53137 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 Bonadeia81e9c82020-05-04 14:14:32150- (instancetype)initWithFactory:(RTC_OBJC_TYPE(RTCPeerConnectionFactory) *)factory
Yura Yaroshevich08f14dd2018-07-09 08:56:06151 nativeRtpTransceiver:
152 (rtc::scoped_refptr<webrtc::RtpTransceiverInterface>)nativeRtpTransceiver {
153 NSParameterAssert(factory);
Steve Anton8cb344a2018-02-27 23:34:53154 NSParameterAssert(nativeRtpTransceiver);
155 if (self = [super init]) {
Yura Yaroshevich08f14dd2018-07-09 08:56:06156 _factory = factory;
Steve Anton8cb344a2018-02-27 23:34:53157 _nativeRtpTransceiver = nativeRtpTransceiver;
Mirko Bonadeia81e9c82020-05-04 14:14:32158 _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 Anton8cb344a2018-02-27 23:34:53165 }
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 Handell45c104b2020-03-11 09:51:13180 case RTCRtpTransceiverDirectionStopped:
181 return webrtc::RtpTransceiverDirection::kStopped;
Steve Anton8cb344a2018-02-27 23:34:53182 }
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 Handell45c104b2020-03-11 09:51:13196 case webrtc::RtpTransceiverDirection::kStopped:
197 return RTCRtpTransceiverDirectionStopped;
Steve Anton8cb344a2018-02-27 23:34:53198 }
199}
200
201@end