Skip to content

Commit

Permalink
fix.
Browse files Browse the repository at this point in the history
  • Loading branch information
cloudwebrtc committed Nov 20, 2023
1 parent ca2bc19 commit 7c0dfc4
Show file tree
Hide file tree
Showing 2 changed files with 47 additions and 28 deletions.
70 changes: 43 additions & 27 deletions lib/src/widgets/video_track_renderer.dart
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@ import 'package:flutter_webrtc/flutter_webrtc.dart' as rtc;
import '../events.dart';
import '../extensions.dart';
import '../internal/events.dart';
import '../logger.dart';
import '../managers/event.dart';
import '../support/platform.dart';
import '../track/local/local.dart';
Expand Down Expand Up @@ -51,29 +52,38 @@ class VideoTrackRenderer extends StatefulWidget {

class _VideoTrackRendererState extends State<VideoTrackRenderer> {
rtc.RTCVideoRenderer? _renderer;
bool _rendererReady = false;
EventsListener<TrackEvent>? _listener;
// Used to compute visibility information
late GlobalKey _internalKey;

Future<rtc.RTCVideoRenderer?> _initializeRenderer() async {
_renderer ??= rtc.RTCVideoRenderer();
await _renderer!.initialize();
await _attach();
return _renderer;
}

void disposeRenderer() {
try {
_renderer?.srcObject = null;
_renderer?.dispose();
_renderer = null;
} catch (e) {
logger.warning('Got error disposing renderer: $e');
}
}

@override
void initState() {
super.initState();
_internalKey = widget.track.addViewKey();
(() async {
_renderer ??= rtc.RTCVideoRenderer();
await _renderer?.initialize();
await _attach();
setState(() => _rendererReady = true);
})();
}

@override
void dispose() {
widget.track.removeViewKey(_internalKey);
_listener?.dispose();
_renderer?.srcObject = null;
_renderer?.dispose();
disposeRenderer();
super.dispose();
}

Expand Down Expand Up @@ -110,24 +120,30 @@ class _VideoTrackRendererState extends State<VideoTrackRenderer> {
}

@override
Widget build(BuildContext context) => !_rendererReady
? Container()
: Builder(
key: _internalKey,
builder: (ctx) {
// let it render before notifying build
WidgetsBindingCompatible.instance
?.addPostFrameCallback((timeStamp) {
widget.track.onVideoViewBuild?.call(_internalKey);
});
return rtc.RTCVideoView(
_renderer!,
mirror: _shouldMirror(),
filterQuality: FilterQuality.medium,
objectFit: widget.fit,
);
},
);
Widget build(BuildContext context) => FutureBuilder(
future: _initializeRenderer(),
builder: (context, snapshot) {
if (snapshot.hasData && _renderer != null) {
return Builder(
key: _internalKey,
builder: (ctx) {
// let it render before notifying build
WidgetsBindingCompatible.instance
?.addPostFrameCallback((timeStamp) {
widget.track.onVideoViewBuild?.call(_internalKey);
});
return rtc.RTCVideoView(
_renderer!,
mirror: _shouldMirror(),
filterQuality: FilterQuality.medium,
objectFit: widget.fit,
);
},
);
}

return Container();
});

bool _shouldMirror() {
// off for screen share
Expand Down
5 changes: 4 additions & 1 deletion pubspec.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,10 @@ dependencies:
uuid: '>=3.0.6'
synchronized: ^3.0.0+3
protobuf: ^3.0.0
flutter_webrtc: 0.9.46
flutter_webrtc:
git:
url: https://github.com/flutter-webrtc/flutter-webrtc.git
ref: fix/throw-exception-for-set-src-object
flutter_window_close: ^0.2.2
device_info_plus: '>=8.0.0'
js: ^0.6.4
Expand Down

0 comments on commit 7c0dfc4

Please sign in to comment.