forked from Hyddan/dash.js
-
Notifications
You must be signed in to change notification settings - Fork 0
/
test_live.html
180 lines (144 loc) · 5.61 KB
/
test_live.html
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
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="utf-8"/>
<title>MediaSource Test</title>
<meta name="description" content="" />
<script>
var ms,
sourceBuffer,
video,
baseUrl = "http://pixie.path1.com/dash/",
mimetype,
codec,
loaded = 0,
frags,
template,
playbackStarted = false;
/*
<AdaptationSet frameRate="30000/1001" mimeType="video/mp4" codecs="avc1.4D401F" startWithSAP="1" segmentAlignment="true">
<SegmentTemplate timescale="30000" duration="60060" media="$Bandwidth$/$Number$.m4v" initialization="$Bandwidth$/0.m4s" />
<Representation width="342" height="192" id="v0" bandwidth="500000" />
<Representation width="556" height="312" id="v1" bandwidth="525000" />
<Representation width="708" height="398" id="v2" bandwidth="850000" />
<Representation width="832" height="468" id="v3" bandwidth="1175000" />
<Representation width="942" height="528" id="v4" bandwidth="1500000" />
</AdaptationSet>
<AdaptationSet mimeType="audio/mp4" codecs="mp4a.40.2" startWithSAP="1" segmentAlignment="true">
<SegmentTemplate timescale="48000" duration="96096" media="audio/$Number$.m4a" initialization="audio/0.m4s" />
<Representation id="a0" bandwidth="96000" />
</AdaptationSet>
*/
function loadVideo() {
mimetype = "video/mp4";
codec = "avc1.4D401F";
template = {
timescale: 30000,
duration: 60060,
media: "500000/$Number$.m4v",
initialization: "500000/0.m4s"
};
loadTemplate();
}
function loadAudio() {
mimetype = "audio/mp4";
codec = "mp4a.40.5";
template = {
timescale: 48000,
duration: 96096,
media: "audio/$Number$.m4a",
initialization: "audio/0.m4s"
};
loadTemplate();
}
function loadTemplate() {
console.log("CALCULATE FRAGMENTS");
var startTime = new Date("2013-04-05T19:03:57"),
now = new Date(),
offset = (now.getTime() - startTime.getTime()) / 1000,
len,
num;
offset -= 20;
frags = [];
num = Math.floor(offset / (template.duration / template.timescale));
len = num + 10;
for (num; num < len; num += 1) {
frags.push(baseUrl + template.media.split("$Number$").join(num));
}
console.log("FRAGMENTS:");
console.log(frags);
console.log("START");
window.MediaSource = window.MediaSource || window.WebKitMediaSource;
ms = new MediaSource();
video = document.querySelector('video');
video.src = window.URL.createObjectURL(ms);
ms.addEventListener('webkitsourceopen', opened, false);
ms.addEventListener('webkitsourceclose', closed, false);
}
function opened() {
var value = mimetype + ';codecs="' + codec + '"',
url = baseUrl + template.initialization;
console.log("OPENED");
console.log(value);
sourceBuffer = ms.addSourceBuffer(value);
console.log("LOAD INIT");
var req = new XMLHttpRequest();
req.responseType = "arraybuffer";
req.open("GET", url, true);
req.onload = function () {
console.log("INIT DONE LOADING");
sourceBuffer.append(new Uint8Array(req.response));
loadNext();
}
req.onerror = function () {
alert("Could not load init.");
}
req.send();
}
function loadNext() {
if (loaded >= frags.length) {
console.log("EVERYTHING IS LOADED!");
return;
}
var url = frags[loaded],
i,
len,
ranges = sourceBuffer.buffered;
console.log("--STATUS-------");
console.log("CURRENT TIME: " + video.currentTime);
console.log("BUFFERED RANGES: " + ranges.length);
for (i = 0, len = ranges.length; i < len; i += 1) {
console.log("RANGE: " + ranges.start(i) + " - " + ranges.end(i));
}
console.log("---------------");
console.log("LOAD FRAGMENT: " + url);
var req = new XMLHttpRequest();
req.responseType = "arraybuffer";
req.open("GET", url, true);
req.onload = function () {
console.log("FRAGMENT DONE LOADING");
sourceBuffer.append(new Uint8Array(req.response));
if (!playbackStarted) {
playbackStarted = true;
video.play();
video.currentTime = sourceBuffer.buffered.start(0);
}
loaded += 1;
loadNext();
}
req.onerror = function () {
alert("Could not load fragment.");
}
req.send();
}
function closed() {
alert("MediaSource Closed.");
}
</script>
</head>
<body>
<button type="button" onclick="loadVideo()">Load Video</button>
<button type="button" onclick="loadAudio()">Load Audio</button>
<video controls autoplay width="320" height="240"></video>
</body>
</html>