播放透過 Google Cloud Video Stitcher API 註冊的直播串流
本指南說明如何使用適用於 iOS 的 IMA DAI SDK,為向 Google Cloud Video Stitcher API 註冊的活動請求及播放直播,以及如何在播放期間插入廣告插播。
本指南將擴充入門指南中的基本範例,說明如何使用 IMA DAI。
如要瞭解如何與其他平台整合,或使用 IMA 用戶端 SDK,請參閱「互動式媒體廣告 SDK」。
如要查看或跟著完成整合範例,請下載 Objective-C 或 Swift 的 Cloud Video Stitcher 範例。
設定 Google Cloud 專案
輸入下列變數,供 IMA SDK 使用:
- 位置
- 建立即時設定的 Google Cloud 區域:
LOCATION - 專案編號
- 使用 Video Stitcher API 的 Google Cloud 雲端專案編號:
PROJECT_NUMBER - OAuth 權杖
服務帳戶的短期 OAuth 權杖,並具備 Video Stitcher 使用者角色:
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 範例,然後將 Basic Example 解壓縮至新資料夾。這個範例是 Xcode 專案,依賴 Cocoapods 載入 IMA SDK。
如要準備執行範例,請確認已安裝 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 *streamRequest =
[[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:streamRequest];
}
(選用) 新增串流工作階段選項
如要自訂串流要求,請在 IMAVideoStitcherLiveStreamRequest 中填入 videoStitcherSessionOptions 參數,加入工作階段選項,覆寫預設的 Cloud Video Stitcher API 設定。如果提供的選項無法辨識,Cloud Video Stitcher API 會傳回 HTTP 400 錯誤。如需協助,請參閱疑難排解指南。
舉例來說,您可以透過下列程式碼片段覆寫資訊清單選項,要求兩個串流資訊清單,並依最低到最高位元率排序轉譯內容。
Objective-C
// Define session options JSON string.
// The following session options are examples. Use session options
// that are compatible with your video stream.
NSString *sessionOptionsStr =
@"{"
" \"manifestOptions\": {"
" \"bitrateOrder\": \"ascending\""
" }"
"}";
// 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 *streamRequest =
[[IMAVideoStitcherLiveStreamRequest alloc] initWithLiveStreamEventID:kLiveConfigID
region:kLocation
projectNumber:kProjectNumber
OAuthToken:kOAuthToken
networkCode:kNetworkCode
customAssetKey:kCustomAssetKey
adDisplayContainer:adDisplayContainer
videoDisplay:imaVideoDisplay
userContext:nil
videoStitcherSessionOptions:sessionOptions];
[self.adsLoader requestStreamWithRequest:streamRequest];
Swift
// Define session options JSON string.
// The following session options are examples. Use session options
// that are compatible with your video stream.
let sessionOptionsStr = """
{
"manifestOptions": {
"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 streamRequest = IMAVideoStitcherLiveStreamRequest(
liveStreamEventID:ViewController.liveConfigID
region:ViewController.location
projectNumber:ViewController.projectNumber
OAuthToken:ViewController.oAuthToken
networkCode:ViewController.networkCode
customAssetKey:ViewController.customAssetKey
adDisplayContainer:adDisplayContainer
videoDisplay:imaVideoDisplay
userContext:nil
videoStitcherSessionOptions:sessionOptions)
adsLoader?.requestStream(with: streamRequest)
執行專案,然後要求及播放自訂直播。
插入廣告插播
Google Cloud Video Stitcher API 會針對每個廣告插播時間,插入從廣告代碼擷取的廣告。廣告插播會在資訊清單中以廣告標記表示。廣告標記由直播編碼器插入。
如果使用自己的直播,請插入廣告標記。如要進一步瞭解支援的 HLS 和 DASH 廣告標記,請參閱廣告標記說明文件。
如果使用 Google Cloud Livestream API 建立直播,請插入廣告插播頻道事件。
插入廣告插播後,系統會立即播放廣告。
清除所用資源
您已使用 Google Cloud Video Stitcher API 成功代管直播串流,並使用 iOS 適用的 IMA DAI SDK 提出要求,現在請務必清理所有放送資源。
請按照直播清理指南操作,移除所有不必要的資源和資產。