This guide demonstrates how to use the IMA DAI SDK for iOS 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
- User context
- The user context for tracking requests.Can be
nil
. Defaults tonil
in this guide.
Download and prepare the basic example
Download the IMA DAI examples for iOS and extract the Basic Example into a new folder. This example is an Xcode project that relies on Cocoapods to load the IMA 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
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 live stream
To replace the sample stream with your live stream, you need to use the
IMAVideoStitcherLiveStreamRequest
class that automatically creates 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 examples for requesting a VOD stream or a live
stream from Google's DAI servers. To make the sample work with the Google Cloud
Video Stitcher API, you will need to replace the current requestStream
function with one that uses the IMAVideoStitcherLiveStreamRequest
class:
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 = @"//devimages.apple.com/iphone/samples/bipbop/bipbopall.m3u8"; static NSString *const kLiveConfigId = @"LIVE_CONFIG_ID"; static NSString *const kLocation = @"LOCATION"; static NSString *const kProjectNumber = @"PROJECT_NUMBER"; static NSString *const kOAuthToken = @"OAUTH_TOKEN"; static NSString *const kNetworkCode = @"NETWORK_CODE"; static NSString *const kCustomAssetKey = @"ASSET_KEY"; @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]; IMAVideoStitcherLiveStreamRequest *request = [[IMAVideoStitcherLiveStreamRequest alloc] initWithLiveStreamEventID:kLiveConfigId region:kLocation projectNumber:kProjectNumber OAuthToken:kOAuthToken networkCode:kNetworkCode customAssetKey:kCustomAssetKey adDisplayContainer:adDisplayContainer videoDisplay:imaVideoDisplay userContext:nil]; [self.adsLoader requestStreamWithRequest:request]; } ...
Run the project, then you can 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 iOS, it's important to clean up any serving resources.
Follow the Unregister live stream events guide to end your live stream event.