|
1 | | -import { Component, ElementRef, OnInit, ViewChild } from '@angular/core'; |
| 1 | +import { Component, ElementRef, inject, OnInit, ViewChild } from '@angular/core'; |
| 2 | +import { MessageService } from 'primeng/api'; |
| 3 | +import { SelectChangeEvent } from 'primeng/select'; |
| 4 | +import { urls } from 'src/api/urls'; |
| 5 | +import { getAllVideos, updateVideos } from 'src/api/video.api'; |
| 6 | +import APIService from 'src/services/api.service'; |
2 | 7 | import MediaMTXWebRTCReader from 'src/utils/MediaMTXReader'; |
3 | 8 | @Component({ |
4 | 9 | selector: 'camera-page', |
5 | 10 | templateUrl: './camera-page.component.html', |
6 | 11 | styleUrl: './camera-page.component.css' |
7 | 12 | }) |
8 | 13 | export class CameraPageComponent implements OnInit { |
| 14 | + private serverService = inject(APIService); |
| 15 | + private messageService = inject(MessageService); |
| 16 | + |
9 | 17 | url = 'http://192.168.100.11:8889/frontcam/'; |
10 | 18 | urlAvailable = false; |
| 19 | + liveStream = true; |
| 20 | + |
| 21 | + selectedVideoName: string = 'Live Stream'; |
| 22 | + playbackVideoUrl?: string; |
| 23 | + videoUrls: string[] = ['Live Stream']; |
| 24 | + videoUrlsIsLoading: boolean = true; |
11 | 25 |
|
12 | 26 | @ViewChild('remoteVideo') remoteVideo?: ElementRef<HTMLVideoElement>; |
| 27 | + @ViewChild('playbackVideo', { static: false }) playbackVideoRef!: ElementRef<HTMLVideoElement>; |
13 | 28 |
|
14 | 29 | async ngOnInit(): Promise<void> { |
15 | 30 | this.urlAvailable = await this.checkConnection(); |
16 | | - console.log(window.location.href); |
17 | 31 | new MediaMTXWebRTCReader({ |
18 | 32 | url: new URL('whep', this.url), |
19 | 33 | onError: console.log, |
20 | 34 | onTrack: (e) => { |
21 | | - console.log(e); |
22 | 35 | if (this.remoteVideo) { |
23 | 36 | [this.remoteVideo.nativeElement.srcObject] = e.streams; |
24 | 37 | } |
25 | 38 | } |
26 | 39 | }); |
| 40 | + |
| 41 | + const videosQueryResponse = this.serverService.query<string[]>(() => getAllVideos(), { queryKey: ['videos'] }); |
| 42 | + videosQueryResponse.error.subscribe((error) => { |
| 43 | + error && this.messageService.add({ severity: 'error', summary: 'Error', detail: error.message }); |
| 44 | + }); |
| 45 | + videosQueryResponse.isLoading.subscribe((isLoading) => { |
| 46 | + this.videoUrlsIsLoading = isLoading; |
| 47 | + }); |
| 48 | + videosQueryResponse.data.subscribe((data) => { |
| 49 | + if (data) this.videoUrls = data.concat('Live Stream'); |
| 50 | + }); |
27 | 51 | } |
28 | 52 |
|
29 | 53 | checkConnection = (): Promise<boolean> => |
30 | 54 | fetch(this.url, { method: 'HEAD' }) |
31 | 55 | .then(() => true) |
32 | 56 | .catch(() => false); |
| 57 | + |
| 58 | + onVideoSelected = (videoName: string) => { |
| 59 | + this.playbackVideoUrl = urls.getVideo(videoName); |
| 60 | + this.selectedVideoName = videoName; |
| 61 | + this.liveStream = false; |
| 62 | + // Wait for Angular to update the DOM |
| 63 | + setTimeout(() => { |
| 64 | + const videoEl = this.playbackVideoRef.nativeElement; |
| 65 | + videoEl.load(); // This reloads the new <source> inside the <video> |
| 66 | + videoEl.play(); |
| 67 | + }); |
| 68 | + }; |
| 69 | + |
| 70 | + onLiveStreamSelected = () => { |
| 71 | + this.selectedVideoName = 'Live Stream'; |
| 72 | + this.playbackVideoUrl = undefined; |
| 73 | + this.liveStream = true; |
| 74 | + }; |
| 75 | + |
| 76 | + onDropdownChange = (event: SelectChangeEvent) => { |
| 77 | + if (event.value === 'Live Stream') { |
| 78 | + this.onLiveStreamSelected(); |
| 79 | + } else { |
| 80 | + this.onVideoSelected(event.value); |
| 81 | + } |
| 82 | + }; |
| 83 | + |
| 84 | + onUpdateVideosPressed = () => { |
| 85 | + const updateVideoQueryResponse = this.serverService.query(() => updateVideos(), { invalidates: ['videos'] }); |
| 86 | + this.messageService.add({ |
| 87 | + severity: 'success', |
| 88 | + summary: 'Update Videos', |
| 89 | + detail: 'A request was made for scylla to update its videos, this may take a minute, please refresh in a moment' |
| 90 | + }); |
| 91 | + updateVideoQueryResponse.error.subscribe((error) => { |
| 92 | + error && this.messageService.add({ severity: 'error', summary: 'Error', detail: error.message }); |
| 93 | + }); |
| 94 | + }; |
33 | 95 | } |
0 commit comments