➕ Add livekit
This commit is contained in:
@ -1,12 +0,0 @@
|
||||
import 'dart:io';
|
||||
|
||||
import 'package:dio/dio.dart';
|
||||
import 'package:native_dio_adapter/native_dio_adapter.dart';
|
||||
|
||||
Dio addClientAdapter(Dio client) {
|
||||
if (Platform.isAndroid || Platform.isIOS || Platform.isMacOS) {
|
||||
// Switch to native implementation if possible
|
||||
client.httpClientAdapter = NativeAdapter();
|
||||
}
|
||||
return client;
|
||||
}
|
@ -1,2 +0,0 @@
|
||||
export 'package:surface/providers/adapters/sn_network_web.dart'
|
||||
if (dart.library.io) 'package:surface/providers/adapters/sn_network_native.dart';
|
@ -1,5 +0,0 @@
|
||||
import 'package:dio/dio.dart';
|
||||
|
||||
Dio addClientAdapter(Dio client) {
|
||||
return client;
|
||||
}
|
@ -5,7 +5,6 @@ import 'dart:developer';
|
||||
import 'package:dio/dio.dart';
|
||||
import 'package:dio_smart_retry/dio_smart_retry.dart';
|
||||
import 'package:shared_preferences/shared_preferences.dart';
|
||||
import 'package:surface/providers/adapters/sn_network_universal.dart';
|
||||
import 'package:synchronized/synchronized.dart';
|
||||
|
||||
const kAtkStoreKey = 'nex_user_atk';
|
||||
@ -21,7 +20,7 @@ const kNetworkServerDirectory = [
|
||||
];
|
||||
|
||||
class SnNetworkProvider {
|
||||
late Dio client;
|
||||
late final Dio client;
|
||||
|
||||
late final SharedPreferences _prefs;
|
||||
|
||||
@ -53,8 +52,6 @@ class SnNetworkProvider {
|
||||
),
|
||||
);
|
||||
|
||||
client = addClientAdapter(client);
|
||||
|
||||
SharedPreferences.getInstance().then((prefs) {
|
||||
_prefs = prefs;
|
||||
client.options.baseUrl =
|
||||
|
@ -14,114 +14,7 @@ class CallRoomScreen extends StatefulWidget {
|
||||
State<CallRoomScreen> createState() => _CallRoomScreenState();
|
||||
}
|
||||
|
||||
const _kLocalWebRtcBaseUrl = 'http://localhost:8001';
|
||||
|
||||
class _CallRoomScreenState extends State<CallRoomScreen> {
|
||||
RTCPeerConnection? _peerConnection;
|
||||
MediaStream? _localStream;
|
||||
WebSocketChannel? _wsChannel;
|
||||
|
||||
@override
|
||||
void initState() {
|
||||
super.initState();
|
||||
_initWebRtc();
|
||||
}
|
||||
|
||||
Future<void> _initWebRtc() async {
|
||||
final client = Dio();
|
||||
client.options.baseUrl = _kLocalWebRtcBaseUrl;
|
||||
|
||||
final configResp = await client.get('/.well-known/webrtc');
|
||||
|
||||
// Get user media (audio only)
|
||||
_localStream = await navigator.mediaDevices.getUserMedia({
|
||||
'audio': true,
|
||||
'video': false,
|
||||
});
|
||||
|
||||
// Configure Peer Connection
|
||||
Map<String, dynamic> config = {
|
||||
'iceServers': configResp.data['ice_servers']
|
||||
};
|
||||
|
||||
_peerConnection = await createPeerConnection(config);
|
||||
|
||||
// Add local stream to peer connection
|
||||
_peerConnection?.addStream(_localStream!);
|
||||
|
||||
// Listen for ICE candidates
|
||||
_peerConnection?.onIceCandidate = (RTCIceCandidate candidate) {
|
||||
print('New ICE candidate: ${candidate.candidate}');
|
||||
// Send the candidate to the signaling server
|
||||
};
|
||||
|
||||
// Handle remote stream
|
||||
_peerConnection?.onAddStream = (MediaStream stream) {
|
||||
print('Remote stream added');
|
||||
// Play the remote stream
|
||||
};
|
||||
|
||||
_wsChannel = WebSocketChannel.connect(
|
||||
Uri.parse('$_kLocalWebRtcBaseUrl/webrtc'),
|
||||
);
|
||||
await _wsChannel!.ready;
|
||||
|
||||
_wsChannel!.stream.listen((event) {
|
||||
final Map<String, dynamic> data = jsonDecode(event);
|
||||
|
||||
switch (data['type']) {
|
||||
case 'offer':
|
||||
_handleOffer(data);
|
||||
break;
|
||||
case 'answer':
|
||||
_handleAnswer(data);
|
||||
break;
|
||||
case 'candidate':
|
||||
_handleCandidate(data);
|
||||
break;
|
||||
}
|
||||
});
|
||||
}
|
||||
|
||||
Future<void> _handleOffer(Map<String, dynamic> data) async {
|
||||
// Set remote description
|
||||
final offer = RTCSessionDescription(data['sdp'], data['type']);
|
||||
await _peerConnection?.setRemoteDescription(offer);
|
||||
|
||||
// Create and send answer
|
||||
final answer = await _peerConnection?.createAnswer();
|
||||
await _peerConnection?.setLocalDescription(answer!);
|
||||
|
||||
_wsChannel?.sink.add({
|
||||
'type': 'answer',
|
||||
'sdp': answer?.sdp,
|
||||
});
|
||||
}
|
||||
|
||||
Future<void> _handleAnswer(Map<String, dynamic> data) async {
|
||||
// Set remote description
|
||||
final answer = RTCSessionDescription(data['sdp'], data['type']);
|
||||
await _peerConnection?.setRemoteDescription(answer);
|
||||
}
|
||||
|
||||
Future<void> _handleCandidate(Map<String, dynamic> data) async {
|
||||
// Add ICE candidate
|
||||
final candidate = RTCIceCandidate(
|
||||
data['candidate'],
|
||||
data['sdpMid'],
|
||||
data['sdpMLineIndex'],
|
||||
);
|
||||
await _peerConnection?.addCandidate(candidate);
|
||||
}
|
||||
|
||||
@override
|
||||
void dispose() {
|
||||
_wsChannel?.sink.close();
|
||||
_localStream?.dispose();
|
||||
_peerConnection?.close();
|
||||
super.dispose();
|
||||
}
|
||||
|
||||
@override
|
||||
Widget build(BuildContext context) {
|
||||
return Scaffold(
|
||||
|
Reference in New Issue
Block a user