summaryrefslogtreecommitdiff
path: root/extensions/TimedMediaHandler/MwEmbedModules/EmbedPlayer/resources/mw.EmbedTypes.js
blob: 21b2f452326fdcdf6c9590884b31ea88e26d5efe (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
359
360
/**
 * mw.EmbedTypes object handles setting and getting of supported embed types:
 * closely mirrors OggHandler so that its easier to share efforts in this area:
 * http://svn.wikimedia.org/viewvc/mediawiki/trunk/extensions/OggHandler/OggPlayer.js
 */
( function( mw, $ ) { "use strict";

/**
 * Setup local players and supported mime types In an ideal world we would query the plugin
 * to get what mime types it supports in practice not always reliable/available
 *
 * We can't cleanly store these values per library since player library is sometimes
 * loaded post player detection
 */
// Flash based players:
var kplayer = new mw.MediaPlayer('kplayer', [
	'video/x-flv',
	'video/h264',
	'video/mp4; codecs="avc1.42E01E"',
	'video/mp4; codecs="avc1.42E01E, mp4a.40.2"',
	'audio/mpeg'
], 'Kplayer');

// Native html5 players
var oggNativePlayer = new mw.MediaPlayer( 'oggNative', [
	'video/ogg',
	'video/ogg; codecs="theora"',
	'video/ogg; codecs="theora, vorbis"',
	'audio/ogg',
	'audio/ogg; codecs="vorbis"',
	'application/ogg'
], 'Native' );
// Native html5 players
var opusNativePlayer = new mw.MediaPlayer( 'opusNative', [
	'audio/ogg; codecs="opus"',
], 'Native' );
var h264NativePlayer = new mw.MediaPlayer( 'h264Native', [
	'video/h264',
	'video/mp4; codecs="avc1.42E01E, mp4a.40.2"'
], 'Native' );
var appleVdnPlayer = new mw.MediaPlayer( 'appleVdn', [
	'application/vnd.apple.mpegurl',
	'application/vnd.apple.mpegurl; codecs="avc1.42E01E"'
], 'Native');
var mp3NativePlayer = new mw.MediaPlayer( 'mp3Native', [
	'audio/mpeg',
	'audio/mp3'
], 'Native' );
var aacNativePlayer = new mw.MediaPlayer( 'aacNative', [
	'audio/mp4',
	'audio/mp4; codecs="mp4a.40.5"'
], 'Native' );
var webmNativePlayer = new mw.MediaPlayer( 'webmNative', [
	'video/webm',
	'video/webm; codecs="vp8"',
	'video/webm; codecs="vp8, vorbis"',
	'audio/webm',
	'audio/webm; codecs="vorbis"'
], 'Native' );
var vp9NativePlayer = new mw.MediaPlayer( 'vp9Native', [
	'video/webm; codecs="vp9"',
	'video/webm; codecs="vp9, opus"',
	'video/webm; codecs="vp9, vorbis"',
	'audio/webm; codecs="opus"'
], 'Native' );

// Image Overlay player ( extends native )
var imageOverlayPlayer = new mw.MediaPlayer( 'imageOverlay', [
	'image/jpeg',
	'image/png'
], 'ImageOverlay' );

// VLC player
//var vlcMimeList = ['video/ogg', 'audio/ogg', 'audio/mpeg', 'application/ogg', 'video/x-flv', 'video/mp4', 'video/h264', 'video/x-msvideo', 'video/mpeg', 'video/3gp'];
//var vlcPlayer = new mw.MediaPlayer( 'vlc-player', vlcMimeList, 'Vlc' );

var vlcAppPlayer = new mw.MediaPlayer( 'vlcAppPlayer', [
	'video/ogg',
	'video/ogg; codecs="theora"',
	'video/ogg; codecs="theora, vorbis"',
	'audio/ogg',
	'audio/ogg; codecs="vorbis"',
	'audio/ogg; codecs="opus"',
	'application/ogg',
	'video/webm',
	'video/webm; codecs="vp8"',
	'video/webm; codecs="vp8, vorbis"',
], 'VLCApp' );

var IEWebMPrompt = new mw.MediaPlayer( 'IEWebMPrompt', [
	'video/webm',
	'video/webm; codecs="vp8"',
	'video/webm; codecs="vp8, vorbis"'
], 'IEWebMPrompt' );

var ogvJsPlayer = new mw.MediaPlayer( 'ogvJsPlayer', [
	'video/ogg',
	'video/ogg; codecs="theora"',
	'video/ogg; codecs="theora, vorbis"',
	'video/ogg; codecs="theora, opus"',
	'audio/ogg',
	'audio/ogg; codecs="vorbis"',
	'audio/ogg; codecs="opus"',
	'application/ogg'
], 'OgvJs' );

// Generic plugin
//var oggPluginPlayer = new mw.MediaPlayer( 'oggPlugin', ['video/ogg', 'application/ogg'], 'Generic' );


mw.EmbedTypes = {

	// MediaPlayers object ( supports methods for quering set of browser players )
	mediaPlayers: null,

	 // Detect flag for completion
	 detect_done:false,

	/**
	 * Runs the detect method and update the detect_done flag
	 *
	 * @constructor
	 */
	 init: function() {
		// detect supported types
		this.detect();
		this.detect_done = true;
	},

	getMediaPlayers: function(){
		if( this.mediaPlayers  ){
			return this.mediaPlayers;
		}
		this.mediaPlayers = new mw.MediaPlayers();
		// detect available players
		this.detectPlayers();
		return this.mediaPlayers;
	},

	/**
	 * If the browsers supports a given mimetype
	 *
	 * @param {String}
	 *      mimeType Mime type for browser plug-in check
	 */
	supportedMimeType: function( mimeType ) {
		for ( var i =0; i < navigator.plugins.length; i++ ) {
			var plugin = navigator.plugins[i];
			if ( typeof plugin[ mimeType ] != "undefined" ){
				return true;
			}
		}
		return false;
	},
	addFlashPlayer: function(){
		if( !mw.config.get( 'EmbedPlayer.DisableHTML5FlashFallback' ) ){
			this.mediaPlayers.addPlayer( kplayer );
		}
	},
	/**
	 * Detects what plug-ins the client supports
	 */
	detectPlayers: function() {
		mw.log( "EmbedTypes::detectPlayers running detect" );

		// All players support for playing "images"
		this.mediaPlayers.addPlayer( imageOverlayPlayer );

		// Some browsers filter out duplicate mime types, hiding some plugins
		var uniqueMimesOnly = $.client.test( { opera: null, safari: null } );

		// Use core mw.supportsFlash check:
		if( mw.supportsFlash() ){
			this.addFlashPlayer();
		}

		// ActiveX plugins
		if ( $.client.profile().name === 'msie' ) {
			 // VLC
			 //if ( this.testActiveX( 'VideoLAN.VLCPlugin.2' ) ) {
			 //	 this.mediaPlayers.addPlayer( vlcPlayer );
			 //}

			 // quicktime (currently off)
			 // if ( this.testActiveX(
				// 'QuickTimeCheckObject.QuickTimeCheck.1' ) )
			 // this.mediaPlayers.addPlayer(quicktimeActiveXPlayer);
		 }
		// <video> element
		if ( ! mw.config.get('EmbedPlayer.DisableVideoTagSupport' ) // to support testing limited / old browsers
				&&
				(
				typeof HTMLVideoElement == 'object' // Firefox, Safari
					||
				typeof HTMLVideoElement == 'function' // Opera
				)
		){
			// Test what codecs the native player supports:
			try {
				var dummyvid = document.createElement( "video" );
				if( dummyvid.canPlayType ) {
					// Add the webm player
					if( dummyvid.canPlayType('video/webm; codecs="vp8, vorbis"') ){
						this.mediaPlayers.addPlayer( webmNativePlayer );
					}
					if( dummyvid.canPlayType('video/webm; codecs="vp9, opus"') ){
						this.mediaPlayers.addPlayer( vp9NativePlayer );
					}

					// Test for MP3:
					if ( this.supportedMimeType('audio/mpeg') ) {
							this.mediaPlayers.addPlayer( mp3NativePlayer );
					}

					// Test for AAC:
					if ( dummyvid.canPlayType( 'audio/mp4; codecs="mp4a.40.5"' ) ) {
							this.mediaPlayers.addPlayer( aacNativePlayer );
					}

					// Test for h264:
					if ( dummyvid.canPlayType('video/mp4; codecs="avc1.42E01E, mp4a.40.2"' ) ) {
						this.mediaPlayers.addPlayer( h264NativePlayer );
						// Check for iOS for vdn player support ( apple adaptive ) or vdn canPlayType != '' ( ie maybe/probably )
						if( mw.isIOS() || dummyvid.canPlayType('application/vnd.apple.mpegurl; codecs="avc1.42E01E"' ) ){
							// Android 3x lies about HLS support ( only add if not Android 3.x )
							if( navigator.userAgent.indexOf( 'Android 3.') == -1 ){
								this.mediaPlayers.addPlayer( appleVdnPlayer );
							}
						}

					}
					// For now if Android assume we support h264Native (FIXME
					// test on real devices )
					if ( mw.isAndroid2() ){
						this.mediaPlayers.addPlayer( h264NativePlayer );
					}

					// Test for ogg
					if ( dummyvid.canPlayType( 'video/ogg; codecs="theora, vorbis"' ) || 
						dummyvid.canPlayType( 'audio/ogg; codecs="vorbis"' )
					) {
						this.mediaPlayers.addPlayer( oggNativePlayer );
					}

					// Test for opus
					if ( dummyvid.canPlayType( 'audio/ogg; codecs="opus"' ).replace(/maybe/, '') ) {
						this.mediaPlayers.addPlayer( opusNativePlayer );
					}
				}
			} catch ( e ) {
				mw.log( 'could not run canPlayType ' + e );
			}
		}

		 // "navigator" plugins
		if ( navigator.mimeTypes && navigator.mimeTypes.length > 0 ) {
			for ( var i = 0; i < navigator.mimeTypes.length; i++ ) {
				var type = navigator.mimeTypes[i].type;
				var semicolonPos = type.indexOf( ';' );
				if ( semicolonPos > -1 ) {
					type = type.substr( 0, semicolonPos );
				}
				// mw.log( 'on type: ' + type );
				var pluginName = navigator.mimeTypes[i].enabledPlugin ? navigator.mimeTypes[i].enabledPlugin.name : '';
				if ( !pluginName ) {
					// In case it is null or undefined
					pluginName = '';
				}
				//if ( pluginName.toLowerCase() == 'vlc multimedia plugin' || pluginName.toLowerCase() == 'vlc multimedia plug-in' ) {
				//	this.mediaPlayers.addPlayer( vlcPlayer );
				//	continue;
				//}

				if ( (type == 'video/mpeg' || type == 'video/x-msvideo') ){
					//pluginName.toLowerCase() == 'vlc multimedia plugin' ) {
					//this.mediaPlayers.addPlayer( vlcMozillaPlayer );
				}

				if ( type == 'application/ogg' ) {
					//if ( pluginName.toLowerCase() == 'vlc multimedia plugin' ) {
						//this.mediaPlayers.addPlayer( vlcMozillaPlayer );
					//else if ( pluginName.indexOf( 'QuickTime' ) > -1 )
						//this.mediaPlayers.addPlayer(quicktimeMozillaPlayer);
					//} else {
						//this.mediaPlayers.addPlayer( oggPluginPlayer );
					//}
					continue;
				} else if ( uniqueMimesOnly ) {
					if ( type == 'application/x-vlc-player' ) {
						// this.mediaPlayers.addPlayer( vlcMozillaPlayer );
						continue;
					} else if ( type == 'video/quicktime' ) {
						// this.mediaPlayers.addPlayer(quicktimeMozillaPlayer);
						continue;
					}
				}
			}
		}

		if ( mw.isIOS() ) {
			this.mediaPlayers.addPlayer( vlcAppPlayer );
		}

		// Note IE 11 doesn't identify itself as 'MSIE'.
		// For simplicity just check for the rendering engine codename 'Trident'.
		if ( navigator.userAgent.indexOf( 'Trident' ) != -1 ) {
			if ( this.mediaPlayers.isSupportedPlayer( 'webmNative' ) ) {
				// IE has the WebM components already, leave it be!
			} else if ( navigator.userAgent.indexOf( 'ARM' ) != -1 ) {
				// Windows RT doesn't allow installation of the WebM components.
				// Don't tease the poor user.
			} else {
				// Prompt user to install the WebM media components for IE 9+
				this.mediaPlayers.addPlayer( IEWebMPrompt );
			}
		}

		// ogv.js compatibility detection...
		if ( OGVCompat.supported( 'OGVPlayer' ) ) {
			// ogv.js emscripten version
			//
			// Works in:
			// * Safari 6.1+ on Mac OS X
			// * Safari on iOS 8+ (best on 64-bit devices)
			// * IE 10/11 on Windows 7/8/8.1 (requires Flash for audio)
			// * Edge on Windows 10 (no plugins needed)
			//
			// Current Firefox, Chrome, Opera all work great too, but use
			// native playback by default of course!
			//
			this.mediaPlayers.addPlayer( ogvJsPlayer );
		}

		// Allow extensions to detect and add their own "players"
		mw.log("EmbedPlayer::trigger:embedPlayerUpdateMediaPlayersEvent");
		$( mw ).trigger( 'embedPlayerUpdateMediaPlayersEvent' , this.mediaPlayers );

	},

	/**
	 * Test IE for activeX by name
	 *
	 * @param {String}
	 * 		name Name of ActiveXObject to look for
	 */
	testActiveX : function ( name ) {
		mw.log("EmbedPlayer::detect: test testActiveX: " + name);
		var hasObj = true;
		try {
			// No IE, not a class called "name", it's a variable
			var obj = new ActiveXObject( '' + name );
		} catch ( e ) {
			hasObj = false;
		}
		return hasObj;
	}
};


} )( mediaWiki, jQuery );