2020-10-29 20:56:24 +03:00
|
|
|
/*
|
2020-11-05 12:51:02 +03:00
|
|
|
Copyright 2015, 2016, 2019 The Matrix.org Foundation C.I.C.
|
2020-10-29 20:56:24 +03: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.
|
|
|
|
*/
|
|
|
|
|
|
|
|
import classnames from 'classnames';
|
|
|
|
import { MatrixCall } from 'matrix-js-sdk/src/webrtc/call';
|
2021-06-29 15:11:58 +03:00
|
|
|
import React, { createRef } from 'react';
|
2020-10-29 20:56:24 +03:00
|
|
|
import SettingsStore from "../../../settings/SettingsStore";
|
2021-03-07 10:13:35 +03:00
|
|
|
import { CallFeed, CallFeedEvent } from 'matrix-js-sdk/src/webrtc/callFeed';
|
|
|
|
import { logger } from 'matrix-js-sdk/src/logger';
|
2021-06-29 15:11:58 +03:00
|
|
|
import MemberAvatar from "../avatars/MemberAvatar";
|
|
|
|
import { replaceableComponent } from "../../../utils/replaceableComponent";
|
2020-10-29 20:56:24 +03:00
|
|
|
|
|
|
|
interface IProps {
|
2021-07-02 01:23:03 +03:00
|
|
|
call: MatrixCall;
|
2020-10-29 20:56:24 +03:00
|
|
|
|
2021-07-02 01:23:03 +03:00
|
|
|
feed: CallFeed;
|
2021-03-07 10:13:35 +03:00
|
|
|
|
2021-04-27 12:59:26 +03:00
|
|
|
// Whether this call view is for picture-in-picture mode
|
2021-03-07 10:13:35 +03:00
|
|
|
// otherwise, it's the larger call view when viewing the room the call is in.
|
|
|
|
// This is sort of a proxy for a number of things but we currently have no
|
|
|
|
// need to control those things separately, so this is simpler.
|
|
|
|
pipMode?: boolean;
|
2020-10-29 20:56:24 +03:00
|
|
|
|
|
|
|
// a callback which is called when the video element is resized
|
|
|
|
// due to a change in video metadata
|
2021-07-07 12:00:42 +03:00
|
|
|
onResize?: (e: Event) => void;
|
2021-05-07 22:34:56 +03:00
|
|
|
|
2021-07-07 12:00:42 +03:00
|
|
|
primary: boolean;
|
2020-10-29 20:56:24 +03:00
|
|
|
}
|
|
|
|
|
2021-03-07 10:13:35 +03:00
|
|
|
interface IState {
|
2021-04-04 09:50:25 +03:00
|
|
|
audioMuted: boolean;
|
|
|
|
videoMuted: boolean;
|
2021-03-07 10:13:35 +03:00
|
|
|
}
|
|
|
|
|
2021-03-09 06:20:07 +03:00
|
|
|
@replaceableComponent("views.voip.VideoFeed")
|
2021-03-07 10:13:35 +03:00
|
|
|
export default class VideoFeed extends React.Component<IProps, IState> {
|
2021-04-27 13:02:41 +03:00
|
|
|
private element = createRef<HTMLVideoElement>();
|
2020-10-29 20:56:24 +03:00
|
|
|
|
2021-03-07 10:13:35 +03:00
|
|
|
constructor(props: IProps) {
|
|
|
|
super(props);
|
|
|
|
|
|
|
|
this.state = {
|
2021-04-04 09:50:25 +03:00
|
|
|
audioMuted: this.props.feed.isAudioMuted(),
|
|
|
|
videoMuted: this.props.feed.isVideoMuted(),
|
2021-03-07 10:13:35 +03:00
|
|
|
};
|
2020-12-04 22:41:48 +03:00
|
|
|
}
|
|
|
|
|
2021-03-07 10:13:35 +03:00
|
|
|
componentDidMount() {
|
2021-06-12 10:19:45 +03:00
|
|
|
this.updateFeed(null, this.props.feed);
|
2021-04-27 13:02:41 +03:00
|
|
|
this.playMedia();
|
2021-04-12 17:19:05 +03:00
|
|
|
}
|
|
|
|
|
2021-04-23 20:41:55 +03:00
|
|
|
componentWillUnmount() {
|
2021-06-12 10:19:45 +03:00
|
|
|
this.updateFeed(this.props.feed, null);
|
2021-04-27 13:02:41 +03:00
|
|
|
this.element.current?.removeEventListener('resize', this.onResize);
|
2021-06-12 10:19:45 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
componentDidUpdate(prevProps: IProps) {
|
|
|
|
this.updateFeed(prevProps.feed, this.props.feed);
|
|
|
|
}
|
|
|
|
|
2021-06-12 13:16:52 +03:00
|
|
|
static getDerivedStateFromProps(props: IProps) {
|
|
|
|
return {
|
|
|
|
audioMuted: props.feed.isAudioMuted(),
|
|
|
|
videoMuted: props.feed.isVideoMuted(),
|
|
|
|
};
|
|
|
|
}
|
|
|
|
|
2021-06-12 10:19:45 +03:00
|
|
|
private updateFeed(oldFeed: CallFeed, newFeed: CallFeed) {
|
|
|
|
if (oldFeed === newFeed) return;
|
|
|
|
|
|
|
|
if (oldFeed) {
|
|
|
|
this.props.feed.removeListener(CallFeedEvent.NewStream, this.onNewStream);
|
|
|
|
this.stopMedia();
|
|
|
|
}
|
|
|
|
if (newFeed) {
|
|
|
|
this.props.feed.addListener(CallFeedEvent.NewStream, this.onNewStream);
|
|
|
|
this.playMedia();
|
|
|
|
}
|
2021-04-23 20:41:55 +03:00
|
|
|
}
|
2021-03-10 10:31:01 +03:00
|
|
|
|
2021-04-27 13:02:41 +03:00
|
|
|
private playMedia() {
|
|
|
|
const element = this.element.current;
|
|
|
|
if (!element) return;
|
|
|
|
// We play audio in AudioFeed, not here
|
|
|
|
element.muted = true;
|
2021-04-23 20:41:55 +03:00
|
|
|
element.srcObject = this.props.feed.stream;
|
|
|
|
element.autoplay = true;
|
2021-03-10 10:31:01 +03:00
|
|
|
try {
|
|
|
|
// A note on calling methods on media elements:
|
|
|
|
// We used to have queues per media element to serialise all calls on those elements.
|
|
|
|
// The reason given for this was that load() and play() were racing. However, we now
|
|
|
|
// never call load() explicitly so this seems unnecessary. However, serialising every
|
|
|
|
// operation was causing bugs where video would not resume because some play command
|
|
|
|
// had got stuck and all media operations were queued up behind it. If necessary, we
|
|
|
|
// should serialise the ones that need to be serialised but then be able to interrupt
|
|
|
|
// them with another load() which will cancel the pending one, but since we don't call
|
|
|
|
// load() explicitly, it shouldn't be a problem. - Dave
|
2021-05-20 16:54:47 +03:00
|
|
|
element.play();
|
2021-03-10 10:31:01 +03:00
|
|
|
} catch (e) {
|
|
|
|
logger.info("Failed to play media element with feed", this.props.feed, e);
|
2020-10-29 20:56:24 +03:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-04-27 13:02:41 +03:00
|
|
|
private stopMedia() {
|
|
|
|
const element = this.element.current;
|
|
|
|
if (!element) return;
|
|
|
|
|
2021-04-23 20:41:55 +03:00
|
|
|
element.pause();
|
|
|
|
element.src = null;
|
2021-03-10 10:31:01 +03:00
|
|
|
|
|
|
|
// As per comment in componentDidMount, setting the sink ID back to the
|
|
|
|
// default once the call is over makes setSinkId work reliably. - Dave
|
|
|
|
// Since we are not using the same element anymore, the above doesn't
|
|
|
|
// seem to be necessary - Šimon
|
|
|
|
}
|
|
|
|
|
2021-04-13 21:21:03 +03:00
|
|
|
private onNewStream = () => {
|
2021-04-04 09:50:25 +03:00
|
|
|
this.setState({
|
|
|
|
audioMuted: this.props.feed.isAudioMuted(),
|
|
|
|
videoMuted: this.props.feed.isVideoMuted(),
|
|
|
|
});
|
2021-04-27 13:02:41 +03:00
|
|
|
this.playMedia();
|
2021-04-19 08:42:32 +03:00
|
|
|
};
|
2020-12-04 22:41:48 +03:00
|
|
|
|
2021-04-13 21:21:03 +03:00
|
|
|
private onResize = (e) => {
|
2021-03-07 10:13:35 +03:00
|
|
|
if (this.props.onResize && !this.props.feed.isLocal()) {
|
2020-10-29 20:56:24 +03:00
|
|
|
this.props.onResize(e);
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
|
|
|
render() {
|
|
|
|
const videoClasses = {
|
|
|
|
mx_VideoFeed: true,
|
2021-06-12 09:15:26 +03:00
|
|
|
mx_VideoFeed_primary: this.props.primary,
|
|
|
|
mx_VideoFeed_secondary: !this.props.primary,
|
2021-04-04 09:50:25 +03:00
|
|
|
mx_VideoFeed_voice: this.state.videoMuted,
|
2021-04-27 13:02:41 +03:00
|
|
|
mx_VideoFeed_video: !this.state.videoMuted,
|
2020-10-29 20:56:24 +03:00
|
|
|
mx_VideoFeed_mirror: (
|
2021-03-07 10:13:35 +03:00
|
|
|
this.props.feed.isLocal() &&
|
2020-10-29 20:56:24 +03:00
|
|
|
SettingsStore.getValue('VideoView.flipVideoHorizontally')
|
|
|
|
),
|
|
|
|
};
|
|
|
|
|
2021-04-04 09:50:25 +03:00
|
|
|
if (this.state.videoMuted) {
|
2021-04-04 09:33:53 +03:00
|
|
|
const member = this.props.feed.getMember();
|
2021-06-12 11:48:32 +03:00
|
|
|
let avatarSize;
|
|
|
|
if (this.props.pipMode) avatarSize = 76;
|
|
|
|
else if (!this.props.primary) avatarSize = 34;
|
|
|
|
else avatarSize = 160;
|
2021-03-07 10:13:35 +03:00
|
|
|
|
|
|
|
return (
|
|
|
|
<div className={classnames(videoClasses)} >
|
|
|
|
<MemberAvatar
|
|
|
|
member={member}
|
|
|
|
height={avatarSize}
|
|
|
|
width={avatarSize}
|
|
|
|
/>
|
|
|
|
</div>
|
|
|
|
);
|
|
|
|
} else {
|
|
|
|
return (
|
2021-04-27 13:02:41 +03:00
|
|
|
<video className={classnames(videoClasses)} ref={this.element} />
|
2021-03-07 10:13:35 +03:00
|
|
|
);
|
|
|
|
}
|
2020-10-29 20:56:24 +03:00
|
|
|
}
|
|
|
|
}
|