hydrogen-web/src/domain/session/room/CallViewModel.ts

245 lines
7.9 KiB
TypeScript
Raw Normal View History

2022-03-17 17:34:14 +05:30
/*
2022-03-17 17:37:55 +05:30
Copyright 2022 The Matrix.org Foundation C.I.C.
2022-03-17 17:34:14 +05:30
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 {AvatarSource} from "../../AvatarSource";
2022-03-17 17:37:55 +05:30
import {ViewModel, Options as BaseOptions} from "../../ViewModel";
import {getStreamVideoTrack, getStreamAudioTrack} from "../../../matrix/calls/common";
import {avatarInitials, getIdentifierColorNumber, getAvatarHttpUrl} from "../../avatar";
2022-04-26 21:26:46 +05:30
import {EventObservableValue} from "../../../observable/value/EventObservableValue";
import {ObservableValueMap} from "../../../observable/map/ObservableValueMap";
import type {Room} from "../../../matrix/room/Room";
2022-03-17 17:34:14 +05:30
import type {GroupCall} from "../../../matrix/calls/group/GroupCall";
2022-03-17 17:37:55 +05:30
import type {Member} from "../../../matrix/calls/group/Member";
import type {RoomMember} from "../../../matrix/room/members/RoomMember";
2022-03-17 17:37:55 +05:30
import type {BaseObservableList} from "../../../observable/list/BaseObservableList";
import type {BaseObservableValue} from "../../../observable/value/BaseObservableValue";
2022-04-13 22:04:01 +05:30
import type {Stream} from "../../../platform/types/MediaDevices";
import type {MediaRepository} from "../../../matrix/net/MediaRepository";
2022-03-17 17:34:14 +05:30
type Options = BaseOptions & {
call: GroupCall,
room: Room,
};
2022-03-17 17:34:14 +05:30
2022-03-17 17:37:55 +05:30
export class CallViewModel extends ViewModel<Options> {
2022-04-26 21:26:46 +05:30
public readonly memberViewModels: BaseObservableList<IStreamViewModel>;
2022-03-17 17:34:14 +05:30
2022-03-17 17:37:55 +05:30
constructor(options: Options) {
2022-03-17 17:34:14 +05:30
super(options);
const ownMemberViewModelMap = new ObservableValueMap("self", new EventObservableValue(this.call, "change"))
.mapValues((call, emitChange) => new OwnMemberViewModel(this.childOptions({call, emitChange})), () => {});
this.memberViewModels = this.call.members
2022-04-07 20:25:26 +05:30
.filterValues(member => member.isConnected)
.mapValues(member => new CallMemberViewModel(this.childOptions({member, mediaRepository: this.getOption("room").mediaRepository})))
.join(ownMemberViewModelMap)
2022-03-21 21:00:13 +05:30
.sortValues((a, b) => a.compare(b));
2022-06-09 19:03:59 +05:30
this.track(this.memberViewModels.subscribe({
onRemove: () => {
this.emitChange(); // update memberCount
},
onAdd: () => {
this.emitChange(); // update memberCount
},
onUpdate: () => {},
onReset: () => {},
onMove: () => {}
}))
2022-03-17 17:34:14 +05:30
}
2022-06-09 19:03:59 +05:30
get isCameraMuted(): boolean {
return this.call.muteSettings?.camera ?? true;
2022-06-09 19:03:59 +05:30
}
get isMicrophoneMuted(): boolean {
return this.call.muteSettings?.microphone ?? true;
2022-03-24 18:22:19 +05:30
}
2022-06-09 19:03:59 +05:30
get memberCount(): number {
return this.memberViewModels.length;
}
2022-03-17 17:34:14 +05:30
get name(): string {
2022-03-24 18:22:19 +05:30
return this.call.name;
2022-03-17 17:37:55 +05:30
}
2022-03-21 21:00:13 +05:30
get id(): string {
2022-03-24 18:22:19 +05:30
return this.call.id;
2022-03-21 21:00:13 +05:30
}
2022-06-09 19:03:59 +05:30
private get call(): GroupCall {
return this.getOption("call");
2022-03-24 18:22:19 +05:30
}
async hangup() {
2022-03-24 18:22:19 +05:30
if (this.call.hasJoined) {
await this.call.leave();
2022-03-24 18:22:19 +05:30
}
2022-03-17 17:37:55 +05:30
}
2022-04-15 02:49:44 +05:30
2022-06-09 19:03:59 +05:30
async toggleCamera() {
const {localMedia, muteSettings} = this.call;
if (muteSettings && localMedia) {
// unmute but no track?
if (muteSettings.camera && !getStreamVideoTrack(localMedia.userMedia)) {
const stream = await this.platform.mediaDevices.getMediaTracks(!muteSettings.microphone, true);
await this.call.setMedia(localMedia.withUserMedia(stream));
} else {
await this.call.setMuted(muteSettings.toggleCamera());
}
2022-06-09 19:03:59 +05:30
this.emitChange();
}
}
async toggleMicrophone() {
const {localMedia, muteSettings} = this.call;
if (muteSettings && localMedia) {
// unmute but no track?
if (muteSettings.microphone && !getStreamAudioTrack(localMedia.userMedia)) {
const stream = await this.platform.mediaDevices.getMediaTracks(true, !muteSettings.camera);
await this.call.setMedia(localMedia.withUserMedia(stream));
} else {
await this.call.setMuted(muteSettings.toggleMicrophone());
}
2022-06-09 19:03:59 +05:30
this.emitChange();
2022-05-04 22:14:11 +05:30
}
2022-04-15 02:49:44 +05:30
}
2022-03-17 17:37:55 +05:30
}
class OwnMemberViewModel extends ViewModel<Options> implements IStreamViewModel {
private memberObservable: undefined | BaseObservableValue<RoomMember>;
constructor(options: Options) {
super(options);
this.init();
}
async init() {
const room = this.getOption("room");
this.memberObservable = await room.observeMember(room.user.id);
this.track(this.memberObservable!.subscribe(() => {
this.emitChange(undefined);
}));
}
2022-03-17 17:37:55 +05:30
get stream(): Stream | undefined {
return this.call.localMedia?.userMedia;
}
private get call(): GroupCall {
return this.getOption("call");
}
get isCameraMuted(): boolean {
return this.call.muteSettings?.camera ?? true;
}
get isMicrophoneMuted(): boolean {
return this.call.muteSettings?.microphone ?? true;
}
get avatarLetter(): string {
const member = this.memberObservable?.get();
if (member) {
return avatarInitials(member.name);
} else {
return this.getOption("room").user.id;
}
}
get avatarColorNumber(): number {
return getIdentifierColorNumber(this.getOption("room").user.id);
}
avatarUrl(size: number): string | undefined {
const member = this.memberObservable?.get();
if (member) {
return getAvatarHttpUrl(member.avatarUrl, size, this.platform, this.getOption("room").mediaRepository);
}
}
get avatarTitle(): string {
const member = this.memberObservable?.get();
if (member) {
return member.name;
} else {
return this.getOption("room").user.id;
}
}
compare(other: OwnMemberViewModel | CallMemberViewModel): number {
return -1;
}
}
type MemberOptions = BaseOptions & {
member: Member,
mediaRepository: MediaRepository
};
export class CallMemberViewModel extends ViewModel<MemberOptions> implements IStreamViewModel {
2022-04-13 22:04:01 +05:30
get stream(): Stream | undefined {
return this.member.remoteMedia?.userMedia;
2022-03-24 18:22:19 +05:30
}
private get member(): Member {
return this.getOption("member");
2022-03-17 17:34:14 +05:30
}
2022-03-21 21:00:13 +05:30
get isCameraMuted(): boolean {
return this.member.remoteMuteSettings?.camera ?? true;
}
get isMicrophoneMuted(): boolean {
return this.member.remoteMuteSettings?.microphone ?? true;
}
get avatarLetter(): string {
return avatarInitials(this.member.member.name);
}
get avatarColorNumber(): number {
return getIdentifierColorNumber(this.member.userId);
}
avatarUrl(size: number): string | undefined {
const {avatarUrl} = this.member.member;
const mediaRepository = this.getOption("mediaRepository");
return getAvatarHttpUrl(avatarUrl, size, this.platform, mediaRepository);
}
get avatarTitle(): string {
return this.member.member.name;
}
compare(other: OwnMemberViewModel | CallMemberViewModel): number {
if (other instanceof OwnMemberViewModel) {
return -other.compare(this);
}
2022-03-24 18:22:19 +05:30
const myUserId = this.member.member.userId;
const otherUserId = other.member.member.userId;
2022-03-21 21:00:13 +05:30
if(myUserId === otherUserId) {
return 0;
}
return myUserId < otherUserId ? -1 : 1;
}
2022-03-17 17:34:14 +05:30
}
export interface IStreamViewModel extends AvatarSource, ViewModel {
get stream(): Stream | undefined;
get isCameraMuted(): boolean;
get isMicrophoneMuted(): boolean;
}