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;
41 extern ANPSystemInterfaceV0 gSystemI;
42 extern ANPTypefaceInterfaceV0 gTypefaceI;
43 extern ANPWindowInterfaceV0 gWindowI;
44
45 ///////////////////////////////////////////////////////////////////////////////
46
VideoPlugin(NPP inst)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) {
56 gLogI.log(kError_ANPLogType, "Error selecting input events.");
57 }
58 }
59
~VideoPlugin()60 VideoPlugin::~VideoPlugin() {
61 setContext(NULL);
62 destroySurface();
63 }
64
getSurface()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;
96 }
97
destroySurface()98 void VideoPlugin::destroySurface() {
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
handleEvent(const ANPEvent * evt)106 int16_t VideoPlugin::handleEvent(const ANPEvent* evt) {
107 switch (evt->eventType) {
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 }
119 case kDraw_ANPEventType:
120 gLogI.log(kError_ANPLogType, " ------ %p the plugin did not request draw events", inst());
121 break;
122 case kTouch_ANPEventType:
123 if (kDown_ANPTouchAction == evt->data.touch.action) {
124 gLogI.log(kDebug_ANPLogType, " ------ %p requesting fullscreen mode", inst());
125 gWindowI.requestFullScreen(inst());
126 }
127 return 1;
128 case kKey_ANPEventType:
129 gLogI.log(kError_ANPLogType, " ------ %p the plugin did not request key events", inst());
130 break;
131 default:
132 break;
133 }
134 return 0; // unknown or unhandled event
135 }
136