Video stream not displaying in browser using WebRTC

I’m currently working on a project where I’m trying to display a video stream received via WebRTC in a browser using VueJS (Quasar Framework) from one component to another. However, despite setting the srcObject of the remote video element with the received MediaStream, the video is not displaying. Here’s my code:

senderClient.vue:

<template>
    <div>
        <video ref="localVideo" autoplay></video>
        <q-btn @click="startStreaming" label="Iniciar Transmissão"/>
    </div>
</template>
  
<script setup>
    import { ref, onMounted } from 'vue';
    import io from 'socket.io-client';

    let localStream;
    let peerConnection;
    const localVideo = ref(null);

    const serverUrl = 'http://localhost';
    const serverPort = 3000;
    const socket = io(`${serverUrl}:${serverPort}`, { transports: ["websocket"] });

    onMounted(async () => {
    try {
        localStream = await navigator.mediaDevices.getUserMedia({ video: true, audio: false });
        localVideo.value.srcObject = localStream;
    } catch (error) {
        console.error('Erro ao acessar a câmera:', error);
    }
    });

    function startStreaming() {
    peerConnection = new RTCPeerConnection();

    localStream.getTracks().forEach(track => {
        peerConnection.addTrack(track, localStream);
    });

    peerConnection.createOffer()
        .then(offer => peerConnection.setLocalDescription(offer))
        .then(() => {
        const offer = peerConnection.localDescription;
        sendMessage({ type: 'offer', data: offer });
        })
        .catch(error => {
        console.error('Erro ao criar oferta:', error);
        });
    }

    // Função para enviar mensagem para o servidor WebSocket
    function sendMessage(message) {
    socket.emit('message', message);
    }
</script>

receiverClient.vue:

<template>
    <div>
        <video ref="remoteVideo" autoplay controls></video>
    </div>
</template>

<script setup>
    import { ref, onMounted } from 'vue';
    import io from 'socket.io-client';

    const serverUrl = 'http://localhost';
    const serverPort = 3000;
    const socket = io(`${serverUrl}:${serverPort}`, { transports: ["websocket"] });

    let remoteStream;
    const remoteVideo = ref(null);
    let peerConnection;

    onMounted(() => {
        peerConnection = new RTCPeerConnection();

        peerConnection.ontrack = event => {
            remoteStream = event.streams[0];
            console.log('MediaStream recebido:', remoteStream);
            if (remoteVideo.value) {
            remoteVideo.value.srcObject = remoteStream;
            console.log('Número de faixas de vídeo:', remoteStream.getVideoTracks().length);
            console.log('Elemento de vídeo remoto:', remoteVideo.value);
            console.log('srcObject definido:', remoteVideo.value.srcObject);
            
            // Adicionando um novo elemento de vídeo
            const newVideoElement = document.createElement('video');
            newVideoElement.autoplay = true;
            newVideoElement.controls = true; // Adicionei a opção controls
            document.body.appendChild(newVideoElement); // Adicione o novo elemento de vídeo ao DOM
            newVideoElement.srcObject = remoteStream; // Atribua o MediaStream ao novo elemento de vídeo
            } else {
            console.error('Elemento de vídeo remoto não está definido.');
            }
        };

        // Recebendo a oferta do servidor WebSocket
        socket.on('message', message => {
            if (message.type === 'offer') {
            handleOffer(message.data);
            }
        });
    });

    // Função para processar a oferta e iniciar comunicação WebRTC
    function handleOffer(offer) {
        peerConnection.setRemoteDescription(new RTCSessionDescription(offer))
            .then(() => {
            return navigator.mediaDevices.getUserMedia({ video: true, audio: false });
            })
            .then(stream => {
            stream.getTracks().forEach(track => {
                peerConnection.addTrack(track, stream);
            });
            return peerConnection.createAnswer();
            })
            .then(answer => {
            return peerConnection.setLocalDescription(answer);
            })
            .then(() => {
            const answer = peerConnection.localDescription;
            sendMessage({ type: 'answer', data: answer });
            })
            .catch(error => {
            console.error('Erro ao processar oferta:', error);
            });
    }

    // Função para enviar mensagem para o servidor WebSocket
    function sendMessage(message) {
        socket.emit('message', message);
    }
</script>

server.js (Websocket Server):

