|
| 1 | +/* |
| 2 | + * ***************************************************************************** |
| 3 | + * Copyright (C) 2014-2024 Dennis Sheirer |
| 4 | + * |
| 5 | + * This program is free software: you can redistribute it and/or modify |
| 6 | + * it under the terms of the GNU General Public License as published by |
| 7 | + * the Free Software Foundation, either version 3 of the License, or |
| 8 | + * (at your option) any later version. |
| 9 | + * |
| 10 | + * This program is distributed in the hope that it will be useful, |
| 11 | + * but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 12 | + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
| 13 | + * GNU General Public License for more details. |
| 14 | + * |
| 15 | + * You should have received a copy of the GNU General Public License |
| 16 | + * along with this program. If not, see <http://www.gnu.org/licenses/> |
| 17 | + * **************************************************************************** |
| 18 | + */ |
| 19 | + |
| 20 | +package io.github.dsheirer.audio.broadcast; |
| 21 | + |
| 22 | +import io.github.dsheirer.alias.Alias; |
| 23 | +import io.github.dsheirer.alias.AliasList; |
| 24 | +import io.github.dsheirer.alias.id.broadcast.BroadcastChannel; |
| 25 | +import io.github.dsheirer.alias.id.talkgroup.Talkgroup; |
| 26 | +import io.github.dsheirer.audio.AudioSegment; |
| 27 | +import io.github.dsheirer.dsp.oscillator.ScalarRealOscillator; |
| 28 | +import io.github.dsheirer.identifier.patch.PatchGroup; |
| 29 | +import io.github.dsheirer.identifier.radio.RadioIdentifier; |
| 30 | +import io.github.dsheirer.identifier.talkgroup.TalkgroupIdentifier; |
| 31 | +import io.github.dsheirer.message.TimeslotMessage; |
| 32 | +import io.github.dsheirer.module.decode.p25.identifier.patch.APCO25PatchGroup; |
| 33 | +import io.github.dsheirer.module.decode.p25.identifier.radio.APCO25RadioIdentifier; |
| 34 | +import io.github.dsheirer.module.decode.p25.identifier.talkgroup.APCO25Talkgroup; |
| 35 | +import io.github.dsheirer.preference.UserPreferences; |
| 36 | +import io.github.dsheirer.protocol.Protocol; |
| 37 | +import io.github.dsheirer.sample.Listener; |
| 38 | +import java.io.IOException; |
| 39 | +import java.nio.file.Files; |
| 40 | +import java.nio.file.Path; |
| 41 | +import java.util.concurrent.CountDownLatch; |
| 42 | +import java.util.concurrent.TimeUnit; |
| 43 | +import java.util.stream.Stream; |
| 44 | +import org.junit.jupiter.api.Test; |
| 45 | + |
| 46 | +import static org.junit.jupiter.api.Assertions.assertTrue; |
| 47 | + |
| 48 | +/** |
| 49 | + * Automated testing for the AudioStreamingManager that includes for testing streaming a patch group audio segment as |
| 50 | + * an individual stream aliased against the patch group, or broken up into the set of patched talkgroups and streamed |
| 51 | + * according to the aliases for each patched talkgroup. |
| 52 | + */ |
| 53 | +public class AudioStreamingManagerTest |
| 54 | +{ |
| 55 | + private static final int TALKGROUP_1 = 100; |
| 56 | + private static final int TALKGROUP_2 = 200; |
| 57 | + private static final int TALKGROUP_3 = 300; |
| 58 | + private static final int RADIO_1 = 9999; |
| 59 | + |
| 60 | + @Test |
| 61 | + public void testPatchGroupStreamingAsPatchGroup() |
| 62 | + { |
| 63 | + int expectedRecordingsCount = 1; |
| 64 | + |
| 65 | + //We use a countdown latch to count the number of expected audio recordings produced. |
| 66 | + CountDownLatch latch = new CountDownLatch(expectedRecordingsCount); |
| 67 | + Listener<AudioRecording> listener = audioRecording -> { |
| 68 | + latch.countDown(); |
| 69 | + }; |
| 70 | + |
| 71 | + UserPreferences userPreferences = new UserPreferences(); |
| 72 | + userPreferences.getCallManagementPreference().setPatchGroupStreamingOption(PatchGroupStreamingOption.PATCH_GROUP); |
| 73 | + AudioStreamingManager manager = new AudioStreamingManager(listener, BroadcastFormat.MP3, userPreferences); |
| 74 | + manager.start(); |
| 75 | + manager.receive(getAudioSegment()); |
| 76 | + |
| 77 | + boolean success = false; |
| 78 | + |
| 79 | + try |
| 80 | + { |
| 81 | + success = latch.await(5, TimeUnit.SECONDS); |
| 82 | + } |
| 83 | + catch(InterruptedException e) |
| 84 | + { |
| 85 | + throw new RuntimeException(e); |
| 86 | + } |
| 87 | + |
| 88 | + cleanupStreamingDirectory(userPreferences.getDirectoryPreference().getDirectoryStreaming()); |
| 89 | + |
| 90 | + assertTrue(success, "Stream patch group audio as PATCHED GROUP failed to produce [" + |
| 91 | + latch.getCount() + "/" + expectedRecordingsCount + "] streaming recordings"); |
| 92 | + } |
| 93 | + |
| 94 | + @Test |
| 95 | + public void testPatchGroupStreamingAsIndividualGroups() |
| 96 | + { |
| 97 | + int expectedRecordingsCount = 2; |
| 98 | + |
| 99 | + //We use a countdown latch to count the number of expected audio recordings produced. In this case, we expect |
| 100 | + //two audio recordings, one for stream B and one for stream C associated with the two patched talkgroups. |
| 101 | + CountDownLatch latch = new CountDownLatch(expectedRecordingsCount); |
| 102 | + Listener<AudioRecording> listener = audioRecording -> { |
| 103 | + latch.countDown(); |
| 104 | + }; |
| 105 | + |
| 106 | + UserPreferences userPreferences = new UserPreferences(); |
| 107 | + userPreferences.getCallManagementPreference().setPatchGroupStreamingOption(PatchGroupStreamingOption.TALKGROUPS); |
| 108 | + AudioStreamingManager manager = new AudioStreamingManager(listener, BroadcastFormat.MP3, userPreferences); |
| 109 | + manager.start(); |
| 110 | + manager.receive(getAudioSegment()); |
| 111 | + |
| 112 | + boolean success = false; |
| 113 | + |
| 114 | + try |
| 115 | + { |
| 116 | + success = latch.await(5, TimeUnit.SECONDS); |
| 117 | + } |
| 118 | + catch(InterruptedException e) |
| 119 | + { |
| 120 | + throw new RuntimeException(e); |
| 121 | + } |
| 122 | + |
| 123 | + cleanupStreamingDirectory(userPreferences.getDirectoryPreference().getDirectoryStreaming()); |
| 124 | + |
| 125 | + assertTrue(success, "Stream patch group audio as INDIVIDUAL TALKGROUPS failed to produce [" + |
| 126 | + latch.getCount() + "/" + expectedRecordingsCount + "] streaming recordings"); |
| 127 | + } |
| 128 | + |
| 129 | + /** |
| 130 | + * Cleanup any generated streaming recordings. |
| 131 | + * @param streamingDirectory |
| 132 | + */ |
| 133 | + private void cleanupStreamingDirectory(Path streamingDirectory) |
| 134 | + { |
| 135 | + if(Files.exists(streamingDirectory)) |
| 136 | + { |
| 137 | + try(Stream<Path> fileStream = Files.list(streamingDirectory)) |
| 138 | + { |
| 139 | + fileStream.forEach(path -> { |
| 140 | + try |
| 141 | + { |
| 142 | + Files.delete(path); |
| 143 | + } |
| 144 | + catch(IOException e) |
| 145 | + { |
| 146 | + e.printStackTrace(); |
| 147 | + } |
| 148 | + }); |
| 149 | + } |
| 150 | + catch(IOException e) |
| 151 | + { |
| 152 | + e.printStackTrace(); |
| 153 | + } |
| 154 | + } |
| 155 | + } |
| 156 | + |
| 157 | + /** |
| 158 | + * Creates an audio segment with audio using the supplied alias list. |
| 159 | + * @return audio segment |
| 160 | + */ |
| 161 | + private static AudioSegment getAudioSegment() |
| 162 | + { |
| 163 | + AliasList aliasList = getAliasList(); |
| 164 | + |
| 165 | + AudioSegment audioSegment = new AudioSegment(aliasList, TimeslotMessage.TIMESLOT_0); |
| 166 | + ScalarRealOscillator oscillator = new ScalarRealOscillator(1000, 8000); |
| 167 | + for(int x = 0; x < 100; x++) |
| 168 | + { |
| 169 | + audioSegment.addAudio(oscillator.generate(500)); |
| 170 | + } |
| 171 | + audioSegment.addIdentifier(getPatchGroup()); |
| 172 | + audioSegment.addIdentifier(getRadio()); |
| 173 | + audioSegment.completeProperty().set(true); |
| 174 | + return audioSegment; |
| 175 | + } |
| 176 | + |
| 177 | + private static AliasList getAliasList() |
| 178 | + { |
| 179 | + AliasList aliasList = new AliasList("test"); |
| 180 | + |
| 181 | + Alias patchAlias = new Alias("patch"); |
| 182 | + patchAlias.addAliasID(new Talkgroup(Protocol.APCO25, 100)); |
| 183 | + patchAlias.addAliasID(new BroadcastChannel("Stream A")); |
| 184 | + aliasList.addAlias(patchAlias); |
| 185 | + |
| 186 | + Alias talkgroupAlias1 = new Alias("talkgroup1"); |
| 187 | + talkgroupAlias1.addAliasID(new Talkgroup(Protocol.APCO25, 200)); |
| 188 | + talkgroupAlias1.addAliasID(new BroadcastChannel("Stream B")); |
| 189 | + aliasList.addAlias(talkgroupAlias1); |
| 190 | + |
| 191 | + Alias talkgroupAlias2 = new Alias("talkgroup2"); |
| 192 | + talkgroupAlias2.addAliasID(new Talkgroup(Protocol.APCO25, 300)); |
| 193 | + talkgroupAlias2.addAliasID(new BroadcastChannel("Stream C")); |
| 194 | + aliasList.addAlias(talkgroupAlias2); |
| 195 | + |
| 196 | + return aliasList; |
| 197 | + } |
| 198 | + |
| 199 | + /** |
| 200 | + * Creates a patch group |
| 201 | + * @return p25 patch group |
| 202 | + */ |
| 203 | + private static APCO25PatchGroup getPatchGroup() |
| 204 | + { |
| 205 | + TalkgroupIdentifier talkgroup1 = APCO25Talkgroup.create(TALKGROUP_1); |
| 206 | + TalkgroupIdentifier talkgroup2 = APCO25Talkgroup.create(TALKGROUP_2); |
| 207 | + TalkgroupIdentifier talkgroup3 = APCO25Talkgroup.create(TALKGROUP_3); |
| 208 | + |
| 209 | + PatchGroup pg = new PatchGroup(talkgroup1); |
| 210 | + pg.addPatchedTalkgroup(talkgroup2); |
| 211 | + pg.addPatchedTalkgroup(talkgroup3); |
| 212 | + return APCO25PatchGroup.create(pg); |
| 213 | + } |
| 214 | + |
| 215 | + /** |
| 216 | + * Creates a source radio identifier. |
| 217 | + * @return radio |
| 218 | + */ |
| 219 | + private static RadioIdentifier getRadio() |
| 220 | + { |
| 221 | + return APCO25RadioIdentifier.createFrom(RADIO_1); |
| 222 | + } |
| 223 | +} |
0 commit comments