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 <comphelper/diagnose_ex.hxx>
24 #include <sal/log.hxx>
26 #include <vcl/canvastools.hxx>
27 #include <vcl/syschild.hxx>
28 #include <vcl/sysdata.hxx>
29 #include <vcl/window.hxx>
30 #include <vcl/graph.hxx>
32 #include <basegfx/utils/canvastools.hxx>
33 #include <basegfx/matrix/b2dhommatrixtools.hxx>
34 #include <basegfx/point/b2dpoint.hxx>
35 #include <basegfx/matrix/b2dhommatrix.hxx>
36 #include <basegfx/range/b2irange.hxx>
37 #include <canvas/canvastools.hxx>
38 #include <cppcanvas/canvas.hxx>
39 #include <avmedia/mediawindow.hxx>
40 #include <svx/svdobj.hxx>
41 #include <svx/svdomedia.hxx>
43 #include <com/sun/star/awt/XWindow.hpp>
44 #include <com/sun/star/beans/XPropertySet.hpp>
45 #include <com/sun/star/lang/XComponent.hpp>
46 #include <com/sun/star/lang/NoSupportException.hpp>
47 #include <com/sun/star/media/XPlayer.hpp>
48 #include <com/sun/star/media/XPlayerWindow.hpp>
49 #include <com/sun/star/presentation/XSlideShowView.hpp>
50 #include <com/sun/star/rendering/XCanvas.hpp>
52 #include "viewmediashape.hxx"
54 #include <unoview.hxx>
56 using namespace ::com::sun::star
;
58 namespace slideshow::internal
60 ViewMediaShape::ViewMediaShape( const ViewLayerSharedPtr
& rViewLayer
,
61 uno::Reference
< drawing::XShape
> xShape
,
62 uno::Reference
< uno::XComponentContext
> xContext
) :
63 mpViewLayer( rViewLayer
),
64 maWindowOffset( 0, 0 ),
66 mxShape(std::move( xShape
)),
69 mxComponentContext(std::move( xContext
)),
70 mbIsSoundEnabled(true)
72 ENSURE_OR_THROW( mxShape
.is(), "ViewMediaShape::ViewMediaShape(): Invalid Shape" );
73 ENSURE_OR_THROW( mpViewLayer
, "ViewMediaShape::ViewMediaShape(): Invalid View" );
74 ENSURE_OR_THROW( mpViewLayer
->getCanvas(), "ViewMediaShape::ViewMediaShape(): Invalid ViewLayer canvas" );
75 ENSURE_OR_THROW( mxComponentContext
.is(), "ViewMediaShape::ViewMediaShape(): Invalid component context" );
77 UnoViewSharedPtr
xUnoView(std::dynamic_pointer_cast
<UnoView
>(rViewLayer
));
80 mbIsSoundEnabled
= xUnoView
->isSoundEnabled();
84 ViewMediaShape::~ViewMediaShape()
90 catch (const uno::Exception
&)
92 TOOLS_WARN_EXCEPTION("slideshow", "");
96 const ViewLayerSharedPtr
& ViewMediaShape::getViewLayer() const
101 void ViewMediaShape::startMedia()
104 implInitialize( maBounds
);
110 void ViewMediaShape::endMedia()
112 // shutdown player window
113 if( mxPlayerWindow
.is() )
115 mxPlayerWindow
->dispose();
116 mxPlayerWindow
.clear();
119 mpMediaWindow
.disposeAndClear();
126 uno::Reference
< lang::XComponent
> xComponent( mxPlayer
, uno::UNO_QUERY
);
128 if( xComponent
.is() )
129 xComponent
->dispose();
135 void ViewMediaShape::pauseMedia()
141 void ViewMediaShape::setMediaTime(double fTime
)
144 mxPlayer
->setMediaTime(fTime
);
147 void ViewMediaShape::setLooping(bool bLooping
)
151 mxPlayer
->setPlaybackLoop(bLooping
);
155 bool ViewMediaShape::render( const ::basegfx::B2DRectangle
& rBounds
) const
157 #if !HAVE_FEATURE_AVMEDIA
160 ::cppcanvas::CanvasSharedPtr pCanvas
= mpViewLayer
->getCanvas();
165 if( !mpMediaWindow
&& !mxPlayerWindow
.is() )
167 uno::Reference
< graphic::XGraphic
> xGraphic
;
168 uno::Reference
< beans::XPropertySet
> xPropSet( mxShape
, uno::UNO_QUERY
);
171 xPropSet
->getPropertyValue("FallbackGraphic") >>= xGraphic
;
174 Graphic
aGraphic(xGraphic
);
175 const BitmapEx aBmp
= aGraphic
.GetBitmapEx();
177 uno::Reference
< rendering::XBitmap
> xBitmap(vcl::unotools::xBitmapFromBitmapEx(aBmp
));
179 rendering::ViewState aViewState
;
180 aViewState
.AffineTransform
= pCanvas
->getViewState().AffineTransform
;
182 rendering::RenderState aRenderState
;
183 ::canvas::tools::initRenderState( aRenderState
);
185 const ::Size
aBmpSize( aBmp
.GetSizePixel() );
187 const ::basegfx::B2DVector
aScale( rBounds
.getWidth() / aBmpSize
.Width(),
188 rBounds
.getHeight() / aBmpSize
.Height() );
189 const basegfx::B2DHomMatrix
aTranslation(basegfx::utils::createScaleTranslateB2DHomMatrix(
190 aScale
, rBounds
.getMinimum()));
191 ::canvas::tools::setRenderStateTransform( aRenderState
, aTranslation
);
193 pCanvas
->getUNOCanvas()->drawBitmap( xBitmap
,
201 bool ViewMediaShape::resize( const ::basegfx::B2DRectangle
& rNewBounds
) const
203 maBounds
= rNewBounds
;
205 ::cppcanvas::CanvasSharedPtr pCanvas
= mpViewLayer
->getCanvas();
210 if( !mxPlayerWindow
.is() )
213 uno::Reference
< beans::XPropertySet
> xPropSet( pCanvas
->getUNOCanvas()->getDevice(),
216 uno::Reference
< awt::XWindow
> xParentWindow
;
218 getPropertyValue( xParentWindow
,
222 const awt::Rectangle
aRect( xParentWindow
->getPosSize() );
224 maWindowOffset
.X
= aRect
.X
;
225 maWindowOffset
.Y
= aRect
.Y
;
228 ::basegfx::B2DRange aTmpRange
;
229 ::canvas::tools::calcTransformedRectBounds( aTmpRange
,
231 mpViewLayer
->getTransformation() );
232 const ::basegfx::B2IRange
& rRangePix(
233 ::basegfx::unotools::b2ISurroundingRangeFromB2DRange( aTmpRange
));
235 mxPlayerWindow
->setEnable( !rRangePix
.isEmpty() );
237 if( rRangePix
.isEmpty() )
240 awt::Rectangle aCanvasArea
;
241 UnoViewSharedPtr
xUnoView(std::dynamic_pointer_cast
<UnoView
>(mpViewLayer
));
243 aCanvasArea
= xUnoView
->getUnoView()->getCanvasArea();
245 const Point
aPosPixel( rRangePix
.getMinX() + maWindowOffset
.X
+ aCanvasArea
.X
,
246 rRangePix
.getMinY() + maWindowOffset
.Y
+ aCanvasArea
.Y
);
247 const Size
aSizePixel( rRangePix
.getMaxX() - rRangePix
.getMinX(),
248 rRangePix
.getMaxY() - rRangePix
.getMinY() );
252 mpMediaWindow
->SetPosSizePixel( aPosPixel
, aSizePixel
);
253 mxPlayerWindow
->setPosSize( 0, 0,
254 aSizePixel
.Width(), aSizePixel
.Height(),
259 mxPlayerWindow
->setPosSize( aPosPixel
.X(), aPosPixel
.Y(),
260 aSizePixel
.Width(), aSizePixel
.Height(),
268 bool ViewMediaShape::implInitialize( const ::basegfx::B2DRectangle
& rBounds
)
270 if( !mxPlayer
.is() && mxShape
.is() )
272 ENSURE_OR_RETURN_FALSE( mpViewLayer
->getCanvas(),
273 "ViewMediaShape::implInitialize(): Invalid layer canvas" );
275 uno::Reference
< rendering::XCanvas
> xCanvas( mpViewLayer
->getCanvas()->getUNOCanvas() );
279 uno::Reference
< beans::XPropertySet
> xPropSet
;
282 xPropSet
.set( mxShape
, uno::UNO_QUERY
);
289 xPropSet
->getPropertyValue("MediaMimeType") >>= sMimeType
;
290 if ((xPropSet
->getPropertyValue("PrivateTempFileURL") >>= aURL
)
293 implInitializeMediaPlayer( aURL
, sMimeType
);
295 else if (xPropSet
->getPropertyValue("MediaURL") >>= aURL
)
297 implInitializeMediaPlayer( aURL
, sMimeType
);
301 // create visible object
302 uno::Sequence
< uno::Any
> aDeviceParams
;
304 if( ::canvas::tools::getDeviceInfo( xCanvas
, aDeviceParams
).getLength() > 1 )
306 implInitializePlayerWindow( rBounds
, aDeviceParams
);
309 // set player properties
310 implSetMediaProperties( xPropSet
);
312 catch( uno::RuntimeException
& )
316 catch( uno::Exception
& )
318 TOOLS_WARN_EXCEPTION( "slideshow", "" );
323 return mxPlayer
.is() || mxPlayerWindow
.is();
327 void ViewMediaShape::implSetMediaProperties( const uno::Reference
< beans::XPropertySet
>& rxProps
)
332 mxPlayer
->setMediaTime( 0.0 );
338 getPropertyValue( bLoop
,
341 mxPlayer
->setPlaybackLoop( bLoop
);
344 getPropertyValue( bMute
,
347 mxPlayer
->setMute( bMute
|| !mbIsSoundEnabled
);
349 sal_Int16
nVolumeDB(0);
350 getPropertyValue( nVolumeDB
,
353 mxPlayer
->setVolumeDB( nVolumeDB
);
355 if( mxPlayerWindow
.is() )
357 media::ZoomLevel
eZoom(media::ZoomLevel_FIT_TO_WINDOW
);
358 getPropertyValue( eZoom
,
361 mxPlayerWindow
->setZoomLevel( eZoom
);
366 void ViewMediaShape::implInitializeMediaPlayer( const OUString
& rMediaURL
, const OUString
& rMimeType
)
368 #if !HAVE_FEATURE_AVMEDIA
377 if( !rMediaURL
.isEmpty() )
379 mxPlayer
= avmedia::MediaWindow::createPlayer( rMediaURL
, ""/*TODO!*/, &rMimeType
);
382 catch( uno::RuntimeException
& )
386 catch( const uno::Exception
& )
388 throw lang::NoSupportException( "No video support for " + rMediaURL
);
394 void ViewMediaShape::implInitializePlayerWindow( const ::basegfx::B2DRectangle
& rBounds
,
395 const uno::Sequence
< uno::Any
>& rVCLDeviceParams
)
397 SAL_INFO("slideshow", "ViewMediaShape::implInitializePlayerWindow" );
398 if( mpMediaWindow
|| rBounds
.isEmpty() )
405 rVCLDeviceParams
[ 1 ] >>= aVal
;
407 OutputDevice
* pDevice
= reinterpret_cast<OutputDevice
*>(aVal
);
408 vcl::Window
* pWindow
= pDevice
? pDevice
->GetOwnerWindow() : nullptr;
412 ::basegfx::B2DRange aTmpRange
;
413 ::canvas::tools::calcTransformedRectBounds( aTmpRange
,
415 mpViewLayer
->getTransformation() );
416 const ::basegfx::B2IRange
& rRangePix(
417 ::basegfx::unotools::b2ISurroundingRangeFromB2DRange( aTmpRange
));
419 if( !rRangePix
.isEmpty() )
421 awt::Rectangle
aAWTRect( rRangePix
.getMinX(),
423 rRangePix
.getMaxX() - rRangePix
.getMinX(),
424 rRangePix
.getMaxY() - rRangePix
.getMinY() );
426 mpMediaWindow
.disposeAndClear();
427 mpMediaWindow
= VclPtr
<SystemChildWindow
>::Create( pWindow
, WB_CLIPCHILDREN
);
428 UnoViewSharedPtr
xUnoView(std::dynamic_pointer_cast
<UnoView
>(mpViewLayer
));
431 awt::Rectangle aCanvasArea
= xUnoView
->getUnoView()->getCanvasArea();
432 aAWTRect
.X
+= aCanvasArea
.X
;
433 aAWTRect
.Y
+= aCanvasArea
.Y
;
435 mpMediaWindow
->SetPosSizePixel( Point( aAWTRect
.X
, aAWTRect
.Y
),
436 Size( aAWTRect
.Width
, aAWTRect
.Height
) );
438 mpMediaWindow
->SetBackground( COL_BLACK
);
439 mpMediaWindow
->SetParentClipMode( ParentClipMode::NoClip
);
440 mpMediaWindow
->EnableEraseBackground( false );
441 mpMediaWindow
->SetForwardKey( true );
442 mpMediaWindow
->SetMouseTransparent( true );
443 mpMediaWindow
->Show();
447 sal_IntPtr
nParentWindowHandle(0);
448 const SystemEnvData
* pEnvData
= mpMediaWindow
->GetSystemData();
449 // tdf#139609 gtk doesn't need the handle, and fetching it is undesirable
450 if (!pEnvData
|| pEnvData
->toolkit
!= SystemEnvData::Toolkit::Gtk
)
451 nParentWindowHandle
= mpMediaWindow
->GetParentWindowHandle();
453 aAWTRect
.X
= aAWTRect
.Y
= 0;
455 SdrObject
* pObj
= SdrObject::getSdrObjectFromXShape(mxShape
);
456 auto pMediaObj
= dynamic_cast<SdrMediaObj
*>(pObj
);
457 const avmedia::MediaItem
* pMediaItem
= nullptr;
460 pMediaItem
= &pMediaObj
->getMediaProperties();
463 uno::Sequence
< uno::Any
> aArgs
{
464 uno::Any(nParentWindowHandle
),
466 uno::Any(reinterpret_cast< sal_IntPtr
>( mpMediaWindow
.get() )),
467 // Media item contains media properties, e.g. cropping.
468 uno::Any(reinterpret_cast< sal_IntPtr
>( pMediaItem
))
471 mxPlayerWindow
.set( mxPlayer
->createPlayerWindow( aArgs
) );
473 if( mxPlayerWindow
.is() )
475 mxPlayerWindow
->setVisible( true );
476 mxPlayerWindow
->setEnable( true );
480 if( !mxPlayerWindow
.is() )
482 //if there was no playerwindow, then clear the mpMediaWindow too
483 //so that we can draw a placeholder instead in that space
484 mpMediaWindow
.disposeAndClear();
489 catch( uno::RuntimeException
& )
493 catch( uno::Exception
& )
495 TOOLS_WARN_EXCEPTION( "slideshow", "" );
500 /* vim:set shiftwidth=4 softtabstop=4 expandtab: */