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.
This guide demonstrates how to play a DAI Pod Serving stream, using the IMA DAI SDK for iOS with a simple video player. If you would like to follow along with a completed sample, download the pod serving example.
IMA DAI Pod Serving overview
Implementing pod serving using the IMA DAI SDK for iOS involves two main components:
-
IMAStreamRequest
: An object that defines a stream request to Google's advertising servers. Must be created usingIMAPodStreamRequest()
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 9.2 or later
- CocoaPods (preferred) or a downloaded copy of the IMA SDK for iOS
- An iOS app already set up with the IMA DAI SDK to play video streams with DAI ads. If you do not already have such an app we recommend using the iOS DAI BasicExample as a starting point. The BasicExample will be the codebase referenced in this guide. For more information about how to implement the IMA DAI SDK, see the Quick Start Guide.
1. Set up your pod serving variables
All changes needed for pod serving will be done in ViewController.swift in swift or ViewController.m in Objective-C. The snippets in this guide will default to using the Swift language. The first step will be to update the constant variables.
Here are the ad pod stream request constants this guide will be adding:
STREAM_URL
: The video stream url provided by your manifest manipulator or 3rd 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 will be replaced with the stream ID, before making a request.NETWORK_CODE
: The network code for your AdManager360 account.CUSTOM_ASSET_KEY
: The custom asset key that identifies your pod serving event in AdManager360. This may be created by your manifest manipulator or 3rd 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? ...
2. 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) } ...
3 Edit and set the stream URL
Modify the IMAAdsLoaderDelegate
methods, using streamManager.streamId
to get
the stream ID. This will then need to be inserted into the STREAM_URL
,
replacing "[[STREAMID]]"
. Once 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 iOS DAI SDK. To see other examples of the iOS SDK being used, look to the samples on GitHub.