common error constants

This commit is contained in:
Maxim Devaev
2024-03-28 17:17:22 +02:00
parent d64077c2d5
commit a21f527bce
14 changed files with 85 additions and 39 deletions

View File

@@ -32,6 +32,7 @@
#include <opus/opus.h> #include <opus/opus.h>
#include "uslibs/types.h" #include "uslibs/types.h"
#include "uslibs/errors.h"
#include "uslibs/tools.h" #include "uslibs/tools.h"
#include "uslibs/array.h" #include "uslibs/array.h"
#include "uslibs/ring.h" #include "uslibs/ring.h"
@@ -185,12 +186,12 @@ int us_audio_get_encoded(us_audio_s *audio, u8 *data, uz *size, u64 *pts) {
} }
const int ri = us_ring_consumer_acquire(audio->enc_ring, 0.1); const int ri = us_ring_consumer_acquire(audio->enc_ring, 0.1);
if (ri < 0) { if (ri < 0) {
return -2; return US_ERROR_NO_DATA;
} }
const _enc_buffer_s *const buf = audio->enc_ring->items[ri]; const _enc_buffer_s *const buf = audio->enc_ring->items[ri];
if (*size < buf->used) { if (*size < buf->used) {
us_ring_consumer_release(audio->enc_ring, ri); us_ring_consumer_release(audio->enc_ring, ri);
return -3; return US_ERROR_NO_DATA;
} }
memcpy(data, buf->data, buf->used); memcpy(data, buf->data, buf->used);
*size = buf->used; *size = buf->used;

View File

@@ -27,6 +27,7 @@
#include <linux/videodev2.h> #include <linux/videodev2.h>
#include "uslibs/types.h" #include "uslibs/types.h"
#include "uslibs/errors.h"
#include "uslibs/tools.h" #include "uslibs/tools.h"
#include "uslibs/frame.h" #include "uslibs/frame.h"
#include "uslibs/memsinksh.h" #include "uslibs/memsinksh.h"
@@ -54,7 +55,7 @@ int us_memsink_fd_wait_frame(int fd, us_memsink_shared_s *mem, u64 last_id) {
} }
usleep(1000); // lock_polling usleep(1000); // lock_polling
} while (now_ts < deadline_ts); } while (now_ts < deadline_ts);
return -2; return US_ERROR_NO_DATA;
} }
int us_memsink_fd_get_frame(int fd, us_memsink_shared_s *mem, us_frame_s *frame, u64 *frame_id, bool key_required) { int us_memsink_fd_get_frame(int fd, us_memsink_shared_s *mem, us_frame_s *frame, u64 *frame_id, bool key_required) {

View File

@@ -37,6 +37,7 @@
#include "uslibs/types.h" #include "uslibs/types.h"
#include "uslibs/const.h" #include "uslibs/const.h"
#include "uslibs/errors.h"
#include "uslibs/tools.h" #include "uslibs/tools.h"
#include "uslibs/threading.h" #include "uslibs/threading.h"
#include "uslibs/list.h" #include "uslibs/list.h"
@@ -178,7 +179,7 @@ static void *_video_sink_thread(void *arg) {
if (ri >= 0 && frame->key) { if (ri >= 0 && frame->key) {
atomic_store(&_g_key_required, false); atomic_store(&_g_key_required, false);
} }
} else if (waited != -2) { } else if (waited != US_ERROR_NO_DATA) {
goto close_memsink; goto close_memsink;
} }
} }

1
janus/src/uslibs/errors.h Symbolic link
View File

@@ -0,0 +1 @@
../../../src/libs/errors.h

1
python/src/uslibs/errors.h Symbolic link
View File

@@ -0,0 +1 @@
../../../src/libs/errors.h

View File

