2019-09-09 18:50:35 +03:00
|
|
|
part of '../../../main.dart';
|
2019-01-29 21:59:05 +02:00
|
|
|
|
2019-02-20 16:39:57 +02:00
|
|
|
class CameraStreamView extends StatefulWidget {
|
2019-01-29 21:59:05 +02:00
|
|
|
|
2019-02-20 16:39:57 +02:00
|
|
|
CameraStreamView({Key key}) : super(key: key);
|
2019-01-29 21:59:05 +02:00
|
|
|
|
|
|
|
@override
|
2019-02-20 16:39:57 +02:00
|
|
|
_CameraStreamViewState createState() => _CameraStreamViewState();
|
2019-01-29 21:59:05 +02:00
|
|
|
}
|
|
|
|
|
2019-02-20 16:39:57 +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;
|
2020-02-20 23:48:22 +02:00
|
|
|
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((){
|
2020-02-20 23:48:22 +02:00
|
|
|
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-06-21 21:01:53 +03:00
|
|
|
);
|
2019-02-20 13:57:25 +02:00
|
|
|
}
|
|
|
|
|
2019-01-29 21:59:05 +02:00
|
|
|
@override
|
|
|
|
Widget build(BuildContext context) {
|
2020-02-20 13:18:09 +02:00
|
|
|
_entity = EntityModel
|
2019-02-20 16:39:57 +02:00
|
|
|
.of(context)
|
|
|
|
.entityWrapper
|
|
|
|
.entity;
|
2020-02-20 16:33:03 +02:00
|
|
|
if (_entity.supportStream && !started) {
|
|
|
|
loadStreamUrl();
|
|
|
|
return buildLoading();
|
2020-02-20 23:48:22 +02:00
|
|
|
} 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(
|
2020-02-20 23:48:22 +02:00
|
|
|
aspectRatio: aspectRatio,
|
2020-02-20 16:33:03 +02:00
|
|
|
child: WebView(
|
|
|
|
initialUrl: streamUrl,
|
|
|
|
initialMediaPlaybackPolicy: AutoMediaPlaybackPolicy.always_allow,
|
|
|
|
debuggingEnabled: Logger.isInDebugMode,
|
|
|
|
javascriptMode: JavascriptMode.unrestricted,
|
2020-02-20 23:48:22 +02:00
|
|
|
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){
|
2020-02-20 23:48:22 +02:00
|
|
|
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();
|
|
|
|
}
|
|
|
|
}
|