muaz-khan / WebRTC-Experiment

WebRTC, WebRTC and WebRTC. Everything here is all about WebRTC!!
https://www.webrtc-experiment.com/
MIT License
11.77k stars 3.95k forks source link

one way video and two way audio #188

Open pastissolo opened 10 years ago

pastissolo commented 10 years ago

Hi, i am trying to set up a project for video assistance, audio comunication should be both ways, video comunication should be only one way, from one peer to other(s).

i found that impossible to do; in my code, video and audio comunication both ways work perfectly; than i changed into :

connection.onNewSession = function(session) {
                    if (sessions[session.sessionid] ) return;
                    sessions[session.sessionid] = session;

                    var tr = document.createElement('tr');
                    tr.innerHTML = '<td><strong>' + session.extra['session-name'] +                  '</strong> is an active session.</td>' +
                        '<td><button class="join">Join</button></td>';
                    roomsList.insertBefore(tr, roomsList.firstChild);

                    tr.querySelector('.join').setAttribute('data-sessionid', session.sessionid);
                    tr.querySelector('.join').onclick = function() {
                        this.disabled = true;
                        session = sessions[this.getAttribute('data-sessionid')];
                        if (!session) alert('No room to join.');

                        session.session ={
                        audio: true
                        }
                        //connection.sdpConstraints.mandatory = {
                        //            OfferToReceiveAudio: true,
                        //            OfferToReceiveVideo: true
                        //  }

                        connection.join(session);
                      //  session.join({audio: true});
                    };
                };

This way:

i found that offers to receive audio and video are setted by default the same as getusermedia capture options:

setConstraints: function () {
                this.constraints = {
                    optional: this.sdpConstraints.optional || [],
                    mandatory: this.sdpConstraints.mandatory || {
                        OfferToReceiveAudio: !!this.session.audio,
                        OfferToReceiveVideo: !!this.session.video || !!this.session.screen
                    }
                };

so i forced sdpConstraints:

connection.sdpConstraints.mandatory = {
                                 OfferToReceiveAudio: true,
                                 OfferToReceiveVideo: true
                          }

it is commented on the code above inside onNewSession().

but this way the behaviour is odd:

do you have any idea?

muaz-khan commented 10 years ago

You should try a similar demo!

Initiator's Code

// initiator's session
var initiator = new RTCMultiConnection();

// initiator will share both audio and video!
initiator.session = {
    audio: true,
    video: true
};

initiator.transmitRoomOnce = true;
initiator.open();

Participant's Code

// this is participant's code
var participant = new RTCMultiConnection();
participant.onNewSession = function (session) {

    // participant will join with only audio!
    session.join({
        audio: true
    });

};
participant.connect();

This example is many-to-many i.e. audio will be shared among all "interconnected" users; and all participants will receive video from initiator.

pastissolo commented 10 years ago

???? sorry, pheraps i am confused.... the method
-connect() has to be called from both initiator and participant to set up the signaling channel. if i don't i can see the peers can't comunicate;

if i do ( i added initiator.connect() at the end of your first block of code) what happens is that initiator capture his own audio and video and share it with participant, participant, on the other hand, capture his own audio but it does not share it with initiator.

ps: i am using websocket

muaz-khan commented 10 years ago

Is "connect" method mandatory?

No. NEVER!

What actually "connect" method do?

This method initializes RTCMultiSession object; which is the backbone object used to handle everything in RTCMultiConnection.

On constructor invocation of RTCMultiSession object, it (i.e. RTCMultiSession) tries to connect with signaling-server.

That's why it is said that "connect" method connect with signaling server!

Both "open" and "join" do the same job! Both initializes RTCMultiSession which (i.e. RTCMultiSession), on constructor invocation, quickly tries to connect with signaling server.

Difference between "open" and "connect" is that "open" method do two especial things for you:

  1. changes your role to "initiator"
  2. starts transmitting room-descriptions

Room-description is what passed over onNewSession event.

Why above code snippets failed for me?

Because you're using transmitRoomOnce which transmits room-description as soon as it is created; however transmits just once. So, newcomers or late-viewers can't see/access those room-descriptions.

For initiator

var initiator = new RTCMultiConnection('channel-id');

// initiator will share both audio and video!
initiator.session = {
    audio: true,
    video: true
};

// don't need to call "connect" here
// it will be redundant
// though it will NOT affect the code!
initiator.open();

For participant

// this is participant's code
var participant = new RTCMultiConnection('channel-id');

// this object stores "room-descriptions" to make sure
// duplicate descriptions are not passed over "onNewSession" event handler
var listOfRoomDescriptions = {};

participant.onNewSession = function (roomDescription) {
    if (listOfRoomDescriptions[roomDescription.sessionid]) return;
    listOfRoomDescriptions[roomDescription.sessionid] = roomDescription;

    // participant will join with only audio!
    roomDescription.join({
        audio: true
    });

};
participant.connect();