/flowplayer/trunk/src/actionscript/org/flowplayer/view/StageVideoWrapper.as

http://flowplayer-core.googlecode.com/ · ActionScript · 188 lines · 123 code · 42 blank · 23 comment · 13 complexity · d93a40bac065984032c0367dbd867e6f MD5 · raw file

  1. /*
  2. * Copyright (c) 2008, 2009 Flowplayer Oy
  3. *
  4. * This file is part of Flowplayer.
  5. *
  6. * Flowplayer is free software: you can redistribute it and/or modify
  7. * it under the terms of the GNU General Public License as published by
  8. * the Free Software Foundation, either version 3 of the License, or
  9. * (at your option) any later version.
  10. *
  11. * Flowplayer is distributed in the hope that it will be useful,
  12. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  13. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  14. * GNU General Public License for more details.
  15. *
  16. * You should have received a copy of the GNU General Public License
  17. * along with Flowplayer. If not, see <http://www.gnu.org/licenses/>.
  18. */
  19. package org.flowplayer.view {
  20. import flash.net.NetStreamPlayOptions;
  21. import flash.net.NetStreamPlayTransitions;
  22. import flash.net.NetStream;
  23. import flash.media.Video;
  24. import flash.media.StageVideo;
  25. import flash.media.StageVideoAvailability;
  26. import flash.geom.Rectangle;
  27. import flash.geom.Point;
  28. import flash.display.Stage;
  29. import flash.events.Event;
  30. import flash.events.StageVideoAvailabilityEvent;
  31. import flash.events.StageVideoEvent;
  32. import org.flowplayer.model.Clip;
  33. import org.flowplayer.model.ClipEventType;
  34. import org.flowplayer.util.Log;
  35. /**
  36. * @author api
  37. */
  38. public class StageVideoWrapper extends Video {
  39. private var _stageVideo:StageVideo;
  40. private var _stage:Stage;
  41. private var _clip:Clip;
  42. private var _netStream:NetStream = null;
  43. private var _hasStageVideo:Boolean = false;
  44. private var _visible:Boolean = true;
  45. private var log:Log = new Log(this);
  46. public function StageVideoWrapper(clip:Clip) {
  47. super();
  48. _clip = clip;
  49. addEventListener(Event.ADDED_TO_STAGE, onAddedToStage);
  50. addEventListener(Event.REMOVED_FROM_STAGE, onRemovedFromStage);
  51. }
  52. private function onAddedToStage(event:Event):void {
  53. _stage = stage;
  54. _stage.addEventListener(StageVideoAvailabilityEvent.STAGE_VIDEO_AVAILABILITY, onAvailabilityChanged);
  55. }
  56. private function onRemovedFromStage(event:Event):void {
  57. _stage.removeEventListener(StageVideoAvailabilityEvent.STAGE_VIDEO_AVAILABILITY, onAvailabilityChanged);
  58. _stage = null;
  59. }
  60. public function get stageVideo():StageVideo {
  61. return hasStageVideo ? _stageVideo : null;
  62. }
  63. public function get hasStageVideo():Boolean {
  64. return _hasStageVideo;
  65. }
  66. private function onAvailabilityChanged(event:StageVideoAvailabilityEvent):void {
  67. log.debug("StageVideo Availability changed: " + event.availability);
  68. var availableNow:Boolean = event.availability == StageVideoAvailability.AVAILABLE;
  69. useStageVideo(availableNow)
  70. }
  71. private function useStageVideo(availableNow:Boolean):void {
  72. log.debug("useStageVideo : "+ availableNow);
  73. _hasStageVideo = availableNow;
  74. if ( _hasStageVideo && _stage.stageVideos.length ) {
  75. _stageVideo = _stage.stageVideos[0];
  76. super.visible = false;
  77. //#503 update viewport when stage is added to obtain the coordnates correctly.
  78. _updateStageVideo();
  79. } else {
  80. super.visible = true;
  81. _hasStageVideo = false;
  82. }
  83. attachNetStream(_netStream);
  84. }
  85. override public function attachNetStream(netStream:NetStream):void {
  86. _netStream = netStream;
  87. if ( hasStageVideo ) {
  88. log.info("Attaching netstream to stageVideo");
  89. stageVideo.attachNetStream(_netStream);
  90. stageVideo.addEventListener(StageVideoEvent.RENDER_STATE, _displayStageVideo);
  91. } else {
  92. log.info("Attaching netstream to video");
  93. super.attachNetStream(_netStream);
  94. if ( _stageVideo != null )
  95. _stageVideo.attachNetStream(null);
  96. visible = _visible;
  97. _clip.dispatch(ClipEventType.STAGE_VIDEO_STATE_CHANGE, stageVideo);
  98. }
  99. }
  100. private function _displayStageVideo(event:StageVideoEvent):void {
  101. //#612 add some logging for the render state
  102. log.debug("Stagevideo Render State: " + event.status);
  103. if(event.status != 'software')
  104. return;
  105. stageVideo.removeEventListener(StageVideoEvent.RENDER_STATE, _displayStageVideo);
  106. super.attachNetStream(null);
  107. visible = _visible;
  108. _clip.dispatch(ClipEventType.STAGE_VIDEO_STATE_CHANGE, stageVideo);
  109. }
  110. override public function get videoWidth():int {
  111. return hasStageVideo ? stageVideo.videoWidth : super.videoWidth;
  112. }
  113. override public function get videoHeight():int {
  114. return hasStageVideo ? stageVideo.videoHeight : super.videoHeight;
  115. }
  116. override public function set visible(value:Boolean):void {
  117. log.debug("set visible " + value);
  118. _visible = value;
  119. if ( hasStageVideo ) {
  120. _updateStageVideo();
  121. super.visible = false;
  122. } else {
  123. super.visible = _visible;
  124. }
  125. }
  126. override public function get visible():Boolean {
  127. return _visible;
  128. }
  129. private function _updateStageVideo():void {
  130. if ( ! hasStageVideo )
  131. return;
  132. var p:Point = localToGlobal(new Point(0, 0));
  133. var r:Rectangle = _visible ? new Rectangle(p.x, p.y, width, height) : new Rectangle(0, 0, 0, 0);
  134. _stageVideo.viewPort = r;
  135. _clip.dispatch(ClipEventType.STAGE_VIDEO_STATE_CHANGE, stageVideo);
  136. }
  137. override public function set width(value:Number):void {
  138. super.width = value;
  139. _updateStageVideo();
  140. }
  141. override public function set height(value:Number):void {
  142. super.height = value;
  143. _updateStageVideo();
  144. }
  145. }
  146. }