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 | #include "talk/app/webrtc/java/jni/androidvideocapturer_jni.h" |
| 30 | |
| 31 | #include "talk/app/webrtc/java/jni/classreferenceholder.h" |
perkj@webrtc.org | 3db042e | 2015-02-19 08:43:38 +0000 | [diff] [blame] | 32 | #include "webrtc/base/bind.h" |
perkj@webrtc.org | 96e4db9 | 2015-02-13 12:46:51 +0000 | [diff] [blame] | 33 | |
| 34 | namespace webrtc_jni { |
| 35 | |
| 36 | jobject AndroidVideoCapturerJni::application_context_ = nullptr; |
| 37 | |
| 38 | // static |
| 39 | int AndroidVideoCapturerJni::SetAndroidObjects(JNIEnv* jni, |
| 40 | jobject appliction_context) { |
| 41 | if (application_context_) { |
| 42 | jni->DeleteGlobalRef(application_context_); |
| 43 | } |
| 44 | application_context_ = NewGlobalRef(jni, appliction_context); |
| 45 | |
| 46 | return 0; |
| 47 | } |
| 48 | |
perkj@webrtc.org | 112f127 | 2015-02-25 09:20:07 +0000 | [diff] [blame] | 49 | // static |
| 50 | rtc::scoped_ptr<AndroidVideoCapturerJni> |
| 51 | AndroidVideoCapturerJni::Create(JNIEnv* jni, |
| 52 | jobject j_video_capture, |
| 53 | jstring device_name) { |
| 54 | rtc::scoped_ptr<AndroidVideoCapturerJni> capturer( |
| 55 | new AndroidVideoCapturerJni(jni, |
| 56 | j_video_capture)); |
| 57 | |
| 58 | if (capturer->Init(device_name)) |
| 59 | return capturer.Pass(); |
| 60 | return nullptr; |
| 61 | } |
| 62 | |
perkj@webrtc.org | 96e4db9 | 2015-02-13 12:46:51 +0000 | [diff] [blame] | 63 | AndroidVideoCapturerJni::AndroidVideoCapturerJni(JNIEnv* jni, |
| 64 | jobject j_video_capturer) |
| 65 | : j_capturer_global_(jni, j_video_capturer), |
| 66 | j_video_capturer_class_( |
| 67 | jni, FindClass(jni, "org/webrtc/VideoCapturerAndroid")), |
perkj@webrtc.org | 3db042e | 2015-02-19 08:43:38 +0000 | [diff] [blame] | 68 | j_observer_class_( |
perkj@webrtc.org | 96e4db9 | 2015-02-13 12:46:51 +0000 | [diff] [blame] | 69 | jni, |
| 70 | FindClass(jni, |
perkj@webrtc.org | 3db042e | 2015-02-19 08:43:38 +0000 | [diff] [blame] | 71 | "org/webrtc/VideoCapturerAndroid$NativeObserver")), |
perkj@webrtc.org | 112f127 | 2015-02-25 09:20:07 +0000 | [diff] [blame] | 72 | capturer_(nullptr) { |
perkj@webrtc.org | 3db042e | 2015-02-19 08:43:38 +0000 | [diff] [blame] | 73 | thread_checker_.DetachFromThread(); |
| 74 | } |
perkj@webrtc.org | 96e4db9 | 2015-02-13 12:46:51 +0000 | [diff] [blame] | 75 | |
perkj@webrtc.org | 3db042e | 2015-02-19 08:43:38 +0000 | [diff] [blame] | 76 | bool AndroidVideoCapturerJni::Init(jstring device_name) { |
| 77 | const jmethodID m(GetMethodID( |
| 78 | jni(), *j_video_capturer_class_, "init", "(Ljava/lang/String;)Z")); |
| 79 | if (!jni()->CallBooleanMethod(*j_capturer_global_, m, device_name)) { |
| 80 | return false; |
| 81 | } |
| 82 | CHECK_EXCEPTION(jni()) << "error during CallVoidMethod"; |
| 83 | return true; |
| 84 | } |
| 85 | |
| 86 | AndroidVideoCapturerJni::~AndroidVideoCapturerJni() { |
perkj@webrtc.org | 3db042e | 2015-02-19 08:43:38 +0000 | [diff] [blame] | 87 | } |
perkj@webrtc.org | 96e4db9 | 2015-02-13 12:46:51 +0000 | [diff] [blame] | 88 | |
| 89 | void AndroidVideoCapturerJni::Start(int width, int height, int framerate, |
| 90 | webrtc::AndroidVideoCapturer* capturer) { |
perkj@webrtc.org | 3db042e | 2015-02-19 08:43:38 +0000 | [diff] [blame] | 91 | DCHECK(thread_checker_.CalledOnValidThread()); |
perkj@webrtc.org | 112f127 | 2015-02-25 09:20:07 +0000 | [diff] [blame] | 92 | DCHECK(capturer_ == nullptr); |
| 93 | thread_ = rtc::Thread::Current(); |
| 94 | capturer_ = capturer; |
perkj@webrtc.org | 3db042e | 2015-02-19 08:43:38 +0000 | [diff] [blame] | 95 | |
perkj@webrtc.org | 96e4db9 | 2015-02-13 12:46:51 +0000 | [diff] [blame] | 96 | j_frame_observer_ = NewGlobalRef( |
| 97 | jni(), |
perkj@webrtc.org | 3db042e | 2015-02-19 08:43:38 +0000 | [diff] [blame] | 98 | jni()->NewObject(*j_observer_class_, |
perkj@webrtc.org | 96e4db9 | 2015-02-13 12:46:51 +0000 | [diff] [blame] | 99 | GetMethodID(jni(), |
perkj@webrtc.org | 3db042e | 2015-02-19 08:43:38 +0000 | [diff] [blame] | 100 | *j_observer_class_, |
perkj@webrtc.org | 96e4db9 | 2015-02-13 12:46:51 +0000 | [diff] [blame] | 101 | "<init>", |
| 102 | "(J)V"), |
perkj@webrtc.org | 112f127 | 2015-02-25 09:20:07 +0000 | [diff] [blame] | 103 | jlongFromPointer(this))); |
perkj@webrtc.org | 96e4db9 | 2015-02-13 12:46:51 +0000 | [diff] [blame] | 104 | CHECK_EXCEPTION(jni()) << "error during NewObject"; |
| 105 | |
| 106 | jmethodID m = GetMethodID( |
| 107 | jni(), *j_video_capturer_class_, "startCapture", |
| 108 | "(IIILandroid/content/Context;" |
| 109 | "Lorg/webrtc/VideoCapturerAndroid$CapturerObserver;)V"); |
| 110 | jni()->CallVoidMethod(*j_capturer_global_, |
| 111 | m, width, height, |
| 112 | framerate, |
| 113 | application_context_, |
| 114 | j_frame_observer_); |
| 115 | CHECK_EXCEPTION(jni()) << "error during VideoCapturerAndroid.startCapture"; |
| 116 | } |
| 117 | |
perkj@webrtc.org | 3db042e | 2015-02-19 08:43:38 +0000 | [diff] [blame] | 118 | void AndroidVideoCapturerJni::Stop() { |
| 119 | DCHECK(thread_checker_.CalledOnValidThread()); |
perkj@webrtc.org | 112f127 | 2015-02-25 09:20:07 +0000 | [diff] [blame] | 120 | capturer_ = nullptr; |
perkj@webrtc.org | 96e4db9 | 2015-02-13 12:46:51 +0000 | [diff] [blame] | 121 | jmethodID m = GetMethodID(jni(), *j_video_capturer_class_, |
perkj@webrtc.org | 3db042e | 2015-02-19 08:43:38 +0000 | [diff] [blame] | 122 | "stopCapture", "()V"); |
| 123 | jni()->CallVoidMethod(*j_capturer_global_, m); |
perkj@webrtc.org | 96e4db9 | 2015-02-13 12:46:51 +0000 | [diff] [blame] | 124 | CHECK_EXCEPTION(jni()) << "error during VideoCapturerAndroid.stopCapture"; |
| 125 | DeleteGlobalRef(jni(), j_frame_observer_); |
perkj@webrtc.org | 112f127 | 2015-02-25 09:20:07 +0000 | [diff] [blame] | 126 | // Do not process frames in flight after stop have returned since |
| 127 | // the memory buffers they point to have been deleted. |
| 128 | rtc::MessageQueueManager::Clear(&invoker_); |
| 129 | } |
| 130 | |
| 131 | void AndroidVideoCapturerJni::ReturnBuffer(int64 time_stamp) { |
| 132 | jmethodID m = GetMethodID(jni(), *j_video_capturer_class_, |
| 133 | "returnBuffer", "(J)V"); |
| 134 | jni()->CallVoidMethod(*j_capturer_global_, m, time_stamp); |
perkj@webrtc.org | 96e4db9 | 2015-02-13 12:46:51 +0000 | [diff] [blame] | 135 | } |
| 136 | |
| 137 | std::string AndroidVideoCapturerJni::GetSupportedFormats() { |
| 138 | jmethodID m = |
| 139 | GetMethodID(jni(), *j_video_capturer_class_, |
| 140 | "getSupportedFormatsAsJson", "()Ljava/lang/String;"); |
| 141 | jstring j_json_caps = |
| 142 | (jstring) jni()->CallObjectMethod(*j_capturer_global_, m); |
| 143 | CHECK_EXCEPTION(jni()) << "error during supportedFormatsAsJson"; |
| 144 | return JavaToStdString(jni(), j_json_caps); |
| 145 | } |
| 146 | |
perkj@webrtc.org | 112f127 | 2015-02-25 09:20:07 +0000 | [diff] [blame] | 147 | void AndroidVideoCapturerJni::OnCapturerStarted(bool success) { |
| 148 | invoker_.AsyncInvoke<void>( |
| 149 | thread_, |
| 150 | rtc::Bind(&AndroidVideoCapturerJni::OnCapturerStarted_w, this, success)); |
| 151 | } |
| 152 | |
| 153 | void AndroidVideoCapturerJni::OnIncomingFrame(void* video_frame, |
| 154 | int length, |
| 155 | int rotation, |
| 156 | int64 time_stamp) { |
| 157 | invoker_.AsyncInvoke<void>( |
| 158 | thread_, |
| 159 | rtc::Bind(&AndroidVideoCapturerJni::OnIncomingFrame_w, |
| 160 | this, video_frame, length, rotation, time_stamp)); |
| 161 | } |
| 162 | |
| 163 | void AndroidVideoCapturerJni::OnCapturerStarted_w(bool success) { |
| 164 | DCHECK(thread_checker_.CalledOnValidThread()); |
| 165 | if (capturer_) |
| 166 | capturer_->OnCapturerStarted(success); |
| 167 | } |
| 168 | |
| 169 | void AndroidVideoCapturerJni::OnIncomingFrame_w(void* video_frame, |
| 170 | int length, |
| 171 | int rotation, |
| 172 | int64 time_stamp) { |
| 173 | DCHECK(thread_checker_.CalledOnValidThread()); |
| 174 | if (capturer_) |
| 175 | capturer_->OnIncomingFrame(video_frame, length, rotation, time_stamp); |
| 176 | } |
| 177 | |
perkj@webrtc.org | 96e4db9 | 2015-02-13 12:46:51 +0000 | [diff] [blame] | 178 | JNIEnv* AndroidVideoCapturerJni::jni() { return AttachCurrentThreadIfNeeded(); } |
| 179 | |
perkj@webrtc.org | 3db042e | 2015-02-19 08:43:38 +0000 | [diff] [blame] | 180 | JOW(void, VideoCapturerAndroid_00024NativeObserver_nativeOnFrameCaptured) |
perkj@webrtc.org | 41d8fda | 2015-02-27 18:50:53 +0000 | [diff] [blame^] | 181 | (JNIEnv* jni, jclass, jlong j_capturer, jbyteArray j_frame, jint length, |
perkj@webrtc.org | 96e4db9 | 2015-02-13 12:46:51 +0000 | [diff] [blame] | 182 | jint rotation, jlong ts) { |
perkj@webrtc.org | 41d8fda | 2015-02-27 18:50:53 +0000 | [diff] [blame^] | 183 | jboolean is_copy = true; |
| 184 | jbyte* bytes = jni->GetByteArrayElements(j_frame, &is_copy); |
| 185 | if (!is_copy) { |
| 186 | reinterpret_cast<AndroidVideoCapturerJni*>( |
| 187 | j_capturer)->OnIncomingFrame(bytes, length, rotation, ts); |
| 188 | } else { |
| 189 | // If this is a copy of the original frame, it means that the memory |
| 190 | // is not direct memory and thus VideoCapturerAndroid does not guarantee |
| 191 | // that the memory is valid when we have released |j_frame|. |
| 192 | DCHECK(false) << "j_frame is a copy."; |
| 193 | } |
| 194 | jni->ReleaseByteArrayElements(j_frame, bytes, JNI_ABORT); |
perkj@webrtc.org | 96e4db9 | 2015-02-13 12:46:51 +0000 | [diff] [blame] | 195 | } |
| 196 | |
perkj@webrtc.org | 3db042e | 2015-02-19 08:43:38 +0000 | [diff] [blame] | 197 | JOW(void, VideoCapturerAndroid_00024NativeObserver_nativeCapturerStarted) |
perkj@webrtc.org | 112f127 | 2015-02-25 09:20:07 +0000 | [diff] [blame] | 198 | (JNIEnv* jni, jclass, jlong j_capturer, jboolean j_success) { |
| 199 | reinterpret_cast<AndroidVideoCapturerJni*>(j_capturer)->OnCapturerStarted( |
| 200 | j_success); |
perkj@webrtc.org | 96e4db9 | 2015-02-13 12:46:51 +0000 | [diff] [blame] | 201 | } |
| 202 | |
| 203 | } // namespace webrtc_jni |
| 204 | |