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
|
|
|
|
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 {
|
|
|
|
|
2019-04-10 11:26:15 +00:00
|
|
|
class Reader;
|
2019-02-17 06:54:57 +00:00
|
|
|
class File;
|
2019-02-17 11:08:29 +00:00
|
|
|
class AudioTrack;
|
|
|
|
class VideoTrack;
|
2019-12-12 06:56:08 +00:00
|
|
|
class Instance;
|
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.
|
2020-05-20 11:04:56 +00:00
|
|
|
explicit Player(std::shared_ptr<Reader> reader);
|
2019-02-17 06:54:57 +00:00
|
|
|
|
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-12-18 18:26:17 +00:00
|
|
|
// Allow to irreversibly stop only audio track.
|
|
|
|
void stopAudio();
|
|
|
|
|
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-12-19 10:50:33 +00:00
|
|
|
void setWaitForMarkAsShown(bool wait);
|
2019-02-21 16:01:55 +00:00
|
|
|
|
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-05-31 16:45:03 +00:00
|
|
|
[[nodiscard]] rpl::producer<bool> fullInCache() const;
|
2019-02-17 11:08:29 +00:00
|
|
|
|
2019-03-26 08:54:51 +00:00
|
|
|
[[nodiscard]] QSize videoSize() const;
|
2019-12-12 06:56:08 +00:00
|
|
|
[[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();
|
2019-02-17 06:54:57 +00:00
|
|
|
|
2019-12-23 10:27:20 +00:00
|
|
|
void setLoaderPriority(int priority);
|
|
|
|
|
2019-02-28 21:03:25 +00:00
|
|
|
[[nodiscard]] Media::Player::TrackState prepareLegacyState() const;
|
|
|
|
|
2019-12-17 14:30:14 +00:00
|
|
|
void lock();
|
|
|
|
void unlock();
|
|
|
|
[[nodiscard]] bool locked() 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-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;
|
2019-02-21 13:40:09 +00:00
|
|
|
void fileWaitingForData() override;
|
2019-05-31 16:45:03 +00:00
|
|
|
void fileFullInCache(bool fullInCache) override;
|
2019-12-19 15:14:05 +00:00
|
|
|
bool fileProcessPackets(
|
|
|
|
base::flat_map<int, std::vector<FFmpeg::Packet>> &packets) override;
|
2019-12-21 13:57:06 +00:00
|
|
|
void fileProcessEndOfFile() override;
|
2019-02-17 06:54:57 +00:00
|
|
|
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-04-11 10:52:49 +00:00
|
|
|
void stop(bool stillActive);
|
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>
|
2019-06-26 15:04:38 +00:00
|
|
|
int durationByPacket(const Track &track, const FFmpeg::Packet &packet);
|
2019-03-13 14:58:50 +00:00
|
|
|
|
|
|
|
// 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-12-11 14:01:11 +00:00
|
|
|
crl::time _currentFrameTime = 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-05-31 16:45:03 +00:00
|
|
|
rpl::event_stream<bool> _fullInCache;
|
|
|
|
std::optional<bool> _fullInCacheSinceStart;
|
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-12-17 14:30:14 +00:00
|
|
|
int _locks = 0;
|
|
|
|
|
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
|