perkj@webrtc.org | 96e4db9 | 2015-02-13 12:46:51 +0000 | [diff] [blame] | 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 | |
Henrik Kjellander | 15583c1 | 2016-02-10 10:53:12 +0100 | [diff] [blame] | 29 | #ifndef WEBRTC_API_JAVA_JNI_ANDROIDVIDEOCAPTURER_JNI_H_ |
| 30 | #define WEBRTC_API_JAVA_JNI_ANDROIDVIDEOCAPTURER_JNI_H_ |
perkj@webrtc.org | 96e4db9 | 2015-02-13 12:46:51 +0000 | [diff] [blame] | 31 | |
| 32 | #include <string> |
| 33 | |
Henrik Kjellander | 15583c1 | 2016-02-10 10:53:12 +0100 | [diff] [blame] | 34 | #include "webrtc/api/androidvideocapturer.h" |
| 35 | #include "webrtc/api/java/jni/jni_helpers.h" |
perkj@webrtc.org | 112f127 | 2015-02-25 09:20:07 +0000 | [diff] [blame] | 36 | #include "webrtc/base/asyncinvoker.h" |
Magnus Jedvert | c464f50 | 2015-08-25 23:22:08 +0200 | [diff] [blame] | 37 | #include "webrtc/base/criticalsection.h" |
perkj@webrtc.org | 3db042e | 2015-02-19 08:43:38 +0000 | [diff] [blame] | 38 | #include "webrtc/base/thread_checker.h" |
perkj | 88518a2 | 2015-12-18 00:37:06 -0800 | [diff] [blame] | 39 | #include "webrtc/common_video/include/i420_buffer_pool.h" |
perkj@webrtc.org | 96e4db9 | 2015-02-13 12:46:51 +0000 | [diff] [blame] | 40 | |
| 41 | namespace webrtc_jni { |
| 42 | |
kjellander | 60ca31b | 2016-01-04 10:15:53 -0800 | [diff] [blame] | 43 | struct NativeHandleImpl; |
perkj | 88518a2 | 2015-12-18 00:37:06 -0800 | [diff] [blame] | 44 | class SurfaceTextureHelper; |
perkj | ac30642 | 2015-10-08 15:32:38 +0200 | [diff] [blame] | 45 | |
perkj@webrtc.org | 96e4db9 | 2015-02-13 12:46:51 +0000 | [diff] [blame] | 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); |
perkj@webrtc.org | 96e4db9 | 2015-02-13 12:46:51 +0000 | [diff] [blame] | 52 | |
nisse | c490e01 | 2015-12-10 06:23:33 -0800 | [diff] [blame] | 53 | AndroidVideoCapturerJni(JNIEnv* jni, |
| 54 | jobject j_video_capturer, |
| 55 | jobject j_surface_texture_helper); |
perkj@webrtc.org | 3db042e | 2015-02-19 08:43:38 +0000 | [diff] [blame] | 56 | |
perkj@webrtc.org | 96e4db9 | 2015-02-13 12:46:51 +0000 | [diff] [blame] | 57 | void Start(int width, int height, int framerate, |
| 58 | webrtc::AndroidVideoCapturer* capturer) override; |
perkj@webrtc.org | 3db042e | 2015-02-19 08:43:38 +0000 | [diff] [blame] | 59 | void Stop() override; |
perkj@webrtc.org | 96e4db9 | 2015-02-13 12:46:51 +0000 | [diff] [blame] | 60 | |
| 61 | std::string GetSupportedFormats() override; |
| 62 | |
perkj@webrtc.org | 112f127 | 2015-02-25 09:20:07 +0000 | [diff] [blame] | 63 | // Called from VideoCapturerAndroid::NativeObserver on a Java thread. |
| 64 | void OnCapturerStarted(bool success); |
perkj | ac30642 | 2015-10-08 15:32:38 +0200 | [diff] [blame] | 65 | void OnMemoryBufferFrame(void* video_frame, int length, int width, |
| 66 | int height, int rotation, int64_t timestamp_ns); |
Per | 71f5a9a | 2015-12-11 09:32:37 +0100 | [diff] [blame] | 67 | void OnTextureFrame(int width, int height, int rotation, int64_t timestamp_ns, |
Per | 488e75f | 2015-11-19 10:43:36 +0100 | [diff] [blame] | 68 | const NativeHandleImpl& handle); |
Åsa Persson | 2b67925 | 2015-06-15 09:53:05 +0200 | [diff] [blame] | 69 | void OnOutputFormatRequest(int width, int height, int fps); |
perkj | e0bce24 | 2015-10-05 16:21:54 +0200 | [diff] [blame] | 70 | |
| 71 | protected: |
Per | 3354419 | 2015-04-02 12:30:51 +0200 | [diff] [blame] | 72 | ~AndroidVideoCapturerJni(); |
perkj@webrtc.org | 112f127 | 2015-02-25 09:20:07 +0000 | [diff] [blame] | 73 | |
perkj | e0bce24 | 2015-10-05 16:21:54 +0200 | [diff] [blame] | 74 | private: |
perkj@webrtc.org | 96e4db9 | 2015-02-13 12:46:51 +0000 | [diff] [blame] | 75 | JNIEnv* jni(); |
| 76 | |
olka | 30a5b5e | 2015-10-20 11:04:56 -0700 | [diff] [blame] | 77 | // To avoid deducing Args from the 3rd parameter of AsyncCapturerInvoke. |
| 78 | template <typename T> |
| 79 | struct Identity { |
| 80 | typedef T type; |
| 81 | }; |
| 82 | |
Magnus Jedvert | c464f50 | 2015-08-25 23:22:08 +0200 | [diff] [blame] | 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...), |
olka | 30a5b5e | 2015-10-20 11:04:56 -0700 | [diff] [blame] | 89 | typename Identity<Args>::type... args); |
Magnus Jedvert | c464f50 | 2015-08-25 23:22:08 +0200 | [diff] [blame] | 90 | |
nisse | c490e01 | 2015-12-10 06:23:33 -0800 | [diff] [blame] | 91 | const ScopedGlobalRef<jobject> j_video_capturer_; |
perkj@webrtc.org | 96e4db9 | 2015-02-13 12:46:51 +0000 | [diff] [blame] | 92 | const ScopedGlobalRef<jclass> j_video_capturer_class_; |
perkj@webrtc.org | 3db042e | 2015-02-19 08:43:38 +0000 | [diff] [blame] | 93 | const ScopedGlobalRef<jclass> j_observer_class_; |
perkj@webrtc.org | 96e4db9 | 2015-02-13 12:46:51 +0000 | [diff] [blame] | 94 | |
perkj | 88518a2 | 2015-12-18 00:37:06 -0800 | [diff] [blame] | 95 | // Used on the Java thread running the camera. |
| 96 | webrtc::I420BufferPool buffer_pool_; |
| 97 | rtc::scoped_refptr<SurfaceTextureHelper> surface_texture_helper_; |
perkj@webrtc.org | 3db042e | 2015-02-19 08:43:38 +0000 | [diff] [blame] | 98 | rtc::ThreadChecker thread_checker_; |
| 99 | |
perkj@webrtc.org | 112f127 | 2015-02-25 09:20:07 +0000 | [diff] [blame] | 100 | // |capturer| is a guaranteed to be a valid pointer between a call to |
| 101 | // AndroidVideoCapturerDelegate::Start |
| 102 | // until AndroidVideoCapturerDelegate::Stop. |
Magnus Jedvert | c464f50 | 2015-08-25 23:22:08 +0200 | [diff] [blame] | 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_); |
perkj@webrtc.org | 3db042e | 2015-02-19 08:43:38 +0000 | [diff] [blame] | 108 | |
perkj@webrtc.org | 96e4db9 | 2015-02-13 12:46:51 +0000 | [diff] [blame] | 109 | static jobject application_context_; |
| 110 | |
henrikg | 3c089d7 | 2015-09-16 05:37:44 -0700 | [diff] [blame] | 111 | RTC_DISALLOW_COPY_AND_ASSIGN(AndroidVideoCapturerJni); |
perkj@webrtc.org | 96e4db9 | 2015-02-13 12:46:51 +0000 | [diff] [blame] | 112 | }; |
| 113 | |
| 114 | } // namespace webrtc_jni |
| 115 | |
Henrik Kjellander | 15583c1 | 2016-02-10 10:53:12 +0100 | [diff] [blame] | 116 | #endif // WEBRTC_API_JAVA_JNI_ANDROIDVIDEOCAPTURER_JNI_H_ |