初始化 TTVideoPlayer 工程:模块化播放框架、同同影音 App 与文档

Media3 内核播放框架(player-api/core/media3/source/render/lifecycle/telemetry)、
WebDAV/SMB/Jellyfin 来源、DLNA/Google Cast 投屏、RTMP 原生库与完整 App。
This commit is contained in:
tongtong committed 2026-11-30 18:13:40 +08:00
commit 922ec80bcc
147 files changed
+20169

No files matched your search

+6
View File
@@ -0,0 +1,6 @@
plugins { kotlin("jvm"); `java-library` }
kotlin { jvmToolchain(17) }
dependencies {
api(project(":player-api"))
testImplementation("junit:junit:4.13.2")
}
@@ -0,0 +1,66 @@
package com.ttstd.player.cast
import com.ttstd.player.api.*
import java.net.URI
enum class CastRoute { DIRECT, RELAY, UNSUPPORTED }
data class CastDecision(val route: CastRoute, val mimeType: String, val reason: String = "")
/** 投屏是把媒体地址交给接收端,不是屏幕镜像。私有文件通过手机 HTTP relay 按 Range 提供。 */
object CastPolicy {
fun assess(request: PlaybackRequest): CastDecision {
val uri = runCatching { URI(request.uri) }.getOrNull()
?: return CastDecision(CastRoute.UNSUPPORTED, "", "媒体地址无效")
val scheme = uri.scheme?.lowercase()
val path = uri.path.orEmpty().lowercase()
val adaptive = request.sourceType in setOf(MediaSourceType.HLS, MediaSourceType.DASH, MediaSourceType.SMOOTH_STREAMING) ||
(request.sourceType == MediaSourceType.AUTO && (path.endsWith(".m3u8") || path.endsWith(".mpd") || path.contains(".ism/")))
val mime = when {
request.sourceType == MediaSourceType.HLS || path.endsWith(".m3u8") -> "application/x-mpegURL"
request.sourceType == MediaSourceType.DASH || path.endsWith(".mpd") -> "application/dash+xml"
path.endsWith(".webm") -> "video/webm"
path.endsWith(".mkv") -> "video/x-matroska"
else -> "video/mp4"
}
if (request.sourceType in setOf(MediaSourceType.RTSP, MediaSourceType.RTMP, MediaSourceType.FLV, MediaSourceType.SMOOTH_STREAMING) || scheme in setOf("rtsp", "rtmp") || path.endsWith(".flv"))
return CastDecision(CastRoute.UNSUPPORTED, mime, "该来源需由媒体服务器转换为接收端支持的 HTTP 视频")
if (scheme in setOf("http", "https") && request.headers.isEmpty() && uri.userInfo == null)
return CastDecision(CastRoute.DIRECT, mime)
if (adaptive) return CastDecision(CastRoute.UNSUPPORTED, mime, "带认证的自适应流需要服务端提供可投屏地址,当前不代理清单与分片")
if (scheme in setOf("http", "https", "tt-source", "file", "content")) return CastDecision(CastRoute.RELAY, mime)
return CastDecision(CastRoute.UNSUPPORTED, mime, "该来源尚不能投屏")
}
}
/** 单段字节范围。多 Range 请求明确拒绝,不把错误范围当成整文件发送。 */
data class ByteRange(val start: Long, val endInclusive: Long) {
val length: Long get() = endInclusive - start + 1
companion object {
fun parse(header: String, total: Long): ByteRange {
require(total > 0 && header.startsWith("bytes=") && !header.contains(','))
val parts = header.removePrefix("bytes=").split('-', limit = 2)
require(parts.size == 2)
val start: Long
val end: Long
if (parts[0].isEmpty()) {
val suffix = parts[1].toLong(); require(suffix > 0)
start = (total - suffix).coerceAtLeast(0); end = total - 1
} else {
start = parts[0].toLong()
end = parts[1].takeIf(String::isNotEmpty)?.toLong()?.coerceAtMost(total - 1) ?: (total - 1)
}
require(start >= 0 && start < total && end >= start)
return ByteRange(start, end)
}
}
}
data class RemotePlaybackState(val positionMs: Long = 0, val durationMs: Long? = null, val playing: Boolean = false)
interface CastProgressObserver {
suspend fun started(positionMs: Long)
suspend fun progress(state: RemotePlaybackState)
suspend fun stopped(positionMs: Long)
}
data class CastMedia(val uri: String, val title: String, val mimeType: String, val startPositionMs: Long = 0) {
override fun toString() = "CastMedia(uri=REDACTED, mimeType=$mimeType)"
}
@@ -0,0 +1,21 @@
package com.ttstd.player.cast
import com.ttstd.player.api.*
import org.junit.Assert.*
import org.junit.Test
class CastPolicyTest {
@Test fun choosesRelayForCredentialsAndRejectsPrivateAdaptiveStreams() {
assertEquals(CastRoute.DIRECT, CastPolicy.assess(PlaybackRequest("https://host/a.mp4")).route)
assertEquals(CastRoute.RELAY, CastPolicy.assess(PlaybackRequest("https://host/a.mp4", mapOf("Authorization" to "secret"))).route)
assertEquals(CastRoute.RELAY, CastPolicy.assess(PlaybackRequest("tt-source://id/movie.mkv")).route)
assertEquals(CastRoute.UNSUPPORTED, CastPolicy.assess(PlaybackRequest("https://host/play", mapOf("Authorization" to "secret"), sourceType = MediaSourceType.HLS)).route)
assertEquals(CastRoute.UNSUPPORTED, CastPolicy.assess(PlaybackRequest("rtsp://host/live")).route)
}
@Test fun rangeParsingSupportsOpenAndSuffixRangesAndRejectsInvalidRanges() {
assertEquals(ByteRange(2, 5), ByteRange.parse("bytes=2-5", 10))
assertEquals(ByteRange(2, 9), ByteRange.parse("bytes=2-", 10))
assertEquals(ByteRange(7, 9), ByteRange.parse("bytes=-3", 10))
assertEquals(ByteRange(0, 9), ByteRange.parse("bytes=-30", 10))
listOf("bytes=10-", "bytes=5-2", "bytes=0-1,3-4", "bytes=-0").forEach { assertTrue(runCatching { ByteRange.parse(it, 10) }.isFailure) }
}
}