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

186 lines
5.3 KiB
C++
Raw Normal View History

2015-10-05 08:36:50 +08:00
#include "src/audio/audio.h"
#include "src/core/toxcall.h"
#include "src/core/coreav.h"
#include "src/persistence/settings.h"
#include "src/video/camerasource.h"
#include "src/video/corevideosource.h"
#include <QTimer>
#include <QtConcurrent/QtConcurrent>
2015-10-05 08:36:50 +08:00
#ifdef QTOX_FILTER_AUDIO
#include "src/audio/audiofilterer.h"
#endif
using namespace std;
ToxCall::ToxCall(uint32_t CallId)
: sendAudioTimer{new QTimer}, callId{CallId},
inactive{true}, muteMic{false}, muteVol{false}, alSource{0}
{
qCritical() << "CREATED CALL "<<callId;
2015-10-05 08:36:50 +08:00
sendAudioTimer->setInterval(5);
sendAudioTimer->setSingleShot(true);
Audio::getInstance().subscribeInput();
#ifdef QTOX_FILTER_AUDIO
if (Settings::getInstance().getFilterAudio())
{
filterer = new AudioFilterer();
filterer->startFilter(AUDIO_SAMPLE_RATE);
}
else
{
filterer = nullptr;
}
#endif
}
ToxCall::ToxCall(ToxCall&& other)
: sendAudioTimer{other.sendAudioTimer}, callId{other.callId},
inactive{other.inactive}, muteMic{other.muteMic}, muteVol{other.muteVol},
alSource{other.alSource}
{
other.sendAudioTimer = nullptr;
other.callId = numeric_limits<decltype(callId)>::max();
other.alSource = 0;
#ifdef QTOX_FILTER_AUDIO
filterer = other.filterer;
other.filterer = nullptr;
#endif
}
ToxCall::~ToxCall()
{
if (callId != (uint32_t)(int32_t)-1)
qCritical() << "DELETED CALL "<<callId;
2015-10-05 08:36:50 +08:00
if (sendAudioTimer)
{
QObject::disconnect(sendAudioTimer, nullptr, nullptr, nullptr);
sendAudioTimer->stop();
Audio::getInstance().unsubscribeInput();
}
if (alSource)
Audio::deleteSource(&alSource);
#ifdef QTOX_FILTER_AUDIO
if (filterer)
delete filterer;
#endif
}
const ToxCall& ToxCall::operator=(ToxCall&& other)
{
sendAudioTimer = other.sendAudioTimer;
other.sendAudioTimer = nullptr;
callId = other.callId;
other.callId = numeric_limits<decltype(callId)>::max();
inactive = other.inactive;
muteMic = other.muteMic;
muteVol = other.muteVol;
alSource = other.alSource;
other.alSource = 0;
#ifdef QTOX_FILTER_AUDIO
filterer = other.filterer;
other.filterer = nullptr;
#endif
return *this;
}
ToxFriendCall::ToxFriendCall(uint32_t FriendNum, bool VideoEnabled, CoreAV& av)
: ToxCall(FriendNum),
videoEnabled{VideoEnabled}, videoSource{nullptr},
state{static_cast<TOXAV_FRIEND_CALL_STATE>(0)}
{
auto audioTimerCopy = sendAudioTimer; // this might change after a move, but not sendAudioTimer
QObject::connect(sendAudioTimer, &QTimer::timeout, [FriendNum,&av,audioTimerCopy]()
{
// If sendCallAudio returns false, there was a serious error and we might as well stop the timer
if (av.sendCallAudio(FriendNum))
audioTimerCopy->start();
});
sendAudioTimer->start();
if (videoEnabled)
{
videoSource = new CoreVideoSource;
CameraSource& source = CameraSource::getInstance();
if (!source.isOpen())
source.open();
source.subscribe();
QObject::connect(&source, &VideoSource::frameAvailable,
[FriendNum,&av](shared_ptr<VideoFrame> frame){av.sendCallVideo(FriendNum,frame);});
}
}
ToxFriendCall::ToxFriendCall(ToxFriendCall&& other)
: ToxCall(move(other)),
videoEnabled{other.videoEnabled}, videoSource{other.videoSource},
state{other.state}
{
other.videoEnabled = false;
other.videoSource = nullptr;
}
ToxFriendCall::~ToxFriendCall()
{
if (videoEnabled)
{
// This destructor could be running in a toxav callback while holding toxav locks.
// If the CameraSource thread calls toxav *_send_frame, we might deadlock the toxav and CameraSource locks,
// so we unsuscribe asynchronously, it's fine if the webcam takes a couple milliseconds more to poweroff.
QtConcurrent::run([](){CameraSource::getInstance().unsubscribe();});
2015-10-05 08:36:50 +08:00
if (videoSource)
{
videoSource->setDeleteOnClose(true);
videoSource = nullptr;
}
}
}
const ToxFriendCall& ToxFriendCall::operator=(ToxFriendCall&& other)
{
ToxCall::operator =(move(other));
videoEnabled = other.videoEnabled;
other.videoEnabled = false;
videoSource = other.videoSource;
other.videoSource = nullptr;
state = other.state;
return *this;
}
ToxGroupCall::ToxGroupCall(int GroupNum, CoreAV &av)
: ToxCall(static_cast<decltype(callId)>(GroupNum))
{
static_assert(numeric_limits<decltype(callId)>::max() >= numeric_limits<decltype(GroupNum)>::max(),
"The callId must be able to represent any group number, change its type if needed");
auto audioTimerCopy = sendAudioTimer; // this might change after a move, but not sendAudioTimer
QObject::connect(sendAudioTimer, &QTimer::timeout, [GroupNum,&av,audioTimerCopy]()
{
// If sendGroupCallAudio returns false, there was a serious error and we might as well stop the timer
if (av.sendGroupCallAudio(GroupNum))
audioTimerCopy->start();
});
sendAudioTimer->start();
}
ToxGroupCall::ToxGroupCall(ToxGroupCall&& other)
: ToxCall(move(other))
{
}
const ToxGroupCall &ToxGroupCall::operator=(ToxGroupCall &&other)
{
ToxCall::operator =(move(other));
return *this;
}