1 /*
2  * libjingle
3  * Copyright 2015 Google Inc.
4  *
5  * Redistribution and use in source and binary forms, with or without
6  * modification, are permitted provided that the following conditions are met:
7  *
8  *  1. Redistributions of source code must retain the above copyright notice,
9  *     this list of conditions and the following disclaimer.
10  *  2. Redistributions in binary form must reproduce the above copyright notice,
11  *     this list of conditions and the following disclaimer in the documentation
12  *     and/or other materials provided with the distribution.
13  *  3. The name of the author may not be used to endorse or promote products
14  *     derived from this software without specific prior written permission.
15  *
16  * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR IMPLIED
17  * WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF
18  * MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO
19  * EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
20  * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
21  * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS;
22  * OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY,
23  * WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR
24  * OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF
25  * ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
26  *
27  */
28 
29 #ifndef TALK_APP_WEBRTC_JAVA_JNI_ANDROIDVIDEOCAPTURER_JNI_H_
30 #define TALK_APP_WEBRTC_JAVA_JNI_ANDROIDVIDEOCAPTURER_JNI_H_
31 
32 #include <string>
33 
34 #include "talk/app/webrtc/androidvideocapturer.h"
35 #include "talk/app/webrtc/java/jni/jni_helpers.h"
36 #include "webrtc/base/asyncinvoker.h"
37 #include "webrtc/base/criticalsection.h"
38 #include "webrtc/base/thread_checker.h"
39 #include "webrtc/common_video/include/i420_buffer_pool.h"
40 
41 namespace webrtc_jni {
42 
43 struct NativeHandleImpl;
44 class SurfaceTextureHelper;
45 
46 // AndroidVideoCapturerJni implements AndroidVideoCapturerDelegate.
47 // The purpose of the delegate is to hide the JNI specifics from the C++ only
48 // AndroidVideoCapturer.
49 class AndroidVideoCapturerJni : public webrtc::AndroidVideoCapturerDelegate {
50  public:
51   static int SetAndroidObjects(JNIEnv* jni, jobject appliction_context);
52 
53   AndroidVideoCapturerJni(JNIEnv* jni,
54                           jobject j_video_capturer,
55                           jobject j_surface_texture_helper);
56 
57   void Start(int width, int height, int framerate,
58              webrtc::AndroidVideoCapturer* capturer) override;
59   void Stop() override;
60 
61   std::string GetSupportedFormats() override;
62 
63   // Called from VideoCapturerAndroid::NativeObserver on a Java thread.
64   void OnCapturerStarted(bool success);
65   void OnMemoryBufferFrame(void* video_frame, int length, int width,
66                            int height, int rotation, int64_t timestamp_ns);
67   void OnTextureFrame(int width, int height, int rotation, int64_t timestamp_ns,
68                       const NativeHandleImpl& handle);
69   void OnOutputFormatRequest(int width, int height, int fps);
70 
71  protected:
72   ~AndroidVideoCapturerJni();
73 
74  private:
75   JNIEnv* jni();
76 
77   // To avoid deducing Args from the 3rd parameter of AsyncCapturerInvoke.
78   template <typename T>
79   struct Identity {
80     typedef T type;
81   };
82 
83   // Helper function to make safe asynchronous calls to |capturer_|. The calls
84   // are not guaranteed to be delivered.
85   template <typename... Args>
86   void AsyncCapturerInvoke(
87       const char* method_name,
88       void (webrtc::AndroidVideoCapturer::*method)(Args...),
89       typename Identity<Args>::type... args);
90 
91   const ScopedGlobalRef<jobject> j_video_capturer_;
92   const ScopedGlobalRef<jclass> j_video_capturer_class_;
93   const ScopedGlobalRef<jclass> j_observer_class_;
94 
95   // Used on the Java thread running the camera.
96   webrtc::I420BufferPool buffer_pool_;
97   rtc::scoped_refptr<SurfaceTextureHelper> surface_texture_helper_;
98   rtc::ThreadChecker thread_checker_;
99 
100   // |capturer| is a guaranteed to be a valid pointer between a call to
101   // AndroidVideoCapturerDelegate::Start
102   // until AndroidVideoCapturerDelegate::Stop.
103   rtc::CriticalSection capturer_lock_;
104   webrtc::AndroidVideoCapturer* capturer_ GUARDED_BY(capturer_lock_);
105   // |invoker_| is used to communicate with |capturer_| on the thread Start() is
106   // called on.
107   rtc::scoped_ptr<rtc::GuardedAsyncInvoker> invoker_ GUARDED_BY(capturer_lock_);
108 
109   static jobject application_context_;
110 
111   RTC_DISALLOW_COPY_AND_ASSIGN(AndroidVideoCapturerJni);
112 };
113 
114 }  // namespace webrtc_jni
115 
116 #endif  // TALK_APP_WEBRTC_JAVA_JNI_ANDROIDVIDEOCAPTURER_JNI_H_
117