blob: 9b3053c94033ca24cd923fcb8972c73275a2ef57 [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"
perkjac306422015-10-08 15:32:38 +020031#include "talk/app/webrtc/java/jni/native_handle_impl.h"
perkj@webrtc.org3db042e2015-02-19 08:43:38 +000032#include "webrtc/base/bind.h"
Magnus Jedvertc464f502015-08-25 23:22:08 +020033#include "webrtc/common_video/libyuv/include/webrtc_libyuv.h"
perkj@webrtc.org96e4db92015-02-13 12:46:51 +000034
35namespace webrtc_jni {
36
37jobject AndroidVideoCapturerJni::application_context_ = nullptr;
38
39// static
40int AndroidVideoCapturerJni::SetAndroidObjects(JNIEnv* jni,
41 jobject appliction_context) {
42 if (application_context_) {
43 jni->DeleteGlobalRef(application_context_);
44 }
45 application_context_ = NewGlobalRef(jni, appliction_context);
46
47 return 0;
48}
49
nissec490e012015-12-10 06:23:33 -080050AndroidVideoCapturerJni::AndroidVideoCapturerJni(
51 JNIEnv* jni,
52 jobject j_video_capturer,
53 jobject j_surface_texture_helper)
54 : j_video_capturer_(jni, j_video_capturer),
55 j_surface_texture_helper_(jni, j_surface_texture_helper),
perkj@webrtc.org96e4db92015-02-13 12:46:51 +000056 j_video_capturer_class_(
57 jni, FindClass(jni, "org/webrtc/VideoCapturerAndroid")),
perkj@webrtc.org3db042e2015-02-19 08:43:38 +000058 j_observer_class_(
perkj@webrtc.org96e4db92015-02-13 12:46:51 +000059 jni,
60 FindClass(jni,
perkj@webrtc.org3db042e2015-02-19 08:43:38 +000061 "org/webrtc/VideoCapturerAndroid$NativeObserver")),
Magnus Jedvertc464f502015-08-25 23:22:08 +020062 capturer_(nullptr) {
Alex Glaznev8c054152015-04-20 13:00:49 -070063 LOG(LS_INFO) << "AndroidVideoCapturerJni ctor";
perkj@webrtc.org3db042e2015-02-19 08:43:38 +000064 thread_checker_.DetachFromThread();
65}
perkj@webrtc.org96e4db92015-02-13 12:46:51 +000066
perkj@webrtc.org3db042e2015-02-19 08:43:38 +000067AndroidVideoCapturerJni::~AndroidVideoCapturerJni() {
Magnus Jedvertc464f502015-08-25 23:22:08 +020068 LOG(LS_INFO) << "AndroidVideoCapturerJni dtor";
Magnus Jedvertf706c8a2015-09-23 12:01:28 +020069 jni()->CallVoidMethod(
nissec490e012015-12-10 06:23:33 -080070 *j_video_capturer_,
Magnus Jedvertf706c8a2015-09-23 12:01:28 +020071 GetMethodID(jni(), *j_video_capturer_class_, "release", "()V"));
72 CHECK_EXCEPTION(jni()) << "error during VideoCapturerAndroid.release()";
perkj@webrtc.org3db042e2015-02-19 08:43:38 +000073}
perkj@webrtc.org96e4db92015-02-13 12:46:51 +000074
75void AndroidVideoCapturerJni::Start(int width, int height, int framerate,
76 webrtc::AndroidVideoCapturer* capturer) {
Alex Glaznev8c054152015-04-20 13:00:49 -070077 LOG(LS_INFO) << "AndroidVideoCapturerJni start";
henrikg91d6ede2015-09-17 00:24:34 -070078 RTC_DCHECK(thread_checker_.CalledOnValidThread());
Magnus Jedvertc464f502015-08-25 23:22:08 +020079 {
80 rtc::CritScope cs(&capturer_lock_);
henrikg91d6ede2015-09-17 00:24:34 -070081 RTC_CHECK(capturer_ == nullptr);
82 RTC_CHECK(invoker_.get() == nullptr);
Magnus Jedvertc464f502015-08-25 23:22:08 +020083 capturer_ = capturer;
84 invoker_.reset(new rtc::GuardedAsyncInvoker());
85 }
86 jobject j_frame_observer =
perkj@webrtc.org3db042e2015-02-19 08:43:38 +000087 jni()->NewObject(*j_observer_class_,
Magnus Jedvertc464f502015-08-25 23:22:08 +020088 GetMethodID(jni(), *j_observer_class_, "<init>", "(J)V"),
89 jlongFromPointer(this));
perkj@webrtc.org96e4db92015-02-13 12:46:51 +000090 CHECK_EXCEPTION(jni()) << "error during NewObject";
91
92 jmethodID m = GetMethodID(
93 jni(), *j_video_capturer_class_, "startCapture",
94 "(IIILandroid/content/Context;"
95 "Lorg/webrtc/VideoCapturerAndroid$CapturerObserver;)V");
nissec490e012015-12-10 06:23:33 -080096 jni()->CallVoidMethod(*j_video_capturer_,
perkj@webrtc.org96e4db92015-02-13 12:46:51 +000097 m, width, height,
98 framerate,
99 application_context_,
Magnus Jedvertc464f502015-08-25 23:22:08 +0200100 j_frame_observer);
perkj@webrtc.org96e4db92015-02-13 12:46:51 +0000101 CHECK_EXCEPTION(jni()) << "error during VideoCapturerAndroid.startCapture";
102}
103
perkj@webrtc.org3db042e2015-02-19 08:43:38 +0000104void AndroidVideoCapturerJni::Stop() {
Alex Glaznev8c054152015-04-20 13:00:49 -0700105 LOG(LS_INFO) << "AndroidVideoCapturerJni stop";
henrikg91d6ede2015-09-17 00:24:34 -0700106 RTC_DCHECK(thread_checker_.CalledOnValidThread());
Magnus Jedvertc464f502015-08-25 23:22:08 +0200107 {
108 rtc::CritScope cs(&capturer_lock_);
109 // Destroying |invoker_| will cancel all pending calls to |capturer_|.
110 invoker_ = nullptr;
111 capturer_ = nullptr;
112 }
perkj@webrtc.org96e4db92015-02-13 12:46:51 +0000113 jmethodID m = GetMethodID(jni(), *j_video_capturer_class_,
perkj@webrtc.org3db042e2015-02-19 08:43:38 +0000114 "stopCapture", "()V");
nissec490e012015-12-10 06:23:33 -0800115 jni()->CallVoidMethod(*j_video_capturer_, m);
perkj@webrtc.org96e4db92015-02-13 12:46:51 +0000116 CHECK_EXCEPTION(jni()) << "error during VideoCapturerAndroid.stopCapture";
Alex Glaznev8c054152015-04-20 13:00:49 -0700117 LOG(LS_INFO) << "AndroidVideoCapturerJni stop done";
perkj@webrtc.org112f1272015-02-25 09:20:07 +0000118}
119
Magnus Jedvertc464f502015-08-25 23:22:08 +0200120template <typename... Args>
121void AndroidVideoCapturerJni::AsyncCapturerInvoke(
122 const char* method_name,
123 void (webrtc::AndroidVideoCapturer::*method)(Args...),
olka30a5b5e2015-10-20 11:04:56 -0700124 typename Identity<Args>::type... args) {
Magnus Jedvertc464f502015-08-25 23:22:08 +0200125 rtc::CritScope cs(&capturer_lock_);
126 if (!invoker_) {
127 LOG(LS_WARNING) << method_name << "() called for closed capturer.";
Alex Glaznevc4905fb2015-04-20 16:54:42 -0700128 return;
129 }
Magnus Jedvertc464f502015-08-25 23:22:08 +0200130 invoker_->AsyncInvoke<void>(rtc::Bind(method, capturer_, args...));
131}
132
Peter Boström0c4e06b2015-10-07 12:23:21 +0200133void AndroidVideoCapturerJni::ReturnBuffer(int64_t time_stamp) {
perkj@webrtc.org112f1272015-02-25 09:20:07 +0000134 jmethodID m = GetMethodID(jni(), *j_video_capturer_class_,
135 "returnBuffer", "(J)V");
nissec490e012015-12-10 06:23:33 -0800136 jni()->CallVoidMethod(*j_video_capturer_, m, time_stamp);
Per33544192015-04-02 12:30:51 +0200137 CHECK_EXCEPTION(jni()) << "error during VideoCapturerAndroid.returnBuffer";
perkj@webrtc.org96e4db92015-02-13 12:46:51 +0000138}
139
140std::string AndroidVideoCapturerJni::GetSupportedFormats() {
141 jmethodID m =
142 GetMethodID(jni(), *j_video_capturer_class_,
143 "getSupportedFormatsAsJson", "()Ljava/lang/String;");
144 jstring j_json_caps =
nissec490e012015-12-10 06:23:33 -0800145 (jstring) jni()->CallObjectMethod(*j_video_capturer_, m);
perkj@webrtc.org96e4db92015-02-13 12:46:51 +0000146 CHECK_EXCEPTION(jni()) << "error during supportedFormatsAsJson";
147 return JavaToStdString(jni(), j_json_caps);
148}
149
perkj@webrtc.org112f1272015-02-25 09:20:07 +0000150void AndroidVideoCapturerJni::OnCapturerStarted(bool success) {
Magnus Jedvertc464f502015-08-25 23:22:08 +0200151 LOG(LS_INFO) << "AndroidVideoCapturerJni capture started: " << success;
152 AsyncCapturerInvoke("OnCapturerStarted",
153 &webrtc::AndroidVideoCapturer::OnCapturerStarted,
154 success);
perkj@webrtc.org112f1272015-02-25 09:20:07 +0000155}
156
perkjac306422015-10-08 15:32:38 +0200157void AndroidVideoCapturerJni::OnMemoryBufferFrame(void* video_frame,
158 int length,
159 int width,
160 int height,
161 int rotation,
162 int64_t timestamp_ns) {
magjedb5815c82015-09-29 01:13:43 -0700163 const uint8_t* y_plane = static_cast<uint8_t*>(video_frame);
Magnus Jedvertc464f502015-08-25 23:22:08 +0200164 // Android guarantees that the stride is a multiple of 16.
165 // http://developer.android.com/reference/android/hardware/Camera.Parameters.html#setPreviewFormat%28int%29
166 int y_stride;
167 int uv_stride;
168 webrtc::Calc16ByteAlignedStride(width, &y_stride, &uv_stride);
169 const uint8_t* v_plane = y_plane + y_stride * height;
170 const uint8_t* u_plane =
171 v_plane + uv_stride * webrtc::AlignInt(height, 2) / 2;
172
173 // Wrap the Java buffer, and call ReturnBuffer() in the wrapped
174 // VideoFrameBuffer destructor.
175 rtc::scoped_refptr<webrtc::VideoFrameBuffer> buffer(
176 new rtc::RefCountedObject<webrtc::WrappedI420Buffer>(
177 width, height, y_plane, y_stride, u_plane, uv_stride, v_plane,
178 uv_stride,
perkjac306422015-10-08 15:32:38 +0200179 rtc::Bind(&AndroidVideoCapturerJni::ReturnBuffer, this,
180 timestamp_ns)));
Magnus Jedvertc464f502015-08-25 23:22:08 +0200181 AsyncCapturerInvoke("OnIncomingFrame",
182 &webrtc::AndroidVideoCapturer::OnIncomingFrame,
perkjac306422015-10-08 15:32:38 +0200183 buffer, rotation, timestamp_ns);
184}
185
Per488e75f2015-11-19 10:43:36 +0100186void AndroidVideoCapturerJni::OnTextureFrame(int width,
187 int height,
188 int64_t timestamp_ns,
189 const NativeHandleImpl& handle) {
perkjac306422015-10-08 15:32:38 +0200190 rtc::scoped_refptr<webrtc::VideoFrameBuffer> buffer(
magjed52a30e32015-10-12 06:53:20 -0700191 new rtc::RefCountedObject<AndroidTextureBuffer>(
nissec490e012015-12-10 06:23:33 -0800192 width, height, handle, *j_surface_texture_helper_,
perkjac306422015-10-08 15:32:38 +0200193 rtc::Bind(&AndroidVideoCapturerJni::ReturnBuffer, this,
194 timestamp_ns)));
195 AsyncCapturerInvoke("OnIncomingFrame",
196 &webrtc::AndroidVideoCapturer::OnIncomingFrame,
197 buffer, 0, timestamp_ns);
perkj@webrtc.org112f1272015-02-25 09:20:07 +0000198}
199
Åsa Persson2b679252015-06-15 09:53:05 +0200200void AndroidVideoCapturerJni::OnOutputFormatRequest(int width,
201 int height,
202 int fps) {
Magnus Jedvertc464f502015-08-25 23:22:08 +0200203 AsyncCapturerInvoke("OnOutputFormatRequest",
204 &webrtc::AndroidVideoCapturer::OnOutputFormatRequest,
205 width, height, fps);
Åsa Persson2b679252015-06-15 09:53:05 +0200206}
207
perkj@webrtc.org96e4db92015-02-13 12:46:51 +0000208JNIEnv* AndroidVideoCapturerJni::jni() { return AttachCurrentThreadIfNeeded(); }
209
perkj3d06eca2015-10-08 12:53:33 +0200210JOW(void,
211 VideoCapturerAndroid_00024NativeObserver_nativeOnByteBufferFrameCaptured)
magjedb5815c82015-09-29 01:13:43 -0700212 (JNIEnv* jni, jclass, jlong j_capturer, jbyteArray j_frame, jint length,
perkjac306422015-10-08 15:32:38 +0200213 jint width, jint height, jint rotation, jlong timestamp) {
magjedb5815c82015-09-29 01:13:43 -0700214 jboolean is_copy = true;
215 jbyte* bytes = jni->GetByteArrayElements(j_frame, &is_copy);
216 // If this is a copy of the original frame, it means that the memory
217 // is not direct memory and thus VideoCapturerAndroid does not guarantee
218 // that the memory is valid when we have released |j_frame|.
219 // TODO(magjed): Move ReleaseByteArrayElements() into ReturnBuffer() and
220 // remove this check.
221 RTC_CHECK(!is_copy)
222 << "NativeObserver_nativeOnFrameCaptured: frame is a copy";
Magnus Jedvertc464f502015-08-25 23:22:08 +0200223 reinterpret_cast<AndroidVideoCapturerJni*>(j_capturer)
perkjac306422015-10-08 15:32:38 +0200224 ->OnMemoryBufferFrame(bytes, length, width, height, rotation, timestamp);
magjedb5815c82015-09-29 01:13:43 -0700225 jni->ReleaseByteArrayElements(j_frame, bytes, JNI_ABORT);
perkj@webrtc.org96e4db92015-02-13 12:46:51 +0000226}
227
perkjac306422015-10-08 15:32:38 +0200228JOW(void, VideoCapturerAndroid_00024NativeObserver_nativeOnTextureFrameCaptured)
229 (JNIEnv* jni, jclass, jlong j_capturer, jint j_width, jint j_height,
230 jint j_oes_texture_id, jfloatArray j_transform_matrix,
231 jlong j_timestamp) {
232 reinterpret_cast<AndroidVideoCapturerJni*>(j_capturer)
233 ->OnTextureFrame(j_width, j_height, j_timestamp,
Per488e75f2015-11-19 10:43:36 +0100234 NativeHandleImpl(jni, j_oes_texture_id,
235 j_transform_matrix));
perkjac306422015-10-08 15:32:38 +0200236}
237
perkj@webrtc.org3db042e2015-02-19 08:43:38 +0000238JOW(void, VideoCapturerAndroid_00024NativeObserver_nativeCapturerStarted)
perkj@webrtc.org112f1272015-02-25 09:20:07 +0000239 (JNIEnv* jni, jclass, jlong j_capturer, jboolean j_success) {
Alex Glaznev8c054152015-04-20 13:00:49 -0700240 LOG(LS_INFO) << "NativeObserver_nativeCapturerStarted";
perkj@webrtc.org112f1272015-02-25 09:20:07 +0000241 reinterpret_cast<AndroidVideoCapturerJni*>(j_capturer)->OnCapturerStarted(
242 j_success);
perkj@webrtc.org96e4db92015-02-13 12:46:51 +0000243}
244
Åsa Persson2b679252015-06-15 09:53:05 +0200245JOW(void, VideoCapturerAndroid_00024NativeObserver_nativeOnOutputFormatRequest)
246 (JNIEnv* jni, jclass, jlong j_capturer, jint j_width, jint j_height,
247 jint j_fps) {
248 LOG(LS_INFO) << "NativeObserver_nativeOnOutputFormatRequest";
249 reinterpret_cast<AndroidVideoCapturerJni*>(j_capturer)->OnOutputFormatRequest(
250 j_width, j_height, j_fps);
251}
252
perkje0bce242015-10-05 16:21:54 +0200253JOW(jlong, VideoCapturerAndroid_nativeCreateVideoCapturer)
nissec490e012015-12-10 06:23:33 -0800254 (JNIEnv* jni, jclass,
255 jobject j_video_capturer, jobject j_surface_texture_helper) {
perkje0bce242015-10-05 16:21:54 +0200256 rtc::scoped_refptr<webrtc::AndroidVideoCapturerDelegate> delegate =
nissec490e012015-12-10 06:23:33 -0800257 new rtc::RefCountedObject<AndroidVideoCapturerJni>(
258 jni, j_video_capturer, j_surface_texture_helper);
perkje0bce242015-10-05 16:21:54 +0200259 rtc::scoped_ptr<cricket::VideoCapturer> capturer(
260 new webrtc::AndroidVideoCapturer(delegate));
261 // Caller takes ownership of the cricket::VideoCapturer* pointer.
262 return jlongFromPointer(capturer.release());
263}
264
perkj@webrtc.org96e4db92015-02-13 12:46:51 +0000265} // namespace webrtc_jni