videosurface.cpp 1.4 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162
  1. #include "videosurface.h"
  2. #include <QVideoSurfaceFormat>
  3. #include <QDebug>
  4. const int FILLCOLOR = 0xFF0000;
  5. VideoSurface::VideoSurface(QObject *parent) :
  6. QAbstractVideoSurface(parent)
  7. {
  8. m_lastFrame.fill(FILLCOLOR);
  9. }
  10. VideoSurface::~VideoSurface()
  11. {
  12. stop();
  13. }
  14. QImage VideoSurface::frame() const
  15. {
  16. return m_lastFrame;
  17. }
  18. QList<QVideoFrame::PixelFormat> VideoSurface::supportedPixelFormats(QAbstractVideoBuffer::HandleType handleType) const
  19. {
  20. return QList<QVideoFrame::PixelFormat>() << QVideoFrame::Format_ARGB32;
  21. }
  22. bool VideoSurface::present(const QVideoFrame &frame)
  23. {
  24. if (frame.isValid())
  25. {
  26. QVideoFrame videoFrame(frame);
  27. if( videoFrame.map(QAbstractVideoBuffer::ReadOnly) )
  28. {
  29. m_lastFrame = QImage(videoFrame.width(), videoFrame.height(), QImage::Format_ARGB32);
  30. memcpy(m_lastFrame.bits(), videoFrame.bits(), videoFrame.mappedBytes());
  31. videoFrame.unmap();
  32. emit frameAvailable();
  33. return true;
  34. }
  35. }
  36. return false;
  37. }
  38. bool VideoSurface::start(const QVideoSurfaceFormat &format)
  39. {
  40. if (isActive()) {
  41. stop();
  42. } else if (!format.frameSize().isEmpty()) {
  43. return QAbstractVideoSurface::start(format);
  44. }
  45. return false;
  46. }
  47. void VideoSurface::stop()
  48. {
  49. m_lastFrame.fill(FILLCOLOR);
  50. QAbstractVideoSurface::stop();
  51. }