Android PCast™ Express - Deprecated - PhenixRTS Docs

PCast™ Express - Deprecated

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 android.content.Context;

import com.phenixrts.common.RequestStatus;
import com.phenixrts.environment.android.AndroidContext;
import com.phenixrts.express.PCastExpress;
import com.phenixrts.express.PCastExpressFactory;
import com.phenixrts.express.PCastExpressOptions;
import com.phenixrts.pcast.PCastInitializeOptions;

// IMPORTANT: Before accessing any of the static factories, make sure the context is passed to Phenix:
final Context context = ...;  // e.g. Activity.getApplication();
AndroidContext.setContext(context);

final PCastExpressOptions pcastExpressOptions =
    PCastExpressFactory.createPCastExpressOptionsBuilder()
        .withAuthenticationToken("DIGEST:eyJhc...")
        .withUnrecoverableErrorCallback((RequestStatus status, String description) -> {
          // Best to restart app, or attempt to re-create PCastExpress
        })
        .buildPCastExpressOptions();

final PCastExpress pcastExpress = PCastExpressFactory.createPCastExpress(pcastExpressOptions);

PCastExpressOptionsBuilder

Name Type Default Description
withAuthenticationToken (required) String The authentication token generated using the Phenix EdgeAuth library.
withUnrecoverableErrorCallback (optional) PCastExpressOptions.UnrecoverableErrorCallback Function to be called when authentication fails or a failure occurs that is unrecoverable.
withPCastUri (optional) String Allows overriding default PCast™ URI.
withPCastInitializationOptions (optional) PCastInitializeOptions Use custom options when initializing PCast™.
buildPCastExpressOptions none Builds the PCastExpressOptions

Publishing Local Media

Publish local user media:

import android.view.SurfaceView;
import com.phenixrts.common.RequestStatus;
import com.phenixrts.express.ExpressPublisher;
import com.phenixrts.express.PCastExpress;
import com.phenixrts.express.PCastExpressFactory;
import com.phenixrts.express.PublishOptions;
import com.phenixrts.pcast.AudioEchoCancelationMode;
import com.phenixrts.pcast.DeviceCapability;
import com.phenixrts.pcast.DeviceConstraint;
import com.phenixrts.pcast.FacingMode;
import com.phenixrts.pcast.Renderer;
import com.phenixrts.pcast.UserMediaOptions;
import com.phenixrts.pcast.android.AndroidVideoRenderSurface;
import java.util.Arrays;

final PCastExpress pcastExpress = ...;  // previously obtained

final UserMediaOptions userMediaConstraints = new UserMediaOptions();

userMediaConstraints.getVideoOptions().enabled = true;
userMediaConstraints.getVideoOptions().capabilityConstraints.put(
    DeviceCapability.FACING_MODE,
    Arrays.asList(new DeviceConstraint(FacingMode.USER)));

userMediaConstraints.getAudioOptions().enabled = true;
userMediaConstraints.getAudioOptions().capabilityConstraints.put(
    DeviceCapability.AUDIO_ECHO_CANCELATION_MODE,
    Arrays.asList(new DeviceConstraint(AudioEchoCancelationMode.ON)));

final PublishOptions publishOptions = PCastExpressFactory.createPublishOptionsBuilder()
    .withStreamToken("DIGEST:eyJhc...")
    .withMediaConstraints(userMediaConstraints)
    .buildPublishOptions();

pcastExpress.publish(publishOptions, (RequestStatus status, ExpressPublisher publisher) -> {
  if (status == RequestStatus.OK) {
    // Do something with publisher
  } else {
    // Handle error
  }
});

// Create a publisher with an automatically started preview renderer
final SurfaceView view = ...;  // previously obtained
final AndroidVideoRenderSurface renderSurface = new AndroidVideoRenderSurface(view.getHolder());

final PublishOptions publishOptionsWithPreview = PCastExpressFactory.createPublishOptionsBuilder()
    .withStreamToken("DIGEST:eyJhc...")
    .withMediaConstraints(userMediaConstraints)
    .withPreviewRenderer(renderSurface)
    .buildPublishOptions();

pcastExpress.publish(
    publishOptions, (RequestStatus status, ExpressPublisher publisher, Renderer preview) -> {
  if (status == RequestStatus.OK) {
    // Do something with publisher and preview renderer
  } else {
    // Handle error
  }
});

Notes:

Publisher Parameters

Name Type Description
options (required) PublishOptions Publish options
callback (required) PCastExpress.PublishCallback or PCastExpress.PublishWithPreviewCallback Callback for error/success handling

PublishOptionsBuilder

Name Type Default Description
withMediaConstraints (required) UserMediaOptions getUserMedia options Constraints to get the user media.
withUserMedia (optional) UserMediaStream alternative to withMediaConstraints - you can pass user media stream returned from getUserMedia.
withPreviewRenderer (optional) AndroidVideoRenderSurface Render layer on which to display local preview. If none of the withPreview... methods are called, no preview renderer will be instantiated.
withPreviewRenderer (optional) none Will trigger instantiation of preview renderer. Useful for audio only type streams that do not require a render surface.
withPreviewRendererOptions (optional) RendererOptions Options passed to preview renderer. Will trigger instantiation of preview renderer.
withMonitor (optional) MonitorOptions.SetupFailedCallback, MonitorOptions.StreamEndedCallback, MonitorOptions Options for monitoring a publisher for failure.
withTags (optional) Strings[] Tags for the stream.
withStreamToken (required) String The publish token generated using the Phenix EdgeAuth library.
buildPublishOptions none Builds the PublishOptions

