1 /*
2  *  Copyright 2015 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 #ifndef WEBRTC_SDK_ANDROID_SRC_JNI_SURFACETEXTUREHELPER_JNI_H_
12 #define WEBRTC_SDK_ANDROID_SRC_JNI_SURFACETEXTUREHELPER_JNI_H_
13 
14 #include <jni.h>
15 
16 #include "webrtc/sdk/android/src/jni/jni_helpers.h"
17 #include "webrtc/sdk/android/src/jni/native_handle_impl.h"
18 #include "webrtc/api/video/video_frame_buffer.h"
19 #include "webrtc/base/refcount.h"
20 #include "webrtc/base/scoped_ref_ptr.h"
21 
22 namespace 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 // It owns the corresponding java object, and calls the java dispose
34 // method when destroyed.
35 // Usage:
36 // 1. Create an instance of this class.
37 // 2. Get the Java SurfaceTextureHelper with GetJavaSurfaceTextureHelper().
38 // 3. Register a listener to the Java SurfaceListener and start producing
39 // new buffers.
40 // 4. Call CreateTextureFrame to wrap the Java texture in a VideoFrameBuffer.
41 class SurfaceTextureHelper : public rtc::RefCountInterface {
42  public:
43   // Might return null if creating the Java SurfaceTextureHelper fails.
44   static rtc::scoped_refptr<SurfaceTextureHelper> create(
45       JNIEnv* jni, const char* thread_name, jobject j_egl_context);
46 
47   jobject GetJavaSurfaceTextureHelper() const;
48 
49   rtc::scoped_refptr<webrtc::VideoFrameBuffer> CreateTextureFrame(
50       int width,
51       int height,
52       const NativeHandleImpl& native_handle);
53 
54   // May be called on arbitrary thread.
55   void ReturnTextureFrame() const;
56 
57  protected:
58   ~SurfaceTextureHelper();
59   SurfaceTextureHelper(JNIEnv* jni, jobject j_surface_texture_helper);
60 
61  private:
62   const ScopedGlobalRef<jobject> j_surface_texture_helper_;
63   const jmethodID j_return_texture_method_;
64 };
65 
66 }  // namespace webrtc_jni
67 
68 #endif  // WEBRTC_SDK_ANDROID_SRC_JNI_SURFACETEXTUREHELPER_JNI_H_
69