Get RTMP Status updates in realtime (v2.0.0.beta-3) #1584
Answered
by
shogo4405
abhinandan-chakraborty
asked this question in
Q&A
-
Hi, I am testing out v2.0.0.beta-3 on my iPhone 12 mini --> streaming to --> This is my basic setup: public class LivePageModel: ObservableObject {
@Published var stream: RTMPStream?
private let mixer = MediaMixer()
private let connection = RTMPConnection()
public var mthkView = MTHKView(frame: .zero)
private var subscription: Task<(), Error>?
public func initCamera() {
Task { @MainActor in
let session = AVAudioSession.sharedInstance()
do {
try session.setCategory(
.playAndRecord, mode: .default, options: [.defaultToSpeaker, .allowBluetooth])
try session.setActive(true)
guard
let videoDevice = AVCaptureDevice.default(
.builtInDualWideCamera, for: .video, position: .back),
let audioDevice = AVCaptureDevice.default(for: .audio)
else {
throw CameraLoadState.cameraNotFound
}
stream = RTMPStream(connection: connection)
try await mixer.attachAudio(audioDevice)
try await mixer.attachVideo(videoDevice)
await mixer.addOutput(stream!)
await stream!.addOutput(mthkView)
subscription = Task {
for await status in await stream!.status {
print("AAAAAstatus: \(status)") //<-- expecting stream of data
}
}
//success
} catch {
//err
}
}
}
} now when publishing stream: public class LivePageModel: ObservableObject {
//other methods
public func startStream() {
Task {
do {
let result: RTMPResponse = try await connection.connect(
"rtmp://0.0.0.0:1935/stream/\(stream_id)")
print("--> rtmp connect status: \(result)")
let pubResult: RTMPResponse = try await stream.publish("")
print("--> rtmp publish status: \(result)")
} catch RTMPConnection.Error.requestFailed(let response) {
print("--> rtmp connection error: \(response.status)")
} catch RTMPStream.Error.requestFailed(let response) {
print("--> rtmp publish error: \(response.status)")
} catch {
print("--> rtmp unknown error: \(error)")
}
}
}
} This whole setup works fine but there are few different scenarios that I am unable to handle:
This is the log I get even when stream url is invalid and server has closed the connection.
Maybe I'm missing something and there is an API to handle stream status properly ? |
Beta Was this translation helpful? Give feedback.
Answered by
shogo4405
Oct 3, 2024
Replies: 1 comment 4 replies
-
Beta Was this translation helpful? Give feedback.
4 replies
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
In this log, it is normal behavior that '1' does not appear. In this case, the publishing succeeded, but later it was treated as disconnected by the server.
RTMPStream.Error.requestFailed
is returned when a value other thanNetStream.Publish.Start
is received. For example,NetStream.Publish.BadName
, etc.RTMPConnection.Code.ConnectClosed
inconnection.status
.RTMPConnection.$connected
as true/false using Combine.I mistakenly referred to the wrong code. It should be caught in
RTMPConnection.status
, notRTMPStream.status
."