初始化 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,16 @@
|
||||
plugins { id("com.android.library"); kotlin("android") }
|
||||
android {
|
||||
namespace = "com.ttstd.player.librarywebdav"
|
||||
compileSdk = 37
|
||||
defaultConfig { minSdk = 23 }
|
||||
testOptions { unitTests.isIncludeAndroidResources = true }
|
||||
compileOptions { sourceCompatibility = JavaVersion.VERSION_17; targetCompatibility = JavaVersion.VERSION_17 }
|
||||
}
|
||||
kotlin { jvmToolchain(17) }
|
||||
dependencies {
|
||||
api(project(":library-http"))
|
||||
implementation("org.jetbrains.kotlinx:kotlinx-coroutines-android:1.10.2")
|
||||
testImplementation("org.robolectric:robolectric:4.14.1")
|
||||
testImplementation("com.squareup.okhttp3:mockwebserver:4.12.0")
|
||||
testImplementation("junit:junit:4.13.2")
|
||||
}
|
||||
@@ -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)) })
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,51 @@
|
||||
package com.ttstd.player.library.webdav
|
||||
|
||||
import com.ttstd.player.library.*
|
||||
import kotlinx.coroutines.runBlocking
|
||||
import okhttp3.Credentials
|
||||
import okhttp3.mockwebserver.*
|
||||
import org.junit.Assert.*
|
||||
import org.junit.Test
|
||||
import org.junit.runner.RunWith
|
||||
import org.robolectric.RobolectricTestRunner
|
||||
import org.robolectric.annotation.Config
|
||||
|
||||
@RunWith(RobolectricTestRunner::class)
|
||||
@Config(sdk = [28])
|
||||
class WebDavLibraryTest {
|
||||
private val listing = """<d:multistatus xmlns:d="DAV:">
|
||||
<d:response><d:href>/dav/</d:href><d:propstat><d:prop><d:resourcetype><d:collection/></d:resourcetype></d:prop><d:status>HTTP/1.1 200 OK</d:status></d:propstat></d:response>
|
||||
<d:response><d:href>/dav/Movie.mp4</d:href><d:propstat><d:prop><d:displayname>Movie.mp4</d:displayname><d:getcontentlength>123</d:getcontentlength><d:getetag>v1</d:getetag></d:prop><d:status>HTTP/1.1 200 OK</d:status></d:propstat></d:response>
|
||||
<d:response><d:href>/dav/Movie.zh.srt</d:href><d:propstat><d:prop><d:displayname>Movie.zh.srt</d:displayname></d:prop><d:status>HTTP/1.1 200 OK</d:status></d:propstat></d:response>
|
||||
<d:response><d:href>http://other.test/steal</d:href><d:propstat><d:prop/><d:status>HTTP/1.1 200 OK</d:status></d:propstat></d:response>
|
||||
</d:multistatus>"""
|
||||
@Test fun browsesAuthenticatedDirectoryAndResolvesSubtitles() = runBlocking {
|
||||
MockWebServer().use { server ->
|
||||
server.enqueue(MockResponse().setResponseCode(207).setBody(listing))
|
||||
server.enqueue(MockResponse().setResponseCode(207).setBody(listing))
|
||||
server.start()
|
||||
val library = WebDavLibrary(LibraryAccount("id", "DAV", LibraryKind.WEBDAV, server.url("/dav/").toString(), "user", "secret"))
|
||||
val entries = library.list().entries
|
||||
assertEquals(2, entries.size)
|
||||
val playback = library.resolve(entries.first { it.name == "Movie.mp4" })
|
||||
assertEquals(123L, playback.contentLength)
|
||||
assertEquals(1, playback.request.subtitles.size)
|
||||
assertEquals("application/x-subrip", playback.request.subtitles[0].mimeType)
|
||||
assertNotNull(playback.request.cacheKey)
|
||||
assertNull(playback.receiverRequest)
|
||||
val request = server.takeRequest()
|
||||
assertEquals("PROPFIND", request.method)
|
||||
assertEquals("1", request.getHeader("Depth"))
|
||||
assertEquals(Credentials.basic("user", "secret"), request.getHeader("Authorization"))
|
||||
}
|
||||
}
|
||||
@Test fun rejectsRedirectInsteadOfForwardingCredentials() = runBlocking {
|
||||
MockWebServer().use { server ->
|
||||
server.enqueue(MockResponse().setResponseCode(302).setHeader("Location", "https://other.test/dav"))
|
||||
server.start()
|
||||
val library = WebDavLibrary(LibraryAccount("id", "DAV", LibraryKind.WEBDAV, server.url("/dav/").toString(), "user", "secret"))
|
||||
assertTrue(runCatching { library.list() }.exceptionOrNull() is LibraryException)
|
||||
assertEquals(1, server.requestCount)
|
||||
}
|
||||
}
|
||||
}
|
||||
Reference in new issue
Block a user