tdesktop/Telegram/SourceFiles/media/streaming/media_streaming_player.h

214 lines
5.8 KiB
C
Raw Normal View History

/*
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"
#include "base/weak_ptr.h"
#include "base/timer.h"
2019-02-28 21:03:25 +00:00
namespace Media {
namespace Player {
struct TrackState;
} // namespace Player
} // namespace Media
namespace Media {
namespace Streaming {
class Reader;
class File;
class AudioTrack;
class VideoTrack;
class Instance;
class Player final : private FileDelegate {
public:
// Public interfaces is used from the main thread.
explicit Player(std::shared_ptr<Reader> reader);
// 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);
void pause();
void resume();
void stop();
// Allow to irreversibly stop only audio track.
void stopAudio();
[[nodiscard]] bool active() const;
[[nodiscard]] bool ready() const;
2019-02-21 16:01:55 +00:00
[[nodiscard]] float64 speed() const;
2019-02-21 16:01:55 +00:00
void setSpeed(float64 speed); // 0.5 <= speed <= 2.
2019-12-19 10:50:33 +00:00
void setWaitForMarkAsShown(bool wait);
2019-02-21 16:01:55 +00:00
[[nodiscard]] bool playing() const;
[[nodiscard]] bool buffering() const;
[[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;
[[nodiscard]] rpl::producer<Update, Error> updates() const;
[[nodiscard]] rpl::producer<bool> fullInCache() const;
[[nodiscard]] QSize videoSize() const;
[[nodiscard]] QImage frame(
const FrameRequest &request,
const Instance *instance = nullptr) const;
void unregisterInstance(not_null<const Instance*> instance);
2019-12-11 14:01:11 +00:00
bool markFrameShown();
void setLoaderPriority(int priority);
2019-02-28 21:03:25 +00:00
[[nodiscard]] Media::Player::TrackState prepareLegacyState() const;
void lock();
void unlock();
[[nodiscard]] bool locked() const;
[[nodiscard]] rpl::lifetime &lifetime();
~Player();
private:
enum class Stage {
Uninitialized,
Initializing,
Ready,
Started,
};
// Thread-safe.
not_null<FileDelegate*> delegate();
// FileDelegate methods are called only from the File thread.
2019-05-31 11:45:35 +00:00
bool fileReady(int headerSize, Stream &&video, Stream &&audio) override;
2019-03-05 13:56:27 +00:00
void fileError(Error error) override;
void fileWaitingForData() override;
void fileFullInCache(bool fullInCache) override;
bool fileProcessPackets(
base::flat_map<int, std::vector<FFmpeg::Packet>> &packets) override;
2019-12-21 13:57:06 +00:00
void fileProcessEndOfFile() override;
bool fileReadMore() override;
// 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();
void stop(bool stillActive);
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();
void renderFrame(crl::time now);
void audioReceivedTill(crl::time position);
void audioPlayedTill(crl::time position);
void videoReceivedTill(crl::time position);
void videoPlayedTill(crl::time position);
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;
void checkResumeFromWaitingForData();
[[nodiscard]] crl::time getCurrentReceivedTill(crl::time duration) const;
void savePreviousReceivedTill(
const PlaybackOptions &options,
crl::time previousReceivedTill);
2019-03-05 13:56:27 +00:00
[[nodiscard]] crl::time loadInAdvanceFor() const;
template <typename Track>
2019-06-26 15:04:38 +00:00
int durationByPacket(const Track &track, const FFmpeg::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();
template <typename Track>
void trackReceivedTill(
const Track &track,
TrackState &state,
crl::time position);
template <typename Track>
void trackSendReceivedTill(
const Track &track,
TrackState &state);
template <typename Track>
void trackPlayedTill(
const Track &track,
TrackState &state,
crl::time position);
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;
std::unique_ptr<AudioTrack> _audio;
std::unique_ptr<VideoTrack> _video;
2019-02-21 11:15:44 +00:00
// Immutable while File is active.
base::has_weak_ptr _sessionGuard;
// Immutable while File is active except '.speed'.
// '.speed' is changed from the main thread.
PlaybackOptions _options;
// Belongs to the File thread while File is active.
bool _readTillEnd = false;
bool _waitingForData = false;
2019-02-26 14:09:08 +00:00
std::atomic<bool> _pauseReading = false;
// Belongs to the main thread.
Information _information;
Stage _stage = Stage::Uninitialized;
2019-03-05 13:56:27 +00:00
std::optional<Error> _lastFailure;
bool _pausedByUser = false;
bool _pausedByWaitingForData = false;
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;
crl::time _startedTime = kTimeUnknown;
2019-02-21 14:57:00 +00:00
crl::time _pausedTime = kTimeUnknown;
2019-12-11 14:01:11 +00:00
crl::time _currentFrameTime = kTimeUnknown;
crl::time _nextFrameTime = kTimeUnknown;
base::Timer _renderFrameTimer;
rpl::event_stream<Update, Error> _updates;
rpl::event_stream<bool> _fullInCache;
std::optional<bool> _fullInCacheSinceStart;
2019-03-05 13:56:27 +00:00
crl::time _totalDuration = kTimeUnknown;
crl::time _loopingShift = 0;
crl::time _previousReceivedTill = kTimeUnknown;
std::atomic<int> _durationByPackets = 0;
int _durationByLastAudioPacket = 0;
int _durationByLastVideoPacket = 0;
int _locks = 0;
rpl::lifetime _lifetime;
rpl::lifetime _sessionLifetime;
};
} // namespace Streaming
} // namespace Media