Guest User

Untitled

a guest
May 24th, 2018
79
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 3.83 KB | None | 0 0
  1. package xyz.vivekc.webrtccodelab;
  2.  
  3. import android.os.Bundle;
  4. import android.support.v7.app.AppCompatActivity;
  5. import android.util.Log;
  6.  
  7. import org.webrtc.AudioSource;
  8. import org.webrtc.AudioTrack;
  9. import org.webrtc.Camera1Enumerator;
  10. import org.webrtc.Camera2Enumerator;
  11. import org.webrtc.CameraEnumerator;
  12. import org.webrtc.CameraVideoCapturer;
  13. import org.webrtc.EglBase;
  14. import org.webrtc.Logging;
  15. import org.webrtc.MediaConstraints;
  16. import org.webrtc.PeerConnectionFactory;
  17. import org.webrtc.SurfaceViewRenderer;
  18. import org.webrtc.VideoCapturer;
  19. import org.webrtc.VideoCapturerAndroid;
  20. import org.webrtc.VideoRenderer;
  21. import org.webrtc.VideoSource;
  22. import org.webrtc.VideoTrack;
  23.  
  24. public class MainActivity extends AppCompatActivity {
  25. private static final String TAG = "MainActivity";
  26.  
  27. @Override
  28. protected void onCreate(Bundle savedInstanceState) {
  29. super.onCreate(savedInstanceState);
  30. setContentView(R.layout.activity_main);
  31.  
  32. //Initialize PeerConnectionFactory globals.
  33. //Params are context, initAudio,initVideo and videoCodecHwAcceleration
  34. PeerConnectionFactory.initializeAndroidGlobals(this, true, true, true);
  35.  
  36. //Create a new PeerConnectionFactory instance.
  37. PeerConnectionFactory.Options options = new PeerConnectionFactory.Options();
  38. PeerConnectionFactory peerConnectionFactory = new PeerConnectionFactory(options);
  39.  
  40.  
  41. //Now create a VideoCapturer instance. Callback methods are there if you want to do something! Duh!
  42. VideoCapturer videoCapturerAndroid = createVideoCapturer();
  43. //Create MediaConstraints - Will be useful for specifying video and audio constraints. More on this later!
  44. MediaConstraints constraints = new MediaConstraints();
  45.  
  46. //Create a VideoSource instance
  47. VideoSource videoSource = peerConnectionFactory.createVideoSource(videoCapturerAndroid);
  48. VideoTrack localVideoTrack = peerConnectionFactory.createVideoTrack("100", videoSource);
  49.  
  50. //create an AudioSource instance
  51. AudioSource audioSource = peerConnectionFactory.createAudioSource(constraints);
  52. AudioTrack localAudioTrack = peerConnectionFactory.createAudioTrack("101", audioSource);
  53.  
  54. //we will start capturing the video from the camera
  55. //params are width,height and fps
  56. videoCapturerAndroid.startCapture(1000, 1000, 30);
  57.  
  58. //create surface renderer, init it and add the renderer to the track
  59. SurfaceViewRenderer videoView = (SurfaceViewRenderer) findViewById(R.id.surface_rendeer);
  60. videoView.setMirror(true);
  61.  
  62. EglBase rootEglBase = EglBase.create();
  63. videoView.init(rootEglBase.getEglBaseContext(), null);
  64.  
  65. localVideoTrack.addRenderer(new VideoRenderer(videoView));
  66. }
  67.  
  68.  
  69. private VideoCapturer createVideoCapturer() {
  70. VideoCapturer videoCapturer;
  71. videoCapturer = createCameraCapturer(new Camera1Enumerator(false));
  72. return videoCapturer;
  73. }
  74.  
  75. private VideoCapturer createCameraCapturer(CameraEnumerator enumerator) {
  76. final String[] deviceNames = enumerator.getDeviceNames();
  77.  
  78. // Trying to find a front facing camera!
  79. for (String deviceName : deviceNames) {
  80. if (enumerator.isFrontFacing(deviceName)) {
  81. VideoCapturer videoCapturer = enumerator.createCapturer(deviceName, null);
  82.  
  83. if (videoCapturer != null) {
  84. return videoCapturer;
  85. }
  86. }
  87. }
  88.  
  89. // We were not able to find a front cam. Look for other cameras
  90. for (String deviceName : deviceNames) {
  91. if (!enumerator.isFrontFacing(deviceName)) {
  92. VideoCapturer videoCapturer = enumerator.createCapturer(deviceName, null);
  93. if (videoCapturer != null) {
  94. return videoCapturer;
  95. }
  96. }
  97. }
  98.  
  99. return null;
  100. }
  101. }
Add Comment
Please, Sign In to add comment