All files / app/component/chat/chat-video chat-video.component.ts

96.72% Statements 59/61
84.84% Branches 28/33
92.85% Functions 26/28
97.72% Lines 43/44

Press n or j to go to the next uncovered block, b, p or k for the previous block.

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 1208x           8x             8x                           36x     36x     36x 36x 36x 36x 36x       8x 4x   2x 1x 1x                     2x       48x   32x         8x       10x 9x   2x       9x 8x 3x       8x       9x 9x 9x   9x   7x 2x 2x   5x 5x     2x 2x 2x         6x 6x 6x   6x 6x        
import { AsyncPipe } from '@angular/common';
import { AfterViewInit, ChangeDetectionStrategy, Component, Input } from '@angular/core';
import { RouterLink } from '@angular/router';
import { runInAction } from 'mobx';
import { MobxAngularModule } from 'mobx-angular';
import { map, Observable } from 'rxjs';
import { TitleDirective } from '../../../directive/title.directive';
import { Ext } from '../../../model/ext';
import { AdminService } from '../../../service/admin.service';
import { ExtService } from '../../../service/api/ext.service';
import { TaggingService } from '../../../service/api/tagging.service';
import { VideoService } from '../../../service/video.service';
import { Store } from '../../../store/store';
import { hasTag } from '../../../util/tag';
 
@Component({
  selector: 'app-chat-video',
  templateUrl: './chat-video.component.html',
  styleUrl: './chat-video.component.scss',
  changeDetection: ChangeDetectionStrategy.OnPush,
  imports: [
    MobxAngularModule,
    RouterLink,
    TitleDirective,
    AsyncPipe,
  ],
})
export class ChatVideoComponentI implements AfterViewInit {
 
  @Input()
  url = 'tag:/chat';
 
  constructor(
    public store: Store,
    private admin: AdminService,
    private exts: ExtService,
    private ts: TaggingService,
    private vs: VideoService,
  ) { }
 
  ngAfterViewInit() {
    if (this.store.local.inCall() && !this.store.video.enabled) {
      this.ts.getResponse(this.url)
        .subscribe(ref => {
          if (hasTag('plugin/user/lobby', ref)) {
            this.ts.deleteResponse('plugin/user/lobby', this.url).subscribe();
            Eif (confirm($localize`Rejoin the call?`)) this.call();
          }
        });
    }
  }
 
  authorExt$(user: string): Observable<Ext> {
    return this.exts.getCachedExt(user).pipe(map(x => ({ ...x, name: x.name || x.tag.substring('+user/'.length) })));
  }
 
  set speaker(user: string) {
    runInAction(() => this.store.video.activeSpeaker = (user === this.store.account.tag) ? '' : user);
  }
 
  get userStreams() {
    return [...this.store.video.streams.entries()].map(e =>({
      tag: e[0],
      streams: e[1].filter(s => s.stream.getTracks().some(t => t.readyState === 'live')),
    }));
  }
 
  get isTwoPersonCall() {
    return this.userStreams.length === 1;
  }
 
  get featuredStream() {
    if (this.userStreams.length === 1 || !this.store.video.activeSpeaker) {
      return this.userStreams[0];
    }
    return this.userStreams.find(u => u.tag === this.store.video.activeSpeaker) || this.userStreams[0];
  }
 
  get gridStreams() {
    if (this.userStreams.length === 1) return [];
    if (!this.store.video.activeSpeaker) return this.userStreams.slice(1);
    return this.userStreams.filter(u => u.tag !== this.store.video.activeSpeaker);
  }
 
  get hungup() {
    return [...this.store.video.hungup.entries()].filter(e => e[1]).map(e => e[0]);
  }
 
  call() {
    runInAction(() => {
      this.store.video.enabled = true;
      this.store.local.setInCall(true);
    });
    navigator.mediaDevices.getUserMedia(this.admin.getPlugin('plugin/user/video')!.config!.gumConfig)
      .then(stream => {
        if (!this.store.video.enabled) {
          stream.getTracks().forEach(t => t.stop());
          return;
        }
        this.ts.respond(['public', 'plugin/user/lobby'], this.url)
          .subscribe(() => this.vs.call(this.url, stream));
      })
      .catch(err => {
        console.log('Raised error when capturing:', err);
        this.hangup();
        alert($localize`Unable to access camera or microphone. Please check your browser permissions and try again.`);
      });
  }
 
  hangup() {
    runInAction(() => {
      this.store.video.enabled = false;
      this.store.local.setInCall(false);
    });
    this.ts.deleteResponse('plugin/user/lobby', this.url).subscribe();
    this.vs.hangup();
  }
 
}