blob: 02b9f22015926d5c7efed8fd35e8010ae241d925 [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
50AndroidVideoCapturerJni::AndroidVideoCapturerJni(JNIEnv* jni,
51 jobject j_video_capturer)
52 : j_capturer_global_(jni, j_video_capturer),
53 j_video_capturer_class_(
54 jni, FindClass(jni, "org/webrtc/VideoCapturerAndroid")),
perkj@webrtc.org3db042e2015-02-19 08:43:38 +000055 j_observer_class_(
perkj@webrtc.org96e4db92015-02-13 12:46:51 +000056 jni,
57 FindClass(jni,
perkj@webrtc.org3db042e2015-02-19 08:43:38 +000058 "org/webrtc/VideoCapturerAndroid$NativeObserver")),
Magnus Jedvertc464f502015-08-25 23:22:08 +020059 capturer_(nullptr) {
Alex Glaznev8c054152015-04-20 13:00:49 -070060 LOG(LS_INFO) << "AndroidVideoCapturerJni ctor";
perkj@webrtc.org3db042e2015-02-19 08:43:38 +000061 thread_checker_.DetachFromThread();
62}
perkj@webrtc.org96e4db92015-02-13 12:46:51 +000063
perkj@webrtc.org3db042e2015-02-19 08:43:38 +000064AndroidVideoCapturerJni::~AndroidVideoCapturerJni() {
Magnus Jedvertc464f502015-08-25 23:22:08 +020065 LOG(LS_INFO) << "AndroidVideoCapturerJni dtor";
Magnus Jedvertf706c8a2015-09-23 12:01:28 +020066 jni()->CallVoidMethod(
67 *j_capturer_global_,
68 GetMethodID(jni(), *j_video_capturer_class_, "release", "()V"));
69 CHECK_EXCEPTION(jni()) << "error during VideoCapturerAndroid.release()";
perkj@webrtc.org3db042e2015-02-19 08:43:38 +000070}
perkj@webrtc.org96e4db92015-02-13 12:46:51 +000071
72void AndroidVideoCapturerJni::Start(int width, int height, int framerate,
73 webrtc::AndroidVideoCapturer* capturer) {
Alex Glaznev8c054152015-04-20 13:00:49 -070074 LOG(LS_INFO) << "AndroidVideoCapturerJni start";
henrikg91d6ede2015-09-17 00:24:34 -070075 RTC_DCHECK(thread_checker_.CalledOnValidThread());
Magnus Jedvertc464f502015-08-25 23:22:08 +020076 {
77 rtc::CritScope cs(&capturer_lock_);
henrikg91d6ede2015-09-17 00:24:34 -070078 RTC_CHECK(capturer_ == nullptr);
79 RTC_CHECK(invoker_.get() == nullptr);
Magnus Jedvertc464f502015-08-25 23:22:08 +020080 capturer_ = capturer;
81 invoker_.reset(new rtc::GuardedAsyncInvoker());
82 }
83 jobject j_frame_observer =
perkj@webrtc.org3db042e2015-02-19 08:43:38 +000084 jni()->NewObject(*j_observer_class_,
Magnus Jedvertc464f502015-08-25 23:22:08 +020085 GetMethodID(jni(), *j_observer_class_, "<init>", "(J)V"),
86 jlongFromPointer(this));
perkj@webrtc.org96e4db92015-02-13 12:46:51 +000087 CHECK_EXCEPTION(jni()) << "error during NewObject";
88
89 jmethodID m = GetMethodID(
90 jni(), *j_video_capturer_class_, "startCapture",
91 "(IIILandroid/content/Context;"
92 "Lorg/webrtc/VideoCapturerAndroid$CapturerObserver;)V");
93 jni()->CallVoidMethod(*j_capturer_global_,
94 m, width, height,
95 framerate,
96 application_context_,
Magnus Jedvertc464f502015-08-25 23:22:08 +020097 j_frame_observer);
perkj@webrtc.org96e4db92015-02-13 12:46:51 +000098 CHECK_EXCEPTION(jni()) << "error during VideoCapturerAndroid.startCapture";
99}
100
perkj@webrtc.org3db042e2015-02-19 08:43:38 +0000101void AndroidVideoCapturerJni::Stop() {
Alex Glaznev8c054152015-04-20 13:00:49 -0700102 LOG(LS_INFO) << "AndroidVideoCapturerJni stop";
henrikg91d6ede2015-09-17 00:24:34 -0700103 RTC_DCHECK(thread_checker_.CalledOnValidThread());
Magnus Jedvertc464f502015-08-25 23:22:08 +0200104 {
105 rtc::CritScope cs(&capturer_lock_);
106 // Destroying |invoker_| will cancel all pending calls to |capturer_|.
107 invoker_ = nullptr;
108 capturer_ = nullptr;
109 }
perkj@webrtc.org96e4db92015-02-13 12:46:51 +0000110 jmethodID m = GetMethodID(jni(), *j_video_capturer_class_,
perkj@webrtc.org3db042e2015-02-19 08:43:38 +0000111 "stopCapture", "()V");
112 jni()->CallVoidMethod(*j_capturer_global_, m);
perkj@webrtc.org96e4db92015-02-13 12:46:51 +0000113 CHECK_EXCEPTION(jni()) << "error during VideoCapturerAndroid.stopCapture";
Alex Glaznev8c054152015-04-20 13:00:49 -0700114 LOG(LS_INFO) << "AndroidVideoCapturerJni stop done";
perkj@webrtc.org112f1272015-02-25 09:20:07 +0000115}
116
Magnus Jedvertc464f502015-08-25 23:22:08 +0200117template <typename... Args>
118void AndroidVideoCapturerJni::AsyncCapturerInvoke(
119 const char* method_name,
120 void (webrtc::AndroidVideoCapturer::*method)(Args...),
olka30a5b5e2015-10-20 11:04:56 -0700121 typename Identity<Args>::type... args) {
Magnus Jedvertc464f502015-08-25 23:22:08 +0200122 rtc::CritScope cs(&capturer_lock_);
123 if (!invoker_) {
124 LOG(LS_WARNING) << method_name << "() called for closed capturer.";
Alex Glaznevc4905fb2015-04-20 16:54:42 -0700125 return;
126 }
Magnus Jedvertc464f502015-08-25 23:22:08 +0200127 invoker_->AsyncInvoke<void>(rtc::Bind(method, capturer_, args...));
128}
129
Peter Boström0c4e06b2015-10-07 12:23:21 +0200130void AndroidVideoCapturerJni::ReturnBuffer(int64_t time_stamp) {
perkj@webrtc.org112f1272015-02-25 09:20:07 +0000131 jmethodID m = GetMethodID(jni(), *j_video_capturer_class_,
132 "returnBuffer", "(J)V");
133 jni()->CallVoidMethod(*j_capturer_global_, m, time_stamp);
Per33544192015-04-02 12:30:51 +0200134 CHECK_EXCEPTION(jni()) << "error during VideoCapturerAndroid.returnBuffer";
perkj@webrtc.org96e4db92015-02-13 12:46:51 +0000135}
136
137std::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.org112f1272015-02-25 09:20:07 +0000147void AndroidVideoCapturerJni::OnCapturerStarted(bool success) {
Magnus Jedvertc464f502015-08-25 23:22:08 +0200148 LOG(LS_INFO) << "AndroidVideoCapturerJni capture started: " << success;
149 AsyncCapturerInvoke("OnCapturerStarted",
150 &webrtc::AndroidVideoCapturer::OnCapturerStarted,
151 success);
perkj@webrtc.org112f1272015-02-25 09:20:07 +0000152}
153
perkjac306422015-10-08 15:32:38 +0200154void AndroidVideoCapturerJni::OnMemoryBufferFrame(void* video_frame,
155 int length,
156 int width,
157 int height,
158 int rotation,
159 int64_t timestamp_ns) {
magjedb5815c82015-09-29 01:13:43 -0700160 const uint8_t* y_plane = static_cast<uint8_t*>(video_frame);
Magnus Jedvertc464f502015-08-25 23:22:08 +0200161 // Android guarantees that the stride is a multiple of 16.
162 // http://developer.android.com/reference/android/hardware/Camera.Parameters.html#setPreviewFormat%28int%29
163 int y_stride;
164 int uv_stride;
165 webrtc::Calc16ByteAlignedStride(width, &y_stride, &uv_stride);
166 const uint8_t* v_plane = y_plane + y_stride * height;
167 const uint8_t* u_plane =
168 v_plane + uv_stride * webrtc::AlignInt(height, 2) / 2;
169
170 // Wrap the Java buffer, and call ReturnBuffer() in the wrapped
171 // VideoFrameBuffer destructor.
172 rtc::scoped_refptr<webrtc::VideoFrameBuffer> buffer(
173 new rtc::RefCountedObject<webrtc::WrappedI420Buffer>(
174 width, height, y_plane, y_stride, u_plane, uv_stride, v_plane,
175 uv_stride,
perkjac306422015-10-08 15:32:38 +0200176 rtc::Bind(&AndroidVideoCapturerJni::ReturnBuffer, this,
177 timestamp_ns)));
Magnus Jedvertc464f502015-08-25 23:22:08 +0200178 AsyncCapturerInvoke("OnIncomingFrame",
179 &webrtc::AndroidVideoCapturer::OnIncomingFrame,
perkjac306422015-10-08 15:32:38 +0200180 buffer, rotation, timestamp_ns);
181}
182
Perc01c2542015-11-13 16:58:26 +0100183void AndroidVideoCapturerJni::OnTextureFrame(
184 int width,
185 int height,
186 int64_t timestamp_ns,
187 const NativeTextureHandleImpl& handle) {
188 // TODO(magjed): Fix this. See bug webrtc:4993.
189 RTC_NOTREACHED()
190 << "The rest of the stack for Android expects the native "
191 "handle to be a NativeHandleImpl with a SurfaceTexture, not a "
192 "NativeTextureHandleImpl";
perkjac306422015-10-08 15:32:38 +0200193 rtc::scoped_refptr<webrtc::VideoFrameBuffer> buffer(
magjed52a30e32015-10-12 06:53:20 -0700194 new rtc::RefCountedObject<AndroidTextureBuffer>(
perkjac306422015-10-08 15:32:38 +0200195 width, height, handle,
196 rtc::Bind(&AndroidVideoCapturerJni::ReturnBuffer, this,
197 timestamp_ns)));
198 AsyncCapturerInvoke("OnIncomingFrame",
199 &webrtc::AndroidVideoCapturer::OnIncomingFrame,
200 buffer, 0, timestamp_ns);
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
perkj3d06eca2015-10-08 12:53:33 +0200213JOW(void,
214 VideoCapturerAndroid_00024NativeObserver_nativeOnByteBufferFrameCaptured)
magjedb5815c82015-09-29 01:13:43 -0700215 (JNIEnv* jni, jclass, jlong j_capturer, jbyteArray j_frame, jint length,
perkjac306422015-10-08 15:32:38 +0200216 jint width, jint height, jint rotation, jlong timestamp) {
magjedb5815c82015-09-29 01:13:43 -0700217 jboolean is_copy = true;
218 jbyte* bytes = jni->GetByteArrayElements(j_frame, &is_copy);
219 // If this is a copy of the original frame, it means that the memory
220 // is not direct memory and thus VideoCapturerAndroid does not guarantee
221 // that the memory is valid when we have released |j_frame|.
222 // TODO(magjed): Move ReleaseByteArrayElements() into ReturnBuffer() and
223 // remove this check.
224 RTC_CHECK(!is_copy)
225 << "NativeObserver_nativeOnFrameCaptured: frame is a copy";
Magnus Jedvertc464f502015-08-25 23:22:08 +0200226 reinterpret_cast<AndroidVideoCapturerJni*>(j_capturer)
perkjac306422015-10-08 15:32:38 +0200227 ->OnMemoryBufferFrame(bytes, length, width, height, rotation, timestamp);
magjedb5815c82015-09-29 01:13:43 -0700228 jni->ReleaseByteArrayElements(j_frame, bytes, JNI_ABORT);
perkj@webrtc.org96e4db92015-02-13 12:46:51 +0000229}
230
perkjac306422015-10-08 15:32:38 +0200231JOW(void, VideoCapturerAndroid_00024NativeObserver_nativeOnTextureFrameCaptured)
232 (JNIEnv* jni, jclass, jlong j_capturer, jint j_width, jint j_height,
233 jint j_oes_texture_id, jfloatArray j_transform_matrix,
234 jlong j_timestamp) {
235 reinterpret_cast<AndroidVideoCapturerJni*>(j_capturer)
236 ->OnTextureFrame(j_width, j_height, j_timestamp,
Perc01c2542015-11-13 16:58:26 +0100237 NativeTextureHandleImpl(jni, j_oes_texture_id,
238 j_transform_matrix));
perkjac306422015-10-08 15:32:38 +0200239}
240
perkj@webrtc.org3db042e2015-02-19 08:43:38 +0000241JOW(void, VideoCapturerAndroid_00024NativeObserver_nativeCapturerStarted)
perkj@webrtc.org112f1272015-02-25 09:20:07 +0000242 (JNIEnv* jni, jclass, jlong j_capturer, jboolean j_success) {
Alex Glaznev8c054152015-04-20 13:00:49 -0700243 LOG(LS_INFO) << "NativeObserver_nativeCapturerStarted";
perkj@webrtc.org112f1272015-02-25 09:20:07 +0000244 reinterpret_cast<AndroidVideoCapturerJni*>(j_capturer)->OnCapturerStarted(
245 j_success);
perkj@webrtc.org96e4db92015-02-13 12:46:51 +0000246}
247
Åsa Persson2b679252015-06-15 09:53:05 +0200248JOW(void, VideoCapturerAndroid_00024NativeObserver_nativeOnOutputFormatRequest)
249 (JNIEnv* jni, jclass, jlong j_capturer, jint j_width, jint j_height,
250 jint j_fps) {
251 LOG(LS_INFO) << "NativeObserver_nativeOnOutputFormatRequest";
252 reinterpret_cast<AndroidVideoCapturerJni*>(j_capturer)->OnOutputFormatRequest(
253 j_width, j_height, j_fps);
254}
255
perkje0bce242015-10-05 16:21:54 +0200256JOW(jlong, VideoCapturerAndroid_nativeCreateVideoCapturer)
257 (JNIEnv* jni, jclass, jobject j_video_capturer) {
258 rtc::scoped_refptr<webrtc::AndroidVideoCapturerDelegate> delegate =
259 new rtc::RefCountedObject<AndroidVideoCapturerJni>(jni, j_video_capturer);
260 rtc::scoped_ptr<cricket::VideoCapturer> capturer(
261 new webrtc::AndroidVideoCapturer(delegate));
262 // Caller takes ownership of the cricket::VideoCapturer* pointer.
263 return jlongFromPointer(capturer.release());
264}
265
perkj@webrtc.org96e4db92015-02-13 12:46:51 +0000266} // namespace webrtc_jni