Skip to content

Commit

Permalink
Merge pull request #780 from patrick-tresp/master
Browse files Browse the repository at this point in the history
catch NPEs when calling replaceTrack
  • Loading branch information
hthetiot authored Aug 14, 2024
2 parents fba0d64 + 96be63b commit a8a1c78
Showing 1 changed file with 33 additions and 30 deletions.
63 changes: 33 additions & 30 deletions src/iosrtcPlugin.swift
Original file line number Diff line number Diff line change
Expand Up @@ -1444,39 +1444,42 @@ class iosrtcPlugin : CDVPlugin {
}

@objc(RTCPeerConnection_RTCRtpSender_replaceTrack:) func RTCPeerConnection_RTCRtpSender_replaceTrack(_ command: CDVInvokedUrlCommand) {
NSLog("iosrtcPlugin#RTCPeerConnection_RTCRtpSender_replaceTrack()")
NSLog("iosrtcPlugin#RTCPeerConnection_RTCRtpSender_replaceTrack()")

let pcId = command.argument(at: 0) as! Int
let senderId = command.argument(at: 1) as! Int
let trackId : String? = command.argument(at: 2) as? String

let pluginRTCPeerConnection = self.pluginRTCPeerConnections[pcId]
guard let pcId = command.argument(at: 0) as? Int,
let senderId = command.argument(at: 1) as? Int else {
NSLog("iosrtcPlugin#RTCPeerConnection_RTCRtpSender_replaceTrack() | ERROR: Invalid pcId or senderId")
return
}

if pluginRTCPeerConnection == nil {
NSLog("iosrtcPlugin#RTCPeerConnection_RTCRtpSender_replaceTrack() | ERROR: pluginRTCPeerConnection with pcId=%@ does not exist", String(pcId))
return;
}
let trackId: String? = command.argument(at: 2) as? String

let pluginMediaStreamTrack = trackId != nil ? self.pluginMediaStreamTracks[trackId!] : nil
guard let pluginRTCPeerConnection = self.pluginRTCPeerConnections[pcId] else {
NSLog("iosrtcPlugin#RTCPeerConnection_RTCRtpSender_replaceTrack() | ERROR: pluginRTCPeerConnection with pcId=%@ does not exist", String(pcId))
return
}

self.queue.async { [weak pluginRTCPeerConnection, weak pluginMediaStreamTrack] in
let pluginRTCRptSender = pluginRTCPeerConnection!.pluginRTCRtpSenders[senderId]
if pluginRTCRptSender == nil {
NSLog("iosrtcPlugin#RTCPeerConnection_RTCRtpSender_replaceTrack() | ERROR: pluginRTCRptSender with id=%@ does not exist", String(senderId))
self.emit(command.callbackId,
result: CDVPluginResult(
status: CDVCommandStatus_ERROR,
messageAs: "Cannot find native RTCRtpSender with id=\(senderId)"))
return;
}
self.queue.async { [weak pluginRTCPeerConnection] in
guard let pluginRTCRptSender = pluginRTCPeerConnection?.pluginRTCRtpSenders[senderId] else {
NSLog("iosrtcPlugin#RTCPeerConnection_RTCRtpSender_replaceTrack() | ERROR: Unable to find sender")
return
}

pluginRTCRptSender!.replaceTrack(pluginMediaStreamTrack)
self.emit(command.callbackId,
result: CDVPluginResult(
status: CDVCommandStatus_OK,
messageAs: [
"track": pluginMediaStreamTrack?.getJSON()
]))
}
}
if let trackId = trackId, let pluginMediaStreamTrack = self.pluginMediaStreamTracks[trackId] {
pluginRTCRptSender.replaceTrack(pluginMediaStreamTrack)
self.emit(command.callbackId,
result: CDVPluginResult(
status: CDVCommandStatus_OK,
messageAs: [
"track": pluginMediaStreamTrack.getJSON()
]))
} else {
NSLog("iosrtcPlugin#RTCPeerConnection_RTCRtpSender_replaceTrack() | ERROR: Unable to find track")
self.emit(command.callbackId,
result: CDVPluginResult(
status: CDVCommandStatus_ERROR,
messageAs: "Cannot find native RTCRtpSender track"))
}
}
}
}

0 comments on commit a8a1c78

Please sign in to comment.