newvideo.cpp 6.35 KB
Newer Older
Sébastien Blin's avatar
Sébastien Blin committed
1
/****************************************************************************
Sébastien Blin's avatar
Sébastien Blin committed
2
 *    Copyright (C) 2018-2019 Savoir-faire Linux Inc.                                  *
Sébastien Blin's avatar
Sébastien Blin committed
3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30
 *   Author: Sébastien Blin <sebastien.blin@savoirfairelinux.com>           *
 *                                                                          *
 *   This library is free software; you can redistribute it and/or          *
 *   modify it under the terms of the GNU Lesser General Public             *
 *   License as published by the Free Software Foundation; either           *
 *   version 2.1 of the License, or (at your option) any later version.     *
 *                                                                          *
 *   This library 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      *
 *   Lesser 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/>.  *
 ***************************************************************************/

// LRC
#include "api/newvideo.h"
#include "dbus/videomanager.h"
#ifdef ENABLE_LIBWRAP
 #include "directrenderer.h"
#else
 #include "shmrenderer.h"
#endif

// std
#include <mutex>

31 32 33
// Qt
#include <QSize>

Sébastien Blin's avatar
Sébastien Blin committed
34 35 36 37 38 39 40 41 42
namespace lrc
{

using namespace api::video;

class RendererPimpl: public QObject
{
public:
    RendererPimpl(Renderer& linked, const std::string& id,
43
        Settings videoSettings, const std::string& shmPath,
Kateryna Kostiuk's avatar
Kateryna Kostiuk committed
44
        const bool useAVFrame);
Sébastien Blin's avatar
Sébastien Blin committed
45 46 47 48 49 50 51
    ~RendererPimpl();

    Renderer& linked;

    std::string id_;
    Settings videoSettings_;
    QThread thread_;
Kateryna Kostiuk's avatar
Kateryna Kostiuk committed
52
    bool usingAVFrame_;
Sébastien Blin's avatar
Sébastien Blin committed
53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82

    /**
     * Convert a string (wxh) to a QSize
     * @param res the string to convert
     * @return the QSize object
     */
    static QSize stringToQSize(const std::string& res);

