-
Notifications
You must be signed in to change notification settings - Fork 35
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
[trtc][flutter] Add Windows screen sharing functionality
- Loading branch information
Showing
3 changed files
with
164 additions
and
2 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,70 @@ | ||
import 'dart:async'; | ||
import 'dart:typed_data'; | ||
import 'dart:ui' as ui; | ||
import 'package:flutter/material.dart'; | ||
|
||
class BgraImage extends StatefulWidget { | ||
final Uint8List? bgraData; | ||
final int width; | ||
final int height; | ||
|
||
BgraImage({ | ||
Key? key, | ||
required this.bgraData, | ||
required this.width, | ||
required this.height, | ||
}) : super(key: key); | ||
|
||
@override | ||
_BgraImageState createState() => _BgraImageState(); | ||
} | ||
|
||
class _BgraImageState extends State<BgraImage> { | ||
late Future<ui.Image?> _imageFuture; | ||
|
||
@override | ||
void initState() { | ||
super.initState(); | ||
if (widget.bgraData != null) { | ||
_imageFuture = _bgraToImage(widget.bgraData!, widget.width, widget.height); | ||
} else { | ||
_imageFuture = Future.value(null); | ||
} | ||
} | ||
|
||
Future<ui.Image?> _bgraToImage(Uint8List bgraData, int width, int height) async { | ||
final completer = Completer<ui.Image>(); | ||
|
||
ui.decodeImageFromPixels( | ||
bgraData, | ||
width, | ||
height, | ||
ui.PixelFormat.bgra8888, | ||
(ui.Image image) { | ||
completer.complete(image); | ||
}, | ||
); | ||
|
||
return completer.future; | ||
} | ||
|
||
@override | ||
Widget build(BuildContext context) { | ||
return FutureBuilder<ui.Image?>( | ||
future: _imageFuture, | ||
builder: (BuildContext context, AsyncSnapshot<ui.Image?> snapshot) { | ||
if (snapshot.connectionState == ConnectionState.done) { | ||
if (snapshot.data != null) { | ||
return RawImage( | ||
image: snapshot.data!, | ||
); | ||
} else { | ||
return Container(color: Colors.black); | ||
} | ||
} else { | ||
return CircularProgressIndicator(); | ||
} | ||
}, | ||
); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,58 @@ | ||
|
||
import 'package:tencent_trtc_cloud/trtc_cloud_def.dart'; | ||
import 'package:flutter/material.dart'; | ||
|
||
import 'package:trtc_demo/ui/bgra_image.dart'; | ||
|
||
class WindowSelectorDialog extends StatelessWidget { | ||
final TRTCScreenCaptureSourceList windows; | ||
|
||
WindowSelectorDialog({required this.windows}); | ||
|
||
@override | ||
Widget build(BuildContext context) { | ||
return AlertDialog( | ||
title: Text('Select a window to share'), | ||
content: Container( | ||
width: double.maxFinite, | ||
child: GridView.builder( | ||
itemCount: windows.count, | ||
gridDelegate: SliverGridDelegateWithFixedCrossAxisCount( | ||
crossAxisCount: 5, | ||
crossAxisSpacing: 8, | ||
mainAxisSpacing: 8, | ||
childAspectRatio: 1, | ||
), | ||
itemBuilder: (context, index) { | ||
final window = windows.sourceInfo[index]; | ||
final imageBuffer = (window.type == TRTCScreenCaptureSourceType.window) | ||
? window.iconBGRA | ||
: window.thumbBGRA; | ||
return InkWell( | ||
onTap: () { | ||
Navigator.of(context).pop(index); | ||
}, | ||
child: Column( | ||
crossAxisAlignment: CrossAxisAlignment.start, | ||
children: [ | ||
Text( | ||
window.sourceName!, | ||
style: TextStyle(fontSize: 12), | ||
), | ||
SizedBox(height: 8), | ||
imageBuffer != null | ||
? BgraImage( | ||
bgraData: imageBuffer.buffer!, | ||
width: imageBuffer.width!, | ||
height: imageBuffer.height!, | ||
) | ||
: Placeholder(fallbackWidth: 50, fallbackHeight: 50), | ||
], | ||
), | ||
); | ||
}, | ||
), | ||
), | ||
); | ||
} | ||
} |