iOS PCast™ Express - PhenixRTS Docs

PCast™ Express

The PCast™ Express extends the PCast™ API to provide a single-step configuration based API for:

This API is intended to be used as a supplement to the Room Express although it can be used to stream all on its own.

Initializing

import PhenixSdk

let pcastExpressOptions = PhenixPCastExpressFactory.createPCastExpressOptionsBuilder(unrecoverableErrorCallback: { status, errorDescription in
          // Best option is to try rebuilding the ChannelExpress instance and/or quit your app
    })
     .buildPCastExpressOptions()

let pcastExpress = PhenixPCastExpressFactory.createPCastExpress(pcastExpressOptions)

PhenixPCastExpressOptionsBuilder

Name Type Default Description
withAuthenticationToken (required) NSString The authentication token generated using the Phenix EdgeAuth library.
withPCastInitializationOptions (optional) PhenixPCastInitializeOptions Use custom options when initializing PCast™.
buildPCastExpressOptions none Builds the PhenixPCastExpressOptions

Publishing Local Media

Publish local user media:

import PhenixSdk

let pcastExpress: PhenixPCastExpress = ...  // previously obtained

let userMediaConstraints = PhenixUserMediaOptions()
userMediaConstraints.video.enabled = true
userMediaConstraints.video.capabilityConstraints[PhenixDeviceCapability.facingMode.rawValue] =
    [PhenixDeviceConstraint.initWith(PhenixFacingMode.user)]
userMediaConstraints.audio.enabled = true
userMediaConstraints.audio.capabilityConstraints[PhenixDeviceCapability.audioEchoCancellationMode.rawValue] =
    [PhenixDeviceConstraint.initWith(PhenixAudioEchoCancellationMode.on)]

let publishOptions = PhenixPCastExpressFactory.createPublishOptionsBuilder()
    .withStreamToken("DIGEST:eyJhc...")
    .withMediaConstraints(userMediaConstraints)
    .buildPublishOptions()

pcastExpress.publish(publishOptions) { (status: PhenixRequestStatus, publisher: PhenixExpressPublisher?) in
  if status == .ok {
    // Do something with publisher
  } else {
    // Handle error
  }
}

Publishing Local Media Parameters

Name Type Description
options (required) PhenixPublishOptions Publish options
callback (required) function Callback for error/success handling

PhenixPublishOptionsBuilder

Name Type Default Description
withMediaConstraints (required) PhenixUserMediaOptions getUserMedia options Constraints to get the user media.
withUserMedia (optional) PhenixUserMediaStream alternative to withMediaConstraints - you can pass user media stream returned from getUserMedia.
withMonitor (optional) PhenixMonitorSetupFailedCallback, PhenixMonitorStreamEndedCallback, PhenixMonitorOptions Options for monitoring a publisher for failure.
withTags (optional) NSArray of NSStrings Tags for the stream.
withStreamToken (required) NSString The publish token generated using the Phenix EdgeAuth library.
buildPublishOptions none Builds the PhenixPublishOptions

Publishing Local Media Callback Arguments

Name Type Description
status PhenixRequestStatus The status of the operation
publisher PhenixExpressPublisher Phenix publisher object

PhenixExpressPublisher

Shares most methods with regular PhenixPublisher returned by PhenixPCast, see Publish a Stream.

Name Signature Returns Description
stop () void Stops publisher. Subscribers will receive stream ended.
stop (reason) void Stops publisher with a custom reason. Subscribers will receive PhenixStreamEndedReasonCustom reason.
enableAudio () void Unmutes audio.
disableAudio () void Mutes audio.
enableVideo () void Unmutes video.
disableVideo () void Mutes video (black frames).
setDataQualityChangedCallback (callback) void Listen for Data Quality Feedback
limitBandwidth (bandwidthLimitInBps) PhenixDisposable Temporarily limit published video bitrate, see Limit Bandwidth
getStreamId () NSString Returns stream ID of publisher
hasEnded () bool Indicates whether publisher has ended, e.g. by stop having been invoked

Subscribing to Published Media

Subscribe to streams published with the Phenix platform

import PhenixSdk

let pcastExpress: PhenixPCastExpress = ...  // previously obtained
let renderLayer: CALayer = ...  // previously obtained

