Hi Quinn!
I did some research on that error and I am thinking that the issue is related to how im using/ not using self through out my code.
This is how I create my web socket. I am a little confused what i should be using as the delegate and the delegate queue. The class I am working in right now inherits the URLSessionWebSocketDelegate.
let urlString = "ws://localhost:8000"
if let url = URL(string: urlString) {
var request = URLRequest(url: url)
let session = URLSession(configuration: .default, delegate: self, delegateQueue: OperationQueue.main)
webSocket = session.webSocketTask(with: request)
webSocket!.resume()
}
Here is my websocket send and receive. I am sending a video stream to a server and I want that stream back to replace the webcam stream. I marked the line in the code bellow that causes the exception in the crash report. The only difference between the testing app that works just fine and the extension that doesn't work is the single if deviceSource._isExtension
webSocket?.send(.string(stringData!), completionHandler: { [weak self] error in
guard let weakself = self else {
logger.error("error in setting weak self 1")
return
}
if let error = error {
print("Failed with Error \(error.localizedDescription)")
} else {
weakself.webSocket?.receive(completionHandler: { [weak self] result in
guard let weakself = self else {
logger.error("error in setting weak self 2")
return
}
switch result {
case .failure(let error):
print(error.localizedDescription)
case .success(let message):
switch message {
case .string(let base64Image):
// Processing user input not shown
var timimgInfo = CMSampleTimingInfo()
var formatDescription: CMFormatDescription? = nil
CMVideoFormatDescriptionCreateForImageBuffer(allocator: kCFAllocatorDefault, imageBuffer: pixelBuffer2!, formatDescriptionOut: &formatDescription)
err = CMSampleBufferCreateReadyWithImageBuffer(allocator: kCFAllocatorDefault, imageBuffer: pixelBuffer2!, formatDescription: formatDescription!, sampleTiming: &timimgInfo, sampleBufferOut: &sampleBuffer2)
if err == 0 {
if deviceSource._isExtension { //Extension code
if let sb2 = sampleBuffer2 {
// this is the line that crashes according to the crash report (ExtensionProvider.swift:400)
weakself.stream.send(sampleBuffer2!, discontinuity: [], hostTimeInNanoseconds: UInt64(timimgInfo.presentationTimeStamp.seconds * Double(NSEC_PER_SEC)))
} else {
logger.error("sb2 is null")
}
} else {
deviceSource.extensionDeviceSourceDelegate?.bufferReceived(sampleBuffer2!) // testing app works just fine
}
} else {
logger.error("Error in stream: \(err)")
}
case .data(let data):
let base64Image = String(decoding: data, as: UTF8.self)
default:
print("Unknown type received from WebSocket")
}
}
})
}
})```