blob: 45396b6e8375a305681ec6ef51eaae5d83826fe1 [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
magjed962c26b2015-10-12 02:25:42 -070037namespace {
38
39class CameraTextureBuffer : public webrtc::NativeHandleBuffer {
40 public:
41 CameraTextureBuffer(int width, int height,
42 const NativeHandleImpl& native_handle,
43 const rtc::Callback0<void>& no_longer_used)
44 : webrtc::NativeHandleBuffer(&native_handle_, width, height),
45 native_handle_(native_handle),
46 no_longer_used_cb_(no_longer_used) {}
47
48 ~CameraTextureBuffer() {
49 no_longer_used_cb_();
50 }
51
52 rtc::scoped_refptr<VideoFrameBuffer> NativeToI420Buffer() override {
53 RTC_NOTREACHED()
54 << "CameraTextureBuffer::NativeToI420Buffer not implemented.";
55 return nullptr;
56 }
57
58 private:
59 NativeHandleImpl native_handle_;
60 rtc::Callback0<void> no_longer_used_cb_;
61};
62
63} // anonymous namespace
64
perkj@webrtc.org96e4db92015-02-13 12:46:51 +000065jobject AndroidVideoCapturerJni::application_context_ = nullptr;
66
67// static
68int AndroidVideoCapturerJni::SetAndroidObjects(JNIEnv* jni,
69 jobject appliction_context) {
70 if (application_context_) {
71 jni->DeleteGlobalRef(application_context_);
72 }
73 application_context_ = NewGlobalRef(jni, appliction_context);
74
75 return 0;
76}
77
78AndroidVideoCapturerJni::AndroidVideoCapturerJni(JNIEnv* jni,
79 jobject j_video_capturer)
80 : j_capturer_global_(jni, j_video_capturer),
81 j_video_capturer_class_(
82 jni, FindClass(jni, "org/webrtc/VideoCapturerAndroid")),
perkj@webrtc.org3db042e2015-02-19 08:43:38 +000083 j_observer_class_(
perkj@webrtc.org96e4db92015-02-13 12:46:51 +000084 jni,
85 FindClass(jni,
perkj@webrtc.org3db042e2015-02-19 08:43:38 +000086 "org/webrtc/VideoCapturerAndroid$NativeObserver")),
Magnus Jedvertc464f502015-08-25 23:22:08 +020087 capturer_(nullptr) {
Alex Glaznev8c054152015-04-20 13:00:49 -070088 LOG(LS_INFO) << "AndroidVideoCapturerJni ctor";
perkj@webrtc.org3db042e2015-02-19 08:43:38 +000089 thread_checker_.DetachFromThread();
90}
perkj@webrtc.org96e4db92015-02-13 12:46:51 +000091
perkj@webrtc.org3db042e2015-02-19 08:43:38 +000092AndroidVideoCapturerJni::~AndroidVideoCapturerJni() {
Magnus Jedvertc464f502015-08-25 23:22:08 +020093 LOG(LS_INFO) << "AndroidVideoCapturerJni dtor";
Magnus Jedvertf706c8a2015-09-23 12:01:28 +020094 jni()->CallVoidMethod(
95 *j_capturer_global_,
96 GetMethodID(jni(), *j_video_capturer_class_, "release", "()V"));
97 CHECK_EXCEPTION(jni()) << "error during VideoCapturerAndroid.release()";
perkj@webrtc.org3db042e2015-02-19 08:43:38 +000098}
perkj@webrtc.org96e4db92015-02-13 12:46:51 +000099
100void AndroidVideoCapturerJni::Start(int width, int height, int framerate,
101 webrtc::AndroidVideoCapturer* capturer) {
Alex Glaznev8c054152015-04-20 13:00:49 -0700102 LOG(LS_INFO) << "AndroidVideoCapturerJni start";
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_);
henrikg91d6ede2015-09-17 00:24:34 -0700106 RTC_CHECK(capturer_ == nullptr);
107 RTC_CHECK(invoker_.get() == nullptr);
Magnus Jedvertc464f502015-08-25 23:22:08 +0200108 capturer_ = capturer;
109 invoker_.reset(new rtc::GuardedAsyncInvoker());
110 }
111 jobject j_frame_observer =
perkj@webrtc.org3db042e2015-02-19 08:43:38 +0000112 jni()->NewObject(*j_observer_class_,
Magnus Jedvertc464f502015-08-25 23:22:08 +0200113 GetMethodID(jni(), *j_observer_class_, "<init>", "(J)V"),
114 jlongFromPointer(this));
perkj@webrtc.org96e4db92015-02-13 12:46:51 +0000115 CHECK_EXCEPTION(jni()) << "error during NewObject";
116
117 jmethodID m = GetMethodID(
118 jni(), *j_video_capturer_class_, "startCapture",
119 "(IIILandroid/content/Context;"
120 "Lorg/webrtc/VideoCapturerAndroid$CapturerObserver;)V");
121 jni()->CallVoidMethod(*j_capturer_global_,
122 m, width, height,
123 framerate,
124 application_context_,
Magnus Jedvertc464f502015-08-25 23:22:08 +0200125 j_frame_observer);
perkj@webrtc.org96e4db92015-02-13 12:46:51 +0000126 CHECK_EXCEPTION(jni()) << "error during VideoCapturerAndroid.startCapture";
127}
128
perkj@webrtc.org3db042e2015-02-19 08:43:38 +0000129void AndroidVideoCapturerJni::Stop() {
Alex Glaznev8c054152015-04-20 13:00:49 -0700130 LOG(LS_INFO) << "AndroidVideoCapturerJni stop";
henrikg91d6ede2015-09-17 00:24:34 -0700131 RTC_DCHECK(thread_checker_.CalledOnValidThread());
Magnus Jedvertc464f502015-08-25 23:22:08 +0200132 {
133 rtc::CritScope cs(&capturer_lock_);
134 // Destroying |invoker_| will cancel all pending calls to |capturer_|.
135 invoker_ = nullptr;
136 capturer_ = nullptr;
137 }
perkj@webrtc.org96e4db92015-02-13 12:46:51 +0000138 jmethodID m = GetMethodID(jni(), *j_video_capturer_class_,
perkj@webrtc.org3db042e2015-02-19 08:43:38 +0000139 "stopCapture", "()V");
140 jni()->CallVoidMethod(*j_capturer_global_, m);
perkj@webrtc.org96e4db92015-02-13 12:46:51 +0000141 CHECK_EXCEPTION(jni()) << "error during VideoCapturerAndroid.stopCapture";
Alex Glaznev8c054152015-04-20 13:00:49 -0700142 LOG(LS_INFO) << "AndroidVideoCapturerJni stop done";
perkj@webrtc.org112f1272015-02-25 09:20:07 +0000143}
144
Magnus Jedvertc464f502015-08-25 23:22:08 +0200145template <typename... Args>
146void AndroidVideoCapturerJni::AsyncCapturerInvoke(
147 const char* method_name,
148 void (webrtc::AndroidVideoCapturer::*method)(Args...),
149 Args... args) {
150 rtc::CritScope cs(&capturer_lock_);
151 if (!invoker_) {
152 LOG(LS_WARNING) << method_name << "() called for closed capturer.";
Alex Glaznevc4905fb2015-04-20 16:54:42 -0700153 return;
154 }
Magnus Jedvertc464f502015-08-25 23:22:08 +0200155 invoker_->AsyncInvoke<void>(rtc::Bind(method, capturer_, args...));
156}
157
Peter Boström0c4e06b2015-10-07 12:23:21 +0200158void AndroidVideoCapturerJni::ReturnBuffer(int64_t time_stamp) {
perkj@webrtc.org112f1272015-02-25 09:20:07 +0000159 jmethodID m = GetMethodID(jni(), *j_video_capturer_class_,
160 "returnBuffer", "(J)V");
161 jni()->CallVoidMethod(*j_capturer_global_, m, time_stamp);
Per33544192015-04-02 12:30:51 +0200162 CHECK_EXCEPTION(jni()) << "error during VideoCapturerAndroid.returnBuffer";
perkj@webrtc.org96e4db92015-02-13 12:46:51 +0000163}
164
165std::string AndroidVideoCapturerJni::GetSupportedFormats() {
166 jmethodID m =
167 GetMethodID(jni(), *j_video_capturer_class_,
168 "getSupportedFormatsAsJson", "()Ljava/lang/String;");
169 jstring j_json_caps =
170 (jstring) jni()->CallObjectMethod(*j_capturer_global_, m);
171 CHECK_EXCEPTION(jni()) << "error during supportedFormatsAsJson";
172 return JavaToStdString(jni(), j_json_caps);
173}
174
perkj@webrtc.org112f1272015-02-25 09:20:07 +0000175void AndroidVideoCapturerJni::OnCapturerStarted(bool success) {
Magnus Jedvertc464f502015-08-25 23:22:08 +0200176 LOG(LS_INFO) << "AndroidVideoCapturerJni capture started: " << success;
177 AsyncCapturerInvoke("OnCapturerStarted",
178 &webrtc::AndroidVideoCapturer::OnCapturerStarted,
179 success);
perkj@webrtc.org112f1272015-02-25 09:20:07 +0000180}
181
perkjac306422015-10-08 15:32:38 +0200182void AndroidVideoCapturerJni::OnMemoryBufferFrame(void* video_frame,
183 int length,
184 int width,
185 int height,
186 int rotation,
187 int64_t timestamp_ns) {
magjedb5815c82015-09-29 01:13:43 -0700188 const uint8_t* y_plane = static_cast<uint8_t*>(video_frame);
Magnus Jedvertc464f502015-08-25 23:22:08 +0200189 // Android guarantees that the stride is a multiple of 16.
190 // http://developer.android.com/reference/android/hardware/Camera.Parameters.html#setPreviewFormat%28int%29
191 int y_stride;
192 int uv_stride;
193 webrtc::Calc16ByteAlignedStride(width, &y_stride, &uv_stride);
194 const uint8_t* v_plane = y_plane + y_stride * height;
195 const uint8_t* u_plane =
196 v_plane + uv_stride * webrtc::AlignInt(height, 2) / 2;
197
198 // Wrap the Java buffer, and call ReturnBuffer() in the wrapped
199 // VideoFrameBuffer destructor.
200 rtc::scoped_refptr<webrtc::VideoFrameBuffer> buffer(
201 new rtc::RefCountedObject<webrtc::WrappedI420Buffer>(
202 width, height, y_plane, y_stride, u_plane, uv_stride, v_plane,
203 uv_stride,
perkjac306422015-10-08 15:32:38 +0200204 rtc::Bind(&AndroidVideoCapturerJni::ReturnBuffer, this,
205 timestamp_ns)));
Magnus Jedvertc464f502015-08-25 23:22:08 +0200206 AsyncCapturerInvoke("OnIncomingFrame",
207 &webrtc::AndroidVideoCapturer::OnIncomingFrame,
perkjac306422015-10-08 15:32:38 +0200208 buffer, rotation, timestamp_ns);
209}
210
211void AndroidVideoCapturerJni::OnTextureFrame(int width,
212 int height,
213 int64_t timestamp_ns,
214 const NativeHandleImpl& handle) {
215 rtc::scoped_refptr<webrtc::VideoFrameBuffer> buffer(
magjed962c26b2015-10-12 02:25:42 -0700216 new rtc::RefCountedObject<CameraTextureBuffer>(
perkjac306422015-10-08 15:32:38 +0200217 width, height, handle,
218 rtc::Bind(&AndroidVideoCapturerJni::ReturnBuffer, this,
219 timestamp_ns)));
220 AsyncCapturerInvoke("OnIncomingFrame",
221 &webrtc::AndroidVideoCapturer::OnIncomingFrame,
222 buffer, 0, timestamp_ns);
perkj@webrtc.org112f1272015-02-25 09:20:07 +0000223}
224
Åsa Persson2b679252015-06-15 09:53:05 +0200225void AndroidVideoCapturerJni::OnOutputFormatRequest(int width,
226 int height,
227 int fps) {
Magnus Jedvertc464f502015-08-25 23:22:08 +0200228 AsyncCapturerInvoke("OnOutputFormatRequest",
229 &webrtc::AndroidVideoCapturer::OnOutputFormatRequest,
230 width, height, fps);
Åsa Persson2b679252015-06-15 09:53:05 +0200231}
232
perkj@webrtc.org96e4db92015-02-13 12:46:51 +0000233JNIEnv* AndroidVideoCapturerJni::jni() { return AttachCurrentThreadIfNeeded(); }
234
perkj3d06eca2015-10-08 12:53:33 +0200235JOW(void,
236 VideoCapturerAndroid_00024NativeObserver_nativeOnByteBufferFrameCaptured)
magjedb5815c82015-09-29 01:13:43 -0700237 (JNIEnv* jni, jclass, jlong j_capturer, jbyteArray j_frame, jint length,
perkjac306422015-10-08 15:32:38 +0200238 jint width, jint height, jint rotation, jlong timestamp) {
magjedb5815c82015-09-29 01:13:43 -0700239 jboolean is_copy = true;
240 jbyte* bytes = jni->GetByteArrayElements(j_frame, &is_copy);
241 // If this is a copy of the original frame, it means that the memory
242 // is not direct memory and thus VideoCapturerAndroid does not guarantee
243 // that the memory is valid when we have released |j_frame|.
244 // TODO(magjed): Move ReleaseByteArrayElements() into ReturnBuffer() and
245 // remove this check.
246 RTC_CHECK(!is_copy)
247 << "NativeObserver_nativeOnFrameCaptured: frame is a copy";
Magnus Jedvertc464f502015-08-25 23:22:08 +0200248 reinterpret_cast<AndroidVideoCapturerJni*>(j_capturer)
perkjac306422015-10-08 15:32:38 +0200249 ->OnMemoryBufferFrame(bytes, length, width, height, rotation, timestamp);
magjedb5815c82015-09-29 01:13:43 -0700250 jni->ReleaseByteArrayElements(j_frame, bytes, JNI_ABORT);
perkj@webrtc.org96e4db92015-02-13 12:46:51 +0000251}
252
perkjac306422015-10-08 15:32:38 +0200253JOW(void, VideoCapturerAndroid_00024NativeObserver_nativeOnTextureFrameCaptured)
254 (JNIEnv* jni, jclass, jlong j_capturer, jint j_width, jint j_height,
255 jint j_oes_texture_id, jfloatArray j_transform_matrix,
256 jlong j_timestamp) {
257 reinterpret_cast<AndroidVideoCapturerJni*>(j_capturer)
258 ->OnTextureFrame(j_width, j_height, j_timestamp,
259 NativeHandleImpl(jni, j_oes_texture_id,
260 j_transform_matrix));
261}
262
perkj@webrtc.org3db042e2015-02-19 08:43:38 +0000263JOW(void, VideoCapturerAndroid_00024NativeObserver_nativeCapturerStarted)
perkj@webrtc.org112f1272015-02-25 09:20:07 +0000264 (JNIEnv* jni, jclass, jlong j_capturer, jboolean j_success) {
Alex Glaznev8c054152015-04-20 13:00:49 -0700265 LOG(LS_INFO) << "NativeObserver_nativeCapturerStarted";
perkj@webrtc.org112f1272015-02-25 09:20:07 +0000266 reinterpret_cast<AndroidVideoCapturerJni*>(j_capturer)->OnCapturerStarted(
267 j_success);
perkj@webrtc.org96e4db92015-02-13 12:46:51 +0000268}
269
Åsa Persson2b679252015-06-15 09:53:05 +0200270JOW(void, VideoCapturerAndroid_00024NativeObserver_nativeOnOutputFormatRequest)
271 (JNIEnv* jni, jclass, jlong j_capturer, jint j_width, jint j_height,
272 jint j_fps) {
273 LOG(LS_INFO) << "NativeObserver_nativeOnOutputFormatRequest";
274 reinterpret_cast<AndroidVideoCapturerJni*>(j_capturer)->OnOutputFormatRequest(
275 j_width, j_height, j_fps);
276}
277
perkje0bce242015-10-05 16:21:54 +0200278JOW(jlong, VideoCapturerAndroid_nativeCreateVideoCapturer)
279 (JNIEnv* jni, jclass, jobject j_video_capturer) {
280 rtc::scoped_refptr<webrtc::AndroidVideoCapturerDelegate> delegate =
281 new rtc::RefCountedObject<AndroidVideoCapturerJni>(jni, j_video_capturer);
282 rtc::scoped_ptr<cricket::VideoCapturer> capturer(
283 new webrtc::AndroidVideoCapturer(delegate));
284 // Caller takes ownership of the cricket::VideoCapturer* pointer.
285 return jlongFromPointer(capturer.release());
286}
287
perkj@webrtc.org96e4db92015-02-13 12:46:51 +0000288} // namespace webrtc_jni