blob: ad873392aca7b20620b909cb747c8a0741499e55 [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")),
perkj@webrtc.org112f1272015-02-25 09:20:07 +000071 capturer_(nullptr) {
Alex Glaznev8c054152015-04-20 13:00:49 -070072 LOG(LS_INFO) << "AndroidVideoCapturerJni ctor";
perkj@webrtc.org3db042e2015-02-19 08:43:38 +000073 thread_checker_.DetachFromThread();
74}
perkj@webrtc.org96e4db92015-02-13 12:46:51 +000075
perkj@webrtc.org3db042e2015-02-19 08:43:38 +000076bool AndroidVideoCapturerJni::Init(jstring device_name) {
77 const jmethodID m(GetMethodID(
78 jni(), *j_video_capturer_class_, "init", "(Ljava/lang/String;)Z"));
79 if (!jni()->CallBooleanMethod(*j_capturer_global_, m, device_name)) {
80 return false;
81 }
82 CHECK_EXCEPTION(jni()) << "error during CallVoidMethod";
83 return true;
84}
85
86AndroidVideoCapturerJni::~AndroidVideoCapturerJni() {
Alex Glaznev8c054152015-04-20 13:00:49 -070087 LOG(LS_INFO) << "AndroidVideoCapturerJni dtor";
perkj@webrtc.org3db042e2015-02-19 08:43:38 +000088}
perkj@webrtc.org96e4db92015-02-13 12:46:51 +000089
90void AndroidVideoCapturerJni::Start(int width, int height, int framerate,
91 webrtc::AndroidVideoCapturer* capturer) {
Alex Glaznev8c054152015-04-20 13:00:49 -070092 LOG(LS_INFO) << "AndroidVideoCapturerJni start";
93 CHECK(thread_checker_.CalledOnValidThread());
94 CHECK(capturer_ == nullptr);
perkj@webrtc.org112f1272015-02-25 09:20:07 +000095 thread_ = rtc::Thread::Current();
96 capturer_ = capturer;
perkj@webrtc.org3db042e2015-02-19 08:43:38 +000097
perkj@webrtc.org96e4db92015-02-13 12:46:51 +000098 j_frame_observer_ = NewGlobalRef(
99 jni(),
perkj@webrtc.org3db042e2015-02-19 08:43:38 +0000100 jni()->NewObject(*j_observer_class_,
perkj@webrtc.org96e4db92015-02-13 12:46:51 +0000101 GetMethodID(jni(),
perkj@webrtc.org3db042e2015-02-19 08:43:38 +0000102 *j_observer_class_,
perkj@webrtc.org96e4db92015-02-13 12:46:51 +0000103 "<init>",
104 "(J)V"),
perkj@webrtc.org112f1272015-02-25 09:20:07 +0000105 jlongFromPointer(this)));
perkj@webrtc.org96e4db92015-02-13 12:46:51 +0000106 CHECK_EXCEPTION(jni()) << "error during NewObject";
107
108 jmethodID m = GetMethodID(
109 jni(), *j_video_capturer_class_, "startCapture",
110 "(IIILandroid/content/Context;"
111 "Lorg/webrtc/VideoCapturerAndroid$CapturerObserver;)V");
112 jni()->CallVoidMethod(*j_capturer_global_,
113 m, width, height,
114 framerate,
115 application_context_,
116 j_frame_observer_);
117 CHECK_EXCEPTION(jni()) << "error during VideoCapturerAndroid.startCapture";
118}
119
perkj@webrtc.org3db042e2015-02-19 08:43:38 +0000120void AndroidVideoCapturerJni::Stop() {
Alex Glaznev8c054152015-04-20 13:00:49 -0700121 LOG(LS_INFO) << "AndroidVideoCapturerJni stop";
122 CHECK(thread_checker_.CalledOnValidThread());
perkj@webrtc.org112f1272015-02-25 09:20:07 +0000123 capturer_ = nullptr;
perkj@webrtc.org96e4db92015-02-13 12:46:51 +0000124 jmethodID m = GetMethodID(jni(), *j_video_capturer_class_,
perkj@webrtc.org3db042e2015-02-19 08:43:38 +0000125 "stopCapture", "()V");
126 jni()->CallVoidMethod(*j_capturer_global_, m);
perkj@webrtc.org96e4db92015-02-13 12:46:51 +0000127 CHECK_EXCEPTION(jni()) << "error during VideoCapturerAndroid.stopCapture";
128 DeleteGlobalRef(jni(), j_frame_observer_);
Alex Glaznev8c054152015-04-20 13:00:49 -0700129 LOG(LS_INFO) << "AndroidVideoCapturerJni stop done";
perkj@webrtc.org112f1272015-02-25 09:20:07 +0000130}
131
132void AndroidVideoCapturerJni::ReturnBuffer(int64 time_stamp) {
Per33544192015-04-02 12:30:51 +0200133 invoker_.AsyncInvoke<void>(
134 thread_,
135 rtc::Bind(&AndroidVideoCapturerJni::ReturnBuffer_w, this, time_stamp));
136}
137
138void AndroidVideoCapturerJni::ReturnBuffer_w(int64 time_stamp) {
perkj@webrtc.org112f1272015-02-25 09:20:07 +0000139 jmethodID m = GetMethodID(jni(), *j_video_capturer_class_,
140 "returnBuffer", "(J)V");
141 jni()->CallVoidMethod(*j_capturer_global_, m, time_stamp);
Per33544192015-04-02 12:30:51 +0200142 CHECK_EXCEPTION(jni()) << "error during VideoCapturerAndroid.returnBuffer";
perkj@webrtc.org96e4db92015-02-13 12:46:51 +0000143}
144
145std::string AndroidVideoCapturerJni::GetSupportedFormats() {
146 jmethodID m =
147 GetMethodID(jni(), *j_video_capturer_class_,
148 "getSupportedFormatsAsJson", "()Ljava/lang/String;");
149 jstring j_json_caps =
150 (jstring) jni()->CallObjectMethod(*j_capturer_global_, m);
151 CHECK_EXCEPTION(jni()) << "error during supportedFormatsAsJson";
152 return JavaToStdString(jni(), j_json_caps);
153}
154
perkj@webrtc.org112f1272015-02-25 09:20:07 +0000155void AndroidVideoCapturerJni::OnCapturerStarted(bool success) {
Alex Glaznev8c054152015-04-20 13:00:49 -0700156 LOG(LS_INFO) << "AndroidVideoCapturerJni capture started: " << success;
perkj@webrtc.org112f1272015-02-25 09:20:07 +0000157 invoker_.AsyncInvoke<void>(
158 thread_,
159 rtc::Bind(&AndroidVideoCapturerJni::OnCapturerStarted_w, this, success));
160}
161
162void AndroidVideoCapturerJni::OnIncomingFrame(void* video_frame,
163 int length,
164 int rotation,
165 int64 time_stamp) {
166 invoker_.AsyncInvoke<void>(
167 thread_,
168 rtc::Bind(&AndroidVideoCapturerJni::OnIncomingFrame_w,
169 this, video_frame, length, rotation, time_stamp));
170}
171
172void AndroidVideoCapturerJni::OnCapturerStarted_w(bool success) {
Alex Glaznev8c054152015-04-20 13:00:49 -0700173 CHECK(thread_checker_.CalledOnValidThread());
174 if (capturer_) {
perkj@webrtc.org112f1272015-02-25 09:20:07 +0000175 capturer_->OnCapturerStarted(success);
Alex Glaznev8c054152015-04-20 13:00:49 -0700176 } else {
177 LOG(LS_ERROR) << "OnCapturerStarted_w is called for null capturer.";
178 }
perkj@webrtc.org112f1272015-02-25 09:20:07 +0000179}
180
181void AndroidVideoCapturerJni::OnIncomingFrame_w(void* video_frame,
182 int length,
183 int rotation,
184 int64 time_stamp) {
Alex Glaznev8c054152015-04-20 13:00:49 -0700185 CHECK(thread_checker_.CalledOnValidThread());
186 if (capturer_) {
perkj@webrtc.org112f1272015-02-25 09:20:07 +0000187 capturer_->OnIncomingFrame(video_frame, length, rotation, time_stamp);
Alex Glaznev8c054152015-04-20 13:00:49 -0700188 } else {
189 LOG(LS_INFO) <<
190 "Frame arrived after camera has been stopped: " << time_stamp;
Per49a862e2015-04-07 14:16:09 +0200191 ReturnBuffer_w(time_stamp);
Alex Glaznev8c054152015-04-20 13:00:49 -0700192 }
perkj@webrtc.org112f1272015-02-25 09:20:07 +0000193}
194
perkj@webrtc.org96e4db92015-02-13 12:46:51 +0000195JNIEnv* AndroidVideoCapturerJni::jni() { return AttachCurrentThreadIfNeeded(); }
196
perkj@webrtc.org3db042e2015-02-19 08:43:38 +0000197JOW(void, VideoCapturerAndroid_00024NativeObserver_nativeOnFrameCaptured)
perkj@webrtc.org41d8fda2015-02-27 18:50:53 +0000198 (JNIEnv* jni, jclass, jlong j_capturer, jbyteArray j_frame, jint length,
perkj@webrtc.org96e4db92015-02-13 12:46:51 +0000199 jint rotation, jlong ts) {
perkj@webrtc.org41d8fda2015-02-27 18:50:53 +0000200 jboolean is_copy = true;
201 jbyte* bytes = jni->GetByteArrayElements(j_frame, &is_copy);
202 if (!is_copy) {
203 reinterpret_cast<AndroidVideoCapturerJni*>(
204 j_capturer)->OnIncomingFrame(bytes, length, rotation, ts);
205 } else {
206 // If this is a copy of the original frame, it means that the memory
207 // is not direct memory and thus VideoCapturerAndroid does not guarantee
208 // that the memory is valid when we have released |j_frame|.
Alex Glaznev8c054152015-04-20 13:00:49 -0700209 LOG(LS_ERROR) << "NativeObserver_nativeOnFrameCaptured: frame is a copy";
210 CHECK(false) << "j_frame is a copy.";
perkj@webrtc.org41d8fda2015-02-27 18:50:53 +0000211 }
212 jni->ReleaseByteArrayElements(j_frame, bytes, JNI_ABORT);
perkj@webrtc.org96e4db92015-02-13 12:46:51 +0000213}
214
perkj@webrtc.org3db042e2015-02-19 08:43:38 +0000215JOW(void, VideoCapturerAndroid_00024NativeObserver_nativeCapturerStarted)
perkj@webrtc.org112f1272015-02-25 09:20:07 +0000216 (JNIEnv* jni, jclass, jlong j_capturer, jboolean j_success) {
Alex Glaznev8c054152015-04-20 13:00:49 -0700217 LOG(LS_INFO) << "NativeObserver_nativeCapturerStarted";
perkj@webrtc.org112f1272015-02-25 09:20:07 +0000218 reinterpret_cast<AndroidVideoCapturerJni*>(j_capturer)->OnCapturerStarted(
219 j_success);
perkj@webrtc.org96e4db92015-02-13 12:46:51 +0000220}
221
222} // namespace webrtc_jni
223