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

<br />

This guide demonstrates how to play a DAI Pod Serving live or VOD stream using
the IMA DAI SDK for tvOS with a video player. To view or follow a completed
sample integration, download the Pod Serving example app
([Obj C](https://github.com/googleads/googleads-ima-tvos-dai/tree/main/ObjectiveC/podserving) or
[Swift](https://github.com/googleads/googleads-ima-tvos-dai/tree/main/Swift/podserving)).


**Note:** Apple only provides frameworks to support [HTTP Live Streaming (HLS)](https://developer.apple.com/documentation/http_live_streaming), such as AVKit, AVFoundation, and WebKit. These frameworks won't work with DASH stream format.

<br />

> [!IMPORTANT]
> **Key Point:** To use IMA DAI Pod Serving, you must be working with a Pod Serving partner and must have an [Ad Manager 360
> Advanced](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).


**Note:** 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).

<br />

## IMA DAI Pod Serving overview

Implementing Pod Serving using the IMA DAI SDK for tvOS involves two main
components:

- [`StreamRequest`](https://developers.google.com/ad-manager/dynamic-ad-insertion/sdk/tvos/reference/Classes/IMAStreamRequest.md.txt): An object that defines a stream request to
  Google's advertising servers. Must be created using either
  [`IMAPodStreamRequest`](https://developers.google.com/ad-manager/dynamic-ad-insertion/sdk/tvos/reference/Classes/IMAPodStreamRequest.md.txt)
  or [`IMAPodVODStreamRequest`](https://developers.google.com/ad-manager/dynamic-ad-insertion/sdk/tvos/reference/Classes/IMAPodVODStreamRequest.md.txt)
  to enable Pod Serving. Both methods require a **Network Code** and
  `IMAPodStreamRequest` also needs a **Custom Asset Key** . An **API Key** is
  optional.

- [`IMAStreamManager`](https://developers.google.com/ad-manager/dynamic-ad-insertion/sdk/tvos/reference/Classes/IMAStreamManager.md.txt): 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

Before you begin, you need the following:

- Xcode 13 or later.
- Method for installing the IMA SDK:
  - [Swift Package Manager](https://docs.swift.org/swiftpm/documentation/packagemanagerdocs/) (preferred)
  - A download copy of [IMA SDK for tvOS](https://developers.google.com/interactive-media-ads/docs/sdks/tvos/dai/download)
- A tvOS app already set up with the IMA DAI SDK to play video streams with DAI ads. If you don't already have such an app, we recommend using the [tvOS DAI
  BasicExample](https://github.com/googleads/googleads-ima-tvos-dai/releases) as a starting point. The BasicExample has the codebase referenced in this guide. For more information about how to implement the IMA DAI SDK, see the [Quick start guide](https://developers.google.com/ad-manager/dynamic-ad-insertion/sdk/tvos).

## Set up your Pod Serving variables

Make all changes for Pod Serving in `ViewController.swift` in
Swift or `ViewController.m` in Objective-C. The snippets in this guide
default to using the Swift language. The first step is to update the
constant variables.

Add the following ad pod stream request constants:

- `STREAM_URL` (**Only used for livestreams** ): The video stream URL provided by your manifest manipulator or third-party partner using Pod Serving. It should require you to insert the stream ID provided by the IMA DAI SDK, before you make a request. In this case, the stream URL includes a placeholder, `"[[STREAMID]]"`, which you replace with the stream ID before making a request.
- `NETWORK_CODE`: The network code for your Ad Manager 360 account.
- `CUSTOM_ASSET_KEY` (**Only used for livestreams**): The custom asset key that identifies your Pod Serving event in Ad Manager 360. Your manifest manipulator or 3rd party Pod Serving partner can create this.
- `API_KEY` (**Only used for livestreams**): An optional API key that can be required to retrieve a stream ID from the IMA DAI SDK.

Change the example's variable section to match the following:

> [!NOTE]
> **Note:** The provided stream parameters are for a Live pod stream. To test with a Pod Serving VOD stream, you need to provide your own test stream parameters.

```swift
...
class ViewController:
  UIViewController,
  IMAAdsLoaderDelegate,
  IMAStreamManagerDelegate,
  AVPlayerViewControllerDelegate
{
  static let streamUrl = ""
static let networkCode = ""
static let customAssetKey = ""
static let APIKey = ""
  static let backupStreamURLString = ""

  var adsLoader: IMAAdsLoader?
  ...
```

## Create either a live or VOD pod stream request

### Livestream Pod Serving


Modify the `requestStream` method to create a live pod stream request by
instantiating `IMAPodStreamRequest`.

```swift
  ...

  func requestStream() {
    guard let playerViewController = self.playerViewController else return
    guard let adContainerView = self.adContainerView else return
    guard let adsLoader = self.adsLoader else return

    self.videoDisplay = IMAAVPlayerVideoDisplay(avPlayer: playerViewController.player)
    adDisplayContainer = IMAAdDisplayContainer(
      adContainer: adContainerView, viewController: self)

    // Create a podserving stream request.
request = IMAPodStreamRequest(
networkCode: self.networkCode,
customAssetKey: self.customAssetKey,
adDisplayContainer: adDisplayContainer,
videoDisplay: self.videoDisplay,
userContext: nil)

    adsLoader.requestStream(with: request)
  }

  ...
```

### VOD stream Pod Serving


Modify the `requestStream` method to create a VOD pod stream request by
instantiating `IMAPodVODStreamRequest`.

```swift
  ...

  func requestStream() {
    guard let playerViewController = self.playerViewController else return
    guard let adContainerView = self.adContainerView else return
    guard let adsLoader = self.adsLoader else return

    self.videoDisplay = IMAAVPlayerVideoDisplay(avPlayer: playerViewController.player)
    adDisplayContainer = IMAAdDisplayContainer(
      adContainer: adContainerView, viewController: self)

    // Create a podserving stream request.
request = IMAPodVODStreamRequest(
networkCode: self.networkCode,
adDisplayContainer: adDisplayContainer,
videoDisplay: self.videoDisplay,
userContext: nil)

    adsLoader.requestStream(with: request)
  }

  ...
```

## Edit and set the stream URL

### Livestream Pod Serving


Modify the `IMAAdsLoaderDelegate` methods, using `streamManager.streamId` to
get the stream ID. Then insert the stream ID into the `STREAM_URL`, replacing
`"[[STREAMID]]"`. After you make this change, set the new stream URL using
`videoDisplay.loadStream()`.

```swift
  ...

  // MARK: - IMAAdsLoaderDelegate
  func adsLoader(_ loader: IMAAdsLoader!, adsLoadedWith adsLoadedData: IMAAdsLoadedData!) {
    let streamManager = adsLoadedData.streamManager
let streamId = streamManager.streamId
let urlString = streamUrl.replacingOccurrences(of: "\[\[STREAMID\]\]", with: streamId)
let loadedStreamUrl = URL(string:urlString)
self.videoDisplay.loadStream(loadedStreamUrl, subtitles:@\[\])
    self.videoDisplay.play()
    streamManager.delegate = self
    streamManager.initialize(with: nil)
    self.streamManager = streamManager
  }

  ...
```

### VOD stream Pod Serving


Modify the `IMAAdsLoaderDelegate` methods, using `streamManager.streamId` to
get the stream ID. Then, request a stream URL from your video technology
partner (VTP) and call `IMAStreamManager.loadThirdPartyStream()` to have IMA
load the stream URL and any subtitles returned by your TVP.

```swift
  ...

  // MARK: - IMAAdsLoaderDelegate
  func adsLoader(_ loader: IMAAdsLoader!, adsLoadedWith adsLoadedData: IMAAdsLoadedData!) {
    let streamManager = adsLoadedData.streamManager
let streamId = streamManager.streamId
// 'vtpInterface' is a place holder for your own video technology partner
// (VTP) API calls.
let streamUrl = vtpInterface.requestStreamURL(streamID)
    self.streamManager.loadThirdPartyStream(streamUrl, streamSubtitles:@\[\])
    streamManager.delegate = self
    streamManager.initialize(with: nil)
    self.streamManager = streamManager
  }
```

## Clean up IMA DAI assets

When you have successfully finished requesting and displaying ads in a Pod
Serving stream with IMA DAI SDK, we suggest you clean up any resources after the
Pod Serving session is complete. Call `IMAStreamManager.destroy()` to stop
stream playback, stop all ad tracking, and release all of the loaded stream
assets.

To see other examples of the tvOS SDK being used, see
[samples on GitHub](https://github.com/googleads/googleads-ima-tvos-dai).