xref: /aosp_15_r20/external/webrtc/sdk/objc/api/peerconnection/RTCRtpTransceiver.mm (revision d9f758449e529ab9291ac668be2861e7a55c2422)
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
13#import "RTCRtpEncodingParameters+Private.h"
14#import "RTCRtpParameters+Private.h"
15#import "RTCRtpReceiver+Private.h"
16#import "RTCRtpSender+Private.h"
17#import "base/RTCLogging.h"
18#import "helpers/NSString+StdString.h"
19
20NSString *const kRTCRtpTransceiverErrorDomain = @"org.webrtc.RTCRtpTranceiver";
21
22@implementation RTC_OBJC_TYPE (RTCRtpTransceiverInit)
23
24@synthesize direction = _direction;
25@synthesize streamIds = _streamIds;
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;
37  init.direction =
38      [RTC_OBJC_TYPE(RTCRtpTransceiver) nativeRtpTransceiverDirectionFromDirection:_direction];
39  for (NSString *streamId in _streamIds) {
40    init.stream_ids.push_back([streamId UTF8String]);
41  }
42  for (RTC_OBJC_TYPE(RTCRtpEncodingParameters) * sendEncoding in _sendEncodings) {
43    init.send_encodings.push_back(sendEncoding.nativeParameters);
44  }
45  return init;
46}
47
48@end
49
50@implementation RTC_OBJC_TYPE (RTCRtpTransceiver) {
51  RTC_OBJC_TYPE(RTCPeerConnectionFactory) * _factory;
52  rtc::scoped_refptr<webrtc::RtpTransceiverInterface> _nativeRtpTransceiver;
53}
54
55- (RTCRtpMediaType)mediaType {
56  return [RTC_OBJC_TYPE(RTCRtpReceiver)
57      mediaTypeForNativeMediaType:_nativeRtpTransceiver->media_type()];
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 {
76  return [RTC_OBJC_TYPE(RTCRtpTransceiver)
77      rtpTransceiverDirectionFromNativeDirection:_nativeRtpTransceiver->direction()];
78}
79
80- (void)setDirection:(RTCRtpTransceiverDirection)direction error:(NSError **)error {
81  webrtc::RTCError nativeError = _nativeRtpTransceiver->SetDirectionWithError(
82      [RTC_OBJC_TYPE(RTCRtpTransceiver) nativeRtpTransceiverDirectionFromDirection:direction]);
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  }
92}
93
94- (BOOL)currentDirection:(RTCRtpTransceiverDirection *)currentDirectionOut {
95  if (_nativeRtpTransceiver->current_direction()) {
96    *currentDirectionOut = [RTC_OBJC_TYPE(RTCRtpTransceiver)
97        rtpTransceiverDirectionFromNativeDirection:*_nativeRtpTransceiver->current_direction()];
98    return YES;
99  } else {
100    return NO;
101  }
102}
103
104- (void)stopInternal {
105  _nativeRtpTransceiver->StopInternal();
106}
107
108- (NSString *)description {
109  return [NSString
110      stringWithFormat:@"RTC_OBJC_TYPE(RTCRtpTransceiver) {\n  sender: %@\n  receiver: %@\n}",
111                       _sender,
112                       _receiver];
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  }
125  RTC_OBJC_TYPE(RTCRtpTransceiver) *transceiver = (RTC_OBJC_TYPE(RTCRtpTransceiver) *)object;
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
139- (instancetype)initWithFactory:(RTC_OBJC_TYPE(RTCPeerConnectionFactory) *)factory
140           nativeRtpTransceiver:
141               (rtc::scoped_refptr<webrtc::RtpTransceiverInterface>)nativeRtpTransceiver {
142  NSParameterAssert(factory);
143  NSParameterAssert(nativeRtpTransceiver);
144  if (self = [super init]) {
145    _factory = factory;
146    _nativeRtpTransceiver = nativeRtpTransceiver;
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);
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;
169    case RTCRtpTransceiverDirectionStopped:
170      return webrtc::RtpTransceiverDirection::kStopped;
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;
185    case webrtc::RtpTransceiverDirection::kStopped:
186      return RTCRtpTransceiverDirectionStopped;
187  }
188}
189
190@end
191