2019-06-24 22:01:18 +08:00
|
|
|
/*
|
|
|
|
Copyright © 2019 by The qTox Project Contributors
|
|
|
|
|
|
|
|
This file is part of qTox, a Qt-based graphical interface for Tox.
|
|
|
|
|
|
|
|
qTox is libre software: you can redistribute it and/or modify
|
|
|
|
it under the terms of the GNU General Public License as published by
|
|
|
|
the Free Software Foundation, either version 3 of the License, or
|
|
|
|
(at your option) any later version.
|
|
|
|
|
|
|
|
qTox is distributed in the hope that it will be useful,
|
|
|
|
but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
|
|
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
|
|
|
GNU General Public License for more details.
|
|
|
|
|
|
|
|
You should have received a copy of the GNU General Public License
|
|
|
|
along with qTox. If not, see <http://www.gnu.org/licenses/>.
|
|
|
|
*/
|
|
|
|
|
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
|
|
|
|
2019-05-19 01:08:58 +08:00
|
|
|
ToxCall::ToxCall(bool VideoEnabled, CoreAV& av, IAudioControl& audio)
|
2018-06-07 03:43:46 +08:00
|
|
|
: av{&av}
|
2019-05-19 01:08:58 +08:00
|
|
|
, audio(audio)
|
2018-06-07 03:43:46 +08:00
|
|
|
, videoEnabled{VideoEnabled}
|
2019-05-19 01:08:58 +08:00
|
|
|
, audioSource(audio.makeSource())
|
2019-04-24 05:05:49 +08:00
|
|
|
{}
|
2015-10-05 08:36:50 +08:00
|
|
|
|
|
|
|
ToxCall::~ToxCall()
|
|
|
|
{
|
2018-03-16 13:32:54 +08:00
|
|
|
QObject::disconnect(audioInConn);
|
2019-05-02 03:45:18 +08:00
|
|
|
QObject::disconnect(audioSrcInvalid);
|
|
|
|
|
2018-03-16 13:32:54 +08:00
|
|
|
if (videoEnabled) {
|
|
|
|
QObject::disconnect(videoInConn);
|
|
|
|
CameraSource::getInstance().unsubscribe();
|
2017-10-28 01:35:25 +08:00
|
|
|
}
|
2015-10-05 08:36:50 +08:00
|
|
|
}
|
|
|
|
|
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;
|
|
|
|
}
|
|
|
|
|
2019-05-19 01:08:58 +08:00
|
|
|
ToxFriendCall::ToxFriendCall(uint32_t FriendNum, bool VideoEnabled, CoreAV& av, IAudioControl& audio)
|
|
|
|
: ToxCall(VideoEnabled, av, audio)
|
|
|
|
, sink(audio.makeSink())
|
2019-04-05 07:29:21 +08:00
|
|
|
, friendId{FriendNum}
|
2017-10-28 01:35:25 +08:00
|
|
|
{
|
2019-05-02 03:45:18 +08:00
|
|
|
// TODO(sudden6): move this to audio source
|
|
|
|
audioInConn =
|
|
|
|
QObject::connect(audioSource.get(), &IAudioSource::frameAvailable,
|
|
|
|
[this](const int16_t* pcm, size_t samples, uint8_t chans, uint32_t rate) {
|
|
|
|
this->av->sendCallAudio(this->friendId, pcm, samples, chans, rate);
|
|
|
|
});
|
|
|
|
|
|
|
|
audioSrcInvalid = QObject::connect(audioSource.get(), &IAudioSource::invalidated,
|
|
|
|
[this]() { this->onAudioSourceInvalidated(); });
|
2018-07-01 16:39:33 +08:00
|
|
|
|
|
|
|
if (!audioInConn) {
|
2019-04-24 05:05:49 +08:00
|
|
|
qDebug() << "Audio input connection not working";
|
2018-07-01 16:39:33 +08:00
|
|
|
}
|
|
|
|
|
2019-04-05 07:29:21 +08:00
|
|
|
audioSinkInvalid = QObject::connect(sink.get(), &IAudioSink::invalidated,
|
2019-04-24 05:05:49 +08:00
|
|
|
[this]() { this->onAudioSinkInvalidated(); });
|
2019-04-05 07:29:21 +08:00
|
|
|
|
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()
|
|
|
|
{
|
2019-04-05 07:29:21 +08:00
|
|
|
QObject::disconnect(audioSinkInvalid);
|
|
|
|
}
|
|
|
|
|
2019-05-02 03:45:18 +08:00
|
|
|
void ToxFriendCall::onAudioSourceInvalidated()
|
|
|
|
{
|
2019-05-19 01:08:58 +08:00
|
|
|
auto newSrc = audio.makeSource();
|
2019-05-02 03:45:18 +08:00
|
|
|
audioInConn =
|
|
|
|
QObject::connect(newSrc.get(), &IAudioSource::frameAvailable,
|
|
|
|
[this](const int16_t* pcm, size_t samples, uint8_t chans, uint32_t rate) {
|
|
|
|
this->av->sendCallAudio(this->friendId, pcm, samples, chans, rate);
|
|
|
|
});
|
|
|
|
audioSource = std::move(newSrc);
|
|
|
|
|
|
|
|
audioSrcInvalid = QObject::connect(audioSource.get(), &IAudioSource::invalidated,
|
|
|
|
[this]() { this->onAudioSourceInvalidated(); });
|
|
|
|
}
|
|
|
|
|
2019-04-05 07:29:21 +08:00
|
|
|
void ToxFriendCall::onAudioSinkInvalidated()
|
|
|
|
{
|
2019-05-19 01:08:58 +08:00
|
|
|
auto newSink = audio.makeSink();
|
2019-04-05 07:29:21 +08:00
|
|
|
|
2019-04-26 03:30:02 +08:00
|
|
|
audioSinkInvalid = QObject::connect(newSink.get(), &IAudioSink::invalidated,
|
2019-04-24 05:05:49 +08:00
|
|
|
[this]() { this->onAudioSinkInvalidated(); });
|
2019-04-26 03:30:02 +08:00
|
|
|
sink = std::move(newSink);
|
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
|
|
|
}
|
|
|
|
|
2019-04-26 03:30:02 +08:00
|
|
|
void ToxFriendCall::playAudioBuffer(const int16_t* data, int samples, unsigned channels,
|
2019-05-02 03:45:18 +08:00
|
|
|
int sampleRate) const
|
2019-04-05 07:29:21 +08:00
|
|
|
{
|
2019-04-26 03:30:02 +08:00
|
|
|
if (sink) {
|
|
|
|
sink->playAudioBuffer(data, samples, channels, sampleRate);
|
|
|
|
}
|
2019-04-05 07:29:21 +08:00
|
|
|
}
|
|
|
|
|
2019-05-19 01:08:58 +08:00
|
|
|
ToxGroupCall::ToxGroupCall(int GroupNum, CoreAV& av, IAudioControl& audio)
|
|
|
|
: ToxCall(false, av, audio)
|
2019-04-05 07:29:21 +08:00
|
|
|
, groupId{GroupNum}
|
2015-10-05 08:36:50 +08:00
|
|
|
{
|
2018-07-01 16:39:33 +08:00
|
|
|
// register audio
|
2019-05-02 03:45:18 +08:00
|
|
|
audioInConn =
|
|
|
|
QObject::connect(audioSource.get(), &IAudioSource::frameAvailable,
|
|
|
|
[this](const int16_t* pcm, size_t samples, uint8_t chans, uint32_t rate) {
|
|
|
|
this->av->sendGroupCallAudio(this->groupId, pcm, samples, chans, rate);
|
|
|
|
});
|
2018-07-01 16:39:33 +08:00
|
|
|
|
|
|
|
if (!audioInConn) {
|
2019-04-24 05:05:49 +08:00
|
|
|
qDebug() << "Audio input connection not working";
|
2018-07-01 16:39:33 +08:00
|
|
|
}
|
2019-05-02 03:45:18 +08:00
|
|
|
|
|
|
|
audioSrcInvalid = QObject::connect(audioSource.get(), &IAudioSource::invalidated,
|
|
|
|
[this]() { this->onAudioSourceInvalidated(); });
|
2015-10-05 08:36:50 +08:00
|
|
|
}
|
|
|
|
|
2017-02-06 07:34:41 +08:00
|
|
|
ToxGroupCall::~ToxGroupCall()
|
|
|
|
{
|
2019-04-05 07:29:21 +08:00
|
|
|
// disconnect all Qt connections
|
|
|
|
clearPeers();
|
|
|
|
}
|
2017-02-06 07:34:41 +08:00
|
|
|
|
2019-05-02 03:45:18 +08:00
|
|
|
void ToxGroupCall::onAudioSourceInvalidated()
|
|
|
|
{
|
2019-05-19 01:08:58 +08:00
|
|
|
auto newSrc = audio.makeSource();
|
2019-05-02 03:45:18 +08:00
|
|
|
// TODO(sudden6): move this to audio source
|
|
|
|
audioInConn =
|
|
|
|
QObject::connect(audioSource.get(), &IAudioSource::frameAvailable,
|
|
|
|
[this](const int16_t* pcm, size_t samples, uint8_t chans, uint32_t rate) {
|
|
|
|
this->av->sendGroupCallAudio(this->groupId, pcm, samples, chans, rate);
|
|
|
|
});
|
|
|
|
|
|
|
|
audioSource = std::move(newSrc);
|
|
|
|
|
|
|
|
audioSrcInvalid = QObject::connect(audioSource.get(), &IAudioSource::invalidated,
|
|
|
|
[this]() { this->onAudioSourceInvalidated(); });
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2019-04-05 07:29:21 +08:00
|
|
|
void ToxGroupCall::onAudioSinkInvalidated(ToxPk peerId)
|
|
|
|
{
|
|
|
|
removePeer(peerId);
|
|
|
|
addPeer(peerId);
|
2017-02-06 07:34:41 +08:00
|
|
|
}
|
|
|
|
|
2019-01-24 01:23:54 +08:00
|
|
|
void ToxGroupCall::removePeer(ToxPk peerId)
|
2017-10-28 01:35:25 +08:00
|
|
|
{
|
2019-04-05 07:29:21 +08:00
|
|
|
const auto& source = peers.find(peerId);
|
2019-04-24 05:05:49 +08:00
|
|
|
if (source == peers.cend()) {
|
2019-04-05 07:29:21 +08:00
|
|
|
qDebug() << "Peer:" << peerId.toString() << "does not have a source, can't remove";
|
2018-07-01 16:16:21 +08:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2019-04-05 07:29:21 +08:00
|
|
|
peers.erase(source);
|
|
|
|
QObject::disconnect(sinkInvalid[peerId]);
|
|
|
|
sinkInvalid.erase(peerId);
|
2017-10-28 01:35:25 +08:00
|
|
|
}
|
|
|
|
|
2019-01-24 01:23:54 +08:00
|
|
|
void ToxGroupCall::addPeer(ToxPk peerId)
|
2018-07-01 16:16:21 +08:00
|
|
|
{
|
2019-04-26 03:30:02 +08:00
|
|
|
std::unique_ptr<IAudioSink> newSink = audio.makeSink();
|
|
|
|
peers.emplace(peerId, std::move(newSink));
|
2019-04-05 07:29:21 +08:00
|
|
|
|
2019-04-24 05:05:49 +08:00
|
|
|
QMetaObject::Connection con =
|
2019-04-26 03:30:02 +08:00
|
|
|
QObject::connect(newSink.get(), &IAudioSink::invalidated,
|
2019-04-24 05:05:49 +08:00
|
|
|
[this, peerId]() { this->onAudioSinkInvalidated(peerId); });
|
2019-04-05 07:29:21 +08:00
|
|
|
|
|
|
|
sinkInvalid.insert({peerId, con});
|
2018-07-01 16:16:21 +08:00
|
|
|
}
|
|
|
|
|
2019-01-24 01:23:54 +08:00
|
|
|
bool ToxGroupCall::havePeer(ToxPk peerId)
|
2018-07-01 16:16:21 +08:00
|
|
|
{
|
2019-04-05 07:29:21 +08:00
|
|
|
const auto& source = peers.find(peerId);
|
|
|
|
return source != peers.cend();
|
2018-07-01 16:16:21 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
void ToxGroupCall::clearPeers()
|
2017-10-28 01:35:25 +08:00
|
|
|
{
|
2019-04-05 07:29:21 +08:00
|
|
|
peers.clear();
|
2019-04-24 05:05:49 +08:00
|
|
|
for (auto con : sinkInvalid) {
|
2019-04-05 07:29:21 +08:00
|
|
|
QObject::disconnect(con.second);
|
2018-07-01 16:16:21 +08:00
|
|
|
}
|
|
|
|
|
2019-04-05 07:29:21 +08:00
|
|
|
sinkInvalid.clear();
|
2018-07-01 16:16:21 +08:00
|
|
|
}
|
|
|
|
|
2019-04-26 03:30:02 +08:00
|
|
|
void ToxGroupCall::playAudioBuffer(const ToxPk& peer, const int16_t* data, int samples,
|
|
|
|
unsigned channels, int sampleRate)
|
2018-07-01 16:16:21 +08:00
|
|
|
{
|
2019-04-24 05:05:49 +08:00
|
|
|
if (!havePeer(peer)) {
|
2019-04-05 07:29:21 +08:00
|
|
|
addPeer(peer);
|
2018-07-01 16:16:21 +08:00
|
|
|
}
|
2019-04-05 07:29:21 +08:00
|
|
|
const auto& source = peers.find(peer);
|
2019-04-26 03:30:02 +08:00
|
|
|
if (source->second) {
|
|
|
|
source->second->playAudioBuffer(data, samples, channels, sampleRate);
|
|
|
|
}
|
2017-10-28 01:35:25 +08:00
|
|
|
}
|