Skip to content

Commit

Permalink
Merge pull request #88 from log-to-code/update-examples
Browse files Browse the repository at this point in the history
Update examples
  • Loading branch information
deepch authored Feb 16, 2022
2 parents b1d9030 + aff184f commit 44cf6c4
Show file tree
Hide file tree
Showing 6 changed files with 164 additions and 257 deletions.
51 changes: 14 additions & 37 deletions docs/examples/hls/index.html
Original file line number Diff line number Diff line change
Expand Up @@ -3,54 +3,31 @@
<head>
<meta charset="utf-8">
<title>RTSPtoWeb HLS example</title>

<style type="text/css">
video { border: 1px solid black; }
</style>
</head>
<body>
<h1>RTSPtoWeb HLS example</h1>

<input type="hidden" name="hls-url" id="hls-url"
value="http://localhost:8083/stream/demo/channel/0/hls/live/index.m3u8">

<video id="hls-video" autoplay muted playsinline width="100%" controls="controls"></video>
<video id="hls-video" autoplay muted playsinline controls
style="max-width: 100%; max-height: 100%;"></video>

<script>
document.addEventListener('DOMContentLoaded', function() {
let videoEl = document.querySelector('#hls-video');
let url = document.querySelector('#hls-url').value;

videoEl.addEventListener('loadeddata', () => {
videoEl.play();
});

videoEl.addEventListener('error', (e) => {
console.log('video_error', e)
});

if (videoEl.canPlayType('application/vnd.apple.mpegurl')) {
videoEl.src = url;
} else if (Hls.isSupported()) {
var hls = new Hls();
hls.attachMedia(videoEl);
hls.on(Hls.Events.MEDIA_ATTACHED, function () {
hls.loadSource(url);
hls.on(Hls.Events.MANIFEST_PARSED, function (event, data) {
console.log(
'manifest loaded, found ' + data.levels.length + ' quality level'
);
});
});
document.addEventListener('DOMContentLoaded', function () {
const videoEl = document.querySelector('#hls-video')
const hlsUrl = document.querySelector('#hls-url').value

if (Hls.isSupported()) {
const hls = new Hls()
hls.loadSource(hlsUrl)
hls.attachMedia(videoEl)
} else if (video.canPlayType('application/vnd.apple.mpegurl')) {
videoEl.src = hlsUrl
}
});
})
</script>

<script
src="https://cdnjs.cloudflare.com/ajax/libs/hls.js/1.0.7/hls.min.js"
integrity="sha512-t2KkWPX3gnw9G4Wx4NYq3XISm5485OOojISxYYIqVXMEpkQy8oBvFBSvLjatPYNxrOcuSrd2Y83y27yEtNYbEw=="
crossorigin="anonymous"
referrerpolicy="no-referrer">
</script>
<script src="https://cdn.jsdelivr.net/npm/hls.js@latest"></script>
</body>
</html>
51 changes: 14 additions & 37 deletions docs/examples/hlsll/index.html
Original file line number Diff line number Diff line change
Expand Up @@ -3,54 +3,31 @@
<head>
<meta charset="utf-8">
<title>RTSPtoWeb HLS-LL example</title>

<style type="text/css">
video { border: 1px solid black; }
</style>
</head>
<body>
<h1>RTSPtoWeb HLS-LL example</h1>

<input type="hidden" name="hlsll-url" id="hlsll-url"
value="http://localhost:8083/stream/demo/channel/0/hlsll/live/index.m3u8">

<video id="hlsll-video" autoplay muted playsinline width="100%" controls="controls"></video>
<video id="hlsll-video" autoplay muted playsinline controls
style="max-width: 100%; max-height: 100%;"></video>

<script>
document.addEventListener('DOMContentLoaded', function() {
let videoEl = document.querySelector('#hlsll-video');
let url = document.querySelector('#hlsll-url').value;

videoEl.addEventListener('loadeddata', () => {
videoEl.play();
});

videoEl.addEventListener('error', (e) => {
console.log('video_error', e)
});

if (videoEl.canPlayType('application/vnd.apple.mpegurl')) {
videoEl.src = url;
} else if (Hls.isSupported()) {
var hls = new Hls();
hls.attachMedia(videoEl);
hls.on(Hls.Events.MEDIA_ATTACHED, function () {
hls.loadSource(url);
hls.on(Hls.Events.MANIFEST_PARSED, function (event, data) {
console.log(
'manifest loaded, found ' + data.levels.length + ' quality level'
);
});
});
document.addEventListener('DOMContentLoaded', function () {
const videoEl = document.querySelector('#hlsll-video')
const hlsllUrl = document.querySelector('#hlsll-url').value

if (Hls.isSupported()) {
const hls = new Hls()
hls.loadSource(hlsllUrl)
hls.attachMedia(videoEl)
} else if (video.canPlayType('application/vnd.apple.mpegurl')) {
videoEl.src = hlsllUrl
}
});
})
</script>

<script
src="https://cdnjs.cloudflare.com/ajax/libs/hls.js/1.0.7/hls.min.js"
integrity="sha512-t2KkWPX3gnw9G4Wx4NYq3XISm5485OOojISxYYIqVXMEpkQy8oBvFBSvLjatPYNxrOcuSrd2Y83y27yEtNYbEw=="
crossorigin="anonymous"
referrerpolicy="no-referrer">
</script>
<script src="https://cdn.jsdelivr.net/npm/hls.js@latest"></script>
</body>
</html>
103 changes: 3 additions & 100 deletions docs/examples/mse/index.html
Original file line number Diff line number Diff line change
Expand Up @@ -3,113 +3,16 @@
<head>
<meta charset="utf-8">
<title>RTSPtoWeb MSE example</title>

<style type="text/css">
video { border: 1px solid black; }
</style>
</head>
<body>
<h1>RTSPtoWeb MSE example</h1>

<input type="hidden" name="mse-url" id="mse-url"
value="ws://localhost:8083/stream/demo/channel/0/mse?uuid=demo&channel=0">

<video id="mse-video" autoplay muted playsinline width="100%"></video>

<script>
// Extracted from RTSPtoWeb

let mseQueue = [],
mseSourceBuffer,
mseStreamingStarted = false;

function startPlay() {
let videoEl = document.querySelector('#mse-video');
let url = document.querySelector('#mse-url').value;

location.protocol == 'https:' ? protocol = 'wss' : protocol = 'ws';
let mse = new MediaSource();
videoEl.src = window.URL.createObjectURL(mse);
mse.addEventListener('sourceopen', function() {
let ws = new WebSocket(url);
ws.binaryType = 'arraybuffer';
ws.onopen = function(event) {
console.log('Connect to ws');
}
ws.onmessage = function(event) {
let data = new Uint8Array(event.data);
if (data[0] == 9) {
decoded_arr = data.slice(1);
if (window.TextDecoder) {
mimeCodec = new TextDecoder('utf-8').decode(decoded_arr);
} else {
mimeCodec = Utf8ArrayToStr(decoded_arr);
}
mseSourceBuffer = mse.addSourceBuffer('video/mp4; codecs="' + mimeCodec + '"');
mseSourceBuffer.mode = 'segments'
mseSourceBuffer.addEventListener('updateend', pushPacket);

} else {
readPacket(event.data);
}
};
}, false);

}

function pushPacket() {
let videoEl = document.querySelector('#mse-video');

if (!mseSourceBuffer.updating) {
if (mseQueue.length > 0) {
packet = mseQueue.shift();
mseSourceBuffer.appendBuffer(packet);
} else {
mseStreamingStarted = false;
}
}
if (videoEl.buffered.length > 0) {
if (typeof document.hidden !== 'undefined' && document.hidden) {
//no sound, browser paused video without sound in background
videoEl.currentTime = videoEl.buffered.end((videoEl.buffered.length - 1)) - 0.5;
}
}
}

function readPacket(packet) {
if (!mseStreamingStarted) {
mseSourceBuffer.appendBuffer(packet);
mseStreamingStarted = true;
return;
}
mseQueue.push(packet);
if (!mseSourceBuffer.updating) {
pushPacket();
}
}

document.addEventListener('DOMContentLoaded', function() {
let videoEl = document.querySelector('#mse-video');

videoEl.addEventListener('loadeddata', () => {
videoEl.play();
});

//fix stalled video in safari
videoEl.addEventListener('pause', () => {
if (videoEl.currentTime > videoEl.buffered.end(videoEl.buffered.length - 1)) {
videoEl.currentTime = videoEl.buffered.end(videoEl.buffered.length - 1) - 0.1;
videoEl.play();
}
});

videoEl.addEventListener('error', (e) => {
console.log('video_error', e)
});

startPlay();
});
</script>
<video id="mse-video" autoplay muted playsinline controls
style="max-width: 100%; max-height: 100%;"></video>

<script src="main.js"></script>
</body>
</html>
78 changes: 78 additions & 0 deletions docs/examples/mse/main.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,78 @@
document.addEventListener('DOMContentLoaded', function () {
const mseQueue = []
let mseSourceBuffer
let mseStreamingStarted = false

function startPlay (videoEl, url) {
const mse = new MediaSource()
videoEl.src = window.URL.createObjectURL(mse)
mse.addEventListener('sourceopen', function () {
const ws = new WebSocket(url)
ws.binaryType = 'arraybuffer'
ws.onopen = function (event) {
console.log('Connect to ws')
}
ws.onmessage = function (event) {
const data = new Uint8Array(event.data)
if (data[0] === 9) {
let mimeCodec
const decodedArr = data.slice(1)
if (window.TextDecoder) {
mimeCodec = new TextDecoder('utf-8').decode(decodedArr)
} else {
mimeCodec = Utf8ArrayToStr(decodedArr)
}
mseSourceBuffer = mse.addSourceBuffer('video/mp4; codecs="' + mimeCodec + '"')
mseSourceBuffer.mode = 'segments'
mseSourceBuffer.addEventListener('updateend', pushPacket)
} else {
readPacket(event.data)
}
}
}, false)
}

function pushPacket () {
const videoEl = document.querySelector('#mse-video')
let packet

if (!mseSourceBuffer.updating) {
if (mseQueue.length > 0) {
packet = mseQueue.shift()
mseSourceBuffer.appendBuffer(packet)
} else {
mseStreamingStarted = false
}
}
if (videoEl.buffered.length > 0) {
if (typeof document.hidden !== 'undefined' && document.hidden) {
// no sound, browser paused video without sound in background
videoEl.currentTime = videoEl.buffered.end((videoEl.buffered.length - 1)) - 0.5
}
}
}

function readPacket (packet) {
if (!mseStreamingStarted) {
mseSourceBuffer.appendBuffer(packet)
mseStreamingStarted = true
return
}
mseQueue.push(packet)
if (!mseSourceBuffer.updating) {
pushPacket()
}
}
const videoEl = document.querySelector('#mse-video')
const mseUrl = document.querySelector('#mse-url').value

// fix stalled video in safari
videoEl.addEventListener('pause', () => {
if (videoEl.currentTime > videoEl.buffered.end(videoEl.buffered.length - 1)) {
videoEl.currentTime = videoEl.buffered.end(videoEl.buffered.length - 1) - 0.1
videoEl.play()
}
})

startPlay(videoEl, mseUrl)
})
Loading

0 comments on commit 44cf6c4

Please sign in to comment.