blob: 3f3b7a6caddc4b446e4271dd824fcd5f537c195c [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"
perkj@webrtc.org96e4db92015-02-13 12:46:51 +000032
33namespace webrtc_jni {
34
35jobject AndroidVideoCapturerJni::application_context_ = nullptr;
36
37// static
38int AndroidVideoCapturerJni::SetAndroidObjects(JNIEnv* jni,
39 jobject appliction_context) {
40 if (application_context_) {
41 jni->DeleteGlobalRef(application_context_);
42 }
43 application_context_ = NewGlobalRef(jni, appliction_context);
44
45 return 0;
46}
47
perkj@webrtc.org112f1272015-02-25 09:20:07 +000048// static
Per33544192015-04-02 12:30:51 +020049rtc::scoped_refptr<AndroidVideoCapturerJni>
perkj@webrtc.org112f1272015-02-25 09:20:07 +000050AndroidVideoCapturerJni::Create(JNIEnv* jni,
51 jobject j_video_capture,
52 jstring device_name) {
Alex Glaznev8c054152015-04-20 13:00:49 -070053 LOG(LS_INFO) << "AndroidVideoCapturerJni::Create";
Per33544192015-04-02 12:30:51 +020054 rtc::scoped_refptr<AndroidVideoCapturerJni> capturer(
55 new rtc::RefCountedObject<AndroidVideoCapturerJni>(jni, j_video_capture));
perkj@webrtc.org112f1272015-02-25 09:20:07 +000056
Alex Glaznev2f5be9a2015-05-19 10:56:32 -070057 if (capturer->Init(device_name)) {
Per33544192015-04-02 12:30:51 +020058 return capturer;
Alex Glaznev2f5be9a2015-05-19 10:56:32 -070059 }
60 LOG(LS_ERROR) << "AndroidVideoCapturerJni init fails";
perkj@webrtc.org112f1272015-02-25 09:20:07 +000061 return nullptr;
62}
63
perkj@webrtc.org96e4db92015-02-13 12:46:51 +000064AndroidVideoCapturerJni::AndroidVideoCapturerJni(JNIEnv* jni,
65 jobject j_video_capturer)
66 : j_capturer_global_(jni, j_video_capturer),
67 j_video_capturer_class_(
68 jni, FindClass(jni, "org/webrtc/VideoCapturerAndroid")),
perkj@webrtc.org3db042e2015-02-19 08:43:38 +000069 j_observer_class_(
perkj@webrtc.org96e4db92015-02-13 12:46:51 +000070 jni,
71 FindClass(jni,
perkj@webrtc.org3db042e2015-02-19 08:43:38 +000072 "org/webrtc/VideoCapturerAndroid$NativeObserver")),
Alex Glaznevc4905fb2015-04-20 16:54:42 -070073 capturer_(nullptr),
74 thread_(nullptr),
75 valid_global_refs_(true) {
Alex Glaznev8c054152015-04-20 13:00:49 -070076 LOG(LS_INFO) << "AndroidVideoCapturerJni ctor";
perkj@webrtc.org3db042e2015-02-19 08:43:38 +000077 thread_checker_.DetachFromThread();
78}
perkj@webrtc.org96e4db92015-02-13 12:46:51 +000079
perkj@webrtc.org3db042e2015-02-19 08:43:38 +000080bool AndroidVideoCapturerJni::Init(jstring device_name) {
81 const jmethodID m(GetMethodID(
82 jni(), *j_video_capturer_class_, "init", "(Ljava/lang/String;)Z"));
83 if (!jni()->CallBooleanMethod(*j_capturer_global_, m, device_name)) {
84 return false;
85 }
86 CHECK_EXCEPTION(jni()) << "error during CallVoidMethod";
87 return true;
88}
89
90AndroidVideoCapturerJni::~AndroidVideoCapturerJni() {
Alex Glaznevc4905fb2015-04-20 16:54:42 -070091 valid_global_refs_ = false;
92 if (thread_ != nullptr) {
93 LOG(LS_INFO) << "AndroidVideoCapturerJni dtor - flush invoker";
94 invoker_.Flush(thread_);
95 }
96 LOG(LS_INFO) << "AndroidVideoCapturerJni dtor done";
perkj@webrtc.org3db042e2015-02-19 08:43:38 +000097}
perkj@webrtc.org96e4db92015-02-13 12:46:51 +000098
99void AndroidVideoCapturerJni::Start(int width, int height, int framerate,
100 webrtc::AndroidVideoCapturer* capturer) {
Alex Glaznev8c054152015-04-20 13:00:49 -0700101 LOG(LS_INFO) << "AndroidVideoCapturerJni start";
102 CHECK(thread_checker_.CalledOnValidThread());
103 CHECK(capturer_ == nullptr);
perkj@webrtc.org112f1272015-02-25 09:20:07 +0000104 thread_ = rtc::Thread::Current();
105 capturer_ = capturer;
perkj@webrtc.org3db042e2015-02-19 08:43:38 +0000106
perkj@webrtc.org96e4db92015-02-13 12:46:51 +0000107 j_frame_observer_ = NewGlobalRef(
108 jni(),
perkj@webrtc.org3db042e2015-02-19 08:43:38 +0000109 jni()->NewObject(*j_observer_class_,
perkj@webrtc.org96e4db92015-02-13 12:46:51 +0000110 GetMethodID(jni(),
perkj@webrtc.org3db042e2015-02-19 08:43:38 +0000111 *j_observer_class_,
perkj@webrtc.org96e4db92015-02-13 12:46:51 +0000112 "<init>",
113 "(J)V"),
perkj@webrtc.org112f1272015-02-25 09:20:07 +0000114 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_,
125 j_frame_observer_);
126 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";
131 CHECK(thread_checker_.CalledOnValidThread());
perkj@webrtc.org112f1272015-02-25 09:20:07 +0000132 capturer_ = nullptr;
perkj@webrtc.org96e4db92015-02-13 12:46:51 +0000133 jmethodID m = GetMethodID(jni(), *j_video_capturer_class_,
perkj@webrtc.org3db042e2015-02-19 08:43:38 +0000134 "stopCapture", "()V");
135 jni()->CallVoidMethod(*j_capturer_global_, m);
perkj@webrtc.org96e4db92015-02-13 12:46:51 +0000136 CHECK_EXCEPTION(jni()) << "error during VideoCapturerAndroid.stopCapture";
137 DeleteGlobalRef(jni(), j_frame_observer_);
Alex Glaznev8c054152015-04-20 13:00:49 -0700138 LOG(LS_INFO) << "AndroidVideoCapturerJni stop done";
perkj@webrtc.org112f1272015-02-25 09:20:07 +0000139}
140
141void AndroidVideoCapturerJni::ReturnBuffer(int64 time_stamp) {
Per33544192015-04-02 12:30:51 +0200142 invoker_.AsyncInvoke<void>(
143 thread_,
144 rtc::Bind(&AndroidVideoCapturerJni::ReturnBuffer_w, this, time_stamp));
145}
146
147void AndroidVideoCapturerJni::ReturnBuffer_w(int64 time_stamp) {
Alex Glaznevc4905fb2015-04-20 16:54:42 -0700148 if (!valid_global_refs_) {
149 LOG(LS_ERROR) << "ReturnBuffer_w is called for invalid global refs.";
150 return;
151 }
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) {
Alex Glaznev8c054152015-04-20 13:00:49 -0700169 LOG(LS_INFO) << "AndroidVideoCapturerJni capture started: " << success;
perkj@webrtc.org112f1272015-02-25 09:20:07 +0000170 invoker_.AsyncInvoke<void>(
171 thread_,
172 rtc::Bind(&AndroidVideoCapturerJni::OnCapturerStarted_w, this, success));
173}
174
175void AndroidVideoCapturerJni::OnIncomingFrame(void* video_frame,
176 int length,
177 int rotation,
178 int64 time_stamp) {
179 invoker_.AsyncInvoke<void>(
180 thread_,
181 rtc::Bind(&AndroidVideoCapturerJni::OnIncomingFrame_w,
182 this, video_frame, length, rotation, time_stamp));
183}
184
Åsa Persson2b679252015-06-15 09:53:05 +0200185void AndroidVideoCapturerJni::OnOutputFormatRequest(int width,
186 int height,
187 int fps) {
188 invoker_.AsyncInvoke<void>(
189 thread_,
190 rtc::Bind(&AndroidVideoCapturerJni::OnOutputFormatRequest_w,
191 this, width, height, fps));
192}
193
perkj@webrtc.org112f1272015-02-25 09:20:07 +0000194void AndroidVideoCapturerJni::OnCapturerStarted_w(bool success) {
Alex Glaznev8c054152015-04-20 13:00:49 -0700195 CHECK(thread_checker_.CalledOnValidThread());
196 if (capturer_) {
perkj@webrtc.org112f1272015-02-25 09:20:07 +0000197 capturer_->OnCapturerStarted(success);
Alex Glaznev8c054152015-04-20 13:00:49 -0700198 } else {
Alex Glaznevc4905fb2015-04-20 16:54:42 -0700199 LOG(LS_WARNING) << "OnCapturerStarted_w is called for closed capturer.";
Alex Glaznev8c054152015-04-20 13:00:49 -0700200 }
perkj@webrtc.org112f1272015-02-25 09:20:07 +0000201}
202
203void AndroidVideoCapturerJni::OnIncomingFrame_w(void* video_frame,
204 int length,
205 int rotation,
206 int64 time_stamp) {
Alex Glaznev8c054152015-04-20 13:00:49 -0700207 CHECK(thread_checker_.CalledOnValidThread());
208 if (capturer_) {
perkj@webrtc.org112f1272015-02-25 09:20:07 +0000209 capturer_->OnIncomingFrame(video_frame, length, rotation, time_stamp);
Alex Glaznev8c054152015-04-20 13:00:49 -0700210 } else {
211 LOG(LS_INFO) <<
Alex Glaznevc4905fb2015-04-20 16:54:42 -0700212 "Frame arrived after camera has been stopped: " << time_stamp <<
213 ". Valid global refs: " << valid_global_refs_;
Per49a862e2015-04-07 14:16:09 +0200214 ReturnBuffer_w(time_stamp);
Alex Glaznev8c054152015-04-20 13:00:49 -0700215 }
perkj@webrtc.org112f1272015-02-25 09:20:07 +0000216}
217
Åsa Persson2b679252015-06-15 09:53:05 +0200218void AndroidVideoCapturerJni::OnOutputFormatRequest_w(int width,
219 int height,
220 int fps) {
221 CHECK(thread_checker_.CalledOnValidThread());
222 if (capturer_) {
223 capturer_->OnOutputFormatRequest(width, height, fps);
224 } else {
225 LOG(LS_WARNING) << "OnOutputFormatRequest_w is called for closed capturer.";
226 }
227}
228
perkj@webrtc.org96e4db92015-02-13 12:46:51 +0000229JNIEnv* AndroidVideoCapturerJni::jni() { return AttachCurrentThreadIfNeeded(); }
230
perkj@webrtc.org3db042e2015-02-19 08:43:38 +0000231JOW(void, VideoCapturerAndroid_00024NativeObserver_nativeOnFrameCaptured)
perkj@webrtc.org41d8fda2015-02-27 18:50:53 +0000232 (JNIEnv* jni, jclass, jlong j_capturer, jbyteArray j_frame, jint length,
perkj@webrtc.org96e4db92015-02-13 12:46:51 +0000233 jint rotation, jlong ts) {
perkj@webrtc.org41d8fda2015-02-27 18:50:53 +0000234 jboolean is_copy = true;
235 jbyte* bytes = jni->GetByteArrayElements(j_frame, &is_copy);
236 if (!is_copy) {
237 reinterpret_cast<AndroidVideoCapturerJni*>(
238 j_capturer)->OnIncomingFrame(bytes, length, rotation, ts);
239 } else {
240 // If this is a copy of the original frame, it means that the memory
241 // is not direct memory and thus VideoCapturerAndroid does not guarantee
242 // that the memory is valid when we have released |j_frame|.
Alex Glaznev8c054152015-04-20 13:00:49 -0700243 LOG(LS_ERROR) << "NativeObserver_nativeOnFrameCaptured: frame is a copy";
244 CHECK(false) << "j_frame is a copy.";
perkj@webrtc.org41d8fda2015-02-27 18:50:53 +0000245 }
246 jni->ReleaseByteArrayElements(j_frame, bytes, JNI_ABORT);
perkj@webrtc.org96e4db92015-02-13 12:46:51 +0000247}
248
perkj@webrtc.org3db042e2015-02-19 08:43:38 +0000249JOW(void, VideoCapturerAndroid_00024NativeObserver_nativeCapturerStarted)
perkj@webrtc.org112f1272015-02-25 09:20:07 +0000250 (JNIEnv* jni, jclass, jlong j_capturer, jboolean j_success) {
Alex Glaznev8c054152015-04-20 13:00:49 -0700251 LOG(LS_INFO) << "NativeObserver_nativeCapturerStarted";
perkj@webrtc.org112f1272015-02-25 09:20:07 +0000252 reinterpret_cast<AndroidVideoCapturerJni*>(j_capturer)->OnCapturerStarted(
253 j_success);
perkj@webrtc.org96e4db92015-02-13 12:46:51 +0000254}
255
Åsa Persson2b679252015-06-15 09:53:05 +0200256JOW(void, VideoCapturerAndroid_00024NativeObserver_nativeOnOutputFormatRequest)
257 (JNIEnv* jni, jclass, jlong j_capturer, jint j_width, jint j_height,
258 jint j_fps) {
259 LOG(LS_INFO) << "NativeObserver_nativeOnOutputFormatRequest";
260 reinterpret_cast<AndroidVideoCapturerJni*>(j_capturer)->OnOutputFormatRequest(
261 j_width, j_height, j_fps);
262}
263
perkj@webrtc.org96e4db92015-02-13 12:46:51 +0000264} // namespace webrtc_jni
265