Files
RetroArch/camera/camera_driver.c
libretroadmin 8042f59dbc camera: frames reach the core as planar YCbCr, in place
RETRO_CAMERA_BUFFER_PLANAR hands a core the capture device's own 4:2:0
planes, untouched, for the duration of the callback; the frame
callback sits past the end of retro_camera_callback and is read only
when its caps bit is set, so older cores are never read past.

The Android driver picks the cheapest delivery the core accepts: the
SurfaceTexture for a GL hardware context, the planes for a planar
core, and XRGB8888 converted once by image_yuv_blit only for a core
that takes nothing else. Planes come from Camera2 through the NDK
(resolved at runtime, the HAL buffer mapped where it lies) and fall
back to the legacy Camera's NV21 preview buffers, rotated between the
camera and the reader without a copy. No permission is declared: a
port that declares CAMERA gets the prompt, others get no camera.
2026-10-10 19:44:44 +02:00

182 lines
5.2 KiB
C

/* RetroArch - A frontend for libretro.
* Copyright (C) 2010-2014 - Hans-Kristian Arntzen
* Copyright (C) 2011-2021 - Daniel De Matteis
*
* RetroArch 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 Found-
* ation, either version 3 of the License, or (at your option) any later version.
*
* RetroArch 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 RetroArch.
* If not, see <http://www.gnu.org/licenses/>.
*/
#include <stdint.h>
#include <libretro.h>
#include <string/stdstring.h>
#include "../configuration.h"
#include "../driver.h"
#include "../list_special.h"
#include "../runloop.h"
#include "../verbosity.h"
#include "camera_driver.h"
#if defined(HAVE_FFMPEG) && defined(HAVE_AVFORMAT) && defined(HAVE_AVCODEC) && \
defined(HAVE_AVDEVICE) && defined(HAVE_AVUTIL) && defined(HAVE_SWSCALE)
/* FFMPEG consists of several libraries, and the camera driver needs most of them.
* The camera driver uses APIs introduced in avformat 58 (ffmpeg 4.0). */
#include <libavformat/version.h>
#if LIBAVFORMAT_VERSION_MAJOR >= 58
#define HAVE_FFMPEG_CAMERA
#endif
#endif
static void *nullcamera_init(const char *device, uint64_t caps,
unsigned dims) { return (void*)-1; }
static void nullcamera_free(void *data) { }
static void nullcamera_stop(void *data) { }
static bool nullcamera_start(void *data) { return true; }
static bool nullcamera_poll(void *a,
retro_camera_frame_raw_framebuffer_t b,
retro_camera_frame_opengl_texture_t c,
retro_camera_frame_planar_t d) { return true; }
static camera_driver_t camera_null = {
nullcamera_init,
nullcamera_free,
nullcamera_start,
nullcamera_stop,
nullcamera_poll,
"null",
};
const camera_driver_t *camera_drivers[] = {
#ifdef HAVE_V4L2
&camera_v4l2,
#endif
#if defined(HAVE_PIPEWIRE) && defined(HAVE_PIPEWIRE_STABLE)
&camera_pipewire,
#endif
#ifdef __EMSCRIPTEN__
&camera_rwebcam,
#endif
#ifdef ANDROID
&camera_android,
#endif
#ifdef HAVE_AVF
&camera_avfoundation,
#endif
#ifdef HAVE_FFMPEG_CAMERA
&camera_ffmpeg,
#endif
&camera_null,
NULL,
};
static camera_driver_state_t camera_driver_st;
camera_driver_state_t *camera_state_get_ptr(void)
{
return &camera_driver_st;
}
/**
* config_get_camera_driver_options:
*
* Get an enumerated list of all camera driver names,
* separated by '|'.
*
* Returns: string listing of all camera driver names,
* separated by '|'.
**/
const char *config_get_camera_driver_options(void)
{
return char_list_new_special(STRING_LIST_CAMERA_DRIVERS, NULL);
}
bool driver_camera_start(void)
{
camera_driver_state_t *camera_st = &camera_driver_st;
if ( camera_st
&& camera_st->data
&& camera_st->driver
&& camera_st->driver->start)
{
settings_t *settings = config_get_ptr();
bool camera_allow = settings->bools.camera_allow;
if (camera_allow)
{
bool ok = camera_st->driver->start(camera_st->data);
/* The bit means "poll this every frame": up only when the
* driver can be polled and the core gave it somewhere to
* deliver. Everything the iterate used to re-test lives
* here, once, at the edge. */
runloop_frame_work_set(RUNLOOP_WORK_CAMERA,
ok
&& camera_st->driver->poll
&& camera_st->cb.caps);
return ok;
}
runloop_msg_queue_push(
"Camera is explicitly disabled.\n",
STRLEN_CONST("Camera is explicitly disabled.\n"),
1, 180, false,
NULL, MESSAGE_QUEUE_ICON_DEFAULT, MESSAGE_QUEUE_CATEGORY_INFO);
}
return true;
}
void driver_camera_stop(void)
{
camera_driver_state_t *camera_st = &camera_driver_st;
if ( camera_st->driver
&& camera_st->driver->stop
&& camera_st->data)
camera_st->driver->stop(camera_st->data);
runloop_frame_work_set(RUNLOOP_WORK_CAMERA, false);
}
bool camera_driver_find_driver(const char *prefix,
bool verbosity_enabled)
{
settings_t *settings = config_get_ptr();
camera_driver_state_t
*camera_st = &camera_driver_st;
int i = (int)driver_find_index(
"camera_driver",
settings->arrays.camera_driver);
if (i >= 0)
camera_st->driver = (const camera_driver_t*)camera_drivers[i];
else
{
if (verbosity_enabled)
{
unsigned d;
RARCH_ERR("Couldn't find any %s named \"%s\"\n", prefix,
settings->arrays.camera_driver);
RARCH_LOG_OUTPUT("Available %ss are:\n", prefix);
for (d = 0; camera_drivers[d]; d++)
{
if (camera_drivers[d])
{
RARCH_LOG_OUTPUT("\t%s\n", camera_drivers[d]->ident);
}
}
RARCH_WARN("Going to default to first %s...\n", prefix);
}
if (!(camera_st->driver = (const camera_driver_t*)camera_drivers[0]))
return false;
}
return true;
}