라이브 스트림에 프리롤 광고 로드

플랫폼 선택: HTML5 Android iOS tvOS

IMA SDK는 VOD뿐만 아니라 라이브 스트림으로도 수익을 창출하는 데 사용할 수 있습니다. 라이브 스트림의 경우 각 광고 시점에 대해 새 광고 요청을 해야 합니다. 모든 시청자가 동시에 광고를 요청하여 광고 서버가 과부하되지 않도록 이러한 요청을 분산하세요.

이를 위해 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();
}