在 iOS 上使用 IMA DAI SDK

播放通过 Google Cloud Video Stitcher API 注册的直播

本指南演示了如何使用 iOS 版 IMA DAI SDK 为通过 Google Cloud Video Stitcher API 注册的事件请求并播放直播,以及如何在播放期间插入广告插播时间点。

本指南对 IMA DAI 入门指南中的基本示例进行了扩展。

如需了解如何与其他平台集成或使用 IMA 客户端 SDK,请参阅互动式媒体广告 SDK

设置 Google Cloud 项目

输入以下变量,以便在 IMA SDK 中使用:

位置
创建实时配置的 Google Cloud 区域LOCATION
项目编号
使用 Video Stitcher API 的 Google Cloud 项目编号:PROJECT_NUMBER
OAuth 令牌

具有 Video Stitcher 用户角色的服务帐号的短期有效 OAuth 令牌:

OAUTH_TOKEN

详细了解如何为服务帐号创建短期有效凭据。只要 OAuth 令牌未过期,就可以在多个请求中重复使用。

广告资源网代码

用于请求广告的 Ad Manager 广告资源网代码: NETWORK_CODE

实时配置 ID
您在创建直播活动时指定的直播配置 ID: LIVE_CONFIG_ID
自定义素材资源键
在使用 Video Stitcher API 为直播活动创建配置的过程中生成的 Ad Manager 自定义素材资源键: CUSTOM_ASSET_KEY

用户情境
用于跟踪请求的用户上下文。可以是 nil本指南中默认为 nil

下载并准备基本示例

下载适用于 iOS 的 IMA DAI 示例并将基本示例提取到新文件夹中。此示例是一个依赖于 Cocoapods 加载 IMA SDK 的 Xcode 项目。

如需准备运行示例,请确保已安装 CocoaPods,然后在终端中打开基本示例的文件夹并运行以下命令:

pod install --repo-update

该命令运行完成后,您会在项目文件夹中看到一个名为 BasicExample.xcworkspace 的文件。在 Xcode 中打开此文件并运行示例,以确保测试视频和广告按预期播放。

请求直播

若要将示例视频流替换为您的直播活动,您需要使用 IMAVideoStitcherLiveStreamRequest 类,该类会自动创建与 Google Ad Manager 的广告会话。您可以使用 Google Ad Manager 界面找到生成的 DAI 会话,以进行监控和调试。

在现有示例中,有一些示例用于从 Google 的 DAI 服务器请求 VOD 视频流或直播。如需使示例能够与 Google Cloud Video Stitcher API 配合使用,您需要将当前的 requestStream 函数替换为使用 IMAVideoStitcherLiveStreamRequest 类的函数:

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 = @"CUSTOM_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
                                                               videoStitcherSessionOptions:nil];

  [self.adsLoader requestStreamWithRequest:request];
}

(可选)添加直播会话选项

IMAVideoStitcherLiveStreamRequest 中填充 videoStitcherSessionOptions 参数,以添加会话选项来覆盖默认 Cloud Video Stitcher API 配置,从而自定义视频流请求。 如果您提供的选项无法识别,则 Cloud Video Stitcher API 将返回 HTTP 400 错误作为响应。如需帮助,请参阅问题排查指南

例如,您可以使用以下代码段替换清单选项,该代码段请求两个数据流清单,它们的呈现结果按比特率从低到高排序。

Objective-C

 // define session options JSON string
 NSString *sessionOptionsStr =
   @"{"
   "  \"manifestOptions\": {"
   "    \"includeRenditions\":["
   "      {\"bitrateBps\": 3000, \"codecs\": \"hvc1.1.4.L126.B0, mp4a.40.2\"},"
   "      {\"bitrateBps\": 2000, \"codecs\": \"avc1.64001f, mp4a.40.2\"},"
   "    ]"
   "  }"
   "}";
 // convert JSON NSString to NSDictionary
 NSData *sessionOptionsData = [sessionOptionsStr dataUsingEncoding:NSUTF8StringEncoding];
 NSError *error = nil;
 NSDictionary *sessionOptions = [NSJSONSerialization
                       JSONObjectWithData:sessionOptionsData
                       options:0
                       error:&error];
 // make stream request
 IMAVideoStitcherLiveStreamRequest *request =
     [[IMAVideoStitcherLiveStreamRequest alloc] initWithLiveStreamEventID:kLiveConfigId
                                                                   region:kRegion
                                                           projectNumber:kProjectNumber
                                                               OAuthToken:kOAuthToken
                                                             networkCode:kNetworkCode
                                                           customAssetKey:kCustomAssetKey
                                                       adDisplayContainer:adDisplayContainer
                                                             videoDisplay:imaVideoDisplay
                                                             userContext:nil
                                             videoStitcherSessionOptions:sessionOptions];
 [self.adsLoader requestStreamWithRequest:request];

Swift

 // define session options JSON string
 let sessionOptionsStr = """
     {
       "manifestOptions": {
         "includeRenditions": [
           {"bitrateBps": 3000, "codecs": "hvc1.1.4.L126.B0, mp4a.40.2"},
           {"bitrateBps": 2000, "codecs": "avc1.64001f, mp4a.40.2"},
         ],
         "bitrateOrder": "ascending"
       }
     }
     """
 // convert JSON string to dictionary
 guard let sessionOptionsData = sessionOptionsStr.data(using: .utf8, allowLossyConversion: false) else { return nil }
 let sessionOptions = try? JSONSerialization.jsonObject(with: sessionOptionsData, options: .mutableContainers)
 // make stream request
 let request = IMAVideoStitcherLiveStreamRequest(
   liveStreamEventID:ViewController.liveConfigId
   region:ViewController.region
   projectNumber:ViewController.projectNumber
   OAuthToken:ViewController.oAuthToken
   networkCode:ViewController.networkCode
   customAssetKey:ViewController.customAssetKey
   adDisplayContainer:adDisplayContainer
   videoDisplay:imaVideoDisplay
   userContext:nil
   videoStitcherSessionOptions:sessionOptions)
 adsLoader?.requestStream(with: request)

运行项目,然后你可以请求并播放你的自定义直播。

插入广告插播时间点

Google Cloud Video Stitcher API 会针对每个广告插播时间点插入从广告代码中检索到的广告。在清单中使用广告标记来指明广告插播时间点。广告标记由直播编码器插入。

插入广告插播时间点之后,广告会立即播放。

清理

现在,您已成功使用 Google Cloud Video Stitcher API 托管了直播,并使用适用于 iOS 的 IMA DAI SDK 发出了请求,请务必清除所有投放资源。

请按照直播清理指南移除所有不需要的资源和资产。