Browse Source

refactor(coreav): move group call control to ToxCall

reviewable/pr6235/r16
sudden6 5 years ago committed by Anthony Bilinski
parent
commit
b200611a47
No known key found for this signature in database
GPG Key ID: 2AA8E0DA1B31FB3C
  1. 89
      src/core/coreav.cpp
  2. 10
      src/core/coreav.h
  3. 58
      src/widget/form/groupchatform.cpp
  4. 4
      src/widget/form/groupchatform.h
  5. 7
      src/widget/widget.cpp

89
src/core/coreav.cpp

@ -427,28 +427,12 @@ void CoreAV::groupCallCallback(void* tox, uint32_t group, uint32_t peer, const i @@ -427,28 +427,12 @@ void CoreAV::groupCallCallback(void* tox, uint32_t group, uint32_t peer, const i
call.playAudioBuffer(peerPk, data, samples, channels, sample_rate);
}
/**
* @brief Called from core to make sure the source for that peer is invalidated when they leave.
* @param group Group Index
* @param peer Peer Index
*/
void CoreAV::invalidateGroupCallPeerSource(const Group& group, ToxPk peerPk)
{
QWriteLocker locker{&callsLock};
auto it = groupCalls.find(group.getId());
if (it == groupCalls.end()) {
return;
}
it->second->removePeer(peerPk);
}
/**
* @brief Starts a call in an existing AV groupchat.
* @note Call from the GUI thread.
* @param groupId Id of group to join
*/
void CoreAV::joinGroupCall(const Group& group)
CoreAV::ToxGroupCallPtr CoreAV::joinGroupCall(const Group& group)
{
QWriteLocker locker{&callsLock};
@ -461,12 +445,13 @@ void CoreAV::joinGroupCall(const Group& group) @@ -461,12 +445,13 @@ void CoreAV::joinGroupCall(const Group& group)
// Call Objects must be owned by CoreAV or there will be locking problems with Audio
groupcall->moveToThread(this->thread());
assert(groupcall != nullptr);
auto ret = groupCalls.emplace(group.getId(), std::move(groupcall));
auto ret = groupCalls.emplace(group.getId(), groupcall);
if (ret.second == false) {
qWarning() << "This group call already exists, not joining!";
return;
return {};
}
ret.first->second->setActive(true);
return groupcall;
}
/**
@ -503,72 +488,6 @@ bool CoreAV::sendGroupCallAudio(int groupId, const int16_t* pcm, size_t samples, @@ -503,72 +488,6 @@ bool CoreAV::sendGroupCallAudio(int groupId, const int16_t* pcm, size_t samples,
return true;
}
/**
* @brief Mutes or unmutes the group call's input (microphone).
* @param g The group
* @param mute True to mute, false to unmute
*/
void CoreAV::muteCallInput(const Group* g, bool mute)
{
QWriteLocker locker{&callsLock};
auto it = groupCalls.find(g->getId());
if (g && (it != groupCalls.end())) {
it->second->setMuteMic(mute);
}
}
/**
* @brief Mutes or unmutes the group call's output (speaker).
* @param g The group
* @param mute True to mute, false to unmute
*/
void CoreAV::muteCallOutput(const Group* g, bool mute)
{
QWriteLocker locker{&callsLock};
auto it = groupCalls.find(g->getId());
if (g && (it != groupCalls.end())) {
it->second->setMuteVol(mute);
}
}
/**
* @brief Returns the group calls input (microphone) state.
* @param groupId The group id to check
* @return true when muted, false otherwise
*/
bool CoreAV::isGroupCallInputMuted(const Group* g) const
{
QReadLocker locker{&callsLock};
if (!g) {
return false;
}
const uint32_t groupId = g->getId();
auto it = groupCalls.find(groupId);
return (it != groupCalls.end()) && it->second->getMuteMic();
}
/**
* @brief Returns the group calls output (speaker) state.
* @param groupId The group id to check
* @return true when muted, false otherwise
*/
bool CoreAV::isGroupCallOutputMuted(const Group* g) const
{
QReadLocker locker{&callsLock};
if (!g) {
return false;
}
const uint32_t groupId = g->getId();
auto it = groupCalls.find(groupId);
return (it != groupCalls.end()) && it->second->getMuteVol();
}
/**
* @brief Signal to all peers that we're not sending video anymore.
* @note The next frame sent cancels this.

10
src/core/coreav.h

@ -51,6 +51,7 @@ class CoreAV : public QObject @@ -51,6 +51,7 @@ class CoreAV : public QObject
public:
using CoreAVPtr = std::unique_ptr<CoreAV>;
using ToxFriendCallPtr = std::shared_ptr<ToxFriendCall>;
using ToxGroupCallPtr = std::shared_ptr<ToxGroupCall>;
static CoreAVPtr makeCoreAV(Tox* core, CompatibleRecursiveMutex& toxCoreLock,
IAudioSettings& audioSettings, IGroupSettings& groupSettings);
@ -69,17 +70,12 @@ public: @@ -69,17 +70,12 @@ public:
void sendNoVideo();
void joinGroupCall(const Group& group);
ToxGroupCallPtr joinGroupCall(const Group& group);
void leaveGroupCall(int groupNum);
void muteCallInput(const Group* g, bool mute);
void muteCallOutput(const Group* g, bool mute);
bool isGroupCallInputMuted(const Group* g) const;
bool isGroupCallOutputMuted(const Group* g) const;
static void groupCallCallback(void* tox, uint32_t group, uint32_t peer, const int16_t* data,
unsigned samples, uint8_t channels, uint32_t sample_rate,
void* core);
void invalidateGroupCallPeerSource(const Group& group, ToxPk peerPk);
public slots:
ToxFriendCallPtr startCall(uint32_t friendNum, bool video);
@ -136,8 +132,6 @@ private: @@ -136,8 +132,6 @@ private:
*/
std::map<uint32_t, ToxFriendCallPtr> calls;
using ToxGroupCallPtr = std::shared_ptr<ToxGroupCall>;
/**
* @brief Maps group IDs to ToxGroupCalls.
* @note Need to use STL container here, because Qt containers need a copy constructor.

58
src/widget/form/groupchatform.cpp

@ -38,6 +38,7 @@ @@ -38,6 +38,7 @@
#include "src/widget/translator.h"
#include "src/persistence/igroupsettings.h"
#include <QDebug>
#include <QDragEnterEvent>
#include <QMimeData>
#include <QRegularExpression>
@ -86,7 +87,6 @@ GroupChatForm::GroupChatForm(Core& _core, Group* chatGroup, IChatLog& chatLog, I @@ -86,7 +87,6 @@ GroupChatForm::GroupChatForm(Core& _core, Group* chatGroup, IChatLog& chatLog, I
: GenericChatForm(_core, chatGroup, chatLog, messageDispatcher)
, core{_core}
, group(chatGroup)
, inCall(false)
, settings(_settings)
{
nusersLabel = new QLabel();
@ -243,6 +243,9 @@ void GroupChatForm::onUserLeft(const ToxPk& user, const QString& name) @@ -243,6 +243,9 @@ void GroupChatForm::onUserLeft(const ToxPk& user, const QString& name)
addSystemInfoMessage(QDateTime::currentDateTime(), SystemMessageType::userLeftGroup, {name});
}
updateUserNames();
if (call) {
call->removePeer(user);
}
}
void GroupChatForm::onPeerNameChanged(const ToxPk& peer, const QString& oldName, const QString& newName)
@ -308,48 +311,38 @@ void GroupChatForm::dropEvent(QDropEvent* ev) @@ -308,48 +311,38 @@ void GroupChatForm::dropEvent(QDropEvent* ev)
void GroupChatForm::onMicMuteToggle()
{
if (audioInputFlag) {
CoreAV* av = core.getAv();
const bool oldMuteState = av->isGroupCallInputMuted(group);
const bool newMute = !oldMuteState;
av->muteCallInput(group, newMute);
headWidget->updateMuteMicButton(inCall, newMute);
const bool muteState = call->getMuteMic();
call->setMuteMic(!muteState);
headWidget->updateMuteMicButton(true, !muteState);
}
}
void GroupChatForm::onVolMuteToggle()
{
if (audioOutputFlag) {
CoreAV* av = core.getAv();
const bool oldMuteState = av->isGroupCallOutputMuted(group);
const bool newMute = !oldMuteState;
av->muteCallOutput(group, newMute);
headWidget->updateMuteVolButton(inCall, newMute);
const bool muteState = call->getMuteVol();
call->setMuteVol(!muteState);
headWidget->updateMuteVolButton(true, !muteState);
}
}
void GroupChatForm::onCallClicked()
{
CoreAV* av = core.getAv();
if (!inCall) {
joinGroupCall();
} else {
if (call) {
leaveGroupCall();
} else {
joinGroupCall();
}
headWidget->updateCallButtons(true, inCall);
const bool inMute = av->isGroupCallInputMuted(group);
headWidget->updateMuteMicButton(inCall, inMute);
const bool outMute = av->isGroupCallOutputMuted(group);
headWidget->updateMuteVolButton(inCall, outMute);
headWidget->updateCallButtons(true, call != nullptr);
headWidget->updateMuteMicButton(call != nullptr, call->getMuteMic());
headWidget->updateMuteVolButton(call != nullptr, call->getMuteVol());
}
void GroupChatForm::keyPressEvent(QKeyEvent* ev)
{
// Push to talk (CTRL+P)
if (ev->key() == Qt::Key_P && (ev->modifiers() & Qt::ControlModifier) && inCall) {
if (ev->key() == Qt::Key_P && (ev->modifiers() & Qt::ControlModifier) && (call != nullptr)) {
onMicMuteToggle();
}
@ -360,7 +353,7 @@ void GroupChatForm::keyPressEvent(QKeyEvent* ev) @@ -360,7 +353,7 @@ void GroupChatForm::keyPressEvent(QKeyEvent* ev)
void GroupChatForm::keyReleaseEvent(QKeyEvent* ev)
{
// Push to talk (CTRL+P)
if (ev->key() == Qt::Key_P && (ev->modifiers() & Qt::ControlModifier) && inCall) {
if (ev->key() == Qt::Key_P && (ev->modifiers() & Qt::ControlModifier) && (call != nullptr)) {
onMicMuteToggle();
}
@ -374,7 +367,7 @@ void GroupChatForm::keyReleaseEvent(QKeyEvent* ev) @@ -374,7 +367,7 @@ void GroupChatForm::keyReleaseEvent(QKeyEvent* ev)
void GroupChatForm::updateUserCount(int numPeers)
{
nusersLabel->setText(tr("%n user(s) in chat", "Number of users in chat", numPeers));
headWidget->updateCallButtons(true, inCall);
headWidget->updateCallButtons(true, call != nullptr);
}
void GroupChatForm::retranslateUi()
@ -441,17 +434,20 @@ void GroupChatForm::onLabelContextMenuRequested(const QPoint& localPos) @@ -441,17 +434,20 @@ void GroupChatForm::onLabelContextMenuRequested(const QPoint& localPos)
void GroupChatForm::joinGroupCall()
{
CoreAV* av = core.getAv();
av->joinGroupCall(*group);
call = av->joinGroupCall(*group);
if (!call) {
qDebug() << "Failed to join group call";
return;
}
audioInputFlag = true;
audioOutputFlag = true;
inCall = true;
}
void GroupChatForm::leaveGroupCall()
{
CoreAV* av = core.getAv();
av->leaveGroupCall(group->getId());
call->endCall();
call.reset();
audioInputFlag = false;
audioOutputFlag = false;
inCall = false;
}

4
src/widget/form/groupchatform.h

@ -21,6 +21,8 @@ @@ -21,6 +21,8 @@
#include "genericchatform.h"
#include "src/core/toxpk.h"
#include "src/core/toxcall.h"
#include "src/core/toxpk.h"
#include <QMap>
namespace Ui {
@ -79,6 +81,6 @@ private: @@ -79,6 +81,6 @@ private:
FlowLayout* namesListLayout;
QLabel* nusersLabel;
TabCompleter* tabber;
bool inCall;
IGroupSettings& settings;
std::shared_ptr<ToxGroupCall> call;
};

7
src/widget/widget.cpp

@ -2113,13 +2113,6 @@ Group* Widget::createGroup(uint32_t groupnumber, const GroupId& groupId) @@ -2113,13 +2113,6 @@ Group* Widget::createGroup(uint32_t groupnumber, const GroupId& groupId)
GroupList::addGroup(*core, groupnumber, groupId, groupName, enabled, core->getUsername());
assert(newgroup);
if (enabled) {
connect(newgroup, &Group::userLeft, [=](const ToxPk& user){
CoreAV *av = core->getAv();
assert(av);
av->invalidateGroupCallPeerSource(*newgroup, user);
});
}
auto dialogManager = ContentDialogManager::getInstance();
auto rawChatroom = new GroupChatroom(newgroup, dialogManager, *core);
std::shared_ptr<GroupChatroom> chatroom(rawChatroom);

Loading…
Cancel
Save