fix: isolate remote desktop webrtc sessions

This commit is contained in:
Haitao Pan 2026-06-08 21:09:11 +08:00
parent b222434a9d
commit 064e0fdc27
3 changed files with 18 additions and 6 deletions

View File

@ -3,6 +3,7 @@ import 'dart:convert';
import 'package:flutter/foundation.dart';
import 'package:flutter_webrtc/flutter_webrtc.dart';
import '../../app/app_controller.dart';
import '../../runtime/gateway_runtime_helpers.dart';
String desktopConnectionStateName(RTCPeerConnectionState state) {
final value = state.toString().split('.').last;
@ -31,6 +32,10 @@ Map<String, Object?> desktopOfferParams({
};
}
String desktopSessionId() {
return 'remote-desktop-${randomIdInternal()}';
}
Future<MediaStream?> desktopRemoteVideoStreamForTrack(
RTCTrackEvent event, {
required Future<MediaStream> Function(String label) createFallbackStream,
@ -242,11 +247,8 @@ class DesktopClient {
}
};
// Add transceivers for receiving video and audio
await _peerConnection!.addTransceiver(
kind: RTCRtpMediaType.RTCRtpMediaTypeAudio,
init: RTCRtpTransceiverInit(direction: TransceiverDirection.RecvOnly),
);
// Bridge publishes a video-only desktop stream; keep SDP m-line mapping
// simple so reconnects do not depend on rejected audio sections.
await _peerConnection!.addTransceiver(
kind: RTCRtpMediaType.RTCRtpMediaTypeVideo,
init: RTCRtpTransceiverInit(direction: TransceiverDirection.RecvOnly),

View File

@ -76,7 +76,7 @@ class _DesktopViewState extends State<DesktopView> {
_initRenderer();
_client = DesktopClient(
controller: widget.controller,
sessionId: 'remote-desktop-session',
sessionId: desktopSessionId(),
);
_inputHandler = DesktopInputHandler(
onSendInput: (event) {

View File

@ -143,6 +143,16 @@ void main() {
expect(params['height'], 720);
});
test('generates distinct desktop session ids for parallel app instances', () {
final first = desktopSessionId();
final second = desktopSessionId();
expect(first, startsWith('remote-desktop-'));
expect(second, startsWith('remote-desktop-'));
expect(first, isNot(second));
expect(first, isNot('remote-desktop-session'));
});
test('uses bridge-provided remote stream when present', () async {
var fallbackCreated = false;
final providedStream = FakeMediaStream('provided-stream');