videowidget.cpp 6.45 KB
Newer Older
Edric Milaret's avatar
Edric Milaret committed
1
/***************************************************************************
Edric Milaret's avatar
Edric Milaret committed
2
 * Copyright (C) 2015 by Savoir-faire Linux                                *
Edric Milaret's avatar
Edric Milaret committed
3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25
 * Author: Edric Ladent Milaret <edric.ladent-milaret@savoirfairelinux.com>*
 *                                                                         *
 * This program is free software; you can redistribute it and/or modify    *
 * it under the terms of the GNU General Public License as published by    *
 * the Free Software Foundation; either version 3 of the License, or       *
 * (at your option) any later version.                                     *
 *                                                                         *
 * This program is distributed in the hope that it will be useful,         *
 * but WITHOUT ANY WARRANTY; without even the implied warranty of          *
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the           *
 * GNU General Public License for more details.                            *
 *                                                                         *
 * You should have received a copy of the GNU General Public License       *
 * along with this program.  If not, see <http://www.gnu.org/licenses/>.   *
 **************************************************************************/

#include "videowidget.h"

VideoWidget::VideoWidget(QWidget *parent) :
    QWidget(parent)
  , previewRenderer_(nullptr)
  , renderer_(nullptr)
{
Edric Milaret's avatar
Edric Milaret committed
26
    connect(&Video::PreviewManager::instance(),
Edric Milaret's avatar
Edric Milaret committed
27
            SIGNAL(previewStarted(Video::Renderer*)),
Edric Milaret's avatar
Edric Milaret committed
28
            this, SLOT(previewStarted(Video::Renderer*)));
Edric Milaret's avatar
Edric Milaret committed
29
    connect(&CallModel::instance(),
Edric Milaret's avatar
Edric Milaret committed
30 31 32
            SIGNAL(rendererAdded(Call*,Video::Renderer*)),
            this, SLOT(callInitiated(Call*, Video::Renderer*)),
            Qt::ConnectionType::DirectConnection);
Edric Milaret's avatar
Edric Milaret committed
33 34 35 36 37

    QPalette pal(palette());
    pal.setColor(QPalette::Background, Qt::black);
    this->setAutoFillBackground(true);
    this->setPalette(pal);
Edric Milaret's avatar
Edric Milaret committed
38 39 40 41 42 43 44
}

VideoWidget::~VideoWidget()
{}

void
VideoWidget::previewStarted(Video::Renderer *renderer) {
Eloi Bail's avatar
Eloi Bail committed
45 46 47
    //Enforce that only one videowidget we'll be used at the same time
    if (not isVisible())
        return;
48 49 50 51 52
    previewRenderer_ = renderer;
    connect(previewRenderer_, SIGNAL(frameUpdated()),
            this, SLOT(frameFromPreview()));
    connect(previewRenderer_, SIGNAL(stopped()),
            this, SLOT(previewStopped()));
Edric Milaret's avatar
Edric Milaret committed
53 54 55
}

void
Edric Milaret's avatar
Edric Milaret committed
56 57
VideoWidget::previewStopped() {
    disconnect(previewRenderer_, SIGNAL(frameUpdated()),
58
               this, SLOT(frameFromPreview()));
Edric Milaret's avatar
Edric Milaret committed
59
    disconnect(previewRenderer_, SIGNAL(stopped()),
60
               this, SLOT(renderingStopped()));
Edric Milaret's avatar
Edric Milaret committed
61 62 63 64 65
    previewRenderer_ = nullptr;
}

void
VideoWidget::frameFromPreview() {
Eloi Bail's avatar
Eloi Bail committed
66
    if (previewRenderer_ && previewRenderer_->isRendering()) {
67 68 69 70 71
        {
            QMutexLocker lock(&mutex_);
            auto tmp  = previewRenderer_->currentFrame();
            if (tmp.storage.size())
                currentPreviewFrame_ = tmp;
Eloi Bail's avatar
Eloi Bail committed
72
        }
73
        update();
Edric Milaret's avatar
Edric Milaret committed
74 75 76 77
    }
}

