Skip to content

Commit

Permalink
PlatformPlayer: Tighten deallocate() and close() methods.
Browse files Browse the repository at this point in the history
More missing bits of implementation done with, players should now
deallocate and close() properly in relation to the official docs.

DOOM II RPG doesn't seem to leak memory in players anymore, which
means that the mediaCache has probably outlived its usefulness, as
midi performance has also been improved recently after reworking
how midis are loaded in custom and default soundfonts.
  • Loading branch information
AShiningRay committed Oct 19, 2024
1 parent e040367 commit 0bce5f9
Showing 1 changed file with 16 additions and 17 deletions.
33 changes: 16 additions & 17 deletions src/org/recompile/mobile/PlatformPlayer.java
Original file line number Diff line number Diff line change
Expand Up @@ -150,11 +150,12 @@ public void close()
try
{
player.stop();
cacheDeallocate();
deallocate();
state = Player.CLOSED;
player = null;
notifyListeners(PlayerListener.CLOSED, null);
}
catch (Exception e) { }
catch (Exception e) { System.out.println("Could not close player: " + e.getMessage()); }
}

public int getState() { return state; }
Expand Down Expand Up @@ -207,14 +208,19 @@ private void notifyListeners(String event, Object eventData)
}
}

/* Due to the media cache, don't actually deallocate here when a MIDlet requests so */
public void deallocate()
{
if(getState() == Player.CLOSED) { throw new IllegalStateException("Cannot deallocate player, it is already CLOSED."); }

stop();
//player.deallocate();
state = Player.REALIZED;
player.deallocate();

/*
* Only set state to REALIZED if we have effectively moved into REALIZED or higher (PREFETCHED, etc),
* as deallocate can be called during the transition from UNREALIZED to REALIZED, and if that happens,
* we can't actually set it as REALIZED, it must be kept as UNREALIZED.
*/
if(state > Player.UNREALIZED) {state = Player.REALIZED;}
}

/*
Expand Down Expand Up @@ -375,10 +381,12 @@ public midiPlayer(InputStream stream)
}
}

public void realize()
{
public void realize() { state = Player.REALIZED; }

public void prefetch()
{
try
{
{
midi.open();

if(Manager.useCustomMidi && Manager.hasLoadedCustomMidi)
Expand All @@ -390,15 +398,6 @@ public void realize()
midi.getTransmitter().setReceiver(MidiSystem.getReceiver());
}

state = Player.REALIZED;
}
catch (MidiUnavailableException e) { System.out.println("Couldn't realize midi player: " + e.getMessage());}
}

public void prefetch()
{
try
{
/* Make a new copy of the media stream, as prefetch() can be called more than once during the player's lifecycle */
midiSequence = MidiSystem.getSequence(new ByteArrayInputStream(stream));
midi.setSequence(midiSequence);
Expand Down

0 comments on commit 0bce5f9

Please sign in to comment.