Last active
February 6, 2020 07:35
-
-
Save vivek1794/e150eb380a07e4e9e6ac54a39ace77ac to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
public void start() { | |
//Initialize PeerConnectionFactory globals. | |
//Params are context, initAudio,initVideo and videoCodecHwAcceleration | |
PeerConnectionFactory.initializeAndroidGlobals(this, true, true, true); | |
//Create a new PeerConnectionFactory instance. | |
PeerConnectionFactory.Options options = new PeerConnectionFactory.Options(); | |
peerConnectionFactory = new PeerConnectionFactory(options); | |
//Now create a VideoCapturer instance. Callback methods are there if you want to do something! Duh! | |
VideoCapturer videoCapturerAndroid = getVideoCapturer(new CustomCameraEventsHandler()); | |
//Create MediaConstraints - Will be useful for specifying video and audio constraints. | |
audioConstraints = new MediaConstraints(); | |
videoConstraints = new MediaConstraints(); | |
//Create a VideoSource instance | |
videoSource = peerConnectionFactory.createVideoSource(videoCapturerAndroid, videoConstraints); | |
localVideoTrack = peerConnectionFactory.createVideoTrack("100", videoSource); | |
//create an AudioSource instance | |
audioSource = peerConnectionFactory.createAudioSource(audioConstraints); | |
localAudioTrack = peerConnectionFactory.createAudioTrack("101", audioSource); | |
localVideoView.setVisibility(View.VISIBLE); | |
//create a videoRenderer based on SurfaceViewRenderer instance | |
localRenderer = new VideoRenderer(localVideoView); | |
// And finally, with our VideoRenderer ready, we | |
// can add our renderer to the VideoTrack. | |
localVideoTrack.addRenderer(localRenderer); | |
} | |
private void call() { | |
//we already have video and audio tracks. Now create peerconnections | |
List<PeerConnection.IceServer> iceServers = new ArrayList<>(); | |
//create sdpConstraints | |
sdpConstraints = new MediaConstraints(); | |
sdpConstraints.mandatory.add(new MediaConstraints.KeyValuePair("offerToReceiveAudio", "true")); | |
sdpConstraints.mandatory.add(new MediaConstraints.KeyValuePair("offerToReceiveVideo", "true")); | |
//creating localPeer | |
localPeer = peerConnectionFactory.createPeerConnection(iceServers, sdpConstraints, new CustomPeerConnectionObserver("localPeerCreation") { | |
@Override | |
public void onIceCandidate(IceCandidate iceCandidate) { | |
super.onIceCandidate(iceCandidate); | |
onIceCandidateReceived(localPeer, iceCandidate); | |
} | |
}); | |
//creating remotePeer | |
remotePeer = peerConnectionFactory.createPeerConnection(iceServers, sdpConstraints, new CustomPeerConnectionObserver("remotePeerCreation") { | |
@Override | |
public void onIceCandidate(IceCandidate iceCandidate) { | |
super.onIceCandidate(iceCandidate); | |
onIceCandidateReceived(remotePeer, iceCandidate); | |
} | |
@Override | |
public void onAddStream(MediaStream mediaStream) { | |
super.onAddStream(mediaStream); | |
gotRemoteStream(mediaStream); | |
} | |
}); | |
//creating local mediastream | |
MediaStream stream = peerConnectionFactory.createLocalMediaStream("102"); | |
stream.addTrack(localAudioTrack); | |
stream.addTrack(localVideoTrack); | |
localPeer.addStream(stream); | |
//creating Offer | |
localPeer.createOffer(new CustomSdpObserver("localCreateOffer"){ | |
@Override | |
public void onCreateSuccess(SessionDescription sessionDescription) { | |
//we have localOffer. Set it as local desc for localpeer and remote desc for remote peer. | |
//try to create answer from the remote peer. | |
super.onCreateSuccess(sessionDescription); | |
localPeer.setLocalDescription(new CustomSdpObserver("localSetLocalDesc"), sessionDescription); | |
remotePeer.setRemoteDescription(new CustomSdpObserver("remoteSetRemoteDesc"), sessionDescription); | |
remotePeer.createAnswer(new CustomSdpObserver("remoteCreateOffer") { | |
@Override | |
public void onCreateSuccess(SessionDescription sessionDescription) { | |
//remote answer generated. Now set it as local desc for remote peer and remote desc for local peer. | |
super.onCreateSuccess(sessionDescription); | |
remotePeer.setLocalDescription(new CustomSdpObserver("remoteSetLocalDesc"), sessionDescription); | |
localPeer.setRemoteDescription(new CustomSdpObserver("localSetRemoteDesc"), sessionDescription); | |
} | |
},new MediaConstraints()); | |
} | |
},sdpConstraints); | |
} | |
private void hangup() { | |
localPeer.close(); | |
remotePeer.close(); | |
localPeer = null; | |
remotePeer = null; | |
} | |
private void gotRemoteStream(MediaStream stream) { | |
//we have remote video stream. add to the renderer. | |
final VideoTrack videoTrack = stream.videoTracks.getFirst(); | |
AudioTrack audioTrack = stream.audioTracks.getFirst(); | |
runOnUiThread(new Runnable() { | |
@Override | |
public void run() { | |
try { | |
remoteRenderer = new VideoRenderer(remoteVideoView); | |
remoteVideoView.setVisibility(View.VISIBLE); | |
videoTrack.addRenderer(remoteRenderer); | |
} catch (Exception e) { | |
e.printStackTrace(); | |
} | |
} | |
}); | |
} | |
public void onIceCandidateReceived(PeerConnection peer, IceCandidate iceCandidate) { | |
//we have received ice candidate. We can set it to the other peer. | |
if (peer == localPeer) { | |
remotePeer.addIceCandidate(iceCandidate); | |
} else { | |
localPeer.addIceCandidate(iceCandidate); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment