Merge pull request #215 from DSheirer/198-audio-recorder-handles-left-open

Resolves issue audio recorders left open
This commit is contained in:
Denny Sheirer
2017-03-03 10:58:08 +00:00
committed by GitHub
3 changed files with 296 additions and 264 deletions

View File

@@ -29,38 +29,60 @@ import util.ThreadPool;
import java.io.File; import java.io.File;
import java.util.HashMap; import java.util.HashMap;
import java.util.Iterator;
import java.util.Map; import java.util.Map;
import java.util.concurrent.ScheduledFuture;
import java.util.concurrent.TimeUnit;
public class RecorderManager implements Listener<AudioPacket> public class RecorderManager implements Listener<AudioPacket>
{ {
private static final Logger mLog = LoggerFactory.getLogger(RecorderManager.class); private static final Logger mLog = LoggerFactory.getLogger(RecorderManager.class);
public static final int AUDIO_SAMPLE_RATE = 8000; public static final int AUDIO_SAMPLE_RATE = 8000;
public static final long IDLE_RECORDER_REMOVAL_THRESHOLD = 6000; //6 seconds
private Map<String,RealBufferWaveRecorder> mRecorders = new HashMap<>(); private Map<String,RealBufferWaveRecorder> mRecorders = new HashMap<>();
private ScheduledFuture<?> mRecorderMonitorFuture;
private boolean mCanStartNewRecorders = true; private boolean mCanStartNewRecorders = true;
/** /**
* Manages all audio recording for all processing channels. Reconstructs audio streams and distributes channel * Audio recording manager. Monitors stream of audio packets produced by decoding channels and automatically starts
* audio to each of the recorders, starting and stopping the recorders as needed. * audio recorders when the channel's metadata designates a call as recordable. Routes call audio to each recorder
* based on audio packet metadata. Recorders are shutdown when the channel sends an end-call audio packet
* indicating that the call is complete. A separate recording monitor periodically checks for idled recorders to
* be stopped for cases when the channel fails to send an end-call audio packet.
*/ */
public RecorderManager() public RecorderManager()
{ {
mRecorderMonitorFuture = ThreadPool.SCHEDULED.scheduleAtFixedRate(new RecorderMonitor(), 2,
2, TimeUnit.SECONDS);
} }
/**
* Prepares this class for shutdown
*/
public void dispose() public void dispose()
{ {
if(mRecorderMonitorFuture != null)
{
mRecorderMonitorFuture.cancel(true);
}
} }
/**
* Primary ingest point for audio packets from all decoding channels
* @param audioPacket to process
*/
@Override @Override
public void receive(AudioPacket audioPacket) public void receive(AudioPacket audioPacket)
{ {
if(audioPacket.hasMetadata() && if(audioPacket.hasMetadata() && audioPacket.getMetadata().isRecordable())
audioPacket.getMetadata().isRecordable())
{ {
String identifier = audioPacket.getMetadata().getUniqueIdentifier(); String identifier = audioPacket.getMetadata().getUniqueIdentifier();
synchronized(mRecorders)
{
if(mRecorders.containsKey(identifier)) if(mRecorders.containsKey(identifier))
{ {
RealBufferWaveRecorder recorder = mRecorders.get(identifier); RealBufferWaveRecorder recorder = mRecorders.get(identifier);
@@ -108,6 +130,7 @@ public class RecorderManager implements Listener<AudioPacket>
} }
} }
} }
}
/** /**
* Constructs a file name and path for an audio recording * Constructs a file name and path for an audio recording
@@ -150,4 +173,31 @@ public class RecorderManager implements Listener<AudioPacket>
return new ComplexBufferWaveRecorder(AUDIO_SAMPLE_RATE, sb.toString()); return new ComplexBufferWaveRecorder(AUDIO_SAMPLE_RATE, sb.toString());
} }
/**
* Monitors currently running recorders and removes/closes any recorders that are idle more than 6 seconds
*/
public class RecorderMonitor implements Runnable
{
@Override
public void run()
{
synchronized(mRecorders)
{
Iterator<Map.Entry<String,RealBufferWaveRecorder>> it = mRecorders.entrySet().iterator();
while(it.hasNext())
{
Map.Entry<String,RealBufferWaveRecorder> entry = it.next();
if(entry.getValue().getLastBufferReceived() + IDLE_RECORDER_REMOVAL_THRESHOLD < System.currentTimeMillis())
{
mLog.info("Removing idle recorder [" + entry.getKey() + "]");
it.remove();
entry.getValue().stop();
}
}
}
}
}
} }

View File

