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

https://github.com/longlho/flowplayer
ActionScript | 84 lines | 48 code | 21 blank | 15 comment | 8 complexity | 1db03a603f1cc1994c9673258cf124b2 MD5 | raw file
  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 flash.net.NetStream;
  12. import flash.net.NetStreamPlayOptions;
  13. import flash.net.NetStreamPlayTransitions;
  14. import flash.events.NetStatusEvent;
  15. import org.flowplayer.model.ClipEvent;
  16. import org.flowplayer.view.Flowplayer;
  17. import org.flowplayer.util.Log;
  18. public class StreamSwitchManager {
  19. private var _netStream:NetStream;
  20. private var _streamSelectionManager:IStreamSelectionManager;
  21. private var _player:Flowplayer;
  22. private var _previousBitrateItem:BitrateItem;
  23. protected var log:Log = new Log(this);
  24. public function StreamSwitchManager(netStream:NetStream, streamSelectionManager:IStreamSelectionManager, player:Flowplayer) {
  25. _netStream = netStream;
  26. _streamSelectionManager = streamSelectionManager;
  27. _player = player;
  28. }
  29. public function get previousBitrateItem():BitrateItem {
  30. return _previousBitrateItem;
  31. }
  32. public function switchStream(bitrateItem:BitrateItem):void {
  33. _previousBitrateItem = _streamSelectionManager.currentBitrateItem;
  34. _streamSelectionManager.changeStreamNames(bitrateItem);
  35. //#463 Adding switch prevention if the mapped bitrate has not changed.
  36. if (bitrateItem.bitrate == _previousBitrateItem.bitrate) {
  37. log.debug("Mapped bitrate " + bitrateItem.bitrate + " has not changed will not switch");
  38. return;
  39. }
  40. //#404 allow play2 for http streams, will reset correctly.
  41. if (_netStream && _netStream.hasOwnProperty("play2")) {
  42. var netStreamPlayOptions:NetStreamPlayOptions = new NetStreamPlayOptions();
  43. if (_previousBitrateItem) {
  44. netStreamPlayOptions.oldStreamName = _previousBitrateItem.url;
  45. netStreamPlayOptions.transition = NetStreamPlayTransitions.SWITCH;
  46. } else {
  47. netStreamPlayOptions.transition = NetStreamPlayTransitions.RESET;
  48. }
  49. //#370 set the clip start time or else dynamic switching doesn't function correctly.
  50. //#547 don't set the start property unless set, causes problems for live streams.
  51. if (_player.currentClip.start) netStreamPlayOptions.start = _player.currentClip.start;
  52. netStreamPlayOptions.streamName = bitrateItem.url;
  53. //#417 provide previous item name in the logs.
  54. log.debug("calling switchStream with dynamic stream switch support, stream name is " + netStreamPlayOptions.streamName + ", previous stream name: " + _previousBitrateItem.streamName);
  55. //_player.streamProvider.netStream.play2(netStreamPlayOptions);
  56. _player.switchStream(_player.currentClip, netStreamPlayOptions);
  57. } else {
  58. log.debug("calling switchStream, stream name is " + bitrateItem.url);
  59. _player.switchStream(_player.currentClip);
  60. }
  61. }
  62. }
  63. }