From 285f83d47bfd9814565911dad3e33801254f8613 Mon Sep 17 00:00:00 2001 From: Jonas Oreland Date: Fri, 7 Feb 2020 10:30:08 +0100 Subject: [PATCH] Add support for injecting VideoBitrateAllocatorFactory also on IOS This patch exposes webrtc::PeerConnectionDependencies c++-object and makes it possible to supply one when creating a PeerConnection. This makes it possible to e.g inject a VideoBitrateAllocatorFactory. Bug: webrtc:10547 Change-Id: Ib7431bdcec1380e7903dc5f66f3583501aeab0a5 Reviewed-on: https://webrtc-review.googlesource.com/c/src/+/168307 Commit-Queue: Jonas Oreland Reviewed-by: Anders Carlsson Cr-Commit-Position: refs/heads/master@{#30480} --- .../RTCPeerConnection+Private.h | 12 ++++++- .../api/peerconnection/RTCPeerConnection.mm | 30 +++++++++++++---- .../RTCPeerConnectionFactory+Native.h | 11 +++++++ .../RTCPeerConnectionFactory.mm | 12 +++++++ sdk/objc/unittests/RTCPeerConnectionTest.mm | 33 +++++++++++++++++++ 5 files changed, 90 insertions(+), 8 deletions(-) diff --git a/sdk/objc/api/peerconnection/RTCPeerConnection+Private.h b/sdk/objc/api/peerconnection/RTCPeerConnection+Private.h index ce08cd5f6a..93b4ec7c3f 100644 --- a/sdk/objc/api/peerconnection/RTCPeerConnection+Private.h +++ b/sdk/objc/api/peerconnection/RTCPeerConnection+Private.h @@ -78,7 +78,17 @@ class PeerConnectionDelegateAdapter : public PeerConnectionObserver { - (instancetype)initWithFactory:(RTCPeerConnectionFactory *)factory configuration:(RTCConfiguration *)configuration constraints:(RTCMediaConstraints *)constraints - delegate:(nullable id)delegate + delegate:(nullable id)delegate; + +/** Initialize an RTCPeerConnection with a configuration, constraints, + * delegate and PeerConnectionDependencies. + */ +- (instancetype)initWithDependencies:(RTCPeerConnectionFactory *)factory + configuration:(RTCConfiguration *)configuration + constraints:(RTCMediaConstraints *)constraints + dependencies: + (std::unique_ptr)dependencies + delegate:(nullable id)delegate NS_DESIGNATED_INITIALIZER; + (webrtc::PeerConnectionInterface::SignalingState)nativeSignalingStateForState: diff --git a/sdk/objc/api/peerconnection/RTCPeerConnection.mm b/sdk/objc/api/peerconnection/RTCPeerConnection.mm index 32a8a4baea..ebdd12033f 100644 --- a/sdk/objc/api/peerconnection/RTCPeerConnection.mm +++ b/sdk/objc/api/peerconnection/RTCPeerConnection.mm @@ -307,6 +307,23 @@ void PeerConnectionDelegateAdapter::OnRemoveTrack( constraints:(RTCMediaConstraints *)constraints delegate:(id)delegate { NSParameterAssert(factory); + std::unique_ptr dependencies = + std::make_unique(nullptr); + return [self initWithDependencies:factory + configuration:configuration + constraints:constraints + dependencies:std::move(dependencies) + delegate:delegate]; +} + +- (instancetype)initWithDependencies:(RTCPeerConnectionFactory *)factory + configuration:(RTCConfiguration *)configuration + constraints:(RTCMediaConstraints *)constraints + dependencies: + (std::unique_ptr)dependencies + delegate:(id)delegate { + NSParameterAssert(factory); + NSParameterAssert(dependencies.get()); std::unique_ptr config( [configuration createNativeConfiguration]); if (!config) { @@ -315,13 +332,12 @@ void PeerConnectionDelegateAdapter::OnRemoveTrack( if (self = [super init]) { _observer.reset(new webrtc::PeerConnectionDelegateAdapter(self)); _nativeConstraints = constraints.nativeConstraints; - CopyConstraintsIntoRtcConfiguration(_nativeConstraints.get(), - config.get()); - _peerConnection = - factory.nativeFactory->CreatePeerConnection(*config, - nullptr, - nullptr, - _observer.get()); + CopyConstraintsIntoRtcConfiguration(_nativeConstraints.get(), config.get()); + + webrtc::PeerConnectionDependencies deps = std::move(*dependencies.release()); + deps.observer = _observer.get(); + _peerConnection = factory.nativeFactory->CreatePeerConnection(*config, std::move(deps)); + if (!_peerConnection) { return nil; } diff --git a/sdk/objc/api/peerconnection/RTCPeerConnectionFactory+Native.h b/sdk/objc/api/peerconnection/RTCPeerConnectionFactory+Native.h index 7a57645c0e..7922c91b4b 100644 --- a/sdk/objc/api/peerconnection/RTCPeerConnectionFactory+Native.h +++ b/sdk/objc/api/peerconnection/RTCPeerConnectionFactory+Native.h @@ -22,6 +22,7 @@ class NetworkControllerFactoryInterface; class VideoEncoderFactory; class VideoDecoderFactory; class AudioProcessing; +struct PeerConnectionDependencies; } // namespace webrtc @@ -87,6 +88,16 @@ NS_ASSUME_NONNULL_BEGIN decoderFactory:(nullable id)decoderFactory mediaTransportFactory: (std::unique_ptr)mediaTransportFactory; + +/** Initialize an RTCPeerConnection with a configuration, constraints, and + * dependencies. + */ +- (RTCPeerConnection *) + peerConnectionWithDependencies:(RTCConfiguration *)configuration + constraints:(RTCMediaConstraints *)constraints + dependencies:(std::unique_ptr)dependencies + delegate:(nullable id)delegate; + @end NS_ASSUME_NONNULL_END diff --git a/sdk/objc/api/peerconnection/RTCPeerConnectionFactory.mm b/sdk/objc/api/peerconnection/RTCPeerConnectionFactory.mm index 4ac33d2436..b2e12d33e2 100644 --- a/sdk/objc/api/peerconnection/RTCPeerConnectionFactory.mm +++ b/sdk/objc/api/peerconnection/RTCPeerConnectionFactory.mm @@ -296,6 +296,18 @@ delegate:delegate]; } +- (RTCPeerConnection *) + peerConnectionWithDependencies:(RTCConfiguration *)configuration + constraints:(RTCMediaConstraints *)constraints + dependencies:(std::unique_ptr)dependencies + delegate:(id)delegate { + return [[RTCPeerConnection alloc] initWithDependencies:self + configuration:configuration + constraints:constraints + dependencies:std::move(dependencies) + delegate:delegate]; +} + - (void)setOptions:(nonnull RTCPeerConnectionFactoryOptions *)options { RTC_DCHECK(options != nil); _nativeFactory->SetOptions(options.nativeOptions); diff --git a/sdk/objc/unittests/RTCPeerConnectionTest.mm b/sdk/objc/unittests/RTCPeerConnectionTest.mm index 3532258799..53fe27b932 100644 --- a/sdk/objc/unittests/RTCPeerConnectionTest.mm +++ b/sdk/objc/unittests/RTCPeerConnectionTest.mm @@ -10,6 +10,7 @@ #import +#include #include #include "rtc_base/gunit.h" @@ -20,11 +21,13 @@ #import "api/peerconnection/RTCIceServer.h" #import "api/peerconnection/RTCMediaConstraints.h" #import "api/peerconnection/RTCPeerConnection.h" +#import "api/peerconnection/RTCPeerConnectionFactory+Native.h" #import "api/peerconnection/RTCPeerConnectionFactory.h" #import "helpers/NSString+StdString.h" @interface RTCPeerConnectionTest : NSObject - (void)testConfigurationGetter; +- (void)testWithDependencies; @end @implementation RTCPeerConnectionTest @@ -104,6 +107,29 @@ newConfig.cryptoOptions.sframeRequireFrameEncryption); } +- (void)testWithDependencies { + NSArray *urlStrings = @[ @"stun:stun1.example.net" ]; + RTCIceServer *server = [[RTCIceServer alloc] initWithURLStrings:urlStrings]; + + RTCConfiguration *config = [[RTCConfiguration alloc] init]; + config.iceServers = @[ server ]; + RTCMediaConstraints *contraints = [[RTCMediaConstraints alloc] initWithMandatoryConstraints:@{} + optionalConstraints:nil]; + RTCPeerConnectionFactory *factory = [[RTCPeerConnectionFactory alloc] init]; + + RTCConfiguration *newConfig; + std::unique_ptr pc_dependencies = + std::make_unique(nullptr); + @autoreleasepool { + RTCPeerConnection *peerConnection = + [factory peerConnectionWithDependencies:config + constraints:contraints + dependencies:std::move(pc_dependencies) + delegate:nil]; + newConfig = peerConnection.configuration; + } +} + @end TEST(RTCPeerConnectionTest, ConfigurationGetterTest) { @@ -112,3 +138,10 @@ TEST(RTCPeerConnectionTest, ConfigurationGetterTest) { [test testConfigurationGetter]; } } + +TEST(RTCPeerConnectionTest, TestWithDependencies) { + @autoreleasepool { + RTCPeerConnectionTest *test = [[RTCPeerConnectionTest alloc] init]; + [test testWithDependencies]; + } +}