...
Table of Contents |
---|
Example of stream convertion to HLS and playing it in browser supporting HLS natively
The player shows how to convert stream published on WCS server to HLS and play it in browser. HLS stream cut starts automatically when strea is requested by HLS URL, for example https://demo.flashphoner.com:8445/test/test.m3u8 on the screenshot below
The code of the example
The source code can be accessed on server by the following path:
...
Where host is WCS server address
Analyzing the code
...
To analize the code get hlsget hls-native.js file version with hash 51703a2 ecbadc3 which is available available here and can be downloaded in build build 2.0.5.28.2753.141212.
1. A server HLS URL detection
getHLSUrl() code
Code Block | ||||
---|---|---|---|---|
| ||||
function initPage() { $("#header").text("HLS Native Player Minimal"); $("#urlServer").val(getHLSUrl()); ... } |
2. div element setup to pass to the player
A div element for stream playback is passed to player
...
3. Stream name detection (the stream should be published to server)
encodeURIComponent() code
Code Block | ||||
---|---|---|---|---|
| ||||
function playBtnClick() { if (validateForm()) { var streamName = $('#playStream').val(); streamName = encodeURIComponent(streamName); ... } } |
4. HLS stream URL forming
If authentication key and token are set, they will be included to stream URL
Code Block | ||||
---|---|---|---|---|
| ||||
function playBtnClick() { if (validateForm()) { ... var videoSrc = $("#urlServer").val() + '/' + streamName + '/' + streamName + '.m3u8'; var key = $('#key').val(); var token = $("#token").val(); if (key.length > 0 && token.length > 0) { videoSrc += "?" + key + "=" + token; } ... } } |
5. Player starting
If browser does not support HLS playback natively, player will not be launched and a warning will be displayed
Code Block | ||||
---|---|---|---|---|
| ||||
function playBtnClick() { if (validateForm()) { ... if (remoteVideo.canPlayType('application/vnd.apple.mpegurl')) { remoteVideo.src = videoSrc; remoteVideo.addEventListener('loadedmetadata', function() { console.log("Play native HLS"); remoteVideo.play(); onStarted(); }); } else { $("#notifyFlash").text("Your browser doesn't support native HLS playback"); } } } |
6. Playback stopping
Code Block | ||||
---|---|---|---|---|
| ||||
function stopBtnClick() { if (remoteVideo != null) { console.log("Stop HTML5 player"); remoteVideo.pause(); remoteVideo.currentTime = 0; remoteVideo.removeAttribute('src'); remoteVideo.load(); } onStopped(); } |