<commit_before>/**
 * Copyright 2015 CANAL+ Group
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *     http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

/**
 * /!\ This file is feature-switchable.
 * It always should be imported through the `features` object.
 */

import {
  merge as observableMerge,
  Observable,
  of as observableOf,
  Subject,
} from "rxjs";
import {
  ignoreElements,
  map,
  mergeMap,
} from "rxjs/operators";
import {
  clearElementSrc,
  setElementSrc$,
} from "../../compat";
import {
  ICustomError,
  MediaError,
} from "../../errors";
import log from "../../log";
import { IKeySystemOption } from "../eme/types";
import { IStreamClockTick } from "./clock";
import createEMEManager from "./create_eme_manager";
import { IInitialTimeOptions } from "./get_initial_time";
import createMediaErrorHandler from "./media_error_handler";
import SpeedManager from "./speed_manager";
import StallingManager from "./stalling_manager";
import EVENTS, {
  IStreamEvent,
} from "./stream_events";
import seekAndLoadOnMediaEvent from "./video_events";

/**
 * @param {HTMLMediaElement} mediaElement
 * @returns {number}
 */
function getDirectFileInitialTime(
  mediaElement : HTMLMediaElement,
  startAt? : IInitialTimeOptions
) : number {
  if (!startAt) {
    return 0;
  }

  if (startAt.position != null) {
    return startAt.position;
  } else if (startAt.wallClockTime != null) {
    return startAt.wallClockTime;
  } else if (startAt.fromFirstPosition != null) {
    return startAt.fromFirstPosition;
  }

  const duration = mediaElement.duration;
  if (!duration || !isFinite(duration)) {
    log.warn("startAt.fromLastPosition set but no known duration, " +
      "beginning at 0.");
    return 0;
  }

  if (startAt.fromLastPosition) {
    return Math.max(0, duration + startAt.fromLastPosition);
  } else if (startAt.percentage != null) {
    const { percentage } = startAt;
    if (percentage >= 100) {
      return duration;
    } else if (percentage <= 0) {
      return 0;
    }
    const ratio = +percentage / 100;
    return duration * ratio;
  }

  return 0;
}

export interface IDirectFileStreamOptions {
  autoPlay : boolean;
  clock$ : Observable<IStreamClockTick>;
  keySystems : IKeySystemOption[];
  mediaElement : HTMLMediaElement;
  speed$ : Observable<number>;
  startAt? : IInitialTimeOptions;
  url : string;
}

/**
 * Initialize stream playback by merging all Observables that are required to
 * make the system cooperate.
 * @param {MediaSource} mediaSource
 * @returns {Observable}
 */
export default function StreamDirectFile({
  autoPlay,
  clock$,
  keySystems,
  mediaElement,
  speed$,
  startAt,
  url,
} : IDirectFileStreamOptions) : Observable<IStreamEvent> {
  /**
   * Observable through which all warning events will be sent.
   * @type {Subject}
   */
  const warning$ = new Subject<Error|ICustomError>();
  const warningEvents$ = warning$.pipe(map(EVENTS.warning));

  clearElementSrc(mediaElement);

  log.debug("calculating initial time");
  const initialTime = () =><commit_msg>stream: fix JSDOC for directfile<commit_after>/**
 * Copyright 2015 CANAL+ Group
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *     http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

/**
 * /!\ This file is feature-switchable.
 * It always should be imported through the `features` object.
 */

import {
  merge as observableMerge,
  Observable,
  of as observableOf,
  Subject,
} from "rxjs";
import {
  ignoreElements,
  map,
  mergeMap,
} from "rxjs/operators";
import {
  clearElementSrc,
  setElementSrc$,
} from "../../compat";
import {
  ICustomError,
  MediaError,
} from "../../errors";
import log from "../../log";
import { IKeySystemOption } from "../eme/types";
import { IStreamClockTick } from "./clock";
import createEMEManager from "./create_eme_manager";
import { IInitialTimeOptions } from "./get_initial_time";
import createMediaErrorHandler from "./media_error_handler";
import SpeedManager from "./speed_manager";
import StallingManager from "./stalling_manager";
import EVENTS, {
  IStreamEvent,
} from "./stream_events";
import seekAndLoadOnMediaEvent from "./video_events";

/**
 * @param {HTMLMediaElement} mediaElement
 * @returns {number}
 */
function getDirectFileInitialTime(
  mediaElement : HTMLMediaElement,
  startAt? : IInitialTimeOptions
) : number {
  if (!startAt) {
    return 0;
  }

  if (startAt.position != null) {
    return startAt.position;
  } else if (startAt.wallClockTime != null) {
    return startAt.wallClockTime;
  } else if (startAt.fromFirstPosition != null) {
    return startAt.fromFirstPosition;
  }

  const duration = mediaElement.duration;
  if (!duration || !isFinite(duration)) {
    log.warn("startAt.fromLastPosition set but no known duration, " +
      "beginning at 0.");
    return 0;
  }

  if (startAt.fromLastPosition) {
    return Math.max(0, duration + startAt.fromLastPosition);
  } else if (startAt.percentage != null) {
    const { percentage } = startAt;
    if (percentage >= 100) {
      return duration;
    } else if (percentage <= 0) {
      return 0;
    }
    const ratio = +percentage / 100;
    return duration * ratio;
  }

  return 0;
}

export interface IDirectFileStreamOptions {
  autoPlay : boolean;
  clock$ : Observable<IStreamClockTick>;
  keySystems : IKeySystemOption[];
  mediaElement : HTMLMediaElement;
  speed$ : Observable<number>;
  startAt? : IInitialTimeOptions;
  url : string;
}

/**
 * @param {Object} directfileOptions
 * @returns {Observable}
 */
export default function StreamDirectFile({
  autoPlay,
  clock$,
  keySystems,
  mediaElement,
  speed$,
  startAt,
  url,
} : IDirectFileStreamOptions) : Observable<IStreamEvent> {
  /**
   * Observable through which all warning events will be sent.
   * @type {Subject}
   */
  const warning$ = new Subject<Error|ICustomError>();
  const warningEvents$ = warning$.pipe(map(EVENTS.warning));

  clearElementSrc(mediaElement);

  log.debug("calculating initial time");
  const initialTime = () =>
{'fraction_non_alphanumeric': 0.11512872432745155, 'fraction_numerical': 0.00607463118310674, 'mean_word_length': 3.80542043085476, 'pattern_counts': {'":': 0, '<': 13, '<?xml version=': 0, '>': 15, 'https://': 0, 'lorem ipsum': 0, 'www.': 2, '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': '3195891', 'n_tokens_mistral': 2055, 'n_tokens_neox': 2011, 'n_words': 631}