improved video playback

post and get request lib
This commit is contained in:
lukas-heiligenbrunner 2020-03-01 10:15:32 +01:00
parent fd84ddb42f
commit fad3fe019f
8 changed files with 65 additions and 18 deletions

View File

@ -47,11 +47,8 @@ class MainPage : HttpHandler {
val s = path.substring(path.length - 3)
if (s == ".js") mime = "application/javascript"
if (s == "css") mime = "text/css"
if (s == "mp4") {
mime = "text/css"
sendVideo(fs, t)
return
}
if (s == "mp4") return sendVideo(fs, t) // special reply on videos
val h = t.responseHeaders
h["Content-Type"] = mime
@ -60,19 +57,16 @@ class MainPage : HttpHandler {
val buffer = ByteArray(0x10000)
var count: Int
// todo check if output stream is even open
// fs.skip(4096)
while (fs.read(buffer).also { count = it } >= 0) {
os.write(buffer, 0, count)
}
debug("left loop!")
fs.close()
os.close()
}
}
private fun sendVideo(fs: InputStream, t: HttpExchange) {
var mime = "video/mp4"
val size = fs.available()
var offset = 0
t.requestHeaders["Range"]?.get(0)?.apply {
offset = Integer.valueOf(this.substring(this.indexOf("bytes=") + 6, this.indexOf("-")))
@ -80,24 +74,29 @@ class MainPage : HttpHandler {
println(offset)
val h = t.responseHeaders
h["Content-Type"] = "video/mp4"
h["Content-Length"] = "${145310237 - offset}"
h["Content-Length"] = "${size - offset}"
h["Accept-Ranges"] = "bytes"
if (offset != 0) {
h["Content-Range"] = "bytes $offset-145310236/145310237"
h["Content-Range"] = "bytes $offset-${size - 1}/$size"
// send back partial header on partial data!
t.sendResponseHeaders(206, 0)
}
else {
// send back normal 200 status code on normal request
t.sendResponseHeaders(200, 0)
}
t.sendResponseHeaders(200, 0)
val os = t.responseBody
val buffer = ByteArray(1024)
var count: Int
// todo check if output stream is even open
// fs.skip(offset.toLong())
// skip offset bytes
fs.skip(offset.toLong())
while (fs.read(buffer).also { count = it } >= 0) {
os.write(buffer, 0, count)
}

View File

@ -3,6 +3,7 @@ package eu.heili.hometheater.website
import com.sun.net.httpserver.HttpServer
import eu.heili.hometheater.basicutils.Log.Log.criticalerror
import eu.heili.hometheater.basicutils.Log.Log.info
import eu.heili.hometheater.website.datarequests.TestData
import java.io.IOException
import java.net.BindException
import java.net.InetSocketAddress
@ -13,6 +14,7 @@ class Webserver {
try {
val server = HttpServer.create(InetSocketAddress(8080), 0)
server.createContext("/", MainPage())
server.createContext("/data/test", TestData())
// todo insert get and post request sites here!
server.executor = java.util.concurrent.Executors.newCachedThreadPool() // creates a default executor
server.start()

View File

@ -1,4 +1,4 @@
package com.wasteinformationserver.website.basicrequest
package eu.heili.hometheater.website.basicrequest
import com.sun.net.httpserver.HttpExchange
import com.sun.net.httpserver.HttpHandler

View File

@ -1,4 +1,4 @@
package com.wasteinformationserver.website.basicrequest
package eu.heili.hometheater.website.basicrequest
import com.sun.net.httpserver.HttpExchange
import com.sun.net.httpserver.HttpHandler

View File

@ -0,0 +1,10 @@
package eu.heili.hometheater.website.datarequests
import eu.heili.hometheater.website.basicrequest.PostRequest
import java.util.HashMap
class TestData : PostRequest() {
override fun request(params: HashMap<String, String>): String {
return """{"status":"ok"}"""
}
}

View File

@ -2,9 +2,13 @@
<html lang="en">
<head>
<meta charset="UTF-8">
<script src="js/BaseRequest.js"></script>
<script src="js/index.js"></script>
<title>Home-Theater</title>
</head>
<body>
main body
<div class="mediawrapper">
</div>
</body>
</html>

View File

@ -0,0 +1,27 @@
class Req {
static post(url, data, callback) {
const xhttp = new XMLHttpRequest();
xhttp.onreadystatechange = function(){
if (this.readyState == 4 && this.status == 200) {
callback(JSON.parse(xhttp.responseText));
}
}
xhttp.open("POST",url,true);
xhttp.send(data);
}
static get(url, callback) {
const xhttp = new XMLHttpRequest();
xhttp.onreadystatechange = function(){
if (this.readyState == 4 && this.status == 200) {
callback(JSON.parse(xhttp.responseText));
}
}
xhttp.open("GET",url,true);
xhttp.send();
}
static ready(callback){
document.addEventListener('DOMContentLoaded', callback);
}
}

View File

@ -0,0 +1,5 @@
Req.ready((event) => {
Req.post("/data/test","action=test",function (dta) {
console.log(dta);
});
});