2 * Copyright (C) 2011 The Android Open Source Project
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
8 * http://www.apache.org/licenses/LICENSE-2.0
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.
17 #ifndef MediaListener_h
18 #define MediaListener_h
20 #if USE(ACCELERATED_COMPOSITING)
22 #include <gui/SurfaceTexture.h>
23 #include <gui/SurfaceTextureClient.h>
25 #include <JNIUtility.h>
26 #include "MediaTexture.h"
27 #include "WebCoreJni.h"
31 #include <cutils/log.h>
32 #include <wtf/text/CString.h>
35 #define XLOG(...) android_printLog(ANDROID_LOG_DEBUG, "MediaListener", __VA_ARGS__)
46 class MediaListener : public android::SurfaceTexture::FrameAvailableListener {
49 MediaListener(jobject weakWebViewRef,
50 const sp<android::SurfaceTexture>& surfaceTexture,
51 const sp<ANativeWindow>& nativeWindow)
52 : m_weakWebViewRef(weakWebViewRef)
53 , m_postInvalMethod(0)
54 , m_frameAvailable(false)
55 , m_surfaceTexture(surfaceTexture)
56 , m_nativeWindow(nativeWindow)
57 , m_framerateCallback(0)
59 if (!m_weakWebViewRef)
62 JNIEnv* env = JSC::Bindings::getJNIEnv();
63 jobject localWebViewRef = env->NewLocalRef(m_weakWebViewRef);
64 if (localWebViewRef) {
65 jclass wvClass = env->GetObjectClass(localWebViewRef);
66 m_postInvalMethod = env->GetMethodID(wvClass, "postInvalidate", "()V");
67 env->DeleteLocalRef(wvClass);
68 env->DeleteLocalRef(localWebViewRef);
73 virtual void onFrameAvailable()
75 JNIEnv* env = JSC::Bindings::getJNIEnv();
76 jobject localWebViewRef = env->NewLocalRef(m_weakWebViewRef);
77 if (localWebViewRef) {
78 env->CallVoidMethod(localWebViewRef, m_postInvalMethod);
79 env->DeleteLocalRef(localWebViewRef);
82 if (!m_frameAvailable) {
83 m_frameAvailable = true;
85 if (m_framerateCallback)
86 m_framerateCallback(m_nativeWindow.get(), m_surfaceTexture->getTimestamp());
89 bool isFrameAvailable() { return m_frameAvailable; }
90 void setFramerateCallback(FramerateCallbackProc callback) { m_framerateCallback = callback; }
93 jobject m_weakWebViewRef;
94 jmethodID m_postInvalMethod;
95 bool m_frameAvailable;
96 sp<android::SurfaceTexture> m_surfaceTexture;
97 sp<ANativeWindow> m_nativeWindow;
98 FramerateCallbackProc m_framerateCallback;
101 } // namespace WebCore
103 #endif // USE(ACCELERATED_COMPOSITING)
104 #endif // MediaListener_h