PageRenderTime 334ms CodeModel.GetById 8ms RepoModel.GetById 0ms app.codeStats 0ms

/plugins/pseudostreaming/src/actionscript/org/flowplayer/pseudostreaming/DefaultSeekDataStore.as

https://github.com/longlho/flowplayer
ActionScript | 98 lines | 69 code | 16 blank | 13 comment | 12 complexity | d9c625f2a06cc76fd4de2b916af546f2 MD5 | raw file
Possible License(s): MIT, BSD-3-Clause
  1. /*
  2. * This file is part of Flowplayer, http://flowplayer.org
  3. *
  4. * By: Anssi Piirainen, <support@flowplayer.org>
  5. * Copyright (c) 2008-2011 Flowplayer Oy
  6. * H.264 support by: Arjen Wagenaar, <h264@code-shop.com>
  7. * Copyright (c) 2009 CodeShop B.V.
  8. *
  9. * Released under the MIT License:
  10. * http://www.opensource.org/licenses/mit-license.php
  11. */
  12. package org.flowplayer.pseudostreaming {
  13. import org.flowplayer.util.Log;
  14. import org.flowplayer.model.ClipEventType;
  15. import org.flowplayer.model.Clip;
  16. public class DefaultSeekDataStore {
  17. protected var log:Log = new Log(this);
  18. protected var _keyFrameTimes:Array;
  19. protected var _keyFrameFilePositions:Array;
  20. private var _prevSeekTime:Number = 0;
  21. private function init(clip:Clip, metaData:Object):void {
  22. if (! metaData) return;
  23. log.debug("will extract keyframe metadata");
  24. try {
  25. _keyFrameTimes = extractKeyFrameTimes(metaData);
  26. _keyFrameFilePositions = extractKeyFrameFilePositions(metaData);
  27. } catch (e:Error) {
  28. log.error("error getting keyframes " + e.message);
  29. clip.dispatch(ClipEventType.ERROR, e.message);
  30. }
  31. // log.info("_keyFrameTimes array lenth is " + (_keyFrameTimes ? _keyFrameTimes.length+"" : "null array"));
  32. // log.info("_keyFrameFilePositions array lenth is " + (_keyFrameFilePositions ? _keyFrameFilePositions.length+"" : "null array"));
  33. }
  34. public static function create(clip:Clip, metaData:Object):DefaultSeekDataStore {
  35. var log:Log = new Log("org.flowplayer.pseudostreaming::DefaultKeyFrameStore");
  36. log.debug("extracting keyframe times and filepositions");
  37. var store:DefaultSeekDataStore = metaData.seekpoints ? new H264SeekDataStore() : new FLVSeekDataStore();
  38. store.init(clip, metaData);
  39. return store;
  40. }
  41. protected function extractKeyFrameFilePositions(metadata:Object):Array {
  42. return null;
  43. }
  44. protected function extractKeyFrameTimes(metadata:Object):Array {
  45. return null;
  46. }
  47. internal function allowRandomSeek():Boolean {
  48. return _keyFrameTimes != null && _keyFrameTimes.length > 0;
  49. }
  50. internal function get dataAvailable():Boolean {
  51. return _keyFrameTimes != null;
  52. }
  53. public function getQueryStringStartValue(seekPosition: Number, rangeBegin:Number = 0, rangeEnd:Number = undefined):Number {
  54. if (!rangeEnd) {
  55. rangeEnd = _keyFrameTimes.length - 1;
  56. }
  57. if (rangeBegin == rangeEnd || rangeEnd - rangeBegin == 1) {
  58. _prevSeekTime =_keyFrameTimes[rangeBegin];
  59. return queryParamValue(rangeBegin);
  60. }
  61. var rangeMid:Number = Math.floor((rangeEnd + rangeBegin)/2);
  62. if (_keyFrameTimes[rangeMid] >= seekPosition) {
  63. return getQueryStringStartValue(seekPosition, rangeBegin, rangeMid);
  64. } else {
  65. var offset:Number = (rangeEnd - rangeMid) == 1 ? 0 : 1;
  66. return getQueryStringStartValue(seekPosition, rangeMid + offset, rangeEnd);
  67. }
  68. }
  69. protected function queryParamValue(pos:Number):Number {
  70. return _keyFrameFilePositions[pos];
  71. }
  72. public function reset():void {
  73. _prevSeekTime = 0;
  74. }
  75. public function inBufferSeekTarget(target:Number):Number {
  76. return Math.max(target - _prevSeekTime, 0);
  77. }
  78. public function currentPlayheadTime(time:Number, start:Number):Number {
  79. return (time - start) + _prevSeekTime;
  80. }
  81. }
  82. }