here it is ... the upload-api, script-server, js2 (javascript phase2) branch merge...
[lhc/web/wiklou.git] / js2 / mwEmbed / libEmbedVideo / vlcEmbed.js
1 /*
2 * vlc embed based on: http://people.videolan.org/~damienf/plugin-0.8.6.html
3 * javascript api: http://www.videolan.org/doc/play-howto/en/ch04.html
4 * assume version > 0.8.5.1
5 */
6 var vlcEmbed = {
7 instanceOf:'vlcEmbed',
8 supports: {'play_head':true,
9 'pause':true,
10 'stop':true,
11 'fullscreen':true,
12 'time_display':true,
13 'volume_control':true,
14
15 'playlist_driver':true, //if the object supports playlist functions
16 'overlay':false
17 },
18 //init vars:
19 monitorTimerId : 0,
20 prevState : 0,
21 pejs_count:0, //post embed js count
22
23 getEmbedHTML: function(){
24 //give VLC 150ms to initialize before we start playback
25 //@@todo should be able to do this as an ready event
26 this.pejs_count=0;
27 setTimeout('document.getElementById(\''+this.id+'\').postEmbedJS()', 150);
28 return this.getEmbedObj();
29 },
30 getEmbedObj:function(){
31 var embed_code = '<object classid="clsid:9BE31822-FDAD-461B-AD51-BE1D1C159921" '+
32 'codebase="http://downloads.videolan.org/pub/videolan/vlc/latest/win32/axvlc.cab#Version=0,8,6,0" '+
33 'id="' + this.pid + '" events="True" height="'+this.height+'" width="'+this.width+'"' +
34 '>'+
35 '<param name="MRL" value="">'+
36 '<param name="ShowDisplay" value="True">'+
37 '<param name="AutoLoop" value="False">'+
38 '<param name="AutoPlay" value="False">'+
39 '<param name="Volume" value="50">'+
40 '<param name="StartTime" value="0">'+
41 '<embed pluginspage="http://www.videolan.org" type="application/x-vlc-plugin" '+
42 'progid="VideoLAN.VLCPlugin.2" name="' + this.pid + '" '+
43 'height="'+this.height+'" width="'+this.width+'" ' +
44 //set the style too 'just to be sure'
45 'style="width:'+this.width+'px;height:'+this.height+'px;" '+
46 '>'+
47 '</object>';
48 js_log('embed with: ' + embed_code);
49 return embed_code;
50 },
51
52 /*
53 * some java script to start vlc playback after the embed:
54 */
55 postEmbedJS: function(){
56 //load a pointer to the vlc into the object (this.vlc)
57 this.getVLC();
58 if( this.vlc.log ){
59 //manipulate the dom object to make sure vlc has the correct size:
60 this.vlc.style.width=this.width;
61 this.vlc.style.height=this.height;
62 this.vlc.playlist.items.clear();
63 //@@todo if client supports seeking no need to send seek_offset to URI
64 js_log('vlc play::' + this.getSrc() );
65 var itemId = this.vlc.playlist.add( this.getSrc() );
66 if( itemId != -1 ){
67 //play
68 this.vlc.playlist.playItem(itemId);
69 }else{
70 js_log("error:cannot play at the moment !");
71 }
72 //if controls enabled start up javascript interface and monitor:
73 if(this.controls){
74 //activate the slider: scriptaculus based)
75 //this.activateSlider();
76 //start doing status updates every 1/10th of a second
77 }
78 setTimeout('$j(\'#'+this.id+'\').get(0).monitor()',100);
79 }else{
80 js_log('postEmbedJS:vlc not ready');
81 this.pejs_count++;
82 if( this.pejs_count < 10 ){
83 setTimeout('document.getElementById(\''+this.id+'\').postEmbedJS()',100);
84 }else{
85 js_log('vlc never ready');
86 }
87 }
88 },
89 //disable local seeking (while we don't know what we have avaliable)
90 doSeek : function(perc){
91 if( this.supportsURLTimeEncoding() ){
92 this.parent_doSeek(perc);
93 }else if( this.vlc ) {
94 this.seeking=true;
95 js_log("do vlc http seek to: " + perc)
96 if( (this.vlc.input.state == 3) && (this.vlc.input.position != perc) )
97 {
98 this.vlc.input.position = perc;
99 this.setStatus( 'seeking...' );
100 }
101 }else{
102 this.doPlayThenSeek(perc);
103 }
104 this.parent_monitor();
105 },
106 doPlayThenSeek:function(perc){
107 js_log('doPlayThenSeekHack');
108 var _this = this;
109 this.play();
110 var rfsCount = 0;
111 var readyForSeek = function(){
112 _this.getVLC();
113 var newState = _this.vlc.input.state;
114 //if playing we are ready to do the
115 if(newState==3){
116 _this.doSeek(perc);
117 }else{
118 //try to get player for 10 seconds:
119 if( rfsCount < 200 ){
120 setTimeout(readyForSeek, 50);
121 rfsCount++;
122 }else{
123 js_log('error:doPlayThenSeek failed');
124 }
125 }
126 }
127 readyForSeek();
128 },
129 playMovieAt: function (order){
130 //@@todo add clips to playlist after (order) and then play
131 this.play();
132 },
133 /*
134 * updates the status time
135 */
136 monitor: function(){
137 this.getVLC();
138 if(!this.vlc)
139 return ;
140 if( this.vlc.log ){
141 //js_log( 'state:' + this.vlc.input.state);
142 //js_log('time: ' + this.vlc.input.time);
143 //js_log('pos: ' + this.vlc.input.position);
144 if( this.vlc.log.messages.count > 0 ){
145 // there is one or more messages in the log
146 var iter = this.vlc.log.messages.iterator();
147 while( iter.hasNext ){
148 var msg = iter.next();
149 var msgtype = msg.type.toString();
150 if( (msg.severity == 1) && (msgtype == "input") )
151 {
152 js_log( msg.message );
153 }
154 }
155 // clear the log once finished to avoid clogging
156 this.vlc.log.messages.clear();
157 }
158 var newState = this.vlc.input.state;
159 if( this.prevState != newState ){
160 if( newState == 0 )
161 {
162 // current media has stopped
163 this.onStop();
164 }
165 else if( newState == 1 )
166 {
167 // current media is opening/connecting
168 this.onOpen();
169 }
170 else if( newState == 2 )
171 {
172 // current media is buffering data
173 this.onBuffer();
174 }
175 else if( newState == 3 )
176 {
177 // current media is now playing
178 this.onPlay();
179 }
180 else if( this.vlc.input.state == 4 )
181 {
182 // current media is now paused
183 this.onPause();
184 }
185 this.prevState = newState;
186 }else if( newState == 3 ){
187 // current media is playing
188 this.onPlaying();
189 }
190 }
191 //update the status and check timmer via universal parent monitor
192 this.parent_monitor();
193 },
194 /* events */
195 onOpen: function(){
196 this.setStatus("Opening...");
197 //document.getElementById("info_"+this.id).innerHTML = "Opening...";
198 //document.getElementById("PlayOrPause").disabled = true;
199 //document.getElementById("Stop").disabled = false;
200 },
201 onBuffer: function(){
202 this.setStatus("Buffering...");
203 //document.getElementById("info_"+this.id).innerHTML = "Buffering...";
204 //document.getElementById("PlayOrPause").disabled = true;
205 //document.getElementById("Stop").disabled = false;
206 },
207 onPlay: function(){
208 //document.getElementById("PlayOrPause").value = "Pause";
209 //document.getElementById("PlayOrPause").disabled = false;
210 //document.getElementById("Stop").disabled = false;
211 this.onPlaying();
212 },
213 liveFeedRoll: 0,
214 onPlaying: function(){
215 this.seeking=false;
216 //for now trust the duration from url over vlc input.length
217 if( !this.getDuration() && this.vlc.input.length > 0 )
218 {
219 //js_log('setting duration to ' + this.vlc.input.length /1000);
220 this.duration = this.vlc.input.length /1000;
221 }
222 this.currentTime = this.vlc.input.time/1000;
223 },
224 onPause: function(){
225 this.parent_pause(); //update the inteface if paused via native control
226 },
227 onStop: function(){
228 js_log('vlc:onStop:');
229 if(!this.seeking)
230 this.onClipDone();
231 },
232 /* js hooks/controls */
233 play : function(){
234 js_log('f:vlcPlay');
235 this.getVLC();
236 //call the parent
237 this.parent_play();
238 if( this.vlc ){
239 //plugin is already being present send play call:
240 // clear the message log and enable error logging
241 if( this.vlc.log ){
242 this.vlc.log.messages.clear();
243 }
244 if(this.vlc.playlist)
245 this.vlc.playlist.play();
246
247 this.monitor();
248 this.paused=false;
249 }
250 },
251 stop : function(){
252 if(this.vlc){
253 if(typeof this.vlc != 'undefined' ){
254 if(typeof this.vlc.playlist != 'undefined'){
255 //dont' stop (issues all the plugin-stop actions)
256 //this.vlc.playlist.stop();
257 if( this.monitorTimerId != 0 )
258 {
259 clearInterval(this.monitorTimerId);
260 this.monitorTimerId = 0;
261 }
262 }
263 }
264 }
265 //this.onStop();
266 //do parent stop
267 this.parent_stop();
268 },
269 pause : function(){
270 this.parent_pause(); //update the inteface if paused via native control
271 if( this.vlc ){
272 this.vlc.playlist.togglePause();
273 }
274 },
275 toggleMute:function(){
276 this.parent_toggleMute();
277 this.getVLC();
278 if(this.vlc)
279 this.vlc.audio.toggleMute();
280 },
281 //@@ Suport UpDateVolumen
282 updateVolumen:function(perc){
283 this.getVLC();
284 if(this.vlc)
285 this.vlc.audio.volume = perc*100;
286 },
287 //@@ Get Volumen
288 getVolumen:function(){
289 this.getVLC();
290 if(this.vlc)
291 return this.vlc.audio.volume / 100;
292 },
293 fullscreen : function(){
294 if(this.vlc){
295 if(this.vlc.video)
296 this.vlc.video.toggleFullscreen();
297 }
298 },
299 /* returns current time in float seconds
300 * as per html5 we should just have an attribute by name of CurrentTime
301 * http://www.whatwg.org/specs/web-apps/current-work/#currenttime
302 currentTime : function(){
303 if(typeof this.vlc != 'undefined' ){
304 if(typeof this.vlc.input != 'undefined' ){
305 return this.vlc.input.time/1000;
306 }
307 }
308 return '0';
309 },
310 */
311 // get the embed vlc object
312 getVLC : function(){
313 this.vlc = this.getPluginEmbed();
314 }
315 };
316