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

Play VOD streams registered with Google Cloud Video Stitcher API

This guide demonstrates how to use the IMA DAI SDK for tvOS to request and play a Google Cloud VOD stream session.

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.

Enter the following variables for use in the IMA SDK:

Location
The Google Cloud region where your VOD 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 OAuth tokens. 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

Ad tag URL

Ad Manager URL of the ad tag:

AD_TAG_URL

For testing, you can use the IMA VMAP Pre-roll sample.

Content source URL

The URL string of the stream manifest for your VOD content: CONTENT_SOURCE_URL

Set up the basic example

Go to the IMA tvOS DAI Github release pageand download either the basic Objective-C example or advanced Swift example, depending on your language of choice. This example is a tvOS Xcode project that relies on Cocoapods to load the IMA DAI SDK.

To prepare the sample to run, make sure CocoaPods is installed, then open the basic example's folder in the terminal and run the following command:

pod install --repo-update

Once that command completes, you see a file named BasicExample.xcworkspace or AdvancedExample.xcworkspace in your project folder. Open this file in Xcode and run the sample to ensure that the test video and ads play as expected.

Request a VOD stream

To replace the sample stream with your ad stitched VOD stream, use IMAVideoStitcherVODStreamRequest 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.

In the existing sample, there are examples for requesting a VOD stream or a livestream from Google's DAI servers. To make it work with the Google Cloud Video Stitcher API, you need to replace the current requestStream function with one that uses the IMAVideoStitcherVODStreamRequest class.

Here's an example:

Objective-C

ViewController.m

#import <GoogleInteractiveMediaAds/GoogleInteractiveMediaAds.h>
/// Fallback URL in case something goes wrong in loading the stream. If all goes well,
/// this will not be used.
static NSString *const kTestAppContentUrl_M3U8 =
    @"http://devimages.apple.com/iphone/samples/bipbop/bipbopall.m3u8";
static NSString *const kContentSourceUrl = @"CONTENT_SOURCE_URL";
static NSString *const kAdTagUrl = @"AD_TAG_URL";
static NSString *const kLocation = @"LOCATION";
static NSString *const kProjectNumber = @"PROJECT_NUMBER";
static NSString *const kOAuthToken = @"OAUTH_TOKEN";
static NSString *const kNetworkCode = @"NETWORK_CODE_";
@interface ViewController () <IMAAdsLoaderDelegate, IMAStreamManagerDelegate>
...
- (void)requestStream {
    // Create an ad display container for ad rendering.
    IMAAdDisplayContainer *adDisplayContainer =
        [[IMAAdDisplayContainer alloc] initWithAdContainer:self.videoView
                                            viewController:self
                                            companionSlots:nil];
    // Create an IMAAVPlayerVideoDisplay to give the SDK access to your video player.
    IMAAVPlayerVideoDisplay *imaVideoDisplay =
        [[IMAAVPlayerVideoDisplay alloc] initWithAVPlayer:self.contentPlayer];
    IMAVideoStitcherVODStreamRequest *request =
        [[IMAVideoStitcherVODStreamRequest alloc] initWithAdTagURL:kAdTagUrl
                                                            region:kLocation
                                                     projectNumber:kProjectNumber
                                                        OAuthToken:kOAuthToken
                                                       networkCode:kNetworkCode
                                                  contentSourceURL:kContentSourceUrl
                                                adDisplayContainer:adDisplayContainer
                                                      videoDisplay:imaVideoDisplay
                                                       userContext:nil];
    [self.adsLoader requestStreamWithRequest:request];
}

Swift

VideoPlayerViewController.swift

func requestStream() {
  guard let avPlayer = playerViewController.player else { return }
  if #available(tvOS 14.0, *) {
    self.pipProxy = IMAPictureInPictureProxy(avPlayerViewControllerDelegate: self)
    playerViewController.delegate = self.pipProxy
  }
  self.videoDisplay = IMAAVPlayerVideoDisplay(avPlayer: avPlayer)
  videoDisplay!.playerVideoDisplayDelegate = self
  let request: IMAStreamRequest
  request = IMAVideoStitcherVODStreamRequest(
    adTagURL:@"AD_TAG_URL",
    region:@"LOCATION"
    projectNumber:@"PROJECT_NUMBER"
    OAuthToken:@"OAUTH_TOKEN"
    networkCode:@"NETWORK_CODE"
    contentSourceURL:@"CONTENT_SOURCE_URL"
    adDisplayContainer:adDisplayContainer
    videoDisplay:imaVideoDisplay
    userContext:nil)
  self.adsLoader!.requestStream(with: request)
}
</pre>

Run the project, then you can request and play your custom VOD stream.

Clean up

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

Follow the VOD clean up guide to remove any unneeded resources and assets.