Files
WebRTC-Broadcast/public/js/services/MediaHandler.ts
2025-09-05 00:36:54 -04:00

55 lines
1.3 KiB
TypeScript

import type { IMediaHandler } from '../interfaces/IWebRTCClient.ts';
export class MediaHandler implements IMediaHandler {
private localStream: MediaStream | null = null;
private videoElement: HTMLVideoElement | null = null;
constructor(videoElementId?: string) {
if (videoElementId) {
this.videoElement = document.getElementById(videoElementId) as HTMLVideoElement;
}
}
async getLocalStream(): Promise<MediaStream> {
try {
this.localStream = await navigator.mediaDevices.getUserMedia({
video: {
width: { ideal: 1280 },
height: { ideal: 720 },
frameRate: { ideal: 30 }
},
audio: true
});
if (this.videoElement) {
this.videoElement.srcObject = this.localStream;
}
return this.localStream;
} catch (error) {
console.error('Error accessing media devices:', error);
throw error;
}
}
stopLocalStream(): void {
if (this.localStream) {
this.localStream.getTracks().forEach(track => {
track.stop();
});
this.localStream = null;
}
if (this.videoElement) {
this.videoElement.srcObject = null;
}
}
getLocalVideo(): HTMLVideoElement | null {
return this.videoElement;
}
getCurrentStream(): MediaStream | null {
return this.localStream;
}
}