summaryrefslogtreecommitdiff
path: root/extensions/TimedMediaHandler/MwEmbedModules/EmbedPlayer/resources/mw.MediaSource.js
blob: 9449a5d4383c8c21395cb50e61a60682eb12edfe (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
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
/**
 * mediaSource class represents a source for a media element.
 *
 * @param {Element}
 *      element: MIME type of the source.
 * @constructor
 */

/**
 * The base source attribute checks also see:
 * http://dev.w3.org/html5/spec/Overview.html#the-source-element
 */

( function( mw, $ ) { "use strict";

mw.mergeConfig( 'EmbedPlayer.SourceAttributes', [
	// source id
	'id',

	// media url
	'src',

	// Title string for the source asset
	'title',

	// boolean if we support temporal url requests on the source media
	'URLTimeEncoding',

	// Store the node name for type identification
	'nodeName',

	/**
	 * data- attributes ( not yet standards )
	*/

	// Media has a startOffset ( used for plugins that
	// display ogg page time rather than presentation time
	'data-startoffset',

	// A hint to the duration of the media file so that duration
	// can be displayed in the player without loading the media file
	'data-durationhint',

	// Source stream qualities
	// NOTE data- is striped from the attribute as we build out the "mediaSource" object
	'data-shorttitle', // short title for stream ( useful for stream switching control bar widget)
	'data-width', // the width of the stream
	'data-height', // the height of the stream
	'data-bandwidth', // the overall bitrate of the stream in bytes
	'data-sizebytes', // the size of the stream in bytes
	'data-framerate', // the framereate of the stream
	'data-flavorid', // a source flavor id ( useful for targeting devices )
	'data-aspect', // the aspect ratio, useful for adaptive protocal urls that don't have a strict height / width

	// Used as title in download panel
	'data-title',

	// Used for download attribute on mediawiki
	'data-mwtitle',
	// used for setting the api provider for mediawiki
	'data-mwprovider',

	// to disable menu or timedText for a given embed
	'data-disablecontrols',

	// used for language direction of subtitles
	'data-dir',

	// Media start time
	'start',

	// Media end time
	'end',

	// If the source is the default source
	'default'
] );

mw.MediaSource = function( element ) {
	this.init( element );
};

mw.MediaSource.prototype = {
	// MIME type of the source.
	mimeType:null,

	// URI of the source.
	uri:null,

	// Title of the source.
	title: null,

	// True if the source has been marked as the default.
	markedDefault: false,

	// True if the source supports url specification of offset and duration
	URLTimeEncoding:false,

	// Start offset of the requested segment
	startOffset: 0,

	// Duration of the requested segment (0 if not known)
	duration:0,

	// source id
	id: null,

	// Start time in npt format
	startNpt: null,

	// End time in npt format
	endNpt: null,

	// Language of the file
	srclang: null,
	/**
	 * MediaSource constructor:
	 */
	init : function( element ) {
		var _this = this;
		// mw.log('EmbedPlayer::adding mediaSource: ' + element);
		this.src = $( element ).attr( 'src' );

		// Set default URLTimeEncoding if we have a time url:
		// not ideal way to discover if content is on an oggz_chop server.
		// should check some other way.
		var pUrl = new mw.Uri ( this.src );
		if ( typeof pUrl.query[ 't' ] != 'undefined' ) {
			this.URLTimeEncoding = true;
		}

		var sourceAttr = mw.config.get( 'EmbedPlayer.SourceAttributes' );
		$.each( sourceAttr, function( inx, attr ){
			if ( $( element ).attr( attr ) ) {
				// strip data- from the attribute name
				var attrName = ( attr.indexOf('data-') === 0) ? attr.substr(5) : attr
				_this[ attrName ] = $( element ).attr( attr );
			}
		});

		// Normalize "label" to "title" ( label is the actual spec so use that over title )
		if( this.label ){
			this.title = this.label;
		}

		// Set the content type:
		if ( $( element ).attr( 'type' ) ) {
			this.mimeType = $( element ).attr( 'type' );
		}else if ( $( element ).attr( 'content-type' ) ) {
			this.mimeType = $( element ).attr( 'content-type' );
		}else if( $( element )[0].tagName.toLowerCase() == 'audio' ){
			// If the element is an "audio" tag set audio format
			this.mimeType = 'audio/ogg';
		} else {
			this.mimeType = this.detectType( this.src );
		}

		// Conform the mime type to ogg
		if( this.mimeType == 'video/theora') {
			this.mimeType = 'video/ogg';
		}

		if( this.mimeType == 'audio/vorbis') {
			this.mimeType = 'audio/ogg';
		}

		// Check for parent elements ( supplies categories in "track" )
		if( $( element ).parent().attr('category') ) {
			this.category = $( element ).parent().attr('category');
		}

		if( $( element ).attr( 'default' ) ){
			this.markedDefault = true;
		}

		// Get the url duration ( if applicable )
		this.getURLDuration();
	},

	/**
	 * Update Source title via Element
	 *
	 * @param {Element}
	 *      element Source element to update attributes from
	 */
	updateSource: function( element ) {
		// for now just update the title:
		if ( $( element ).attr( "title" ) ) {
			this.title = $( element ).attr( "title" );
		}
	},

	/**
	 * Updates the src time and start & end
	 *
	 * @param {String}
	 *      start_time: in NPT format
	 * @param {String}
	 *      end_time: in NPT format
	 */
	updateSrcTime: function ( startNpt, endNpt ) {
		// mw.log("f:updateSrcTime: "+ startNpt+'/'+ endNpt + ' from org: ' +
		// this.startNpt+ '/'+this.endNpt);
		// mw.log("pre uri:" + this.src);
		// if we have time we can use:
		if ( this.URLTimeEncoding ) {
			// make sure its a valid start time / end time (else set default)
			if ( !mw.npt2seconds( startNpt ) ) {
				startNpt = this.startNpt;
			}

			if ( !mw.npt2seconds( endNpt ) ) {
				endNpt = this.endNpt;
			}

			this.src = mw.replaceUrlParams( this.src, {
				't': startNpt + '/' + endNpt
			});

			// update the duration
			this.getURLDuration();
		}
	},

	/**
	 * Sets the duration and sets the end time if unset
	 *
	 * @param {Float}
	 *      duration: in seconds
	 */
	setDuration: function ( duration ) {
		this.duration = duration;
		if ( !this.endNpt ) {
			this.endNpt = mw.seconds2npt( this.startOffset + duration );
		}
	},

	/**
	 * MIME type accessor function.
	 *
	 * @return {String} the MIME type of the source.
	 */
	getMIMEType: function() {
		if( this.mimeType ) {
			return this.mimeType;
		}
		this.mimeType = this.detectType( this.src );
		return this.mimeType;
	},
	/**
	 * Update the local src
	 * @param {String}
	 * 		src The URL to the media asset
	 */
	setSrc: function( src ){
		this.src = src;
	},

	/**
	 * URI function.
	 *
	 * @param {Number}
	 *      serverSeekTime Int: Used to adjust the URI for url based
	 *      seeks)
	 * @return {String} the URI of the source.
	 */
	getSrc: function( serverSeekTime ) {
		if ( !serverSeekTime || !this.URLTimeEncoding ) {
			return this.src;
		}
		var endvar = '';
		if ( this.endNpt ) {
			endvar = '/' + this.endNpt;
		}
		return mw.replaceUrlParams( this.src,
			{
				't': mw.seconds2npt( serverSeekTime ) + endvar
	  		}
		);
	},
	/**
	 * Title accessor function.
	 *
	 * @return {String} Title of the source.
	 */
	getTitle : function() {
		if( this.title ){
			return this.title;
		}
		// Text tracks use "label" instead of "title"
		if( this.label ){
			return this.label;
		}

		// Return a Title based on mime type:
		var mimeType = this.getMIMEType().split( ';' )[0];
		switch( mimeType ) {
			case 'video/h264' :
			case 'video/mp4' :
				return mw.msg( 'mwe-embedplayer-video-h264' );
			break;
			case 'video/x-flv' :
				return mw.msg( 'mwe-embedplayer-video-flv' );
			break;
			case 'video/webm' :
				return mw.msg( 'mwe-embedplayer-video-webm');
			break;
			case 'video/ogg' :
				return mw.msg( 'mwe-embedplayer-video-ogg' );
			break;
			case 'audio/ogg' :
				return mw.msg( 'mwe-embedplayer-video-audio' );
			break;
			case 'audio/mpeg' :
				return mw.msg('mwe-embedplayer-audio-mpeg');
			break;
			case 'video/3gp' :
				return mw.msg('mwe-embedplayer-video-3gp');
			break;
			case 'video/mpeg' :
				return mw.msg('mwe-embedplayer-video-mpeg');
			break;
			case 'video/x-msvideo' :
				return mw.msg('mwe-embedplayer-video-msvideo' );
			break;
		}

		// Return title based on file name:
		try{
			var fileName = new mw.Uri( mw.absoluteUrl( this.getSrc() ) ).path.split('/').pop();
			if( fileName ){
				return fileName;
			}
		} catch(e){}

		// Return the mime type string if not known type.
		return this.mimeType;
	},
	/**
	 * Get a short title for the stream
	 */
	getShortTitle: function(){
		var _this =this;
		if( this.shorttitle ){
			return this.shorttitle;
		}
		// Just use a short "long title"
		var longTitle = this.getTitle();
		if(longTitle.length > 20) {
			longTitle = longTitle.substring(0,17)+"...";
		}
		return longTitle
	},
	/**
	 *
	 * Get Duration of the media in milliseconds from the source url.
	 *
	 * Supports media_url?t=ntp_start/ntp_end url request format
	 */
	getURLDuration : function() {
		// check if we have a URLTimeEncoding:
		if ( this.URLTimeEncoding ) {
			var annoURL = new mw.Uri( this.src );
			if ( annoURL.query.t ) {
				var times = annoURL.query.t.split( '/' );
				this.startNpt = times[0];
				this.endNpt = times[1];
				this.startOffset = mw.npt2seconds( this.startNpt );
				this.duration = mw.npt2seconds( this.endNpt ) - this.startOffset;
			} else {
				// look for this info as attributes
				if ( this.startOffset ) {
					this.startNpt = mw.seconds2npt( this.startOffset );
				}
				if ( this.duration ) {
					this.endNpt = mw.seconds2npt( parseInt( this.duration ) + parseInt( this.startOffset ) );
				}
			}
		}
	},
	/**
	* Get the extension of a url
	* @param String uri
	*/
	getExt : function( uri ){
		var urlParts = new mw.Uri( uri );
		// Get the extension from the url or from the relative name:
		var ext = ( urlParts.file ) ?  /[^.]+$/.exec( urlParts.file )  :  /[^.]+$/.exec( uri );
		// remove the hash string if present
		ext = /[^#]*/g.exec( ext.toString() );
		ext = ext || '';
		return ext.toString().toLowerCase();
	},
	/**
	 * Get the flavorId if available.
	 */
	getFlavorId: function(){
		if( this.flavorid ){
			return this.flavorid;
		}
		return ;
	},

	/**
	 * Attempts to detect the type of a media file based on the URI.
	 *
	 * @param {String}
	 *      uri URI of the media file.
	 * @return {String} The guessed MIME type of the file.
	 */
	detectType: function( uri ) {
		// NOTE: if media is on the same server as the javascript
		// we can issue a HEAD request and read the mime type of the media...
		// ( this will detect media mime type independently of the url name )
		// http://www.jibbering.com/2002/4/httprequest.html
		switch( this.getExt( uri ) ) {
			case 'smil':
			case 'sml':
				return 'application/smil';
			break;
			case 'm4v':
			case 'mp4':
				return 'video/h264';
			break;
			case 'm3u8':
				return 'application/vnd.apple.mpegurl';
			break;
			case 'webm':
				return 'video/webm';
			break;
			case '3gp':
				return 'video/3gp';
			break;
			case 'srt':
				return 'text/x-srt';
			break;
			case 'flv':
				return 'video/x-flv';
			break;
			case 'ogg':
			case 'ogv':
				return 'video/ogg';
			break;
			case 'oga':
				return 'audio/ogg';
			break;
			case 'mp3':
				return 'audio/mpeg';
			case 'm4a':
				return 'audio/mp4';
			break;
			case 'anx':
				return 'video/ogg';
			break;
			case 'xml':
				return 'text/xml';
			break;
			case 'avi':
				return 'video/x-msvideo';
			break;
			case 'mpg':
				return 'video/mpeg';
			break;
			case 'mpeg':
				return 'video/mpeg';
			break;
		}
		mw.log( "Error: could not detect type of media src: " + uri );
	},
	/**
	 * bitrate is mesured in kbs rather than bandwith bytes per second
	 */
	getBitrate: function() {
		if( this.bandwidth ){
			return this.bandwidth / 1024;
		}
		return 0;
	},
	/**
	 * Get the size of the stream in bytes
	 */
	getSize: function(){
		if( this.sizebytes ){
			return this.sizebytes;
		}
		return 0;
	}
};

} )( mediaWiki, jQuery );