qTox  Version: nightly | Commit: bc751c8e1cac455f9690654fcfe0f560d2d7dfdd
camerasource.h
Go to the documentation of this file.
1 /*
2  Copyright © 2015-2019 by 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 #pragma once
21 
22 #include "src/video/videomode.h"
23 #include "src/video/videosource.h"
24 #include <QFuture>
25 #include <QHash>
26 #include <QReadWriteLock>
27 #include <QString>
28 #include <QVector>
29 #include <atomic>
30 
31 class CameraDevice;
32 struct AVCodecContext;
33 
34 class CameraSource : public VideoSource
35 {
36  Q_OBJECT
37 
38 public:
39  static CameraSource& getInstance();
40  static void destroyInstance();
41  void setupDefault();
42  bool isNone() const;
43 
44  // VideoSource interface
45  void subscribe() override;
46  void unsubscribe() override;
47 
48 public slots:
49  void setupDevice(const QString& deviceName, const VideoMode& mode);
50 
51 signals:
52  void deviceOpened();
53  void openFailed();
54 
55 private:
56  CameraSource();
57  ~CameraSource();
58  void stream();
59 
60 private slots:
61  void openDevice();
62  void closeDevice();
63 
64 private:
65  QFuture<void> streamFuture;
66  QThread* deviceThread;
67 
68  QString deviceName;
71  AVCodecContext* cctx;
72  // TODO: Remove when ffmpeg version will be bumped to the 3.1.0
73  AVCodecContext* cctxOrig;
75 
76  QReadWriteLock deviceMutex;
77  QReadWriteLock streamMutex;
78 
79  std::atomic_bool _isNone;
80  std::atomic_int subscriptions;
81 
83 };
CameraSource::setupDefault
void setupDefault()
Setup default device.
Definition: camerasource.cpp:145
videomode.h
CameraSource::unsubscribe
void unsubscribe() override
Stop emitting frameAvailable signals, and free associated resources if necessary.
Definition: camerasource.cpp:246
CameraSource::instance
static CameraSource * instance
Definition: camerasource.h:82
CameraSource::cctx
AVCodecContext * cctx
Codec context of the camera's selected video stream.
Definition: camerasource.h:71
VideoSource
An abstract source of video frames.
Definition: videosource.h:29
CameraSource::~CameraSource
~CameraSource()
Definition: camerasource.cpp:198
CameraSource::deviceName
QString deviceName
Short name of the device for CameraDevice's open(QString)
Definition: camerasource.h:68
CameraSource
This class is a wrapper to share a camera's captured video frames.
Definition: camerasource.h:34
CameraSource::openDevice
void openDevice()
Opens the video device and starts streaming.
Definition: camerasource.cpp:260
CameraSource::isNone
bool isNone() const
Definition: camerasource.cpp:193
CameraSource::mode
VideoMode mode
What mode we tried to open the device in, all zeros means default mode.
Definition: camerasource.h:70
CameraSource::cctxOrig
AVCodecContext * cctxOrig
Codec context of the camera's selected video stream.
Definition: camerasource.h:73
CameraSource::deviceOpened
void deviceOpened()
CameraSource::setupDevice
void setupDevice(const QString &deviceName, const VideoMode &mode)
Change the device and mode.
Definition: camerasource.cpp:162
CameraSource::videoStreamIndex
int videoStreamIndex
A camera can have multiple streams, this is the one we're decoding.
Definition: camerasource.h:74
CameraSource::device
CameraDevice * device
Non-owning pointer to an open CameraDevice, or nullptr. Not atomic, synced with memfences when become...
Definition: camerasource.h:69
CameraSource::stream
void stream()
Blocking. Decodes video stream and emits new frames.
Definition: camerasource.cpp:409
CameraSource::deviceThread
QThread * deviceThread
Definition: camerasource.h:66
CameraSource::deviceMutex
QReadWriteLock deviceMutex
Definition: camerasource.h:76
CameraSource::destroyInstance
static void destroyInstance()
Definition: camerasource.cpp:135
CameraSource::streamFuture
QFuture< void > streamFuture
Future of the streaming thread.
Definition: camerasource.h:65
CameraSource::getInstance
static CameraSource & getInstance()
Returns the singleton instance.
Definition: camerasource.cpp:128
CameraSource::CameraSource
CameraSource()
Definition: camerasource.cpp:95
VideoMode
Describes a video mode supported by a device.
Definition: videomode.h:25
CameraSource::openFailed
void openFailed()
CameraSource::closeDevice
void closeDevice()
Closes the video device and stops streaming.
Definition: camerasource.cpp:376
CameraSource::streamMutex
QReadWriteLock streamMutex
Definition: camerasource.h:77
CameraSource::subscribe
void subscribe() override
If subscribe sucessfully opens the source, it will start emitting frameAvailable signals.
Definition: camerasource.cpp:238
CameraSource::subscriptions
std::atomic_int subscriptions
Remember how many times we subscribed for RAII.
Definition: camerasource.h:80
CameraDevice
Definition: cameradevice.h:35
CameraSource::_isNone
std::atomic_bool _isNone
Definition: camerasource.h:79
videosource.h