blob: a51c6efa7a1709c04e235ae19530997ae646a0fd [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
57 if (capturer->Init(device_name))
Per33544192015-04-02 12:30:51 +020058 return capturer;
perkj@webrtc.org112f1272015-02-25 09:20:07 +000059 return nullptr;
60}
61
perkj@webrtc.org96e4db92015-02-13 12:46:51 +000062AndroidVideoCapturerJni::AndroidVideoCapturerJni(JNIEnv* jni,
63 jobject j_video_capturer)
64 : j_capturer_global_(jni, j_video_capturer),
65 j_video_capturer_class_(
66 jni, FindClass(jni, "org/webrtc/VideoCapturerAndroid")),
perkj@webrtc.org3db042e2015-02-19 08:43:38 +000067 j_observer_class_(
perkj@webrtc.org96e4db92015-02-13 12:46:51 +000068 jni,
69 FindClass(jni,
perkj@webrtc.org3db042e2015-02-19 08:43:38 +000070 "org/webrtc/VideoCapturerAndroid$NativeObserver")),
Alex Glaznevc4905fb2015-04-20 16:54:42 -070071 capturer_(nullptr),
72 thread_(nullptr),
73 valid_global_refs_(true) {
Alex Glaznev8c054152015-04-20 13:00:49 -070074 LOG(LS_INFO) << "AndroidVideoCapturerJni ctor";
perkj@webrtc.org3db042e2015-02-19 08:43:38 +000075 thread_checker_.DetachFromThread();
76}
perkj@webrtc.org96e4db92015-02-13 12:46:51 +000077
perkj@webrtc.org3db042e2015-02-19 08:43:38 +000078bool AndroidVideoCapturerJni::Init(jstring device_name) {
79 const jmethodID m(GetMethodID(
80 jni(), *j_video_capturer_class_, "init", "(Ljava/lang/String;)Z"));
81 if (!jni()->CallBooleanMethod(*j_capturer_global_, m, device_name)) {
82 return false;
83 }
84 CHECK_EXCEPTION(jni()) << "error during CallVoidMethod";
85 return true;
86}
87
88AndroidVideoCapturerJni::~AndroidVideoCapturerJni() {
Alex Glaznevc4905fb2015-04-20 16:54:42 -070089 valid_global_refs_ = false;
90 if (thread_ != nullptr) {
91 LOG(LS_INFO) << "AndroidVideoCapturerJni dtor - flush invoker";
92 invoker_.Flush(thread_);
93 }
94 LOG(LS_INFO) << "AndroidVideoCapturerJni dtor done";
perkj@webrtc.org3db042e2015-02-19 08:43:38 +000095}
perkj@webrtc.org96e4db92015-02-13 12:46:51 +000096
97void AndroidVideoCapturerJni::Start(int width, int height, int framerate,
98 webrtc::AndroidVideoCapturer* capturer) {
Alex Glaznev8c054152015-04-20 13:00:49 -070099 LOG(LS_INFO) << "AndroidVideoCapturerJni start";
100 CHECK(thread_checker_.CalledOnValidThread());
101 CHECK(capturer_ == nullptr);
perkj@webrtc.org112f1272015-02-25 09:20:07 +0000102 thread_ = rtc::Thread::Current();
103 capturer_ = capturer;
perkj@webrtc.org3db042e2015-02-19 08:43:38 +0000104
perkj@webrtc.org96e4db92015-02-13 12:46:51 +0000105 j_frame_observer_ = NewGlobalRef(
106 jni(),
perkj@webrtc.org3db042e2015-02-19 08:43:38 +0000107 jni()->NewObject(*j_observer_class_,
perkj@webrtc.org96e4db92015-02-13 12:46:51 +0000108 GetMethodID(jni(),
perkj@webrtc.org3db042e2015-02-19 08:43:38 +0000109 *j_observer_class_,
perkj@webrtc.org96e4db92015-02-13 12:46:51 +0000110 "<init>",
111 "(J)V"),
perkj@webrtc.org112f1272015-02-25 09:20:07 +0000112 jlongFromPointer(this)));
perkj@webrtc.org96e4db92015-02-13 12:46:51 +0000113 CHECK_EXCEPTION(jni()) << "error during NewObject";
114
115 jmethodID m = GetMethodID(
116 jni(), *j_video_capturer_class_, "startCapture",
117 "(IIILandroid/content/Context;"
118 "Lorg/webrtc/VideoCapturerAndroid$CapturerObserver;)V");
119 jni()->CallVoidMethod(*j_capturer_global_,
120 m, width, height,
121 framerate,
122 application_context_,
123 j_frame_observer_);
124 CHECK_EXCEPTION(jni()) << "error during VideoCapturerAndroid.startCapture";
125}
126
perkj@webrtc.org3db042e2015-02-19 08:43:38 +0000127void AndroidVideoCapturerJni::Stop() {
Alex Glaznev8c054152015-04-20 13:00:49 -0700128 LOG(LS_INFO) << "AndroidVideoCapturerJni stop";
129 CHECK(thread_checker_.CalledOnValidThread());
perkj@webrtc.org112f1272015-02-25 09:20:07 +0000130 capturer_ = nullptr;
perkj@webrtc.org96e4db92015-02-13 12:46:51 +0000131 jmethodID m = GetMethodID(jni(), *j_video_capturer_class_,
perkj@webrtc.org3db042e2015-02-19 08:43:38 +0000132 "stopCapture", "()V");
133 jni()->CallVoidMethod(*j_capturer_global_, m);
perkj@webrtc.org96e4db92015-02-13 12:46:51 +0000134 CHECK_EXCEPTION(jni()) << "error during VideoCapturerAndroid.stopCapture";
135 DeleteGlobalRef(jni(), j_frame_observer_);
Alex Glaznev8c054152015-04-20 13:00:49 -0700136 LOG(LS_INFO) << "AndroidVideoCapturerJni stop done";
perkj@webrtc.org112f1272015-02-25 09:20:07 +0000137}
138
139void AndroidVideoCapturerJni::ReturnBuffer(int64 time_stamp) {
Per33544192015-04-02 12:30:51 +0200140 invoker_.AsyncInvoke<void>(
141 thread_,
142 rtc::Bind(&AndroidVideoCapturerJni::ReturnBuffer_w, this, time_stamp));
143}
144
145void AndroidVideoCapturerJni::ReturnBuffer_w(int64 time_stamp) {
Alex Glaznevc4905fb2015-04-20 16:54:42 -0700146 if (!valid_global_refs_) {
147 LOG(LS_ERROR) << "ReturnBuffer_w is called for invalid global refs.";
148 return;
149 }
perkj@webrtc.org112f1272015-02-25 09:20:07 +0000150 jmethodID m = GetMethodID(jni(), *j_video_capturer_class_,
151 "returnBuffer", "(J)V");
152 jni()->CallVoidMethod(*j_capturer_global_, m, time_stamp);
Per33544192015-04-02 12:30:51 +0200153 CHECK_EXCEPTION(jni()) << "error during VideoCapturerAndroid.returnBuffer";
perkj@webrtc.org96e4db92015-02-13 12:46:51 +0000154}
155
156std::string AndroidVideoCapturerJni::GetSupportedFormats() {
157 jmethodID m =
158 GetMethodID(jni(), *j_video_capturer_class_,
159 "getSupportedFormatsAsJson", "()Ljava/lang/String;");
160 jstring j_json_caps =
161 (jstring) jni()->CallObjectMethod(*j_capturer_global_, m);
162 CHECK_EXCEPTION(jni()) << "error during supportedFormatsAsJson";
163 return JavaToStdString(jni(), j_json_caps);
164}
165
perkj@webrtc.org112f1272015-02-25 09:20:07 +0000166void AndroidVideoCapturerJni::OnCapturerStarted(bool success) {
Alex Glaznev8c054152015-04-20 13:00:49 -0700167 LOG(LS_INFO) << "AndroidVideoCapturerJni capture started: " << success;
perkj@webrtc.org112f1272015-02-25 09:20:07 +0000168 invoker_.AsyncInvoke<void>(
169 thread_,
170 rtc::Bind(&AndroidVideoCapturerJni::OnCapturerStarted_w, this, success));
171}
172
173void AndroidVideoCapturerJni::OnIncomingFrame(void* video_frame,
174 int length,
175 int rotation,
176 int64 time_stamp) {
177 invoker_.AsyncInvoke<void>(
178 thread_,
179 rtc::Bind(&AndroidVideoCapturerJni::OnIncomingFrame_w,
180 this, video_frame, length, rotation, time_stamp));
181}
182
183void AndroidVideoCapturerJni::OnCapturerStarted_w(bool success) {
Alex Glaznev8c054152015-04-20 13:00:49 -0700184 CHECK(thread_checker_.CalledOnValidThread());
185 if (capturer_) {
perkj@webrtc.org112f1272015-02-25 09:20:07 +0000186 capturer_->OnCapturerStarted(success);
Alex Glaznev8c054152015-04-20 13:00:49 -0700187 } else {
Alex Glaznevc4905fb2015-04-20 16:54:42 -0700188 LOG(LS_WARNING) << "OnCapturerStarted_w is called for closed capturer.";
Alex Glaznev8c054152015-04-20 13:00:49 -0700189 }
perkj@webrtc.org112f1272015-02-25 09:20:07 +0000190}
191
192void AndroidVideoCapturerJni::OnIncomingFrame_w(void* video_frame,
193 int length,
194 int rotation,
195 int64 time_stamp) {
Alex Glaznev8c054152015-04-20 13:00:49 -0700196 CHECK(thread_checker_.CalledOnValidThread());
197 if (capturer_) {
perkj@webrtc.org112f1272015-02-25 09:20:07 +0000198 capturer_->OnIncomingFrame(video_frame, length, rotation, time_stamp);
Alex Glaznev8c054152015-04-20 13:00:49 -0700199 } else {
200 LOG(LS_INFO) <<
Alex Glaznevc4905fb2015-04-20 16:54:42 -0700201 "Frame arrived after camera has been stopped: " << time_stamp <<
202 ". Valid global refs: " << valid_global_refs_;
Per49a862e2015-04-07 14:16:09 +0200203 ReturnBuffer_w(time_stamp);
Alex Glaznev8c054152015-04-20 13:00:49 -0700204 }
perkj@webrtc.org112f1272015-02-25 09:20:07 +0000205}
206
perkj@webrtc.org96e4db92015-02-13 12:46:51 +0000207JNIEnv* AndroidVideoCapturerJni::jni() { return AttachCurrentThreadIfNeeded(); }
208
perkj@webrtc.org3db042e2015-02-19 08:43:38 +0000209JOW(void, VideoCapturerAndroid_00024NativeObserver_nativeOnFrameCaptured)
perkj@webrtc.org41d8fda2015-02-27 18:50:53 +0000210 (JNIEnv* jni, jclass, jlong j_capturer, jbyteArray j_frame, jint length,
perkj@webrtc.org96e4db92015-02-13 12:46:51 +0000211 jint rotation, jlong ts) {
perkj@webrtc.org41d8fda2015-02-27 18:50:53 +0000212 jboolean is_copy = true;
213 jbyte* bytes = jni->GetByteArrayElements(j_frame, &is_copy);
214 if (!is_copy) {
215 reinterpret_cast<AndroidVideoCapturerJni*>(
216 j_capturer)->OnIncomingFrame(bytes, length, rotation, ts);
217 } else {
218 // If this is a copy of the original frame, it means that the memory
219 // is not direct memory and thus VideoCapturerAndroid does not guarantee
220 // that the memory is valid when we have released |j_frame|.
Alex Glaznev8c054152015-04-20 13:00:49 -0700221 LOG(LS_ERROR) << "NativeObserver_nativeOnFrameCaptured: frame is a copy";
222 CHECK(false) << "j_frame is a copy.";
perkj@webrtc.org41d8fda2015-02-27 18:50:53 +0000223 }
224 jni->ReleaseByteArrayElements(j_frame, bytes, JNI_ABORT);
perkj@webrtc.org96e4db92015-02-13 12:46:51 +0000225}
226
perkj@webrtc.org3db042e2015-02-19 08:43:38 +0000227JOW(void, VideoCapturerAndroid_00024NativeObserver_nativeCapturerStarted)
perkj@webrtc.org112f1272015-02-25 09:20:07 +0000228 (JNIEnv* jni, jclass, jlong j_capturer, jboolean j_success) {
Alex Glaznev8c054152015-04-20 13:00:49 -0700229 LOG(LS_INFO) << "NativeObserver_nativeCapturerStarted";
perkj@webrtc.org112f1272015-02-25 09:20:07 +0000230 reinterpret_cast<AndroidVideoCapturerJni*>(j_capturer)->OnCapturerStarted(
231 j_success);
perkj@webrtc.org96e4db92015-02-13 12:46:51 +0000232}
233
234} // namespace webrtc_jni
235