Cache multiple custom ringtone tracks.

This commit is contained in:
John Preston 2022-04-05 18:08:35 +04:00
parent 2156e15732
commit 071f599d48
2 changed files with 37 additions and 21 deletions

View file

@ -468,8 +468,7 @@ void System::showNext() {
}; };
auto ms = crl::now(), nextAlert = crl::time(0); auto ms = crl::now(), nextAlert = crl::time(0);
auto alert = false; auto alertPeer = (PeerData*)nullptr;
std::shared_ptr<Data::DocumentMedia> customSound = nullptr;
for (auto i = _whenAlerts.begin(); i != _whenAlerts.end();) { for (auto i = _whenAlerts.begin(); i != _whenAlerts.end();) {
while (!i->second.empty() && i->second.begin()->first <= ms) { while (!i->second.empty() && i->second.begin()->first <= ms) {
const auto peer = i->first->peer; const auto peer = i->first->peer;
@ -477,19 +476,13 @@ void System::showNext() {
const auto peerUnknown = notifySettings.muteUnknown(peer); const auto peerUnknown = notifySettings.muteUnknown(peer);
const auto peerAlert = !peerUnknown const auto peerAlert = !peerUnknown
&& !notifySettings.isMuted(peer); && !notifySettings.isMuted(peer);
const auto peerSoundId = !notifySettings.soundUnknown(peer)
? notifySettings.sound(peer).id
: 0;
const auto from = i->second.begin()->second; const auto from = i->second.begin()->second;
const auto fromUnknown = (!from const auto fromUnknown = (!from
|| notifySettings.muteUnknown(from)); || notifySettings.muteUnknown(from));
const auto fromAlert = !fromUnknown const auto fromAlert = !fromUnknown
&& !notifySettings.isMuted(from); && !notifySettings.isMuted(from);
if (peerAlert || fromAlert) { if (peerAlert || fromAlert) {
alert = true; alertPeer = peer;
if (peerSoundId) {
customSound = notifySettings.lookupRingtone(peerSoundId);
}
} }
while (!i->second.empty() while (!i->second.empty()
&& i->second.begin()->first <= ms + kMinimalAlertDelay) { && i->second.begin()->first <= ms + kMinimalAlertDelay) {
@ -506,7 +499,7 @@ void System::showNext() {
} }
} }
const auto &settings = Core::App().settings(); const auto &settings = Core::App().settings();
if (alert) { if (alertPeer) {
if (settings.flashBounceNotify() && !_manager->skipFlashBounce()) { if (settings.flashBounceNotify() && !_manager->skipFlashBounce()) {
if (const auto window = Core::App().primaryWindow()) { if (const auto window = Core::App().primaryWindow()) {
if (const auto handle = window->widget()->windowHandle()) { if (const auto handle = window->widget()->windowHandle()) {
@ -516,16 +509,9 @@ void System::showNext() {
} }
} }
if (settings.soundNotify() && !_manager->skipAudio()) { if (settings.soundNotify() && !_manager->skipAudio()) {
const auto custom = customSound const auto track = lookupSound(
&& !customSound->bytes().isEmpty(); alertPeer,
if (custom) { alertPeer->owner().notifySettings().sound(alertPeer).id);
const auto bytes = customSound->bytes();
_customSoundTrack = Media::Audio::Current().createTrack();
_customSoundTrack->fillFromData(bytes::make_vector(bytes));
} else {
ensureSoundCreated();
}
const auto &track = custom ? _customSoundTrack : _soundTrack;
track->playOnce(); track->playOnce();
Media::Player::mixer()->suppressAll(track->getLengthMs()); Media::Player::mixer()->suppressAll(track->getLengthMs());
Media::Player::mixer()->faderOnTimer(); Media::Player::mixer()->faderOnTimer();
@ -712,6 +698,31 @@ void System::showNext() {
} }
} }
not_null<Media::Audio::Track*> System::lookupSound(
not_null<PeerData*> peer,
DocumentId id) {
if (!id) {
ensureSoundCreated();
return _soundTrack.get();
}
const auto i = _customSoundTracks.find(id);
if (i != end(_customSoundTracks)) {
return i->second.get();
}
const auto &notifySettings = peer->owner().notifySettings();
const auto custom = notifySettings.lookupRingtone(id);
if (custom && !custom->bytes().isEmpty()) {
const auto j = _customSoundTracks.emplace(
id,
Media::Audio::Current().createTrack()
).first;
j->second->fillFromData(bytes::make_vector(custom->bytes()));
return j->second.get();
}
ensureSoundCreated();
return _soundTrack.get();
}
void System::ensureSoundCreated() { void System::ensureSoundCreated() {
if (_soundTrack) { if (_soundTrack) {
return; return;

View file

@ -158,6 +158,9 @@ private:
void showNext(); void showNext();
void showGrouped(); void showGrouped();
void ensureSoundCreated(); void ensureSoundCreated();
[[nodiscard]] not_null<Media::Audio::Track*> lookupSound(
not_null<PeerData*> peer,
DocumentId id);
base::flat_map< base::flat_map<
not_null<History*>, not_null<History*>,
@ -181,7 +184,9 @@ private:
rpl::event_stream<ChangeType> _settingsChanged; rpl::event_stream<ChangeType> _settingsChanged;
std::unique_ptr<Media::Audio::Track> _soundTrack; std::unique_ptr<Media::Audio::Track> _soundTrack;
std::unique_ptr<Media::Audio::Track> _customSoundTrack; base::flat_map<
DocumentId,
std::unique_ptr<Media::Audio::Track>> _customSoundTracks;
int _lastForwardedCount = 0; int _lastForwardedCount = 0;
uint64 _lastHistorySessionId = 0; uint64 _lastHistorySessionId = 0;