blob: bf3ab7666f9970ef8bcd182d95cd818dbc6448c9 [file] [log] [blame]
Derek Sollenbergerb8947ee2009-10-05 14:40:18 -04001/*
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
35extern NPNetscapeFuncs* browser;
36extern ANPBitmapInterfaceV0 gBitmapI;
37extern ANPCanvasInterfaceV0 gCanvasI;
38extern ANPLogInterfaceV0 gLogI;
39extern ANPPaintInterfaceV0 gPaintI;
40extern ANPSurfaceInterfaceV0 gSurfaceI;
Derek Sollenbergerd53b56d2010-01-11 12:31:49 -050041extern ANPSystemInterfaceV0 gSystemI;
Derek Sollenbergerb8947ee2009-10-05 14:40:18 -040042extern ANPTypefaceInterfaceV0 gTypefaceI;
Derek Sollenbergere62ce172009-11-30 11:52:06 -050043extern ANPWindowInterfaceV0 gWindowI;
Derek Sollenbergerb8947ee2009-10-05 14:40:18 -040044
45///////////////////////////////////////////////////////////////////////////////
46
47VideoPlugin::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 Sollenbergere62ce172009-11-30 11:52:06 -050056 gLogI.log(kError_ANPLogType, "Error selecting input events.");
Derek Sollenbergerb8947ee2009-10-05 14:40:18 -040057 }
58}
59
60VideoPlugin::~VideoPlugin() {
Derek Sollenbergerd53b56d2010-01-11 12:31:49 -050061 setContext(NULL);
62 destroySurface();
Derek Sollenbergerb8947ee2009-10-05 14:40:18 -040063}
64
Derek Sollenbergerd53b56d2010-01-11 12:31:49 -050065jobject 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 Sollenbergerb8947ee2009-10-05 14:40:18 -040096}
97
Derek Sollenbergerd53b56d2010-01-11 12:31:49 -050098void VideoPlugin::destroySurface() {
Derek Sollenbergerb8947ee2009-10-05 14:40:18 -040099 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 Murdoch3b6f45d2010-05-12 14:09:26 +0100106int16_t VideoPlugin::handleEvent(const ANPEvent* evt) {
Derek Sollenbergerb8947ee2009-10-05 14:40:18 -0400107 switch (evt->eventType) {
Derek Sollenbergerd53b56d2010-01-11 12:31:49 -0500108 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 Sollenbergerb8947ee2009-10-05 14:40:18 -0400119 case kDraw_ANPEventType:
Derek Sollenbergere62ce172009-11-30 11:52:06 -0500120 gLogI.log(kError_ANPLogType, " ------ %p the plugin did not request draw events", inst());
Derek Sollenbergerb8947ee2009-10-05 14:40:18 -0400121 break;
122 case kTouch_ANPEventType:
123 if (kDown_ANPTouchAction == evt->data.touch.action) {
Derek Sollenbergere62ce172009-11-30 11:52:06 -0500124 gLogI.log(kDebug_ANPLogType, " ------ %p requesting fullscreen mode", inst());
Derek Sollenbergerb8947ee2009-10-05 14:40:18 -0400125 gWindowI.requestFullScreen(inst());
126 }
127 return 1;
128 case kKey_ANPEventType:
Derek Sollenbergere62ce172009-11-30 11:52:06 -0500129 gLogI.log(kError_ANPLogType, " ------ %p the plugin did not request key events", inst());
Derek Sollenbergerb8947ee2009-10-05 14:40:18 -0400130 break;
131 default:
132 break;
133 }
134 return 0; // unknown or unhandled event
135}