Skip to content

Instantly share code, notes, and snippets.

@shriphani
Created October 19, 2015 01:37
Show Gist options
  • Save shriphani/d011bad5083772b67119 to your computer and use it in GitHub Desktop.
Save shriphani/d011bad5083772b67119 to your computer and use it in GitHub Desktop.
import java.io.FileWriter;
import java.nio.ByteBuffer;
import java.util.LinkedList;
import org.webrtc.DataChannel;
import org.webrtc.DataChannel.Buffer;
import org.webrtc.IceCandidate;
import org.webrtc.MediaConstraints;
import org.webrtc.MediaStream;
import org.webrtc.PeerConnection;
import org.webrtc.DataChannel.Init;
import org.webrtc.PeerConnection.IceConnectionState;
import org.webrtc.PeerConnection.IceGatheringState;
import org.webrtc.PeerConnection.SignalingState;
import org.webrtc.VideoRenderer.I420Frame;
import org.webrtc.PeerConnectionFactory;
import org.webrtc.SdpObserver;
import org.webrtc.SessionDescription;
import org.webrtc.VideoCapturer;
import org.webrtc.VideoRenderer;
import org.webrtc.VideoSource;
import org.webrtc.VideoTrack;
// this spins up 2 clients.
// one sends stuff, the other mirrors it.
public class SingleProcProtocol {
private static class OfferPCObserver implements PeerConnection.Observer, DataChannel.Observer {
private IceCandidate candidate = null;
private DataChannel dataChannel = null;
@Override
public void onAddStream(MediaStream arg0) {
// TODO Auto-generated method stub
System.out.println("I have now received my mirror stream!!!!");
}
@Override
public void onDataChannel(DataChannel arg0) {
// TODO Auto-generated method stub
System.out.println("Offer data channel");
arg0.registerObserver(this);
dataChannel = arg0;
}
@Override
public void onIceCandidate(IceCandidate arg0) {
if (candidate == null) {
System.out.println("Offer Ice Candidate Generated");
this.candidate = arg0;
}
}
public IceCandidate getCandidate() {
return this.candidate;
}
@Override
public void onIceConnectionChange(IceConnectionState arg0) {
// TODO Auto-generated method stub
}
@Override
public void onIceConnectionReceivingChange(boolean arg0) {
// TODO Auto-generated method stub
}
@Override
public void onIceGatheringChange(IceGatheringState arg0) {
// TODO Auto-generated method stub
}
@Override
public void onRemoveStream(MediaStream arg0) {
// TODO Auto-generated method stub
}
@Override
public void onRenegotiationNeeded() {
// TODO Auto-generated method stub
}
@Override
public void onSignalingChange(SignalingState arg0) {
// TODO Auto-generated method stub
}
@Override
public void onMessage(DataChannel.Buffer buffer) {
System.out.println("Message received!!!!!");
}
@Override
public void onBufferedAmountChange(long arg0) {
// TODO Auto-generated method stub
}
@Override
public void onStateChange() {
// TODO Auto-generated method stub
}
}
private static class AnswerPCObserver implements PeerConnection.Observer,
DataChannel.Observer{
private IceCandidate candidate = null;
private boolean stream = false;
VideoRenderer renderer = null;
VideoTrack track = null;
@Override
public void onAddStream(MediaStream arg0) {
// TODO Auto-generated method stub
System.out.println("Answerer got a stream");
if (!stream) {
stream = true;
// renderer = new VideoRenderer(new VideoRenderer.Callbacks() {
//
// @Override
// public void renderFrame(I420Frame arg0) {
// System.out.println("Added frame");
//
// }
// });
renderer = VideoRenderer.createGui(640, 480);
track = arg0.videoTracks.get(0);
System.out.println("UGH");
track.addRenderer(renderer);
}
}
@Override
public void onDataChannel(DataChannel arg0) {
arg0.registerObserver(this);
}
@Override
public void onIceCandidate(IceCandidate arg0) {
if (candidate == null) {
System.out.println("Answer Ice Candidate generated");
this.candidate = arg0;
}
}
public IceCandidate getCandidate() {
return this.candidate;
}
@Override
public void onIceConnectionChange(IceConnectionState arg0) {
// TODO Auto-generated method stub
}
@Override
public void onIceConnectionReceivingChange(boolean arg0) {
// TODO Auto-generated method stub
}
@Override
public void onIceGatheringChange(IceGatheringState arg0) {
// TODO Auto-generated method stub
}
@Override
public void onRemoveStream(MediaStream arg0) {
// TODO Auto-generated method stub
}
@Override
public void onRenegotiationNeeded() {
// TODO Auto-generated method stub
}
@Override
public void onSignalingChange(SignalingState arg0) {
// TODO Auto-generated method stub
}
@Override
public void onBufferedAmountChange(long arg0) {
// TODO Auto-generated method stub
}
@Override
public void onMessage(Buffer arg0) {
// TODO Auto-generated method stub
System.out.println("GOT A MESSAGE FROM THE OFFERER");
}
@Override
public void onStateChange() {
// TODO Auto-generated method stub
}
}
// the SDPObserver used by the Offering client
private static class OfferSdpObserver implements SdpObserver {
private SessionDescription sdp = null;
private String error = null;
private PeerConnection pc = null;
//private CountDownLatch latch = new CountDownLatch(1);
public OfferSdpObserver(PeerConnection pc) {
this.pc = pc;
}
@Override
public void onCreateSuccess(SessionDescription sdp) {
// offer created, serialize to disk
System.out.println("Offer successfully created");
this.sdp = sdp;
// this should now fire off ice-candidates;
pc.setLocalDescription(this, sdp);
//
}
@Override
public void onSetSuccess() {
System.out.println("Offer obs Set succeeded");
//pc.setRemoteDescription(this, this.sdp);
}
@Override
public void onCreateFailure(String error) {
onSetFailure(error);
}
@Override
public void onSetFailure(String error) {
this.error = error;
System.out.println(error);
}
public SessionDescription getSdp() {
return sdp;
}
}
// the SDPObserver used by the Offering client
private static class AnswerSdpObserver implements SdpObserver {
private SessionDescription sdp = null;
private PeerConnection pc = null;
private boolean success = false;
public AnswerSdpObserver(PeerConnection pc) {
this.pc = pc;
}
@Override
public void onCreateSuccess(SessionDescription sdp) {
System.out.println("Answer create succeeded");
// This now fires off ice candidates;
this.sdp = sdp;
pc.setLocalDescription(this, sdp);
}
@Override
public void onSetSuccess() {
if (!success) {
System.out.println("Answering obs Set succeeded");
System.out.println("Creating answer");
pc.createAnswer(this, new MediaConstraints());
success = true;
} else {
System.out.println("Answer local Descr set");
}
}
@Override
public void onCreateFailure(String error) {
onSetFailure(error);
}
@Override
public void onSetFailure(String error) {
System.out.println(error);
}
public SessionDescription getSdp() {
return sdp;
}
}
public static void main(String ... args) {
PeerConnectionFactory factory = new PeerConnectionFactory();
PeerConnectionFactory.Options options = new PeerConnectionFactory.Options();
options.networkIgnoreMask = 0;
factory.setOptions(options);
MediaConstraints pcConstraints = new MediaConstraints();
pcConstraints.optional.add(
new MediaConstraints.KeyValuePair("DtlsSrtpKeyAgreement", "true"));
pcConstraints.optional.add(new MediaConstraints.KeyValuePair("OfferToReceiveVideo", "true"));
pcConstraints.optional.add(new MediaConstraints.KeyValuePair("OfferToReceiveAudio", "true"));
//pcConstraints.mandatory.add(new MediaConstraints.KeyValuePair("OfferToSendVideo", "true"));
//pcConstraints.optional.add(
// new MediaConstraints.KeyValuePair("internalSctpDataChannels",
// "true")
// );
//pcConstraints.optional.add(
// new MediaConstraints.KeyValuePair("RtpDataChannels", "true"));
LinkedList<PeerConnection.IceServer> iceServers =
new LinkedList<PeerConnection.IceServer>();
iceServers.add(new PeerConnection.IceServer(
"stun:stun.l.google.com:19302"));
OfferPCObserver offerObserver = new OfferPCObserver();
VideoSource videoSource = factory.createVideoSource(VideoCapturer.create(""), pcConstraints );
VideoTrack videoTrack = factory.createVideoTrack("ARDAMSv0", videoSource);
MediaStream lms = factory.createLocalMediaStream("ARDAMS");
lms.addTrack(videoTrack);
MediaStream mirrorStream = factory.createLocalMediaStream("mirror");
PeerConnection offerpc = factory.createPeerConnection(iceServers, pcConstraints, offerObserver);
offerpc.addStream(lms);
// now comes the answerer
AnswerPCObserver answerObserver = new AnswerPCObserver();
PeerConnection answerpc = factory.createPeerConnection(iceServers, pcConstraints, answerObserver);
//answerpc.addStream(mirrorStream);
// offering pc must now set up its offer
//DataChannel.Init init = new DataChannel.Init();
//init.id = 1;
//DataChannel.Init init2 = new DataChannel.Init();
//init.id = 2;
//DataChannel answerDc = answerpc.createDataChannel("1", init);
//DataChannel offerDc = offerpc.createDataChannel("2", init2);
//System.out.println("answer dc: " + answerDc);
OfferSdpObserver offerSdpObserver = new OfferSdpObserver(offerpc);
offerpc.createOffer(offerSdpObserver, new MediaConstraints());
// answer pc must now see this offer and create an answer
AnswerSdpObserver answerSdpObserver = new AnswerSdpObserver(answerpc);
SessionDescription offerSdp = offerSdpObserver.getSdp();
//answerDc.registerObserver(offerObserver);
//offerDc.registerObserver(answerObserver);
while (offerSdp == null) {
try {
Thread.sleep(1000);
} catch (Exception e) {
// pass
}
offerSdp = offerSdpObserver.getSdp();
}
System.out.println("Setting local description: offer");
//offerpc.setLocalDescription(offerSdpObserver, offerSdp);
while (answerpc.signalingState() != SignalingState.STABLE) {
try {
Thread.sleep(1000);
System.out.println("answerpc is not ready to signal");
} catch (Exception e) {
// pass
}
}
System.out.println("Setting remote description: answer");
answerpc.setRemoteDescription(answerSdpObserver, offerSdp);
//answerpc.createAnswer(answerSdpObserver, new MediaConstraints());
SessionDescription answerSdp = answerSdpObserver.getSdp();
while (answerSdp == null) {
try {
Thread.sleep(1000);
System.out.println("Answer sdp is null");
} catch (Exception e) {
// pass
}
answerSdp = answerSdpObserver.getSdp();
}
while (offerpc.signalingState() != SignalingState.STABLE) {
try {
Thread.sleep(1000);
System.out.println("offerpc is not ready to signal");
offerpc.setRemoteDescription(offerSdpObserver, answerSdp);
} catch (Exception e) {
// pass
}
}
//answerpc.setLocalDescription(answerSdpObserver, answerSdp);
//while ()
// now both guys fire off their candidates
//offerpc.setLocalDescription(offerSdpObserver, offerSdp);
// Now both pc's exchange their candidate information
IceCandidate offerCandidate = null;
IceCandidate answerCandidate = null;
while (offerCandidate == null || answerCandidate == null) {
try {
Thread.sleep(1000);
} catch (Exception e) {
// fuck it
}
System.out.println("Offer candidate: " + offerCandidate);
System.out.println("Answer candidate: " + answerCandidate);
System.out.println("Candidates not recd yet");
offerCandidate = offerObserver.getCandidate();
answerCandidate = answerObserver.getCandidate();
}
System.out.println("Candidates received");
//offerpc.addIceCandidate(answerCandidate);
//answerpc.addIceCandidate(offerCandidate);
//VideoTrack mirrorTrack = answerObserver.track;
//mirrorStream.addTrack(mirrorTrack);
//answerpc.addStream(mirrorStream);
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment