hls-playback-controller.js
2.95 KB
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
(function(window) {
var
ManifestController = window.videojs.hls.ManifestController,
SegmentController = window.videojs.hls.SegmentController,
MediaSource = window.videojs.MediaSource,
SegmentParser = window.videojs.hls.SegmentParser;
window.videojs.hls.HLSPlaybackController = function(player) {
var self = this;
self.player = player;
self.mediaSource = new MediaSource();
self.parser = new SegmentParser();
self.manifestLoaded = false;
self.currentSegment = 0;
// register external callbacks
self.rendition = function(rendition) {
self.currentRendition = rendition;
self.loadManifest(self.currentRendition.url, self.onM3U8LoadComplete, self.onM3U8LoadError, self.onM3U8Update);
};
self.loadManifest = function(manifestUrl, onDataCallback) {
self.mediaSource.addEventListener('sourceopen', function() {
// feed parsed bytes into the player
self.sourceBuffer = self.mediaSource.addSourceBuffer('video/flv; codecs="vp6,aac"');
self.parser = new SegmentParser();
self.sourceBuffer.appendBuffer(self.parser.getFlvHeader(), self.player);
if (onDataCallback) {
self.manifestLoadCompleteCallback = onDataCallback;
}
self.manifestController = new ManifestController();
self.manifestController.loadManifest(manifestUrl, self.onM3U8LoadComplete, self.onM3U8LoadError, self.onM3U8Update);
}, false);
self.player.src({
src: window.videojs.URL.createObjectURL(self.mediaSource),
type: "video/flv"
});
};
self.onM3U8LoadComplete = function(m3u8) {
if (m3u8.invalidReasons.length === 0) {
if (m3u8.isPlaylist) {
self.currentPlaylist = m3u8;
self.rendition(self.currentPlaylist.playlistItems[0]);
} else {
self.currentManifest = m3u8;
self.manifestLoaded = true;
self.loadSegment(self.currentManifest.mediaItems[0]);
if (self.manifestLoadCompleteCallback) {
self.manifestLoadCompleteCallback(m3u8);
}
}
}
};
self.onM3U8LoadError = function() {};
self.onM3U8Update = function() {};
self.loadSegment = function(segment) {
self.segmentController = new SegmentController();
self.segmentController.loadSegment(segment.url, self.onSegmentLoadComplete, self.onSegmentLoadError);
};
self.onSegmentLoadComplete = function(segment) {
self.parser.parseSegmentBinaryData(segment.binaryData);
while (self.parser.tagsAvailable()) {
self.sourceBuffer.appendBuffer(self.parser.getNextTag().bytes, self.player);
}
if (self.currentSegment < self.currentManifest.mediaItems.length-1) {
self.loadNextSegment();
}
};
self.loadNextSegment = function() {
self.currentSegment++;
self.loadSegment(self.currentManifest.mediaItems[self.currentSegment]);
};
self.onSegmentLoadError = function() {};
};
})(this);