/* 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 "data/data_shared_media.h" class AudioMsgId; namespace Media { namespace Audio { class Instance; } // namespace Audio namespace Streaming { class Loader; struct PlaybackOptions; struct Update; enum class Error; } // namespace Streaming } // namespace Media namespace Media { namespace Player { void start(not_null instance); void finish(not_null instance); class Instance; Instance *instance(); struct TrackState; class Instance : private base::Subscriber { public: 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); TrackState getState(AudioMsgId::Type type) const; AudioMsgId current(AudioMsgId::Type type) const { if (auto data = getData(type)) { return data->current; } return AudioMsgId(); } bool repeatEnabled(AudioMsgId::Type type) const { if (auto data = getData(type)) { return data->repeatEnabled; } return false; } void toggleRepeat(AudioMsgId::Type type) { if (auto data = getData(type)) { data->repeatEnabled = !data->repeatEnabled; _repeatChangedNotifier.notify(type); } } bool isSeeking(AudioMsgId::Type type) const { if (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); bool nextAvailable(AudioMsgId::Type type) const; bool previousAvailable(AudioMsgId::Type type) const; struct Switch { AudioMsgId from; FullMsgId to; }; base::Observable &switchToNextNotifier() { return _switchToNextNotifier; } base::Observable &playerWidgetOver() { return _playerWidgetOver; } base::Observable &updatedNotifier() { return _updatedNotifier; } base::Observable &tracksFinishedNotifier() { return _tracksFinishedNotifier; } base::Observable &trackChangedNotifier() { return _trackChangedNotifier; } base::Observable &repeatChangedNotifier() { return _repeatChangedNotifier; } rpl::producer<> playlistChanges(AudioMsgId::Type type) const; void documentLoadProgress(DocumentData *document); void handleLogout(); private: using SharedMediaType = Storage::SharedMediaType; using SliceKey = SparseIdsMergedSlice::Key; struct Streamed; 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 playlistIndex; rpl::lifetime playlistLifetime; rpl::event_stream<> playlistChanges; History *history = nullptr; History *migrated = nullptr; bool repeatEnabled = false; bool isPlaying = false; bool resumeOnCallEnd = false; std::unique_ptr streamed; }; Instance(); ~Instance(); friend void start(not_null instance); friend void finish(not_null instance); void setupShortcuts(); void playStreamed( const AudioMsgId &audioId, std::unique_ptr loader); Streaming::PlaybackOptions streamingOptions( const AudioMsgId &audioId, crl::time position = 0); // 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); std::optional playlistKey(not_null data) const; bool validPlaylist(not_null data); void validatePlaylist(not_null data); void playlistUpdated(not_null data); bool moveInPlaylist(not_null data, int delta, bool autonext); HistoryItem *itemByIndex(not_null data, int index); void handleStreamingUpdate( not_null data, Streaming::Update &&update); void handleStreamingError( not_null data, Streaming::Error &&error); void clearStreamed(not_null data); void emitUpdate(AudioMsgId::Type type); template void emitUpdate(AudioMsgId::Type type, CheckCallback check); 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; } Data _songData; Data _voiceData; base::Observable _switchToNextNotifier; base::Observable _playerWidgetOver; base::Observable _updatedNotifier; base::Observable _tracksFinishedNotifier; base::Observable _trackChangedNotifier; base::Observable _repeatChangedNotifier; rpl::lifetime _lifetime; }; } // namespace Player } // namespace Media