• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
2  *  Copyright (c) 2018 The WebRTC project authors. All Rights Reserved.
3  *
4  *  Use of this source code is governed by a BSD-style license
5  *  that can be found in the LICENSE file in the root of the source
6  *  tree. An additional intellectual property rights grant can be found
7  *  in the file PATENTS.  All contributing project authors may
8  *  be found in the AUTHORS file in the root of the source tree.
9  */
10 
11 package org.webrtc.examples.androidnativeapi;
12 
13 import android.app.Activity;
14 import android.content.Context;
15 import android.os.Bundle;
16 import android.support.annotation.Nullable;
17 import android.widget.Button;
18 import org.webrtc.Camera1Enumerator;
19 import org.webrtc.Camera2Enumerator;
20 import org.webrtc.CameraEnumerator;
21 import org.webrtc.ContextUtils;
22 import org.webrtc.EglBase;
23 import org.webrtc.GlRectDrawer;
24 import org.webrtc.SurfaceTextureHelper;
25 import org.webrtc.SurfaceViewRenderer;
26 import org.webrtc.VideoCapturer;
27 
28 public class MainActivity extends Activity {
29   private @Nullable CallClient callClient;
30   private @Nullable EglBase eglBase;
31   private @Nullable SurfaceViewRenderer localRenderer;
32   private @Nullable SurfaceViewRenderer remoteRenderer;
33   private @Nullable SurfaceTextureHelper videoCapturerSurfaceTextureHelper;
34   private @Nullable VideoCapturer videoCapturer;
35 
36   @Override
onCreate(Bundle savedInstance)37   protected void onCreate(Bundle savedInstance) {
38     ContextUtils.initialize(getApplicationContext());
39 
40     super.onCreate(savedInstance);
41     setContentView(R.layout.activity_main);
42 
43     System.loadLibrary("examples_androidnativeapi_jni");
44     callClient = new CallClient(getApplicationContext());
45 
46     Button callButton = (Button) findViewById(R.id.call_button);
47     callButton.setOnClickListener((view) -> {
48       if (videoCapturer == null) {
49         videoCapturer = createVideoCapturer(getApplicationContext());
50       }
51       callClient.call(
52           localRenderer, remoteRenderer, videoCapturer, videoCapturerSurfaceTextureHelper);
53     });
54 
55     Button hangupButton = (Button) findViewById(R.id.hangup_button);
56     hangupButton.setOnClickListener((view) -> { hangup(); });
57   }
58 
59   @Override
onStart()60   protected void onStart() {
61     super.onStart();
62 
63     eglBase = EglBase.create(null /* sharedContext */, EglBase.CONFIG_PLAIN);
64     localRenderer = (SurfaceViewRenderer) findViewById(R.id.local_renderer);
65     remoteRenderer = (SurfaceViewRenderer) findViewById(R.id.remote_renderer);
66 
67     localRenderer.init(eglBase.getEglBaseContext(), null /* rendererEvents */, EglBase.CONFIG_PLAIN,
68         new GlRectDrawer());
69     remoteRenderer.init(eglBase.getEglBaseContext(), null /* rendererEvents */,
70         EglBase.CONFIG_PLAIN, new GlRectDrawer());
71 
72     videoCapturerSurfaceTextureHelper =
73         SurfaceTextureHelper.create("VideoCapturerThread", eglBase.getEglBaseContext());
74   }
75 
76   @Override
onStop()77   protected void onStop() {
78     hangup();
79 
80     localRenderer.release();
81     remoteRenderer.release();
82     videoCapturerSurfaceTextureHelper.dispose();
83     eglBase.release();
84 
85     localRenderer = null;
86     remoteRenderer = null;
87     videoCapturerSurfaceTextureHelper = null;
88     eglBase = null;
89 
90     super.onStop();
91   }
92 
93   @Override
onDestroy()94   protected void onDestroy() {
95     callClient.close();
96     callClient = null;
97 
98     super.onDestroy();
99   }
100 
hangup()101   private void hangup() {
102     if (videoCapturer != null) {
103       try {
104         videoCapturer.stopCapture();
105       } catch (InterruptedException e) {
106         throw new RuntimeException(e);
107       }
108       videoCapturer.dispose();
109       videoCapturer = null;
110     }
111     callClient.hangup();
112   }
113 
createVideoCapturer(Context context)114   private static VideoCapturer createVideoCapturer(Context context) {
115     CameraEnumerator enumerator = Camera2Enumerator.isSupported(context)
116         ? new Camera2Enumerator(context)
117         : new Camera1Enumerator();
118     return enumerator.createCapturer(enumerator.getDeviceNames()[0], null /* eventsHandler */);
119   }
120 }
121