blob: dfe1b1b7f590304350b1e0e98c61175e3323c59c [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
50rtc::scoped_ptr<AndroidVideoCapturerJni>
51AndroidVideoCapturerJni::Create(JNIEnv* jni,
52 jobject j_video_capture,
53 jstring device_name) {
54 rtc::scoped_ptr<AndroidVideoCapturerJni> capturer(
55 new AndroidVideoCapturerJni(jni,
56 j_video_capture));
57
58 if (capturer->Init(device_name))
59 return capturer.Pass();
60 return nullptr;
61}
62
perkj@webrtc.org96e4db92015-02-13 12:46:51 +000063AndroidVideoCapturerJni::AndroidVideoCapturerJni(JNIEnv* jni,
64 jobject j_video_capturer)
65 : j_capturer_global_(jni, j_video_capturer),
66 j_video_capturer_class_(
67 jni, FindClass(jni, "org/webrtc/VideoCapturerAndroid")),
perkj@webrtc.org3db042e2015-02-19 08:43:38 +000068 j_observer_class_(
perkj@webrtc.org96e4db92015-02-13 12:46:51 +000069 jni,
70 FindClass(jni,
perkj@webrtc.org3db042e2015-02-19 08:43:38 +000071 "org/webrtc/VideoCapturerAndroid$NativeObserver")),
perkj@webrtc.org112f1272015-02-25 09:20:07 +000072 capturer_(nullptr) {
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() {
perkj@webrtc.org3db042e2015-02-19 08:43:38 +000087}
perkj@webrtc.org96e4db92015-02-13 12:46:51 +000088
89void AndroidVideoCapturerJni::Start(int width, int height, int framerate,
90 webrtc::AndroidVideoCapturer* capturer) {
perkj@webrtc.org3db042e2015-02-19 08:43:38 +000091 DCHECK(thread_checker_.CalledOnValidThread());
perkj@webrtc.org112f1272015-02-25 09:20:07 +000092 DCHECK(capturer_ == nullptr);
93 thread_ = rtc::Thread::Current();
94 capturer_ = capturer;
perkj@webrtc.org3db042e2015-02-19 08:43:38 +000095
perkj@webrtc.org96e4db92015-02-13 12:46:51 +000096 j_frame_observer_ = NewGlobalRef(
97 jni(),
perkj@webrtc.org3db042e2015-02-19 08:43:38 +000098 jni()->NewObject(*j_observer_class_,
perkj@webrtc.org96e4db92015-02-13 12:46:51 +000099 GetMethodID(jni(),
perkj@webrtc.org3db042e2015-02-19 08:43:38 +0000100 *j_observer_class_,
perkj@webrtc.org96e4db92015-02-13 12:46:51 +0000101 "<init>",
102 "(J)V"),
perkj@webrtc.org112f1272015-02-25 09:20:07 +0000103 jlongFromPointer(this)));
perkj@webrtc.org96e4db92015-02-13 12:46:51 +0000104 CHECK_EXCEPTION(jni()) << "error during NewObject";
105
106 jmethodID m = GetMethodID(
107 jni(), *j_video_capturer_class_, "startCapture",
108 "(IIILandroid/content/Context;"
109 "Lorg/webrtc/VideoCapturerAndroid$CapturerObserver;)V");
110 jni()->CallVoidMethod(*j_capturer_global_,
111 m, width, height,
112 framerate,
113 application_context_,
114 j_frame_observer_);
115 CHECK_EXCEPTION(jni()) << "error during VideoCapturerAndroid.startCapture";
116}
117
perkj@webrtc.org3db042e2015-02-19 08:43:38 +0000118void AndroidVideoCapturerJni::Stop() {
119 DCHECK(thread_checker_.CalledOnValidThread());
perkj@webrtc.org112f1272015-02-25 09:20:07 +0000120 capturer_ = nullptr;
perkj@webrtc.org96e4db92015-02-13 12:46:51 +0000121 jmethodID m = GetMethodID(jni(), *j_video_capturer_class_,
perkj@webrtc.org3db042e2015-02-19 08:43:38 +0000122 "stopCapture", "()V");
123 jni()->CallVoidMethod(*j_capturer_global_, m);
perkj@webrtc.org96e4db92015-02-13 12:46:51 +0000124 CHECK_EXCEPTION(jni()) << "error during VideoCapturerAndroid.stopCapture";
125 DeleteGlobalRef(jni(), j_frame_observer_);
perkj@webrtc.org112f1272015-02-25 09:20:07 +0000126 // Do not process frames in flight after stop have returned since
127 // the memory buffers they point to have been deleted.
128 rtc::MessageQueueManager::Clear(&invoker_);
129}
130
131void AndroidVideoCapturerJni::ReturnBuffer(int64 time_stamp) {
132 jmethodID m = GetMethodID(jni(), *j_video_capturer_class_,
133 "returnBuffer", "(J)V");
134 jni()->CallVoidMethod(*j_capturer_global_, m, time_stamp);
perkj@webrtc.org96e4db92015-02-13 12:46:51 +0000135}
136
137std::string AndroidVideoCapturerJni::GetSupportedFormats() {
138 jmethodID m =
139 GetMethodID(jni(), *j_video_capturer_class_,
140 "getSupportedFormatsAsJson", "()Ljava/lang/String;");
141 jstring j_json_caps =
142 (jstring) jni()->CallObjectMethod(*j_capturer_global_, m);
143 CHECK_EXCEPTION(jni()) << "error during supportedFormatsAsJson";
144 return JavaToStdString(jni(), j_json_caps);
145}
146
perkj@webrtc.org112f1272015-02-25 09:20:07 +0000147void AndroidVideoCapturerJni::OnCapturerStarted(bool success) {
148 invoker_.AsyncInvoke<void>(
149 thread_,
150 rtc::Bind(&AndroidVideoCapturerJni::OnCapturerStarted_w, this, success));
151}
152
153void AndroidVideoCapturerJni::OnIncomingFrame(void* video_frame,
154 int length,
155 int rotation,
156 int64 time_stamp) {
157 invoker_.AsyncInvoke<void>(
158 thread_,
159 rtc::Bind(&AndroidVideoCapturerJni::OnIncomingFrame_w,
160 this, video_frame, length, rotation, time_stamp));
161}
162
163void AndroidVideoCapturerJni::OnCapturerStarted_w(bool success) {
164 DCHECK(thread_checker_.CalledOnValidThread());
165 if (capturer_)
166 capturer_->OnCapturerStarted(success);
167}
168
169void AndroidVideoCapturerJni::OnIncomingFrame_w(void* video_frame,
170 int length,
171 int rotation,
172 int64 time_stamp) {
173 DCHECK(thread_checker_.CalledOnValidThread());
174 if (capturer_)
175 capturer_->OnIncomingFrame(video_frame, length, rotation, time_stamp);
176}
177
perkj@webrtc.org96e4db92015-02-13 12:46:51 +0000178JNIEnv* AndroidVideoCapturerJni::jni() { return AttachCurrentThreadIfNeeded(); }
179
perkj@webrtc.org3db042e2015-02-19 08:43:38 +0000180JOW(void, VideoCapturerAndroid_00024NativeObserver_nativeOnFrameCaptured)
perkj@webrtc.org41d8fda2015-02-27 18:50:53 +0000181 (JNIEnv* jni, jclass, jlong j_capturer, jbyteArray j_frame, jint length,
perkj@webrtc.org96e4db92015-02-13 12:46:51 +0000182 jint rotation, jlong ts) {
perkj@webrtc.org41d8fda2015-02-27 18:50:53 +0000183 jboolean is_copy = true;
184 jbyte* bytes = jni->GetByteArrayElements(j_frame, &is_copy);
185 if (!is_copy) {
186 reinterpret_cast<AndroidVideoCapturerJni*>(
187 j_capturer)->OnIncomingFrame(bytes, length, rotation, ts);
188 } else {
189 // If this is a copy of the original frame, it means that the memory
190 // is not direct memory and thus VideoCapturerAndroid does not guarantee
191 // that the memory is valid when we have released |j_frame|.
192 DCHECK(false) << "j_frame is a copy.";
193 }
194 jni->ReleaseByteArrayElements(j_frame, bytes, JNI_ABORT);
perkj@webrtc.org96e4db92015-02-13 12:46:51 +0000195}
196
perkj@webrtc.org3db042e2015-02-19 08:43:38 +0000197JOW(void, VideoCapturerAndroid_00024NativeObserver_nativeCapturerStarted)
perkj@webrtc.org112f1272015-02-25 09:20:07 +0000198 (JNIEnv* jni, jclass, jlong j_capturer, jboolean j_success) {
199 reinterpret_cast<AndroidVideoCapturerJni*>(j_capturer)->OnCapturerStarted(
200 j_success);
perkj@webrtc.org96e4db92015-02-13 12:46:51 +0000201}
202
203} // namespace webrtc_jni
204