2019-02-17 06:54:57 +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
|
|
|
|
*/
|
|
|
|
#pragma once
|
|
|
|
|
|
|
|
#include "media/streaming/media_streaming_common.h"
|
|
|
|
#include "media/streaming/media_streaming_file_delegate.h"
|
2019-02-17 11:08:29 +00:00
|
|
|
#include "base/weak_ptr.h"
|
|
|
|
#include "base/timer.h"
|
2019-02-17 06:54:57 +00:00
|
|
|
|
|
|
|
namespace Data {
|
|
|
|
class Session;
|
|
|
|
} // namespace Data
|
|
|
|
|
2019-02-28 21:03:25 +00:00
|
|
|
namespace Media {
|
|
|
|
namespace Player {
|
|
|
|
struct TrackState;
|
|
|
|
} // namespace Player
|
|
|
|
} // namespace Media
|
|
|
|
|
2019-02-17 06:54:57 +00:00
|
|
|
namespace Media {
|
|
|
|
namespace Streaming {
|
|
|
|
|
|
|
|
class Loader;
|
|
|
|
class File;
|
2019-02-17 11:08:29 +00:00
|
|
|
class AudioTrack;
|
|
|
|
class VideoTrack;
|
2019-02-17 06:54:57 +00:00
|
|
|
|
|
|
|
class Player final : private FileDelegate {
|
|
|
|
public:
|
2019-02-17 11:08:29 +00:00
|
|
|
// Public interfaces is used from the main thread.
|
2019-02-17 06:54:57 +00:00
|
|
|
Player(not_null<Data::Session*> owner, std::unique_ptr<Loader> loader);
|
|
|
|
|
2019-02-17 11:08:29 +00:00
|
|
|
// Because we remember 'this' in calls to crl::on_main.
|
|
|
|
Player(const Player &other) = delete;
|
|
|
|
Player &operator=(const Player &other) = delete;
|
|
|
|
|
2019-02-21 14:57:00 +00:00
|
|
|
void play(const PlaybackOptions &options);
|
2019-02-17 06:54:57 +00:00
|
|
|
void pause();
|
|
|
|
void resume();
|
|
|
|
void stop();
|
|
|
|
|
2019-03-04 11:28:52 +00:00
|
|
|
[[nodiscard]] bool active() const;
|
|
|
|
[[nodiscard]] bool ready() const;
|
2019-02-21 16:01:55 +00:00
|
|
|
|
2019-03-04 11:28:52 +00:00
|
|
|
[[nodiscard]] float64 speed() const;
|
2019-02-21 16:01:55 +00:00
|
|
|
void setSpeed(float64 speed); // 0.5 <= speed <= 2.
|
|
|
|
|
2019-02-17 11:08:29 +00:00
|
|
|
[[nodiscard]] bool playing() const;
|
2019-02-22 14:28:10 +00:00
|
|
|
[[nodiscard]] bool buffering() const;
|
2019-02-17 11:08:29 +00:00
|
|
|
[[nodiscard]] bool paused() const;
|
2019-03-05 13:56:27 +00:00
|
|
|
[[nodiscard]] std::optional<Error> failed() const;
|
2019-02-27 11:36:19 +00:00
|
|
|
[[nodiscard]] bool finished() const;
|
2019-02-17 11:08:29 +00:00
|
|
|
|
|
|
|
[[nodiscard]] rpl::producer<Update, Error> updates() const;
|
|
|
|
|
2019-03-26 08:54:51 +00:00
|
|
|
[[nodiscard]] QSize videoSize() const;
|
2019-02-17 11:08:29 +00:00
|
|
|
[[nodiscard]] QImage frame(const FrameRequest &request) const;
|
2019-02-17 06:54:57 +00:00
|
|
|
|
2019-02-28 21:03:25 +00:00
|
|
|
[[nodiscard]] Media::Player::TrackState prepareLegacyState() const;
|
|
|
|
|
2019-02-17 11:08:29 +00:00
|
|
|
[[nodiscard]] rpl::lifetime &lifetime();
|
2019-02-17 06:54:57 +00:00
|
|
|
|
|
|
|
~Player();
|
|
|
|
|
|
|
|
private:
|
2019-02-17 11:08:29 +00:00
|
|
|
enum class Stage {
|
|
|
|
Uninitialized,
|
|
|
|
Initializing,
|
|
|
|
Ready,
|
|
|
|
Started,
|
|
|
|
};
|
|
|
|
|
|
|
|
// Thread-safe.
|
2019-02-17 06:54:57 +00:00
|
|
|
not_null<FileDelegate*> delegate();
|
|
|
|
|
2019-02-17 11:08:29 +00:00
|
|
|
// FileDelegate methods are called only from the File thread.
|
2019-03-05 07:40:25 +00:00
|
|
|
bool fileReady(Stream &&video, Stream &&audio) override;
|
2019-03-05 13:56:27 +00:00
|
|
|
void fileError(Error error) override;
|
2019-02-21 13:40:09 +00:00
|
|
|
void fileWaitingForData() override;
|
2019-02-17 06:54:57 +00:00
|
|
|
bool fileProcessPacket(Packet &&packet) override;
|
|
|
|
bool fileReadMore() override;
|
|
|
|
|
2019-02-17 11:08:29 +00:00
|
|
|
// Called from the main thread.
|
|
|
|
void streamReady(Information &&information);
|
2019-03-05 13:56:27 +00:00
|
|
|
void streamFailed(Error error);
|
2019-02-21 14:57:00 +00:00
|
|
|
void start();
|
2019-02-17 11:08:29 +00:00
|
|
|
void provideStartInformation();
|
2019-03-05 13:56:27 +00:00
|
|
|
void fail(Error error);
|
2019-03-07 13:23:19 +00:00
|
|
|
void checkVideoStep();
|
|
|
|
void checkNextFrameRender();
|
|
|
|
void checkNextFrameAvailability();
|
2019-02-17 11:08:29 +00:00
|
|
|
void renderFrame(crl::time now);
|
2019-02-20 13:28:48 +00:00
|
|
|
void audioReceivedTill(crl::time position);
|
|
|
|
void audioPlayedTill(crl::time position);
|
|
|
|
void videoReceivedTill(crl::time position);
|
|
|
|
void videoPlayedTill(crl::time position);
|
|
|
|
|
2019-02-22 14:28:10 +00:00
|
|
|
void updatePausedState();
|
2019-02-26 14:09:08 +00:00
|
|
|
[[nodiscard]] bool trackReceivedEnough(
|
|
|
|
const TrackState &state,
|
|
|
|
crl::time amount) const;
|
|
|
|
[[nodiscard]] bool bothReceivedEnough(crl::time amount) const;
|
2019-02-28 21:03:25 +00:00
|
|
|
[[nodiscard]] bool receivedTillEnd() const;
|
2019-02-22 14:28:10 +00:00
|
|
|
void checkResumeFromWaitingForData();
|
2019-03-13 14:58:50 +00:00
|
|
|
[[nodiscard]] crl::time getCurrentReceivedTill(crl::time duration) const;
|
2019-03-05 11:06:54 +00:00
|
|
|
void savePreviousReceivedTill(
|
|
|
|
const PlaybackOptions &options,
|
|
|
|
crl::time previousReceivedTill);
|
2019-03-05 13:56:27 +00:00
|
|
|
[[nodiscard]] crl::time loadInAdvanceFor() const;
|
2019-02-22 14:28:10 +00:00
|
|
|
|
2019-03-13 14:58:50 +00:00
|
|
|
template <typename Track>
|
|
|
|
int durationByPacket(const Track &track, const Packet &packet);
|
|
|
|
|
|
|
|
// Valid after fileReady call ends. Thread-safe.
|
|
|
|
[[nodiscard]] crl::time computeAudioDuration() const;
|
|
|
|
[[nodiscard]] crl::time computeVideoDuration() const;
|
|
|
|
[[nodiscard]] crl::time computeTotalDuration() const;
|
|
|
|
void setDurationByPackets();
|
|
|
|
|
2019-02-20 13:28:48 +00:00
|
|
|
template <typename Track>
|
|
|
|
void trackReceivedTill(
|
|
|
|
const Track &track,
|
|
|
|
TrackState &state,
|
|
|
|
crl::time position);
|
|
|
|
|
2019-02-22 14:28:10 +00:00
|
|
|
template <typename Track>
|
|
|
|
void trackSendReceivedTill(
|
|
|
|
const Track &track,
|
|
|
|
TrackState &state);
|
|
|
|
|
2019-02-20 13:28:48 +00:00
|
|
|
template <typename Track>
|
|
|
|
void trackPlayedTill(
|
|
|
|
const Track &track,
|
|
|
|
TrackState &state,
|
|
|
|
crl::time position);
|
2019-02-17 11:08:29 +00:00
|
|
|
|
2019-02-17 06:54:57 +00:00
|
|
|
const std::unique_ptr<File> _file;
|
|
|
|
|
2019-02-21 11:15:44 +00:00
|
|
|
// Immutable while File is active after it is ready.
|
|
|
|
AudioMsgId _audioId;
|
2019-02-17 11:08:29 +00:00
|
|
|
std::unique_ptr<AudioTrack> _audio;
|
|
|
|
std::unique_ptr<VideoTrack> _video;
|
2019-02-21 11:15:44 +00:00
|
|
|
|
|
|
|
// Immutable while File is active.
|
2019-02-17 11:08:29 +00:00
|
|
|
base::has_weak_ptr _sessionGuard;
|
2019-03-05 07:40:25 +00:00
|
|
|
|
|
|
|
// Immutable while File is active except '.speed'.
|
|
|
|
// '.speed' is changed from the main thread.
|
2019-02-21 09:17:25 +00:00
|
|
|
PlaybackOptions _options;
|
2019-02-17 06:54:57 +00:00
|
|
|
|
2019-02-17 11:08:29 +00:00
|
|
|
// Belongs to the File thread while File is active.
|
|
|
|
bool _readTillEnd = false;
|
2019-02-21 13:40:09 +00:00
|
|
|
bool _waitingForData = false;
|
2019-02-17 11:08:29 +00:00
|
|
|
|
2019-02-26 14:09:08 +00:00
|
|
|
std::atomic<bool> _pauseReading = false;
|
|
|
|
|
2019-02-17 11:08:29 +00:00
|
|
|
// Belongs to the main thread.
|
|
|
|
Information _information;
|
|
|
|
Stage _stage = Stage::Uninitialized;
|
2019-03-05 13:56:27 +00:00
|
|
|
std::optional<Error> _lastFailure;
|
2019-02-22 14:28:10 +00:00
|
|
|
bool _pausedByUser = false;
|
|
|
|
bool _pausedByWaitingForData = false;
|
2019-02-17 11:08:29 +00:00
|
|
|
bool _paused = false;
|
2019-02-22 11:58:26 +00:00
|
|
|
bool _audioFinished = false;
|
|
|
|
bool _videoFinished = false;
|
2019-03-05 13:56:27 +00:00
|
|
|
bool _remoteLoader = false;
|
2019-02-17 06:54:57 +00:00
|
|
|
|
2019-02-21 09:17:25 +00:00
|
|
|
crl::time _startedTime = kTimeUnknown;
|
2019-02-21 14:57:00 +00:00
|
|
|
crl::time _pausedTime = kTimeUnknown;
|
2019-02-17 11:08:29 +00:00
|
|
|
crl::time _nextFrameTime = kTimeUnknown;
|
|
|
|
base::Timer _renderFrameTimer;
|
2019-02-17 06:54:57 +00:00
|
|
|
rpl::event_stream<Update, Error> _updates;
|
2019-02-22 14:28:10 +00:00
|
|
|
|
2019-03-05 13:56:27 +00:00
|
|
|
crl::time _totalDuration = kTimeUnknown;
|
2019-03-05 11:06:54 +00:00
|
|
|
crl::time _loopingShift = 0;
|
|
|
|
crl::time _previousReceivedTill = kTimeUnknown;
|
2019-03-13 14:58:50 +00:00
|
|
|
std::atomic<int> _durationByPackets = 0;
|
|
|
|
int _durationByLastAudioPacket = 0;
|
|
|
|
int _durationByLastVideoPacket = 0;
|
2019-03-05 11:06:54 +00:00
|
|
|
|
2019-02-17 11:08:29 +00:00
|
|
|
rpl::lifetime _lifetime;
|
2019-02-22 14:28:10 +00:00
|
|
|
rpl::lifetime _sessionLifetime;
|
2019-02-17 06:54:57 +00:00
|
|
|
|
|
|
|
};
|
|
|
|
|
|
|
|
} // namespace Streaming
|
|
|
|
} // namespace Media
|