Get started with the IMA DAI SDK

IMA SDKs make it easy to integrate multimedia ads into your websites and apps. IMA SDKs can request ads from any VAST-compliant ad server and manage ad playback in your apps. With IMA DAI SDKs, apps make a stream request for ad and content video—either VOD or live content. The SDK then returns a combined video stream, so that you don't have to manage switching between ad and content video within your app.

Select the DAI solution you're interested in

Pod serving DAI

This guide demonstrates how to play a DAI Pod Serving stream, using the IMA DAI SDK for tvOS with a simple video player. If you would like to follow along with a completed sample, download the pod serving example.

To use IMA DAI Pod Serving, you must be working with a pod serving partner and must have an Ad Manager 360 Advanced 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.

IMA DAI Pod Serving overview

Implementing pod serving using the IMA DAI SDK for tvOS involves two main components:

  • StreamRequest: An object that defines a stream request to Google's advertising servers. Must be created using IMAPodStreamRequest() to enable pod serving. This method specifies a Network Code, Custom Asset Key, and an optional API key.
  • 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

Before you begin, you need the following:

  • Xcode 13 or later.
  • CocoaPods (preferred), Swift Package Manager, or a downloaded copy of the IMA SDK for tvOS.
  • 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 as a starting point. The BasicExample is the codebase referenced in this guide.

Set up your pod serving variables

All changes needed for pod serving is done in ViewController.swift in Swift or ViewController.m in Objective-C. The snippets in this guide defaults to using the Swift language. The first step is updating the constant variables.

Here are the ad pod stream request constants that this guide adds:

  • STREAM_URL: 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 is replaced with the stream ID before making a request.
  • NETWORK_CODE: The network code for your Ad Manager 360 account.
  • CUSTOM_ASSET_KEY: The custom asset key that identifies your pod serving event in Ad Manager 360. This can be created by your manifest manipulator or third-party pod serving partner.
  • API_KEY: 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:

class ViewController:
  UIViewController,
  IMAAdsLoaderDelegate,
  IMAStreamManagerDelegate,
  AVPlayerViewControllerDelegate
{
  static let streamUrl =
    "https://encodersim.sandbox.google.com/masterPlaylist/9c654d63-5373-4673-8c8d-6d92b66b9d46/master.m3u8?gen-seg-redirect=true&network=51636543&event=google-sample&pids=devrel4628000,devrel896000,devrel3528000,devrel1428000,devrel2628000,devrel1928000&seg-host=dai.google.com&stream_id=[[STREAMID]]"
  static let networkCode = "51636543"
  static let customAssetKey = "google-sample"
  static let APIKey = ""
  static let backupStreamURLString =
    "http://googleimadev-vh.akamaihd.net/i/big_buck_bunny/bbb-,480p,720p,1080p,.mov.csmil/master.m3u8"

  var adsLoader: IMAAdsLoader?

Create a stream request with the IMAPodStreamRequest class

Modify the requestStream method to create a Pod Stream request by instantiating IMAPodStreamRequest.

  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)

    adsLoader.requestStream(with: request)
  }

Edit and set the stream URL

Modify the IMAAdsLoaderDelegate methods, using streamManager.streamId to get the stream ID. This then needs to be inserted into the STREAM_URL, replacing "[[STREAMID]]". After this change has been made, the new stream URL can be set using videoDisplay.loadStream().

  // 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 streamUrl = URL(string:urlString)
    self.videoDisplay.loadStream(streamUrl, subtitles:@[])
    self.videoDisplay.play()
    streamManager.delegate = self
    streamManager.initialize(with: nil)
    self.streamManager = streamManager
  }

That's it! You're now requesting and displaying ads in a pod serving stream with the IMA tvOS DAI SDK. To see other examples of the IMA SDK being used, look to the samples on GitHub.