Broadcast Live Stream

To broadcast a live stream video, we have provided a convenient broadcaster tool called EkoStreamBroadcaster. You will need to import EkoLiveVideoBroadcastKit.framework into your app.

Authorize the required permissions

EkoStreamBroadcaster requires the following permissions to work properly.

  1. Camera access

  2. Microphone access

Before using EkoStreamBroadcaster, please make sure these permissions are already granted.

Here are the steps to ask for the permissions.

  1. Add NSCameraUsageDescription to your app's info.plist.

  2. Add NSMicrophoneUsageDescription to your app's info.plist.

  3. Call AVCaptureDevice.requestAccess(for: .video, completion: ...)

  4. Call AVAudioSession.sharedInstance().requestRecordPermission(_:)

Create an EkoStreamBroadcaster

You can create broadcaster instance by providing EkoClient.

broadcaster = EkoStreamBroadcaster(client: ekoClient)

Prepare a configuration

Before going live, you will need to setup the broadcaster session by creating EkoStreamBroadcasterConfiguration. This configuration will be applied for the next live session.

// Prepare configs
let config = EkoStreamBroadcasterConfiguration()
config.canvasFitting = .fill
config.bitrate = 3_000_000 // 3mbps
config.frameRate = .fps30

// Setup the next live session with the given configs.
broadcaster.setup(with: config)

Preview the video

The broadcaster object provides "preview view". After finish setting up the broadcaster, you then can take the preview view to attach into your screen.

// Attach the preview view to show on the screen.
myView.addSubview(broadcaster.previewView)

Setup video resolution

You can setup video resolution anytime "before" going live. This setting will affects both the rendering of preview view, and the resolution of live stream session.

// This settings apply the following changes.
// 1. The preview view to render at 720p.
// 2. The live stream session to broadcast at 720p.
broadcaster.videoResolution = CGSize(width: 1280, height: 720)

Start live stream session

To begin broadcasting the live stream, call

broadcaster.startPublish(title: "title", description: "description")

Stop live stream session

To stop broadcasting the live stream, call

broadcaster.stopPublish()

Switch camera position

By default, the broadcaster will use the back camera. However you can switch camera position anytime by calling

// Use the back camera
broadcaster.cameraPosition = .back

// Use the front camera
broadcaster.cameraPosition = .front

Observe a broadcaster state

To observe a broadcaster state, we provide a delegate object that you can set to listen broadcaster's events..

broadcaster.delegate = self

EkoStreamBroadcasterState represent all the possible states.

  • .idle indicates a state of stream in an idle state.

  • .connecting indicates a state of stream that it's connecting to the stream server.

  • .connected indicates a state of stream that it's connected to the stream server.

  • .disconnected indicates a status of stream that it's disconnected.

The code below show an example of printing out the state of broadcaster, when it changes.

extension MyViewController: EkoStreamBroadcasterDelegate {
    
    func ekoStreamBroadcasterDidUpdateState(_ broadcaster: EkoStreamBroadcaster) {
        print(broadcaster.state)
    }
    
}

Last updated