feat(discovery): 被控端免填 IP,UDP 信标自动发现
- Rust: 新增 discovery::beacon(UDP 41003),被动接收 offer、用包源地址作设备 IP、单播回 host;FFI 增加 ttstd_discovery_start/devices/connect/stop(ABI 3) - Android: 新增 HostBeacon 广播自身并解析 host 应答;ReceiverSession 在 host 为空时等信标再拨号;主界面移除 IP/信令输入框,改为显示发现状态 - Flutter: discover 优先取同网段信标结果并与信令合并去重;connect 先起监听再通知设备拨号;设备卡片标注「UDP 自动发现」 - 视频诊断: 缺少 SPS/PPS 的提示改为收到关键帧后才告警,避免刚接入就误报 - 文档: DEMO 增加发现流程、防火墙与模拟器 NAT 说明及实测截图
This commit is contained in:
1 parent
df2affa9e2
commit
5442dbff2c
13 files changed
+835
-74
No files matched your search
@@ -37,14 +37,28 @@ class SenderBridge {
|
||||
.distinct();
|
||||
}
|
||||
|
||||
/// 发现设备:优先用同网段 UDP 信标(被控端广播、主控端被动收集,无需填 IP),
|
||||
/// 信令服务器只作为跨网段兜底。
|
||||
Future<List<DeviceInfo>> discover({String? signalingUrl}) async {
|
||||
final devices = <DeviceInfo>[];
|
||||
final native = _native;
|
||||
if (native != null) {
|
||||
try {
|
||||
native.startDiscovery();
|
||||
devices.addAll(DeviceInfo.listFromJson(native.discoveryDevices()));
|
||||
} on Object {
|
||||
// 信标不可用(端口被占用等)时退化为信令发现。
|
||||
}
|
||||
}
|
||||
if (signalingUrl != null && signalingUrl.trim().isNotEmpty) {
|
||||
final discovered = await DiscoveryClient.listDevices(signalingUrl);
|
||||
if (discovered.isNotEmpty) return discovered;
|
||||
}
|
||||
if (_native != null) {
|
||||
return const <DeviceInfo>[];
|
||||
for (final device in await DiscoveryClient.listDevices(signalingUrl)) {
|
||||
final duplicated = devices.any(
|
||||
(item) => item.ip == device.ip && item.controlPort == device.controlPort,
|
||||
);
|
||||
if (!duplicated) devices.add(device);
|
||||
}
|
||||
}
|
||||
if (devices.isNotEmpty || native != null) return devices;
|
||||
try {
|
||||
final raw = await _method.invokeListMethod<dynamic>('discover');
|
||||
if (raw == null || raw.isEmpty) return const <DeviceInfo>[];
|
||||
@@ -92,7 +106,11 @@ class SenderBridge {
|
||||
Future<void> connect(DeviceInfo device) async {
|
||||
final native = _native;
|
||||
if (native != null) {
|
||||
// 先确保本机控制/媒体端口在监听,再让被控端立刻拨号(不必等它下次广播)。
|
||||
native.startDemoServer();
|
||||
if (device.ip.isNotEmpty) {
|
||||
native.discoveryConnect(device.ip);
|
||||
}
|
||||
return;
|
||||
}
|
||||
await _invokeIgnoringMissing('connect', device.toJson());
|
||||
|
||||
@@ -97,6 +97,15 @@ class NativeCore {
|
||||
late final DartDemoSendControlFn _demoSendControl = _library
|
||||
.lookup<NativeFunction<NativeDemoSendControlFn>>('ttstd_demo_send_control')
|
||||
.asFunction();
|
||||
late final DartDiscoveryStartFn _discoveryStart = _library
|
||||
.lookup<NativeFunction<NativeDiscoveryStartFn>>('ttstd_discovery_start')
|
||||
.asFunction();
|
||||
late final DartDiscoveryDevicesFn _discoveryDevices = _library
|
||||
.lookup<NativeFunction<NativeDiscoveryDevicesFn>>('ttstd_discovery_devices')
|
||||
.asFunction();
|
||||
late final DartDiscoveryConnectFn _discoveryConnect = _library
|
||||
.lookup<NativeFunction<NativeDiscoveryConnectFn>>('ttstd_discovery_connect')
|
||||
.asFunction();
|
||||
|
||||
bool get isAvailable => !_disposed;
|
||||
|
||||
@@ -209,6 +218,37 @@ class NativeCore {
|
||||
}
|
||||
}
|
||||
|
||||
/// 启动同网段自动发现(UDP 41003 信标),幂等。
|
||||
bool startDiscovery({
|
||||
int controlPort = 41001,
|
||||
int mediaPort = 41000,
|
||||
int audioPort = 41002,
|
||||
}) {
|
||||
return _discoveryStart(controlPort, mediaPort, audioPort) == ok;
|
||||
}
|
||||
|
||||
/// 同网段已发现的设备(字段与信令 `/list` 的 devices 一致)。
|
||||
List<dynamic> discoveryDevices() {
|
||||
final out = calloc<Uint8>(65536);
|
||||
try {
|
||||
final written = _discoveryDevices(out, 65536);
|
||||
_check(written, 'ttstd_discovery_devices');
|
||||
final decoded = jsonDecode(utf8.decode(_copy(out, written)));
|
||||
if (decoded is! Map<String, dynamic>) return const <dynamic>[];
|
||||
final devices = decoded['devices'];
|
||||
return devices is List ? devices : const <dynamic>[];
|
||||
} finally {
|
||||
calloc.free(out);
|
||||
}
|
||||
}
|
||||
|
||||
/// 通知指定设备「主控端已就绪」,让它主动拨号接入。
|
||||
bool discoveryConnect(String ip) {
|
||||
final bytes = utf8.encode(ip.trim());
|
||||
if (bytes.isEmpty) return false;
|
||||
return _withBytes(bytes, (ptr) => _discoveryConnect(ptr, bytes.length) == ok);
|
||||
}
|
||||
|
||||
void dispose() => _disposed = true;
|
||||
|
||||
void _check(int code, String symbol) {
|
||||
@@ -395,3 +435,9 @@ typedef NativeDemoSendControlFn = Int32 Function(
|
||||
typedef DartDemoSendControlFn = int Function(
|
||||
int, int, Pointer<Uint8>, int,
|
||||
);
|
||||
typedef NativeDiscoveryStartFn = Int32 Function(Uint16, Uint16, Uint16);
|
||||
typedef DartDiscoveryStartFn = int Function(int, int, int);
|
||||
typedef NativeDiscoveryDevicesFn = Int32 Function(Pointer<Uint8>, UintPtr);
|
||||
typedef DartDiscoveryDevicesFn = int Function(Pointer<Uint8>, int);
|
||||
typedef NativeDiscoveryConnectFn = Int32 Function(Pointer<Uint8>, UintPtr);
|
||||
typedef DartDiscoveryConnectFn = int Function(Pointer<Uint8>, int);
|
||||
@@ -133,8 +133,14 @@ class _DeviceCard extends StatelessWidget {
|
||||
],
|
||||
),
|
||||
const SizedBox(height: 6),
|
||||
Text('指纹 ${device.shortFingerprint} · ${device.resolution}',
|
||||
style: theme.textTheme.labelSmall?.copyWith(color: theme.colorScheme.outline)),
|
||||
Text(
|
||||
[
|
||||
if (device.fingerprint.isNotEmpty) '指纹 ${device.shortFingerprint}',
|
||||
if (device.resolution.isNotEmpty) device.resolution,
|
||||
'UDP 自动发现',
|
||||
].join(' · '),
|
||||
style: theme.textTheme.labelSmall?.copyWith(color: theme.colorScheme.outline),
|
||||
),
|
||||
],
|
||||
),
|
||||
),
|
||||
@@ -159,7 +165,8 @@ class _EmptyState extends StatelessWidget {
|
||||
const Text('未发现设备'),
|
||||
const SizedBox(height: 4),
|
||||
Text(
|
||||
'本机监听端点已移至设置;请确认被控端与主控端处于同一网段',
|
||||
'被控端开启投屏后会自动出现(UDP 41003 自动发现,无需填写 IP)\n'
|
||||
'若长时间未出现,请确认两端在同一网段且防火墙放行 UDP 41003',
|
||||
style: Theme.of(context).textTheme.bodySmall,
|
||||
textAlign: TextAlign.center,
|
||||
),
|
||||
|
||||
Reference in new issue
Block a user