Home > front end >  How to implement webrtc MediaStream object reuse
How to implement webrtc MediaStream object reuse

Time:10-09

Now use webrtc, found that the connection is established, can't correct to send and receive audio and video data, the code is as follows:
 
"Use strict";
//UserMedia configure
Var mediaConstraints={
Audio: true,//We want an audio track
Video: true
};

Var offerConstraints={
OfferToReceiveAudio: 1,
OfferToReceiveVideo: 1,
VoiceActivityDetection: false
};

Var myPeerConnection=null;//RTCPeerConnection
Var localStream=null;
Var webStream=null;
Var testsdp=null;
Var dataChannel=null;

//real send function
The function the send (MSG) {
Var msgJSON=JSON. Stringify (MSG);
MySend ({
Address: the document. The getElementById (' name '). The value
}, msgJSON, (err)=& gt; {
If (err) {
console.log(err);
}
});
}

The function sendToServer (MSG) {
The console. The log (" send MSG ");
The console. The log (MSG);
Var msgJSON=JSON. Stringify (MSG);
Send (msgJSON);
}

The function handleNewICECandidateMsg (MSG) {
Try {
Var candidate=new RTCIceCandidate (MSG) candidate);
The console. The log (" * * * Adding received ICE candidate: "+ JSON. The stringify (candidate));

if (! MyPeerConnection) {createPeerConnection (); }
MyPeerConnection. AddIceCandidate (candidate)
{} the catch (err)
ReportError (err);
}
}

//called when the receive MSG
The function onMessage (res) {
Var MSG=JSON. Parse (JSON parse (res) data));
The console. The log (" Message received: "+ MSG. Type);
The console. The log (MSG);

The switch (MSG) type) {
Case "video - offer" ://Invitation and offer to chat
HandleVideoOfferMsg (MSG);
break;

Case "video - the answer:"//the Callee has opportunities our offer
HandleVideoAnswerMsg (MSG);
break;

Case "hang - up" ://The other peer has hung up The call
HandleHangUpMsg (MSG);
break;

Case "of" new - ice - candidate:
HandleNewICECandidateMsg (MSG);
break;

Default:
Log_error (" Unknown message received: ");
The console. The log (MSG);
}
};
//called when RTCPeerConnection. AddStream was called
The function handleAddStream (event) {
The console. The log (" * * * handleAddStream ");
console.log(event);
WebStream=event. Streams [0] | | event. The stream;
Document. The getElementById (" received_video "). SrcObject=webStream;
Document. The getElementById (" received_video "). CrossOrigin='*';
Document. The getElementById (" hangup - button "). The disabled=false;
}

The function handleICECandidateEvent (event) {
If (event. Candidate) {
Console. The log (" * * * Outgoing ICE candidate: "+ event. The candidate. The candidate).

SendToServer ({
Type: "new - ice - candidate,"
Candidate: event. Candidate
});
}
}

The function handleSignalStateChanged (event) {
The console. The log (" * * * * * handleSignalStateChanged * * * * * ");
The console. The log (event)
}

//create a RTCPeerConnection
The function createPeerConnection () {
The console. The log (" Setting up a connection... ");

MyPeerConnection=new RTCPeerConnection ({" rtcpMuxPolicy ":" require "and" bundlePolicy ":" Max - bundle ", "iceServers" : []});
MyPeerConnection. Ontrack=handleAddStream;
MyPeerConnection. Onicecandidate=handleICECandidateEvent
MyPeerConnection. Onsignalingstatechange=handleSignalStateChanged;
}
//close the Video call chat
The function closeVideoCall () {
Var localVideo=document. GetElementById (" local_video ");


The console. The log (" Closing the call ");

//Close the RTCPeerConnection

If (myPeerConnection) {
The console. The log (" -- -- & gt; Closing the peer connection ");

If (localVideo. SrcObject) {
LocalVideo. Pause ();
LocalVideo. SrcObject. GetTracks (). The forEach (track=& gt; {
Track. Stop ();
});
}

//Close the peer connection

MyPeerConnection. Close ();
//dataChannel. Close ();
//dataChannel=null;
MyPeerConnection=null;
}
Document. The getElementById (" hangup - button "). The disabled=true;
}
//deal with the hangup MSG
The function handleHangUpMsg (MSG) {
The console. The log (" * * * a Received hang up notification from other peer ");

CloseVideoCall ();
}
//the click event
The function hangUpCall () {
CloseVideoCall ();

SendToServer ({
Type: "hang - up"
});
}

The function sendMsg () {
//dataChannel. Send (" hello, world ");
}

//the click event to start a video call chat
The function the invite () {
The console. The log (" Starting to prepare an invitation ");
If (myPeerConnection) {
The console. The log (" You can 't start a call because You already have one open!" );
} else {
CreatePeerConnection ();
The navigator. MediaDevices. GetUserMedia (mediaConstraints). Then (function (stream) {
LocalStream=stream;
The console. The log (' localStream ');
The console. The log (localStream);
//Adding a local stream won 't trigger the onaddstream callback
nullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnull
  • Related