let subscribeOptions = PhenixPCastExpressFactory.createSubscribeOptionsBuilder()
    .withStreamId("us-west#us-west1-b.zzzzzzzz.20000000.xxxxxxxx")
    .withStreamToken("DIGEST:eyJhc...")
    .withRenderer(renderLayer)
    .buildSubscribeOptions()

pcastExpress.subscribe(subscribeOptions) { (status: PhenixRequestStatus, subscriber: PhenixExpressSubscriber?, renderer: PhenixRenderer?) in
  if status == .ok {
    // Do something with subscriber

if let renderer = renderer {
      // Returned if `withRenderer...` option was enabled - Do something with renderer
    }
  } else {
    // Handle error
  }
}

Subscribe Parameters

Name Type Description
options (required) PhenixSubscribeOptions Subscribe options
callback (required) function Callback for error/success handling

Subscribe Options

Name Type Default Description
withStreamId (required) NSString The stream ID of the published stream
withStreamToken (required) NSString The subscribe token generated using the Phenix EdgeAuth library.
withRenderer (optional) CALayer Render layer on which to display stream. If none of the withRenderer... methods are called, no renderer will be instantiated.
withRendererOptions (optional) PhenixRendererOptions Options passed to renderer.
withMonitor (optional) PhenixMonitorSetupFailedCallback, PhenixMonitorStreamEndedCallback, PhenixMonitorOptions Options for monitoring a subscriber for failure.
buildSubscribeOptions none Builds the PhenixSubscribeOptions

PhenixExpressSubscriber

Shares most methods with regular PhenixMediaStream returned by PhenixPCast, see Subscribe to a Stream.

Name Signature Returns Description
createRenderer () PhenixRenderer Creates a new renderer.
getAudioTracks () NSArray of PhenixMediaStreamTrack Returns all associated audio tracks of this stream
getVideoTracks () NSArray of PhenixMediaStreamTrack Returns all associated video tracks of this stream
stop () void Stops subscription. This will trigger the stream ended event.
disableAudio () void Mutes audio.
enableVideo () void Unmutes video.
subscribeToNielsenId3Tag (PhenixNielsenId3TagCallback, PhenixNielsenId3ErrorCallback) PhenixDisposable Register a callback to be called when a new Nielsen ID3 tag is received.

Express Get User Media

Get local user media. For now this is merely a wrapper around Get Local User Media.

import PhenixSdk

let pcastExpress: PhenixPCastExpress = ...  // previously obtained

let userMediaOptions = PhenixUserMediaOptions()

pcastExpress.getUserMedia(userMediaOptions) { (status: PhenixRequestStatus, userMedia: PhenixUserMediaStream?) in
  if status == .ok {
    // Do something with user media stream
  } else {
    // Handle error
  }
}

Express Get User Media Parameters

Name Type Description
options (required) PhenixUserMediaOptions User media options
callback (required) function Callback for error/success handling

Express Get User Media Callback Arguments

Name Type Description
status PhenixRequestStatus The status of the operation.
userMedia PhenixUserMediaStream User media stream

Handling authentication failure

Observe SDK AuthenticationStatus:

import PhenixSdk

var disposable: PhenixDisposable = ...  // declare a property

self.disposable = pcastExpress.getObservableAuthenticationStatus().subscribe { [weak self] change in
  guard let self = self else { return }
  let status = PhenixAuthenticationStatus(rawValue: change!.value.intValue)!

if PhenixAuthenticationStatus.unauthenticated == status {
    self.handleUnauthenticatedStatus()
  }
}

Fetch a new fresh Authentication token and set a new fetched token to SDK:

import PhenixSdk

func fetchANewAuthenticationToken(_ closure: @escaping (String) -> Void) {
  // This method calls a client backend to fetch a new authentication token generated using EdgeAuth library
  // and returns a fetched token using closure callback
}

func handleUnauthenticatedStatus() {
  fetchANewAuthenticationToken({ [weak self] token in
    guard let self = self else { return }
    // Set a new token to SDK
    let status = self.pcastExpress.setAuthenticationToken(token)
    if status != PhenixRequestStatus.ok {
      // Handle error when setting a new token
      // This situation can only occur if the token is invalid and cannot be processed by the SDK.
    }
  })
}

Clean up

Subscribers and publishers are kept alive for as long as they are being referenced in your app. PhenixPCastExpress will only shut down once it is no longer being referenced.