Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

How to addTracks to a connection? #801

Open
ohuu opened this issue Mar 8, 2021 · 6 comments
Open

How to addTracks to a connection? #801

ohuu opened this issue Mar 8, 2021 · 6 comments
Labels
client related to peerjs client

Comments

@ohuu
Copy link

ohuu commented Mar 8, 2021

I'm trying to stream multiple video and audio streams from one peer to the other over a single connection. From what I can gather reading WebRTC docs this can be acheived by called RtcPeerConnection.addTrack on one side of the connection as long as the other side has a track listener (e.g. pc.addEventListener('track', (evt) => ...)).

However, this doesn't seem to work. My code is as follows:

Caller:

const call = peer.call(peerId, localStream);

call.on("stream", (remoteStream) => { // <-- triggered when call is answered
    console.log(`received remote stream from host ${remoteSocketId} ${remoteStream}`);
    addUpdateStream(remoteSocketId, remoteStream);
});

// listen for tracks
console.log("adding track event listener");
call.peerConnection.addEventListener("track", (evt) => { // <-- Not triggered
    console.log(`received track ${evt.track.label} with ${evt.streams.length} streams from the host`);
    evt.streams.forEach(stream => {
        console.log(`${stream.id} - ${stream.getTracks().length}`);
    })
});

Receiver:

peer.on("call", (call) => {
    call.answer(localStream); // Answer the call with an A/V stream.
    call.on("stream", (remoteStream) => {
        addUpdateStream(remoteSocketId, remoteStream);
    });

    // if you are the host then add the studio streams as tracks to the local stream
    if (amiHost.value) {
        hostStreams.value.forEach(stream => {
            stream.getTracks().forEach(track => {
                console.log(`adding track ${track.label} to peer connection`);
                call.peerConnection.addTrack(track, stream); // <-- tracks are definitely added to the connection
            })
        });
    }
});

Am I misunderstanding something here? Is it even possible to add multiple streams to a single connection or do I need to open a new connection for each stream? If there are any examples of doing something similar that would be great.

@bobby-white
Copy link

Did you happen to figure out a solution for this?

@doleron
Copy link

doleron commented Apr 25, 2022

same problem

1 similar comment
@skytree8
Copy link

skytree8 commented May 8, 2023

same problem

@aschelch
Copy link

up please ;)

@qstiegler
Copy link

please help

@irgalamarr irgalamarr added the client related to peerjs client label Feb 24, 2024
@ElliotRoe
Copy link

Same problem here

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
client related to peerjs client
Projects
None yet
Development

No branches or pull requests

8 participants