LibreOffice Module slideshow (master) 1
viewmediashape.cxx
Go to the documentation of this file.
1/* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */
2/*
3 * This file is part of the LibreOffice project.
4 *
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/.
8 *
9 * This file incorporates work covered by the following license notice:
10 *
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 .
18 */
19
20#include <config_features.h>
21
23
24#include <sal/log.hxx>
25#include <utility>
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>
31
38#include <cppcanvas/canvas.hxx>
40#include <svx/svdobj.hxx>
41#include <svx/svdomedia.hxx>
42
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>
51
52#include "viewmediashape.hxx"
53#include <tools.hxx>
54#include <unoview.hxx>
55
56using namespace ::com::sun::star;
57
58namespace slideshow::internal
59{
61 uno::Reference< drawing::XShape > xShape,
62 uno::Reference< uno::XComponentContext > xContext ) :
63 mpViewLayer( rViewLayer ),
64 maWindowOffset( 0, 0 ),
65 maBounds(),
66 mxShape(std::move( xShape )),
67 mxPlayer(),
68 mxPlayerWindow(),
69 mxComponentContext(std::move( xContext )),
71 {
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" );
76
77 UnoViewSharedPtr xUnoView(std::dynamic_pointer_cast<UnoView>(rViewLayer));
78 if (xUnoView)
79 {
80 mbIsSoundEnabled = xUnoView->isSoundEnabled();
81 }
82 }
83
84 ViewMediaShape::~ViewMediaShape()
85 {
86 try
87 {
88 endMedia();
89 }
90 catch (const uno::Exception &)
91 {
92 TOOLS_WARN_EXCEPTION("slideshow", "");
93 }
94 }
95
96 const ViewLayerSharedPtr& ViewMediaShape::getViewLayer() const
97 {
98 return mpViewLayer;
99 }
100
101 void ViewMediaShape::startMedia()
102 {
103 if( !mxPlayer.is() )
104 implInitialize( maBounds );
105
106 if (mxPlayer.is())
107 mxPlayer->start();
108 }
109
110 void ViewMediaShape::endMedia()
111 {
112 // shutdown player window
113 if( mxPlayerWindow.is() )
114 {
115 mxPlayerWindow->dispose();
116 mxPlayerWindow.clear();
117 }
118
119 mpMediaWindow.disposeAndClear();
120
121 // shutdown player
122 if( mxPlayer.is() )
123 {
124 mxPlayer->stop();
125
126 uno::Reference< lang::XComponent > xComponent( mxPlayer, uno::UNO_QUERY );
127
128 if( xComponent.is() )
129 xComponent->dispose();
130
131 mxPlayer.clear();
132 }
133 }
134
135 void ViewMediaShape::pauseMedia()
136 {
137 if (mxPlayer.is())
138 mxPlayer->stop();
139 }
140
141 void ViewMediaShape::setMediaTime(double fTime)
142 {
143 if (mxPlayer.is())
144 mxPlayer->setMediaTime(fTime);
145 }
146
147 void ViewMediaShape::setLooping(bool bLooping)
148 {
149 if (mxPlayer.is())
150 {
151 mxPlayer->setPlaybackLoop(bLooping);
152 }
153 }
154
155 bool ViewMediaShape::render( const ::basegfx::B2DRectangle& rBounds ) const
156 {
157#if !HAVE_FEATURE_AVMEDIA
158 (void) rBounds;
159#else
160 ::cppcanvas::CanvasSharedPtr pCanvas = mpViewLayer->getCanvas();
161
162 if( !pCanvas )
163 return false;
164
165 if( !mpMediaWindow && !mxPlayerWindow.is() )
166 {
168 uno::Reference< beans::XPropertySet > xPropSet( mxShape, uno::UNO_QUERY );
169 if (xPropSet.is())
170 {
171 xPropSet->getPropertyValue("FallbackGraphic") >>= xGraphic;
172 }
173
174 Graphic aGraphic(xGraphic);
175 const BitmapEx aBmp = aGraphic.GetBitmapEx();
176
178
179 rendering::ViewState aViewState;
180 aViewState.AffineTransform = pCanvas->getViewState().AffineTransform;
181
182 rendering::RenderState aRenderState;
183 ::canvas::tools::initRenderState( aRenderState );
184
185 const ::Size aBmpSize( aBmp.GetSizePixel() );
186
187 const ::basegfx::B2DVector aScale( rBounds.getWidth() / aBmpSize.Width(),
188 rBounds.getHeight() / aBmpSize.Height() );
190 aScale, rBounds.getMinimum()));
191 ::canvas::tools::setRenderStateTransform( aRenderState, aTranslation );
192
193 pCanvas->getUNOCanvas()->drawBitmap( xBitmap,
194 aViewState,
195 aRenderState );
196 }
197#endif
198 return true;
199 }
200
201 bool ViewMediaShape::resize( const ::basegfx::B2DRectangle& rNewBounds ) const
202 {
203 maBounds = rNewBounds;
204
205 ::cppcanvas::CanvasSharedPtr pCanvas = mpViewLayer->getCanvas();
206
207 if( !pCanvas )
208 return false;
209
210 if( !mxPlayerWindow.is() )
211 return true;
212
213 uno::Reference< beans::XPropertySet > xPropSet( pCanvas->getUNOCanvas()->getDevice(),
214 uno::UNO_QUERY );
215
216 uno::Reference< awt::XWindow > xParentWindow;
217 if( xPropSet.is() &&
218 getPropertyValue( xParentWindow,
219 xPropSet,
220 "Window") )
221 {
222 const awt::Rectangle aRect( xParentWindow->getPosSize() );
223
224 maWindowOffset.X = aRect.X;
225 maWindowOffset.Y = aRect.Y;
226 }
227
228 ::basegfx::B2DRange aTmpRange;
229 ::canvas::tools::calcTransformedRectBounds( aTmpRange,
230 rNewBounds,
231 mpViewLayer->getTransformation() );
232 const ::basegfx::B2IRange& rRangePix(
233 ::basegfx::unotools::b2ISurroundingRangeFromB2DRange( aTmpRange ));
234
235 mxPlayerWindow->setEnable( !rRangePix.isEmpty() );
236
237 if( rRangePix.isEmpty() )
238 return true;
239
240 awt::Rectangle aCanvasArea;
241 UnoViewSharedPtr xUnoView(std::dynamic_pointer_cast<UnoView>(mpViewLayer));
242 if (xUnoView)
243 aCanvasArea = xUnoView->getUnoView()->getCanvasArea();
244
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() );
249
250 if( mpMediaWindow )
251 {
252 mpMediaWindow->SetPosSizePixel( aPosPixel, aSizePixel );
253 mxPlayerWindow->setPosSize( 0, 0,
254 aSizePixel.Width(), aSizePixel.Height(),
255 0 );
256 }
257 else
258 {
259 mxPlayerWindow->setPosSize( aPosPixel.X(), aPosPixel.Y(),
260 aSizePixel.Width(), aSizePixel.Height(),
261 0 );
262 }
263
264 return true;
265 }
266
267
268 bool ViewMediaShape::implInitialize( const ::basegfx::B2DRectangle& rBounds )
269 {
270 if( !mxPlayer.is() && mxShape.is() )
271 {
272 ENSURE_OR_RETURN_FALSE( mpViewLayer->getCanvas(),
273 "ViewMediaShape::implInitialize(): Invalid layer canvas" );
274
275 uno::Reference< rendering::XCanvas > xCanvas( mpViewLayer->getCanvas()->getUNOCanvas() );
276
277 if( xCanvas.is() )
278 {
280 try
281 {
282 xPropSet.set( mxShape, uno::UNO_QUERY );
283 OUString sMimeType;
284
285 // create Player
286 if (xPropSet.is())
287 {
288 OUString aURL;
289 xPropSet->getPropertyValue("MediaMimeType") >>= sMimeType;
290 if ((xPropSet->getPropertyValue("PrivateTempFileURL") >>= aURL)
291 && !aURL.isEmpty())
292 {
293 implInitializeMediaPlayer( aURL, sMimeType );
294 }
295 else if (xPropSet->getPropertyValue("MediaURL") >>= aURL)
296 {
297 implInitializeMediaPlayer( aURL, sMimeType );
298 }
299 }
300
301 // create visible object
302 uno::Sequence< uno::Any > aDeviceParams;
303
304 if( ::canvas::tools::getDeviceInfo( xCanvas, aDeviceParams ).getLength() > 1 )
305 {
306 implInitializePlayerWindow( rBounds, aDeviceParams );
307 }
308
309 // set player properties
310 implSetMediaProperties( xPropSet );
311 }
312 catch( uno::RuntimeException& )
313 {
314 throw;
315 }
316 catch( uno::Exception& )
317 {
318 TOOLS_WARN_EXCEPTION( "slideshow", "" );
319 }
320 }
321 }
322
323 return mxPlayer.is() || mxPlayerWindow.is();
324 }
325
326
327 void ViewMediaShape::implSetMediaProperties( const uno::Reference< beans::XPropertySet >& rxProps )
328 {
329 if( !mxPlayer.is() )
330 return;
331
332 mxPlayer->setMediaTime( 0.0 );
333
334 if( !rxProps.is() )
335 return;
336
337 bool bLoop( false );
338 getPropertyValue( bLoop,
339 rxProps,
340 "Loop");
341 mxPlayer->setPlaybackLoop( bLoop );
342
343 bool bMute( false );
344 getPropertyValue( bMute,
345 rxProps,
346 "Mute");
347 mxPlayer->setMute( bMute || !mbIsSoundEnabled);
348
349 sal_Int16 nVolumeDB(0);
350 getPropertyValue( nVolumeDB,
351 rxProps,
352 "VolumeDB");
353 mxPlayer->setVolumeDB( nVolumeDB );
354
355 if( mxPlayerWindow.is() )
356 {
357 media::ZoomLevel eZoom(media::ZoomLevel_FIT_TO_WINDOW);
358 getPropertyValue( eZoom,
359 rxProps,
360 "Zoom");
361 mxPlayerWindow->setZoomLevel( eZoom );
362 }
363 }
364
365
366 void ViewMediaShape::implInitializeMediaPlayer( const OUString& rMediaURL, const OUString& rMimeType )
367 {
368#if !HAVE_FEATURE_AVMEDIA
369 (void) rMediaURL;
370 (void) rMimeType;
371#else
372 if( mxPlayer.is() )
373 return;
374
375 try
376 {
377 if( !rMediaURL.isEmpty() )
378 {
379 mxPlayer = avmedia::MediaWindow::createPlayer( rMediaURL, ""/*TODO!*/, &rMimeType );
380 }
381 }
382 catch( uno::RuntimeException& )
383 {
384 throw;
385 }
386 catch( const uno::Exception& )
387 {
388 throw lang::NoSupportException( "No video support for " + rMediaURL );
389 }
390#endif
391 }
392
393
394 void ViewMediaShape::implInitializePlayerWindow( const ::basegfx::B2DRectangle& rBounds,
395 const uno::Sequence< uno::Any >& rVCLDeviceParams )
396 {
397 SAL_INFO("slideshow", "ViewMediaShape::implInitializePlayerWindow" );
398 if( mpMediaWindow || rBounds.isEmpty() )
399 return;
400
401 try
402 {
403 sal_Int64 aVal=0;
404
405 rVCLDeviceParams[ 1 ] >>= aVal;
406
407 OutputDevice* pDevice = reinterpret_cast<OutputDevice*>(aVal);
408 vcl::Window* pWindow = pDevice ? pDevice->GetOwnerWindow() : nullptr;
409
410 if( pWindow )
411 {
412 ::basegfx::B2DRange aTmpRange;
413 ::canvas::tools::calcTransformedRectBounds( aTmpRange,
414 rBounds,
415 mpViewLayer->getTransformation() );
416 const ::basegfx::B2IRange& rRangePix(
417 ::basegfx::unotools::b2ISurroundingRangeFromB2DRange( aTmpRange ));
418
419 if( !rRangePix.isEmpty() )
420 {
421 awt::Rectangle aAWTRect( rRangePix.getMinX(),
422 rRangePix.getMinY(),
423 rRangePix.getMaxX() - rRangePix.getMinX(),
424 rRangePix.getMaxY() - rRangePix.getMinY() );
425 {
426 mpMediaWindow.disposeAndClear();
427 mpMediaWindow = VclPtr<SystemChildWindow>::Create( pWindow, WB_CLIPCHILDREN );
428 UnoViewSharedPtr xUnoView(std::dynamic_pointer_cast<UnoView>(mpViewLayer));
429 if (xUnoView)
430 {
431 awt::Rectangle aCanvasArea = xUnoView->getUnoView()->getCanvasArea();
432 aAWTRect.X += aCanvasArea.X;
433 aAWTRect.Y += aCanvasArea.Y;
434 }
435 mpMediaWindow->SetPosSizePixel( Point( aAWTRect.X, aAWTRect.Y ),
436 Size( aAWTRect.Width, aAWTRect.Height ) );
437 }
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();
444
445 if( mxPlayer.is() )
446 {
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();
452
453 aAWTRect.X = aAWTRect.Y = 0;
454
456 auto pMediaObj = dynamic_cast<SdrMediaObj*>(pObj);
457 const avmedia::MediaItem* pMediaItem = nullptr;
458 if (pMediaObj)
459 {
460 pMediaItem = &pMediaObj->getMediaProperties();
461 }
462
464 uno::Any(nParentWindowHandle),
465 uno::Any(aAWTRect),
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 ))
469 };
470
471 mxPlayerWindow.set( mxPlayer->createPlayerWindow( aArgs ) );
472
473 if( mxPlayerWindow.is() )
474 {
475 mxPlayerWindow->setVisible( true );
476 mxPlayerWindow->setEnable( true );
477 }
478 }
479
480 if( !mxPlayerWindow.is() )
481 {
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();
485 }
486 }
487 }
488 }
489 catch( uno::RuntimeException& )
490 {
491 throw;
492 }
493 catch( uno::Exception& )
494 {
495 TOOLS_WARN_EXCEPTION( "slideshow", "" );
496 }
497 }
498}
499
500/* vim:set shiftwidth=4 softtabstop=4 expandtab: */
css::uno::Reference< css::uno::XComponentContext > mxComponentContext
::basegfx::B2DRectangle maBounds
const Size & GetSizePixel() const
BitmapEx GetBitmapEx(const GraphicConversionParameters &rParameters=GraphicConversionParameters()) const
virtual vcl::Window * GetOwnerWindow() const
constexpr tools::Long Y() const
constexpr tools::Long X() const
static SdrObject * getSdrObjectFromXShape(const css::uno::Reference< css::uno::XInterface > &xInt)
constexpr tools::Long Height() const
constexpr tools::Long Width() const
static VclPtr< reference_type > Create(Arg &&... arg)
static css::uno::Reference< css::media::XPlayer > createPlayer(const OUString &rURL, const OUString &rReferer, const OUString *pMimeType=nullptr)
ViewMediaShape(const ViewLayerSharedPtr &rViewLayer, css::uno::Reference< css::drawing::XShape > xShape, css::uno::Reference< css::uno::XComponentContext > xContext)
Create a ViewMediaShape for the given View.
constexpr ::Color COL_BLACK(0x00, 0x00, 0x00)
#define TOOLS_WARN_EXCEPTION(area, stream)
#define ENSURE_OR_RETURN_FALSE(c, m)
#define ENSURE_OR_THROW(c, m)
URL aURL
#define SAL_INFO(area, stream)
double getLength(const B2DPolygon &rCandidate)
B2DHomMatrix createScaleTranslateB2DHomMatrix(double fScaleX, double fScaleY, double fTranslateX, double fTranslateY)
std::shared_ptr< Canvas > CanvasSharedPtr
bool getPropertyValue(ValueType &rValue, css::uno::Reference< css::beans::XPropertySet > const &xPropSet, OUString const &propName)
Definition: tools.hxx:278
std::shared_ptr< ViewLayer > ViewLayerSharedPtr
Definition: viewlayer.hxx:168
std::shared_ptr< UnoView > UnoViewSharedPtr
uno::Reference< rendering::XBitmap > xBitmapFromBitmapEx(const ::BitmapEx &inputBitmap)
uno::Reference< drawing::XShape > const mxShape
bool mbIsSoundEnabled
Definition: slideview.cxx:738
Toolkit toolkit
WinBits const WB_CLIPCHILDREN