正在查看: 哔哩哔哩 v1.6.6 应用的 DanmakuClient.java JAVA 源代码文件
本页面展示 JAVA 反编译生成的源代码文件,支持语法高亮显示。 仅供安全研究与技术分析使用,严禁用于任何非法用途。请遵守相关法律法规。
正在查看: 哔哩哔哩 v1.6.6 应用的 DanmakuClient.java JAVA 源代码文件
本页面展示 JAVA 反编译生成的源代码文件,支持语法高亮显示。 仅供安全研究与技术分析使用,严禁用于任何非法用途。请遵守相关法律法规。
package mybl;
import android.content.Context;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.text.SpannableStringBuilder;
import android.text.style.AbsoluteSizeSpan;
import android.text.style.ImageSpan;
import android.util.DisplayMetrics;
import bl.abd;
import bl.pz;
import bl.qa;
import bl.qb;
import bl.qe;
import com.bilibili.tv.MainApplication;
import java.io.ByteArrayInputStream;
import java.io.ByteArrayOutputStream;
import java.io.DataInputStream;
import java.io.DataOutputStream;
import java.io.IOException;
import java.net.URI;
import java.net.URL;
import java.util.Iterator;
import java.util.concurrent.Callable;
import java.util.concurrent.Executors;
import java.util.zip.InflaterOutputStream;
import org.json.JSONObject;
import tv.danmaku.ijk.media.player.IjkMediaPlayer;
import tv.danmaku.videoplayer.core.danmaku.DanmakuConfig;
import tv.danmaku.videoplayer.core.danmaku.IDanmakuPlayer;
import tv.danmaku.videoplayer.core.danmaku.comment.DrawableItem;
public class DanmakuClient {
public static float baseScreenScale = 0.0f;
public static float densityScale = 0.0f;
public static int mAlpha;
public static float mScale;
public static IDanmakuPlayer player;
public DanmakuWebSocketClient client;
public Thread client_thread;
public int roomId;
public String token;
public DanmakuClient(int i) {
if (baseScreenScale == 0.0f) {
Context applicationContext = MainApplication.a().getApplicationContext();
DisplayMetrics displayMetrics = applicationContext.getResources().getDisplayMetrics();
baseScreenScale = (displayMetrics.heightPixels / 15.0f) / 25.0f;
densityScale = displayMetrics.density;
mScale = abd.f(applicationContext);
mAlpha = (int) (abd.g(applicationContext) * 255.0f);
}
this.roomId = i;
try {
final JSONObject jSONObject = (JSONObject) Executors.newSingleThreadExecutor().submit(new Callable<JSONObject>() {
@Override
public JSONObject call() {
return ((Response) pz.a(new qa.a(Response.class).a("https://api.live.bilibili.com/xlive/web-room/v1/index/getDanmuInfo").a(true).b("id", String.valueOf(DanmakuClient.this.roomId)).a(new qb()).a(), "GET")).e();
}
}).get();
this.token = jSONObject.optString("token");
this.client_thread = new Thread(new Runnable() {
@Override
public void run() {
DanmakuClient.this.startClient("ws://" + jSONObject.optJSONArray("host_list").optJSONObject(0).optString(IjkMediaPlayer.OnNativeInvokeListener.ARG_DNS_HOST) + ":" + jSONObject.optJSONArray("host_list").optJSONObject(0).optInt("ws_port") + "/sub");
}
});
this.client_thread.start();
} catch (Exception e) {
e.printStackTrace();
}
}
public void startClient(String str) {
String str2 = "{\"uid\": 0, \"roomid\": " + String.valueOf(this.roomId) + ",\"buvid\":\"00000000-0000-0000-0000-00000000000000000infoc\", \"protover\": 2, \"platform\": \"web\", \"type\": 2, \"key\": \"" + this.token + "\"}";
try {
this.client = new DanmakuWebSocketClient(new URI(str));
this.client.connectBlocking();
ByteArrayOutputStream byteArrayOutputStream = new ByteArrayOutputStream();
DataOutputStream dataOutputStream = new DataOutputStream(byteArrayOutputStream);
dataOutputStream.writeInt(str2.length() + 16);
dataOutputStream.writeShort(16);
dataOutputStream.writeShort(0);
dataOutputStream.writeInt(7);
dataOutputStream.writeInt(0);
dataOutputStream.write(str2.getBytes());
this.client.send(byteArrayOutputStream.toByteArray());
while (!this.client.isClosed()) {
ByteArrayOutputStream byteArrayOutputStream2 = new ByteArrayOutputStream();
DataOutputStream dataOutputStream2 = new DataOutputStream(byteArrayOutputStream2);
dataOutputStream2.writeInt(16);
dataOutputStream2.writeShort(16);
dataOutputStream2.writeShort(0);
dataOutputStream2.writeInt(2);
dataOutputStream2.writeInt(0);
this.client.send(byteArrayOutputStream2.toByteArray());
Thread.sleep(20000L);
}
} catch (Exception e) {
e.printStackTrace();
}
}
class AnonymousClass3 implements Runnable {
final DataInputStream val$inFromServer;
AnonymousClass3(DataInputStream dataInputStream) {
this.val$inFromServer = dataInputStream;
}
@Override
public void run() {
DanmakuClient.this.readClient(this.val$inFromServer);
}
}
public static void parse(byte[] bArr) {
DataInputStream dataInputStream = new DataInputStream(new ByteArrayInputStream(bArr));
while (dataInputStream.available() > 0) {
try {
int readInt = dataInputStream.readInt();
short readShort = dataInputStream.readShort();
short readShort2 = dataInputStream.readShort();
int readInt2 = dataInputStream.readInt();
dataInputStream.readInt();
byte[] bArr2 = new byte[readInt - readShort];
dataInputStream.read(bArr2);
if (readShort2 == 2) {
parse(decompress_zlib(bArr2));
}
if (readShort2 == 0 && readInt2 != 3) {
JSONObject jSONObject = new JSONObject(new String(bArr2));
if (jSONObject.optString("cmd").equals("DANMU_MSG")) {
JSONObject jSONObject2 = new JSONObject(jSONObject.optJSONArray("info").getJSONArray(0).getJSONObject(15).optString("extra"));
int optInt = jSONObject2.optInt("color");
if (jSONObject2.optInt("mode") == 0) {
}
int optInt2 = jSONObject2.optInt("dm_type");
int optInt3 = jSONObject2.optInt("font_size");
JSONObject optJSONObject = jSONObject2.optJSONObject("emots");
String optString = jSONObject2.optString("content");
DrawableItem drawableItem = new DrawableItem();
SpannableStringBuilder spannableStringBuilder = new SpannableStringBuilder(optString + " ");
spannableStringBuilder.setSpan(new AbsoluteSizeSpan((int) (optInt3 * baseScreenScale * mScale)), 0, optString.length() + 1, 33);
spannableStringBuilder.setSpan(new StrokedSpan(mAlpha, (optInt & 16777215) | DanmakuConfig.SHADOW_COLOR_DEFAULT, DanmakuConfig.SHADOW_COLOR_DEFAULT), 0, optString.length(), 33);
if (optJSONObject != null) {
Iterator<String> keys = optJSONObject.keys();
while (keys.hasNext()) {
String next = keys.next();
int optInt4 = optJSONObject.optJSONObject(next).optInt("count");
Bitmap decodeStream = BitmapFactory.decodeStream(new URL(optJSONObject.optJSONObject(next).optString("url")).openStream());
float height = (((optInt3 * baseScreenScale) * densityScale) * mScale) / decodeStream.getHeight();
Bitmap createScaledBitmap = Bitmap.createScaledBitmap(decodeStream, (int) (decodeStream.getWidth() * height), (int) (height * decodeStream.getHeight()), true);
int i = 0;
for (int i2 = 0; i2 < optInt4; i2++) {
spannableStringBuilder.setSpan(new ImageSpan(createScaledBitmap), optString.indexOf(next, i), optString.indexOf(next, i) + next.length(), 33);
i = optString.indexOf(next, i) + next.length();
}
}
}
if (optInt2 == 1) {
Bitmap decodeStream2 = BitmapFactory.decodeStream(new URL(jSONObject.optJSONArray("info").getJSONArray(0).getJSONObject(13).optString("url")).openStream());
float height2 = ((((1.5f * optInt3) * baseScreenScale) * densityScale) * mScale) / decodeStream2.getHeight();
spannableStringBuilder.setSpan(new ImageSpan(Bitmap.createScaledBitmap(decodeStream2, (int) (decodeStream2.getWidth() * height2), (int) (height2 * decodeStream2.getHeight()), true)), 0, optString.length(), 33);
}
drawableItem.mSpannableString = spannableStringBuilder;
player.onDanmakuAppended(drawableItem);
Thread.sleep(100L);
}
}
} catch (Exception e) {
e.printStackTrace();
return;
}
}
}
public static byte[] decompress_zlib(byte[] bArr) {
ByteArrayOutputStream byteArrayOutputStream = new ByteArrayOutputStream();
InflaterOutputStream inflaterOutputStream = new InflaterOutputStream(byteArrayOutputStream);
try {
inflaterOutputStream.write(bArr);
inflaterOutputStream.close();
} catch (IOException e) {
e.printStackTrace();
}
return byteArrayOutputStream.toByteArray();
}
public void release() {
if (this.client != null && !this.client.isClosed()) {
try {
this.client.close();
this.client_thread.interrupt();
} catch (Exception e) {
e.printStackTrace();
}
}
}
public static class Response extends qe {
public JSONObject e() {
try {
if (a()) {
JSONObject optJSONObject = new JSONObject(new String(this.b)).optJSONObject("data");
if (optJSONObject != null) {
return optJSONObject;
}
}
return null;
} catch (Exception e) {
return null;
}
}
}
}