Skip to content

Commit

Permalink
pc: handle PC initialization failures
Browse files Browse the repository at this point in the history
It should usually only trigger for bugs in the library, but things will
break in very weird ways otherwise.

Ref: #1453
  • Loading branch information
saghul committed Oct 6, 2023
1 parent dbbdbb4 commit 3053aea
Show file tree
Hide file tree
Showing 3 changed files with 18 additions and 4 deletions.
8 changes: 6 additions & 2 deletions android/src/main/java/com/oney/WebRTCModule/WebRTCModule.java
Original file line number Diff line number Diff line change
Expand Up @@ -367,16 +367,20 @@ private PeerConnection.RTCConfiguration parseRTCConfiguration(ReadableMap map) {
}

@ReactMethod(isBlockingSynchronousMethod = true)
public void peerConnectionInit(ReadableMap configuration, int id) {
public boolean peerConnectionInit(ReadableMap configuration, int id) {
PeerConnection.RTCConfiguration rtcConfiguration = parseRTCConfiguration(configuration);

try {
ThreadUtils
return (boolean) ThreadUtils
.submitToExecutor(() -> {
PeerConnectionObserver observer = new PeerConnectionObserver(this, id);
PeerConnection peerConnection = mFactory.createPeerConnection(rtcConfiguration, observer);
if (peerConnection == null) {
return false;
}
observer.setPeerConnection(peerConnection);
mPeerConnectionObservers.put(id, observer);
return true;
})
.get();
} catch (ExecutionException | InterruptedException e) {
Expand Down
9 changes: 8 additions & 1 deletion ios/RCTWebRTC/WebRTCModule+RTCPeerConnection.m
Original file line number Diff line number Diff line change
Expand Up @@ -76,13 +76,20 @@ @implementation WebRTCModule (RTCPeerConnection)
RCT_EXPORT_BLOCKING_SYNCHRONOUS_METHOD(peerConnectionInit
: (RTCConfiguration *)configuration objectID
: (nonnull NSNumber *)objectID) {
__block BOOL ret = YES;

dispatch_sync(self.workerQueue, ^{
NSDictionary *optionalConstraints = @{@"DtlsSrtpKeyAgreement" : @"true"};
RTCMediaConstraints *constraints =
[[RTCMediaConstraints alloc] initWithMandatoryConstraints:nil optionalConstraints:optionalConstraints];
RTCPeerConnection *peerConnection = [self.peerConnectionFactory peerConnectionWithConfiguration:configuration
constraints:constraints
delegate:self];
if (peerConnection == nil) {
ret = NO;
return;
}

peerConnection.dataChannels = [NSMutableDictionary new];
peerConnection.reactTag = objectID;
peerConnection.remoteStreams = [NSMutableDictionary new];
Expand All @@ -93,7 +100,7 @@ @implementation WebRTCModule (RTCPeerConnection)
self.peerConnections[objectID] = peerConnection;
});

return nil;
return @(ret);
}

RCT_EXPORT_METHOD(peerConnectionSetConfiguration
Expand Down
5 changes: 4 additions & 1 deletion src/RTCPeerConnection.ts
Original file line number Diff line number Diff line change
Expand Up @@ -82,7 +82,10 @@ export default class RTCPeerConnection extends EventTarget<RTCPeerConnectionEven
super();

this._pcId = nextPeerConnectionId++;
WebRTCModule.peerConnectionInit(configuration, this._pcId);

if (!WebRTCModule.peerConnectionInit(configuration, this._pcId)) {
throw new Error('Failed to initialize PeerConnection, check the native logs!');
}

this._transceivers = [];
this._remoteStreams = new Map();
Expand Down

0 comments on commit 3053aea

Please sign in to comment.