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 "uslibs/types.h"
#include "uslibs/errors.h"
#include "uslibs/tools.h"
#include "uslibs/array.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);
if (ri < 0) {
return -2;
return US_ERROR_NO_DATA;
}
const _enc_buffer_s *const buf = audio->enc_ring->items[ri];
if (*size < buf->used) {
us_ring_consumer_release(audio->enc_ring, ri);
return -3;
return US_ERROR_NO_DATA;
}
memcpy(data, buf->data, buf->used);
*size = buf->used;

View File

@ -27,6 +27,7 @@
#include <linux/videodev2.h>
#include "uslibs/types.h"
#include "uslibs/errors.h"
#include "uslibs/tools.h"
#include "uslibs/frame.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
} 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) {

View File

@ -37,6 +37,7 @@
#include "uslibs/types.h"
#include "uslibs/const.h"
#include "uslibs/errors.h"
#include "uslibs/tools.h"
#include "uslibs/threading.h"
#include "uslibs/list.h"
@ -178,7 +179,7 @@ static void *_video_sink_thread(void *arg) {
if (ri >= 0 && frame->key) {
atomic_store(&_g_key_required, false);
}
} else if (waited != -2) {
} else if (waited != US_ERROR_NO_DATA) {
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 "uslibs/types.h"
#include "uslibs/errors.h"
#include "uslibs/tools.h"
#include "uslibs/frame.h"
#include "uslibs/memsinksh.h"
@ -175,9 +176,9 @@ static int _wait_frame(_MemsinkObject *self) {
if (PyErr_CheckSignals() < 0) {
return -1;
}
} while (now_ts < deadline_ts);
return -2;
return US_ERROR_NO_DATA;
}
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)) {
case 0: break;
case -2: Py_RETURN_NONE;
case US_ERROR_NO_DATA: Py_RETURN_NONE;
default: return NULL;
}

View File

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

View File

@ -41,6 +41,7 @@
#include <linux/v4l2-controls.h>
#include "types.h"
#include "errors.h"
#include "tools.h"
#include "array.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__
US_LOG_PERROR("No access to capture device");
}
goto tmp_error;
goto error_no_device;
}
_LOG_DEBUG("Opening capture device ...");
@ -197,7 +198,7 @@ int us_capture_open(us_capture_s *cap) {
run->open_error_reported = line;
_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");
return 0;
tmp_error:
error_no_device:
us_capture_close(cap);
return -2;
return US_ERROR_NO_DEVICE;
error_no_signal:
us_capture_close(cap);
return US_ERROR_NO_DATA;
error:
run->open_error_reported = 0;
@ -305,7 +310,7 @@ int us_capture_hwbuf_grab(us_capture_s *cap, us_capture_hwbuf_s **hw) {
// или эвент V4L2. Обработка эвентов более приоритетна, чем кадров.
// - Если есть новые фреймы, то пропустить их все, пока не закончатся и вернуть
// самый-самый свежий, содержащий при этом валидные данные.
// - Если таковых не нашлось, вернуть -2.
// - Если таковых не нашлось, вернуть US_ERROR_NO_DATA.
// - Ошибка -1 возвращается при любых сбоях.
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) {
break; // Process any latest valid frame
} 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");

View File

@ -38,6 +38,7 @@
#include <libdrm/drm.h>
#include "../types.h"
#include "../errors.h"
#include "../tools.h"
#include "../logging.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)) {
case 0: break;
case -2: goto unplugged;
case US_ERROR_NO_DEVICE: goto unplugged;
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);
switch (_drm_find_sink(drm, width, height, hz)) {
case 0: break;
case -2: goto unplugged;
case US_ERROR_NO_DEVICE: goto unplugged;
default: goto error;
}
if ((stub == 0) && (width != run->mode.hdisplay || height < run->mode.vdisplay)) {
@ -179,7 +180,7 @@ unplugged:
run->unplugged_reported = true;
}
us_drm_close(drm);
return -2;
return US_ERROR_NO_DEVICE;
}
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);
switch (_drm_check_status(drm)) {
case 0: break;
case -2: return 0; // Unplugged, nice
case US_ERROR_NO_DEVICE: return 0; // Unplugged, nice
// Во время переключения DPMS монитор моргает один раз состоянием disconnected,
// а потом почему-то снова оказывается connected. Так что просто считаем,
// что отсоединенный монитор на этом этапе - это нормально.
@ -262,7 +263,7 @@ int us_drm_wait_for_vsync(us_drm_s *drm) {
switch (_drm_check_status(drm)) {
case 0: break;
case -2: return -2;
case US_ERROR_NO_DEVICE: return US_ERROR_NO_DEVICE;
default: return -1;
}
_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)) {
case 0: break;
case -2: return -2;
case US_ERROR_NO_DEVICE: return US_ERROR_NO_DEVICE;
default: return -1;
}
_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)) {
case 0: break;
case -2: return -2;
case US_ERROR_NO_DEVICE: return US_ERROR_NO_DEVICE;
default: return -1;
}
_drm_ensure_dpms_power(drm, true);
@ -434,7 +435,7 @@ static int _drm_check_status(us_drm_s *drm) {
goto error;
}
_LOG_DEBUG("Current display status: %c", status_ch);
return (status_ch == 'd' ? -2 : 0);
return (status_ch == 'd' ? US_ERROR_NO_DEVICE : 0);
error:
US_CLOSE_FD(run->status_fd);
@ -613,7 +614,7 @@ done:
unplugged:
drmModeFreeResources(res);
return -2;
return US_ERROR_NO_DEVICE;
}
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 "types.h"
#include "errors.h"
#include "tools.h"
#include "logging.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) {
US_LOG_ERROR("%s-sink: Can't put frame: is too big (%zu > %zu)",
sink->name, frame->used, sink->data_size);
return 0; // -2
return 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 (errno == EWOULDBLOCK) {
return -2;
return US_ERROR_NO_DATA;
}
US_LOG_PERROR("%s-sink: Can't lock memory", sink->name);
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;
if (sink->mem->magic != US_MEMSINK_MAGIC) {
retval = -2; // Not updated
retval = US_ERROR_NO_DATA; // Not updated
goto done;
}
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();
if (sink->mem->id == sink->last_readed_id) {
retval = -2; // Not updated
retval = US_ERROR_NO_DATA; // Not updated
goto done;
}

View File

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

View File

@ -24,6 +24,7 @@
#include <stdlib.h>
#include <stdatomic.h>
#include <limits.h>
#include <unistd.h>
#include <errno.h>
#include <assert.h>
@ -31,6 +32,7 @@
#include <pthread.h>
#include "../libs/types.h"
#include "../libs/errors.h"
#include "../libs/tools.h"
#include "../libs/threading.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)) {
us_capture_hwbuf_s *hw;
switch (us_capture_hwbuf_grab(cap, &hw)) {
case -2: continue; // Broken frame
case -1: goto close; // Error
default: break; // Grabbed on >= 0
case 0 ... INT_MAX: break; // Grabbed buffer number
case US_ERROR_NO_DATA: continue; // Broken frame
default: goto close; // Any error
}
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
);
switch (us_capture_open(stream->cap)) {
case -2:
case 0: break;
case US_ERROR_NO_DEVICE:
case US_ERROR_NO_DATA:
if (!waiting_reported) {
waiting_reported = true;
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);
# endif
goto offline_and_retry;
case -1:
default:
waiting_reported = false;
# ifdef WITH_V4P
_stream_drm_ensure_no_signal(stream);
# endif
goto offline_and_retry;
default: break;
}
us_encoder_open(stream->enc, stream->cap);
return 0;

View File

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