Skip to content

Instantly share code, notes, and snippets.

View Abhi1code's full-sized avatar
🎯
Focusing

Abhishek Meharia Abhi1code

🎯
Focusing
View GitHub Profile
apply plugin: 'com.android.application'
android {
compileSdkVersion 29
buildToolsVersion "29.0.2"
defaultConfig {
applicationId "com.matr..."
minSdkVersion 21
targetSdkVersion 29
versionCode 1
package com.m...;
import org.webrtc.DataChannel;
public class CustomDataChannelObserver implements DataChannel.Observer {
public CustomDataChannelObserver(){
}
@Override
package com.m...;
import com.mat...;
import org.webrtc.DataChannel;
import org.webrtc.IceCandidate;
import org.webrtc.MediaStream;
import org.webrtc.PeerConnection;
import org.webrtc.RtpReceiver;
package com.matrixf...n;
import org.webrtc.SdpObserver;
import org.webrtc.SessionDescription;
public class CustomSdpObserver implements SdpObserver {
public CustomSdpObserver(){
}
peerConnection = peerConnectionFactory.createPeerConnection(rtcConfig, new CustomPeerConnectionObserver() {
@Override
public void onIceCandidate(IceCandidate iceCandidate) {
super.onIceCandidate(iceCandidate);
//
}
@Override
public void onDataChannel(DataChannel dataChannel) {
public void setIceCandidate(String candidate, String sdpmid, int sdpmlineIndex){
peerConnection.addIceCandidate(new IceCandidate(sdpmid, sdpmlineIndex, candidate));
}
DataChannel.Init dcInit = new DataChannel.Init();
dataChannel = peerConnection.createDataChannel("1", dcInit);
dataChannel.registerObserver(new CustomDataChannelObserver(){
@Override
public void onMessage(DataChannel.Buffer buffer) {
super.onMessage(buffer);
// will be called when message is received from another peer
}
});
public void initialize(){
PeerConnectionFactory.InitializationOptions initializationOptions =
PeerConnectionFactory.InitializationOptions.builder(context)
.createInitializationOptions();
PeerConnectionFactory.initialize(initializationOptions);
PeerConnectionFactory.Options options = new PeerConnectionFactory.Options();
peerConnectionFactory = PeerConnectionFactory.builder().setOptions(options)
.createPeerConnectionFactory();
public void createOffer(){
peerConnection.createOffer(new CustomSdpObserver(){
@Override
public void onCreateSuccess(SessionDescription sessionDescription) {
super.onCreateSuccess(sessionDescription);
peerConnection.setLocalDescription(new CustomSdpObserver(), sessionDescription);
// sessionDescription.description is string which needs to the shared across network
public void createAnswer(){
peerConnection.createAnswer(new CustomSdpObserver(){
@Override
public void onCreateSuccess(SessionDescription sessionDescription) {
super.onCreateSuccess(sessionDescription);
peerConnection.setLocalDescription(new CustomSdpObserver(), sessionDescription);
// sessionDescription.description is string which needs to the shared across network