Skip to content

Commit 966d2f6

Browse files
committed
- improve recorder
1 parent 92fde03 commit 966d2f6

File tree

2 files changed

+27
-33
lines changed

2 files changed

+27
-33
lines changed

extension-recorder/src/main/java/io/github/jwdeveloper/tiktok/extension/recorder/impl/RecorderListener.java

Lines changed: 16 additions & 30 deletions
Original file line numberDiff line numberDiff line change
@@ -30,6 +30,7 @@
3030
import io.github.jwdeveloper.tiktok.extension.recorder.api.LiveRecorder;
3131
import io.github.jwdeveloper.tiktok.extension.recorder.impl.data.*;
3232
import io.github.jwdeveloper.tiktok.extension.recorder.impl.enums.LiveQuality;
33+
import io.github.jwdeveloper.tiktok.extension.recorder.impl.event.TikTokLiveRecorderStartedEvent;
3334
import io.github.jwdeveloper.tiktok.live.LiveClient;
3435
import io.github.jwdeveloper.tiktok.models.ConnectionState;
3536

@@ -60,7 +61,9 @@ private void onResponse(LiveClient liveClient, TikTokRoomDataResponseEvent event
6061
var json = event.getLiveData().getJson();
6162

6263
liveClient.getLogger().info("Searching for live download url");
63-
downloadData = settings.getPrepareDownloadData() != null ? settings.getPrepareDownloadData().apply(json) : mapToDownloadData(json);
64+
downloadData = settings.getPrepareDownloadData() != null ?
65+
settings.getPrepareDownloadData().apply(json) :
66+
mapToDownloadData(json);
6467

6568
if (downloadData.getDownloadLiveUrl().isEmpty())
6669
liveClient.getLogger().warning("Unable to find download live url!");
@@ -72,8 +75,11 @@ private void onResponse(LiveClient liveClient, TikTokRoomDataResponseEvent event
7275
private void onConnected(LiveClient liveClient, TikTokConnectedEvent event) {
7376
if (isConnected())
7477
return;
78+
79+
7580
liveDownloadThread = new Thread(() -> {
7681
try {
82+
liveClient.getLogger().info("Recording started");
7783
var url = new URL(downloadData.getFullUrl());
7884
HttpsURLConnection connection = (HttpsURLConnection) url.openConnection();
7985
var headers = LiveClientSettings.DefaultRequestHeaders();
@@ -87,8 +93,8 @@ private void onConnected(LiveClient liveClient, TikTokConnectedEvent event) {
8793
file.createNewFile();
8894

8995
try (
90-
var in = connection.getInputStream();
91-
var fos = new FileOutputStream(file)
96+
var in = connection.getInputStream();
97+
var fos = new FileOutputStream(file)
9298
) {
9399
byte[] dataBuffer = new byte[1024];
94100
int bytesRead;
@@ -98,13 +104,19 @@ private void onConnected(LiveClient liveClient, TikTokConnectedEvent event) {
98104
}
99105
} catch (IOException ignored) {
100106
} finally {
101-
liveClient.getLogger().severe("Stopped recording "+liveClient.getRoomInfo().getHostName());
107+
liveClient.getLogger().severe("Stopped recording " + liveClient.getRoomInfo().getHostName());
102108
}
103109
} catch (Exception e) {
104110
e.printStackTrace();
105111
}
106112
});
107113

114+
var recordingStartedEvent = new TikTokLiveRecorderStartedEvent(downloadData);
115+
liveClient.publishEvent(recordingStartedEvent);
116+
if (recordingStartedEvent.isCanceled()) {
117+
liveClient.getLogger().info("Recording cancelled");
118+
return;
119+
}
108120
liveDownloadThread.start();
109121
}
110122

@@ -120,32 +132,6 @@ private void onLiveEnded(LiveClient liveClient, TikTokLiveEndedEvent event) {
120132
liveDownloadThread.interrupt();
121133
}
122134

123-
private int terminateFfmpeg(final Process process) {
124-
if (!process.isAlive()) {
125-
// ffmpeg -version, do nothing
126-
return process.exitValue();
127-
}
128-
129-
// ffmpeg -f x11grab
130-
System.out.println("About to destroy the child process...");
131-
try (final OutputStreamWriter out = new OutputStreamWriter(process.getOutputStream(), UTF_8)) {
132-
out.write('q');
133-
} catch (final IOException ioe) {
134-
ioe.printStackTrace();
135-
}
136-
try {
137-
if (!process.waitFor(5L, TimeUnit.SECONDS)) {
138-
process.destroy();
139-
process.waitFor();
140-
}
141-
return process.exitValue();
142-
} catch (InterruptedException ie) {
143-
System.out.println("Interrupted");
144-
ie.printStackTrace();
145-
Thread.currentThread().interrupt();
146-
return -1;
147-
}
148-
}
149135

150136
private DownloadData mapToDownloadData(String json) {
151137

extension-recorder/src/main/java/io/github/jwdeveloper/tiktok/extension/recorder/impl/event/TikTokLiveRecorderStartedEvent.java

Lines changed: 11 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -26,10 +26,18 @@
2626
import io.github.jwdeveloper.tiktok.extension.recorder.impl.data.DownloadData;
2727
import lombok.AllArgsConstructor;
2828
import lombok.Data;
29+
import lombok.Getter;
30+
import lombok.Setter;
2931

3032
@AllArgsConstructor
31-
@Data
32-
public class TikTokLiveRecorderStartedEvent extends TikTokEvent
33-
{
33+
@Getter
34+
public class TikTokLiveRecorderStartedEvent extends TikTokEvent {
3435
DownloadData downloadData;
36+
37+
@Setter
38+
boolean canceled;
39+
40+
public TikTokLiveRecorderStartedEvent(DownloadData downloadData) {
41+
this.downloadData = downloadData;
42+
}
3543
}

0 commit comments

Comments
 (0)