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 "mediawindowbase_impl.hxx"
21 #include <avmedia/mediaitem.hxx>
22 #include "mediamisc.hxx"
23 #include "mediawindow.hrc"
24 #include <rtl/ustring.hxx>
25 #include <tools/urlobj.hxx>
26 #include <comphelper/processfactory.hxx>
27 #include <com/sun/star/lang/XMultiServiceFactory.hpp>
28 #include <com/sun/star/lang/XComponent.hpp>
29 #include <com/sun/star/media/XManager.hpp>
31 using namespace ::com::sun::star
;
33 namespace avmedia
{ namespace priv
{
35 // -----------------------
36 // - MediaWindowBaseImpl -
37 // -----------------------
40 MediaWindowBaseImpl::MediaWindowBaseImpl( MediaWindow
* pMediaWindow
)
41 : mpMediaWindow( pMediaWindow
)
45 // ---------------------------------------------------------------------
47 MediaWindowBaseImpl::~MediaWindowBaseImpl()
49 uno::Reference
< lang::XMultiServiceFactory
> xFactory( ::comphelper::getProcessServiceFactory() );
52 // -------------------------------------------------------------------------
54 uno::Reference
< media::XPlayer
> MediaWindowBaseImpl::createPlayer( const ::rtl::OUString
& rURL
)
56 uno::Reference
< media::XPlayer
> xPlayer
;
57 uno::Reference
< lang::XMultiServiceFactory
> xFactory( ::comphelper::getProcessServiceFactory() );
61 static const char * aServiceManagers
[] = {
62 AVMEDIA_MANAGER_SERVICE_NAME
,
63 // a fallback path just for gstreamer which has
64 // two significant versions deployed at once ...
65 #ifdef AVMEDIA_MANAGER_SERVICE_NAME_OLD
66 AVMEDIA_MANAGER_SERVICE_NAME_OLD
70 for( sal_uInt32 i
= 0; !xPlayer
.is() && i
< SAL_N_ELEMENTS( aServiceManagers
); ++i
)
72 const rtl::OUString
aServiceName( aServiceManagers
[ i
],
73 strlen( aServiceManagers
[ i
] ),
74 RTL_TEXTENCODING_ASCII_US
);
76 uno::Reference
< ::com::sun::star::media::XManager
> xManager
;
79 xManager
= uno::Reference
< ::com::sun::star::media::XManager
>(
80 xFactory
->createInstance( aServiceName
), uno::UNO_QUERY
);
82 xPlayer
= uno::Reference
< media::XPlayer
>( xManager
->createPlayer( rURL
),
86 "failed to create media player service " << aServiceName
);
87 } catch ( const uno::Exception
&e
) {
90 "couldn't create media player " AVMEDIA_MANAGER_SERVICE_NAME
91 ", exception '" << e
.Message
<< '\'');
99 void MediaWindowBaseImpl::setURL( const OUString
& rURL
,
100 OUString
const& rTempURL
)
102 if( rURL
!= getURL() )
107 if( mxPlayerWindow
.is() )
109 mxPlayerWindow
->setVisible( false );
110 mxPlayerWindow
.clear();
114 mTempFileURL
= OUString();
116 if (rTempURL
.getLength())
119 mTempFileURL
= rTempURL
;
123 INetURLObject
aURL( rURL
);
125 if (aURL
.GetProtocol() != INET_PROT_NOT_VALID
)
126 maFileURL
= aURL
.GetMainURL(INetURLObject::DECODE_UNAMBIGUOUS
);
131 mxPlayer
= createPlayer(
132 (mTempFileURL
.getLength()) ? mTempFileURL
: maFileURL
);
137 // ---------------------------------------------------------------------
139 void MediaWindowBaseImpl::onURLChanged()
143 // ---------------------------------------------------------------------
145 const OUString
& MediaWindowBaseImpl::getURL() const
150 // ---------------------------------------------------------------------
152 bool MediaWindowBaseImpl::isValid() const
154 return( getPlayer().is() );
157 // ---------------------------------------------------------------------
159 void MediaWindowBaseImpl::stopPlayingInternal( bool bStop
)
163 bStop
? mxPlayer
->stop() : mxPlayer
->start();
167 // ---------------------------------------------------------------------
169 MediaWindow
* MediaWindowBaseImpl::getMediaWindow() const
171 return mpMediaWindow
;
174 // ---------------------------------------------------------------------
176 uno::Reference
< media::XPlayer
> MediaWindowBaseImpl::getPlayer() const
181 // ---------------------------------------------------------------------
183 uno::Reference
< media::XPlayerWindow
> MediaWindowBaseImpl::getPlayerWindow() const
185 return mxPlayerWindow
;
188 // ---------------------------------------------------------------------
190 void MediaWindowBaseImpl::setPlayerWindow( const uno::Reference
< media::XPlayerWindow
>& rxPlayerWindow
)
192 mxPlayerWindow
= rxPlayerWindow
;
195 // ---------------------------------------------------------------------
197 void MediaWindowBaseImpl::cleanUp()
199 uno::Reference
< lang::XComponent
> xComponent( mxPlayer
, uno::UNO_QUERY
);
200 if( xComponent
.is() ) // this stops the player
201 xComponent
->dispose();
205 mpMediaWindow
= NULL
;
208 // ---------------------------------------------------------------------
210 Size
MediaWindowBaseImpl::getPreferredSize() const
216 awt::Size
aPrefSize( mxPlayer
->getPreferredPlayerWindowSize() );
218 aRet
.Width() = aPrefSize
.Width
;
219 aRet
.Height() = aPrefSize
.Height
;
225 // ---------------------------------------------------------------------
227 bool MediaWindowBaseImpl::setZoom( ::com::sun::star::media::ZoomLevel eLevel
)
229 return( mxPlayerWindow
.is() ? mxPlayerWindow
->setZoomLevel( eLevel
) : false );
232 // -------------------------------------------------------------------------
234 ::com::sun::star::media::ZoomLevel
MediaWindowBaseImpl::getZoom() const
236 return( mxPlayerWindow
.is() ? mxPlayerWindow
->getZoomLevel() : ::com::sun::star::media::ZoomLevel_NOT_AVAILABLE
);
239 // ---------------------------------------------------------------------
241 bool MediaWindowBaseImpl::start()
243 return( mxPlayer
.is() ? ( mxPlayer
->start(), true ) : false );
246 // ---------------------------------------------------------------------
248 void MediaWindowBaseImpl::stop()
254 // ---------------------------------------------------------------------
256 bool MediaWindowBaseImpl::isPlaying() const
258 return( mxPlayer
.is() && mxPlayer
->isPlaying() );
261 // ---------------------------------------------------------------------
263 double MediaWindowBaseImpl::getDuration() const
265 return( mxPlayer
.is() ? mxPlayer
->getDuration() : 0.0 );
268 // ---------------------------------------------------------------------
270 void MediaWindowBaseImpl::setMediaTime( double fTime
)
273 mxPlayer
->setMediaTime( fTime
);
276 // ---------------------------------------------------------------------
278 double MediaWindowBaseImpl::getMediaTime() const
280 return( mxPlayer
.is() ? mxPlayer
->getMediaTime() : 0.0 );
283 // ---------------------------------------------------------------------
285 double MediaWindowBaseImpl::getRate() const
287 return( mxPlayer
.is() ? mxPlayer
->getRate() : 0.0 );
290 // ---------------------------------------------------------------------
292 void MediaWindowBaseImpl::setPlaybackLoop( bool bSet
)
295 mxPlayer
->setPlaybackLoop( bSet
);
298 // ---------------------------------------------------------------------
300 bool MediaWindowBaseImpl::isPlaybackLoop() const
302 return( mxPlayer
.is() ? mxPlayer
->isPlaybackLoop() : false );
305 // ---------------------------------------------------------------------
307 void MediaWindowBaseImpl::setMute( bool bSet
)
310 mxPlayer
->setMute( bSet
);
313 // ---------------------------------------------------------------------
315 bool MediaWindowBaseImpl::isMute() const
317 return( mxPlayer
.is() ? mxPlayer
->isMute() : false );
320 // ---------------------------------------------------------------------
322 void MediaWindowBaseImpl::setVolumeDB( sal_Int16 nVolumeDB
)
325 mxPlayer
->setVolumeDB( nVolumeDB
);
328 // ---------------------------------------------------------------------
330 sal_Int16
MediaWindowBaseImpl::getVolumeDB() const
332 return( mxPlayer
.is() ? mxPlayer
->getVolumeDB() : 0 );
335 // -------------------------------------------------------------------------
337 void MediaWindowBaseImpl::updateMediaItem( MediaItem
& rItem
) const
340 rItem
.setState( ( getRate() > 1.0 ) ? MEDIASTATE_PLAYFFW
: MEDIASTATE_PLAY
);
342 rItem
.setState( ( 0.0 == getMediaTime() ) ? MEDIASTATE_STOP
: MEDIASTATE_PAUSE
);
344 rItem
.setDuration( getDuration() );
345 rItem
.setTime( getMediaTime() );
346 rItem
.setLoop( isPlaybackLoop() );
347 rItem
.setMute( isMute() );
348 rItem
.setVolumeDB( getVolumeDB() );
349 rItem
.setZoom( getZoom() );
350 rItem
.setURL( getURL(), &mTempFileURL
);
353 // -------------------------------------------------------------------------
355 void MediaWindowBaseImpl::executeMediaItem( const MediaItem
& rItem
)
357 const sal_uInt32 nMaskSet
= rItem
.getMaskSet();
360 if( nMaskSet
& AVMEDIA_SETMASK_URL
)
361 setURL( rItem
.getURL(), rItem
.getTempURL() );
363 // set different states next
364 if( nMaskSet
& AVMEDIA_SETMASK_TIME
)
365 setMediaTime( ::std::min( rItem
.getTime(), getDuration() ) );
367 if( nMaskSet
& AVMEDIA_SETMASK_LOOP
)
368 setPlaybackLoop( rItem
.isLoop() );
370 if( nMaskSet
& AVMEDIA_SETMASK_MUTE
)
371 setMute( rItem
.isMute() );
373 if( nMaskSet
& AVMEDIA_SETMASK_VOLUMEDB
)
374 setVolumeDB( rItem
.getVolumeDB() );
376 if( nMaskSet
& AVMEDIA_SETMASK_ZOOM
)
377 setZoom( rItem
.getZoom() );
379 // set play state at last
380 if( nMaskSet
& AVMEDIA_SETMASK_STATE
)
382 switch( rItem
.getState() )
384 case( MEDIASTATE_PLAY
):
385 case( MEDIASTATE_PLAYFFW
):
393 case( MEDIASTATE_PAUSE
):
400 case( MEDIASTATE_STOP
):
415 } // namespace avemdia
417 /* vim:set shiftwidth=4 softtabstop=4 expandtab: */