1
0
mirror of https://github.com/qTox/qTox.git synced 2024-03-22 14:00:36 +08:00

refactor: Rename methods in CameraSource

To make the method more correctly reflect the essence
This commit is contained in:
Diadlo 2017-06-26 12:57:46 +03:00
parent 3c21af200c
commit 3b2352a0ee
No known key found for this signature in database
GPG Key ID: 5AF9F2E29107C727
4 changed files with 29 additions and 31 deletions

View File

@ -130,8 +130,8 @@ ToxFriendCall::ToxFriendCall(uint32_t FriendNum, bool VideoEnabled, CoreAV& av)
videoSource = new CoreVideoSource; videoSource = new CoreVideoSource;
CameraSource& source = CameraSource::getInstance(); CameraSource& source = CameraSource::getInstance();
if (!source.isOpen()) if (source.isNone())
source.open(); source.setupDefault();
source.subscribe(); source.subscribe();
QObject::connect(&source, &VideoSource::frameAvailable, QObject::connect(&source, &VideoSource::frameAvailable,
[FriendNum, &av](shared_ptr<VideoFrame> frame) { [FriendNum, &av](shared_ptr<VideoFrame> frame) {

View File

@ -99,7 +99,7 @@ CameraSource::CameraSource()
, cctxOrig{nullptr} , cctxOrig{nullptr}
#endif #endif
, videoStreamIndex{-1} , videoStreamIndex{-1}
, _isOpen{false} , _isNone{true}
, streamBlocker{false} , streamBlocker{false}
, subscriptions{0} , subscriptions{0}
{ {
@ -129,17 +129,15 @@ void CameraSource::destroyInstance()
} }
/** /**
* @brief Opens the source for the camera device. * @brief Setup default device
* @note If a device is already open, the source will seamlessly switch to the new device. * @note If a device is already open, the source will seamlessly switch to the new device.
*
* Opens the source for the camera device in argument, in the settings, or the system default.
*/ */
void CameraSource::open() void CameraSource::setupDefault()
{ {
open(CameraDevice::getDefaultDeviceName()); setupDevice(CameraDevice::getDefaultDeviceName());
} }
void CameraSource::open(const QString& deviceName) void CameraSource::setupDevice(const QString& deviceName)
{ {
bool isScreen = CameraDevice::isScreen(deviceName); bool isScreen = CameraDevice::isScreen(deviceName);
VideoMode mode = VideoMode(Settings::getInstance().getScreenRegion()); VideoMode mode = VideoMode(Settings::getInstance().getScreenRegion());
@ -148,10 +146,14 @@ void CameraSource::open(const QString& deviceName)
mode.FPS = Settings::getInstance().getCamVideoFPS(); mode.FPS = Settings::getInstance().getCamVideoFPS();
} }
open(deviceName, mode); setupDevice(deviceName, mode);
} }
void CameraSource::open(const QString& DeviceName, const VideoMode& Mode) /**
* @brief Change the device and mode.
* @note If a device is already open, the source will seamlessly switch to the new device.
*/
void CameraSource::setupDevice(const QString& DeviceName, const VideoMode& Mode)
{ {
QWriteLocker locker{&streamMutex}; QWriteLocker locker{&streamMutex};
@ -164,32 +166,27 @@ void CameraSource::open(const QString& DeviceName, const VideoMode& Mode)
deviceName = DeviceName; deviceName = DeviceName;
mode = Mode; mode = Mode;
_isOpen = (deviceName != "none"); _isNone = (deviceName == "none");
if (subscriptions && _isOpen) if (subscriptions && !_isNone)
openDevice(); openDevice();
} }
/**
* @brief Stops streaming.
*
* Equivalent to opening the source with the video device "none".
*/
void CameraSource::close() void CameraSource::close()
{ {
open("none"); setupDevice("none");
} }
bool CameraSource::isOpen() bool CameraSource::isNone() const
{ {
return _isOpen; return _isNone;
} }
CameraSource::~CameraSource() CameraSource::~CameraSource()
{ {
QWriteLocker locker{&streamMutex}; QWriteLocker locker{&streamMutex};
if (!_isOpen) { if (_isNone) {
return; return;
} }
@ -223,7 +220,7 @@ bool CameraSource::subscribe()
{ {
QWriteLocker locker{&streamMutex}; QWriteLocker locker{&streamMutex};
if (!_isOpen) { if (_isNone) {
++subscriptions; ++subscriptions;
return true; return true;
} }
@ -248,7 +245,7 @@ void CameraSource::unsubscribe()
{ {
QWriteLocker locker{&streamMutex}; QWriteLocker locker{&streamMutex};
if (!_isOpen) { if (_isNone) {
--subscriptions; --subscriptions;
return; return;
} }

View File

@ -39,11 +39,11 @@ class CameraSource : public VideoSource
public: public:
static CameraSource& getInstance(); static CameraSource& getInstance();
static void destroyInstance(); static void destroyInstance();
void open(); void setupDefault();
void open(const QString& deviceName); void setupDevice(const QString& deviceName);
void open(const QString& deviceName, const VideoMode& mode); void setupDevice(const QString& deviceName, const VideoMode& mode);
void close(); void close();
bool isOpen(); bool isNone() const;
// VideoSource interface // VideoSource interface
virtual bool subscribe() override; virtual bool subscribe() override;
@ -69,7 +69,8 @@ private:
AVCodecContext* cctxOrig; AVCodecContext* cctxOrig;
int videoStreamIndex; int videoStreamIndex;
QReadWriteLock streamMutex; QReadWriteLock streamMutex;
std::atomic_bool _isOpen;
std::atomic_bool _isNone;
std::atomic_bool streamBlocker; std::atomic_bool streamBlocker;
std::atomic_int subscriptions; std::atomic_int subscriptions;

View File

@ -131,7 +131,7 @@ void AVForm::open(const QString& devName, const VideoMode& mode)
QRect rect = mode.toRect(); QRect rect = mode.toRect();
Settings::getInstance().setCamVideoRes(rect); Settings::getInstance().setCamVideoRes(rect);
Settings::getInstance().setCamVideoFPS(static_cast<quint16>(mode.FPS)); Settings::getInstance().setCamVideoFPS(static_cast<quint16>(mode.FPS));
camera.open(devName, mode); camera.setupDevice(devName, mode);
} }
void AVForm::rescanDevices() void AVForm::rescanDevices()
@ -393,7 +393,7 @@ void AVForm::on_videoDevCombobox_currentIndexChanged(int index)
if (0 < modeIndex && modeIndex < videoModes.size()) if (0 < modeIndex && modeIndex < videoModes.size())
mode = videoModes[modeIndex]; mode = videoModes[modeIndex];
camera.open(dev, mode); camera.setupDevice(dev, mode);
if (dev == "none") if (dev == "none")
Core::getInstance()->getAv()->sendNoVideo(); Core::getInstance()->getAv()->sendNoVideo();
} }