Organize sources
This commit is contained in:
144
http/http.c
Normal file
144
http/http.c
Normal file
@ -0,0 +1,144 @@
|
||||
#include <unistd.h>
|
||||
#include <stdio.h>
|
||||
#include <stdlib.h>
|
||||
#include <sys/types.h>
|
||||
#include <sys/socket.h>
|
||||
#include <netinet/in.h>
|
||||
#include <netinet/ip.h>
|
||||
#include <netinet/tcp.h>
|
||||
#include <arpa/inet.h>
|
||||
|
||||
#include "http/http.h"
|
||||
|
||||
#define BUFSIZE 256
|
||||
|
||||
static int http_listen(int listen_port, int maxcons)
|
||||
{
|
||||
struct sockaddr_in server = {0};
|
||||
int listenfd = -1;
|
||||
|
||||
// getaddrinfo for host
|
||||
server.sin_family = AF_INET;
|
||||
server.sin_addr.s_addr = INADDR_ANY;
|
||||
server.sin_port = htons(listen_port);
|
||||
|
||||
listenfd = socket(AF_INET, SOCK_STREAM, 0);
|
||||
if (listenfd < 0) {
|
||||
perror("socket");
|
||||
return -1;
|
||||
}
|
||||
|
||||
int optval = 1;
|
||||
setsockopt(listenfd, SOL_SOCKET, SO_REUSEADDR, &optval, sizeof(int));
|
||||
|
||||
if (bind(listenfd, (struct sockaddr *)&server, sizeof(server)) < 0) {
|
||||
perror("bind");
|
||||
goto error;
|
||||
}
|
||||
|
||||
if (listen(listenfd, maxcons) < 0) {
|
||||
perror("listen");
|
||||
goto error;
|
||||
}
|
||||
|
||||
return listenfd;
|
||||
error:
|
||||
if (listenfd >= 0)
|
||||
close(listenfd);
|
||||
return -1;
|
||||
}
|
||||
|
||||
static void http_process(http_worker_t *worker, FILE *stream)
|
||||
{
|
||||
// Read headers
|
||||
if (!fgets(worker->client_method, sizeof(worker->client_method), stream)) {
|
||||
return;
|
||||
}
|
||||
|
||||
// Consume headers
|
||||
for(int i = 0; i < 50; i++) {
|
||||
char line[BUFSIZE];
|
||||
if (!fgets(line, BUFSIZE, stream))
|
||||
return;
|
||||
if (line[0] == '\r' && line[1] == '\n')
|
||||
break;
|
||||
}
|
||||
|
||||
for (int i = 0; worker->methods[i].name; i++) {
|
||||
if (strstr(worker->client_method, worker->methods[i].name)) {
|
||||
worker->methods[i].func(worker, stream);
|
||||
return;
|
||||
}
|
||||
}
|
||||
|
||||
http_404(worker, stream);
|
||||
}
|
||||
|
||||
static void http_client(http_worker_t *worker)
|
||||
{
|
||||
worker->client_host = inet_ntoa(worker->client_addr.sin_addr);
|
||||
printf("Client connected %s.\n", worker->client_host);
|
||||
|
||||
struct timeval tv;
|
||||
tv.tv_sec = 3;
|
||||
tv.tv_usec = 0;
|
||||
setsockopt(worker->client_fd, SOL_SOCKET, SO_RCVTIMEO, (const char*)&tv, sizeof(tv));
|
||||
setsockopt(worker->client_fd, SOL_SOCKET, SO_SNDTIMEO, (const char*)&tv, sizeof(tv));
|
||||
|
||||
int on = 1;
|
||||
setsockopt(worker->client_fd, IPPROTO_TCP, TCP_NODELAY, (void *)&on, sizeof(on));
|
||||
setsockopt(worker->client_fd, SOL_SOCKET, SO_KEEPALIVE, (void *)&on, sizeof(on));
|
||||
|
||||
FILE *stream = fdopen(worker->client_fd, "r+");
|
||||
if (stream) {
|
||||
http_process(worker, stream);
|
||||
fclose(stream);
|
||||
}
|
||||
|
||||
close(worker->client_fd);
|
||||
worker->client_fd = -1;
|
||||
|
||||
printf("Client disconnected %s.\n", worker->client_host);
|
||||
worker->client_host = NULL;
|
||||
}
|
||||
|
||||
static int http_worker(http_worker_t *worker)
|
||||
{
|
||||
while (1) {
|
||||
int addrlen = sizeof(worker->client_addr);
|
||||
worker->client_fd = accept(worker->listen_fd, (struct sockaddr *)&worker->client_addr, &addrlen);
|
||||
if (worker->client_fd < 0) {
|
||||
goto error;
|
||||
}
|
||||
|
||||
http_client(worker);
|
||||
}
|
||||
|
||||
error:
|
||||
free(worker->name);
|
||||
free(worker);
|
||||
return -1;
|
||||
}
|
||||
|
||||
int http_server(int listen_port, int maxcons, http_method_t *methods)
|
||||
{
|
||||
int listen_fd = http_listen(9092, maxcons);
|
||||
if (listen_fd < 0) {
|
||||
return -1;
|
||||
}
|
||||
|
||||
while (maxcons-- > 0) {
|
||||
char name[20];
|
||||
sprintf(name, "HTTP%d/%d", listen_port, maxcons);
|
||||
|
||||
http_worker_t *worker = calloc(1, sizeof(http_worker_t));
|
||||
worker->name = strdup(name);
|
||||
worker->listen_fd = listen_fd;
|
||||
worker->methods = methods;
|
||||
worker->client_fd = -1;
|
||||
pthread_create(&worker->thread, NULL, (void *(*)(void*))http_worker, worker);
|
||||
}
|
||||
|
||||
return listen_fd;
|
||||
}
|
||||
|
49
http/http.h
Normal file
49
http/http.h
Normal file
@ -0,0 +1,49 @@
|
||||
#pragma once
|
||||
|
||||
#include <stdbool.h>
|
||||
#include <stdio.h>
|
||||
#include <stdlib.h>
|
||||
#include <string.h>
|
||||
#include <netinet/ip.h>
|
||||
|
||||
typedef struct buffer_s buffer_t;
|
||||
typedef struct http_worker_s http_worker_t;
|
||||
|
||||
typedef void (*http_method_fn)(struct http_worker_s *worker, FILE *stream);
|
||||
|
||||
typedef struct http_method_s {
|
||||
const char *name;
|
||||
http_method_fn func;
|
||||
} http_method_t;
|
||||
|
||||
typedef struct http_worker_s {
|
||||
char *name;
|
||||
int listen_fd;
|
||||
http_method_t *methods;
|
||||
pthread_t thread;
|
||||
|
||||
int client_fd;
|
||||
struct sockaddr_in client_addr;
|
||||
char *client_host;
|
||||
char client_method[256];
|
||||
} http_worker_t;
|
||||
|
||||
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);
|
||||
|
||||
// M-JPEG
|
||||
void http_snapshot(http_worker_t *worker, FILE *stream);
|
||||
void http_stream(http_worker_t *worker, FILE *stream);
|
||||
void http_jpeg_capture(struct buffer_s *buf);
|
||||
bool http_jpeg_needs_buffer();
|
||||
|
||||
// H264
|
||||
void http_h264_capture(buffer_t *buf);
|
||||
void http_video(http_worker_t *worker, FILE *stream);
|
||||
bool http_h264_needs_buffer();
|
||||
|
61
http/http_h264.c
Normal file
61
http/http_h264.c
Normal file
@ -0,0 +1,61 @@
|
||||
#include <stdio.h>
|
||||
#include <stdlib.h>
|
||||
|
||||
#include "http/http.h"
|
||||
#include "hw/buffer.h"
|
||||
#include "hw/buffer_lock.h"
|
||||
#include "hw/buffer_list.h"
|
||||
#include "hw/device.h"
|
||||
|
||||
DEFINE_BUFFER_LOCK(http_h264);
|
||||
|
||||
static const char *const VIDEO_HEADER = "HTTP/1.0 200 OK\r\n"
|
||||
"Access-Control-Allow-Origin: *\r\n"
|
||||
"Connection: close\r\n"
|
||||
"Content-Type: video/webm;codecs=h264\r\n"
|
||||
"\r\n";
|
||||
|
||||
void http_h264_capture(buffer_t *buf)
|
||||
{
|
||||
buffer_lock_capture(&http_h264, buf);
|
||||
}
|
||||
|
||||
bool http_h264_needs_buffer()
|
||||
{
|
||||
return buffer_lock_needs_buffer(&http_h264);
|
||||
}
|
||||
|
||||
void http_video(http_worker_t *worker, FILE *stream)
|
||||
{
|
||||
bool had_key_frame = false;
|
||||
bool requested_key_frame = false;
|
||||
int counter = 0;
|
||||
fprintf(stream, VIDEO_HEADER);
|
||||
|
||||
buffer_lock_use(&http_h264, 1);
|
||||
|
||||
while (!feof(stream)) {
|
||||
buffer_t *buf = buffer_lock_get(&http_h264, 3, &counter);
|
||||
if (!buf) {
|
||||
goto error;
|
||||
}
|
||||
|
||||
if (buf->v4l2_buffer.flags & V4L2_BUF_FLAG_KEYFRAME) {
|
||||
had_key_frame = true;
|
||||
E_LOG_DEBUG(buf, "Got key frame!");
|
||||
}
|
||||
|
||||
if (had_key_frame) {
|
||||
if (!fwrite(buf->start, buf->used, 1, stream)) {
|
||||
goto error;
|
||||
}
|
||||
} else if (!requested_key_frame) {
|
||||
device_force_key(buf->buf_list->device);
|
||||
requested_key_frame = true;
|
||||
}
|
||||
buffer_consumed(buf);
|
||||
}
|
||||
|
||||
error:
|
||||
buffer_lock_use(&http_h264, -1);
|
||||
}
|
93
http/http_jpeg.c
Normal file
93
http/http_jpeg.c
Normal file
@ -0,0 +1,93 @@
|
||||
#include <stdio.h>
|
||||
#include <stdlib.h>
|
||||
|
||||
#include "http/http.h"
|
||||
#include "hw/buffer.h"
|
||||
#include "hw/buffer_lock.h"
|
||||
|
||||
DEFINE_BUFFER_LOCK(http_jpeg);
|
||||
|
||||
#define PART_BOUNDARY "123456789000000000000987654321"
|
||||
#define CONTENT_TYPE "image/jpeg"
|
||||
#define CONTENT_LENGTH "Content-Length"
|
||||
|
||||
static const char *const STREAM_HEADER = "HTTP/1.0 200 OK\r\n"
|
||||
"Access-Control-Allow-Origin: *\r\n"
|
||||
"Connection: close\r\n"
|
||||
"Content-Type: multipart/x-mixed-replace;boundary=" PART_BOUNDARY "\r\n"
|
||||
"\r\n"
|
||||
"--" PART_BOUNDARY "\r\n";
|
||||
static const char *const STREAM_ERROR = "Content-Type: text/plain\r\n"
|
||||
"\r\n"
|
||||
"Error: %d (%s).\r\n"
|
||||
"--" PART_BOUNDARY "\r\n";
|
||||
static const char *const STREAM_TIMEDOUT = "Content-Type: text/plain\r\n"
|
||||
"\r\n"
|
||||
"Timedout.\r\n"
|
||||
"--" PART_BOUNDARY "\r\n";
|
||||
static const char *const STREAM_PART = "Content-Type: " CONTENT_TYPE "\r\n" CONTENT_LENGTH ": %u\r\n\r\n";
|
||||
static const char *const STREAM_BOUNDARY = "\r\n"
|
||||
"--" PART_BOUNDARY "\r\n";
|
||||
|
||||
bool http_jpeg_needs_buffer()
|
||||
{
|
||||
return buffer_lock_needs_buffer(&http_jpeg);
|
||||
}
|
||||
|
||||
void http_jpeg_capture(buffer_t *buf)
|
||||
{
|
||||
buffer_lock_capture(&http_jpeg, buf);
|
||||
}
|
||||
|
||||
void http_snapshot(http_worker_t *worker, FILE *stream)
|
||||
{
|
||||
int counter = 0;
|
||||
buffer_lock_use(&http_jpeg, 1);
|
||||
buffer_t *buf = buffer_lock_get(&http_jpeg, 1, &counter);
|
||||
|
||||
if (!buf) {
|
||||
http_404_header(worker, stream);
|
||||
fprintf(stream, "No snapshot captured yet.\r\n");
|
||||
return;
|
||||
}
|
||||
|
||||
fprintf(stream, "HTTP/1.1 200 OK\r\n");
|
||||
fprintf(stream, "Content-Type: image/jpeg\r\n");
|
||||
fprintf(stream, "Content-Length: %d\r\n", buf->used);
|
||||
fprintf(stream, "\r\n");
|
||||
fwrite(buf->start, buf->used, 1, stream);
|
||||
buffer_consumed(buf);
|
||||
error:
|
||||
buffer_lock_use(&http_jpeg, -1);
|
||||
}
|
||||
|
||||
void http_stream(http_worker_t *worker, FILE *stream)
|
||||
{
|
||||
int counter = 0;
|
||||
fprintf(stream, STREAM_HEADER);
|
||||
buffer_lock_use(&http_jpeg, 1);
|
||||
|
||||
while (!feof(stream)) {
|
||||
buffer_t *buf = buffer_lock_get(&http_jpeg, 3, &counter);
|
||||
|
||||
if (!buf) {
|
||||
fprintf(stream, STREAM_ERROR, -1, "No frames.");
|
||||
goto error;
|
||||
}
|
||||
|
||||
if (!fprintf(stream, STREAM_PART, buf->used)) {
|
||||
goto error;
|
||||
}
|
||||
if (!fwrite(buf->start, buf->used, 1, stream)) {
|
||||
goto error;
|
||||
}
|
||||
if (!fprintf(stream, STREAM_BOUNDARY)) {
|
||||
goto error;
|
||||
}
|
||||
|
||||
buffer_consumed(buf);
|
||||
}
|
||||
|
||||
error:
|
||||
buffer_lock_use(&http_jpeg, -1);
|
||||
}
|
52
http/http_methods.c
Normal file
52
http/http_methods.c
Normal file
@ -0,0 +1,52 @@
|
||||
#include <stdio.h>
|
||||
#include <stdlib.h>
|
||||
|
||||
#include "http/http.h"
|
||||
#include "hw/buffer.h"
|
||||
#include "hw/buffer_lock.h"
|
||||
|
||||
void http_index(http_worker_t *worker, FILE *stream)
|
||||
{
|
||||
fprintf(stream, "HTTP/1.1 200 OK\r\n");
|
||||
fprintf(stream, "Content-Type: text/plain\r\n");
|
||||
fprintf(stream, "\r\n");
|
||||
fprintf(stream, "Text.\r\n");
|
||||
fflush(stream);
|
||||
}
|
||||
|
||||
void http_404_header(http_worker_t *worker, FILE *stream)
|
||||
{
|
||||
fprintf(stream, "HTTP/1.1 404 Not Found\r\n");
|
||||
fprintf(stream, "Content-Type: text/plain\r\n");
|
||||
fprintf(stream, "\r\n");
|
||||
}
|
||||
|
||||
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 html_video_html[];
|
||||
extern unsigned int html_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(html_video_html, 1, html_video_html_len, stream);
|
||||
fflush(stream);
|
||||
}
|
||||
|
||||
void http_jmuxer_js(http_worker_t *worker, FILE *stream)
|
||||
{
|
||||
extern unsigned char html_jmuxer_min_js[];
|
||||
extern unsigned int html_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(html_jmuxer_min_js, 1, html_jmuxer_min_js_len, stream);
|
||||
fflush(stream);
|
||||
}
|
Reference in New Issue
Block a user