2015-01-12 18:42:43 +00:00
|
|
|
/* RetroArch - A frontend for libretro.
|
|
|
|
* Copyright (C) 2010-2014 - Hans-Kristian Arntzen
|
|
|
|
* Copyright (C) 2011-2015 - 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 <string.h>
|
2015-09-04 19:13:12 +00:00
|
|
|
|
2015-01-12 18:42:43 +00:00
|
|
|
#include "camera_driver.h"
|
2015-01-12 18:44:29 +00:00
|
|
|
#include "../general.h"
|
2015-10-26 07:42:34 +00:00
|
|
|
#include "../string_list_special.h"
|
2015-12-06 18:31:47 +00:00
|
|
|
#include "../system.h"
|
2015-11-23 11:03:38 +00:00
|
|
|
#include "../verbosity.h"
|
2015-01-12 18:42:43 +00:00
|
|
|
|
|
|
|
static const camera_driver_t *camera_drivers[] = {
|
|
|
|
#ifdef HAVE_V4L2
|
|
|
|
&camera_v4l2,
|
|
|
|
#endif
|
|
|
|
#ifdef EMSCRIPTEN
|
|
|
|
&camera_rwebcam,
|
|
|
|
#endif
|
|
|
|
#ifdef ANDROID
|
|
|
|
&camera_android,
|
|
|
|
#endif
|
2015-04-20 12:14:45 +00:00
|
|
|
#if defined(HAVE_AVFOUNDATION)
|
|
|
|
#if defined(HAVE_COCOA) || defined(HAVE_COCOATOUCH)
|
2015-04-20 09:44:28 +00:00
|
|
|
&camera_avfoundation,
|
2015-04-20 12:14:45 +00:00
|
|
|
#endif
|
2015-01-12 18:42:43 +00:00
|
|
|
#endif
|
|
|
|
&camera_null,
|
|
|
|
NULL,
|
|
|
|
};
|
|
|
|
|
2015-12-05 14:52:25 +00:00
|
|
|
static const camera_driver_t *camera_driver;
|
|
|
|
static void *camera_data;
|
|
|
|
|
2015-01-12 18:42:43 +00:00
|
|
|
/**
|
|
|
|
* camera_driver_find_handle:
|
2015-01-15 20:10:36 +00:00
|
|
|
* @idx : index of driver to get handle to.
|
2015-01-12 18:42:43 +00:00
|
|
|
*
|
|
|
|
* Returns: handle to camera driver at index. Can be NULL
|
|
|
|
* if nothing found.
|
|
|
|
**/
|
2015-01-15 20:10:36 +00:00
|
|
|
const void *camera_driver_find_handle(int idx)
|
2015-01-12 18:42:43 +00:00
|
|
|
{
|
2015-01-15 20:10:36 +00:00
|
|
|
const void *drv = camera_drivers[idx];
|
2015-01-12 18:42:43 +00:00
|
|
|
if (!drv)
|
|
|
|
return NULL;
|
|
|
|
return drv;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* camera_driver_find_ident:
|
2015-01-15 20:10:36 +00:00
|
|
|
* @idx : index of driver to get handle to.
|
2015-01-12 18:42:43 +00:00
|
|
|
*
|
|
|
|
* Returns: Human-readable identifier of camera driver at index. Can be NULL
|
|
|
|
* if nothing found.
|
|
|
|
**/
|
2015-01-15 20:10:36 +00:00
|
|
|
const char *camera_driver_find_ident(int idx)
|
2015-01-12 18:42:43 +00:00
|
|
|
{
|
2015-01-15 20:10:36 +00:00
|
|
|
const camera_driver_t *drv = camera_drivers[idx];
|
2015-01-12 18:42:43 +00:00
|
|
|
if (!drv)
|
|
|
|
return NULL;
|
|
|
|
return drv->ident;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* 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)
|
|
|
|
{
|
2015-10-26 18:41:20 +00:00
|
|
|
return char_list_new_special(STRING_LIST_CAMERA_DRIVERS, NULL);
|
2015-01-12 18:42:43 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
void find_camera_driver(void)
|
|
|
|
{
|
2015-03-20 18:48:23 +00:00
|
|
|
settings_t *settings = config_get_ptr();
|
|
|
|
int i = find_driver_index("camera_driver", settings->camera.driver);
|
2015-03-18 18:40:00 +00:00
|
|
|
|
2015-01-12 18:42:43 +00:00
|
|
|
if (i >= 0)
|
2015-12-05 14:52:25 +00:00
|
|
|
camera_driver = (const camera_driver_t*)camera_driver_find_handle(i);
|
2015-01-12 18:42:43 +00:00
|
|
|
else
|
|
|
|
{
|
|
|
|
unsigned d;
|
|
|
|
RARCH_ERR("Couldn't find any camera driver named \"%s\"\n",
|
2015-03-20 18:48:23 +00:00
|
|
|
settings->camera.driver);
|
2015-01-12 18:42:43 +00:00
|
|
|
RARCH_LOG_OUTPUT("Available camera drivers are:\n");
|
|
|
|
for (d = 0; camera_driver_find_handle(d); d++)
|
|
|
|
RARCH_LOG_OUTPUT("\t%s\n", camera_driver_find_ident(d));
|
|
|
|
|
|
|
|
RARCH_WARN("Going to default to first camera driver...\n");
|
|
|
|
|
2015-12-05 14:52:25 +00:00
|
|
|
camera_driver = (const camera_driver_t*)camera_driver_find_handle(0);
|
2015-01-12 18:42:43 +00:00
|
|
|
|
2015-12-05 14:52:25 +00:00
|
|
|
if (!camera_driver)
|
2015-10-26 02:18:13 +00:00
|
|
|
retro_fail(1, "find_camera_driver()");
|
2015-01-12 18:42:43 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* driver_camera_start:
|
|
|
|
*
|
|
|
|
* Starts camera driver interface.
|
|
|
|
* Used by RETRO_ENVIRONMENT_GET_CAMERA_INTERFACE.
|
|
|
|
*
|
|
|
|
* Returns: true (1) if successful, otherwise false (0).
|
|
|
|
**/
|
|
|
|
bool driver_camera_start(void)
|
|
|
|
{
|
2015-03-20 18:48:23 +00:00
|
|
|
settings_t *settings = config_get_ptr();
|
|
|
|
|
2015-12-05 14:52:25 +00:00
|
|
|
if (camera_driver && camera_data && camera_driver->start)
|
2015-01-12 18:42:43 +00:00
|
|
|
{
|
2015-03-20 18:48:23 +00:00
|
|
|
if (settings->camera.allow)
|
2015-12-05 14:52:25 +00:00
|
|
|
return camera_driver->start(camera_data);
|
2015-01-12 18:42:43 +00:00
|
|
|
|
2015-03-15 01:59:38 +00:00
|
|
|
rarch_main_msg_queue_push(
|
|
|
|
"Camera is explicitly disabled.\n", 1, 180, false);
|
2015-01-12 18:42:43 +00:00
|
|
|
}
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* driver_camera_stop:
|
|
|
|
*
|
|
|
|
* Stops camera driver.
|
|
|
|
* Used by RETRO_ENVIRONMENT_GET_CAMERA_INTERFACE.
|
|
|
|
*
|
|
|
|
* Returns: true (1) if successful, otherwise false (0).
|
|
|
|
**/
|
|
|
|
void driver_camera_stop(void)
|
|
|
|
{
|
2015-12-05 14:52:25 +00:00
|
|
|
if (camera_driver && camera_driver->stop && camera_data)
|
|
|
|
camera_driver->stop(camera_data);
|
2015-01-12 18:42:43 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* driver_camera_poll:
|
|
|
|
*
|
|
|
|
* Call camera driver's poll function.
|
|
|
|
* Used by RETRO_ENVIRONMENT_GET_CAMERA_INTERFACE.
|
|
|
|
*
|
|
|
|
* Returns: true (1) if successful, otherwise false (0).
|
|
|
|
**/
|
|
|
|
void driver_camera_poll(void)
|
|
|
|
{
|
2015-06-25 11:00:35 +00:00
|
|
|
rarch_system_info_t *system = rarch_system_info_get_ptr();
|
2015-03-21 03:43:18 +00:00
|
|
|
|
2015-12-05 14:52:25 +00:00
|
|
|
if (camera_driver && camera_driver->poll && camera_data)
|
|
|
|
camera_driver->poll(camera_data,
|
2015-06-25 11:00:35 +00:00
|
|
|
system->camera_callback.frame_raw_framebuffer,
|
|
|
|
system->camera_callback.frame_opengl_texture);
|
2015-01-12 18:42:43 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
void init_camera(void)
|
|
|
|
{
|
2015-12-05 14:52:25 +00:00
|
|
|
settings_t *settings = config_get_ptr();
|
2015-06-25 11:00:35 +00:00
|
|
|
rarch_system_info_t *system = rarch_system_info_get_ptr();
|
2015-03-18 18:40:00 +00:00
|
|
|
|
2015-06-25 11:00:35 +00:00
|
|
|
/* Resource leaks will follow if camera is initialized twice. */
|
2015-12-05 14:52:25 +00:00
|
|
|
if (camera_data)
|
2015-01-12 18:42:43 +00:00
|
|
|
return;
|
|
|
|
|
|
|
|
find_camera_driver();
|
|
|
|
|
2015-12-05 14:52:25 +00:00
|
|
|
camera_data = camera_driver->init(
|
2015-03-20 18:48:23 +00:00
|
|
|
*settings->camera.device ? settings->camera.device : NULL,
|
2015-06-25 11:00:35 +00:00
|
|
|
system->camera_callback.caps,
|
2015-03-20 18:48:23 +00:00
|
|
|
settings->camera.width ?
|
2015-06-25 11:00:35 +00:00
|
|
|
settings->camera.width : system->camera_callback.width,
|
2015-03-20 18:48:23 +00:00
|
|
|
settings->camera.height ?
|
2015-06-25 11:00:35 +00:00
|
|
|
settings->camera.height : system->camera_callback.height);
|
2015-01-12 18:42:43 +00:00
|
|
|
|
2015-12-05 14:52:25 +00:00
|
|
|
if (!camera_data)
|
2015-01-12 18:42:43 +00:00
|
|
|
{
|
|
|
|
RARCH_ERR("Failed to initialize camera driver. Will continue without camera.\n");
|
2015-12-05 13:39:52 +00:00
|
|
|
camera_driver_ctl(RARCH_CAMERA_CTL_UNSET_ACTIVE, NULL);
|
2015-01-12 18:42:43 +00:00
|
|
|
}
|
|
|
|
|
2015-06-25 11:00:35 +00:00
|
|
|
if (system->camera_callback.initialized)
|
|
|
|
system->camera_callback.initialized();
|
2015-01-12 18:42:43 +00:00
|
|
|
}
|
|
|
|
|
2015-12-05 22:03:05 +00:00
|
|
|
static void uninit_camera(void)
|
2015-01-12 18:42:43 +00:00
|
|
|
{
|
2015-06-25 11:00:35 +00:00
|
|
|
rarch_system_info_t *system = rarch_system_info_get_ptr();
|
2015-03-18 18:40:00 +00:00
|
|
|
|
2015-12-05 14:52:25 +00:00
|
|
|
if (camera_data && camera_driver)
|
2015-01-12 18:42:43 +00:00
|
|
|
{
|
2015-06-25 11:00:35 +00:00
|
|
|
if (system->camera_callback.deinitialized)
|
|
|
|
system->camera_callback.deinitialized();
|
2015-01-12 18:42:43 +00:00
|
|
|
|
2015-12-05 14:52:25 +00:00
|
|
|
if (camera_driver->free)
|
|
|
|
camera_driver->free(camera_data);
|
2015-01-12 18:42:43 +00:00
|
|
|
}
|
2015-12-05 14:52:25 +00:00
|
|
|
|
|
|
|
camera_data = NULL;
|
2015-01-12 18:42:43 +00:00
|
|
|
}
|
2015-12-05 13:39:52 +00:00
|
|
|
|
|
|
|
bool camera_driver_ctl(enum rarch_camera_ctl_state state, void *data)
|
|
|
|
{
|
|
|
|
static bool camera_driver_active = false;
|
|
|
|
static bool camera_driver_data_own = false;
|
|
|
|
|
|
|
|
switch (state)
|
|
|
|
{
|
|
|
|
case RARCH_CAMERA_CTL_SET_OWN_DRIVER:
|
|
|
|
camera_driver_data_own = true;
|
|
|
|
break;
|
|
|
|
case RARCH_CAMERA_CTL_UNSET_OWN_DRIVER:
|
|
|
|
camera_driver_data_own = false;
|
|
|
|
break;
|
|
|
|
case RARCH_CAMERA_CTL_OWNS_DRIVER:
|
|
|
|
return camera_driver_data_own;
|
|
|
|
case RARCH_CAMERA_CTL_SET_ACTIVE:
|
|
|
|
camera_driver_active = true;
|
|
|
|
break;
|
|
|
|
case RARCH_CAMERA_CTL_UNSET_ACTIVE:
|
|
|
|
camera_driver_active = false;
|
|
|
|
break;
|
|
|
|
case RARCH_CAMERA_CTL_IS_ACTIVE:
|
|
|
|
return camera_driver_active;
|
2015-12-05 22:03:05 +00:00
|
|
|
case RARCH_CAMERA_CTL_DEINIT:
|
|
|
|
uninit_camera();
|
|
|
|
break;
|
2015-12-05 13:39:52 +00:00
|
|
|
default:
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
|
|
|
return false;
|
|
|
|
}
|