Skip to content

Commit

Permalink
Set correct ratio for camera stream widget (viamrobotics#121)
Browse files Browse the repository at this point in the history
  • Loading branch information
njooma authored Sep 29, 2023
1 parent 7b583a1 commit 9a24f3e
Showing 1 changed file with 18 additions and 5 deletions.
23 changes: 18 additions & 5 deletions lib/widgets/camera_stream.dart
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,8 @@ class _ViamCameraStreamViewState extends State<ViamCameraStreamView> {
late RTCVideoRenderer _renderer;
late StreamSubscription<MediaStream> _streamSub;
Exception? _error;
int _width = 160;
int _height = 90;

@override
void initState() {
Expand All @@ -44,7 +46,15 @@ class _ViamCameraStreamViewState extends State<ViamCameraStreamView> {
final stream = widget.streamClient.getStream();
_streamSub = stream.listen((event) {
_error = null;
_renderer.srcObject = event;
_renderer.setSrcObject(stream: event);
_renderer.onResize = () {
setState(() {
if (_renderer.videoWidth > 0 && _renderer.videoHeight > 0) {
_width = _renderer.videoWidth;
_height = _renderer.videoHeight;
}
});
};
setState(() {});
});
_streamSub.onError((error, trace) {
Expand Down Expand Up @@ -74,9 +84,12 @@ class _ViamCameraStreamViewState extends State<ViamCameraStreamView> {
Text(_error.toString(), textAlign: TextAlign.center),
]),
)
: Container(
constraints: const BoxConstraints(maxHeight: 300),
child: RTCVideoView(_renderer),
);
: LayoutBuilder(builder: ((context, constraints) {
return Container(
decoration: const BoxDecoration(color: Colors.black),
constraints: BoxConstraints(maxHeight: _height * constraints.maxWidth / _width),
child: RTCVideoView(_renderer),
);
}));
}
}

0 comments on commit 9a24f3e

Please sign in to comment.