tdesktop/Telegram/SourceFiles/media/view/media_view_playback_progres...

156 lines
4.1 KiB
C++
Raw Normal View History

2016-07-11 18:05:46 +00:00
/*
This file is part of Telegram Desktop,
the official desktop application for the Telegram messaging service.
2016-07-11 18:05:46 +00:00
For license and copyright information please follow this link:
https://github.com/telegramdesktop/tdesktop/blob/master/LEGAL
2016-07-11 18:05:46 +00:00
*/
2019-02-27 11:36:19 +00:00
#include "media/view/media_view_playback_progress.h"
2016-07-11 18:05:46 +00:00
#include "media/audio/media_audio.h"
2016-07-11 18:05:46 +00:00
#include "styles/style_mediaview.h"
namespace Media {
2019-02-27 11:36:19 +00:00
namespace View {
namespace {
constexpr auto kPlaybackAnimationDurationMs = crl::time(200);
} // namespace
2016-07-11 18:05:46 +00:00
PlaybackProgress::PlaybackProgress()
2019-04-01 14:53:18 +00:00
: _valueAnimation([=](crl::time now) {
return valueAnimationCallback(now);
})
, _receivedTillAnimation([=](crl::time now) {
return receivedTillAnimationCallback(now);
}) {
2016-07-11 18:05:46 +00:00
}
2019-02-27 11:36:19 +00:00
void PlaybackProgress::updateState(const Player::TrackState &state) {
_playing = !Player::IsStopped(state.state);
const auto length = state.length;
const auto position = Player::IsStoppedAtEnd(state.state)
? state.length
: Player::IsStoppedOrStopping(state.state)
? 0
: state.position;
const auto receivedTill = (length && state.receivedTill > position)
? state.receivedTill
: -1;
const auto wasInLoadingState = _inLoadingState;
if (wasInLoadingState) {
_inLoadingState = false;
if (_inLoadingStateChanged) {
_inLoadingStateChanged(false);
}
}
const auto progress = (position > length)
? 1.
: length
? snap(float64(position) / length, 0., 1.)
: 0.;
const auto receivedTillProgress = (receivedTill > position)
? snap(float64(receivedTill) / length, 0., 1.)
: -1.;
const auto animatedPosition = position + (state.frequency * kPlaybackAnimationDurationMs / 1000);
const auto animatedProgress = length ? qMax(float64(animatedPosition) / length, 0.) : 0.;
if (length != _length || position != _position || wasInLoadingState) {
if (auto animated = (length && _length && animatedProgress > value())) {
setValue(animatedProgress, animated);
} else {
setValue(progress, animated);
}
2016-07-11 18:05:46 +00:00
_position = position;
_length = length;
2016-07-11 18:05:46 +00:00
}
if (receivedTill != _receivedTill) {
setReceivedTill(receivedTillProgress);
_receivedTill = receivedTill;
}
2016-07-11 18:05:46 +00:00
}
2019-02-27 11:36:19 +00:00
void PlaybackProgress::updateLoadingState(float64 progress) {
if (!_inLoadingState) {
_inLoadingState = true;
if (_inLoadingStateChanged) {
_inLoadingStateChanged(true);
}
}
auto animated = (progress > value());
setValue(progress, animated);
}
2019-02-27 11:36:19 +00:00
float64 PlaybackProgress::value() const {
return qMin(a_value.current(), 1.);
}
2019-02-27 11:36:19 +00:00
void PlaybackProgress::setValue(float64 value, bool animated) {
if (animated) {
valueAnimationCallback(crl::now());
a_value.start(value);
2019-04-01 14:53:18 +00:00
_valueAnimation.start();
} else {
a_value = anim::value(value, value);
2019-04-01 14:53:18 +00:00
_valueAnimation.stop();
}
emitUpdatedValue();
}
void PlaybackProgress::setReceivedTill(float64 value) {
const auto current = a_receivedTill.current();
if (value > current && current > 0.) {
receivedTillAnimationCallback(crl::now());
a_receivedTill.start(value);
2019-04-01 14:53:18 +00:00
_receivedTillAnimation.start();
} else if (value > a_value.current()) {
a_receivedTill = anim::value(a_value.current(), value);
2019-04-01 14:53:18 +00:00
_receivedTillAnimation.start();
} else {
a_receivedTill = anim::value(-1., -1.);
2019-04-01 14:53:18 +00:00
_receivedTillAnimation.stop();
}
emitUpdatedValue();
}
bool PlaybackProgress::valueAnimationCallback(float64 now) {
2019-04-01 14:53:18 +00:00
const auto time = (now - _valueAnimation.started());
2019-03-07 11:35:28 +00:00
const auto dt = anim::Disabled()
? 1.
: (time / kPlaybackAnimationDurationMs);
if (dt >= 1.) {
a_value.finish();
} else {
a_value.update(dt, anim::linear);
}
2019-03-07 11:35:28 +00:00
emitUpdatedValue();
return (dt < 1.);
}
bool PlaybackProgress::receivedTillAnimationCallback(float64 now) {
2019-04-01 14:53:18 +00:00
const auto time = now - _receivedTillAnimation.started();
2019-03-07 11:35:28 +00:00
const auto dt = anim::Disabled()
? 1.
: (time / kPlaybackAnimationDurationMs);
if (dt >= 1.) {
a_receivedTill.finish();
} else {
a_receivedTill.update(dt, anim::linear);
}
2019-03-07 11:35:28 +00:00
emitUpdatedValue();
return (dt < 1.);
}
void PlaybackProgress::emitUpdatedValue() {
if (_valueChanged) {
const auto value = a_value.current();
const auto receivedTill = a_receivedTill.current();
_valueChanged(value, std::max(value, receivedTill));
}
}
2019-02-27 11:36:19 +00:00
} // namespace View
2016-07-11 18:05:46 +00:00
} // namespace Media