import { createServer } from 'http';
import { Server } from 'socket.io';

const httpServer = createServer();
const io = new Server(httpServer);

io.on('connection', socket => {
  console.log('Um cliente se conectou:', socket.id);

  socket.on('disconnect', () => {
    console.log('Um cliente se desconectou:', socket.id);
  });

  socket.on('message', message => {
    socket.broadcast.emit('message', message);
    console.log('Mensagem enviada para todos os clientes:', message);
  });
});

const PORT = process.env.PORT || 3000;
httpServer.listen(PORT);
console.log(`Servidor Socket.io rodando em http://localhost:${PORT}`);

log

Trang chủ Giới thiệu Sinh nhật bé trai Sinh nhật bé gái Tổ chức sự kiện Biểu diễn giải trí Dịch vụ khác Trang trí tiệc cưới Tổ chức khai trương Tư vấn dịch vụ Thư viện ảnh Tin tức - sự kiện Liên hệ Chú hề sinh nhật Trang trí YEAR END PARTY công ty Trang trí tất niên cuối năm Trang trí tất niên xu hướng mới nhất Trang trí sinh nhật bé trai Hải Đăng Trang trí sinh nhật bé Khánh Vân Trang trí sinh nhật Bích Ngân Trang trí sinh nhật bé Thanh Trang Thuê ông già Noel phát quà Biểu diễn xiếc khỉ Xiếc quay đĩa Dịch vụ tổ chức sự kiện 5 sao Thông tin về chúng tôi Dịch vụ sinh nhật bé trai Dịch vụ sinh nhật bé gái Sự kiện trọn gói Các tiết mục giải trí Dịch vụ bổ trợ Tiệc cưới sang trọng Dịch vụ khai trương Tư vấn tổ chức sự kiện Hình ảnh sự kiện Cập nhật tin tức Liên hệ ngay Thuê chú hề chuyên nghiệp Tiệc tất niên cho công ty Trang trí tiệc cuối năm Tiệc tất niên độc đáo Sinh nhật bé Hải Đăng Sinh nhật đáng yêu bé Khánh Vân Sinh nhật sang trọng Bích Ngân Tiệc sinh nhật bé Thanh Trang Dịch vụ ông già Noel Xiếc thú vui nhộn Biểu diễn xiếc quay đĩa Dịch vụ tổ chức tiệc uy tín Khám phá dịch vụ của chúng tôi Tiệc sinh nhật cho bé trai Trang trí tiệc cho bé gái Gói sự kiện chuyên nghiệp Chương trình giải trí hấp dẫn Dịch vụ hỗ trợ sự kiện Trang trí tiệc cưới đẹp Khởi đầu thành công với khai trương Chuyên gia tư vấn sự kiện Xem ảnh các sự kiện đẹp Tin mới về sự kiện Kết nối với đội ngũ chuyên gia Chú hề vui nhộn cho tiệc sinh nhật Ý tưởng tiệc cuối năm Tất niên độc đáo Trang trí tiệc hiện đại Tổ chức sinh nhật cho Hải Đăng Sinh nhật độc quyền Khánh Vân Phong cách tiệc Bích Ngân Trang trí tiệc bé Thanh Trang Thuê dịch vụ ông già Noel chuyên nghiệp Xem xiếc khỉ đặc sắc Xiếc quay đĩa thú vị
Trang chủ Giới thiệu Sinh nhật bé trai Sinh nhật bé gái Tổ chức sự kiện Biểu diễn giải trí Dịch vụ khác Trang trí tiệc cưới Tổ chức khai trương Tư vấn dịch vụ Thư viện ảnh Tin tức - sự kiện Liên hệ Chú hề sinh nhật Trang trí YEAR END PARTY công ty Trang trí tất niên cuối năm Trang trí tất niên xu hướng mới nhất Trang trí sinh nhật bé trai Hải Đăng Trang trí sinh nhật bé Khánh Vân Trang trí sinh nhật Bích Ngân Trang trí sinh nhật bé Thanh Trang Thuê ông già Noel phát quà Biểu diễn xiếc khỉ Xiếc quay đĩa
Thiết kế website Thiết kế website Thiết kế website Cách kháng tài khoản quảng cáo Mua bán Fanpage Facebook Dịch vụ SEO Tổ chức sinh nhật