tagging audio streams and changing audio sink to pulseaudio
[profile/ivi/webkit-efl.git] / Source / WebCore / platform / graphics / gstreamer / VideoLayerTizen.cpp
1 /*
2     Copyright (C) 2012 Samsung Electronics.
3
4     This library is free software; you can redistribute it and/or
5     modify it under the terms of the GNU Library General Public
6     License as published by the Free Software Foundation; either
7     version 2.1 of the License, or (at your option) any later version.
8
9     This library is distributed in the hope that it will be useful,
10     but WITHOUT ANY WARRANTY; without even the implied warranty of
11     MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
12     Library General Public License for more details.
13
14     You should have received a copy of the GNU Library General Public License
15     along with this library; see the file COPYING.LIB.  If not, write to
16     the Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor,
17     Boston, MA 02110-1301, USA.
18 */
19
20 #include "config.h"
21 #include "VideoLayerTizen.h"
22
23 #if ENABLE(TIZEN_ACCELERATED_COMPOSITING) && USE(TIZEN_TEXTURE_MAPPER)
24
25 #include "GraphicsContext.h"
26 #include "IntRect.h"
27 #include "RenderLayerBacking.h"
28
29 #if ENABLE(TIZEN_WEBKIT2_TILED_AC_SHARED_PLATFORM_SURFACE)
30 #include <gst/interfaces/xoverlay.h>
31 #include <gst/video/gstvideosink.h>
32 #include <gst/video/video.h>
33 #else
34 #include <cairo-xlib.h>
35 #include <X11/Xlib.h>
36 #include <X11/Xutil.h>
37 #endif // ENABLE(TIZEN_WEBKIT2_TILED_AC_SHARED_PLATFORM_SURFACE)
38
39 #if !ENABLE(TIZEN_WEBKIT2_TILED_AC_SHARED_PLATFORM_SURFACE)
40 static Display* g_nativeDisplay = 0;
41 static int g_nativeWindow = 0;
42 #endif
43
44 namespace WebCore {
45
46 #if USE(ACCELERATED_VIDEO_VAAPI)
47 void setVaapiEnv()
48 {
49     static bool envSet = false;
50     if (!envSet) {
51         envSet = true;
52         setenv("PSB_VIDEO_CTEXTURE", "1", true);
53
54     }
55 }
56 #endif
57
58 VideoLayerTizen::VideoLayerTizen(HTMLMediaElement* media)
59     : m_media(media)
60 #if ENABLE(TIZEN_WEBKIT2_TILED_AC_SHARED_PLATFORM_SURFACE)
61     , m_videoSink(0)
62 #else
63     , m_platformSurfaceID(0)
64 #endif // ENABLE(TIZEN_WEBKIT2_TILED_AC_SHARED_PLATFORM_SURFACE)
65 {
66 #if !ENABLE(TIZEN_WEBKIT2_TILED_AC_SHARED_PLATFORM_SURFACE)
67     if (!g_nativeDisplay)
68            g_nativeDisplay = XOpenDisplay(0);
69
70        if (!g_nativeDisplay)
71            return;
72
73        g_nativeWindow = XCreateSimpleWindow(g_nativeDisplay, XDefaultRootWindow(g_nativeDisplay),
74                                0, 0, 1, 1, 0,
75                                BlackPixel(g_nativeDisplay, 0), WhitePixel(g_nativeDisplay, 0));
76        XFlush(g_nativeDisplay);
77 #endif
78 }
79
80 VideoLayerTizen::~VideoLayerTizen()
81 {
82     syncLayer(0);
83
84 #if ENABLE(TIZEN_WEBKIT2_TILED_AC_SHARED_PLATFORM_SURFACE)
85     if (m_platformSurface)
86         m_platformSurface.clear();
87
88     if (m_platformSurfaceToBeRemoved)
89         m_platformSurfaceToBeRemoved.clear();
90
91     m_videoSink = 0;
92 #else
93     if (m_platformSurfaceID) {
94         XFreePixmap(g_nativeDisplay, m_platformSurfaceID);
95         m_platformSurfaceID = 0;
96     }
97     if (g_nativeWindow) {
98         XDestroyWindow(g_nativeDisplay, g_nativeWindow);
99         g_nativeWindow = 0;
100     }
101 #endif // ENABLE(TIZEN_WEBKIT2_TILED_AC_SHARED_PLATFORM_SURFACE)
102 }
103
104 PlatformLayer* VideoLayerTizen::platformLayer() const
105 {
106     return const_cast<TextureMapperPlatformLayer*>(static_cast<const TextureMapperPlatformLayer*>(this));
107 }
108
109 void VideoLayerTizen::syncLayer(VideoLayerTizen* layer)
110 {
111     RenderBox* renderBox = m_media->renderBox();
112     if (renderBox && renderBox->hasAcceleratedCompositing()) {
113         RenderLayer* renderLayer = renderBox->layer();
114         if (renderLayer && renderLayer->isComposited()) {
115             GraphicsLayer* graphicsLayer = renderLayer->backing()->graphicsLayer();
116             if (graphicsLayer)
117                 graphicsLayer->setContentsToMedia(layer);
118         }
119     }
120 }
121
122 #if ENABLE(TIZEN_WEBKIT2_TILED_AC_SHARED_PLATFORM_SURFACE)
123 static gboolean platformSurfaceUpdateTimeoutCallback(VideoLayerTizen* layer)
124 {
125     layer->notifySyncRequired();
126     return FALSE;
127 }
128
129 bool VideoLayerTizen::swapPlatformSurfaces()
130 {
131     return true;
132 }
133
134 void VideoLayerTizen::freePlatformSurface(int id)
135 {
136     if (m_platformSurfaceToBeRemoved)
137         m_platformSurfaceToBeRemoved.clear();
138 }
139
140 void VideoLayerTizen::platformSurfaceUpdated()
141 {
142     notifySyncRequired();
143 }
144
145 GstElement* VideoLayerTizen::createVideoSink()
146 {
147 #if !USE(ACCELERATED_VIDEO_VAAPI)
148     GstElement* videoSink = gst_element_factory_make("xvimagesink", "xvimagesink");
149     g_object_set(videoSink, "rotate", 0, NULL);
150 #else
151     setVaapiEnv();
152     GstElement* videoSink = gst_element_factory_make("vaapisink", "vaapisink");
153     g_object_set(videoSink, "is-pixmap", 1, NULL);
154 #endif
155     m_videoSink = videoSink;
156
157     return videoSink;
158 }
159
160 void VideoLayerTizen::notifySyncRequired()
161 {
162     if (!m_platformSurface)
163         return;
164
165     syncLayer(this);
166 }
167
168 void VideoLayerTizen::setOverlay(IntSize size)
169 {
170     if (size.isEmpty())
171         return;
172
173     if (!m_videoSink)
174         return;
175
176     if (m_platformSurface)
177         m_platformSurfaceToBeRemoved = m_platformSurface.release();
178
179     m_platformSurface = SharedVideoPlatformSurfaceTizen::create(size);
180     m_platformSurface->setVideoPlatformSurfaceUpdateListener(this);
181
182     gst_x_overlay_set_window_handle(GST_X_OVERLAY(m_videoSink), m_platformSurface->id());
183 #if !USE(ACCELERATED_VIDEO_VAAPI)
184     g_object_set(m_videoSink, "rotate", 0, NULL);
185 #else
186     g_object_set(m_videoSink, "is-pixmap", 1, NULL);
187 #endif
188 }
189
190 void VideoLayerTizen::paintCurrentFrameInContext(GraphicsContext* context, const IntRect& rect)
191 {
192     if (m_platformSurface)
193         m_platformSurface->paintCurrentFrameInContext(context, rect);
194 }
195 #else
196 void VideoLayerTizen::paintVideoLayer(IntSize videoSize)
197 {
198     if (videoSize.isEmpty())
199         return;
200
201     if (m_videoSize != videoSize) {
202         m_videoSize = videoSize;
203
204         if (m_platformSurfaceID) {
205             XFreePixmap(g_nativeDisplay, m_platformSurfaceID);
206             m_platformSurfaceID = 0;
207         }
208         m_platformSurfaceID = XCreatePixmap(g_nativeDisplay, g_nativeWindow, m_videoSize.width(), m_videoSize.height(), DefaultDepth(g_nativeDisplay, DefaultScreen(g_nativeDisplay)));
209         XFlush(g_nativeDisplay);
210     }
211
212     RefPtr<cairo_surface_t> surface = adoptRef(cairo_xlib_surface_create(g_nativeDisplay, m_platformSurfaceID, DefaultVisual(g_nativeDisplay, DefaultScreen(g_nativeDisplay)), m_videoSize.width(), m_videoSize.height()));
213     if (cairo_surface_status(surface.get()) != CAIRO_STATUS_SUCCESS)
214        return;
215
216     RefPtr<cairo_t> cr = adoptRef(cairo_create(surface.get()));
217     OwnPtr<WebCore::GraphicsContext> context = adoptPtr(new GraphicsContext(cr.get()));
218     m_media->player()->paint(context.get(), IntRect(0, 0, m_videoSize.width(), m_videoSize.height()));
219
220     syncLayer(this);
221 }
222 #endif // ENABLE(TIZEN_WEBKIT2_TILED_AC_SHARED_PLATFORM_SURFACE)
223
224 #if USE(GRAPHICS_SURFACE) || ENABLE(TIZEN_CANVAS_GRAPHICS_SURFACE)
225 uint64_t VideoLayerTizen::graphicsSurfaceToken() const
226 {
227 #if ENABLE(TIZEN_WEBKIT2_TILED_AC_SHARED_PLATFORM_SURFACE)
228     return (m_platformSurface ? m_platformSurface->id() : 0);
229 #else
230     return m_platformSurfaceID;
231 #endif
232 }
233
234 uint32_t VideoLayerTizen::copyToGraphicsSurface()
235 {
236 #if ENABLE(TIZEN_WEBKIT2_TILED_AC_SHARED_PLATFORM_SURFACE)
237     return m_platformSurface ? m_platformSurface->id() : 0;
238 #else
239     return m_platformSurfaceID;
240 #endif
241 }
242
243 int VideoLayerTizen::graphicsSurfaceFlags() const
244 {
245 #if ENABLE(TIZEN_WEBKIT2_TILED_AC_SHARED_PLATFORM_SURFACE)
246     return m_platformSurface ? m_platformSurface->graphicsSurfaceFlags() : GraphicsSurface::Is2D;
247 #else
248     return GraphicsSurface::Is2D;
249 #endif
250 }
251 #endif // USE(GRAPHICS_SURFACE) || ENABLE(TIZEN_CANVAS_GRAPHICS_SURFACE)
252
253 } // WebCore
254
255 #endif // ENABLE(TIZEN_ACCELERATED_COMPOSITING) && USE(TIZEN_TEXTURE_MAPPER)