-Reload MF Grabber only once per WebUI update

- Cleanup
This commit is contained in:
Paulchen Panther
2021-01-26 20:01:23 +01:00
parent b0afa7cd03
commit 456cccd9cf
10 changed files with 109 additions and 147 deletions

View File

@@ -4,7 +4,7 @@
// Constants
namespace { const bool verbose = false; }
MFGrabber::MFGrabber(const QString & device, unsigned width, unsigned height, unsigned fps, unsigned input, int pixelDecimation, QString flipMode)
MFGrabber::MFGrabber(const QString & device, unsigned width, unsigned height, unsigned fps, int pixelDecimation, QString flipMode)
: Grabber("V4L2:"+device)
, _deviceName(device)
, _hr(S_FALSE)
@@ -30,11 +30,9 @@ MFGrabber::MFGrabber(const QString & device, unsigned width, unsigned height, un
, _x_frac_max(0.75)
, _y_frac_max(0.75)
{
setInput(input);
setWidthHeight(width, height);
setFramerate(fps);
setFlipMode(flipMode);
// setDeviceVideoStandard(device, videoStandard); // TODO
CoInitializeEx(0, COINIT_MULTITHREADED);
_hr = MFStartup(MF_VERSION, MFSTARTUP_NOSOCKET);
@@ -178,7 +176,8 @@ HRESULT MFGrabber::init_device(QString deviceName, DevicePropertiesItem props)
IMFMediaType* type = nullptr;
HRESULT hr = S_OK;
Debug(_log, "Init %s, %d x %d @ %d fps (%s) => %s", QSTRING_CSTR(deviceName), props.x, props.y, props.fps, QSTRING_CSTR(pixelFormatToString(pixelformat)), QSTRING_CSTR(guid));
Debug(_log, "Init %s, %d x %d @ %d fps (%s)", QSTRING_CSTR(deviceName), props.x, props.y, props.fps, QSTRING_CSTR(pixelFormatToString(pixelformat)));
DebugIf(verbose, _log, "Symbolic link: %s", QSTRING_CSTR(guid));
hr = MFCreateAttributes(&deviceAttributes, 2);
if(FAILED(hr))
@@ -426,7 +425,7 @@ void MFGrabber::enumVideoCaptureDevices()
IMFActivate** devices;
if(SUCCEEDED(MFEnumDeviceSources(attr, &devices, &count)))
{
Debug(_log, "Detected devices: %u", count);
DebugIf(verbose, _log, "Detected devices: %u", count);
for(UINT32 i = 0; i < count; i++)
{
UINT32 length;
@@ -441,7 +440,7 @@ void MFGrabber::enumVideoCaptureDevices()
MFGrabber::DeviceProperties properties;
properties.name = QString::fromUtf16((const ushort*)symlink);
Info(_log, "Found capture device: %s", QSTRING_CSTR(dev));
Debug(_log, "Found capture device: %s", QSTRING_CSTR(dev));
IMFMediaSource *pSource = nullptr;
if(SUCCEEDED(devices[i]->ActivateObject(IID_PPV_ARGS(&pSource))))
{
@@ -523,7 +522,7 @@ void MFGrabber::enumVideoCaptureDevices()
void MFGrabber::start_capturing()
{
if (_sourceReader)
if (_initialized && _sourceReader)
{
HRESULT hr = _sourceReader->ReadSample(MF_SOURCE_READER_FIRST_VIDEO_STREAM, 0, NULL, NULL, NULL, NULL);
if (!SUCCEEDED(hr))
@@ -610,7 +609,7 @@ bool MFGrabber::start()
try
{
_threadManager.start();
Info(_log, "Decoding threads: %d",_threadManager._maxThreads);
DebugIf(verbose, _log, "Decoding threads: %d",_threadManager._maxThreads);
if(init())
{
@@ -631,10 +630,11 @@ void MFGrabber::stop()
{
if(_initialized)
{
_initialized = false;
_threadManager.stop();
uninit_device();
_pixelFormat = _pixelFormatConfig;
_deviceProperties.clear();
_initialized = false;
Info(_log, "Stopped");
}
}
@@ -741,6 +741,16 @@ void MFGrabber::setCecDetectionEnable(bool enable)
}
}
bool MFGrabber::setDevice(QString device)
{
if(_deviceName != device)
{
_deviceName = device;
return true;
}
return false;
}
void MFGrabber::setPixelDecimation(int pixelDecimation)
{
if(_pixelDecimation != pixelDecimation)
@@ -753,46 +763,11 @@ void MFGrabber::setFlipMode(QString flipMode)
Grabber::setFlipMode(parseFlipMode(flipMode));
}
void MFGrabber::setDeviceVideoStandard(QString device, VideoStandard videoStandard)
{
if(_deviceName != device)
{
_deviceName = device;
if(_initialized && !device.isEmpty())
{
Debug(_log,"Restarting Media Foundation grabber");
uninit();
start();
}
}
}
bool MFGrabber::setInput(int input)
{
if(Grabber::setInput(input))
{
bool started = _initialized;
uninit();
if(started)
start();
return true;
}
return false;
}
bool MFGrabber::setWidthHeight(int width, int height)
{
if(Grabber::setWidthHeight(width,height))
if(Grabber::setWidthHeight(width, height))
{
Debug(_log,"Set width:height to: %i:&i", width, height);
if(_initialized)
{
Debug(_log,"Restarting Media Foundation grabber");
uninit();
start();
}
Debug(_log,"Set width:height to: %i:%i", width, height);
return true;
}
return false;
@@ -803,12 +778,6 @@ bool MFGrabber::setFramerate(int fps)
if(Grabber::setFramerate(fps))
{
Debug(_log,"Set fps to: %i", fps);
if(_initialized)
{
Debug(_log,"Restarting Media Foundation grabber");
uninit();
start();
}
return true;
}
return false;
@@ -821,22 +790,20 @@ void MFGrabber::setFpsSoftwareDecimation(int decimation)
Debug(_log,"Every %ith image per second are processed", decimation);
}
void MFGrabber::setEncoding(QString enc)
bool MFGrabber::setEncoding(QString enc)
{
if(_pixelFormat != parsePixelFormat(enc))
if(_pixelFormatConfig != parsePixelFormat(enc))
{
Debug(_log,"Set encoding to: %s", QSTRING_CSTR(enc));
_pixelFormat = parsePixelFormat(enc);
if(_initialized)
{
Debug(_log,"Restarting Media Foundation Grabber");
uninit();
start();
}
Debug(_log,"Set hardware encoding to: %s", QSTRING_CSTR(enc.toUpper()));
_pixelFormatConfig = parsePixelFormat(enc);
if(!_initialized)
_pixelFormat = _pixelFormatConfig;
return true;
}
return false;
}
void MFGrabber::setBrightnessContrastSaturationHue(int brightness, int contrast, int saturation, int hue)
bool MFGrabber::setBrightnessContrastSaturationHue(int brightness, int contrast, int saturation, int hue)
{
if(_brightness != brightness || _contrast != contrast || _saturation != saturation || _hue != hue)
{
@@ -846,12 +813,21 @@ void MFGrabber::setBrightnessContrastSaturationHue(int brightness, int contrast,
_hue = hue;
Debug(_log,"Set brightness to %i, contrast to %i, saturation to %i, hue to %i", _brightness, _contrast, _saturation, _hue);
return true;
}
return false;
}
if(_initialized)
{
Debug(_log,"Restarting Media Foundation Grabber");
uninit();
start();
}
void MFGrabber::reloadGrabber()
{
if(_initialized)
{
Debug(_log,"Reloading Media Foundation Grabber");
// stop grabber
uninit();
// restore pixelformat to configs value
_pixelFormat = _pixelFormatConfig;
// start grabber
start();
}
}

View File

@@ -331,7 +331,13 @@ public:
STDMETHODIMP OnFlush(DWORD) { return S_OK; }
private:
virtual ~SourceReaderCB() {}
virtual ~SourceReaderCB()
{
_transform->ProcessMessage(MFT_MESSAGE_NOTIFY_END_OF_STREAM, 0);
_transform->ProcessMessage(MFT_MESSAGE_NOTIFY_END_STREAMING, 0);
SAFE_RELEASE(_transform);
}
void NotifyError(HRESULT hr) { Error(_grabber->_log, "Source Reader error"); }
private:

View File

@@ -6,7 +6,6 @@ MFThread::MFThread()
: _isBusy(false)
, _semaphore(1)
, _localData(nullptr)
, _localDataSize(0)
, _scalingFactorsCount(0)
, _scalingFactors(nullptr)
, _transform(nullptr)
@@ -28,7 +27,6 @@ MFThread::~MFThread()
{
free(_localData);
_localData = nullptr;
_localDataSize = 0;
}
}
@@ -41,7 +39,7 @@ void MFThread::setup(
_threadIndex = threadIndex;
_lineLength = lineLength;
_pixelFormat = pixelFormat;
_size = size;
_size = (unsigned long) size;
_width = width;
_height = height;
_subsamp = subsamp;
@@ -59,15 +57,10 @@ void MFThread::setup(
_imageResampler.setHorizontalPixelDecimation(_pixelDecimation);
_imageResampler.setVerticalPixelDecimation(_pixelDecimation);
if (size > _localDataSize)
{
if (_localData)
tjFree(_localData);
_localData = (uint8_t*)tjAlloc(size + 1);
_localDataSize = size;
}
if (_localData)
tjFree(_localData);
_localData = (uint8_t*)tjAlloc(size + 1);
memcpy(_localData, sharedData, size);
}
@@ -121,21 +114,13 @@ void MFThread::processImageMjpeg()
if (_flipMode == FlipMode::BOTH || _flipMode == FlipMode::HORIZONTAL)
{
_xform->op = TJXOP_HFLIP;
uint8_t* dstBuf = nullptr;
unsigned long dstSize = 0;
tjTransform(_transform, _localData, _size, 1, &dstBuf, &dstSize, _xform, TJFLAG_FASTDCT | TJFLAG_FASTUPSAMPLE);
_localData = dstBuf;
_size = dstSize;
tjTransform(_transform, _localData, _size, 1, &_localData, &_size, _xform, TJFLAG_FASTDCT | TJFLAG_FASTUPSAMPLE);
}
if (_flipMode == FlipMode::BOTH || _flipMode == FlipMode::VERTICAL)
{
_xform->op = TJXOP_VFLIP;
uint8_t *dstBuf = nullptr;
unsigned long dstSize = 0;
tjTransform(_transform, _localData, _size, 1, &dstBuf, &dstSize, _xform, TJFLAG_FASTDCT | TJFLAG_FASTUPSAMPLE);
_localData = dstBuf;
_size = dstSize;
tjTransform(_transform, _localData, _size, 1, &_localData, &_size, _xform, TJFLAG_FASTDCT | TJFLAG_FASTUPSAMPLE);
}
if (!_decompress)

View File

@@ -5,9 +5,9 @@
// qt
#include <QTimer>
MFWrapper::MFWrapper(const QString &device, unsigned grabWidth, unsigned grabHeight, unsigned fps, unsigned input, int pixelDecimation, QString flipMode)
MFWrapper::MFWrapper(const QString &device, unsigned grabWidth, unsigned grabHeight, unsigned fps, int pixelDecimation, QString flipMode)
: GrabberWrapper("V4L2:"+device, &_grabber, grabWidth, grabHeight, 10)
, _grabber(device, grabWidth, grabHeight, fps, input, pixelDecimation, flipMode)
, _grabber(device, grabWidth, grabHeight, fps, pixelDecimation, flipMode)
{
_ggrabber = &_grabber;
@@ -79,9 +79,9 @@ bool MFWrapper::getCecDetectionEnable() const
return _grabber.getCecDetectionEnabled();
}
void MFWrapper::setDeviceVideoStandard(const QString& device, VideoStandard videoStandard)
bool MFWrapper::setDevice(const QString& device)
{
_grabber.setDeviceVideoStandard(device, VideoStandard::NO_CHANGE);
return _grabber.setDevice(device);
}
void MFWrapper::setFpsSoftwareDecimation(int decimation)
@@ -89,14 +89,14 @@ void MFWrapper::setFpsSoftwareDecimation(int decimation)
_grabber.setFpsSoftwareDecimation(decimation);
}
void MFWrapper::setEncoding(QString enc)
bool MFWrapper::setEncoding(QString enc)
{
_grabber.setEncoding(enc);
return _grabber.setEncoding(enc);
}
void MFWrapper::setBrightnessContrastSaturationHue(int brightness, int contrast, int saturation, int hue)
bool MFWrapper::setBrightnessContrastSaturationHue(int brightness, int contrast, int saturation, int hue)
{
_grabber.setBrightnessContrastSaturationHue(brightness, contrast, saturation, hue);
return _grabber.setBrightnessContrastSaturationHue(brightness, contrast, saturation, hue);
}
void MFWrapper::handleSettingsUpdate(settings::type type, const QJsonDocument& config)
@@ -105,20 +105,23 @@ void MFWrapper::handleSettingsUpdate(settings::type type, const QJsonDocument& c
{
// extract settings
const QJsonObject& obj = config.object();
// reload state
bool reload = false;
// device name, video standard
_grabber.setDeviceVideoStandard(
obj["device"].toString("auto"),
parseVideoStandard(obj["standard"].toString("no-change")));
if (_grabber.setDevice(obj["device"].toString("auto")))
reload = true;
// device input
_grabber.setInput(obj["input"].toInt(-1));
// device resolution
_grabber.setWidthHeight(obj["width"].toInt(0), obj["height"].toInt(0));
if (_grabber.setWidthHeight(obj["width"].toInt(0), obj["height"].toInt(0)))
reload = true;
// device framerate
_grabber.setFramerate(obj["fps"].toInt(15));
if (_grabber.setFramerate(obj["fps"].toInt(15)))
reload = true;
// image size decimation
_grabber.setPixelDecimation(obj["sizeDecimation"].toInt(8));
@@ -134,10 +137,8 @@ void MFWrapper::handleSettingsUpdate(settings::type type, const QJsonDocument& c
obj["cropBottom"].toInt(0));
// Brightness, Contrast, Saturation, Hue
_grabber.setBrightnessContrastSaturationHue(obj["hardware_brightness"].toInt(0),
obj["hardware_contrast"].toInt(0),
obj["hardware_saturation"].toInt(0),
obj["hardware_hue"].toInt(0));
if (_grabber.setBrightnessContrastSaturationHue(obj["hardware_brightness"].toInt(0), obj["hardware_contrast"].toInt(0), obj["hardware_saturation"].toInt(0), obj["hardware_hue"].toInt(0)))
reload = true;
// CEC Standby
_grabber.setCecDetectionEnable(obj["cecDetection"].toBool(true));
@@ -146,6 +147,7 @@ void MFWrapper::handleSettingsUpdate(settings::type type, const QJsonDocument& c
_grabber.setFpsSoftwareDecimation(obj["fpsSoftwareDecimation"].toInt(1));
// Signal detection
_grabber.setSignalDetectionEnable(obj["signalDetection"].toBool(true));
_grabber.setSignalDetectionOffset(
obj["sDHOffsetMin"].toDouble(0.25),
obj["sDVOffsetMin"].toDouble(0.25),
@@ -156,9 +158,13 @@ void MFWrapper::handleSettingsUpdate(settings::type type, const QJsonDocument& c
obj["greenSignalThreshold"].toDouble(0.0)/100.0,
obj["blueSignalThreshold"].toDouble(0.0)/100.0,
obj["noSignalCounterThreshold"].toInt(50) );
_grabber.setSignalDetectionEnable(obj["signalDetection"].toBool(true));
// Hardware encoding format
_grabber.setEncoding(obj["encoding"].toString("NO_CHANGE"));
if (_grabber.setEncoding(obj["encoding"].toString("NO_CHANGE")))
reload = true;
// Reload the Grabber if any settings have been changed that require it
if (reload)
_grabber.reloadGrabber();
}
}