Finished the amlogic grabber for the 'WeTek Play'

Former-commit-id: e459cdfe6273ad2bfee92d2d190801ebdc691a5c
This commit is contained in:
T.van der Zwan
2015-08-20 09:51:44 +02:00
parent 348e0c1ee8
commit 5497fbf577
11 changed files with 265 additions and 71 deletions

View File

@@ -1,5 +1,6 @@
// STL includes
#include <algorithm>
#include <cassert>
#include <iostream>
@@ -14,16 +15,22 @@
// Local includes
#include "AmlogicGrabber.h"
// Flags copied from 'linux/amlogic/amports/amvideocap.h' at https://github.com/codesnake/linux-amlogic/
// Flags copied from 'include/linux/amlogic/amports/amvideocap.h' at https://github.com/codesnake/linux-amlogic
#define AMVIDEOCAP_IOC_MAGIC 'V'
#define AMVIDEOCAP_IOW_SET_WANTFRAME_WIDTH _IOW(AMVIDEOCAP_IOC_MAGIC, 0x02, int)
#define AMVIDEOCAP_IOW_SET_WANTFRAME_HEIGHT _IOW(AMVIDEOCAP_IOC_MAGIC, 0x03, int)
#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)
AmlogicGrabber::AmlogicGrabber(const unsigned width, const unsigned height) :
_width(width),
_height(height),
// Minimum required width or height is 160
_width(std::max(160u, width)),
_height(std::max(160u, height)),
_amlogicCaptureDev(-1)
{
std::cout << "[" << __PRETTY_FUNCTION__ << "] constructed(" << _width << "x" << _height << ")" << std::endl;
}
AmlogicGrabber::~AmlogicGrabber()
@@ -54,40 +61,90 @@ void AmlogicGrabber::setVideoMode(const VideoMode videoMode)
}
}
void AmlogicGrabber::grabFrame(Image<ColorRgb> & image)
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)
{
std::cerr << "Failed to open video device(" << videoDevice << "): " << strerror(errno) << std::endl;
return false;
}
// Check the video disabled flag
int videoDisabled;
if (ioctl(video_fd, AMSTREAM_IOC_GET_VIDEO_DISABLE, &videoDisabled) == -1)
{
std::cerr << "Failed to retrieve video state from device: " << strerror(errno) << std::endl;
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)
{
// resize the given image if needed
if (image.width() != _width || image.height() != _height)
{
image.resize(_width, _height);
}
_amlogicCaptureDev = open("/dev/amvideocap0", O_RDONLY, 0);
// 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
if (_amlogicCaptureDev == -1)
{
std::cerr << "[" << __PRETTY_FUNCTION__ << "] Failed to open the AMLOGIC device (" << errno << ")" << std::endl;
return;
_amlogicCaptureDev = open("/dev/amvideocap0", O_RDONLY, 0);
// If the device is still not open, there is something wrong
if (_amlogicCaptureDev == -1)
{
std::cerr << "[" << __PRETTY_FUNCTION__ << "] Failed to open the AMLOGIC device (" << errno << "): " << strerror(errno) << std::endl;
return -1;
}
}
if (ioctl(_amlogicCaptureDev, AMVIDEOCAP_IOW_SET_WANTFRAME_WIDTH, _width) == -1 ||
ioctl(_amlogicCaptureDev, AMVIDEOCAP_IOW_SET_WANTFRAME_HEIGHT, _height) == -1)
if (ioctl(_amlogicCaptureDev, AMVIDEOCAP_IOW_SET_WANTFRAME_WIDTH, _width) == -1 ||
ioctl(_amlogicCaptureDev, AMVIDEOCAP_IOW_SET_WANTFRAME_HEIGHT, _height) == -1)
{
// Failed to configure frame width
std::cerr << "[" << __PRETTY_FUNCTION__ << "] Failed to configure capture size (" << errno << ")" << std::endl;
return;
std::cerr << "[" << __PRETTY_FUNCTION__ << "] Failed to configure capture size (" << errno << "): " << strerror(errno) << std::endl;
return -1;
}
std::cout << "AMLOGIC grabber created (size " << _width << "x" << _height << ")" << std::endl;
// Read the snapshot into the memory
void * image_ptr = image.memptr();
const size_t bytesToRead = _width * _height * sizeof(ColorRgb);
const size_t bytesRead = pread(_amlogicCaptureDev, image_ptr, bytesToRead, 0);
if (bytesToRead != bytesRead)
const ssize_t bytesToRead = _width * _height * sizeof(ColorBgr);
const ssize_t bytesRead = pread(_amlogicCaptureDev, image_ptr, bytesToRead, 0);
if (bytesRead == -1)
{
std::cerr << "[" << __PRETTY_FUNCTION__ << "] Read of device failed (erno=" << errno << "): " << strerror(errno) << std::endl;
return -1;
}
else if (bytesToRead != bytesRead)
{
// Read of snapshot failed
std::cerr << "[" << __PRETTY_FUNCTION__ << "] Capture failed to grab entire image [bytesToRead(" << bytesToRead << ") != bytesRead(" << bytesRead << ")]" << std::endl;
return -1;
}
// For now we always close the device again
close(_amlogicCaptureDev);
_amlogicCaptureDev = -1;
return 0;
}