blob: 69c350ab5e5b71e2d8891854abb2c3e0876c271c [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";
perkj@webrtc.org3db042e2015-02-19 08:43:38 +000091}
perkj@webrtc.org96e4db92015-02-13 12:46:51 +000092
93void AndroidVideoCapturerJni::Start(int width, int height, int framerate,
94 webrtc::AndroidVideoCapturer* capturer) {
Alex Glaznev8c054152015-04-20 13:00:49 -070095 LOG(LS_INFO) << "AndroidVideoCapturerJni start";
henrikg91d6ede2015-09-17 00:24:34 -070096 RTC_DCHECK(thread_checker_.CalledOnValidThread());
Magnus Jedvertc464f502015-08-25 23:22:08 +020097 {
98 rtc::CritScope cs(&capturer_lock_);
henrikg91d6ede2015-09-17 00:24:34 -070099 RTC_CHECK(capturer_ == nullptr);
100 RTC_CHECK(invoker_.get() == nullptr);
Magnus Jedvertc464f502015-08-25 23:22:08 +0200101 capturer_ = capturer;
102 invoker_.reset(new rtc::GuardedAsyncInvoker());
103 }
104 jobject j_frame_observer =
perkj@webrtc.org3db042e2015-02-19 08:43:38 +0000105 jni()->NewObject(*j_observer_class_,
Magnus Jedvertc464f502015-08-25 23:22:08 +0200106 GetMethodID(jni(), *j_observer_class_, "<init>", "(J)V"),
107 jlongFromPointer(this));
perkj@webrtc.org96e4db92015-02-13 12:46:51 +0000108 CHECK_EXCEPTION(jni()) << "error during NewObject";
109
110 jmethodID m = GetMethodID(
111 jni(), *j_video_capturer_class_, "startCapture",
112 "(IIILandroid/content/Context;"
113 "Lorg/webrtc/VideoCapturerAndroid$CapturerObserver;)V");
114 jni()->CallVoidMethod(*j_capturer_global_,
115 m, width, height,
116 framerate,
117 application_context_,
Magnus Jedvertc464f502015-08-25 23:22:08 +0200118 j_frame_observer);
perkj@webrtc.org96e4db92015-02-13 12:46:51 +0000119 CHECK_EXCEPTION(jni()) << "error during VideoCapturerAndroid.startCapture";
120}
121
perkj@webrtc.org3db042e2015-02-19 08:43:38 +0000122void AndroidVideoCapturerJni::Stop() {
Alex Glaznev8c054152015-04-20 13:00:49 -0700123 LOG(LS_INFO) << "AndroidVideoCapturerJni stop";
henrikg91d6ede2015-09-17 00:24:34 -0700124 RTC_DCHECK(thread_checker_.CalledOnValidThread());
Magnus Jedvertc464f502015-08-25 23:22:08 +0200125 {
126 rtc::CritScope cs(&capturer_lock_);
127 // Destroying |invoker_| will cancel all pending calls to |capturer_|.
128 invoker_ = nullptr;
129 capturer_ = nullptr;
130 }
perkj@webrtc.org96e4db92015-02-13 12:46:51 +0000131 jmethodID m = GetMethodID(jni(), *j_video_capturer_class_,
perkj@webrtc.org3db042e2015-02-19 08:43:38 +0000132 "stopCapture", "()V");
133 jni()->CallVoidMethod(*j_capturer_global_, m);
perkj@webrtc.org96e4db92015-02-13 12:46:51 +0000134 CHECK_EXCEPTION(jni()) << "error during VideoCapturerAndroid.stopCapture";
Alex Glaznev8c054152015-04-20 13:00:49 -0700135 LOG(LS_INFO) << "AndroidVideoCapturerJni stop done";
perkj@webrtc.org112f1272015-02-25 09:20:07 +0000136}
137
Magnus Jedvertc464f502015-08-25 23:22:08 +0200138template <typename... Args>
139void AndroidVideoCapturerJni::AsyncCapturerInvoke(
140 const char* method_name,
141 void (webrtc::AndroidVideoCapturer::*method)(Args...),
142 Args... args) {
143 rtc::CritScope cs(&capturer_lock_);
144 if (!invoker_) {
145 LOG(LS_WARNING) << method_name << "() called for closed capturer.";
Alex Glaznevc4905fb2015-04-20 16:54:42 -0700146 return;
147 }
Magnus Jedvertc464f502015-08-25 23:22:08 +0200148 invoker_->AsyncInvoke<void>(rtc::Bind(method, capturer_, args...));
149}
150
151void AndroidVideoCapturerJni::ReturnBuffer(int64 time_stamp) {
perkj@webrtc.org112f1272015-02-25 09:20:07 +0000152 jmethodID m = GetMethodID(jni(), *j_video_capturer_class_,
153 "returnBuffer", "(J)V");
154 jni()->CallVoidMethod(*j_capturer_global_, m, time_stamp);
Per33544192015-04-02 12:30:51 +0200155 CHECK_EXCEPTION(jni()) << "error during VideoCapturerAndroid.returnBuffer";
perkj@webrtc.org96e4db92015-02-13 12:46:51 +0000156}
157
158std::string AndroidVideoCapturerJni::GetSupportedFormats() {
159 jmethodID m =
160 GetMethodID(jni(), *j_video_capturer_class_,
161 "getSupportedFormatsAsJson", "()Ljava/lang/String;");
162 jstring j_json_caps =
163 (jstring) jni()->CallObjectMethod(*j_capturer_global_, m);
164 CHECK_EXCEPTION(jni()) << "error during supportedFormatsAsJson";
165 return JavaToStdString(jni(), j_json_caps);
166}
167
perkj@webrtc.org112f1272015-02-25 09:20:07 +0000168void AndroidVideoCapturerJni::OnCapturerStarted(bool success) {
Magnus Jedvertc464f502015-08-25 23:22:08 +0200169 LOG(LS_INFO) << "AndroidVideoCapturerJni capture started: " << success;
170 AsyncCapturerInvoke("OnCapturerStarted",
171 &webrtc::AndroidVideoCapturer::OnCapturerStarted,
172 success);
perkj@webrtc.org112f1272015-02-25 09:20:07 +0000173}
174
175void AndroidVideoCapturerJni::OnIncomingFrame(void* video_frame,
176 int length,
magjedb69ab792015-07-22 02:32:00 -0700177 int width,
178 int height,
perkj@webrtc.org112f1272015-02-25 09:20:07 +0000179 int rotation,
180 int64 time_stamp) {
Magnus Jedvertc464f502015-08-25 23:22:08 +0200181 const uint8_t* y_plane = static_cast<uint8_t*>(video_frame);
182 // Android guarantees that the stride is a multiple of 16.
183 // http://developer.android.com/reference/android/hardware/Camera.Parameters.html#setPreviewFormat%28int%29
184 int y_stride;
185 int uv_stride;
186 webrtc::Calc16ByteAlignedStride(width, &y_stride, &uv_stride);
187 const uint8_t* v_plane = y_plane + y_stride * height;
188 const uint8_t* u_plane =
189 v_plane + uv_stride * webrtc::AlignInt(height, 2) / 2;
190
191 // Wrap the Java buffer, and call ReturnBuffer() in the wrapped
192 // VideoFrameBuffer destructor.
193 rtc::scoped_refptr<webrtc::VideoFrameBuffer> buffer(
194 new rtc::RefCountedObject<webrtc::WrappedI420Buffer>(
195 width, height, y_plane, y_stride, u_plane, uv_stride, v_plane,
196 uv_stride,
197 rtc::Bind(&AndroidVideoCapturerJni::ReturnBuffer, this, time_stamp)));
198 AsyncCapturerInvoke("OnIncomingFrame",
199 &webrtc::AndroidVideoCapturer::OnIncomingFrame,
200 buffer, rotation, time_stamp);
perkj@webrtc.org112f1272015-02-25 09:20:07 +0000201}
202
Åsa Persson2b679252015-06-15 09:53:05 +0200203void AndroidVideoCapturerJni::OnOutputFormatRequest(int width,
204 int height,
205 int fps) {
Magnus Jedvertc464f502015-08-25 23:22:08 +0200206 AsyncCapturerInvoke("OnOutputFormatRequest",
207 &webrtc::AndroidVideoCapturer::OnOutputFormatRequest,
208 width, height, fps);
Åsa Persson2b679252015-06-15 09:53:05 +0200209}
210
perkj@webrtc.org96e4db92015-02-13 12:46:51 +0000211JNIEnv* AndroidVideoCapturerJni::jni() { return AttachCurrentThreadIfNeeded(); }
212
perkj@webrtc.org3db042e2015-02-19 08:43:38 +0000213JOW(void, VideoCapturerAndroid_00024NativeObserver_nativeOnFrameCaptured)
perkj@webrtc.org41d8fda2015-02-27 18:50:53 +0000214 (JNIEnv* jni, jclass, jlong j_capturer, jbyteArray j_frame, jint length,
magjedb69ab792015-07-22 02:32:00 -0700215 jint width, jint height, jint rotation, jlong ts) {
perkj@webrtc.org41d8fda2015-02-27 18:50:53 +0000216 jboolean is_copy = true;
217 jbyte* bytes = jni->GetByteArrayElements(j_frame, &is_copy);
Magnus Jedvertc464f502015-08-25 23:22:08 +0200218 // If this is a copy of the original frame, it means that the memory
219 // is not direct memory and thus VideoCapturerAndroid does not guarantee
220 // that the memory is valid when we have released |j_frame|.
221 // TODO(magjed): Move ReleaseByteArrayElements() into ReturnBuffer() and
222 // remove this check.
henrikg91d6ede2015-09-17 00:24:34 -0700223 RTC_CHECK(!is_copy)
224 << "NativeObserver_nativeOnFrameCaptured: frame is a copy";
Magnus Jedvertc464f502015-08-25 23:22:08 +0200225 reinterpret_cast<AndroidVideoCapturerJni*>(j_capturer)
226 ->OnIncomingFrame(bytes, length, width, height, rotation, ts);
perkj@webrtc.org41d8fda2015-02-27 18:50:53 +0000227 jni->ReleaseByteArrayElements(j_frame, bytes, JNI_ABORT);
perkj@webrtc.org96e4db92015-02-13 12:46:51 +0000228}
229
perkj@webrtc.org3db042e2015-02-19 08:43:38 +0000230JOW(void, VideoCapturerAndroid_00024NativeObserver_nativeCapturerStarted)
perkj@webrtc.org112f1272015-02-25 09:20:07 +0000231 (JNIEnv* jni, jclass, jlong j_capturer, jboolean j_success) {
Alex Glaznev8c054152015-04-20 13:00:49 -0700232 LOG(LS_INFO) << "NativeObserver_nativeCapturerStarted";
perkj@webrtc.org112f1272015-02-25 09:20:07 +0000233 reinterpret_cast<AndroidVideoCapturerJni*>(j_capturer)->OnCapturerStarted(
234 j_success);
perkj@webrtc.org96e4db92015-02-13 12:46:51 +0000235}
236
Åsa Persson2b679252015-06-15 09:53:05 +0200237JOW(void, VideoCapturerAndroid_00024NativeObserver_nativeOnOutputFormatRequest)
238 (JNIEnv* jni, jclass, jlong j_capturer, jint j_width, jint j_height,
239 jint j_fps) {
240 LOG(LS_INFO) << "NativeObserver_nativeOnOutputFormatRequest";
241 reinterpret_cast<AndroidVideoCapturerJni*>(j_capturer)->OnOutputFormatRequest(
242 j_width, j_height, j_fps);
243}
244
perkj@webrtc.org96e4db92015-02-13 12:46:51 +0000245} // namespace webrtc_jni