Skip to content

Commit

Permalink
fix issue with pluginMediaStream creation causing black screen due du…
Browse files Browse the repository at this point in the history
…plicate rtcMediaStream.steamId
  • Loading branch information
hthetiot committed Aug 4, 2020
1 parent 6e30d45 commit 402baf4
Show file tree
Hide file tree
Showing 2 changed files with 41 additions and 20 deletions.
41 changes: 24 additions & 17 deletions src/PluginRTCPeerConnection.swift
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,6 @@ class PluginRTCPeerConnection : NSObject, RTCPeerConnectionDelegate {
var onSetDescriptionFailureCallback: ((_ error: Error) -> Void)!
var onGetStatsCallback: ((_ array: NSArray) -> Void)!

var streamIds: [String] = []
var pluginMediaStreams: [String : PluginMediaStream]! = [:]
var trackIdsToSenders: [String : RTCRtpSender] = [:]

Expand Down Expand Up @@ -475,18 +474,15 @@ class PluginRTCPeerConnection : NSObject, RTCPeerConnectionDelegate {
return
}

for streamId: String in streamIds {
let pluginMediaStream = pluginMediaStreams[streamId];
self.eventListenerForRemoveStream(pluginMediaStream!.id)
for (_, pluginMediaStream) in pluginMediaStreams {
self.eventListenerForRemoveStream(pluginMediaStream.id)
}

streamIds = [];
pluginMediaStreams = [:];

self.rtcPeerConnection.close()
}


func RTCDataChannel_sendString(
_ dcId: Int,
data: String,
Expand Down Expand Up @@ -579,19 +575,26 @@ class PluginRTCPeerConnection : NSObject, RTCPeerConnectionDelegate {
return nil;
}

//if (pluginMediaStreams[stream!.streamId] == nil) {
let pluginMediaStream = PluginMediaStream(rtcMediaStream: stream!)
var currentPluginMediaStream : PluginMediaStream? = nil;

pluginMediaStream.run()
for (_, pluginMediaStream) in pluginMediaStreams {
if (pluginMediaStream.rtcMediaStream == stream) {
currentPluginMediaStream = pluginMediaStream;
break;
}
}

// Let the plugin store it in its dictionary.
streamIds.append(stream!.streamId)
pluginMediaStreams[stream!.streamId] = pluginMediaStream;
if (currentPluginMediaStream == nil) {

self.eventListenerForAddStream(pluginMediaStream)
//}
currentPluginMediaStream = PluginMediaStream(rtcMediaStream: stream!)

return pluginMediaStreams[stream!.streamId]!;
currentPluginMediaStream!.run()

// Let the plugin store it in its dictionary.
pluginMediaStreams[currentPluginMediaStream!.id] = currentPluginMediaStream;
}

return currentPluginMediaStream;
}

/** Called when media is received on a new stream from remote peer. */
Expand All @@ -600,6 +603,8 @@ class PluginRTCPeerConnection : NSObject, RTCPeerConnectionDelegate {

let pluginMediaStream = getPluginMediaStream(stream: stream);

self.eventListenerForAddStream(pluginMediaStream!)

// Fire the 'addstream' event so the JS will create a new MediaStream.
self.eventListener([
"type": "addstream",
Expand All @@ -612,10 +617,12 @@ class PluginRTCPeerConnection : NSObject, RTCPeerConnectionDelegate {
func peerConnection(_ peerConnection: RTCPeerConnection, didRemove stream: RTCMediaStream) {
NSLog("PluginRTCPeerConnection | onremovestream")

let pluginMediaStream = pluginMediaStreams[stream.streamId];
let pluginMediaStream = getPluginMediaStream(stream: stream);

self.eventListenerForRemoveStream(pluginMediaStream!.id)

// Let the plugin remove it from its dictionary.
self.eventListenerForRemoveStream(pluginMediaStream!.id)
pluginMediaStreams[pluginMediaStream!.id] = nil;

self.eventListener([
"type": "removestream",
Expand Down
20 changes: 17 additions & 3 deletions src/iosrtcPlugin.swift
Original file line number Diff line number Diff line change
Expand Up @@ -113,8 +113,8 @@ class iosrtcPlugin : CDVPlugin {
result?.setKeepCallbackAs(true);
self.emit(command.callbackId, result: result!)
},
eventListenerForAddStream: self.saveMediaStream,
eventListenerForRemoveStream: self.deleteMediaStream
eventListenerForAddStream: self.addMediaStream,
eventListenerForRemoveStream: self.removeMediaStream
)

// Store the pluginRTCPeerConnection into the dictionary.
Expand Down Expand Up @@ -1116,11 +1116,25 @@ class iosrtcPlugin : CDVPlugin {
}
}

fileprivate func addMediaStream(_ pluginMediaStream: PluginMediaStream) {
// Create Handled by MediaStream_init

if self.pluginMediaStreams[pluginMediaStream.id] == nil {
self.saveMediaStream(pluginMediaStream)
} else {
NSLog("iosrtcPlugin#addMediaStream() | WARN: pluginMediaStream with id=%@ already exist", String(pluginMediaStream.id))
}
}

fileprivate func removeMediaStream(_ id: String) {
// GC Handled by MediaStream_release
}

fileprivate func saveMediaStream(_ pluginMediaStream: PluginMediaStream) {
if self.pluginMediaStreams[pluginMediaStream.id] == nil {
self.pluginMediaStreams[pluginMediaStream.id] = pluginMediaStream
} else {
NSLog("- PluginMediaStreams already exist [id:%@]", String(pluginMediaStream.id))
NSLog("iosrtcPlugin#saveMediaStream() | WARN: PluginMediaStream already exist [id:%@]", String(pluginMediaStream.id))
return;
}

Expand Down

0 comments on commit 402baf4

Please sign in to comment.