Skip to content

Commit

Permalink
common error constants
Browse files Browse the repository at this point in the history
  • Loading branch information
mdevaev committed Mar 28, 2024
1 parent d64077c commit a21f527
Show file tree
Hide file tree
Showing 14 changed files with 85 additions and 39 deletions.
5 changes: 3 additions & 2 deletions janus/src/audio.c
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Expand Down Expand Up @@ -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;
Expand Down
3 changes: 2 additions & 1 deletion janus/src/memsinkfd.c
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Expand Down Expand Up @@ -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) {
Expand Down
3 changes: 2 additions & 1 deletion janus/src/plugin.c
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Expand Down Expand Up @@ -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;
}
}
Expand Down
1 change: 1 addition & 0 deletions janus/src/uslibs/errors.h
1 change: 1 addition & 0 deletions python/src/uslibs/errors.h
7 changes: 4 additions & 3 deletions python/src/ustreamer.c
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Expand Down Expand Up @@ -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) {
Expand All @@ -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;
}

Expand Down
7 changes: 4 additions & 3 deletions src/dump/main.c
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Expand Down Expand Up @@ -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();
Expand Down Expand Up @@ -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;
Expand Down
17 changes: 11 additions & 6 deletions src/libs/capture.c
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,7 @@
#include <linux/v4l2-controls.h>

#include "types.h"
#include "errors.h"
#include "tools.h"
#include "array.h"
#include "logging.h"
Expand Down Expand Up @@ -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 ...");
Expand All @@ -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;
}
}

Expand Down Expand Up @@ -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;
Expand Down Expand Up @@ -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) {
Expand Down Expand Up @@ -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");
Expand Down
19 changes: 10 additions & 9 deletions src/libs/drm/drm.c
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,7 @@
#include <libdrm/drm.h>

#include "../types.h"
#include "../errors.h"
#include "../tools.h"
#include "../logging.h"
#include "../frame.h"
Expand Down Expand Up @@ -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;
}

Expand Down Expand Up @@ -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)) {
Expand Down Expand Up @@ -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) {
Expand Down Expand Up @@ -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. Так что просто считаем,
// что отсоединенный монитор на этом этапе - это нормально.
Expand All @@ -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);
Expand Down Expand Up @@ -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);
Expand Down Expand Up @@ -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);
Expand Down Expand Up @@ -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);
Expand Down Expand Up @@ -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) {
Expand Down
27 changes: 27 additions & 0 deletions src/libs/errors.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
/*****************************************************************************
# #
# uStreamer - Lightweight and fast MJPEG-HTTP streamer. #
# #
# Copyright (C) 2018-2024 Maxim Devaev <[email protected]> #
# #
# 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
9 changes: 5 additions & 4 deletions src/libs/memsink.c
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,7 @@
#include <sys/mman.h>

#include "types.h"
#include "errors.h"
#include "tools.h"
#include "logging.h"
#include "frame.h"
Expand Down Expand Up @@ -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) {
Expand Down Expand Up @@ -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;
Expand All @@ -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) {
Expand All @@ -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;
}

Expand Down
2 changes: 1 addition & 1 deletion src/libs/tc358743.c
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down
Loading

0 comments on commit a21f527

Please sign in to comment.