223 lines
7.5 KiB
Java
223 lines
7.5 KiB
Java
package com.baidu.cloud.media.download;
|
|
|
|
import android.content.Context;
|
|
import android.content.SharedPreferences;
|
|
import android.os.Build;
|
|
import android.text.TextUtils;
|
|
import android.util.Log;
|
|
import java.util.ArrayList;
|
|
import java.util.List;
|
|
import java.util.Scanner;
|
|
import org.json.JSONObject;
|
|
|
|
/* loaded from: classes.dex */
|
|
final class d {
|
|
private String a;
|
|
private List<String> b;
|
|
private a c;
|
|
private Context d;
|
|
private String f;
|
|
private String e = null;
|
|
private String g = null;
|
|
|
|
public static abstract class a {
|
|
public abstract void a(int i);
|
|
|
|
public abstract void a(List<String> list);
|
|
}
|
|
|
|
public d(Context context, String str) {
|
|
this.a = "";
|
|
this.b = null;
|
|
this.c = null;
|
|
this.d = context.getApplicationContext();
|
|
this.f = str;
|
|
this.a = "";
|
|
this.b = new ArrayList();
|
|
this.c = null;
|
|
}
|
|
|
|
private long a(String str) {
|
|
try {
|
|
String[] split = str.split(":|=|,");
|
|
for (int i = 0; i < split.length; i++) {
|
|
if (split[i].trim().equals("BANDWIDTH")) {
|
|
return Long.parseLong(split[i + 1].trim());
|
|
}
|
|
}
|
|
return 0L;
|
|
} catch (Exception e) {
|
|
Log.d("M3U8Parser", "" + e.getMessage());
|
|
return 0L;
|
|
}
|
|
}
|
|
|
|
private void a() {
|
|
SharedPreferences.Editor edit = this.d.getSharedPreferences("__cyberplayer_dl_sec", 0).edit();
|
|
edit.putString(this.f, this.e);
|
|
if (Build.VERSION.SDK_INT >= 9) {
|
|
edit.apply();
|
|
} else {
|
|
edit.commit();
|
|
}
|
|
}
|
|
|
|
private void a(String str, List<String> list) {
|
|
String str2;
|
|
if (str == null || TextUtils.isEmpty(str)) {
|
|
this.c.a(1);
|
|
return;
|
|
}
|
|
Scanner scanner = new Scanner(str);
|
|
StringBuilder sb = new StringBuilder();
|
|
String str3 = null;
|
|
long j = 0;
|
|
String str4 = null;
|
|
boolean z = false;
|
|
boolean z2 = false;
|
|
while (true) {
|
|
try {
|
|
String str5 = "";
|
|
if (!scanner.hasNextLine()) {
|
|
scanner.close();
|
|
if (!z) {
|
|
if (this.e != null) {
|
|
a();
|
|
}
|
|
if (com.baidu.cloud.media.download.a.a(sb.toString().getBytes(), this.f)) {
|
|
this.c.a(this.b);
|
|
return;
|
|
} else {
|
|
this.c.a(5);
|
|
return;
|
|
}
|
|
}
|
|
this.b.clear();
|
|
if (!str3.startsWith("http://")) {
|
|
str5 = this.a;
|
|
}
|
|
a(com.baidu.cloud.media.download.a.a(str5 + str3), this.b);
|
|
return;
|
|
}
|
|
String nextLine = scanner.nextLine();
|
|
if (!z2 && !nextLine.startsWith("#EXTM3U")) {
|
|
scanner.close();
|
|
this.c.a(4);
|
|
return;
|
|
}
|
|
if (TextUtils.isEmpty(str4) || !str4.startsWith("#EXT-X-STREAM-INF:")) {
|
|
if (!TextUtils.isEmpty(str4) && str4.startsWith("#EXTINF:")) {
|
|
if (!nextLine.contains("://")) {
|
|
str5 = this.a;
|
|
}
|
|
if (nextLine.startsWith("/")) {
|
|
str5 = str5.substring(0, str5.indexOf("/", str5.indexOf("://") + 3));
|
|
} else if (nextLine.startsWith("../")) {
|
|
if (str5.endsWith("/")) {
|
|
str5 = str5.substring(0, str5.length() - 1);
|
|
}
|
|
str5 = str5.substring(0, str5.lastIndexOf("/") + 1);
|
|
nextLine = nextLine.substring(3);
|
|
}
|
|
list.add(str5 + nextLine);
|
|
str2 = String.valueOf(list.size()) + ".ts\n";
|
|
} else if (TextUtils.isEmpty(nextLine) || !nextLine.startsWith("#EXT-X-KEY:")) {
|
|
str2 = nextLine + "\n";
|
|
} else {
|
|
String b = b(nextLine);
|
|
if (b == null) {
|
|
scanner.close();
|
|
this.c.a(6);
|
|
return;
|
|
} else {
|
|
str2 = b + "\n";
|
|
}
|
|
}
|
|
sb.append(str2);
|
|
str4 = nextLine;
|
|
} else {
|
|
long a2 = a(str4);
|
|
if (a2 > j) {
|
|
str3 = nextLine;
|
|
} else {
|
|
a2 = j;
|
|
}
|
|
j = a2;
|
|
str4 = nextLine;
|
|
z = true;
|
|
}
|
|
z2 = true;
|
|
} catch (Exception e) {
|
|
Log.d("M3U8Parser", Log.getStackTraceString(e));
|
|
this.c.a(2);
|
|
return;
|
|
}
|
|
}
|
|
}
|
|
|
|
private String b(String str) {
|
|
String str2;
|
|
String str3 = null;
|
|
try {
|
|
boolean contains = str.contains("=media-drm-player-binding");
|
|
boolean contains2 = str.contains("=media-drm-safe-code");
|
|
boolean contains3 = str.contains("=media-drm-token");
|
|
if (!contains && !contains2 && !contains3) {
|
|
return str;
|
|
}
|
|
if (this.e == null) {
|
|
String substring = str.substring(str.indexOf("URI=\"") + 5);
|
|
String substring2 = substring.substring(0, substring.indexOf("\""));
|
|
String str4 = "pid-android-1";
|
|
try {
|
|
String a2 = VideoDownloadManager.b().a();
|
|
if (!TextUtils.isEmpty(a2)) {
|
|
str4 = a2;
|
|
}
|
|
} catch (Exception unused) {
|
|
}
|
|
String str5 = substring2 + "&playerId=" + str4;
|
|
if (contains3) {
|
|
str5 = str5 + "&token=" + this.g;
|
|
}
|
|
this.e = LocalHlsSec.bytes2HexStr(new LocalHlsSec().crypt(this.d, LocalHlsSec.hexStr2Bytes(new JSONObject(com.baidu.cloud.media.download.a.a(str5)).getString("encryptedVideoKey")), 0));
|
|
}
|
|
if (contains) {
|
|
str2 = "media-drm-player-binding";
|
|
} else if (contains2) {
|
|
str2 = "media-drm-safe-code";
|
|
} else {
|
|
if (!contains3) {
|
|
return str;
|
|
}
|
|
str2 = "media-drm-token";
|
|
}
|
|
try {
|
|
str = str.replace(str2, "media-drm-local-key");
|
|
return str;
|
|
} catch (Exception e) {
|
|
str3 = str;
|
|
e = e;
|
|
Log.d("M3U8Parser", "", e);
|
|
return str3;
|
|
}
|
|
} catch (Exception e2) {
|
|
e = e2;
|
|
}
|
|
}
|
|
|
|
public int a(String str, String str2, a aVar) {
|
|
if (TextUtils.isEmpty(str) || aVar == null) {
|
|
return 1;
|
|
}
|
|
this.g = str2;
|
|
this.c = aVar;
|
|
this.b.clear();
|
|
if (TextUtils.isEmpty(this.a)) {
|
|
this.a = str.substring(0, str.lastIndexOf(47) + 1);
|
|
}
|
|
a(com.baidu.cloud.media.download.a.a(str), this.b);
|
|
return 0;
|
|
}
|
|
}
|