@@ -1,6 +1,6 @@
/******************************************************************************* /*******************************************************************************
* SDR Trunk * sdrtrunk
* Copyright (C) 2014,2015 Dennis Sheirer * Copyright (C) 2014-2017 Dennis Sheirer
* *
* This program is free software: you can redistribute it and/or modify * This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by * it under the terms of the GNU General Public License as published by
@@ -14,9 +14,20 @@
* *
* You should have received a copy of the GNU General Public License * You should have received a copy of the GNU General Public License
* along with this program. If not, see <http://www.gnu.org/licenses/> * along with this program. If not, see <http://www.gnu.org/licenses/>
*
******************************************************************************/ ******************************************************************************/
package record.wave; package record.wave;
import module.Module;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import sample.ConversionUtils;
import sample.Listener;
import sample.real.IFilteredRealBufferListener;
import sample.real.RealBuffer;
import util.TimeStamp;
import javax.sound.sampled.AudioFormat;
import java.io.IOException; import java.io.IOException;
import java.nio.file.Path; import java.nio.file.Path;
import java.nio.file.Paths; import java.nio.file.Paths;
@@ -26,27 +37,13 @@ import java.util.concurrent.ScheduledFuture;
import java.util.concurrent.TimeUnit; import java.util.concurrent.TimeUnit;
import java.util.concurrent.atomic.AtomicBoolean; import java.util.concurrent.atomic.AtomicBoolean;
import javax.sound.sampled.AudioFormat;
import module.Module;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import sample.ConversionUtils;
import sample.Listener;
import sample.real.IFilteredRealBufferListener;
import sample.real.RealBuffer;
import util.TimeStamp;
/** /**
* WAVE audio recorder module for recording real sample buffers to a wave file * WAVE audio recorder module for recording real sample buffers to a wave file
*/ */
public class RealBufferWaveRecorder extends Module public class RealBufferWaveRecorder extends Module
implements IFilteredRealBufferListener, Listener<RealBuffer> implements IFilteredRealBufferListener, Listener<RealBuffer>
{ {
private final static Logger mLog = private final static Logger mLog = LoggerFactory.getLogger(RealBufferWaveRecorder.class);
LoggerFactory.getLogger( RealBufferWaveRecorder.class );
private WaveWriter mWriter; private WaveWriter mWriter;
private String mFilePrefix; private String mFilePrefix;
@@ -55,8 +52,7 @@ public class RealBufferWaveRecorder extends Module
private BufferProcessor mBufferProcessor; private BufferProcessor mBufferProcessor;
private ScheduledFuture<?> mProcessorHandle; private ScheduledFuture<?> mProcessorHandle;
private LinkedBlockingQueue<RealBuffer> mBuffers = private LinkedBlockingQueue<RealBuffer> mBuffers = new LinkedBlockingQueue<>(500);
new LinkedBlockingQueue<>( 500 );
private long mLastBufferReceived; private long mLastBufferReceived;
private AtomicBoolean mRunning = new AtomicBoolean(); private AtomicBoolean mRunning = new AtomicBoolean();
@@ -72,6 +68,14 @@ public class RealBufferWaveRecorder extends Module
mFilePrefix = filePrefix; mFilePrefix = filePrefix;
} }
/**
* Indicates if the recorder is currently running.
*/
public boolean isRunning()
{
return mRunning.get();
}
/** /**
* Timestamp of when the latest buffer was received by this recorder * Timestamp of when the latest buffer was received by this recorder
*/ */
@@ -107,8 +111,7 @@ public class RealBufferWaveRecorder extends Module
mWriter = new WaveWriter(mAudioFormat, mFile); mWriter = new WaveWriter(mAudioFormat, mFile);
/* Schedule the processor to run every 500 milliseconds */ /* Schedule the processor to run every 500 milliseconds */
mProcessorHandle = executor.scheduleAtFixedRate( mProcessorHandle = executor.scheduleAtFixedRate(mBufferProcessor, 0, 500, TimeUnit.MILLISECONDS);
mBufferProcessor, 0, 500, TimeUnit.MILLISECONDS );
} }
catch(IOException io) catch(IOException io)
{ {
@@ -119,10 +122,23 @@ public class RealBufferWaveRecorder extends Module
public void stop() public void stop()
{ {
/* Signal the buffer processor to end with a poison pill */ if(mRunning.compareAndSet(true, false))
receive( new PoisonPill() ); {
try
{
write();
mRunning.set( false ); if(mWriter != null)
{
mWriter.close();
mWriter = null;
}
}
catch(IOException ioe)
{
mLog.error("Error writing final audio buffers to recording during shutdown", ioe);
}
}
} }
@Override @Override
@@ -134,9 +150,7 @@ public class RealBufferWaveRecorder extends Module
if(!success) if(!success)
{ {
mLog.error( "recorder buffer overflow - purging [" + mLog.error("recorder buffer overflow - purging [" + mFile.toFile().getAbsolutePath() + "]");
mFile.toFile().getAbsolutePath() + "]" );
mBuffers.clear(); mBuffers.clear();
} }
@@ -161,50 +175,28 @@ public class RealBufferWaveRecorder extends Module
{ {
} }
/**
* Writes all audio currently in the queue to the file
* @throws IOException if there are any errors writing the audio
*/
private void write() throws IOException
{
RealBuffer buffer = mBuffers.poll();
while(buffer != null)
{
mWriter.write(ConversionUtils.convertToSigned16BitSamples(buffer));
buffer = mBuffers.poll();
}
}
public class BufferProcessor implements Runnable public class BufferProcessor implements Runnable
{ {
public void run() public void run()
{ {
try try
{ {
RealBuffer buffer = mBuffers.poll(); write();
while( buffer != null )
{
if( buffer instanceof PoisonPill )
{
buffer = null;
mBuffers.clear();
if( mWriter != null )
{
try
{
mWriter.close();
}
catch( IOException io )
{
mLog.error( "Error stopping complex wave recorder [" +
getFile() + "]", io );
}
}
if( mProcessorHandle != null )
{
mProcessorHandle.cancel( true );
}
mProcessorHandle = null;
mWriter = null;
mFile = null;
}
else
{
mWriter.write( ConversionUtils.convertToSigned16BitSamples( buffer ) );
buffer = mBuffers.poll();
}
}
} }
catch(IOException ioe) catch(IOException ioe)
{ {
@@ -212,20 +204,8 @@ public class RealBufferWaveRecorder extends Module
mBuffers.clear(); mBuffers.clear();
stop(); stop();
mLog.error( "IOException while trying to write to the wave " mLog.error("IO Exception while trying to write to the wave writer", ioe);
+ "writer", ioe );
} }
} }
} }
/**
* This is used as a sentinel value to signal the buffer processor to end
*/
public class PoisonPill extends RealBuffer
{
public PoisonPill()
{
super( new float[ 1 ] );
}
}
} }

