Skip to content
Snippets Groups Projects
Commit e8b69145 authored by Andreas Traczyk's avatar Andreas Traczyk
Browse files

videoprovider: expose the renderer's `active` value QML

Simplifies the property `activeRenderers`.

Gitlab: #1161
Change-Id: I4d39aaf3b96bba774c2492551f0426ea42446690
parent da5d0412
No related branches found
No related tags found
No related merge requests found
...@@ -55,7 +55,7 @@ Item { ...@@ -55,7 +55,7 @@ Item {
antialiasing: true antialiasing: true
anchors.fill: parent anchors.fill: parent
opacity: videoProvider.renderers[rendererId] !== undefined opacity: videoProvider.activeRenderers[rendererId] === true
visible: opacity visible: opacity
fillMode: crop ? VideoOutput.PreserveAspectCrop : VideoOutput.PreserveAspectFit fillMode: crop ? VideoOutput.PreserveAspectCrop : VideoOutput.PreserveAspectFit
......
...@@ -79,14 +79,14 @@ VideoProvider::subscribe(QObject* obj, const QString& id) ...@@ -79,14 +79,14 @@ VideoProvider::subscribe(QObject* obj, const QString& id)
static_cast<Qt::ConnectionType>(Qt::DirectConnection | Qt::UniqueConnection)); static_cast<Qt::ConnectionType>(Qt::DirectConnection | Qt::UniqueConnection));
QWriteLocker lock(&renderersMutex_); QWriteLocker lock(&renderersMutex_);
FrameObject* fo = nullptr;
// Check if we already have a FrameObject for this id. // Check if we already have a FrameObject for this id.
auto it = renderers_.find(id); auto it = renderers_.find(id);
if (it == renderers_.end()) { if (it == renderers_.end()) {
qDebug() << "Creating new FrameObject for id:" << id; qDebug() << "Creating new FrameObject for id:" << id;
FrameObject& object = renderers_[id]; fo = &renderers_[id];
object.active = false;
it = renderers_.find(id);
} else { } else {
fo = &it->second;
// Make sure it's not already subscribed to this QVideoSink. // Make sure it's not already subscribed to this QVideoSink.
if (it->second.subscribers.contains(sink)) { if (it->second.subscribers.contains(sink)) {
qWarning() << Q_FUNC_INFO << "QVideoSink already subscribed to id:" << id; qWarning() << Q_FUNC_INFO << "QVideoSink already subscribed to id:" << id;
...@@ -94,7 +94,7 @@ VideoProvider::subscribe(QObject* obj, const QString& id) ...@@ -94,7 +94,7 @@ VideoProvider::subscribe(QObject* obj, const QString& id)
} }
} }
it->second.subscribers.insert(sink); fo->subscribers.insert(sink);
qDebug().noquote() << QString("Added sink: 0x%1 to subscribers for id: %2") qDebug().noquote() << QString("Added sink: 0x%1 to subscribers for id: %2")
.arg((quintptr) obj, QT_POINTER_SIZE, 16, QChar('0')) .arg((quintptr) obj, QT_POINTER_SIZE, 16, QChar('0'))
.arg(id); .arg(id);
...@@ -154,6 +154,7 @@ VideoProvider::onRendererStarted(const QString& id, const QSize& size) ...@@ -154,6 +154,7 @@ VideoProvider::onRendererStarted(const QString& id, const QSize& size)
auto frameFormat = QVideoFrameFormat(size, pixelFormat); auto frameFormat = QVideoFrameFormat(size, pixelFormat);
renderersMutex_.lockForWrite(); renderersMutex_.lockForWrite();
auto it = renderers_.find(id); auto it = renderers_.find(id);
if (it == renderers_.end()) { if (it == renderers_.end()) {
qDebug() << "Create new QVideoFrame" << frameFormat.frameSize(); qDebug() << "Create new QVideoFrame" << frameFormat.frameSize();
...@@ -165,9 +166,9 @@ VideoProvider::onRendererStarted(const QString& id, const QSize& size) ...@@ -165,9 +166,9 @@ VideoProvider::onRendererStarted(const QString& id, const QSize& size)
it->second.active = true; it->second.active = true;
qDebug() << "QVideoFrame reset to" << frameFormat.frameSize(); qDebug() << "QVideoFrame reset to" << frameFormat.frameSize();
} }
renderersMutex_.unlock();
Q_EMIT renderersChanged(); renderersMutex_.unlock();
Q_EMIT activeRenderersChanged();
} }
void void
...@@ -279,21 +280,21 @@ VideoProvider::onRendererStopped(const QString& id) ...@@ -279,21 +280,21 @@ VideoProvider::onRendererStopped(const QString& id)
qWarning() << Q_FUNC_INFO << "Can't find renderer for id:" << id; qWarning() << Q_FUNC_INFO << "Can't find renderer for id:" << id;
return; return;
} }
if (it->second.subscribers.isEmpty()) { if (it->second.subscribers.isEmpty()) {
renderers_.erase(id); renderers_.erase(id);
renderersMutex_.unlock(); renderersMutex_.unlock();
Q_EMIT renderersChanged(); Q_EMIT activeRenderersChanged();
return; return;
} }
it->second.frameMutex.lockForWrite(); it->second.frameMutex.lockForWrite();
it->second.videoFrame = QVideoFrame(); it->second.videoFrame = QVideoFrame();
it->second.active = false; it->second.active = true;
it->second.frameMutex.unlock(); it->second.frameMutex.unlock();
renderersMutex_.unlock(); renderersMutex_.unlock();
Q_EMIT activeRenderersChanged();
Q_EMIT renderersChanged();
} }
void void
...@@ -331,17 +332,12 @@ VideoProvider::copyUnaligned(QVideoFrame& dst, const video::Frame& src) ...@@ -331,17 +332,12 @@ VideoProvider::copyUnaligned(QVideoFrame& dst, const video::Frame& src)
} }
QVariantMap QVariantMap
VideoProvider::getRenderers() VideoProvider::getActiveRenderers()
{ {
QVariantMap map; QVariantMap activeRenderers;
renderersMutex_.lockForRead(); QReadLocker lk(&renderersMutex_);
for (auto& r : renderers_) { for (auto& r : renderers_) {
if (r.second.active) { activeRenderers[r.first] = r.second.active;
r.second.frameMutex.lockForRead();
map[r.first] = r.second.videoFrame.size();
r.second.frameMutex.unlock();
} }
} return activeRenderers;
renderersMutex_.unlock();
return map;
} }
...@@ -40,6 +40,7 @@ class VideoProvider final : public QObject ...@@ -40,6 +40,7 @@ class VideoProvider final : public QObject
{ {
Q_OBJECT Q_OBJECT
QML_ELEMENT QML_ELEMENT
Q_PROPERTY(QVariantMap activeRenderers READ getActiveRenderers NOTIFY activeRenderersChanged)
public: public:
explicit VideoProvider(AVModel& avModel, QObject* parent = nullptr); explicit VideoProvider(AVModel& avModel, QObject* parent = nullptr);
~VideoProvider() = default; ~VideoProvider() = default;
...@@ -49,9 +50,8 @@ public: ...@@ -49,9 +50,8 @@ public:
Q_INVOKABLE QString captureVideoFrame(const QString& id); Q_INVOKABLE QString captureVideoFrame(const QString& id);
Q_INVOKABLE QImage captureRawVideoFrame(const QString& id); Q_INVOKABLE QImage captureRawVideoFrame(const QString& id);
Q_PROPERTY(QVariantMap renderers READ getRenderers NOTIFY renderersChanged) QVariantMap getActiveRenderers();
QVariantMap getRenderers(); Q_SIGNAL void activeRenderersChanged();
Q_SIGNAL void renderersChanged();
private Q_SLOTS: private Q_SLOTS:
void onRendererStarted(const QString& id, const QSize& size); void onRendererStarted(const QString& id, const QSize& size);
...@@ -69,7 +69,7 @@ private: ...@@ -69,7 +69,7 @@ private:
QReadWriteLock frameMutex; QReadWriteLock frameMutex;
QSet<QVideoSink*> subscribers; QSet<QVideoSink*> subscribers;
QReadWriteLock subscribersMutex; QReadWriteLock subscribersMutex;
std::atomic_bool active; bool active;
}; };
std::map<QString, FrameObject> renderers_; std::map<QString, FrameObject> renderers_;
QReadWriteLock renderersMutex_; QReadWriteLock renderersMutex_;
......
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Please register or to comment