Steve Anton | 8cb344a | 2018-02-27 15:34:53 -0800 | [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 | |
| 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; |
Seth Hampson | 513449e | 2018-03-06 09:35:56 -0800 | [diff] [blame] | 23 | @synthesize streamIds = _streamIds; |
Steve Anton | 8cb344a | 2018-02-27 15:34:53 -0800 | [diff] [blame] | 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]; |
Seth Hampson | 513449e | 2018-03-06 09:35:56 -0800 | [diff] [blame] | 36 | for (NSString *streamId in _streamIds) { |
| 37 | init.stream_ids.push_back([streamId UTF8String]); |
Steve Anton | 8cb344a | 2018-02-27 15:34:53 -0800 | [diff] [blame] | 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 { |
Yura Yaroshevich | 08f14dd | 2018-07-09 11:56:06 +0300 | [diff] [blame] | 48 | RTCPeerConnectionFactory *_factory; |
Steve Anton | 8cb344a | 2018-02-27 15:34:53 -0800 | [diff] [blame] | 49 | rtc::scoped_refptr<webrtc::RtpTransceiverInterface> _nativeRtpTransceiver; |
| 50 | } |
| 51 | |
| 52 | - (RTCRtpMediaType)mediaType { |
| 53 | return [RTCRtpReceiver mediaTypeForNativeMediaType:_nativeRtpTransceiver->media_type()]; |
| 54 | } |
| 55 | |
| 56 | - (NSString *)mid { |
| 57 | if (_nativeRtpTransceiver->mid()) { |
| 58 | return [NSString stringForStdString:*_nativeRtpTransceiver->mid()]; |
| 59 | } else { |
| 60 | return nil; |
| 61 | } |
| 62 | } |
| 63 | |
| 64 | @synthesize sender = _sender; |
| 65 | @synthesize receiver = _receiver; |
| 66 | |
| 67 | - (BOOL)isStopped { |
| 68 | return _nativeRtpTransceiver->stopped(); |
| 69 | } |
| 70 | |
| 71 | - (RTCRtpTransceiverDirection)direction { |
| 72 | return [RTCRtpTransceiver |
| 73 | rtpTransceiverDirectionFromNativeDirection:_nativeRtpTransceiver->direction()]; |
| 74 | } |
| 75 | |
| 76 | - (void)setDirection:(RTCRtpTransceiverDirection)direction { |
| 77 | _nativeRtpTransceiver->SetDirection( |
| 78 | [RTCRtpTransceiver nativeRtpTransceiverDirectionFromDirection:direction]); |
| 79 | } |
| 80 | |
| 81 | - (BOOL)currentDirection:(RTCRtpTransceiverDirection *)currentDirectionOut { |
| 82 | if (_nativeRtpTransceiver->current_direction()) { |
| 83 | *currentDirectionOut = [RTCRtpTransceiver |
| 84 | rtpTransceiverDirectionFromNativeDirection:*_nativeRtpTransceiver->current_direction()]; |
| 85 | return YES; |
| 86 | } else { |
| 87 | return NO; |
| 88 | } |
| 89 | } |
| 90 | |
| 91 | - (void)stop { |
| 92 | _nativeRtpTransceiver->Stop(); |
| 93 | } |
| 94 | |
| 95 | - (NSString *)description { |
| 96 | return [NSString |
| 97 | stringWithFormat:@"RTCRtpTransceiver {\n sender: %@\n receiver: %@\n}", _sender, _receiver]; |
| 98 | } |
| 99 | |
| 100 | - (BOOL)isEqual:(id)object { |
| 101 | if (self == object) { |
| 102 | return YES; |
| 103 | } |
| 104 | if (object == nil) { |
| 105 | return NO; |
| 106 | } |
| 107 | if (![object isMemberOfClass:[self class]]) { |
| 108 | return NO; |
| 109 | } |
| 110 | RTCRtpTransceiver *transceiver = (RTCRtpTransceiver *)object; |
| 111 | return _nativeRtpTransceiver == transceiver.nativeRtpTransceiver; |
| 112 | } |
| 113 | |
| 114 | - (NSUInteger)hash { |
| 115 | return (NSUInteger)_nativeRtpTransceiver.get(); |
| 116 | } |
| 117 | |
| 118 | #pragma mark - Private |
| 119 | |
| 120 | - (rtc::scoped_refptr<webrtc::RtpTransceiverInterface>)nativeRtpTransceiver { |
| 121 | return _nativeRtpTransceiver; |
| 122 | } |
| 123 | |
Yura Yaroshevich | 08f14dd | 2018-07-09 11:56:06 +0300 | [diff] [blame] | 124 | - (instancetype)initWithFactory:(RTCPeerConnectionFactory *)factory |
| 125 | nativeRtpTransceiver: |
| 126 | (rtc::scoped_refptr<webrtc::RtpTransceiverInterface>)nativeRtpTransceiver { |
| 127 | NSParameterAssert(factory); |
Steve Anton | 8cb344a | 2018-02-27 15:34:53 -0800 | [diff] [blame] | 128 | NSParameterAssert(nativeRtpTransceiver); |
| 129 | if (self = [super init]) { |
Yura Yaroshevich | 08f14dd | 2018-07-09 11:56:06 +0300 | [diff] [blame] | 130 | _factory = factory; |
Steve Anton | 8cb344a | 2018-02-27 15:34:53 -0800 | [diff] [blame] | 131 | _nativeRtpTransceiver = nativeRtpTransceiver; |
Yura Yaroshevich | ef43aaf | 2018-07-09 19:16:32 +0300 | [diff] [blame] | 132 | _sender = [[RTCRtpSender alloc] initWithFactory:_factory |
| 133 | nativeRtpSender:nativeRtpTransceiver->sender()]; |
Steve Anton | 8cb344a | 2018-02-27 15:34:53 -0800 | [diff] [blame] | 134 | _receiver = [[RTCRtpReceiver alloc] initWithNativeRtpReceiver:nativeRtpTransceiver->receiver()]; |
| 135 | RTCLogInfo(@"RTCRtpTransceiver(%p): created transceiver: %@", self, self.description); |
| 136 | } |
| 137 | return self; |
| 138 | } |
| 139 | |
| 140 | + (webrtc::RtpTransceiverDirection)nativeRtpTransceiverDirectionFromDirection: |
| 141 | (RTCRtpTransceiverDirection)direction { |
| 142 | switch (direction) { |
| 143 | case RTCRtpTransceiverDirectionSendRecv: |
| 144 | return webrtc::RtpTransceiverDirection::kSendRecv; |
| 145 | case RTCRtpTransceiverDirectionSendOnly: |
| 146 | return webrtc::RtpTransceiverDirection::kSendOnly; |
| 147 | case RTCRtpTransceiverDirectionRecvOnly: |
| 148 | return webrtc::RtpTransceiverDirection::kRecvOnly; |
| 149 | case RTCRtpTransceiverDirectionInactive: |
| 150 | return webrtc::RtpTransceiverDirection::kInactive; |
| 151 | } |
| 152 | } |
| 153 | |
| 154 | + (RTCRtpTransceiverDirection)rtpTransceiverDirectionFromNativeDirection: |
| 155 | (webrtc::RtpTransceiverDirection)nativeDirection { |
| 156 | switch (nativeDirection) { |
| 157 | case webrtc::RtpTransceiverDirection::kSendRecv: |
| 158 | return RTCRtpTransceiverDirectionSendRecv; |
| 159 | case webrtc::RtpTransceiverDirection::kSendOnly: |
| 160 | return RTCRtpTransceiverDirectionSendOnly; |
| 161 | case webrtc::RtpTransceiverDirection::kRecvOnly: |
| 162 | return RTCRtpTransceiverDirectionRecvOnly; |
| 163 | case webrtc::RtpTransceiverDirection::kInactive: |
| 164 | return RTCRtpTransceiverDirectionInactive; |
| 165 | } |
| 166 | } |
| 167 | |
| 168 | @end |