1/* 2 * Copyright 2016 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 "RTCRtpSender+Private.h" 12 13#import "RTCDtmfSender+Private.h" 14#import "RTCMediaStreamTrack+Private.h" 15#import "RTCRtpParameters+Private.h" 16#import "RTCRtpSender+Native.h" 17#import "base/RTCLogging.h" 18#import "helpers/NSString+StdString.h" 19 20#include "api/media_stream_interface.h" 21 22@implementation RTC_OBJC_TYPE (RTCRtpSender) { 23 RTC_OBJC_TYPE(RTCPeerConnectionFactory) * _factory; 24 rtc::scoped_refptr<webrtc::RtpSenderInterface> _nativeRtpSender; 25} 26 27@synthesize dtmfSender = _dtmfSender; 28 29- (NSString *)senderId { 30 return [NSString stringForStdString:_nativeRtpSender->id()]; 31} 32 33- (RTC_OBJC_TYPE(RTCRtpParameters) *)parameters { 34 return [[RTC_OBJC_TYPE(RTCRtpParameters) alloc] 35 initWithNativeParameters:_nativeRtpSender->GetParameters()]; 36} 37 38- (void)setParameters:(RTC_OBJC_TYPE(RTCRtpParameters) *)parameters { 39 if (!_nativeRtpSender->SetParameters(parameters.nativeParameters).ok()) { 40 RTCLogError(@"RTC_OBJC_TYPE(RTCRtpSender)(%p): Failed to set parameters: %@", self, parameters); 41 } 42} 43 44- (RTC_OBJC_TYPE(RTCMediaStreamTrack) *)track { 45 rtc::scoped_refptr<webrtc::MediaStreamTrackInterface> nativeTrack( 46 _nativeRtpSender->track()); 47 if (nativeTrack) { 48 return [RTC_OBJC_TYPE(RTCMediaStreamTrack) mediaTrackForNativeTrack:nativeTrack 49 factory:_factory]; 50 } 51 return nil; 52} 53 54- (void)setTrack:(RTC_OBJC_TYPE(RTCMediaStreamTrack) *)track { 55 if (!_nativeRtpSender->SetTrack(track.nativeTrack)) { 56 RTCLogError(@"RTC_OBJC_TYPE(RTCRtpSender)(%p): Failed to set track %@", self, track); 57 } 58} 59 60- (NSArray<NSString *> *)streamIds { 61 std::vector<std::string> nativeStreamIds = _nativeRtpSender->stream_ids(); 62 NSMutableArray *streamIds = [NSMutableArray arrayWithCapacity:nativeStreamIds.size()]; 63 for (const auto &s : nativeStreamIds) { 64 [streamIds addObject:[NSString stringForStdString:s]]; 65 } 66 return streamIds; 67} 68 69- (void)setStreamIds:(NSArray<NSString *> *)streamIds { 70 std::vector<std::string> nativeStreamIds; 71 for (NSString *streamId in streamIds) { 72 nativeStreamIds.push_back([streamId UTF8String]); 73 } 74 _nativeRtpSender->SetStreams(nativeStreamIds); 75} 76 77- (NSString *)description { 78 return [NSString 79 stringWithFormat:@"RTC_OBJC_TYPE(RTCRtpSender) {\n senderId: %@\n}", self.senderId]; 80} 81 82- (BOOL)isEqual:(id)object { 83 if (self == object) { 84 return YES; 85 } 86 if (object == nil) { 87 return NO; 88 } 89 if (![object isMemberOfClass:[self class]]) { 90 return NO; 91 } 92 RTC_OBJC_TYPE(RTCRtpSender) *sender = (RTC_OBJC_TYPE(RTCRtpSender) *)object; 93 return _nativeRtpSender == sender.nativeRtpSender; 94} 95 96- (NSUInteger)hash { 97 return (NSUInteger)_nativeRtpSender.get(); 98} 99 100#pragma mark - Native 101 102- (void)setFrameEncryptor:(rtc::scoped_refptr<webrtc::FrameEncryptorInterface>)frameEncryptor { 103 _nativeRtpSender->SetFrameEncryptor(frameEncryptor); 104} 105 106#pragma mark - Private 107 108- (rtc::scoped_refptr<webrtc::RtpSenderInterface>)nativeRtpSender { 109 return _nativeRtpSender; 110} 111 112- (instancetype)initWithFactory:(RTC_OBJC_TYPE(RTCPeerConnectionFactory) *)factory 113 nativeRtpSender:(rtc::scoped_refptr<webrtc::RtpSenderInterface>)nativeRtpSender { 114 NSParameterAssert(factory); 115 NSParameterAssert(nativeRtpSender); 116 if (self = [super init]) { 117 _factory = factory; 118 _nativeRtpSender = nativeRtpSender; 119 rtc::scoped_refptr<webrtc::DtmfSenderInterface> nativeDtmfSender( 120 _nativeRtpSender->GetDtmfSender()); 121 if (nativeDtmfSender) { 122 _dtmfSender = 123 [[RTC_OBJC_TYPE(RTCDtmfSender) alloc] initWithNativeDtmfSender:nativeDtmfSender]; 124 } 125 RTCLogInfo(@"RTC_OBJC_TYPE(RTCRtpSender)(%p): created sender: %@", self, self.description); 126 } 127 return self; 128} 129 130@end 131