1 /* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */
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>
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>
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"
67 #include <unoview.hxx>
69 using namespace ::com::sun::star
;
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 ),
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
));
95 mbIsSoundEnabled
= xUnoView
->isSoundEnabled();
99 ViewMediaShape::~ViewMediaShape()
105 catch (const uno::Exception
&)
107 TOOLS_WARN_EXCEPTION("slideshow", "");
111 const ViewLayerSharedPtr
& ViewMediaShape::getViewLayer() const
116 void ViewMediaShape::startMedia()
119 implInitialize( maBounds
);
125 void ViewMediaShape::endMedia()
127 // shutdown player window
128 if( mxPlayerWindow
.is() )
130 mxPlayerWindow
->dispose();
131 mxPlayerWindow
.clear();
134 mpMediaWindow
.disposeAndClear();
141 uno::Reference
< lang::XComponent
> xComponent( mxPlayer
, uno::UNO_QUERY
);
143 if( xComponent
.is() )
144 xComponent
->dispose();
150 void ViewMediaShape::pauseMedia()
156 void ViewMediaShape::setMediaTime(double fTime
)
159 mxPlayer
->setMediaTime(fTime
);
162 bool ViewMediaShape::render( const ::basegfx::B2DRectangle
& rBounds
) const
164 #if !HAVE_FEATURE_AVMEDIA
167 ::cppcanvas::CanvasSharedPtr pCanvas
= mpViewLayer
->getCanvas();
172 if( !mpMediaWindow
.get() && !mxPlayerWindow
.is() )
174 uno::Reference
< graphic::XGraphic
> xGraphic
;
175 uno::Reference
< beans::XPropertySet
> xPropSet( mxShape
, uno::UNO_QUERY
);
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
,
208 bool ViewMediaShape::resize( const ::basegfx::B2DRectangle
& rNewBounds
) const
210 maBounds
= rNewBounds
;
212 ::cppcanvas::CanvasSharedPtr pCanvas
= mpViewLayer
->getCanvas();
217 if( !mxPlayerWindow
.is() )
220 uno::Reference
< beans::XPropertySet
> xPropSet( pCanvas
->getUNOCanvas()->getDevice(),
223 uno::Reference
< awt::XWindow
> xParentWindow
;
225 getPropertyValue( xParentWindow
,
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
,
238 mpViewLayer
->getTransformation() );
239 const ::basegfx::B2IRange
& rRangePix(
240 ::basegfx::unotools::b2ISurroundingRangeFromB2DRange( aTmpRange
));
242 mxPlayerWindow
->setEnable( !rRangePix
.isEmpty() );
244 if( rRangePix
.isEmpty() )
247 awt::Rectangle aCanvasArea
;
248 UnoViewSharedPtr
xUnoView(std::dynamic_pointer_cast
<UnoView
>(mpViewLayer
));
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(),
266 mxPlayerWindow
->setPosSize( aPosPixel
.X(), aPosPixel
.Y(),
267 aSizePixel
.Width(), aSizePixel
.Height(),
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() );
286 uno::Reference
< beans::XPropertySet
> xPropSet
;
289 xPropSet
.set( mxShape
, uno::UNO_QUERY
);
296 xPropSet
->getPropertyValue("MediaMimeType") >>= sMimeType
;
297 if ((xPropSet
->getPropertyValue("PrivateTempFileURL") >>= aURL
)
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
& )
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
)
339 mxPlayer
->setMediaTime( 0.0 );
345 getPropertyValue( bLoop
,
348 mxPlayer
->setPlaybackLoop( bLoop
);
351 getPropertyValue( bMute
,
354 mxPlayer
->setMute( bMute
|| !mbIsSoundEnabled
);
356 sal_Int16
nVolumeDB(0);
357 getPropertyValue( nVolumeDB
,
360 mxPlayer
->setVolumeDB( nVolumeDB
);
362 if( mxPlayerWindow
.is() )
364 media::ZoomLevel
eZoom(media::ZoomLevel_FIT_TO_WINDOW
);
365 getPropertyValue( eZoom
,
368 mxPlayerWindow
->setZoomLevel( eZoom
);
373 void ViewMediaShape::implInitializeMediaPlayer( const OUString
& rMediaURL
, const OUString
& rMimeType
)
375 #if !HAVE_FEATURE_AVMEDIA
384 if( !rMediaURL
.isEmpty() )
386 mxPlayer
= avmedia::MediaWindow::createPlayer( rMediaURL
, ""/*TODO!*/, &rMimeType
);
389 catch( uno::RuntimeException
& )
393 catch( const uno::Exception
& )
395 throw lang::NoSupportException( "No video support for " + rMediaURL
);
401 void ViewMediaShape::implInitializePlayerWindow( const ::basegfx::B2DRectangle
& rBounds
,
402 const uno::Sequence
< uno::Any
>& rVCLDeviceParams
,
405 SAL_INFO("slideshow", "ViewMediaShape::implInitializePlayerWindow" );
406 if( mpMediaWindow
.get() || rBounds
.isEmpty() )
413 rVCLDeviceParams
[ 1 ] >>= aVal
;
415 vcl::Window
* pWindow
= reinterpret_cast< vcl::Window
* >( aVal
);
419 ::basegfx::B2DRange aTmpRange
;
420 ::canvas::tools::calcTransformedRectBounds( aTmpRange
,
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(),
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
));
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();
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
& )
484 catch( uno::Exception
& )
486 TOOLS_WARN_EXCEPTION( "slideshow", "" );
492 /* vim:set shiftwidth=4 softtabstop=4 expandtab: */