Added TikTokLiveRecorderEndedEvent and updated RecorderListener to use CompletableFuture and a cancellation token!

This commit is contained in:
kohlerpop1
2024-10-17 16:53:00 -04:00
parent 75f3896a86
commit eef9d43d01
3 changed files with 80 additions and 42 deletions

View File

@@ -30,7 +30,7 @@ import io.github.jwdeveloper.tiktok.data.settings.LiveClientSettings;
import io.github.jwdeveloper.tiktok.extension.recorder.api.LiveRecorder;
import io.github.jwdeveloper.tiktok.extension.recorder.impl.data.*;
import io.github.jwdeveloper.tiktok.extension.recorder.impl.enums.LiveQuality;
import io.github.jwdeveloper.tiktok.extension.recorder.impl.event.TikTokLiveRecorderStartedEvent;
import io.github.jwdeveloper.tiktok.extension.recorder.impl.event.*;
import io.github.jwdeveloper.tiktok.live.LiveClient;
import io.github.jwdeveloper.tiktok.models.ConnectionState;
@@ -38,14 +38,17 @@ import java.io.*;
import java.net.URI;
import java.net.http.*;
import java.time.Duration;
import java.util.concurrent.CompletableFuture;
import java.util.concurrent.atomic.AtomicBoolean;
import java.util.function.BiConsumer;
public class RecorderListener implements LiveRecorder {
private final BiConsumer<RecorderSettings, LiveClient> consumer;
private final RecorderSettings settings;
private final AtomicBoolean token = new AtomicBoolean();
private DownloadData downloadData;
private Thread liveDownloadThread;
private CompletableFuture<Void> future;
public RecorderListener(BiConsumer<RecorderSettings, LiveClient> consumer) {
this.consumer = consumer;
@@ -74,7 +77,12 @@ public class RecorderListener implements LiveRecorder {
if (isConnected() || downloadData.getDownloadLiveUrl().isEmpty())
return;
liveDownloadThread = new Thread(() -> {
var recordingStartedEvent = new TikTokLiveRecorderStartedEvent(downloadData);
liveClient.publishEvent(recordingStartedEvent);
if (recordingStartedEvent.isCanceled())
liveClient.getLogger().info("Recording cancelled");
else
future = CompletableFuture.runAsync(() -> {
try {
liveClient.getLogger().info("Recording started "+liveClient.getRoomInfo().getHostName());
@@ -94,40 +102,34 @@ public class RecorderListener implements LiveRecorder {
) {
byte[] dataBuffer = new byte[1024];
int bytesRead;
while ((!settings.isStopOnDisconnect() || liveClient.getRoomInfo().getConnectionState() == ConnectionState.CONNECTED) && (bytesRead = in.read(dataBuffer)) != -1) {
while (!token.get() && (!settings.isStopOnDisconnect() || liveClient.getRoomInfo().getConnectionState() == ConnectionState.CONNECTED) && (bytesRead = in.read(dataBuffer)) != -1) {
fos.write(dataBuffer, 0, bytesRead);
fos.flush();
}
} catch (IOException ignored) {
} catch (IOException e) {
e.printStackTrace();
} finally {
liveClient.getLogger().severe("Stopped recording " + liveClient.getRoomInfo().getHostName());
liveClient.getLogger().info("Stopped recording " + liveClient.getRoomInfo().getHostName());
liveClient.publishEvent(new TikTokLiveRecorderEndedEvent(settings));
}
} catch (Exception e) {
e.printStackTrace();
}
});
var recordingStartedEvent = new TikTokLiveRecorderStartedEvent(downloadData);
liveClient.publishEvent(recordingStartedEvent);
if (recordingStartedEvent.isCanceled())
liveClient.getLogger().info("Recording cancelled");
else
liveDownloadThread.start();
}
@TikTokEventObserver
private void onDisconnected(LiveClient liveClient, TikTokDisconnectedEvent event) {
if (isConnected() && settings.isStopOnDisconnect())
liveDownloadThread.interrupt();
token.set(true);
}
@TikTokEventObserver
private void onLiveEnded(LiveClient liveClient, TikTokLiveEndedEvent event) {
if (isConnected())
liveDownloadThread.interrupt();
token.set(true);
}
private DownloadData mapToDownloadData(String json) {
try {
var parsedJson = JsonParser.parseString(json);
@@ -164,6 +166,6 @@ public class RecorderListener implements LiveRecorder {
}
private boolean isConnected() {
return liveDownloadThread != null && liveDownloadThread.isAlive();
return future != null && !future.isDone();
}
}

View File

@@ -0,0 +1,37 @@
/*
* Copyright (c) 2023-2023 jwdeveloper jacekwoln@gmail.com
*
* Permission is hereby granted, free of charge, to any person obtaining
* a copy of this software and associated documentation files (the
* "Software"), to deal in the Software without restriction, including
* without limitation the rights to use, copy, modify, merge, publish,
* distribute, sublicense, and/or sell copies of the Software, and to
* permit persons to whom the Software is furnished to do so, subject to
* the following conditions:
*
* The above copyright notice and this permission notice shall be
* included in all copies or substantial portions of the Software.
*
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
* EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
* MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
* NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
* LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
* OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
* WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
*/
package io.github.jwdeveloper.tiktok.extension.recorder.impl.event;
import io.github.jwdeveloper.tiktok.data.events.common.TikTokEvent;
import io.github.jwdeveloper.tiktok.extension.recorder.impl.data.*;
import lombok.*;
@Getter
public class TikTokLiveRecorderEndedEvent extends TikTokEvent {
private final RecorderSettings settings;
public TikTokLiveRecorderEndedEvent(RecorderSettings settings) {
this.settings = settings;
}
}

View File

@@ -25,7 +25,6 @@ package io.github.jwdeveloper.tiktok.extension.recorder.impl.event;
import io.github.jwdeveloper.tiktok.data.events.common.TikTokEvent;
import io.github.jwdeveloper.tiktok.extension.recorder.impl.data.DownloadData;
import lombok.AllArgsConstructor;
import lombok.Data;
import lombok.Getter;
import lombok.Setter;