初始化 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,93 @@
|
||||
package com.ttstd.player.library.smb
|
||||
|
||||
import com.hierynomus.msdtyp.AccessMask
|
||||
import com.hierynomus.msfscc.FileAttributes
|
||||
import com.hierynomus.msfscc.fileinformation.FileStandardInformation
|
||||
import com.hierynomus.mssmb2.*
|
||||
import com.hierynomus.smbj.SMBClient
|
||||
import com.hierynomus.smbj.SmbConfig
|
||||
import com.hierynomus.smbj.auth.AuthenticationContext
|
||||
import com.hierynomus.smbj.share.DiskShare
|
||||
import com.ttstd.player.api.*
|
||||
import com.ttstd.player.library.*
|
||||
import kotlinx.coroutines.Dispatchers
|
||||
import kotlinx.coroutines.withContext
|
||||
import java.net.URI
|
||||
import java.util.EnumSet
|
||||
import java.util.concurrent.TimeUnit
|
||||
|
||||
/** SMB2/3 来源,不把密码写入 smb URI。连接超时有界,每个随机读取句柄独占连接。 */
|
||||
class SmbLibrary(override val account: LibraryAccount) : MediaLibrary {
|
||||
private val root = URI(account.endpoint)
|
||||
private val parts = root.path.trim('/').split('/').filter { it.isNotEmpty() }
|
||||
private val shareName = parts.firstOrNull() ?: throw LibraryException("SMB 地址必须包含共享名")
|
||||
private val base = parts.drop(1).joinToString("\\")
|
||||
|
||||
fun openConnection(): SmbConnection {
|
||||
require(root.scheme.equals("smb", true) && root.host != null && root.userInfo == null)
|
||||
val client = SMBClient(SmbConfig.builder().withTimeout(10, TimeUnit.SECONDS).withSoTimeout(10, TimeUnit.SECONDS).build())
|
||||
try {
|
||||
val connection = client.connect(root.host, if (root.port > 0) root.port else 445)
|
||||
try {
|
||||
val auth = if (account.username.isBlank()) AuthenticationContext.anonymous()
|
||||
else AuthenticationContext(account.username, account.password.toCharArray(), account.domain)
|
||||
val session = connection.authenticate(auth)
|
||||
val share = session.connectShare(shareName) as? DiskShare ?: throw LibraryException("不是磁盘共享")
|
||||
return SmbConnection(client, connection, session, share)
|
||||
} catch (e: Exception) { connection.close(); throw e }
|
||||
} catch (e: Exception) { client.close(); throw LibraryException("SMB 连接或认证失败") }
|
||||
}
|
||||
private fun path(id: String): String {
|
||||
require(id.split('/', '\\').none { it == ".." || it == "." } && !id.startsWith('/') && !id.startsWith('\\')) { "非法 SMB 路径" }
|
||||
return listOf(base, id.replace('/', '\\')).filter { it.isNotEmpty() }.joinToString("\\")
|
||||
}
|
||||
override suspend fun list(folderId: String, offset: Int): LibraryPage = withContext(Dispatchers.IO) {
|
||||
openConnection().use { connection ->
|
||||
val entries = connection.share.list(path(folderId)).filter { it.fileName != "." && it.fileName != ".." }.map {
|
||||
LibraryEntry(listOf(folderId, it.fileName).filter(String::isNotEmpty).joinToString("/"), it.fileName,
|
||||
it.fileAttributes and FileAttributes.FILE_ATTRIBUTE_DIRECTORY.value != 0L,
|
||||
it.endOfFile, it.lastWriteTime.toString())
|
||||
}.sortedWith(compareBy<LibraryEntry> { !it.isFolder }.thenBy { it.name.lowercase() })
|
||||
LibraryPage(entries)
|
||||
}
|
||||
}
|
||||
/** tt-source URI 只包含账户 ID 与路径,DataSource 通过宿主账户仓库取得凭证。 */
|
||||
fun uri(id: String): String = URI("tt-source", account.id, "/$id", null).toASCIIString()
|
||||
override suspend fun resolve(entry: LibraryEntry, options: LibraryPlaybackOptions): LibraryPlayback {
|
||||
require(!entry.isFolder)
|
||||
val parent = entry.id.substringBeforeLast('/', "")
|
||||
val siblings = list(parent).entries
|
||||
val fresh = siblings.firstOrNull { it.id == entry.id } ?: entry
|
||||
val subtitles = siblings.filter { LibraryFiles.matchesSubtitle(entry.name, it.name) && !it.isFolder }
|
||||
.map { ExternalSubtitle(uri(it.id), LibraryFiles.subtitleMime(it.name)!!) }
|
||||
val key = "${account.id}:${fresh.id}:${fresh.version}:${fresh.size}"
|
||||
return LibraryPlayback(PlaybackRequest(uri(entry.id), startPositionMs = options.startPositionMs, autoplay = true,
|
||||
subtitles = subtitles, cacheKey = key), key, fresh.name, fresh.size, subtitles = subtitles,
|
||||
refresh = { position -> resolve(fresh, options.copy(startPositionMs = position)) })
|
||||
}
|
||||
fun openReader(id: String): SmbRandomReader {
|
||||
val connection = openConnection()
|
||||
try {
|
||||
val file = connection.share.openFile(path(id), EnumSet.of(AccessMask.GENERIC_READ), EnumSet.of(FileAttributes.FILE_ATTRIBUTE_NORMAL),
|
||||
SMB2ShareAccess.ALL, SMB2CreateDisposition.FILE_OPEN, EnumSet.of(SMB2CreateOptions.FILE_NON_DIRECTORY_FILE))
|
||||
return SmbRandomReader(connection, file)
|
||||
} catch (e: Exception) { connection.close(); throw LibraryException("无法打开 SMB 文件") }
|
||||
}
|
||||
}
|
||||
|
||||
class SmbConnection(
|
||||
private val client: SMBClient,
|
||||
private val connection: com.hierynomus.smbj.connection.Connection,
|
||||
private val session: com.hierynomus.smbj.session.Session,
|
||||
val share: DiskShare,
|
||||
) : AutoCloseable {
|
||||
override fun close() {
|
||||
runCatching { share.close() }; runCatching { session.close() }; runCatching { connection.close() }; runCatching { client.close() }
|
||||
}
|
||||
}
|
||||
/** 连续读取复用句柄,seek 直接使用服务器偏移,不通过 skip 从头读。 */
|
||||
class SmbRandomReader(private val connection: SmbConnection, private val file: com.hierynomus.smbj.share.File) : AutoCloseable {
|
||||
val length: Long = file.getFileInformation(FileStandardInformation::class.java).endOfFile
|
||||
fun read(buffer: ByteArray, position: Long, offset: Int, length: Int): Int = file.read(buffer, position, offset, length)
|
||||
override fun close() { try { file.close() } finally { connection.close() } }
|
||||
}
|
||||
Reference in new issue
Block a user