1
I have a simple nodejs + node-media-server RTMP server running and I am studying and trying and trying to create a live stream app in Swift, however I am having some issues on how to send non http request in Swift, Here's my LiveStream class code:
import Foundation
final class LiveStream: NSObject {
private let streamName: String
private var outputStream: OutputStream!
private var inputStream: InputStream!
private var streamTask: URLSessionStreamTask!
private lazy var netService: NetService = {
let service = NetService(domain: "streaming-course.herokuapp.com", type: "_rtmp._tcp.", name: streamName, port: 1935)
service.delegate = self
return service
}()
init(streamName: String) {
self.streamName = streamName
super.init()
configure()
}
func write(data: Data) {
var bytesWrittenLength = 0
data.withUnsafeBytes { unsafeRawPointer in
guard let uint8Pointer = unsafeRawPointer.bindMemory(to: UInt8.self).baseAddress else { return }
bytesWrittenLength += outputStream.write(uint8Pointer, maxLength: data.count)
}
print("Bytes written:", bytesWrittenLength)
}
func write(pointer: UnsafeRawPointer?) {
var bytesWrittenLength = 0
guard let uint8Pointer = pointer?.bindMemory(to: UInt8.self, capacity: 1024) else { return }
bytesWrittenLength += outputStream.write(uint8Pointer, maxLength: 1024)
print("Bytes written:", bytesWrittenLength)
}
}
private extension LiveStream {
func configure() {
let session = URLSession.shared
streamTask = session.streamTask(with: netService)
streamTask.resume()
streamTask.captureStreams()
}
}
extension LiveStream: NetServiceDelegate {
func netService(_ sender: NetService, didAcceptConnectionWith inputStream: InputStream, outputStream: OutputStream) {
print("Connection accepted")
}
}
extension LiveStream: URLSessionStreamDelegate {
func urlSession(_ session: URLSession, streamTask: URLSessionStreamTask, didBecome inputStream: InputStream, outputStream: OutputStream) {
self.outputStream = outputStream
self.inputStream = inputStream
inputStream.schedule(in: .main, forMode: .default)
outputStream.schedule(in: .main, forMode: .default)
inputStream.open()
outputStream.open()
}
}
The current state of my app is crash right at initialisation with logged error message:
'NSInvalidArgumentException', reason: '-[NSNetService _internalNetService]: unrecognized selector sent to instance 0x2801e6640'
So I am not initialising NetService correctly + plus I have no idea if using URLSessionStreamTask is really the way to go when sending a rmtp request...
Does anyone have any idea Why Am I getting this error? and If going towards the correct direction with this class setup?
I appreciate your attention, Thank you in advance