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()
51 // -------------------------------------------------------------------------
53 uno::Reference
< media::XPlayer
> MediaWindowBaseImpl::createPlayer( const OUString
& rURL
)
55 uno::Reference
< media::XPlayer
> xPlayer
;
56 uno::Reference
< uno::XComponentContext
> xContext( ::comphelper::getProcessComponentContext() );
58 static const char * aServiceManagers
[] = {
59 AVMEDIA_MANAGER_SERVICE_NAME
,
60 // a fallback path just for gstreamer which has
61 // two significant versions deployed at once ...
62 #ifdef AVMEDIA_MANAGER_SERVICE_NAME_OLD
63 AVMEDIA_MANAGER_SERVICE_NAME_OLD
67 for( sal_uInt32 i
= 0; !xPlayer
.is() && i
< SAL_N_ELEMENTS( aServiceManagers
); ++i
)
69 const OUString
aServiceName( aServiceManagers
[ i
],
70 strlen( aServiceManagers
[ i
] ),
71 RTL_TEXTENCODING_ASCII_US
);
74 uno::Reference
< media::XManager
> xManager (
75 xContext
->getServiceManager()->createInstanceWithContext(aServiceName
, xContext
),
78 xPlayer
= uno::Reference
< media::XPlayer
>( xManager
->createPlayer( rURL
),
82 "failed to create media player service " << aServiceName
);
83 } catch ( const uno::Exception
&e
) {
85 "couldn't create media player " AVMEDIA_MANAGER_SERVICE_NAME
86 ", exception '" << e
.Message
<< '\'');
93 void MediaWindowBaseImpl::setURL( const OUString
& rURL
,
94 OUString
const& rTempURL
)
96 if( rURL
!= getURL() )
101 if( mxPlayerWindow
.is() )
103 mxPlayerWindow
->setVisible( false );
104 mxPlayerWindow
.clear();
108 mTempFileURL
= OUString();
110 if (!rTempURL
.isEmpty())
113 mTempFileURL
= rTempURL
;
117 INetURLObject
aURL( rURL
);
119 if (aURL
.GetProtocol() != INET_PROT_NOT_VALID
)
120 maFileURL
= aURL
.GetMainURL(INetURLObject::DECODE_UNAMBIGUOUS
);
125 mxPlayer
= createPlayer(
126 (!mTempFileURL
.isEmpty()) ? mTempFileURL
: maFileURL
);
131 // ---------------------------------------------------------------------
133 void MediaWindowBaseImpl::onURLChanged()
137 // ---------------------------------------------------------------------
139 const OUString
& MediaWindowBaseImpl::getURL() const
144 // ---------------------------------------------------------------------
146 bool MediaWindowBaseImpl::isValid() const
148 return( getPlayer().is() );
151 // ---------------------------------------------------------------------
153 void MediaWindowBaseImpl::stopPlayingInternal( bool bStop
)
157 bStop
? mxPlayer
->stop() : mxPlayer
->start();
161 // ---------------------------------------------------------------------
163 MediaWindow
* MediaWindowBaseImpl::getMediaWindow() const
165 return mpMediaWindow
;
168 // ---------------------------------------------------------------------
170 uno::Reference
< media::XPlayer
> MediaWindowBaseImpl::getPlayer() const
175 // ---------------------------------------------------------------------
177 uno::Reference
< media::XPlayerWindow
> MediaWindowBaseImpl::getPlayerWindow() const
179 return mxPlayerWindow
;
182 // ---------------------------------------------------------------------
184 void MediaWindowBaseImpl::setPlayerWindow( const uno::Reference
< media::XPlayerWindow
>& rxPlayerWindow
)
186 mxPlayerWindow
= rxPlayerWindow
;
189 // ---------------------------------------------------------------------
191 void MediaWindowBaseImpl::cleanUp()
193 uno::Reference
< lang::XComponent
> xComponent( mxPlayer
, uno::UNO_QUERY
);
194 if( xComponent
.is() ) // this stops the player
195 xComponent
->dispose();
199 mpMediaWindow
= NULL
;
202 // ---------------------------------------------------------------------
204 Size
MediaWindowBaseImpl::getPreferredSize() const
210 awt::Size
aPrefSize( mxPlayer
->getPreferredPlayerWindowSize() );
212 aRet
.Width() = aPrefSize
.Width
;
213 aRet
.Height() = aPrefSize
.Height
;
219 // ---------------------------------------------------------------------
221 bool MediaWindowBaseImpl::setZoom( ::com::sun::star::media::ZoomLevel eLevel
)
223 return( mxPlayerWindow
.is() ? mxPlayerWindow
->setZoomLevel( eLevel
) : false );
226 // -------------------------------------------------------------------------
228 ::com::sun::star::media::ZoomLevel
MediaWindowBaseImpl::getZoom() const
230 return( mxPlayerWindow
.is() ? mxPlayerWindow
->getZoomLevel() : media::ZoomLevel_NOT_AVAILABLE
);
233 // ---------------------------------------------------------------------
235 bool MediaWindowBaseImpl::start()
237 return( mxPlayer
.is() ? ( mxPlayer
->start(), true ) : false );
240 // ---------------------------------------------------------------------
242 void MediaWindowBaseImpl::stop()
248 // ---------------------------------------------------------------------
250 bool MediaWindowBaseImpl::isPlaying() const
252 return( mxPlayer
.is() && mxPlayer
->isPlaying() );
255 // ---------------------------------------------------------------------
257 double MediaWindowBaseImpl::getDuration() const
259 return( mxPlayer
.is() ? mxPlayer
->getDuration() : 0.0 );
262 // ---------------------------------------------------------------------
264 void MediaWindowBaseImpl::setMediaTime( double fTime
)
267 mxPlayer
->setMediaTime( fTime
);
270 // ---------------------------------------------------------------------
272 double MediaWindowBaseImpl::getMediaTime() const
274 return( mxPlayer
.is() ? mxPlayer
->getMediaTime() : 0.0 );
277 // ---------------------------------------------------------------------
279 double MediaWindowBaseImpl::getRate() const
281 return( mxPlayer
.is() ? mxPlayer
->getRate() : 0.0 );
284 // ---------------------------------------------------------------------
286 void MediaWindowBaseImpl::setPlaybackLoop( bool bSet
)
289 mxPlayer
->setPlaybackLoop( bSet
);
292 // ---------------------------------------------------------------------
294 bool MediaWindowBaseImpl::isPlaybackLoop() const
296 return( mxPlayer
.is() ? mxPlayer
->isPlaybackLoop() : false );
299 // ---------------------------------------------------------------------
301 void MediaWindowBaseImpl::setMute( bool bSet
)
304 mxPlayer
->setMute( bSet
);
307 // ---------------------------------------------------------------------
309 bool MediaWindowBaseImpl::isMute() const
311 return( mxPlayer
.is() ? mxPlayer
->isMute() : false );
314 // ---------------------------------------------------------------------
316 void MediaWindowBaseImpl::setVolumeDB( sal_Int16 nVolumeDB
)
319 mxPlayer
->setVolumeDB( nVolumeDB
);
322 // ---------------------------------------------------------------------
324 sal_Int16
MediaWindowBaseImpl::getVolumeDB() const
326 return( mxPlayer
.is() ? mxPlayer
->getVolumeDB() : 0 );
329 // -------------------------------------------------------------------------
331 void MediaWindowBaseImpl::updateMediaItem( MediaItem
& rItem
) const
334 rItem
.setState( ( getRate() > 1.0 ) ? MEDIASTATE_PLAYFFW
: MEDIASTATE_PLAY
);
336 rItem
.setState( ( 0.0 == getMediaTime() ) ? MEDIASTATE_STOP
: MEDIASTATE_PAUSE
);
338 rItem
.setDuration( getDuration() );
339 rItem
.setTime( getMediaTime() );
340 rItem
.setLoop( isPlaybackLoop() );
341 rItem
.setMute( isMute() );
342 rItem
.setVolumeDB( getVolumeDB() );
343 rItem
.setZoom( getZoom() );
344 rItem
.setURL( getURL(), &mTempFileURL
);
347 // -------------------------------------------------------------------------
349 void MediaWindowBaseImpl::executeMediaItem( const MediaItem
& rItem
)
351 const sal_uInt32 nMaskSet
= rItem
.getMaskSet();
354 if( nMaskSet
& AVMEDIA_SETMASK_URL
)
355 setURL( rItem
.getURL(), rItem
.getTempURL() );
357 // set different states next
358 if( nMaskSet
& AVMEDIA_SETMASK_TIME
)
359 setMediaTime( ::std::min( rItem
.getTime(), getDuration() ) );
361 if( nMaskSet
& AVMEDIA_SETMASK_LOOP
)
362 setPlaybackLoop( rItem
.isLoop() );
364 if( nMaskSet
& AVMEDIA_SETMASK_MUTE
)
365 setMute( rItem
.isMute() );
367 if( nMaskSet
& AVMEDIA_SETMASK_VOLUMEDB
)
368 setVolumeDB( rItem
.getVolumeDB() );
370 if( nMaskSet
& AVMEDIA_SETMASK_ZOOM
)
371 setZoom( rItem
.getZoom() );
373 // set play state at last
374 if( nMaskSet
& AVMEDIA_SETMASK_STATE
)
376 switch( rItem
.getState() )
378 case( MEDIASTATE_PLAY
):
379 case( MEDIASTATE_PLAYFFW
):
387 case( MEDIASTATE_PAUSE
):
394 case( MEDIASTATE_STOP
):
409 } // namespace avemdia
411 /* vim:set shiftwidth=4 softtabstop=4 expandtab: */