Skip to content

Commit

Permalink
#2006 AudioOutput now requests a new source data line from the mixer …
Browse files Browse the repository at this point in the history
…when it detects that the line no longer accepts audio byte data. (#2011)

Co-authored-by: Dennis Sheirer <[email protected]>
  • Loading branch information
DSheirer and Dennis Sheirer authored Oct 13, 2024
1 parent 1b3ce43 commit 67f91b8
Showing 1 changed file with 41 additions and 4 deletions.
45 changes: 41 additions & 4 deletions src/main/java/io/github/dsheirer/audio/playback/AudioOutput.java
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@
import io.github.dsheirer.eventbus.MyEventBus;
import io.github.dsheirer.identifier.IdentifierCollection;
import io.github.dsheirer.identifier.IdentifierUpdateNotification;
import io.github.dsheirer.log.LoggingSuppressor;
import io.github.dsheirer.preference.PreferenceType;
import io.github.dsheirer.preference.UserPreferences;
import io.github.dsheirer.sample.Broadcaster;
Expand Down Expand Up @@ -58,6 +59,7 @@
public abstract class AudioOutput implements LineListener, Listener<IdentifierUpdateNotification>
{
private final static Logger mLog = LoggerFactory.getLogger(AudioOutput.class);
private static final LoggingSuppressor LOGGING_SUPPRESSOR = new LoggingSuppressor(mLog);
private int mBufferStartThreshold;
private int mBufferStopThreshold;
private Listener<IdentifierCollection> mIdentifierCollectionListener;
Expand All @@ -83,6 +85,7 @@ public abstract class AudioOutput implements LineListener, Listener<IdentifierUp
private long mOutputLastTimestamp = 0;
private static final long STALE_PLAYBACK_THRESHOLD_MS = 500;
private AudioFormat mAudioFormat;
private Line.Info mLineInfo;
private int mRequestedBufferSize;

/**
Expand All @@ -109,11 +112,12 @@ public AudioOutput(Mixer mixer, MixerChannel mixerChannel, AudioFormat audioForm
mUserPreferences = userPreferences;
mDropDuplicates = mUserPreferences.getDuplicateCallDetectionPreference().isDuplicatePlaybackSuppressionEnabled();
mAudioFormat = audioFormat;
mLineInfo = lineInfo;
mRequestedBufferSize = requestedBufferSize;

try
{
mOutput = (SourceDataLine) mMixer.getLine(lineInfo);
mOutput = (SourceDataLine) mMixer.getLine(mLineInfo);

if(mOutput != null)
{
Expand Down Expand Up @@ -270,6 +274,12 @@ private void updateToneInsertionAudioClips()
*/
private void playAudio(ByteBuffer buffer)
{
if(mOutput == null)
{
LOGGING_SUPPRESSOR.error("null output", 5, "Audio Output is null - ignoring audio playback request");
return;
}

if(buffer != null)
{
int wrote = 0;
Expand Down Expand Up @@ -298,6 +308,29 @@ private void playAudio(ByteBuffer buffer)
{
mOutput.close();

try
{
mOutput = (SourceDataLine)mMixer.getLine(mLineInfo);

if(mOutput != null)
{
LOGGING_SUPPRESSOR.info("reopen audio output success", 5,
"Closed and reopened audio output - success - mOutput is not null");
}
else
{
LOGGING_SUPPRESSOR.info("reopen audio output fail", 5,
"Closed and reopened audio output - fail - mOutput is null");
return;
}
}
catch(LineUnavailableException lue)
{
LOGGING_SUPPRESSOR.error("reopen fail for lua", 3, "Attempt to reopen " +
"audio source data line failed", lue);
return;
}

try
{
mOutput.open(mAudioFormat, mRequestedBufferSize);
Expand All @@ -315,7 +348,8 @@ private void playAudio(ByteBuffer buffer)
}
catch(IllegalArgumentException iae)
{
mLog.warn("Couldn't obtain MASTER GAIN control for stereo line [" + getChannelName() + "]");
mLog.warn("Couldn't obtain MASTER GAIN control for stereo line [" + getChannelName() +
"] after reopening the source data line");
}

try
Expand All @@ -325,7 +359,8 @@ private void playAudio(ByteBuffer buffer)
}
catch(IllegalArgumentException iae)
{
mLog.warn("Couldn't obtain MUTE control for stereo line [" + getChannelName() + "]");
mLog.warn("Couldn't obtain MUTE control for stereo line [" + getChannelName() +
"] after reopening the source data line");
}

mAudioStartEvent = new AudioEvent(AudioEvent.Type.AUDIO_STARTED, getChannelName());
Expand All @@ -334,8 +369,10 @@ private void playAudio(ByteBuffer buffer)
}
catch(LineUnavailableException e)
{
mLog.error("Couldn't obtain audio source data line for audio output - mixer [" +
LOGGING_SUPPRESSOR.error("failed reopen source data line lua", 3,
"Failed to (re)open the source data line for audio output - mixer [" +
mMixer.getMixerInfo().getName() + "]");
return;
}
}
}
Expand Down

0 comments on commit 67f91b8

Please sign in to comment.