andrew@webrtc.org | a7b57da | 2012-10-22 18:19:23 +0000 | [diff] [blame] | 1 | /* |
| 2 | * Copyright (c) 2011 The WebRTC project authors. All Rights Reserved. |
| 3 | * |
| 4 | * Use of this source code is governed by a BSD-style license |
| 5 | * that can be found in the LICENSE file in the root of the source |
| 6 | * tree. An additional intellectual property rights grant can be found |
| 7 | * in the file PATENTS. All contributing project authors may |
| 8 | * be found in the AUTHORS file in the root of the source tree. |
| 9 | */ |
| 10 | |
| 11 | #include <string.h> // memset |
| 12 | #include <android/log.h> |
| 13 | |
| 14 | #include "org_webrtc_capturemoduleandroidtest_VideoCaptureModuleTest.h" |
| 15 | #include "../../../interface/video_capture_factory.h" |
| 16 | #include "../../../../../video_render/main/interface/video_render.h" |
| 17 | #include "../../testAPI/testPlatformDependent.h" |
| 18 | #include "../../testAPI/testPlatformDependent.h" |
| 19 | #ifdef RENDER_PREVIEW |
| 20 | #include "../../testAPI/Renderer.h" |
| 21 | #endif |
| 22 | |
| 23 | using namespace webrtc; |
| 24 | #define WEBRTC_LOG_TAG "*WEBRTCN*" // As in WEBRTC Native... |
| 25 | // ADM data struct |
| 26 | typedef struct |
| 27 | { |
| 28 | // Other |
| 29 | JavaVM* jvm; |
| 30 | Renderer* renderer; |
| 31 | VideoCaptureModule* _videoCapture; |
| 32 | VideoCaptureModule::DeviceInfo*_captureInfo; |
| 33 | } JniData; |
| 34 | |
| 35 | // Global variables visible in this file |
| 36 | static JniData jniData; |
| 37 | |
| 38 | ////////////////////////////////////////////////////////////////// |
| 39 | // General functions |
| 40 | ////////////////////////////////////////////////////////////////// |
| 41 | |
| 42 | ///////////////////////////////////////////// |
| 43 | // JNI_OnLoad |
| 44 | // |
| 45 | jint JNI_OnLoad(JavaVM* vm, void* /*reserved*/) |
| 46 | { |
| 47 | __android_log_write(ANDROID_LOG_DEBUG, WEBRTC_LOG_TAG, "JNI_OnLoad"); |
| 48 | if (!vm) |
| 49 | { |
| 50 | __android_log_write(ANDROID_LOG_ERROR, WEBRTC_LOG_TAG, |
| 51 | "JNI_OnLoad did not receive a valid VM pointer"); |
| 52 | return -1; |
| 53 | } |
| 54 | |
| 55 | // Get JNI |
| 56 | JNIEnv* env; |
| 57 | if (JNI_OK != vm->GetEnv(reinterpret_cast<void**> (&env), JNI_VERSION_1_4)) |
| 58 | { |
| 59 | __android_log_write(ANDROID_LOG_ERROR, WEBRTC_LOG_TAG, |
| 60 | "JNI_OnLoad could not get JNI env"); |
| 61 | return -1; |
| 62 | } |
| 63 | |
| 64 | // Init JniData data |
| 65 | memset(&jniData, 0, sizeof(jniData)); |
| 66 | |
| 67 | // Store the JVM |
| 68 | jniData.jvm = vm; |
| 69 | |
| 70 | return JNI_VERSION_1_4; |
| 71 | } |
| 72 | |
| 73 | ///////////////////////////////////////////// |
| 74 | // Run Test |
| 75 | // |
| 76 | JNIEXPORT jint JNICALL |
| 77 | Java_org_webrtc_capturemoduleandroidtest_VideoCaptureModuleTest_RunTest( |
| 78 | JNIEnv * env, |
| 79 | jobject context, |
| 80 | jobject surface) |
| 81 | { |
| 82 | __android_log_write(ANDROID_LOG_DEBUG, WEBRTC_LOG_TAG, "Run test"); |
| 83 | // Set instance independent Java objects |
| 84 | VideoCaptureModule::SetAndroidObjects(jniData.jvm, context); |
| 85 | |
| 86 | // Start test |
| 87 | __android_log_write(ANDROID_LOG_DEBUG, WEBRTC_LOG_TAG, |
| 88 | "Create testPlatformDependent"); |
| 89 | testPlatformDependent testPlatformDependent; |
| 90 | testPlatformDependent.SetRenderer(jniData.renderer); |
| 91 | testPlatformDependent.DoTest(); |
| 92 | |
| 93 | // Clear instance independent Java objects |
| 94 | VideoCaptureModule::SetAndroidObjects(NULL, NULL); |
| 95 | |
| 96 | return 0; |
| 97 | } |
| 98 | |
| 99 | JNIEXPORT jint JNICALL |
| 100 | Java_org_webrtc_capturemoduleandroidtest_VideoCaptureModuleTest_RenderInit( |
| 101 | JNIEnv * env, |
| 102 | jobject context, |
| 103 | jobject surface) |
| 104 | { |
| 105 | VideoRender::SetAndroidObjects(jniData.jvm); |
| 106 | #ifdef RENDER_PREVIEW |
| 107 | Renderer::SetRenderWindow(surface); |
| 108 | jniData.renderer=new Renderer(true); |
| 109 | #endif |
| 110 | } |
| 111 | |
| 112 | JNIEXPORT jint JNICALL |
| 113 | Java_org_webrtc_capturemoduleandroidtest_VideoCaptureModuleTest_StartCapture( |
| 114 | JNIEnv * env, |
| 115 | jobject context) |
| 116 | { |
| 117 | if (!jniData._captureInfo) { |
| 118 | VideoCaptureModule::SetAndroidObjects(jniData.jvm, context); |
| 119 | jniData._captureInfo = VideoCaptureFactory::CreateDeviceInfo(5); |
| 120 | WebRtc_UWord8 id[256]; |
| 121 | WebRtc_UWord8 name[256]; |
| 122 | jniData._captureInfo->GetDeviceName(0, name, 256, id, 256); |
| 123 | jniData._videoCapture = VideoCaptureFactory::Create(0, id); |
| 124 | VideoCaptureCapability capability; |
| 125 | |
| 126 | jniData._captureInfo->GetCapability(id, 0, capability); |
| 127 | capability.width = 176; |
| 128 | capability.height = 144; |
| 129 | capability.maxFPS = 15; |
| 130 | |
| 131 | jniData._videoCapture->StartCapture(capability); |
| 132 | } |
| 133 | return 0; |
| 134 | } |
| 135 | |
| 136 | JNIEXPORT jint JNICALL |
| 137 | Java_org_webrtc_capturemoduleandroidtest_VideoCaptureModuleTest_StopCapture( |
| 138 | JNIEnv * env, |
| 139 | jobject context) |
| 140 | { |
| 141 | if (jniData._videoCapture) { |
| 142 | jniData._videoCapture->StopCapture(); |
| 143 | delete jniData._captureInfo; |
| 144 | VideoCaptureModule::Destroy(jniData._videoCapture); |
| 145 | jniData._videoCapture = NULL; |
| 146 | jniData._captureInfo = NULL; |
| 147 | } |
| 148 | return 0; |
| 149 | } |