初始化 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,59 @@
|
||||
package com.ttstd.player.library.webdav
|
||||
|
||||
import com.ttstd.player.api.*
|
||||
import com.ttstd.player.library.*
|
||||
import com.ttstd.player.library.http.*
|
||||
import kotlinx.coroutines.Dispatchers
|
||||
import kotlinx.coroutines.withContext
|
||||
import okhttp3.*
|
||||
import okhttp3.HttpUrl.Companion.toHttpUrl
|
||||
import okhttp3.MediaType.Companion.toMediaType
|
||||
import okhttp3.RequestBody.Companion.toRequestBody
|
||||
|
||||
/** 支持 Basic 认证和匿名 WebDAV。href 必须同源且处于配置根目录内。 */
|
||||
class WebDavLibrary(override val account: LibraryAccount) : MediaLibrary {
|
||||
private val root = (account.endpoint.trimEnd('/') + "/").toHttpUrl()
|
||||
private val headers = if (account.username.isEmpty()) emptyMap() else mapOf("Authorization" to Credentials.basic(account.username, account.password))
|
||||
|
||||
private fun url(id: String): HttpUrl {
|
||||
val result = if (id.isEmpty()) root else id.toHttpUrl()
|
||||
require(result.scheme == root.scheme && result.host == root.host && result.port == root.port &&
|
||||
result.encodedPath.startsWith(root.encodedPath)) { "目录超出来源范围" }
|
||||
return result
|
||||
}
|
||||
override suspend fun list(folderId: String, offset: Int): LibraryPage = withContext(Dispatchers.IO) {
|
||||
val target = url(folderId)
|
||||
val xml = """<?xml version="1.0"?><d:propfind xmlns:d="DAV:"><d:prop><d:displayname/><d:resourcetype/><d:getcontentlength/><d:getetag/><d:getlastmodified/></d:prop></d:propfind>"""
|
||||
val request = Request.Builder().url(target).method("PROPFIND", xml.toRequestBody("application/xml; charset=utf-8".toMediaType()))
|
||||
.header("Depth", "1").apply { headers.forEach { (k, v) -> header(k, v) } }.build()
|
||||
LibraryHttp.execute(request).use { response ->
|
||||
val document = SafeXml.parse(LibraryHttp.checkedBody(response))
|
||||
val entries = SafeXml.elements(document, "response").mapNotNull { item ->
|
||||
val href = SafeXml.text(item, "href")
|
||||
val child = target.resolve(href) ?: return@mapNotNull null
|
||||
if (runCatching { url(child.toString()) }.isFailure || child.encodedPath.trimEnd('/') == target.encodedPath.trimEnd('/')) return@mapNotNull null
|
||||
val propstat = SafeXml.elements(item, "propstat").firstOrNull { SafeXml.text(it, "status").contains(" 200 ") } ?: return@mapNotNull null
|
||||
val name = SafeXml.text(propstat, "displayname").ifBlank { child.pathSegments.lastOrNull { it.isNotEmpty() }.orEmpty() }
|
||||
LibraryEntry(child.toString(), name, SafeXml.elements(propstat, "collection").isNotEmpty(),
|
||||
SafeXml.text(propstat, "getcontentlength").toLongOrNull(),
|
||||
SafeXml.text(propstat, "getetag").ifBlank { SafeXml.text(propstat, "getlastmodified") })
|
||||
}.sortedWith(compareBy<LibraryEntry> { !it.isFolder }.thenBy { it.name.lowercase() })
|
||||
LibraryPage(entries)
|
||||
}
|
||||
}
|
||||
|
||||
override suspend fun resolve(entry: LibraryEntry, options: LibraryPlaybackOptions): LibraryPlayback {
|
||||
require(!entry.isFolder)
|
||||
val target = url(entry.id)
|
||||
val parent = target.newBuilder().removePathSegment(target.pathSegments.lastIndex).addPathSegment("").build()
|
||||
val siblings = list(parent.toString()).entries
|
||||
val fresh = siblings.firstOrNull { it.id == entry.id } ?: entry
|
||||
val subtitles = siblings.filter { !it.isFolder && LibraryFiles.matchesSubtitle(entry.name, it.name) }
|
||||
.map { ExternalSubtitle(url(it.id).toString(), LibraryFiles.subtitleMime(it.name)!!) }
|
||||
val key = "${account.id}:${fresh.id}:${fresh.version}:${fresh.size}"
|
||||
val request = PlaybackRequest(target.toString(), headers, options.startPositionMs, true, subtitles,
|
||||
cacheKey = key)
|
||||
return LibraryPlayback(request, key, fresh.name, fresh.size, subtitles = subtitles,
|
||||
receiverRequest = request.takeIf { headers.isEmpty() }, refresh = { position -> resolve(fresh, options.copy(startPositionMs = position)) })
|
||||
}
|
||||
}
|
||||
Reference in new issue
Block a user