diff --git a/assets/js/cameraImgViewHelper.js b/assets/js/cameraImgViewHelper.js index 9bc184a..5df0c40 100644 --- a/assets/js/cameraImgViewHelper.js +++ b/assets/js/cameraImgViewHelper.js @@ -1,18 +1,20 @@ +var messageChannel = 'HA_entity_id_placeholder'; + function fixCameraImgView() { + window.clearInterval(window.bodyDetectInterval); var img = document.getElementsByTagName('img'); if (img && img.length) { img[0].setAttribute('width', document.body.clientWidth); - img[0].setAttribute('style', 'margin-top: ' + ((document.body.clientHeight - img[0].offsetHeight) / 2)); - } - var ovrl = document.getElementById('appOverlay'); - if (ovrl) { - ovrl.remove(); + img[0].removeAttribute('style'); + setTimeout(function() { + window[messageChannel].postMessage(document.body.clientWidth / img[0].offsetHeight); + }, 100); } } window.bodyDetectInterval = setInterval(function() { - if (document.body != null) { - setTimeout(fixCameraImgView, 1000); + if (document.body != null && document.getElementsByTagName('img').length) { + fixCameraImgView(); } }, 100); \ No newline at end of file diff --git a/lib/entities/camera/widgets/camera_stream_view.dart b/lib/entities/camera/widgets/camera_stream_view.dart index 2e51483..d9384fc 100644 --- a/lib/entities/camera/widgets/camera_stream_view.dart +++ b/lib/entities/camera/widgets/camera_stream_view.dart @@ -16,7 +16,7 @@ class _CameraStreamViewState extends State { VideoPlayerController videoPlayerController; Timer monitorTimer; bool started = false; - double aspectRatio = 1.78; + double aspectRatio = 1.33; @override void initState() { @@ -24,9 +24,6 @@ class _CameraStreamViewState extends State { } void loadStreamUrl() { - setState((){ - started = true; - }); Logger.d("[Camera Player] Loading stream url"); HomeAssistant().getCameraStream(_entity.entityId) .then((data) { @@ -44,6 +41,7 @@ class _CameraStreamViewState extends State { videoPlayerController = VideoPlayerController.network("${ConnectionManager().httpWebHost}${data["url"]}"); videoPlayerController.initialize().then((_) { setState((){ + started = true; aspectRatio = videoPlayerController.value.aspectRatio; }); autoPlay(); @@ -89,7 +87,7 @@ class _CameraStreamViewState extends State { if (_entity.supportStream && !started) { loadStreamUrl(); return buildLoading(); - } else if (_entity.supportStream && started) { + } else if (_entity.supportStream) { if (videoPlayerController.value.initialized) { return AspectRatio( aspectRatio: aspectRatio, @@ -102,18 +100,28 @@ class _CameraStreamViewState extends State { streamUrl = '${ConnectionManager().httpWebHost}/api/camera_proxy_stream/${_entity .entityId}?token=${_entity.attributes['access_token']}'; return AspectRatio( - aspectRatio: 1.33, + aspectRatio: aspectRatio, child: WebView( initialUrl: streamUrl, initialMediaPlaybackPolicy: AutoMediaPlaybackPolicy.always_allow, debuggingEnabled: Logger.isInDebugMode, javascriptMode: JavascriptMode.unrestricted, + javascriptChannels: { + JavascriptChannel( + name: 'HA_${_entity.entityId.replaceAll('.', '_')}', + onMessageReceived: ((message) { + setState((){ + aspectRatio = double.tryParse(message.message) ?? 1.33; + }); + }) + ) + }, onWebViewCreated: (WebViewController controller) { webViewController = controller; }, onPageStarted: (url) { rootBundle.loadString('assets/js/cameraImgViewHelper.js').then((js){ - webViewController.evaluateJavascript(js); + webViewController.evaluateJavascript(js.replaceFirst('entity_id_placeholder', _entity.entityId.replaceAll('.', '_'))); }); }, ),