This repository has been archived on 2023-11-18. You can view files and clone it, but cannot push or open issues or pull requests.
ha_client/lib/entities/camera/widgets/camera_stream_view.dart

138 lines
3.8 KiB
Dart
Raw Normal View History

2019-09-09 18:50:35 +03:00
part of '../../../main.dart';
2019-01-29 21:59:05 +02:00
class CameraStreamView extends StatefulWidget {
2019-01-29 21:59:05 +02:00
CameraStreamView({Key key}) : super(key: key);
2019-01-29 21:59:05 +02:00
@override
_CameraStreamViewState createState() => _CameraStreamViewState();
2019-01-29 21:59:05 +02:00
}
class _CameraStreamViewState extends State<CameraStreamView> {
2019-01-29 21:59:05 +02:00
2020-02-20 16:33:03 +02:00
CameraEntity _entity;
String streamUrl = "";
WebViewController webViewController;
VideoPlayerController videoPlayerController;
Timer monitorTimer;
bool started = false;
double aspectRatio = 1.33;
2020-02-20 16:33:03 +02:00
2019-01-29 21:59:05 +02:00
@override
void initState() {
super.initState();
2019-01-31 01:04:13 +02:00
}
2020-02-20 16:33:03 +02:00
void loadStreamUrl() {
Logger.d("[Camera Player] Loading stream url");
HomeAssistant().getCameraStream(_entity.entityId)
.then((data) {
Logger.d("[Camera Player] Stream url: ${ConnectionManager().httpWebHost}${data["url"]}");
if (videoPlayerController != null) {
videoPlayerController.dispose().then((_) => createPlayer(data));
} else {
createPlayer(data);
}
})
.catchError((e) => Logger.e("[Camera Player] $e"));
}
2019-01-29 21:59:05 +02:00
2020-02-20 16:33:03 +02:00
void createPlayer(data) {
videoPlayerController = VideoPlayerController.network("${ConnectionManager().httpWebHost}${data["url"]}");
videoPlayerController.initialize().then((_) {
setState((){
started = true;
2020-02-20 16:33:03 +02:00
aspectRatio = videoPlayerController.value.aspectRatio;
});
autoPlay();
startMonitor();
}).catchError((e) {
Logger.e("[Camera Player] Error player init. Retrying");
loadStreamUrl();
});
}
void autoPlay() {
if (!videoPlayerController.value.isPlaying) {
videoPlayerController.play();
}
}
void startMonitor() {
monitorTimer = Timer.periodic(Duration(milliseconds: 500), (timer) {
if (videoPlayerController.value.hasError) {
setState(() {
timer.cancel();
started = false;
});
}
});
}
Widget buildLoading() {
return AspectRatio(
aspectRatio: aspectRatio,
child: Center(
child: CircularProgressIndicator()
)
);
2019-02-20 13:57:25 +02:00
}
2019-01-29 21:59:05 +02:00
@override
Widget build(BuildContext context) {
_entity = EntityModel
.of(context)
.entityWrapper
.entity;
2020-02-20 16:33:03 +02:00
if (_entity.supportStream && !started) {
loadStreamUrl();
return buildLoading();
} else if (_entity.supportStream) {
2020-02-20 16:33:03 +02:00
if (videoPlayerController.value.initialized) {
return AspectRatio(
aspectRatio: aspectRatio,
child: VideoPlayer(videoPlayerController),
);
} else {
return buildLoading();
}
} else {
streamUrl = '${ConnectionManager().httpWebHost}/api/camera_proxy_stream/${_entity
.entityId}?token=${_entity.attributes['access_token']}';
return AspectRatio(
aspectRatio: aspectRatio,
2020-02-20 16:33:03 +02:00
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;
});
})
)
},
2020-02-20 16:33:03 +02:00
onWebViewCreated: (WebViewController controller) {
webViewController = controller;
},
onPageStarted: (url) {
rootBundle.loadString('assets/js/cameraImgViewHelper.js').then((js){
webViewController.evaluateJavascript(js.replaceFirst('entity_id_placeholder', _entity.entityId.replaceAll('.', '_')));
2020-02-20 16:33:03 +02:00
});
},
),
);
}
2019-01-29 21:59:05 +02:00
}
@override
void dispose() {
2020-02-20 16:33:03 +02:00
monitorTimer?.cancel();
videoPlayerController?.dispose();
2019-01-29 21:59:05 +02:00
super.dispose();
}
}