Skip to content

Commit 9b8f331

Browse files
committed
Playlist Library: Improve failed preset handling using a loop
Also removing failed presets immediately, which will prevent the same preset from being tried again. Increased default retry count to 500.
1 parent b3e7e8c commit 9b8f331

File tree

3 files changed

+69
-51
lines changed

3 files changed

+69
-51
lines changed

src/playlist/PlaylistCWrapper.cpp

Lines changed: 61 additions & 46 deletions
Original file line numberDiff line numberDiff line change
@@ -62,45 +62,18 @@ void PlaylistCWrapper::OnPresetSwitchFailed(const char* presetFilename, const ch
6262
}
6363

6464
auto* playlist = reinterpret_cast<PlaylistCWrapper*>(userData);
65+
66+
playlist->m_lastPresetSwitchFailed = true;
67+
playlist->m_lastFailedPresetFileName = presetFilename;
68+
playlist->m_lastFailedPresetError = message;
69+
6570
auto lastDirection = playlist->GetLastNavigationDirection();
6671

6772
if (lastDirection != NavigationDirection::Last)
6873
{
6974
// Don't let the user go back to a broken preset.
7075
playlist->RemoveLastHistoryEntry();
7176
}
72-
73-
// Preset switch may fail due to broken presets, retry a few times before giving up.
74-
if (playlist->m_presetSwitchFailedCount >= playlist->m_presetSwitchRetryCount)
75-
{
76-
if (playlist->m_presetSwitchFailedEventCallback != nullptr)
77-
{
78-
playlist->m_presetSwitchFailedEventCallback(presetFilename, message,
79-
playlist->m_presetSwitchFailedEventUserData);
80-
}
81-
82-
return;
83-
}
84-
85-
playlist->m_presetSwitchFailedCount++;
86-
87-
uint32_t playlistIndex{};
88-
switch (lastDirection)
89-
{
90-
case NavigationDirection::Previous:
91-
playlistIndex = playlist->PreviousPresetIndex();
92-
break;
93-
94-
case NavigationDirection::Next:
95-
playlistIndex = playlist->NextPresetIndex();
96-
break;
97-
98-
case NavigationDirection::Last:
99-
playlistIndex = playlist->LastPresetIndex();
100-
break;
101-
}
102-
103-
playlist->PlayPresetIndex(playlistIndex, playlist->m_hardCutRequested, false);
10477
}
10578

10679

