11import 'dart:convert' ;
22import 'dart:async' ;
3+ import 'package:flutter/material.dart' ;
34import 'package:flutter_webrtc/flutter_webrtc.dart' ;
45
6+ import '../utils/screen_select_dialog.dart' ;
57import 'random_string.dart' ;
68
79import '../utils/device_info.dart'
@@ -34,12 +36,13 @@ class Session {
3436}
3537
3638class Signaling {
37- Signaling (this ._host);
39+ Signaling (this ._host, this ._context );
3840
3941 JsonEncoder _encoder = JsonEncoder ();
4042 JsonDecoder _decoder = JsonDecoder ();
4143 String _selfId = randomNumeric (6 );
4244 SimpleWebSocket ? _socket;
45+ BuildContext ? _context;
4346 var _host;
4447 var _port = 8086 ;
4548 var _turnCredential;
@@ -303,7 +306,8 @@ class Signaling {
303306 await _socket? .connect ();
304307 }
305308
306- Future <MediaStream > createStream (String media, bool userScreen) async {
309+ Future <MediaStream > createStream (String media, bool userScreen,
310+ {BuildContext ? context}) async {
307311 final Map <String , dynamic > mediaConstraints = {
308312 'audio' : userScreen ? false : true ,
309313 'video' : userScreen
@@ -319,22 +323,43 @@ class Signaling {
319323 'optional' : [],
320324 }
321325 };
326+ late MediaStream stream;
327+ if (userScreen) {
328+ if (WebRTC .platformIsDesktop) {
329+ final source = await showDialog <DesktopCapturerSource >(
330+ context: context! ,
331+ builder: (context) => ScreenSelectDialog (),
332+ );
333+ stream = await navigator.mediaDevices.getDisplayMedia (< String , dynamic > {
334+ 'video' : source == null
335+ ? true
336+ : {
337+ 'deviceId' : {'exact' : source.id},
338+ 'mandatory' : {'frameRate' : 30.0 }
339+ }
340+ });
341+ } else {
342+ stream = await navigator.mediaDevices.getDisplayMedia (mediaConstraints);
343+ }
344+ } else {
345+ stream = await navigator.mediaDevices.getUserMedia (mediaConstraints);
346+ }
322347
323- MediaStream stream = userScreen
324- ? await navigator.mediaDevices.getDisplayMedia (mediaConstraints)
325- : await navigator.mediaDevices.getUserMedia (mediaConstraints);
326348 onLocalStream? .call (stream);
327349 return stream;
328350 }
329351
330- Future <Session > _createSession (Session ? session,
331- {required String peerId,
332- required String sessionId,
333- required String media,
334- required bool screenSharing}) async {
352+ Future <Session > _createSession (
353+ Session ? session, {
354+ required String peerId,
355+ required String sessionId,
356+ required String media,
357+ required bool screenSharing,
358+ }) async {
335359 var newSession = session ?? Session (sid: sessionId, pid: peerId);
336360 if (media != 'data' )
337- _localStream = await createStream (media, screenSharing);
361+ _localStream =
362+ await createStream (media, screenSharing, context: _context);
338363 print (_iceServers);
339364 RTCPeerConnection pc = await createPeerConnection ({
340365 ..._iceServers,
0 commit comments