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.
|
|
|
|
*/
|
|
|
|
|
2022-04-26 17:57:28 +05:30
|
|
|
import {AvatarSource} from "../../AvatarSource";
|
2022-03-17 17:37:55 +05:30
|
|
|
import {ViewModel, Options as BaseOptions} from "../../ViewModel";
|
2022-04-26 17:57:28 +05:30
|
|
|
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";
|
2022-06-02 21:01:17 +05:30
|
|
|
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";
|
2022-06-02 21:01:17 +05:30
|
|
|
import type {RoomMember} from "../../../matrix/room/members/RoomMember";
|
2022-03-17 17:37:55 +05:30
|
|
|
import type {BaseObservableList} from "../../../observable/list/BaseObservableList";
|
2022-06-02 21:01:17 +05:30
|
|
|
import type {BaseObservableValue} from "../../../observable/value/BaseObservableValue";
|
2022-04-13 22:04:01 +05:30
|
|
|
import type {Stream} from "../../../platform/types/MediaDevices";
|
2022-04-26 17:57:28 +05:30
|
|
|
import type {MediaRepository} from "../../../matrix/net/MediaRepository";
|
2022-03-17 17:34:14 +05:30
|
|
|
|
2022-04-26 17:57:28 +05:30
|
|
|
type Options = BaseOptions & {
|
|
|
|
call: GroupCall,
|
2022-06-02 21:01:17 +05:30
|
|
|
room: Room,
|
2022-04-26 17:57:28 +05:30
|
|
|
};
|
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);
|
2022-04-26 17:57:28 +05:30
|
|
|
const ownMemberViewModelMap = new ObservableValueMap("self", new EventObservableValue(this.call, "change"))
|
2022-06-02 21:01:17 +05:30
|
|
|
.mapValues((call, emitChange) => new OwnMemberViewModel(this.childOptions({call, emitChange})), () => {});
|
2022-04-26 17:57:28 +05:30
|
|
|
this.memberViewModels = this.call.members
|
2022-04-07 20:25:26 +05:30
|
|
|
.filterValues(member => member.isConnected)
|
2022-06-02 21:01:17 +05:30
|
|
|
.mapValues(member => new CallMemberViewModel(this.childOptions({member, mediaRepository: this.getOption("room").mediaRepository})))
|
2022-04-26 17:57:28 +05:30
|
|
|
.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 {
|
2022-06-10 20:40:23 +05:30
|
|
|
return this.call.muteSettings?.camera ?? true;
|
2022-06-09 19:03:59 +05:30
|
|
|
}
|
|
|
|
|
|
|
|
get isMicrophoneMuted(): boolean {
|
2022-06-10 20:40:23 +05:30
|
|
|
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
|
|
|
}
|
|
|
|
|
2022-06-10 20:40:23 +05:30
|
|
|
async hangup() {
|
2022-03-24 18:22:19 +05:30
|
|
|
if (this.call.hasJoined) {
|
2022-06-10 20:40:23 +05:30
|
|
|
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() {
|
2022-06-10 20:40:23 +05:30
|
|
|
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() {
|
2022-06-10 20:40:23 +05:30
|
|
|
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
|
|
|
}
|
|
|
|
|
2022-06-02 21:01:17 +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
|
|
|
|
2022-04-26 17:57:28 +05:30
|
|
|
get stream(): Stream | undefined {
|
|
|
|
return this.call.localMedia?.userMedia;
|
|
|
|
}
|
|
|
|
|
|
|
|
private get call(): GroupCall {
|
|
|
|
return this.getOption("call");
|
|
|
|
}
|
|
|
|
|
|
|
|
get isCameraMuted(): boolean {
|
2022-06-10 20:40:23 +05:30
|
|
|
return this.call.muteSettings?.camera ?? true;
|
2022-04-26 17:57:28 +05:30
|
|
|
}
|
|
|
|
|
|
|
|
get isMicrophoneMuted(): boolean {
|
2022-06-10 20:40:23 +05:30
|
|
|
return this.call.muteSettings?.microphone ?? true;
|
2022-04-26 17:57:28 +05:30
|
|
|
}
|
|
|
|
|
|
|
|
get avatarLetter(): string {
|
2022-06-02 21:01:17 +05:30
|
|
|
const member = this.memberObservable?.get();
|
|
|
|
if (member) {
|
|
|
|
return avatarInitials(member.name);
|
|
|
|
} else {
|
2022-06-03 19:19:45 +05:30
|
|
|
return this.getOption("room").user.id;
|
2022-06-02 21:01:17 +05:30
|
|
|
}
|
2022-04-26 17:57:28 +05:30
|
|
|
}
|
|
|
|
|
|
|
|
get avatarColorNumber(): number {
|
2022-06-03 19:19:45 +05:30
|
|
|
return getIdentifierColorNumber(this.getOption("room").user.id);
|
2022-04-26 17:57:28 +05:30
|
|
|
}
|
|
|
|
|
|
|
|
avatarUrl(size: number): string | undefined {
|
2022-06-02 21:01:17 +05:30
|
|
|
const member = this.memberObservable?.get();
|
|
|
|
if (member) {
|
|
|
|
return getAvatarHttpUrl(member.avatarUrl, size, this.platform, this.getOption("room").mediaRepository);
|
|
|
|
}
|
2022-04-26 17:57:28 +05:30
|
|
|
}
|
|
|
|
|
|
|
|
get avatarTitle(): string {
|
2022-06-02 21:01:17 +05:30
|
|
|
const member = this.memberObservable?.get();
|
|
|
|
if (member) {
|
|
|
|
return member.name;
|
|
|
|
} else {
|
2022-06-03 19:19:45 +05:30
|
|
|
return this.getOption("room").user.id;
|
2022-06-02 21:01:17 +05:30
|
|
|
}
|
2022-04-26 17:57:28 +05:30
|
|
|
}
|
|
|
|
|
|
|
|
compare(other: OwnMemberViewModel | CallMemberViewModel): number {
|
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2022-06-02 19:26:23 +05:30
|
|
|
type MemberOptions = BaseOptions & {
|
|
|
|
member: Member,
|
|
|
|
mediaRepository: MediaRepository
|
|
|
|
};
|
2022-04-26 17:57:28 +05:30
|
|
|
|
|
|
|
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
|
|
|
|
2022-04-26 17:57:28 +05:30
|
|
|
get isCameraMuted(): boolean {
|
2022-06-10 20:40:23 +05:30
|
|
|
return this.member.remoteMuteSettings?.camera ?? true;
|
2022-04-26 17:57:28 +05:30
|
|
|
}
|
|
|
|
|
|
|
|
get isMicrophoneMuted(): boolean {
|
2022-06-10 20:40:23 +05:30
|
|
|
return this.member.remoteMuteSettings?.microphone ?? true;
|
2022-04-26 17:57:28 +05:30
|
|
|
}
|
|
|
|
|
|
|
|
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
|
|
|
}
|
2022-04-26 17:57:28 +05:30
|
|
|
|
|
|
|
export interface IStreamViewModel extends AvatarSource, ViewModel {
|
|
|
|
get stream(): Stream | undefined;
|
|
|
|
get isCameraMuted(): boolean;
|
|
|
|
get isMicrophoneMuted(): boolean;
|
|
|
|
}
|