summaryrefslogtreecommitdiff
path: root/extensions/TimedMediaHandler/resources/ext.tmh.OgvJsSupport.js
blob: af3fd9f54e8b7b1ed6d2bceb157e0ca9c907b171 (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
( function( $, mw ) {

	var support = mw.OgvJsSupport = {
		/**
		 * Ensure that the OGVPlayer class is loaded before continuing.
		 *
		 * @return jQuery.Deferred
		 */
		loadOgvJs: function() {
			return $.Deferred( function( deferred ) {
				if ( typeof OGVPlayer === 'undefined' ) {
					mw.loader.using( 'ext.tmh.OgvJs', function() {
						OGVLoader.base = support.basePath();
						deferred.resolve();
					} );
				} else {
					deferred.resolve();
				}
			});
		},

		/**
		 * Get the base path of ogv.js and friends.
		 *
		 * @return string
		 */
		basePath: function() {
			var ext = mw.config.get( 'wgExtensionAssetsPath' ),
				binPlayers = ext + '/TimedMediaHandler/MwEmbedModules/EmbedPlayer/binPlayers';
			return binPlayers + '/ogv.js';
		},

		/**
		 * Return a stub audio context
		 *
		 * This is used for iOS Safari to enable Web Audio by triggering an empty
		 * audio output channel during a user input event handler. Without that,
		 * audio is left disabled and won't work when we start things up after an
		 * asynchronous code load.
		 *
		 * @return AudioContext
		 */
		initAudioContext: function() {
			var AudioContext = window.AudioContext || window.webkitAudioContext;
			if ( AudioContext ) {
				var context = new AudioContext(),
					node;
				if ( context.createScriptProcessor ) {
					node = context.createScriptProcessor( 1024, 0, 2 );
				} else if ( context.createJavaScriptNode ) {
					node = context.createJavaScriptNode( 1024, 0, 2 );
				} else {
					throw new Error( 'Bad version of web audio API?' );
				}

				// Don't actually run any audio, just start & stop the node
				node.connect( context.destination );
				node.disconnect();

				return context;
			} else {
				return null;
			}
		}
	};

} )( jQuery, mediaWiki );