mirror of
https://github.com/jwdeveloper/TikTokLiveJava.git
synced 2026-02-27 08:49:40 -05:00
- improve recorder
This commit is contained in:
@@ -30,6 +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.live.LiveClient;
|
||||
import io.github.jwdeveloper.tiktok.models.ConnectionState;
|
||||
|
||||
@@ -60,7 +61,9 @@ public class RecorderListener implements LiveRecorder {
|
||||
var json = event.getLiveData().getJson();
|
||||
|
||||
liveClient.getLogger().info("Searching for live download url");
|
||||
downloadData = settings.getPrepareDownloadData() != null ? settings.getPrepareDownloadData().apply(json) : mapToDownloadData(json);
|
||||
downloadData = settings.getPrepareDownloadData() != null ?
|
||||
settings.getPrepareDownloadData().apply(json) :
|
||||
mapToDownloadData(json);
|
||||
|
||||
if (downloadData.getDownloadLiveUrl().isEmpty())
|
||||
liveClient.getLogger().warning("Unable to find download live url!");
|
||||
@@ -72,8 +75,11 @@ public class RecorderListener implements LiveRecorder {
|
||||
private void onConnected(LiveClient liveClient, TikTokConnectedEvent event) {
|
||||
if (isConnected())
|
||||
return;
|
||||
|
||||
|
||||
liveDownloadThread = new Thread(() -> {
|
||||
try {
|
||||
liveClient.getLogger().info("Recording started");
|
||||
var url = new URL(downloadData.getFullUrl());
|
||||
HttpsURLConnection connection = (HttpsURLConnection) url.openConnection();
|
||||
var headers = LiveClientSettings.DefaultRequestHeaders();
|
||||
@@ -98,13 +104,19 @@ public class RecorderListener implements LiveRecorder {
|
||||
}
|
||||
} catch (IOException ignored) {
|
||||
} finally {
|
||||
liveClient.getLogger().severe("Stopped recording "+liveClient.getRoomInfo().getHostName());
|
||||
liveClient.getLogger().severe("Stopped recording " + liveClient.getRoomInfo().getHostName());
|
||||
}
|
||||
} catch (Exception e) {
|
||||
e.printStackTrace();
|
||||
}
|
||||
});
|
||||
|
||||
var recordingStartedEvent = new TikTokLiveRecorderStartedEvent(downloadData);
|
||||
liveClient.publishEvent(recordingStartedEvent);
|
||||
if (recordingStartedEvent.isCanceled()) {
|
||||
liveClient.getLogger().info("Recording cancelled");
|
||||
return;
|
||||
}
|
||||
liveDownloadThread.start();
|
||||
}
|
||||
|
||||
@@ -120,32 +132,6 @@ public class RecorderListener implements LiveRecorder {
|
||||
liveDownloadThread.interrupt();
|
||||
}
|
||||
|
||||
private int terminateFfmpeg(final Process process) {
|
||||
if (!process.isAlive()) {
|
||||
// ffmpeg -version, do nothing
|
||||
return process.exitValue();
|
||||
}
|
||||
|
||||
// ffmpeg -f x11grab
|
||||
System.out.println("About to destroy the child process...");
|
||||
try (final OutputStreamWriter out = new OutputStreamWriter(process.getOutputStream(), UTF_8)) {
|
||||
out.write('q');
|
||||
} catch (final IOException ioe) {
|
||||
ioe.printStackTrace();
|
||||
}
|
||||
try {
|
||||
if (!process.waitFor(5L, TimeUnit.SECONDS)) {
|
||||
process.destroy();
|
||||
process.waitFor();
|
||||
}
|
||||
return process.exitValue();
|
||||
} catch (InterruptedException ie) {
|
||||
System.out.println("Interrupted");
|
||||
ie.printStackTrace();
|
||||
Thread.currentThread().interrupt();
|
||||
return -1;
|
||||
}
|
||||
}
|
||||
|
||||
private DownloadData mapToDownloadData(String json) {
|
||||
|
||||
|
||||
@@ -26,10 +26,18 @@ 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;
|
||||
|
||||
@AllArgsConstructor
|
||||
@Data
|
||||
public class TikTokLiveRecorderStartedEvent extends TikTokEvent
|
||||
{
|
||||
@Getter
|
||||
public class TikTokLiveRecorderStartedEvent extends TikTokEvent {
|
||||
DownloadData downloadData;
|
||||
|
||||
@Setter
|
||||
boolean canceled;
|
||||
|
||||
public TikTokLiveRecorderStartedEvent(DownloadData downloadData) {
|
||||
this.downloadData = downloadData;
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user