/* 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 "base/observer.h" #include "data/data_audio_msg_id.h" #include "data/data_shared_media.h" class AudioMsgId; class DocumentData; class History; namespace Media { namespace Audio { class Instance; } // namespace Audio } // namespace Media namespace Media { namespace View { class PlaybackProgress; } // namespace View } // namespace Media namespace Media { namespace Streaming { class Document; class Instance; struct PlaybackOptions; struct Update; enum class Error; } // namespace Streaming } // namespace Media namespace base { class PowerSaveBlocker; } // namespace base namespace Media { namespace Player { extern const char kOptionDisableAutoplayNext[]; enum class RepeatMode { None, One, All, }; enum class OrderMode { Default, Reverse, Shuffle, }; class Instance; struct TrackState; void start(not_null instance); void finish(not_null instance); void SaveLastPlaybackPosition( not_null document, const TrackState &state); not_null instance(); class Instance : private base::Subscriber { public: enum class Seeking { Start, Finish, Cancel, }; void play(AudioMsgId::Type type); void pause(AudioMsgId::Type type); void stop(AudioMsgId::Type type); void playPause(AudioMsgId::Type type); bool next(AudioMsgId::Type type); bool previous(AudioMsgId::Type type); AudioMsgId::Type getActiveType() const; void play() { play(getActiveType()); } void pause() { pause(getActiveType()); } void stop() { stop(getActiveType()); } void playPause() { playPause(getActiveType()); } bool next() { return next(getActiveType()); } bool previous() { return previous(getActiveType()); } void playPauseCancelClicked(AudioMsgId::Type type); void play(const AudioMsgId &audioId); void playPause(const AudioMsgId &audioId); [[nodiscard]] TrackState getState(AudioMsgId::Type type) const; [[nodiscard]] Streaming::Instance *roundVideoStreamed( HistoryItem *item) const; [[nodiscard]] View::PlaybackProgress *roundVideoPlayback( HistoryItem *item) const; [[nodiscard]] AudioMsgId current(AudioMsgId::Type type) const { if (const auto data = getData(type)) { return data->current; } return AudioMsgId(); } [[nodiscard]] bool isSeeking(AudioMsgId::Type type) const { if (const auto data = getData(type)) { return (data->seeking == data->current); } return false; } void startSeeking(AudioMsgId::Type type); void finishSeeking(AudioMsgId::Type type, float64 progress); void cancelSeeking(AudioMsgId::Type type); void updateVoicePlaybackSpeed(); [[nodiscard]] bool nextAvailable(AudioMsgId::Type type) const; [[nodiscard]] bool previousAvailable(AudioMsgId::Type type) const; struct Switch { AudioMsgId from; FullMsgId to; }; [[nodiscard]] rpl::producer switchToNextEvents() const { return _switchToNext.events(); } [[nodiscard]] rpl::producer tracksFinished() const { return _tracksFinished.events(); } [[nodiscard]] rpl::producer trackChanged() const { return _trackChanged.events(); } [[nodiscard]] rpl::producer<> playlistChanges( AudioMsgId::Type type) const; [[nodiscard]] rpl::producer updatedNotifier() const { return _updatedNotifier.events(); } [[nodiscard]] rpl::producer<> stops(AudioMsgId::Type type) const; [[nodiscard]] rpl::producer<> startsPlay(AudioMsgId::Type type) const; [[nodiscard]] rpl::producer seekingChanges( AudioMsgId::Type type) const; [[nodiscard]] bool pauseGifByRoundVideo() const; private: using SharedMediaType = Storage::SharedMediaType; using SliceKey = SparseIdsMergedSlice::Key; struct Streamed; struct ShuffleData; struct Data { Data(AudioMsgId::Type type, SharedMediaType overview); Data(Data &&other); Data &operator=(Data &&other); ~Data(); AudioMsgId::Type type; Storage::SharedMediaType overview; AudioMsgId current; AudioMsgId seeking; std::optional playlistSlice; std::optional playlistSliceKey; std::optional playlistRequestedKey; std::optional playlistOtherSlice; std::optional playlistOtherRequestedKey; std::optional playlistIndex; rpl::lifetime playlistLifetime; rpl::lifetime playlistOtherLifetime; rpl::lifetime sessionLifetime; rpl::event_stream<> playlistChanges; History *history = nullptr; History *migrated = nullptr; Main::Session *session = nullptr; bool isPlaying = false; bool resumeOnCallEnd = false; std::unique_ptr streamed; std::unique_ptr shuffleData; std::unique_ptr powerSaveBlocker; std::unique_ptr powerSaveBlockerVideo; }; struct SeekingChanges { Seeking seeking; AudioMsgId::Type type; }; Instance(); ~Instance(); friend void start(not_null instance); friend void finish(not_null instance); void setupShortcuts(); void playStreamed( const AudioMsgId &audioId, std::shared_ptr shared); Streaming::PlaybackOptions streamingOptions( const AudioMsgId &audioId, crl::time position = -1); // Observed notifications. void handleSongUpdate(const AudioMsgId &audioId); void pauseOnCall(AudioMsgId::Type type); void resumeOnCall(AudioMsgId::Type type); void setCurrent(const AudioMsgId &audioId); void refreshPlaylist(not_null data); void refreshOtherPlaylist(not_null data); std::optional playlistKey(not_null data) const; bool validPlaylist(not_null data) const; void validatePlaylist(not_null data); std::optional playlistOtherKey( not_null data) const; bool validOtherPlaylist(not_null data) const; void validateOtherPlaylist(not_null data); void playlistUpdated(not_null data); bool moveInPlaylist(not_null data, int delta, bool autonext); void updatePowerSaveBlocker( not_null data, const TrackState &state); HistoryItem *itemByIndex(not_null data, int index); void stopAndClear(not_null data); [[nodiscard]] MsgId computeCurrentUniversalId( not_null data) const; void validateShuffleData(not_null data); void setupShuffleData(not_null data); void ensureShuffleMove(not_null data, int delta); void handleStreamingUpdate( not_null data, Streaming::Update &&update); void handleStreamingError( not_null data, Streaming::Error &&error); void clearStreamed(not_null data, bool savePosition = true); void emitUpdate(AudioMsgId::Type type); template void emitUpdate(AudioMsgId::Type type, CheckCallback check); [[nodiscard]] RepeatMode repeat(not_null data) const; [[nodiscard]] rpl::producer repeatChanges( not_null data) const; [[nodiscard]] OrderMode order(not_null data) const; [[nodiscard]] rpl::producer orderChanges( not_null data) const; Data *getData(AudioMsgId::Type type) { if (type == AudioMsgId::Type::Song) { return &_songData; } else if (type == AudioMsgId::Type::Voice) { return &_voiceData; } return nullptr; } const Data *getData(AudioMsgId::Type type) const { if (type == AudioMsgId::Type::Song) { return &_songData; } else if (type == AudioMsgId::Type::Voice) { return &_voiceData; } return nullptr; } HistoryItem *roundVideoItem() const; void requestRoundVideoResize() const; void requestRoundVideoRepaint() const; void setHistory( not_null data, History *history, Main::Session *sessionFallback = nullptr); void setSession(not_null data, Main::Session *session); Data _songData; Data _voiceData; bool _roundPlaying = false; rpl::event_stream _switchToNext; rpl::event_stream _tracksFinished; rpl::event_stream _trackChanged; rpl::event_stream _playerStopped; rpl::event_stream _playerStartedPlay; rpl::event_stream _updatedNotifier; rpl::event_stream _seekingChanges; rpl::lifetime _lifetime; }; } // namespace Player } // namespace Media