diff options
Diffstat (limited to 'third_party/libwebrtc/examples/androidnativeapi/java')
2 files changed, 192 insertions, 0 deletions
diff --git a/third_party/libwebrtc/examples/androidnativeapi/java/org/webrtc/examples/androidnativeapi/CallClient.java b/third_party/libwebrtc/examples/androidnativeapi/java/org/webrtc/examples/androidnativeapi/CallClient.java new file mode 100644 index 0000000000..7369a1286d --- /dev/null +++ b/third_party/libwebrtc/examples/androidnativeapi/java/org/webrtc/examples/androidnativeapi/CallClient.java @@ -0,0 +1,72 @@ +/* + * Copyright (c) 2018 The WebRTC project authors. All Rights Reserved. + * + * Use of this source code is governed by a BSD-style license + * that can be found in the LICENSE file in the root of the source + * tree. An additional intellectual property rights grant can be found + * in the file PATENTS. All contributing project authors may + * be found in the AUTHORS file in the root of the source tree. + */ + +package org.webrtc.examples.androidnativeapi; + +import android.content.Context; +import android.os.Handler; +import android.os.HandlerThread; +import org.webrtc.CapturerObserver; +import org.webrtc.SurfaceTextureHelper; +import org.webrtc.VideoCapturer; +import org.webrtc.VideoSink; + +public class CallClient { + private static final String TAG = "CallClient"; + private static final int CAPTURE_WIDTH = 640; + private static final int CAPTURE_HEIGHT = 480; + private static final int CAPTURE_FPS = 30; + + private final Context applicationContext; + private final HandlerThread thread; + private final Handler handler; + + private long nativeClient; + private SurfaceTextureHelper surfaceTextureHelper; + private VideoCapturer videoCapturer; + + public CallClient(Context applicationContext) { + this.applicationContext = applicationContext; + thread = new HandlerThread(TAG + "Thread"); + thread.start(); + handler = new Handler(thread.getLooper()); + handler.post(() -> { nativeClient = nativeCreateClient(); }); + } + + public void call(VideoSink localSink, VideoSink remoteSink, VideoCapturer videoCapturer, + SurfaceTextureHelper videoCapturerSurfaceTextureHelper) { + handler.post(() -> { + nativeCall(nativeClient, localSink, remoteSink); + videoCapturer.initialize(videoCapturerSurfaceTextureHelper, applicationContext, + nativeGetJavaVideoCapturerObserver(nativeClient)); + videoCapturer.startCapture(CAPTURE_WIDTH, CAPTURE_HEIGHT, CAPTURE_FPS); + }); + } + + public void hangup() { + handler.post(() -> { nativeHangup(nativeClient); }); + } + + public void close() { + handler.post(() -> { + nativeDelete(nativeClient); + nativeClient = 0; + }); + thread.quitSafely(); + } + + private static native long nativeCreateClient(); + private static native void nativeCall( + long nativeAndroidCallClient, VideoSink localSink, VideoSink remoteSink); + private static native void nativeHangup(long nativeAndroidCallClient); + private static native void nativeDelete(long nativeAndroidCallClient); + private static native CapturerObserver nativeGetJavaVideoCapturerObserver( + long nativeAndroidCallClient); +} diff --git a/third_party/libwebrtc/examples/androidnativeapi/java/org/webrtc/examples/androidnativeapi/MainActivity.java b/third_party/libwebrtc/examples/androidnativeapi/java/org/webrtc/examples/androidnativeapi/MainActivity.java new file mode 100644 index 0000000000..72fc0a686d --- /dev/null +++ b/third_party/libwebrtc/examples/androidnativeapi/java/org/webrtc/examples/androidnativeapi/MainActivity.java @@ -0,0 +1,120 @@ +/* + * Copyright (c) 2018 The WebRTC project authors. All Rights Reserved. + * + * Use of this source code is governed by a BSD-style license + * that can be found in the LICENSE file in the root of the source + * tree. An additional intellectual property rights grant can be found + * in the file PATENTS. All contributing project authors may + * be found in the AUTHORS file in the root of the source tree. + */ + +package org.webrtc.examples.androidnativeapi; + +import android.app.Activity; +import android.content.Context; +import android.os.Bundle; +import android.widget.Button; +import androidx.annotation.Nullable; +import org.webrtc.Camera1Enumerator; +import org.webrtc.Camera2Enumerator; +import org.webrtc.CameraEnumerator; +import org.webrtc.ContextUtils; +import org.webrtc.EglBase; +import org.webrtc.GlRectDrawer; +import org.webrtc.SurfaceTextureHelper; +import org.webrtc.SurfaceViewRenderer; +import org.webrtc.VideoCapturer; + +public class MainActivity extends Activity { + private @Nullable CallClient callClient; + private @Nullable EglBase eglBase; + private @Nullable SurfaceViewRenderer localRenderer; + private @Nullable SurfaceViewRenderer remoteRenderer; + private @Nullable SurfaceTextureHelper videoCapturerSurfaceTextureHelper; + private @Nullable VideoCapturer videoCapturer; + + @Override + protected void onCreate(Bundle savedInstance) { + ContextUtils.initialize(getApplicationContext()); + + super.onCreate(savedInstance); + setContentView(R.layout.activity_main); + + System.loadLibrary("examples_androidnativeapi_jni"); + callClient = new CallClient(getApplicationContext()); + + Button callButton = (Button) findViewById(R.id.call_button); + callButton.setOnClickListener((view) -> { + if (videoCapturer == null) { + videoCapturer = createVideoCapturer(getApplicationContext()); + } + callClient.call( + localRenderer, remoteRenderer, videoCapturer, videoCapturerSurfaceTextureHelper); + }); + + Button hangupButton = (Button) findViewById(R.id.hangup_button); + hangupButton.setOnClickListener((view) -> { hangup(); }); + } + + @Override + protected void onStart() { + super.onStart(); + + eglBase = EglBase.create(null /* sharedContext */, EglBase.CONFIG_PLAIN); + localRenderer = (SurfaceViewRenderer) findViewById(R.id.local_renderer); + remoteRenderer = (SurfaceViewRenderer) findViewById(R.id.remote_renderer); + + localRenderer.init(eglBase.getEglBaseContext(), null /* rendererEvents */, EglBase.CONFIG_PLAIN, + new GlRectDrawer()); + remoteRenderer.init(eglBase.getEglBaseContext(), null /* rendererEvents */, + EglBase.CONFIG_PLAIN, new GlRectDrawer()); + + videoCapturerSurfaceTextureHelper = + SurfaceTextureHelper.create("VideoCapturerThread", eglBase.getEglBaseContext()); + } + + @Override + protected void onStop() { + hangup(); + + localRenderer.release(); + remoteRenderer.release(); + videoCapturerSurfaceTextureHelper.dispose(); + eglBase.release(); + + localRenderer = null; + remoteRenderer = null; + videoCapturerSurfaceTextureHelper = null; + eglBase = null; + + super.onStop(); + } + + @Override + protected void onDestroy() { + callClient.close(); + callClient = null; + + super.onDestroy(); + } + + private void hangup() { + if (videoCapturer != null) { + try { + videoCapturer.stopCapture(); + } catch (InterruptedException e) { + throw new RuntimeException(e); + } + videoCapturer.dispose(); + videoCapturer = null; + } + callClient.hangup(); + } + + private static VideoCapturer createVideoCapturer(Context context) { + CameraEnumerator enumerator = Camera2Enumerator.isSupported(context) + ? new Camera2Enumerator(context) + : new Camera1Enumerator(); + return enumerator.createCapturer(enumerator.getDeviceNames()[0], null /* eventsHandler */); + } +} |