summaryrefslogtreecommitdiff
path: root/extensions/TimedMediaHandler/MwEmbedModules/EmbedPlayer/resources/mw.EmbedPlayerVlc.js
blob: c668cf4075fe193a6590c387fe370201335e39ba (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
/*
* VLC embed based on: http://people.videolan.org/~damienf/plugin-0.8.6.html
* javascript api: http://www.videolan.org/doc/play-howto/en/ch04.html
*  assume version > 0.8.5.1
*/
( function( mw, $ ) { "use strict";

mw.EmbedPlayerVlc = {

	//Instance Name:
	instanceOf : 'Vlc',

	//What the vlc player / plug-in supports:
	supports : {
		'playHead':true,
		'pause':true,
		'stop':true,
		'fullscreen':true,
		'timeDisplay':true,
		'volumeControl':true,

		'playlist_driver':true, // if the object supports playlist functions
		'overlay':false
	},

	// The previous state of the player instance
	prevState : 0,

	// Counter for waiting for vlc embed to be ready
	waitForVlcCount:0,

	// Store the current play time for vlc
	vlcCurrentTime: 0,

	/**
	* Get embed HTML
	*/
	embedPlayerHTML: function() {
		var _this = this;
		$( this ).html(
			'<object classid="clsid:9BE31822-FDAD-461B-AD51-BE1D1C159921" ' +
				'codebase="http://downloads.videolan.org/pub/videolan/vlc/latest/win32/axvlc.cab#Version=0,8,6,0" ' +
				'id="' + this.pid + '" events="True" height="' + this.getPlayerHeight() + '" width="' + this.getPlayerWidth() + '"' +
				'>' +
					'<param name="MRL" value="">' +
					'<param name="ShowDisplay" value="True">' +
					'<param name="AutoLoop" value="False">' +
					'<param name="AutoPlay" value="False">' +
					'<param name="Volume" value="50">' +
					'<param name="StartTime" value="0">' +
					'<embed pluginspage="http://www.videolan.org" type="application/x-vlc-plugin" ' +
						'progid="VideoLAN.VLCPlugin.2" name="' + this.pid + '" ' +
						'height="' + this.getHeight() + '" width="' + this.getWidth() + '" ' +
						// Set the style for IE layout issues )
						'style="width:' + this.getWidth() + 'px;height:' + this.getHeight() + 'px;" ' +
					'>' +
			'</object>'
		)
		// Call postEmbedActions directly ( postEmbedJs will wait for player ready )
		_this.postEmbedActions();
	},

	/**
	* Javascript to run post vlc embedding
	* Inserts the requested src to the embed instance
	*/
	postEmbedActions: function() {
		var _this = this;
		// Load a pointer to the vlc into the object (this.playerElement)
		this.getPlayerElement();
		if ( this.playerElement && this.playerElement.playlist) {
			// manipulate the dom object to make sure vlc has the correct size:
			this.playerElement.style.width = this.getWidth();
			this.playerElement.style.height = this.getHeight();
			this.playerElement.playlist.items.clear();

			// VLC likes absolute urls:
			var src = mw.absoluteUrl( this.getSrc() ) ;

			mw.log( "EmbedPlayerVlc:: postEmbedActions play src:" + src );
			var itemId = this.playerElement.playlist.add( src );
			if ( itemId != -1 ) {
				// Play
				this.playerElement.playlist.playItem( itemId );
			} else {
				mw.log( "Error:: EmbedPlayerVlc can not play" );
			}
			setTimeout( function() {
				_this.monitor();
			}, 100 );
		} else {
			mw.log( 'EmbedPlayerVlc:: postEmbedActions: vlc not ready' );
			this.waitForVlcCount++;
			if ( this.waitForVlcCount < 10 ) {
				setTimeout( function() {
					_this.postEmbedActions();
				}, 100 );
			} else {
				mw.log( 'EmbedPlayerVlc:: vlc never ready' );
			}
		}
	},

	/**
	* Handles seek requests based on temporal media source type support
	*
	* @param {Float} percent Seek to this percent of the stream
	*/
	seek : function( percent ) {
		this.getPlayerElement();
		// Use the parent (re) embed with new seek url method if urlTimeEncoding is supported.
		if ( this.supportsURLTimeEncoding() ) {
			this.parent_seek( percent );
		} else if ( this.playerElement ) {
			this.seeking = true;
			mw.log( "EmbedPlayerVlc:: seek to: " + percent )
			if ( ( this.playerElement.input.state == 3 ) && ( this.playerElement.input.position != percent ) )
			{
				this.playerElement.input.position = percent;
				this.controlBuilder.setStatus( mw.msg('mwe-embedplayer-seeking') );
			}
		} else {
			this.doPlayThenSeek( percent );
		}
		this.parent_monitor();
	},

	/**
	* Issues a play request then seeks to a given time
	*
	* @param {Float} percent Seek to this percent of the stream after playing
	*/
	doPlayThenSeek:function( percent ) {
		mw.log( 'EmbedPlayerVlc:: doPlayThenSeek' );
		var _this = this;
		this.play();
		var seekCount = 0;
		var readyForSeek = function() {
			_this.getPlayerElement();
			var newState = _this.playerElement.input.state;
			// If playing we are ready to do the seek
			if ( newState == 3 ) {
				_this.seek( percent );
			} else {
				// Try to get player for 10 seconds:
				if ( seekCount < 200 ) {
					setTimeout( readyForSeek, 50 );
					rfsCount++;
				} else {
					mw.log( 'Error: EmbedPlayerVlc doPlayThenSeek failed' );
				}
			}
		}
		readyForSeek();
	},

	/**
	* Updates the status time and player state
	*/
	monitor: function() {
		this.getPlayerElement();
		if ( ! this.playerElement ){
			return ;
		}
		// Try to get relay vlc log messages to the console.
		try{
			if ( this.playerElement.log.messages.count > 0 ) {
				// there is one or more messages in the log
				var iter = this.playerElement.log.messages.iterator();
				while ( iter.hasNext ) {
					var msg = iter.next();
					var msgtype = msg.type.toString();
					if ( ( msg.severity == 1 ) && ( msgtype == "input" ) )
					{
						mw.log( msg.message );
					}
				}
				// clear the log once finished to avoid clogging
				this.playerElement.log.messages.clear();
			}

			var newState = this.playerElement.input.state;
			if ( this.prevState != newState ) {
				if ( newState == 0 )
				{
					// current media has stopped
					this.onStop();
				}
				else if ( newState == 1 )
				{
					// current media is opening/connecting
					this.onOpen();
				}
				else if ( newState == 2 )
				{
					// current media is buffering data
					this.onBuffer();
				}
				else if ( newState == 3 )
				{
					// current media is now playing
					this.onPlay();
				}
				else if ( this.playerElement.input.state == 4 ) {
					// current media is now paused
					this.onPause();
				}
				this.prevState = newState;
			} else if ( newState == 3 ) {
				// current media is playing
				this.onPlaying();
			}
		} catch( e ){
			mw.log("EmbedPlayerVlc:: Monitor error");
		}
		// Update the status and check timer via universal parent monitor
		this.parent_monitor();
	},

	/**
	* Events:
	*/
	onOpen: function() {
		// Open is considered equivalent to other players buffer status:
		this.controlBuilder.setStatus( mw.msg('mwe-embedplayer-buffering') );
	},
	onBuffer: function() {
		this.controlBuilder.setStatus(  mw.msg('mwe-embedplayer-buffering') );
	},
	onPlay: function() {
		this.onPlaying();
	},
	onPlaying: function() {
		this.seeking = false;
		// For now trust the duration from url over vlc input.length
		if ( !this.getDuration() && this.playerElement.input.length > 0 )
		{
			// mw.log('setting duration to ' + this.playerElement.input.length /1000);
			this.duration = this.playerElement.input.length / 1000;
		}
		this.vlcCurrentTime = this.playerElement.input.time / 1000;
	},

	/**
	* Get the embed player time
	*/
	getPlayerElementTime: function(){
		return this.vlcCurrentTime;
	},

	onPause: function() {
		// Update the interface if paused via native plugin
		this.parent_pause();
	},
	onStop: function() {
		mw.log( 'EmbedPlayerVlc:: onStop' );
		if ( !this.seeking ){
			this.onClipDone();
		}
	},

	/**
	* Handles play requests
	*/
	play: function() {
		mw.log( 'EmbedPlayerVlc:: play' );
		// Update the interface
		this.parent_play();
		if ( this.getPlayerElement() ) {
			// plugin is already being present send play call:
			// clear the message log and enable error logging
			if ( this.playerElement.log ) {
				this.playerElement.log.messages.clear();
			}
			if ( this.playerElement.playlist && typeof this.playerElement.playlist.play == 'function')
				this.playerElement.playlist.play();

			if( typeof this.playerElement.play == 'function' )
				this.playerElement.play();

			this.paused = false;

			// re-start the monitor:
			this.monitor();
		}
	},

	/**
	* Passes the Pause request to the plugin.
	* calls parent "pause" to update interface
	*/
	pause: function() {
		this.parent_pause(); // update the interface if paused via native control
		if ( this.getPlayerElement() ) {
			try{
				this.playerElement.playlist.togglePause();
			} catch( e ){
				mw.log("EmbedPlayerVlc:: Could not pause video " + e);
			}
		}
	},

	/**
	* Mutes the video
	* calls parent "toggleMute" to update interface
	*/
	toggleMute:function() {
		this.parent_toggleMute();
		if ( this.getPlayerElement() ){
			this.playerElement.audio.toggleMute();
		}
	},

	/**
	* Update the player volume
	* @parm {Float} percent Percent of total volume
	*/
	setPlayerElementVolume: function ( percent ) {
		if ( this.getPlayerElement() && this.playerElement.audio ) {
			this.playerElement.audio.volume = percent * 100;
		}
	},

	/**
	* Gets the current volume
	* @return {Float} percent percent of total volume
	*/
	getVolumen:function() {
		if ( this.getPlayerElement() ){
			return this.playerElement.audio.volume / 100;
		}
	},

	/**
	* Passes fullscreen request to plugin
	*/
	fullscreen : function() {
		if ( this.playerElement ) {
			if ( this.playerElement.video ){
				try{
					this.playerElement.video.toggleFullscreen();
				} catch ( e ){
					mw.log("EmbedPlayerVlc:: toggle fullscreen : possible error: " + e);
				}
			}
		}
	},

	/**
	* Get the embed vlc object
	*/
	getPlayerElement : function() {
		this.playerElement = $( '#' + this.pid )[0];
		return this.playerElement;
	}
};

} )( mediaWiki, jQuery );