mirror of
https://github.com/HaishinKit/HaishinKit.swift.git
synced 2026-05-07 20:12:28 +00:00
2.8 KiB
2.8 KiB
SRTHaishinKit
This module supports the SRT protocol.
🔍 Overview
SRTHaishinKit is SRT protocols stack in Swift. It internally uses a library that is built from libsrt and converted into an xcframework.
🎨 Features
- Publish
- H264, HEVC and AAC support.
- Playback
- H264, HEVC and AAC support.
- SRT Mode
- caller
- listener
- rendezvous
📓 Usage
Logging
- Defining a Swift wrapper method for
srt_setloglevel.
await SRTLogger.shared.setLevel(.debug)
Publish
let mixer = MediaMixer()
let connection = SRTConnection()
let stream = SRTStream(connection: connection)
let hkView = MTHKView(frame: view.bounds)
Task {
do {
try await mixer.attachAudio(AVCaptureDevice.default(for: .audio))
} catch {
print(error)
}
do {
try await mixer.attachVideo(AVCaptureDevice.default(.builtInWideAngleCamera, for: .video, position: .back))
} catch {
print(error)
}
await mixer.addOutput(stream)
}
Task { MainActor in
await stream.addOutput(hkView)
// add ViewController#view
view.addSubview(hkView)
}
Task {
stream.attachAudioPlayer(audioPlayer)
do {
try await connection.connect("srt://host:port")
await stream.publish()
} catch {
print(error)
}
}
Playback
let connection = SRTConnection()
let stream = SRTStream(connection: connection)
let hkView = MTHKView(frame: view.bounds)
let audioPlayer = AudioPlayer(AVAudioEngine())
Task { MainActor in
await stream.addOutput(hkView)
// add ViewController#view
view.addSubview(hkView)
}
Task {
// requires attachAudioPlayer
await stream.attachAudioPlayer(audioPlayer)
do {
try await connection.connect("srt://host:port")
await stream.play()
} catch {
print(error)
}
}
Specify socket options.
- On the HaishinKit side, the default settings of libsrt are used.
- Please check the following code for the support status.
- Many SRT options can be defined as query parameters in the connection URL as follows.
try await connection.connect("srt://host:port?key=value")
Session
import SRTHaishinKit
await StreamSessionBuilderFactory.shared.register(SRTSessionFactory())
🔧 Test
ffplay as a SRT service for publish HaishinKit.
$ ffplay -i 'srt://${YOUR_IP_ADDRESS}?mode=listener'
ffmpeg as a SRT service for playback HaishinKit.
$ ffmpeg -stream_loop -1 -re -i input.mp4 -c copy -f mpegts 'srt://0.0.0.0:9998?mode=listener'
📜 License
SRTHaishinKit
- SRTHaishinKit is licensed under the BSD-3-Clause.
libsrt.xcframework
- libsrt.xcframework is licensed under MPLv2.0.
- This is a build of https://github.com/Haivision/srt as an xcframework.