Skip to main content
Version: 2.0.x

Initialize Live Stream - iOS

To configure livestreaming with VideoSDK you require two things, first the token which will be used for Authentication purpose and a streamId which will be used to specify where the hosts and audience will join.

Generating Token​

You can generate a token in two ways:

  1. Temporary Token : You can visit Dashboard's API Key section and generate a temporary token from there.

  2. Server : You can setup JWT in backend and make an API call to get the token from your server.

To learn more about Authentication and token in detail you can follow this guide.

// Update server url here.
let LOCAL_SERVER_URL = "http://192.168.0.101:9000"

class APIService {

class func getToken(completion: @escaping (Result<String, Error>) -> Void) {
var url = URL("string: LOCAL_SERVER_URL")!
url = url.appendingPathComponent("get-token")

URLSession.shared.dataTask(with: url) { data, response, error in
if let data = data, let token = data.toJSON()["token"] as? String {
completion(.success(token))
} else if let err = error {
completion(.failure(err))
}
}
.resume()
}
}

Generating Stream Id​

With the token ready, you can now get the streamId from the VideoSDK's rooms API.

// Update server url here.
let LOCAL_SERVER_URL = "http://192.168.0.101:9000"

class APIService {

class func createStreamId(token: String, completion: @escaping (Result<String, Error>) -> Void) {
var url = URL(string: LOCAL_SERVER_URL)!
url = url.appendingPathComponent("create-meeting")

let params = ["token": token]

var request = URLRequest(url: url)
request.httpMethod = "POST"
request.httpBody = try? JSONSerialization.data(withJSONObject: params, options: [])

URLSession.shared.dataTask(with: request) { data, response, error in
if let data = data, let meetingId = data.toJSON()["meetingId"] as? String {
completion(.success(meetingId))
} else if let err = error {
completion(.failure(err))
}
}
.resume()
}
}

Initialization of Live Stream​

  1. To initialize the live stream, first we have to set the token of VideoSDK class. By using config() method, you can set the token property of VideoSDK class.
config
VideoSDK.config("<your token>");
  1. Now, we can initialize the live stream using a factory method provided by the SDK called initMeeting(). By passing the parameters according to the need, it will generate a new Meeting class and the initiated meeting will be returned.

initMeeting() is responsible for initializing the meeting with the provided configuration, which includes the meetingId, name, participantId and many more.

Let's take a deeper look at the available configuration options first.

initMeeting
    let meeting = VideoSDK.initMeeting(
meetingId: "abcd-efgh-hijk",
participantId: "JD", //optional
participantName: "John Doe",
micEnabled: true,
webcamEnabled: true,
customCameraVideoStream: customVideoStream, //optional
customAudioTrack: customAudioStream, //optional
mode: .SEND_AND_RECV, //optional (default mode is conference)
)
  • meetingId :

    • This is a unique identifier that allows participants to join a specific live stream.
    • It will be in the format of xxx-yyy-zzz and will be generated using the VideoSDK's Room API.
  • mode :

    • This paramter defines the mode the participant will enter the live stream with.
    • SEND_AND_RECV: For hosts/co-hosts who can send and receive media.
    • RECV_ONLY: For audience members who can only receive media.​
  • name:

    • This represents the name of the participant in the live streams.
    • It will accept String type value.
  • micEnabled:

    • This is a boolean flag, indicating whether a participant's microphone will be automatically enabled when they join the live stream. This property will be completely ignored for Audience participants, as they are not allowed to publish their audio.
  • webcamEnabled:

    • This is a boolean flag, indicating whether a participant's webcam will be automatically enabled when they join the live stream. This property will be completely ignored for Audience participants, as they are not allowed to publish their video.
  • metaData:

    • If you want to provide additional details about the participants joining the live stream, such as their profile image, you can pass that information in this parameter.
    • It has to be of Object type.
    • This is an OPTIONAL parameter.
  • participantId:

    • This is a unique identifier for the participant's inside the live stream.

      • It can be used to specify the unique identifier which can be linked with your own database service.
      • It has to be of String type.
      • This is an OPTIONAL parameter. By default VideoSDK will generate unique id for each participant.
  • multistream:

    • This is a boolean flag, indicating whether the host's media stream should send multiple resolution layers or a single resolution layer.
caution

You must ensure that the participantId is not repeated in the same livestream. This will enable VideoSDK to eliminate any participant associated with that participantId.

API Reference​

The API references for all the methods and events utilised in this guide are provided below.

Got a Question? Ask us on discord