@@ -132,22 +105,64 @@ void PlaylistCWrapper::SetPresetSwitchFailedCallback(projectm_playlist_preset_sw
132105

133106
void PlaylistCWrapper::PlayPresetIndex(uint32_t index, bool hardCut, bool resetFailureCount)
134107
{
135-
if (resetFailureCount)
136-
{
137-
m_presetSwitchFailedCount = 0;
138-
}
139-
140108
m_hardCutRequested = hardCut;
141109

142-
const auto& playlistItems = Items();
110+
auto& playlistItems = Items();
143111

144-
if (playlistItems.size() <= index)
112+
uint32_t failedCount = 0;
113+
while (true)
145114
{
146-
return;
147-
}
115+
if (playlistItems.size() <= index)
116+
{
117+
return;
118+
}
119+
120+
projectm_load_preset_file(m_projectMInstance,
121+
playlistItems.at(index).Filename().c_str(), !hardCut);
148122

149-
projectm_load_preset_file(m_projectMInstance,
150-
playlistItems.at(index).Filename().c_str(), !hardCut);
123+
if (!m_lastPresetSwitchFailed)
124+
{
125+
break;
126+
}
127+
128+
failedCount++;
129+
130+
if (failedCount >= m_presetSwitchRetryCount)
131+
{
132+
if (m_presetSwitchFailedEventCallback != nullptr)
133+
{
134+
m_presetSwitchFailedEventCallback(m_lastFailedPresetFileName.c_str(),
135+
m_lastFailedPresetError.c_str(),
136+
m_presetSwitchFailedEventUserData);
137+
}
138+
139+
return;
140+
}
141+
142+
m_lastPresetSwitchFailed = false;
143+
144+
// Remove failed preset from playlist
145+
RemoveItem(index);
146+
147+
if (playlistItems.empty())
148+
{
149+
return;
150+
}
151+
152+
// Set next index to proper value depending on navigation
153+
switch (GetLastNavigationDirection())
154+
{
155+
case NavigationDirection::Last:
156+
index = LastPresetIndex();
157+
break;
158+
case NavigationDirection::Next:
159+
index = NextPresetIndex();
160+
break;
161+
case NavigationDirection::Previous:
162+
index = PreviousPresetIndex();
163+
break;
164+
}
165+
}
151166

152167
if (m_presetSwitchedEventCallback != nullptr)
153168
{
@@ -156,13 +171,13 @@ void PlaylistCWrapper::PlayPresetIndex(uint32_t index, bool hardCut, bool resetF
156171
}
157172

158173

159-
void PlaylistCWrapper::SetLastNavigationDirection(PlaylistCWrapper::NavigationDirection direction)
174+
void PlaylistCWrapper::SetLastNavigationDirection(NavigationDirection direction)
160175
{
161176
m_lastNavigationDirection = direction;
162177
}
163178

164179

165-
auto PlaylistCWrapper::GetLastNavigationDirection() const -> PlaylistCWrapper::NavigationDirection
180+
auto PlaylistCWrapper::GetLastNavigationDirection() const -> NavigationDirection
166181
{
167182
return m_lastNavigationDirection;
168183
}
@@ -263,7 +278,7 @@ auto projectm_playlist_items(projectm_playlist_handle instance, uint32_t start,
263278

264279
if (start >= items.size())
265280
{
266-
auto* array = new char* [1] {};
281+
auto* array = new char*[1]{};
267282
return array;
268283
}
269284

src/playlist/PlaylistCWrapper.hpp

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -98,8 +98,10 @@ class PlaylistCWrapper : public Playlist
9898
private:
9999
projectm_handle m_projectMInstance{nullptr}; //!< The projectM instance handle this instance is connected to.
100100

101-
uint32_t m_presetSwitchRetryCount{5}; //!< Number of switch retries before sending the failure event to the application.
102-
uint32_t m_presetSwitchFailedCount{0}; //!< Number of retries since the last preset switch.
101+
uint32_t m_presetSwitchRetryCount{500}; //!< Number of switch retries before sending the failure event to the application.
102+
bool m_lastPresetSwitchFailed{false}; //!< Indicates that the last preset switch has failed.
103+
std::string m_lastFailedPresetFileName; //!< File name of the last failed preset.
104+
std::string m_lastFailedPresetError; //!< Error message of the last failure.
103105

104106
bool m_hardCutRequested{false}; //!< Stores the type of the last requested switch attempt.
105107

src/playlist/api/projectM-4/playlist_playback.h

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -28,8 +28,8 @@
2828

2929
#include "projectM-4/playlist_types.h"
3030

31-
#include <stdint.h>
3231
#include <stdbool.h>
32+
#include <stdint.h>
3333

3434
#ifdef __cplusplus
3535
extern "C" {
@@ -53,9 +53,10 @@ PROJECTM_PLAYLIST_EXPORT bool projectm_playlist_get_shuffle(projectm_playlist_ha
5353

5454
/**
5555
* @brief Sets the number of retries after failed preset switches.
56-
* @note Don't set this value too high, as each retry is done recursively.
56+
* @note Retry behavior changed in v4.2, using a loop. Default retry count is now 500. Failed items
57+
* are also being removed from the playlist, so they're not tried again.
5758
* @param instance The playlist manager instance.
58-
* @param retry_count The number of retries after failed preset switches. Default is 5. Set to 0
59+
* @param retry_count The number of retries after failed preset switches. Default is 500. Set to 0
5960
* to simply forward the failure event from projectM.
6061
* @since 4.0.0
6162
*/

0 commit comments

Comments
 (0)