void
78
VideoWidget::paintEvent(QPaintEvent *evt) {
Edric Milaret's avatar
Edric Milaret committed
79 80
    Q_UNUSED(evt)
    QPainter painter(this);
81 82 83 84

    if (renderer_) {
        {
            QMutexLocker lock(&mutex_);
85 86 87
            if (currentDistantFrame_.storage.size() != 0
                && currentDistantFrame_.storage.size() ==
                    (renderer_->size().height()*renderer_->size().width()*4)) {
88 89 90 91 92 93 94 95 96 97 98 99 100
                frameDistant_ = std::move(currentDistantFrame_.storage);
                distantImage_.reset(new QImage((uchar*)frameDistant_.data(),
                                               renderer_->size().width(),
                                               renderer_->size().height(),
                                               QImage::Format_ARGB32_Premultiplied));
            }
        }
        if (distantImage_) {
            auto scaledDistant = distantImage_->scaled(size(), Qt::KeepAspectRatio);
            auto xDiff = (width() - scaledDistant.width()) / 2;
            auto yDiff = (height() - scaledDistant.height()) /2;
            painter.drawImage(QRect(xDiff,yDiff,scaledDistant.width(),scaledDistant.height()), scaledDistant);
        }
Edric Milaret's avatar
Edric Milaret committed
101
    }
102 103 104
    if (previewRenderer_) {
        {
            QMutexLocker lock(&mutex_);
105 106 107
            if (currentPreviewFrame_.storage.size() != 0
                 && currentPreviewFrame_.storage.size() ==
                    (previewRenderer_->size().height()*previewRenderer_->size().width()*4)) {
108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126
                framePreview_ = std::move(currentPreviewFrame_.storage);
                previewImage_.reset(new QImage((uchar*)framePreview_.data(),
                                               previewRenderer_->size().width(),
                                               previewRenderer_->size().height(),
                                               QImage::Format_ARGB32_Premultiplied));

            }
        }
        if (previewImage_) {
            auto previewHeight = !renderer_ ? height() : height()/4;
            auto previewWidth = !renderer_  ? width() : width()/4;
            auto scaledPreview = previewImage_->scaled(previewWidth, previewHeight, Qt::KeepAspectRatio);
            auto xDiff = (previewWidth - scaledPreview.width()) / 2;
            auto yDiff = (previewHeight - scaledPreview.height()) / 2;
            auto yPos = !renderer_ ? yDiff : height() - previewHeight - previewMargin_;
            auto xPos = !renderer_ ? xDiff : width() - scaledPreview.width() - previewMargin_;
            painter.drawImage(QRect(xPos,yPos,scaledPreview.width(),scaledPreview.height()),
                              scaledPreview);
        }
Edric Milaret's avatar
Edric Milaret committed
127 128 129 130 131 132 133
    }
    painter.end();
}

void
VideoWidget::callInitiated(Call* call, Video::Renderer *renderer) {
    Q_UNUSED(call)
Eloi Bail's avatar
Eloi Bail committed
134 135 136
    //Enforce that only one videowidget we'll be used at the same time
    if (not isVisible())
        return;
137 138 139 140
    renderer_ = renderer;
    connect(renderer_, SIGNAL(frameUpdated()), this, SLOT(frameFromDistant()));
    connect(renderer_, SIGNAL(stopped()),this, SLOT(renderingStopped()),
            Qt::ConnectionType::DirectConnection);
Edric Milaret's avatar
Edric Milaret committed
141 142 143 144
}

void
VideoWidget::frameFromDistant() {
Eloi Bail's avatar
Eloi Bail committed
145
    if (renderer_ && renderer_->isRendering()) {
146 147 148 149 150 151
        {
            QMutexLocker lock(&mutex_);
            auto tmp  = renderer_->currentFrame();
            if (tmp.storage.size())
                currentDistantFrame_ = tmp;

Eloi Bail's avatar
Eloi Bail committed
152
        }
153
        update();
Edric Milaret's avatar
Edric Milaret committed
154 155 156 157 158
    }
}

void
VideoWidget::renderingStopped() {
Edric Milaret's avatar
Edric Milaret committed
159 160
    disconnect(renderer_, SIGNAL(frameUpdated()), this, SLOT(frameFromDistant()));
    disconnect(renderer_, SIGNAL(stopped()),this, SLOT(renderingStopped()));
Edric Milaret's avatar
Edric Milaret committed
161 162
    renderer_ = nullptr;
}