blob: 4f575b1b4dbdc847e3a673e6271285e2fdc9acb6 [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"
32
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
48AndroidVideoCapturerJni::AndroidVideoCapturerJni(JNIEnv* jni,
49 jobject j_video_capturer)
50 : j_capturer_global_(jni, j_video_capturer),
51 j_video_capturer_class_(
52 jni, FindClass(jni, "org/webrtc/VideoCapturerAndroid")),
53 j_frame_observer_class_(
54 jni,
55 FindClass(jni,
56 "org/webrtc/VideoCapturerAndroid$NativeFrameObserver")) {
57 }
58
59AndroidVideoCapturerJni::~AndroidVideoCapturerJni() {}
60
61void AndroidVideoCapturerJni::Start(int width, int height, int framerate,
62 webrtc::AndroidVideoCapturer* capturer) {
63 j_frame_observer_ = NewGlobalRef(
64 jni(),
65 jni()->NewObject(*j_frame_observer_class_,
66 GetMethodID(jni(),
67 *j_frame_observer_class_,
68 "<init>",
69 "(J)V"),
70 jlongFromPointer(capturer)));
71 CHECK_EXCEPTION(jni()) << "error during NewObject";
72
73 jmethodID m = GetMethodID(
74 jni(), *j_video_capturer_class_, "startCapture",
75 "(IIILandroid/content/Context;"
76 "Lorg/webrtc/VideoCapturerAndroid$CapturerObserver;)V");
77 jni()->CallVoidMethod(*j_capturer_global_,
78 m, width, height,
79 framerate,
80 application_context_,
81 j_frame_observer_);
82 CHECK_EXCEPTION(jni()) << "error during VideoCapturerAndroid.startCapture";
83}
84
85bool AndroidVideoCapturerJni::Stop() {
86 jmethodID m = GetMethodID(jni(), *j_video_capturer_class_,
87 "stopCapture", "()Z");
88 jboolean result = jni()->CallBooleanMethod(*j_capturer_global_, m);
89 CHECK_EXCEPTION(jni()) << "error during VideoCapturerAndroid.stopCapture";
90 DeleteGlobalRef(jni(), j_frame_observer_);
91 return result;
92}
93
94std::string AndroidVideoCapturerJni::GetSupportedFormats() {
95 jmethodID m =
96 GetMethodID(jni(), *j_video_capturer_class_,
97 "getSupportedFormatsAsJson", "()Ljava/lang/String;");
98 jstring j_json_caps =
99 (jstring) jni()->CallObjectMethod(*j_capturer_global_, m);
100 CHECK_EXCEPTION(jni()) << "error during supportedFormatsAsJson";
101 return JavaToStdString(jni(), j_json_caps);
102}
103
104JNIEnv* AndroidVideoCapturerJni::jni() { return AttachCurrentThreadIfNeeded(); }
105
106JOW(void, VideoCapturerAndroid_00024NativeFrameObserver_nativeOnFrameCaptured)
107 (JNIEnv* jni, jclass, jlong j_capturer, jbyteArray j_frame,
108 jint rotation, jlong ts) {
109 jbyte* bytes = jni->GetByteArrayElements(j_frame, NULL);
110 reinterpret_cast<webrtc::AndroidVideoCapturer*>(
111 j_capturer)->OnIncomingFrame(bytes, jni->GetArrayLength(j_frame),
112 rotation, ts);
113 jni->ReleaseByteArrayElements(j_frame, bytes, JNI_ABORT);
114}
115
116JOW(void, VideoCapturerAndroid_00024NativeFrameObserver_nativeCapturerStarted)
117 (JNIEnv* jni, jclass, jlong j_capturer, jboolean j_success) {
118 reinterpret_cast<webrtc::AndroidVideoCapturer*>(
119 j_capturer)->OnCapturerStarted(j_success);
120}
121
122} // namespace webrtc_jni
123