<commit_before>import {HTML5Video, Log, Events, PlayerError} from 'clappr'
import shaka from 'shaka-player'

const SEND_STATS_INTERVAL_MS = 30 * 1e3
const DEFAULT_LEVEL_AUTO = -1

class DashShakaPlayback extends HTML5Video {
  static get Events () {
    return {
      SHAKA_READY: 'shaka:ready'
    }
  }

  static get shakaPlayer() { return shaka }

  static canPlay (resource, mimeType = '') {
    shaka.polyfill.installAll()
    let browserSupported = shaka.Player.isBrowserSupported()
    let resourceParts = resource.split('?')[0].match(/.*\.(.*)$/) || []
    return browserSupported && ((resourceParts[1] === 'mpd') || mimeType.indexOf('application/dash+xml') > -1)
  }

  get name () {
    return 'dash_shaka_playback'
  }

  get shakaVersion () {
    return shaka.player.Player.version
  }

  get shakaPlayerInstance () {
    return this._player
  }

  get levels () {
    return this._levels
  }

  get seekRange() {
    if (!this.shakaPlayerInstance) return { start: 0, end: 0}

    return this.shakaPlayerInstance.seekRange()
  }

  set currentLevel (id) {
    this._currentLevelId = id
    let isAuto = this._currentLevelId === DEFAULT_LEVEL_AUTO

    this.trigger(Events.PLAYBACK_LEVEL_SWITCH_START)
    if (!isAuto) {
      this._player.configure({abr: {enabled: false}})
      this._pendingAdaptationEvent = true
      this.selectTrack(this.videoTracks.filter((t) => t.id === this._currentLevelId)[0])
    }
    else {
      this._player.configure({abr: {enabled: true}})
      this.trigger(Events.PLAYBACK_LEVEL_SWITCH_END)
    }
  }

  get currentLevel () {
    return this._currentLevelId || DEFAULT_LEVEL_AUTO
  }

  get dvrEnabled() {
    return this._duration >= this._minDvrSize && this.getPlaybackType() === 'live'
  }

  getDuration() {
    return this._duration
  }

  get _duration() {
    if (!this.shakaPlayerInstance) return 0

    return this.seekRange.end - this.seekRange.start
  }

  getCurrentTime() {
    if (!this.shakaPlayerInstance) return 0

    return this.shakaPlayerInstance.getMediaElement().currentTime - this.seekRange.start
  }

  get _startTime() {
    return this.seekRange.start
  }

  get presentationTimeline() {
    if (!this.shakaPlayerInstance) return

    return this.shakaPlayerInstance.getManifest().presentationTimeline
  }

  get bandwidthEstimate() {
    if (!this.shakaPlayerInstance) return
    return this.shakaPlayerInstance.getStats().estimatedBandwidth
  }

  constructor (...args) {
    super(...args)
    this._levels = []
    this._pendingAdaptationEvent = false
    this._isShakaReadyState = false

    this._minDvrSize = typeof (this.options.shakaMinimumDvrSize) === 'undefined' ? 60 : this.options.shakaMinimumDvrSize
  }

  getProgramDateTime() {
    if (!this.shakaPlayerInstance) return 0

    return new Date((this.presentationTimeline.getPresentationStartTime() + this.seekRange.start) * 1000)
  }

  _updateDvr(status) {
    this.trigger(Events.PLAYBACK_DVR, status)
    this.trigger(Events.PLAYBACK_STATS_ADD, { 'dvr': status })
  }

