peers / peerjs

Simple peer-to-peer with WebRTC.
https://peerjs.com
MIT License
12.48k stars 1.43k forks source link

One way stream #259

Closed gmbad closed 5 years ago

gmbad commented 9 years ago

There is the possibility of only one party to send the audio and video stream, and the other party only receive this estream without the need to access the webcam prompt to receive this stream?

thank you so much,

Gabriel

needitfast commented 9 years ago

Sorry i have no awnser for that yet, but i would like to know as well. I tried just not adding a mediastream to one side but that just results in:

"ERROR" "To call a peer, you must provide a stream from your browser's getUserMedia."

UPDATE: I solved this now by opening a data connection instead of a call on the client side, then calling from the server side with the mediastream

luongnv89 commented 9 years ago

What do you mean by "calling from the server side with the mediastream"? Would love to know how could you manage one way stream. Thank you very much!

needitfast commented 9 years ago

Ok on the client i open a data connection (not a call) then on the server side i open a call to whoever made the data connection and attach the stream like this

peer.on( 'connection', function(conn) { conn.on( 'open', function() { var call = peer.call(conn.peer, window.localStream); }); });

On the client i automatically accept the call and display the video like a normal call.

Basicly one way stream is just not attaching the video stream to one end, but somehow this only works on the receiving end because the call never starts otherwise.

Im not even remotly sure if this is how its supposed to be done, or if there is a more elegant sollution, but it works for me

luongnv89 commented 9 years ago

@needitfast I think I know how it works. Just made a test and leaned that you do not need to open data connection to do that. On the received peers, just answer a call with 'null' (instead of 'window.localStream') or even ignore answering the call (as you did, that is explanation for "but somehow this only works on the receiving end because the call never starts otherwise") Thank you for your reply. Your solution could be used as a "trick" for one way streaming.

needitfast commented 9 years ago

Thanks, i actually tried sending null instead of a stream, for me that resulted in an “error in function”

Von: NGUYEN VAN LUONG [mailto:notifications@github.com] Gesendet: Montag, 26. Jänner 2015 11:10 An: peers/peerjs Cc: Techxpert - Florian Knabl Betreff: Re: [peerjs] One way stream (#259)

@needitfasthttps://github.com/needitfast I think I know how it works. Just made a test and leaned that you do not need to open data connection to do that. On the received peers, just answer a call with 'null' (instead of 'window.localStream') or even ignore answering the call (as you did, that is explanation for "but somehow this only works on the receiving end because the call never starts otherwise") Thank you for your reply. Your solution could be used as a "trick" for one way streaming.

— Reply to this email directly or view it on GitHubhttps://github.com/peers/peerjs/issues/259#issuecomment-71438389.

luongnv89 commented 9 years ago

@needitfast You cannot make a call with the media stream is "null", but you can answer a call with media stream is "null" (just made a test).

needitfast commented 9 years ago

Oh yes, sorry i missunderstood. I needed the call to be triggered from the client side, but streamed from the server side, which is why I did that “data connection hack” so the server opens the actuall streaming connection anyway

Von: NGUYEN VAN LUONG [mailto:notifications@github.com] Gesendet: Montag, 26. Jänner 2015 11:15 An: peers/peerjs Cc: Techxpert - Florian Knabl Betreff: Re: [peerjs] One way stream (#259)

@needitfasthttps://github.com/needitfast You cannot make a call with the media stream is "null", but you can answer a call with media stream is "null" (just made a test).

— Reply to this email directly or view it on GitHubhttps://github.com/peers/peerjs/issues/259#issuecomment-71439035.

mattman93 commented 8 years ago

@needitfast I am trying to do this same thing, can you post more of your code perhaps? Or is it proprietary-ish?

gmbad commented 8 years ago

PeerJS is obsolet. Check the RTCMultiConnection in google...

afrokick commented 5 years ago

Just create MediaStream with empty tracks

madchops1 commented 4 years ago

new MediaStream does not work because the call does not have any return mediastream then

maximus1127 commented 3 years ago

I know I'm super late to this game but adding stream.getTracks().forEach(track => track.stop()); just after the call is made to the client will release the webcam on the initiator side. Not sure if there's a better way but it works for me.

giuseppeg commented 2 years ago

I got one-way streaming to work by implementing @needitfast's idea. In this setup it is the streamer who calls the listener.

Streamer:

navigator.mediaDevices
  .getUserMedia({ video: false, audio: true })
  .then((stream) => {

    const peer = new Peer("STREAMER_ID");

    peer.on('connection', (conn) => {
      conn.on('open', (data) => {
        // when a listener connects, call them!
        peer.call(
          conn.peer,
          stream
        )
      });
    });
  });

Listener:

const peer = new Peer();
const audio = new Audio();

// When receiving the stream, they need to interact with the page in order for play to work. 
// Add a button or something.
document.addEventListener('click', event => {
  audio.play();
});

peer.on("call", (call) => {
  call.on("stream", (remoteStream) => {
    audio.muted = false;
    audio.srcObject = remoteStream;
  })
  call.answer(null);
});

peer.on('open', () => {
  // connect to the streamer so that they will call us
  peer.connect("STREAMER_ID");
});