1 /*************************************************************************
3 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
5 * Copyright 2008 by Sun Microsystems, Inc.
7 * OpenOffice.org - a multi-platform office productivity suite
9 * $RCSfile: viewmediashape.cxx,v $
10 * $Revision: 1.6.2.1 $
12 * This file is part of OpenOffice.org.
14 * OpenOffice.org is free software: you can redistribute it and/or modify
15 * it under the terms of the GNU Lesser General Public License version 3
16 * only, as published by the Free Software Foundation.
18 * OpenOffice.org is distributed in the hope that it will be useful,
19 * but WITHOUT ANY WARRANTY; without even the implied warranty of
20 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
21 * GNU Lesser General Public License version 3 for more details
22 * (a copy is included in the LICENSE file that accompanied this code).
24 * You should have received a copy of the GNU Lesser General Public License
25 * version 3 along with OpenOffice.org. If not, see
26 * <http://www.openoffice.org/license.html>
27 * for a copy of the LGPLv3 License.
29 ************************************************************************/
31 // MARKER(update_precomp.py): autogen include statement, do not remove
32 #include "precompiled_slideshow.hxx"
35 #include <canvas/debug.hxx>
36 #include <tools/diagnose_ex.h>
37 #include <canvas/verbosetrace.hxx>
41 #include <comphelper/anytostring.hxx>
42 #include <cppuhelper/exc_hlp.hxx>
44 #include <vcl/window.hxx>
45 #include <vcl/javachild.hxx>
46 #include <vcl/salbtype.hxx>
48 #include <vcl/sysdata.hxx>
51 #include <basegfx/tools/canvastools.hxx>
52 #include <basegfx/numeric/ftools.hxx>
53 #include <basegfx/polygon/b2dpolygon.hxx>
54 #include <basegfx/point/b2dpoint.hxx>
55 #include <basegfx/matrix/b2dhommatrix.hxx>
56 #include <basegfx/polygon/b2dpolygontools.hxx>
57 #include <basegfx/range/b2irange.hxx>
58 #include <canvas/canvastools.hxx>
59 #include <cppcanvas/vclfactory.hxx>
60 #include <cppcanvas/basegfxfactory.hxx>
61 #include <cppcanvas/basegfxfactory.hxx>
62 #include <avmedia/mediawindow.hxx>
64 #include <com/sun/star/media/XManager.hpp>
65 #include <com/sun/star/media/XPlayer.hpp>
66 #include <com/sun/star/media/XPlayerWindow.hpp>
67 #include <com/sun/star/beans/XPropertySet.hpp>
68 #include <com/sun/star/lang/XMultiComponentFactory.hpp>
69 #include <com/sun/star/lang/NoSupportException.hpp>
70 #include <com/sun/star/awt/XWindow.hpp>
71 #include <com/sun/star/rendering/XCanvas.hpp>
72 #include <com/sun/star/lang/XComponent.hdl>
74 #include "viewmediashape.hxx"
75 #include "mediashape.hxx"
77 #include "unoview.hxx"
79 using namespace ::com::sun::star
;
85 ViewMediaShape::ViewMediaShape( const ViewLayerSharedPtr
& rViewLayer
,
86 const uno::Reference
< drawing::XShape
>& rxShape
,
87 const uno::Reference
< uno::XComponentContext
>& rxContext
) :
88 mpViewLayer( rViewLayer
),
90 maWindowOffset( 0, 0 ),
95 mxComponentContext( rxContext
),
96 mbIsSoundEnabled(true)
98 ENSURE_OR_THROW( mxShape
.is(), "ViewMediaShape::ViewMediaShape(): Invalid Shape" );
99 ENSURE_OR_THROW( mpViewLayer
, "ViewMediaShape::ViewMediaShape(): Invalid View" );
100 ENSURE_OR_THROW( mpViewLayer
->getCanvas(), "ViewMediaShape::ViewMediaShape(): Invalid ViewLayer canvas" );
101 ENSURE_OR_THROW( mxComponentContext
.is(), "ViewMediaShape::ViewMediaShape(): Invalid component context" );
103 UnoViewSharedPtr
pUnoView (::boost::dynamic_pointer_cast
<UnoView
>(rViewLayer
));
106 mbIsSoundEnabled
= pUnoView
->isSoundEnabled();
110 // ---------------------------------------------------------------------
112 ViewMediaShape::~ViewMediaShape()
118 catch (uno::Exception
&)
120 OSL_ENSURE( false, rtl::OUStringToOString(
121 comphelper::anyToString(
122 cppu::getCaughtException() ),
123 RTL_TEXTENCODING_UTF8
).getStr() );
127 // ---------------------------------------------------------------------
129 ViewLayerSharedPtr
ViewMediaShape::getViewLayer() const
134 // ---------------------------------------------------------------------
136 bool ViewMediaShape::startMedia()
139 implInitialize( maBounds
);
141 if( mxPlayer
.is() && ( mxPlayer
->getDuration() > 0.0 ) )
147 // ---------------------------------------------------------------------
149 void ViewMediaShape::endMedia()
151 // shutdown player window
152 if( mxPlayerWindow
.is() )
154 uno::Reference
< lang::XComponent
> xComponent( mxPlayerWindow
, uno::UNO_QUERY
);
156 if( xComponent
.is() )
157 xComponent
->dispose();
159 mxPlayerWindow
.clear();
163 mpMediaWindow
= ::std::auto_ptr
< SystemChildWindow
>();
165 mpMediaWindow
= ::std::auto_ptr
< JavaChildWindow
>();
173 uno::Reference
< lang::XComponent
> xComponent( mxPlayer
, uno::UNO_QUERY
);
175 if( xComponent
.is() )
176 xComponent
->dispose();
182 // ---------------------------------------------------------------------
184 void ViewMediaShape::pauseMedia()
186 if( mxPlayer
.is() && ( mxPlayer
->getDuration() > 0.0 ) )
190 // ---------------------------------------------------------------------
192 void ViewMediaShape::setMediaTime(double fTime
)
194 if( mxPlayer
.is() && ( mxPlayer
->getDuration() > 0.0 ) )
195 mxPlayer
->setMediaTime(fTime
);
198 // ---------------------------------------------------------------------
200 bool ViewMediaShape::render( const ::basegfx::B2DRectangle
& rBounds
) const
202 ::cppcanvas::CanvasSharedPtr pCanvas
= mpViewLayer
->getCanvas();;
207 if( !mpMediaWindow
.get() && !mxPlayerWindow
.is() )
209 // fill the shape background with black
218 bool ViewMediaShape::resize( const ::basegfx::B2DRectangle
& rNewBounds
) const
220 maBounds
= rNewBounds
;
222 ::cppcanvas::CanvasSharedPtr pCanvas
= mpViewLayer
->getCanvas();;
227 if( !mxPlayerWindow
.is() )
230 uno::Reference
< beans::XPropertySet
> xPropSet( pCanvas
->getUNOCanvas()->getDevice(),
233 uno::Reference
< awt::XWindow
> xParentWindow
;
235 getPropertyValue( xParentWindow
,
237 ::rtl::OUString::createFromAscii( "Window" )) )
239 const awt::Rectangle
aRect( xParentWindow
->getPosSize() );
241 maWindowOffset
.X
= aRect
.X
;
242 maWindowOffset
.Y
= aRect
.Y
;
245 ::basegfx::B2DRange aTmpRange
;
246 ::canvas::tools::calcTransformedRectBounds( aTmpRange
,
248 mpViewLayer
->getTransformation() );
249 const ::basegfx::B2IRange
& rRangePix(
250 ::basegfx::unotools::b2ISurroundingRangeFromB2DRange( aTmpRange
));
252 mxPlayerWindow
->setEnable( !rRangePix
.isEmpty() );
254 if( rRangePix
.isEmpty() )
257 const Point
aPosPixel( rRangePix
.getMinX() + maWindowOffset
.X
,
258 rRangePix
.getMinY() + maWindowOffset
.Y
);
259 const Size
aSizePixel( rRangePix
.getMaxX() - rRangePix
.getMinX(),
260 rRangePix
.getMaxY() - rRangePix
.getMinY() );
262 if( mpMediaWindow
.get() )
264 mpMediaWindow
->SetPosSizePixel( aPosPixel
, aSizePixel
);
265 mxPlayerWindow
->setPosSize( 0, 0,
266 aSizePixel
.Width(), aSizePixel
.Height(),
271 mxPlayerWindow
->setPosSize( aPosPixel
.X(), aPosPixel
.Y(),
272 aSizePixel
.Width(), aSizePixel
.Height(),
279 // ---------------------------------------------------------------------
281 bool ViewMediaShape::implInitialize( const ::basegfx::B2DRectangle
& rBounds
)
283 if( !mxPlayer
.is() && mxShape
.is() )
285 ENSURE_OR_RETURN( mpViewLayer
->getCanvas(),
286 "ViewMediaShape::update(): Invalid layer canvas" );
288 uno::Reference
< rendering::XCanvas
> xCanvas( mpViewLayer
->getCanvas()->getUNOCanvas() );
292 uno::Reference
< beans::XPropertySet
> xPropSet
;
293 ::rtl::OUString aURL
;
297 xPropSet
.set( mxShape
, uno::UNO_QUERY
);
301 ( xPropSet
->getPropertyValue(
302 ::rtl::OUString( RTL_CONSTASCII_USTRINGPARAM( "MediaURL" ) ) ) >>=aURL
) )
304 implInitializeMediaPlayer( aURL
);
307 // create visible object
308 uno::Sequence
< uno::Any
> aDeviceParams
;
310 if( ::canvas::tools::getDeviceInfo( xCanvas
, aDeviceParams
).getLength() > 1 )
312 ::rtl::OUString aImplName
;
314 aDeviceParams
[ 0 ] >>= aImplName
;
316 if( aImplName
.endsWithIgnoreAsciiCaseAsciiL(
317 RTL_CONSTASCII_STRINGPARAM("VCL") ) || aImplName
.endsWithIgnoreAsciiCaseAsciiL(
318 RTL_CONSTASCII_STRINGPARAM("Cairo") ) )
320 implInitializeVCLBasedPlayerWindow( rBounds
, aDeviceParams
);
322 else if( aImplName
.endsWithIgnoreAsciiCaseAsciiL(
323 RTL_CONSTASCII_STRINGPARAM("DX")) ||
324 aImplName
.endsWithIgnoreAsciiCaseAsciiL(
325 RTL_CONSTASCII_STRINGPARAM("DX9")))
327 implInitializeDXBasedPlayerWindow( rBounds
, aDeviceParams
);
331 // set player properties
332 implSetMediaProperties( xPropSet
);
334 catch( uno::RuntimeException
& )
338 catch( uno::Exception
& )
341 rtl::OUStringToOString(
342 comphelper::anyToString( cppu::getCaughtException() ),
343 RTL_TEXTENCODING_UTF8
).getStr() );
348 return mxPlayer
.is() || mxPlayerWindow
.is();
351 // ---------------------------------------------------------------------
353 void ViewMediaShape::implSetMediaProperties( const uno::Reference
< beans::XPropertySet
>& rxProps
)
357 mxPlayer
->setMediaTime( 0.0 );
361 sal_Bool
bLoop( false );
362 getPropertyValue( bLoop
,
364 ::rtl::OUString( RTL_CONSTASCII_USTRINGPARAM( "Loop" )));
365 mxPlayer
->setPlaybackLoop( bLoop
);
367 sal_Bool
bMute( false );
368 getPropertyValue( bMute
,
370 ::rtl::OUString( RTL_CONSTASCII_USTRINGPARAM( "Mute" )));
371 mxPlayer
->setMute( bMute
|| !mbIsSoundEnabled
);
373 sal_Int16
nVolumeDB(0);
374 getPropertyValue( nVolumeDB
,
376 ::rtl::OUString( RTL_CONSTASCII_USTRINGPARAM( "VolumeDB" )));
377 mxPlayer
->setVolumeDB( nVolumeDB
);
379 if( mxPlayerWindow
.is() )
381 media::ZoomLevel
eZoom(media::ZoomLevel_FIT_TO_WINDOW
);
382 getPropertyValue( eZoom
,
384 ::rtl::OUString( RTL_CONSTASCII_USTRINGPARAM( "Zoom" )));
385 mxPlayerWindow
->setZoomLevel( eZoom
);
391 // ---------------------------------------------------------------------
393 void ViewMediaShape::implInitializeMediaPlayer( const ::rtl::OUString
& rMediaURL
)
399 if( rMediaURL
.getLength() )
401 mxPlayer
.set( avmedia::MediaWindow::createPlayer( rMediaURL
),
405 catch( uno::RuntimeException
& )
409 catch( const uno::Exception
& )
411 throw lang::NoSupportException(
412 rtl::OUString( RTL_CONSTASCII_USTRINGPARAM(
413 "No video support for ") ) + rMediaURL
,
414 uno::Reference
<uno::XInterface
>() );
419 // ---------------------------------------------------------------------
421 bool ViewMediaShape::implInitializeVCLBasedPlayerWindow( const ::basegfx::B2DRectangle
& rBounds
,
422 const uno::Sequence
< uno::Any
>& rVCLDeviceParams
)
424 OSL_TRACE( "ViewMediaShape::implInitializeVCLBasedPlayerWindow" );
425 if( !mpMediaWindow
.get() && !rBounds
.isEmpty() )
431 rVCLDeviceParams
[ 1 ] >>= aVal
;
433 Window
* pWindow
= reinterpret_cast< Window
* >( aVal
);
437 ::basegfx::B2DRange aTmpRange
;
438 ::canvas::tools::calcTransformedRectBounds( aTmpRange
,
440 mpViewLayer
->getTransformation() );
441 const ::basegfx::B2IRange
& rRangePix(
442 ::basegfx::unotools::b2ISurroundingRangeFromB2DRange( aTmpRange
));
444 if( !rRangePix
.isEmpty() )
446 uno::Sequence
< uno::Any
> aArgs( 3 );
447 awt::Rectangle
aAWTRect( rRangePix
.getMinX(),
449 rRangePix
.getMaxX() - rRangePix
.getMinX(),
450 rRangePix
.getMaxY() - rRangePix
.getMinY() );
453 OSL_TRACE( "created sys child window for viewmediashape" );
454 mpMediaWindow
= ::std::auto_ptr
< SystemChildWindow
>( new SystemChildWindow( pWindow
, WB_CLIPCHILDREN
) );
456 mpMediaWindow
= ::std::auto_ptr
< JavaChildWindow
>( new JavaChildWindow( pWindow
, WB_CLIPCHILDREN
) );
458 mpMediaWindow
->SetBackground( Color( COL_BLACK
) );
459 mpMediaWindow
->SetPosSizePixel( Point( aAWTRect
.X
,
461 Size( aAWTRect
.Width
,
463 mpMediaWindow
->SetParentClipMode( PARENTCLIPMODE_NOCLIP
);
464 mpMediaWindow
->EnableEraseBackground( FALSE
);
465 mpMediaWindow
->EnablePaint( FALSE
);
466 mpMediaWindow
->SetForwardKey( TRUE
);
467 mpMediaWindow
->SetMouseTransparent( TRUE
);
468 mpMediaWindow
->Show();
473 aArgs
[ 0 ] = uno::makeAny(
474 sal::static_int_cast
<sal_IntPtr
>(
475 mpMediaWindow
->getParentWindowHandleForJava()) );
477 aArgs
[ 0 ] = uno::makeAny ( (sal_Int32
) 0 );
479 aAWTRect
.X
= aAWTRect
.Y
= 0;
480 aArgs
[ 1 ] = uno::makeAny( aAWTRect
);
482 const SystemEnvData
*pSystemData
= mpMediaWindow
->GetSystemData();
483 OSL_TRACE( "xwindow id: %ld", pSystemData
->aWindow
);
484 aArgs
[ 2 ] = uno::makeAny( pSystemData
->aWindow
);
487 mxPlayerWindow
.set( mxPlayer
->createPlayerWindow( aArgs
) );
489 if( mxPlayerWindow
.is() )
491 mxPlayerWindow
->setVisible( true );
492 mxPlayerWindow
->setEnable( true );
498 catch( uno::RuntimeException
& )
502 catch( uno::Exception
& )
505 rtl::OUStringToOString(
506 comphelper::anyToString( cppu::getCaughtException() ),
507 RTL_TEXTENCODING_UTF8
).getStr() );
511 return mxPlayerWindow
.is();
514 // ---------------------------------------------------------------------
516 bool ViewMediaShape::implInitializeDXBasedPlayerWindow( const ::basegfx::B2DRectangle
& rBounds
,
517 const uno::Sequence
< uno::Any
>& rDXDeviceParams
)
519 if( !mxPlayerWindow
.is() )
523 if( rDXDeviceParams
.getLength() == 2 )
527 rDXDeviceParams
[ 1 ] >>= aWNDVal
;
531 ::basegfx::B2DRange aTmpRange
;
532 ::canvas::tools::calcTransformedRectBounds( aTmpRange
,
534 mpViewLayer
->getTransformation() );
535 const ::basegfx::B2IRange
& rRangePix(
536 ::basegfx::unotools::b2ISurroundingRangeFromB2DRange( aTmpRange
));
538 if( !rRangePix
.isEmpty() )
540 uno::Sequence
< uno::Any
> aArgs( 2 );
541 awt::Rectangle
aAWTRect( rRangePix
.getMinX() + maWindowOffset
.X
,
542 rRangePix
.getMinY() + maWindowOffset
.Y
,
543 rRangePix
.getMaxX() - rRangePix
.getMinX(),
544 rRangePix
.getMaxY() - rRangePix
.getMinY() );
548 aArgs
[ 0 ] = uno::makeAny(
549 sal::static_int_cast
<sal_Int32
>(
551 aArgs
[ 1 ] = uno::makeAny( aAWTRect
);
553 mxPlayerWindow
.set( mxPlayer
->createPlayerWindow( aArgs
) );
559 catch( uno::RuntimeException
& )
563 catch( uno::Exception
& )
566 rtl::OUStringToOString(
567 comphelper::anyToString( cppu::getCaughtException() ),
568 RTL_TEXTENCODING_UTF8
).getStr() );
572 return mxPlayerWindow
.is();