2016-09-23 19:04:26 +03:00
|
|
|
/*
|
|
|
|
This file is part of Telegram Desktop,
|
|
|
|
the official desktop version of Telegram messaging app, see https://telegram.org
|
|
|
|
|
|
|
|
Telegram Desktop is free software: you can redistribute it and/or modify
|
|
|
|
it under the terms of the GNU General Public License as published by
|
|
|
|
the Free Software Foundation, either version 3 of the License, or
|
|
|
|
(at your option) any later version.
|
|
|
|
|
|
|
|
It is distributed in the hope that it will be useful,
|
|
|
|
but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
|
|
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
|
|
|
GNU General Public License for more details.
|
|
|
|
|
|
|
|
In addition, as a special exception, the copyright holders give permission
|
|
|
|
to link the code of portions of this program with the OpenSSL library.
|
|
|
|
|
|
|
|
Full license: https://github.com/telegramdesktop/tdesktop/blob/master/LICENSE
|
2017-01-11 22:31:31 +04:00
|
|
|
Copyright (c) 2014-2017 John Preston, https://desktop.telegram.org
|
2016-09-23 19:04:26 +03:00
|
|
|
*/
|
|
|
|
#include "media/player/media_player_instance.h"
|
|
|
|
|
2017-09-26 14:49:16 +03:00
|
|
|
#include "data/data_document.h"
|
2016-09-23 19:04:26 +03:00
|
|
|
#include "media/media_audio.h"
|
2017-01-19 11:24:43 +03:00
|
|
|
#include "media/media_audio_capture.h"
|
2017-05-12 20:44:18 +03:00
|
|
|
#include "messenger.h"
|
|
|
|
#include "auth_session.h"
|
|
|
|
#include "calls/calls_instance.h"
|
2017-06-22 18:11:41 +03:00
|
|
|
#include "history/history_media.h"
|
2016-09-23 19:04:26 +03:00
|
|
|
|
|
|
|
namespace Media {
|
|
|
|
namespace Player {
|
|
|
|
namespace {
|
|
|
|
|
|
|
|
Instance *SingleInstance = nullptr;
|
|
|
|
|
|
|
|
} // namespace
|
|
|
|
|
|
|
|
void start() {
|
2017-05-03 14:36:39 +03:00
|
|
|
Audio::Start();
|
|
|
|
Capture::Start();
|
2016-09-23 19:04:26 +03:00
|
|
|
|
2017-01-19 11:24:43 +03:00
|
|
|
SingleInstance = new Instance();
|
2016-09-23 19:04:26 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
void finish() {
|
2016-10-08 12:10:33 +03:00
|
|
|
delete base::take(SingleInstance);
|
2016-09-23 19:04:26 +03:00
|
|
|
|
2017-05-03 14:36:39 +03:00
|
|
|
Capture::Finish();
|
|
|
|
Audio::Finish();
|
2016-09-23 19:04:26 +03:00
|
|
|
}
|
|
|
|
|
2017-05-19 21:11:33 +03:00
|
|
|
Instance::Instance()
|
2017-12-08 22:27:28 +04:00
|
|
|
: _songData(AudioMsgId::Type::Song, SharedMediaType::MusicFile)
|
|
|
|
, _voiceData(AudioMsgId::Type::Voice, SharedMediaType::RoundVoiceFile) {
|
2017-01-19 11:24:43 +03:00
|
|
|
subscribe(Media::Player::Updated(), [this](const AudioMsgId &audioId) {
|
2017-02-11 01:37:37 +03:00
|
|
|
handleSongUpdate(audioId);
|
2016-09-23 19:04:26 +03:00
|
|
|
});
|
2016-10-19 11:59:19 +03:00
|
|
|
subscribe(Global::RefSelfChanged(), [this] {
|
|
|
|
if (!App::self()) {
|
|
|
|
handleLogout();
|
|
|
|
}
|
|
|
|
});
|
2017-05-12 20:44:18 +03:00
|
|
|
|
|
|
|
// While we have one Media::Player::Instance for all authsessions we have to do this.
|
|
|
|
auto handleAuthSessionChange = [this] {
|
|
|
|
if (AuthSession::Exists()) {
|
2017-08-04 16:54:32 +02:00
|
|
|
subscribe(Auth().calls().currentCallChanged(), [this](Calls::Call *call) {
|
2017-05-12 20:44:18 +03:00
|
|
|
if (call) {
|
|
|
|
pause(AudioMsgId::Type::Voice);
|
|
|
|
pause(AudioMsgId::Type::Song);
|
|
|
|
}
|
|
|
|
});
|
|
|
|
}
|
|
|
|
};
|
|
|
|
subscribe(Messenger::Instance().authSessionChanged(), [handleAuthSessionChange] {
|
|
|
|
handleAuthSessionChange();
|
|
|
|
});
|
|
|
|
handleAuthSessionChange();
|
2016-09-23 19:04:26 +03:00
|
|
|
}
|
|
|
|
|
2017-05-19 21:11:33 +03:00
|
|
|
AudioMsgId::Type Instance::getActiveType() const {
|
|
|
|
auto voiceData = getData(AudioMsgId::Type::Voice);
|
|
|
|
if (voiceData->current) {
|
|
|
|
auto state = mixer()->currentState(voiceData->type);
|
2017-05-21 16:16:39 +03:00
|
|
|
if (voiceData->current == state.id && !IsStoppedOrStopping(state.state)) {
|
2017-05-19 21:11:33 +03:00
|
|
|
return voiceData->type;
|
|
|
|
}
|
2016-09-23 19:04:26 +03:00
|
|
|
}
|
2017-05-19 21:11:33 +03:00
|
|
|
return AudioMsgId::Type::Song;
|
|
|
|
}
|
2016-09-23 19:04:26 +03:00
|
|
|
|
|
|
|
void Instance::handleSongUpdate(const AudioMsgId &audioId) {
|
2017-02-11 01:37:37 +03:00
|
|
|
emitUpdate(audioId.type(), [&audioId](const AudioMsgId &playing) {
|
2016-09-23 19:04:26 +03:00
|
|
|
return (audioId == playing);
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
|
|
|
void Instance::setCurrent(const AudioMsgId &audioId) {
|
2017-05-19 21:11:33 +03:00
|
|
|
if (auto data = getData(audioId.type())) {
|
|
|
|
if (data->current != audioId) {
|
|
|
|
data->current = audioId;
|
|
|
|
data->isPlaying = false;
|
|
|
|
|
|
|
|
auto history = data->history;
|
|
|
|
auto migrated = data->migrated;
|
|
|
|
auto item = data->current ? App::histItemById(data->current.contextId()) : nullptr;
|
2017-02-11 01:37:37 +03:00
|
|
|
if (item) {
|
2017-08-31 20:53:03 +03:00
|
|
|
data->history = item->history()->migrateToOrMe();
|
|
|
|
data->migrated = data->history->migrateFrom();
|
2017-02-11 01:37:37 +03:00
|
|
|
} else {
|
2017-05-19 21:11:33 +03:00
|
|
|
data->history = nullptr;
|
|
|
|
data->migrated = nullptr;
|
2017-02-11 01:37:37 +03:00
|
|
|
}
|
2017-05-21 16:16:39 +03:00
|
|
|
_trackChangedNotifier.notify(data->type, true);
|
2017-05-19 21:11:33 +03:00
|
|
|
if (data->history != history || data->migrated != migrated) {
|
|
|
|
rebuildPlaylist(data);
|
2017-02-11 01:37:37 +03:00
|
|
|
}
|
2016-09-23 19:04:26 +03:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2017-12-08 22:27:28 +04:00
|
|
|
void Instance::rebuildPlaylist(not_null<Data*> data) {
|
|
|
|
// #TODO playlist
|
2016-09-23 19:04:26 +03:00
|
|
|
}
|
|
|
|
|
2017-12-08 22:27:28 +04:00
|
|
|
bool Instance::moveInPlaylist(
|
|
|
|
not_null<Data*> data,
|
|
|
|
int delta,
|
|
|
|
bool autonext) {
|
|
|
|
//auto index = data->playlist.indexOf(data->current.contextId());
|
|
|
|
//auto newIndex = index + delta;
|
|
|
|
//if (!data->current || index < 0 || newIndex < 0 || newIndex >= data->playlist.size()) {
|
|
|
|
// rebuildPlaylist(data);
|
|
|
|
// return false;
|
|
|
|
//}
|
|
|
|
|
|
|
|
//auto msgId = data->playlist[newIndex];
|
|
|
|
//if (auto item = App::histItemById(msgId)) {
|
|
|
|
// if (auto media = item->getMedia()) {
|
|
|
|
// if (auto document = media->getDocument()) {
|
|
|
|
// if (autonext) {
|
|
|
|
// _switchToNextNotifier.notify({ data->current, msgId });
|
|
|
|
// }
|
|
|
|
// DocumentOpenClickHandler::doOpen(media->getDocument(), item, ActionOnLoadPlayInline);
|
|
|
|
// return true;
|
|
|
|
// }
|
|
|
|
// }
|
|
|
|
//}
|
|
|
|
//return false;
|
|
|
|
return false;
|
|
|
|
}
|
2017-05-19 21:11:33 +03:00
|
|
|
|
2017-12-08 22:27:28 +04:00
|
|
|
bool Instance::previousAvailable(AudioMsgId::Type type) const {
|
|
|
|
return false;
|
|
|
|
}
|
2016-09-23 19:04:26 +03:00
|
|
|
|
2017-12-08 22:27:28 +04:00
|
|
|
bool Instance::nextAvailable(AudioMsgId::Type type) const {
|
2017-05-21 16:16:39 +03:00
|
|
|
return false;
|
2016-09-23 19:04:26 +03:00
|
|
|
}
|
|
|
|
|
2017-12-08 22:27:28 +04:00
|
|
|
rpl::producer<> Media::Player::Instance::playlistChanges(
|
|
|
|
AudioMsgId::Type type) const {
|
|
|
|
return rpl::never<>();
|
|
|
|
}
|
|
|
|
|
2016-09-23 19:04:26 +03:00
|
|
|
Instance *instance() {
|
2017-05-19 21:11:33 +03:00
|
|
|
Expects(SingleInstance != nullptr);
|
2016-09-23 19:04:26 +03:00
|
|
|
return SingleInstance;
|
|
|
|
}
|
|
|
|
|
2017-05-19 21:11:33 +03:00
|
|
|
void Instance::play(AudioMsgId::Type type) {
|
|
|
|
auto state = mixer()->currentState(type);
|
2017-01-25 00:24:39 +03:00
|
|
|
if (state.id) {
|
|
|
|
if (IsStopped(state.state)) {
|
2017-05-19 21:11:33 +03:00
|
|
|
play(state.id);
|
2017-05-18 23:18:59 +03:00
|
|
|
} else {
|
|
|
|
mixer()->resume(state.id);
|
2016-09-23 19:04:26 +03:00
|
|
|
}
|
2017-05-19 21:11:33 +03:00
|
|
|
} else if (auto data = getData(type)) {
|
|
|
|
if (data->current) {
|
|
|
|
play(data->current);
|
|
|
|
}
|
2016-09-23 19:04:26 +03:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
void Instance::play(const AudioMsgId &audioId) {
|
2017-05-19 21:11:33 +03:00
|
|
|
if (!audioId) {
|
2016-09-23 19:04:26 +03:00
|
|
|
return;
|
|
|
|
}
|
2017-05-19 21:11:33 +03:00
|
|
|
if (audioId.audio()->song() || audioId.audio()->voice()) {
|
|
|
|
mixer()->play(audioId);
|
|
|
|
setCurrent(audioId);
|
|
|
|
if (audioId.audio()->loading()) {
|
|
|
|
documentLoadProgress(audioId.audio());
|
|
|
|
}
|
|
|
|
} else if (audioId.audio()->isRoundVideo()) {
|
|
|
|
if (auto item = App::histItemById(audioId.contextId())) {
|
|
|
|
if (auto media = item->getMedia()) {
|
|
|
|
media->playInline();
|
|
|
|
}
|
|
|
|
}
|
2016-09-23 19:04:26 +03:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2017-02-11 01:37:37 +03:00
|
|
|
void Instance::pause(AudioMsgId::Type type) {
|
|
|
|
auto state = mixer()->currentState(type);
|
2017-01-25 00:24:39 +03:00
|
|
|
if (state.id) {
|
2017-05-18 23:18:59 +03:00
|
|
|
mixer()->pause(state.id);
|
2016-09-23 19:04:26 +03:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2017-05-19 21:11:33 +03:00
|
|
|
void Instance::stop(AudioMsgId::Type type) {
|
|
|
|
auto state = mixer()->currentState(type);
|
2017-05-18 23:18:59 +03:00
|
|
|
if (state.id) {
|
|
|
|
mixer()->stop(state.id);
|
|
|
|
}
|
2016-09-23 19:04:26 +03:00
|
|
|
}
|
|
|
|
|
2017-05-19 21:11:33 +03:00
|
|
|
void Instance::playPause(AudioMsgId::Type type) {
|
|
|
|
auto state = mixer()->currentState(type);
|
2017-01-25 00:24:39 +03:00
|
|
|
if (state.id) {
|
|
|
|
if (IsStopped(state.state)) {
|
2017-05-19 21:11:33 +03:00
|
|
|
play(state.id);
|
2017-05-18 23:18:59 +03:00
|
|
|
} else if (IsPaused(state.state) || state.state == State::Pausing) {
|
|
|
|
mixer()->resume(state.id);
|
2016-09-23 19:04:26 +03:00
|
|
|
} else {
|
2017-05-18 23:18:59 +03:00
|
|
|
mixer()->pause(state.id);
|
2016-09-23 19:04:26 +03:00
|
|
|
}
|
2017-05-19 21:11:33 +03:00
|
|
|
} else if (auto data = getData(type)) {
|
|
|
|
if (data->current) {
|
|
|
|
play(data->current);
|
|
|
|
}
|
2016-09-23 19:04:26 +03:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2017-05-21 16:16:39 +03:00
|
|
|
bool Instance::next(AudioMsgId::Type type) {
|
2017-05-19 21:11:33 +03:00
|
|
|
if (auto data = getData(type)) {
|
2017-05-21 20:08:59 +03:00
|
|
|
return moveInPlaylist(data, 1, false);
|
2017-05-19 21:11:33 +03:00
|
|
|
}
|
2017-05-21 16:16:39 +03:00
|
|
|
return false;
|
2016-09-23 19:04:26 +03:00
|
|
|
}
|
|
|
|
|
2017-05-21 16:16:39 +03:00
|
|
|
bool Instance::previous(AudioMsgId::Type type) {
|
2017-05-19 21:11:33 +03:00
|
|
|
if (auto data = getData(type)) {
|
2017-05-21 20:08:59 +03:00
|
|
|
return moveInPlaylist(data, -1, false);
|
2017-05-19 21:11:33 +03:00
|
|
|
}
|
2017-05-21 16:16:39 +03:00
|
|
|
return false;
|
2016-09-23 19:04:26 +03:00
|
|
|
}
|
|
|
|
|
2017-05-19 21:11:33 +03:00
|
|
|
void Instance::playPauseCancelClicked(AudioMsgId::Type type) {
|
|
|
|
if (isSeeking(type)) {
|
2016-10-09 20:08:16 +03:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2017-05-19 21:11:33 +03:00
|
|
|
auto state = mixer()->currentState(type);
|
2017-05-21 16:16:39 +03:00
|
|
|
auto stopped = IsStoppedOrStopping(state.state);
|
2017-01-25 00:24:39 +03:00
|
|
|
auto showPause = !stopped && (state.state == State::Playing || state.state == State::Resuming || state.state == State::Starting);
|
|
|
|
auto audio = state.id.audio();
|
2016-10-09 20:08:16 +03:00
|
|
|
if (audio && audio->loading()) {
|
|
|
|
audio->cancel();
|
|
|
|
} else if (showPause) {
|
2017-05-19 21:11:33 +03:00
|
|
|
pause(type);
|
2016-10-09 20:08:16 +03:00
|
|
|
} else {
|
2017-05-19 21:11:33 +03:00
|
|
|
play(type);
|
2016-10-09 20:08:16 +03:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2017-02-11 01:37:37 +03:00
|
|
|
void Instance::startSeeking(AudioMsgId::Type type) {
|
2017-05-19 21:11:33 +03:00
|
|
|
if (auto data = getData(type)) {
|
|
|
|
data->seeking = data->current;
|
2017-02-11 01:37:37 +03:00
|
|
|
}
|
|
|
|
pause(type);
|
|
|
|
emitUpdate(type, [](const AudioMsgId &playing) { return true; });
|
2016-09-23 19:04:26 +03:00
|
|
|
}
|
|
|
|
|
2017-02-11 01:37:37 +03:00
|
|
|
void Instance::stopSeeking(AudioMsgId::Type type) {
|
2017-05-19 21:11:33 +03:00
|
|
|
if (auto data = getData(type)) {
|
|
|
|
data->seeking = AudioMsgId();
|
2017-02-11 01:37:37 +03:00
|
|
|
}
|
|
|
|
emitUpdate(type, [](const AudioMsgId &playing) { return true; });
|
2016-09-23 19:04:26 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
void Instance::documentLoadProgress(DocumentData *document) {
|
2017-02-11 01:37:37 +03:00
|
|
|
emitUpdate(document->song() ? AudioMsgId::Type::Song : AudioMsgId::Type::Voice, [document](const AudioMsgId &audioId) {
|
2016-09-23 19:04:26 +03:00
|
|
|
return (audioId.audio() == document);
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
|
|
|
template <typename CheckCallback>
|
2017-02-11 01:37:37 +03:00
|
|
|
void Instance::emitUpdate(AudioMsgId::Type type, CheckCallback check) {
|
|
|
|
auto state = mixer()->currentState(type);
|
2017-01-25 00:24:39 +03:00
|
|
|
if (!state.id || !check(state.id)) {
|
2016-09-23 19:04:26 +03:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2017-01-25 00:24:39 +03:00
|
|
|
setCurrent(state.id);
|
|
|
|
_updatedNotifier.notify(state, true);
|
2016-09-23 19:04:26 +03:00
|
|
|
|
2017-05-19 21:11:33 +03:00
|
|
|
if (auto data = getData(type)) {
|
|
|
|
if (data->isPlaying && state.state == State::StoppedAtEnd) {
|
|
|
|
if (data->repeatEnabled) {
|
|
|
|
play(data->current);
|
2017-05-21 20:08:59 +03:00
|
|
|
} else if (!moveInPlaylist(data, 1, true)) {
|
2017-05-21 16:16:39 +03:00
|
|
|
_tracksFinishedNotifier.notify(type);
|
2017-02-11 01:37:37 +03:00
|
|
|
}
|
2016-09-23 19:04:26 +03:00
|
|
|
}
|
2017-02-11 01:37:37 +03:00
|
|
|
auto isPlaying = !IsStopped(state.state);
|
2017-05-19 21:11:33 +03:00
|
|
|
if (data->isPlaying != isPlaying) {
|
|
|
|
data->isPlaying = isPlaying;
|
|
|
|
if (data->isPlaying) {
|
|
|
|
preloadNext(data);
|
2017-02-11 01:37:37 +03:00
|
|
|
}
|
2016-10-12 22:34:25 +03:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2017-12-08 22:27:28 +04:00
|
|
|
void Instance::preloadNext(not_null<Data*> data) {
|
2017-05-19 21:11:33 +03:00
|
|
|
if (!data->current) {
|
2016-10-12 22:34:25 +03:00
|
|
|
return;
|
|
|
|
}
|
2017-12-08 22:27:28 +04:00
|
|
|
//auto index = data->playlist.indexOf(data->current.contextId());
|
|
|
|
//if (index < 0) {
|
|
|
|
// return;
|
|
|
|
//}
|
|
|
|
//auto nextIndex = index + 1;
|
|
|
|
//if (nextIndex >= data->playlist.size()) {
|
|
|
|
// return;
|
|
|
|
//}
|
|
|
|
//if (auto item = App::histItemById(data->playlist[nextIndex])) {
|
|
|
|
// if (auto media = item->getMedia()) {
|
|
|
|
// if (auto document = media->getDocument()) {
|
|
|
|
// if (!document->loaded(DocumentData::FilePathResolveSaveFromDataSilent)) {
|
|
|
|
// DocumentOpenClickHandler::doOpen(document, nullptr, ActionOnLoadNone);
|
|
|
|
// }
|
|
|
|
// }
|
|
|
|
// }
|
|
|
|
//}
|
2016-09-23 19:04:26 +03:00
|
|
|
}
|
|
|
|
|
2016-10-19 11:59:19 +03:00
|
|
|
void Instance::handleLogout() {
|
2017-12-08 22:27:28 +04:00
|
|
|
const auto reset = [&](AudioMsgId::Type type) {
|
|
|
|
const auto data = getData(type);
|
|
|
|
*data = Data(type, data->overview);
|
|
|
|
};
|
|
|
|
reset(AudioMsgId::Type::Voice);
|
|
|
|
reset(AudioMsgId::Type::Song);
|
2016-10-19 11:59:19 +03:00
|
|
|
_usePanelPlayer.notify(false, true);
|
|
|
|
}
|
|
|
|
|
2016-09-23 19:04:26 +03:00
|
|
|
} // namespace Player
|
|
|
|
} // namespace Media
|