blob: 849c66b5dd2ca6e418d47b82c376ffd392fbbe47 [file] [log] [blame]
Per3e9eb4b2015-09-28 10:52:22 +02001/*
kjellanderb24317b2016-02-10 07:54:43 -08002 * Copyright 2015 The WebRTC project authors. All Rights Reserved.
Per3e9eb4b2015-09-28 10:52:22 +02003 *
kjellanderb24317b2016-02-10 07:54:43 -08004 * 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.
Per3e9eb4b2015-09-28 10:52:22 +02009 */
10
Henrik Kjellander15583c12016-02-10 10:53:12 +010011#ifndef WEBRTC_API_JAVA_JNI_SURFACETEXTUREHELPER_JNI_H_
12#define WEBRTC_API_JAVA_JNI_SURFACETEXTUREHELPER_JNI_H_
Per3e9eb4b2015-09-28 10:52:22 +020013
14#include <jni.h>
15
Henrik Kjellander15583c12016-02-10 10:53:12 +010016#include "webrtc/api/java/jni/jni_helpers.h"
17#include "webrtc/api/java/jni/native_handle_impl.h"
Per3e9eb4b2015-09-28 10:52:22 +020018#include "webrtc/base/refcount.h"
19#include "webrtc/base/scoped_ref_ptr.h"
kjellander6f8ce062015-11-16 13:52:24 -080020#include "webrtc/common_video/include/video_frame_buffer.h"
Per3e9eb4b2015-09-28 10:52:22 +020021
22namespace webrtc_jni {
23
24// Helper class to create and synchronize access to an Android SurfaceTexture.
25// It is used for creating webrtc::VideoFrameBuffers from a SurfaceTexture when
26// the SurfaceTexture has been updated.
27// When the VideoFrameBuffer is released, this class returns the buffer to the
28// java SurfaceTextureHelper so it can be updated safely. The VideoFrameBuffer
29// can be released on an arbitrary thread.
30// SurfaceTextureHelper is reference counted to make sure that it is not
31// destroyed while a VideoFrameBuffer is in use.
32// This class is the C++ counterpart of the java class SurfaceTextureHelper.
33// Usage:
magjed0dc23162016-03-14 03:59:38 -070034// 1. Create an instance of this class.
35// 2. Get the Java SurfaceTextureHelper with GetJavaSurfaceTextureHelper().
Per3e9eb4b2015-09-28 10:52:22 +020036// 3. Register a listener to the Java SurfaceListener and start producing
37// new buffers.
perkj88518a22015-12-18 00:37:06 -080038// 4. Call CreateTextureFrame to wrap the Java texture in a VideoFrameBuffer.
Per3e9eb4b2015-09-28 10:52:22 +020039class SurfaceTextureHelper : public rtc::RefCountInterface {
40 public:
magjed82b750b2016-03-31 00:54:15 -070041 SurfaceTextureHelper(JNIEnv* jni,
42 const char* thread_name,
43 jobject j_egl_context);
magjed0dc23162016-03-14 03:59:38 -070044
45 jobject GetJavaSurfaceTextureHelper() const;
Per3e9eb4b2015-09-28 10:52:22 +020046
47 rtc::scoped_refptr<webrtc::VideoFrameBuffer> CreateTextureFrame(
48 int width,
49 int height,
Per488e75f2015-11-19 10:43:36 +010050 const NativeHandleImpl& native_handle);
Per3e9eb4b2015-09-28 10:52:22 +020051
52 protected:
53 ~SurfaceTextureHelper();
54
55 private:
Per3e9eb4b2015-09-28 10:52:22 +020056 // May be called on arbitrary thread.
57 void ReturnTextureFrame() const;
58
Per3e9eb4b2015-09-28 10:52:22 +020059 const ScopedGlobalRef<jobject> j_surface_texture_helper_;
60 const jmethodID j_return_texture_method_;
61};
62
63} // namespace webrtc_jni
64
Henrik Kjellander15583c12016-02-10 10:53:12 +010065#endif // WEBRTC_API_JAVA_JNI_SURFACETEXTUREHELPER_JNI_H_