2016-07-11 18:05:46 +00:00
|
|
|
/*
|
|
|
|
This file is part of Telegram Desktop,
|
2018-01-03 10:23:14 +00:00
|
|
|
the official desktop application for the Telegram messaging service.
|
2016-07-11 18:05:46 +00:00
|
|
|
|
2018-01-03 10:23:14 +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
|
|
|
|
2019-02-13 12:36:59 +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 {
|
2017-05-18 17:20:07 +00:00
|
|
|
namespace {
|
|
|
|
|
2019-02-19 06:57:53 +00:00
|
|
|
constexpr auto kPlaybackAnimationDurationMs = crl::time(200);
|
2017-05-18 17:20:07 +00:00
|
|
|
|
|
|
|
} // namespace
|
2016-07-11 18:05:46 +00:00
|
|
|
|
2019-03-05 11:06:54 +00:00
|
|
|
PlaybackProgress::PlaybackProgress()
|
2019-04-01 14:53:18 +00:00
|
|
|
: _valueAnimation([=](crl::time now) {
|
|
|
|
return valueAnimationCallback(now);
|
|
|
|
})
|
2019-05-31 11:45:35 +00:00
|
|
|
, _availableTillAnimation([=](crl::time now) {
|
|
|
|
return availableTillAnimationCallback(now);
|
2019-04-01 14:53:18 +00:00
|
|
|
}) {
|
2016-07-11 18:05:46 +00:00
|
|
|
}
|
|
|
|
|
2019-05-31 11:45:35 +00:00
|
|
|
void PlaybackProgress::updateState(
|
|
|
|
const Player::TrackState &state,
|
|
|
|
float64 loadedTillPercent) {
|
2019-03-05 11:06:54 +00:00
|
|
|
_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;
|
2019-05-31 11:45:35 +00:00
|
|
|
const auto loadedTill = (loadedTillPercent != 0.)
|
|
|
|
? int64(std::floor(loadedTillPercent * length))
|
|
|
|
: -1;
|
|
|
|
const auto availableTill = (length && loadedTill > position)
|
|
|
|
? std::max(receivedTill, loadedTill)
|
|
|
|
: receivedTill;
|
2019-03-05 11:06:54 +00:00
|
|
|
|
|
|
|
const auto wasInLoadingState = _inLoadingState;
|
2017-05-18 16:10:39 +00:00
|
|
|
if (wasInLoadingState) {
|
|
|
|
_inLoadingState = false;
|
|
|
|
if (_inLoadingStateChanged) {
|
|
|
|
_inLoadingStateChanged(false);
|
|
|
|
}
|
|
|
|
}
|
2016-10-14 17:10:15 +00:00
|
|
|
|
2019-03-05 11:06:54 +00:00
|
|
|
const auto progress = (position > length)
|
|
|
|
? 1.
|
|
|
|
: length
|
|
|
|
? snap(float64(position) / length, 0., 1.)
|
|
|
|
: 0.;
|
2019-05-31 11:45:35 +00:00
|
|
|
const auto availableTillProgress = (availableTill > position)
|
|
|
|
? snap(float64(availableTill) / length, 0., 1.)
|
2019-03-05 11:06:54 +00:00
|
|
|
: -1.;
|
|
|
|
const auto animatedPosition = position + (state.frequency * kPlaybackAnimationDurationMs / 1000);
|
|
|
|
const auto animatedProgress = length ? qMax(float64(animatedPosition) / length, 0.) : 0.;
|
2017-05-18 16:10:39 +00:00
|
|
|
if (length != _length || position != _position || wasInLoadingState) {
|
2017-05-18 17:20:07 +00:00
|
|
|
if (auto animated = (length && _length && animatedProgress > value())) {
|
|
|
|
setValue(animatedProgress, animated);
|
|
|
|
} else {
|
|
|
|
setValue(progress, animated);
|
|
|
|
}
|
2016-07-11 18:05:46 +00:00
|
|
|
_position = position;
|
2017-05-03 13:01:15 +00:00
|
|
|
_length = length;
|
2016-07-11 18:05:46 +00:00
|
|
|
}
|
2019-05-31 11:45:35 +00:00
|
|
|
if (availableTill != _availableTill) {
|
|
|
|
setAvailableTill(availableTillProgress);
|
|
|
|
_availableTill = availableTill;
|
2019-03-05 11:06:54 +00:00
|
|
|
}
|
2016-07-11 18:05:46 +00:00
|
|
|
}
|
|
|
|
|
2019-02-27 11:36:19 +00:00
|
|
|
void PlaybackProgress::updateLoadingState(float64 progress) {
|
2017-05-18 16:10:39 +00:00
|
|
|
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 {
|
2017-05-18 17:20:07 +00:00
|
|
|
return qMin(a_value.current(), 1.);
|
2017-05-18 16:10:39 +00:00
|
|
|
}
|
|
|
|
|
2019-02-27 11:36:19 +00:00
|
|
|
void PlaybackProgress::setValue(float64 value, bool animated) {
|
2017-05-18 16:10:39 +00:00
|
|
|
if (animated) {
|
2019-03-08 12:35:04 +00:00
|
|
|
valueAnimationCallback(crl::now());
|
2017-05-18 16:10:39 +00:00
|
|
|
a_value.start(value);
|
2019-04-01 14:53:18 +00:00
|
|
|
_valueAnimation.start();
|
2017-05-18 16:10:39 +00:00
|
|
|
} else {
|
|
|
|
a_value = anim::value(value, value);
|
2019-04-01 14:53:18 +00:00
|
|
|
_valueAnimation.stop();
|
2017-05-18 16:10:39 +00:00
|
|
|
}
|
2019-03-05 11:06:54 +00:00
|
|
|
emitUpdatedValue();
|
|
|
|
}
|
|
|
|
|
2019-05-31 11:45:35 +00:00
|
|
|
void PlaybackProgress::setAvailableTill(float64 value) {
|
|
|
|
const auto current = a_availableTill.current();
|
2019-03-05 11:06:54 +00:00
|
|
|
if (value > current && current > 0.) {
|
2019-05-31 11:45:35 +00:00
|
|
|
availableTillAnimationCallback(crl::now());
|
|
|
|
a_availableTill.start(value);
|
|
|
|
_availableTillAnimation.start();
|
2019-03-05 11:06:54 +00:00
|
|
|
} else if (value > a_value.current()) {
|
2019-05-31 11:45:35 +00:00
|
|
|
a_availableTill = anim::value(a_value.current(), value);
|
|
|
|
_availableTillAnimation.start();
|
2019-03-05 11:06:54 +00:00
|
|
|
} else {
|
2019-05-31 11:45:35 +00:00
|
|
|
a_availableTill = anim::value(-1., -1.);
|
|
|
|
_availableTillAnimation.stop();
|
2017-05-18 16:10:39 +00:00
|
|
|
}
|
2019-03-05 11:06:54 +00:00
|
|
|
emitUpdatedValue();
|
2017-05-18 16:10:39 +00:00
|
|
|
}
|
|
|
|
|
2019-03-08 12:35:04 +00:00
|
|
|
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);
|
2017-05-18 17:20:07 +00:00
|
|
|
if (dt >= 1.) {
|
2017-05-18 16:10:39 +00:00
|
|
|
a_value.finish();
|
|
|
|
} else {
|
2017-05-18 17:20:07 +00:00
|
|
|
a_value.update(dt, anim::linear);
|
2017-05-18 16:10:39 +00:00
|
|
|
}
|
2019-03-07 11:35:28 +00:00
|
|
|
emitUpdatedValue();
|
2019-03-08 12:35:04 +00:00
|
|
|
return (dt < 1.);
|
2019-03-05 11:06:54 +00:00
|
|
|
}
|
|
|
|
|
2019-05-31 11:45:35 +00:00
|
|
|
bool PlaybackProgress::availableTillAnimationCallback(float64 now) {
|
|
|
|
const auto time = now - _availableTillAnimation.started();
|
2019-03-07 11:35:28 +00:00
|
|
|
const auto dt = anim::Disabled()
|
|
|
|
? 1.
|
|
|
|
: (time / kPlaybackAnimationDurationMs);
|
2019-03-05 11:06:54 +00:00
|
|
|
if (dt >= 1.) {
|
2019-05-31 11:45:35 +00:00
|
|
|
a_availableTill.finish();
|
2019-03-05 11:06:54 +00:00
|
|
|
} else {
|
2019-05-31 11:45:35 +00:00
|
|
|
a_availableTill.update(dt, anim::linear);
|
2019-03-05 11:06:54 +00:00
|
|
|
}
|
2019-03-07 11:35:28 +00:00
|
|
|
emitUpdatedValue();
|
2019-03-08 12:35:04 +00:00
|
|
|
return (dt < 1.);
|
2019-03-05 11:06:54 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
void PlaybackProgress::emitUpdatedValue() {
|
|
|
|
if (_valueChanged) {
|
|
|
|
const auto value = a_value.current();
|
2019-05-31 11:45:35 +00:00
|
|
|
const auto availableTill = a_availableTill.current();
|
|
|
|
_valueChanged(value, std::max(value, availableTill));
|
2017-05-18 16:10:39 +00:00
|
|
|
}
|
2016-10-13 09:12:12 +00:00
|
|
|
}
|
|
|
|
|
2019-02-27 11:36:19 +00:00
|
|
|
} // namespace View
|
2016-07-11 18:05:46 +00:00
|
|
|
} // namespace Media
|