-
Notifications
You must be signed in to change notification settings - Fork 7
/
Copy pathvg-dash.js
92 lines (83 loc) · 3.5 KB
/
vg-dash.js
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
/**
* @license videogular v1.4.4 http://videogular.com
* Two Fucking Developers http://twofuckingdevelopers.com
* License: MIT
*/
/**
* @ngdoc directive
* @name com.2fdevs.videogular.plugins.dash.directive:vgDash
* @restrict A
* @description
* Adds DASH support for vg-media.
* This plugin requires dash.all.js file available at dash.js project:
* https://github.com/Dash-Industry-Forum/dash.js
*
* <pre>
* <videogular vg-theme="config.theme.url" vg-autoplay="config.autoPlay">
* <vg-media vg-src="sources" vg-dash></vg-media>
* </videogular>
* </pre>
*
*/
"use strict";
angular.module("com.2fdevs.videogular.plugins.dash", [])
.directive(
"vgDash",
["$window", function ($window) {
return {
restrict: "A",
require: "^videogular",
link: function (scope, elem, attr, API) {
var player;
var dashTypeRegEx = /^application\/dash\+xml/i;
function supportsMediaSource() {
return "MediaSource" in $window;
}
//Proceed augmenting behavior only if the browser is capable of playing DASH (supports MediaSource Extensions)
if (supportsMediaSource()) {
//Returns true if the source has the standard DASH type defined OR an .mpd extension.
scope.isDASH = function isDASH(source) {
var hasDashType = dashTypeRegEx.test(source.type);
var hasDashExtension = source.src.indexOf && (source.src.indexOf(".mpd") > 0);
return hasDashType || hasDashExtension;
};
scope.onSourceChange = function onSourceChange(source) {
var url = source.src;
// It's DASH, we use dash.js
if (scope.isDASH(source)) {
if (angular.isFunction(dashjs && dashjs.MediaPlayer)) {
// dash.js version 2.x
player = dashjs.MediaPlayer().create();
player.initialize(API.mediaElement[0], url, API.autoPlay);
} else {
// dash.js version < 2.x
player = new MediaPlayer(new Dash.di.DashContext());
player.setAutoPlay(API.autoPlay);
player.startup();
player.attachView(API.mediaElement[0]);
player.attachSource(url);
}
}
else if (player) {
//not DASH, but the dash.js player is still wired up
//Dettach dash.js from the mediaElement
player.reset();
player = null;
//player.reset() wipes out the new url already applied, so have to reapply
API.mediaElement.attr('src', url);
API.stop();
}
};
scope.$watch(
function () {
return API.sources;
},
function (newVal, oldVal) {
scope.onSourceChange(newVal[0]);
}
);
}
}
}
}
]);