Google Cloud Video Stitcher API で登録された VOD ストリームの再生
このガイドでは、iOS 向け IMA DAI SDK を使用して Google Cloud VOD ストリーム セッションをリクエストして再生する方法について説明します。
このガイドでは、IMA DAI のスタートガイドの基本的な例を拡張します。
他のプラットフォームとの統合や IMA クライアントサイド SDK の使用については、Interactive Media Ads SDK をご覧ください。
完成したサンプル統合を表示または確認する場合は、Objective-C または Swift 用の Cloud Video Stitcher のサンプルをダウンロードしてください。
Google Cloud プロジェクトの設定
IMA SDK で使用する次の変数を入力します。
- ロケーション
- VOD 構成が作成された Google Cloud リージョン:
LOCATION - プロジェクト番号
- Video Stitcher API を使用する Google Cloud プロジェクト番号:
PROJECT_NUMBER - OAuth トークン
Video Stitcher ユーザーロールを持つサービス アカウントの有効期間が短い OAuth トークン:
OAUTH_TOKEN
有効期間が短い OAuth トークンの作成の詳細を確認します。OAuth トークンは、期限が切れていない限り、複数のリクエストで再利用できます。
- ネットワーク コード
広告をリクエストするためのアド マネージャー ネットワーク コード:
NETWORK_CODE
- VOD 構成 ID
VOD ストリームの VOD 構成 ID:
VOD_CONFIG_IDVOD 構成 ID の作成について詳しくは、Cloud Stitching で VOD 構成を作成するガイドをご覧ください。
- ユーザーのコンテキスト
- リクエストをトラッキングするためのユーザー コンテキスト。
nilが可能です。 このガイドではデフォルトでnilに設定されています。
基本的な例の設定
IMA iOS DAI GitHub リリース ページに移動し、基本的な Objective-C のサンプルをダウンロードします。この例は、CocoaPods を使用して IMA DAI SDK を読み込む iOS Xcode プロジェクトです。Swift を使用している場合、IMA には iOS サンプルアプリはありませんが、このガイドの後半にある Swift コード スニペットで、独自のアプリに IMAVideoStitcherVODStreamRequest を実装する方法を確認してください。
サンプルを実行する準備として、CocoaPods がインストールされていることを確認してから、ターミナルで基本例のフォルダを開き、次のコマンドを実行します。
pod install --repo-updateコマンドが完了すると、プロジェクト フォルダに BasicExample.xcworkspace というファイルが表示されます。Xcode でこのファイルを開き、サンプルを実行して、テスト動画と広告が想定どおりに再生されることを確認します。
VOD ストリームのリクエスト
サンプル ストリームを広告合成された VOD ストリームに置き換えるには、IMAVideoStitcherVODStreamRequest を使用して Google アド マネージャーで広告セッションを作成します。Google アド マネージャーの UI を使用して、モニタリングとデバッグのために、生成された DAI セッションを特定できます。
既存のサンプルには、Google の DAI サーバーから VOD ストリームまたはライブ ストリームをリクエストする例があります。Google Cloud Video Stitcher API で動作させるには、現在の requestStream 関数を IMAVideoStitcherVODStreamRequest クラスを使用する関数に置き換える必要があります。
次に例を示します。
Objective-C
ViewController.m
...
#import <GoogleInteractiveMediaAds/GoogleInteractiveMediaAds.h>
/// The Stream's VOD config ID.
static NSString *const kVODConfigID = @"VOD_CONFIG_ID";
/// The Network Code used by the Video Stitcher API.
static NSString *const kNetworkCode = @"NETWORK_CODE";
/// The Google Cloud project using the Video Stitcher API.
static NSString *const kProjectNumber = @"PROJECT_NUMBER";
/// The Google Cloud region containing your project.
static NSString *const kLocation = @"LOCATION";
/// An OAuth Token created by a Google Cloud account with Video Stitcher API
/// permissions.
static NSString *const kOAuthToken = @"OAUTH_TOKEN";
/// Fallback URL in case something goes wrong in loading the stream. If all goes well, this will not
/// be used.
static NSString *const kBackupStreamURLString =
@"http://googleimadev-vh.akamaihd.net/i/big_buck_bunny/bbb-,480p,720p,1080p,.mov.csmil/"
@"master.m3u8";
@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 *streamRequest =
[[IMAVideoStitcherVODStreamRequest alloc] initWithVODConfigID:kVODConfigID
region:kLocation
projectNumber:kProjectNumber
OAuthToken:kOAuthToken
networkCode:kNetworkCode
adDisplayContainer:adDisplayContainer
videoDisplay:imaVideoDisplay
userContext:nil
videoStitcherSessionOptions:nil];
[self.adsLoader requestStreamWithRequest:streamRequest];
}
...
Swift
ViewController.swift
...
class ViewController: UIViewController, IMAAdsLoaderDelegate, IMAStreamManagerDelegate {
/// The Stream's VOD config ID.
static let vodConfigID = "VOD_CONFIG_ID"
/// The Network Code used by the Video Stitcher API.
static let networkCode = "NETWORK_CODE"
/// The Google Cloud project using the Video Stitcher API.
static let projectNumber = "PROJECT_NUMBER"
/// The Google Cloud region containing your project.
static let location = "LOCATION"
/// An OAuth Token created by a Google Cloud account with Video Stitcher API
/// permissions.
static let oAuthToken = "OAUTH_TOKEN"
/// Fallback URL in case something goes wrong in loading the stream. If all goes well, this will
/// not be used.
static let backupStreamURLString = """
http://googleimadev-vh.akamaihd.net/i/big_buck_bunny/\
bbb-,480p,720p,1080p,.mov.csmil/master.m3u8
"""
...
func requestStream() {
// Create an ad display container for ad rendering.
adDisplayContainer = IMAAdDisplayContainer(
adContainer: videoView,
viewController: self,
companionSlots: nil)
// Create an IMAAVPlayerVideoDisplay to give the SDK access to your video player.
let imaVideoDisplay = IMAAVPlayerVideoDisplay(avPlayer: contentPlayer!)
// Create a VOD stream request.
let streamRequest = IMAVideoStitcherVODStreamRequest(
VODConfigID: ViewController.vodConfigID,
region: ViewController.location,
projectNumber: ViewController.projectNumber,
oAuthToken: ViewController.oAuthToken,
networkCode: ViewController.networkCode,
adDisplayContainer: adDisplayContainer!,
videoDisplay: imaVideoDisplay,
userContext: nil,
videoStitcherSessionOptions: nil)
adsLoader?.requestStream(with: streamRequest)
}
...
プロジェクトを実行すると、カスタム VOD ストリームをリクエストして再生できます。
(省略可)ストリーミング セッション オプションを追加する
IMAVideoStitcherVODStreamRequest に videoStitcherSessionOptions パラメータを入力して、デフォルトの Cloud Video Stitcher API 構成をオーバーライドするセッション オプションを追加することで、ストリーム リクエストをカスタマイズします。
認識されないオプションを指定すると、Cloud Video Stitcher API により HTTP 400 エラーの応答が返されます。詳しくは、トラブルシューティング ガイドをご覧ください。
たとえば、次のコード スニペットを使用してマニフェスト オプションをオーバーライドできます。このコードは、ビットレートを低い順に並べた 2 つのストリーム マニフェストをリクエストします。
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
IMAVideoStitcherVODStreamRequest *streamRequest =
[[IMAVideoStitcherVODStreamRequest alloc] initWithVODConfigID:kVODConfigID
region:kLocation
projectNumber:kProjectNumber
OAuthToken:kOAuthToken
networkCode:kNetworkCode
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 = IMAVideoStitcherVODStreamRequest(
vodConfigID:ViewController.vodConfigID
region:ViewController.location
projectNumber:ViewController.projectNumber
OAuthToken:ViewController.oAuthToken
networkCode:ViewController.networkCode
adDisplayContainer:adDisplayContainer
videoDisplay:imaVideoDisplay
userContext:nil
videoStitcherSessionOptions:sessionOptions)
adsLoader?.requestStream(with: streamRequest)
クリーンアップ
Google Cloud Video Stitcher API を使用して VOD ストリームをホストし、iOS 用 IMA DAI SDK を使用してリクエストしたので、サービング リソースをクリーンアップすることが重要です。
VOD のクリーンアップ ガイドに沿って、不要なリソースとアセットを削除します。