Add h264 stream

This commit is contained in:
Kamil Trzcinski 2022-04-04 23:30:02 +02:00
parent 1f94798bee
commit 9c761f7a45
8 changed files with 3077 additions and 2 deletions

View File

@ -1,2 +1,10 @@
camera_stream: *.c *.h
gcc -g -lpthread -o camera_stream *.c
gcc -g -lpthread -o camera_stream *.c
jmuxer.min.c: jmuxer.min.js
xxd -i $< > $@.tmp
mv $@.tmp $@
video.html.c: video.html
xxd -i $< > $@.tmp
mv $@.tmp $@

2
http.h
View File

@ -29,6 +29,8 @@ typedef struct http_worker_s {
int http_server(int listen_port, int maxcons, http_method_t *methods);
void http_index(http_worker_t *worker, FILE *stream);
void http_video_html(http_worker_t *worker, FILE *stream);
void http_jmuxer_js(http_worker_t *worker, FILE *stream);
void http_404_header(http_worker_t *worker, FILE *stream);
void http_404(http_worker_t *worker, FILE *stream);

View File

@ -26,3 +26,27 @@ void http_404(http_worker_t *worker, FILE *stream)
http_404_header(worker, stream);
fprintf(stream, "Nothing here?\r\n");
}
void http_video_html(http_worker_t *worker, FILE *stream)
{
extern unsigned char video_html[];
extern unsigned int video_html_len;
fprintf(stream, "HTTP/1.1 200 OK\r\n");
fprintf(stream, "Content-Type: text/html;charset=UTF-8\r\n");
fprintf(stream, "\r\n");
fwrite(video_html, 1, video_html_len, stream);
fflush(stream);
}
void http_jmuxer_js(http_worker_t *worker, FILE *stream)
{
extern unsigned char jmuxer_min_js[];
extern unsigned int jmuxer_min_js_len;
fprintf(stream, "HTTP/1.1 200 OK\r\n");
fprintf(stream, "Content-Type: text/javascript;charset=UTF-8\r\n");
fprintf(stream, "\r\n");
fwrite(jmuxer_min_js, 1, jmuxer_min_js_len, stream);
fflush(stream);
}

2838
jmuxer.min.c Normal file

File diff suppressed because it is too large Load Diff

1
jmuxer.min.js vendored Normal file

File diff suppressed because one or more lines are too long

4
main.c
View File

@ -154,7 +154,9 @@ int main(int argc, char *argv[])
{ "GET /stream ", http_stream },
{ "GET /?action=snapshot ", http_snapshot },
{ "GET /?action=stream ", http_stream },
{ "GET /video ", http_video },
{ "GET /video ", http_video_html },
{ "GET /video.h264 ", http_video },
{ "GET /jmuxer.min.js ", http_jmuxer_js },
{ NULL, NULL }
};

83
video.html Normal file
View File

@ -0,0 +1,83 @@
<!doctype html>
<html lang="en">
<head>
<meta charset="utf-8">
<title></title>
<link rel="icon" href="data:;base64,iVBORw0KGgo=">
<style>
body{
margin:0;
padding:0;
background-color:#303030;
}
#streamStage{
position:fixed;
top:0;
left:0;
width:100%;
height:100%;
}
#streamStage:before {
content: '';
box-sizing: border-box;
position: absolute;
top: 50%;
left: 50%;
width: 2rem;
height: 2rem;
margin-top: -1rem;
margin-left: -1rem;
}
#stream{
max-height: 100%;
max-width: 100%;
margin: auto;
position: absolute;
top: 0; left: 0; bottom: 0; right: 0;
}
</style>
<script src="/jmuxer.min.js"></script>
</head>
<body>
<div id="streamtage">
<video controls autoplay muted id="stream"></video>
</div>
<body>
<script>
window.onload = function() {
var jmuxer = new JMuxer({
node: 'stream',
mode: 'video',
flushingTime: 0,
fps: 30,
debug: false
});
fetch('/video.h264').then(function(response) {
console.log(response);
const reader = response.body.getReader();
function go() {
reader.read().then(function(result) {
if (!result.done) {
if (!document.hidden){
jmuxer.feed({
video: new Uint8Array(result.value)
});
}
go ();
}
})
}
go ();
})
}
</script>
</body>
</html>

117
video.html.c Normal file
View File

