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 | |
| 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" |
perkj@webrtc.org | 112f127 | 2015-02-25 09:20:07 +0000 | [diff] [blame] | 36 | #include "webrtc/base/asyncinvoker.h" |
perkj@webrtc.org | 3db042e | 2015-02-19 08:43:38 +0000 | [diff] [blame] | 37 | #include "webrtc/base/thread_checker.h" |
perkj@webrtc.org | 96e4db9 | 2015-02-13 12:46:51 +0000 | [diff] [blame] | 38 | |
| 39 | namespace webrtc_jni { |
| 40 | |
| 41 | // AndroidVideoCapturerJni implements AndroidVideoCapturerDelegate. |
| 42 | // The purpose of the delegate is to hide the JNI specifics from the C++ only |
| 43 | // AndroidVideoCapturer. |
| 44 | class AndroidVideoCapturerJni : public webrtc::AndroidVideoCapturerDelegate { |
| 45 | public: |
| 46 | static int SetAndroidObjects(JNIEnv* jni, jobject appliction_context); |
perkj@webrtc.org | 96e4db9 | 2015-02-13 12:46:51 +0000 | [diff] [blame] | 47 | |
perkj@webrtc.org | 112f127 | 2015-02-25 09:20:07 +0000 | [diff] [blame] | 48 | // Creates a new instance of AndroidVideoCapturerJni. Returns a nullptr if |
| 49 | // it can't be created. This happens if |device_name| is invalid. |
Per | 3354419 | 2015-04-02 12:30:51 +0200 | [diff] [blame] | 50 | static rtc::scoped_refptr<AndroidVideoCapturerJni> Create( |
perkj@webrtc.org | 112f127 | 2015-02-25 09:20:07 +0000 | [diff] [blame] | 51 | JNIEnv* jni, |
| 52 | jobject j_video_capture, // Instance of VideoCapturerAndroid |
| 53 | jstring device_name); // Name of the camera to use. |
perkj@webrtc.org | 3db042e | 2015-02-19 08:43:38 +0000 | [diff] [blame] | 54 | |
perkj@webrtc.org | 96e4db9 | 2015-02-13 12:46:51 +0000 | [diff] [blame] | 55 | void Start(int width, int height, int framerate, |
| 56 | webrtc::AndroidVideoCapturer* capturer) override; |
perkj@webrtc.org | 3db042e | 2015-02-19 08:43:38 +0000 | [diff] [blame] | 57 | void Stop() override; |
perkj@webrtc.org | 96e4db9 | 2015-02-13 12:46:51 +0000 | [diff] [blame] | 58 | |
kjellander@webrtc.org | 14665ff | 2015-03-04 12:58:35 +0000 | [diff] [blame] | 59 | void ReturnBuffer(int64 time_stamp) override; |
perkj@webrtc.org | 112f127 | 2015-02-25 09:20:07 +0000 | [diff] [blame] | 60 | |
perkj@webrtc.org | 96e4db9 | 2015-02-13 12:46:51 +0000 | [diff] [blame] | 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); |
| 65 | void OnIncomingFrame(void* video_frame, |
| 66 | int length, |
magjed | b69ab79 | 2015-07-22 02:32:00 -0700 | [diff] [blame^] | 67 | int width, |
| 68 | int height, |
perkj@webrtc.org | 112f127 | 2015-02-25 09:20:07 +0000 | [diff] [blame] | 69 | int rotation, |
| 70 | int64 time_stamp); |
Åsa Persson | 2b67925 | 2015-06-15 09:53:05 +0200 | [diff] [blame] | 71 | void OnOutputFormatRequest(int width, int height, int fps); |
Per | 3354419 | 2015-04-02 12:30:51 +0200 | [diff] [blame] | 72 | protected: |
| 73 | AndroidVideoCapturerJni(JNIEnv* jni, jobject j_video_capturer); |
| 74 | ~AndroidVideoCapturerJni(); |
perkj@webrtc.org | 112f127 | 2015-02-25 09:20:07 +0000 | [diff] [blame] | 75 | |
| 76 | private: |
perkj@webrtc.org | 112f127 | 2015-02-25 09:20:07 +0000 | [diff] [blame] | 77 | bool Init(jstring device_name); |
| 78 | |
| 79 | void OnCapturerStarted_w(bool success); |
| 80 | void OnCapturerStopped_w(); |
| 81 | void OnIncomingFrame_w(void* video_frame, |
| 82 | int length, |
magjed | b69ab79 | 2015-07-22 02:32:00 -0700 | [diff] [blame^] | 83 | int width, |
| 84 | int height, |
perkj@webrtc.org | 112f127 | 2015-02-25 09:20:07 +0000 | [diff] [blame] | 85 | int rotation, |
| 86 | int64 time_stamp); |
Åsa Persson | 2b67925 | 2015-06-15 09:53:05 +0200 | [diff] [blame] | 87 | void OnOutputFormatRequest_w(int width, int height, int fps); |
Per | 3354419 | 2015-04-02 12:30:51 +0200 | [diff] [blame] | 88 | void ReturnBuffer_w(int64 time_stamp); |
perkj@webrtc.org | 112f127 | 2015-02-25 09:20:07 +0000 | [diff] [blame] | 89 | |
perkj@webrtc.org | 96e4db9 | 2015-02-13 12:46:51 +0000 | [diff] [blame] | 90 | JNIEnv* jni(); |
| 91 | |
| 92 | const ScopedGlobalRef<jobject> j_capturer_global_; |
| 93 | const ScopedGlobalRef<jclass> j_video_capturer_class_; |
perkj@webrtc.org | 3db042e | 2015-02-19 08:43:38 +0000 | [diff] [blame] | 94 | const ScopedGlobalRef<jclass> j_observer_class_; |
Alex Glaznev | c4905fb | 2015-04-20 16:54:42 -0700 | [diff] [blame] | 95 | volatile bool valid_global_refs_; |
perkj@webrtc.org | 96e4db9 | 2015-02-13 12:46:51 +0000 | [diff] [blame] | 96 | jobject j_frame_observer_; |
| 97 | |
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 | rtc::Thread* thread_; // The thread where Start is called on. |
| 101 | // |capturer| is a guaranteed to be a valid pointer between a call to |
| 102 | // AndroidVideoCapturerDelegate::Start |
| 103 | // until AndroidVideoCapturerDelegate::Stop. |
| 104 | webrtc::AndroidVideoCapturer* capturer_; |
| 105 | rtc::AsyncInvoker invoker_; |
perkj@webrtc.org | 3db042e | 2015-02-19 08:43:38 +0000 | [diff] [blame] | 106 | |
perkj@webrtc.org | 96e4db9 | 2015-02-13 12:46:51 +0000 | [diff] [blame] | 107 | static jobject application_context_; |
| 108 | |
| 109 | DISALLOW_COPY_AND_ASSIGN(AndroidVideoCapturerJni); |
| 110 | }; |
| 111 | |
| 112 | } // namespace webrtc_jni |
| 113 | |
| 114 | #endif // TALK_APP_WEBRTC_JAVA_JNI_ANDROIDVIDEOCAPTURER_JNI_H_ |