2015-06-06 09:40:08 +08:00
|
|
|
/*
|
2016-11-28 21:33:38 +08:00
|
|
|
Copyright © 2015 by The qTox Project Contributors
|
2015-06-06 09:40:08 +08:00
|
|
|
|
|
|
|
This file is part of qTox, a Qt-based graphical interface for Tox.
|
|
|
|
|
|
|
|
qTox is libre 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.
|
|
|
|
|
|
|
|
qTox 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 qTox. If not, see <http://www.gnu.org/licenses/>.
|
|
|
|
*/
|
|
|
|
|
2015-05-14 10:46:28 +08:00
|
|
|
extern "C" {
|
|
|
|
#include <libavcodec/avcodec.h>
|
2016-04-14 05:33:44 +08:00
|
|
|
#include <libavutil/imgutils.h>
|
2015-05-14 10:46:28 +08:00
|
|
|
}
|
2016-06-16 17:23:01 +08:00
|
|
|
|
2015-05-14 10:46:28 +08:00
|
|
|
#include "corevideosource.h"
|
|
|
|
#include "videoframe.h"
|
|
|
|
|
2016-07-27 06:18:57 +08:00
|
|
|
/**
|
2016-08-01 16:22:17 +08:00
|
|
|
* @class CoreVideoSource
|
|
|
|
* @brief A VideoSource that emits frames received by Core.
|
|
|
|
*/
|
2016-07-27 06:18:57 +08:00
|
|
|
|
|
|
|
/**
|
2016-08-01 16:22:17 +08:00
|
|
|
* @var std::atomic_int subscribers
|
|
|
|
* @brief Number of suscribers
|
|
|
|
*
|
|
|
|
* @var std::atomic_bool deleteOnClose
|
|
|
|
* @brief If true, self-delete after the last suscriber is gone
|
|
|
|
*/
|
2016-07-27 06:18:57 +08:00
|
|
|
|
|
|
|
/**
|
2016-08-01 16:22:17 +08:00
|
|
|
* @brief CoreVideoSource constructor.
|
|
|
|
* @note Only CoreAV should create a CoreVideoSource since
|
|
|
|
* only CoreAV can push images to it.
|
|
|
|
*/
|
2015-05-14 10:46:28 +08:00
|
|
|
CoreVideoSource::CoreVideoSource()
|
2017-02-26 19:52:45 +08:00
|
|
|
: subscribers{0}
|
|
|
|
, deleteOnClose{false}
|
|
|
|
, stopped{false}
|
2015-05-14 10:46:28 +08:00
|
|
|
{
|
|
|
|
}
|
|
|
|
|
2016-07-27 06:18:57 +08:00
|
|
|
/**
|
2016-08-01 16:22:17 +08:00
|
|
|
* @brief Makes a copy of the vpx_image_t and emits it as a new VideoFrame.
|
|
|
|
* @param vpxframe Frame to copy.
|
|
|
|
*/
|
2015-05-14 10:46:28 +08:00
|
|
|
void CoreVideoSource::pushFrame(const vpx_image_t* vpxframe)
|
|
|
|
{
|
2015-10-24 10:26:29 +08:00
|
|
|
if (stopped)
|
|
|
|
return;
|
|
|
|
|
2015-10-24 10:38:00 +08:00
|
|
|
QMutexLocker locker(&biglock);
|
2015-05-14 10:46:28 +08:00
|
|
|
|
|
|
|
std::shared_ptr<VideoFrame> vframe;
|
2016-06-16 17:23:01 +08:00
|
|
|
int width = vpxframe->d_w;
|
|
|
|
int height = vpxframe->d_h;
|
2015-05-14 10:46:28 +08:00
|
|
|
|
|
|
|
if (subscribers <= 0)
|
2015-10-24 10:38:00 +08:00
|
|
|
return;
|
2015-05-14 10:46:28 +08:00
|
|
|
|
2016-06-16 17:23:01 +08:00
|
|
|
AVFrame* avframe = av_frame_alloc();
|
2015-05-14 10:46:28 +08:00
|
|
|
if (!avframe)
|
2015-10-24 10:38:00 +08:00
|
|
|
return;
|
2016-04-25 16:11:56 +08:00
|
|
|
|
2015-05-14 10:46:28 +08:00
|
|
|
avframe->width = width;
|
|
|
|
avframe->height = height;
|
|
|
|
avframe->format = AV_PIX_FMT_YUV420P;
|
|
|
|
|
2017-02-26 19:52:45 +08:00
|
|
|
int bufSize =
|
|
|
|
av_image_alloc(avframe->data, avframe->linesize, width, height,
|
|
|
|
static_cast<AVPixelFormat>(AV_PIX_FMT_YUV420P), VideoFrame::dataAlignment);
|
2016-04-25 16:11:56 +08:00
|
|
|
|
2017-02-26 19:52:45 +08:00
|
|
|
if (bufSize < 0) {
|
2015-05-14 10:46:28 +08:00
|
|
|
av_frame_free(&avframe);
|
2015-10-24 10:38:00 +08:00
|
|
|
return;
|
2015-05-14 10:46:28 +08:00
|
|
|
}
|
|
|
|
|
2017-02-26 19:52:45 +08:00
|
|
|
for (int i = 0; i < 3; ++i) {
|
2016-06-16 17:23:01 +08:00
|
|
|
int dstStride = avframe->linesize[i];
|
|
|
|
int srcStride = vpxframe->stride[i];
|
|
|
|
int minStride = std::min(dstStride, srcStride);
|
|
|
|
int size = (i == 0) ? height : height / 2;
|
|
|
|
|
2017-02-26 19:52:45 +08:00
|
|
|
for (int j = 0; j < size; ++j) {
|
2016-04-25 16:11:56 +08:00
|
|
|
uint8_t* dst = avframe->data[i] + dstStride * j;
|
|
|
|
uint8_t* src = vpxframe->planes[i] + srcStride * j;
|
2016-06-16 17:23:01 +08:00
|
|
|
memcpy(dst, src, minStride);
|
|
|
|
}
|
|
|
|
}
|
2015-05-14 10:46:28 +08:00
|
|
|
|
2016-05-02 16:22:08 +08:00
|
|
|
vframe = std::make_shared<VideoFrame>(id, avframe, true);
|
2015-05-14 10:46:28 +08:00
|
|
|
emit frameAvailable(vframe);
|
|
|
|
}
|
|
|
|
|
|
|
|
bool CoreVideoSource::subscribe()
|
|
|
|
{
|
2015-10-24 10:38:00 +08:00
|
|
|
QMutexLocker locker(&biglock);
|
2015-05-14 10:46:28 +08:00
|
|
|
++subscribers;
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
void CoreVideoSource::unsubscribe()
|
|
|
|
{
|
2015-10-24 10:38:00 +08:00
|
|
|
biglock.lock();
|
2017-02-26 19:52:45 +08:00
|
|
|
if (--subscribers == 0) {
|
|
|
|
if (deleteOnClose) {
|
2015-10-24 10:38:00 +08:00
|
|
|
biglock.unlock();
|
|
|
|
// DANGEROUS: No member access after this point, that's why we manually unlock
|
2015-05-14 10:46:28 +08:00
|
|
|
delete this;
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
}
|
2015-10-24 10:38:00 +08:00
|
|
|
biglock.unlock();
|
2015-05-14 10:46:28 +08:00
|
|
|
}
|
|
|
|
|
2016-07-27 06:18:57 +08:00
|
|
|
/**
|
2016-08-01 16:22:17 +08:00
|
|
|
* @brief Setup delete on close
|
|
|
|
* @param If true, self-delete after the last suscriber is gone
|
|
|
|
*/
|
2015-05-14 10:46:28 +08:00
|
|
|
void CoreVideoSource::setDeleteOnClose(bool newstate)
|
|
|
|
{
|
2015-10-24 10:38:00 +08:00
|
|
|
QMutexLocker locker(&biglock);
|
2015-05-14 10:46:28 +08:00
|
|
|
deleteOnClose = newstate;
|
|
|
|
}
|
2015-10-24 10:26:29 +08:00
|
|
|
|
2016-07-27 06:18:57 +08:00
|
|
|
/**
|
2016-08-01 16:22:17 +08:00
|
|
|
* @brief Stopping the source.
|
|
|
|
* @see The callers in CoreAV for the rationale
|
|
|
|
*
|
|
|
|
* Stopping the source will block any pushFrame calls from doing anything
|
|
|
|
*/
|
2015-10-24 10:26:29 +08:00
|
|
|
void CoreVideoSource::stopSource()
|
|
|
|
{
|
2015-10-24 10:38:00 +08:00
|
|
|
QMutexLocker locker(&biglock);
|
2015-10-24 10:26:29 +08:00
|
|
|
stopped = true;
|
|
|
|
emit sourceStopped();
|
|
|
|
}
|
|
|
|
|
|
|
|
void CoreVideoSource::restartSource()
|
|
|
|
{
|
2015-10-24 10:38:00 +08:00
|
|
|
QMutexLocker locker(&biglock);
|
2015-10-24 10:26:29 +08:00
|
|
|
stopped = false;
|
|
|
|
}
|