shogo4405 / HaishinKit.swift

Camera and Microphone streaming library via RTMP and SRT for iOS, macOS, tvOS and visionOS.
BSD 3-Clause "New" or "Revised" License
2.72k stars 606 forks source link

SRT no video track #1498

Open MishaMgla opened 1 week ago

MishaMgla commented 1 week ago

Describe the bug

I am having trouble to publish stream using SRT. I am using SRS as media server, I can see that stream started, but no video track publishing. Any clues how to resolve it? I believe SRS working properly and I have valid srt url, as I can make it work with OBS.

To Reproduce

Publish SRT stream. Code:

let srtConnection = SRTConnection()
let srtStream = SRTStream(connection: srtConnection)
srtStream?.sessionPreset = AVCaptureSession.Preset.hd1920x1080
        srtStream?.videoSettings = .init(
            videoSize: .init(width: 1080, height: 1920),
            bitRate: currentBitrate * 1000,
            profileLevel: kVTProfileLevel_H264_High_4_1 as String,
            scalingMode: .trim,
            bitRateMode: .constant,
            maxKeyFrameIntervalDuration: 2,
            allowFrameReordering: false,
            isHardwareEncoderEnabled: true
        )
srtStream?.attachCamera(AVCaptureDevice.default(.builtInWideAngleCamera, for: .video, position: .back), track: 0)
srtConnection.addObserver(self, forKeyPath: "connected", options: .new, context: nil)
srtConnection?.open(URL(string: "srt://*********:10080?streamid=#!::r=live/livestream,m=publish"))

override func observeValue(forKeyPath keyPath: String?, of object: Any?, change: [NSKeyValueChangeKey : Any]?, context: UnsafeMutableRawPointer?) {
        if let connection = object as? SRTConnection {
            if keyPath == "connected" {
                if connection.connected {
                    print("SRT connection established")
                    srtStream?.publish()
                } else {
                    print("SRT connection not established")
                }
            }
        }
    }

Expected behavior

Validate that stream is published to SRS server

Version

1.8.0

Smartphone info.

No response

Additional context

Also, I cant make it work with SRT example in the readme, so I used SRTConnection.open() to establish connection and observer for connected state (as if I publish stream before the connection established, app fails with 'Socket broken' error). Previously I published stream with RTMP and it worked.

Screenshots

SRS status

image

Relevant log output

No response

shogo4405 commented 3 days ago

I confirmed that the example app in the main branch + ffmpeg at https://github.com/shogo4405/HaishinKit.swift/tree/main/Examples/macOS is working.

Is it the way you wrote your code, or is it a problem with HaishinKit? First, please try running the sample app and let me know what errors you encounter.

  1. First, start ffplay as follows: ffplay -fflags nobuffer 'srt://0.0.0.0:9998?mode=listener'
  2. Modify Preference.swift as follows and start the example app. Publish from Ingest Text.

    // Temp
    static nonisolated(unsafe) var `default` = Preference()
    
    var uri: String? = "srt://$yourIpAddress:9998"
    var streamName: String? = ""
    }
MishaMgla commented 3 days ago

I ran macOS example, and have no encountered any errors. I didn't take a look at /Examples before, I will rewrite my code using iOS example to define is it the problem with a way I wrote a code or something else.