Bump version to 6.4-15
[LibreOffice.git] / slideshow / source / engine / shapes / viewmediashape.cxx
blobc17b550372b10cdf00705a30019552e357219353
1 /* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */
2 /*
3 * This file is part of the LibreOffice project.
5 * This Source Code Form is subject to the terms of the Mozilla Public
6 * License, v. 2.0. If a copy of the MPL was not distributed with this
7 * file, You can obtain one at http://mozilla.org/MPL/2.0/.
9 * This file incorporates work covered by the following license notice:
11 * Licensed to the Apache Software Foundation (ASF) under one or more
12 * contributor license agreements. See the NOTICE file distributed
13 * with this work for additional information regarding copyright
14 * ownership. The ASF licenses this file to you under the Apache
15 * License, Version 2.0 (the "License"); you may not use this file
16 * except in compliance with the License. You may obtain a copy of
17 * the License at http://www.apache.org/licenses/LICENSE-2.0 .
20 #include <config_features.h>
22 #include <tools/diagnose_ex.h>
24 #include <math.h>
26 #include <comphelper/anytostring.hxx>
27 #include <cppuhelper/exc_hlp.hxx>
29 #include <sal/log.hxx>
30 #include <vcl/canvastools.hxx>
31 #include <vcl/syschild.hxx>
32 #include <vcl/sysdata.hxx>
33 #include <vcl/window.hxx>
34 #include <vcl/graph.hxx>
36 #include <basegfx/utils/canvastools.hxx>
37 #include <basegfx/matrix/b2dhommatrixtools.hxx>
38 #include <basegfx/numeric/ftools.hxx>
39 #include <basegfx/polygon/b2dpolygon.hxx>
40 #include <basegfx/point/b2dpoint.hxx>
41 #include <basegfx/matrix/b2dhommatrix.hxx>
42 #include <basegfx/polygon/b2dpolygontools.hxx>
43 #include <basegfx/range/b2irange.hxx>
44 #include <canvas/canvastools.hxx>
45 #include <cppcanvas/vclfactory.hxx>
46 #include <cppcanvas/basegfxfactory.hxx>
47 #include <avmedia/mediawindow.hxx>
49 #if HAVE_FEATURE_OPENGL
50 #include <vcl/opengl/OpenGLContext.hxx>
51 #endif
53 #include <com/sun/star/awt/XWindow.hpp>
54 #include <com/sun/star/beans/XPropertySet.hpp>
55 #include <com/sun/star/lang/XComponent.hpp>
56 #include <com/sun/star/lang/XMultiComponentFactory.hpp>
57 #include <com/sun/star/lang/NoSupportException.hpp>
58 #include <com/sun/star/media/XManager.hpp>
59 #include <com/sun/star/media/XPlayer.hpp>
60 #include <com/sun/star/media/XPlayerWindow.hpp>
61 #include <com/sun/star/presentation/XSlideShowView.hpp>
62 #include <com/sun/star/rendering/XCanvas.hpp>
64 #include "viewmediashape.hxx"
65 #include "mediashape.hxx"
66 #include <tools.hxx>
67 #include <unoview.hxx>
69 using namespace ::com::sun::star;
71 namespace slideshow
73 namespace internal
75 ViewMediaShape::ViewMediaShape( const ViewLayerSharedPtr& rViewLayer,
76 const uno::Reference< drawing::XShape >& rxShape,
77 const uno::Reference< uno::XComponentContext >& rxContext ) :
78 mpViewLayer( rViewLayer ),
79 maWindowOffset( 0, 0 ),
80 maBounds(),
81 mxShape( rxShape ),
82 mxPlayer(),
83 mxPlayerWindow(),
84 mxComponentContext( rxContext ),
85 mbIsSoundEnabled(true)
87 ENSURE_OR_THROW( mxShape.is(), "ViewMediaShape::ViewMediaShape(): Invalid Shape" );
88 ENSURE_OR_THROW( mpViewLayer, "ViewMediaShape::ViewMediaShape(): Invalid View" );
89 ENSURE_OR_THROW( mpViewLayer->getCanvas(), "ViewMediaShape::ViewMediaShape(): Invalid ViewLayer canvas" );
90 ENSURE_OR_THROW( mxComponentContext.is(), "ViewMediaShape::ViewMediaShape(): Invalid component context" );
92 UnoViewSharedPtr xUnoView(std::dynamic_pointer_cast<UnoView>(rViewLayer));
93 if (xUnoView)
95 mbIsSoundEnabled = xUnoView->isSoundEnabled();
99 ViewMediaShape::~ViewMediaShape()
103 endMedia();
105 catch (const uno::Exception &)
107 TOOLS_WARN_EXCEPTION("slideshow", "");
111 const ViewLayerSharedPtr& ViewMediaShape::getViewLayer() const
113 return mpViewLayer;
116 void ViewMediaShape::startMedia()
118 if( !mxPlayer.is() )
119 implInitialize( maBounds );
121 if (mxPlayer.is())
122 mxPlayer->start();
125 void ViewMediaShape::endMedia()
127 // shutdown player window
128 if( mxPlayerWindow.is() )
130 mxPlayerWindow->dispose();
131 mxPlayerWindow.clear();
134 mpMediaWindow.disposeAndClear();
136 // shutdown player
137 if( mxPlayer.is() )
139 mxPlayer->stop();
141 uno::Reference< lang::XComponent > xComponent( mxPlayer, uno::UNO_QUERY );
143 if( xComponent.is() )
144 xComponent->dispose();
146 mxPlayer.clear();
150 void ViewMediaShape::pauseMedia()
152 if (mxPlayer.is())
153 mxPlayer->stop();
156 void ViewMediaShape::setMediaTime(double fTime)
158 if (mxPlayer.is())
159 mxPlayer->setMediaTime(fTime);
162 bool ViewMediaShape::render( const ::basegfx::B2DRectangle& rBounds ) const
164 #if !HAVE_FEATURE_AVMEDIA
165 (void) rBounds;
166 #else
167 ::cppcanvas::CanvasSharedPtr pCanvas = mpViewLayer->getCanvas();
169 if( !pCanvas )
170 return false;
172 if( !mpMediaWindow.get() && !mxPlayerWindow.is() )
174 uno::Reference< graphic::XGraphic > xGraphic;
175 uno::Reference< beans::XPropertySet > xPropSet( mxShape, uno::UNO_QUERY );
176 if (xPropSet.is())
178 xPropSet->getPropertyValue("FallbackGraphic") >>= xGraphic;
181 Graphic aGraphic(xGraphic);
182 const BitmapEx aBmp = aGraphic.GetBitmapEx();
184 uno::Reference< rendering::XBitmap > xBitmap(vcl::unotools::xBitmapFromBitmapEx(aBmp));
186 rendering::ViewState aViewState;
187 aViewState.AffineTransform = pCanvas->getViewState().AffineTransform;
189 rendering::RenderState aRenderState;
190 ::canvas::tools::initRenderState( aRenderState );
192 const ::Size aBmpSize( aBmp.GetSizePixel() );
194 const ::basegfx::B2DVector aScale( rBounds.getWidth() / aBmpSize.Width(),
195 rBounds.getHeight() / aBmpSize.Height() );
196 const basegfx::B2DHomMatrix aTranslation(basegfx::utils::createScaleTranslateB2DHomMatrix(
197 aScale, rBounds.getMinimum()));
198 ::canvas::tools::setRenderStateTransform( aRenderState, aTranslation );
200 pCanvas->getUNOCanvas()->drawBitmap( xBitmap,
201 aViewState,
202 aRenderState );
204 #endif
205 return true;
208 bool ViewMediaShape::resize( const ::basegfx::B2DRectangle& rNewBounds ) const
210 maBounds = rNewBounds;
212 ::cppcanvas::CanvasSharedPtr pCanvas = mpViewLayer->getCanvas();
214 if( !pCanvas )
215 return false;
217 if( !mxPlayerWindow.is() )
218 return true;
220 uno::Reference< beans::XPropertySet > xPropSet( pCanvas->getUNOCanvas()->getDevice(),
221 uno::UNO_QUERY );
223 uno::Reference< awt::XWindow > xParentWindow;
224 if( xPropSet.is() &&
225 getPropertyValue( xParentWindow,
226 xPropSet,
227 "Window") )
229 const awt::Rectangle aRect( xParentWindow->getPosSize() );
231 maWindowOffset.X = aRect.X;
232 maWindowOffset.Y = aRect.Y;
235 ::basegfx::B2DRange aTmpRange;
236 ::canvas::tools::calcTransformedRectBounds( aTmpRange,
237 rNewBounds,
238 mpViewLayer->getTransformation() );
239 const ::basegfx::B2IRange& rRangePix(
240 ::basegfx::unotools::b2ISurroundingRangeFromB2DRange( aTmpRange ));
242 mxPlayerWindow->setEnable( !rRangePix.isEmpty() );
244 if( rRangePix.isEmpty() )
245 return true;
247 awt::Rectangle aCanvasArea;
248 UnoViewSharedPtr xUnoView(std::dynamic_pointer_cast<UnoView>(mpViewLayer));
249 if (xUnoView)
250 aCanvasArea = xUnoView->getUnoView()->getCanvasArea();
252 const Point aPosPixel( rRangePix.getMinX() + maWindowOffset.X + aCanvasArea.X,
253 rRangePix.getMinY() + maWindowOffset.Y + aCanvasArea.Y );
254 const Size aSizePixel( rRangePix.getMaxX() - rRangePix.getMinX(),
255 rRangePix.getMaxY() - rRangePix.getMinY() );
257 if( mpMediaWindow.get() )
259 mpMediaWindow->SetPosSizePixel( aPosPixel, aSizePixel );
260 mxPlayerWindow->setPosSize( 0, 0,
261 aSizePixel.Width(), aSizePixel.Height(),
262 0 );
264 else
266 mxPlayerWindow->setPosSize( aPosPixel.X(), aPosPixel.Y(),
267 aSizePixel.Width(), aSizePixel.Height(),
268 0 );
271 return true;
275 bool ViewMediaShape::implInitialize( const ::basegfx::B2DRectangle& rBounds )
277 if( !mxPlayer.is() && mxShape.is() )
279 ENSURE_OR_RETURN_FALSE( mpViewLayer->getCanvas(),
280 "ViewMediaShape::implInitialize(): Invalid layer canvas" );
282 uno::Reference< rendering::XCanvas > xCanvas( mpViewLayer->getCanvas()->getUNOCanvas() );
284 if( xCanvas.is() )
286 uno::Reference< beans::XPropertySet > xPropSet;
289 xPropSet.set( mxShape, uno::UNO_QUERY );
290 OUString sMimeType;
292 // create Player
293 if (xPropSet.is())
295 OUString aURL;
296 xPropSet->getPropertyValue("MediaMimeType") >>= sMimeType;
297 if ((xPropSet->getPropertyValue("PrivateTempFileURL") >>= aURL)
298 && !aURL.isEmpty())
300 implInitializeMediaPlayer( aURL, sMimeType );
302 else if (xPropSet->getPropertyValue("MediaURL") >>= aURL)
304 implInitializeMediaPlayer( aURL, sMimeType );
308 // create visible object
309 uno::Sequence< uno::Any > aDeviceParams;
311 if( ::canvas::tools::getDeviceInfo( xCanvas, aDeviceParams ).getLength() > 1 )
313 implInitializePlayerWindow( rBounds, aDeviceParams, sMimeType );
316 // set player properties
317 implSetMediaProperties( xPropSet );
319 catch( uno::RuntimeException& )
321 throw;
323 catch( uno::Exception& )
325 TOOLS_WARN_EXCEPTION( "slideshow", "" );
330 return mxPlayer.is() || mxPlayerWindow.is();
334 void ViewMediaShape::implSetMediaProperties( const uno::Reference< beans::XPropertySet >& rxProps )
336 if( !mxPlayer.is() )
337 return;
339 mxPlayer->setMediaTime( 0.0 );
341 if( !rxProps.is() )
342 return;
344 bool bLoop( false );
345 getPropertyValue( bLoop,
346 rxProps,
347 "Loop");
348 mxPlayer->setPlaybackLoop( bLoop );
350 bool bMute( false );
351 getPropertyValue( bMute,
352 rxProps,
353 "Mute");
354 mxPlayer->setMute( bMute || !mbIsSoundEnabled);
356 sal_Int16 nVolumeDB(0);
357 getPropertyValue( nVolumeDB,
358 rxProps,
359 "VolumeDB");
360 mxPlayer->setVolumeDB( nVolumeDB );
362 if( mxPlayerWindow.is() )
364 media::ZoomLevel eZoom(media::ZoomLevel_FIT_TO_WINDOW);
365 getPropertyValue( eZoom,
366 rxProps,
367 "Zoom");
368 mxPlayerWindow->setZoomLevel( eZoom );
373 void ViewMediaShape::implInitializeMediaPlayer( const OUString& rMediaURL, const OUString& rMimeType )
375 #if !HAVE_FEATURE_AVMEDIA
376 (void) rMediaURL;
377 (void) rMimeType;
378 #else
379 if( mxPlayer.is() )
380 return;
384 if( !rMediaURL.isEmpty() )
386 mxPlayer = avmedia::MediaWindow::createPlayer( rMediaURL, ""/*TODO!*/, &rMimeType );
389 catch( uno::RuntimeException& )
391 throw;
393 catch( const uno::Exception& )
395 throw lang::NoSupportException( "No video support for " + rMediaURL );
397 #endif
401 void ViewMediaShape::implInitializePlayerWindow( const ::basegfx::B2DRectangle& rBounds,
402 const uno::Sequence< uno::Any >& rVCLDeviceParams,
403 const OUString& )
405 SAL_INFO("slideshow", "ViewMediaShape::implInitializePlayerWindow" );
406 if( mpMediaWindow.get() || rBounds.isEmpty() )
407 return;
411 sal_Int64 aVal=0;
413 rVCLDeviceParams[ 1 ] >>= aVal;
415 vcl::Window* pWindow = reinterpret_cast< vcl::Window* >( aVal );
417 if( pWindow )
419 ::basegfx::B2DRange aTmpRange;
420 ::canvas::tools::calcTransformedRectBounds( aTmpRange,
421 rBounds,
422 mpViewLayer->getTransformation() );
423 const ::basegfx::B2IRange& rRangePix(
424 ::basegfx::unotools::b2ISurroundingRangeFromB2DRange( aTmpRange ));
426 if( !rRangePix.isEmpty() )
428 uno::Sequence< uno::Any > aArgs( 3 );
429 awt::Rectangle aAWTRect( rRangePix.getMinX(),
430 rRangePix.getMinY(),
431 rRangePix.getMaxX() - rRangePix.getMinX(),
432 rRangePix.getMaxY() - rRangePix.getMinY() );
434 mpMediaWindow.disposeAndClear();
435 mpMediaWindow = VclPtr<SystemChildWindow>::Create( pWindow, WB_CLIPCHILDREN );
436 UnoViewSharedPtr xUnoView(std::dynamic_pointer_cast<UnoView>(mpViewLayer));
437 if (xUnoView)
439 awt::Rectangle aCanvasArea = xUnoView->getUnoView()->getCanvasArea();
440 aAWTRect.X += aCanvasArea.X;
441 aAWTRect.Y += aCanvasArea.Y;
443 mpMediaWindow->SetPosSizePixel( Point( aAWTRect.X, aAWTRect.Y ),
444 Size( aAWTRect.Width, aAWTRect.Height ) );
446 mpMediaWindow->SetBackground( COL_BLACK );
447 mpMediaWindow->SetParentClipMode( ParentClipMode::NoClip );
448 mpMediaWindow->EnableEraseBackground( false );
449 mpMediaWindow->SetForwardKey( true );
450 mpMediaWindow->SetMouseTransparent( true );
451 mpMediaWindow->Show();
453 if( mxPlayer.is() )
455 aArgs[ 0 ] <<=
456 sal::static_int_cast< sal_IntPtr >( mpMediaWindow->GetParentWindowHandle() );
458 aAWTRect.X = aAWTRect.Y = 0;
459 aArgs[ 1 ] <<= aAWTRect;
460 aArgs[ 2 ] <<= reinterpret_cast< sal_IntPtr >( mpMediaWindow.get() );
462 mxPlayerWindow.set( mxPlayer->createPlayerWindow( aArgs ) );
464 if( mxPlayerWindow.is() )
466 mxPlayerWindow->setVisible( true );
467 mxPlayerWindow->setEnable( true );
471 if( !mxPlayerWindow.is() )
473 //if there was no playerwindow, then clear the mpMediaWindow too
474 //so that we can draw a placeholder instead in that space
475 mpMediaWindow.disposeAndClear();
480 catch( uno::RuntimeException& )
482 throw;
484 catch( uno::Exception& )
486 TOOLS_WARN_EXCEPTION( "slideshow", "" );
492 /* vim:set shiftwidth=4 softtabstop=4 expandtab: */