Derek Sollenberger | b8947ee | 2009-10-05 14:40:18 -0400 | [diff] [blame] | 1 | /* |
| 2 | * Copyright 2009, The Android Open Source Project |
| 3 | * |
| 4 | * Redistribution and use in source and binary forms, with or without |
| 5 | * modification, are permitted provided that the following conditions |
| 6 | * are met: |
| 7 | * * Redistributions of source code must retain the above copyright |
| 8 | * notice, this list of conditions and the following disclaimer. |
| 9 | * * Redistributions in binary form must reproduce the above copyright |
| 10 | * notice, this list of conditions and the following disclaimer in the |
| 11 | * documentation and/or other materials provided with the distribution. |
| 12 | * |
| 13 | * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS ``AS IS'' AND ANY |
| 14 | * EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE |
| 15 | * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR |
| 16 | * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL APPLE COMPUTER, INC. OR |
| 17 | * CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, |
| 18 | * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, |
| 19 | * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR |
| 20 | * PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY |
| 21 | * OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT |
| 22 | * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE |
| 23 | * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. |
| 24 | */ |
| 25 | |
| 26 | #include "VideoPlugin.h" |
| 27 | #include "android_npapi.h" |
| 28 | |
| 29 | #include <stdio.h> |
| 30 | #include <sys/time.h> |
| 31 | #include <time.h> |
| 32 | #include <math.h> |
| 33 | #include <string.h> |
| 34 | |
| 35 | extern NPNetscapeFuncs* browser; |
| 36 | extern ANPBitmapInterfaceV0 gBitmapI; |
| 37 | extern ANPCanvasInterfaceV0 gCanvasI; |
| 38 | extern ANPLogInterfaceV0 gLogI; |
| 39 | extern ANPPaintInterfaceV0 gPaintI; |
| 40 | extern ANPSurfaceInterfaceV0 gSurfaceI; |
Derek Sollenberger | d53b56d | 2010-01-11 12:31:49 -0500 | [diff] [blame] | 41 | extern ANPSystemInterfaceV0 gSystemI; |
Derek Sollenberger | b8947ee | 2009-10-05 14:40:18 -0400 | [diff] [blame] | 42 | extern ANPTypefaceInterfaceV0 gTypefaceI; |
Derek Sollenberger | e62ce17 | 2009-11-30 11:52:06 -0500 | [diff] [blame] | 43 | extern ANPWindowInterfaceV0 gWindowI; |
Derek Sollenberger | b8947ee | 2009-10-05 14:40:18 -0400 | [diff] [blame] | 44 | |
| 45 | /////////////////////////////////////////////////////////////////////////////// |
| 46 | |
| 47 | VideoPlugin::VideoPlugin(NPP inst) : SurfaceSubPlugin(inst) { |
| 48 | |
| 49 | // initialize the drawing surface |
| 50 | m_surface = NULL; |
| 51 | |
| 52 | //register for touch events |
| 53 | ANPEventFlags flags = kTouch_ANPEventFlag; |
| 54 | NPError err = browser->setvalue(inst, kAcceptEvents_ANPSetValue, &flags); |
| 55 | if (err != NPERR_NO_ERROR) { |
Derek Sollenberger | e62ce17 | 2009-11-30 11:52:06 -0500 | [diff] [blame] | 56 | gLogI.log(kError_ANPLogType, "Error selecting input events."); |
Derek Sollenberger | b8947ee | 2009-10-05 14:40:18 -0400 | [diff] [blame] | 57 | } |
| 58 | } |
| 59 | |
| 60 | VideoPlugin::~VideoPlugin() { |
Derek Sollenberger | d53b56d | 2010-01-11 12:31:49 -0500 | [diff] [blame] | 61 | setContext(NULL); |
| 62 | destroySurface(); |
Derek Sollenberger | b8947ee | 2009-10-05 14:40:18 -0400 | [diff] [blame] | 63 | } |
| 64 | |
Derek Sollenberger | d53b56d | 2010-01-11 12:31:49 -0500 | [diff] [blame] | 65 | jobject VideoPlugin::getSurface() { |
| 66 | |
| 67 | if (m_surface) { |
| 68 | return m_surface; |
| 69 | } |
| 70 | |
| 71 | // load the appropriate java class and instantiate it |
| 72 | JNIEnv* env = NULL; |
| 73 | if (gVM->GetEnv((void**) &env, JNI_VERSION_1_4) != JNI_OK) { |
| 74 | gLogI.log(kError_ANPLogType, " ---- getSurface: failed to get env"); |
| 75 | return NULL; |
| 76 | } |
| 77 | |
| 78 | const char* className = "com.android.sampleplugin.VideoSurface"; |
| 79 | jclass videoClass = gSystemI.loadJavaClass(inst(), className); |
| 80 | |
| 81 | if(!videoClass) { |
| 82 | gLogI.log(kError_ANPLogType, " ---- getSurface: failed to load class"); |
| 83 | return NULL; |
| 84 | } |
| 85 | |
| 86 | jmethodID constructor = env->GetMethodID(videoClass, "<init>", "(Landroid/content/Context;)V"); |
| 87 | jobject videoSurface = env->NewObject(videoClass, constructor, m_context); |
| 88 | |
| 89 | if(!videoSurface) { |
| 90 | gLogI.log(kError_ANPLogType, " ---- getSurface: failed to construct object"); |
| 91 | return NULL; |
| 92 | } |
| 93 | |
| 94 | m_surface = env->NewGlobalRef(videoSurface); |
| 95 | return m_surface; |
Derek Sollenberger | b8947ee | 2009-10-05 14:40:18 -0400 | [diff] [blame] | 96 | } |
| 97 | |
Derek Sollenberger | d53b56d | 2010-01-11 12:31:49 -0500 | [diff] [blame] | 98 | void VideoPlugin::destroySurface() { |
Derek Sollenberger | b8947ee | 2009-10-05 14:40:18 -0400 | [diff] [blame] | 99 | JNIEnv* env = NULL; |
| 100 | if (m_surface && gVM->GetEnv((void**) &env, JNI_VERSION_1_4) == JNI_OK) { |
| 101 | env->DeleteGlobalRef(m_surface); |
| 102 | m_surface = NULL; |
| 103 | } |
| 104 | } |
| 105 | |
Ben Murdoch | 3b6f45d | 2010-05-12 14:09:26 +0100 | [diff] [blame] | 106 | int16_t VideoPlugin::handleEvent(const ANPEvent* evt) { |
Derek Sollenberger | b8947ee | 2009-10-05 14:40:18 -0400 | [diff] [blame] | 107 | switch (evt->eventType) { |
Derek Sollenberger | d53b56d | 2010-01-11 12:31:49 -0500 | [diff] [blame] | 108 | case kLifecycle_ANPEventType: { |
| 109 | switch (evt->data.lifecycle.action) { |
| 110 | case kEnterFullScreen_ANPLifecycleAction: |
| 111 | gLogI.log(kDebug_ANPLogType, " ---- %p entering fullscreen", inst()); |
| 112 | break; |
| 113 | case kExitFullScreen_ANPLifecycleAction: |
| 114 | gLogI.log(kDebug_ANPLogType, " ---- %p exiting fullscreen", inst()); |
| 115 | break; |
| 116 | } |
| 117 | break; // end kLifecycle_ANPEventType |
| 118 | } |
Derek Sollenberger | b8947ee | 2009-10-05 14:40:18 -0400 | [diff] [blame] | 119 | case kDraw_ANPEventType: |
Derek Sollenberger | e62ce17 | 2009-11-30 11:52:06 -0500 | [diff] [blame] | 120 | gLogI.log(kError_ANPLogType, " ------ %p the plugin did not request draw events", inst()); |
Derek Sollenberger | b8947ee | 2009-10-05 14:40:18 -0400 | [diff] [blame] | 121 | break; |
| 122 | case kTouch_ANPEventType: |
| 123 | if (kDown_ANPTouchAction == evt->data.touch.action) { |
Derek Sollenberger | e62ce17 | 2009-11-30 11:52:06 -0500 | [diff] [blame] | 124 | gLogI.log(kDebug_ANPLogType, " ------ %p requesting fullscreen mode", inst()); |
Derek Sollenberger | b8947ee | 2009-10-05 14:40:18 -0400 | [diff] [blame] | 125 | gWindowI.requestFullScreen(inst()); |
| 126 | } |
| 127 | return 1; |
| 128 | case kKey_ANPEventType: |
Derek Sollenberger | e62ce17 | 2009-11-30 11:52:06 -0500 | [diff] [blame] | 129 | gLogI.log(kError_ANPLogType, " ------ %p the plugin did not request key events", inst()); |
Derek Sollenberger | b8947ee | 2009-10-05 14:40:18 -0400 | [diff] [blame] | 130 | break; |
| 131 | default: |
| 132 | break; |
| 133 | } |
| 134 | return 0; // unknown or unhandled event |
| 135 | } |