Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -36,15 +36,13 @@ public class LiveClientSettings {
/**
* ISO-Language for Client
*/

private String clientLanguage;

/**
* Whether to Retry if Connection Fails
*/
private boolean retryOnConnectionFailure;


/**
* Before retrying connect, wait for select amount of time
*/
Expand All @@ -53,42 +51,35 @@ public class LiveClientSettings {
/**
* Whether to print Logs to Console
*/

private boolean printToConsole = true;

/**
* LoggingLevel for Logs
*/
private Level logLevel;


/**
* Optional: Use it if you need to change TikTok live hostname in builder
*/
private String hostName;


/**
* Parameters used in requests to TikTok api
*/
private HttpClientSettings httpSettings;


/*
/**
* Optional: Sometimes not every messages from chat are send to TikTokLiveJava to fix this issue you can set sessionId
* documentation how to obtain sessionId https://github.com/isaackogan/TikTok-Live-Connector#send-chat-messages
*/
private String sessionId;

/*
/**
* Optional: By default roomID is fetched before connect to live, but you can set it manually
*
*/
private String roomId;





public static LiveClientSettings createDefault()
{
var httpSettings = new HttpClientSettings();
Expand All @@ -103,12 +94,10 @@ public static LiveClientSettings createDefault()
clientSettings.setPrintToConsole(false);
clientSettings.setLogLevel(Level.ALL);


clientSettings.setHttpSettings(httpSettings);
return clientSettings;
}


/**
* Default Parameters for HTTP-Request
*/
Expand Down Expand Up @@ -147,11 +136,9 @@ public static Map<String, Object> DefaultClientParams() {
clientParams.put("webcast_sdk_version", "1.3.0");
clientParams.put("update_version_code", "1.3.0");


return clientParams;
}


/**
* Default Headers for HTTP-Request
*/
Expand All @@ -167,6 +154,4 @@ public static Map<String, String> DefaultRequestHeaders() {
headers.put("Accept-Language", "en-US,en; q=0.9");
return headers;
}

}

}
Original file line number Diff line number Diff line change
Expand Up @@ -87,6 +87,7 @@ public ProxyData rotate() {
@Override
public void remove() {
proxyList.remove(index);
lastSuccess = false; // index is no longer valid and lastSuccess needs falsified
}

public void setIndex(int index) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@
import io.github.jwdeveloper.tiktok.data.dto.ProxyData;
import io.github.jwdeveloper.tiktok.data.requests.LiveConnectionData;
import io.github.jwdeveloper.tiktok.data.settings.*;
import io.github.jwdeveloper.tiktok.exceptions.TikTokLiveException;
import io.github.jwdeveloper.tiktok.exceptions.*;
import io.github.jwdeveloper.tiktok.live.LiveClient;
import org.java_websocket.client.WebSocketClient;

Expand All @@ -41,7 +41,7 @@ public class TikTokWebSocketClient implements SocketClient {
private final TikTokLiveEventHandler tikTokEventHandler;
private WebSocketClient webSocketClient;

private TikTokWebSocketPingingTask pingingTask;
private final TikTokWebSocketPingingTask pingingTask;
private boolean isConnected;

public TikTokWebSocketClient(
Expand Down Expand Up @@ -91,13 +91,28 @@ private void connectDefault() {
}

public void connectProxy(ProxyClientSettings proxySettings) {
try {
if (proxySettings.getType() == Proxy.Type.SOCKS) {
SSLContext sc = SSLContext.getInstance("SSL");
sc.init(null, new TrustManager[]{new X509TrustManager() {
public void checkClientTrusted(X509Certificate[] x509Certificates, String s) {}
public void checkServerTrusted(X509Certificate[] x509Certificates, String s) {}
public X509Certificate[] getAcceptedIssuers() { return null; }
}}, null);
webSocketClient.setSocketFactory(sc.getSocketFactory());
}
} catch (Exception e) {
// This will never be thrown.
throw new TikTokProxyRequestException("Unable to set Socks proxy SSL instance");
}
while (proxySettings.hasNext()) {
ProxyData proxyData = proxySettings.next();
if (!tryProxyConnection(proxySettings, proxyData)) {
if (proxySettings.isAutoDiscard())
proxySettings.remove();
continue;
}
pingingTask.run(webSocketClient);
isConnected = true;
break;
}
Expand All @@ -107,21 +122,6 @@ public void connectProxy(ProxyClientSettings proxySettings) {

public boolean tryProxyConnection(ProxyClientSettings proxySettings, ProxyData proxyData) {
try {
if (proxySettings.getType() == Proxy.Type.SOCKS) {
SSLContext sc = SSLContext.getInstance("SSL");
sc.init(null, new TrustManager[]{new X509TrustManager() {
public void checkClientTrusted(X509Certificate[] x509Certificates, String s) {
}

public void checkServerTrusted(X509Certificate[] x509Certificates, String s) {
}

public X509Certificate[] getAcceptedIssuers() {
return null;
}
}}, null);
webSocketClient.setSocketFactory(sc.getSocketFactory());
}
webSocketClient.setProxy(new Proxy(proxySettings.getType(), proxyData.toSocketAddress()));
webSocketClient.connect();
return true;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -11,37 +11,28 @@ public class TikTokWebSocketPingingTask
private final int MIN_TIMEOUT = 250;
private final int MAX_TIMEOUT = 500;


public void run(WebSocket webSocket)
{
stop();
thread = new Thread(() ->
{
pingTask(webSocket);
});
isRunning =true;
thread = new Thread(() -> pingTask(webSocket));
isRunning = true;
thread.start();
}

public void stop()
{
if(thread != null)
{
if (thread != null)
thread.interrupt();
}
isRunning = false;
}


private void pingTask(WebSocket webSocket)
{
var random = new Random();
while (isRunning)
{
try
{
if(!webSocket.isOpen())
{
while (isRunning) {
try {
if (!webSocket.isOpen()) {
Thread.sleep(100);
continue;
}
Expand All @@ -50,11 +41,10 @@ private void pingTask(WebSocket webSocket)
var timeout = random.nextInt(MAX_TIMEOUT)+MIN_TIMEOUT;
Thread.sleep(timeout);
}
catch (Exception e)
{
catch (Exception e) {
isRunning = false;
}
}

}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -24,24 +24,18 @@

import com.google.gson.JsonParser;
import io.github.jwdeveloper.tiktok.annotations.TikTokEventObserver;
import io.github.jwdeveloper.tiktok.data.events.TikTokLiveEndedEvent;
import io.github.jwdeveloper.tiktok.data.events.*;
import io.github.jwdeveloper.tiktok.data.events.http.TikTokRoomDataResponseEvent;
import io.github.jwdeveloper.tiktok.data.settings.LiveClientSettings;
import io.github.jwdeveloper.tiktok.extension.recorder.api.LiveRecorder;
import io.github.jwdeveloper.tiktok.data.events.TikTokConnectedEvent;
import io.github.jwdeveloper.tiktok.data.events.TikTokDisconnectedEvent;
import io.github.jwdeveloper.tiktok.exceptions.TikTokLiveException;
import io.github.jwdeveloper.tiktok.extension.recorder.impl.data.DownloadData;
import io.github.jwdeveloper.tiktok.extension.recorder.impl.data.RecorderSettings;
import io.github.jwdeveloper.tiktok.data.events.http.TikTokRoomDataResponseEvent;
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.http.HttpClientFactory;
import io.github.jwdeveloper.tiktok.live.LiveClient;

import javax.net.ssl.HttpsURLConnection;
import java.io.*;
import java.net.URL;
import java.util.List;
import java.util.concurrent.TimeUnit;
import java.util.function.Consumer;

Expand Down Expand Up @@ -86,13 +80,11 @@ private void onResponse(LiveClient liveClient, TikTokRoomDataResponseEvent event
throw new TikTokLiveException("Unable to find download live url!");
}
liveClient.getLogger().info("Live download url found!");

}

@TikTokEventObserver
private void onConnected(LiveClient liveClient, TikTokConnectedEvent event) {
liveDownloadThread = new Thread(() ->
{
liveDownloadThread = new Thread(() -> {
try {
var bufferSize = 1024;
var url = new URL(downloadData.getFullUrl());
Expand All @@ -102,66 +94,57 @@ private void onConnected(LiveClient liveClient, TikTokConnectedEvent event) {
socksConnection.setRequestProperty(entry.getKey(), entry.getValue());
}

try (var in = new BufferedInputStream(socksConnection.getInputStream())) {
var path = settings.getOutputPath() + File.separator + settings.getOutputFileName();
var file = new File(path);
file.getParentFile().mkdirs();
var fileOutputStream = new FileOutputStream(file);
byte dataBuffer[] = new byte[bufferSize];
int bytesRead;
while ((bytesRead = in.read(dataBuffer, 0, bufferSize)) != -1) {
fileOutputStream.write(dataBuffer, 0, bytesRead);
}
} catch (IOException e) {
throw e;
var in = new BufferedInputStream(socksConnection.getInputStream());
var path = settings.getOutputPath() + File.separator + settings.getOutputFileName();
var file = new File(path);
file.getParentFile().mkdirs();
var fileOutputStream = new FileOutputStream(file);
byte[] dataBuffer = new byte[bufferSize];
int bytesRead;
while ((bytesRead = in.read(dataBuffer, 0, bufferSize)) != -1) {
fileOutputStream.write(dataBuffer, 0, bytesRead);
}
} catch (Exception e) {
in.close();
} catch (Exception e) {
e.printStackTrace();
;
}

});


liveDownloadThread.start();
}


private static void downloadUsingStream(String urlStr, String file) throws IOException {
URL url = new URL(urlStr);
BufferedInputStream bis = new BufferedInputStream(url.openStream());
FileOutputStream fis = new FileOutputStream(file);
byte[] buffer = new byte[1024];
int count = 0;
while ((count = bis.read(buffer, 0, 1024)) != -1) {
int count;
while ((count = bis.read(buffer, 0, 1024)) != -1)
fis.write(buffer, 0, count);
}
fis.close();
bis.close();
}


@TikTokEventObserver
private void onDisconnected(LiveClient liveClient, TikTokDisconnectedEvent event) {
liveDownloadThread.interrupt();
if (isConnected())
liveDownloadThread.interrupt();
}

@TikTokEventObserver
private void onDisconnected(LiveClient liveClient, TikTokLiveEndedEvent event) {
liveDownloadThread.interrupt();
if (isConnected())
liveDownloadThread.interrupt();
}

private int terminateFfmpeg(final Process process) {
if (!process.isAlive()) {
/*
* ffmpeg -version, do nothing
*/
// ffmpeg -version, do nothing
return process.exitValue();
}

/*
* ffmpeg -f x11grab
*/
// 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');
Expand All @@ -174,7 +157,7 @@ private int terminateFfmpeg(final Process process) {
process.waitFor();
}
return process.exitValue();
} catch (final InterruptedException ie) {
} catch (InterruptedException ie) {
System.out.println("Interrupted");
ie.printStackTrace();
Thread.currentThread().interrupt();
Expand All @@ -201,12 +184,10 @@ private DownloadData mapToDownloadData(String json) {
.get("flv")
.getAsString();


var sessionId = streamDataJsonObject.getAsJsonObject("common")
.get("session_id")
.getAsString();


//main
//https://pull-f5-tt03.fcdn.eu.tiktokcdn.com/stage/stream-3284937501738533765.flv?session_id=136-20240109000954BF818F1B3A8E5E39E238&_webnoredir=1
//Working
Expand All @@ -216,5 +197,7 @@ private DownloadData mapToDownloadData(String json) {
return new DownloadData(urlLink, sessionId);
}


}
private boolean isConnected() {
return liveDownloadThread != null && liveDownloadThread.isAlive();
}
}
Loading