mirror of
https://github.com/pikvm/ustreamer.git
synced 2026-03-12 02:23:43 +00:00
refactoring
This commit is contained in:
71
src/libs/list.h
Normal file
71
src/libs/list.h
Normal file
@@ -0,0 +1,71 @@
|
|||||||
|
/*****************************************************************************
|
||||||
|
# #
|
||||||
|
# uStreamer - Lightweight and fast MJPG-HTTP streamer. #
|
||||||
|
# #
|
||||||
|
# Copyright (C) 2018-2021 Maxim Devaev <mdevaev@gmail.com> #
|
||||||
|
# #
|
||||||
|
# This program is free software: you can redistribute it and/or modify #
|
||||||
|
# it under the terms of the GNU General Public License as published by #
|
||||||
|
# the Free Software Foundation, either version 3 of the License, or #
|
||||||
|
# (at your option) any later version. #
|
||||||
|
# #
|
||||||
|
# This program is distributed in the hope that it will be useful, #
|
||||||
|
# but WITHOUT ANY WARRANTY; without even the implied warranty of #
|
||||||
|
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the #
|
||||||
|
# GNU General Public License for more details. #
|
||||||
|
# #
|
||||||
|
# You should have received a copy of the GNU General Public License #
|
||||||
|
# along with this program. If not, see <https://www.gnu.org/licenses/>. #
|
||||||
|
# #
|
||||||
|
*****************************************************************************/
|
||||||
|
|
||||||
|
|
||||||
|
#pragma once
|
||||||
|
|
||||||
|
#include <assert.h>
|
||||||
|
|
||||||
|
|
||||||
|
#define LIST_STRUCT(...) \
|
||||||
|
__VA_ARGS__ *prev; \
|
||||||
|
__VA_ARGS__ *next;
|
||||||
|
|
||||||
|
#define LIST_ITERATE(_first, _item, ...) { \
|
||||||
|
for (__typeof__(_first) _item = _first; _item;) { \
|
||||||
|
__typeof__(_first) _next = _item->next; \
|
||||||
|
__VA_ARGS__ \
|
||||||
|
_item = _next; \
|
||||||
|
} \
|
||||||
|
}
|
||||||
|
|
||||||
|
#define LIST_APPEND(_first, _item) { \
|
||||||
|
if (_first == NULL) { \
|
||||||
|
_first = _item; \
|
||||||
|
} else { \
|
||||||
|
__typeof__(_first) _last = _first; \
|
||||||
|
for (; _last->next; _last = _last->next); \
|
||||||
|
_item->prev = _last; \
|
||||||
|
_last->next = _item; \
|
||||||
|
} \
|
||||||
|
}
|
||||||
|
|
||||||
|
#define LIST_APPEND_C(_first, _item, _count) { \
|
||||||
|
LIST_APPEND(_first, _item); \
|
||||||
|
++(_count); \
|
||||||
|
}
|
||||||
|
|
||||||
|
#define LIST_REMOVE(_first, _item) { \
|
||||||
|
if (_item->prev == NULL) { \
|
||||||
|
_first = _item->next; \
|
||||||
|
} else { \
|
||||||
|
_item->prev->next = _item->next; \
|
||||||
|
} \
|
||||||
|
if (_item->next != NULL) { \
|
||||||
|
_item->next->prev = _item->prev; \
|
||||||
|
} \
|
||||||
|
}
|
||||||
|
|
||||||
|
#define LIST_REMOVE_C(_first, _item, _count) { \
|
||||||
|
LIST_REMOVE(_first, _item); \
|
||||||
|
assert((_count) >= 1); \
|
||||||
|
--(_count); \
|
||||||
|
}
|
||||||
@@ -46,36 +46,6 @@
|
|||||||
|
|
||||||
#define ARRAY_LEN(_array) (sizeof(_array) / sizeof(_array[0]))
|
#define ARRAY_LEN(_array) (sizeof(_array) / sizeof(_array[0]))
|
||||||
|
|
||||||
#define LIST_ITERATE(_first, _item, ...) { \
|
|
||||||
for (__typeof__(_first) _item = _first; _item;) { \
|
|
||||||
__typeof__(_first) _next = _item->next; \
|
|
||||||
__VA_ARGS__ \
|
|
||||||
_item = _next; \
|
|
||||||
} \
|
|
||||||
}
|
|
||||||
|
|
||||||
#define LIST_APPEND(_first, _item) { \
|
|
||||||
if (_first == NULL) { \
|
|
||||||
_first = _item; \
|
|
||||||
} else { \
|
|
||||||
__typeof__(_first) _last = _first; \
|
|
||||||
for (; _last->next; _last = _last->next); \
|
|
||||||
_item->prev = _last; \
|
|
||||||
_last->next = _item; \
|
|
||||||
} \
|
|
||||||
}
|
|
||||||
|
|
||||||
#define LIST_REMOVE(_first, _item) { \
|
|
||||||
if (_item->prev == NULL) { \
|
|
||||||
_first = _item->next; \
|
|
||||||
} else { \
|
|
||||||
_item->prev->next = _item->next; \
|
|
||||||
} \
|
|
||||||
if (_item->next != NULL) { \
|
|
||||||
_item->next->prev = _item->prev; \
|
|
||||||
} \
|
|
||||||
}
|
|
||||||
|
|
||||||
|
|
||||||
INLINE const char *bool_to_string(bool flag) {
|
INLINE const char *bool_to_string(bool flag) {
|
||||||
return (flag ? "true" : "false");
|
return (flag ? "true" : "false");
|
||||||
|
|||||||
@@ -475,8 +475,7 @@ static void _http_callback_stream(struct evhttp_request *request, void *v_server
|
|||||||
client->hostport = _http_get_client_hostport(request);
|
client->hostport = _http_get_client_hostport(request);
|
||||||
client->id = get_now_id();
|
client->id = get_now_id();
|
||||||
|
|
||||||
LIST_APPEND(RUN(stream_clients), client);
|
LIST_APPEND_C(RUN(stream_clients), client, RUN(stream_clients_count));
|
||||||
RUN(stream_clients_count) += 1;
|
|
||||||
|
|
||||||
if (RUN(stream_clients_count) == 1) {
|
if (RUN(stream_clients_count) == 1) {
|
||||||
atomic_store(&VID(has_clients), true);
|
atomic_store(&VID(has_clients), true);
|
||||||
@@ -643,10 +642,7 @@ static void _http_callback_stream_error(UNUSED struct bufferevent *buf_event, UN
|
|||||||
stream_client_s *client = (stream_client_s *)v_client;
|
stream_client_s *client = (stream_client_s *)v_client;
|
||||||
server_s *server = client->server;
|
server_s *server = client->server;
|
||||||
|
|
||||||
char *reason = bufferevent_my_format_reason(what);
|
LIST_REMOVE_C(RUN(stream_clients), client, RUN(stream_clients_count));
|
||||||
|
|
||||||
assert(RUN(stream_clients_count) > 0);
|
|
||||||
RUN(stream_clients_count) -= 1;
|
|
||||||
|
|
||||||
if (RUN(stream_clients_count) == 0) {
|
if (RUN(stream_clients_count) == 0) {
|
||||||
atomic_store(&VID(has_clients), false);
|
atomic_store(&VID(has_clients), false);
|
||||||
@@ -655,20 +651,19 @@ static void _http_callback_stream_error(UNUSED struct bufferevent *buf_event, UN
|
|||||||
# endif
|
# endif
|
||||||
}
|
}
|
||||||
|
|
||||||
|
char *reason = bufferevent_my_format_reason(what);
|
||||||
LOG_INFO("HTTP: Disconnected client: %s, id=%" PRIx64 ", %s; clients now: %u",
|
LOG_INFO("HTTP: Disconnected client: %s, id=%" PRIx64 ", %s; clients now: %u",
|
||||||
client->hostport, client->id, reason, RUN(stream_clients_count));
|
client->hostport, client->id, reason, RUN(stream_clients_count));
|
||||||
|
free(reason);
|
||||||
|
|
||||||
struct evhttp_connection *conn = evhttp_request_get_connection(client->request);
|
struct evhttp_connection *conn = evhttp_request_get_connection(client->request);
|
||||||
if (conn) {
|
if (conn) {
|
||||||
evhttp_connection_free(conn);
|
evhttp_connection_free(conn);
|
||||||
}
|
}
|
||||||
|
|
||||||
LIST_REMOVE(RUN(stream_clients), client);
|
|
||||||
free(client->key);
|
free(client->key);
|
||||||
free(client->hostport);
|
free(client->hostport);
|
||||||
free(client);
|
free(client);
|
||||||
|
|
||||||
free(reason);
|
|
||||||
}
|
}
|
||||||
|
|
||||||
static void _http_queue_send_stream(server_s *server, bool stream_updated, bool frame_updated) {
|
static void _http_queue_send_stream(server_s *server, bool stream_updated, bool frame_updated) {
|
||||||
|
|||||||
@@ -58,6 +58,7 @@
|
|||||||
#include "../../libs/process.h"
|
#include "../../libs/process.h"
|
||||||
#include "../../libs/frame.h"
|
#include "../../libs/frame.h"
|
||||||
#include "../../libs/base64.h"
|
#include "../../libs/base64.h"
|
||||||
|
#include "../../libs/list.h"
|
||||||
#include "../data/index_html.h"
|
#include "../data/index_html.h"
|
||||||
#include "../encoder.h"
|
#include "../encoder.h"
|
||||||
#include "../stream.h"
|
#include "../stream.h"
|
||||||
@@ -91,8 +92,7 @@ typedef struct stream_client_sx {
|
|||||||
unsigned fps_accum;
|
unsigned fps_accum;
|
||||||
long long fps_accum_second;
|
long long fps_accum_second;
|
||||||
|
|
||||||
struct stream_client_sx *prev;
|
LIST_STRUCT(struct stream_client_sx);
|
||||||
struct stream_client_sx *next;
|
|
||||||
} stream_client_s;
|
} stream_client_s;
|
||||||
|
|
||||||
typedef struct {
|
typedef struct {
|
||||||
|
|||||||
Reference in New Issue
Block a user