This repository has been archived by the owner on Jan 17, 2023. It is now read-only.
-
-
Notifications
You must be signed in to change notification settings - Fork 5
/
Copy pathvideojs-mpegts.js
143 lines (121 loc) · 4.17 KB
/
videojs-mpegts.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
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
(function(videojs, mpegts) {
// nothing to do if loaded without videojs/mpegts
if (!videojs || !mpegts) {
console.error('Missing videojs/mpegts libraries');
return;
}
/**
*
* @param source
* @param tech
* @constructor
*/
function Html5mpegts(source, tech) {
var options = tech.options_;
var el = tech.el();
var duration = null;
const mediaDataSource = options.mediaDataSource || {};
mediaDataSource.type = mediaDataSource.type === undefined ? 'flv' : mediaDataSource.type;
mediaDataSource.url = source.src;
var player = mpegts.createPlayer(mediaDataSource, options.mpegtsConfig);
/**
* creates an error handler function
* @returns {Function}
*/
function errorHandlerFactory() {
var _recoverDecodingErrorDate = null;
var _recoverAudioCodecErrorDate = null;
return function() {
var now = Date.now();
if (!_recoverDecodingErrorDate || (now - _recoverDecodingErrorDate) > 2000) {
_recoverDecodingErrorDate = now;
console.log('MPEGTS: Error (video)?')
//hls.recoverMediaError();
} else if (!_recoverAudioCodecErrorDate || (now - _recoverAudioCodecErrorDate) > 2000) {
_recoverAudioCodecErrorDate = now;
console.log('MPEGTS: Error (audio)?')
//hls.swapAudioCodec();
//hls.recoverMediaError();
} else {
console.error('Error loading media: File could not be played');
}
}
}
// create separate error handlers for hlsjs and the video tag
var videoTagErrorHandler = errorHandlerFactory();
// listen to error events coming from the video tag
el.addEventListener('error', function(e) {
var mediaError = e.currentTarget.error;
if (mediaError && mediaError.code === mediaError.MEDIA_ERR_DECODE) {
videoTagErrorHandler();
} else {
console.error('Error loading media: File could not be played');
}
});
/**
*
*/
this.dispose = function() {
player.destroy();
};
/**
* returns the duration of the stream, or Infinity if live video
* @returns {Infinity|number}
*/
this.duration = function() {
return duration || el.duration || 0;
};
// update live status on meida info load
player.on(mpegts.Events.MEDIA_INFO, function(data) {
duration = data.duration || Infinity;
});
// try to recover when live and then loading has started
player.on(mpegts.Events.LOADING_COMPLETE, function(data){
//Only if live
if(duration === Infinity){
player.unload();
setTimeout(function(){
player.load();
player.play();
}, 500);
}
})
Object.keys(mpegts.Events).forEach(function(key) {
var eventName = mpegts.Events[key];
player.on(eventName, function(event, data) {
tech.trigger(eventName, data);
});
});
// attach mpegts to videotag
player.attachMediaElement(el);
player.load();
}
var FlvSourceHandler = {
canHandleSource: function(source) {
var flvTypeRE = /^video\/flv$/i;
var flvExtRE = /\.flv/i;
var flvProtoRE = /^((ws:\/\/)|(http:\/\/))/i;
if (flvTypeRE.test(source.type)) {
return 'probably';
} else if (flvExtRE.test(source.src) || flvProtoRE.test(source.src)) {
return 'maybe';
} else {
return '';
}
},
handleSource: function(source, tech) {
return new Html5mpegts(source, tech);
},
canPlayType: function(type) {
var flvTypeRE = /^video\/flv$/i;
if (flvTypeRE.test(type)) {
return 'probably';
}
return '';
}
};
// only attach this source handler is its supported
if (mpegts.isSupported()) {
videojs.getTech('Html5').registerSourceHandler(FlvSourceHandler, 0);
}
})(window.videojs, window.mpegts);