初始化 TTVideoPlayer 工程:模块化播放框架、同同影音 App 与文档
Media3 内核播放框架(player-api/core/media3/source/render/lifecycle/telemetry)、 WebDAV/SMB/Jellyfin 来源、DLNA/Google Cast 投屏、RTMP 原生库与完整 App。
This commit is contained in:
commit
922ec80bcc
147 files changed
+20169
No files matched your search
@@ -0,0 +1 @@
|
||||
<manifest />
|
||||
@@ -0,0 +1,68 @@
|
||||
package com.ttstd.player.library.http
|
||||
|
||||
import com.ttstd.player.library.LibraryException
|
||||
import kotlinx.coroutines.suspendCancellableCoroutine
|
||||
import okhttp3.*
|
||||
import java.io.IOException
|
||||
import java.io.ByteArrayInputStream
|
||||
import javax.xml.parsers.DocumentBuilderFactory
|
||||
import org.w3c.dom.Element
|
||||
import kotlin.coroutines.resume
|
||||
import kotlin.coroutines.resumeWithException
|
||||
|
||||
/** 不自动跨域重定向,避免认证头传到其他域名;不安装任何日志拦截器。 */
|
||||
object LibraryHttp {
|
||||
val client: OkHttpClient = OkHttpClient.Builder().followRedirects(false).followSslRedirects(false)
|
||||
.connectTimeout(10, java.util.concurrent.TimeUnit.SECONDS)
|
||||
.readTimeout(20, java.util.concurrent.TimeUnit.SECONDS)
|
||||
.callTimeout(30, java.util.concurrent.TimeUnit.SECONDS).build()
|
||||
|
||||
suspend fun execute(request: Request): Response = suspendCancellableCoroutine { continuation ->
|
||||
val call = client.newCall(request)
|
||||
continuation.invokeOnCancellation { call.cancel() }
|
||||
call.enqueue(object : Callback {
|
||||
override fun onFailure(call: Call, e: IOException) {
|
||||
if (!continuation.isCancelled) continuation.resumeWithException(LibraryException("网络连接失败"))
|
||||
}
|
||||
override fun onResponse(call: Call, response: Response) {
|
||||
if (continuation.isCancelled) response.close() else continuation.resume(response)
|
||||
}
|
||||
})
|
||||
}
|
||||
|
||||
fun checkedBody(response: Response, maxBytes: Long = 4 * 1024 * 1024): String {
|
||||
if (!response.isSuccessful) throw LibraryException("服务返回 HTTP ${response.code}")
|
||||
val body = response.body ?: throw LibraryException("服务未返回内容")
|
||||
if (body.contentLength() > maxBytes) throw LibraryException("服务响应过大")
|
||||
val input = body.byteStream()
|
||||
val output = java.io.ByteArrayOutputStream()
|
||||
val buffer = ByteArray(8192)
|
||||
while (true) {
|
||||
val read = input.read(buffer)
|
||||
if (read < 0) break
|
||||
if (output.size().toLong() + read > maxBytes) throw LibraryException("服务响应过大")
|
||||
output.write(buffer, 0, read)
|
||||
}
|
||||
return output.toString("UTF-8")
|
||||
}
|
||||
}
|
||||
|
||||
/** WebDAV 与 UPnP 共享的命名空间感知 XML 解析。拒绝 DTD,禁用外部实体。 */
|
||||
object SafeXml {
|
||||
fun parse(text: String): Element {
|
||||
require(!text.contains("<!DOCTYPE", true) && !text.contains("<!ENTITY", true)) { "不接受 XML 实体声明" }
|
||||
val factory = DocumentBuilderFactory.newInstance().apply {
|
||||
isNamespaceAware = true
|
||||
isExpandEntityReferences = false
|
||||
runCatching { setFeature("http://xml.org/sax/features/external-general-entities", false) }
|
||||
runCatching { setFeature("http://xml.org/sax/features/external-parameter-entities", false) }
|
||||
}
|
||||
return factory.newDocumentBuilder().parse(ByteArrayInputStream(text.toByteArray())).documentElement
|
||||
}
|
||||
fun elements(parent: Element, name: String): List<Element> {
|
||||
val nodes = parent.getElementsByTagNameNS("*", name)
|
||||
return (0 until nodes.length).map { nodes.item(it) as Element }
|
||||
}
|
||||
fun text(parent: Element, name: String): String = elements(parent, name).firstOrNull()?.textContent.orEmpty()
|
||||
fun escape(text: String) = text.replace("&", "&").replace("<", "<").replace(">", ">").replace("\"", """).replace("'", "'")
|
||||
}
|
||||
Reference in new issue
Block a user