hls-playback-controller.js
3.55 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
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
(function (window) {
window.videojs.hls.HLSPlaybackController = function (vjsPlayerReference) {
var ManifestController = window.videojs.hls.ManifestController;
var SegmentController = window.videojs.hls.SegmentController;
var MediaSource = window.videojs.MediaSource;
var SegmentParser = window.videojs.hls.SegmentParser;
var M3U8 = window.videojs.hls.M3U8;
var self = this;
self.player = vjsPlayerReference;
self.mediaSource = new MediaSource();
self.parser = new SegmentParser();
self.manifestController = null;
self.segmentController = null;
self.manifestLoaded = false;
self.currentSegment = 0;
self.currentManifest = null;
self.currentPlaylist = null;
self.currentRendition = null;
// Register Externall Callbacks
self.manifestLoadCompleteCallback;
self.player.on('timeupdate', function () {
console.log(self.player.currentTime());
});
self.player.on('onsrcchange', function () {
console.log('src change', self.player.currentSrc());
//if src.url.m3u8 -- loadManifest.url
});
self.rendition = function (rendition) {
self.currentRendition = rendition;
self.loadManifest(self.currentRendition.url, self.onM3U8LoadComplete, self.onM3U8LoadError, self.onM3U8Update);
};
self.loadManifest = function (manifestUrl, onDataCallback, onErrorCallback, onUpdateCallback) {
self.mediaSource.addEventListener('sourceopen', function (event) {
console.log('source open here');
// 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(), video);
if( onDataCallback )
{
self.manifestLoadCompleteCallback = onDataCallback;
}
self.manifestController = new ManifestController();
self.manifestController.loadManifest(manifestUrl, self.onM3U8LoadComplete, self.onM3U8LoadError, self.onM3U8Update);
}, false);
self.player.src({
src: 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 (error) {
};
self.onM3U8Update = function (m3u8) {
};
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);
};
console.log('load another',self.currentSegment,self.currentManifest.mediaItems.length);
if(self.currentSegment < self.currentManifest.mediaItems.length-1)
{
console.log('load another');
self.loadNextSegment();
}
};
self.loadNextSegment = function () {
self.currentSegment++;
self.loadSegment(self.currentManifest.mediaItems[self.currentSegment]);
}
self.onSegmentLoadError = function (error) {
};
};
})(this);