I followed some hints from here to make camera flip from front to back while presenting/streaming via WebRTC on iOS. The flip itself works but when video track changes in RTCMediaStream, the RTCPeerConnection calls again peerConnectionShouldNegotiate delegate method. Here is my current implementation of it (it works on initial call for front camera video, but it fails when called for the second time when video flips):
func peerConnectionShouldNegotiate(_ peerConnection: RTCPeerConnection) {
guard let stream = liveStream else {
return
}
print("peerConnectionShouldNegotiate")
peerConnection.offer(for: mediaConstraints) { (desc, err) in
if let error = err {
print("peerConnectionShouldNegotiate error", error)
} else if let desc = desc {
self.con?.setLocalDescription(desc, completionHandler: { (err) in
if let error = err {
print("set local desc error", error)
} else {
let message: [String: Any] = [
"direction": "publish",
"command": "sendOffer",
"streamInfo": [
"applicationName": stream.appName,
"streamName": stream.streamName,
"sessionId": self.sessionId
],
"sdp": [
"type": RTCSessionDescription.string(for: desc.type),
"sdp": desc.sdp
],
"userData": [
"param1": "value1"
]
]
let messageData = try! JSONSerialization.data(withJSONObject: message, options: [])
let messageStr = String(data: messageData, encoding: .utf8)!
self.socket?.write(string: messageStr)
}
})
} else {
print("this is a no no")
}
}
}
The web-socket response from signaling server comes with error and message Stream name is already in use: [my-stream-id]
I don't know what kind of message I should send to Wowza's signaling server to just update video format in existing stream. It appears sendOffer tries to start a new steam. It's hard to find any documentation about this.
Posting my solution here if somebody else gets the same issue, it took me several days of trial and error to come to a solution.
What I really needed was to just switch camera without re-negotiating
RTCPeerConnection. I saw javascript examples where they switched the video track onRTCRtpSenderbut that didn't work on iOS, also the iOS framework didn't hadreplaceVideoTrackmethod which existed in browser's javascript WebRTC library.My solution was to just update camera on the existing
RTCCameraVideoCapturerand thus preserving existingRTCVideoSourceandRTCVideoTrack.My initial call to create video track is like this:
which successfully negotiated peer connection and then on the flip of the camera I did this:
... and the camera flip worked as charm on live video stream without re-negotiating the connection.