Play live streams registered with Google Cloud Video Stitcher API

This guide demonstrates how to use the IMA DAI SDK for Android to request and play a live stream for an event registered with the Google Cloud Video Stitcher API, and how to insert an ad break during playback.

This guide expands on the basic example from the Get started guide for IMA DAI.

For information on integrating with other platforms or on using the IMA client-side SDKs, see Interactive Media Ads SDKs.

Set up a Google Cloud project

Set up a Google Cloud project and configure service accounts to access the project.

Register a live stream event using your own content live stream or a test live stream. This guide expects an HLS stream.

Enter the following variables for use in the IMA SDK:

Location
The Google Cloud region where your live config was created: LOCATION
Project number
The Google Cloud project number using the Video Stitcher API: PROJECT_NUMBER
OAuth token

A service account's short lived OAuth token with the Video Stitcher user role:

OAUTH_TOKEN

Read more about creating short-lived credentials for service accounts. The OAuth token can be reused across multiple requests as long as it has not expired.

Network code

The Ad Manager network code for requesting ads: NETWORK-CODE

Live config ID
The live config ID you specified when creating your live stream event: LIVE_CONFIG_ID
Custom asset key
The Ad Manager custom asset key generated during the process of registering a live stream event with the Video Stitcher API: ASSET_KEY

Download the basic example

Download and run the IMA Android DAI Basic Example. Click the play button on the video player to start the short film "Tears of Steel", which contains ad breaks every 30 seconds.

Request a live stream

To replace the sample stream with your live stream, you will need to use the sdkFactory.createVideoStitcherLiveStreamRequest() to create an ad session with Google Ad Manager. You can use the Google Ad Manager UI to locate the generated DAI sessions for monitoring and debugging purposes.

In the existing sample, there are conditional statements for requesting a VOD stream or a live stream. To make it work with Google Cloud Video Stitcher API, you need to add a new path to return a StreamRequest created using sdkFactory.createVideoStitcherLiveStreamRequest().

Here's an example:

videoplayerapp/SampleAdsWrapper.java

...
private enum ContentType {
  LIVE_HLS,
  LIVE_DASH,
// Add a Live HLS Google Cloud type.
  LIVE_HLS_GOOGLE_CLOUD,
  VOD_HLS,
  VOD_DASH,
}

// Set CONTENT_TYPE to the associated enum for the
// stream type you would like to test.
private static final ContentType CONTENT_TYPE =
    ContentType.LIVE_HLS_GOOGLE_CLOUD;
...

@Nullable
  private StreamRequest buildStreamRequest() {
    StreamRequest request;
    switch (CONTENT_TYPE) {
      ...
      case LIVE_HLS_GOOGLE_CLOUD:
        // Live HLS stream generated by the Google Cloud Video Stitcher API.
        request = sdkFactory.createVideoStitcherLiveStreamRequest(
          "NETWORK_CODE",
          "ASSET_KEY",
          "LIVE_CONFIG_ID",
          "LOCATION",
          "PROJECT_NUMBER",
          "OAUTH_TOKEN"
        );
        request.setFormat(StreamFormat.HLS);
        return request;
    }
    // Content type not selected.
    return null;
  }
...

Reload the app to request and play your custom live stream.

Insert an ad break

While playing your custom live stream, try inserting an ad break channel event using the Google Cloud Live Stream API. The ad break should be inserted and played immediately.

Clean up

Now that you have successfully hosted a live stream using the Google Cloud Video Stitcher API and requested it using the IMA DAI SDK for Android, it's important to clean up any serving resources.

Follow the Unregister live stream events guide to end your live stream event.