Select platform: [HTML5](https://developers.google.com/ad-manager/dynamic-ad-insertion/sdk/html5/get-started/full-service.md.txt "View this page for the HTML5 platform docs.") [Android](https://developers.google.com/ad-manager/dynamic-ad-insertion/sdk/android/get-started/full-service.md.txt "View this page for the Android platform docs.") [iOS](https://developers.google.com/ad-manager/dynamic-ad-insertion/sdk/ios/get-started/full-service.md.txt "View this page for the iOS platform docs.") [tvOS](https://developers.google.com/ad-manager/dynamic-ad-insertion/sdk/tvos/get-started/full-service.md.txt "View this page for the tvOS platform docs.") [Cast](https://developers.google.com/ad-manager/dynamic-ad-insertion/sdk/cast/get-started/full-service.md.txt "View this page for the Cast platform docs.") [Roku](https://developers.google.com/ad-manager/dynamic-ad-insertion/sdk/roku/get-started/full-service.md.txt "View this page for the Roku platform docs.")

<br />

This guide demonstrates how to play a DAI stream, using the IMA CAF DAI SDK. If
you would like to view or follow along with a completed sample integration,
[download the example](https://developers.google.com/ad-manager/dynamic-ad-insertion/sdk/cast/download.md.txt).


**Note:** The IMA DAI SDK for CAF supports both HLS and DASH streams, with no implementation differences.

<br />

Before using this guide, be sure to familiarize yourself with the [Chromecast
Application Framework's Web Receiver](https://developers.google.com/cast/docs/web_receiver) protocol. This
guide assumes a basic level of familiarity with CAF receiver concepts, such as
[message
interceptors](https://developers.google.com/cast/docs/reference/web_receiver/cast.framework.PlayerManager#setMessageInterceptor)
and
[`mediaInformation`](https://developers.google.com/cast/docs/reference/web_receiver/cast.framework.messages.MediaInformation.md.txt)
objects, as well as familiarity with using the [Cast Command and Control
tool](https://developers.google.com/cast/docs/debugging/cac_tool) to emulate a CAF sender.

To use IMA DAI, you must have an [Ad Manager
360](https://support.google.com/admanager/answer/3485387) account. If you have an Ad
Manager account, contact your account manager for more details. For information
about signing up for Ad Manager, visit the [Ad Manager Help
Center](https://support.google.com/admanager/answer/7084151).

For information on integrating with other platforms, or on using the IMA
client-side SDKs, see [Interactive Media Ads SDKs](https://developers.google.com/interactive-media-ads).

## CAF DAI overview

Implementing DAI using the IMA CAF DAI SDK involves two main components as
demonstrated in this guide:

- [`StreamRequest`](https://developers.google.com/ad-manager/dynamic-ad-insertion/sdk/cast/reference/js/StreamRequest):
  An object that defines a stream request to Google's advertising servers.
  Stream requests come in two main varieties:

  - [`LiveStreamRequest`](https://developers.google.com/ad-manager/dynamic-ad-insertion/sdk/cast/reference/js/LiveStreamRequest): specifies an **Asset Key**, as well as other optional parameters.
  - [`VODStreamRequest`](https://developers.google.com/ad-manager/dynamic-ad-insertion/sdk/cast/reference/js/VODStreamRequest): Specifies a **Content Source ID** , a **Video ID**, as well as other optional parameters.
  - Both request types can optionally include an **API key** needed to access specified streams, and a Google Ad Manager **network code** for the IMA SDK to handle ads identifiers as specified in Google Ad Manager settings.
- [`StreamManager`](https://developers.google.com/ad-manager/dynamic-ad-insertion/sdk/cast/reference/js/StreamManager):
  An object that handles communication between the video stream and the IMA DAI
  SDK, such as firing tracking pings and forwarding stream events to the
  publisher.

## Prerequisites

- A [Cast Developer Console](https://cast.google.com/publish/) account with a registered test device.
- A hosted [web receiver
  app](https://developers.google.com/cast/docs/web_receiver/basic#create_a_basic_web_receiver_app) that is registered with your Cast Developer Console and which can be modified to host the code provided by this guide.
- A sending app that is configured to use your web receiver app. This example uses the [Cast Command and Control tool](https://developers.google.com/cast/docs/debugging/cac_tool.md.txt) as the sender.

## Configure the sender's MediaInfo objects

First, configure your sender app's [MediaInfo
object](https://developers.google.com/cast/docs/reference/web_sender/chrome.cast.media.MediaInfo) to include
the following fields:

|---|---|---|
| `contentId` || A unique identifier for this media item |
| `contentUrl` || The fallback stream URL to load if the DAI StreamRequest fails for any reason |
| `streamType` || For live streams this value should be set to \`LIVE\`. For VOD streams, this value should be set to \`BUFFERED\` |
| `customData` | `assetKey` | **Live streams only.** Identifies the livestream to be loaded |
| `customData` | `contentSourceId` | **VOD streams only.** Identifies the media feed that contains the requested stream. |
| `customData` | `videoId` | **VOD streams only.** Identifies the requested stream within the specified media feed. |
| `customData` | `networkCode` | (Optional) Your Google Ad Manager network code. |
| `customData` | `ApiKey` | An optional API key that can be required to retrieve the stream URL from the IMA DAI SDK |
| `senderCanSkip` | A boolean value to let the receiver know whether the sending device has the ability to display a skip button, enabling support for skippable ads |

To configure these values in the cast command and control tool, click the **Load
Media** tab, and set the custom load request type to `LOAD`. Then replace the
JSON data in the text area with one of the following JSON objects:

**LIVE**

```json
{
  "media": {
    "contentId": "bbb",
    "contentUrl": "https://storage.googleapis.com/interactive-media-ads/media/bbb.m3u8",
    "streamType": "LIVE",
    "customData": {
      "assetKey": "c-rArva4ShKVIAkNfy6HUQ",
      "networkCode": "21775744923",
      "ApiKey": "",
      "senderCanSkip": true
    }
  },
  "credentials": "testCredentials"
}
```

**VOD**

```json
{
  "media": {
    "contentId": "tos",
    "contentUrl": "https://storage.googleapis.com/interactive-media-ads/media/tos.m3u8",
    "streamType": "BUFFERED",
    "customData": {
      "contentSourceId": "2548831",
      "videoId": "tears-of-steel",
      "networkCode": "21775744923",
      "ApiKey": "",
      "senderCanSkip": true
    }
  },
  "credentials": "testCredentials"
}
```

This custom load request object can be sent to the receiver to test the
following steps.

## Create a basic CAF receiver

Following the CAF SDK Basic Receiver Guide, create a basic web receiver.

Your receiver's code should look like this:

```html
<html>
<head>
  <script type="text/javascript"
      src="//www.gstatic.com/cast/sdk/libs/caf_receiver/v3/cast_receiver_framework.js">
  </script>
</head>
<body>
  <cast-media-player></cast-media-player>
  <script>
    cast.framework.CastReceiverContext.getInstance().start();
  </script>
</body>
</html>
```

## Import the IMA DAI SDK and get the Player Manager

Add a script tag to import the IMA DAI SDK for CAF to your web receiver, just
after the script loading CAF. The CAF DAI SDK is evergreen, so there is no need
to set a specific version. Then in the script tag that follows, store the
receiver context and player manager as constants before starting the receiver.

```html
<html>
<head>
  <script type="text/javascript"
      src="//www.gstatic.com/cast/sdk/libs/caf_receiver/v3/cast_receiver_framework.js"></script>
  \<script src="//imasdk.googleapis.com/js/sdkloader/cast_dai.js"\>\</script\>
</head>
<body>
  <cast-media-player></cast-media-player>
  <script>
    const castContext = cast.framework.CastReceiverContext.getInstance();
const playerManager = castContext.getPlayerManager();
castContext.start();
  </script>
</body>
</html>
```

## Initialize the IMA Stream Manager

Initialize the CAF DAI SDK's [Stream
Manager](https://developers.google.com/ad-manager/dynamic-ad-insertion/sdk/cast/reference/js/StreamManager#StreamManager).

```html
<html>
<head>
  <script type="text/javascript"
      src="//www.gstatic.com/cast/sdk/libs/caf_receiver/v3/cast_receiver_framework.js"></script>
  <script src="//imasdk.googleapis.com/js/sdkloader/cast_dai.js"></script>
</head>
<body>
  <cast-media-player></cast-media-player>
  <script>
    const castContext = cast.framework.CastReceiverContext.getInstance();
    const playerManager = castContext.getPlayerManager();
    const streamManager = new google.ima.cast.dai.api.StreamManager();

    castContext.start();
  </script>
</body>
</html>
```

## Create the load message interceptor

The CAF DAI SDK uses the CAF load [message
interceptor](https://developers.google.com/cast/docs/reference/web_receiver/cast.framework.PlayerManager#setMessageInterceptor)
to make stream requests and replace the content URL with the final DAI stream.
The message interceptor calls **streamManager.requestStream()** which handles
setting ad breaks, requesting the stream, and replacing the existing
`contentURL`.

There can only be one load message interceptor, so if your app requires the use
of the interceptor, you need to incorporate any custom functions into the same
callback.

```html
<html>
<head>
  <script type="text/javascript"
      src="//www.gstatic.com/cast/sdk/libs/caf_receiver/v3/cast_receiver_framework.js"></script>
  <script src="//imasdk.googleapis.com/js/sdkloader/cast_dai.js"></script>
</head>
<body>
  <cast-media-player></cast-media-player>
  <script>
    const castContext = cast.framework.CastReceiverContext.getInstance();
    const playerManager = castContext.getPlayerManager();
    const streamManager = new google.ima.cast.dai.api.StreamManager();

    const getStreamRequest = (request) =\> null;
playerManager.setMessageInterceptor(
cast.framework.messages.MessageType.LOAD, (request) =\> {
return streamManager.requestStream(request, getStreamRequest(request))
.then((request) =\> {
this.broadcast('Stream request successful.');
return Promise.resolve(request);
})
.catch((error) =\> {
this.broadcast('Stream request failed.');
return Promise.resolve(request);
});
});

    castContext.start();
  </script>
</body>
</html>
```

## Build the stream request

To complete your CAF DAI integration, you need to build your [stream
request](https://developers.google.com/ad-manager/dynamic-ad-insertion/sdk/cast/reference/js/StreamRequest)
using the data that was included in the `mediaInfo` object from the sender.

```html
<html>
<head>
  <script type="text/javascript"
      src="//www.gstatic.com/cast/sdk/libs/caf_receiver/v3/cast_receiver_framework.js"></script>
  <script src="//imasdk.googleapis.com/js/sdkloader/cast_dai.js"></script>
</head>
<body>
  <cast-media-player></cast-media-player>
  <script>
    const castContext = cast.framework.CastReceiverContext.getInstance();
    const playerManager = castContext.getPlayerManager();
    const streamManager = new google.ima.cast.dai.api.StreamManager();

    const getStreamRequest = (request) =\> {
const imaRequestData = request.media.customData;
let streamRequest = null;
if (imaRequestData.assetKey) {
// Live stream
streamRequest = new google.ima.cast.dai.api.LiveStreamRequest();
streamRequest.assetKey = imaRequestData.assetKey;
} else if (imaRequestData.contentSourceId) {
// VOD stream
streamRequest = new google.ima.cast.dai.api.VODStreamRequest();
streamRequest.contentSourceId = imaRequestData.contentSourceId;
streamRequest.videoId = imaRequestData.videoId;
}
if (streamRequest \&\& imaRequestData.networkCode) {
streamRequest.networkCode = imaRequestData.networkCode;
}
if (streamRequest \&\& imaRequestData.ApiKey) {
streamRequest.ApiKey = imaRequestData.ApiKey;
}
if (streamRequest \&\& imaRequestData.senderCanSkip) {
streamRequest.senderCanSkip = imaRequestData.senderCanSkip;
}
return streamRequest;
};

    playerManager.setMessageInterceptor(
        cast.framework.messages.MessageType.LOAD, (request) => {
          return streamManager.requestStream(request, getStreamRequest(request))
              .then((request) => {
                return Promise.resolve(request);
              })
              .catch((error) => {
                this.broadcast('Stream request failed.');
                return Promise.resolve(request);
              });
        });

    castContext.start();
  </script>
</body>
</html>
```

You're now able to request and playback DAI streams with Google's CAF DAI SDK.
To learn about more advanced SDK features, see the other guides or
[download our sample receiver
applications](https://developers.google.com/ad-manager/dynamic-ad-insertion/sdk/cast/download).