2015-08-07 14:37:41 +02:00
|
|
|
|
|
|
|
// STL includes
|
2015-08-20 09:51:44 +02:00
|
|
|
#include <algorithm>
|
2015-08-07 14:37:41 +02:00
|
|
|
#include <cassert>
|
|
|
|
#include <iostream>
|
2016-07-24 19:56:41 +02:00
|
|
|
|
2015-08-07 14:37:41 +02:00
|
|
|
// Linux includes
|
|
|
|
#include <errno.h>
|
|
|
|
#include <fcntl.h>
|
|
|
|
#include <unistd.h>
|
|
|
|
#include <sys/ioctl.h>
|
|
|
|
#include <sys/stat.h>
|
|
|
|
#include <sys/types.h>
|
|
|
|
|
|
|
|
// Local includes
|
2016-07-24 19:56:41 +02:00
|
|
|
#include <utils/Logger.h>
|
2016-02-24 14:42:25 +01:00
|
|
|
#include <grabber/AmlogicGrabber.h>
|
2015-08-07 14:37:41 +02:00
|
|
|
|
2015-08-20 09:51:44 +02:00
|
|
|
// Flags copied from 'include/linux/amlogic/amports/amvideocap.h' at https://github.com/codesnake/linux-amlogic
|
2015-08-07 14:37:41 +02:00
|
|
|
#define AMVIDEOCAP_IOC_MAGIC 'V'
|
2015-08-20 09:51:44 +02:00
|
|
|
#define AMVIDEOCAP_IOW_SET_WANTFRAME_WIDTH _IOW(AMVIDEOCAP_IOC_MAGIC, 0x02, int)
|
|
|
|
#define AMVIDEOCAP_IOW_SET_WANTFRAME_HEIGHT _IOW(AMVIDEOCAP_IOC_MAGIC, 0x03, int)
|
|
|
|
|
|
|
|
// Flags copied from 'include/linux/amlogic/amports/amvstream.h' at https://github.com/codesnake/linux-amlogic
|
|
|
|
#define AMSTREAM_IOC_MAGIC 'S'
|
|
|
|
#define AMSTREAM_IOC_GET_VIDEO_DISABLE _IOR(AMSTREAM_IOC_MAGIC, 0x48, unsigned long)
|
2015-08-07 14:37:41 +02:00
|
|
|
|
2016-07-24 19:56:41 +02:00
|
|
|
AmlogicGrabber::AmlogicGrabber(const unsigned width, const unsigned height)
|
|
|
|
: _width(std::max(160u, width)) // Minimum required width or height is 160
|
|
|
|
, _height(std::max(160u, height))
|
|
|
|
, _amlogicCaptureDev(-1)
|
2016-07-24 20:31:25 +02:00
|
|
|
, _log(Logger::getInstance("AMLOGICGRABBER"))
|
2015-08-07 14:37:41 +02:00
|
|
|
{
|
2016-07-24 19:56:41 +02:00
|
|
|
Debug(_log, "constructed(%d x %d)",_width,_height);
|
2015-08-07 14:37:41 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
AmlogicGrabber::~AmlogicGrabber()
|
|
|
|
{
|
|
|
|
if (_amlogicCaptureDev != -1)
|
|
|
|
{
|
|
|
|
if (close(_amlogicCaptureDev) == -1)
|
|
|
|
{
|
2016-07-24 19:56:41 +02:00
|
|
|
Error(_log, "Failed to close device (%d - %s)", errno, strerror(errno));
|
2015-08-07 14:37:41 +02:00
|
|
|
}
|
|
|
|
_amlogicCaptureDev = -1;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
void AmlogicGrabber::setVideoMode(const VideoMode videoMode)
|
|
|
|
{
|
|
|
|
switch (videoMode) {
|
|
|
|
case VIDEO_3DSBS:
|
|
|
|
//vc_dispmanx_rect_set(&_rectangle, 0, 0, _width/2, _height);
|
|
|
|
break;
|
|
|
|
case VIDEO_3DTAB:
|
|
|
|
//vc_dispmanx_rect_set(&_rectangle, 0, 0, _width, _height/2);
|
|
|
|
break;
|
|
|
|
case VIDEO_2D:
|
|
|
|
default:
|
|
|
|
//vc_dispmanx_rect_set(&_rectangle, 0, 0, _width, _height);
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2015-08-20 09:51:44 +02:00
|
|
|
bool AmlogicGrabber::isVideoPlaying()
|
|
|
|
{
|
|
|
|
const std::string videoDevice = "/dev/amvideo";
|
|
|
|
|
|
|
|
// Open the video device
|
|
|
|
int video_fd = open(videoDevice.c_str(), O_RDONLY);
|
|
|
|
if (video_fd < 0)
|
|
|
|
{
|
2016-07-24 19:56:41 +02:00
|
|
|
Error(_log, "Failed to open video device(%s): %d - %s", videoDevice.c_str(), errno, strerror(errno));
|
2015-08-20 09:51:44 +02:00
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
// Check the video disabled flag
|
|
|
|
int videoDisabled;
|
|
|
|
if (ioctl(video_fd, AMSTREAM_IOC_GET_VIDEO_DISABLE, &videoDisabled) == -1)
|
|
|
|
{
|
2016-07-24 19:56:41 +02:00
|
|
|
Error(_log, "Failed to retrieve video state from device: %d - %s", errno, strerror(errno));
|
2015-08-20 09:51:44 +02:00
|
|
|
close(video_fd);
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
// Make sure to close the device after use
|
|
|
|
close(video_fd);
|
|
|
|
|
|
|
|
return videoDisabled == 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
int AmlogicGrabber::grabFrame(Image<ColorBgr> & image)
|
2015-08-07 14:37:41 +02:00
|
|
|
{
|
|
|
|
// resize the given image if needed
|
2015-08-08 08:13:59 +02:00
|
|
|
if (image.width() != _width || image.height() != _height)
|
2015-08-07 14:37:41 +02:00
|
|
|
{
|
2015-08-08 08:13:59 +02:00
|
|
|
image.resize(_width, _height);
|
2015-08-07 14:37:41 +02:00
|
|
|
}
|
2015-08-20 09:51:44 +02:00
|
|
|
|
|
|
|
// Make sure video is playing, else there is nothing to grab
|
|
|
|
if (!isVideoPlaying())
|
|
|
|
{
|
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
// If the device is not open, attempt to open it
|
2015-08-08 08:13:59 +02:00
|
|
|
if (_amlogicCaptureDev == -1)
|
|
|
|
{
|
2015-08-20 09:51:44 +02:00
|
|
|
_amlogicCaptureDev = open("/dev/amvideocap0", O_RDONLY, 0);
|
|
|
|
|
|
|
|
// If the device is still not open, there is something wrong
|
|
|
|
if (_amlogicCaptureDev == -1)
|
|
|
|
{
|
2016-07-24 19:56:41 +02:00
|
|
|
Error(_log,"Failed to open the AMLOGIC device (%d - %s):", errno, strerror(errno));
|
2015-08-20 09:51:44 +02:00
|
|
|
return -1;
|
|
|
|
}
|
2015-08-08 08:13:59 +02:00
|
|
|
}
|
2015-08-20 09:51:44 +02:00
|
|
|
|
|
|
|
|
|
|
|
if (ioctl(_amlogicCaptureDev, AMVIDEOCAP_IOW_SET_WANTFRAME_WIDTH, _width) == -1 ||
|
|
|
|
ioctl(_amlogicCaptureDev, AMVIDEOCAP_IOW_SET_WANTFRAME_HEIGHT, _height) == -1)
|
2015-08-08 08:13:59 +02:00
|
|
|
{
|
|
|
|
// Failed to configure frame width
|
2016-07-24 19:56:41 +02:00
|
|
|
Error(_log,"Failed to configure capture size (%d - %s)", errno, strerror(errno));
|
2015-09-03 21:37:49 +02:00
|
|
|
close(_amlogicCaptureDev);
|
|
|
|
_amlogicCaptureDev = -1;
|
2015-08-20 09:51:44 +02:00
|
|
|
return -1;
|
2015-08-08 08:13:59 +02:00
|
|
|
}
|
2015-08-20 09:51:44 +02:00
|
|
|
|
2015-08-07 14:37:41 +02:00
|
|
|
// Read the snapshot into the memory
|
|
|
|
void * image_ptr = image.memptr();
|
2015-08-20 09:51:44 +02:00
|
|
|
const ssize_t bytesToRead = _width * _height * sizeof(ColorBgr);
|
|
|
|
|
|
|
|
const ssize_t bytesRead = pread(_amlogicCaptureDev, image_ptr, bytesToRead, 0);
|
|
|
|
if (bytesRead == -1)
|
|
|
|
{
|
2016-07-24 19:56:41 +02:00
|
|
|
Error(_log,"Read of device failed: %d - %s", errno, strerror(errno));
|
2015-09-03 21:37:49 +02:00
|
|
|
close(_amlogicCaptureDev);
|
|
|
|
_amlogicCaptureDev = -1;
|
2015-08-20 09:51:44 +02:00
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
else if (bytesToRead != bytesRead)
|
2015-08-07 14:37:41 +02:00
|
|
|
{
|
|
|
|
// Read of snapshot failed
|
2016-07-24 19:56:41 +02:00
|
|
|
Error(_log,"Capture failed to grab entire image [bytesToRead(%d) != bytesRead(%d)]", bytesToRead, bytesRead);
|
2015-09-03 21:37:49 +02:00
|
|
|
close(_amlogicCaptureDev);
|
|
|
|
_amlogicCaptureDev = -1;
|
2015-08-20 09:51:44 +02:00
|
|
|
return -1;
|
2015-08-07 14:37:41 +02:00
|
|
|
}
|
2015-08-20 09:51:44 +02:00
|
|
|
|
2015-09-03 21:37:49 +02:00
|
|
|
// For now we always close the device now and again
|
|
|
|
static int readCnt = 0;
|
|
|
|
++readCnt;
|
|
|
|
if (readCnt > 20)
|
|
|
|
{
|
|
|
|
close(_amlogicCaptureDev);
|
|
|
|
_amlogicCaptureDev = -1;
|
|
|
|
readCnt = 0;
|
|
|
|
}
|
2015-08-20 09:51:44 +02:00
|
|
|
return 0;
|
2015-08-07 14:37:41 +02:00
|
|
|
}
|