<commit_before>/*!
Copyright 2016-2020 Brazil Ltd.
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.
*/

import React, {
  useState,
  useRef,
  useEffect,
  useCallback,
  useMemo
} from "react";
import { StatusBar, Platform, StyleSheet } from "react-native";
import Video, { OnProgressData } from "react-native-video";
import KeepAwake from "react-native-keep-awake";
import Toast from "react-native-root-toast";
import { useDispatch, useSelector } from "react-redux";
import qs from "qs";
// @ts-ignore
import { VLCPlayer } from "react-native-vlc-media-player";
// @ts-ignore
import { Immersive } from "react-native-immersive";

import { RootState } from "../modules";
import { LoadingActions } from "../modules/loading";
import { PlayerActions } from "../modules/player";
import { SettingState } from "../modules/setting";
import { ViewerActions, ViewerProgram } from "../modules/viewer";
import { toastOptions } from "../config/constants";

type Setting = SettingState & {
  backend?: {
    type?: string;
    mobileStreamType?: string;
    mobileStreamParams?: string;
  };
  player?: {
    mute?: boolean;
    volume?: string;
    speed?: string;
    repeat?: string;
  };
};
type State = RootState & {
  setting: Setting;
};
const Player = () => {
  const videoRef = useRef<Video>(null);
  const vlcRef = useRef<VLCPlayer>(null);
  const preseek = useRef(0);
  const seekable = useRef(true);
  const seekId = useRef<number | null>(null);
  const initializing = useRef(true);

  const dispatch = useDispatch();
  const type = useSelector<State, string>(
    ({ setting }) => setting.backend?.type || "chinachu"
  );
  const mobileStreamType = useSelector<State, string>(
    ({ setting }) => setting.backend?.mobileStreamType || "mp4"
  );
  const mobileStreamParams = useSelector<State, string>(
    ({ setting }) =>
      setting.backend?.mobileStreamParams || "b:v=1M&b:a=128k&s=1280x720"
  );
  const mute = useSelector<State, boolean>(
    ({ setting }) => setting.player?.mute
  );
  const volume = useSelector<State, number>(({ setting }) =>
    parseInt(
      setting.player?.volume != null ? setting.player?.volume : "100",
      10
    )
  );
  const speed = useSelector<State, number>(({ setting }) =>
    parseFloat(setting.player?.speed || "1")
  );
  const deinterlace = useSelector<State, boolean>(
    ({ setting }) =>
      setting.player?.deinterlace == null || setting.player?.deinterlace
  );
  const repeat = useSelector<State, string>(
    ({ setting }) => setting.player?.repeat || "continue"
  );
  const networkType = useSelector<State, string>(({ network }) => network.type);
  const pause = useSelector<State, boolean>(({ player }) => player.pause);
  const duration = useSelector<State, number>(({ player }) => player.duration);
  const time = useSelector<State, number>(({ player }) => player.time);
  const audioTrack = useSelector<State, number>(
    ({ player }) => player.track.audio || 0
  );
  const dualMonoMode = useSelector<State, string>(
    ({ player }) => player.dualMonoMode
  );
  const seekTime = useSelector<State, number>(({ player }) => player.seekTime);
  const seekPosition = useSelector<State, number>(
    ({ player }) => player.seekPosition
  );
  const programs = useSelector<State, ViewerProgram[]>(
    ({ viewer }) => viewer.programs
  );
  const index = useSelector<State, number>(({ viewer }) => viewer.index);
  const extraIndex = useSelector<State, number>(
    ({ viewer: { extraIndex } }) => extraIndex
  );
  const peakPlay = useSelector<State, boolean>(({ viewer }) => viewer.peakPlay);

  const [bootstrap, setBootstrap] = useState(false);
  const [reset, setReset] = useState(false);
  const [ss, setStartSeconds] = useState(0);

  const recordedProgram = useMemo(
    () => programs[index].recorded?.[extraIndex] || programs[index],
    [programs, index, extraIndex]
  );
  const uri = useMemo(() => {
    let [uri, query] = recordedProgram.stream.split("?");
    if (recordedProgram.type !== "file") {
      if (
        networkType.indexOf("cell") >= 0 &&
        (type === "chinachu" || type === "epgstation")
      ) {
        uri = uri.replace(/[^.]+$/, mobileStreamType);
        query = mobileStreamParams;
      }
      if (type === "chinachu") {
        uri = uri.replace(/m2ts$/, "mp4");
      }
    }
    if (ss > 0) {
      query = `${qs.stringify({ ...qs.parse(query), ss })}`;
    }
    if (query) {
      uri += `?${query}`;
    }

    return uri;
  }, [mobileStreamType, mobileStreamParams, recordedProgram, networkType, ss]);
  const initOptions = useMemo(() => {
    const options = [];
    if (deinterlace) {
      options.push("--deinterlace=1", "--deinterlace-mode=discard");
    } else {
      options.push("--deinterlace=0");
    }
    switch (dualMonoMode) {
      case "both":
        options.push("--stereo-mode=1");
        break;
      case "main":
        options.push("--stereo-mode=3");
        break;
      case "sub":
        options.push("--stereo-mode=4");
        break;
    }<commit_msg>set cache options for VLC<commit_after>/*!
Copyright 2016-2020 Brazil Ltd.
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.
*/

import React, {
  useState,
  useRef,
  useEffect,
  useCallback,
  useMemo
} from "react";
import { StatusBar, Platform, StyleSheet } from "react-native";
import Video, { OnProgressData } from "react-native-video";
import KeepAwake from "react-native-keep-awake";
import Toast from "react-native-root-toast";
import { useDispatch, useSelector } from "react-redux";
import qs from "qs";
// @ts-ignore
import { VLCPlayer } from "react-native-vlc-media-player";
// @ts-ignore
import { Immersive } from "react-native-immersive";

import { RootState } from "../modules";
import { LoadingActions } from "../modules/loading";
import { PlayerActions } from "../modules/player";
import { SettingState } from "../modules/setting";
import { ViewerActions, ViewerProgram } from "../modules/viewer";
import { toastOptions } from "../config/constants";

type Setting = SettingState & {
  backend?: {
    type?: string;
    mobileStreamType?: string;
    mobileStreamParams?: string;
  };
  player?: {
    mute?: boolean;
    volume?: string;
    speed?: string;
    repeat?: string;
  };
};
type State = RootState & {
  setting: Setting;
};
const Player = () => {
  const videoRef = useRef<Video>(null);
  const vlcRef = useRef<VLCPlayer>(null);
  const preseek = useRef(0);
  const seekable = useRef(true);
  const seekId = useRef<number | null>(null);
  const initializing = useRef(true);

  const dispatch = useDispatch();
  const type = useSelector<State, string>(
    ({ setting }) => setting.backend?.type || "chinachu"
  );
  const mobileStreamType = useSelector<State, string>(
    ({ setting }) => setting.backend?.mobileStreamType || "mp4"
  );
  const mobileStreamParams = useSelector<State, string>(
    ({ setting }) =>
      setting.backend?.mobileStreamParams || "b:v=1M&b:a=128k&s=1280x720"
  );
  const mute = useSelector<State, boolean>(
    ({ setting }) => setting.player?.mute
  );
  const volume = useSelector<State, number>(({ setting }) =>
    parseInt(
      setting.player?.volume != null ? setting.player?.volume : "100",
      10
    )
  );
  const speed = useSelector<State, number>(({ setting }) =>
    parseFloat(setting.player?.speed || "1")
  );
  const deinterlace = useSelector<State, boolean>(
    ({ setting }) =>
      setting.player?.deinterlace == null || setting.player?.deinterlace
  );
  const repeat = useSelector<State, string>(
    ({ setting }) => setting.player?.repeat || "continue"
  );
  const networkType = useSelector<State, string>(({ network }) => network.type);
  const pause = useSelector<State, boolean>(({ player }) => player.pause);
  const duration = useSelector<State, number>(({ player }) => player.duration);
  const time = useSelector<State, number>(({ player }) => player.time);
  const audioTrack = useSelector<State, number>(
    ({ player }) => player.track.audio || 0
  );
  const dualMonoMode = useSelector<State, string>(
    ({ player }) => player.dualMonoMode
  );
  const seekTime = useSelector<State, number>(({ player }) => player.seekTime);
  const seekPosition = useSelector<State, number>(
    ({ player }) => player.seekPosition
  );
  const programs = useSelector<State, ViewerProgram[]>(
    ({ viewer }) => viewer.programs
  );
  const index = useSelector<State, number>(({ viewer }) => viewer.index);
  const extraIndex = useSelector<State, number>(
    ({ viewer: { extraIndex } }) => extraIndex
  );
  const peakPlay = useSelector<State, boolean>(({ viewer }) => viewer.peakPlay);

  const [bootstrap, setBootstrap] = useState(false);
  const [reset, setReset] = useState(false);
  const [ss, setStartSeconds] = useState(0);

  const recordedProgram = useMemo(
    () => programs[index].recorded?.[extraIndex] || programs[index],
    [programs, index, extraIndex]
  );
  const uri = useMemo(() => {
    let [uri, query] = recordedProgram.stream.split("?");
    if (recordedProgram.type !== "file") {
      if (
        networkType.indexOf("cell") >= 0 &&
        (type === "chinachu" || type === "epgstation")
      ) {
        uri = uri.replace(/[^.]+$/, mobileStreamType);
        query = mobileStreamParams;
      }
      if (type === "chinachu") {
        uri = uri.replace(/m2ts$/, "mp4");
      }
    }
    if (ss > 0) {
      query = `${qs.stringify({ ...qs.parse(query), ss })}`;
    }
    if (query) {
      uri += `?${query}`;
    }

    return uri;
  }, [mobileStreamType, mobileStreamParams, recordedProgram, networkType, ss]);
  const initOptions = useMemo(() => {
    const options = [
      "--file-caching=5000",
      "--live-caching=10000",
      "--network-caching=10000"
    ];
    if (deinterlace) {
      options.push("--deinterlace=1", "--deinterlace-mode=discard");
    } else {
      options.push("--deinterlace=0");
    }
    switch (dualMonoMode) {
      case "both":
        options.push("--stereo-mode=1");
        break;
      case "main":
        options.push("--stereo-mode=3");
        break;
      case "sub":
        options.push("--stereo-mode=4");
        break;
    }
{'fraction_non_alphanumeric': 0.1588852696344553, 'fraction_numerical': 0.00886717336228737, 'mean_word_length': 3.467663702506063, 'pattern_counts': {'":': 6, '<': 49, '<?xml version=': 0, '>': 101, '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': '2234780', 'n_tokens_mistral': 3414, 'n_tokens_neox': 3315, 'n_words': 863}