ライブ配信のプリフェッチ

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 "Get Started" guide in the prerequisites above.
  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>, google.ima.ViewMode.NORMAL);
  adsManager.start();
}