1 /**************************************************************
2  *
3  * Licensed to the Apache Software Foundation (ASF) under one
4  * or more contributor license agreements.  See the NOTICE file
5  * distributed with this work for additional information
6  * regarding copyright ownership.  The ASF licenses this file
7  * to you under the Apache License, Version 2.0 (the
8  * "License"); you may not use this file except in compliance
9  * with the License.  You may obtain a copy of the License at
10  *
11  *   http://www.apache.org/licenses/LICENSE-2.0
12  *
13  * Unless required by applicable law or agreed to in writing,
14  * software distributed under the License is distributed on an
15  * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
16  * KIND, either express or implied.  See the License for the
17  * specific language governing permissions and limitations
18  * under the License.
19  *
20  *************************************************************/
21 
22 
23 
24 #include "mediawindowbase_impl.hxx"
25 #include <avmedia/mediaitem.hxx>
26 #include "mediamisc.hxx"
27 #include "mediawindow.hrc"
28 #include <tools/urlobj.hxx>
29 #include <comphelper/processfactory.hxx>
30 #include <com/sun/star/lang/XMultiServiceFactory.hpp>
31 #include <com/sun/star/media/XManager.hpp>
32 #ifndef _COM_SUN_STAR_LANG_XCOMPONENT_HDL_
33 #include <com/sun/star/lang/XComponent.hdl>
34 #endif
35 
36 #define MEDIA_TIMER_TIMEOUT 100
37 
38 using namespace ::com::sun::star;
39 
40 namespace avmedia { namespace priv {
41 
42 // -----------------------
43 // - MediaWindowBaseImpl -
44 // -----------------------
45 
46 struct ServiceManager
47 {
48 	const char* pServiceName;
49     sal_Bool    bIsJavaBased;
50 };
51 
52 // ---------------------------------------------------------------------
53 
54 
MediaWindowBaseImpl(MediaWindow * pMediaWindow)55 MediaWindowBaseImpl::MediaWindowBaseImpl( MediaWindow* pMediaWindow ) :
56     mpMediaWindow( pMediaWindow ),
57     mbIsMediaWindowJavaBased( sal_False )
58 {
59 }
60 
61 // ---------------------------------------------------------------------
62 
~MediaWindowBaseImpl()63 MediaWindowBaseImpl::~MediaWindowBaseImpl()
64 {
65     uno::Reference< lang::XMultiServiceFactory > xFactory( ::comphelper::getProcessServiceFactory() );
66 }
67 
68 // -------------------------------------------------------------------------
69 
createPlayer(const::rtl::OUString & rURL,sal_Bool & rbJavaBased)70 uno::Reference< media::XPlayer > MediaWindowBaseImpl::createPlayer( const ::rtl::OUString& rURL, sal_Bool& rbJavaBased )
71 {
72     uno::Reference< lang::XMultiServiceFactory >    xFactory( ::comphelper::getProcessServiceFactory() );
73     uno::Reference< media::XPlayer >                xPlayer;
74 
75     rbJavaBased = sal_False;
76 
77     if( xFactory.is() )
78     {
79         static const ServiceManager aServiceManagers[] =
80         {
81             { AVMEDIA_MANAGER_SERVICE_NAME, AVMEDIA_MANAGER_SERVICE_IS_JAVABASED },
82             { AVMEDIA_MANAGER_SERVICE_NAME_FALLBACK1, AVMEDIA_MANAGER_SERVICE_IS_JAVABASED_FALLBACK1 }
83         };
84 
85         for( sal_uInt32 i = 0; !xPlayer.is() && ( i < ( sizeof( aServiceManagers ) / sizeof( ServiceManager ) ) ); ++i )
86         {
87             const String aServiceName( aServiceManagers[ i ].pServiceName, RTL_TEXTENCODING_ASCII_US );
88 
89             if( aServiceName.Len() )
90             {
91                 OSL_TRACE( "Trying to create media manager service %s", aServiceManagers[ i ].pServiceName );
92 
93                 try
94                 {
95                     uno::Reference< media::XManager > xManager( xFactory->createInstance( aServiceName ), uno::UNO_QUERY );
96 
97                     if( xManager.is() )
98                     {
99                         xPlayer = uno::Reference< media::XPlayer >( xManager->createPlayer( rURL ), uno::UNO_QUERY );
100                     }
101                 }
102                 catch( ... )
103                 {
104                 }
105             }
106 
107             if( xPlayer.is() )
108             {
109                 rbJavaBased = aServiceManagers[ i ].bIsJavaBased;
110             }
111         }
112     }
113 
114     return xPlayer;
115 }
116 
117 // ---------------------------------------------------------------------
118 
setURL(const::rtl::OUString & rURL)119 void MediaWindowBaseImpl::setURL( const ::rtl::OUString& rURL )
120 {
121     if( rURL != getURL() )
122     {
123         INetURLObject aURL( maFileURL = rURL );
124 
125         if( mxPlayer.is() )
126             mxPlayer->stop();
127 
128         if( mxPlayerWindow.is() )
129         {
130             mxPlayerWindow->setVisible( false );
131             mxPlayerWindow.clear();
132         }
133 
134         mxPlayer.clear();
135 
136         if( aURL.GetProtocol() != INET_PROT_NOT_VALID )
137             maFileURL = aURL.GetMainURL( INetURLObject::DECODE_UNAMBIGUOUS );
138 
139         mxPlayer = createPlayer( maFileURL, mbIsMediaWindowJavaBased );
140         onURLChanged();
141     }
142 }
143 
144 // ---------------------------------------------------------------------
145 
onURLChanged()146 void MediaWindowBaseImpl::onURLChanged()
147 {
148 }
149 
150 // ---------------------------------------------------------------------
151 
getURL() const152 const ::rtl::OUString& MediaWindowBaseImpl::getURL() const
153 {
154     return maFileURL;
155 }
156 
157 // ---------------------------------------------------------------------
158 
isValid() const159 bool MediaWindowBaseImpl::isValid() const
160 {
161     return( getPlayer().is() );
162 }
163 
164 // ---------------------------------------------------------------------
165 
stopPlayingInternal(bool bStop)166 void MediaWindowBaseImpl::stopPlayingInternal( bool bStop )
167 {
168     if( isPlaying() )
169     {
170         bStop ? mxPlayer->stop() : mxPlayer->start();
171     }
172 }
173 
174 // ---------------------------------------------------------------------
175 
getMediaWindow() const176 MediaWindow* MediaWindowBaseImpl::getMediaWindow() const
177 {
178     return mpMediaWindow;
179 }
180 
181 // ---------------------------------------------------------------------
182 
getPlayer() const183 uno::Reference< media::XPlayer > MediaWindowBaseImpl::getPlayer() const
184 {
185     return mxPlayer;
186 }
187 
188 // ---------------------------------------------------------------------
189 
getPlayerWindow() const190 uno::Reference< media::XPlayerWindow > MediaWindowBaseImpl::getPlayerWindow() const
191 {
192     return mxPlayerWindow;
193 }
194 
195 // ---------------------------------------------------------------------
196 
setPlayerWindow(const uno::Reference<media::XPlayerWindow> & rxPlayerWindow)197 void MediaWindowBaseImpl::setPlayerWindow( const uno::Reference< media::XPlayerWindow >& rxPlayerWindow )
198 {
199     mxPlayerWindow = rxPlayerWindow;
200 }
201 
202 // ---------------------------------------------------------------------
203 
cleanUp()204 void MediaWindowBaseImpl::cleanUp()
205 {
206     uno::Reference< lang::XComponent > xComponent( mxPlayer, uno::UNO_QUERY );
207     if( xComponent.is() ) // this stops the player
208         xComponent->dispose();
209 
210     mxPlayer.clear();
211 
212     mpMediaWindow = NULL;
213 }
214 
215 // ---------------------------------------------------------------------
216 
hasPreferredSize() const217 bool MediaWindowBaseImpl::hasPreferredSize() const
218 {
219     return( mxPlayerWindow.is() );
220 }
221 
222 // ---------------------------------------------------------------------
223 
getPreferredSize() const224 Size MediaWindowBaseImpl::getPreferredSize() const
225 {
226     Size aRet;
227 
228     if( mxPlayer.is() )
229     {
230         awt::Size aPrefSize( mxPlayer->getPreferredPlayerWindowSize() );
231 
232         aRet.Width() = aPrefSize.Width;
233         aRet.Height() = aPrefSize.Height;
234     }
235 
236     return aRet;
237 }
238 
239 // ---------------------------------------------------------------------
240 
setZoom(::com::sun::star::media::ZoomLevel eLevel)241 bool MediaWindowBaseImpl::setZoom( ::com::sun::star::media::ZoomLevel eLevel )
242 {
243     return( mxPlayerWindow.is() ? mxPlayerWindow->setZoomLevel( eLevel ) : false );
244 }
245 
246 // -------------------------------------------------------------------------
247 
getZoom() const248 ::com::sun::star::media::ZoomLevel MediaWindowBaseImpl::getZoom() const
249 {
250     return( mxPlayerWindow.is() ? mxPlayerWindow->getZoomLevel() : ::com::sun::star::media::ZoomLevel_NOT_AVAILABLE );
251 }
252 
253 // ---------------------------------------------------------------------
254 
start()255 bool MediaWindowBaseImpl::start()
256 {
257     return( mxPlayer.is() ? ( mxPlayer->start(), true ) : false );
258 }
259 
260 // ---------------------------------------------------------------------
261 
stop()262 void MediaWindowBaseImpl::stop()
263 {
264     if( mxPlayer.is() )
265         mxPlayer->stop();
266 }
267 
268 // ---------------------------------------------------------------------
269 
isPlaying() const270 bool MediaWindowBaseImpl::isPlaying() const
271 {
272     return( mxPlayer.is() && mxPlayer->isPlaying() );
273 }
274 
275 // ---------------------------------------------------------------------
276 
getDuration() const277 double MediaWindowBaseImpl::getDuration() const
278 {
279     return( mxPlayer.is() ? mxPlayer->getDuration() : 0.0 );
280 }
281 
282 // ---------------------------------------------------------------------
283 
setMediaTime(double fTime)284 void MediaWindowBaseImpl::setMediaTime( double fTime )
285 {
286     if( mxPlayer.is() )
287         mxPlayer->setMediaTime( fTime );
288 }
289 
290 // ---------------------------------------------------------------------
291 
getMediaTime() const292 double MediaWindowBaseImpl::getMediaTime() const
293 {
294     return( mxPlayer.is() ? mxPlayer->getMediaTime() : 0.0 );
295 }
296 
297 // ---------------------------------------------------------------------
298 
setStopTime(double fTime)299 void MediaWindowBaseImpl::setStopTime( double fTime )
300 {
301     if( mxPlayer.is() )
302         mxPlayer->setStopTime( fTime );
303 }
304 
305 // ---------------------------------------------------------------------
306 
getStopTime() const307 double MediaWindowBaseImpl::getStopTime() const
308 {
309     return( mxPlayer.is() ? mxPlayer->getStopTime() : 0.0 );
310 }
311 
312 // ---------------------------------------------------------------------
313 
setRate(double fRate)314 void MediaWindowBaseImpl::setRate( double fRate )
315 {
316     if( mxPlayer.is() )
317         mxPlayer->setRate( fRate );
318 }
319 
320 // ---------------------------------------------------------------------
321 
getRate() const322 double MediaWindowBaseImpl::getRate() const
323 {
324     return( mxPlayer.is() ? mxPlayer->getRate() : 0.0 );
325 }
326 
327 // ---------------------------------------------------------------------
328 
setPlaybackLoop(bool bSet)329 void MediaWindowBaseImpl::setPlaybackLoop( bool bSet )
330 {
331     if( mxPlayer.is() )
332         mxPlayer->setPlaybackLoop( bSet );
333 }
334 
335 // ---------------------------------------------------------------------
336 
isPlaybackLoop() const337 bool MediaWindowBaseImpl::isPlaybackLoop() const
338 {
339     return( mxPlayer.is() ? mxPlayer->isPlaybackLoop() : false );
340 }
341 
342 // ---------------------------------------------------------------------
343 
setMute(bool bSet)344 void MediaWindowBaseImpl::setMute( bool bSet )
345 {
346     if( mxPlayer.is() )
347         mxPlayer->setMute( bSet );
348 }
349 
350 // ---------------------------------------------------------------------
351 
isMute() const352 bool MediaWindowBaseImpl::isMute() const
353 {
354     return( mxPlayer.is() ? mxPlayer->isMute() : false );
355 }
356 
357 // ---------------------------------------------------------------------
358 
setVolumeDB(sal_Int16 nVolumeDB)359 void MediaWindowBaseImpl::setVolumeDB( sal_Int16 nVolumeDB )
360 {
361     if( mxPlayer.is() )
362         mxPlayer->setVolumeDB( nVolumeDB );
363 }
364 
365 // ---------------------------------------------------------------------
366 
getVolumeDB() const367 sal_Int16 MediaWindowBaseImpl::getVolumeDB() const
368 {
369     return( mxPlayer.is() ? mxPlayer->getVolumeDB() : 0 );
370 }
371 
372 // -------------------------------------------------------------------------
373 
updateMediaItem(MediaItem & rItem) const374 void MediaWindowBaseImpl::updateMediaItem( MediaItem& rItem ) const
375 {
376     if( isPlaying() )
377         rItem.setState( ( getRate() > 1.0 ) ? MEDIASTATE_PLAYFFW : MEDIASTATE_PLAY );
378     else
379         rItem.setState( ( 0.0 == getMediaTime() ) ? MEDIASTATE_STOP : MEDIASTATE_PAUSE );
380 
381     rItem.setDuration( getDuration() );
382     rItem.setTime( getMediaTime() );
383     rItem.setLoop( isPlaybackLoop() );
384     rItem.setMute( isMute() );
385     rItem.setVolumeDB( getVolumeDB() );
386     rItem.setZoom( getZoom() );
387     rItem.setURL( getURL() );
388 }
389 
390 // -------------------------------------------------------------------------
391 
executeMediaItem(const MediaItem & rItem)392 void MediaWindowBaseImpl::executeMediaItem( const MediaItem& rItem )
393 {
394     const sal_uInt32 nMaskSet = rItem.getMaskSet();
395 
396     // set URL first
397     if( nMaskSet & AVMEDIA_SETMASK_URL )
398         setURL( rItem.getURL() );
399 
400     // set different states next
401     if( nMaskSet & AVMEDIA_SETMASK_TIME )
402         setMediaTime( ::std::min( rItem.getTime(), getDuration() ) );
403 
404     if( nMaskSet & AVMEDIA_SETMASK_LOOP )
405         setPlaybackLoop( rItem.isLoop() );
406 
407     if( nMaskSet & AVMEDIA_SETMASK_MUTE )
408         setMute( rItem.isMute() );
409 
410     if( nMaskSet & AVMEDIA_SETMASK_VOLUMEDB )
411         setVolumeDB( rItem.getVolumeDB() );
412 
413     if( nMaskSet & AVMEDIA_SETMASK_ZOOM )
414         setZoom( rItem.getZoom() );
415 
416     // set play state at last
417     if( nMaskSet & AVMEDIA_SETMASK_STATE )
418     {
419         switch( rItem.getState() )
420         {
421             case( MEDIASTATE_PLAY ):
422             case( MEDIASTATE_PLAYFFW ):
423             {
424 /*
425                 const double fNewRate = ( ( MEDIASTATE_PLAYFFW == rItem.getState() ) ? AVMEDIA_FFW_PLAYRATE : 1.0 );
426 
427                 if( getRate() != fNewRate )
428                     setRate( fNewRate );
429 */
430                 if( !isPlaying() )
431                     start();
432             }
433             break;
434 
435             case( MEDIASTATE_PAUSE ):
436             {
437                 if( isPlaying() )
438                     stop();
439             }
440             break;
441 
442             case( MEDIASTATE_STOP ):
443             {
444                 if( isPlaying() )
445                 {
446                     setMediaTime( 0.0 );
447                     stop();
448                     setMediaTime( 0.0 );
449                 }
450             }
451             break;
452         }
453     }
454 }
455 
456 } // namespace priv
457 } // namespace avmedia
458