2015-01-24 23:42:22 +01:00
|
|
|
// STL includes
|
|
|
|
#include <cassert>
|
|
|
|
#include <iostream>
|
|
|
|
|
|
|
|
// Local includes
|
2016-03-09 11:03:20 +01:00
|
|
|
#include <grabber/OsxFrameGrabber.h>
|
2015-01-24 23:42:22 +01:00
|
|
|
|
2017-08-04 23:08:15 +02:00
|
|
|
OsxFrameGrabber::OsxFrameGrabber(const unsigned display, const unsigned width, const unsigned height)
|
|
|
|
: Grabber("OSXGRABBER", width, height)
|
2018-12-27 23:11:32 +01:00
|
|
|
, _screenIndex(100)
|
2015-01-24 23:42:22 +01:00
|
|
|
{
|
2018-12-27 23:11:32 +01:00
|
|
|
// check if display is available
|
|
|
|
setDisplayIndex(display);
|
2015-01-24 23:42:22 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
OsxFrameGrabber::~OsxFrameGrabber()
|
|
|
|
{
|
|
|
|
}
|
|
|
|
|
2017-08-12 07:55:32 +02:00
|
|
|
int OsxFrameGrabber::grabFrame(Image<ColorRgb> & image)
|
2015-01-24 23:42:22 +01:00
|
|
|
{
|
2017-09-01 08:50:37 +02:00
|
|
|
if (!_enabled) return 0;
|
|
|
|
|
2015-01-24 23:42:22 +01:00
|
|
|
CGImageRef dispImage;
|
|
|
|
CFDataRef imgData;
|
2018-12-27 23:11:32 +01:00
|
|
|
unsigned char * pImgData;
|
2015-01-24 23:42:22 +01:00
|
|
|
unsigned dspWidth, dspHeight;
|
2018-12-27 23:11:32 +01:00
|
|
|
|
2015-01-24 23:42:22 +01:00
|
|
|
dispImage = CGDisplayCreateImage(_display);
|
2018-12-27 23:11:32 +01:00
|
|
|
|
2017-08-04 23:08:15 +02:00
|
|
|
// display lost, use main
|
2015-01-24 23:42:22 +01:00
|
|
|
if (dispImage == NULL && _display)
|
|
|
|
{
|
|
|
|
dispImage = CGDisplayCreateImage(kCGDirectMainDisplay);
|
|
|
|
// no displays connected, return
|
|
|
|
if (dispImage == NULL)
|
|
|
|
{
|
2016-07-12 00:53:48 +02:00
|
|
|
Error(_log, "No display connected...");
|
2017-08-12 07:55:32 +02:00
|
|
|
return -1;
|
2015-01-24 23:42:22 +01:00
|
|
|
}
|
|
|
|
}
|
2017-08-12 07:55:32 +02:00
|
|
|
imgData = CGDataProviderCopyData(CGImageGetDataProvider(dispImage));
|
|
|
|
pImgData = (unsigned char*) CFDataGetBytePtr(imgData);
|
|
|
|
dspWidth = CGImageGetWidth(dispImage);
|
2015-01-24 23:42:22 +01:00
|
|
|
dspHeight = CGImageGetHeight(dispImage);
|
2018-12-27 23:11:32 +01:00
|
|
|
|
2017-08-04 23:08:15 +02:00
|
|
|
_imageResampler.setHorizontalPixelDecimation(dspWidth/_width);
|
|
|
|
_imageResampler.setVerticalPixelDecimation(dspHeight/_height);
|
|
|
|
_imageResampler.processImage( pImgData,
|
2015-01-24 23:42:22 +01:00
|
|
|
dspWidth,
|
|
|
|
dspHeight,
|
|
|
|
CGImageGetBytesPerRow(dispImage),
|
2020-06-28 23:05:32 +02:00
|
|
|
PixelFormat::BGR32,
|
2015-01-24 23:42:22 +01:00
|
|
|
image);
|
2018-12-27 23:11:32 +01:00
|
|
|
|
2015-01-24 23:42:22 +01:00
|
|
|
CFRelease(imgData);
|
|
|
|
CGImageRelease(dispImage);
|
2017-08-12 07:55:32 +02:00
|
|
|
|
|
|
|
return 0;
|
2015-01-24 23:42:22 +01:00
|
|
|
}
|
2018-12-27 23:11:32 +01:00
|
|
|
|
|
|
|
void OsxFrameGrabber::setDisplayIndex(int index)
|
|
|
|
{
|
|
|
|
if(_screenIndex != index)
|
|
|
|
{
|
|
|
|
_screenIndex = index;
|
|
|
|
|
|
|
|
CGImageRef image;
|
|
|
|
CGDisplayCount displayCount;
|
|
|
|
CGDirectDisplayID displays[8];
|
|
|
|
|
|
|
|
// get list of displays
|
|
|
|
CGGetActiveDisplayList(8, displays, &displayCount);
|
|
|
|
if (_screenIndex + 1 > displayCount)
|
|
|
|
{
|
|
|
|
Error(_log, "Display with index %d is not available. Using main display", _screenIndex);
|
|
|
|
_display = kCGDirectMainDisplay;
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
_display = displays[_screenIndex];
|
|
|
|
}
|
|
|
|
|
|
|
|
image = CGDisplayCreateImage(_display);
|
2018-12-28 18:12:45 +01:00
|
|
|
if(image == NULL)
|
|
|
|
{
|
|
|
|
Error(_log, "Failed to open main display, disable capture interface");
|
|
|
|
setEnabled(false);
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
else
|
|
|
|
setEnabled(true);
|
2018-12-27 23:11:32 +01:00
|
|
|
|
|
|
|
Info(_log, "Display opened with resolution: %dx%d@%dbit", CGImageGetWidth(image), CGImageGetHeight(image), CGImageGetBitsPerPixel(image));
|
|
|
|
|
|
|
|
CGImageRelease(image);
|
|
|
|
}
|
|
|
|
}
|