-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathhls4all.html
212 lines (155 loc) · 6.13 KB
/
hls4all.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
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
<html>
<head>
<!-- rm *.webm ; ~/src/bin/ffmpeg -y -f video4linux2 -i /dev/video0 -c:v libvpx -quality realtime -g 1 -map 0 -f segment -segment_list playlist.local.txt -segment_time 10 %d.webm -->
<!-- xclock -update 1 -->
<!-- export LD_LIBRARY_PATH=/home/servetti/src/lib/ -->
<!-- ssh-add /home/servetti/.ssh/[email protected] -->
<!-- sh hls4all_purge.sh; ~/src/bin/ffmpeg -y -f x11grab -s 164x164 -i :0.0+66,885 -r 30 -c:v libvpx -g 1 -map 0 -quality realtime -f segment -segment_list playlist.local.txt -segment_time 10 %d.webm -->
<!-- tail -f playlist.local.txt | ( old1="tmp"; old2="tmp"; old3="tmp"; while read filename ; do bash hls4all_transfer.sh $filename $old3 $old2 $old1; old3=$old2; old2=$old1; old1=$filename ; done ) -->
<!--
# hls4all_purge.sh
rm *.webm
at now +1 day -f hls4all_purge.sh
-->
<!--
# hls4all_transfer.sh
FILENAME=$1;
RMFILENAME=$2;
NARGS=$#;
> playlist.txt
for ((i=3; i<=$NARGS; i++)); do
echo "${!i}" >> playlist.txt
done
echo "$FILENAME" >> playlist.txt
( echo "cd htdocs" ; echo "cd hls4all" ; echo "put $FILENAME" ; echo "put playlist.txt" ; echo "rm $RMFILENAME" ) | sftp -b - [email protected]
-->
<script type="text/javascript">
var xmlhttp = new XMLHttpRequest();
var video_elems = new Array();
var seg_history = new Array();
var segment_delay = 0; // measure: segments
var lines;
var cnt;
var last;
var dejitter_delay = 3000; // ms
var hls4all_div;
function load(cur) {
var txt = "Playing " + last + " of: ";
last = getNext(last);
video_elems[cur].src = last;
video_elems[cur].mysrc = last;
seg_history[video_elems[cur].mysrc] = new Object();
var ts = new Date().getTime();
console.log("Prebuffering: " + last + " on " + cur + " at " + ts);
seg_history[video_elems[cur].mysrc].prebuffering = ts;
xmlhttp = new XMLHttpRequest();
xmlhttp.open("GET","playlist.txt",false); // the false makes this synchronous!
xmlhttp.send(null);
var text = xmlhttp.responseText;
lines = text.split("\n");
for(var k=0;k<lines.length;k++) { txt += lines[k] + " "; } console.log(txt);
}
function swap(evt) {
var cur = this.n, next;
if(cur == 0) { next = 1; } else { next = 0; }
console.log("Current ended at: " + video_elems[cur].currentTime);
video_elems[next].play();
console.log("Segment " + video_elems[next].mysrc + " prebuffered at " + seg_history[video_elems[next].mysrc].prebuffering + " last " + seg_history[video_elems[next].mysrc].duration + ".");
video_elems[cur].style.display = "none";
video_elems[next].style.display = "inline";
document.getElementById("playing").innerHTML = "Playing: " + video_elems[next].src;
//video_elems[cur].src = last;
// FIXME: trigger on playout start of first segment
setTimeout( function() {
console.log("Current time: " + video_elems[next].currentTime + " of " + next);
load(cur);
}, 9900);
}
function getNext(last) { return last.replace(/\d+/, function(val) { return parseInt(val,10)+1; }) }
function showDuration(e) {
var velm = this;
seg_history[velm.mysrc].duration = velm.duration;
document.getElementById("duration_").innerHTML = "of " + velm.mysrc + " is " + seg_history[velm.mysrc].duration + " (" + seg_history[velm.mysrc].prebuffering + ")";
}
function startPlayout(velms) {
console.log("Starting playout");
// Register callbacks
velms[0].addEventListener("ended",swap,false);
velms[1].addEventListener("ended",swap,false);
velms[0].addEventListener('timeupdate',eventTime);
velms[1].addEventListener('timeupdate',eventTime);
velms[0].addEventListener('loadstart',loadStart);
velms[1].addEventListener('loadstart',loadStart);
velms[0].addEventListener('durationchange', showDuration );
velms[1].addEventListener('durationchange', showDuration );
velms[0].src = last;
cnt = 2;
velms[0].play();
document.getElementById("playing").innerHTML = "Playing: " + last;
var txt = "Playing " + last + " of: ";
for(var k=0;k<lines.length;k++) { txt += lines[k] + " "; } console.log(txt);
setTimeout( function() {
console.log("Current time: " + velms[0].currentTime);
load(1);
}, 9900);
}
window.onload = function () {
hls4all_div = document.getElementById("hls4all");
var vel0 = document.createElement("video");
var vel1 = document.createElement("video");
vel0.style.display = "inline";
vel0.style.border = "10px solid red";
vel0.n = 0;
vel1.style.display = "none";
vel1.style.border = "10px solid green";
vel1.n = 1;
hls4all_div.appendChild(vel0);
hls4all_div.appendChild(vel1);
video_elems[0] = vel0;
video_elems[1] = vel1;
xmlhttp.open("GET","playlist.txt",false); // the false makes this synchronous!
xmlhttp.send(null);
var text = xmlhttp.responseText;
// but we want to process that one line at a time.
lines = text.split("\n");
if(lines.length >= (segment_delay+2)) {
// start playout
last = lines[lines.length-(segment_delay+2)];
setTimeout( startPlayout(video_elems), dejitter_delay);
console.log("Waiting for dejitter");
} else {
// no video available
alert("Live streaming not jet started, please try again later.");
}
setInterval( function () {
var dt = new Date();
document.getElementById("showTime_").innerHTML = dt.toLocaleTimeString();
}, 1000);
}
function eventTime() {
document.getElementById("currentTime").innerHTML = this.currentTime;
};
function loadStart() {
console.log("loadstart on: " + this.src);
};
</script>
</head>
<body>
<div style="margin: auto; width: 800px; text-align: center;">
<h1>Custom HTTP pull streaming proof of concept</h1>
<p style="text-align: left;" id="playing">Playing: </p>
<p style="text-align: left;">CurrentTime: <span id="currentTime"></span></p>
<p style="text-align: left;">Playlist: <span id="playlist_">_</span></p>
<p style="text-align: left;">Duration: <span id="duration_"></span></p>
<p style="text-align: left;">System time: <span id="showTime_"></span></p>
<div id='hls4all'></div>
<!--
<video id="video1" n="0" style="display: inline; border: 10px solid red;">
<source src=""/>
</video>
<video id="video2" n="1"style="display: none; border: 10px solid green;">
<source src=""/>
</video>
-->
</body>
</html>