Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- 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;
- import org.webrtc.CameraEnumerator;
- import org.webrtc.CameraVideoCapturer;
- import org.webrtc.EglBase;
- import org.webrtc.Logging;
- import org.webrtc.MediaConstraints;
- import org.webrtc.PeerConnectionFactory;
- import org.webrtc.SurfaceViewRenderer;
- import org.webrtc.VideoCapturer;
- import org.webrtc.VideoCapturerAndroid;
- import org.webrtc.VideoRenderer;
- import org.webrtc.VideoSource;
- import org.webrtc.VideoTrack;
- public class MainActivity extends AppCompatActivity {
- private static final String TAG = "MainActivity";
- @Override
- protected void onCreate(Bundle savedInstanceState) {
- super.onCreate(savedInstanceState);
- setContentView(R.layout.activity_main);
- //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 peerConnectionFactory = new PeerConnectionFactory(options);
- //Now create a VideoCapturer instance. Callback methods are there if you want to do something! Duh!
- VideoCapturer videoCapturerAndroid = createVideoCapturer();
- //Create MediaConstraints - Will be useful for specifying video and audio constraints. More on this later!
- MediaConstraints constraints = new MediaConstraints();
- //Create a VideoSource instance
- VideoSource videoSource = peerConnectionFactory.createVideoSource(videoCapturerAndroid);
- VideoTrack localVideoTrack = peerConnectionFactory.createVideoTrack("100", videoSource);
- //create an AudioSource instance
- AudioSource audioSource = peerConnectionFactory.createAudioSource(constraints);
- AudioTrack localAudioTrack = peerConnectionFactory.createAudioTrack("101", audioSource);
- //we will start capturing the video from the camera
- //params are width,height and fps
- videoCapturerAndroid.startCapture(1000, 1000, 30);
- //create surface renderer, init it and add the renderer to the track
- SurfaceViewRenderer videoView = (SurfaceViewRenderer) findViewById(R.id.surface_rendeer);
- videoView.setMirror(true);
- EglBase rootEglBase = EglBase.create();
- videoView.init(rootEglBase.getEglBaseContext(), null);
- localVideoTrack.addRenderer(new VideoRenderer(videoView));
- }
- 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!
- for (String deviceName : deviceNames) {
- if (enumerator.isFrontFacing(deviceName)) {
- VideoCapturer videoCapturer = enumerator.createCapturer(deviceName, null);
- if (videoCapturer != null) {
- return videoCapturer;
- }
- }
- }
- // We were not able to find a front cam. Look for other cameras
- for (String deviceName : deviceNames) {
- if (!enumerator.isFrontFacing(deviceName)) {
- VideoCapturer videoCapturer = enumerator.createCapturer(deviceName, null);
- if (videoCapturer != null) {
- return videoCapturer;
- }
- }
- }
- return null;
- }
- }
Add Comment
Please, Sign In to add comment