blob: 88cc31bc523109ad0c07c3ffaf78e777ef56e262 [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"
30
31#include "talk/app/webrtc/java/jni/classreferenceholder.h"
perkj@webrtc.org3db042e2015-02-19 08:43:38 +000032#include "webrtc/base/bind.h"
perkj@webrtc.org96e4db92015-02-13 12:46:51 +000033
34namespace webrtc_jni {
35
36jobject AndroidVideoCapturerJni::application_context_ = nullptr;
37
38// static
39int AndroidVideoCapturerJni::SetAndroidObjects(JNIEnv* jni,
40 jobject appliction_context) {
41 if (application_context_) {
42 jni->DeleteGlobalRef(application_context_);
43 }
44 application_context_ = NewGlobalRef(jni, appliction_context);
45
46 return 0;
47}
48
perkj@webrtc.org112f1272015-02-25 09:20:07 +000049// static
Per33544192015-04-02 12:30:51 +020050rtc::scoped_refptr<AndroidVideoCapturerJni>
perkj@webrtc.org112f1272015-02-25 09:20:07 +000051AndroidVideoCapturerJni::Create(JNIEnv* jni,
52 jobject j_video_capture,
53 jstring device_name) {
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) {
perkj@webrtc.org3db042e2015-02-19 08:43:38 +000072 thread_checker_.DetachFromThread();
73}
perkj@webrtc.org96e4db92015-02-13 12:46:51 +000074
perkj@webrtc.org3db042e2015-02-19 08:43:38 +000075bool AndroidVideoCapturerJni::Init(jstring device_name) {
76 const jmethodID m(GetMethodID(
77 jni(), *j_video_capturer_class_, "init", "(Ljava/lang/String;)Z"));
78 if (!jni()->CallBooleanMethod(*j_capturer_global_, m, device_name)) {
79 return false;
80 }
81 CHECK_EXCEPTION(jni()) << "error during CallVoidMethod";
82 return true;
83}
84
85AndroidVideoCapturerJni::~AndroidVideoCapturerJni() {
perkj@webrtc.org3db042e2015-02-19 08:43:38 +000086}
perkj@webrtc.org96e4db92015-02-13 12:46:51 +000087
88void AndroidVideoCapturerJni::Start(int width, int height, int framerate,
89 webrtc::AndroidVideoCapturer* capturer) {
perkj@webrtc.org3db042e2015-02-19 08:43:38 +000090 DCHECK(thread_checker_.CalledOnValidThread());
perkj@webrtc.org112f1272015-02-25 09:20:07 +000091 DCHECK(capturer_ == nullptr);
92 thread_ = rtc::Thread::Current();
93 capturer_ = capturer;
perkj@webrtc.org3db042e2015-02-19 08:43:38 +000094
perkj@webrtc.org96e4db92015-02-13 12:46:51 +000095 j_frame_observer_ = NewGlobalRef(
96 jni(),
perkj@webrtc.org3db042e2015-02-19 08:43:38 +000097 jni()->NewObject(*j_observer_class_,
perkj@webrtc.org96e4db92015-02-13 12:46:51 +000098 GetMethodID(jni(),
perkj@webrtc.org3db042e2015-02-19 08:43:38 +000099 *j_observer_class_,
perkj@webrtc.org96e4db92015-02-13 12:46:51 +0000100 "<init>",
101 "(J)V"),
perkj@webrtc.org112f1272015-02-25 09:20:07 +0000102 jlongFromPointer(this)));
perkj@webrtc.org96e4db92015-02-13 12:46:51 +0000103 CHECK_EXCEPTION(jni()) << "error during NewObject";
104
105 jmethodID m = GetMethodID(
106 jni(), *j_video_capturer_class_, "startCapture",
107 "(IIILandroid/content/Context;"
108 "Lorg/webrtc/VideoCapturerAndroid$CapturerObserver;)V");
109 jni()->CallVoidMethod(*j_capturer_global_,
110 m, width, height,
111 framerate,
112 application_context_,
113 j_frame_observer_);
114 CHECK_EXCEPTION(jni()) << "error during VideoCapturerAndroid.startCapture";
115}
116
perkj@webrtc.org3db042e2015-02-19 08:43:38 +0000117void AndroidVideoCapturerJni::Stop() {
118 DCHECK(thread_checker_.CalledOnValidThread());
perkj@webrtc.org112f1272015-02-25 09:20:07 +0000119 capturer_ = nullptr;
perkj@webrtc.org96e4db92015-02-13 12:46:51 +0000120 jmethodID m = GetMethodID(jni(), *j_video_capturer_class_,
perkj@webrtc.org3db042e2015-02-19 08:43:38 +0000121 "stopCapture", "()V");
122 jni()->CallVoidMethod(*j_capturer_global_, m);
perkj@webrtc.org96e4db92015-02-13 12:46:51 +0000123 CHECK_EXCEPTION(jni()) << "error during VideoCapturerAndroid.stopCapture";
124 DeleteGlobalRef(jni(), j_frame_observer_);
perkj@webrtc.org112f1272015-02-25 09:20:07 +0000125}
126
127void AndroidVideoCapturerJni::ReturnBuffer(int64 time_stamp) {
Per33544192015-04-02 12:30:51 +0200128 invoker_.AsyncInvoke<void>(
129 thread_,
130 rtc::Bind(&AndroidVideoCapturerJni::ReturnBuffer_w, this, time_stamp));
131}
132
133void AndroidVideoCapturerJni::ReturnBuffer_w(int64 time_stamp) {
perkj@webrtc.org112f1272015-02-25 09:20:07 +0000134 jmethodID m = GetMethodID(jni(), *j_video_capturer_class_,
135 "returnBuffer", "(J)V");
136 jni()->CallVoidMethod(*j_capturer_global_, m, time_stamp);
Per33544192015-04-02 12:30:51 +0200137 CHECK_EXCEPTION(jni()) << "error during VideoCapturerAndroid.returnBuffer";
perkj@webrtc.org96e4db92015-02-13 12:46:51 +0000138}
139
140std::string AndroidVideoCapturerJni::GetSupportedFormats() {
141 jmethodID m =
142 GetMethodID(jni(), *j_video_capturer_class_,
143 "getSupportedFormatsAsJson", "()Ljava/lang/String;");
144 jstring j_json_caps =
145 (jstring) jni()->CallObjectMethod(*j_capturer_global_, m);
146 CHECK_EXCEPTION(jni()) << "error during supportedFormatsAsJson";
147 return JavaToStdString(jni(), j_json_caps);
148}
149
perkj@webrtc.org112f1272015-02-25 09:20:07 +0000150void AndroidVideoCapturerJni::OnCapturerStarted(bool success) {
151 invoker_.AsyncInvoke<void>(
152 thread_,
153 rtc::Bind(&AndroidVideoCapturerJni::OnCapturerStarted_w, this, success));
154}
155
156void AndroidVideoCapturerJni::OnIncomingFrame(void* video_frame,
157 int length,
158 int rotation,
159 int64 time_stamp) {
160 invoker_.AsyncInvoke<void>(
161 thread_,
162 rtc::Bind(&AndroidVideoCapturerJni::OnIncomingFrame_w,
163 this, video_frame, length, rotation, time_stamp));
164}
165
166void AndroidVideoCapturerJni::OnCapturerStarted_w(bool success) {
167 DCHECK(thread_checker_.CalledOnValidThread());
168 if (capturer_)
169 capturer_->OnCapturerStarted(success);
170}
171
172void AndroidVideoCapturerJni::OnIncomingFrame_w(void* video_frame,
173 int length,
174 int rotation,
175 int64 time_stamp) {
176 DCHECK(thread_checker_.CalledOnValidThread());
177 if (capturer_)
178 capturer_->OnIncomingFrame(video_frame, length, rotation, time_stamp);
Per49a862e2015-04-07 14:16:09 +0200179 else
180 ReturnBuffer_w(time_stamp);
perkj@webrtc.org112f1272015-02-25 09:20:07 +0000181}
182
perkj@webrtc.org96e4db92015-02-13 12:46:51 +0000183JNIEnv* AndroidVideoCapturerJni::jni() { return AttachCurrentThreadIfNeeded(); }
184
perkj@webrtc.org3db042e2015-02-19 08:43:38 +0000185JOW(void, VideoCapturerAndroid_00024NativeObserver_nativeOnFrameCaptured)
perkj@webrtc.org41d8fda2015-02-27 18:50:53 +0000186 (JNIEnv* jni, jclass, jlong j_capturer, jbyteArray j_frame, jint length,
perkj@webrtc.org96e4db92015-02-13 12:46:51 +0000187 jint rotation, jlong ts) {
perkj@webrtc.org41d8fda2015-02-27 18:50:53 +0000188 jboolean is_copy = true;
189 jbyte* bytes = jni->GetByteArrayElements(j_frame, &is_copy);
190 if (!is_copy) {
191 reinterpret_cast<AndroidVideoCapturerJni*>(
192 j_capturer)->OnIncomingFrame(bytes, length, rotation, ts);
193 } else {
194 // If this is a copy of the original frame, it means that the memory
195 // is not direct memory and thus VideoCapturerAndroid does not guarantee
196 // that the memory is valid when we have released |j_frame|.
197 DCHECK(false) << "j_frame is a copy.";
198 }
199 jni->ReleaseByteArrayElements(j_frame, bytes, JNI_ABORT);
perkj@webrtc.org96e4db92015-02-13 12:46:51 +0000200}
201
perkj@webrtc.org3db042e2015-02-19 08:43:38 +0000202JOW(void, VideoCapturerAndroid_00024NativeObserver_nativeCapturerStarted)
perkj@webrtc.org112f1272015-02-25 09:20:07 +0000203 (JNIEnv* jni, jclass, jlong j_capturer, jboolean j_success) {
204 reinterpret_cast<AndroidVideoCapturerJni*>(j_capturer)->OnCapturerStarted(
205 j_success);
perkj@webrtc.org96e4db92015-02-13 12:46:51 +0000206}
207
208} // namespace webrtc_jni
209