2011-09-19 03:32:17 -07:00
|
|
|
#include "myvideosurface.h"
|
|
|
|
|
|
|
|
MyVideoSurface::MyVideoSurface(QWidget* widget, VideoIF* target, QObject* parent)
|
|
|
|
: QAbstractVideoSurface(parent)
|
|
|
|
{
|
|
|
|
m_targetWidget = widget;
|
|
|
|
m_target = target;
|
|
|
|
m_imageFormat = QImage::Format_Invalid;
|
2011-09-21 01:15:08 -07:00
|
|
|
|
|
|
|
timer = new QTimer(this);
|
|
|
|
connect(timer, SIGNAL(timeout()), this, SLOT(sendImageToDecode()));
|
|
|
|
timer->start(500);
|
2011-09-19 03:32:17 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
MyVideoSurface::~MyVideoSurface()
|
|
|
|
{
|
|
|
|
}
|
|
|
|
|
|
|
|
bool MyVideoSurface::start(const QVideoSurfaceFormat &format)
|
|
|
|
{
|
|
|
|
m_videoFormat = format;
|
|
|
|
const QImage::Format imageFormat = QVideoFrame::imageFormatFromPixelFormat(format.pixelFormat());
|
|
|
|
const QSize size = format.frameSize();
|
|
|
|
|
|
|
|
if (imageFormat != QImage::Format_Invalid && !size.isEmpty()) {
|
|
|
|
m_imageFormat = imageFormat;
|
|
|
|
QAbstractVideoSurface::start(format);
|
|
|
|
return true;
|
|
|
|
} else {
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
bool MyVideoSurface::present(const QVideoFrame &frame)
|
|
|
|
{
|
|
|
|
m_frame = frame;
|
|
|
|
if (surfaceFormat().pixelFormat() != m_frame.pixelFormat() ||
|
|
|
|
surfaceFormat().frameSize() != m_frame.size()) {
|
|
|
|
stop();
|
|
|
|
return false;
|
|
|
|
} else {
|
|
|
|
m_target->updateVideo();
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
void MyVideoSurface::paint(QPainter *painter)
|
|
|
|
{
|
|
|
|
if (m_frame.map(QAbstractVideoBuffer::ReadOnly)) {
|
|
|
|
QImage image(
|
|
|
|
m_frame.bits(),
|
|
|
|
m_frame.width(),
|
|
|
|
m_frame.height(),
|
|
|
|
m_frame.bytesPerLine(),
|
|
|
|
m_imageFormat);
|
|
|
|
|
|
|
|
QRect r = m_targetWidget->rect();
|
|
|
|
QPoint centerPic((qAbs(r.size().width() - image.size().width())) / 2, (qAbs(
|
|
|
|
r.size().height() - image.size().height())) / 2);
|
|
|
|
|
|
|
|
if (!image.isNull()) {
|
|
|
|
painter->drawImage(centerPic,image);
|
|
|
|
}
|
|
|
|
|
|
|
|
m_frame.unmap();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
QList<QVideoFrame::PixelFormat> MyVideoSurface::supportedPixelFormats(
|
|
|
|
QAbstractVideoBuffer::HandleType handleType) const
|
|
|
|
{
|
|
|
|
if (handleType == QAbstractVideoBuffer::NoHandle) {
|
|
|
|
return QList<QVideoFrame::PixelFormat>()
|
|
|
|
<< QVideoFrame::Format_RGB32
|
|
|
|
<< QVideoFrame::Format_ARGB32
|
|
|
|
<< QVideoFrame::Format_ARGB32_Premultiplied
|
|
|
|
<< QVideoFrame::Format_RGB565
|
|
|
|
<< QVideoFrame::Format_RGB555;
|
|
|
|
} else {
|
|
|
|
return QList<QVideoFrame::PixelFormat>();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2011-09-21 01:15:08 -07:00
|
|
|
void MyVideoSurface::sendImageToDecode()
|
|
|
|
{
|
|
|
|
if (m_frame.map(QAbstractVideoBuffer::ReadOnly)) {
|
|
|
|
QImage image(
|
|
|
|
m_frame.bits(),
|
|
|
|
m_frame.width(),
|
|
|
|
m_frame.height(),
|
|
|
|
m_frame.bytesPerLine(),
|
|
|
|
m_imageFormat);
|
|
|
|
|
|
|
|
if (!image.isNull())
|
|
|
|
emit imageCaptured(image);
|
|
|
|
|
|
|
|
m_frame.unmap();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|