@@ -14,6 +14,7 @@
#include <Python.h> #include <Python.h>
#include "uslibs/types.h" #include "uslibs/types.h"
#include "uslibs/errors.h"
#include "uslibs/tools.h" #include "uslibs/tools.h"
#include "uslibs/frame.h" #include "uslibs/frame.h"
#include "uslibs/memsinksh.h" #include "uslibs/memsinksh.h"
@@ -175,9 +176,9 @@ static int _wait_frame(_MemsinkObject *self) {
if (PyErr_CheckSignals() < 0) { if (PyErr_CheckSignals() < 0) {
return -1; return -1;
} }
} while (now_ts < deadline_ts); } while (now_ts < deadline_ts);
return -2;
return US_ERROR_NO_DATA;
} }
static PyObject *_MemsinkObject_wait_frame(_MemsinkObject *self, PyObject *args, PyObject *kwargs) { static PyObject *_MemsinkObject_wait_frame(_MemsinkObject *self, PyObject *args, PyObject *kwargs) {
@@ -194,7 +195,7 @@ static PyObject *_MemsinkObject_wait_frame(_MemsinkObject *self, PyObject *args,
switch (_wait_frame(self)) { switch (_wait_frame(self)) {
case 0: break; case 0: break;
case -2: Py_RETURN_NONE; case US_ERROR_NO_DATA: Py_RETURN_NONE;
default: return NULL; default: return NULL;
} }

View File

@@ -31,6 +31,7 @@
#include <assert.h> #include <assert.h>
#include "../libs/const.h" #include "../libs/const.h"
#include "../libs/errors.h"
#include "../libs/tools.h" #include "../libs/tools.h"
#include "../libs/logging.h" #include "../libs/logging.h"
#include "../libs/frame.h" #include "../libs/frame.h"
@@ -234,8 +235,8 @@ static int _dump_sink(
while (!_g_stop) { while (!_g_stop) {
bool key_requested; bool key_requested;
const int error = us_memsink_client_get(sink, frame, &key_requested, key_required); const int got = us_memsink_client_get(sink, frame, &key_requested, key_required);
if (error == 0) { if (got == 0) {
key_required = false; key_required = false;
const long double now = us_get_now_monotonic(); const long double now = us_get_now_monotonic();
@@ -275,7 +276,7 @@ static int _dump_sink(
if (interval_us > 0) { if (interval_us > 0) {
usleep(interval_us); usleep(interval_us);
} }
} else if (error == -2) { } else if (got == US_ERROR_NO_DATA) {
usleep(1000); usleep(1000);
} else { } else {
goto error; goto error;

View File

@@ -41,6 +41,7 @@
#include <linux/v4l2-controls.h> #include <linux/v4l2-controls.h>
#include "types.h" #include "types.h"
#include "errors.h"
#include "tools.h" #include "tools.h"
#include "array.h" #include "array.h"
#include "logging.h" #include "logging.h"
@@ -179,7 +180,7 @@ int us_capture_open(us_capture_s *cap) {
run->open_error_reported = -errno; // Don't confuse it with __LINE__ run->open_error_reported = -errno; // Don't confuse it with __LINE__
US_LOG_PERROR("No access to capture device"); US_LOG_PERROR("No access to capture device");
} }
goto tmp_error; goto error_no_device;
} }
_LOG_DEBUG("Opening capture device ..."); _LOG_DEBUG("Opening capture device ...");
@@ -197,7 +198,7 @@ int us_capture_open(us_capture_s *cap) {
run->open_error_reported = line; run->open_error_reported = line;
_LOG_ERROR("No signal from source"); _LOG_ERROR("No signal from source");
} }
goto tmp_error; goto error_no_signal;
} }
} }
@@ -241,9 +242,13 @@ int us_capture_open(us_capture_s *cap) {
_LOG_INFO("Capturing started"); _LOG_INFO("Capturing started");
return 0; return 0;
tmp_error: error_no_device:
us_capture_close(cap); us_capture_close(cap);
return -2; return US_ERROR_NO_DEVICE;
error_no_signal:
us_capture_close(cap);
return US_ERROR_NO_DATA;
error: error:
run->open_error_reported = 0; run->open_error_reported = 0;
@@ -305,7 +310,7 @@ int us_capture_hwbuf_grab(us_capture_s *cap, us_capture_hwbuf_s **hw) {
// или эвент V4L2. Обработка эвентов более приоритетна, чем кадров. // или эвент V4L2. Обработка эвентов более приоритетна, чем кадров.
// - Если есть новые фреймы, то пропустить их все, пока не закончатся и вернуть // - Если есть новые фреймы, то пропустить их все, пока не закончатся и вернуть
// самый-самый свежий, содержащий при этом валидные данные. // самый-самый свежий, содержащий при этом валидные данные.
// - Если таковых не нашлось, вернуть -2. // - Если таковых не нашлось, вернуть US_ERROR_NO_DATA.
// - Ошибка -1 возвращается при любых сбоях. // - Ошибка -1 возвращается при любых сбоях.
if (_capture_wait_buffer(cap) < 0) { if (_capture_wait_buffer(cap) < 0) {
@@ -392,7 +397,7 @@ int us_capture_hwbuf_grab(us_capture_s *cap, us_capture_hwbuf_s **hw) {
if (buf_got) { if (buf_got) {
break; // Process any latest valid frame break; // Process any latest valid frame
} else if (broken) { } else if (broken) {
return -2; // If we have only broken frames on this capture session return US_ERROR_NO_DATA; // If we have only broken frames on this capture session
} }
} }
_LOG_PERROR("Can't grab HW buffer"); _LOG_PERROR("Can't grab HW buffer");

View File

@@ -38,6 +38,7 @@
#include <libdrm/drm.h> #include <libdrm/drm.h>
#include "../types.h" #include "../types.h"
#include "../errors.h"
#include "../tools.h" #include "../tools.h"
#include "../logging.h" #include "../logging.h"
#include "../frame.h" #include "../frame.h"
@@ -98,7 +99,7 @@ int us_drm_open(us_drm_s *drm, const us_capture_s *cap) {
switch (_drm_check_status(drm)) { switch (_drm_check_status(drm)) {
case 0: break; case 0: break;
case -2: goto unplugged; case US_ERROR_NO_DEVICE: goto unplugged;
default: goto error; default: goto error;
} }
@@ -143,7 +144,7 @@ int us_drm_open(us_drm_s *drm, const us_capture_s *cap) {
const uint hz = (stub > 0 ? 0 : cap->run->hz); const uint hz = (stub > 0 ? 0 : cap->run->hz);
switch (_drm_find_sink(drm, width, height, hz)) { switch (_drm_find_sink(drm, width, height, hz)) {
case 0: break; case 0: break;
case -2: goto unplugged; case US_ERROR_NO_DEVICE: goto unplugged;
default: goto error; default: goto error;
} }
if ((stub == 0) && (width != run->mode.hdisplay || height < run->mode.vdisplay)) { if ((stub == 0) && (width != run->mode.hdisplay || height < run->mode.vdisplay)) {
@@ -179,7 +180,7 @@ unplugged:
run->unplugged_reported = true; run->unplugged_reported = true;
} }
us_drm_close(drm); us_drm_close(drm);
return -2; return US_ERROR_NO_DEVICE;
} }
void us_drm_close(us_drm_s *drm) { void us_drm_close(us_drm_s *drm) {
@@ -245,7 +246,7 @@ int us_drm_dpms_power_off(us_drm_s *drm) {
assert(drm->run->fd >= 0); assert(drm->run->fd >= 0);
switch (_drm_check_status(drm)) { switch (_drm_check_status(drm)) {
case 0: break; case 0: break;
case -2: return 0; // Unplugged, nice case US_ERROR_NO_DEVICE: return 0; // Unplugged, nice
// Во время переключения DPMS монитор моргает один раз состоянием disconnected, // Во время переключения DPMS монитор моргает один раз состоянием disconnected,
// а потом почему-то снова оказывается connected. Так что просто считаем, // а потом почему-то снова оказывается connected. Так что просто считаем,
// что отсоединенный монитор на этом этапе - это нормально. // что отсоединенный монитор на этом этапе - это нормально.
@@ -262,7 +263,7 @@ int us_drm_wait_for_vsync(us_drm_s *drm) {
switch (_drm_check_status(drm)) { switch (_drm_check_status(drm)) {
case 0: break; case 0: break;
case -2: return -2; case US_ERROR_NO_DEVICE: return US_ERROR_NO_DEVICE;
default: return -1; default: return -1;
} }
_drm_ensure_dpms_power(drm, true); _drm_ensure_dpms_power(drm, true);
@@ -317,7 +318,7 @@ int us_drm_expose_stub(us_drm_s *drm, us_drm_stub_e stub, const us_capture_s *ca
switch (_drm_check_status(drm)) { switch (_drm_check_status(drm)) {
case 0: break; case 0: break;
case -2: return -2; case US_ERROR_NO_DEVICE: return US_ERROR_NO_DEVICE;
default: return -1; default: return -1;
} }
_drm_ensure_dpms_power(drm, true); _drm_ensure_dpms_power(drm, true);
@@ -381,7 +382,7 @@ int us_drm_expose_dma(us_drm_s *drm, const us_capture_hwbuf_s *hw) {
switch (_drm_check_status(drm)) { switch (_drm_check_status(drm)) {
case 0: break; case 0: break;
case -2: return -2; case US_ERROR_NO_DEVICE: return US_ERROR_NO_DEVICE;
default: return -1; default: return -1;
} }
_drm_ensure_dpms_power(drm, true); _drm_ensure_dpms_power(drm, true);
@@ -434,7 +435,7 @@ static int _drm_check_status(us_drm_s *drm) {
goto error; goto error;
} }
_LOG_DEBUG("Current display status: %c", status_ch); _LOG_DEBUG("Current display status: %c", status_ch);
return (status_ch == 'd' ? -2 : 0); return (status_ch == 'd' ? US_ERROR_NO_DEVICE : 0);
error: error:
US_CLOSE_FD(run->status_fd); US_CLOSE_FD(run->status_fd);
@@ -613,7 +614,7 @@ done:
unplugged: unplugged:
drmModeFreeResources(res); drmModeFreeResources(res);
return -2; return US_ERROR_NO_DEVICE;
} }
static drmModeModeInfo *_find_best_mode(drmModeConnector *conn, uint width, uint height, float hz) { static drmModeModeInfo *_find_best_mode(drmModeConnector *conn, uint width, uint height, float hz) {

27
src/libs/errors.h Normal file
View File

@@ -0,0 +1,27 @@
/*****************************************************************************
# #
# uStreamer - Lightweight and fast MJPEG-HTTP streamer. #
# #
# Copyright (C) 2018-2024 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
#define US_ERROR_COMMON -1
#define US_ERROR_NO_DEVICE -2
#define US_ERROR_NO_DATA -3

View File

@@ -33,6 +33,7 @@
#include <sys/mman.h> #include <sys/mman.h>
#include "types.h" #include "types.h"
#include "errors.h"
#include "tools.h" #include "tools.h"
#include "logging.h" #include "logging.h"
#include "frame.h" #include "frame.h"
@@ -168,7 +169,7 @@ int us_memsink_server_put(us_memsink_s *sink, const us_frame_s *frame, bool *key
if (frame->used > sink->data_size) { if (frame->used > sink->data_size) {
US_LOG_ERROR("%s-sink: Can't put frame: is too big (%zu > %zu)", US_LOG_ERROR("%s-sink: Can't put frame: is too big (%zu > %zu)",
sink->name, frame->used, sink->data_size); sink->name, frame->used, sink->data_size);
return 0; // -2 return 0;
} }
if (us_flock_timedwait_monotonic(sink->fd, 1) == 0) { if (us_flock_timedwait_monotonic(sink->fd, 1) == 0) {
@@ -213,7 +214,7 @@ int us_memsink_client_get(us_memsink_s *sink, us_frame_s *frame, bool *key_reque
if (us_flock_timedwait_monotonic(sink->fd, sink->timeout) < 0) { if (us_flock_timedwait_monotonic(sink->fd, sink->timeout) < 0) {
if (errno == EWOULDBLOCK) { if (errno == EWOULDBLOCK) {
return -2; return US_ERROR_NO_DATA;
} }
US_LOG_PERROR("%s-sink: Can't lock memory", sink->name); US_LOG_PERROR("%s-sink: Can't lock memory", sink->name);
return -1; return -1;
@@ -222,7 +223,7 @@ int us_memsink_client_get(us_memsink_s *sink, us_frame_s *frame, bool *key_reque
int retval = 0; int retval = 0;
if (sink->mem->magic != US_MEMSINK_MAGIC) { if (sink->mem->magic != US_MEMSINK_MAGIC) {
retval = -2; // Not updated retval = US_ERROR_NO_DATA; // Not updated
goto done; goto done;
} }
if (sink->mem->version != US_MEMSINK_VERSION) { if (sink->mem->version != US_MEMSINK_VERSION) {
@@ -236,7 +237,7 @@ int us_memsink_client_get(us_memsink_s *sink, us_frame_s *frame, bool *key_reque
sink->mem->last_client_ts = us_get_now_monotonic(); sink->mem->last_client_ts = us_get_now_monotonic();
if (sink->mem->id == sink->last_readed_id) { if (sink->mem->id == sink->last_readed_id) {
retval = -2; // Not updated retval = US_ERROR_NO_DATA; // Not updated
goto done; goto done;
} }

View File

@@ -58,7 +58,7 @@ int us_tc358743_xioctl_get_audio_hz(int fd, uint *audio_hz) {
US_MEMSET_ZERO(ctl); US_MEMSET_ZERO(ctl);
ctl.id = TC358743_CID_AUDIO_SAMPLING_RATE; ctl.id = TC358743_CID_AUDIO_SAMPLING_RATE;
if (us_xioctl(fd, VIDIOC_G_CTRL, &ctl) < 0) { if (us_xioctl(fd, VIDIOC_G_CTRL, &ctl) < 0) {
return -2; return -1;
} }
*audio_hz = ctl.value; *audio_hz = ctl.value;
return 0; return 0;

View File

@@ -24,6 +24,7 @@
#include <stdlib.h> #include <stdlib.h>
#include <stdatomic.h> #include <stdatomic.h>
#include <limits.h>
#include <unistd.h> #include <unistd.h>
#include <errno.h> #include <errno.h>
#include <assert.h> #include <assert.h>
@@ -31,6 +32,7 @@
#include <pthread.h> #include <pthread.h>
#include "../libs/types.h" #include "../libs/types.h"
#include "../libs/errors.h"
#include "../libs/tools.h" #include "../libs/tools.h"
#include "../libs/threading.h" #include "../libs/threading.h"
#include "../libs/process.h" #include "../libs/process.h"
@@ -205,9 +207,9 @@ void us_stream_loop(us_stream_s *stream) {
while (!atomic_load(&run->stop) && !atomic_load(&threads_stop)) { while (!atomic_load(&run->stop) && !atomic_load(&threads_stop)) {
us_capture_hwbuf_s *hw; us_capture_hwbuf_s *hw;
switch (us_capture_hwbuf_grab(cap, &hw)) { switch (us_capture_hwbuf_grab(cap, &hw)) {
case -2: continue; // Broken frame case 0 ... INT_MAX: break; // Grabbed buffer number
case -1: goto close; // Error case US_ERROR_NO_DATA: continue; // Broken frame
default: break; // Grabbed on >= 0 default: goto close; // Any error
} }
const sll now_sec_ts = us_floor_ms(us_get_now_monotonic()); const sll now_sec_ts = us_floor_ms(us_get_now_monotonic());
@@ -600,7 +602,9 @@ static int _stream_init_loop(us_stream_s *stream) {
|| run->h264 != NULL || run->h264 != NULL
); );
switch (us_capture_open(stream->cap)) { switch (us_capture_open(stream->cap)) {
case -2: case 0: break;
case US_ERROR_NO_DEVICE:
case US_ERROR_NO_DATA:
if (!waiting_reported) { if (!waiting_reported) {
waiting_reported = true; waiting_reported = true;
US_LOG_INFO("Waiting for the capture device ..."); US_LOG_INFO("Waiting for the capture device ...");
@@ -609,13 +613,12 @@ static int _stream_init_loop(us_stream_s *stream) {
_stream_drm_ensure_no_signal(stream); _stream_drm_ensure_no_signal(stream);
# endif # endif
goto offline_and_retry; goto offline_and_retry;
case -1: default:
waiting_reported = false; waiting_reported = false;
# ifdef WITH_V4P # ifdef WITH_V4P
_stream_drm_ensure_no_signal(stream); _stream_drm_ensure_no_signal(stream);
# endif # endif
goto offline_and_retry; goto offline_and_retry;
default: break;
} }
us_encoder_open(stream->enc, stream->cap); us_encoder_open(stream->enc, stream->cap);
return 0; return 0;

View File

@@ -23,6 +23,7 @@
#include <stdio.h> #include <stdio.h>
#include <stdlib.h> #include <stdlib.h>
#include <stdatomic.h> #include <stdatomic.h>
#include <limits.h>
#include <string.h> #include <string.h>
#include <unistd.h> #include <unistd.h>
#include <getopt.h> #include <getopt.h>
@@ -36,6 +37,7 @@
#include <sys/stat.h> #include <sys/stat.h>
#include "../libs/types.h" #include "../libs/types.h"
#include "../libs/errors.h"
#include "../libs/const.h" #include "../libs/const.h"
#include "../libs/tools.h" #include "../libs/tools.h"
#include "../libs/logging.h" #include "../libs/logging.h"
@@ -227,9 +229,9 @@ static void _main_loop(void) {
us_capture_hwbuf_s *hw; us_capture_hwbuf_s *hw;
switch (us_capture_hwbuf_grab(cap, &hw)) { switch (us_capture_hwbuf_grab(cap, &hw)) {
case -2: continue; // Broken frame case 0 ... INT_MAX: break; // Grabbed buffer number
case -1: goto close; // Any error case US_ERROR_NO_DATA: continue; // Broken frame
default: break; // Grabbed on >= 0 default: goto close; // Any error
} }
if (drm_opened == 0) { if (drm_opened == 0) {