ExpressPublisher Callback Arguments

Name Type Description
status RequestStatus The status of the operation
publisher ExpressPublisher Publisher object
previewRenderer Renderer Optionally provided if any of the withPreview... methods were called on the options builder and publish is invoked with withPreview.

ExpressPublisher

Shares most methods with regular Publisher returned by PCast™, 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 StreamEndedReason.CUSTOM 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) Disposable Temporarily limit published video bitrate, see Limit Bitrate
getStreamId () String 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 android.view.SurfaceView;
import com.phenixrts.common.RequestStatus;
import com.phenixrts.express.ExpressSubscriber;
import com.phenixrts.express.PCastExpress;
import com.phenixrts.express.PCastExpressFactory;
import com.phenixrts.express.SubscribeOptions;
import com.phenixrts.pcast.Renderer;
import com.phenixrts.pcast.android.AndroidVideoRenderSurface;

final PCastExpress pcastExpress = ...;  // previously obtained
final SurfaceView view = ...;  // previously obtained

final AndroidVideoRenderSurface renderSurface = new AndroidVideoRenderSurface(view.getHolder());

final SubscribeOptions subscribeOptions = PCastExpressFactory.createSubscribeOptionsBuilder()
    .withStreamId("us-west#us-west1-b.zzzzzzzz.20000000.xxxxxxxx")
    .withStreamToken("DIGEST:eyJhc...")
    .withRenderer(renderSurface)
    .buildSubscribeOptions();

pcastExpress.subscribe(
    subscribeOptions,
    (RequestStatus status, ExpressSubscriber subscriber, Renderer renderer) -> {
  if (status == RequestStatus.OK) {
    // Do something with subscriber

if (renderer != null) {
      // Returned if `withRenderer...` option was enabled - Do something with renderer
    }
  } else {
    // Handle error
  }
});

Notes:

Subscribe Parameters

Name Type Description
options (required) SubscribeOptions Subscribe options
callback (required) PCastExpress.SubscribeCallback Callback for error/success handling

Subscribe Options

Name Type Default Description
withStreamId (required) String The stream ID of the published stream
withStreamToken (required) String The subscribe token generated using the Phenix EdgeAuth library.
withRenderer (optional) AndroidVideoRenderSurface Render layer on which to display stream. If none of the withRenderer... methods are called, no renderer will be instantiated.
withRenderer (optional) none Will trigger instantiation of renderer. Useful for audio only type streams that do not require a render surface.
withRendererOptions (optional) RendererOptions Options passed to renderer. Will trigger instantiation of renderer.
withMonitor (optional) MonitorOptions.SetupFailedCallback, MonitorOptions.StreamEndedCallback, MonitorOptions Options for monitoring a subscriber for failure.
withTags (optional) NSString[] Tags for the stream
buildSubscribeOptions none Builds the SubscribeOptions

ExpressSubscriber

Shares most methods with regular MediaStream returned by PCast™, see Subscribe to a Stream.

Name Signature Returns Description
createRenderer () Renderer Creates a new renderer. This should only be called if none of the withRenderer... builder methods were invoked.
createRenderer (RendererOptions) Renderer Creates a new renderer with RendererOptions. This should only be called if none of the withRenderer... builder methods were invoked.
getAudioTracks () MediaStreamTrack[] Returns all associated audio tracks of this stream
getVideoTracks () MediaStreamTrack[] Returns all associated video tracks of this stream
getTracks () MediaStreamTrack[] Returns all associated tracks of this stream
stop () void Stops subscription. This will trigger the stream ended event.
disableAudio () void Mutes audio.
enableVideo () void Unmutes video.
disableVideo () void Mutes video (black frames).

Express Get User Media

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

import com.phenixrts.common.RequestStatus;
import com.phenixrts.express.PCastExpress;
import com.phenixrts.pcast.UserMediaOptions;
import com.phenixrts.pcast.UserMediaStream;

final PCastExpress pcastExpress = ...;  // previously obtained

final UserMediaOptions userMediaOptions = new UserMediaOptions();

pcastExpress.getUserMedia(userMediaOptions, (RequestStatus status, UserMediaStream userMedia) -> {
  if (status == RequestStatus.OK) {
    // Do something with user media stream
  } else {
    // Handle error
  }
});

Express Get User Media Parameters

Name Type Description
options (required) UserMediaOptions User media options
callback (required) PCastExpress.GetUserMediaCallback Callback for error/success handling

Express Get User Media Callback Arguments

Name Type Description
status RequestStatus The status of the operation.
userMedia UserMediaStream User media stream

Get PCast™

Get the underlying instance of the PCast™. This is preferred to creating another instance as this will introduce more overhead.

import com.phenixrts.express.PCastExpress;
import com.phenixrts.pcast.PCast;

final PCastExpress pcastExpress = ...;  // previously obtained

final PCast pcast = pcastExpress.getPCast();

Clean up

Subscribers and publishers are kept alive for as long as they are being referenced in your app. To force SDK objects to release their resources, you can call close on them. PCastExpress will only shutdown once it is no longer being referenced or close has been called on it.