2016-09-23 16:04:26 +00:00
|
|
|
/*
|
|
|
|
This file is part of Telegram Desktop,
|
2018-01-03 10:23:14 +00:00
|
|
|
the official desktop application for the Telegram messaging service.
|
2016-09-23 16:04:26 +00:00
|
|
|
|
2018-01-03 10:23:14 +00:00
|
|
|
For license and copyright information please follow this link:
|
|
|
|
https://github.com/telegramdesktop/tdesktop/blob/master/LEGAL
|
2016-09-23 16:04:26 +00:00
|
|
|
*/
|
|
|
|
#pragma once
|
|
|
|
|
2017-12-08 18:27:28 +00:00
|
|
|
#include "data/data_shared_media.h"
|
|
|
|
|
2016-09-23 16:04:26 +00:00
|
|
|
class AudioMsgId;
|
|
|
|
|
|
|
|
namespace Media {
|
2019-02-01 07:09:55 +00:00
|
|
|
namespace Audio {
|
|
|
|
class Instance;
|
|
|
|
} // namespace Audio
|
2019-02-28 21:03:25 +00:00
|
|
|
namespace Streaming {
|
|
|
|
class Loader;
|
|
|
|
struct PlaybackOptions;
|
|
|
|
struct Update;
|
2019-03-05 13:56:27 +00:00
|
|
|
enum class Error;
|
2019-02-28 21:03:25 +00:00
|
|
|
} // namespace Streaming
|
|
|
|
} // namespace Media
|
2019-02-01 07:09:55 +00:00
|
|
|
|
2019-02-28 21:03:25 +00:00
|
|
|
namespace Media {
|
2016-09-23 16:04:26 +00:00
|
|
|
namespace Player {
|
|
|
|
|
2019-02-01 07:09:55 +00:00
|
|
|
void start(not_null<Audio::Instance*> instance);
|
|
|
|
void finish(not_null<Audio::Instance*> instance);
|
2016-09-23 16:04:26 +00:00
|
|
|
|
|
|
|
class Instance;
|
|
|
|
Instance *instance();
|
|
|
|
|
2017-01-24 21:24:39 +00:00
|
|
|
struct TrackState;
|
2016-09-23 16:04:26 +00:00
|
|
|
|
2016-09-28 21:16:02 +00:00
|
|
|
class Instance : private base::Subscriber {
|
2016-09-23 16:04:26 +00:00
|
|
|
public:
|
2017-05-19 18:11:33 +00:00
|
|
|
void play(AudioMsgId::Type type);
|
2017-02-10 22:37:37 +00:00
|
|
|
void pause(AudioMsgId::Type type);
|
2017-05-19 18:11:33 +00:00
|
|
|
void stop(AudioMsgId::Type type);
|
|
|
|
void playPause(AudioMsgId::Type type);
|
2017-05-21 13:16:39 +00:00
|
|
|
bool next(AudioMsgId::Type type);
|
|
|
|
bool previous(AudioMsgId::Type type);
|
|
|
|
|
|
|
|
AudioMsgId::Type getActiveType() const;
|
2016-09-23 16:04:26 +00:00
|
|
|
|
2017-05-19 18:11:33 +00:00
|
|
|
void play() {
|
|
|
|
play(getActiveType());
|
|
|
|
}
|
|
|
|
void pause() {
|
|
|
|
pause(getActiveType());
|
|
|
|
}
|
|
|
|
void stop() {
|
|
|
|
stop(getActiveType());
|
|
|
|
}
|
|
|
|
void playPause() {
|
|
|
|
playPause(getActiveType());
|
|
|
|
}
|
2017-05-21 13:16:39 +00:00
|
|
|
bool next() {
|
|
|
|
return next(getActiveType());
|
2017-05-19 18:11:33 +00:00
|
|
|
}
|
2017-05-21 13:16:39 +00:00
|
|
|
bool previous() {
|
|
|
|
return previous(getActiveType());
|
2017-05-19 18:11:33 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
void playPauseCancelClicked(AudioMsgId::Type type);
|
2016-10-09 17:08:16 +00:00
|
|
|
|
2016-09-23 16:04:26 +00:00
|
|
|
void play(const AudioMsgId &audioId);
|
2019-02-28 21:03:25 +00:00
|
|
|
void playPause(const AudioMsgId &audioId);
|
|
|
|
TrackState getState(AudioMsgId::Type type) const;
|
|
|
|
|
2017-05-19 18:11:33 +00:00
|
|
|
AudioMsgId current(AudioMsgId::Type type) const {
|
|
|
|
if (auto data = getData(type)) {
|
|
|
|
return data->current;
|
|
|
|
}
|
|
|
|
return AudioMsgId();
|
2016-09-23 16:04:26 +00:00
|
|
|
}
|
|
|
|
|
2017-05-19 18:11:33 +00:00
|
|
|
bool repeatEnabled(AudioMsgId::Type type) const {
|
|
|
|
if (auto data = getData(type)) {
|
|
|
|
return data->repeatEnabled;
|
|
|
|
}
|
|
|
|
return false;
|
2016-09-23 16:04:26 +00:00
|
|
|
}
|
2017-05-19 18:11:33 +00:00
|
|
|
void toggleRepeat(AudioMsgId::Type type) {
|
|
|
|
if (auto data = getData(type)) {
|
|
|
|
data->repeatEnabled = !data->repeatEnabled;
|
2017-05-21 13:16:39 +00:00
|
|
|
_repeatChangedNotifier.notify(type);
|
2017-05-19 18:11:33 +00:00
|
|
|
}
|
2016-09-23 16:04:26 +00:00
|
|
|
}
|
|
|
|
|
2017-02-10 22:37:37 +00:00
|
|
|
bool isSeeking(AudioMsgId::Type type) const {
|
2017-05-19 18:11:33 +00:00
|
|
|
if (auto data = getData(type)) {
|
|
|
|
return (data->seeking == data->current);
|
2017-02-10 22:37:37 +00:00
|
|
|
}
|
|
|
|
return false;
|
2016-09-23 16:04:26 +00:00
|
|
|
}
|
2017-02-10 22:37:37 +00:00
|
|
|
void startSeeking(AudioMsgId::Type type);
|
2019-02-28 21:03:25 +00:00
|
|
|
void finishSeeking(AudioMsgId::Type type, float64 progress);
|
|
|
|
void cancelSeeking(AudioMsgId::Type type);
|
2016-09-23 16:04:26 +00:00
|
|
|
|
2017-12-08 18:27:28 +00:00
|
|
|
bool nextAvailable(AudioMsgId::Type type) const;
|
|
|
|
bool previousAvailable(AudioMsgId::Type type) const;
|
2016-09-23 16:04:26 +00:00
|
|
|
|
2017-05-21 17:08:59 +00:00
|
|
|
struct Switch {
|
|
|
|
AudioMsgId from;
|
|
|
|
FullMsgId to;
|
|
|
|
};
|
|
|
|
|
|
|
|
base::Observable<Switch> &switchToNextNotifier() {
|
|
|
|
return _switchToNextNotifier;
|
|
|
|
}
|
2016-10-14 17:10:15 +00:00
|
|
|
base::Observable<bool> &playerWidgetOver() {
|
|
|
|
return _playerWidgetOver;
|
2016-10-12 19:34:25 +00:00
|
|
|
}
|
2017-01-24 21:24:39 +00:00
|
|
|
base::Observable<TrackState> &updatedNotifier() {
|
2016-09-23 16:04:26 +00:00
|
|
|
return _updatedNotifier;
|
|
|
|
}
|
2017-05-21 13:16:39 +00:00
|
|
|
base::Observable<AudioMsgId::Type> &tracksFinishedNotifier() {
|
|
|
|
return _tracksFinishedNotifier;
|
|
|
|
}
|
|
|
|
base::Observable<AudioMsgId::Type> &trackChangedNotifier() {
|
|
|
|
return _trackChangedNotifier;
|
2016-09-23 16:04:26 +00:00
|
|
|
}
|
2017-05-21 13:16:39 +00:00
|
|
|
base::Observable<AudioMsgId::Type> &repeatChangedNotifier() {
|
2016-10-12 19:34:25 +00:00
|
|
|
return _repeatChangedNotifier;
|
|
|
|
}
|
2016-09-23 16:04:26 +00:00
|
|
|
|
2017-12-08 18:27:28 +00:00
|
|
|
rpl::producer<> playlistChanges(AudioMsgId::Type type) const;
|
|
|
|
|
2016-09-23 16:04:26 +00:00
|
|
|
void documentLoadProgress(DocumentData *document);
|
|
|
|
|
2019-02-28 21:03:25 +00:00
|
|
|
void handleLogout();
|
2016-10-19 08:59:19 +00:00
|
|
|
|
2016-09-23 16:04:26 +00:00
|
|
|
private:
|
2017-12-08 18:27:28 +00:00
|
|
|
using SharedMediaType = Storage::SharedMediaType;
|
2017-12-09 10:02:51 +00:00
|
|
|
using SliceKey = SparseIdsMergedSlice::Key;
|
2019-02-28 21:03:25 +00:00
|
|
|
struct Streamed;
|
2017-05-19 18:11:33 +00:00
|
|
|
struct Data {
|
2019-02-28 21:03:25 +00:00
|
|
|
Data(AudioMsgId::Type type, SharedMediaType overview);
|
|
|
|
Data(Data &&other);
|
|
|
|
Data &operator=(Data &&other);
|
|
|
|
~Data();
|
2017-05-19 18:11:33 +00:00
|
|
|
|
|
|
|
AudioMsgId::Type type;
|
2017-12-08 18:27:28 +00:00
|
|
|
Storage::SharedMediaType overview;
|
2017-05-19 18:11:33 +00:00
|
|
|
AudioMsgId current;
|
|
|
|
AudioMsgId seeking;
|
2018-09-21 16:28:46 +00:00
|
|
|
std::optional<SparseIdsMergedSlice> playlistSlice;
|
|
|
|
std::optional<SliceKey> playlistSliceKey;
|
|
|
|
std::optional<SliceKey> playlistRequestedKey;
|
|
|
|
std::optional<int> playlistIndex;
|
2017-12-09 10:02:51 +00:00
|
|
|
rpl::lifetime playlistLifetime;
|
|
|
|
rpl::event_stream<> playlistChanges;
|
2017-05-19 18:11:33 +00:00
|
|
|
History *history = nullptr;
|
|
|
|
History *migrated = nullptr;
|
|
|
|
bool repeatEnabled = false;
|
|
|
|
bool isPlaying = false;
|
2018-10-31 06:51:19 +00:00
|
|
|
bool resumeOnCallEnd = false;
|
2019-02-28 21:03:25 +00:00
|
|
|
std::unique_ptr<Streamed> streamed;
|
2017-05-19 18:11:33 +00:00
|
|
|
};
|
|
|
|
|
2019-02-28 21:03:25 +00:00
|
|
|
Instance();
|
|
|
|
~Instance();
|
|
|
|
|
|
|
|
friend void start(not_null<Audio::Instance*> instance);
|
|
|
|
friend void finish(not_null<Audio::Instance*> instance);
|
|
|
|
|
|
|
|
void setupShortcuts();
|
|
|
|
void playStreamed(
|
|
|
|
const AudioMsgId &audioId,
|
|
|
|
std::unique_ptr<Streaming::Loader> loader);
|
|
|
|
Streaming::PlaybackOptions streamingOptions(
|
|
|
|
const AudioMsgId &audioId,
|
|
|
|
crl::time position = 0);
|
|
|
|
|
2016-09-23 16:04:26 +00:00
|
|
|
// Observed notifications.
|
|
|
|
void handleSongUpdate(const AudioMsgId &audioId);
|
|
|
|
|
2018-10-31 06:51:19 +00:00
|
|
|
void pauseOnCall(AudioMsgId::Type type);
|
|
|
|
void resumeOnCall(AudioMsgId::Type type);
|
|
|
|
|
2016-09-23 16:04:26 +00:00
|
|
|
void setCurrent(const AudioMsgId &audioId);
|
2017-12-09 10:02:51 +00:00
|
|
|
void refreshPlaylist(not_null<Data*> data);
|
2018-09-21 16:28:46 +00:00
|
|
|
std::optional<SliceKey> playlistKey(not_null<Data*> data) const;
|
2017-12-09 10:02:51 +00:00
|
|
|
bool validPlaylist(not_null<Data*> data);
|
|
|
|
void validatePlaylist(not_null<Data*> data);
|
|
|
|
void playlistUpdated(not_null<Data*> data);
|
2017-12-08 18:27:28 +00:00
|
|
|
bool moveInPlaylist(not_null<Data*> data, int delta, bool autonext);
|
2017-12-09 10:02:51 +00:00
|
|
|
HistoryItem *itemByIndex(not_null<Data*> data, int index);
|
2016-09-23 16:04:26 +00:00
|
|
|
|
2019-02-28 21:03:25 +00:00
|
|
|
void handleStreamingUpdate(
|
|
|
|
not_null<Data*> data,
|
|
|
|
Streaming::Update &&update);
|
|
|
|
void handleStreamingError(
|
|
|
|
not_null<Data*> data,
|
|
|
|
Streaming::Error &&error);
|
|
|
|
|
2019-03-05 13:56:27 +00:00
|
|
|
void clearStreamed(not_null<Data *> data);
|
2019-02-28 21:03:25 +00:00
|
|
|
void emitUpdate(AudioMsgId::Type type);
|
2016-09-23 16:04:26 +00:00
|
|
|
template <typename CheckCallback>
|
2017-02-10 22:37:37 +00:00
|
|
|
void emitUpdate(AudioMsgId::Type type, CheckCallback check);
|
2016-09-23 16:04:26 +00:00
|
|
|
|
2017-05-19 18:11:33 +00:00
|
|
|
Data *getData(AudioMsgId::Type type) {
|
|
|
|
if (type == AudioMsgId::Type::Song) {
|
|
|
|
return &_songData;
|
|
|
|
} else if (type == AudioMsgId::Type::Voice) {
|
|
|
|
return &_voiceData;
|
|
|
|
}
|
|
|
|
return nullptr;
|
|
|
|
}
|
2016-09-23 16:04:26 +00:00
|
|
|
|
2017-05-19 18:11:33 +00:00
|
|
|
const Data *getData(AudioMsgId::Type type) const {
|
|
|
|
if (type == AudioMsgId::Type::Song) {
|
|
|
|
return &_songData;
|
|
|
|
} else if (type == AudioMsgId::Type::Voice) {
|
|
|
|
return &_voiceData;
|
|
|
|
}
|
|
|
|
return nullptr;
|
|
|
|
}
|
2016-09-23 16:04:26 +00:00
|
|
|
|
2017-05-19 18:11:33 +00:00
|
|
|
Data _songData;
|
|
|
|
Data _voiceData;
|
2017-02-10 22:37:37 +00:00
|
|
|
|
2017-05-21 17:08:59 +00:00
|
|
|
base::Observable<Switch> _switchToNextNotifier;
|
2016-10-14 17:10:15 +00:00
|
|
|
base::Observable<bool> _playerWidgetOver;
|
2017-01-24 21:24:39 +00:00
|
|
|
base::Observable<TrackState> _updatedNotifier;
|
2017-05-21 13:16:39 +00:00
|
|
|
base::Observable<AudioMsgId::Type> _tracksFinishedNotifier;
|
|
|
|
base::Observable<AudioMsgId::Type> _trackChangedNotifier;
|
|
|
|
base::Observable<AudioMsgId::Type> _repeatChangedNotifier;
|
2016-09-23 16:04:26 +00:00
|
|
|
|
2018-11-16 12:15:14 +00:00
|
|
|
rpl::lifetime _lifetime;
|
|
|
|
|
2016-09-23 16:04:26 +00:00
|
|
|
};
|
|
|
|
|
2019-02-27 11:36:19 +00:00
|
|
|
} // namespace Player
|
2016-09-23 16:04:26 +00:00
|
|
|
} // namespace Media
|