PageRenderTime 207ms CodeModel.GetById 15ms RepoModel.GetById 1ms app.codeStats 0ms

/lib/common/src/actionscript/org/flowplayer/net/StreamSelectionManager.as

https://github.com/longlho/flowplayer
ActionScript | 139 lines | 105 code | 24 blank | 10 comment | 16 complexity | 13050c61bfc6a19773b30f51207b3326 MD5 | raw file
Possible License(s): MIT, BSD-3-Clause
  1. /*
  2. * This file is part of Flowplayer, http://flowplayer.org
  3. *
  4. * By: Daniel Rossi <electroteque@gmail.com>, Anssi Piirainen <api@iki.fi> Flowplayer Oy
  5. * Copyright (c) 2009, 2010 Electroteque Multimedia, Flowplayer Oy
  6. *
  7. * Released under the MIT License:
  8. * http://www.opensource.org/licenses/mit-license.php
  9. */
  10. package org.flowplayer.net {
  11. import org.flowplayer.util.Log;
  12. import org.flowplayer.controller.ClipURLResolver;
  13. import org.flowplayer.view.Flowplayer;
  14. import org.osmf.net.DynamicStreamingItem;
  15. public class StreamSelectionManager implements IStreamSelectionManager {
  16. protected var log:Log = new Log(this);
  17. protected var _streamItems:Vector.<BitrateItem>;
  18. protected var _defaultIndex:Number = -1;
  19. protected var _currentIndex:Number = -1;
  20. protected var _player:Flowplayer;
  21. protected var _resolver:ClipURLResolver;
  22. protected var _previousStreamName:String;
  23. protected var _bitrateResource:BitrateResource;
  24. public function StreamSelectionManager(bitrateResource:BitrateResource, player:Flowplayer, resolver:ClipURLResolver) {
  25. _bitrateResource = bitrateResource;
  26. _streamItems = _bitrateResource.addBitratesToClip(player.playlist.current);
  27. _player = player;
  28. _resolver = resolver;
  29. findDefaultStream();
  30. _currentIndex = _defaultIndex;
  31. }
  32. public function get bitrates():Vector.<BitrateItem> {
  33. return _streamItems;
  34. }
  35. public function get bitrateResource():BitrateResource {
  36. return _bitrateResource;
  37. }
  38. private function findDefaultStream():void {
  39. _defaultIndex = -1;
  40. for (var i:Number = 0; i < _streamItems.length; i++) {
  41. if (_streamItems[i]["isDefault"]) {
  42. _defaultIndex = i;
  43. break;
  44. }
  45. }
  46. if (_defaultIndex == -1) {
  47. //fix for #241 lowest item is the first index not the last once ordered.
  48. _defaultIndex = 0;
  49. log.debug("findDefaultStream(), did not find a default stream -> using the one with lowest bitrate ");
  50. } else {
  51. log.debug("findDefaultStream(), found default item " + getDefaultStream());
  52. }
  53. }
  54. public function getDefaultStream():BitrateItem {
  55. log.debug("getDefaultStream()");
  56. return _streamItems[_defaultIndex];
  57. }
  58. public function getStreamIndex(bitrate:Number):Number {
  59. for (var i:Number = _streamItems.length - 1; i >= 0; i--) {
  60. var item:BitrateItem = _streamItems[i];
  61. if (item.bitrate == bitrate) {
  62. return i;
  63. break;
  64. }
  65. }
  66. return -1;
  67. }
  68. public function getStream(bitrate:Number):BitrateItem {
  69. var index:Number = getStreamIndex(bitrate);
  70. if (index == -1) return getDefaultStream();
  71. return _streamItems[index] as BitrateItem;
  72. }
  73. public function getMappedBitrate(bandwidth:Number = -1):BitrateItem {
  74. if (bandwidth == -1) return getDefaultStream() as BitrateItem;
  75. return getStream(bandwidth) as BitrateItem;
  76. }
  77. public function getItem(index:uint):BitrateItem {
  78. return _streamItems[index];
  79. }
  80. public function get currentIndex():Number {
  81. return _currentIndex;
  82. }
  83. public function set currentIndex(value:Number):void {
  84. _currentIndex = value;
  85. }
  86. public function get currentBitrateItem():BitrateItem {
  87. return _streamItems[_currentIndex];
  88. }
  89. public function set currentBitrateItem(value:BitrateItem):void {
  90. _currentIndex = _streamItems.indexOf(value);
  91. }
  92. public function get streamItems():Vector.<BitrateItem> {
  93. return _streamItems;
  94. }
  95. public function fromName(name:String):BitrateItem {
  96. for (var i:Number = 0; i < _streamItems.length; i++) {
  97. if (_streamItems[i].streamName.indexOf(name) == 0 ||
  98. _streamItems[i].streamName.indexOf("mp4:" + name) == 0) {
  99. return _streamItems[i];
  100. }
  101. }
  102. return null;
  103. }
  104. public function changeStreamNames(mappedBitrate:BitrateItem):void {
  105. var url:String = mappedBitrate.url;
  106. _previousStreamName = _previousStreamName ? _player.currentClip.url : url;
  107. currentBitrateItem = mappedBitrate;
  108. currentIndex = mappedBitrate.index;
  109. _player.currentClip.setResolvedUrl(_resolver, url);
  110. _player.currentClip.setCustomProperty("bitrateResolvedUrl", url);
  111. _player.currentClip.setCustomProperty("mappedBitrate", mappedBitrate);
  112. log.debug("mappedUrl " + url + ", clip.url now " + _player.currentClip.url);
  113. }
  114. }
  115. }