From 2e00d0c536c64a4dc1d552f1004b8c057e85aad2 Mon Sep 17 00:00:00 2001 From: casey langen Date: Sun, 12 Aug 2018 22:37:14 -0700 Subject: [PATCH] OK, now that there are clear repro steps, the crash on invalid metadata should be fixed for reals. --- src/core/audio/PlaybackService.cpp | 30 +++++++++++++++----------- src/plugins/server/Util.h | 8 +++---- src/plugins/server/WebSocketServer.cpp | 24 +++++++++++++++------ 3 files changed, 40 insertions(+), 22 deletions(-) diff --git a/src/core/audio/PlaybackService.cpp b/src/core/audio/PlaybackService.cpp index edc9f3e20..03133dc8f 100755 --- a/src/core/audio/PlaybackService.cpp +++ b/src/core/audio/PlaybackService.cpp @@ -809,7 +809,10 @@ ITrack* PlaybackService::GetTrack(size_t index) { const size_t count = this->playlist.Count(); if (count && index < this->playlist.Count()) { - return this->playlist.Get(index)->GetSdkValue(); + auto track = this->playlist.Get(index); + if (track) { + return track->GetSdkValue(); + } } return nullptr; @@ -1093,17 +1096,20 @@ ITransport::Gain PlaybackService::GainAtIndex(size_t index) { Mode mode = (Mode)playbackPrefs->GetInt(keys::ReplayGainMode.c_str(), (int) Mode::Disabled); if (mode != Mode::Disabled && index < this->playlist.Count()) { - int64_t id = this->playlist.Get(index)->GetId(); - auto query = std::make_shared(id); - if (this->library->Enqueue(query, ILibrary::QuerySynchronous)) { - auto rg = query->GetResult(); - float gain = (mode == Mode::Album) ? rg->albumGain : rg->trackGain; - float peak = (mode == Mode::Album) ? rg->albumPeak : rg->trackPeak; - if (gain != 1.0f) { - /* http://wiki.hydrogenaud.io/index.php?title=ReplayGain_2.0_specification#Reduced_gain */ - result.gain = powf(10.0f, (gain / 20.0f)); - result.peak = (1.0f / peak); - result.peakValid = true; + auto track = this->playlist.Get(index); + if (track) { + int64_t id = track->GetId(); + auto query = std::make_shared(id); + if (this->library->Enqueue(query, ILibrary::QuerySynchronous)) { + auto rg = query->GetResult(); + float gain = (mode == Mode::Album) ? rg->albumGain : rg->trackGain; + float peak = (mode == Mode::Album) ? rg->albumPeak : rg->trackPeak; + if (gain != 1.0f) { + /* http://wiki.hydrogenaud.io/index.php?title=ReplayGain_2.0_specification#Reduced_gain */ + result.gain = powf(10.0f, (gain / 20.0f)); + result.peak = (1.0f / peak); + result.peakValid = true; + } } } } diff --git a/src/plugins/server/Util.h b/src/plugins/server/Util.h index 8787d755a..eaf434ee8 100644 --- a/src/plugins/server/Util.h +++ b/src/plugins/server/Util.h @@ -68,11 +68,11 @@ static std::string GetPreferenceString( template static std::string GetMetadataString( - MetadataT* metadata, - const std::string& key, + MetadataT* metadata, + const std::string& key, const std::string& defaultValue = "missing metadata!") { - if (!metadata) { defaultValue; } + if (!metadata) { return defaultValue; } metadata->GetString(key.c_str(), threadLocalBuffer, sizeof(threadLocalBuffer)); return std::string(threadLocalBuffer); } @@ -89,7 +89,7 @@ static int64_t GetMetadataInt64(MetadataT* metadata, const std::string& key, int static std::string GetValueString( musik::core::sdk::IValue* value, - const std::string& defaultValue = "missing metadata!") + const std::string& defaultValue = "missing metadata!") { if (!value) { return defaultValue; } value->GetValue(threadLocalBuffer, sizeof(threadLocalBuffer)); diff --git a/src/plugins/server/WebSocketServer.cpp b/src/plugins/server/WebSocketServer.cpp index b67552bca..75bb8b2cd 100644 --- a/src/plugins/server/WebSocketServer.cpp +++ b/src/plugins/server/WebSocketServer.cpp @@ -785,9 +785,15 @@ void WebSocketServer::RespondWithPlayQueueTracks(connection_hdl connection, json for (int i = offset; i < to; i++) { ITrack* track = context.playback->GetTrack(i); - if (idsOnly) { data.push_back(GetMetadataString(track, key::external_id)); } - else { data.push_back(this->ReadTrackMetadata(track)); } - track->Release(); + if (idsOnly) { + data.push_back(GetMetadataString(track, key::external_id)); + } + else { + data.push_back(this->ReadTrackMetadata(track)); + } + if (track) { + track->Release(); + } } editor->Release(); @@ -803,9 +809,15 @@ void WebSocketServer::RespondWithPlayQueueTracks(connection_hdl connection, json for (int i = offset; i < to; i++) { ITrack* track = snapshot->GetTrack(i); - if (idsOnly) { data.push_back(GetMetadataString(track, key::external_id)); } - else { data.push_back(this->ReadTrackMetadata(track)); } - track->Release(); + if (idsOnly) { + data.push_back(GetMetadataString(track, key::external_id)); + } + else { + data.push_back(this->ReadTrackMetadata(track)); + } + if (track) { + track->Release(); + } } } }