IMA SDK を使用して、ライブ ストリームとビデオ オンデマンドの両方を収益化できます。ライブ ストリームの場合は、広告ブレークごとに新しい広告リクエストを行う必要があります。これらのリクエストをずらして、すべての視聴者が同時に広告をリクエストして広告サーバーの処理を遅らせることがないようにします。
この問題を解決するために、IMA SDK には AdsRequest.liveStreamPrefetchSeconds
プロパティがあります。このプロパティは、AdsLoader.requestAds()
を呼び出した後、SDK が広告サーバーに接続するまで待機する最大秒数を指定します。実際のリクエスト時間はランダム化されます。たとえば、AdsRequest.liveStreamPrefetchSeconds
を 30 に設定すると、SDK は AdsLoader.requestAds()
を呼び出してから 0 ~ 30 秒待機してから、実際にサーバーにリクエストを送信します。
ライブ ストリームのプリフェッチの実装
広告ブレークが完了したらすぐに次の広告ブレークをプリフェッチすることをおすすめします。これにより、プリフェッチ ウィンドウで最長の時間が確保されます。広告ブレークの間隔が 5 分の場合を考えてみましょう。広告ブレークが完了したら、290 秒のプリフェッチ ウィンドウで次の広告ブレークをリクエストできます(プリフェッチ ウィンドウの最後に送信されたリクエストが解決されるのに十分な時間を確保するため、5 分から 10 秒を引いた値)。
// 5 minutes == 300 seconds. Include a 10 second buffer
var AD_INTERVAL = 290;
function onAdEvent(adEvent) {
var ad = adEvent.getAd();
switch(adEvent.type) {
case google.ima.AdEvent.Type.ALL_ADS_COMPLETED:
// Pre-fetch our next ad break.
requestAds();
// Play those ads in 5 minutes. In a real-world implementation,
// this is likely done as the result of a message from your
// streaming server, not a timeout.
setTimeout(playAds, AD_INTERVAL * 1000);// Convert to ms.
}
}
function requestAds() {
// Destroy the current AdsManager, in case the tag you requested previously
// contains post-rolls (don't play those now).
if (adsManager) {
adsManager.destroy();
}
// Your AdsLoader will be set up on page-load. You should re-use the same
// AdsLoader for every request. For more info on setting up the AdsLoader,
// see the IMA HTML5 client-side "Set up the IMA SDK" guide:
// https://developers.google.com/interactive-media-ads/docs/sdks/html5/client-side/get-started
if (adsLoader) {
// Reset the IMA SDK.
adsLoader.contentComplete();
}
var adsRequest = new google.ima.AdsRequest();
adsRequest.adTagUrl = '...';
adsRequest.linearAdSlotWidth = <linear_width>;
adsRequest.linearAdSlotHeight = <linear_height>;
adsRequest.nonLinearAdSlotWidth = <nonlinear_width>;
adsRequest.nonLinearAdSlotHeight = <nonlinear_height>;
adsRequest.liveStreamPrefechSeconds = AD_INTERVAL;
adsLoader.requestAds(adsRequest);
}
function playAds() {
adsManager.init(<linear_width>, <linear_height>);
adsManager.start();
}