Overview of RTCHaishinKit features
mainRTCHaishinKit is a Swift stack for WHIP and WHEP protocols, built using libdatachannel.
Key capabilities include:
- Publishing (WHIP): Supports H264 and OPUS.
- Playback (WHEP): Supports H264 and OPUS.
repository·main·Indexed 25 days ago
https://github.com/haishinkit/haishinkit.swiftA camera and microphone streaming library for Apple platforms (iOS, macOS, tvOS, visionOS) supporting RTMP, SRT, and WHEP/WHIP protocols. It provides modules for live mixing via MediaMixer, video overlays using ImageScreenObject and VideoTrackScreenObject, and protocol-specific stacks including RTMPHaishinKit, SRTHaishinKit, RTCHaishinKit, and MoQTHaishinKit.
RTCHaishinKit is a Swift stack for WHIP and WHEP protocols, built using libdatachannel.
Key capabilities include:
MoQTHaishinKit is a Swift implementation of the Media over QUIC Transport (MoQT) protocol, based on draft7.
Important Limitations:
HaishinKit provides camera and microphone mixing functionality for live streaming. The library is divided into several modules based on the protocol stack required:
RTMPHaishinKit supports features of the Enhanced RTMP standard developed by Veovera Software Organization.
Important Requirements:
Before installing fastlane, ensure that the latest version of the Xcode command line tools is installed on your system.
xcode-select --installWhile SRTHaishinKit uses default libsrt settings, you can override many SRT options by providing them as query parameters in the connection URL string.
try await connection.connect("srt://host:port?key=value")You can control the logging verbosity of the SRT stack using the SRTLogger singleton. Use setLevel(_:) to set the desired log level (e.g., .debug).
await SRTLogger.shared.setLevel(.debug)The StreamSession API provides a unified interface for implementing clients using RTMP and SRT protocols. It includes internal retry handling.
You must first register the session factories with the StreamSessionBuilderFactory before creating sessions.
streamName.Use the connect method to initiate publishing or playback. The method accepts a completion handler for disconnection events.
To play an RTMP stream, initialize an RTMPConnection and an RTMPStream. You must attach an AudioPlayer (using AVAudioEngine) to the stream to hear audio, and an MTHKView to the stream to see video.
let connection = RTMPConnection()
let stream = RTMPStream(connection: connection)
let audioPlayer = AudioPlayer(AVAudioEngine())
let hkView = MTHKView(frame: view.bounds)
Task { MainActor in
await stream.addOutput(hkView)
}
Task {
// requires attachAudioPlayer
await stream.attachAudioPlayer(audioPlayer)
do {
try await connection.connect("rtmp://localhost/appName/instanceName")
try await stream.play(streamName)
} catch RTMPConnection.Error.requestFailed(let response) {
print(response)
} catch RTMPStream.Error.requestFailed(let response) {
print(response)
} catch {
print(error)
}
}To publish a stream, you need to set up a MediaMixer to capture audio/video, an RTMPConnection to establish the network link, and an RTMPStream to handle the data flow. You can use MTHKView to preview the stream locally.
Supported codecs:
let mixer = MediaMixer()
let connection = RTMPConnection()
let stream = RTMPStream(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 {
do {
try await connection.connect("rtmp://localhost/appName/instanceName")
try await stream.publish(streamName)
} catch RTMPConnection.Error.requestFailed(let response) {
print(response)
} catch RTMPStream.Error.requestFailed(let response) {
print(response)
} catch {
print(error)
}
}To access the camera and microphone on iOS/macOS, you must include the following keys in your Info.plist file with appropriate usage descriptions.
<key>NSCameraUsageDescription</key>
<string>your usage description here</string>
<key>NSMicrophoneUsageDescription</key>
<string>your usage description here</string>RTCHaishinKit is designed to work with the Session API. To use it, you must register a session factory (such as HTTPSessionFactory) using the SessionBuilderFactory.shared.register method.
import RTCHaishinKit
await SessionBuilderFactory.shared.register(HTTPSessionFactory())