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
|
|
|
|
2018-07-01 16:39:33 +08:00
|
|
|
ToxCall::ToxCall(bool VideoEnabled, CoreAV& av)
|
2018-06-07 03:43:46 +08:00
|
|
|
: av{&av}
|
|
|
|
, videoEnabled{VideoEnabled}
|
2015-10-05 08:36:50 +08:00
|
|
|
{
|
|
|
|
}
|
|
|
|
|
2018-05-05 19:45:17 +08:00
|
|
|
/**
|
|
|
|
* @brief ToxCall move constructor
|
|
|
|
* @param other object moved from
|
|
|
|
*/
|
|
|
|
ToxCall::ToxCall(ToxCall&& other) noexcept : active{other.active},
|
|
|
|
av{other.av},
|
|
|
|
audioInConn{other.audioInConn},
|
2018-03-16 13:32:54 +08:00
|
|
|
muteMic{other.muteMic},
|
|
|
|
muteVol{other.muteVol},
|
2018-05-05 19:45:17 +08:00
|
|
|
videoSource{other.videoSource},
|
2018-03-16 13:32:54 +08:00
|
|
|
videoInConn{other.videoInConn},
|
|
|
|
videoEnabled{other.videoEnabled},
|
2018-05-05 19:45:17 +08:00
|
|
|
nullVideoBitrate{other.nullVideoBitrate}
|
2015-10-05 08:36:50 +08:00
|
|
|
{
|
2018-03-16 13:32:54 +08:00
|
|
|
Audio& audio = Audio::getInstance();
|
|
|
|
audio.subscribeInput();
|
2016-01-22 03:17:18 +08:00
|
|
|
other.audioInConn = QMetaObject::Connection();
|
2018-03-16 13:32:54 +08:00
|
|
|
other.videoInConn = QMetaObject::Connection();
|
|
|
|
other.videoEnabled = false; // we don't need to subscribe video because other won't unsubscribe
|
|
|
|
other.videoSource = nullptr;
|
|
|
|
other.av = nullptr;
|
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
|
|
|
|
2018-03-16 13:32:54 +08:00
|
|
|
QObject::disconnect(audioInConn);
|
|
|
|
audio.unsubscribeInput();
|
|
|
|
if (videoEnabled) {
|
|
|
|
QObject::disconnect(videoInConn);
|
|
|
|
CameraSource::getInstance().unsubscribe();
|
2017-10-28 01:35:25 +08:00
|
|
|
}
|
2015-10-05 08:36:50 +08:00
|
|
|
}
|
|
|
|
|
2018-05-05 19:45:17 +08:00
|
|
|
/**
|
|
|
|
* @brief ToxCall move assignement
|
|
|
|
* @param other object moved from
|
|
|
|
* @return object moved to
|
|
|
|
*/
|
2016-07-01 07:23:31 +08:00
|
|
|
ToxCall& ToxCall::operator=(ToxCall&& other) noexcept
|
2015-10-05 08:36:50 +08:00
|
|
|
{
|
2018-03-16 13:32:54 +08:00
|
|
|
QObject::disconnect(audioInConn);
|
2017-11-02 10:58:23 +08:00
|
|
|
|
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;
|
2018-03-16 13:32:54 +08:00
|
|
|
|
|
|
|
Audio::getInstance().subscribeInput();
|
|
|
|
|
|
|
|
videoInConn = other.videoInConn;
|
|
|
|
other.videoInConn = QMetaObject::Connection();
|
|
|
|
videoEnabled = other.videoEnabled;
|
|
|
|
other.videoEnabled = false;
|
|
|
|
nullVideoBitrate = other.nullVideoBitrate;
|
|
|
|
videoSource = other.videoSource;
|
|
|
|
other.videoSource = nullptr;
|
|
|
|
av = other.av;
|
|
|
|
other.av = nullptr;
|
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;
|
|
|
|
}
|
|
|
|
|
2018-03-16 13:32:54 +08:00
|
|
|
bool ToxCall::getVideoEnabled() const
|
2017-10-28 01:35:25 +08:00
|
|
|
{
|
|
|
|
return videoEnabled;
|
|
|
|
}
|
|
|
|
|
2018-03-16 13:32:54 +08:00
|
|
|
void ToxCall::setVideoEnabled(bool value)
|
2017-10-28 01:35:25 +08:00
|
|
|
{
|
|
|
|
videoEnabled = value;
|
|
|
|
}
|
|
|
|
|
2018-03-16 13:32:54 +08:00
|
|
|
bool ToxCall::getNullVideoBitrate() const
|
2017-10-28 01:35:25 +08:00
|
|
|
{
|
|
|
|
return nullVideoBitrate;
|
|
|
|
}
|
|
|
|
|
2018-03-16 13:32:54 +08:00
|
|
|
void ToxCall::setNullVideoBitrate(bool value)
|
2017-10-28 01:35:25 +08:00
|
|
|
{
|
|
|
|
nullVideoBitrate = value;
|
|
|
|
}
|
|
|
|
|
2018-03-16 13:32:54 +08:00
|
|
|
CoreVideoSource* ToxCall::getVideoSource() const
|
2017-10-28 01:35:25 +08:00
|
|
|
{
|
|
|
|
return videoSource;
|
|
|
|
}
|
|
|
|
|
2018-06-30 19:09:29 +08:00
|
|
|
quint32 ToxFriendCall::getAlSource() const
|
2017-10-28 01:35:25 +08:00
|
|
|
{
|
2018-03-16 13:32:54 +08:00
|
|
|
return alSource;
|
2017-10-28 01:35:25 +08:00
|
|
|
}
|
|
|
|
|
2018-06-30 19:09:29 +08:00
|
|
|
void ToxFriendCall::setAlSource(const quint32& value)
|
2017-10-28 01:35:25 +08:00
|
|
|
{
|
2018-03-16 13:32:54 +08:00
|
|
|
alSource = value;
|
2017-10-28 01:35:25 +08:00
|
|
|
}
|
|
|
|
|
2018-03-16 13:32:54 +08:00
|
|
|
ToxFriendCall::ToxFriendCall(uint32_t FriendNum, bool VideoEnabled, CoreAV& av)
|
2018-07-01 16:39:33 +08:00
|
|
|
: ToxCall(VideoEnabled, av)
|
2017-10-28 01:35:25 +08:00
|
|
|
{
|
2018-07-01 16:39:33 +08:00
|
|
|
// register audio
|
2018-06-30 19:09:29 +08:00
|
|
|
Audio& audio = Audio::getInstance();
|
2018-07-01 16:39:33 +08:00
|
|
|
audio.subscribeInput();
|
|
|
|
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 input connection not working";
|
|
|
|
}
|
|
|
|
|
2018-06-30 19:09:29 +08:00
|
|
|
audio.subscribeOutput(alSource);
|
2018-07-01 16:39:33 +08:00
|
|
|
|
|
|
|
// register video
|
|
|
|
if (videoEnabled) {
|
|
|
|
videoSource = new CoreVideoSource();
|
|
|
|
CameraSource& source = CameraSource::getInstance();
|
|
|
|
|
|
|
|
if (source.isNone()) {
|
|
|
|
source.setupDefault();
|
|
|
|
}
|
|
|
|
source.subscribe();
|
|
|
|
videoInConn = QObject::connect(&source, &VideoSource::frameAvailable,
|
|
|
|
[&av, FriendNum](std::shared_ptr<VideoFrame> frame) {
|
|
|
|
av.sendCallVideo(FriendNum, frame);
|
|
|
|
});
|
|
|
|
if (!videoInConn) {
|
|
|
|
qDebug() << "Video connection not working";
|
|
|
|
}
|
|
|
|
}
|
2018-06-30 19:09:29 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
ToxFriendCall::ToxFriendCall(ToxFriendCall &&other) noexcept
|
|
|
|
: ToxCall(std::move(other))
|
|
|
|
, alSource{other.alSource}
|
|
|
|
{
|
|
|
|
other.alSource = 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
ToxFriendCall& ToxFriendCall::operator=(ToxFriendCall &&other) noexcept
|
|
|
|
{
|
|
|
|
ToxCall::operator=(std::move(other));
|
|
|
|
alSource = other.alSource;
|
|
|
|
other.alSource = 0;
|
|
|
|
|
|
|
|
return *this;
|
|
|
|
}
|
|
|
|
|
|
|
|
ToxFriendCall::~ToxFriendCall()
|
|
|
|
{
|
|
|
|
auto& audio = Audio::getInstance();
|
|
|
|
audio.unsubscribeOutput(alSource);
|
2017-10-28 01:35:25 +08:00
|
|
|
}
|
|
|
|
|
2018-03-16 13:32:54 +08:00
|
|
|
void ToxFriendCall::startTimeout(uint32_t callId)
|
2015-10-05 08:36:50 +08:00
|
|
|
{
|
2018-03-16 13:32:54 +08:00
|
|
|
if (!timeoutTimer) {
|
|
|
|
timeoutTimer = std::unique_ptr<QTimer>{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.get(), &QTimer::timeout,
|
|
|
|
[avCopy, callId]() { avCopy->timeoutCall(callId); });
|
2017-04-16 22:14:04 +08:00
|
|
|
}
|
2015-10-05 08:36:50 +08:00
|
|
|
|
2018-03-16 13:32:54 +08:00
|
|
|
if (!timeoutTimer->isActive())
|
|
|
|
timeoutTimer->start(CALL_TIMEOUT);
|
2015-10-05 08:36:50 +08:00
|
|
|
}
|
|
|
|
|
2018-03-16 13:32:54 +08:00
|
|
|
void ToxFriendCall::stopTimeout()
|
2015-10-05 08:36:50 +08:00
|
|
|
{
|
2018-03-16 13:32:54 +08:00
|
|
|
if (!timeoutTimer)
|
|
|
|
return;
|
|
|
|
|
|
|
|
timeoutTimer->stop();
|
|
|
|
timeoutTimer.reset();
|
2015-10-05 08:36:50 +08:00
|
|
|
}
|
|
|
|
|
2018-03-16 13:32:54 +08:00
|
|
|
TOXAV_FRIEND_CALL_STATE ToxFriendCall::getState() const
|
2015-10-05 08:36:50 +08:00
|
|
|
{
|
2018-03-16 13:32:54 +08:00
|
|
|
return state;
|
2015-10-05 08:36:50 +08:00
|
|
|
}
|
|
|
|
|
2018-03-16 13:32:54 +08:00
|
|
|
void ToxFriendCall::setState(const TOXAV_FRIEND_CALL_STATE& value)
|
2015-10-05 08:36:50 +08:00
|
|
|
{
|
2018-03-16 13:32:54 +08:00
|
|
|
state = value;
|
2015-10-05 08:36:50 +08:00
|
|
|
}
|
|
|
|
|
2017-02-26 19:52:45 +08:00
|
|
|
ToxGroupCall::ToxGroupCall(int GroupNum, CoreAV& av)
|
2018-07-01 16:39:33 +08:00
|
|
|
: ToxCall(false, av)
|
2015-10-05 08:36:50 +08:00
|
|
|
{
|
2018-07-01 16:39:33 +08:00
|
|
|
// register audio
|
|
|
|
Audio& audio = Audio::getInstance();
|
|
|
|
audio.subscribeInput();
|
|
|
|
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);
|
|
|
|
});
|
|
|
|
|
|
|
|
if (!audioInConn) {
|
|
|
|
qDebug() << "Audio input connection not working";
|
|
|
|
}
|
2015-10-05 08:36:50 +08:00
|
|
|
}
|
|
|
|
|
2018-06-30 19:09:29 +08:00
|
|
|
ToxGroupCall::ToxGroupCall(ToxGroupCall&& other) noexcept
|
|
|
|
: ToxCall(std::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
|
|
|
{
|
2018-03-16 13:32:54 +08:00
|
|
|
ToxCall::operator=(std::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)
|
|
|
|
{
|
2018-07-01 16:16:21 +08:00
|
|
|
auto& audio = Audio::getInstance();
|
|
|
|
const auto& sourceId = peers.find(peerId);
|
|
|
|
if(sourceId == peers.constEnd()) {
|
|
|
|
qDebug() << "Peer:" << peerId << "does not have a source, can't remove";
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
audio.unsubscribeOutput(sourceId.value());
|
2017-10-28 01:35:25 +08:00
|
|
|
peers.remove(peerId);
|
|
|
|
}
|
|
|
|
|
2018-07-01 16:16:21 +08:00
|
|
|
void ToxGroupCall::addPeer(int peerId)
|
|
|
|
{
|
|
|
|
auto& audio = Audio::getInstance();
|
|
|
|
uint sourceId = 0;
|
|
|
|
audio.subscribeOutput(sourceId);
|
|
|
|
peers.insert(peerId, sourceId);
|
|
|
|
}
|
|
|
|
|
|
|
|
bool ToxGroupCall::havePeer(int peerId)
|
|
|
|
{
|
|
|
|
const auto& sourceId = peers.find(peerId);
|
|
|
|
return sourceId != peers.constEnd();
|
|
|
|
}
|
|
|
|
|
|
|
|
void ToxGroupCall::clearPeers()
|
2017-10-28 01:35:25 +08:00
|
|
|
{
|
2018-07-01 16:16:21 +08:00
|
|
|
Audio& audio = Audio::getInstance();
|
|
|
|
|
|
|
|
for (quint32 v : peers) {
|
|
|
|
audio.unsubscribeOutput(v);
|
|
|
|
}
|
|
|
|
|
|
|
|
peers.clear();
|
|
|
|
}
|
|
|
|
|
|
|
|
quint32 ToxGroupCall::getAlSource(int peer)
|
|
|
|
{
|
|
|
|
if(!havePeer(peer)) {
|
|
|
|
qWarning() << "Getting alSource for non-existant peer";
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
return peers[peer];
|
2017-10-28 01:35:25 +08:00
|
|
|
}
|