2021-05-29 14:13:12 +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
|
|
|
|
*/
|
2021-06-04 20:35:56 +00:00
|
|
|
#include "window/system_media_controls_manager.h"
|
2021-05-29 14:13:12 +00:00
|
|
|
|
|
|
|
#include "base/observer.h"
|
2021-06-04 20:35:56 +00:00
|
|
|
#include "base/platform/base_platform_system_media_controls.h"
|
2021-05-29 14:13:12 +00:00
|
|
|
#include "core/application.h"
|
|
|
|
#include "data/data_document.h"
|
|
|
|
#include "data/data_document_media.h"
|
|
|
|
#include "data/data_file_origin.h"
|
|
|
|
#include "main/main_session.h"
|
|
|
|
#include "media/audio/media_audio.h"
|
|
|
|
#include "media/player/media_player_instance.h"
|
|
|
|
#include "ui/text/format_song_document_name.h"
|
|
|
|
|
2021-06-04 20:35:56 +00:00
|
|
|
namespace Window {
|
2021-05-29 14:13:12 +00:00
|
|
|
|
2021-06-04 20:35:56 +00:00
|
|
|
bool SystemMediaControlsManager::Supported() {
|
|
|
|
return base::Platform::SystemMediaControls::Supported();
|
|
|
|
}
|
2021-05-29 14:13:12 +00:00
|
|
|
|
2021-06-04 20:35:56 +00:00
|
|
|
SystemMediaControlsManager::SystemMediaControlsManager(
|
|
|
|
not_null<QWidget*> parent)
|
|
|
|
: _controls(std::make_unique<base::Platform::SystemMediaControls>()) {
|
2021-05-29 14:13:12 +00:00
|
|
|
|
2021-06-04 20:35:56 +00:00
|
|
|
using PlaybackStatus =
|
|
|
|
base::Platform::SystemMediaControls::PlaybackStatus;
|
|
|
|
using Command = base::Platform::SystemMediaControls::Command;
|
|
|
|
|
|
|
|
const auto inited = _controls->init(parent.get());
|
|
|
|
if (!inited) {
|
|
|
|
LOG(("SystemMediaControlsManager failed to init."));
|
|
|
|
return;
|
|
|
|
}
|
2021-05-29 14:13:12 +00:00
|
|
|
const auto type = AudioMsgId::Type::Song;
|
|
|
|
|
2021-06-04 22:49:24 +00:00
|
|
|
using TrackState = Media::Player::TrackState;
|
2021-05-29 14:13:12 +00:00
|
|
|
const auto mediaPlayer = Media::Player::instance();
|
|
|
|
|
2021-06-04 22:49:24 +00:00
|
|
|
auto trackFilter = rpl::filter([=](const TrackState &state) {
|
|
|
|
return (state.id.type() == type);
|
|
|
|
});
|
|
|
|
|
2021-05-29 14:13:12 +00:00
|
|
|
mediaPlayer->updatedNotifier(
|
2021-06-04 22:49:24 +00:00
|
|
|
) | trackFilter | rpl::map([=](const TrackState &state) {
|
2021-05-29 14:13:12 +00:00
|
|
|
using namespace Media::Player;
|
|
|
|
if (IsStoppedOrStopping(state.state)) {
|
|
|
|
return PlaybackStatus::Stopped;
|
|
|
|
} else if (IsPausedOrPausing(state.state)) {
|
|
|
|
return PlaybackStatus::Paused;
|
|
|
|
}
|
|
|
|
return PlaybackStatus::Playing;
|
|
|
|
}) | rpl::distinct_until_changed(
|
|
|
|
) | rpl::start_with_next([=](PlaybackStatus status) {
|
|
|
|
_controls->setPlaybackStatus(status);
|
|
|
|
}, _lifetime);
|
|
|
|
|
2021-06-04 22:49:24 +00:00
|
|
|
if (_controls->seekingSupported()) {
|
|
|
|
mediaPlayer->updatedNotifier(
|
|
|
|
) | trackFilter | rpl::map([=](const TrackState &state) {
|
|
|
|
return state.position;
|
|
|
|
}) | rpl::distinct_until_changed(
|
|
|
|
) | rpl::start_with_next([=](int position) {
|
|
|
|
_controls->setPosition(position);
|
|
|
|
}, _lifetime);
|
|
|
|
|
|
|
|
mediaPlayer->updatedNotifier(
|
|
|
|
) | trackFilter | rpl::map([=](const TrackState &state) {
|
|
|
|
return state.length;
|
|
|
|
}) | rpl::distinct_until_changed(
|
|
|
|
) | rpl::start_with_next([=](int length) {
|
|
|
|
_controls->setDuration(length);
|
|
|
|
}, _lifetime);
|
|
|
|
}
|
|
|
|
|
2021-05-29 14:13:12 +00:00
|
|
|
rpl::merge(
|
|
|
|
mediaPlayer->stops(type) | rpl::map_to(false),
|
|
|
|
mediaPlayer->startsPlay(type) | rpl::map_to(true)
|
|
|
|
) | rpl::start_with_next([=](bool audio) {
|
2021-06-04 20:35:56 +00:00
|
|
|
_controls->setEnabled(audio);
|
2021-05-29 14:13:12 +00:00
|
|
|
if (audio) {
|
|
|
|
_controls->setIsNextEnabled(mediaPlayer->nextAvailable(type));
|
|
|
|
_controls->setIsPreviousEnabled(
|
|
|
|
mediaPlayer->previousAvailable(type));
|
|
|
|
_controls->setIsPlayPauseEnabled(true);
|
|
|
|
_controls->setIsStopEnabled(true);
|
|
|
|
_controls->setPlaybackStatus(PlaybackStatus::Playing);
|
|
|
|
_controls->updateDisplay();
|
|
|
|
} else {
|
|
|
|
_cachedMediaView.clear();
|
|
|
|
_controls->clearMetadata();
|
|
|
|
}
|
|
|
|
_lifetimeDownload.destroy();
|
|
|
|
}, _lifetime);
|
|
|
|
|
|
|
|
auto trackChanged = base::ObservableViewer(
|
|
|
|
mediaPlayer->trackChangedNotifier()
|
|
|
|
) | rpl::filter([=](AudioMsgId::Type audioType) {
|
|
|
|
return audioType == type;
|
|
|
|
});
|
|
|
|
|
|
|
|
auto unlocked = Core::App().passcodeLockChanges(
|
2021-06-04 20:35:56 +00:00
|
|
|
) | rpl::filter([=](bool locked) {
|
|
|
|
return !locked && (mediaPlayer->current(type));
|
2021-05-29 14:13:12 +00:00
|
|
|
}) | rpl::map([=] {
|
|
|
|
return type;
|
|
|
|
}) | rpl::before_next([=] {
|
|
|
|
_controls->setEnabled(true);
|
|
|
|
_controls->updateDisplay();
|
|
|
|
});
|
|
|
|
|
|
|
|
rpl::merge(
|
|
|
|
std::move(trackChanged),
|
|
|
|
std::move(unlocked)
|
|
|
|
) | rpl::start_with_next([=](AudioMsgId::Type audioType) {
|
|
|
|
_lifetimeDownload.destroy();
|
|
|
|
|
|
|
|
const auto current = mediaPlayer->current(audioType);
|
|
|
|
if (!current) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
const auto document = current.audio();
|
|
|
|
|
|
|
|
const auto &[title, performer] = Ui::Text::FormatSongNameFor(document)
|
|
|
|
.composedName();
|
|
|
|
|
|
|
|
_controls->setArtist(performer);
|
|
|
|
_controls->setTitle(title);
|
|
|
|
|
|
|
|
if (document && document->isSongWithCover()) {
|
|
|
|
const auto view = document->createMediaView();
|
|
|
|
view->thumbnailWanted(current.contextId());
|
|
|
|
_cachedMediaView.push_back(view);
|
|
|
|
if (const auto imagePtr = view->thumbnail()) {
|
|
|
|
_controls->setThumbnail(imagePtr->original());
|
|
|
|
} else {
|
|
|
|
document->session().downloaderTaskFinished(
|
|
|
|
) | rpl::start_with_next([=] {
|
|
|
|
if (const auto imagePtr = view->thumbnail()) {
|
|
|
|
_controls->setThumbnail(imagePtr->original());
|
|
|
|
_lifetimeDownload.destroy();
|
|
|
|
}
|
|
|
|
}, _lifetimeDownload);
|
|
|
|
_controls->clearThumbnail();
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
_controls->clearThumbnail();
|
|
|
|
}
|
|
|
|
}, _lifetime);
|
|
|
|
|
|
|
|
_controls->commandRequests(
|
|
|
|
) | rpl::start_with_next([=](Command command) {
|
|
|
|
switch (command) {
|
2021-06-04 22:49:24 +00:00
|
|
|
case Command::PlayPause: mediaPlayer->playPause(type); break;
|
2021-05-29 14:13:12 +00:00
|
|
|
case Command::Play: mediaPlayer->play(type); break;
|
|
|
|
case Command::Pause: mediaPlayer->pause(type); break;
|
|
|
|
case Command::Next: mediaPlayer->next(type); break;
|
|
|
|
case Command::Previous: mediaPlayer->previous(type); break;
|
|
|
|
case Command::Stop: mediaPlayer->stop(type); break;
|
|
|
|
}
|
|
|
|
}, _lifetime);
|
|
|
|
|
2021-06-04 22:49:24 +00:00
|
|
|
if (_controls->seekingSupported()) {
|
|
|
|
_controls->seekRequests(
|
|
|
|
) | rpl::start_with_next([=](float64 progress) {
|
|
|
|
mediaPlayer->finishSeeking(type, progress);
|
|
|
|
}, _lifetime);
|
|
|
|
}
|
|
|
|
|
2021-05-29 14:13:12 +00:00
|
|
|
Core::App().passcodeLockValue(
|
|
|
|
) | rpl::filter([=](bool locked) {
|
|
|
|
return locked && Core::App().maybeActiveSession();
|
|
|
|
}) | rpl::start_with_next([=] {
|
|
|
|
_controls->setEnabled(false);
|
|
|
|
}, _lifetime);
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
SystemMediaControlsManager::~SystemMediaControlsManager() = default;
|
|
|
|
|
2021-06-04 20:35:56 +00:00
|
|
|
} // namespace Window
|