diff --git a/build.gradle b/build.gradle index 426f2339..bc27a7db 100644 --- a/build.gradle +++ b/build.gradle @@ -151,6 +151,8 @@ dependencies { } implementation 'com.google.code.gson:gson:2.15.0' implementation 'com.google.guava:guava:33.8.0-jre' + implementation 'com.h2database:h2:2.5.252' //Built-in call database + implementation 'com.zaxxer:HikariCP:7.1.0' //Database connection pool implementation 'com.mpatric:mp3agic:0.9.1' implementation 'commons-io:commons-io:2.22.0' implementation 'io.github.dsheirer:jairspy:1.0.12.1' //Airspy tuners - GitHub release, see settings.gradle @@ -171,6 +173,8 @@ dependencies { implementation 'org.apache.mina:mina-core:2.2.10' implementation 'org.apache.mina:mina-http:2.2.10' implementation 'org.controlsfx:controlsfx:11.2.6' + implementation 'org.mariadb.jdbc:mariadb-java-client:3.5.11' //Optional MariaDB call database + implementation 'org.postgresql:postgresql:42.7.14' //Optional PostgreSQL call database implementation 'org.rauschig:jarchivelib:1.2.0' implementation 'org.slf4j:slf4j-api:2.0.20' } @@ -309,9 +313,11 @@ def configure(RuntimeZipTask rt, List jvmArgs) { //jdk.incubator.vector - needed for Project Panama foreign function and vector apis //jdk.accessibility is used with assistive technologies like screen readers //java.management for JVM resource monitoring + //java.sql, java.naming, java.security.jgss, jdk.net - call database JDBC drivers and connection pool //java.base, java.desktop, java.logging, java.net.http, java.prefs, javafx.controls - required by sdrtrunk (jdeps) rt.extension.addModules('jdk.charsets', 'jdk.crypto.ec', 'jdk.incubator.vector', 'jdk.accessibility', 'java.management', - 'java.base', 'java.desktop', 'java.logging', 'java.net.http', 'java.prefs', 'javafx.controls') + 'java.base', 'java.desktop', 'java.logging', 'java.net.http', 'java.prefs', 'javafx.controls', 'java.sql', + 'java.naming', 'java.security.jgss', 'jdk.net') //Use only the modules listed above. The runtime plugin's module auto-detection uses a bundled ASM library that //can't read Java 27 class files and logs a 'Failed to scan' error for each sdrtrunk class. The jdeps module check diff --git a/src/main/java/io/github/dsheirer/alias/AliasList.java b/src/main/java/io/github/dsheirer/alias/AliasList.java index 24357f4e..c657e193 100644 --- a/src/main/java/io/github/dsheirer/alias/AliasList.java +++ b/src/main/java/io/github/dsheirer/alias/AliasList.java @@ -530,6 +530,34 @@ public class AliasList return Collections.emptyList(); } + /** + * Alias for a talkgroup that is identified by its stored values instead of an identifier instance, for example a + * talkgroup from the call database. + * @param protocol of the talkgroup + * @param value of the talkgroup + * @param fullyQualifiedAddress (wacn.system.id) of a fully qualified talkgroup, or null + * @return alias or null + */ + public Alias getTalkgroupAlias(Protocol protocol, int value, String fullyQualifiedAddress) + { + TalkgroupAliasList talkgroupAliasList = protocol != null ? mTalkgroupProtocolMap.get(protocol) : null; + return talkgroupAliasList != null ? talkgroupAliasList.getAlias(value, fullyQualifiedAddress) : null; + } + + /** + * Alias for a radio that is identified by its stored values instead of an identifier instance, for example a + * radio from the call database. + * @param protocol of the radio + * @param value of the radio + * @param fullyQualifiedAddress (wacn.system.id) of a fully qualified radio, or null + * @return alias or null + */ + public Alias getRadioAlias(Protocol protocol, int value, String fullyQualifiedAddress) + { + RadioAliasList radioAliasList = protocol != null ? mRadioProtocolMap.get(protocol) : null; + return radioAliasList != null ? radioAliasList.getAlias(value, fullyQualifiedAddress) : null; + } + private static List toList(Alias alias) { if(alias != null) @@ -670,11 +698,26 @@ public class AliasList //Attempt to do a fully qualified identifier match only if(identifier instanceof FullyQualifiedTalkgroupIdentifier fqti) { - return mFullyQualifiedTalkgroupAliasMap.get(fqti.getFullyQualifiedTalkgroupAddress()); + return getAlias(fqti.getValue(), fqti.getFullyQualifiedTalkgroupAddress()); + } + + return getAlias(identifier.getValue(), null); + } + + /** + * Alias for a talkgroup value, or for a fully qualified talkgroup address. + * @param value of the talkgroup + * @param fullyQualifiedAddress (wacn.system.id) for a fully qualified talkgroup, which is matched exclusively, + * or null. + */ + public Alias getAlias(int value, String fullyQualifiedAddress) + { + if(fullyQualifiedAddress != null) + { + return mFullyQualifiedTalkgroupAliasMap.get(fullyQualifiedAddress); } //Attempt to match the talkgroup value - int value = identifier.getValue(); Alias mapValue = mTalkgroupAliasMap.get(value); if (mapValue != null) @@ -790,11 +833,26 @@ public class AliasList //Match fully qualified identifier only. if(identifier instanceof FullyQualifiedRadioIdentifier fqri) { - return mFullyQualifiedRadioAliasMap.get(fqri.getFullyQualifiedRadioAddress()); + return getAlias(fqri.getValue(), fqri.getFullyQualifiedRadioAddress()); + } + + return getAlias(identifier.getValue(), null); + } + + /** + * Alias for a radio value, or for a fully qualified radio address. + * @param value of the radio + * @param fullyQualifiedAddress (wacn.system.id) for a fully qualified radio, which is matched exclusively, or + * null. + */ + public Alias getAlias(int value, String fullyQualifiedAddress) + { + if(fullyQualifiedAddress != null) + { + return mFullyQualifiedRadioAliasMap.get(fullyQualifiedAddress); } //Attempt to match against the radio identifier - int value = identifier.getValue(); Alias mapValue = mRadioAliasMap.get(value); if(mapValue != null) diff --git a/src/main/java/io/github/dsheirer/audio/AudioEvent.java b/src/main/java/io/github/dsheirer/audio/AudioEvent.java index c77a6ea2..358499f4 100644 --- a/src/main/java/io/github/dsheirer/audio/AudioEvent.java +++ b/src/main/java/io/github/dsheirer/audio/AudioEvent.java @@ -31,6 +31,8 @@ public class AudioEvent AUDIO_STOPPED, AUDIO_CONTINUATION, AUDIO_CONFIGURATION_CHANGE_STARTED, - AUDIO_CONFIGURATION_CHANGE_COMPLETE; + AUDIO_CONFIGURATION_CHANGE_COMPLETE, + REPLAY_STARTED, + REPLAY_STOPPED; } } diff --git a/src/main/java/io/github/dsheirer/audio/AudioSegment.java b/src/main/java/io/github/dsheirer/audio/AudioSegment.java index 750b84f4..9239cadf 100644 --- a/src/main/java/io/github/dsheirer/audio/AudioSegment.java +++ b/src/main/java/io/github/dsheirer/audio/AudioSegment.java @@ -38,6 +38,7 @@ import java.util.List; import java.util.Set; import java.util.concurrent.CopyOnWriteArrayList; import java.util.concurrent.atomic.AtomicInteger; +import java.util.concurrent.atomic.AtomicLong; import javafx.beans.property.BooleanProperty; import javafx.beans.property.IntegerProperty; import javafx.beans.property.SimpleBooleanProperty; @@ -67,6 +68,8 @@ import org.slf4j.LoggerFactory; public class AudioSegment implements Listener { private final static Logger mLog = LoggerFactory.getLogger(AudioSegment.class); + private static final AtomicLong ID_GENERATOR = new AtomicLong(); + private final long mId = ID_GENERATOR.incrementAndGet(); private BooleanProperty mComplete = new SimpleBooleanProperty(false); private BooleanProperty mDuplicate = new SimpleBooleanProperty(false); private BooleanProperty mEncrypted = new SimpleBooleanProperty(false); @@ -96,6 +99,15 @@ public class AudioSegment implements Listener mIdentifierCollection.setTimeslot(timeslot); } + /** + * Unique identifier of this audio segment for the current application session. The call manager uses it as the + * call key, which links a call that is playing on an audio channel to the call in the Calls tab. + */ + public long getId() + { + return mId; + } + /** * Timeslot for this audio segment */ diff --git a/src/main/java/io/github/dsheirer/audio/convert/MP3AudioConverter.java b/src/main/java/io/github/dsheirer/audio/convert/MP3AudioConverter.java index 982021ee..35c48b12 100644 --- a/src/main/java/io/github/dsheirer/audio/convert/MP3AudioConverter.java +++ b/src/main/java/io/github/dsheirer/audio/convert/MP3AudioConverter.java @@ -97,41 +97,114 @@ public class MP3AudioConverter implements IAudioConverter audioPackets = mResampler.resample(audioPackets); } - for(int x = 0; x < audioPackets.size(); x++) + for(float[] audioPacket: audioPackets) { - byte[] bytesToEncode = null; + encodePacket(audioPacket, converted); + } - if(mInputAudioFormat.getAudioFormat().getSampleSizeInBits() == 16) + int finalChunkSize = mEncoder.encodeFinish(mOutputFramesBuffer); + + if(finalChunkSize > 0) + { + converted.add(Arrays.copyOf(mOutputFramesBuffer, finalChunkSize)); + } + + return converted; + } + + /** + * Encodes the PCM audio packet (already resampled) and adds any encoded MP3 frames to the list. + */ + private void encodePacket(float[] audioPacket, List converted) + { + byte[] bytesToEncode; + + if(mInputAudioFormat.getAudioFormat().getSampleSizeInBits() == 16) + { + bytesToEncode = ConversionUtils.convertToSigned16BitSamples(audioPacket).array(); + } + else + { + bytesToEncode = ConversionUtils.convertToSigned32BitSamples(audioPacket).array(); + } + + int bytesToEncodePointer = 0; + + int inputChunkSize = FastMath.min(mOutputFramesBuffer.length, bytesToEncode.length); + int outputChunkSize = 0; + + try + { + while(bytesToEncodePointer < bytesToEncode.length) { - bytesToEncode = ConversionUtils.convertToSigned16BitSamples(audioPackets.get(x)).array(); - } - else - { - bytesToEncode = ConversionUtils.convertToSigned32BitSamples(audioPackets.get(x)).array(); - } + outputChunkSize = mEncoder.encodeBuffer(bytesToEncode, bytesToEncodePointer, inputChunkSize, mOutputFramesBuffer); + bytesToEncodePointer += inputChunkSize; + inputChunkSize = FastMath.min(mOutputFramesBuffer.length, bytesToEncode.length - bytesToEncodePointer); - int bytesToEncodePointer = 0; - - int inputChunkSize = FastMath.min(mOutputFramesBuffer.length, bytesToEncode.length); - int outputChunkSize = 0; - - try - { - while(bytesToEncodePointer < bytesToEncode.length) + if(outputChunkSize > 0) { - outputChunkSize = mEncoder.encodeBuffer(bytesToEncode, bytesToEncodePointer, inputChunkSize, mOutputFramesBuffer); - bytesToEncodePointer += inputChunkSize; - inputChunkSize = FastMath.min(mOutputFramesBuffer.length, bytesToEncode.length - bytesToEncodePointer); - - if(outputChunkSize > 0) - { - converted.add(Arrays.copyOf(mOutputFramesBuffer, outputChunkSize)); - } + converted.add(Arrays.copyOf(mOutputFramesBuffer, outputChunkSize)); } } - catch(Exception e) + } + catch(Exception e) + { + mLog.error("There was an error converting audio to MP3: " + e.getMessage()); + } + } + + /** + * Encodes a chunk of a continuing audio stream to MP3. Unlike {@link #convert(List)}, the encoder and resampler + * keep their state between invocations, so the chunks of a call are encoded as one continuous stream. Invoke + * {@link #finishStream()} after the last chunk. Note: audio normalization is not applied. + * + * @param audioPackets of PCM audio sampled at 8 kHz + * @return encoded MP3 frames, possibly empty + */ + public List encodeStream(List audioPackets) + { + List converted = new ArrayList<>(); + + if(mResampler != null) + { + List resampled = new ArrayList<>(); + mResampler.setListener(resampled::add); + + for(float[] audioPacket: audioPackets) { - mLog.error("There was an error converting audio to MP3: " + e.getMessage()); + mResampler.resample(audioPacket); + } + + mResampler.setListener(null); + audioPackets = resampled; + } + + for(float[] audioPacket: audioPackets) + { + encodePacket(audioPacket, converted); + } + + return converted; + } + + /** + * Flushes the resampler and the encoder at the end of a stream encoded with {@link #encodeStream(List)}. + * @return final encoded MP3 frames + */ + public List finishStream() + { + List converted = new ArrayList<>(); + + if(mResampler != null) + { + List resampled = new ArrayList<>(); + mResampler.setListener(resampled::add); + mResampler.flush(); + mResampler.setListener(null); + + for(float[] audioPacket: resampled) + { + encodePacket(audioPacket, converted); } } diff --git a/src/main/java/io/github/dsheirer/audio/playback/AudioChannel.java b/src/main/java/io/github/dsheirer/audio/playback/AudioChannel.java index 7c94f6d0..81215c53 100644 --- a/src/main/java/io/github/dsheirer/audio/playback/AudioChannel.java +++ b/src/main/java/io/github/dsheirer/audio/playback/AudioChannel.java @@ -61,12 +61,17 @@ public class AudioChannel implements Listener private final String mChannelName; private final AtomicInteger mPeakLevelBits = new AtomicInteger(); - private AudioSegment mCurrentAudioSegment; + private volatile AudioSegment mCurrentAudioSegment; private Listener mIdentifierCollectionListener; private boolean mDropDuplicates; private boolean mMetadataSent = false; private boolean mMuted; private boolean mDisabled; + private volatile TalkgroupHold mHold; + private Listener mHoldListener; + private volatile Runnable mPlaybackListener; + private volatile ReplaySource mPendingReplay; + private volatile ReplaySource mReplay; private float[] mAudioSegmentStartTone; private float[] mAudioSegmentDropTone; private int mCurrentBufferIndex = -1; @@ -107,6 +112,114 @@ public class AudioChannel implements Listener } } + /** + * Hold talkgroup for this channel, or null. While a talkgroup is held, only calls to the talkgroup play on this + * channel, including calls that are otherwise flagged as do-not-monitor. The audio playback manager is + * responsible for steering calls to the channel; use {@link AudioPlaybackManager#setHold(int, TalkgroupHold)}. + */ + public TalkgroupHold getHold() + { + return mHold; + } + + /** + * Sets or clears (null) the hold talkgroup. A call that is playing and doesn't match the hold talkgroup stops + * at the next audio interval. + */ + void setHold(TalkgroupHold hold) + { + mHold = hold; + + Listener listener = mHoldListener; + + if(listener != null) + { + listener.receive(hold); + } + } + + /** + * Registers a single listener to be notified when the hold talkgroup is set (non-null) or cleared (null). + */ + public void setHoldListener(Listener listener) + { + mHoldListener = listener; + } + + /** + * Indicates if the audio segment is allowed on this channel by the hold talkgroup. + */ + public boolean isAllowedByHold(AudioSegment audioSegment) + { + TalkgroupHold hold = mHold; + return hold == null || hold.matches(audioSegment); + } + + /** + * Indicates if this channel is currently playing (or about to play) a call to its hold talkgroup. + */ + boolean isPlayingHeldTalkgroup() + { + TalkgroupHold hold = mHold; + AudioSegment current = mCurrentAudioSegment; + return hold != null && current != null && hold.matches(current); + } + + /** + * Registers a single listener to be notified when this channel starts or stops playing a call audio segment or a + * replay. The listener reads the state from {@link #getCurrentAudioSegment()} and {@link #getReplay()}. Notified + * on the audio playback thread. Used by the audio playback manager. + */ + void setPlaybackListener(Runnable listener) + { + mPlaybackListener = listener; + } + + /** + * Replays the recorded call on this channel. The replay takes over the channel at the next audio interval: a + * call that is playing is dropped and a replay that is playing is stopped. Calls queued for the channel play + * after the replay finishes. Use {@link AudioPlaybackManager#replay(ReplaySource)}. + */ + void startReplay(ReplaySource replay) + { + mPendingReplay = replay; + } + + /** + * Replay that is playing on this channel, or null. + */ + public ReplaySource getReplay() + { + return mReplay; + } + + /** + * Indicates if this channel is replaying (or about to replay) a recorded call. + */ + public boolean isReplaying() + { + ReplaySource replay = mReplay; + return mPendingReplay != null || (replay != null && !replay.isFinished()); + } + + /** + * Audio segment that this channel is currently playing, or null. + */ + public AudioSegment getCurrentAudioSegment() + { + return mCurrentAudioSegment; + } + + private void notifyPlayback() + { + Runnable listener = mPlaybackListener; + + if(listener != null) + { + listener.run(); + } + } + /** * Indicates if this audio channel is enabled. */ @@ -155,7 +268,7 @@ public class AudioChannel implements Listener */ public boolean hasAudioSegment() { - return mCurrentAudioSegment != null; + return mCurrentAudioSegment != null || mReplay != null; } /** @@ -233,6 +346,13 @@ public class AudioChannel implements Listener */ private float[] getNextAudio() { + float[] replayAudio = getReplayAudio(); + + if(replayAudio != null) + { + return replayAudio; + } + if(mAudioBuffer.isFull()) { return mAudioBuffer.get(); @@ -302,6 +422,56 @@ public class AudioChannel implements Listener return audio; } + /** + * Starts a pending replay and provides the next replay audio. Audio thread. + * @return replay audio, or null when no replay is playing (normal call playback continues) + */ + private float[] getReplayAudio() + { + ReplaySource pending = mPendingReplay; + + if(pending != null) + { + mPendingReplay = null; + + if(mReplay != null) + { + mReplay.stop(); + } + + //The replay takes over the channel: drop the buffered audio and the call that is playing + mAudioBuffer.flush(); + + if(mCurrentAudioSegment != null) + { + disposeCurrentAudioSegment(); + clearMetadata(); + } + + mReplay = pending; + notifyPlayback(); + notify(AudioEvent.Type.REPLAY_STARTED); + } + + ReplaySource replay = mReplay; + + if(replay == null) + { + return null; + } + + float[] audio = replay.next(); + + if(audio == null) + { + mReplay = null; + notifyPlayback(); + notify(AudioEvent.Type.REPLAY_STOPPED); + } + + return audio; + } + /** * Notifies listener(s) of the audio event. * @param eventType to notify for this channel @@ -342,7 +512,20 @@ public class AudioChannel implements Listener */ private boolean isThrowaway(AudioSegment audioSegment) { - return audioSegment != null && (audioSegment.isDoNotMonitor() || mDropDuplicates && (audioSegment.isDuplicate())); + if(audioSegment == null) + { + return false; + } + + TalkgroupHold hold = mHold; + + if(hold != null) + { + //A held talkgroup plays even when do-not-monitor, and nothing else plays on a held channel + return !hold.matches(audioSegment) || (mDropDuplicates && audioSegment.isDuplicate()); + } + + return audioSegment.isDoNotMonitor() || mDropDuplicates && (audioSegment.isDuplicate()); } /** @@ -388,9 +571,18 @@ public class AudioChannel implements Listener { MyEventBus.getGlobalEventBus().unregister(this); + ReplaySource replay = mReplay; + + if(replay != null) + { + replay.stop(); + } + disposeCurrentAudioSegment(); mAudioEventBroadcaster.clear(); mIdentifierCollectionListener = null; + mHoldListener = null; + mPlaybackListener = null; } /** @@ -501,6 +693,7 @@ public class AudioChannel implements Listener mCurrentAudioSegment.decrementConsumerCount(); mCurrentAudioSegment.removeIdentifierUpdateNotificationListener(this); mCurrentAudioSegment = null; + notifyPlayback(); } } @@ -538,6 +731,7 @@ public class AudioChannel implements Listener if(audioSegment != null) { + notifyPlayback(); mCurrentAudioSegment.addIdentifierUpdateNotificationListener(this); broadcast(mCurrentAudioSegment.getIdentifierCollection()); } diff --git a/src/main/java/io/github/dsheirer/audio/playback/AudioPlaybackManager.java b/src/main/java/io/github/dsheirer/audio/playback/AudioPlaybackManager.java index e5aaaf0c..b0a6c79b 100644 --- a/src/main/java/io/github/dsheirer/audio/playback/AudioPlaybackManager.java +++ b/src/main/java/io/github/dsheirer/audio/playback/AudioPlaybackManager.java @@ -53,6 +53,7 @@ public class AudioPlaybackManager implements Listener, IAudioContr private static final Logger mLog = LoggerFactory.getLogger(AudioPlaybackManager.class); private final AudioSegmentPrioritySorter mAudioSegmentPrioritySorter = new AudioSegmentPrioritySorter(); private final Broadcaster mControllerBroadcaster = new Broadcaster<>(); + private final Broadcaster mPlaybackBroadcaster = new Broadcaster<>(); private final List mAudioSegments = new ArrayList<>(); private final List mPendingAudioSegments = new ArrayList<>(); private final LinkedTransferQueue mNewAudioSegmentQueue = new LinkedTransferQueue<>(); @@ -61,6 +62,8 @@ public class AudioPlaybackManager implements Listener, IAudioContr private AudioPlaybackDeviceDescriptor mAudioPlaybackDevice; private AudioOutput mAudioOutput; private ScheduledFuture mProcessingTask; + private final TalkgroupHold[] mHolds = new TalkgroupHold[2]; + private final long[] mHoldActivity = new long[2]; /** * Constructs an instance. @@ -114,6 +117,8 @@ public class AudioPlaybackManager implements Listener, IAudioContr */ private void processAudioSegments() { + updateHoldActivity(); + //Process new audio segments queue. If segment has audio, queue it for replay, otherwise place in pending queue AudioSegment newSegment = mNewAudioSegmentQueue.poll(); @@ -182,7 +187,8 @@ public class AudioPlaybackManager implements Listener, IAudioContr { audioSegment = it.next(); - if(audioSegment.isDoNotMonitor() || (audioSegment.isDuplicate() && + if((audioSegment.isDoNotMonitor() && getHoldingChannelIndex(audioSegment) < 0) || + (audioSegment.isDuplicate() && mUserPreferences.getCallManagementPreference().isDuplicatePlaybackSuppressionEnabled())) { it.remove(); @@ -233,20 +239,7 @@ public class AudioPlaybackManager implements Listener, IAudioContr try { - //Assign empty audio outputs first - for(AudioChannel audioChannel: mAudioOutput.getAudioProvider().getAudioChannels()) - { - if(audioChannel.isEmpty()) - { - audioChannel.play(mAudioSegments.removeFirst()); - if(mAudioSegments.isEmpty()) - { - return; - } - } - } - - replaceLowerPriorityQueuedSegments(mAudioSegments, mAudioOutput.getAudioProvider().getAudioChannels()); + assignAudioSegments(mAudioSegments, mAudioOutput.getAudioProvider().getAudioChannels()); } finally { @@ -270,6 +263,352 @@ public class AudioPlaybackManager implements Listener, IAudioContr } } + /** + * Assigns waiting audio segments, in priority order, to empty audio channels. Calls to a hold talkgroup only go to + * the channel that holds the talkgroup, and other calls only go to channels without a hold talkgroup. + * + * @param waiting audio segments, sorted by playback priority. Assigned segments are removed. + * @param audioChannels to assign to. + */ + static void assignAudioSegments(List waiting, List audioChannels) + { + Iterator it = waiting.iterator(); + + while(it.hasNext()) + { + AudioSegment audioSegment = it.next(); + AudioChannel holding = getHoldingChannel(audioSegment, audioChannels); + AudioChannel target = null; + + if(holding != null) + { + if(holding.isEmpty()) + { + target = holding; + } + } + else + { + for(AudioChannel audioChannel: audioChannels) + { + if(audioChannel.getHold() == null && audioChannel.isEmpty() && !audioChannel.isReplaying()) + { + target = audioChannel; + break; + } + } + } + + if(target != null) + { + it.remove(); + target.play(audioSegment); + } + } + + //Priority replacement of queued segments only applies to channels and calls without a hold talkgroup + List unheld = new ArrayList<>(); + + for(AudioChannel audioChannel: audioChannels) + { + if(audioChannel.getHold() == null && !audioChannel.isReplaying()) + { + unheld.add(audioChannel); + } + } + + if(!unheld.isEmpty() && !waiting.isEmpty()) + { + List unmatched = new ArrayList<>(); + it = waiting.iterator(); + + while(it.hasNext()) + { + AudioSegment audioSegment = it.next(); + + if(getHoldingChannel(audioSegment, audioChannels) == null) + { + unmatched.add(audioSegment); + it.remove(); + } + } + + replaceLowerPriorityQueuedSegments(unmatched, unheld); + waiting.addAll(unmatched); + } + } + + /** + * Channel that holds the talkgroup of the audio segment, or null. + */ + static AudioChannel getHoldingChannel(AudioSegment audioSegment, List audioChannels) + { + for(AudioChannel audioChannel: audioChannels) + { + TalkgroupHold hold = audioChannel.getHold(); + + if(hold != null && hold.matches(audioSegment)) + { + return audioChannel; + } + } + + return null; + } + + /** + * Index of the channel that holds the talkgroup of the audio segment, or -1. + */ + private int getHoldingChannelIndex(AudioSegment audioSegment) + { + for(int x = 0; x < mHolds.length; x++) + { + TalkgroupHold hold = mHolds[x]; + + if(hold != null && hold.matches(audioSegment)) + { + return x; + } + } + + return -1; + } + + /** + * Sets (or clears, with null) the hold talkgroup for the audio channel. Setting a talkgroup that another channel + * holds moves the hold to this channel. Queued calls that don't match the hold talkgroup are returned for + * assignment to another channel, and a call that is playing and doesn't match stops. + * + * @param channelIndex of the audio channel (0 = left/mono, 1 = right) + * @param hold talkgroup or null to release the hold + */ + public void setHold(int channelIndex, TalkgroupHold hold) + { + if(channelIndex < 0 || channelIndex >= mHolds.length) + { + return; + } + + mAudioChannelsLock.lock(); + + try + { + if(hold != null) + { + for(int x = 0; x < mHolds.length; x++) + { + if(x != channelIndex && hold.isSameTalkgroup(mHolds[x])) + { + applyHold(x, null); + } + } + } + + applyHold(channelIndex, hold); + } + finally + { + mAudioChannelsLock.unlock(); + } + + mLog.info("Audio channel " + channelIndex + (hold != null ? " hold talkgroup [" + hold + "]" : + " hold talkgroup released")); + } + + /** + * Applies the hold to the channel. Audio channels lock must be held. + */ + private void applyHold(int channelIndex, TalkgroupHold hold) + { + mHolds[channelIndex] = hold; + mHoldActivity[channelIndex] = System.currentTimeMillis(); + + if(mAudioOutput != null) + { + List channels = mAudioOutput.getAudioProvider().getAudioChannels(); + + if(channelIndex < channels.size()) + { + AudioChannel channel = channels.get(channelIndex); + channel.setHold(hold); + + if(hold != null) + { + //Return queued calls that can no longer play on this channel + List keep = new ArrayList<>(); + AudioSegment queued = channel.pollQueuedAudioSegment(); + + while(queued != null) + { + if(hold.matches(queued)) + { + keep.add(queued); + } + else + { + mNewAudioSegmentQueue.add(queued); + } + + queued = channel.pollQueuedAudioSegment(); + } + + for(AudioSegment segment: keep) + { + channel.play(segment); + } + } + } + } + } + + /** + * Registers the listener to be notified when an audio channel starts or stops playing a call. Notified on the + * audio playback thread. + */ + public void addPlaybackListener(Listener listener) + { + mPlaybackBroadcaster.addListener(listener); + } + + public void removePlaybackListener(Listener listener) + { + mPlaybackBroadcaster.removeListener(listener); + } + + /** + * Current playback state of each audio channel, for a listener that registers after playback started. + */ + public List getChannelPlayback() + { + List playback = new ArrayList<>(); + List channels = getAudioChannels(); + + for(int x = 0; x < channels.size(); x++) + { + playback.add(getChannelPlayback(channels.get(x), x, channels.size())); + } + + return playback; + } + + private static ChannelPlayback getChannelPlayback(AudioChannel channel, int channelIndex, int channelCount) + { + AudioSegment current = channel.getCurrentAudioSegment(); + ReplaySource replay = channel.getReplay(); + return new ChannelPlayback(channelIndex, channel.getChannelName(), channelCount, + current != null ? current.getId() : 0, replay != null && !replay.isFinished() ? replay.getCallId() : 0); + } + + /** + * Replays a recorded call through the audio channels. A channel that is already replaying is reused, otherwise + * an idle channel (left first), otherwise the left (or mono) channel, which drops the call that it is playing. + * Live calls aren't assigned to the channel while it replays. + * + * @param replay to play + * @return index of the channel that replays the call, or -1 if there is no audio output + */ + public int replay(ReplaySource replay) + { + mAudioChannelsLock.lock(); + + try + { + List channels = getAudioChannels(); + + if(channels.isEmpty()) + { + return -1; + } + + int selected = -1; + + for(int x = 0; x < channels.size() && selected < 0; x++) + { + if(channels.get(x).isReplaying()) + { + selected = x; + } + } + + for(int x = 0; x < channels.size() && selected < 0; x++) + { + AudioChannel channel = channels.get(x); + + if(!channel.hasAudioSegment() && channel.isEmpty()) + { + selected = x; + } + } + + if(selected < 0) + { + selected = 0; + } + + channels.get(selected).startReplay(replay); + return selected; + } + finally + { + mAudioChannelsLock.unlock(); + } + } + + /** + * Hold talkgroup for the audio channel, or null. + */ + public TalkgroupHold getHold(int channelIndex) + { + return channelIndex >= 0 && channelIndex < mHolds.length ? mHolds[channelIndex] : null; + } + + /** + * Updates the last activity time of each hold talkgroup and releases holds that have been idle longer than the + * automatic release time. + */ + private void updateHoldActivity() + { + int minutes = mUserPreferences.getPlaybackPreference().getHoldAutoReleaseMinutes(); + long now = System.currentTimeMillis(); + + for(int x = 0; x < mHolds.length; x++) + { + TalkgroupHold hold = mHolds[x]; + + if(hold == null) + { + continue; + } + + boolean active = false; + + for(AudioSegment audioSegment: mAudioSegments) + { + if(hold.matches(audioSegment)) + { + active = true; + break; + } + } + + if(!active && mAudioOutput != null) + { + List channels = mAudioOutput.getAudioProvider().getAudioChannels(); + active = x < channels.size() && channels.get(x).isPlayingHeldTalkgroup(); + } + + if(active) + { + mHoldActivity[x] = now; + } + else if(minutes > 0 && (now - mHoldActivity[x]) > TimeUnit.MINUTES.toMillis(minutes)) + { + mLog.info("Releasing idle hold talkgroup [" + hold + "] after " + minutes + " minutes"); + setHold(x, null); + } + } + } + /** * Swaps audio segments queued on the audio channels for waiting audio segments that have a higher playback priority, * so that a segment queued behind the current playback can't hold a higher priority segment out of the channel. @@ -397,6 +736,30 @@ public class AudioPlaybackManager implements Listener, IAudioContr //Note: audio output can use an alternate device if the requested device can't be used, so we assign //the descriptor that was actually used by the audio output mAudioPlaybackDevice = mAudioOutput.getAudioPlaybackDeviceDescriptor(); + + //Re-apply hold talkgroups to the new channels (and drop a right channel hold for a mono device) + List channels = mAudioOutput.getAudioProvider().getAudioChannels(); + + for(int x = 0; x < channels.size(); x++) + { + final int channelIndex = x; + final AudioChannel channel = channels.get(x); + final int outputChannels = channels.size(); + channel.setPlaybackListener(() -> mPlaybackBroadcaster.broadcast( + getChannelPlayback(channel, channelIndex, outputChannels))); + } + + for(int x = 0; x < mHolds.length; x++) + { + if(x < channels.size()) + { + channels.get(x).setHold(mHolds[x]); + } + else + { + mHolds[x] = null; + } + } } finally { diff --git a/src/main/java/io/github/dsheirer/audio/playback/ChannelPlayback.java b/src/main/java/io/github/dsheirer/audio/playback/ChannelPlayback.java new file mode 100644 index 00000000..791a52c4 --- /dev/null +++ b/src/main/java/io/github/dsheirer/audio/playback/ChannelPlayback.java @@ -0,0 +1,49 @@ +/* + * ***************************************************************************** + * Copyright (C) 2014-2026 Dennis Sheirer + * + * 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 + * the Free Software Foundation, either version 3 of the License, or + * (at your option) any later version. + * + * This program is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU General Public License for more details. + * + * You should have received a copy of the GNU General Public License + * along with this program. If not, see + * **************************************************************************** + */ +package io.github.dsheirer.audio.playback; + +/** + * Notification that an audio channel started playing a call, or stopped playing. + * + * @param channelIndex of the audio channel in the audio output (0 = left/mono, 1 = right) + * @param channelName of the audio channel (LEFT, RIGHT or MONO) + * @param channelCount number of channels in the audio output + * @param audioSegmentId of the live call audio segment that is playing, or 0. This is the call key of the call in + * the call manager. + * @param replayCallId database id of the recorded call that is replaying, or 0. + */ +public record ChannelPlayback(int channelIndex, String channelName, int channelCount, long audioSegmentId, + long replayCallId) +{ + /** + * Indicates if the channel is playing a live call or replaying a recorded call + */ + public boolean isPlaying() + { + return audioSegmentId != 0 || replayCallId != 0; + } + + /** + * Indicates if the channel is replaying a recorded call + */ + public boolean isReplay() + { + return replayCallId != 0; + } +} diff --git a/src/main/java/io/github/dsheirer/audio/playback/ReplaySource.java b/src/main/java/io/github/dsheirer/audio/playback/ReplaySource.java new file mode 100644 index 00000000..8840103e --- /dev/null +++ b/src/main/java/io/github/dsheirer/audio/playback/ReplaySource.java @@ -0,0 +1,154 @@ +/* + * ***************************************************************************** + * Copyright (C) 2014-2026 Dennis Sheirer + * + * 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 + * the Free Software Foundation, either version 3 of the License, or + * (at your option) any later version. + * + * This program is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU General Public License for more details. + * + * You should have received a copy of the GNU General Public License + * along with this program. If not, see + * **************************************************************************** + */ +package io.github.dsheirer.audio.playback; + +/** + * Audio of a recorded call that is replayed through an audio channel. The audio channel pulls the audio in 20 ms + * intervals on the audio playback thread, and the user interface pauses, seeks and stops it from the FX thread. + */ +public class ReplaySource +{ + private final float[] mSamples; + private final long mCallId; + private final String mDescription; + private volatile int mPosition; + private volatile boolean mPaused; + private volatile boolean mStopped; + + /** + * Constructs an instance + * @param samples of 8 kHz audio + * @param callId database id of the call that is replayed + * @param description of the call for display (e.g. TO and FROM) + */ + public ReplaySource(float[] samples, long callId, String description) + { + mSamples = samples; + mCallId = callId; + mDescription = description; + } + + /** + * Database id of the replayed call + */ + public long getCallId() + { + return mCallId; + } + + /** + * Description of the replayed call for display + */ + public String getDescription() + { + return mDescription; + } + + /** + * Next 20 ms of audio, silence while paused, or null when the replay is finished or stopped. Audio thread. + */ + float[] next() + { + if(isFinished()) + { + return null; + } + + float[] audio = new float[AudioChannel.SAMPLES_PER_INTERVAL]; + + if(mPaused) + { + return audio; + } + + int position = mPosition; + int length = Math.min(audio.length, mSamples.length - position); + System.arraycopy(mSamples, position, audio, 0, length); + mPosition = position + length; + return audio; + } + + /** + * Indicates if the replay played to the end or was stopped + */ + public boolean isFinished() + { + return mStopped || mPosition >= mSamples.length; + } + + /** + * Indicates if the replay played to the end, as opposed to being stopped or replaced + */ + public boolean isCompleted() + { + return !mStopped && mPosition >= mSamples.length; + } + + public boolean isPaused() + { + return mPaused; + } + + public void setPaused(boolean paused) + { + mPaused = paused; + } + + /** + * Stops the replay. The audio channel releases it at the next audio interval. + */ + public void stop() + { + mStopped = true; + } + + /** + * Moves the replay position + * @param fraction of the recording, 0.0 to 1.0 + */ + public void seek(double fraction) + { + double clamped = Math.max(0.0, Math.min(1.0, fraction)); + mPosition = (int)Math.min(mSamples.length, Math.round(mSamples.length * clamped)); + } + + /** + * Replay position in milliseconds + */ + public long getPositionMillis() + { + return mPosition * 1000L / 8000L; + } + + /** + * Recording duration in milliseconds + */ + public long getDurationMillis() + { + return mSamples.length * 1000L / 8000L; + } + + /** + * Replay position as a fraction of the recording, 0.0 to 1.0 + */ + public double getFraction() + { + return mSamples.length > 0 ? (double)mPosition / mSamples.length : 1.0; + } +} diff --git a/src/main/java/io/github/dsheirer/audio/playback/TalkgroupHold.java b/src/main/java/io/github/dsheirer/audio/playback/TalkgroupHold.java new file mode 100644 index 00000000..3f6cfc62 --- /dev/null +++ b/src/main/java/io/github/dsheirer/audio/playback/TalkgroupHold.java @@ -0,0 +1,167 @@ +/* + * ***************************************************************************** + * Copyright (C) 2014-2026 Dennis Sheirer + * + * 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 + * the Free Software Foundation, either version 3 of the License, or + * (at your option) any later version. + * + * This program is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU General Public License for more details. + * + * You should have received a copy of the GNU General Public License + * along with this program. If not, see + * **************************************************************************** + */ +package io.github.dsheirer.audio.playback; + +import io.github.dsheirer.audio.AudioSegment; +import io.github.dsheirer.database.call.CallRecord; +import io.github.dsheirer.identifier.Identifier; +import io.github.dsheirer.identifier.IdentifierCollection; +import io.github.dsheirer.identifier.Role; +import io.github.dsheirer.identifier.patch.PatchGroup; +import io.github.dsheirer.identifier.patch.PatchGroupIdentifier; +import io.github.dsheirer.identifier.talkgroup.TalkgroupIdentifier; +import io.github.dsheirer.protocol.Protocol; + +/** + * Hold talkgroup for an audio channel. While a channel holds a talkgroup, only calls to that talkgroup play on the + * channel and calls to the talkgroup are steered to the channel. + * + * A call matches when its TO talkgroup has the same protocol and value, or when its TO patch group is (or includes) + * the talkgroup. APCO-25 phase 1 and phase 2 are treated as the same protocol since a P25 system's talkgroups are + * used on both phase 1 and phase 2 traffic channels. + * + * @param protocol of the talkgroup + * @param talkgroup value + * @param label for display (formatted talkgroup and alias) + */ +public record TalkgroupHold(Protocol protocol, int talkgroup, String label) +{ + /** + * Normalizes the protocol for comparison + */ + private static Protocol normalize(Protocol protocol) + { + return protocol == Protocol.APCO25_PHASE2 ? Protocol.APCO25 : protocol; + } + + /** + * Indicates if the talkgroup identifier is this hold talkgroup + */ + public boolean matches(TalkgroupIdentifier identifier) + { + return identifier != null && identifier.getValue() != null && identifier.getValue() == talkgroup && + normalize(identifier.getProtocol()) == normalize(protocol); + } + + /** + * Indicates if the call identifiers have this hold talkgroup (or a patch group with it) as the TO identifier. + */ + public boolean matches(IdentifierCollection identifierCollection) + { + if(identifierCollection == null) + { + return false; + } + + for(Identifier identifier: identifierCollection.getIdentifiers(Role.TO)) + { + if(identifier instanceof TalkgroupIdentifier tg && matches(tg)) + { + return true; + } + + if(identifier instanceof PatchGroupIdentifier pgi && pgi.getValue() != null) + { + PatchGroup patchGroup = pgi.getValue(); + + if(matches(patchGroup.getPatchGroup())) + { + return true; + } + + for(TalkgroupIdentifier patched: patchGroup.getPatchedTalkgroupIdentifiers()) + { + if(matches(patched)) + { + return true; + } + } + } + } + + return false; + } + + /** + * Indicates if the audio segment is a call to this hold talkgroup. + */ + public boolean matches(AudioSegment audioSegment) + { + return audioSegment != null && matches(audioSegment.getIdentifierCollection()); + } + + /** + * Creates a hold for the TO talkgroup (or patch group) of the call identifiers. + * @param identifierCollection of a call + * @param label for display + * @return hold or null if the call doesn't have a TO talkgroup + */ + public static TalkgroupHold from(IdentifierCollection identifierCollection, String label) + { + if(identifierCollection == null) + { + return null; + } + + Identifier to = identifierCollection.getToIdentifier(); + + if(to instanceof PatchGroupIdentifier pgi && pgi.getValue() != null && pgi.getValue().getPatchGroup() != null) + { + to = pgi.getValue().getPatchGroup(); + } + + if(to instanceof TalkgroupIdentifier tg && tg.getValue() != null && tg.getValue() > 0) + { + return new TalkgroupHold(tg.getProtocol(), tg.getValue(), label != null ? label : tg.toString()); + } + + return null; + } + + /** + * Creates a hold for the TO talkgroup of a stored call. + * @param call with a TO talkgroup + * @param toAlias current alias name of the TO talkgroup for the label, or null + * @return hold or null if the call doesn't have a TO talkgroup + */ + public static TalkgroupHold from(CallRecord call, String toAlias) + { + if(call == null || !call.hasTalkgroup() || call.getToValue() > Integer.MAX_VALUE) + { + return null; + } + + String label = call.getToId() + (toAlias != null ? " " + toAlias : ""); + return new TalkgroupHold(call.getProtocolValue(), call.getToValue().intValue(), label); + } + + /** + * Indicates if the other hold is for the same talkgroup (ignoring the label). + */ + public boolean isSameTalkgroup(TalkgroupHold other) + { + return other != null && other.talkgroup == talkgroup && normalize(other.protocol) == normalize(protocol); + } + + @Override + public String toString() + { + return label; + } +} diff --git a/src/main/java/io/github/dsheirer/call/CallManager.java b/src/main/java/io/github/dsheirer/call/CallManager.java new file mode 100644 index 00000000..a65ad879 --- /dev/null +++ b/src/main/java/io/github/dsheirer/call/CallManager.java @@ -0,0 +1,412 @@ +/* + * ***************************************************************************** + * Copyright (C) 2014-2026 Dennis Sheirer + * + * 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 + * the Free Software Foundation, either version 3 of the License, or + * (at your option) any later version. + * + * This program is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU General Public License for more details. + * + * You should have received a copy of the GNU General Public License + * along with this program. If not, see + * **************************************************************************** + */ +package io.github.dsheirer.call; + +import io.github.dsheirer.alias.AliasList; +import io.github.dsheirer.audio.AudioSegment; +import io.github.dsheirer.controller.NamingThreadFactory; +import io.github.dsheirer.database.DatabaseManager; +import io.github.dsheirer.database.call.CallRecord; +import io.github.dsheirer.database.call.CallState; +import io.github.dsheirer.database.call.PatchedTalkgroup; +import io.github.dsheirer.identifier.Form; +import io.github.dsheirer.identifier.Identifier; +import io.github.dsheirer.identifier.IdentifierClass; +import io.github.dsheirer.identifier.IdentifierCollection; +import io.github.dsheirer.identifier.Role; +import io.github.dsheirer.identifier.configuration.DecoderTypeConfigurationIdentifier; +import io.github.dsheirer.identifier.configuration.FrequencyConfigurationIdentifier; +import io.github.dsheirer.identifier.integer.IntegerIdentifier; +import io.github.dsheirer.identifier.patch.PatchGroupIdentifier; +import io.github.dsheirer.identifier.radio.FullyQualifiedRadioIdentifier; +import io.github.dsheirer.identifier.talkgroup.FullyQualifiedTalkgroupIdentifier; +import io.github.dsheirer.identifier.talkgroup.TalkgroupIdentifier; +import io.github.dsheirer.preference.UserPreferences; +import io.github.dsheirer.protocol.Protocol; +import io.github.dsheirer.record.CallRecorder; +import io.github.dsheirer.sample.Broadcaster; +import io.github.dsheirer.sample.Listener; +import org.slf4j.Logger; +import org.slf4j.LoggerFactory; + +import java.util.ArrayList; +import java.util.Iterator; +import java.util.List; +import java.util.Objects; +import java.util.concurrent.Executors; +import java.util.concurrent.LinkedTransferQueue; +import java.util.concurrent.ScheduledExecutorService; +import java.util.concurrent.ScheduledFuture; +import java.util.concurrent.TimeUnit; + +/** + * Manages the life-cycle of every call: the single audio segment listener for calls. + * + * Each audio segment is a call. The call manager creates the call record as soon as the call has audio (or is + * identified as encrypted), updates it as the call identifiers change and completes it when the call ends. Each + * change is saved to the call database and broadcast to the call listeners (Calls tab, and later external + * subscribers), so they see the same call activity. The {@link CallRecorder} records the call audio and fills in + * the recording details of the call record. + * + * Calls are processed on a single call manager thread every 200 milliseconds. + */ +public class CallManager implements Listener +{ + private final static Logger mLog = LoggerFactory.getLogger(CallManager.class); + private static final long PROCESSING_INTERVAL_MS = 200; + private final LinkedTransferQueue mNewAudioSegmentQueue = new LinkedTransferQueue<>(); + private final List mTrackedCalls = new ArrayList<>(); + private final Broadcaster mCallBroadcaster = new Broadcaster<>(); + private final ScheduledExecutorService mExecutor = + Executors.newSingleThreadScheduledExecutor(new NamingThreadFactory("sdrtrunk call manager")); + private final UserPreferences mUserPreferences; + private final DatabaseManager mDatabaseManager; + private final CallRecorder mCallRecorder; + private ScheduledFuture mProcessorHandle; + + /** + * Constructs an instance + * @param userPreferences for call recording and identifier formatting + * @param databaseManager to store the calls, or null to track and record calls without storing them + */ + public CallManager(UserPreferences userPreferences, DatabaseManager databaseManager) + { + mUserPreferences = userPreferences; + mDatabaseManager = databaseManager; + mCallRecorder = new CallRecorder(userPreferences); + } + + /** + * Starts the manager and begins call tracking and audio recording. + */ + public void start() + { + if(mProcessorHandle == null) + { + mProcessorHandle = mExecutor.scheduleWithFixedDelay(this::processSafely, 0, PROCESSING_INTERVAL_MS, + TimeUnit.MILLISECONDS); + } + } + + /** + * Stops the manager, completing any calls and recordings that are still in progress. + */ + public void stop() + { + if(mProcessorHandle != null) + { + mProcessorHandle.cancel(false); + mProcessorHandle = null; + + try + { + mExecutor.submit(() -> { + process(); + + for(TrackedCall trackedCall: mTrackedCalls) + { + trackedCall.complete(); + } + + mTrackedCalls.clear(); + }).get(10, TimeUnit.SECONDS); + } + catch(Exception e) + { + mLog.error("Error completing calls in progress during shutdown", e); + } + + mExecutor.shutdownNow(); + } + } + + /** + * Registers the listener to receive a snapshot of each call when it starts, changes and completes. Snapshots + * of the same call share the same {@link CallRecord#getCallKey()}, which is the {@link AudioSegment#getId()} of + * the call's audio segment. Notified on the call recorder thread. + */ + public void addCallListener(Listener listener) + { + mCallBroadcaster.addListener(listener); + } + + public void removeCallListener(Listener listener) + { + mCallBroadcaster.removeListener(listener); + } + + /** + * Primary receive method for incoming audio segments. The consumer count of the segment is already incremented + * for this manager. + */ + @Override + public void receive(AudioSegment audioSegment) + { + mNewAudioSegmentQueue.add(audioSegment); + } + + private void processSafely() + { + try + { + process(); + } + catch(Throwable t) + { + mLog.error("Error while processing calls", t); + } + } + + /** + * Processes new and tracked audio segments. Call recorder thread only. + */ + private void process() + { + AudioSegment audioSegment = mNewAudioSegmentQueue.poll(); + + while(audioSegment != null) + { + mTrackedCalls.add(new TrackedCall(audioSegment, audioSegment.getId())); + audioSegment = mNewAudioSegmentQueue.poll(); + } + + Iterator it = mTrackedCalls.iterator(); + + while(it.hasNext()) + { + TrackedCall trackedCall = it.next(); + + if(trackedCall.process()) + { + it.remove(); + } + } + } + + /** + * Updates the call record fields from the audio segment. + */ + void update(CallRecord call, AudioSegment audioSegment) + { + IdentifierCollection collection = audioSegment.getIdentifierCollection(); + AliasList aliasList = audioSegment.getAliasList(); + Identifier to = collection.getToIdentifier(); + Identifier from = collection.getFromIdentifier(); + + Protocol protocol = to != null ? to.getProtocol() : (from != null ? from.getProtocol() : null); + + if(protocol == null || protocol == Protocol.UNKNOWN) + { + Identifier decoder = collection.getIdentifier(IdentifierClass.CONFIGURATION, Form.DECODER_TYPE, Role.ANY); + + if(decoder instanceof DecoderTypeConfigurationIdentifier dtci && dtci.getValue() != null) + { + protocol = dtci.getValue().getProtocol(); + } + } + + call.setProtocol(protocol != null ? protocol.name() : null); + call.setSystem(getString(collection, Form.SYSTEM)); + call.setSite(getString(collection, Form.SITE)); + call.setChannel(getString(collection, Form.CHANNEL)); + + Identifier frequency = collection.getIdentifier(IdentifierClass.CONFIGURATION, Form.CHANNEL_FREQUENCY, Role.ANY); + + if(frequency instanceof FrequencyConfigurationIdentifier fci && fci.getValue() != null) + { + call.setFrequency(fci.getValue()); + } + + call.setTimeslot(audioSegment.getTimeslot()); + + var format = mUserPreferences.getTalkgroupFormatPreference(); + call.setToId(to != null ? format.format(to) : null); + call.setToValue(getValue(to)); + call.setToForm(to != null ? to.getForm().name() : null); + call.setToFullyQualified(to instanceof FullyQualifiedTalkgroupIdentifier fqt ? + fqt.getFullyQualifiedTalkgroupAddress() : null); + call.setFromId(from != null ? format.format(from) : null); + call.setFromValue(getValue(from)); + call.setFromForm(from != null ? from.getForm().name() : null); + call.setFromFullyQualified(from instanceof FullyQualifiedRadioIdentifier fqr ? + fqr.getFullyQualifiedRadioAddress() : null); + call.setAliasList(aliasList != null ? aliasList.getName() : null); + call.setPatchedTalkgroups(getPatchedTalkgroups(to)); + + List talkerAliases = collection.getIdentifiers(Form.TALKER_ALIAS); + call.setTalkerAlias(talkerAliases.isEmpty() ? null : talkerAliases.getFirst().toString()); + call.setEncrypted(audioSegment.isEncrypted()); + call.setDuplicate(audioSegment.isDuplicate()); + call.setPriority(audioSegment.monitorPriorityProperty().get()); + call.setAliasRecord(audioSegment.recordAudioProperty().get()); + call.setStartTime(audioSegment.getStartTimestamp()); + } + + /** + * Talkgroups patched into the patch group TO identifier, or an empty list. + */ + static List getPatchedTalkgroups(Identifier to) + { + if(!(to instanceof PatchGroupIdentifier pgi) || pgi.getValue() == null) + { + return List.of(); + } + + List patched = new ArrayList<>(); + + for(TalkgroupIdentifier talkgroup: pgi.getValue().getPatchedTalkgroupIdentifiers()) + { + if(talkgroup != null && talkgroup.getValue() != null) + { + patched.add(new PatchedTalkgroup(talkgroup.getValue(), + talkgroup instanceof FullyQualifiedTalkgroupIdentifier fqt ? + fqt.getFullyQualifiedTalkgroupAddress() : null)); + } + } + + return patched; + } + + private static String getString(IdentifierCollection collection, Form form) + { + Identifier identifier = collection.getIdentifier(IdentifierClass.CONFIGURATION, form, Role.ANY); + return identifier != null && identifier.getValue() != null ? identifier.getValue().toString() : null; + } + + /** + * Numeric value of a talkgroup, patch group (the patch group talkgroup) or radio identifier, or null. + */ + static Long getValue(Identifier identifier) + { + if(identifier instanceof PatchGroupIdentifier pgi && pgi.getValue() != null && + pgi.getValue().getPatchGroup() != null) + { + return pgi.getValue().getPatchGroup().getValue().longValue(); + } + + if(identifier instanceof IntegerIdentifier ii && ii.getValue() != null) + { + return ii.getValue().longValue(); + } + + return null; + } + + /** + * Content of a call snapshot that is compared to detect a change that needs to be saved and broadcast. + */ + private static String getSignature(CallRecord call) + { + return call.getStartTime() + "|" + call.getCallState() + "|" + call.getProtocol() + "|" + call.getSystem() + + "|" + call.getSite() + "|" + call.getChannel() + "|" + call.getFrequency() + "|" + call.getToId() + "|" + + call.getToForm() + "|" + call.getFromId() + "|" + call.getFromForm() + "|" + call.getAliasList() + "|" + call.getPatchedTalkgroups() + "|" + + call.getTalkerAlias() + "|" + + call.isEncrypted() + "|" + call.isDuplicate() + "|" + call.getPriority() + "|" + call.isAliasRecord() + "|" + + call.getRecordingState() + "|" + call.getFilePath() + "|" + call.getEndTime(); + } + + /** + * A call that is being tracked and possibly recorded. Call recorder thread only. + */ + private class TrackedCall + { + private final AudioSegment mAudioSegment; + private final CallRecord mCall = new CallRecord(); + private final DatabaseManager.CallHandle mHandle; + private final CallRecorder.Recording mRecording; + private String mLastSignature; + private boolean mPublished; + + TrackedCall(AudioSegment audioSegment, long callKey) + { + mAudioSegment = audioSegment; + mRecording = mCallRecorder.createRecording(audioSegment); + mCall.setCallKey(callKey); + mHandle = mDatabaseManager != null ? mDatabaseManager.createHandle() : null; + } + + /** + * Processes the call. + * @return true when the call is complete and should no longer be tracked + */ + boolean process() + { + boolean complete = mAudioSegment.isComplete(); + boolean hasAudio = mAudioSegment.hasAudio(); + + if(complete) + { + if(hasAudio || mPublished || mAudioSegment.isEncrypted()) + { + complete(); + } + + mAudioSegment.decrementConsumerCount(); + return true; + } + + //Wait for audio (or an encrypted call) before creating the call, so that segments that never have audio + //don't create empty calls + if(!hasAudio && !mAudioSegment.isEncrypted()) + { + return false; + } + + update(mCall, mAudioSegment); + mRecording.update(mCall); + publish(); + return false; + } + + /** + * Completes the call and its recording. + */ + void complete() + { + update(mCall, mAudioSegment); + mRecording.complete(mCall); + mCall.setEndTime(mAudioSegment.hasAudio() ? mAudioSegment.getEndTimestamp() : System.currentTimeMillis()); + mCall.setCallState(CallState.COMPLETE); + publish(); + } + + /** + * Saves and broadcasts a snapshot of the call when it changed. + */ + private void publish() + { + String signature = getSignature(mCall); + + if(!Objects.equals(signature, mLastSignature)) + { + mLastSignature = signature; + CallRecord snapshot = mCall.copy(); + mPublished = true; + + if(mDatabaseManager != null) + { + //The database writer sets the row id on its own copy + mDatabaseManager.save(mHandle, snapshot.copy()); + } + + mCallBroadcaster.broadcast(snapshot); + } + } + } +} diff --git a/src/main/java/io/github/dsheirer/database/CallDatabase.java b/src/main/java/io/github/dsheirer/database/CallDatabase.java new file mode 100644 index 00000000..6fa47f81 --- /dev/null +++ b/src/main/java/io/github/dsheirer/database/CallDatabase.java @@ -0,0 +1,347 @@ +/* + * ***************************************************************************** + * Copyright (C) 2014-2026 Dennis Sheirer + * + * 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 + * the Free Software Foundation, either version 3 of the License, or + * (at your option) any later version. + * + * This program is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU General Public License for more details. + * + * You should have received a copy of the GNU General Public License + * along with this program. If not, see + * **************************************************************************** + */ +package io.github.dsheirer.database; + +import com.zaxxer.hikari.HikariConfig; +import com.zaxxer.hikari.HikariDataSource; +import io.github.dsheirer.database.call.CallRepository; +import io.github.dsheirer.database.call.ICallRepository; +import org.slf4j.Logger; +import org.slf4j.LoggerFactory; + +import java.nio.file.Files; +import java.nio.file.Path; +import java.sql.Connection; +import java.sql.PreparedStatement; +import java.sql.ResultSet; +import java.sql.SQLException; +import java.sql.Statement; +import java.util.ArrayList; +import java.util.List; +import java.util.Locale; + +/** + * An open connection pool to one database and schema, with the sdrtrunk schema installation logic. + */ +public class CallDatabase implements AutoCloseable +{ + private static final Logger LOGGER = LoggerFactory.getLogger(CallDatabase.class); + + /** + * Version of the sdrtrunk tables that this release creates. Increment and add an upgrade step to + * {@link #getUpgradeStatements(int)} when the tables change. + */ + public static final int SCHEMA_VERSION = 2; + public static final String SCHEMA_INFO_TABLE = "schema_info"; + + private final DatabaseConfiguration mConfiguration; + private final HikariDataSource mDataSource; + private final ICallRepository mCallRepository; + + /** + * Opens a connection pool for the configuration. The schema isn't created or checked. + * @param configuration for the database + * @param databaseDirectory for the built-in database files + * @throws SQLException if the database can't be reached + */ + public CallDatabase(DatabaseConfiguration configuration, Path databaseDirectory) throws SQLException + { + mConfiguration = configuration; + mCallRepository = new CallRepository(configuration.schema()); + + if(configuration.type() == DatabaseType.H2) + { + try + { + Files.createDirectories(databaseDirectory); + } + catch(Exception e) + { + throw new SQLException("Unable to create the database directory [" + databaseDirectory + "]", e); + } + } + + try + { + Class.forName(configuration.type().getDialect().getDriverClass()); + } + catch(ClassNotFoundException cnfe) + { + throw new SQLException("Database driver isn't available: " + configuration.type().getDialect().getDriverClass(), cnfe); + } + + HikariConfig config = new HikariConfig(); + config.setPoolName("sdrtrunk-db-" + configuration.type().name().toLowerCase(Locale.ROOT)); + config.setJdbcUrl(configuration.getJdbcUrl(databaseDirectory)); + config.setDriverClassName(configuration.type().getDialect().getDriverClass()); + + if(configuration.type().isNetwork()) + { + config.setUsername(configuration.user()); + config.setPassword(configuration.password()); + } + else + { + config.setUsername("sa"); + config.setPassword(""); + } + + config.setMaximumPoolSize(4); + config.setMinimumIdle(1); + config.setConnectionTimeout(10_000); + config.setValidationTimeout(5_000); + //Fail immediately when the database can't be reached, so the user gets an answer + config.setInitializationFailTimeout(1); + + try + { + mDataSource = new HikariDataSource(config); + } + catch(Exception e) + { + Throwable cause = e.getCause() != null ? e.getCause() : e; + throw new SQLException("Unable to connect to " + configuration.getDescription() + ": " + + cause.getMessage(), cause); + } + } + + /** + * Configuration for this database + */ + public DatabaseConfiguration getConfiguration() + { + return mConfiguration; + } + + /** + * Repository for the calls table in this schema + */ + public ICallRepository getCallRepository() + { + return mCallRepository; + } + + /** + * Pooled connection. Close it to return it to the pool. + */ + public Connection getConnection() throws SQLException + { + return mDataSource.getConnection(); + } + + /** + * Determines whether the configured schema exists and whether it holds sdrtrunk tables. + */ + public SchemaStatus getSchemaStatus() throws SQLException + { + try(Connection connection = getConnection()) + { + return getSchemaStatus(connection, mConfiguration.schema()); + } + } + + /** + * Determines the status of the named schema. + */ + public static SchemaStatus getSchemaStatus(Connection connection, String schema) throws SQLException + { + boolean schemaExists; + + try(PreparedStatement statement = connection.prepareStatement("SELECT COUNT(*) FROM " + + "information_schema.schemata WHERE LOWER(schema_name) = ?")) + { + statement.setString(1, schema.toLowerCase(Locale.ROOT)); + + try(ResultSet rs = statement.executeQuery()) + { + schemaExists = rs.next() && rs.getLong(1) > 0; + } + } + + if(!schemaExists) + { + return SchemaStatus.MISSING; + } + + try(PreparedStatement statement = connection.prepareStatement("SELECT COUNT(*) FROM " + + "information_schema.tables WHERE LOWER(table_schema) = ? AND LOWER(table_name) = ?")) + { + statement.setString(1, schema.toLowerCase(Locale.ROOT)); + statement.setString(2, SCHEMA_INFO_TABLE); + + try(ResultSet rs = statement.executeQuery()) + { + return rs.next() && rs.getLong(1) > 0 ? SchemaStatus.SDRTRUNK : SchemaStatus.EMPTY; + } + } + } + + /** + * Number of calls stored in the schema, or 0 if the schema doesn't have sdrtrunk tables. + */ + public long getCallCount() throws SQLException + { + if(getSchemaStatus() != SchemaStatus.SDRTRUNK) + { + return 0; + } + + try(Connection connection = getConnection()) + { + return mCallRepository.count(connection); + } + } + + /** + * Creates the schema and tables when they don't exist and upgrades the tables of an older version. + * @throws SQLException on error, or if the schema was created by a newer version of sdrtrunk + */ + public void install() throws SQLException + { + DatabaseType type = mConfiguration.type(); + String schema = mConfiguration.schema(); + + try(Connection connection = getConnection(); Statement statement = connection.createStatement()) + { + statement.execute(type.getDialect().getCreateSchemaStatement(schema)); + statement.execute("CREATE TABLE IF NOT EXISTS " + schema + "." + SCHEMA_INFO_TABLE + + " (version INT NOT NULL)"); + + int version = getVersion(connection); + + if(version > SCHEMA_VERSION) + { + throw new SQLException("Schema [" + schema + "] was created by a newer version of sdrtrunk (schema " + + "version " + version + "). This version supports schema version " + SCHEMA_VERSION + "."); + } + + if(version < SCHEMA_VERSION) + { + for(String sql: getUpgradeStatements(version)) + { + statement.execute(sql); + } + + if(version == 0) + { + statement.executeUpdate("INSERT INTO " + schema + "." + SCHEMA_INFO_TABLE + + " (version) VALUES (" + SCHEMA_VERSION + ")"); + } + else + { + statement.executeUpdate("UPDATE " + schema + "." + SCHEMA_INFO_TABLE + " SET version = " + + SCHEMA_VERSION); + } + + LOGGER.info("Installed sdrtrunk schema version " + SCHEMA_VERSION + " in " + + mConfiguration.getDescription() + (version > 0 ? " (upgraded from version " + version + ")" : "")); + } + } + } + + /** + * Statements that bring the tables from the version argument up to the current version. + */ + private List getUpgradeStatements(int fromVersion) + { + List statements = new ArrayList<>(); + + if(fromVersion < 1) + { + statements.addAll(CallRepository.getCreateStatements(mConfiguration.type().getDialect(), mConfiguration.schema())); + } + + //Version 2: talkgroups patched into patch group calls + if(fromVersion < 2) + { + statements.addAll(CallRepository.getPatchedTalkgroupCreateStatements(mConfiguration.schema())); + } + + return statements; + } + + /** + * Installed schema version, or 0 when nothing is installed. + */ + private int getVersion(Connection connection) throws SQLException + { + try(Statement statement = connection.createStatement(); + ResultSet rs = statement.executeQuery("SELECT MAX(version) FROM " + mConfiguration.schema() + "." + + SCHEMA_INFO_TABLE)) + { + return rs.next() ? rs.getInt(1) : 0; + } + } + + /** + * Drops the schema with all of its tables and then installs an empty sdrtrunk schema. + */ + public void clear() throws SQLException + { + try(Connection connection = getConnection(); Statement statement = connection.createStatement()) + { + statement.execute(mConfiguration.type().getDialect().getDropSchemaStatement(mConfiguration.schema())); + } + + install(); + } + + /** + * Suggests an unused schema name derived from the configured schema name (e.g. sdrtrunk_2). + */ + public String suggestSchemaName() throws SQLException + { + try(Connection connection = getConnection()) + { + String base = mConfiguration.schema(); + + for(int x = 2; x < 1000; x++) + { + String candidate = base + "_" + x; + + if(candidate.length() <= 63 && getSchemaStatus(connection, candidate) == SchemaStatus.MISSING) + { + return candidate; + } + } + } + + return mConfiguration.schema() + "_new"; + } + + /** + * Indicates if the named schema exists in this database. + */ + public boolean schemaExists(String schema) throws SQLException + { + try(Connection connection = getConnection()) + { + return getSchemaStatus(connection, schema) != SchemaStatus.MISSING; + } + } + + @Override + public void close() + { + if(!mDataSource.isClosed()) + { + mDataSource.close(); + } + } +} diff --git a/src/main/java/io/github/dsheirer/database/DatabaseConfiguration.java b/src/main/java/io/github/dsheirer/database/DatabaseConfiguration.java new file mode 100644 index 00000000..52793f9b --- /dev/null +++ b/src/main/java/io/github/dsheirer/database/DatabaseConfiguration.java @@ -0,0 +1,158 @@ +/* + * ***************************************************************************** + * Copyright (C) 2014-2026 Dennis Sheirer + * + * 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 + * the Free Software Foundation, either version 3 of the License, or + * (at your option) any later version. + * + * This program is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU General Public License for more details. + * + * You should have received a copy of the GNU General Public License + * along with this program. If not, see + * **************************************************************************** + */ +package io.github.dsheirer.database; + +import java.nio.file.Path; +import java.util.Objects; +import java.util.regex.Pattern; + +/** + * Connection settings for a call database. + * + * @param type of database engine + * @param host name or address of a network database server (ignored for H2) + * @param port of the network database server (ignored for H2) + * @param database name to connect to (PostgreSQL only) + * @param schema name that holds the sdrtrunk tables + * @param user name for a network database server + * @param password for a network database server + */ +public record DatabaseConfiguration(DatabaseType type, String host, int port, String database, String schema, + String user, String password) +{ + public static final String DEFAULT_SCHEMA = "sdrtrunk"; + + /** + * Schema names are restricted to unquoted SQL identifiers so that they can be used in statements without quoting + * and so that they mean the same thing across all engines (H2 upper-cases unquoted names, the others don't). + */ + private static final Pattern SCHEMA_PATTERN = Pattern.compile("[a-z][a-z0-9_]{0,62}"); + + public DatabaseConfiguration + { + Objects.requireNonNull(type, "type"); + host = host == null ? "" : host.trim(); + database = database == null ? "" : database.trim(); + schema = schema == null || schema.isBlank() ? DEFAULT_SCHEMA : schema.trim().toLowerCase(); + user = user == null ? "" : user; + password = password == null ? "" : password; + } + + /** + * Default configuration: the built-in H2 database with the default schema. + */ + public static DatabaseConfiguration getDefault() + { + return new DatabaseConfiguration(DatabaseType.H2, "", 0, "", DEFAULT_SCHEMA, "", ""); + } + + /** + * Copy of this configuration with a different schema name. + */ + public DatabaseConfiguration withSchema(String schemaName) + { + return new DatabaseConfiguration(type, host, port, database, schemaName, user, password); + } + + /** + * Indicates if the schema name is a valid, unquoted identifier. + */ + public static boolean isValidSchemaName(String schema) + { + return schema != null && SCHEMA_PATTERN.matcher(schema).matches(); + } + + /** + * Validates this configuration. + * @return null if valid, or a description of the problem. + */ + public String validate() + { + if(!isValidSchemaName(schema)) + { + return "Schema name must start with a letter and contain only lower case letters, digits and underscores " + + "(max 63 characters)"; + } + + if(type.isNetwork()) + { + if(host.isEmpty()) + { + return "Host is required"; + } + + if(port <= 0 || port > 65535) + { + return "Port must be in the range 1 - 65535"; + } + + if(user.isEmpty()) + { + return "User name is required"; + } + } + + if(type.requiresDatabaseName() && database.isEmpty()) + { + return "Database name is required"; + } + + return null; + } + + /** + * JDBC URL for this configuration. + * @param databaseDirectory for the built-in H2 database files + */ + public String getJdbcUrl(Path databaseDirectory) + { + return type.getDialect().getJdbcUrl(this, databaseDirectory); + } + + /** + * Short description for the user, without the password. + */ + public String getDescription() + { + return switch(type) + { + case H2 -> type + " - schema [" + schema + "]"; + case MARIADB -> type + " " + user + "@" + host + ":" + port + " - schema [" + schema + "]"; + case POSTGRESQL -> type + " " + user + "@" + host + ":" + port + "/" + database + " - schema [" + + schema + "]"; + }; + } + + /** + * Indicates if this configuration refers to the same database and schema as the other configuration (ignoring + * credentials). + */ + public boolean isSameLocation(DatabaseConfiguration other) + { + return other != null && type == other.type && schema.equals(other.schema) && + (!type.isNetwork() || (host.equalsIgnoreCase(other.host) && port == other.port && + database.equals(other.database))); + } + + @Override + public String toString() + { + return getDescription(); + } +} diff --git a/src/main/java/io/github/dsheirer/database/DatabaseEvent.java b/src/main/java/io/github/dsheirer/database/DatabaseEvent.java new file mode 100644 index 00000000..0400db44 --- /dev/null +++ b/src/main/java/io/github/dsheirer/database/DatabaseEvent.java @@ -0,0 +1,45 @@ +/* + * ***************************************************************************** + * Copyright (C) 2014-2026 Dennis Sheirer + * + * 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 + * the Free Software Foundation, either version 3 of the License, or + * (at your option) any later version. + * + * This program is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU General Public License for more details. + * + * You should have received a copy of the GNU General Public License + * along with this program. If not, see + * **************************************************************************** + */ +package io.github.dsheirer.database; + +/** + * Call database status change notification + * + * @param type of event + * @param configuration of the current database + * @param errorMessage for an ERROR event, otherwise null + */ +public record DatabaseEvent(Type type, DatabaseConfiguration configuration, String errorMessage) +{ + public enum Type + { + /** + * Database opened, or a different database was selected + */ + CONNECTED, + /** + * Database couldn't be opened or the connection was lost + */ + ERROR, + /** + * Stored calls changed outside of the live call updates, for example by an age-off rule + */ + CALLS_CHANGED + } +} diff --git a/src/main/java/io/github/dsheirer/database/DatabaseManager.java b/src/main/java/io/github/dsheirer/database/DatabaseManager.java new file mode 100644 index 00000000..866ffad6 --- /dev/null +++ b/src/main/java/io/github/dsheirer/database/DatabaseManager.java @@ -0,0 +1,718 @@ +/* + * ***************************************************************************** + * Copyright (C) 2014-2026 Dennis Sheirer + * + * 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 + * the Free Software Foundation, either version 3 of the License, or + * (at your option) any later version. + * + * This program is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU General Public License for more details. + * + * You should have received a copy of the GNU General Public License + * along with this program. If not, see + * **************************************************************************** + */ +package io.github.dsheirer.database; + +import io.github.dsheirer.database.ageoff.AgeOffManager; +import io.github.dsheirer.database.call.CallRecord; +import io.github.dsheirer.database.call.ICallRepository; +import io.github.dsheirer.database.call.CallSearchCriteria; +import io.github.dsheirer.database.call.CallState; +import io.github.dsheirer.preference.UserPreferences; +import io.github.dsheirer.sample.Broadcaster; +import io.github.dsheirer.sample.Listener; +import io.github.dsheirer.controller.NamingThreadFactory; +import org.slf4j.Logger; +import org.slf4j.LoggerFactory; + +import java.nio.file.Path; +import java.sql.Connection; +import java.sql.SQLException; +import java.util.ArrayList; +import java.util.Collections; +import java.util.Iterator; +import java.util.LinkedHashMap; +import java.util.List; +import java.util.Map; +import java.util.Set; +import java.util.concurrent.CompletableFuture; +import java.util.concurrent.ExecutorService; +import java.util.concurrent.Executors; +import java.util.concurrent.ScheduledExecutorService; +import java.util.concurrent.ScheduledFuture; +import java.util.concurrent.TimeUnit; +import java.util.function.Consumer; + +/** + * Manages the call database: opens the selected database at startup, writes call records in the background, runs the + * age-off rules and switches between databases (with optional transfer of the existing calls). + * + * All writes, the age-off rules and database switches run on a single database thread, so they never overlap. + * Searches run on a separate thread so that a long write doesn't block the user interface. + * + * Call recording doesn't depend on the database: when the database is unavailable, call updates are held in memory + * (newest update per call) and written when the database becomes available again. + */ +public class DatabaseManager +{ + private static final Logger LOGGER = LoggerFactory.getLogger(DatabaseManager.class); + private static final long FLUSH_INTERVAL_MS = 250; + private static final long RETRY_INTERVAL_MS = 30_000; + private static final int MAX_PENDING_CALLS = 20_000; + private static final int TRANSFER_PAGE_SIZE = 1000; + + private final UserPreferences mUserPreferences; + private final ScheduledExecutorService mDatabaseExecutor = + Executors.newSingleThreadScheduledExecutor(new NamingThreadFactory("sdrtrunk call database")); + private final ExecutorService mQueryExecutor = + Executors.newSingleThreadExecutor(new NamingThreadFactory("sdrtrunk call database query")); + private final Broadcaster mEventBroadcaster = new Broadcaster<>(); + private final Broadcaster mSavedCallBroadcaster = new Broadcaster<>(); + private final Map mPendingCalls = new LinkedHashMap<>(); + private final AgeOffManager mAgeOffManager; + private volatile CallDatabase mDatabase; + private volatile String mErrorMessage; + private volatile int mGeneration = 1; + private long mNextRetry; + private ScheduledFuture mFlushFuture; + + /** + * Constructs an instance + * @param userPreferences for the database selection and age-off rules + */ + public DatabaseManager(UserPreferences userPreferences) + { + mUserPreferences = userPreferences; + mAgeOffManager = new AgeOffManager(this, userPreferences); + } + + /** + * Opens the selected database, installs or upgrades the sdrtrunk schema and starts the background writer. + * Blocks until the database is open (or failed to open) so that calls from the first channels are stored. + */ + public void start() + { + try + { + mDatabaseExecutor.submit(this::openConfiguredDatabase).get(30, TimeUnit.SECONDS); + } + catch(Exception e) + { + LOGGER.error("Timeout or error while opening the call database", e); + } + + mFlushFuture = mDatabaseExecutor.scheduleWithFixedDelay(this::flush, FLUSH_INTERVAL_MS, FLUSH_INTERVAL_MS, + TimeUnit.MILLISECONDS); + mAgeOffManager.start(mDatabaseExecutor); + } + + /** + * Writes any remaining call updates and closes the database. + */ + public void stop() + { + mAgeOffManager.stop(); + + if(mFlushFuture != null) + { + mFlushFuture.cancel(false); + } + + try + { + mDatabaseExecutor.submit(() -> { + flush(); + closeDatabase(); + }).get(15, TimeUnit.SECONDS); + } + catch(Exception e) + { + LOGGER.error("Error while closing the call database", e); + } + + mDatabaseExecutor.shutdownNow(); + mQueryExecutor.shutdownNow(); + } + + /** + * Directory for the built-in database files + */ + public Path getDatabaseDirectory() + { + return mUserPreferences.getDirectoryPreference().getDirectoryDatabase(); + } + + /** + * Opens the database from the user preferences. Database thread only. + */ + private void openConfiguredDatabase() + { + DatabaseConfiguration configuration = mUserPreferences.getDatabasePreference().getDatabaseConfiguration(); + + try + { + CallDatabase database = new CallDatabase(configuration, getDatabaseDirectory()); + + try + { + database.install(); + + try(Connection connection = database.getConnection()) + { + int interrupted = database.getCallRepository().markInterrupted(connection); + + if(interrupted > 0) + { + LOGGER.info("Marked " + interrupted + " calls from the previous session as interrupted"); + } + } + } + catch(SQLException se) + { + database.close(); + throw se; + } + + mDatabase = database; + mErrorMessage = null; + LOGGER.info("Call database opened: " + configuration.getDescription()); + broadcast(DatabaseEvent.Type.CONNECTED); + } + catch(SQLException se) + { + mErrorMessage = se.getMessage(); + mNextRetry = System.currentTimeMillis() + RETRY_INTERVAL_MS; + LOGGER.error("Unable to open the call database [" + configuration.getDescription() + "] - " + + se.getMessage()); + broadcast(DatabaseEvent.Type.ERROR); + } + } + + private void closeDatabase() + { + CallDatabase database = mDatabase; + mDatabase = null; + + if(database != null) + { + database.close(); + } + } + + /** + * Currently open database, or null if the database isn't available. + */ + public CallDatabase getDatabase() + { + return mDatabase; + } + + /** + * Current database configuration (from the preferences). + */ + public DatabaseConfiguration getConfiguration() + { + CallDatabase database = mDatabase; + return database != null ? database.getConfiguration() : + mUserPreferences.getDatabasePreference().getDatabaseConfiguration(); + } + + /** + * Indicates if the database is open + */ + public boolean isConnected() + { + return mDatabase != null; + } + + /** + * Description of the last error opening the database, or null. + */ + public String getErrorMessage() + { + return mErrorMessage; + } + + /** + * Creates a handle that tracks the database row of one call across updates. + */ + public CallHandle createHandle() + { + return new CallHandle(); + } + + /** + * Queues a snapshot of the call for insertion (first time) or update. Only the newest snapshot for each call is + * written, so this can be invoked as often as the call changes. + * @param handle for the call + * @param snapshot of the call that is not modified after this call + */ + public void save(CallHandle handle, CallRecord snapshot) + { + synchronized(mPendingCalls) + { + mPendingCalls.put(handle, snapshot); + + //Bound memory use if the database is unavailable for a long time. Drop the oldest completed calls first. + if(mPendingCalls.size() > MAX_PENDING_CALLS) + { + Iterator> it = mPendingCalls.entrySet().iterator(); + + while(it.hasNext() && mPendingCalls.size() > MAX_PENDING_CALLS) + { + if(!it.next().getValue().isActive()) + { + it.remove(); + } + } + + LOGGER.warn("Call database is unavailable - discarding the oldest pending call updates"); + } + } + } + + /** + * Writes the pending call updates. Database thread only. + */ + private void flush() + { + CallDatabase database = mDatabase; + + if(database == null) + { + //Retry opening the database periodically + if(mErrorMessage != null && System.currentTimeMillis() > mNextRetry) + { + openConfiguredDatabase(); + database = mDatabase; + } + + if(database == null) + { + return; + } + } + + List> batch; + + synchronized(mPendingCalls) + { + if(mPendingCalls.isEmpty()) + { + return; + } + + batch = new ArrayList<>(mPendingCalls.entrySet()); + mPendingCalls.clear(); + } + + ICallRepository repository = database.getCallRepository(); + int written = 0; + + try(Connection connection = database.getConnection()) + { + for(Map.Entry entry: batch) + { + CallHandle handle = entry.getKey(); + CallRecord call = entry.getValue(); + + if(handle.mGeneration == mGeneration && handle.mId > 0) + { + call.setId(handle.mId); + + if(repository.update(connection, call)) + { + written++; + mSavedCallBroadcaster.broadcast(call); + continue; + } + } + + //New call, a call that started before a database switch, or a row removed by an age-off rule. + call.setId(0); + handle.mId = repository.insert(connection, call); + handle.mGeneration = mGeneration; + written++; + mSavedCallBroadcaster.broadcast(call); + } + } + catch(SQLException se) + { + LOGGER.error("Error writing call updates to the database - " + se.getMessage()); + + //Put back the updates that weren't written, unless a newer update arrived in the meantime + synchronized(mPendingCalls) + { + Map newer = new LinkedHashMap<>(mPendingCalls); + mPendingCalls.clear(); + + for(int x = written; x < batch.size(); x++) + { + mPendingCalls.put(batch.get(x).getKey(), batch.get(x).getValue()); + } + + mPendingCalls.putAll(newer); + } + + if(!isReachable(database)) + { + LOGGER.error("Call database connection lost - will retry"); + closeDatabase(); + mErrorMessage = se.getMessage(); + mNextRetry = System.currentTimeMillis() + RETRY_INTERVAL_MS; + broadcast(DatabaseEvent.Type.ERROR); + } + } + } + + private static boolean isReachable(CallDatabase database) + { + try(Connection connection = database.getConnection()) + { + return connection.isValid(5); + } + catch(SQLException se) + { + return false; + } + } + + /** + * Searches the calls table on the query thread. + */ + public CompletableFuture search(CallSearchCriteria criteria, int offset, int limit) + { + return CompletableFuture.supplyAsync(() -> { + CallDatabase database = mDatabase; + + if(database == null) + { + throw new IllegalStateException("Call database is not available" + + (mErrorMessage != null ? ": " + mErrorMessage : "")); + } + + try(Connection connection = database.getConnection()) + { + List calls = database.getCallRepository().search(connection, criteria, offset, limit); + long total = database.getCallRepository().count(connection, criteria); + return new SearchResult(calls, total); + } + catch(SQLException se) + { + throw new IllegalStateException(se.getMessage(), se); + } + }, mQueryExecutor); + } + + /** + * Search results + * @param calls in the requested page + * @param total count of matching calls + */ + public record SearchResult(List calls, long total) {} + + /** + * Connects to the database configuration and reports the state of its schema. Runs on the query thread. + */ + public CompletableFuture probe(DatabaseConfiguration configuration) + { + return CompletableFuture.supplyAsync(() -> { + try(CallDatabase database = new CallDatabase(configuration, getDatabaseDirectory())) + { + SchemaStatus status = database.getSchemaStatus(); + long count = status == SchemaStatus.SDRTRUNK ? database.getCallCount() : 0; + String suggestion = status == SchemaStatus.MISSING ? configuration.schema() : + database.suggestSchemaName(); + return new ProbeResult(status, count, suggestion); + } + catch(SQLException se) + { + throw new IllegalStateException(se.getMessage(), se); + } + }, mQueryExecutor); + } + + /** + * Result of probing a database configuration + * @param status of the configured schema + * @param callCount in the configured schema + * @param suggestedSchema an unused schema name, for the create separate schema option + */ + public record ProbeResult(SchemaStatus status, long callCount, String suggestedSchema) {} + + /** + * Checks whether a schema exists in the database of the configuration. Runs on the query thread. + */ + public CompletableFuture schemaExists(DatabaseConfiguration configuration, String schema) + { + return CompletableFuture.supplyAsync(() -> { + try(CallDatabase database = new CallDatabase(configuration, getDatabaseDirectory())) + { + return database.schemaExists(schema); + } + catch(SQLException se) + { + throw new IllegalStateException(se.getMessage(), se); + } + }, mQueryExecutor); + } + + /** + * How to treat an existing sdrtrunk schema in the database being switched to + */ + public enum ExistingSchemaOption + { + /** + * Keep the existing calls and add to them + */ + APPEND, + /** + * Remove the existing calls and start empty + */ + CLEAR + } + + /** + * Switches to a different database. + * + * @param target configuration, including the schema name to use (a new name for the create separate schema option) + * @param schemaOption for an existing sdrtrunk schema in the target + * @param transfer true to copy the calls from the current database to the target + * @param progress receives status text while the switch runs (on the database thread) + * @return future that completes when the switch is complete, or completes exceptionally on error, in which case + * the current database remains selected + */ + public CompletableFuture switchDatabase(DatabaseConfiguration target, + ExistingSchemaOption schemaOption, boolean transfer, + Consumer progress) + { + CompletableFuture future = new CompletableFuture<>(); + + mDatabaseExecutor.execute(() -> { + CallDatabase targetDatabase = null; + + try + { + progress.accept("Connecting to " + target.getDescription()); + targetDatabase = new CallDatabase(target, getDatabaseDirectory()); + SchemaStatus status = targetDatabase.getSchemaStatus(); + + if(status == SchemaStatus.SDRTRUNK && schemaOption == ExistingSchemaOption.CLEAR) + { + progress.accept("Clearing schema [" + target.schema() + "]"); + targetDatabase.clear(); + } + else + { + progress.accept("Installing schema [" + target.schema() + "]"); + targetDatabase.install(); + } + + long transferred = 0; + long skipped = 0; + CallDatabase current = mDatabase; + + if(transfer && current != null && !current.getConfiguration().isSameLocation(target)) + { + //Write pending updates first so that they are included in the transfer + flush(); + long[] counts = transfer(current, targetDatabase, progress); + transferred = counts[0]; + skipped = counts[1]; + } + + try(Connection connection = targetDatabase.getConnection()) + { + targetDatabase.getCallRepository().markInterrupted(connection); + } + + //Write remaining pending updates to the current database before switching, so that active calls + //continue in the new database + flush(); + CallDatabase previous = mDatabase; + mDatabase = targetDatabase; + mGeneration++; + mErrorMessage = null; + mUserPreferences.getDatabasePreference().setDatabaseConfiguration(target); + + if(previous != null) + { + //Calls in progress continue in the new database, so remove them from the previous database + try(Connection connection = previous.getConnection()) + { + previous.getCallRepository().deleteActive(connection); + } + catch(SQLException se) + { + LOGGER.warn("Unable to remove calls in progress from the previous database - " + + se.getMessage()); + } + + previous.close(); + } + + LOGGER.info("Switched call database to " + target.getDescription() + " - transferred " + + transferred + " calls" + (skipped > 0 ? ", skipped " + skipped + " existing calls" : "")); + broadcast(DatabaseEvent.Type.CONNECTED); + future.complete(new SwitchResult(transferred, skipped)); + } + catch(Exception e) + { + LOGGER.error("Error switching the call database to " + target.getDescription(), e); + + if(targetDatabase != null && targetDatabase != mDatabase) + { + targetDatabase.close(); + } + + future.completeExceptionally(e); + } + }); + + return future; + } + + /** + * Result of a database switch + * @param transferred count of calls copied to the new database + * @param skipped count of calls that already existed in the new database + */ + public record SwitchResult(long transferred, long skipped) {} + + /** + * Copies the calls from the source to the target database, skipping calls in progress (they are written to the + * target after the switch) and calls that already exist in the target. + * @return transferred and skipped counts + */ + private long[] transfer(CallDatabase source, CallDatabase target, Consumer progress) throws SQLException + { + ICallRepository sourceRepository = source.getCallRepository(); + ICallRepository targetRepository = target.getCallRepository(); + long transferred = 0; + long skipped = 0; + + try(Connection sourceConnection = source.getConnection(); Connection targetConnection = target.getConnection()) + { + long total = sourceRepository.count(sourceConnection); + Set existing = targetRepository.count(targetConnection) > 0 ? + targetRepository.getTransferKeys(targetConnection) : Collections.emptySet(); + boolean autoCommit = targetConnection.getAutoCommit(); + targetConnection.setAutoCommit(false); + + try + { + long afterId = 0; + List page = sourceRepository.readPage(sourceConnection, afterId, TRANSFER_PAGE_SIZE); + + while(!page.isEmpty()) + { + List toInsert = new ArrayList<>(); + + for(CallRecord call: page) + { + if(call.getCallState() == CallState.ACTIVE || existing.contains(call.getTransferKey())) + { + skipped++; + } + else + { + toInsert.add(call); + } + } + + if(!toInsert.isEmpty()) + { + targetRepository.insertBatch(targetConnection, toInsert); + targetConnection.commit(); + transferred += toInsert.size(); + } + + afterId = page.getLast().getId(); + progress.accept("Transferring calls: " + (transferred + skipped) + " of " + total); + page = sourceRepository.readPage(sourceConnection, afterId, TRANSFER_PAGE_SIZE); + } + } + catch(SQLException se) + { + targetConnection.rollback(); + throw se; + } + finally + { + targetConnection.setAutoCommit(autoCommit); + } + } + + return new long[]{transferred, skipped}; + } + + /** + * Age-off rule manager + */ + public AgeOffManager getAgeOffManager() + { + return mAgeOffManager; + } + + /** + * Runs the task on the database thread. + */ + public CompletableFuture runOnDatabaseThread(Runnable runnable) + { + return CompletableFuture.runAsync(runnable, mDatabaseExecutor); + } + + /** + * Notifies listeners that the stored calls changed outside of the normal call updates (age-off, switch, delete). + */ + public void notifyCallsChanged() + { + broadcast(DatabaseEvent.Type.CALLS_CHANGED); + } + + private void broadcast(DatabaseEvent.Type type) + { + mEventBroadcaster.broadcast(new DatabaseEvent(type, getConfiguration(), mErrorMessage)); + } + + /** + * Registers the listener to receive database events (on the database thread) + */ + public void addListener(Listener listener) + { + mEventBroadcaster.addListener(listener); + } + + public void removeListener(Listener listener) + { + mEventBroadcaster.removeListener(listener); + } + + /** + * Registers the listener to receive each call after it is written to the database, with its row id assigned. + * Notified on the database thread. + */ + public void addCallSavedListener(Listener listener) + { + mSavedCallBroadcaster.addListener(listener); + } + + public void removeCallSavedListener(Listener listener) + { + mSavedCallBroadcaster.removeListener(listener); + } + + /** + * Tracks the database row of a call. Accessed only on the database thread. + */ + public static class CallHandle + { + private long mId; + private int mGeneration; + } +} diff --git a/src/main/java/io/github/dsheirer/database/DatabaseType.java b/src/main/java/io/github/dsheirer/database/DatabaseType.java new file mode 100644 index 00000000..b6331544 --- /dev/null +++ b/src/main/java/io/github/dsheirer/database/DatabaseType.java @@ -0,0 +1,81 @@ +/* + * ***************************************************************************** + * Copyright (C) 2014-2026 Dennis Sheirer + * + * 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 + * the Free Software Foundation, either version 3 of the License, or + * (at your option) any later version. + * + * This program is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU General Public License for more details. + * + * You should have received a copy of the GNU General Public License + * along with this program. If not, see + * **************************************************************************** + */ +package io.github.dsheirer.database; + +import io.github.dsheirer.database.dialect.H2Dialect; +import io.github.dsheirer.database.dialect.IDatabaseDialect; +import io.github.dsheirer.database.dialect.MariaDBDialect; +import io.github.dsheirer.database.dialect.PostgreSQLDialect; + +/** + * Supported call database engines. The engine differences are implemented by each type's {@link IDatabaseDialect}. + */ +public enum DatabaseType +{ + H2("Built-in (H2)", new H2Dialect()), + MARIADB("MariaDB", new MariaDBDialect()), + POSTGRESQL("PostgreSQL", new PostgreSQLDialect()); + + private final String mLabel; + private final IDatabaseDialect mDialect; + + DatabaseType(String label, IDatabaseDialect dialect) + { + mLabel = label; + mDialect = dialect; + } + + /** + * SQL dialect and connection settings for this engine + */ + public IDatabaseDialect getDialect() + { + return mDialect; + } + + /** + * Indicates if this is a network database server that needs a host, port and credentials. + */ + public boolean isNetwork() + { + return mDialect.isNetwork(); + } + + /** + * Indicates if this engine needs a database name in addition to the schema name. + */ + public boolean requiresDatabaseName() + { + return mDialect.requiresDatabaseName(); + } + + /** + * Default TCP port for a network database, or 0 for the embedded database. + */ + public int getDefaultPort() + { + return mDialect.getDefaultPort(); + } + + @Override + public String toString() + { + return mLabel; + } +} diff --git a/src/main/java/io/github/dsheirer/database/SchemaStatus.java b/src/main/java/io/github/dsheirer/database/SchemaStatus.java new file mode 100644 index 00000000..53719ffd --- /dev/null +++ b/src/main/java/io/github/dsheirer/database/SchemaStatus.java @@ -0,0 +1,38 @@ +/* + * ***************************************************************************** + * Copyright (C) 2014-2026 Dennis Sheirer + * + * 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 + * the Free Software Foundation, either version 3 of the License, or + * (at your option) any later version. + * + * This program is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU General Public License for more details. + * + * You should have received a copy of the GNU General Public License + * along with this program. If not, see + * **************************************************************************** + */ +package io.github.dsheirer.database; + +/** + * State of a schema in a database + */ +public enum SchemaStatus +{ + /** + * Schema doesn't exist + */ + MISSING, + /** + * Schema exists but doesn't have sdrtrunk tables + */ + EMPTY, + /** + * Schema has sdrtrunk tables + */ + SDRTRUNK +} diff --git a/src/main/java/io/github/dsheirer/database/ageoff/AgeOffAction.java b/src/main/java/io/github/dsheirer/database/ageoff/AgeOffAction.java new file mode 100644 index 00000000..7d769290 --- /dev/null +++ b/src/main/java/io/github/dsheirer/database/ageoff/AgeOffAction.java @@ -0,0 +1,41 @@ +/* + * ***************************************************************************** + * Copyright (C) 2014-2026 Dennis Sheirer + * + * 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 + * the Free Software Foundation, either version 3 of the License, or + * (at your option) any later version. + * + * This program is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU General Public License for more details. + * + * You should have received a copy of the GNU General Public License + * along with this program. If not, see + * **************************************************************************** + */ +package io.github.dsheirer.database.ageoff; + +/** + * What an age-off rule removes + */ +public enum AgeOffAction +{ + DELETE_CALL_AND_RECORDING("Delete the call and its recording"), + DELETE_RECORDING_ONLY("Delete the recording, keep the call"); + + private final String mLabel; + + AgeOffAction(String label) + { + mLabel = label; + } + + @Override + public String toString() + { + return mLabel; + } +} diff --git a/src/main/java/io/github/dsheirer/database/ageoff/AgeOffManager.java b/src/main/java/io/github/dsheirer/database/ageoff/AgeOffManager.java new file mode 100644 index 00000000..8ae295c3 --- /dev/null +++ b/src/main/java/io/github/dsheirer/database/ageoff/AgeOffManager.java @@ -0,0 +1,329 @@ +/* + * ***************************************************************************** + * Copyright (C) 2014-2026 Dennis Sheirer + * + * 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 + * the Free Software Foundation, either version 3 of the License, or + * (at your option) any later version. + * + * This program is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU General Public License for more details. + * + * You should have received a copy of the GNU General Public License + * along with this program. If not, see + * **************************************************************************** + */ +package io.github.dsheirer.database.ageoff; + +import io.github.dsheirer.database.CallDatabase; +import io.github.dsheirer.database.DatabaseManager; +import io.github.dsheirer.database.call.CallRecord; +import io.github.dsheirer.database.call.ICallRepository; +import io.github.dsheirer.preference.UserPreferences; +import io.github.dsheirer.preference.database.DatabasePreference; +import org.slf4j.Logger; +import org.slf4j.LoggerFactory; + +import java.io.IOException; +import java.nio.file.Files; +import java.nio.file.Path; +import java.sql.Connection; +import java.sql.SQLException; +import java.util.ArrayList; +import java.util.List; +import java.util.Locale; +import java.util.concurrent.CompletableFuture; +import java.util.concurrent.ScheduledExecutorService; +import java.util.concurrent.ScheduledFuture; +import java.util.concurrent.TimeUnit; + +/** + * Applies the call audio age-off rules: + *
    + *
  • Retention rule: deletes duplicate calls, and Do Not Monitor calls that no alias marked for recording, with + * their recordings, after the retention period (default 1 hour).
  • + *
  • Age rule: removes calls older than a number of days.
  • + *
  • Size rule: removes the oldest recordings when the total size of the recordings exceeds a limit.
  • + *
+ * The age and size rules either delete the call row and its recording file, or delete only the recording file and + * keep the call row. + * + * The rules run a minute after startup, then every 5 minutes, and when the user requests it. Calls in progress are + * never removed. Only recordings tracked in the call database are considered. + */ +public class AgeOffManager +{ + private static final Logger LOGGER = LoggerFactory.getLogger(AgeOffManager.class); + private static final long INITIAL_DELAY_SECONDS = 60; + private static final long INTERVAL_SECONDS = 300; + private static final int PAGE_SIZE = 500; + public static final long BYTES_PER_MB = 1024L * 1024L; + + private final DatabaseManager mDatabaseManager; + private final DatabasePreference mPreference; + private ScheduledFuture mScheduledFuture; + + /** + * Constructs an instance + */ + public AgeOffManager(DatabaseManager databaseManager, UserPreferences userPreferences) + { + mDatabaseManager = databaseManager; + mPreference = userPreferences.getDatabasePreference(); + } + + /** + * Schedules the periodic age-off on the database thread. + */ + public void start(ScheduledExecutorService databaseExecutor) + { + if(mScheduledFuture == null) + { + mScheduledFuture = databaseExecutor.scheduleWithFixedDelay(this::runSafely, INITIAL_DELAY_SECONDS, + INTERVAL_SECONDS, TimeUnit.SECONDS); + } + } + + public void stop() + { + if(mScheduledFuture != null) + { + mScheduledFuture.cancel(false); + mScheduledFuture = null; + } + } + + /** + * Runs the enabled rules now. + * @return future with the results + */ + public CompletableFuture runNow() + { + CompletableFuture future = new CompletableFuture<>(); + mDatabaseManager.runOnDatabaseThread(() -> { + try + { + future.complete(run()); + } + catch(Exception e) + { + future.completeExceptionally(e); + } + }); + return future; + } + + private void runSafely() + { + try + { + run(); + } + catch(Exception e) + { + LOGGER.error("Error applying the call age-off rules", e); + } + } + + /** + * Applies the enabled rules. Database thread only. + */ + private AgeOffResult run() throws SQLException + { + CallDatabase database = mDatabaseManager.getDatabase(); + + RetentionPeriod retention = mPreference.getDuplicateDoNotMonitorRetention(); + + if(database == null || (!retention.isEnabled() && !mPreference.isAgeRuleEnabled() && + !mPreference.isSizeRuleEnabled())) + { + return new AgeOffResult(0, 0, 0); + } + + AgeOffResult result = new AgeOffResult(0, 0, 0); + + try(Connection connection = database.getConnection()) + { + ICallRepository repository = database.getCallRepository(); + + if(retention.isEnabled()) + { + long cutoff = System.currentTimeMillis() - retention.getMilliseconds(); + result = result.add(applyRetentionRule(connection, repository, cutoff)); + } + + if(mPreference.isAgeRuleEnabled()) + { + long cutoff = System.currentTimeMillis() - TimeUnit.DAYS.toMillis(mPreference.getAgeRuleDays()); + result = result.add(applyAgeRule(connection, repository, cutoff, mPreference.getAgeRuleAction())); + } + + if(mPreference.isSizeRuleEnabled()) + { + long limit = mPreference.getSizeRuleLimitMB() * BYTES_PER_MB; + result = result.add(applySizeRule(connection, repository, limit, mPreference.getSizeRuleAction())); + } + } + + if(result.calls() > 0 || result.recordings() > 0) + { + LOGGER.info("Call age-off removed " + result.calls() + " calls and " + result.recordings() + + " recordings (" + (result.bytes() / BYTES_PER_MB) + " MB)"); + mDatabaseManager.notifyCallsChanged(); + } + + return result; + } + + /** + * Deletes duplicate calls and Do Not Monitor calls (without an alias record option) that started before the + * cutoff time, with their recordings. + */ + static AgeOffResult applyRetentionRule(Connection connection, ICallRepository repository, long cutoff) + throws SQLException + { + AgeOffResult result = new AgeOffResult(0, 0, 0); + List page = repository.getDuplicateAndDoNotMonitorCalls(connection, cutoff, PAGE_SIZE); + + while(!page.isEmpty()) + { + result = result.add(remove(connection, repository, page, AgeOffAction.DELETE_CALL_AND_RECORDING)); + page = repository.getDuplicateAndDoNotMonitorCalls(connection, cutoff, PAGE_SIZE); + } + + return result; + } + + /** + * Removes the calls (or recordings) of calls that started before the cutoff time. + */ + static AgeOffResult applyAgeRule(Connection connection, ICallRepository repository, long cutoff, + AgeOffAction action) throws SQLException + { + AgeOffResult result = new AgeOffResult(0, 0, 0); + List page = action == AgeOffAction.DELETE_CALL_AND_RECORDING ? + repository.getCallsStartedBefore(connection, cutoff, PAGE_SIZE) : + repository.getRecordedCalls(connection, cutoff, PAGE_SIZE); + + while(!page.isEmpty()) + { + result = result.add(remove(connection, repository, page, action)); + + page = action == AgeOffAction.DELETE_CALL_AND_RECORDING ? + repository.getCallsStartedBefore(connection, cutoff, PAGE_SIZE) : + repository.getRecordedCalls(connection, cutoff, PAGE_SIZE); + } + + return result; + } + + /** + * Removes the oldest recordings until the total size of the recordings is at or below the limit. + */ + static AgeOffResult applySizeRule(Connection connection, ICallRepository repository, long limitBytes, + AgeOffAction action) throws SQLException + { + AgeOffResult result = new AgeOffResult(0, 0, 0); + long total = repository.getRecordedFileSize(connection); + + while(total > limitBytes) + { + List page = repository.getRecordedCalls(connection, 0, PAGE_SIZE); + + if(page.isEmpty()) + { + break; + } + + List toRemove = new ArrayList<>(); + + for(CallRecord call: page) + { + if(total <= limitBytes) + { + break; + } + + toRemove.add(call); + total -= call.getFileSize(); + } + + result = result.add(remove(connection, repository, toRemove, action)); + } + + return result; + } + + /** + * Deletes the recording files of the calls and then deletes the call rows or marks their recordings deleted. + */ + private static AgeOffResult remove(Connection connection, ICallRepository repository, List calls, + AgeOffAction action) throws SQLException + { + long bytes = 0; + int recordings = 0; + List ids = new ArrayList<>(); + + for(CallRecord call: calls) + { + ids.add(call.getId()); + + if(call.getFilePath() != null && deleteRecording(call.getFilePath())) + { + recordings++; + bytes += call.getFileSize(); + } + } + + if(action == AgeOffAction.DELETE_CALL_AND_RECORDING) + { + repository.delete(connection, ids); + return new AgeOffResult(ids.size(), recordings, bytes); + } + + repository.markRecordingsDeleted(connection, ids); + return new AgeOffResult(0, recordings, bytes); + } + + /** + * Deletes the audio recording file. Only audio files (.mp3, .wav) are deleted. + * @return true if the file was deleted + */ + static boolean deleteRecording(String filePath) + { + String lower = filePath.toLowerCase(Locale.ROOT); + + if(!lower.endsWith(".mp3") && !lower.endsWith(".wav")) + { + LOGGER.warn("Age-off skipped deleting non-audio file [" + filePath + "]"); + return false; + } + + try + { + return Files.deleteIfExists(Path.of(filePath)); + } + catch(IOException | RuntimeException e) + { + LOGGER.warn("Age-off unable to delete recording [" + filePath + "] - " + e.getMessage()); + return false; + } + } + + /** + * Age-off results + * @param calls deleted + * @param recordings deleted + * @param bytes of recordings deleted + */ + public record AgeOffResult(long calls, long recordings, long bytes) + { + public AgeOffResult add(AgeOffResult other) + { + return new AgeOffResult(calls + other.calls, recordings + other.recordings, bytes + other.bytes); + } + } +} diff --git a/src/main/java/io/github/dsheirer/database/ageoff/RetentionPeriod.java b/src/main/java/io/github/dsheirer/database/ageoff/RetentionPeriod.java new file mode 100644 index 00000000..ad9f6639 --- /dev/null +++ b/src/main/java/io/github/dsheirer/database/ageoff/RetentionPeriod.java @@ -0,0 +1,71 @@ +/* + * ***************************************************************************** + * Copyright (C) 2014-2026 Dennis Sheirer + * + * 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 + * the Free Software Foundation, either version 3 of the License, or + * (at your option) any later version. + * + * This program is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU General Public License for more details. + * + * You should have received a copy of the GNU General Public License + * along with this program. If not, see + * **************************************************************************** + */ +package io.github.dsheirer.database.ageoff; + +import java.util.concurrent.TimeUnit; + +/** + * How long duplicate and Do Not Monitor calls (and their recordings) are kept before they are deleted + */ +public enum RetentionPeriod +{ + MINUTES_30("30 minutes", TimeUnit.MINUTES.toMillis(30)), + HOUR_1("1 hour", TimeUnit.HOURS.toMillis(1)), + HOURS_2("2 hours", TimeUnit.HOURS.toMillis(2)), + HOURS_4("4 hours", TimeUnit.HOURS.toMillis(4)), + HOURS_8("8 hours", TimeUnit.HOURS.toMillis(8)), + HOURS_12("12 hours", TimeUnit.HOURS.toMillis(12)), + DAY_1("1 day", TimeUnit.DAYS.toMillis(1)), + DAYS_2("2 days", TimeUnit.DAYS.toMillis(2)), + DAYS_7("7 days", TimeUnit.DAYS.toMillis(7)), + NEVER("Never delete", 0); + + public static final RetentionPeriod DEFAULT = HOUR_1; + + private final String mLabel; + private final long mMilliseconds; + + RetentionPeriod(String label, long milliseconds) + { + mLabel = label; + mMilliseconds = milliseconds; + } + + /** + * Retention time in milliseconds, or 0 for never + */ + public long getMilliseconds() + { + return mMilliseconds; + } + + /** + * Indicates if calls are deleted after this period + */ + public boolean isEnabled() + { + return this != NEVER; + } + + @Override + public String toString() + { + return mLabel; + } +} diff --git a/src/main/java/io/github/dsheirer/database/call/CallRecord.java b/src/main/java/io/github/dsheirer/database/call/CallRecord.java new file mode 100644 index 00000000..4ccb6d6f --- /dev/null +++ b/src/main/java/io/github/dsheirer/database/call/CallRecord.java @@ -0,0 +1,568 @@ +/* + * ***************************************************************************** + * Copyright (C) 2014-2026 Dennis Sheirer + * + * 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 + * the Free Software Foundation, either version 3 of the License, or + * (at your option) any later version. + * + * This program is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU General Public License for more details. + * + * You should have received a copy of the GNU General Public License + * along with this program. If not, see + * **************************************************************************** + */ +package io.github.dsheirer.database.call; + +import io.github.dsheirer.protocol.Protocol; + +import java.util.List; + +/** + * A call audio event as stored in the calls table. + * + * Instances are mutable while they are being built, but a record that has been handed to the database writer or to + * the user interface is a snapshot that is never changed afterwards. Use {@link #copy()} to create a new snapshot. + */ +public class CallRecord +{ + public static final String FORM_TALKGROUP = "TALKGROUP"; + public static final String FORM_PATCH_GROUP = "PATCH_GROUP"; + public static final String FORM_RADIO = "RADIO"; + + private long mId; + private long mCallKey; + private long mStartTime; + private long mEndTime; + private CallState mCallState = CallState.ACTIVE; + private String mProtocol; + private String mSystem; + private String mSite; + private String mChannel; + private long mFrequency; + private int mTimeslot; + private String mToId; + private Long mToValue; + private String mToForm; + private String mToFullyQualified; + private String mFromId; + private Long mFromValue; + private String mFromForm; + private String mFromFullyQualified; + private String mAliasList; + private List mPatchedTalkgroups = List.of(); + private String mTalkerAlias; + private boolean mEncrypted; + private boolean mDuplicate; + private int mPriority; + private boolean mAliasRecord; + private String mRecordFormat; + private String mFilePath; + private long mFileSize; + private RecordingState mRecordingState = RecordingState.NONE; + + /** + * Creates a copy (snapshot) of this record. + */ + public CallRecord copy() + { + CallRecord copy = new CallRecord(); + copy.mId = mId; + copy.mCallKey = mCallKey; + copy.mStartTime = mStartTime; + copy.mEndTime = mEndTime; + copy.mCallState = mCallState; + copy.mProtocol = mProtocol; + copy.mSystem = mSystem; + copy.mSite = mSite; + copy.mChannel = mChannel; + copy.mFrequency = mFrequency; + copy.mTimeslot = mTimeslot; + copy.mToId = mToId; + copy.mToValue = mToValue; + copy.mToForm = mToForm; + copy.mToFullyQualified = mToFullyQualified; + copy.mFromId = mFromId; + copy.mFromValue = mFromValue; + copy.mFromForm = mFromForm; + copy.mFromFullyQualified = mFromFullyQualified; + copy.mAliasList = mAliasList; + copy.mPatchedTalkgroups = mPatchedTalkgroups; + copy.mTalkerAlias = mTalkerAlias; + copy.mEncrypted = mEncrypted; + copy.mDuplicate = mDuplicate; + copy.mPriority = mPriority; + copy.mAliasRecord = mAliasRecord; + copy.mRecordFormat = mRecordFormat; + copy.mFilePath = mFilePath; + copy.mFileSize = mFileSize; + copy.mRecordingState = mRecordingState; + return copy; + } + + /** + * Database row identifier, or 0 if this call hasn't been inserted yet. + */ + public long getId() + { + return mId; + } + + public void setId(long id) + { + mId = id; + } + + /** + * In-memory key that identifies a live call across snapshots, or 0 for records loaded from the database. + */ + public long getCallKey() + { + return mCallKey; + } + + public void setCallKey(long callKey) + { + mCallKey = callKey; + } + + /** + * Call start time, milliseconds since epoch + */ + public long getStartTime() + { + return mStartTime; + } + + public void setStartTime(long startTime) + { + mStartTime = startTime; + } + + /** + * Call end time, milliseconds since epoch, or 0 while the call is active. + */ + public long getEndTime() + { + return mEndTime; + } + + public void setEndTime(long endTime) + { + mEndTime = endTime; + } + + /** + * Call duration in milliseconds. For an active call this is the elapsed time up to now. + */ + public long getDuration() + { + if(mEndTime > 0) + { + return Math.max(0, mEndTime - mStartTime); + } + + return isActive() ? Math.max(0, System.currentTimeMillis() - mStartTime) : 0; + } + + public CallState getCallState() + { + return mCallState; + } + + public void setCallState(CallState callState) + { + mCallState = callState; + } + + /** + * Indicates if the call is in progress. + */ + public boolean isActive() + { + return mCallState == CallState.ACTIVE; + } + + /** + * Protocol enumeration entry name (e.g. APCO25) + */ + public String getProtocol() + { + return mProtocol; + } + + public void setProtocol(String protocol) + { + mProtocol = protocol; + } + + /** + * Protocol, or UNKNOWN if the stored value isn't recognized. + */ + public Protocol getProtocolValue() + { + if(mProtocol != null) + { + try + { + return Protocol.valueOf(mProtocol); + } + catch(IllegalArgumentException iae) + { + //Fall through + } + } + + return Protocol.UNKNOWN; + } + + public String getSystem() + { + return mSystem; + } + + public void setSystem(String system) + { + mSystem = system; + } + + public String getSite() + { + return mSite; + } + + public void setSite(String site) + { + mSite = site; + } + + public String getChannel() + { + return mChannel; + } + + public void setChannel(String channel) + { + mChannel = channel; + } + + /** + * Channel frequency in Hertz, or 0 if unknown. + */ + public long getFrequency() + { + return mFrequency; + } + + public void setFrequency(long frequency) + { + mFrequency = frequency; + } + + public int getTimeslot() + { + return mTimeslot; + } + + public void setTimeslot(int timeslot) + { + mTimeslot = timeslot; + } + + /** + * Formatted TO identifier (talkgroup, patch group or radio) + */ + public String getToId() + { + return mToId; + } + + public void setToId(String toId) + { + mToId = toId; + } + + /** + * Numeric TO identifier value, or null. For a patch group, this is the patch group talkgroup. + */ + public Long getToValue() + { + return mToValue; + } + + public void setToValue(Long toValue) + { + mToValue = toValue; + } + + /** + * Indicates if the TO identifier of this call is a talkgroup or patch group that can be used as a hold talkgroup. + */ + public boolean hasTalkgroup() + { + return mToValue != null && mToValue > 0 && isToTalkgroup(); + } + + /** + * Indicates if the TO identifier is a talkgroup or patch group (as opposed to a radio, for a private call). + */ + public boolean isToTalkgroup() + { + return FORM_TALKGROUP.equals(mToForm) || FORM_PATCH_GROUP.equals(mToForm); + } + + /** + * Call type (group, patch, private or telephone), derived from the TO and FROM identifier forms. + */ + public CallType getCallType() + { + return CallType.from(mToForm, mFromForm); + } + + /** + * Identifier form (Form enumeration name, e.g. TALKGROUP, PATCH_GROUP or RADIO) of the TO identifier, or null. + */ + public String getToForm() + { + return mToForm; + } + + public void setToForm(String toForm) + { + mToForm = toForm; + } + + /** + * Fully qualified address (wacn.system.id) of a fully qualified (roaming) TO talkgroup, or null. + */ + public String getToFullyQualified() + { + return mToFullyQualified; + } + + public void setToFullyQualified(String toFullyQualified) + { + mToFullyQualified = toFullyQualified; + } + + /** + * Identifier form (Form enumeration name, e.g. RADIO) of the FROM identifier, or null. + */ + public String getFromForm() + { + return mFromForm; + } + + public void setFromForm(String fromForm) + { + mFromForm = fromForm; + } + + /** + * Indicates if the FROM identifier is a radio + */ + public boolean isFromRadio() + { + return FORM_RADIO.equals(mFromForm); + } + + /** + * Fully qualified address (wacn.system.id) of a fully qualified (roaming) FROM radio, or null. + */ + public String getFromFullyQualified() + { + return mFromFullyQualified; + } + + public void setFromFullyQualified(String fromFullyQualified) + { + mFromFullyQualified = fromFullyQualified; + } + + /** + * Talkgroups that are patched into the TO patch group of the call, or an empty list. Searching for a talkgroup + * finds the calls that it participated in through a patch. + */ + public List getPatchedTalkgroups() + { + return mPatchedTalkgroups; + } + + public void setPatchedTalkgroups(List patchedTalkgroups) + { + mPatchedTalkgroups = patchedTalkgroups == null ? List.of() : List.copyOf(patchedTalkgroups); + } + + /** + * Name of the alias list that was assigned to the channel for this call, or null. Aliases for the TO and FROM + * identifiers are looked up in the current version of this alias list when the call is displayed. + */ + public String getAliasList() + { + return mAliasList; + } + + public void setAliasList(String aliasList) + { + mAliasList = aliasList; + } + + public String getFromId() + { + return mFromId; + } + + public void setFromId(String fromId) + { + mFromId = fromId; + } + + public Long getFromValue() + { + return mFromValue; + } + + public void setFromValue(Long fromValue) + { + mFromValue = fromValue; + } + + public String getTalkerAlias() + { + return mTalkerAlias; + } + + public void setTalkerAlias(String talkerAlias) + { + mTalkerAlias = talkerAlias; + } + + public boolean isEncrypted() + { + return mEncrypted; + } + + public void setEncrypted(boolean encrypted) + { + mEncrypted = encrypted; + } + + public boolean isDuplicate() + { + return mDuplicate; + } + + public void setDuplicate(boolean duplicate) + { + mDuplicate = duplicate; + } + + /** + * Alias playback priority + */ + public int getPriority() + { + return mPriority; + } + + public void setPriority(int priority) + { + mPriority = priority; + } + + /** + * Indicates if an alias of the call has the record option, as opposed to the call being recorded only because + * all calls are recorded. Do Not Monitor calls with an alias record option are kept by the retention rule. + */ + public boolean isAliasRecord() + { + return mAliasRecord; + } + + public void setAliasRecord(boolean aliasRecord) + { + mAliasRecord = aliasRecord; + } + + /** + * Recording format (MP3 or WAVE), or null when the call isn't recorded. + */ + public String getRecordFormat() + { + return mRecordFormat; + } + + public void setRecordFormat(String recordFormat) + { + mRecordFormat = recordFormat; + } + + /** + * Absolute path of the audio recording file, or null. + */ + public String getFilePath() + { + return mFilePath; + } + + public void setFilePath(String filePath) + { + mFilePath = filePath; + } + + /** + * Recording file size in bytes + */ + public long getFileSize() + { + return mFileSize; + } + + public void setFileSize(long fileSize) + { + mFileSize = fileSize; + } + + public RecordingState getRecordingState() + { + return mRecordingState; + } + + public void setRecordingState(RecordingState recordingState) + { + mRecordingState = recordingState; + } + + /** + * Indicates if this call has a completed audio recording that can be played. + */ + public boolean isPlayable() + { + return mRecordingState == RecordingState.RECORDED && mFilePath != null; + } + + /** + * Key used to detect a call that already exists in a target database during an append transfer. The recording + * file path identifies a call uniquely. Calls without a recording use their start time, channel and TO. + */ + public String getTransferKey() + { + if(mFilePath != null && !mFilePath.isEmpty()) + { + return "F:" + mFilePath; + } + + return "C:" + mStartTime + "|" + mChannel + "|" + mTimeslot + "|" + mToId + "|" + mFromId; + } + + @Override + public String toString() + { + return "Call " + mId + " " + mProtocol + " TO:" + mToId + " FROM:" + mFromId + " " + mCallState + " " + + mRecordingState + (mFilePath != null ? " " + mFilePath : ""); + } +} diff --git a/src/main/java/io/github/dsheirer/database/call/CallRepository.java b/src/main/java/io/github/dsheirer/database/call/CallRepository.java new file mode 100644 index 00000000..d3c9c94a --- /dev/null +++ b/src/main/java/io/github/dsheirer/database/call/CallRepository.java @@ -0,0 +1,909 @@ +/* + * ***************************************************************************** + * Copyright (C) 2014-2026 Dennis Sheirer + * + * 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 + * the Free Software Foundation, either version 3 of the License, or + * (at your option) any later version. + * + * This program is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU General Public License for more details. + * + * You should have received a copy of the GNU General Public License + * along with this program. If not, see + * **************************************************************************** + */ +package io.github.dsheirer.database.call; + +import io.github.dsheirer.alias.id.priority.Priority; +import io.github.dsheirer.database.dialect.IDatabaseDialect; + +import java.sql.Connection; +import java.sql.PreparedStatement; +import java.sql.ResultSet; +import java.sql.SQLException; +import java.sql.Statement; +import java.sql.Types; +import java.util.ArrayList; +import java.util.Collections; +import java.util.HashMap; +import java.util.HashSet; +import java.util.List; +import java.util.Locale; +import java.util.Map; +import java.util.Set; + +/** + * JDBC implementation of the calls table, using SQL that is portable across all supported database engines. All + * methods take a connection from the caller and qualify the table name with the schema, so this class has no state + * other than the schema name. + */ +public class CallRepository implements ICallRepository +{ + public static final String TABLE = "calls"; + public static final String PATCHED_TABLE = "patched_talkgroups"; + + /** + * Columns in insert/select order, excluding the generated id. + */ + private static final String COLUMNS = "start_time, end_time, duration_ms, call_state, protocol, system_name, " + + "site_name, channel_name, frequency, timeslot, to_id, to_value, to_form, to_fq, from_id, from_value, " + + "from_form, from_fq, alias_list, talker_alias, encrypted, duplicate_call, priority, alias_record, " + + "record_format, file_path, file_size, recording_state"; + private static final int COLUMN_COUNT = 28; + + /** + * Maximum stored lengths of the text columns, so that a long alias or channel name can't fail an insert. + */ + private static final int LENGTH_SHORT = 32; + private static final int LENGTH_ID = 64; + private static final int LENGTH_NAME = 255; + private static final int LENGTH_PATH = 1024; + + private final String mSchema; + private final String mTable; + private final String mPatchedTable; + + /** + * Constructs an instance + * @param schema that holds the calls table + */ + public CallRepository(String schema) + { + mSchema = schema; + mTable = schema + "." + TABLE; + mPatchedTable = schema + "." + PATCHED_TABLE; + } + + /** + * Statements that create the calls table and its indexes. + */ + public static List getCreateStatements(IDatabaseDialect dialect, String schema) + { + String table = schema + "." + TABLE; + List statements = new ArrayList<>(); + statements.add("CREATE TABLE IF NOT EXISTS " + table + " (" + + "id " + dialect.getIdentityColumn() + ", " + + "start_time BIGINT NOT NULL, " + + "end_time BIGINT NOT NULL, " + + "duration_ms BIGINT NOT NULL, " + + "call_state VARCHAR(" + LENGTH_SHORT + ") NOT NULL, " + + "protocol VARCHAR(" + LENGTH_SHORT + "), " + + "system_name VARCHAR(" + LENGTH_NAME + "), " + + "site_name VARCHAR(" + LENGTH_NAME + "), " + + "channel_name VARCHAR(" + LENGTH_NAME + "), " + + "frequency BIGINT NOT NULL, " + + "timeslot INT NOT NULL, " + + "to_id VARCHAR(" + LENGTH_ID + "), " + + "to_value BIGINT, " + + "to_form VARCHAR(" + LENGTH_SHORT + "), " + + "to_fq VARCHAR(" + LENGTH_ID + "), " + + "from_id VARCHAR(" + LENGTH_ID + "), " + + "from_value BIGINT, " + + "from_form VARCHAR(" + LENGTH_SHORT + "), " + + "from_fq VARCHAR(" + LENGTH_ID + "), " + + "alias_list VARCHAR(" + LENGTH_NAME + "), " + + "talker_alias VARCHAR(" + LENGTH_NAME + "), " + + "encrypted BOOLEAN NOT NULL, " + + "duplicate_call BOOLEAN NOT NULL, " + + "priority INT NOT NULL, " + + "alias_record BOOLEAN NOT NULL, " + + "record_format VARCHAR(" + LENGTH_SHORT + "), " + + "file_path VARCHAR(" + LENGTH_PATH + "), " + + "file_size BIGINT NOT NULL, " + + "recording_state VARCHAR(" + LENGTH_SHORT + ") NOT NULL)"); + + //Index names are scoped to the schema (H2, PostgreSQL) or the table (MariaDB), so prefix them with the table + statements.add("CREATE INDEX IF NOT EXISTS calls_start_time_idx ON " + table + " (start_time)"); + statements.add("CREATE INDEX IF NOT EXISTS calls_to_value_idx ON " + table + " (to_value)"); + statements.add("CREATE INDEX IF NOT EXISTS calls_from_value_idx ON " + table + " (from_value)"); + statements.add("CREATE INDEX IF NOT EXISTS calls_recording_state_idx ON " + table + + " (recording_state, start_time)"); + return statements; + } + + /** + * Statements that create the patched talkgroups table (schema version 2). Rows are deleted with their call. + */ + public static List getPatchedTalkgroupCreateStatements(String schema) + { + String table = schema + "." + PATCHED_TABLE; + List statements = new ArrayList<>(); + statements.add("CREATE TABLE IF NOT EXISTS " + table + " (" + + "call_id BIGINT NOT NULL REFERENCES " + schema + "." + TABLE + "(id) ON DELETE CASCADE, " + + "talkgroup BIGINT NOT NULL, " + + "fq VARCHAR(" + LENGTH_ID + "))"); + statements.add("CREATE INDEX IF NOT EXISTS patched_talkgroups_call_idx ON " + table + " (call_id)"); + statements.add("CREATE INDEX IF NOT EXISTS patched_talkgroups_talkgroup_idx ON " + table + " (talkgroup)"); + return statements; + } + + /** + * Inserts the call and assigns the generated row id to the call. + * @return generated row id + */ + @Override + public long insert(Connection connection, CallRecord call) throws SQLException + { + String sql = "INSERT INTO " + mTable + " (" + COLUMNS + ") VALUES (" + "?, ".repeat(COLUMN_COUNT - 1) + "?)"; + + try(PreparedStatement statement = connection.prepareStatement(sql, new String[]{"id"})) + { + bind(statement, call); + statement.executeUpdate(); + + try(ResultSet keys = statement.getGeneratedKeys()) + { + if(keys.next()) + { + long id = keys.getLong(1); + call.setId(id); + insertPatchedTalkgroups(connection, id, call.getPatchedTalkgroups()); + return id; + } + } + } + + throw new SQLException("Database didn't return a generated id for the inserted call"); + } + + /** + * Inserts the calls in a single batch without retrieving the generated ids. + */ + @Override + public void insertBatch(Connection connection, List calls) throws SQLException + { + String sql = "INSERT INTO " + mTable + " (" + COLUMNS + ") VALUES (" + "?, ".repeat(COLUMN_COUNT - 1) + "?)"; + + List patched = new ArrayList<>(); + + try(PreparedStatement statement = connection.prepareStatement(sql)) + { + for(CallRecord call: calls) + { + //Calls with patched talkgroups need their generated id for the patched talkgroup rows + if(!call.getPatchedTalkgroups().isEmpty()) + { + patched.add(call); + continue; + } + + bind(statement, call); + statement.addBatch(); + } + + statement.executeBatch(); + } + + for(CallRecord call: patched) + { + insert(connection, call); + } + } + + /** + * Inserts the patched talkgroup rows of a call + */ + private void insertPatchedTalkgroups(Connection connection, long callId, List talkgroups) + throws SQLException + { + if(talkgroups.isEmpty()) + { + return; + } + + try(PreparedStatement statement = connection.prepareStatement("INSERT INTO " + mPatchedTable + + " (call_id, talkgroup, fq) VALUES (?, ?, ?)")) + { + for(PatchedTalkgroup talkgroup: talkgroups) + { + statement.setLong(1, callId); + statement.setLong(2, talkgroup.value()); + setString(statement, 3, talkgroup.fullyQualified(), LENGTH_ID); + statement.addBatch(); + } + + statement.executeBatch(); + } + } + + /** + * Replaces the patched talkgroup rows of a call + */ + private void replacePatchedTalkgroups(Connection connection, long callId, List talkgroups) + throws SQLException + { + try(PreparedStatement statement = connection.prepareStatement("DELETE FROM " + mPatchedTable + + " WHERE call_id = ?")) + { + statement.setLong(1, callId); + statement.executeUpdate(); + } + + insertPatchedTalkgroups(connection, callId, talkgroups); + } + + /** + * Loads the patched talkgroups of the patch group calls in the list. + */ + private void loadPatchedTalkgroups(Connection connection, List calls) throws SQLException + { + Map patchCalls = new HashMap<>(); + + for(CallRecord call: calls) + { + if(CallRecord.FORM_PATCH_GROUP.equals(call.getToForm())) + { + patchCalls.put(call.getId(), call); + } + } + + if(patchCalls.isEmpty()) + { + return; + } + + Map> talkgroups = new HashMap<>(); + String placeholders = String.join(", ", Collections.nCopies(patchCalls.size(), "?")); + + try(PreparedStatement statement = connection.prepareStatement("SELECT call_id, talkgroup, fq FROM " + + mPatchedTable + " WHERE call_id IN (" + placeholders + ") ORDER BY call_id, talkgroup")) + { + int index = 1; + + for(Long id: patchCalls.keySet()) + { + statement.setLong(index++, id); + } + + try(ResultSet rs = statement.executeQuery()) + { + while(rs.next()) + { + talkgroups.computeIfAbsent(rs.getLong(1), id -> new ArrayList<>()) + .add(new PatchedTalkgroup(rs.getLong(2), rs.getString(3))); + } + } + } + + for(Map.Entry> entry: talkgroups.entrySet()) + { + patchCalls.get(entry.getKey()).setPatchedTalkgroups(entry.getValue()); + } + } + + /** + * Updates all columns of the existing call row. + * @return true if the row exists and was updated + */ + @Override + public boolean update(Connection connection, CallRecord call) throws SQLException + { + String assignments = String.join(" = ?, ", COLUMNS.split(", ")) + " = ?"; + String sql = "UPDATE " + mTable + " SET " + assignments + " WHERE id = ?"; + + try(PreparedStatement statement = connection.prepareStatement(sql)) + { + bind(statement, call); + statement.setLong(COLUMN_COUNT + 1, call.getId()); + + if(statement.executeUpdate() == 0) + { + return false; + } + } + + if(CallRecord.FORM_PATCH_GROUP.equals(call.getToForm()) || !call.getPatchedTalkgroups().isEmpty()) + { + replacePatchedTalkgroups(connection, call.getId(), call.getPatchedTalkgroups()); + } + + return true; + } + + /** + * Binds the call column values to statement parameters 1 - COLUMN_COUNT. + */ + private static void bind(PreparedStatement statement, CallRecord call) throws SQLException + { + int index = 1; + statement.setLong(index++, call.getStartTime()); + statement.setLong(index++, call.getEndTime()); + statement.setLong(index++, call.getEndTime() > 0 ? call.getDuration() : 0); + statement.setString(index++, call.getCallState().name()); + setString(statement, index++, call.getProtocol(), LENGTH_SHORT); + setString(statement, index++, call.getSystem(), LENGTH_NAME); + setString(statement, index++, call.getSite(), LENGTH_NAME); + setString(statement, index++, call.getChannel(), LENGTH_NAME); + statement.setLong(index++, call.getFrequency()); + statement.setInt(index++, call.getTimeslot()); + setString(statement, index++, call.getToId(), LENGTH_ID); + setLong(statement, index++, call.getToValue()); + setString(statement, index++, call.getToForm(), LENGTH_SHORT); + setString(statement, index++, call.getToFullyQualified(), LENGTH_ID); + setString(statement, index++, call.getFromId(), LENGTH_ID); + setLong(statement, index++, call.getFromValue()); + setString(statement, index++, call.getFromForm(), LENGTH_SHORT); + setString(statement, index++, call.getFromFullyQualified(), LENGTH_ID); + setString(statement, index++, call.getAliasList(), LENGTH_NAME); + setString(statement, index++, call.getTalkerAlias(), LENGTH_NAME); + statement.setBoolean(index++, call.isEncrypted()); + statement.setBoolean(index++, call.isDuplicate()); + statement.setInt(index++, call.getPriority()); + statement.setBoolean(index++, call.isAliasRecord()); + setString(statement, index++, call.getRecordFormat(), LENGTH_SHORT); + setString(statement, index++, call.getFilePath(), LENGTH_PATH); + statement.setLong(index++, call.getFileSize()); + statement.setString(index, call.getRecordingState().name()); + } + + private static void setString(PreparedStatement statement, int index, String value, int maxLength) + throws SQLException + { + if(value == null) + { + statement.setNull(index, Types.VARCHAR); + } + else + { + statement.setString(index, value.length() > maxLength ? value.substring(0, maxLength) : value); + } + } + + private static void setLong(PreparedStatement statement, int index, Long value) throws SQLException + { + if(value == null) + { + statement.setNull(index, Types.BIGINT); + } + else + { + statement.setLong(index, value); + } + } + + /** + * Creates a call record from the current result set row. + */ + private static CallRecord read(ResultSet rs) throws SQLException + { + CallRecord call = new CallRecord(); + call.setId(rs.getLong("id")); + call.setStartTime(rs.getLong("start_time")); + call.setEndTime(rs.getLong("end_time")); + call.setCallState(CallState.fromValue(rs.getString("call_state"))); + call.setProtocol(rs.getString("protocol")); + call.setSystem(rs.getString("system_name")); + call.setSite(rs.getString("site_name")); + call.setChannel(rs.getString("channel_name")); + call.setFrequency(rs.getLong("frequency")); + call.setTimeslot(rs.getInt("timeslot")); + call.setToId(rs.getString("to_id")); + call.setToValue(getLong(rs, "to_value")); + call.setToForm(rs.getString("to_form")); + call.setToFullyQualified(rs.getString("to_fq")); + call.setFromId(rs.getString("from_id")); + call.setFromValue(getLong(rs, "from_value")); + call.setFromForm(rs.getString("from_form")); + call.setFromFullyQualified(rs.getString("from_fq")); + call.setAliasList(rs.getString("alias_list")); + call.setTalkerAlias(rs.getString("talker_alias")); + call.setEncrypted(rs.getBoolean("encrypted")); + call.setDuplicate(rs.getBoolean("duplicate_call")); + call.setPriority(rs.getInt("priority")); + call.setAliasRecord(rs.getBoolean("alias_record")); + call.setRecordFormat(rs.getString("record_format")); + call.setFilePath(rs.getString("file_path")); + call.setFileSize(rs.getLong("file_size")); + call.setRecordingState(RecordingState.fromValue(rs.getString("recording_state"))); + return call; + } + + private static Long getLong(ResultSet rs, String column) throws SQLException + { + long value = rs.getLong(column); + return rs.wasNull() ? null : value; + } + + /** + * Marks any calls left in the active state, by a previous session that didn't shut down cleanly, as interrupted. + * Recordings that were being written are left as recorded if the file has data. + * @return count of calls updated + */ + @Override + public int markInterrupted(Connection connection) throws SQLException + { + try(Statement statement = connection.createStatement()) + { + int count = statement.executeUpdate("UPDATE " + mTable + " SET call_state = '" + + CallState.INTERRUPTED.name() + "' WHERE call_state = '" + CallState.ACTIVE.name() + "'"); + statement.executeUpdate("UPDATE " + mTable + " SET recording_state = '" + RecordingState.RECORDED.name() + + "' WHERE recording_state = '" + RecordingState.RECORDING.name() + "' AND file_path IS NOT NULL"); + return count; + } + } + + @Override + public int deleteActive(Connection connection) throws SQLException + { + try(PreparedStatement statement = connection.prepareStatement("DELETE FROM " + mTable + + " WHERE call_state = ?")) + { + statement.setString(1, CallState.ACTIVE.name()); + return statement.executeUpdate(); + } + } + + /** + * Searches for calls matching the criteria, newest first. + * @param criteria to filter by + * @param offset of the first row to return + * @param limit maximum rows to return + */ + @Override + public List search(Connection connection, CallSearchCriteria criteria, int offset, int limit) + throws SQLException + { + List parameters = new ArrayList<>(); + String where = getWhereClause(criteria, parameters); + String sql = "SELECT id, " + COLUMNS + " FROM " + mTable + " c" + where + + " ORDER BY start_time DESC, id DESC LIMIT ? OFFSET ?"; + parameters.add(limit); + parameters.add(offset); + + List calls = new ArrayList<>(); + + try(PreparedStatement statement = connection.prepareStatement(sql)) + { + bindParameters(statement, parameters); + + try(ResultSet rs = statement.executeQuery()) + { + while(rs.next()) + { + calls.add(read(rs)); + } + } + } + + loadPatchedTalkgroups(connection, calls); + return calls; + } + + /** + * Count of calls matching the criteria + */ + @Override + public long count(Connection connection, CallSearchCriteria criteria) throws SQLException + { + List parameters = new ArrayList<>(); + String sql = "SELECT COUNT(*) FROM " + mTable + " c" + getWhereClause(criteria, parameters); + + try(PreparedStatement statement = connection.prepareStatement(sql)) + { + bindParameters(statement, parameters); + + try(ResultSet rs = statement.executeQuery()) + { + return rs.next() ? rs.getLong(1) : 0; + } + } + } + + /** + * Builds the WHERE clause (with a leading space) for the criteria and adds the parameter values to the list. + */ + private String getWhereClause(CallSearchCriteria criteria, List parameters) + { + List clauses = new ArrayList<>(); + + if(!criteria.to().isEmpty()) + { + clauses.add(getIdentifierClause("to", criteria.to(), "to_form IN ('" + CallRecord.FORM_TALKGROUP + "', '" + + CallRecord.FORM_PATCH_GROUP + "')", mPatchedTable, parameters)); + } + + if(!criteria.from().isEmpty()) + { + clauses.add(getIdentifierClause("from", criteria.from(), "from_form = '" + CallRecord.FORM_RADIO + "'", + null, parameters)); + } + + if(!criteria.talkerAlias().isEmpty()) + { + clauses.add("LOWER(talker_alias) LIKE ? ESCAPE '!'"); + parameters.add(getLikePattern(criteria.talkerAlias())); + } + + if(criteria.protocol() != null) + { + clauses.add("protocol = ?"); + parameters.add(criteria.protocol().name()); + } + + if(criteria.callType() != null) + { + clauses.add(criteria.callType().getWhereClause()); + } + + if(!criteria.system().isEmpty()) + { + clauses.add("LOWER(system_name) LIKE ? ESCAPE '!'"); + parameters.add(getLikePattern(criteria.system())); + } + + if(criteria.startTime() > 0) + { + clauses.add("start_time >= ?"); + parameters.add(criteria.startTime()); + } + + if(criteria.endTime() > 0) + { + clauses.add("start_time <= ?"); + parameters.add(criteria.endTime()); + } + + return clauses.isEmpty() ? "" : " WHERE " + String.join(" AND ", clauses); + } + + /** + * WHERE clause for a TO or FROM identifier filter. + * @param prefix of the identifier columns (to or from) + * @param filter to apply + * @param formClause that restricts an alias filter to the identifier forms that the alias identifiers apply to + * @param patchedTable to also match the talkgroups patched into a patch group call (TO filter), or null + * @param parameters to add the parameter values to + */ + private static String getIdentifierClause(String prefix, IdentifierFilter filter, String formClause, + String patchedTable, List parameters) + { + String valueColumn = prefix + "_value"; + String fqColumn = prefix + "_fq"; + String patched = patchedTable != null ? "EXISTS (SELECT 1 FROM " + patchedTable + " p WHERE p.call_id = c.id AND " : + null; + + if(filter.isAlias()) + { + List matches = new ArrayList<>(); + List matchParameters = new ArrayList<>(); + + for(IdentifierFilter.ValueRange range: filter.ranges()) + { + matches.add("(" + fqColumn + " IS NULL AND protocol = ? AND " + valueColumn + " BETWEEN ? AND ?)"); + matchParameters.add(range.protocol().name()); + matchParameters.add((long)range.min()); + matchParameters.add((long)range.max()); + + if(patched != null) + { + matches.add("(protocol = ? AND " + patched + "p.fq IS NULL AND p.talkgroup BETWEEN ? AND ?))"); + matchParameters.add(range.protocol().name()); + matchParameters.add((long)range.min()); + matchParameters.add((long)range.max()); + } + } + + for(IdentifierFilter.FullyQualifiedAddress address: filter.fullyQualified()) + { + matches.add("(protocol = ? AND " + fqColumn + " = ?)"); + matchParameters.add(address.protocol().name()); + matchParameters.add(address.address()); + + if(patched != null) + { + matches.add("(protocol = ? AND " + patched + "p.fq = ?))"); + matchParameters.add(address.protocol().name()); + matchParameters.add(address.address()); + } + } + + if(matches.isEmpty()) + { + return "1 = 0"; + } + + parameters.add(filter.aliasList() != null ? filter.aliasList().toLowerCase(Locale.ROOT) : ""); + parameters.addAll(matchParameters); + return "(LOWER(alias_list) = ? AND " + formClause + " AND (" + String.join(" OR ", matches) + "))"; + } + + Long value = CallSearchCriteria.parseValue(filter.text()); + parameters.add(getLikePattern(filter.text())); + + if(value != null) + { + parameters.add(value); + + if(patched != null) + { + parameters.add(value); + return "(LOWER(" + prefix + "_id) LIKE ? ESCAPE '!' OR " + valueColumn + " = ? OR " + patched + + "p.talkgroup = ?))"; + } + + return "(LOWER(" + prefix + "_id) LIKE ? ESCAPE '!' OR " + valueColumn + " = ?)"; + } + + return "LOWER(" + prefix + "_id) LIKE ? ESCAPE '!'"; + } + + /** + * Case-insensitive 'contains' LIKE pattern with the LIKE wildcard characters escaped. + */ + static String getLikePattern(String text) + { + String escaped = text.toLowerCase(Locale.ROOT).replace("!", "!!").replace("%", "!%").replace("_", "!_"); + return "%" + escaped + "%"; + } + + private static void bindParameters(PreparedStatement statement, List parameters) throws SQLException + { + for(int x = 0; x < parameters.size(); x++) + { + Object parameter = parameters.get(x); + + if(parameter instanceof String s) + { + statement.setString(x + 1, s); + } + else if(parameter instanceof Integer i) + { + statement.setInt(x + 1, i); + } + else if(parameter instanceof Long l) + { + statement.setLong(x + 1, l); + } + else + { + throw new IllegalArgumentException("Unsupported parameter type: " + parameter); + } + } + } + + /** + * Reads a page of calls in id order, for transferring calls between databases. + * @param afterId only return calls with an id greater than this value + * @param limit maximum number of calls + */ + @Override + public List readPage(Connection connection, long afterId, int limit) throws SQLException + { + String sql = "SELECT id, " + COLUMNS + " FROM " + mTable + " WHERE id > ? ORDER BY id LIMIT ?"; + List calls = new ArrayList<>(); + + try(PreparedStatement statement = connection.prepareStatement(sql)) + { + statement.setLong(1, afterId); + statement.setInt(2, limit); + + try(ResultSet rs = statement.executeQuery()) + { + while(rs.next()) + { + calls.add(read(rs)); + } + } + } + + loadPatchedTalkgroups(connection, calls); + return calls; + } + + /** + * Total number of call rows + */ + @Override + public long count(Connection connection) throws SQLException + { + return count(connection, CallSearchCriteria.all()); + } + + /** + * Transfer keys (see {@link CallRecord#getTransferKey()}) of all calls in the table, for skipping calls that + * already exist when appending. + */ + @Override + public Set getTransferKeys(Connection connection) throws SQLException + { + Set keys = new HashSet<>(); + long afterId = 0; + List page = readPage(connection, afterId, 5000); + + while(!page.isEmpty()) + { + for(CallRecord call: page) + { + keys.add(call.getTransferKey()); + } + + afterId = page.getLast().getId(); + page = readPage(connection, afterId, 5000); + } + + return keys; + } + + /** + * Calls with a recording, oldest first, for age-off processing. + * @param startedBefore only include calls that started before this time, or 0 for any time + * @param limit maximum number of calls + */ + @Override + public List getRecordedCalls(Connection connection, long startedBefore, int limit) throws SQLException + { + String sql = "SELECT id, " + COLUMNS + " FROM " + mTable + " WHERE recording_state = ?" + + (startedBefore > 0 ? " AND start_time < ?" : "") + " ORDER BY start_time, id LIMIT ?"; + List calls = new ArrayList<>(); + + try(PreparedStatement statement = connection.prepareStatement(sql)) + { + int index = 1; + statement.setString(index++, RecordingState.RECORDED.name()); + + if(startedBefore > 0) + { + statement.setLong(index++, startedBefore); + } + + statement.setInt(index, limit); + + try(ResultSet rs = statement.executeQuery()) + { + while(rs.next()) + { + calls.add(read(rs)); + } + } + } + + return calls; + } + + /** + * Completed calls that started before the time, oldest first, for age-off processing. + */ + @Override + public List getCallsStartedBefore(Connection connection, long startedBefore, int limit) + throws SQLException + { + String sql = "SELECT id, " + COLUMNS + " FROM " + mTable + " WHERE start_time < ? AND call_state <> ? " + + "ORDER BY start_time, id LIMIT ?"; + List calls = new ArrayList<>(); + + try(PreparedStatement statement = connection.prepareStatement(sql)) + { + statement.setLong(1, startedBefore); + statement.setString(2, CallState.ACTIVE.name()); + statement.setInt(3, limit); + + try(ResultSet rs = statement.executeQuery()) + { + while(rs.next()) + { + calls.add(read(rs)); + } + } + } + + return calls; + } + + @Override + public List getDuplicateAndDoNotMonitorCalls(Connection connection, long startedBefore, int limit) + throws SQLException + { + String sql = "SELECT id, " + COLUMNS + " FROM " + mTable + " WHERE start_time < ? AND call_state <> ? AND " + + "(duplicate_call = ? OR (priority <= ? AND alias_record = ?)) ORDER BY start_time, id LIMIT ?"; + List calls = new ArrayList<>(); + + try(PreparedStatement statement = connection.prepareStatement(sql)) + { + statement.setLong(1, startedBefore); + statement.setString(2, CallState.ACTIVE.name()); + statement.setBoolean(3, true); + statement.setInt(4, Priority.DO_NOT_MONITOR); + statement.setBoolean(5, false); + statement.setInt(6, limit); + + try(ResultSet rs = statement.executeQuery()) + { + while(rs.next()) + { + calls.add(read(rs)); + } + } + } + + return calls; + } + + /** + * Total size of all recordings that are still available. + */ + @Override + public long getRecordedFileSize(Connection connection) throws SQLException + { + try(PreparedStatement statement = connection.prepareStatement("SELECT SUM(file_size) FROM " + mTable + + " WHERE recording_state = ?")) + { + statement.setString(1, RecordingState.RECORDED.name()); + + try(ResultSet rs = statement.executeQuery()) + { + return rs.next() ? rs.getLong(1) : 0; + } + } + } + + /** + * Deletes the call rows + */ + @Override + public void delete(Connection connection, List ids) throws SQLException + { + try(PreparedStatement statement = connection.prepareStatement("DELETE FROM " + mTable + " WHERE id = ?")) + { + for(Long id: ids) + { + statement.setLong(1, id); + statement.addBatch(); + } + + statement.executeBatch(); + } + } + + /** + * Clears the recording file of the calls and marks the recordings as deleted, keeping the call rows. + */ + @Override + public void markRecordingsDeleted(Connection connection, List ids) throws SQLException + { + try(PreparedStatement statement = connection.prepareStatement("UPDATE " + mTable + " SET recording_state = ?, " + + "file_path = NULL, file_size = 0 WHERE id = ?")) + { + for(Long id: ids) + { + statement.setString(1, RecordingState.DELETED.name()); + statement.setLong(2, id); + statement.addBatch(); + } + + statement.executeBatch(); + } + } + + /** + * Schema name + */ + @Override + public String getSchema() + { + return mSchema; + } +} diff --git a/src/main/java/io/github/dsheirer/database/call/CallSearchCriteria.java b/src/main/java/io/github/dsheirer/database/call/CallSearchCriteria.java new file mode 100644 index 00000000..6506d8bc --- /dev/null +++ b/src/main/java/io/github/dsheirer/database/call/CallSearchCriteria.java @@ -0,0 +1,151 @@ +/* + * ***************************************************************************** + * Copyright (C) 2014-2026 Dennis Sheirer + * + * 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 + * the Free Software Foundation, either version 3 of the License, or + * (at your option) any later version. + * + * This program is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU General Public License for more details. + * + * You should have received a copy of the GNU General Public License + * along with this program. If not, see + * **************************************************************************** + */ +package io.github.dsheirer.database.call; + +import io.github.dsheirer.protocol.Protocol; + +import java.util.List; +import java.util.Locale; + +/** + * Search filter for the calls table. Empty text fields, empty identifier filters and null values don't filter. + * + * @param to filter for the TO identifier (talkgroup, patch group or radio): typed text or a selected talkgroup alias + * @param from filter for the FROM identifier (radio): typed text or a selected radio alias + * @param talkerAlias text matched against the decoded talker alias + * @param protocol to match or null + * @param callType to match or null + * @param system text matched against the system name + * @param startTime earliest call start time (ms since epoch) or 0 + * @param endTime latest call start time (ms since epoch) or 0 + */ +public record CallSearchCriteria(IdentifierFilter to, IdentifierFilter from, String talkerAlias, Protocol protocol, + CallType callType, String system, long startTime, long endTime) +{ + /** + * Criteria without a call type filter + */ + public CallSearchCriteria(IdentifierFilter to, IdentifierFilter from, String talkerAlias, Protocol protocol, + String system, long startTime, long endTime) + { + this(to, from, talkerAlias, protocol, null, system, startTime, endTime); + } + + public CallSearchCriteria + { + to = to == null ? IdentifierFilter.none() : to; + from = from == null ? IdentifierFilter.none() : from; + talkerAlias = clean(talkerAlias); + system = clean(system); + } + + /** + * Criteria that match every call + */ + public static CallSearchCriteria all() + { + return new CallSearchCriteria(null, null, null, null, null, 0, 0); + } + + private static String clean(String value) + { + return value == null ? "" : value.trim(); + } + + /** + * Indicates if these criteria match every call. + */ + public boolean isEmpty() + { + return to.isEmpty() && from.isEmpty() && talkerAlias.isEmpty() && system.isEmpty() && protocol == null && + callType == null && startTime == 0 && endTime == 0; + } + + /** + * Parses the text as a decimal identifier value, or null if it isn't a number. + */ + static Long parseValue(String text) + { + if(text == null || text.isEmpty() || text.length() > 18) + { + return null; + } + + for(int x = 0; x < text.length(); x++) + { + if(!Character.isDigit(text.charAt(x))) + { + return null; + } + } + + return Long.parseLong(text); + } + + /** + * Evaluates the criteria against a call in memory, using the same rules as the database query. This is used to + * decide whether a live call belongs in the currently displayed search results. + */ + public boolean matches(CallRecord call) + { + if(!to.isEmpty() && !to.matches(call, call.getToId(), call.getToValue(), call.getToFullyQualified(), + call.isToTalkgroup(), call.getPatchedTalkgroups())) + { + return false; + } + + if(!from.isEmpty() && !from.matches(call, call.getFromId(), call.getFromValue(), + call.getFromFullyQualified(), call.isFromRadio(), List.of())) + { + return false; + } + + if(!talkerAlias.isEmpty() && !contains(call.getTalkerAlias(), talkerAlias)) + { + return false; + } + + if(protocol != null && !protocol.name().equals(call.getProtocol())) + { + return false; + } + + if(callType != null && call.getCallType() != callType) + { + return false; + } + + if(!system.isEmpty() && !contains(call.getSystem(), system)) + { + return false; + } + + if(startTime > 0 && call.getStartTime() < startTime) + { + return false; + } + + return endTime <= 0 || call.getStartTime() <= endTime; + } + + private static boolean contains(String value, String text) + { + return value != null && value.toLowerCase(Locale.ROOT).contains(text.toLowerCase(Locale.ROOT)); + } +} diff --git a/src/main/java/io/github/dsheirer/database/call/CallState.java b/src/main/java/io/github/dsheirer/database/call/CallState.java new file mode 100644 index 00000000..6c377d18 --- /dev/null +++ b/src/main/java/io/github/dsheirer/database/call/CallState.java @@ -0,0 +1,71 @@ +/* + * ***************************************************************************** + * Copyright (C) 2014-2026 Dennis Sheirer + * + * 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 + * the Free Software Foundation, either version 3 of the License, or + * (at your option) any later version. + * + * This program is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU General Public License for more details. + * + * You should have received a copy of the GNU General Public License + * along with this program. If not, see + * **************************************************************************** + */ +package io.github.dsheirer.database.call; + +/** + * Call life-cycle state + */ +public enum CallState +{ + /** + * Call is in progress + */ + ACTIVE("Active"), + /** + * Call completed normally + */ + COMPLETE("Complete"), + /** + * Call was in progress when sdrtrunk stopped (e.g. a crash) and never completed + */ + INTERRUPTED("Interrupted"); + + private final String mLabel; + + CallState(String label) + { + mLabel = label; + } + + @Override + public String toString() + { + return mLabel; + } + + /** + * Parses the stored value, defaulting to COMPLETE for unrecognized values. + */ + public static CallState fromValue(String value) + { + if(value != null) + { + try + { + return CallState.valueOf(value); + } + catch(IllegalArgumentException iae) + { + //Fall through + } + } + + return COMPLETE; + } +} diff --git a/src/main/java/io/github/dsheirer/database/call/CallType.java b/src/main/java/io/github/dsheirer/database/call/CallType.java new file mode 100644 index 00000000..3e3e5b0b --- /dev/null +++ b/src/main/java/io/github/dsheirer/database/call/CallType.java @@ -0,0 +1,120 @@ +/* + * ***************************************************************************** + * Copyright (C) 2014-2026 Dennis Sheirer + * + * 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 + * the Free Software Foundation, either version 3 of the License, or + * (at your option) any later version. + * + * This program is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU General Public License for more details. + * + * You should have received a copy of the GNU General Public License + * along with this program. If not, see + * **************************************************************************** + */ +package io.github.dsheirer.database.call; + +/** + * Type of call, derived from the stored TO and FROM identifier forms. + */ +public enum CallType +{ + /** + * Call to a talkgroup + */ + GROUP("Group"), + /** + * Call to a patch group (talkgroups patched together) + */ + PATCH("Patch"), + /** + * Private (individual) call from one radio to another radio + */ + PRIVATE("Private"), + /** + * Telephone interconnect call between a radio and a telephone + */ + TELEPHONE("Telephone"), + /** + * Call without a TO identifier that identifies the call type (e.g. conventional analog) + */ + UNKNOWN(""); + + private static final String FORM_TELEPHONE_NUMBER = "TELEPHONE_NUMBER"; + private final String mLabel; + + CallType(String label) + { + mLabel = label; + } + + /** + * Call type for the TO and FROM identifier forms (Form enumeration names). + */ + public static CallType from(String toForm, String fromForm) + { + if(CallRecord.FORM_PATCH_GROUP.equals(toForm)) + { + return PATCH; + } + + if(CallRecord.FORM_TALKGROUP.equals(toForm)) + { + return GROUP; + } + + if(FORM_TELEPHONE_NUMBER.equals(toForm) || FORM_TELEPHONE_NUMBER.equals(fromForm)) + { + return TELEPHONE; + } + + if(CallRecord.FORM_RADIO.equals(toForm)) + { + return PRIVATE; + } + + return UNKNOWN; + } + + /** + * SQL condition on the to_form and from_form columns that selects the calls of this type, using the same rules as + * {@link #from(String, String)}. The values are constants, so the condition has no parameters. + */ + public String getWhereClause() + { + String patch = "'" + CallRecord.FORM_PATCH_GROUP + "'"; + String group = "'" + CallRecord.FORM_TALKGROUP + "'"; + String radio = "'" + CallRecord.FORM_RADIO + "'"; + String telephone = "'" + FORM_TELEPHONE_NUMBER + "'"; + String fromNotTelephone = "(from_form IS NULL OR from_form <> " + telephone + ")"; + + return switch(this) + { + case PATCH -> "to_form = " + patch; + case GROUP -> "to_form = " + group; + case TELEPHONE -> "(to_form = " + telephone + " OR (from_form = " + telephone + " AND (to_form IS NULL " + + "OR to_form NOT IN (" + patch + ", " + group + "))))"; + case PRIVATE -> "(to_form = " + radio + " AND " + fromNotTelephone + ")"; + case UNKNOWN -> "((to_form IS NULL OR to_form NOT IN (" + patch + ", " + group + ", " + radio + ", " + + telephone + ")) AND " + fromNotTelephone + ")"; + }; + } + + /** + * Label for selecting the type in a search filter (the unknown type is labeled Other) + */ + public String getFilterLabel() + { + return this == UNKNOWN ? "Other" : mLabel; + } + + @Override + public String toString() + { + return mLabel; + } +} diff --git a/src/main/java/io/github/dsheirer/database/call/ICallRepository.java b/src/main/java/io/github/dsheirer/database/call/ICallRepository.java new file mode 100644 index 00000000..0fff8eb6 --- /dev/null +++ b/src/main/java/io/github/dsheirer/database/call/ICallRepository.java @@ -0,0 +1,125 @@ +/* + * ***************************************************************************** + * Copyright (C) 2014-2026 Dennis Sheirer + * + * 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 + * the Free Software Foundation, either version 3 of the License, or + * (at your option) any later version. + * + * This program is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU General Public License for more details. + * + * You should have received a copy of the GNU General Public License + * along with this program. If not, see + * **************************************************************************** + */ +package io.github.dsheirer.database.call; + +import java.sql.Connection; +import java.sql.SQLException; +import java.util.List; +import java.util.Set; + +/** + * Storage operations for call audio events in one database schema. Each method uses the connection from the caller + * so that the caller controls pooling and transactions. + */ +public interface ICallRepository +{ + /** + * Inserts the call and assigns the generated row id to the call. + * @return generated row id + */ + long insert(Connection connection, CallRecord call) throws SQLException; + + /** + * Inserts the calls in a single batch without retrieving the generated ids. + */ + void insertBatch(Connection connection, List calls) throws SQLException; + + /** + * Updates the existing call row identified by the call's id. + * @return true if the row exists and was updated + */ + boolean update(Connection connection, CallRecord call) throws SQLException; + + /** + * Marks calls left in the active state by a previous session as interrupted. + * @return count of calls updated + */ + int markInterrupted(Connection connection) throws SQLException; + + /** + * Deletes the calls that are in progress. Used when switching databases: calls in progress continue in the new + * database, so their rows are removed from the old database. + * @return count of calls deleted + */ + int deleteActive(Connection connection) throws SQLException; + + /** + * Calls matching the criteria, newest first. + */ + List search(Connection connection, CallSearchCriteria criteria, int offset, int limit) + throws SQLException; + + /** + * Count of calls matching the criteria + */ + long count(Connection connection, CallSearchCriteria criteria) throws SQLException; + + /** + * Total number of calls + */ + long count(Connection connection) throws SQLException; + + /** + * Page of calls in id order with an id greater than the argument, for transfers between databases. + */ + List readPage(Connection connection, long afterId, int limit) throws SQLException; + + /** + * Transfer keys of all calls (see {@link CallRecord#getTransferKey()}) + */ + Set getTransferKeys(Connection connection) throws SQLException; + + /** + * Calls with an available recording, oldest first. + * @param startedBefore only calls that started before this time, or 0 for any time + */ + List getRecordedCalls(Connection connection, long startedBefore, int limit) throws SQLException; + + /** + * Calls that are not in progress and started before the time, oldest first. + */ + List getCallsStartedBefore(Connection connection, long startedBefore, int limit) throws SQLException; + + /** + * Completed calls that started before the time and are duplicates, or Do Not Monitor calls without an alias record + * option, oldest first. + */ + List getDuplicateAndDoNotMonitorCalls(Connection connection, long startedBefore, int limit) + throws SQLException; + + /** + * Total size in bytes of all available recordings + */ + long getRecordedFileSize(Connection connection) throws SQLException; + + /** + * Deletes the call rows + */ + void delete(Connection connection, List ids) throws SQLException; + + /** + * Clears the recording file of the calls and marks their recordings deleted, keeping the call rows. + */ + void markRecordingsDeleted(Connection connection, List ids) throws SQLException; + + /** + * Schema that holds the calls table + */ + String getSchema(); +} diff --git a/src/main/java/io/github/dsheirer/database/call/IdentifierFilter.java b/src/main/java/io/github/dsheirer/database/call/IdentifierFilter.java new file mode 100644 index 00000000..a3ef09b3 --- /dev/null +++ b/src/main/java/io/github/dsheirer/database/call/IdentifierFilter.java @@ -0,0 +1,285 @@ +/* + * ***************************************************************************** + * Copyright (C) 2014-2026 Dennis Sheirer + * + * 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 + * the Free Software Foundation, either version 3 of the License, or + * (at your option) any later version. + * + * This program is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU General Public License for more details. + * + * You should have received a copy of the GNU General Public License + * along with this program. If not, see + * **************************************************************************** + */ +package io.github.dsheirer.database.call; + +import io.github.dsheirer.alias.Alias; +import io.github.dsheirer.alias.id.AliasID; +import io.github.dsheirer.alias.id.radio.P25FullyQualifiedRadio; +import io.github.dsheirer.alias.id.radio.Radio; +import io.github.dsheirer.alias.id.radio.RadioRange; +import io.github.dsheirer.alias.id.talkgroup.P25FullyQualifiedTalkgroup; +import io.github.dsheirer.alias.id.talkgroup.Talkgroup; +import io.github.dsheirer.alias.id.talkgroup.TalkgroupRange; +import io.github.dsheirer.protocol.Protocol; + +import java.util.ArrayList; +import java.util.List; +import java.util.Locale; + +/** + * Search filter for the TO or FROM identifier of a call. The filter either matches typed text against the formatted + * identifier (or the exact identifier value when the text is a number), or matches the identifiers of a selected + * alias: calls of the alias's alias list whose identifier value is one of the alias's values or ranges, or whose fully + * qualified (roaming) address is one of the alias's fully qualified identifiers. This uses the same matching as the + * alias list, so a search for an alias finds the calls that the alias list currently aliases with that alias. + * + * @param text typed text, or empty for no text filter (ignored when an alias is selected) + * @param aliasName of the selected alias, or null + * @param aliasList name of the selected alias, or null + * @param ranges of identifier values for the selected alias + * @param fullyQualified addresses for the selected alias + */ +public record IdentifierFilter(String text, String aliasName, String aliasList, List ranges, + List fullyQualified) +{ + /** + * Range of identifier values (inclusive) for a protocol + */ + public record ValueRange(Protocol protocol, int min, int max) + { + public boolean contains(Protocol callProtocol, long value) + { + return protocol == callProtocol && min <= value && value <= max; + } + } + + /** + * Fully qualified identifier address (wacn.system.id) for a protocol + */ + public record FullyQualifiedAddress(Protocol protocol, String address) {} + + public IdentifierFilter + { + text = text == null ? "" : text.trim(); + ranges = ranges == null ? List.of() : List.copyOf(ranges); + fullyQualified = fullyQualified == null ? List.of() : List.copyOf(fullyQualified); + } + + /** + * Filter that matches every call + */ + public static IdentifierFilter none() + { + return new IdentifierFilter("", null, null, null, null); + } + + /** + * Filter for typed text + */ + public static IdentifierFilter text(String text) + { + return new IdentifierFilter(text, null, null, null, null); + } + + /** + * Filter for the talkgroup identifiers (talkgroups, ranges, fully qualified talkgroups) of the alias + */ + public static IdentifierFilter talkgroupAlias(Alias alias) + { + List ranges = new ArrayList<>(); + List fullyQualified = new ArrayList<>(); + + for(AliasID id: alias.getAliasIdentifiers()) + { + if(!id.isValid()) + { + continue; + } + + if(id instanceof P25FullyQualifiedTalkgroup fqt) + { + fullyQualified.add(new FullyQualifiedAddress(fqt.getProtocol(), fqt.getHashKey())); + } + else if(id instanceof Talkgroup talkgroup) + { + ranges.add(new ValueRange(talkgroup.getProtocol(), talkgroup.getValue(), talkgroup.getValue())); + } + else if(id instanceof TalkgroupRange range) + { + ranges.add(new ValueRange(range.getProtocol(), range.getMinTalkgroup(), range.getMaxTalkgroup())); + } + } + + return new IdentifierFilter("", alias.getName(), alias.getAliasListName(), ranges, fullyQualified); + } + + /** + * Filter for the radio identifiers (radios, ranges, fully qualified radios) of the alias + */ + public static IdentifierFilter radioAlias(Alias alias) + { + List ranges = new ArrayList<>(); + List fullyQualified = new ArrayList<>(); + + for(AliasID id: alias.getAliasIdentifiers()) + { + if(!id.isValid()) + { + continue; + } + + if(id instanceof P25FullyQualifiedRadio fqr) + { + fullyQualified.add(new FullyQualifiedAddress(fqr.getProtocol(), fqr.getHashKey())); + } + else if(id instanceof Radio radio) + { + ranges.add(new ValueRange(radio.getProtocol(), radio.getValue(), radio.getValue())); + } + else if(id instanceof RadioRange range) + { + ranges.add(new ValueRange(range.getProtocol(), range.getMinRadio(), range.getMaxRadio())); + } + } + + return new IdentifierFilter("", alias.getName(), alias.getAliasListName(), ranges, fullyQualified); + } + + /** + * Indicates if the alias has talkgroup identifiers that a talkgroup alias filter can search for + */ + public static boolean hasTalkgroups(Alias alias) + { + IdentifierFilter filter = talkgroupAlias(alias); + return !filter.ranges().isEmpty() || !filter.fullyQualified().isEmpty(); + } + + /** + * Indicates if the alias has radio identifiers that a radio alias filter can search for + */ + public static boolean hasRadios(Alias alias) + { + IdentifierFilter filter = radioAlias(alias); + return !filter.ranges().isEmpty() || !filter.fullyQualified().isEmpty(); + } + + /** + * Indicates if this filter is for a selected alias + */ + public boolean isAlias() + { + return aliasName != null; + } + + /** + * Indicates if this filter doesn't filter anything. An alias filter always filters: an alias without matching + * identifiers matches no calls. + */ + public boolean isEmpty() + { + return !isAlias() && text.isEmpty(); + } + + /** + * Evaluates the filter against a call identifier in memory, using the same rules as the database query. + * @param call to evaluate + * @param formatted identifier text + * @param value of the identifier, or null + * @param fullyQualifiedAddress of the identifier, or null + * @param formMatches true if the identifier form is the form that the alias identifiers apply to + * @param patched talkgroups patched into the TO patch group, which also match, or an empty list + */ + boolean matches(CallRecord call, String formatted, Long value, String fullyQualifiedAddress, boolean formMatches, + List patched) + { + if(isAlias()) + { + if(!formMatches || call.getAliasList() == null || aliasList == null || + !call.getAliasList().equalsIgnoreCase(aliasList)) + { + return false; + } + + Protocol protocol = call.getProtocolValue(); + + for(PatchedTalkgroup talkgroup: patched) + { + if(matchesAlias(protocol, talkgroup.value(), talkgroup.fullyQualified())) + { + return true; + } + } + + return matchesAlias(protocol, value, fullyQualifiedAddress); + } + + if(text.isEmpty()) + { + return true; + } + + Long parsed = CallSearchCriteria.parseValue(text); + + if(parsed != null) + { + if(parsed.equals(value)) + { + return true; + } + + for(PatchedTalkgroup talkgroup: patched) + { + if(talkgroup.value() == parsed) + { + return true; + } + } + } + + return formatted != null && formatted.toLowerCase(Locale.ROOT).contains(text.toLowerCase(Locale.ROOT)); + } + + /** + * Indicates if the identifier value or fully qualified address matches the alias identifiers + */ + private boolean matchesAlias(Protocol protocol, Long value, String fullyQualifiedAddress) + { + if(fullyQualifiedAddress != null) + { + for(FullyQualifiedAddress address: fullyQualified) + { + if(address.protocol() == protocol && address.address().equals(fullyQualifiedAddress)) + { + return true; + } + } + + return false; + } + + if(value != null) + { + for(ValueRange range: ranges) + { + if(range.contains(protocol, value)) + { + return true; + } + } + } + + return false; + } + + @Override + public String toString() + { + return isAlias() ? aliasName + " (" + aliasList + ")" : text; + } +} diff --git a/src/main/java/io/github/dsheirer/database/call/PatchedTalkgroup.java b/src/main/java/io/github/dsheirer/database/call/PatchedTalkgroup.java new file mode 100644 index 00000000..7493796c --- /dev/null +++ b/src/main/java/io/github/dsheirer/database/call/PatchedTalkgroup.java @@ -0,0 +1,34 @@ +/* + * ***************************************************************************** + * Copyright (C) 2014-2026 Dennis Sheirer + * + * 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 + * the Free Software Foundation, either version 3 of the License, or + * (at your option) any later version. + * + * This program is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU General Public License for more details. + * + * You should have received a copy of the GNU General Public License + * along with this program. If not, see + * **************************************************************************** + */ +package io.github.dsheirer.database.call; + +/** + * A talkgroup that is patched into the patch group of a call. The protocol is the protocol of the call. + * + * @param value of the talkgroup + * @param fullyQualified address (wacn.system.id) of a fully qualified (roaming) talkgroup, or null + */ +public record PatchedTalkgroup(long value, String fullyQualified) +{ + @Override + public String toString() + { + return fullyQualified != null ? value + "(" + fullyQualified + ")" : String.valueOf(value); + } +} diff --git a/src/main/java/io/github/dsheirer/database/call/RecordingState.java b/src/main/java/io/github/dsheirer/database/call/RecordingState.java new file mode 100644 index 00000000..49e419e8 --- /dev/null +++ b/src/main/java/io/github/dsheirer/database/call/RecordingState.java @@ -0,0 +1,79 @@ +/* + * ***************************************************************************** + * Copyright (C) 2014-2026 Dennis Sheirer + * + * 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 + * the Free Software Foundation, either version 3 of the License, or + * (at your option) any later version. + * + * This program is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU General Public License for more details. + * + * You should have received a copy of the GNU General Public License + * along with this program. If not, see + * **************************************************************************** + */ +package io.github.dsheirer.database.call; + +/** + * State of the audio recording file for a call + */ +public enum RecordingState +{ + /** + * Call is not recorded + */ + NONE("Not Recorded"), + /** + * Recording is being written + */ + RECORDING("Recording"), + /** + * Recording is complete and the file is available + */ + RECORDED("Recorded"), + /** + * Recording file was deleted by an age-off rule or by the user + */ + DELETED("Deleted"), + /** + * Recording failed with an error + */ + FAILED("Failed"); + + private final String mLabel; + + RecordingState(String label) + { + mLabel = label; + } + + @Override + public String toString() + { + return mLabel; + } + + /** + * Parses the stored value, defaulting to NONE for unrecognized values. + */ + public static RecordingState fromValue(String value) + { + if(value != null) + { + try + { + return RecordingState.valueOf(value); + } + catch(IllegalArgumentException iae) + { + //Fall through + } + } + + return NONE; + } +} diff --git a/src/main/java/io/github/dsheirer/database/dialect/H2Dialect.java b/src/main/java/io/github/dsheirer/database/dialect/H2Dialect.java new file mode 100644 index 00000000..0008f265 --- /dev/null +++ b/src/main/java/io/github/dsheirer/database/dialect/H2Dialect.java @@ -0,0 +1,75 @@ +/* + * ***************************************************************************** + * Copyright (C) 2014-2026 Dennis Sheirer + * + * 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 + * the Free Software Foundation, either version 3 of the License, or + * (at your option) any later version. + * + * This program is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU General Public License for more details. + * + * You should have received a copy of the GNU General Public License + * along with this program. If not, see + * **************************************************************************** + */ +package io.github.dsheirer.database.dialect; + +import io.github.dsheirer.database.DatabaseConfiguration; + +import java.nio.file.Path; + +/** + * Built-in H2 embedded database, stored in the application 'db' directory. + */ +public class H2Dialect implements IDatabaseDialect +{ + public static final String DATABASE_FILE_NAME = "sdrtrunk"; + + @Override + public String getDriverClass() + { + return "org.h2.Driver"; + } + + @Override + public int getDefaultPort() + { + return 0; + } + + @Override + public boolean isNetwork() + { + return false; + } + + @Override + public boolean requiresDatabaseName() + { + return false; + } + + @Override + public String getJdbcUrl(DatabaseConfiguration configuration, Path databaseDirectory) + { + //DB_CLOSE_ON_EXIT=FALSE: the application closes the pool during shutdown, after the last call update. + return "jdbc:h2:file:" + databaseDirectory.resolve(DATABASE_FILE_NAME).toAbsolutePath() + + ";DB_CLOSE_ON_EXIT=FALSE"; + } + + @Override + public String getIdentityColumn() + { + return "BIGINT GENERATED BY DEFAULT AS IDENTITY PRIMARY KEY"; + } + + @Override + public String getDropSchemaStatement(String schema) + { + return "DROP SCHEMA IF EXISTS " + schema + " CASCADE"; + } +} diff --git a/src/main/java/io/github/dsheirer/database/dialect/IDatabaseDialect.java b/src/main/java/io/github/dsheirer/database/dialect/IDatabaseDialect.java new file mode 100644 index 00000000..326d1562 --- /dev/null +++ b/src/main/java/io/github/dsheirer/database/dialect/IDatabaseDialect.java @@ -0,0 +1,77 @@ +/* + * ***************************************************************************** + * Copyright (C) 2014-2026 Dennis Sheirer + * + * 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 + * the Free Software Foundation, either version 3 of the License, or + * (at your option) any later version. + * + * This program is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU General Public License for more details. + * + * You should have received a copy of the GNU General Public License + * along with this program. If not, see + * **************************************************************************** + */ +package io.github.dsheirer.database.dialect; + +import io.github.dsheirer.database.DatabaseConfiguration; + +import java.nio.file.Path; + +/** + * Per-engine connection settings and SQL differences for a call database. Everything else that sdrtrunk sends to + * the database is portable SQL shared by all engines. + * + * All supported engines support schemas, so sdrtrunk always qualifies its tables with the configured schema name. + */ +public interface IDatabaseDialect +{ + /** + * JDBC driver class name + */ + String getDriverClass(); + + /** + * Default TCP port for a network database server, or 0 for an embedded database. + */ + int getDefaultPort(); + + /** + * Indicates if this is a network database server that needs a host, port and credentials. + */ + boolean isNetwork(); + + /** + * Indicates if the engine needs a database name in addition to the schema name. + */ + boolean requiresDatabaseName(); + + /** + * JDBC URL for the configuration + * @param configuration with the host, port and database name + * @param databaseDirectory for embedded database files + */ + String getJdbcUrl(DatabaseConfiguration configuration, Path databaseDirectory); + + /** + * Column definition for an auto-generated 64-bit primary key. + */ + String getIdentityColumn(); + + /** + * Statement that creates the schema when it doesn't exist. + */ + default String getCreateSchemaStatement(String schema) + { + return "CREATE SCHEMA IF NOT EXISTS " + schema; + } + + /** + * Statement that removes the schema and all of its tables. + */ + String getDropSchemaStatement(String schema); +} diff --git a/src/main/java/io/github/dsheirer/database/dialect/MariaDBDialect.java b/src/main/java/io/github/dsheirer/database/dialect/MariaDBDialect.java new file mode 100644 index 00000000..2cb17159 --- /dev/null +++ b/src/main/java/io/github/dsheirer/database/dialect/MariaDBDialect.java @@ -0,0 +1,72 @@ +/* + * ***************************************************************************** + * Copyright (C) 2014-2026 Dennis Sheirer + * + * 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 + * the Free Software Foundation, either version 3 of the License, or + * (at your option) any later version. + * + * This program is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU General Public License for more details. + * + * You should have received a copy of the GNU General Public License + * along with this program. If not, see + * **************************************************************************** + */ +package io.github.dsheirer.database.dialect; + +import io.github.dsheirer.database.DatabaseConfiguration; + +import java.nio.file.Path; + +/** + * MariaDB network database server. In MariaDB a schema is a database. + */ +public class MariaDBDialect implements IDatabaseDialect +{ + @Override + public String getDriverClass() + { + return "org.mariadb.jdbc.Driver"; + } + + @Override + public int getDefaultPort() + { + return 3306; + } + + @Override + public boolean isNetwork() + { + return true; + } + + @Override + public boolean requiresDatabaseName() + { + return false; + } + + @Override + public String getJdbcUrl(DatabaseConfiguration configuration, Path databaseDirectory) + { + return "jdbc:mariadb://" + configuration.host() + ":" + configuration.port() + "/"; + } + + @Override + public String getIdentityColumn() + { + return "BIGINT AUTO_INCREMENT PRIMARY KEY"; + } + + @Override + public String getDropSchemaStatement(String schema) + { + //Dropping a MariaDB schema (database) drops all of its tables + return "DROP SCHEMA IF EXISTS " + schema; + } +} diff --git a/src/main/java/io/github/dsheirer/database/dialect/PostgreSQLDialect.java b/src/main/java/io/github/dsheirer/database/dialect/PostgreSQLDialect.java new file mode 100644 index 00000000..25895a26 --- /dev/null +++ b/src/main/java/io/github/dsheirer/database/dialect/PostgreSQLDialect.java @@ -0,0 +1,72 @@ +/* + * ***************************************************************************** + * Copyright (C) 2014-2026 Dennis Sheirer + * + * 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 + * the Free Software Foundation, either version 3 of the License, or + * (at your option) any later version. + * + * This program is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU General Public License for more details. + * + * You should have received a copy of the GNU General Public License + * along with this program. If not, see + * **************************************************************************** + */ +package io.github.dsheirer.database.dialect; + +import io.github.dsheirer.database.DatabaseConfiguration; + +import java.nio.file.Path; + +/** + * PostgreSQL network database server. The schema lives inside the database named in the configuration. + */ +public class PostgreSQLDialect implements IDatabaseDialect +{ + @Override + public String getDriverClass() + { + return "org.postgresql.Driver"; + } + + @Override + public int getDefaultPort() + { + return 5432; + } + + @Override + public boolean isNetwork() + { + return true; + } + + @Override + public boolean requiresDatabaseName() + { + return true; + } + + @Override + public String getJdbcUrl(DatabaseConfiguration configuration, Path databaseDirectory) + { + return "jdbc:postgresql://" + configuration.host() + ":" + configuration.port() + "/" + + configuration.database(); + } + + @Override + public String getIdentityColumn() + { + return "BIGINT GENERATED BY DEFAULT AS IDENTITY PRIMARY KEY"; + } + + @Override + public String getDropSchemaStatement(String schema) + { + return "DROP SCHEMA IF EXISTS " + schema + " CASCADE"; + } +} diff --git a/src/main/java/io/github/dsheirer/gui/MainWindow.java b/src/main/java/io/github/dsheirer/gui/MainWindow.java index c472dfe1..e5c0b33b 100644 --- a/src/main/java/io/github/dsheirer/gui/MainWindow.java +++ b/src/main/java/io/github/dsheirer/gui/MainWindow.java @@ -21,6 +21,9 @@ package io.github.dsheirer.gui; import io.github.dsheirer.alias.AliasModel; import io.github.dsheirer.audio.playback.AudioPlaybackManager; +import io.github.dsheirer.call.CallManager; +import io.github.dsheirer.database.DatabaseManager; +import io.github.dsheirer.gui.calls.CallsView; import io.github.dsheirer.eventbus.MyEventBus; import io.github.dsheirer.gui.audio.AudioView; import io.github.dsheirer.gui.broadcast.BroadcastStatusView; @@ -128,6 +131,7 @@ public class MainWindow private final MainSpectralDisplay mSpectralDisplay; private final HBox mSpectralHiddenBar; private final NowPlayingView mNowPlayingView; + private final CallsView mCallsView; private final TabPane mTabPane = new TabPane(); private BroadcastStatusView mBroadcastStatusView; private StatusBox mStatusBox; @@ -155,11 +159,14 @@ public class MainWindow * @param mapService for the map * @param resourceMonitor for the status bar * @param diagnosticMonitor for the reports + * @param databaseManager for the Calls tab + * @param callManager for live call updates in the Calls tab * @param exitHandler that shuts down the application, invoked on a background thread */ public MainWindow(UserPreferences userPreferences, PlaylistManager playlistManager, TunerManager tunerManager, AudioPlaybackManager audioPlaybackManager, AliasModel aliasModel, IconModel iconModel, MapService mapService, ResourceMonitor resourceMonitor, DiagnosticMonitor diagnosticMonitor, + DatabaseManager databaseManager, CallManager callManager, Runnable exitHandler) { mUserPreferences = userPreferences; @@ -182,6 +189,7 @@ public class MainWindow boolean detailsVisible = mPreferences.getBoolean(PREFERENCE_NOW_PLAYING_DETAILS_VISIBLE, true); mNowPlayingView = new NowPlayingView(playlistManager, iconModel, userPreferences, detailsVisible); + mCallsView = new CallsView(databaseManager, callManager, audioPlaybackManager, aliasModel); createTabs(mapService, iconModel); URL stylesheet = MainWindow.class.getResource("/main_window.css"); @@ -298,6 +306,7 @@ public class MainWindow public void dispose() { mAudioView.dispose(); + mCallsView.dispose(); if(mBroadcastStatusView != null) { @@ -352,6 +361,7 @@ public class MainWindow new RadioReferencePlaylistActions(mPlaylistManager), true); Tab nowPlaying = new Tab("Now Playing", mNowPlayingView); + Tab calls = new Tab("Calls", mCallsView); Tab map = new Tab("Map", mapView); Tab tuners = new Tab("Tuners", new io.github.dsheirer.source.tuner.ui.TunerView(mTunerManager, mUserPreferences)); @@ -364,7 +374,7 @@ public class MainWindow playlist.setGraphic(playIcon); playlist.setContent(new Region()); - mTabPane.getTabs().addAll(nowPlaying, map, tuners, playlist); + mTabPane.getTabs().addAll(nowPlaying, calls, map, tuners, playlist); mTabPane.setTabClosingPolicy(TabPane.TabClosingPolicy.UNAVAILABLE); mTabPane.getStyleClass().add("main-tab-pane"); mTabPane.getSelectionModel().selectedItemProperty().addListener((observable, oldTab, newTab) -> { diff --git a/src/main/java/io/github/dsheirer/gui/SDRTrunk.java b/src/main/java/io/github/dsheirer/gui/SDRTrunk.java index d674d65b..edcd840e 100644 --- a/src/main/java/io/github/dsheirer/gui/SDRTrunk.java +++ b/src/main/java/io/github/dsheirer/gui/SDRTrunk.java @@ -38,11 +38,12 @@ import io.github.dsheirer.log.ApplicationLog; import io.github.dsheirer.map.MapService; import io.github.dsheirer.module.log.EventLogManager; import io.github.dsheirer.monitor.DiagnosticMonitor; +import io.github.dsheirer.call.CallManager; +import io.github.dsheirer.database.DatabaseManager; import io.github.dsheirer.monitor.ResourceMonitor; import io.github.dsheirer.playlist.PlaylistManager; import io.github.dsheirer.preference.UserPreferences; import io.github.dsheirer.properties.SystemProperties; -import io.github.dsheirer.record.AudioRecordingManager; import io.github.dsheirer.sample.Listener; import io.github.dsheirer.settings.MapViewSetting; import io.github.dsheirer.settings.SettingsManager; @@ -72,7 +73,8 @@ public class SDRTrunk implements Listener { private final static Logger mLog = LoggerFactory.getLogger(SDRTrunk.class); - private AudioRecordingManager mAudioRecordingManager; + private CallManager mCallManager; + private DatabaseManager mDatabaseManager; private AudioStreamingManager mAudioStreamingManager; private DiagnosticMonitor mDiagnosticMonitor; private IconModel mIconModel = new IconModel(); @@ -158,9 +160,12 @@ public class SDRTrunk implements Listener mTunerManager, headless); mDiagnosticMonitor.start(); + mDatabaseManager = new DatabaseManager(mUserPreferences); + mDatabaseManager.start(); + if(!headless) { - mWindowManager = new WindowManager(mUserPreferences, mTunerManager, mPlaylistManager); + mWindowManager = new WindowManager(mUserPreferences, mTunerManager, mPlaylistManager, mDatabaseManager); } CalibrationManager calibrationManager = CalibrationManager.getInstance(mUserPreferences); @@ -171,8 +176,9 @@ public class SDRTrunk implements Listener AudioPlaybackManager audioPlaybackManager = new AudioPlaybackManager(mUserPreferences); - mAudioRecordingManager = new AudioRecordingManager(mUserPreferences); - mAudioRecordingManager.start(); + mCallManager = new CallManager(mUserPreferences, mDatabaseManager); + mCallManager.start(); + mResourceMonitor.setDatabasePreference(mUserPreferences.getDatabasePreference()); mAudioStreamingManager = new AudioStreamingManager(mPlaylistManager.getBroadcastModel(), BroadcastFormat.MP3, mUserPreferences); @@ -182,7 +188,7 @@ public class SDRTrunk implements Listener mPlaylistManager.getChannelProcessingManager().addAudioSegmentListener(duplicateCallDetector); mPlaylistManager.getChannelProcessingManager().addAudioSegmentListener(audioPlaybackManager); - mPlaylistManager.getChannelProcessingManager().addAudioSegmentListener(mAudioRecordingManager); + mPlaylistManager.getChannelProcessingManager().addAudioSegmentListener(mCallManager); mPlaylistManager.getChannelProcessingManager().addAudioSegmentListener(mAudioStreamingManager); MapService mapService = new MapService(aliasModel, mIconModel); @@ -193,7 +199,7 @@ public class SDRTrunk implements Listener mResourceMonitor.start(); mMainWindow = FxToolkit.callAndWait(() -> new MainWindow(mUserPreferences, mPlaylistManager, mTunerManager, audioPlaybackManager, aliasModel, mIconModel, mapService, mResourceMonitor, - mDiagnosticMonitor, this::processShutdown)); + mDiagnosticMonitor, mDatabaseManager, mCallManager, this::processShutdown)); } TunerSpectralDisplayManager tunerSpectralDisplayManager = new TunerSpectralDisplayManager( @@ -326,7 +332,9 @@ public class SDRTrunk implements Listener mLog.info("Stopping channels ..."); mPlaylistManager.getChannelProcessingManager().shutdown(); - mAudioRecordingManager.stop(); + mCallManager.stop(); + mLog.info("Closing call database ..."); + mDatabaseManager.stop(); mResourceMonitor.stop(); mLog.info("Stopping tuners ..."); diff --git a/src/main/java/io/github/dsheirer/gui/WindowManager.java b/src/main/java/io/github/dsheirer/gui/WindowManager.java index 5591c8af..0e666a65 100644 --- a/src/main/java/io/github/dsheirer/gui/WindowManager.java +++ b/src/main/java/io/github/dsheirer/gui/WindowManager.java @@ -25,6 +25,7 @@ import io.github.dsheirer.gui.icon.IconManager; import io.github.dsheirer.gui.icon.ViewIconManagerRequest; import io.github.dsheirer.gui.playlist.PlaylistEditor; import io.github.dsheirer.gui.playlist.PlaylistEditorRequest; +import io.github.dsheirer.database.DatabaseManager; import io.github.dsheirer.gui.playlist.channelMap.ChannelMapEditor; import io.github.dsheirer.gui.playlist.channelMap.ViewChannelMapEditorRequest; import io.github.dsheirer.gui.preference.UserPreferencesEditor; @@ -70,6 +71,7 @@ public class WindowManager private PlaylistManager mPlaylistManager; private TunerManager mTunerManager; private UserPreferences mUserPreferences; + private final DatabaseManager mDatabaseManager; private UserPreferencesEditor mUserPreferencesEditor; private MessageRecordingViewer mMessageRecordingViewer; @@ -85,10 +87,13 @@ public class WindowManager * @param userPreferences for the windows and their stored locations * @param tunerManager for the playlist editor * @param playlistManager for the playlist and channel map editors + * @param databaseManager for the call database preference editor */ - public WindowManager(UserPreferences userPreferences, TunerManager tunerManager, PlaylistManager playlistManager) + public WindowManager(UserPreferences userPreferences, TunerManager tunerManager, PlaylistManager playlistManager, + DatabaseManager databaseManager) { mUserPreferences = userPreferences; + mDatabaseManager = databaseManager; mTunerManager = tunerManager; mPlaylistManager = playlistManager; @@ -309,7 +314,7 @@ public class WindowManager { if(mUserPreferencesEditor == null) { - mUserPreferencesEditor = new UserPreferencesEditor(mUserPreferences); + mUserPreferencesEditor = new UserPreferencesEditor(mUserPreferences, mDatabaseManager); } return mUserPreferencesEditor; diff --git a/src/main/java/io/github/dsheirer/gui/audio/AudioChannelView.java b/src/main/java/io/github/dsheirer/gui/audio/AudioChannelView.java index 1c5d9ace..9c733f63 100644 --- a/src/main/java/io/github/dsheirer/gui/audio/AudioChannelView.java +++ b/src/main/java/io/github/dsheirer/gui/audio/AudioChannelView.java @@ -25,6 +25,9 @@ import io.github.dsheirer.alias.AliasList; import io.github.dsheirer.alias.AliasModel; import io.github.dsheirer.audio.AudioEvent; import io.github.dsheirer.audio.playback.AudioChannel; +import io.github.dsheirer.audio.playback.AudioPlaybackManager; +import io.github.dsheirer.audio.playback.ReplaySource; +import io.github.dsheirer.audio.playback.TalkgroupHold; import io.github.dsheirer.icon.Icon; import io.github.dsheirer.icon.IconModel; import io.github.dsheirer.identifier.Form; @@ -80,6 +83,10 @@ public class AudioChannelView extends HBox private final Label mStatusLabel = new Label(); private final Label mElapsedLabel = new Label(); private final ToggleButton mMuteButton = new ToggleButton(); + private final ToggleButton mHoldButton = new ToggleButton(); + private final Label mHoldLabel = new Label(); + private final AudioPlaybackManager mAudioPlaybackManager; + private final int mChannelIndex; private final IconNode mMuteIcon = new IconNode(FontAwesome.VOLUME_UP); private final Label mToLabel = new Label(NO_CALL); private final Label mToDetailLabel = new Label(); @@ -103,12 +110,16 @@ public class AudioChannelView extends HBox * @param talkgroupFormatPreference for identifier formatting * @param updatesPerSecond rate that {@link #tick()} is invoked * @param muteChangeListener notified on the FX thread when this channel's mute state changes + * @param audioPlaybackManager for setting the hold talkgroup, or null to hide the hold button + * @param channelIndex of this channel in the audio output */ public AudioChannelView(AudioChannel audioChannel, AliasModel aliasModel, IconModel iconModel, TalkgroupFormatPreference talkgroupFormatPreference, double updatesPerSecond, - Runnable muteChangeListener) + Runnable muteChangeListener, AudioPlaybackManager audioPlaybackManager, int channelIndex) { mAudioChannel = audioChannel; + mAudioPlaybackManager = audioPlaybackManager; + mChannelIndex = channelIndex; mAliasModel = aliasModel; mIconModel = iconModel; mTalkgroupFormatPreference = talkgroupFormatPreference; @@ -143,6 +154,8 @@ public class AudioChannelView extends HBox mChannelTag.setText(audioChannel.getChannelName()); mChannelTag.getStyleClass().add("audio-channel-tag"); + mChannelTag.getStyleClass().add("RIGHT".equals(audioChannel.getChannelName()) ? "audio-channel-tag-right" : + "audio-channel-tag-left"); mStatusLabel.getStyleClass().add("audio-status"); mElapsedLabel.getStyleClass().add("audio-elapsed"); mMuteIcon.setIconSize(15); @@ -154,9 +167,24 @@ public class AudioChannelView extends HBox mAudioChannel.setMuted(mMuteButton.isSelected()); updateMuted(); }); + IconNode holdIcon = new IconNode(FontAwesome.THUMB_TACK); + holdIcon.setIconSize(15); + holdIcon.getStyleClass().add("audio-button-icon"); + mHoldButton.setGraphic(holdIcon); + mHoldButton.getStyleClass().addAll("audio-icon-button", "audio-hold-button"); + mHoldButton.setFocusTraversable(true); + mHoldButton.setOnAction(event -> toggleHold()); + mHoldButton.setVisible(audioPlaybackManager != null); + mHoldButton.setManaged(audioPlaybackManager != null); + mHoldLabel.getStyleClass().add("audio-hold-label"); + mHoldLabel.setTextOverrun(OverrunStyle.ELLIPSIS); + mHoldLabel.setMinWidth(0); + mHoldLabel.setVisible(false); + mHoldLabel.setManaged(false); Region headerSpacer = new Region(); HBox.setHgrow(headerSpacer, Priority.ALWAYS); - HBox header = new HBox(8, mChannelTag, mStatusLabel, headerSpacer, mElapsedLabel, mMuteButton); + HBox header = new HBox(8, mChannelTag, mStatusLabel, mHoldLabel, headerSpacer, mElapsedLabel, mHoldButton, + mMuteButton); header.setAlignment(Pos.CENTER_LEFT); mToLabel.getStyleClass().add("audio-to"); @@ -202,6 +230,79 @@ public class AudioChannelView extends HBox mAudioChannel.addAudioEventListener(mAudioEventListener); mAudioChannel.setIdentifierCollectionListener(mMetadataListener); + mAudioChannel.setHoldListener(hold -> Platform.runLater(this::updateHold)); + updateHold(); + } + + /** + * Holds the talkgroup of the current (or last) call on this channel, or releases the hold. + */ + private void toggleHold() + { + if(mAudioPlaybackManager == null) + { + return; + } + + if(mAudioChannel.getHold() != null) + { + mAudioPlaybackManager.setHold(mChannelIndex, null); + } + else + { + TalkgroupHold hold = getCallHold(); + + if(hold != null) + { + mAudioPlaybackManager.setHold(mChannelIndex, hold); + } + } + + updateHold(); + } + + /** + * Hold for the TO talkgroup of the current or last call, labeled with the talkgroup and its alias, or null. + */ + private TalkgroupHold getCallHold() + { + IdentifierCollection collection = mIdentifierCollection; + TalkgroupHold hold = TalkgroupHold.from(collection, null); + + if(hold == null) + { + return null; + } + + Identifier to = collection.getToIdentifier(); + AliasList aliasList = mAliasModel != null ? mAliasModel.getAliasList(collection) : null; + List aliases = getAliases(aliasList, to); + String label = format(to); + + if(!aliases.isEmpty()) + { + label = label + " " + Joiner.on(", ").skipNulls().join(aliases); + } + + return new TalkgroupHold(hold.protocol(), hold.talkgroup(), label); + } + + /** + * Updates the hold button and label from the channel's hold talkgroup. FX thread. + */ + private void updateHold() + { + TalkgroupHold hold = mAudioChannel.getHold(); + boolean held = hold != null; + mHoldButton.setSelected(held); + mHoldButton.setDisable(!held && getCallHold() == null); + mHoldButton.setTooltip(new Tooltip(held ? "Release hold on talkgroup " + hold : + "Hold this talkgroup: only play calls to this talkgroup on the " + getChannelDescription())); + mHoldButton.setAccessibleText(held ? "Release hold" : "Hold talkgroup"); + mHoldLabel.setText(held ? "HOLD " + hold : ""); + mHoldLabel.setVisible(held); + mHoldLabel.setManaged(held); + pseudoClassStateChanged(AudioView.HELD, held); } /** @@ -211,6 +312,7 @@ public class AudioChannelView extends HBox { mAudioChannel.removeAudioEventListener(mAudioEventListener); mAudioChannel.removeAudioMetadataListener(); + mAudioChannel.setHoldListener(null); } /** @@ -285,6 +387,7 @@ public class AudioChannelView extends HBox switch(audioEvent.getType()) { case AUDIO_STOPPED -> Platform.runLater(() -> setIdentifierCollection(null)); + case REPLAY_STARTED, REPLAY_STOPPED -> Platform.runLater(this::updateReplay); case AUDIO_MUTED, AUDIO_UNMUTED -> Platform.runLater(() -> { updateMuted(); mMuteChangeListener.run(); @@ -336,6 +439,35 @@ public class AudioChannelView extends HBox pseudoClassStateChanged(AudioView.ACTIVE, true); updateDisplay(); updateElapsed(); + updateHold(); + } + + /** + * Shows the recorded call that is replaying on this channel, or restores the call display when the replay ends. + * FX thread. + */ + private void updateReplay() + { + ReplaySource replay = mAudioChannel.getReplay(); + + if(replay != null && !replay.isFinished()) + { + mActive = false; + mHasPlayed = true; + mCallStartNanos = System.nanoTime(); + mToLabel.setText("Replay"); + mToDetailLabel.setText(""); + mFromBox.setVisible(false); + mContextLabel.setText(replay.getDescription()); + setAlias(null); + pseudoClassStateChanged(AudioView.ACTIVE, true); + mStatusLabel.setText(mAudioChannel.isMuted() ? "MUTED" : "REPLAY"); + } + else + { + pseudoClassStateChanged(AudioView.ACTIVE, false); + updateDisplay(); + } } /** @@ -343,6 +475,15 @@ public class AudioChannelView extends HBox */ private void updateStatus() { + ReplaySource replay = mAudioChannel.getReplay(); + + if(replay != null && !replay.isFinished()) + { + mStatusLabel.setText(mAudioChannel.isMuted() ? "MUTED" : "REPLAY"); + mElapsedLabel.setVisible(false); + return; + } + String status; if(mActive) diff --git a/src/main/java/io/github/dsheirer/gui/audio/AudioView.java b/src/main/java/io/github/dsheirer/gui/audio/AudioView.java index 3979245e..84bba81f 100644 --- a/src/main/java/io/github/dsheirer/gui/audio/AudioView.java +++ b/src/main/java/io/github/dsheirer/gui/audio/AudioView.java @@ -73,6 +73,7 @@ public class AudioView extends HBox private static final Logger mLog = LoggerFactory.getLogger(AudioView.class); static final PseudoClass ACTIVE = PseudoClass.getPseudoClass("active"); static final PseudoClass MUTED = PseudoClass.getPseudoClass("muted"); + static final PseudoClass HELD = PseudoClass.getPseudoClass("held"); private static final double UPDATES_PER_SECOND = 25.0; private final AudioPlaybackManager mAudioPlaybackManager; @@ -260,10 +261,12 @@ public class AudioView extends HBox List channels = mAudioPlaybackManager.getAudioChannels(); - for(AudioChannel channel: channels) + for(int x = 0; x < channels.size(); x++) { + AudioChannel channel = channels.get(x); AudioChannelView view = new AudioChannelView(channel, mAliasModel, mIconModel, - mUserPreferences.getTalkgroupFormatPreference(), UPDATES_PER_SECOND, this::updateMuted); + mUserPreferences.getTalkgroupFormatPreference(), UPDATES_PER_SECOND, this::updateMuted, + mAudioPlaybackManager, x); mChannelViews.add(view); mChannelsBox.getChildren().add(view); } diff --git a/src/main/java/io/github/dsheirer/gui/calls/AliasSearchField.java b/src/main/java/io/github/dsheirer/gui/calls/AliasSearchField.java new file mode 100644 index 00000000..c0f81781 --- /dev/null +++ b/src/main/java/io/github/dsheirer/gui/calls/AliasSearchField.java @@ -0,0 +1,240 @@ +/* + * ***************************************************************************** + * Copyright (C) 2014-2026 Dennis Sheirer + * + * 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 + * the Free Software Foundation, either version 3 of the License, or + * (at your option) any later version. + * + * This program is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU General Public License for more details. + * + * You should have received a copy of the GNU General Public License + * along with this program. If not, see + * **************************************************************************** + */ +package io.github.dsheirer.gui.calls; + +import io.github.dsheirer.alias.Alias; +import io.github.dsheirer.alias.AliasModel; +import io.github.dsheirer.database.call.IdentifierFilter; +import javafx.css.PseudoClass; +import javafx.geometry.Side; +import javafx.scene.control.ContextMenu; +import javafx.scene.control.CustomMenuItem; +import javafx.scene.control.Label; +import javafx.scene.control.TextField; +import javafx.scene.control.Tooltip; +import javafx.scene.input.KeyCode; + +import java.util.ArrayList; +import java.util.Comparator; +import java.util.List; +import java.util.Locale; + +/** + * Search field for the TO (talkgroup) or FROM (radio) identifier of a call. As the user types, a list of matching + * aliases from the current alias lists drops down. Selecting an alias filters the calls by the alias's talkgroup or + * radio identifiers. Typing without selecting an alias filters by the identifier text or value. + */ +public class AliasSearchField extends TextField +{ + private static final PseudoClass ALIAS_SELECTED = PseudoClass.getPseudoClass("alias-selected"); + private static final int MAX_SUGGESTIONS = 25; + + /** + * Identifier kind that the field searches + */ + public enum Kind + { + TALKGROUP, + RADIO + } + + private final Kind mKind; + private final AliasModel mAliasModel; + private final ContextMenu mSuggestions = new ContextMenu(); + private final Runnable mSearchAction; + private IdentifierFilter mAliasFilter; + private boolean mSettingText; + + /** + * Constructs an instance + * @param kind of identifier to search + * @param aliasModel for the alias suggestions, or null for text search only + * @param prompt text + * @param searchAction invoked when the user selects an alias or presses enter + */ + public AliasSearchField(Kind kind, AliasModel aliasModel, String prompt, Runnable searchAction) + { + mKind = kind; + mAliasModel = aliasModel; + mSearchAction = searchAction; + setPromptText(prompt); + getStyleClass().add("calls-alias-search"); + mSuggestions.setAutoHide(true); + updateTooltip(); + + textProperty().addListener((observable, oldValue, newValue) -> { + if(!mSettingText) + { + mAliasFilter = null; + pseudoClassStateChanged(ALIAS_SELECTED, false); + updateTooltip(); + showSuggestions(newValue); + } + }); + + focusedProperty().addListener((observable, wasFocused, focused) -> { + if(!focused) + { + mSuggestions.hide(); + } + }); + + setOnKeyPressed(event -> { + if(event.getCode() == KeyCode.ESCAPE) + { + mSuggestions.hide(); + } + else if(event.getCode() == KeyCode.DOWN && !mSuggestions.getItems().isEmpty()) + { + if(!mSuggestions.isShowing()) + { + mSuggestions.show(this, Side.BOTTOM, 0, 0); + } + + mSuggestions.getSkin().getNode().lookupAll(".menu-item").stream().findFirst() + .ifPresent(node -> node.requestFocus()); + event.consume(); + } + }); + + setOnAction(event -> { + mSuggestions.hide(); + mSearchAction.run(); + }); + } + + /** + * Current search filter: the selected alias, or the typed text. + */ + public IdentifierFilter getFilter() + { + return mAliasFilter != null ? mAliasFilter : IdentifierFilter.text(getText()); + } + + /** + * Clears the text and any selected alias + */ + public void reset() + { + mSettingText = true; + setText(""); + mSettingText = false; + mAliasFilter = null; + pseudoClassStateChanged(ALIAS_SELECTED, false); + updateTooltip(); + mSuggestions.hide(); + } + + /** + * Selects the alias as the search filter + */ + public void select(Alias alias) + { + mAliasFilter = mKind == Kind.TALKGROUP ? IdentifierFilter.talkgroupAlias(alias) : + IdentifierFilter.radioAlias(alias); + mSettingText = true; + setText(alias.getName()); + positionCaret(getText().length()); + mSettingText = false; + pseudoClassStateChanged(ALIAS_SELECTED, true); + updateTooltip(); + mSuggestions.hide(); + mSearchAction.run(); + } + + private void updateTooltip() + { + String what = mKind == Kind.TALKGROUP ? "talkgroup" : "radio"; + + if(mAliasFilter != null) + { + setTooltip(new Tooltip("Alias [" + mAliasFilter.aliasName() + "] in alias list [" + + mAliasFilter.aliasList() + "]: calls with the alias " + what + " identifiers")); + } + else + { + setTooltip(new Tooltip("Type a " + what + " alias name and select it from the list, or type part of the " + + what + " identifier or its exact value")); + } + } + + /** + * Aliases (with identifiers of this field's kind) whose name contains the text, sorted by name. + */ + List getMatchingAliases(String text) + { + List matches = new ArrayList<>(); + + if(mAliasModel == null || text == null || text.isBlank()) + { + return matches; + } + + String lower = text.trim().toLowerCase(Locale.ROOT); + + for(Alias alias: new ArrayList<>(mAliasModel.aliasList())) + { + if(alias.getName() != null && alias.getName().toLowerCase(Locale.ROOT).contains(lower) && + (mKind == Kind.TALKGROUP ? IdentifierFilter.hasTalkgroups(alias) : IdentifierFilter.hasRadios(alias))) + { + matches.add(alias); + } + } + + //Names that start with the text first, then alphabetical + matches.sort(Comparator.comparing((Alias alias) -> !alias.getName().toLowerCase(Locale.ROOT) + .startsWith(lower)).thenComparing(alias -> alias.getName().toLowerCase(Locale.ROOT)) + .thenComparing(alias -> alias.getAliasListName() != null ? alias.getAliasListName() : "")); + return matches; + } + + private void showSuggestions(String text) + { + List matches = getMatchingAliases(text); + mSuggestions.getItems().clear(); + + if(matches.isEmpty() || !isFocused()) + { + mSuggestions.hide(); + return; + } + + for(Alias alias: matches.subList(0, Math.min(MAX_SUGGESTIONS, matches.size()))) + { + Label label = new Label(alias.getName() + " (" + (alias.getAliasListName() != null ? + alias.getAliasListName() : "no list") + ")"); + CustomMenuItem item = new CustomMenuItem(label, true); + item.setOnAction(event -> select(alias)); + mSuggestions.getItems().add(item); + } + + if(matches.size() > MAX_SUGGESTIONS) + { + CustomMenuItem more = new CustomMenuItem(new Label((matches.size() - MAX_SUGGESTIONS) + + " more - keep typing to narrow the list"), false); + more.setDisable(true); + mSuggestions.getItems().add(more); + } + + if(!mSuggestions.isShowing()) + { + mSuggestions.show(this, Side.BOTTOM, 0, 0); + } + } +} diff --git a/src/main/java/io/github/dsheirer/gui/calls/CallAliasResolver.java b/src/main/java/io/github/dsheirer/gui/calls/CallAliasResolver.java new file mode 100644 index 00000000..b4fb6eb9 --- /dev/null +++ b/src/main/java/io/github/dsheirer/gui/calls/CallAliasResolver.java @@ -0,0 +1,151 @@ +/* + * ***************************************************************************** + * Copyright (C) 2014-2026 Dennis Sheirer + * + * 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 + * the Free Software Foundation, either version 3 of the License, or + * (at your option) any later version. + * + * This program is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU General Public License for more details. + * + * You should have received a copy of the GNU General Public License + * along with this program. If not, see + * **************************************************************************** + */ +package io.github.dsheirer.gui.calls; + +import io.github.dsheirer.alias.Alias; +import io.github.dsheirer.alias.AliasList; +import io.github.dsheirer.alias.AliasModel; +import io.github.dsheirer.database.call.CallRecord; +import io.github.dsheirer.database.call.PatchedTalkgroup; + +/** + * Looks up the current aliases for the TO and FROM identifiers of a stored call, using the call's alias list from the + * alias model. Calls are aliased with the latest alias values, so historical calls pick up alias changes. + */ +public class CallAliasResolver +{ + private final AliasModel mAliasModel; + + /** + * Constructs an instance + * @param aliasModel with the current aliases, or null to disable alias lookup + */ + public CallAliasResolver(AliasModel aliasModel) + { + mAliasModel = aliasModel; + } + + private AliasList getAliasList(CallRecord call) + { + if(mAliasModel == null || call == null || call.getAliasList() == null || call.getAliasList().isEmpty()) + { + return null; + } + + return mAliasModel.getAliasList(call.getAliasList()); + } + + /** + * Current alias for the TO talkgroup, patch group or radio of the call, or null. + */ + public Alias getToAlias(CallRecord call) + { + AliasList aliasList = getAliasList(call); + + if(aliasList == null || call.getToValue() == null || call.getToValue() > Integer.MAX_VALUE) + { + return null; + } + + int value = call.getToValue().intValue(); + + if(call.isToTalkgroup()) + { + return aliasList.getTalkgroupAlias(call.getProtocolValue(), value, call.getToFullyQualified()); + } + + if(CallRecord.FORM_RADIO.equals(call.getToForm())) + { + return aliasList.getRadioAlias(call.getProtocolValue(), value, call.getToFullyQualified()); + } + + return null; + } + + /** + * Current alias for the FROM radio of the call, or null. + */ + public Alias getFromAlias(CallRecord call) + { + AliasList aliasList = getAliasList(call); + + if(aliasList == null || !call.isFromRadio() || call.getFromValue() == null || + call.getFromValue() > Integer.MAX_VALUE) + { + return null; + } + + return aliasList.getRadioAlias(call.getProtocolValue(), call.getFromValue().intValue(), + call.getFromFullyQualified()); + } + + /** + * Talkgroups patched into the TO patch group, with their current alias names, e.g. "1001 Fire Dispatch, 1002", + * or null when the call isn't a patch group call. + */ + public String getPatchedTalkgroups(CallRecord call) + { + if(call == null || call.getPatchedTalkgroups().isEmpty()) + { + return null; + } + + AliasList aliasList = getAliasList(call); + StringBuilder sb = new StringBuilder(); + + for(PatchedTalkgroup talkgroup: call.getPatchedTalkgroups()) + { + if(sb.length() > 0) + { + sb.append(", "); + } + + sb.append(talkgroup.value()); + + Alias alias = aliasList != null && talkgroup.value() <= Integer.MAX_VALUE ? + aliasList.getTalkgroupAlias(call.getProtocolValue(), (int)talkgroup.value(), + talkgroup.fullyQualified()) : null; + + if(alias != null) + { + sb.append(" ").append(alias.getName()); + } + } + + return sb.toString(); + } + + /** + * Name of the current TO alias, or null + */ + public String getToAliasName(CallRecord call) + { + Alias alias = getToAlias(call); + return alias != null ? alias.getName() : null; + } + + /** + * Name of the current FROM alias, or null + */ + public String getFromAliasName(CallRecord call) + { + Alias alias = getFromAlias(call); + return alias != null ? alias.getName() : null; + } +} diff --git a/src/main/java/io/github/dsheirer/gui/calls/CallPlayer.java b/src/main/java/io/github/dsheirer/gui/calls/CallPlayer.java new file mode 100644 index 00000000..4e4337b4 --- /dev/null +++ b/src/main/java/io/github/dsheirer/gui/calls/CallPlayer.java @@ -0,0 +1,370 @@ +/* + * ***************************************************************************** + * Copyright (C) 2014-2026 Dennis Sheirer + * + * 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 + * the Free Software Foundation, either version 3 of the License, or + * (at your option) any later version. + * + * This program is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU General Public License for more details. + * + * You should have received a copy of the GNU General Public License + * along with this program. If not, see + * **************************************************************************** + */ +package io.github.dsheirer.gui.calls; + +import io.github.dsheirer.audio.playback.AudioChannel; +import io.github.dsheirer.audio.playback.AudioPlaybackManager; +import io.github.dsheirer.audio.playback.ReplaySource; +import io.github.dsheirer.database.call.CallRecord; +import io.github.dsheirer.record.RecordingReader; +import javafx.animation.KeyFrame; +import javafx.animation.Timeline; +import javafx.application.Platform; +import javafx.beans.property.ObjectProperty; +import javafx.beans.property.SimpleObjectProperty; +import javafx.geometry.Pos; +import javafx.scene.control.Button; +import javafx.scene.control.CheckBox; +import javafx.scene.control.Label; +import javafx.scene.control.Slider; +import javafx.scene.control.Tooltip; +import javafx.scene.layout.HBox; +import javafx.scene.layout.Priority; +import javafx.util.Duration; +import jiconfont.icons.font_awesome.FontAwesome; +import jiconfont.javafx.IconNode; +import org.slf4j.Logger; +import org.slf4j.LoggerFactory; + +import java.nio.file.Files; +import java.nio.file.Path; +import java.util.List; +import java.util.concurrent.CompletableFuture; +import java.util.function.Consumer; + +/** + * Replays call recordings in the Calls tab through the sdrtrunk audio channels (the same audio output, channels, + * mute and level meters as live calls). The audio playback manager chooses the channel: an idle channel, otherwise + * the left (or mono) channel, which drops the live call that it is playing. + */ +public class CallPlayer extends HBox +{ + private static final Logger LOGGER = LoggerFactory.getLogger(CallPlayer.class); + private final AudioPlaybackManager mAudioPlaybackManager; + private final Button mPlayButton = new Button(); + private final IconNode mPlayIcon = new IconNode(FontAwesome.PLAY); + private final Button mStopButton = new Button(); + private final Slider mPositionSlider = new Slider(0, 1, 0); + private final Label mTimeLabel = new Label("0:00 / 0:00"); + private final Label mCallLabel = new Label("Double-click a recorded call to replay it"); + private final CheckBox mAutoPlayCheckBox = new CheckBox("Auto-play next"); + private final ObjectProperty mCurrentCall = new SimpleObjectProperty<>(); + private final Timeline mProgressTimeline; + private CallAliasResolver mAliasResolver = new CallAliasResolver(null); + private Consumer mFinishedListener; + private ReplaySource mReplay; + private long mLoadSequence; + + /** + * Constructs an instance + * @param audioPlaybackManager that replays the calls, or null to disable replay + */ + public CallPlayer(AudioPlaybackManager audioPlaybackManager) + { + super(8); + mAudioPlaybackManager = audioPlaybackManager; + setAlignment(Pos.CENTER_LEFT); + getStyleClass().add("calls-player"); + + mPlayIcon.setIconSize(14); + mPlayIcon.fillProperty().bind(mPlayButton.textFillProperty()); + mPlayButton.setGraphic(mPlayIcon); + mPlayButton.setTooltip(new Tooltip("Pause / resume")); + mPlayButton.setOnAction(event -> togglePause()); + + IconNode stopIcon = new IconNode(FontAwesome.STOP); + stopIcon.setIconSize(14); + stopIcon.fillProperty().bind(mStopButton.textFillProperty()); + mStopButton.setGraphic(stopIcon); + mStopButton.setTooltip(new Tooltip("Stop the replay")); + mStopButton.setOnAction(event -> stop()); + + mPositionSlider.setPrefWidth(260); + mPositionSlider.setMinWidth(100); + mPositionSlider.valueChangingProperty().addListener((obs, old, changing) -> { + if(!changing) + { + seek(); + } + }); + mPositionSlider.setOnMouseReleased(event -> seek()); + + mTimeLabel.setMinWidth(Label.USE_PREF_SIZE); + mCallLabel.setMinWidth(0); + mAutoPlayCheckBox.setTooltip(new Tooltip("When a replay finishes, replay the next newer recorded call")); + mAutoPlayCheckBox.setMinWidth(Label.USE_PREF_SIZE); + HBox.setHgrow(mCallLabel, Priority.ALWAYS); + mCallLabel.setMaxWidth(Double.MAX_VALUE); + + getChildren().addAll(mPlayButton, mStopButton, mPositionSlider, mTimeLabel, mCallLabel, mAutoPlayCheckBox); + + mProgressTimeline = new Timeline(new KeyFrame(Duration.millis(100), event -> updateProgress())); + mProgressTimeline.setCycleCount(Timeline.INDEFINITE); + updateControls(); + } + + /** + * Sets the resolver for the current TO and FROM aliases shown for the replayed call + */ + public void setAliasResolver(CallAliasResolver aliasResolver) + { + mAliasResolver = aliasResolver; + } + + /** + * Registers a listener for when a replay plays to the end (used for auto-play of the next call) + */ + public void setFinishedListener(Consumer listener) + { + mFinishedListener = listener; + } + + /** + * Indicates if auto-play of the next call is enabled + */ + public boolean isAutoPlay() + { + return mAutoPlayCheckBox.isSelected(); + } + + /** + * Call that is loaded in the player, or null + */ + public ObjectProperty currentCallProperty() + { + return mCurrentCall; + } + + /** + * Replays the recording of the call. The recording is read on a background thread. FX thread. + * @return true if the replay is starting + */ + public boolean play(CallRecord call) + { + if(call == null || !call.isPlayable()) + { + return false; + } + + if(mAudioPlaybackManager == null || mAudioPlaybackManager.getAudioChannels().isEmpty()) + { + mCallLabel.setText("No audio output is available for replay"); + return false; + } + + Path path = Path.of(call.getFilePath()); + + if(!Files.exists(path)) + { + mCallLabel.setText("Recording file not found: " + path.getFileName()); + return false; + } + + stopReplay(); + long sequence = ++mLoadSequence; + String description = describe(call); + mCurrentCall.set(call); + mCallLabel.setText("Loading " + description); + updateControls(); + + CompletableFuture.supplyAsync(() -> { + try + { + return RecordingReader.read(path); + } + catch(Exception e) + { + throw new IllegalStateException(e.getMessage(), e); + } + }).whenComplete((samples, error) -> Platform.runLater(() -> { + if(sequence != mLoadSequence) + { + return; //Another call was selected while this one was loading + } + + if(error != null) + { + Throwable cause = error.getCause() != null ? error.getCause() : error; + LOGGER.warn("Unable to replay recording [" + path + "] - " + cause.getMessage()); + mCallLabel.setText("Unable to replay " + path.getFileName() + ": " + cause.getMessage()); + mCurrentCall.set(null); + updateControls(); + return; + } + + ReplaySource replay = new ReplaySource(samples, call.getId(), description); + int channelIndex = mAudioPlaybackManager.replay(replay); + + if(channelIndex < 0) + { + mCallLabel.setText("No audio output is available for replay"); + mCurrentCall.set(null); + updateControls(); + return; + } + + mReplay = replay; + mCallLabel.setText(getChannelLabel(channelIndex) + description); + mProgressTimeline.play(); + updateControls(); + })); + + return true; + } + + private String getChannelLabel(int channelIndex) + { + List channels = mAudioPlaybackManager.getAudioChannels(); + + if(channels.size() > 1 && channelIndex < channels.size()) + { + return channels.get(channelIndex).getChannelName() + ": "; + } + + return ""; + } + + private String describe(CallRecord call) + { + StringBuilder sb = new StringBuilder(); + sb.append(CallsView.formatTime(call.getStartTime())); + + if(call.getToId() != null) + { + sb.append(" TO:").append(call.getToId()); + String toAlias = mAliasResolver.getToAliasName(call); + + if(toAlias != null) + { + sb.append(" ").append(toAlias); + } + } + + if(call.getFromId() != null) + { + sb.append(" FROM:").append(call.getFromId()); + String fromAlias = mAliasResolver.getFromAliasName(call); + + if(fromAlias != null) + { + sb.append(" ").append(fromAlias); + } + } + + return sb.toString(); + } + + private void togglePause() + { + if(mReplay != null) + { + mReplay.setPaused(!mReplay.isPaused()); + updateControls(); + } + } + + /** + * Stops the replay. FX thread. + */ + public void stop() + { + mLoadSequence++; + stopReplay(); + mCurrentCall.set(null); + mCallLabel.setText("Double-click a recorded call to replay it"); + mPositionSlider.setValue(0); + mTimeLabel.setText("0:00 / 0:00"); + updateControls(); + } + + private void stopReplay() + { + mProgressTimeline.stop(); + + if(mReplay != null) + { + mReplay.stop(); + mReplay = null; + } + } + + private void seek() + { + if(mReplay != null) + { + mReplay.seek(mPositionSlider.getValue()); + } + } + + /** + * Updates the position display and detects the end of the replay. FX thread. + */ + private void updateProgress() + { + ReplaySource replay = mReplay; + + if(replay == null) + { + mProgressTimeline.stop(); + return; + } + + if(!mPositionSlider.isValueChanging()) + { + mPositionSlider.setValue(replay.getFraction()); + } + + mTimeLabel.setText(format(replay.getPositionMillis()) + " / " + format(replay.getDurationMillis())); + + if(replay.isFinished()) + { + CallRecord finished = mCurrentCall.get(); + boolean completed = replay.isCompleted(); + stop(); + + if(completed && mFinishedListener != null && finished != null) + { + mFinishedListener.accept(finished); + } + } + } + + private static String format(long millis) + { + long seconds = Math.max(0, millis / 1000); + return seconds / 60 + ":" + String.format("%02d", seconds % 60); + } + + private void updateControls() + { + boolean loaded = mReplay != null; + mPlayButton.setDisable(!loaded); + mStopButton.setDisable(!loaded && mCurrentCall.get() == null); + mPositionSlider.setDisable(!loaded); + mPlayIcon.setIconCode(loaded && !mReplay.isPaused() ? FontAwesome.PAUSE : FontAwesome.PLAY); + } + + /** + * Stops the replay and releases resources. + */ + public void dispose() + { + stop(); + } +} diff --git a/src/main/java/io/github/dsheirer/gui/calls/CallsView.java b/src/main/java/io/github/dsheirer/gui/calls/CallsView.java new file mode 100644 index 00000000..79926bdf --- /dev/null +++ b/src/main/java/io/github/dsheirer/gui/calls/CallsView.java @@ -0,0 +1,1136 @@ +/* + * ***************************************************************************** + * Copyright (C) 2014-2026 Dennis Sheirer + * + * 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 + * the Free Software Foundation, either version 3 of the License, or + * (at your option) any later version. + * + * This program is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU General Public License for more details. + * + * You should have received a copy of the GNU General Public License + * along with this program. If not, see + * **************************************************************************** + */ +package io.github.dsheirer.gui.calls; + +import io.github.dsheirer.alias.Alias; +import io.github.dsheirer.alias.AliasModel; +import io.github.dsheirer.audio.playback.AudioChannel; +import io.github.dsheirer.audio.playback.AudioPlaybackManager; +import io.github.dsheirer.audio.playback.ChannelPlayback; +import io.github.dsheirer.audio.playback.TalkgroupHold; +import io.github.dsheirer.call.CallManager; +import io.github.dsheirer.database.DatabaseEvent; +import io.github.dsheirer.database.DatabaseManager; +import io.github.dsheirer.database.call.CallRecord; +import io.github.dsheirer.database.call.CallSearchCriteria; +import io.github.dsheirer.database.call.CallType; +import io.github.dsheirer.database.call.RecordingState; +import io.github.dsheirer.gui.table.ColumnConfigurationButton; +import io.github.dsheirer.gui.table.TableColumnMonitor; +import io.github.dsheirer.protocol.Protocol; +import io.github.dsheirer.sample.Listener; +import javafx.animation.KeyFrame; +import javafx.animation.Timeline; +import javafx.application.Platform; +import javafx.beans.property.ReadOnlyObjectWrapper; +import javafx.collections.FXCollections; +import javafx.collections.ListChangeListener; +import javafx.css.PseudoClass; +import javafx.collections.ObservableList; +import javafx.geometry.Insets; +import javafx.geometry.Pos; +import javafx.scene.control.Button; +import javafx.scene.control.ComboBox; +import javafx.scene.control.ContextMenu; +import javafx.scene.control.DatePicker; +import javafx.scene.control.Label; +import javafx.scene.control.ListCell; +import javafx.scene.control.MenuItem; +import javafx.scene.control.SeparatorMenuItem; +import javafx.scene.control.TableCell; +import javafx.scene.control.TableColumn; +import javafx.scene.control.TableRow; +import javafx.scene.control.TableView; +import javafx.scene.control.TextField; +import javafx.scene.control.Tooltip; +import javafx.scene.input.Clipboard; +import javafx.scene.input.ClipboardContent; +import javafx.scene.input.KeyCode; +import javafx.scene.input.MouseButton; +import javafx.scene.layout.HBox; +import javafx.scene.layout.Priority; +import javafx.scene.layout.Region; +import javafx.scene.layout.VBox; +import javafx.util.Duration; +import jiconfont.icons.font_awesome.FontAwesome; +import jiconfont.javafx.IconNode; + +import java.text.SimpleDateFormat; +import java.time.LocalDate; +import java.time.ZoneId; +import java.util.ArrayList; +import java.util.Date; +import java.util.HashMap; +import java.util.LinkedHashMap; +import java.util.List; +import java.util.Map; +import java.util.function.Function; +import java.util.prefs.Preferences; + +/** + * Calls tab: searchable view of the call database with live updates for calls in progress and a built-in player for + * call recordings. + * + * The table shows one page of search results, newest first. On the first page, new calls that match the search + * criteria are added at the top as they start, and the duration of calls in progress counts up. + */ +public class CallsView extends VBox +{ + private static final String TABLE_KEY = "calls.view.table"; + private static final String PREFERENCE_PAGE_SIZE = "calls.view.page.size"; + static final List PAGE_SIZES = List.of(50, 100, 250, 500); + static final int DEFAULT_PAGE_SIZE = 500; + private final Preferences mPreferences = Preferences.userNodeForPackage(CallsView.class); + private final ComboBox mPageSizeComboBox = new ComboBox<>(); + private int mPageSize = DEFAULT_PAGE_SIZE; + private static final ThreadLocal TIME_FORMAT = + ThreadLocal.withInitial(() -> new SimpleDateFormat("yyyy-MM-dd HH:mm:ss")); + + private final DatabaseManager mDatabaseManager; + private final CallManager mCallManager; + private final AudioPlaybackManager mAudioPlaybackManager; + private final ObservableList mCalls = FXCollections.observableArrayList(); + private final TableView mTableView = new TableView<>(mCalls); + private final TableColumnMonitor mTableColumnMonitor; + private final CallPlayer mCallPlayer; + private final Map mLiveCalls = new LinkedHashMap<>(); + private static final int MAX_CALL_KEYS = 5000; + /** + * Call key (audio segment id) of recent calls by database row id, so that calls reloaded from the database keep + * their link to the live call and to the audio channel that plays them. Oldest entries are dropped. + */ + private final Map mCallKeyById = new LinkedHashMap<>() + { + @Override + protected boolean removeEldestEntry(Map.Entry eldest) + { + return size() > MAX_CALL_KEYS; + } + }; + private final Listener mCallListener = call -> Platform.runLater(() -> receiveLiveCall(call)); + private final Listener mSavedListener = call -> Platform.runLater(() -> receiveSavedCall(call)); + private final Listener mDatabaseListener = event -> Platform.runLater(() -> receive(event)); + private final Listener mPlaybackListener = playback -> Platform.runLater(() -> receive(playback)); + private final Map mPlaybackByChannel = new HashMap<>(); + + private final AliasSearchField mToField; + private final AliasSearchField mFromField; + private final TextField mTalkerAliasField = new TextField(); + private final CallAliasResolver mAliasResolver; + private final ListChangeListener mAliasChangeListener = change -> scheduleAliasRefresh(); + private final AliasModel mAliasModel; + private boolean mAliasRefreshPending; + private final TextField mSystemField = new TextField(); + private final ComboBox mProtocolComboBox = new ComboBox<>(); + private final ComboBox mCallTypeComboBox = new ComboBox<>(); + private final DatePicker mFromDatePicker = new DatePicker(); + private final DatePicker mToDatePicker = new DatePicker(); + private final Label mStatusLabel = new Label(); + private final Button mPreviousButton = new Button("Newer"); + private final Button mNextButton = new Button("Older"); + private final Timeline mDurationTimeline; + private CallSearchCriteria mCriteria = CallSearchCriteria.all(); + private int mOffset = 0; + private long mTotal = 0; + private long mSearchSequence = 0; + + /** + * Constructs an instance + * @param databaseManager for searching the calls + * @param callManager for live call updates + * @param audioPlaybackManager for holding a talkgroup on an audio channel + * @param aliasModel for the current TO and FROM aliases of the calls, and the alias search lists + */ + public CallsView(DatabaseManager databaseManager, CallManager callManager, + AudioPlaybackManager audioPlaybackManager, AliasModel aliasModel) + { + mAliasModel = aliasModel; + mAliasResolver = new CallAliasResolver(aliasModel); + mToField = new AliasSearchField(AliasSearchField.Kind.TALKGROUP, aliasModel, "To / talkgroup", + this::applySearch); + mFromField = new AliasSearchField(AliasSearchField.Kind.RADIO, aliasModel, "From / radio", this::applySearch); + mCallPlayer = new CallPlayer(audioPlaybackManager); + mCallPlayer.setAliasResolver(mAliasResolver); + mDatabaseManager = databaseManager; + mCallManager = callManager; + mAudioPlaybackManager = audioPlaybackManager; + + setSpacing(6); + setPadding(new Insets(6)); + getStyleClass().add("calls-view"); + + createColumns(); + mTableColumnMonitor = new TableColumnMonitor<>(mTableView, TABLE_KEY); + mTableView.setPlaceholder(new Label("No calls")); + mTableView.getSelectionModel().setSelectionMode(javafx.scene.control.SelectionMode.SINGLE); + mTableView.setRowFactory(table -> createRow()); + mTableView.setOnKeyPressed(event -> { + if(event.getCode() == KeyCode.ENTER) + { + play(mTableView.getSelectionModel().getSelectedItem()); + } + }); + VBox.setVgrow(mTableView, Priority.ALWAYS); + + mCallPlayer.setFinishedListener(this::playNext); + + getChildren().addAll(createSearchBar(), mTableView, createPagingBar(), mCallPlayer); + + mDurationTimeline = new Timeline(new KeyFrame(Duration.seconds(1), event -> refreshActiveDurations())); + mDurationTimeline.setCycleCount(Timeline.INDEFINITE); + mDurationTimeline.play(); + + if(mCallManager != null) + { + mCallManager.addCallListener(mCallListener); + } + + if(mDatabaseManager != null) + { + mDatabaseManager.addCallSavedListener(mSavedListener); + mDatabaseManager.addListener(mDatabaseListener); + } + + if(mAliasModel != null) + { + mAliasModel.aliasList().addListener(mAliasChangeListener); + } + + if(mAudioPlaybackManager != null) + { + for(ChannelPlayback playback: mAudioPlaybackManager.getChannelPlayback()) + { + mPlaybackByChannel.put(playback.channelIndex(), playback); + } + + mAudioPlaybackManager.addPlaybackListener(mPlaybackListener); + } + + search(); + } + + /** + * Deregisters listeners and releases the player + */ + public void dispose() + { + mDurationTimeline.stop(); + mCallPlayer.dispose(); + + if(mCallManager != null) + { + mCallManager.removeCallListener(mCallListener); + } + + if(mDatabaseManager != null) + { + mDatabaseManager.removeCallSavedListener(mSavedListener); + mDatabaseManager.removeListener(mDatabaseListener); + } + + if(mAliasModel != null) + { + mAliasModel.aliasList().removeListener(mAliasChangeListener); + } + + if(mAudioPlaybackManager != null) + { + mAudioPlaybackManager.removePlaybackListener(mPlaybackListener); + } + } + + /** + * Audio channel playback change. FX thread. + */ + private void receive(ChannelPlayback playback) + { + ChannelPlayback previous = mPlaybackByChannel.put(playback.channelIndex(), playback); + + //Drop the state of channels that no longer exist after an audio output change (e.g. stereo to mono) + mPlaybackByChannel.keySet().removeIf(index -> index >= playback.channelCount()); + + if(previous == null || previous.audioSegmentId() != playback.audioSegmentId() || + previous.replayCallId() != playback.replayCallId()) + { + mTableView.refresh(); + } + } + + /** + * Audio channel that is playing the call, or null if the call isn't playing. + */ + ChannelPlayback getPlayback(CallRecord call) + { + ChannelPlayback replay = getReplay(call); + + if(replay != null) + { + return replay; + } + + if(call == null || call.getCallKey() == 0) + { + return null; + } + + for(ChannelPlayback playback: mPlaybackByChannel.values()) + { + if(playback.audioSegmentId() == call.getCallKey()) + { + return playback; + } + } + + return null; + } + + /** + * Audio channel that is replaying the recorded call, or null. + */ + ChannelPlayback getReplay(CallRecord call) + { + if(call == null || call.getId() == 0) + { + return null; + } + + for(ChannelPlayback playback: mPlaybackByChannel.values()) + { + if(playback.replayCallId() == call.getId()) + { + return playback; + } + } + + return null; + } + + /** + * Style suffix for an audio channel: left, right or mono + */ + static String getChannelStyle(ChannelPlayback playback) + { + return switch(playback.channelName()) + { + case "LEFT" -> "left"; + case "RIGHT" -> "right"; + default -> "mono"; + }; + } + + /** + * Refreshes the alias columns once after a burst of alias changes (e.g. loading or editing the playlist). + */ + private void scheduleAliasRefresh() + { + if(!mAliasRefreshPending) + { + mAliasRefreshPending = true; + Platform.runLater(() -> { + mAliasRefreshPending = false; + mTableView.refresh(); + }); + } + } + + /** + * Page size from the stored preference, or the default when the stored value isn't one of the choices. + */ + static int getStoredPageSize(int stored) + { + return PAGE_SIZES.contains(stored) ? stored : DEFAULT_PAGE_SIZE; + } + + /** + * Formats a timestamp for display + */ + static String formatTime(long timestamp) + { + return timestamp > 0 ? TIME_FORMAT.get().format(new Date(timestamp)) : ""; + } + + private static String formatDuration(long milliseconds) + { + long seconds = Math.max(0, milliseconds / 1000); + + if(seconds >= 3600) + { + return String.format("%d:%02d:%02d", seconds / 3600, (seconds / 60) % 60, seconds % 60); + } + + return String.format("%d:%02d", seconds / 60, seconds % 60); + } + + private HBox createSearchBar() + { + mToField.setPrefColumnCount(12); + mFromField.setPrefColumnCount(12); + mTalkerAliasField.setPromptText("Talker alias"); + mTalkerAliasField.setPrefColumnCount(9); + mTalkerAliasField.setTooltip(new Tooltip("Matches part of the talker alias decoded from the radio")); + mSystemField.setPromptText("System"); + mSystemField.setPrefColumnCount(8); + + mProtocolComboBox.getItems().add(null); + mProtocolComboBox.getItems().addAll(Protocol.TALKGROUP_PROTOCOLS); + + if(!mProtocolComboBox.getItems().contains(Protocol.APCO25_PHASE2)) + { + mProtocolComboBox.getItems().add(Protocol.APCO25_PHASE2); + } + + mProtocolComboBox.setButtonCell(createProtocolCell()); + mProtocolComboBox.setCellFactory(list -> createProtocolCell()); + mProtocolComboBox.setPromptText("Any Protocol"); + + mCallTypeComboBox.getItems().add(null); + mCallTypeComboBox.getItems().addAll(CallType.GROUP, CallType.PATCH, CallType.PRIVATE, CallType.TELEPHONE, + CallType.UNKNOWN); + mCallTypeComboBox.setButtonCell(createCallTypeCell()); + mCallTypeComboBox.setCellFactory(list -> createCallTypeCell()); + mCallTypeComboBox.setPromptText("Any Type"); + mCallTypeComboBox.setTooltip(new Tooltip("Call type: group, patch, private (radio to radio), telephone " + + "interconnect, or other (no talkgroup or radio, e.g. conventional analog)")); + mCallTypeComboBox.setOnAction(event -> applySearch()); + + mFromDatePicker.setPromptText("From date"); + mFromDatePicker.setPrefWidth(130); + mToDatePicker.setPromptText("To date"); + mToDatePicker.setPrefWidth(130); + + Button searchButton = new Button("Search"); + IconNode searchIcon = new IconNode(FontAwesome.SEARCH); + searchIcon.setIconSize(12); + searchIcon.fillProperty().bind(searchButton.textFillProperty()); + searchButton.setGraphic(searchIcon); + searchButton.setDefaultButton(false); + searchButton.setOnAction(event -> applySearch()); + + Button clearButton = new Button("Clear"); + clearButton.setOnAction(event -> { + mToField.reset(); + mFromField.reset(); + mTalkerAliasField.clear(); + mSystemField.clear(); + mProtocolComboBox.setValue(null); + mCallTypeComboBox.setValue(null); + mFromDatePicker.setValue(null); + mToDatePicker.setValue(null); + applySearch(); + }); + + for(TextField field: new TextField[]{mTalkerAliasField, mSystemField}) + { + field.setOnAction(event -> applySearch()); + } + + Region spacer = new Region(); + HBox.setHgrow(spacer, Priority.ALWAYS); + HBox bar = new HBox(6, mToField, mFromField, mTalkerAliasField, mCallTypeComboBox, mProtocolComboBox, + mSystemField, + mFromDatePicker, mToDatePicker, searchButton, clearButton, spacer, + new ColumnConfigurationButton(mTableColumnMonitor, "Calls Columns")); + bar.setAlignment(Pos.CENTER_LEFT); + return bar; + } + + private static ListCell createCallTypeCell() + { + return new ListCell<>() + { + @Override + protected void updateItem(CallType item, boolean empty) + { + super.updateItem(item, empty); + setText(empty ? null : (item == null ? "Any Type" : item.getFilterLabel())); + } + }; + } + + private static ListCell createProtocolCell() + { + return new ListCell<>() + { + @Override + protected void updateItem(Protocol item, boolean empty) + { + super.updateItem(item, empty); + setText(empty ? null : (item == null ? "Any Protocol" : item.toString())); + } + }; + } + + private HBox createPagingBar() + { + mPreviousButton.setOnAction(event -> { + mOffset = Math.max(0, mOffset - mPageSize); + search(); + }); + mNextButton.setOnAction(event -> { + mOffset += mPageSize; + search(); + }); + Button refreshButton = new Button("Refresh"); + refreshButton.setOnAction(event -> search()); + + mPageSize = getStoredPageSize(mPreferences.getInt(PREFERENCE_PAGE_SIZE, DEFAULT_PAGE_SIZE)); + mPageSizeComboBox.getItems().addAll(PAGE_SIZES); + mPageSizeComboBox.setValue(mPageSize); + mPageSizeComboBox.setTooltip(new Tooltip("Number of calls per page")); + mPageSizeComboBox.valueProperty().addListener((observable, oldValue, newValue) -> { + if(newValue != null && newValue != mPageSize) + { + //Keep the first displayed call on the new page + mOffset = (mOffset / newValue) * newValue; + mPageSize = newValue; + mPreferences.putInt(PREFERENCE_PAGE_SIZE, newValue); + search(); + } + }); + Label pageSizeLabel = new Label("Page size:"); + pageSizeLabel.setLabelFor(mPageSizeComboBox); + + Region spacer = new Region(); + HBox.setHgrow(spacer, Priority.ALWAYS); + HBox bar = new HBox(6, mStatusLabel, spacer, pageSizeLabel, mPageSizeComboBox, refreshButton, + mPreviousButton, mNextButton); + bar.setAlignment(Pos.CENTER_LEFT); + return bar; + } + + private void createColumns() + { + TableColumn start = column("start", "Start", 140, + call -> formatTime(call.getStartTime())); + TableColumn duration = column("duration", "Duration", 70, + call -> formatDuration(call.getDuration())); + TableColumn status = new TableColumn<>(""); + status.setId("status"); + TableColumnMonitor.setName(status, "Status"); + status.setPrefWidth(34); + status.setSortable(false); + status.setCellValueFactory(cdf -> new ReadOnlyObjectWrapper<>(cdf.getValue())); + status.setCellFactory(column -> new StatusCell()); + + TableColumn playing = new TableColumn<>("Playing"); + playing.setId("playing"); + playing.setPrefWidth(70); + playing.setCellValueFactory(cdf -> new ReadOnlyObjectWrapper<>(cdf.getValue())); + playing.setCellFactory(column -> new PlayingCell()); + + mTableView.getColumns().add(start); + mTableView.getColumns().add(duration); + mTableView.getColumns().add(status); + mTableView.getColumns().add(playing); + mTableView.getColumns().add(column("callType", "Type", 70, call -> call.getCallType().toString())); + mTableView.getColumns().add(column("to", "To", 90, CallRecord::getToId)); + mTableView.getColumns().add(column("toAlias", "To Alias", 150, mAliasResolver::getToAliasName)); + mTableView.getColumns().add(column("patched", "Patched", 140, mAliasResolver::getPatchedTalkgroups)); + mTableView.getColumns().add(column("from", "From", 90, CallRecord::getFromId)); + mTableView.getColumns().add(column("fromAlias", "From Alias", 150, mAliasResolver::getFromAliasName)); + mTableView.getColumns().add(column("talkerAlias", "Talker Alias", 120, CallRecord::getTalkerAlias)); + TableColumn recording = column("recording", "Recording", 90, + call -> call.getRecordingState().toString()); + recording.setVisible(false); + mTableView.getColumns().add(recording); + TableColumn file = column("file", "File", 260, call -> call.getFilePath() != null ? + java.nio.file.Path.of(call.getFilePath()).getFileName().toString() : ""); + file.setVisible(false); + mTableView.getColumns().add(file); + + //Protocol, system, site and channel are at the end by default (#2742) + mTableView.getColumns().add(column("protocol", "Protocol", 80, call -> call.getProtocolValue() == + Protocol.UNKNOWN ? "" : call.getProtocolValue().toString())); + mTableView.getColumns().add(column("system", "System", 110, CallRecord::getSystem)); + mTableView.getColumns().add(column("site", "Site", 90, CallRecord::getSite)); + mTableView.getColumns().add(column("channel", "Channel", 110, CallRecord::getChannel)); + TableColumn frequency = column("frequency", "Frequency", 90, + call -> call.getFrequency() > 0 ? String.format("%.5f", call.getFrequency() / 1E6) : ""); + frequency.setVisible(false); + mTableView.getColumns().add(frequency); + TableColumn timeslot = column("timeslot", "TS", 34, + call -> call.getTimeslot() > 0 ? String.valueOf(call.getTimeslot()) : ""); + timeslot.setVisible(false); + mTableView.getColumns().add(timeslot); + + for(TableColumn column: mTableView.getColumns()) + { + column.setSortable(false); + } + } + + private static TableColumn column(String id, String title, double width, + Function value) + { + TableColumn column = new TableColumn<>(title); + column.setId(id); + column.setPrefWidth(width); + column.setCellValueFactory(cdf -> new ReadOnlyObjectWrapper<>(cdf.getValue() != null ? + value.apply(cdf.getValue()) : null)); + return column; + } + + /** + * Status icon cell: in progress, playable recording, encrypted, duplicate + */ + private class StatusCell extends TableCell + { + private final IconNode mIcon = new IconNode(); + + StatusCell() + { + mIcon.setIconSize(12); + mIcon.getStyleClass().add("calls-status-icon"); + setAlignment(Pos.CENTER); + } + + @Override + protected void updateItem(CallRecord call, boolean empty) + { + super.updateItem(call, empty); + + if(empty || call == null) + { + setGraphic(null); + setTooltip(null); + return; + } + + String tip; + + if(call.isActive()) + { + mIcon.setIconCode(FontAwesome.CIRCLE); + mIcon.setFill(javafx.scene.paint.Color.web("#3ddc84")); + tip = "In progress" + (call.getRecordingState() == RecordingState.RECORDING ? " - recording" : ""); + } + else if(mCallPlayer.currentCallProperty().get() != null && isSameCall(call, + mCallPlayer.currentCallProperty().get())) + { + mIcon.setIconCode(FontAwesome.VOLUME_UP); + mIcon.setFill(javafx.scene.paint.Color.web("#4a90d9")); + tip = "Playing"; + } + else if(call.isPlayable()) + { + mIcon.setIconCode(FontAwesome.PLAY_CIRCLE_O); + mIcon.setFill(javafx.scene.paint.Color.GRAY); + tip = "Recorded - double-click to play"; + } + else if(call.isEncrypted()) + { + mIcon.setIconCode(FontAwesome.LOCK); + mIcon.setFill(javafx.scene.paint.Color.GRAY); + tip = "Encrypted"; + } + else + { + mIcon.setIconCode(FontAwesome.MINUS); + mIcon.setFill(javafx.scene.paint.Color.GRAY); + tip = call.getRecordingState().toString(); + } + + if(call.isDuplicate()) + { + tip += " (duplicate)"; + } + + setGraphic(mIcon); + setTooltip(new Tooltip(tip)); + } + } + + /** + * Audio channel chip (LEFT, RIGHT or MONO) for a call that is playing on an audio channel + */ + private class PlayingCell extends TableCell + { + private final Label mChip = new Label(); + private final IconNode mIcon = new IconNode(FontAwesome.VOLUME_UP); + + PlayingCell() + { + mIcon.setIconSize(11); + mIcon.getStyleClass().add("calls-playing-icon"); + mChip.setGraphic(mIcon); + mChip.getStyleClass().add("calls-playing-chip"); + setAlignment(Pos.CENTER_LEFT); + } + + @Override + protected void updateItem(CallRecord call, boolean empty) + { + super.updateItem(call, empty); + ChannelPlayback playback = empty ? null : getPlayback(call); + + if(playback == null) + { + setGraphic(null); + setTooltip(null); + return; + } + + mChip.getStyleClass().removeAll("calls-playing-left", "calls-playing-right", "calls-playing-mono"); + mChip.getStyleClass().add("calls-playing-" + getChannelStyle(playback)); + mChip.setText(playback.channelName()); + mIcon.setIconCode(playback.isReplay() ? FontAwesome.HISTORY : FontAwesome.VOLUME_UP); + setGraphic(mChip); + setTooltip(new Tooltip((playback.isReplay() ? "Replaying" : "Playing") + " on the " + + (playback.channelCount() > 1 ? playback.channelName().toLowerCase() + " audio channel" : + "audio output"))); + } + } + + private static final PseudoClass PLAYING_LEFT = PseudoClass.getPseudoClass("playing-left"); + private static final PseudoClass PLAYING_RIGHT = PseudoClass.getPseudoClass("playing-right"); + private static final PseudoClass PLAYING_MONO = PseudoClass.getPseudoClass("playing-mono"); + + private TableRow createRow() + { + TableRow row = new TableRow<>() + { + @Override + protected void updateItem(CallRecord call, boolean empty) + { + super.updateItem(call, empty); + ChannelPlayback playback = empty ? null : getPlayback(call); + String style = playback != null ? getChannelStyle(playback) : ""; + pseudoClassStateChanged(PLAYING_LEFT, style.equals("left")); + pseudoClassStateChanged(PLAYING_RIGHT, style.equals("right")); + pseudoClassStateChanged(PLAYING_MONO, style.equals("mono")); + } + }; + + row.setOnMouseClicked(event -> { + if(event.getButton() == MouseButton.PRIMARY && event.getClickCount() == 2 && !row.isEmpty()) + { + play(row.getItem()); + } + }); + + row.setOnContextMenuRequested(event -> { + if(!row.isEmpty()) + { + mTableView.getSelectionModel().select(row.getIndex()); + createContextMenu(row.getItem()).show(row, event.getScreenX(), event.getScreenY()); + event.consume(); + } + }); + + return row; + } + + /** + * Context menu for a call: play, hold talkgroup on an audio channel, copy file path. + */ + private ContextMenu createContextMenu(CallRecord call) + { + ContextMenu menu = new ContextMenu(); + + MenuItem play = new MenuItem("Play Recording"); + play.setDisable(!call.isPlayable()); + play.setOnAction(event -> play(call)); + menu.getItems().add(play); + + TalkgroupHold hold = TalkgroupHold.from(call, mAliasResolver.getToAliasName(call)); + + if(mAudioPlaybackManager != null) + { + List channels = mAudioPlaybackManager.getAudioChannels(); + + if(!channels.isEmpty()) + { + menu.getItems().add(new SeparatorMenuItem()); + } + + for(int x = 0; x < channels.size(); x++) + { + final int channelIndex = x; + String channelName = getChannelLabel(channels.get(x), channels.size()); + TalkgroupHold current = mAudioPlaybackManager.getHold(x); + + MenuItem holdItem = new MenuItem(hold != null ? "Hold Talkgroup " + call.getToId() + channelName : + "Hold Talkgroup" + channelName); + holdItem.setDisable(hold == null || hold.isSameTalkgroup(current)); + holdItem.setOnAction(event -> mAudioPlaybackManager.setHold(channelIndex, hold)); + menu.getItems().add(holdItem); + + if(current != null) + { + MenuItem release = new MenuItem("Release Hold " + current + channelName); + release.setOnAction(event -> mAudioPlaybackManager.setHold(channelIndex, null)); + menu.getItems().add(release); + } + } + } + + menu.getItems().add(new SeparatorMenuItem()); + MenuItem copy = new MenuItem("Copy Recording File Path"); + copy.setDisable(call.getFilePath() == null); + copy.setOnAction(event -> { + ClipboardContent content = new ClipboardContent(); + content.putString(call.getFilePath()); + Clipboard.getSystemClipboard().setContent(content); + }); + menu.getItems().add(copy); + + return menu; + } + + /** + * Menu label suffix for the audio channel, empty for a mono output. + */ + static String getChannelLabel(AudioChannel channel, int channelCount) + { + if(channelCount <= 1) + { + return ""; + } + + return switch(channel.getChannelName()) + { + case "LEFT" -> " on Left Channel"; + case "RIGHT" -> " on Right Channel"; + default -> " on " + channel.getChannelName(); + }; + } + + private void play(CallRecord call) + { + if(call != null && call.isPlayable()) + { + mCallPlayer.play(call); + mTableView.refresh(); + } + } + + /** + * Plays the next newer recorded call after the finished call, when auto-play is enabled. + */ + private void playNext(CallRecord finished) + { + mTableView.refresh(); + + if(!mCallPlayer.isAutoPlay()) + { + return; + } + + int index = indexOf(finished); + + //Newest calls are at the top, so the next newer call is above the finished call + for(int x = index - 1; x >= 0; x--) + { + CallRecord candidate = mCalls.get(x); + + if(candidate.isPlayable()) + { + mTableView.getSelectionModel().select(x); + mTableView.scrollTo(Math.max(0, x - 3)); + play(candidate); + return; + } + } + } + + private int indexOf(CallRecord call) + { + for(int x = 0; x < mCalls.size(); x++) + { + if(isSameCall(mCalls.get(x), call)) + { + return x; + } + } + + return -1; + } + + private static boolean isSameCall(CallRecord a, CallRecord b) + { + if(a.getCallKey() != 0 && a.getCallKey() == b.getCallKey()) + { + return true; + } + + return a.getId() != 0 && a.getId() == b.getId(); + } + + private void applySearch() + { + long start = toMillis(mFromDatePicker.getValue(), false); + long end = toMillis(mToDatePicker.getValue(), true); + mCriteria = new CallSearchCriteria(mToField.getFilter(), mFromField.getFilter(), mTalkerAliasField.getText(), + mProtocolComboBox.getValue(), mCallTypeComboBox.getValue(), mSystemField.getText(), start, end); + mOffset = 0; + search(); + } + + private static long toMillis(LocalDate date, boolean endOfDay) + { + if(date == null) + { + return 0; + } + + LocalDate day = endOfDay ? date.plusDays(1) : date; + long millis = day.atStartOfDay(ZoneId.systemDefault()).toInstant().toEpochMilli(); + return endOfDay ? millis - 1 : millis; + } + + /** + * Runs the current search and replaces the table contents with the results. + */ + private void search() + { + if(mDatabaseManager == null) + { + mStatusLabel.setText("Call database is not available"); + return; + } + + long sequence = ++mSearchSequence; + CallSearchCriteria criteria = mCriteria; + int offset = mOffset; + mStatusLabel.setText("Searching ..."); + + mDatabaseManager.search(criteria, offset, mPageSize).whenComplete((result, error) -> Platform.runLater(() -> { + if(sequence != mSearchSequence) + { + return; + } + + if(error != null) + { + Throwable cause = error.getCause() != null ? error.getCause() : error; + mStatusLabel.setText("Search failed: " + cause.getMessage()); + mCalls.clear(); + mTotal = 0; + updatePaging(); + return; + } + + CallRecord selected = mTableView.getSelectionModel().getSelectedItem(); + List calls = new ArrayList<>(); + + if(offset == 0) + { + //Live calls that haven't been written to the database yet + List unsaved = new ArrayList<>(); + + for(CallRecord live: mLiveCalls.values()) + { + if(live.getId() == 0 && criteria.matches(live)) + { + unsaved.add(live); + } + } + + unsaved.sort((a, b) -> Long.compare(b.getStartTime(), a.getStartTime())); + calls.addAll(unsaved); + } + + for(CallRecord stored: result.calls()) + { + //Use the live snapshot for a call in progress, so that it continues to update. A recent completed + //call gets its call key back, so that it shows when it's still playing on an audio channel. + Long callKey = mCallKeyById.get(stored.getId()); + CallRecord live = callKey != null ? mLiveCalls.get(callKey) : null; + + if(live != null) + { + calls.add(live); + } + else if(callKey != null) + { + CallRecord keyed = stored.copy(); + keyed.setCallKey(callKey); + calls.add(keyed); + } + else + { + calls.add(stored); + } + } + + mCalls.setAll(calls); + mTotal = result.total(); + updatePaging(); + + if(selected != null) + { + int index = indexOf(selected); + + if(index >= 0) + { + mTableView.getSelectionModel().select(index); + } + } + })); + } + + private void updatePaging() + { + mPreviousButton.setDisable(mOffset == 0); + mNextButton.setDisable(mOffset + mPageSize >= mTotal); + + if(mTotal == 0) + { + mStatusLabel.setText(mCriteria.isEmpty() ? "No calls" : "No matching calls"); + } + else + { + mStatusLabel.setText("Calls " + (mOffset + 1) + " - " + Math.min(mTotal, mOffset + mPageSize) + " of " + + mTotal + (mCriteria.isEmpty() ? "" : " matching")); + } + } + + /** + * Live call snapshot from the call recorder. FX thread. + */ + private void receiveLiveCall(CallRecord call) + { + //Keep the database id that this view already learned for the call + CallRecord previous = mLiveCalls.get(call.getCallKey()); + + if(previous != null && previous.getId() != 0 && call.getId() == 0) + { + call = call.copy(); + call.setId(previous.getId()); + } + + if(call.isActive()) + { + mLiveCalls.put(call.getCallKey(), call); + } + else + { + mLiveCalls.remove(call.getCallKey()); + } + + int index = indexOf(call); + + if(index >= 0) + { + mCalls.set(index, call); + } + else if(mOffset == 0 && mCriteria.matches(call)) + { + //New call (or a short call that completed before its first update). Insert in start time order. + int insert = 0; + + while(insert < mCalls.size() && mCalls.get(insert).getStartTime() > call.getStartTime()) + { + insert++; + } + + mCalls.add(insert, call); + mTotal++; + + if(mCalls.size() > mPageSize) + { + mCalls.removeLast(); + } + + updatePaging(); + } + } + + /** + * Call written to the database, with its row id. FX thread. + */ + private void receiveSavedCall(CallRecord saved) + { + if(saved.getCallKey() == 0) + { + return; + } + + CallRecord live = mLiveCalls.get(saved.getCallKey()); + mCallKeyById.put(saved.getId(), saved.getCallKey()); + + if(live != null) + { + if(live.getId() != saved.getId()) + { + CallRecord updated = live.copy(); + updated.setId(saved.getId()); + mLiveCalls.put(saved.getCallKey(), updated); + + int index = indexOf(updated); + + if(index >= 0) + { + mCalls.set(index, updated); + } + } + } + else + { + //Completed call: record the id on the row so it matches the stored call after a search + for(int x = 0; x < mCalls.size(); x++) + { + CallRecord row = mCalls.get(x); + + if(row.getCallKey() == saved.getCallKey() && row.getId() != saved.getId()) + { + CallRecord updated = row.copy(); + updated.setId(saved.getId()); + mCalls.set(x, updated); + break; + } + } + } + } + + private void receive(DatabaseEvent event) + { + if(event.type() == DatabaseEvent.Type.CONNECTED || event.type() == DatabaseEvent.Type.CALLS_CHANGED) + { + if(event.type() == DatabaseEvent.Type.CONNECTED) + { + mCallKeyById.clear(); + } + + search(); + } + else if(event.type() == DatabaseEvent.Type.ERROR) + { + mStatusLabel.setText("Call database is not available: " + event.errorMessage()); + } + } + + /** + * Refreshes the duration of the calls in progress once per second. + */ + private void refreshActiveDurations() + { + if(!mLiveCalls.isEmpty() && isVisible() && getScene() != null) + { + for(CallRecord call: mCalls) + { + if(call.isActive()) + { + mTableView.refresh(); + return; + } + } + } + } +} diff --git a/src/main/java/io/github/dsheirer/gui/playlist/PlaylistEditorApplication.java b/src/main/java/io/github/dsheirer/gui/playlist/PlaylistEditorApplication.java index f63adf0a..b53424e4 100644 --- a/src/main/java/io/github/dsheirer/gui/playlist/PlaylistEditorApplication.java +++ b/src/main/java/io/github/dsheirer/gui/playlist/PlaylistEditorApplication.java @@ -52,7 +52,7 @@ public class PlaylistEditorApplication extends Application mPlaylistManager = new PlaylistManager(mUserPreferences, mTunerManager, aliasModel, eventLogManager, new IconModel()); mPlaylistManager.init(); - mWindowManager = new WindowManager(mUserPreferences, mTunerManager, mPlaylistManager); + mWindowManager = new WindowManager(mUserPreferences, mTunerManager, mPlaylistManager, null); } @Override diff --git a/src/main/java/io/github/dsheirer/gui/preference/PreferenceEditorFactory.java b/src/main/java/io/github/dsheirer/gui/preference/PreferenceEditorFactory.java index 7b6efc81..ae8bffca 100644 --- a/src/main/java/io/github/dsheirer/gui/preference/PreferenceEditorFactory.java +++ b/src/main/java/io/github/dsheirer/gui/preference/PreferenceEditorFactory.java @@ -19,6 +19,8 @@ package io.github.dsheirer.gui.preference; +import io.github.dsheirer.database.DatabaseManager; +import io.github.dsheirer.gui.preference.database.DatabasePreferenceEditor; import io.github.dsheirer.gui.preference.application.AppearancePreferenceEditor; import io.github.dsheirer.gui.preference.application.ApplicationPreferenceEditor; import io.github.dsheirer.gui.preference.calibration.VectorCalibrationPreferenceEditor; @@ -38,7 +40,8 @@ import javafx.scene.Node; */ public class PreferenceEditorFactory { - public static Node getEditor(PreferenceEditorType preferenceEditorType, UserPreferences userPreferences) + public static Node getEditor(PreferenceEditorType preferenceEditorType, UserPreferences userPreferences, + DatabaseManager databaseManager) { switch(preferenceEditorType) { @@ -54,6 +57,8 @@ public class PreferenceEditorFactory return new PlaybackPreferenceEditor(userPreferences); case AUDIO_RECORD: return new RecordPreferenceEditor(userPreferences); + case DATABASE: + return new DatabasePreferenceEditor(userPreferences, databaseManager); case CHANNEL_EVENT: return new DecodeEventViewPreferenceEditor(userPreferences); case DIRECTORY: diff --git a/src/main/java/io/github/dsheirer/gui/preference/PreferenceEditorType.java b/src/main/java/io/github/dsheirer/gui/preference/PreferenceEditorType.java index 612cf398..889e3122 100644 --- a/src/main/java/io/github/dsheirer/gui/preference/PreferenceEditorType.java +++ b/src/main/java/io/github/dsheirer/gui/preference/PreferenceEditorType.java @@ -27,6 +27,7 @@ public enum PreferenceEditorType APPLICATION("General"), APPEARANCE("Appearance"), CHANNEL_EVENT("Channel Events"), + DATABASE("Call Database"), DIRECTORY("Directories"), JMBE_LIBRARY("JMBE Audio Library"), RADIO_REFERENCE_ALIAS_LISTS("Alias Lists"), diff --git a/src/main/java/io/github/dsheirer/gui/preference/UserPreferencesEditor.java b/src/main/java/io/github/dsheirer/gui/preference/UserPreferencesEditor.java index db8cc7ae..5c2cc0ab 100644 --- a/src/main/java/io/github/dsheirer/gui/preference/UserPreferencesEditor.java +++ b/src/main/java/io/github/dsheirer/gui/preference/UserPreferencesEditor.java @@ -19,6 +19,7 @@ package io.github.dsheirer.gui.preference; +import io.github.dsheirer.database.DatabaseManager; import io.github.dsheirer.eventbus.MyEventBus; import io.github.dsheirer.gui.playlist.ViewPlaylistRequest; import io.github.dsheirer.preference.UserPreferences; @@ -53,6 +54,7 @@ public class UserPreferencesEditor extends BorderPane { private final static Logger mLog = LoggerFactory.getLogger(UserPreferencesEditor.class); + private DatabaseManager mDatabaseManager; private Map mEditors = new EnumMap<>(PreferenceEditorType.class); private UserPreferences mUserPreferences; private MenuBar mMenuBar; @@ -66,9 +68,10 @@ public class UserPreferencesEditor extends BorderPane * * @param userPreferences to edit */ - public UserPreferencesEditor(UserPreferences userPreferences) + public UserPreferencesEditor(UserPreferences userPreferences, DatabaseManager databaseManager) { mUserPreferences = userPreferences; + mDatabaseManager = databaseManager; setTop(getMenuBar()); @@ -184,6 +187,11 @@ public class UserPreferencesEditor extends BorderPane treeRoot.getChildren().add(cpuItem); cpuItem.setExpanded(true); + TreeItem databaseItem = new TreeItem<>("Database"); + databaseItem.getChildren().add(new TreeItem(PreferenceEditorType.DATABASE)); + treeRoot.getChildren().add(databaseItem); + databaseItem.setExpanded(true); + TreeItem decoderItem = new TreeItem<>("Decoder"); decoderItem.getChildren().add(new TreeItem(PreferenceEditorType.JMBE_LIBRARY)); treeRoot.getChildren().add(decoderItem); @@ -282,7 +290,7 @@ public class UserPreferencesEditor extends BorderPane } else { - editor = PreferenceEditorFactory.getEditor(type, getUserPreferences()); + editor = PreferenceEditorFactory.getEditor(type, getUserPreferences(), mDatabaseManager); mEditors.put(type, editor); } } diff --git a/src/main/java/io/github/dsheirer/gui/preference/call/CallManagementPreferenceEditor.java b/src/main/java/io/github/dsheirer/gui/preference/call/CallManagementPreferenceEditor.java index 95e8af87..aade668d 100644 --- a/src/main/java/io/github/dsheirer/gui/preference/call/CallManagementPreferenceEditor.java +++ b/src/main/java/io/github/dsheirer/gui/preference/call/CallManagementPreferenceEditor.java @@ -47,7 +47,6 @@ public class CallManagementPreferenceEditor extends HBox private ToggleSwitch mDetectDuplicateTalkgroups; private ToggleSwitch mDetectDuplicateRadios; private ToggleSwitch mSuppressDuplicateListening; - private ToggleSwitch mSuppressDuplicateRecording; private ToggleSwitch mSuppressDuplicateStreaming; private ComboBox mPatchGroupStreamingOptionComboBox; @@ -124,14 +123,6 @@ public class CallManagementPreferenceEditor extends HBox GridPane.setConstraints(listeningLabel, 1, row); mEditorPane.getChildren().add(listeningLabel); - GridPane.setConstraints(getSuppressDuplicateRecording(), 0, ++row); - mEditorPane.getChildren().add(getSuppressDuplicateRecording()); - - Label recordingLabel = new Label("Recording"); - recordingLabel.setWrapText(true); - GridPane.setConstraints(recordingLabel, 1, row); - mEditorPane.getChildren().add(recordingLabel); - GridPane.setConstraints(getSuppressDuplicateStreaming(), 0, ++row); mEditorPane.getChildren().add(getSuppressDuplicateStreaming()); @@ -140,6 +131,13 @@ public class CallManagementPreferenceEditor extends HBox GridPane.setConstraints(streamingLabel, 1, row); mEditorPane.getChildren().add(streamingLabel); + Label recordingNote = new Label("Duplicate calls are recorded and listed in the call database. They " + + "are deleted after the duplicate and Do Not Monitor retention time set in the Call Database " + + "preferences."); + recordingNote.setWrapText(true); + GridPane.setConstraints(recordingNote, 0, ++row, 2, 1); + mEditorPane.getChildren().add(recordingNote); + Separator separator2 = new Separator(); GridPane.setHgrow(separator2, Priority.ALWAYS); GridPane.setConstraints(separator2, 0, ++row, 2, 1); @@ -199,22 +197,6 @@ public class CallManagementPreferenceEditor extends HBox return mSuppressDuplicateListening; } - private ToggleSwitch getSuppressDuplicateRecording() - { - if(mSuppressDuplicateRecording == null) - { - mSuppressDuplicateRecording = new ToggleSwitch(); - mSuppressDuplicateRecording.disableProperty() - .bind(Bindings.and(getDetectDuplicateTalkgroups().selectedProperty().not(), - getDetectDuplicateRadios().selectedProperty().not())); - mSuppressDuplicateRecording.setSelected(mPreference.isDuplicateRecordingSuppressionEnabled()); - mSuppressDuplicateRecording.selectedProperty() - .addListener((observable, oldValue, newValue) -> mPreference.setDuplicateRecordingSuppressionEnabled(newValue)); - } - - return mSuppressDuplicateRecording; - } - private ToggleSwitch getSuppressDuplicateStreaming() { if(mSuppressDuplicateStreaming == null) diff --git a/src/main/java/io/github/dsheirer/gui/preference/database/DatabasePreferenceEditor.java b/src/main/java/io/github/dsheirer/gui/preference/database/DatabasePreferenceEditor.java new file mode 100644 index 00000000..4c526594 --- /dev/null +++ b/src/main/java/io/github/dsheirer/gui/preference/database/DatabasePreferenceEditor.java @@ -0,0 +1,777 @@ +/* + * ***************************************************************************** + * Copyright (C) 2014-2026 Dennis Sheirer + * + * 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 + * the Free Software Foundation, either version 3 of the License, or + * (at your option) any later version. + * + * This program is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU General Public License for more details. + * + * You should have received a copy of the GNU General Public License + * along with this program. If not, see + * **************************************************************************** + */ +package io.github.dsheirer.gui.preference.database; + +import io.github.dsheirer.database.DatabaseConfiguration; +import io.github.dsheirer.database.DatabaseEvent; +import io.github.dsheirer.database.DatabaseManager; +import io.github.dsheirer.database.DatabaseType; +import io.github.dsheirer.database.SchemaStatus; +import io.github.dsheirer.database.ageoff.AgeOffAction; +import io.github.dsheirer.database.ageoff.AgeOffManager; +import io.github.dsheirer.database.ageoff.RetentionPeriod; +import io.github.dsheirer.preference.UserPreferences; +import io.github.dsheirer.preference.database.DatabasePreference; +import io.github.dsheirer.sample.Listener; +import javafx.application.Platform; +import javafx.geometry.Insets; +import javafx.scene.control.Alert; +import javafx.scene.control.Button; +import javafx.scene.control.ButtonBar; +import javafx.scene.control.ButtonType; +import javafx.scene.control.CheckBox; +import javafx.scene.control.ComboBox; +import javafx.scene.control.Label; +import javafx.scene.control.PasswordField; +import javafx.scene.control.ProgressIndicator; +import javafx.scene.control.ScrollPane; +import javafx.scene.control.Separator; +import javafx.scene.control.Spinner; +import javafx.scene.control.TextField; +import javafx.scene.control.TextInputDialog; +import javafx.scene.control.Tooltip; +import javafx.scene.layout.ColumnConstraints; +import javafx.scene.layout.GridPane; +import javafx.scene.layout.HBox; +import javafx.scene.layout.Priority; +import javafx.scene.layout.Region; +import javafx.scene.layout.VBox; +import javafx.stage.Modality; +import javafx.stage.Window; + +import java.util.Optional; +import java.util.concurrent.CompletableFuture; +import java.util.concurrent.CompletionException; + +/** + * Preference editor for the call database selection and the call audio age-off rules. + * + * Applying a different database asks whether to transfer the existing calls, and when the target schema already holds + * sdrtrunk calls, whether to append to it, clear it, or create a separately named schema. + */ +public class DatabasePreferenceEditor extends VBox +{ + private static final int MB_PER_GB = 1024; + + private final DatabasePreference mPreference; + private final DatabaseManager mDatabaseManager; + private final Listener mDatabaseListener = event -> Platform.runLater(this::updateStatus); + + private final Label mStatusLabel = new Label(); + private final ComboBox mTypeComboBox = new ComboBox<>(); + private final TextField mHostField = new TextField(); + private final TextField mPortField = new TextField(); + private final TextField mDatabaseField = new TextField(); + private final TextField mSchemaField = new TextField(); + private final TextField mUserField = new TextField(); + private final PasswordField mPasswordField = new PasswordField(); + private final Label mLocationLabel = new Label(); + private final Label mHostLabel = new Label("Host:"); + private final Label mPortLabel = new Label("Port:"); + private final Label mDatabaseLabel = new Label("Database:"); + private final Label mUserLabel = new Label("User:"); + private final Label mPasswordLabel = new Label("Password:"); + private final Label mResultLabel = new Label(); + private final Button mTestButton = new Button("Test Connection"); + private final Button mApplyButton = new Button("Apply"); + private final Button mResetButton = new Button("Reset"); + private final ProgressIndicator mProgress = new ProgressIndicator(); + + private final CheckBox mAgeRuleCheckBox = new CheckBox("Remove calls older than"); + private final Spinner mAgeDaysSpinner = new Spinner<>(1, 36500, 30); + private final ComboBox mAgeActionComboBox = new ComboBox<>(); + private final CheckBox mSizeRuleCheckBox = new CheckBox("Remove the oldest recordings when they exceed"); + private final Spinner mSizeGBSpinner = new Spinner<>(1, 100_000, 10); + private final ComboBox mSizeActionComboBox = new ComboBox<>(); + private final ComboBox mRetentionComboBox = new ComboBox<>(); + private final Button mRunAgeOffButton = new Button("Run Age-Off Now"); + private final Label mAgeOffResultLabel = new Label(); + + /** + * Constructs an instance + * @param userPreferences for the database preference + * @param databaseManager to test and switch databases + */ + public DatabasePreferenceEditor(UserPreferences userPreferences, DatabaseManager databaseManager) + { + mPreference = userPreferences.getDatabasePreference(); + mDatabaseManager = databaseManager; + + setPadding(new Insets(10)); + setSpacing(10); + + VBox content = new VBox(10, createDatabasePane(), new Separator(), createAgeOffPane()); + content.setPadding(new Insets(0, 10, 10, 0)); + ScrollPane scrollPane = new ScrollPane(content); + scrollPane.setFitToWidth(true); + VBox.setVgrow(scrollPane, Priority.ALWAYS); + getChildren().add(scrollPane); + + loadConfiguration(mPreference.getDatabaseConfiguration()); + updateStatus(); + + if(mDatabaseManager != null) + { + mDatabaseManager.addListener(mDatabaseListener); + } + } + + /** + * Deregisters from the database manager + */ + public void dispose() + { + if(mDatabaseManager != null) + { + mDatabaseManager.removeListener(mDatabaseListener); + } + } + + private VBox createDatabasePane() + { + Label title = new Label("Call Database"); + title.setStyle("-fx-font-weight: bold;"); + Label description = new Label("sdrtrunk stores a record of each call in this database. The built-in " + + "database needs no setup. You can use a MariaDB or PostgreSQL server instead; sdrtrunk creates the " + + "schema and tables automatically. The user needs permission to create schemas and tables."); + description.setWrapText(true); + mStatusLabel.setWrapText(true); + + mTypeComboBox.getItems().addAll(DatabaseType.values()); + mTypeComboBox.setOnAction(event -> { + DatabaseType type = mTypeComboBox.getValue(); + + if(type != null) + { + DatabaseConfiguration stored = mPreference.getStoredConfiguration(type); + + mPortField.setText(type.isNetwork() ? String.valueOf(stored.port()) : ""); + + updateFieldVisibility(); + } + }); + + mPortField.setPrefColumnCount(6); + mSchemaField.setTooltip(new Tooltip("Lower case letters, digits and underscores, starting with a letter")); + mPasswordField.setTooltip(new Tooltip("Note: the password is stored in plain text in the sdrtrunk " + + "preferences")); + mDatabaseField.setTooltip(new Tooltip("PostgreSQL database that holds the sdrtrunk schema")); + + GridPane grid = new GridPane(); + grid.setHgap(10); + grid.setVgap(8); + int row = 0; + ColumnConstraints labelColumn = new ColumnConstraints(); + labelColumn.setMinWidth(Region.USE_PREF_SIZE); + ColumnConstraints fieldColumn = new ColumnConstraints(); + fieldColumn.setHgrow(Priority.ALWAYS); + fieldColumn.setMaxWidth(420); + grid.getColumnConstraints().addAll(labelColumn, fieldColumn); + grid.add(new Label("Type:"), 0, row); + grid.add(mTypeComboBox, 1, row++); + grid.add(mHostLabel, 0, row); + grid.add(mHostField, 1, row++); + grid.add(mPortLabel, 0, row); + grid.add(mPortField, 1, row++); + grid.add(mDatabaseLabel, 0, row); + grid.add(mDatabaseField, 1, row++); + grid.add(mUserLabel, 0, row); + grid.add(mUserField, 1, row++); + grid.add(mPasswordLabel, 0, row); + grid.add(mPasswordField, 1, row++); + grid.add(new Label("Schema:"), 0, row); + grid.add(mSchemaField, 1, row++); + grid.add(mLocationLabel, 1, row++); + mLocationLabel.setWrapText(true); + GridPane.setHgrow(mHostField, Priority.ALWAYS); + mDatabaseLabel.setText("Database Name:"); + + mProgress.setVisible(false); + mProgress.setPrefSize(18, 18); + mTestButton.setOnAction(event -> testConnection()); + mApplyButton.setOnAction(event -> apply()); + mResetButton.setTooltip(new Tooltip("Discard edits and show the database that is in use")); + mResetButton.setOnAction(event -> { + loadConfiguration(mDatabaseManager != null ? mDatabaseManager.getConfiguration() : + mPreference.getDatabaseConfiguration()); + mResultLabel.setText(""); + }); + HBox buttons = new HBox(10, mTestButton, mApplyButton, mResetButton, mProgress); + mResultLabel.setWrapText(true); + + return new VBox(8, title, description, mStatusLabel, grid, buttons, mResultLabel); + } + + private VBox createAgeOffPane() + { + Label title = new Label("Call Audio Age-Off"); + title.setStyle("-fx-font-weight: bold;"); + Label description = new Label("Age-off rules remove calls and their recording files automatically " + + "(a minute after startup and then every 5 minutes). Calls in progress are never removed. Only " + + "recordings listed in the call database count toward the size limit. When the size rule is enabled, " + + "the status bar recordings meter shows usage against the size limit."); + description.setWrapText(true); + + mAgeActionComboBox.getItems().addAll(AgeOffAction.values()); + mSizeActionComboBox.getItems().addAll(AgeOffAction.values()); + mAgeDaysSpinner.setEditable(true); + mAgeDaysSpinner.setPrefWidth(100); + mSizeGBSpinner.setEditable(true); + mSizeGBSpinner.setPrefWidth(100); + + mAgeRuleCheckBox.setSelected(mPreference.isAgeRuleEnabled()); + mAgeDaysSpinner.getValueFactory().setValue(mPreference.getAgeRuleDays()); + mAgeActionComboBox.setValue(mPreference.getAgeRuleAction()); + mSizeRuleCheckBox.setSelected(mPreference.isSizeRuleEnabled()); + mSizeGBSpinner.getValueFactory().setValue(Math.max(1, mPreference.getSizeRuleLimitMB() / MB_PER_GB)); + mSizeActionComboBox.setValue(mPreference.getSizeRuleAction()); + + mAgeRuleCheckBox.selectedProperty().addListener((obs, old, selected) -> { + mPreference.setAgeRuleEnabled(selected); + updateAgeOffControls(); + }); + mAgeDaysSpinner.valueProperty().addListener((obs, old, value) -> { + if(value != null) + { + mPreference.setAgeRuleDays(value); + } + }); + mAgeActionComboBox.valueProperty().addListener((obs, old, value) -> { + if(value != null) + { + mPreference.setAgeRuleAction(value); + } + }); + mSizeRuleCheckBox.selectedProperty().addListener((obs, old, selected) -> { + mPreference.setSizeRuleEnabled(selected); + updateAgeOffControls(); + }); + mSizeGBSpinner.valueProperty().addListener((obs, old, value) -> { + if(value != null) + { + mPreference.setSizeRuleLimitMB(value * MB_PER_GB); + } + }); + mSizeActionComboBox.valueProperty().addListener((obs, old, value) -> { + if(value != null) + { + mPreference.setSizeRuleAction(value); + } + }); + + mRetentionComboBox.getItems().addAll(RetentionPeriod.values()); + mRetentionComboBox.setValue(mPreference.getDuplicateDoNotMonitorRetention()); + mRetentionComboBox.valueProperty().addListener((obs, old, value) -> { + if(value != null) + { + mPreference.setDuplicateDoNotMonitorRetention(value); + updateAgeOffControls(); + } + }); + Label retentionLabel = new Label("Delete duplicate and Do Not Monitor calls after"); + Label retentionNote = new Label("Deletes the call and its recording. A Do Not Monitor call is kept when " + + "an alias of the call has the Record option."); + retentionNote.setWrapText(true); + + GridPane grid = new GridPane(); + grid.setHgap(10); + grid.setVgap(8); + ColumnConstraints ruleColumn = new ColumnConstraints(); + ruleColumn.setMinWidth(Region.USE_PREF_SIZE); + grid.getColumnConstraints().add(ruleColumn); + grid.add(retentionLabel, 0, 0); + grid.add(mRetentionComboBox, 1, 0, 2, 1); + grid.add(retentionNote, 0, 1, 3, 1); + grid.add(mAgeRuleCheckBox, 0, 2); + grid.add(new HBox(6, mAgeDaysSpinner, new Label("days")), 1, 2); + grid.add(mAgeActionComboBox, 2, 2); + grid.add(mSizeRuleCheckBox, 0, 3); + grid.add(new HBox(6, mSizeGBSpinner, new Label("GB")), 1, 3); + grid.add(mSizeActionComboBox, 2, 3); + + mRunAgeOffButton.setOnAction(event -> runAgeOff()); + mAgeOffResultLabel.setWrapText(true); + updateAgeOffControls(); + + return new VBox(8, title, description, grid, new HBox(10, mRunAgeOffButton, mAgeOffResultLabel)); + } + + private void updateAgeOffControls() + { + mAgeDaysSpinner.setDisable(!mAgeRuleCheckBox.isSelected()); + mAgeActionComboBox.setDisable(!mAgeRuleCheckBox.isSelected()); + mSizeGBSpinner.setDisable(!mSizeRuleCheckBox.isSelected()); + mSizeActionComboBox.setDisable(!mSizeRuleCheckBox.isSelected()); + RetentionPeriod retention = mRetentionComboBox.getValue(); + mRunAgeOffButton.setDisable(mDatabaseManager == null || ((retention == null || !retention.isEnabled()) && + !mAgeRuleCheckBox.isSelected() && !mSizeRuleCheckBox.isSelected())); + } + + private void runAgeOff() + { + if(mDatabaseManager == null) + { + return; + } + + if(!confirm("Run Age-Off", "Apply the enabled age-off rules now?", + "Calls and recording files that the rules select are deleted permanently.")) + { + return; + } + + mRunAgeOffButton.setDisable(true); + mAgeOffResultLabel.setText("Running ..."); + mDatabaseManager.getAgeOffManager().runNow().whenComplete((result, error) -> Platform.runLater(() -> { + updateAgeOffControls(); + + if(error != null) + { + mAgeOffResultLabel.setText("Error: " + getMessage(error)); + } + else + { + mAgeOffResultLabel.setText("Removed " + result.calls() + " calls and " + result.recordings() + + " recordings (" + (result.bytes() / AgeOffManager.BYTES_PER_MB) + " MB)"); + } + })); + } + + /** + * Shows the configuration in the editor fields. + */ + private void loadConfiguration(DatabaseConfiguration configuration) + { + DatabaseConfiguration network = configuration.type().isNetwork() ? configuration : + mPreference.getStoredConfiguration(DatabaseType.MARIADB); + mTypeComboBox.setValue(configuration.type()); + mHostField.setText(network.host()); + mPortField.setText(configuration.type().isNetwork() ? String.valueOf(configuration.port()) : ""); + mDatabaseField.setText(network.database()); + mUserField.setText(network.user()); + mPasswordField.setText(network.password()); + mSchemaField.setText(configuration.schema()); + updateFieldVisibility(); + } + + private void updateFieldVisibility() + { + DatabaseType type = mTypeComboBox.getValue(); + boolean network = type != null && type.isNetwork(); + boolean database = type != null && type.requiresDatabaseName(); + + for(javafx.scene.Node node: new javafx.scene.Node[]{mHostLabel, mHostField, mPortLabel, mPortField, mUserLabel, + mUserField, mPasswordLabel, mPasswordField}) + { + node.setVisible(network); + node.setManaged(network); + } + + mDatabaseLabel.setVisible(database); + mDatabaseLabel.setManaged(database); + mDatabaseField.setVisible(database); + mDatabaseField.setManaged(database); + + if(!network && mDatabaseManager != null) + { + mLocationLabel.setText("Database files: " + mDatabaseManager.getDatabaseDirectory()); + mLocationLabel.setVisible(true); + mLocationLabel.setManaged(true); + } + else + { + mLocationLabel.setVisible(false); + mLocationLabel.setManaged(false); + } + } + + private void updateStatus() + { + if(mDatabaseManager == null) + { + mStatusLabel.setText("Call database is not available"); + return; + } + + if(mDatabaseManager.isConnected()) + { + mStatusLabel.setText("In use: " + mDatabaseManager.getConfiguration().getDescription()); + mStatusLabel.setStyle(""); + } + else + { + mStatusLabel.setText("Not connected: " + mDatabaseManager.getConfiguration().getDescription() + + (mDatabaseManager.getErrorMessage() != null ? " - " + mDatabaseManager.getErrorMessage() : "") + + "\nCalls are still recorded and are stored when the database becomes available."); + mStatusLabel.setStyle("-fx-text-fill: #d9534f;"); + } + } + + /** + * Creates a configuration from the editor fields. + * @return configuration, or null if the fields are invalid (the problem is shown to the user) + */ + private DatabaseConfiguration getEditedConfiguration() + { + DatabaseType type = mTypeComboBox.getValue(); + + if(type == null) + { + showResult("Select a database", true); + return null; + } + + int port = 0; + + if(type.isNetwork()) + { + try + { + port = Integer.parseInt(mPortField.getText().trim()); + } + catch(NumberFormatException nfe) + { + showResult("Port must be a number", true); + return null; + } + } + + DatabaseConfiguration configuration = new DatabaseConfiguration(type, mHostField.getText(), port, + mDatabaseField.getText(), mSchemaField.getText(), mUserField.getText(), mPasswordField.getText()); + String problem = configuration.validate(); + + if(problem != null) + { + showResult(problem, true); + return null; + } + + return configuration; + } + + private void showResult(String text, boolean error) + { + mResultLabel.setText(text); + mResultLabel.setStyle(error ? "-fx-text-fill: #d9534f;" : ""); + } + + private void setBusy(boolean busy) + { + mProgress.setVisible(busy); + mTestButton.setDisable(busy); + mApplyButton.setDisable(busy); + mResetButton.setDisable(busy); + } + + private void testConnection() + { + DatabaseConfiguration configuration = getEditedConfiguration(); + + if(configuration == null || mDatabaseManager == null) + { + return; + } + + setBusy(true); + showResult("Connecting ...", false); + mDatabaseManager.probe(configuration).whenComplete((result, error) -> Platform.runLater(() -> { + setBusy(false); + + if(error != null) + { + showResult("Connection failed: " + getMessage(error), true); + } + else + { + showResult("Connection successful. " + describe(configuration, result), false); + } + })); + } + + private static String describe(DatabaseConfiguration configuration, DatabaseManager.ProbeResult result) + { + return switch(result.status()) + { + case MISSING -> "Schema [" + configuration.schema() + "] doesn't exist and will be created."; + case EMPTY -> "Schema [" + configuration.schema() + "] exists and the sdrtrunk tables will be created in it."; + case SDRTRUNK -> "Schema [" + configuration.schema() + "] has sdrtrunk tables with " + result.callCount() + + " calls."; + }; + } + + /** + * Applies the edited database selection. + */ + private void apply() + { + DatabaseConfiguration target = getEditedConfiguration(); + + if(target == null || mDatabaseManager == null) + { + return; + } + + DatabaseConfiguration current = mDatabaseManager.getConfiguration(); + + if(target.equals(current) && mDatabaseManager.isConnected()) + { + showResult("This database is already in use.", false); + return; + } + + setBusy(true); + showResult("Connecting ...", false); + + mDatabaseManager.probe(target).whenComplete((result, error) -> Platform.runLater(() -> { + if(error != null) + { + setBusy(false); + showResult("Connection failed: " + getMessage(error), true); + return; + } + + //Same database and schema, for example a changed password: reconnect without moving any calls + if(target.isSameLocation(current)) + { + startSwitch(target, DatabaseManager.ExistingSchemaOption.APPEND, false); + return; + } + + chooseSchemaOption(target, result); + })); + } + + /** + * Asks how to treat an existing sdrtrunk schema in the target database, then continues with the transfer question. + */ + private void chooseSchemaOption(DatabaseConfiguration target, DatabaseManager.ProbeResult result) + { + if(result.status() != SchemaStatus.SDRTRUNK) + { + chooseTransfer(target, DatabaseManager.ExistingSchemaOption.APPEND); + return; + } + + ButtonType append = new ButtonType("Append", ButtonBar.ButtonData.YES); + ButtonType clear = new ButtonType("Clear", ButtonBar.ButtonData.OTHER); + ButtonType separate = new ButtonType("Create Separate Schema", ButtonBar.ButtonData.OTHER); + ButtonType cancel = new ButtonType("Cancel", ButtonBar.ButtonData.CANCEL_CLOSE); + + Alert alert = new Alert(Alert.AlertType.CONFIRMATION, "", append, clear, separate, cancel); + alert.initOwner(getWindow()); + alert.initModality(Modality.WINDOW_MODAL); + alert.setTitle("Existing sdrtrunk Schema"); + alert.setHeaderText("Schema [" + target.schema() + "] already has sdrtrunk tables with " + result.callCount() + + " calls"); + alert.setContentText("Append: keep the existing calls and add new calls to them.\n\n" + + "Clear: delete the existing calls and start with an empty schema. Recording files are not deleted.\n\n" + + "Create Separate Schema: leave this schema as it is and use a new schema with a different name."); + alert.getDialogPane().setMinWidth(560); + + Optional choice = alert.showAndWait(); + + if(choice.isEmpty() || choice.get() == cancel) + { + setBusy(false); + showResult("Cancelled", false); + } + else if(choice.get() == append) + { + chooseTransfer(target, DatabaseManager.ExistingSchemaOption.APPEND); + } + else if(choice.get() == clear) + { + if(confirm("Clear Schema", "Delete all " + result.callCount() + " calls in schema [" + target.schema() + + "]?", "This can't be undone.")) + { + chooseTransfer(target, DatabaseManager.ExistingSchemaOption.CLEAR); + } + else + { + setBusy(false); + showResult("Cancelled", false); + } + } + else + { + chooseSeparateSchema(target, result.suggestedSchema()); + } + } + + /** + * Asks for the name of a separate schema and checks that it doesn't exist. + */ + private void chooseSeparateSchema(DatabaseConfiguration target, String suggestion) + { + TextInputDialog dialog = new TextInputDialog(suggestion); + dialog.initOwner(getWindow()); + dialog.setTitle("Create Separate Schema"); + dialog.setHeaderText("Name for the new schema"); + dialog.setContentText("Schema:"); + + Optional name = dialog.showAndWait(); + + if(name.isEmpty()) + { + setBusy(false); + showResult("Cancelled", false); + return; + } + + String schema = name.get().trim().toLowerCase(); + + if(!DatabaseConfiguration.isValidSchemaName(schema)) + { + showError("Invalid Schema Name", "Schema names start with a letter and contain only lower case letters, " + + "digits and underscores (max 63 characters)."); + chooseSeparateSchema(target, suggestion); + return; + } + + DatabaseConfiguration separate = target.withSchema(schema); + + mDatabaseManager.schemaExists(target, schema).whenComplete((exists, error) -> Platform.runLater(() -> { + if(error != null) + { + setBusy(false); + showResult("Error: " + getMessage(error), true); + } + else if(exists) + { + showError("Schema Exists", "Schema [" + schema + "] already exists. Choose a different name."); + chooseSeparateSchema(target, suggestion); + } + else + { + mSchemaField.setText(schema); + chooseTransfer(separate, DatabaseManager.ExistingSchemaOption.APPEND); + } + })); + } + + /** + * Asks whether to transfer the calls from the current database, then switches. + */ + private void chooseTransfer(DatabaseConfiguration target, DatabaseManager.ExistingSchemaOption option) + { + CompletableFuture countFuture = CompletableFuture.supplyAsync(() -> { + try + { + return mDatabaseManager.isConnected() ? mDatabaseManager.getDatabase().getCallCount() : 0L; + } + catch(Exception e) + { + return 0L; + } + }); + + countFuture.whenComplete((count, error) -> Platform.runLater(() -> { + long calls = count != null ? count : 0; + + if(calls <= 0) + { + startSwitch(target, option, false); + return; + } + + ButtonType transfer = new ButtonType("Transfer", ButtonBar.ButtonData.YES); + ButtonType skip = new ButtonType("Don't Transfer", ButtonBar.ButtonData.NO); + ButtonType cancel = new ButtonType("Cancel", ButtonBar.ButtonData.CANCEL_CLOSE); + Alert alert = new Alert(Alert.AlertType.CONFIRMATION, "", transfer, skip, cancel); + alert.initOwner(getWindow()); + alert.initModality(Modality.WINDOW_MODAL); + alert.setTitle("Transfer Calls"); + alert.setHeaderText("Transfer your existing calls to the new database?"); + alert.setContentText("Copy the " + calls + " calls in " + mDatabaseManager.getConfiguration() + .getDescription() + " to " + target.getDescription() + ".\n\nThe calls are copied; the current " + + "database isn't changed." + (option == DatabaseManager.ExistingSchemaOption.APPEND ? + " Calls that already exist in the new database are skipped." : "")); + alert.getDialogPane().setMinWidth(560); + + Optional choice = alert.showAndWait(); + + if(choice.isEmpty() || choice.get() == cancel) + { + setBusy(false); + showResult("Cancelled", false); + } + else + { + startSwitch(target, option, choice.get() == transfer); + } + })); + } + + private void startSwitch(DatabaseConfiguration target, DatabaseManager.ExistingSchemaOption option, + boolean transfer) + { + setBusy(true); + showResult("Switching ...", false); + + mDatabaseManager.switchDatabase(target, option, transfer, + status -> Platform.runLater(() -> showResult(status, false))) + .whenComplete((result, error) -> Platform.runLater(() -> { + setBusy(false); + + if(error != null) + { + showResult("Unable to switch databases: " + getMessage(error) + "\nThe current database is " + + "still in use.", true); + } + else + { + loadConfiguration(target); + showResult("Now using " + target.getDescription() + (transfer ? ". Transferred " + + result.transferred() + " calls" + (result.skipped() > 0 ? ", skipped " + result.skipped() + + " calls that already existed or were in progress" : "") + "." : "."), false); + } + + updateStatus(); + })); + } + + private boolean confirm(String title, String header, String content) + { + Alert alert = new Alert(Alert.AlertType.CONFIRMATION, content, ButtonType.OK, ButtonType.CANCEL); + alert.initOwner(getWindow()); + alert.setTitle(title); + alert.setHeaderText(header); + return alert.showAndWait().filter(button -> button == ButtonType.OK).isPresent(); + } + + private void showError(String title, String content) + { + Alert alert = new Alert(Alert.AlertType.ERROR, content, ButtonType.OK); + alert.initOwner(getWindow()); + alert.setTitle(title); + alert.setHeaderText(null); + alert.showAndWait(); + } + + private Window getWindow() + { + return getScene() != null ? getScene().getWindow() : null; + } + + private static String getMessage(Throwable error) + { + Throwable cause = error instanceof CompletionException && error.getCause() != null ? error.getCause() : error; + return cause.getMessage() != null ? cause.getMessage() : cause.getClass().getSimpleName(); + } +} diff --git a/src/main/java/io/github/dsheirer/gui/preference/playback/PlaybackPreferenceEditor.java b/src/main/java/io/github/dsheirer/gui/preference/playback/PlaybackPreferenceEditor.java index c0ed703c..57ada1fa 100644 --- a/src/main/java/io/github/dsheirer/gui/preference/playback/PlaybackPreferenceEditor.java +++ b/src/main/java/io/github/dsheirer/gui/preference/playback/PlaybackPreferenceEditor.java @@ -33,6 +33,8 @@ import javafx.scene.control.Button; import javafx.scene.control.ComboBox; import javafx.scene.control.Label; import javafx.scene.control.Separator; +import javafx.scene.control.Spinner; +import javafx.scene.control.Tooltip; import javafx.scene.layout.GridPane; import javafx.scene.layout.HBox; import javafx.scene.layout.Priority; @@ -111,6 +113,24 @@ public class PlaybackPreferenceEditor extends HBox mEditorPane.add(dropVolumeLabel, 3, row); mEditorPane.add(getDropToneVolumeComboBox(), 4, row); mEditorPane.add(getTestDropToneButton(), 5, row); + + mEditorPane.add(new Separator(Orientation.HORIZONTAL), 0, ++row, 6, 1); + mEditorPane.add(new Label("Talkgroup Hold"), 0, ++row, 2, 1); + Label releaseLabel = new Label("Release an idle hold after"); + mEditorPane.add(releaseLabel, 0, ++row, 2, 1); + Spinner releaseSpinner = new Spinner<>(0, 1440, mPlaybackPreference.getHoldAutoReleaseMinutes()); + releaseSpinner.setEditable(true); + releaseSpinner.setPrefWidth(90); + releaseSpinner.setTooltip(new Tooltip("Minutes without a call to the hold talkgroup before the hold is " + + "released automatically. 0 = never release automatically.")); + releaseSpinner.valueProperty().addListener((observable, oldValue, newValue) -> { + if(newValue != null) + { + mPlaybackPreference.setHoldAutoReleaseMinutes(newValue); + } + }); + mEditorPane.add(releaseSpinner, 2, row); + mEditorPane.add(new Label("minutes (0 = never)"), 3, row, 2, 1); } return mEditorPane; diff --git a/src/main/java/io/github/dsheirer/gui/preference/record/RecordPreferenceEditor.java b/src/main/java/io/github/dsheirer/gui/preference/record/RecordPreferenceEditor.java index 3df5c362..2ef67aff 100644 --- a/src/main/java/io/github/dsheirer/gui/preference/record/RecordPreferenceEditor.java +++ b/src/main/java/io/github/dsheirer/gui/preference/record/RecordPreferenceEditor.java @@ -26,6 +26,7 @@ import io.github.dsheirer.record.RecordFormat; import javafx.beans.value.ChangeListener; import javafx.beans.value.ObservableValue; import javafx.geometry.Insets; +import javafx.scene.control.CheckBox; import javafx.scene.control.ComboBox; import javafx.scene.control.Label; import javafx.scene.control.Tooltip; @@ -71,6 +72,19 @@ public class RecordPreferenceEditor extends HBox Label basebandLabel = new Label("Baseband I/Q Recording Format:"); mEditorPane.add(basebandLabel, 0, 1); mEditorPane.add(getBasebandRecordFormatComboBox(), 1, 1); + + CheckBox recordAll = new CheckBox("Record all calls"); + recordAll.setSelected(mRecordPreference.isRecordAllCalls()); + recordAll.selectedProperty().addListener((observable, oldValue, selected) -> + mRecordPreference.setRecordAllCalls(selected)); + mEditorPane.add(recordAll, 0, 2, 2, 1); + Label recordAllNote = new Label("Records the audio of every call. When off, only calls with an alias " + + "that has the Record option are recorded. Duplicate and Do Not Monitor calls are deleted after the " + + "retention time set in the Call Database preferences. Use the Call Database age-off rules to limit " + + "the disk space used by recordings."); + recordAllNote.setWrapText(true); + recordAllNote.setMaxWidth(560); + mEditorPane.add(recordAllNote, 0, 3, 2, 1); } return mEditorPane; diff --git a/src/main/java/io/github/dsheirer/module/decode/p25/phase2/P25P2MessageFramer.java b/src/main/java/io/github/dsheirer/module/decode/p25/phase2/P25P2MessageFramer.java index 9667ef23..0ac894d5 100644 --- a/src/main/java/io/github/dsheirer/module/decode/p25/phase2/P25P2MessageFramer.java +++ b/src/main/java/io/github/dsheirer/module/decode/p25/phase2/P25P2MessageFramer.java @@ -33,7 +33,7 @@ import io.github.dsheirer.module.decode.p25.audio.P25P2AudioModule; import io.github.dsheirer.module.decode.p25.phase2.enumeration.ScrambleParameters; import io.github.dsheirer.module.decode.p25.phase2.message.P25P2Message; import io.github.dsheirer.preference.UserPreferences; -import io.github.dsheirer.record.AudioRecordingManager; +import io.github.dsheirer.call.CallManager; import io.github.dsheirer.record.binary.BinaryReader; import io.github.dsheirer.sample.Listener; import java.io.IOException; @@ -202,7 +202,7 @@ public class P25P2MessageFramer extends C4FMMessageFramer // alias.addAliasID(new Talkgroup(Protocol.APCO25, 11857)); // alias.addAliasID(new Talkgroup(Protocol.APCO25, 12601)); // aliasList.addAlias(alias); - AudioRecordingManager recordingManager = new AudioRecordingManager(userPreferences); + CallManager recordingManager = new CallManager(userPreferences, null); recordingManager.start(); ProcessingChain processingChain = new ProcessingChain(channel, new AliasModel()); diff --git a/src/main/java/io/github/dsheirer/monitor/ResourceMonitor.java b/src/main/java/io/github/dsheirer/monitor/ResourceMonitor.java index 996b87a5..39655fcb 100644 --- a/src/main/java/io/github/dsheirer/monitor/ResourceMonitor.java +++ b/src/main/java/io/github/dsheirer/monitor/ResourceMonitor.java @@ -21,6 +21,7 @@ package io.github.dsheirer.monitor; import io.github.dsheirer.log.LoggingSuppressor; import io.github.dsheirer.preference.UserPreferences; +import io.github.dsheirer.preference.database.DatabasePreference; import io.github.dsheirer.util.ThreadPool; import java.io.IOException; import java.lang.management.ManagementFactory; @@ -56,8 +57,9 @@ public class ResourceMonitor { private static final Logger sLog = LoggerFactory.getLogger(ResourceMonitor.class); private static final LoggingSuppressor sLogSuppressor = new LoggingSuppressor(sLog); - private static final int SCALOR_MEGABYTE = 1024 * 1024; + private static final long SCALOR_MEGABYTE = 1024L * 1024L; private UserPreferences mUserPreferences; + private volatile DatabasePreference mDatabasePreference; private ScheduledFuture mMemoryCpuMonitorFuture; private ScheduledFuture mStorageMonitorFuture; private LongProperty mMemoryTotal = new SimpleLongProperty(); @@ -108,6 +110,14 @@ public class ResourceMonitor } } + /** + * Sets the call database preference so that the recordings usage meter can use the size age-off rule limit. + */ + public void setDatabasePreference(DatabasePreference databasePreference) + { + mDatabasePreference = databasePreference; + } + /** * Stops resource monitoring. */ @@ -159,6 +169,14 @@ public class ResourceMonitor long thresholdEventLog = mUserPreferences.getDirectoryPreference().getDirectoryMaxUsageEventLogs() * SCALOR_MEGABYTE; long thresholdRecording = mUserPreferences.getDirectoryPreference().getDirectoryMaxUsageRecordings() * SCALOR_MEGABYTE; + //When the call audio size age-off rule is enabled, the recordings meter shows usage against the rule's limit + DatabasePreference databasePreference = mDatabasePreference; + + if(databasePreference != null && databasePreference.isSizeRuleEnabled()) + { + thresholdRecording = databasePreference.getSizeRuleLimitMB() * SCALOR_MEGABYTE; + } + Path recordingPath = mUserPreferences.getDirectoryPreference().getDirectoryRecording(); Path eventLogsPath = mUserPreferences.getDirectoryPreference().getDirectoryEventLog(); diff --git a/src/main/java/io/github/dsheirer/preference/PreferenceType.java b/src/main/java/io/github/dsheirer/preference/PreferenceType.java index 8acaa623..9a7c3ed7 100644 --- a/src/main/java/io/github/dsheirer/preference/PreferenceType.java +++ b/src/main/java/io/github/dsheirer/preference/PreferenceType.java @@ -26,6 +26,7 @@ public enum PreferenceType { APPLICATION, CALIBRATION, + DATABASE, DECODE_EVENT, DIRECTORY, DUPLICATE_CALL_DETECTION, diff --git a/src/main/java/io/github/dsheirer/preference/UserPreferences.java b/src/main/java/io/github/dsheirer/preference/UserPreferences.java index f6a7ed00..6c23e81a 100644 --- a/src/main/java/io/github/dsheirer/preference/UserPreferences.java +++ b/src/main/java/io/github/dsheirer/preference/UserPreferences.java @@ -22,6 +22,7 @@ package io.github.dsheirer.preference; import io.github.dsheirer.eventbus.MyEventBus; import io.github.dsheirer.preference.application.ApplicationPreference; import io.github.dsheirer.preference.calibration.VectorCalibrationPreference; +import io.github.dsheirer.preference.database.DatabasePreference; import io.github.dsheirer.preference.decoder.JmbeLibraryPreference; import io.github.dsheirer.preference.directory.DirectoryPreference; import io.github.dsheirer.preference.duplicate.CallManagementPreference; @@ -74,6 +75,7 @@ public class UserPreferences implements Listener private VectorCalibrationPreference mVectorCalibrationPreference; private SwingPreference mSwingPreference = new SwingPreference(); + private DatabasePreference mDatabasePreference; private JavaFxPreferences mJavaFxPreferences = new JavaFxPreferences(); /** @@ -111,7 +113,15 @@ public class UserPreferences implements Listener } /** - * Decode Event preferences + * Call database and age-off preferences + */ + public DatabasePreference getDatabasePreference() + { + return mDatabasePreference; + } + + /** + * Decode event preferences */ public DecodeEventPreference getDecodeEventPreference() { @@ -231,6 +241,7 @@ public class UserPreferences implements Listener { mApplicationPreference = new ApplicationPreference(this::receive); mChannelMultiFrequencyPreference = new ChannelMultiFrequencyPreference(this::receive); + mDatabasePreference = new DatabasePreference(this::receive); mDecodeEventPreference = new DecodeEventPreference(this::receive); mDirectoryPreference = new DirectoryPreference(this::receive); mCallManagementPreference = new CallManagementPreference(this::receive); diff --git a/src/main/java/io/github/dsheirer/preference/database/DatabasePreference.java b/src/main/java/io/github/dsheirer/preference/database/DatabasePreference.java new file mode 100644 index 00000000..c571ec69 --- /dev/null +++ b/src/main/java/io/github/dsheirer/preference/database/DatabasePreference.java @@ -0,0 +1,240 @@ +/* + * ***************************************************************************** + * Copyright (C) 2014-2026 Dennis Sheirer + * + * 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 + * the Free Software Foundation, either version 3 of the License, or + * (at your option) any later version. + * + * This program is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU General Public License for more details. + * + * You should have received a copy of the GNU General Public License + * along with this program. If not, see + * **************************************************************************** + */ +package io.github.dsheirer.preference.database; + +import io.github.dsheirer.database.DatabaseConfiguration; +import io.github.dsheirer.database.DatabaseType; +import io.github.dsheirer.database.ageoff.AgeOffAction; +import io.github.dsheirer.database.ageoff.RetentionPeriod; +import io.github.dsheirer.preference.Preference; +import io.github.dsheirer.preference.PreferenceType; +import io.github.dsheirer.sample.Listener; + +import java.util.prefs.Preferences; + +/** + * Call database selection and call audio age-off rule preferences. + * + * Note: the network database password is stored in plain text in the Java preferences store. + */ +public class DatabasePreference extends Preference +{ + private static final String KEY_TYPE = "database.type"; + private static final String KEY_HOST = "database.host"; + private static final String KEY_PORT = "database.port"; + private static final String KEY_DATABASE = "database.name"; + private static final String KEY_SCHEMA = "database.schema"; + private static final String KEY_USER = "database.user"; + private static final String KEY_PASSWORD = "database.password"; + private static final String KEY_AGE_RULE_ENABLED = "ageoff.age.enabled"; + private static final String KEY_AGE_RULE_DAYS = "ageoff.age.days"; + private static final String KEY_AGE_RULE_ACTION = "ageoff.age.action"; + private static final String KEY_SIZE_RULE_ENABLED = "ageoff.size.enabled"; + private static final String KEY_SIZE_RULE_LIMIT_MB = "ageoff.size.limit.mb"; + private static final String KEY_SIZE_RULE_ACTION = "ageoff.size.action"; + private static final String KEY_DUPLICATE_DNM_RETENTION = "ageoff.duplicate.dnm.retention"; + + public static final String DEFAULT_POSTGRES_DATABASE = "postgres"; + public static final int DEFAULT_AGE_DAYS = 30; + public static final int DEFAULT_SIZE_LIMIT_MB = 10 * 1024; + + private final Preferences mPreferences = Preferences.userNodeForPackage(DatabasePreference.class); + + /** + * Constructs an instance + * @param updateListener to receive notifications whenever these preferences change + */ + public DatabasePreference(Listener updateListener) + { + super(updateListener); + } + + @Override + public PreferenceType getPreferenceType() + { + return PreferenceType.DATABASE; + } + + /** + * Currently selected call database. Defaults to the built-in H2 database. + */ + public DatabaseConfiguration getDatabaseConfiguration() + { + DatabaseType type; + + try + { + type = DatabaseType.valueOf(mPreferences.get(KEY_TYPE, DatabaseType.H2.name())); + } + catch(IllegalArgumentException iae) + { + type = DatabaseType.H2; + } + + return new DatabaseConfiguration(type, mPreferences.get(KEY_HOST, "localhost"), + mPreferences.getInt(KEY_PORT, type.getDefaultPort()), + type.requiresDatabaseName() ? mPreferences.get(KEY_DATABASE, DEFAULT_POSTGRES_DATABASE) : "", + mPreferences.get(KEY_SCHEMA, DatabaseConfiguration.DEFAULT_SCHEMA), + mPreferences.get(KEY_USER, ""), mPreferences.get(KEY_PASSWORD, "")); + } + + /** + * Stores the selected call database. The database manager applies the change; this only persists it. + */ + public void setDatabaseConfiguration(DatabaseConfiguration configuration) + { + mPreferences.put(KEY_TYPE, configuration.type().name()); + mPreferences.put(KEY_SCHEMA, configuration.schema()); + + if(configuration.type().isNetwork()) + { + mPreferences.put(KEY_HOST, configuration.host()); + mPreferences.putInt(KEY_PORT, configuration.port()); + mPreferences.put(KEY_USER, configuration.user()); + mPreferences.put(KEY_PASSWORD, configuration.password()); + } + + if(configuration.type().requiresDatabaseName()) + { + mPreferences.put(KEY_DATABASE, configuration.database()); + } + + notifyPreferenceUpdated(); + } + + /** + * Last used network settings for the database type, used to pre-fill the editor when the user switches types. + */ + public DatabaseConfiguration getStoredConfiguration(DatabaseType type) + { + DatabaseConfiguration current = getDatabaseConfiguration(); + int port = current.type() == type ? current.port() : type.getDefaultPort(); + return new DatabaseConfiguration(type, mPreferences.get(KEY_HOST, "localhost"), port, + type.requiresDatabaseName() ? mPreferences.get(KEY_DATABASE, DEFAULT_POSTGRES_DATABASE) : "", + current.schema(), + mPreferences.get(KEY_USER, ""), mPreferences.get(KEY_PASSWORD, "")); + } + + public boolean isAgeRuleEnabled() + { + return mPreferences.getBoolean(KEY_AGE_RULE_ENABLED, false); + } + + public void setAgeRuleEnabled(boolean enabled) + { + mPreferences.putBoolean(KEY_AGE_RULE_ENABLED, enabled); + notifyPreferenceUpdated(); + } + + /** + * Maximum call age in days for the age rule + */ + public int getAgeRuleDays() + { + return Math.max(1, mPreferences.getInt(KEY_AGE_RULE_DAYS, DEFAULT_AGE_DAYS)); + } + + public void setAgeRuleDays(int days) + { + mPreferences.putInt(KEY_AGE_RULE_DAYS, Math.max(1, days)); + notifyPreferenceUpdated(); + } + + public AgeOffAction getAgeRuleAction() + { + return getAction(KEY_AGE_RULE_ACTION); + } + + public void setAgeRuleAction(AgeOffAction action) + { + mPreferences.put(KEY_AGE_RULE_ACTION, action.name()); + notifyPreferenceUpdated(); + } + + public boolean isSizeRuleEnabled() + { + return mPreferences.getBoolean(KEY_SIZE_RULE_ENABLED, false); + } + + public void setSizeRuleEnabled(boolean enabled) + { + mPreferences.putBoolean(KEY_SIZE_RULE_ENABLED, enabled); + notifyPreferenceUpdated(); + } + + /** + * Maximum total size of call recordings in megabytes for the size rule + */ + public int getSizeRuleLimitMB() + { + return Math.max(1, mPreferences.getInt(KEY_SIZE_RULE_LIMIT_MB, DEFAULT_SIZE_LIMIT_MB)); + } + + public void setSizeRuleLimitMB(int limitMB) + { + mPreferences.putInt(KEY_SIZE_RULE_LIMIT_MB, Math.max(1, limitMB)); + notifyPreferenceUpdated(); + } + + public AgeOffAction getSizeRuleAction() + { + return getAction(KEY_SIZE_RULE_ACTION); + } + + public void setSizeRuleAction(AgeOffAction action) + { + mPreferences.put(KEY_SIZE_RULE_ACTION, action.name()); + notifyPreferenceUpdated(); + } + + /** + * How long duplicate calls, and Do Not Monitor calls that aren't marked for recording by an alias, are kept + * before the call and its recording are deleted. + */ + public RetentionPeriod getDuplicateDoNotMonitorRetention() + { + try + { + return RetentionPeriod.valueOf(mPreferences.get(KEY_DUPLICATE_DNM_RETENTION, + RetentionPeriod.DEFAULT.name())); + } + catch(IllegalArgumentException iae) + { + return RetentionPeriod.DEFAULT; + } + } + + public void setDuplicateDoNotMonitorRetention(RetentionPeriod retention) + { + mPreferences.put(KEY_DUPLICATE_DNM_RETENTION, retention.name()); + notifyPreferenceUpdated(); + } + + private AgeOffAction getAction(String key) + { + try + { + return AgeOffAction.valueOf(mPreferences.get(key, AgeOffAction.DELETE_CALL_AND_RECORDING.name())); + } + catch(IllegalArgumentException iae) + { + return AgeOffAction.DELETE_CALL_AND_RECORDING; + } + } +} diff --git a/src/main/java/io/github/dsheirer/preference/directory/DirectoryPreference.java b/src/main/java/io/github/dsheirer/preference/directory/DirectoryPreference.java index 4ef86832..045f0119 100644 --- a/src/main/java/io/github/dsheirer/preference/directory/DirectoryPreference.java +++ b/src/main/java/io/github/dsheirer/preference/directory/DirectoryPreference.java @@ -43,6 +43,7 @@ public class DirectoryPreference extends Preference private static final String DIRECTORY_APPLICATION_ROOT = "SDRTrunk"; private static final String DIRECTORY_APPLICATION_LOG = "logs"; private static final String DIRECTORY_CONFIGURATION = "configuration"; + private static final String DIRECTORY_DATABASE = "db"; private static final String DIRECTORY_EVENT_LOG = "event_logs"; private static final String DIRECTORY_JMBE = "jmbe"; private static final String DIRECTORY_PLAYLIST = "playlist"; @@ -480,6 +481,14 @@ public class DirectoryPreference extends Preference return Paths.get(System.getProperty("user.home"), DIRECTORY_APPLICATION_ROOT); } + /** + * Built-in (H2) call database directory: the 'db' folder in the application root directory. + */ + public Path getDirectoryDatabase() + { + return getDirectoryApplicationRoot().resolve(DIRECTORY_DATABASE); + } + /** * Default event logs directory */ diff --git a/src/main/java/io/github/dsheirer/preference/duplicate/CallManagementPreference.java b/src/main/java/io/github/dsheirer/preference/duplicate/CallManagementPreference.java index 1a8ea7ea..b033059c 100644 --- a/src/main/java/io/github/dsheirer/preference/duplicate/CallManagementPreference.java +++ b/src/main/java/io/github/dsheirer/preference/duplicate/CallManagementPreference.java @@ -143,7 +143,10 @@ public class CallManagementPreference extends Preference implements ICallManagem /** * Preference for suppressing recording of duplicate calls. + * @deprecated duplicate calls are always recorded and are deleted after the call database duplicate and Do Not + * Monitor retention time (#2742). Kept so that the stored preference value remains readable. */ + @Deprecated public boolean isDuplicateRecordingSuppressionEnabled() { if(mDuplicateRecordingSuppressionEnabled == null) @@ -156,7 +159,9 @@ public class CallManagementPreference extends Preference implements ICallManagem /** * Sets the preference for suppressing recording of duplicate calls. + * @deprecated see {@link #isDuplicateRecordingSuppressionEnabled()} */ + @Deprecated public void setDuplicateRecordingSuppressionEnabled(boolean enabled) { mPreferences.putBoolean(PREFERENCE_KEY_SUPPRESS_DUPLICATE_RECORDING, enabled); diff --git a/src/main/java/io/github/dsheirer/preference/playback/PlaybackPreference.java b/src/main/java/io/github/dsheirer/preference/playback/PlaybackPreference.java index 714655c5..42bdcfd6 100644 --- a/src/main/java/io/github/dsheirer/preference/playback/PlaybackPreference.java +++ b/src/main/java/io/github/dsheirer/preference/playback/PlaybackPreference.java @@ -46,6 +46,7 @@ public class PlaybackPreference extends Preference private static final String PREFERENCE_KEY_AUDIO_DEVICE_NAME = "audio.playback.device.name"; private static final String PREFERENCE_KEY_AUDIO_CHANNEL_COUNT = "audio.playback.channel.count"; + private static final String PREFERENCE_KEY_HOLD_AUTO_RELEASE_MINUTES = "audio.playback.hold.auto.release.minutes"; public static final int TONE_LENGTH_SAMPLES = 160; private final static Logger mLog = LoggerFactory.getLogger(PlaybackPreference.class); @@ -296,4 +297,22 @@ public class PlaybackPreference extends Preference notifyPreferenceUpdated(); } } + + /** + * Minutes without a call to a hold talkgroup before the hold is released automatically, or 0 to never release + * a hold automatically. + */ + public int getHoldAutoReleaseMinutes() + { + return Math.max(0, mPreferences.getInt(PREFERENCE_KEY_HOLD_AUTO_RELEASE_MINUTES, 0)); + } + + /** + * Sets the hold talkgroup automatic release time in minutes (0 = never). + */ + public void setHoldAutoReleaseMinutes(int minutes) + { + mPreferences.putInt(PREFERENCE_KEY_HOLD_AUTO_RELEASE_MINUTES, Math.max(0, minutes)); + notifyPreferenceUpdated(); + } } diff --git a/src/main/java/io/github/dsheirer/preference/record/RecordPreference.java b/src/main/java/io/github/dsheirer/preference/record/RecordPreference.java index f813ecc0..cf5f9c07 100644 --- a/src/main/java/io/github/dsheirer/preference/record/RecordPreference.java +++ b/src/main/java/io/github/dsheirer/preference/record/RecordPreference.java @@ -37,6 +37,7 @@ public class RecordPreference extends Preference private static final String PREFERENCE_KEY_AUDIO_RECORD_FORMAT = "audio.record.format"; private static final RecordFormat DEFAULT_RECORD_FORMAT = RecordFormat.MP3; private static final String PREFERENCE_KEY_BASEBAND_RECORD_FORMAT = "baseband.record.format"; + private static final String PREFERENCE_KEY_RECORD_ALL_CALLS = "audio.record.all.calls"; private static final BasebandRecordFormat DEFAULT_BASEBAND_RECORD_FORMAT = BasebandRecordFormat.WAVE; private final static Logger mLog = LoggerFactory.getLogger(RecordPreference.class); private Preferences mPreferences = Preferences.userNodeForPackage(RecordPreference.class); @@ -131,4 +132,22 @@ public class RecordPreference extends Preference mPreferences.put(PREFERENCE_KEY_BASEBAND_RECORD_FORMAT, basebandRecordFormat.name()); notifyPreferenceUpdated(); } + + /** + * Indicates if the audio of every call is recorded. When false, only calls with an alias that has the record + * option are recorded. Default: true. + */ + public boolean isRecordAllCalls() + { + return mPreferences.getBoolean(PREFERENCE_KEY_RECORD_ALL_CALLS, true); + } + + /** + * Sets whether the audio of every call is recorded + */ + public void setRecordAllCalls(boolean recordAllCalls) + { + mPreferences.putBoolean(PREFERENCE_KEY_RECORD_ALL_CALLS, recordAllCalls); + notifyPreferenceUpdated(); + } } diff --git a/src/main/java/io/github/dsheirer/record/AudioRecordingManager.java b/src/main/java/io/github/dsheirer/record/CallRecorder.java similarity index 51% rename from src/main/java/io/github/dsheirer/record/AudioRecordingManager.java rename to src/main/java/io/github/dsheirer/record/CallRecorder.java index cd19a7d7..b5a89694 100644 --- a/src/main/java/io/github/dsheirer/record/AudioRecordingManager.java +++ b/src/main/java/io/github/dsheirer/record/CallRecorder.java @@ -1,6 +1,6 @@ /* * ***************************************************************************** - * Copyright (C) 2014-2024 Dennis Sheirer + * Copyright (C) 2014-2026 Dennis Sheirer * * 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 @@ -16,10 +16,11 @@ * along with this program. If not, see * **************************************************************************** */ - package io.github.dsheirer.record; import io.github.dsheirer.audio.AudioSegment; +import io.github.dsheirer.database.call.CallRecord; +import io.github.dsheirer.database.call.RecordingState; import io.github.dsheirer.identifier.Form; import io.github.dsheirer.identifier.Identifier; import io.github.dsheirer.identifier.IdentifierClass; @@ -30,150 +31,243 @@ import io.github.dsheirer.identifier.tone.Tone; import io.github.dsheirer.identifier.tone.ToneIdentifier; import io.github.dsheirer.identifier.tone.ToneSequence; import io.github.dsheirer.preference.UserPreferences; -import io.github.dsheirer.sample.Listener; +import io.github.dsheirer.record.stream.IStreamingAudioWriter; +import io.github.dsheirer.record.stream.StreamingMP3Writer; +import io.github.dsheirer.record.stream.StreamingWaveWriter; +import io.github.dsheirer.record.wave.AudioMetadataUtils; import io.github.dsheirer.util.StringUtils; -import io.github.dsheirer.util.ThreadPool; import io.github.dsheirer.util.TimeStamp; -import java.io.IOException; -import java.nio.file.Path; -import java.util.List; -import java.util.concurrent.LinkedTransferQueue; -import java.util.concurrent.ScheduledFuture; -import java.util.concurrent.TimeUnit; -import javafx.beans.value.ChangeListener; -import javafx.beans.value.ObservableValue; import org.slf4j.Logger; import org.slf4j.LoggerFactory; +import java.io.IOException; +import java.nio.file.Files; +import java.nio.file.Path; +import java.util.ArrayList; +import java.util.List; + /** - * Monitors audio segments and upon completion records any audio segments that have been flagged as recordable + * Records call audio to MP3 or WAVE files while the call is in progress (streaming). + * + * Every call is recorded when the record all calls preference is enabled (default), otherwise only calls with an + * alias that has the record option. The recording file exists from the start of the call, and its path is stored + * with the call. The file name is fixed when the recording starts, so it contains the identifiers known at that time. + * The recording starts when the call has a TO identifier, or after one second of audio without one. The final + * metadata (ID3/LIST tags) is written when the call completes. + * + * The call manager creates one {@link Recording} per call and drives it from its call processing thread. This class + * isn't thread safe: all methods must be invoked on that thread. */ -public class AudioRecordingManager implements Listener +public class CallRecorder { - private final static Logger mLog = LoggerFactory.getLogger(AudioRecordingManager.class); - private LinkedTransferQueue mCompletedAudioSegmentQueue = new LinkedTransferQueue<>(); - private ScheduledFuture mQueueProcessorHandle; - private UserPreferences mUserPreferences; + private final static Logger mLog = LoggerFactory.getLogger(CallRecorder.class); + private static final long MAX_WAIT_FOR_TO_IDENTIFIER_MS = 1000; + private final UserPreferences mUserPreferences; private int mUnknownAudioRecordingIndex = 1; private int mDuplicateAudioRecordingSuffix = 1; private String mPreviousRecordingPath = null; /** * Constructs an instance - * @param userPreferences to determine audio recording format + * @param userPreferences for the recording format, record all calls and the recordings directory */ - public AudioRecordingManager(UserPreferences userPreferences) + public CallRecorder(UserPreferences userPreferences) { mUserPreferences = userPreferences; } /** - * Starts the manager and begins audio segment recording. + * Creates the recording for a call. The recording starts when the call is recordable. */ - public void start() + public Recording createRecording(AudioSegment audioSegment) { - if(mQueueProcessorHandle == null) - { - mQueueProcessorHandle = ThreadPool.SCHEDULED.scheduleAtFixedRate(new QueueProcessor(), - 0, 1, TimeUnit.SECONDS); - } - } - - /** - * Stops the manager and records any remaining queued audio segments. - */ - public void stop() - { - if(mQueueProcessorHandle != null) - { - mQueueProcessorHandle.cancel(true); - processAudioSegments(); - mQueueProcessorHandle = null; - } - } - - /** - * Primary receive method for incoming audio segments to be recorded - */ - @Override - public void receive(AudioSegment audioSegment) - { - audioSegment.completeProperty().addListener(new AudioSegmentCompletionMonitor(audioSegment)); - } - - /** - * Processes audio segments that have been flagged as complete. - * @param audioSegment - */ - public void processCompletedAudioSegment(AudioSegment audioSegment) - { - //Debug - if(audioSegment.getAudioBufferCount() == 0) - { -// mLog.debug("Audio Segment detected with 0 audio buffers"); - } - - List toIdentifiers = audioSegment.getIdentifierCollection().getIdentifiers(Role.TO); - - if(toIdentifiers.isEmpty()) - { -// mLog.debug("Audio Segment detected with NO TO identifiers"); - } - - if(audioSegment.recordAudioProperty().get()) - { - mCompletedAudioSegmentQueue.add(audioSegment); - } - else - { - audioSegment.decrementConsumerCount(); - } - } - - /** - * Processes any queued audio segments - */ - private void processAudioSegments() - { - RecordFormat recordFormat = mUserPreferences.getRecordPreference().getAudioRecordFormat(); - AudioSegment audioSegment = mCompletedAudioSegmentQueue.poll(); - - while(audioSegment != null) - { - if(audioSegment.isDuplicate() && mUserPreferences.getCallManagementPreference().isDuplicateRecordingSuppressionEnabled()) - { - audioSegment.decrementConsumerCount(); - } - else - { - Path path = getAudioRecordingPath(audioSegment.getIdentifierCollection(), recordFormat); - - try - { - AudioSegmentRecorder.record(audioSegment, path, recordFormat, mUserPreferences); - } - catch(IOException ioe) - { - mLog.error("Error recording audio segment to [" + path.toString() + "]"); - } - - audioSegment.decrementConsumerCount(); - } - - //Grab the next one to record - audioSegment = mCompletedAudioSegmentQueue.poll(); - } + return new Recording(audioSegment); } /** * Base path to recordings folder - * @return */ public Path getRecordingBasePath() { return mUserPreferences.getDirectoryPreference().getDirectoryRecording(); } + /** + * Indicates if the call is recorded: all calls when record all calls is enabled, otherwise only calls with an + * alias that has the record option. Duplicate and Do Not Monitor calls are recorded and later deleted by the + * call database retention rule. + */ + public boolean isRecordable(AudioSegment audioSegment) + { + return mUserPreferences.getRecordPreference().isRecordAllCalls() || audioSegment.recordAudioProperty().get(); + } + + /** + * Creates a unique recording file path for the identifiers. + */ + private Path getUniqueRecordingPath(IdentifierCollection identifierCollection, RecordFormat recordFormat) + { + Path path = getAudioRecordingPath(identifierCollection, recordFormat); + + if(!Files.exists(path)) + { + return path; + } + + String name = path.getFileName().toString(); + String base = name.substring(0, name.length() - recordFormat.getExtension().length()); + + for(int version = 2; version < 1000; version++) + { + Path candidate = path.resolveSibling(base + "_V" + version + recordFormat.getExtension()); + + if(!Files.exists(candidate)) + { + return candidate; + } + } + + return path.resolveSibling(base + "_" + System.nanoTime() + recordFormat.getExtension()); + } + + /** + * Opens a streaming writer for the recording format. + */ + private IStreamingAudioWriter createWriter(Path path, RecordFormat recordFormat, AudioSegment audioSegment) + throws IOException + { + return switch(recordFormat) + { + case MP3 -> new StreamingMP3Writer(path, mUserPreferences.getMP3Preference().getAudioSampleRate(), + mUserPreferences.getMP3Preference().getMP3Setting(), + mUserPreferences.getMP3Preference().isNormalizeAudioBeforeEncode(), + AudioMetadataUtils.getMetadataMap(audioSegment.getIdentifierCollection(), audioSegment.getAliasList())); + case WAVE -> new StreamingWaveWriter(path); + }; + } + + /** + * Recording of one call. It updates the recording fields of the call record (format, file path, file size and + * recording state). + */ + public class Recording + { + private final AudioSegment mAudioSegment; + private IStreamingAudioWriter mWriter; + private int mBuffersWritten; + private boolean mRecordingAttempted; + + private Recording(AudioSegment audioSegment) + { + mAudioSegment = audioSegment; + } + + /** + * Starts the recording when the call becomes recordable, and writes the audio received since the last update. + * @param call record to update with the recording details + */ + public void update(CallRecord call) + { + if(mWriter != null) + { + writeAudio(call); + return; + } + + if(mRecordingAttempted || !isRecordable(mAudioSegment) || !mAudioSegment.hasAudio()) + { + return; + } + + //Give the decoder a moment to provide the TO identifier, since it is part of the file name + if(mAudioSegment.getIdentifierCollection().getToIdentifier() == null && + mAudioSegment.getDuration() < MAX_WAIT_FOR_TO_IDENTIFIER_MS && !mAudioSegment.isComplete()) + { + return; + } + + mRecordingAttempted = true; + RecordFormat recordFormat = mUserPreferences.getRecordPreference().getAudioRecordFormat(); + Path path = getUniqueRecordingPath(mAudioSegment.getIdentifierCollection(), recordFormat); + + try + { + mWriter = createWriter(path, recordFormat, mAudioSegment); + call.setRecordFormat(recordFormat.name()); + call.setFilePath(path.toAbsolutePath().toString()); + call.setRecordingState(RecordingState.RECORDING); + writeAudio(call); + } + catch(IOException ioe) + { + mLog.error("Error creating audio recording [" + path + "] - " + ioe.getMessage()); + mWriter = null; + call.setRecordingState(RecordingState.FAILED); + } + } + + /** + * Writes the audio buffers that haven't been written yet. + */ + private void writeAudio(CallRecord call) + { + int count = mAudioSegment.getAudioBufferCount(); + + if(mWriter != null && mBuffersWritten < count) + { + List buffers = new ArrayList<>(mAudioSegment.getAudioBuffers().subList(mBuffersWritten, count)); + + try + { + mWriter.write(buffers); + mBuffersWritten = count; + } + catch(IOException ioe) + { + mLog.error("Error writing audio recording [" + mWriter.getPath() + "] - " + ioe.getMessage()); + mWriter.abort(); + mWriter = null; + call.setRecordingState(RecordingState.FAILED); + } + } + } + + /** + * Writes the remaining audio and the final metadata, and closes the recording. Invoke when the call + * completes. + * @param call record to update with the final recording details + */ + public void complete(CallRecord call) + { + update(call); + + if(mWriter != null) + { + writeAudio(call); + + if(mWriter != null) + { + Path path = mWriter.getPath(); + + try + { + mWriter.close(AudioMetadataUtils.getMetadataMap(mAudioSegment.getIdentifierCollection(), + mAudioSegment.getAliasList())); + call.setFileSize(Files.size(path)); + call.setRecordingState(RecordingState.RECORDED); + } + catch(IOException ioe) + { + mLog.error("Error completing audio recording [" + path + "] - " + ioe.getMessage()); + call.setRecordingState(Files.exists(path) ? RecordingState.RECORDED : RecordingState.FAILED); + } + + mWriter = null; + } + } + } + } + /** * Provides a formatted audio recording filename to use as the final audio filename. */ @@ -323,22 +417,6 @@ public class AudioRecordingManager implements Listener * Audio segment completion monitor. Listens for the audio segment's complete flag to be set and then * queues the audio segment for recording. */ - public class AudioSegmentCompletionMonitor implements ChangeListener - { - private AudioSegment mAudioSegment; - - public AudioSegmentCompletionMonitor(AudioSegment audioSegment) - { - mAudioSegment = audioSegment; - } - - @Override - public void changed(ObservableValue observable, Boolean oldValue, Boolean newValue) - { - mAudioSegment.completeProperty().removeListener(this); - processCompletedAudioSegment(mAudioSegment); - } - } public static String clean(String value) { @@ -354,23 +432,4 @@ public class AudioRecordingManager implements Listener return null; } - - /** - * Threaded queue processor to process/record each recordable audio segment - */ - public class QueueProcessor implements Runnable - { - @Override - public void run() - { - try - { - processAudioSegments(); - } - catch(Throwable t) - { - mLog.error("Error while processing queued audio segments to recordings", t); - } - } - } } diff --git a/src/main/java/io/github/dsheirer/record/RecordingReader.java b/src/main/java/io/github/dsheirer/record/RecordingReader.java new file mode 100644 index 00000000..86c12568 --- /dev/null +++ b/src/main/java/io/github/dsheirer/record/RecordingReader.java @@ -0,0 +1,200 @@ +/* + * ***************************************************************************** + * Copyright (C) 2014-2026 Dennis Sheirer + * + * 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 + * the Free Software Foundation, either version 3 of the License, or + * (at your option) any later version. + * + * This program is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU General Public License for more details. + * + * You should have received a copy of the GNU General Public License + * along with this program. If not, see + * **************************************************************************** + */ +package io.github.dsheirer.record; + +import io.github.dsheirer.dsp.filter.resample.RealResampler; +import net.sourceforge.lame.lowlevel.LameDecoder; + +import javax.sound.sampled.AudioFormat; +import javax.sound.sampled.AudioInputStream; +import javax.sound.sampled.AudioSystem; +import javax.sound.sampled.UnsupportedAudioFileException; +import java.io.ByteArrayOutputStream; +import java.io.IOException; +import java.nio.ByteBuffer; +import java.nio.ByteOrder; +import java.nio.file.Path; +import java.util.ArrayList; +import java.util.List; +import java.util.Locale; + +/** + * Reads a call audio recording (MP3 or WAVE) as 8 kHz mono PCM samples for replay through the audio channels. + */ +public class RecordingReader +{ + public static final int SAMPLE_RATE = 8000; + + private RecordingReader() + { + } + + /** + * Reads the recording. + * @param path of an .mp3 or .wav recording + * @return 8 kHz mono samples in the range -1.0 to 1.0 + * @throws IOException if the file can't be read or decoded + */ + public static float[] read(Path path) throws IOException + { + String name = path.getFileName().toString().toLowerCase(Locale.ROOT); + + if(name.endsWith(".mp3")) + { + return readMP3(path); + } + + if(name.endsWith(".wav")) + { + return readWave(path); + } + + throw new IOException("Unsupported recording format: " + path.getFileName()); + } + + private static float[] readMP3(Path path) throws IOException + { + LameDecoder decoder; + + try + { + decoder = new LameDecoder(path.toAbsolutePath().toString()); + } + catch(Exception e) + { + throw new IOException("Unable to open MP3 recording " + path.getFileName() + ": " + e.getMessage(), e); + } + + try + { + int channels = Math.max(1, decoder.getChannels()); + + //The decoder reports 1152 samples per frame, but MPEG-2/2.5 frames (sample rates below 32 kHz, e.g. the + //default 16 kHz recordings) contain 576 samples, and the decoder doesn't report how many bytes it wrote. + int samplesPerFrame = decoder.getSampleRate() >= 32000 ? 1152 : 576; + int frameBytes = samplesPerFrame * channels * 2; + ByteBuffer buffer = ByteBuffer.allocate(Math.max(frameBytes, decoder.getFrameSize() * channels * 2)); + ByteArrayOutputStream pcm = new ByteArrayOutputStream(); + + while(decoder.decode(buffer)) + { + pcm.write(buffer.array(), 0, frameBytes); + buffer.clear(); + } + + float[] samples = toFloat(pcm.toByteArray(), channels, ByteOrder.LITTLE_ENDIAN); + return resample(samples, decoder.getSampleRate()); + } + catch(RuntimeException re) + { + throw new IOException("Unable to decode MP3 recording " + path.getFileName() + ": " + re.getMessage(), re); + } + finally + { + decoder.close(); + } + } + + private static float[] readWave(Path path) throws IOException + { + try(AudioInputStream source = AudioSystem.getAudioInputStream(path.toFile())) + { + AudioFormat format = source.getFormat(); + AudioFormat pcmFormat = new AudioFormat(AudioFormat.Encoding.PCM_SIGNED, format.getSampleRate(), 16, + format.getChannels(), format.getChannels() * 2, format.getSampleRate(), false); + + try(AudioInputStream pcm = AudioSystem.getAudioInputStream(pcmFormat, source)) + { + float[] samples = toFloat(pcm.readAllBytes(), format.getChannels(), ByteOrder.LITTLE_ENDIAN); + return resample(samples, Math.round(format.getSampleRate())); + } + } + catch(UnsupportedAudioFileException e) + { + throw new IOException("Unsupported WAVE recording " + path.getFileName() + ": " + e.getMessage(), e); + } + } + + /** + * Converts 16-bit PCM to mono float samples, averaging the channels. + */ + static float[] toFloat(byte[] pcm, int channels, ByteOrder order) + { + ByteBuffer buffer = ByteBuffer.wrap(pcm).order(order); + int frames = pcm.length / (2 * channels); + float[] samples = new float[frames]; + + for(int x = 0; x < frames; x++) + { + float sum = 0; + + for(int c = 0; c < channels; c++) + { + sum += buffer.getShort() / 32768.0f; + } + + samples[x] = sum / channels; + } + + return samples; + } + + /** + * Resamples to 8 kHz. + */ + static float[] resample(float[] samples, int sampleRate) + { + if(sampleRate == SAMPLE_RATE || samples.length == 0) + { + return samples; + } + + RealResampler resampler = new RealResampler(sampleRate, SAMPLE_RATE, 4096, 512); + List output = new ArrayList<>(); + resampler.setListener(output::add); + int chunk = 4096; + + for(int offset = 0; offset < samples.length; offset += chunk) + { + float[] piece = new float[Math.min(chunk, samples.length - offset)]; + System.arraycopy(samples, offset, piece, 0, piece.length); + resampler.resample(piece); + } + + resampler.flush(); + + int length = 0; + + for(float[] buffer: output) + { + length += buffer.length; + } + + float[] resampled = new float[length]; + int position = 0; + + for(float[] buffer: output) + { + System.arraycopy(buffer, 0, resampled, position, buffer.length); + position += buffer.length; + } + + return resampled; + } +} diff --git a/src/main/java/io/github/dsheirer/record/stream/IStreamingAudioWriter.java b/src/main/java/io/github/dsheirer/record/stream/IStreamingAudioWriter.java new file mode 100644 index 00000000..4064737d --- /dev/null +++ b/src/main/java/io/github/dsheirer/record/stream/IStreamingAudioWriter.java @@ -0,0 +1,54 @@ +/* + * ***************************************************************************** + * Copyright (C) 2014-2026 Dennis Sheirer + * + * 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 + * the Free Software Foundation, either version 3 of the License, or + * (at your option) any later version. + * + * This program is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU General Public License for more details. + * + * You should have received a copy of the GNU General Public License + * along with this program. If not, see + * **************************************************************************** + */ +package io.github.dsheirer.record.stream; + +import io.github.dsheirer.record.wave.AudioMetadata; + +import java.io.IOException; +import java.nio.file.Path; +import java.util.List; +import java.util.Map; + +/** + * Writes a call audio recording incrementally while the call is in progress, so that the recording file exists from + * the start of the call. The metadata (e.g. ID3 tags) is written when the recording is opened and rewritten with the + * final call metadata when the recording is closed. + */ +public interface IStreamingAudioWriter +{ + /** + * Writes the next chunk of 8 kHz PCM audio. The buffers are not modified. + */ + void write(List audioBuffers) throws IOException; + + /** + * Finishes the recording with the final call metadata and closes the file. + */ + void close(Map metadata) throws IOException; + + /** + * Closes the file without finishing it, for example when the recording will be discarded. + */ + void abort(); + + /** + * Recording file + */ + Path getPath(); +} diff --git a/src/main/java/io/github/dsheirer/record/stream/StreamingMP3Writer.java b/src/main/java/io/github/dsheirer/record/stream/StreamingMP3Writer.java new file mode 100644 index 00000000..617fab58 --- /dev/null +++ b/src/main/java/io/github/dsheirer/record/stream/StreamingMP3Writer.java @@ -0,0 +1,254 @@ +/* + * ***************************************************************************** + * Copyright (C) 2014-2026 Dennis Sheirer + * + * 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 + * the Free Software Foundation, either version 3 of the License, or + * (at your option) any later version. + * + * This program is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU General Public License for more details. + * + * You should have received a copy of the GNU General Public License + * along with this program. If not, see + * **************************************************************************** + */ +package io.github.dsheirer.record.stream; + +import io.github.dsheirer.audio.convert.InputAudioFormat; +import io.github.dsheirer.audio.convert.MP3AudioConverter; +import io.github.dsheirer.audio.convert.MP3Setting; +import io.github.dsheirer.record.wave.AudioMetadata; +import io.github.dsheirer.record.wave.AudioMetadataUtils; +import org.slf4j.Logger; +import org.slf4j.LoggerFactory; + +import java.io.IOException; +import java.nio.ByteBuffer; +import java.nio.channels.FileChannel; +import java.nio.file.Files; +import java.nio.file.Path; +import java.nio.file.StandardCopyOption; +import java.nio.file.StandardOpenOption; +import java.util.ArrayList; +import java.util.Arrays; +import java.util.List; +import java.util.Map; + +/** + * Streaming MP3 recording. + * + * The ID3v2 tag has to be at the start of the file, but the final call metadata is only known when the call ends. The + * writer reserves space for the tag by writing the initial tag padded to a fixed size, and overwrites it in place with + * the final tag when the recording closes. If the final tag doesn't fit (very rare), the file is rewritten. + * + * Audio normalization: the whole-call normalization of the non-streaming recorder can't be used while the call is in + * progress. Instead the gain is derived from the peak level seen so far in the call, so the gain can only decrease + * during the call and the audio never clips. The same 0.95 target and -20 dBFS gain limit are used. + */ +public class StreamingMP3Writer implements IStreamingAudioWriter +{ + private static final Logger LOGGER = LoggerFactory.getLogger(StreamingMP3Writer.class); + static final int RESERVED_TAG_SIZE = 4096; + private static final int ID3_HEADER_SIZE = 10; + private static final float NORMALIZE_TARGET = 0.95f; + private static final float NORMALIZE_MINIMUM_PEAK = 0.1f; + + private final Path mPath; + private final FileChannel mFileChannel; + private final MP3AudioConverter mConverter; + private final boolean mNormalize; + private float mPeak = NORMALIZE_MINIMUM_PEAK; + + /** + * Creates the recording file and writes the initial (padded) ID3 tag. + * @param path for the new file, which must not exist + * @param inputAudioFormat for the encoder + * @param mp3Setting for the encoder + * @param normalize to apply peak-tracking audio gain normalization + * @param initialMetadata known at the start of the call + */ + public StreamingMP3Writer(Path path, InputAudioFormat inputAudioFormat, MP3Setting mp3Setting, boolean normalize, + Map initialMetadata) throws IOException + { + mPath = path; + mNormalize = normalize; + mConverter = new MP3AudioConverter(inputAudioFormat, mp3Setting, false); + mFileChannel = FileChannel.open(path, StandardOpenOption.WRITE, StandardOpenOption.CREATE_NEW); + + byte[] tag = getPaddedTag(AudioMetadataUtils.getMP3ID3(initialMetadata), RESERVED_TAG_SIZE); + + if(tag == null) + { + //Initial tag is too large to pad - reserve an empty tag and rely on the rewrite at close + tag = getPaddedTag(AudioMetadataUtils.getMP3ID3(Map.of()), RESERVED_TAG_SIZE); + } + + writeFully(ByteBuffer.wrap(tag)); + } + + @Override + public void write(List audioBuffers) throws IOException + { + List buffers = mNormalize ? normalize(audioBuffers) : audioBuffers; + writeFrames(mConverter.encodeStream(buffers)); + } + + /** + * Applies the peak-tracking normalization gain to copies of the buffers. + */ + List normalize(List audioBuffers) + { + List normalized = new ArrayList<>(audioBuffers.size()); + + for(float[] audioBuffer: audioBuffers) + { + for(float sample: audioBuffer) + { + float magnitude = Math.abs(sample); + + if(magnitude > mPeak && !Float.isNaN(magnitude)) + { + mPeak = magnitude; + } + } + + float gain = NORMALIZE_TARGET / mPeak; + float[] copy = Arrays.copyOf(audioBuffer, audioBuffer.length); + + for(int x = 0; x < copy.length; x++) + { + copy[x] *= gain; + } + + normalized.add(copy); + } + + return normalized; + } + + private void writeFrames(List frames) throws IOException + { + for(byte[] frame: frames) + { + writeFully(ByteBuffer.wrap(frame)); + } + } + + private void writeFully(ByteBuffer buffer) throws IOException + { + while(buffer.hasRemaining()) + { + mFileChannel.write(buffer); + } + } + + @Override + public void close(Map metadata) throws IOException + { + try + { + writeFrames(mConverter.finishStream()); + + byte[] tag = getPaddedTag(AudioMetadataUtils.getMP3ID3(metadata), RESERVED_TAG_SIZE); + + if(tag != null) + { + mFileChannel.write(ByteBuffer.wrap(tag), 0); + mFileChannel.force(true); + mFileChannel.close(); + } + else + { + mFileChannel.force(true); + mFileChannel.close(); + rewriteWithTag(AudioMetadataUtils.getMP3ID3(metadata)); + } + } + finally + { + if(mFileChannel.isOpen()) + { + mFileChannel.close(); + } + } + } + + /** + * Rewrites the file with the (unpadded) tag replacing the reserved tag space. + */ + private void rewriteWithTag(byte[] tag) throws IOException + { + Path temp = mPath.resolveSibling(mPath.getFileName() + ".tmp"); + + try(FileChannel source = FileChannel.open(mPath, StandardOpenOption.READ); + FileChannel target = FileChannel.open(temp, StandardOpenOption.WRITE, StandardOpenOption.CREATE, + StandardOpenOption.TRUNCATE_EXISTING)) + { + ByteBuffer buffer = ByteBuffer.wrap(tag); + + while(buffer.hasRemaining()) + { + target.write(buffer); + } + + long position = RESERVED_TAG_SIZE; + long size = source.size(); + + while(position < size) + { + position += source.transferTo(position, size - position, target); + } + } + + Files.move(temp, mPath, StandardCopyOption.REPLACE_EXISTING); + } + + @Override + public void abort() + { + try + { + mFileChannel.close(); + } + catch(IOException ioe) + { + LOGGER.warn("Error closing aborted recording [" + mPath + "] - " + ioe.getMessage()); + } + } + + @Override + public Path getPath() + { + return mPath; + } + + /** + * Pads an ID3v2 tag with zero bytes to the requested total size and updates the tag size in the header. ID3v2 + * readers treat the zero bytes after the last frame as padding. + * @param tag complete ID3v2 tag (header and frames, no footer) + * @param size total size for the padded tag + * @return padded tag, or null if the tag is larger than the requested size or isn't a valid ID3v2 tag. + */ + static byte[] getPaddedTag(byte[] tag, int size) + { + if(tag == null || tag.length < ID3_HEADER_SIZE || tag.length > size || tag[0] != 'I' || tag[1] != 'D' || + tag[2] != '3' || (tag[5] & 0x10) != 0) + { + return null; + } + + byte[] padded = Arrays.copyOf(tag, size); + int tagSize = size - ID3_HEADER_SIZE; + + //Synchsafe integer: 4 bytes of 7 bits each + padded[6] = (byte)((tagSize >> 21) & 0x7F); + padded[7] = (byte)((tagSize >> 14) & 0x7F); + padded[8] = (byte)((tagSize >> 7) & 0x7F); + padded[9] = (byte)(tagSize & 0x7F); + return padded; + } +} diff --git a/src/main/java/io/github/dsheirer/record/stream/StreamingWaveWriter.java b/src/main/java/io/github/dsheirer/record/stream/StreamingWaveWriter.java new file mode 100644 index 00000000..52282f0b --- /dev/null +++ b/src/main/java/io/github/dsheirer/record/stream/StreamingWaveWriter.java @@ -0,0 +1,92 @@ +/* + * ***************************************************************************** + * Copyright (C) 2014-2026 Dennis Sheirer + * + * 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 + * the Free Software Foundation, either version 3 of the License, or + * (at your option) any later version. + * + * This program is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU General Public License for more details. + * + * You should have received a copy of the GNU General Public License + * along with this program. If not, see + * **************************************************************************** + */ +package io.github.dsheirer.record.stream; + +import io.github.dsheirer.audio.AudioFormats; +import io.github.dsheirer.record.wave.AudioMetadata; +import io.github.dsheirer.record.wave.AudioMetadataUtils; +import io.github.dsheirer.record.wave.WaveWriter; +import io.github.dsheirer.sample.ConversionUtils; +import org.slf4j.Logger; +import org.slf4j.LoggerFactory; + +import java.io.IOException; +import java.nio.ByteBuffer; +import java.nio.file.Path; +import java.util.List; +import java.util.Map; + +/** + * Streaming WAVE recording. The RIFF and data chunk sizes are updated after each write, so the file is a valid WAVE + * file throughout the call. The LIST and ID3 metadata chunks are appended when the recording is closed. + */ +public class StreamingWaveWriter implements IStreamingAudioWriter +{ + private static final Logger LOGGER = LoggerFactory.getLogger(StreamingWaveWriter.class); + private final WaveWriter mWaveWriter; + private final Path mPath; + + /** + * Creates the recording file. + * @param path for the new file, which must not exist + */ + public StreamingWaveWriter(Path path) throws IOException + { + mPath = path; + mWaveWriter = new WaveWriter(AudioFormats.PCM_SIGNED_8000_HZ_16_BIT_MONO, path); + } + + @Override + public void write(List audioBuffers) throws IOException + { + for(float[] audioBuffer: audioBuffers) + { + mWaveWriter.writeData(ConversionUtils.convertToSigned16BitSamples(audioBuffer)); + } + } + + @Override + public void close(Map metadata) throws IOException + { + ByteBuffer listChunk = AudioMetadataUtils.getLISTChunk(metadata); + byte[] id3Bytes = AudioMetadataUtils.getMP3ID3(metadata); + ByteBuffer id3Chunk = AudioMetadataUtils.getID3Chunk(id3Bytes); + mWaveWriter.writeMetadata(listChunk, id3Chunk); + mWaveWriter.close(); + } + + @Override + public void abort() + { + try + { + mWaveWriter.close(); + } + catch(IOException ioe) + { + LOGGER.warn("Error closing aborted recording [" + mPath + "] - " + ioe.getMessage()); + } + } + + @Override + public Path getPath() + { + return mPath; + } +} diff --git a/src/main/resources/audio_panel.css b/src/main/resources/audio_panel.css index bcc17de3..cfad1a20 100644 --- a/src/main/resources/audio_panel.css +++ b/src/main/resources/audio_panel.css @@ -33,6 +33,7 @@ -audio-text-dim: #5f6b78; -audio-live: #3ddc84; -audio-muted: #ff5c5c; + -audio-hold: #f0b429; -audio-accent: #3ddc84; -fx-background-color: -audio-display; @@ -97,6 +98,29 @@ -fx-fill: -audio-muted; } +.audio-view .audio-hold-button:selected +{ + -fx-background-color: derive(-audio-hold, -60%); + -fx-border-color: -audio-hold; +} + +.audio-view .audio-hold-button:selected .audio-button-icon +{ + -fx-fill: -audio-hold; +} + +.audio-view .audio-hold-label +{ + -fx-text-fill: -audio-hold; + -fx-font-size: 0.8em; + -fx-font-weight: bold; +} + +.audio-view .audio-hold-label .text +{ + -fx-fill: -audio-hold; +} + .audio-view .audio-master-mute { -fx-padding: 6 10 6 10; @@ -140,6 +164,13 @@ -fx-border-color: -audio-accent; } +/* A channel that holds a talkgroup keeps the hold color border, playing or not */ +.audio-view .audio-channel:held, +.audio-view .audio-channel:held:active +{ + -fx-border-color: -audio-hold; +} + .audio-view .audio-accent { -fx-background-color: transparent; @@ -195,6 +226,29 @@ -fx-fill: -audio-text-secondary; } +/* Channel tag colors match the Playing column of the Calls tab: left (and mono) blue, right orange */ +.audio-view .audio-channel-tag-left +{ + -fx-background-color: #4a90d9; +} + +.audio-view .audio-channel-tag-right +{ + -fx-background-color: #e8912d; +} + +.audio-view .audio-channel-tag-left, +.audio-view .audio-channel-tag-right +{ + -fx-text-fill: white; +} + +.audio-view .audio-channel-tag-left .text, +.audio-view .audio-channel-tag-right .text +{ + -fx-fill: white; +} + .audio-view .audio-status { -fx-text-fill: -audio-text-dim; diff --git a/src/main/resources/logback.xml b/src/main/resources/logback.xml index d28fe55d..f4247bde 100644 --- a/src/main/resources/logback.xml +++ b/src/main/resources/logback.xml @@ -34,4 +34,9 @@ + + + + \ No newline at end of file diff --git a/src/main/resources/main_window.css b/src/main/resources/main_window.css index 4fadad80..12015545 100644 --- a/src/main/resources/main_window.css +++ b/src/main/resources/main_window.css @@ -45,3 +45,66 @@ { -fx-fill: -fx-accent; } + +/* Calls tab: a TO/FROM search field shows a selected alias (rather than typed text) in bold */ +.calls-view .calls-alias-search:alias-selected +{ + -fx-font-weight: bold; +} + +/* Calls tab: calls that are playing on an audio channel. Left (and mono) is blue and right is orange, matching the + channel tags in the audio panel. The row tint is a translucent overlay so it works with every theme and with the + row selection color. Chip text is set on the label and its .text node for the dark theme. */ +.calls-view +{ + -calls-playing-left: #4a90d9; + -calls-playing-right: #e8912d; +} + +/* The channel color edge is a background layer, not a border: a border takes layout space and shifts the row's + cells out of alignment with the other rows. */ +.calls-view .table-row-cell:playing-left, +.calls-view .table-row-cell:playing-mono +{ + -fx-background-color: -fx-table-cell-border-color, -calls-playing-left, -fx-background, + rgba(74, 144, 217, 0.20); + -fx-background-insets: 0, 0 0 1 0, 0 0 1 4, 0 0 1 4; +} + +.calls-view .table-row-cell:playing-right +{ + -fx-background-color: -fx-table-cell-border-color, -calls-playing-right, -fx-background, + rgba(232, 145, 45, 0.20); + -fx-background-insets: 0, 0 0 1 0, 0 0 1 4, 0 0 1 4; +} + +.calls-view .calls-playing-chip +{ + -fx-background-radius: 4; + -fx-padding: 0 6 0 5; + -fx-graphic-text-gap: 4; + -fx-font-size: 0.8em; + -fx-font-weight: bold; + -fx-text-fill: white; +} + +.calls-view .calls-playing-chip .text +{ + -fx-fill: white; +} + +.calls-view .calls-playing-chip .calls-playing-icon +{ + -fx-fill: white; +} + +.calls-view .calls-playing-left, +.calls-view .calls-playing-mono +{ + -fx-background-color: -calls-playing-left; +} + +.calls-view .calls-playing-right +{ + -fx-background-color: -calls-playing-right; +} diff --git a/src/test/java/io/github/dsheirer/audio/playback/AudioChannelPlaybackListenerTest.java b/src/test/java/io/github/dsheirer/audio/playback/AudioChannelPlaybackListenerTest.java new file mode 100644 index 00000000..7f6fd9db --- /dev/null +++ b/src/test/java/io/github/dsheirer/audio/playback/AudioChannelPlaybackListenerTest.java @@ -0,0 +1,151 @@ +/* + * ***************************************************************************** + * Copyright (C) 2014-2026 Dennis Sheirer + * + * 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 + * the Free Software Foundation, either version 3 of the License, or + * (at your option) any later version. + * + * This program is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU General Public License for more details. + * + * You should have received a copy of the GNU General Public License + * along with this program. If not, see + * **************************************************************************** + */ +package io.github.dsheirer.audio.playback; + +import io.github.dsheirer.alias.AliasList; +import io.github.dsheirer.audio.AudioSegment; +import io.github.dsheirer.preference.UserPreferences; +import org.junit.jupiter.api.AfterEach; +import org.junit.jupiter.api.BeforeEach; +import org.junit.jupiter.api.Test; + +import java.util.ArrayList; +import java.util.List; + +import static org.junit.jupiter.api.Assertions.assertEquals; +import static org.junit.jupiter.api.Assertions.assertNotEquals; +import static org.junit.jupiter.api.Assertions.assertNull; +import static org.junit.jupiter.api.Assertions.assertSame; + +/** + * The audio channel reports the call (audio segment) that it starts and stops playing, which the Calls tab uses to + * show which calls are playing on each channel. + */ +public class AudioChannelPlaybackListenerTest +{ + private static final float[] AUDIO = new float[AudioChannel.SAMPLES_PER_INTERVAL]; + private AudioChannel mChannel; + private final List mEvents = new ArrayList<>(); + + @BeforeEach + public void setup() + { + mChannel = new AudioChannel(new UserPreferences(), "LEFT"); + mChannel.setPlaybackListener(() -> mEvents.add(mChannel.getCurrentAudioSegment())); + } + + @AfterEach + public void teardown() + { + mChannel.dispose(); + } + + private static AudioSegment segment() + { + AudioSegment segment = new AudioSegment(new AliasList("test"), 0); + segment.incrementConsumerCount(); + segment.addAudio(AUDIO); + return segment; + } + + @Test + public void reportsStartAndStopOfEachCall() + { + AudioSegment first = segment(); + AudioSegment second = segment(); + assertNotEquals(first.getId(), second.getId(), "audio segment ids are unique"); + + mChannel.play(first); + mChannel.play(second); + mChannel.getAudio(); + assertSame(first, mChannel.getCurrentAudioSegment()); + assertEquals(List.of(first), mEvents, "first call started"); + + first.completeProperty().set(true); + second.completeProperty().set(true); + + for(int x = 0; x < 10; x++) + { + mChannel.getAudio(); + } + + assertSame(first, mEvents.get(0)); + assertNull(mEvents.get(1), "first call stopped"); + assertSame(second, mEvents.get(2), "second call started"); + assertNull(mEvents.get(3), "second call stopped"); + assertNull(mChannel.getCurrentAudioSegment()); + } + + @Test + public void replayTakesOverChannelAndLiveCallsResumeAfter() + { + AudioSegment live = segment(); + mChannel.play(live); + mChannel.getAudio(); + assertSame(live, mChannel.getCurrentAudioSegment()); + + AudioSegment queued = segment(); + mChannel.play(queued); + + float[] samples = new float[AudioChannel.SAMPLES_PER_INTERVAL * 3]; + java.util.Arrays.fill(samples, 0.25f); + ReplaySource replay = new ReplaySource(samples, 42, "Replay"); + mChannel.startReplay(replay); + assertEquals(true, mChannel.isReplaying()); + + float[] audio = mChannel.getAudio(); + assertEquals(0.25f, audio[0], 0.0001f, "replay audio replaces the live call"); + assertNull(mChannel.getCurrentAudioSegment(), "live call dropped"); + assertSame(replay, mChannel.getReplay()); + + replay.setPaused(true); + assertEquals(0.0f, mChannel.getAudio()[0], 0.0001f, "paused replay is silent"); + replay.setPaused(false); + mChannel.getAudio(); + mChannel.getAudio(); + assertEquals(true, replay.isCompleted(), "replay played to the end"); + + //Next interval releases the replay and the queued live call plays + mChannel.getAudio(); + assertNull(mChannel.getReplay()); + assertSame(queued, mChannel.getCurrentAudioSegment(), "queued live call plays after the replay"); + } + + @Test + public void replayingChannelIsSkippedForLiveCalls() + { + AudioChannel right = new AudioChannel(new UserPreferences(), "RIGHT"); + + try + { + mChannel.startReplay(new ReplaySource(new float[8000], 7, "Replay")); + AudioSegment live = segment(); + List waiting = new ArrayList<>(List.of(live)); + + AudioPlaybackManager.assignAudioSegments(waiting, List.of(mChannel, right)); + + assertNull(mChannel.peekQueuedAudioSegment(), "replaying channel isn't assigned live calls"); + assertSame(live, right.peekQueuedAudioSegment()); + } + finally + { + right.dispose(); + } + } +} diff --git a/src/test/java/io/github/dsheirer/audio/playback/TalkgroupHoldTest.java b/src/test/java/io/github/dsheirer/audio/playback/TalkgroupHoldTest.java new file mode 100644 index 00000000..87db8e13 --- /dev/null +++ b/src/test/java/io/github/dsheirer/audio/playback/TalkgroupHoldTest.java @@ -0,0 +1,186 @@ +/* + * ***************************************************************************** + * Copyright (C) 2014-2026 Dennis Sheirer + * + * 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 + * the Free Software Foundation, either version 3 of the License, or + * (at your option) any later version. + * + * This program is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU General Public License for more details. + * + * You should have received a copy of the GNU General Public License + * along with this program. If not, see + * **************************************************************************** + */ +package io.github.dsheirer.audio.playback; + +import io.github.dsheirer.alias.AliasList; +import io.github.dsheirer.audio.AudioSegment; +import io.github.dsheirer.database.call.CallRecord; +import io.github.dsheirer.identifier.patch.PatchGroup; +import io.github.dsheirer.module.decode.dmr.identifier.DMRTalkgroup; +import io.github.dsheirer.module.decode.p25.identifier.patch.APCO25PatchGroup; +import io.github.dsheirer.module.decode.p25.identifier.talkgroup.APCO25Talkgroup; +import io.github.dsheirer.preference.UserPreferences; +import io.github.dsheirer.protocol.Protocol; +import org.junit.jupiter.api.AfterEach; +import org.junit.jupiter.api.BeforeEach; +import org.junit.jupiter.api.Test; + +import java.util.ArrayList; +import java.util.List; + +import static org.junit.jupiter.api.Assertions.assertEquals; +import static org.junit.jupiter.api.Assertions.assertFalse; +import static org.junit.jupiter.api.Assertions.assertNotNull; +import static org.junit.jupiter.api.Assertions.assertNull; +import static org.junit.jupiter.api.Assertions.assertSame; +import static org.junit.jupiter.api.Assertions.assertTrue; + +/** + * Talkgroup hold matching and hold-aware assignment of calls to audio channels. + */ +public class TalkgroupHoldTest +{ + private static final float[] AUDIO = new float[AudioChannel.SAMPLES_PER_INTERVAL]; + private AudioChannel mLeft; + private AudioChannel mRight; + + @BeforeEach + public void setup() + { + UserPreferences userPreferences = new UserPreferences(); + mLeft = new AudioChannel(userPreferences, "LEFT"); + mRight = new AudioChannel(userPreferences, "RIGHT"); + } + + @AfterEach + public void teardown() + { + mLeft.dispose(); + mRight.dispose(); + } + + private static AudioSegment call(int talkgroup) + { + AudioSegment segment = new AudioSegment(new AliasList("test"), 0); + segment.incrementConsumerCount(); + segment.addIdentifier(APCO25Talkgroup.create(talkgroup)); + segment.addAudio(AUDIO); + return segment; + } + + private static TalkgroupHold hold(int talkgroup) + { + return new TalkgroupHold(Protocol.APCO25, talkgroup, String.valueOf(talkgroup)); + } + + @Test + public void matchesTalkgroupAndProtocol() + { + TalkgroupHold hold = hold(100); + assertTrue(hold.matches(call(100))); + assertFalse(hold.matches(call(101))); + + AudioSegment dmr = new AudioSegment(new AliasList("test"), 0); + dmr.addIdentifier(DMRTalkgroup.create(100)); + assertFalse(hold.matches(dmr), "same value on a different protocol"); + + TalkgroupHold phase2 = new TalkgroupHold(Protocol.APCO25_PHASE2, 100, "100"); + assertTrue(phase2.matches(call(100)), "P25 phase 1 and phase 2 share talkgroups"); + } + + @Test + public void matchesPatchGroupAndMembers() + { + PatchGroup patchGroup = new PatchGroup(APCO25Talkgroup.create(500)); + patchGroup.addPatchedTalkgroup(APCO25Talkgroup.create(100)); + AudioSegment segment = new AudioSegment(new AliasList("test"), 0); + segment.addIdentifier(APCO25PatchGroup.create(patchGroup)); + + assertTrue(hold(500).matches(segment), "patch group talkgroup"); + assertTrue(hold(100).matches(segment), "patched member talkgroup"); + assertFalse(hold(101).matches(segment)); + } + + @Test + public void createFromCallRecord() + { + CallRecord call = new CallRecord(); + call.setProtocol(Protocol.APCO25.name()); + call.setToId("100"); + call.setToValue(100L); + call.setToForm(CallRecord.FORM_TALKGROUP); + TalkgroupHold hold = TalkgroupHold.from(call, "Fire"); + assertNotNull(hold); + assertEquals("100 Fire", hold.label()); + assertTrue(hold.matches(call(100))); + + call.setToForm(CallRecord.FORM_RADIO); + assertNull(TalkgroupHold.from(call, null), "private call to a radio isn't a talkgroup"); + } + + @Test + public void heldTalkgroupIsSteeredToHoldingChannel() + { + mRight.setHold(hold(100)); + AudioSegment held = call(100); + List waiting = new ArrayList<>(List.of(held)); + + AudioPlaybackManager.assignAudioSegments(waiting, List.of(mLeft, mRight)); + + assertTrue(waiting.isEmpty()); + assertSame(held, mRight.peekQueuedAudioSegment(), "held call goes to the holding channel"); + assertNull(mLeft.peekQueuedAudioSegment(), "even though the left channel is free"); + } + + @Test + public void otherCallsAreKeptOffHoldingChannel() + { + mLeft.setHold(hold(100)); + AudioSegment first = call(200); + AudioSegment second = call(300); + List waiting = new ArrayList<>(List.of(first, second)); + + AudioPlaybackManager.assignAudioSegments(waiting, List.of(mLeft, mRight)); + + assertNull(mLeft.peekQueuedAudioSegment(), "held channel stays free for the held talkgroup"); + assertSame(first, mRight.peekQueuedAudioSegment()); + assertEquals(List.of(second), waiting, "second call waits for the unheld channel"); + } + + @Test + public void heldChannelPlaysDoNotMonitorCallOfHeldTalkgroupOnly() + { + mLeft.setHold(hold(100)); + AudioSegment held = call(100); + held.monitorPriorityProperty().set(-1); + assertTrue(held.isDoNotMonitor()); + AudioSegment other = call(200); + + mLeft.play(other); + mLeft.play(held); + + //The non-matching call is thrown away and the held do-not-monitor call plays + assertNotNull(mLeft.getAudio()); + assertTrue(mLeft.isPlayingHeldTalkgroup()); + } + + @Test + public void noHoldKeepsExistingAssignment() + { + AudioSegment first = call(200); + AudioSegment second = call(300); + List waiting = new ArrayList<>(List.of(first, second)); + + AudioPlaybackManager.assignAudioSegments(waiting, List.of(mLeft, mRight)); + + assertSame(first, mLeft.peekQueuedAudioSegment()); + assertSame(second, mRight.peekQueuedAudioSegment()); + assertTrue(waiting.isEmpty()); + } +} diff --git a/src/test/java/io/github/dsheirer/call/CallManagerPatchTest.java b/src/test/java/io/github/dsheirer/call/CallManagerPatchTest.java new file mode 100644 index 00000000..ad7bd990 --- /dev/null +++ b/src/test/java/io/github/dsheirer/call/CallManagerPatchTest.java @@ -0,0 +1,49 @@ +/* + * ***************************************************************************** + * Copyright (C) 2014-2026 Dennis Sheirer + * + * 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 + * the Free Software Foundation, either version 3 of the License, or + * (at your option) any later version. + * + * This program is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU General Public License for more details. + * + * You should have received a copy of the GNU General Public License + * along with this program. If not, see + * **************************************************************************** + */ +package io.github.dsheirer.call; + +import io.github.dsheirer.database.call.PatchedTalkgroup; +import io.github.dsheirer.identifier.patch.PatchGroup; +import io.github.dsheirer.module.decode.p25.identifier.patch.APCO25PatchGroup; +import io.github.dsheirer.module.decode.p25.identifier.talkgroup.APCO25FullyQualifiedTalkgroupIdentifier; +import io.github.dsheirer.module.decode.p25.identifier.talkgroup.APCO25Talkgroup; +import org.junit.jupiter.api.Test; + +import java.util.List; + +import static org.junit.jupiter.api.Assertions.assertEquals; + +/** + * The call manager captures the talkgroups patched into a patch group call. + */ +public class CallManagerPatchTest +{ + @Test + public void capturesPatchedTalkgroups() + { + PatchGroup patchGroup = new PatchGroup(APCO25Talkgroup.create(500)); + patchGroup.addPatchedTalkgroup(APCO25Talkgroup.create(1001)); + patchGroup.addPatchedTalkgroup(APCO25FullyQualifiedTalkgroupIdentifier.createTo(1002, 781824, 1, 1002)); + + List patched = CallManager.getPatchedTalkgroups(APCO25PatchGroup.create(patchGroup)); + + assertEquals(List.of(new PatchedTalkgroup(1001, null), new PatchedTalkgroup(1002, "781824.1.1002")), patched); + assertEquals(List.of(), CallManager.getPatchedTalkgroups(APCO25Talkgroup.create(1001)), "not a patch group"); + } +} diff --git a/src/test/java/io/github/dsheirer/database/CallDatabaseTest.java b/src/test/java/io/github/dsheirer/database/CallDatabaseTest.java new file mode 100644 index 00000000..518f7cf5 --- /dev/null +++ b/src/test/java/io/github/dsheirer/database/CallDatabaseTest.java @@ -0,0 +1,453 @@ +/* + * ***************************************************************************** + * Copyright (C) 2014-2026 Dennis Sheirer + * + * 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 + * the Free Software Foundation, either version 3 of the License, or + * (at your option) any later version. + * + * This program is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU General Public License for more details. + * + * You should have received a copy of the GNU General Public License + * along with this program. If not, see + * **************************************************************************** + */ +package io.github.dsheirer.database; + +import io.github.dsheirer.alias.Alias; +import io.github.dsheirer.alias.id.radio.RadioRange; +import io.github.dsheirer.alias.id.talkgroup.P25FullyQualifiedTalkgroup; +import io.github.dsheirer.alias.id.talkgroup.Talkgroup; +import io.github.dsheirer.alias.id.talkgroup.TalkgroupRange; +import io.github.dsheirer.database.call.CallRecord; +import io.github.dsheirer.database.call.ICallRepository; +import io.github.dsheirer.database.call.CallSearchCriteria; +import io.github.dsheirer.database.call.CallState; +import io.github.dsheirer.database.call.CallType; +import io.github.dsheirer.database.call.CallRepository; +import io.github.dsheirer.database.call.IdentifierFilter; +import io.github.dsheirer.database.call.PatchedTalkgroup; +import io.github.dsheirer.database.call.RecordingState; +import io.github.dsheirer.protocol.Protocol; +import org.junit.jupiter.api.Assumptions; +import org.junit.jupiter.api.Test; +import org.junit.jupiter.api.io.TempDir; + +import java.nio.file.Path; +import java.sql.Connection; +import java.util.List; + +import static org.junit.jupiter.api.Assertions.assertEquals; +import static org.junit.jupiter.api.Assertions.assertFalse; +import static org.junit.jupiter.api.Assertions.assertNotEquals; +import static org.junit.jupiter.api.Assertions.assertTrue; + +/** + * Schema installation, call insert/update/search and age-off queries against the built-in H2 database. + * + * The same checks run against network MariaDB and PostgreSQL servers when these environment variables are set to + * host:port:user:password (and, for PostgreSQL, host:port:user:password:database): + * SDRTRUNK_TEST_MARIADB, SDRTRUNK_TEST_POSTGRES + * The tests use (and drop) the schema 'sdrtrunk_unit_test'. + */ +public class CallDatabaseTest +{ + private static final String TEST_SCHEMA = "sdrtrunk_unit_test"; + + @TempDir + Path mTempDir; + + @Test + public void h2() + throws Exception + { + exercise(new DatabaseConfiguration(DatabaseType.H2, "", 0, "", TEST_SCHEMA, "", "")); + } + + @Test + public void mariadb() + throws Exception + { + DatabaseConfiguration configuration = fromEnvironment("SDRTRUNK_TEST_MARIADB", DatabaseType.MARIADB); + Assumptions.assumeTrue(configuration != null, "SDRTRUNK_TEST_MARIADB not set"); + exercise(configuration); + } + + @Test + public void postgres() + throws Exception + { + DatabaseConfiguration configuration = fromEnvironment("SDRTRUNK_TEST_POSTGRES", DatabaseType.POSTGRESQL); + Assumptions.assumeTrue(configuration != null, "SDRTRUNK_TEST_POSTGRES not set"); + exercise(configuration); + } + + private static DatabaseConfiguration fromEnvironment(String variable, DatabaseType type) + { + String value = System.getenv(variable); + + if(value == null || value.isBlank()) + { + return null; + } + + String[] parts = value.split(":"); + String database = parts.length > 4 ? parts[4] : "postgres"; + return new DatabaseConfiguration(type, parts[0], Integer.parseInt(parts[1]), database, TEST_SCHEMA, parts[2], + parts[3]); + } + + private static CallRecord call(long start, String to, long toValue, String talkerAlias, String from, + long fromValue, String path, long size) + { + CallRecord call = new CallRecord(); + call.setStartTime(start); + call.setProtocol(Protocol.APCO25.name()); + call.setSystem("Metro System"); + call.setSite("Site 1"); + call.setChannel("Control"); + call.setFrequency(851_012_500); + call.setToId(to); + call.setToValue(toValue); + call.setToForm(CallRecord.FORM_TALKGROUP); + call.setFromId(from); + call.setFromValue(fromValue); + call.setFromForm(CallRecord.FORM_RADIO); + call.setAliasList("Metro"); + call.setTalkerAlias(talkerAlias); + call.setRecordFormat("MP3"); + call.setFilePath(path); + call.setFileSize(size); + call.setRecordingState(path != null ? RecordingState.RECORDING : RecordingState.NONE); + return call; + } + + private void exercise(DatabaseConfiguration configuration) throws Exception + { + try(CallDatabase database = new CallDatabase(configuration, mTempDir)) + { + try(Connection connection = database.getConnection(); + java.sql.Statement statement = connection.createStatement()) + { + statement.execute(configuration.type().getDialect().getDropSchemaStatement(TEST_SCHEMA)); + } + + assertEquals(SchemaStatus.MISSING, database.getSchemaStatus()); + database.install(); + assertEquals(SchemaStatus.SDRTRUNK, database.getSchemaStatus()); + //Installing again is a no-op + database.install(); + + ICallRepository repository = database.getCallRepository(); + + try(Connection connection = database.getConnection()) + { + CallRecord active = call(1000, "1234", 1234, "Fire Dispatch", "5551212", 5551212, "/rec/a.mp3", 0); + long id = repository.insert(connection, active); + assertTrue(id > 0); + + active.setEndTime(4000); + active.setCallState(CallState.COMPLETE); + active.setRecordingState(RecordingState.RECORDED); + active.setFileSize(10_000); + assertTrue(repository.update(connection, active)); + + CallRecord second = call(5000, "2000", 2000, "Police_Tac 100%", "777", 777, "/rec/b.mp3", 0); + second.setRecordingState(RecordingState.RECORDED); + second.setFileSize(20_000); + second.setCallState(CallState.COMPLETE); + second.setEndTime(6000); + repository.insert(connection, second); + + CallRecord unrecorded = call(7000, "2000", 2000, "Police_Tac 100%", "778", 778, null, 0); + long unrecordedId = repository.insert(connection, unrecorded); + assertNotEquals(id, unrecordedId); + + assertEquals(3, repository.count(connection)); + + List all = repository.search(connection, CallSearchCriteria.all(), 0, 100); + assertEquals(3, all.size()); + assertEquals(7000, all.get(0).getStartTime(), "newest first"); + CallRecord loaded = all.get(2); + assertEquals(3000, loaded.getDuration()); + assertEquals("Fire Dispatch", loaded.getTalkerAlias()); + assertEquals("Metro", loaded.getAliasList()); + assertEquals(1234L, loaded.getToValue()); + assertTrue(loaded.isToTalkgroup()); + assertTrue(loaded.isFromRadio()); + assertEquals(CallState.COMPLETE, loaded.getCallState()); + assertEquals(Protocol.APCO25, loaded.getProtocolValue()); + + assertEquals(2, search(repository, connection, "2000", null, null)); + assertEquals(1, search(repository, connection, "123", null, null), "partial match"); + assertEquals(1, search(repository, connection, null, "5551212", null)); + assertEquals(1, search(repository, connection, null, null, "fire dis"), "case insensitive"); + assertEquals(2, search(repository, connection, null, null, "tac 100%"), "literal percent"); + assertEquals(0, search(repository, connection, null, null, "police%tac"), "escaped wildcard"); + assertEquals(0, search(repository, connection, null, null, "police_tac_"), "escaped underscore"); + assertEquals(2, repository.count(connection, new CallSearchCriteria(null, null, null, + Protocol.APCO25, "metro", 4000, 8000))); + + for(CallRecord call: all) + { + CallSearchCriteria criteria = new CallSearchCriteria(IdentifierFilter.text("2000"), null, "tac", + null, null, 0, 0); + assertEquals(call.getToValue() == 2000, criteria.matches(call), "in-memory matches agrees"); + } + + //Roaming (fully qualified) talkgroup call: local value 2000, fully qualified 0xBEE00.1.2000 + CallRecord roaming = call(8000, "2000", 2000, null, "779", 779, null, 0); + roaming.setToFullyQualified("781824.1.2000"); + repository.insert(connection, roaming); + + //Alias searches: the alias identifiers are translated to value, range and fully qualified filters + Alias talkgroupAlias = new Alias("Police Tac"); + talkgroupAlias.setAliasListName("Metro"); + talkgroupAlias.addAliasID(new Talkgroup(Protocol.APCO25, 2000)); + talkgroupAlias.addAliasID(new TalkgroupRange(Protocol.APCO25, 1200, 1300)); + assertEquals(3, aliasSearch(repository, connection, IdentifierFilter.talkgroupAlias(talkgroupAlias), + null), "talkgroup value and range, not the fully qualified call"); + + Alias roamingAlias = new Alias("Roaming Tac"); + roamingAlias.setAliasListName("metro"); + roamingAlias.addAliasID(new P25FullyQualifiedTalkgroup(781824, 1, 2000)); + assertEquals(1, aliasSearch(repository, connection, IdentifierFilter.talkgroupAlias(roamingAlias), + null), "fully qualified talkgroup only, alias list name case-insensitive"); + + Alias otherList = new Alias("Police Tac"); + otherList.setAliasListName("County"); + otherList.addAliasID(new Talkgroup(Protocol.APCO25, 2000)); + assertEquals(0, aliasSearch(repository, connection, IdentifierFilter.talkgroupAlias(otherList), null), + "alias from a different alias list"); + + Alias dmrAlias = new Alias("DMR 2000"); + dmrAlias.setAliasListName("Metro"); + dmrAlias.addAliasID(new Talkgroup(Protocol.DMR, 2000)); + assertEquals(0, aliasSearch(repository, connection, IdentifierFilter.talkgroupAlias(dmrAlias), null), + "different protocol"); + + Alias radioAlias = new Alias("Engine 7"); + radioAlias.setAliasListName("Metro"); + radioAlias.addAliasID(new RadioRange(Protocol.APCO25, 776, 778)); + assertEquals(2, aliasSearch(repository, connection, null, IdentifierFilter.radioAlias(radioAlias)), + "radio range"); + assertEquals(2, aliasSearch(repository, connection, IdentifierFilter.talkgroupAlias(talkgroupAlias), + IdentifierFilter.radioAlias(radioAlias)), "talkgroup and radio alias combined"); + + List withRoaming = repository.search(connection, CallSearchCriteria.all(), 0, 100); + + for(IdentifierFilter filter: List.of(IdentifierFilter.talkgroupAlias(talkgroupAlias), + IdentifierFilter.talkgroupAlias(roamingAlias), IdentifierFilter.talkgroupAlias(otherList), + IdentifierFilter.talkgroupAlias(dmrAlias))) + { + CallSearchCriteria criteria = new CallSearchCriteria(filter, null, null, null, null, 0, 0); + long inMemory = withRoaming.stream().filter(criteria::matches).count(); + assertEquals(repository.count(connection, criteria), inMemory, "in-memory alias match " + filter); + } + + repository.delete(connection, List.of(roaming.getId())); + + //Retention rule: duplicates and Do Not Monitor calls without an alias record option + CallRecord duplicate = call(100, "3000", 3000, null, "1", 1, null, 0); + duplicate.setDuplicate(true); + duplicate.setCallState(CallState.COMPLETE); + CallRecord doNotMonitor = call(200, "3001", 3001, null, "2", 2, null, 0); + doNotMonitor.setPriority(-1); + doNotMonitor.setCallState(CallState.COMPLETE); + CallRecord recordedDoNotMonitor = call(300, "3002", 3002, null, "3", 3, null, 0); + recordedDoNotMonitor.setPriority(-1); + recordedDoNotMonitor.setAliasRecord(true); + recordedDoNotMonitor.setCallState(CallState.COMPLETE); + CallRecord activeDuplicate = call(400, "3003", 3003, null, "4", 4, null, 0); + activeDuplicate.setDuplicate(true); + CallRecord newDuplicate = call(900_000, "3004", 3004, null, "5", 5, null, 0); + newDuplicate.setDuplicate(true); + newDuplicate.setCallState(CallState.COMPLETE); + List retention = List.of(duplicate, doNotMonitor, recordedDoNotMonitor, activeDuplicate, + newDuplicate); + + for(CallRecord call: retention) + { + repository.insert(connection, call); + } + + List expired = repository.getDuplicateAndDoNotMonitorCalls(connection, 1000, 100); + assertEquals(List.of(100L, 200L), expired.stream().map(CallRecord::getStartTime).toList(), + "completed duplicate and Do Not Monitor calls before the cutoff, oldest first"); + assertTrue(repository.search(connection, new CallSearchCriteria(IdentifierFilter.text("3002"), null, + null, null, null, 0, 0), 0, 1).getFirst().isAliasRecord()); + repository.delete(connection, retention.stream().map(CallRecord::getId).toList()); + + //Patch group call: patch group 500 with patched talkgroups 2000 and 4000 (fully qualified 4001) + CallRecord patch = call(9500, "P:500", 500, null, "9", 9, null, 0); + patch.setToForm(CallRecord.FORM_PATCH_GROUP); + patch.setPatchedTalkgroups(List.of(new PatchedTalkgroup(4000, null), + new PatchedTalkgroup(4001, "781824.1.4001"))); + repository.insert(connection, patch); + patch.setPatchedTalkgroups(List.of(new PatchedTalkgroup(2000, null), new PatchedTalkgroup(4000, null), + new PatchedTalkgroup(4001, "781824.1.4001"))); + repository.update(connection, patch); + + CallRecord loadedPatch = repository.search(connection, new CallSearchCriteria( + IdentifierFilter.text("4000"), null, null, null, null, 0, 0), 0, 10).getFirst(); + assertEquals(patch.getId(), loadedPatch.getId(), "typed patched talkgroup finds the patch call"); + assertEquals(3, loadedPatch.getPatchedTalkgroups().size(), "patched talkgroups replaced on update"); + assertEquals(1, search(repository, connection, "500", null, null), "patch group value still matches"); + assertEquals(3, search(repository, connection, "2000", null, null), + "talkgroup 2000 calls plus the patch call that included it"); + + Alias patchedAlias = new Alias("Tac 4000"); + patchedAlias.setAliasListName("Metro"); + patchedAlias.addAliasID(new TalkgroupRange(Protocol.APCO25, 3990, 4005)); + assertEquals(1, aliasSearch(repository, connection, IdentifierFilter.talkgroupAlias(patchedAlias), + null), "alias range matches a patched talkgroup"); + Alias patchedFq = new Alias("Roaming 4001"); + patchedFq.setAliasListName("Metro"); + patchedFq.addAliasID(new P25FullyQualifiedTalkgroup(781824, 1, 4001)); + assertEquals(1, aliasSearch(repository, connection, IdentifierFilter.talkgroupAlias(patchedFq), + null), "fully qualified patched talkgroup"); + + List withPatch = repository.search(connection, CallSearchCriteria.all(), 0, 100); + + for(IdentifierFilter filter: List.of(IdentifierFilter.text("4000"), IdentifierFilter.text("2000"), + IdentifierFilter.talkgroupAlias(patchedAlias), IdentifierFilter.talkgroupAlias(patchedFq))) + { + CallSearchCriteria criteria = new CallSearchCriteria(filter, null, null, null, null, 0, 0); + assertEquals(repository.count(connection, criteria), + withPatch.stream().filter(criteria::matches).count(), "in-memory patched match " + filter); + } + + assertEquals(3, repository.readPage(connection, patch.getId() - 1, 1).getFirst() + .getPatchedTalkgroups().size(), "transfer page reads patched talkgroups"); + + //Call type filter: one call of each type alongside the group and patch calls + CallRecord privateCall = call(9600, "777", 777, null, "778", 778, null, 0); + privateCall.setToForm(CallRecord.FORM_RADIO); + CallRecord phoneTo = call(9601, "5551234", 0, null, "779", 779, null, 0); + phoneTo.setToForm("TELEPHONE_NUMBER"); + phoneTo.setToValue(null); + CallRecord phoneFrom = call(9602, "780", 780, null, "5559876", 0, null, 0); + phoneFrom.setToForm(CallRecord.FORM_RADIO); + phoneFrom.setFromForm("TELEPHONE_NUMBER"); + CallRecord analog = call(9603, null, 0, null, null, 0, null, 0); + analog.setToForm(null); + analog.setToValue(null); + analog.setFromForm(null); + List typed = List.of(privateCall, phoneTo, phoneFrom, analog); + + for(CallRecord call: typed) + { + repository.insert(connection, call); + } + + List everything = repository.search(connection, CallSearchCriteria.all(), 0, 1000); + + for(CallType type: CallType.values()) + { + CallSearchCriteria criteria = new CallSearchCriteria(null, null, null, null, type, null, 0, 0); + long expected = everything.stream().filter(c -> c.getCallType() == type).count(); + assertEquals(expected, repository.count(connection, criteria), "SQL type filter " + type); + assertEquals(expected, everything.stream().filter(criteria::matches).count(), + "in-memory type filter " + type); + assertTrue(expected > 0, "test data has a " + type + " call"); + } + + assertEquals(2, repository.count(connection, new CallSearchCriteria(null, null, null, null, + CallType.TELEPHONE, null, 0, 0)), "telephone calls in both directions"); + repository.delete(connection, typed.stream().map(CallRecord::getId).toList()); + + repository.delete(connection, List.of(patch.getId())); + + try(java.sql.Statement statement = connection.createStatement(); + java.sql.ResultSet rs = statement.executeQuery("SELECT COUNT(*) FROM " + TEST_SCHEMA + "." + + CallRepository.PATCHED_TABLE)) + { + rs.next(); + assertEquals(0, rs.getLong(1), "patched talkgroups deleted with the call"); + } + + assertEquals(30_000, repository.getRecordedFileSize(connection)); + List recorded = repository.getRecordedCalls(connection, 0, 10); + assertEquals(2, recorded.size()); + assertEquals(1000, recorded.get(0).getStartTime(), "oldest first"); + assertEquals(1, repository.getRecordedCalls(connection, 4000, 10).size()); + + repository.markRecordingsDeleted(connection, List.of(id)); + assertEquals(20_000, repository.getRecordedFileSize(connection)); + CallRecord deleted = repository.search(connection, CallSearchCriteria.all(), 2, 1).getFirst(); + assertEquals(RecordingState.DELETED, deleted.getRecordingState()); + assertEquals(null, deleted.getFilePath()); + + assertEquals(3, repository.getTransferKeys(connection).size()); + + repository.delete(connection, List.of(unrecordedId)); + assertEquals(2, repository.count(connection)); + + CallRecord interrupted = call(9000, "3", 3, null, "4", 4, "/rec/c.mp3", 0); + repository.insert(connection, interrupted); + assertEquals(1, repository.markInterrupted(connection)); + CallRecord reloaded = repository.search(connection, CallSearchCriteria.all(), 0, 1).getFirst(); + assertEquals(CallState.INTERRUPTED, reloaded.getCallState()); + assertEquals(RecordingState.RECORDED, reloaded.getRecordingState()); + + List transfer = repository.readPage(connection, 0, 100); + assertEquals(3, transfer.size()); + repository.insertBatch(connection, transfer); + assertEquals(6, repository.count(connection)); + } + + //Upgrade from schema version 1: the patched talkgroups table is added + try(Connection connection = database.getConnection(); + java.sql.Statement statement = connection.createStatement()) + { + statement.execute("DROP TABLE " + TEST_SCHEMA + "." + CallRepository.PATCHED_TABLE); + statement.executeUpdate("UPDATE " + TEST_SCHEMA + "." + CallDatabase.SCHEMA_INFO_TABLE + + " SET version = 1"); + } + + database.install(); + + try(Connection connection = database.getConnection(); + java.sql.Statement statement = connection.createStatement(); + java.sql.ResultSet rs = statement.executeQuery("SELECT version FROM " + TEST_SCHEMA + "." + + CallDatabase.SCHEMA_INFO_TABLE)) + { + rs.next(); + assertEquals(CallDatabase.SCHEMA_VERSION, rs.getInt(1), "upgraded to the current version"); + statement.executeQuery("SELECT COUNT(*) FROM " + TEST_SCHEMA + "." + CallRepository.PATCHED_TABLE) + .close(); + } + + String suggested = database.suggestSchemaName(); + assertEquals(TEST_SCHEMA + "_2", suggested); + assertFalse(database.schemaExists(suggested)); + + database.clear(); + assertEquals(SchemaStatus.SDRTRUNK, database.getSchemaStatus()); + assertEquals(0, database.getCallCount()); + + try(Connection connection = database.getConnection(); + java.sql.Statement statement = connection.createStatement()) + { + statement.execute(configuration.type().getDialect().getDropSchemaStatement(TEST_SCHEMA)); + } + } + } + + private static long search(ICallRepository repository, Connection connection, String talkgroup, String radio, + String talkerAlias) throws Exception + { + return repository.count(connection, new CallSearchCriteria(IdentifierFilter.text(talkgroup), + IdentifierFilter.text(radio), talkerAlias, null, null, 0, 0)); + } + + private static long aliasSearch(ICallRepository repository, Connection connection, IdentifierFilter to, + IdentifierFilter from) throws Exception + { + return repository.count(connection, new CallSearchCriteria(to, from, null, null, null, 0, 0)); + } +} diff --git a/src/test/java/io/github/dsheirer/database/call/CallTypeTest.java b/src/test/java/io/github/dsheirer/database/call/CallTypeTest.java new file mode 100644 index 00000000..29840eab --- /dev/null +++ b/src/test/java/io/github/dsheirer/database/call/CallTypeTest.java @@ -0,0 +1,45 @@ +/* + * ***************************************************************************** + * Copyright (C) 2014-2026 Dennis Sheirer + * + * 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 + * the Free Software Foundation, either version 3 of the License, or + * (at your option) any later version. + * + * This program is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU General Public License for more details. + * + * You should have received a copy of the GNU General Public License + * along with this program. If not, see + * **************************************************************************** + */ +package io.github.dsheirer.database.call; + +import org.junit.jupiter.api.Test; + +import static org.junit.jupiter.api.Assertions.assertEquals; + +/** + * Call type derived from the stored TO and FROM identifier forms + */ +public class CallTypeTest +{ + @Test + public void callTypes() + { + assertEquals(CallType.GROUP, CallType.from("TALKGROUP", "RADIO")); + assertEquals(CallType.PATCH, CallType.from("PATCH_GROUP", "RADIO")); + assertEquals(CallType.PRIVATE, CallType.from("RADIO", "RADIO")); + assertEquals(CallType.TELEPHONE, CallType.from("TELEPHONE_NUMBER", "RADIO"), "radio to telephone"); + assertEquals(CallType.TELEPHONE, CallType.from("RADIO", "TELEPHONE_NUMBER"), "telephone to radio"); + assertEquals(CallType.UNKNOWN, CallType.from(null, null), "no TO identifier"); + + CallRecord call = new CallRecord(); + call.setToForm(CallRecord.FORM_RADIO); + call.setFromForm(CallRecord.FORM_RADIO); + assertEquals("Private", call.getCallType().toString()); + } +} diff --git a/src/test/java/io/github/dsheirer/gui/calls/CallsViewPageSizeTest.java b/src/test/java/io/github/dsheirer/gui/calls/CallsViewPageSizeTest.java new file mode 100644 index 00000000..c1e19ce6 --- /dev/null +++ b/src/test/java/io/github/dsheirer/gui/calls/CallsViewPageSizeTest.java @@ -0,0 +1,40 @@ +/* + * ***************************************************************************** + * Copyright (C) 2014-2026 Dennis Sheirer + * + * 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 + * the Free Software Foundation, either version 3 of the License, or + * (at your option) any later version. + * + * This program is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU General Public License for more details. + * + * You should have received a copy of the GNU General Public License + * along with this program. If not, see + * **************************************************************************** + */ +package io.github.dsheirer.gui.calls; + +import org.junit.jupiter.api.Test; + +import java.util.List; + +import static org.junit.jupiter.api.Assertions.assertEquals; + +/** + * Calls tab page size preference choices + */ +public class CallsViewPageSizeTest +{ + @Test + public void pageSizeChoices() + { + assertEquals(List.of(50, 100, 250, 500), CallsView.PAGE_SIZES); + assertEquals(500, CallsView.DEFAULT_PAGE_SIZE); + assertEquals(250, CallsView.getStoredPageSize(250)); + assertEquals(500, CallsView.getStoredPageSize(75), "invalid stored value uses the default"); + } +} diff --git a/src/test/java/io/github/dsheirer/record/RecordingReaderTest.java b/src/test/java/io/github/dsheirer/record/RecordingReaderTest.java new file mode 100644 index 00000000..33a6b816 --- /dev/null +++ b/src/test/java/io/github/dsheirer/record/RecordingReaderTest.java @@ -0,0 +1,121 @@ +/* + * ***************************************************************************** + * Copyright (C) 2014-2026 Dennis Sheirer + * + * 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 + * the Free Software Foundation, either version 3 of the License, or + * (at your option) any later version. + * + * This program is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU General Public License for more details. + * + * You should have received a copy of the GNU General Public License + * along with this program. If not, see + * **************************************************************************** + */ +package io.github.dsheirer.record; + +import io.github.dsheirer.audio.convert.InputAudioFormat; +import io.github.dsheirer.audio.convert.MP3Setting; +import io.github.dsheirer.record.stream.StreamingMP3Writer; +import io.github.dsheirer.record.stream.StreamingWaveWriter; +import io.github.dsheirer.record.wave.AudioMetadata; +import org.junit.jupiter.api.Test; +import org.junit.jupiter.api.io.TempDir; + +import java.nio.file.Path; +import java.util.ArrayList; +import java.util.List; +import java.util.Map; + +import static org.junit.jupiter.api.Assertions.assertEquals; +import static org.junit.jupiter.api.Assertions.assertTrue; + +/** + * Recordings written by the streaming writers read back as 8 kHz audio for replay. + */ +public class RecordingReaderTest +{ + @TempDir + Path mTempDir; + + private static List tone(int buffers) + { + List audio = new ArrayList<>(); + int sample = 0; + + for(int x = 0; x < buffers; x++) + { + float[] buffer = new float[160]; + + for(int y = 0; y < 160; y++) + { + buffer[y] = 0.5f * (float)Math.sin(2.0 * Math.PI * 1000.0 * sample++ / 8000.0); + } + + audio.add(buffer); + } + + return audio; + } + + /** + * RMS of the middle of the samples and the count of zero crossings, to verify level and frequency. + */ + private static void assertTone(float[] samples, int expectedSamples, String label) + { + assertEquals(expectedSamples, samples.length, expectedSamples * 0.08, label + " length"); + int start = samples.length / 4; + int end = samples.length * 3 / 4; + double sum = 0; + int crossings = 0; + + for(int x = start; x < end; x++) + { + sum += samples[x] * samples[x]; + + if(x > start && (samples[x - 1] < 0) != (samples[x] < 0)) + { + crossings++; + } + } + + double rms = Math.sqrt(sum / (end - start)); + double frequency = crossings / 2.0 / ((end - start) / 8000.0); + assertEquals(0.5 / Math.sqrt(2), rms, 0.07, label + " level"); + assertEquals(1000, frequency, 30, label + " frequency"); + } + + @Test + public void readsStreamingMP3() throws Exception + { + for(InputAudioFormat format: new InputAudioFormat[]{InputAudioFormat.SR_16000, InputAudioFormat.SR_8000, + InputAudioFormat.SR_44100}) + { + Path path = mTempDir.resolve("call_" + format.name() + ".mp3"); + StreamingMP3Writer writer = new StreamingMP3Writer(path, format, MP3Setting.CBR_16, false, Map.of()); + writer.write(tone(100)); + writer.close(Map.of(AudioMetadata.TRACK_TITLE, "Test")); + + float[] samples = RecordingReader.read(path); + assertTone(samples, 16000, "MP3 " + format); + } + } + + @Test + public void readsStreamingWave() throws Exception + { + Path path = mTempDir.resolve("call.wav"); + StreamingWaveWriter writer = new StreamingWaveWriter(path); + writer.write(tone(100)); + writer.close(Map.of(AudioMetadata.TRACK_TITLE, "Test")); + + float[] samples = RecordingReader.read(path); + assertEquals(16000, samples.length, "exact length"); + assertTone(samples, 16000, "WAVE"); + assertTrue(samples.length > 0); + } +} diff --git a/src/test/java/io/github/dsheirer/record/stream/StreamingAudioWriterTest.java b/src/test/java/io/github/dsheirer/record/stream/StreamingAudioWriterTest.java new file mode 100644 index 00000000..42ae9e8e --- /dev/null +++ b/src/test/java/io/github/dsheirer/record/stream/StreamingAudioWriterTest.java @@ -0,0 +1,209 @@ +/* + * ***************************************************************************** + * Copyright (C) 2014-2026 Dennis Sheirer + * + * 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 + * the Free Software Foundation, either version 3 of the License, or + * (at your option) any later version. + * + * This program is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU General Public License for more details. + * + * You should have received a copy of the GNU General Public License + * along with this program. If not, see + * **************************************************************************** + */ +package io.github.dsheirer.record.stream; + +import com.mpatric.mp3agic.ID3v2; +import com.mpatric.mp3agic.Mp3File; +import io.github.dsheirer.audio.convert.InputAudioFormat; +import io.github.dsheirer.audio.convert.MP3Setting; +import io.github.dsheirer.record.wave.AudioMetadata; +import io.github.dsheirer.record.wave.AudioMetadataUtils; +import org.junit.jupiter.api.Test; +import org.junit.jupiter.api.io.TempDir; + +import javax.sound.sampled.AudioInputStream; +import javax.sound.sampled.AudioSystem; +import java.nio.file.Files; +import java.nio.file.Path; +import java.util.ArrayList; +import java.util.EnumMap; +import java.util.List; +import java.util.Map; + +import static org.junit.jupiter.api.Assertions.assertEquals; +import static org.junit.jupiter.api.Assertions.assertNotNull; +import static org.junit.jupiter.api.Assertions.assertNull; +import static org.junit.jupiter.api.Assertions.assertTrue; + +/** + * Streaming MP3 and WAVE recordings: audio written in chunks during the call and the final metadata written at close. + */ +public class StreamingAudioWriterTest +{ + private static final int BUFFER_SIZE = 160; + + @TempDir + Path mTempDir; + + /** + * 1 kHz tone at 8 kHz in 20 ms buffers + */ + private static List tone(int buffers, float amplitude) + { + List audio = new ArrayList<>(); + int sample = 0; + + for(int x = 0; x < buffers; x++) + { + float[] buffer = new float[BUFFER_SIZE]; + + for(int y = 0; y < BUFFER_SIZE; y++) + { + buffer[y] = amplitude * (float)Math.sin(2.0 * Math.PI * 1000.0 * sample++ / 8000.0); + } + + audio.add(buffer); + } + + return audio; + } + + private static Map metadata(String title) + { + Map metadata = new EnumMap<>(AudioMetadata.class); + metadata.put(AudioMetadata.TRACK_TITLE, title); + metadata.put(AudioMetadata.ARTIST_NAME, "Radio 1234"); + return metadata; + } + + @Test + public void mp3StreamIsPlayableDuringAndAfterTheCall() throws Exception + { + Path path = mTempDir.resolve("call.mp3"); + StreamingMP3Writer writer = new StreamingMP3Writer(path, InputAudioFormat.SR_16000, MP3Setting.CBR_16, false, + metadata("Initial")); + List audio = tone(250, 0.5f); + + //5 seconds of audio written in 10 chunks, like the recorder does during a call + for(int x = 0; x < 10; x++) + { + writer.write(audio.subList(x * 25, (x + 1) * 25)); + + if(x == 4) + { + assertTrue(Files.size(path) > StreamingMP3Writer.RESERVED_TAG_SIZE, "file grows during the call"); + Mp3File partial = new Mp3File(path.toString()); + assertTrue(partial.getFrameCount() > 0, "partial recording has audio frames"); + } + } + + writer.close(metadata("Final Title")); + + Mp3File mp3 = new Mp3File(path.toString()); + ID3v2 tag = mp3.getId3v2Tag(); + assertNotNull(tag); + assertEquals("Final Title", tag.getTitle(), "final metadata replaces the initial tag"); + assertEquals("Radio 1234", tag.getArtist()); + assertEquals(StreamingMP3Writer.RESERVED_TAG_SIZE, mp3.getStartOffset(), "audio starts after reserved tag"); + assertEquals(5000, mp3.getLengthInMilliseconds(), 300, "duration"); + } + + @Test + public void mp3FinalTagLargerThanReservedSpaceRewritesFile() throws Exception + { + Path path = mTempDir.resolve("long.mp3"); + StreamingMP3Writer writer = new StreamingMP3Writer(path, InputAudioFormat.SR_16000, MP3Setting.CBR_16, false, + metadata("Initial")); + writer.write(tone(100, 0.5f)); + String longTitle = "T".repeat(StreamingMP3Writer.RESERVED_TAG_SIZE * 2); + writer.close(metadata(longTitle)); + + Mp3File mp3 = new Mp3File(path.toString()); + assertEquals(longTitle, mp3.getId3v2Tag().getTitle()); + assertEquals(2000, mp3.getLengthInMilliseconds(), 300); + assertTrue(!Files.exists(path.resolveSibling("long.mp3.tmp")), "temporary file removed"); + } + + @Test + public void paddedTagKeepsFrames() throws Exception + { + byte[] tag = AudioMetadataUtils.getMP3ID3(metadata("Padded")); + byte[] padded = StreamingMP3Writer.getPaddedTag(tag, 4096); + assertNotNull(padded); + assertEquals(4096, padded.length); + assertNull(StreamingMP3Writer.getPaddedTag(tag, tag.length - 1), "tag doesn't fit"); + } + + @Test + public void normalizationGainOnlyDecreases() + { + StreamingMP3Writer writer = null; + + try + { + writer = new StreamingMP3Writer(mTempDir.resolve("n.mp3"), InputAudioFormat.SR_16000, + MP3Setting.CBR_16, true, metadata("N")); + float[] quiet = tone(1, 0.05f).getFirst(); + float[] loud = tone(1, 0.5f).getFirst(); + List first = writer.normalize(List.of(quiet)); + List second = writer.normalize(List.of(loud)); + List third = writer.normalize(List.of(quiet)); + + assertEquals(0.95f * 0.05f / 0.1f, peak(first.getFirst()), 0.01f, "quiet audio gain limited to 20 dB"); + assertEquals(0.95f, peak(second.getFirst()), 0.01f, "loud audio normalized to the target"); + assertEquals(0.95f * 0.05f / 0.5f, peak(third.getFirst()), 0.01f, "gain stays at the lower value"); + assertEquals(0.05f, peak(quiet), 0.001f, "source buffers aren't modified"); + } + catch(Exception e) + { + throw new AssertionError(e); + } + finally + { + if(writer != null) + { + writer.abort(); + } + } + } + + private static float peak(float[] samples) + { + float peak = 0; + + for(float sample: samples) + { + peak = Math.max(peak, Math.abs(sample)); + } + + return peak; + } + + @Test + public void waveStreamIsValidDuringAndAfterTheCall() throws Exception + { + Path path = mTempDir.resolve("call.wav"); + StreamingWaveWriter writer = new StreamingWaveWriter(path); + List audio = tone(100, 0.5f); + writer.write(audio.subList(0, 50)); + + try(AudioInputStream partial = AudioSystem.getAudioInputStream(path.toFile())) + { + assertEquals(50 * BUFFER_SIZE, partial.getFrameLength(), "partial file has a valid header"); + } + + writer.write(audio.subList(50, 100)); + writer.close(metadata("Wave")); + + try(AudioInputStream stream = AudioSystem.getAudioInputStream(path.toFile())) + { + assertEquals(100 * BUFFER_SIZE, stream.getFrameLength()); + } + } +}