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

222 lines
6.6 KiB
C++
Raw Normal View History

#include "src/core/toxcall.h"
#include "src/audio/audio.h"
#include "src/core/coreav.h"
#include "src/persistence/settings.h"
#include "src/video/camerasource.h"
#include "src/video/corevideosource.h"
2015-10-05 08:36:50 +08:00
#include <QTimer>
#include <QtConcurrent/QtConcurrent>
2015-10-05 08:36:50 +08:00
2016-07-27 06:21:22 +08:00
/**
2016-08-01 16:20:56 +08:00
* @var uint32_t ToxCall::callId
* @brief Could be a friendNum or groupNum, must uniquely identify the call. Do not modify!
*
* @var bool ToxCall::inactive
* @brief True while we're not participating. (stopped group call, ringing but hasn't started yet,
* ...)
2016-08-01 16:20:56 +08:00
*
* @var bool ToxFriendCall::videoEnabled
2016-08-01 16:20:56 +08:00
* @brief True if our user asked for a video call, sending and recieving.
*
* @var bool ToxFriendCall::nullVideoBitrate
2016-08-01 16:20:56 +08:00
* @brief True if our video bitrate is zero, i.e. if the device is closed.
*
* @var TOXAV_FRIEND_CALL_STATE ToxFriendCall::state
2016-08-01 16:20:56 +08:00
* @brief State of the peer (not ours!)
*
* @var QMap ToxGroupCall::peers
* @brief Keeps sources for users in group calls.
2016-08-01 16:20:56 +08:00
*/
2016-07-27 06:21:22 +08:00
2015-10-05 08:36:50 +08:00
using namespace std;
ToxCall::ToxCall(uint32_t CallId)
: alSource{0}
, inactive{true}
, muteMic{false}
, muteVol{false}
2015-10-05 08:36:50 +08:00
{
2015-11-09 04:13:01 +08:00
Audio& audio = Audio::getInstance();
audio.subscribeInput();
audio.subscribeOutput(alSource);
2015-10-05 08:36:50 +08:00
}
ToxCall::ToxCall(ToxCall&& other) noexcept : audioInConn{other.audioInConn},
alSource{other.alSource},
inactive{other.inactive},
muteMic{other.muteMic},
muteVol{other.muteVol}
2015-10-05 08:36:50 +08:00
{
other.audioInConn = QMetaObject::Connection();
2015-10-05 08:36:50 +08:00
other.alSource = 0;
// required -> ownership of audio input is moved to new instance
Audio& audio = Audio::getInstance();
audio.subscribeInput();
2015-10-05 08:36:50 +08:00
}
ToxCall::~ToxCall()
{
Audio& audio = Audio::getInstance();
2015-12-11 07:16:21 +08:00
QObject::disconnect(audioInConn);
audio.unsubscribeInput();
audio.unsubscribeOutput(alSource);
2015-10-05 08:36:50 +08:00
}
ToxCall& ToxCall::operator=(ToxCall&& other) noexcept
2015-10-05 08:36:50 +08:00
{
audioInConn = other.audioInConn;
other.audioInConn = QMetaObject::Connection();
2015-10-05 08:36:50 +08:00
inactive = other.inactive;
muteMic = other.muteMic;
muteVol = other.muteVol;
alSource = other.alSource;
other.alSource = 0;
// required -> ownership of audio input is moved to new instance
Audio& audio = Audio::getInstance();
audio.subscribeInput();
2015-10-05 08:36:50 +08:00
return *this;
}
void ToxFriendCall::startTimeout(uint32_t callId)
2015-10-24 07:53:10 +08:00
{
if (!timeoutTimer) {
2015-10-24 07:53:10 +08:00
timeoutTimer = new QTimer();
// We might move, so we need copies of members. CoreAV won't move while we're alive
CoreAV* avCopy = av;
QObject::connect(timeoutTimer, &QTimer::timeout,
[avCopy, callId]() { avCopy->timeoutCall(callId); });
2015-10-24 07:53:10 +08:00
}
if (!timeoutTimer->isActive())
timeoutTimer->start(CALL_TIMEOUT);
}
void ToxFriendCall::stopTimeout()
{
if (!timeoutTimer)
return;
timeoutTimer->stop();
delete timeoutTimer;
timeoutTimer = nullptr;
}
2015-10-05 08:36:50 +08:00
ToxFriendCall::ToxFriendCall(uint32_t FriendNum, bool VideoEnabled, CoreAV& av)
: ToxCall(FriendNum)
, videoEnabled{VideoEnabled}
, nullVideoBitrate{false}
, videoSource{nullptr}
, state{static_cast<TOXAV_FRIEND_CALL_STATE>(0)}
, av{&av}
, timeoutTimer{nullptr}
2015-10-05 08:36:50 +08:00
{
audioInConn = QObject::connect(&Audio::getInstance(), &Audio::frameAvailable,
[&av, FriendNum](const int16_t* pcm, size_t samples,
uint8_t chans, uint32_t rate) {
av.sendCallAudio(FriendNum, pcm, samples, chans, rate);
});
if(!audioInConn) {
qDebug() << "Audio connection not working";
}
2015-10-05 08:36:50 +08:00
if (videoEnabled) {
2015-10-05 08:36:50 +08:00
videoSource = new CoreVideoSource;
CameraSource& source = CameraSource::getInstance();
if (source.isNone())
source.setupDefault();
2015-10-05 08:36:50 +08:00
source.subscribe();
QObject::connect(&source, &VideoSource::frameAvailable,
[FriendNum, &av](shared_ptr<VideoFrame> frame) {
av.sendCallVideo(FriendNum, frame);
});
2015-10-05 08:36:50 +08:00
}
}
ToxFriendCall::ToxFriendCall(ToxFriendCall&& other) noexcept
2015-10-05 08:36:50 +08:00
: ToxCall(move(other)),
videoEnabled{other.videoEnabled},
nullVideoBitrate{other.nullVideoBitrate},
videoSource{other.videoSource},
state{other.state},
av{other.av},
timeoutTimer{other.timeoutTimer}
2015-10-05 08:36:50 +08:00
{
other.videoEnabled = false;
other.videoSource = nullptr;
2015-10-24 07:53:10 +08:00
other.timeoutTimer = nullptr;
2015-10-05 08:36:50 +08:00
}
ToxFriendCall::~ToxFriendCall()
{
2015-10-24 07:53:10 +08:00
if (timeoutTimer)
delete timeoutTimer;
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(); });
if (videoSource) {
2015-10-05 08:36:50 +08:00
videoSource->setDeleteOnClose(true);
videoSource = nullptr;
}
}
}
ToxFriendCall& ToxFriendCall::operator=(ToxFriendCall&& other) noexcept
2015-10-05 08:36:50 +08:00
{
ToxCall::operator=(move(other));
2015-10-05 08:36:50 +08:00
videoEnabled = other.videoEnabled;
other.videoEnabled = false;
videoSource = other.videoSource;
other.videoSource = nullptr;
state = other.state;
2015-10-24 07:53:10 +08:00
timeoutTimer = other.timeoutTimer;
other.timeoutTimer = nullptr;
av = other.av;
nullVideoBitrate = other.nullVideoBitrate;
2015-10-05 08:36:50 +08:00
return *this;
}
ToxGroupCall::ToxGroupCall(int GroupNum, CoreAV& av)
: ToxCall(static_cast<uint32_t>(GroupNum))
2015-10-05 08:36:50 +08:00
{
static_assert(
numeric_limits<uint32_t>::max() >= numeric_limits<decltype(GroupNum)>::max(),
"The callId must be able to represent any group number, change its type if needed");
2015-10-05 08:36:50 +08:00
audioInConn = QObject::connect(&Audio::getInstance(), &Audio::frameAvailable,
[&av, GroupNum](const int16_t* pcm, size_t samples,
uint8_t chans, uint32_t rate) {
av.sendGroupCallAudio(GroupNum, pcm, samples, chans, rate);
});
2015-10-05 08:36:50 +08:00
}
ToxGroupCall::ToxGroupCall(ToxGroupCall&& other) noexcept : ToxCall(move(other))
2015-10-05 08:36:50 +08:00
{
}
ToxGroupCall::~ToxGroupCall()
{
Audio& audio = Audio::getInstance();
for (quint32 v : peers) {
audio.unsubscribeOutput(v);
}
}
ToxGroupCall& ToxGroupCall::operator=(ToxGroupCall&& other) noexcept
2015-10-05 08:36:50 +08:00
{
ToxCall::operator=(move(other));
2015-10-05 08:36:50 +08:00
return *this;
}