  seek(time) {
    if (time < 0) {
      Log.warn('Attempt to seek to a negative time. Resetting to live point. Use seekToLivePoint() to seek to the live point.')
      time = this._duration
    }
    // assume live if time within 3 seconds of end of stream<commit_msg>refactor(src): prevent access of invalid references<commit_after>import {HTML5Video, Log, Events, PlayerError} from 'clappr'
import shaka from 'shaka-player'

const SEND_STATS_INTERVAL_MS = 30 * 1e3
const DEFAULT_LEVEL_AUTO = -1

class DashShakaPlayback extends HTML5Video {
  static get Events () {
    return {
      SHAKA_READY: 'shaka:ready'
    }
  }

  static get shakaPlayer() { return shaka }

  static canPlay (resource, mimeType = '') {
    shaka.polyfill.installAll()
    let browserSupported = shaka.Player.isBrowserSupported()
    let resourceParts = resource.split('?')[0].match(/.*\.(.*)$/) || []
    return browserSupported && ((resourceParts[1] === 'mpd') || mimeType.indexOf('application/dash+xml') > -1)
  }

  get name () {
    return 'dash_shaka_playback'
  }

  get shakaVersion () {
    return shaka.player.Player.version
  }

  get shakaPlayerInstance () {
    return this._player
  }

  get levels () {
    return this._levels
  }

  get seekRange() {
    if (!this.shakaPlayerInstance) return { start: 0, end: 0}

    return this.shakaPlayerInstance.seekRange()
  }

  set currentLevel (id) {
    this._currentLevelId = id
    let isAuto = this._currentLevelId === DEFAULT_LEVEL_AUTO

    this.trigger(Events.PLAYBACK_LEVEL_SWITCH_START)
    if (!isAuto) {
      this._player.configure({abr: {enabled: false}})
      this._pendingAdaptationEvent = true
      this.selectTrack(this.videoTracks.filter((t) => t.id === this._currentLevelId)[0])
    }
    else {
      this._player.configure({abr: {enabled: true}})
      this.trigger(Events.PLAYBACK_LEVEL_SWITCH_END)
    }
  }

  get currentLevel () {
    return this._currentLevelId || DEFAULT_LEVEL_AUTO
  }

  get dvrEnabled() {
    return this._duration >= this._minDvrSize && this.getPlaybackType() === 'live'
  }

  getDuration() {
    return this._duration
  }

  get _duration() {
    if (!this.shakaPlayerInstance) return 0

    return this.seekRange.end - this.seekRange.start
  }

  getCurrentTime() {
    if (!this.shakaPlayerInstance) return 0

    return this.shakaPlayerInstance.getMediaElement().currentTime - this.seekRange.start
  }

  get _startTime() {
    return this.seekRange.start
  }

  get presentationTimeline() {
    if (!this.shakaPlayerInstance || !this.shakaPlayerInstance.getManifest()) return null

    return this.shakaPlayerInstance.getManifest().presentationTimeline
  }

  get bandwidthEstimate() {
    if (!this.shakaPlayerInstance) return
    return this.shakaPlayerInstance.getStats().estimatedBandwidth
  }

  constructor (...args) {
    super(...args)
    this._levels = []
    this._pendingAdaptationEvent = false
    this._isShakaReadyState = false

    this._minDvrSize = typeof (this.options.shakaMinimumDvrSize) === 'undefined' ? 60 : this.options.shakaMinimumDvrSize
  }

  getProgramDateTime() {
    if (!this.shakaPlayerInstance || !this.presentationTimeline) return 0

    return new Date((this.presentationTimeline.getPresentationStartTime() + this.seekRange.start) * 1000)
  }

  _updateDvr(status) {
    this.trigger(Events.PLAYBACK_DVR, status)
    this.trigger(Events.PLAYBACK_STATS_ADD, { 'dvr': status })
  }

  seek(time) {
    if (time < 0) {
      Log.warn('Attempt to seek to a negative time. Resetting to live point. Use seekToLivePoint() to seek to the live point.')
      time = this._duration
    }
    // assume live if time within 3 seconds of end of stream
{'fraction_non_alphanumeric': 0.11972463334331039, 'fraction_numerical': 0.007183478000598623, 'mean_word_length': 3.4612817089452603, 'pattern_counts': {'":': 0, '<': 5, '<?xml version=': 0, '>': 9, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 2}, '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': '649116', 'n_tokens_mistral': 2208, 'n_tokens_neox': 2107, 'n_words': 443}