blob: 74a9372d866e1d8042f478bedc46aeacf4f50814 [file] [log] [blame]
perkj@webrtc.org96e4db92015-02-13 12:46:51 +00001/*
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"
perkj@webrtc.org96e4db92015-02-13 12:46:51 +000030#include "talk/app/webrtc/java/jni/classreferenceholder.h"
perkj@webrtc.org3db042e2015-02-19 08:43:38 +000031#include "webrtc/base/bind.h"
Magnus Jedvertc464f502015-08-25 23:22:08 +020032#include "webrtc/common_video/libyuv/include/webrtc_libyuv.h"
perkj@webrtc.org96e4db92015-02-13 12:46:51 +000033
34namespace webrtc_jni {
35
36jobject AndroidVideoCapturerJni::application_context_ = nullptr;
37
38// static
39int 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.org112f1272015-02-25 09:20:07 +000049// static
Per33544192015-04-02 12:30:51 +020050rtc::scoped_refptr<AndroidVideoCapturerJni>
perkj@webrtc.org112f1272015-02-25 09:20:07 +000051AndroidVideoCapturerJni::Create(JNIEnv* jni,
52 jobject j_video_capture,
53 jstring device_name) {
Alex Glaznev8c054152015-04-20 13:00:49 -070054 LOG(LS_INFO) << "AndroidVideoCapturerJni::Create";
Per33544192015-04-02 12:30:51 +020055 rtc::scoped_refptr<AndroidVideoCapturerJni> capturer(
56 new rtc::RefCountedObject<AndroidVideoCapturerJni>(jni, j_video_capture));
perkj@webrtc.org112f1272015-02-25 09:20:07 +000057
Alex Glaznev2f5be9a2015-05-19 10:56:32 -070058 if (capturer->Init(device_name)) {
Per33544192015-04-02 12:30:51 +020059 return capturer;
Alex Glaznev2f5be9a2015-05-19 10:56:32 -070060 }
61 LOG(LS_ERROR) << "AndroidVideoCapturerJni init fails";
perkj@webrtc.org112f1272015-02-25 09:20:07 +000062 return nullptr;
63}
64
perkj@webrtc.org96e4db92015-02-13 12:46:51 +000065AndroidVideoCapturerJni::AndroidVideoCapturerJni(JNIEnv* jni,
66 jobject j_video_capturer)
67 : j_capturer_global_(jni, j_video_capturer),
68 j_video_capturer_class_(
69 jni, FindClass(jni, "org/webrtc/VideoCapturerAndroid")),
perkj@webrtc.org3db042e2015-02-19 08:43:38 +000070 j_observer_class_(
perkj@webrtc.org96e4db92015-02-13 12:46:51 +000071 jni,
72 FindClass(jni,
perkj@webrtc.org3db042e2015-02-19 08:43:38 +000073 "org/webrtc/VideoCapturerAndroid$NativeObserver")),
Magnus Jedvertc464f502015-08-25 23:22:08 +020074 capturer_(nullptr) {
Alex Glaznev8c054152015-04-20 13:00:49 -070075 LOG(LS_INFO) << "AndroidVideoCapturerJni ctor";
perkj@webrtc.org3db042e2015-02-19 08:43:38 +000076 thread_checker_.DetachFromThread();
77}
perkj@webrtc.org96e4db92015-02-13 12:46:51 +000078
perkj@webrtc.org3db042e2015-02-19 08:43:38 +000079bool AndroidVideoCapturerJni::Init(jstring device_name) {
80 const jmethodID m(GetMethodID(
81 jni(), *j_video_capturer_class_, "init", "(Ljava/lang/String;)Z"));
82 if (!jni()->CallBooleanMethod(*j_capturer_global_, m, device_name)) {
83 return false;
84 }
85 CHECK_EXCEPTION(jni()) << "error during CallVoidMethod";
86 return true;
87}
88
89AndroidVideoCapturerJni::~AndroidVideoCapturerJni() {
Magnus Jedvertc464f502015-08-25 23:22:08 +020090 LOG(LS_INFO) << "AndroidVideoCapturerJni dtor";
Magnus Jedvertf706c8a2015-09-23 12:01:28 +020091 jni()->CallVoidMethod(
92 *j_capturer_global_,
93 GetMethodID(jni(), *j_video_capturer_class_, "release", "()V"));
94 CHECK_EXCEPTION(jni()) << "error during VideoCapturerAndroid.release()";
perkj@webrtc.org3db042e2015-02-19 08:43:38 +000095}
perkj@webrtc.org96e4db92015-02-13 12:46:51 +000096
97void AndroidVideoCapturerJni::Start(int width, int height, int framerate,
98 webrtc::AndroidVideoCapturer* capturer) {
Alex Glaznev8c054152015-04-20 13:00:49 -070099 LOG(LS_INFO) << "AndroidVideoCapturerJni start";
henrikg91d6ede2015-09-17 00:24:34 -0700100 RTC_DCHECK(thread_checker_.CalledOnValidThread());
Magnus Jedvertc464f502015-08-25 23:22:08 +0200101 {
102 rtc::CritScope cs(&capturer_lock_);
henrikg91d6ede2015-09-17 00:24:34 -0700103 RTC_CHECK(capturer_ == nullptr);
104 RTC_CHECK(invoker_.get() == nullptr);
Magnus Jedvertc464f502015-08-25 23:22:08 +0200105 capturer_ = capturer;
106 invoker_.reset(new rtc::GuardedAsyncInvoker());
107 }
108 jobject j_frame_observer =
perkj@webrtc.org3db042e2015-02-19 08:43:38 +0000109 jni()->NewObject(*j_observer_class_,
Magnus Jedvertc464f502015-08-25 23:22:08 +0200110 GetMethodID(jni(), *j_observer_class_, "<init>", "(J)V"),
111 jlongFromPointer(this));
perkj@webrtc.org96e4db92015-02-13 12:46:51 +0000112 CHECK_EXCEPTION(jni()) << "error during NewObject";
113
114 jmethodID m = GetMethodID(
115 jni(), *j_video_capturer_class_, "startCapture",
116 "(IIILandroid/content/Context;"
117 "Lorg/webrtc/VideoCapturerAndroid$CapturerObserver;)V");
118 jni()->CallVoidMethod(*j_capturer_global_,
119 m, width, height,
120 framerate,
121 application_context_,
Magnus Jedvertc464f502015-08-25 23:22:08 +0200122 j_frame_observer);
perkj@webrtc.org96e4db92015-02-13 12:46:51 +0000123 CHECK_EXCEPTION(jni()) << "error during VideoCapturerAndroid.startCapture";
124}
125
perkj@webrtc.org3db042e2015-02-19 08:43:38 +0000126void AndroidVideoCapturerJni::Stop() {
Alex Glaznev8c054152015-04-20 13:00:49 -0700127 LOG(LS_INFO) << "AndroidVideoCapturerJni stop";
henrikg91d6ede2015-09-17 00:24:34 -0700128 RTC_DCHECK(thread_checker_.CalledOnValidThread());
Magnus Jedvertc464f502015-08-25 23:22:08 +0200129 {
130 rtc::CritScope cs(&capturer_lock_);
131 // Destroying |invoker_| will cancel all pending calls to |capturer_|.
132 invoker_ = nullptr;
133 capturer_ = nullptr;
134 }
perkj@webrtc.org96e4db92015-02-13 12:46:51 +0000135 jmethodID m = GetMethodID(jni(), *j_video_capturer_class_,
perkj@webrtc.org3db042e2015-02-19 08:43:38 +0000136 "stopCapture", "()V");
137 jni()->CallVoidMethod(*j_capturer_global_, m);
perkj@webrtc.org96e4db92015-02-13 12:46:51 +0000138 CHECK_EXCEPTION(jni()) << "error during VideoCapturerAndroid.stopCapture";
Alex Glaznev8c054152015-04-20 13:00:49 -0700139 LOG(LS_INFO) << "AndroidVideoCapturerJni stop done";
perkj@webrtc.org112f1272015-02-25 09:20:07 +0000140}
141
Magnus Jedvertc464f502015-08-25 23:22:08 +0200142template <typename... Args>
143void AndroidVideoCapturerJni::AsyncCapturerInvoke(
144 const char* method_name,
145 void (webrtc::AndroidVideoCapturer::*method)(Args...),
146 Args... args) {
147 rtc::CritScope cs(&capturer_lock_);
148 if (!invoker_) {
149 LOG(LS_WARNING) << method_name << "() called for closed capturer.";
Alex Glaznevc4905fb2015-04-20 16:54:42 -0700150 return;
151 }
Magnus Jedvertc464f502015-08-25 23:22:08 +0200152 invoker_->AsyncInvoke<void>(rtc::Bind(method, capturer_, args...));
153}
154
155void AndroidVideoCapturerJni::ReturnBuffer(int64 time_stamp) {
perkj@webrtc.org112f1272015-02-25 09:20:07 +0000156 jmethodID m = GetMethodID(jni(), *j_video_capturer_class_,
157 "returnBuffer", "(J)V");
158 jni()->CallVoidMethod(*j_capturer_global_, m, time_stamp);
Per33544192015-04-02 12:30:51 +0200159 CHECK_EXCEPTION(jni()) << "error during VideoCapturerAndroid.returnBuffer";
perkj@webrtc.org96e4db92015-02-13 12:46:51 +0000160}
161
162std::string AndroidVideoCapturerJni::GetSupportedFormats() {
163 jmethodID m =
164 GetMethodID(jni(), *j_video_capturer_class_,
165 "getSupportedFormatsAsJson", "()Ljava/lang/String;");
166 jstring j_json_caps =
167 (jstring) jni()->CallObjectMethod(*j_capturer_global_, m);
168 CHECK_EXCEPTION(jni()) << "error during supportedFormatsAsJson";
169 return JavaToStdString(jni(), j_json_caps);
170}
171
perkj@webrtc.org112f1272015-02-25 09:20:07 +0000172void AndroidVideoCapturerJni::OnCapturerStarted(bool success) {
Magnus Jedvertc464f502015-08-25 23:22:08 +0200173 LOG(LS_INFO) << "AndroidVideoCapturerJni capture started: " << success;
174 AsyncCapturerInvoke("OnCapturerStarted",
175 &webrtc::AndroidVideoCapturer::OnCapturerStarted,
176 success);
perkj@webrtc.org112f1272015-02-25 09:20:07 +0000177}
178
179void AndroidVideoCapturerJni::OnIncomingFrame(void* video_frame,
180 int length,
magjedb69ab792015-07-22 02:32:00 -0700181 int width,
182 int height,
perkj@webrtc.org112f1272015-02-25 09:20:07 +0000183 int rotation,
184 int64 time_stamp) {
Magnus Jedvertc464f502015-08-25 23:22:08 +0200185 const uint8_t* y_plane = static_cast<uint8_t*>(video_frame);
186 // Android guarantees that the stride is a multiple of 16.
187 // http://developer.android.com/reference/android/hardware/Camera.Parameters.html#setPreviewFormat%28int%29
188 int y_stride;
189 int uv_stride;
190 webrtc::Calc16ByteAlignedStride(width, &y_stride, &uv_stride);
191 const uint8_t* v_plane = y_plane + y_stride * height;
192 const uint8_t* u_plane =
193 v_plane + uv_stride * webrtc::AlignInt(height, 2) / 2;
194
195 // Wrap the Java buffer, and call ReturnBuffer() in the wrapped
196 // VideoFrameBuffer destructor.
197 rtc::scoped_refptr<webrtc::VideoFrameBuffer> buffer(
198 new rtc::RefCountedObject<webrtc::WrappedI420Buffer>(
199 width, height, y_plane, y_stride, u_plane, uv_stride, v_plane,
200 uv_stride,
201 rtc::Bind(&AndroidVideoCapturerJni::ReturnBuffer, this, time_stamp)));
202 AsyncCapturerInvoke("OnIncomingFrame",
203 &webrtc::AndroidVideoCapturer::OnIncomingFrame,
204 buffer, rotation, time_stamp);
perkj@webrtc.org112f1272015-02-25 09:20:07 +0000205}
206
Åsa Persson2b679252015-06-15 09:53:05 +0200207void AndroidVideoCapturerJni::OnOutputFormatRequest(int width,
208 int height,
209 int fps) {
Magnus Jedvertc464f502015-08-25 23:22:08 +0200210 AsyncCapturerInvoke("OnOutputFormatRequest",
211 &webrtc::AndroidVideoCapturer::OnOutputFormatRequest,
212 width, height, fps);
Åsa Persson2b679252015-06-15 09:53:05 +0200213}
214
perkj@webrtc.org96e4db92015-02-13 12:46:51 +0000215JNIEnv* AndroidVideoCapturerJni::jni() { return AttachCurrentThreadIfNeeded(); }
216
perkj@webrtc.org3db042e2015-02-19 08:43:38 +0000217JOW(void, VideoCapturerAndroid_00024NativeObserver_nativeOnFrameCaptured)
perkj@webrtc.org41d8fda2015-02-27 18:50:53 +0000218 (JNIEnv* jni, jclass, jlong j_capturer, jbyteArray j_frame, jint length,
magjedb69ab792015-07-22 02:32:00 -0700219 jint width, jint height, jint rotation, jlong ts) {
perkj@webrtc.org41d8fda2015-02-27 18:50:53 +0000220 jboolean is_copy = true;
221 jbyte* bytes = jni->GetByteArrayElements(j_frame, &is_copy);
Magnus Jedvertc464f502015-08-25 23:22:08 +0200222 // If this is a copy of the original frame, it means that the memory
223 // is not direct memory and thus VideoCapturerAndroid does not guarantee
224 // that the memory is valid when we have released |j_frame|.
225 // TODO(magjed): Move ReleaseByteArrayElements() into ReturnBuffer() and
226 // remove this check.
henrikg91d6ede2015-09-17 00:24:34 -0700227 RTC_CHECK(!is_copy)
228 << "NativeObserver_nativeOnFrameCaptured: frame is a copy";
Magnus Jedvertc464f502015-08-25 23:22:08 +0200229 reinterpret_cast<AndroidVideoCapturerJni*>(j_capturer)
230 ->OnIncomingFrame(bytes, length, width, height, rotation, ts);
perkj@webrtc.org41d8fda2015-02-27 18:50:53 +0000231 jni->ReleaseByteArrayElements(j_frame, bytes, JNI_ABORT);
perkj@webrtc.org96e4db92015-02-13 12:46:51 +0000232}
233
perkj@webrtc.org3db042e2015-02-19 08:43:38 +0000234JOW(void, VideoCapturerAndroid_00024NativeObserver_nativeCapturerStarted)
perkj@webrtc.org112f1272015-02-25 09:20:07 +0000235 (JNIEnv* jni, jclass, jlong j_capturer, jboolean j_success) {
Alex Glaznev8c054152015-04-20 13:00:49 -0700236 LOG(LS_INFO) << "NativeObserver_nativeCapturerStarted";
perkj@webrtc.org112f1272015-02-25 09:20:07 +0000237 reinterpret_cast<AndroidVideoCapturerJni*>(j_capturer)->OnCapturerStarted(
238 j_success);
perkj@webrtc.org96e4db92015-02-13 12:46:51 +0000239}
240
Åsa Persson2b679252015-06-15 09:53:05 +0200241JOW(void, VideoCapturerAndroid_00024NativeObserver_nativeOnOutputFormatRequest)
242 (JNIEnv* jni, jclass, jlong j_capturer, jint j_width, jint j_height,
243 jint j_fps) {
244 LOG(LS_INFO) << "NativeObserver_nativeOnOutputFormatRequest";
245 reinterpret_cast<AndroidVideoCapturerJni*>(j_capturer)->OnOutputFormatRequest(
246 j_width, j_height, j_fps);
247}
248
perkj@webrtc.org96e4db92015-02-13 12:46:51 +0000249} // namespace webrtc_jni