2021-03-11 13:30:45 -05:00
|
|
|
/*
|
2020-08-03 13:27:42 -04:00
|
|
|
* Copyright (C) 2020 by Savoir-faire Linux
|
|
|
|
* Author : Edric Ladent Milaret<edric.ladent - milaret @savoirfairelinux.com>
|
|
|
|
* Author : Andreas Traczyk<andreas.traczyk @savoirfairelinux.com>
|
|
|
|
* Author: Mingrui Zhang <mingrui.zhang@savoirfairelinux.com>
|
|
|
|
*
|
|
|
|
* This program is free 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.
|
|
|
|
*
|
|
|
|
* This program 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 this program. If not, see <http://www.gnu.org/licenses/>.
|
|
|
|
*/
|
|
|
|
|
|
|
|
#include "avadapter.h"
|
2020-11-03 17:28:25 -05:00
|
|
|
#include "qtutils.h"
|
2020-08-03 13:27:42 -04:00
|
|
|
|
2021-08-26 14:57:24 -04:00
|
|
|
#include "api/newcodecmodel.h"
|
|
|
|
#include "api/newdevicemodel.h"
|
|
|
|
|
2020-11-26 15:05:01 +01:00
|
|
|
#ifdef Q_OS_LINUX
|
|
|
|
#include "xrectsel.h"
|
|
|
|
#endif
|
|
|
|
|
2020-11-03 17:28:25 -05:00
|
|
|
#include <QtConcurrent/QtConcurrent>
|
2020-09-04 14:51:39 -04:00
|
|
|
#include <QApplication>
|
2020-11-26 15:05:01 +01:00
|
|
|
#include <QPainter>
|
2020-08-03 13:27:42 -04:00
|
|
|
#include <QScreen>
|
|
|
|
|
2021-03-30 14:22:39 -04:00
|
|
|
AvAdapter::AvAdapter(LRCInstance* instance, QObject* parent)
|
|
|
|
: QmlAdapterBase(instance, parent)
|
2020-11-03 17:28:25 -05:00
|
|
|
{
|
2021-05-26 17:58:50 -04:00
|
|
|
connect(lrcInstance_->renderer(), &RenderManager::previewFrameStarted, [this]() {
|
|
|
|
// TODO: listen to the correct signals that are needed to be added in daemon or lrc
|
2021-08-31 10:37:11 -04:00
|
|
|
auto callId = lrcInstance_->getCurrentCallId();
|
2021-05-26 17:58:50 -04:00
|
|
|
if (!callId.isEmpty())
|
|
|
|
set_currentRenderingDeviceType(
|
|
|
|
lrcInstance_->avModel().getCurrentRenderedDevice(callId).type);
|
|
|
|
});
|
2020-08-03 13:27:42 -04:00
|
|
|
|
2021-08-31 10:37:11 -04:00
|
|
|
connect(&lrcInstance_->avModel(),
|
|
|
|
&lrc::api::AVModel::audioDeviceEvent,
|
|
|
|
this,
|
|
|
|
&AvAdapter::onAudioDeviceEvent);
|
2020-08-03 13:27:42 -04:00
|
|
|
}
|
|
|
|
|
2021-05-03 15:16:03 -04:00
|
|
|
// The top left corner of primary screen is (0, 0).
|
|
|
|
// For Qt, QScreen geometry contains x, y location relative to primary screen.
|
|
|
|
// The purpose of the function is to use calculate a boundingRect for virtual desktop
|
|
|
|
// to help screen sharing.
|
|
|
|
const QRect
|
|
|
|
AvAdapter::getAllScreensBoundingRect()
|
|
|
|
{
|
|
|
|
auto screens = QGuiApplication::screens();
|
|
|
|
|
|
|
|
// p0 is for x axis, p1 is for y axis,
|
|
|
|
// points contain values that are the maximum positive and negative domain value
|
|
|
|
QPoint p0(0, 0), p1(0, 0);
|
|
|
|
|
|
|
|
for (auto scr : screens) {
|
|
|
|
auto devicePixelRatio = scr->devicePixelRatio();
|
|
|
|
auto screenRect = scr->geometry();
|
|
|
|
|
|
|
|
if (screenRect.y() < 0 && p1.y() < abs(screenRect.y()))
|
|
|
|
p1.setY(abs(screenRect.y()));
|
|
|
|
else if (screenRect.y() >= 0
|
|
|
|
&& p1.x() < screenRect.y() + screenRect.height() * devicePixelRatio)
|
|
|
|
p1.setX(screenRect.y() + screenRect.height() * devicePixelRatio);
|
|
|
|
|
|
|
|
if (screenRect.x() < 0 && p0.y() < abs(screenRect.x()))
|
|
|
|
p0.setY(abs(screenRect.x()));
|
|
|
|
else if (screenRect.x() >= 0
|
|
|
|
&& p0.x() < screenRect.x() + screenRect.width() * devicePixelRatio)
|
|
|
|
p0.setX(screenRect.x() + screenRect.width() * devicePixelRatio);
|
|
|
|
}
|
|
|
|
|
|
|
|
return QRect(-p0.y(), -p1.y(), p0.y() + p0.x(), p1.y() + p1.x());
|
|
|
|
}
|
|
|
|
|
2020-08-03 13:27:42 -04:00
|
|
|
void
|
|
|
|
AvAdapter::shareEntireScreen(int screenNumber)
|
|
|
|
{
|
2020-11-26 15:05:01 +01:00
|
|
|
QScreen* screen = QGuiApplication::screens().at(screenNumber);
|
2020-08-03 13:27:42 -04:00
|
|
|
if (!screen)
|
|
|
|
return;
|
2020-11-26 15:05:01 +01:00
|
|
|
QRect rect = screen->geometry();
|
|
|
|
|
2021-10-19 15:24:19 -04:00
|
|
|
auto resource = lrcInstance_->avModel().getDisplay(getScreenNumber(),
|
2021-03-11 13:30:45 -05:00
|
|
|
rect.x(),
|
|
|
|
rect.y(),
|
2021-05-03 15:16:03 -04:00
|
|
|
rect.width() * screen->devicePixelRatio(),
|
2021-10-19 15:24:19 -04:00
|
|
|
rect.height() * screen->devicePixelRatio());
|
|
|
|
auto callId = lrcInstance_->getCurrentCallId();
|
|
|
|
lrcInstance_->getCurrentCallModel()->requestMediaChange(callId,
|
|
|
|
"video_0",
|
|
|
|
resource,
|
|
|
|
lrc::api::NewCallModel::MediaRequestType::SCREENSHARING,
|
|
|
|
false);
|
|
|
|
set_currentRenderingDeviceType(
|
|
|
|
lrcInstance_->avModel().getCurrentRenderedDevice(callId).type);
|
2020-11-26 15:05:01 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
void
|
|
|
|
AvAdapter::shareAllScreens()
|
|
|
|
{
|
2021-05-03 15:16:03 -04:00
|
|
|
const auto arrangementRect = getAllScreensBoundingRect();
|
2020-11-26 15:05:01 +01:00
|
|
|
|
2021-10-19 15:24:19 -04:00
|
|
|
auto resource = lrcInstance_->avModel().getDisplay(getScreenNumber(),
|
|
|
|
arrangementRect.x(),
|
|
|
|
arrangementRect.y(),
|
|
|
|
arrangementRect.width(),
|
|
|
|
arrangementRect.height());
|
|
|
|
auto callId = lrcInstance_->getCurrentCallId();
|
|
|
|
lrcInstance_->getCurrentCallModel()->requestMediaChange(callId,
|
|
|
|
"video_0",
|
|
|
|
resource,
|
|
|
|
lrc::api::NewCallModel::MediaRequestType::SCREENSHARING,
|
|
|
|
false);
|
|
|
|
set_currentRenderingDeviceType(
|
|
|
|
lrcInstance_->avModel().getCurrentRenderedDevice(callId).type);
|
2020-08-03 13:27:42 -04:00
|
|
|
}
|
|
|
|
|
2020-11-26 15:05:01 +01:00
|
|
|
void
|
2020-08-03 13:27:42 -04:00
|
|
|
AvAdapter::captureScreen(int screenNumber)
|
|
|
|
{
|
2020-11-26 15:05:01 +01:00
|
|
|
QtConcurrent::run([this, screenNumber]() {
|
|
|
|
QScreen* screen = QGuiApplication::screens().at(screenNumber);
|
|
|
|
if (!screen)
|
|
|
|
return;
|
|
|
|
/*
|
|
|
|
* The screen window id is always 0.
|
|
|
|
*/
|
|
|
|
auto pixmap = screen->grabWindow(0);
|
2020-08-03 13:27:42 -04:00
|
|
|
|
2020-11-26 15:05:01 +01:00
|
|
|
QBuffer buffer;
|
|
|
|
buffer.open(QIODevice::WriteOnly);
|
|
|
|
pixmap.save(&buffer, "PNG");
|
|
|
|
|
2021-03-29 16:51:53 -04:00
|
|
|
Q_EMIT screenCaptured(screenNumber, Utils::byteArrayToBase64String(buffer.data()));
|
2020-11-26 15:05:01 +01:00
|
|
|
});
|
|
|
|
}
|
|
|
|
|
|
|
|
void
|
|
|
|
AvAdapter::captureAllScreens()
|
|
|
|
{
|
|
|
|
QtConcurrent::run([this]() {
|
|
|
|
auto screens = QGuiApplication::screens();
|
|
|
|
|
|
|
|
QList<QPixmap> scrs;
|
|
|
|
int width = 0, height = 0, currentPoint = 0;
|
|
|
|
|
2021-05-03 15:16:03 -04:00
|
|
|
for (auto scr : screens) {
|
2020-11-26 15:05:01 +01:00
|
|
|
QPixmap pix = scr->grabWindow(0);
|
2021-05-03 15:16:03 -04:00
|
|
|
auto devicePixelRatio = scr->devicePixelRatio();
|
|
|
|
width += scr->geometry().width() * devicePixelRatio;
|
|
|
|
if (height < scr->geometry().height() * devicePixelRatio)
|
|
|
|
height = scr->geometry().height() * devicePixelRatio;
|
2020-11-26 15:05:01 +01:00
|
|
|
scrs << pix;
|
|
|
|
}
|
|
|
|
|
|
|
|
QPixmap final(width, height);
|
|
|
|
QPainter painter(&final);
|
|
|
|
final.fill(Qt::black);
|
|
|
|
|
2021-05-03 15:16:03 -04:00
|
|
|
for (auto scr : scrs) {
|
|
|
|
painter.drawPixmap(currentPoint, 0, scr.width(), scr.height(), scr);
|
2020-11-26 15:05:01 +01:00
|
|
|
currentPoint += scr.width();
|
|
|
|
}
|
|
|
|
|
|
|
|
QBuffer buffer;
|
|
|
|
buffer.open(QIODevice::WriteOnly);
|
|
|
|
final.save(&buffer, "PNG");
|
2021-03-29 16:51:53 -04:00
|
|
|
Q_EMIT screenCaptured(-1, Utils::byteArrayToBase64String(buffer.data()));
|
2020-11-26 15:05:01 +01:00
|
|
|
});
|
2020-08-03 13:27:42 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
void
|
2020-09-10 17:40:51 -04:00
|
|
|
AvAdapter::shareFile(const QString& filePath)
|
2020-08-03 13:27:42 -04:00
|
|
|
{
|
2021-10-19 15:24:19 -04:00
|
|
|
auto callId = lrcInstance_->getCurrentCallId();
|
|
|
|
if (!callId.isEmpty()) {
|
|
|
|
lrcInstance_->getCurrentCallModel()
|
|
|
|
->requestMediaChange(callId,
|
|
|
|
"video_0",
|
|
|
|
filePath,
|
|
|
|
lrc::api::NewCallModel::MediaRequestType::FILESHARING,
|
|
|
|
false);
|
|
|
|
set_currentRenderingDeviceType(
|
|
|
|
lrcInstance_->avModel().getCurrentRenderedDevice(callId).type);
|
|
|
|
}
|
2020-08-03 13:27:42 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
void
|
2020-11-26 15:05:01 +01:00
|
|
|
AvAdapter::shareScreenArea(unsigned x, unsigned y, unsigned width, unsigned height)
|
2020-08-03 13:27:42 -04:00
|
|
|
{
|
2020-11-26 15:05:01 +01:00
|
|
|
#ifdef Q_OS_LINUX
|
|
|
|
// xrectsel will freeze all displays too fast so that the call
|
|
|
|
// context menu will not be closed even closed signal is emitted
|
|
|
|
// use timer to wait until popup is closed
|
|
|
|
QTimer::singleShot(100, [=]() mutable {
|
|
|
|
x = y = width = height = 0;
|
|
|
|
xrectsel(&x, &y, &width, &height);
|
2021-10-19 15:24:19 -04:00
|
|
|
auto resource = lrcInstance_->avModel().getDisplay(getScreenNumber(),
|
|
|
|
x,
|
|
|
|
y,
|
|
|
|
width < 128 ? 128 : width,
|
|
|
|
height < 128 ? 128 : height);
|
|
|
|
auto callId = lrcInstance_->getCurrentCallId();
|
|
|
|
lrcInstance_->getCurrentCallModel()->requestMediaChange(callId,
|
|
|
|
"video_0",
|
|
|
|
resource,
|
|
|
|
lrc::api::NewCallModel::MediaRequestType::SCREENSHARING,
|
|
|
|
false);
|
|
|
|
set_currentRenderingDeviceType(
|
|
|
|
lrcInstance_->avModel().getCurrentRenderedDevice(callId).type);
|
2020-11-26 15:05:01 +01:00
|
|
|
});
|
|
|
|
#else
|
2021-10-19 15:24:19 -04:00
|
|
|
auto resource = lrcInstance_->avModel().getDisplay(getScreenNumber(),
|
|
|
|
x,
|
|
|
|
y,
|
|
|
|
width < 128 ? 128 : width,
|
|
|
|
height < 128 ? 128 : height);
|
|
|
|
auto callId = lrcInstance_->getCurrentCallId();
|
|
|
|
lrcInstance_->getCurrentCallModel()->requestMediaChange(callId,
|
|
|
|
"video_0",
|
|
|
|
resource,
|
|
|
|
lrc::api::NewCallModel::MediaRequestType::SCREENSHARING,
|
|
|
|
false);
|
|
|
|
set_currentRenderingDeviceType(
|
|
|
|
lrcInstance_->avModel().getCurrentRenderedDevice(callId).type);
|
2020-11-26 15:05:01 +01:00
|
|
|
#endif
|
2020-08-18 17:21:28 +02:00
|
|
|
}
|
2020-09-30 14:54:25 -04:00
|
|
|
|
2021-05-26 17:58:50 -04:00
|
|
|
void
|
2021-05-31 14:04:38 -04:00
|
|
|
AvAdapter::stopSharing()
|
2021-05-26 17:58:50 -04:00
|
|
|
{
|
2021-08-31 10:37:11 -04:00
|
|
|
auto callId = lrcInstance_->getCurrentCallId();
|
2021-10-19 15:24:19 -04:00
|
|
|
if (!callId.isEmpty()) {
|
|
|
|
lrcInstance_->getCurrentCallModel()
|
|
|
|
->requestMediaChange(callId,
|
|
|
|
"video_0",
|
|
|
|
lrcInstance_->avModel().getCurrentVideoCaptureDevice(),
|
|
|
|
lrc::api::NewCallModel::MediaRequestType::CAMERA,
|
|
|
|
false);
|
2021-05-26 17:58:50 -04:00
|
|
|
lrcInstance_->avModel().switchInputTo(lrcInstance_->avModel().getCurrentVideoCaptureDevice(),
|
|
|
|
callId);
|
2021-10-19 15:24:19 -04:00
|
|
|
set_currentRenderingDeviceType(
|
|
|
|
lrcInstance_->avModel().getCurrentRenderedDevice(callId).type);
|
|
|
|
}
|
2021-05-26 17:58:50 -04:00
|
|
|
}
|
|
|
|
|
2020-09-30 14:54:25 -04:00
|
|
|
void
|
2021-09-13 11:00:18 -04:00
|
|
|
AvAdapter::startAudioMeter()
|
2020-09-30 14:54:25 -04:00
|
|
|
{
|
2021-09-13 11:00:18 -04:00
|
|
|
lrcInstance_->startAudioMeter();
|
2020-09-30 14:54:25 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
void
|
2021-09-13 11:00:18 -04:00
|
|
|
AvAdapter::stopAudioMeter()
|
2020-09-30 14:54:25 -04:00
|
|
|
{
|
2021-09-13 11:00:18 -04:00
|
|
|
lrcInstance_->stopAudioMeter();
|
2020-09-30 14:54:25 -04:00
|
|
|
}
|
2020-11-03 17:28:25 -05:00
|
|
|
|
2021-06-02 12:11:49 -04:00
|
|
|
void
|
|
|
|
AvAdapter::onAudioDeviceEvent()
|
2020-11-26 15:05:01 +01:00
|
|
|
{
|
2021-06-02 12:11:49 -04:00
|
|
|
auto& avModel = lrcInstance_->avModel();
|
|
|
|
auto inputs = avModel.getAudioInputDevices().size();
|
|
|
|
auto outputs = avModel.getAudioOutputDevices().size();
|
|
|
|
Q_EMIT audioDeviceListChanged(inputs, outputs);
|
2020-11-26 15:05:01 +01:00
|
|
|
}
|
|
|
|
|
2021-01-06 17:23:28 -05:00
|
|
|
int
|
|
|
|
AvAdapter::getScreenNumber() const
|
|
|
|
{
|
|
|
|
int display = 0;
|
|
|
|
|
|
|
|
#ifdef Q_OS_LINUX
|
|
|
|
// Get display
|
|
|
|
QString display_env {getenv("DISPLAY")};
|
|
|
|
if (!display_env.isEmpty()) {
|
2021-03-31 14:10:29 -04:00
|
|
|
auto list = display_env.split(':', QString::SplitBehavior::SkipEmptyParts);
|
2021-01-06 17:23:28 -05:00
|
|
|
// Should only be one display, so get the first one
|
|
|
|
if (list.size() > 0) {
|
|
|
|
display = list.at(0).toInt();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
#endif
|
|
|
|
return display;
|
|
|
|
}
|
2021-08-26 14:57:24 -04:00
|
|
|
|
|
|
|
void
|
|
|
|
AvAdapter::setDeviceName(const QString& deviceName)
|
|
|
|
{
|
|
|
|
lrcInstance_->getCurrentAccountInfo().deviceModel->setCurrentDeviceName(deviceName);
|
|
|
|
}
|
|
|
|
|
|
|
|
void
|
2021-08-31 10:37:11 -04:00
|
|
|
AvAdapter::enableCodec(unsigned int id, bool isToEnable)
|
2021-08-26 14:57:24 -04:00
|
|
|
{
|
2021-08-31 10:37:11 -04:00
|
|
|
lrcInstance_->getCurrentAccountInfo().codecModel->enable(id, isToEnable);
|
2021-08-26 14:57:24 -04:00
|
|
|
}
|
|
|
|
|
2021-08-31 10:37:11 -04:00
|
|
|
void
|
|
|
|
AvAdapter::increaseCodecPriority(unsigned int id, bool isVideo)
|
2021-08-26 14:57:24 -04:00
|
|
|
{
|
2021-08-31 10:37:11 -04:00
|
|
|
lrcInstance_->getCurrentAccountInfo().codecModel->increasePriority(id, isVideo);
|
2021-08-26 14:57:24 -04:00
|
|
|
}
|
|
|
|
|
2021-08-31 10:37:11 -04:00
|
|
|
void
|
|
|
|
AvAdapter::decreaseCodecPriority(unsigned int id, bool isVideo)
|
2021-08-26 14:57:24 -04:00
|
|
|
{
|
2021-08-31 10:37:11 -04:00
|
|
|
lrcInstance_->getCurrentAccountInfo().codecModel->decreasePriority(id, isVideo);
|
2021-08-26 14:57:24 -04:00
|
|
|
}
|
|
|
|
|
2021-08-31 10:37:11 -04:00
|
|
|
bool
|
|
|
|
AvAdapter::getHardwareAcceleration()
|
|
|
|
{
|
|
|
|
return lrcInstance_->avModel().getHardwareAcceleration();
|
|
|
|
}
|
2021-08-26 14:57:24 -04:00
|
|
|
void
|
2021-08-31 10:37:11 -04:00
|
|
|
AvAdapter::setHardwareAcceleration(bool accelerate)
|
2021-08-26 14:57:24 -04:00
|
|
|
{
|
2021-08-31 10:37:11 -04:00
|
|
|
lrcInstance_->avModel().setHardwareAcceleration(accelerate);
|
2021-08-26 14:57:24 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
void
|
2021-08-31 10:37:11 -04:00
|
|
|
AvAdapter::startPreviewing(bool force)
|
2021-08-26 14:57:24 -04:00
|
|
|
{
|
2021-08-31 10:37:11 -04:00
|
|
|
lrcInstance_->renderer()->startPreviewing(force);
|
2021-08-26 14:57:24 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
void
|
2021-08-31 10:37:11 -04:00
|
|
|
AvAdapter::stopPreviewing()
|
2021-08-26 14:57:24 -04:00
|
|
|
{
|
2021-08-31 10:37:11 -04:00
|
|
|
if (!lrcInstance_->hasActiveCall(true)) {
|
|
|
|
lrcInstance_->renderer()->stopPreviewing();
|
|
|
|
}
|
2021-08-26 14:57:24 -04:00
|
|
|
}
|
2021-08-31 10:37:11 -04:00
|
|
|
|
|
|
|
bool
|
|
|
|
AvAdapter::isPreviewing()
|
|
|
|
{
|
|
|
|
return lrcInstance_->renderer()->isPreviewing();
|
|
|
|
}
|