Camera stream aspect ratio calculations

This commit is contained in:
Yegor Vialov 2020-02-20 21:48:22 +00:00
parent 35d8607484
commit 06f994a827
2 changed files with 24 additions and 14 deletions

View File

@ -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);

View File

@ -16,7 +16,7 @@ class _CameraStreamViewState extends State<CameraStreamView> {
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<CameraStreamView> {
}
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<CameraStreamView> {
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<CameraStreamView> {
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<CameraStreamView> {
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('.', '_')));
});
},
),