<commit_before>/*
 * video-js-mbr
 *
 *
 * Copyright (c) 2014 Brightcove
 * All rights reserved.
 */

(function (window, videojs) {

  videojs.mbr = {
    autoSwitch: true,
    currentIndex: 0,
    bandwidth: -1
  };

  var init = function (options) {
    var player = this,
    autoSwitch = (options && options.autoSwitch !== undefined) ? options.autoSwitch : videojs.mbr.autoSwitch,
    bandwidthOverride = (options && options.bandwidthOverride !== undefined) ? options.bandwidthOverride : videojs.mbr.bandwidth,
    currentIndex = (options && options.currentIndex !== undefined) ? options.currentIndex : videojs.mbr.currentIndex,

    originalSelectPlaylistFunction,
    originalBandwidthFunction,

    onBandwidthOverride = function(value) {
      if(value) {
        // Don't do anything here, we are using the override value on getter.
      } else {
        //console.log('bandwidth overridden');
        return bandwidthOverride;
      }
    },

    onManualModeSelectPlaylistOverride = function() {
      console.log('selectPlaylist overridden due to manual mode');
      var playlists = player.hls.master.playlists.slice();
      return playlists[currentIndex];
    };

    player.mbr.autoSwitch = function (value) {
      if (value !== undefined) {
        //console.log('setting autoswitch:', value);

        autoSwitch = value;

        if(autoSwitch) {
          player.hls.selectPlaylist = originalSelectPlaylistFunction;
        } else {
          player.hls.selectPlaylist = onManualModeSelectPlaylistOverride;
        }

      } else {
        return autoSwitch;
      }
    };

    player.mbr.bandwidthOverride = function (bandwidth) {
      if (bandwidth) {
        //console.log('setting bandwidth override:', bandwidth);
        bandwidthOverride = bandwidth;
        if (player.hls && player.hls.sourceBuffer) {
          if (bandwidth === -1) {
            player.hls.bandwidth = originalBandwidthFunction;
          } else {
            player.hls.bandwidth = onBandwidthOverride;
          }
        }
      } else {
        return bandwidthOverride;
      }
    };

    player.mbr.currentIndex = function (index) {
      if (index !== undefined) {
        console.log('setting current index:', index);
        if (currentIndex !== index) {
          currentIndex = index;

          console.log('needs switch');
          player.hls.media = player.hls.selectPlaylist();
          player.currentTime(player.currentTime());
        }
      } else {
        return currentIndex;
      }
    };

    /*
     Event Handlers
     */

    player.on('loadedmanifest', function () {
      if (player.hls.media === undefined) {
        if (player.hls.master.playlists &&
            player.hls.master.playlists instanceof Array &&
            player.hls.master.playlists.length > 1) {

          console.log('MBR SOURCE DETECTED BY PLUGIN');

          originalBandwidthFunction = player.hls.bandwidth;
          originalSelectPlaylistFunction = player.hls.selectPlaylist;

          player.mbr.autoSwitch(autoSwitch);
          player.mbr.currentIndex(currentIndex);
          player.mbr.bandwidthOverride(bandwidthOverride);

        }
      }<commit_msg>update with new validate function on hls plugin<commit_after>/*
 * video-js-mbr
 *
 *
 * Copyright (c) 2014 Brightcove
 * All rights reserved.
 */

(function (window, videojs) {

  videojs.mbr = {
    autoSwitch: true,
    currentIndex: 0,
    bandwidth: -1
  };

  var init = function (options) {
    var player = this,
    autoSwitch = (options && options.autoSwitch !== undefined) ? options.autoSwitch : videojs.mbr.autoSwitch,
    bandwidthOverride = (options && options.bandwidthOverride !== undefined) ? options.bandwidthOverride : videojs.mbr.bandwidth,
    currentIndex = (options && options.currentIndex !== undefined) ? options.currentIndex : videojs.mbr.currentIndex,

    originalSelectPlaylistFunction,
    originalBandwidthFunction,

    onBandwidthOverride = function(value) {
      if(value) {
        // Don't do anything here, we are using the override value on getter.
      } else {
        //console.log('bandwidth overridden');
        return bandwidthOverride;
      }
    },

    onManualModeSelectPlaylistOverride = function() {
      var playlists = player.hls.master.playlists.slice();
      return playlists[currentIndex];
    };

    player.mbr.autoSwitch = function (value) {
      if (value !== undefined) {

        autoSwitch = value;

        if(autoSwitch) {
          player.hls.selectPlaylist = originalSelectPlaylistFunction;
        } else {
          player.hls.selectPlaylist = onManualModeSelectPlaylistOverride;
        }

      } else {
        return autoSwitch;
      }
    };

    player.mbr.bandwidthOverride = function (bandwidth) {
      if (bandwidth) {
        //console.log('setting bandwidth override:', bandwidth);
        bandwidthOverride = bandwidth;
        if (player.hls && player.hls.sourceBuffer) {
          if (bandwidth === -1) {
            player.hls.bandwidth = originalBandwidthFunction;
          } else {
            player.hls.bandwidth = onBandwidthOverride;
          }
        }
      } else {
        return bandwidthOverride;
      }
    };

    player.mbr.currentIndex = function (index) {
      if (index !== undefined) {
        console.log('setting current index:', index);
        if (currentIndex !== index) {
          currentIndex = index;
          player.hls.validate(player.hls.selectPlaylist());

        }
      } else {
        return currentIndex;
      }
    };

    /*
     Event Handlers
     */

    player.on('loadedmanifest', function () {
      if (player.hls.media === undefined) {
        if (player.hls.master.playlists &&
            player.hls.master.playlists instanceof Array &&
            player.hls.master.playlists.length > 1) {

          console.log('MBR SOURCE DETECTED BY PLUGIN');

          originalBandwidthFunction = player.hls.bandwidth;
          originalSelectPlaylistFunction = player.hls.selectPlaylist;

          player.mbr.autoSwitch(autoSwitch);
          player.mbr.currentIndex(currentIndex);
          player.mbr.bandwidthOverride(bandwidthOverride);

        }
      }
{'fraction_non_alphanumeric': 0.111417004048583, 'fraction_numerical': 0.002591093117408907, 'mean_word_length': 2.3510580575149214, 'pattern_counts': {'":': 0, '<': 3, '<?xml version=': 0, '>': 5, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '7086398', 'n_tokens_mistral': 1780, 'n_tokens_neox': 1710, 'n_words': 340}