Skip to content

Commit

Permalink
Manager: Avoid deallocating streams that are currently running
Browse files Browse the repository at this point in the history
Previously, the logic behind deallocating streams only took into
account if the player index had reached the end of the array, and
started deallocating from its first position onwards. This was a
problem because it is possible that some of those positions would
still be running when FreeJ2ME went to deallocate them.

Now it will also take into account whether the current position
is running or not, and will only forcefully deallocate a position
that's running if the entire array is allocated, and all the
players are running, to make space for a new one.
  • Loading branch information
AShiningRay committed Oct 22, 2023
1 parent 1aa4a1d commit 0dfcb9a
Showing 1 changed file with 17 additions and 3 deletions.
20 changes: 17 additions & 3 deletions src/javax/microedition/media/Manager.java
Original file line number Diff line number Diff line change
Expand Up @@ -33,10 +33,24 @@ public static Player createPlayer(InputStream stream, String type) throws IOExce
if(type.equalsIgnoreCase("audio/mid") || type.equalsIgnoreCase("audio/midi") || type.equalsIgnoreCase("sp-midi") || type.equalsIgnoreCase("audio/spmidi"))
{
if(midiPlayersIndex >= midiPlayers.length) { midiPlayersIndex = 0; }
if(midiPlayers[midiPlayersIndex] != null) { midiPlayers[midiPlayersIndex].deallocate(); }
for(; midiPlayersIndex < midiPlayers.length; midiPlayersIndex++)
{
if(midiPlayers[midiPlayersIndex] == null) { break; } /* A null position means we can use it right away */
/* Otherwise, we only deallocate a position if it is not playing (running). */
else if(midiPlayers[midiPlayersIndex] != null && midiPlayers[midiPlayersIndex].getState() == Player.PREFETCHED)
{
midiPlayers[midiPlayersIndex].deallocate();
break;
}
/* If we ever reach this one, it's because all the other slots are used, and are playing */
else if(midiPlayersIndex == midiPlayers.length-1)
{
midiPlayers[midiPlayersIndex].deallocate();
break;
}
}
midiPlayers[midiPlayersIndex] = new PlatformPlayer(stream, type);
midiPlayersIndex++;
return midiPlayers[midiPlayersIndex-1];
return midiPlayers[midiPlayersIndex++];
}
else
{
Expand Down

0 comments on commit 0dfcb9a

Please sign in to comment.