1
1
import 'dart:convert' ;
2
2
import 'dart:async' ;
3
+ import 'package:flutter/material.dart' ;
3
4
import 'package:flutter_webrtc/flutter_webrtc.dart' ;
4
5
6
+ import '../utils/screen_select_dialog.dart' ;
5
7
import 'random_string.dart' ;
6
8
7
9
import '../utils/device_info.dart'
@@ -34,12 +36,13 @@ class Session {
34
36
}
35
37
36
38
class Signaling {
37
- Signaling (this ._host);
39
+ Signaling (this ._host, this ._context );
38
40
39
41
JsonEncoder _encoder = JsonEncoder ();
40
42
JsonDecoder _decoder = JsonDecoder ();
41
43
String _selfId = randomNumeric (6 );
42
44
SimpleWebSocket ? _socket;
45
+ BuildContext ? _context;
43
46
var _host;
44
47
var _port = 8086 ;
45
48
var _turnCredential;
@@ -303,7 +306,8 @@ class Signaling {
303
306
await _socket? .connect ();
304
307
}
305
308
306
- Future <MediaStream > createStream (String media, bool userScreen) async {
309
+ Future <MediaStream > createStream (String media, bool userScreen,
310
+ {BuildContext ? context}) async {
307
311
final Map <String , dynamic > mediaConstraints = {
308
312
'audio' : userScreen ? false : true ,
309
313
'video' : userScreen
@@ -319,22 +323,43 @@ class Signaling {
319
323
'optional' : [],
320
324
}
321
325
};
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
+ }
322
347
323
- MediaStream stream = userScreen
324
- ? await navigator.mediaDevices.getDisplayMedia (mediaConstraints)
325
- : await navigator.mediaDevices.getUserMedia (mediaConstraints);
326
348
onLocalStream? .call (stream);
327
349
return stream;
328
350
}
329
351
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 {
335
359
var newSession = session ?? Session (sid: sessionId, pid: peerId);
336
360
if (media != 'data' )
337
- _localStream = await createStream (media, screenSharing);
361
+ _localStream =
362
+ await createStream (media, screenSharing, context: _context);
338
363
print (_iceServers);
339
364
RTCPeerConnection pc = await createPeerConnection ({
340
365
..._iceServers,
0 commit comments