Skip to content

Instantly share code, notes, and snippets.

@robin-raymond
Last active October 4, 2015 14:47
Show Gist options
  • Save robin-raymond/90684c41edbcaf49a6e6 to your computer and use it in GitHub Desktop.
Save robin-raymond/90684c41edbcaf49a6e6 to your computer and use it in GitHub Desktop.
// Assume we have an audioTrack and a videoTrack to send.
// Assume also that we have a signaling function called signaller and a myCapsToSendParams library function.
// Create the ICE gatherer and transport. Since we are multiplexing RTP/RTCP and A/V we only need one.
var gatherOptions = new RTCIceGatherOptions;
gatherOptions.gatherPolicy = RTCIceGatherPolicy.all;
gatherOptions.iceservers = ... ;
var iceGatherer = new RTCIceGatherer(gatherOptions);
iceGatherer.onerror = errorHandler;
iceGatherer.onlocalcandidate = function (event) {signaller.mySendLocalCandidate(event.candidate);}
var iceTransport = new RTCIceTransport();
iceTransport.onicestatechange = ... ;
mySignaller.onRemoteCandidate = function(remote) {iceTransport.addRemoteCandidate(remote.candidate);}
// Create the DTLS transport. We only need one.
var dtlsTransport = new RTCDtlsTransport(iceTransport);
// Create the sender and receiver objects
var audioSender = new RtpSender(audioTrack, dtlsTransport);
var videoSender = new RtpSender(videoTrack, dtlsTransport);
var audioReceiver = new RtpReceiver(dtlsTransport);
var videoReceiver = new RtpReceiver(dtlsTransport);
// Retrieve the receiver and sender capabilities
var recvAudioCaps = RTCRtpReceiver.getCapabilities("audio");
var recvVideoCaps = RTCRtpReceiver.getCapabilities("video");
var sendAudioCaps = RTCRtpSender.getCapabilities("audio");
var recvVideoCaps = RTCRtpSender.getCapabilities("video");
// At this point, ICE/DTLS parameters and Send/Receive capabilities can be exchanged.
mySignaller.myOfferTracks({
// Offer the ICE and DTLS parameters
"ice": iceGatherer.getLocalParameters(),
"dtls": dtlsTransport.getLocalParameters(),
// Offer the receiver and sender audio and video capabilities.
"recvAudioCaps": recvAudioCaps,
"recvVideoCaps": recvVideoCaps,
"sendAudioCaps": sendAudioCaps,
"sendVideoCaps": sendVideoCaps
},
function(answer) {
// The responder answers with its preferences, parameters and capabilities
// Derive the send and receive parameters, assuming that RTP/RTCP mux will be enabled.
var audioSendParams = myCapsToSendParams(sendAudioCaps, answer.recvAudioCaps);
var videoSendParams = myCapsToSendParams(sendVideoCaps, answer.recvVideoCaps);
var audioRecvParams = myCapsToRecvParams(recvAudioCaps, answer.sendAudioCaps);
var videoRecvParams = myCapsToRecvParams(recvVideoCaps, answer.sendVideoCaps);
// Since we only have a single ICE transport and DTLS transport,
// no need for the ICE Transport Controller.
iceTransport.start(iceGatherer,answer.ice,RTCIceRole.controlling);
dtlsTransport.onerror = errorHandler;
dtlsTransport.start(remote.dtls);
};
// Set the audio and video send and receive parameters.
audioSender.send(audioSendParams);
videoSender.send(videoSendParams);
audioReceiver.receive(audioRecvParams);
videoReceiver.receive(videoRecvParams);
});
// Now we can render/play
// audioReceiver.track and videoReceiver.track.
// Helper functions
function errorHandler (error) {
console.log('Error encountered: ' + error.name);
}
RTCRtpParameters function myCapsToSendParams (RTCRtpCapabilities sendCaps, RTCRtpCapabilities remoteRecvCaps) {
// Function returning the sender RTCRtpParameters, based on the local sender and remote receiver capabilities.
// The goal is to enable a single stream audio and video call with minimum fuss.
// Steps to be followed:
// 1. Determine the RTP features that the receiver and sender have in common.
// 2. Determine the codecs that the sender and receiver have in common.
// 3. Within each common codec, determine the common formats, header extensions and rtcpFeedback mechanisms.
// 4. Determine the payloadType to be used, based on the receiver preferredPayloadType.
// 5. Set RTCRtcpParameters such as mux to their default values.
// 6. Return RTCRtpParameters enabling the jointly supported features and codecs.
}
RTCRtpParameters function myCapsToRecvParams (RTCRtpCapabilities recvCaps, RTCRtpCapabilities remoteSendCaps) {
// Function returning the receiver RTCRtpParameters, based on the local receiver and remote sender capabilities.
return myCapsToSendParams(remoteSendCaps, recvCaps);
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment