diff --git a/janus/src/audio.c b/janus/src/audio.c index 146a00d7..ec33c217 100644 --- a/janus/src/audio.c +++ b/janus/src/audio.c @@ -32,6 +32,7 @@ #include #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; diff --git a/janus/src/memsinkfd.c b/janus/src/memsinkfd.c index 0157c262..9730863e 100644 --- a/janus/src/memsinkfd.c +++ b/janus/src/memsinkfd.c @@ -27,6 +27,7 @@ #include #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) { diff --git a/janus/src/plugin.c b/janus/src/plugin.c index e3492d75..703340f5 100644 --- a/janus/src/plugin.c +++ b/janus/src/plugin.c @@ -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; } } diff --git a/janus/src/uslibs/errors.h b/janus/src/uslibs/errors.h new file mode 120000 index 00000000..5bf1521c --- /dev/null +++ b/janus/src/uslibs/errors.h @@ -0,0 +1 @@ +../../../src/libs/errors.h \ No newline at end of file diff --git a/python/src/uslibs/errors.h b/python/src/uslibs/errors.h new file mode 120000 index 00000000..5bf1521c --- /dev/null +++ b/python/src/uslibs/errors.h @@ -0,0 +1 @@ +../../../src/libs/errors.h \ No newline at end of file diff --git a/python/src/ustreamer.c b/python/src/ustreamer.c index 068cea53..dc620b27 100644 --- a/python/src/ustreamer.c +++ b/python/src/ustreamer.c @@ -14,6 +14,7 @@ #include #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; } diff --git a/src/dump/main.c b/src/dump/main.c index 3c7fa377..1616379b 100644 --- a/src/dump/main.c +++ b/src/dump/main.c @@ -31,6 +31,7 @@ #include #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; diff --git a/src/libs/capture.c b/src/libs/capture.c index 6011316c..4c556f2e 100644 --- a/src/libs/capture.c +++ b/src/libs/capture.c @@ -41,6 +41,7 @@ #include #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"); diff --git a/src/libs/drm/drm.c b/src/libs/drm/drm.c index 4d3e21a5..de560a31 100644 --- a/src/libs/drm/drm.c +++ b/src/libs/drm/drm.c @@ -38,6 +38,7 @@ #include #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 @@ int us_drm_open(us_drm_s *drm, const us_capture_s *cap) { 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 @@ static int _drm_find_sink(us_drm_s *drm, uint width, uint height, float hz) { unplugged: drmModeFreeResources(res); - return -2; + return US_ERROR_NO_DEVICE; } static drmModeModeInfo *_find_best_mode(drmModeConnector *conn, uint width, uint height, float hz) { diff --git a/src/libs/errors.h b/src/libs/errors.h new file mode 100644 index 00000000..295c42b8 --- /dev/null +++ b/src/libs/errors.h @@ -0,0 +1,27 @@ +/***************************************************************************** +# # +# uStreamer - Lightweight and fast MJPEG-HTTP streamer. # +# # +# Copyright (C) 2018-2024 Maxim Devaev # +# # +# 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 . # +# # +*****************************************************************************/ + + +#pragma once + +#define US_ERROR_COMMON -1 +#define US_ERROR_NO_DEVICE -2 +#define US_ERROR_NO_DATA -3 diff --git a/src/libs/memsink.c b/src/libs/memsink.c index 3ae63464..d2aa1520 100644 --- a/src/libs/memsink.c +++ b/src/libs/memsink.c @@ -33,6 +33,7 @@ #include #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; } diff --git a/src/libs/tc358743.c b/src/libs/tc358743.c index d0f6746b..3adccb85 100644 --- a/src/libs/tc358743.c +++ b/src/libs/tc358743.c @@ -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; diff --git a/src/ustreamer/stream.c b/src/ustreamer/stream.c index f71fba7f..3675a153 100644 --- a/src/ustreamer/stream.c +++ b/src/ustreamer/stream.c @@ -24,6 +24,7 @@ #include #include +#include #include #include #include @@ -31,6 +32,7 @@ #include #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; diff --git a/src/v4p/main.c b/src/v4p/main.c index 4e3a8dfa..24294781 100644 --- a/src/v4p/main.c +++ b/src/v4p/main.c @@ -23,6 +23,7 @@ #include #include #include +#include #include #include #include @@ -36,6 +37,7 @@ #include #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) {