xref: /trunk/main/avmedia/source/viewer/mediawindowbase_impl.cxx (revision 46880872fedb27c2d4aea27d378c5f20891d16b4)
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/XMultiComponentFactory.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< uno::XComponentContext > xContext( ::comphelper::getProcessComponentContext() );
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< uno::XComponentContext >        xContext( ::comphelper::getProcessComponentContext() );
73     uno::Reference< media::XPlayer >                xPlayer;
74 
75     rbJavaBased = sal_False;
76 
77     if( xContext.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         uno::Reference< lang::XMultiComponentFactory > xFactory = xContext->getServiceManager();
86         for( sal_uInt32 i = 0; !xPlayer.is() && ( i < ( sizeof( aServiceManagers ) / sizeof( ServiceManager ) ) ); ++i )
87         {
88             const String aServiceName( aServiceManagers[ i ].pServiceName, RTL_TEXTENCODING_ASCII_US );
89 
90             if( aServiceName.Len() )
91             {
92                 OSL_TRACE( "Trying to create media manager service %s", aServiceManagers[ i ].pServiceName );
93 
94                 try
95                 {
96                     uno::Reference< media::XManager > xManager( xFactory->createInstanceWithContext( aServiceName, xContext ), uno::UNO_QUERY );
97 
98                     if( xManager.is() )
99                     {
100                         xPlayer = uno::Reference< media::XPlayer >( xManager->createPlayer( rURL ), uno::UNO_QUERY );
101                     }
102                 }
103                 catch( ... )
104                 {
105                 }
106             }
107 
108             if( xPlayer.is() )
109             {
110                 rbJavaBased = aServiceManagers[ i ].bIsJavaBased;
111             }
112         }
113     }
114 
115     return xPlayer;
116 }
117 
118 // ---------------------------------------------------------------------
119 
setURL(const::rtl::OUString & rURL)120 void MediaWindowBaseImpl::setURL( const ::rtl::OUString& rURL )
121 {
122     if( rURL != getURL() )
123     {
124         INetURLObject aURL( maFileURL = rURL );
125 
126         if( mxPlayer.is() )
127             mxPlayer->stop();
128 
129         if( mxPlayerWindow.is() )
130         {
131             mxPlayerWindow->setVisible( false );
132             mxPlayerWindow.clear();
133         }
134 
135         mxPlayer.clear();
136 
137         if( aURL.GetProtocol() != INET_PROT_NOT_VALID )
138             maFileURL = aURL.GetMainURL( INetURLObject::DECODE_UNAMBIGUOUS );
139 
140         mxPlayer = createPlayer( maFileURL, mbIsMediaWindowJavaBased );
141         onURLChanged();
142     }
143 }
144 
145 // ---------------------------------------------------------------------
146 
onURLChanged()147 void MediaWindowBaseImpl::onURLChanged()
148 {
149 }
150 
151 // ---------------------------------------------------------------------
152 
getURL() const153 const ::rtl::OUString& MediaWindowBaseImpl::getURL() const
154 {
155     return maFileURL;
156 }
157 
158 // ---------------------------------------------------------------------
159 
isValid() const160 bool MediaWindowBaseImpl::isValid() const
161 {
162     return( getPlayer().is() );
163 }
164 
165 // ---------------------------------------------------------------------
166 
stopPlayingInternal(bool bStop)167 void MediaWindowBaseImpl::stopPlayingInternal( bool bStop )
168 {
169     if( isPlaying() )
170     {
171         bStop ? mxPlayer->stop() : mxPlayer->start();
172     }
173 }
174 
175 // ---------------------------------------------------------------------
176 
getMediaWindow() const177 MediaWindow* MediaWindowBaseImpl::getMediaWindow() const
178 {
179     return mpMediaWindow;
180 }
181 
182 // ---------------------------------------------------------------------
183 
getPlayer() const184 uno::Reference< media::XPlayer > MediaWindowBaseImpl::getPlayer() const
185 {
186     return mxPlayer;
187 }
188 
189 // ---------------------------------------------------------------------
190 
getPlayerWindow() const191 uno::Reference< media::XPlayerWindow > MediaWindowBaseImpl::getPlayerWindow() const
192 {
193     return mxPlayerWindow;
194 }
195 
196 // ---------------------------------------------------------------------
197 
setPlayerWindow(const uno::Reference<media::XPlayerWindow> & rxPlayerWindow)198 void MediaWindowBaseImpl::setPlayerWindow( const uno::Reference< media::XPlayerWindow >& rxPlayerWindow )
199 {
200     mxPlayerWindow = rxPlayerWindow;
201 }
202 
203 // ---------------------------------------------------------------------
204 
cleanUp()205 void MediaWindowBaseImpl::cleanUp()
206 {
207     uno::Reference< lang::XComponent > xComponent( mxPlayer, uno::UNO_QUERY );
208     if( xComponent.is() ) // this stops the player
209         xComponent->dispose();
210 
211     mxPlayer.clear();
212 
213     mpMediaWindow = NULL;
214 }
215 
216 // ---------------------------------------------------------------------
217 
hasPreferredSize() const218 bool MediaWindowBaseImpl::hasPreferredSize() const
219 {
220     return( mxPlayerWindow.is() );
221 }
222 
223 // ---------------------------------------------------------------------
224 
getPreferredSize() const225 Size MediaWindowBaseImpl::getPreferredSize() const
226 {
227     Size aRet;
228 
229     if( mxPlayer.is() )
230     {
231         awt::Size aPrefSize( mxPlayer->getPreferredPlayerWindowSize() );
232 
233         aRet.Width() = aPrefSize.Width;
234         aRet.Height() = aPrefSize.Height;
235     }
236 
237     return aRet;
238 }
239 
240 // ---------------------------------------------------------------------
241 
setZoom(::com::sun::star::media::ZoomLevel eLevel)242 bool MediaWindowBaseImpl::setZoom( ::com::sun::star::media::ZoomLevel eLevel )
243 {
244     return( mxPlayerWindow.is() ? mxPlayerWindow->setZoomLevel( eLevel ) : false );
245 }
246 
247 // -------------------------------------------------------------------------
248 
getZoom() const249 ::com::sun::star::media::ZoomLevel MediaWindowBaseImpl::getZoom() const
250 {
251     return( mxPlayerWindow.is() ? mxPlayerWindow->getZoomLevel() : ::com::sun::star::media::ZoomLevel_NOT_AVAILABLE );
252 }
253 
254 // ---------------------------------------------------------------------
255 
start()256 bool MediaWindowBaseImpl::start()
257 {
258     return( mxPlayer.is() ? ( mxPlayer->start(), true ) : false );
259 }
260 
261 // ---------------------------------------------------------------------
262 
stop()263 void MediaWindowBaseImpl::stop()
264 {
265     if( mxPlayer.is() )
266         mxPlayer->stop();
267 }
268 
269 // ---------------------------------------------------------------------
270 
isPlaying() const271 bool MediaWindowBaseImpl::isPlaying() const
272 {
273     return( mxPlayer.is() && mxPlayer->isPlaying() );
274 }
275 
276 // ---------------------------------------------------------------------
277 
getDuration() const278 double MediaWindowBaseImpl::getDuration() const
279 {
280     return( mxPlayer.is() ? mxPlayer->getDuration() : 0.0 );
281 }
282 
283 // ---------------------------------------------------------------------
284 
setMediaTime(double fTime)285 void MediaWindowBaseImpl::setMediaTime( double fTime )
286 {
287     if( mxPlayer.is() )
288         mxPlayer->setMediaTime( fTime );
289 }
290 
291 // ---------------------------------------------------------------------
292 
getMediaTime() const293 double MediaWindowBaseImpl::getMediaTime() const
294 {
295     return( mxPlayer.is() ? mxPlayer->getMediaTime() : 0.0 );
296 }
297 
298 // ---------------------------------------------------------------------
299 
setStopTime(double fTime)300 void MediaWindowBaseImpl::setStopTime( double fTime )
301 {
302     if( mxPlayer.is() )
303         mxPlayer->setStopTime( fTime );
304 }
305 
306 // ---------------------------------------------------------------------
307 
getStopTime() const308 double MediaWindowBaseImpl::getStopTime() const
309 {
310     return( mxPlayer.is() ? mxPlayer->getStopTime() : 0.0 );
311 }
312 
313 // ---------------------------------------------------------------------
314 
setRate(double fRate)315 void MediaWindowBaseImpl::setRate( double fRate )
316 {
317     if( mxPlayer.is() )
318         mxPlayer->setRate( fRate );
319 }
320 
321 // ---------------------------------------------------------------------
322 
getRate() const323 double MediaWindowBaseImpl::getRate() const
324 {
325     return( mxPlayer.is() ? mxPlayer->getRate() : 0.0 );
326 }
327 
328 // ---------------------------------------------------------------------
329 
setPlaybackLoop(bool bSet)330 void MediaWindowBaseImpl::setPlaybackLoop( bool bSet )
331 {
332     if( mxPlayer.is() )
333         mxPlayer->setPlaybackLoop( bSet );
334 }
335 
336 // ---------------------------------------------------------------------
337 
isPlaybackLoop() const338 bool MediaWindowBaseImpl::isPlaybackLoop() const
339 {
340     return( mxPlayer.is() ? mxPlayer->isPlaybackLoop() : false );
341 }
342 
343 // ---------------------------------------------------------------------
344 
setMute(bool bSet)345 void MediaWindowBaseImpl::setMute( bool bSet )
346 {
347     if( mxPlayer.is() )
348         mxPlayer->setMute( bSet );
349 }
350 
351 // ---------------------------------------------------------------------
352 
isMute() const353 bool MediaWindowBaseImpl::isMute() const
354 {
355     return( mxPlayer.is() ? mxPlayer->isMute() : false );
356 }
357 
358 // ---------------------------------------------------------------------
359 
setVolumeDB(sal_Int16 nVolumeDB)360 void MediaWindowBaseImpl::setVolumeDB( sal_Int16 nVolumeDB )
361 {
362     if( mxPlayer.is() )
363         mxPlayer->setVolumeDB( nVolumeDB );
364 }
365 
366 // ---------------------------------------------------------------------
367 
getVolumeDB() const368 sal_Int16 MediaWindowBaseImpl::getVolumeDB() const
369 {
370     return( mxPlayer.is() ? mxPlayer->getVolumeDB() : 0 );
371 }
372 
373 // -------------------------------------------------------------------------
374 
updateMediaItem(MediaItem & rItem) const375 void MediaWindowBaseImpl::updateMediaItem( MediaItem& rItem ) const
376 {
377     if( isPlaying() )
378         rItem.setState( ( getRate() > 1.0 ) ? MEDIASTATE_PLAYFFW : MEDIASTATE_PLAY );
379     else
380         rItem.setState( ( 0.0 == getMediaTime() ) ? MEDIASTATE_STOP : MEDIASTATE_PAUSE );
381 
382     rItem.setDuration( getDuration() );
383     rItem.setTime( getMediaTime() );
384     rItem.setLoop( isPlaybackLoop() );
385     rItem.setMute( isMute() );
386     rItem.setVolumeDB( getVolumeDB() );
387     rItem.setZoom( getZoom() );
388     rItem.setURL( getURL() );
389 }
390 
391 // -------------------------------------------------------------------------
392 
executeMediaItem(const MediaItem & rItem)393 void MediaWindowBaseImpl::executeMediaItem( const MediaItem& rItem )
394 {
395     const sal_uInt32 nMaskSet = rItem.getMaskSet();
396 
397     // set URL first
398     if( nMaskSet & AVMEDIA_SETMASK_URL )
399         setURL( rItem.getURL() );
400 
401     // set different states next
402     if( nMaskSet & AVMEDIA_SETMASK_TIME )
403         setMediaTime( ::std::min( rItem.getTime(), getDuration() ) );
404 
405     if( nMaskSet & AVMEDIA_SETMASK_LOOP )
406         setPlaybackLoop( rItem.isLoop() );
407 
408     if( nMaskSet & AVMEDIA_SETMASK_MUTE )
409         setMute( rItem.isMute() );
410 
411     if( nMaskSet & AVMEDIA_SETMASK_VOLUMEDB )
412         setVolumeDB( rItem.getVolumeDB() );
413 
414     if( nMaskSet & AVMEDIA_SETMASK_ZOOM )
415         setZoom( rItem.getZoom() );
416 
417     // set play state at last
418     if( nMaskSet & AVMEDIA_SETMASK_STATE )
419     {
420         switch( rItem.getState() )
421         {
422             case( MEDIASTATE_PLAY ):
423             case( MEDIASTATE_PLAYFFW ):
424             {
425 /*
426                 const double fNewRate = ( ( MEDIASTATE_PLAYFFW == rItem.getState() ) ? AVMEDIA_FFW_PLAYRATE : 1.0 );
427 
428                 if( getRate() != fNewRate )
429                     setRate( fNewRate );
430 */
431                 if( !isPlaying() )
432                     start();
433             }
434             break;
435 
436             case( MEDIASTATE_PAUSE ):
437             {
438                 if( isPlaying() )
439                     stop();
440             }
441             break;
442 
443             case( MEDIASTATE_STOP ):
444             {
445                 if( isPlaying() )
446                 {
447                     setMediaTime( 0.0 );
448                     stop();
449                     setMediaTime( 0.0 );
450                 }
451             }
452             break;
453         }
454     }
455 }
456 
457 } // namespace priv
458 } // namespace avmedia
459