use of net.robinfriedli.aiode.audio.exec.BlockingTrackLoadingExecutor in project aiode by robinfriedli.
the class AudioTrafficSimulationCommand method runAdmin.
@Override
public void runAdmin() throws Exception {
Aiode aiode = Aiode.get();
AudioManager audioManager = aiode.getAudioManager();
AudioPlayerManager playerManager = audioManager.getPlayerManager();
PlayableFactory playableFactory = audioManager.createPlayableFactory(getSpotifyService(), new BlockingTrackLoadingExecutor());
CommandContext context = getContext();
Guild guild = context.getGuild();
ThreadExecutionQueue threadExecutionQueue = aiode.getExecutionQueueManager().getForGuild(guild);
SpotifyApi spotifyApi = context.getSpotifyApi();
AudioTrackLoader audioTrackLoader = new AudioTrackLoader(playerManager);
int streams = getArgumentValueOrElse("streams", DEFAULT_STREAM_COUNT);
int durationSecs = getArgumentValueOrElse("duration", DEFAULT_DURATION);
String playbackUrl = getArgumentValueOrElse("url", DEFAULT_PLAYBACK_URL);
Integer nativeBuffer = getArgumentValueWithTypeOrElse("nativeBuffer", Integer.class, null);
List<Playable> playables = playableFactory.createPlayables(playbackUrl, spotifyApi, true);
if (playables.isEmpty()) {
throw new InvalidCommandException("No playables found for url " + playbackUrl);
}
Playable playable = playables.get(0);
AudioItem audioItem = audioTrackLoader.loadByIdentifier(playable.getPlaybackUrl());
AudioTrack track;
if (audioItem instanceof AudioTrack) {
track = (AudioTrack) audioItem;
} else {
throw new IllegalStateException("Could not get AudioTrack for Playable " + playable);
}
LoopTrackListener loopTrackListener = new LoopTrackListener(track);
List<Thread> playbackThreads = nativeBuffer != null ? null : Lists.newArrayListWithCapacity(streams);
List<Tuple2<IAudioSendSystem, AudioPlayer>> audioSendSystems = nativeBuffer != null ? Lists.newArrayListWithCapacity(streams) : null;
NativeAudioSendFactory nativeAudioSendFactory = nativeBuffer != null ? new NativeAudioSendFactory(nativeBuffer) : null;
LoggingUncaughtExceptionHandler eh = new LoggingUncaughtExceptionHandler();
for (int i = 0; i < streams; i++) {
AudioPlayer player = playerManager.createPlayer();
player.addListener(loopTrackListener);
player.playTrack(track.makeClone());
if (nativeAudioSendFactory != null) {
IAudioSendSystem sendSystem = nativeAudioSendFactory.createSendSystem(new FakePacketProvider(player));
audioSendSystems.add(new Tuple2<>(sendSystem, player));
} else {
Thread playbackThread = new Thread(new PlayerPollingRunnable(player));
playbackThread.setDaemon(true);
playbackThread.setUncaughtExceptionHandler(eh);
playbackThread.setName("simulated-playback-thread-" + i);
playbackThreads.add(playbackThread);
}
}
QueuedTask playbackThreadsManagementTask = new QueuedTask(threadExecutionQueue, new FakePlayerManagementTask(playbackThreads, audioSendSystems, durationSecs)) {
@Override
protected boolean isPrivileged() {
return true;
}
};
playbackThreadsManagementTask.setName("simulated-playback-management-task");
threadExecutionQueue.add(playbackThreadsManagementTask, false);
}
use of net.robinfriedli.aiode.audio.exec.BlockingTrackLoadingExecutor in project aiode by robinfriedli.
the class AddCommand method withUserResponse.
@Override
public void withUserResponse(Object option) {
Session session = getContext().getSession();
String playlistName = getArgumentValue(definingArgument);
Playlist playlist = SearchEngine.searchLocalList(session, playlistName);
if (playlist == null) {
throw new NoResultsFoundException(String.format("No local list found for '%s'", getToAddString()));
}
PlayableFactory playableFactory = Aiode.get().getAudioManager().createPlayableFactory(getSpotifyService(), new BlockingTrackLoadingExecutor());
List<Playable> playables = playableFactory.createPlayables(false, option);
addPlayables(playlist, playables);
}
Aggregations