The main goals of this library are:
- Easy-to-use: Provide a simple and intuitive API for RTMP streaming that abstracts away the complexities of the underlying protocol.
- Efficient: Handle different message types and chunk sizes for efficient streaming.
- Extensible: Allow developers to customize the library and extend its functionality.
- Robust: Handle various network conditions and recover from errors gracefully.
- Connect to RTMP servers and authenticate using various methods
- RTMP
- RTMPS
- Publishing: Publish live streams with audio and video
- Playing: Play live streams and receive audio/video data
- Send and receive metadata and event messages
- Handle different message types and chunk sizes for efficient streaming
- Support for multiple streams and different message header types (Type 0, 1, 2, 3)
- Support AMF0 and AMF3 codec
- Real-time transmission statistics and monitoring
- Built with Swift 6 strict concurrency support
- Modern async/await API with AsyncStream for event handling
HPRTMP is built with modern Swift technologies:
- Swift 6 Strict Concurrency: Full support for Swift 6's strict concurrency checking, ensuring thread-safe operations
- Actor-based Design: Core sessions (
RTMPPublishSession,RTMPPlayerSession) are implemented as actors for safe concurrent access - AsyncStream Events: Event-driven architecture using AsyncStream for real-time data flow (video, audio, metadata, statistics)
- SwiftNIO Network Layer: High-performance networking built on Apple's SwiftNIO framework with TLS/SSL support
- iOS 14.0+ / macOS 11+
- Swift 6.0+
- SwiftNIO - For high-performance networking
- SwiftNIO SSL - For RTMPS (secure RTMP) support
You can install HPRTMP using Swift Package Manager by adding the following line to your Package.swift file:
dependencies: [
.package(url: "https://github.com/huiping192/HPRTMP.git", from: "0.0.3")
]Alternatively, you can clone this repository and use the included HPRTMP.xcodeproj file, or you can copy the source files directly into your project.
let session = RTMPPublishSession()
// Configure the session
let configure = PublishConfigure()
// Start publishing (async operation)
await session.publish(url: "rtmp://your.rtmp.server/app/key", configure: configure)
// Subscribe to status updates
Task {
for await status in session.statusStream {
switch status {
case .publishStart:
print("Publishing started")
case .failed(let error):
print("Failed: \(error)")
default:
break
}
}
}
// Send audio and video headers
await session.publishAudioHeader(data: audioHeaderData)
await session.publishVideoHeader(data: videoHeaderData)
// Publish audio and video data
await session.publishAudio(data: audioData, delta: delta)
await session.publishVideo(data: videoData, delta: delta)
// Stop publishing when done
await session.stop()let session = RTMPPlayerSession()
// Start playing
await session.play(url: "rtmp://your.rtmp.server/app/streamkey")
// Subscribe to video stream
Task {
for await (videoData, timestamp) in session.videoStream {
// Handle video data
print("Received video: \(videoData.count) bytes at \(timestamp)")
}
}
// Subscribe to audio stream
Task {
for await (audioData, timestamp) in session.audioStream {
// Handle audio data
print("Received audio: \(audioData.count) bytes at \(timestamp)")
}
}
// Subscribe to metadata
Task {
for await metadata in session.metaStream {
print("Received metadata: \(metadata)")
}
}
// Subscribe to status updates
Task {
for await status in session.statusStream {
switch status {
case .playStart:
print("Playback started")
case .failed(let error):
print("Failed: \(error)")
case .disconnected:
print("Disconnected")
default:
break
}
}
}
// Subscribe to transmission statistics
Task {
for await stats in session.statisticsStream {
print("Bitrate: \(stats.currentBitrate) bps")
}
}
// Stop playing when done
await session.stop()// Use rtmps:// for secure streaming (default port 443)
await session.publish(url: "rtmps://your.rtmp.server/app/key", configure: configure)
// Or use custom port for publishing
await session.publish(url: "rtmps://your.rtmp.server:8443/app/key", configure: configure)
// Playing also supports RTMPS
await session.play(url: "rtmps://your.rtmp.server/app/streamkey")Contributions are welcome! If you encounter any issues or have suggestions for improvements, please open an issue or submit a pull request.
HPRTMP is available under the MIT license. See the LICENSE file for more information.