2016-12-19 10:26:26 +08:00
|
|
|
#include "src/core/toxcall.h"
|
2017-02-26 19:52:45 +08:00
|
|
|
#include "src/audio/audio.h"
|
2016-12-19 10:26:26 +08:00
|
|
|
#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>
|
2015-10-08 03:53:22 +08:00
|
|
|
#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
|
2017-02-26 19:52:45 +08:00
|
|
|
* @brief True while we're not participating. (stopped group call, ringing but hasn't started yet,
|
|
|
|
* ...)
|
2016-08-01 16:20:56 +08:00
|
|
|
*
|
2016-11-06 06:10:22 +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.
|
|
|
|
*
|
2016-11-06 06:10:22 +08:00
|
|
|
* @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.
|
|
|
|
*
|
2016-11-06 06:10:22 +08:00
|
|
|
* @var TOXAV_FRIEND_CALL_STATE ToxFriendCall::state
|
2016-08-01 16:20:56 +08:00
|
|
|
* @brief State of the peer (not ours!)
|
2017-02-06 07:34:41 +08:00
|
|
|
*
|
|
|
|
* @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;
|
|
|
|
|
2017-10-28 01:35:25 +08:00
|
|
|
ToxCall::ToxCall()
|
|
|
|
: active{false}
|
2017-02-26 19:52:45 +08:00
|
|
|
, muteMic{false}
|
|
|
|
, muteVol{false}
|
2015-10-05 08:36:50 +08:00
|
|
|
{
|
2015-11-09 04:13:01 +08:00
|
|
|
Audio& audio = Audio::getInstance();
|
2015-12-10 13:48:28 +08:00
|
|
|
audio.subscribeInput();
|
2015-10-05 08:36:50 +08:00
|
|
|
}
|
|
|
|
|
2017-10-28 20:17:14 +08:00
|
|
|
ToxCall::ToxCall(ToxCall&& other) noexcept
|
|
|
|
: audioInConn{other.audioInConn}
|
|
|
|
, active{other.active}
|
|
|
|
, muteMic{other.muteMic}
|
|
|
|
, muteVol{other.muteVol}
|
2015-10-05 08:36:50 +08:00
|
|
|
{
|
2016-01-22 03:17:18 +08:00
|
|
|
other.audioInConn = QMetaObject::Connection();
|
2017-10-28 01:35:25 +08:00
|
|
|
// invalidate object, all resources are moved
|
|
|
|
other.valid = false;
|
2015-10-05 08:36:50 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
ToxCall::~ToxCall()
|
|
|
|
{
|
2015-12-05 22:11:06 +08:00
|
|
|
Audio& audio = Audio::getInstance();
|
2015-12-11 07:16:21 +08:00
|
|
|
|
2017-10-28 01:35:25 +08:00
|
|
|
// only free resources if they weren't moved
|
2017-10-28 20:17:14 +08:00
|
|
|
if (valid) {
|
2017-10-28 01:35:25 +08:00
|
|
|
QObject::disconnect(audioInConn);
|
|
|
|
audio.unsubscribeInput();
|
|
|
|
}
|
2015-10-05 08:36:50 +08:00
|
|
|
}
|
|
|
|
|
2016-07-01 07:23:31 +08:00
|
|
|
ToxCall& ToxCall::operator=(ToxCall&& other) noexcept
|
2015-10-05 08:36:50 +08:00
|
|
|
{
|
2017-11-02 10:58:23 +08:00
|
|
|
if (valid) {
|
|
|
|
// if we're already valid, we need to cleanup our resources since we're going to inherit the resources
|
|
|
|
// that are being moved in
|
|
|
|
QObject::disconnect(audioInConn);
|
|
|
|
Audio::getInstance().unsubscribeInput();
|
|
|
|
}
|
|
|
|
|
2016-01-22 03:17:18 +08:00
|
|
|
audioInConn = other.audioInConn;
|
|
|
|
other.audioInConn = QMetaObject::Connection();
|
2017-10-28 01:35:25 +08:00
|
|
|
active = other.active;
|
2015-10-05 08:36:50 +08:00
|
|
|
muteMic = other.muteMic;
|
|
|
|
muteVol = other.muteVol;
|
2017-10-28 01:35:25 +08:00
|
|
|
// invalidate object, all resources are moved
|
|
|
|
other.valid = false;
|
2015-12-10 13:48:28 +08:00
|
|
|
|
2015-10-05 08:36:50 +08:00
|
|
|
return *this;
|
|
|
|
}
|
|
|
|
|
2017-10-28 01:35:25 +08:00
|
|
|
bool ToxCall::isActive() const
|
|
|
|
{
|
|
|
|
return active;
|
|
|
|
}
|
|
|
|
|
|
|
|
void ToxCall::setActive(bool value)
|
|
|
|
{
|
|
|
|
active = value;
|
|
|
|
}
|
|
|
|
|
|
|
|
bool ToxCall::getMuteVol() const
|
|
|
|
{
|
|
|
|
return muteVol;
|
|
|
|
}
|
|
|
|
|
|
|
|
void ToxCall::setMuteVol(bool value)
|
|
|
|
{
|
|
|
|
muteVol = value;
|
|
|
|
}
|
|
|
|
|
|
|
|
bool ToxCall::getMuteMic() const
|
|
|
|
{
|
|
|
|
return muteMic;
|
|
|
|
}
|
|
|
|
|
|
|
|
void ToxCall::setMuteMic(bool value)
|
|
|
|
{
|
|
|
|
muteMic = value;
|
|
|
|
}
|
|
|
|
|
2017-10-27 07:38:00 +08:00
|
|
|
void ToxFriendCall::startTimeout(uint32_t callId)
|
2015-10-24 07:53:10 +08:00
|
|
|
{
|
2017-02-26 19:52:45 +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;
|
2017-02-26 19:52:45 +08:00
|
|
|
QObject::connect(timeoutTimer, &QTimer::timeout,
|
2017-10-27 07:38:00 +08:00
|
|
|
[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;
|
|
|
|
}
|
|
|
|
|
2017-10-28 01:35:25 +08:00
|
|
|
bool ToxFriendCall::getVideoEnabled() const
|
|
|
|
{
|
|
|
|
return videoEnabled;
|
|
|
|
}
|
|
|
|
|
|
|
|
void ToxFriendCall::setVideoEnabled(bool value)
|
|
|
|
{
|
|
|
|
videoEnabled = value;
|
|
|
|
}
|
|
|
|
|
|
|
|
bool ToxFriendCall::getNullVideoBitrate() const
|
|
|
|
{
|
|
|
|
return nullVideoBitrate;
|
|
|
|
}
|
|
|
|
|
|
|
|
void ToxFriendCall::setNullVideoBitrate(bool value)
|
|
|
|
{
|
|
|
|
nullVideoBitrate = value;
|
|
|
|
}
|
|
|
|
|
2017-10-28 20:17:14 +08:00
|
|
|
CoreVideoSource* ToxFriendCall::getVideoSource() const
|
2017-10-28 01:35:25 +08:00
|
|
|
{
|
|
|
|
return videoSource;
|
|
|
|
}
|
|
|
|
|
|
|
|
TOXAV_FRIEND_CALL_STATE ToxFriendCall::getState() const
|
|
|
|
{
|
|
|
|
return state;
|
|
|
|
}
|
|
|
|
|
2017-10-28 20:17:14 +08:00
|
|
|
void ToxFriendCall::setState(const TOXAV_FRIEND_CALL_STATE& value)
|
2017-10-28 01:35:25 +08:00
|
|
|
{
|
|
|
|
state = value;
|
|
|
|
}
|
|
|
|
|
|
|
|
quint32 ToxFriendCall::getAlSource() const
|
|
|
|
{
|
|
|
|
return alSource;
|
|
|
|
}
|
|
|
|
|
2017-10-28 20:17:14 +08:00
|
|
|
void ToxFriendCall::setAlSource(const quint32& value)
|
2017-10-28 01:35:25 +08:00
|
|
|
{
|
|
|
|
alSource = value;
|
|
|
|
}
|
|
|
|
|
|
|
|
ToxFriendCall::ToxFriendCall(uint32_t friendId, bool VideoEnabled, CoreAV& av)
|
|
|
|
: ToxCall()
|
2017-02-26 19:52:45 +08:00
|
|
|
, 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
|
|
|
{
|
2017-10-28 01:35:25 +08:00
|
|
|
Audio& audio = Audio::getInstance();
|
|
|
|
audioInConn = QObject::connect(&audio, &Audio::frameAvailable,
|
|
|
|
[&av, friendId](const int16_t* pcm, size_t samples,
|
2017-10-28 20:17:14 +08:00
|
|
|
uint8_t chans, uint32_t rate) {
|
2017-10-28 01:35:25 +08:00
|
|
|
av.sendCallAudio(friendId, pcm, samples, chans, rate);
|
2017-02-26 19:52:45 +08:00
|
|
|
});
|
2017-10-28 20:17:14 +08:00
|
|
|
if (!audioInConn) {
|
2017-04-16 22:14:04 +08:00
|
|
|
qDebug() << "Audio connection not working";
|
|
|
|
}
|
2015-10-05 08:36:50 +08:00
|
|
|
|
2017-10-28 01:35:25 +08:00
|
|
|
// subscribe audio output
|
|
|
|
audio.subscribeOutput(alSource);
|
|
|
|
|
2017-02-26 19:52:45 +08:00
|
|
|
if (videoEnabled) {
|
2015-10-05 08:36:50 +08:00
|
|
|
videoSource = new CoreVideoSource;
|
|
|
|
CameraSource& source = CameraSource::getInstance();
|
2016-06-23 07:11:02 +08:00
|
|
|
|
2017-06-26 17:57:46 +08:00
|
|
|
if (source.isNone())
|
|
|
|
source.setupDefault();
|
2015-10-05 08:36:50 +08:00
|
|
|
source.subscribe();
|
|
|
|
QObject::connect(&source, &VideoSource::frameAvailable,
|
2017-10-28 01:35:25 +08:00
|
|
|
[friendId, &av](shared_ptr<VideoFrame> frame) {
|
|
|
|
av.sendCallVideo(friendId, frame);
|
2017-02-26 19:52:45 +08:00
|
|
|
});
|
2015-10-05 08:36:50 +08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2015-11-06 08:49:54 +08:00
|
|
|
ToxFriendCall::ToxFriendCall(ToxFriendCall&& other) noexcept
|
2017-10-28 20:17:14 +08:00
|
|
|
: ToxCall(move(other))
|
|
|
|
, alSource{other.alSource}
|
|
|
|
, 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;
|
2017-10-28 01:35:25 +08:00
|
|
|
// TODO: wrong, because "0" could be a valid source id
|
|
|
|
other.alSource = 0;
|
2015-10-05 08:36:50 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
ToxFriendCall::~ToxFriendCall()
|
|
|
|
{
|
2015-10-24 07:53:10 +08:00
|
|
|
if (timeoutTimer)
|
|
|
|
delete timeoutTimer;
|
|
|
|
|
2017-02-26 19:52:45 +08:00
|
|
|
if (videoEnabled) {
|
2015-10-08 03:53:22 +08:00
|
|
|
// This destructor could be running in a toxav callback while holding toxav locks.
|
2017-02-26 19:52:45 +08:00
|
|
|
// 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;
|
|
|
|
}
|
|
|
|
}
|
2017-10-28 20:17:14 +08:00
|
|
|
if (valid) {
|
2017-10-28 01:35:25 +08:00
|
|
|
Audio::getInstance().unsubscribeOutput(alSource);
|
|
|
|
}
|
2015-10-05 08:36:50 +08:00
|
|
|
}
|
|
|
|
|
2016-07-01 07:23:31 +08:00
|
|
|
ToxFriendCall& ToxFriendCall::operator=(ToxFriendCall&& other) noexcept
|
2015-10-05 08:36:50 +08:00
|
|
|
{
|
2017-02-26 19:52:45 +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;
|
2015-10-24 10:26:29 +08:00
|
|
|
nullVideoBitrate = other.nullVideoBitrate;
|
2017-10-28 01:35:25 +08:00
|
|
|
alSource = other.alSource;
|
|
|
|
// TODO: wrong, because "0" could be a valid source id
|
|
|
|
other.alSource = 0;
|
2015-10-05 08:36:50 +08:00
|
|
|
|
|
|
|
return *this;
|
|
|
|
}
|
|
|
|
|
2017-02-26 19:52:45 +08:00
|
|
|
ToxGroupCall::ToxGroupCall(int GroupNum, CoreAV& av)
|
2017-10-28 01:35:25 +08:00
|
|
|
: ToxCall()
|
2015-10-05 08:36:50 +08:00
|
|
|
{
|
2017-02-26 19:52:45 +08:00
|
|
|
static_assert(
|
2017-10-27 07:38:00 +08:00
|
|
|
numeric_limits<uint32_t>::max() >= numeric_limits<decltype(GroupNum)>::max(),
|
2017-02-26 19:52:45 +08:00
|
|
|
"The callId must be able to represent any group number, change its type if needed");
|
2015-10-05 08:36:50 +08:00
|
|
|
|
2016-01-22 03:17:18 +08:00
|
|
|
audioInConn = QObject::connect(&Audio::getInstance(), &Audio::frameAvailable,
|
2017-02-26 19:52:45 +08:00
|
|
|
[&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
|
|
|
}
|
|
|
|
|
2017-10-28 20:17:14 +08:00
|
|
|
ToxGroupCall::ToxGroupCall(ToxGroupCall&& other) noexcept
|
|
|
|
: ToxCall(move(other))
|
|
|
|
, peers{other.peers}
|
2015-10-05 08:36:50 +08:00
|
|
|
{
|
2017-10-28 01:35:25 +08:00
|
|
|
// all peers were moved, this ensures audio output is unsubscribed only once
|
|
|
|
other.peers.clear();
|
2015-10-05 08:36:50 +08:00
|
|
|
}
|
|
|
|
|
2017-02-06 07:34:41 +08:00
|
|
|
ToxGroupCall::~ToxGroupCall()
|
|
|
|
{
|
|
|
|
Audio& audio = Audio::getInstance();
|
|
|
|
|
2017-02-26 19:52:45 +08:00
|
|
|
for (quint32 v : peers) {
|
2017-02-06 07:34:41 +08:00
|
|
|
audio.unsubscribeOutput(v);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2017-02-26 19:52:45 +08:00
|
|
|
ToxGroupCall& ToxGroupCall::operator=(ToxGroupCall&& other) noexcept
|
2015-10-05 08:36:50 +08:00
|
|
|
{
|
2017-02-26 19:52:45 +08:00
|
|
|
ToxCall::operator=(move(other));
|
2017-10-28 01:35:25 +08:00
|
|
|
peers = other.peers;
|
|
|
|
other.peers.clear();
|
2015-10-05 08:36:50 +08:00
|
|
|
|
|
|
|
return *this;
|
|
|
|
}
|
2017-10-28 01:35:25 +08:00
|
|
|
|
|
|
|
void ToxGroupCall::removePeer(int peerId)
|
|
|
|
{
|
|
|
|
peers.remove(peerId);
|
|
|
|
}
|
|
|
|
|
2017-11-02 10:58:23 +08:00
|
|
|
QMap<int, quint32>& ToxGroupCall::getPeers()
|
2017-10-28 01:35:25 +08:00
|
|
|
{
|
|
|
|
return peers;
|
|
|
|
}
|