    std::mutex rendering_mtx_;

#ifdef ENABLE_LIBWRAP
     std::unique_ptr<Video::DirectRenderer> renderer;
#else
     std::unique_ptr<Video::ShmRenderer> renderer;
#endif

public Q_SLOTS:
    /**
     * Detect when the current frame is updated
     */
    void slotFrameUpdated();
};

namespace api
{

namespace video
{

Renderer::Renderer(const std::string& id, Settings videoSettings,
Kateryna Kostiuk's avatar
Kateryna Kostiuk committed
83 84
    const std::string& shmPath, const bool useAVFrame)
: pimpl_(std::make_unique<RendererPimpl>(*this, id, videoSettings, shmPath, useAVFrame))
Sébastien Blin's avatar
Sébastien Blin committed
85 86 87 88 89 90 91 92 93 94 95 96 97
{}

Renderer::~Renderer()
{
    stopRendering();
}

void
Renderer::initThread()
{
    if (!pimpl_->renderer)
        return;
#ifdef ENABLE_LIBWRAP
Kateryna Kostiuk's avatar
Kateryna Kostiuk committed
98 99 100 101 102
    if(pimpl_->usingAVFrame_) {
        VideoManager::instance().registerAVSinkTarget(pimpl_->id_.c_str(), pimpl_->renderer->avTarget());
    } else {
        VideoManager::instance().registerSinkTarget(pimpl_->id_.c_str(), pimpl_->renderer->target());
    }
Sébastien Blin's avatar
Sébastien Blin committed
103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118
#endif
    if (!pimpl_->thread_.isRunning())
       pimpl_->thread_.start();
}

void
Renderer::update(const std::string& res, const std::string& shmPath)
{
    if (!pimpl_->thread_.isRunning())
       pimpl_->thread_.start();

    // res = "WIDTHxHEIGHT"
    QSize size = RendererPimpl::stringToQSize(res);
    pimpl_->renderer->setSize(size);

#ifdef ENABLE_LIBWRAP
Kateryna Kostiuk's avatar
Kateryna Kostiuk committed
119 120 121 122 123
    if(pimpl_->usingAVFrame_) {
        VideoManager::instance().registerAVSinkTarget(pimpl_->id_.c_str(), pimpl_->renderer->avTarget());
    } else {
        VideoManager::instance().registerSinkTarget(pimpl_->id_.c_str(), pimpl_->renderer->target());
    }
Sébastien Blin's avatar
Sébastien Blin committed
124 125 126 127 128 129 130 131 132 133 134 135 136 137
#else //ENABLE_LIBWRAP
    pimpl_->renderer->setShmPath(shmPath.c_str());
#endif
}

bool
Renderer::isRendering() const
{
    std::lock_guard<std::mutex> lk(pimpl_->rendering_mtx_);
    if (pimpl_->renderer)
        return pimpl_->renderer->isRendering();
    return false;
}

Kateryna Kostiuk's avatar
Kateryna Kostiuk committed
138 139 140
void
Renderer::useAVFrame(bool useAVFrame) {
    pimpl_->usingAVFrame_ = useAVFrame;
141 142
#ifdef ENABLE_LIBWRAP
    pimpl_->renderer->configureTarget(useAVFrame);
Sébastien Blin's avatar
Sébastien Blin committed
143
#endif
Kateryna Kostiuk's avatar
Kateryna Kostiuk committed
144 145
}

Sébastien Blin's avatar
Sébastien Blin committed
146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165
std::string
Renderer::getId() const
{
    return pimpl_->id_;
}

Frame
Renderer::currentFrame() const
{
    // TODO(sblin) remove Video::Frame when deleting old models.
    auto frame = pimpl_->renderer->currentFrame();
    Frame result;
    result.ptr = frame.ptr;
    result.size = frame.size;
    result.storage = frame.storage;
    result.height = frame.height;
    result.width = frame.width;
    return result;
}

166
#if defined(ENABLE_LIBWRAP)
Kateryna Kostiuk's avatar
Kateryna Kostiuk committed
167 168 169 170 171
std::unique_ptr<AVFrame, void(*)(AVFrame*)>
Renderer::currentAVFrame() const
{
    return pimpl_->renderer->currentAVFrame();
}
172
#endif
Kateryna Kostiuk's avatar
Kateryna Kostiuk committed
173

Sébastien Blin's avatar
Sébastien Blin committed
174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211
QSize
Renderer::size() const
{
    if (pimpl_->renderer)
        return pimpl_->renderer->size();
    return QSize();
}

void
Renderer::quit()
{
    pimpl_->thread_.quit();
    pimpl_->thread_.wait();
}

void
Renderer::startRendering()
{
    if (pimpl_->renderer) {
        std::lock_guard<std::mutex> lk(pimpl_->rendering_mtx_);
        if (pimpl_->renderer->isRendering())
            return;
        pimpl_->renderer->startRendering();
    }
}

void
Renderer::stopRendering()
{
    if (pimpl_->renderer) {
        std::lock_guard<std::mutex> lk(pimpl_->rendering_mtx_);
        pimpl_->renderer->stopRendering();
    }
}

}} // end of api::video

RendererPimpl::RendererPimpl(Renderer& linked, const std::string& id,
212
    Settings videoSettings, const std::string& shmPath, bool useAVFrame)
Sébastien Blin's avatar
Sébastien Blin committed
213
: linked(linked)
214 215 216
, id_(id)
, videoSettings_(videoSettings)
, usingAVFrame_(useAVFrame)
Sébastien Blin's avatar
Sébastien Blin committed
217 218 219
{
    QSize size = stringToQSize(videoSettings.size);
#ifdef ENABLE_LIBWRAP
Kateryna Kostiuk's avatar
Kateryna Kostiuk committed
220
    renderer = std::make_unique<Video::DirectRenderer>(id.c_str(), size, usingAVFrame_);
Sébastien Blin's avatar
Sébastien Blin committed
221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254
#else  // ENABLE_LIBWRAP
    renderer = std::make_unique<Video::ShmRenderer>(id.c_str(), shmPath.c_str(), size);
#endif
    renderer->moveToThread(&thread_);

    connect(&*renderer, &Video::Renderer::frameUpdated,
        this, &RendererPimpl::slotFrameUpdated);
}

RendererPimpl::~RendererPimpl()
{

}

QSize
RendererPimpl::stringToQSize(const std::string& res)
{
    QString sizeStr = res.c_str();
    auto sizeSplited = sizeStr.split('x');
    if (sizeSplited.size() != 2) return {};
    auto width = sizeSplited.at(0).toInt();
    auto height = sizeSplited.at(1).toInt();
    return QSize(width, height);
}

void
RendererPimpl::slotFrameUpdated()
{
    emit linked.frameUpdated(id_);
}

} // end of namespace lrc

#include "api/moc_newvideo.cpp"