1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492
|
/*
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"
#include "ui/boxes/confirm_box.h"
#include "platform/platform_specific.h"
#include "main/main_session.h"
#include "lang/lang_keys.h"
#include "styles/style_settings.h"
#include "ui/widgets/continuous_sliders.h"
#include "window/window_session_controller.h"
#include "core/application.h"
#include "core/core_settings.h"
#include "calls/calls_call.h"
#include "calls/calls_instance.h"
#include "calls/calls_video_bubble.h"
#include "apiwrap.h"
#include "api/api_authorizations.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 "styles/style_layers.h"
namespace Settings {
namespace {
using namespace Webrtc;
} // namespace
Calls::Calls(
QWidget *parent,
not_null<Window::SessionController*> controller)
: Section(parent)
, _controller(controller) {
setupContent();
requestPermissionAndStartTestingMicrophone();
}
Calls::~Calls() = default;
rpl::producer<QString> Calls::title() {
return tr::lng_settings_section_call_settings();
}
Webrtc::VideoTrack *Calls::AddCameraSubsection(
std::shared_ptr<Ui::Show> show,
not_null<Ui::VerticalLayout*> content,
bool saveToSettings) {
auto &lifetime = content->lifetime();
const auto hasCall = (Core::App().calls().currentCall() != nullptr);
const auto cameraNameStream = lifetime.make_state<
rpl::event_stream<QString>
>();
auto capturerOwner = lifetime.make_state<
std::shared_ptr<tgcalls::VideoCaptureInterface>
>();
const auto track = lifetime.make_state<VideoTrack>(
(hasCall
? VideoState::Inactive
: VideoState::Active));
const auto currentCameraName = [&] {
const auto cameras = GetVideoInputList();
const auto i = ranges::find(
cameras,
Core::App().settings().callVideoInputDeviceId(),
&VideoInput::id);
return (i != end(cameras))
? i->name
: tr::lng_settings_call_device_default(tr::now);
}();
AddButtonWithLabel(
content,
tr::lng_settings_call_input_device(),
rpl::single(
currentCameraName
) | rpl::then(
cameraNameStream->events()
),
st::settingsButtonNoIcon
)->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(content, [=](int option) {
cameraNameStream->fire_copy(options[option]);
const auto deviceId = option
? devices[option - 1].id
: "default";
if (saveToSettings) {
Core::App().settings().setCallVideoInputDeviceId(deviceId);
Core::App().saveSettingsDelayed();
}
if (const auto call = Core::App().calls().currentCall()) {
call->setCurrentCameraDevice(deviceId);
}
if (*capturerOwner) {
(*capturerOwner)->switchToDevice(
deviceId.toStdString(),
false);
}
});
show->showBox(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 = lifetime.make_state<::Calls::VideoBubble>(
bubbleWrap,
track);
const auto padding = st::settingsButtonNoIcon.padding.left();
const auto top = st::boxRoundShadow.extend.top();
const auto bottom = st::boxRoundShadow.extend.bottom();
auto frameSize = track->renderNextFrame(
) | rpl::map([=] {
return track->frameSize();
}) | rpl::filter([=](QSize size) {
return !size.isEmpty()
&& !Core::App().calls().currentCall()
&& !Core::App().calls().currentGroupCall();
});
auto bubbleWidth = bubbleWrap->widthValue(
) | rpl::filter([=](int width) {
return width > 2 * padding + 1;
});
rpl::combine(
std::move(bubbleWidth),
std::move(frameSize)
) | rpl::start_with_next([=](int width, QSize frame) {
const auto useWidth = (width - 2 * padding);
const auto useHeight = std::min(
((useWidth * frame.height()) / frame.width()),
(useWidth * 480) / 640);
bubbleWrap->resize(width, top + useHeight + bottom);
bubble->updateGeometry(
::Calls::VideoBubble::DragMode::None,
QRect(padding, top, useWidth, useHeight));
bubbleWrap->update();
}, bubbleWrap->lifetime());
using namespace rpl::mappers;
const auto checkCapturer = [=] {
if (*capturerOwner
|| Core::App().calls().currentCall()
|| Core::App().calls().currentGroupCall()) {
return;
}
*capturerOwner = Core::App().calls().getVideoCapture(
Core::App().settings().callVideoInputDeviceId(),
false);
(*capturerOwner)->setPreferredAspectRatio(0.);
track->setState(VideoState::Active);
(*capturerOwner)->setState(tgcalls::VideoState::Active);
(*capturerOwner)->setOutput(track->sink());
};
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);
*capturerOwner = nullptr;
} else {
crl::on_main(content, checkCapturer);
}
}, lifetime);
return track;
}
void Calls::sectionSaveChanges(FnMut<void()> done) {
if (_micTester) {
_micTester.reset();
}
done();
}
void Calls::setupContent() {
const auto content = Ui::CreateChild<Ui::VerticalLayout>(this);
if (!GetVideoInputList().empty()) {
AddSkip(content);
AddSubsectionTitle(content, tr::lng_settings_call_camera());
AddCameraSubsection(
std::make_shared<Window::Show>(_controller),
content,
true);
AddSkip(content);
AddDivider(content);
}
AddSkip(content);
AddSubsectionTitle(content, tr::lng_settings_call_section_output());
AddButtonWithLabel(
content,
tr::lng_settings_call_output_device(),
rpl::single(
CurrentAudioOutputName()
) | rpl::then(
_outputNameStream.events()
),
st::settingsButtonNoIcon
)->addClickHandler([=] {
_controller->show(ChooseAudioOutputBox(crl::guard(this, [=](
const QString &id,
const QString &name) {
_outputNameStream.fire_copy(name);
})));
});
AddSkip(content);
AddDivider(content);
AddSkip(content);
AddSubsectionTitle(content, tr::lng_settings_call_section_input());
AddButtonWithLabel(
content,
tr::lng_settings_call_input_device(),
rpl::single(
CurrentAudioInputName()
) | rpl::then(
_inputNameStream.events()
),
st::settingsButtonNoIcon
)->addClickHandler([=] {
_controller->show(ChooseAudioInputBox(crl::guard(this, [=](
const QString &id,
const QString &name) {
_inputNameStream.fire_copy(name);
if (_micTester) {
_micTester->setDeviceId(id);
}
})));
});
_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);
});
AddSkip(content);
AddDivider(content);
AddSkip(content);
AddSubsectionTitle(content, tr::lng_settings_call_section_other());
const auto api = &_controller->session().api();
AddButton(
content,
tr::lng_settings_call_accept_calls(),
st::settingsButtonNoIcon
)->toggleOn(
api->authorizations().callsDisabledHereValue(
) | rpl::map(!rpl::mappers::_1)
)->toggledChanges(
) | rpl::filter([=](bool value) {
return (value == api->authorizations().callsDisabledHere());
}) | start_with_next([=](bool value) {
api->authorizations().toggleCallsDisabledHere(!value);
}, content->lifetime());
AddButton(
content,
tr::lng_settings_call_open_system_prefs(),
st::settingsButtonNoIcon
)->addClickHandler([=] {
const auto opened = Platform::OpenSystemSettings(
Platform::SystemSettingsType::Audio);
if (!opened) {
_controller->show(
Ui::MakeInformBox(tr::lng_linux_no_audio_prefs()));
}
});
AddSkip(content);
Ui::ResizeFitChild(this, content);
}
void Calls::requestPermissionAndStartTestingMicrophone() {
const auto status = Platform::GetPermissionStatus(
Platform::PermissionType::Microphone);
if (status == Platform::PermissionStatus::Granted) {
startTestingMicrophone();
} else if (status == Platform::PermissionStatus::CanRequest) {
const auto startTestingChecked = crl::guard(this, [=](
Platform::PermissionStatus status) {
if (status == Platform::PermissionStatus::Granted) {
crl::on_main(crl::guard(this, [=] {
startTestingMicrophone();
}));
}
});
Platform::RequestPermission(
Platform::PermissionType::Microphone,
startTestingChecked);
} else {
const auto showSystemSettings = [controller = _controller] {
Platform::OpenSystemSettingsForPermission(
Platform::PermissionType::Microphone);
controller->hideLayer();
};
_controller->show(Ui::MakeConfirmBox({
.text = tr::lng_no_mic_permission(),
.confirmed = showSystemSettings,
.confirmText = tr::lng_menu_settings(),
}));
}
}
void Calls::startTestingMicrophone() {
_levelUpdateTimer.callEach(kMicTestUpdateInterval);
_micTester = std::make_unique<AudioInputTester>(
Core::App().settings().callAudioBackend(),
Core::App().settings().callInputDeviceId());
}
QString CurrentAudioOutputName() {
const auto &settings = Core::App().settings();
const auto list = GetAudioOutputList(settings.callAudioBackend());
const auto i = ranges::find(
list,
settings.callOutputDeviceId(),
&AudioOutput::id);
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());
const auto i = ranges::find(
list,
settings.callInputDeviceId(),
&AudioInput::id);
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)
) | ranges::to_vector;
const auto i = ranges::find(
list,
settings.callOutputDeviceId(),
&AudioOutput::id);
const auto currentOption = (i != end(list))
? int(i - begin(list) + 1)
: 0;
const auto save = [=](int option) {
const auto deviceId = option
? list[option - 1].id
: "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,
});
});
}
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)
) | ranges::to_vector;
const auto i = ranges::find(
list,
Core::App().settings().callInputDeviceId(),
&AudioInput::id);
const auto currentOption = (i != end(list))
? int(i - begin(list) + 1)
: 0;
const auto save = [=](int option) {
const auto deviceId = option
? list[option - 1].id
: "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,
});
});
}
//
//object_ptr<Ui::GenericBox> ChooseAudioBackendBox(
// 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();
// Core::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,
// });
// });
//}
} // namespace Settings
|