<commit_before>/* This Source Code Form is subject to the terms of the Mozilla Public
 * License, v. 2.0. If a copy of the MPL was not distributed with this
 * file, You can obtain one at http://mozilla.org/MPL/2.0/. */
 package org.mangui.hls.controller {
    import flash.events.Event;
    import flash.events.TimerEvent;
    import flash.utils.getTimer;
    import flash.utils.Timer;
    import org.mangui.hls.constant.HLSPlayStates;
    import org.mangui.hls.event.HLSEvent;
    import org.mangui.hls.HLS;
    CONFIG::LOGGING {
        import org.mangui.hls.utils.Log;
    }
    /*
     * class that control/monitor FPS
     */
    public class FPSController {
      /** Reference to the HLS controller. **/
      private var _hls : HLS;
      private var _timer : Timer;
      private var _lastTimer : int;
      private var _lastdroppedFrames : int;
      private var _hiddenVideo : Boolean;

      public function FPSController(hls : HLS) {
          _hls = hls;
          _hls.addEventListener(HLSEvent.PLAYBACK_STATE, _playbackStateHandler);
          _timer = new Timer(50,0);
          _timer.addEventListener(TimerEvent.TIMER, _checkFPS);
      }

      public function dispose() : void {
          _hls.removeEventListener(HLSEvent.PLAYBACK_STATE, _playbackStateHandler);
      }

      private function _playbackStateHandler(event : HLSEvent) : void {
        switch(event.state) {
          case HLSPlayStates.PLAYING:
            // start fps check timer when switching to playing state
            _lastTimer = 0;
            _hiddenVideo = true;
            _timer.start();
            break;
          default:
            if(_timer.running)  {
              // stop it in all other cases
              _lastTimer = 0;
              _hiddenVideo = true;
              _timer.stop();
                CONFIG::LOGGING {
                  Log.info("video not playing, stop monitoring dropped FPS");
                }
            }
            break;
        }
      };

      private function _checkFPS(e : Event) : void {
        var newTimer : int = getTimer();
        if(_lastTimer) {
          var delta:int = newTimer - _lastTimer;
          /* according to http://www.kaourantin.net/2010/03/timing-it-right.html, when player is hidden, Flash timer only runs at 8Hz.
             here we armed our timer to 50ms, if delta time between 2 runs is more than 100ms, consider that our player is hidden ...
          */
          the idea here is
          if(delta && delta < 100) {
            if(_hiddenVideo == false) {
              var deltaDroppedFrames : int = _hls.stream.info.droppedFrames - _lastdroppedFrames;
              var dropFPS : Number = 1000*deltaDroppedFrames/delta;
              if(dropFPS > 1) {
                CONFIG::LOGGING {
                  Log.warn("!!! display/dropped FPS > 1, dispatch event:" + _hls.stream.currentFPS.toFixed(2) + "/" + dropFPS.toFixed(2));
                  _hls.dispatchEvent(new HLSEvent(HLSEvent.FPS_DROP, _hls.currentLevel));<commit_msg>explain how "is player visible" heuristics works<commit_after>/* This Source Code Form is subject to the terms of the Mozilla Public
 * License, v. 2.0. If a copy of the MPL was not distributed with this
 * file, You can obtain one at http://mozilla.org/MPL/2.0/. */
 package org.mangui.hls.controller {
    import flash.events.Event;
    import flash.events.TimerEvent;
    import flash.utils.getTimer;
    import flash.utils.Timer;
    import org.mangui.hls.constant.HLSPlayStates;
    import org.mangui.hls.event.HLSEvent;
    import org.mangui.hls.HLS;
    CONFIG::LOGGING {
        import org.mangui.hls.utils.Log;
    }
    /*
     * class that control/monitor FPS
     */
    public class FPSController {
      /** Reference to the HLS controller. **/
      private var _hls : HLS;
      private var _timer : Timer;
      private var _lastTimer : int;
      private var _lastdroppedFrames : int;
      private var _hiddenVideo : Boolean;

      public function FPSController(hls : HLS) {
          _hls = hls;
          _hls.addEventListener(HLSEvent.PLAYBACK_STATE, _playbackStateHandler);
          _timer = new Timer(50,0);
          _timer.addEventListener(TimerEvent.TIMER, _checkFPS);
      }

      public function dispose() : void {
          _hls.removeEventListener(HLSEvent.PLAYBACK_STATE, _playbackStateHandler);
      }

      private function _playbackStateHandler(event : HLSEvent) : void {
        switch(event.state) {
          case HLSPlayStates.PLAYING:
            // start fps check timer when switching to playing state
            _lastTimer = 0;
            _hiddenVideo = true;
            _timer.start();
            break;
          default:
            if(_timer.running)  {
              // stop it in all other cases
              _lastTimer = 0;
              _hiddenVideo = true;
              _timer.stop();
                CONFIG::LOGGING {
                  Log.info("video not playing, stop monitoring dropped FPS");
                }
            }
            break;
        }
      };

      private function _checkFPS(e : Event) : void {
        var newTimer : int = getTimer();
        if(_lastTimer) {
          var delta:int = newTimer - _lastTimer;
          /* according to http://www.kaourantin.net/2010/03/timing-it-right.html, when player is hidden, Flash timer only runs at 8Hz.
             here we armed our timer to 50ms, if delta time between 2 runs is more than 100ms, consider that our player is hidden ...
          */
          if(delta && delta < 100) {
            if(_hiddenVideo == false) {
              var deltaDroppedFrames : int = _hls.stream.info.droppedFrames - _lastdroppedFrames;
              var dropFPS : Number = 1000*deltaDroppedFrames/delta;
              if(dropFPS > 1) {
                CONFIG::LOGGING {
                  Log.warn("!!! display/dropped FPS > 1, dispatch event:" + _hls.stream.currentFPS.toFixed(2) + "/" + dropFPS.toFixed(2));
                  _hls.dispatchEvent(new HLSEvent(HLSEvent.FPS_DROP, _hls.currentLevel));
{'fraction_non_alphanumeric': 0.09320066334991708, 'fraction_numerical': 0.010945273631840797, 'mean_word_length': 1.981216015818092, 'pattern_counts': {'":': 0, '<': 5, '<?xml version=': 0, '>': 7, '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': '2486294', 'n_tokens_mistral': 1824, 'n_tokens_neox': 1729, 'n_words': 465}