View File

@@ -1,6 +1,6 @@
/******************************************************************************* /*******************************************************************************
* SDR Trunk * sdrtrunk
* Copyright (C) 2014 Dennis Sheirer * Copyright (C) 2014-2017 Dennis Sheirer
* *
* This program is free software: you can redistribute it and/or modify * This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by * it under the terms of the GNU General Public License as published by
@@ -14,6 +14,7 @@
* *
* You should have received a copy of the GNU General Public License * You should have received a copy of the GNU General Public License
* along with this program. If not, see <http://www.gnu.org/licenses/> * along with this program. If not, see <http://www.gnu.org/licenses/>
*
******************************************************************************/ ******************************************************************************/
package record.wave; package record.wave;
@@ -34,10 +35,7 @@ import javax.sound.sampled.AudioFormat;
public class WaveWriter implements AutoCloseable public class WaveWriter implements AutoCloseable
{ {
// private final static Logger mLog = LoggerFactory.getLogger( WaveWriter.class ); private static final Pattern FILENAME_PATTERN = Pattern.compile( "(.*_)(\\d+)(\\.wav)" );
private static final Pattern FILENAME_PATTERN =
Pattern.compile( "(.*_)(\\d+)(\\.wav)" );
public static final long MAX_WAVE_SIZE = 2l * (long)Integer.MAX_VALUE; public static final long MAX_WAVE_SIZE = 2l * (long)Integer.MAX_VALUE;
private AudioFormat mAudioFormat; private AudioFormat mAudioFormat;
@@ -58,8 +56,7 @@ public class WaveWriter implements AutoCloseable
* @param maxSize - maximum file size ( range: 1 - 4,294,967,294 bytes ) * @param maxSize - maximum file size ( range: 1 - 4,294,967,294 bytes )
* @throws IOException - if there are any IO issues * @throws IOException - if there are any IO issues
*/ */
public WaveWriter( AudioFormat format, Path file, long maxSize ) public WaveWriter( AudioFormat format, Path file, long maxSize ) throws IOException
throws IOException
{ {
Validate.isTrue(format != null); Validate.isTrue(format != null);
Validate.isTrue( file != null ); Validate.isTrue( file != null );
@@ -102,15 +99,11 @@ public class WaveWriter implements AutoCloseable
while( Files.exists( mFile ) ) while( Files.exists( mFile ) )
{ {
mFile = Paths.get( mFile.toFile().getAbsolutePath() mFile = Paths.get( mFile.toFile().getAbsolutePath().replace( ".wav", "_" + version + ".wav" ) );
.replace( ".wav", "_" + version + ".wav" ) );
version++; version++;
} }
Files.createFile( mFile ); mFileChannel = (FileChannel.open( mFile, StandardOpenOption.WRITE, StandardOpenOption.CREATE_NEW ) );
mFileChannel = (FileChannel.open( mFile, StandardOpenOption.WRITE ) );
ByteBuffer header = WaveUtils.getWaveHeader( mAudioFormat ); ByteBuffer header = WaveUtils.getWaveHeader( mAudioFormat );
@@ -129,6 +122,15 @@ public class WaveWriter implements AutoCloseable
{ {
mFileChannel.force( true ); mFileChannel.force( true );
mFileChannel.close(); mFileChannel.close();
mFileChannel = null;
}
@Override
protected void finalize() throws IOException
{
mFileChannel.force( true );
mFileChannel.close();
mFileChannel = null;
} }
/** /**