blob: ce182bc8992b15d0632a1842862734bdae00afde [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
13#import "NSString+StdString.h"
14#import "RTCRtpEncodingParameters+Private.h"
15#import "RTCRtpParameters+Private.h"
16#import "RTCRtpReceiver+Private.h"
17#import "RTCRtpSender+Private.h"
18#import "WebRTC/RTCLogging.h"
19
20@implementation RTCRtpTransceiverInit
21
22@synthesize direction = _direction;
23@synthesize streamLabels = _streamLabels;
24@synthesize sendEncodings = _sendEncodings;
25
26- (instancetype)init {
27 if (self = [super init]) {
28 _direction = RTCRtpTransceiverDirectionSendRecv;
29 }
30 return self;
31}
32
33- (webrtc::RtpTransceiverInit)nativeInit {
34 webrtc::RtpTransceiverInit init;
35 init.direction = [RTCRtpTransceiver nativeRtpTransceiverDirectionFromDirection:_direction];
36 for (NSString *streamLabel in _streamLabels) {
37 init.stream_labels.push_back([streamLabel UTF8String]);
38 }
39 for (RTCRtpEncodingParameters *sendEncoding in _sendEncodings) {
40 init.send_encodings.push_back(sendEncoding.nativeParameters);
41 }
42 return init;
43}
44
45@end
46
47@implementation RTCRtpTransceiver {
48 rtc::scoped_refptr<webrtc::RtpTransceiverInterface> _nativeRtpTransceiver;
49}
50
51- (RTCRtpMediaType)mediaType {
52 return [RTCRtpReceiver mediaTypeForNativeMediaType:_nativeRtpTransceiver->media_type()];
53}
54
55- (NSString *)mid {
56 if (_nativeRtpTransceiver->mid()) {
57 return [NSString stringForStdString:*_nativeRtpTransceiver->mid()];
58 } else {
59 return nil;
60 }
61}
62
63@synthesize sender = _sender;
64@synthesize receiver = _receiver;
65
66- (BOOL)isStopped {
67 return _nativeRtpTransceiver->stopped();
68}
69
70- (RTCRtpTransceiverDirection)direction {
71 return [RTCRtpTransceiver
72 rtpTransceiverDirectionFromNativeDirection:_nativeRtpTransceiver->direction()];
73}
74
75- (void)setDirection:(RTCRtpTransceiverDirection)direction {
76 _nativeRtpTransceiver->SetDirection(
77 [RTCRtpTransceiver nativeRtpTransceiverDirectionFromDirection:direction]);
78}
79
80- (BOOL)currentDirection:(RTCRtpTransceiverDirection *)currentDirectionOut {
81 if (_nativeRtpTransceiver->current_direction()) {
82 *currentDirectionOut = [RTCRtpTransceiver
83 rtpTransceiverDirectionFromNativeDirection:*_nativeRtpTransceiver->current_direction()];
84 return YES;
85 } else {
86 return NO;
87 }
88}
89
90- (void)stop {
91 _nativeRtpTransceiver->Stop();
92}
93
94- (NSString *)description {
95 return [NSString
96 stringWithFormat:@"RTCRtpTransceiver {\n sender: %@\n receiver: %@\n}", _sender, _receiver];
97}
98
99- (BOOL)isEqual:(id)object {
100 if (self == object) {
101 return YES;
102 }
103 if (object == nil) {
104 return NO;
105 }
106 if (![object isMemberOfClass:[self class]]) {
107 return NO;
108 }
109 RTCRtpTransceiver *transceiver = (RTCRtpTransceiver *)object;
110 return _nativeRtpTransceiver == transceiver.nativeRtpTransceiver;
111}
112
113- (NSUInteger)hash {
114 return (NSUInteger)_nativeRtpTransceiver.get();
115}
116
117#pragma mark - Private
118
119- (rtc::scoped_refptr<webrtc::RtpTransceiverInterface>)nativeRtpTransceiver {
120 return _nativeRtpTransceiver;
121}
122
123- (instancetype)initWithNativeRtpTransceiver:
124 (rtc::scoped_refptr<webrtc::RtpTransceiverInterface>)nativeRtpTransceiver {
125 NSParameterAssert(nativeRtpTransceiver);
126 if (self = [super init]) {
127 _nativeRtpTransceiver = nativeRtpTransceiver;
128 _sender = [[RTCRtpSender alloc] initWithNativeRtpSender:nativeRtpTransceiver->sender()];
129 _receiver = [[RTCRtpReceiver alloc] initWithNativeRtpReceiver:nativeRtpTransceiver->receiver()];
130 RTCLogInfo(@"RTCRtpTransceiver(%p): created transceiver: %@", self, self.description);
131 }
132 return self;
133}
134
135+ (webrtc::RtpTransceiverDirection)nativeRtpTransceiverDirectionFromDirection:
136 (RTCRtpTransceiverDirection)direction {
137 switch (direction) {
138 case RTCRtpTransceiverDirectionSendRecv:
139 return webrtc::RtpTransceiverDirection::kSendRecv;
140 case RTCRtpTransceiverDirectionSendOnly:
141 return webrtc::RtpTransceiverDirection::kSendOnly;
142 case RTCRtpTransceiverDirectionRecvOnly:
143 return webrtc::RtpTransceiverDirection::kRecvOnly;
144 case RTCRtpTransceiverDirectionInactive:
145 return webrtc::RtpTransceiverDirection::kInactive;
146 }
147}
148
149+ (RTCRtpTransceiverDirection)rtpTransceiverDirectionFromNativeDirection:
150 (webrtc::RtpTransceiverDirection)nativeDirection {
151 switch (nativeDirection) {
152 case webrtc::RtpTransceiverDirection::kSendRecv:
153 return RTCRtpTransceiverDirectionSendRecv;
154 case webrtc::RtpTransceiverDirection::kSendOnly:
155 return RTCRtpTransceiverDirectionSendOnly;
156 case webrtc::RtpTransceiverDirection::kRecvOnly:
157 return RTCRtpTransceiverDirectionRecvOnly;
158 case webrtc::RtpTransceiverDirection::kInactive:
159 return RTCRtpTransceiverDirectionInactive;
160 }
161}
162
163@end