newvideo.h 4.47 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 31 32 33 34 35
 *   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/>.  *
 ***************************************************************************/
#pragma once

// Std
#include <map>
#include <memory>
#include <string>
#include <vector>

// Qt
#include <qobject.h>

// Lrc
#include "typedefs.h"

// Qt
#include <QObject>
#include <QThread>

Kateryna Kostiuk's avatar
Kateryna Kostiuk committed
36 37
struct AVFrame;

Sébastien Blin's avatar
Sébastien Blin committed
38 39 40 41 42 43 44 45 46 47 48 49 50 51 52
namespace lrc
{

class RendererPimpl;

namespace api
{

namespace video
{

constexpr static const char PREVIEW_RENDERER_ID[] = "local";

using Channel = std::string;
using Resolution = std::string;
53
using Framerate = float;
Sébastien Blin's avatar
Sébastien Blin committed
54
using FrameratesList = std::vector<Framerate>;
55 56
using ResRateList = std::vector<std::pair<Resolution, FrameratesList>>;
using Capabilities = std::map<Channel, ResRateList>;
Sébastien Blin's avatar
Sébastien Blin committed
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 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97

/**
 * This class is used by Renderer class to expose video data frame
 * that could be owned by instances of this class or shared.
 * If an instance carries data, "storage.size()" is greater than 0
 * and equals to "size", "ptr" is equals to "storage.data()".
 * If shared data is carried, only "ptr" and "size" are set.
 */
struct Frame {
   uint8_t*             ptr     { nullptr };
   std::size_t          size    { 0       };
   std::vector<uint8_t> storage {         };
   // Next variables are currently used with DirectRenderer only
   unsigned int         height  { 0       };
   unsigned int         width   { 0       };
};

enum DeviceType
{
    CAMERA,
    DISPLAY,
    FILE,
    INVALID
};

/**
 * This class describes the current rendered device
 */
struct RenderedDevice
{
    std::string name;
    DeviceType type = INVALID;
};

/**
 * This class describes current video settings
 */
struct Settings
{
    Channel channel = "";
    std::string name = "";
98
    std::string id = "";
Sébastien Blin's avatar
Sébastien Blin committed
99 100 101 102 103 104 105 106
    Framerate rate = 0;
    Resolution size = "";
};

class LIB_EXPORT Renderer : public QObject {
    Q_OBJECT
public:
    Renderer(const std::string& id, Settings videoSettings,
Kateryna Kostiuk's avatar
Kateryna Kostiuk committed
107
        const std::string& shmPath = "", const bool useAVFrame = false);
Sébastien Blin's avatar
Sébastien Blin committed
108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137
    ~Renderer();

    /**
     * Start the renderer in its own thread
     */
    void initThread();
    /**
     * Update size and shmPath of a renderer
     * @param res new resolution "wxh"
     * @param shmPath new shmPath
     */
    void update(const std::string& res, const std::string& shmPath);
    /**
     * Stop renderer and thread
     */
    void quit();

    // Getters
    /**
     * @return if renderer is rendering
     */
    bool isRendering() const;
    /**
     * @return renderer's id
     */
    std::string getId() const;
    /**
     * @return current rendered frame
     */
    Frame currentFrame() const;
138

139
#if defined(ENABLE_LIBWRAP)
Kateryna Kostiuk's avatar
Kateryna Kostiuk committed
140 141 142 143
    /**
     * @return current avframe
     */
    std::unique_ptr<AVFrame, void(*)(AVFrame*)> currentAVFrame() const;
144 145
#endif

Sébastien Blin's avatar
Sébastien Blin committed
146 147 148 149 150 151 152 153 154 155 156 157 158 159
    /**
     * @return current size
     */
    QSize size() const; // TODO convert into std format!

    // Utils
    /**
     * Start rendering
     */
    void startRendering();
    /**
     * Stop rendering
     */
    void stopRendering();
Kateryna Kostiuk's avatar
Kateryna Kostiuk committed
160 161 162 163
    /**
     * set to true to receive AVFrames from render
     */
    void useAVFrame(bool useAVFrame);
Sébastien Blin's avatar
Sébastien Blin committed
164 165 166 167 168 169 170 171 172 173 174 175 176 177 178

Q_SIGNALS:
    /**
     * Emitted when a new frame is ready
     * @param id
     */
    void frameUpdated(const std::string& id);

private:
    std::unique_ptr<RendererPimpl> pimpl_;
};

} // namespace video
} // namespace api
} // namespace lrc