Code refactoring for video-rtc.js

This commit is contained in:
Alex X
2025-09-30 12:12:29 +03:00
parent dd7130d2d4
commit 7d9862202a
+14 -14
View File
@@ -185,7 +185,7 @@ export class VideoRTC extends HTMLElement {
/** @param {Function} isSupported */ /** @param {Function} isSupported */
codecs(isSupported) { codecs(isSupported) {
return this.CODECS return this.CODECS
.filter(codec => this.media.indexOf(codec.indexOf('vc1') > 0 ? 'video' : 'audio') >= 0) .filter(codec => this.media.includes(codec.includes('vc1') ? 'video' : 'audio'))
.filter(codec => isSupported(`video/mp4; codecs="${codec}"`)).join(); .filter(codec => isSupported(`video/mp4; codecs="${codec}"`)).join();
} }
@@ -350,23 +350,23 @@ export class VideoRTC extends HTMLElement {
const modes = []; const modes = [];
if (this.mode.indexOf('mse') >= 0 && ('MediaSource' in window || 'ManagedMediaSource' in window)) { if (this.mode.includes('mse') && ('MediaSource' in window || 'ManagedMediaSource' in window)) {
modes.push('mse'); modes.push('mse');
this.onmse(); this.onmse();
} else if (this.mode.indexOf('hls') >= 0 && this.video.canPlayType('application/vnd.apple.mpegurl')) { } else if (this.mode.includes('hls') && this.video.canPlayType('application/vnd.apple.mpegurl')) {
modes.push('hls'); modes.push('hls');
this.onhls(); this.onhls();
} else if (this.mode.indexOf('mp4') >= 0) { } else if (this.mode.includes('mp4')) {
modes.push('mp4'); modes.push('mp4');
this.onmp4(); this.onmp4();
} }
if (this.mode.indexOf('webrtc') >= 0 && 'RTCPeerConnection' in window) { if (this.mode.includes('webrtc') && 'RTCPeerConnection' in window) {
modes.push('webrtc'); modes.push('webrtc');
this.onwebrtc(); this.onwebrtc();
} }
if (this.mode.indexOf('mjpeg') >= 0) { if (this.mode.includes('mjpeg')) {
if (modes.length) { if (modes.length) {
this.onmessage['mjpeg'] = msg => { this.onmessage['mjpeg'] = msg => {
if (msg.type !== 'error' || msg.value.indexOf(modes[0]) !== 0) return; if (msg.type !== 'error' || msg.value.indexOf(modes[0]) !== 0) return;
@@ -490,7 +490,7 @@ export class VideoRTC extends HTMLElement {
const pc = new RTCPeerConnection(this.pcConfig); const pc = new RTCPeerConnection(this.pcConfig);
pc.addEventListener('icecandidate', ev => { pc.addEventListener('icecandidate', ev => {
if (ev.candidate && this.mode.indexOf('webrtc/tcp') >= 0 && ev.candidate.protocol === 'udp') return; if (ev.candidate && this.mode.includes('webrtc/tcp') && ev.candidate.protocol === 'udp') return;
const candidate = ev.candidate ? ev.candidate.toJSON().candidate : ''; const candidate = ev.candidate ? ev.candidate.toJSON().candidate : '';
this.send({type: 'webrtc/candidate', value: candidate}); this.send({type: 'webrtc/candidate', value: candidate});
@@ -518,7 +518,7 @@ export class VideoRTC extends HTMLElement {
this.onmessage['webrtc'] = msg => { this.onmessage['webrtc'] = msg => {
switch (msg.type) { switch (msg.type) {
case 'webrtc/candidate': case 'webrtc/candidate':
if (this.mode.indexOf('webrtc/tcp') >= 0 && msg.value.indexOf(' udp ') > 0) return; if (this.mode.includes('webrtc/tcp') && msg.value.includes(' udp ')) return;
pc.addIceCandidate({candidate: msg.value, sdpMid: '0'}).catch(er => { pc.addIceCandidate({candidate: msg.value, sdpMid: '0'}).catch(er => {
console.warn(er); console.warn(er);
@@ -530,7 +530,7 @@ export class VideoRTC extends HTMLElement {
}); });
break; break;
case 'error': case 'error':
if (msg.value.indexOf('webrtc/offer') < 0) return; if (!msg.value.includes('webrtc/offer')) return;
pc.close(); pc.close();
} }
}; };
@@ -549,7 +549,7 @@ export class VideoRTC extends HTMLElement {
*/ */
async createOffer(pc) { async createOffer(pc) {
try { try {
if (this.media.indexOf('microphone') >= 0) { if (this.media.includes('microphone')) {
const media = await navigator.mediaDevices.getUserMedia({audio: true}); const media = await navigator.mediaDevices.getUserMedia({audio: true});
media.getTracks().forEach(track => { media.getTracks().forEach(track => {
pc.addTransceiver(track, {direction: 'sendonly'}); pc.addTransceiver(track, {direction: 'sendonly'});
@@ -560,7 +560,7 @@ export class VideoRTC extends HTMLElement {
} }
for (const kind of ['video', 'audio']) { for (const kind of ['video', 'audio']) {
if (this.media.indexOf(kind) >= 0) { if (this.media.includes(kind)) {
pc.addTransceiver(kind, {direction: 'recvonly'}); pc.addTransceiver(kind, {direction: 'recvonly'});
} }
} }
@@ -587,9 +587,9 @@ export class VideoRTC extends HTMLElement {
} }
if (stream.getAudioTracks().length > 0) rtcPriority += 0x102; if (stream.getAudioTracks().length > 0) rtcPriority += 0x102;
if (this.mseCodecs.indexOf('hvc1.') >= 0) msePriority += 0x230; if (this.mseCodecs.includes('hvc1.')) msePriority += 0x230;
if (this.mseCodecs.indexOf('avc1.') >= 0) msePriority += 0x210; if (this.mseCodecs.includes('avc1.')) msePriority += 0x210;
if (this.mseCodecs.indexOf('mp4a.') >= 0) msePriority += 0x101; if (this.mseCodecs.includes('mp4a.')) msePriority += 0x101;
if (rtcPriority >= msePriority) { if (rtcPriority >= msePriority) {
this.video.srcObject = stream; this.video.srcObject = stream;