2016-07-11 18:05:46 +00:00
|
|
|
/*
|
|
|
|
This file is part of Telegram Desktop,
|
|
|
|
the official desktop version of Telegram messaging app, see https://telegram.org
|
|
|
|
|
|
|
|
Telegram Desktop is free software: you can redistribute it and/or modify
|
|
|
|
it under the terms of the GNU General Public License as published by
|
|
|
|
the Free Software Foundation, either version 3 of the License, or
|
|
|
|
(at your option) any later version.
|
|
|
|
|
|
|
|
It is distributed in the hope that it will be useful,
|
|
|
|
but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
|
|
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
|
|
|
GNU General Public License for more details.
|
|
|
|
|
|
|
|
In addition, as a special exception, the copyright holders give permission
|
|
|
|
to link the code of portions of this program with the OpenSSL library.
|
|
|
|
|
|
|
|
Full license: https://github.com/telegramdesktop/tdesktop/blob/master/LICENSE
|
2017-01-11 18:31:31 +00:00
|
|
|
Copyright (c) 2014-2017 John Preston, https://desktop.telegram.org
|
2016-07-11 18:05:46 +00:00
|
|
|
*/
|
|
|
|
#include "media/view/media_clip_playback.h"
|
|
|
|
|
|
|
|
#include "styles/style_mediaview.h"
|
|
|
|
#include "media/media_audio.h"
|
|
|
|
|
|
|
|
namespace Media {
|
|
|
|
namespace Clip {
|
2017-05-18 17:20:07 +00:00
|
|
|
namespace {
|
|
|
|
|
|
|
|
constexpr auto kPlaybackAnimationDurationMs = TimeMs(200);
|
|
|
|
|
|
|
|
} // namespace
|
2016-07-11 18:05:46 +00:00
|
|
|
|
2017-05-18 16:10:39 +00:00
|
|
|
Playback::Playback() : _a_value(animation(this, &Playback::step_value)) {
|
2016-07-11 18:05:46 +00:00
|
|
|
}
|
|
|
|
|
2017-01-24 21:24:39 +00:00
|
|
|
void Playback::updateState(const Player::TrackState &state) {
|
2017-05-03 13:01:15 +00:00
|
|
|
qint64 position = 0, length = state.length;
|
2016-07-11 18:05:46 +00:00
|
|
|
|
2017-05-18 16:10:39 +00:00
|
|
|
auto wasInLoadingState = _inLoadingState;
|
|
|
|
if (wasInLoadingState) {
|
|
|
|
_inLoadingState = false;
|
|
|
|
if (_inLoadingStateChanged) {
|
|
|
|
_inLoadingStateChanged(false);
|
|
|
|
}
|
|
|
|
}
|
2016-10-14 17:10:15 +00:00
|
|
|
|
2017-01-24 21:24:39 +00:00
|
|
|
_playing = !Player::IsStopped(state.state);
|
2017-05-21 13:16:39 +00:00
|
|
|
if (Player::IsStoppedAtEnd(state.state)) {
|
2017-05-03 13:01:15 +00:00
|
|
|
position = state.length;
|
2017-05-21 13:16:39 +00:00
|
|
|
} else if (!Player::IsStoppedOrStopping(state.state)) {
|
|
|
|
position = state.position;
|
2016-07-11 18:05:46 +00:00
|
|
|
} else {
|
|
|
|
position = 0;
|
|
|
|
}
|
|
|
|
|
2017-05-18 16:10:39 +00:00
|
|
|
auto progress = 0.;
|
2017-05-03 13:01:15 +00:00
|
|
|
if (position > length) {
|
2016-07-12 12:28:07 +00:00
|
|
|
progress = 1.;
|
2017-05-03 13:01:15 +00:00
|
|
|
} else if (length) {
|
2017-05-18 17:20:07 +00:00
|
|
|
progress = snap(float64(position) / length, 0., 1.);
|
2016-07-11 18:05:46 +00:00
|
|
|
}
|
2017-05-18 17:20:07 +00:00
|
|
|
auto animatedPosition = position + (state.frequency * kPlaybackAnimationDurationMs / 1000);
|
|
|
|
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
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2016-10-13 09:12:12 +00:00
|
|
|
void Playback::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);
|
|
|
|
}
|
|
|
|
|
|
|
|
float64 Playback::value() const {
|
2017-05-18 17:20:07 +00:00
|
|
|
return qMin(a_value.current(), 1.);
|
2017-05-18 16:10:39 +00:00
|
|
|
}
|
|
|
|
|
2017-05-24 14:36:58 +00:00
|
|
|
float64 Playback::value(TimeMs ms) {
|
|
|
|
_a_value.step(ms);
|
|
|
|
return value();
|
|
|
|
}
|
|
|
|
|
2017-05-18 16:10:39 +00:00
|
|
|
void Playback::setValue(float64 value, bool animated) {
|
|
|
|
if (animated) {
|
|
|
|
a_value.start(value);
|
|
|
|
_a_value.start();
|
|
|
|
} else {
|
|
|
|
a_value = anim::value(value, value);
|
|
|
|
_a_value.stop();
|
|
|
|
}
|
|
|
|
if (_valueChanged) {
|
|
|
|
_valueChanged(a_value.current());
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
void Playback::step_value(float64 ms, bool timer) {
|
2017-05-18 17:20:07 +00:00
|
|
|
auto dt = ms / kPlaybackAnimationDurationMs;
|
|
|
|
if (dt >= 1.) {
|
2017-05-18 16:10:39 +00:00
|
|
|
_a_value.stop();
|
|
|
|
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
|
|
|
}
|
|
|
|
if (timer && _valueChanged) {
|
|
|
|
_valueChanged(a_value.current());
|
|
|
|
}
|
2016-10-13 09:12:12 +00:00
|
|
|
}
|
|
|
|
|
2016-07-11 18:05:46 +00:00
|
|
|
} // namespace Clip
|
|
|
|
} // namespace Media
|