YARP
Yet Another Robot Platform
 
Loading...
Searching...
No Matches
videoproducer.cpp
Go to the documentation of this file.
1/*
2 * SPDX-FileCopyrightText: 2006-2021 Istituto Italiano di Tecnologia (IIT)
3 * SPDX-License-Identifier: LGPL-2.1-or-later
4 */
5
6#include "videoproducer.h"
7#include <QVideoFrame>
8
10 QObject(parent)
11{
12 m_format = nullptr;
13 m_surface = nullptr;
14 m_frame = nullptr;
15}
16
18{
19 if(m_format)
20 {
21 delete m_format;
22 m_format = nullptr;
23 }
24}
25
26
31QAbstractVideoSurface* VideoProducer::videoSurface() const
32{
33 return m_surface;
34}
35
40void VideoProducer::setVideoSurface(QAbstractVideoSurface *surface)
41{
42 if (m_surface != surface && m_surface && m_surface->isActive()) {
43 m_surface->stop();
44 }
45 m_surface = surface;
46
47}
48
53{
54 if(!m_format){
55 return 0;
56 }
57 int w = m_format->frameWidth();
58 return w;
59}
60
65{
66 if(!m_format){
67 return 0;
68 }
69 int h = m_format->frameHeight();
70 return h;
71}
72
77{
78 if (m_surface){
79 if(m_surface->isActive() && (m_format->frameWidth() != frame->size().width() ||
80 m_format->frameHeight() != frame->size().height()))
81 {
82 m_surface->stop();
83 delete m_format;
84 m_format = nullptr;
85 }
86
87 if (!m_surface->isActive())
88 {
89 QSize s = frame->size();
90 QVideoFrame::PixelFormat f = frame->pixelFormat();
91 m_format = new QVideoSurfaceFormat(s, f);
92
93 bool b = m_surface->start(*m_format);
94 if (b)
95 {
96 qDebug("Surface STARTED! Dimensions: %dx%d -- PixelFormat: %d", s.width(), s.height(), (int)f);
97 }
98 else
99 {
100 qDebug("Surface START ERROR");
101 delete m_format;
102 }
103 emit resizeWindowRequest();
104 }
105 }
106
107 if (m_surface && m_format){
108 bool b = m_surface->present(*frame);
109 if (!b) {
110 qWarning("Surface PRESENT ERROR");
111 }
112 else{
113 mutex.lock();
114 if(m_frame != nullptr){
115 delete m_frame;
116 }
117 m_frame = new QVideoFrame(*frame);
118 mutex.unlock();
119 }
120 }
121
122}
123
129QString VideoProducer::getPixelAsStr(int x, int y){
130 mutex.lock();
131 if (m_frame == nullptr || x>=m_frame->size().width() || y>=m_frame->size().height() || x<0 || y<0){
132 mutex.unlock();
133 return QString("Invalid");
134 }
135 m_frame->map( QAbstractVideoBuffer::ReadOnly );
136 QImage image(m_frame->bits(), m_frame->width(),
137 m_frame->height(), m_frame->bytesPerLine(),
138 QImage::Format_RGB32);
139 mutex.unlock();
140 QRgb pixelVal = image.pixel(x,y);
141 QString rgbHex("#");
142 rgbHex += QString::number(pixelVal,16);
143
144 return rgbHex;
145}
void setVideoSurface(QAbstractVideoSurface *surface)
sets the abstract surface
void resizeWindowRequest()
QString getPixelAsStr(int x, int y)
Pics the rgb value of the pixel specified by x and y and return it as a string.
QAbstractVideoSurface * videoSurface
int getHeight()
returns the height of the surface
int getWidth()
returns the width of the surface
void onNewVideoContentReceived(QVideoFrame *frame)
This gets the frame and presents it to the abstract surface.
VideoProducer(QObject *parent=0)