| 1 | /** |
|---|
| 2 | * Wrapper for load and playback of Youtube videos through their API. |
|---|
| 3 | **/ |
|---|
| 4 | package com.longtailvideo.jwplayer.media { |
|---|
| 5 | import com.jeroenwijering.events.*; |
|---|
| 6 | import com.longtailvideo.jwplayer.events.MediaEvent; |
|---|
| 7 | import com.longtailvideo.jwplayer.model.PlayerConfig; |
|---|
| 8 | import com.longtailvideo.jwplayer.model.PlaylistItem; |
|---|
| 9 | import com.longtailvideo.jwplayer.player.PlayerState; |
|---|
| 10 | import com.longtailvideo.jwplayer.utils.RootReference; |
|---|
| 11 | |
|---|
| 12 | import flash.display.Loader; |
|---|
| 13 | import flash.events.*; |
|---|
| 14 | import flash.net.LocalConnection; |
|---|
| 15 | import flash.net.URLRequest; |
|---|
| 16 | import flash.system.Security; |
|---|
| 17 | |
|---|
| 18 | |
|---|
| 19 | public class YouTubeMediaProvider extends MediaProvider { |
|---|
| 20 | /** Loader for loading the YouTube proxy **/ |
|---|
| 21 | private var loader:Loader; |
|---|
| 22 | /** 'Unique' string to use for proxy connection. **/ |
|---|
| 23 | private var unique:String; |
|---|
| 24 | /** Connection towards the YT proxy. **/ |
|---|
| 25 | private var outgoing:LocalConnection; |
|---|
| 26 | /** connection from the YT proxy. **/ |
|---|
| 27 | private var inbound:LocalConnection; |
|---|
| 28 | /** Save that a load call has been sent. **/ |
|---|
| 29 | private var loading:Boolean; |
|---|
| 30 | /** Save the connection state. **/ |
|---|
| 31 | private var connected:Boolean; |
|---|
| 32 | |
|---|
| 33 | |
|---|
| 34 | /** Setup YouTube connections and load proxy. **/ |
|---|
| 35 | public function YouTubeMediaProvider() { |
|---|
| 36 | } |
|---|
| 37 | |
|---|
| 38 | |
|---|
| 39 | public override function initializeMediaProvider(cfg:PlayerConfig):void { |
|---|
| 40 | super.initializeMediaProvider(cfg); |
|---|
| 41 | _provider = 'youtube'; |
|---|
| 42 | Security.allowDomain('*'); |
|---|
| 43 | outgoing = new LocalConnection(); |
|---|
| 44 | outgoing.allowDomain('*'); |
|---|
| 45 | outgoing.allowInsecureDomain('*'); |
|---|
| 46 | outgoing.addEventListener(StatusEvent.STATUS, onLocalConnectionStatusChange); |
|---|
| 47 | inbound = new LocalConnection(); |
|---|
| 48 | inbound.allowDomain('*'); |
|---|
| 49 | inbound.allowInsecureDomain('*'); |
|---|
| 50 | inbound.addEventListener(StatusEvent.STATUS, onLocalConnectionStatusChange); |
|---|
| 51 | inbound.client = this; |
|---|
| 52 | loader = new Loader(); |
|---|
| 53 | loader.contentLoaderInfo.addEventListener(IOErrorEvent.IO_ERROR, errorHandler); |
|---|
| 54 | } |
|---|
| 55 | |
|---|
| 56 | |
|---|
| 57 | /** Catch load errors. **/ |
|---|
| 58 | private function errorHandler(evt:ErrorEvent):void { |
|---|
| 59 | stop(); |
|---|
| 60 | sendMediaEvent(MediaEvent.JWPLAYER_MEDIA_ERROR, {message: evt.text}); |
|---|
| 61 | } |
|---|
| 62 | |
|---|
| 63 | |
|---|
| 64 | /** xtract the current ID from a youtube URL **/ |
|---|
| 65 | private function getID(url:String):String { |
|---|
| 66 | var arr:Array = url.split('?'); |
|---|
| 67 | var str:String = ''; |
|---|
| 68 | for (var i:String in arr) { |
|---|
| 69 | if (arr[i].substr(0, 2) == 'v=') { |
|---|
| 70 | str = arr[i].substr(2); |
|---|
| 71 | } |
|---|
| 72 | } |
|---|
| 73 | if (str == '') { |
|---|
| 74 | str = url.substr(url.indexOf('/v/') + 3); |
|---|
| 75 | } |
|---|
| 76 | if (str.indexOf('&') > -1) { |
|---|
| 77 | str = str.substr(0, str.indexOf('&')); |
|---|
| 78 | } |
|---|
| 79 | return str; |
|---|
| 80 | } |
|---|
| 81 | |
|---|
| 82 | |
|---|
| 83 | /** Get the location of yt.swf. **/ |
|---|
| 84 | private function getLocation():String { |
|---|
| 85 | var loc:String; |
|---|
| 86 | var url:String = RootReference.stage.loaderInfo.url; |
|---|
| 87 | if (url.indexOf('http://') == 0) { |
|---|
| 88 | unique = Math.random().toString().substr(2); |
|---|
| 89 | loc = url.substr(0, url.indexOf('.swf')); |
|---|
| 90 | loc = loc.substr(0, loc.lastIndexOf('/') + 1) + 'yt.swf?unique=' + unique; |
|---|
| 91 | } else { |
|---|
| 92 | unique = '1'; |
|---|
| 93 | loc = 'yt.swf'; |
|---|
| 94 | } |
|---|
| 95 | return loc; |
|---|
| 96 | } |
|---|
| 97 | |
|---|
| 98 | |
|---|
| 99 | /** Load the YouTube movie. **/ |
|---|
| 100 | override public function load(itm:PlaylistItem):void { |
|---|
| 101 | _item = itm; |
|---|
| 102 | _position = 0; |
|---|
| 103 | loading = true; |
|---|
| 104 | if (connected) { |
|---|
| 105 | if (outgoing) { |
|---|
| 106 | var gid:String = getID(_item.file); |
|---|
| 107 | outgoing.send('AS3_' + unique, "cueVideoById", gid, _item.start); |
|---|
| 108 | resize(_config.width, _config.width / 4 * 3); |
|---|
| 109 | media = loader; |
|---|
| 110 | sendMediaEvent(MediaEvent.JWPLAYER_MEDIA_LOADED); |
|---|
| 111 | _config.mute == true ? setVolume(0) : setVolume(_config.volume); |
|---|
| 112 | setState(PlayerState.BUFFERING); |
|---|
| 113 | sendMediaEvent(MediaEvent.JWPLAYER_MEDIA_BUFFER_FULL); |
|---|
| 114 | } |
|---|
| 115 | } else { |
|---|
| 116 | loader.load(new URLRequest(getLocation())); |
|---|
| 117 | inbound.connect('AS2_' + unique); |
|---|
| 118 | } |
|---|
| 119 | } |
|---|
| 120 | |
|---|
| 121 | |
|---|
| 122 | /** Pause the YouTube movie. **/ |
|---|
| 123 | override public function pause():void { |
|---|
| 124 | outgoing.send('AS3_' + unique, "pauseVideo"); |
|---|
| 125 | super.pause(); |
|---|
| 126 | } |
|---|
| 127 | |
|---|
| 128 | |
|---|
| 129 | /** Play or pause the video. **/ |
|---|
| 130 | override public function play():void { |
|---|
| 131 | outgoing.send('AS3_' + unique, "playVideo"); |
|---|
| 132 | super.play(); |
|---|
| 133 | } |
|---|
| 134 | |
|---|
| 135 | |
|---|
| 136 | /** SWF loaded; add it to the tree **/ |
|---|
| 137 | public function onSwfLoadComplete():void { |
|---|
| 138 | connected = true; |
|---|
| 139 | if (loading) { |
|---|
| 140 | load(_item); |
|---|
| 141 | } |
|---|
| 142 | } |
|---|
| 143 | |
|---|
| 144 | |
|---|
| 145 | /** error was thrown without this handler **/ |
|---|
| 146 | public function onLocalConnectionStatusChange(evt:StatusEvent):void { |
|---|
| 147 | // sendMediaEvent(MediaEvent.JWPLAYER_MEDIA_META,{status:evt.code}); |
|---|
| 148 | } |
|---|
| 149 | |
|---|
| 150 | |
|---|
| 151 | /** Catch youtube errors. **/ |
|---|
| 152 | public function onError(erc:Number):void { |
|---|
| 153 | stop(); |
|---|
| 154 | var msg:String = 'Video not found or deleted: ' + getID(item['file']); |
|---|
| 155 | if (erc == 101 || erc == 150) { |
|---|
| 156 | msg = 'Embedding this video is disabled by its owner.'; |
|---|
| 157 | } |
|---|
| 158 | sendMediaEvent(MediaEvent.JWPLAYER_MEDIA_ERROR, {message: msg}); |
|---|
| 159 | } |
|---|
| 160 | |
|---|
| 161 | |
|---|
| 162 | /** Catch youtube state changes. **/ |
|---|
| 163 | public function onStateChange(stt:Number):void { |
|---|
| 164 | switch (Number(stt)) { |
|---|
| 165 | case -1: |
|---|
| 166 | // setState(PlayerState.IDLE); |
|---|
| 167 | break; |
|---|
| 168 | case 0: |
|---|
| 169 | if (_config.state != PlayerState.BUFFERING && _config.state != PlayerState.IDLE) { |
|---|
| 170 | setState(PlayerState.IDLE); |
|---|
| 171 | sendMediaEvent(MediaEvent.JWPLAYER_MEDIA_COMPLETE); |
|---|
| 172 | } |
|---|
| 173 | break; |
|---|
| 174 | case 1: |
|---|
| 175 | super.play(); |
|---|
| 176 | break; |
|---|
| 177 | case 2: |
|---|
| 178 | super.pause(); |
|---|
| 179 | break; |
|---|
| 180 | case 3: |
|---|
| 181 | setState(PlayerState.BUFFERING); |
|---|
| 182 | break; |
|---|
| 183 | } |
|---|
| 184 | } |
|---|
| 185 | |
|---|
| 186 | |
|---|
| 187 | /** Catch Youtube load changes **/ |
|---|
| 188 | public function onLoadChange(ldd:Number, ttl:Number, off:Number):void { |
|---|
| 189 | sendBufferEvent(ldd / ttl * 100); |
|---|
| 190 | } |
|---|
| 191 | |
|---|
| 192 | |
|---|
| 193 | /** Catch Youtube _position changes **/ |
|---|
| 194 | public function onTimeChange(pos:Number, dur:Number):void { |
|---|
| 195 | sendMediaEvent(MediaEvent.JWPLAYER_MEDIA_TIME, {position: pos, duration: dur}); |
|---|
| 196 | if (item.duration < 0) { |
|---|
| 197 | item.duration = dur; |
|---|
| 198 | } |
|---|
| 199 | } |
|---|
| 200 | |
|---|
| 201 | |
|---|
| 202 | /** Resize the YT player. **/ |
|---|
| 203 | public override function resize(wid:Number, hei:Number):void { |
|---|
| 204 | outgoing.send('AS3_' + unique, "setSize", wid, hei); |
|---|
| 205 | } |
|---|
| 206 | |
|---|
| 207 | |
|---|
| 208 | /** Seek to _position. **/ |
|---|
| 209 | override public function seek(pos:Number):void { |
|---|
| 210 | outgoing.send('AS3_' + unique, "seekTo", pos); |
|---|
| 211 | play(); |
|---|
| 212 | } |
|---|
| 213 | |
|---|
| 214 | |
|---|
| 215 | /** Destroy the youtube video. **/ |
|---|
| 216 | override public function stop():void { |
|---|
| 217 | if (connected) { |
|---|
| 218 | outgoing.send('AS3_' + unique, "stopVideo"); |
|---|
| 219 | } else { |
|---|
| 220 | loading = false; |
|---|
| 221 | } |
|---|
| 222 | super.stop(); |
|---|
| 223 | } |
|---|
| 224 | |
|---|
| 225 | |
|---|
| 226 | /** Set the volume level. **/ |
|---|
| 227 | override public function setVolume(pct:Number):void { |
|---|
| 228 | outgoing.send('AS3_' + unique, "setVolume", pct); |
|---|
| 229 | } |
|---|
| 230 | } |
|---|
| 231 | } |
|---|