tdesktop/Telegram/SourceFiles/settings/settings_calls.cpp

450 lines
13 KiB
C++
Raw Normal View History

2019-01-05 11:08:02 +00:00
/*
This file is part of Telegram Desktop,
the official desktop application for the Telegram messaging service.
For license and copyright information please follow this link:
https://github.com/telegramdesktop/tdesktop/blob/master/LEGAL
*/
#include "settings/settings_calls.h"
#include "settings/settings_common.h"
#include "ui/wrap/vertical_layout.h"
#include "ui/wrap/slide_wrap.h"
#include "ui/widgets/labels.h"
#include "ui/widgets/checkbox.h"
#include "ui/widgets/level_meter.h"
#include "ui/widgets/buttons.h"
#include "ui/boxes/single_choice_box.h"
2019-01-05 11:08:02 +00:00
#include "boxes/confirm_box.h"
#include "platform/platform_specific.h"
#include "main/main_session.h"
2019-01-05 11:08:02 +00:00
#include "lang/lang_keys.h"
#include "layout.h"
#include "styles/style_settings.h"
#include "ui/widgets/continuous_sliders.h"
2019-08-06 16:40:08 +00:00
#include "window/window_session_controller.h"
#include "core/application.h"
#include "core/core_settings.h"
#include "calls/calls_call.h"
2019-01-05 11:08:02 +00:00
#include "calls/calls_instance.h"
#include "calls/calls_video_bubble.h"
#include "webrtc/webrtc_media_devices.h"
#include "webrtc/webrtc_video_track.h"
#include "webrtc/webrtc_audio_input_tester.h"
#include "webrtc/webrtc_create_adm.h" // Webrtc::Backend.
#include "tgcalls/VideoCaptureInterface.h"
#include "facades.h"
#include "app.h" // App::restart().
#include "styles/style_layers.h"
2019-01-05 11:08:02 +00:00
namespace Settings {
namespace {
using namespace Webrtc;
} // namespace
2019-01-05 11:08:02 +00:00
2019-07-24 14:00:30 +00:00
Calls::Calls(
QWidget *parent,
not_null<Window::SessionController*> controller)
: Section(parent)
, _controller(controller) {
setupContent();
requestPermissionAndStartTestingMicrophone();
2019-01-05 11:08:02 +00:00
}
2020-11-28 16:18:51 +00:00
Calls::~Calls() = default;
2019-01-05 11:08:02 +00:00
2019-01-11 10:07:56 +00:00
void Calls::sectionSaveChanges(FnMut<void()> done) {
2019-01-05 11:08:02 +00:00
if (_micTester) {
_micTester.reset();
}
done();
}
void Calls::setupContent() {
2019-01-05 11:08:02 +00:00
const auto content = Ui::CreateChild<Ui::VerticalLayout>(this);
const auto &settings = Core::App().settings();
const auto cameras = GetVideoInputList();
if (!cameras.empty()) {
const auto hasCall = (Core::App().calls().currentCall() != nullptr);
auto capturerOwner = Core::App().calls().getVideoCapture();
const auto capturer = capturerOwner.get();
content->lifetime().add([owner = std::move(capturerOwner)]{});
const auto track = content->lifetime().make_state<VideoTrack>(
(hasCall
? VideoState::Inactive
: VideoState::Active));
const auto currentCameraName = [&] {
const auto i = ranges::find(
cameras,
settings.callVideoInputDeviceId(),
&VideoInput::id);
return (i != end(cameras))
2020-11-28 16:18:51 +00:00
? i->name
: tr::lng_settings_call_device_default(tr::now);
}();
AddSkip(content);
AddSubsectionTitle(content, tr::lng_settings_call_camera());
AddButtonWithLabel(
content,
tr::lng_settings_call_input_device(),
rpl::single(
currentCameraName
) | rpl::then(
_cameraNameStream.events()
),
st::settingsButton
)->addClickHandler([=] {
const auto &devices = GetVideoInputList();
const auto options = ranges::views::concat(
ranges::views::single(tr::lng_settings_call_device_default(tr::now)),
devices | ranges::views::transform(&VideoInput::name)
) | ranges::to_vector;
const auto i = ranges::find(
devices,
Core::App().settings().callVideoInputDeviceId(),
&VideoInput::id);
const auto currentOption = (i != end(devices))
? int(i - begin(devices) + 1)
: 0;
const auto save = crl::guard(this, [=](int option) {
_cameraNameStream.fire_copy(options[option]);
const auto deviceId = option
? devices[option - 1].id
: "default";
capturer->switchToDevice(deviceId.toStdString());
Core::App().settings().setCallVideoInputDeviceId(deviceId);
Core::App().saveSettingsDelayed();
if (const auto call = Core::App().calls().currentCall()) {
call->setCurrentVideoDevice(deviceId);
}
});
Ui::show(Box([=](not_null<Ui::GenericBox*> box) {
SingleChoiceBox(box, {
.title = tr::lng_settings_call_camera(),
.options = options,
.initialSelection = currentOption,
.callback = save,
});
}));
});
const auto bubbleWrap = content->add(object_ptr<Ui::RpWidget>(content));
const auto bubble = content->lifetime().make_state<::Calls::VideoBubble>(
bubbleWrap,
track);
const auto padding = st::settingsButton.padding.left();
const auto top = st::boxRoundShadow.extend.top();
const auto bottom = st::boxRoundShadow.extend.bottom();
bubbleWrap->widthValue(
) | rpl::filter([=](int width) {
return (width > 2 * padding + 1);
}) | rpl::start_with_next([=](int width) {
const auto use = (width - 2 * padding);
bubble->updateGeometry(
::Calls::VideoBubble::DragMode::None,
QRect(padding, top, use, (use * 480) / 640));
}, bubbleWrap->lifetime());
track->renderNextFrame(
) | rpl::start_with_next([=] {
const auto size = track->frameSize();
if (size.isEmpty()
|| Core::App().calls().currentCall()
|| Core::App().calls().currentGroupCall()) {
return;
}
const auto width = bubbleWrap->width();
const auto use = (width - 2 * padding);
const auto height = std::min(
((use * size.height()) / size.width()),
(use * 480) / 640);
bubbleWrap->resize(width, top + height + bottom);
bubbleWrap->update();
}, bubbleWrap->lifetime());
using namespace rpl::mappers;
rpl::combine(
Core::App().calls().currentCallValue(),
Core::App().calls().currentGroupCallValue(),
_1 || _2
) | rpl::start_with_next([=](bool has) {
if (has) {
track->setState(VideoState::Inactive);
bubbleWrap->resize(bubbleWrap->width(), 0);
} else {
capturer->setPreferredAspectRatio(0.);
track->setState(VideoState::Active);
capturer->setOutput(track->sink());
}
}, content->lifetime());
AddSkip(content);
AddDivider(content);
}
2019-01-05 11:08:02 +00:00
AddSkip(content);
2019-06-18 12:16:43 +00:00
AddSubsectionTitle(content, tr::lng_settings_call_section_output());
2019-01-11 10:07:56 +00:00
AddButtonWithLabel(
2019-01-05 11:08:02 +00:00
content,
2019-06-18 12:16:43 +00:00
tr::lng_settings_call_output_device(),
2019-01-11 10:07:56 +00:00
rpl::single(
2020-11-28 16:18:51 +00:00
CurrentAudioOutputName()
2019-01-11 10:07:56 +00:00
) | rpl::then(
_outputNameStream.events()
),
st::settingsButton
)->addClickHandler([=] {
2020-11-28 16:18:51 +00:00
Ui::show(ChooseAudioOutputBox(crl::guard(this, [=](
const QString &id,
const QString &name) {
_outputNameStream.fire_copy(name);
})));
2019-01-05 11:08:02 +00:00
});
AddSkip(content);
AddDivider(content);
AddSkip(content);
2019-06-18 12:16:43 +00:00
AddSubsectionTitle(content, tr::lng_settings_call_section_input());
2019-01-11 10:07:56 +00:00
AddButtonWithLabel(
2019-01-05 11:08:02 +00:00
content,
2019-06-18 12:16:43 +00:00
tr::lng_settings_call_input_device(),
2019-01-11 10:07:56 +00:00
rpl::single(
2020-11-28 16:18:51 +00:00
CurrentAudioInputName()
2019-01-11 10:07:56 +00:00
) | rpl::then(
_inputNameStream.events()
),
st::settingsButton
)->addClickHandler([=] {
Ui::show(ChooseAudioInputBox(crl::guard(this, [=](
2020-11-28 16:18:51 +00:00
const QString &id,
const QString &name) {
_inputNameStream.fire_copy(name);
2019-01-05 11:08:02 +00:00
if (_micTester) {
2020-11-28 16:18:51 +00:00
_micTester->setDeviceId(id);
2019-01-05 11:08:02 +00:00
}
2020-11-28 16:18:51 +00:00
})));
2019-01-05 11:08:02 +00:00
});
2019-01-11 10:07:56 +00:00
_micTestLevel = content->add(
object_ptr<Ui::LevelMeter>(
content,
st::defaultLevelMeter),
st::settingsLevelMeterPadding);
_micTestLevel->resize(QSize(0, st::defaultLevelMeter.height));
_levelUpdateTimer.setCallback([=] {
const auto was = _micLevel;
_micLevel = _micTester->getAndResetLevel();
_micLevelAnimation.start([=] {
_micTestLevel->setValue(_micLevelAnimation.value(_micLevel));
}, was, _micLevel, kMicTestAnimationDuration);
2019-01-05 11:08:02 +00:00
});
AddSkip(content);
AddDivider(content);
AddSkip(content);
2019-06-18 12:16:43 +00:00
AddSubsectionTitle(content, tr::lng_settings_call_section_other());
2019-01-05 11:08:02 +00:00
AddButton(
content,
tr::lng_settings_call_accept_calls(),
st::settingsButton
)->toggleOn(rpl::single(
!settings.disableCalls()
))->toggledChanges(
) | rpl::filter([&settings](bool value) {
return (settings.disableCalls() == value);
}) | rpl::start_with_next([=](bool value) {
Core::App().settings().setDisableCalls(!value);
Core::App().saveSettingsDelayed();
}, content->lifetime());
2019-01-11 10:07:56 +00:00
AddButton(
content,
2019-06-18 12:16:43 +00:00
tr::lng_settings_call_open_system_prefs(),
2019-01-11 10:07:56 +00:00
st::settingsButton
)->addClickHandler([] {
const auto opened = Platform::OpenSystemSettings(
Platform::SystemSettingsType::Audio);
if (!opened) {
2019-06-19 15:09:03 +00:00
Ui::show(Box<InformBox>(tr::lng_linux_no_audio_prefs(tr::now)));
2019-01-05 11:08:02 +00:00
}
});
2019-01-05 11:08:02 +00:00
AddSkip(content);
Ui::ResizeFitChild(this, content);
}
2019-01-11 10:07:56 +00:00
void Calls::requestPermissionAndStartTestingMicrophone() {
const auto status = Platform::GetPermissionStatus(
Platform::PermissionType::Microphone);
2019-01-05 11:08:02 +00:00
if (status == Platform::PermissionStatus::Granted) {
startTestingMicrophone();
} else if (status == Platform::PermissionStatus::CanRequest) {
2019-01-11 10:07:56 +00:00
const auto startTestingChecked = crl::guard(this, [=](
Platform::PermissionStatus status) {
2019-01-05 11:08:02 +00:00
if (status == Platform::PermissionStatus::Granted) {
2019-01-11 10:07:56 +00:00
crl::on_main(crl::guard(this, [=] {
2019-01-05 11:08:02 +00:00
startTestingMicrophone();
}));
}
2019-01-11 10:07:56 +00:00
});
Platform::RequestPermission(
Platform::PermissionType::Microphone,
startTestingChecked);
2019-01-05 11:08:02 +00:00
} else {
2019-01-11 10:07:56 +00:00
const auto showSystemSettings = [] {
Platform::OpenSystemSettingsForPermission(
Platform::PermissionType::Microphone);
2019-01-05 11:08:02 +00:00
Ui::hideLayer();
2019-01-11 10:07:56 +00:00
};
Ui::show(Box<ConfirmBox>(
2019-06-19 15:09:03 +00:00
tr::lng_no_mic_permission(tr::now),
tr::lng_menu_settings(tr::now),
2019-01-11 10:07:56 +00:00
showSystemSettings));
2019-01-05 11:08:02 +00:00
}
}
2019-01-11 10:07:56 +00:00
void Calls::startTestingMicrophone() {
_levelUpdateTimer.callEach(kMicTestUpdateInterval);
_micTester = std::make_unique<AudioInputTester>(
Core::App().settings().callAudioBackend(),
Core::App().settings().callInputDeviceId());
2019-01-05 11:08:02 +00:00
}
2020-11-28 16:18:51 +00:00
QString CurrentAudioOutputName() {
const auto &settings = Core::App().settings();
const auto list = GetAudioOutputList(settings.callAudioBackend());
2020-11-28 16:18:51 +00:00
const auto i = ranges::find(
list,
settings.callOutputDeviceId(),
&AudioOutput::id);
2020-11-28 16:18:51 +00:00
return (i != end(list))
? i->name
: tr::lng_settings_call_device_default(tr::now);
}
QString CurrentAudioInputName() {
const auto &settings = Core::App().settings();
const auto list = GetAudioInputList(settings.callAudioBackend());
2020-11-28 16:18:51 +00:00
const auto i = ranges::find(
list,
settings.callInputDeviceId(),
&AudioInput::id);
2020-11-28 16:18:51 +00:00
return (i != end(list))
? i->name
: tr::lng_settings_call_device_default(tr::now);
}
object_ptr<Ui::GenericBox> ChooseAudioOutputBox(
Fn<void(QString id, QString name)> chosen,
const style::Checkbox *st,
const style::Radio *radioSt) {
const auto &settings = Core::App().settings();
const auto list = GetAudioOutputList(settings.callAudioBackend());
const auto options = ranges::views::concat(
ranges::views::single(tr::lng_settings_call_device_default(tr::now)),
list | ranges::views::transform(&AudioOutput::name)
2020-11-28 16:18:51 +00:00
) | ranges::to_vector;
const auto i = ranges::find(
list,
settings.callOutputDeviceId(),
&AudioOutput::id);
const auto currentOption = (i != end(list))
? int(i - begin(list) + 1)
2020-11-28 16:18:51 +00:00
: 0;
const auto save = [=](int option) {
const auto deviceId = option
? list[option - 1].id
2020-11-28 16:18:51 +00:00
: "default";
Core::App().calls().setCurrentAudioDevice(false, deviceId);
chosen(deviceId, options[option]);
};
return Box([=](not_null<Ui::GenericBox*> box) {
SingleChoiceBox(box, {
.title = tr::lng_settings_call_output_device(),
.options = options,
.initialSelection = currentOption,
.callback = save,
.st = st,
.radioSt = radioSt,
});
});
2020-11-28 16:18:51 +00:00
}
object_ptr<Ui::GenericBox> ChooseAudioInputBox(
Fn<void(QString id, QString name)> chosen,
const style::Checkbox *st,
const style::Radio *radioSt) {
const auto &settings = Core::App().settings();
const auto list = GetAudioInputList(settings.callAudioBackend());
const auto options = ranges::views::concat(
ranges::views::single(tr::lng_settings_call_device_default(tr::now)),
list | ranges::views::transform(&AudioInput::name)
2020-11-28 16:18:51 +00:00
) | ranges::to_vector;
const auto i = ranges::find(
list,
2020-11-28 16:18:51 +00:00
Core::App().settings().callInputDeviceId(),
&AudioInput::id);
const auto currentOption = (i != end(list))
? int(i - begin(list) + 1)
2020-11-28 16:18:51 +00:00
: 0;
const auto save = [=](int option) {
const auto deviceId = option
? list[option - 1].id
2020-11-28 16:18:51 +00:00
: "default";
Core::App().calls().setCurrentAudioDevice(true, deviceId);
chosen(deviceId, options[option]);
};
return Box([=](not_null<Ui::GenericBox*> box) {
SingleChoiceBox(box, {
.title = tr::lng_settings_call_input_device(),
.options = options,
.initialSelection = currentOption,
.callback = save,
.st = st,
.radioSt = radioSt,
});
});
2020-11-28 16:18:51 +00:00
}
//
//object_ptr<Ui::GenericBox> ChooseAudioBackendBox(
2021-01-21 15:57:12 +00:00
// const style::Checkbox *st,
// const style::Radio *radioSt) {
// const auto &settings = Core::App().settings();
// const auto list = GetAudioInputList(settings.callAudioBackend());
// const auto options = std::vector<QString>{
// "OpenAL",
// "Webrtc ADM",
//#ifdef Q_OS_WIN
// "Webrtc ADM2",
//#endif // Q_OS_WIN
// };
// const auto currentOption = static_cast<int>(settings.callAudioBackend());
// const auto save = [=](int option) {
// Core::App().settings().setCallAudioBackend(
// static_cast<Webrtc::Backend>(option));
// Core::App().saveSettings();
// App::restart();
// };
// return Box([=](not_null<Ui::GenericBox*> box) {
// SingleChoiceBox(box, {
// .title = rpl::single<QString>("Calls audio backend"),
// .options = options,
// .initialSelection = currentOption,
// .callback = save,
// .st = st,
// .radioSt = radioSt,
// });
// });
2021-01-21 15:57:12 +00:00
//}
2019-01-05 11:08:02 +00:00
} // namespace Settings