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