feat: Miracast 链路验证(Wi-Fi Direct 探测)
新增 player-cast-miracast 模块与 demo 验证页:WifiP2pManager 发现/连接、 解析 WFD 信息并展示 P2P 组状态,不做 WFD RTSP 与推流。实测可发现 Windows「无线显示器」(WFD primary sink,ctrl 7236,5GHz 5745MHz), 但公共 API 无法注入 WFD 信息元素,Windows 接收端不接受纯 P2P 组网, 完整 Miracast 源仍需系统 WFD 服务或真实 Miracast 设备。
This commit is contained in:
1 parent
9a3ff31278
commit
6f5ae72fba
8 files changed
+292
-1
No files matched your search
@@ -35,6 +35,7 @@ dependencies {
|
||||
implementation(project(":player-cast-dlna"))
|
||||
implementation(project(":player-cast-relay"))
|
||||
implementation(project(":player-cast-google"))
|
||||
implementation(project(":player-cast-miracast"))
|
||||
implementation("androidx.appcompat:appcompat:1.7.1")
|
||||
implementation(project(":player-core"))
|
||||
implementation(project(":player-media3"))
|
||||
|
||||
@@ -3,6 +3,7 @@
|
||||
<!-- 验证宿主允许输入任意 HTTP 测试源;library 不修改宿主的网络安全策略。 -->
|
||||
<application android:name="com.ttstd.player.demo.PlayerApplication" android:usesCleartextTraffic="true" android:theme="@style/Theme.AppCompat.DayNight.NoActionBar" android:label="TTVideoPlayer" android:allowBackup="false" android:supportsRtl="true">
|
||||
<activity android:name="com.ttstd.player.demo.LibraryActivity" android:exported="false" />
|
||||
<activity android:name="com.ttstd.player.demo.MiracastProbeActivity" android:exported="true" />
|
||||
<activity android:name="com.ttstd.player.demo.MainActivity" android:exported="true">
|
||||
<intent-filter>
|
||||
<action android:name="android.intent.action.MAIN" />
|
||||
|
||||
@@ -97,6 +97,9 @@ class MainActivity : AppCompatActivity() {
|
||||
button("DLNA 投屏:搜索电视") { casting.discoverDlna() }
|
||||
button("DLNA 播放控制 / 返回本机") { casting.showDlnaControls() }
|
||||
button("Google Cast 投屏") { casting.google() }
|
||||
button("Miracast 链路验证(Wi-Fi Direct)") {
|
||||
startActivity(android.content.Intent(this@MainActivity, MiracastProbeActivity::class.java))
|
||||
}
|
||||
button("选择音轨 / 字幕") {
|
||||
val tracks = engine.state.value.tracks
|
||||
val labels = arrayOf("关闭字幕") + tracks.map { "${it.type} · ${it.label ?: it.language ?: it.id}${if (it.selected) " ✓" else ""}" }
|
||||
|
||||
@@ -0,0 +1,105 @@
|
||||
package com.ttstd.player.demo
|
||||
|
||||
import android.Manifest
|
||||
import android.content.pm.PackageManager
|
||||
import android.os.Build
|
||||
import android.os.Bundle
|
||||
import android.widget.Button
|
||||
import android.widget.LinearLayout
|
||||
import android.widget.ScrollView
|
||||
import android.widget.TextView
|
||||
import androidx.activity.result.contract.ActivityResultContracts
|
||||
import androidx.appcompat.app.AppCompatActivity
|
||||
import androidx.core.content.ContextCompat
|
||||
import androidx.lifecycle.Lifecycle
|
||||
import androidx.lifecycle.lifecycleScope
|
||||
import androidx.lifecycle.repeatOnLifecycle
|
||||
import com.ttstd.player.cast.miracast.MiracastPeer
|
||||
import com.ttstd.player.cast.miracast.MiracastProbe
|
||||
import kotlinx.coroutines.launch
|
||||
|
||||
/** Miracast 链路验证页:Wi-Fi Direct 发现/连接 Windows 无线显示器(不做推流)。 */
|
||||
class MiracastProbeActivity : AppCompatActivity() {
|
||||
private lateinit var probe: MiracastProbe
|
||||
private lateinit var status: TextView
|
||||
private lateinit var groupView: TextView
|
||||
private lateinit var list: LinearLayout
|
||||
private val permission = if (Build.VERSION.SDK_INT >= 33) {
|
||||
Manifest.permission.NEARBY_WIFI_DEVICES
|
||||
} else {
|
||||
Manifest.permission.ACCESS_FINE_LOCATION
|
||||
}
|
||||
private val requestPermission = registerForActivityResult(ActivityResultContracts.RequestPermission()) { granted ->
|
||||
if (granted) begin() else status.text = "缺少 Wi-Fi 扫描权限:$permission"
|
||||
}
|
||||
|
||||
override fun onCreate(savedInstanceState: Bundle?) {
|
||||
super.onCreate(savedInstanceState)
|
||||
probe = MiracastProbe.create(this)
|
||||
val root = LinearLayout(this).apply {
|
||||
orientation = LinearLayout.VERTICAL
|
||||
setPadding(24, 24, 24, 24)
|
||||
}
|
||||
status = TextView(this).apply { text = "Miracast 链路验证(Wi-Fi Direct)" }
|
||||
groupView = TextView(this).apply { text = "未连接 P2P 组" }
|
||||
list = LinearLayout(this).apply { orientation = LinearLayout.VERTICAL }
|
||||
root.addView(status)
|
||||
root.addView(groupView)
|
||||
root.addView(Button(this).apply { text = "重新搜索"; isAllCaps = false; setOnClickListener { probe.discover() } })
|
||||
root.addView(Button(this).apply { text = "断开 P2P"; isAllCaps = false; setOnClickListener { probe.disconnect() } })
|
||||
val macInput = android.widget.EditText(this).apply {
|
||||
hint = "P2P MAC(例如 de:f2:3c:e9:49:b3)"
|
||||
setSingleLine(true)
|
||||
}
|
||||
root.addView(macInput)
|
||||
root.addView(Button(this).apply {
|
||||
text = "按 MAC 连接"
|
||||
isAllCaps = false
|
||||
setOnClickListener { macInput.text?.toString()?.trim()?.takeIf { it.isNotEmpty() }?.let(probe::connect) }
|
||||
})
|
||||
root.addView(ScrollView(this).apply { addView(list) }, LinearLayout.LayoutParams(-1, 0, 1f))
|
||||
setContentView(root)
|
||||
if (ContextCompat.checkSelfPermission(this, permission) == PackageManager.PERMISSION_GRANTED) {
|
||||
begin()
|
||||
} else {
|
||||
requestPermission.launch(permission)
|
||||
}
|
||||
}
|
||||
|
||||
private fun begin() {
|
||||
probe.start()
|
||||
lifecycleScope.launch {
|
||||
repeatOnLifecycle(Lifecycle.State.STARTED) {
|
||||
launch {
|
||||
probe.status.collect { status.text = "Miracast 链路验证(Wi-Fi Direct)\n$it" }
|
||||
}
|
||||
launch {
|
||||
probe.group.collect { groupView.text = it ?: "未连接 P2P 组" }
|
||||
}
|
||||
launch {
|
||||
probe.peers.collect(::render)
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
private fun render(peers: List<MiracastPeer>) {
|
||||
list.removeAllViews()
|
||||
if (peers.isEmpty()) {
|
||||
list.addView(TextView(this).apply { text = "未发现设备(请确认电脑「无线显示器」已打开)" })
|
||||
return
|
||||
}
|
||||
peers.forEach { peer ->
|
||||
list.addView(Button(this).apply {
|
||||
isAllCaps = false
|
||||
text = "${peer.name} · ${peer.address}\n${peer.statusText}${peer.wfd?.let { " · $it" } ?: ""}"
|
||||
setOnClickListener { probe.connect(peer.address) }
|
||||
})
|
||||
}
|
||||
}
|
||||
|
||||
override fun onDestroy() {
|
||||
if (::probe.isInitialized) probe.stop()
|
||||
super.onDestroy()
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,14 @@
|
||||
plugins { id("com.android.library"); kotlin("android") }
|
||||
android {
|
||||
namespace = "com.ttstd.player.cast.miracast"
|
||||
compileSdk = 37
|
||||
defaultConfig { minSdk = 23 }
|
||||
compileOptions {
|
||||
sourceCompatibility = JavaVersion.VERSION_17
|
||||
targetCompatibility = JavaVersion.VERSION_17
|
||||
}
|
||||
}
|
||||
kotlin { jvmToolchain(17) }
|
||||
dependencies {
|
||||
implementation("org.jetbrains.kotlinx:kotlinx-coroutines-android:1.10.2")
|
||||
}
|
||||
@@ -0,0 +1,7 @@
|
||||
<?xml version="1.0" encoding="utf-8"?>
|
||||
<manifest xmlns:android="http://schemas.android.com/apk/res/android">
|
||||
<uses-permission android:name="android.permission.ACCESS_WIFI_STATE" />
|
||||
<uses-permission android:name="android.permission.CHANGE_WIFI_STATE" />
|
||||
<uses-permission android:name="android.permission.ACCESS_FINE_LOCATION" android:maxSdkVersion="32" />
|
||||
<uses-permission android:name="android.permission.NEARBY_WIFI_DEVICES" android:usesPermissionFlags="neverForLocation" />
|
||||
</manifest>
|
||||
@@ -0,0 +1,160 @@
|
||||
package com.ttstd.player.cast.miracast
|
||||
|
||||
import android.content.BroadcastReceiver
|
||||
import android.content.Context
|
||||
import android.content.Intent
|
||||
import android.content.IntentFilter
|
||||
import android.net.NetworkInfo
|
||||
import android.net.wifi.p2p.WifiP2pConfig
|
||||
import android.net.wifi.p2p.WifiP2pDevice
|
||||
import android.net.wifi.p2p.WifiP2pInfo
|
||||
import android.net.wifi.p2p.WifiP2pManager
|
||||
import android.net.wifi.p2p.WifiP2pWfdInfo
|
||||
import android.net.wifi.WpsInfo
|
||||
import kotlinx.coroutines.flow.MutableStateFlow
|
||||
import kotlinx.coroutines.flow.StateFlow
|
||||
|
||||
data class MiracastPeer(
|
||||
val name: String,
|
||||
val address: String,
|
||||
val statusText: String,
|
||||
val wfd: String?,
|
||||
)
|
||||
|
||||
/** Miracast 链路验证(第一阶段):只做 Wi-Fi Direct 发现、连接与组信息展示,
|
||||
* 不做 WFD RTSP 握手和屏幕推流。 */
|
||||
class MiracastProbe private constructor(private val context: Context) {
|
||||
private val manager = context.getSystemService(Context.WIFI_P2P_SERVICE) as WifiP2pManager
|
||||
private val channel = manager.initialize(context, context.mainLooper, null)
|
||||
|
||||
private val _status = MutableStateFlow("未开始")
|
||||
private val _peers = MutableStateFlow<List<MiracastPeer>>(emptyList())
|
||||
private val _group = MutableStateFlow<String?>(null)
|
||||
private var receiverRegistered = false
|
||||
|
||||
val status: StateFlow<String> = _status
|
||||
val peers: StateFlow<List<MiracastPeer>> = _peers
|
||||
val group: StateFlow<String?> = _group
|
||||
|
||||
private val receiver = object : BroadcastReceiver() {
|
||||
override fun onReceive(ctx: Context, intent: Intent) {
|
||||
when (intent.action) {
|
||||
WifiP2pManager.WIFI_P2P_STATE_CHANGED_ACTION -> {
|
||||
val state = intent.getIntExtra(WifiP2pManager.EXTRA_WIFI_STATE, -1)
|
||||
_status.value = if (state == WifiP2pManager.WIFI_P2P_STATE_ENABLED) {
|
||||
"Wi-Fi Direct 已开启"
|
||||
} else {
|
||||
"Wi-Fi Direct 已关闭"
|
||||
}
|
||||
}
|
||||
WifiP2pManager.WIFI_P2P_PEERS_CHANGED_ACTION -> requestPeers()
|
||||
WifiP2pManager.WIFI_P2P_CONNECTION_CHANGED_ACTION -> {
|
||||
@Suppress("DEPRECATION")
|
||||
val info = intent.getParcelableExtra<NetworkInfo>(WifiP2pManager.EXTRA_NETWORK_INFO)
|
||||
if (info?.isConnected == true) {
|
||||
requestConnectionInfo()
|
||||
} else {
|
||||
_group.value = null
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
fun start() {
|
||||
if (!receiverRegistered) {
|
||||
val filter = IntentFilter().apply {
|
||||
addAction(WifiP2pManager.WIFI_P2P_STATE_CHANGED_ACTION)
|
||||
addAction(WifiP2pManager.WIFI_P2P_PEERS_CHANGED_ACTION)
|
||||
addAction(WifiP2pManager.WIFI_P2P_CONNECTION_CHANGED_ACTION)
|
||||
addAction(WifiP2pManager.WIFI_P2P_THIS_DEVICE_CHANGED_ACTION)
|
||||
}
|
||||
context.registerReceiver(receiver, filter)
|
||||
receiverRegistered = true
|
||||
}
|
||||
discover()
|
||||
}
|
||||
|
||||
fun stop() {
|
||||
if (receiverRegistered) {
|
||||
runCatching { context.unregisterReceiver(receiver) }
|
||||
receiverRegistered = false
|
||||
}
|
||||
}
|
||||
|
||||
fun discover() {
|
||||
_status.value = "正在搜索 Wi-Fi Direct 设备…"
|
||||
manager.discoverPeers(channel, listener("搜索"))
|
||||
}
|
||||
|
||||
fun connect(address: String) {
|
||||
val config = WifiP2pConfig().apply {
|
||||
deviceAddress = address
|
||||
wps.setup = WpsInfo.PBC
|
||||
}
|
||||
_status.value = "正在连接 $address…"
|
||||
manager.connect(channel, config, listener("连接"))
|
||||
}
|
||||
|
||||
fun disconnect() {
|
||||
manager.removeGroup(channel, listener("断开"))
|
||||
_group.value = null
|
||||
}
|
||||
|
||||
private fun requestPeers() {
|
||||
@Suppress("DEPRECATION")
|
||||
manager.requestPeers(channel) { list ->
|
||||
_peers.value = list.deviceList.map { it.toPeer() }.sortedWith(compareBy({ it.wfd == null }, { it.name }))
|
||||
}
|
||||
}
|
||||
|
||||
private fun requestConnectionInfo() {
|
||||
@Suppress("DEPRECATION")
|
||||
manager.requestConnectionInfo(channel) { info: WifiP2pInfo ->
|
||||
_group.value = buildString {
|
||||
append("已连接 P2P:")
|
||||
append(if (info.isGroupOwner) "本机组主" else "对端组主")
|
||||
info.groupOwnerAddress?.hostAddress?.let { append(" · GO $it") }
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
private fun WifiP2pDevice.toPeer(): MiracastPeer {
|
||||
val wfdText = runCatching {
|
||||
val info: WifiP2pWfdInfo = wfdInfo ?: return@runCatching null
|
||||
"WFD type=${info.deviceType} ctrl=${info.controlPort} ${if (info.isSessionAvailable) "可会话" else "空闲"}"
|
||||
}.getOrNull()
|
||||
return MiracastPeer(deviceName?.takeIf { it.isNotBlank() } ?: "(未命名)", deviceAddress, statusText(this.status), wfdText)
|
||||
}
|
||||
|
||||
private fun statusText(status: Int) = when (status) {
|
||||
WifiP2pDevice.AVAILABLE -> "可用"
|
||||
WifiP2pDevice.INVITED -> "已邀请"
|
||||
WifiP2pDevice.CONNECTED -> "已连接"
|
||||
WifiP2pDevice.FAILED -> "失败"
|
||||
WifiP2pDevice.UNAVAILABLE -> "不可用"
|
||||
else -> status.toString()
|
||||
}
|
||||
|
||||
private fun listener(operation: String) = object : WifiP2pManager.ActionListener {
|
||||
override fun onSuccess() {
|
||||
_status.value = "$operation 成功"
|
||||
requestPeers()
|
||||
}
|
||||
|
||||
override fun onFailure(reason: Int) {
|
||||
_status.value = "$operation 失败:${reasonText(reason)}"
|
||||
}
|
||||
}
|
||||
|
||||
private fun reasonText(reason: Int) = when (reason) {
|
||||
WifiP2pManager.P2P_UNSUPPORTED -> "不支持 P2P"
|
||||
WifiP2pManager.BUSY -> "忙"
|
||||
WifiP2pManager.ERROR -> "内部错误"
|
||||
else -> reason.toString()
|
||||
}
|
||||
|
||||
companion object {
|
||||
fun create(context: Context) = MiracastProbe(context.applicationContext)
|
||||
}
|
||||
}
|
||||
+1
-1
@@ -39,7 +39,7 @@ include(":player-media3-source")
|
||||
include(":player-media3-ffmpeg")
|
||||
include(":player-rtmp-native")
|
||||
|
||||
include(":library-api", ":library-http", ":library-webdav", ":library-smb", ":library-jellyfin", ":library-storage", ":player-cast-api", ":player-cast-dlna", ":player-cast-relay", ":player-cast-google")
|
||||
include(":library-api", ":library-http", ":library-webdav", ":library-smb", ":library-jellyfin", ":library-storage", ":player-cast-api", ":player-cast-dlna", ":player-cast-relay", ":player-cast-google", ":player-cast-miracast")
|
||||
include(":library-media3")
|
||||
|
||||
include(":app")
|
||||
Reference in new issue
Block a user