• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
2  * Copyright (C) 2011 The Android Open Source Project
3  *
4  * Licensed under the Apache License, Version 2.0 (the "License");
5  * you may not use this file except in compliance with the License.
6  * You may obtain a copy of the License at
7  *
8  *      http://www.apache.org/licenses/LICENSE-2.0
9  *
10  * Unless required by applicable law or agreed to in writing, software
11  * distributed under the License is distributed on an "AS IS" BASIS,
12  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13  * See the License for the specific language governing permissions and
14  * limitations under the License.
15  */
16 
17 #ifndef MediaListener_h
18 #define MediaListener_h
19 
20 #if USE(ACCELERATED_COMPOSITING)
21 
22 #include <gui/SurfaceTexture.h>
23 #include <gui/SurfaceTextureClient.h>
24 #include <jni.h>
25 #include <JNIUtility.h>
26 #include "MediaTexture.h"
27 #include "WebCoreJni.h"
28 
29 namespace WebCore {
30 
31 class MediaListener : public android::SurfaceTexture::FrameAvailableListener {
32 
33 public:
MediaListener(jobject weakWebViewRef,const sp<android::SurfaceTexture> & surfaceTexture,const sp<ANativeWindow> & nativeWindow)34     MediaListener(jobject weakWebViewRef,
35                   const sp<android::SurfaceTexture>& surfaceTexture,
36                   const sp<ANativeWindow>& nativeWindow)
37         : m_weakWebViewRef(weakWebViewRef)
38         , m_postInvalMethod(0)
39         , m_frameAvailable(false)
40         , m_surfaceTexture(surfaceTexture)
41         , m_nativeWindow(nativeWindow)
42         , m_framerateCallback(0)
43     {
44         if (!m_weakWebViewRef)
45             return;
46 
47         JNIEnv* env = JSC::Bindings::getJNIEnv();
48         jobject localWebViewRef = env->NewLocalRef(m_weakWebViewRef);
49         if (localWebViewRef) {
50             jclass wvClass = env->GetObjectClass(localWebViewRef);
51             m_postInvalMethod = env->GetMethodID(wvClass, "postInvalidate", "()V");
52             env->DeleteLocalRef(wvClass);
53             env->DeleteLocalRef(localWebViewRef);
54         }
55         checkException(env);
56     }
57 
onFrameAvailable()58     virtual void onFrameAvailable()
59     {
60         JNIEnv* env = JSC::Bindings::getJNIEnv();
61         jobject localWebViewRef = env->NewLocalRef(m_weakWebViewRef);
62         if (localWebViewRef) {
63             env->CallVoidMethod(localWebViewRef, m_postInvalMethod);
64             env->DeleteLocalRef(localWebViewRef);
65         }
66         checkException(env);
67         if (!m_frameAvailable) {
68             m_frameAvailable = true;
69         }
70         if (m_framerateCallback)
71             m_framerateCallback(m_nativeWindow.get(), m_surfaceTexture->getTimestamp());
72     }
73 
isFrameAvailable()74     bool isFrameAvailable() { return m_frameAvailable; }
setFramerateCallback(FramerateCallbackProc callback)75     void setFramerateCallback(FramerateCallbackProc callback) { m_framerateCallback = callback; }
76 
77 private:
78     jobject m_weakWebViewRef;
79     jmethodID m_postInvalMethod;
80     bool m_frameAvailable;
81     sp<android::SurfaceTexture> m_surfaceTexture;
82     sp<ANativeWindow> m_nativeWindow;
83     FramerateCallbackProc m_framerateCallback;
84 };
85 
86 } // namespace WebCore
87 
88 #endif // USE(ACCELERATED_COMPOSITING)
89 #endif  // MediaListener_h
90