@ -0,0 +1,117 @@
unsigned char video_html[] = {
0x3c, 0x21, 0x64, 0x6f, 0x63, 0x74, 0x79, 0x70, 0x65, 0x20, 0x68, 0x74,
0x6d, 0x6c, 0x3e, 0x0a, 0x0a, 0x3c, 0x68, 0x74, 0x6d, 0x6c, 0x20, 0x6c,
0x61, 0x6e, 0x67, 0x3d, 0x22, 0x65, 0x6e, 0x22, 0x3e, 0x0a, 0x3c, 0x68,
0x65, 0x61, 0x64, 0x3e, 0x0a, 0x3c, 0x6d, 0x65, 0x74, 0x61, 0x20, 0x63,
0x68, 0x61, 0x72, 0x73, 0x65, 0x74, 0x3d, 0x22, 0x75, 0x74, 0x66, 0x2d,
0x38, 0x22, 0x3e, 0x0a, 0x3c, 0x74, 0x69, 0x74, 0x6c, 0x65, 0x3e, 0x3c,
0x2f, 0x74, 0x69, 0x74, 0x6c, 0x65, 0x3e, 0x0a, 0x3c, 0x6c, 0x69, 0x6e,
0x6b, 0x20, 0x72, 0x65, 0x6c, 0x3d, 0x22, 0x69, 0x63, 0x6f, 0x6e, 0x22,
0x20, 0x68, 0x72, 0x65, 0x66, 0x3d, 0x22, 0x64, 0x61, 0x74, 0x61, 0x3a,
0x3b, 0x62, 0x61, 0x73, 0x65, 0x36, 0x34, 0x2c, 0x69, 0x56, 0x42, 0x4f,
0x52, 0x77, 0x30, 0x4b, 0x47, 0x67, 0x6f, 0x3d, 0x22, 0x3e, 0x0a, 0x3c,
0x73, 0x74, 0x79, 0x6c, 0x65, 0x3e, 0x0a, 0x62, 0x6f, 0x64, 0x79, 0x7b,
0x0a, 0x6d, 0x61, 0x72, 0x67, 0x69, 0x6e, 0x3a, 0x30, 0x3b, 0x0a, 0x70,
0x61, 0x64, 0x64, 0x69, 0x6e, 0x67, 0x3a, 0x30, 0x3b, 0x0a, 0x62, 0x61,
0x63, 0x6b, 0x67, 0x72, 0x6f, 0x75, 0x6e, 0x64, 0x2d, 0x63, 0x6f, 0x6c,
0x6f, 0x72, 0x3a, 0x23, 0x33, 0x30, 0x33, 0x30, 0x33, 0x30, 0x3b, 0x0a,
0x7d, 0x0a, 0x0a, 0x23, 0x73, 0x74, 0x72, 0x65, 0x61, 0x6d, 0x53, 0x74,
0x61, 0x67, 0x65, 0x7b, 0x0a, 0x70, 0x6f, 0x73, 0x69, 0x74, 0x69, 0x6f,
0x6e, 0x3a, 0x66, 0x69, 0x78, 0x65, 0x64, 0x3b, 0x0a, 0x74, 0x6f, 0x70,
0x3a, 0x30, 0x3b, 0x0a, 0x6c, 0x65, 0x66, 0x74, 0x3a, 0x30, 0x3b, 0x0a,
0x77, 0x69, 0x64, 0x74, 0x68, 0x3a, 0x31, 0x30, 0x30, 0x25, 0x3b, 0x0a,
0x68, 0x65, 0x69, 0x67, 0x68, 0x74, 0x3a, 0x31, 0x30, 0x30, 0x25, 0x3b,
0x0a, 0x7d, 0x0a, 0x0a, 0x23, 0x73, 0x74, 0x72, 0x65, 0x61, 0x6d, 0x53,
0x74, 0x61, 0x67, 0x65, 0x3a, 0x62, 0x65, 0x66, 0x6f, 0x72, 0x65, 0x20,
0x7b, 0x0a, 0x63, 0x6f, 0x6e, 0x74, 0x65, 0x6e, 0x74, 0x3a, 0x20, 0x27,
0x27, 0x3b, 0x0a, 0x62, 0x6f, 0x78, 0x2d, 0x73, 0x69, 0x7a, 0x69, 0x6e,
0x67, 0x3a, 0x20, 0x62, 0x6f, 0x72, 0x64, 0x65, 0x72, 0x2d, 0x62, 0x6f,
0x78, 0x3b, 0x0a, 0x70, 0x6f, 0x73, 0x69, 0x74, 0x69, 0x6f, 0x6e, 0x3a,
0x20, 0x61, 0x62, 0x73, 0x6f, 0x6c, 0x75, 0x74, 0x65, 0x3b, 0x0a, 0x74,
0x6f, 0x70, 0x3a, 0x20, 0x35, 0x30, 0x25, 0x3b, 0x0a, 0x6c, 0x65, 0x66,
0x74, 0x3a, 0x20, 0x35, 0x30, 0x25, 0x3b, 0x0a, 0x77, 0x69, 0x64, 0x74,
0x68, 0x3a, 0x20, 0x32, 0x72, 0x65, 0x6d, 0x3b, 0x0a, 0x68, 0x65, 0x69,
0x67, 0x68, 0x74, 0x3a, 0x20, 0x32, 0x72, 0x65, 0x6d, 0x3b, 0x0a, 0x6d,
0x61, 0x72, 0x67, 0x69, 0x6e, 0x2d, 0x74, 0x6f, 0x70, 0x3a, 0x20, 0x2d,
0x31, 0x72, 0x65, 0x6d, 0x3b, 0x0a, 0x6d, 0x61, 0x72, 0x67, 0x69, 0x6e,
0x2d, 0x6c, 0x65, 0x66, 0x74, 0x3a, 0x20, 0x2d, 0x31, 0x72, 0x65, 0x6d,
0x3b, 0x0a, 0x7d, 0x0a, 0x0a, 0x23, 0x73, 0x74, 0x72, 0x65, 0x61, 0x6d,
0x7b, 0x0a, 0x6d, 0x61, 0x78, 0x2d, 0x68, 0x65, 0x69, 0x67, 0x68, 0x74,
0x3a, 0x20, 0x31, 0x30, 0x30, 0x25, 0x3b, 0x0a, 0x6d, 0x61, 0x78, 0x2d,
0x77, 0x69, 0x64, 0x74, 0x68, 0x3a, 0x20, 0x31, 0x30, 0x30, 0x25, 0x3b,
0x0a, 0x6d, 0x61, 0x72, 0x67, 0x69, 0x6e, 0x3a, 0x20, 0x61, 0x75, 0x74,
0x6f, 0x3b, 0x0a, 0x70, 0x6f, 0x73, 0x69, 0x74, 0x69, 0x6f, 0x6e, 0x3a,
0x20, 0x61, 0x62, 0x73, 0x6f, 0x6c, 0x75, 0x74, 0x65, 0x3b, 0x0a, 0x74,
0x6f, 0x70, 0x3a, 0x20, 0x30, 0x3b, 0x20, 0x6c, 0x65, 0x66, 0x74, 0x3a,
0x20, 0x30, 0x3b, 0x20, 0x62, 0x6f, 0x74, 0x74, 0x6f, 0x6d, 0x3a, 0x20,
0x30, 0x3b, 0x20, 0x72, 0x69, 0x67, 0x68, 0x74, 0x3a, 0x20, 0x30, 0x3b,
0x0a, 0x7d, 0x0a, 0x3c, 0x2f, 0x73, 0x74, 0x79, 0x6c, 0x65, 0x3e, 0x0a,
0x3c, 0x73, 0x63, 0x72, 0x69, 0x70, 0x74, 0x20, 0x73, 0x72, 0x63, 0x3d,
0x22, 0x2f, 0x6a, 0x6d, 0x75, 0x78, 0x65, 0x72, 0x2e, 0x6d, 0x69, 0x6e,
0x2e, 0x6a, 0x73, 0x22, 0x3e, 0x3c, 0x2f, 0x73, 0x63, 0x72, 0x69, 0x70,
0x74, 0x3e, 0x0a, 0x3c, 0x2f, 0x68, 0x65, 0x61, 0x64, 0x3e, 0x0a, 0x3c,
0x62, 0x6f, 0x64, 0x79, 0x3e, 0x0a, 0x09, 0x3c, 0x64, 0x69, 0x76, 0x20,
0x69, 0x64, 0x3d, 0x22, 0x73, 0x74, 0x72, 0x65, 0x61, 0x6d, 0x74, 0x61,
0x67, 0x65, 0x22, 0x3e, 0x0a, 0x09, 0x09, 0x20, 0x3c, 0x76, 0x69, 0x64,
0x65, 0x6f, 0x20, 0x63, 0x6f, 0x6e, 0x74, 0x72, 0x6f, 0x6c, 0x73, 0x20,
0x61, 0x75, 0x74, 0x6f, 0x70, 0x6c, 0x61, 0x79, 0x20, 0x6d, 0x75, 0x74,
0x65, 0x64, 0x20, 0x69, 0x64, 0x3d, 0x22, 0x73, 0x74, 0x72, 0x65, 0x61,
0x6d, 0x22, 0x3e, 0x3c, 0x2f, 0x76, 0x69, 0x64, 0x65, 0x6f, 0x3e, 0x09,
0x0a, 0x09, 0x3c, 0x2f, 0x64, 0x69, 0x76, 0x3e, 0x0a, 0x3c, 0x62, 0x6f,
0x64, 0x79, 0x3e, 0x0a, 0x20, 0x20, 0x20, 0x20, 0x3c, 0x73, 0x63, 0x72,
0x69, 0x70, 0x74, 0x3e, 0x0a, 0x09, 0x77, 0x69, 0x6e, 0x64, 0x6f, 0x77,
0x2e, 0x6f, 0x6e, 0x6c, 0x6f, 0x61, 0x64, 0x20, 0x3d, 0x20, 0x66, 0x75,
0x6e, 0x63, 0x74, 0x69, 0x6f, 0x6e, 0x28, 0x29, 0x20, 0x7b, 0x0a, 0x09,
0x09, 0x76, 0x61, 0x72, 0x20, 0x6a, 0x6d, 0x75, 0x78, 0x65, 0x72, 0x20,
0x3d, 0x20, 0x6e, 0x65, 0x77, 0x20, 0x4a, 0x4d, 0x75, 0x78, 0x65, 0x72,
0x28, 0x7b, 0x0a, 0x09, 0x09, 0x09, 0x6e, 0x6f, 0x64, 0x65, 0x3a, 0x20,
0x27, 0x73, 0x74, 0x72, 0x65, 0x61, 0x6d, 0x27, 0x2c, 0x0a, 0x09, 0x09,
0x09, 0x6d, 0x6f, 0x64, 0x65, 0x3a, 0x20, 0x27, 0x76, 0x69, 0x64, 0x65,
0x6f, 0x27, 0x2c, 0x0a, 0x09, 0x09, 0x09, 0x66, 0x6c, 0x75, 0x73, 0x68,
0x69, 0x6e, 0x67, 0x54, 0x69, 0x6d, 0x65, 0x3a, 0x20, 0x30, 0x2c, 0x0a,
0x09, 0x09, 0x09, 0x66, 0x70, 0x73, 0x3a, 0x20, 0x33, 0x30, 0x2c, 0x0a,
0x09, 0x09, 0x09, 0x64, 0x65, 0x62, 0x75, 0x67, 0x3a, 0x20, 0x66, 0x61,
0x6c, 0x73, 0x65, 0x0a, 0x09, 0x09, 0x20, 0x7d, 0x29, 0x3b, 0x0a, 0x0a,
0x20, 0x20, 0x20, 0x20, 0x66, 0x65, 0x74, 0x63, 0x68, 0x28, 0x27, 0x2f,
0x76, 0x69, 0x64, 0x65, 0x6f, 0x2e, 0x68, 0x32, 0x36, 0x34, 0x27, 0x29,
0x2e, 0x74, 0x68, 0x65, 0x6e, 0x28, 0x66, 0x75, 0x6e, 0x63, 0x74, 0x69,
0x6f, 0x6e, 0x28, 0x72, 0x65, 0x73, 0x70, 0x6f, 0x6e, 0x73, 0x65, 0x29,
0x20, 0x7b, 0x0a, 0x20, 0x20, 0x20, 0x20, 0x20, 0x20, 0x63, 0x6f, 0x6e,
0x73, 0x6f, 0x6c, 0x65, 0x2e, 0x6c, 0x6f, 0x67, 0x28, 0x72, 0x65, 0x73,
0x70, 0x6f, 0x6e, 0x73, 0x65, 0x29, 0x3b, 0x0a, 0x0a, 0x20, 0x20, 0x20,
0x20, 0x20, 0x20, 0x63, 0x6f, 0x6e, 0x73, 0x74, 0x20, 0x72, 0x65, 0x61,
0x64, 0x65, 0x72, 0x20, 0x3d, 0x20, 0x72, 0x65, 0x73, 0x70, 0x6f, 0x6e,
0x73, 0x65, 0x2e, 0x62, 0x6f, 0x64, 0x79, 0x2e, 0x67, 0x65, 0x74, 0x52,
0x65, 0x61, 0x64, 0x65, 0x72, 0x28, 0x29, 0x3b, 0x0a, 0x0a, 0x20, 0x20,
0x20, 0x20, 0x20, 0x20, 0x66, 0x75, 0x6e, 0x63, 0x74, 0x69, 0x6f, 0x6e,
0x20, 0x67, 0x6f, 0x28, 0x29, 0x20, 0x7b, 0x0a, 0x20, 0x20, 0x20, 0x20,
0x20, 0x20, 0x20, 0x20, 0x72, 0x65, 0x61, 0x64, 0x65, 0x72, 0x2e, 0x72,
0x65, 0x61, 0x64, 0x28, 0x29, 0x2e, 0x74, 0x68, 0x65, 0x6e, 0x28, 0x66,
0x75, 0x6e, 0x63, 0x74, 0x69, 0x6f, 0x6e, 0x28, 0x72, 0x65, 0x73, 0x75,
0x6c, 0x74, 0x29, 0x20, 0x7b, 0x0a, 0x20, 0x20, 0x20, 0x20, 0x20, 0x20,
0x20, 0x20, 0x20, 0x20, 0x69, 0x66, 0x20, 0x28, 0x21, 0x72, 0x65, 0x73,
0x75, 0x6c, 0x74, 0x2e, 0x64, 0x6f, 0x6e, 0x65, 0x29, 0x20, 0x7b, 0x0a,
0x20, 0x20, 0x20, 0x20, 0x20, 0x20, 0x20, 0x20, 0x20, 0x20, 0x20, 0x20,
0x69, 0x66, 0x20, 0x28, 0x21, 0x64, 0x6f, 0x63, 0x75, 0x6d, 0x65, 0x6e,
0x74, 0x2e, 0x68, 0x69, 0x64, 0x64, 0x65, 0x6e, 0x29, 0x7b, 0x0a, 0x20,
0x20, 0x20, 0x20, 0x20, 0x20, 0x20, 0x20, 0x20, 0x20, 0x20, 0x20, 0x20,
0x20, 0x6a, 0x6d, 0x75, 0x78, 0x65, 0x72, 0x2e, 0x66, 0x65, 0x65, 0x64,
0x28, 0x7b, 0x0a, 0x20, 0x20, 0x20, 0x20, 0x20, 0x20, 0x20, 0x20, 0x20,
0x20, 0x20, 0x20, 0x20, 0x20, 0x20, 0x20, 0x76, 0x69, 0x64, 0x65, 0x6f,
0x3a, 0x20, 0x6e, 0x65, 0x77, 0x20, 0x55, 0x69, 0x6e, 0x74, 0x38, 0x41,
0x72, 0x72, 0x61, 0x79, 0x28, 0x72, 0x65, 0x73, 0x75, 0x6c, 0x74, 0x2e,
0x76, 0x61, 0x6c, 0x75, 0x65, 0x29, 0x0a, 0x20, 0x20, 0x20, 0x20, 0x20,
0x20, 0x20, 0x20, 0x20, 0x20, 0x20, 0x20, 0x20, 0x20, 0x7d, 0x29, 0x3b,
0x0a, 0x20, 0x20, 0x20, 0x20, 0x20, 0x20, 0x20, 0x20, 0x20, 0x20, 0x20,
0x20, 0x7d, 0x0a, 0x20, 0x20, 0x20, 0x20, 0x20, 0x20, 0x20, 0x20, 0x20,
0x20, 0x20, 0x20, 0x67, 0x6f, 0x20, 0x28, 0x29, 0x3b, 0x0a, 0x20, 0x20,
0x20, 0x20, 0x20, 0x20, 0x20, 0x20, 0x20, 0x20, 0x7d, 0x0a, 0x20, 0x20,
0x20, 0x20, 0x20, 0x20, 0x20, 0x20, 0x7d, 0x29, 0x0a, 0x20, 0x20, 0x20,
0x20, 0x20, 0x20, 0x7d, 0x0a, 0x0a, 0x20, 0x20, 0x20, 0x20, 0x20, 0x20,
0x67, 0x6f, 0x20, 0x28, 0x29, 0x3b, 0x0a, 0x20, 0x20, 0x20, 0x20, 0x7d,
0x29, 0x0a, 0x09, 0x7d, 0x20, 0x20, 0x20, 0x20, 0x20, 0x0a, 0x20, 0x20,
0x20, 0x20, 0x3c, 0x2f, 0x73, 0x63, 0x72, 0x69, 0x70, 0x74, 0x3e, 0x0a,
0x3c, 0x2f, 0x62, 0x6f, 0x64, 0x79, 0x3e, 0x0a, 0x3c, 0x2f, 0x68, 0x74,
0x6d, 0x6c, 0x3e
};
unsigned int video_html_len = 1359;