video_mixer.cpp 3.91 KB
Newer Older
1 2 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
/*
 *  Copyright (C) 2013 Savoir-Faire Linux Inc.
 *
 *  Author: Guillaume Roguez <Guillaume.Roguez@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, write to the Free Software
 *  Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301 USA.
 *
 *  Additional permission under GNU GPL version 3 section 7:
 *
 *  If you modify this program, or any covered work, by linking or
 *  combining it with the OpenSSL project's OpenSSL library (or a
 *  modified version of that library), containing parts covered by the
 *  terms of the OpenSSL or SSLeay licenses, Savoir-Faire Linux Inc.
 *  grants you additional permission to convey the resulting work.
 *  Corresponding Source for a non-source form of such a combination
 *  shall include the source code for the parts of OpenSSL used as well
 *  as that of the covered work.
 */

32
#include "libav_deps.h"
33 34 35
#include "video_mixer.h"
#include "check.h"

36 37
#include <cmath>

38 39 40
namespace sfl_video {

VideoMixer::VideoMixer() :
41 42 43 44 45
    VideoGenerator::VideoGenerator()
    , sourceScaler_()
    , scaledFrame_()
    , width_(0)
    , height_(0)
46 47
    , renderMutex_()
    , renderCv_()
48
{ start(); }
49 50 51

VideoMixer::~VideoMixer()
{
52 53
    stop();
    join();
54 55
}

56
void VideoMixer::process()
57
{
58 59
    waitForUpdate();
    rendering();
60 61
}

62
void VideoMixer::waitForUpdate()
63
{
64 65
    std::unique_lock<std::mutex> lk(renderMutex_);
    renderCv_.wait(lk);
66 67
}

68 69
void VideoMixer::update(Observable<VideoFrameSP>* ob, VideoFrameSP& frame_p)
{ renderCv_.notify_one(); }
70

71
void VideoMixer::rendering()
72
{
73 74 75
    if (!width_ or !height_)
        return;

76
#if 0
77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92
    // For all sources:
    //   - take source frame
    //   - scale it down and layout it
    //   - publish the result frame
    // Current layout is a squared distribution

    const int n=sourceList_.size();
    const int zoom=ceil(sqrt(n));
    const int cell_width=width_ / zoom;
    const int cell_height=height_ / zoom;

    VideoFrame &output = getNewFrame();

    // Blit frame function support only YUV420P pixel format
    if (!output.allocBuffer(width_, height_, VIDEO_PIXFMT_YUV420P))
        WARN("VideoFrame::allocBuffer() failed");
Guillaume Roguez's avatar
Guillaume Roguez committed
93

94 95 96 97 98 99
    if (!scaledFrame_.allocBuffer(cell_width, cell_height, VIDEO_PIXFMT_YUV420P))
        WARN("VideoFrame::allocBuffer() failed");

    int lastInputWidth=0;
    int lastInputHeight=0;
    int i=0;
100
    for (VideoNode* src : sourceList_) {
101 102 103
        int xoff = (i % zoom) * cell_width;
        int yoff = (i / zoom) * cell_height;

104
        VideoFrameSP input=src->obtainLastFrame();
105 106 107 108 109 110 111 112 113 114 115 116 117 118 119
        if (input) {
            // scaling context allocation may be time consuming
            // so reset it only if needed
            if (input->getWidth() != lastInputWidth ||
                input->getHeight() != lastInputHeight)
                sourceScaler_.reset();

            sourceScaler_.scale(*input, scaledFrame_);
            output.blit(scaledFrame_, xoff, yoff);

            lastInputWidth = input->getWidth();
            lastInputHeight = input->getHeight();
        }

        i++;
120
    }
121
    publishFrame();
122
#endif
123 124
}

125 126 127 128 129 130
void VideoMixer::setDimensions(int width, int height)
{
    // FIXME: unprotected write (see rendering())
    width_ = width;
    height_ = height;
}
131

132 133
int VideoMixer::getWidth() const { return width_; }
int VideoMixer::getHeight() const { return height_; }
134
int VideoMixer::getPixelFormat() const { return VIDEO_PIXFMT_YUV420P; }
135

136
} // end namespace sfl_video