qTox  Version: nightly | Commit: bc751c8e1cac455f9690654fcfe0f560d2d7dfdd
corevideosource.cpp
Go to the documentation of this file.
1 /*
2  Copyright © 2015-2019 The qTox Project Contributors
3 
4  This file is part of qTox, a Qt-based graphical interface for Tox.
5 
6  qTox is libre software: you can redistribute it and/or modify
7  it under the terms of the GNU General Public License as published by
8  the Free Software Foundation, either version 3 of the License, or
9  (at your option) any later version.
10 
11  qTox is distributed in the hope that it will be useful,
12  but WITHOUT ANY WARRANTY; without even the implied warranty of
13  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14  GNU General Public License for more details.
15 
16  You should have received a copy of the GNU General Public License
17  along with qTox. If not, see <http://www.gnu.org/licenses/>.
18 */
19 
20 extern "C" {
21 #pragma GCC diagnostic push
22 #pragma GCC diagnostic ignored "-Wold-style-cast"
23 #include <libavcodec/avcodec.h>
24 #include <libavutil/imgutils.h>
25 #pragma GCC diagnostic pop
26 }
27 
28 #include "corevideosource.h"
29 #include "videoframe.h"
30 
50  : subscribers{0}
51  , deleteOnClose{false}
52  , stopped{false}
53 {
54 }
55 
60 void CoreVideoSource::pushFrame(const vpx_image_t* vpxframe)
61 {
62  if (stopped)
63  return;
64 
65  QMutexLocker locker(&biglock);
66 
67  std::shared_ptr<VideoFrame> vframe;
68  int width = vpxframe->d_w;
69  int height = vpxframe->d_h;
70 
71  if (subscribers <= 0)
72  return;
73 
74  AVFrame* avframe = av_frame_alloc();
75  if (!avframe)
76  return;
77 
78  avframe->width = width;
79  avframe->height = height;
80  avframe->format = AV_PIX_FMT_YUV420P;
81 
82  int bufSize =
83  av_image_alloc(avframe->data, avframe->linesize, width, height,
84  static_cast<AVPixelFormat>(AV_PIX_FMT_YUV420P), VideoFrame::dataAlignment);
85 
86  if (bufSize < 0) {
87  av_frame_free(&avframe);
88  return;
89  }
90 
91  for (int i = 0; i < 3; ++i) {
92  int dstStride = avframe->linesize[i];
93  int srcStride = vpxframe->stride[i];
94  int minStride = std::min(dstStride, srcStride);
95  int size = (i == 0) ? height : height / 2;
96 
97  for (int j = 0; j < size; ++j) {
98  uint8_t* dst = avframe->data[i] + dstStride * j;
99  uint8_t* src = vpxframe->planes[i] + srcStride * j;
100  memcpy(dst, src, minStride);
101  }
102  }
103 
104  vframe = std::make_shared<VideoFrame>(id, avframe, true);
105  emit frameAvailable(vframe);
106 }
107 
109 {
110  QMutexLocker locker(&biglock);
111  ++subscribers;
112 }
113 
115 {
116  biglock.lock();
117  if (--subscribers == 0) {
118  if (deleteOnClose) {
119  biglock.unlock();
120  // DANGEROUS: No member access after this point, that's why we manually unlock
121  delete this;
122  return;
123  }
124  }
125  biglock.unlock();
126 }
127 
133 {
134  QMutexLocker locker(&biglock);
135  deleteOnClose = newstate;
136 }
137 
145 {
146  QMutexLocker locker(&biglock);
147  stopped = true;
148  emit sourceStopped();
149 }
150 
152 {
153  QMutexLocker locker(&biglock);
154  stopped = false;
155 }
VideoSource::frameAvailable
void frameAvailable(std::shared_ptr< VideoFrame > frame)
Emitted when new frame available to use.
CoreVideoSource::deleteOnClose
std::atomic_bool deleteOnClose
Definition: corevideosource.h:47
videoframe.h
VideoSource::sourceStopped
void sourceStopped()
Emitted when the source is stopped for an indefinite amount of time, but might restart sending frames...
CoreVideoSource::stopped
std::atomic_bool stopped
Definition: corevideosource.h:49
CoreVideoSource::unsubscribe
void unsubscribe() override
Stop emitting frameAvailable signals, and free associated resources if necessary.
Definition: corevideosource.cpp:114
CoreVideoSource::biglock
QMutex biglock
Definition: corevideosource.h:48
CoreVideoSource::pushFrame
void pushFrame(const vpx_image_t *frame)
Makes a copy of the vpx_image_t and emits it as a new VideoFrame.
Definition: corevideosource.cpp:60
CoreVideoSource::CoreVideoSource
CoreVideoSource()
CoreVideoSource constructor.
Definition: corevideosource.cpp:49
CoreVideoSource::subscribers
std::atomic_int subscribers
Definition: corevideosource.h:46
CoreVideoSource::restartSource
void restartSource()
Definition: corevideosource.cpp:151
CoreVideoSource::setDeleteOnClose
void setDeleteOnClose(bool newstate)
Setup delete on close.
Definition: corevideosource.cpp:132
CoreVideoSource::subscribe
void subscribe() override
If subscribe sucessfully opens the source, it will start emitting frameAvailable signals.
Definition: corevideosource.cpp:108
corevideosource.h
VideoFrame::dataAlignment
static constexpr int dataAlignment
Data alignment parameter used to populate AVFrame buffers.
Definition: videoframe.h:94
CoreVideoSource::stopSource
void stopSource()
Stopping the source.
Definition: corevideosource.cpp:144