Skip to content

Instantly share code, notes, and snippets.

@vivek1794
vivek1794 / WebRtcActivity.java
Last active April 8, 2020 18:45
A sample webrtc intro activity
package xyz.vivekc.webrtccodelab;
import android.os.Bundle;
import android.support.v7.app.AppCompatActivity;
import android.util.Log;
import org.webrtc.AudioSource;
import org.webrtc.AudioTrack;
import org.webrtc.Camera1Enumerator;
import org.webrtc.Camera2Enumerator;
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!
@vivek1794
vivek1794 / CreateVideoCapturer.java
Created May 1, 2017 12:58
Method to create and return a video capturer for WebRTC using Camera 1 API
private VideoCapturer createVideoCapturer() {
VideoCapturer videoCapturer;
videoCapturer = createCameraCapturer(new Camera1Enumerator(false));
return videoCapturer;
}
private VideoCapturer createCameraCapturer(CameraEnumerator enumerator) {
final String[] deviceNames = enumerator.getDeviceNames();
// Trying to find a front facing camera!
'use strict';
var os = require('os');
var nodeStatic = require('node-static');
var https = require('https');
var socketIO = require('socket.io');
var fs = require("fs");
var options = {
key: fs.readFileSync('key.pem'),
cert: fs.readFileSync('cert.pem')
public class MainActivity extends AppCompatActivity implements View.OnClickListener, SignallingClient.SignalingInterface {
PeerConnectionFactory peerConnectionFactory;
MediaConstraints audioConstraints;
MediaConstraints videoConstraints;
MediaConstraints sdpConstraints;
VideoSource videoSource;
VideoTrack localVideoTrack;
AudioSource audioSource;
AudioTrack localAudioTrack;
class SignallingClient {
private static SignallingClient instance;
private String roomName = null;
private Socket socket;
boolean isChannelReady = false;
boolean isInitiator = false;
boolean isStarted = false;
private SignalingInterface callback;
//This piece of code should not go into production!!
class MainActivityKotlin : AppCompatActivity(), View.OnClickListener, SignallingClientKotlin.SignalingInterface {
private val rootEglBase by lazy { EglBase.create() }
private val peerConnectionFactory: PeerConnectionFactory by lazy {
//Initialize PeerConnectionFactory globals.
val initializationOptions = PeerConnectionFactory.InitializationOptions.builder(this)
.setEnableVideoHwAcceleration(true)
.createInitializationOptions()
PeerConnectionFactory.initialize(initializationOptions)
internal class SignallingClientKotlin {
internal interface SignalingInterface {
fun onRemoteHangUp(msg: String)
fun onOfferReceived(data: JSONObject)
fun onAnswerReceived(data: JSONObject)
apply plugin: 'kotlin-multiplatform'
kotlin {
targets {
//create a target for Android from presets.jvm
fromPreset(presets.jvm, 'android')
//Xcode sets SDK_NAME environment variable - based on whether the
//target device is a simulator or a real device, the preset should vary
package xyz.vivekc.shared
//Mark the function dependent on platform implementation to be `expect`
expect fun getCurrentDate(): String
//This is the function which would be called by the Android or iOS app
fun getDate(): String {
return "Today's Date is ${getCurrentDate()}"
}