2015-11-27 15:02:32 +00:00
|
|
|
/*
|
2021-07-16 10:57:14 -06:00
|
|
|
Copyright 2015 - 2021 The Matrix.org Foundation C.I.C.
|
2015-11-27 15:02:32 +00:00
|
|
|
|
|
|
|
|
Licensed under the Apache License, Version 2.0 (the "License");
|
|
|
|
|
you may not use this file except in compliance with the License.
|
|
|
|
|
You may obtain a copy of the License at
|
|
|
|
|
|
|
|
|
|
http://www.apache.org/licenses/LICENSE-2.0
|
|
|
|
|
|
|
|
|
|
Unless required by applicable law or agreed to in writing, software
|
|
|
|
|
distributed under the License is distributed on an "AS IS" BASIS,
|
|
|
|
|
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
|
|
|
|
See the License for the specific language governing permissions and
|
|
|
|
|
limitations under the License.
|
|
|
|
|
*/
|
|
|
|
|
|
2023-03-13 15:07:20 +00:00
|
|
|
import React, { ReactNode } from "react";
|
2021-05-21 21:20:00 +01:00
|
|
|
import { decode } from "blurhash";
|
2021-12-09 09:10:23 +00:00
|
|
|
import { logger } from "matrix-js-sdk/src/logger";
|
2021-05-21 21:20:00 +01:00
|
|
|
|
2017-05-30 16:09:57 +02:00
|
|
|
import { _t } from "../../../languageHandler";
|
2017-10-29 01:43:52 -06:00
|
|
|
import SettingsStore from "../../../settings/SettingsStore";
|
2020-04-24 22:14:41 +01:00
|
|
|
import InlineSpinner from "../elements/InlineSpinner";
|
2021-06-29 13:11:58 +01:00
|
|
|
import { mediaFromContent } from "../../../customisations/Media";
|
2022-03-24 15:13:11 -06:00
|
|
|
import { BLURHASH_FIELD } from "../../../utils/image-media";
|
2021-07-14 20:51:20 -06:00
|
|
|
import { IMediaEventContent } from "../../../customisations/models/IMediaEventContent";
|
2021-07-16 10:57:14 -06:00
|
|
|
import { IBodyProps } from "./IBodyProps";
|
2021-07-16 11:05:04 -06:00
|
|
|
import MFileBody from "./MFileBody";
|
2021-11-17 08:19:30 -07:00
|
|
|
import { ImageSize, suggestedSize as suggestedVideoSize } from "../../../settings/enums/ImageSize";
|
2022-02-18 15:56:05 +00:00
|
|
|
import RoomContext, { TimelineRenderingType } from "../../../contexts/RoomContext";
|
2022-07-06 13:29:26 +02:00
|
|
|
import MediaProcessingError from "./shared/MediaProcessingError";
|
2021-09-21 17:48:09 +02:00
|
|
|
|
2020-10-23 16:31:30 +01:00
|
|
|
interface IState {
|
2023-03-13 15:07:20 +00:00
|
|
|
decryptedUrl: string | null;
|
|
|
|
|
decryptedThumbnailUrl: string | null;
|
|
|
|
|
decryptedBlob: Blob | null;
|
2021-07-01 20:48:34 +01:00
|
|
|
error?: any;
|
|
|
|
|
fetchingData: boolean;
|
2021-05-21 21:20:00 +01:00
|
|
|
posterLoading: boolean;
|
2023-03-13 15:07:20 +00:00
|
|
|
blurhashUrl: string | null;
|
2020-10-23 16:31:30 +01:00
|
|
|
}
|
2016-12-20 12:33:07 +00:00
|
|
|
|
2021-07-16 10:57:14 -06:00
|
|
|
export default class MVideoBody extends React.PureComponent<IBodyProps, IState> {
|
2022-12-16 12:29:59 +00:00
|
|
|
public static contextType = RoomContext;
|
2022-02-18 15:56:05 +00:00
|
|
|
public context!: React.ContextType<typeof RoomContext>;
|
|
|
|
|
|
2020-11-19 13:26:14 +00:00
|
|
|
private videoRef = React.createRef<HTMLVideoElement>();
|
2023-05-10 08:41:55 +01:00
|
|
|
private sizeWatcher?: string;
|
2020-11-19 13:26:14 +00:00
|
|
|
|
2023-02-13 11:39:16 +00:00
|
|
|
public constructor(props: IBodyProps) {
|
2020-10-23 16:31:30 +01:00
|
|
|
super(props);
|
2021-07-14 20:51:20 -06:00
|
|
|
|
2020-10-23 16:31:30 +01:00
|
|
|
this.state = {
|
2020-10-23 16:33:40 +01:00
|
|
|
fetchingData: false,
|
2020-10-23 16:31:30 +01:00
|
|
|
decryptedUrl: null,
|
|
|
|
|
decryptedThumbnailUrl: null,
|
|
|
|
|
decryptedBlob: null,
|
|
|
|
|
error: null,
|
2021-05-21 21:20:00 +01:00
|
|
|
posterLoading: false,
|
|
|
|
|
blurhashUrl: null,
|
2021-06-29 13:11:58 +01:00
|
|
|
};
|
2020-10-23 16:31:30 +01:00
|
|
|
}
|
2016-11-04 13:05:34 +00:00
|
|
|
|
2023-03-13 15:07:20 +00:00
|
|
|
private getContentUrl(): string | undefined {
|
2021-07-21 11:35:27 +05:30
|
|
|
const content = this.props.mxEvent.getContent<IMediaEventContent>();
|
2021-08-14 00:14:57 +05:30
|
|
|
// During export, the content url will point to the MSC, which will later point to a local url
|
2023-03-13 15:07:20 +00:00
|
|
|
if (this.props.forExport) return content.file?.url ?? content.url;
|
2021-06-29 12:54:44 +05:30
|
|
|
const media = mediaFromContent(content);
|
2021-03-03 19:06:46 -07:00
|
|
|
if (media.isEncrypted) {
|
2023-03-13 15:07:20 +00:00
|
|
|
return this.state.decryptedUrl ?? undefined;
|
2016-11-04 13:05:34 +00:00
|
|
|
} else {
|
2023-03-13 15:07:20 +00:00
|
|
|
return media.srcHttp ?? undefined;
|
2016-11-04 13:05:34 +00:00
|
|
|
}
|
2020-08-29 12:14:16 +01:00
|
|
|
}
|
2016-11-04 13:05:34 +00:00
|
|
|
|
2020-11-19 13:26:14 +00:00
|
|
|
private hasContentUrl(): boolean {
|
|
|
|
|
const url = this.getContentUrl();
|
2023-03-13 15:07:20 +00:00
|
|
|
return !!url && !url.startsWith("data:");
|
2020-11-19 13:26:14 +00:00
|
|
|
}
|
|
|
|
|
|
2020-11-18 17:41:54 +00:00
|
|
|
private getThumbUrl(): string | null {
|
2021-05-31 21:01:19 +05:30
|
|
|
// there's no need of thumbnail when the content is local
|
2021-06-09 15:23:47 +05:30
|
|
|
if (this.props.forExport) return null;
|
2021-05-31 21:01:19 +05:30
|
|
|
|
2021-07-14 20:51:20 -06:00
|
|
|
const content = this.props.mxEvent.getContent<IMediaEventContent>();
|
2021-03-03 19:06:46 -07:00
|
|
|
const media = mediaFromContent(content);
|
2021-05-21 21:20:00 +01:00
|
|
|
|
|
|
|
|
if (media.isEncrypted && this.state.decryptedThumbnailUrl) {
|
2016-11-04 13:05:34 +00:00
|
|
|
return this.state.decryptedThumbnailUrl;
|
2021-05-21 21:20:00 +01:00
|
|
|
} else if (this.state.posterLoading) {
|
|
|
|
|
return this.state.blurhashUrl;
|
2021-03-03 19:06:46 -07:00
|
|
|
} else if (media.hasThumbnail) {
|
|
|
|
|
return media.thumbnailHttp;
|
2016-11-04 13:05:34 +00:00
|
|
|
} else {
|
2016-11-04 13:13:25 +00:00
|
|
|
return null;
|
2016-11-04 13:05:34 +00:00
|
|
|
}
|
2020-08-29 12:14:16 +01:00
|
|
|
}
|
2016-11-04 13:05:34 +00:00
|
|
|
|
2023-01-12 13:25:14 +00:00
|
|
|
private loadBlurhash(): void {
|
2021-05-21 21:20:00 +01:00
|
|
|
const info = this.props.mxEvent.getContent()?.info;
|
|
|
|
|
if (!info[BLURHASH_FIELD]) return;
|
|
|
|
|
|
|
|
|
|
const canvas = document.createElement("canvas");
|
|
|
|
|
|
2022-03-22 18:16:03 -04:00
|
|
|
const { w: width, h: height } = suggestedVideoSize(SettingsStore.getValue("Images.size") as ImageSize, {
|
|
|
|
|
w: info.w,
|
|
|
|
|
h: info.h,
|
|
|
|
|
});
|
2021-05-21 21:20:00 +01:00
|
|
|
|
|
|
|
|
canvas.width = width;
|
|
|
|
|
canvas.height = height;
|
|
|
|
|
|
|
|
|
|
const pixels = decode(info[BLURHASH_FIELD], width, height);
|
2023-03-29 08:22:35 +01:00
|
|
|
const ctx = canvas.getContext("2d")!;
|
2021-05-21 21:20:00 +01:00
|
|
|
const imgData = ctx.createImageData(width, height);
|
|
|
|
|
imgData.data.set(pixels);
|
|
|
|
|
ctx.putImageData(imgData, 0, 0);
|
|
|
|
|
|
|
|
|
|
this.setState({
|
|
|
|
|
blurhashUrl: canvas.toDataURL(),
|
|
|
|
|
posterLoading: true,
|
|
|
|
|
});
|
|
|
|
|
|
2021-07-14 20:51:20 -06:00
|
|
|
const content = this.props.mxEvent.getContent<IMediaEventContent>();
|
2021-05-21 21:20:00 +01:00
|
|
|
const media = mediaFromContent(content);
|
|
|
|
|
if (media.hasThumbnail) {
|
|
|
|
|
const image = new Image();
|
|
|
|
|
image.onload = () => {
|
|
|
|
|
this.setState({ posterLoading: false });
|
|
|
|
|
};
|
2023-03-29 08:22:35 +01:00
|
|
|
image.src = media.thumbnailHttp!;
|
2021-05-21 21:20:00 +01:00
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2023-01-12 13:25:14 +00:00
|
|
|
public async componentDidMount(): Promise<void> {
|
2021-11-17 08:19:30 -07:00
|
|
|
this.sizeWatcher = SettingsStore.watchSetting("Images.size", null, () => {
|
|
|
|
|
this.forceUpdate(); // we don't really have a reliable thing to update, so just update the whole thing
|
|
|
|
|
});
|
|
|
|
|
|
2022-06-13 13:25:32 +01:00
|
|
|
try {
|
|
|
|
|
this.loadBlurhash();
|
|
|
|
|
} catch (e) {
|
|
|
|
|
logger.error("Failed to load blurhash", e);
|
|
|
|
|
}
|
2021-05-21 21:20:00 +01:00
|
|
|
|
2021-07-15 14:19:07 -06:00
|
|
|
if (this.props.mediaEventHelper.media.isEncrypted && this.state.decryptedUrl === null) {
|
2020-10-23 16:33:40 +01:00
|
|
|
try {
|
2021-11-17 08:19:30 -07:00
|
|
|
const autoplay = SettingsStore.getValue("autoplayVideo") as boolean;
|
2021-07-15 14:19:07 -06:00
|
|
|
const thumbnailUrl = await this.props.mediaEventHelper.thumbnailUrl.value;
|
2020-10-23 16:33:40 +01:00
|
|
|
if (autoplay) {
|
2021-09-21 17:48:09 +02:00
|
|
|
logger.log("Preloading video");
|
2016-11-08 16:26:25 +00:00
|
|
|
this.setState({
|
2021-07-15 14:19:07 -06:00
|
|
|
decryptedUrl: await this.props.mediaEventHelper.sourceUrl.value,
|
2016-11-04 15:39:39 +00:00
|
|
|
decryptedThumbnailUrl: thumbnailUrl,
|
2021-07-15 14:19:07 -06:00
|
|
|
decryptedBlob: await this.props.mediaEventHelper.sourceBlob.value,
|
2016-11-08 16:26:25 +00:00
|
|
|
});
|
2023-05-11 09:56:56 +01:00
|
|
|
this.props.onHeightChanged?.();
|
2020-10-23 16:33:40 +01:00
|
|
|
} else {
|
2021-09-21 17:48:09 +02:00
|
|
|
logger.log("NOT preloading video");
|
2021-07-14 20:51:20 -06:00
|
|
|
const content = this.props.mxEvent.getContent<IMediaEventContent>();
|
2022-03-22 12:23:25 +00:00
|
|
|
|
|
|
|
|
let mimetype = content?.info?.mimetype;
|
|
|
|
|
|
|
|
|
|
// clobber quicktime muxed files to be considered MP4 so browsers
|
|
|
|
|
// are willing to play them
|
|
|
|
|
if (mimetype == "video/quicktime") {
|
|
|
|
|
mimetype = "video/mp4";
|
|
|
|
|
}
|
|
|
|
|
|
2020-10-23 16:33:40 +01:00
|
|
|
this.setState({
|
2020-11-19 13:26:14 +00:00
|
|
|
// For Chrome and Electron, we need to set some non-empty `src` to
|
|
|
|
|
// enable the play button. Firefox does not seem to care either
|
|
|
|
|
// way, so it's fine to do for all browsers.
|
2022-03-22 12:23:25 +00:00
|
|
|
decryptedUrl: `data:${mimetype},`,
|
|
|
|
|
decryptedThumbnailUrl: thumbnailUrl || `data:${mimetype},`,
|
2020-10-23 16:33:40 +01:00
|
|
|
decryptedBlob: null,
|
|
|
|
|
});
|
|
|
|
|
}
|
|
|
|
|
} catch (err) {
|
2021-10-15 16:31:29 +02:00
|
|
|
logger.warn("Unable to decrypt attachment: ", err);
|
2016-11-04 13:05:34 +00:00
|
|
|
// Set a placeholder image when we can't decrypt the image.
|
2016-11-18 20:08:26 +00:00
|
|
|
this.setState({
|
|
|
|
|
error: err,
|
|
|
|
|
});
|
2020-10-23 16:33:40 +01:00
|
|
|
}
|
2016-11-04 13:05:34 +00:00
|
|
|
}
|
2020-08-29 12:14:16 +01:00
|
|
|
}
|
2016-11-04 13:05:34 +00:00
|
|
|
|
2023-01-12 13:25:14 +00:00
|
|
|
public componentWillUnmount(): void {
|
2023-05-10 08:41:55 +01:00
|
|
|
if (this.sizeWatcher) SettingsStore.unwatchSetting(this.sizeWatcher);
|
2021-11-17 08:19:30 -07:00
|
|
|
}
|
|
|
|
|
|
2023-01-12 13:25:14 +00:00
|
|
|
private videoOnPlay = async (): Promise<void> => {
|
2020-11-19 13:26:14 +00:00
|
|
|
if (this.hasContentUrl() || this.state.fetchingData || this.state.error) {
|
2020-10-30 00:34:18 +00:00
|
|
|
// We have the file, we are fetching the file, or there is an error.
|
2020-10-23 16:33:40 +01:00
|
|
|
return;
|
|
|
|
|
}
|
|
|
|
|
this.setState({
|
|
|
|
|
// To stop subsequent download attempts
|
|
|
|
|
fetchingData: true,
|
|
|
|
|
});
|
2021-07-15 14:19:07 -06:00
|
|
|
if (!this.props.mediaEventHelper.media.isEncrypted) {
|
2020-10-23 16:33:40 +01:00
|
|
|
this.setState({
|
|
|
|
|
error: "No file given in content",
|
|
|
|
|
});
|
|
|
|
|
return;
|
|
|
|
|
}
|
|
|
|
|
this.setState(
|
|
|
|
|
{
|
2021-07-15 14:19:07 -06:00
|
|
|
decryptedUrl: await this.props.mediaEventHelper.sourceUrl.value,
|
|
|
|
|
decryptedBlob: await this.props.mediaEventHelper.sourceBlob.value,
|
2020-10-30 00:34:18 +00:00
|
|
|
fetchingData: false,
|
2020-11-19 13:26:14 +00:00
|
|
|
},
|
|
|
|
|
() => {
|
|
|
|
|
if (!this.videoRef.current) return;
|
|
|
|
|
this.videoRef.current.play();
|
2020-10-23 16:33:40 +01:00
|
|
|
},
|
|
|
|
|
);
|
2023-05-11 09:56:56 +01:00
|
|
|
this.props.onHeightChanged?.();
|
2021-06-29 13:11:58 +01:00
|
|
|
};
|
2020-10-23 16:33:40 +01:00
|
|
|
|
2022-02-18 15:56:05 +00:00
|
|
|
protected get showFileBody(): boolean {
|
|
|
|
|
return (
|
|
|
|
|
this.context.timelineRenderingType !== TimelineRenderingType.Room &&
|
|
|
|
|
this.context.timelineRenderingType !== TimelineRenderingType.Pinned &&
|
|
|
|
|
this.context.timelineRenderingType !== TimelineRenderingType.Search
|
|
|
|
|
);
|
|
|
|
|
}
|
|
|
|
|
|
2023-03-13 15:07:20 +00:00
|
|
|
private getFileBody = (): ReactNode => {
|
2021-06-09 15:23:47 +05:30
|
|
|
if (this.props.forExport) return null;
|
2022-02-18 15:56:05 +00:00
|
|
|
return this.showFileBody && <MFileBody {...this.props} showGenericPlaceholder={false} />;
|
2021-06-30 14:08:22 +05:30
|
|
|
};
|
2021-05-31 21:01:19 +05:30
|
|
|
|
2023-02-13 17:01:43 +00:00
|
|
|
public render(): React.ReactNode {
|
2016-11-08 11:42:20 +00:00
|
|
|
const content = this.props.mxEvent.getContent();
|
2023-06-06 09:20:21 +01:00
|
|
|
const autoplay = !this.props.inhibitInteraction && SettingsStore.getValue("autoplayVideo");
|
2016-11-04 13:05:34 +00:00
|
|
|
|
2022-03-28 15:07:02 -04:00
|
|
|
let aspectRatio;
|
|
|
|
|
if (content.info?.w && content.info?.h) {
|
|
|
|
|
aspectRatio = `${content.info.w}/${content.info.h}`;
|
|
|
|
|
}
|
|
|
|
|
const { w: maxWidth, h: maxHeight } = suggestedVideoSize(SettingsStore.getValue("Images.size") as ImageSize, {
|
|
|
|
|
w: content.info?.w,
|
|
|
|
|
h: content.info?.h,
|
|
|
|
|
});
|
|
|
|
|
|
|
|
|
|
// HACK: This div fills out space while the video loads, to prevent scroll jumps
|
|
|
|
|
const spaceFiller = <div style={{ width: maxWidth, height: maxHeight }} />;
|
|
|
|
|
|
2016-11-18 20:08:26 +00:00
|
|
|
if (this.state.error !== null) {
|
|
|
|
|
return (
|
2022-07-06 13:29:26 +02:00
|
|
|
<MediaProcessingError className="mx_MVideoBody">{_t("Error decrypting video")}</MediaProcessingError>
|
2016-11-18 20:08:26 +00:00
|
|
|
);
|
|
|
|
|
}
|
|
|
|
|
|
2021-05-31 21:01:19 +05:30
|
|
|
// Important: If we aren't autoplaying and we haven't decrypted it yet, show a video with a poster.
|
2021-06-09 15:23:47 +05:30
|
|
|
if (!this.props.forExport && content.file !== undefined && this.state.decryptedUrl === null && autoplay) {
|
2016-11-04 13:05:34 +00:00
|
|
|
// Need to decrypt the attachment
|
|
|
|
|
// The attachment is decrypted in componentDidMount.
|
2022-03-28 15:07:02 -04:00
|
|
|
// For now show a spinner.
|
2016-11-04 13:05:34 +00:00
|
|
|
return (
|
2019-12-08 12:12:06 +00:00
|
|
|
<span className="mx_MVideoBody">
|
2022-03-28 15:07:02 -04:00
|
|
|
<div className="mx_MVideoBody_container" style={{ maxWidth, maxHeight, aspectRatio }}>
|
2020-06-26 01:18:02 +01:00
|
|
|
<InlineSpinner />
|
2016-11-18 20:08:26 +00:00
|
|
|
</div>
|
2022-03-28 15:07:02 -04:00
|
|
|
{spaceFiller}
|
2016-11-04 13:05:34 +00:00
|
|
|
</span>
|
|
|
|
|
);
|
|
|
|
|
}
|
|
|
|
|
|
2020-11-18 17:41:54 +00:00
|
|
|
const contentUrl = this.getContentUrl();
|
|
|
|
|
const thumbUrl = this.getThumbUrl();
|
2023-03-13 15:07:20 +00:00
|
|
|
let poster: string | undefined;
|
2017-02-27 22:16:56 +00:00
|
|
|
let preload = "metadata";
|
2022-03-22 18:16:03 -04:00
|
|
|
if (content.info && thumbUrl) {
|
|
|
|
|
poster = thumbUrl;
|
|
|
|
|
preload = "none";
|
2015-11-27 15:02:32 +00:00
|
|
|
}
|
2021-05-31 21:01:19 +05:30
|
|
|
|
|
|
|
|
const fileBody = this.getFileBody();
|
2015-11-27 15:02:32 +00:00
|
|
|
return (
|
2016-01-03 00:11:11 +00:00
|
|
|
<span className="mx_MVideoBody">
|
2022-03-28 15:07:02 -04:00
|
|
|
<div className="mx_MVideoBody_container" style={{ maxWidth, maxHeight, aspectRatio }}>
|
|
|
|
|
<video
|
|
|
|
|
className="mx_MVideoBody"
|
|
|
|
|
ref={this.videoRef}
|
|
|
|
|
src={contentUrl}
|
|
|
|
|
title={content.body}
|
2023-06-06 09:20:21 +01:00
|
|
|
controls={!this.props.inhibitInteraction}
|
2022-04-25 13:33:49 +01:00
|
|
|
// Disable downloading as it doesn't work with e2ee video,
|
|
|
|
|
// users should use the dedicated Download button in the Message Action Bar
|
|
|
|
|
controlsList="nodownload"
|
2022-03-28 15:07:02 -04:00
|
|
|
preload={preload}
|
|
|
|
|
muted={autoplay}
|
|
|
|
|
autoPlay={autoplay}
|
|
|
|
|
poster={poster}
|
|
|
|
|
onPlay={this.videoOnPlay}
|
|
|
|
|
/>
|
|
|
|
|
{spaceFiller}
|
|
|
|
|
</div>
|
2021-05-31 21:01:19 +05:30
|
|
|
{fileBody}
|
2015-11-27 15:02:32 +00:00
|
|
|
</span>
|
|
|
|
|
);
|
2020-08-29 12:14:16 +01:00
|
|
|
}
|
|
|
|
|
}
|