From 846499a4fb922671c577fc1e67842d6d614e9029 Mon Sep 17 00:00:00 2001 From: John Preston Date: Tue, 12 Mar 2019 09:09:53 +0400 Subject: [PATCH] Fix volume saving to settings. --- .../SourceFiles/media/audio/media_audio.cpp | 145 +++++++++--------- .../SourceFiles/media/audio/media_audio.h | 4 +- .../media/player/media_player_instance.cpp | 12 +- .../player/media_player_volume_controller.cpp | 2 + .../media/view/media_view_overlay_widget.cpp | 5 + .../media/view/media_view_overlay_widget.h | 1 + .../view/media_view_playback_controls.cpp | 6 +- .../media/view/media_view_playback_controls.h | 1 + .../ui/widgets/continuous_sliders.cpp | 2 +- 9 files changed, 95 insertions(+), 83 deletions(-) diff --git a/Telegram/SourceFiles/media/audio/media_audio.cpp b/Telegram/SourceFiles/media/audio/media_audio.cpp index 905eb7c982..3f2770dbd2 100644 --- a/Telegram/SourceFiles/media/audio/media_audio.cpp +++ b/Telegram/SourceFiles/media/audio/media_audio.cpp @@ -775,16 +775,15 @@ bool Mixer::fadedStop(AudioMsgId::Type type, bool *fadedStart) { return false; } -void Mixer::play(const AudioMsgId &audio, crl::time positionMs) { - setSongVolume(Global::SongVolume()); - play(audio, nullptr, positionMs); -} - void Mixer::play( const AudioMsgId &audio, std::unique_ptr externalData, crl::time positionMs) { - Expects((externalData != nullptr) == (audio.externalPlayId() != 0)); + Expects(externalData != nullptr); + Expects(audio.externalPlayId() != 0); + + setSongVolume(Global::SongVolume()); + setVideoVolume(Global::VideoVolume()); auto type = audio.type(); AudioMsgId stopped; @@ -1049,72 +1048,74 @@ void Mixer::resume(const AudioMsgId &audio, bool fast) { } if (current) emit updated(current); } - -void Mixer::seek(AudioMsgId::Type type, crl::time positionMs) { - QMutexLocker lock(&AudioMutex); - - const auto current = trackForType(type); - const auto audio = current->state.id; - - Audio::AttachToDevice(); - const auto streamCreated = current->isStreamCreated(); - const auto position = (positionMs * current->frequency) / 1000LL; - const auto fastSeek = [&] { - const auto loadedStart = current->bufferedPosition; - const auto loadedLength = current->bufferedLength; - const auto skipBack = (current->loaded ? 0 : kDefaultFrequency); - const auto availableEnd = loadedStart + loadedLength - skipBack; - if (position < loadedStart) { - return false; - } else if (position >= availableEnd) { - return false; - } else if (!streamCreated) { - return false; - } else if (IsStoppedOrStopping(current->state.state)) { - return false; - } - return true; - }(); - if (fastSeek) { - alSourcei(current->stream.source, AL_SAMPLE_OFFSET, position - current->bufferedPosition); - if (!checkCurrentALError(type)) return; - - alSourcef(current->stream.source, AL_GAIN, ComputeVolume(type)); - if (!checkCurrentALError(type)) return; - - resetFadeStartPosition(type, position - current->bufferedPosition); - } else { - setStoppedState(current); - } - switch (current->state.state) { - case State::Pausing: - case State::Paused: - case State::PausedAtEnd: { - if (current->state.state == State::PausedAtEnd) { - current->state.state = State::Paused; - } - lock.unlock(); - return resume(audio, true); - } break; - case State::Starting: - case State::Resuming: - case State::Playing: { - current->state.state = State::Pausing; - resetFadeStartPosition(type); - if (type == AudioMsgId::Type::Voice) { - emit unsuppressSong(); - } - } break; - case State::Stopping: - case State::Stopped: - case State::StoppedAtEnd: - case State::StoppedAtError: - case State::StoppedAtStart: { - lock.unlock(); - } return play(audio, positionMs); - } - emit faderOnTimer(); -} +// +// Right now all the music is played in the streaming player. +// +//void Mixer::seek(AudioMsgId::Type type, crl::time positionMs) { +// QMutexLocker lock(&AudioMutex); +// +// const auto current = trackForType(type); +// const auto audio = current->state.id; +// +// Audio::AttachToDevice(); +// const auto streamCreated = current->isStreamCreated(); +// const auto position = (positionMs * current->frequency) / 1000LL; +// const auto fastSeek = [&] { +// const auto loadedStart = current->bufferedPosition; +// const auto loadedLength = current->bufferedLength; +// const auto skipBack = (current->loaded ? 0 : kDefaultFrequency); +// const auto availableEnd = loadedStart + loadedLength - skipBack; +// if (position < loadedStart) { +// return false; +// } else if (position >= availableEnd) { +// return false; +// } else if (!streamCreated) { +// return false; +// } else if (IsStoppedOrStopping(current->state.state)) { +// return false; +// } +// return true; +// }(); +// if (fastSeek) { +// alSourcei(current->stream.source, AL_SAMPLE_OFFSET, position - current->bufferedPosition); +// if (!checkCurrentALError(type)) return; +// +// alSourcef(current->stream.source, AL_GAIN, ComputeVolume(type)); +// if (!checkCurrentALError(type)) return; +// +// resetFadeStartPosition(type, position - current->bufferedPosition); +// } else { +// setStoppedState(current); +// } +// switch (current->state.state) { +// case State::Pausing: +// case State::Paused: +// case State::PausedAtEnd: { +// if (current->state.state == State::PausedAtEnd) { +// current->state.state = State::Paused; +// } +// lock.unlock(); +// return resume(audio, true); +// } break; +// case State::Starting: +// case State::Resuming: +// case State::Playing: { +// current->state.state = State::Pausing; +// resetFadeStartPosition(type); +// if (type == AudioMsgId::Type::Voice) { +// emit unsuppressSong(); +// } +// } break; +// case State::Stopping: +// case State::Stopped: +// case State::StoppedAtEnd: +// case State::StoppedAtError: +// case State::StoppedAtStart: { +// lock.unlock(); +// } return play(audio, positionMs); +// } +// emit faderOnTimer(); +//} void Mixer::stop(const AudioMsgId &audio) { AudioMsgId current; diff --git a/Telegram/SourceFiles/media/audio/media_audio.h b/Telegram/SourceFiles/media/audio/media_audio.h index 50737abfff..442421727b 100644 --- a/Telegram/SourceFiles/media/audio/media_audio.h +++ b/Telegram/SourceFiles/media/audio/media_audio.h @@ -128,14 +128,12 @@ class Mixer : public QObject, private base::Subscriber { public: explicit Mixer(not_null instance); - void play(const AudioMsgId &audio, crl::time positionMs = 0); void play( const AudioMsgId &audio, std::unique_ptr externalData, - crl::time positionMs = 0); + crl::time positionMs); void pause(const AudioMsgId &audio, bool fast = false); void resume(const AudioMsgId &audio, bool fast = false); - void seek(AudioMsgId::Type type, crl::time positionMs); // type == AudioMsgId::Type::Song void stop(const AudioMsgId &audio); void stop(const AudioMsgId &audio, State state); diff --git a/Telegram/SourceFiles/media/player/media_player_instance.cpp b/Telegram/SourceFiles/media/player/media_player_instance.cpp index cb5ba2b87c..968b9b4f36 100644 --- a/Telegram/SourceFiles/media/player/media_player_instance.cpp +++ b/Telegram/SourceFiles/media/player/media_player_instance.cpp @@ -556,11 +556,13 @@ void Instance::finishSeeking(AudioMsgId::Type type, float64 progress) { position)); emitUpdate(type); } - } else { - const auto state = getState(type); - if (state.id && state.length && state.frequency) { - mixer()->seek(type, qRound(progress * state.length * 1000. / state.frequency)); - } + // + // Right now all music is played in streaming player. + //} else { + // const auto state = getState(type); + // if (state.id && state.length && state.frequency) { + // mixer()->seek(type, qRound(progress * state.length * 1000. / state.frequency)); + // } } } cancelSeeking(type); diff --git a/Telegram/SourceFiles/media/player/media_player_volume_controller.cpp b/Telegram/SourceFiles/media/player/media_player_volume_controller.cpp index a5f10e72d1..5191ace157 100644 --- a/Telegram/SourceFiles/media/player/media_player_volume_controller.cpp +++ b/Telegram/SourceFiles/media/player/media_player_volume_controller.cpp @@ -14,6 +14,7 @@ https://github.com/telegramdesktop/tdesktop/blob/master/LEGAL #include "styles/style_media_player.h" #include "styles/style_widgets.h" #include "mainwindow.h" +#include "auth_session.h" namespace Media { namespace Player { @@ -29,6 +30,7 @@ VolumeController::VolumeController(QWidget *parent) : TWidget(parent) Global::SetRememberedSongVolume(volume); } applyVolumeChange(volume); + Auth().saveSettingsDelayed(); }); subscribe(Global::RefSongVolumeChanged(), [this] { if (!_slider->isChanging()) { diff --git a/Telegram/SourceFiles/media/view/media_view_overlay_widget.cpp b/Telegram/SourceFiles/media/view/media_view_overlay_widget.cpp index 0836c33596..55aab77aa5 100644 --- a/Telegram/SourceFiles/media/view/media_view_overlay_widget.cpp +++ b/Telegram/SourceFiles/media/view/media_view_overlay_widget.cpp @@ -2297,6 +2297,11 @@ void OverlayWidget::playbackControlsVolumeChanged(float64 volume) { Global::SetVideoVolume(volume); updateMixerVideoVolume(); Global::RefVideoVolumeChanged().notify(); + Auth().saveSettingsDelayed(); +} + +float64 OverlayWidget::playbackControlsCurrentVolume() { + return Global::VideoVolume(); } void OverlayWidget::playbackToggleFullScreen() { diff --git a/Telegram/SourceFiles/media/view/media_view_overlay_widget.h b/Telegram/SourceFiles/media/view/media_view_overlay_widget.h index 7c5a000989..74d396161a 100644 --- a/Telegram/SourceFiles/media/view/media_view_overlay_widget.h +++ b/Telegram/SourceFiles/media/view/media_view_overlay_widget.h @@ -163,6 +163,7 @@ private: void playbackControlsSeekProgress(crl::time position) override; void playbackControlsSeekFinished(crl::time position) override; void playbackControlsVolumeChanged(float64 volume) override; + float64 playbackControlsCurrentVolume() override; void playbackControlsToFullScreen() override; void playbackControlsFromFullScreen() override; void playbackPauseResume(); diff --git a/Telegram/SourceFiles/media/view/media_view_playback_controls.cpp b/Telegram/SourceFiles/media/view/media_view_playback_controls.cpp index c71aa9021a..4bc467716f 100644 --- a/Telegram/SourceFiles/media/view/media_view_playback_controls.cpp +++ b/Telegram/SourceFiles/media/view/media_view_playback_controls.cpp @@ -19,7 +19,9 @@ https://github.com/telegramdesktop/tdesktop/blob/master/LEGAL namespace Media { namespace View { -PlaybackControls::PlaybackControls(QWidget *parent, not_null delegate) +PlaybackControls::PlaybackControls( + QWidget *parent, + not_null delegate) : RpWidget(parent) , _delegate(delegate) , _playPauseResume(this, st::mediaviewPlayButton) @@ -38,7 +40,7 @@ PlaybackControls::PlaybackControls(QWidget *parent, not_null delegat fadeUpdated(opacity); }); - _volumeController->setValue(Global::VideoVolume()); + _volumeController->setValue(_delegate->playbackControlsCurrentVolume()); _volumeController->setChangeProgressCallback([=](float64 value) { _delegate->playbackControlsVolumeChanged(value); }); diff --git a/Telegram/SourceFiles/media/view/media_view_playback_controls.h b/Telegram/SourceFiles/media/view/media_view_playback_controls.h index 44ca3b5a94..a3b47a5af3 100644 --- a/Telegram/SourceFiles/media/view/media_view_playback_controls.h +++ b/Telegram/SourceFiles/media/view/media_view_playback_controls.h @@ -34,6 +34,7 @@ public: virtual void playbackControlsSeekProgress(crl::time position) = 0; virtual void playbackControlsSeekFinished(crl::time position) = 0; virtual void playbackControlsVolumeChanged(float64 volume) = 0; + [[nodiscard]] virtual float64 playbackControlsCurrentVolume() = 0; virtual void playbackControlsToFullScreen() = 0; virtual void playbackControlsFromFullScreen() = 0; }; diff --git a/Telegram/SourceFiles/ui/widgets/continuous_sliders.cpp b/Telegram/SourceFiles/ui/widgets/continuous_sliders.cpp index 825e6dd23a..ff3bf3c3b4 100644 --- a/Telegram/SourceFiles/ui/widgets/continuous_sliders.cpp +++ b/Telegram/SourceFiles/ui/widgets/continuous_sliders.cpp @@ -50,7 +50,7 @@ QRect ContinuousSlider::getSeekRect() const { } void ContinuousSlider::setValue(float64 value) { - setValue(value, value); + setValue(value, -1); } void ContinuousSlider::setValue(float64 value, float64 receivedTill) {