harbour-fernschreiber/qml/components/messageContent/MessageVideo.qml

514 lines
19 KiB
QML
Raw Permalink Normal View History

2020-08-28 11:41:18 +03:00
/*
Copyright (C) 2020 Sebastian J. Wolf and other contributors
2020-08-28 11:41:18 +03:00
This file is part of Fernschreiber.
Fernschreiber is free software: you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation, either version 3 of the License, or
(at your option) any later version.
Fernschreiber is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU General Public License for more details.
You should have received a copy of the GNU General Public License
along with Fernschreiber. If not, see <http://www.gnu.org/licenses/>.
*/
2020-10-31 22:49:03 +03:00
import QtQuick 2.6
2020-08-28 11:41:18 +03:00
import Sailfish.Silica 1.0
2020-10-31 22:50:32 +03:00
import QtMultimedia 5.6
2021-01-15 13:30:55 +03:00
import "../"
import "../../js/functions.js" as Functions
import "../../js/debug.js" as Debug
2020-08-28 11:41:18 +03:00
MessageContentBase {
2020-08-28 11:41:18 +03:00
id: videoMessageComponent
property var videoData: ( rawMessage.content['@type'] === "messageVideo" ) ? rawMessage.content.video : ( ( rawMessage.content['@type'] === "messageAnimation" ) ? rawMessage.content.animation : rawMessage.content.video_note )
2020-08-28 11:41:18 +03:00
property string videoUrl;
property int previewFileId;
property int videoFileId;
property bool isVideoNote : false;
2020-08-28 11:41:18 +03:00
property bool fullscreen : false;
property bool onScreen: messageListItem ? messageListItem.page.status === PageStatus.Active : true;
2020-08-28 14:02:54 +03:00
property string videoType : "video";
2020-09-30 00:37:56 +03:00
property bool playRequested: false;
2020-08-28 11:41:18 +03:00
height: videoMessageComponent.isVideoNote ? width : Functions.getVideoHeight(width, videoData)
2020-08-28 11:41:18 +03:00
Timer {
id: screensaverTimer
interval: 30000
running: false
repeat: true
triggeredOnStart: true
onTriggered: {
tdLibWrapper.controlScreenSaver(false);
}
}
function getTimeString(rawSeconds) {
var minutes = Math.floor( rawSeconds / 60 );
var seconds = rawSeconds - ( minutes * 60 );
if ( minutes < 10 ) {
minutes = "0" + minutes;
}
if ( seconds < 10 ) {
seconds = "0" + seconds;
}
return minutes + ":" + seconds;
}
function disableScreensaver() {
screensaverTimer.start();
}
function enableScreensaver() {
screensaverTimer.stop();
tdLibWrapper.controlScreenSaver(true);
}
Component.onCompleted: {
updateVideoThumbnail();
}
function updateVideoThumbnail() {
2020-08-29 17:58:48 +03:00
if (videoData) {
if (typeof rawMessage !== "undefined") {
videoMessageComponent.isVideoNote = rawMessage.content['@type'] === "messageVideoNote";
}
videoMessageComponent.videoType = videoMessageComponent.isVideoNote ? "video" : videoData['@type'];
2020-08-28 14:02:54 +03:00
videoFileId = videoData[videoType].id;
2021-01-06 14:58:16 +03:00
if (typeof rawMessage !== "undefined" && rawMessage.content['@type'] === "messageAnimation") {
playButton.visible = true;
fullscreenButton.visible = !videoMessageComponent.fullscreen;
handlePlay();
} else if (typeof videoData.thumbnail !== "undefined") {
previewFileId = videoData.thumbnail.file.id;
if (videoData.thumbnail.file.local.is_downloading_completed) {
placeholderImage.source = videoData.thumbnail.file.local.path;
2020-08-28 14:02:54 +03:00
} else {
tdLibWrapper.downloadFile(previewFileId);
}
2020-08-28 11:41:18 +03:00
} else {
2023-06-20 10:46:01 +03:00
placeholderImage.source = "image://theme/icon-m-video?white";
2020-08-28 14:02:54 +03:00
placeholderImage.width = Theme.itemSizeLarge
placeholderImage.height = Theme.itemSizeLarge
2020-08-28 11:41:18 +03:00
}
}
}
function handlePlay() {
2020-09-30 00:37:56 +03:00
playRequested = true;
2020-08-28 14:02:54 +03:00
if (videoData[videoType].local.is_downloading_completed) {
videoUrl = videoData[videoType].local.path;
2020-08-28 11:41:18 +03:00
videoComponentLoader.active = true;
} else {
videoDownloadBusyIndicator.running = true;
tdLibWrapper.downloadFile(videoFileId);
}
}
Connections {
target: tdLibWrapper
onFileUpdated: {
2020-08-29 17:58:48 +03:00
if (videoData) {
if (fileInformation.local.is_downloading_completed && fileId === previewFileId) {
videoData.thumbnail.photo = fileInformation;
placeholderImage.source = fileInformation.local.path;
}
2020-09-30 00:37:56 +03:00
if (!fileInformation.remote.is_uploading_active && fileInformation.local.is_downloading_completed && fileId === videoFileId) {
2020-08-29 17:58:48 +03:00
videoDownloadBusyIndicator.running = false;
videoData[videoType] = fileInformation;
videoUrl = fileInformation.local.path;
2020-09-30 00:37:56 +03:00
if (onScreen && playRequested) {
playRequested = false;
2020-08-29 17:58:48 +03:00
videoComponentLoader.active = true;
2020-08-28 11:41:18 +03:00
}
}
2020-11-14 23:01:52 +03:00
if (fileId === videoFileId) {
downloadingProgressBar.maximumValue = fileInformation.size;
downloadingProgressBar.value = fileInformation.local.downloaded_size;
}
2020-08-28 11:41:18 +03:00
}
}
}
Image {
id: placeholderImage
width: parent.width
height: parent.height
2020-08-28 14:02:54 +03:00
anchors.centerIn: parent
2020-08-28 11:41:18 +03:00
fillMode: Image.PreserveAspectCrop
2020-08-29 17:32:43 +03:00
asynchronous: true
2020-08-28 11:41:18 +03:00
visible: status === Image.Ready ? true : false
layer.enabled: videoMessageComponent.highlighted
2020-12-21 05:42:06 +03:00
layer.effect: PressEffect { source: placeholderImage }
2020-08-28 11:41:18 +03:00
}
BackgroundImage {
2020-08-28 11:41:18 +03:00
visible: placeholderImage.status !== Image.Ready
}
Rectangle {
color: "black"
opacity: 0.3
height: parent.height
width: parent.width
visible: playButton.visible
}
2020-11-14 23:01:52 +03:00
Column {
2020-08-28 11:41:18 +03:00
width: parent.width
2020-11-14 23:01:52 +03:00
height: downloadingProgressBar.height + videoControlRow.height
anchors.centerIn: parent
Row {
id: videoControlRow
width: parent.width
Item {
width: videoMessageComponent.fullscreen ? parent.width : ( parent.width / 2 )
2020-08-28 11:41:18 +03:00
height: Theme.iconSizeLarge
IconButton {
2020-11-14 23:01:52 +03:00
id: playButton
anchors.centerIn: parent
width: Theme.iconSizeLarge
height: Theme.iconSizeLarge
icon {
source: "image://theme/icon-l-play?white"
asynchronous: true
}
highlighted: videoMessageComponent.highlighted || down
2020-11-14 23:01:52 +03:00
visible: placeholderImage.status === Image.Ready ? true : false
onClicked: {
fullscreenItem.visible = false;
handlePlay();
2020-08-28 11:41:18 +03:00
}
}
2020-11-14 23:01:52 +03:00
BusyIndicator {
id: videoDownloadBusyIndicator
running: false
visible: running
anchors.centerIn: parent
size: BusyIndicatorSize.Large
}
2020-08-28 11:41:18 +03:00
}
2020-11-14 23:01:52 +03:00
Item {
id: fullscreenItem
width: parent.width / 2
2020-08-28 11:41:18 +03:00
height: Theme.iconSizeLarge
2020-11-14 23:01:52 +03:00
visible: !videoMessageComponent.fullscreen
IconButton {
id: fullscreenButton
anchors.centerIn: parent
width: Theme.iconSizeLarge
height: Theme.iconSizeLarge
icon {
asynchronous: true
2021-01-15 13:30:55 +03:00
source: "../../../images/icon-l-fullscreen.svg"
2020-11-14 23:01:52 +03:00
sourceSize {
width: Theme.iconSizeLarge
height: Theme.iconSizeLarge
}
}
highlighted: videoMessageComponent.highlighted || down
2020-11-14 23:01:52 +03:00
visible: ( placeholderImage.status === Image.Ready && !videoMessageComponent.fullscreen ) ? true : false
onClicked: {
pageStack.push(Qt.resolvedUrl("../../pages/VideoPage.qml"), {"videoData": videoData, "sourceMessage": rawMessage});
2020-08-28 11:41:18 +03:00
}
}
2020-08-28 11:41:18 +03:00
}
}
2020-11-14 23:01:52 +03:00
ProgressBar {
id: downloadingProgressBar
minimumValue: 0
maximumValue: 100
value: 0
visible: videoDownloadBusyIndicator.visible
width: parent.width
}
2020-08-28 11:41:18 +03:00
}
Rectangle {
width: parent.width
height: parent.height
color: "lightgrey"
visible: placeholderImage.status === Image.Error ? true : false
opacity: 0.3
}
Rectangle {
id: errorTextOverlay
color: "black"
opacity: 0.8
width: parent.width
height: parent.height
visible: false
}
Text {
id: errorText
visible: false
width: parent.width
color: Theme.primaryColor
font.pixelSize: Theme.fontSizeExtraSmall
horizontalAlignment: Text.AlignHCenter
anchors {
verticalCenter: parent.verticalCenter
}
wrapMode: Text.Wrap
text: ""
}
Loader {
id: videoComponentLoader
active: false
width: parent.width
height: videoMessageComponent.isVideoNote ? width : Functions.getVideoHeight(parent.width, videoData)
2020-08-28 11:41:18 +03:00
sourceComponent: videoComponent
}
Component {
id: videoComponent
Item {
width: parent ? parent.width : 0
height: parent ? parent.height : 0
Connections {
target: messageVideo
onPlaying: {
playButton.visible = false;
placeholderImage.visible = false;
messageVideo.visible = true;
}
}
Video {
id: messageVideo
Component.onCompleted: {
if (messageVideo.error === MediaPlayer.NoError) {
messageVideo.play();
timeLeftTimer.start();
} else {
errorText.text = qsTr("Error loading video! " + messageVideo.errorString)
errorTextOverlay.visible = true;
errorText.visible = true;
}
}
onStatusChanged: {
if (status == MediaPlayer.NoMedia) {
2020-11-20 23:58:26 +03:00
Debug.log("No Media");
2020-08-28 11:41:18 +03:00
videoBusyIndicator.visible = false;
}
if (status == MediaPlayer.Loading) {
2020-11-20 23:58:26 +03:00
Debug.log("Loading");
2020-08-28 11:41:18 +03:00
videoBusyIndicator.visible = true;
}
if (status == MediaPlayer.Loaded) {
2020-11-20 23:58:26 +03:00
Debug.log("Loaded");
2020-08-28 11:41:18 +03:00
videoBusyIndicator.visible = false;
}
if (status == MediaPlayer.Buffering) {
2020-11-20 23:58:26 +03:00
Debug.log("Buffering");
2020-08-28 11:41:18 +03:00
videoBusyIndicator.visible = true;
}
if (status == MediaPlayer.Stalled) {
2020-11-20 23:58:26 +03:00
Debug.log("Stalled");
2020-08-28 11:41:18 +03:00
videoBusyIndicator.visible = true;
}
if (status == MediaPlayer.Buffered) {
2020-11-20 23:58:26 +03:00
Debug.log("Buffered");
2020-08-28 11:41:18 +03:00
videoBusyIndicator.visible = false;
}
if (status == MediaPlayer.EndOfMedia) {
2020-11-20 23:58:26 +03:00
Debug.log("End of Media");
2020-08-28 11:41:18 +03:00
videoBusyIndicator.visible = false;
}
if (status == MediaPlayer.InvalidMedia) {
2020-11-20 23:58:26 +03:00
Debug.log("Invalid Media");
2020-08-28 11:41:18 +03:00
videoBusyIndicator.visible = false;
}
if (status == MediaPlayer.UnknownStatus) {
2020-11-20 23:58:26 +03:00
Debug.log("Unknown Status");
2020-08-28 11:41:18 +03:00
videoBusyIndicator.visible = false;
}
}
visible: false
width: parent.width
height: parent.height
source: videoUrl
layer.enabled: videoMessageComponent.highlighted
2021-01-06 14:58:16 +03:00
layer.effect: PressEffect { source: messageVideo }
2020-08-28 11:41:18 +03:00
onStopped: {
enableScreensaver();
messageVideo.visible = false;
placeholderImage.visible = true;
playButton.visible = true;
videoComponentLoader.active = false;
fullscreenItem.visible = !videoMessageComponent.fullscreen;
}
2021-01-06 14:58:16 +03:00
MouseArea {
anchors.fill: parent
onClicked: {
if (messageVideo.playbackState === MediaPlayer.PlayingState) {
enableScreensaver();
messageVideo.pause();
timeLeftItem.visible = true;
} else {
disableScreensaver();
messageVideo.play();
timeLeftTimer.start();
}
}
}
2020-08-28 11:41:18 +03:00
}
BusyIndicator {
id: videoBusyIndicator
anchors.horizontalCenter: parent.horizontalCenter
anchors.verticalCenter: parent.verticalCenter
visible: false
running: visible
size: BusyIndicatorSize.Medium
onVisibleChanged: {
if (visible) {
enableScreensaver();
} else {
disableScreensaver();
}
}
}
Timer {
id: timeLeftTimer
repeat: false
interval: 2000
onTriggered: {
timeLeftItem.visible = false;
}
}
Item {
id: timeLeftItem
width: parent.width
height: parent.height
anchors.bottom: parent.bottom
anchors.horizontalCenter: parent.horizontalCenter
visible: messageVideo.visible
opacity: visible ? 1 : 0
Behavior on opacity { NumberAnimation {} }
Rectangle {
id: positionTextOverlay
color: "black"
opacity: 0.3
width: parent.width
height: parent.height
anchors.bottom: parent.bottom
anchors.horizontalCenter: parent.horizontalCenter
visible: pausedRow.visible
}
Row {
id: pausedRow
width: parent.width
height: parent.height - ( messageVideoSlider.visible ? messageVideoSlider.height : 0 ) - ( positionText.visible ? positionText.height : 0 )
visible: videoComponentLoader.active && messageVideo.playbackState === MediaPlayer.PausedState
Item {
height: parent.height
width: videoMessageComponent.fullscreen ? parent.width : ( parent.width / 2 )
IconButton {
2020-08-28 11:41:18 +03:00
id: pausedPlayButton
anchors.centerIn: parent
width: Theme.iconSizeLarge
height: Theme.iconSizeLarge
highlighted: videoMessageComponent.highlighted || down
icon {
asynchronous: true
source: "image://theme/icon-l-play?white"
}
onClicked: {
disableScreensaver();
messageVideo.play();
timeLeftTimer.start();
2020-08-28 11:41:18 +03:00
}
}
}
Item {
id: pausedFullscreenItem
height: parent.height
width: parent.width / 2
visible: !videoMessageComponent.fullscreen
IconButton {
2020-08-28 11:41:18 +03:00
id: pausedFullscreenButton
anchors.centerIn: parent
width: Theme.iconSizeLarge
height: Theme.iconSizeLarge
highlighted: videoMessageComponent.highlighted || down
icon {
asynchronous: true
2021-01-15 13:30:55 +03:00
source: "../../../images/icon-l-fullscreen.svg"
sourceSize {
width: Theme.iconSizeLarge
height: Theme.iconSizeLarge
2020-08-28 11:41:18 +03:00
}
}
visible: ( videoComponentLoader.active && messageVideo.playbackState === MediaPlayer.PausedState ) ? true : false
onClicked: {
pageStack.push(Qt.resolvedUrl("../../pages/VideoPage.qml"), {"videoData": videoData, "sourceMessage": rawMessage});
}
2020-08-28 11:41:18 +03:00
}
}
}
Slider {
id: messageVideoSlider
width: parent.width
anchors.horizontalCenter: parent.horizontalCenter
anchors.bottom: positionText.top
minimumValue: 0
maximumValue: messageVideo.duration ? messageVideo.duration : 0.1
highlighted: videoMessageComponent.highlighted || down
2020-08-28 11:41:18 +03:00
stepSize: 1
value: messageVideo.position
enabled: messageVideo.seekable
visible: (messageVideo.duration > 0)
onReleased: {
messageVideo.seek(Math.floor(value));
messageVideo.play();
timeLeftTimer.start();
}
valueText: getTimeString(Math.round((messageVideo.duration - messageVideoSlider.value) / 1000))
}
Text {
id: positionText
visible: messageVideo.visible && messageVideo.duration === 0
color: videoMessageComponent.highlighted ? Theme.secondaryColor : Theme.primaryColor
2020-08-28 11:41:18 +03:00
font.pixelSize: videoMessageComponent.fullscreen ? Theme.fontSizeSmall : Theme.fontSizeTiny
anchors {
bottom: parent.bottom
bottomMargin: Theme.paddingSmall
horizontalCenter: positionTextOverlay.horizontalCenter
}
wrapMode: Text.Wrap
text: ( messageVideo.duration - messageVideo.position ) > 0 ? getTimeString(Math.round((messageVideo.duration - messageVideo.position) / 1000)) : "-:-"
}
}
}
}
}