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-08 18:58:34 +00:00
|
|
|
#include "media/system_media_controls_manager.h"
|
2021-05-29 14:13:12 +00:00
|
|
|
|
2021-09-05 20:48:37 +00:00
|
|
|
#include "media/audio/media_audio.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"
|
2021-06-06 20:34:32 +00:00
|
|
|
#include "core/core_settings.h"
|
2021-05-29 14:13:12 +00:00
|
|
|
#include "data/data_document.h"
|
|
|
|
#include "data/data_document_media.h"
|
|
|
|
#include "data/data_file_origin.h"
|
2021-06-06 19:49:15 +00:00
|
|
|
#include "mainwidget.h"
|
|
|
|
#include "main/main_account.h"
|
2021-05-29 14:13:12 +00:00
|
|
|
#include "main/main_session.h"
|
|
|
|
#include "media/audio/media_audio.h"
|
2021-06-08 04:14:36 +00:00
|
|
|
#include "media/streaming/media_streaming_instance.h"
|
|
|
|
#include "media/streaming/media_streaming_player.h"
|
2021-05-29 14:13:12 +00:00
|
|
|
#include "ui/text/format_song_document_name.h"
|
2021-06-06 19:49:15 +00:00
|
|
|
#include "window/window_controller.h"
|
2021-05-29 14:13:12 +00:00
|
|
|
|
2021-06-08 18:58:34 +00:00
|
|
|
namespace Media {
|
2021-12-26 18:49:35 +00:00
|
|
|
namespace {
|
|
|
|
|
|
|
|
[[nodiscard]] auto RepeatModeToLoopStatus(Media::Player::RepeatMode mode) {
|
|
|
|
using Mode = Media::Player::RepeatMode;
|
|
|
|
using Status = base::Platform::SystemMediaControls::LoopStatus;
|
|
|
|
switch (mode) {
|
|
|
|
case Mode::None: return Status::None;
|
|
|
|
case Mode::One: return Status::Track;
|
|
|
|
case Mode::All: return Status::Playlist;
|
|
|
|
}
|
|
|
|
Unexpected("RepeatModeToLoopStatus in SystemMediaControlsManager");
|
|
|
|
}
|
|
|
|
|
|
|
|
} // namespace
|
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(
|
2021-06-06 19:49:15 +00:00
|
|
|
not_null<Window::Controller*> controller)
|
2021-06-04 20:35:56 +00:00
|
|
|
: _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;
|
|
|
|
|
2021-07-09 11:17:48 +00:00
|
|
|
_controls->setServiceName(qsl("org.mpris.MediaPlayer2.tdesktop"));
|
2021-06-06 19:49:28 +00:00
|
|
|
_controls->setApplicationName(AppName.utf16());
|
2021-06-06 19:49:15 +00:00
|
|
|
const auto inited = _controls->init(controller->widget());
|
2021-06-04 20:35:56 +00:00
|
|
|
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;
|
2021-06-08 04:14:36 +00:00
|
|
|
if (_streamed) {
|
|
|
|
const auto &player = _streamed->player();
|
|
|
|
if (player.buffering() || !player.playing()) {
|
|
|
|
return PlaybackStatus::Paused;
|
|
|
|
}
|
|
|
|
}
|
2021-05-29 14:13:12 +00:00
|
|
|
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);
|
|
|
|
|
|
|
|
rpl::merge(
|
|
|
|
mediaPlayer->stops(type) | rpl::map_to(false),
|
|
|
|
mediaPlayer->startsPlay(type) | rpl::map_to(true)
|
2021-06-07 05:40:50 +00:00
|
|
|
) | rpl::distinct_until_changed() | 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();
|
2021-06-08 04:14:36 +00:00
|
|
|
_streamed = nullptr;
|
2021-05-29 14:13:12 +00:00
|
|
|
_controls->clearMetadata();
|
|
|
|
}
|
|
|
|
_lifetimeDownload.destroy();
|
|
|
|
}, _lifetime);
|
|
|
|
|
2021-11-19 11:55:09 +00:00
|
|
|
auto trackChanged = mediaPlayer->trackChanged(
|
2021-05-29 14:13:12 +00:00
|
|
|
) | 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;
|
|
|
|
}
|
2021-06-07 05:28:53 +00:00
|
|
|
if ((_lastAudioMsgId.contextId() == current.contextId())
|
|
|
|
&& (_lastAudioMsgId.audio() == current.audio())
|
|
|
|
&& (_lastAudioMsgId.type() == current.type())) {
|
|
|
|
return;
|
|
|
|
}
|
2021-05-29 14:13:12 +00:00
|
|
|
const auto document = current.audio();
|
|
|
|
|
|
|
|
const auto &[title, performer] = Ui::Text::FormatSongNameFor(document)
|
|
|
|
.composedName();
|
|
|
|
|
|
|
|
_controls->setArtist(performer);
|
|
|
|
_controls->setTitle(title);
|
|
|
|
|
2021-06-07 05:40:50 +00:00
|
|
|
if (_controls->seekingSupported()) {
|
|
|
|
const auto state = mediaPlayer->getState(audioType);
|
|
|
|
_controls->setDuration(state.length);
|
|
|
|
// macOS NowPlaying and Linux MPRIS update the track position
|
|
|
|
// according to the rate property
|
|
|
|
// while the playback status is "playing",
|
|
|
|
// so we should change the track position only when
|
|
|
|
// the track is changed
|
|
|
|
// or when the position is changed by the user.
|
|
|
|
_controls->setPosition(state.position);
|
2021-06-08 04:14:36 +00:00
|
|
|
|
|
|
|
_streamed = std::make_unique<Media::Streaming::Instance>(
|
|
|
|
document,
|
|
|
|
current.contextId(),
|
|
|
|
nullptr);
|
2021-06-07 05:40:50 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// Setting a thumbnail can take a long time,
|
|
|
|
// so we need to update the display before that.
|
|
|
|
_controls->updateDisplay();
|
|
|
|
|
2021-05-29 14:13:12 +00:00
|
|
|
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();
|
|
|
|
}
|
2021-06-07 05:28:53 +00:00
|
|
|
|
|
|
|
_lastAudioMsgId = current;
|
2021-05-29 14:13:12 +00:00
|
|
|
}, _lifetime);
|
|
|
|
|
2021-06-08 01:14:54 +00:00
|
|
|
mediaPlayer->playlistChanges(
|
|
|
|
type
|
|
|
|
) | rpl::start_with_next([=] {
|
|
|
|
_controls->setIsNextEnabled(mediaPlayer->nextAvailable(type));
|
|
|
|
_controls->setIsPreviousEnabled(mediaPlayer->previousAvailable(type));
|
|
|
|
}, _lifetime);
|
|
|
|
|
2021-12-26 18:49:35 +00:00
|
|
|
using Media::Player::RepeatMode;
|
|
|
|
using Media::Player::OrderMode;
|
|
|
|
|
|
|
|
Core::App().settings().playerRepeatModeValue(
|
|
|
|
) | rpl::start_with_next([=](RepeatMode mode) {
|
|
|
|
_controls->setLoopStatus(RepeatModeToLoopStatus(mode));
|
|
|
|
}, _lifetime);
|
|
|
|
|
|
|
|
Core::App().settings().playerOrderModeValue(
|
|
|
|
) | rpl::start_with_next([=](OrderMode mode) {
|
|
|
|
if (mode != OrderMode::Shuffle) {
|
|
|
|
_lastOrderMode = mode;
|
|
|
|
}
|
|
|
|
_controls->setShuffle(mode == OrderMode::Shuffle);
|
|
|
|
}, _lifetime);
|
|
|
|
|
2021-05-29 14:13:12 +00:00
|
|
|
_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;
|
2021-06-06 19:49:15 +00:00
|
|
|
case Command::Raise: controller->widget()->showFromTray(); break;
|
2021-12-26 18:49:35 +00:00
|
|
|
case Command::LoopNone: {
|
|
|
|
Core::App().settings().setPlayerRepeatMode(RepeatMode::None);
|
|
|
|
Core::App().saveSettingsDelayed();
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
case Command::LoopTrack: {
|
|
|
|
Core::App().settings().setPlayerRepeatMode(RepeatMode::One);
|
|
|
|
Core::App().saveSettingsDelayed();
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
case Command::LoopPlaylist: {
|
|
|
|
Core::App().settings().setPlayerRepeatMode(RepeatMode::All);
|
|
|
|
Core::App().saveSettingsDelayed();
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
case Command::Shuffle: {
|
|
|
|
const auto current = Core::App().settings().playerOrderMode();
|
|
|
|
Core::App().settings().setPlayerOrderMode((current == OrderMode::Shuffle)
|
|
|
|
? _lastOrderMode
|
|
|
|
: OrderMode::Shuffle);
|
|
|
|
Core::App().saveSettingsDelayed();
|
|
|
|
break;
|
|
|
|
}
|
2021-06-06 19:49:15 +00:00
|
|
|
case Command::Quit: {
|
|
|
|
if (const auto main = controller->widget()->sessionContent()) {
|
|
|
|
main->closeBothPlayers();
|
|
|
|
}
|
|
|
|
break;
|
|
|
|
}
|
2021-05-29 14:13:12 +00:00
|
|
|
}
|
|
|
|
}, _lifetime);
|
|
|
|
|
2021-06-04 22:49:24 +00:00
|
|
|
if (_controls->seekingSupported()) {
|
2021-06-07 05:40:50 +00:00
|
|
|
mediaPlayer->seekingChanges(
|
|
|
|
type
|
|
|
|
) | rpl::filter([](Media::Player::Instance::Seeking seeking) {
|
|
|
|
return (seeking == Media::Player::Instance::Seeking::Finish);
|
|
|
|
}) | rpl::map([=] {
|
|
|
|
return mediaPlayer->getState(type).position;
|
|
|
|
}) | rpl::distinct_until_changed(
|
|
|
|
) | rpl::start_with_next([=](int position) {
|
|
|
|
_controls->setPosition(position);
|
|
|
|
_controls->updateDisplay();
|
|
|
|
}, _lifetime);
|
|
|
|
|
2021-06-04 22:49:24 +00:00
|
|
|
_controls->seekRequests(
|
|
|
|
) | rpl::start_with_next([=](float64 progress) {
|
|
|
|
mediaPlayer->finishSeeking(type, progress);
|
|
|
|
}, _lifetime);
|
2021-06-07 05:40:50 +00:00
|
|
|
|
|
|
|
_controls->updatePositionRequests(
|
|
|
|
) | rpl::start_with_next([=] {
|
|
|
|
_controls->setPosition(mediaPlayer->getState(type).position);
|
|
|
|
}, _lifetime);
|
2021-06-04 22:49:24 +00:00
|
|
|
}
|
|
|
|
|
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);
|
|
|
|
|
2021-06-06 20:34:32 +00:00
|
|
|
if (_controls->volumeSupported()) {
|
|
|
|
rpl::single(
|
|
|
|
Core::App().settings().songVolume()
|
|
|
|
) | rpl::then(
|
|
|
|
Core::App().settings().songVolumeChanges()
|
|
|
|
) | rpl::start_with_next([=](float64 volume) {
|
|
|
|
_controls->setVolume(volume);
|
|
|
|
}, _lifetime);
|
|
|
|
|
|
|
|
_controls->volumeChangeRequests(
|
|
|
|
) | rpl::start_with_next([](float64 volume) {
|
2021-09-05 20:48:37 +00:00
|
|
|
Player::mixer()->setSongVolume(volume);
|
2021-09-06 12:40:11 +00:00
|
|
|
if (volume > 0) {
|
|
|
|
Core::App().settings().setRememberedSongVolume(volume);
|
|
|
|
}
|
2021-06-06 20:34:32 +00:00
|
|
|
Core::App().settings().setSongVolume(volume);
|
|
|
|
}, _lifetime);
|
|
|
|
}
|
|
|
|
|
2021-05-29 14:13:12 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
SystemMediaControlsManager::~SystemMediaControlsManager() = default;
|
|
|
|
|
2021-06-08 18:58:34 +00:00
|
|
|
} // namespace Media
|