blob: 8813c89de4eb7148910b217e8fe3b93febb674a0 [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"
perkj88518a22015-12-18 00:37:06 -080032#include "talk/app/webrtc/java/jni/surfacetexturehelper_jni.h"
33#include "third_party/libyuv/include/libyuv/convert.h"
perkj@webrtc.org3db042e2015-02-19 08:43:38 +000034#include "webrtc/base/bind.h"
perkj@webrtc.org96e4db92015-02-13 12:46:51 +000035
36namespace webrtc_jni {
37
38jobject AndroidVideoCapturerJni::application_context_ = nullptr;
39
40// static
41int AndroidVideoCapturerJni::SetAndroidObjects(JNIEnv* jni,
42 jobject appliction_context) {
43 if (application_context_) {
44 jni->DeleteGlobalRef(application_context_);
45 }
46 application_context_ = NewGlobalRef(jni, appliction_context);
47
48 return 0;
49}
50
nissec490e012015-12-10 06:23:33 -080051AndroidVideoCapturerJni::AndroidVideoCapturerJni(
52 JNIEnv* jni,
53 jobject j_video_capturer,
54 jobject j_surface_texture_helper)
55 : j_video_capturer_(jni, j_video_capturer),
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")),
perkj88518a22015-12-18 00:37:06 -080062 surface_texture_helper_(new rtc::RefCountedObject<SurfaceTextureHelper>(
63 jni, j_surface_texture_helper)),
Magnus Jedvertc464f502015-08-25 23:22:08 +020064 capturer_(nullptr) {
Alex Glaznev8c054152015-04-20 13:00:49 -070065 LOG(LS_INFO) << "AndroidVideoCapturerJni ctor";
perkj@webrtc.org3db042e2015-02-19 08:43:38 +000066 thread_checker_.DetachFromThread();
67}
perkj@webrtc.org96e4db92015-02-13 12:46:51 +000068
perkj@webrtc.org3db042e2015-02-19 08:43:38 +000069AndroidVideoCapturerJni::~AndroidVideoCapturerJni() {
Magnus Jedvertc464f502015-08-25 23:22:08 +020070 LOG(LS_INFO) << "AndroidVideoCapturerJni dtor";
Magnus Jedvertf706c8a2015-09-23 12:01:28 +020071 jni()->CallVoidMethod(
nissec490e012015-12-10 06:23:33 -080072 *j_video_capturer_,
Magnus Jedvertf706c8a2015-09-23 12:01:28 +020073 GetMethodID(jni(), *j_video_capturer_class_, "release", "()V"));
74 CHECK_EXCEPTION(jni()) << "error during VideoCapturerAndroid.release()";
perkj@webrtc.org3db042e2015-02-19 08:43:38 +000075}
perkj@webrtc.org96e4db92015-02-13 12:46:51 +000076
77void AndroidVideoCapturerJni::Start(int width, int height, int framerate,
78 webrtc::AndroidVideoCapturer* capturer) {
Alex Glaznev8c054152015-04-20 13:00:49 -070079 LOG(LS_INFO) << "AndroidVideoCapturerJni start";
henrikg91d6ede2015-09-17 00:24:34 -070080 RTC_DCHECK(thread_checker_.CalledOnValidThread());
Magnus Jedvertc464f502015-08-25 23:22:08 +020081 {
82 rtc::CritScope cs(&capturer_lock_);
henrikg91d6ede2015-09-17 00:24:34 -070083 RTC_CHECK(capturer_ == nullptr);
84 RTC_CHECK(invoker_.get() == nullptr);
Magnus Jedvertc464f502015-08-25 23:22:08 +020085 capturer_ = capturer;
86 invoker_.reset(new rtc::GuardedAsyncInvoker());
87 }
88 jobject j_frame_observer =
perkj@webrtc.org3db042e2015-02-19 08:43:38 +000089 jni()->NewObject(*j_observer_class_,
Magnus Jedvertc464f502015-08-25 23:22:08 +020090 GetMethodID(jni(), *j_observer_class_, "<init>", "(J)V"),
91 jlongFromPointer(this));
perkj@webrtc.org96e4db92015-02-13 12:46:51 +000092 CHECK_EXCEPTION(jni()) << "error during NewObject";
93
94 jmethodID m = GetMethodID(
95 jni(), *j_video_capturer_class_, "startCapture",
96 "(IIILandroid/content/Context;"
97 "Lorg/webrtc/VideoCapturerAndroid$CapturerObserver;)V");
nissec490e012015-12-10 06:23:33 -080098 jni()->CallVoidMethod(*j_video_capturer_,
perkj@webrtc.org96e4db92015-02-13 12:46:51 +000099 m, width, height,
100 framerate,
101 application_context_,
Magnus Jedvertc464f502015-08-25 23:22:08 +0200102 j_frame_observer);
perkj@webrtc.org96e4db92015-02-13 12:46:51 +0000103 CHECK_EXCEPTION(jni()) << "error during VideoCapturerAndroid.startCapture";
104}
105
perkj@webrtc.org3db042e2015-02-19 08:43:38 +0000106void AndroidVideoCapturerJni::Stop() {
Alex Glaznev8c054152015-04-20 13:00:49 -0700107 LOG(LS_INFO) << "AndroidVideoCapturerJni stop";
henrikg91d6ede2015-09-17 00:24:34 -0700108 RTC_DCHECK(thread_checker_.CalledOnValidThread());
Magnus Jedvertc464f502015-08-25 23:22:08 +0200109 {
110 rtc::CritScope cs(&capturer_lock_);
111 // Destroying |invoker_| will cancel all pending calls to |capturer_|.
112 invoker_ = nullptr;
113 capturer_ = nullptr;
114 }
perkj@webrtc.org96e4db92015-02-13 12:46:51 +0000115 jmethodID m = GetMethodID(jni(), *j_video_capturer_class_,
perkj@webrtc.org3db042e2015-02-19 08:43:38 +0000116 "stopCapture", "()V");
nissec490e012015-12-10 06:23:33 -0800117 jni()->CallVoidMethod(*j_video_capturer_, m);
perkj@webrtc.org96e4db92015-02-13 12:46:51 +0000118 CHECK_EXCEPTION(jni()) << "error during VideoCapturerAndroid.stopCapture";
Alex Glaznev8c054152015-04-20 13:00:49 -0700119 LOG(LS_INFO) << "AndroidVideoCapturerJni stop done";
perkj@webrtc.org112f1272015-02-25 09:20:07 +0000120}
121
Magnus Jedvertc464f502015-08-25 23:22:08 +0200122template <typename... Args>
123void AndroidVideoCapturerJni::AsyncCapturerInvoke(
124 const char* method_name,
125 void (webrtc::AndroidVideoCapturer::*method)(Args...),
olka30a5b5e2015-10-20 11:04:56 -0700126 typename Identity<Args>::type... args) {
Magnus Jedvertc464f502015-08-25 23:22:08 +0200127 rtc::CritScope cs(&capturer_lock_);
128 if (!invoker_) {
129 LOG(LS_WARNING) << method_name << "() called for closed capturer.";
Alex Glaznevc4905fb2015-04-20 16:54:42 -0700130 return;
131 }
Magnus Jedvertc464f502015-08-25 23:22:08 +0200132 invoker_->AsyncInvoke<void>(rtc::Bind(method, capturer_, args...));
133}
134
perkj@webrtc.org96e4db92015-02-13 12:46:51 +0000135std::string AndroidVideoCapturerJni::GetSupportedFormats() {
136 jmethodID m =
137 GetMethodID(jni(), *j_video_capturer_class_,
138 "getSupportedFormatsAsJson", "()Ljava/lang/String;");
139 jstring j_json_caps =
nissec490e012015-12-10 06:23:33 -0800140 (jstring) jni()->CallObjectMethod(*j_video_capturer_, m);
perkj@webrtc.org96e4db92015-02-13 12:46:51 +0000141 CHECK_EXCEPTION(jni()) << "error during supportedFormatsAsJson";
142 return JavaToStdString(jni(), j_json_caps);
143}
144
perkj@webrtc.org112f1272015-02-25 09:20:07 +0000145void AndroidVideoCapturerJni::OnCapturerStarted(bool success) {
Magnus Jedvertc464f502015-08-25 23:22:08 +0200146 LOG(LS_INFO) << "AndroidVideoCapturerJni capture started: " << success;
147 AsyncCapturerInvoke("OnCapturerStarted",
148 &webrtc::AndroidVideoCapturer::OnCapturerStarted,
149 success);
perkj@webrtc.org112f1272015-02-25 09:20:07 +0000150}
151
perkjac306422015-10-08 15:32:38 +0200152void AndroidVideoCapturerJni::OnMemoryBufferFrame(void* video_frame,
153 int length,
154 int width,
155 int height,
156 int rotation,
157 int64_t timestamp_ns) {
magjedb5815c82015-09-29 01:13:43 -0700158 const uint8_t* y_plane = static_cast<uint8_t*>(video_frame);
perkj88518a22015-12-18 00:37:06 -0800159 const uint8_t* vu_plane = y_plane + width * height;
Magnus Jedvertc464f502015-08-25 23:22:08 +0200160
perkj88518a22015-12-18 00:37:06 -0800161 rtc::scoped_refptr<webrtc::VideoFrameBuffer> buffer =
162 buffer_pool_.CreateBuffer(width, height);
163 libyuv::NV21ToI420(
164 y_plane, width,
165 vu_plane, width,
166 buffer->MutableData(webrtc::kYPlane), buffer->stride(webrtc::kYPlane),
167 buffer->MutableData(webrtc::kUPlane), buffer->stride(webrtc::kUPlane),
168 buffer->MutableData(webrtc::kVPlane), buffer->stride(webrtc::kVPlane),
169 width, height);
Magnus Jedvertc464f502015-08-25 23:22:08 +0200170 AsyncCapturerInvoke("OnIncomingFrame",
171 &webrtc::AndroidVideoCapturer::OnIncomingFrame,
perkjac306422015-10-08 15:32:38 +0200172 buffer, rotation, timestamp_ns);
173}
174
Per488e75f2015-11-19 10:43:36 +0100175void AndroidVideoCapturerJni::OnTextureFrame(int width,
176 int height,
Per71f5a9a2015-12-11 09:32:37 +0100177 int rotation,
Per488e75f2015-11-19 10:43:36 +0100178 int64_t timestamp_ns,
179 const NativeHandleImpl& handle) {
perkjac306422015-10-08 15:32:38 +0200180 rtc::scoped_refptr<webrtc::VideoFrameBuffer> buffer(
perkj88518a22015-12-18 00:37:06 -0800181 surface_texture_helper_->CreateTextureFrame(width, height, handle));
182
perkjac306422015-10-08 15:32:38 +0200183 AsyncCapturerInvoke("OnIncomingFrame",
184 &webrtc::AndroidVideoCapturer::OnIncomingFrame,
Per71f5a9a2015-12-11 09:32:37 +0100185 buffer, rotation, timestamp_ns);
perkj@webrtc.org112f1272015-02-25 09:20:07 +0000186}
187
Åsa Persson2b679252015-06-15 09:53:05 +0200188void AndroidVideoCapturerJni::OnOutputFormatRequest(int width,
189 int height,
190 int fps) {
Magnus Jedvertc464f502015-08-25 23:22:08 +0200191 AsyncCapturerInvoke("OnOutputFormatRequest",
192 &webrtc::AndroidVideoCapturer::OnOutputFormatRequest,
193 width, height, fps);
Åsa Persson2b679252015-06-15 09:53:05 +0200194}
195
perkj@webrtc.org96e4db92015-02-13 12:46:51 +0000196JNIEnv* AndroidVideoCapturerJni::jni() { return AttachCurrentThreadIfNeeded(); }
197
perkj3d06eca2015-10-08 12:53:33 +0200198JOW(void,
199 VideoCapturerAndroid_00024NativeObserver_nativeOnByteBufferFrameCaptured)
magjedb5815c82015-09-29 01:13:43 -0700200 (JNIEnv* jni, jclass, jlong j_capturer, jbyteArray j_frame, jint length,
perkjac306422015-10-08 15:32:38 +0200201 jint width, jint height, jint rotation, jlong timestamp) {
magjedb5815c82015-09-29 01:13:43 -0700202 jboolean is_copy = true;
203 jbyte* bytes = jni->GetByteArrayElements(j_frame, &is_copy);
Magnus Jedvertc464f502015-08-25 23:22:08 +0200204 reinterpret_cast<AndroidVideoCapturerJni*>(j_capturer)
perkjac306422015-10-08 15:32:38 +0200205 ->OnMemoryBufferFrame(bytes, length, width, height, rotation, timestamp);
magjedb5815c82015-09-29 01:13:43 -0700206 jni->ReleaseByteArrayElements(j_frame, bytes, JNI_ABORT);
perkj@webrtc.org96e4db92015-02-13 12:46:51 +0000207}
208
perkjac306422015-10-08 15:32:38 +0200209JOW(void, VideoCapturerAndroid_00024NativeObserver_nativeOnTextureFrameCaptured)
210 (JNIEnv* jni, jclass, jlong j_capturer, jint j_width, jint j_height,
211 jint j_oes_texture_id, jfloatArray j_transform_matrix,
Per71f5a9a2015-12-11 09:32:37 +0100212 jint j_rotation, jlong j_timestamp) {
perkjac306422015-10-08 15:32:38 +0200213 reinterpret_cast<AndroidVideoCapturerJni*>(j_capturer)
Per71f5a9a2015-12-11 09:32:37 +0100214 ->OnTextureFrame(j_width, j_height, j_rotation, j_timestamp,
Per488e75f2015-11-19 10:43:36 +0100215 NativeHandleImpl(jni, j_oes_texture_id,
216 j_transform_matrix));
perkjac306422015-10-08 15:32:38 +0200217}
218
perkj@webrtc.org3db042e2015-02-19 08:43:38 +0000219JOW(void, VideoCapturerAndroid_00024NativeObserver_nativeCapturerStarted)
perkj@webrtc.org112f1272015-02-25 09:20:07 +0000220 (JNIEnv* jni, jclass, jlong j_capturer, jboolean j_success) {
Alex Glaznev8c054152015-04-20 13:00:49 -0700221 LOG(LS_INFO) << "NativeObserver_nativeCapturerStarted";
perkj@webrtc.org112f1272015-02-25 09:20:07 +0000222 reinterpret_cast<AndroidVideoCapturerJni*>(j_capturer)->OnCapturerStarted(
223 j_success);
perkj@webrtc.org96e4db92015-02-13 12:46:51 +0000224}
225
Åsa Persson2b679252015-06-15 09:53:05 +0200226JOW(void, VideoCapturerAndroid_00024NativeObserver_nativeOnOutputFormatRequest)
227 (JNIEnv* jni, jclass, jlong j_capturer, jint j_width, jint j_height,
228 jint j_fps) {
229 LOG(LS_INFO) << "NativeObserver_nativeOnOutputFormatRequest";
230 reinterpret_cast<AndroidVideoCapturerJni*>(j_capturer)->OnOutputFormatRequest(
231 j_width, j_height, j_fps);
232}
233
perkje0bce242015-10-05 16:21:54 +0200234JOW(jlong, VideoCapturerAndroid_nativeCreateVideoCapturer)
nissec490e012015-12-10 06:23:33 -0800235 (JNIEnv* jni, jclass,
236 jobject j_video_capturer, jobject j_surface_texture_helper) {
perkje0bce242015-10-05 16:21:54 +0200237 rtc::scoped_refptr<webrtc::AndroidVideoCapturerDelegate> delegate =
nissec490e012015-12-10 06:23:33 -0800238 new rtc::RefCountedObject<AndroidVideoCapturerJni>(
239 jni, j_video_capturer, j_surface_texture_helper);
perkje0bce242015-10-05 16:21:54 +0200240 rtc::scoped_ptr<cricket::VideoCapturer> capturer(
241 new webrtc::AndroidVideoCapturer(delegate));
242 // Caller takes ownership of the cricket::VideoCapturer* pointer.
243 return jlongFromPointer(capturer.release());
244}
245
perkj@webrtc.org96e4db92015-02-13 12:46:51 +0000246} // namespace webrtc_jni