Live - Straas/Straas-iOS-sdk GitHub Wiki
Overview
- StraaS Live SDK provides an easy way to establish a Real-Time broadcasting to StraaS.
- Live SDK is the successor of the Streaming SDK, based on Apple's Metal GPU API instead of OpenGL compared to the previous version.
- Streaming SDK will stop major updates. Because Apple has deprecated OpenGL already, developers should consider migrate quickly if still using Streaming SDK.
- Live SDK is developed by Swift. Full support for Objective-C, but Swift is more recommended.
Installation
Requires Swift 5.0/Xcode 12.2 or later.
CocoaPods
Add following lines to your Podfile:
pod 'StraaS-iOS-SDK/Live', '~> 1.0.0-beta'
Then run pod install
in the command line.
(You might need to do pod repo update
before pod install
.)
Getting Started
Note: Before using the SDK, you need to configuration your application first.
Permission
StraaS Live SDK needs the permission of the camera and the microphone. You must add the NSCameraUsageDescription and NSMicrophoneUsageDescription key in your app's Info.plist to access the device's camera in iOS 10.0 and later.
Initialize
After successfully configured application, create a STSLiveManager instance directly.
var liveManager = STSLiveManager(withJWT: JWT)
JWT is the broadcaster's member token. Each logged-in StraaS member has a JWT as user identity. (How to get a JWT)
Preparation
After getting a live manager, you can call prepare
method to attach camera, audio and preview.
You can also call attachCamera
, attachAudio
and attachPreview
separately.
These methods can be called any time, even live event is broadcasting.
var previewView: MTHKView!
liveManager.prepare(camera: AVCaptureDevice.listBackDevices().first,
audio: AVCaptureDevice.default(for: .audio),
preview: previewView) { (error) in
print("prepare error = \(error)")
}
previewView is a MTHKView where you want to preview the video from camera.
Configuration
You can call setBroadcastConfig
to set a new configuration for broadcast, or use modifyBroadcastConfig
to change current configuration.
These methods can also be called any time, but some properties will make effective at next startLive. (Ex. videoSize
, check STSLiveBroadcastConfigSetter
to know more.)
let setter = STSLiveBroadcastConfigSetter()
setter.videoBitrate = 2000 * 1000
liveManager.setBroadcastConfig(setter: setter)
liveManager.modifyBroadcastConfig { (setter) in
setter.mirrored = !setter.mirrored
}
STSLiveManagerDelegate
You can use [STSLiveManagerDelegate] to add delegate about error and broadcast statistics update.
liveManager.delegate = self
func liveManager(_ manager: STSLiveManager, onError error: NSError) {
}
Start Live
Before start a live, you should have a ready live event on CMS.
You can use createLive
to create it, or use getLive
to get live event you created.
One-step
This sample will create a live event with given title and start it.
let liveTitle = "Hello StraaS!"
liveManager.startLive(withTitle: liveTitle) { (liveInfo) in
print("Live started.")
} failure: { (error, liveInfo) in
print("Start failure.")
}
Two-step
You can use createLive
to create it, or use getLive
to get STSLiveInfo
you created.
And start live with that liveInfo.
func createLive() {
let liveConfig = STSLiveConfig(withTitle: "Hello StraaS!")
liveManager.createLive(withConfig: liveConfig, succeed: { [weak self] (liveInfo) in
self?.startLive(liveInfo)
}, failure: nil)
}
func getLive() {
let liveID = ""
liveManager.getLive(withLiveID: liveID, succeed: { [weak self] (liveInfo) in
self?.startLive(liveInfo)
}, failure: nil)
}
func startLive(_ liveInfo: STSLiveInfo) {
liveManager.startLive(withLiveInfo: liveInfo, succeed: { (liveInfo) in
print("Live started.")
}, failure: { (error) in
print("Start failure.")
})
}
Stop live
Call stopLive
to stop broadcasting.
liveManager.stopLive()
End live
Call endLive
to end live event. This live event will can't startLive any more.
Video Filter
You can use STSBroadcastFilter
to make some effect at every image.
For example, you can use STSBroadcastBlackWhiteFilter
to make Black-White effect, STSBroadcastBeautyFaceEffect
can smooth and whitening faces.
var filter = STSBroadcastBlackWhiteFilter()
liveManager.filters.append(filter)
You can subclass STSBroadcastFilter
and implement protocol STSBroadcastFilterDelegate
to make customized filter.
Note: Objective-C can't subclass STSBroadcastFilter because it's written in Swift.