MSE Safari Protect

100% I-Frames

by dustinkerstein

HTML

<video id="PanoMoment"></video>

CSS

html, body {
  margin: 0;
  width: 100%;
  height: 100%;
  overflow: hidden;
  background-color: #000;
}

#PanoMoment {
  margin: 0;
  width: 100%;
  height: 100%;
}

JavaScript

var sourceBuffer;
var frameRate;
var index = 0;
var segments = [];
var ms = new MediaSource();
var videoBits, arrayOfBytes, chunk, file;

var video = document.querySelector('video');
video.src = window.URL.createObjectURL(ms);
ms.addEventListener('sourceopen', onMediaSourceOpen);

function animate() {
  window.requestAnimationFrame(render);
}

function render() {
  window.requestAnimationFrame(render);
  if (video.readyState === video.HAVE_ENOUGH_DATA) {
    index = (index + 1) % segments.length;
    nextSegment(index);
  }
}

function videoInit(initBytes) {
  videoBits = initBytes;
  chunk = initBytes.slice(0, parseInt(segments[0].getAttribute("mediaRange").toString().split('-')[1]) + 1);
  appendToBuffer(chunk);
  animate();
}

function onMediaSourceOpen() {
  sourceBuffer = ms.addSourceBuffer('video/mp4; codecs="avc1.640033"');
  getData('uhd_dash.mpd'); // 100% I-Frames
}

function nextSegment(index) {

  if (index < segments.length) {
    arrayOfBytes = segments[index].getAttribute("mediaRange").toString().split('-');
    chunk = videoBits.slice(arrayOfBytes[0], parseInt(arrayOfBytes[1]) + 1);
    appendToBuffer(chunk);
  }

}

function appendToBuffer(videoChunk) {
  if (sourceBuffer && sourceBuffer.updating) {
    console.log("Buffer not ready");
  }
  if (videoChunk && sourceBuffer && !sourceBuffer.updating) {
    var bufferedIndices = [];
    if (sourceBuffer.buffered.length > 0) {
      for (var i = 0; i < sourceBuffer.buffered.length; i++) { 
        for (var j = sourceBuffer.buffered.start(i); j < sourceBuffer.buffered.end(i); j++) { 
          bufferedIndices[j] = true;
        }
      }
    }
    if (!bufferedIndices[index]) {
      sourceBuffer.appendBuffer(videoChunk)
    }
    video.fastSeek(index);
    //video.currentTime = index;
  }
}

function getData(url) {
  if (url !== "") {
    url = "https://files.panomoments.com/" + url;
    var xhr = new XMLHttpRequest(); // Set up xhr request
    xhr.open("GET", url, true); // Open the request
   ...